From 65519302333eb170b38fdeae3cbb026ca22526c3 Mon Sep 17 00:00:00 2001 From: "Such Meme, Many Skill" Date: Sun, 5 Apr 2020 16:07:25 +0200 Subject: [PATCH] cleanup of mmc restores/dumps --- source/tegraexplorer/common/common.h | 3 +- source/tegraexplorer/common/structs.c | 3 +- source/tegraexplorer/emmc/emmc.c | 8 +++ source/tegraexplorer/emmc/emmc.h | 1 + .../emmc/{emmcdumpbis.c => emmcdumppart.c} | 6 +- source/tegraexplorer/emmc/emmcmenu.c | 71 ------------------- source/tegraexplorer/emmc/emmcmenu.h | 3 +- source/tegraexplorer/emmc/emmcoperations.h | 8 ++- .../{emmcrestorebis.c => emmcrestorepart.c} | 63 +++++++++++++++- source/tegraexplorer/fs/filemenu.c | 6 +- source/tegraexplorer/fs/fsextractbis.c | 38 +++++++--- 11 files changed, 110 insertions(+), 100 deletions(-) rename source/tegraexplorer/emmc/{emmcdumpbis.c => emmcdumppart.c} (96%) rename source/tegraexplorer/emmc/{emmcrestorebis.c => emmcrestorepart.c} (74%) diff --git a/source/tegraexplorer/common/common.h b/source/tegraexplorer/common/common.h index 99df700..df681de 100644 --- a/source/tegraexplorer/common/common.h +++ b/source/tegraexplorer/common/common.h @@ -85,8 +85,7 @@ enum fs_menu_file_return { FILE_PAYLOAD, FILE_SCRIPT, FILE_HEXVIEW, - FILE_DUMPBIS, - FILE_RESTOREBIS + FILE_DUMPBIS }; extern menu_entry fs_menu_file[]; diff --git a/source/tegraexplorer/common/structs.c b/source/tegraexplorer/common/structs.c index acb2319..06592bc 100644 --- a/source/tegraexplorer/common/structs.c +++ b/source/tegraexplorer/common/structs.c @@ -52,8 +52,7 @@ menu_entry fs_menu_file[] = { {"Launch Payload", COLOR_ORANGE, ISMENU}, {"Launch Script", COLOR_YELLOW, ISMENU}, {"View Hex", COLOR_GREEN, ISMENU}, - {"\nExtract BIS", COLOR_YELLOW, ISMENU}, - {"Restore BIS", COLOR_RED, ISMENU} + {"\nExtract BIS", COLOR_YELLOW, ISMENU} }; menu_entry fs_menu_folder[] = { diff --git a/source/tegraexplorer/emmc/emmc.c b/source/tegraexplorer/emmc/emmc.c index 62cc751..29113aa 100644 --- a/source/tegraexplorer/emmc/emmc.c +++ b/source/tegraexplorer/emmc/emmc.c @@ -240,4 +240,12 @@ link_t *selectGpt(short mmcType){ return &emu_gpt; } return NULL; +} + +int checkGptRules(char *in){ + for (int i = 0; gpt_fs_rules[i].name != NULL; i++){ + if (!strcmp(in, gpt_fs_rules[i].name)) + return gpt_fs_rules[i].property; + } + return 0; } \ No newline at end of file diff --git a/source/tegraexplorer/emmc/emmc.h b/source/tegraexplorer/emmc/emmc.h index 2bbb03c..94a3796 100644 --- a/source/tegraexplorer/emmc/emmc.h +++ b/source/tegraexplorer/emmc/emmc.h @@ -18,6 +18,7 @@ void disconnect_mmc(); int connect_part(const char *partition); void dumpEmuGpt(); link_t *selectGpt(short mmcType); +int checkGptRules(char *in); static const u8 zeros[0x10] = {0}; diff --git a/source/tegraexplorer/emmc/emmcdumpbis.c b/source/tegraexplorer/emmc/emmcdumppart.c similarity index 96% rename from source/tegraexplorer/emmc/emmcdumpbis.c rename to source/tegraexplorer/emmc/emmcdumppart.c index 36955dc..7fc02e6 100644 --- a/source/tegraexplorer/emmc/emmcdumpbis.c +++ b/source/tegraexplorer/emmc/emmcdumppart.c @@ -18,7 +18,7 @@ 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){ +int emmcDumpPart(char *path, sdmmc_storage_t *mmcstorage, emmc_part_t *part){ FIL fp; u8 *buf; u32 lba_curr = part->lba_start; @@ -158,7 +158,7 @@ int emmcDumpBoot(char *basePath){ gfx_printf("Dumping %s\n", bootPart.name); RESETCOLOR; - dump_emmc_part(path, &storage, &bootPart); + emmcDumpPart(path, &storage, &bootPart); free(path); } emummc_storage_set_mmc_partition(&storage, 0); @@ -178,7 +178,7 @@ int emmcDumpSpecific(char *part, char *path){ SWAPCOLOR(COLOR_VIOLET); gfx_printf("Dumping %s\n", part); RESETCOLOR; - dump_emmc_part(path, &storage, system_part); + emmcDumpPart(path, &storage, system_part); return 0; } \ No newline at end of file diff --git a/source/tegraexplorer/emmc/emmcmenu.c b/source/tegraexplorer/emmc/emmcmenu.c index 2f663a0..dcb4a98 100644 --- a/source/tegraexplorer/emmc/emmcmenu.c +++ b/source/tegraexplorer/emmc/emmcmenu.c @@ -3,21 +3,12 @@ #include "../../mem/heap.h" #include "../../utils/types.h" #include "../../libs/fatfs/ff.h" -#include "../../utils/sprintf.h" #include "../../utils/btn.h" #include "../../gfx/gfx.h" #include "../../utils/util.h" -#include "../../hos/pkg1.h" -#include "../../storage/sdmmc.h" #include "../../storage/nx_emmc.h" -#include "../../sec/tsec.h" -#include "../../soc/t210.h" -#include "../../soc/fuse.h" #include "../../mem/mc.h" -#include "../../sec/se.h" -#include "../../soc/hw_init.h" #include "../../mem/emc.h" -#include "../../mem/sdram.h" #include "../../storage/emummc.h" #include "../../config/config.h" #include "../common/common.h" @@ -38,13 +29,6 @@ extern emmc_part_t *system_part; extern char *clipboard; extern u8 clipboardhelper; -int checkGptRules(char *in){ - for (int i = 0; gpt_fs_rules[i].name != NULL; i++){ - if (!strcmp(in, gpt_fs_rules[i].name)) - return gpt_fs_rules[i].property; - } - return 0; -} void addEntry(emmc_part_t *part, u8 property, int spot){ if (mmcMenuEntries[spot].name != NULL){ @@ -140,61 +124,6 @@ int handleEntries(short mmcType, menu_entry part){ return 0; } -emmc_part_t *mmcFindPart(char *path, short mmcType){ - char *filename, *extention, *path_local; - emmc_part_t *part; - - utils_copystring(path, &path_local); - filename = strrchr(path_local, '/') + 1; - extention = strrchr(path_local, '.'); - - if (extention != NULL) - *extention = '\0'; - - if (checkGptRules(filename)){ - gfx_errDisplay("mmcFindPart", ERR_CANNOT_COPY_FILE_TO_FS_PART, 1); - free(path_local); - return NULL; - } - - part = nx_emmc_part_find(selectGpt(mmcType), filename); - - if (part != NULL){ - emummc_storage_set_mmc_partition(&storage, 0); - free(path_local); - return part; - } - - if (!strcmp(filename, "BOOT0") || !strcmp(filename, "BOOT1")){ - const u32 BOOT_PART_SIZE = storage.ext_csd.boot_mult << 17; - part = calloc(1, sizeof(emmc_part_t)); - - part->lba_start = 0; - part->lba_end = (BOOT_PART_SIZE / NX_EMMC_BLOCKSIZE) - 1; - - strcpy(part->name, filename); - - emummc_storage_set_mmc_partition(&storage, (!strcmp(filename, "BOOT0")) ? 1 : 2); - free(path_local); - return part; - } - - //gfx_printf("Path: %s\nFilename: %s", path, filename); - //btn_wait(); - gfx_errDisplay("mmcFindPart", ERR_NO_DESTENATION, 2); - free(path_local); - return NULL; -} - -int mmcFlashFile(char *path, short mmcType){ - emmc_part_t *part; - part = mmcFindPart(path, mmcType); - if (part != NULL){ - return restore_emmc_part(path, &storage, part); - } - return 1; -} - int makeMmcMenu(short mmcType){ int count, selection; count = fillMmcMenu(mmcType); diff --git a/source/tegraexplorer/emmc/emmcmenu.h b/source/tegraexplorer/emmc/emmcmenu.h index b5830f7..7f4e131 100644 --- a/source/tegraexplorer/emmc/emmcmenu.h +++ b/source/tegraexplorer/emmc/emmcmenu.h @@ -1,4 +1,3 @@ #pragma once -int makeMmcMenu(short mmcType); -int mmcFlashFile(char *path, short mmcType); \ No newline at end of file +int makeMmcMenu(short mmcType); \ No newline at end of file diff --git a/source/tegraexplorer/emmc/emmcoperations.h b/source/tegraexplorer/emmc/emmcoperations.h index 3fed9ae..81d7eb1 100644 --- a/source/tegraexplorer/emmc/emmcoperations.h +++ b/source/tegraexplorer/emmc/emmcoperations.h @@ -2,7 +2,9 @@ #include "../../utils/types.h" #include "../../storage/nx_emmc.h" -int restore_bis_using_file(char *path, u8 mmctype); int emmcDumpSpecific(char *part, char *path); -int restore_emmc_part(char *path, sdmmc_storage_t *mmcstorage, emmc_part_t *part); -int emmcDumpBoot(char *basePath); \ No newline at end of file +int emmcDumpBoot(char *basePath); +int mmcFlashFile(char *path, short mmcType); + +int emmcDumpPart(char *path, sdmmc_storage_t *mmcstorage, emmc_part_t *part); +int emmcRestorePart(char *path, sdmmc_storage_t *mmcstorage, emmc_part_t *part); \ No newline at end of file diff --git a/source/tegraexplorer/emmc/emmcrestorebis.c b/source/tegraexplorer/emmc/emmcrestorepart.c similarity index 74% rename from source/tegraexplorer/emmc/emmcrestorebis.c rename to source/tegraexplorer/emmc/emmcrestorepart.c index 176d4ba..272a8f4 100644 --- a/source/tegraexplorer/emmc/emmcrestorebis.c +++ b/source/tegraexplorer/emmc/emmcrestorepart.c @@ -18,7 +18,7 @@ extern sdmmc_storage_t storage; extern emmc_part_t *system_part; -int restore_emmc_part(char *path, sdmmc_storage_t *mmcstorage, emmc_part_t *part){ +int emmcRestorePart(char *path, sdmmc_storage_t *mmcstorage, emmc_part_t *part){ FIL fp; FILINFO fno; u8 *buf; @@ -106,6 +106,7 @@ int restore_emmc_part(char *path, sdmmc_storage_t *mmcstorage, emmc_part_t *part } // function replaced by new mmc implementation. Will be removed at some point +/* int restore_emmc_file(char *path, const char *target, u8 partition, u8 mmctype){ connect_mmc(mmctype); @@ -165,4 +166,64 @@ int restore_bis_using_file(char *path, u8 mmctype){ btn_wait(); return 0; +} +*/ + +emmc_part_t *mmcFindPart(char *path, short mmcType){ + char *filename, *extention, *path_local; + emmc_part_t *part; + + utils_copystring(path, &path_local); + filename = strrchr(path_local, '/') + 1; + extention = strrchr(path_local, '.'); + + if (extention != NULL) + *extention = '\0'; + + if (checkGptRules(filename)){ + gfx_errDisplay("mmcFindPart", ERR_CANNOT_COPY_FILE_TO_FS_PART, 1); + free(path_local); + return NULL; + } + + part = nx_emmc_part_find(selectGpt(mmcType), filename); + + if (part != NULL){ + emummc_storage_set_mmc_partition(&storage, 0); + free(path_local); + return part; + } + + if (!strcmp(filename, "BOOT0") || !strcmp(filename, "BOOT1")){ + const u32 BOOT_PART_SIZE = storage.ext_csd.boot_mult << 17; + part = calloc(1, sizeof(emmc_part_t)); + + part->lba_start = 0; + part->lba_end = (BOOT_PART_SIZE / NX_EMMC_BLOCKSIZE) - 1; + + strcpy(part->name, filename); + + emummc_storage_set_mmc_partition(&storage, (!strcmp(filename, "BOOT0")) ? 1 : 2); + free(path_local); + return part; + } + + //gfx_printf("Path: %s\nFilename: %s", path, filename); + //btn_wait(); + gfx_errDisplay("mmcFindPart", ERR_NO_DESTENATION, 2); + free(path_local); + return NULL; +} + +int mmcFlashFile(char *path, short mmcType){ + emmc_part_t *part; + int res; + + part = mmcFindPart(path, mmcType); + if (part != NULL){ + res = emmcRestorePart(path, &storage, part); + emummc_storage_set_mmc_partition(&storage, 0); + return res; + } + return 1; } \ No newline at end of file diff --git a/source/tegraexplorer/fs/filemenu.c b/source/tegraexplorer/fs/filemenu.c index ee1dbf7..bdb8503 100644 --- a/source/tegraexplorer/fs/filemenu.c +++ b/source/tegraexplorer/fs/filemenu.c @@ -146,9 +146,8 @@ int filemenu(menu_entry file){ SETBIT(fs_menu_file[7].property, ISHIDE, !(strstr(file.name, ".bin") != NULL && file.property & ISKB)); SETBIT(fs_menu_file[8].property, ISHIDE, !(strstr(file.name, ".te") != NULL)); SETBIT(fs_menu_file[10].property, ISHIDE, !(strstr(file.name, ".bis") != NULL)); - SETBIT(fs_menu_file[11].property, ISHIDE, !(strstr(file.name, ".bis") != NULL)); - temp = menu_make(fs_menu_file, 12, "-- File Menu --"); + temp = menu_make(fs_menu_file, 11, "-- File Menu --"); switch (temp){ case FILE_COPY: @@ -177,9 +176,6 @@ int filemenu(menu_entry file){ fsreader_readfolder(currentpath); btn_wait(); break; - case FILE_RESTOREBIS: - restore_bis_using_file(fsutil_getnextloc(currentpath, file.name), SYSMMC); - break; case -1: return -1; } diff --git a/source/tegraexplorer/fs/fsextractbis.c b/source/tegraexplorer/fs/fsextractbis.c index 5e50b91..a40a975 100644 --- a/source/tegraexplorer/fs/fsextractbis.c +++ b/source/tegraexplorer/fs/fsextractbis.c @@ -12,6 +12,7 @@ #include "../../storage/nx_emmc.h" #include "../common/types.h" #include "../utils/utils.h" +#include "fsactions.h" u32 DecodeInt(u8* data) { u32 out = 0; @@ -55,6 +56,7 @@ int extract_bis_file(char *path, char *outfolder){ u8 args; u8 temp[0x4]; u32 filesizes[4]; + char *tempPath; if ((res = f_open(&in, path, FA_READ | FA_OPEN_EXISTING))){ gfx_errDisplay("extract_bis_file", res, 0); @@ -71,21 +73,35 @@ int extract_bis_file(char *path, char *outfolder){ gfx_printf("Version: %s\n\n", version); - gfx_printf("\nExtracting BOOT0\n"); - if (args & BOOT0_ARG) + if (args & BOOT0_ARG){ + gfx_printf("\nExtracting BOOT0\n"); gen_part(filesizes[0], &in, fsutil_getnextloc(outfolder, "BOOT0.bin")); - - gfx_printf("Extracting BOOT1\n"); - if (args & BOOT1_ARG) + } + + if (args & BOOT1_ARG){ + gfx_printf("Extracting BOOT1\n"); gen_part(filesizes[1], &in, fsutil_getnextloc(outfolder, "BOOT1.bin")); + } - gfx_printf("Extracting BCPKG2_1\n"); - if (args & BCPKG2_1_ARG) - gen_part(filesizes[2], &in, fsutil_getnextloc(outfolder, "BCPKG2-1-Normal-Main")); + if (args & BCPKG2_1_ARG){ + utils_copystring(fsutil_getnextloc(outfolder, "BCPKG2-1-Normal-Main"), &tempPath); + gfx_printf("Extracting BCPKG2_1/2\n"); - gfx_printf("Extracting BCPKG2_3\n"); - if (args & BCPKG2_3_ARG) - gen_part(filesizes[3], &in, fsutil_getnextloc(outfolder, "BCPKG2-3-SafeMode-Main")); + gen_part(filesizes[2], &in, tempPath); + fsact_copy(tempPath, fsutil_getnextloc(outfolder, "BCPKG2-2-Normal-Sub"), COPY_MODE_PRINT); + RESETCOLOR; + free(tempPath); + } + + if (args & BCPKG2_3_ARG){ + utils_copystring(fsutil_getnextloc(outfolder, "BCPKG2-3-SafeMode-Main"), &tempPath); + gfx_printf("Extracting BCPKG2_3/4\n"); + + gen_part(filesizes[3], &in, tempPath); + fsact_copy(tempPath, fsutil_getnextloc(outfolder, "BCPKG2-4-SafeMode-Sub"), COPY_MODE_PRINT); + RESETCOLOR; + free(tempPath); + } gfx_printf("\n\nDone!\n");