2018-05-01 05:15:48 +00:00
|
|
|
/*
|
2018-08-05 11:40:32 +00:00
|
|
|
* Copyright (c) 2018 naehrwert
|
|
|
|
* Copyright (c) 2018 st4rk
|
|
|
|
* Copyright (c) 2018 Ced2911
|
|
|
|
* Copyright (C) 2018 CTCaer
|
2018-09-09 22:44:04 +00:00
|
|
|
* Copyright (c) 2018 balika011
|
2018-08-05 11:40:32 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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/>.
|
|
|
|
*/
|
2018-05-01 05:15:48 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
2018-08-05 11:40:32 +00:00
|
|
|
|
2018-05-01 05:15:48 +00:00
|
|
|
#include "hos.h"
|
2018-08-13 08:58:24 +00:00
|
|
|
#include "../storage/sdmmc.h"
|
|
|
|
#include "../storage/nx_emmc.h"
|
|
|
|
#include "../soc/t210.h"
|
|
|
|
#include "../sec/se.h"
|
|
|
|
#include "../sec/se_t210.h"
|
|
|
|
#include "../soc/pmc.h"
|
|
|
|
#include "../soc/cluster.h"
|
|
|
|
#include "../mem/heap.h"
|
|
|
|
#include "../sec/tsec.h"
|
|
|
|
#include "../utils/util.h"
|
2018-05-01 05:15:48 +00:00
|
|
|
#include "pkg1.h"
|
|
|
|
#include "pkg2.h"
|
2018-08-13 08:58:24 +00:00
|
|
|
#include "../libs/fatfs/ff.h"
|
|
|
|
#include "../gfx/di.h"
|
|
|
|
#include "../config/config.h"
|
|
|
|
#include "../mem/mc.h"
|
2018-09-18 21:28:13 +00:00
|
|
|
#include "../soc/fuse.h"
|
2018-09-24 20:47:35 +00:00
|
|
|
#include "../utils/dirlist.h"
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2018-08-13 08:58:24 +00:00
|
|
|
#include "../gfx/gfx.h"
|
2018-05-01 05:15:48 +00:00
|
|
|
extern gfx_ctxt_t gfx_ctxt;
|
|
|
|
extern gfx_con_t gfx_con;
|
2018-08-13 23:30:44 +00:00
|
|
|
extern hekate_config h_cfg;
|
|
|
|
|
2018-06-23 04:04:41 +00:00
|
|
|
extern void sd_unmount();
|
2018-08-13 23:30:44 +00:00
|
|
|
|
2018-06-01 16:02:13 +00:00
|
|
|
//#define DPRINTF(...) gfx_printf(&gfx_con, __VA_ARGS__)
|
|
|
|
#define DPRINTF(...)
|
2018-05-13 01:13:17 +00:00
|
|
|
|
2018-06-08 11:07:25 +00:00
|
|
|
typedef struct _launch_ctxt_t
|
|
|
|
{
|
|
|
|
void *keyblob;
|
|
|
|
|
|
|
|
void *pkg1;
|
|
|
|
const pkg1_id_t *pkg1_id;
|
|
|
|
const pkg2_kernel_id_t *pkg2_kernel_id;
|
|
|
|
|
|
|
|
void *warmboot;
|
|
|
|
u32 warmboot_size;
|
|
|
|
void *secmon;
|
|
|
|
u32 secmon_size;
|
|
|
|
|
|
|
|
void *pkg2;
|
|
|
|
u32 pkg2_size;
|
|
|
|
|
|
|
|
void *kernel;
|
|
|
|
u32 kernel_size;
|
|
|
|
link_t kip1_list;
|
2018-07-22 20:22:10 +00:00
|
|
|
char* kip1_patches;
|
2018-06-08 11:07:25 +00:00
|
|
|
|
2018-08-13 09:12:53 +00:00
|
|
|
bool svcperm;
|
|
|
|
bool debugmode;
|
|
|
|
bool atmosphere;
|
2018-06-08 11:07:25 +00:00
|
|
|
} launch_ctxt_t;
|
|
|
|
|
|
|
|
typedef struct _merge_kip_t
|
|
|
|
{
|
|
|
|
void *kip1;
|
|
|
|
link_t link;
|
|
|
|
} merge_kip_t;
|
|
|
|
|
|
|
|
#define KB_FIRMWARE_VERSION_100_200 0
|
|
|
|
#define KB_FIRMWARE_VERSION_300 1
|
|
|
|
#define KB_FIRMWARE_VERSION_301 2
|
|
|
|
#define KB_FIRMWARE_VERSION_400 3
|
|
|
|
#define KB_FIRMWARE_VERSION_500 4
|
2018-09-18 00:08:05 +00:00
|
|
|
#define KB_FIRMWARE_VERSION_600 5
|
|
|
|
#define KB_FIRMWARE_VERSION_MAX KB_FIRMWARE_VERSION_600
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2018-07-10 13:59:03 +00:00
|
|
|
// Exosphère magic "XBC0".
|
2018-07-01 02:03:17 +00:00
|
|
|
#define MAGIC_EXOSPHERE 0x30434258
|
|
|
|
|
2018-06-15 11:28:27 +00:00
|
|
|
static const u8 keyblob_keyseeds[][0x10] = {
|
2018-05-01 05:15:48 +00:00
|
|
|
{ 0xDF, 0x20, 0x6F, 0x59, 0x44, 0x54, 0xEF, 0xDC, 0x70, 0x74, 0x48, 0x3B, 0x0D, 0xED, 0x9F, 0xD3 }, //1.0.0
|
|
|
|
{ 0x0C, 0x25, 0x61, 0x5D, 0x68, 0x4C, 0xEB, 0x42, 0x1C, 0x23, 0x79, 0xEA, 0x82, 0x25, 0x12, 0xAC }, //3.0.0
|
|
|
|
{ 0x33, 0x76, 0x85, 0xEE, 0x88, 0x4A, 0xAE, 0x0A, 0xC2, 0x8A, 0xFD, 0x7D, 0x63, 0xC0, 0x43, 0x3B }, //3.0.1
|
|
|
|
{ 0x2D, 0x1F, 0x48, 0x80, 0xED, 0xEC, 0xED, 0x3E, 0x3C, 0xF2, 0x48, 0xB5, 0x65, 0x7D, 0xF7, 0xBE }, //4.0.0
|
2018-09-18 00:08:05 +00:00
|
|
|
{ 0xBB, 0x5A, 0x01, 0xF9, 0x88, 0xAF, 0xF5, 0xFC, 0x6C, 0xFF, 0x07, 0x9E, 0x13, 0x3C, 0x39, 0x80 }, //5.0.0
|
|
|
|
{ 0xD8, 0xCC, 0xE1, 0x26, 0x6A, 0x35, 0x3F, 0xCC, 0x20, 0xF3, 0x2D, 0x3B, 0x51, 0x7D, 0xE9, 0xC0 } //6.0.0
|
2018-05-01 05:15:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const u8 cmac_keyseed[0x10] =
|
|
|
|
{ 0x59, 0xC7, 0xFB, 0x6F, 0xBE, 0x9B, 0xBE, 0x87, 0x65, 0x6B, 0x15, 0xC0, 0x53, 0x73, 0x36, 0xA5 };
|
|
|
|
|
2018-05-13 01:13:17 +00:00
|
|
|
static const u8 master_keyseed_retail[0x10] =
|
2018-05-01 05:15:48 +00:00
|
|
|
{ 0xD8, 0xA2, 0x41, 0x0A, 0xC6, 0xC5, 0x90, 0x01, 0xC6, 0x1D, 0x6A, 0x26, 0x7C, 0x51, 0x3F, 0x3C };
|
|
|
|
|
2018-05-13 01:13:17 +00:00
|
|
|
static const u8 console_keyseed[0x10] =
|
2018-05-01 05:15:48 +00:00
|
|
|
{ 0x4F, 0x02, 0x5F, 0x0E, 0xB6, 0x6D, 0x11, 0x0E, 0xDC, 0x32, 0x7D, 0x41, 0x86, 0xC2, 0xF4, 0x78 };
|
|
|
|
|
|
|
|
static const u8 key8_keyseed[] =
|
|
|
|
{ 0xFB, 0x8B, 0x6A, 0x9C, 0x79, 0x00, 0xC8, 0x49, 0xEF, 0xD2, 0x4D, 0x85, 0x4D, 0x30, 0xA0, 0xC7 };
|
|
|
|
|
2018-06-15 11:28:27 +00:00
|
|
|
static const u8 master_keyseed_4xx_5xx[0x10] =
|
2018-05-04 07:15:29 +00:00
|
|
|
{ 0x2D, 0xC1, 0xF4, 0x8D, 0xF3, 0x5B, 0x69, 0x33, 0x42, 0x10, 0xAC, 0x65, 0xDA, 0x90, 0x46, 0x66 };
|
|
|
|
|
2018-06-15 11:28:27 +00:00
|
|
|
static const u8 console_keyseed_4xx_5xx[0x10] =
|
2018-05-04 07:15:29 +00:00
|
|
|
{ 0x0C, 0x91, 0x09, 0xDB, 0x93, 0x93, 0x07, 0x81, 0x07, 0x3C, 0xC4, 0x16, 0x22, 0x7C, 0x6C, 0x28 };
|
|
|
|
|
|
|
|
|
2018-05-01 05:15:48 +00:00
|
|
|
static void _se_lock()
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i < 16; i++)
|
|
|
|
se_key_acc_ctrl(i, 0x15);
|
|
|
|
|
|
|
|
for (u32 i = 0; i < 2; i++)
|
|
|
|
se_rsa_acc_ctrl(i, 1);
|
|
|
|
|
2018-07-10 13:59:03 +00:00
|
|
|
SE(0x4) = 0; // Make this reg secure only.
|
|
|
|
SE(SE_KEY_TABLE_ACCESS_LOCK_OFFSET) = 0; // Make all key access regs secure only.
|
|
|
|
SE(SE_RSA_KEYTABLE_ACCESS_LOCK_OFFSET) = 0; // Make all RSA access regs secure only.
|
|
|
|
SE(SE_SECURITY_0) &= 0xFFFFFFFB; // Make access lock regs secure only.
|
2018-09-18 20:38:54 +00:00
|
|
|
memset((void *)IPATCH_BASE, 0, 13);
|
2018-09-09 22:44:04 +00:00
|
|
|
SB(SB_CSR) = 0x10; // Protected IROM enable.
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2018-07-10 13:59:03 +00:00
|
|
|
// This is useful for documenting the bits in the SE config registers, so we can keep it around.
|
2018-05-01 05:15:48 +00:00
|
|
|
/*gfx_printf(&gfx_con, "SE(SE_SECURITY_0) = %08X\n", SE(SE_SECURITY_0));
|
|
|
|
gfx_printf(&gfx_con, "SE(0x4) = %08X\n", SE(0x4));
|
|
|
|
gfx_printf(&gfx_con, "SE(SE_KEY_TABLE_ACCESS_LOCK_OFFSET) = %08X\n", SE(SE_KEY_TABLE_ACCESS_LOCK_OFFSET));
|
|
|
|
gfx_printf(&gfx_con, "SE(SE_RSA_KEYTABLE_ACCESS_LOCK_OFFSET) = %08X\n", SE(SE_RSA_KEYTABLE_ACCESS_LOCK_OFFSET));
|
|
|
|
for(u32 i = 0; i < 16; i++)
|
|
|
|
gfx_printf(&gfx_con, "%02X ", SE(SE_KEY_TABLE_ACCESS_REG_OFFSET + i * 4) & 0xFF);
|
|
|
|
gfx_putc(&gfx_con, '\n');
|
|
|
|
for(u32 i = 0; i < 2; i++)
|
|
|
|
gfx_printf(&gfx_con, "%02X ", SE(SE_RSA_KEYTABLE_ACCESS_REG_OFFSET + i * 4) & 0xFF);
|
|
|
|
gfx_putc(&gfx_con, '\n');
|
|
|
|
gfx_hexdump(&gfx_con, SE_BASE, (void *)SE_BASE, 0x400);*/
|
|
|
|
}
|
|
|
|
|
2018-09-24 20:22:19 +00:00
|
|
|
void _pmc_scratch_lock(u32 kb)
|
|
|
|
{
|
|
|
|
switch (kb)
|
|
|
|
{
|
|
|
|
case KB_FIRMWARE_VERSION_100_200:
|
|
|
|
case KB_FIRMWARE_VERSION_300:
|
|
|
|
case KB_FIRMWARE_VERSION_301:
|
|
|
|
PMC(APBDEV_PMC_SEC_DISABLE) = 0x7FFFF3;
|
|
|
|
PMC(APBDEV_PMC_SEC_DISABLE2) = 0xFFFFFFFF;
|
|
|
|
PMC(APBDEV_PMC_SEC_DISABLE3) = 0xFFAFFFFF;
|
|
|
|
PMC(APBDEV_PMC_SEC_DISABLE4) = 0xFFFFFFFF;
|
|
|
|
PMC(APBDEV_PMC_SEC_DISABLE5) = 0xFFFFFFFF;
|
|
|
|
PMC(APBDEV_PMC_SEC_DISABLE6) = 0xFFFFFFFF;
|
|
|
|
PMC(APBDEV_PMC_SEC_DISABLE7) = 0xFFFFFFFF;
|
|
|
|
PMC(APBDEV_PMC_SEC_DISABLE8) = 0xFFAAFFFF;
|
|
|
|
break;
|
|
|
|
case KB_FIRMWARE_VERSION_400:
|
|
|
|
case KB_FIRMWARE_VERSION_500:
|
|
|
|
case KB_FIRMWARE_VERSION_600:
|
|
|
|
default:
|
|
|
|
PMC(APBDEV_PMC_SEC_DISABLE2) |= 0x3FCFFFF;
|
|
|
|
PMC(APBDEV_PMC_SEC_DISABLE4) |= 0x3F3FFFFF;
|
|
|
|
PMC(APBDEV_PMC_SEC_DISABLE5) = 0xFFFFFFFF;
|
|
|
|
PMC(APBDEV_PMC_SEC_DISABLE6) |= 0xF3FFC00F;
|
|
|
|
PMC(APBDEV_PMC_SEC_DISABLE7) |= 0x3FFFFF;
|
|
|
|
PMC(APBDEV_PMC_SEC_DISABLE8) |= 0xFF;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-13 01:13:17 +00:00
|
|
|
int keygen(u8 *keyblob, u32 kb, void *tsec_fw)
|
2018-05-01 05:15:48 +00:00
|
|
|
{
|
2018-05-13 01:13:17 +00:00
|
|
|
u8 tmp[0x10];
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2018-06-15 11:28:27 +00:00
|
|
|
if (kb > KB_FIRMWARE_VERSION_MAX)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
se_key_acc_ctrl(13, 0x15);
|
|
|
|
se_key_acc_ctrl(14, 0x15);
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2018-07-10 13:59:03 +00:00
|
|
|
// Get TSEC key.
|
2018-05-01 05:15:48 +00:00
|
|
|
if (tsec_query(tmp, 1, tsec_fw) < 0)
|
|
|
|
return 0;
|
|
|
|
|
2018-06-15 11:28:27 +00:00
|
|
|
se_aes_key_set(13, tmp, 0x10);
|
2018-05-13 01:13:17 +00:00
|
|
|
|
2018-07-10 13:59:03 +00:00
|
|
|
// Derive keyblob keys from TSEC+SBK.
|
2018-06-15 11:28:27 +00:00
|
|
|
se_aes_crypt_block_ecb(13, 0, tmp, keyblob_keyseeds[0]);
|
|
|
|
se_aes_unwrap_key(15, 14, tmp);
|
|
|
|
se_aes_crypt_block_ecb(13, 0, tmp, keyblob_keyseeds[kb]);
|
|
|
|
se_aes_unwrap_key(13, 14, tmp);
|
2018-05-13 01:13:17 +00:00
|
|
|
|
2018-07-10 13:59:03 +00:00
|
|
|
// Clear SBK.
|
2018-06-15 11:28:27 +00:00
|
|
|
se_aes_key_clear(14);
|
2018-05-01 05:15:48 +00:00
|
|
|
|
|
|
|
//TODO: verify keyblob CMAC.
|
|
|
|
//se_aes_unwrap_key(11, 13, cmac_keyseed);
|
|
|
|
//se_aes_cmac(tmp, 0x10, 11, keyblob + 0x10, 0xA0);
|
|
|
|
//if (!memcmp(keyblob, tmp, 0x10))
|
|
|
|
// return 0;
|
|
|
|
|
2018-06-15 11:28:27 +00:00
|
|
|
se_aes_crypt_block_ecb(13, 0, tmp, cmac_keyseed);
|
|
|
|
se_aes_unwrap_key(11, 13, cmac_keyseed);
|
2018-05-04 07:15:29 +00:00
|
|
|
|
2018-07-10 13:59:03 +00:00
|
|
|
// Decrypt keyblob and set keyslots.
|
2018-06-15 11:28:27 +00:00
|
|
|
se_aes_crypt_ctr(13, keyblob + 0x20, 0x90, keyblob + 0x20, 0x90, keyblob + 0x10);
|
2018-08-05 11:40:32 +00:00
|
|
|
se_aes_key_set(11, keyblob + 0x20 + 0x80, 0x10); // Package1 key.
|
2018-06-15 11:28:27 +00:00
|
|
|
se_aes_key_set(12, keyblob + 0x20, 0x10);
|
|
|
|
se_aes_key_set(13, keyblob + 0x20, 0x10);
|
2018-05-04 07:15:29 +00:00
|
|
|
|
2018-06-15 11:28:27 +00:00
|
|
|
se_aes_crypt_block_ecb(12, 0, tmp, master_keyseed_retail);
|
2018-05-04 07:15:29 +00:00
|
|
|
|
2018-06-08 11:07:25 +00:00
|
|
|
switch (kb)
|
|
|
|
{
|
2018-08-05 11:40:32 +00:00
|
|
|
case KB_FIRMWARE_VERSION_100_200:
|
|
|
|
case KB_FIRMWARE_VERSION_300:
|
|
|
|
case KB_FIRMWARE_VERSION_301:
|
|
|
|
se_aes_unwrap_key(13, 15, console_keyseed);
|
|
|
|
se_aes_unwrap_key(12, 12, master_keyseed_retail);
|
|
|
|
break;
|
|
|
|
case KB_FIRMWARE_VERSION_400:
|
|
|
|
se_aes_unwrap_key(13, 15, console_keyseed_4xx_5xx);
|
|
|
|
se_aes_unwrap_key(15, 15, console_keyseed);
|
|
|
|
se_aes_unwrap_key(14, 12, master_keyseed_4xx_5xx);
|
|
|
|
se_aes_unwrap_key(12, 12, master_keyseed_retail);
|
|
|
|
break;
|
|
|
|
case KB_FIRMWARE_VERSION_500:
|
2018-09-18 00:08:05 +00:00
|
|
|
case KB_FIRMWARE_VERSION_600:
|
2018-09-18 21:28:13 +00:00
|
|
|
default:
|
2018-08-05 11:40:32 +00:00
|
|
|
se_aes_unwrap_key(10, 15, console_keyseed_4xx_5xx);
|
|
|
|
se_aes_unwrap_key(15, 15, console_keyseed);
|
|
|
|
se_aes_unwrap_key(14, 12, master_keyseed_4xx_5xx);
|
|
|
|
se_aes_unwrap_key(12, 12, master_keyseed_retail);
|
|
|
|
break;
|
2018-05-04 07:15:29 +00:00
|
|
|
}
|
2018-05-13 01:13:17 +00:00
|
|
|
|
2018-07-10 13:59:03 +00:00
|
|
|
// Package2 key.
|
2018-06-15 11:28:27 +00:00
|
|
|
se_key_acc_ctrl(8, 0x15);
|
|
|
|
se_aes_unwrap_key(8, 12, key8_keyseed);
|
2018-06-08 09:42:24 +00:00
|
|
|
|
|
|
|
return 1;
|
2018-05-01 05:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _read_emmc_pkg1(launch_ctxt_t *ctxt)
|
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
sdmmc_storage_t storage;
|
|
|
|
sdmmc_t sdmmc;
|
|
|
|
|
|
|
|
sdmmc_storage_init_mmc(&storage, &sdmmc, SDMMC_4, SDMMC_BUS_WIDTH_8, 4);
|
|
|
|
|
2018-07-10 13:59:03 +00:00
|
|
|
// Read package1.
|
2018-05-01 05:15:48 +00:00
|
|
|
ctxt->pkg1 = (u8 *)malloc(0x40000);
|
|
|
|
sdmmc_storage_set_mmc_partition(&storage, 1);
|
|
|
|
sdmmc_storage_read(&storage, 0x100000 / NX_EMMC_BLOCKSIZE, 0x40000 / NX_EMMC_BLOCKSIZE, ctxt->pkg1);
|
|
|
|
ctxt->pkg1_id = pkg1_identify(ctxt->pkg1);
|
|
|
|
if (!ctxt->pkg1_id)
|
|
|
|
{
|
2018-07-10 13:59:03 +00:00
|
|
|
gfx_printf(&gfx_con, "%kUnknown package1,\nVersion (= '%s').%k\n", 0xFFFF0000, (char *)ctxt->pkg1 + 0x10, 0xFFCCCCCC);
|
2018-05-01 05:15:48 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2018-06-18 06:04:13 +00:00
|
|
|
gfx_printf(&gfx_con, "Identified package1 ('%s'),\nKeyblob version %d\n\n", (char *)(ctxt->pkg1 + 0x10), ctxt->pkg1_id->kb);
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2018-07-10 13:59:03 +00:00
|
|
|
// Read the correct keyblob.
|
2018-07-07 15:36:18 +00:00
|
|
|
ctxt->keyblob = (u8 *)calloc(NX_EMMC_BLOCKSIZE, 1);
|
2018-05-01 05:15:48 +00:00
|
|
|
sdmmc_storage_read(&storage, 0x180000 / NX_EMMC_BLOCKSIZE + ctxt->pkg1_id->kb, 1, ctxt->keyblob);
|
|
|
|
|
|
|
|
res = 1;
|
|
|
|
|
|
|
|
out:;
|
|
|
|
sdmmc_storage_end(&storage);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-09-18 21:28:13 +00:00
|
|
|
static u8 *_read_emmc_pkg2(launch_ctxt_t *ctxt)
|
2018-05-01 05:15:48 +00:00
|
|
|
{
|
2018-09-18 21:28:13 +00:00
|
|
|
u8 *bctBuf = NULL;
|
2018-05-01 05:15:48 +00:00
|
|
|
sdmmc_storage_t storage;
|
|
|
|
sdmmc_t sdmmc;
|
|
|
|
|
2018-09-18 21:28:13 +00:00
|
|
|
if (!sdmmc_storage_init_mmc(&storage, &sdmmc, SDMMC_4, SDMMC_BUS_WIDTH_8, 4))
|
|
|
|
return NULL;
|
2018-05-01 05:15:48 +00:00
|
|
|
sdmmc_storage_set_mmc_partition(&storage, 0);
|
|
|
|
|
2018-07-10 13:59:03 +00:00
|
|
|
// Parse eMMC GPT.
|
2018-05-01 05:15:48 +00:00
|
|
|
LIST_INIT(gpt);
|
|
|
|
nx_emmc_gpt_parse(&gpt, &storage);
|
2018-06-01 16:02:13 +00:00
|
|
|
DPRINTF("Parsed GPT\n");
|
2018-07-10 13:59:03 +00:00
|
|
|
// Find package2 partition.
|
2018-05-01 05:15:48 +00:00
|
|
|
emmc_part_t *pkg2_part = nx_emmc_part_find(&gpt, "BCPKG2-1-Normal-Main");
|
|
|
|
if (!pkg2_part)
|
|
|
|
goto out;
|
|
|
|
|
2018-07-10 13:59:03 +00:00
|
|
|
// Read in package2 header and get package2 real size.
|
2018-05-01 05:15:48 +00:00
|
|
|
//TODO: implement memalign for DMA buffers.
|
2018-09-18 21:28:13 +00:00
|
|
|
static const u32 BCT_SIZE = 0x4000;
|
|
|
|
bctBuf = (u8 *)malloc(BCT_SIZE);
|
|
|
|
nx_emmc_part_read(&storage, pkg2_part, BCT_SIZE / NX_EMMC_BLOCKSIZE, 1, bctBuf);
|
|
|
|
u32 *hdr = (u32 *)(bctBuf + 0x100);
|
2018-05-01 05:15:48 +00:00
|
|
|
u32 pkg2_size = hdr[0] ^ hdr[2] ^ hdr[3];
|
2018-06-01 16:02:13 +00:00
|
|
|
DPRINTF("pkg2 size on emmc is %08X\n", pkg2_size);
|
2018-09-18 21:28:13 +00:00
|
|
|
|
|
|
|
// Read in Boot Config.
|
|
|
|
memset(bctBuf, 0, BCT_SIZE);
|
|
|
|
nx_emmc_part_read(&storage, pkg2_part, 0, BCT_SIZE / NX_EMMC_BLOCKSIZE, bctBuf);
|
|
|
|
|
2018-08-05 11:40:32 +00:00
|
|
|
// Read in package2.
|
2018-05-01 05:15:48 +00:00
|
|
|
u32 pkg2_size_aligned = ALIGN(pkg2_size, NX_EMMC_BLOCKSIZE);
|
2018-06-01 16:02:13 +00:00
|
|
|
DPRINTF("pkg2 size aligned is %08X\n", pkg2_size_aligned);
|
2018-05-01 05:15:48 +00:00
|
|
|
ctxt->pkg2 = malloc(pkg2_size_aligned);
|
|
|
|
ctxt->pkg2_size = pkg2_size;
|
2018-09-18 21:28:13 +00:00
|
|
|
nx_emmc_part_read(&storage, pkg2_part, BCT_SIZE / NX_EMMC_BLOCKSIZE,
|
2018-05-01 05:15:48 +00:00
|
|
|
pkg2_size_aligned / NX_EMMC_BLOCKSIZE, ctxt->pkg2);
|
|
|
|
out:;
|
|
|
|
nx_emmc_gpt_free(&gpt);
|
|
|
|
sdmmc_storage_end(&storage);
|
2018-09-18 21:28:13 +00:00
|
|
|
return bctBuf;
|
2018-05-01 05:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _config_warmboot(launch_ctxt_t *ctxt, const char *value)
|
|
|
|
{
|
|
|
|
FIL fp;
|
|
|
|
if (f_open(&fp, value, FA_READ) != FR_OK)
|
|
|
|
return 0;
|
|
|
|
ctxt->warmboot_size = f_size(&fp);
|
|
|
|
ctxt->warmboot = malloc(ctxt->warmboot_size);
|
|
|
|
f_read(&fp, ctxt->warmboot, ctxt->warmboot_size, NULL);
|
|
|
|
f_close(&fp);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _config_secmon(launch_ctxt_t *ctxt, const char *value)
|
|
|
|
{
|
|
|
|
FIL fp;
|
|
|
|
if (f_open(&fp, value, FA_READ) != FR_OK)
|
|
|
|
return 0;
|
|
|
|
ctxt->secmon_size = f_size(&fp);
|
|
|
|
ctxt->secmon = malloc(ctxt->secmon_size);
|
|
|
|
f_read(&fp, ctxt->secmon, ctxt->secmon_size, NULL);
|
|
|
|
f_close(&fp);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _config_kernel(launch_ctxt_t *ctxt, const char *value)
|
|
|
|
{
|
|
|
|
FIL fp;
|
|
|
|
if (f_open(&fp, value, FA_READ) != FR_OK)
|
|
|
|
return 0;
|
|
|
|
ctxt->kernel_size = f_size(&fp);
|
|
|
|
ctxt->kernel = malloc(ctxt->kernel_size);
|
|
|
|
f_read(&fp, ctxt->kernel, ctxt->kernel_size, NULL);
|
|
|
|
f_close(&fp);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _config_kip1(launch_ctxt_t *ctxt, const char *value)
|
|
|
|
{
|
|
|
|
FIL fp;
|
2018-09-24 20:47:35 +00:00
|
|
|
|
|
|
|
if (!memcmp(value + strlen(value) - 1, "*", 1))
|
|
|
|
{
|
|
|
|
char *dir = (char *)malloc(256);
|
|
|
|
memcpy(dir, value, strlen(value) + 1);
|
|
|
|
|
|
|
|
u32 dirlen = 0;
|
|
|
|
dir[strlen(dir) - 2] = 0;
|
2018-11-09 00:09:14 +00:00
|
|
|
char *filelist = dirlist(dir, "*.kip*", false);
|
2018-09-24 20:47:35 +00:00
|
|
|
|
|
|
|
memcpy(dir + strlen(dir), "/", 2);
|
|
|
|
dirlen = strlen(dir);
|
|
|
|
|
|
|
|
u32 i = 0;
|
|
|
|
if (filelist)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (!filelist[i * 256])
|
|
|
|
break;
|
|
|
|
|
|
|
|
memcpy(dir + dirlen, &filelist[i * 256], strlen(&filelist[i * 256]) + 1);
|
|
|
|
if (f_open(&fp, dir, FA_READ))
|
|
|
|
{
|
|
|
|
free(dir);
|
|
|
|
free(filelist);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
merge_kip_t *mkip1 = (merge_kip_t *)malloc(sizeof(merge_kip_t));
|
|
|
|
mkip1->kip1 = malloc(f_size(&fp));
|
|
|
|
f_read(&fp, mkip1->kip1, f_size(&fp), NULL);
|
|
|
|
DPRINTF("Loaded kip1 from SD (size %08X)\n", f_size(&fp));
|
|
|
|
f_close(&fp);
|
|
|
|
list_append(&ctxt->kip1_list, &mkip1->link);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(dir);
|
|
|
|
free(filelist);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (f_open(&fp, value, FA_READ))
|
|
|
|
return 0;
|
|
|
|
merge_kip_t *mkip1 = (merge_kip_t *)malloc(sizeof(merge_kip_t));
|
|
|
|
mkip1->kip1 = malloc(f_size(&fp));
|
|
|
|
f_read(&fp, mkip1->kip1, f_size(&fp), NULL);
|
|
|
|
DPRINTF("Loaded kip1 from SD (size %08X)\n", f_size(&fp));
|
|
|
|
f_close(&fp);
|
|
|
|
list_append(&ctxt->kip1_list, &mkip1->link);
|
|
|
|
}
|
|
|
|
|
2018-05-01 05:15:48 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-06-01 16:22:11 +00:00
|
|
|
static int _config_svcperm(launch_ctxt_t *ctxt, const char *value)
|
|
|
|
{
|
2018-06-08 11:07:25 +00:00
|
|
|
if (*value == '1')
|
2018-06-01 16:22:11 +00:00
|
|
|
{
|
|
|
|
DPRINTF("Disabled SVC verification\n");
|
2018-08-13 09:12:53 +00:00
|
|
|
ctxt->svcperm = true;
|
2018-06-01 16:22:11 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _config_debugmode(launch_ctxt_t *ctxt, const char *value)
|
|
|
|
{
|
2018-06-08 11:07:25 +00:00
|
|
|
if (*value == '1')
|
2018-06-01 16:22:11 +00:00
|
|
|
{
|
|
|
|
DPRINTF("Enabled Debug mode\n");
|
2018-08-13 09:12:53 +00:00
|
|
|
ctxt->debugmode = true;
|
2018-06-01 16:22:11 +00:00
|
|
|
}
|
2018-06-08 09:42:24 +00:00
|
|
|
return 1;
|
2018-06-01 16:22:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 02:03:17 +00:00
|
|
|
static int _config_atmosphere(launch_ctxt_t *ctxt, const char *value)
|
|
|
|
{
|
|
|
|
if (*value == '1')
|
|
|
|
{
|
|
|
|
DPRINTF("Enabled atmosphere patching\n");
|
2018-08-13 09:12:53 +00:00
|
|
|
ctxt->atmosphere = true;
|
2018-07-01 02:03:17 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-07-22 20:22:10 +00:00
|
|
|
static int _config_kip1patch(launch_ctxt_t *ctxt, const char *value)
|
|
|
|
{
|
|
|
|
if (value == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int valueLen = strlen(value);
|
2018-08-21 01:37:40 +00:00
|
|
|
if (!valueLen)
|
2018-07-22 20:22:10 +00:00
|
|
|
return 0;
|
2018-08-05 11:40:32 +00:00
|
|
|
|
2018-07-22 20:22:10 +00:00
|
|
|
if (ctxt->kip1_patches == NULL)
|
|
|
|
{
|
2018-08-05 11:40:32 +00:00
|
|
|
ctxt->kip1_patches = malloc(valueLen + 1);
|
2018-07-22 20:22:10 +00:00
|
|
|
memcpy(ctxt->kip1_patches, value, valueLen);
|
|
|
|
ctxt->kip1_patches[valueLen] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-05 11:40:32 +00:00
|
|
|
char *oldAlloc = ctxt->kip1_patches;
|
2018-07-22 20:22:10 +00:00
|
|
|
int oldSize = strlen(oldAlloc);
|
2018-08-05 11:40:32 +00:00
|
|
|
ctxt->kip1_patches = malloc(oldSize + 1 + valueLen + 1);
|
2018-07-22 20:22:10 +00:00
|
|
|
memcpy(ctxt->kip1_patches, oldAlloc, oldSize);
|
2018-08-05 11:40:32 +00:00
|
|
|
free(oldAlloc);
|
|
|
|
oldAlloc = NULL;
|
2018-07-22 20:22:10 +00:00
|
|
|
ctxt->kip1_patches[oldSize++] = ',';
|
|
|
|
memcpy(&ctxt->kip1_patches[oldSize], value, valueLen);
|
2018-08-05 11:40:32 +00:00
|
|
|
ctxt->kip1_patches[oldSize + valueLen] = 0;
|
2018-07-22 20:22:10 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-05-01 05:15:48 +00:00
|
|
|
typedef struct _cfg_handler_t
|
|
|
|
{
|
|
|
|
const char *key;
|
|
|
|
int (*handler)(launch_ctxt_t *ctxt, const char *value);
|
|
|
|
} cfg_handler_t;
|
|
|
|
|
|
|
|
static const cfg_handler_t _config_handlers[] = {
|
|
|
|
{ "warmboot", _config_warmboot },
|
|
|
|
{ "secmon", _config_secmon },
|
|
|
|
{ "kernel", _config_kernel },
|
|
|
|
{ "kip1", _config_kip1 },
|
2018-07-22 20:22:10 +00:00
|
|
|
{ "kip1patch", _config_kip1patch },
|
2018-06-01 16:22:11 +00:00
|
|
|
{ "fullsvcperm", _config_svcperm },
|
|
|
|
{ "debugmode", _config_debugmode },
|
2018-07-01 02:03:17 +00:00
|
|
|
{ "atmosphere", _config_atmosphere },
|
2018-05-01 05:15:48 +00:00
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static int _config(launch_ctxt_t *ctxt, ini_sec_t *cfg)
|
|
|
|
{
|
|
|
|
LIST_FOREACH_ENTRY(ini_kv_t, kv, &cfg->kvs, link)
|
2018-08-21 01:37:40 +00:00
|
|
|
{
|
2018-05-01 05:15:48 +00:00
|
|
|
for(u32 i = 0; _config_handlers[i].key; i++)
|
2018-08-21 01:37:40 +00:00
|
|
|
{
|
|
|
|
if (!strcmp(_config_handlers[i].key, kv->key))
|
|
|
|
if (!_config_handlers[i].handler(ctxt, kv->val))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-01 05:15:48 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-07-07 15:36:18 +00:00
|
|
|
static void _free_launch_components(launch_ctxt_t *ctxt)
|
|
|
|
{
|
|
|
|
free(ctxt->keyblob);
|
|
|
|
free(ctxt->pkg1);
|
|
|
|
free(ctxt->pkg2);
|
|
|
|
free(ctxt->warmboot);
|
|
|
|
free(ctxt->secmon);
|
|
|
|
free(ctxt->kernel);
|
2018-07-22 20:22:10 +00:00
|
|
|
free(ctxt->kip1_patches);
|
2018-07-07 15:36:18 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 05:15:48 +00:00
|
|
|
int hos_launch(ini_sec_t *cfg)
|
|
|
|
{
|
2018-07-01 02:03:17 +00:00
|
|
|
int bootStateDramPkg2 = 0;
|
|
|
|
int bootStatePkg2Continue = 0;
|
|
|
|
int exoFwNumber = 0;
|
2018-05-01 05:15:48 +00:00
|
|
|
launch_ctxt_t ctxt;
|
2018-06-08 11:07:25 +00:00
|
|
|
|
2018-05-01 05:15:48 +00:00
|
|
|
memset(&ctxt, 0, sizeof(launch_ctxt_t));
|
|
|
|
list_init(&ctxt.kip1_list);
|
|
|
|
|
2018-06-23 04:07:53 +00:00
|
|
|
if (!gfx_con.mute)
|
|
|
|
gfx_clear_grey(&gfx_ctxt, 0x1B);
|
2018-06-03 06:07:03 +00:00
|
|
|
gfx_con_setpos(&gfx_con, 0, 0);
|
|
|
|
|
2018-07-01 02:03:17 +00:00
|
|
|
// Try to parse config if present.
|
2018-05-01 05:15:48 +00:00
|
|
|
if (cfg && !_config(&ctxt, cfg))
|
|
|
|
return 0;
|
2018-06-08 11:07:25 +00:00
|
|
|
|
2018-06-03 06:07:03 +00:00
|
|
|
gfx_printf(&gfx_con, "Initializing...\n\n");
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2018-07-01 02:03:17 +00:00
|
|
|
// Read package1 and the correct keyblob.
|
2018-05-01 05:15:48 +00:00
|
|
|
if (!_read_emmc_pkg1(&ctxt))
|
|
|
|
return 0;
|
|
|
|
|
2018-06-01 16:02:13 +00:00
|
|
|
gfx_printf(&gfx_con, "Loaded package1 and keyblob\n");
|
2018-06-08 11:07:25 +00:00
|
|
|
|
2018-07-01 02:03:17 +00:00
|
|
|
// Generate keys.
|
2018-07-09 13:02:47 +00:00
|
|
|
if (!h_cfg.se_keygen_done)
|
2018-07-07 15:36:18 +00:00
|
|
|
{
|
|
|
|
keygen(ctxt.keyblob, ctxt.pkg1_id->kb, (u8 *)ctxt.pkg1 + ctxt.pkg1_id->tsec_off);
|
2018-07-09 13:02:47 +00:00
|
|
|
h_cfg.se_keygen_done = 1;
|
2018-07-07 15:36:18 +00:00
|
|
|
DPRINTF("Generated keys\n");
|
|
|
|
}
|
2018-06-08 11:07:25 +00:00
|
|
|
|
2018-07-01 02:03:17 +00:00
|
|
|
// Decrypt and unpack package1 if we require parts of it.
|
2018-05-01 05:15:48 +00:00
|
|
|
if (!ctxt.warmboot || !ctxt.secmon)
|
|
|
|
{
|
|
|
|
pkg1_decrypt(ctxt.pkg1_id, ctxt.pkg1);
|
2018-06-04 22:55:32 +00:00
|
|
|
pkg1_unpack((void *)ctxt.pkg1_id->warmboot_base, (void *)ctxt.pkg1_id->secmon_base, NULL, ctxt.pkg1_id, ctxt.pkg1);
|
2018-06-01 16:02:13 +00:00
|
|
|
gfx_printf(&gfx_con, "Decrypted and unpacked package1\n");
|
2018-05-01 05:15:48 +00:00
|
|
|
}
|
2018-06-08 11:07:25 +00:00
|
|
|
|
2018-07-01 02:03:17 +00:00
|
|
|
// Replace 'warmboot.bin' if requested.
|
2018-05-01 05:15:48 +00:00
|
|
|
if (ctxt.warmboot)
|
2018-06-04 02:44:40 +00:00
|
|
|
memcpy((void *)ctxt.pkg1_id->warmboot_base, ctxt.warmboot, ctxt.warmboot_size);
|
2018-07-01 02:03:17 +00:00
|
|
|
// Set warmboot address in PMC if required.
|
2018-06-04 02:44:40 +00:00
|
|
|
if (ctxt.pkg1_id->set_warmboot)
|
2018-06-11 22:30:19 +00:00
|
|
|
PMC(APBDEV_PMC_SCRATCH1) = ctxt.pkg1_id->warmboot_base;
|
2018-06-08 11:07:25 +00:00
|
|
|
|
2018-07-01 02:03:17 +00:00
|
|
|
// Replace 'SecureMonitor' if requested.
|
2018-06-08 11:07:25 +00:00
|
|
|
if (ctxt.secmon)
|
2018-05-01 05:15:48 +00:00
|
|
|
memcpy((void *)ctxt.pkg1_id->secmon_base, ctxt.secmon, ctxt.secmon_size);
|
|
|
|
else
|
|
|
|
{
|
2018-07-01 02:03:17 +00:00
|
|
|
// Else we patch it to allow for an unsigned package2 and patched kernel.
|
2018-05-01 05:15:48 +00:00
|
|
|
patch_t *secmon_patchset = ctxt.pkg1_id->secmon_patchset;
|
2018-06-19 13:53:41 +00:00
|
|
|
gfx_printf(&gfx_con, "%kPatching Security Monitor%k\n", 0xFFFFBA00, 0xFFCCCCCC);
|
2018-06-08 11:07:25 +00:00
|
|
|
for (u32 i = 0; secmon_patchset[i].off != 0xFFFFFFFF; i++)
|
|
|
|
*(vu32 *)(ctxt.pkg1_id->secmon_base + secmon_patchset[i].off) = secmon_patchset[i].val;
|
|
|
|
}
|
2018-06-04 23:04:08 +00:00
|
|
|
|
2018-06-08 11:07:25 +00:00
|
|
|
gfx_printf(&gfx_con, "Loaded warmboot.bin and secmon\n");
|
2018-05-13 01:13:17 +00:00
|
|
|
|
2018-07-01 02:03:17 +00:00
|
|
|
// Read package2.
|
2018-09-18 21:28:13 +00:00
|
|
|
u8 *bootConfigBuf = _read_emmc_pkg2(&ctxt);
|
|
|
|
if (!bootConfigBuf)
|
2018-06-08 11:07:25 +00:00
|
|
|
return 0;
|
2018-05-13 01:13:17 +00:00
|
|
|
|
2018-06-08 11:07:25 +00:00
|
|
|
gfx_printf(&gfx_con, "Read package2\n");
|
2018-05-13 01:13:17 +00:00
|
|
|
|
2018-07-01 02:03:17 +00:00
|
|
|
// Decrypt package2 and parse KIP1 blobs in INI1 section.
|
2018-06-08 11:07:25 +00:00
|
|
|
pkg2_hdr_t *pkg2_hdr = pkg2_decrypt(ctxt.pkg2);
|
2018-05-13 01:13:17 +00:00
|
|
|
|
2018-06-08 11:07:25 +00:00
|
|
|
LIST_INIT(kip1_info);
|
|
|
|
pkg2_parse_kips(&kip1_info, pkg2_hdr);
|
|
|
|
|
|
|
|
gfx_printf(&gfx_con, "Parsed ini1\n");
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2018-07-01 02:03:17 +00:00
|
|
|
// Use the kernel included in package2 in case we didn't load one already.
|
2018-06-08 11:07:25 +00:00
|
|
|
if (!ctxt.kernel)
|
|
|
|
{
|
|
|
|
ctxt.kernel = pkg2_hdr->data;
|
|
|
|
ctxt.kernel_size = pkg2_hdr->sec_size[PKG2_SEC_KERNEL];
|
|
|
|
|
2018-07-01 02:03:17 +00:00
|
|
|
if (ctxt.svcperm || ctxt.debugmode || ctxt.atmosphere)
|
2018-06-08 11:07:25 +00:00
|
|
|
{
|
2018-06-09 12:05:29 +00:00
|
|
|
u32 kernel_crc32 = crc32c(ctxt.kernel, ctxt.kernel_size);
|
2018-06-08 11:07:25 +00:00
|
|
|
ctxt.pkg2_kernel_id = pkg2_identify(kernel_crc32);
|
|
|
|
|
2018-07-01 02:03:17 +00:00
|
|
|
// In case a kernel patch option is set; allows to disable SVC verification or/and enable debug mode.
|
|
|
|
kernel_patch_t *kernel_patchset = ctxt.pkg2_kernel_id->kernel_patchset;
|
2018-06-08 11:07:25 +00:00
|
|
|
if (kernel_patchset != NULL)
|
|
|
|
{
|
2018-06-19 13:53:41 +00:00
|
|
|
gfx_printf(&gfx_con, "%kPatching kernel%k\n", 0xFFFFBA00, 0xFFCCCCCC);
|
2018-07-01 02:03:17 +00:00
|
|
|
u32 *temp;
|
|
|
|
for (u32 i = 0; kernel_patchset[i].id != 0xFFFFFFFF; i++)
|
|
|
|
{
|
|
|
|
if ((ctxt.svcperm && kernel_patchset[i].id == SVC_VERIFY_DS)
|
|
|
|
|| (ctxt.debugmode && kernel_patchset[i].id == DEBUG_MODE_EN)
|
|
|
|
|| (ctxt.atmosphere && kernel_patchset[i].id == ATM_GEN_PATCH))
|
|
|
|
*(vu32 *)(ctxt.kernel + kernel_patchset[i].off) = kernel_patchset[i].val;
|
|
|
|
else if (ctxt.atmosphere && kernel_patchset[i].id == ATM_ARR_PATCH)
|
|
|
|
{
|
|
|
|
temp = (u32 *)kernel_patchset[i].ptr;
|
|
|
|
for (u32 j = 0; j < kernel_patchset[i].val; j++)
|
|
|
|
*(vu32 *)(ctxt.kernel + kernel_patchset[i].off + (j << 2)) = temp[j];
|
|
|
|
}
|
|
|
|
}
|
2018-06-08 11:07:25 +00:00
|
|
|
}
|
2018-05-04 07:15:29 +00:00
|
|
|
}
|
2018-05-01 05:15:48 +00:00
|
|
|
}
|
2018-06-08 11:07:25 +00:00
|
|
|
|
2018-07-01 02:03:17 +00:00
|
|
|
// Merge extra KIP1s into loaded ones.
|
2018-06-19 13:53:41 +00:00
|
|
|
gfx_printf(&gfx_con, "%kPatching kernel initial processes%k\n", 0xFFFFBA00, 0xFFCCCCCC);
|
2018-06-08 11:07:25 +00:00
|
|
|
LIST_FOREACH_ENTRY(merge_kip_t, mki, &ctxt.kip1_list, link)
|
|
|
|
pkg2_merge_kip(&kip1_info, (pkg2_kip1_t *)mki->kip1);
|
|
|
|
|
2018-08-05 11:40:32 +00:00
|
|
|
// Patch kip1s in memory if needed.
|
2018-07-22 20:22:10 +00:00
|
|
|
const char* unappliedPatch = pkg2_patch_kips(&kip1_info, ctxt.kip1_patches);
|
|
|
|
if (unappliedPatch != NULL)
|
|
|
|
{
|
|
|
|
gfx_printf(&gfx_con, "%kREQUESTED PATCH '%s' NOT APPLIED!%k\n", 0xFFFF0000, unappliedPatch, 0xFFCCCCCC);
|
2018-08-05 11:40:32 +00:00
|
|
|
sd_unmount(); // Just exiting is not enough until pkg2_patch_kips stops modifying the string passed into it.
|
|
|
|
while(1) {} // MUST stop here, because if user requests 'nogc' but it's not applied, their GC controller gets updated!
|
2018-07-22 20:22:10 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 02:03:17 +00:00
|
|
|
// Rebuild and encrypt package2.
|
2018-06-08 11:07:25 +00:00
|
|
|
pkg2_build_encrypt((void *)0xA9800000, ctxt.kernel, ctxt.kernel_size, &kip1_info);
|
|
|
|
gfx_printf(&gfx_con, "Rebuilt and loaded package2\n");
|
|
|
|
|
2018-07-01 02:03:17 +00:00
|
|
|
// Unmount SD card.
|
2018-06-23 04:04:41 +00:00
|
|
|
sd_unmount();
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2018-06-19 13:53:41 +00:00
|
|
|
gfx_printf(&gfx_con, "\n%kBooting...%k\n", 0xFF96FF00, 0xFFCCCCCC);
|
2018-06-01 16:22:11 +00:00
|
|
|
|
2018-06-15 11:28:27 +00:00
|
|
|
se_aes_key_clear(8);
|
|
|
|
se_aes_key_clear(11);
|
2018-06-01 21:12:22 +00:00
|
|
|
|
2018-07-01 02:03:17 +00:00
|
|
|
// Final per firmware configuration.
|
2018-06-08 11:07:25 +00:00
|
|
|
switch (ctxt.pkg1_id->kb)
|
|
|
|
{
|
|
|
|
case KB_FIRMWARE_VERSION_100_200:
|
2018-07-01 02:03:17 +00:00
|
|
|
if (!exoFwNumber)
|
|
|
|
{
|
2018-08-05 11:40:32 +00:00
|
|
|
if (!strcmp(ctxt.pkg1_id->id, "20161121183008"))
|
2018-07-01 02:03:17 +00:00
|
|
|
exoFwNumber = 1;
|
|
|
|
else
|
|
|
|
exoFwNumber = 2;
|
|
|
|
}
|
2018-06-08 11:07:25 +00:00
|
|
|
case KB_FIRMWARE_VERSION_300:
|
|
|
|
case KB_FIRMWARE_VERSION_301:
|
2018-07-09 20:01:40 +00:00
|
|
|
if (ctxt.pkg1_id->kb == KB_FIRMWARE_VERSION_300)
|
2018-09-24 20:22:19 +00:00
|
|
|
PMC(APBDEV_PMC_SECURE_SCRATCH32) = 0xE3; // Warmboot 3.0.0 PA address id.
|
2018-07-09 20:01:40 +00:00
|
|
|
else if (ctxt.pkg1_id->kb == KB_FIRMWARE_VERSION_301)
|
2018-09-24 20:22:19 +00:00
|
|
|
PMC(APBDEV_PMC_SECURE_SCRATCH32) = 0x104; // Warmboot 3.0.1/.2 PA address id.
|
2018-06-15 11:28:27 +00:00
|
|
|
se_key_acc_ctrl(12, 0xFF);
|
|
|
|
se_key_acc_ctrl(13, 0xFF);
|
2018-06-08 11:07:25 +00:00
|
|
|
bootStateDramPkg2 = 2;
|
|
|
|
bootStatePkg2Continue = 3;
|
2018-07-01 02:03:17 +00:00
|
|
|
if (!exoFwNumber)
|
|
|
|
exoFwNumber = 3;
|
2018-06-08 11:07:25 +00:00
|
|
|
break;
|
|
|
|
case KB_FIRMWARE_VERSION_400:
|
2018-07-01 02:03:17 +00:00
|
|
|
if (!exoFwNumber)
|
|
|
|
exoFwNumber = 4;
|
2018-06-08 11:07:25 +00:00
|
|
|
case KB_FIRMWARE_VERSION_500:
|
2018-09-18 00:08:05 +00:00
|
|
|
if (!exoFwNumber)
|
|
|
|
exoFwNumber = 5;
|
|
|
|
case KB_FIRMWARE_VERSION_600:
|
2018-07-07 15:36:18 +00:00
|
|
|
default:
|
2018-06-15 11:28:27 +00:00
|
|
|
se_key_acc_ctrl(12, 0xFF);
|
|
|
|
se_key_acc_ctrl(15, 0xFF);
|
2018-06-08 11:07:25 +00:00
|
|
|
bootStateDramPkg2 = 2;
|
|
|
|
bootStatePkg2Continue = 4;
|
2018-07-01 02:03:17 +00:00
|
|
|
if (!exoFwNumber)
|
2018-09-18 00:08:05 +00:00
|
|
|
exoFwNumber = 6;
|
2018-06-08 11:07:25 +00:00
|
|
|
break;
|
2018-05-28 23:11:22 +00:00
|
|
|
}
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2018-07-07 15:36:18 +00:00
|
|
|
// Free allocated memory.
|
|
|
|
ini_free_section(cfg);
|
|
|
|
_free_launch_components(&ctxt);
|
|
|
|
|
2018-09-18 21:28:13 +00:00
|
|
|
// Clear BCT area for retail units and copy it over if dev unit.
|
2018-09-18 00:08:05 +00:00
|
|
|
if (ctxt.pkg1_id->kb < KB_FIRMWARE_VERSION_600)
|
2018-09-18 21:28:13 +00:00
|
|
|
{
|
2018-09-18 00:08:05 +00:00
|
|
|
memset((void *)0x4003D000, 0, 0x3000);
|
2018-09-18 21:28:13 +00:00
|
|
|
if ((fuse_read_odm(4) & 3) == 3)
|
|
|
|
memcpy((void *)0x4003D000, bootConfigBuf, 0x1000);
|
|
|
|
}
|
2018-09-18 00:08:05 +00:00
|
|
|
else
|
2018-09-18 21:28:13 +00:00
|
|
|
{
|
2018-09-18 00:08:05 +00:00
|
|
|
memset((void *)0x4003F000, 0, 0x1000);
|
2018-09-18 21:28:13 +00:00
|
|
|
if ((fuse_read_odm(4) & 3) == 3)
|
|
|
|
memcpy((void *)0x4003F800, bootConfigBuf, 0x800);
|
|
|
|
}
|
|
|
|
free(bootConfigBuf);
|
2018-07-01 02:03:17 +00:00
|
|
|
|
2018-07-04 22:51:36 +00:00
|
|
|
// Config Exosphère if booting Atmosphère.
|
2018-07-01 02:03:17 +00:00
|
|
|
if (ctxt.atmosphere)
|
|
|
|
{
|
|
|
|
vu32 *mb_exo_magic = (vu32 *)0x40002E40;
|
|
|
|
vu32 *mb_exo_fw_no = (vu32 *)0x40002E44;
|
|
|
|
|
|
|
|
*mb_exo_magic = MAGIC_EXOSPHERE;
|
|
|
|
*mb_exo_fw_no = exoFwNumber;
|
|
|
|
}
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2018-08-05 11:40:32 +00:00
|
|
|
// Finalize MC carveout and lock SE before starting 'SecureMonitor'.
|
|
|
|
mc_config_carveout_finalize();
|
2018-05-01 05:15:48 +00:00
|
|
|
_se_lock();
|
|
|
|
|
2018-09-24 20:22:19 +00:00
|
|
|
//TODO: pkg1.1 locks PMC scratches, we can do that too at some point. For <4.0.0 after secmon?
|
|
|
|
//_pmc_scratch_lock(ctxt.pkg1_id->kb);
|
|
|
|
|
2018-07-09 19:13:56 +00:00
|
|
|
// < 4.0.0 Signals - 0: Nothing ready, 1: BCT ready, 2: DRAM and pkg2 ready, 3: Continue boot.
|
|
|
|
// >= 4.0.0 Signals - 0: Nothing ready, 1: BCT ready, 2: DRAM ready, 4: pkg2 ready and continue boot.
|
2018-05-01 05:15:48 +00:00
|
|
|
vu32 *mb_in = (vu32 *)0x40002EF8;
|
2018-08-05 11:40:32 +00:00
|
|
|
// Non-zero: Secmon ready.
|
2018-05-01 05:15:48 +00:00
|
|
|
vu32 *mb_out = (vu32 *)0x40002EFC;
|
|
|
|
|
2018-07-09 19:13:56 +00:00
|
|
|
// Start from DRAM ready signal.
|
2018-06-04 22:55:32 +00:00
|
|
|
*mb_in = bootStateDramPkg2;
|
2018-05-01 05:15:48 +00:00
|
|
|
*mb_out = 0;
|
|
|
|
|
2018-08-13 09:12:53 +00:00
|
|
|
// Disable display. This must be executed before secmon to provide support for all fw versions.
|
|
|
|
display_end();
|
2018-07-22 12:18:30 +00:00
|
|
|
|
2018-07-09 19:13:56 +00:00
|
|
|
// Wait for secmon to get ready.
|
2018-05-01 05:15:48 +00:00
|
|
|
cluster_boot_cpu0(ctxt.pkg1_id->secmon_base);
|
|
|
|
while (!*mb_out)
|
2018-09-18 20:38:54 +00:00
|
|
|
usleep(1); // This only works when in IRAM or with a trained DRAM.
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2018-07-09 19:13:56 +00:00
|
|
|
// Signal pkg2 ready and continue boot.
|
2018-06-04 22:55:32 +00:00
|
|
|
*mb_in = bootStatePkg2Continue;
|
2018-05-01 05:15:48 +00:00
|
|
|
|
2018-07-09 19:13:56 +00:00
|
|
|
// Halt ourselves in waitevent state and resume if there's JTAG activity.
|
2018-05-01 05:15:48 +00:00
|
|
|
while (1)
|
2018-06-24 19:31:24 +00:00
|
|
|
FLOW_CTLR(FLOW_CTLR_HALT_COP_EVENTS) = 0x50000000;
|
2018-05-01 05:15:48 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|