Skip to content

Commit

Permalink
fat: extract FatFs wrapper code into its own library
Browse files Browse the repository at this point in the history
  • Loading branch information
rasky committed Oct 5, 2024
1 parent 1371d09 commit 87495ef
Show file tree
Hide file tree
Showing 6 changed files with 476 additions and 358 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ LIBDRAGON_OBJS += \
$(BUILD_DIR)/n64sys.o $(BUILD_DIR)/interrupt.o $(BUILD_DIR)/backtrace.o \
$(BUILD_DIR)/fmath.o $(BUILD_DIR)/inthandler.o $(BUILD_DIR)/entrypoint.o \
$(BUILD_DIR)/debug.o $(BUILD_DIR)/debugcpp.o $(BUILD_DIR)/usb.o $(BUILD_DIR)/libcart/cart.o $(BUILD_DIR)/fatfs/ff.o \
$(BUILD_DIR)/fatfs/ffunicode.o $(BUILD_DIR)/rompak.o $(BUILD_DIR)/dragonfs.o \
$(BUILD_DIR)/fatfs/ffunicode.o $(BUILD_DIR)/fat.o $(BUILD_DIR)/rompak.o $(BUILD_DIR)/dragonfs.o \
$(BUILD_DIR)/audio.o $(BUILD_DIR)/display.o $(BUILD_DIR)/surface.o \
$(BUILD_DIR)/console.o $(BUILD_DIR)/asset.o $(BUILD_DIR)/pifile.o \
$(BUILD_DIR)/compress/lzh5.o $(BUILD_DIR)/compress/lz4_dec.o $(BUILD_DIR)/compress/lz4_dec_fast.o $(BUILD_DIR)/compress/ringbuf.o \
Expand Down Expand Up @@ -135,6 +135,7 @@ install: install-mk libdragon
install -Cv -m 0644 include/display.h $(INSTALLDIR)/mips64-elf/include/display.h
install -Cv -m 0644 include/debug.h $(INSTALLDIR)/mips64-elf/include/debug.h
install -Cv -m 0644 include/debugcpp.h $(INSTALLDIR)/mips64-elf/include/debugcpp.h
install -Cv -m 0644 include/fat.h $(INSTALLDIR)/mips64-elf/include/fat.h
install -Cv -m 0644 include/usb.h $(INSTALLDIR)/mips64-elf/include/usb.h
install -Cv -m 0644 include/console.h $(INSTALLDIR)/mips64-elf/include/console.h
install -Cv -m 0644 include/joybus.h $(INSTALLDIR)/mips64-elf/include/joybus.h
Expand Down
90 changes: 90 additions & 0 deletions include/fat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* @file fat.h
* @brief FAT filesystem interface
* @ingroup lowlevel
*
* This file allows multiple clients to access and use the FatFs library
* within libdragon for different scopes.
*
* FatFs is a generic FAT filesystem module for small embedded systems,
* written by ChaN. It is available at http://elm-chan.org/fsw/ff/00index_e.html.
*
* FatFs is currently used by libdragon for a single use case: to implement
* access to the SD card in flashcarts. This access is currently implemented
* by the debug library (debug.h), initialized via #debug_init_sdfs.
*
* The APIs exported by this file are useful only if you need to mount a FAT
* volume coming from some other sources (eg: a FAT image within a ROM, or
* a FAT volume accessible via some custom USB protocol, or whatever else).
* If you need this, call #fat_mount to configure a FatFs volume (eg:
* #FAT_VOLUME_CUSTOM), which you will then be able to access via standard
* C file operations.
*/

#ifndef LIBDRAGON_FAT_H
#define LIBDRAGON_FAT_H

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Definition of the various FAT volumes.
*
* Currently, we only allocate a single volume, which is used for the SD card
* in flashcarts.
*
* If you need to mount a FAT volume coming from some other sources, you
* can use the #FAT_VOLUME_CUSTOM volume.
*/
enum {
FAT_VOLUME_SD = 0, ///< Volume for SD cards
FAT_VOLUME_CUSTOM = 1, ///< Custom volume, free for usage
};

/**
* @brief Interface for disk operations required to implement a volume.
*
* These interfaces are identical to diskio.h from FatFs. It basically just
* adds one indirection layer to it, to dispatch the calls to the correct
* volume.
*/
typedef struct {
///@cond
int (*disk_initialize)(void);
int (*disk_status)(void);
int (*disk_read)(uint8_t* buff, int sector, int count);
int (*disk_read_sdram)(uint8_t* buff, int sector, int count);
int (*disk_write)(const uint8_t* buff, int sector, int count);
int (*disk_ioctl)(uint8_t cmd, void* buff);
///@endcond
} fat_disk_t;

/**
* @brief Mount a new FAT volume through the FatFs library.
*
* This function allows to mount a new FAT volume through the FatFs library.
* Access to the actual disk is done through the provided disk operations,
* so that the volume can be backed by any kind of storage.
*
* After calling this function, you will be able to access the volume via
* standard C file operations.
*
* @param prefix Prefix to use for the volume in stdio calls like
* fopen (eg: "sd:"). If this is NULL, the volume
* will not be accessible via stdio calls, but only
* via the FatFs API.
* @param fatfs_volume_id ID of the volume within FatFs (eg: #FAT_VOLUME_CUSTOM)
* @param disk Table of disk operations to use for this volume
*
* @return 0 on success, -1 on failure (errno will be set)
*/
int fat_mount(const char *prefix, int fatfs_volume_id, const fat_disk_t* disk);

#ifdef __cplusplus
}
#endif

#endif
1 change: 1 addition & 0 deletions include/libdragon.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "audio.h"
#include "console.h"
#include "debug.h"
#include "fat.h"
#include "joybus.h"
#include "joybus_accessory.h"
#include "pixelfx.h"
Expand Down
Loading

0 comments on commit 87495ef

Please sign in to comment.