From 87592ff3d863be01dedd452d61254d3a91c12d65 Mon Sep 17 00:00:00 2001 From: Luke Street Date: Mon, 29 Feb 2016 00:47:23 -0500 Subject: [PATCH] Add arm9loaderhax build --- Makefile | 11 ++- a9lh.ld | 130 ++++++++++++++++++++++++++++++++ a9lh.specs | 5 ++ source/abstraction/a9lh-start.s | 93 +++++++++++++++++++++++ source/draw.h | 2 +- source/fs.c | 2 +- 6 files changed, 240 insertions(+), 3 deletions(-) create mode 100644 a9lh.ld create mode 100644 a9lh.specs create mode 100644 source/abstraction/a9lh-start.s diff --git a/Makefile b/Makefile index a3dba4a..74a13c2 100644 --- a/Makefile +++ b/Makefile @@ -54,6 +54,8 @@ ifeq ($(EXEC_METHOD),GATEWAY) LDFLAGS += --specs=../gateway.specs else ifeq ($(EXEC_METHOD),BOOTSTRAP) LDFLAGS += --specs=../bootstrap.specs +else ifeq ($(EXEC_METHOD),A9LH) + LDFLAGS += --specs=../a9lh.specs endif LIBS := @@ -108,7 +110,7 @@ export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) -.PHONY: common clean all gateway bootstrap cakehax cakerop brahma release +.PHONY: common clean all gateway bootstrap a9lh cakehax cakerop brahma release #--------------------------------------------------------------------------------- all: brahma @@ -128,6 +130,10 @@ gateway: common bootstrap: common @make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile EXEC_METHOD=BOOTSTRAP +a9lh: common + @make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile EXEC_METHOD=A9LH + @cp $(OUTPUT).bin $(OUTPUT_D)/arm9loaderhax.bin + cakehax: submodules common @make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile EXEC_METHOD=GATEWAY @make dir_out=$(OUTPUT_D) name=$(TARGET).dat -C CakeHax bigpayload @@ -151,6 +157,8 @@ release: @make --no-print-directory gateway @-make --no-print-directory cakerop @rm -fr $(BUILD) $(OUTPUT).bin $(OUTPUT).elf $(CURDIR)/$(LOADER)/data + @make --no-print-directory a9lh + @rm -fr $(BUILD) $(OUTPUT).bin $(OUTPUT).elf $(CURDIR)/$(LOADER)/data @-make --no-print-directory brahma @[ -d $(RELEASE) ] || mkdir -p $(RELEASE) @[ -d $(RELEASE)/$(TARGET) ] || mkdir -p $(RELEASE)/$(TARGET) @@ -161,6 +169,7 @@ release: @-cp $(OUTPUT).nds $(RELEASE) @-cp $(OUTPUT).3dsx $(RELEASE)/$(TARGET) @-cp $(OUTPUT).smdh $(RELEASE)/$(TARGET) + @-cp $(OUTPUT_D)/arm9loaderhax.bin $(RELEASE) @cp $(CURDIR)/scripts/*.py $(RELEASE)/scripts @cp $(CURDIR)/README.md $(RELEASE) @-[ ! -n "$(strip $(THEME))" ] || (mkdir $(RELEASE)/$(THEME) && cp $(CURDIR)/resources/$(THEME)/*.bin $(RELEASE)/$(THEME)) diff --git a/a9lh.ld b/a9lh.ld new file mode 100644 index 0000000..21d6f08 --- /dev/null +++ b/a9lh.ld @@ -0,0 +1,130 @@ +OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm", "elf32-littlearm") +OUTPUT_ARCH(arm) +ENTRY(_start) + +MEMORY + { + ram : ORIGIN = 0x23F00000, LENGTH = 128K + } + +SECTIONS +{ + .init : + { + __text_start = . ; + KEEP (*(.init)) + . = ALIGN(4); /* REQUIRED. LD is flaky without it. */ + } >ram = 0xff + .plt : { *(.plt) } >ram = 0xff + + .text : /* ALIGN (4): */ + { + *(.text .stub .text.* .gnu.linkonce.t.*) + KEEP (*(.text.*personality*)) + /* .gnu.warning sections are handled specially by elf32.em. */ + *(.gnu.warning) + *(.glue_7t) *(.glue_7) *(.vfp11_veneer) + . = ALIGN(4); /* REQUIRED. LD is flaky without it. */ + } >ram = 0xff + + .fini : + { + KEEP (*(.fini)) + } >ram =0xff + + __text_end = . ; + + .rodata : + { + *(.rodata) + *all.rodata*(*) + *(.roda) + *(.rodata.*) + *(.gnu.linkonce.r*) + SORT(CONSTRUCTORS) + . = ALIGN(4); /* REQUIRED. LD is flaky without it. */ + } >ram = 0xff + + .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >ram + __exidx_start = .; + .ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) } >ram + __exidx_end = .; + +/* Ensure the __preinit_array_start label is properly aligned. We + could instead move the label definition inside the section, but + the linker would then create the section even if it turns out to + be empty, which isn't pretty. */ + . = ALIGN(32 / 8); + PROVIDE (__preinit_array_start = .); + .preinit_array : { KEEP (*(.preinit_array)) } >ram = 0xff + PROVIDE (__preinit_array_end = .); + PROVIDE (__init_array_start = .); + .init_array : { KEEP (*(.init_array)) } >ram = 0xff + PROVIDE (__init_array_end = .); + PROVIDE (__fini_array_start = .); + .fini_array : { KEEP (*(.fini_array)) } >ram = 0xff + PROVIDE (__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 (*(EXCLUDE_FILE (*crtend.o) .ctors)) + KEEP (*(SORT(.ctors.*))) + KEEP (*(.ctors)) + . = ALIGN(4); /* REQUIRED. LD is flaky without it. */ + } >ram = 0xff + + .dtors : + { + KEEP (*crtbegin.o(.dtors)) + KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors)) + KEEP (*(SORT(.dtors.*))) + KEEP (*(.dtors)) + . = ALIGN(4); /* REQUIRED. LD is flaky without it. */ + } >ram = 0xff + + .eh_frame : + { + KEEP (*(.eh_frame)) + . = ALIGN(4); /* REQUIRED. LD is flaky without it. */ + } >ram = 0xff + + .gcc_except_table : + { + *(.gcc_except_table) + . = ALIGN(4); /* REQUIRED. LD is flaky without it. */ + } >ram = 0xff + .jcr : { KEEP (*(.jcr)) } >ram = 0 + .got : { *(.got.plt) *(.got) } >ram = 0 + + .data ALIGN(4) : { + __data_start = ABSOLUTE(.); + *(.data) + *(.data.*) + *(.gnu.linkonce.d*) + CONSTRUCTORS + . = ALIGN(4); + __data_end = ABSOLUTE(.) ; + } >ram = 0xff + + .bss ALIGN(4) : + { + __bss_start = ABSOLUTE(.); + __bss_start__ = ABSOLUTE(.); + *(.dynbss) + *(.gnu.linkonce.b*) + *(.bss*) + *(COMMON) + . = ALIGN(4); /* REQUIRED. LD is flaky without it. */ + __bss_end__ = ABSOLUTE(.); + __end__ = ABSOLUTE(.); + } >ram + + .stack 0x80000 : { _stack = .; *(.stack) } +} diff --git a/a9lh.specs b/a9lh.specs new file mode 100644 index 0000000..79b8164 --- /dev/null +++ b/a9lh.specs @@ -0,0 +1,5 @@ +%rename link old_link + +*link: +%(old_link) -T ../a9lh.ld%s + diff --git a/source/abstraction/a9lh-start.s b/source/abstraction/a9lh-start.s new file mode 100644 index 0000000..0bed011 --- /dev/null +++ b/source/abstraction/a9lh-start.s @@ -0,0 +1,93 @@ +#ifdef EXEC_A9LH + +.section ".init" +.global _start +.extern main +.align 4 +.arm + +#define SIZE_32KB 0b01110 +#define SIZE_128KB 0b10000 +#define SIZE_512KB 0b10010 +#define SIZE_2MB 0b10100 +#define SIZE_128MB 0b11010 +#define SIZE_256MB 0b11011 +#define SIZE_4GB 0b11111 + +@ Makes a MPU partition value +#define MAKE_PARTITION(offset, size_enum) \ + (((offset) >> 12 << 12) | ((size_enum) << 1) | 1) + + +_start: + b _init + +_mpu_partition_table: + .word MAKE_PARTITION(0x00000000, SIZE_4GB) @ 0: Background region + .word MAKE_PARTITION(0x00000000, SIZE_128MB) @ 1: Instruction TCM (mirrored every 32KB) + .word MAKE_PARTITION(0x08000000, SIZE_2MB) @ 2: ARM9 internal memory + .word MAKE_PARTITION(0x10000000, SIZE_128MB) @ 3: IO region + .word MAKE_PARTITION(0x18000000, SIZE_128MB) @ 4: external device memory + .word MAKE_PARTITION(0x1FF80000, SIZE_512KB) @ 5: AXI WRAM + .word MAKE_PARTITION(0x20000000, SIZE_256MB) @ 6: FCRAM + .word 0 @ 7: Unused + +_populate_mpu: + push {r4-r5, lr} + ldr r4, =_mpu_partition_table + + ldr r5, [r4, #0x0] @ mmu_partition_table[0] load + mcr p15, 0, r5, c6, c0, 0 @ mmu_partition_table[0] write + ldr r5, [r4, #0x4] + mcr p15, 0, r5, c6, c1, 0 + ldr r5, [r4, #0x8] + mcr p15, 0, r5, c6, c2, 0 + ldr r5, [r4, #0xC] + mcr p15, 0, r5, c6, c3, 0 + ldr r5, [r4, #0x10] + mcr p15, 0, r5, c6, c4, 0 + ldr r5, [r4, #0x14] + mcr p15, 0, r5, c6, c5, 0 + ldr r5, [r4, #0x18] + mcr p15, 0, r5, c6, c6, 0 + ldr r5, [r4, #0x1C] + mcr p15, 0, r5, c6, c7, 0 + + @ Give read/write access to all the memory regions + ldr r5, =0x03333333 + mcr p15, 0, r5, c5, c0, 2 @ data access + ldr r5, =0x03300330 + mcr p15, 0, r5, c5, c0, 3 @ instruction access + + mov r5, #0x66 + mcr p15, 0, r5, c2, c0, 0 @ data cachable + mcr p15, 0, r5, c2, c0, 1 @ instruction cachable + + mov r5, #0x10 + mcr p15, 0, r5, c3, c0, 0 @ data bufferable + + pop {r4-r5, pc} + +_enable_caches: + push {r4-r5, lr} + + bl _populate_mpu + mov r5, #0 + mcr p15, 0, r5, c7, c5, 0 @ flush I-cache + mcr p15, 0, r5, c7, c6, 0 @ flush D-cache + + mrc p15, 0, r4, c1, c0, 0 + orr r4, r4, #(1<<12) @ instruction cache enable + orr r4, r4, #(1<<2) @ data cache enable + orr r4, r4, #(1<<0) @ mpu enable + mcr p15, 0, r4, c1, c0, 0 + + pop {r4-r5, pc} + +_init: + push {r0-r12, lr} + + bl _enable_caches + bl main + +#endif // EXEC_A9LH \ No newline at end of file diff --git a/source/draw.h b/source/draw.h index 2b2f80a..c517871 100644 --- a/source/draw.h +++ b/source/draw.h @@ -35,7 +35,7 @@ #define DBG_N_CHARS_Y ((DBG_END_Y - DBG_START_Y) / DBG_STEP_Y) #define DBG_N_CHARS_X (((DBG_END_X - DBG_START_X) / 8) + 1) -#ifdef EXEC_GATEWAY +#if defined(EXEC_GATEWAY) || defined(EXEC_A9LH) #define TOP_SCREEN0 (u8*)(*(u32*)((uint32_t)0x080FFFC0 + 4 * (*(u32*)0x080FFFD8 & 1))) #define BOT_SCREEN0 (u8*)(*(u32*)((uint32_t)0x080FFFD0 + 4 * (*(u32*)0x080FFFDC & 1))) #define TOP_SCREEN1 TOP_SCREEN0 diff --git a/source/fs.c b/source/fs.c index f7aeadc..8038ca2 100644 --- a/source/fs.c +++ b/source/fs.c @@ -9,7 +9,7 @@ static DIR dir; bool InitFS() { -#ifndef EXEC_GATEWAY +#ifdef EXEC_BOOTSTRAP // TODO: Magic? *(u32*)0x10000020 = 0; *(u32*)0x10000020 = 0x340;