From 3ab95dfd64cc6b15d6c5359c06f5ff6a2078d7ba Mon Sep 17 00:00:00 2001 From: suchmememanyskill Date: Wed, 21 Jul 2021 15:19:27 +0200 Subject: [PATCH] add crude firmware dumping script --- scripts/FirmwareDump.te | 77 +++++++++++++++++++++++++ scripts/HelloWorld.te | 20 +++++-- source/script/genericClass.c | 2 - source/script/intClass.c | 9 +++ source/script/standardLibrary.c | 99 +++++++++++++++++++++++++++++++++ source/tegraexplorer/mainmenu.c | 5 +- 6 files changed, 202 insertions(+), 10 deletions(-) create mode 100644 scripts/FirmwareDump.te diff --git a/scripts/FirmwareDump.te b/scripts/FirmwareDump.te new file mode 100644 index 0000000..1a56086 --- /dev/null +++ b/scripts/FirmwareDump.te @@ -0,0 +1,77 @@ +if (mountsys("SYSTEM")){ + print("SYSTEM MOUNT FAIL") + exit() +} + +a = readsave("bis:/save/8000000000000120") +b = a.readFile("/meta/imkvdb.arc") +print(b.len(), "\n") + +c = ["BYTE[]", 0x09, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01] + +i = 1 +#while (i + c.len() < b.len()){ +# if (b.slice(i, 8).project() == c) {\ + +fuck = b.find(c) +if (fuck < 0) { + print("Not found") + exit() +} + +i = fuck + +d = b.slice(i + 8, 4).project() +ver = (d[3] << 24) | (d[2] << 16) | (d[1] << 8) | (d[0]) +rev = (ver & ((1 << 16) - 1)) +pat = ((ver >> 16) & ((1 << 4) - 1)) +min = ((ver >> 20) & ((1 << 6) - 1)) +maj = ((ver >> 26) & ((1 << 6) - 1)) +print("") +println(ver.str(), " ", maj.str(), ".", min.str(), ".", pat.str(), ".", rev.str()) + + +# i = i + 1 +#} + + +mkdir("sd:/tegraexplorer") +mkdir("sd:/tegraexplorer/Firmware") +baseSdPath = "sd:/tegraexplorer/Firmware/" + maj.str() + "." + min.str() + "." + pat.str() +mkdir(baseSdPath) + +contents = readdir("bis:/Contents/registered") +if (contents.result != 0){ + println("Fail reading dir") + exit() +} + +contents.files.foreach("x") { + fullPath = "bis:/Contents/registered/" + x + name = x + if (ncatype(fullPath) == 1){ + name = name - 4 + ".cnmt.nca" + } + print("\r", x) + fullSdPath = baseSdPath + "/" + name + if (filecopy(fullPath, fullSdPath)){ + println("\nErr during copy") + exit() + } +} + +contents.folders.foreach("x") { + fullPath = "bis:/Contents/registered/" + x + "/00" + name = x + if (ncatype(fullPath) == 1){ + name = name - 4 + ".cnmt.nca" + } + print("\r", x) + fullSdPath = baseSdPath + "/" + name + if (filecopy(fullPath, fullSdPath)){ + println("\nErr during copy") + exit() + } +} + +print("end") \ No newline at end of file diff --git a/scripts/HelloWorld.te b/scripts/HelloWorld.te index 280d909..e373aaa 100644 --- a/scripts/HelloWorld.te +++ b/scripts/HelloWorld.te @@ -5,8 +5,18 @@ a="Hello world!\n" a.print() -i=0 -while (i<10){ - println(i) - i=i+1 -} \ No newline at end of file +#i=0 +#while (i<10){ +# println(i) +# i=i+1 +#} + +a = readdir("sd:/tegraexplorer") +println(a.result) +println(a.files.len()) +println(a.folders.len()) +println(a.fileSizes.len()) + +a.files.foreach("b") { + println(b) +} \ No newline at end of file diff --git a/source/script/genericClass.c b/source/script/genericClass.c index 4aa0597..9dde959 100644 --- a/source/script/genericClass.c +++ b/source/script/genericClass.c @@ -255,8 +255,6 @@ Variable_t* callMemberFunctionDirect(Variable_t* var, char* memberName, Variable void freeVariableInternal(Variable_t* referencedTarget) { switch (referencedTarget->variableType) { case StringClass: - if (referencedTarget->string.free) - gfx_printf("FREE STRING GETTING FREED AAA"); FREE(referencedTarget->string.value); break; case StringArrayClass: diff --git a/source/script/intClass.c b/source/script/intClass.c index d03988e..065356b 100644 --- a/source/script/intClass.c +++ b/source/script/intClass.c @@ -1,7 +1,9 @@ #include "intClass.h" +#include "StringClass.h" #include "compat.h" #include #include +#include IntClass_t createIntClass(s64 in) { IntClass_t a = { in }; @@ -20,6 +22,12 @@ ClassFunction(printIntVariable) { return &emptyClass; } +ClassFunction(intToStr) { + char buff[64] = {0}; + s_printf(buff, "%d", getIntValue(caller)); + return newStringVariablePtr(CpyStr(buff), 0, 1); +} + #define IntOpFunction(name, op) ClassFunction(name) { s64 i1 = getIntValue(caller); s64 i2 = getIntValue(*args); return newIntVariablePtr((i1 op i2)); } IntOpFunction(addInt, +) @@ -52,6 +60,7 @@ u8 oneIntArgInt[] = { IntClass }; ClassFunctionTableEntry_t intFunctions[] = { {"print", printIntVariable, 0, 0}, {"not", notInt, 0, 0}, + {"str", intToStr, 0, 0}, IntOpFunctionEntry("+", addInt), IntOpFunctionEntry("-", minusInt), IntOpFunctionEntry("*", multInt), diff --git a/source/script/standardLibrary.c b/source/script/standardLibrary.c index cf7719e..6c26dd8 100644 --- a/source/script/standardLibrary.c +++ b/source/script/standardLibrary.c @@ -11,6 +11,10 @@ #ifndef WIN32 #include "../storage/mountmanager.h" #include "../keys/keys.h" +#include "../fs/readers/folderReader.h" +#include "../fs/fscopy.h" +#include +#include "../keys/nca.h" #endif ClassFunction(stdIf) { @@ -122,6 +126,79 @@ ClassFunction(stdSetPixel) { gfx_set_pixel_horz(args[0]->integer.value, args[1]->integer.value, color); return &emptyClass; } + +ClassFunction(stdReadDir){ + Vector_t dict = newVec(sizeof(Dict_t), 4); + Variable_t* resPtr = newIntVariablePtr(0); + Dict_t temp = {.name = CpyStr("result"), .var = resPtr}; + vecAdd(&dict, temp); + + Variable_t fileNamesArray = {.variableType = StringArrayClass, .solvedArray.vector = newVec(sizeof(char*), 0)}; + Variable_t dirNamesArray = {.variableType = StringArrayClass, .solvedArray.vector = newVec(sizeof(char*), 0)}; + Variable_t fileSizeArray = {.variableType = IntArrayClass, .solvedArray.vector = newVec(sizeof(s64), 0)}; + + DIR dir; + if ((resPtr->integer.value = f_opendir(&dir, args[0]->string.value))){ + Variable_t ret = {.variableType = DictionaryClass, .dictionary.vector = dict}; + return copyVariableToPtr(ret); + } + + FILINFO fno; + while (!(resPtr->integer.value = f_readdir(&dir, &fno)) && fno.fname[0]){ + char *name = CpyStr(fno.fname); + if (fno.fattrib & AM_DIR){ + vecAdd(&dirNamesArray.solvedArray.vector, name); + } + else { + vecAdd(&fileNamesArray.solvedArray.vector, name); + s64 size = fno.fsize; + vecAdd(&fileSizeArray.solvedArray.vector, size); + } + } + + f_closedir(&dir); + + temp.name = CpyStr("files"); + temp.var = copyVariableToPtr(fileNamesArray); + vecAdd(&dict, temp); + + temp.name = CpyStr("folders"); + temp.var = copyVariableToPtr(dirNamesArray); + vecAdd(&dict, temp); + + temp.name = CpyStr("fileSizes"); + temp.var = copyVariableToPtr(fileSizeArray); + vecAdd(&dict, temp); + + Variable_t ret = {.variableType = DictionaryClass, .dictionary.vector = dict}; + return copyVariableToPtr(ret); +} + +ClassFunction(stdFileCopy){ + ErrCode_t e = FileCopy(args[0]->string.value, args[1]->string.value, 0); + return newIntVariablePtr(e.err); +} + +ClassFunction(stdMkdir){ + return newIntVariablePtr(f_mkdir(args[0]->string.value)); +} + +// Broken???? +ClassFunction(stdGetMemUsage){ + heap_monitor_t mon; + heap_monitor(&mon, false); + Dict_t a = {.name = CpyStr("used"), .var = newIntVariablePtr((s64)mon.used)}; + Dict_t b = {.name = CpyStr("total"), .var = newIntVariablePtr((s64)mon.total)}; + Variable_t ret = {.variableType = DictionaryClass, .dictionary.vector = newVec(sizeof(Dict_t), 2)}; + vecAdd(&ret, a); + vecAdd(&ret, b); + return copyVariableToPtr(ret); +} + +ClassFunction(stdGetNcaType){ + int type = GetNcaType(args[0]->string.value); + return newIntVariablePtr(type); +} #else ClassFunction(stdMountSysmmc){ return newIntVariablePtr(0); @@ -132,12 +209,29 @@ ClassFunction(stdMountSave){ ClassFunction(stdSetPixel) { return newIntVariablePtr(0); } + +ClassFunction(stdReadDir){ + return newIntVariablePtr(0); +} + +ClassFunction(stdFileCopy){ + return newIntVariablePtr(0); +} + +ClassFunction(stdMkdir){ + return newIntVariablePtr(0); +} + +ClassFunction(stdGetMemUsage) { + return newIntVariablePtr(0); +} #endif u8 oneIntoneFunction[] = { IntClass, FunctionClass }; u8 doubleFunctionClass[] = { FunctionClass, FunctionClass }; u8 oneStringArgStd[] = {StringClass}; u8 threeIntsStd[] = { IntClass, IntClass, IntClass }; +u8 twoStringArgStd[] = {StringClass, StringClass}; ClassFunctionTableEntry_t standardFunctionDefenitions[] = { {"if", stdIf, 2, oneIntoneFunction}, @@ -150,6 +244,11 @@ ClassFunctionTableEntry_t standardFunctionDefenitions[] = { {"break", stdBreak, 0, 0}, {"dict", stdDict, 0, 0}, {"setpixel", stdSetPixel, 3, threeIntsStd}, + {"readdir", stdReadDir, 1, oneStringArgStd}, + {"filecopy", stdFileCopy, 2, twoStringArgStd}, + {"mkdir", stdMkdir, 1, oneStringArgStd}, + {"memory", stdGetMemUsage, 0, 0}, + {"ncatype", stdGetNcaType, 1, oneStringArgStd}, }; ClassFunctionTableEntry_t* searchStdLib(char* funcName) { diff --git a/source/tegraexplorer/mainmenu.c b/source/tegraexplorer/mainmenu.c index 16fccf5..2e5db63 100644 --- a/source/tegraexplorer/mainmenu.c +++ b/source/tegraexplorer/mainmenu.c @@ -22,7 +22,7 @@ #include #include "../fs/menus/filemenu.h" -//#define INCLUDE_BUILTIN_SCRIPTS 1 +#define INCLUDE_BUILTIN_SCRIPTS 1 #ifdef INCLUDE_BUILTIN_SCRIPTS #include "../script/builtin.h" @@ -121,11 +121,10 @@ void ViewCredits(){ if (hidRead()->r) gfx_printf("%k\"I'm not even sure if it works\" - meme", COLOR_ORANGE); -/* Leaving this here for my debugging needs :) heap_monitor_t a = {0}; heap_monitor(&a, false); gfx_printf("\nUsed: %d\nTotal: %d\n", a.used, a.total); -*/ + hidWait(); }