From 7f937331075ed06ddef949ada6c148a7b9fcb4d9 Mon Sep 17 00:00:00 2001 From: Aurora Date: Tue, 30 Aug 2016 02:18:32 +0200 Subject: [PATCH 1/2] Rewrite the module copying function --- source/firm.c | 39 ++++++++++++++++++--------------------- source/memory.c | 2 +- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/source/firm.c b/source/firm.c index fe7629f..53fc7db 100755 --- a/source/firm.c +++ b/source/firm.c @@ -351,33 +351,30 @@ static inline void patch2xNativeAndSafeFirm(void) static inline void copySection0AndInjectSystemModules(void) { - u8 *arm11Section0 = (u8 *)firm + section[0].offset; + u32 srcModuleSize, + dstModuleSize; - struct + for(u8 *src = (u8 *)firm + section[0].offset, *srcEnd = src + section[0].size, *dst = section[0].address; + src < srcEnd; src += srcModuleSize, dst += dstModuleSize) { - u32 size; - const u8 *addr; - } modules[5]; + srcModuleSize = *(u32 *)(src + 0x104) * 0x200; + char *moduleName = (char *)(src + 0x200); - u32 n = 0, - loaderIndex; - u8 *pos = arm11Section0; + void *module; - for(u8 *end = pos + section[0].size; pos < end; pos += modules[n++].size) - { - modules[n].addr = pos; - modules[n].size = *(u32 *)(pos + 0x104) * 0x200; + if(memcmp(moduleName, "loader", 6) == 0) + { + module = (void *)injector; + dstModuleSize = injector_size; + } + else + { + module = src; + dstModuleSize = srcModuleSize; + } - if(memcmp(modules[n].addr + 0x200, "loader", 7) == 0) loaderIndex = n; + memcpy(dst, module, dstModuleSize); } - - modules[loaderIndex].addr = injector; - modules[loaderIndex].size = injector_size; - - pos = section[0].address; - - for(u32 i = 0; i < n; pos += modules[i++].size) - memcpy(pos, modules[i].addr, modules[i].size); } static inline void launchFirm(FirmwareType firmType) diff --git a/source/memory.c b/source/memory.c index e7a05c9..03a36ae 100644 --- a/source/memory.c +++ b/source/memory.c @@ -41,7 +41,7 @@ void memset32(void *dest, u32 filler, u32 size) { u32 *dest32 = (u32 *)dest; - for (u32 i = 0; i < size / 4; i++) + for(u32 i = 0; i < size / 4; i++) dest32[i] = filler; } From f221915a9524dd7fda6957f5dab8606fd973e1c7 Mon Sep 17 00:00:00 2001 From: Aurora Date: Tue, 30 Aug 2016 16:56:19 +0200 Subject: [PATCH 2/2] Get rid of createDirectory and make fileWrite handle directory tree creation --- source/config.c | 6 +----- source/fs.c | 24 +++++++++++++++++------- source/fs.h | 1 - source/pin.c | 6 +----- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/source/config.c b/source/config.c index b0fed6a..7518ae4 100644 --- a/source/config.c +++ b/source/config.c @@ -57,11 +57,7 @@ void writeConfig(const char *configPath, u32 configTemp) configData.formatVersionMinor = CONFIG_VERSIONMINOR; if(!fileWrite(&configData, configPath, sizeof(cfgData))) - { - createDirectory("luma"); - if(!fileWrite(&configData, configPath, sizeof(cfgData))) - error("Error writing the configuration file"); - } + error("Error writing the configuration file"); } } diff --git a/source/fs.c b/source/fs.c index f318026..f428bbe 100644 --- a/source/fs.c +++ b/source/fs.c @@ -64,7 +64,9 @@ bool fileWrite(const void *buffer, const char *path, u32 size) { FIL file; - if(f_open(&file, path, FA_WRITE | FA_OPEN_ALWAYS) == FR_OK) + FRESULT result = f_open(&file, path, FA_WRITE | FA_OPEN_ALWAYS); + + if(result == FR_OK) { unsigned int written; f_write(&file, buffer, size, &written); @@ -72,8 +74,21 @@ bool fileWrite(const void *buffer, const char *path, u32 size) return true; } + else if(result == FR_NO_PATH) + { + char folder[256]; - return false; + for(u32 i = 1; path[i] != 0; i++) + if(path[i] == '/') + { + memcpy(folder, path, i); + folder[i] = 0; + f_mkdir(folder); + } + + return fileWrite(buffer, path, size); + } + else return false; } void fileDelete(const char *path) @@ -81,11 +96,6 @@ void fileDelete(const char *path) f_unlink(path); } -void createDirectory(const char *path) -{ - f_mkdir(path); -} - void loadPayload(u32 pressed) { const char *pattern; diff --git a/source/fs.h b/source/fs.h index 7e7d9a9..d74f24f 100644 --- a/source/fs.h +++ b/source/fs.h @@ -33,6 +33,5 @@ u32 fileRead(void *dest, const char *path); u32 getFileSize(const char *path); bool fileWrite(const void *buffer, const char *path, u32 size); void fileDelete(const char *path); -void createDirectory(const char *path); void loadPayload(u32 pressed); u32 firmRead(void *dest, u32 firmType); \ No newline at end of file diff --git a/source/pin.c b/source/pin.c index 0f52200..e46a1d3 100644 --- a/source/pin.c +++ b/source/pin.c @@ -96,11 +96,7 @@ void newPin(bool allowSkipping) memcpy(pin.hash, tmp, 32); if(!fileWrite(&pin, "/luma/pin.bin", sizeof(PINData))) - { - createDirectory("luma"); - if(!fileWrite(&pin, "/luma/pin.bin", sizeof(PINData))) - error("Error writing the PIN file"); - } + error("Error writing the PIN file"); } bool verifyPin(void)