From 4a69ea192232cb0e0967eea05c8a19bc5bf18a79 Mon Sep 17 00:00:00 2001 From: shchmue Date: Mon, 18 May 2020 15:52:19 -0600 Subject: [PATCH] Add FSS0 support --- source/hos/fss.c | 27 ---------------- source/hos/sept.c | 81 +++++++++++++++++++++++++++++++++------------- source/keys/keys.c | 10 +++++- 3 files changed, 68 insertions(+), 50 deletions(-) diff --git a/source/hos/fss.c b/source/hos/fss.c index 8e255ea..a68deeb 100644 --- a/source/hos/fss.c +++ b/source/hos/fss.c @@ -79,31 +79,6 @@ typedef struct _fss_content_t char name[0x10]; } fss_content_t; -static void _update_r2p(const char *path) -{ - char *r2p_path = malloc(256); - u32 path_len = strlen(path); - strcpy(r2p_path, path); - - while(path_len) - { - if ((r2p_path[path_len - 1] == '/') || (r2p_path[path_len - 1] == 0x5C)) - { - r2p_path[path_len] = 0; - strcat(r2p_path, "reboot_payload.bin"); - u8 *r2p_payload = sd_file_read(r2p_path, NULL); - - is_ipl_updated(r2p_payload, r2p_path, h_cfg.updater2p ? true : false); - - free(r2p_payload); - break; - } - path_len--; - } - - free(r2p_path); -} - int parse_fss(launch_ctxt_t *ctxt, const char *path, fss0_sept_t *sept_ctxt) { FIL fp; @@ -224,8 +199,6 @@ out: gfx_printf("Done!\n"); f_close(&fp); - _update_r2p(path); - return (!sept_ctxt ? 1 : sept_used); } diff --git a/source/hos/sept.c b/source/hos/sept.c index 4d4c6fa..84d0181 100644 --- a/source/hos/sept.c +++ b/source/hos/sept.c @@ -17,7 +17,9 @@ #include #include "sept.h" +#include "../config/ini.h" #include "../gfx/di.h" +#include "../hos/fss.h" #include "../hos/hos.h" #include "../libs/fatfs/ff.h" #include "../mem/heap.h" @@ -28,6 +30,7 @@ #include "../storage/nx_sd.h" #include "../storage/sdmmc.h" #include "../utils/btn.h" +#include "../utils/list.h" #include "../utils/types.h" #include "../gfx/gfx.h" @@ -65,42 +68,76 @@ extern void reloc_patcher(u32 payload_dst, u32 payload_src, u32 payload_size); int reboot_to_sept(const u8 *tsec_fw, const u32 tsec_size, const u32 kb) { FIL fp; + bool fss0_sept_used = false; // Copy warmboot reboot code and TSEC fw. memcpy((u8 *)(SEPT_PK1T_ADDR - WB_RST_SIZE), (u8 *)warmboot_reboot, sizeof(warmboot_reboot)); memcpy((void *)SEPT_PK1T_ADDR, tsec_fw, tsec_size); *(vu32 *)SEPT_TCSZ_ADDR = tsec_size; - // Copy sept-primary. - if (f_open(&fp, "sd:/sept/sept-primary.bin", FA_READ)) - goto error; + LIST_INIT(ini_sections); + if (ini_parse(&ini_sections, "bootloader/hekate_ipl.ini", false)) + { + bool found = false; + LIST_FOREACH_ENTRY(ini_sec_t, ini_sec, &ini_sections, link) + { + // Only parse non config sections. + if (ini_sec->type == INI_CHOICE && strcmp(ini_sec->name, "config")) + { + LIST_FOREACH_ENTRY(ini_kv_t, kv, &ini_sec->kvs, link) + { + if (!strcmp("fss0", kv->key)) + { + fss0_sept_t sept_ctxt; + sept_ctxt.kb = kb; + sept_ctxt.sept_primary = (void *)SEPT_STG1_ADDR; + sept_ctxt.sept_secondary = (void *)SEPT_STG2_ADDR; + fss0_sept_used = parse_fss(NULL, kv->val, &sept_ctxt); - if (f_read(&fp, (u8 *)SEPT_STG1_ADDR, f_size(&fp), NULL)) - { - f_close(&fp); - goto error; + found = true; + break; + } + } + } + if (found) + break; + } } - f_close(&fp); - // Copy sept-secondary. - if (kb < KB_FIRMWARE_VERSION_810) + + if (!fss0_sept_used) { - if (f_open(&fp, "sd:/sept/sept-secondary_00.enc", FA_READ)) - if (f_open(&fp, "sd:/sept/sept-secondary.enc", FA_READ)) // Try the deprecated version. - goto error; - } - else - { - if (f_open(&fp, "sd:/sept/sept-secondary_01.enc", FA_READ)) + // Copy sept-primary. + if (f_open(&fp, "sd:/sept/sept-primary.bin", FA_READ)) goto error; - } - if (f_read(&fp, (u8 *)SEPT_STG2_ADDR, f_size(&fp), NULL)) - { + if (f_read(&fp, (u8 *)SEPT_STG1_ADDR, f_size(&fp), NULL)) + { + f_close(&fp); + goto error; + } + f_close(&fp); + + // Copy sept-secondary. + if (kb < KB_FIRMWARE_VERSION_810) + { + if (f_open(&fp, "sd:/sept/sept-secondary_00.enc", FA_READ)) + if (f_open(&fp, "sd:/sept/sept-secondary.enc", FA_READ)) // Try the deprecated version. + goto error; + } + else + { + if (f_open(&fp, "sd:/sept/sept-secondary_01.enc", FA_READ)) + goto error; + } + + if (f_read(&fp, (u8 *)SEPT_STG2_ADDR, f_size(&fp), NULL)) + { + f_close(&fp); + goto error; + } f_close(&fp); - goto error; } - f_close(&fp); // Save auto boot config to sept payload, if any. boot_cfg_t *tmp_cfg = malloc(sizeof(boot_cfg_t)); diff --git a/source/keys/keys.c b/source/keys/keys.c index 5097a10..c29ab2c 100644 --- a/source/keys/keys.c +++ b/source/keys/keys.c @@ -304,7 +304,7 @@ get_tsec: ; break; } memcpy(master_key[kb-1], master_key[kb], 0x10); - memcpy(master_key[kb], zeros, 0x10); + memset(master_key[kb], 0, 0x10); } if (_key_exists(temp_key)) { EPRINTF("Unable to derive master key."); @@ -527,6 +527,8 @@ pkg2_done: for (u32 j = 0; j < 3; j++) { _generate_kek(8, fs_keys[2 + j], master_key[i], aes_kek_generation_source, NULL); se_aes_crypt_block_ecb(8, 0, key_area_key[j][i], aes_key_generation_source); + if (j == 2) + gfx_hexdump(i, key_area_key[j][i], 0x10); } } se_aes_key_set(8, master_key[i], 0x10); @@ -1088,8 +1090,14 @@ static void _save_key(const char *name, const void *data, u32 len, char *outbuf) static void _save_key_family(const char *name, const void *data, u32 start_key, u32 num_keys, u32 len, char *outbuf) { char temp_name[0x40] = {0}; + if (memcmp(name, "key_area_key_system", 19) == 0) { + gfx_hexdump(0, data, num_keys * 0x10); + } for (u32 i = 0; i < num_keys; i++) { sprintf(temp_name, "%s_%02x", name, i + start_key); + if (memcmp(name, "key_area_key_system", 19) == 0) { + gfx_printf("attempt save key %x\n", i); + } _save_key(temp_name, data + i * len, len, outbuf); } }