From 155234c562ff2323ffc73a58483868b9b4685252 Mon Sep 17 00:00:00 2001 From: acevest Date: Mon, 16 Oct 2023 11:46:03 +0800 Subject: [PATCH] =?utf8?q?disk=E5=86=85=E6=A0=B8=E4=BB=BB=E5=8A=A1?= =?utf8?q?=E8=AF=BB=E7=A1=AC=E7=9B=98=E5=88=86=E5=8C=BA=E6=97=B6=EF=BC=8C?= =?utf8?q?=E9=BB=98=E8=AE=A4=E5=8A=A0=E4=B8=8A=E5=85=B6=E8=B5=B7=E5=A7=8B?= =?utf8?q?=E5=81=8F=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- drivers/ata.c | 17 +++++++++-------- kernel/task_disk.c | 20 +++++++++++++++----- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/drivers/ata.c b/drivers/ata.c index c50da08..6aaef8f 100644 --- a/drivers/ata.c +++ b/drivers/ata.c @@ -45,7 +45,9 @@ void ata_test(uint64_t nr) { u16 identify[256]; void ata_send_read_identify_cmd(int drv) {} -void ata_read_data(int drv, int sect_cnt, void *dst) { insl(REG_DATA(drv), dst, (512 * sect_cnt) / sizeof(uint32_t)); } +void ata_pio_read_data(int drv, int sect_cnt, void *dst) { + insl(REG_DATA(drv), dst, (512 * sect_cnt) / sizeof(uint32_t)); +} // 这里所用的drv是逻辑编号 ATA0、ATA1下的Master、Salve的drv分别为0,1,2,3 void ata_read_identify(int drv, int disable_intr) { @@ -216,6 +218,7 @@ void read_partition_table(ide_drive_t *drv, uint32_t mbr_ext_offset, uint32_t lb ide_part_t *part = 0; uint32_t part_id = 0; + // 用来计算保存下一个扩展分区的起始位置 uint32_t lba_extended_partition = 0; const char *pe = sect + PARTITION_TABLE_OFFSET; for (int i = 0; i < 4; i++) { @@ -241,8 +244,10 @@ void read_partition_table(ide_drive_t *drv, uint32_t mbr_ext_offset, uint32_t lb continue; } + // 用于计算保存下一个分区的起始位置 uint32_t lba_offset = 0; if (0x05 == pt.type) { + assert(lba_extended_partition == 0); // 最多只允许有一个扩展分区 mbr_ext_offset = mbr_ext_offset != 0 ? mbr_ext_offset : pt.lba_start; uint32_t offset = depth == 0 ? lba_partition_table : pt.lba_start; lba_offset = mbr_ext_offset + offset; @@ -255,8 +260,8 @@ void read_partition_table(ide_drive_t *drv, uint32_t mbr_ext_offset, uint32_t lb part->type = pt.type; part->lba_start = lba_offset; uint32_t size = pt.lba_end; - part->lba_end = part->lba_start + size - 1; - printk("part[%02d] %02X %u %u\n", part_id, pt.type, lba_offset, part->lba_end); + part->lba_end = part->lba_start + size; + printk("part[%02d] %02X %10u %-10u\n", part_id, pt.type, lba_offset, part->lba_end - 1); } // 每个分区16个字节 @@ -288,14 +293,10 @@ void ide_read_partions() { } void ide_disk_read(dev_t dev, uint32_t sect_nr, uint32_t count, char *buf) { - ide_drive_t *drv = ide_get_drive(dev); - int part_id = DEV_MINOR((dev)) & 0xFF; // TODO: 换成宏定义 - uint64_t lba_offset = drv->partions[part_id].lba_start; - disk_request_t r; r.dev = dev; r.command = DISK_REQ_READ; - r.pos = lba_offset + sect_nr; + r.pos = sect_nr; r.count = count; r.buf = buf; send_disk_request(&r); diff --git a/kernel/task_disk.c b/kernel/task_disk.c index 6706b85..a774398 100644 --- a/kernel/task_disk.c +++ b/kernel/task_disk.c @@ -12,6 +12,10 @@ #include #include +void ata_read_identify(int drv, int disable_intr); +void ata_pio_read_data(int drv_no, int sect_cnt, void *dst); +void ata_dma_read_ext(int drv, uint64_t pos, uint16_t count, void *dest); + void disk_init() { // ... } @@ -81,18 +85,25 @@ void disk_task_entry(void *arg) { if (drv->present == 0) { panic("disk not present"); } + assert(drv->present == 1); + + int part_id = DEV_MINOR((r->dev)) & 0xFF; + assert(part_id < MAX_DISK_PARTIONS); + assert(MAKE_DISK_DEV(drv_no, part_id) == r->dev); + + uint64_t pos = r->pos + drv->partions[part_id].lba_start; + assert(pos < drv->partions[part_id].lba_end); switch (r->command) { case DISK_REQ_IDENTIFY: + printk("try to read disk drive %u identify", drv_no); assert(r->count == 1); - void ata_read_identify(int drv, int disable_intr); ata_read_identify(drv_no, 0); break; case DISK_REQ_READ: assert(r->count > 0); assert(r->buf != NULL); - void ata_dma_read_ext(int drv, uint64_t pos, uint16_t count, void *dest); - ata_dma_read_ext(drv_no, r->pos, r->count, r->buf); + ata_dma_read_ext(drv_no, pos, r->count, r->buf); break; default: panic("invalid disk request command"); @@ -105,8 +116,7 @@ void disk_task_entry(void *arg) { // 读数据 if (DISK_REQ_IDENTIFY == r->command) { - void ata_read_data(int drv_no, int sect_cnt, void *dst); - ata_read_data(drv_no, 1, r->buf); + ata_pio_read_data(drv_no, 1, r->buf); } // 唤醒等待该请求的进程 -- 2.44.0