Ensure the booted EmuNAND can actually exist before the FAT partition

This commit is contained in:
Aurora 2016-09-22 20:00:48 +02:00
parent 5e8990f571
commit e11edd5dee

View File

@ -27,12 +27,21 @@
void locateEmuNand(u32 *emuHeader, FirmwareSource *nandType) void locateEmuNand(u32 *emuHeader, FirmwareSource *nandType)
{ {
static u8 temp[0x200]; static u8 __attribute__((aligned(4))) temp[0x200];
const u32 nandSize = getMMCDevice(0)->total_size; static u32 nandSize = 0,
fatStart;
bool found = false; bool found = false;
if(!nandSize && !sdmmc_sdcard_readsectors(0, 1, temp))
{
nandSize = getMMCDevice(0)->total_size;
fatStart = *(u32 *)(temp + 0x1C6); //First sector of the FAT partition
}
for(u32 i = 0; i < 3 && !found; i++) for(u32 i = 0; i < 3 && !found; i++)
{ {
static const u32 roundedMinsizes[] = {0x1D8000, 0x26E000};
u32 nandOffset; u32 nandOffset;
switch(i) switch(i)
{ {
@ -40,7 +49,7 @@ void locateEmuNand(u32 *emuHeader, FirmwareSource *nandType)
nandOffset = ROUND_TO_4MB(nandSize + 1); //"Default" layout nandOffset = ROUND_TO_4MB(nandSize + 1); //"Default" layout
break; break;
case 2: case 2:
nandOffset = isN3DS ? 0x26E000 : 0x1D8000; //"Minsize" layout nandOffset = roundedMinsizes[isN3DS ? 1 : 0]; //"Minsize" layout
break; break;
default: default:
nandOffset = *nandType == FIRMWARE_EMUNAND ? 0 : (nandSize > 0x200000 ? 0x400000 : 0x200000); //"Legacy" layout nandOffset = *nandType == FIRMWARE_EMUNAND ? 0 : (nandSize > 0x200000 ? 0x400000 : 0x200000); //"Legacy" layout
@ -49,6 +58,8 @@ void locateEmuNand(u32 *emuHeader, FirmwareSource *nandType)
if(*nandType != FIRMWARE_EMUNAND) nandOffset *= ((u32)*nandType - 1); if(*nandType != FIRMWARE_EMUNAND) nandOffset *= ((u32)*nandType - 1);
if(fatStart >= nandOffset + roundedMinsizes[isN3DS ? 1 : 0])
{
//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)
{ {
@ -64,6 +75,7 @@ void locateEmuNand(u32 *emuHeader, FirmwareSource *nandType)
*emuHeader = nandOffset + nandSize; *emuHeader = nandOffset + nandSize;
found = true; found = true;
} }
}
if(*nandType == FIRMWARE_EMUNAND) break; if(*nandType == FIRMWARE_EMUNAND) break;
} }