2018-05-05 17:35:00 +01:00
|
|
|
#include <stdio.h>
|
2018-05-05 23:45:30 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2018-04-12 04:56:11 +01:00
|
|
|
#include "utils.h"
|
|
|
|
#include "package2.h"
|
|
|
|
#include "stratosphere.h"
|
|
|
|
#include "sd_utils.h"
|
|
|
|
|
2018-05-05 23:45:30 +01:00
|
|
|
static uint8_t *g_stratosphere_ini1;
|
2018-04-12 04:56:11 +01:00
|
|
|
static bool g_initialized_stratosphere_ini1 = false;
|
|
|
|
|
2018-05-05 23:45:30 +01:00
|
|
|
static uint8_t *g_ini1_buffer;
|
|
|
|
|
|
|
|
void stratosphere_allocate_mem(void) {
|
|
|
|
/* TODO call it*/
|
|
|
|
g_stratosphere_ini1 = (uint8_t *)malloc(PACKAGE2_SIZE_MAX);
|
|
|
|
g_ini1_buffer = (uint8_t *)malloc(PACKAGE2_SIZE_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
void stratosphere_free_mem(void) {
|
|
|
|
/* TODO call it*/
|
|
|
|
free(g_stratosphere_ini1);
|
|
|
|
free(g_ini1_buffer);
|
|
|
|
}
|
2018-04-12 04:56:11 +01:00
|
|
|
|
|
|
|
ini1_header_t *stratosphere_get_ini1(void) {
|
|
|
|
ini1_header_t *ini1_header = (ini1_header_t *)g_stratosphere_ini1;
|
|
|
|
if (g_initialized_stratosphere_ini1) {
|
|
|
|
return ini1_header;
|
|
|
|
}
|
|
|
|
ini1_header->magic = MAGIC_INI1;
|
|
|
|
ini1_header->size = sizeof(ini1_header_t);
|
|
|
|
ini1_header->num_processes = 0;
|
|
|
|
ini1_header->_0xC = 0;
|
2018-05-05 17:35:00 +01:00
|
|
|
|
2018-04-12 04:56:11 +01:00
|
|
|
/* TODO: When we have processes, copy them into ini1_header->kip_data here. */
|
2018-05-05 17:35:00 +01:00
|
|
|
|
2018-04-12 04:56:11 +01:00
|
|
|
g_initialized_stratosphere_ini1 = true;
|
|
|
|
return ini1_header;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Merges some number of INI1s into a single INI1. It's assumed that the INIs are in order of preference. */
|
2018-04-12 05:32:45 +01:00
|
|
|
size_t stratosphere_merge_inis(void *dst, ini1_header_t **inis, unsigned int num_inis) {
|
2018-04-12 04:56:11 +01:00
|
|
|
char sd_path[0x300] = {0};
|
|
|
|
/* Validate all ini headers. */
|
|
|
|
for (unsigned int i = 0; i < num_inis; i++) {
|
|
|
|
if (inis[i] == NULL || inis[i]->magic != MAGIC_INI1 || inis[i]->num_processes > INI1_MAX_KIPS) {
|
2018-05-05 22:55:40 +01:00
|
|
|
printf("Error: INI1s[%d] section appears to not contain an INI1!\n", i);
|
2018-04-12 04:56:11 +01:00
|
|
|
generic_panic();
|
|
|
|
}
|
|
|
|
}
|
2018-05-05 17:35:00 +01:00
|
|
|
|
2018-04-12 04:56:11 +01:00
|
|
|
uint64_t process_list[INI1_MAX_KIPS] = {0};
|
2018-05-05 17:35:00 +01:00
|
|
|
|
2018-05-05 23:45:30 +01:00
|
|
|
memset(g_ini1_buffer, 0, PACKAGE2_SIZE_MAX);
|
2018-04-12 04:56:11 +01:00
|
|
|
ini1_header_t *merged = (ini1_header_t *)g_ini1_buffer;
|
|
|
|
merged->magic = MAGIC_INI1;
|
|
|
|
merged->num_processes = 0;
|
|
|
|
merged->_0xC = 0;
|
|
|
|
size_t remaining_size = PACKAGE2_SIZE_MAX - sizeof(ini1_header_t);
|
2018-05-05 17:35:00 +01:00
|
|
|
|
2018-04-12 04:56:11 +01:00
|
|
|
unsigned char *current_dst_kip = merged->kip_data;
|
2018-05-05 17:35:00 +01:00
|
|
|
|
2018-04-12 04:56:11 +01:00
|
|
|
/* Actually merge into the inis. */
|
|
|
|
for (unsigned int i = 0; i < num_inis; i++) {
|
|
|
|
uint64_t offset = 0;
|
|
|
|
for (unsigned int p = 0; p < inis[i]->num_processes; p++) {
|
|
|
|
kip1_header_t *current_kip = (kip1_header_t *)(inis[i]->kip_data + offset);
|
|
|
|
if (current_kip->magic != MAGIC_KIP1) {
|
2018-05-05 22:55:40 +01:00
|
|
|
printf("Error: INI1s[%d][%d] appears not to be a KIP1!\n", i, p);
|
2018-04-12 04:56:11 +01:00
|
|
|
generic_panic();
|
|
|
|
}
|
2018-05-05 17:35:00 +01:00
|
|
|
|
|
|
|
|
2018-04-12 04:56:11 +01:00
|
|
|
bool already_loaded = false;
|
|
|
|
for (unsigned int j = 0; j < merged->num_processes; j++) {
|
|
|
|
if (process_list[j] == current_kip->title_id) {
|
|
|
|
already_loaded = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (already_loaded) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-05 17:35:00 +01:00
|
|
|
|
2018-04-12 05:10:02 +01:00
|
|
|
/* TODO: What folder should these be read out of? */
|
2018-05-05 22:55:40 +01:00
|
|
|
snprintf(sd_path, sizeof(sd_path), "atmosphere/titles/%016llx/%016llx.kip", current_kip->title_id, current_kip->title_id);
|
2018-05-05 17:35:00 +01:00
|
|
|
|
2018-04-12 04:56:11 +01:00
|
|
|
/* Try to load an override KIP from SD, if possible. */
|
|
|
|
if (read_sd_file(current_dst_kip, remaining_size, sd_path)) {
|
|
|
|
kip1_header_t *sd_kip = (kip1_header_t *)(current_dst_kip);
|
|
|
|
if (sd_kip->magic != MAGIC_KIP1) {
|
2018-05-05 22:55:40 +01:00
|
|
|
printf("Error: %s is not a KIP1?\n", sd_path);
|
2018-04-12 04:56:11 +01:00
|
|
|
generic_panic();
|
|
|
|
} else if (sd_kip->title_id != current_kip->title_id) {
|
2018-05-05 22:55:40 +01:00
|
|
|
printf("Error: %s has wrong Title ID!\n", sd_path);
|
2018-04-12 04:56:11 +01:00
|
|
|
generic_panic();
|
|
|
|
}
|
2018-04-12 09:47:27 +01:00
|
|
|
uint64_t sd_kip_size = kip1_get_size_from_header(sd_kip);
|
|
|
|
remaining_size -= sd_kip_size;
|
|
|
|
current_dst_kip += sd_kip_size;
|
2018-04-12 04:56:11 +01:00
|
|
|
} else {
|
|
|
|
uint64_t current_kip_size = kip1_get_size_from_header(current_kip);
|
|
|
|
if (current_kip_size > remaining_size) {
|
2018-05-05 22:55:40 +01:00
|
|
|
printf("Error: Not enough space for all the KIP1s!\n");
|
2018-04-12 04:56:11 +01:00
|
|
|
generic_panic();
|
|
|
|
}
|
|
|
|
memcpy(current_dst_kip, current_kip, current_kip_size);
|
|
|
|
remaining_size -= current_kip_size;
|
|
|
|
current_dst_kip += current_kip_size;
|
|
|
|
}
|
2018-05-05 17:35:00 +01:00
|
|
|
|
2018-04-12 04:56:11 +01: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 17:35:00 +01:00
|
|
|
|
2018-04-12 04:56:11 +01:00
|
|
|
/* Copy merged INI1 to destination. */
|
|
|
|
memcpy(dst, merged, merged->size);
|
2018-05-05 17:35:00 +01:00
|
|
|
|
2018-04-12 05:32:45 +01:00
|
|
|
return merged->size;
|
2018-05-05 17:35:00 +01:00
|
|
|
}
|