diff --git a/mount/mount.c b/mount/mount.c index 98a6314..f0a5c5c 100644 --- a/mount/mount.c +++ b/mount/mount.c @@ -1,16 +1,16 @@ -#include -#include #include "mount.h" #include "../MassStorage.h" -#include "../mbr/mbr.h" #include "../fs/fat32/fat32.h" +#include "../mbr/mbr.h" +#include +#include #define AUTOMOUNT_SECTOR_SIZE (512) -#define MIN(a,b) (((a) < (b)) ? (a) : (b)) +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) -static Mnt_MountTable mtab = { 0 }; -static Mnt_FileTable ftab = { 0 }; +static Mnt_MountTable mtab = {0}; +static Mnt_FileTable ftab = {0}; /** * Attempts to mount a volume. @@ -18,10 +18,10 @@ static Mnt_FileTable ftab = { 0 }; * @param part pointer to partition entry * @return 0 if concluded without error (even if partition of unknown type could not be mounted) */ -static int mnt_mount_volume(MassStorage * mstg, const PartEntry * part) { +static int mnt_mount_volume(MassStorage *mstg, const PartEntry *part) { // fetch common partition details - Mnt_Volume * next_entry = mtab.vols + mtab.mounted_vols; // pointer to control block area - uint8_t * cbk = next_entry->fs_control_block_area; // control block area to be written + Mnt_Volume *next_entry = mtab.vols + mtab.mounted_vols; // pointer to control block area + uint8_t *cbk = next_entry->fs_control_block_area; // control block area to be written next_entry->mnt_name[MNT_MAX_MOUNT_NAME_LENGTH] = '\0'; // write terminating zero in advance int mount_success = -1; @@ -31,14 +31,13 @@ static int mnt_mount_volume(MassStorage * mstg, const PartEntry * part) { break; case FAT32_CHS_PART_ID: // FAT32 case FAT32_LBA_PART_ID: { - Fat32_CtrlBlock *ctrl = (Fat32_CtrlBlock *) cbk; + Fat32_CtrlBlock *ctrl = (Fat32_CtrlBlock *)cbk; mount_success = fat32_load(ctrl, part->lba_fa, mstg); if (mount_success == 0) { fat32_get_volume_label(ctrl, next_entry->mnt_name, MNT_MAX_MOUNT_NAME_LENGTH); // get volume name - next_entry->driver = fat32_driver; // assign driver + next_entry->driver = fat32_driver; // assign driver } - } - break; + } break; default: // unmountable file system's found on the disk MSG("Unknown file system type: %d, cannot mount!\n", part->part_type); break; @@ -47,7 +46,7 @@ static int mnt_mount_volume(MassStorage * mstg, const PartEntry * part) { // if succeeded, then maintain mount table if (mount_success > -1) { next_entry->part_size = part->sector_count * AUTOMOUNT_SECTOR_SIZE; // save volume size - mtab.mounted_vols++; // increase number of mounted partitions + mtab.mounted_vols++; // increase number of mounted partitions } return 0; @@ -58,13 +57,13 @@ static int mnt_mount_volume(MassStorage * mstg, const PartEntry * part) { * @param mstg pointer to Mass Storage object * @return 0 if successful */ -int mnt_automount_disk(MassStorage * mstg) { +int mnt_automount_disk(MassStorage *mstg) { // load the first sector uint8_t buf[AUTOMOUNT_SECTOR_SIZE]; mstg->read_sector(mstg, 0, buf); // fetch partitions from the disk - const PartEntry * partList = mbr_get_partitions(buf); + const PartEntry *partList = mbr_get_partitions(buf); if (partList == NULL) { // not an MBR disk return 0; } @@ -74,8 +73,8 @@ int mnt_automount_disk(MassStorage * mstg) { PartEntry parts[MBR_MAX_PARTITIONS]; memcpy(parts, partList, sizeof(PartEntry) * MBR_MAX_PARTITIONS); // copy partition table for (uint8_t i = 0; (i < MBR_MAX_PARTITIONS) && (mtab.mounted_vols < MNT_MAX_MOUNTED_VOLUMES); i++) { - const PartEntry * part = parts + i; // fetch a partition - mnt_mount_volume(mstg, part); // attempt mounting a volume + const PartEntry *part = parts + i; // fetch a partition + mnt_mount_volume(mstg, part); // attempt mounting a volume } return 0; @@ -86,8 +85,8 @@ int mnt_automount_disk(MassStorage * mstg) { * @param name volume name * @return pointer to volume, if found OR NULL */ -Mnt_Volume * mnt_get_volume_by_name(const char * name) { - Mnt_Volume * vol = NULL; +Mnt_Volume *mnt_get_volume_by_name(const char *name) { + Mnt_Volume *vol = NULL; for (uint8_t i = 0; i < mtab.mounted_vols; i++) { if (!strncmp(mtab.vols[i].mnt_name, name, MNT_MAX_MOUNT_NAME_LENGTH)) { vol = mtab.vols + i; @@ -106,8 +105,8 @@ Mnt_Volume * mnt_get_volume_by_name(const char * name) { * @param max_volname_len size of free area not including terminating zero * @return pointer to the relative path */ -const char * mnt_separate_volume_path(const char * volpath, char * volname, uint16_t max_volname_len) { - const char * iter = volpath; +const char *mnt_separate_volume_path(const char *volpath, char *volname, uint16_t max_volname_len) { + const char *iter = volpath; // strip first '/' if (*iter == MNT_DIRSEP) { @@ -138,8 +137,8 @@ const char * mnt_separate_volume_path(const char * volpath, char * volname, uint * @param path path of the file on the volume (relative to volume root) * @return pointer to a mounted volume OR NULL if not found */ -Mnt_Volume * mnt_get_volume_by_path(const char * volpath, const char ** path) { - const char * ret; +Mnt_Volume *mnt_get_volume_by_path(const char *volpath, const char **path) { + const char *ret; char volname[MNT_MAX_MOUNT_NAME_LENGTH + 1]; ret = mnt_separate_volume_path(volpath, volname, MNT_MAX_MOUNT_NAME_LENGTH); if (path != NULL) { @@ -156,12 +155,13 @@ Mnt_Volume * mnt_get_volume_by_path(const char * volpath, const char ** path) { * Allocate space for a newly opened file's helper object. * @return allocated slot OR NULL is allocation was not possible */ -Mnt_File * mnt_alloc_file_helper() { - Mnt_File * slot = NULL; - for (uint8_t i = 0; i < MNT_MAX_OPEN_FILES; i++) { +Mnt_File *mnt_alloc_file_helper() { + Mnt_File *slot = NULL; + for (uint8_t i = 0; (i < MNT_MAX_OPEN_FILES) && (ftab.open_files < MNT_MAX_OPEN_FILES); i++) { if (ftab.files[i].ctrl_word == MNT_FTAB_BLOCK_FREE) { ftab.files[i].ctrl_word = MNT_FTAB_BLOCK_OCCUPIED; slot = ftab.files + i; + ftab.open_files++; break; } } @@ -172,8 +172,11 @@ Mnt_File * mnt_alloc_file_helper() { * Release file helper. * @param helper pointer to helper */ -void mnt_free_file_helper(Mnt_File * file) { - file->ctrl_word = MNT_FTAB_BLOCK_FREE; +void mnt_free_file_helper(Mnt_File *file) { + if (ftab.open_files > 0) { + file->ctrl_word = MNT_FTAB_BLOCK_FREE; + ftab.open_files--; + } } // ----------------------- @@ -192,10 +195,10 @@ void mnt_list(const char *dir) { } // if not empty path is specified - const char * relpath; - Mnt_Volume * vol = mnt_get_volume_by_path(dir, &relpath); + const char *relpath; + Mnt_Volume *vol = mnt_get_volume_by_path(dir, &relpath); if (vol != NULL) { - (vol->driver).list((void *) vol->fs_control_block_area, relpath); + (vol->driver).list((void *)vol->fs_control_block_area, relpath); } } @@ -204,15 +207,19 @@ void mnt_list(const char *dir) { * @param path full path to file * @return pointer to file handle */ -Mnt_File * mnt_open(const char * path) { - Mnt_File * file = NULL; - const char * relpath; - Mnt_Volume * vol = mnt_get_volume_by_path(path, &relpath); +Mnt_File *mnt_open(const char *path) { + Mnt_File *file = NULL; + const char *relpath; + Mnt_Volume *vol = mnt_get_volume_by_path(path, &relpath); if (vol != NULL) { file = mnt_alloc_file_helper(); if (file != NULL) { file->vol = vol; - (vol->driver).open(vol->fs_control_block_area, relpath, file->helper); + int success = (vol->driver).open(vol->fs_control_block_area, relpath, file->helper); + if (success != 0) { // release file helper if could not open file + mnt_free_file_helper(file); + file = NULL; + } } } return file; @@ -225,9 +232,9 @@ Mnt_File * mnt_open(const char * path) { * @param buf destination buffer * @return actual read length */ -int mnt_read(Mnt_File * file, unsigned long len, void * buf) { - Mnt_Volume * vol = file->vol; - return vol->driver.read(vol->fs_control_block_area, (void *) file->helper, len, buf); +int mnt_read(Mnt_File *file, unsigned long len, void *buf) { + Mnt_Volume *vol = file->vol; + return vol->driver.read(vol->fs_control_block_area, (void *)file->helper, len, buf); } /** @@ -236,11 +243,41 @@ int mnt_read(Mnt_File * file, unsigned long len, void * buf) { * @param pos position from the beginning * @return ??? */ -int mnt_seek(Mnt_File * file, unsigned long pos) { - Mnt_Volume * vol = file->vol; - return vol->driver.seek(vol->fs_control_block_area, (void *) file->helper, pos); +int mnt_seek(Mnt_File *file, unsigned long pos) { + Mnt_Volume *vol = file->vol; + return vol->driver.seek(vol->fs_control_block_area, (void *)file->helper, pos); } void mnt_close(Mnt_File *file) { mnt_free_file_helper(file); } + +// -------------- + +/** + * Get file system name from type ID. + * @param fst file system type ID + * @return file system name + */ +static inline const char *mnt_fs_type_to_str(uint8_t fst) { + switch (fst) { + case FAT32_CHS_PART_ID: + case FAT32_LBA_PART_ID: + return "FAT32"; + default: + return "N/A"; + } +} + +/** + * Print state and statistical infromation. + */ +void mnt_print_info() { + MSG("\n\n---- MOUNT/FILES stats ----\n"); + MSG(" Mounted volumes: %u\n", mtab.mounted_vols); + for (uint32_t i = 0; i < mtab.mounted_vols; i++) { + MSG(" /%s %.3f MB\n", mtab.vols[i].mnt_name, mtab.vols[i].part_size * 1E-06); + } + MSG(" Open files: %u\n\n", ftab.open_files); + MSG("---------------------------\n\n"); +} diff --git a/mount/mount.h b/mount/mount.h index 1e6b996..f4d4d6f 100644 --- a/mount/mount.h +++ b/mount/mount.h @@ -31,6 +31,7 @@ typedef struct { typedef struct { Mnt_File files[MNT_MAX_OPEN_FILES]; // file helpers + uint8_t open_files; // number of open files } Mnt_FileTable; int mnt_automount_disk(MassStorage * mstg); // attempt to automount volumes @@ -45,4 +46,6 @@ int mnt_seek(Mnt_File * file, unsigned long pos); void mnt_close(Mnt_File * file); +void mnt_print_info(); + #endif //EMBPART_MOUNT_H