From 12136d9289c6643cdc331a6c1e2822d53d3b4afe Mon Sep 17 00:00:00 2001 From: suchmememanyskill Date: Mon, 4 Jan 2021 16:57:47 +0100 Subject: [PATCH] [Script] add last bits Fixed: Scripts should free at the end Added: ncaGetType, saveSign Modified: pathCombine can now take 2+ args to combine --- source/fs/menus/filemenu.c | 10 +++++-- source/keys/save.c | 51 +++++++++++++++++++++++++++++++++ source/keys/save.h | 4 +++ source/script/functions.c | 45 +++++++++++++++++++++++++++-- source/script/parser.c | 24 ++++++++++++---- source/script/variables.c | 2 +- source/storage/gptmenu.c | 10 ++++--- source/storage/mountmanager.c | 1 + source/tegraexplorer/mainmenu.c | 3 +- 9 files changed, 132 insertions(+), 18 deletions(-) create mode 100644 source/keys/save.c create mode 100644 source/keys/save.h diff --git a/source/fs/menus/filemenu.c b/source/fs/menus/filemenu.c index 309f9d9..130ead5 100644 --- a/source/fs/menus/filemenu.c +++ b/source/fs/menus/filemenu.c @@ -78,6 +78,9 @@ void RunScript(char *path, FSEntry_t entry){ if (!script) return; + if (((entry.size >= 64 && entry.sizeDef == 1) || entry.sizeDef >= 2) && !TConf.minervaEnabled) + return; + gfx_clearscreen(); scriptCtx_t ctx = createScriptCtx(); ctx.script = runLexar(script, size); @@ -88,10 +91,11 @@ void RunScript(char *path, FSEntry_t entry){ printError(mainLoop(&ctx)); - freeVariableVector(&ctx.varDict); + freeDictVector(&ctx.varDict); lexarVectorClear(&ctx.script); - gfx_printf("\nend of script"); - hidWait(); + + gfx_printf("\nScript done!\nPress any key"); + hidWait(); } void RenameFile(char *path, FSEntry_t entry){ diff --git a/source/keys/save.c b/source/keys/save.c new file mode 100644 index 0000000..dc359f3 --- /dev/null +++ b/source/keys/save.c @@ -0,0 +1,51 @@ +#include "save.h" +#include +#include +#include +#include +#include "../err.h" + +ErrCode_t saveCommit(const char *path){ + FIL file; + int res; + u8 *buf, hash[0x20], *cmac_data, cmac[0x10]; + const u32 hashed_data_size = 0x3D00, cmac_data_size = 0x200; + + buf = malloc(hashed_data_size); + cmac_data = malloc(cmac_data_size); + + if ((res = f_open(&file, path, FA_OPEN_EXISTING | FA_READ | FA_WRITE))){ + goto out_free; + } + + f_lseek(&file, 0x300); + if ((res = f_read(&file, buf, hashed_data_size, NULL))){ + goto out_free; + } + + se_calc_sha256_oneshot(hash, buf, hashed_data_size); + + f_lseek(&file, 0x108); + if ((res = f_write(&file, hash, sizeof(hash), NULL))){ + goto out_free; + } + + f_lseek(&file, 0x100); + if ((res = f_read(&file, cmac_data, cmac_data_size, NULL))){ + goto out_free; + } + + se_aes_cmac(8, cmac, 0x10, cmac_data, cmac_data_size); + + f_lseek(&file, 0); + + if ((res = f_write(&file, cmac, sizeof(cmac), NULL))){ + goto out_free; + } + +out_free:; + free(buf); + free(cmac_data); + f_close(&file); + return newErrCode(res); +} \ No newline at end of file diff --git a/source/keys/save.h b/source/keys/save.h new file mode 100644 index 0000000..fd93e1b --- /dev/null +++ b/source/keys/save.h @@ -0,0 +1,4 @@ +#pragma once +#include "../err.h" + +ErrCode_t saveCommit(const char *path); \ No newline at end of file diff --git a/source/script/functions.c b/source/script/functions.c index 84c48d8..d84d3cb 100644 --- a/source/script/functions.c +++ b/source/script/functions.c @@ -16,11 +16,16 @@ #include "../storage/emummc.h" #include "../fs/readers/folderReader.h" #include "../utils/utils.h" +#include "../keys/keys.h" #include "../storage/emmcfile.h" +#include "../keys/nca.h" +#include "../keys/save.h" +#include "../tegraexplorer/tconf.h" #define scriptFunction(name) Variable_t name(scriptCtx_t *ctx, Variable_t *vars, u32 varLen) #define varInt(i) newVar(IntType, 0, i) +#define varStr(s) newVar(StringType, 1, .stringType = s) scriptFunction(funcIf) { setCurIndentInstruction(ctx, (vars[0].integerType == 0), 0, -1); @@ -267,7 +272,23 @@ scriptFunction(funcMakeMenu){ // Args: Str, Str scriptFunction(funcCombinePath){ - return newVar(StringType, 1, .stringType = CombinePaths(vars[0].stringType, vars[1].stringType)); + if (varLen <= 1) + return NullVar; + + for (int i = 0; i < varLen; i++){ + if (vars[i].varType != StringType) + return ErrVar(ERRINVALIDTYPE); + } + + char *res = CpyStr(vars[0].stringType); + + for (int i = 1; i < varLen; i++){ + char *temp = CombinePaths(res, vars[i].stringType); + free(res); + res = temp; + } + + return varStr(res); } // Args: Str @@ -300,6 +321,9 @@ scriptFunction(funcMmcConnect){ // Args: Str scriptFunction(funcMmcMount){ + if (!TConf.keysDumped) + return ErrVar(ERRFATALFUNCFAIL); + return varInt((mountMMCPart(vars[0].stringType).err)); } @@ -354,6 +378,20 @@ scriptFunction(funcMmcRestore){ return varInt((DumpOrWriteEmmcPart(vars[0].stringType, vars[1].stringType, 1, vars[2].integerType).err)); } +scriptFunction(funcGetNcaType){ + if (!TConf.keysDumped) + return ErrVar(ERRFATALFUNCFAIL); + + return varInt((GetNcaType(vars[0].stringType))); +} + +scriptFunction(funcSignSave){ + if (!TConf.keysDumped) + return ErrVar(ERRFATALFUNCFAIL); + + return varInt((saveCommit(vars[0].stringType).err)); +} + u8 fiveInts[] = {IntType, IntType, IntType, IntType, IntType}; u8 singleIntArray[] = { IntArrayType }; u8 singleInt[] = { IntType }; @@ -390,7 +428,7 @@ functionStruct_t scriptFunctions[] = { {"version", funcGetVer, 0, NULL}, {"menu", funcMakeMenu, 2, MenuArgs}, // for the optional arg {"menu", funcMakeMenu, 3, MenuArgs}, - {"pathCombine", funcCombinePath, 2, twoStrings}, + {"pathCombine", funcCombinePath, varArgs, NULL}, {"pathEscFolder", funcEscFolder, 1, singleStr}, {"fileMove", funcFileMove, 2, twoStrings}, {"fileCopy", funcFileCopy, 2, twoStrings}, @@ -403,8 +441,9 @@ functionStruct_t scriptFunctions[] = { {"dirDel", funcDelDir, 1, singleStr}, {"mmcDump", funcMmcDump, 2, mmcReadWrite}, {"mmcRestore", funcMmcRestore, 3, mmcReadWrite}, + {"ncaGetType", funcGetNcaType, 1, singleStr}, + {"saveSign", funcSignSave, 1, singleStr}, // Left from old: keyboard(?) - // Should implement still: saveSign, getNcaType }; Variable_t executeFunction(scriptCtx_t* ctx, char* func_name, lexarToken_t *start, u32 len) { diff --git a/source/script/parser.c b/source/script/parser.c index d1e14d3..d52c72c 100644 --- a/source/script/parser.c +++ b/source/script/parser.c @@ -78,13 +78,15 @@ scriptResult_t mainLoop(scriptCtx_t* ctx) { void printToken(lexarToken_t* token) { switch (token->token) { case Variable: - case VariableAssignment: - case Function: - case FunctionAssignment: case ArrayVariable: - case ArrayVariableAssignment: + case Function: gfx_printf("%s", token->text); break; + case FunctionAssignment: + case VariableAssignment: + case ArrayVariableAssignment: + gfx_printf("%s=", token->text); + break; case StrLit: //printf("%d: '%s'\n", vec.tokens[i].token, vec.tokens[i].text); gfx_printf("\"%s\"", token->text); @@ -100,12 +102,24 @@ void printToken(lexarToken_t* token) { } } +char *ErrorText[] = { + "Bad operator", + "Double not", + "Syntax err", + "Invalid type", + "No var", + "No func", + "Inactive indent", + "Div by 0", + "Func fail" +}; + void printError(scriptResult_t res) { if (res.resCode) { if (res.resCode == ERRESCSCRIPT) return; - gfx_printf("Error %d found!\nNear: ", res.resCode); + gfx_printf("Error found! %s\nNear: ", ErrorText[res.resCode - 1]); for (int i = 0; i < res.len; i++) { printToken(&res.nearToken[i]); } diff --git a/source/script/variables.c b/source/script/variables.c index 88f0f45..ed3c2a5 100644 --- a/source/script/variables.c +++ b/source/script/variables.c @@ -15,7 +15,7 @@ void freeVariable(Variable_t dv) { case StringArrayType:; char** strArray = vecGetArray(char**, dv.vectorType); for (u32 i = 0; i < dv.vectorType.count; i++){ - FREE(strArray[i]); + free(strArray[i]); } case IntArrayType: diff --git a/source/storage/gptmenu.c b/source/storage/gptmenu.c index a91db9a..0451cb5 100644 --- a/source/storage/gptmenu.c +++ b/source/storage/gptmenu.c @@ -118,11 +118,13 @@ void GptMenu(u8 MMCType){ DrawError(err); } else { - if (TConf.curExplorerLoc > LOC_SD) - ResetCopyParams(); + if (TConf.keysDumped){ + if (TConf.curExplorerLoc > LOC_SD) + ResetCopyParams(); - TConf.curExplorerLoc = LOC_EMMC; - FileExplorer("bis:/"); + TConf.curExplorerLoc = LOC_EMMC; + FileExplorer("bis:/"); + } } } else { diff --git a/source/storage/mountmanager.c b/source/storage/mountmanager.c index 2876abb..5ee13e6 100644 --- a/source/storage/mountmanager.c +++ b/source/storage/mountmanager.c @@ -22,6 +22,7 @@ void SetKeySlots(){ // Not for bis but whatever se_aes_key_set(6, dumpedKeys.header_key + 0x00, 0x10); se_aes_key_set(7, dumpedKeys.header_key + 0x10, 0x10); + se_aes_key_set(8, dumpedKeys.save_mac_key, 0x10); } } diff --git a/source/tegraexplorer/mainmenu.c b/source/tegraexplorer/mainmenu.c index 7d74d8b..20b4c2c 100644 --- a/source/tegraexplorer/mainmenu.c +++ b/source/tegraexplorer/mainmenu.c @@ -141,8 +141,7 @@ void EnterMainMenu(){ // -- Explore -- mainMenuEntries[MainBrowseSd].hide = !sd_mounted; mainMenuEntries[MainMountSd].name = (sd_mounted) ? "Unmount SD" : "Mount SD"; - mainMenuEntries[MainBrowseEmmc].hide = !TConf.keysDumped; - mainMenuEntries[MainBrowseEmummc].hide = (!TConf.keysDumped || !emu_cfg.enabled || !sd_mounted); + mainMenuEntries[MainBrowseEmummc].hide = (!emu_cfg.enabled || !sd_mounted); // -- Tools -- mainMenuEntries[MainPartitionSd].hide = (!is_sd_inited || sd_get_card_removed());