From b8cafaf24dec1c573f878ea8e78616b328ee5670 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Sun, 7 May 2023 23:14:43 +0200 Subject: [PATCH] feature (MBR): implemented MBR --- inc/drivers/fs/mbr.h | 40 ++++++++++++++++++++++++++++++++++++++++ src/drivers/ahci.c | 20 +++++++++++++++++--- src/drivers/fs/mbr.c | 16 ++++++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 inc/drivers/fs/mbr.h create mode 100644 src/drivers/fs/mbr.c diff --git a/inc/drivers/fs/mbr.h b/inc/drivers/fs/mbr.h new file mode 100644 index 0000000..fa13f9c --- /dev/null +++ b/inc/drivers/fs/mbr.h @@ -0,0 +1,40 @@ +// This file is part of noxos and licensed under the MIT open source license + +#ifndef KERNEL_MBR_H +#define KERNEL_MBR_H + +#include "utils/stdtypes.h" +#include "utils/string.h" + +typedef enum { + MBR_PARTITION_TYPE_UNUSED = 0x00, + MBR_PARTITION_TYPE_GPT = 0xEE, + MBR_PARTITION_TYPE_EXT2 = 0x83, + MBR_PARTITION_TYPE_FAT12 = 0x01, + MBR_PARTITION_TYPE_FAT16 = 0x04, + MBR_PARTITION_TYPE_FAT32_CHS = 0x0B, + MBR_PARTITION_TYPE_FAT32_LBA = 0x0C +} mbr_partition_types_E; + +typedef struct { + uint8_t boot_indicator; + uint8_t start_head; + uint16_t start_sector:6; + uint16_t start_cylinder:10; + uint8_t partition_type; + uint8_t end_head; + uint16_t end_sector:6; + uint16_t end_cylinder:10; + uint32_t start_lba; + uint32_t num_sectors; +}__attribute__((packed)) mbr_partition_header_T; + +typedef struct { + uint8_t bootstrap [446]; + mbr_partition_header_T partitions [4]; + uint8_t signature [2]; +} mbr_header_T; + +string_t mbr_partition_type_to_string(mbr_partition_types_E type); + +#endif //KERNEL_MBR_H diff --git a/src/drivers/ahci.c b/src/drivers/ahci.c index 35a664d..41d355c 100644 --- a/src/drivers/ahci.c +++ b/src/drivers/ahci.c @@ -1,6 +1,7 @@ // This file is part of noxos and licensed under the MIT open source license #include "drivers/ahci.h" +#include "drivers/fs/mbr.h" #include "utils/memory.h" #include "utils/logger.h" #include "mm/page_map.h" @@ -50,9 +51,22 @@ ahci_controller_T* ahci_controller_alloc(pci_device_T* pci_device) { for (int i = 0; i < controller->num_ports; i++) { log(LOG_INFO, " %s", g_ahci_device_type_strings[controller->ports[i].type]); ahci_port_init(&controller->ports[i]); - uint8_t* buffer = memory_allocate(512 + PFRAME_SIZE); - if (ahci_port_read(&controller->ports[i], 0, 1, CEIL_TO((uint64_t)buffer, PFRAME_SIZE))) - memory_hexdump(CEIL_TO((uint64_t)buffer, PFRAME_SIZE), 512); + + uint8_t* addr = memory_allocate(512 + PFRAME_SIZE); + void* buffer = (void*)CEIL_TO((uint64_t)addr, PFRAME_SIZE); + + if (ahci_port_read(&controller->ports[i], 0, 1, buffer)) { + mbr_header_T* mbr = (mbr_header_T*)buffer; + + log(LOG_INFO, " MBR:"); + for (int j = 0; j < 4; j++) { + mbr_partition_types_E type = mbr->partitions[j].partition_type; + if (type == MBR_PARTITION_TYPE_UNUSED) continue; + log(LOG_INFO, " p%d: 0x%xb - %s", j, type, mbr_partition_type_to_string(type)); + } + } + + memory_free(addr); } return controller; diff --git a/src/drivers/fs/mbr.c b/src/drivers/fs/mbr.c new file mode 100644 index 0000000..75eaad6 --- /dev/null +++ b/src/drivers/fs/mbr.c @@ -0,0 +1,16 @@ +// This file is part of noxos and licensed under the MIT open source license + +#include "drivers/fs/mbr.h" + +string_t mbr_partition_type_to_string(mbr_partition_types_E type) { + switch (type) { + case MBR_PARTITION_TYPE_UNUSED: return "Unused"; + case MBR_PARTITION_TYPE_GPT: return "GUID Partition Table"; + case MBR_PARTITION_TYPE_EXT2: return "EXT2"; + case MBR_PARTITION_TYPE_FAT12: return "FAT12"; + case MBR_PARTITION_TYPE_FAT16: return "FAT16"; + case MBR_PARTITION_TYPE_FAT32_CHS: return "FAT32 (CHS)"; + case MBR_PARTITION_TYPE_FAT32_LBA: return "FAT32 (LBA)"; + default: return "Unknown"; + } +} \ No newline at end of file