diff --git a/sys/fs/fat/fat.c b/sys/fs/fat/fat.c index 07b94a9..0eab1cd 100644 --- a/sys/fs/fat/fat.c +++ b/sys/fs/fat/fat.c @@ -1,5 +1,5 @@ /***************************************************************************************** - Copyright (c) 2019 The UbixOS Project + Copyright (c) 2002-2004 The UbixOS Project All rights reserved. Redistribution and use in source and binary forms, with or without modification, are @@ -23,7 +23,9 @@ TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - *****************************************************************************************/ + $Id: ubixfs.c 79 2016-01-11 16:21:27Z reddawg $ + +*****************************************************************************************/ #include #include @@ -40,34 +42,70 @@ fileDescriptor_t *_fd; struct vfs_mountPoint *_mp; -int media_read(unsigned long sector, unsigned char *buffer, - unsigned long sector_count) { - _mp->device->devInfo->read(_mp->device->devInfo->info, buffer, sector, - sector_count); +int media_read(unsigned long sector, unsigned char *buffer, unsigned long sector_count) +{ + _mp->device->devInfo->read(_mp->device->devInfo->info, buffer, sector, sector_count); - return 1; + return 1; } -int media_write(unsigned long sector, unsigned char *buffer, - unsigned long sector_count) { - _mp->device->devInfo->write(_mp->device->devInfo->info, buffer, sector, - sector_count); +int media_write(unsigned long sector, unsigned char *buffer, unsigned long sector_count) +{ + _mp->device->devInfo->write(_mp->device->devInfo->info, buffer, sector, sector_count); - return 1; + return 1; } + int fat_initialize(struct vfs_mountPoint *mp) { - // Attach media access functions to library - if (fl_attach_media(media_read, media_write) != FAT_INIT_OK) { - kprintf("ERROR: Media attach failed\n"); - return (0); - } + FL_FILE *file; + _mp = mp; + // Attach media access functions to library + if (fl_attach_media(media_read, media_write) != FAT_INIT_OK) + { + kprintf("ERROR: Media attach failed\n"); + return(0); + } + + // List root directory + fl_listdirectory("/"); + // Create File + /* + file = fl_fopen("/file.bin", "w"); + unsigned char data[] = { 1, 2, 3, 4 }; + if (file) + { + // Write some data + if (fl_fwrite(data, 1, sizeof(data), file) != sizeof(data)) + kprintf("ERROR: Write file failed\n"); + } + else + kprintf("ERROR: Create file failed\n"); - return (1); + // Close file + fl_fclose(file); + + fl_listdirectory("/"); + + // Delete File + if (fl_remove("/file.bin") < 0) + kprintf("ERROR: Delete file failed\n"); + + // List root directory + fl_listdirectory("/"); + + size_t size; + + file = fl_fopen("/shell", "r"); + size = fl_fread(data, 1, sizeof(data), file); + kprintf("READ: %i", size); + fl_fclose(file); + */ + return(1); } -int read_fat(fileDescriptor_t *fd, char *data, uInt32 offset, long size) { - FL_FILE *_file = (FL_FILE*) fd->res; +int read_fat(fileDescriptor_t *fd,char *data,uInt32 offset,long size) { + FL_FILE *_file = (FL_FILE *)fd->res; kprintf("Reading: %i[%i]\n", size, offset); fl_fseek(_file, offset, 0); @@ -76,11 +114,12 @@ kprintf("Read: %i\n", size); /* Return */ - return (size); -} + return(size); + } + int write_fat() { - return (0); + return(0); } int open_fat(const char *file, fileDescriptor_t *fd) { @@ -95,51 +134,54 @@ kprintf(file); - _file = fl_fopen(file, "r"); - if (!_file) { - kprintf("ERROR: Open file failed\n"); - return (0x0); - } - else { - fd->res = _file; - fd->perms = 0x1; - } + _file = fl_fopen(file, "r"); + if (!_file) { + kprintf("ERROR: Open file failed\n"); + return(0x0); + } + else { + fd->res = _file; + fd->perms = 0x1; + } - return (0x1); + return(0x1); } + + int unlink_fat() { - return (0); + return(0); } int mkdir_fat() { - return (0); + return(0); } int fat_init() { - // Initialise File IO Library - fl_init(); + // Initialise File IO Library + fl_init(); /* Set up our file system structure */ - struct fileSystem ubixFileSystem = { NULL, /* prev */ - NULL, /* next */ - (void*) fat_initialize, /* vfsInitFS */ - (void*) read_fat, /* vfsRead */ - (void*) write_fat, /* vfsWrite */ - (void*) open_fat, /* vfsOpenFile */ - (void*) unlink_fat, /* vfsUnlink */ - (void*) mkdir_fat, /* vfsMakeDir */ - NULL, /* vfsRemDir */ - NULL, /* vfsSync */ - 0xFA /* vfsType */ - }; /* ubixFileSystem */ + struct fileSystem ubixFileSystem = + {NULL, /* prev */ + NULL, /* next */ + (void *)fat_initialize, /* vfsInitFS */ + (void *)read_fat, /* vfsRead */ + (void *)write_fat, /* vfsWrite */ + (void *)open_fat, /* vfsOpenFile */ + (void *)unlink_fat, /* vfsUnlink */ + (void *)mkdir_fat, /* vfsMakeDir */ + NULL, /* vfsRemDir */ + NULL, /* vfsSync */ + 0xFA /* vfsType */ + }; /* ubixFileSystem */ /* Add UbixFS */ if (vfsRegisterFS(ubixFileSystem) != 0x0) { kpanic("Unable To Enable UbixFS"); - return (0x1); - } + return(0x1); + } /* Return */ - return (0x0); -} + return(0x0); + } diff --git a/sys/fs/fat/fat_filelib.c b/sys/fs/fat/fat_filelib.c index af17feb..354a352 100644 --- a/sys/fs/fat/fat_filelib.c +++ b/sys/fs/fat/fat_filelib.c @@ -43,12 +43,12 @@ //----------------------------------------------------------------------------- // Locals //----------------------------------------------------------------------------- -static FL_FILE _files[FATFS_MAX_OPEN_FILES]; -static int _filelib_init = 0; -static int _filelib_valid = 0; -static struct fatfs _fs; -static struct fat_list _open_file_list; -static struct fat_list _free_file_list; +static FL_FILE _files[FATFS_MAX_OPEN_FILES]; +static int _filelib_init = 0; +static int _filelib_valid = 0; +static struct fatfs _fs; +static struct fat_list _open_file_list; +static struct fat_list _free_file_list; //----------------------------------------------------------------------------- // Macros @@ -63,52 +63,55 @@ //----------------------------------------------------------------------------- // Local Functions //----------------------------------------------------------------------------- -static void _fl_init(); +static void _fl_init(); //----------------------------------------------------------------------------- // _allocate_file: Find a slot in the open files buffer for a new file //----------------------------------------------------------------------------- -static FL_FILE* _allocate_file(void) { - // Allocate free file - struct fat_node *node = fat_list_pop_head(&_free_file_list); +static FL_FILE* _allocate_file(void) +{ + // Allocate free file + struct fat_node *node = fat_list_pop_head(&_free_file_list); - // Add to open list - if (node) - fat_list_insert_last(&_open_file_list, node); + // Add to open list + if (node) + fat_list_insert_last(&_open_file_list, node); - return fat_list_entry(node, FL_FILE, list_node); + return fat_list_entry(node, FL_FILE, list_node); } //----------------------------------------------------------------------------- // _check_file_open: Returns true if the file is already open //----------------------------------------------------------------------------- -static int _check_file_open(FL_FILE *file) { - struct fat_node *node; +static int _check_file_open(FL_FILE* file) +{ + struct fat_node *node; - // Compare open files - fat_list_for_each(&_open_file_list, node) - { - FL_FILE *openFile = fat_list_entry(node, FL_FILE, list_node); + // Compare open files + fat_list_for_each(&_open_file_list, node) + { + FL_FILE* openFile = fat_list_entry(node, FL_FILE, list_node); - // If not the current file - if (openFile != file) { - // Compare path and name - if ((fatfs_compare_names(openFile->path, file->path)) - && (fatfs_compare_names(openFile->filename, file->filename))) - return 1; + // If not the current file + if (openFile != file) + { + // Compare path and name + if ( (fatfs_compare_names(openFile->path,file->path)) && (fatfs_compare_names(openFile->filename,file->filename)) ) + return 1; + } } - } - return 0; + return 0; } //----------------------------------------------------------------------------- // _free_file: Free open file handle //----------------------------------------------------------------------------- -static void _free_file(FL_FILE *file) { - // Remove from open list - fat_list_remove(&_open_file_list, &file->list_node); +static void _free_file(FL_FILE* file) +{ + // Remove from open list + fat_list_remove(&_open_file_list, &file->list_node); - // Add to free list - fat_list_insert_last(&_free_file_list, &file->list_node); + // Add to free list + fat_list_insert_last(&_free_file_list, &file->list_node); } //----------------------------------------------------------------------------- @@ -119,142 +122,150 @@ // _open_directory: Cycle through path string to find the start cluster // address of the highest subdir. //----------------------------------------------------------------------------- -static int _open_directory(char *path, uint32 *pathCluster) { - int levels; - int sublevel; - char currentfolder[FATFS_MAX_LONG_FILENAME]; - struct fat_dir_entry sfEntry; - uint32 startcluster; +static int _open_directory(char *path, uint32 *pathCluster) +{ + int levels; + int sublevel; + char currentfolder[FATFS_MAX_LONG_FILENAME]; + struct fat_dir_entry sfEntry; + uint32 startcluster; - // Set starting cluster to root cluster - startcluster = fatfs_get_root_cluster(&_fs); + // Set starting cluster to root cluster + startcluster = fatfs_get_root_cluster(&_fs); - // Find number of levels - levels = fatfs_total_path_levels(path); + // Find number of levels + levels = fatfs_total_path_levels(path); - // Cycle through each level and get the start sector - for (sublevel = 0; sublevel < (levels + 1); sublevel++) { - if (fatfs_get_substring(path, sublevel, currentfolder, - sizeof(currentfolder)) == -1) - return 0; + // Cycle through each level and get the start sector + for (sublevel=0;sublevel<(levels+1);sublevel++) + { + if (fatfs_get_substring(path, sublevel, currentfolder, sizeof(currentfolder)) == -1) + return 0; - // Find clusteraddress for folder (currentfolder) - if (fatfs_get_file_entry(&_fs, startcluster, currentfolder, &sfEntry)) { - // Check entry is folder - if (fatfs_entry_is_dir(&sfEntry)) - startcluster = ((FAT_HTONS((uint32 )sfEntry.FstClusHI)) << 16) - + FAT_HTONS(sfEntry.FstClusLO); - else - return 0; + // Find clusteraddress for folder (currentfolder) + if (fatfs_get_file_entry(&_fs, startcluster, currentfolder,&sfEntry)) + { + // Check entry is folder + if (fatfs_entry_is_dir(&sfEntry)) + startcluster = ((FAT_HTONS((uint32)sfEntry.FstClusHI))<<16) + FAT_HTONS(sfEntry.FstClusLO); + else + return 0; + } + else + return 0; } - else - return 0; - } - *pathCluster = startcluster; - return 1; + *pathCluster = startcluster; + return 1; } //----------------------------------------------------------------------------- // _create_directory: Cycle through path string and create the end directory //----------------------------------------------------------------------------- #if FATFS_INC_WRITE_SUPPORT -static int _create_directory(char *path) { - FL_FILE *file; - struct fat_dir_entry sfEntry; - char shortFilename[FAT_SFN_SIZE_FULL]; - int tailNum = 0; - int i; +static int _create_directory(char *path) +{ + FL_FILE* file; + struct fat_dir_entry sfEntry; + char shortFilename[FAT_SFN_SIZE_FULL]; + int tailNum = 0; + int i; - // Allocate a new file handle - file = _allocate_file(); - if (!file) - return 0; + // Allocate a new file handle + file = _allocate_file(); + if (!file) + return 0; - // Clear filename - memset(file->path, '\0', sizeof(file->path)); - memset(file->filename, '\0', sizeof(file->filename)); + // Clear filename + memset(file->path, '\0', sizeof(file->path)); + memset(file->filename, '\0', sizeof(file->filename)); - // Split full path into filename and directory path - if (fatfs_split_path((char*) path, file->path, sizeof(file->path), - file->filename, sizeof(file->filename)) == -1) { - _free_file(file); - return 0; - } - - // Check if file already open - if (_check_file_open(file)) { - _free_file(file); - return 0; - } - - // If file is in the root dir - if (file->path[0] == 0) - file->parentcluster = fatfs_get_root_cluster(&_fs); - else { - // Find parent directory start cluster - if (!_open_directory(file->path, &file->parentcluster)) { - _free_file(file); - return 0; + // Split full path into filename and directory path + if (fatfs_split_path((char*)path, file->path, sizeof(file->path), file->filename, sizeof(file->filename)) == -1) + { + _free_file(file); + return 0; } - } - // Check if same filename exists in directory - if (fatfs_get_file_entry(&_fs, file->parentcluster, file->filename, &sfEntry) - == 1) { - _free_file(file); - return 0; - } - - file->startcluster = 0; - - // Create the file space for the folder (at least one clusters worth!) - if (!fatfs_allocate_free_space(&_fs, 1, &file->startcluster, 1)) { - _free_file(file); - return 0; - } - - // Erase new directory cluster - memset(file->file_data_sector, 0x00, FAT_SECTOR_SIZE); - for (i = 0; i < _fs.sectors_per_cluster; i++) { - if (!fatfs_write_sector(&_fs, file->startcluster, i, - file->file_data_sector)) { - _free_file(file); - return 0; + // Check if file already open + if (_check_file_open(file)) + { + _free_file(file); + return 0; } - } + + // If file is in the root dir + if (file->path[0] == 0) + file->parentcluster = fatfs_get_root_cluster(&_fs); + else + { + // Find parent directory start cluster + if (!_open_directory(file->path, &file->parentcluster)) + { + _free_file(file); + return 0; + } + } + + // Check if same filename exists in directory + if (fatfs_get_file_entry(&_fs, file->parentcluster, file->filename,&sfEntry) == 1) + { + _free_file(file); + return 0; + } + + file->startcluster = 0; + + // Create the file space for the folder (at least one clusters worth!) + if (!fatfs_allocate_free_space(&_fs, 1, &file->startcluster, 1)) + { + _free_file(file); + return 0; + } + + // Erase new directory cluster + memset(file->file_data_sector, 0x00, FAT_SECTOR_SIZE); + for (i=0;i<_fs.sectors_per_cluster;i++) + { + if (!fatfs_write_sector(&_fs, file->startcluster, i, file->file_data_sector)) + { + _free_file(file); + return 0; + } + } #if FATFS_INC_LFN_SUPPORT - // Generate a short filename & tail - tailNum = 0; - do { - // Create a standard short filename (without tail) - fatfs_lfn_create_sfn(shortFilename, file->filename); + // Generate a short filename & tail + tailNum = 0; + do + { + // Create a standard short filename (without tail) + fatfs_lfn_create_sfn(shortFilename, file->filename); - // If second hit or more, generate a ~n tail - if (tailNum != 0) - fatfs_lfn_generate_tail((char*) file->shortfilename, shortFilename, - tailNum); - // Try with no tail if first entry - else - memcpy(file->shortfilename, shortFilename, FAT_SFN_SIZE_FULL); + // If second hit or more, generate a ~n tail + if (tailNum != 0) + fatfs_lfn_generate_tail((char*)file->shortfilename, shortFilename, tailNum); + // Try with no tail if first entry + else + memcpy(file->shortfilename, shortFilename, FAT_SFN_SIZE_FULL); - // Check if entry exists already or not - if (fatfs_sfn_exists(&_fs, file->parentcluster, (char*) file->shortfilename) - == 0) - break; + // Check if entry exists already or not + if (fatfs_sfn_exists(&_fs, file->parentcluster, (char*)file->shortfilename) == 0) + break; - tailNum++; - } while (tailNum < 9999); + tailNum++; + } + while (tailNum < 9999); - // We reached the max number of duplicate short file names (unlikely!) - if (tailNum == 9999) { - // Delete allocated space - fatfs_free_cluster_chain(&_fs, file->startcluster); + // We reached the max number of duplicate short file names (unlikely!) + if (tailNum == 9999) + { + // Delete allocated space + fatfs_free_cluster_chain(&_fs, file->startcluster); - _free_file(file); - return 0; - } + _free_file(file); + return 0; + } #else // Create a standard short filename (without tail) if (!fatfs_lfn_create_sfn(shortFilename, file->filename)) @@ -280,194 +291,204 @@ } #endif - // Add file to disk - if (!fatfs_add_file_entry(&_fs, file->parentcluster, (char*) file->filename, - (char*) file->shortfilename, file->startcluster, 0, 1)) { - // Delete allocated space - fatfs_free_cluster_chain(&_fs, file->startcluster); + // Add file to disk + if (!fatfs_add_file_entry(&_fs, file->parentcluster, (char*)file->filename, (char*)file->shortfilename, file->startcluster, 0, 1)) + { + // Delete allocated space + fatfs_free_cluster_chain(&_fs, file->startcluster); + + _free_file(file); + return 0; + } + + // General + file->filelength = 0; + file->bytenum = 0; + file->file_data_address = 0xFFFFFFFF; + file->file_data_dirty = 0; + file->filelength_changed = 0; + + // Quick lookup for next link in the chain + file->last_fat_lookup.ClusterIdx = 0xFFFFFFFF; + file->last_fat_lookup.CurrentCluster = 0xFFFFFFFF; + + fatfs_fat_purge(&_fs); _free_file(file); - return 0; - } - - // General - file->filelength = 0; - file->bytenum = 0; - file->file_data_address = 0xFFFFFFFF; - file->file_data_dirty = 0; - file->filelength_changed = 0; - - // Quick lookup for next link in the chain - file->last_fat_lookup.ClusterIdx = 0xFFFFFFFF; - file->last_fat_lookup.CurrentCluster = 0xFFFFFFFF; - - fatfs_fat_purge(&_fs); - - _free_file(file); - return 1; + return 1; } #endif //----------------------------------------------------------------------------- // _open_file: Open a file for reading //----------------------------------------------------------------------------- -static FL_FILE* _open_file(const char *path) { - FL_FILE *file; - struct fat_dir_entry sfEntry; +static FL_FILE* _open_file(const char *path) +{ + FL_FILE* file; + struct fat_dir_entry sfEntry; - // Allocate a new file handle - file = _allocate_file(); - if (!file) - return NULL; + // Allocate a new file handle + file = _allocate_file(); + if (!file) + return NULL; - // Clear filename - memset(file->path, '\0', sizeof(file->path)); - memset(file->filename, '\0', sizeof(file->filename)); + // Clear filename + memset(file->path, '\0', sizeof(file->path)); + memset(file->filename, '\0', sizeof(file->filename)); - // Split full path into filename and directory path - if (fatfs_split_path((char*) path, file->path, sizeof(file->path), - file->filename, sizeof(file->filename)) == -1) { - _free_file(file); - return NULL; - } - - // Check if file already open - if (_check_file_open(file)) { - _free_file(file); - return NULL; - } - - // If file is in the root dir - if (file->path[0] == 0) - file->parentcluster = fatfs_get_root_cluster(&_fs); - else { - // Find parent directory start cluster - if (!_open_directory(file->path, &file->parentcluster)) { - _free_file(file); - return NULL; - } - } - - // Using dir cluster address search for filename - if (fatfs_get_file_entry(&_fs, file->parentcluster, file->filename, &sfEntry)) - // Make sure entry is file not dir! - if (fatfs_entry_is_file(&sfEntry)) { - // Initialise file details - memcpy(file->shortfilename, sfEntry.Name, FAT_SFN_SIZE_FULL); - file->filelength = FAT_HTONL(sfEntry.FileSize); - file->bytenum = 0; - file->startcluster = ((FAT_HTONS((uint32 )sfEntry.FstClusHI)) << 16) - + FAT_HTONS(sfEntry.FstClusLO); - file->file_data_address = 0xFFFFFFFF; - file->file_data_dirty = 0; - file->filelength_changed = 0; - - // Quick lookup for next link in the chain - file->last_fat_lookup.ClusterIdx = 0xFFFFFFFF; - file->last_fat_lookup.CurrentCluster = 0xFFFFFFFF; - - fatfs_cache_init(&_fs, file); - - fatfs_fat_purge(&_fs); - - return file; + // Split full path into filename and directory path + if (fatfs_split_path((char*)path, file->path, sizeof(file->path), file->filename, sizeof(file->filename)) == -1) + { + _free_file(file); + return NULL; } - _free_file(file); - return NULL; + // Check if file already open + if (_check_file_open(file)) + { + _free_file(file); + return NULL; + } + + // If file is in the root dir + if (file->path[0]==0) + file->parentcluster = fatfs_get_root_cluster(&_fs); + else + { + // Find parent directory start cluster + if (!_open_directory(file->path, &file->parentcluster)) + { + _free_file(file); + return NULL; + } + } + + // Using dir cluster address search for filename + if (fatfs_get_file_entry(&_fs, file->parentcluster, file->filename,&sfEntry)) + // Make sure entry is file not dir! + if (fatfs_entry_is_file(&sfEntry)) + { + // Initialise file details + memcpy(file->shortfilename, sfEntry.Name, FAT_SFN_SIZE_FULL); + file->filelength = FAT_HTONL(sfEntry.FileSize); + file->bytenum = 0; + file->startcluster = ((FAT_HTONS((uint32)sfEntry.FstClusHI))<<16) + FAT_HTONS(sfEntry.FstClusLO); + file->file_data_address = 0xFFFFFFFF; + file->file_data_dirty = 0; + file->filelength_changed = 0; + + // Quick lookup for next link in the chain + file->last_fat_lookup.ClusterIdx = 0xFFFFFFFF; + file->last_fat_lookup.CurrentCluster = 0xFFFFFFFF; + + fatfs_cache_init(&_fs, file); + + fatfs_fat_purge(&_fs); + + return file; + } + + _free_file(file); + return NULL; } //----------------------------------------------------------------------------- // _create_file: Create a new file //----------------------------------------------------------------------------- #if FATFS_INC_WRITE_SUPPORT -static FL_FILE* _create_file(const char *filename) { - FL_FILE *file; - struct fat_dir_entry sfEntry; - char shortFilename[FAT_SFN_SIZE_FULL]; - int tailNum = 0; +static FL_FILE* _create_file(const char *filename) +{ + FL_FILE* file; + struct fat_dir_entry sfEntry; + char shortFilename[FAT_SFN_SIZE_FULL]; + int tailNum = 0; - // No write access? - if (!_fs.disk_io.write_media) - return NULL; + // No write access? + if (!_fs.disk_io.write_media) + return NULL; - // Allocate a new file handle - file = _allocate_file(); - if (!file) - return NULL; + // Allocate a new file handle + file = _allocate_file(); + if (!file) + return NULL; - // Clear filename - memset(file->path, '\0', sizeof(file->path)); - memset(file->filename, '\0', sizeof(file->filename)); + // Clear filename + memset(file->path, '\0', sizeof(file->path)); + memset(file->filename, '\0', sizeof(file->filename)); - // Split full path into filename and directory path - if (fatfs_split_path((char*) filename, file->path, sizeof(file->path), - file->filename, sizeof(file->filename)) == -1) { - _free_file(file); - return NULL; - } - - // Check if file already open - if (_check_file_open(file)) { - _free_file(file); - return NULL; - } - - // If file is in the root dir - if (file->path[0] == 0) - file->parentcluster = fatfs_get_root_cluster(&_fs); - else { - // Find parent directory start cluster - if (!_open_directory(file->path, &file->parentcluster)) { - _free_file(file); - return NULL; + // Split full path into filename and directory path + if (fatfs_split_path((char*)filename, file->path, sizeof(file->path), file->filename, sizeof(file->filename)) == -1) + { + _free_file(file); + return NULL; } - } - // Check if same filename exists in directory - if (fatfs_get_file_entry(&_fs, file->parentcluster, file->filename, &sfEntry) - == 1) { - _free_file(file); - return NULL; - } + // Check if file already open + if (_check_file_open(file)) + { + _free_file(file); + return NULL; + } - file->startcluster = 0; + // If file is in the root dir + if (file->path[0] == 0) + file->parentcluster = fatfs_get_root_cluster(&_fs); + else + { + // Find parent directory start cluster + if (!_open_directory(file->path, &file->parentcluster)) + { + _free_file(file); + return NULL; + } + } - // Create the file space for the file (at least one clusters worth!) - if (!fatfs_allocate_free_space(&_fs, 1, &file->startcluster, 1)) { - _free_file(file); - return NULL; - } + // Check if same filename exists in directory + if (fatfs_get_file_entry(&_fs, file->parentcluster, file->filename,&sfEntry) == 1) + { + _free_file(file); + return NULL; + } + + file->startcluster = 0; + + // Create the file space for the file (at least one clusters worth!) + if (!fatfs_allocate_free_space(&_fs, 1, &file->startcluster, 1)) + { + _free_file(file); + return NULL; + } #if FATFS_INC_LFN_SUPPORT - // Generate a short filename & tail - tailNum = 0; - do { - // Create a standard short filename (without tail) - fatfs_lfn_create_sfn(shortFilename, file->filename); + // Generate a short filename & tail + tailNum = 0; + do + { + // Create a standard short filename (without tail) + fatfs_lfn_create_sfn(shortFilename, file->filename); - // If second hit or more, generate a ~n tail - if (tailNum != 0) - fatfs_lfn_generate_tail((char*) file->shortfilename, shortFilename, - tailNum); - // Try with no tail if first entry - else - memcpy(file->shortfilename, shortFilename, FAT_SFN_SIZE_FULL); + // If second hit or more, generate a ~n tail + if (tailNum != 0) + fatfs_lfn_generate_tail((char*)file->shortfilename, shortFilename, tailNum); + // Try with no tail if first entry + else + memcpy(file->shortfilename, shortFilename, FAT_SFN_SIZE_FULL); - // Check if entry exists already or not - if (fatfs_sfn_exists(&_fs, file->parentcluster, (char*) file->shortfilename) - == 0) - break; + // Check if entry exists already or not + if (fatfs_sfn_exists(&_fs, file->parentcluster, (char*)file->shortfilename) == 0) + break; - tailNum++; - } while (tailNum < 9999); + tailNum++; + } + while (tailNum < 9999); - // We reached the max number of duplicate short file names (unlikely!) - if (tailNum == 9999) { - // Delete allocated space - fatfs_free_cluster_chain(&_fs, file->startcluster); + // We reached the max number of duplicate short file names (unlikely!) + if (tailNum == 9999) + { + // Delete allocated space + fatfs_free_cluster_chain(&_fs, file->startcluster); - _free_file(file); - return NULL; - } + _free_file(file); + return NULL; + } #else // Create a standard short filename (without tail) if (!fatfs_lfn_create_sfn(shortFilename, file->filename)) @@ -493,105 +514,111 @@ } #endif - // Add file to disk - if (!fatfs_add_file_entry(&_fs, file->parentcluster, (char*) file->filename, - (char*) file->shortfilename, file->startcluster, 0, 0)) { - // Delete allocated space - fatfs_free_cluster_chain(&_fs, file->startcluster); + // Add file to disk + if (!fatfs_add_file_entry(&_fs, file->parentcluster, (char*)file->filename, (char*)file->shortfilename, file->startcluster, 0, 0)) + { + // Delete allocated space + fatfs_free_cluster_chain(&_fs, file->startcluster); - _free_file(file); - return NULL; - } + _free_file(file); + return NULL; + } - // General - file->filelength = 0; - file->bytenum = 0; - file->file_data_address = 0xFFFFFFFF; - file->file_data_dirty = 0; - file->filelength_changed = 0; + // General + file->filelength = 0; + file->bytenum = 0; + file->file_data_address = 0xFFFFFFFF; + file->file_data_dirty = 0; + file->filelength_changed = 0; - // Quick lookup for next link in the chain - file->last_fat_lookup.ClusterIdx = 0xFFFFFFFF; - file->last_fat_lookup.CurrentCluster = 0xFFFFFFFF; + // Quick lookup for next link in the chain + file->last_fat_lookup.ClusterIdx = 0xFFFFFFFF; + file->last_fat_lookup.CurrentCluster = 0xFFFFFFFF; - fatfs_cache_init(&_fs, file); + fatfs_cache_init(&_fs, file); - fatfs_fat_purge(&_fs); + fatfs_fat_purge(&_fs); - return file; + return file; } #endif //----------------------------------------------------------------------------- // _read_sectors: Read sector(s) from disk to file //----------------------------------------------------------------------------- -static uint32 _read_sectors(FL_FILE *file, uint32 offset, uint8 *buffer, - uint32 count) { - uint32 Sector = 0; - uint32 ClusterIdx = 0; - uint32 Cluster = 0; - uint32 i; - uint32 lba; +static uint32 _read_sectors(FL_FILE* file, uint32 offset, uint8 *buffer, uint32 count) +{ + uint32 Sector = 0; + uint32 ClusterIdx = 0; + uint32 Cluster = 0; + uint32 i; + uint32 lba; - // Find cluster index within file & sector with cluster - ClusterIdx = offset / _fs.sectors_per_cluster; - Sector = offset - (ClusterIdx * _fs.sectors_per_cluster); + // Find cluster index within file & sector with cluster + ClusterIdx = offset / _fs.sectors_per_cluster; + Sector = offset - (ClusterIdx * _fs.sectors_per_cluster); - // Limit number of sectors read to the number remaining in this cluster - if ((Sector + count) > _fs.sectors_per_cluster) - count = _fs.sectors_per_cluster - Sector; + // Limit number of sectors read to the number remaining in this cluster + if ((Sector + count) > _fs.sectors_per_cluster) + count = _fs.sectors_per_cluster - Sector; - // Quick lookup for next link in the chain - if (ClusterIdx == file->last_fat_lookup.ClusterIdx) - Cluster = file->last_fat_lookup.CurrentCluster; - // Else walk the chain - else { - // Starting from last recorded cluster? - if (ClusterIdx && ClusterIdx == file->last_fat_lookup.ClusterIdx + 1) { - i = file->last_fat_lookup.ClusterIdx; - Cluster = file->last_fat_lookup.CurrentCluster; - } - // Start searching from the beginning.. - else { - // Set start of cluster chain to initial value - i = 0; - Cluster = file->startcluster; + // Quick lookup for next link in the chain + if (ClusterIdx == file->last_fat_lookup.ClusterIdx) + Cluster = file->last_fat_lookup.CurrentCluster; + // Else walk the chain + else + { + // Starting from last recorded cluster? + if (ClusterIdx && ClusterIdx == file->last_fat_lookup.ClusterIdx + 1) + { + i = file->last_fat_lookup.ClusterIdx; + Cluster = file->last_fat_lookup.CurrentCluster; + } + // Start searching from the beginning.. + else + { + // Set start of cluster chain to initial value + i = 0; + Cluster = file->startcluster; + } + + // Follow chain to find cluster to read + for ( ;ilast_fat_lookup.CurrentCluster = Cluster; + file->last_fat_lookup.ClusterIdx = ClusterIdx; + } } - // Follow chain to find cluster to read - for (; i < ClusterIdx; i++) { - uint32 nextCluster; + // If end of cluster chain then return false + if (Cluster == FAT32_LAST_CLUSTER) + return 0; - // Does the entry exist in the cache? - if (!fatfs_cache_get_next_cluster(&_fs, file, i, &nextCluster)) { - // Scan file linked list to find next entry - nextCluster = fatfs_find_next_cluster(&_fs, Cluster); + // Calculate sector address + lba = fatfs_lba_of_cluster(&_fs, Cluster) + Sector; - // Push entry into cache - fatfs_cache_set_next_cluster(&_fs, file, i, nextCluster); - } - - Cluster = nextCluster; - } - - // Record current cluster lookup details (if valid) - if (Cluster != FAT32_LAST_CLUSTER) { - file->last_fat_lookup.CurrentCluster = Cluster; - file->last_fat_lookup.ClusterIdx = ClusterIdx; - } - } - - // If end of cluster chain then return false - if (Cluster == FAT32_LAST_CLUSTER) - return 0; - - // Calculate sector address - lba = fatfs_lba_of_cluster(&_fs, Cluster) + Sector; - - // Read sector of file - if (fatfs_sector_read(&_fs, lba, buffer, count)) - return count; - else - return 0; + // Read sector of file + if (fatfs_sector_read(&_fs, lba, buffer, count)) + return count; + else + return 0; } //----------------------------------------------------------------------------- @@ -601,716 +628,757 @@ //----------------------------------------------------------------------------- // fl_init: Initialise library //----------------------------------------------------------------------------- -void fl_init(void) { - int i; +void fl_init(void) +{ + int i; - fat_list_init(&_free_file_list); - fat_list_init(&_open_file_list); + fat_list_init(&_free_file_list); + fat_list_init(&_open_file_list); - // Add all file objects to free list - for (i = 0; i < FATFS_MAX_OPEN_FILES; i++) - fat_list_insert_last(&_free_file_list, &_files[i].list_node); + // Add all file objects to free list + for (i=0;iflags = flags; + // Create New +#if FATFS_INC_WRITE_SUPPORT + if (!file && (flags & FILE_CREATE)) + file = _create_file(path); +#endif - FL_UNLOCK(&_fs); - return file; + // Write Existing (and not open due to read or create) + if (!(flags & FILE_READ)) + if ((flags & FILE_CREATE) && !file) + if (flags & (FILE_WRITE | FILE_APPEND)) + file = _open_file(path); + + if (file) + file->flags = flags; + + FL_UNLOCK(&_fs); + return file; } //----------------------------------------------------------------------------- // _write_sectors: Write sector(s) to disk //----------------------------------------------------------------------------- #if FATFS_INC_WRITE_SUPPORT -static uint32 _write_sectors(FL_FILE *file, uint32 offset, uint8 *buf, - uint32 count) { - uint32 SectorNumber = 0; - uint32 ClusterIdx = 0; - uint32 Cluster = 0; - uint32 LastCluster = FAT32_LAST_CLUSTER; - uint32 i; - uint32 lba; - uint32 TotalWriteCount = count; +static uint32 _write_sectors(FL_FILE* file, uint32 offset, uint8 *buf, uint32 count) +{ + uint32 SectorNumber = 0; + uint32 ClusterIdx = 0; + uint32 Cluster = 0; + uint32 LastCluster = FAT32_LAST_CLUSTER; + uint32 i; + uint32 lba; + uint32 TotalWriteCount = count; - // Find values for Cluster index & sector within cluster - ClusterIdx = offset / _fs.sectors_per_cluster; - SectorNumber = offset - (ClusterIdx * _fs.sectors_per_cluster); + // Find values for Cluster index & sector within cluster + ClusterIdx = offset / _fs.sectors_per_cluster; + SectorNumber = offset - (ClusterIdx * _fs.sectors_per_cluster); - // Limit number of sectors written to the number remaining in this cluster - if ((SectorNumber + count) > _fs.sectors_per_cluster) - count = _fs.sectors_per_cluster - SectorNumber; + // Limit number of sectors written to the number remaining in this cluster + if ((SectorNumber + count) > _fs.sectors_per_cluster) + count = _fs.sectors_per_cluster - SectorNumber; - // Quick lookup for next link in the chain - if (ClusterIdx == file->last_fat_lookup.ClusterIdx) - Cluster = file->last_fat_lookup.CurrentCluster; - // Else walk the chain - else { - // Starting from last recorded cluster? - if (ClusterIdx && ClusterIdx == file->last_fat_lookup.ClusterIdx + 1) { - i = file->last_fat_lookup.ClusterIdx; - Cluster = file->last_fat_lookup.CurrentCluster; - } - // Start searching from the beginning.. - else { - // Set start of cluster chain to initial value - i = 0; - Cluster = file->startcluster; + // Quick lookup for next link in the chain + if (ClusterIdx == file->last_fat_lookup.ClusterIdx) + Cluster = file->last_fat_lookup.CurrentCluster; + // Else walk the chain + else + { + // Starting from last recorded cluster? + if (ClusterIdx && ClusterIdx == file->last_fat_lookup.ClusterIdx + 1) + { + i = file->last_fat_lookup.ClusterIdx; + Cluster = file->last_fat_lookup.CurrentCluster; + } + // Start searching from the beginning.. + else + { + // Set start of cluster chain to initial value + i = 0; + Cluster = file->startcluster; + } + + // Follow chain to find cluster to read + for ( ;ilast_fat_lookup.CurrentCluster = Cluster; + file->last_fat_lookup.ClusterIdx = ClusterIdx; } - // Follow chain to find cluster to read - for (; i < ClusterIdx; i++) { - uint32 nextCluster; + // Calculate write address + lba = fatfs_lba_of_cluster(&_fs, Cluster) + SectorNumber; - // Does the entry exist in the cache? - if (!fatfs_cache_get_next_cluster(&_fs, file, i, &nextCluster)) { - // Scan file linked list to find next entry - nextCluster = fatfs_find_next_cluster(&_fs, Cluster); - - // Push entry into cache - fatfs_cache_set_next_cluster(&_fs, file, i, nextCluster); - } - - LastCluster = Cluster; - Cluster = nextCluster; - - // Dont keep following a dead end - if (Cluster == FAT32_LAST_CLUSTER) - break; - } - - // If we have reached the end of the chain, allocate more! - if (Cluster == FAT32_LAST_CLUSTER) { - // Add some more cluster(s) to the last good cluster chain - if (!fatfs_add_free_space(&_fs, &LastCluster, - (TotalWriteCount + _fs.sectors_per_cluster - 1) - / _fs.sectors_per_cluster)) + if (fatfs_sector_write(&_fs, lba, buf, count)) + return count; + else return 0; - - Cluster = LastCluster; - } - - // Record current cluster lookup details - file->last_fat_lookup.CurrentCluster = Cluster; - file->last_fat_lookup.ClusterIdx = ClusterIdx; - } - - // Calculate write address - lba = fatfs_lba_of_cluster(&_fs, Cluster) + SectorNumber; - - if (fatfs_sector_write(&_fs, lba, buf, count)) - return count; - else - return 0; } #endif //----------------------------------------------------------------------------- // fl_fflush: Flush un-written data to the file //----------------------------------------------------------------------------- -int fl_fflush(void *f) { +int fl_fflush(void *f) +{ #if FATFS_INC_WRITE_SUPPORT - FL_FILE *file = (FL_FILE*) f; + FL_FILE *file = (FL_FILE *)f; - // If first call to library, initialise - CHECK_FL_INIT(); + // If first call to library, initialise + CHECK_FL_INIT(); - if (file) { - FL_LOCK(&_fs); + if (file) + { + FL_LOCK(&_fs); - // If some write data still in buffer - if (file->file_data_dirty) { - // Write back current sector before loading next - if (_write_sectors(file, file->file_data_address, file->file_data_sector, - 1)) - file->file_data_dirty = 0; + // If some write data still in buffer + if (file->file_data_dirty) + { + // Write back current sector before loading next + if (_write_sectors(file, file->file_data_address, file->file_data_sector, 1)) + file->file_data_dirty = 0; + } + + FL_UNLOCK(&_fs); } - - FL_UNLOCK(&_fs); - } #endif - return 0; + return 0; } //----------------------------------------------------------------------------- // fl_fclose: Close an open file //----------------------------------------------------------------------------- -void fl_fclose(void *f) { - FL_FILE *file = (FL_FILE*) f; +void fl_fclose(void *f) +{ + FL_FILE *file = (FL_FILE *)f; - // If first call to library, initialise - CHECK_FL_INIT(); + // If first call to library, initialise + CHECK_FL_INIT(); - if (file) { - FL_LOCK(&_fs); + if (file) + { + FL_LOCK(&_fs); - // Flush un-written data to file - fl_fflush(f); + // Flush un-written data to file + fl_fflush(f); - // File size changed? - if (file->filelength_changed) { + // File size changed? + if (file->filelength_changed) + { #if FATFS_INC_WRITE_SUPPORT - // Update filesize in directory - fatfs_update_file_length(&_fs, file->parentcluster, - (char*) file->shortfilename, file->filelength); + // Update filesize in directory + fatfs_update_file_length(&_fs, file->parentcluster, (char*)file->shortfilename, file->filelength); #endif - file->filelength_changed = 0; + file->filelength_changed = 0; + } + + file->bytenum = 0; + file->filelength = 0; + file->startcluster = 0; + file->file_data_address = 0xFFFFFFFF; + file->file_data_dirty = 0; + file->filelength_changed = 0; + + // Free file handle + _free_file(file); + + fatfs_fat_purge(&_fs); + + FL_UNLOCK(&_fs); } - - file->bytenum = 0; - file->filelength = 0; - file->startcluster = 0; - file->file_data_address = 0xFFFFFFFF; - file->file_data_dirty = 0; - file->filelength_changed = 0; - - // Free file handle - _free_file(file); - - fatfs_fat_purge(&_fs); - - FL_UNLOCK(&_fs); - } } //----------------------------------------------------------------------------- // fl_fgetc: Get a character in the stream //----------------------------------------------------------------------------- -int fl_fgetc(void *f) { - int res; - uint8 data = 0; +int fl_fgetc(void *f) +{ + int res; + uint8 data = 0; - res = fl_fread(&data, 1, 1, f); - if (res == 1) - return (int) data; - else - return res; + res = fl_fread(&data, 1, 1, f); + if (res == 1) + return (int)data; + else + return res; } //----------------------------------------------------------------------------- // fl_fgets: Get a string from a stream //----------------------------------------------------------------------------- -char* fl_fgets(char *s, int n, void *f) { - int idx = 0; +char *fl_fgets(char *s, int n, void *f) +{ + int idx = 0; - // Space for null terminator? - if (n > 0) { - // While space (+space for null terminator) - while (idx < (n - 1)) { - int ch = fl_fgetc(f); + // Space for null terminator? + if (n > 0) + { + // While space (+space for null terminator) + while (idx < (n-1)) + { + int ch = fl_fgetc(f); - // EOF / Error? - if (ch < 0) - break; + // EOF / Error? + if (ch < 0) + break; - // Store character read from stream - s[idx++] = (char) ch; + // Store character read from stream + s[idx++] = (char)ch; - // End of line? - if (ch == '\n') - break; + // End of line? + if (ch == '\n') + break; + } + + if (idx > 0) + s[idx] = '\0'; } - if (idx > 0) - s[idx] = '\0'; - } - - return (idx > 0) ? s : 0; + return (idx > 0) ? s : 0; } //----------------------------------------------------------------------------- // fl_fread: Read a block of data from the file //----------------------------------------------------------------------------- -int fl_fread(void *buffer, int size, int length, void *f) { - uint32 sector; - uint32 offset; - int copyCount; - int count = size * length; - int bytesRead = 0; +int fl_fread(void * buffer, int size, int length, void *f ) +{ + uint32 sector; + uint32 offset; + int copyCount; + int count = size * length; + int bytesRead = 0; - FL_FILE *file = (FL_FILE*) f; + FL_FILE *file = (FL_FILE *)f; - // If first call to library, initialise - CHECK_FL_INIT(); + // If first call to library, initialise + CHECK_FL_INIT(); - if (buffer == NULL || file == NULL) - return -1; + if (buffer==NULL || file==NULL) + return -1; - // No read permissions - if (!(file->flags & FILE_READ)) - return -1; + // No read permissions + if (!(file->flags & FILE_READ)) + return -1; - // Nothing to be done - if (!count) - return 0; + // Nothing to be done + if (!count) + return 0; - // Check if read starts past end of file - if (file->bytenum >= file->filelength) - return -1; + // Check if read starts past end of file + if (file->bytenum >= file->filelength) + return -1; - // Limit to file size - if ((file->bytenum + count) > file->filelength) - count = file->filelength - file->bytenum; + // Limit to file size + if ( (file->bytenum + count) > file->filelength ) + count = file->filelength - file->bytenum; - // Calculate start sector - sector = file->bytenum / FAT_SECTOR_SIZE; + // Calculate start sector + sector = file->bytenum / FAT_SECTOR_SIZE; - // Offset to start copying data from first sector - offset = file->bytenum % FAT_SECTOR_SIZE; + // Offset to start copying data from first sector + offset = file->bytenum % FAT_SECTOR_SIZE; - while (bytesRead < count) { - // Read whole sector, read from media directly into target buffer - if ((offset == 0) && ((count - bytesRead) >= FAT_SECTOR_SIZE)) { - // Read as many sectors as possible into target buffer - uint32 sectorsRead = _read_sectors(file, sector, - (uint8*) ((uint8*) buffer + bytesRead), - (count - bytesRead) / FAT_SECTOR_SIZE); - if (sectorsRead) { - // We have upto one sector to copy - copyCount = FAT_SECTOR_SIZE * sectorsRead; + while (bytesRead < count) + { + // Read whole sector, read from media directly into target buffer + if ((offset == 0) && ((count - bytesRead) >= FAT_SECTOR_SIZE)) + { + // Read as many sectors as possible into target buffer + uint32 sectorsRead = _read_sectors(file, sector, (uint8*)((uint8*)buffer + bytesRead), (count - bytesRead) / FAT_SECTOR_SIZE); + if (sectorsRead) + { + // We have upto one sector to copy + copyCount = FAT_SECTOR_SIZE * sectorsRead; - // Move onto next sector and reset copy offset - sector += sectorsRead; - offset = 0; - } - else - break; - } - else { - // Do we need to re-read the sector? - if (file->file_data_address != sector) { - // Flush un-written data to file - if (file->file_data_dirty) - fl_fflush(file); + // Move onto next sector and reset copy offset + sector+= sectorsRead; + offset = 0; + } + else + break; + } + else + { + // Do we need to re-read the sector? + if (file->file_data_address != sector) + { + // Flush un-written data to file + if (file->file_data_dirty) + fl_fflush(file); - // Get LBA of sector offset within file - if (!_read_sectors(file, sector, file->file_data_sector, 1)) - // Read failed - out of range (probably) - break; + // Get LBA of sector offset within file + if (!_read_sectors(file, sector, file->file_data_sector, 1)) + // Read failed - out of range (probably) + break; - file->file_data_address = sector; - file->file_data_dirty = 0; - } + file->file_data_address = sector; + file->file_data_dirty = 0; + } - // We have upto one sector to copy - copyCount = FAT_SECTOR_SIZE - offset; + // We have upto one sector to copy + copyCount = FAT_SECTOR_SIZE - offset; - // Only require some of this sector? - if (copyCount > (count - bytesRead)) - copyCount = (count - bytesRead); + // Only require some of this sector? + if (copyCount > (count - bytesRead)) + copyCount = (count - bytesRead); - // Copy to application buffer - memcpy((uint8*) ((uint8*) buffer + bytesRead), - (uint8*) (file->file_data_sector + offset), copyCount); + // Copy to application buffer + memcpy( (uint8*)((uint8*)buffer + bytesRead), (uint8*)(file->file_data_sector + offset), copyCount); - // Move onto next sector and reset copy offset - sector++; - offset = 0; + // Move onto next sector and reset copy offset + sector++; + offset = 0; + } + + // Increase total read count + bytesRead += copyCount; + + // Increment file pointer + file->bytenum += copyCount; } - // Increase total read count - bytesRead += copyCount; - - // Increment file pointer - file->bytenum += copyCount; - } - - return bytesRead; + return bytesRead; } //----------------------------------------------------------------------------- // fl_fseek: Seek to a specific place in the file //----------------------------------------------------------------------------- -int fl_fseek(void *f, long offset, int origin) { - FL_FILE *file = (FL_FILE*) f; - int res = -1; +int fl_fseek( void *f, long offset, int origin ) +{ + FL_FILE *file = (FL_FILE *)f; + int res = -1; - // If first call to library, initialise - CHECK_FL_INIT(); + // If first call to library, initialise + CHECK_FL_INIT(); - if (!file) - return -1; + if (!file) + return -1; - if (origin == SEEK_END && offset != 0) - return -1; + if (origin == SEEK_END && offset != 0) + return -1; - FL_LOCK(&_fs); + FL_LOCK(&_fs); - // Invalidate file buffer - file->file_data_address = 0xFFFFFFFF; - file->file_data_dirty = 0; + // Invalidate file buffer + file->file_data_address = 0xFFFFFFFF; + file->file_data_dirty = 0; - if (origin == SEEK_SET) { - file->bytenum = (uint32) offset; + if (origin == SEEK_SET) + { + file->bytenum = (uint32)offset; - if (file->bytenum > file->filelength) - file->bytenum = file->filelength; + if (file->bytenum > file->filelength) + file->bytenum = file->filelength; - res = 0; - } - else if (origin == SEEK_CUR) { - // Positive shift - if (offset >= 0) { - file->bytenum += offset; + res = 0; + } + else if (origin == SEEK_CUR) + { + // Positive shift + if (offset >= 0) + { + file->bytenum += offset; - if (file->bytenum > file->filelength) + if (file->bytenum > file->filelength) + file->bytenum = file->filelength; + } + // Negative shift + else + { + // Make shift positive + offset = -offset; + + // Limit to negative shift to start of file + if ((uint32)offset > file->bytenum) + file->bytenum = 0; + else + file->bytenum-= offset; + } + + res = 0; + } + else if (origin == SEEK_END) + { file->bytenum = file->filelength; + res = 0; } - // Negative shift - else { - // Make shift positive - offset = -offset; + else + res = -1; - // Limit to negative shift to start of file - if ((uint32) offset > file->bytenum) - file->bytenum = 0; - else - file->bytenum -= offset; - } + FL_UNLOCK(&_fs); - res = 0; - } - else if (origin == SEEK_END) { - file->bytenum = file->filelength; - res = 0; - } - else - res = -1; - - FL_UNLOCK(&_fs); - - return res; + return res; } //----------------------------------------------------------------------------- // fl_fgetpos: Get the current file position //----------------------------------------------------------------------------- -int fl_fgetpos(void *f, uint32 *position) { - FL_FILE *file = (FL_FILE*) f; +int fl_fgetpos(void *f , uint32 * position) +{ + FL_FILE *file = (FL_FILE *)f; - if (!file) - return -1; + if (!file) + return -1; - FL_LOCK(&_fs); + FL_LOCK(&_fs); - // Get position - *position = file->bytenum; + // Get position + *position = file->bytenum; - FL_UNLOCK(&_fs); + FL_UNLOCK(&_fs); - return 0; + return 0; } //----------------------------------------------------------------------------- // fl_ftell: Get the current file position //----------------------------------------------------------------------------- -long fl_ftell(void *f) { - uint32 pos = 0; +long fl_ftell(void *f) +{ + uint32 pos = 0; - fl_fgetpos(f, &pos); + fl_fgetpos(f, &pos); - return (long) pos; + return (long)pos; } //----------------------------------------------------------------------------- // fl_feof: Is the file pointer at the end of the stream? //----------------------------------------------------------------------------- -int fl_feof(void *f) { - FL_FILE *file = (FL_FILE*) f; - int res; +int fl_feof(void *f) +{ + FL_FILE *file = (FL_FILE *)f; + int res; - if (!file) - return -1; + if (!file) + return -1; - FL_LOCK(&_fs); + FL_LOCK(&_fs); - if (file->bytenum == file->filelength) - res = EOF; - else - res = 0; + if (file->bytenum == file->filelength) + res = EOF; + else + res = 0; - FL_UNLOCK(&_fs); + FL_UNLOCK(&_fs); - return res; + return res; } //----------------------------------------------------------------------------- // fl_fputc: Write a character to the stream //----------------------------------------------------------------------------- #if FATFS_INC_WRITE_SUPPORT -int fl_fputc(int c, void *f) { - uint8 data = (uint8) c; - int res; +int fl_fputc(int c, void *f) +{ + uint8 data = (uint8)c; + int res; - res = fl_fwrite(&data, 1, 1, f); - if (res == 1) - return c; - else - return res; + res = fl_fwrite(&data, 1, 1, f); + if (res == 1) + return c; + else + return res; } #endif //----------------------------------------------------------------------------- // fl_fwrite: Write a block of data to the stream //----------------------------------------------------------------------------- #if FATFS_INC_WRITE_SUPPORT -int fl_fwrite(const void *data, int size, int count, void *f) { - FL_FILE *file = (FL_FILE*) f; - uint32 sector; - uint32 offset; - uint32 length = (size * count); - uint8 *buffer = (uint8*) data; - uint32 bytesWritten = 0; - uint32 copyCount; +int fl_fwrite(const void * data, int size, int count, void *f ) +{ + FL_FILE *file = (FL_FILE *)f; + uint32 sector; + uint32 offset; + uint32 length = (size*count); + uint8 *buffer = (uint8 *)data; + uint32 bytesWritten = 0; + uint32 copyCount; - // If first call to library, initialise - CHECK_FL_INIT(); + // If first call to library, initialise + CHECK_FL_INIT(); - if (!file) - return -1; + if (!file) + return -1; - FL_LOCK(&_fs); + FL_LOCK(&_fs); - // No write permissions - if (!(file->flags & FILE_WRITE)) { - FL_UNLOCK(&_fs); - return -1; - } - - // Append writes to end of file - if (file->flags & FILE_APPEND) - file->bytenum = file->filelength; - // Else write to current position - - // Calculate start sector - sector = file->bytenum / FAT_SECTOR_SIZE; - - // Offset to start copying data from first sector - offset = file->bytenum % FAT_SECTOR_SIZE; - - while (bytesWritten < length) { - // Whole sector or more to be written? - if ((offset == 0) && ((length - bytesWritten) >= FAT_SECTOR_SIZE)) { - uint32 sectorsWrote; - - // Buffered sector, flush back to disk - if (file->file_data_address != 0xFFFFFFFF) { - // Flush un-written data to file - if (file->file_data_dirty) - fl_fflush(file); - - file->file_data_address = 0xFFFFFFFF; - file->file_data_dirty = 0; - } - - // Write as many sectors as possible - sectorsWrote = _write_sectors(file, sector, - (uint8*) (buffer + bytesWritten), - (length - bytesWritten) / FAT_SECTOR_SIZE); - copyCount = FAT_SECTOR_SIZE * sectorsWrote; - - // Increase total read count - bytesWritten += copyCount; - - // Increment file pointer - file->bytenum += copyCount; - - // Move onto next sector and reset copy offset - sector += sectorsWrote; - offset = 0; - - if (!sectorsWrote) - break; + // No write permissions + if (!(file->flags & FILE_WRITE)) + { + FL_UNLOCK(&_fs); + return -1; } - else { - // We have upto one sector to copy - copyCount = FAT_SECTOR_SIZE - offset; - // Only require some of this sector? - if (copyCount > (length - bytesWritten)) - copyCount = (length - bytesWritten); + // Append writes to end of file + if (file->flags & FILE_APPEND) + file->bytenum = file->filelength; + // Else write to current position - // Do we need to read a new sector? - if (file->file_data_address != sector) { - // Flush un-written data to file - if (file->file_data_dirty) - fl_fflush(file); + // Calculate start sector + sector = file->bytenum / FAT_SECTOR_SIZE; - // If we plan to overwrite the whole sector, we don't need to read it first! - if (copyCount != FAT_SECTOR_SIZE) { - // NOTE: This does not have succeed; if last sector of file - // reached, no valid data will be read in, but write will - // allocate some more space for new data. + // Offset to start copying data from first sector + offset = file->bytenum % FAT_SECTOR_SIZE; - // Get LBA of sector offset within file - if (!_read_sectors(file, sector, file->file_data_sector, 1)) - memset(file->file_data_sector, 0x00, FAT_SECTOR_SIZE); + while (bytesWritten < length) + { + // Whole sector or more to be written? + if ((offset == 0) && ((length - bytesWritten) >= FAT_SECTOR_SIZE)) + { + uint32 sectorsWrote; + + // Buffered sector, flush back to disk + if (file->file_data_address != 0xFFFFFFFF) + { + // Flush un-written data to file + if (file->file_data_dirty) + fl_fflush(file); + + file->file_data_address = 0xFFFFFFFF; + file->file_data_dirty = 0; + } + + // Write as many sectors as possible + sectorsWrote = _write_sectors(file, sector, (uint8*)(buffer + bytesWritten), (length - bytesWritten) / FAT_SECTOR_SIZE); + copyCount = FAT_SECTOR_SIZE * sectorsWrote; + + // Increase total read count + bytesWritten += copyCount; + + // Increment file pointer + file->bytenum += copyCount; + + // Move onto next sector and reset copy offset + sector+= sectorsWrote; + offset = 0; + + if (!sectorsWrote) + break; } + else + { + // We have upto one sector to copy + copyCount = FAT_SECTOR_SIZE - offset; - file->file_data_address = sector; - file->file_data_dirty = 0; - } + // Only require some of this sector? + if (copyCount > (length - bytesWritten)) + copyCount = (length - bytesWritten); - // Copy from application buffer into sector buffer - memcpy((uint8*) (file->file_data_sector + offset), - (uint8*) (buffer + bytesWritten), copyCount); + // Do we need to read a new sector? + if (file->file_data_address != sector) + { + // Flush un-written data to file + if (file->file_data_dirty) + fl_fflush(file); - // Mark buffer as dirty - file->file_data_dirty = 1; + // If we plan to overwrite the whole sector, we don't need to read it first! + if (copyCount != FAT_SECTOR_SIZE) + { + // NOTE: This does not have succeed; if last sector of file + // reached, no valid data will be read in, but write will + // allocate some more space for new data. - // Increase total read count - bytesWritten += copyCount; + // Get LBA of sector offset within file + if (!_read_sectors(file, sector, file->file_data_sector, 1)) + memset(file->file_data_sector, 0x00, FAT_SECTOR_SIZE); + } - // Increment file pointer - file->bytenum += copyCount; + file->file_data_address = sector; + file->file_data_dirty = 0; + } - // Move onto next sector and reset copy offset - sector++; - offset = 0; + // Copy from application buffer into sector buffer + memcpy((uint8*)(file->file_data_sector + offset), (uint8*)(buffer + bytesWritten), copyCount); + + // Mark buffer as dirty + file->file_data_dirty = 1; + + // Increase total read count + bytesWritten += copyCount; + + // Increment file pointer + file->bytenum += copyCount; + + // Move onto next sector and reset copy offset + sector++; + offset = 0; + } } - } - // Write increased extent of the file? - if (file->bytenum > file->filelength) { - // Increase file size to new point - file->filelength = file->bytenum; + // Write increased extent of the file? + if (file->bytenum > file->filelength) + { + // Increase file size to new point + file->filelength = file->bytenum; - // We are changing the file length and this - // will need to be writen back at some point - file->filelength_changed = 1; - } + // We are changing the file length and this + // will need to be writen back at some point + file->filelength_changed = 1; + } #if FATFS_INC_TIME_DATE_SUPPORT // If time & date support is enabled, always force directory entry to be @@ -1318,92 +1386,100 @@ file->filelength_changed = 1; #endif - FL_UNLOCK(&_fs); + FL_UNLOCK(&_fs); - return (size * count); + return (size*count); } #endif //----------------------------------------------------------------------------- // fl_fputs: Write a character string to the stream //----------------------------------------------------------------------------- #if FATFS_INC_WRITE_SUPPORT -int fl_fputs(const char *str, void *f) { - int len = (int) strlen(str); - int res = fl_fwrite(str, 1, len, f); +int fl_fputs(const char * str, void *f) +{ + int len = (int)strlen(str); + int res = fl_fwrite(str, 1, len, f); - if (res == len) - return len; - else - return res; + if (res == len) + return len; + else + return res; } #endif //----------------------------------------------------------------------------- // fl_remove: Remove a file from the filesystem //----------------------------------------------------------------------------- #if FATFS_INC_WRITE_SUPPORT -int fl_remove(const char *filename) { - FL_FILE *file; - int res = -1; +int fl_remove( const char * filename ) +{ + FL_FILE* file; + int res = -1; - FL_LOCK(&_fs); + FL_LOCK(&_fs); - // Use read_file as this will check if the file is already open! - file = fl_fopen((char*) filename, "r"); - if (file) { - // Delete allocated space - if (fatfs_free_cluster_chain(&_fs, file->startcluster)) { - // Remove directory entries - if (fatfs_mark_file_deleted(&_fs, file->parentcluster, - (char*) file->shortfilename)) { - // Close the file handle (this should not write anything to the file - // as we have not changed the file since opening it!) - fl_fclose(file); + // Use read_file as this will check if the file is already open! + file = fl_fopen((char*)filename, "r"); + if (file) + { + // Delete allocated space + if (fatfs_free_cluster_chain(&_fs, file->startcluster)) + { + // Remove directory entries + if (fatfs_mark_file_deleted(&_fs, file->parentcluster, (char*)file->shortfilename)) + { + // Close the file handle (this should not write anything to the file + // as we have not changed the file since opening it!) + fl_fclose(file); - res = 0; - } + res = 0; + } + } } - } - FL_UNLOCK(&_fs); + FL_UNLOCK(&_fs); - return res; + return res; } #endif //----------------------------------------------------------------------------- // fl_createdirectory: Create a directory based on a path //----------------------------------------------------------------------------- #if FATFS_INC_WRITE_SUPPORT -int fl_createdirectory(const char *path) { - int res; +int fl_createdirectory(const char *path) +{ + int res; - // If first call to library, initialise - CHECK_FL_INIT(); + // If first call to library, initialise + CHECK_FL_INIT(); - FL_LOCK(&_fs); - res = _create_directory((char*) path); - FL_UNLOCK(&_fs); + FL_LOCK(&_fs); + res =_create_directory((char*)path); + FL_UNLOCK(&_fs); - return res; + return res; } #endif //----------------------------------------------------------------------------- // fl_listdirectory: List a directory based on a path //----------------------------------------------------------------------------- #if FATFS_DIR_LIST_SUPPORT -void fl_listdirectory(const char *path) { - FL_DIR dirstat; +void fl_listdirectory(const char *path) +{ + FL_DIR dirstat; - // If first call to library, initialise - CHECK_FL_INIT(); + // If first call to library, initialise + CHECK_FL_INIT(); - FL_LOCK(&_fs); + FL_LOCK(&_fs); - FAT_PRINTF(("\r\nDirectory %s\r\n", path)); + FAT_PRINTF(("\r\nDirectory %s\r\n", path)); - if (fl_opendir(path, &dirstat)) { - struct fs_dir_ent dirent; + if (fl_opendir(path, &dirstat)) + { + struct fs_dir_ent dirent; - while (fl_readdir(&dirstat, &dirent) == 0) { + while (fl_readdir(&dirstat, &dirent) == 0) + { #if FATFS_INC_TIME_DATE_SUPPORT int d,m,y,h,mn,s; fatfs_convert_from_fat_time(dirent.write_time, &h,&m,&s); @@ -1411,101 +1487,109 @@ FAT_PRINTF(("%02d/%02d/%04d %02d:%02d ", d,mn,y,h,m)); #endif - if (dirent.is_dir) { - FAT_PRINTF(("%s \r\n", dirent.filename)); - } - else { - FAT_PRINTF(("%s [%d bytes]\r\n", dirent.filename, dirent.size)); - } + if (dirent.is_dir) + { + FAT_PRINTF(("%s \r\n", dirent.filename)); + } + else + { + FAT_PRINTF(("%s [%d bytes]\r\n", dirent.filename, dirent.size)); + } + } + + fl_closedir(&dirstat); } - fl_closedir(&dirstat); - } - - FL_UNLOCK(&_fs); + FL_UNLOCK(&_fs); } #endif //----------------------------------------------------------------------------- // fl_opendir: Opens a directory for listing //----------------------------------------------------------------------------- #if FATFS_DIR_LIST_SUPPORT -FL_DIR* fl_opendir(const char *path, FL_DIR *dir) { - int levels; - int res = 1; - uint32 cluster = FAT32_INVALID_CLUSTER; +FL_DIR* fl_opendir(const char* path, FL_DIR *dir) +{ + int levels; + int res = 1; + uint32 cluster = FAT32_INVALID_CLUSTER; - // If first call to library, initialise - CHECK_FL_INIT(); + // If first call to library, initialise + CHECK_FL_INIT(); - FL_LOCK(&_fs); + FL_LOCK(&_fs); - levels = fatfs_total_path_levels((char*) path) + 1; + levels = fatfs_total_path_levels((char*)path) + 1; - // If path is in the root dir - if (levels == 0) - cluster = fatfs_get_root_cluster(&_fs); - // Find parent directory start cluster - else - res = _open_directory((char*) path, &cluster); + // If path is in the root dir + if (levels == 0) + cluster = fatfs_get_root_cluster(&_fs); + // Find parent directory start cluster + else + res = _open_directory((char*)path, &cluster); - if (res) - fatfs_list_directory_start(&_fs, dir, cluster); + if (res) + fatfs_list_directory_start(&_fs, dir, cluster); - FL_UNLOCK(&_fs); + FL_UNLOCK(&_fs); - return cluster != FAT32_INVALID_CLUSTER ? dir : 0; + return cluster != FAT32_INVALID_CLUSTER ? dir : 0; } #endif //----------------------------------------------------------------------------- // fl_readdir: Get next item in directory //----------------------------------------------------------------------------- #if FATFS_DIR_LIST_SUPPORT -int fl_readdir(FL_DIR *dirls, fl_dirent *entry) { - int res = 0; +int fl_readdir(FL_DIR *dirls, fl_dirent *entry) +{ + int res = 0; - // If first call to library, initialise - CHECK_FL_INIT(); + // If first call to library, initialise + CHECK_FL_INIT(); - FL_LOCK(&_fs); + FL_LOCK(&_fs); - res = fatfs_list_directory_next(&_fs, dirls, entry); + res = fatfs_list_directory_next(&_fs, dirls, entry); - FL_UNLOCK(&_fs); + FL_UNLOCK(&_fs); - return res ? 0 : -1; + return res ? 0 : -1; } #endif //----------------------------------------------------------------------------- // fl_closedir: Close directory after listing //----------------------------------------------------------------------------- #if FATFS_DIR_LIST_SUPPORT -int fl_closedir(FL_DIR *dir) { - // Not used - return 0; +int fl_closedir(FL_DIR* dir) +{ + // Not used + return 0; } #endif //----------------------------------------------------------------------------- // fl_is_dir: Is this a directory? //----------------------------------------------------------------------------- #if FATFS_DIR_LIST_SUPPORT -int fl_is_dir(const char *path) { - int res = 0; - FL_DIR dir; +int fl_is_dir(const char *path) +{ + int res = 0; + FL_DIR dir; - if (fl_opendir(path, &dir)) { - res = 1; - fl_closedir(&dir); - } + if (fl_opendir(path, &dir)) + { + res = 1; + fl_closedir(&dir); + } - return res; + return res; } #endif //----------------------------------------------------------------------------- // fl_format: Format a partition with either FAT16 or FAT32 based on size //----------------------------------------------------------------------------- #if FATFS_INC_FORMAT_SUPPORT -int fl_format(uint32 volume_sectors, const char *name) { - return fatfs_format(&_fs, volume_sectors, name); +int fl_format(uint32 volume_sectors, const char *name) +{ + return fatfs_format(&_fs, volume_sectors, name); } #endif /*FATFS_INC_FORMAT_SUPPORT*/ //----------------------------------------------------------------------------- diff --git a/sys/init/main.c b/sys/init/main.c index 186114e..a627402 100644 --- a/sys/init/main.c +++ b/sys/init/main.c @@ -111,7 +111,7 @@ static char *argv_init[2] = { "init", - NULL, }; /* ARGV For Initial Process */ + 0x0, }; /* ARGV For Initial Process */ static char *envp_init[6] = { "HOME=/", @@ -119,7 +119,7 @@ "PATH=/bin:/sbin:/usr/bin:/usr/sbin", "USER=root", "GROUP=admin", - NULL, }; /* ENVP For Initial Process */ + 0x0, }; /* ENVP For Initial Process */ struct bootinfo _bootinfo; char _kernelname[512];