feat: setup initial fat reading logic

This commit is contained in:
2021-02-11 22:01:57 +01:00
parent 90ef4522ca
commit 555c1177a6
4 changed files with 179 additions and 23 deletions

View File

@@ -8,6 +8,7 @@
#include <mem/mem.h>
#include <libc/kprintf.h>
#include <fs/blockdev.h>
#include <libc/libc.h>
typedef struct {
u8 bootable;
@@ -34,6 +35,19 @@ typedef struct {
u32 start_lba;
} mbr_block_driver_info;
u8 mbr_check_device(const block_device *device, u8 *first_sector);
block_dev_driver mbr_driver = {
.name = "mbr",
.flags.root_only = 1,
.check_device = mbr_check_device,
.free_device = NULL, // todo
};
void mbr_register_block_driver() {
block_dev_register_driver(&mbr_driver);
}
u8 mbr_block_dev_access(const block_device *device, u8 direction, u32 lba, u8 sectors, void *target) {
if (!device->flags.present || lba > device->num_lba) {
return BLOCK_DEV_ACCESS_ERR;
@@ -49,32 +63,33 @@ u8 mbr_block_dev_access(const block_device *device, u8 direction, u32 lba, u8 se
}
u8 mbr_check_device(const block_device *device, u8 *first_sector) {
mbr_table *table = (mbr_table *) &first_sector[512 - sizeof(mbr_table)];
if (table->signature[0] != 0x55 && table->signature[1] != 0xAA) { // AA 55 but in little endian
mbr_table table;
memcpy((u8 *) &table, first_sector + (device->block_size - sizeof(mbr_table)), sizeof(mbr_table));
if (table.signature[0] != 0x55 && table.signature[1] != 0xAA) { // AA 55 but in little endian
return BLOCK_DEV_DRIVER_CHECK_NO_MATCH;
}
if (table->reserved != 0x0000 && table->reserved != 0x5A5A) {
if (table.reserved != 0x0000 && table.reserved != 0x5A5A) {
return BLOCK_DEV_DRIVER_CHECK_NO_MATCH;
}
for (int i = 0; i < 4; ++i) {
if (table->entries[i].num_lbas > device->num_lba || table->entries[i].start_lba > device->num_lba) {
if (table.entries[i].num_lbas > device->num_lba || table.entries[i].start_lba > device->num_lba) {
return BLOCK_DEV_DRIVER_CHECK_NO_MATCH;
}
}
for (int i = 0; i < 4; ++i) {
if (table->entries[i].system_id == 0) continue;
if (table.entries[i].system_id == 0) continue;
mbr_block_driver_info *info = malloc(sizeof(mbr_block_driver_info));
info->device = device;
info->start_lba = table->entries[i].start_lba;
info->start_lba = table.entries[i].start_lba;
block_device logical_device = {
.flags.present = 1,
.num_lba = table->entries[i].num_lbas,
.num_lba = table.entries[i].num_lbas,
.block_size = device->block_size,
.access = mbr_block_dev_access,
.device_info = info,
@@ -89,17 +104,6 @@ u8 mbr_check_device(const block_device *device, u8 *first_sector) {
return BLOCK_DEV_DRIVER_CHECK_OK;
}
block_dev_driver mbr_driver = {
.name = "mbr",
.flags.root_only = 1,
.check_device = mbr_check_device,
.free_device = NULL, // todo
};
void mbr_register_block_driver() {
block_dev_register_driver(&mbr_driver);
}
void mbr_read_from_ide(u8 ide_drive) {
char *mbr_data = malloc(0x200);
ide_access(0, ide_drive, 0, 1, mbr_data);