From 9d36f741d96093705acb1d787706c7b909f30baf Mon Sep 17 00:00:00 2001 From: "Such Meme, Many Skill" Date: Wed, 18 Mar 2020 23:58:32 +0100 Subject: [PATCH] Day2 of restructuring/rewriting --- source/tegraexplorer/common/common.h | 41 +- source/tegraexplorer/common/strings.c | 15 +- source/tegraexplorer/common/structs.c | 35 +- source/tegraexplorer/common/types.h | 7 + source/tegraexplorer/emmc.c | 2 - source/tegraexplorer/fs.c | 473 --------------------- source/tegraexplorer/fs.h | 51 --- source/tegraexplorer/fs/entrymenu.h | 7 + source/tegraexplorer/fs/filemenu.c | 185 ++++++++ source/tegraexplorer/fs/foldermenu.c | 89 ++++ source/tegraexplorer/fs/fsactions.c | 193 +++++++++ source/tegraexplorer/fs/fsactions.h | 6 + source/tegraexplorer/fs/fsmenu.c | 115 +++++ source/tegraexplorer/fs/fsmenu.h | 3 + source/tegraexplorer/fs/fsreader.c | 106 +++++ source/tegraexplorer/fs/fsreader.h | 7 + source/tegraexplorer/fs/fsutils.c | 90 ++++ source/tegraexplorer/fs/fsutils.h | 10 + source/tegraexplorer/gfx.c | 299 ------------- source/tegraexplorer/gfx.h | 17 - source/tegraexplorer/gfx/gfxutils.c | 16 +- source/tegraexplorer/gfx/gfxutils.h | 2 +- source/tegraexplorer/gfx/menu.c | 20 +- source/tegraexplorer/io.c | 590 -------------------------- source/tegraexplorer/io.h | 30 -- source/tegraexplorer/te.c | 46 +- source/tegraexplorer/utils/script.c | 11 +- source/tegraexplorer/utils/tools.c | 7 +- source/tegraexplorer/utils/utils.c | 8 + source/tegraexplorer/utils/utils.h | 3 +- 30 files changed, 981 insertions(+), 1503 deletions(-) delete mode 100644 source/tegraexplorer/fs.c delete mode 100644 source/tegraexplorer/fs.h create mode 100644 source/tegraexplorer/fs/entrymenu.h create mode 100644 source/tegraexplorer/fs/filemenu.c create mode 100644 source/tegraexplorer/fs/foldermenu.c create mode 100644 source/tegraexplorer/fs/fsactions.c create mode 100644 source/tegraexplorer/fs/fsactions.h create mode 100644 source/tegraexplorer/fs/fsmenu.c create mode 100644 source/tegraexplorer/fs/fsmenu.h create mode 100644 source/tegraexplorer/fs/fsreader.c create mode 100644 source/tegraexplorer/fs/fsreader.h create mode 100644 source/tegraexplorer/fs/fsutils.c create mode 100644 source/tegraexplorer/fs/fsutils.h delete mode 100644 source/tegraexplorer/gfx.c delete mode 100644 source/tegraexplorer/gfx.h delete mode 100644 source/tegraexplorer/io.c delete mode 100644 source/tegraexplorer/io.h diff --git a/source/tegraexplorer/common/common.h b/source/tegraexplorer/common/common.h index b971d72..2c8e465 100644 --- a/source/tegraexplorer/common/common.h +++ b/source/tegraexplorer/common/common.h @@ -6,6 +6,16 @@ extern const char *menu_sd_states[]; extern const char *emmc_fs_entries[]; extern const char *utils_err_codes[]; +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 +}; + +extern const char *utils_err_codes_te[]; + enum mainmenu_main_return { MAIN_SDCARD = 0, MAIN_EMMC_SAF, @@ -56,4 +66,33 @@ enum mmc_types { EMUMMC }; -extern menu_entry utils_mmcChoice[]; \ No newline at end of file +extern menu_entry utils_mmcChoice[]; + +enum fs_menu_file_return { + FILE_COPY = 4, + FILE_MOVE, + FILE_DELETE, + FILE_PAYLOAD, + FILE_SCRIPT, + FILE_HEXVIEW, + FILE_DUMPBIS, + FILE_RESTOREBIS +}; + +extern menu_entry fs_menu_file[]; + +enum fs_menu_folder_return { + DIR_EXITFOLDER = 2, + DIR_COPYFOLDER, + DIR_DELETEFOLDER +}; + +extern menu_entry fs_menu_folder[]; + +enum fs_menu_startdir_return { + FILEMENU_RETURN = 0, + FILEMENU_CLIPBOARD, + FILEMENU_CURFOLDER +}; + +extern menu_entry fs_menu_startdir[]; \ No newline at end of file diff --git a/source/tegraexplorer/common/strings.c b/source/tegraexplorer/common/strings.c index 05f1809..2ce0917 100644 --- a/source/tegraexplorer/common/strings.c +++ b/source/tegraexplorer/common/strings.c @@ -13,15 +13,15 @@ const char *menu_sd_states[] = { }; const char *emmc_fs_entries[] = { + "SAFE", "SYSTEM", - "USER", - "SAFE" + "USER" }; const char *utils_err_codes[] = { "OK", "I/O ERROR", - "DRIVE LOOKUP FAILED", + "NO DISK", "NOT READY", "NO FILE", "NO PATH", @@ -34,4 +34,13 @@ const char *utils_err_codes[] = { "NO MEM", "NO FAT", "MKFS ABORT" +}; + +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" }; \ No newline at end of file diff --git a/source/tegraexplorer/common/structs.c b/source/tegraexplorer/common/structs.c index e3cef82..05c87dd 100644 --- a/source/tegraexplorer/common/structs.c +++ b/source/tegraexplorer/common/structs.c @@ -3,12 +3,12 @@ menu_entry mainmenu_main[] = { {"[SD:/] SD CARD\n", COLOR_GREEN, ISMENU}, + {"[SAFE:/] EMMC", COLOR_ORANGE, ISMENU}, {"[SYSTEM:/] EMMC", COLOR_ORANGE, ISMENU}, {"[USER:/] EMMC", COLOR_ORANGE, ISMENU}, - {"[SAFE:/] EMMC", COLOR_ORANGE, ISMENU}, - {"\n[SYSTEM:/] EMUMMC", COLOR_BLUE, ISMENU}, + {"\n[SAFE:/] EMUMMC", COLOR_BLUE, ISMENU}, + {"[SYSTEM:/] EMUMMC", COLOR_BLUE, ISMENU}, {"[USER:/] EMUMMC", COLOR_BLUE, ISMENU}, - {"[SAFE:/] EMUMMC", COLOR_BLUE, ISMENU}, {"\nMount/Unmount SD", COLOR_WHITE, ISMENU}, {"Tools", COLOR_VIOLET, ISMENU}, {"SD format", COLOR_VIOLET, ISMENU}, @@ -45,4 +45,33 @@ menu_entry utils_mmcChoice[] = { {"Back\n", COLOR_WHITE, ISMENU}, {"SysMMC", COLOR_ORANGE, ISMENU}, {"EmuMMC", COLOR_BLUE, ISMENU} +}; + +menu_entry fs_menu_file[] = { + {NULL, COLOR_GREEN, ISMENU | ISSKIP}, + {NULL, COLOR_VIOLET, ISMENU | ISSKIP}, + {NULL, COLOR_VIOLET, ISMENU | ISSKIP}, + {"\n\n\nBack", COLOR_WHITE, ISMENU}, + {"\nCopy to clipboard", COLOR_BLUE, ISMENU}, + {"Move to clipboard", COLOR_BLUE, ISMENU}, + {"\nDelete file\n", COLOR_RED, ISMENU}, + {"Launch Payload", COLOR_ORANGE, ISMENU}, + {"Launch Script", COLOR_YELLOW, ISMENU}, + {"View Hex", COLOR_GREEN, ISMENU}, + {"Extract BIS", COLOR_RED, ISMENU}, + {"Restore BIS", COLOR_RED, ISMENU} +}; + +menu_entry fs_menu_folder[] = { + {NULL, COLOR_VIOLET, ISMENU | ISSKIP}, + {"\nBack", COLOR_WHITE, ISMENU}, + {"Return to main menu\n", COLOR_BLUE, ISMENU}, + {"Copy to clipboard", COLOR_VIOLET, ISMENU}, + {"Delete folder", COLOR_RED, ISMENU} +}; + +menu_entry fs_menu_startdir[] = { + {"Folder -> previous folder ", COLOR_ORANGE, ISMENU}, + {"Clipboard -> Current folder ", COLOR_ORANGE, ISMENU}, + {"Current folder menu ", COLOR_ORANGE, ISMENU} }; \ No newline at end of file diff --git a/source/tegraexplorer/common/types.h b/source/tegraexplorer/common/types.h index 44f6579..031b809 100644 --- a/source/tegraexplorer/common/types.h +++ b/source/tegraexplorer/common/types.h @@ -24,6 +24,13 @@ 1000 0000: Size component is a GigaByte : note that this won't surpass gigabytes, but i don't expect people to have a single file that's a terrabyte big */ +#define COPY_MODE_PRINT 0x1 +#define COPY_MODE_CANCEL 0x2 +#define BUFSIZE 32768 + +#define OPERATIONCOPY 0x2 +#define OPERATIONMOVE 0x4 + typedef struct { char *name; u32 storage; diff --git a/source/tegraexplorer/emmc.c b/source/tegraexplorer/emmc.c index d19328c..27fa998 100644 --- a/source/tegraexplorer/emmc.c +++ b/source/tegraexplorer/emmc.c @@ -1,7 +1,5 @@ #include #include "../mem/heap.h" -#include "gfx.h" -#include "fs.h" #include "emmc.h" #include "../utils/types.h" #include "../libs/fatfs/ff.h" diff --git a/source/tegraexplorer/fs.c b/source/tegraexplorer/fs.c deleted file mode 100644 index 366fa07..0000000 --- a/source/tegraexplorer/fs.c +++ /dev/null @@ -1,473 +0,0 @@ -#include -#include "../mem/heap.h" -#include "gfx.h" -#include "fs.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 "io.h" -#include "utils/script.h" -#include "te.h" - -fs_entry *fileobjects; -char rootpath[10] = ""; -char *currentpath = ""; -char *clipboard = ""; -u8 clipboardhelper = 0; -extern const char sizevalues[4][3]; -extern int launch_payload(char *path); - -menu_item explfilemenu[13] = { - {"-- File Menu --", COLOR_BLUE, -1, 0}, - {"FILE", COLOR_GREEN, -1, 0}, - {"\nSIZE", COLOR_VIOLET, -1, 0}, - {"ATTRIB", COLOR_VIOLET, -1, 0}, - {"\n\n\nBack", COLOR_WHITE, -1, 1}, - {"\nCopy to clipboard", COLOR_BLUE, COPY, 1}, - {"Move to clipboard", COLOR_BLUE, MOVE, 1}, - {"\nDelete file\n", COLOR_RED, DELETE, 1}, - {"Launch Payload", COLOR_ORANGE, PAYLOAD, 1}, - {"Launch Script", COLOR_YELLOW, SCRIPT, 1}, - {"View Hex", COLOR_GREEN, HEXVIEW, 1}, - {"Extract BIS", COLOR_RED, DUMPBIS, 1}, - {"Restore BIS", COLOR_RED, RESTOREBIS, 1} -}; - -menu_item explfoldermenu[6] = { - {"-- Folder Menu --\n", COLOR_BLUE, -1, 0}, - {"ATTRIB", COLOR_VIOLET, -1, 0}, - {"\n\nBack", COLOR_WHITE, -1, 1}, - {"Return to main menu\n", COLOR_BLUE, EXITFOLDER, 1}, - {"Copy to clipboard", COLOR_VIOLET, COPYFOLDER, 1}, - {"Delete folder", COLOR_RED, DELETEFOLDER, 1} -}; - -void writecurpath(const char *in){ - if (currentpath != NULL) - free(currentpath); - - size_t len = strlen(in) + 1; - currentpath = (char*) malloc (len); - strcpy(currentpath, in); - - strcpy(currentpath, in); -} - -void writeclipboard(const char *in, bool move, bool folder){ - if (clipboard != NULL) - free(clipboard); - - clipboardhelper = 0; - - if (move) - clipboardhelper |= (OPERATIONMOVE); - else - clipboardhelper |= (OPERATIONCOPY); - - if (folder) - clipboardhelper |= (ISDIR); - - size_t len = strlen(in) + 1; - clipboard = (char*) malloc (len); - strcpy(clipboard, in); - - strcpy(clipboard, in); -} - -char *getnextloc(const char *current, const char *add){ - static char *ret; - - if (ret != NULL){ - free(ret); - ret = NULL; - } - - size_t size = strlen(current) + strlen(add) + 1; - ret = (char*) malloc (size); - if (!strcmp(rootpath, current)) - sprintf(ret, "%s%s", current, add); - else - sprintf(ret, "%s/%s", current, add); - - return ret; -} - -char *getprevloc(char *current){ - static char *ret; - char *temp; - - if (ret != NULL){ - free(ret); - ret = NULL; - } - - size_t size = strlen(current) + 1; - - ret = (char*) malloc (size); - strcpy(ret, current); - - temp = strrchr(ret, '/'); - memset(temp, '\0', 1); - - if (strlen(rootpath) > strlen(ret)) - strcpy(ret, rootpath); - - return ret; -} - -int getfileobjamount(){ - int amount = 0; - - while (fileobjects[amount].name != NULL) - amount++; - - return amount; -} - -fs_entry getfileobj(int spot){ - return fileobjects[spot]; -} - -void copyfile(const char *path, const char *outfolder){ - char *filename = strrchr(path, '/') + 1; - char *outstring; - int res; - - clearscreen(); - makestring(getnextloc(outfolder, filename), &outstring); - - gfx_printf("Note:\nTo stop the transfer hold Vol-\n\n%s\nProgress: ", outstring); - - if (!strcmp(path, outstring)){ - message(COLOR_RED, "\nIn and out are the same!"); - } - else if (clipboardhelper & OPERATIONMOVE){ - if (strcmp(rootpath, "emmc:/")){ - f_rename(path, outstring); - readfolder(currentpath); - } - else - message(COLOR_RED, "\nMoving in emummc is not allowed!"); - } - - else if (clipboardhelper & OPERATIONCOPY) { - res = copy(path, outstring, true, true); - if (res){ - gfx_printf("\n\n%kSomething went wrong while copying!\n\nErrcode: %d%k", COLOR_RED, res, COLOR_WHITE); - btn_wait(); - } - readfolder(currentpath); - } - - else { - message(COLOR_RED, "\nClipboard is empty!"); - } - - free (outstring); - clipboardhelper = 0; -} - -void addobject(char* name, int spot, bool isfol, bool isarc){ - size_t length = strlen(name) + 1; - u64 size = 0; - int sizes = 0; - fileobjects[spot].property = 0; - - if (fileobjects[spot].name != NULL){ - free(fileobjects[spot].name); - fileobjects[spot].name = NULL; - } - - fileobjects[spot].name = (char*) malloc (length); - strlcpy(fileobjects[spot].name, name, length); - - if (isfol) - fileobjects[spot].property |= (ISDIR); - else { - size = getfilesize(getnextloc(currentpath, name)); - - while (size > 1024){ - size /= 1024; - sizes++; - } - - if (sizes > 3) - sizes = 0; - - fileobjects[spot].property |= (1 << (4 + sizes)); - fileobjects[spot].size = size; - } - - if (isarc) - fileobjects[spot].property |= (ISARC); -} - -void clearfileobjects(){ - if (fileobjects != NULL){ - for (int i = 0; fileobjects[i].name != NULL; i++){ - free(fileobjects[i].name); - fileobjects[i].name = NULL; - } - free(fileobjects); - fileobjects = NULL; - } -} - -void createfileobjects(int size){ - fileobjects = calloc (size + 1, sizeof(fs_entry)); - fileobjects[size].name = NULL; -} - -int readfolder(const char *path){ - DIR dir; - FILINFO fno; - int folderamount = 0, res; - - clearfileobjects(); - createfileobjects(getfolderentryamount(path)); - - if ((res = f_opendir(&dir, path))){ - message(COLOR_RED, "Error during f_opendir: %d", res); - return -1; - } - - while (!f_readdir(&dir, &fno) && fno.fname[0]){ - addobject(fno.fname, folderamount++, (fno.fattrib & AM_DIR), (fno.fattrib & AM_ARC)); - } - - f_closedir(&dir); - return folderamount; -} - -int delfile(const char *path, const char *filename){ - char *tempmessage; - size_t tempmessagesize = strlen(filename) + 65; - tempmessage = (char*) malloc (tempmessagesize); - - sprintf(tempmessage, "Are you sure you want to delete:\n%s\n\nPress vol+/- to cancel\n", filename); - if (makewaitmenu(tempmessage, "Press power to delete", 3)){ - f_unlink(path); - readfolder(currentpath); - return 0; - } - else - return -1; -} - -void copyfolder(char *in, char *out){ - bool fatalerror = false; - int res; - - if (!strcmp(in, rootpath)){ - message(COLOR_RED, "\nIn is root\nAborting!"); - fatalerror = true; - } - - if (strstr(out, in) != NULL && !fatalerror){ - message(COLOR_RED, "\nOut is a part of in!\nAborting!"); - fatalerror = true; - } - - if (!strcmp(in, out) && !fatalerror){ - message(COLOR_RED, "\nIn is the same as out!\nAborting!"); - fatalerror = true; - } - - if (!fatalerror){ - clearscreen(); - gfx_printf("\nCopying folder, please wait\n"); - if ((res = copy_recursive(in, out))) - message(COLOR_RED, "copy_recursive() failed!\nErrcode %d", res); - readfolder(currentpath); - } - clipboardhelper = 0; -} - -int filemenu(fs_entry file){ - int temp; - FILINFO attribs; - strlcpy(explfilemenu[1].name, file.name, 43); - - for (temp = 4; temp < 8; temp++) - if ((file.property & (1 << temp))) - break; - - sprintf(explfilemenu[2].name, "\nSize: %d %s", file.size, sizevalues[temp - 4]); - - if (f_stat(getnextloc(currentpath, file.name), &attribs)) - explfilemenu[3].property = -1; - else { - explfilemenu[3].property = 0; - sprintf(explfilemenu[3].name, "Attribs: %c%c%c%c", - (attribs.fattrib & AM_RDO) ? 'R' : '-', - (attribs.fattrib & AM_SYS) ? 'S' : '-', - (attribs.fattrib & AM_HID) ? 'H' : '-', - (attribs.fattrib & AM_ARC) ? 'A' : '-'); - } - - if (strstr(file.name, ".bin") != NULL && file.size & ISKB){ - explfilemenu[8].property = 1; - } - else - explfilemenu[8].property = -1; - - if (strstr(file.name, ".tegrascript") != NULL) - explfilemenu[9].property = 1; - else - explfilemenu[9].property = -1; - - if (strstr(file.name, ".bis") != NULL){ - explfilemenu[11].property = 1; - explfilemenu[12].property = 1; - } - else { - explfilemenu[11].property = -1; - explfilemenu[12].property = -1; - } - - temp = makemenu(explfilemenu, 13); - - switch (temp){ - case COPY: - writeclipboard(getnextloc(currentpath, file.name), false, false); - break; - case MOVE: - writeclipboard(getnextloc(currentpath, file.name), true, false); - break; - case DELETE: - delfile(getnextloc(currentpath, file.name), file.name); - break; - case PAYLOAD: - launch_payload(getnextloc(currentpath, file.name)); - break; - case SCRIPT: - ParseScript(getnextloc(currentpath, file.name)); - break; - case HEXVIEW: - viewbytes(getnextloc(currentpath, file.name)); - break; - case DUMPBIS: - clearscreen(); - extract_bis_file(getnextloc(currentpath, file.name), currentpath); - btn_wait(); - break; - case RESTOREBIS: - message(COLOR_ORANGE, "Stubbed"); - break; - } - - return 0; -} - -int foldermenu(){ - int res; - FILINFO attribs; - - if (!strcmp(rootpath, currentpath)){ - explfoldermenu[4].property = -1; - explfoldermenu[5].property = -1; - } - else { - explfoldermenu[4].property = 1; - explfoldermenu[5].property = 1; - } - - if (f_stat(currentpath, &attribs)) - explfoldermenu[1].property = -1; - else { - explfoldermenu[1].property = 0; - sprintf(explfoldermenu[1].name, "Attribs: %c%c%c%c", - (attribs.fattrib & AM_RDO) ? 'R' : '-', - (attribs.fattrib & AM_SYS) ? 'S' : '-', - (attribs.fattrib & AM_HID) ? 'H' : '-', - (attribs.fattrib & AM_ARC) ? 'A' : '-'); - } - - res = makemenu(explfoldermenu, 6); - - switch (res){ - case EXITFOLDER: - return -1; - case DELETEFOLDER: - if (makewaitmenu("Do you want to delete this folder?\nThe entire folder, with all subcontents\n will be deleted!!!\n\nPress vol+/- to cancel\n", "Press power to contine...", 3)){ - clearscreen(); - gfx_printf("\nDeleting folder, please wait...\n"); - if ((res = del_recursive(currentpath))){ - message(COLOR_RED, "Error during del_recursive()! %d", res); - } - writecurpath(getprevloc(currentpath)); - readfolder(currentpath); - } - break; - case COPYFOLDER: - writeclipboard(currentpath, false, true); - break; - } - - return 0; -} - -void fileexplorer(const char *startpath){ - int res, tempint; - bool breakfree = false; - - if (!strcmp(rootpath, "emmc:/") && !strcmp(startpath, "emmc:/")) - clipboardhelper = 0; - - strcpy(rootpath, startpath); - writecurpath(startpath); - readfolder(currentpath); - - if (strcmp(rootpath, "emmc:/")) - explfilemenu[6].property = 1; - else - explfilemenu[6].property = -1; - - while (1){ - res = makefilemenu(fileobjects, getfileobjamount(), currentpath); - if (res < 1){ - switch (res){ - case -2: - if (!strcmp(rootpath, currentpath)) - breakfree = true; - else { - writecurpath(getprevloc(currentpath)); - readfolder(currentpath); - } - - break; - - case -1: - if (clipboardhelper & ISDIR) - copyfolder(clipboard, currentpath); - else - copyfile(clipboard, currentpath); - break; - - case 0: - tempint = foldermenu(); - - if (tempint == -1) - breakfree = true; - - break; - - } - } - else { - if (fileobjects[res - 1].property & ISDIR){ - writecurpath(getnextloc(currentpath, fileobjects[res - 1].name)); - readfolder(currentpath); - } - else { - filemenu(fileobjects[res - 1]); - } - } - - if (breakfree) - break; - } -} \ No newline at end of file diff --git a/source/tegraexplorer/fs.h b/source/tegraexplorer/fs.h deleted file mode 100644 index c7c7550..0000000 --- a/source/tegraexplorer/fs.h +++ /dev/null @@ -1,51 +0,0 @@ -#pragma once -#include "../utils/types.h" - -#define ISDIR (1 << 0) -#define ISARC (1 << 1) - -#define ISGB (1 << 7) -#define ISMB (1 << 6) -#define ISKB (1 << 5) -#define ISB (1 << 4) - -#define OPERATIONCOPY (1 << 1) -#define OPERATIONMOVE (1 << 2) - -#define BUFSIZE 32768 - -/* Bit table for property: -0000 0001: Directory bit -0000 0010: Archive bit -0001 0000: Size component is a Byte -0010 0000: Size component is a KiloByte -0100 0000: Size component is a MegaByte -1000 0000: Size component is a GigaByte : note that this won't surpass gigabytes, but i don't expect people to have a single file that's a terrabyte big -*/ - -typedef struct _fs_entry { - char* name; - u16 size; - u8 property; -} fs_entry; - -enum filemenuoptions { - COPY = 1, - MOVE, - DELETE, - PAYLOAD, - SCRIPT, - HEXVIEW, - DUMPBIS, - RESTOREBIS -}; - -enum foldermenuoptions { - EXITFOLDER = 1, - DELETEFOLDER, - COPYFOLDER -}; - -int readfolder(const char *path); -void fileexplorer(const char *startpath); -char *getnextloc(const char *current, const char *add); \ No newline at end of file diff --git a/source/tegraexplorer/fs/entrymenu.h b/source/tegraexplorer/fs/entrymenu.h new file mode 100644 index 0000000..4db2b06 --- /dev/null +++ b/source/tegraexplorer/fs/entrymenu.h @@ -0,0 +1,7 @@ +#pragma once +#include "../common/types.h" + +int filemenu(menu_entry file); +void copyfile(const char *src_in, const char *outfolder); +int foldermenu(); +void copyfolder(char *in, char *out); \ No newline at end of file diff --git a/source/tegraexplorer/fs/filemenu.c b/source/tegraexplorer/fs/filemenu.c new file mode 100644 index 0000000..db71527 --- /dev/null +++ b/source/tegraexplorer/fs/filemenu.c @@ -0,0 +1,185 @@ +#include +#include "entrymenu.h" +#include "../common/common.h" +#include "../../libs/fatfs/ff.h" +#include "../../utils/btn.h" +#include "../../gfx/gfx.h" +#include "fsutils.h" +#include "fsactions.h" +#include "../utils/utils.h" +#include "../gfx/gfxutils.h" +#include "../../mem/heap.h" +#include "fsreader.h" +#include "../gfx/menu.h" +#include "../common/types.h" +#include "../../utils/sprintf.h" +#include "../utils/script.h" + +extern char *currentpath; +extern char *clipboard; +extern u8 clipboardhelper; +extern int launch_payload(char *path); + +int delfile(const char *path, const char *filename){ + gfx_clearscreen(); + SWAPCOLOR(COLOR_ORANGE); + gfx_printf("Are you sure you want to delete:\n%s\n\nPress vol+/- to cancel\n", filename); + if (gfx_makewaitmenu("Press power to delete", 3)){ + f_unlink(path); + fsreader_readfolder(currentpath); + return 0; + } + else + return -1; +} + +void viewbytes(char *path){ + FIL in; + u8 print[2048]; + u32 size; + QWORD offset = 0; + int res; + + gfx_clearscreen(); + if ((res = f_open(&in, path, FA_READ | FA_OPEN_EXISTING))){ + gfx_errDisplay("viewbytes", res, 1); + return; + } + + msleep(200); + + while (1){ + f_lseek(&in, offset * 16); + + if ((res = f_read(&in, &print, 2048 * sizeof(u8), &size))){ + gfx_errDisplay("viewbytes", res, 2); + return; + } + + gfx_con_setpos(0, 31); + gfx_hexdump(offset * 16, print, size * sizeof(u8)); + + res = btn_read(); + + if (!res) + res = btn_wait(); + + if (res & BTN_VOL_DOWN && 2048 * sizeof(u8) == size) + offset++; + if (res & BTN_VOL_UP && offset > 0) + offset--; + if (res & BTN_POWER) + break; + } + f_close(&in); +} + +void copyfile(const char *src_in, const char *outfolder){ + char *in, *out, *filename; + int res; + + gfx_clearscreen(); + utils_copystring(src_in, &in); + utils_copystring(strrchr(in, '/') + 1, &filename); + utils_copystring(fsutil_getnextloc(outfolder, filename), &out); + + gfx_printf("Note:\nTo stop the transfer hold Vol-\n\n%s\nProgress: ", filename); + + if (!strcmp(in, out)){ + gfx_errDisplay("gfxcopy", ERR_SAME_LOC, 1); + return; + } + + if (clipboardhelper & OPERATIONMOVE){ + if ((res = f_rename(in, out))){ + gfx_errDisplay("gfxcopy", res, 2); + return; + } + } + else if (clipboardhelper & OPERATIONCOPY) { + if (fsact_copy(in, out, COPY_MODE_CANCEL | COPY_MODE_PRINT)) + return; + } + + else { + gfx_errDisplay("gfxcopy", ERR_EMPTY_CLIPBOARD, 3); + return; + } + + free(in); + free(out); + free(filename); + fsreader_readfolder(currentpath); + clipboardhelper = 0; +} + +int filemenu(menu_entry file){ + int temp; + FILINFO attribs; + + for (int i = 0; i < 3; i++) + if (fs_menu_file[i].name != NULL) + free(fs_menu_file[i].name); + + utils_copystring(file.name, &fs_menu_file[0].name); + fs_menu_file[1].name = malloc(16); + fs_menu_file[2].name = malloc(16); + + for (temp = 4; temp < 8; temp++) + if ((file.property & (1 << temp))) + break; + + + sprintf(fs_menu_file[1].name, "\nSize: %d %s", file.storage, gfx_file_size_names[temp - 4]); + + if (f_stat(fsutil_getnextloc(currentpath, file.name), &attribs)) + SETBIT(fs_menu_file[2].property, ISHIDE, 1); + else { + SETBIT(fs_menu_file[2].property, ISHIDE, 0); + sprintf(fs_menu_file[2].name, "Attribs: %c%c%c%c", + (attribs.fattrib & AM_RDO) ? 'R' : '-', + (attribs.fattrib & AM_SYS) ? 'S' : '-', + (attribs.fattrib & AM_HID) ? 'H' : '-', + (attribs.fattrib & AM_ARC) ? 'A' : '-'); + } + + 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, ".tegrascript") != 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 --"); + + switch (temp){ + case FILE_COPY: + fsreader_writeclipboard(fsutil_getnextloc(currentpath, file.name), OPERATIONCOPY); + break; + case FILE_MOVE: + fsreader_writeclipboard(fsutil_getnextloc(currentpath, file.name), OPERATIONMOVE); + break; + case FILE_DELETE: + delfile(fsutil_getnextloc(currentpath, file.name), file.name); + break; + case FILE_PAYLOAD: + launch_payload(fsutil_getnextloc(currentpath, file.name)); + break; + case FILE_SCRIPT: + ParseScript(fsutil_getnextloc(currentpath, file.name)); + break; + case FILE_HEXVIEW: + viewbytes(fsutil_getnextloc(currentpath, file.name)); + break; + case FILE_DUMPBIS: + /* + clearscreen(); + extract_bis_file(getnextloc(currentpath, file.name), currentpath); + btn_wait(); + */ + break; + case FILE_RESTOREBIS: + gfx_message(COLOR_ORANGE, "Stubbed"); + break; + } + + return 0; +} diff --git a/source/tegraexplorer/fs/foldermenu.c b/source/tegraexplorer/fs/foldermenu.c new file mode 100644 index 0000000..5aa5719 --- /dev/null +++ b/source/tegraexplorer/fs/foldermenu.c @@ -0,0 +1,89 @@ +#include +#include "entrymenu.h" +#include "../common/common.h" +#include "../../libs/fatfs/ff.h" +#include "../../mem/heap.h" +#include "../gfx/menu.h" +#include "fsreader.h" +#include "../gfx/gfxutils.h" +#include "fsactions.h" +#include "fsutils.h" +#include "../../utils/sprintf.h" + +extern char *currentpath; +extern char *clipboard; +extern u8 clipboardhelper; + +void copyfolder(char *in, char *out){ + int res; + + res = strlen(in); + if ((*(in + res - 1) == '/')){ + gfx_errDisplay("copyfolder", ERR_FOLDER_ROOT, 1); + } + else if (strstr(out, in) != NULL){ + gfx_errDisplay("copyfolder", ERR_DEST_PART_OF_SRC, 2); + } + else if (!strcmp(in, out)){ + gfx_errDisplay("copyfolder", ERR_SAME_LOC, 3); + } + else { + gfx_clearscreen(); + gfx_printf("\nCopying folder, please wait\n"); + fsact_copy_recursive(in, out); + fsreader_readfolder(currentpath); + } + + clipboardhelper = 0; +} + +int foldermenu(){ + int res; + FILINFO attribs; + + if (fs_menu_folder[0].name != NULL) + free(fs_menu_folder[0].name); + + fs_menu_folder[0].name = malloc(16); + + res = strlen(currentpath); + + SETBIT(fs_menu_folder[3].property, ISHIDE, (*(currentpath + res - 1) == '/')); + SETBIT(fs_menu_folder[4].property, ISHIDE, (*(currentpath + res - 1) == '/')); + + if (f_stat(currentpath, &attribs)) + SETBIT(fs_menu_folder[0].property, ISHIDE, 1); + else { + SETBIT(fs_menu_folder[0].property, ISHIDE, 0); + sprintf(fs_menu_folder[0].name, "Attribs: %c%c%c%c", + (attribs.fattrib & AM_RDO) ? 'R' : '-', + (attribs.fattrib & AM_SYS) ? 'S' : '-', + (attribs.fattrib & AM_HID) ? 'H' : '-', + (attribs.fattrib & AM_ARC) ? 'A' : '-'); + } + + res = menu_make(fs_menu_folder, 5, currentpath); + + switch (res){ + case DIR_EXITFOLDER: + return -1; + case DIR_COPYFOLDER: + fsreader_writeclipboard(currentpath, OPERATIONCOPY | ISDIR); + break; + case DIR_DELETEFOLDER: + gfx_clearscreen(); + gfx_printf("Do you want to delete this folder?\nThe entire folder, with all subcontents\n will be deleted!!!\n\nPress vol+/- to cancel\n"); + if (gfx_makewaitmenu("Press power to contine...", 3)){ + gfx_clearscreen(); + gfx_printf("\nDeleting folder, please wait...\n"); + + fsact_del_recursive(currentpath); + + fsreader_writecurpath(fsutil_getprevloc(currentpath)); + fsreader_readfolder(currentpath); + } + break; + } + + return 0; +} \ No newline at end of file diff --git a/source/tegraexplorer/fs/fsactions.c b/source/tegraexplorer/fs/fsactions.c new file mode 100644 index 0000000..d193d89 --- /dev/null +++ b/source/tegraexplorer/fs/fsactions.c @@ -0,0 +1,193 @@ +#include + +#include "fsactions.h" +#include "../common/common.h" +#include "../../libs/fatfs/ff.h" +#include "../../gfx/gfx.h" +#include "../gfx/gfxutils.h" +#include "../utils/utils.h" +#include "../../mem/heap.h" +#include "../../utils/btn.h" +#include "fsutils.h" + +int fsact_copy(const char *locin, const char *locout, u8 options){ + FIL in, out; + FILINFO in_info; + u64 sizeoffile, sizecopied = 0, totalsize; + UINT temp1, temp2; + u8 *buff; + unsigned int x, y, i = 0; + int res; + + gfx_con_getpos(&x, &y); + + if (!strcmp(locin, locout)){ + gfx_errDisplay("copy", ERR_SAME_LOC, 1); + return -1; + } + + if ((res = f_open(&in, locin, FA_READ | FA_OPEN_EXISTING))){ + gfx_errDisplay("copy", res, 2); + return -1; + } + + if (f_stat(locin, &in_info)){ + gfx_errDisplay("copy", res, 3); + return -1; + } + + if (f_open(&out, locout, FA_CREATE_ALWAYS | FA_WRITE)){ + gfx_errDisplay("copy", res, 4); + return -1; + } + + buff = malloc (BUFSIZE); + sizeoffile = f_size(&in); + totalsize = sizeoffile; + + while (sizeoffile > 0){ + if ((res = f_read(&in, buff, (sizeoffile > BUFSIZE) ? BUFSIZE : sizeoffile, &temp1))){ + gfx_errDisplay("copy", res, 5); + return -1; + } + + if ((res = f_write(&out, buff, (sizeoffile > BUFSIZE) ? BUFSIZE : sizeoffile, &temp2))){ + gfx_errDisplay("copy", res, 6); + return -1; + } + + if (temp1 != temp2){ + gfx_errDisplay("copy", ERR_DISK_WRITE_FAILED, 7); + return -1; + } + + sizeoffile -= temp1; + sizecopied += temp1; + + if (options & COPY_MODE_PRINT && 10 > i++){ + gfx_printf("%k[%d%%]%k", COLOR_GREEN, ((sizecopied * 100) / totalsize) ,COLOR_WHITE); + gfx_con_setpos(x, y); + + i = 0; + + if (options & COPY_MODE_CANCEL) + if (btn_read() & BTN_VOL_DOWN){ + f_unlink(locout); + break; + } + } + } + + f_close(&in); + f_close(&out); + free(buff); + + if ((res = f_chmod(locout, in_info.fattrib, 0x3A))){ + gfx_errDisplay("copy", res, 8); + return -1; + } + + f_stat(locin, &in_info); //somehow stops fatfs from being weird + return 0; +} + +int fsact_del_recursive(char *path){ + DIR dir; + FILINFO fno; + int res; + u32 x, y; + char *localpath = NULL; + + gfx_con_getpos(&x, &y); + utils_copystring(path, &localpath); + + if ((res = f_opendir(&dir, localpath))){ + gfx_errDisplay("del_recursive", res, 1); + return -1; + } + + while (!f_readdir(&dir, &fno) && fno.fname[0]){ + if (fno.fattrib & AM_DIR){ + fsact_del_recursive(fsutil_getnextloc(localpath, fno.fname)); + } + else { + SWAPCOLOR(COLOR_RED); + gfx_printf("\r"); + gfx_printandclear(fno.fname, 37); + + if ((res = f_unlink(fsutil_getnextloc(localpath, fno.fname)))){ + gfx_errDisplay("del_recursive", res, 2); + return -1; + } + } + } + + f_closedir(&dir); + + if ((res = f_unlink(localpath))){ + gfx_errDisplay("del_recursive", res, 3); + return -1; + } + + free(localpath); + return 0; +} + +int fsact_copy_recursive(char *path, char *dstpath){ + DIR dir; + FILINFO fno; + int res; + u32 x, y; + char *startpath = NULL, *destpath = NULL, *destfoldername = NULL, *temp = NULL; + + gfx_con_getpos(&x, &y); + + utils_copystring(path, &startpath); + utils_copystring(strrchr(path, '/') + 1, &destfoldername); + utils_copystring(fsutil_getnextloc(dstpath, destfoldername), &destpath); + + if ((res = f_opendir(&dir, startpath))){ + gfx_errDisplay("copy_recursive", res, 1); + return -1; + } + + f_mkdir(destpath); + + while (!f_readdir(&dir, &fno) && fno.fname[0]){ + if (fno.fattrib & AM_DIR){ + fsact_copy_recursive(fsutil_getnextloc(startpath, fno.fname), destpath); + } + else { + SWAPCOLOR(COLOR_GREEN); + gfx_printf("\r"); + gfx_printandclear(fno.fname, 37); + + utils_copystring(fsutil_getnextloc(startpath, fno.fname), &temp); + + if ((res = fsact_copy(temp, fsutil_getnextloc(destpath, fno.fname), COPY_MODE_PRINT))){ + gfx_errDisplay("copy_recursive", res, 2); + return -1; + } + + free(temp); + } + } + + f_closedir(&dir); + + if ((res = (f_stat(startpath, &fno)))){ + gfx_errDisplay("copy_recursive", res, 3); + return -1; + } + + if ((res = f_chmod(destpath, fno.fattrib, 0x3A))){ + gfx_errDisplay("copy_recursive", res, 4); + return -1; + } + + free(startpath); + free(destpath); + free(destfoldername); + + return 0; +} \ No newline at end of file diff --git a/source/tegraexplorer/fs/fsactions.h b/source/tegraexplorer/fs/fsactions.h new file mode 100644 index 0000000..c93a353 --- /dev/null +++ b/source/tegraexplorer/fs/fsactions.h @@ -0,0 +1,6 @@ +#pragma once +#include "../../utils/types.h" + +int fsact_copy(const char *locin, const char *locout, u8 options); +int fsact_del_recursive(char *path); +int fsact_copy_recursive(char *path, char *dstpath); \ No newline at end of file diff --git a/source/tegraexplorer/fs/fsmenu.c b/source/tegraexplorer/fs/fsmenu.c new file mode 100644 index 0000000..2cdf8fd --- /dev/null +++ b/source/tegraexplorer/fs/fsmenu.c @@ -0,0 +1,115 @@ +#include +#include "fsmenu.h" +#include "fsreader.h" +#include "../gfx/menu.h" +#include "../gfx/gfxutils.h" +#include "fsutils.h" +#include "../common/common.h" +#include "../../libs/fatfs/ff.h" +#include "entrymenu.h" + +extern char *currentpath; +extern char *clipboard; +extern u8 clipboardhelper; +int lastentry = 0; + +void fileexplorer(const char *startpath, int type){ + int res; + + if (lastentry > 0 && lastentry == type) + clipboardhelper = 0; + + lastentry = type; + + fsreader_writecurpath(startpath); + fsreader_readfolder(currentpath); + + /* + if (strcmp(rootpath, "emmc:/")) + explfilemenu[6].property = 1; + else + explfilemenu[6].property = -1; + */ + + while (1){ + res = menu_make(fsreader_files, fsutil_getfileobjamount(fsreader_files), currentpath); + switch (res){ + case FILEMENU_RETURN: + if (!strcmp(startpath, currentpath)) + return; + else { + fsreader_writecurpath(fsutil_getprevloc(currentpath)); + fsreader_readfolder(currentpath); + } + + break; + case FILEMENU_CLIPBOARD: + if (clipboardhelper & ISDIR) + copyfolder(clipboard, currentpath); + else + copyfile(clipboard, currentpath); + break; + + case FILEMENU_CURFOLDER: + if (foldermenu()) + return; + break; + + default: + if(fsreader_files[res].property & ISDIR){ + fsreader_writecurpath(fsutil_getnextloc(currentpath, fsreader_files[res].name)); + fsreader_readfolder(currentpath); + } + else + filemenu(fsreader_files[res]); + + break; + } + } + /* + while (1){ + res = makefilemenu(fileobjects, getfileobjamount(), currentpath); + if (res < 1){ + switch (res){ + case -2: + if (!strcmp(rootpath, currentpath)) + breakfree = true; + else { + writecurpath(getprevloc(currentpath)); + readfolder(currentpath); + } + + break; + + case -1: + if (clipboardhelper & ISDIR) + copyfolder(clipboard, currentpath); + else + copyfile(clipboard, currentpath); + break; + + case 0: + tempint = foldermenu(); + + if (tempint == -1) + breakfree = true; + + break; + + } + } + else { + if (fileobjects[res - 1].property & ISDIR){ + writecurpath(getnextloc(currentpath, fileobjects[res - 1].name)); + readfolder(currentpath); + } + else { + filemenu(fileobjects[res - 1]); + } + } + + if (breakfree) + break; + } + */ +} \ No newline at end of file diff --git a/source/tegraexplorer/fs/fsmenu.h b/source/tegraexplorer/fs/fsmenu.h new file mode 100644 index 0000000..fcd7043 --- /dev/null +++ b/source/tegraexplorer/fs/fsmenu.h @@ -0,0 +1,3 @@ +#pragma once + +void fileexplorer(const char *startpath, int type); \ No newline at end of file diff --git a/source/tegraexplorer/fs/fsreader.c b/source/tegraexplorer/fs/fsreader.c new file mode 100644 index 0000000..f2089dc --- /dev/null +++ b/source/tegraexplorer/fs/fsreader.c @@ -0,0 +1,106 @@ +#include "fsreader.h" +#include "../common/types.h" +#include "../common/common.h" +#include "fsutils.h" +#include "../../utils/types.h" +#include "../../libs/fatfs/ff.h" +#include "../gfx/gfxutils.h" +#include "../utils/utils.h" +#include "../../mem/heap.h" + +menu_entry *fsreader_files = NULL; +char *currentpath = NULL; +char *clipboard = NULL; +u8 clipboardhelper = 0; + +void fsreader_writecurpath(const char *in){ + if (currentpath != NULL) + free(currentpath); + + utils_copystring(in, ¤tpath); +} + +void fsreader_writeclipboard(const char *in, u8 args){ + if (clipboard != NULL) + free(clipboard); + + clipboardhelper = args; + + utils_copystring(in, &clipboard); +} + +void clearfileobjects(){ + if (fsreader_files != NULL){ + for (int i = 0; fsreader_files[i].name != NULL; i++){ + free(fsreader_files[i].name); + fsreader_files[i].name = NULL; + } + free(fsreader_files); + fsreader_files = NULL; + } +} + +void createfileobjects(int size){ + fsreader_files = calloc (size + 1, sizeof(menu_entry)); + fsreader_files[size].name = NULL; +} + +void addobject(char* name, int spot, u8 attribs){ + u64 size = 0; + int sizes = 0; + fsreader_files[spot].property = 0; + + if (fsreader_files[spot].name != NULL){ + free(fsreader_files[spot].name); + fsreader_files[spot].name = NULL; + } + + utils_copystring(name, &(fsreader_files[spot].name)); + + if (attribs & AM_DIR) + fsreader_files[spot].property |= (ISDIR); + else { + size = fsutil_getfilesize(fsutil_getnextloc(currentpath, name)); + + while (size > 1024){ + size /= 1024; + sizes++; + } + + if (sizes > 3) + sizes = 0; + + fsreader_files[spot].property |= (1 << (4 + sizes)); + fsreader_files[spot].storage = size; + } + + if (attribs & AM_ARC) + fsreader_files[spot].property |= (ISARC); +} + +int fsreader_readfolder(const char *path){ + DIR dir; + FILINFO fno; + int folderamount, res; + + clearfileobjects(); + createfileobjects(fsutil_getfolderentryamount(path) + 3); + + for (folderamount = 0; folderamount < 3; folderamount++){ + utils_copystring(fs_menu_startdir[folderamount].name, &(fsreader_files[folderamount].name)); + fsreader_files[folderamount].storage = fs_menu_startdir[folderamount].storage; + fsreader_files[folderamount].property = fs_menu_startdir[folderamount].property; + } + + if ((res = f_opendir(&dir, path))){ + gfx_errDisplay("readfolder", res, 0); + return -1; + } + + while (!f_readdir(&dir, &fno) && fno.fname[0]){ + addobject(fno.fname, folderamount++, fno.fattrib); + } + + f_closedir(&dir); + return folderamount; +} \ No newline at end of file diff --git a/source/tegraexplorer/fs/fsreader.h b/source/tegraexplorer/fs/fsreader.h new file mode 100644 index 0000000..e7356dd --- /dev/null +++ b/source/tegraexplorer/fs/fsreader.h @@ -0,0 +1,7 @@ +#pragma once +#include "../common/types.h" + +extern menu_entry *fsreader_files; +void fsreader_writecurpath(const char *in); +void fsreader_writeclipboard(const char *in, u8 args); +int fsreader_readfolder(const char *path); \ No newline at end of file diff --git a/source/tegraexplorer/fs/fsutils.c b/source/tegraexplorer/fs/fsutils.c new file mode 100644 index 0000000..3e615b3 --- /dev/null +++ b/source/tegraexplorer/fs/fsutils.c @@ -0,0 +1,90 @@ +#include +#include "fsutils.h" +#include "../utils/utils.h" +#include "../common/types.h" +#include "../../utils/types.h" +#include "../../libs/fatfs/ff.h" +#include "../../mem/heap.h" +#include "../../utils/sprintf.h" +#include "../gfx/gfxutils.h" + +char *fsutil_getnextloc(const char *current, const char *add){ + static char *ret; + + if (ret != NULL){ + free(ret); + ret = NULL; + } + + size_t size = strlen(current) + strlen(add) + 2; + ret = (char*) malloc (size); + + if (current[strlen(current) - 1] == '/') + sprintf(ret, "%s%s", current, add); + else + sprintf(ret, "%s/%s", current, add); + + return ret; +} + +char *fsutil_getprevloc(char *current){ + static char *ret; + char *temp; + + if (ret != NULL){ + free(ret); + ret = NULL; + } + + utils_copystring(current, &ret); + temp = strrchr(ret, '/'); + + if (*(temp - 1) == ':') + temp++; + + *temp = '\0'; + + return ret; +} + +int fsutil_getfileobjamount(menu_entry *entries){ + int amount = 0; + + while (entries[amount].name != NULL) + amount++; + + return amount; +} + +bool fsutil_checkfile(char* path){ + FRESULT fr; + FILINFO fno; + + fr = f_stat(path, &fno); + + return !(fr & FR_NO_FILE); +} + +u64 fsutil_getfilesize(char *path){ + FILINFO fno; + f_stat(path, &fno); + return fno.fsize; +} + +int fsutil_getfolderentryamount(const char *path){ + DIR dir; + FILINFO fno; + int folderamount = 0, res; + + if ((res = f_opendir(&dir, path))){ + gfx_errDisplay("fsutil_getdircount", res, 0); + return -1; + } + + while (!f_readdir(&dir, &fno) && fno.fname[0]){ + folderamount++; + } + + f_closedir(&dir); + return folderamount; +} \ No newline at end of file diff --git a/source/tegraexplorer/fs/fsutils.h b/source/tegraexplorer/fs/fsutils.h new file mode 100644 index 0000000..6001b1e --- /dev/null +++ b/source/tegraexplorer/fs/fsutils.h @@ -0,0 +1,10 @@ +#pragma once +#include "../common/types.h" +#include "../../utils/types.h" + +char *fsutil_getnextloc(const char *current, const char *add); +char *fsutil_getprevloc(char *current); +int fsutil_getfileobjamount(menu_entry *entries); +bool fsutil_checkfile(char* path); +u64 fsutil_getfilesize(char *path); +int fsutil_getfolderentryamount(const char *path); \ No newline at end of file diff --git a/source/tegraexplorer/gfx.c b/source/tegraexplorer/gfx.c deleted file mode 100644 index 83ecc00..0000000 --- a/source/tegraexplorer/gfx.c +++ /dev/null @@ -1,299 +0,0 @@ -#include -#include -#include -#include "../gfx/gfx.h" -#include "te.h" -#include "../utils/btn.h" -#include "../utils/util.h" -#include "gfx.h" -#include "fs.h" -#include "../mem/minerva.h" -#include "../power/max17050.h" -#include -#include "../storage/emummc.h" -#include "emmc.h" - -const char fixedoptions[3][50] = { - "Folder -> previous folder ", - "Clipboard -> Current folder ", - "Current folder menu " -}; - -const char sizevalues[4][3] = { - "B ", - "KB", - "MB", - "GB" -}; - -void clearscreen(){ - int battery = 0; - max17050_get_property(MAX17050_RepSOC, &battery); - - gfx_clear_grey(0x1B); - SWAPCOLOR(COLOR_DEFAULT); - SWAPBGCOLOR(COLOR_WHITE); - - gfx_box(0, 1263, 719, 1279, COLOR_WHITE); - gfx_con_setpos(0, 1263); - gfx_printf("Move: Vol+/- | Select: Pow | Battery: %3d%%", battery >> 8); - - gfx_box(0, 0, 719, 15, COLOR_WHITE); - gfx_con_setpos(0, 0); - gfx_printf("Tegraexplorer v1.3.3\n"); - - RESETCOLOR; -} - -int message(u32 color, const char* message, ...){ - va_list ap; - va_start(ap, message); - - clearscreen(); - SWAPCOLOR(color); - - gfx_vprintf(message, ap); - - va_end(ap); - return btn_wait(); -} - -/* -int gfx_errprint(u32 color, int func, int err, int add){ - clearscreen(); - SWAPCOLOR(COLOR_ORANGE); - gfx_printf("\nAn error occured:\n\n"); - gfx_printf("Function: %s\nErrcode: %d\nDesc: %s\n"); - - if (add) - gfx_printf("Additional info: %d"); - - gfx_printf("\nPress any button to return"); - - - RESETCOLOR; - return btn_wait(); -} -*/ -// Change makemenu to combine makefilemenu to save space + make nagivation more consistant -int makemenu(menu_item menu[], int menuamount){ - int currentpos = 1, i, res; - clearscreen(); - - while (1){ - gfx_con_setpos(0, 31); - - if (currentpos == 1){ - while (currentpos < menuamount && menu[currentpos - 1].property < 1) - currentpos++; - } - if (currentpos == menuamount){ - while (currentpos > 1 && menu[currentpos - 1].property < 1) - currentpos--; - } - - for (i = 0; i < menuamount; i++){ - if (menu[i].property < 0) - continue; - if (i == currentpos - 1) - gfx_printf("%k%K%s%K\n", COLOR_DEFAULT, COLOR_WHITE, menu[i].name, COLOR_DEFAULT); - else - gfx_printf("%k%s\n", menu[i].color, menu[i].name); - } - gfx_printf("\n%k%s", COLOR_WHITE, menuamount); - - res = btn_wait(); - - if (res & BTN_VOL_UP && currentpos > 1){ - currentpos--; - while(menu[currentpos - 1].property < 1 && currentpos > 1) - currentpos--; - } - - else if (res & BTN_VOL_DOWN && currentpos < menuamount){ - currentpos++; - while(menu[currentpos - 1].property < 1 && currentpos < menuamount) - currentpos++; - } - - else if (res & BTN_POWER) - return menu[currentpos - 1].internal_function; - } -} - -void printbytes(u8 print[], u32 size, u32 offset){ - gfx_con_setpos(0, 31); - gfx_hexdump(offset, print, size * sizeof(u8)); -} - -int makewaitmenu(char *initialmessage, char *hiddenmessage, int timer){ - clearscreen(); - return makewaitmenunoclear(initialmessage, hiddenmessage, timer); -} - -int makewaitmenunoclear(char *initialmessage, char *hiddenmessage, int timer){ - int res; - u32 start = get_tmr_s(); - - gfx_printf(initialmessage); - - while (btn_read() != 0); - - while(1){ - res = btn_read(); - - if (res & BTN_VOL_DOWN || res & BTN_VOL_UP) - return 0; - - if (start + timer > get_tmr_s()) - gfx_printf("\r ", timer + start - get_tmr_s()); - - else if (res & BTN_POWER) - return 1; - - else - gfx_printf("\r%k%s%k", COLOR_RED, hiddenmessage, COLOR_WHITE); - } -} - -/* -int mmcChoiceMenu(){ - if (emu_cfg.enabled){ - - } - - return SYSMMC; -} -*/ - -void gfx_print_length(int size, char *toprint){ - char *temp; - temp = (char*) malloc (size + 1); - - if (strlen(toprint) > size){ - strlcpy(temp, toprint, size); - memset(temp + size - 3, '.', 3); - memset(temp + size, '\0', 1); - } - else - strcpy(temp, toprint); - - gfx_printf("%s", temp); - free(temp); -} - -void printfsentry(fs_entry file, bool highlight, bool refresh){ - int size = 0; - char *display; - int length; - - display = (char*) malloc (38); - memset(display + 37, '\0', 1); - - if (strlen(file.name) > 37){ - strlcpy(display, file.name, 37); - memset(display + 34, '.', 3); - } - else - strcpy(display, file.name); - - - if (highlight) - gfx_printf("%K%k", COLOR_WHITE, COLOR_DEFAULT); - else - RESETCOLOR; - - if (file.property & ISDIR) - gfx_printf("%s", display); - else { - for (size = 4; size < 8; size++) - if ((file.property & (1 << size))) - break; - - SWAPCOLOR(COLOR_VIOLET); - gfx_printf("%s", display); - } - - RESETCOLOR; - - if (refresh){ - length = strlen(display); - for (int i = 0; i < (42 - length); i++) - gfx_printf(" "); - } - - if (file.property & ISDIR) - gfx_printf("\n"); - else { - SWAPCOLOR(COLOR_BLUE); - gfx_printf("\a%d\e%s", file.size, sizevalues[size - 4]); - } - - free(display); -} - -int makefilemenu(fs_entry *files, int amount, char *path){ - int currentpos = -2, i, res = 0, offset = 0, quickoffset = 300; - u32 timer, scrolltimer; - bool refresh = false; - clearscreen(); - gfx_con_setpos(544, 0); - gfx_printf("%K%k%d files\n%K%k%s%k\n\n", COLOR_WHITE, COLOR_DEFAULT, amount, COLOR_DEFAULT, COLOR_GREEN, path, COLOR_DEFAULT); - while (1){ - gfx_con_setpos(0, 47); - timer = get_tmr_ms(); - for (i = -3 + offset; i < amount && i < 60 + offset; i++){ - if (i < 0){ - if (i == currentpos - 1) - gfx_printf("%k%K%s%K\n", COLOR_ORANGE, COLOR_WHITE, fixedoptions[i + 3], COLOR_DEFAULT); - else - gfx_printf("%k%K%s\n", COLOR_ORANGE, COLOR_DEFAULT, fixedoptions[i + 3]); - } - else - printfsentry(files[i], (i == currentpos - 1), refresh); - } - refresh = false; - 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); - - res = 0; - while (!res){ - res = btn_read(); - - if (!res) - quickoffset = 300; - - if (quickoffset < 300){ - scrolltimer = get_tmr_ms(); - while (res){ - if (scrolltimer + quickoffset <= get_tmr_ms()) - break; - - res = btn_read(); - } - } - - if (quickoffset > 46 && res) - quickoffset -= 45; - } - - if ((res & BTN_VOL_UP) && currentpos > -2){ - currentpos--; - if (offset != 0 && currentpos < offset - 2){ - offset--; - refresh = true; - } - } - if ((res & BTN_VOL_DOWN) && currentpos < amount){ - currentpos++; - if (currentpos - offset > 60){ - offset++; - refresh = true; - } - } - if (res & BTN_POWER) - return currentpos; - } - minerva_periodic_training(); -} \ No newline at end of file diff --git a/source/tegraexplorer/gfx.h b/source/tegraexplorer/gfx.h deleted file mode 100644 index 064773c..0000000 --- a/source/tegraexplorer/gfx.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include "te.h" -#include "fs.h" -#include "../gfx/gfx.h" - -#define SWAPCOLOR(color) gfx_printf("%k", color) -#define SWAPBGCOLOR(color) gfx_printf("%K", color) -#define RESETCOLOR gfx_printf("%k%K", COLOR_WHITE, COLOR_DEFAULT) - -int makemenu(menu_item menu[], int menuamount); -int message(u32 color, const char* message, ...); -void clearscreen(); -int makefilemenu(fs_entry *files, int amount, char *path); -void printbytes(u8 print[], u32 size, u32 offset); -int makewaitmenu(char *initialmessage, char *hiddenmessage, int timer); -void gfx_print_length(int size, char *toprint); -int makewaitmenunoclear(char *initialmessage, char *hiddenmessage, int timer); \ No newline at end of file diff --git a/source/tegraexplorer/gfx/gfxutils.c b/source/tegraexplorer/gfx/gfxutils.c index 7513672..d93f4e2 100644 --- a/source/tegraexplorer/gfx/gfxutils.c +++ b/source/tegraexplorer/gfx/gfxutils.c @@ -42,7 +42,7 @@ int gfx_message(u32 color, const char* message, ...){ return btn_wait(); } -int gfx_errprint(char *src_func, int err, int loc){ +int gfx_errDisplay(char *src_func, int err, int loc){ gfx_clearscreen(); SWAPCOLOR(COLOR_ORANGE); gfx_printf("\nAn error occured:\n\n"); @@ -50,6 +50,8 @@ int gfx_errprint(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) + gfx_printf("Desc: %s\n", utils_err_codes_te[err - 50]); if (loc) gfx_printf("Loc: %d\n", loc); @@ -104,8 +106,18 @@ void gfx_printandclear(char *in, int length){ gfx_printlength(length, in); gfx_con_getpos(&x, &y); + RESETCOLOR; - gfx_box(x, y, 719, y + 16, COLOR_DEFAULT); + for (int i = (703 - x) / 16; i > 0; i--) + gfx_printf(" "); + + gfx_con_setpos(x, y); + + //gfx_box(x, y, 719, y + 16, COLOR_DEFAULT); + /* + u8 color = 0x1B; + gfx_set_rect_grey(&color, 719 - x, 16, x, y); + */ } void gfx_printfilesize(int size, char *type){ diff --git a/source/tegraexplorer/gfx/gfxutils.h b/source/tegraexplorer/gfx/gfxutils.h index 8673cf9..9689d08 100644 --- a/source/tegraexplorer/gfx/gfxutils.h +++ b/source/tegraexplorer/gfx/gfxutils.h @@ -8,7 +8,7 @@ void gfx_clearscreen(); int gfx_message(u32 color, const char* message, ...); -int gfx_errprint(char *src_func, int err, int loc); +int gfx_errDisplay(char *src_func, int err, int loc); int gfx_makewaitmenu(char *hiddenmessage, int timer); void gfx_printlength(int size, char *toprint); void gfx_printandclear(char *in, int length); diff --git a/source/tegraexplorer/gfx/menu.c b/source/tegraexplorer/gfx/menu.c index 1901370..a37f4bb 100644 --- a/source/tegraexplorer/gfx/menu.c +++ b/source/tegraexplorer/gfx/menu.c @@ -40,12 +40,13 @@ void _printentry(menu_entry entry, bool highlighted, bool refresh){ gfx_printf("\n"); else { SWAPCOLOR(COLOR_BLUE); + SWAPBGCOLOR(COLOR_DEFAULT); gfx_printf("\a%d\e%s", entry.storage, gfx_file_size_names[size - 4]); } } int menu_make(menu_entry *entries, int amount, char *toptext){ - int currentpos = 0, i, res = 0, offset = 0, delay = 300; + int currentpos = 0, i, res = 0, offset = 0, delay = 300, minscreen = 0, maxscreen = 59; u32 scrolltimer, timer; bool refresh = false; @@ -64,6 +65,7 @@ int menu_make(menu_entry *entries, int amount, char *toptext){ while (!(res & BTN_POWER)){ gfx_con_setpos(0, 47); timer = get_tmr_ms(); + refresh = false; if (!currentpos){ while (currentpos < amount && entries[currentpos].property & (ISSKIP | ISHIDE)) @@ -74,11 +76,25 @@ int menu_make(menu_entry *entries, int amount, char *toptext){ currentpos--; } + if (currentpos > maxscreen){ + offset += currentpos - maxscreen; + minscreen += currentpos - maxscreen; + maxscreen += currentpos - maxscreen; + refresh = true; + } + + if (currentpos < minscreen){ + offset -= minscreen - currentpos; + maxscreen -= minscreen - currentpos; + minscreen -= minscreen - currentpos; + refresh = true; + } + for (int i = 0 + offset; i < amount && i < 60 + offset; i++) 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 ", COLOR_BLUE, COLOR_DEFAULT, (offset + 60 < amount) ? "v" : " ", (offset > 0) ? "^" : " ", get_tmr_ms() - timer); + 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); while (btn_read() & BTN_POWER); diff --git a/source/tegraexplorer/io.c b/source/tegraexplorer/io.c deleted file mode 100644 index 047cacd..0000000 --- a/source/tegraexplorer/io.c +++ /dev/null @@ -1,590 +0,0 @@ -#include -#include "../mem/heap.h" -#include "gfx.h" -#include "fs.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 "io.h" -#include "emmc.h" -#include "../storage/sdmmc.h" -#include "../storage/nx_emmc.h" -#include "../utils/types.h" -#include "../storage/emummc.h" - -extern sdmmc_storage_t storage; -extern emmc_part_t *system_part; - -bool checkfile(char* path){ - FRESULT fr; - FILINFO fno; - - fr = f_stat(path, &fno); - - return !(fr & FR_NO_FILE); -} - -void viewbytes(char *path){ - FIL in; - u8 print[2048]; - u32 size; - QWORD offset = 0; - int res; - - clearscreen(); - res = f_open(&in, path, FA_READ | FA_OPEN_EXISTING); - if (res != FR_OK){ - message(COLOR_RED, "File Opening Failed\nErrcode %d", res); - return; - } - - msleep(200); - - while (1){ - f_lseek(&in, offset * 16); - - res = f_read(&in, &print, 2048 * sizeof(u8), &size); - if (res != FR_OK){ - message(COLOR_RED, "Reading Failed!\nErrcode %d", res); - return; - } - - printbytes(print, size, offset * 16); - res = btn_read(); - - if (!res) - res = btn_wait(); - - if (res & BTN_VOL_DOWN && 2048 * sizeof(u8) == size) - offset++; - if (res & BTN_VOL_UP && offset > 0) - offset--; - if (res & BTN_POWER) - break; - } - f_close(&in); -} - -int copy(const char *locin, const char *locout, bool print, bool canCancel){ - FIL in, out; - FILINFO in_info; - u64 sizeoffile, sizecopied = 0, totalsize; - UINT temp1, temp2; - u8 *buff; - unsigned int x, y, i = 0; - int res; - - gfx_con_getpos(&x, &y); - - if (!strcmp(locin, locout)){ - return 21; - } - - if ((res = f_open(&in, locin, FA_READ | FA_OPEN_EXISTING))){ - return 22; - } - - if (f_stat(locin, &in_info)) - return 22; - - if (f_open(&out, locout, FA_CREATE_ALWAYS | FA_WRITE)){ - return 23; - } - - buff = malloc (BUFSIZE); - sizeoffile = f_size(&in); - totalsize = sizeoffile; - - while (sizeoffile > 0){ - if ((res = f_read(&in, buff, (sizeoffile > BUFSIZE) ? BUFSIZE : sizeoffile, &temp1))) - return res; - if ((res = f_write(&out, buff, (sizeoffile > BUFSIZE) ? BUFSIZE : sizeoffile, &temp2))) - return res; - - if (temp1 != temp2) - return 24; - - sizeoffile -= temp1; - sizecopied += temp1; - - if (print && 10 > i++){ - gfx_printf("%k[%d%%]%k", COLOR_GREEN, ((sizecopied * 100) / totalsize) ,COLOR_WHITE); - gfx_con_setpos(x, y); - - i = 0; - - if (canCancel) - if (btn_read() & BTN_VOL_DOWN){ - f_unlink(locout); - break; - } - } - } - - f_close(&in); - f_close(&out); - free(buff); - - if ((res = f_chmod(locout, in_info.fattrib, 0x3A))) - return res; - - f_stat(locin, &in_info); //somehow stops fatfs from being weird - return 0; -} - -u64 getfilesize(char *path){ - FILINFO fno; - f_stat(path, &fno); - return fno.fsize; -} - -int getfolderentryamount(const char *path){ - DIR dir; - FILINFO fno; - int folderamount = 0; - - if ((f_opendir(&dir, path))){ - return -1; - } - - while (!f_readdir(&dir, &fno) && fno.fname[0]){ - folderamount++; - } - - f_closedir(&dir); - return folderamount; -} - -void makestring(char *in, char **out){ - *out = (char *) malloc (strlen(in) + 1); - strcpy(*out, in); -} - -int del_recursive(char *path){ - DIR dir; - FILINFO fno; - int res; - u32 x, y; - char *localpath = NULL; - - gfx_con_getpos(&x, &y); - makestring(path, &localpath); - - if ((res = f_opendir(&dir, localpath))){ - //message(COLOR_RED, "Error during f_opendir: %d", res); - return -1; - } - - while (!f_readdir(&dir, &fno) && fno.fname[0]){ - if (fno.fattrib & AM_DIR){ - del_recursive(getnextloc(localpath, fno.fname)); - } - else { - gfx_box(0, y, 719, y + 16, COLOR_DEFAULT); - SWAPCOLOR(COLOR_RED); - gfx_printf("\r"); - gfx_print_length(37, fno.fname); - gfx_printf(" "); - - if ((res = f_unlink(getnextloc(localpath, fno.fname)))) - return res; - } - } - - f_closedir(&dir); - - if ((res = f_unlink(localpath))){ - return res; - } - - free(localpath); - - return 0; -} - -int copy_recursive(char *path, char *dstpath){ - DIR dir; - FILINFO fno; - int res; - u32 x, y; - char *startpath = NULL, *destpath = NULL, *destfoldername = NULL, *temp = NULL; - - gfx_con_getpos(&x, &y); - - makestring(path, &startpath); - makestring(strrchr(path, '/') + 1, &destfoldername); - - destpath = (char*) malloc (strlen(dstpath) + strlen(destfoldername) + 2); - sprintf(destpath, (dstpath[strlen(dstpath) - 1] == '/') ? "%s%s" : "%s/%s", dstpath, destfoldername); - - if ((res = f_opendir(&dir, startpath))){ - message(COLOR_RED, "Error during f_opendir: %d", res); - return 21; - } - - f_mkdir(destpath); - - while (!f_readdir(&dir, &fno) && fno.fname[0]){ - if (fno.fattrib & AM_DIR){ - copy_recursive(getnextloc(startpath, fno.fname), destpath); - } - else { - gfx_box(0, y, 719, y + 16, COLOR_DEFAULT); - SWAPCOLOR(COLOR_GREEN); - gfx_printf("\r"); - gfx_print_length(37, fno.fname); - gfx_printf(" "); - makestring(getnextloc(startpath, fno.fname), &temp); - - if ((res = copy(temp, getnextloc(destpath, fno.fname), true, false))){ - return res; - } - - free(temp); - } - } - - f_closedir(&dir); - - if (f_stat(startpath, &fno)) - return 22; - - if ((res = f_chmod(destpath, fno.fattrib, 0x3A))) - return res; - - free(startpath); - free(destpath); - free(destfoldername); - - return 0; -} - -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(16384, sizeof(u8)); - - if ((res = f_open(&fp, path, FA_CREATE_ALWAYS | FA_WRITE))){ - message(COLOR_RED, "f_open() failed! err: %d", res); - return -1; - } - - f_lseek(&fp, totalSize); - f_lseek(&fp, 0); - - while (totalSectors > 0){ - num = MIN(totalSectors, 32); - if (!emummc_storage_read(mmcstorage, lba_curr, num, buf)){ - message(COLOR_RED, "eMMC read failed!"); - return -1; - } - if ((res = f_write(&fp, buf, num * NX_EMMC_BLOCKSIZE, NULL))){ - message(COLOR_RED, "f_write() failed! err: %d", res); - 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); - 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; - - sdmmc_storage_set_mmc_partition(&storage, i + 1); - makestring(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])){ - message(COLOR_RED, "Find of partition failed!\nPart: %s", pkg2names[i]); - return -1; - } - - makestring(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; -} - -int restore_emmc_part(char *path, sdmmc_storage_t *mmcstorage, emmc_part_t *part){ - FIL fp; - FILINFO fno; - u8 *buf; - u32 lba_curr = part->lba_start; - u32 bytesWritten = 0; - u32 totalSectorsDest = part->lba_end - part->lba_start + 1; - u64 totalSizeDest = (u64)((u64)totalSectorsDest << 9); - u64 totalSize; - u32 num = 0; - u32 pct = 0; - int res; - - gfx_printf("Initializing\r"); - - buf = calloc(16384, sizeof(u8)); - - if (!buf){ - message(COLOR_RED, "Calloc returned null!"); - return -1; - } - - if ((res = f_stat(path, &fno))){ - message(COLOR_RED, "f_stat() failed! err: %d", res); - return -1; - } - - totalSize = fno.fsize; - u64 totalSectors = totalSize / NX_EMMC_BLOCKSIZE; - - if (totalSize > totalSizeDest){ - message(COLOR_RED, "File too big for destenation!"); - return -1; - } - - gfx_printf("Flashing %s\n", part->name); - - if (totalSize < totalSizeDest){ - SWAPCOLOR(COLOR_ORANGE); - u8 btnres = makewaitmenunoclear( - "File is too small for destenation.\nDo you want to flash it anyway?\n\nVol +/- to Cancel\n", - "Power to Confirm", - 2 - ); - - RESETCOLOR; - - if (!btnres){ - gfx_printf("\nCancelled: %s\n\n", part->name); - return 0; - } - else - gfx_printf("\nFlashing %s\n", part->name); - } - - if ((res = f_open(&fp, path, FA_OPEN_ALWAYS | FA_READ))){ - message(COLOR_RED, "f_open() failed! err: %d", res); - return -1; - } - - while (totalSectors > 0){ - num = MIN(totalSectors, 32); - - if ((res = f_read(&fp, buf, num * NX_EMMC_BLOCKSIZE, NULL))){ - message(COLOR_RED, "f_read() failed! err: %d", res); - return -1; - } - - if (!emummc_storage_write(mmcstorage, lba_curr, num, buf)){ - message(COLOR_RED, "eMMC write failed!"); - return -1; - } - - lba_curr += num; - totalSectors -= num; - bytesWritten += num * NX_EMMC_BLOCKSIZE; - - pct = (u64)((u64)(bytesWritten) * 100u) / (u64)(fno.fsize); - gfx_printf("Progress: %d%%\r", pct); - } - - gfx_printf(" \r"); - f_close(&fp); - free(buf); - return 0; -} - -int restore_emmc_file(char *path, const char *target, u8 partition, u8 mmctype){ - connect_mmc(mmctype); - - if (partition){ - 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; - - strcpy(bootPart.name, target); - - sdmmc_storage_set_mmc_partition(&storage, partition); - restore_emmc_part(path, &storage, &bootPart); - } - else { - if (connect_part(target)){ - message(COLOR_RED, "Find of partition failed!\nPart: %s", target); - return -1; - } - restore_emmc_part(path, &storage, system_part); - } - return 0; -} - -int restore_bis_using_file(char *path, u8 mmctype){ - f_mkdir("sd:/tegraexplorer"); - f_mkdir("sd:/tegraexplorer/bis"); - clearscreen(); - - if (extract_bis_file(path, "sd:/tegraexplorer/bis")){ - message(COLOR_ORANGE, "Failed to find file!"); - return -1; - } - - if (!makewaitmenunoclear( - "\nAre you sure you want to flash these files\nThis could leave your switch unbootable!\n\nVol +/- to cancel\n", - "Power to confirm", - 5 - )) - { - return 0; - } - - gfx_printf("\nRestoring BIS...\n\n"); - - restore_emmc_file("sd:/tegraexplorer/bis/BOOT0.bin", "BOOT0", 1, mmctype); - restore_emmc_file("sd:/tegraexplorer/bis/BOOT1.bin", "BOOT1", 2, mmctype); - restore_emmc_file("sd:/tegraexplorer/bis/BCPKG2-1-Normal-Main", pkg2names[0], 0, mmctype); - restore_emmc_file("sd:/tegraexplorer/bis/BCPKG2-1-Normal-Main", pkg2names[1], 0, mmctype); - restore_emmc_file("sd:/tegraexplorer/bis/BCPKG2-3-SafeMode-Main", pkg2names[2], 0, mmctype); - restore_emmc_file("sd:/tegraexplorer/bis/BCPKG2-3-SafeMode-Main", pkg2names[3], 0, mmctype); - - gfx_printf("\n\nDone!\nPress any button to return"); - btn_wait(); - - return 0; -} - -u32 DecodeInt(u8* data) { - u32 out = 0; - - for (int i = 0; i < 4; i++) { - out |= (data[i] << ((3 - i) * 8)); - } - - return out; -} - -void copy_fil_size(FIL* in_src, FIL* out_src, int size_src){ - u8* buff; - buff = calloc(16384, sizeof(u8)); - - int size = size_src; - int copysize; - - while (size > 0){ - copysize = MIN(16834, size); - f_read(in_src, buff, copysize, NULL); - f_write(out_src, buff, copysize, NULL); - size -= copysize; - } -} - -void gen_part(int size, FIL* in, char *path){ - FIL out; - - f_open(&out, path, FA_WRITE | FA_CREATE_ALWAYS); - copy_fil_size(in, &out, size); - f_close(&out); -} - -int extract_bis_file(char *path, char *outfolder){ - FIL in; - int res; - u8 version[0x10]; - u8 args; - u8 temp[0x4]; - u32 filesizes[4]; - - if ((res = f_open(&in, path, FA_READ | FA_OPEN_EXISTING))){ - message(COLOR_RED, "Opening file failed! err: %d", res); - return -1; - } - - f_read(&in, version, 0x10, NULL); - f_read(&in, &args, 1, NULL); - - for (int i = 0; i < 4; i++){ - f_read(&in, temp, 4, NULL); - filesizes[i] = DecodeInt(temp); - } - - gfx_printf("Version: %s\n\n", version); - - /* - for (int i = 0; i < 4; i++) { - gfx_printf("%d ", filesizes[i]); - } - */ - - gfx_printf("\nExtracting BOOT0\n"); - if (args & BOOT0_ARG) - gen_part(filesizes[0], &in, getnextloc(outfolder, "BOOT0.bin")); - - gfx_printf("Extracting BOOT1\n"); - if (args & BOOT1_ARG) - gen_part(filesizes[1], &in, getnextloc(outfolder, "BOOT1.bin")); - - gfx_printf("Extracting BCPKG2_1\n"); - if (args & BCPKG2_1_ARG) - gen_part(filesizes[2], &in, getnextloc(outfolder, "BCPKG2-1-Normal-Main")); - - gfx_printf("Extracting BCPKG2_3\n"); - if (args & BCPKG2_3_ARG) - gen_part(filesizes[3], &in, getnextloc(outfolder, "BCPKG2-3-SafeMode-Main")); - - gfx_printf("\n\nDone!\n"); - - f_close(&in); - return 0; -} \ No newline at end of file diff --git a/source/tegraexplorer/io.h b/source/tegraexplorer/io.h deleted file mode 100644 index bc1b7cb..0000000 --- a/source/tegraexplorer/io.h +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once - -bool checkfile(char* path); -void viewbytes(char *path); -int copy(const char *locin, const char *locout, bool print, bool canCancel); -u64 getfilesize(char *path); -int getfolderentryamount(const char *path); -void makestring(char *in, char **out); -int del_recursive(char *path); -int copy_recursive(char *path, char *dstpath); -int dump_emmc_parts(u16 parts, u8 mmctype); -int extract_bis_file(char *path, char *outfolder); -int restore_bis_using_file(char *path, u8 mmctype); - -#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 - -static 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/te.c b/source/tegraexplorer/te.c index d573f97..faff565 100644 --- a/source/tegraexplorer/te.c +++ b/source/tegraexplorer/te.c @@ -1,11 +1,8 @@ #include #include #include "te.h" -#include "gfx.h" #include "../utils/util.h" #include "utils/tools.h" -#include "fs.h" -#include "io.h" #include "../utils/btn.h" #include "emmc.h" #include "../storage/emummc.h" @@ -16,6 +13,8 @@ #include "utils/utils.h" #include "gfx/gfxutils.h" +#include "fs/fsutils.h" +#include "fs/fsmenu.h" extern bool sd_mount(); extern void sd_unmount(); @@ -75,20 +74,22 @@ menu_item mmcChoice[3] = { -int res = 0; +int res = 0, meter = 0; void MainMenu_SDCard(){ - fileexplorer("SD:/"); + fileexplorer("SD:/", 0); } void MainMenu_EMMC(){ - if (makewaitmenu("You're about to enter EMMC\nModifying anything here\n can result in a BRICK!\n\nPlease only continue\n if you know what you're doing\n\nPress Vol+/- to return\n", "Press Power to enter", 4)){ + gfx_clearscreen(); + gfx_printf("You're about to enter EMMC\nModifying anything here\n can result in a BRICK!\n\nPlease only continue\n if you know what you're doing\n\nPress Vol+/- to return\n"); + if (gfx_makewaitmenu("Press Power to enter", 4)){ connect_mmc(SYSMMC); if (!mount_mmc(emmc_fs_entries[res - 2], res - 1)) - fileexplorer("emmc:/"); + fileexplorer("emmc:/", 1); else - message(COLOR_RED, "EMMC failed to mount!"); + gfx_message(COLOR_RED, "EMMC failed to mount!"); } } @@ -96,9 +97,10 @@ void MainMenu_EMUMMC(){ connect_mmc(EMUMMC); if (!mount_mmc(emmc_fs_entries[res - 5], res - 4)) - fileexplorer("emmc:/"); + fileexplorer("emmc:/", 1); else - message(COLOR_RED, "EMUMMC failed to mount!"); + gfx_message(COLOR_RED, "EMUMMC failed to mount!"); + } void MainMenu_MountSD(){ @@ -128,11 +130,12 @@ 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: - if (makewaitmenu( - "WARNING!\nThis will mess with your switch boot files\nMake a nand backup beforehand!\n\nThis will pull from path:\nsd:/tegraexplorer/boot.bis\n\nVol +/- to cancel\n", + /* + gfx_printf("WARNING!\nThis will mess with your switch boot files\nMake a nand backup beforehand!\n\nThis will pull from path:\nsd:/tegraexplorer/boot.bis\n\nVol +/- to cancel\n"); + if (gfx_makewaitmenu( "Power to confirm", 5 )) @@ -140,6 +143,7 @@ void MainMenu_Tools(){ if ((res = utils_mmcMenu()) > 0) restore_bis_using_file("sd:/tegraexplorer/boot.bis", res); } + */ break; } } @@ -149,7 +153,9 @@ void MainMenu_SDFormat(){ res = menu_make(mainmenu_format, 3, "-- Format Menu --"); if (res > 0){ - if(makewaitmenu("Are you sure you want to format your sd?\nThis will delete everything on your SD card\nThis action is irreversible!\n\nPress Vol+/- to cancel\n", "Press Power to continue", 10)){ + gfx_clearscreen(); + gfx_printf("Are you sure you want to format your sd?\nThis will delete everything on your SD card\nThis action is irreversible!\n\nPress Vol+/- to cancel\n"); + if(gfx_makewaitmenu("Press Power to continue", 10)){ if (format(res)){ sd_unmount(); } @@ -158,7 +164,9 @@ void MainMenu_SDFormat(){ } void MainMenu_Credits(){ - message(COLOR_WHITE, CREDITS_MESSAGE); + if (++meter >= 3) + gfx_errDisplay("credits", 53, 0); + gfx_message(COLOR_WHITE, CREDITS_MESSAGE); } void MainMenu_Exit(){ @@ -168,8 +176,8 @@ void MainMenu_Exit(){ shutdownmenu[6].property = (checkfile("/atmosphere/reboot_payload.bin")) ? 1 : -1; */ - SETBIT(mainmenu_shutdown[4].property, ISHIDE, !checkfile("/bootloader/update.bin")); - SETBIT(mainmenu_shutdown[5].property, ISHIDE, !checkfile("/atmosphere/reboot_payload.bin")); + SETBIT(mainmenu_shutdown[4].property, ISHIDE, !fsutil_checkfile("/bootloader/update.bin")); + SETBIT(mainmenu_shutdown[5].property, ISHIDE, !fsutil_checkfile("/atmosphere/reboot_payload.bin")); } else { for (int i = 4; i <= 5; i++) @@ -213,6 +221,8 @@ part_handler mainmenu_functions[] = { }; void RunMenuOption(int option){ + if (option != 11) + meter = 0; if (option > 0) mainmenu_functions[option - 1](); } @@ -256,7 +266,7 @@ void te_main(){ int setter; if (dump_biskeys() == -1){ - message(COLOR_RED, "Biskeys failed to dump!\nEmmc will not be mounted!"); + gfx_message(COLOR_RED, "Biskeys failed to dump!\nEmmc will not be mounted!"); for (int i = 1; i <= 3; i++) mainmenu_main[i].property |= ISHIDE; } diff --git a/source/tegraexplorer/utils/script.c b/source/tegraexplorer/utils/script.c index d2f04cf..27b3aa4 100644 --- a/source/tegraexplorer/utils/script.c +++ b/source/tegraexplorer/utils/script.c @@ -1,8 +1,6 @@ #include #include "../../mem/heap.h" #include "../gfx/gfxutils.h" -#include "../fs.h" -#include "../io.h" #include "../emmc.h" #include "../../utils/types.h" #include "../../libs/fatfs/ff.h" @@ -13,6 +11,7 @@ #include "../../storage/emummc.h" #include "script.h" #include "../common/common.h" +#include "../fs/fsactions.h" #include @@ -81,15 +80,15 @@ void Part_Delete(){ } void Part_DeleteRecursive(){ - errcode = del_recursive(args[0]); + errcode = fsact_del_recursive(args[0]); } void Part_Copy(){ - errcode = copy(args[0], args[1], true, false); + errcode = fsact_copy(args[0], args[1], COPY_MODE_PRINT); } void Part_RecursiveCopy(){ - errcode = copy_recursive(args[0], args[1]); + errcode = fsact_copy_recursive(args[0], args[1]); } void Part_MakeFolder(){ @@ -186,7 +185,7 @@ void ParseScript(char* path){ res = f_open(&in, path, FA_READ | FA_OPEN_EXISTING); if (res != FR_OK){ - gfx_errprint("ParseScript", res, 1); + gfx_errDisplay("ParseScript", res, 1); return; } diff --git a/source/tegraexplorer/utils/tools.c b/source/tegraexplorer/utils/tools.c index d570295..86217ee 100644 --- a/source/tegraexplorer/utils/tools.c +++ b/source/tegraexplorer/utils/tools.c @@ -11,9 +11,8 @@ #include "../../utils/sprintf.h" #include "../../soc/fuse.h" #include "../emmc.h" -#include "../fs.h" -#include "../io.h" #include "../common/common.h" +#include "../fs/fsactions.h" extern bool sd_mount(); extern void sd_unmount(); @@ -126,7 +125,7 @@ int dumpfirmware(int mmc){ else sprintf(syspath, "%s/%s", path, fno.fname); - ret = copy(syspath, sdpath, false, false); + ret = fsact_copy(syspath, sdpath, 0); gfx_printf("%d %s\r", ++amount, fno.fname); @@ -156,7 +155,7 @@ void dumpusersaves(int mmc){ SWAPCOLOR(COLOR_GREEN); - res = copy_recursive("emmc:/save", "sd:/tegraexplorer"); + res = fsact_copy_recursive("emmc:/save", "sd:/tegraexplorer"); SWAPCOLOR(COLOR_ORANGE); gfx_printf("\rResult copy_recursive() %d\n\n", res); diff --git a/source/tegraexplorer/utils/utils.c b/source/tegraexplorer/utils/utils.c index 0a98b50..e3f9bb0 100644 --- a/source/tegraexplorer/utils/utils.c +++ b/source/tegraexplorer/utils/utils.c @@ -1,7 +1,9 @@ +#include #include "utils.h" #include "../common/common.h" #include "../gfx/menu.h" #include "../../storage/emummc.h" +#include "../../mem/heap.h" int utils_mmcMenu(){ int res; @@ -10,4 +12,10 @@ int utils_mmcMenu(){ return menu_make(utils_mmcChoice, 3, "-- Choose MMC --"); else return SYSMMC; +} + +void utils_copystring(const char *in, char **out){ + int len = strlen(in) + 1; + *out = (char *) malloc (len); + strcpy(*out, in); } \ No newline at end of file diff --git a/source/tegraexplorer/utils/utils.h b/source/tegraexplorer/utils/utils.h index eff2122..ffeed42 100644 --- a/source/tegraexplorer/utils/utils.h +++ b/source/tegraexplorer/utils/utils.h @@ -1,3 +1,4 @@ #pragma once -int utils_mmcMenu(); \ No newline at end of file +int utils_mmcMenu(); +void utils_copystring(const char *in, char **out); \ No newline at end of file