From ce156ab4e76a807d9b59d51bbd39dc01eed58b20 Mon Sep 17 00:00:00 2001 From: CTCaer Date: Tue, 20 Oct 2020 11:53:28 +0300 Subject: [PATCH] hos: Automate some eks and bis checks --- bdk/soc/hw_init.c | 15 ++++++++------- bootloader/hos/hos.c | 4 ++++ nyx/nyx_gui/config.c | 1 + nyx/nyx_gui/config.h | 1 + nyx/nyx_gui/hos/hos.c | 17 ++++++++++++----- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/bdk/soc/hw_init.c b/bdk/soc/hw_init.c index 3c346a3..0f597fe 100644 --- a/bdk/soc/hw_init.c +++ b/bdk/soc/hw_init.c @@ -56,7 +56,7 @@ extern volatile nyx_storage_t *nyx_str; * PCLK - 68MHz init (-> 136MHz -> OC/4). */ -void _config_oscillators() +static void _config_oscillators() { CLOCK(CLK_RST_CONTROLLER_SPARE_REG0) = (CLOCK(CLK_RST_CONTROLLER_SPARE_REG0) & 0xFFFFFFF3) | 4; // Set CLK_M_DIVISOR to 2. SYSCTR0(SYSCTR0_CNTFID0) = 19200000; // Set counter frequency. @@ -79,7 +79,7 @@ void _config_oscillators() CLOCK(CLK_RST_CONTROLLER_CLK_SYSTEM_RATE) = 2; // Set HCLK div to 1 and PCLK div to 3. } -void _config_gpios() +static void _config_gpios() { // Clamp inputs when tristated. APB_MISC(APB_MISC_PP_PINMUX_GLOBAL) = 0; @@ -123,14 +123,14 @@ void _config_gpios() // gpio_config(GPIO_PORT_Y, GPIO_PIN_1, GPIO_MODE_GPIO); } -void _config_pmc_scratch() +static void _config_pmc_scratch() { PMC(APBDEV_PMC_SCRATCH20) &= 0xFFF3FFFF; // Unset Debug console from Customer Option. PMC(APBDEV_PMC_SCRATCH190) &= 0xFFFFFFFE; // Unset DATA_DQ_E_IVREF EMC_PMACRO_DATA_PAD_TX_CTRL PMC(APBDEV_PMC_SECURE_SCRATCH21) |= PMC_FUSE_PRIVATEKEYDISABLE_TZ_STICKY_BIT; } -void _mbist_workaround() +static void _mbist_workaround() { // Make sure Audio clocks are enabled before accessing I2S. CLOCK(CLK_RST_CONTROLLER_CLK_OUT_ENB_V) |= BIT(CLK_V_AHUB); @@ -232,13 +232,14 @@ void _mbist_workaround() CLOCK(CLK_RST_CONTROLLER_CLK_SOURCE_NVENC) = (CLOCK(CLK_RST_CONTROLLER_CLK_SOURCE_NVENC) & 0x1FFFFFFF) | 0x80000000; // Set clock source to PLLP_OUT. } -void _config_se_brom() +static void _config_se_brom() { // Enable fuse clock. clock_enable_fuse(true); // Skip SBK/SSK if sept was run. - if (!(b_cfg.boot_cfg & BOOT_CFG_SEPT_RUN)) + bool sbk_skip = b_cfg.boot_cfg & BOOT_CFG_SEPT_RUN || FUSE(FUSE_PRIVATE_KEY0) == 0xFFFFFFFF; + if (!sbk_skip) { // Bootrom part we skipped. u32 sbk[4] = { @@ -268,7 +269,7 @@ void _config_se_brom() APB_MISC(APB_MISC_PP_STRAPPING_OPT_A) = (APB_MISC(APB_MISC_PP_STRAPPING_OPT_A) & 0xF0) | (7 << 10); } -void _config_regulators() +static void _config_regulators() { // Disable low battery shutdown monitor. max77620_low_battery_monitor_config(false); diff --git a/bootloader/hos/hos.c b/bootloader/hos/hos.c index f36e362..20db5f6 100644 --- a/bootloader/hos/hos.c +++ b/bootloader/hos/hos.c @@ -269,6 +269,10 @@ void hos_eks_save(u32 kb) u8 *keys = (u8 *)calloc(0x1000, 1); se_get_aes_keys(keys + 0x800, keys, 0x10); + // Set SBK back. + if (FUSE(FUSE_PRIVATE_KEY0) == 0xFFFFFFFF) + se_aes_key_set(14, keys + 14 * 0x10, 0x10); + // Set magic and personalized info. h_cfg.eks->magic = HOS_EKS_MAGIC; h_cfg.eks->enabled[key_idx] = kb; diff --git a/nyx/nyx_gui/config.c b/nyx/nyx_gui/config.c index a36095f..42d0226 100644 --- a/nyx/nyx_gui/config.c +++ b/nyx/nyx_gui/config.c @@ -48,6 +48,7 @@ void set_default_configuration() h_cfg.sept_run = EMC(EMC_SCRATCH0) & EMC_SEPT_RUN; h_cfg.aes_slots_new = false; h_cfg.rcm_patched = fuse_check_patched_rcm(); + h_cfg.sbk_set = FUSE(FUSE_PRIVATE_KEY0) == 0xFFFFFFFF; h_cfg.emummc_force_disable = false; sd_power_cycle_time_start = 0; diff --git a/nyx/nyx_gui/config.h b/nyx/nyx_gui/config.h index 56c044d..308b0f7 100644 --- a/nyx/nyx_gui/config.h +++ b/nyx/nyx_gui/config.h @@ -37,6 +37,7 @@ typedef struct _hekate_config bool aes_slots_new; bool emummc_force_disable; bool rcm_patched; + bool sbk_set; u32 errors; hos_eks_mbr_t *eks; } hekate_config; diff --git a/nyx/nyx_gui/hos/hos.c b/nyx/nyx_gui/hos/hos.c index ca8c9de..d4043bb 100644 --- a/nyx/nyx_gui/hos/hos.c +++ b/nyx/nyx_gui/hos/hos.c @@ -214,6 +214,10 @@ void hos_eks_save(u32 kb) u8 *keys = (u8 *)calloc(0x1000, 1); se_get_aes_keys(keys + 0x800, keys, 0x10); + // Set SBK back. + if (h_cfg.sbk_set) + se_aes_key_set(14, keys + 14 * 0x10, 0x10); + // Set magic and personalized info. h_cfg.eks->magic = HOS_EKS_MAGIC; h_cfg.eks->enabled[key_idx] = kb; @@ -659,15 +663,18 @@ void hos_bis_keys_clear() se_aes_key_clear(i); // Set SBK back. - u32 sbk[4] = { + if (!h_cfg.sbk_set) + { + u32 sbk[4] = { FUSE(FUSE_PRIVATE_KEY0), FUSE(FUSE_PRIVATE_KEY1), FUSE(FUSE_PRIVATE_KEY2), FUSE(FUSE_PRIVATE_KEY3) }; - // Set SBK to slot 14. - se_aes_key_set(14, sbk, 0x10); + // Set SBK to slot 14. + se_aes_key_set(14, sbk, 0x10); - // Lock SBK from being read. - se_key_acc_ctrl(14, SE_KEY_TBL_DIS_KEYREAD_FLAG); + // Lock SBK from being read. + se_key_acc_ctrl(14, SE_KEY_TBL_DIS_KEYREAD_FLAG); + } } \ No newline at end of file