From d349bdb1f848d30fdd28ae485ea8b0fac261ffa3 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sun, 20 Jan 2019 02:38:45 -0800 Subject: [PATCH] fusee/exo: build warmboot, use instead of Nintendo's. --- exosphere/Makefile | 11 ++-- fusee/fusee-secondary/Makefile | 4 +- fusee/fusee-secondary/src/lp0.h | 52 ++++++++++++++++++ fusee/fusee-secondary/src/nxboot.c | 87 ++++++++++++++++++++++-------- 4 files changed, 128 insertions(+), 26 deletions(-) create mode 100644 fusee/fusee-secondary/src/lp0.h diff --git a/exosphere/Makefile b/exosphere/Makefile index 4eb2d4491..cb06f6f5f 100644 --- a/exosphere/Makefile +++ b/exosphere/Makefile @@ -75,6 +75,7 @@ export TOPDIR := $(CURDIR) export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ $(foreach dir,$(DATA),$(CURDIR)/$(dir)) \ + $(TOPDIR)/lp0fw \ $(TOPDIR)/sc7fw export DEPSDIR := $(CURDIR)/$(BUILD) @@ -82,7 +83,7 @@ export DEPSDIR := $(CURDIR)/$(BUILD) CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) -BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) sc7fw.bin +BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) sc7fw.bin lp0fw.bin #--------------------------------------------------------------------------------- # use CXX for linking C++ projects, CC for standard C @@ -109,7 +110,7 @@ export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) -.PHONY: $(BUILD) build_sc7fw clean all +.PHONY: $(BUILD) build_sc7fw build_lp0fw clean all #--------------------------------------------------------------------------------- all: $(BUILD) @@ -117,7 +118,10 @@ all: $(BUILD) check_sc7fw: @$(MAKE) -C sc7fw all -$(BUILD): check_sc7fw +check_lp0fw: + @$(MAKE) -C lp0fw all + +$(BUILD): check_sc7fw check_lp0fw @[ -d $@ ] || mkdir -p $@ @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile @@ -125,6 +129,7 @@ $(BUILD): check_sc7fw clean: @echo clean ... @$(MAKE) -C $(TOPDIR)/sc7fw clean + @$(MAKE) -C $(TOPDIR)/lp0fw clean @rm -fr $(BUILD) $(TARGET).bin $(TARGET).elf diff --git a/fusee/fusee-secondary/Makefile b/fusee/fusee-secondary/Makefile index c2c849f4d..90e963b63 100644 --- a/fusee/fusee-secondary/Makefile +++ b/fusee/fusee-secondary/Makefile @@ -79,7 +79,7 @@ export TOPDIR := $(CURDIR) export KIPDIRS := $(AMS)/stratosphere/loader $(AMS)/stratosphere/pm $(AMS)/stratosphere/sm $(AMS)/stratosphere/boot $(AMS)/stratosphere/fs_mitm export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ $(foreach dir,$(DATA),$(CURDIR)/$(dir)) \ - $(AMS)/exosphere $(AMS)/thermosphere $(KIPDIRS) + $(AMS)/exosphere $(AMS)/exosphere/lp0fw $(AMS)/thermosphere $(KIPDIRS) export DEPSDIR := $(CURDIR)/$(BUILD) @@ -87,7 +87,7 @@ CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) KIPFILES := loader.kip pm.kip sm.kip fs_mitm.kip boot_100.kip boot_200.kip -BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) exosphere.bin thermosphere.bin splash_screen.bmp $(KIPFILES) +BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) exosphere.bin lp0fw.bin thermosphere.bin splash_screen.bmp $(KIPFILES) #--------------------------------------------------------------------------------- # use CXX for linking C++ projects, CC for standard C diff --git a/fusee/fusee-secondary/src/lp0.h b/fusee/fusee-secondary/src/lp0.h new file mode 100644 index 000000000..bcb09fa63 --- /dev/null +++ b/fusee/fusee-secondary/src/lp0.h @@ -0,0 +1,52 @@ +/* + * 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 . + */ + +#ifndef FUSEE_WARMBOOT_BIN_LP0_H +#define FUSEE_WARMBOOT_BIN_LP0_H + +#include "utils.h" + +/* WBT0 */ +#define WARMBOOT_MAGIC 0x30544257 + +typedef struct { + uint32_t entrypoint_insn; + uint32_t magic; + uint32_t target_firmware; + uint32_t padding[2]; +} warmboot_metadata_t; + +typedef struct { + uint32_t warmboot_fw_size; + uint32_t dst_address; + uint32_t entrypoint; + uint32_t code_size; +} warmboot_nv_header_t; + +typedef struct { + uint32_t warmboot_fw_size; + uint32_t _0x4[3]; + uint8_t rsa_modulus[0x100]; + uint8_t _0x110[0x10]; + uint8_t rsa_signature[0x100]; + uint8_t _0x220[0x10]; + warmboot_nv_header_t nv_header; + warmboot_metadata_t ams_metadata; +} warmboot_ams_header_t; + +_Static_assert(sizeof(warmboot_ams_header_t) == 0x254, "Warmboot header definition!"); + +#endif diff --git a/fusee/fusee-secondary/src/nxboot.c b/fusee/fusee-secondary/src/nxboot.c index 37e4d293c..6879d4ec8 100644 --- a/fusee/fusee-secondary/src/nxboot.c +++ b/fusee/fusee-secondary/src/nxboot.c @@ -41,6 +41,7 @@ #include "package2.h" #include "smmu.h" #include "tsec.h" +#include "lp0.h" #include "loader.h" #include "splash_screen.h" #include "exocfg.h" @@ -50,10 +51,52 @@ #define u8 uint8_t #define u32 uint32_t #include "exosphere_bin.h" +#include "lp0fw_bin.h" #include "lib/log.h" #undef u8 #undef u32 +extern const uint8_t lp0fw_bin[]; +extern const uint32_t lp0fw_bin_size; + +static const uint8_t retail_pkc_modulus[0x100] = { + 0xF7, 0x86, 0x47, 0xAB, 0x71, 0x89, 0x81, 0xB5, 0xCF, 0x0C, 0xB0, 0xE8, 0x48, 0xA7, 0xFD, 0xAD, + 0xCB, 0x4E, 0x4A, 0x52, 0x0B, 0x1A, 0x8E, 0xDE, 0x41, 0x87, 0x6F, 0xB7, 0x31, 0x05, 0x5F, 0xAA, + 0xEA, 0x97, 0x76, 0x21, 0x20, 0x2B, 0x40, 0x48, 0x76, 0x55, 0x35, 0x03, 0xFE, 0x7F, 0x67, 0x62, + 0xFD, 0x4E, 0xE1, 0x22, 0xF8, 0xF0, 0x97, 0x39, 0xEF, 0xEA, 0x47, 0x89, 0x3C, 0xDB, 0xF0, 0x02, + 0xAD, 0x0C, 0x96, 0xCA, 0x82, 0xAB, 0xB3, 0xCB, 0x98, 0xC8, 0xDC, 0xC6, 0xAC, 0x5C, 0x93, 0x3B, + 0x84, 0x3D, 0x51, 0x91, 0x9E, 0xC1, 0x29, 0x22, 0x95, 0xF0, 0xA1, 0x51, 0xBA, 0xAF, 0x5D, 0xC3, + 0xAB, 0x04, 0x1B, 0x43, 0x61, 0x7D, 0xEA, 0x65, 0x95, 0x24, 0x3C, 0x51, 0x3E, 0x8F, 0xDB, 0xDB, + 0xC1, 0xC4, 0x2D, 0x04, 0x29, 0x5A, 0xD7, 0x34, 0x6B, 0xCC, 0xF1, 0x06, 0xF9, 0xC9, 0xE1, 0xF9, + 0x61, 0x52, 0xE2, 0x05, 0x51, 0xB1, 0x3D, 0x88, 0xF9, 0xA9, 0x27, 0xA5, 0x6F, 0x4D, 0xE7, 0x22, + 0x48, 0xA5, 0xF8, 0x12, 0xA2, 0xC2, 0x5A, 0xA0, 0xBF, 0xC8, 0x76, 0x4B, 0x66, 0xFE, 0x1C, 0x73, + 0x00, 0x29, 0x26, 0xCD, 0x18, 0x4F, 0xC2, 0xB0, 0x51, 0x77, 0x2E, 0x91, 0x09, 0x1B, 0x41, 0x5D, + 0x89, 0x5E, 0xEE, 0x24, 0x22, 0x47, 0xE5, 0xE5, 0xF1, 0x86, 0x99, 0x67, 0x08, 0x28, 0x42, 0xF0, + 0x58, 0x62, 0x54, 0xC6, 0x5B, 0xDC, 0xE6, 0x80, 0x85, 0x6F, 0xE2, 0x72, 0xB9, 0x7E, 0x36, 0x64, + 0x48, 0x85, 0x10, 0xA4, 0x75, 0x38, 0x79, 0x76, 0x8B, 0x51, 0xD5, 0x87, 0xC3, 0x02, 0xC9, 0x1B, + 0x93, 0x22, 0x49, 0xEA, 0xAB, 0xA0, 0xB5, 0xB1, 0x3C, 0x10, 0xC4, 0x71, 0xF0, 0xF1, 0x81, 0x1A, + 0x3A, 0x9C, 0xFC, 0x51, 0x61, 0xB1, 0x4B, 0x18, 0xB2, 0x3D, 0xAA, 0xD6, 0xAC, 0x72, 0x26, 0xB7 +}; + +static const uint8_t dev_pkc_modulus[0x100] = { + 0x37, 0x84, 0x14, 0xB3, 0x78, 0xA4, 0x7F, 0xD8, 0x71, 0x45, 0xCD, 0x90, 0x51, 0x51, 0xBF, 0x2C, + 0x27, 0x03, 0x30, 0x46, 0xBE, 0x8F, 0x99, 0x3E, 0x9F, 0x36, 0x4D, 0xEB, 0xF7, 0x0E, 0x81, 0x7F, + 0xE4, 0x6B, 0xA8, 0x42, 0x8A, 0xA5, 0x4F, 0x76, 0xCC, 0xCB, 0xC5, 0x31, 0xA8, 0x5A, 0x70, 0x51, + 0x34, 0xBF, 0x1E, 0x8D, 0x6E, 0xCF, 0x05, 0x84, 0xCF, 0x8B, 0xE5, 0x9C, 0x3A, 0xA5, 0xCD, 0x1A, + 0x9C, 0xAC, 0x59, 0x30, 0x09, 0x21, 0x3C, 0xBE, 0x07, 0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0xD1, 0x30, 0xBB +}; + 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; int tmp = 0; @@ -277,8 +320,6 @@ uint32_t nxboot_main(void) { void *warmboot_memaddr; void *package1loader; size_t package1loader_size; - package1_header_t *package1; - size_t package1_size; uint32_t available_revision; FILE *boot0, *pk2file; void *exosphere_memaddr; @@ -403,7 +444,7 @@ uint32_t nxboot_main(void) { nxboot_set_bootreason(); } - /* Read the warmboot firmware from a file, otherwise from PK1. */ + /* Read the warmboot firmware from a file, otherwise from Atmosphere's implementation. */ if (loader_ctx->warmboot_path[0] != '\0') { warmboot_fw_size = get_file_size(loader_ctx->warmboot_path); if (warmboot_fw_size == 0) { @@ -420,30 +461,34 @@ uint32_t nxboot_main(void) { fatal_error("[NXBOOT]: Could not read the warmboot firmware from %s!\n", loader_ctx->warmboot_path); } } else { - if (target_firmware >= ATMOSPHERE_TARGET_FIRMWARE_620) { - /* Package1 was decrypted during TSEC emulation. */ - const uint8_t *package1_hdr = (const uint8_t *)package1loader + 0x7000 - 0x20; - package1 = (package1_header_t *)(package1_hdr + 0x20); - package1_size = *(uint32_t *)package1_hdr; - warmboot_fw = package1_get_warmboot_fw(package1); - warmboot_fw_size = package1->warmboot_size; - } else { - /* Decrypt package1 and extract the warmboot firmware. */ - uint8_t ctr[16]; - package1_size = package1_get_encrypted_package1(&package1, ctr, package1loader, package1loader_size); - if (package1_decrypt(package1, package1_size, ctr)) { - warmboot_fw = package1_get_warmboot_fw(package1); - warmboot_fw_size = package1->warmboot_size; - } else { - warmboot_fw = NULL; - warmboot_fw_size = 0; - } + /* Use Atmosphere's warmboot firmware implementation. */ + warmboot_fw_size = lp0fw_bin_size; + warmboot_fw = malloc(warmboot_fw_size); + + if (warmboot_fw == NULL) { + fatal_error("[NXBOOT]: Out of memory!\n"); } + memcpy(warmboot_fw, lp0fw_bin, warmboot_fw_size); + if (warmboot_fw_size == 0) { fatal_error("[NXBOOT]: Could not read the warmboot firmware from Package1!\n"); } } + + /* Patch warmboot firmware for atmosphere. */ + if (warmboot_fw != NULL && warmboot_fw_size >= sizeof(warmboot_ams_header_t)) { + warmboot_ams_header_t *ams_header = (warmboot_ams_header_t *)warmboot_fw; + if (ams_header->ams_metadata.magic == WARMBOOT_MAGIC) { + /* Set target firmware */ + ams_header->ams_metadata.target_firmware = target_firmware; + + /* Set RSA modulus */ + const uint8_t *pkc_modulus = fuse_get_retail_type() != 0 ? retail_pkc_modulus : dev_pkc_modulus; + memcpy(ams_header->rsa_modulus, pkc_modulus, sizeof(ams_header->rsa_modulus)); + } + } + /* Select the right address for the warmboot firmware. */ if (MAILBOX_EXOSPHERE_CONFIGURATION->target_firmware < ATMOSPHERE_TARGET_FIRMWARE_400) {