Improve detection of device that contains the NDS ROM

When accessing the filesystem, what a user normally wants is to access
the same drive that holds the NDS ROM being run.

On the DS it is easy. The only available drive is the one of the DLDI
driver.

On DSi, it isn't so easy. It is possible to run a ROM that is either on
the SD card or in a flashcard (accessed with DLDI).

The easiest way to determine the right drive to use is to use argv[0].
The loader stores the location of the NDS ROM there, including the
drive.

The trivial detection should only be used as a fallback mechanism if
argv[0] isn't present.
This commit is contained in:
Antonio Niño Díaz 2023-04-19 00:47:52 +01:00
parent 5ef632b409
commit 4a257af310

View File

@ -218,16 +218,43 @@ void NF_SetRootFolder(const char* folder) {
// Define la carpeta inicial de la FAT
sprintf(NF_ROOTFOLDER, "%s", folder);
// Check if DSi SD (sd:/) exists.
// If this fails, check if DLDI (fat:/) exists.
// If both fails, then fatInitDefault() either failed, or was not called.
if (access("sd:/", F_OK) == 0) {
// Si es correcto, cambia al ROOT del SD
chdir("sd:/");
} else if (access("fat:/", F_OK) == 0) {
// Si es correcto, cambia al ROOT del FAT
chdir("fat:/");
} else {
// Check where the NDS is running from
bool init_ok = false;
// First, try to detect the drive where the NDS ROM is. If argv has been
// provided by the loader, it will contain the drive and the path of the
// ROM.
if (__system_argv->argvMagic == ARGV_MAGIC && __system_argv->argc >= 1) {
if (strncmp(__system_argv->argv[0], "fat:", 4) == 0) {
// If argv starts by "fat:", try to setup "fat:" as root
if (access("fat:/", F_OK) == 0) {
chdir("fat:/");
init_ok = true;
}
} else if (strncmp(__system_argv->argv[0], "sd:", 3) == 0) {
// If argv starts by "sd:", try to setup "sd:" as root
if (access("sd:/", F_OK) == 0) {
chdir("sd:/");
init_ok = true;
}
}
}
// Second, try to bruteforce the detection. Check if there is access to
// the SD card of the DSi first. If not, try with DLDI.
if (!init_ok) {
if (access("sd:/", F_OK) == 0) {
chdir("sd:/");
init_ok = true;
} else if (access("fat:/", F_OK) == 0) {
chdir("fat:/");
init_ok = true;
}
}
// If that didn't work, give up.
if (!init_ok) {
// Fallo. Deten el programa
consoleDemoInit(); // Inicializa la consola de texto
if (NF_GetLanguage() == 5) {
@ -248,9 +275,7 @@ void NF_SetRootFolder(const char* folder) {
swiWaitForVBlank();
}
}
}
}