sdmmc: Use global emmc storage in various places

This commit is contained in:
CTCaer 2021-02-06 03:19:42 +02:00
parent a80cc0ae2c
commit 8cd438146d
12 changed files with 138 additions and 167 deletions

View file

@ -131,10 +131,7 @@ void print_mmc_info()
static const u32 SECTORS_TO_MIB_COEFF = 11; static const u32 SECTORS_TO_MIB_COEFF = 11;
sdmmc_storage_t storage; if (!sdmmc_storage_init_mmc(&emmc_storage, &emmc_sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400))
sdmmc_t sdmmc;
if (!sdmmc_storage_init_mmc(&storage, &sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400))
{ {
EPRINTF("Failed to init eMMC."); EPRINTF("Failed to init eMMC.");
goto out; goto out;
@ -145,7 +142,7 @@ void print_mmc_info()
u32 speed = 0; u32 speed = 0;
gfx_printf("%kCID:%k\n", 0xFF00DDFF, 0xFFCCCCCC); gfx_printf("%kCID:%k\n", 0xFF00DDFF, 0xFFCCCCCC);
switch (storage.csd.mmca_vsn) switch (emmc_storage.csd.mmca_vsn)
{ {
case 2: /* MMC v2.0 - v2.2 */ case 2: /* MMC v2.0 - v2.2 */
case 3: /* MMC v3.1 - v3.3 */ case 3: /* MMC v3.1 - v3.3 */
@ -158,22 +155,22 @@ void print_mmc_info()
" Prd Rev: %X\n" " Prd Rev: %X\n"
" S/N: %04X\n" " S/N: %04X\n"
" Month/Year: %02d/%04d\n\n", " Month/Year: %02d/%04d\n\n",
storage.cid.manfid, storage.cid.card_bga, storage.cid.oemid, emmc_storage.cid.manfid, emmc_storage.cid.card_bga, emmc_storage.cid.oemid,
storage.cid.prod_name[0], storage.cid.prod_name[1], storage.cid.prod_name[2], emmc_storage.cid.prod_name[0], emmc_storage.cid.prod_name[1], emmc_storage.cid.prod_name[2],
storage.cid.prod_name[3], storage.cid.prod_name[4], storage.cid.prod_name[5], emmc_storage.cid.prod_name[3], emmc_storage.cid.prod_name[4], emmc_storage.cid.prod_name[5],
storage.cid.prv, storage.cid.serial, storage.cid.month, storage.cid.year); emmc_storage.cid.prv, emmc_storage.cid.serial, emmc_storage.cid.month, emmc_storage.cid.year);
break; break;
default: default:
break; break;
} }
if (storage.csd.structure == 0) if (emmc_storage.csd.structure == 0)
EPRINTF("Unknown CSD structure."); EPRINTF("Unknown CSD structure.");
else else
{ {
gfx_printf("%kExtended CSD V1.%d:%k\n", gfx_printf("%kExtended CSD V1.%d:%k\n",
0xFF00DDFF, storage.ext_csd.ext_struct, 0xFFCCCCCC); 0xFF00DDFF, emmc_storage.ext_csd.ext_struct, 0xFFCCCCCC);
card_type = storage.ext_csd.card_type; card_type = emmc_storage.ext_csd.card_type;
char card_type_support[96]; char card_type_support[96];
card_type_support[0] = 0; card_type_support[0] = 0;
if (card_type & EXT_CSD_CARD_TYPE_HS_26) if (card_type & EXT_CSD_CARD_TYPE_HS_26)
@ -211,16 +208,16 @@ void print_mmc_info()
" Max Rate: %d MB/s (%d MHz)\n" " Max Rate: %d MB/s (%d MHz)\n"
" Current Rate: %d MB/s\n" " Current Rate: %d MB/s\n"
" Type Support: ", " Type Support: ",
storage.csd.mmca_vsn, storage.ext_csd.rev, storage.ext_csd.dev_version, storage.csd.cmdclass, emmc_storage.csd.mmca_vsn, emmc_storage.ext_csd.rev, emmc_storage.ext_csd.dev_version, emmc_storage.csd.cmdclass,
storage.csd.capacity == (4096 * 512) ? "High" : "Low", speed & 0xFFFF, (speed >> 16) & 0xFFFF, emmc_storage.csd.capacity == (4096 * 512) ? "High" : "Low", speed & 0xFFFF, (speed >> 16) & 0xFFFF,
storage.csd.busspeed); emmc_storage.csd.busspeed);
gfx_con.fntsz = 8; gfx_con.fntsz = 8;
gfx_printf("%s", card_type_support); gfx_printf("%s", card_type_support);
gfx_con.fntsz = 16; gfx_con.fntsz = 16;
gfx_printf("\n\n", card_type_support); gfx_printf("\n\n", card_type_support);
u32 boot_size = storage.ext_csd.boot_mult << 17; u32 boot_size = emmc_storage.ext_csd.boot_mult << 17;
u32 rpmb_size = storage.ext_csd.rpmb_mult << 17; u32 rpmb_size = emmc_storage.ext_csd.rpmb_mult << 17;
gfx_printf("%keMMC Partitions:%k\n", 0xFF00DDFF, 0xFFCCCCCC); gfx_printf("%keMMC Partitions:%k\n", 0xFF00DDFF, 0xFFCCCCCC);
gfx_printf(" 1: %kBOOT0 %k\n Size: %5d KiB (LBA Sectors: 0x%07X)\n", 0xFF96FF00, 0xFFCCCCCC, gfx_printf(" 1: %kBOOT0 %k\n Size: %5d KiB (LBA Sectors: 0x%07X)\n", 0xFF96FF00, 0xFFCCCCCC,
boot_size / 1024, boot_size / 512); boot_size / 1024, boot_size / 512);
@ -232,13 +229,13 @@ void print_mmc_info()
rpmb_size / 1024, rpmb_size / 512); rpmb_size / 1024, rpmb_size / 512);
gfx_put_small_sep(); gfx_put_small_sep();
gfx_printf(" 0: %kGPP (USER) %k\n Size: %5d MiB (LBA Sectors: 0x%07X)\n\n", 0xFF96FF00, 0xFFCCCCCC, gfx_printf(" 0: %kGPP (USER) %k\n Size: %5d MiB (LBA Sectors: 0x%07X)\n\n", 0xFF96FF00, 0xFFCCCCCC,
storage.sec_cnt >> SECTORS_TO_MIB_COEFF, storage.sec_cnt); emmc_storage.sec_cnt >> SECTORS_TO_MIB_COEFF, emmc_storage.sec_cnt);
gfx_put_small_sep(); gfx_put_small_sep();
gfx_printf("%kGPP (eMMC USER) partition table:%k\n", 0xFF00DDFF, 0xFFCCCCCC); gfx_printf("%kGPP (eMMC USER) partition table:%k\n", 0xFF00DDFF, 0xFFCCCCCC);
sdmmc_storage_set_mmc_partition(&storage, EMMC_GPP); sdmmc_storage_set_mmc_partition(&emmc_storage, EMMC_GPP);
LIST_INIT(gpt); LIST_INIT(gpt);
nx_emmc_gpt_parse(&gpt, &storage); nx_emmc_gpt_parse(&gpt, &emmc_storage);
int gpp_idx = 0; int gpp_idx = 0;
LIST_FOREACH_ENTRY(emmc_part_t, part, &gpt, link) LIST_FOREACH_ENTRY(emmc_part_t, part, &gpt, link)
{ {
@ -252,7 +249,7 @@ void print_mmc_info()
} }
out: out:
sdmmc_storage_end(&storage); sdmmc_storage_end(&emmc_storage);
btn_wait(); btn_wait();
} }
@ -338,16 +335,14 @@ void print_tsec_key()
u32 retries = 0; u32 retries = 0;
tsec_ctxt_t tsec_ctxt; tsec_ctxt_t tsec_ctxt;
sdmmc_storage_t storage;
sdmmc_t sdmmc;
sdmmc_storage_init_mmc(&storage, &sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400); sdmmc_storage_init_mmc(&emmc_storage, &emmc_sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400);
// Read package1. // Read package1.
u8 *pkg1 = (u8 *)malloc(0x40000); u8 *pkg1 = (u8 *)malloc(0x40000);
sdmmc_storage_set_mmc_partition(&storage, EMMC_BOOT0); sdmmc_storage_set_mmc_partition(&emmc_storage, EMMC_BOOT0);
sdmmc_storage_read(&storage, 0x100000 / NX_EMMC_BLOCKSIZE, 0x40000 / NX_EMMC_BLOCKSIZE, pkg1); sdmmc_storage_read(&emmc_storage, 0x100000 / NX_EMMC_BLOCKSIZE, 0x40000 / NX_EMMC_BLOCKSIZE, pkg1);
sdmmc_storage_end(&storage); sdmmc_storage_end(&emmc_storage);
const pkg1_id_t *pkg1_id = pkg1_identify(pkg1); const pkg1_id_t *pkg1_id = pkg1_identify(pkg1);
if (!pkg1_id) if (!pkg1_id)
{ {

View file

@ -66,23 +66,21 @@ void dump_packages12()
gfx_clear_partial_grey(0x1B, 0, 1256); gfx_clear_partial_grey(0x1B, 0, 1256);
gfx_con_setpos(0, 0); gfx_con_setpos(0, 0);
sdmmc_storage_t storage; if (!sdmmc_storage_init_mmc(&emmc_storage, &emmc_sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400))
sdmmc_t sdmmc;
if (!sdmmc_storage_init_mmc(&storage, &sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400))
{ {
EPRINTF("Failed to init eMMC."); EPRINTF("Failed to init eMMC.");
goto out_free; goto out_free;
} }
sdmmc_storage_set_mmc_partition(&storage, EMMC_BOOT0); sdmmc_storage_set_mmc_partition(&emmc_storage, EMMC_BOOT0);
// Read package1. // Read package1.
sdmmc_storage_read(&storage, 0x100000 / NX_EMMC_BLOCKSIZE, 0x40000 / NX_EMMC_BLOCKSIZE, pkg1); sdmmc_storage_read(&emmc_storage, 0x100000 / NX_EMMC_BLOCKSIZE, 0x40000 / NX_EMMC_BLOCKSIZE, pkg1);
const pkg1_id_t *pkg1_id = pkg1_identify(pkg1); const pkg1_id_t *pkg1_id = pkg1_identify(pkg1);
if (!pkg1_id) if (!pkg1_id)
{ {
EPRINTF("Unknown pkg1 version for reading\nTSEC firmware."); EPRINTF("Unknown pkg1 version for reading\nTSEC firmware.");
// Dump package1. // Dump package1.
emmcsn_path_impl(path, "/pkg1", "pkg1_enc.bin", &storage); emmcsn_path_impl(path, "/pkg1", "pkg1_enc.bin", &emmc_storage);
if (sd_save_to_file(pkg1, 0x40000, path)) if (sd_save_to_file(pkg1, 0x40000, path))
goto out_free; goto out_free;
gfx_puts("\nEnc pkg1 dumped to pkg1_enc.bin\n"); gfx_puts("\nEnc pkg1 dumped to pkg1_enc.bin\n");
@ -116,7 +114,7 @@ void dump_packages12()
// Read keyblob. // Read keyblob.
u8 *keyblob = (u8 *)calloc(NX_EMMC_BLOCKSIZE, 1); u8 *keyblob = (u8 *)calloc(NX_EMMC_BLOCKSIZE, 1);
sdmmc_storage_read(&storage, 0x180000 / NX_EMMC_BLOCKSIZE + kb, 1, keyblob); sdmmc_storage_read(&emmc_storage, 0x180000 / NX_EMMC_BLOCKSIZE + kb, 1, keyblob);
// Decrypt. // Decrypt.
hos_keygen(keyblob, kb, &tsec_ctxt, NULL); hos_keygen(keyblob, kb, &tsec_ctxt, NULL);
@ -156,35 +154,35 @@ void dump_packages12()
gfx_printf("%kWarmboot size: %k0x%05X\n\n", 0xFFC7EA46, 0xFFCCCCCC, hdr_pk11->wb_size); gfx_printf("%kWarmboot size: %k0x%05X\n\n", 0xFFC7EA46, 0xFFCCCCCC, hdr_pk11->wb_size);
// Dump package1.1. // Dump package1.1.
emmcsn_path_impl(path, "/pkg1", "pkg1_decr.bin", &storage); emmcsn_path_impl(path, "/pkg1", "pkg1_decr.bin", &emmc_storage);
if (sd_save_to_file(pkg1, 0x40000, path)) if (sd_save_to_file(pkg1, 0x40000, path))
goto out_free; goto out_free;
gfx_puts("\npkg1 dumped to pkg1_decr.bin\n"); gfx_puts("\npkg1 dumped to pkg1_decr.bin\n");
// Dump nxbootloader. // Dump nxbootloader.
emmcsn_path_impl(path, "/pkg1", "nxloader.bin", &storage); emmcsn_path_impl(path, "/pkg1", "nxloader.bin", &emmc_storage);
if (sd_save_to_file(loader, hdr_pk11->ldr_size, path)) if (sd_save_to_file(loader, hdr_pk11->ldr_size, path))
goto out_free; goto out_free;
gfx_puts("NX Bootloader dumped to nxloader.bin\n"); gfx_puts("NX Bootloader dumped to nxloader.bin\n");
// Dump secmon. // Dump secmon.
emmcsn_path_impl(path, "/pkg1", "secmon.bin", &storage); emmcsn_path_impl(path, "/pkg1", "secmon.bin", &emmc_storage);
if (sd_save_to_file(secmon, hdr_pk11->sm_size, path)) if (sd_save_to_file(secmon, hdr_pk11->sm_size, path))
goto out_free; goto out_free;
gfx_puts("Secure Monitor dumped to secmon.bin\n"); gfx_puts("Secure Monitor dumped to secmon.bin\n");
// Dump warmboot. // Dump warmboot.
emmcsn_path_impl(path, "/pkg1", "warmboot.bin", &storage); emmcsn_path_impl(path, "/pkg1", "warmboot.bin", &emmc_storage);
if (sd_save_to_file(warmboot, hdr_pk11->wb_size, path)) if (sd_save_to_file(warmboot, hdr_pk11->wb_size, path))
goto out_free; goto out_free;
gfx_puts("Warmboot dumped to warmboot.bin\n\n\n"); gfx_puts("Warmboot dumped to warmboot.bin\n\n\n");
} }
// Dump package2.1. // Dump package2.1.
sdmmc_storage_set_mmc_partition(&storage, EMMC_GPP); sdmmc_storage_set_mmc_partition(&emmc_storage, EMMC_GPP);
// Parse eMMC GPT. // Parse eMMC GPT.
LIST_INIT(gpt); LIST_INIT(gpt);
nx_emmc_gpt_parse(&gpt, &storage); nx_emmc_gpt_parse(&gpt, &emmc_storage);
// Find package2 partition. // Find package2 partition.
emmc_part_t *pkg2_part = nx_emmc_part_find(&gpt, "BCPKG2-1-Normal-Main"); emmc_part_t *pkg2_part = nx_emmc_part_find(&gpt, "BCPKG2-1-Normal-Main");
if (!pkg2_part) if (!pkg2_part)
@ -192,14 +190,14 @@ void dump_packages12()
// Read in package2 header and get package2 real size. // Read in package2 header and get package2 real size.
u8 *tmp = (u8 *)malloc(NX_EMMC_BLOCKSIZE); u8 *tmp = (u8 *)malloc(NX_EMMC_BLOCKSIZE);
nx_emmc_part_read(&storage, pkg2_part, 0x4000 / NX_EMMC_BLOCKSIZE, 1, tmp); nx_emmc_part_read(&emmc_storage, pkg2_part, 0x4000 / NX_EMMC_BLOCKSIZE, 1, tmp);
u32 *hdr_pkg2_raw = (u32 *)(tmp + 0x100); u32 *hdr_pkg2_raw = (u32 *)(tmp + 0x100);
u32 pkg2_size = hdr_pkg2_raw[0] ^ hdr_pkg2_raw[2] ^ hdr_pkg2_raw[3]; u32 pkg2_size = hdr_pkg2_raw[0] ^ hdr_pkg2_raw[2] ^ hdr_pkg2_raw[3];
free(tmp); free(tmp);
// Read in package2. // Read in package2.
u32 pkg2_size_aligned = ALIGN(pkg2_size, NX_EMMC_BLOCKSIZE); u32 pkg2_size_aligned = ALIGN(pkg2_size, NX_EMMC_BLOCKSIZE);
pkg2 = malloc(pkg2_size_aligned); pkg2 = malloc(pkg2_size_aligned);
nx_emmc_part_read(&storage, pkg2_part, 0x4000 / NX_EMMC_BLOCKSIZE, nx_emmc_part_read(&emmc_storage, pkg2_part, 0x4000 / NX_EMMC_BLOCKSIZE,
pkg2_size_aligned / NX_EMMC_BLOCKSIZE, pkg2); pkg2_size_aligned / NX_EMMC_BLOCKSIZE, pkg2);
// Decrypt package2 and parse KIP1 blobs in INI1 section. // Decrypt package2 and parse KIP1 blobs in INI1 section.
pkg2_hdr_t *pkg2_hdr = pkg2_decrypt(pkg2, kb); pkg2_hdr_t *pkg2_hdr = pkg2_decrypt(pkg2, kb);
@ -214,19 +212,19 @@ void dump_packages12()
gfx_printf("%kINI1 size: %k0x%05X\n\n", 0xFFC7EA46, 0xFFCCCCCC, pkg2_hdr->sec_size[PKG2_SEC_INI1]); gfx_printf("%kINI1 size: %k0x%05X\n\n", 0xFFC7EA46, 0xFFCCCCCC, pkg2_hdr->sec_size[PKG2_SEC_INI1]);
// Dump pkg2.1. // Dump pkg2.1.
emmcsn_path_impl(path, "/pkg2", "pkg2_decr.bin", &storage); emmcsn_path_impl(path, "/pkg2", "pkg2_decr.bin", &emmc_storage);
if (sd_save_to_file(pkg2, pkg2_hdr->sec_size[PKG2_SEC_KERNEL] + pkg2_hdr->sec_size[PKG2_SEC_INI1], path)) if (sd_save_to_file(pkg2, pkg2_hdr->sec_size[PKG2_SEC_KERNEL] + pkg2_hdr->sec_size[PKG2_SEC_INI1], path))
goto out; goto out;
gfx_puts("\npkg2 dumped to pkg2_decr.bin\n"); gfx_puts("\npkg2 dumped to pkg2_decr.bin\n");
// Dump kernel. // Dump kernel.
emmcsn_path_impl(path, "/pkg2", "kernel.bin", &storage); emmcsn_path_impl(path, "/pkg2", "kernel.bin", &emmc_storage);
if (sd_save_to_file(pkg2_hdr->data, pkg2_hdr->sec_size[PKG2_SEC_KERNEL], path)) if (sd_save_to_file(pkg2_hdr->data, pkg2_hdr->sec_size[PKG2_SEC_KERNEL], path))
goto out; goto out;
gfx_puts("Kernel dumped to kernel.bin\n"); gfx_puts("Kernel dumped to kernel.bin\n");
// Dump INI1. // Dump INI1.
emmcsn_path_impl(path, "/pkg2", "ini1.bin", &storage); emmcsn_path_impl(path, "/pkg2", "ini1.bin", &emmc_storage);
u32 ini1_off = pkg2_hdr->sec_size[PKG2_SEC_KERNEL]; u32 ini1_off = pkg2_hdr->sec_size[PKG2_SEC_KERNEL];
u32 ini1_size = pkg2_hdr->sec_size[PKG2_SEC_INI1]; u32 ini1_size = pkg2_hdr->sec_size[PKG2_SEC_INI1];
if (!ini1_size) if (!ini1_size)
@ -257,7 +255,7 @@ out_free:
free(warmboot); free(warmboot);
free(loader); free(loader);
free(pkg2); free(pkg2);
sdmmc_storage_end(&storage); sdmmc_storage_end(&emmc_storage);
sd_end(); sd_end();
if (kb >= KB_FIRMWARE_VERSION_620) if (kb >= KB_FIRMWARE_VERSION_620)
@ -268,20 +266,17 @@ out_free:
void _toggle_autorcm(bool enable) void _toggle_autorcm(bool enable)
{ {
sdmmc_storage_t storage;
sdmmc_t sdmmc;
gfx_clear_partial_grey(0x1B, 0, 1256); gfx_clear_partial_grey(0x1B, 0, 1256);
gfx_con_setpos(0, 0); gfx_con_setpos(0, 0);
if (!sdmmc_storage_init_mmc(&storage, &sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400)) if (!sdmmc_storage_init_mmc(&emmc_storage, &emmc_sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400))
{ {
EPRINTF("Failed to init eMMC."); EPRINTF("Failed to init eMMC.");
goto out; goto out;
} }
u8 *tempbuf = (u8 *)malloc(0x200); u8 *tempbuf = (u8 *)malloc(0x200);
sdmmc_storage_set_mmc_partition(&storage, EMMC_BOOT0); sdmmc_storage_set_mmc_partition(&emmc_storage, EMMC_BOOT0);
int i, sect = 0; int i, sect = 0;
u8 corr_mod0, mod1; u8 corr_mod0, mod1;
@ -293,7 +288,7 @@ void _toggle_autorcm(bool enable)
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
{ {
sect = (0x200 + (0x4000 * i)) / NX_EMMC_BLOCKSIZE; sect = (0x200 + (0x4000 * i)) / NX_EMMC_BLOCKSIZE;
sdmmc_storage_read(&storage, sect, 1, tempbuf); sdmmc_storage_read(&emmc_storage, sect, 1, tempbuf);
// Check if 2nd byte of modulus is correct. // Check if 2nd byte of modulus is correct.
if (tempbuf[0x11] != mod1) if (tempbuf[0x11] != mod1)
@ -303,11 +298,11 @@ void _toggle_autorcm(bool enable)
tempbuf[0x10] = 0; tempbuf[0x10] = 0;
else else
tempbuf[0x10] = corr_mod0; tempbuf[0x10] = corr_mod0;
sdmmc_storage_write(&storage, sect, 1, tempbuf); sdmmc_storage_write(&emmc_storage, sect, 1, tempbuf);
} }
free(tempbuf); free(tempbuf);
sdmmc_storage_end(&storage); sdmmc_storage_end(&emmc_storage);
if (enable) if (enable)
gfx_printf("%kAutoRCM mode enabled!%k", 0xFFFFBA00, 0xFFCCCCCC); gfx_printf("%kAutoRCM mode enabled!%k", 0xFFFFBA00, 0xFFCCCCCC);
@ -338,11 +333,9 @@ void menu_autorcm()
} }
// Do a simple check on the main BCT. // Do a simple check on the main BCT.
sdmmc_storage_t storage;
sdmmc_t sdmmc;
bool disabled = true; bool disabled = true;
if (!sdmmc_storage_init_mmc(&storage, &sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400)) if (!sdmmc_storage_init_mmc(&emmc_storage, &emmc_sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400))
{ {
EPRINTF("Failed to init eMMC."); EPRINTF("Failed to init eMMC.");
btn_wait(); btn_wait();
@ -355,8 +348,8 @@ void menu_autorcm()
nx_emmc_get_autorcm_masks(&mod0, &mod1); nx_emmc_get_autorcm_masks(&mod0, &mod1);
u8 *tempbuf = (u8 *)malloc(0x200); u8 *tempbuf = (u8 *)malloc(0x200);
sdmmc_storage_set_mmc_partition(&storage, EMMC_BOOT0); sdmmc_storage_set_mmc_partition(&emmc_storage, EMMC_BOOT0);
sdmmc_storage_read(&storage, 0x200 / NX_EMMC_BLOCKSIZE, 1, tempbuf); sdmmc_storage_read(&emmc_storage, 0x200 / NX_EMMC_BLOCKSIZE, 1, tempbuf);
// Check if 2nd byte of modulus is correct. // Check if 2nd byte of modulus is correct.
if (tempbuf[0x11] == mod1) if (tempbuf[0x11] == mod1)
@ -364,7 +357,7 @@ void menu_autorcm()
disabled = false; disabled = false;
free(tempbuf); free(tempbuf);
sdmmc_storage_end(&storage); sdmmc_storage_end(&emmc_storage);
// Create AutoRCM menu. // Create AutoRCM menu.
ment_t *ments = (ment_t *)malloc(sizeof(ment_t) * 6); ment_t *ments = (ment_t *)malloc(sizeof(ment_t) * 6);

View file

@ -594,8 +594,8 @@ static int _read_emmc_pkg1(launch_ctxt_t *ctxt)
try_load: try_load:
// Read package1. // Read package1.
emummc_storage_set_mmc_partition(&emmc_storage, EMMC_BOOT0); emummc_storage_set_mmc_partition(EMMC_BOOT0);
emummc_storage_read(&emmc_storage, bootloader_offset / NX_EMMC_BLOCKSIZE, BOOTLOADER_SIZE / NX_EMMC_BLOCKSIZE, ctxt->pkg1); emummc_storage_read(bootloader_offset / NX_EMMC_BLOCKSIZE, BOOTLOADER_SIZE / NX_EMMC_BLOCKSIZE, ctxt->pkg1);
ctxt->pkg1_id = pkg1_identify(ctxt->pkg1 + pk1_offset); ctxt->pkg1_id = pkg1_identify(ctxt->pkg1 + pk1_offset);
if (!ctxt->pkg1_id) if (!ctxt->pkg1_id)
@ -618,7 +618,7 @@ try_load:
// Read the correct keyblob. // Read the correct keyblob.
ctxt->keyblob = (u8 *)calloc(NX_EMMC_BLOCKSIZE, 1); ctxt->keyblob = (u8 *)calloc(NX_EMMC_BLOCKSIZE, 1);
emummc_storage_read(&emmc_storage, HOS_KEYBLOBS_OFFSET / NX_EMMC_BLOCKSIZE + ctxt->pkg1_id->kb, 1, ctxt->keyblob); emummc_storage_read(HOS_KEYBLOBS_OFFSET / NX_EMMC_BLOCKSIZE + ctxt->pkg1_id->kb, 1, ctxt->keyblob);
return 1; return 1;
} }
@ -627,7 +627,7 @@ static u8 *_read_emmc_pkg2(launch_ctxt_t *ctxt)
{ {
u8 *bctBuf = NULL; u8 *bctBuf = NULL;
emummc_storage_set_mmc_partition(&emmc_storage, EMMC_GPP); emummc_storage_set_mmc_partition(EMMC_GPP);
// Parse eMMC GPT. // Parse eMMC GPT.
LIST_INIT(gpt); LIST_INIT(gpt);
@ -738,13 +738,13 @@ int hos_launch(ini_sec_t *cfg)
gfx_puts("Initializing...\n\n"); gfx_puts("Initializing...\n\n");
// Initialize eMMC/emuMMC. // Initialize eMMC/emuMMC.
int res = emummc_storage_init_mmc(&emmc_storage, &emmc_sdmmc); int res = emummc_storage_init_mmc();
if (res) if (res)
{ {
if (res == 2) if (res == 2)
_hos_crit_error("Failed to init eMMC"); _hos_crit_error("Failed to init eMMC.");
else else
_hos_crit_error("Failed to init emuMMC"); _hos_crit_error("Failed to init emuMMC.");
goto error; goto error;
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2019 CTCaer * Copyright (c) 2019-2021 CTCaer
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License, * under the terms and conditions of the GNU General Public License,
@ -93,23 +93,21 @@ void check_sept(ini_sec_t *cfg_sec)
u8 *pkg1 = (u8 *)calloc(1, 0x40000); u8 *pkg1 = (u8 *)calloc(1, 0x40000);
sdmmc_storage_t storage; int res = emummc_storage_init_mmc();
sdmmc_t sdmmc;
int res = emummc_storage_init_mmc(&storage, &sdmmc);
if (res) if (res)
{ {
if (res == 2) if (res == 2)
EPRINTF("Failed to init eMMC"); EPRINTF("Failed to init eMMC.");
else else
EPRINTF("Failed to init emuMMC"); EPRINTF("Failed to init emuMMC.");
goto out_free; goto out_free;
} }
emummc_storage_set_mmc_partition(&storage, EMMC_BOOT0); emummc_storage_set_mmc_partition(EMMC_BOOT0);
// Read package1. // Read package1.
emummc_storage_read(&storage, 0x100000 / NX_EMMC_BLOCKSIZE, 0x40000 / NX_EMMC_BLOCKSIZE, pkg1); emummc_storage_read(0x100000 / NX_EMMC_BLOCKSIZE, 0x40000 / NX_EMMC_BLOCKSIZE, pkg1);
const pkg1_id_t *pkg1_id = pkg1_identify(pkg1); const pkg1_id_t *pkg1_id = pkg1_identify(pkg1);
if (!pkg1_id) if (!pkg1_id)
{ {
@ -129,13 +127,13 @@ void check_sept(ini_sec_t *cfg_sec)
goto out_free; goto out_free;
} }
sdmmc_storage_end(&storage); sdmmc_storage_end(&emmc_storage);
reboot_to_sept((u8 *)pkg1 + pkg1_id->tsec_off, pkg1_id->kb, cfg_sec); reboot_to_sept((u8 *)pkg1 + pkg1_id->tsec_off, pkg1_id->kb, cfg_sec);
} }
out_free: out_free:
free(pkg1); free(pkg1);
sdmmc_storage_end(&storage); sdmmc_storage_end(&emmc_storage);
} }
int reboot_to_sept(const u8 *tsec_fw, u32 kb, ini_sec_t *cfg_sec) int reboot_to_sept(const u8 *tsec_fw, u32 kb, ini_sec_t *cfg_sec)

View file

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2018 naehrwert * Copyright (c) 2018 naehrwert
* *
* Copyright (c) 2018-2020 CTCaer * Copyright (c) 2018-2021 CTCaer
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License, * under the terms and conditions of the GNU General Public License,
@ -73,8 +73,6 @@ volatile nyx_storage_t *nyx_str = (nyx_storage_t *)NYX_STORAGE_ADDR;
void emmcsn_path_impl(char *path, char *sub_dir, char *filename, sdmmc_storage_t *storage) void emmcsn_path_impl(char *path, char *sub_dir, char *filename, sdmmc_storage_t *storage)
{ {
sdmmc_storage_t storage2;
sdmmc_t sdmmc;
char emmcSN[9]; char emmcSN[9];
bool init_done = false; bool init_done = false;
@ -83,12 +81,12 @@ void emmcsn_path_impl(char *path, char *sub_dir, char *filename, sdmmc_storage_t
if (!storage) if (!storage)
{ {
if (!sdmmc_storage_init_mmc(&storage2, &sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400)) if (!sdmmc_storage_init_mmc(&emmc_storage, &emmc_sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400))
memcpy(emmcSN, "00000000", 9); memcpy(emmcSN, "00000000", 9);
else else
{ {
init_done = true; init_done = true;
itoa(storage2.cid.serial, emmcSN, 16); itoa(emmc_storage.cid.serial, emmcSN, 16);
} }
} }
else else
@ -107,7 +105,7 @@ void emmcsn_path_impl(char *path, char *sub_dir, char *filename, sdmmc_storage_t
memcpy(path + strlen(path), filename, filename_len + 1); memcpy(path + strlen(path), filename, filename_len + 1);
if (init_done) if (init_done)
sdmmc_storage_end(&storage2); sdmmc_storage_end(&emmc_storage);
} }
void check_power_off_from_hos() void check_power_off_from_hos()
@ -1120,18 +1118,15 @@ out:
static void _patched_rcm_protection() static void _patched_rcm_protection()
{ {
sdmmc_storage_t storage;
sdmmc_t sdmmc;
if (!h_cfg.rcm_patched || hw_get_chip_id() == GP_HIDREV_MAJOR_T210B01) if (!h_cfg.rcm_patched || hw_get_chip_id() == GP_HIDREV_MAJOR_T210B01)
return; return;
// Check if AutoRCM is enabled and protect from a permanent brick. // Check if AutoRCM is enabled and protect from a permanent brick.
if (!sdmmc_storage_init_mmc(&storage, &sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400)) if (!sdmmc_storage_init_mmc(&emmc_storage, &emmc_sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400))
return; return;
u8 *buf = (u8 *)malloc(0x200); u8 *buf = (u8 *)malloc(0x200);
sdmmc_storage_set_mmc_partition(&storage, EMMC_BOOT0); sdmmc_storage_set_mmc_partition(&emmc_storage, EMMC_BOOT0);
u32 sector; u32 sector;
u8 corr_mod0, mod1; u8 corr_mod0, mod1;
@ -1143,7 +1138,7 @@ static void _patched_rcm_protection()
for (u32 i = 0; i < 4; i++) for (u32 i = 0; i < 4; i++)
{ {
sector = 1 + (32 * i); // 0x4000 bct + 0x200 offset. sector = 1 + (32 * i); // 0x4000 bct + 0x200 offset.
sdmmc_storage_read(&storage, sector, 1, buf); sdmmc_storage_read(&emmc_storage, sector, 1, buf);
// Check if 2nd byte of modulus is correct. // Check if 2nd byte of modulus is correct.
if (buf[0x11] != mod1) if (buf[0x11] != mod1)
@ -1154,12 +1149,12 @@ static void _patched_rcm_protection()
{ {
buf[0x10] = corr_mod0; buf[0x10] = corr_mod0;
sdmmc_storage_write(&storage, sector, 1, buf); sdmmc_storage_write(&emmc_storage, sector, 1, buf);
} }
} }
free(buf); free(buf);
sdmmc_storage_end(&storage); sdmmc_storage_end(&emmc_storage);
} }
#define EXCP_EN_ADDR 0x4003FFFC #define EXCP_EN_ADDR 0x4003FFFC

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2019 CTCaer * Copyright (c) 2019-2021 CTCaer
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License, * under the terms and conditions of the GNU General Public License,
@ -131,13 +131,13 @@ static int emummc_raw_get_part_off(int part_idx)
return 2; return 2;
} }
int emummc_storage_init_mmc(sdmmc_storage_t *storage, sdmmc_t *sdmmc) int emummc_storage_init_mmc()
{ {
FILINFO fno; FILINFO fno;
emu_cfg.active_part = 0; emu_cfg.active_part = 0;
// Always init eMMC even when in emuMMC. eMMC is needed from the emuMMC driver anyway. // Always init eMMC even when in emuMMC. eMMC is needed from the emuMMC driver anyway.
if (!sdmmc_storage_init_mmc(storage, sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400)) if (!sdmmc_storage_init_mmc(&emmc_storage, &emmc_sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400))
return 2; return 2;
if (!emu_cfg.enabled || h_cfg.emummc_force_disable) if (!emu_cfg.enabled || h_cfg.emummc_force_disable)
@ -173,21 +173,21 @@ out:
return 1; return 1;
} }
int emummc_storage_end(sdmmc_storage_t *storage) int emummc_storage_end()
{ {
if (!emu_cfg.enabled || h_cfg.emummc_force_disable) if (!emu_cfg.enabled || h_cfg.emummc_force_disable)
sdmmc_storage_end(storage); sdmmc_storage_end(&emmc_storage);
else else
sd_end(); sd_end();
return 1; return 1;
} }
int emummc_storage_read(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, void *buf) int emummc_storage_read(u32 sector, u32 num_sectors, void *buf)
{ {
FIL fp; FIL fp;
if (!emu_cfg.enabled || h_cfg.emummc_force_disable) if (!emu_cfg.enabled || h_cfg.emummc_force_disable)
return sdmmc_storage_read(storage, sector, num_sectors, buf); return sdmmc_storage_read(&emmc_storage, sector, num_sectors, buf);
else if (emu_cfg.sector) else if (emu_cfg.sector)
{ {
sector += emu_cfg.sector; sector += emu_cfg.sector;
@ -228,11 +228,11 @@ int emummc_storage_read(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, v
return 1; return 1;
} }
int emummc_storage_write(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, void *buf) int emummc_storage_write(u32 sector, u32 num_sectors, void *buf)
{ {
FIL fp; FIL fp;
if (!emu_cfg.enabled || h_cfg.emummc_force_disable) if (!emu_cfg.enabled || h_cfg.emummc_force_disable)
return sdmmc_storage_write(storage, sector, num_sectors, buf); return sdmmc_storage_write(&emmc_storage, sector, num_sectors, buf);
else if (emu_cfg.sector) else if (emu_cfg.sector)
{ {
sector += emu_cfg.sector; sector += emu_cfg.sector;
@ -253,15 +253,13 @@ int emummc_storage_write(sdmmc_storage_t *storage, u32 sector, u32 num_sectors,
itoa(file_part, emu_cfg.emummc_file_based_path + strlen(emu_cfg.emummc_file_based_path) - 1, 10); itoa(file_part, emu_cfg.emummc_file_based_path + strlen(emu_cfg.emummc_file_based_path) - 1, 10);
} }
} }
if (f_open(&fp, emu_cfg.emummc_file_based_path, FA_WRITE)) if (f_open(&fp, emu_cfg.emummc_file_based_path, FA_WRITE))
{
gfx_printf("e5\n");
return 0; return 0;
}
f_lseek(&fp, (u64)sector << 9); f_lseek(&fp, (u64)sector << 9);
if (f_write(&fp, buf, (u64)num_sectors << 9, NULL)) if (f_write(&fp, buf, (u64)num_sectors << 9, NULL))
{ {
gfx_printf("e6\n");
f_close(&fp); f_close(&fp);
return 0; return 0;
} }
@ -271,13 +269,12 @@ int emummc_storage_write(sdmmc_storage_t *storage, u32 sector, u32 num_sectors,
} }
} }
int emummc_storage_set_mmc_partition(sdmmc_storage_t *storage, u32 partition) int emummc_storage_set_mmc_partition(u32 partition)
{ {
emu_cfg.active_part = partition; emu_cfg.active_part = partition;
sdmmc_storage_set_mmc_partition(&emmc_storage, partition);
if (!emu_cfg.enabled || h_cfg.emummc_force_disable) if (!emu_cfg.enabled || h_cfg.emummc_force_disable || emu_cfg.sector)
sdmmc_storage_set_mmc_partition(storage, partition);
else if (emu_cfg.sector)
return 1; return 1;
else else
{ {

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2019 CTCaer * Copyright (c) 2019-2021 CTCaer
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License, * under the terms and conditions of the GNU General Public License,
@ -51,10 +51,10 @@ extern emummc_cfg_t emu_cfg;
void emummc_load_cfg(); void emummc_load_cfg();
bool emummc_set_path(char *path); bool emummc_set_path(char *path);
int emummc_storage_init_mmc(sdmmc_storage_t *storage, sdmmc_t *sdmmc); int emummc_storage_init_mmc();
int emummc_storage_end(sdmmc_storage_t *storage); int emummc_storage_end();
int emummc_storage_read(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, void *buf); int emummc_storage_read(u32 sector, u32 num_sectors, void *buf);
int emummc_storage_write(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, void *buf); int emummc_storage_write(u32 sector, u32 num_sectors, void *buf);
int emummc_storage_set_mmc_partition(sdmmc_storage_t *storage, u32 partition); int emummc_storage_set_mmc_partition(u32 partition);
#endif #endif

View file

@ -1,6 +1,6 @@
/* /*
* Copyright (c) 2018 naehrwert * Copyright (c) 2018 naehrwert
* Copyright (c) 2019-2020 CTCaer * Copyright (c) 2019-2021 CTCaer
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License, * under the terms and conditions of the GNU General Public License,
@ -32,7 +32,7 @@ void nx_emmc_gpt_parse(link_t *gpt, sdmmc_storage_t *storage)
{ {
gpt_t *gpt_buf = (gpt_t *)calloc(NX_GPT_NUM_BLOCKS, NX_EMMC_BLOCKSIZE); gpt_t *gpt_buf = (gpt_t *)calloc(NX_GPT_NUM_BLOCKS, NX_EMMC_BLOCKSIZE);
emummc_storage_read(storage, NX_GPT_FIRST_LBA, NX_GPT_NUM_BLOCKS, gpt_buf); emummc_storage_read(NX_GPT_FIRST_LBA, NX_GPT_NUM_BLOCKS, gpt_buf);
for (u32 i = 0; i < gpt_buf->header.num_part_ents; i++) for (u32 i = 0; i < gpt_buf->header.num_part_ents; i++)
{ {
@ -78,7 +78,7 @@ int nx_emmc_part_read(sdmmc_storage_t *storage, emmc_part_t *part, u32 sector_of
if (part->lba_start + sector_off > part->lba_end) if (part->lba_start + sector_off > part->lba_end)
return 0; return 0;
return emummc_storage_read(storage, part->lba_start + sector_off, num_sectors, buf); return emummc_storage_read(part->lba_start + sector_off, num_sectors, buf);
} }
int nx_emmc_part_write(sdmmc_storage_t *storage, emmc_part_t *part, u32 sector_off, u32 num_sectors, void *buf) int nx_emmc_part_write(sdmmc_storage_t *storage, emmc_part_t *part, u32 sector_off, u32 num_sectors, void *buf)
@ -87,7 +87,7 @@ int nx_emmc_part_write(sdmmc_storage_t *storage, emmc_part_t *part, u32 sector_o
if (part->lba_start + sector_off > part->lba_end) if (part->lba_start + sector_off > part->lba_end)
return 0; return 0;
return sdmmc_storage_write(storage, part->lba_start + sector_off, num_sectors, buf); return sdmmc_storage_write(&emmc_storage, part->lba_start + sector_off, num_sectors, buf);
} }
void nx_emmc_get_autorcm_masks(u8 *mod0, u8 *mod1) void nx_emmc_get_autorcm_masks(u8 *mod0, u8 *mod1)

View file

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2018 naehrwert * Copyright (c) 2018 naehrwert
* Copyright (c) 2018 Rajko Stojadinovic * Copyright (c) 2018 Rajko Stojadinovic
* Copyright (c) 2018-2020 CTCaer * Copyright (c) 2018-2021 CTCaer
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License, * under the terms and conditions of the GNU General Public License,
@ -41,7 +41,6 @@
#define NUM_SECTORS_PER_ITER 8192 // 4MB Cache. #define NUM_SECTORS_PER_ITER 8192 // 4MB Cache.
#define OUT_FILENAME_SZ 128 #define OUT_FILENAME_SZ 128
#define HASH_FILENAME_SZ (OUT_FILENAME_SZ + 11) // 11 == strlen(".sha256sums") #define HASH_FILENAME_SZ (OUT_FILENAME_SZ + 11) // 11 == strlen(".sha256sums")
#define SHA256_SZ 0x20
extern nyx_config n_cfg; extern nyx_config n_cfg;
@ -156,8 +155,8 @@ static int _dump_emmc_verify(emmc_tool_gui_t *gui, sdmmc_storage_t *storage, u32
const char hexa[] = "0123456789abcdef"; const char hexa[] = "0123456789abcdef";
DWORD *clmt = NULL; DWORD *clmt = NULL;
u8 hashEm[SHA256_SZ]; u8 hashEm[SE_SHA_256_SIZE];
u8 hashSd[SHA256_SZ]; u8 hashSd[SE_SHA_256_SIZE];
if (f_open(&fp, outFilename, FA_READ) == FR_OK) if (f_open(&fp, outFilename, FA_READ) == FR_OK)
{ {
@ -254,7 +253,7 @@ static int _dump_emmc_verify(emmc_tool_gui_t *gui, sdmmc_storage_t *storage, u32
manual_system_maintenance(false); manual_system_maintenance(false);
se_calc_sha256_finalize(hashEm, NULL); se_calc_sha256_finalize(hashEm, NULL);
se_calc_sha256_oneshot(hashSd, bufSd, num << 9); se_calc_sha256_oneshot(hashSd, bufSd, num << 9);
res = memcmp(hashEm, hashSd, 0x10); res = memcmp(hashEm, hashSd, SE_SHA_256_SIZE / 2);
if (res) if (res)
{ {
@ -276,14 +275,14 @@ static int _dump_emmc_verify(emmc_tool_gui_t *gui, sdmmc_storage_t *storage, u32
if (n_cfg.verification == 3) if (n_cfg.verification == 3)
{ {
// Transform computed hash to readable hexadecimal // Transform computed hash to readable hexadecimal
char hashStr[SHA256_SZ * 2 + 1]; char hashStr[SE_SHA_256_SIZE * 2 + 1];
char *hashStrPtr = hashStr; char *hashStrPtr = hashStr;
for (int i = 0; i < SHA256_SZ; i++) for (int i = 0; i < SE_SHA_256_SIZE; i++)
{ {
*(hashStrPtr++) = hexa[hashSd[i] >> 4]; *(hashStrPtr++) = hexa[hashSd[i] >> 4];
*(hashStrPtr++) = hexa[hashSd[i] & 0x0F]; *(hashStrPtr++) = hexa[hashSd[i] & 0x0F];
} }
hashStr[SHA256_SZ * 2] = '\0'; hashStr[SE_SHA_256_SIZE * 2] = '\0';
f_puts(hashStr, &hashFp); f_puts(hashStr, &hashFp);
f_puts("\n", &hashFp); f_puts("\n", &hashFp);

View file

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2018 naehrwert * Copyright (c) 2018 naehrwert
* Copyright (c) 2018 Rajko Stojadinovic * Copyright (c) 2018 Rajko Stojadinovic
* Copyright (c) 2018-2020 CTCaer * Copyright (c) 2018-2021 CTCaer
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License, * under the terms and conditions of the GNU General Public License,
@ -349,6 +349,7 @@ void dump_emummc_file(emmc_tool_gui_t *gui)
u32 timer = 0; u32 timer = 0;
char *txt_buf = (char *)malloc(0x4000); char *txt_buf = (char *)malloc(0x4000);
gui->base_path = (char *)malloc(OUT_FILENAME_SZ);
gui->txt_buf = txt_buf; gui->txt_buf = txt_buf;
s_printf(txt_buf, ""); s_printf(txt_buf, "");
@ -368,9 +369,7 @@ void dump_emummc_file(emmc_tool_gui_t *gui)
// Get SD Card free space for Partial Backup. // Get SD Card free space for Partial Backup.
f_getfree("", &sd_fs.free_clst, NULL); f_getfree("", &sd_fs.free_clst, NULL);
sdmmc_storage_t storage; if (!sdmmc_storage_init_mmc(&emmc_storage, &emmc_sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400))
sdmmc_t sdmmc;
if (!sdmmc_storage_init_mmc(&storage, &sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400))
{ {
lv_label_set_text(gui->label_info, "#FFDD00 Failed to init eMMC!#"); lv_label_set_text(gui->label_info, "#FFDD00 Failed to init eMMC!#");
goto out; goto out;
@ -382,7 +381,6 @@ void dump_emummc_file(emmc_tool_gui_t *gui)
f_mkdir("emuMMC"); f_mkdir("emuMMC");
strcpy(sdPath, "emuMMC/SD"); strcpy(sdPath, "emuMMC/SD");
base_len = strlen(sdPath); base_len = strlen(sdPath);
gui->base_path = (char *)malloc(OUT_FILENAME_SZ);
for (int j = 0; j < 100; j++) for (int j = 0; j < 100; j++)
{ {
@ -398,7 +396,7 @@ void dump_emummc_file(emmc_tool_gui_t *gui)
strcpy(gui->base_path, sdPath); strcpy(gui->base_path, sdPath);
timer = get_tmr_s(); timer = get_tmr_s();
const u32 BOOT_PART_SIZE = storage.ext_csd.boot_mult << 17; const u32 BOOT_PART_SIZE = emmc_storage.ext_csd.boot_mult << 17;
emmc_part_t bootPart; emmc_part_t bootPart;
memset(&bootPart, 0, sizeof(bootPart)); memset(&bootPart, 0, sizeof(bootPart));
@ -417,10 +415,10 @@ void dump_emummc_file(emmc_tool_gui_t *gui)
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, txt_buf); lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, txt_buf);
manual_system_maintenance(true); manual_system_maintenance(true);
sdmmc_storage_set_mmc_partition(&storage, i + 1); sdmmc_storage_set_mmc_partition(&emmc_storage, i + 1);
strcat(sdPath, bootPart.name); strcat(sdPath, bootPart.name);
res = _dump_emummc_file_part(gui, sdPath, &storage, &bootPart); res = _dump_emummc_file_part(gui, sdPath, &emmc_storage, &bootPart);
if (!res) if (!res)
{ {
@ -437,10 +435,10 @@ void dump_emummc_file(emmc_tool_gui_t *gui)
} }
// Get GP partition size dynamically. // Get GP partition size dynamically.
sdmmc_storage_set_mmc_partition(&storage, EMMC_GPP); sdmmc_storage_set_mmc_partition(&emmc_storage, EMMC_GPP);
// Get GP partition size dynamically. // Get GP partition size dynamically.
const u32 RAW_AREA_NUM_SECTORS = storage.sec_cnt; const u32 RAW_AREA_NUM_SECTORS = emmc_storage.sec_cnt;
emmc_part_t rawPart; emmc_part_t rawPart;
memset(&rawPart, 0, sizeof(rawPart)); memset(&rawPart, 0, sizeof(rawPart));
@ -455,7 +453,7 @@ void dump_emummc_file(emmc_tool_gui_t *gui)
lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, txt_buf); lv_label_ins_text(gui->label_log, LV_LABEL_POS_LAST, txt_buf);
manual_system_maintenance(true); manual_system_maintenance(true);
res = _dump_emummc_file_part(gui, sdPath, &storage, &rawPart); res = _dump_emummc_file_part(gui, sdPath, &emmc_storage, &rawPart);
if (!res) if (!res)
s_printf(txt_buf, "#FFDD00 Failed!#\n"); s_printf(txt_buf, "#FFDD00 Failed!#\n");
@ -468,7 +466,7 @@ void dump_emummc_file(emmc_tool_gui_t *gui)
out_failed: out_failed:
timer = get_tmr_s() - timer; timer = get_tmr_s() - timer;
sdmmc_storage_end(&storage); sdmmc_storage_end(&emmc_storage);
if (res) if (res)
{ {
@ -637,6 +635,7 @@ void dump_emummc_raw(emmc_tool_gui_t *gui, int part_idx, u32 sector_start)
u32 timer = 0; u32 timer = 0;
char *txt_buf = (char *)malloc(0x4000); char *txt_buf = (char *)malloc(0x4000);
gui->base_path = (char *)malloc(OUT_FILENAME_SZ);
gui->txt_buf = txt_buf; gui->txt_buf = txt_buf;
s_printf(txt_buf, ""); s_printf(txt_buf, "");
@ -663,7 +662,6 @@ void dump_emummc_raw(emmc_tool_gui_t *gui, int part_idx, u32 sector_start)
// Create Restore folders, if they do not exist. // Create Restore folders, if they do not exist.
f_mkdir("emuMMC"); f_mkdir("emuMMC");
s_printf(sdPath, "emuMMC/RAW%d", part_idx); s_printf(sdPath, "emuMMC/RAW%d", part_idx);
gui->base_path = (char *)malloc(OUT_FILENAME_SZ);
f_mkdir(sdPath); f_mkdir(sdPath);
strcat(sdPath, "/"); strcat(sdPath, "/");
strcpy(gui->base_path, sdPath); strcpy(gui->base_path, sdPath);

View file

@ -1,6 +1,6 @@
/* /*
* Copyright (c) 2018 naehrwert * Copyright (c) 2018 naehrwert
* Copyright (c) 2018-2020 CTCaer * Copyright (c) 2018-2021 CTCaer
* Copyright (c) 2018 balika011 * Copyright (c) 2018 balika011
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
@ -269,7 +269,7 @@ static lv_res_t _tsec_keys_dump_window_action(lv_obj_t * btn)
{ {
char path[64]; char path[64];
emmcsn_path_impl(path, "/dumps", "tsec_keys.bin", NULL); emmcsn_path_impl(path, "/dumps", "tsec_keys.bin", NULL);
error = sd_save_to_file(tsec_keys, 0x10 * 2, path); error = sd_save_to_file(tsec_keys, SE_KEY_128_SIZE * 2, path);
sd_unmount(); sd_unmount();
} }

View file

@ -1,6 +1,6 @@
/* /*
* Copyright (c) 2018 naehrwert * Copyright (c) 2018 naehrwert
* Copyright (c) 2018-2020 CTCaer * Copyright (c) 2018-2021 CTCaer
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License, * under the terms and conditions of the GNU General Public License,
@ -1095,19 +1095,14 @@ static lv_res_t _create_window_dump_pk12_tool(lv_obj_t *btn)
char *txt_buf = (char *)malloc(0x4000); char *txt_buf = (char *)malloc(0x4000);
tsec_ctxt_t tsec_ctxt; if (!sdmmc_storage_init_mmc(&emmc_storage, &emmc_sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400))
sdmmc_storage_t storage;
sdmmc_t sdmmc;
if (!sdmmc_storage_init_mmc(&storage, &sdmmc, SDMMC_BUS_WIDTH_8, SDHCI_TIMING_MMC_HS400))
{ {
lv_label_set_text(lb_desc, "#FFDD00 Failed to init eMMC!#"); lv_label_set_text(lb_desc, "#FFDD00 Failed to init eMMC!#");
goto out_free; goto out_free;
} }
sdmmc_storage_set_mmc_partition(&storage, EMMC_BOOT0); sdmmc_storage_set_mmc_partition(&emmc_storage, EMMC_BOOT0);
// Read package1. // Read package1.
static const u32 BOOTLOADER_SIZE = 0x40000; static const u32 BOOTLOADER_SIZE = 0x40000;
@ -1116,7 +1111,7 @@ static lv_res_t _create_window_dump_pk12_tool(lv_obj_t *btn)
char *build_date = malloc(32); char *build_date = malloc(32);
u32 pk1_offset = h_cfg.t210b01 ? sizeof(bl_hdr_t210b01_t) : 0; // Skip T210B01 OEM header. u32 pk1_offset = h_cfg.t210b01 ? sizeof(bl_hdr_t210b01_t) : 0; // Skip T210B01 OEM header.
sdmmc_storage_read(&storage, BOOTLOADER_MAIN_OFFSET / NX_EMMC_BLOCKSIZE, BOOTLOADER_SIZE / NX_EMMC_BLOCKSIZE, pkg1); sdmmc_storage_read(&emmc_storage, BOOTLOADER_MAIN_OFFSET / NX_EMMC_BLOCKSIZE, BOOTLOADER_SIZE / NX_EMMC_BLOCKSIZE, pkg1);
const pkg1_id_t *pkg1_id = pkg1_identify(pkg1 + pk1_offset, build_date); const pkg1_id_t *pkg1_id = pkg1_identify(pkg1 + pk1_offset, build_date);
@ -1132,7 +1127,7 @@ static lv_res_t _create_window_dump_pk12_tool(lv_obj_t *btn)
lv_label_set_text(lb_desc, txt_buf); lv_label_set_text(lb_desc, txt_buf);
manual_system_maintenance(true); manual_system_maintenance(true);
emmcsn_path_impl(path, "/pkg1", "pkg1_enc.bin", &storage); emmcsn_path_impl(path, "/pkg1", "pkg1_enc.bin", &emmc_storage);
if (sd_save_to_file(pkg1, BOOTLOADER_SIZE, path)) if (sd_save_to_file(pkg1, BOOTLOADER_SIZE, path))
goto out_free; goto out_free;
@ -1147,6 +1142,7 @@ static lv_res_t _create_window_dump_pk12_tool(lv_obj_t *btn)
if (!h_cfg.se_keygen_done) if (!h_cfg.se_keygen_done)
{ {
tsec_ctxt_t tsec_ctxt;
tsec_ctxt.fw = (void *)(pkg1 + pkg1_id->tsec_off); tsec_ctxt.fw = (void *)(pkg1 + pkg1_id->tsec_off);
tsec_ctxt.pkg1 = (void *)pkg1; tsec_ctxt.pkg1 = (void *)pkg1;
tsec_ctxt.pkg11_off = pkg1_id->pkg11_off; tsec_ctxt.pkg11_off = pkg1_id->pkg11_off;
@ -1178,7 +1174,7 @@ static lv_res_t _create_window_dump_pk12_tool(lv_obj_t *btn)
// Read keyblob. // Read keyblob.
u8 *keyblob = (u8 *)calloc(NX_EMMC_BLOCKSIZE, 1); u8 *keyblob = (u8 *)calloc(NX_EMMC_BLOCKSIZE, 1);
sdmmc_storage_read(&storage, HOS_KEYBLOBS_OFFSET / NX_EMMC_BLOCKSIZE + kb, 1, keyblob); sdmmc_storage_read(&emmc_storage, HOS_KEYBLOBS_OFFSET / NX_EMMC_BLOCKSIZE + kb, 1, keyblob);
// Decrypt. // Decrypt.
hos_keygen(keyblob, kb, &tsec_ctxt); hos_keygen(keyblob, kb, &tsec_ctxt);
@ -1230,7 +1226,7 @@ static lv_res_t _create_window_dump_pk12_tool(lv_obj_t *btn)
manual_system_maintenance(true); manual_system_maintenance(true);
// Dump package1.1. // Dump package1.1.
emmcsn_path_impl(path, "/pkg1", "pkg1_decr.bin", &storage); emmcsn_path_impl(path, "/pkg1", "pkg1_decr.bin", &emmc_storage);
if (sd_save_to_file(pkg1, 0x40000, path)) if (sd_save_to_file(pkg1, 0x40000, path))
goto out_free; goto out_free;
strcat(txt_buf, "pkg1 dumped to pkg1_decr.bin\n"); strcat(txt_buf, "pkg1 dumped to pkg1_decr.bin\n");
@ -1238,7 +1234,7 @@ static lv_res_t _create_window_dump_pk12_tool(lv_obj_t *btn)
manual_system_maintenance(true); manual_system_maintenance(true);
// Dump nxbootloader. // Dump nxbootloader.
emmcsn_path_impl(path, "/pkg1", "nxloader.bin", &storage); emmcsn_path_impl(path, "/pkg1", "nxloader.bin", &emmc_storage);
if (sd_save_to_file(loader, hdr_pk11->ldr_size, path)) if (sd_save_to_file(loader, hdr_pk11->ldr_size, path))
goto out_free; goto out_free;
strcat(txt_buf, "NX Bootloader dumped to nxloader.bin\n"); strcat(txt_buf, "NX Bootloader dumped to nxloader.bin\n");
@ -1246,7 +1242,7 @@ static lv_res_t _create_window_dump_pk12_tool(lv_obj_t *btn)
manual_system_maintenance(true); manual_system_maintenance(true);
// Dump secmon. // Dump secmon.
emmcsn_path_impl(path, "/pkg1", "secmon.bin", &storage); emmcsn_path_impl(path, "/pkg1", "secmon.bin", &emmc_storage);
if (sd_save_to_file(secmon, hdr_pk11->sm_size, path)) if (sd_save_to_file(secmon, hdr_pk11->sm_size, path))
goto out_free; goto out_free;
strcat(txt_buf, "Secure Monitor dumped to secmon.bin\n"); strcat(txt_buf, "Secure Monitor dumped to secmon.bin\n");
@ -1254,7 +1250,7 @@ static lv_res_t _create_window_dump_pk12_tool(lv_obj_t *btn)
manual_system_maintenance(true); manual_system_maintenance(true);
// Dump warmboot. // Dump warmboot.
emmcsn_path_impl(path, "/pkg1", "warmboot.bin", &storage); emmcsn_path_impl(path, "/pkg1", "warmboot.bin", &emmc_storage);
if (sd_save_to_file(warmboot, hdr_pk11->wb_size, path)) if (sd_save_to_file(warmboot, hdr_pk11->wb_size, path))
goto out_free; goto out_free;
// If T210B01, save a copy of decrypted warmboot binary also. // If T210B01, save a copy of decrypted warmboot binary also.
@ -1263,7 +1259,7 @@ static lv_res_t _create_window_dump_pk12_tool(lv_obj_t *btn)
se_aes_iv_clear(13); se_aes_iv_clear(13);
se_aes_crypt_cbc(13, 0, warmboot + 0x330, hdr_pk11->wb_size - 0x330, warmboot + 0x330, hdr_pk11->wb_size - 0x330); se_aes_crypt_cbc(13, 0, warmboot + 0x330, hdr_pk11->wb_size - 0x330, warmboot + 0x330, hdr_pk11->wb_size - 0x330);
emmcsn_path_impl(path, "/pkg1", "warmboot_dec.bin", &storage); emmcsn_path_impl(path, "/pkg1", "warmboot_dec.bin", &emmc_storage);
if (sd_save_to_file(warmboot, hdr_pk11->wb_size, path)) if (sd_save_to_file(warmboot, hdr_pk11->wb_size, path))
goto out_free; goto out_free;
} }
@ -1273,10 +1269,10 @@ static lv_res_t _create_window_dump_pk12_tool(lv_obj_t *btn)
} }
// Dump package2.1. // Dump package2.1.
sdmmc_storage_set_mmc_partition(&storage, EMMC_GPP); sdmmc_storage_set_mmc_partition(&emmc_storage, EMMC_GPP);
// Parse eMMC GPT. // Parse eMMC GPT.
LIST_INIT(gpt); LIST_INIT(gpt);
nx_emmc_gpt_parse(&gpt, &storage); nx_emmc_gpt_parse(&gpt, &emmc_storage);
// Find package2 partition. // Find package2 partition.
emmc_part_t *pkg2_part = nx_emmc_part_find(&gpt, "BCPKG2-1-Normal-Main"); emmc_part_t *pkg2_part = nx_emmc_part_find(&gpt, "BCPKG2-1-Normal-Main");
if (!pkg2_part) if (!pkg2_part)
@ -1284,17 +1280,17 @@ static lv_res_t _create_window_dump_pk12_tool(lv_obj_t *btn)
// Read in package2 header and get package2 real size. // Read in package2 header and get package2 real size.
u8 *tmp = (u8 *)malloc(NX_EMMC_BLOCKSIZE); u8 *tmp = (u8 *)malloc(NX_EMMC_BLOCKSIZE);
nx_emmc_part_read(&storage, pkg2_part, 0x4000 / NX_EMMC_BLOCKSIZE, 1, tmp); nx_emmc_part_read(&emmc_storage, pkg2_part, 0x4000 / NX_EMMC_BLOCKSIZE, 1, tmp);
u32 *hdr_pkg2_raw = (u32 *)(tmp + 0x100); u32 *hdr_pkg2_raw = (u32 *)(tmp + 0x100);
u32 pkg2_size = hdr_pkg2_raw[0] ^ hdr_pkg2_raw[2] ^ hdr_pkg2_raw[3]; u32 pkg2_size = hdr_pkg2_raw[0] ^ hdr_pkg2_raw[2] ^ hdr_pkg2_raw[3];
free(tmp); free(tmp);
// Read in package2. // Read in package2.
u32 pkg2_size_aligned = ALIGN(pkg2_size, NX_EMMC_BLOCKSIZE); u32 pkg2_size_aligned = ALIGN(pkg2_size, NX_EMMC_BLOCKSIZE);
pkg2 = malloc(pkg2_size_aligned); pkg2 = malloc(pkg2_size_aligned);
nx_emmc_part_read(&storage, pkg2_part, 0x4000 / NX_EMMC_BLOCKSIZE, nx_emmc_part_read(&emmc_storage, pkg2_part, 0x4000 / NX_EMMC_BLOCKSIZE,
pkg2_size_aligned / NX_EMMC_BLOCKSIZE, pkg2); pkg2_size_aligned / NX_EMMC_BLOCKSIZE, pkg2);
#if 0 #if 0
emmcsn_path_impl(path, "/pkg2", "pkg2_encr.bin", &storage); emmcsn_path_impl(path, "/pkg2", "pkg2_encr.bin", &emmc_storage);
if (sd_save_to_file(pkg2, pkg2_size_aligned, path)) if (sd_save_to_file(pkg2, pkg2_size_aligned, path))
goto out; goto out;
gfx_puts("\npkg2 dumped to pkg2_encr.bin\n"); gfx_puts("\npkg2 dumped to pkg2_encr.bin\n");
@ -1326,7 +1322,7 @@ static lv_res_t _create_window_dump_pk12_tool(lv_obj_t *btn)
manual_system_maintenance(true); manual_system_maintenance(true);
// Dump pkg2.1. // Dump pkg2.1.
emmcsn_path_impl(path, "/pkg2", "pkg2_decr.bin", &storage); emmcsn_path_impl(path, "/pkg2", "pkg2_decr.bin", &emmc_storage);
if (sd_save_to_file(pkg2, pkg2_hdr->sec_size[PKG2_SEC_KERNEL] + pkg2_hdr->sec_size[PKG2_SEC_INI1], path)) if (sd_save_to_file(pkg2, pkg2_hdr->sec_size[PKG2_SEC_KERNEL] + pkg2_hdr->sec_size[PKG2_SEC_INI1], path))
goto out; goto out;
strcat(txt_buf, "pkg2 dumped to pkg2_decr.bin\n"); strcat(txt_buf, "pkg2 dumped to pkg2_decr.bin\n");
@ -1334,7 +1330,7 @@ static lv_res_t _create_window_dump_pk12_tool(lv_obj_t *btn)
manual_system_maintenance(true); manual_system_maintenance(true);
// Dump kernel. // Dump kernel.
emmcsn_path_impl(path, "/pkg2", "kernel.bin", &storage); emmcsn_path_impl(path, "/pkg2", "kernel.bin", &emmc_storage);
if (sd_save_to_file(pkg2_hdr->data, pkg2_hdr->sec_size[PKG2_SEC_KERNEL], path)) if (sd_save_to_file(pkg2_hdr->data, pkg2_hdr->sec_size[PKG2_SEC_KERNEL], path))
goto out; goto out;
strcat(txt_buf, "Kernel dumped to kernel.bin\n"); strcat(txt_buf, "Kernel dumped to kernel.bin\n");
@ -1358,7 +1354,7 @@ static lv_res_t _create_window_dump_pk12_tool(lv_obj_t *btn)
} }
pkg2_ini1_t *ini1 = (pkg2_ini1_t *)(pkg2_hdr->data + ini1_off); pkg2_ini1_t *ini1 = (pkg2_ini1_t *)(pkg2_hdr->data + ini1_off);
emmcsn_path_impl(path, "/pkg2", "ini1.bin", &storage); emmcsn_path_impl(path, "/pkg2", "ini1.bin", &emmc_storage);
if (sd_save_to_file(ini1, ini1_size, path)) if (sd_save_to_file(ini1, ini1_size, path))
goto out; goto out;
@ -1385,7 +1381,7 @@ static lv_res_t _create_window_dump_pk12_tool(lv_obj_t *btn)
kip1 = (pkg2_kip1_t *)kip_buffer; kip1 = (pkg2_kip1_t *)kip_buffer;
} }
emmcsn_path_impl(path, "/pkg2/ini1", filename, &storage); emmcsn_path_impl(path, "/pkg2/ini1", filename, &emmc_storage);
if (sd_save_to_file(kip1, kip1_size, path)) if (sd_save_to_file(kip1, kip1_size, path))
{ {
free(kip_buffer); free(kip_buffer);
@ -1409,7 +1405,7 @@ out_free:
free(loader); free(loader);
free(pkg2); free(pkg2);
free(txt_buf); free(txt_buf);
sdmmc_storage_end(&storage); sdmmc_storage_end(&emmc_storage);
sd_unmount(); sd_unmount();
if (kb >= KB_FIRMWARE_VERSION_620) if (kb >= KB_FIRMWARE_VERSION_620)