From: AceVest Date: Wed, 2 Jul 2014 16:12:17 +0000 (+0800) Subject: read partition table X-Git-Tag: 0.3.0~38 X-Git-Url: http://zhaoyanbai.com/repos/man.host.html?a=commitdiff_plain;h=b25a6af460b5124d934547a709c8e0aa6596e5b8;p=kernel.git read partition table --- diff --git a/drivers/ide.c b/drivers/ide.c index 7d6e49e..3fc7d4a 100644 --- a/drivers/ide.c +++ b/drivers/ide.c @@ -94,22 +94,23 @@ void ide_printd() { printd(MPL_IDE, "ide pio_cnt %d dma_cnt %d irq_cnt %d", drv.pio_cnt, drv.dma_cnt, drv.irq_cnt); } -void ide_cmd_out(Dev dev, u32 nsect, u64 sect_nr, u32 cmd) + +void _ide_cmd_out(dev_t dev, u32 sect_cnt, u64 sect_nr, u32 cmd, bool pio) { drv.pio_cnt++; drv.read_mode = cmd; ide_printd(); - outb(0x00, REG_CTL(dev)); + outb(0x00|(pio?0:HD_CTL_NIEN), REG_CTL(dev)); outb(0x40, REG_DEVSEL(dev)); - outb(0, REG_NSECTOR(dev)); // High + outb((u8)((sect_cnt>>8)&0xFF), REG_NSECTOR(dev)); // High outb((u8)((sect_nr>>24)&0xFF), REG_LBAL(dev)); outb((u8)((sect_nr>>32)&0xFF), REG_LBAM(dev)); outb((u8)((sect_nr>>40)&0xFF), REG_LBAH(dev)); - outb((u8)nsect, REG_NSECTOR(dev)); // Low + outb((u8)((sect_cnt>>0)&0xFF), REG_NSECTOR(dev)); // Low outb((u8)((sect_nr>> 0)&0xFF), REG_LBAL(dev)); outb((u8)((sect_nr>> 8)&0xFF), REG_LBAM(dev)); outb((u8)((sect_nr>>16)&0xFF), REG_LBAH(dev)); @@ -117,6 +118,36 @@ void ide_cmd_out(Dev dev, u32 nsect, u64 sect_nr, u32 cmd) outb(cmd, REG_CMD(dev)); } +void ide_wait_ready() +{ + u32 retires = 100; + + do + { + int drq_retires = 100000; + while(!hd_drq(dev) && --drq_retires) + /* do nothing */; + + if(drq_retires != 0) + break; + }while(--retires); + + if(retires == 0) + panic("hard disk is not ready"); +} + + +void ide_cmd_out(dev_t dev, u32 sect_cnt, u64 sect_nr, u32 cmd) +{ + _ide_cmd_out(dev, sect_cnt, sect_nr, cmd, true); +} + +void ide_wait_read(dev_t dev, u32 sect_cnt, u64 sect_nr, char *buf) +{ + _ide_cmd_out(dev, sect_cnt, sect_nr, HD_CMD_READ_EXT, false); + ide_wait_ready(); + insl(REG_DATA(dev), buf, (sect_cnt*512)>>2); +} void dump_pci_ide(); @@ -196,9 +227,10 @@ void ide_irq() void print_ide_identify(const char *buf) { - char *p; - short *ident; - int i; + char *p; + short *ident; + int i, j; + unsigned char c; ident = (short *) buf; @@ -209,9 +241,14 @@ void print_ide_identify(const char *buf) p = (char *) (ident+10); for(i=0; i<20; i++) - { hd_sn[i] = p[i]; + for(j=0; j<20; j+=2) + { + c = hd_sn[j]; + hd_sn[j] = hd_sn[j+1]; + hd_sn[j+1] = c; } + hd_sn[i] = 0; p = (char *) (ident+27); @@ -219,6 +256,12 @@ void print_ide_identify(const char *buf) { hd_model[i] = p[i]; } + for(j=0; j<20; j+=2) + { + c = hd_model[j]; + hd_model[j] = hd_model[j+1]; + hd_model[j+1] = c; + } hd_model[i] = 0; @@ -235,24 +278,11 @@ void print_ide_identify(const char *buf) void ide_read_identify() { - outb(0x02, REG_CTL(0)); - outb(0x40, REG_DEVSEL(dev)); + outb(HD_CTL_NIEN, REG_CTL(0)); + outb(0x00, REG_DEVSEL(dev)); outb(HD_CMD_IDENTIFY, REG_CMD(dev)); - u32 retires = 100; - - do - { - int drq_retires = 100000; - while(!hd_drq(dev) && --drq_retires) - /* do nothing */; - - if(drq_retires != 0) - break; - }while(--retires); - - if(retires == 0) - panic("hard disk is not ready"); + ide_wait_ready(); insl(REG_DATA(0), buf, 512>>2); print_ide_identify(buf); @@ -268,7 +298,6 @@ unsigned long gprdt = 0; inb(HD_CHL0_CTL_BASE); \ } - void ide_dma_pci_lba48() { drv.dma_cnt ++; @@ -344,6 +373,13 @@ void ide_dma_pci_lba48() inb(drv.bus_status); } +typedef struct { + unsigned int a; + unsigned int b; + unsigned int lba; + unsigned int sect_cnt; +} hd_part_t ; + void ide_init() { memset((void *)&drv, 0, sizeof(drv)); @@ -353,4 +389,16 @@ void ide_init() ide_read_identify(); ide_printd(); #endif + ide_wait_read(0, 1, 0, data); + + hd_part_t *p = (hd_part_t *)(data+PARTITION_TABLE_OFFSET); + int i; + for(i=0; i<4; ++i) + { + printk(" Partition %d LBA %d SectCnt %d\n", i, p->lba, p->sect_cnt); + p++; + } + + u16_t sig = *((u16_t *) (data+510)); + printk("IDE_INIT______READ %04x\n", sig); } diff --git a/drivers/ide.h b/drivers/ide.h index 99a8252..ef85db4 100644 --- a/drivers/ide.h +++ b/drivers/ide.h @@ -53,11 +53,11 @@ extern unsigned int HD_CHL1_CTL_BASE; #define HD_CMD_IDENTIFY 0xEC #define HD_CTL 0 -#define HD_CTL_NORETRY 0x80 /* disable access retry */ -#define HD_CTL_NOECC 0x40 /* disable ecc retry */ -#define HD_CTL_EIGHTHEADS 0x08 /* more than 8 heads */ -#define HD_CTL_RESET 0x04 /* reset controller */ -#define HD_CTL_DISABLE_INT 0x02 /* disable interrupts */ +#define HD_CTL_HOB 0x80 /* high order byte (LBA-48bit) */ +//#define HD_CTL_NOECC 0x40 /* disable ecc retry */ +//#define HD_CTL_EIGHTHEADS 0x08 /* more than 8 heads */ +#define HD_CTL_SRST 0x04 /* soft reset controller */ +#define HD_CTL_NIEN 0x02 /* disable interrupts */ #define HD_GET_CHL(dev) (0) /* only support channel 0 */ #define HD_GET_DEV(dev) (0) /* only support one hard disk */ @@ -116,3 +116,7 @@ extern unsigned int HD_CHL1_CTL_BASE; #define PCI_IDE_STATUS_DRV1 0x40 #define PCI_IDE_STATUS_SIMPLEX 0x80 #define PCI_IDE_PRDT 4 + + + +#define PARTITION_TABLE_OFFSET 0x1BE diff --git a/drivers/keyboard.c b/drivers/keyboard.c index 54354b2..48d6013 100644 --- a/drivers/keyboard.c +++ b/drivers/keyboard.c @@ -81,8 +81,7 @@ void kbd_handler(unsigned int irq, pt_regs_t * regs, void *dev_id) if(scan_code == 0x43); // F9 if(scan_code == 0x44); // F10 - if(scan_code == 0x57) // F11 - poweroff(); + if(scan_code == 0x57); // F11 if(scan_code == 0x58) // F12 vga_dbg_toggle(); diff --git a/drivers/vga.c b/drivers/vga.c index 501d4f6..8964ecc 100644 --- a/drivers/vga.c +++ b/drivers/vga.c @@ -42,6 +42,7 @@ typedef struct { } vga_screen_t; #define VGA_MAX_SCREEN_CNT 4 +unsigned int vga_screen_cnt() {return VGA_MAX_SCREEN_CNT;} vga_screen_t vga_screen[VGA_MAX_SCREEN_CNT] = { { 0, diff --git a/fs/ext2.c b/fs/ext2.c index 009bc20..bc0df52 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -18,7 +18,7 @@ unsigned int ext2_start_sect; Inode ext2_root_inode; -void hd_read(Dev dev, u64 sect_nr, void *buf, u32 count) { /*TODO*/ } +void hd_read(dev_t dev, u64 sect_nr, void *buf, u32 count) { /*TODO*/ } void ext2_read_block(int block_id, char *buf); static void ext2_print_group_descriptor(pGroupDesc p); static void ext2_print_inode(pInode p); diff --git a/fs/fs.c b/fs/fs.c index d3d568f..8d7d41e 100644 --- a/fs/fs.c +++ b/fs/fs.c @@ -15,15 +15,14 @@ #include #define SECT_SIZE 512 -File file_table[NR_FILES] - __attribute__ ((__aligned__(PAGE_SIZE))) = {{0,},}; +File file_table[NR_FILES] __attribute__ ((__aligned__(PAGE_SIZE))) = {{0,},}; extern unsigned int ext2_start_sect; -void hd_read(Dev dev, u64 sect_nr, void *buf, u32 count); -void init_file_table(); -void save_boot_part(int n, pPartition p, u32 base_sect); -void read_ext_part(u32 base, u32 offset); +void hd_read(dev_t dev, u64 sect_nr, void *buf, u32 count); +void init_file_table(); +void save_boot_part(int n, pPartition p, u32 base_sect); +void read_ext_part(u32 base, u32 offset); -void setup_fs() +void setup_fs() { int i, minor; pPartition p; @@ -137,7 +136,7 @@ void setup_fs() #endif } -void init_file_table() +void init_file_table() { int i; @@ -149,7 +148,7 @@ void init_file_table() } } -void save_boot_part(int n, pPartition p, u32 base_sect) +void save_boot_part(int n, pPartition p, u32 base_sect) { if(p->Type == 0x05) panic("partition should not be extended"); @@ -161,7 +160,7 @@ void save_boot_part(int n, pPartition p, u32 base_sect) } static unsigned int ext_part = 5; -void read_ext_part(u32 base, u32 offset) +void read_ext_part(u32 base, u32 offset) { unsigned char *buf; pPartition p; @@ -195,4 +194,3 @@ void read_ext_part(u32 base, u32 offset) free_virt_pages(buf); } - diff --git a/include/printk.h b/include/printk.h index 5d70088..eaa3f13 100644 --- a/include/printk.h +++ b/include/printk.h @@ -16,6 +16,7 @@ #pragma once +void switch_printk_screen(); int printk(char *fmtstr, ...); int printd(unsigned int line, const char *fmtstr, ...); diff --git a/include/system.h b/include/system.h index acd7834..3dca32f 100644 --- a/include/system.h +++ b/include/system.h @@ -154,26 +154,26 @@ typedef struct pt_regs u16 ss, _ss; } __attribute__((packed)) pt_regs_t; -typedef unsigned long Dev, *pDev; +typedef unsigned long dev_t; typedef struct system { - u32 mmap_addr; - u32 mmap_size; // Byte + u32 mmap_addr; + u32 mmap_size; // Byte - u32 mm_lower; // KB - u32 mm_upper; // KB - u64 mm_size; // Byte + u32 mm_lower; // KB + u32 mm_upper; // KB + u64 mm_size; // Byte - u32 page_count; - pPage page_map; - u32 page_bitmap; + u32 page_count; + pPage page_map; + u32 page_bitmap; u32 *page_dir; - u32 *pte_start; - u32 *pte_end; + u32 *pte_start; + u32 *pte_end; - u32 kernel_end; + u32 kernel_end; // +-------+-------+-------+-------+ // | drive | part1 | part2 | part3 | @@ -181,13 +181,13 @@ typedef struct system // Partition numbers always start from zero. // Unused partition bytes must be set to 0xFF. // More Infomation see 'info multiboot' - u32 boot_device; + u32 boot_device; - Dev root_dev; + dev_t root_dev; #define CMD_LINE_SIZE 128 - char *cmdline; + char *cmdline; - u32 debug; + u32 debug; } System, *pSystem; extern System system; diff --git a/kernel/pci.c b/kernel/pci.c index a506658..87052f7 100644 --- a/kernel/pci.c +++ b/kernel/pci.c @@ -402,6 +402,8 @@ pci_info_t pci_info[] = { const char *pci_get_info(unsigned int classcode, unsigned int progif) { pci_info_t *p = pci_info; + const char *s = 0; + while(p->code != 0 || p->flag != 0 || p->info != 0) { unsigned long code = classcode; @@ -413,10 +415,19 @@ const char *pci_get_info(unsigned int classcode, unsigned int progif) } if(p->code == code) - return p->info; + { + if(p->flag == 0) + { + return p->info; + } + else + { + s = p->info; + } + } p++; } - return 0; + return s; } diff --git a/kernel/printk.c b/kernel/printk.c index 7839b8b..5818ee8 100644 --- a/kernel/printk.c +++ b/kernel/printk.c @@ -18,13 +18,21 @@ extern void vga_puts(unsigned int nr, const char *buf, unsigned char color); extern void vga_dbg_puts(unsigned long line, const char *buf, unsigned char color); +unsigned int printk_screen_nr = 0; + +extern unsigned int vga_screen_cnt(); +void switch_printk_screen() +{ + printk_screen_nr++; + printk_screen_nr %= vga_screen_cnt(); +} + char pkbuf[1024]; -extern int bvga; int printk(const char *fmtstr, ...) { char *args = (char*)(((char*)&fmtstr)+4); vsprintf(pkbuf, fmtstr, args); - vga_puts(0, pkbuf,0x2); + vga_puts(printk_screen_nr, pkbuf, 0x2); return 0; } @@ -33,6 +41,6 @@ int printd(unsigned int line, const char *fmtstr, ...) { char *args = (char*)(((char*)&fmtstr)+4); vsprintf(pdbuf, fmtstr, args); - vga_dbg_puts(line, pdbuf,0x7); + vga_dbg_puts(line, pdbuf, 0x7); return 0; } diff --git a/kernel/setup.c b/kernel/setup.c index df12110..fc42ac6 100644 --- a/kernel/setup.c +++ b/kernel/setup.c @@ -84,9 +84,9 @@ void setup_kernel() setup_tasks(); setup_irqs(); - + switch_printk_screen(); setup_pci(); - + switch_printk_screen(); void ide_init(); ide_init();