+++ /dev/null
-#ifndef __FCRAM_H
-#define __FCRAM_H
-
-// File to keep track of all the fcram offsets in use.
-// It provides an easy overview of all that is used.
-
-#include <stdint.h>
-#include <stddef.h>
-
-extern void *fcram_temp;
-
-// Space between most of the locations
-#define FCRAM_SPACING 0x100000
-
-// Start of the space we use
-#define FCRAM_START 0x24000000
-
-// firm.c
-#define FCRAM_FIRM_LOC FCRAM_START
-#define FCRAM_TWL_FIRM_LOC (FCRAM_FIRM_LOC + FCRAM_SPACING) // Double size
-#define FCRAM_AGB_FIRM_LOC (FCRAM_TWL_FIRM_LOC + FCRAM_SPACING * 2)
-
-// Throwaway temporary space. Don't expect it to stay sane.
-#define FCRAM_JUNK_LOC FCRAM_START
-
-// Location to perform static allocations at.
-#define FCRAM_STATIC_ALLOC_LOC (FCRAM_START + FCRAM_SPACING)
-
-// Grow memory segment.
-void *fake_sbrk(size_t bytes);
-
-// Allocate memory for use.
-void *malloc (size_t size);
-
-// Free in-use memory.
-void free (void* ptr);
-
-#endif
--- /dev/null
+#ifndef __FCRAM_H
+#define __FCRAM_H
+
+#include <stdint.h>
+#include <stddef.h>
+
+// Space between most of the locations (remove me)
+#define FCRAM_SPACING 0x100000
+
+// Grow program break
+void *sbrk(size_t bytes);
+
+// Allocate memory for use.
+void *malloc (size_t size);
+
+// Free in-use memory.
+void free (void* ptr);
+
+#endif
inc_dir = $(top_srcdir)/include
-corbenik_SOURCES = patch/reboot.c patch/svc.c patch/module.c patch/emunand.c main.c option.c std/fs.c std/draw.c std/memory.c std/abort.c menu.c chain.c firm/version.c firm/firm.c firm/decryptor.c firm/fcram.c interp.c input.c patcher.c display.c start.s interrupt.c screeninit.c
+corbenik_SOURCES = patch/reboot.c patch/svc.c patch/module.c patch/emunand.c main.c option.c std/fs.c std/draw.c std/memory.c std/abort.c menu.c chain.c firm/version.c firm/firm.c firm/decryptor.c std/allocator.c interp.c input.c patcher.c display.c start.s interrupt.c screeninit.c
// Firmware is likely encrypted. Decrypt.
if (!read_file(firm_key, path_firmkey, AES_BLOCK_SIZE)) {
+ uint8_t* temp = malloc(FCRAM_SPACING);
// Missing firmkey. Attempt to get from CETK (only works if system was booted)
- if (!read_file((void *)FCRAM_JUNK_LOC, path_cetk, FCRAM_SPACING) || decrypt_cetk_key(firm_key, (void *)FCRAM_JUNK_LOC)) {
+ if (!read_file(temp, path_cetk, FCRAM_SPACING) || decrypt_cetk_key(firm_key, temp)) {
fprintf(stderr, " No firmkey and failed to extract from cetk\n");
return 1;
} else {
fprintf(stderr, " Saving firmkey for future use.\n");
write_file(firm_key, path_firmkey, AES_BLOCK_SIZE);
}
+ free(temp);
} else {
fprintf(stderr, " Read firmkey from filesystem.\n");
}
. = ALIGN(4);
- __end__ = 0x20400000;
+ __end__ = .;
}
#include <ctr9/io.h>
#include <common.h>
-uint8_t *emunand_temp = (uint8_t *)FCRAM_JUNK_LOC;
-
void
verify_emunand(uint32_t index, uint32_t *off, uint32_t *head)
{
uint32_t nandSize = getMMCDevice(0)->total_size;
+ uint8_t *emunand_temp = (uint8_t*)malloc(2048);
+
uint32_t offset;
if (nandSize > 0x200000)
offset = 0x400000 * index;
} else {
abort("emunand: selected NAND image is not valid.\n");
}
+
+ free(emunand_temp);
}
static void *
}
size_t size = fsize(f);
- fread((void *)FCRAM_JUNK_LOC, 1, size, f);
+ uint8_t* temp = malloc(size);
+ fread(temp, 1, size, f);
fclose(f);
// Look for the section that holds all the sysmodules
if (!sysmodule_section) {
fprintf(stderr, "Module: sysmodule section not found\n");
+ free(temp);
return 1;
}
- ncch_h *module = (ncch_h *)FCRAM_JUNK_LOC;
+ ncch_h *module = (ncch_h *)temp;
ncch_h *sysmodule = (ncch_h *)((uint32_t)firm_loc + sysmodule_section->offset);
// Check if we want to replace an existing sysmodule
fprintf(stderr, "Module: injected modules.\n");
+ free(temp);
+
return 0;
}
// 16 <- AES block size.
#define SALLOC_ALIGN 16
-void *fcram_temp = (void *)0x23000000;
-
-void *fcram_static_mem = (void*)FCRAM_STATIC_ALLOC_LOC;
-
struct alloc_info* first_mem = NULL;
-// Low level static allocator / sbrk-like function.
-void *fake_sbrk(size_t bytes) {
- void *ret = fcram_static_mem;
+void* sbrk(int incr) {
+ extern uint32_t __end__; /* Defined by the linker */
+ static uint32_t *heap_end;
+ uint32_t *prev_heap_end;
+
+ if (heap_end == 0) {
+ heap_end = &__end__;
+ }
- fcram_static_mem = (uint8_t*)fcram_static_mem + bytes;
+ prev_heap_end = heap_end;
+ if (heap_end + incr > stack_ptr)
+ abort("Heap overflowed!\n");
- return ret;
+ heap_end += incr;
+ return (void*) prev_heap_end;
}
// This is an incredibly crappy and inefficient implementation of malloc/free nicked from stackoverflow.
block = block->next;
}
- block = (free_block*)fake_sbrk(size);
+ block = (free_block*)sbrk(size);
block->size = size;
return ((char*)block) + sizeof(free_block);