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-09-28 01:57:18 +02:00
|
|
|
#include "../build/bundled.h"
|
2016-03-19 17:30:56 +01:00
|
|
|
|
2016-10-02 17:18:49 +02:00
|
|
|
static const u8 sectorHash[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
|
|
|
},
|
|
|
|
firm0Hash[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
|
|
|
},
|
|
|
|
firm0A9lhHash[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
|
|
|
},
|
|
|
|
firm0100Hash[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-02 17:32:18 +02:00
|
|
|
},
|
|
|
|
firm1Hash[SHA_256_HASH_SIZE] = {
|
2016-03-19 17:30:56 +01:00
|
|
|
0xD2, 0x53, 0xC1, 0xCC, 0x0A, 0x5F, 0xFA, 0xC6, 0xB3, 0x83, 0xDA, 0xC1, 0x82, 0x7C, 0xFB, 0x3B,
|
|
|
|
0x2D, 0x3D, 0x56, 0x6C, 0x6A, 0x1A, 0x8E, 0x52, 0x54, 0xE3, 0x89, 0xC2, 0x95, 0x06, 0x23, 0xE5
|
|
|
|
};
|
|
|
|
|
2016-09-26 23:15:26 +02:00
|
|
|
u32 posY;
|
2016-06-07 13:53:24 +02:00
|
|
|
|
|
|
|
void main(void)
|
|
|
|
{
|
2016-09-28 01:57:18 +02:00
|
|
|
vu32 *magic = (vu32 *)0x25000000;
|
2016-10-16 20:40:56 +02:00
|
|
|
bool isOtpless = ISA9LH && magic[0] == 0xABADCAFE && magic[1] == 0xDEADCAFE;
|
2016-09-28 01:57:18 +02:00
|
|
|
|
|
|
|
initScreens();
|
|
|
|
|
2016-03-27 19:33:21 +02:00
|
|
|
drawString(TITLE, 10, 10, COLOR_TITLE);
|
2016-06-08 14:41:52 +02:00
|
|
|
posY = drawString("Thanks to delebile, #cakey and StandardBus", 10, 40, COLOR_WHITE);
|
2016-03-20 04:34:20 +01:00
|
|
|
|
2016-10-06 17:12:44 +02:00
|
|
|
if(!sdmmc_sdcard_init(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();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-28 22:31:44 +02:00
|
|
|
magic[0] = magic[1] = 0;
|
2016-09-28 01:57:18 +02:00
|
|
|
posY = drawString("Finalizing install...", 10, posY + SPACING_Y, COLOR_WHITE);
|
|
|
|
pressed = 0;
|
|
|
|
}
|
|
|
|
|
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-10-16 20:40:56 +02:00
|
|
|
if(!ISA9LH && !ISN3DS)
|
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-10-16 20:40:56 +02:00
|
|
|
if(ISA9LH || !ISN3DS) setupKeyslot0x11(otp, ISA9LH);
|
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-10-16 20:40:56 +02:00
|
|
|
if(ISA9LH || ISN3DS) getSector(keySector, ISA9LH);
|
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-09-28 14:56:11 +02:00
|
|
|
if(!verifyHash(keySector, sizeof(keySector), sectorHash))
|
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;
|
|
|
|
for(i = 1; i < 5; i++)
|
|
|
|
if(memcmp(keySector + AES_BLOCK_SIZE, key2s[i], AES_BLOCK_SIZE) == 0) break;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!verifyHash((void *)FIRM0_OFFSET, SECTION2_POSITION, firm0A9lhHash))
|
|
|
|
{
|
|
|
|
if(!verifyHash((void *)FIRM0_OFFSET, SECTION2_POSITION, firm090A9lhHash))
|
|
|
|
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-10-16 20:40:56 +02:00
|
|
|
if(!ISA9LH || updateKey2 || isOtpless) generateSector(keySector, (!ISA9LH && ISN3DS) ? 1 : 0);
|
|
|
|
|
|
|
|
if(!ISA9LH && ISN3DS)
|
|
|
|
{
|
|
|
|
//Read 10.0 FIRM0
|
|
|
|
if(fileRead((void *)FIRM0_100_OFFSET, "a9lh/firm0_100.bin", FIRM0100_SIZE) != FIRM0100_SIZE)
|
|
|
|
shutdown(1, "Error: firm0_100.bin doesn't exist or has a wrong size");
|
|
|
|
if(!verifyHash((void *)FIRM0_100_OFFSET, FIRM0100_SIZE, firm0100Hash))
|
|
|
|
shutdown(1, "Error: firm0_100.bin is invalid or corrupted");
|
|
|
|
}
|
2016-09-28 01:57:18 +02:00
|
|
|
|
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-06-07 13:53:24 +02:00
|
|
|
if(!verifyHash((void *)FIRM0_OFFSET, FIRM0_SIZE, firm0Hash))
|
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");
|
|
|
|
if(!verifyHash((void *)FIRM1_OFFSET, FIRM1_SIZE, firm1Hash))
|
|
|
|
shutdown(1, "Error: firm1.bin is invalid or corrupted");
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
posY = drawString("Couldn't verify stage1 and/or stage2 integrity!", 10, posY + 10, COLOR_RED);
|
|
|
|
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-03-19 17:30:56 +01:00
|
|
|
|
2016-10-16 20:40:56 +02:00
|
|
|
if(!ISA9LH && ISN3DS)
|
2016-09-28 01:57:18 +02:00
|
|
|
{
|
2016-10-02 17:18:49 +02:00
|
|
|
*(vu32 *)0x80FD0FC = 0xEAFFCBBF; //B 0x80F0000
|
2016-09-28 01:57:18 +02:00
|
|
|
memcpy((void *)0x80F0000, loader_bin, loader_bin_size);
|
2016-06-09 17:27:10 +02:00
|
|
|
|
2016-09-30 01:02:34 +02:00
|
|
|
writeFirm((u8 *)FIRM0_100_OFFSET, false, FIRM0100_SIZE);
|
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-09-26 23:15:26 +02:00
|
|
|
writeFirm((u8 *)FIRM0_OFFSET, false, FIRM0_SIZE);
|
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
|
|
|
posY = drawString("You are about to uninstall A9LH!", 10, posY + 10, COLOR_RED);
|
|
|
|
posY = drawString("Doing this will require having 9.0 to reinstall!", 10, posY, COLOR_RED);
|
2016-10-02 17:18:49 +02:00
|
|
|
inputSequence();
|
2016-06-08 14:14:26 +02:00
|
|
|
|
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-09-28 01:57:18 +02:00
|
|
|
setupKeyslot0x11(NULL, true);
|
2016-09-28 14:56:11 +02:00
|
|
|
getSector(keySector, true);
|
|
|
|
if(memcmp(keySector + AES_BLOCK_SIZE, key2s[1], AES_BLOCK_SIZE) != 0 && memcmp(keySector + AES_BLOCK_SIZE, key2s[2], AES_BLOCK_SIZE) != 0)
|
2016-06-08 14:14:26 +02:00
|
|
|
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-06-07 13:53:24 +02:00
|
|
|
switch(firmRead((void *)FIRM0_OFFSET))
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
shutdown(1, "Error: more than one FIRM cxi has been detected");
|
|
|
|
break;
|
|
|
|
case 2:
|
2016-06-08 14:14:26 +02:00
|
|
|
shutdown(1, "Error: a FIRM equal or newer than 11.0\nhas been detected");
|
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
|
|
|
|
|
|
|
//Decrypt it and get its size
|
2016-10-16 20:40:56 +02:00
|
|
|
u32 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
|
|
|
}
|