Move over script and tools

This commit is contained in:
Such Meme, Many Skill
2020-03-18 12:18:39 +01:00
parent c60eb4b722
commit ff618bc285
10 changed files with 67 additions and 44 deletions

View File

@@ -0,0 +1,284 @@
#include <string.h>
#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"
#include "../../utils/sprintf.h"
#include "../../utils/btn.h"
#include "../../gfx/gfx.h"
#include "../../utils/util.h"
#include "../../storage/emummc.h"
#include "script.h"
#include "../common/common.h"
#include <stdlib.h>
char func[11] = "", args[2][128] = {"", ""};
int res, errcode;
const int scriptver = 133;
bool forceExit = false;
u32 currentcolor;
void Part_CheckFile(){
FILINFO fno;
errcode = f_stat(args[0], &fno);
}
void Part_SetColor(){
if (strcmpcheck(args[0], "RED"))
currentcolor = COLOR_RED;
else if (strcmpcheck(args[0], "ORANGE"))
currentcolor = COLOR_ORANGE;
else if (strcmpcheck(args[0], "YELLOW"))
currentcolor = COLOR_YELLOW;
else if (strcmpcheck(args[0], "GREEN"))
currentcolor = COLOR_GREEN;
else if (strcmpcheck(args[0], "BLUE"))
currentcolor = COLOR_BLUE;
else if (strcmpcheck(args[0], "VIOLET"))
currentcolor = COLOR_VIOLET;
else if (strcmpcheck(args[0], "WHITE"))
currentcolor = COLOR_WHITE;
}
void Part_Wait(){
int waitamount, begintime;
SWAPCOLOR(currentcolor);
waitamount = atoi(args[0]);
begintime = get_tmr_s();
while (begintime + waitamount > get_tmr_s()){
gfx_printf("\r<Wait %d seconds> ", (begintime + waitamount) - get_tmr_s());
}
gfx_printf("\r \r");
}
void Part_VersionCheck(){
int givenversion = atoi(args[0]);
if (givenversion > scriptver){
gfx_printf("Script required version is too high\nUpdate TegraExplorer!");
btn_wait();
forceExit = true;
}
}
void Part_Move(){
errcode = f_rename(args[0], args[1]);
if (errcode)
f_rename(args[0], args[1]);
}
void Part_Delete(){
errcode = f_unlink(args[0]);
if (errcode)
f_unlink(args[0]);
}
void Part_DeleteRecursive(){
errcode = del_recursive(args[0]);
}
void Part_Copy(){
errcode = copy(args[0], args[1], true, false);
}
void Part_RecursiveCopy(){
errcode = copy_recursive(args[0], args[1]);
}
void Part_MakeFolder(){
errcode = f_mkdir(args[0]);
if (errcode)
f_mkdir(args[0]);
}
void Part_ConnectMMC(){
if (strcmpcheck(args[0], "SYSMMC"))
connect_mmc(SYSMMC);
if (strcmpcheck(args[0], "EMUMMC"))
connect_mmc(EMUMMC);
}
void Part_MountMMC(){
errcode = mount_mmc(args[0], 2);
}
void Part_Print(){
RESETCOLOR;
SWAPCOLOR(currentcolor);
gfx_printf("%s\n", args[0]);
}
void Part_ErrorPrint(){
RESETCOLOR;
SWAPCOLOR(COLOR_RED);
gfx_printf("Errorcode: %d\n", errcode);
}
void Part_Exit(){
forceExit = true;
}
u8 buttons_pressed = 0;
void Part_WaitOnUser(){
buttons_pressed = btn_wait();
}
script_parts parts[] = {
{"COPY", Part_Copy, 2},
{"COPY-R", Part_RecursiveCopy, 2},
{"MKDIR", Part_MakeFolder, 1},
{"CON_MMC", Part_ConnectMMC, 1},
{"MNT_MMC", Part_MountMMC, 1},
{"PRINT", Part_Print, 1},
{"ERRPRINT", Part_ErrorPrint, 0},
{"EXIT", Part_Exit, 0},
{"PAUSE", Part_WaitOnUser, 0},
{"DEL", Part_Delete, 1},
{"DEL-R", Part_DeleteRecursive, 1},
{"MOVE", Part_Move, 2},
{"VERSION", Part_VersionCheck, 1},
{"WAIT", Part_Wait, 1},
{"COLOR", Part_SetColor, 1},
{"CHECKPATH", Part_CheckFile, 1},
{"NULL", NULL, -1}
};
int ParsePart(){
int i;
for (i = 0; parts[i].arg_amount != -1; i++){
if (strcmpcheck(func, parts[i].name))
return i;
}
gfx_printf("Parsing error...\nPress any key to continue");
btn_wait();
forceExit = true;
return -1;
}
FIL in;
UINT endByte = 0;
char GetNextByte(){
char single;
f_read(&in, &single, sizeof(char), &endByte);
if (sizeof(char) != endByte)
forceExit = true;
return single;
}
void ParseScript(char* path){
char currentchar;
int strlength;
bool inifstatement = false;
forceExit = false;
currentcolor = COLOR_WHITE;
gfx_clearscreen();
res = f_open(&in, path, FA_READ | FA_OPEN_EXISTING);
if (res != FR_OK){
gfx_errprint("ParseScript", res, 1);
return;
}
while (!forceExit){
currentchar = GetNextByte();
if (endByte == 0 || currentchar == (char)EOF)
break;
switch(currentchar){
case '{':
if (!inifstatement)
while (currentchar != '}')
currentchar = GetNextByte();
break;
case '}':
if (inifstatement)
inifstatement = false;
break;
case '<':
strlength = 0;
currentchar = GetNextByte();
while (currentchar != '>'){
func[strlength++] = currentchar;
currentchar = GetNextByte();
}
func[strlength] = '\0';
res = ParsePart();
if (res == -1)
break;
for (int i = 0; i < parts[res].arg_amount; i++){
while (currentchar != 0x22)
currentchar = GetNextByte();
strlength = 0;
currentchar = GetNextByte();
while (currentchar != 0x22){
args[i][strlength++] = currentchar;
currentchar = GetNextByte();
}
args[i][strlength] = '\0';
if (i < parts[res].arg_amount)
currentchar = GetNextByte();
}
parts[res].handler();
break;
case '$':
strlength = 0;
currentchar = GetNextByte();
while (currentchar != '$'){
func[strlength++] = currentchar;
currentchar = GetNextByte();
}
func[strlength] = '\0';
if (strcmpcheck(func, "ERROR") || strcmpcheck(func, "TRUE")){
inifstatement = (errcode);
}
else if (strcmpcheck(func, "NOERROR") || strcmpcheck(func, "FALSE")){
inifstatement = (!errcode);
}
else if (strcmpcheck(func, "BTN_POWER")){
inifstatement = (buttons_pressed & BTN_POWER);
}
else if (strcmpcheck(func, "BTN_VOL+")){
inifstatement = (buttons_pressed & BTN_VOL_UP);
}
else if (strcmpcheck(func, "BTN_VOL-")){
inifstatement = (buttons_pressed & BTN_VOL_DOWN);
}
else if (strcmpcheck(func, "EMUMMC")){
inifstatement = (emu_cfg.enabled);
}
else if (strcmpcheck(func, "NOEMUMMC")){
inifstatement = (!emu_cfg.enabled);
}
if (inifstatement)
while(currentchar != '{')
currentchar = GetNextByte();
break;
}
}
f_close(&in);
}

View File

@@ -0,0 +1,11 @@
#pragma once
#define strcmpcheck(x, y) (!strcmp(x, y))
typedef void (*part_handler)();
typedef struct _script_parts {
char name[11];
part_handler handler;
short arg_amount;
} script_parts;
void ParseScript(char* path);

View File

@@ -0,0 +1,250 @@
#include "tools.h"
#include "../gfx/gfxutils.h"
#include "../../libs/fatfs/ff.h"
#include "../../gfx/gfx.h"
#include "../../utils/btn.h"
#include "../../soc/gpio.h"
#include "../../utils/util.h"
#include "../../utils/types.h"
#include "../../libs/fatfs/diskio.h"
#include "../../storage/sdmmc.h"
#include "../../utils/sprintf.h"
#include "../../soc/fuse.h"
#include "../emmc.h"
#include "../fs.h"
#include "../io.h"
#include "../common/common.h"
extern bool sd_mount();
extern void sd_unmount();
extern sdmmc_storage_t sd_storage;
void displayinfo(){
gfx_clearscreen();
FATFS *fs;
DWORD fre_clust, fre_sect, tot_sect;
u32 capacity;
u8 fuse_count = 0;
pkg1_info pkg1 = returnpkg1info();
int res;
for (u32 i = 0; i < 32; i++){
if ((fuse_read_odm(7) >> i) & 1)
fuse_count++;
}
SWAPCOLOR(COLOR_ORANGE);
gfx_printf("Fuse count: %d\nPKG1 version: %d\nPKG1 id: %s\n\n", fuse_count, pkg1.ver, pkg1.id);
print_biskeys();
RESETCOLOR;
gfx_printf("\n-----\n\n");
SWAPCOLOR(COLOR_BLUE);
if (!sd_mount()){
gfx_printf("SD mount failed!\nFailed to display SD info\n");
}
else {
gfx_printf("Getting storage info: please wait...");
res = f_getfree("sd:", &fre_clust, &fs);
gfx_printf("\nResult getfree: %d\n\n", res);
tot_sect = (fs->n_fatent - 2) * fs->csize;
fre_sect = fre_clust * fs->csize;
capacity = sd_storage.csd.capacity;
gfx_printf("Entire sd:\nSectors: %d\nSpace total: %d MB\n\n", capacity, capacity / 2048);
gfx_printf("First partition on SD:\nSectors: %d\nSpace total: %d MB\nSpace free: %d MB\n\n", tot_sect, tot_sect / 2048, fre_sect / 2048);
}
RESETCOLOR;
gfx_printf("Press any key to continue");
btn_wait();
}
void displaygpio(){
int res;
gfx_clearscreen();
gfx_printf("Updates gpio pins every 50ms:\nPress power to exit");
msleep(200);
while (1){
msleep(10);
gfx_con_setpos(0, 63);
for (int i = 0; i <= 30; i++){
gfx_printf("\nPort %d: ", i);
for (int i2 = 7; i2 >= 0; i2--)
gfx_printf("%d", gpio_read(i, (1 << i2)));
}
res = btn_read();
if (res & BTN_POWER)
break;
}
}
int dumpfirmware(int mmc){
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] = "";
pkg1_info pkg1 = returnpkg1info();
u32 timer = get_tmr_s();
gfx_clearscreen();
connect_mmc(mmc);
mount_mmc("SYSTEM", 2);
gfx_printf("PKG1 version: %d\n", pkg1.ver);
ret = f_mkdir("sd:/tegraexplorer");
gfx_printf("Creating 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 (%s)", pkg1.ver, pkg1.id);
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, 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 dumpusersaves(int mmc){
int res;
connect_mmc(mmc);
mount_mmc("USER", 2);
gfx_clearscreen();
res = f_mkdir("sd:/tegraexplorer");
gfx_printf("Creating sd:/tegraexplorer, res: %d\nCopying:\n", res);
SWAPCOLOR(COLOR_GREEN);
res = copy_recursive("emmc:/save", "sd:/tegraexplorer");
SWAPCOLOR(COLOR_ORANGE);
gfx_printf("\rResult copy_recursive() %d\n\n", res);
if (res){
SWAPCOLOR(COLOR_RED);
gfx_printf("Dump failed!\n");
}
else
gfx_printf("Saves are located in SD:/tegraexplorer/save\n");
gfx_printf("Press any key to continue");
btn_wait();
}
int format(int mode){
gfx_clearscreen();
int res;
bool fatalerror = false;
DWORD plist[] = {666, 61145088};
u32 timer, totalsectors, alignedsectors, extrasectors;
BYTE work[FF_MAX_SS];
DWORD clustsize = 32768;
BYTE formatoptions = 0;
formatoptions |= (FM_FAT32);
//formatoptions |= (FM_SFD);
disconnect_mmc();
timer = get_tmr_s();
totalsectors = sd_storage.csd.capacity;
if (mode == FORMAT_EMUMMC){
if (totalsectors < 83886080){
gfx_printf("%kYou seem to be running this on a <=32GB SD\nNot enough free space for emummc!", COLOR_RED);
fatalerror = true;
}
else {
totalsectors -= plist[1];
alignedsectors = (totalsectors / 2048) * 2048;
extrasectors = totalsectors - alignedsectors;
plist[0] = alignedsectors;
plist[1] += extrasectors;
gfx_printf("\nStarting SD partitioning:\nTotalSectors: %d\nPartition1 (SD): %d\nPartition2 (EMUMMC): %d\n", plist[0] + plist[1], plist[0], plist[1]);
}
}
else {
plist[0] = totalsectors;
plist[1] = 0;
}
if (!fatalerror){
gfx_printf("\nPartitioning SD...\n");
res = f_fdisk(0, plist, &work);
if (res){
gfx_printf("%kf_fdisk returned %d!\n", COLOR_RED, res);
fatalerror = true;
}
else
gfx_printf("Done!\n");
}
if (!fatalerror){
gfx_printf("\n\nFormatting Partition1...\n");
res = f_mkfs("0:", formatoptions, clustsize, &work, sizeof work);
if (res){
gfx_printf("%kf_mkfs returned %d!\n", COLOR_RED, res);
fatalerror = true;
}
else
gfx_printf("Smells like a formatted SD\n\n");
}
sd_unmount();
if (!fatalerror){
if (!sd_mount())
gfx_printf("%kSd failed to mount!\n", COLOR_ORANGE);
else {
gfx_printf("Sd mounted!\n");
}
}
connect_mmc(SYSMMC);
gfx_printf("\nPress any button to return%k\nTotal time taken: %ds", COLOR_WHITE, (get_tmr_s() - timer));
btn_wait();
return fatalerror;
}

View File

@@ -0,0 +1,7 @@
#pragma once
void displayinfo();
void displaygpio();
int format(int mode);
int dumpfirmware(int mmc);
void dumpusersaves(int mmc);