2018-09-07 16:00:13 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Atmosphère-NX
|
|
|
|
*
|
|
|
|
* 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-05 22:55:40 +01:00
|
|
|
#include <stdio.h>
|
2018-05-10 22:48:41 +01:00
|
|
|
#include <string.h>
|
2018-05-14 01:40:35 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <malloc.h>
|
2018-04-09 22:34:23 +01:00
|
|
|
#include "utils.h"
|
2018-05-14 20:17:06 +01:00
|
|
|
#include "fs_utils.h"
|
2018-04-09 22:34:23 +01:00
|
|
|
#include "nxboot.h"
|
2018-08-18 17:59:33 +01:00
|
|
|
#include "nxfs.h"
|
2018-08-29 18:28:21 +01:00
|
|
|
#include "bct.h"
|
2018-08-18 17:59:33 +01:00
|
|
|
#include "di.h"
|
|
|
|
#include "mc.h"
|
|
|
|
#include "se.h"
|
2018-08-19 17:09:30 +01:00
|
|
|
#include "pmc.h"
|
2018-08-29 18:28:21 +01:00
|
|
|
#include "i2c.h"
|
|
|
|
#include "max77620.h"
|
2018-08-18 17:59:33 +01:00
|
|
|
#include "cluster.h"
|
|
|
|
#include "flow.h"
|
|
|
|
#include "timers.h"
|
2018-04-10 23:37:58 +01:00
|
|
|
#include "key_derivation.h"
|
2018-05-10 22:48:41 +01:00
|
|
|
#include "package1.h"
|
2018-04-12 01:19:01 +01:00
|
|
|
#include "package2.h"
|
2018-04-10 23:09:00 +01:00
|
|
|
#include "loader.h"
|
|
|
|
#include "splash_screen.h"
|
2018-04-10 23:37:58 +01:00
|
|
|
#include "exocfg.h"
|
|
|
|
#include "display/video_fb.h"
|
|
|
|
#include "lib/ini.h"
|
2018-05-18 23:54:38 +01:00
|
|
|
|
|
|
|
#define u8 uint8_t
|
|
|
|
#define u32 uint32_t
|
|
|
|
#include "exosphere_bin.h"
|
|
|
|
#undef u8
|
|
|
|
#undef u32
|
|
|
|
|
2018-04-10 23:37:58 +01:00
|
|
|
static int exosphere_ini_handler(void *user, const char *section, const char *name, const char *value) {
|
|
|
|
exosphere_config_t *exo_cfg = (exosphere_config_t *)user;
|
|
|
|
if (strcmp(section, "exosphere") == 0) {
|
|
|
|
if (strcmp(name, EXOSPHERE_TARGETFW_KEY) == 0) {
|
|
|
|
sscanf(value, "%d", &exo_cfg->target_firmware);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:57:01 +01:00
|
|
|
static uint32_t nxboot_get_target_firmware(const void *package1loader) {
|
|
|
|
const package1loader_header_t *package1loader_header = (const package1loader_header_t *)package1loader;
|
|
|
|
switch (package1loader_header->version) {
|
|
|
|
case 0x01: /* 1.0.0 */
|
|
|
|
return EXOSPHERE_TARGET_FIRMWARE_100;
|
|
|
|
case 0x02: /* 2.0.0 - 2.3.0 */
|
|
|
|
return EXOSPHERE_TARGET_FIRMWARE_200;
|
|
|
|
case 0x04: /* 3.0.0 and 3.0.1 - 3.0.2 */
|
|
|
|
return EXOSPHERE_TARGET_FIRMWARE_300;
|
|
|
|
case 0x07: /* 4.0.0 - 4.1.0 */
|
|
|
|
return EXOSPHERE_TARGET_FIRMWARE_400;
|
|
|
|
case 0x0B: /* 5.0.0 - 5.1.0 */
|
|
|
|
return EXOSPHERE_TARGET_FIRMWARE_500;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nxboot_configure_exosphere(uint32_t target_firmware) {
|
2018-04-10 23:37:58 +01:00
|
|
|
exosphere_config_t exo_cfg = {0};
|
2018-05-05 22:55:40 +01:00
|
|
|
|
2018-04-10 23:37:58 +01:00
|
|
|
exo_cfg.magic = MAGIC_EXOSPHERE_BOOTCONFIG;
|
2018-08-23 19:57:01 +01:00
|
|
|
exo_cfg.target_firmware = target_firmware;
|
2018-05-05 22:55:40 +01:00
|
|
|
|
2018-04-10 23:37:58 +01:00
|
|
|
if (ini_parse_string(get_loader_ctx()->bct0, exosphere_ini_handler, &exo_cfg) < 0) {
|
2018-08-23 19:57:01 +01:00
|
|
|
fatal_error("[NXBOOT]: Failed to parse BCT.ini!\n");
|
2018-04-10 23:37:58 +01:00
|
|
|
}
|
2018-05-05 22:55:40 +01:00
|
|
|
|
2018-08-23 19:57:01 +01:00
|
|
|
if ((exo_cfg.target_firmware < EXOSPHERE_TARGET_FIRMWARE_MIN) || (exo_cfg.target_firmware > EXOSPHERE_TARGET_FIRMWARE_MAX)) {
|
|
|
|
fatal_error("[NXBOOT]: Invalid Exosphere target firmware!\n");
|
2018-04-10 23:37:58 +01:00
|
|
|
}
|
2018-05-05 22:55:40 +01:00
|
|
|
|
2018-04-10 23:37:58 +01:00
|
|
|
*(MAILBOX_EXOSPHERE_CONFIGURATION) = exo_cfg;
|
|
|
|
}
|
2018-04-09 22:34:23 +01:00
|
|
|
|
2018-08-29 18:28:21 +01:00
|
|
|
static void nxboot_set_bootreason() {
|
|
|
|
boot_reason_t boot_reason = {0};
|
|
|
|
FILE *boot0;
|
|
|
|
nvboot_config_table *bct;
|
|
|
|
nv_bootloader_info *bootloader_info;
|
|
|
|
|
|
|
|
/* Allocate memory for the BCT. */
|
|
|
|
bct = malloc(sizeof(nvboot_config_table));
|
|
|
|
if (bct == NULL) {
|
|
|
|
fatal_error("[NXBOOT]: Out of memory!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open boot0. */
|
|
|
|
boot0 = fopen("boot0:/", "rb");
|
|
|
|
if (boot0 == NULL) {
|
|
|
|
fatal_error("[NXBOOT]: Failed to open boot0!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the BCT. */
|
|
|
|
if (fread(bct, sizeof(nvboot_config_table), 1, boot0) == 0) {
|
|
|
|
fatal_error("[NXBOOT]: Failed to read the BCT!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close boot0. */
|
|
|
|
fclose(boot0);
|
|
|
|
|
|
|
|
/* Populate bootloader parameters. */
|
|
|
|
bootloader_info = &bct->bootloader[0];
|
|
|
|
boot_reason.bootloader_version = bootloader_info->version;
|
|
|
|
boot_reason.bootloader_start_block = bootloader_info->start_blk;
|
|
|
|
boot_reason.bootloader_start_page = bootloader_info->start_page;
|
|
|
|
boot_reason.bootloader_attribute = bootloader_info->attribute;
|
|
|
|
|
|
|
|
uint8_t power_key_intr = 0;
|
|
|
|
uint8_t rtc_intr = 0;
|
|
|
|
i2c_query(I2C_5, MAX77620_PWR_I2C_ADDR, MAX77620_REG_ONOFFIRQ, &power_key_intr, 1);
|
|
|
|
i2c_query(I2C_5, MAX77620_RTC_I2C_ADDR, MAX77620_REG_RTCINT, &rtc_intr, 1);
|
|
|
|
|
|
|
|
/* Set PMIC value. */
|
|
|
|
boot_reason.boot_reason_value = ((rtc_intr << 0x08) | power_key_intr);
|
|
|
|
|
|
|
|
/* TODO: Find out what these mean. */
|
|
|
|
if (power_key_intr & 0x80)
|
|
|
|
boot_reason.boot_reason_state = 0x01;
|
|
|
|
else if (power_key_intr & 0x08)
|
|
|
|
boot_reason.boot_reason_state = 0x02;
|
|
|
|
else if (rtc_intr & 0x02)
|
|
|
|
boot_reason.boot_reason_state = 0x03;
|
|
|
|
else if (rtc_intr & 0x04)
|
|
|
|
boot_reason.boot_reason_state = 0x04;
|
|
|
|
|
|
|
|
/* Set in memory. */
|
2018-09-05 20:51:03 +01:00
|
|
|
memcpy((void *)MAILBOX_NX_BOOTLOADER_BOOT_REASON_BASE, &boot_reason, sizeof(boot_reason));
|
2018-08-29 18:28:21 +01:00
|
|
|
|
|
|
|
/* Clean up. */
|
|
|
|
free(bct);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nxboot_move_bootconfig() {
|
|
|
|
FILE *bcfile;
|
|
|
|
void *bootconfig;
|
|
|
|
|
|
|
|
/* Allocate memory for reading BootConfig. */
|
|
|
|
bootconfig = memalign(0x1000, 0x4000);
|
|
|
|
if (bootconfig == NULL) {
|
|
|
|
fatal_error("[NXBOOT]: Out of memory!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get BootConfig from the Package2 partition. */
|
|
|
|
bcfile = fopen("bcpkg21:/", "rb");
|
|
|
|
if (bcfile == NULL) {
|
|
|
|
fatal_error("[NXBOOT]: Failed to open BootConfig from eMMC!\n");
|
|
|
|
}
|
|
|
|
if (fread(bootconfig, 0x4000, 1, bcfile) < 1) {
|
|
|
|
fatal_error("[NXBOOT]: Failed to read BootConfig!\n");
|
|
|
|
}
|
|
|
|
fclose(bcfile);
|
|
|
|
|
|
|
|
/* Copy the first 0x3000 bytes into IRAM. */
|
|
|
|
memset((void *)0x4003D000, 0, 0x3000);
|
|
|
|
memcpy((void *)0x4003D000, bootconfig, 0x3000);
|
|
|
|
|
|
|
|
/* Clean up. */
|
|
|
|
free(bootconfig);
|
|
|
|
}
|
|
|
|
|
2018-04-09 22:34:23 +01:00
|
|
|
/* This is the main function responsible for booting Horizon. */
|
2018-05-14 01:40:35 +01:00
|
|
|
static nx_keyblob_t __attribute__((aligned(16))) g_keyblobs[32];
|
2018-04-09 22:34:23 +01:00
|
|
|
void nxboot_main(void) {
|
2018-08-19 17:09:30 +01:00
|
|
|
volatile tegra_pmc_t *pmc = pmc_get_regs();
|
|
|
|
volatile tegra_se_t *se = se_get_regs();
|
2018-04-10 23:09:00 +01:00
|
|
|
loader_ctx_t *loader_ctx = get_loader_ctx();
|
2018-05-17 14:56:02 +01:00
|
|
|
package2_header_t *package2;
|
|
|
|
size_t package2_size;
|
2018-05-14 20:17:06 +01:00
|
|
|
void *tsec_fw;
|
|
|
|
size_t tsec_fw_size;
|
|
|
|
void *warmboot_fw;
|
|
|
|
size_t warmboot_fw_size;
|
2018-08-19 17:09:30 +01:00
|
|
|
void *warmboot_memaddr;
|
2018-05-14 20:17:06 +01:00
|
|
|
void *package1loader;
|
2018-08-23 19:57:01 +01:00
|
|
|
size_t package1loader_size;
|
2018-05-14 20:17:06 +01:00
|
|
|
package1_header_t *package1;
|
|
|
|
size_t package1_size;
|
2018-05-17 14:56:02 +01:00
|
|
|
uint32_t available_revision;
|
2018-05-17 22:18:33 +01:00
|
|
|
FILE *boot0, *pk2file;
|
2018-05-14 20:17:06 +01:00
|
|
|
void *exosphere_memaddr;
|
2018-05-14 01:40:35 +01:00
|
|
|
|
2018-08-23 19:57:01 +01:00
|
|
|
/* Allocate memory for reading Package2. */
|
2018-08-29 18:28:21 +01:00
|
|
|
package2 = memalign(0x1000, PACKAGE2_SIZE_MAX);
|
2018-05-17 14:56:02 +01:00
|
|
|
if (package2 == NULL) {
|
2018-08-23 19:57:01 +01:00
|
|
|
fatal_error("[NXBOOT]: Out of memory!\n");
|
2018-05-17 14:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read Package2 from a file, otherwise from its partition(s). */
|
2018-08-23 19:57:01 +01:00
|
|
|
printf("[NXBOOT]: Reading package2...\n");
|
2018-05-17 14:56:02 +01:00
|
|
|
if (loader_ctx->package2_path[0] != '\0') {
|
2018-05-17 22:18:33 +01:00
|
|
|
pk2file = fopen(loader_ctx->package2_path, "rb");
|
|
|
|
if (pk2file == NULL) {
|
2018-08-23 19:57:01 +01:00
|
|
|
fatal_error("[NXBOOT]: Failed to open Package2 from %s: %s!\n", loader_ctx->package2_path, strerror(errno));
|
2018-05-17 14:56:02 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-05-17 22:18:33 +01:00
|
|
|
pk2file = fopen("bcpkg21:/", "rb");
|
2018-08-29 18:28:21 +01:00
|
|
|
if (pk2file == NULL) {
|
|
|
|
fatal_error("[NXBOOT]: Failed to open Package2 from eMMC: %s!\n", strerror(errno));
|
|
|
|
}
|
|
|
|
if (fseek(pk2file, 0x4000, SEEK_SET) != 0) {
|
|
|
|
fatal_error("[NXBOOT]: Failed to seek Package2 in eMMC: %s!\n", strerror(errno));
|
2018-05-17 22:18:33 +01:00
|
|
|
fclose(pk2file);
|
2018-05-17 14:56:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 22:18:33 +01:00
|
|
|
setvbuf(pk2file, NULL, _IONBF, 0); /* Workaround. */
|
|
|
|
if (fread(package2, sizeof(package2_header_t), 1, pk2file) < 1) {
|
2018-08-23 19:57:01 +01:00
|
|
|
fatal_error("[NXBOOT]: Failed to read Package2!\n");
|
2018-05-17 22:18:33 +01:00
|
|
|
}
|
|
|
|
package2_size = package2_meta_get_size(&package2->metadata);
|
2018-08-29 18:28:21 +01:00
|
|
|
if ((package2_size > PACKAGE2_SIZE_MAX) || (package2_size <= sizeof(package2_header_t))) {
|
2018-08-23 19:57:01 +01:00
|
|
|
fatal_error("[NXBOOT]: Package2 is too big or too small!\n");
|
2018-05-17 22:18:33 +01:00
|
|
|
}
|
|
|
|
if (fread(package2->data, package2_size - sizeof(package2_header_t), 1, pk2file) < 1) {
|
2018-08-23 19:57:01 +01:00
|
|
|
fatal_error("[NXBOOT]: Failed to read Package2!\n");
|
2018-05-17 22:18:33 +01:00
|
|
|
}
|
|
|
|
fclose(pk2file);
|
2018-08-23 19:57:01 +01:00
|
|
|
|
|
|
|
/* Read and parse boot0. */
|
|
|
|
printf("[NXBOOT]: Reading boot0...\n");
|
2018-05-17 14:56:02 +01:00
|
|
|
boot0 = fopen("boot0:/", "rb");
|
2018-08-29 18:28:21 +01:00
|
|
|
if ((boot0 == NULL) || (package1_read_and_parse_boot0(&package1loader, &package1loader_size, g_keyblobs, &available_revision, boot0) == -1)) {
|
2018-08-23 19:57:01 +01:00
|
|
|
fatal_error("[NXBOOT]: Couldn't parse boot0: %s!\n", strerror(errno));
|
2018-05-14 01:40:35 +01:00
|
|
|
}
|
|
|
|
fclose(boot0);
|
|
|
|
|
2018-05-14 20:17:06 +01:00
|
|
|
/* Read the TSEC firmware from a file, otherwise from PK1L. */
|
|
|
|
if (loader_ctx->tsecfw_path[0] != '\0') {
|
|
|
|
tsec_fw_size = get_file_size(loader_ctx->tsecfw_path);
|
2018-08-29 18:28:21 +01:00
|
|
|
if ((tsec_fw_size != 0) && (tsec_fw_size != 0xF00)) {
|
2018-08-23 19:57:01 +01:00
|
|
|
fatal_error("[NXBOOT]: TSEC firmware from %s has a wrong size!\n", loader_ctx->tsecfw_path);
|
2018-05-14 20:17:06 +01:00
|
|
|
} else if (tsec_fw_size == 0) {
|
2018-08-23 19:57:01 +01:00
|
|
|
fatal_error("[NXBOOT]: Could not read the TSEC firmware from %s!\n", loader_ctx->tsecfw_path);
|
2018-05-14 20:17:06 +01:00
|
|
|
}
|
2018-08-29 18:28:21 +01:00
|
|
|
|
|
|
|
/* Allocate memory for the TSEC firmware. */
|
2018-05-14 20:17:06 +01:00
|
|
|
tsec_fw = memalign(0x100, tsec_fw_size);
|
2018-08-29 18:28:21 +01:00
|
|
|
|
2018-05-14 20:17:06 +01:00
|
|
|
if (tsec_fw == NULL) {
|
2018-08-23 19:57:01 +01:00
|
|
|
fatal_error("[NXBOOT]: Out of memory!\n");
|
2018-05-14 20:17:06 +01:00
|
|
|
}
|
|
|
|
if (read_from_file(tsec_fw, tsec_fw_size, loader_ctx->tsecfw_path) != tsec_fw_size) {
|
2018-08-23 19:57:01 +01:00
|
|
|
fatal_error("[NXBOOT]: Could not read the TSEC firmware from %s!\n", loader_ctx->tsecfw_path);
|
2018-05-14 20:17:06 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-05-14 01:40:35 +01:00
|
|
|
tsec_fw_size = package1_get_tsec_fw(&tsec_fw, package1loader, package1loader_size);
|
|
|
|
if (tsec_fw_size == 0) {
|
2018-08-23 19:57:01 +01:00
|
|
|
fatal_error("[NXBOOT]: Failed to read the TSEC firmware from Package1loader!\n");
|
2018-05-14 01:40:35 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-23 19:57:01 +01:00
|
|
|
|
|
|
|
/* Find the system's target firmware. */
|
|
|
|
uint32_t target_firmware = nxboot_get_target_firmware(package1loader);
|
|
|
|
if (!target_firmware)
|
|
|
|
fatal_error("[NXBOOT]: Failed to detect target firmware!\n");
|
|
|
|
else
|
|
|
|
printf("[NXBOOT]: Detected target firmware %ld!\n", target_firmware);
|
2018-05-05 22:55:40 +01:00
|
|
|
|
2018-08-23 19:57:01 +01:00
|
|
|
/* Setup boot configuration for Exosphère. */
|
|
|
|
nxboot_configure_exosphere(target_firmware);
|
|
|
|
|
2018-08-29 18:28:21 +01:00
|
|
|
/* Initialize Boot Reason on older firmware versions. */
|
|
|
|
if (MAILBOX_EXOSPHERE_CONFIGURATION->target_firmware < EXOSPHERE_TARGET_FIRMWARE_400) {
|
|
|
|
printf("[NXBOOT]: Initializing Boot Reason...\n");
|
|
|
|
nxboot_set_bootreason();
|
|
|
|
}
|
|
|
|
|
2018-04-10 23:37:58 +01:00
|
|
|
/* Derive keydata. */
|
2018-05-17 14:56:02 +01:00
|
|
|
if (derive_nx_keydata(MAILBOX_EXOSPHERE_CONFIGURATION->target_firmware, g_keyblobs, available_revision, tsec_fw, tsec_fw_size) != 0) {
|
2018-08-23 19:57:01 +01:00
|
|
|
fatal_error("[NXBOOT]: Key derivation failed!\n");
|
2018-05-14 01:40:35 +01:00
|
|
|
}
|
2018-05-05 22:55:40 +01:00
|
|
|
|
2018-05-14 20:17:06 +01:00
|
|
|
/* Read the warmboot firmware from a file, otherwise from PK1. */
|
|
|
|
if (loader_ctx->warmboot_path[0] != '\0') {
|
|
|
|
warmboot_fw_size = get_file_size(loader_ctx->warmboot_path);
|
|
|
|
if (warmboot_fw_size == 0) {
|
2018-08-23 19:57:01 +01:00
|
|
|
fatal_error("[NXBOOT]: Could not read the warmboot firmware from %s!\n", loader_ctx->warmboot_path);
|
2018-05-14 20:17:06 +01:00
|
|
|
}
|
|
|
|
|
2018-08-29 18:28:21 +01:00
|
|
|
/* Allocate memory for the warmboot firmware. */
|
2018-05-14 20:17:06 +01:00
|
|
|
warmboot_fw = malloc(warmboot_fw_size);
|
2018-08-29 18:28:21 +01:00
|
|
|
|
2018-05-14 20:17:06 +01:00
|
|
|
if (warmboot_fw == NULL) {
|
2018-08-23 19:57:01 +01:00
|
|
|
fatal_error("[NXBOOT]: Out of memory!\n");
|
2018-05-14 20:17:06 +01:00
|
|
|
}
|
|
|
|
if (read_from_file(warmboot_fw, warmboot_fw_size, loader_ctx->warmboot_path) != warmboot_fw_size) {
|
2018-08-23 19:57:01 +01:00
|
|
|
fatal_error("[NXBOOT]: Could not read the warmboot firmware from %s!\n", loader_ctx->warmboot_path);
|
2018-05-14 20:17:06 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-05-14 01:40:35 +01:00
|
|
|
uint8_t ctr[16];
|
|
|
|
package1_size = package1_get_encrypted_package1(&package1, ctr, package1loader, package1loader_size);
|
2018-08-19 17:09:30 +01:00
|
|
|
if (package1_decrypt(package1, package1_size, ctr)) {
|
2018-05-14 01:40:35 +01:00
|
|
|
warmboot_fw = package1_get_warmboot_fw(package1);
|
|
|
|
warmboot_fw_size = package1->warmboot_size;
|
|
|
|
} else {
|
|
|
|
warmboot_fw = NULL;
|
|
|
|
warmboot_fw_size = 0;
|
|
|
|
}
|
2018-05-10 22:48:41 +01:00
|
|
|
|
2018-05-14 01:40:35 +01:00
|
|
|
if (warmboot_fw_size == 0) {
|
2018-08-23 19:57:01 +01:00
|
|
|
fatal_error("[NXBOOT]: Could not read the warmboot firmware from Package1!\n");
|
2018-05-14 01:40:35 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-19 17:09:30 +01:00
|
|
|
|
|
|
|
/* Select the right address for the warmboot firmware. */
|
|
|
|
if (MAILBOX_EXOSPHERE_CONFIGURATION->target_firmware < EXOSPHERE_TARGET_FIRMWARE_400) {
|
|
|
|
warmboot_memaddr = (void *)0x8000D000;
|
|
|
|
} else {
|
|
|
|
warmboot_memaddr = (void *)0x4003B000;
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:57:01 +01:00
|
|
|
printf("[NXBOOT]: Copying warmboot firmware...\n");
|
2018-08-19 17:09:30 +01:00
|
|
|
|
|
|
|
/* Copy the warmboot firmware and set the address in PMC if necessary. */
|
|
|
|
if (warmboot_fw && (warmboot_fw_size > 0)) {
|
|
|
|
memcpy(warmboot_memaddr, warmboot_fw, warmboot_fw_size);
|
|
|
|
if (MAILBOX_EXOSPHERE_CONFIGURATION->target_firmware < EXOSPHERE_TARGET_FIRMWARE_400)
|
|
|
|
pmc->scratch1 = (uint32_t)warmboot_memaddr;
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:57:01 +01:00
|
|
|
printf("[NXBOOT]: Rebuilding package2...\n");
|
2018-08-18 17:59:33 +01:00
|
|
|
|
2018-05-14 20:17:06 +01:00
|
|
|
/* Patch package2, adding Thermosphère + custom KIPs. */
|
2018-05-15 15:09:06 +01:00
|
|
|
package2_rebuild_and_copy(package2, MAILBOX_EXOSPHERE_CONFIGURATION->target_firmware);
|
2018-05-14 01:40:35 +01:00
|
|
|
|
2018-08-29 18:28:21 +01:00
|
|
|
printf(u8"[NXBOOT]: Reading Exosphère...\n");
|
2018-08-18 17:59:33 +01:00
|
|
|
|
2018-08-19 17:09:30 +01:00
|
|
|
/* Select the right address for Exosphère. */
|
2018-09-05 20:51:03 +01:00
|
|
|
if (MAILBOX_EXOSPHERE_CONFIGURATION->target_firmware < EXOSPHERE_TARGET_FIRMWARE_400) {
|
2018-08-29 18:28:21 +01:00
|
|
|
exosphere_memaddr = (void *)0x4002D000;
|
2018-05-14 20:17:06 +01:00
|
|
|
} else {
|
2018-08-29 18:28:21 +01:00
|
|
|
exosphere_memaddr = (void *)0x4002B000;
|
2018-05-14 20:17:06 +01:00
|
|
|
}
|
|
|
|
|
2018-08-19 17:09:30 +01:00
|
|
|
/* Copy Exosphère to a good location or read it directly to it. */
|
2018-05-14 20:17:06 +01:00
|
|
|
if (loader_ctx->exosphere_path[0] != '\0') {
|
|
|
|
size_t exosphere_size = get_file_size(loader_ctx->exosphere_path);
|
|
|
|
if (exosphere_size == 0) {
|
2018-08-29 18:28:21 +01:00
|
|
|
fatal_error(u8"[NXBOOT]: Could not read Exosphère from %s!\n", loader_ctx->exosphere_path);
|
2018-05-14 20:17:06 +01:00
|
|
|
} else if (exosphere_size > 0x10000) {
|
|
|
|
/* The maximum is actually a bit less than that. */
|
2018-08-29 18:28:21 +01:00
|
|
|
fatal_error(u8"[NXBOOT]: Exosphère from %s is too big!\n", loader_ctx->exosphere_path);
|
2018-05-14 20:17:06 +01:00
|
|
|
}
|
2018-05-15 15:09:06 +01:00
|
|
|
|
2018-05-14 20:17:06 +01:00
|
|
|
if (read_from_file(exosphere_memaddr, exosphere_size, loader_ctx->exosphere_path) != exosphere_size) {
|
2018-08-29 18:28:21 +01:00
|
|
|
fatal_error(u8"[NXBOOT]: Could not read Exosphère from %s!\n", loader_ctx->exosphere_path);
|
2018-05-14 20:17:06 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-05-15 01:45:31 +01:00
|
|
|
memcpy(exosphere_memaddr, exosphere_bin, exosphere_bin_size);
|
2018-05-14 20:17:06 +01:00
|
|
|
}
|
2018-08-18 17:59:33 +01:00
|
|
|
|
2018-08-29 18:28:21 +01:00
|
|
|
/* Move BootConfig. */
|
|
|
|
printf("[NXBOOT]: Moving BootConfig...\n");
|
|
|
|
nxboot_move_bootconfig();
|
|
|
|
|
2018-08-30 19:49:33 +01:00
|
|
|
/* Set 3.0.0/3.0.1/3.0.2 warmboot security check. */
|
|
|
|
if (MAILBOX_EXOSPHERE_CONFIGURATION->target_firmware == EXOSPHERE_TARGET_FIRMWARE_300) {
|
|
|
|
const package1loader_header_t *package1loader_header = (const package1loader_header_t *)package1loader;
|
|
|
|
if (!strcmp(package1loader_header->build_timestamp, "20170519101410"))
|
|
|
|
pmc->secure_scratch32 = 0xE3; /* Warmboot 3.0.0 security check.*/
|
|
|
|
else if (!strcmp(package1loader_header->build_timestamp, "20170710161758"))
|
|
|
|
pmc->secure_scratch32 = 0x104; /* Warmboot 3.0.1/3.0.2 security check. */
|
|
|
|
}
|
|
|
|
|
2018-08-18 17:59:33 +01:00
|
|
|
/* Clean up. */
|
|
|
|
free(package1loader);
|
|
|
|
if (loader_ctx->tsecfw_path[0] != '\0') {
|
|
|
|
free(tsec_fw);
|
|
|
|
}
|
|
|
|
if (loader_ctx->warmboot_path[0] != '\0') {
|
|
|
|
free(warmboot_fw);
|
|
|
|
}
|
|
|
|
free(package2);
|
|
|
|
|
|
|
|
/* Clear used keyslots. */
|
|
|
|
clear_aes_keyslot(KEYSLOT_SWITCH_PACKAGE2KEY);
|
|
|
|
clear_aes_keyslot(KEYSLOT_SWITCH_RNGKEY);
|
|
|
|
|
|
|
|
/* Lock keyslots. */
|
|
|
|
set_aes_keyslot_flags(KEYSLOT_SWITCH_MASTERKEY, 0xFF);
|
|
|
|
if (MAILBOX_EXOSPHERE_CONFIGURATION->target_firmware < EXOSPHERE_TARGET_FIRMWARE_400) {
|
|
|
|
set_aes_keyslot_flags(KEYSLOT_SWITCH_DEVICEKEY, 0xFF);
|
|
|
|
} else {
|
|
|
|
set_aes_keyslot_flags(KEYSLOT_SWITCH_4XOLDDEVICEKEY, 0xFF);
|
|
|
|
}
|
2018-05-05 22:55:40 +01:00
|
|
|
|
2018-08-18 17:59:33 +01:00
|
|
|
/* Finalize the GPU UCODE carveout. */
|
2018-08-19 17:09:30 +01:00
|
|
|
mc_config_carveout_finalize();
|
2018-08-18 17:59:33 +01:00
|
|
|
|
|
|
|
/* Lock AES keyslots. */
|
|
|
|
for (uint32_t i = 0; i < 16; i++)
|
2018-08-19 17:09:30 +01:00
|
|
|
set_aes_keyslot_flags(i, 0x15);
|
2018-08-18 17:59:33 +01:00
|
|
|
|
|
|
|
/* Lock RSA keyslots. */
|
2018-08-19 17:09:30 +01:00
|
|
|
for (uint32_t i = 0; i < 2; i++)
|
|
|
|
set_rsa_keyslot_flags(i, 1);
|
2018-08-18 17:59:33 +01:00
|
|
|
|
|
|
|
/* Lock the Security Engine. */
|
|
|
|
se->_0x4 = 0;
|
|
|
|
se->AES_KEY_READ_DISABLE_REG = 0;
|
|
|
|
se->RSA_KEY_READ_DISABLE_REG = 0;
|
|
|
|
se->_0x0 &= 0xFFFFFFFB;
|
|
|
|
|
2018-05-14 20:17:06 +01:00
|
|
|
/* Boot up Exosphère. */
|
2018-04-10 23:37:58 +01:00
|
|
|
MAILBOX_NX_BOOTLOADER_IS_SECMON_AWAKE = 0;
|
2018-05-15 19:24:37 +01:00
|
|
|
if (MAILBOX_EXOSPHERE_CONFIGURATION->target_firmware < EXOSPHERE_TARGET_FIRMWARE_400) {
|
2018-04-10 23:37:58 +01:00
|
|
|
MAILBOX_NX_BOOTLOADER_SETUP_STATE = NX_BOOTLOADER_STATE_LOADED_PACKAGE2;
|
|
|
|
} else {
|
2018-09-05 20:51:03 +01:00
|
|
|
MAILBOX_NX_BOOTLOADER_SETUP_STATE = NX_BOOTLOADER_STATE_DRAM_INITIALIZED_4X;
|
2018-04-10 23:37:58 +01:00
|
|
|
}
|
2018-08-18 17:59:33 +01:00
|
|
|
|
2018-08-23 19:57:01 +01:00
|
|
|
printf("[NXBOOT]: Powering on the CCPLEX...\n");
|
2018-08-18 17:59:33 +01:00
|
|
|
|
|
|
|
/* Display splash screen. */
|
|
|
|
display_splash_screen_bmp(loader_ctx->custom_splash_path);
|
|
|
|
|
2018-08-31 23:52:31 +01:00
|
|
|
/* Unmount everything. */
|
|
|
|
nxfs_unmount_all();
|
|
|
|
|
2018-08-23 19:57:01 +01:00
|
|
|
/* Terminate the display. */
|
|
|
|
display_end();
|
2018-08-18 17:59:33 +01:00
|
|
|
|
|
|
|
/* Boot CPU0. */
|
2018-08-30 19:49:33 +01:00
|
|
|
cluster_boot_cpu0((uint32_t)exosphere_memaddr);
|
2018-08-18 17:59:33 +01:00
|
|
|
|
|
|
|
/* Wait for Exosphère to wake up. */
|
2018-04-10 23:37:58 +01:00
|
|
|
while (MAILBOX_NX_BOOTLOADER_IS_SECMON_AWAKE == 0) {
|
2018-08-18 17:59:33 +01:00
|
|
|
udelay(1);
|
2018-04-10 23:37:58 +01:00
|
|
|
}
|
2018-08-18 17:59:33 +01:00
|
|
|
|
|
|
|
/* Signal Exosphère. */
|
2018-05-15 19:24:37 +01:00
|
|
|
if (MAILBOX_EXOSPHERE_CONFIGURATION->target_firmware < EXOSPHERE_TARGET_FIRMWARE_400) {
|
2018-04-10 23:37:58 +01:00
|
|
|
MAILBOX_NX_BOOTLOADER_SETUP_STATE = NX_BOOTLOADER_STATE_FINISHED;
|
|
|
|
} else {
|
|
|
|
MAILBOX_NX_BOOTLOADER_SETUP_STATE = NX_BOOTLOADER_STATE_FINISHED_4X;
|
|
|
|
}
|
2018-05-05 22:55:40 +01:00
|
|
|
|
2018-08-18 17:59:33 +01:00
|
|
|
/* Halt ourselves in waitevent state. */
|
2018-06-03 01:24:02 +01:00
|
|
|
while (1) {
|
2018-08-18 17:59:33 +01:00
|
|
|
FLOW_CTLR_HALT_COP_EVENTS_0 = 0x50000000;
|
2018-06-03 01:24:02 +01:00
|
|
|
}
|
2018-05-05 22:55:40 +01:00
|
|
|
}
|