nyx: hos: deduplicate cal0 dumping

This commit is contained in:
CTCaer 2023-07-28 03:28:21 +03:00
parent 317abb2f4e
commit 21da947c02
4 changed files with 57 additions and 56 deletions

View file

@ -33,8 +33,6 @@ extern volatile nyx_storage_t *nyx_str;
extern lv_res_t launch_payload(lv_obj_t *list); extern lv_res_t launch_payload(lv_obj_t *list);
extern char *emmcsn_path_impl(char *path, char *sub_dir, char *filename, sdmmc_storage_t *storage); extern char *emmcsn_path_impl(char *path, char *sub_dir, char *filename, sdmmc_storage_t *storage);
u8 *cal0_buf = NULL;
static lv_res_t _create_window_dump_done(int error, char *dump_filenames) static lv_res_t _create_window_dump_done(int error, char *dump_filenames)
{ {
lv_obj_t *dark_bg = lv_obj_create(lv_scr_act(), NULL); lv_obj_t *dark_bg = lv_obj_create(lv_scr_act(), NULL);
@ -237,55 +235,6 @@ static lv_res_t _kfuse_dump_window_action(lv_obj_t * btn)
return LV_RES_OK; return LV_RES_OK;
} }
int dump_cal0()
{
// Init eMMC.
if (!emmc_initialize(false))
return 1;
// Generate BIS keys
hos_bis_keygen();
if (!cal0_buf)
cal0_buf = malloc(SZ_64K);
// Read and decrypt CAL0.
emmc_set_partition(EMMC_GPP);
LIST_INIT(gpt);
emmc_gpt_parse(&gpt);
emmc_part_t *cal0_part = emmc_part_find(&gpt, "PRODINFO"); // check if null
nx_emmc_bis_init(cal0_part, false, 0);
nx_emmc_bis_read(0, 0x40, cal0_buf);
nx_emmc_bis_end();
emmc_gpt_free(&gpt);
emmc_end();
// Clear BIS keys slots.
hos_bis_keys_clear();
nx_emmc_cal0_t *cal0 = (nx_emmc_cal0_t *)cal0_buf;
// Check keys validity.
if (memcmp(&cal0->magic, "CAL0", 4))
{
free(cal0_buf);
cal0_buf = NULL;
// Clear EKS keys.
hos_eks_clear(KB_FIRMWARE_VERSION_MAX);
return 2;
}
u32 hash[8];
se_calc_sha256_oneshot(hash, (u8 *)cal0 + 0x40, cal0->body_size);
if (memcmp(hash, cal0->body_sha256, 0x20))
return 3;
return 0;
}
static lv_res_t _create_mbox_cal0(lv_obj_t *btn) static lv_res_t _create_mbox_cal0(lv_obj_t *btn)
{ {
lv_obj_t *dark_bg = lv_obj_create(lv_scr_act(), NULL); lv_obj_t *dark_bg = lv_obj_create(lv_scr_act(), NULL);
@ -311,7 +260,7 @@ static lv_res_t _create_mbox_cal0(lv_obj_t *btn)
sd_mount(); sd_mount();
// Dump CAL0. // Dump CAL0.
int cal0_res = dump_cal0(); int cal0_res = hos_dump_cal0();
// Check result. Don't error if hash doesn't match. // Check result. Don't error if hash doesn't match.
if (cal0_res == 1) if (cal0_res == 1)

View file

@ -29,7 +29,6 @@
extern hekate_config h_cfg; extern hekate_config h_cfg;
extern nyx_config n_cfg; extern nyx_config n_cfg;
extern u8 *cal0_buf;
static lv_obj_t *autoboot_btn; static lv_obj_t *autoboot_btn;
static bool autoboot_first_time = true; static bool autoboot_first_time = true;
@ -863,7 +862,7 @@ static lv_res_t _joycon_info_dump_action(lv_obj_t * btn)
if (nx_hoag) if (nx_hoag)
{ {
error = dump_cal0(); error = hos_dump_cal0();
if (!error) if (!error)
goto save_data; goto save_data;
} }
@ -1044,7 +1043,7 @@ disabled:;
{ {
s_printf(txt_buf, s_printf(txt_buf,
"Dumping to SD card finished!\n" "Dumping to SD card finished!\n"
"Saved to: #C7EA46 switchroot/lite_gamepad.cal#\n\n"); "Saved to: #C7EA46 switchroot/switch.cal#\n\n");
strcat(txt_buf, "#C7EA46 Success!#\n#C7EA46 Found Lite Gamepad data!#\n"); strcat(txt_buf, "#C7EA46 Success!#\n#C7EA46 Found Lite Gamepad data!#\n");
} }
} }

View file

@ -27,6 +27,7 @@
extern hekate_config h_cfg; extern hekate_config h_cfg;
u8 *cal0_buf = NULL;
static u8 *bis_keys = NULL; static u8 *bis_keys = NULL;
typedef struct _tsec_keys_t typedef struct _tsec_keys_t
@ -668,3 +669,52 @@ void hos_bis_keys_clear()
for (u32 i = 0; i < 6; i++) for (u32 i = 0; i < 6; i++)
se_aes_key_clear(i); se_aes_key_clear(i);
} }
int hos_dump_cal0()
{
// Init eMMC.
if (!emmc_initialize(false))
return 1;
// Generate BIS keys
hos_bis_keygen();
if (!cal0_buf)
cal0_buf = malloc(SZ_64K);
// Read and decrypt CAL0.
emmc_set_partition(EMMC_GPP);
LIST_INIT(gpt);
emmc_gpt_parse(&gpt);
emmc_part_t *cal0_part = emmc_part_find(&gpt, "PRODINFO"); // check if null
nx_emmc_bis_init(cal0_part, false, 0);
nx_emmc_bis_read(0, 0x40, cal0_buf);
nx_emmc_bis_end();
emmc_gpt_free(&gpt);
emmc_end();
// Clear BIS keys slots.
hos_bis_keys_clear();
nx_emmc_cal0_t *cal0 = (nx_emmc_cal0_t *)cal0_buf;
// Check keys validity.
if (memcmp(&cal0->magic, "CAL0", 4))
{
free(cal0_buf);
cal0_buf = NULL;
// Clear EKS keys.
hos_eks_clear(KB_FIRMWARE_VERSION_MAX);
return 2;
}
u32 hash[8];
se_calc_sha256_oneshot(hash, (u8 *)cal0 + 0x40, cal0->body_size);
if (memcmp(hash, cal0->body_sha256, 0x20))
return 3;
return 0;
}

View file

@ -86,9 +86,12 @@ typedef struct _launch_ctxt_t
ini_sec_t *cfg; ini_sec_t *cfg;
} launch_ctxt_t; } launch_ctxt_t;
extern u8 *cal0_buf;
void hos_eks_clear(u32 kb); void hos_eks_clear(u32 kb);
int hos_keygen(void *keyblob, u32 kb, tsec_ctxt_t *tsec_ctxt); int hos_keygen(void *keyblob, u32 kb, tsec_ctxt_t *tsec_ctxt);
int hos_bis_keygen(); int hos_bis_keygen();
void hos_bis_keys_clear(); void hos_bis_keys_clear();
int hos_dump_cal0();
#endif #endif