2016-06-07 13:53:24 +02:00
|
|
|
/*
|
|
|
|
* installer.c
|
|
|
|
*/
|
|
|
|
|
2016-03-19 17:30:56 +01:00
|
|
|
#include "memory.h"
|
|
|
|
#include "fs.h"
|
|
|
|
#include "crypto.h"
|
2016-09-26 23:15:26 +02:00
|
|
|
#include "screen.h"
|
2016-03-19 17:30:56 +01:00
|
|
|
#include "draw.h"
|
|
|
|
#include "utils.h"
|
2016-09-28 01:57:18 +02:00
|
|
|
#include "types.h"
|
2016-10-16 20:40:56 +02:00
|
|
|
#include "installer.h"
|
2016-03-19 17:30:56 +01:00
|
|
|
#include "fatfs/sdmmc/sdmmc.h"
|
|
|
|
|
2016-11-11 17:44:44 +01:00
|
|
|
static const u8 sectorHashRetail[SHA_256_HASH_SIZE] = {
|
2016-03-19 17:30:56 +01:00
|
|
|
0x82, 0xF2, 0x73, 0x0D, 0x2C, 0x2D, 0xA3, 0xF3, 0x01, 0x65, 0xF9, 0x87, 0xFD, 0xCC, 0xAC, 0x5C,
|
|
|
|
0xBA, 0xB2, 0x4B, 0x4E, 0x5F, 0x65, 0xC9, 0x81, 0xCD, 0x7B, 0xE6, 0xF4, 0x38, 0xE6, 0xD9, 0xD3
|
2016-10-02 17:32:18 +02:00
|
|
|
},
|
2016-11-11 17:44:44 +01:00
|
|
|
firm0HashRetail[SHA_256_HASH_SIZE] = {
|
2016-06-07 13:53:24 +02:00
|
|
|
0x6E, 0x4D, 0x14, 0xAD, 0x51, 0x50, 0xA5, 0x9A, 0x87, 0x59, 0x62, 0xB7, 0x09, 0x0A, 0x3C, 0x74,
|
|
|
|
0x4F, 0x72, 0x4B, 0xBD, 0x97, 0x39, 0x33, 0xF2, 0x11, 0xC9, 0x35, 0x22, 0xC8, 0xBB, 0x1C, 0x7D
|
2016-10-02 17:32:18 +02:00
|
|
|
},
|
2016-11-11 17:44:44 +01:00
|
|
|
firm0A9lhHashRetail[SHA_256_HASH_SIZE] = {
|
2016-06-07 14:35:52 +02:00
|
|
|
0x79, 0x3D, 0x35, 0x7B, 0x8F, 0xF1, 0xFC, 0xF0, 0x8F, 0xB6, 0xDB, 0x51, 0x31, 0xD4, 0xA7, 0x74,
|
|
|
|
0x8E, 0xF0, 0x4A, 0xB1, 0xA6, 0x7F, 0xCD, 0xAB, 0x0C, 0x0A, 0xC0, 0x69, 0xA7, 0x9D, 0xC5, 0x04
|
2016-10-16 20:40:56 +02:00
|
|
|
},
|
|
|
|
firm090A9lhHash[SHA_256_HASH_SIZE] = {
|
|
|
|
0x68, 0x52, 0xCC, 0x21, 0x89, 0xAE, 0x28, 0x38, 0x1A, 0x75, 0x90, 0xE7, 0x38, 0x23, 0x48, 0x41,
|
|
|
|
0x8E, 0x80, 0x78, 0x75, 0x27, 0x64, 0x04, 0xD6, 0x28, 0xD6, 0xFA, 0x39, 0xA8, 0x6F, 0xB0, 0x3F
|
2016-10-02 17:32:18 +02:00
|
|
|
},
|
2016-11-22 22:45:14 +01:00
|
|
|
firm1HashRetail[SHA_256_HASH_SIZE] = {
|
2016-09-28 01:57:18 +02:00
|
|
|
0xD8, 0x2D, 0xB7, 0xB4, 0x38, 0x2B, 0x07, 0x88, 0x99, 0x77, 0x91, 0x0C, 0xC6, 0xEC, 0x6D, 0x87,
|
|
|
|
0x7D, 0x21, 0x79, 0x23, 0xD7, 0x60, 0xAF, 0x4E, 0x8B, 0x3A, 0xAB, 0xB2, 0x63, 0xE4, 0x21, 0xC6
|
2016-10-17 16:22:35 +02:00
|
|
|
},
|
|
|
|
firm104O3DSHash[SHA_256_HASH_SIZE] = {
|
|
|
|
0x5D, 0x33, 0xD9, 0xCE, 0xE3, 0x39, 0x05, 0xD5, 0xCE, 0x37, 0xFE, 0xFB, 0xB5, 0xEC, 0x73, 0x6A,
|
|
|
|
0xA0, 0x10, 0xAD, 0x87, 0xF8, 0xDC, 0x55, 0x39, 0xFD, 0xDB, 0x48, 0x69, 0xAC, 0x5F, 0x3C, 0x2B
|
|
|
|
},
|
|
|
|
firm104N3DSHash[SHA_256_HASH_SIZE] = {
|
|
|
|
0x2D, 0x6B, 0xCC, 0xCE, 0x3B, 0x81, 0xD7, 0xCA, 0x67, 0x17, 0x90, 0x33, 0x35, 0x4D, 0xFA, 0xA5,
|
|
|
|
0x70, 0xF4, 0x7A, 0x99, 0xBB, 0x60, 0x0C, 0x2F, 0x34, 0x90, 0xFF, 0x10, 0xD4, 0x4C, 0x97, 0x42
|
2016-11-11 17:44:44 +01:00
|
|
|
},
|
|
|
|
sectorHashDev[SHA_256_HASH_SIZE] = {
|
|
|
|
0xB2, 0x91, 0xD9, 0xB1, 0x33, 0x05, 0x79, 0x0D, 0x47, 0xC6, 0x06, 0x98, 0x4C, 0x67, 0xC3, 0x70,
|
|
|
|
0x09, 0x54, 0xE3, 0x85, 0xDE, 0x47, 0x55, 0xAF, 0xC6, 0xCB, 0x1D, 0x8D, 0xC7, 0x84, 0x5A, 0x64
|
|
|
|
},
|
|
|
|
firm0HashDev[SHA_256_HASH_SIZE] = {
|
|
|
|
0xCD, 0x62, 0xA6, 0x58, 0x40, 0x1B, 0x8B, 0x8F, 0xD3, 0x2C, 0x72, 0x58, 0xD8, 0x24, 0x21, 0x36,
|
|
|
|
0xCF, 0x83, 0x40, 0xA3, 0x34, 0x8E, 0xED, 0x33, 0x0A, 0x1A, 0x16, 0x04, 0x49, 0xC9, 0x74, 0x3E
|
|
|
|
},
|
|
|
|
firm0A9lhHashDev[SHA_256_HASH_SIZE] = {
|
|
|
|
0x60, 0xBB, 0xD1, 0x35, 0x44, 0x2F, 0xBD, 0x47, 0x69, 0xBF, 0x36, 0x4B, 0x0B, 0x79, 0x6E, 0x4C,
|
|
|
|
0xE1, 0xB2, 0xDB, 0x7A, 0xAD, 0xF0, 0x04, 0x31, 0xCB, 0xBD, 0x54, 0xD3, 0x99, 0x8C, 0x9C, 0xD2
|
|
|
|
},
|
|
|
|
firm1HashDev[SHA_256_HASH_SIZE] = {
|
|
|
|
0xCD, 0x87, 0x85, 0x33, 0x76, 0xCA, 0x2A, 0x3F, 0xFC, 0x24, 0x4C, 0x29, 0x95, 0x8B, 0xA8, 0x34,
|
|
|
|
0xF2, 0x38, 0x14, 0x58, 0x10, 0x83, 0x56, 0x4F, 0x0D, 0x5A, 0xDB, 0x29, 0x12, 0xD8, 0xA9, 0x84
|
2016-03-19 17:30:56 +01:00
|
|
|
};
|
|
|
|
|
2016-09-26 23:15:26 +02:00
|
|
|
u32 posY;
|
2016-06-07 13:53:24 +02:00
|
|
|
|
2016-11-22 22:45:14 +01:00
|
|
|
static void drawTitle(void)
|
2016-06-07 13:53:24 +02:00
|
|
|
{
|
2016-11-22 22:45:14 +01:00
|
|
|
initScreens();
|
2016-09-28 01:57:18 +02:00
|
|
|
|
2016-10-17 16:22:35 +02:00
|
|
|
posY = drawString(TITLE, 10, 10, COLOR_TITLE);
|
|
|
|
posY = drawString("Thanks to delebile, #cakey and StandardBus", 10, posY + SPACING_Y, COLOR_WHITE);
|
2016-11-14 17:27:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void main(void)
|
|
|
|
{
|
2016-11-22 23:04:15 +01:00
|
|
|
bool isOtpless = ISA9LH && *(vu32 *)0x80FD0FC == 0xEAFE4AA3 && magic == 0xDEADCAFE;
|
2016-11-14 17:27:10 +01:00
|
|
|
|
2016-11-22 22:45:14 +01:00
|
|
|
if(!isOtpless) drawTitle();
|
2016-03-20 04:34:20 +01:00
|
|
|
|
2016-11-22 22:45:14 +01:00
|
|
|
if(!sdmmc_sdcard_init(!isOtpless, true) && !isOtpless)
|
2016-09-28 21:52:51 +02:00
|
|
|
shutdown(1, "Error: failed to initialize SD and NAND");
|
|
|
|
|
2016-09-28 01:57:18 +02:00
|
|
|
u32 pressed;
|
2016-09-26 23:15:26 +02:00
|
|
|
|
2016-09-28 01:57:18 +02:00
|
|
|
if(!isOtpless)
|
|
|
|
{
|
2016-10-16 20:40:56 +02:00
|
|
|
posY = drawString(ISA9LH ? "Press SELECT to update A9LH, START to uninstall" : "Press SELECT for a full install", 10, posY + SPACING_Y, COLOR_WHITE);
|
2016-09-28 01:57:18 +02:00
|
|
|
posY = drawString("Press any other button to shutdown", 10, posY, COLOR_WHITE);
|
|
|
|
pressed = waitInput();
|
|
|
|
}
|
|
|
|
|
2016-10-16 20:40:56 +02:00
|
|
|
if(isOtpless || pressed == BUTTON_SELECT) installer(isOtpless);
|
|
|
|
if(pressed == BUTTON_START && ISA9LH) uninstaller();
|
2016-06-07 13:53:24 +02:00
|
|
|
|
|
|
|
shutdown(0, NULL);
|
|
|
|
}
|
|
|
|
|
2016-10-16 20:40:56 +02:00
|
|
|
static inline void installer(bool isOtpless)
|
2016-06-07 13:53:24 +02:00
|
|
|
{
|
2016-10-16 20:40:56 +02:00
|
|
|
bool updateKey2 = false,
|
|
|
|
updateFirm0 = false,
|
|
|
|
updateFirm1 = false;
|
2016-09-28 14:56:11 +02:00
|
|
|
u8 otp[256] = {0},
|
|
|
|
keySector[512];
|
|
|
|
|
2016-09-28 21:52:51 +02:00
|
|
|
if(!isOtpless && !mountFs(true))
|
2016-06-08 14:33:27 +02:00
|
|
|
shutdown(1, "Error: failed to mount the SD card");
|
2016-03-24 00:14:29 +01:00
|
|
|
|
2016-09-28 01:57:18 +02:00
|
|
|
//If making a first install on O3DS, we need the OTP
|
2016-11-12 02:37:46 +01:00
|
|
|
if(!ISA9LH && (!ISN3DS || ISDEVUNIT))
|
2016-06-07 13:53:24 +02:00
|
|
|
{
|
2016-09-26 23:15:26 +02:00
|
|
|
const char otpPath[] = "a9lh/otp.bin";
|
|
|
|
|
|
|
|
//Prefer OTP from memory if available
|
2016-09-28 14:56:11 +02:00
|
|
|
if(memcmp((void *)OTP_FROM_MEM, otp, sizeof(otp)) == 0)
|
2016-06-07 13:53:24 +02:00
|
|
|
{
|
2016-08-24 13:16:08 -07:00
|
|
|
// Read OTP from file
|
2016-09-28 14:56:11 +02:00
|
|
|
if(fileRead(otp, otpPath, sizeof(otp)) != sizeof(otp))
|
2016-09-26 23:15:26 +02:00
|
|
|
shutdown(1, "Error: otp.bin doesn't exist and can't be dumped");
|
2016-08-24 13:16:08 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-26 23:15:26 +02:00
|
|
|
//Write OTP from memory to file
|
2016-09-28 14:56:11 +02:00
|
|
|
fileWrite((void *)OTP_FROM_MEM, otpPath, sizeof(otp));
|
|
|
|
memcpy(otp, (void *)OTP_FROM_MEM, sizeof(otp));
|
2016-06-07 13:53:24 +02:00
|
|
|
}
|
2016-03-20 04:34:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Setup the key sector de/encryption with the SHA register or otp.bin
|
2016-11-12 00:55:57 +01:00
|
|
|
if(ISA9LH || !ISN3DS || ISDEVUNIT) setupKeyslot0x11(otp);
|
2016-03-20 04:34:20 +01:00
|
|
|
|
2016-03-19 17:30:56 +01:00
|
|
|
//Calculate the CTR for the 3DS partitions
|
2016-09-28 01:57:18 +02:00
|
|
|
getNandCtr();
|
2016-03-19 17:30:56 +01:00
|
|
|
|
2016-03-25 04:47:07 +01:00
|
|
|
//Get NAND FIRM0 and test that the CTR is correct
|
2016-09-28 01:57:18 +02:00
|
|
|
if(!isOtpless)
|
|
|
|
{
|
|
|
|
readFirm0((u8 *)FIRM0_OFFSET, FIRM0_SIZE);
|
|
|
|
if(memcmp((void *)FIRM0_OFFSET, "FIRM", 4) != 0)
|
|
|
|
shutdown(1, "Error: failed to setup FIRM encryption");
|
|
|
|
}
|
2016-03-19 17:30:56 +01:00
|
|
|
|
2016-03-25 16:51:09 +01:00
|
|
|
//If booting from A9LH or on N3DS, we can use the key sector from NAND
|
2016-11-11 17:44:44 +01:00
|
|
|
if(ISA9LH || ISN3DS) getSector(keySector);
|
2016-09-28 01:57:18 +02:00
|
|
|
else
|
2016-06-07 13:53:24 +02:00
|
|
|
{
|
2016-09-28 01:57:18 +02:00
|
|
|
//Read decrypted key sector
|
2016-09-28 14:56:11 +02:00
|
|
|
if(fileRead(keySector, "a9lh/secret_sector.bin", sizeof(keySector)) != sizeof(keySector))
|
2016-09-28 01:57:18 +02:00
|
|
|
shutdown(1, "Error: secret_sector.bin doesn't exist or has\na wrong size");
|
2016-11-11 17:44:44 +01:00
|
|
|
if(!verifyHash(keySector, sizeof(keySector), !ISDEVUNIT ? sectorHashRetail : sectorHashDev))
|
2016-09-28 01:57:18 +02:00
|
|
|
shutdown(1, "Error: secret_sector.bin is invalid or corrupted");
|
|
|
|
}
|
2016-06-07 13:53:24 +02:00
|
|
|
|
2016-10-16 20:40:56 +02:00
|
|
|
if(ISA9LH && !isOtpless)
|
2016-09-28 01:57:18 +02:00
|
|
|
{
|
2016-10-16 20:40:56 +02:00
|
|
|
u32 i;
|
2016-11-11 17:44:44 +01:00
|
|
|
|
|
|
|
if(!ISDEVUNIT)
|
|
|
|
{
|
|
|
|
for(i = 1; i < 5; i++)
|
|
|
|
if(memcmp(keySector + AES_BLOCK_SIZE, key2s[i], AES_BLOCK_SIZE) == 0) break;
|
|
|
|
}
|
|
|
|
else i = memcmp(keySector + AES_BLOCK_SIZE, devKey2s[1], AES_BLOCK_SIZE) == 0 ? 1 : 5;
|
2016-06-07 13:53:24 +02:00
|
|
|
|
2016-10-16 20:40:56 +02:00
|
|
|
switch(i)
|
|
|
|
{
|
|
|
|
case 5:
|
|
|
|
shutdown(1, "Error: the OTP hash or the NAND key sector\nare invalid");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
updateKey2 = true;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
updateFirm1 = true;
|
|
|
|
updateKey2 = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-11-11 17:44:44 +01:00
|
|
|
if(!verifyHash((void *)FIRM0_OFFSET, SECTION2_POSITION, !ISDEVUNIT ? firm0A9lhHashRetail : firm0A9lhHashDev))
|
2016-10-16 20:40:56 +02:00
|
|
|
{
|
2016-11-11 17:44:44 +01:00
|
|
|
if(ISDEVUNIT || !verifyHash((void *)FIRM0_OFFSET, SECTION2_POSITION, firm090A9lhHash))
|
2016-10-16 20:40:56 +02:00
|
|
|
shutdown(1, "Error: NAND FIRM0 is invalid");
|
|
|
|
|
|
|
|
updateFirm0 = true;
|
|
|
|
}
|
2016-06-07 13:53:24 +02:00
|
|
|
}
|
2016-09-28 01:57:18 +02:00
|
|
|
|
2016-11-11 17:44:44 +01:00
|
|
|
if(!ISA9LH || updateKey2 || isOtpless) generateSector(keySector, (!ISA9LH && ISN3DS && !ISDEVUNIT) ? 1 : 0);
|
2016-10-16 20:40:56 +02:00
|
|
|
|
|
|
|
if(!ISA9LH || updateFirm0)
|
2016-06-07 13:53:24 +02:00
|
|
|
{
|
2016-03-25 04:47:07 +01:00
|
|
|
//Read FIRM0
|
2016-09-28 01:57:18 +02:00
|
|
|
if(fileRead((void *)FIRM0_OFFSET, "a9lh/firm0.bin", FIRM0_SIZE) != FIRM0_SIZE)
|
2016-03-25 04:47:07 +01:00
|
|
|
shutdown(1, "Error: firm0.bin doesn't exist or has a wrong size");
|
2016-11-11 17:44:44 +01:00
|
|
|
if(!verifyHash((void *)FIRM0_OFFSET, FIRM0_SIZE, !ISDEVUNIT ? firm0HashRetail : firm0HashDev))
|
2016-03-25 04:47:07 +01:00
|
|
|
shutdown(1, "Error: firm0.bin is invalid or corrupted");
|
2016-06-07 13:53:24 +02:00
|
|
|
}
|
2016-03-25 04:47:07 +01:00
|
|
|
|
2016-10-16 20:40:56 +02:00
|
|
|
if(!ISA9LH || updateFirm1)
|
2016-09-28 22:18:48 +02:00
|
|
|
{
|
|
|
|
//Read FIRM1
|
|
|
|
if(fileRead((void *)FIRM1_OFFSET, "a9lh/firm1.bin", FIRM1_SIZE) != FIRM1_SIZE)
|
|
|
|
shutdown(1, "Error: firm1.bin doesn't exist or has a wrong size");
|
2016-11-11 17:44:44 +01:00
|
|
|
if(!verifyHash((void *)FIRM1_OFFSET, FIRM1_SIZE, !ISDEVUNIT ? firm1HashRetail : firm1HashDev))
|
2016-09-28 22:18:48 +02:00
|
|
|
shutdown(1, "Error: firm1.bin is invalid or corrupted");
|
|
|
|
}
|
|
|
|
|
2016-11-22 23:04:15 +01:00
|
|
|
if(!ISA9LH && ISN3DS && !ISDEVUNIT)
|
|
|
|
{
|
|
|
|
magic = 0xDEADCAFE;
|
|
|
|
|
|
|
|
if(!fileWrite((void *)0x23F00000, "arm9loaderhax.bin", 0x10000))
|
|
|
|
shutdown(1, "Error: couldn't write arm9loaderhax.bin");
|
|
|
|
}
|
2016-11-22 22:45:14 +01:00
|
|
|
|
2016-09-28 01:57:18 +02:00
|
|
|
if(!isOtpless)
|
2016-06-07 13:53:24 +02:00
|
|
|
{
|
2016-10-02 17:18:49 +02:00
|
|
|
bool missingStage1Hash,
|
|
|
|
missingStage2Hash;
|
|
|
|
u8 stageHash[SHA_256_HASH_SIZE];
|
|
|
|
|
2016-09-28 01:57:18 +02:00
|
|
|
//Inject stage1
|
|
|
|
memset32((void *)STAGE1_OFFSET, 0, MAX_STAGE1_SIZE);
|
2016-10-02 17:32:18 +02:00
|
|
|
u32 stageSize = fileRead((void *)STAGE1_OFFSET, "a9lh/payload_stage1.bin", MAX_STAGE1_SIZE);
|
|
|
|
if(!stageSize)
|
2016-09-28 01:57:18 +02:00
|
|
|
shutdown(1, "Error: payload_stage1.bin doesn't exist or\nexceeds max size");
|
2016-06-07 13:53:24 +02:00
|
|
|
|
2016-09-28 01:57:18 +02:00
|
|
|
const u8 zeroes[688] = {0};
|
|
|
|
if(memcmp(zeroes, (void *)STAGE1_OFFSET, 688) == 0)
|
|
|
|
shutdown(1, "Error: the payload_stage1.bin you're attempting\nto install is not compatible");
|
|
|
|
|
2016-10-02 17:18:49 +02:00
|
|
|
//Verify stage1
|
|
|
|
if(fileRead(stageHash, "a9lh/payload_stage1.bin.sha", sizeof(stageHash)) == sizeof(stageHash))
|
|
|
|
{
|
2016-10-02 17:32:18 +02:00
|
|
|
if(!verifyHash((void *)STAGE1_OFFSET, stageSize, stageHash))
|
2016-10-02 17:18:49 +02:00
|
|
|
shutdown(1, "Error: payload_stage1.bin is invalid\nor corrupted");
|
|
|
|
|
|
|
|
missingStage1Hash = false;
|
|
|
|
}
|
|
|
|
else missingStage1Hash = true;
|
|
|
|
|
2016-09-28 01:57:18 +02:00
|
|
|
//Read stage2
|
|
|
|
memset32((void *)STAGE2_OFFSET, 0, MAX_STAGE2_SIZE);
|
2016-10-02 17:32:18 +02:00
|
|
|
stageSize = fileRead((void *)STAGE2_OFFSET, "a9lh/payload_stage2.bin", MAX_STAGE2_SIZE);
|
|
|
|
if(!stageSize)
|
2016-09-28 01:57:18 +02:00
|
|
|
shutdown(1, "Error: payload_stage2.bin doesn't exist or\nexceeds max size");
|
|
|
|
|
2016-10-02 17:18:49 +02:00
|
|
|
//Verify stage2
|
|
|
|
if(fileRead(stageHash, "a9lh/payload_stage2.bin.sha", sizeof(stageHash)) == sizeof(stageHash))
|
|
|
|
{
|
2016-10-02 17:32:18 +02:00
|
|
|
if(!verifyHash((void *)STAGE2_OFFSET, stageSize, stageHash))
|
2016-10-02 17:18:49 +02:00
|
|
|
shutdown(1, "Error: payload_stage2.bin is invalid\nor corrupted");
|
|
|
|
|
|
|
|
missingStage2Hash = false;
|
|
|
|
}
|
|
|
|
else missingStage2Hash = true;
|
|
|
|
|
|
|
|
if(missingStage1Hash || missingStage2Hash)
|
|
|
|
{
|
2016-10-17 16:22:35 +02:00
|
|
|
posY = drawString("Couldn't verify stage1 and/or stage2 integrity!", 10, posY + SPACING_Y, COLOR_RED);
|
2016-10-02 17:18:49 +02:00
|
|
|
posY = drawString("Continuing might be dangerous!", 10, posY, COLOR_RED);
|
|
|
|
inputSequence();
|
|
|
|
}
|
|
|
|
|
2016-09-28 01:57:18 +02:00
|
|
|
posY = drawString("All checks passed, installing...", 10, posY + SPACING_Y, COLOR_WHITE);
|
|
|
|
|
|
|
|
sdmmc_nand_writesectors(0x5C000, MAX_STAGE2_SIZE / 0x200, (u8 *)STAGE2_OFFSET);
|
2016-03-25 04:47:07 +01:00
|
|
|
}
|
2016-03-19 17:30:56 +01:00
|
|
|
|
2016-10-16 20:40:56 +02:00
|
|
|
if(!ISA9LH || updateFirm1) writeFirm((u8 *)FIRM1_OFFSET, true, FIRM1_SIZE);
|
|
|
|
if(!ISA9LH || updateKey2 || isOtpless) sdmmc_nand_writesectors(0x96, 1, keySector);
|
2016-11-22 22:45:14 +01:00
|
|
|
if(!isOtpless) writeFirm((u8 *)FIRM0_OFFSET, false, FIRM0_SIZE);
|
|
|
|
else
|
|
|
|
{
|
2016-11-22 23:04:15 +01:00
|
|
|
*(vu32 *)0x80FD0FC = 0;
|
2016-11-22 23:26:04 +01:00
|
|
|
|
2016-11-22 22:45:14 +01:00
|
|
|
drawTitle();
|
2016-11-22 23:26:04 +01:00
|
|
|
|
|
|
|
if(sdmmc_sdcard_init(true, false) && mountFs(true)) fileDelete("arm9loaderhax.bin");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
posY = drawString("Couldn't remove arm9loaderhax.bin!", 10, posY + SPACING_Y, COLOR_RED);
|
|
|
|
posY = drawString("Do it yourself after the install ends", 10, posY, COLOR_RED);
|
|
|
|
}
|
2016-11-22 22:45:14 +01:00
|
|
|
}
|
2016-03-19 17:30:56 +01:00
|
|
|
|
2016-11-11 17:44:44 +01:00
|
|
|
if(!ISA9LH && ISN3DS && !ISDEVUNIT)
|
2016-09-28 01:57:18 +02:00
|
|
|
{
|
2016-11-22 23:04:15 +01:00
|
|
|
*(vu32 *)0x80FD0FC = 0xEAFE4AA3;
|
2016-03-19 17:30:56 +01:00
|
|
|
|
2016-09-28 01:57:18 +02:00
|
|
|
mcuReboot();
|
|
|
|
}
|
2016-03-19 17:30:56 +01:00
|
|
|
|
2016-10-16 20:40:56 +02:00
|
|
|
shutdown(2, ISA9LH && !isOtpless ? "Update: success!" : "Full install: success!");
|
2016-06-07 13:53:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void uninstaller(void)
|
|
|
|
{
|
2016-09-28 14:56:11 +02:00
|
|
|
u8 keySector[512];
|
|
|
|
|
2016-06-08 14:41:52 +02:00
|
|
|
//New 3DSes need a key sector with a proper key2, Old 3DSes have a blank key sector
|
2016-10-16 20:40:56 +02:00
|
|
|
if(ISN3DS)
|
2016-06-07 13:53:24 +02:00
|
|
|
{
|
2016-11-11 17:44:44 +01:00
|
|
|
setupKeyslot0x11(NULL);
|
|
|
|
getSector(keySector);
|
2016-10-16 20:48:25 +02:00
|
|
|
|
|
|
|
u32 i;
|
2016-11-11 17:44:44 +01:00
|
|
|
|
|
|
|
if(!ISDEVUNIT)
|
|
|
|
{
|
|
|
|
for(i = 1; i < 5; i++)
|
|
|
|
if(memcmp(keySector + AES_BLOCK_SIZE, key2s[i], AES_BLOCK_SIZE) == 0) break;
|
|
|
|
}
|
|
|
|
else i = memcmp(keySector + AES_BLOCK_SIZE, devKey2s[1], AES_BLOCK_SIZE) == 0 ? 1 : 5;
|
2016-10-16 20:48:25 +02:00
|
|
|
|
|
|
|
if(i == 5) shutdown(1, "Error: the OTP hash or the NAND key sector\nare invalid");
|
|
|
|
|
2016-09-28 14:56:11 +02:00
|
|
|
generateSector(keySector, 2);
|
2016-06-07 13:53:24 +02:00
|
|
|
}
|
2016-09-28 14:56:11 +02:00
|
|
|
else memset32(keySector, 0, sizeof(keySector));
|
2016-06-07 13:53:24 +02:00
|
|
|
|
2016-09-28 21:52:51 +02:00
|
|
|
if(!mountFs(false))
|
2016-06-08 14:33:27 +02:00
|
|
|
shutdown(1, "Error: failed to mount CTRNAND");
|
2016-06-07 13:53:24 +02:00
|
|
|
|
2016-06-08 14:41:52 +02:00
|
|
|
//Read FIRM cxi from CTRNAND
|
2016-10-17 16:22:35 +02:00
|
|
|
u32 firmSize = 0,
|
|
|
|
result = firmRead((void *)FIRM0_OFFSET);
|
|
|
|
|
|
|
|
switch(result)
|
2016-06-07 13:53:24 +02:00
|
|
|
{
|
|
|
|
case 1:
|
2016-10-17 16:30:21 +02:00
|
|
|
shutdown(1, "Error: more than one FIRM has been detected");
|
2016-06-07 13:53:24 +02:00
|
|
|
break;
|
2016-10-17 16:22:35 +02:00
|
|
|
case 5:
|
2016-11-14 17:42:39 +01:00
|
|
|
posY = drawString("FIRM 11.0/11.1/11.2 has been detected!", 10, posY + SPACING_Y, COLOR_RED);
|
2016-10-17 16:22:35 +02:00
|
|
|
posY = drawString("Press SELECT to load 10.4 FIRM from SD", 10, posY + SPACING_Y, COLOR_WHITE);
|
|
|
|
posY = drawString("Press any other button to load FIRM from CTRNAND", 10, posY, COLOR_RED);
|
|
|
|
|
|
|
|
if(waitInput() == BUTTON_SELECT)
|
|
|
|
{
|
|
|
|
u32 firm104Size = ISN3DS ? 0xF2000 : 0xEA000;
|
|
|
|
|
|
|
|
unmountCtrNand();
|
|
|
|
|
|
|
|
if(!mountFs(true)) shutdown(1, "Error: failed to mount the SD card");
|
|
|
|
|
2016-10-17 16:30:21 +02:00
|
|
|
if(fileRead((void *)FIRM0_OFFSET, "a9lh/firm104.bin", firm104Size) != firm104Size)
|
2016-10-17 16:22:35 +02:00
|
|
|
shutdown(1, "Error: firm104.bin doesn't exist or has a wrong size");
|
|
|
|
|
|
|
|
if(!verifyHash((void *)FIRM0_OFFSET, firm104Size, ISN3DS ? firm104N3DSHash : firm104O3DSHash))
|
|
|
|
shutdown(1, "Error: firm104.bin is invalid or corrupted");
|
|
|
|
|
|
|
|
firmSize = firm104Size;
|
|
|
|
break;
|
|
|
|
}
|
2016-06-07 13:53:24 +02:00
|
|
|
case 2:
|
2016-11-14 17:42:39 +01:00
|
|
|
if(result == 2) posY = drawString("A FIRM newer than 11.2 has been detected!", 10, posY + SPACING_Y, COLOR_RED);
|
2016-10-17 16:22:35 +02:00
|
|
|
posY = drawString("You are about to uninstall A9LH!", 10, posY + SPACING_Y, COLOR_RED);
|
|
|
|
posY = drawString("To reinstall you'll need an hardmod or a DSi dg!", 10, posY, COLOR_RED);
|
2016-06-07 13:53:24 +02:00
|
|
|
break;
|
2016-09-28 14:56:11 +02:00
|
|
|
case 3:
|
|
|
|
shutdown(1, "Error: the CTRNAND FIRM is too large");
|
|
|
|
break;
|
2016-09-28 21:52:51 +02:00
|
|
|
case 4:
|
|
|
|
shutdown(1, "Error: couldn't read FIRM from CTRNAND");
|
|
|
|
break;
|
2016-06-07 13:53:24 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-06-08 14:41:52 +02:00
|
|
|
|
2016-10-17 16:22:35 +02:00
|
|
|
if(firmSize != 0 || !result)
|
|
|
|
{
|
|
|
|
posY = drawString("You are about to uninstall A9LH!", 10, posY + SPACING_Y, COLOR_RED);
|
|
|
|
posY = drawString("To reinstall you'll need 9.2 or lower!", 10, posY, COLOR_RED);
|
|
|
|
}
|
|
|
|
|
|
|
|
inputSequence();
|
|
|
|
|
2016-06-08 14:41:52 +02:00
|
|
|
//Decrypt it and get its size
|
2016-10-17 16:22:35 +02:00
|
|
|
if(!firmSize)
|
|
|
|
{
|
|
|
|
firmSize = decryptExeFs((Cxi *)FIRM0_OFFSET);
|
|
|
|
if(firmSize == 0) shutdown(1, "Error: couldn't decrypt the CTRNAND FIRM");
|
|
|
|
}
|
2016-06-07 13:53:24 +02:00
|
|
|
|
2016-06-08 14:41:52 +02:00
|
|
|
//writeFirm encrypts in-place, so we need two copies
|
2016-06-07 13:53:24 +02:00
|
|
|
memcpy((void *)FIRM1_OFFSET, (void *)FIRM0_OFFSET, firmSize);
|
|
|
|
|
2016-06-08 14:41:52 +02:00
|
|
|
//Zero out the stage2 space on NAND
|
2016-06-07 13:53:24 +02:00
|
|
|
memset32((void *)STAGE2_OFFSET, 0, MAX_STAGE2_SIZE);
|
|
|
|
|
2016-06-08 14:41:52 +02:00
|
|
|
posY = drawString("All checks passed, uninstalling...", 10, posY + SPACING_Y, COLOR_WHITE);
|
2016-06-07 13:53:24 +02:00
|
|
|
|
2016-06-08 14:41:52 +02:00
|
|
|
//Point of no return, install stuff in the safest order
|
2016-09-28 14:56:11 +02:00
|
|
|
sdmmc_nand_writesectors(0x96, 1, keySector);
|
2016-09-26 23:15:26 +02:00
|
|
|
writeFirm((u8 *)FIRM0_OFFSET, false, firmSize);
|
|
|
|
writeFirm((u8 *)FIRM1_OFFSET, true, firmSize);
|
|
|
|
sdmmc_nand_writesectors(0x5C000, MAX_STAGE2_SIZE / 0x200, (u8 *)STAGE2_OFFSET);
|
2016-06-07 13:53:24 +02:00
|
|
|
|
|
|
|
shutdown(2, "Uninstall: success!");
|
2016-09-26 23:15:26 +02:00
|
|
|
}
|