From: acevest Date: Sat, 14 Oct 2023 12:03:57 +0000 (+0800) Subject: 硬盘的设备号从驱动器细化到分区 X-Git-Url: http://zhaoyanbai.com/repos/%22/xml/v3/mem/static/gitweb.js?a=commitdiff_plain;h=fad6a4867b51e40c1cec511926f4c3fc70ac31fc;p=kernel.git 硬盘的设备号从驱动器细化到分区 --- diff --git a/boot/cmdline.c b/boot/cmdline.c index 4b6fd28..534191d 100644 --- a/boot/cmdline.c +++ b/boot/cmdline.c @@ -10,6 +10,7 @@ *-------------------------------------------------------------------------- */ #include +#include #include #include #include @@ -33,8 +34,18 @@ void parse_cmdline(const char *cmdline) { printk("cmdline: %s\n", system.cmdline); get_value("root", value); - assert(value[0] == 'h' && value[1] == 'd' && value[2] == 'a'); - system.root_dev = MAKE_DEV(DEV_MAJOR_HDA, atoi(value + 3)); + + assert(strlen(value) >= 4); + + assert(value[0] == 'h' && value[1] == 'd'); + assert(value[2] >= 'a' && value[2] <= 'd'); + + int disk_drv_no = value[2] - 'a'; + + uint32_t part_no = atoi(value + 3); + assert(part_no >= 1); + + system.root_dev = MAKE_DISK_DEV(disk_drv_no, part_no); printk("root device %s [0x%08x]\n", value, system.root_dev); get_value("delay", value); diff --git a/drivers/ata.c b/drivers/ata.c index 4a522fe..b6baa08 100644 --- a/drivers/ata.c +++ b/drivers/ata.c @@ -69,28 +69,6 @@ void ata_read_identity_string(const uint16_t *identify, int bgn, int end, char * buf[i] = 0; } -void ata_read_partions(ide_part_t *part, const char *buf) { - int offset = PARTITION_TABLE_OFFSET; - const char *p = buf + offset; - - for (int i = 0; i < 4; i++) { - part->flags = (uint8_t)p[0]; - part->type = (uint8_t)p[4]; - part->lba_start = *((uint32_t *)(p + 8)); - uint32_t size = *((uint32_t *)(p + 12)); - part->lba_end = part->lba_start + size; - - printk("part[%d] %02X %02X %u %u\n", i, part->flags, part->type, part->lba_start, - part->lba_end == 0 ? 0 : part->lba_end - 1); - - // 这里应该再判断一下part->flags,如果是扩展分区还需要再读取 - // 先这样实现 - - p += 16; // 每个分区16个字节 - part++; - } -} - // 《AT Attachment 8 - ATA/ATAPI Command Set》 void ide_ata_init() { for (int i = 0; i < MAX_IDE_DRIVE_CNT; i++) { @@ -215,6 +193,7 @@ void ide_ata_init() { // 1~4代表各个主分区 // 5~15 代表各个逻辑分区 drv->partions[0].flags = 0x00; + drv->partions[0].type = 0x00; drv->partions[0].lba_start = 0; drv->partions[0].lba_end = drv->max_lba; } @@ -227,7 +206,9 @@ void read_partition_table(ide_drive_t *drv, uint32_t ext_lba, uint32_t offset_lb disk_request_t r; char *sect = kmalloc(SECT_SIZE, 0); - r.dev = MAKE_DEV(DEV_MAJOR_IDE0 + (drv->drv_no >> 1), drv->drv_no % 2); + + // part_no == 0 代表整场硬盘 + r.dev = MAKE_DISK_DEV(drv->drv_no, 0); r.command = DISK_REQ_READ; r.pos = base + offset_lba; r.count = 1; @@ -240,13 +221,13 @@ void read_partition_table(ide_drive_t *drv, uint32_t ext_lba, uint32_t offset_lb // MBR里的分区占据 [1,4] part_id = 1; } else { - // 扩展分区里的逻辑分区占据 [5,MAX_IDE_PARTIONS) + // 扩展分区里的逻辑分区占据 [5,MAX_DISK_PARTIONS) part_id = 5 + depth - 1; } const char *p = sect + PARTITION_TABLE_OFFSET; for (int i = 0; i < 4; i++) { - if (part_id >= MAX_IDE_PARTIONS) { + if (part_id >= MAX_DISK_PARTIONS) { break; } part = drv->partions + part_id; @@ -265,7 +246,7 @@ void read_partition_table(ide_drive_t *drv, uint32_t ext_lba, uint32_t offset_lb ext_lba = ext_lba != 0 ? ext_lba : part->lba_start; read_partition_table(drv, ext_lba, part->lba_start, depth + 1); } else { - printk("part[%d] %02X %u %u\n", part_id + drv->drv_no * MAX_IDE_PARTIONS, part->type, part->lba_start, + printk("part[%d] %02X %u %u\n", part_id + drv->drv_no * MAX_DISK_PARTIONS, part->type, part->lba_start, size); } @@ -486,3 +467,25 @@ int ata_pio_read_ext(int drv, uint64_t pos, uint16_t count, int timeout, void *d // return st; // } + +// void ata_read_partions(ide_part_t *part, const char *buf) { +// int offset = PARTITION_TABLE_OFFSET; +// const char *p = buf + offset; + +// for (int i = 0; i < 4; i++) { +// part->flags = (uint8_t)p[0]; +// part->type = (uint8_t)p[4]; +// part->lba_start = *((uint32_t *)(p + 8)); +// uint32_t size = *((uint32_t *)(p + 12)); +// part->lba_end = part->lba_start + size; + +// printk("part[%d] %02X %02X %u %u\n", i, part->flags, part->type, part->lba_start, +// part->lba_end == 0 ? 0 : part->lba_end - 1); + +// // 这里应该再判断一下part->flags,如果是扩展分区还需要再读取 +// // 先这样实现 + +// p += 16; // 每个分区16个字节 +// part++; +// } +// } diff --git a/drivers/block.c b/drivers/block.c index 2fa1450..cb03173 100644 --- a/drivers/block.c +++ b/drivers/block.c @@ -24,7 +24,7 @@ blk_buffer_t *get_hash_block_buffer(dev_t dev, uint32_t block, uint16_t size) {} blk_buffer_t *block_read(dev_t dev, uint32_t block) { blk_buffer_t *bb = 0; - assert(DEV_MAJOR(dev) == DEV_MAJOR_IDE0); + assert(DEV_MAJOR(dev) == DEV_MAJOR_DISK); assert(DEV_MINOR(dev) == 1); // 目前不提供hash表组强起来的缓冲功能 @@ -51,7 +51,7 @@ void ata_read_ext2_sb() { const int size = offset + 1024; const int block = 1; - blk_buffer_t *bb = block_read(MAKE_DEV(DEV_MAJOR_IDE0, 1), block); + blk_buffer_t *bb = block_read(system.root_dev, block); ext2_sb_t *p = (ext2_sb_t *)(bb->data + offset); printk("inodes count %u inodes per group %u free %u\n", p->s_inodes_count, p->s_inodes_per_group, diff --git a/drivers/ide.c b/drivers/ide.c index 85284ef..4a9e332 100644 --- a/drivers/ide.c +++ b/drivers/ide.c @@ -177,13 +177,12 @@ void ide_init() { ide_drive_t *ide_get_drive(dev_t dev) { int major = DEV_MAJOR(dev); int minor = DEV_MINOR(dev); - assert(major == DEV_MAJOR_IDE0 || major == DEV_MAJOR_IDE1); - assert(minor < MAX_IDE_PARTIONS * 2); // 因为一个通道有两个硬盘,每个硬盘最大MAX_IDE_PARTIONS分区 - int channel = major == DEV_MAJOR_IDE0 ? 0 : 1; - int index = minor / MAX_IDE_PARTIONS; + int drv_no = minor / MAX_DISK_PARTIONS; - int drv_no = channel * 2 + index; + assert(major == DEV_MAJOR_DISK); + assert(minor >= 0); + assert(drv_no < MAX_IDE_DRIVE_CNT); ide_drive_t *drv = ide_drives + drv_no; diff --git a/drivers/ide.h b/drivers/ide.h index f738519..743bb0b 100644 --- a/drivers/ide.h +++ b/drivers/ide.h @@ -151,7 +151,9 @@ // #define PARTITION_CNT 4 #define PARTITION_TABLE_OFFSET 0x1BE -#define MAX_IDE_PARTIONS 16 +#define MAX_DISK_PARTIONS 16 + +#define MAKE_DISK_DEV(drv_no, part_no) MAKE_DEV(DEV_MAJOR_DISK, (drv_no)*MAX_DISK_PARTIONS + (part_no)) // 分区定义 typedef struct ide_part_ { @@ -205,7 +207,7 @@ typedef struct _ide_drive { ide_pci_controller_t *ide_pci_controller; - ide_part_t partions[MAX_IDE_PARTIONS]; + ide_part_t partions[MAX_DISK_PARTIONS]; } ide_drive_t; #define MAX_IDE_DRIVE_CNT 4 diff --git a/gdbscript b/gdbscript index 2304377..7e970f0 100644 --- a/gdbscript +++ b/gdbscript @@ -1,7 +1,5 @@ -# kill qemu on gdb exit -#define hook-quit -# kill -#end +# 避免退出gdb时二次确认 +set confirm off #b *0x7c00 diff --git a/include/fs.h b/include/fs.h index 6cfc181..77bb046 100644 --- a/include/fs.h +++ b/include/fs.h @@ -23,8 +23,9 @@ #define DEV_MAJOR_UNUSED 0x0000 #define DEV_MAJOR_MEM 0x0001 #define DEV_MAJOR_TTY 0x0002 -#define DEV_MAJOR_IDE0 0x0003 -#define DEV_MAJOR_HDA DEV_MAJOR_IDE0 +#define DEV_MAJOR_DISK 0x0003 +// #define DEV_MAJOR_IDE0 0x0003 +// #define DEV_MAJOR_HDA DEV_MAJOR_IDE0 #define DEV_MAJOR_IDE1 0x0004 #define DEV_MAJOR_SCSI0 0x0005 #define DEV_MAJOR_SCSI2 0x0006 diff --git a/kernel/sched.c b/kernel/sched.c index 50bdb3d..2f832ab 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -234,6 +234,7 @@ void schedule() { task_union *next = sel != 0 ? sel : root; next->state = TASK_RUNNING; + next->reason = ""; if (prev != next) { next->sched_cnt++; diff --git a/kernel/setup.c b/kernel/setup.c index cfda533..d947589 100644 --- a/kernel/setup.c +++ b/kernel/setup.c @@ -80,7 +80,7 @@ void setup_kernel() { boot_delay(DEFAULT_BOOT_DELAY_TICKS); extern tty_t *const monitor_tty; - // tty_switch(monitor_tty); + tty_switch(monitor_tty); boot_delay(DEFAULT_BOOT_DELAY_TICKS); diff --git a/kernel/task_disk.c b/kernel/task_disk.c index b26ac64..6706b85 100644 --- a/kernel/task_disk.c +++ b/kernel/task_disk.c @@ -61,9 +61,9 @@ void disk_task_entry(void *arg) { asm("hlt;"); } - // printk("wait request for hard disk %d\n", drv_no); + // printk("wait request for hard disk channel %d\n", channel); down(&ide_ctrl->request_queue.sem); - // printk("hard disk %d\n", drv_no); + // printk("hard disk channel %d\n", channel); mutex_lock(&ide_ctrl->request_mutex); disk_request_t *r; @@ -76,7 +76,6 @@ void disk_task_entry(void *arg) { atomic_inc(&ide_ctrl->consumed_cnt); mutex_unlock(&ide_ctrl->request_mutex); - // TODO dev -> drv ide_drive_t *drv = ide_get_drive(r->dev); int drv_no = drv->drv_no; if (drv->present == 0) { @@ -96,11 +95,12 @@ void disk_task_entry(void *arg) { ata_dma_read_ext(drv_no, r->pos, r->count, r->buf); break; default: + panic("invalid disk request command"); break; } // 等待硬盘中断 - printk("down ide req\n"); + // printk("down ide req\n"); down(&ide_ctrl->disk_intr_sem); // 读数据 diff --git a/kernel/task_root.c b/kernel/task_root.c index b959074..9ea4224 100644 --- a/kernel/task_root.c +++ b/kernel/task_root.c @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -106,14 +107,14 @@ void taskA_entry() { memset(disk_buf1, 0, 512); disk_request_t r; - r.dev = 0; + r.dev = MAKE_DISK_DEV(0, 0); r.command = DISK_REQ_READ; r.pos = sect_nr; r.count = 1; r.buf = disk_buf1; send_disk_request(&r); - verify_hd_data(sect_nr, disk_buf1, current->name); + // verify_hd_data(sect_nr, disk_buf1, current->name); for (int i = 0; i < 2; i++) { asm("hlt;"); @@ -129,13 +130,13 @@ void taskB_entry() { uint64_t sect_nr = get_next_deubug_sect_nr(); memset(disk_buf2, 0, 512); disk_request_t r; - r.dev = 2; + r.dev = MAKE_DISK_DEV(2, 0); r.command = DISK_REQ_READ; r.pos = sect_nr; r.count = 1; r.buf = disk_buf2; send_disk_request(&r); - verify_hd_data(sect_nr, disk_buf2, current->name); + // verify_hd_data(sect_nr, disk_buf2, current->name); for (int i = 0; i < 1; i++) { asm("hlt;"); @@ -178,9 +179,11 @@ void root_task_entry() { // asm("hlt;"); // } - // kernel_task("tskA", taskA_entry, NULL); - // kernel_task("tskB", taskB_entry, NULL); - // kernel_task("tskC", taskC_entry, NULL); +#if 1 + kernel_task("tskA", taskA_entry, NULL); + kernel_task("tskB", taskB_entry, NULL); + kernel_task("tskC", taskC_entry, NULL); +#endif current->priority = 1; while (1) { diff --git a/kernel/task_user.c b/kernel/task_user.c index 03d381e..cb8b880 100644 --- a/kernel/task_user.c +++ b/kernel/task_user.c @@ -43,7 +43,7 @@ void __ring3text__ __attribute__((__aligned__(PAGE_SIZE))) ring3_entry() { // 共享代码会利用保存在栈上的地址返回到sysexit_return_address处 "pushl $0;" "pushl $0;" - "movl $2, %%ebx;" + "movl $200, %%ebx;" "pushl %%ebp;" "movl %%esp, %%ebp;" "sysenter;" diff --git a/scripts/iso.grub.cfg b/scripts/iso.grub.cfg index 1950e45..00c0acc 100644 --- a/scripts/iso.grub.cfg +++ b/scripts/iso.grub.cfg @@ -10,6 +10,6 @@ menuentry 'Kernel' --class os { #insmod ext2 #set gfxpayload=1024x768x32 #insmod all_video - multiboot2 /boot/Kernel root=hda0 delay=2 + multiboot2 /boot/Kernel root=hda1 delay=2 boot }