Add file system
This commit is contained in:
parent
ef2f3c0ada
commit
3253619eaf
@ -1154,7 +1154,12 @@ errors fat FAT_ERR_ {
|
||||
failure BLOCK_BOUNDS "The block number is out of bounds",
|
||||
failure CREATE_ROOT "Tried to create root directory",
|
||||
failure BAD_FILENAME "Filename is not allowed",
|
||||
failure TYPE_NOT_SUPPORTED "Only FAT32 is supported",
|
||||
// NOTE rueegges: library internal
|
||||
failure PATH_EMPTY "The path that was given is empty",
|
||||
failure FILE_TOO_LONG "The cluster chain for this file would become longer than allowed by the FAT32 spec",
|
||||
failure TOO_MANY_HANDLES "Currently the FAT32 library allows at most FAT32_MAX_OPEN_HANDLERS handlers per dir/file",
|
||||
failure OPENED "The directory entry is currently opened and can not be deleted",
|
||||
};
|
||||
|
||||
// errors generated by VFS's fs cache library
|
||||
|
||||
@ -66,7 +66,7 @@ errval_t closedir(fs_dirhandle_t handle);
|
||||
*
|
||||
* @returns SYS_ERR_OK on successful reading the next entry
|
||||
* FS_ERR_INVALID_FH if the directory handle was invalid
|
||||
* FS_ERR_NOTFOUND there is no next file in the directory
|
||||
* FS_ERR_INDEX_BOUNDS there is no next file in the directory
|
||||
*/
|
||||
errval_t readdir(fs_dirhandle_t handle, char **name);
|
||||
|
||||
|
||||
312
include/fs/fat32.h
Normal file
312
include/fs/fat32.h
Normal file
@ -0,0 +1,312 @@
|
||||
#ifndef _INIT_FAT32_H_
|
||||
#define _INIT_FAT32_H_
|
||||
|
||||
#include <fs/fs.h>
|
||||
#include <drivers/sdhc.h>
|
||||
|
||||
#define FAT32_SHORTNAME_SIZE 8 // bytes
|
||||
#define FAT32_SHORTNAME_EXT_SIZE 3 // bytes
|
||||
#define FAT32_BLOCK_SIZE SDHC_BLOCK_SIZE
|
||||
|
||||
#define FAT32_EOFC_MARK 0x0FFFFFF8
|
||||
|
||||
// "your FAT file system driver must not allow a cluster chain to be created that is longer than 0x100000000 bytes,
|
||||
// and the last byte of the last cluster in a chain that long cannot be allocated to the file" - FAT32 spec
|
||||
#define FAT32_MAX_FILESIZE (0x100000000 - 1)
|
||||
|
||||
/**
|
||||
* ATTR_READ_ONLY Indicates that writes to the file should fail.
|
||||
* ATTR_HIDDEN Indicates that normal directory listings should not show this file.
|
||||
* ATTR_SYSTEM Indicates that this is an operating system file.
|
||||
* ATTR_VOLUME_ID There should only be one “file” on the volume that has this attribute set, and that file must be in the root directory. This name of this file is actually the label for the volume. DIR_FstClusHI and DIR_FstClusLO must always be 0 for the volume label (no data clusters are allocated to the volume label file).
|
||||
* ATTR_DIRECTORY Indicates that this file is actually a container for other files.
|
||||
* // TODO rueegges: don't forget to set this
|
||||
* ATTR_ARCHIVE This attribute supports backup utilities. This bit is set by the FAT file system driver when a file is created, renamed, or written to. Backup utilities may use this attribute to indicate which files on the volume have been modified since the last time that a backup was performed.
|
||||
*/
|
||||
#define FAT32_ATTR_READ_ONLY 0x01
|
||||
#define FAT32_ATTR_HIDDEN 0x02
|
||||
#define FAT32_ATTR_SYSTEM 0x04
|
||||
#define FAT32_ATTR_VOLUME_ID 0x08
|
||||
#define FAT32_ATTR_DIRECTORY 0x10
|
||||
#define FAT32_ATTR_ARCHIVE 0x20
|
||||
#define FAT32_ATTR_LONG_NAME (FAT32_ATTR_READ_ONLY|FAT32_ATTR_HIDDEN|FAT32_ATTR_SYSTEM|FAT32_ATTR_VOLUME_ID)
|
||||
|
||||
typedef errval_t (*fat_block_read_fn_t)(uint32_t sector_number, size_t offset, size_t size, void *dst);
|
||||
typedef errval_t (*fat_block_write_fn_t)(uint32_t sector_number, size_t offset, size_t size, const void *src);
|
||||
typedef errval_t (*fat_block_lock_fn_t)(void);
|
||||
typedef errval_t (*fat_block_unlock_fn_t)(void);
|
||||
typedef errval_t (*fat_block_register_handle_fn_t)(uint64_t directory_entry_id);
|
||||
typedef errval_t (*fat_block_unregister_handle_fn_t)(uint64_t directory_entry_id);
|
||||
typedef errval_t (*fat_block_count_handles_fn_t)(uint64_t directory_entry_id, size_t *count);
|
||||
|
||||
// typedef errval_t (*fat_block_lock_read_fn_t)(int block_index);
|
||||
// typedef errval_t (*fat_block_lock_write_fn_t)(int block_index);
|
||||
// typedef errval_t (*fat_block_unlock_read_fn_t)(int block_index);
|
||||
// typedef errval_t (*fat_block_unlock_write_fn_t)(int block_index);
|
||||
|
||||
#pragma pack(1)
|
||||
/**
|
||||
* @brief FAT32 specific part of the boot sector
|
||||
*/
|
||||
struct bpb_fat32 {
|
||||
/**
|
||||
* @brief count of sectors occupied by a single FAT
|
||||
*/
|
||||
uint32_t fat_sectors_32_count;
|
||||
/**
|
||||
* @brief only valid if mirror flag is set. zero based index of active FAT
|
||||
*/
|
||||
uint16_t ext_flag_active_fat:4;
|
||||
uint16_t _ext_flag_reserved_1:3;
|
||||
/**
|
||||
* @brief 0 -> FAT is mirrored to all FATs, 1-> single FAT is active (specified in bits 0-3)
|
||||
*/
|
||||
uint16_t ext_flag_mirrored:1;
|
||||
uint16_t _ext_flag_reserved_2:8;
|
||||
/**
|
||||
* @brief Currently only version 0 specified
|
||||
*/
|
||||
uint16_t fs_version;
|
||||
/**
|
||||
* @brief cluster number of the first cluster of the root directory
|
||||
*
|
||||
* usually 2
|
||||
*/
|
||||
uint32_t root_cluster_number;
|
||||
/**
|
||||
* @brief sector number of the fs infor structure in the reserved area of the FAT32 volume
|
||||
*
|
||||
* usually 1
|
||||
*/
|
||||
uint16_t fs_info_sector_number;
|
||||
/**
|
||||
* @brief if non-zero indicates the sector in the reserved area of the volume containing a copy of the bpb
|
||||
*
|
||||
* usually 6
|
||||
*/
|
||||
uint16_t backup_boot_sector_number;
|
||||
uint8_t _reserved[12];
|
||||
/**
|
||||
* @brief int 0x13 drive number
|
||||
*
|
||||
* don't think we need this
|
||||
*/
|
||||
uint8_t drive_number;
|
||||
uint8_t _reserved1;
|
||||
/**
|
||||
* @brief indicates that the next 3 fields are present
|
||||
* 0x29 means that the next 3 fields are present? no other values specified...
|
||||
*/
|
||||
uint8_t extended_boot_signature;
|
||||
/**
|
||||
* @brief together with volume_label allows volume tracking on removable media
|
||||
*
|
||||
*/
|
||||
uint32_t volume_serial_number;
|
||||
/**
|
||||
* @brief same as the 11-byte volume label recorded in the root directory
|
||||
* "NO NAME " means that there is no label defined
|
||||
*/
|
||||
char volume_label[11];
|
||||
/**
|
||||
* @brief Always set to "FAT32 " for FAT32 file systems
|
||||
*
|
||||
* MUST NOT be used for file system type determination.
|
||||
* just seems to be here for fun?
|
||||
*/
|
||||
char file_system_type[8];
|
||||
};
|
||||
|
||||
#pragma pack(1)
|
||||
union bpb_fat {
|
||||
struct bpb_fat32 bpb_fat32;
|
||||
};
|
||||
|
||||
#pragma pack(1)
|
||||
struct bpb {
|
||||
uint8_t jmp_boot[3];
|
||||
unsigned char oem_name[8];
|
||||
uint16_t bytes_per_sector;
|
||||
uint8_t sectors_per_cluster;
|
||||
uint16_t reserved_sector_count;
|
||||
// number of consecutive FAT copies (there is only one "main" FAT but n-1 copies)
|
||||
// should be 2 in most cases but can be any value >= 1
|
||||
uint8_t fat_count;
|
||||
// MUST be 0 for FAT32
|
||||
uint16_t root_entry_count;
|
||||
// MUST be 0 for FAT32
|
||||
uint16_t total_sector_16_count;
|
||||
/*
|
||||
* valid values: 0xF0 0xF8 0xF9 0xFA 0xFB 0xFC 0xFD 0xFE 0xFF
|
||||
* not really used anymore
|
||||
*/
|
||||
uint8_t media;
|
||||
// MUST be 0 for FAT32
|
||||
uint16_t fat_sectors_16_count;
|
||||
// unsure. something to do with interrput 0x13
|
||||
// don't think we need this
|
||||
uint16_t sectors_per_track;
|
||||
// unsure. something to do with interrput 0x13
|
||||
// don't think we need this
|
||||
uint16_t number_of_heads;
|
||||
/**
|
||||
* @brief number of sectors preceding the partition containing this FAT volume
|
||||
*
|
||||
* should be 0 for non-partitioned devices, operating system specific, mainly relevant for 0x13 interrupt
|
||||
*/
|
||||
uint32_t hidden_sector_count;
|
||||
/**
|
||||
* @brief total count of sectors on the volume
|
||||
*/
|
||||
uint32_t total_sector_32_count;
|
||||
|
||||
/**
|
||||
* @brief FAT type specific information
|
||||
*/
|
||||
union bpb_fat bpb_fat;
|
||||
};
|
||||
STATIC_ASSERT(sizeof(struct bpb) <= 512-2, "BPB struct may be at most 512-2 bytes long");
|
||||
|
||||
#pragma pack(1)
|
||||
struct fat32_fsinfo {
|
||||
uint32_t lead_signature; // Value 0x41615252
|
||||
uint8_t _reserved1[480];
|
||||
uint32_t structure_signature; // Value 0x61417272.
|
||||
uint32_t free_count; // 0xFFFFFFFF means free count is unknown
|
||||
uint32_t next_free; // 0xFFFFFFFF means not set, just a hint at which we should start looking for free clusters
|
||||
uint8_t _reserved2[12];
|
||||
uint32_t trail_signature; // Value 0xAA550000
|
||||
};
|
||||
|
||||
#pragma pack(1)
|
||||
/**
|
||||
* @brief Single entry of a directory
|
||||
* a directory is like a file but containing a list of structs of this type
|
||||
*
|
||||
*/
|
||||
struct fat32_directory_entry {
|
||||
/**
|
||||
* DIR_Name[0] == 0xE5, then the directory entry is free
|
||||
* If DIR_Name[0] == 0x00, then the directory entry is free (same as for 0xE5), and there are no allocated directory entries after this on
|
||||
* If DIR_Name[0] == 0x05, then the actual file name character for this byte is 0xE5. 0xE5 is actually a valid KANJI lead byte value for the character set used in Japan. The special 0x05 value is used so that this special file name case for Japan can be handled properly and not cause FAT file system code to think that the entry is free.
|
||||
*
|
||||
* The DIR_Name field is actually broken into two parts+ the 8-character main part of the name, and the 3-character extension. These two parts are “trailing space padded” with bytes of 0x20.
|
||||
* DIR_Name[0] may not equal 0x20. There is an implied ‘.’ character between the main part of the name and the extension part of the name that is not present in DIR_Name
|
||||
* Lower case characters are not allowed in DIR_Name (what these characters are is country specific). The following characters are not legal in any bytes of DIR_Name:
|
||||
* - Values less than 0x20 except for the special case of 0x05 in DIR_Name[0] described above.
|
||||
* - 0x22, 0x2A, 0x2B, 0x2C, 0x2E, 0x2F, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x5B, 0x5C, 0x5D, and 0x7C
|
||||
*/
|
||||
char short_name[FAT32_SHORTNAME_SIZE];
|
||||
char short_name_ext[FAT32_SHORTNAME_EXT_SIZE];
|
||||
uint8_t attributes; // upper two bits are reserved and should be 0
|
||||
uint8_t nt_reserved;
|
||||
uint8_t creation_time_deci_seconds;
|
||||
uint8_t _unknown[6];
|
||||
|
||||
uint16_t first_cluster_number_high_word;
|
||||
uint16_t write_time; // also creation is write
|
||||
uint16_t write_date; // also creation is write
|
||||
uint16_t first_cluster_number_low_word;
|
||||
// zero for directories
|
||||
uint32_t file_size; // in bytes
|
||||
};
|
||||
STATIC_ASSERT(sizeof(struct fat32_directory_entry) == 32, "FAT32 Directory entries are 32 bytes in size");
|
||||
|
||||
struct fat32_directory_entry_ref {
|
||||
uint32_t sector_number;
|
||||
size_t index_in_sector;
|
||||
};
|
||||
|
||||
struct fat32_path_resolve_result {
|
||||
struct fat32_directory_entry directory_entry;
|
||||
struct fat32_directory_entry_ref directory_entry_ref;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Struct for an open fat32 file system
|
||||
*
|
||||
* Note: We don't format file systems so the boot sector never changes
|
||||
* look at the bpb and bpb_fat32 structs for more info on the fields
|
||||
*/
|
||||
struct fat32 {
|
||||
|
||||
// block functions
|
||||
fat_block_read_fn_t read_object_fn;
|
||||
fat_block_write_fn_t write_object_fn;
|
||||
fat_block_lock_fn_t lock_fn;
|
||||
fat_block_lock_fn_t unlock_fn;
|
||||
fat_block_register_handle_fn_t register_handle_fn;
|
||||
fat_block_unregister_handle_fn_t unregister_handle_fn;
|
||||
fat_block_count_handles_fn_t count_handles_fn;
|
||||
|
||||
// very primitive implementation of mounting
|
||||
char *mount;
|
||||
|
||||
// metadata
|
||||
|
||||
uint16_t bytes_per_sector;
|
||||
uint8_t sectors_per_cluster;
|
||||
uint16_t reserved_sector_count;
|
||||
uint8_t fat_count;
|
||||
uint32_t total_sector_32_count;
|
||||
uint32_t fat_sectors_32_count;
|
||||
uint8_t ext_flag_active_fat:4;
|
||||
uint8_t ext_flag_mirrored:1;
|
||||
uint32_t root_cluster_number;
|
||||
|
||||
// information calculated during init
|
||||
|
||||
// the first sector containing actual directory or file data
|
||||
uint32_t first_data_sector;
|
||||
// the largest valid cluster number
|
||||
uint32_t max_cluster_number;
|
||||
// the number of bytes in a cluster
|
||||
size_t bytes_per_cluster;
|
||||
};
|
||||
|
||||
struct fat32_handle {
|
||||
bool is_dir;
|
||||
|
||||
uint32_t first_cluster;
|
||||
uint32_t current_cluster;
|
||||
size_t byte_offset;
|
||||
|
||||
struct fat32_directory_entry_ref directory_entry_ref;
|
||||
};
|
||||
|
||||
#define FAT32_FAT_ENTRIES_PER_SECTOR (SDHC_BLOCK_SIZE / sizeof(uint32_t))
|
||||
STATIC_ASSERT(FAT32_FAT_ENTRIES_PER_SECTOR * sizeof(uint32_t) == SDHC_BLOCK_SIZE, "fat does not align with blocks");
|
||||
#define FAT32_DIRECTORY_ENTRIES_PER_SECTOR (SDHC_BLOCK_SIZE / sizeof(struct fat32_directory_entry))
|
||||
STATIC_ASSERT(FAT32_DIRECTORY_ENTRIES_PER_SECTOR * sizeof(struct fat32_directory_entry) == SDHC_BLOCK_SIZE, "directories do not align with blocks");
|
||||
|
||||
errval_t fat32_init(
|
||||
struct fat32 **return_fat32,
|
||||
fat_block_read_fn_t read_block_fn,
|
||||
fat_block_write_fn_t write_block_fn,
|
||||
fat_block_lock_fn_t lock_fn,
|
||||
fat_block_unlock_fn_t unlock_fn,
|
||||
fat_block_register_handle_fn_t register_handle_fn,
|
||||
fat_block_unregister_handle_fn_t unregister_handle_fn,
|
||||
fat_block_count_handles_fn_t count_handles_fn,
|
||||
char *mount
|
||||
);
|
||||
|
||||
// dirs
|
||||
errval_t fat32_mkdir(struct fat32 *fat32, const char *path);
|
||||
errval_t fat32_rmdir(struct fat32 *fat32, const char *path);
|
||||
errval_t fat32_opendir(struct fat32 *fat32, const char *path, struct fat32_handle **dir_handle);
|
||||
errval_t fat32_closedir(struct fat32 *fat32, struct fat32_handle *dir_handle);
|
||||
errval_t fat32_stat(struct fat32 *fat32, struct fat32_handle *dir_handle, struct fs_fileinfo *fileinfo);
|
||||
errval_t fat32_readdir(struct fat32 *fat32, struct fat32_handle *dir_handle, char **name);
|
||||
errval_t fat32_rm(struct fat32 *fat32, const char *path);
|
||||
|
||||
// files
|
||||
errval_t fat32_fopen(struct fat32 *fat32, const char *path, struct fat32_handle **file_handle);
|
||||
errval_t fat32_fcreate(struct fat32 *fat32, const char *path, struct fat32_handle **file_handle);
|
||||
errval_t fat32_fread(struct fat32 *fat32, struct fat32_handle *file_handle, void *buffer, size_t bytes, size_t *bytes_read);
|
||||
errval_t fat32_fwrite(struct fat32 *fat32, struct fat32_handle *file_handle, const void *buffer, size_t bytes, size_t *bytes_written);
|
||||
errval_t fat32_fclose(struct fat32 *fat32, struct fat32_handle *file_handle);
|
||||
errval_t fat32_seek(struct fat32 *fat32, struct fat32_handle *file_handle, enum fs_seekpos whence, off_t offset);
|
||||
errval_t fat32_tell(struct fat32 *fat32, struct fat32_handle *file_handle, size_t *pos);
|
||||
|
||||
#endif /* _INIT_FAT32_H_ */
|
||||
@ -17,7 +17,9 @@
|
||||
"fs.c",
|
||||
"fopen.c",
|
||||
"ramfs.c",
|
||||
"dirent.c"
|
||||
]
|
||||
"dirent.c",
|
||||
"fat32.c"
|
||||
],
|
||||
addLibraries = [ "block_driver" ]
|
||||
}
|
||||
]
|
||||
|
||||
@ -105,7 +105,7 @@ errval_t closedir(fs_dirhandle_t handle)
|
||||
*
|
||||
* @returns SYS_ERR_OK on successful reading the next entry
|
||||
* FS_ERR_INVALID_FH if the directory handle was invalid
|
||||
* FS_ERR_NOTFOUND there is no next file in the directory
|
||||
* FS_ERR_INDEX_BOUNDS there is no next file in the directory
|
||||
*/
|
||||
errval_t readdir(fs_dirhandle_t handle, char **name)
|
||||
{
|
||||
@ -148,7 +148,7 @@ errval_t rmdir(const char *path)
|
||||
* @param path the path to be removed
|
||||
*
|
||||
* @returns SYS_ERR_OK on successful removal
|
||||
* FS_ERR_NOTDIR if the path is not a directory
|
||||
* FS_ERR_NOTFILE if the path is not a file
|
||||
* FS_ERR_NOTFOUND if there is no such path
|
||||
*/
|
||||
errval_t rm(const char *path)
|
||||
@ -163,7 +163,7 @@ errval_t rm(const char *path)
|
||||
* @param buf returned data of the filehandle information
|
||||
*
|
||||
* @returns SYS_ERR_OK on successful removal
|
||||
* FS_ERR_INVALID_FH if the directory is not emtpy
|
||||
* FS_ERR_INVALID_FH if the directory handle was invalid
|
||||
*/
|
||||
errval_t fstat(fs_dirhandle_t handle, struct fs_fileinfo *buf)
|
||||
{
|
||||
|
||||
1689
lib/fs/fat32.c
Normal file
1689
lib/fs/fat32.c
Normal file
File diff suppressed because it is too large
Load Diff
@ -19,8 +19,8 @@
|
||||
|
||||
#include <fs/fs.h>
|
||||
#include <fs/dirent.h>
|
||||
#include <fs/ramfs.h>
|
||||
#include "fs_internal.h"
|
||||
#include <fs/fat32.h>
|
||||
|
||||
|
||||
static void *mount;
|
||||
@ -92,29 +92,29 @@ static void fdtab_free(int fd)
|
||||
//XXX: flags are ignored...
|
||||
static int fs_libc_open(char *path, int flags)
|
||||
{
|
||||
ramfs_handle_t vh;
|
||||
struct fat32_handle *vh;
|
||||
errval_t err;
|
||||
|
||||
// If O_CREAT was given, we use ramfsfs_create()
|
||||
// If O_CREAT was given, we use create
|
||||
if(flags & O_CREAT) {
|
||||
// If O_EXCL was also given, we check whether we can open() first
|
||||
if(flags & O_EXCL) {
|
||||
err = ramfs_open(mount, path, &vh);
|
||||
err = fat32_fopen(mount, path, &vh);
|
||||
if(err_is_ok(err)) {
|
||||
ramfs_close(mount, vh);
|
||||
fat32_fclose(mount, vh);
|
||||
errno = EEXIST;
|
||||
return -1;
|
||||
}
|
||||
assert(err_no(err) == FS_ERR_NOTFOUND);
|
||||
}
|
||||
|
||||
err = ramfs_create(mount, path, &vh);
|
||||
err = fat32_fcreate(mount, path, &vh);
|
||||
if (err_is_fail(err) && err == FS_ERR_EXISTS) {
|
||||
err = ramfs_open(mount, path, &vh);
|
||||
err = fat32_fopen(mount, path, &vh);
|
||||
}
|
||||
} else {
|
||||
// Regular open()
|
||||
err = ramfs_open(mount, path, &vh);
|
||||
err = fat32_fopen(mount, path, &vh);
|
||||
}
|
||||
|
||||
if (err_is_fail(err)) {
|
||||
@ -137,7 +137,7 @@ static int fs_libc_open(char *path, int flags)
|
||||
};
|
||||
int fd = fdtab_alloc(&e);
|
||||
if (fd < 0) {
|
||||
ramfs_close(mount, vh);
|
||||
fat32_fclose(mount, vh);
|
||||
return -1;
|
||||
} else {
|
||||
return fd;
|
||||
@ -153,9 +153,9 @@ static int fs_libc_read(int fd, void *buf, size_t len)
|
||||
switch(e->type) {
|
||||
case FDTAB_TYPE_FILE:
|
||||
{
|
||||
ramfs_handle_t fh = e->handle;
|
||||
struct fat32_handle *fh = e->handle;
|
||||
assert(e->handle);
|
||||
err = ramfs_read(mount, fh, buf, len, &retlen);
|
||||
err = fat32_fread(mount, fh, buf, len, &retlen);
|
||||
if (err_is_fail(err)) {
|
||||
return -1;
|
||||
}
|
||||
@ -180,8 +180,8 @@ static int fs_libc_write(int fd, void *buf, size_t len)
|
||||
switch(e->type) {
|
||||
case FDTAB_TYPE_FILE:
|
||||
{
|
||||
ramfs_handle_t fh = e->handle;
|
||||
errval_t err = ramfs_write(mount, fh, buf, len, &retlen);
|
||||
struct fat32_handle *fh = e->handle;
|
||||
errval_t err = fat32_fwrite(mount, fh, buf, len, &retlen);
|
||||
if (err_is_fail(err)) {
|
||||
return -1;
|
||||
}
|
||||
@ -202,10 +202,10 @@ static int fs_libc_close(int fd)
|
||||
return -1;
|
||||
}
|
||||
|
||||
ramfs_handle_t fh = e->handle;
|
||||
struct fat32_handle *fh = e->handle;
|
||||
switch(e->type) {
|
||||
case FDTAB_TYPE_FILE:
|
||||
err = ramfs_close(mount, fh);
|
||||
err = fat32_fclose(mount, fh);
|
||||
if (err_is_fail(err)) {
|
||||
return -1;
|
||||
}
|
||||
@ -221,7 +221,7 @@ static int fs_libc_close(int fd)
|
||||
static off_t fs_libc_lseek(int fd, off_t offset, int whence)
|
||||
{
|
||||
struct fdtab_entry *e = fdtab_get(fd);
|
||||
ramfs_handle_t fh = e->handle;
|
||||
struct fat32_handle *fh = e->handle;
|
||||
switch(e->type) {
|
||||
case FDTAB_TYPE_FILE:
|
||||
{
|
||||
@ -246,13 +246,13 @@ static off_t fs_libc_lseek(int fd, off_t offset, int whence)
|
||||
return -1;
|
||||
}
|
||||
|
||||
err = ramfs_seek(mount, fh, fs_whence, offset);
|
||||
err = fat32_seek(mount, fh, fs_whence, offset);
|
||||
if(err_is_fail(err)) {
|
||||
DEBUG_ERR(err, "vfs_seek");
|
||||
return -1;
|
||||
}
|
||||
|
||||
err = ramfs_tell(mount, fh, &retpos);
|
||||
err = fat32_tell(mount, fh, &retpos);
|
||||
if(err_is_fail(err)) {
|
||||
return -1;
|
||||
}
|
||||
@ -265,13 +265,13 @@ static off_t fs_libc_lseek(int fd, off_t offset, int whence)
|
||||
}
|
||||
}
|
||||
|
||||
static errval_t fs_mkdir(const char *path){ return ramfs_mkdir(mount, path);}
|
||||
static errval_t fs_rmdir(const char *path){ return ramfs_rmdir(mount, path); }
|
||||
static errval_t fs_rm(const char *path){ return ramfs_remove(mount, path); }
|
||||
static errval_t fs_opendir(const char *path, fs_dirhandle_t *h){ return ramfs_opendir(mount, path, h); }
|
||||
static errval_t fs_readdir(fs_dirhandle_t h, char **name) { return ramfs_dir_read_next(mount, h, name, NULL); }
|
||||
static errval_t fs_closedir(fs_dirhandle_t h) { return ramfs_closedir(mount, h); }
|
||||
static errval_t fs_fstat(fs_dirhandle_t h, struct fs_fileinfo *b) { return ramfs_stat(mount, h, b); }
|
||||
static errval_t fs_mkdir(const char *path){ return fat32_mkdir(mount, path);}
|
||||
static errval_t fs_rmdir(const char *path){ return fat32_rmdir(mount, path); }
|
||||
static errval_t fs_rm(const char *path){ return fat32_rm(mount, path); }
|
||||
static errval_t fs_opendir(const char *path, fs_dirhandle_t *h){ return fat32_opendir(mount, path, (struct fat32_handle **) h); }
|
||||
static errval_t fs_readdir(fs_dirhandle_t h, char **name) { return fat32_readdir(mount, h, name); }
|
||||
static errval_t fs_closedir(fs_dirhandle_t h) { return fat32_closedir(mount, h); }
|
||||
static errval_t fs_fstat(fs_dirhandle_t h, struct fs_fileinfo *b) { return fat32_stat(mount, h, b); }
|
||||
|
||||
typedef int fsopen_fn_t(char *, int);
|
||||
typedef int fsread_fn_t(int, void *buf, size_t);
|
||||
|
||||
33
lib/fs/fs.c
33
lib/fs/fs.c
@ -4,13 +4,12 @@
|
||||
*/
|
||||
|
||||
#include <aos/aos.h>
|
||||
|
||||
#include <drivers/block_driver.h>
|
||||
|
||||
#include <fs/fs.h>
|
||||
#include <fs/dirent.h>
|
||||
#include <fs/ramfs.h>
|
||||
|
||||
#include "fs_internal.h"
|
||||
#include <fs/fat32.h>
|
||||
|
||||
/*
|
||||
* Copyright (c) 2016 ETH Zurich.
|
||||
@ -34,17 +33,33 @@ errval_t filesystem_init(void)
|
||||
errval_t err;
|
||||
|
||||
/* TODO: Filesystem project: hook up your init code here */
|
||||
err = block_driver_init();
|
||||
if (err_is_fail(err)) return err;
|
||||
struct fat32 *fat32;
|
||||
err = fat32_init(
|
||||
&fat32,
|
||||
block_driver_read_object,
|
||||
block_driver_write_object,
|
||||
block_driver_lock,
|
||||
block_driver_unlock,
|
||||
block_driver_register_handle,
|
||||
block_driver_unregister_handle,
|
||||
block_driver_count_handles,
|
||||
"/sdcard"
|
||||
);
|
||||
if(err_is_fail(err)) return err;
|
||||
|
||||
ramfs_mount_t st = NULL;
|
||||
err = ramfs_mount("/", &st);
|
||||
if (err_is_fail(err)) {
|
||||
return err;
|
||||
}
|
||||
// ramfs_mount_t st = NULL;
|
||||
// err = ramfs_mount("/", &st);
|
||||
// if (err_is_fail(err)) {
|
||||
// return err;
|
||||
// }
|
||||
|
||||
/* TODO: Mount your sdcard at /sdcard */
|
||||
// done during fat32_init
|
||||
|
||||
/* register libc fopen/fread and friends */
|
||||
fs_libc_init(st);
|
||||
fs_libc_init(fat32);
|
||||
|
||||
return SYS_ERR_OK;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user