diff --git a/source/crypto/keydb.c b/source/crypto/keydb.c index 32faa09..f49c0e8 100644 --- a/source/crypto/keydb.c +++ b/source/crypto/keydb.c @@ -218,7 +218,7 @@ u32 LoadKeyFromFile(void* key, u32 keyslot, char type, char* id) return 0; } -u32 InitKeyDb( void ) +u32 InitKeyDb(const char* path) { // use this to quickly initialize all applicable keys in aeskeydb.bin static const u64 keyslot_whitelist = (1ull<<0x02)|(1ull<<0x03)|(1ull<<0x05)|(1ull<<0x18)|(1ull<<0x19)|(1ull<<0x1A)|(1ull<<0x1B)| @@ -226,7 +226,7 @@ u32 InitKeyDb( void ) // try to load aeskeydb.bin file AesKeyInfo* keydb = (AesKeyInfo*) (void*) TEMP_BUFFER; - u32 nkeys = LoadKeyDb(NULL, keydb, TEMP_BUFFER_SIZE); + u32 nkeys = LoadKeyDb(path, keydb, TEMP_BUFFER_SIZE); if (!nkeys) return 1; // apply all applicable keys @@ -234,7 +234,7 @@ u32 InitKeyDb( void ) AesKeyInfo* info = &(keydb[i]); if ((info->slot >= 0x40) || ((info->type != 'X') && (info->type != 'Y') && (info->type != 'N') && (info->type != 'I'))) return 1; // looks faulty, better stop right here - if (!((1ull<slot)&keyslot_whitelist)) continue; // not in keyslot whitelist + if (!path && !((1ull<slot)&keyslot_whitelist)) continue; // not in keyslot whitelist if ((info->type == 'I') || (*(info->id)) || (info->keyUnitType && (info->keyUnitType != GetUnitKeysType())) || (CheckKeySlot(info->slot, info->type) == 0)) continue; // most likely valid, but not applicable or already set if (info->isEncrypted) CryptAesKeyInfo(info); // decrypt key diff --git a/source/crypto/keydb.h b/source/crypto/keydb.h index 44ab194..862d523 100644 --- a/source/crypto/keydb.h +++ b/source/crypto/keydb.h @@ -21,4 +21,4 @@ typedef struct { u32 GetUnitKeysType(void); void CryptAesKeyInfo(AesKeyInfo* info); u32 LoadKeyFromFile(void* key, u32 keyslot, char type, char* id); -u32 InitKeyDb(void); +u32 InitKeyDb(const char* path); diff --git a/source/filesys/filetype.h b/source/filesys/filetype.h index 01a63c2..46e5a43 100644 --- a/source/filesys/filetype.h +++ b/source/filesys/filetype.h @@ -18,12 +18,11 @@ #define SYS_FIRM (1UL<<13) #define SYS_TICKDB (1UL<<14) #define BIN_NCCHNFO (1UL<<15) -#define BIN_LAUNCH (1UL<<16) -#define BIN_TIKDB (1UL<<17) -#define BIN_KEYDB (1UL<<18) -#define BIN_LEGKEY (1UL<<19) -#define TXT_SCRIPT (1UL<<20) -#define TXT_GENERIC (1UL<<21) +#define BIN_TIKDB (1UL<<16) +#define BIN_KEYDB (1UL<<17) +#define BIN_LEGKEY (1UL<<18) +#define TXT_SCRIPT (1UL<<19) +#define TXT_GENERIC (1UL<<20) #define TYPE_BASE 0x00FFFFFF // 24 bit reserved for base types #define FLAG_ENC (1UL<<28) @@ -47,8 +46,8 @@ #define FTYPE_RESTORABLE(tp) (tp&(IMG_NAND)) #define FTYPE_EBACKUP(tp) (tp&(IMG_NAND)) #define FTYPE_XORPAD(tp) (tp&(BIN_NCCHNFO)) +#define FTYPE_KEYINIT(tp) (tp&(BIN_KEYDB)) #define FTYPE_SCRIPT(tp) (tp&(TXT_SCRIPT)) -#define FTYPE_PAYLOAD(tp) (tp&(BIN_LAUNCH)) #define FTYPE_BOOTABLE(tp) (tp&(SYS_FIRM)) u32 IdentifyFileType(const char* path); diff --git a/source/godmode.c b/source/godmode.c index c4a6c0e..6851666 100644 --- a/source/godmode.c +++ b/source/godmode.c @@ -904,11 +904,12 @@ u32 FileHandlerMenu(char* current_path, u32* cursor, u32* scroll, DirStruct* cur bool restorable = (FTYPE_RESTORABLE(filetype) && IS_A9LH && !(drvtype & DRV_SYSNAND)); bool ebackupable = (FTYPE_EBACKUP(filetype)); bool xorpadable = (FTYPE_XORPAD(filetype)); + bool keyinitable = (FTYPE_KEYINIT(filetype)); bool scriptable = (FTYPE_SCRIPT(filetype)); bool bootable = (FTYPE_BOOTABLE(filetype) && !(drvtype & DRV_VIRTUAL)); bool special_opt = mountable || verificable || decryptable || encryptable || cia_buildable || cia_buildable_legit || cxi_dumpable || tik_buildable || key_buildable || titleinfo || renamable || transferable || hsinjectable || restorable || xorpadable || - ebackupable || bootable || scriptable; + ebackupable || keyinitable || bootable || scriptable; char pathstr[32+1]; TruncateString(pathstr, curr_entry->path, 32, 8); @@ -1063,6 +1064,7 @@ u32 FileHandlerMenu(char* current_path, u32* cursor, u32* scroll, DirStruct* cur int rename = (renamable) ? ++n_opt : -1; int xorpad = (xorpadable) ? ++n_opt : -1; int xorpad_inplace = (xorpadable) ? ++n_opt : -1; + int keyinit = (keyinitable) ? ++n_opt : -1; int boot = (bootable) ? ++n_opt : -1; int script = (scriptable) ? ++n_opt : -1; if (mount > 0) optionstr[mount-1] = "Mount image to drive"; @@ -1083,6 +1085,7 @@ u32 FileHandlerMenu(char* current_path, u32* cursor, u32* scroll, DirStruct* cur if (rename > 0) optionstr[rename-1] = "Rename file"; if (xorpad > 0) optionstr[xorpad-1] = "Build XORpads (SD output)"; if (xorpad_inplace > 0) optionstr[xorpad_inplace-1] = "Build XORpads (inplace)"; + if (keyinit > 0) optionstr[keyinit-1] = "Init " KEYDB_NAME; if (boot > 0) optionstr[boot-1] = "Boot FIRM"; if (script > 0) optionstr[script-1] = "Execute GM9 script"; @@ -1417,6 +1420,10 @@ u32 FileHandlerMenu(char* current_path, u32* cursor, u32* scroll, DirStruct* cur (success) ? "completed" : "failed!"); GetDirContents(current_dir, current_path); return 0; + } else if ((user_select == keyinit)) { + if (ShowPrompt(true, "Warning: Keys are not verified.\nContinue on your own risk?")) + ShowPrompt(false, "%s\nAESkeydb init %s", pathstr, (InitKeyDb(curr_entry->path) == 0) ? "success" : "failed"); + return 0; } else if ((user_select == boot)) { size_t firm_size = FileGetSize(curr_entry->path); if (firm_size > TEMP_BUFFER_SIZE) { diff --git a/source/nand/nand.c b/source/nand/nand.c index 9da817a..fbb6381 100644 --- a/source/nand/nand.c +++ b/source/nand/nand.c @@ -171,7 +171,7 @@ bool InitNandCrypto(bool init_full) LoadKeyFromFile(NULL, 0x24, 'Y', NULL); // part #5: FULL INIT - if (init_full) InitKeyDb(); + if (init_full) InitKeyDb(NULL); return true; }