2015-08-05 03:57:37 +02:00
|
|
|
/*
|
2016-07-05 16:05:53 +02:00
|
|
|
* This file is part of Luma3DS
|
2020-04-25 14:26:21 +02:00
|
|
|
* Copyright (C) 2016-2020 Aurora Wright, TuxSH
|
2016-07-05 16:05:53 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2017-06-05 02:02:04 +02:00
|
|
|
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
|
|
|
|
* * Requiring preservation of specified reasonable legal notices or
|
|
|
|
* author attributions in that material or in the Appropriate Legal
|
|
|
|
* Notices displayed by works containing it.
|
|
|
|
* * Prohibiting misrepresentation of the origin of that material,
|
|
|
|
* or requiring that modified versions of such material be marked in
|
|
|
|
* reasonable ways as different from the original version.
|
2015-08-05 03:57:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "firm.h"
|
2016-04-05 05:27:28 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "fs.h"
|
2016-09-19 14:03:55 +02:00
|
|
|
#include "exceptions.h"
|
2015-08-05 03:57:37 +02:00
|
|
|
#include "patches.h"
|
|
|
|
#include "memory.h"
|
2016-06-13 21:14:53 +02:00
|
|
|
#include "cache.h"
|
2015-08-05 03:57:37 +02:00
|
|
|
#include "emunand.h"
|
2015-08-21 20:11:23 +02:00
|
|
|
#include "crypto.h"
|
2016-06-10 21:48:22 +02:00
|
|
|
#include "screen.h"
|
2017-05-07 17:58:44 +02:00
|
|
|
#include "fmt.h"
|
2018-05-22 17:51:57 +02:00
|
|
|
#include "chainloader.h"
|
2016-09-11 03:29:39 +02:00
|
|
|
|
2017-06-10 02:39:00 +02:00
|
|
|
static Firm *firm = (Firm *)0x20001000;
|
|
|
|
|
|
|
|
static __attribute__((noinline)) bool overlaps(u32 as, u32 ae, u32 bs, u32 be)
|
|
|
|
{
|
|
|
|
if(as <= bs && bs <= ae)
|
|
|
|
return true;
|
|
|
|
if(bs <= as && as <= be)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __attribute__((noinline)) bool inRange(u32 as, u32 ae, u32 bs, u32 be)
|
|
|
|
{
|
|
|
|
if(as >= bs && ae <= be)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-10 02:55:55 +02:00
|
|
|
static bool checkFirm(u32 firmSize)
|
2017-06-10 02:39:00 +02:00
|
|
|
{
|
2020-04-25 14:17:23 +02:00
|
|
|
if(memcmp(firm->magic, "FIRM", 4) != 0 || firm->arm9Entry == NULL) //Allow for the Arm11 entrypoint to be zero in which case nothing is done on the Arm11 side
|
2017-06-10 02:39:00 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
bool arm9EpFound = false,
|
|
|
|
arm11EpFound = false;
|
|
|
|
|
|
|
|
u32 size = 0x200;
|
|
|
|
for(u32 i = 0; i < 4; i++)
|
|
|
|
size += firm->section[i].size;
|
|
|
|
|
2017-06-10 02:55:55 +02:00
|
|
|
if(firmSize < size) return false;
|
2017-06-10 02:39:00 +02:00
|
|
|
|
|
|
|
for(u32 i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
FirmSection *section = &firm->section[i];
|
|
|
|
|
|
|
|
//Allow empty sections
|
|
|
|
if(section->size == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if((section->offset < 0x200) ||
|
|
|
|
(section->address + section->size < section->address) || //Overflow check
|
|
|
|
((u32)section->address & 3) || (section->offset & 0x1FF) || (section->size & 0x1FF) || //Alignment check
|
|
|
|
(overlaps((u32)section->address, (u32)section->address + section->size, (u32)firm, (u32)firm + size)) ||
|
|
|
|
((!inRange((u32)section->address, (u32)section->address + section->size, 0x08000000, 0x08000000 + 0x00100000)) &&
|
|
|
|
(!inRange((u32)section->address, (u32)section->address + section->size, 0x18000000, 0x18000000 + 0x00600000)) &&
|
|
|
|
(!inRange((u32)section->address, (u32)section->address + section->size, 0x1FF00000, 0x1FFFFC00)) &&
|
|
|
|
(!inRange((u32)section->address, (u32)section->address + section->size, 0x20000000, 0x20000000 + 0x8000000))))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
__attribute__((aligned(4))) u8 hash[0x20];
|
|
|
|
|
|
|
|
sha(hash, (u8 *)firm + section->offset, section->size, SHA_256_MODE);
|
|
|
|
|
|
|
|
if(memcmp(hash, section->hash, 0x20) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(firm->arm9Entry >= section->address && firm->arm9Entry < (section->address + section->size))
|
|
|
|
arm9EpFound = true;
|
|
|
|
|
|
|
|
if(firm->arm11Entry >= section->address && firm->arm11Entry < (section->address + section->size))
|
|
|
|
arm11EpFound = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return arm9EpFound && (firm->arm11Entry == NULL || arm11EpFound);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 loadFirmFromStorage(FirmwareType firmType)
|
2016-04-11 05:15:44 +02:00
|
|
|
{
|
2017-08-16 18:02:35 +02:00
|
|
|
static const char *firmwareFiles[] = {
|
2017-05-23 02:44:04 +02:00
|
|
|
"native.firm",
|
|
|
|
"twl.firm",
|
|
|
|
"agb.firm",
|
|
|
|
"safe.firm",
|
|
|
|
"sysupdater.firm"
|
2016-09-18 23:50:44 +02:00
|
|
|
},
|
2017-08-16 18:02:35 +02:00
|
|
|
*cetkFiles[] = {
|
2016-10-12 02:28:08 +02:00
|
|
|
"cetk",
|
|
|
|
"cetk_twl",
|
|
|
|
"cetk_agb",
|
2016-11-12 22:27:03 +01:00
|
|
|
"cetk_safe",
|
|
|
|
"cetk_sysupdater"
|
2016-08-04 13:44:51 +02:00
|
|
|
};
|
2016-04-04 18:19:00 +02:00
|
|
|
|
2018-02-09 05:03:22 +01:00
|
|
|
u32 firmSize = fileRead(firm, firmwareFiles[(u32)firmType], 0x400000 + sizeof(Cxi) + 0x200);
|
2016-11-15 19:29:48 +01:00
|
|
|
|
2017-06-10 02:39:00 +02:00
|
|
|
if(!firmSize) return 0;
|
2016-11-15 19:29:48 +01:00
|
|
|
|
2017-06-10 02:39:00 +02:00
|
|
|
static const char *extFirmError = "The external FIRM is not valid.";
|
|
|
|
|
|
|
|
if(firmSize <= sizeof(Cxi) + 0x200) error(extFirmError);
|
2016-11-15 19:29:48 +01:00
|
|
|
|
|
|
|
if(memcmp(firm, "FIRM", 4) != 0)
|
|
|
|
{
|
2017-06-10 02:39:00 +02:00
|
|
|
if(firmSize <= sizeof(Cxi) + 0x400) error(extFirmError);
|
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
u8 cetk[0xA50];
|
|
|
|
|
2018-02-09 05:03:22 +01:00
|
|
|
if(fileRead(cetk, cetkFiles[(u32)firmType], sizeof(cetk)) != sizeof(cetk))
|
2017-06-10 02:39:00 +02:00
|
|
|
error("The cetk is missing or corrupted.");
|
|
|
|
|
|
|
|
firmSize = decryptNusFirm((Ticket *)(cetk + 0x140), (Cxi *)firm, firmSize);
|
|
|
|
|
|
|
|
if(!firmSize) error("Unable to decrypt the external FIRM.");
|
2016-11-15 19:29:48 +01:00
|
|
|
}
|
|
|
|
|
2017-06-28 20:22:58 +02:00
|
|
|
if(!checkFirm(firmSize)) error("The external FIRM is invalid or corrupted.");
|
|
|
|
|
2017-06-10 02:39:00 +02:00
|
|
|
return firmSize;
|
|
|
|
}
|
|
|
|
|
2018-02-05 03:47:43 +01:00
|
|
|
u32 loadNintendoFirm(FirmwareType *firmType, FirmwareSource nandType, bool loadFromStorage, bool isSafeMode)
|
|
|
|
{
|
|
|
|
u32 firmVersion,
|
|
|
|
firmSize;
|
|
|
|
|
|
|
|
bool ctrNandError = isSdMode && !mountFs(false, false);
|
|
|
|
|
|
|
|
if(!ctrNandError)
|
|
|
|
{
|
|
|
|
//Load FIRM from CTRNAND
|
|
|
|
firmVersion = firmRead(firm, (u32)*firmType);
|
|
|
|
|
|
|
|
if(firmVersion == 0xFFFFFFFF) ctrNandError = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
firmSize = decryptExeFs((Cxi *)firm);
|
|
|
|
|
2018-02-09 05:03:22 +01:00
|
|
|
if(!firmSize || !checkFirm(firmSize)) ctrNandError = true;
|
2018-02-05 03:47:43 +01:00
|
|
|
}
|
|
|
|
}
|
2017-06-10 02:39:00 +02:00
|
|
|
|
|
|
|
bool loadedFromStorage = false;
|
|
|
|
|
2018-02-05 03:47:43 +01:00
|
|
|
if(loadFromStorage || ctrNandError)
|
2017-06-10 02:39:00 +02:00
|
|
|
{
|
|
|
|
u32 result = loadFirmFromStorage(*firmType);
|
|
|
|
|
|
|
|
if(result != 0)
|
|
|
|
{
|
|
|
|
loadedFromStorage = true;
|
|
|
|
firmSize = result;
|
|
|
|
}
|
2018-02-05 03:47:43 +01:00
|
|
|
else if(ctrNandError) error("Unable to mount CTRNAND or load the CTRNAND FIRM.\nPlease use an external one.");
|
2017-06-10 02:39:00 +02:00
|
|
|
}
|
|
|
|
|
2020-04-25 14:17:23 +02:00
|
|
|
//Check that the FIRM is right for the console from the Arm9 section address
|
2016-11-15 19:29:48 +01:00
|
|
|
if((firm->section[3].offset != 0 ? firm->section[3].address : firm->section[2].address) != (ISN3DS ? (u8 *)0x8006000 : (u8 *)0x8006800))
|
2017-06-10 02:39:00 +02:00
|
|
|
error("The %s FIRM is not for this console.", loadedFromStorage ? "external" : "CTRNAND");
|
2016-11-15 19:29:48 +01:00
|
|
|
|
2018-02-09 05:03:22 +01:00
|
|
|
if(!ISN3DS && *firmType == NATIVE_FIRM && firm->section[0].address == (u8 *)0x1FF80000)
|
|
|
|
{
|
|
|
|
//We can't boot < 3.x EmuNANDs
|
|
|
|
if(nandType != FIRMWARE_SYSNAND) error("An old unsupported EmuNAND has been detected.\nLuma3DS is unable to boot it.");
|
|
|
|
|
|
|
|
if(isSafeMode) error("SAFE_MODE is not supported on 1.x/2.x FIRM.");
|
|
|
|
|
|
|
|
*firmType = NATIVE_FIRM1X2X;
|
|
|
|
}
|
|
|
|
|
2017-06-28 20:22:58 +02:00
|
|
|
if(loadedFromStorage || ISDEVUNIT)
|
|
|
|
{
|
|
|
|
firmVersion = 0xFFFFFFFF;
|
|
|
|
|
|
|
|
if(!ISN3DS && *firmType == NATIVE_FIRM)
|
|
|
|
{
|
|
|
|
__attribute__((aligned(4))) static const u8 hashes[3][0x20] = {
|
|
|
|
{0x39, 0x75, 0xB5, 0x28, 0x24, 0x5E, 0x8B, 0x56, 0xBC, 0x83, 0x79, 0x41, 0x09, 0x2C, 0x42, 0xE6,
|
|
|
|
0x26, 0xB6, 0x80, 0x59, 0xA5, 0x56, 0xF9, 0xF9, 0x6E, 0xF3, 0x63, 0x05, 0x58, 0xDF, 0x35, 0xEF},
|
|
|
|
{0x81, 0x9E, 0x71, 0x58, 0xE5, 0x44, 0x73, 0xF7, 0x48, 0x78, 0x7C, 0xEF, 0x5E, 0x30, 0xE2, 0x28,
|
|
|
|
0x78, 0x0B, 0x21, 0x23, 0x94, 0x63, 0xE8, 0x4E, 0x06, 0xBB, 0xD6, 0x8D, 0xA0, 0x99, 0xAE, 0x98},
|
|
|
|
{0x1D, 0xD5, 0xB0, 0xC2, 0xD9, 0x4A, 0x4A, 0xF3, 0x23, 0xDD, 0x2F, 0x65, 0x21, 0x95, 0x9B, 0x7E,
|
|
|
|
0xF2, 0x71, 0x7E, 0xB6, 0x7A, 0x3A, 0x74, 0x78, 0x0D, 0xE3, 0xB5, 0x0C, 0x2B, 0x7F, 0x85, 0x37}
|
|
|
|
};
|
|
|
|
|
|
|
|
u32 i;
|
|
|
|
for(i = 0; i < 3; i++) if(memcmp(firm->section[1].hash, hashes[i], 0x20) == 0) break;
|
|
|
|
|
|
|
|
switch(i)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
firmVersion = 0x18;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
firmVersion = 0x1D;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
firmVersion = 0x1F;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return firmVersion;
|
2017-06-10 02:39:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loadHomebrewFirm(u32 pressed)
|
|
|
|
{
|
|
|
|
char path[10 + 255];
|
|
|
|
bool found = !pressed ? payloadMenu(path) : findPayload(path, pressed);
|
|
|
|
|
|
|
|
if(!found) return;
|
|
|
|
|
2017-06-10 02:55:55 +02:00
|
|
|
u32 maxPayloadSize = (u32)((u8 *)0x27FFE000 - (u8 *)firm),
|
|
|
|
payloadSize = fileRead(firm, path, maxPayloadSize);
|
2017-06-10 02:39:00 +02:00
|
|
|
|
|
|
|
if(payloadSize <= 0x200 || !checkFirm(payloadSize)) error("The payload is invalid or corrupted.");
|
|
|
|
|
|
|
|
char absPath[24 + 255];
|
|
|
|
|
|
|
|
if(isSdMode) sprintf(absPath, "sdmc:/luma/%s", path);
|
|
|
|
else sprintf(absPath, "nand:/rw/luma/%s", path);
|
|
|
|
|
|
|
|
char *argv[2] = {absPath, (char *)fbs};
|
2017-06-10 02:55:55 +02:00
|
|
|
|
2017-06-10 02:39:00 +02:00
|
|
|
initScreens();
|
|
|
|
|
|
|
|
launchFirm((firm->reserved2[0] & 1) ? 2 : 1, argv);
|
2016-11-15 19:29:48 +01:00
|
|
|
}
|
|
|
|
|
2017-06-19 18:13:59 +02:00
|
|
|
static inline void mergeSection0(FirmwareType firmType, u32 firmVersion, bool loadFromStorage)
|
2017-05-23 02:44:04 +02:00
|
|
|
{
|
2017-06-09 17:29:26 +02:00
|
|
|
u32 srcModuleSize,
|
|
|
|
nbModules = 0;
|
2017-05-23 02:44:04 +02:00
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
char name[8];
|
|
|
|
u8 *src;
|
|
|
|
u32 size;
|
2017-05-24 15:18:31 +02:00
|
|
|
} moduleList[6];
|
2017-05-23 02:44:04 +02:00
|
|
|
|
|
|
|
//1) Parse info concerning Nintendo's modules
|
2017-05-24 15:18:31 +02:00
|
|
|
for(u8 *src = (u8 *)firm + firm->section[0].offset, *srcEnd = src + firm->section[0].size; src < srcEnd; src += srcModuleSize, nbModules++)
|
2017-05-23 02:44:04 +02:00
|
|
|
{
|
|
|
|
memcpy(moduleList[nbModules].name, ((Cxi *)src)->exHeader.systemControlInfo.appTitle, 8);
|
|
|
|
moduleList[nbModules].src = src;
|
2017-06-16 22:37:04 +02:00
|
|
|
srcModuleSize = moduleList[nbModules].size = ((Cxi *)src)->ncch.contentSize * 0x200;
|
2017-05-23 02:44:04 +02:00
|
|
|
}
|
2017-05-24 15:18:31 +02:00
|
|
|
|
2017-06-19 18:13:59 +02:00
|
|
|
if(firmType == NATIVE_FIRM && (ISN3DS || firmVersion >= 0x1D))
|
2017-05-23 02:44:04 +02:00
|
|
|
{
|
2017-06-16 22:37:04 +02:00
|
|
|
//2) Merge that info with our own modules'
|
2018-05-23 16:31:24 +02:00
|
|
|
for(u8 *src = (u8 *)0x18180000; memcmp(((Cxi *)src)->ncch.magic, "NCCH", 4) == 0; src += srcModuleSize)
|
2017-05-24 15:18:31 +02:00
|
|
|
{
|
|
|
|
const char *name = ((Cxi *)src)->exHeader.systemControlInfo.appTitle;
|
2017-05-23 02:44:04 +02:00
|
|
|
|
2017-05-24 15:18:31 +02:00
|
|
|
u32 i;
|
2017-05-23 02:44:04 +02:00
|
|
|
|
2017-06-09 17:29:26 +02:00
|
|
|
for(i = 0; i < 5 && memcmp(name, moduleList[i].name, 8) != 0; i++);
|
|
|
|
|
|
|
|
if(i == 5)
|
|
|
|
{
|
|
|
|
nbModules++;
|
|
|
|
memcpy(moduleList[i].name, ((Cxi *)src)->exHeader.systemControlInfo.appTitle, 8);
|
|
|
|
}
|
2017-05-23 02:44:04 +02:00
|
|
|
|
2017-05-24 15:18:31 +02:00
|
|
|
moduleList[i].src = src;
|
|
|
|
srcModuleSize = moduleList[i].size = ((Cxi *)src)->ncch.contentSize * 0x200;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//3) Read or copy the modules
|
2017-05-23 02:44:04 +02:00
|
|
|
u8 *dst = firm->section[0].address;
|
2017-06-09 17:29:26 +02:00
|
|
|
const char *extModuleSizeError = "The external FIRM modules are too large.";
|
2017-06-26 19:03:57 +02:00
|
|
|
for(u32 i = 0, dstModuleSize, maxModuleSize = firmType == NATIVE_FIRM ? 0x80000 : 0x600000; i < nbModules; i++, dst += dstModuleSize, maxModuleSize -= dstModuleSize)
|
2017-05-23 02:44:04 +02:00
|
|
|
{
|
|
|
|
if(loadFromStorage)
|
|
|
|
{
|
|
|
|
char fileName[24];
|
|
|
|
|
|
|
|
//Read modules from files if they exist
|
|
|
|
sprintf(fileName, "sysmodules/%.8s.cxi", moduleList[i].name);
|
|
|
|
|
2017-05-24 15:18:31 +02:00
|
|
|
dstModuleSize = getFileSize(fileName);
|
2017-05-23 02:44:04 +02:00
|
|
|
|
|
|
|
if(dstModuleSize != 0)
|
|
|
|
{
|
2017-06-09 17:29:26 +02:00
|
|
|
if(dstModuleSize > maxModuleSize) error(extModuleSizeError);
|
2017-05-23 02:44:04 +02:00
|
|
|
|
|
|
|
if(dstModuleSize <= sizeof(Cxi) + 0x200 ||
|
|
|
|
fileRead(dst, fileName, dstModuleSize) != dstModuleSize ||
|
|
|
|
memcmp(((Cxi *)dst)->ncch.magic, "NCCH", 4) != 0 ||
|
2017-05-24 15:18:31 +02:00
|
|
|
memcmp(moduleList[i].name, ((Cxi *)dst)->exHeader.systemControlInfo.appTitle, sizeof(((Cxi *)dst)->exHeader.systemControlInfo.appTitle)) != 0)
|
2017-05-23 02:44:04 +02:00
|
|
|
error("An external FIRM module is invalid or corrupted.");
|
2017-05-24 15:18:31 +02:00
|
|
|
|
2017-06-09 17:29:26 +02:00
|
|
|
continue;
|
2017-05-23 02:44:04 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-24 15:18:31 +02:00
|
|
|
|
2017-06-09 17:29:26 +02:00
|
|
|
dstModuleSize = moduleList[i].size;
|
|
|
|
|
|
|
|
if(dstModuleSize > maxModuleSize) error(extModuleSizeError);
|
|
|
|
|
|
|
|
memcpy(dst, moduleList[i].src, dstModuleSize);
|
2017-05-23 02:44:04 +02:00
|
|
|
}
|
|
|
|
|
2017-05-24 15:18:31 +02:00
|
|
|
//4) Patch NATIVE_FIRM if necessary
|
2017-06-09 17:29:26 +02:00
|
|
|
if(nbModules == 6)
|
2017-05-23 02:44:04 +02:00
|
|
|
{
|
2017-05-26 04:48:58 +02:00
|
|
|
if(patchK11ModuleLoading(firm->section[0].size, dst - firm->section[0].address, (u8 *)firm + firm->section[1].offset, firm->section[1].size) != 0)
|
2017-05-23 02:44:04 +02:00
|
|
|
error("Failed to inject custom sysmodule");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-21 18:57:45 +02:00
|
|
|
u32 patchNativeFirm(u32 firmVersion, FirmwareSource nandType, bool loadFromStorage, bool isFirmProtEnabled, bool needToInitSd, bool doUnitinfoPatch)
|
2016-04-03 17:56:09 +02:00
|
|
|
{
|
2016-10-10 01:34:53 +02:00
|
|
|
u8 *arm9Section = (u8 *)firm + firm->section[2].offset,
|
|
|
|
*arm11Section1 = (u8 *)firm + firm->section[1].offset;
|
2016-05-27 14:08:06 +02:00
|
|
|
|
2016-10-10 01:34:53 +02:00
|
|
|
if(ISN3DS)
|
2016-04-11 05:15:44 +02:00
|
|
|
{
|
2020-04-25 14:17:23 +02:00
|
|
|
//Decrypt Arm9Bin and patch Arm9 entrypoint to skip kernel9loader
|
2016-10-08 14:23:08 +02:00
|
|
|
kernel9Loader((Arm9Bin *)arm9Section);
|
2016-04-11 05:15:44 +02:00
|
|
|
firm->arm9Entry = (u8 *)0x801B01C;
|
|
|
|
}
|
2017-06-16 22:37:04 +02:00
|
|
|
|
2016-05-12 15:27:22 +02:00
|
|
|
//Find the Process9 .code location, size and memory address
|
2016-05-11 19:28:28 +02:00
|
|
|
u32 process9Size,
|
|
|
|
process9MemAddr;
|
2016-10-10 13:29:34 +02:00
|
|
|
u8 *process9Offset = getProcess9Info(arm9Section, firm->section[2].size, &process9Size, &process9MemAddr);
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-10-10 01:34:53 +02:00
|
|
|
//Find the Kernel11 SVC table and handler, exceptions page and free space locations
|
2016-09-10 21:12:53 +02:00
|
|
|
u32 baseK11VA;
|
2016-08-29 15:35:24 +02:00
|
|
|
u8 *freeK11Space;
|
2016-09-18 18:31:16 +02:00
|
|
|
u32 *arm11SvcHandler,
|
2016-09-03 15:41:45 +02:00
|
|
|
*arm11ExceptionsPage,
|
2017-06-05 02:02:04 +02:00
|
|
|
*arm11SvcTable = getKernel11Info(arm11Section1, firm->section[1].size, &baseK11VA, &freeK11Space, &arm11SvcHandler, &arm11ExceptionsPage);
|
2016-10-10 01:34:53 +02:00
|
|
|
|
2016-10-15 18:05:46 +02:00
|
|
|
u32 kernel9Size = (u32)(process9Offset - arm9Section) - sizeof(Cxi) - 0x200,
|
2016-10-15 16:16:53 +02:00
|
|
|
ret = 0;
|
2016-08-29 15:35:24 +02:00
|
|
|
|
2017-06-19 18:13:59 +02:00
|
|
|
//Skip on FIRMs < 4.0
|
2017-06-26 17:10:24 +02:00
|
|
|
if(ISN3DS || firmVersion >= 0x1D)
|
2017-06-19 18:13:59 +02:00
|
|
|
{
|
2018-08-21 18:57:45 +02:00
|
|
|
ret += installK11Extension(arm11Section1, firm->section[1].size, needToInitSd, baseK11VA, arm11ExceptionsPage, &freeK11Space);
|
2017-06-19 18:13:59 +02:00
|
|
|
ret += patchKernel11(arm11Section1, firm->section[1].size, baseK11VA, arm11SvcTable, arm11ExceptionsPage);
|
|
|
|
}
|
2017-06-05 02:02:04 +02:00
|
|
|
|
2016-05-25 20:08:37 +02:00
|
|
|
//Apply signature patches
|
2016-10-10 01:34:53 +02:00
|
|
|
ret += patchSignatureChecks(process9Offset, process9Size);
|
2016-05-27 14:08:06 +02:00
|
|
|
|
2016-09-08 02:12:29 +02:00
|
|
|
//Apply EmuNAND patches
|
2017-06-19 18:13:59 +02:00
|
|
|
if(nandType != FIRMWARE_SYSNAND) ret += patchEmuNand(arm9Section, kernel9Size, process9Offset, process9Size, firm->section[2].address, firmVersion);
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-11-04 22:28:33 +01:00
|
|
|
//Apply FIRM0/1 writes patches on SysNAND to protect A9LH
|
2017-08-20 16:08:54 +02:00
|
|
|
else if(isFirmProtEnabled) ret += patchFirmWrites(process9Offset, process9Size);
|
2016-05-11 19:28:28 +02:00
|
|
|
|
2016-07-19 20:03:35 +02:00
|
|
|
//Apply firmlaunch patches
|
2016-10-15 00:32:00 +02:00
|
|
|
ret += patchFirmlaunches(process9Offset, process9Size, process9MemAddr);
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2016-11-10 13:17:01 +01:00
|
|
|
//Apply dev unit check patches related to NCCH encryption
|
2016-11-04 22:28:33 +01:00
|
|
|
if(!ISDEVUNIT)
|
|
|
|
{
|
|
|
|
ret += patchZeroKeyNcchEncryptionCheck(process9Offset, process9Size);
|
|
|
|
ret += patchNandNcchEncryptionCheck(process9Offset, process9Size);
|
|
|
|
}
|
|
|
|
|
2017-06-19 18:13:59 +02:00
|
|
|
//Apply anti-anti-DG patches on 11.0+
|
|
|
|
if(firmVersion >= (ISN3DS ? 0x21 : 0x52)) ret += patchTitleInstallMinVersionChecks(process9Offset, process9Size, firmVersion);
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2019-04-06 16:27:17 +02:00
|
|
|
//Patch P9 AM ticket wrapper on 11.8+ to use 0 Key and IV, only with UNITINFO patch on to prevent NIM from actually sending any
|
|
|
|
if(doUnitinfoPatch && firmVersion >= (ISN3DS ? 0x35 : 0x64)) ret += patchP9AMTicketWrapperZeroKeyIV(process9Offset, process9Size, firmVersion);
|
2018-09-10 20:57:14 +02:00
|
|
|
|
2016-11-10 13:17:01 +01:00
|
|
|
//Apply UNITINFO patches
|
2017-03-05 01:56:24 +01:00
|
|
|
if(doUnitinfoPatch)
|
2016-11-10 13:10:02 +01:00
|
|
|
{
|
|
|
|
ret += patchUnitInfoValueSet(arm9Section, kernel9Size);
|
2016-11-10 13:17:01 +01:00
|
|
|
if(!ISDEVUNIT) ret += patchCheckForDevCommonKey(process9Offset, process9Size);
|
2016-11-10 13:10:02 +01:00
|
|
|
}
|
2016-09-03 02:02:03 +02:00
|
|
|
|
2020-04-25 14:17:23 +02:00
|
|
|
//Arm9 exception handlers
|
2017-06-09 02:31:14 +02:00
|
|
|
ret += patchArm9ExceptionHandlersInstall(arm9Section, kernel9Size);
|
|
|
|
ret += patchSvcBreak9(arm9Section, kernel9Size, (u32)firm->section[2].address);
|
|
|
|
ret += patchKernel9Panic(arm9Section, kernel9Size);
|
2016-08-23 20:39:42 +02:00
|
|
|
|
2017-11-02 21:52:14 +01:00
|
|
|
ret += patchP9AccessChecks(process9Offset, process9Size);
|
2016-10-10 01:34:53 +02:00
|
|
|
|
2017-06-19 18:13:59 +02:00
|
|
|
mergeSection0(NATIVE_FIRM, firmVersion, loadFromStorage);
|
2017-05-23 02:44:04 +02:00
|
|
|
firm->section[0].size = 0;
|
|
|
|
|
2016-10-10 01:34:53 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
u32 patchTwlFirm(u32 firmVersion, bool loadFromStorage, bool doUnitinfoPatch)
|
2016-10-10 01:34:53 +02:00
|
|
|
{
|
|
|
|
u8 *arm9Section = (u8 *)firm + firm->section[3].offset;
|
2016-12-22 23:11:15 +01:00
|
|
|
|
2020-04-25 14:17:23 +02:00
|
|
|
//On N3DS, decrypt Arm9Bin and patch Arm9 entrypoint to skip kernel9loader
|
2016-10-10 01:34:53 +02:00
|
|
|
if(ISN3DS)
|
|
|
|
{
|
|
|
|
kernel9Loader((Arm9Bin *)arm9Section);
|
|
|
|
firm->arm9Entry = (u8 *)0x801301C;
|
2016-08-04 20:10:31 +02:00
|
|
|
}
|
2016-10-10 01:34:53 +02:00
|
|
|
|
2016-10-15 19:22:32 +02:00
|
|
|
//Find the Process9 .code location, size and memory address
|
|
|
|
u32 process9Size,
|
|
|
|
process9MemAddr;
|
|
|
|
u8 *process9Offset = getProcess9Info(arm9Section, firm->section[3].size, &process9Size, &process9MemAddr);
|
2016-10-10 02:51:44 +02:00
|
|
|
|
2016-10-15 19:22:32 +02:00
|
|
|
u32 kernel9Size = (u32)(process9Offset - arm9Section) - sizeof(Cxi) - 0x200,
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
ret += patchLgySignatureChecks(process9Offset, process9Size);
|
|
|
|
ret += patchTwlInvalidSignatureChecks(process9Offset, process9Size);
|
|
|
|
ret += patchTwlNintendoLogoChecks(process9Offset, process9Size);
|
|
|
|
ret += patchTwlWhitelistChecks(process9Offset, process9Size);
|
|
|
|
if(ISN3DS || firmVersion > 0x11) ret += patchTwlFlashcartChecks(process9Offset, process9Size, firmVersion);
|
|
|
|
else if(!ISN3DS && firmVersion == 0x11) ret += patchOldTwlFlashcartChecks(process9Offset, process9Size);
|
|
|
|
ret += patchTwlShaHashChecks(process9Offset, process9Size);
|
2016-10-10 01:34:53 +02:00
|
|
|
|
|
|
|
//Apply UNITINFO patch
|
2017-03-05 01:56:24 +01:00
|
|
|
if(doUnitinfoPatch) ret += patchUnitInfoValueSet(arm9Section, kernel9Size);
|
2016-10-10 01:34:53 +02:00
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
if(loadFromStorage)
|
|
|
|
{
|
2017-06-28 20:22:58 +02:00
|
|
|
mergeSection0(TWL_FIRM, 0, true);
|
2017-05-23 02:44:04 +02:00
|
|
|
firm->section[0].size = 0;
|
|
|
|
}
|
|
|
|
|
2016-10-10 01:34:53 +02:00
|
|
|
return ret;
|
2015-08-07 09:01:42 +02:00
|
|
|
}
|
|
|
|
|
2017-06-28 20:22:58 +02:00
|
|
|
u32 patchAgbFirm(bool loadFromStorage, bool doUnitinfoPatch)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-10-10 01:34:53 +02:00
|
|
|
u8 *arm9Section = (u8 *)firm + firm->section[3].offset;
|
2016-12-22 23:11:15 +01:00
|
|
|
|
2020-04-25 14:17:23 +02:00
|
|
|
//On N3DS, decrypt Arm9Bin and patch Arm9 entrypoint to skip kernel9loader
|
2016-10-10 01:34:53 +02:00
|
|
|
if(ISN3DS)
|
2016-05-25 14:34:43 +02:00
|
|
|
{
|
2016-10-08 14:23:08 +02:00
|
|
|
kernel9Loader((Arm9Bin *)arm9Section);
|
2016-05-25 14:34:43 +02:00
|
|
|
firm->arm9Entry = (u8 *)0x801301C;
|
|
|
|
}
|
2016-09-03 02:02:03 +02:00
|
|
|
|
2016-10-15 19:22:32 +02:00
|
|
|
//Find the Process9 .code location, size and memory address
|
|
|
|
u32 process9Size,
|
|
|
|
process9MemAddr;
|
|
|
|
u8 *process9Offset = getProcess9Info(arm9Section, firm->section[3].size, &process9Size, &process9MemAddr);
|
|
|
|
|
|
|
|
u32 kernel9Size = (u32)(process9Offset - arm9Section) - sizeof(Cxi) - 0x200,
|
|
|
|
ret = 0;
|
2016-10-10 02:51:44 +02:00
|
|
|
|
2016-10-15 19:22:32 +02:00
|
|
|
ret += patchLgySignatureChecks(process9Offset, process9Size);
|
|
|
|
if(CONFIG(SHOWGBABOOT)) ret += patchAgbBootSplash(process9Offset, process9Size);
|
2016-09-08 18:47:13 +02:00
|
|
|
|
2016-08-23 20:39:42 +02:00
|
|
|
//Apply UNITINFO patch
|
2017-03-05 01:56:24 +01:00
|
|
|
if(doUnitinfoPatch) ret += patchUnitInfoValueSet(arm9Section, kernel9Size);
|
2016-10-10 01:34:53 +02:00
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
if(loadFromStorage)
|
|
|
|
{
|
2017-06-28 20:22:58 +02:00
|
|
|
mergeSection0(AGB_FIRM, 0, true);
|
2017-05-23 02:44:04 +02:00
|
|
|
firm->section[0].size = 0;
|
|
|
|
}
|
|
|
|
|
2016-10-10 01:34:53 +02:00
|
|
|
return ret;
|
2016-06-04 21:11:15 +02:00
|
|
|
}
|
2016-04-02 17:58:06 +02:00
|
|
|
|
2017-06-09 02:31:14 +02:00
|
|
|
u32 patch1x2xNativeAndSafeFirm(void)
|
2016-05-10 23:06:32 +02:00
|
|
|
{
|
2016-10-10 01:34:53 +02:00
|
|
|
u8 *arm9Section = (u8 *)firm + firm->section[2].offset;
|
2016-05-11 19:28:28 +02:00
|
|
|
|
2016-10-10 01:34:53 +02:00
|
|
|
if(ISN3DS)
|
2016-05-11 19:28:28 +02:00
|
|
|
{
|
2020-04-25 14:17:23 +02:00
|
|
|
//Decrypt Arm9Bin and patch Arm9 entrypoint to skip kernel9loader
|
2016-10-08 14:23:08 +02:00
|
|
|
kernel9Loader((Arm9Bin *)arm9Section);
|
2016-05-25 14:34:43 +02:00
|
|
|
firm->arm9Entry = (u8 *)0x801B01C;
|
2016-05-11 19:28:28 +02:00
|
|
|
}
|
2016-10-15 19:22:32 +02:00
|
|
|
|
|
|
|
//Find the Process9 .code location, size and memory address
|
|
|
|
u32 process9Size,
|
|
|
|
process9MemAddr;
|
|
|
|
u8 *process9Offset = getProcess9Info(arm9Section, firm->section[2].size, &process9Size, &process9MemAddr);
|
|
|
|
|
|
|
|
u32 kernel9Size = (u32)(process9Offset - arm9Section) - sizeof(Cxi) - 0x200,
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
ret += ISN3DS ? patchFirmWrites(process9Offset, process9Size) : patchOldFirmWrites(process9Offset, process9Size);
|
2017-02-08 23:18:08 +01:00
|
|
|
|
2017-01-08 18:29:26 +01:00
|
|
|
ret += ISN3DS ? patchSignatureChecks(process9Offset, process9Size) : patchOldSignatureChecks(process9Offset, process9Size);
|
2016-08-23 20:39:42 +02:00
|
|
|
|
2020-04-25 14:17:23 +02:00
|
|
|
//Arm9 exception handlers
|
2017-06-09 02:31:14 +02:00
|
|
|
ret += patchArm9ExceptionHandlersInstall(arm9Section, kernel9Size);
|
|
|
|
ret += patchSvcBreak9(arm9Section, kernel9Size, (u32)firm->section[2].address);
|
2016-10-10 01:34:53 +02:00
|
|
|
|
|
|
|
return ret;
|
2016-05-10 23:06:32 +02:00
|
|
|
}
|
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
void launchFirm(int argc, char **argv)
|
2016-05-11 19:28:28 +02:00
|
|
|
{
|
2017-05-23 02:44:04 +02:00
|
|
|
prepareArm11ForFirmlaunch();
|
2018-05-22 17:51:57 +02:00
|
|
|
chainload(argc, argv, firm);
|
2016-12-22 23:11:15 +01:00
|
|
|
}
|