diff --git a/source/common/common.h b/source/common/common.h index 150fae7..48aeb1d 100644 --- a/source/common/common.h +++ b/source/common/common.h @@ -46,7 +46,7 @@ #endif // input / output paths -#define SUPPORT_PATHS "0:/gm9/support", "0:", "0:/files9" // legacy paths +#define SUPPORT_PATH "0:/gm9/support" #define SCRIPT_PATH "0:/gm9/scripts" #define PAYLOAD_PATH "0:/gm9/payloads" #define OUTPUT_PATH "0:/gm9/out" diff --git a/source/crypto/keydb.c b/source/crypto/keydb.c index 0d1462a..68b64ed 100644 --- a/source/crypto/keydb.c +++ b/source/crypto/keydb.c @@ -128,15 +128,9 @@ u32 LoadKeyDb(const char* path_db, AesKeyInfo* keydb, u32 bsize) { if (fsize) memcpy(keydb, aeskeydb_bin, aeskeydb_bin_size); #else // try to load aeskeydb.bin file - const char* base[] = { SUPPORT_PATHS }; - for (u32 i = 0; i < (sizeof(base)/sizeof(char*)); i++) { - char path[64]; - snprintf(path, 64, "%s/%s", base[i], KEYDB_NAME); - if (f_open(&fp, path, FA_READ | FA_OPEN_EXISTING) == FR_OK) { - if ((f_read(&fp, keydb, bsize, &fsize) != FR_OK) || (fsize >= bsize)) fsize = 0; - f_close(&fp); - break; - } + if (f_open(&fp, SUPPORT_PATH "/" KEYDB_NAME, FA_READ | FA_OPEN_EXISTING) == FR_OK) { + if ((f_read(&fp, keydb, bsize, &fsize) != FR_OK) || (fsize >= bsize)) fsize = 0; + f_close(&fp); } #endif } @@ -149,7 +143,6 @@ u32 LoadKeyDb(const char* path_db, AesKeyInfo* keydb, u32 bsize) { u32 LoadKeyFromFile(void* key, u32 keyslot, char type, char* id) { - const char* base[] = { SUPPORT_PATHS }; u8 keystore[16] __attribute__((aligned(32))) = {0}; bool found = false; @@ -181,15 +174,13 @@ u32 LoadKeyFromFile(void* key, u32 keyslot, char type, char* id) // load legacy slot0x??Key?.bin file instead if (!found && (type != 'I')) { - for (u32 i = 0; !found && (i < (sizeof(base)/sizeof(char*))); i++) { - FIL fp; - char path[64]; - UINT btr; - snprintf(path, 64, "%s/slot0x%02lXKey%s%s.bin", base[i], keyslot, - (type == 'X') ? "X" : (type == 'Y') ? "Y" : (type == 'I') ? "IV" : "", (id) ? id : ""); - if (f_open(&fp, path, FA_READ | FA_OPEN_EXISTING) != FR_OK) continue; - if ((f_read(&fp, key, 16, &btr) == FR_OK) && (btr == 16)) - found = true; + char path[64]; + FIL fp; + UINT btr; + snprintf(path, 64, "%s/slot0x%02lXKey%s%s.bin", SUPPORT_PATH, keyslot, + (type == 'X') ? "X" : (type == 'Y') ? "Y" : (type == 'I') ? "IV" : "", (id) ? id : ""); + if (f_open(&fp, path, FA_READ | FA_OPEN_EXISTING) == FR_OK) { + found = ((f_read(&fp, key, 16, &btr) == FR_OK) && (btr == 16)); f_close(&fp); } } diff --git a/source/game/ncch.c b/source/game/ncch.c index 0c4259e..ffe73bc 100644 --- a/source/game/ncch.c +++ b/source/game/ncch.c @@ -108,26 +108,22 @@ u32 GetNcchSeed(u8* seed, NcchHeader* ncch) { } // not found -> try seeddb.bin - const char* base[] = { SUPPORT_PATHS }; - for (u32 i = 0; i < (sizeof(base)/sizeof(char*)); i++) { + if (f_open(&file, SUPPORT_PATH "/" SEEDDB_NAME, FA_READ | FA_OPEN_EXISTING) == FR_OK) { SeedInfo* seeddb = (SeedInfo*) (TEMP_BUFFER + (TEMP_BUFFER_SIZE/2)); - snprintf(path, 128, "%s/%s", base[i], SEEDDB_NAME); - if (f_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK) - continue; f_read(&file, seeddb, TEMP_BUFFER_SIZE / 2, &btr); f_close(&file); - if (seeddb->n_entries > (btr - 16) / 32) - continue; // filesize / seeddb size mismatch - for (u32 s = 0; s < seeddb->n_entries; s++) { - if (titleId != seeddb->entries[s].titleId) - continue; - memcpy(lseed, seeddb->entries[s].seed, 16); - sha_quick(sha256sum, lseed, 16 + 8, SHA256_MODE); - if (hash_seed == sha256sum[0]) { - memcpy(seed, lseed, 16); - return 0; // found! + if (seeddb->n_entries <= (btr - 16) / 32) { // check filesize / seeddb size + for (u32 s = 0; s < seeddb->n_entries; s++) { + if (titleId != seeddb->entries[s].titleId) + continue; + memcpy(lseed, seeddb->entries[s].seed, 16); + sha_quick(sha256sum, lseed, 16 + 8, SHA256_MODE); + if (hash_seed == sha256sum[0]) { + memcpy(seed, lseed, 16); + return 0; // found! + } } - } + } } // out of options -> failed! diff --git a/source/game/ticket.c b/source/game/ticket.c index e1e22e3..4b462be 100644 --- a/source/game/ticket.c +++ b/source/game/ticket.c @@ -105,31 +105,27 @@ u32 FindTitleKey(Ticket* ticket, u8* title_id) { // search for a titlekey inside encTitleKeys.bin / decTitleKeys.bin // when found, add it to the ticket for (u32 enc = 0; (enc <= 1) && !found; enc++) { - const char* base[] = { SUPPORT_PATHS }; - for (u32 i = 0; (i < (sizeof(base)/sizeof(char*))) && !found; i++) { - TitleKeysInfo* tikdb = (TitleKeysInfo*) (TEMP_BUFFER + (TEMP_BUFFER_SIZE/2)); - char path[64]; - FIL file; - UINT btr; - - snprintf(path, 64, "%s/%s", base[i], (enc) ? TIKDB_NAME_ENC : TIKDB_NAME_DEC); - if (f_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK) + TitleKeysInfo* tikdb = (TitleKeysInfo*) (TEMP_BUFFER + (TEMP_BUFFER_SIZE/2)); + const char* path = (enc) ? SUPPORT_PATH "/" TIKDB_NAME_ENC : SUPPORT_PATH "/" TIKDB_NAME_DEC; + FIL file; + UINT btr; + + if (f_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK) + continue; + f_read(&file, tikdb, TEMP_BUFFER_SIZE / 2, &btr); + f_close(&file); + if (tikdb->n_entries > (btr - 16) / 32) + continue; // filesize / titlekey db size mismatch + for (u32 t = 0; t < tikdb->n_entries; t++) { + TitleKeyEntry* tik = tikdb->entries + t; + if (memcmp(title_id, tik->title_id, 8) != 0) continue; - f_read(&file, tikdb, TEMP_BUFFER_SIZE / 2, &btr); - f_close(&file); - if (tikdb->n_entries > (btr - 16) / 32) - continue; // filesize / titlekey db size mismatch - for (u32 t = 0; t < tikdb->n_entries; t++) { - TitleKeyEntry* tik = tikdb->entries + t; - if (memcmp(title_id, tik->title_id, 8) != 0) - continue; - if (!enc && (CryptTitleKey(tik, true, TICKET_DEVKIT(ticket)) != 0)) // encrypt the key first - continue; - memcpy(ticket->titlekey, tik->titlekey, 16); - ticket->commonkey_idx = tik->commonkey_idx; - found = true; // found, inserted - break; - } + if (!enc && (CryptTitleKey(tik, true, TICKET_DEVKIT(ticket)) != 0)) // encrypt the key first + continue; + memcpy(ticket->titlekey, tik->titlekey, 16); + ticket->commonkey_idx = tik->commonkey_idx; + found = true; // found, inserted + break; } }