From 2d6411f60a1d784944729107b6c23d56ae71f19f Mon Sep 17 00:00:00 2001 From: "Such Meme, Many Skill" Date: Thu, 19 Mar 2020 13:00:42 +0100 Subject: [PATCH] Add more general error codes & re-implement bis dumping --- source/tegraexplorer/common/common.h | 7 +- source/tegraexplorer/common/strings.c | 19 +++- source/tegraexplorer/common/types.h | 8 ++ source/tegraexplorer/emmc/emmc.c | 8 +- source/tegraexplorer/emmc/emmcdumpbis.c | 120 +++++++++++++++++++++ source/tegraexplorer/emmc/emmcoperations.h | 4 + source/tegraexplorer/gfx/gfxutils.c | 2 +- source/tegraexplorer/gfx/menu.c | 16 ++- source/tegraexplorer/mainmenu.c | 10 +- 9 files changed, 175 insertions(+), 19 deletions(-) create mode 100644 source/tegraexplorer/emmc/emmcdumpbis.c create mode 100644 source/tegraexplorer/emmc/emmcoperations.h diff --git a/source/tegraexplorer/common/common.h b/source/tegraexplorer/common/common.h index 2c8e465..263e001 100644 --- a/source/tegraexplorer/common/common.h +++ b/source/tegraexplorer/common/common.h @@ -5,13 +5,18 @@ extern const char *gfx_file_size_names[]; extern const char *menu_sd_states[]; extern const char *emmc_fs_entries[]; extern const char *utils_err_codes[]; +extern const char *pkg2names[]; enum utils_err_codes_te_call { ERR_SAME_LOC = 50, ERR_DISK_WRITE_FAILED, ERR_EMPTY_CLIPBOARD, ERR_FOLDER_ROOT = 54, - ERR_DEST_PART_OF_SRC + ERR_DEST_PART_OF_SRC, + ERR_PART_NOT_FOUND, + ERR_BISKEY_DUMP_FAILED, + ERR_MEM_ALLOC_FAILED, + ERR_EMMC_READ_FAILED }; extern const char *utils_err_codes_te[]; diff --git a/source/tegraexplorer/common/strings.c b/source/tegraexplorer/common/strings.c index 2ce0917..df5c50b 100644 --- a/source/tegraexplorer/common/strings.c +++ b/source/tegraexplorer/common/strings.c @@ -40,7 +40,20 @@ const char *utils_err_codes_te[] = { // these start at 50 "SAME LOC", "DISK WRITE FAILED", "EMPTY CLIPBOARD", - "DEFENITION OF INSANITY" - "FOLDER ROOT" - "DEST PART OF SRC" + "DEFENITION OF INSANITY", + "FOLDER ROOT", + "DEST PART OF SRC", + "PART NOT FOUND", + "BISKEY DUMP FAILED", + "MEM ALLOC FAILED", + "EMMC READ FAILED" +}; + +const char *pkg2names[] = { + "BCPKG2-1-Normal-Main", + "BCPKG2-2-Normal-Sub", + "BCPKG2-3-SafeMode-Main", + "BCPKG2-4-SafeMode-Sub", + "BCPKG2-5-Repair-Main", + "BCPKG2-6-Repair-Sub" }; \ No newline at end of file diff --git a/source/tegraexplorer/common/types.h b/source/tegraexplorer/common/types.h index 031b809..05a9441 100644 --- a/source/tegraexplorer/common/types.h +++ b/source/tegraexplorer/common/types.h @@ -31,6 +31,14 @@ #define OPERATIONCOPY 0x2 #define OPERATIONMOVE 0x4 +#define PART_BOOT 0x1 +#define PART_PKG2 0x2 + +#define BOOT0_ARG 0x80 +#define BOOT1_ARG 0x40 +#define BCPKG2_1_ARG 0x20 +#define BCPKG2_3_ARG 0x10 + typedef struct { char *name; u32 storage; diff --git a/source/tegraexplorer/emmc/emmc.c b/source/tegraexplorer/emmc/emmc.c index 6d9b349..7485a92 100644 --- a/source/tegraexplorer/emmc/emmc.c +++ b/source/tegraexplorer/emmc/emmc.c @@ -21,6 +21,7 @@ #include "../../storage/emummc.h" #include "../../config/config.h" #include "../common/common.h" +#include "../gfx/gfxutils.h" sdmmc_storage_t storage; emmc_part_t *system_part; @@ -65,7 +66,7 @@ int connect_part(const char *partition){ system_part = nx_emmc_part_find(&gpt, partition); if (!system_part) { - gfx_printf("Failed to locate %s partition.", partition); + gfx_errDisplay("connect_mmc_part", ERR_PART_NOT_FOUND, 0); return -1; } @@ -73,6 +74,7 @@ int connect_part(const char *partition){ } int mount_mmc(const char *partition, const int biskeynumb){ + int res; f_unmount("emmc:"); se_aes_key_set(8, bis_key[biskeynumb] + 0x00, 0x10); @@ -81,8 +83,8 @@ int mount_mmc(const char *partition, const int biskeynumb){ if (connect_part(partition)) return -1; - if (f_mount(&emmc, "emmc:", 1)) { - gfx_printf("Mount failed of %s.", partition); + if ((res = f_mount(&emmc, "emmc:", 1))) { + gfx_errDisplay("mount_mmc", res, 0); return -1; } diff --git a/source/tegraexplorer/emmc/emmcdumpbis.c b/source/tegraexplorer/emmc/emmcdumpbis.c new file mode 100644 index 0000000..35f21a6 --- /dev/null +++ b/source/tegraexplorer/emmc/emmcdumpbis.c @@ -0,0 +1,120 @@ +#include +#include "emmcoperations.h" +#include "../../gfx/gfx.h" +#include "../gfx/gfxutils.h" +#include "../../utils/types.h" +#include "emmc.h" +#include "../../storage/emummc.h" +#include "../common/common.h" +#include "../../libs/fatfs/ff.h" +#include "../../utils/sprintf.h" +#include "../../utils/btn.h" +#include "../../mem/heap.h" +#include "../../storage/nx_emmc.h" +#include "../common/types.h" +#include "../utils/utils.h" +#include "../fs/fsutils.h" + +extern sdmmc_storage_t storage; +extern emmc_part_t *system_part; + +int dump_emmc_part(char *path, sdmmc_storage_t *mmcstorage, emmc_part_t *part){ + FIL fp; + u8 *buf; + u32 lba_curr = part->lba_start; + u32 bytesWritten = 0; + u32 totalSectors = part->lba_end - part->lba_start + 1; + u64 totalSize = (u64)((u64)totalSectors << 9); + u32 num = 0; + u32 pct = 0; + int res; + + gfx_printf("Initializing\r"); + buf = calloc(BUFSIZE, sizeof(u8)); + + if (!buf){ + gfx_errDisplay("dump_emmc_part", ERR_MEM_ALLOC_FAILED, 1); + return -1; + } + + if ((res = f_open(&fp, path, FA_CREATE_ALWAYS | FA_WRITE))){ + gfx_errDisplay("dump_emmc_part", res, 2); + return -1; + } + + f_lseek(&fp, totalSize); + f_lseek(&fp, 0); + + while (totalSectors > 0){ + num = MIN(totalSectors, 64); + if (!emummc_storage_read(mmcstorage, lba_curr, num, buf)){ + gfx_errDisplay("dump_emmc_part", ERR_EMMC_READ_FAILED, 3); + return -1; + } + if ((res = f_write(&fp, buf, num * NX_EMMC_BLOCKSIZE, NULL))){ + gfx_errDisplay("dump_emmc_part", res, 4); + return -1; + } + pct = (u64)((u64)(lba_curr - part->lba_start) * 100u) / (u64)(part->lba_end - part->lba_start); + gfx_printf("Progress: %d%%\r", pct); + + lba_curr += num; + totalSectors -= num; + bytesWritten += num * NX_EMMC_BLOCKSIZE; + } + gfx_printf(" \r"); + f_close(&fp); + free(buf); + return 0; +} + +int dump_emmc_parts(u16 parts, u8 mmctype){ + char *path; + char basepath[] = "sd:/tegraexplorer/dumps"; + f_mkdir("sd:/tegraexplorer"); + f_mkdir("sd:/tegraexplorer/dumps"); + + connect_mmc(mmctype); + gfx_clearscreen(); + + if (parts & PART_BOOT){ + emmc_part_t bootPart; + const u32 BOOT_PART_SIZE = storage.ext_csd.boot_mult << 17; + memset(&bootPart, 0, sizeof(bootPart)); + + bootPart.lba_start = 0; + bootPart.lba_end = (BOOT_PART_SIZE / NX_EMMC_BLOCKSIZE) - 1; + + for (int i = 0; i < 2; i++){ + strcpy(bootPart.name, "BOOT"); + bootPart.name[4] = (u8)('0' + i); + bootPart.name[5] = 0; + + emummc_storage_set_mmc_partition(&storage, i + 1); + utils_copystring(fsutil_getnextloc(basepath, bootPart.name), &path); + gfx_printf("Dumping %s\n", bootPart.name); + + dump_emmc_part(path, &storage, &bootPart); + free(path); + } + } + + if (parts & PART_PKG2){ + for (int i = 0; i < 6; i++){ + if (connect_part(pkg2names[i])){ + gfx_errDisplay("dump_emmc_parts", ERR_PART_NOT_FOUND, 0); + return -1; + } + + utils_copystring(fsutil_getnextloc(basepath, system_part->name), &path); + gfx_printf("Dumping %s\n", system_part->name); + + dump_emmc_part(path, &storage, system_part); + free(path); + } + } + + gfx_printf("\nDone!"); + btn_wait(); + return 0; +} \ No newline at end of file diff --git a/source/tegraexplorer/emmc/emmcoperations.h b/source/tegraexplorer/emmc/emmcoperations.h new file mode 100644 index 0000000..5a06cb5 --- /dev/null +++ b/source/tegraexplorer/emmc/emmcoperations.h @@ -0,0 +1,4 @@ +#pragma once +#include "../../utils/types.h" + +int dump_emmc_parts(u16 parts, u8 mmctype); \ No newline at end of file diff --git a/source/tegraexplorer/gfx/gfxutils.c b/source/tegraexplorer/gfx/gfxutils.c index d93f4e2..747d58c 100644 --- a/source/tegraexplorer/gfx/gfxutils.c +++ b/source/tegraexplorer/gfx/gfxutils.c @@ -50,7 +50,7 @@ int gfx_errDisplay(char *src_func, int err, int loc){ if (err < 15) gfx_printf("Desc: %s\n", utils_err_codes[err]); - else if (err >= 50 && err <= ERR_DEST_PART_OF_SRC) + else if (err >= 50 && err <= ERR_BISKEY_DUMP_FAILED) gfx_printf("Desc: %s\n", utils_err_codes_te[err - 50]); if (loc) diff --git a/source/tegraexplorer/gfx/menu.c b/source/tegraexplorer/gfx/menu.c index a37f4bb..d302a82 100644 --- a/source/tegraexplorer/gfx/menu.c +++ b/source/tegraexplorer/gfx/menu.c @@ -8,20 +8,25 @@ void _printentry(menu_entry entry, bool highlighted, bool refresh){ int size; - + u32 color = (entry.property & ISMENU) ? entry.storage : ((entry.property & ISDIR) ? COLOR_WHITE : COLOR_VIOLET); + /* if (entry.property & ISMENU) SWAPCOLOR(entry.storage); else if (entry.property & ISDIR) SWAPCOLOR(COLOR_WHITE); else { SWAPCOLOR(COLOR_VIOLET); + } + */ + if (!(entry.property & ISMENU && entry.property & ISDIR)){ for (size = 4; size < 8; size++) if ((entry.property & (1 << size))) break; - } - + } + + /* if (highlighted){ SWAPBGCOLOR(COLOR_WHITE); if ((entry.property & ISMENU) ? entry.storage == COLOR_WHITE : entry.property & ISDIR) @@ -29,6 +34,9 @@ void _printentry(menu_entry entry, bool highlighted, bool refresh){ } else SWAPBGCOLOR(COLOR_DEFAULT); + */ + SWAPCOLOR((highlighted) ? COLOR_DEFAULT : color); + SWAPBGCOLOR((highlighted) ? color : COLOR_DEFAULT); if (refresh) @@ -94,7 +102,7 @@ int menu_make(menu_entry *entries, int amount, char *toptext){ if (!(entries[i].property & ISHIDE)) _printentry(entries[i], (i == currentpos), refresh); - gfx_printf("\n%k%K %s %s\n\nTime taken for screen draw: %dms\n%d", COLOR_BLUE, COLOR_DEFAULT, (offset + 60 < amount) ? "v" : " ", (offset > 0) ? "^" : " ", get_tmr_ms() - timer, currentpos); + gfx_printf("\n%k%K %s %s\n\nTime taken for screen draw: %dms", COLOR_BLUE, COLOR_DEFAULT, (offset + 60 < amount) ? "v" : " ", (offset > 0) ? "^" : " ", get_tmr_ms() - timer); while (btn_read() & BTN_POWER); diff --git a/source/tegraexplorer/mainmenu.c b/source/tegraexplorer/mainmenu.c index 9561d51..8e45ba1 100644 --- a/source/tegraexplorer/mainmenu.c +++ b/source/tegraexplorer/mainmenu.c @@ -15,6 +15,7 @@ #include "gfx/gfxutils.h" #include "fs/fsutils.h" #include "fs/fsmenu.h" +#include "emmc/emmcoperations.h" extern bool sd_mount(); extern void sd_unmount(); @@ -88,8 +89,6 @@ void MainMenu_EMMC(){ if (!mount_mmc(emmc_fs_entries[res - 2], res - 1)) fileexplorer("emmc:/", 1); - else - gfx_message(COLOR_RED, "EMMC failed to mount!"); } } @@ -98,9 +97,6 @@ void MainMenu_EMUMMC(){ if (!mount_mmc(emmc_fs_entries[res - 5], res - 4)) fileexplorer("emmc:/", 1); - else - gfx_message(COLOR_RED, "EMUMMC failed to mount!"); - } void MainMenu_MountSD(){ @@ -130,7 +126,7 @@ void MainMenu_Tools(){ break; case TOOLS_DUMP_BOOT: - //dump_emmc_parts(PART_BOOT | PART_PKG2, SYSMMC); + dump_emmc_parts(PART_BOOT | PART_PKG2, SYSMMC); break; case TOOLS_RESTORE_BOOT: /* @@ -266,7 +262,7 @@ void te_main(){ int setter; if (dump_biskeys() == -1){ - gfx_message(COLOR_RED, "Biskeys failed to dump!\nEmmc will not be mounted!"); + gfx_errDisplay("dump_biskey", ERR_BISKEY_DUMP_FAILED, 0); for (int i = 1; i <= 3; i++) mainmenu_main[i].property |= ISHIDE; }