From af2db137f97dc499496e25f07245f375b60f4747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiesner=20Andr=C3=A1s?= Date: Sat, 11 Nov 2023 06:39:21 +0100 Subject: [PATCH] Automount initial steps --- CMakeLists.txt | 5 ++++- fs/fat32/fat32.c | 10 +++++----- fs/fat32/fat32.h | 6 +++++- fs/fs_driver.h | 15 +++++++++++++++ mbr/mbr.c | 15 +++++++++++++-- mbr/mbr.h | 2 +- mount/mount.c | 5 +++++ mount/mount.h | 19 +++++++++++++++++++ 8 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 fs/fs_driver.h create mode 100644 mount/mount.c create mode 100644 mount/mount.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f1cbaf..b6c98eb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,4 +12,7 @@ add_library(embpart embpart.c MassStorage.c MassStorage.h file_interface.c - file_interface.h) \ No newline at end of file + file_interface.h + mount/mount.c + mount/mount.h + fs/fs_driver.h) \ No newline at end of file diff --git a/fs/fat32/fat32.c b/fs/fat32/fat32.c index c6e315a..2f9b4ba 100644 --- a/fs/fat32/fat32.c +++ b/fs/fat32/fat32.c @@ -1,7 +1,3 @@ -// -// Created by epagris on 2023.09.28.. -// - #include #include #include "fat32.h" @@ -654,4 +650,8 @@ uint32_t fat32_read_file_stream(const Fat32_CtrlBlock *ctrl, Fat32_FileHelper *f } return read_len; -} \ No newline at end of file +} + +// ---------------------- + +Fs_Driver fat32_driver = { (fs_drv_open) fat32_open_file }; \ No newline at end of file diff --git a/fs/fat32/fat32.h b/fs/fat32/fat32.h index 3d75cbd..6ee5281 100644 --- a/fs/fat32/fat32.h +++ b/fs/fat32/fat32.h @@ -6,6 +6,7 @@ #ifdef __linux #include #include "../../MassStorage.h" +#include "../fs_driver.h" #define MSG(...) printf(__VA_ARGS__) #endif @@ -100,12 +101,14 @@ typedef struct { } Fat32_SeekHint; typedef struct { - const Fat32_CtrlBlock * ctrl; // pointer FAT32 control block + const Fat32_CtrlBlock * ctrl; // pointer to FAT32 control block Fat32_SeekHint last_pos; // last read position uint32_t entry_ba; // FileEntry byte address on the mass storage uint32_t size; // file size } Fat32_FileHelper; +extern Fs_Driver fat32_driver; + int fat32_load(Fat32_CtrlBlock *ctrl, uint32_t bpb_s, const MassStorage * mstg); int fat32_list_dir(const Fat32_CtrlBlock *ctrl, const char * dir); @@ -120,4 +123,5 @@ uint32_t fat32_seek_stream(const Fat32_CtrlBlock *ctrl, uint32_t pos, Fat32_File uint32_t fat32_read_file_stream(const Fat32_CtrlBlock *ctrl, Fat32_FileHelper *file_helper, uint32_t len, uint8_t *p); + #endif //EMBPART_FAT32_H diff --git a/fs/fs_driver.h b/fs/fs_driver.h new file mode 100644 index 0000000..3e668de --- /dev/null +++ b/fs/fs_driver.h @@ -0,0 +1,15 @@ +#ifndef EMBPART_FS_DRIVER_H +#define EMBPART_FS_DRIVER_H + +typedef int (*fs_drv_open)(const void * ctrl, const char * path, void * file); +typedef int (*fs_drv_seek)(const void * ctrl, void * file, unsigned long pos); +typedef int (*fs_drv_read)(const void * ctrl, void * file, unsigned long len, void * buf); + +// File system driver +typedef struct { + fs_drv_open open; + fs_drv_seek seek; + fs_drv_read read; +} Fs_Driver; + +#endif //EMBPART_FS_DRIVER_H diff --git a/mbr/mbr.c b/mbr/mbr.c index 9c0303c..df8eb2f 100644 --- a/mbr/mbr.c +++ b/mbr/mbr.c @@ -1,7 +1,18 @@ #include "mbr.h" +#include + #define MBR_PART_TABLE_OFFSET (446) +#define MBR_BOOT_SIGNATURE_OFFSET (510) +#define MBR_BOOT_SIGNATURE0 (0x55) +#define MBR_BOOT_SIGNATURE1 (0xAA) const PartEntry *mbr_get_partitions(const uint8_t *mbr_sec) { - return (PartEntry *)(mbr_sec + MBR_PART_TABLE_OFFSET); -} + // check if disk is MBR type + if ((mbr_sec[MBR_BOOT_SIGNATURE_OFFSET] != MBR_BOOT_SIGNATURE0) || + (mbr_sec[MBR_BOOT_SIGNATURE_OFFSET + 1] != MBR_BOOT_SIGNATURE1)) { + return NULL; // if not + } else { + return (PartEntry *) (mbr_sec + MBR_PART_TABLE_OFFSET); // if it is + } +} \ No newline at end of file diff --git a/mbr/mbr.h b/mbr/mbr.h index 1dda15e..a436113 100644 --- a/mbr/mbr.h +++ b/mbr/mbr.h @@ -12,7 +12,7 @@ typedef struct { uint8_t chsa_la[3]; // CHS address of last absolute sector in partition uint32_t lba_fa; // LBA of first absolute sector uint32_t sector_count; // number of sectors the partition includes -} PartEntry; +} __attribute__((packed)) PartEntry; /** * Get pointer to partition table diff --git a/mount/mount.c b/mount/mount.c new file mode 100644 index 0000000..68608c6 --- /dev/null +++ b/mount/mount.c @@ -0,0 +1,5 @@ +#include "mount.h" + +int mnt_automount_disk() { + +} \ No newline at end of file diff --git a/mount/mount.h b/mount/mount.h new file mode 100644 index 0000000..dae9167 --- /dev/null +++ b/mount/mount.h @@ -0,0 +1,19 @@ +#ifndef EMBPART_MOUNT_H +#define EMBPART_MOUNT_H + +#include + +#define MNT_MAX_MOUNTED_VOLUMES (4) +#define MNT_MAX_MOUNT_NAME_LENGTH (15) + +typedef struct { + void * fs_control_block; // pointer to file system control block + uint32_t part_size; // partition size + char mnt_name[MNT_MAX_MOUNT_NAME_LENGTH + 1]; // name of the mount +} Mnt_MountEntry; + +typedef struct { + Mnt_MountEntry vols[MNT_MAX_MOUNTED_VOLUMES]; // volume slots +} Mnt_MountTable; + +#endif //EMBPART_MOUNT_H