Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boot order preference via sd:/boot_order.txt #39

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions stage2/arm9/source/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,61 @@ 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);

if (fr != FR_OK || br == 0)
{
endOfFile = true;
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