?? hd.c
字號:
#include <fairysky/types.h>#include <fairysky/defs.h>#include <asm/system.h>#include <asm/irq.h>#include <asm/io.h>#include <fairysky/hd.h>#include <fairysky/kernel.h>#ifdef WINDOWS_CYGWIN#define HD_HANDLER hd_handler#define HD_INTERRUPT hd_interrupt#else#define HD_HANDLER _hd_handler#define HD_INTERRUPT _hd_interrupt#endifHD_INFO hd_info;struct hd_request_struct hd0_request;#define port_read(port, buf, nr) \ __asm__("cld; rep; insw"::"d" (port),"D" (buf),"c" (nr))#define port_write(port, buf, nr) \ __asm__("cld; rep; outsw"::"d" (port),"S" (buf),"c" (nr))#define HD_CMD_PORT 0x03f6#define HD_DATA_PORT 0x01f0#define HD_ERROR_PORT 0x01f1#define HD_NSECTOR_PORT 0x01f2#define HD_SECTOR_PORT 0x01f3#define HD_LCLY_PORT 0x01f4#define HD_HCLY_PORT 0x01f5#define HD_MODE_DEV_HEAD_PORT 0x01f6#define HD_STATE_PORT 0x01f7#define HD_COMMAND_PORT 0x01f7#define HD_ERR_STATE 0x01#define HD_INDEX_STATE 0x02#define HD_ECC_STATE 0x04#define HD_DRQ_STATE 0x08#define HD_SEEK_STATE 0x10#define HD_WRERR_STATE 0x20#define HD_READY_STATE 0x40#define HD_BUSY_STATE 0x80#define HD_CHS_MODE 0#define HD_LBA_MODE 1#define HD_CMD_READ 0x00100000#define HD_CMD_WRITE 0x00110000extern void hd_out(u8 driver, u8 head, u8 begin_sector, u8 sector_count, u16 cylinder_nr, u8 hd_cmd, void (* hd_intr)(void));s32 sys_setup(){ hd_out(0, 0, 1, 2, 1, HD_READ, NULL);}static int hd_out_result(){ int i = inb_p(HD_STATUS); if ((i & (BUSY_STAT | READY_STAT | WRERR_STAT | SEEK_STAT | ERR_STAT)) == (READY_STAT | SEEK_STAT)) { return 0; } if (i & 1) { i = inb(HD_ERROR); } return 1;}static int hd_ready(){ int retries = 10000; while (--retries && (inb_p(HD_STATUS) & 0XC0) != 0X40) { ; } return retries;}void HD_HANDLER(){ printk("\n\nHD_HANDLER\n\n"); outb_p(EOI, PORT_8259S); if (hd_out_result()) { panic("read hd error\n"); return; } //port_read(HD_DATA, hd0_request.buffer, 256); //print_hex((void *)buf, 128); __asm__ __volatile__( "cld \n\t" "rep insw \n\t" ::"D"(hd0_request.buffer), "dx"(0x1f0), "cx"(256) ); print_hex((void *)hd0_request.buffer, 128); //panic("HD_HANDLER");}void hd_out(u8 driver, u8 head, u8 begin_sector, u8 sector_count, u16 cylinder_nr, u8 hd_cmd, void (* hd_intr)(void)){ s16 port; int i; u8 *ptmp; if (driver > 1 || head > 15) { panic("Try to contrl bad sector"); } if (hd_ready() == 0) { panic("HD controller not ready"); } outb(hd_info.hd_ctl, HD_CMD); // outb(0xeb, HD_CMD);//IO Delay 讓CPU等待一段時間,因為硬盤的速度比CPU要慢很多 // outb(0, HD_CMD); outb(hd_info.hd_wpcom >> 2, HD_ERROR); outb(sector_count, HD_NSECTOR); //讀寫扇區總數 outb(begin_sector, HD_SECTOR); //起始扇區 outb(cylinder_nr, HD_LCYL); //柱面號低8位 outb(cylinder_nr >> 8, HD_HCYL); //柱面號高8位 outb_p(0xE0 | (driver << 4) | head, HD_CURRENT); //驅動器號+磁頭號 outb(hd_cmd, HD_STATUS); //硬盤控制命令 /* ptmp = index_to_vaddr(get_free_page()); memset(ptmp, 1, 4096); printk("ptmp:%XH\n", ptmp); hd0_request.buffer = ptmp; __asm__ __volatile__( "cld \n\t" "rep insw \n\t" //::"D"(hd0_request.buffer), ::"D"(ptmp), "dx"(0x1f0), "cx"(256) ); print_hex((void *)ptmp, 128); */}extern void HD_INTERRUPT(void);int init_hd(){ u8 *p; u8 *ptmp; hd_info.hd_cyl = *(u16 *)(pparams->BIOS); hd_info.hd_head = *(u8 *)(pparams->BIOS + 2); hd_info.hd_wpcom = *(u16 *)(pparams->BIOS + 5); hd_info.hd_ctl = *(u8 *)(pparams->BIOS + 8); hd_info.hd_lzone = *(u16 *)(pparams->BIOS + 12); hd_info.hd_sect = *(u8 *)(pparams->BIOS + 14); /* printk("hd_cyl:%d\n", hd_info.hd_cyl); printk("hd_head:%d\n", hd_info.hd_head); printk("hd_wpcom:%d\n", hd_info.hd_wpcom); printk("hd_ctl:%d\n", hd_info.hd_ctl); printk("hd_lzone:%d\n", hd_info.hd_lzone); printk("hd_sect:%d\n", hd_info.hd_sect); */ set_trap_gate(0x2E, HD_INTERRUPT); outb_p(inb_p(0x21) & 0xFB, PORT_8259M + 1); outb_p(inb_p(0xA1) & 0xBF, PORT_8259S + 1); //outb(inb(0x21) & 0xFB, PORT_8259M + 1); //outb(inb(0xA1) & 0xBF, PORT_8259S + 1); ptmp = p = index_to_vaddr(get_free_page()); memset(p, 1, 4096); hd0_request.device=0; hd0_request.block=0; hd0_request.buffer=p; printk("hd0_request.buffer:%XH\n", hd0_request.buffer); //hd_read(); hd_out(0, //driver 0, //head 0, //begin_sector 1, //sector_count 0, //cylinder_nr HD_READ, //hd_cmd NULL); //print_hex((void *)p, 128);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -