1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-26 13:52:21 +00:00

emunand: Move all emulation code to fusee-secondary and simplify logic

This commit is contained in:
hexkyz 2019-04-07 19:02:01 +01:00
parent 9eb0b84ed1
commit 92816be055
8 changed files with 315 additions and 193 deletions

View file

@ -35,14 +35,7 @@ extern void (*__program_exit_callback)(int rc);
static void *g_framebuffer;
static char g_bct0_buffer[BCTO_MAX_SIZE];
typedef struct {
bool enabled;
char path[0x100];
} emunand_config_t;
#define CONFIG_LOG_LEVEL_KEY "log_level"
#define EMUNAND_ENABLED_KEY "emunand_enabled"
#define EMUNAND_PATH_KEY "emunand_path"
#define DEFAULT_BCT0_FOR_DEBUG \
"BCT0\n"\
@ -91,26 +84,6 @@ static int config_ini_handler(void *user, const char *section, const char *name,
return 1;
}
static int emunand_ini_handler(void *user, const char *section, const char *name, const char *value) {
emunand_config_t *emunand_cfg = (emunand_config_t *)user;
if (strcmp(section, "emunand") == 0) {
if (strcmp(name, EMUNAND_ENABLED_KEY) == 0) {
int tmp = 0;
sscanf(value, "%d", &tmp);
emunand_cfg->enabled = (tmp != 0);
}
if (strcmp(name, EMUNAND_PATH_KEY) == 0) {
strncpy(emunand_cfg->path, value, sizeof(emunand_cfg->path) - 1);
emunand_cfg->path[sizeof(emunand_cfg->path) - 1] = '\0';
} else {
return 0;
}
} else {
return 0;
}
return 1;
}
static void setup_env(void) {
g_framebuffer = (void *)0xC0000000;
@ -159,7 +132,6 @@ int main(void) {
stage2_args_t *stage2_args;
uint32_t stage2_version = 0;
ScreenLogLevel log_level = SCREEN_LOG_LEVEL_MANDATORY;
emunand_config_t emunand_cfg = {.enabled = false, .path = ""};
/* Override the global logging level. */
log_set_log_level(log_level);
@ -170,9 +142,8 @@ int main(void) {
/* Load the BCT0 configuration ini off of the SD. */
bct0 = load_config();
/* Extract the logging level and emunand settings from the BCT.ini file. */
if ((ini_parse_string(bct0, config_ini_handler, &log_level) < 0) ||
(ini_parse_string(bct0, emunand_ini_handler, &emunand_cfg) < 0)) {
/* Extract the logging level from the BCT.ini file. */
if (ini_parse_string(bct0, config_ini_handler, &log_level) < 0) {
fatal_error("Failed to parse BCT.ini!\n");
}
@ -190,9 +161,6 @@ int main(void) {
memcpy(&stage2_args->version, &stage2_version, 4);
memcpy(&stage2_args->log_level, &log_level, sizeof(log_level));
stage2_args->display_initialized = false;
stage2_args->emunand_enabled = emunand_cfg.enabled;
strncpy(stage2_args->emunand_path, emunand_cfg.path, sizeof(stage2_args->emunand_path) - 1);
stage2_args->emunand_path[sizeof(stage2_args->emunand_path) - 1] = '\0';
strcpy(stage2_args->bct0, bct0);
g_chainloader_argc = 2;

View file

@ -46,8 +46,6 @@ typedef struct {
uint32_t version;
ScreenLogLevel log_level;
bool display_initialized;
bool emunand_enabled;
char emunand_path[0x100];
char bct0[BCTO_MAX_SIZE];
} stage2_args_t;

View file

@ -51,8 +51,9 @@ static void setup_env(void) {
/* Set up exception handlers. */
setup_exception_handlers();
if (nxfs_mount_all(g_stage2_args->emunand_enabled, g_stage2_args->emunand_path) < 0) {
fatal_error("Failed to mount at least one partition: %s\n", strerror(errno));
/* Initialize the file system by mounting the SD card. */
if (nxfs_init() < 0) {
fatal_error("Failed to initialize the file system: %s\n", strerror(errno));
}
/* Train DRAM. */
@ -61,7 +62,7 @@ static void setup_env(void) {
static void cleanup_env(void) {
/* Unmount everything (this causes all open files to be flushed and closed) */
nxfs_unmount_all();
nxfs_end();
//console_end();
}

View file

@ -100,6 +100,26 @@ static const uint8_t dev_pkc_modulus[0x100] = {
0xD5, 0x52, 0xDA, 0xEC, 0x41, 0xA4, 0xAD, 0x7B, 0x36, 0x86, 0x18, 0xB4, 0x5B, 0xD1, 0x30, 0xBB
};
static int emunand_ini_handler(void *user, const char *section, const char *name, const char *value) {
emunand_config_t *emunand_cfg = (emunand_config_t *)user;
if (strcmp(section, "emunand") == 0) {
if (strcmp(name, EMUNAND_ENABLED_KEY) == 0) {
int tmp = 0;
sscanf(value, "%d", &tmp);
emunand_cfg->enabled = (tmp != 0);
}
if (strcmp(name, EMUNAND_PATH_KEY) == 0) {
strncpy(emunand_cfg->path, value, sizeof(emunand_cfg->path) - 1);
emunand_cfg->path[sizeof(emunand_cfg->path) - 1] = '\0';
} else {
return 0;
}
} else {
return 0;
}
return 1;
}
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;
@ -177,6 +197,44 @@ static uint32_t nxboot_get_target_firmware(const void *package1loader) {
}
}
static bool nxboot_configure_emunand() {
emunand_config_t emunand_cfg = {.enabled = false, .path = ""};
/* Load emunand settings from BCT.ini file. */
if (ini_parse_string(get_loader_ctx()->bct0, emunand_ini_handler, &emunand_cfg) < 0) {
fatal_error("[NXBOOT]: Failed to parse BCT.ini!\n");
}
if (emunand_cfg.enabled) {
bool do_nand_backup = false;
/* TODO: Check if the supplied path is valid. */
/* TODO: Check if all emunand image files are present. */
if (do_nand_backup) {
/* Mount real NAND. */
if (nxfs_mount_emmc() < 0) {
fatal_error("[NXBOOT]: Failed to mount eMMC!\n");
}
/* TODO: Read real NAND and create a backup image. */
/* Unmount real NAND. */
if (nxfs_unmount_emmc() < 0) {
fatal_error("[NXBOOT]: Failed to unmount eMMC!\n");
}
}
/* Mount emulated NAND. */
if (nxfs_mount_emu_emmc(emunand_cfg.path) < 0) {
fatal_error("[NXBOOT]: Failed to mount emulated eMMC!\n");
}
}
return emunand_cfg.enabled;
}
static void nxboot_configure_exosphere(uint32_t target_firmware, unsigned int keygen_type) {
exosphere_config_t exo_cfg = {0};
@ -335,6 +393,13 @@ uint32_t nxboot_main(void) {
FILE *boot0, *pk2file;
void *exosphere_memaddr;
/* Configure emunand or mount the real NAND. */
if (!nxboot_configure_emunand()) {
if (nxfs_mount_emmc() < 0) {
fatal_error("[NXBOOT]: Failed to mount eMMC!\n");
}
}
/* Allocate memory for reading Package2. */
package2 = memalign(0x1000, PACKAGE2_SIZE_MAX);
if (package2 == NULL) {
@ -601,7 +666,7 @@ uint32_t nxboot_main(void) {
print(SCREEN_LOG_LEVEL_INFO, "[NXBOOT]: Powering on the CCPLEX...\n");
/* Unmount everything. */
nxfs_unmount_all();
nxfs_end();
/* Return the memory address for booting CPU0. */
return (uint32_t)exosphere_memaddr;

View file

@ -19,6 +19,14 @@
#include "utils.h"
#define EMUNAND_ENABLED_KEY "emunand_enabled"
#define EMUNAND_PATH_KEY "emunand_path"
typedef struct {
bool enabled;
char path[0x100];
} emunand_config_t;
#define MAILBOX_NX_BOOTLOADER_BASE_100_620 0x40002E00
#define MAILBOX_NX_BOOTLOADER_BASE_700 0x40000000
#define MAILBOX_NX_BOOTLOADER_BASE(targetfw) ((targetfw >= ATMOSPHERE_TARGET_FIRMWARE_700) ? (MAILBOX_NX_BOOTLOADER_BASE_700) : (MAILBOX_NX_BOOTLOADER_BASE_100_620))

View file

@ -30,6 +30,10 @@ static bool g_ahb_redirect_enabled = false;
static bool g_sd_device_initialized = false;
static bool g_emmc_device_initialized = false;
static bool g_fsdev_ready = false;
static bool g_rawdev_ready = false;
static bool g_emudev_ready = false;
static sdmmc_t g_sd_sdmmc = {0};
static sdmmc_t g_emmc_sdmmc = {0};
@ -264,10 +268,9 @@ static int nxfs_mount_partition_gpt_callback(const efi_entry_t *entry, void *par
return 0;
}
int nxfs_mount_all(bool emunand_enabled, const char *emunand_path) {
int nxfs_mount_sd() {
device_partition_t model;
int rc;
FILE *rawnand;
/* Setup a template for the SD card. */
model = g_mmc_devpart_template;
@ -289,7 +292,99 @@ int nxfs_mount_all(bool emunand_enabled, const char *emunand_path) {
return -1;
}
if (emunand_enabled) {
/* All fs devices are ready. */
if (rc == 0) {
g_fsdev_ready = true;
}
return rc;
}
int nxfs_mount_emmc() {
device_partition_t model;
int rc;
FILE *rawnand;
/* Setup a template for boot0. */
model = g_mmc_devpart_template;
model.device_struct = &g_emmc_boot0_mmcpart;
model.start_sector = 0;
model.num_sectors = 0x184000 / model.sector_size;
/* Mount boot0 device. */
rc = rawdev_mount_device("boot0", &model, true);
if (rc == -1) {
return -1;
}
/* Register boot0 device. */
rc = rawdev_register_device("boot0");
if (rc == -1) {
return -1;
}
/* Setup a template for boot1. */
model = g_mmc_devpart_template;
model.device_struct = &g_emmc_boot1_mmcpart;
model.start_sector = 0;
model.num_sectors = 0x80000 / model.sector_size;
/* Mount boot1 device. */
rc = rawdev_mount_device("boot1", &model, false);
if (rc == -1) {
return -1;
}
/* Don't register boot1 for now. */
/* Setup a template for raw NAND. */
model = g_mmc_devpart_template;
model.device_struct = &g_emmc_user_mmcpart;
model.start_sector = 0;
model.num_sectors = (256ull << 30) / model.sector_size;
/* Mount raw NAND device. */
rc = rawdev_mount_device("rawnand", &model, false);
if (rc == -1) {
return -1;
}
/* Register raw NAND device. */
rc = rawdev_register_device("rawnand");
if (rc == -1) {
return -1;
}
/* Open raw NAND device. */
rawnand = fopen("rawnand:/", "rb");
if (rawnand == NULL) {
return -1;
}
/* Iterate the GPT and mount each raw NAND partition. */
rc = gpt_iterate_through_entries(rawnand, model.sector_size, nxfs_mount_partition_gpt_callback, &model);
/* Close raw NAND device. */
fclose(rawnand);
/* All raw devices are ready. */
if (rc == 0) {
g_rawdev_ready = true;
}
return rc;
}
int nxfs_mount_emu_emmc(const char *emunand_path) {
device_partition_t model;
int rc;
FILE *rawnand;
/* Setup emunand paths. */
char emu_boot0_path[0x100];
char emu_boot1_path[0x100];
@ -364,76 +459,58 @@ int nxfs_mount_all(bool emunand_enabled, const char *emunand_path) {
/* Close emulated raw NAND device. */
fclose(rawnand);
} else {
/* Setup a template for boot0. */
model = g_mmc_devpart_template;
model.device_struct = &g_emmc_boot0_mmcpart;
model.start_sector = 0;
model.num_sectors = 0x184000 / model.sector_size;
/* Mount boot0 device. */
rc = rawdev_mount_device("boot0", &model, true);
if (rc == -1) {
return -1;
/* All emulated devices are ready. */
if (rc == 0) {
g_emudev_ready = true;
}
/* Register boot0 device. */
rc = rawdev_register_device("boot0");
if (rc == -1) {
return -1;
return rc;
}
/* Setup a template for boot1. */
model = g_mmc_devpart_template;
model.device_struct = &g_emmc_boot1_mmcpart;
model.start_sector = 0;
model.num_sectors = 0x80000 / model.sector_size;
int nxfs_unmount_sd() {
int rc = 0;
/* Mount boot1 device. */
rc = rawdev_mount_device("boot1", &model, false);
if (rc == -1) {
return -1;
/* Unmount all fs devices. */
if (g_fsdev_ready) {
rc = fsdev_unmount_all();
g_fsdev_ready = false;
}
/* Don't register boot1 for now. */
/* Setup a template for raw NAND. */
model = g_mmc_devpart_template;
model.device_struct = &g_emmc_user_mmcpart;
model.start_sector = 0;
model.num_sectors = (256ull << 30) / model.sector_size;
/* Mount raw NAND device. */
rc = rawdev_mount_device("rawnand", &model, false);
if (rc == -1) {
return -1;
return rc;
}
/* Register raw NAND device. */
rc = rawdev_register_device("rawnand");
if (rc == -1) {
return -1;
int nxfs_unmount_emmc() {
int rc = 0;
/* Unmount all raw devices. */
if (g_rawdev_ready) {
rc = rawdev_unmount_all();
g_rawdev_ready = false;
}
/* Open raw NAND device. */
rawnand = fopen("rawnand:/", "rb");
if (rawnand == NULL) {
return -1;
return rc;
}
/* Iterate the GPT and mount each raw NAND partition. */
rc = gpt_iterate_through_entries(rawnand, model.sector_size, nxfs_mount_partition_gpt_callback, &model);
int nxfs_unmount_emu_emmc() {
int rc = 0;
/* Close raw NAND device. */
fclose(rawnand);
/* Unmount all emulated devices. */
if (g_emudev_ready) {
rc = emudev_unmount_all();
g_emudev_ready = false;
}
/* Set the default file system device. */
return rc;
}
int nxfs_init() {
int rc;
/* Mount and register the SD card. */
rc = nxfs_mount_sd();
/* Set the SD card as the default file system device. */
if (rc == 0) {
rc = fsdev_set_default_device("sdmc");
}
@ -441,6 +518,6 @@ int nxfs_mount_all(bool emunand_enabled, const char *emunand_path) {
return rc;
}
int nxfs_unmount_all() {
return ((fsdev_unmount_all() || rawdev_unmount_all() || emudev_unmount_all()) ? -1 : 0);
int nxfs_end() {
return ((nxfs_unmount_sd() || nxfs_unmount_emmc() || nxfs_unmount_emu_emmc()) ? -1 : 0);
}

View file

@ -21,7 +21,14 @@
#include "raw_dev.h"
#include "emu_dev.h"
int nxfs_mount_all(bool emunand_enabled, const char *emunand_path);
int nxfs_unmount_all();
int nxfs_init();
int nxfs_end();
int nxfs_mount_sd();
int nxfs_mount_emmc();
int nxfs_mount_emu_emmc(const char *emunand_path);
int nxfs_unmount_sd();
int nxfs_unmount_emmc();
int nxfs_unmount_emu_emmc();
#endif

View file

@ -32,8 +32,6 @@ typedef struct {
uint32_t version;
ScreenLogLevel log_level;
bool display_initialized;
bool emunand_enabled;
char emunand_path[0x100];
char bct0[BCTO_MAX_SIZE];
} stage2_args_t;