From ee8da6519defddeaf05eec7c174c24c2924bbe07 Mon Sep 17 00:00:00 2001 From: Pk11 Date: Sun, 9 Jan 2022 14:38:14 -0600 Subject: [PATCH] Better NAND written check Previously it'd break if you missed anywhere --- arm9/src/install.c | 2 -- arm9/src/installmenu.c | 1 - arm9/src/main.c | 7 ++---- arm9/src/main.h | 1 - arm9/src/nand/nandio.c | 54 ++++++++++++++++++++++++------------------ arm9/src/storage.c | 15 ------------ 6 files changed, 33 insertions(+), 47 deletions(-) diff --git a/arm9/src/install.c b/arm9/src/install.c index 5f2f6b9..a0d6169 100644 --- a/arm9/src/install.c +++ b/arm9/src/install.c @@ -391,8 +391,6 @@ bool install(char* fpath, bool systemTitle) sprintf(dirPath, "nand:/title/%08x/%08x", (unsigned int)h->tid_high, (unsigned int)h->tid_low); - nandWritten = true; - //check if title is free if (_titleIsUsed(h)) { diff --git a/arm9/src/installmenu.c b/arm9/src/installmenu.c index 2d04807..969ecf1 100644 --- a/arm9/src/installmenu.c +++ b/arm9/src/installmenu.c @@ -306,7 +306,6 @@ static bool delete(Menu* m) { if (remove(fpath) == 0) { - nandWritten = true; result = true; messageBox("\x1B[42mFile deleted.\x1B[47m"); } diff --git a/arm9/src/main.c b/arm9/src/main.c index 32589a0..188097f 100644 --- a/arm9/src/main.c +++ b/arm9/src/main.c @@ -7,7 +7,6 @@ #define VERSION "0.7.1" bool programEnd = false; -bool nandWritten = false; PrintConsole topScreen; PrintConsole bottomScreen; @@ -156,10 +155,8 @@ int main(int argc, char **argv) clearScreen(&bottomScreen); printf("Unmounting NAND...\n"); fatUnmount("nand:"); - if(nandWritten) { - printf("Merging stages...\n"); - nandio_shutdown(); - } + printf("Merging stages...\n"); + nandio_shutdown(); fifoSendValue32(FIFO_USER_02, 0x54495845); // 'EXIT' diff --git a/arm9/src/main.h b/arm9/src/main.h index ad00ee8..08f0915 100644 --- a/arm9/src/main.h +++ b/arm9/src/main.h @@ -6,7 +6,6 @@ #include extern bool programEnd; -extern bool nandWritten; void installMenu(); void titleMenu(); diff --git a/arm9/src/nand/nandio.c b/arm9/src/nand/nandio.c index 77ae219..c330b2c 100644 --- a/arm9/src/nand/nandio.c +++ b/arm9/src/nand/nandio.c @@ -33,6 +33,8 @@ const DISC_INTERFACE io_dsi_nand = { bool is3DS; +static bool nandWritten = false; + extern bool nand_Startup(); static u8* crypt_buf = 0; @@ -167,6 +169,8 @@ bool nandio_read_sectors(sec_t offset, sec_t len, void *buffer) bool nandio_write_sectors(sec_t offset, sec_t len, const void *buffer) { + nandWritten = true; + while (len >= CRYPT_BUF_LEN) { if (!write_sectors(offset, CRYPT_BUF_LEN, buffer)) @@ -193,33 +197,37 @@ bool nandio_clear_status() bool nandio_shutdown() { - // at cleanup we synchronize the FAT statgings - // A FatFS might have multiple copies of the FAT. - // we will get them back synchonized as we just worked on the first copy - // this allows us to revert changes in the FAT if we did not properly finish - // and did not push the changes to the other copies - // to do this we read the first partition sector - nandio_read_sectors(fat_sig_fix_offset, 1, sector_buf) ; - u8 stagingLevels = sector_buf[0x10] ; - u8 reservedSectors = sector_buf[0x0E] ; - u16 sectorsPerFatCopy = sector_buf[0x16] | ((u16)sector_buf[0x17] << 8) ; -/* - iprintf("[i] Staging for %i FAT copies\n",stagingLevels); - iprintf("[i] Stages starting at %i\n",reservedSectors); - iprintf("[i] %i sectors per stage\n",sectorsPerFatCopy); -*/ - if (stagingLevels > 1) - { - for (u32 sector = 0;sector < sectorsPerFatCopy; sector++) + if(nandWritten) { + + // at cleanup we synchronize the FAT statgings + // A FatFS might have multiple copies of the FAT. + // we will get them back synchonized as we just worked on the first copy + // this allows us to revert changes in the FAT if we did not properly finish + // and did not push the changes to the other copies + // to do this we read the first partition sector + nandio_read_sectors(fat_sig_fix_offset, 1, sector_buf) ; + u8 stagingLevels = sector_buf[0x10] ; + u8 reservedSectors = sector_buf[0x0E] ; + u16 sectorsPerFatCopy = sector_buf[0x16] | ((u16)sector_buf[0x17] << 8) ; + /* + iprintf("[i] Staging for %i FAT copies\n",stagingLevels); + iprintf("[i] Stages starting at %i\n",reservedSectors); + iprintf("[i] %i sectors per stage\n",sectorsPerFatCopy); + */ + if (stagingLevels > 1) { - // read fat sector - nandio_read_sectors(fat_sig_fix_offset + reservedSectors + sector, 1, sector_buf) ; - // write to each copy, except the source copy - for (int stage = 1;stage < stagingLevels;stage++) + for (u32 sector = 0;sector < sectorsPerFatCopy; sector++) { - nandio_write_sectors(fat_sig_fix_offset + reservedSectors + sector + (stage *sectorsPerFatCopy), 1, sector_buf) ; + // read fat sector + nandio_read_sectors(fat_sig_fix_offset + reservedSectors + sector, 1, sector_buf) ; + // write to each copy, except the source copy + for (int stage = 1;stage < stagingLevels;stage++) + { + nandio_write_sectors(fat_sig_fix_offset + reservedSectors + sector + (stage *sectorsPerFatCopy), 1, sector_buf) ; + } } } + nandWritten = false; } free(crypt_buf); crypt_buf = 0; diff --git a/arm9/src/storage.c b/arm9/src/storage.c index 74c8fe8..9e3fd78 100644 --- a/arm9/src/storage.c +++ b/arm9/src/storage.c @@ -83,9 +83,6 @@ int copyFile(char const* src, char const* dst) { if (!src) return 1; - if(strncmp(dst, "nand:", 5) == 0) - nandWritten = true; - unsigned long long size = getFileSizePath(src); return copyFilePart(src, 0, size, dst); } @@ -95,9 +92,6 @@ int copyFilePart(char const* src, u32 offset, u32 size, char const* dst) if (!src) return 1; if (!dst) return 2; - if(strncmp(dst, "nand:", 5) == 0) - nandWritten = true; - FILE* fin = fopen(src, "rb"); if (!fin) @@ -185,9 +179,6 @@ bool padFile(char const* path, int size) { if (!path) return false; - if(strncmp(path, "nand:", 5) == 0) - nandWritten = true; - FILE* f = fopen(path, "ab"); if (!f) { @@ -221,9 +212,6 @@ bool copyDir(char const* src, char const* dst) { if (!src || !dst) return false; - if(strncmp(dst, "nand:", 5) == 0) - nandWritten = true; - // iprintf("copyDir\n%s\n%s\n\n", src, dst); bool result = true; @@ -321,9 +309,6 @@ bool deleteDir(char const* path) { if (!path) return false; - if(strncmp(path, "nand:", 5) == 0) - nandWritten = true; - if (strcmp("/", path) == 0) { //oh fuck no