Skip to content

Commit

Permalink
Boot order preference implemented via sd:/boot_order.txt, defaults to…
Browse files Browse the repository at this point in the history
… boot.firm
  • Loading branch information
PlugNPush committed Jan 14, 2024
1 parent b16b547 commit 38fcecf
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
60 changes: 60 additions & 0 deletions stage2/arm9/source/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,63 @@ bool fileWrite(const void *buffer, const char *path, u32 size)
return false;
}
}

bool fileExists(const char* path)
{
FIL file;
if(f_open(&file, path, FA_READ) != FR_OK) return false;
f_close(&file);
return true;
}

u32 fileReadLine(char *dest, const char *path, u32 lineNum, u32 maxSize)
{
FIL fil;
FRESULT fr;
UINT br;
u32 totalRead = 0;
u32 currentLine = 0;
bool lineContentStarted = false;
bool endOfFile = false;

fr = f_open(&fil, path, FA_READ);
if (fr != FR_OK)
return 0;

while (!endOfFile)
{
char c;
fr = f_read(&fil, &c, 1, &br);

// Check for end of file or read error
if (fr != FR_OK || br == 0)
{
endOfFile = true;
// Check if end of file is reached at the target line
if (currentLine != lineNum || !lineContentStarted)
break;
}

if (c == '\n' || c == '\r')
{
if (currentLine == lineNum)
{
if (!lineContentStarted)
totalRead = 1;
break;
}
currentLine++;
lineContentStarted = false;
}
else if (currentLine == lineNum && totalRead < maxSize - 1)
{
dest[totalRead++] = c;
lineContentStarted = true;
}
}

dest[totalRead] = '\0';
f_close(&fil);

return totalRead;
}
2 changes: 2 additions & 0 deletions stage2/arm9/source/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ bool mountCtrNand(void);
void unmountCtrNand(void);
u32 fileRead(void *dest, const char *path, u32 size, u32 maxSize);
bool fileWrite(const void *buffer, const char *path, u32 size);
bool fileExists(const char* path);
u32 fileReadLine(char *dest, const char *path, u32 lineNum, u32 maxSize);
31 changes: 30 additions & 1 deletion stage2/arm9/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,38 @@ static void invokeArm11Function(Arm11Operation op)
while(*operation != ARM11_READY);
}

const char *getFirmNameFromBootOrder(void)
{
const char* bootOrder = NULL;
if (fileExists("boot_order.txt"))
bootOrder = "boot_order.txt";
else if (fileExists("bootorder.txt"))
bootOrder = "bootorder.txt";

if (bootOrder)
{
static char line[2048];
u32 lineNumber = 0;
u32 bytesRead;

while ((bytesRead = fileReadLine(line, bootOrder, lineNumber, sizeof(line))) > 0)
{
line[bytesRead] = '\0';

if (fileExists(line))
return line;

lineNumber++;
}
}

return "boot.firm";
}

static FirmLoadStatus loadFirm(Firm **outFirm)
{
static const char *firmName = "boot.firm";
const char *firmName = getFirmNameFromBootOrder();

Firm *firmHeader = (Firm *)0x080A0000;
u32 rd = fileRead(firmHeader, firmName, 0x200, 0);
if (rd != 0x200)
Expand Down

0 comments on commit 38fcecf

Please sign in to comment.