?? 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.*//** 本程序是底層硬盤中斷輔助程序。主要用于掃描請求列表,使用中斷在函數之間跳轉。* 由于所有的函數都是在中斷里調用的,所以這些函數不可以睡眠。請特別注意。* 由Drew Eckhardt 修改,利用CMOS 信息檢測硬盤數。*/#include <linux/config.h> // 內核配置頭文件。定義鍵盤語言和硬盤類型(HD_TYPE)可選項。#include <linux/sched.h> // 調度程序頭文件,定義了任務結構task_struct、初始任務0 的數據,// 還有一些有關描述符參數設置和獲取的嵌入式匯編函數宏語句。#include <linux/fs.h> // 文件系統頭文件。定義文件表結構(file,buffer_head,m_inode 等)。#include <linux/kernel.h> // 內核頭文件。含有一些內核常用函數的原形定義。#include <linux/hdreg.h> // 硬盤參數頭文件。定義訪問硬盤寄存器端口,狀態碼,分區表等信息。#include <asm/system.h> // 系統頭文件。定義了設置或修改描述符/中斷門等的嵌入式匯編宏。#include <asm/io.h> // io 頭文件。定義硬件端口輸入/輸出宏匯編語句。#include <asm/segment.h> // 段操作頭文件。定義了有關段寄存器操作的嵌入式匯編函數。#define MAJOR_NR 3 // 硬盤主設備號是3。#include "blk.h" // 塊設備頭文件。定義請求數據結構、塊設備數據結構和宏函數等信息。#define CMOS_READ(addr) ({ \ // 讀CMOS 參數宏函數。outb_p (0x80 | addr, 0x70);inb_p (0x71);})/* Max read/write errors/sector */#define MAX_ERRORS 7 // 讀/寫一個扇區時允許的最多出錯次數。#define MAX_HD 2 // 系統支持的最多硬盤數。 static void recal_intr (void); // 硬盤中斷程序在復位操作時會調用的重新校正函數(287 行)。 static int recalibrate = 1; // 重新校正標志。 static int reset = 1; // 復位標志。/** This struct defines the HD's and their types.*//* 下面結構定義了硬盤參數及類型 */// 各字段分別是磁頭數、每磁道扇區數、柱面數、寫前預補償柱面號、磁頭著陸區柱面號、控制字節。 struct hd_i_struct { int head, sect, cyl, wpcom, lzone, ctl; };#ifdef HD_TYPE // 如果已經在include/linux/config.h 中定義了HD_TYPE…struct hd_i_struct hd_info[] ={HD_TYPE}; // 取定義好的參數作為hd_info[]的數據。#define NR_HD ((sizeof (hd_info))/(sizeof (struct hd_i_struct))) // 計算硬盤數。#else // 否則,都設為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// 定義硬盤分區結構。給出每個分區的物理起始扇區號、分區扇區總數。// 其中5 的倍數處的項(例如hd[0]和hd[5]等)代表整個硬盤中的參數。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 中取數據。#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' *//* 下面該函數只在初始化時被調用一次。用靜態變量callable 作為可調用標志。*/// 該函數的參數由初始化程序init/main.c 的init 子程序設置為指向0x90080 處,此處存放著setup.s// 程序從BIOS 取得的2 個硬盤的基本參數表(32 字節)。硬盤參數表信息參見下面列表后的說明。// 本函數主要功能是讀取CMOS 和硬盤參數表信息,用于設置硬盤分區結構hd,并加載RAM 虛擬盤和// 根文件系統。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,當運行該函數時將其設置為0,使本函數只能執行一次。 if (!callable) return -1; callable = 0;// 如果沒有在config.h 中定義硬盤參數,就從0x90080 處讀入。#ifndef HD_TYPE for (drive = 0; drive < 2; drive++) { hd_info[drive].cyl = *(unsigned short *) BIOS; // 柱面數。 hd_info[drive].head = *(unsigned char *) (2 + BIOS); // 磁頭數。 hd_info[drive].wpcom = *(unsigned short *) (5 + BIOS); // 寫前預補償柱面號。 hd_info[drive].ctl = *(unsigned char *) (8 + BIOS); // 控制字節。 hd_info[drive].lzone = *(unsigned short *) (12 + BIOS); // 磁頭著陸區柱面號。 hd_info[drive].sect = *(unsigned char *) (14 + BIOS); // 每磁道扇區數。 BIOS += 16; // 每個硬盤的參數表長16 字節,這里BIOS 指向下一個表。 }// setup.s 程序在取BIOS 中的硬盤參數表信息時,如果只有1 個硬盤,就會將對應第2 個硬盤的// 16 字節全部清零。因此這里只要判斷第2 個硬盤柱面數是否為0 就可以知道有沒有第2 個硬盤了。 if (hd_info[1].cyl) NR_HD = 2; // 硬盤數置為2。 else NR_HD = 1;#endif// 設置每個硬盤的起始扇區號和扇區總數。其中編號i*5 含義參見本程序后的有關說明。 for (i = 0; i < NR_HD; i++) { hd[i * 5].start_sect = 0; // 硬盤起始扇區號。 hd[i * 5].nr_sects = hd_info[i].head * hd_info[i].sect * hd_info[i].cyl; // 硬盤總扇區數。 }/*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 有關硬盤的信息有些懷疑:可能會出現這樣的情況,我們有一塊SCSI/ESDI/等的* 控制器,它是以ST-506 方式與BIOS 兼容的,因而會出現在我們的BIOS 參數表中,但卻又不* 是寄存器兼容的,因此這些參數在CMOS 中又不存在。* 另外,我們假設ST-506 驅動器(如果有的話)是系統中的基本驅動器,也即以驅動器1 或2* 出現的驅動器。* 第1 個驅動器參數存放在CMOS 字節0x12 的高半字節中,第2 個存放在低半字節中。該4 位字節* 信息可以是驅動器類型,也可能僅是0xf。0xf 表示使用CMOS 中0x19 字節作為驅動器1 的8 位* 類型字節,使用CMOS 中0x1A 字節作為驅動器2 的類型字節。* 總之,一個非零值意味著我們有一個AT 控制器硬盤兼容的驅動器。*/// 這里根據上述原理來檢測硬盤到底是否是AT 控制器兼容的。有關CMOS 信息請參見4.2.3.1 節。 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 控制器兼容的,硬盤數據結構清零。// 若NR_HD=1,則將第2 個硬盤的參數清零。 for (i = NR_HD; i < 2; i++) { hd[i * 5].start_sect = 0; hd[i * 5].nr_sects = 0; }// 讀取每一個硬盤上第1 塊數據(第1 個扇區有用),獲取其中的分區表信息。// 首先利用函數bread()讀硬盤第1 塊數據(fs/buffer.c,267),參數中的0x300 是硬盤的主設備號// (參見列表后的說明)。然后根據硬盤頭1 個扇區位置0x1fe 處的兩個字節是否為'55AA'來判斷// 該扇區中位于0x1BE 開始的分區表是否有效。最后將分區表信息放入硬盤分區數據結構hd 中。 for (drive = 0; drive < NR_HD; drive++) { if (!(bh = bread (0x300 + drive * 5, 0))) { // 0x300, 0x305 邏輯設備號。 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) { // 判斷硬盤信息有效標志'55AA'。 printk ("Bad partition table on drive %d\n\r", drive); panic (""); } p = 0x1BE + (void *) bh->b_data; // 分區表位于硬盤第1 扇區的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); // 釋放為存放硬盤塊而申請的內存緩沖區頁。 } if (NR_HD) // 如果有硬盤存在并且已讀入分區表,則打印分區表正常信息。 printk ("Partition table%s ok.\n\r", (NR_HD > 1) ? "s" : ""); rd_load (); // 加載(創建)RAMDISK(kernel/blk_drv/ramdisk.c,71)。 mount_root (); // 安裝根文件系統(fs/super.c,242)。 return (0);}//// 判斷并循環等待驅動器就緒。// 讀硬盤控制器狀態寄存器端口HD_STATUS(0x1f7),并循環檢測驅動器就緒比特位和控制器忙位。static int controller_ready (void){ int retries = 10000; while (--retries && (inb_p (HD_STATUS) & 0xc0) != 0x40); return (retries); // 返回等待循環的次數。}//// 檢測硬盤執行命令后的狀態。(win_表示溫切斯特硬盤的縮寫)// 讀取狀態寄存器中的命令執行結果狀態。返回0 表示正常,1 出錯。如果執行命令錯,// 則再讀錯誤寄存器HD_ERROR(0x1f1)。static int win_result (void)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -