From 6a1b0a474ca091f37c862b47abdbe190620363f8 Mon Sep 17 00:00:00 2001 From: fgsfds Date: Thu, 14 Sep 2023 21:35:05 +0200 Subject: [PATCH] port: allow specifying path to eeprom file --eeprom-file file.bin --- port/include/fs.h | 3 +++ port/src/fs.c | 16 ++++++++++++++-- port/src/libultra.c | 30 +++++++++++++++++++++++++++--- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/port/include/fs.h b/port/include/fs.h index dc299cf57..561c41456 100644 --- a/port/include/fs.h +++ b/port/include/fs.h @@ -10,6 +10,9 @@ s32 fsInit(void); const char *fsFullPath(const char *relPath); +s32 fsPathIsAbsolute(const char *path); +s32 fsPathIsCwdRelative(const char *path); + void *fsFileLoad(const char *name, u32 *outSize); s32 fsFileSize(const char *name); diff --git a/port/src/fs.c b/port/src/fs.c index 79d284155..fb56a6af9 100644 --- a/port/src/fs.c +++ b/port/src/fs.c @@ -18,7 +18,8 @@ static char saveDir[FS_MAXPATH + 1]; // replaces $S static char homeDir[FS_MAXPATH + 1]; // replaces $H static char exeDir[FS_MAXPATH + 1]; // replaces $E -static s32 fsPathIsWritable(const char *path) { +static s32 fsPathIsWritable(const char *path) +{ #ifdef PLATFORM_WIN32 // on windows access() on directories will only check if the directory exists, so char tmp[FS_MAXPATH + 1] = { 0 }; @@ -35,6 +36,17 @@ static s32 fsPathIsWritable(const char *path) { #endif } +s32 fsPathIsAbsolute(const char *path) +{ + return (path[0] == '/' || (isalpha(path[0]) && path[1] == ':')); +} + +s32 fsPathIsCwdRelative(const char *path) +{ + // ., .., ./, ../ + return (path[0] == '.' && (path[1] == '.' || path[1] == '/' || path[1] == '\\' || path[1] == '\0')); +} + const char *fsFullPath(const char *relPath) { static char pathBuf[FS_MAXPATH + 1]; @@ -59,7 +71,7 @@ const char *fsFullPath(const char *relPath) } // couldn't expand anything, return as is return relPath; - } else if (!dataDir[0] || relPath[0] == '.' || relPath[0] == '/' || (isalpha(relPath[0]) && relPath[1] == ':')) { + } else if (!dataDir[0] || fsPathIsAbsolute(relPath) || fsPathIsCwdRelative(relPath)) { // user explicitly wants working directory or this is an absolute path or we have no dataDir set up yet return relPath; } diff --git a/port/src/libultra.c b/port/src/libultra.c index aa1f7522a..08e8858cb 100644 --- a/port/src/libultra.c +++ b/port/src/libultra.c @@ -27,6 +27,7 @@ s32 osResetType = 0; /* 0 = cold reset */ s32 osViClock = VI_NTSC_CLOCK; static u8 eeprom[EEPROM_SIZE]; +static char eepromPath[FS_MAXPATH + 1]; static s32 eepromLoaded = 0; /* Time */ @@ -252,6 +253,21 @@ s32 __osMotorAccess(OSPfs *pfs, s32 cmd) /* Eeprom */ +static inline void osEepromSetPath(void) +{ + const char *extPath = sysArgGetString("--eeprom-file"); + if (extPath && extPath[0]) { + if (extPath[0] == '$' || fsPathIsAbsolute(extPath) || fsPathIsCwdRelative(extPath)) { + strncpy(eepromPath, extPath, FS_MAXPATH); + } else { + // just a filename, look for it in the save dir + snprintf(eepromPath, FS_MAXPATH, "$S/%s", extPath); + } + } else { + strncpy(eepromPath, EEPROM_PATH, FS_MAXPATH); + } +} + static inline void osEeepromLoad(const char *fname) { if (!eepromLoaded) { @@ -284,7 +300,11 @@ s32 osEepromProbe(OSMesgQueue *mq) s32 osEepromLongRead(OSMesgQueue *mq, u8 address, u8 *buffer, int nbytes) { - osEeepromLoad(EEPROM_PATH); + if (!eepromPath[0]) { + osEepromSetPath(); + } + + osEeepromLoad(eepromPath); memcpy(buffer, eeprom + address * 8, nbytes); @@ -293,11 +313,15 @@ s32 osEepromLongRead(OSMesgQueue *mq, u8 address, u8 *buffer, int nbytes) s32 osEepromLongWrite(OSMesgQueue *mq, u8 address, u8 *buffer, int nbytes) { - osEeepromLoad(EEPROM_PATH); + if (!eepromPath[0]) { + osEepromSetPath(); + } + + osEeepromLoad(eepromPath); memcpy(eeprom + address * 8, buffer, nbytes); - osEeepromSave(EEPROM_PATH); + osEeepromSave(eepromPath); return 0; }