diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-11-08 06:43:18 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-11-08 06:43:18 +0800 |
| commit | e3598eddc3ec9a3deb321a2c04803246db4abb84 (patch) | |
| tree | 71d9b4e53ae08fa04e3395f49504012461e8bf5a | |
| parent | 9ffbfc713f9e258bacdfb601293512ecd51d1733 (diff) | |
| download | rcc-project-e3598eddc3ec9a3deb321a2c04803246db4abb84.tar.gz rcc-project-e3598eddc3ec9a3deb321a2c04803246db4abb84.tar.xz | |
[mubios] Add ATA
Signed-off-by: Richard Qin <richard.qin.x@gmail.com>
| -rw-r--r-- | mubios/core/post.c | 11 | ||||
| -rw-r--r-- | mubios/drivers/ata.c | 93 | ||||
| -rw-r--r-- | mubios/include/bios.h | 2 | ||||
| -rw-r--r-- | mubios/include/io.h | 10 |
4 files changed, 115 insertions, 1 deletions
diff --git a/mubios/core/post.c b/mubios/core/post.c index a37f192..076d262 100644 --- a/mubios/core/post.c +++ b/mubios/core/post.c @@ -36,5 +36,14 @@ post_main (void) pit_init (); ioapic_init (); - serial_write ("mubios: IVT/PIC/PIT initialized\r\n"); + volatile u8 *mbr; + u8 ata_status; + + mbr = (volatile u8 *)0x00007C00u; + ata_status = ata_read_sector28 (0, (u16 *)mbr); + + if (ata_status == 0 && mbr[0x01FE] == 0x55 && mbr[0x01FF] == 0xAA) + serial_write ("mubios: MBR loaded\r\n"); + else + serial_write ("mubios: MBR read failed\r\n"); } diff --git a/mubios/drivers/ata.c b/mubios/drivers/ata.c new file mode 100644 index 0000000..a0cd12a --- /dev/null +++ b/mubios/drivers/ata.c @@ -0,0 +1,93 @@ +/* + RCC-Project + + Copyright (C) 2026 Richard Qin + + This file is part of RCC-Project. + + RCC-Project is free software; you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your option) any + later version. + + RCC-Project is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + RCC-Project; see the file COPYING. If not see + <http://www.gnu.org/licenses/>. + */ + +#include "bios.h" +#include "io.h" + +#define ATA_DATA 0x1F0u +#define ATA_SECTOR_COUNT 0x1F2u +#define ATA_LBA_LOW 0x1F3u +#define ATA_LBA_MID 0x1F4u +#define ATA_LBA_HIGH 0x1F5u +#define ATA_DRIVE 0x1F6u +#define ATA_STATUS 0x1F7u +#define ATA_COMMAND 0x1F7u + +#define ATA_STATUS_ERR 0x01u +#define ATA_STATUS_DRQ 0x08u +#define ATA_STATUS_BSY 0x80u + +#define ATA_CMD_READ_SECTORS 0x20u + +static u8 +ata_wait_not_busy (void) +{ + u8 status; + + do + { + status = io_in8 (ATA_STATUS); + } + while ((status & ATA_STATUS_BSY) != 0); + + return status; +} + +static void +ata_delay_400ns (void) +{ + io_in8 (ATA_STATUS); + io_in8 (ATA_STATUS); + io_in8 (ATA_STATUS); + io_in8 (ATA_STATUS); +} + +u8 +ata_read_sector28 (u32 lba, u16 *buffer) +{ + u8 status; + u16 index; + + status = ata_wait_not_busy (); + if ((status & ATA_STATUS_ERR) != 0) + return 1; + + io_out8 (ATA_DRIVE, (u8)(0xE0u | ((lba >> 24) & 0x0Fu))); + ata_delay_400ns (); + + io_out8 (ATA_SECTOR_COUNT, 1); + io_out8 (ATA_LBA_LOW, (u8)(lba & 0xFFu)); + io_out8 (ATA_LBA_MID, (u8)((lba >> 8) & 0xFFu)); + io_out8 (ATA_LBA_HIGH, (u8)((lba >> 16) & 0xFFu)); + io_out8 (ATA_COMMAND, ATA_CMD_READ_SECTORS); + + status = ata_wait_not_busy (); + if ((status & ATA_STATUS_ERR) != 0) + return 1; + + if ((status & ATA_STATUS_DRQ) == 0) + return 1; + + for (index = 0; index < 256; ++index) + buffer[index] = io_in16 (ATA_DATA); + + return 0; +} diff --git a/mubios/include/bios.h b/mubios/include/bios.h index eee22e8..7d2d447 100644 --- a/mubios/include/bios.h +++ b/mubios/include/bios.h @@ -50,4 +50,6 @@ void ioapic_init (void); void ivt_init (void); +u8 ata_read_sector28 (u32 lba, u16 *buffer); + #endif // MUBIOS_BIOS_H diff --git a/mubios/include/io.h b/mubios/include/io.h index 8b50508..cedd1c1 100644 --- a/mubios/include/io.h +++ b/mubios/include/io.h @@ -40,4 +40,14 @@ io_in8 (u16 port) return value; } +static inline u16 +io_in16 (u16 port) +{ + u16 value; + + __asm__ volatile ("inw %1, %0" : "=a"(value) : "dN"(port)); + + return value; +} + #endif // MUBIOS_IO_H |
