Various changes/cleanups, removed useless code, simplified chronometer functions

This commit is contained in:
Aurora 2016-08-15 03:51:48 +02:00
parent 75acdc8a98
commit aa7c2c0009
10 changed files with 46 additions and 82 deletions

View File

@ -311,11 +311,8 @@ void ctrNandInit(void)
if(isN3DS)
{
if(!isDevUnit)
{
u8 __attribute__((aligned(4))) keyY0x5[0x10] = {0x4D, 0x80, 0x4F, 0x4E, 0x99, 0x90, 0x19, 0x46, 0x13, 0xA2, 0x04, 0xAC, 0x58, 0x44, 0x60, 0xBE};
aes_setkey(0x05, keyY0x5, AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL);
}
u8 __attribute__((aligned(4))) keyY0x5[0x10] = {0x4D, 0x80, 0x4F, 0x4E, 0x99, 0x90, 0x19, 0x46, 0x13, 0xA2, 0x04, 0xAC, 0x58, 0x44, 0x60, 0xBE};
aes_setkey(0x05, keyY0x5, AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL);
nandSlot = 0x05;
fatStart = 0x5CAD7;
@ -361,11 +358,10 @@ void setRSAMod0DerivedKeys(void)
aes_setkey(0x25, keyX0x25, AES_KEYX, AES_INPUT_BE | AES_INPUT_NORMAL);
aes_setkey(0x2F, keyY0x2F, AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL);
/* [3dbrew] The first 0x10-bytes are checked by the v6.0/v7.0 NATIVE_FIRM keyinit function,
when non-zero it clears this block and continues to do the key generation.
Otherwise when this block was already all-zero, it immediately returns.
*/
Otherwise when this block was already all-zero, it immediately returns. */
memset32((void *)0x01FFCD00, 0, 0x10);
}
}
@ -408,7 +404,7 @@ void arm9Loader(u8 *arm9Section)
//Firm keys
u8 __attribute__((aligned(4))) keyY[0x10];
u8 __attribute__((aligned(4))) arm9BinCTR[0x10];
u8 __attribute__((aligned(4))) arm9BinCTR[0x10];
u8 arm9BinSlot = a9lVersion ? 0x16 : 0x15;
//Setup keys needed for arm9bin decryption
@ -424,7 +420,7 @@ void arm9Loader(u8 *arm9Section)
if(a9lVersion)
{
u8 __attribute__((aligned(4))) keyX[0x10];
if(!isDevUnit)
{
const u8 __attribute__((aligned(4))) key1[0x10] = {0x07, 0x29, 0x44, 0x38, 0xF8, 0xC9, 0x75, 0x93, 0xAA, 0x0E, 0x4A, 0xB4, 0xAE, 0x84, 0xC1, 0xD8};
@ -453,11 +449,10 @@ void arm9Loader(u8 *arm9Section)
//Set keys 0x19..0x1F keyXs
aes_use_keyslot(0x11);
for(u8 slot = 0x19; slot < 0x20; slot++)
for(u8 slot = 0x19; slot < 0x20; slot++, keyData[0xF]++)
{
aes(decKey, keyData, 1, NULL, AES_ECB_DECRYPT_MODE, 0);
aes_setkey(slot, decKey, AES_KEYX, AES_INPUT_BE | AES_INPUT_NORMAL);
keyData[0xF] += 1;
}
}
}

View File

@ -56,8 +56,7 @@ void main(void)
isA9lh;
u32 newConfig,
emuHeader,
nbChronoStarted = 0;
emuHeader;
FirmwareType firmType;
FirmwareSource nandType;
@ -135,12 +134,12 @@ void main(void)
if(needConfig != DONT_CONFIGURE)
{
//If no configuration file exists or SELECT is held, load configuration menu
bool shouldLoadConfigurationMenu = needConfig == CREATE_CONFIGURATION || ((pressed & BUTTON_SELECT) && !(pressed & BUTTON_L1));
bool pinExists = CONFIG(7) && readPin(&pin);
bool shouldLoadConfigurationMenu = needConfig == CREATE_CONFIGURATION || ((pressed & BUTTON_SELECT) && !(pressed & BUTTON_L1)),
pinExists = CONFIG(7) && readPin(&pin);
if(pinExists || shouldLoadConfigurationMenu)
{
bool needToDeinit = initScreens();
initScreens();
//If we get here we should check the PIN (if it exists) in all cases
if(pinExists) verifyPin(&pin, true);
@ -154,21 +153,11 @@ void main(void)
//Zero the last booted FIRM flag
CFG_BOOTENV = 0;
nbChronoStarted = 1;
chrono(0);
chrono(2);
//Update pressed buttons
pressed = HID_PAD;
}
if(needToDeinit)
{
//Turn off backlight
i2cWriteRegister(I2C_DEV_MCU, 0x22, 0x16);
deinitScreens();
PDN_GPU_CNT = 1;
}
}
if(isA9lh && !CFG_BOOTENV && pressed == SAFE_MODE)
@ -183,22 +172,19 @@ void main(void)
if(CONFIG(6) && loadSplash())
{
nbChronoStarted = 2;
chrono(0);
chrono(3);
nbChronoStarted = 0;
//Update pressed buttons
pressed = HID_PAD;
}
bool shouldLoadPayload = (pressed & SINGLE_PAYLOAD_BUTTONS) || ((pressed & BUTTON_L1) && (pressed & L_PAYLOAD_BUTTONS));
if(shouldLoadPayload)
loadPayload(pressed, nbChronoStarted != 2);
if(shouldLoadPayload) loadPayload(pressed);
if(!CONFIG(6) && loadSplash())
{
nbChronoStarted = 2;
chrono(0);
chrono(3);
}
//If R is pressed, boot the non-updated NAND with the FIRM of the opposite one
@ -266,12 +252,6 @@ void main(void)
break;
}
if(nbChronoStarted)
{
if(nbChronoStarted == 2) chrono(3);
stopChrono();
}
launchFirm(firmType, isFirmlaunch);
}

View File

@ -81,7 +81,7 @@ void createDirectory(const char *path)
f_mkdir(path);
}
void loadPayload(u32 pressed, bool needToInitScreens)
void loadPayload(u32 pressed)
{
const char *pattern;
@ -106,7 +106,7 @@ void loadPayload(u32 pressed, bool needToInitScreens)
if(result == FR_OK && info.fname[0])
{
if(needToInitScreens) initScreens();
initScreens();
u32 *const loaderAddress = (u32 *)0x24FFFF00;

View File

@ -33,5 +33,5 @@ u32 fileRead(void *dest, const char *path);
u32 getFileSize(const char *path);
bool fileWrite(const void *buffer, const char *path, u32 size);
void createDirectory(const char *path);
void loadPayload(u32 pressed, bool needToInitScreens);
void loadPayload(u32 pressed);
u32 firmRead(void *dest, u32 firmType);

View File

@ -185,7 +185,7 @@ u32 getLoader(u8 *pos, u32 *loaderSize)
u8 *off = pos;
u32 size;
while(1)
while(true)
{
size = *(u32 *)(off + 0x104) * 0x200;
if(*(u32 *)(off + 0x200) == 0x64616F6C) break;

View File

@ -39,9 +39,10 @@ bool readPin(PINData *out)
{
u8 __attribute__((aligned(4))) zeroes[16] = {0};
u8 __attribute__((aligned(4))) tmp[32] = {0};
if(fileRead(out, "/luma/pin.bin") != sizeof(PINData)) return false;
else if(memcmp(out->magic, "PINF", 4) != 0) return false;
if(memcmp(out->magic, "PINF", 4) != 0) return false;
computePINHash(tmp, zeroes, 1);
return memcmp(out->testHash, tmp, 32) == 0; //test vector verification (SD card has (or hasn't) been used on another console)
}
@ -62,16 +63,15 @@ PINData newPin(void)
drawString("Enter your NEW PIN: ", 10, 10, COLOR_WHITE);
u32 pressed = 0;
// Set the default value as 0x00 so we can check if there are any unentered characters.
u8 __attribute__((aligned(4))) enteredPassword[16 * ((PIN_LENGTH + 15) / 16)] = {0}; // pad to AES block length
u32 cnt = 0;
int charDrawPos = 20 * SPACING_X;
int cnt = 0;
while(true)
{
u32 pressed;
do
{
pressed = waitInput();
@ -89,9 +89,8 @@ PINData newPin(void)
charDrawPos += 2 * SPACING_X;
// we leave the rest of the array zeroed out.
if (cnt >= PIN_LENGTH)
if(cnt >= PIN_LENGTH)
{
PINData pin = {0};
u8 __attribute__((aligned(4))) tmp[32] = {0};
u8 __attribute__((aligned(4))) zeroes[16] = {0};
@ -119,16 +118,16 @@ void verifyPin(PINData *in, bool allowQuit)
drawString("Press START to shutdown or enter pin to proceed.", 10, 10, COLOR_WHITE);
drawString("Pin: ", 10, 10 + 2 * SPACING_Y, COLOR_WHITE);
u32 pressed = 0;
// Set the default characters as 0x00 so we can check if there are any unentered characters.
u8 __attribute__((aligned(4))) enteredPassword[16 * ((PIN_LENGTH + 15) / 16)] = {0};
u32 cnt = 0;
bool unlock;
int charDrawPos = 5 * SPACING_X, cnt = 0;
int charDrawPos = 5 * SPACING_X;
while(true)
{
u32 pressed;
do
{
pressed = waitInput();
@ -159,22 +158,18 @@ void verifyPin(PINData *in, bool allowQuit)
computePINHash(tmp, enteredPassword, (PIN_LENGTH + 15) / 16);
unlock = memcmp(in->hash, tmp, 32) == 0;
if (!unlock)
{
// re zero out all 16 just in case.
memset32(enteredPassword, 0, 16);
pressed = 0;
if(!unlock)
{
charDrawPos = 5 * SPACING_X;
cnt = 0;
clearScreens();
drawString("Press START to shutdown or enter pin to proceed.", 10, 10, COLOR_WHITE);
drawString("Pin: ", 10, 10 + 2 * SPACING_Y, COLOR_WHITE);
drawString("Wrong pin! Try again!", 10, 10 + 3 * SPACING_Y, COLOR_RED);
}
else return;
else break;
}
}
}

View File

@ -140,10 +140,8 @@ void clearScreens(void)
invokeArm11Function(ARM11);
}
bool initScreens(void)
void initScreens(void)
{
bool needToInit = PDN_GPU_CNT == 1;
void __attribute__((naked)) ARM11(void)
{
//Disable interrupts
@ -242,7 +240,7 @@ bool initScreens(void)
WAIT_FOR_ARM9();
}
if(needToInit)
if(PDN_GPU_CNT == 1)
{
flushDCacheRange(&config, 4);
flushDCacheRange((void *)fb, sizeof(struct fb));
@ -254,6 +252,4 @@ bool initScreens(void)
else updateBrightness(MULTICONFIG(0));
clearScreens();
return needToInit;
}

View File

@ -42,4 +42,4 @@ static volatile struct fb {
void deinitScreens(void);
void updateBrightness(u32 brightnessIndex);
void clearScreens(void);
bool initScreens(void);
void initScreens(void);

View File

@ -84,14 +84,19 @@ static inline void startChrono(u64 initialTicks)
for(u32 i = 1; i < 4; i++) REG_TIMER_CNT(i) = 0x84; //Count-up; enabled
}
static inline void stopChrono(void)
{
for(u32 i = 0; i < 4; i++) REG_TIMER_CNT(i) &= ~0x80;
}
void chrono(u32 seconds)
{
static u64 startingTicks = 0;
startChrono(0);
if(!startingTicks) startChrono(0);
u64 startingTicks = 0;
for(u32 i = 0; i < 4; i++) startingTicks |= REG_TIMER_VAL(i) << (16 * i);
u64 res;
do
{
res = 0;
@ -99,12 +104,7 @@ void chrono(u32 seconds)
}
while(res - startingTicks < seconds * TICKS_PER_SEC);
if(!seconds) startingTicks = res;
}
void stopChrono(void)
{
for(u32 i = 0; i < 4; i++) REG_TIMER_CNT(i) &= ~0x80;
stopChrono();
}
void error(const char *message)

View File

@ -31,7 +31,5 @@
u32 waitInput(void);
void mcuReboot(void);
void mcuPowerOff(void);
void chrono(u32 seconds);
void stopChrono(void);
void error(const char *message);