diff --git a/include/arm9/config.h b/include/arm9/config.h new file mode 100644 index 0000000..d3f9548 --- /dev/null +++ b/include/arm9/config.h @@ -0,0 +1,19 @@ +#pragma once + +enum Keys { + KBootOption1 = 0, + KBootOption2, + KBootOption3, + KBootOption1Buttons, + KBootOption2Buttons, + KBootOption3Buttons, + KBootMode, + // ... + KLast +}; + +#define numKeys KLast + +void *configCopyText(int key); +const void *configGetData(int key); +const char *configGetKeyText(int key); diff --git a/include/arm9/menu.h b/include/arm9/menu.h index 55bdec2..099fa15 100644 --- a/include/arm9/menu.h +++ b/include/arm9/menu.h @@ -7,7 +7,8 @@ STATE_NAND_RESTORE, STATE_OPTIONS_MENU, STATE_FIRM_LAUNCH, - STATE_TEST_MENU + STATE_TEST_MENU, + STATE_TEST_CONFIG }; typedef struct { @@ -21,12 +22,13 @@ } menu_state_options; const menu_state_options menu_main = { - 4, + 5, { {"Launch FIRM", STATE_FIRM_LAUNCH}, {"NAND tools...", STATE_NAND_MENU}, {"Options...", STATE_OPTIONS_MENU}, - {"DEBUG TEST", STATE_TEST_MENU} + {"DEBUG TEST", STATE_TEST_MENU}, + {"CONFIG TEST", STATE_TEST_CONFIG} } }; diff --git a/include/util.h b/include/util.h index a5061c2..e2c178b 100644 --- a/include/util.h +++ b/include/util.h @@ -12,7 +12,7 @@ // case insensitive string compare function -int stricmp(const char *str1, const char *str2); +int strnicmp(const char *str1, const char *str2, u32 len); // custom safe strncpy, string is always 0-terminated for buflen > 0 void strncpy_s(char *dest, const char *src, u32 nchars, const u32 buflen); diff --git a/source/arm9/config_parser.c b/source/arm9/config_parser.c index a9eabfc..8e22c76 100644 --- a/source/arm9/config_parser.c +++ b/source/arm9/config_parser.c @@ -13,6 +13,7 @@ #include "arm9/interrupt.h" #include "util.h" #include "hid.h" +#include "arm9/config.h" #define MAX_FILE_SIZE 0x4000 - 1 @@ -22,7 +23,7 @@ typedef struct { char *textData; // ptr to the first char of an attribute's data inside filebuf - u32 textLength; + u32 textLength; // length of the above data, without '\0' ofc void *data; // used to store the final native data } AttributeEntryType; @@ -39,18 +40,6 @@ static bool parseBootOptionPad(AttributeEntryType *attr); static bool parseBootMode(AttributeEntryType *attr); -enum Keys { - KBootOption1 = 0, - KBootOption2, - KBootOption3, - KBootOption1Buttons, - KBootOption2Buttons, - KBootOption3Buttons, - KBootMode -}; - -#define numKeys 7 - static const char *keyStrings[] = { "BOOT_OPTION1", "BOOT_OPTION2", @@ -75,7 +64,7 @@ static char *filebuf = NULL; -/* */ +/* This loads the config file from SD card and parses it */ bool loadConfigFile() { FILINFO fileStat; @@ -138,13 +127,19 @@ if(start) { - // verify definition char + /* verify definition char */ start = start + strlen(attrName); + + while(*start == ' ') start++; + c = *start; if(c != '=') start = NULL; else + { start ++; + while(*start == ' ') start++; + } } return start; @@ -206,7 +201,9 @@ } else { // TODO: what to do if ret == false ? + //printf("Parsing attr text with size %i\n%s\n", curAttr->textLength, curAttr->textData); ret = keyFunctions[i].parse(curAttr); + //if(ret) printf("success!\n"); } } @@ -248,11 +245,19 @@ { attr->data = NULL; - if(!isValidPath(attr->textData)) + char *buf = (char *) malloc(attr->textLength + 1); + if(!buf) return false; + + memcpy(buf, attr->textData, attr->textLength); + buf[attr->textLength] = '\0'; + + if(!isValidPath(buf)) + { + free(buf); return false; - - attr->data = strdup(attr->textData); - if(!attr->data) return false; + } + + attr->data = buf; return true; } @@ -262,21 +267,26 @@ char c; char *textData = attr->textData; u32 padValue = 0; - u32 i = 0; + u32 i; static const char * convTable[] = { "A", "B", "SELECT", "START", "RIGHT", "LEFT", "UP", "DOWN", "R", "L", "X", "Y" }; - for(; (c = *textData) != '\0'; textData ++) + for(; (c = *textData) != '\0' && textData < attr->textData + attr->textLength;) { - if(strnicmp(textData, convTable[i], strlen(convTable[i])) == 0) + for(i=0; idata = (u32 *) malloc(sizeof(u32)); @@ -293,3 +303,39 @@ attr->data = NULL; return true; } + +void *configCopyText(int key) +{ + if(key < 0 || key >= KLast) + return NULL; + + if(!attributes[key].textData) + return NULL; + + char *buf = (char *) malloc(attributes[key].textLength + 1); + if(!buf) + return NULL; + + memcpy(buf, attributes[key].textData, attributes[key].textLength); + buf[attributes[key].textLength] = '\0'; + + return buf; +} + +const void *configGetData(int key) +{ + if(key < 0 || key >= KLast) + return NULL; + + return attributes[key].data; +} + +const char *configGetKeyText(int key) +{ + if(key < 0 || key >= KLast) + return NULL; + + return keyStrings[key]; +} + + diff --git a/source/arm9/main.c b/source/arm9/main.c index 06529b0..f4bb94e 100644 --- a/source/arm9/main.c +++ b/source/arm9/main.c @@ -8,12 +8,14 @@ #include "arm9/fatfs/ff.h" #include "arm9/dev.h" #include "arm9/firm.h" +#include "arm9/config.h" static void devs_init(); static void mount_fs(); static void unit_detect(); static void boot_env_detect(); static void fw_detect(); +static void loadSettings(); int main(void) { @@ -47,6 +49,12 @@ printf("Detecting firmware...\n"); fw_detect(); + + printf("Loading settings...\n"); + + loadSettings(); + + wait(0x8000000); consoleClear(); @@ -110,7 +118,6 @@ res = f_mount(&nand_fs, "nand:", 1); if(res == FR_OK) printf("%s\e[0m\n", res_str[1]); else printf("%s ERROR 0x%d\e[0m\n", res_str[0], res); - wait(0x8000000); } void unmount_fs() @@ -163,6 +170,14 @@ free(nand_sector); } +static void loadSettings() +{ + const char *res_str[2] = {"\x1B[31mFailed!", "\x1B[32mOK!"}; + + bool success = loadConfigFile(); + printf(res_str[success]); +} + u8 rng_get_byte() { u32 tmp = REG_PRNG[0]; diff --git a/source/arm9/menu.c b/source/arm9/menu.c index 82ecdd5..a3b07d7 100644 --- a/source/arm9/menu.c +++ b/source/arm9/menu.c @@ -1,4 +1,5 @@ #include +#include #include #include "types.h" #include "mem_map.h" @@ -14,6 +15,7 @@ #include "arm9/menu.h" #include "arm9/timer.h" #include "arm9/interrupt.h" +#include "arm9/config.h" u8 color; enum menu_state_type menu_state; @@ -171,6 +173,37 @@ menu_state = STATE_MAIN; break; + case STATE_TEST_CONFIG: + consoleClear(); + consoleSelect(&con_top); + consoleClear(); + printf("Key Text Data:\n"); + for(int i=0; i"); + } + printf("\n\nKey Raw Data:\n"); + for(int i=0; i"); + } + for(;;); + break; + default: printf("OOPS!\n"); break; }