1
Fork 0
This repository has been archived on 2025-04-09. You can view files and clone it, but cannot push or open issues or pull requests.
arm9switcher/source/main.cpp
2024-01-03 16:09:05 -05:00

162 lines
3.1 KiB
C++

#include <3ds.h>
#include <stdio.h>
#include <cstdio>
#include <unistd.h>
//Filenames
#define EMUNAND_BIN "arm9loaderhax_emunand.bin"
#define SYSNAND_BIN "arm9loaderhax_sysnand.bin"
#define PAYLOAD_BIN "arm9loaderhax.bin"
//Options
#define USERINPUT true
bool choice(const char* msg, const char* ch1, const char* ch2) {
printf(msg);
printf(ch1);
printf(ch2);
while(aptMainLoop()) {
gspWaitForVBlank();
hidScanInput();
if(hidKeysDown() & KEY_A) {
return true;
}
if(hidKeysDown() & KEY_B) {
return false;
}
gfxFlushBuffers();
gfxSwapBuffers();
}
}
bool fileExists(const char* path) {
if( access( path, F_OK ) != -1 ) {
return true;
} else {
return false;
}
}
void changetoEmunand() {
printf("Changing to emunand...\n");
//TODO: Prompt user for conflict
if(fileExists(SYSNAND_BIN)) {
if(choice("Already existing sysnand bin! Remove?\n", "A: Yes\n", "B: No\n")) {
remove(SYSNAND_BIN);
printf("Removed existing sysnand bin...\n");
} else {
printf("Cancelling...\n");
return;
}
}
//Rename files
bool a = rename(PAYLOAD_BIN, SYSNAND_BIN);
if(!a) {
printf("Error while renaming file!\n");
}
rename(EMUNAND_BIN, PAYLOAD_BIN);
//Die
printf("Successfully changed payload to emunand!");
}
void changetoSysnand() {
printf("Changing to sysnand...\n");
//TODO: Prompt user for conflict
if(fileExists(EMUNAND_BIN)) {
if(choice("Already existing emunand bin! Remove?\n", "A: Yes\n", "B: No\n")) {
remove(EMUNAND_BIN);
printf("Removed existing emunand bin...\n");
} else {
printf("Cancelling...\n");
return;
}
}
//Rename files
bool a = rename(PAYLOAD_BIN, EMUNAND_BIN);
if(!a) {
printf("Error while renaming file!\n");
}
rename(SYSNAND_BIN, PAYLOAD_BIN);
//Die
printf("Successfully changed payload to sysnand!");
}
void scan() {
//Checks if this exists at all
if(fileExists(PAYLOAD_BIN)) {
printf("Found arm9loaderhax.bin\n");
//Change to emunand
if(fileExists(EMUNAND_BIN)) {
printf("Found emunand bin!\n");
changetoEmunand();
} else {
//Change to sysnand
if(fileExists(SYSNAND_BIN)) {
printf("Found sysnand bin!\n");
changetoSysnand();
}
}
} else {
//If both files exist, default to sysnand
if(fileExists(EMUNAND_BIN) && fileExists(SYSNAND_BIN)) {
printf("Changing to sysnand...\n");
//Rename files
rename(SYSNAND_BIN, PAYLOAD_BIN);
//Die
printf("Successfully changed payload to sysnand!");
} else {
//Redundant?
if(fileExists(SYSNAND_BIN)) {
changetoSysnand();
} else {
//Try emunand bin
if(fileExists(EMUNAND_BIN)) {
changetoEmunand();
} else {
//Die
}
}
}
}
}
int main(int argc, char **argv) {
gfxInitDefault();
consoleInit(GFX_BOTTOM, NULL);
printf("Checking for files on SD card...\n");
scan();
if(USERINPUT) {
printf("Press START to exit.");
while(aptMainLoop()) {
gspWaitForVBlank();
hidScanInput();
if(hidKeysDown() & KEY_START)
break;
gfxFlushBuffers();
gfxSwapBuffers();
}
}
gfxExit();
return 0;
}