diff --git a/source/buttons.h b/source/buttons.h index 3d034c1..cd5c871 100644 --- a/source/buttons.h +++ b/source/buttons.h @@ -44,4 +44,4 @@ #define SINGLE_PAYLOAD_BUTTONS (DPAD_BUTTONS | BUTTON_B | BUTTON_X | BUTTON_Y) #define L_PAYLOAD_BUTTONS (BUTTON_R1 | BUTTON_A | BUTTON_START | BUTTON_SELECT) #define MENU_BUTTONS (DPAD_BUTTONS | BUTTON_A | BUTTON_START) -#define PIN_BUTTONS (BUTTON_A | BUTTON_B | BUTTON_X | BUTTON_Y | DPAD_BUTTONS | BUTTON_START) \ No newline at end of file +#define PIN_BUTTONS (BUTTON_A | BUTTON_B | BUTTON_X | BUTTON_Y | DPAD_BUTTONS | BUTTON_START | BUTTON_SELECT) \ No newline at end of file diff --git a/source/config.c b/source/config.c index f6e5097..391216f 100644 --- a/source/config.c +++ b/source/config.c @@ -397,6 +397,5 @@ void configMenu(bool isSdMode, bool oldPinStatus, u32 oldPinMode) else if(oldPinStatus) fileDelete(PIN_FILE); while(HID_PAD & PIN_BUTTONS); - startChrono(); - while(chrono(false) < 2); + wait(false, 2ULL); } \ No newline at end of file diff --git a/source/draw.c b/source/draw.c index edcf582..4852a74 100644 --- a/source/draw.c +++ b/source/draw.c @@ -55,9 +55,7 @@ bool loadSplash(void) else { swapFramebuffers(true); - - startChrono(); - while(chrono(false) < 3); + wait(false, 3ULL); ret = true; } diff --git a/source/fs.c b/source/fs.c index 60d91cb..f47962f 100644 --- a/source/fs.c +++ b/source/fs.c @@ -190,7 +190,7 @@ void loadPayload(u32 pressed, const char *payloadPath) void payloadMenu(void) { DIR dir; - char path[_MAX_LFN + 10] = "payloads"; + char path[62] = "payloads"; if(f_opendir(&dir, path) == FR_OK) { @@ -272,8 +272,7 @@ void payloadMenu(void) } while(HID_PAD & MENU_BUTTONS); - startChrono(); - while(chrono(false) < 2); + wait(false, 2ULL); } } } diff --git a/source/i2c.c b/source/i2c.c index 286bbf1..8b7c250 100644 --- a/source/i2c.c +++ b/source/i2c.c @@ -25,7 +25,6 @@ */ #include "i2c.h" -#include "utils.h" //----------------------------------------------------------------------------- @@ -155,16 +154,7 @@ bool i2cWriteRegister(u8 dev_id, u8 reg, u8 data) *i2cGetCntReg(bus_id) = 0xC1; i2cStop(bus_id, 0); - if(i2cGetResult(bus_id)) - { - if(dev_id == I2C_DEV_MCU) - { - startChrono(); - while(chrono(true) < 3); - } - - return true; - } + if(i2cGetResult(bus_id)) return true; } *i2cGetCntReg(bus_id) = 0xC5; i2cWaitBusy(bus_id); diff --git a/source/main.c b/source/main.c index 4280361..cd83d34 100644 --- a/source/main.c +++ b/source/main.c @@ -148,8 +148,7 @@ void main(void) if(pinExists && !shouldLoadConfigMenu) { while(HID_PAD & PIN_BUTTONS); - startChrono(); - while(chrono(false) < 2); + wait(false, 2ULL); } } else diff --git a/source/pin.c b/source/pin.c index 2c380e5..185230e 100644 --- a/source/pin.c +++ b/source/pin.c @@ -50,19 +50,29 @@ void newPin(bool allowSkipping, u32 pinMode) u8 length = 4 + 2 * (pinMode - 1); - const char *title = allowSkipping ? "Press START to skip or enter a new PIN" : "Enter a new PIN to proceed"; - drawString(title, true, 10, 10, COLOR_TITLE); - drawString("PIN ( digits): ", true, 10, 10 + 2 * SPACING_Y, COLOR_WHITE); - drawCharacter('0' + length, true, 10 + 5 * SPACING_X, 10 + 2 * SPACING_Y, COLOR_WHITE); + drawString("Enter a new PIN using ABXY and the DPad", true, 10, 10, COLOR_TITLE); + drawString(allowSkipping ? "Press START to skip, SELECT to reset" : "Press SELECT to reset", true, 10, 10 + SPACING_Y, COLOR_TITLE); + + drawString("PIN ( digits): ", true, 10, 10 + 3 * SPACING_Y, COLOR_WHITE); + drawCharacter('0' + length, true, 10 + 5 * SPACING_X, 10 + 3 * SPACING_Y, COLOR_WHITE); //Pad to AES block length with zeroes __attribute__((aligned(4))) u8 enteredPassword[AES_BLOCK_SIZE] = {0}; + bool reset = false; u8 cnt = 0; - u32 charDrawPos = 16 * SPACING_X; while(cnt < length) { + if(reset) + { + for(u32 i = 0; i < cnt; i++) + drawCharacter((char)enteredPassword[i], true, 10 + (16 + 2 * i) * SPACING_X, 10 + 3 * SPACING_Y, COLOR_BLACK); + + cnt = 0; + reset = false; + } + u32 pressed; do { @@ -74,14 +84,17 @@ void newPin(bool allowSkipping, u32 pinMode) if(!allowSkipping) pressed &= ~BUTTON_START; if(pressed & BUTTON_START) return; - if(!pressed) continue; - char key = pinKeyToLetter(pressed); - enteredPassword[cnt++] = (u8)key; //Add character to password + if(pressed & BUTTON_SELECT) reset = true; + else if(pressed != 0) + { + enteredPassword[cnt] = (u8)pinKeyToLetter(pressed); //Add character to password - //Visualize character on screen - drawCharacter(key, true, 10 + charDrawPos, 10 + 2 * SPACING_Y, COLOR_WHITE); - charDrawPos += 2 * SPACING_X; + //Visualize character on screen + drawCharacter(enteredPassword[cnt], true, 10 + (16 + 2 * cnt) * SPACING_X, 10 + 3 * SPACING_Y, COLOR_WHITE); + + cnt++; + } } PinData pin; @@ -125,12 +138,11 @@ bool verifyPin(u32 pinMode) initScreens(); - //Pad to AES block length with zeroes - __attribute__((aligned(4))) u8 enteredPassword[AES_BLOCK_SIZE] = {0}; + drawString("Enter the PIN using ABXY and the DPad to proceed", true, 10, 10, COLOR_TITLE); + drawString("Press START to shutdown, SELECT to clear", true, 10, 10 + SPACING_Y, COLOR_TITLE); - bool unlock = false; - u8 cnt = 0; - u32 charDrawPos = 16 * SPACING_X; + drawString("PIN ( digits): ", true, 10, 10 + 3 * SPACING_Y, COLOR_WHITE); + drawCharacter('0' + lengthBlock[0], true, 10 + 5 * SPACING_X, 10 + 3 * SPACING_Y, COLOR_WHITE); const char *messageFile = "pinmessage.txt"; @@ -147,11 +159,23 @@ bool verifyPin(u32 pinMode) } } + //Pad to AES block length with zeroes + __attribute__((aligned(4))) u8 enteredPassword[AES_BLOCK_SIZE] = {0}; + + bool unlock = false, + reset = false; + u8 cnt = 0; + while(!unlock) { - drawString("Press START to shutdown or enter PIN to proceed", true, 10, 10, COLOR_TITLE); - drawString("PIN ( digits): ", true, 10, 10 + 2 * SPACING_Y, COLOR_WHITE); - drawCharacter('0' + lengthBlock[0], true, 10 + 5 * SPACING_X, 10 + 2 * SPACING_Y, COLOR_WHITE); + if(reset) + { + for(u32 i = 0; i < cnt; i++) + drawCharacter((char)enteredPassword[i], true, 10 + (16 + 2 * i) * SPACING_X, 10 + 3 * SPACING_Y, COLOR_BLACK); + + cnt = 0; + reset = false; + } u32 pressed; do @@ -164,28 +188,25 @@ bool verifyPin(u32 pinMode) pressed &= PIN_BUTTONS; - if(!pressed) continue; - - char key = pinKeyToLetter(pressed); - enteredPassword[cnt++] = (u8)key; //Add character to password - - //Visualize character on screen - drawCharacter(key, true, 10 + charDrawPos, 10 + 2 * SPACING_Y, COLOR_WHITE); - charDrawPos += 2 * SPACING_X; - - if(cnt >= lengthBlock[0]) + if(pressed & BUTTON_SELECT) reset = true; + else if(pressed != 0) { - computePinHash(tmp, enteredPassword); - unlock = memcmp(pin.hash, tmp, sizeof(tmp)) == 0; + enteredPassword[cnt] = (u8)pinKeyToLetter(pressed); //Add character to password - if(!unlock) + //Visualize character on screen + drawCharacter((char)enteredPassword[cnt], true, 10 + (16 + 2 * cnt) * SPACING_X, 10 + 3 * SPACING_Y, COLOR_WHITE); + + if(++cnt >= lengthBlock[0]) { - charDrawPos = 16 * SPACING_X; - cnt = 0; + computePinHash(tmp, enteredPassword); + unlock = memcmp(pin.hash, tmp, sizeof(tmp)) == 0; - clearScreens(true, false, false); + if(!unlock) + { + reset = true; - drawString("Wrong PIN, try again", true, 10, 10 + 4 * SPACING_Y, COLOR_RED); + drawString("Wrong PIN, try again", true, 10, 10 + 5 * SPACING_Y, COLOR_RED); + } } } } diff --git a/source/screen.c b/source/screen.c index d95a1ba..58d0c4a 100644 --- a/source/screen.c +++ b/source/screen.c @@ -40,6 +40,7 @@ #include "memory.h" #include "cache.h" #include "i2c.h" +#include "utils.h" vu32 *arm11Entry = (vu32 *)BRAHMA_ARM11_ENTRY; static const u32 brightness[4] = {0x5F, 0x4C, 0x39, 0x26}; @@ -295,6 +296,7 @@ void initScreens(void) //Turn on backlight i2cWriteRegister(I2C_DEV_MCU, 0x22, 0x2A); + wait(true, 3ULL); } else updateBrightness(MULTICONFIG(BRIGHTNESS)); diff --git a/source/utils.c b/source/utils.c index 60af492..c82b3d5 100644 --- a/source/utils.c +++ b/source/utils.c @@ -31,16 +31,38 @@ #include "draw.h" #include "cache.h" +static void startChrono(void) +{ + REG_TIMER_CNT(0) = 0; //67MHz + for(u32 i = 1; i < 4; i++) REG_TIMER_CNT(i) = 4; //Count-up + + for(u32 i = 0; i < 4; i++) REG_TIMER_VAL(i) = 0; + + REG_TIMER_CNT(0) = 0x80; //67MHz; enabled + for(u32 i = 1; i < 4; i++) REG_TIMER_CNT(i) = 0x84; //Count-up; enabled +} + +static u64 chrono(bool isMilliseconds) +{ + u64 res; + for(u32 i = 0; i < 4; i++) res |= REG_TIMER_VAL(i) << (16 * i); + + if(isMilliseconds) res /= (TICKS_PER_SEC / 1000); + else res /= TICKS_PER_SEC; + + return res; +} + u32 waitInput(bool isMenu) { - static u64 dPadDelay = 0; + static u64 dPadDelay = 0ULL; bool pressedKey = false; u32 key, oldKey = HID_PAD; if(isMenu) { - dPadDelay = dPadDelay > 0 ? 87 : 143; + dPadDelay = dPadDelay > 0ULL ? 87ULL : 143ULL; startChrono(); } @@ -77,26 +99,10 @@ void mcuPowerOff(void) while(true); } -void startChrono(void) +void wait(bool isMilliseconds, u64 amount) { - REG_TIMER_CNT(0) = 0; //67MHz - for(u32 i = 1; i < 4; i++) REG_TIMER_CNT(i) = 4; //Count-up - - for(u32 i = 0; i < 4; i++) REG_TIMER_VAL(i) = 0; - - REG_TIMER_CNT(0) = 0x80; //67MHz; enabled - for(u32 i = 1; i < 4; i++) REG_TIMER_CNT(i) = 0x84; //Count-up; enabled -} - -u64 chrono(bool isMilliseconds) -{ - u64 res; - for(u32 i = 0; i < 4; i++) res |= REG_TIMER_VAL(i) << (16 * i); - - if(isMilliseconds) res /= (TICKS_PER_SEC / 1000); - else res /= TICKS_PER_SEC; - - return res; + startChrono(); + while(chrono(isMilliseconds) < amount); } void error(const char *message) diff --git a/source/utils.h b/source/utils.h index 1504ffe..ea9c455 100644 --- a/source/utils.h +++ b/source/utils.h @@ -34,6 +34,5 @@ u32 waitInput(bool isMenu); void mcuPowerOff(void); -void startChrono(void); -u64 chrono(bool isMilliseconds); +void wait(bool isMilliseconds, u64 amount); void error(const char *message); \ No newline at end of file