Better NAND written check

Previously it'd break if you missed anywhere
This commit is contained in:
Pk11 2022-01-09 14:38:14 -06:00
parent 0d957e476a
commit ee8da6519d
6 changed files with 33 additions and 47 deletions

View File

@ -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); sprintf(dirPath, "nand:/title/%08x/%08x", (unsigned int)h->tid_high, (unsigned int)h->tid_low);
nandWritten = true;
//check if title is free //check if title is free
if (_titleIsUsed(h)) if (_titleIsUsed(h))
{ {

View File

@ -306,7 +306,6 @@ static bool delete(Menu* m)
{ {
if (remove(fpath) == 0) if (remove(fpath) == 0)
{ {
nandWritten = true;
result = true; result = true;
messageBox("\x1B[42mFile deleted.\x1B[47m"); messageBox("\x1B[42mFile deleted.\x1B[47m");
} }

View File

@ -7,7 +7,6 @@
#define VERSION "0.7.1" #define VERSION "0.7.1"
bool programEnd = false; bool programEnd = false;
bool nandWritten = false;
PrintConsole topScreen; PrintConsole topScreen;
PrintConsole bottomScreen; PrintConsole bottomScreen;
@ -156,10 +155,8 @@ int main(int argc, char **argv)
clearScreen(&bottomScreen); clearScreen(&bottomScreen);
printf("Unmounting NAND...\n"); printf("Unmounting NAND...\n");
fatUnmount("nand:"); fatUnmount("nand:");
if(nandWritten) { printf("Merging stages...\n");
printf("Merging stages...\n"); nandio_shutdown();
nandio_shutdown();
}
fifoSendValue32(FIFO_USER_02, 0x54495845); // 'EXIT' fifoSendValue32(FIFO_USER_02, 0x54495845); // 'EXIT'

View File

@ -6,7 +6,6 @@
#include <stdio.h> #include <stdio.h>
extern bool programEnd; extern bool programEnd;
extern bool nandWritten;
void installMenu(); void installMenu();
void titleMenu(); void titleMenu();

View File

@ -33,6 +33,8 @@ const DISC_INTERFACE io_dsi_nand = {
bool is3DS; bool is3DS;
static bool nandWritten = false;
extern bool nand_Startup(); extern bool nand_Startup();
static u8* crypt_buf = 0; 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) bool nandio_write_sectors(sec_t offset, sec_t len, const void *buffer)
{ {
nandWritten = true;
while (len >= CRYPT_BUF_LEN) while (len >= CRYPT_BUF_LEN)
{ {
if (!write_sectors(offset, CRYPT_BUF_LEN, buffer)) if (!write_sectors(offset, CRYPT_BUF_LEN, buffer))
@ -193,33 +197,37 @@ bool nandio_clear_status()
bool nandio_shutdown() bool nandio_shutdown()
{ {
// at cleanup we synchronize the FAT statgings if(nandWritten) {
// A FatFS might have multiple copies of the FAT.
// we will get them back synchonized as we just worked on the first copy // at cleanup we synchronize the FAT statgings
// this allows us to revert changes in the FAT if we did not properly finish // A FatFS might have multiple copies of the FAT.
// and did not push the changes to the other copies // we will get them back synchonized as we just worked on the first copy
// to do this we read the first partition sector // this allows us to revert changes in the FAT if we did not properly finish
nandio_read_sectors(fat_sig_fix_offset, 1, sector_buf) ; // and did not push the changes to the other copies
u8 stagingLevels = sector_buf[0x10] ; // to do this we read the first partition sector
u8 reservedSectors = sector_buf[0x0E] ; nandio_read_sectors(fat_sig_fix_offset, 1, sector_buf) ;
u16 sectorsPerFatCopy = sector_buf[0x16] | ((u16)sector_buf[0x17] << 8) ; u8 stagingLevels = sector_buf[0x10] ;
/* u8 reservedSectors = sector_buf[0x0E] ;
iprintf("[i] Staging for %i FAT copies\n",stagingLevels); u16 sectorsPerFatCopy = sector_buf[0x16] | ((u16)sector_buf[0x17] << 8) ;
iprintf("[i] Stages starting at %i\n",reservedSectors); /*
iprintf("[i] %i sectors per stage\n",sectorsPerFatCopy); iprintf("[i] Staging for %i FAT copies\n",stagingLevels);
*/ iprintf("[i] Stages starting at %i\n",reservedSectors);
if (stagingLevels > 1) iprintf("[i] %i sectors per stage\n",sectorsPerFatCopy);
{ */
for (u32 sector = 0;sector < sectorsPerFatCopy; sector++) if (stagingLevels > 1)
{ {
// read fat sector for (u32 sector = 0;sector < sectorsPerFatCopy; 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) ; // 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); free(crypt_buf);
crypt_buf = 0; crypt_buf = 0;

View File

@ -83,9 +83,6 @@ int copyFile(char const* src, char const* dst)
{ {
if (!src) return 1; if (!src) return 1;
if(strncmp(dst, "nand:", 5) == 0)
nandWritten = true;
unsigned long long size = getFileSizePath(src); unsigned long long size = getFileSizePath(src);
return copyFilePart(src, 0, size, dst); 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 (!src) return 1;
if (!dst) return 2; if (!dst) return 2;
if(strncmp(dst, "nand:", 5) == 0)
nandWritten = true;
FILE* fin = fopen(src, "rb"); FILE* fin = fopen(src, "rb");
if (!fin) if (!fin)
@ -185,9 +179,6 @@ bool padFile(char const* path, int size)
{ {
if (!path) return false; if (!path) return false;
if(strncmp(path, "nand:", 5) == 0)
nandWritten = true;
FILE* f = fopen(path, "ab"); FILE* f = fopen(path, "ab");
if (!f) if (!f)
{ {
@ -221,9 +212,6 @@ bool copyDir(char const* src, char const* dst)
{ {
if (!src || !dst) return false; if (!src || !dst) return false;
if(strncmp(dst, "nand:", 5) == 0)
nandWritten = true;
// iprintf("copyDir\n%s\n%s\n\n", src, dst); // iprintf("copyDir\n%s\n%s\n\n", src, dst);
bool result = true; bool result = true;
@ -321,9 +309,6 @@ bool deleteDir(char const* path)
{ {
if (!path) return false; if (!path) return false;
if(strncmp(path, "nand:", 5) == 0)
nandWritten = true;
if (strcmp("/", path) == 0) if (strcmp("/", path) == 0)
{ {
//oh fuck no //oh fuck no