2018-05-05 16:35:00 +00:00
|
|
|
#include <stdio.h>
|
2018-05-05 22:45:30 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2018-05-15 14:09:06 +00:00
|
|
|
#include "exocfg.h"
|
2018-04-12 03:56:11 +00:00
|
|
|
#include "utils.h"
|
|
|
|
#include "package2.h"
|
|
|
|
#include "stratosphere.h"
|
2018-05-13 21:49:50 +00:00
|
|
|
#include "fs_utils.h"
|
2018-04-12 03:56:11 +00:00
|
|
|
|
2018-05-18 22:54:38 +00:00
|
|
|
#define u8 uint8_t
|
|
|
|
#define u32 uint32_t
|
|
|
|
#include "loader_kip.h"
|
|
|
|
#include "pm_kip.h"
|
|
|
|
#include "sm_kip.h"
|
|
|
|
#include "boot_100_kip.h"
|
|
|
|
#include "boot_200_kip.h"
|
|
|
|
#undef u8
|
|
|
|
#undef u32
|
|
|
|
|
2018-05-08 16:46:54 +00:00
|
|
|
static ini1_header_t *g_stratosphere_ini1 = NULL;
|
2018-04-12 03:56:11 +00:00
|
|
|
|
2018-08-31 20:10:06 +00:00
|
|
|
static bool g_stratosphere_loader_enabled = true;
|
|
|
|
static bool g_stratosphere_sm_enabled = true;
|
|
|
|
static bool g_stratosphere_pm_enabled = false;
|
|
|
|
static bool g_stratosphere_boot_enabled = false;
|
|
|
|
|
2018-05-15 17:47:10 +00:00
|
|
|
extern const uint8_t boot_100_kip[], boot_200_kip[];
|
2018-05-15 10:38:07 +00:00
|
|
|
extern const uint8_t loader_kip[], pm_kip[], sm_kip[];
|
2018-05-15 17:47:10 +00:00
|
|
|
extern const uint32_t boot_100_kip_size, boot_200_kip_size;
|
2018-05-15 10:38:07 +00:00
|
|
|
extern const uint32_t loader_kip_size, pm_kip_size, sm_kip_size;
|
|
|
|
|
|
|
|
/* GCC doesn't consider the size as const... we have to write it ourselves. */
|
|
|
|
|
2018-05-15 14:09:06 +00:00
|
|
|
ini1_header_t *stratosphere_get_ini1(uint32_t target_firmware) {
|
2018-08-31 20:10:06 +00:00
|
|
|
const uint8_t *boot_kip = NULL;
|
2018-05-15 17:47:10 +00:00
|
|
|
uint32_t boot_kip_size = 0;
|
2018-08-31 20:10:06 +00:00
|
|
|
uint32_t num_processes = 0;
|
2018-05-15 10:38:07 +00:00
|
|
|
uint8_t *data;
|
|
|
|
|
2018-05-08 16:46:54 +00:00
|
|
|
if (g_stratosphere_ini1 != NULL) {
|
|
|
|
return g_stratosphere_ini1;
|
|
|
|
}
|
2018-05-05 22:45:30 +00:00
|
|
|
|
2018-05-15 14:09:06 +00:00
|
|
|
if (target_firmware <= EXOSPHERE_TARGET_FIRMWARE_100) {
|
2018-08-31 20:10:06 +00:00
|
|
|
boot_kip = boot_100_kip;
|
2018-05-15 17:47:10 +00:00
|
|
|
boot_kip_size = boot_100_kip_size;
|
2018-05-15 14:09:06 +00:00
|
|
|
} else {
|
2018-08-31 20:10:06 +00:00
|
|
|
boot_kip = boot_200_kip;
|
2018-05-15 17:47:10 +00:00
|
|
|
boot_kip_size = boot_200_kip_size;
|
2018-05-15 14:09:06 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 20:10:06 +00:00
|
|
|
size_t size = sizeof(ini1_header_t);
|
|
|
|
|
|
|
|
/* Calculate our processes' sizes. */
|
|
|
|
if (g_stratosphere_loader_enabled) {
|
|
|
|
size += loader_kip_size;
|
|
|
|
num_processes++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_stratosphere_pm_enabled) {
|
|
|
|
size += pm_kip_size;
|
|
|
|
num_processes++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_stratosphere_sm_enabled) {
|
|
|
|
size += sm_kip_size;
|
|
|
|
num_processes++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_stratosphere_boot_enabled) {
|
|
|
|
size += boot_kip_size;
|
|
|
|
num_processes++;
|
|
|
|
}
|
|
|
|
|
2018-05-08 16:46:54 +00:00
|
|
|
g_stratosphere_ini1 = (ini1_header_t *)malloc(size);
|
2018-04-12 03:56:11 +00:00
|
|
|
|
2018-05-21 15:45:05 +00:00
|
|
|
if (g_stratosphere_ini1 == NULL) {
|
2018-05-20 14:18:48 +00:00
|
|
|
fatal_error("stratosphere_get_ini1: out of memory!\n");
|
2018-04-12 03:56:11 +00:00
|
|
|
}
|
2018-05-08 16:46:54 +00:00
|
|
|
|
|
|
|
g_stratosphere_ini1->magic = MAGIC_INI1;
|
2018-05-15 10:38:07 +00:00
|
|
|
g_stratosphere_ini1->size = size;
|
2018-08-31 20:10:06 +00:00
|
|
|
g_stratosphere_ini1->num_processes = num_processes;
|
2018-05-08 16:46:54 +00:00
|
|
|
g_stratosphere_ini1->_0xC = 0;
|
2018-05-05 16:35:00 +00:00
|
|
|
|
2018-05-15 10:38:07 +00:00
|
|
|
data = g_stratosphere_ini1->kip_data;
|
|
|
|
|
|
|
|
/* Copy our processes. */
|
2018-08-31 20:10:06 +00:00
|
|
|
if (g_stratosphere_loader_enabled) {
|
|
|
|
memcpy(data, loader_kip, loader_kip_size);
|
|
|
|
data += loader_kip_size;
|
|
|
|
}
|
2018-05-15 10:38:07 +00:00
|
|
|
|
2018-08-31 20:10:06 +00:00
|
|
|
if (g_stratosphere_pm_enabled) {
|
|
|
|
memcpy(data, pm_kip, pm_kip_size);
|
|
|
|
data += pm_kip_size;
|
|
|
|
}
|
2018-05-15 10:38:07 +00:00
|
|
|
|
2018-08-31 20:10:06 +00:00
|
|
|
if (g_stratosphere_sm_enabled) {
|
|
|
|
memcpy(data, sm_kip, sm_kip_size);
|
|
|
|
data += sm_kip_size;
|
|
|
|
}
|
2018-05-15 10:38:07 +00:00
|
|
|
|
2018-08-31 20:10:06 +00:00
|
|
|
if (g_stratosphere_boot_enabled) {
|
|
|
|
memcpy(data, boot_kip, boot_kip_size);
|
|
|
|
data += boot_kip_size;
|
|
|
|
}
|
2018-08-18 16:59:33 +00:00
|
|
|
|
2018-05-08 16:46:54 +00:00
|
|
|
return g_stratosphere_ini1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stratosphere_free_ini1(void) {
|
|
|
|
free(g_stratosphere_ini1);
|
|
|
|
g_stratosphere_ini1 = NULL;
|
2018-04-12 03:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Merges some number of INI1s into a single INI1. It's assumed that the INIs are in order of preference. */
|
2018-05-08 16:46:54 +00:00
|
|
|
ini1_header_t *stratosphere_merge_inis(ini1_header_t **inis, size_t num_inis) {
|
2018-05-07 21:32:45 +00:00
|
|
|
char sd_path[0x100] = {0};
|
2018-05-08 16:46:54 +00:00
|
|
|
uint32_t total_num_processes = 0;
|
|
|
|
|
2018-04-12 03:56:11 +00:00
|
|
|
/* Validate all ini headers. */
|
2018-05-08 16:46:54 +00:00
|
|
|
for (size_t i = 0; i < num_inis; i++) {
|
2018-04-12 03:56:11 +00:00
|
|
|
if (inis[i] == NULL || inis[i]->magic != MAGIC_INI1 || inis[i]->num_processes > INI1_MAX_KIPS) {
|
2018-05-20 14:18:48 +00:00
|
|
|
fatal_error("INI1s[%d] section appears to not contain an INI1!\n", i);
|
2018-05-08 16:46:54 +00:00
|
|
|
} else {
|
|
|
|
total_num_processes += inis[i]->num_processes;
|
2018-04-12 03:56:11 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-05 16:35:00 +00:00
|
|
|
|
2018-05-08 16:46:54 +00:00
|
|
|
if (total_num_processes > INI1_MAX_KIPS) {
|
2018-05-20 14:18:48 +00:00
|
|
|
fatal_error("The resulting INI1 would have too many KIPs!\n");
|
2018-05-08 16:46:54 +00:00
|
|
|
}
|
|
|
|
|
2018-04-12 03:56:11 +00:00
|
|
|
uint64_t process_list[INI1_MAX_KIPS] = {0};
|
2018-05-08 16:46:54 +00:00
|
|
|
ini1_header_t *merged = (ini1_header_t *)malloc(PACKAGE2_SIZE_MAX); /* because of SD file overrides */
|
|
|
|
|
|
|
|
if (merged == NULL) {
|
2018-05-20 14:18:48 +00:00
|
|
|
fatal_error("stratosphere_merge_inis: out of memory!\n");
|
2018-05-08 16:46:54 +00:00
|
|
|
}
|
2018-05-05 16:35:00 +00:00
|
|
|
|
2018-04-12 03:56:11 +00:00
|
|
|
merged->magic = MAGIC_INI1;
|
|
|
|
merged->num_processes = 0;
|
|
|
|
merged->_0xC = 0;
|
|
|
|
size_t remaining_size = PACKAGE2_SIZE_MAX - sizeof(ini1_header_t);
|
2018-05-08 16:46:54 +00:00
|
|
|
size_t read_size;
|
2018-05-05 16:35:00 +00:00
|
|
|
|
2018-04-12 03:56:11 +00:00
|
|
|
unsigned char *current_dst_kip = merged->kip_data;
|
2018-05-05 16:35:00 +00:00
|
|
|
|
2018-04-12 03:56:11 +00:00
|
|
|
/* Actually merge into the inis. */
|
2018-05-08 16:46:54 +00:00
|
|
|
for (size_t i = 0; i < num_inis; i++) {
|
|
|
|
size_t offset = 0;
|
|
|
|
for (size_t p = 0; p < (size_t)inis[i]->num_processes; p++) {
|
2018-04-12 03:56:11 +00:00
|
|
|
kip1_header_t *current_kip = (kip1_header_t *)(inis[i]->kip_data + offset);
|
|
|
|
if (current_kip->magic != MAGIC_KIP1) {
|
2018-05-20 14:18:48 +00:00
|
|
|
fatal_error("INI1s[%zu][%zu] appears not to be a KIP1!\n", i, p);
|
2018-04-12 03:56:11 +00:00
|
|
|
}
|
2018-05-05 16:35:00 +00:00
|
|
|
|
2018-06-04 21:04:44 +00:00
|
|
|
offset += kip1_get_size_from_header(current_kip);
|
|
|
|
|
2018-04-12 03:56:11 +00:00
|
|
|
bool already_loaded = false;
|
2018-05-08 16:46:54 +00:00
|
|
|
for (uint32_t j = 0; j < merged->num_processes; j++) {
|
2018-04-12 03:56:11 +00:00
|
|
|
if (process_list[j] == current_kip->title_id) {
|
|
|
|
already_loaded = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (already_loaded) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-05 16:35:00 +00:00
|
|
|
|
2018-04-12 04:10:02 +00:00
|
|
|
/* TODO: What folder should these be read out of? */
|
2018-05-08 16:46:54 +00:00
|
|
|
snprintf(sd_path, sizeof(sd_path), "atmosphere/titles/%016llX/%016llX.kip", current_kip->title_id, current_kip->title_id);
|
2018-05-05 16:35:00 +00:00
|
|
|
|
2018-04-12 03:56:11 +00:00
|
|
|
/* Try to load an override KIP from SD, if possible. */
|
2018-05-13 21:49:50 +00:00
|
|
|
read_size = read_from_file(current_dst_kip, remaining_size, sd_path);
|
2018-05-08 16:46:54 +00:00
|
|
|
if (read_size != 0) {
|
2018-04-12 03:56:11 +00:00
|
|
|
kip1_header_t *sd_kip = (kip1_header_t *)(current_dst_kip);
|
2018-05-08 16:46:54 +00:00
|
|
|
if (read_size < sizeof(kip1_header_t) || sd_kip->magic != MAGIC_KIP1) {
|
2018-05-20 14:18:48 +00:00
|
|
|
fatal_error("%s is not a KIP1?\n", sd_path);
|
2018-04-12 03:56:11 +00:00
|
|
|
} else if (sd_kip->title_id != current_kip->title_id) {
|
2018-05-20 14:18:48 +00:00
|
|
|
fatal_error("%s has wrong Title ID!\n", sd_path);
|
2018-04-12 03:56:11 +00:00
|
|
|
}
|
2018-05-08 16:46:54 +00:00
|
|
|
size_t expected_sd_kip_size = kip1_get_size_from_header(sd_kip);
|
|
|
|
if (expected_sd_kip_size != read_size) {
|
2018-05-21 15:45:05 +00:00
|
|
|
fatal_error("%s has wrong size or there is not enough space (expected 0x%zx, read 0x%zx)!\n",
|
2018-05-08 16:46:54 +00:00
|
|
|
sd_path, expected_sd_kip_size, read_size);
|
|
|
|
}
|
|
|
|
remaining_size -= expected_sd_kip_size;
|
|
|
|
current_dst_kip += expected_sd_kip_size;
|
2018-04-12 03:56:11 +00:00
|
|
|
} else {
|
2018-05-08 16:46:54 +00:00
|
|
|
size_t current_kip_size = kip1_get_size_from_header(current_kip);
|
2018-04-12 03:56:11 +00:00
|
|
|
if (current_kip_size > remaining_size) {
|
2018-05-20 14:18:48 +00:00
|
|
|
fatal_error("Not enough space for all the KIP1s!\n");
|
2018-04-12 03:56:11 +00:00
|
|
|
}
|
|
|
|
memcpy(current_dst_kip, current_kip, current_kip_size);
|
|
|
|
remaining_size -= current_kip_size;
|
|
|
|
current_dst_kip += current_kip_size;
|
|
|
|
}
|
2018-05-05 16:35:00 +00:00
|
|
|
|
2018-04-12 03:56:11 +00:00
|
|
|
process_list[merged->num_processes++] = current_kip->title_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
merged->size = sizeof(ini1_header_t) + (uint32_t)(current_dst_kip - merged->kip_data);
|
2018-05-05 16:35:00 +00:00
|
|
|
|
2018-05-08 16:46:54 +00:00
|
|
|
return merged;
|
2018-05-05 16:35:00 +00:00
|
|
|
}
|