?? hd.c
字號:
/** linux/kernel/hd.c** (C) 1991 Linus Torvalds*//** This is the low-level hd interrupt support. It traverses the* request-list, using interrupts to jump between functions. As* all the functions are called within interrupts, we may not* sleep. Special care is recommended.** modified by Drew Eckhardt to check nr of hd's from the CMOS.*//** 本程序是底層硬盤中斷輔助程序。主要用于掃描請求列表,使用中斷在函數(shù)之間跳轉(zhuǎn)。* 由于所有的函數(shù)都是在中斷里調(diào)用的,所以這些函數(shù)不可以睡眠。請?zhí)貏e注意。* 由Drew Eckhardt 修改,利用CMOS 信息檢測硬盤數(shù)。*/#include <linux/config.h> // 內(nèi)核配置頭文件。定義鍵盤語言和硬盤類型(HD_TYPE)可選項。#include <linux/sched.h> // 調(diào)度程序頭文件,定義了任務(wù)結(jié)構(gòu)task_struct、初始任務(wù)0 的數(shù)據(jù),// 還有一些有關(guān)描述符參數(shù)設(shè)置和獲取的嵌入式匯編函數(shù)宏語句。#include <linux/fs.h> // 文件系統(tǒng)頭文件。定義文件表結(jié)構(gòu)(file,buffer_head,m_inode 等)。#include <linux/kernel.h> // 內(nèi)核頭文件。含有一些內(nèi)核常用函數(shù)的原形定義。#include <linux/hdreg.h> // 硬盤參數(shù)頭文件。定義訪問硬盤寄存器端口,狀態(tài)碼,分區(qū)表等信息。#include <asm/system.h> // 系統(tǒng)頭文件。定義了設(shè)置或修改描述符/中斷門等的嵌入式匯編宏。#include <asm/io.h> // io 頭文件。定義硬件端口輸入/輸出宏匯編語句。#include <asm/segment.h> // 段操作頭文件。定義了有關(guān)段寄存器操作的嵌入式匯編函數(shù)。#define MAJOR_NR 3 // 硬盤主設(shè)備號是3。#include "blk.h" // 塊設(shè)備頭文件。定義請求數(shù)據(jù)結(jié)構(gòu)、塊設(shè)備數(shù)據(jù)結(jié)構(gòu)和宏函數(shù)等信息。#define CMOS_READ(addr) ({ \ // 讀CMOS 參數(shù)宏函數(shù)。outb_p (0x80 | addr, 0x70);inb_p (0x71);})/* Max read/write errors/sector */#define MAX_ERRORS 7 // 讀/寫一個扇區(qū)時允許的最多出錯次數(shù)。#define MAX_HD 2 // 系統(tǒng)支持的最多硬盤數(shù)。 static void recal_intr (void); // 硬盤中斷程序在復(fù)位操作時會調(diào)用的重新校正函數(shù)(287 行)。 static int recalibrate = 1; // 重新校正標(biāo)志。 static int reset = 1; // 復(fù)位標(biāo)志。/** This struct defines the HD's and their types.*//* 下面結(jié)構(gòu)定義了硬盤參數(shù)及類型 */// 各字段分別是磁頭數(shù)、每磁道扇區(qū)數(shù)、柱面數(shù)、寫前預(yù)補(bǔ)償柱面號、磁頭著陸區(qū)柱面號、控制字節(jié)。 struct hd_i_struct { int head, sect, cyl, wpcom, lzone, ctl; };#ifdef HD_TYPE // 如果已經(jīng)在include/linux/config.h 中定義了HD_TYPE…struct hd_i_struct hd_info[] ={HD_TYPE}; // 取定義好的參數(shù)作為hd_info[]的數(shù)據(jù)。#define NR_HD ((sizeof (hd_info))/(sizeof (struct hd_i_struct))) // 計算硬盤數(shù)。#else // 否則,都設(shè)為0 值。struct hd_i_struct hd_info[] ={ { 0, 0, 0, 0, 0, 0} , { 0, 0, 0, 0, 0, 0}};static int NR_HD = 0;#endif// 定義硬盤分區(qū)結(jié)構(gòu)。給出每個分區(qū)的物理起始扇區(qū)號、分區(qū)扇區(qū)總數(shù)。// 其中5 的倍數(shù)處的項(例如hd[0]和hd[5]等)代表整個硬盤中的參數(shù)。static struct hd_struct{ long start_sect; long nr_sects;}hd[5 * MAX_HD] ={ { 0, 0},};// 讀端口port,共讀nr 字,保存在buf 中。#define port_read(port,buf,nr) \__asm__( "cld;rep;insw":: "d" (port), "D" (buf), "c" (nr): "cx", "di")// 寫端口port,共寫nr 字,從buf 中取數(shù)據(jù)。#define port_write(port,buf,nr) \__asm__( "cld;rep;outsw":: "d" (port), "S" (buf), "c" (nr): "cx", "si")extern void hd_interrupt (void);extern void rd_load (void);/* This may be used only once, enforced by 'static int callable' *//* 下面該函數(shù)只在初始化時被調(diào)用一次。用靜態(tài)變量callable 作為可調(diào)用標(biāo)志。*/// 該函數(shù)的參數(shù)由初始化程序init/main.c 的init 子程序設(shè)置為指向0x90080 處,此處存放著setup.s// 程序從BIOS 取得的2 個硬盤的基本參數(shù)表(32 字節(jié))。硬盤參數(shù)表信息參見下面列表后的說明。// 本函數(shù)主要功能是讀取CMOS 和硬盤參數(shù)表信息,用于設(shè)置硬盤分區(qū)結(jié)構(gòu)hd,并加載RAM 虛擬盤和// 根文件系統(tǒng)。int sys_setup (void *BIOS){ static int callable = 1; int i, drive; unsigned char cmos_disks; struct partition *p; struct buffer_head *bh;// 初始化時callable=1,當(dāng)運(yùn)行該函數(shù)時將其設(shè)置為0,使本函數(shù)只能執(zhí)行一次。 if (!callable) return -1; callable = 0;// 如果沒有在config.h 中定義硬盤參數(shù),就從0x90080 處讀入。#ifndef HD_TYPE for (drive = 0; drive < 2; drive++) { hd_info[drive].cyl = *(unsigned short *) BIOS; // 柱面數(shù)。 hd_info[drive].head = *(unsigned char *) (2 + BIOS); // 磁頭數(shù)。 hd_info[drive].wpcom = *(unsigned short *) (5 + BIOS); // 寫前預(yù)補(bǔ)償柱面號。 hd_info[drive].ctl = *(unsigned char *) (8 + BIOS); // 控制字節(jié)。 hd_info[drive].lzone = *(unsigned short *) (12 + BIOS); // 磁頭著陸區(qū)柱面號。 hd_info[drive].sect = *(unsigned char *) (14 + BIOS); // 每磁道扇區(qū)數(shù)。 BIOS += 16; // 每個硬盤的參數(shù)表長16 字節(jié),這里BIOS 指向下一個表。 }// setup.s 程序在取BIOS 中的硬盤參數(shù)表信息時,如果只有1 個硬盤,就會將對應(yīng)第2 個硬盤的// 16 字節(jié)全部清零。因此這里只要判斷第2 個硬盤柱面數(shù)是否為0 就可以知道有沒有第2 個硬盤了。 if (hd_info[1].cyl) NR_HD = 2; // 硬盤數(shù)置為2。 else NR_HD = 1;#endif// 設(shè)置每個硬盤的起始扇區(qū)號和扇區(qū)總數(shù)。其中編號i*5 含義參見本程序后的有關(guān)說明。 for (i = 0; i < NR_HD; i++) { hd[i * 5].start_sect = 0; // 硬盤起始扇區(qū)號。 hd[i * 5].nr_sects = hd_info[i].head * hd_info[i].sect * hd_info[i].cyl; // 硬盤總扇區(qū)數(shù)。 }/*We querry CMOS about hard disks : it could be thatwe have a SCSI/ESDI/etc controller that is BIOScompatable with ST-506, and thus showing up in ourBIOS table, but not register compatable, and thereforenot present in CMOS.Furthurmore, we will assume that our ST-506 drives<if any> are the primary drives in the system, andthe ones reflected as drive 1 or 2.The first drive is stored in the high nibble of CMOSbyte 0x12, the second in the low nibble. This will beeither a 4 bit drive type or 0xf indicating use byte 0x19for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS.Needless to say, a non-zero value means we havean AT controller hard disk for that drive.*//** 我們對CMOS 有關(guān)硬盤的信息有些懷疑:可能會出現(xiàn)這樣的情況,我們有一塊SCSI/ESDI/等的* 控制器,它是以ST-506 方式與BIOS 兼容的,因而會出現(xiàn)在我們的BIOS 參數(shù)表中,但卻又不* 是寄存器兼容的,因此這些參數(shù)在CMOS 中又不存在。* 另外,我們假設(shè)ST-506 驅(qū)動器(如果有的話)是系統(tǒng)中的基本驅(qū)動器,也即以驅(qū)動器1 或2* 出現(xiàn)的驅(qū)動器。* 第1 個驅(qū)動器參數(shù)存放在CMOS 字節(jié)0x12 的高半字節(jié)中,第2 個存放在低半字節(jié)中。該4 位字節(jié)* 信息可以是驅(qū)動器類型,也可能僅是0xf。0xf 表示使用CMOS 中0x19 字節(jié)作為驅(qū)動器1 的8 位* 類型字節(jié),使用CMOS 中0x1A 字節(jié)作為驅(qū)動器2 的類型字節(jié)。* 總之,一個非零值意味著我們有一個AT 控制器硬盤兼容的驅(qū)動器。*/// 這里根據(jù)上述原理來檢測硬盤到底是否是AT 控制器兼容的。有關(guān)CMOS 信息請參見4.2.3.1 節(jié)。 if ((cmos_disks = CMOS_READ (0x12)) & 0xf0) if (cmos_disks & 0x0f) NR_HD = 2; else NR_HD = 1; else NR_HD = 0;// 若NR_HD=0,則兩個硬盤都不是AT 控制器兼容的,硬盤數(shù)據(jù)結(jié)構(gòu)清零。// 若NR_HD=1,則將第2 個硬盤的參數(shù)清零。 for (i = NR_HD; i < 2; i++) { hd[i * 5].start_sect = 0; hd[i * 5].nr_sects = 0; }// 讀取每一個硬盤上第1 塊數(shù)據(jù)(第1 個扇區(qū)有用),獲取其中的分區(qū)表信息。// 首先利用函數(shù)bread()讀硬盤第1 塊數(shù)據(jù)(fs/buffer.c,267),參數(shù)中的0x300 是硬盤的主設(shè)備號// (參見列表后的說明)。然后根據(jù)硬盤頭1 個扇區(qū)位置0x1fe 處的兩個字節(jié)是否為'55AA'來判斷// 該扇區(qū)中位于0x1BE 開始的分區(qū)表是否有效。最后將分區(qū)表信息放入硬盤分區(qū)數(shù)據(jù)結(jié)構(gòu)hd 中。 for (drive = 0; drive < NR_HD; drive++) { if (!(bh = bread (0x300 + drive * 5, 0))) { // 0x300, 0x305 邏輯設(shè)備號。 printk ("Unable to read partition table of drive %d\n\r", drive); panic (""); } if (bh->b_data[510] != 0x55 || (unsigned char) bh->b_data[511] != 0xAA) { // 判斷硬盤信息有效標(biāo)志'55AA'。 printk ("Bad partition table on drive %d\n\r", drive); panic (""); } p = 0x1BE + (void *) bh->b_data; // 分區(qū)表位于硬盤第1 扇區(qū)的0x1BE 處。 for (i = 1; i < 5; i++, p++) { hd[i + 5 * drive].start_sect = p->start_sect; hd[i + 5 * drive].nr_sects = p->nr_sects; } brelse (bh); // 釋放為存放硬盤塊而申請的內(nèi)存緩沖區(qū)頁。 } if (NR_HD) // 如果有硬盤存在并且已讀入分區(qū)表,則打印分區(qū)表正常信息。 printk ("Partition table%s ok.\n\r", (NR_HD > 1) ? "s" : ""); rd_load (); // 加載(創(chuàng)建)RAMDISK(kernel/blk_drv/ramdisk.c,71)。 mount_root (); // 安裝根文件系統(tǒng)(fs/super.c,242)。 return (0);}//// 判斷并循環(huán)等待驅(qū)動器就緒。// 讀硬盤控制器狀態(tài)寄存器端口HD_STATUS(0x1f7),并循環(huán)檢測驅(qū)動器就緒比特位和控制器忙位。static int controller_ready (void){ int retries = 10000; while (--retries && (inb_p (HD_STATUS) & 0xc0) != 0x40); return (retries); // 返回等待循環(huán)的次數(shù)。}//// 檢測硬盤執(zhí)行命令后的狀態(tài)。(win_表示溫切斯特硬盤的縮寫)// 讀取狀態(tài)寄存器中的命令執(zhí)行結(jié)果狀態(tài)。返回0 表示正常,1 出錯。如果執(zhí)行命令錯,// 則再讀錯誤寄存器HD_ERROR(0x1f1)。static int win_result (void)
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -