mirror of
https://github.com/Atmosphere-NX/Atmosphere
synced 2024-11-09 22:56:35 +00:00
Change the allocation logic in stratosphere.c
This commit is contained in:
parent
70bb496388
commit
7df879d346
4 changed files with 76 additions and 55 deletions
|
@ -14,7 +14,7 @@ typedef struct {
|
||||||
uint32_t size;
|
uint32_t size;
|
||||||
uint32_t num_processes;
|
uint32_t num_processes;
|
||||||
uint32_t _0xC;
|
uint32_t _0xC;
|
||||||
unsigned char kip_data[];
|
uint8_t kip_data[];
|
||||||
} ini1_header_t;
|
} ini1_header_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -35,13 +35,12 @@ typedef struct {
|
||||||
uint8_t flags;
|
uint8_t flags;
|
||||||
kip_section_header_t section_headers[6];
|
kip_section_header_t section_headers[6];
|
||||||
uint32_t capabilities[0x20];
|
uint32_t capabilities[0x20];
|
||||||
unsigned char data[];
|
uint8_t data[];
|
||||||
} kip1_header_t;
|
} kip1_header_t;
|
||||||
|
|
||||||
static inline uint64_t kip1_get_size_from_header(kip1_header_t *header) {
|
static inline size_t kip1_get_size_from_header(kip1_header_t *header) {
|
||||||
/* Header + .text + .rodata + .rwdata */
|
/* Header + .text + .rodata + .rwdata */
|
||||||
return 0x100 + header->section_headers[0].compressed_size + header->section_headers[1].compressed_size + header->section_headers[2].compressed_size;
|
return 0x100 + header->section_headers[0].compressed_size + header->section_headers[1].compressed_size + header->section_headers[2].compressed_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -233,11 +233,19 @@ void package2_patch_kernel(void) {
|
||||||
void package2_patch_ini1(void) {
|
void package2_patch_ini1(void) {
|
||||||
/* TODO: Do we want to support loading another INI from sd:/whatever/INI1.bin? */
|
/* TODO: Do we want to support loading another INI from sd:/whatever/INI1.bin? */
|
||||||
ini1_header_t *inis_to_merge[STRATOSPHERE_INI1_MAX] = {0};
|
ini1_header_t *inis_to_merge[STRATOSPHERE_INI1_MAX] = {0};
|
||||||
|
ini1_header_t *merged;
|
||||||
|
|
||||||
inis_to_merge[STRATOSPHERE_INI1_EMBEDDED] = stratosphere_get_ini1();
|
inis_to_merge[STRATOSPHERE_INI1_EMBEDDED] = stratosphere_get_ini1();
|
||||||
inis_to_merge[STRATOSPHERE_INI1_PACKAGE2] = (ini1_header_t *)g_package2_sections[PACKAGE2_SECTION_INI1];
|
inis_to_merge[STRATOSPHERE_INI1_PACKAGE2] = (ini1_header_t *)g_package2_sections[PACKAGE2_SECTION_INI1];
|
||||||
|
|
||||||
/* Merge all of the INI1s. */
|
/* Merge all of the INI1s. */
|
||||||
g_patched_package2_header->metadata.section_sizes[PACKAGE2_SECTION_INI1] = stratosphere_merge_inis(g_package2_sections[PACKAGE2_SECTION_INI1], inis_to_merge, STRATOSPHERE_INI1_MAX);
|
merged = stratosphere_merge_inis(inis_to_merge, STRATOSPHERE_INI1_MAX);
|
||||||
|
memcpy(g_package2_sections[PACKAGE2_SECTION_INI1], merged, merged->size);
|
||||||
|
g_patched_package2_header->metadata.section_sizes[PACKAGE2_SECTION_INI1] = merged->size;
|
||||||
|
|
||||||
|
/* Free temporary buffers. */
|
||||||
|
free(merged);
|
||||||
|
stratosphere_free_ini1();
|
||||||
}
|
}
|
||||||
|
|
||||||
void package2_fixup_header_and_section_hashes(void) {
|
void package2_fixup_header_and_section_hashes(void) {
|
||||||
|
|
|
@ -6,74 +6,84 @@
|
||||||
#include "stratosphere.h"
|
#include "stratosphere.h"
|
||||||
#include "sd_utils.h"
|
#include "sd_utils.h"
|
||||||
|
|
||||||
static uint8_t *g_stratosphere_ini1;
|
static ini1_header_t *g_stratosphere_ini1 = NULL;
|
||||||
static bool g_initialized_stratosphere_ini1 = false;
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
ini1_header_t *stratosphere_get_ini1(void) {
|
ini1_header_t *stratosphere_get_ini1(void) {
|
||||||
ini1_header_t *ini1_header = (ini1_header_t *)g_stratosphere_ini1;
|
if (g_stratosphere_ini1 != NULL) {
|
||||||
if (g_initialized_stratosphere_ini1) {
|
return g_stratosphere_ini1;
|
||||||
return ini1_header;
|
|
||||||
}
|
}
|
||||||
ini1_header->magic = MAGIC_INI1;
|
|
||||||
ini1_header->size = sizeof(ini1_header_t);
|
size_t size = 0; /* TODO */
|
||||||
ini1_header->num_processes = 0;
|
g_stratosphere_ini1 = (ini1_header_t *)malloc(size);
|
||||||
ini1_header->_0xC = 0;
|
|
||||||
|
if (g_stratosphere_ini1 != NULL) {
|
||||||
|
printf("Error: stratosphere_get_ini1: out of memory!\n");
|
||||||
|
generic_panic();
|
||||||
|
}
|
||||||
|
|
||||||
|
g_stratosphere_ini1->magic = MAGIC_INI1;
|
||||||
|
g_stratosphere_ini1->size = sizeof(ini1_header_t);
|
||||||
|
g_stratosphere_ini1->num_processes = 0;
|
||||||
|
g_stratosphere_ini1->_0xC = 0;
|
||||||
|
|
||||||
/* TODO: When we have processes, copy them into ini1_header->kip_data here. */
|
/* TODO: When we have processes, copy them into ini1_header->kip_data here. */
|
||||||
|
|
||||||
g_initialized_stratosphere_ini1 = true;
|
return g_stratosphere_ini1;
|
||||||
return ini1_header;
|
}
|
||||||
|
|
||||||
|
void stratosphere_free_ini1(void) {
|
||||||
|
free(g_stratosphere_ini1);
|
||||||
|
g_stratosphere_ini1 = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Merges some number of INI1s into a single INI1. It's assumed that the INIs are in order of preference. */
|
/* Merges some number of INI1s into a single INI1. It's assumed that the INIs are in order of preference. */
|
||||||
size_t stratosphere_merge_inis(void *dst, ini1_header_t **inis, unsigned int num_inis) {
|
ini1_header_t *stratosphere_merge_inis(ini1_header_t **inis, size_t num_inis) {
|
||||||
char sd_path[0x100] = {0};
|
char sd_path[0x100] = {0};
|
||||||
|
uint32_t total_num_processes = 0;
|
||||||
|
|
||||||
/* Validate all ini headers. */
|
/* Validate all ini headers. */
|
||||||
for (unsigned int i = 0; i < num_inis; i++) {
|
for (size_t i = 0; i < num_inis; i++) {
|
||||||
if (inis[i] == NULL || inis[i]->magic != MAGIC_INI1 || inis[i]->num_processes > INI1_MAX_KIPS) {
|
if (inis[i] == NULL || inis[i]->magic != MAGIC_INI1 || inis[i]->num_processes > INI1_MAX_KIPS) {
|
||||||
printf("Error: INI1s[%d] section appears to not contain an INI1!\n", i);
|
printf("Error: INI1s[%d] section appears to not contain an INI1!\n", i);
|
||||||
generic_panic();
|
generic_panic();
|
||||||
|
} else {
|
||||||
|
total_num_processes += inis[i]->num_processes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (total_num_processes > INI1_MAX_KIPS) {
|
||||||
|
printf("Error: The resulting INI1 would have too many KIPs!\n");
|
||||||
|
generic_panic();
|
||||||
|
}
|
||||||
|
|
||||||
uint64_t process_list[INI1_MAX_KIPS] = {0};
|
uint64_t process_list[INI1_MAX_KIPS] = {0};
|
||||||
|
ini1_header_t *merged = (ini1_header_t *)malloc(PACKAGE2_SIZE_MAX); /* because of SD file overrides */
|
||||||
|
|
||||||
|
if (merged == NULL) {
|
||||||
|
printf("Error: stratosphere_merge_inis: out of memory!\n");
|
||||||
|
generic_panic();
|
||||||
|
}
|
||||||
|
|
||||||
memset(g_ini1_buffer, 0, PACKAGE2_SIZE_MAX);
|
|
||||||
ini1_header_t *merged = (ini1_header_t *)g_ini1_buffer;
|
|
||||||
merged->magic = MAGIC_INI1;
|
merged->magic = MAGIC_INI1;
|
||||||
merged->num_processes = 0;
|
merged->num_processes = 0;
|
||||||
merged->_0xC = 0;
|
merged->_0xC = 0;
|
||||||
size_t remaining_size = PACKAGE2_SIZE_MAX - sizeof(ini1_header_t);
|
size_t remaining_size = PACKAGE2_SIZE_MAX - sizeof(ini1_header_t);
|
||||||
|
size_t read_size;
|
||||||
|
|
||||||
unsigned char *current_dst_kip = merged->kip_data;
|
unsigned char *current_dst_kip = merged->kip_data;
|
||||||
|
|
||||||
/* Actually merge into the inis. */
|
/* Actually merge into the inis. */
|
||||||
for (unsigned int i = 0; i < num_inis; i++) {
|
for (size_t i = 0; i < num_inis; i++) {
|
||||||
uint64_t offset = 0;
|
size_t offset = 0;
|
||||||
for (unsigned int p = 0; p < inis[i]->num_processes; p++) {
|
for (size_t p = 0; p < (size_t)inis[i]->num_processes; p++) {
|
||||||
kip1_header_t *current_kip = (kip1_header_t *)(inis[i]->kip_data + offset);
|
kip1_header_t *current_kip = (kip1_header_t *)(inis[i]->kip_data + offset);
|
||||||
if (current_kip->magic != MAGIC_KIP1) {
|
if (current_kip->magic != MAGIC_KIP1) {
|
||||||
printf("Error: INI1s[%d][%d] appears not to be a KIP1!\n", i, p);
|
printf("Error: INI1s[%zu][%zu] appears not to be a KIP1!\n", i, p);
|
||||||
generic_panic();
|
generic_panic();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool already_loaded = false;
|
bool already_loaded = false;
|
||||||
for (unsigned int j = 0; j < merged->num_processes; j++) {
|
for (uint32_t j = 0; j < merged->num_processes; j++) {
|
||||||
if (process_list[j] == current_kip->title_id) {
|
if (process_list[j] == current_kip->title_id) {
|
||||||
already_loaded = true;
|
already_loaded = true;
|
||||||
break;
|
break;
|
||||||
|
@ -84,23 +94,29 @@ size_t stratosphere_merge_inis(void *dst, ini1_header_t **inis, unsigned int num
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: What folder should these be read out of? */
|
/* TODO: What folder should these be read out of? */
|
||||||
snprintf(sd_path, sizeof(sd_path), "atmosphere/titles/%016llx/%016llx.kip", current_kip->title_id, current_kip->title_id);
|
snprintf(sd_path, sizeof(sd_path), "atmosphere/titles/%016llX/%016llX.kip", current_kip->title_id, current_kip->title_id);
|
||||||
|
|
||||||
/* Try to load an override KIP from SD, if possible. */
|
/* Try to load an override KIP from SD, if possible. */
|
||||||
if (read_sd_file(current_dst_kip, remaining_size, sd_path)) {
|
read_size = read_sd_file(current_dst_kip, remaining_size, sd_path);
|
||||||
|
if (read_size != 0) {
|
||||||
kip1_header_t *sd_kip = (kip1_header_t *)(current_dst_kip);
|
kip1_header_t *sd_kip = (kip1_header_t *)(current_dst_kip);
|
||||||
if (sd_kip->magic != MAGIC_KIP1) {
|
if (read_size < sizeof(kip1_header_t) || sd_kip->magic != MAGIC_KIP1) {
|
||||||
printf("Error: %s is not a KIP1?\n", sd_path);
|
printf("Error: %s is not a KIP1?\n", sd_path);
|
||||||
generic_panic();
|
generic_panic();
|
||||||
} else if (sd_kip->title_id != current_kip->title_id) {
|
} else if (sd_kip->title_id != current_kip->title_id) {
|
||||||
printf("Error: %s has wrong Title ID!\n", sd_path);
|
printf("Error: %s has wrong Title ID!\n", sd_path);
|
||||||
generic_panic();
|
generic_panic();
|
||||||
}
|
}
|
||||||
uint64_t sd_kip_size = kip1_get_size_from_header(sd_kip);
|
size_t expected_sd_kip_size = kip1_get_size_from_header(sd_kip);
|
||||||
remaining_size -= sd_kip_size;
|
if (expected_sd_kip_size != read_size) {
|
||||||
current_dst_kip += sd_kip_size;
|
printf("Error: %s has wrong size or there is not enough space (expected 0x%zx, read 0x%zx)!\n",
|
||||||
|
sd_path, expected_sd_kip_size, read_size);
|
||||||
|
generic_panic();
|
||||||
|
}
|
||||||
|
remaining_size -= expected_sd_kip_size;
|
||||||
|
current_dst_kip += expected_sd_kip_size;
|
||||||
} else {
|
} else {
|
||||||
uint64_t current_kip_size = kip1_get_size_from_header(current_kip);
|
size_t current_kip_size = kip1_get_size_from_header(current_kip);
|
||||||
if (current_kip_size > remaining_size) {
|
if (current_kip_size > remaining_size) {
|
||||||
printf("Error: Not enough space for all the KIP1s!\n");
|
printf("Error: Not enough space for all the KIP1s!\n");
|
||||||
generic_panic();
|
generic_panic();
|
||||||
|
@ -115,8 +131,5 @@ size_t stratosphere_merge_inis(void *dst, ini1_header_t **inis, unsigned int num
|
||||||
}
|
}
|
||||||
merged->size = sizeof(ini1_header_t) + (uint32_t)(current_dst_kip - merged->kip_data);
|
merged->size = sizeof(ini1_header_t) + (uint32_t)(current_dst_kip - merged->kip_data);
|
||||||
|
|
||||||
/* Copy merged INI1 to destination. */
|
return merged;
|
||||||
memcpy(dst, merged, merged->size);
|
|
||||||
|
|
||||||
return merged->size;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,8 @@
|
||||||
#define STRATOSPHERE_INI1_MAX 0x2
|
#define STRATOSPHERE_INI1_MAX 0x2
|
||||||
|
|
||||||
ini1_header_t *stratosphere_get_ini1(void);
|
ini1_header_t *stratosphere_get_ini1(void);
|
||||||
|
void stratosphere_free_ini1(void);
|
||||||
|
|
||||||
size_t stratosphere_merge_inis(void *dst, ini1_header_t **inis, unsigned int num_inis);
|
ini1_header_t *stratosphere_merge_inis(ini1_header_t **inis, unsigned int num_inis);
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in a new issue