From 97bbe2a3333fce25b656b753c8e5484045322879 Mon Sep 17 00:00:00 2001 From: "Such Meme, Many Skill" Date: Sat, 4 Apr 2020 22:15:13 +0200 Subject: [PATCH] Add partition restoring via clipboard --- source/tegraexplorer/common/common.h | 4 +- source/tegraexplorer/common/strings.c | 4 +- source/tegraexplorer/common/structs.c | 2 +- source/tegraexplorer/emmc/emmcmenu.c | 129 ++++++++++++++++----- source/tegraexplorer/emmc/emmcoperations.h | 4 +- source/tegraexplorer/emmc/emmcrestorebis.c | 1 + source/tegraexplorer/gfx/gfxutils.c | 2 +- 7 files changed, 111 insertions(+), 35 deletions(-) diff --git a/source/tegraexplorer/common/common.h b/source/tegraexplorer/common/common.h index c8d1947..b0e97d0 100644 --- a/source/tegraexplorer/common/common.h +++ b/source/tegraexplorer/common/common.h @@ -21,7 +21,9 @@ enum utils_err_codes_te_call { ERR_EMMC_WRITE_FAILED, ERR_FILE_TOO_BIG_FOR_DEST, ERR_SD_EJECTED, - ERR_PARSE_FAIL + ERR_PARSE_FAIL, + ERR_CANNOT_COPY_FILE_TO_FS_PART, + ERR_NO_DESTENATION }; extern const char *utils_err_codes_te[]; diff --git a/source/tegraexplorer/common/strings.c b/source/tegraexplorer/common/strings.c index 5d25aee..86297f6 100644 --- a/source/tegraexplorer/common/strings.c +++ b/source/tegraexplorer/common/strings.c @@ -50,7 +50,9 @@ const char *utils_err_codes_te[] = { // these start at 50 "EMMC WRITE FAILED", "FILE TOO BIG FOR DEST", "SD EJECTED", - "PARSING FAILED" + "PARSING FAILED", + "CANNOT COPY FILE TO FS PART", + "NO DESTENATION" }; const char *pkg2names[] = { diff --git a/source/tegraexplorer/common/structs.c b/source/tegraexplorer/common/structs.c index 6fa0f43..89fc0ff 100644 --- a/source/tegraexplorer/common/structs.c +++ b/source/tegraexplorer/common/structs.c @@ -81,7 +81,7 @@ gpt_entry_rule gpt_fs_rules[] = { menu_entry mmcmenu_start[] = { {"Back", COLOR_ORANGE, ISMENU}, - {"RESERVED\n", COLOR_ORANGE, ISMENU}, + {"Clipboard -> Partition\n", COLOR_ORANGE, ISMENU}, {"BOOT0/1", COLOR_BLUE, isBOOT | ISMENU} }; diff --git a/source/tegraexplorer/emmc/emmcmenu.c b/source/tegraexplorer/emmc/emmcmenu.c index 080f19e..e5357cf 100644 --- a/source/tegraexplorer/emmc/emmcmenu.c +++ b/source/tegraexplorer/emmc/emmcmenu.c @@ -33,6 +33,10 @@ #include "../fs/fsutils.h" menu_entry *mmcMenuEntries = NULL; +extern sdmmc_storage_t storage; +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++){ @@ -98,6 +102,90 @@ int fillMmcMenu(short mmcType){ return count; } +int handleEntries(short mmcType, menu_entry part){ + if (part.property & ISDIR){ + if (!mount_mmc(part.name, part.storage)) + fileexplorer("emmc:/", 1); + } + else { + if (mmcmenu_filemenu[1].name != NULL) + free(mmcmenu_filemenu[1].name); + + utils_copystring(part.name, &mmcmenu_filemenu[1].name); + + if ((menu_make(mmcmenu_filemenu, 4, "-- RAW PARTITION --")) < 3) + return 0; + + if (part.property & isBOOT){ + dump_emmc_parts(PART_BOOT, (u8)mmcType); + } + else { + f_mkdir("sd:/tegraexplorer"); + f_mkdir("sd:/tegraexplorer/partition_dumps"); + + gfx_clearscreen(); + gfx_printf("Dumping %s...\n", part.name); + + if (!dump_emmc_specific(part.name, fsutil_getnextloc("sd:/tegraexplorer/partition_dumps", part.name))){ + gfx_printf("\nDone!"); + btn_wait(); + } + } + } + + return 0; +} + +emmc_part_t *mmcFindPart(char *path, short mmcType){ + char *filename, *extention; + emmc_part_t *part; + filename = strrchr(path, '/') + 1; + extention = strrchr(path, '.'); + + if (extention != NULL) + *extention = '\0'; + + if (checkGptRules(filename)){ + gfx_errDisplay("mmcFindPart", ERR_CANNOT_COPY_FILE_TO_FS_PART, 1); + return NULL; + } + + part = nx_emmc_part_find(selectGpt(mmcType), filename); + + if (part != NULL){ + emummc_storage_set_mmc_partition(&storage, 0); + 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); + return part; + } + + //gfx_printf("Path: %s\nFilename: %s", path, filename); + //btn_wait(); + gfx_errDisplay("mmcFindPart", ERR_NO_DESTENATION, 2); + 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); + } + clipboardhelper = 0; + return 1; +} + int makeMmcMenu(short mmcType){ int count, selection; count = fillMmcMenu(mmcType); @@ -109,38 +197,19 @@ int makeMmcMenu(short mmcType){ case 0: return 0; case 1: + if (!(clipboardhelper & ISDIR) && (clipboardhelper & OPERATIONCOPY)){ + gfx_clearscreen(); + if (!mmcFlashFile(clipboard, mmcType)){ + gfx_printf("\nDone!"); + btn_wait(); + } + } + else + gfx_errDisplay("mmcMenu", ERR_EMPTY_CLIPBOARD, 0); break; //stubbed default: - if (mmcMenuEntries[selection].property & ISDIR){ - if (!mount_mmc(mmcMenuEntries[selection].name, mmcMenuEntries[selection].storage)) - fileexplorer("emmc:/", 1); - } - else { - if (mmcmenu_filemenu[1].name != NULL) - free(mmcmenu_filemenu[1].name); - - utils_copystring(mmcMenuEntries[selection].name, &mmcmenu_filemenu[1].name); - - if ((menu_make(mmcmenu_filemenu, 4, "-- RAW PARTITION --")) < 3){ - break; - } - - if (mmcMenuEntries[selection].property & isBOOT){ - dump_emmc_parts(PART_BOOT, (u8)mmcType); - } - else { - f_mkdir("sd:/tegraexplorer"); - f_mkdir("sd:/tegraexplorer/partition_dumps"); - - gfx_clearscreen(); - gfx_printf("Dumping %s...\n", mmcMenuEntries[selection].name); - - if (!dump_emmc_specific(mmcMenuEntries[selection].name, fsutil_getnextloc("sd:/tegraexplorer/partition_dumps", mmcMenuEntries[selection].name))){ - gfx_printf("\nDone!"); - btn_wait(); - } - } - } + handleEntries(mmcType, mmcMenuEntries[selection]); + break; } } } \ No newline at end of file diff --git a/source/tegraexplorer/emmc/emmcoperations.h b/source/tegraexplorer/emmc/emmcoperations.h index 64eb784..a616860 100644 --- a/source/tegraexplorer/emmc/emmcoperations.h +++ b/source/tegraexplorer/emmc/emmcoperations.h @@ -1,6 +1,8 @@ #pragma once #include "../../utils/types.h" +#include "../../storage/nx_emmc.h" int dump_emmc_parts(u16 parts, u8 mmctype); int restore_bis_using_file(char *path, u8 mmctype); -int dump_emmc_specific(char *part, char *path); \ No newline at end of file +int dump_emmc_specific(char *part, char *path); +int restore_emmc_part(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/emmcrestorebis.c index 10ddefd..176d4ba 100644 --- a/source/tegraexplorer/emmc/emmcrestorebis.c +++ b/source/tegraexplorer/emmc/emmcrestorebis.c @@ -105,6 +105,7 @@ int restore_emmc_part(char *path, sdmmc_storage_t *mmcstorage, emmc_part_t *part return 0; } +// 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); diff --git a/source/tegraexplorer/gfx/gfxutils.c b/source/tegraexplorer/gfx/gfxutils.c index 72a04f9..cf58a7a 100644 --- a/source/tegraexplorer/gfx/gfxutils.c +++ b/source/tegraexplorer/gfx/gfxutils.c @@ -54,7 +54,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 >= ERR_SAME_LOC && err <= ERR_PARSE_FAIL) + else if (err >= ERR_SAME_LOC && err <= ERR_NO_DESTENATION) gfx_printf("Desc: %s\n", utils_err_codes_te[err - 50]); if (loc)