Various small fixes

This commit is contained in:
CTCaer 2020-05-05 19:11:39 +03:00
parent a6be3714a3
commit 822e0dcd98
11 changed files with 154 additions and 79 deletions

View file

@ -39,6 +39,7 @@
#include "../soc/smmu.h"
#include "../soc/t210.h"
#include "../storage/emummc.h"
#include "../storage/mbr_gpt.h"
#include "../storage/nx_emmc.h"
#include "../storage/nx_sd.h"
#include "../storage/sdmmc.h"
@ -51,8 +52,7 @@ extern hekate_config h_cfg;
#define DPRINTF(...)
#define EHPRINTFARGS(text, args...) \
({ display_backlight_brightness(h_cfg.backlight, 1000); \
gfx_con.mute = false; \
({ gfx_con.mute = false; \
gfx_printf("%k"text"%k\n", 0xFFFF0000, args, 0xFFCCCCCC); })
#define PKG2_LOAD_ADDR 0xA9800000
@ -103,8 +103,6 @@ static void _hos_crit_error(const char *text)
{
gfx_con.mute = false;
gfx_printf("%k%s%k\n", 0xFFFF0000, text, 0xFFCCCCCC);
display_backlight_brightness(h_cfg.backlight, 1000);
}
static void _se_lock(bool lock_se)
@ -183,15 +181,42 @@ void _sysctr0_reset()
SYSCTR0(SYSCTR0_COUNTERID11) = 0;
}
bool hos_eks_rw_try(u8 *buf, bool write)
{
mbr_t *mbr = (mbr_t *)buf;
for (u32 i = 0; i < 3; i++)
{
if (!write)
{
if (sdmmc_storage_read(&sd_storage, 0, 1, mbr))
{
if (mbr->partitions[0].status != 0xFF &&
mbr->partitions[0].start_sct &&
mbr->partitions[0].size_sct)
return true;
else
return false;
}
}
else
{
if (sdmmc_storage_write(&sd_storage, 0, 1, mbr))
return true;
}
}
return false;
}
void hos_eks_get()
{
// Check if EKS already found and parsed.
if (!h_cfg.eks)
{
u8 *mbr = calloc(512 , 1);
// Read EKS blob.
sdmmc_storage_read(&sd_storage, 0, 1, mbr);
u8 *mbr = calloc(512 , 1);
if (!hos_eks_rw_try(mbr, false))
goto out;
// Decrypt EKS blob.
hos_eks_mbr_t *eks = (hos_eks_mbr_t *)(mbr + 0x10);
@ -208,6 +233,7 @@ void hos_eks_get()
return;
}
out:
free(mbr);
}
}
@ -221,12 +247,29 @@ void hos_eks_save(u32 kb)
if (key_idx > 5)
return;
bool new_eks = false;
if (!h_cfg.eks)
{
h_cfg.eks = calloc(512 , 1);
new_eks = true;
}
// If matching blob doesn't exist, create it.
if (!(h_cfg.eks->enabled & (1 << key_idx)))
{
// Read EKS blob.
u8 *mbr = calloc(512 , 1);
if (!hos_eks_rw_try(mbr, false))
{
if (new_eks)
{
free(h_cfg.eks);
h_cfg.eks = NULL;
}
goto out;
}
// Get keys.
u8 *keys = (u8 *)calloc(0x1000, 1);
se_get_aes_keys(keys + 0x800, keys, 0x10);
@ -244,20 +287,21 @@ void hos_eks_save(u32 kb)
memcpy(h_cfg.eks->keys[key_idx].fdk, keys + 13 * 0x10, 0x10);
memcpy(h_cfg.eks->keys[key_idx].dkk, keys + 15 * 0x10, 0x10);
// Encrypt EKS.
// Encrypt EKS blob.
u8 *eks = calloc(512 , 1);
memcpy(eks, h_cfg.eks, sizeof(hos_eks_mbr_t));
se_aes_crypt_ecb(14, 1, eks, sizeof(hos_eks_mbr_t), eks, sizeof(hos_eks_mbr_t));
// Write EKS to SD.
u8 *mbr = calloc(512 , 1);
sdmmc_storage_read(&sd_storage, 0, 1, mbr);
// Write EKS blob to SD.
memset(mbr, 0, 0x10);
memcpy(mbr + 0x10, eks, sizeof(hos_eks_mbr_t));
sdmmc_storage_write(&sd_storage, 0, 1, mbr);
hos_eks_rw_try(mbr, true);
free(eks);
free(mbr);
free(keys);
out:
free(mbr);
}
}
}
@ -270,21 +314,25 @@ void hos_eks_clear(u32 kb)
u8 key_idx = kb - KB_FIRMWARE_VERSION_700;
if (h_cfg.eks->enabled & (1 << key_idx))
{
// Read EKS blob.
u8 *mbr = calloc(512 , 1);
if (!hos_eks_rw_try(mbr, false))
goto out;
// Disable current Master key version.
h_cfg.eks->enabled &= ~(1 << key_idx);
// Encrypt EKS.
// Encrypt EKS blob.
u8 *eks = calloc(512 , 1);
memcpy(eks, h_cfg.eks, sizeof(hos_eks_mbr_t));
se_aes_crypt_ecb(14, 1, eks, sizeof(hos_eks_mbr_t), eks, sizeof(hos_eks_mbr_t));
// Write EKS to SD.
u8 *mbr = calloc(512 , 1);
sdmmc_storage_read(&sd_storage, 0, 1, mbr);
// Write EKS blob to SD.
memcpy(mbr + 0x10, eks, sizeof(hos_eks_mbr_t));
sdmmc_storage_write(&sd_storage, 0, 1, mbr);
hos_eks_rw_try(mbr, true);
free(eks);
out:
free(mbr);
}
}
@ -477,8 +525,8 @@ static int _read_emmc_pkg1(launch_ctxt_t *ctxt)
if (!ctxt->pkg1_id)
{
_hos_crit_error("Unknown pkg1 version.");
EHPRINTFARGS("%sNot yet supported HOS version!",
(emu_cfg.enabled && !h_cfg.emummc_force_disable) ? "Is emuMMC corrupt?\nOr " : "");
EHPRINTFARGS("HOS version not supported!%s",
(emu_cfg.enabled && !h_cfg.emummc_force_disable) ? "\nOr emuMMC corrupt!" : "");
goto out;
}
gfx_printf("Identified pkg1 and Keyblob %d\n\n", ctxt->pkg1_id->kb);
@ -489,7 +537,7 @@ static int _read_emmc_pkg1(launch_ctxt_t *ctxt)
res = 1;
out:;
out:
sdmmc_storage_end(&storage);
return res;
}
@ -731,7 +779,10 @@ int hos_launch(ini_sec_t *cfg)
// Read package2.
u8 *bootConfigBuf = _read_emmc_pkg2(&ctxt);
if (!bootConfigBuf)
{
_hos_crit_error("Pkg2 read failed!");
return 0;
}
gfx_printf("Read pkg2\n");

View file

@ -68,7 +68,7 @@ typedef struct _hos_eks_mbr_t
u32 rsvd2[3];
} hos_eks_mbr_t;
static_assert(sizeof(hos_eks_mbr_t) < 424, "HOS EKS storage bigger than MBR!");
static_assert(sizeof(hos_eks_mbr_t) == 416, "HOS EKS storage bigger than MBR!");
typedef struct _launch_ctxt_t
{

View file

@ -136,7 +136,7 @@ void *malloc(u32 size)
void *calloc(u32 num, u32 size)
{
void *res = (void *)_heap_alloc(&_heap, num * size);
memset(res, 0, num * size);
memset(res, 0, ALIGN(num * size, sizeof(hnode_t))); // Clear the aligned size.
return res;
}
@ -164,7 +164,7 @@ void heap_monitor(heap_monitor_t *mon, bool print_node_stats)
count, node->used, (u32)node + sizeof(hnode_t), node->size);
count++;
if (node->next)
node = node->next;
else

View file

@ -39,7 +39,7 @@ typedef struct _mbr_part_t
typedef struct _mbr_t
{
u8 bootstrap[0x1B8];
u8 bootstrap[440];
u32 signature;
u16 copy_protected;
mbr_part_t partitions[4];

View file

@ -49,15 +49,8 @@ extern void emmcsn_path_impl(char *path, char *sub_dir, char *filename, sdmmc_st
static lv_res_t _create_window_dump_done(int error, char *dump_filenames)
{
lv_style_t *darken;
darken = (lv_style_t *)malloc(sizeof(lv_style_t));
lv_style_copy(darken, &lv_style_plain);
darken->body.main_color = LV_COLOR_BLACK;
darken->body.grad_color = darken->body.main_color;
darken->body.opa = LV_OPA_30;
lv_obj_t *dark_bg = lv_obj_create(lv_scr_act(), NULL);
lv_obj_set_style(dark_bg, darken);
lv_obj_set_style(dark_bg, &mbox_darken);
lv_obj_set_size(dark_bg, LV_HOR_RES, LV_VER_RES);
static const char * mbox_btn_map[] = { "\211", "\222OK", "\211", "" };
@ -705,15 +698,8 @@ static lv_res_t _create_mbox_benchmark(bool sd_bench)
sdmmc_storage_t emmc_storage;
sdmmc_storage_t *storage;
lv_style_t *darken;
darken = (lv_style_t *)malloc(sizeof(lv_style_t));
lv_style_copy(darken, &lv_style_plain);
darken->body.main_color = LV_COLOR_BLACK;
darken->body.grad_color = darken->body.main_color;
darken->body.opa = LV_OPA_30;
lv_obj_t *dark_bg = lv_obj_create(lv_scr_act(), NULL);
lv_obj_set_style(dark_bg, darken);
lv_obj_set_style(dark_bg, &mbox_darken);
lv_obj_set_size(dark_bg, LV_HOR_RES, LV_VER_RES);
static const char * mbox_btn_map[] = { "\211", "\222OK", "\211", "" };
@ -1134,7 +1120,7 @@ static lv_res_t _create_window_sdcard_info_status(lv_obj_t *btn)
lv_obj_set_width(lb_desc4, lv_obj_get_width(desc4));
lv_label_set_text(lb_desc4,
"#00DDFF SDMMC1 Error Counts:#\n"
"#00DDFF SDMMC1 Errors:#\n"
"Init fails:\n"
"Read/Write fails:\n"
"Read/Write errors:"

View file

@ -531,15 +531,8 @@ static lv_res_t _create_mbox_clock_edit_action(lv_obj_t * btns, const char * txt
static lv_res_t _create_mbox_clock_edit(lv_obj_t *btn)
{
lv_style_t *darken;
darken = malloc(sizeof(lv_style_t));
lv_style_copy(darken, &lv_style_plain);
darken->body.main_color = LV_COLOR_BLACK;
darken->body.grad_color = darken->body.main_color;
darken->body.opa = LV_OPA_30;
lv_obj_t *dark_bg = lv_obj_create(lv_scr_act(), NULL);
lv_obj_set_style(dark_bg, darken);
lv_obj_set_style(dark_bg, &mbox_darken);
lv_obj_set_size(dark_bg, LV_HOR_RES, LV_VER_RES);
static const char * mbox_btn_map[] = { "\211", "\222Done", "\222Cancel", "\211", "" };
@ -626,15 +619,8 @@ static lv_res_t _joycon_info_dump_action(lv_obj_t * btn)
sd_unmount(false);
}
lv_style_t *darken;
darken = (lv_style_t *)malloc(sizeof(lv_style_t));
lv_style_copy(darken, &lv_style_plain);
darken->body.main_color = LV_COLOR_BLACK;
darken->body.grad_color = darken->body.main_color;
darken->body.opa = LV_OPA_30;
lv_obj_t *dark_bg = lv_obj_create(lv_scr_act(), NULL);
lv_obj_set_style(dark_bg, darken);
lv_obj_set_style(dark_bg, &mbox_darken);
lv_obj_set_size(dark_bg, LV_HOR_RES, LV_VER_RES);
static const char * mbox_btn_map[] = { "\211", "\222OK", "\211", "" };

View file

@ -1649,8 +1649,8 @@ void create_tab_tools(lv_theme_t *th, lv_obj_t *parent)
lv_tabview_set_sliding(tv, false);
lv_tabview_set_btns_pos(tv, LV_TABVIEW_BTNS_POS_BOTTOM);
lv_obj_t *tab1= lv_tabview_add_tab(tv, "eMMC "SYMBOL_DOT" Dump Pkg1/2 "SYMBOL_DOT" USB Tools");
lv_obj_t *tab2 = lv_tabview_add_tab(tv, "Archive bit "SYMBOL_DOT" AutoRCM "SYMBOL_DOT" Touch Tuning");
lv_obj_t *tab1= lv_tabview_add_tab(tv, "eMMC "SYMBOL_DOT" Pkg1/2 "SYMBOL_DOT" USB Tools");
lv_obj_t *tab2 = lv_tabview_add_tab(tv, "Arch bit "SYMBOL_DOT" RCM "SYMBOL_DOT" Touch "SYMBOL_DOT" Partitions");
lv_obj_t *line_sep = lv_line_create(tv, NULL);
static const lv_point_t line_pp[] = { {0, 0}, { 0, LV_DPI / 4} };

View file

@ -2,7 +2,7 @@
* Copyright (c) 2018 naehrwert
* Copyright (c) 2018 st4rk
* Copyright (c) 2018 Ced2911
* Copyright (c) 2018-2019 CTCaer
* Copyright (c) 2018-2020 CTCaer
* Copyright (c) 2018 balika011
*
* This program is free software; you can redistribute it and/or modify it
@ -36,6 +36,7 @@
#include "../soc/pmc.h"
#include "../soc/smmu.h"
#include "../soc/t210.h"
#include "../storage/mbr_gpt.h"
#include "../storage/nx_emmc.h"
#include "../storage/nx_sd.h"
#include "../storage/sdmmc.h"
@ -90,15 +91,42 @@ static const u8 master_keyseed_620[0x10] =
static const u8 console_keyseed_4xx_5xx[0x10] =
{ 0x0C, 0x91, 0x09, 0xDB, 0x93, 0x93, 0x07, 0x81, 0x07, 0x3C, 0xC4, 0x16, 0x22, 0x7C, 0x6C, 0x28 };
bool hos_eks_rw_try(u8 *buf, bool write)
{
mbr_t *mbr = (mbr_t *)buf;
for (u32 i = 0; i < 3; i++)
{
if (!write)
{
if (sdmmc_storage_read(&sd_storage, 0, 1, mbr))
{
if (mbr->partitions[0].status != 0xFF &&
mbr->partitions[0].start_sct &&
mbr->partitions[0].size_sct)
return true;
else
return false;
}
}
else
{
if (sdmmc_storage_write(&sd_storage, 0, 1, mbr))
return true;
}
}
return false;
}
void hos_eks_get()
{
// Check if EKS already found and parsed.
if (!h_cfg.eks)
{
u8 *mbr = calloc(512 , 1);
// Read EKS blob.
sdmmc_storage_read(&sd_storage, 0, 1, mbr);
u8 *mbr = calloc(512 , 1);
if (!hos_eks_rw_try(mbr, false))
goto out;
// Decrypt EKS blob.
hos_eks_mbr_t *eks = (hos_eks_mbr_t *)(mbr + 0x10);
@ -115,6 +143,7 @@ void hos_eks_get()
return;
}
out:
free(mbr);
}
}
@ -128,12 +157,29 @@ void hos_eks_save(u32 kb)
if (key_idx > 5)
return;
bool new_eks = false;
if (!h_cfg.eks)
{
h_cfg.eks = calloc(512 , 1);
new_eks = true;
}
// If matching blob doesn't exist, create it.
if (!(h_cfg.eks->enabled & (1 << key_idx)))
{
// Read EKS blob.
u8 *mbr = calloc(512 , 1);
if (!hos_eks_rw_try(mbr, false))
{
if (new_eks)
{
free(h_cfg.eks);
h_cfg.eks = NULL;
}
goto out;
}
// Get keys.
u8 *keys = (u8 *)calloc(0x1000, 1);
se_get_aes_keys(keys + 0x800, keys, 0x10);
@ -151,20 +197,21 @@ void hos_eks_save(u32 kb)
memcpy(h_cfg.eks->keys[key_idx].fdk, keys + 13 * 0x10, 0x10);
memcpy(h_cfg.eks->keys[key_idx].dkk, keys + 15 * 0x10, 0x10);
// Encrypt EKS.
// Encrypt EKS blob.
u8 *eks = calloc(512 , 1);
memcpy(eks, h_cfg.eks, sizeof(hos_eks_mbr_t));
se_aes_crypt_ecb(14, 1, eks, sizeof(hos_eks_mbr_t), eks, sizeof(hos_eks_mbr_t));
// Write EKS to SD.
u8 *mbr = calloc(512 , 1);
sdmmc_storage_read(&sd_storage, 0, 1, mbr);
// Write EKS blob to SD.
memset(mbr, 0, 0x10);
memcpy(mbr + 0x10, eks, sizeof(hos_eks_mbr_t));
sdmmc_storage_write(&sd_storage, 0, 1, mbr);
hos_eks_rw_try(mbr, true);
free(eks);
free(mbr);
free(keys);
out:
free(mbr);
}
}
}
@ -177,25 +224,30 @@ void hos_eks_clear(u32 kb)
u8 key_idx = kb - KB_FIRMWARE_VERSION_700;
if (h_cfg.eks->enabled & (1 << key_idx))
{
// Read EKS blob.
u8 *mbr = calloc(512 , 1);
if (!hos_eks_rw_try(mbr, false))
goto out;
// Disable current Master key version.
h_cfg.eks->enabled &= ~(1 << key_idx);
// Encrypt EKS.
// Encrypt EKS blob.
u8 *eks = calloc(512 , 1);
memcpy(eks, h_cfg.eks, sizeof(hos_eks_mbr_t));
se_aes_crypt_ecb(14, 1, eks, sizeof(hos_eks_mbr_t), eks, sizeof(hos_eks_mbr_t));
// Write EKS to SD.
u8 *mbr = calloc(512 , 1);
sdmmc_storage_read(&sd_storage, 0, 1, mbr);
// Write EKS blob to SD.
memcpy(mbr + 0x10, eks, sizeof(hos_eks_mbr_t));
sdmmc_storage_write(&sd_storage, 0, 1, mbr);
hos_eks_rw_try(mbr, true);
free(eks);
out:
free(mbr);
}
}
}
int hos_keygen(u8 *keyblob, u32 kb, tsec_ctxt_t *tsec_ctxt)
{
u8 tmp[0x20];

View file

@ -68,7 +68,7 @@ typedef struct _hos_eks_mbr_t
u32 rsvd2[3];
} hos_eks_mbr_t;
static_assert(sizeof(hos_eks_mbr_t) < 424, "HOS EKS storage bigger than MBR!");
static_assert(sizeof(hos_eks_mbr_t) == 416, "HOS EKS storage bigger than MBR!");
typedef struct _launch_ctxt_t
{

View file

@ -136,7 +136,7 @@ void *malloc(u32 size)
void *calloc(u32 num, u32 size)
{
void *res = (void *)_heap_alloc(&_heap, num * size);
memset(res, 0, num * size);
memset(res, 0, ALIGN(num * size, sizeof(hnode_t))); // Clear the aligned size.
return res;
}
@ -164,7 +164,7 @@ void heap_monitor(heap_monitor_t *mon, bool print_node_stats)
count, node->used, (u32)node + sizeof(hnode_t), node->size);
count++;
if (node->next)
node = node->next;
else

View file

@ -39,7 +39,7 @@ typedef struct _mbr_part_t
typedef struct _mbr_t
{
u8 bootstrap[0x1B8];
u8 bootstrap[440];
u32 signature;
u16 copy_protected;
mbr_part_t partitions[4];