From ae3e1e07cffbf252a15667f3f3a9c74a1307e7d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiesner=20Andr=C3=A1s?= Date: Sat, 30 Sep 2023 06:37:35 +0200 Subject: [PATCH] Fat32 ctrl block --- fs/fat32/fat32.c | 20 ++++++++++++++++++++ fs/fat32/fat32.h | 15 +++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/fs/fat32/fat32.c b/fs/fat32/fat32.c index 91b42ca..37cf962 100644 --- a/fs/fat32/fat32.c +++ b/fs/fat32/fat32.c @@ -2,4 +2,24 @@ // Created by epagris on 2023.09.28.. // +#include #include "fat32.h" + +int fat32_load(Fat32_CtrlBlock *ctrl, const uint8_t *bpb, uint32_t bpb_s) { + ctrl->bpb_s = bpb_s; + + const Bpb_Fat32 * bpb_obj = (Bpb_Fat32 *)bpb; + + ctrl->bytes_per_sector = bpb_obj->bytes_per_sector; + ctrl->sectors_per_cluster = bpb_obj->sectors_per_cluster; + ctrl->sectors_per_fat = bpb_obj->n_sectors_per_fat; + memcpy(ctrl->volume_label, bpb_obj->volume_label, 11); + ctrl->serial_number = bpb_obj->volume_serial_number; + ctrl->fat_copies = bpb_obj->n_fats; + + ctrl->fat_s = ctrl->bpb_s + bpb_obj->n_reserved_sectors; // FATs are stored on the hidden area right after the reserved sectors + ctrl->data_s = ctrl->fat_s + ctrl->fat_copies * ctrl->sectors_per_fat; // data region begins right after the FAT copies + ctrl->root_s = bpb_obj->root_first_cluster * ctrl->sectors_per_cluster + ctrl->data_s; // first sector of root directory is determined using the cluster number as an offset + + return 0; +} diff --git a/fs/fat32/fat32.h b/fs/fat32/fat32.h index 53452af..a3695ae 100644 --- a/fs/fat32/fat32.h +++ b/fs/fat32/fat32.h @@ -33,4 +33,19 @@ typedef struct { uint8_t fs_type[8]; // file system type ("FAT32") } __attribute__((packed)) Bpb_Fat32; +typedef struct { + uint32_t bpb_s; // sector of bios parameter block + uint32_t fat_s; // first sector of the FAT area + uint32_t root_s; // first sector of root directory + uint32_t data_s; // first sector of the data area (~volume) + uint32_t serial_number; // serial number of the volume + uint32_t sectors_per_fat; // sectors per FAT table + uint16_t bytes_per_sector; // number of bytes creating a sector + uint16_t sectors_per_cluster; // number of sectors creating a cluster + uint8_t fat_copies; // number of FAT table copies + uint8_t volume_label[11]; // pointer to volume label +} Fat32_CtrlBlock; + +int fat32_load(Fat32_CtrlBlock *ctrl, const uint8_t *bpb, uint32_t bpb_s); + #endif //EMBPART_FAT32_H