diff --git a/source/tegraexplorer/emmc.c b/source/tegraexplorer/emmc.c index e177301..0799156 100644 --- a/source/tegraexplorer/emmc.c +++ b/source/tegraexplorer/emmc.c @@ -70,6 +70,14 @@ int mount_emmc(char *partition, int biskeynumb){ return 0; } +short returnpkg1ver(){ + return pkg1ver; +} + +void disconnect_emmc(){ + sdmmc_storage_end(&storage); +} + int dump_biskeys(){ u8 temp_key[0x10], device_key[0x10] = {0}; tsec_ctxt_t tsec_ctxt; diff --git a/source/tegraexplorer/emmc.h b/source/tegraexplorer/emmc.h index 5d68209..751f283 100644 --- a/source/tegraexplorer/emmc.h +++ b/source/tegraexplorer/emmc.h @@ -4,6 +4,8 @@ int dump_biskeys(); void print_biskeys(); int mount_emmc(char *partition, int biskeynumb); +short returnpkg1ver(); +void disconnect_emmc(); static const u8 zeros[0x10] = {0}; diff --git a/source/tegraexplorer/fs.c b/source/tegraexplorer/fs.c index 91f406e..18468d2 100644 --- a/source/tegraexplorer/fs.c +++ b/source/tegraexplorer/fs.c @@ -295,7 +295,7 @@ int readfolder(const char *path){ message(errmes, COLOR_RED); } - while (!f_readdir(&dir, &fno) && fno.fname[0]){ + while (!f_readdir(&dir, &fno) && fno.fname[0] && folderamount < 500){ addobject(fno.fname, folderamount++, (fno.fattrib & AM_DIR), (fno.fattrib & AM_ARC)); } diff --git a/source/tegraexplorer/fs.h b/source/tegraexplorer/fs.h index 94f8641..a44619e 100644 --- a/source/tegraexplorer/fs.h +++ b/source/tegraexplorer/fs.h @@ -40,4 +40,5 @@ enum filemenuoptions { int readfolder(const char *path); void filemenu(); bool checkfile(char* path); -u64 getfilesize(char *path); \ No newline at end of file +u64 getfilesize(char *path); +int copy(const char *locin, const char *locout, bool print); \ No newline at end of file diff --git a/source/tegraexplorer/gfx.c b/source/tegraexplorer/gfx.c index cc73e60..94c1f82 100644 --- a/source/tegraexplorer/gfx.c +++ b/source/tegraexplorer/gfx.c @@ -99,7 +99,7 @@ int makewaitmenu(char *initialmessage, char *hiddenmessage, int timer){ return 0; if (start + timer > get_tmr_s()) - gfx_printf("\r", timer + start - get_tmr_s()); + gfx_printf("\r ", timer + start - get_tmr_s()); else if (res & BTN_POWER) return 1; diff --git a/source/tegraexplorer/te.c b/source/tegraexplorer/te.c index 05e1321..7e2073a 100644 --- a/source/tegraexplorer/te.c +++ b/source/tegraexplorer/te.c @@ -37,8 +37,15 @@ menu_item toolsmenu[5] = { {"-- TOOLS --\n", COLOR_VIOLET, -1, 0}, {"Back", COLOR_WHITE, -1, 1}, {"\nDisplay Console Info", COLOR_GREEN, DISPLAY_INFO, 1}, - {"Display GPIO pins [DEV]", COLOR_RED, DISPLAY_GPIO, 1}, - {"FORMAT TEST", COLOR_RED, FORMATFAT32, 1} + {"Display GPIO pins", COLOR_VIOLET, DISPLAY_GPIO, 1}, + {"Dump Firmware", COLOR_BLUE, DUMPFIRMWARE, 1} +}; + +menu_item formatmenu[4] = { + {"-- FORMAT SD --\n", COLOR_RED, -1, 0}, + {"Back\n", COLOR_WHITE, -1, 1}, + {"Format to FAT32", COLOR_RED, FORMAT_ALL_FAT32, 1}, + {"Format for EmuMMC setup", COLOR_RED, FORMAT_EMUMMC, 1} }; void fillmainmenu(){ @@ -111,17 +118,34 @@ void te_main(){ case TOOLS: res = makemenu(toolsmenu, 5); - if (res == DISPLAY_INFO) - displayinfo(); - - if (res == DISPLAY_GPIO) - displaygpio(); - - if (res == FORMATFAT32) - format(); - + switch(res){ + case DISPLAY_INFO: + displayinfo(); + break; + case DISPLAY_GPIO: + displaygpio(); + break; + case DUMPFIRMWARE: + dumpfirmware(); + break; + } break; + case SD_FORMAT: + res = makemenu(formatmenu, 4); + + switch(res){ + case FORMAT_ALL_FAT32: + if (makewaitmenu("Are you sure you want to format your sd?\nThis action is irreversible!\n\nPress Vol+/- to cancel\n", "Press Power to continue", 10)) + format(1); + break; + case FORMAT_EMUMMC: + if (makewaitmenu("Are you sure you want to format your sd?\nThis action is irreversible!\n\nPress Vol+/- to cancel\n", "Press Power to continue", 10)) + format(0); + break; + } + break; + case CREDITS: message(CREDITS_MESSAGE, COLOR_WHITE); break; diff --git a/source/tegraexplorer/te.h b/source/tegraexplorer/te.h index 72e83d9..d491c11 100644 --- a/source/tegraexplorer/te.h +++ b/source/tegraexplorer/te.h @@ -33,7 +33,12 @@ enum shutdownmenu_return { enum toolsmenu_return { DISPLAY_INFO = 1, DISPLAY_GPIO, - FORMATFAT32 + DUMPFIRMWARE +}; + +enum formatmenu_return { + FORMAT_ALL_FAT32, + FORMAT_EMUMMC }; //menu_item mainmenu[MAINMENU_AMOUNT]; diff --git a/source/tegraexplorer/tools.c b/source/tegraexplorer/tools.c index 3d83f3d..5b0e3bf 100644 --- a/source/tegraexplorer/tools.c +++ b/source/tegraexplorer/tools.c @@ -8,7 +8,9 @@ #include "../utils/types.h" #include "../libs/fatfs/diskio.h" #include "../storage/sdmmc.h" +#include "../utils/sprintf.h" #include "emmc.h" +#include "fs.h" extern bool sd_mount(); extern void sd_unmount(); @@ -17,8 +19,6 @@ extern sdmmc_storage_t sd_storage; void displayinfo(){ clearscreen(); - - FATFS *fs; DWORD fre_clust, fre_sect, tot_sect; u32 capacity; @@ -69,31 +69,95 @@ void displaygpio(){ } } -void format(){ +int dumpfirmware(){ + DIR dir; + FILINFO fno; + bool fail = false; + int ret, amount = 0; + char path[100] = "emmc:/Contents/registered"; + char sdfolderpath[100] = ""; + char syspath[100] = ""; + char sdpath[100] = ""; + short pkg1ver = returnpkg1ver(); + u32 timer = get_tmr_s(); + + clearscreen(); + + gfx_printf("PKG1 version: %d\n", pkg1ver); + + ret = f_mkdir("sd:/tegraexplorer"); + gfx_printf("Creating making sd:/tegraexplorer %d\n", ret); + + ret = f_mkdir("sd:/tegraexplorer/Firmware"); + gfx_printf("Creating sd:/tegraexplorer/Firmware %d\n", ret); + + sprintf(sdfolderpath, "sd:/tegraexplorer/Firmware/%d", pkg1ver); + ret = f_mkdir(sdfolderpath); + gfx_printf("Creating %s %d\n", sdfolderpath, ret); + + ret = f_opendir(&dir, path); + gfx_printf("Result opening system:/ %d\n\n%k", ret, COLOR_GREEN); + + while(!f_readdir(&dir, &fno) && fno.fname[0] && !fail){ + sprintf(sdpath, "%s/%s", sdfolderpath, fno.fname); + + if (fno.fattrib & AM_DIR) + sprintf(syspath, "%s/%s/00", path, fno.fname); + else + sprintf(syspath, "%s/%s", path, fno.fname); + + ret = copy(syspath, sdpath, false); + + gfx_printf("%d %s\r", ++amount, fno.fname); + + if (ret != 0) + fail = true; + } + + if (fail) + gfx_printf("%k\n\nDump failed! Aborting (%d)", COLOR_RED, ret); + + gfx_printf("%k\n\nPress any button to continue...\nTime taken: %ds", COLOR_WHITE, get_tmr_s() - timer); + + btn_wait(); + + return fail; +} + +void format(int mode){ clearscreen(); int res; bool fatalerror = false; - + DWORD plist[] = {666, 61145088}; u32 timer, totalsectors; BYTE work[FF_MAX_SS]; - DWORD plist[] = {666, 61145088}; DWORD clustsize = 16 * 512; BYTE formatoptions = 0; formatoptions |= (FM_FAT32); //formatoptions |= (FM_SFD); - timer = get_tmr_s(); + disconnect_emmc(); + timer = get_tmr_s(); totalsectors = sd_storage.csd.capacity; - if (totalsectors < 61145088){ - gfx_printf("%k\nNot enough free space for emummc!", COLOR_RED); - fatalerror = true; + if (mode == 0){ + if (totalsectors < 61145088){ + gfx_printf("%k\nNot enough free space for emummc!", COLOR_RED); + fatalerror = true; + } + + if (!fatalerror){ + plist[0] = totalsectors - 61145088; + gfx_printf("\nStarting SD partitioning:\nTotalSectors: %d\nPartition1 (SD): %d\nPartition2 (EMUMMC): %d\n", totalsectors, plist[0], plist[1]); + } + } + else { + plist[0] = totalsectors; + plist[1] = 0; } if (!fatalerror){ - plist[0] = totalsectors - 61145088; - gfx_printf("\nStarting SD partitioning:\nTotalSectors: %d\nPartition1 (SD): %d\nPartition2 (EMUMMC): %d\n", totalsectors, plist[0], plist[1]); gfx_printf("\nPartitioning SD...\n"); res = f_fdisk(0, plist, &work); @@ -127,6 +191,9 @@ void format(){ } } + dump_biskeys(); + mount_emmc("SYSTEM", 2); + gfx_printf("\nPress any button to return%k\nTotal time taken: %ds", COLOR_WHITE, (get_tmr_s() - timer)); btn_wait(); } \ No newline at end of file diff --git a/source/tegraexplorer/tools.h b/source/tegraexplorer/tools.h index a13c5f5..bdf9e65 100644 --- a/source/tegraexplorer/tools.h +++ b/source/tegraexplorer/tools.h @@ -2,4 +2,5 @@ void displayinfo(); void displaygpio(); -void format(); \ No newline at end of file +void format(int mode); +int dumpfirmware(); \ No newline at end of file