malloc(3) is now newlib's implementation instead of my shitty one. Turns out; after poking around the latest linker.ld in libctr9, all this time...
It was a linker script issue. I. Hate. Everything.
Miscellaneous changes also here:
* f{whatever} -> cr{whatever} to avoid clashes with newlib (and hopefully facillitate replacement)
* FIRM allocation size was made to be fixed at 0x100000 as a stopgap. Turns out that memory corruption was massive, and the only thing saving my ass before was the allocator being...dumb.
SUBDIRS = include host external source patch contrib
ACLOCAL_AMFLAGS = -I m4
-source: include host external
+source: include host external patch contrib
external: include host
#define __COMMON_H
#include <stdlib.h>
+#include <malloc.h>
#include <corbconf.h>
*
* \return zero on success
*/
-int patch_firm_all(uint64_t tid, firm_h* firm, const char* module_path);
+int patch_firm_all(uint64_t tid, firm_h** firm, const char* module_path);
/* Generates patch cache for boot/loader for the current configuration.
*
int patch_svc_calls(firm_h* firm_loc);
int patch_reboot(firm_h* firm_loc);
-int patch_modules(firm_h* firm_loc, const char* module_path);
+int patch_modules(firm_h** firm_loc, const char* module_path);
#endif
*
* \param channel Handle to flush output on
*/
-void fflush(void *channel);
+void crflush(void *channel);
/* Moves the cursor/output location on a display device
*
#define SEEK_CUR 1
#define SEEK_END 2
-int fmount(void);
+int crmount(void);
-int fumount(void);
+int crumount(void);
void recurse_call(const char *name, void (*call_fun)(char*));
int rrmdir(const char *dir_path);
-FILE *fopen(const char *filename, const char *mode);
+FILE *cropen(const char *filename, const char *mode);
-void fclose(FILE *fp);
+void crclose(FILE *fp);
-void fseek(FILE *fp, int64_t offset, int whence);
+void crseek(FILE *fp, int64_t offset, int whence);
-size_t ftell(FILE *fp);
+size_t crtell(FILE *fp);
-int feof(FILE *fp);
+int creof(FILE *fp);
-size_t fsize(FILE *fp);
+size_t crsize(FILE *fp);
-size_t fwrite(const void *buffer, size_t elementSize, size_t elementCnt, FILE *fp);
+size_t crwrite(const void *buffer, size_t elementSize, size_t elementCnt, FILE *fp);
-size_t fread(void *buffer, size_t elementSize, size_t elementCnt, FILE *fp);
+size_t crread(void *buffer, size_t elementSize, size_t elementCnt, FILE *fp);
size_t write_file(void *data, const char *path, size_t size);
// Look ma, dynamically allocating the CakeHax struct! (joking)
// We literally just discard the previous state - for sanity's sake.
// On chainload, it is needed to copy the framebuffer struct.
- framebuffers = malloc(sizeof(struct framebuffers));
+ framebuffers = memalign(16, sizeof(struct framebuffers));
}
void __attribute__((naked)) ARM11(void) {
uint32_t size = 0, b_size = 0;
uint8_t* chain_data;
- FILE* f = fopen(code_file, "r");
+ FILE* f = cropen(code_file, "r");
if (!f) {
// File missing.
panic("Missing chainloader.\n");
}
- b_size = fsize(f);
- fread(bootstrap, 1, b_size, f);
- fclose(f);
+ b_size = crsize(f);
+ crread(bootstrap, 1, b_size, f);
+ crclose(f);
chain_data = bootstrap + b_size;
- f = fopen(chain_file, "r");
+ f = cropen(chain_file, "r");
if (!f) {
// File missing.
panic("Missing program to chainload?\n");
}
- size = fsize(f);
- fread(chain_data, 1, size, f);
- fclose(f);
+ size = crsize(f);
+ crread(chain_data, 1, size, f);
+ crclose(f);
fprintf(stderr, "Setting argc, argv...\n");
void chainload_menu() {
if (chains == NULL) {
- chains = malloc(sizeof(struct options_s) * 100);
+ chains = memalign(16, sizeof(struct options_s) * 100);
list_chain_build(PATH_CHAINS);
}
config->options[OPTION_ACCENT_COLOR] = 2;
config->options[OPTION_BRIGHTNESS] = 3;
- if (!(conf_handle = fopen(config_file_path, "w")))
+ if (!(conf_handle = cropen(config_file_path, "w")))
poweroff();
- fwrite(config, 1, sizeof(struct config_file) + PATCH_MAX, conf_handle);
- fclose(conf_handle);
+ crwrite(config, 1, sizeof(struct config_file) + PATCH_MAX, conf_handle);
+ crclose(conf_handle);
}
void
mk_structure(); // Make directory structure if needed.
if (!config_file_path) {
- config_file_path = malloc(256); // MAX_PATH
+ config_file_path = memalign(16, 256); // MAX_PATH
memset(config_file_path, 0, 256);
sdmmc_get_cid(1, cid);
- FILE* f = fopen(SYSCONFDIR "/current-nand-cid", "r");
+ FILE* f = cropen(SYSCONFDIR "/current-nand-cid", "r");
if (!f) {
// Nonexistent. Write it.
- f = fopen(SYSCONFDIR "/current-nand-cid", "w");
- fwrite(cid, 1, 4, f);
- fclose(f);
- f = fopen(SYSCONFDIR "/current-nand-cid", "r");
+ f = cropen(SYSCONFDIR "/current-nand-cid", "w");
+ crwrite(cid, 1, 4, f);
+ crclose(f);
+ f = cropen(SYSCONFDIR "/current-nand-cid", "r");
}
- fread(&cid[1], 1, 4, f);
+ crread(&cid[1], 1, 4, f);
// If our console's CID doesn't match what was read, we need to regenerate caches immediately when we can.
if (cid[0] != cid[1]) {
cid_cp >>= 4;
}
- config = (struct config_file*)malloc(sizeof(struct config_file) + PATCH_MAX);
+ config = (struct config_file*)memalign(16, sizeof(struct config_file) + PATCH_MAX);
memset(config, 0, sizeof(struct config_file) + PATCH_MAX);
enable_list = (uint8_t*)config + sizeof(struct config_file);
- fclose(f);
+ crclose(f);
}
// Zero on success.
- if (!(conf_handle = fopen(config_file_path, "r"))) {
+ if (!(conf_handle = cropen(config_file_path, "r"))) {
regenerate_config();
} else {
- fread(config, 1, sizeof(struct config_file) + PATCH_MAX, conf_handle);
+ crread(config, 1, sizeof(struct config_file) + PATCH_MAX, conf_handle);
- fclose(conf_handle);
+ crclose(conf_handle);
if (memcmp(&(config->magic), CONFIG_MAGIC, 4)) {
f_unlink(config_file_path);
save_config(void)
{
if (changed_consoles) {
- FILE* f = fopen(SYSCONFDIR "/current-nand-cid", "w");
- fwrite(cid, 1, 4, f);
- fclose(f);
+ FILE* f = cropen(SYSCONFDIR "/current-nand-cid", "w");
+ crwrite(cid, 1, 4, f);
+ crclose(f);
}
f_unlink(config_file_path);
- if (!(conf_handle = fopen(config_file_path, "w")))
+ if (!(conf_handle = cropen(config_file_path, "w")))
while(1);
- fwrite(config, 1, sizeof(struct config_file) + PATCH_MAX, conf_handle);
+ crwrite(config, 1, sizeof(struct config_file) + PATCH_MAX, conf_handle);
- fclose(conf_handle);
+ crclose(conf_handle);
}
void change_opt(void* val) {
int save_dec = 0;
char* decpath = strdupcat(path, ".dec");
- firm_file = fopen(decpath, "r");
+ firm_file = cropen(decpath, "r");
if (!firm_file) {
- firm_file = fopen(path, "r");
+ firm_file = cropen(path, "r");
if (!firm_file) {
return NULL;
}
}
- size = fsize(firm_file);
+ size = crsize(firm_file);
if (size_out)
*size_out = size;
- mem = malloc(size);
+ // FIXME - Temp fix; allocating way more memory than needed to prevent memory corruption
+ mem = memalign(16, 0x1000000);
firm_h *firm = (firm_h*)mem;
- fread(mem, 1, size, firm_file);
+ crread(mem, 1, size, firm_file);
- fclose(firm_file);
+ crclose(firm_file);
if (memcmp(firm->magic, "FIRM", 4)) {
char *key_path = strdupcat(path, ".key");
// Attempt to open keyfile.
- uint8_t* firmkey = malloc(16);
+ uint8_t* firmkey = memalign(16, 16);
if (read_file(firmkey, key_path, 16) != 16) {
// Keyfile couldn't be opened, try the cetk.
free(firmkey);
free(cetk_path);
// Save firmkey.
- FILE* keyfile = fopen(key_path, "w");
- fwrite(firmkey, 1, 16, keyfile);
- fclose(keyfile);
+ FILE* keyfile = cropen(key_path, "w");
+ crwrite(firmkey, 1, 16, keyfile);
+ crclose(keyfile);
}
free(key_path);
// Save decrypted FIRM.
if (save_dec == 1) {
- firm_file = fopen(decpath, "w");
- fwrite(firm, 1, size, firm_file);
- fclose(firm_file);
+ firm_file = cropen(decpath, "w");
+ crwrite(firm, 1, size, firm_file);
+ crclose(firm_file);
}
free(decpath);
free(sig);
- if (patch_firm_all(tid, firm, module_path)) {
+ if (patch_firm_all(tid, &firm, module_path)) {
free(firm);
return 1;
}
- FILE* f = fopen(prepatch_path, "w");
- fwrite(firm, 1, size, f);
- fclose(f);
+ FILE* f = cropen(prepatch_path, "w");
+ crwrite(firm, 1, size, f);
+ crclose(f);
free(firm);
free(sig);
- if (patch_firm_all(tid, firm, module_path)) {
+ if (patch_firm_all(tid, &firm, module_path)) {
free(firm);
return 1;
}
- FILE* f = fopen(prepatch_path, "w");
- fwrite(firm, 1, size, f);
- fclose(f);
+ FILE* f = cropen(prepatch_path, "w");
+ crwrite(firm, 1, size, f);
+ crclose(f);
firmlaunch(firm); // <- should NOT return if all is well
free(firm); // Really, no point in this. Why not, though.
- fflush(stderr); // Flush logs if need be before unmount.
+ crflush(stderr); // Flush logs if need be before unmount.
- fumount(); // Unmount SD.
+ crumount(); // Unmount SD.
deinitScreens(); // Turn off display
if (ss_keyn[0][0] == 0) {
ss_keyn[0][0] = 1;
- FILE* key = fopen(PATH_SLOT0X11KEY95, "r");
+ FILE* key = cropen(PATH_SLOT0X11KEY95, "r");
if (key) {
- fread(ss_keyn[0], 1, AES_BLOCK_SIZE, key);
- fclose(key);
+ crread(ss_keyn[0], 1, AES_BLOCK_SIZE, key);
+ crclose(key);
}
}
if (ss_keyn[1][0] == 0) {
ss_keyn[1][0] = 1;
- FILE* key = fopen(PATH_SLOT0X11KEY96, "r");
+ FILE* key = cropen(PATH_SLOT0X11KEY96, "r");
if (key) {
- fread(ss_keyn[1], 1, AES_BLOCK_SIZE, key);
- fclose(key);
+ crread(ss_keyn[1], 1, AES_BLOCK_SIZE, key);
+ crclose(key);
}
}
uint8_t*
get_titlekey(char *cetk_filename)
{
- FILE* f = fopen(cetk_filename, "r");
- size_t size = fsize(f);
+ FILE* f = cropen(cetk_filename, "r");
+ size_t size = crsize(f);
- uint8_t* cetk = malloc(size);
+ uint8_t* cetk = memalign(16, size);
- fread(cetk, 1, size, f);
+ crread(cetk, 1, size, f);
- fclose(f);
+ crclose(f);
uint8_t iv[AES_BLOCK_SIZE] = { 0 };
uint32_t sigtype = __builtin_bswap32(*(const uint32_t *)cetk);
set_Y3D_cetk(1);
- uint8_t *key = malloc(AES_BLOCK_SIZE);
+ uint8_t *key = memalign(16, AES_BLOCK_SIZE);
memcpy(iv, ticket->titleID, sizeof(ticket->titleID));
memcpy(key, ticket->titleKey, sizeof(ticket->titleKey));
return NULL;
}
- firm_h* dest = malloc(size);
+ firm_h* dest = memalign(16, size);
memcpy(dest, firm, size);
{
// What follows is a heuristic to detect the firmware's properties. Checks are as follows:
- struct firm_signature *signature = (struct firm_signature*)malloc(sizeof(struct firm_signature));
+ struct firm_signature *signature = (struct firm_signature*)memalign(16, sizeof(struct firm_signature));
signature->type = type_native;
signature->k9l = 0;
(void)fname;
// NYI
#else
- FILE* f = fopen(fname, "r");
- fread(current_mode->memory + offset, 1, fsize(f), f);
- offset += fsize(f);
+ FILE* f = cropen(fname, "r");
+ crread(current_mode->memory + offset, 1, crsize(f), f);
+ offset += crsize(f);
code += strlen(fname);
- fclose(f);
+ crclose(f);
#endif
break;
default:
// Read patch to scrap memory.
- FILE *f = fopen(cache_path, "r");
+ FILE *f = cropen(cache_path, "r");
if (!f) {
// File wasn't found. The user didn't enable anything.
return 0;
}
- patch_len = fsize(f);
+ patch_len = crsize(f);
- uint8_t* patch_mem = malloc(patch_len);
+ uint8_t* patch_mem = memalign(16, patch_len);
- fread(patch_mem, 1, patch_len, f);
- fclose(f);
+ crread(patch_mem, 1, patch_len, f);
+ crclose(f);
#endif
int debug = 0;
uint8_t *patch_mem;
// Read patch to scrap memory.
- FILE *f = fopen(filename, "r");
+ FILE *f = cropen(filename, "r");
if (!f) {
// File wasn't found. The user didn't enable anything.
return 0;
}
- uint32_t len = fsize(f);
+ uint32_t len = crsize(f);
uint8_t* patch_loc = malloc(len);
- fread(patch_loc, 1, len, f);
- fclose(f);
+ crread(patch_loc, 1, len, f);
+ crclose(f);
patch = (struct system_patch*)patch_loc;
char reset = 0xFF;
- FILE *cache = fopen(cache_path, "w");
- fseek(cache, 0, SEEK_END);
- fwrite(patch_mem, 1, patch_len, cache);
- fwrite(&reset, 1, 1, cache);
- fclose(cache);
+ FILE *cache = cropen(cache_path, "w");
+ crseek(cache, 0, SEEK_END);
+ crwrite(patch_mem, 1, patch_len, cache);
+ crwrite(&reset, 1, 1, cache);
+ crclose(cache);
// Add to cache.
}
}
+/* Script for -z combreloc: combine and sort reloc sections */
+/* Copyright (C) 2014 Free Software Foundation, Inc.
+ Copying and distribution of this script, with or without modification,
+ are permitted in any medium without royalty provided the copyright
+ notice and this notice are preserved. */
+OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm",
+ "elf32-littlearm")
OUTPUT_ARCH(arm)
ENTRY(_start)
+/* SEARCH_DIR("/opt/devkitpro/devkitARM/arm-none-eabi/lib"); */
SECTIONS
{
- . = 0x23F00000;
-
- .text.start : {
- *(.text.start)
+ /* Read-only sections, merged into text segment: */
+ /* Changed default address to 0x23F00000 */
+ PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x23F00000)); . = SEGMENT_START("text-segment", 0x23F00000);
+ .interp : { *(.interp) }
+ .note.gnu.build-id : { *(.note.gnu.build-id) }
+ .hash : { *(.hash) }
+ .gnu.hash : { *(.gnu.hash) }
+ .dynsym : { *(.dynsym) }
+ .dynstr : { *(.dynstr) }
+ .gnu.version : { *(.gnu.version) }
+ .gnu.version_d : { *(.gnu.version_d) }
+ .gnu.version_r : { *(.gnu.version_r) }
+ .rel.dyn :
+ {
+ *(.rel.init)
+ *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)
+ *(.rel.fini)
+ *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)
+ *(.rel.data.rel.ro .rel.data.rel.ro.* .rel.gnu.linkonce.d.rel.ro.*)
+ *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)
+ *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)
+ *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)
+ *(.rel.ctors)
+ *(.rel.dtors)
+ *(.rel.got)
+ *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)
+ PROVIDE_HIDDEN (__rel_iplt_start = .);
+ *(.rel.iplt)
+ PROVIDE_HIDDEN (__rel_iplt_end = .);
}
-
- . = ALIGN(4);
-
- .text : {
- *(.text)
- }
-
- . = ALIGN(4);
-
- .data : {
- *(.data)
+ .rela.dyn :
+ {
+ *(.rela.init)
+ *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)
+ *(.rela.fini)
+ *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)
+ *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)
+ *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)
+ *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)
+ *(.rela.ctors)
+ *(.rela.dtors)
+ *(.rela.got)
+ *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)
+ PROVIDE_HIDDEN (__rela_iplt_start = .);
+ *(.rela.iplt)
+ PROVIDE_HIDDEN (__rela_iplt_end = .);
}
-
- . = ALIGN(4);
-
- .rodata : {
- *(.rodata)
+ .rel.plt :
+ {
+ *(.rel.plt)
}
-
- . = ALIGN(4);
-
- .rel : {
- *(.rel)
+ .rela.plt :
+ {
+ *(.rela.plt)
}
-
- . = ALIGN(4);
-
- .symtab : {
- *(.symtab)
- }
-
- . = ALIGN(4);
-
- .bss : {
- __bss_start = .;
- *(.bss COMMON)
- }
- __bss_end = .;
-
- . = ALIGN(4);
-
- __end__ = 0x24000000;
+ /* Make sure the start.o entry is at the beginning since that's the default alh entry */
+ arm9loaderhax_entry :
+ {
+ KEEP (*(.text.start))
+ }
+ arm9loaderhax_screeninit (INFO):
+ {
+ KEEP (*(screeninit))
+ }
+ .init :
+ {
+ KEEP (*(SORT_NONE(.init)))
+ }
+ .plt : { *(.plt) }
+ .iplt : { *(.iplt) }
+ .text :
+ {
+ *(.text.unlikely .text.*_unlikely .text.unlikely.*)
+ *(.text.exit .text.exit.*)
+ *(.text.startup .text.startup.*)
+ *(.text.hot .text.hot.*)
+ *(.text .stub EXCLUDE_FILE (start.o) .text.* .gnu.linkonce.t.*)
+ /* .gnu.warning sections are handled specially by elf32.em. */
+ *(.gnu.warning)
+ *(.glue_7t) *(.glue_7) *(.vfp11_veneer) *(.v4_bx)
+ }
+ .fini :
+ {
+ KEEP (*(SORT_NONE(.fini)))
+ }
+ PROVIDE (__etext = .);
+ PROVIDE (_etext = .);
+ PROVIDE (etext = .);
+ .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) }
+ .rodata1 : { *(.rodata1) }
+ .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) }
+ PROVIDE_HIDDEN (__exidx_start = .);
+ .ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) }
+ PROVIDE_HIDDEN (__exidx_end = .);
+ .eh_frame_hdr : { *(.eh_frame_hdr) }
+ .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) }
+ .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table
+ .gcc_except_table.*) }
+ /* These sections are generated by the Sun/Oracle C++ compiler. */
+ .exception_ranges : ONLY_IF_RO { *(.exception_ranges
+ .exception_ranges*) }
+ /* Adjust the address for the data segment. We want to adjust up to
+ the same address within the page on the next page up. */
+ . = ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1));
+ /* Exception handling */
+ .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) }
+ .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) }
+ .exception_ranges : ONLY_IF_RW { *(.exception_ranges .exception_ranges*) }
+ /* Thread Local Storage sections */
+ .tdata : { *(.tdata .tdata.* .gnu.linkonce.td.*) }
+ .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }
+ .preinit_array :
+ {
+ PROVIDE_HIDDEN (__preinit_array_start = .);
+ KEEP (*(.preinit_array))
+ PROVIDE_HIDDEN (__preinit_array_end = .);
+ }
+ .init_array :
+ {
+ PROVIDE_HIDDEN (__init_array_start = .);
+ KEEP (*(SORT(.init_array.*)))
+ KEEP (*(.init_array ))
+ PROVIDE_HIDDEN (__init_array_end = .);
+ }
+ .fini_array :
+ {
+ PROVIDE_HIDDEN (__fini_array_start = .);
+ KEEP (*(SORT(.fini_array.*)))
+ KEEP (*(.fini_array ))
+ PROVIDE_HIDDEN (__fini_array_end = .);
+ }
+ .ctors :
+ {
+ /* gcc uses crtbegin.o to find the start of
+ the constructors, so we make sure it is
+ first. Because this is a wildcard, it
+ doesn't matter if the user does not
+ actually link against crtbegin.o; the
+ linker won't look for a file to match a
+ wildcard. The wildcard also means that it
+ doesn't matter which directory crtbegin.o
+ is in. */
+ KEEP (*crtbegin.o(.ctors))
+ KEEP (*crtbegin?.o(.ctors))
+ /* We don't want to include the .ctor section from
+ the crtend.o file until after the sorted ctors.
+ The .ctor section from the crtend file contains the
+ end of ctors marker and it must be last */
+ KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))
+ KEEP (*(SORT(.ctors.*)))
+ KEEP (*(.ctors))
+ }
+ .dtors :
+ {
+ KEEP (*crtbegin.o(.dtors))
+ KEEP (*crtbegin?.o(.dtors))
+ KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))
+ KEEP (*(SORT(.dtors.*)))
+ KEEP (*(.dtors))
+ }
+ .jcr : { KEEP (*(.jcr)) }
+ .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) }
+ .dynamic : { *(.dynamic) }
+ .got : { *(.got.plt) *(.igot.plt) *(.got) *(.igot) }
+ .data :
+ {
+ __data_start = . ;
+ *(.data .data.* .gnu.linkonce.d.*)
+ SORT(CONSTRUCTORS)
+ }
+ .data1 : { *(.data1) }
+ _edata = .; PROVIDE (edata = .);
+ . = .;
+ __bss_start = .;
+ __bss_start__ = .;
+ .bss :
+ {
+ *(.dynbss)
+ *(.bss .bss.* .gnu.linkonce.b.*)
+ *(COMMON)
+ /* Align here to ensure that the .bss section occupies space up to
+ _end. Align after .bss to ensure correct alignment even if the
+ .bss section disappears because there are no input sections.
+ FIXME: Why do we need it? When there is no .bss section, we don't
+ pad the .data section. */
+ . = ALIGN(. != 0 ? 32 / 8 : 1);
+ }
+ _bss_end__ = . ; __bss_end__ = . ;
+ . = ALIGN(32 / 8);
+ . = SEGMENT_START("ldata-segment", .);
+ . = ALIGN(32 / 8);
+ __end__ = . ;
+ _end = .; PROVIDE (end = .);
+ /* Stabs debugging sections. */
+ .stab 0 : { *(.stab) }
+ .stabstr 0 : { *(.stabstr) }
+ .stab.excl 0 : { *(.stab.excl) }
+ .stab.exclstr 0 : { *(.stab.exclstr) }
+ .stab.index 0 : { *(.stab.index) }
+ .stab.indexstr 0 : { *(.stab.indexstr) }
+ .comment 0 : { *(.comment) }
+ /* DWARF debug sections.
+ Symbols in the DWARF debugging sections are relative to the beginning
+ of the section so we begin them at 0. */
+ /* DWARF 1 */
+ .debug 0 : { *(.debug) }
+ .line 0 : { *(.line) }
+ /* GNU DWARF 1 extensions */
+ .debug_srcinfo 0 : { *(.debug_srcinfo) }
+ .debug_sfnames 0 : { *(.debug_sfnames) }
+ /* DWARF 1.1 and DWARF 2 */
+ .debug_aranges 0 : { *(.debug_aranges) }
+ .debug_pubnames 0 : { *(.debug_pubnames) }
+ /* DWARF 2 */
+ .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }
+ .debug_abbrev 0 : { *(.debug_abbrev) }
+ .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end ) }
+ .debug_frame 0 : { *(.debug_frame) }
+ .debug_str 0 : { *(.debug_str) }
+ .debug_loc 0 : { *(.debug_loc) }
+ .debug_macinfo 0 : { *(.debug_macinfo) }
+ /* SGI/MIPS DWARF 2 extensions */
+ .debug_weaknames 0 : { *(.debug_weaknames) }
+ .debug_funcnames 0 : { *(.debug_funcnames) }
+ .debug_typenames 0 : { *(.debug_typenames) }
+ .debug_varnames 0 : { *(.debug_varnames) }
+ /* DWARF 3 */
+ .debug_pubtypes 0 : { *(.debug_pubtypes) }
+ .debug_ranges 0 : { *(.debug_ranges) }
+ /* DWARF Extension. */
+ .debug_macro 0 : { *(.debug_macro) }
+ .stack 0x27F00000 :
+ {
+ _stack = .;
+ *(.stack)
+ }
+ .ARM.attributes 0 : { KEEP (*(.ARM.attributes)) KEEP (*(.gnu.attributes)) }
+ .note.gnu.arm.ident 0 : { KEEP (*(.note.gnu.arm.ident)) }
+ /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) }
}
+
std_init();
- if (fmount())
+ if (crmount())
poweroff(); // Failed to mount SD. Bomb out.
load_config(); // Load configuration.
wait();
}
+ if (get_opt_u32(OPTION_OVERLY_VERBOSE) && !get_opt_u32(OPTION_SILENCE)) {
+ struct mallinfo mal = mallinfo();
+ fprintf(stderr, "arena: %u\n"
+ "ordblks: %u\n"
+ "uordblks: %u\n"
+ "fordblks: %u\n",
+ mal.arena, mal.ordblks, mal.uordblks, mal.fordblks);
+ wait();
+ }
+
boot_firm(config->firm[0], PATH_NATIVE_P, PATH_MODULE_NATIVE);
fprintf(stderr, "Firmlaunch failed, returning to menu\n");
void
reset()
{
- fflush(stderr);
+ crflush(stderr);
- fumount(); // Unmount SD.
+ crumount(); // Unmount SD.
// Reboot.
fprintf(BOTTOM_SCREEN, "Rebooting system...\n");
void
poweroff()
{
- fflush(stderr);
+ crflush(stderr);
- fumount(); // Unmount SD.
+ crumount(); // Unmount SD.
// Power off
fprintf(BOTTOM_SCREEN, "Powering off system...\n");
current_menu_index_patches = 0;
if (!patches)
- patches = malloc(sizeof(struct options_s) * 258); // FIXME - hard limit. Implement realloc.
+ patches = memalign(16, sizeof(struct options_s) * 258); // FIXME - hard limit. Implement realloc.
patches[0].name = "Patches";
patches[0].desc = "";
{
uint32_t nandSize = getMMCDevice(0)->total_size;
- uint8_t *emunand_temp = (uint8_t*)malloc(2048);
+ uint8_t *emunand_temp = (uint8_t*)memalign(16, 2048);
uint32_t offset;
if (nandSize > 0x200000)
if (!emuCodeOffset)
return 1;
- FILE *f = fopen(PATH_EMUNAND_CODE, "r");
+ FILE *f = cropen(PATH_EMUNAND_CODE, "r");
if (!f)
return 1;
- uint32_t emunand_size = fsize(f);
- fread(emuCodeOffset, 1, emunand_size, f);
- fclose(f);
+ uint32_t emunand_size = crsize(f);
+ crread(emuCodeOffset, 1, emunand_size, f);
+ crclose(f);
uint32_t branchOffset = (uintptr_t)emuCodeOffset - ((uintptr_t)firm_loc + firm_loc->section[2].offset - firm_loc->section[2].address);
return;
// TODO - load other module cxis here
- FILE *f = fopen(fpath, "r");
+ FILE *f = cropen(fpath, "r");
if (!f) {
fprintf(stderr, "Module: %s not found\n", fpath);
return;
}
- size_t size = fsize(f);
+ size_t size = crsize(f);
uint8_t* temp = malloc(size);
- fread(temp, 1, size, f);
- fclose(f);
+ crread(temp, 1, size, f);
+ crclose(f);
int section_index = 0;
firm_section_h *sysmodule_section = &firm_modules->section[0];
}
int
-patch_modules(firm_h* firm_loc, const char* module_path)
+patch_modules(firm_h** firm_loc, const char* module_path)
{
- firm_modules = firm_loc;
+ firm_modules = *firm_loc;
recurse_call(module_path, inject_module);
return 0;
// Firmlaunch function offset - offset in BLX opcode (A4-16 - ARM DDI 0100E) + 1
uint32_t fOpenOffset = (uint32_t)(off + 9 - (-((*(uint32_t *)off & 0x00FFFFFF) << 2) & (0xFFFFFF << 2)) - (uint32_t)process9Offset + process9MemAddr);
- fprintf(stderr, "reboot: fopen @ %lx\n", fOpenOffset);
+ fprintf(stderr, "reboot: cropen @ %lx\n", fOpenOffset);
wait();
// Copy firmlaunch code
- FILE *f = fopen(PATH_REBOOT_HOOK, "r");
+ FILE *f = cropen(PATH_REBOOT_HOOK, "r");
if (!f)
panic("reboot: hook not found on SD\n");
- uint32_t size = fsize(f);
- fread(off, 1, size, f);
- fclose(f);
+ uint32_t size = crsize(f);
+ crread(off, 1, size, f);
+ crclose(f);
// Put the fOpen offset in the right location
- uint32_t *pos_fopen = (uint32_t *)memfind(off, size, "open", 4);
- if (!pos_fopen)
+ uint32_t *pos_cropen = (uint32_t *)memfind(off, size, "open", 4);
+ if (!pos_cropen)
return 1;
- *pos_fopen = fOpenOffset;
+ *pos_cropen = fOpenOffset;
uint32_t *pos_native = (uint32_t *)memfind(off, size, "NATF", 4);
uint32_t *pos_twl = (uint32_t *)memfind(off, size, "TWLF", 4);
fprintf(stderr, "reboot: rebc @ %lx\n", (uint32_t)pos_rebc);
- f = fopen(PATH_REBOOT_CODE, "r");
+ f = cropen(PATH_REBOOT_CODE, "r");
if (!f)
return 1;
- fread(mem, 1, fsize(f), f);
- fclose(f);
+ crread(mem, 1, crsize(f), f);
+ crclose(f);
return 0;
}
if (!svcTable[0x7B]) {
fprintf(stderr, "svc: 0x7B (backdoor) missing.\n");
- FILE *data = fopen(PATH_BACKDOOR, "r");
- uint32_t size = fsize(data);
+ FILE *data = cropen(PATH_BACKDOOR, "r");
+ uint32_t size = crsize(data);
fprintf(stderr, "Svc: backdoor is %lu bytes\n", size);
fprintf(stderr, "Svc: Read code to %lx\n", (uint32_t)svc_tab_open);
- fread(svc_tab_open, 1, size, data);
+ crread(svc_tab_open, 1, size, data);
- fclose(data);
+ crclose(data);
// memcpy(svc_tab_open, svcbackdoor, sizeof(svcbackdoor));
svcTable[0x7B] = 0xFFFF0000 + (uint32_t)((uint8_t *)svc_tab_open - (uint8_t *)exceptionsPage);
}
int
-patch_firm_all(uint64_t tid, firm_h* firm, const char* module_path)
+patch_firm_all(uint64_t tid, firm_h** firm, const char* module_path)
{
int exit = 0;
- execb(tid, firm);
+ execb(tid, *firm);
switch (tid) {
case 0x0004013800000002LLu: // NFIRM
case 0x0004013820000002LLu:
// Hook firmlaunch?
if (get_opt_u32(OPTION_REBOOT))
- patch_reboot(firm);
+ patch_reboot(*firm);
// Use EmuNAND?
if (get_opt_u32(OPTION_EMUNAND))
- patch_emunand(firm, get_opt_u32(OPTION_EMUNAND_INDEX));
+ patch_emunand(*firm, get_opt_u32(OPTION_EMUNAND_INDEX));
// Inject services?
if (get_opt_u32(OPTION_SVCS))
- if (patch_svc_calls(firm))
+ if (patch_svc_calls(*firm))
exit |= 2;
break;
case 0x0004013800000102LLu:
.word setup_stacks-.
__bss_start_offset:
-.word __bss_start-.
+.word __bss_start__-.
__bss_end_offset:
-.word __bss_end-.
+.word __bss_end__-.
clear_bss_offset:
.word clear_bss-.
f_unlink(PATH_TEMP "/screenshot.ppm");
// Open the screenshot blob used by hbmenu et al
- FILE* f = fopen(PATH_TEMP "/screenshot.ppm", "w");
+ FILE* f = cropen(PATH_TEMP "/screenshot.ppm", "w");
if (!f) return;
- fwrite("P6 400 480 255 ", 1, 15, f);
+ crwrite("P6 400 480 255 ", 1, 15, f);
for(int y = 0; y < 240; y++) {
for(int x = 0; x < 400; x++) {
int yDisplacement = ((240 - y - 1) * SCREEN_DEPTH);
int pos = xDisplacement + yDisplacement;
- fwrite(& framebuffers->top_left[pos + 3], 1, 1, f);
- fwrite(& framebuffers->top_left[pos + 2], 1, 1, f);
- fwrite(& framebuffers->top_left[pos + 1], 1, 1, f);
+ crwrite(& framebuffers->top_left[pos + 3], 1, 1, f);
+ crwrite(& framebuffers->top_left[pos + 2], 1, 1, f);
+ crwrite(& framebuffers->top_left[pos + 1], 1, 1, f);
}
}
for(int y = 0; y < 240; y++) {
for (int i = 0; i < 40 * 3; i++)
- fwrite(& zero, 1, 1, f);
+ crwrite(& zero, 1, 1, f);
for (int x = 0; x < 320; x++) {
int xDisplacement = (x * SCREEN_DEPTH * 240);
int yDisplacement = ((240 - y - 1) * SCREEN_DEPTH);
int pos = xDisplacement + yDisplacement;
- fwrite(& framebuffers->bottom[pos + 3], 1, 1, f);
- fwrite(& framebuffers->bottom[pos + 2], 1, 1, f);
- fwrite(& framebuffers->bottom[pos + 1], 1, 1, f);
+ crwrite(& framebuffers->bottom[pos + 3], 1, 1, f);
+ crwrite(& framebuffers->bottom[pos + 2], 1, 1, f);
+ crwrite(& framebuffers->bottom[pos + 1], 1, 1, f);
}
for (int i = 0; i < 40 * 3; i++)
- fwrite(& zero, 1, 1, f);
+ crwrite(& zero, 1, 1, f);
}
- fclose(f);
+ crclose(f);
fprintf(stderr, "Screenshot: %s\n", PATH_TEMP "/screenshot.ppm");
}
}
void load_bg_top(const char* fname_top) {
- FILE* f = fopen(fname_top, "r");
+ FILE* f = cropen(fname_top, "r");
if (!f) return;
for (int i=1; i < TOP_SIZE; i += 4) {
- fread(&top_bg[i], 1, 3, f);
+ crread(&top_bg[i], 1, 3, f);
}
- fclose(f);
+ crclose(f);
}
void load_bg_bottom(const char* fname_bottom) {
- FILE* f = fopen(fname_bottom, "r");
+ FILE* f = cropen(fname_bottom, "r");
if (!f)
return;
for (int i=1; i < BOTTOM_SIZE; i += 4) {
- fread(&bottom_bg[i], 1, 3, f);
+ crread(&bottom_bg[i], 1, 3, f);
}
- fclose(f);
+ crclose(f);
}
void set_font(const char* filename) {
// TODO - Unicode support. Right now, we only load 32
- FILE* f = fopen(filename, "r");
+ FILE* f = cropen(filename, "r");
if (!f) panic("Failed to load font file!\n");
unsigned int new_w, new_h;
- fread(&new_w, 1, 4, f);
- fread(&new_h, 1, 4, f);
+ crread(&new_w, 1, 4, f);
+ crread(&new_h, 1, 4, f);
if (new_w == 0 || new_h == 0) {
panic("Invalid font file: w/h is 0 - not loaded\n");
font_data = malloc(c_font_w * new_h * (256 - ' '));
- fread(font_data, 1, c_font_w * new_h * (256 - ' '), f); // Skip non-printing chars.
+ crread(font_data, 1, c_font_w * new_h * (256 - ' '), f); // Skip non-printing chars.
- fclose(f);
+ crclose(f);
font_w = new_w;
font_h = new_h;
if (log_size == 0)
return;
- FILE *f = fopen(PATH_BOOTLOG, "w");
- fseek(f, 0, SEEK_END);
+ FILE *f = cropen(PATH_BOOTLOG, "w");
+ crseek(f, 0, SEEK_END);
- fwrite(log_buffer, 1, log_size, f);
+ crwrite(log_buffer, 1, log_size, f);
- fclose(f);
+ crclose(f);
log_size = 0;
}
}
} else {
// FILE*, not stdin or stdout.
- fwrite(&c, 1, 1, (FILE *)buf);
+ crwrite(&c, 1, 1, (FILE *)buf);
}
}
}
void
-fflush(void *channel)
+crflush(void *channel)
{
if (channel == BOTTOM_SCREEN) {
dump_log(1);
}
int
-fmount(void)
+crmount(void)
{
if(!set_up_fs && ctr_fatfs_initialize(&nand_io, NULL, NULL, &sd_io))
return 1;
}
int
-fumount(void)
+crumount(void)
{
if (f_mount(NULL, "SD:", 1))
return 1;
}
FILE *
-fopen(const char *filename, const char *mode)
+cropen(const char *filename, const char *mode)
{
if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a')
return NULL; // Mode not valid.
}
void
-fclose(FILE *fp)
+crclose(FILE *fp)
{
if (fp == NULL || !fp->is_open)
return;
}
void
-fseek(FILE *fp, int64_t offset, int whence)
+crseek(FILE *fp, int64_t offset, int whence)
{
if (fp == NULL || !fp->is_open)
return;
fixed_offset = 0;
break;
case SEEK_CUR:
- fixed_offset = ftell(fp);
+ fixed_offset = crtell(fp);
break;
case SEEK_END:
- fixed_offset = fsize(fp);
+ fixed_offset = crsize(fp);
break;
default:
return;
}
size_t
-ftell(FILE *fp)
+crtell(FILE *fp)
{
if (fp == NULL || !fp->is_open)
return 0;
}
int
-feof(FILE *fp)
+creof(FILE *fp)
{
if (fp == NULL || !fp->is_open)
return 0;
}
size_t
-fsize(FILE *fp)
+crsize(FILE *fp)
{
if (fp == NULL || !fp->is_open)
return 0;
}
size_t
-fwrite(const void *buffer, size_t elementSize, size_t elementCnt, FILE *fp)
+crwrite(const void *buffer, size_t elementSize, size_t elementCnt, FILE *fp)
{
if (fp == NULL || !fp->is_open)
return 0;
}
size_t
-fread(void *buffer, size_t elementSize, size_t elementCnt, FILE *fp)
+crread(void *buffer, size_t elementSize, size_t elementCnt, FILE *fp)
{
if (!fp->is_open)
return 0;
size_t
write_file(void *data, const char *path, size_t size)
{
- FILE *temp = fopen(path, "w");
+ FILE *temp = cropen(path, "w");
if (!temp)
return 0;
if (!temp->is_open) {
- fclose(temp);
+ crclose(temp);
return 0;
}
- size_t wrote = fwrite(data, 1, size, temp);
+ size_t wrote = crwrite(data, 1, size, temp);
- fclose(temp);
+ crclose(temp);
return wrote;
}
size_t
read_file(void *data, const char *path, size_t size)
{
- FILE *temp = fopen(path, "r");
+ FILE *temp = cropen(path, "r");
if (!temp)
return 0;
if (!temp->is_open) {
- fclose(temp);
+ crclose(temp);
return 0;
}
- size_t read = fread(data, 1, size, temp);
+ size_t read = crread(data, 1, size, temp);
- fclose(temp);
+ crclose(temp);
return read;
}