- mount: info printing added

This commit is contained in:
Wiesner András 2023-11-11 20:55:21 +01:00
parent 9363db67f8
commit d07827a66a
2 changed files with 83 additions and 43 deletions

View File

@ -1,16 +1,16 @@
#include <stddef.h>
#include <memory.h>
#include "mount.h" #include "mount.h"
#include "../MassStorage.h" #include "../MassStorage.h"
#include "../mbr/mbr.h"
#include "../fs/fat32/fat32.h" #include "../fs/fat32/fat32.h"
#include "../mbr/mbr.h"
#include <memory.h>
#include <stddef.h>
#define AUTOMOUNT_SECTOR_SIZE (512) #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_MountTable mtab = {0};
static Mnt_FileTable ftab = { 0 }; static Mnt_FileTable ftab = {0};
/** /**
* Attempts to mount a volume. * Attempts to mount a volume.
@ -18,10 +18,10 @@ static Mnt_FileTable ftab = { 0 };
* @param part pointer to partition entry * @param part pointer to partition entry
* @return 0 if concluded without error (even if partition of unknown type could not be mounted) * @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 // fetch common partition details
Mnt_Volume * next_entry = mtab.vols + mtab.mounted_vols; // pointer to control block area 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 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 next_entry->mnt_name[MNT_MAX_MOUNT_NAME_LENGTH] = '\0'; // write terminating zero in advance
int mount_success = -1; int mount_success = -1;
@ -31,14 +31,13 @@ static int mnt_mount_volume(MassStorage * mstg, const PartEntry * part) {
break; break;
case FAT32_CHS_PART_ID: // FAT32 case FAT32_CHS_PART_ID: // FAT32
case FAT32_LBA_PART_ID: { 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); mount_success = fat32_load(ctrl, part->lba_fa, mstg);
if (mount_success == 0) { if (mount_success == 0) {
fat32_get_volume_label(ctrl, next_entry->mnt_name, MNT_MAX_MOUNT_NAME_LENGTH); // get volume name 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 default: // unmountable file system's found on the disk
MSG("Unknown file system type: %d, cannot mount!\n", part->part_type); MSG("Unknown file system type: %d, cannot mount!\n", part->part_type);
break; break;
@ -47,7 +46,7 @@ static int mnt_mount_volume(MassStorage * mstg, const PartEntry * part) {
// if succeeded, then maintain mount table // if succeeded, then maintain mount table
if (mount_success > -1) { if (mount_success > -1) {
next_entry->part_size = part->sector_count * AUTOMOUNT_SECTOR_SIZE; // save volume size 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; return 0;
@ -58,13 +57,13 @@ static int mnt_mount_volume(MassStorage * mstg, const PartEntry * part) {
* @param mstg pointer to Mass Storage object * @param mstg pointer to Mass Storage object
* @return 0 if successful * @return 0 if successful
*/ */
int mnt_automount_disk(MassStorage * mstg) { int mnt_automount_disk(MassStorage *mstg) {
// load the first sector // load the first sector
uint8_t buf[AUTOMOUNT_SECTOR_SIZE]; uint8_t buf[AUTOMOUNT_SECTOR_SIZE];
mstg->read_sector(mstg, 0, buf); mstg->read_sector(mstg, 0, buf);
// fetch partitions from the disk // 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 if (partList == NULL) { // not an MBR disk
return 0; return 0;
} }
@ -74,8 +73,8 @@ int mnt_automount_disk(MassStorage * mstg) {
PartEntry parts[MBR_MAX_PARTITIONS]; PartEntry parts[MBR_MAX_PARTITIONS];
memcpy(parts, partList, sizeof(PartEntry) * MBR_MAX_PARTITIONS); // copy partition table 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++) { 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 const PartEntry *part = parts + i; // fetch a partition
mnt_mount_volume(mstg, part); // attempt mounting a volume mnt_mount_volume(mstg, part); // attempt mounting a volume
} }
return 0; return 0;
@ -86,8 +85,8 @@ int mnt_automount_disk(MassStorage * mstg) {
* @param name volume name * @param name volume name
* @return pointer to volume, if found OR NULL * @return pointer to volume, if found OR NULL
*/ */
Mnt_Volume * mnt_get_volume_by_name(const char * name) { Mnt_Volume *mnt_get_volume_by_name(const char *name) {
Mnt_Volume * vol = NULL; Mnt_Volume *vol = NULL;
for (uint8_t i = 0; i < mtab.mounted_vols; i++) { for (uint8_t i = 0; i < mtab.mounted_vols; i++) {
if (!strncmp(mtab.vols[i].mnt_name, name, MNT_MAX_MOUNT_NAME_LENGTH)) { if (!strncmp(mtab.vols[i].mnt_name, name, MNT_MAX_MOUNT_NAME_LENGTH)) {
vol = mtab.vols + i; 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 * @param max_volname_len size of free area not including terminating zero
* @return pointer to the relative path * @return pointer to the relative path
*/ */
const char * mnt_separate_volume_path(const char * volpath, char * volname, uint16_t max_volname_len) { const char *mnt_separate_volume_path(const char *volpath, char *volname, uint16_t max_volname_len) {
const char * iter = volpath; const char *iter = volpath;
// strip first '/' // strip first '/'
if (*iter == MNT_DIRSEP) { 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) * @param path path of the file on the volume (relative to volume root)
* @return pointer to a mounted volume OR NULL if not found * @return pointer to a mounted volume OR NULL if not found
*/ */
Mnt_Volume * mnt_get_volume_by_path(const char * volpath, const char ** path) { Mnt_Volume *mnt_get_volume_by_path(const char *volpath, const char **path) {
const char * ret; const char *ret;
char volname[MNT_MAX_MOUNT_NAME_LENGTH + 1]; char volname[MNT_MAX_MOUNT_NAME_LENGTH + 1];
ret = mnt_separate_volume_path(volpath, volname, MNT_MAX_MOUNT_NAME_LENGTH); ret = mnt_separate_volume_path(volpath, volname, MNT_MAX_MOUNT_NAME_LENGTH);
if (path != NULL) { 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. * Allocate space for a newly opened file's helper object.
* @return allocated slot OR NULL is allocation was not possible * @return allocated slot OR NULL is allocation was not possible
*/ */
Mnt_File * mnt_alloc_file_helper() { Mnt_File *mnt_alloc_file_helper() {
Mnt_File * slot = NULL; Mnt_File *slot = NULL;
for (uint8_t i = 0; i < MNT_MAX_OPEN_FILES; i++) { 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) { if (ftab.files[i].ctrl_word == MNT_FTAB_BLOCK_FREE) {
ftab.files[i].ctrl_word = MNT_FTAB_BLOCK_OCCUPIED; ftab.files[i].ctrl_word = MNT_FTAB_BLOCK_OCCUPIED;
slot = ftab.files + i; slot = ftab.files + i;
ftab.open_files++;
break; break;
} }
} }
@ -172,8 +172,11 @@ Mnt_File * mnt_alloc_file_helper() {
* Release file helper. * Release file helper.
* @param helper pointer to helper * @param helper pointer to helper
*/ */
void mnt_free_file_helper(Mnt_File * file) { void mnt_free_file_helper(Mnt_File *file) {
file->ctrl_word = MNT_FTAB_BLOCK_FREE; 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 // if not empty path is specified
const char * relpath; const char *relpath;
Mnt_Volume * vol = mnt_get_volume_by_path(dir, &relpath); Mnt_Volume *vol = mnt_get_volume_by_path(dir, &relpath);
if (vol != NULL) { 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 * @param path full path to file
* @return pointer to file handle * @return pointer to file handle
*/ */
Mnt_File * mnt_open(const char * path) { Mnt_File *mnt_open(const char *path) {
Mnt_File * file = NULL; Mnt_File *file = NULL;
const char * relpath; const char *relpath;
Mnt_Volume * vol = mnt_get_volume_by_path(path, &relpath); Mnt_Volume *vol = mnt_get_volume_by_path(path, &relpath);
if (vol != NULL) { if (vol != NULL) {
file = mnt_alloc_file_helper(); file = mnt_alloc_file_helper();
if (file != NULL) { if (file != NULL) {
file->vol = vol; 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; return file;
@ -225,9 +232,9 @@ Mnt_File * mnt_open(const char * path) {
* @param buf destination buffer * @param buf destination buffer
* @return actual read length * @return actual read length
*/ */
int mnt_read(Mnt_File * file, unsigned long len, void * buf) { int mnt_read(Mnt_File *file, unsigned long len, void *buf) {
Mnt_Volume * vol = file->vol; Mnt_Volume *vol = file->vol;
return vol->driver.read(vol->fs_control_block_area, (void *) file->helper, len, buf); 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 * @param pos position from the beginning
* @return ??? * @return ???
*/ */
int mnt_seek(Mnt_File * file, unsigned long pos) { int mnt_seek(Mnt_File *file, unsigned long pos) {
Mnt_Volume * vol = file->vol; Mnt_Volume *vol = file->vol;
return vol->driver.seek(vol->fs_control_block_area, (void *) file->helper, pos); return vol->driver.seek(vol->fs_control_block_area, (void *)file->helper, pos);
} }
void mnt_close(Mnt_File *file) { void mnt_close(Mnt_File *file) {
mnt_free_file_helper(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");
}

View File

@ -31,6 +31,7 @@ typedef struct {
typedef struct { typedef struct {
Mnt_File files[MNT_MAX_OPEN_FILES]; // file helpers Mnt_File files[MNT_MAX_OPEN_FILES]; // file helpers
uint8_t open_files; // number of open files
} Mnt_FileTable; } Mnt_FileTable;
int mnt_automount_disk(MassStorage * mstg); // attempt to automount volumes 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_close(Mnt_File * file);
void mnt_print_info();
#endif //EMBPART_MOUNT_H #endif //EMBPART_MOUNT_H