2015-08-05 03:57:37 +02:00
|
|
|
/*
|
2016-07-05 16:05:53 +02:00
|
|
|
* This file is part of Luma3DS
|
|
|
|
* Copyright (C) 2016 Aurora Wright, TuxSH
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
* Additional Terms 7.b of GPLv3 applies 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.
|
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-08-30 21:02:45 +02:00
|
|
|
#include "strings.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"
|
2016-09-23 02:06:04 +02:00
|
|
|
#include "../build/bundled.h"
|
2016-09-11 03:29:39 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
static inline bool loadFirmFromStorage(FirmwareType firmType)
|
2016-04-11 05:15:44 +02:00
|
|
|
{
|
2016-09-18 23:50:44 +02:00
|
|
|
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
|
|
|
},
|
|
|
|
*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
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
u32 firmSize = fileRead(firm, firmType == NATIVE_FIRM1X2X ? firmwareFiles[0] : firmwareFiles[(u32)firmType], 0x400000 + sizeof(Cxi) + 0x200);
|
|
|
|
|
|
|
|
if(!firmSize) return false;
|
|
|
|
|
|
|
|
if(firmSize <= sizeof(Cxi) + 0x200) error("The FIRM in /luma is not valid.");
|
|
|
|
|
|
|
|
if(memcmp(firm, "FIRM", 4) != 0)
|
|
|
|
{
|
|
|
|
u8 cetk[0xA50];
|
|
|
|
|
|
|
|
if(fileRead(cetk, firmType == NATIVE_FIRM1X2X ? cetkFiles[0] : cetkFiles[(u32)firmType], sizeof(cetk)) != sizeof(cetk) ||
|
|
|
|
!decryptNusFirm((Ticket *)(cetk + 0x140), (Cxi *)firm, firmSize))
|
|
|
|
error("The FIRM in /luma is encrypted or corrupted.");
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check that the FIRM is right for the console from the ARM9 section address
|
|
|
|
if((firm->section[3].offset != 0 ? firm->section[3].address : firm->section[2].address) != (ISN3DS ? (u8 *)0x8006000 : (u8 *)0x8006800))
|
|
|
|
error("The FIRM in /luma is not for this console.");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
static inline void mergeSection0(FirmwareType firmType, bool loadFromStorage)
|
|
|
|
{
|
2017-05-24 15:18:31 +02:00
|
|
|
u32 srcModuleSize;
|
2017-05-23 02:44:04 +02:00
|
|
|
const char *extModuleSizeError = "The external FIRM modules are too large.";
|
|
|
|
|
2017-05-24 15:18:31 +02:00
|
|
|
u32 nbModules = 0,
|
|
|
|
isCustomModule = false;
|
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-05-24 15:18:31 +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
|
|
|
|
|
|
|
if(firmType == NATIVE_FIRM)
|
2017-05-23 02:44:04 +02:00
|
|
|
{
|
2017-05-24 15:18:31 +02:00
|
|
|
//2) Merge that info with our own modules'
|
|
|
|
for(u8 *src = (u8 *)0x1FF60000; src < (u8 *)(0x1FF60000 + LUMA_SECTION0_SIZE); src += srcModuleSize)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
for(i = 0; i < nbModules && memcmp(name, moduleList[i].name, 8) != 0; i++);
|
2017-05-23 02:44:04 +02:00
|
|
|
|
2017-05-24 15:18:31 +02:00
|
|
|
if(i == nbModules) isCustomModule = true;
|
2017-05-23 02:44:04 +02:00
|
|
|
|
2017-05-24 15:18:31 +02:00
|
|
|
memcpy(moduleList[i].name, ((Cxi *)src)->exHeader.systemControlInfo.appTitle, 8);
|
|
|
|
moduleList[i].src = src;
|
|
|
|
srcModuleSize = moduleList[i].size = ((Cxi *)src)->ncch.contentSize * 0x200;
|
|
|
|
}
|
2017-05-23 02:44:04 +02:00
|
|
|
|
2017-05-24 15:18:31 +02:00
|
|
|
if(isCustomModule) nbModules++;
|
|
|
|
}
|
|
|
|
|
|
|
|
//3) Read or copy the modules
|
2017-05-23 02:44:04 +02:00
|
|
|
u8 *dst = firm->section[0].address;
|
2017-05-24 15:18:31 +02:00
|
|
|
for(u32 i = 0, dstModuleSize; i < nbModules; i++)
|
2017-05-23 02:44:04 +02:00
|
|
|
{
|
2017-05-24 15:18:31 +02:00
|
|
|
dstModuleSize = 0;
|
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-05-24 15:18:31 +02:00
|
|
|
if(dstModuleSize > 0x60000) 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-05-23 02:44:04 +02:00
|
|
|
dst += dstModuleSize;
|
|
|
|
}
|
|
|
|
}
|
2017-05-24 15:18:31 +02:00
|
|
|
|
|
|
|
if(!dstModuleSize)
|
2017-05-23 02:44:04 +02:00
|
|
|
{
|
|
|
|
memcpy(dst, moduleList[i].src, moduleList[i].size);
|
|
|
|
dst += moduleList[i].size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 15:18:31 +02:00
|
|
|
//4) Patch NATIVE_FIRM if necessary
|
|
|
|
if(isCustomModule)
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
u32 loadFirm(FirmwareType *firmType, FirmwareSource nandType, bool loadFromStorage, bool isSafeMode)
|
|
|
|
{
|
2016-09-08 22:33:25 +02:00
|
|
|
//Load FIRM from CTRNAND
|
2016-09-02 15:05:52 +02:00
|
|
|
u32 firmVersion = firmRead(firm, (u32)*firmType);
|
2016-09-08 22:33:25 +02:00
|
|
|
|
2016-10-10 18:56:19 +02:00
|
|
|
if(firmVersion == 0xFFFFFFFF) error("Failed to get the CTRNAND FIRM.");
|
2016-10-07 14:27:30 +02:00
|
|
|
|
2016-10-08 01:52:04 +02:00
|
|
|
bool mustLoadFromStorage = false;
|
2016-04-28 16:11:25 +02:00
|
|
|
|
2016-11-05 17:46:47 +01:00
|
|
|
if(!ISN3DS && *firmType == NATIVE_FIRM && !ISDEVUNIT)
|
2016-08-04 13:44:51 +02:00
|
|
|
{
|
2016-09-02 15:05:52 +02:00
|
|
|
if(firmVersion < 0x18)
|
2016-08-27 13:11:55 +02:00
|
|
|
{
|
2016-09-02 15:15:22 +02:00
|
|
|
//We can't boot < 3.x EmuNANDs
|
2016-12-22 23:11:15 +01:00
|
|
|
if(nandType != FIRMWARE_SYSNAND)
|
2016-09-22 22:35:17 +02:00
|
|
|
error("An old unsupported EmuNAND has been detected.\nLuma3DS is unable to boot it.");
|
2016-08-28 02:38:30 +02:00
|
|
|
|
2016-11-03 18:55:40 +01:00
|
|
|
if(isSafeMode) error("SAFE_MODE is not supported on 1.x/2.x FIRM.");
|
2016-08-27 00:34:25 +02:00
|
|
|
|
2016-09-02 15:05:52 +02:00
|
|
|
*firmType = NATIVE_FIRM1X2X;
|
2016-08-27 13:11:55 +02:00
|
|
|
}
|
2016-07-18 16:58:29 +02:00
|
|
|
|
2016-10-08 01:52:04 +02:00
|
|
|
//We can't boot a 3.x/4.x NATIVE_FIRM, load one from SD/CTRNAND
|
|
|
|
else if(firmVersion < 0x25) mustLoadFromStorage = true;
|
2016-09-02 15:05:52 +02:00
|
|
|
}
|
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
if((loadFromStorage || mustLoadFromStorage) && loadFirmFromStorage(*firmType)) firmVersion = 0xFFFFFFFF;
|
|
|
|
else
|
2016-09-02 15:05:52 +02:00
|
|
|
{
|
2016-10-08 01:52:04 +02:00
|
|
|
if(mustLoadFromStorage) error("An old unsupported FIRM has been detected.\nCopy a firmware.bin in /luma to boot.");
|
2016-10-10 01:34:53 +02:00
|
|
|
if(!decryptExeFs((Cxi *)firm)) error("The CTRNAND FIRM is corrupted.");
|
2016-11-04 22:28:33 +01:00
|
|
|
if(ISDEVUNIT) firmVersion = 0xFFFFFFFF;
|
2016-08-30 02:03:56 +02:00
|
|
|
}
|
2016-09-08 22:33:25 +02:00
|
|
|
|
2016-07-18 16:58:29 +02:00
|
|
|
return firmVersion;
|
2015-08-05 11:54:00 +02:00
|
|
|
}
|
|
|
|
|
2017-05-26 03:07:39 +02:00
|
|
|
u32 patchNativeFirm(u32 firmVersion, FirmwareSource nandType, bool loadFromStorage, bool isSafeMode, bool doUnitinfoPatch, bool enableExceptionHandlers)
|
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
|
|
|
{
|
2016-09-20 14:29:26 +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-05-17 13:02:01 +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-12-22 23:11:15 +01:00
|
|
|
*arm11DAbtHandler,
|
2016-09-03 15:41:45 +02:00
|
|
|
*arm11ExceptionsPage,
|
2016-12-22 23:11:15 +01:00
|
|
|
*arm11SvcTable = getKernel11Info(arm11Section1, firm->section[1].size, &baseK11VA, &freeK11Space, &arm11SvcHandler, &arm11DAbtHandler, &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
|
|
|
|
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-05-26 03:07:39 +02:00
|
|
|
if(nandType != FIRMWARE_SYSNAND) ret += patchEmuNand(arm9Section, kernel9Size, process9Offset, process9Size, firm->section[2].address);
|
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-05-17 14:53:29 +02:00
|
|
|
else 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);
|
|
|
|
}
|
|
|
|
|
2016-07-18 16:58:29 +02:00
|
|
|
//11.0 FIRM patches
|
2016-10-10 01:34:53 +02:00
|
|
|
if(firmVersion >= (ISN3DS ? 0x21 : 0x52))
|
2016-05-27 04:16:49 +02:00
|
|
|
{
|
2016-07-18 16:58:29 +02:00
|
|
|
//Apply anti-anti-DG patches
|
2016-10-10 01:34:53 +02:00
|
|
|
ret += patchTitleInstallMinVersionChecks(process9Offset, process9Size, firmVersion);
|
2016-05-27 04:16:49 +02:00
|
|
|
|
2016-08-03 14:13:26 +02:00
|
|
|
//Restore svcBackdoor
|
2016-10-10 03:05:23 +02:00
|
|
|
ret += reimplementSvcBackdoor(arm11Section1, arm11SvcTable, baseK11VA, &freeK11Space);
|
2016-05-27 14:50:30 +02:00
|
|
|
}
|
2016-04-03 17:56:09 +02:00
|
|
|
|
2017-04-13 03:38:36 +02:00
|
|
|
//Stub svc 0x59 on 11.3+ FIRMs
|
|
|
|
if(firmVersion >= (ISN3DS ? 0x2D : 0x5C)) ret += stubSvcRestrictGpuDma(arm11Section1, arm11SvcTable, baseK11VA);
|
2017-02-08 23:18:08 +01:00
|
|
|
|
2016-11-03 18:55:40 +01:00
|
|
|
ret += implementSvcGetCFWInfo(arm11Section1, arm11SvcTable, baseK11VA, &freeK11Space, isSafeMode);
|
2016-08-30 02:03:56 +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
|
|
|
|
2017-05-17 14:53:29 +02:00
|
|
|
if(enableExceptionHandlers)
|
2016-08-23 20:39:42 +02:00
|
|
|
{
|
2016-09-19 14:03:55 +02:00
|
|
|
//ARM11 exception handlers
|
|
|
|
u32 codeSetOffset,
|
2016-10-10 01:34:53 +02:00
|
|
|
stackAddress = getInfoForArm11ExceptionHandlers(arm11Section1, firm->section[1].size, &codeSetOffset);
|
2016-12-22 23:11:15 +01:00
|
|
|
ret += installArm11Handlers(arm11ExceptionsPage, stackAddress, codeSetOffset, arm11DAbtHandler, baseK11VA + ((u8 *)arm11DAbtHandler - arm11Section1));
|
2017-04-15 23:11:53 +02:00
|
|
|
patchSvcBreak11(arm11Section1, arm11SvcTable, baseK11VA);
|
2016-10-10 01:34:53 +02:00
|
|
|
ret += patchKernel11Panic(arm11Section1, firm->section[1].size);
|
2016-08-30 02:03:56 +02:00
|
|
|
|
2016-09-19 14:03:55 +02:00
|
|
|
//ARM9 exception handlers
|
2016-10-15 16:16:53 +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
|
|
|
}
|
|
|
|
|
2016-11-16 03:41:59 +01:00
|
|
|
bool patchAccess = CONFIG(PATCHACCESS),
|
|
|
|
patchGames = CONFIG(PATCHGAMES);
|
|
|
|
|
|
|
|
if(patchAccess || patchGames)
|
2016-08-04 20:10:31 +02:00
|
|
|
{
|
2016-11-16 03:41:59 +01:00
|
|
|
ret += patchK11ModuleChecks(arm11Section1, firm->section[1].size, &freeK11Space, patchGames);
|
|
|
|
|
|
|
|
if(patchAccess)
|
|
|
|
{
|
|
|
|
ret += patchArm11SvcAccessChecks(arm11SvcHandler, (u32 *)(arm11Section1 + firm->section[1].size));
|
|
|
|
ret += patchP9AccessChecks(process9Offset, process9Size);
|
|
|
|
}
|
2016-10-10 01:34:53 +02:00
|
|
|
}
|
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
mergeSection0(NATIVE_FIRM, loadFromStorage);
|
|
|
|
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
|
|
|
|
2016-10-10 01:34:53 +02:00
|
|
|
//On N3DS, decrypt ARM9Bin and patch ARM9 entrypoint to skip kernel9loader
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
mergeSection0(TWL_FIRM, true);
|
|
|
|
firm->section[0].size = 0;
|
|
|
|
}
|
|
|
|
|
2016-10-10 01:34:53 +02:00
|
|
|
return ret;
|
2015-08-07 09:01:42 +02:00
|
|
|
}
|
|
|
|
|
2017-05-23 02:44:04 +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
|
|
|
|
2016-09-20 14:29:26 +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)
|
|
|
|
{
|
|
|
|
mergeSection0(AGB_FIRM, true);
|
|
|
|
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-03-05 01:56:24 +01:00
|
|
|
u32 patch1x2xNativeAndSafeFirm(bool enableExceptionHandlers)
|
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
|
|
|
{
|
2016-09-20 14:29:26 +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
|
|
|
|
2017-03-05 01:56:24 +01:00
|
|
|
if(enableExceptionHandlers)
|
2016-08-23 20:39:42 +02:00
|
|
|
{
|
2016-09-19 14:03:55 +02:00
|
|
|
//ARM9 exception handlers
|
2016-10-15 19:22:32 +02:00
|
|
|
ret += patchArm9ExceptionHandlersInstall(arm9Section, kernel9Size);
|
|
|
|
ret += patchSvcBreak9(arm9Section, kernel9Size, (u32)firm->section[2].address);
|
2016-08-23 20:39:42 +02:00
|
|
|
}
|
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
|
|
|
static __attribute__((noinline)) bool overlaps(u32 as, u32 ae, u32 bs, u32 be)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2017-05-23 02:44:04 +02:00
|
|
|
if (as <= bs && bs <= ae)
|
|
|
|
return true;
|
|
|
|
else if (bs <= as && as <= be)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-12 14:48:52 +02:00
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
bool checkFirmPayload(void)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
return false;
|
2016-06-27 13:21:08 +02:00
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
u32 size = 0x200;
|
|
|
|
for(u32 i = 0; i < 4; i++)
|
|
|
|
size += firm->section[i].size;
|
2016-06-27 13:21:08 +02:00
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
bool arm9EpFound = false,
|
|
|
|
arm11EpFound = false;
|
2016-09-19 14:03:55 +02:00
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
for(u32 i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
__attribute__((aligned(4))) u8 hash[0x20];
|
2016-10-16 02:47:53 +02:00
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
FirmSection *section = &firm->section[i];
|
2016-10-16 04:03:00 +02:00
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
//Allow empty sections
|
|
|
|
if(section->size == 0)
|
|
|
|
continue;
|
2016-10-16 02:47:53 +02:00
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
if((section->offset < 0x200) ||
|
|
|
|
(section->address + section->size < section->address) || //Overflow check
|
|
|
|
((u32)section->address & 3) || (section->offset & 0x1FF) || (section->size & 0x1FF) || //Alignment check
|
2017-05-24 15:18:31 +02:00
|
|
|
(overlaps((u32)section->address, (u32)section->address + section->size, 0x01FF8000, 0x01FF8000 + 0x8000)) ||
|
2017-05-23 13:33:32 +02:00
|
|
|
(overlaps((u32)section->address, (u32)section->address + section->size, 0x1FFFFC00, 0x20000000)) ||
|
2017-05-23 02:44:04 +02:00
|
|
|
(overlaps((u32)section->address, (u32)section->address + section->size, (u32)firm + section->offset, (u32)firm + size)))
|
|
|
|
return false;
|
2016-09-19 14:03:55 +02:00
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
sha(hash, (u8 *)firm + section->offset, section->size, SHA_256_MODE);
|
2016-08-30 02:03:56 +02:00
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
if(memcmp(hash, section->hash, 0x20) != 0)
|
|
|
|
return false;
|
2016-08-30 02:03:56 +02:00
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
if(firm->arm9Entry >= section->address && firm->arm9Entry < (section->address + section->size))
|
|
|
|
arm9EpFound = true;
|
2016-10-16 02:47:53 +02:00
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
if(firm->arm11Entry >= section->address && firm->arm11Entry < (section->address + section->size))
|
|
|
|
arm11EpFound = true;
|
2016-06-27 13:21:08 +02:00
|
|
|
}
|
2017-05-23 02:44:04 +02:00
|
|
|
|
|
|
|
return arm9EpFound && (firm->arm11Entry == NULL || arm11EpFound);
|
2016-04-02 17:58:06 +02:00
|
|
|
}
|
2016-09-08 18:47:13 +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-24 15:18:31 +02:00
|
|
|
u32 *loaderAddress = (u32 *)0x01FF9000;
|
2015-08-05 03:57:37 +02:00
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
prepareArm11ForFirmlaunch();
|
2016-02-29 16:28:43 +01:00
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
memcpy(loaderAddress, loader_bin, loader_bin_size);
|
2016-03-11 15:08:05 +01:00
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
flushDCacheRange(loaderAddress, loader_bin_size);
|
|
|
|
flushICacheRange(loaderAddress, loader_bin_size);
|
2016-07-18 16:58:29 +02:00
|
|
|
|
2017-05-23 02:44:04 +02:00
|
|
|
((void (*)(int, char **, u32))loaderAddress)(argc, argv, 0x0000BEEF);
|
2016-12-22 23:11:15 +01:00
|
|
|
}
|