From 75dfcd07a97a65dc86ad66080c56216406dd3e1f Mon Sep 17 00:00:00 2001 From: TuxSH Date: Mon, 7 May 2018 01:02:23 +0200 Subject: [PATCH] Add chainloader (stage2) (need to edit more files) --- fusee/fusee-secondary/linker.ld | 44 +++++++++++++++++-------- fusee/fusee-secondary/src/chainloader.c | 12 +++++++ fusee/fusee-secondary/src/chainloader.h | 13 ++++++++ fusee/fusee-secondary/src/start.s | 12 ++++++- 4 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 fusee/fusee-secondary/src/chainloader.c create mode 100644 fusee/fusee-secondary/src/chainloader.h diff --git a/fusee/fusee-secondary/linker.ld b/fusee/fusee-secondary/linker.ld index c5a94b7d8..fca868305 100644 --- a/fusee/fusee-secondary/linker.ld +++ b/fusee/fusee-secondary/linker.ld @@ -3,6 +3,12 @@ OUTPUT_ARCH(arm) ENTRY(_start) /* Mostly copied from https://github.com/devkitPro/buildscripts/blob/master/dkarm-eabi/crtls/3dsx.ld */ +MEMORY +{ + NULL : ORIGIN = 0x00000000, LENGTH = 0x1000 + main : ORIGIN = 0xFFF00000, LENGTH = 0x00100000 + low_iram : ORIGIN = 0x40003000, LENGTH = 0x4000 +} SECTIONS { @@ -30,7 +36,7 @@ SECTIONS /* .fini */ KEEP( *(.fini) ) . = ALIGN(4); - } + } >main .rodata : { @@ -41,14 +47,14 @@ SECTIONS *(.gnu.linkonce.r*) SORT(CONSTRUCTORS) . = ALIGN(4); - } + } >main .preinit_array ALIGN(4) : { PROVIDE (__preinit_array_start = .); KEEP (*(.preinit_array)) PROVIDE (__preinit_array_end = .); - } + } >main .init_array ALIGN(4) : { @@ -56,7 +62,7 @@ SECTIONS KEEP (*(SORT(.init_array.*))) KEEP (*(.init_array)) PROVIDE (__init_array_end = .); - } + } >main .fini_array ALIGN(4) : { @@ -64,7 +70,7 @@ SECTIONS KEEP (*(.fini_array)) KEEP (*(SORT(.fini_array.*))) PROVIDE (__fini_array_end = .); - } + } >main .ctors ALIGN(4) : { @@ -72,7 +78,7 @@ SECTIONS KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors)) KEEP (*(SORT(.ctors.*))) KEEP (*(.ctors)) - } + } >main .dtors ALIGN(4) : { @@ -80,11 +86,11 @@ SECTIONS KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors)) KEEP (*(SORT(.dtors.*))) KEEP (*(.dtors)) - } + } >main - .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } + .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >main __exidx_start = .; - ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) } + ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) } >main __exidx_end = .; .data : @@ -94,19 +100,31 @@ SECTIONS *(.gnu.linkonce.d*) CONSTRUCTORS . = ALIGN(4); - } + } >main + + .chainloader : + { + . = ALIGN(32); + KEEP(*(.chainloader.text.start)) + build/chainloader.o(.text*) + build/chainloader.o(.rodata*) + build/chainloader.o(.data*) + . = ALIGN(8); + build/chainloader.o(.bss*) + . = ALIGN(32); + } >low_iram - __bss_start__ = ALIGN(32); .bss : { + __bss_start__ = ALIGN(32); *(.dynbss) *(.bss) *(.bss.*) *(.gnu.linkonce.b*) *(COMMON) . = ALIGN(8); - } - __bss_end__ = .; + __bss_end__ = .; + } >main __end__ = ABSOLUTE(.) ; /* ================== diff --git a/fusee/fusee-secondary/src/chainloader.c b/fusee/fusee-secondary/src/chainloader.c new file mode 100644 index 000000000..144095353 --- /dev/null +++ b/fusee/fusee-secondary/src/chainloader.c @@ -0,0 +1,12 @@ +#include "chainloader.h" + +uint8_t g_payload_arg_data[PAYLOAD_ARG_DATA_MAX_SIZE] = {1}; + +#pragma GCC optimize (3) +void relocate_and_chainload_main(uintptr_t load_address, uintptr_t src_address, size_t size, int argc) { + for(size_t i = 0; i < size; i++) { + *(uint8_t *)(load_address + i) = *(uint8_t *)(src_address + i); + } + + ((void (*)(int, void *))load_address)(argc, g_payload_arg_data); +} diff --git a/fusee/fusee-secondary/src/chainloader.h b/fusee/fusee-secondary/src/chainloader.h new file mode 100644 index 000000000..19010d284 --- /dev/null +++ b/fusee/fusee-secondary/src/chainloader.h @@ -0,0 +1,13 @@ +#ifndef FUSEE_CHAINLOADER_H +#define FUSEE_CHAINLOADER_H + +#include +#include + +#define PAYLOAD_ARG_DATA_MAX_SIZE 0x1000 + +extern uint8_t g_payload_arg_data[PAYLOAD_ARG_DATA_MAX_SIZE]; + +void relocate_and_chainload(uintptr_t load_address, uintptr_t src_address, size_t size, int argc); + +#endif diff --git a/fusee/fusee-secondary/src/start.s b/fusee/fusee-secondary/src/start.s index 5a060b0be..b90b9b12f 100644 --- a/fusee/fusee-secondary/src/start.s +++ b/fusee/fusee-secondary/src/start.s @@ -2,10 +2,11 @@ mov r\@, #0 .endm -.section .text.start +.section .text.start, "ax", %progbits .arm .align 5 .global _start +.type _start, %function _start: /* Insert NOPs for convenience (i.e. to use Nintendo's BCTs, for example) */ .rept 16 @@ -57,3 +58,12 @@ _start: ldmfd sp!, {r0, r1} bl main b . + +.section .chainloader.text.start, "ax", %progbits +.arm +.align 5 +.global relocate_and_chainload +.type relocate_and_chainload, %function +relocate_and_chainload: + ldr sp, =0x40010000 + b relocate_and_chainload_main