Skip to content

Commit

Permalink
Refactor boot mode/firmlaunch detection, fix firmlaunch from FIRM boots
Browse files Browse the repository at this point in the history
  • Loading branch information
AuroraWright committed Aug 27, 2017
1 parent 2492c82 commit df93e47
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 58 deletions.
27 changes: 12 additions & 15 deletions source/exceptions.c
Expand Up @@ -89,20 +89,7 @@ void detectAndProcessExceptionDumps(void)
drawString(true, 10, 10, COLOR_RED, "An exception occurred");
u32 posY;
if(dumpHeader->processor == 11) posY = drawFormattedString(true, 10, 30, COLOR_WHITE, "Processor: ARM11 (core %u)", dumpHeader->core);
else posY = drawString(true, 10, 30, COLOR_WHITE, "Processor: ARM9");

const char *faultStatusInfos = NULL;
if(dumpHeader->type >= 2)
{
u32 xfsr = dumpHeader->type == 2 ? regs[18] : regs[17];
xfsr &= 0xF;
for(u32 i = 0; i < 15; i++)
if(xfsr == faultStatusValues[i])
{
faultStatusInfos = faultStatusNames[i];
break;
}
}
else posY = drawString(true, 10, 30, COLOR_WHITE, "Processor: ARM9");

if(dumpHeader->type == 2)
{
Expand Down Expand Up @@ -130,7 +117,17 @@ void detectAndProcessExceptionDumps(void)
else
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "Exception type: %s", handledExceptionNames[dumpHeader->type]);

if(faultStatusInfos != NULL) posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "Fault status: %s", faultStatusInfos);
if(dumpHeader->type >= 2)
{
u32 xfsr = (dumpHeader->type == 2 ? regs[18] : regs[17]) & 0xF;

for(u32 i = 0; i < 15; i++)
if(xfsr == faultStatusValues[i])
{
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "Fault status: %s", faultStatusNames[i]);
break;
}
}

if(dumpHeader->processor == 11 && dumpHeader->additionalDataSize != 0)
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE,
Expand Down
74 changes: 38 additions & 36 deletions source/main.c
Expand Up @@ -42,60 +42,65 @@ extern CfgData configData;
extern ConfigurationStatus needConfig;
extern FirmwareSource firmSource;

bool isFirmlaunch = false,
isSdMode;
bool isSdMode;
u16 launchedPath[41];
BootType bootType;

void main(int argc, char **argv, u32 magicWord)
{
bool isFirmProtEnabled,
isSafeMode = false,
isNoForceFlagSet = false;
isNoForceFlagSet = false,
isNtrBoot;
FirmwareType firmType;
FirmwareSource nandType;
const vu8 *bootMediaStatus = (const vu8 *)0x1FFFE00C;
const vu32 *bootPartitionsStatus = (const vu32 *)0x1FFFE010;

//Shell closed, no error booting NTRCARD, NAND paritions not even considered
bootType = (bootMediaStatus[3] == 2 && !bootMediaStatus[1] && !bootPartitionsStatus[0] && !bootPartitionsStatus[1]) ? NTR : B9S;

isNtrBoot = bootMediaStatus[3] == 2 && !bootMediaStatus[1] && !bootPartitionsStatus[0] && !bootPartitionsStatus[1];
if((magicWord & 0xFFFF) == 0xBEEF && argc >= 1) //Normal (B9S) boot
{
bootType = isNtrBoot ? B9SNTR : B9S;

u32 i;
for(i = 0; i < 40 && argv[0][i] != 0; i++) //Copy and convert the path to UTF-16
launchedPath[i] = argv[0][i];
launchedPath[i] = 0;
}
else if(magicWord == 0xBABE && argc == 2) //Firmlaunch
{
bootType = FIRMLAUNCH;

u32 i;
u16 *p = (u16 *)argv[0];
for(i = 0; i < 40 && p[i] != 0; i++)
launchedPath[i] = p[i];
launchedPath[i] = 0;

isFirmlaunch = true;
}
else if(magicWord == 0xB002) //FIRM/NTRCARD boot
{
if(bootType != NTR)
if(isNtrBoot) bootType = NTR;
else
{
const char *path;
if(!((vu8 *)bootPartitionsStatus)[2])
{
bootType = FIRM0;
path = "firm0:";
}
else
{
bootType = FIRM1;
path = "firm1:";
}

for(u32 i = 0; i < 7; i++) //Copy and convert the path to UTF-16
launchedPath[i] = path[i];
const char *path;
if(!((vu8 *)bootPartitionsStatus)[2])
{
bootType = FIRM0;
path = "firm0:";
}
else
{
bootType = FIRM1;
path = "firm1:";
}

for(u32 i = 0; i < 7; i++) //Copy and convert the path to UTF-16
launchedPath[i] = path[i];
}

setupKeyslots();
}
else error("Launched using an unsupported loader.");

Expand All @@ -110,13 +115,18 @@ void main(int argc, char **argv, u32 magicWord)
if(!mountFs(false, true)) error("Failed to mount CTRNAND.");
isSdMode = false;
}
else if(bootType != B9S)
else if(bootType == NTR || memcmp(launchedPath, u"firm", 8) == 0)
{
setupKeyslots();

if(mountFs(true, false)) isSdMode = true;
else if(mountFs(false, true)) isSdMode = false;
else error("Failed to mount SD and CTRNAND.");

if(bootType == NTR)
{
while(HID_PAD & NTRBOOT_BUTTONS);
loadHomebrewFirm(0);
mcuPowerOff();
}
}
else
{
Expand All @@ -130,18 +140,11 @@ void main(int argc, char **argv, u32 magicWord)
error("Launched from an unsupported location: %s.", mountPoint);
}

if(bootType == NTR && magicWord == 0xB002)
{
while(HID_PAD & NTRBOOT_BUTTONS);
loadHomebrewFirm(0);
mcuPowerOff();
}

//Attempt to read the configuration file
needConfig = readConfig() ? MODIFY_CONFIGURATION : CREATE_CONFIGURATION;

//Determine if this is a firmlaunch boot
if(isFirmlaunch)
if(bootType == FIRMLAUNCH)
{
if(needConfig == CREATE_CONFIGURATION) mcuPowerOff();

Expand Down Expand Up @@ -177,7 +180,6 @@ void main(int argc, char **argv, u32 magicWord)
//If it's a MCU reboot, try to force boot options
if(CFG_BOOTENV && needConfig != CREATE_CONFIGURATION)
{

//Always force a SysNAND boot when quitting AGB_FIRM
if(CFG_BOOTENV == 7)
{
Expand Down Expand Up @@ -310,7 +312,7 @@ void main(int argc, char **argv, u32 magicWord)
else if(firmSource != FIRMWARE_SYSNAND)
locateEmuNand(&firmSource);

if(!isFirmlaunch)
if(bootType == FIRMLAUNCH)
{
configData.bootConfig = ((bootType == NTR ? 1 : 0) << 7) | ((u32)isNoForceFlagSet << 6) | ((u32)firmSource << 3) | (u32)nandType;
writeConfig(false);
Expand Down Expand Up @@ -343,6 +345,6 @@ void main(int argc, char **argv, u32 magicWord)

if(res != 0) error("Failed to apply %u FIRM patch(es).", res);

if(!isFirmlaunch) deinitScreens();
if(bootType != FIRMLAUNCH) deinitScreens();
launchFirm(0, NULL);
}
10 changes: 5 additions & 5 deletions source/types.h
Expand Up @@ -117,16 +117,16 @@ typedef enum FirmwareType
typedef enum bootType
{
B9S = 0,
NTR,
B9SNTR,
FIRM0,
FIRM1
FIRM1,
FIRMLAUNCH,
NTR
} BootType;

extern bool isFirmlaunch,
isSdMode;
extern bool isSdMode;

extern BootType bootType;

extern u16 launchedFirmTidLow[8];
extern u16 launchedPath[41];

4 changes: 2 additions & 2 deletions source/utils.c
Expand Up @@ -104,7 +104,7 @@ u32 waitInput(bool isMenu)

void mcuPowerOff(void)
{
if(!isFirmlaunch && ARESCREENSINITIALIZED) clearScreens(false);
if(bootType != FIRMLAUNCH && ARESCREENSINITIALIZED) clearScreens(false);

//Ensure that all memory transfers have completed and that the data cache has been flushed
flushEntireDCache();
Expand All @@ -131,7 +131,7 @@ void error(const char *fmt, ...)
vsprintf(buf, fmt, args);
va_end(args);

if(!isFirmlaunch)
if(bootType != FIRMLAUNCH)
{
initScreens();

Expand Down

0 comments on commit df93e47

Please sign in to comment.