From 09a84a6af214858576177a9eb3d0a657227c727f Mon Sep 17 00:00:00 2001 From: zeyadhost Date: Mon, 11 May 2026 17:19:46 +0300 Subject: [PATCH] fix fat32 volume labels --- src/dev/disk_manager.c | 31 ++++++++++++++++++++++++++++++- src/fs/mkfs_fat32.c | 13 +++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/dev/disk_manager.c b/src/dev/disk_manager.c index 8e8a485..3ec6e0a 100644 --- a/src/dev/disk_manager.c +++ b/src/dev/disk_manager.c @@ -42,6 +42,32 @@ static int dm_strlen(const char *s) { return n; } +static void dm_copy_fat_label(char *dst, const uint8_t *src) { + int end = 11; + while (end > 0 && src[end - 1] == ' ') end--; + for (int i = 0; i < end && i < 31; i++) dst[i] = (char)src[i]; + dst[end < 31 ? end : 31] = 0; +} + +static void disk_load_fat32_label(Disk *disk) { + uint8_t *buffer; + FAT32_BootSector *bpb; + char label[32]; + + if (!disk || !disk->read_sector) return; + + buffer = (uint8_t*)kmalloc(512); + if (!buffer) return; + + if (disk->read_sector(disk, 0, buffer) == 0 && buffer[510] == 0x55 && buffer[511] == 0xAA) { + bpb = (FAT32_BootSector*)buffer; + dm_copy_fat_label(label, bpb->volume_label); + if (label[0]) dm_strcpy(disk->label, label); + } + + kfree(buffer); +} + // === ATA Definitions (Legacy IDE PIO — kept as fallback) === #define ATA_PRIMARY_IO 0x1F0 @@ -387,6 +413,8 @@ void disk_register_partition(Disk *parent, uint32_t lba_offset, uint32_t sector_ part->is_partition = true; part->registered = true; + if (is_fat32) disk_load_fat32_label(part); + disks[disk_count++] = part; serial_write("[DISK] Registered /dev/"); @@ -567,6 +595,7 @@ static void parse_mbr_partitions(Disk *disk) { serial_write("\n"); disk->is_fat32 = true; disk->partition_lba_offset = 0; + disk_load_fat32_label(disk); } else if (part_count == 0) { serial_write("[DISK] No MBR partitions found on /dev/"); serial_write(disk->devname); @@ -1040,4 +1069,4 @@ int disk_rescan(Disk *disk) { parse_mbr_partitions(disk); return 0; -} \ No newline at end of file +} diff --git a/src/fs/mkfs_fat32.c b/src/fs/mkfs_fat32.c index 730de84..bcf44bb 100644 --- a/src/fs/mkfs_fat32.c +++ b/src/fs/mkfs_fat32.c @@ -28,6 +28,13 @@ static void mf_strncpy(char *dst, const char *src, int n) { while (i < n) { dst[i++] = ' '; } /* FAT labels are space-padded */ } +static void mf_set_disk_label(Disk *disk, const char *label) { + int end = 11; + while (end > 0 && label[end - 1] == ' ') end--; + for (int i = 0; i < end && i < 31; i++) disk->label[i] = label[i]; + disk->label[end < 31 ? end : 31] = 0; +} + // On-disk BPB structures typedef struct __attribute__((packed)) { @@ -269,13 +276,15 @@ int mkfs_fat32_format(Disk *disk, uint32_t sector_count, const char *label) { kfree(buf); + disk->is_fat32 = true; + mf_set_disk_label(disk, upper_label); + serial_write("[MKFS] FAT32 formatted: "); serial_write(disk->devname); serial_write(" label="); char lb[12]; - mf_memcpy(lb, vol_label, 11); + mf_memcpy(lb, upper_label, 11); lb[11] = 0; - for (int i = 0; i < 11; i++) lb[i] = (lb[i] >= 'a' && lb[i] <= 'z') ? lb[i] - 32 : lb[i]; for (int i = 10; i >= 0 && lb[i] == ' '; i--) lb[i] = 0; serial_write(lb); serial_write(" spc=");