Refactor the emuNAND code

This commit is contained in:
Aurora 2016-09-07 18:04:31 +02:00
parent 63073ea07f
commit deb91d1d02
4 changed files with 59 additions and 64 deletions

View File

@ -25,63 +25,58 @@
#include "fatfs/sdmmc/sdmmc.h" #include "fatfs/sdmmc/sdmmc.h"
#include "../build/emunandpatch.h" #include "../build/emunandpatch.h"
#define O3DS_TOSHIBA_NAND 0x1DD000 void locateEmuNand(u32 *emuHeader, FirmwareSource *emuNand)
#define O3DS_LEGACY_FAT 0x200000
#define O3DS_DEFAULT_FAT 0x1DE000
#define O3DS_MINIMUM_FAT 0x1D8000
#define N3DS_LEGACY_FAT 0x400000
#define N3DS_DEFAULT_FAT 0x3B2000
#define N3DS_MINIMUM_FAT 0x26E000
void locateEmuNand(u32 *off, u32 *head, FirmwareSource *emuNand)
{ {
static u8 temp[0x200]; static u8 temp[0x200];
const u32 nandSize = getMMCDevice(0)->total_size; const u32 nandSize = getMMCDevice(0)->total_size;
const u32 nandLayoutO3DS[3] = { O3DS_LEGACY_FAT, O3DS_DEFAULT_FAT, O3DS_MINIMUM_FAT }; // Legacy, Default, Minimum
const u32 nandLayoutN3DS[3] = { N3DS_LEGACY_FAT, N3DS_DEFAULT_FAT, N3DS_MINIMUM_FAT }; // Legacy, Default, Minimum
u8 i;
u32 nandOffset;
bool found = false; bool found = false;
for (i = 0; i < 3; i++) for (u32 i = 0; i < 3 && !found; i++)
{ {
if (i > 0 && *emuNand != FIRMWARE_EMUNAND2) break; u32 nandOffset;
// Check for 'Legacy', 'Default' and 'Minimum' partition layouts when checking for the 2nd EmuNAND switch(i)
nandOffset = (*emuNand == FIRMWARE_EMUNAND ? 0 : ((isN3DS || nandSize > O3DS_TOSHIBA_NAND) ? nandLayoutN3DS[i] : nandLayoutO3DS[i])); {
case 1:
// Exception for 2DS nandOffset = nandSize + 1; //"Default" layout
if (i == 2 && !isN3DS && nandOffset == N3DS_MINIMUM_FAT) nandOffset = O3DS_MINIMUM_FAT; break;
case 2:
nandOffset = isN3DS ? 0x26E000 : 0x1D8000; //"Minsize" layout
break;
default:
nandOffset = *emuNand == FIRMWARE_EMUNAND ? 0 : (nandSize > 0x200000 ? 0x400000 : 0x200000); //"Legacy" layout
break;
}
//Check for RedNAND //Check for RedNAND
if(!sdmmc_sdcard_readsectors(nandOffset + 1, 1, temp) && *(u32 *)(temp + 0x100) == NCSD_MAGIC) if(!sdmmc_sdcard_readsectors(nandOffset + 1, 1, temp) && *(u32 *)(temp + 0x100) == NCSD_MAGIC)
{ {
*off = nandOffset + 1; emuOffset = nandOffset + 1;
*head = nandOffset + 1; *emuHeader = nandOffset + 1;
found = true; found = true;
break;
} }
//Check for Gateway emuNAND //Check for Gateway emuNAND
else if(!sdmmc_sdcard_readsectors(nandOffset + nandSize, 1, temp) && *(u32 *)(temp + 0x100) == NCSD_MAGIC) else if(!sdmmc_sdcard_readsectors(nandOffset + nandSize, 1, temp) && *(u32 *)(temp + 0x100) == NCSD_MAGIC)
{ {
*off = nandOffset; emuOffset = nandOffset;
*head = nandOffset + nandSize; *emuHeader = nandOffset + nandSize;
found = true; found = true;
break;
} }
if(*emuNand == FIRMWARE_EMUNAND) break;
} }
/* Fallback to the first emuNAND if there's no second one, /* Fallback to the first emuNAND if there's no second one,
or to SysNAND if there isn't any */ or to SysNAND if there isn't any */
if(!found) if(!found)
{ {
*emuNand = (*emuNand == FIRMWARE_EMUNAND2) ? FIRMWARE_EMUNAND : FIRMWARE_SYSNAND; if(*emuNand != FIRMWARE_EMUNAND)
if(*emuNand) locateEmuNand(off, head, emuNand); {
*emuNand = FIRMWARE_EMUNAND;
locateEmuNand(emuHeader, emuNand);
}
else *emuNand = FIRMWARE_SYSNAND;
} }
} }

View File

@ -29,5 +29,5 @@
extern u32 emuOffset; extern u32 emuOffset;
extern bool isN3DS; extern bool isN3DS;
void locateEmuNand(u32 *off, u32 *head, FirmwareSource *emuNand); void locateEmuNand(u32 *emuHeader, FirmwareSource *emuNand);
void patchEmuNand(u8 *arm9Section, u32 arm9SectionSize, u8 *process9Offset, u32 process9Size, u32 emuHeader, u32 branchAdditive); void patchEmuNand(u8 *arm9Section, u32 arm9SectionSize, u8 *process9Offset, u32 process9Size, u32 emuHeader, u32 branchAdditive);

View File

@ -195,13 +195,13 @@ void main(void)
//If we need to boot emuNAND, make sure it exists //If we need to boot emuNAND, make sure it exists
if(nandType != FIRMWARE_SYSNAND) if(nandType != FIRMWARE_SYSNAND)
{ {
locateEmuNand(&emuOffset, &emuHeader, &nandType); locateEmuNand(&emuHeader, &nandType);
if(nandType == FIRMWARE_SYSNAND) firmSource = FIRMWARE_SYSNAND; if(nandType == FIRMWARE_SYSNAND) firmSource = FIRMWARE_SYSNAND;
} }
//Same if we're using emuNAND as the FIRM source //Same if we're using emuNAND as the FIRM source
else if(firmSource != FIRMWARE_SYSNAND) else if(firmSource != FIRMWARE_SYSNAND)
locateEmuNand(&emuOffset, &emuHeader, &firmSource); locateEmuNand(&emuHeader, &firmSource);
if(!isFirmlaunch) if(!isFirmlaunch)
{ {