diff --git a/asm/make.cmd b/asm/make.cmd index 0617458..5c53250 100644 --- a/asm/make.cmd +++ b/asm/make.cmd @@ -1,4 +1,4 @@ @echo off -for /R %%i IN (*.S) DO E:\devkitPro\devkitPPC\bin\powerpc-eabi-as.exe %%i -o %%~ni.elf -for /R %%i IN (*.S) DO E:\devkitPro\devkitPPC\bin\powerpc-eabi-strip.exe -s %%~ni.elf -O binary -o %%~ni.bin -for /R %%i in (*.bin) DO "C:\Users\crediar\Documents\Visual Studio 2010\Projects\bin2h\Release\bin2h.exe" %%~ni.bin +for /R %%i IN (*.S) DO %devkitpro:~1,1%:\%devkitpro:~3%\devkitPPC\bin\powerpc-eabi-as.exe %%i -o %%~ni.elf +for /R %%i IN (*.S) DO %devkitpro:~1,1%:\%devkitpro:~3%\devkitPPC\bin\powerpc-eabi-strip.exe -s %%~ni.elf -O binary -o %%~ni.bin +for /R %%i in (*.bin) DO "..\tools\bin2h\bin2h.exe" %%~ni.bin diff --git a/elfloader/.deps/string.d b/elfloader/.deps/string.d new file mode 100644 index 0000000..53b9f7d --- /dev/null +++ b/elfloader/.deps/string.d @@ -0,0 +1,7 @@ +string.o: string.c ../string.c ../string.h ../global.h + +../string.c: + +../string.h: + +../global.h: diff --git a/elfloader/.deps/stub.d b/elfloader/.deps/stub.d new file mode 100644 index 0000000..0f667a5 --- /dev/null +++ b/elfloader/.deps/stub.d @@ -0,0 +1,18 @@ +stub.o: stub.c types.h utils.h start.h hollywood.h string.h ../string.h \ + ../global.h elf.h + +types.h: + +utils.h: + +start.h: + +hollywood.h: + +string.h: + +../string.h: + +../global.h: + +elf.h: diff --git a/elfloader/.deps/utils.d b/elfloader/.deps/utils.d new file mode 100644 index 0000000..87eecaf --- /dev/null +++ b/elfloader/.deps/utils.d @@ -0,0 +1,9 @@ +utils.o: utils.c types.h utils.h hollywood.h start.h + +types.h: + +utils.h: + +hollywood.h: + +start.h: diff --git a/elfloader/Makefile b/elfloader/Makefile new file mode 100644 index 0000000..2199f7f --- /dev/null +++ b/elfloader/Makefile @@ -0,0 +1,23 @@ +include starlet.mk + +CFLAGS += -fpic -fno-builtin-memcpy +LDSCRIPT = stub.ld +LIBS = -lgcc + +TARGET = elfloader.elf +TARGET_BIN = elfloader.bin +OBJS = start.o stub.o string.o utils.o + +include common.mk + +all: $(TARGET_BIN) + +$(TARGET_BIN): $(TARGET) + @echo " OBJCPY $@" + @$(OBJCOPY) -O binary $< $@ + +clean: myclean + +myclean: + -rm -f $(TARGET_BIN) + diff --git a/elfloader/common.mk b/elfloader/common.mk new file mode 100644 index 0000000..cd9fdcb --- /dev/null +++ b/elfloader/common.mk @@ -0,0 +1,60 @@ +AR = $(PREFIX)ar +AS = $(PREFIX)as +CC = $(PREFIX)gcc +CXX = $(PREFIX)g++ +LD = $(PREFIX)ld +OBJCOPY = $(PREFIX)objcopy +RANLIB = $(PREFIX)ranlib +STRIP = $(PREFIX)strip + +BIN2S = $(DEVKITPPC)/bin/bin2s + +ifeq ($(NOMAPFILE),) +LDFLAGS += -Wl,-Map,$(TARGET).map +endif + +ifneq ($(LDSCRIPT),) +LDFLAGS += -Wl,-T$(LDSCRIPT) +endif + +DEPDIR = .deps + +all: $(TARGET) + +$(TARGET): $(OBJS) + @echo " LINK $@" + @$(CC) $(LDFLAGS) $(OBJS) $(LIBS) -o $@ + +ifneq ($(LDSCRIPT),) +$(TARGET): $(LDSCRIPT) +endif + +%.o: %.c + @echo " COMPILE $<" + @mkdir -p $(DEPDIR) + @$(CC) $(CFLAGS) $(DEFINES) -Wp,-MMD,$(DEPDIR)/$(*F).d,-MQ,"$@",-MP -c $< -o $@ + +%.o: %.s + @echo " ASSEMBLE $<" + @$(CC) $(CFLAGS) $(DEFINES) $(ASFLAGS) -c $< -o $@ + +%.o: %.S + @echo " ASSEMBLE $<" + @$(CC) $(CFLAGS) $(DEFINES) $(ASFLAGS) -c $< -o $@ + +clean: + rm -rf $(DEPDIR) + rm -f $(TARGET) $(TARGET).map $(OBJS) + +define bin2o + @echo " BIN2S $(notdir $<)" + @$(BIN2S) -a 32 $< | $(AS) -o $(@) + @echo "extern const u8" `(echo $( `(echo $(> `(echo $(> `(echo $( + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ +#ifndef __ELF_H__ +#define __ELF_H__ + +#include "types.h" + +#define EI_NIDENT 16 + +typedef struct { + unsigned char e_ident[EI_NIDENT]; + u16 e_type; + u16 e_machine; + u32 e_version; + void *e_entry; + u32 e_phoff; + u32 e_shoff; + u32 e_flags; + u16 e_ehsize; + u16 e_phentsize; + u16 e_phnum; + u16 e_shentsize; + u16 e_shnum; + u16 e_shtrndx; +} Elf32_Ehdr; + +typedef struct { + u32 p_type; + u32 p_offset; + void *p_vaddr; + void *p_paddr; + u32 p_filesz; + u32 p_memsz; + u32 p_flags; + u32 p_align; +} Elf32_Phdr; + +#define PT_NULL 0 +#define PT_LOAD 1 +#define PT_DYNAMIC 2 +#define PT_INTERP 3 +#define PT_NOTE 4 +#define PT_SHLIB 5 +#define PT_PHDR 6 + +#endif + diff --git a/elfloader/elfloader.elf.map b/elfloader/elfloader.elf.map new file mode 100644 index 0000000..6e09c4c --- /dev/null +++ b/elfloader/elfloader.elf.map @@ -0,0 +1,168 @@ +Archive member included because of file (symbol) + +e:/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.6.1/be\libgcc.a(_udivsi3.o) + utils.o (__aeabi_uidiv) +e:/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.6.1/be\libgcc.a(_dvmd_tls.o) + e:/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.6.1/be\libgcc.a(_udivsi3.o) (__aeabi_idiv0) + +Discarded input sections + + .text 0x00000000 0x0 start.o + .data 0x00000000 0x0 start.o + .bss 0x00000000 0x0 start.o + .text 0x00000000 0x0 stub.o + .data 0x00000000 0x0 stub.o + .bss 0x00000000 0x0 stub.o + .text 0x00000000 0x0 string.o + .data 0x00000000 0x0 string.o + .bss 0x00000000 0x0 string.o + .text.strnlen 0x00000000 0x2c string.o + .text.strlen 0x00000000 0x20 string.o + .text.strncpy 0x00000000 0x4c string.o + .text.strcpy 0x00000000 0x18 string.o + .text.strcmp 0x00000000 0x28 string.o + .text.strncmp 0x00000000 0x48 string.o + .text.memset 0x00000000 0x1c string.o + .text.strchr 0x00000000 0x28 string.o + .text 0x00000000 0x0 utils.o + .data 0x00000000 0x0 utils.o + .bss 0x00000000 0x0 utils.o + .data 0x00000000 0x0 e:/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.6.1/be\libgcc.a(_udivsi3.o) + .bss 0x00000000 0x0 e:/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.6.1/be\libgcc.a(_udivsi3.o) + .data 0x00000000 0x0 e:/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.6.1/be\libgcc.a(_dvmd_tls.o) + .bss 0x00000000 0x0 e:/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.6.1/be\libgcc.a(_dvmd_tls.o) + +Memory Configuration + +Name Origin Length Attributes +*default* 0x00000000 0xffffffff + +Linker script and memory map + + 0x00000000 __base_addr = 0x0 + 0x00000000 . = __base_addr + +.header 0x00000000 0x10 + 0x00000000 __header = . + 0x00000000 0x4 LONG 0x10 __code_start + 0x00000004 0x4 LONG 0x4b0 __loader_size + 0x00000008 0x4 LONG 0x0 + 0x0000000c 0x4 LONG 0x0 + 0x00000010 . = ALIGN (0x10) + 0x00000010 __code_start = . + +.init 0x00000010 0xc8 + *(.init) + .init 0x00000010 0xc8 start.o + 0x00000010 _start + 0x000000bc debug_output + 0x000000d8 . = ALIGN (0x4) + +.got 0x000000d8 0x0 + 0x000000d8 __got_start = . + *(.got.*) + *(.got) + 0x000000d8 . = ALIGN (0x4) + 0x000000d8 __got_end = . + +.text 0x000000d8 0x2d4 + *(.text.*) + .text.disable_boot0 + 0x000000d8 0x1c stub.o + .text.mem_setswap + 0x000000f4 0x1c stub.o + .text.loadelf 0x00000110 0x90 stub.o + 0x00000110 loadelf + .text._main 0x000001a0 0x44 stub.o + 0x000001a0 _main + .text.memcmp 0x000001e4 0x48 string.o + 0x000001e4 memcmp + .text.udelay 0x0000022c 0x38 utils.o + 0x0000022c udelay + .text.panic 0x00000264 0x30 utils.o + 0x00000264 panic + *(.gnu.warning) + *(.gnu.linkonce.t*) + *(.glue_7) + .glue_7 0x00000000 0x0 linker stubs + *(.glue_7t) + .glue_7t 0x00000000 0x0 linker stubs + 0x00000294 . = ALIGN (0x4) + .text 0x00000294 0x114 e:/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.6.1/be\libgcc.a(_udivsi3.o) + 0x00000294 __aeabi_uidiv + 0x00000294 __udivsi3 + 0x00000388 __aeabi_uidivmod + .text 0x000003a8 0x4 e:/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.6.1/be\libgcc.a(_dvmd_tls.o) + 0x000003a8 __aeabi_idiv0 + 0x000003a8 __aeabi_ldiv0 + 0x000003ac __text_end = . + +.vfp11_veneer 0x000003ac 0x0 + .vfp11_veneer 0x00000000 0x0 linker stubs + +.v4_bx 0x000003ac 0x0 + .v4_bx 0x00000000 0x0 linker stubs + +.rodata 0x000003ac 0x8 + *(.rodata) + *all.rodata*(*) + *(.roda) + *(.rodata.*) + .rodata.str1.1 + 0x000003ac 0x8 stub.o + *(.gnu.linkonce.r*) + 0x000003b4 . = ALIGN (0x4) + +.data 0x000003b4 0x0 + *(.data) + *(.data.*) + *(.gnu.linkonce.d*) + 0x000003b4 . = ALIGN (0x4) + +.bss 0x000003b4 0xc + 0x000003b4 __bss_start = . + *(.dynbss) + *(.gnu.linkonce.b*) + *(.bss*) + *(.sbss*) + *(COMMON) + 0x000003c0 . = ALIGN (0x20) + *fill* 0x000003b4 0xc 00 + 0x000003c0 __bss_end = . + 0x000003c0 __stack_end = __bss_end + 0x000004c0 __stack_addr = (__bss_end + 0x100) + 0x000004c0 __end = __stack_addr + 0x000004b0 __loader_size = (__end - __code_start) + [0x000003c0] PROVIDE (__stack_end, __stack_end) + [0x000004c0] PROVIDE (__stack_addr, __stack_addr) + [0x000000d8] PROVIDE (__got_start, __got_start) + [0x000000d8] PROVIDE (__got_end, __got_end) + [0x000003b4] PROVIDE (__bss_start, __bss_start) + [0x000003c0] PROVIDE (__bss_end, __bss_end) +LOAD start.o +LOAD stub.o +LOAD string.o +LOAD utils.o +LOAD e:/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.6.1/be\libgcc.a +OUTPUT(elfloader.elf elf32-bigarm) + +.ARM.attributes + 0x00000000 0x30 + .ARM.attributes + 0x00000000 0x24 start.o + .ARM.attributes + 0x00000024 0x34 stub.o + .ARM.attributes + 0x00000058 0x34 string.o + .ARM.attributes + 0x0000008c 0x34 utils.o + .ARM.attributes + 0x000000c0 0x1a e:/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.6.1/be\libgcc.a(_udivsi3.o) + .ARM.attributes + 0x000000da 0x18 e:/devkitpro/devkitarm/bin/../lib/gcc/arm-eabi/4.6.1/be\libgcc.a(_dvmd_tls.o) + +.comment 0x00000000 0x22 + .comment 0x00000000 0x22 stub.o + 0x23 (size before relaxing) + .comment 0x00000000 0x23 string.o + .comment 0x00000000 0x23 utils.o diff --git a/elfloader/hollywood.h b/elfloader/hollywood.h new file mode 100644 index 0000000..ce8e908 --- /dev/null +++ b/elfloader/hollywood.h @@ -0,0 +1,31 @@ +/* + mini - a Free Software replacement for the Nintendo/BroadOn IOS. + + ELF loader: Hollywood register definitions + +Copyright (C) 2008, 2009 Hector Martin "marcan" + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ +#ifndef __HOLLYWOOD_H__ +#define __HOLLYWOOD_H__ + +#define HW_REG_BASE 0xd800000 +#define HW_TIMER (HW_REG_BASE + 0x010) +#define HW_MEMMIRR (HW_REG_BASE + 0x060) +#define HW_BOOT0 (HW_REG_BASE + 0x18c) + +#endif + diff --git a/elfloader/starlet.mk b/elfloader/starlet.mk new file mode 100644 index 0000000..47051da --- /dev/null +++ b/elfloader/starlet.mk @@ -0,0 +1,12 @@ +ifeq ($(strip $(DEVKITARM)),) +$(error "Set DEVKITARM in your environment.") +endif + +PREFIX = $(DEVKITARM)/bin/arm-eabi- + +CFLAGS = -mbig-endian -mcpu=arm926ej-s +CFLAGS += -fomit-frame-pointer -ffunction-sections +CFLAGS += -Wall -Wextra -Os -pipe +ASFLAGS = +LDFLAGS = -mbig-endian -n -nostartfiles -nodefaultlibs -Wl,-gc-sections + diff --git a/elfloader/start.S b/elfloader/start.S new file mode 100644 index 0000000..8b9a7d6 --- /dev/null +++ b/elfloader/start.S @@ -0,0 +1,113 @@ +/* + mini - a Free Software replacement for the Nintendo/BroadOn IOS. + + ELF loader: system startup + +Copyright (C) 2008, 2009 Hector Martin "marcan" + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ +.arm + +.extern _main +.extern __got_start +.extern __got_end +.extern __bss_start +.extern __bss_end +.extern __stack_addr +.extern delay +.globl _start +.globl debug_output + +.section .init + +_start: + @ Get real address of _start + sub r4, pc, #8 + @ Subtract offset to get the address that we were loaded at + ldr r0, =_start + sub r4, r4, r0 + @ Output 0x42 to the debug port + mov r0, #0x42 + bl debug_output + + @ Set up a stack + ldr sp, =__stack_addr + add sp, r4 + + @ Output 0x43 to the debug port + mov r0, #0x43 + bl debug_output + + @ relocate the GOT entries + ldr r1, =__got_start + add r1, r4 + ldr r2, =__got_end + add r2, r4 +got_loop: + @ check for the end + cmp r1, r2 + beq done_got + @ read the GOT entry + ldr r3, [r1] + @ add our base address + add r3, r4 + str r3, [r1] + @ move on + add r1, r1, #4 + b got_loop + +done_got: + @ clear BSS + ldr r1, =__bss_start + add r1, r4 + ldr r2, =__bss_end + add r2, r4 + mov r3, #0 +bss_loop: + @ check for the end + cmp r1, r2 + beq done_bss + @ clear the word and move on + str r3, [r1] + add r1, r1, #4 + b bss_loop + +done_bss: + mov r0, #0x44 + bl debug_output + @ take the plunge + mov r0, r4 + bl _main + @ _main returned! Go to whatever address it returned... + mov r1, r0 + mov r0, r4 + mov pc, r1 + +.pool + +debug_output: + @ load address of port + mov r3, #0xd800000 + @ load old value + ldr r2, [r3, #0xe0] + @ clear debug byte + bic r2, r2, #0xFF0000 + @ insert new value + and r0, r0, #0xFF + orr r2, r2, r0, LSL #16 + @ store back + str r2, [r3, #0xe0] + mov pc, lr diff --git a/elfloader/start.h b/elfloader/start.h new file mode 100644 index 0000000..a3458b1 --- /dev/null +++ b/elfloader/start.h @@ -0,0 +1,30 @@ +/* + mini - a Free Software replacement for the Nintendo/BroadOn IOS. + + ELF loader: system startup + +Copyright (C) 2008, 2009 Hector Martin "marcan" + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ +#ifndef __START_H__ +#define __START_H__ + +#include "types.h" + +void debug_output(u8 byte); + +#endif + diff --git a/elfloader/start.o b/elfloader/start.o new file mode 100644 index 0000000..c19df0b Binary files /dev/null and b/elfloader/start.o differ diff --git a/elfloader/string.c b/elfloader/string.c new file mode 100644 index 0000000..483743f --- /dev/null +++ b/elfloader/string.c @@ -0,0 +1 @@ +#include "../string.c" \ No newline at end of file diff --git a/elfloader/string.h b/elfloader/string.h new file mode 100644 index 0000000..0bf3b83 --- /dev/null +++ b/elfloader/string.h @@ -0,0 +1 @@ +#include "../string.h" \ No newline at end of file diff --git a/elfloader/string.o b/elfloader/string.o new file mode 100644 index 0000000..b6c7d60 Binary files /dev/null and b/elfloader/string.o differ diff --git a/elfloader/stub.c b/elfloader/stub.c new file mode 100644 index 0000000..4a849d8 --- /dev/null +++ b/elfloader/stub.c @@ -0,0 +1,86 @@ +/* + mini - a Free Software replacement for the Nintendo/BroadOn IOS. + + ELF loader + +Copyright (C) 2008, 2009 Hector Martin "marcan" + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ +#include "types.h" +#include "utils.h" +#include "start.h" +#include "hollywood.h" +#include "string.h" +#include "elf.h" + +typedef struct { + u32 hdrsize; + u32 loadersize; + u32 elfsize; + u32 argument; +} ioshdr; + +void *loadelf(const u8 *elf) { + if(memcmp("\x7F" "ELF\x01\x02\x01",elf,7)) { + panic(0xE3); + } + + Elf32_Ehdr *ehdr = (Elf32_Ehdr*)elf; + if(ehdr->e_phoff == 0) { + panic(0xE4); + } + int count = ehdr->e_phnum; + Elf32_Phdr *phdr = (Elf32_Phdr*)(elf + ehdr->e_phoff); + while(count--) + { + if(phdr->p_type == PT_LOAD) { + const void *src = elf + phdr->p_offset; + memcpy(phdr->p_paddr, src, phdr->p_filesz); + } + phdr++; + } + return ehdr->e_entry; +} + +static inline void disable_boot0() +{ + set32(HW_BOOT0, 0x1000); +} + +static inline void mem_setswap() +{ + set32(HW_MEMMIRR, 0x20); +} + +void *_main(void *base) +{ + ioshdr *hdr = (ioshdr*)base; + u8 *elf; + void *entry; + + elf = (u8*) base; + elf += hdr->hdrsize + hdr->loadersize; + + debug_output(0xF1); + mem_setswap(1); + disable_boot0(1); + + entry = loadelf(elf); + debug_output(0xC1); + return entry; + +} + diff --git a/elfloader/stub.ld b/elfloader/stub.ld new file mode 100644 index 0000000..11d8b4f --- /dev/null +++ b/elfloader/stub.ld @@ -0,0 +1,117 @@ +/* + elfloader - a Free Software replacement for the Nintendo/BroadOn IOS. + +Copyright (C) 2008, 2009 Hector Martin "marcan" + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ +OUTPUT_FORMAT("elf32-bigarm") +OUTPUT_ARCH(arm) +EXTERN(_start) +ENTRY(_start) + +__base_addr = 0; + +SECTIONS +{ + . = __base_addr; + .header : + { + __header = .; + /* Entry point (offset) */ + LONG(__code_start); + /* Loader size */ + LONG(__loader_size); + /* ELF size */ + LONG(0); + /* Boot argument? */ + LONG(0); + . = ALIGN(16); + } + + __code_start = .; + + .init : + { + *(.init) + . = ALIGN(4); + } + + .got : + { + __got_start = .; + *(.got.*) + *(.got) + . = ALIGN(4); + __got_end = . ; + } + + .text : + { + *(.text.*) + *(.gnu.warning) + *(.gnu.linkonce.t*) + *(.glue_7) + *(.glue_7t) + . = ALIGN(4); + } + + __text_end = . ; + + .rodata : + { + *(.rodata) + *all.rodata*(*) + *(.roda) + *(.rodata.*) + *(.gnu.linkonce.r*) + . = ALIGN(4); + } + + .data : + { + *(.data) + *(.data.*) + *(.gnu.linkonce.d*) + . = ALIGN(4); + } + + .bss : + { + __bss_start = . ; + *(.dynbss) + *(.gnu.linkonce.b*) + *(.bss*) + *(.sbss*) + *(COMMON) + . = ALIGN(32); + __bss_end = . ; + } + +} + +__stack_end = (__bss_end); +__stack_addr = (__bss_end + 0x100); + +__end = __stack_addr ; +__loader_size = __end - __code_start; + +PROVIDE (__stack_end = __stack_end); +PROVIDE (__stack_addr = __stack_addr); +PROVIDE (__got_start = __got_start); +PROVIDE (__got_end = __got_end); +PROVIDE (__bss_start = __bss_start); +PROVIDE (__bss_end = __bss_end); + diff --git a/elfloader/stub.o b/elfloader/stub.o new file mode 100644 index 0000000..7b7bc9f Binary files /dev/null and b/elfloader/stub.o differ diff --git a/elfloader/types.h b/elfloader/types.h new file mode 100644 index 0000000..837d854 --- /dev/null +++ b/elfloader/types.h @@ -0,0 +1,50 @@ +/* + mini - a Free Software replacement for the Nintendo/BroadOn IOS. + + ELF loader: types + +Copyright (C) 2008, 2009 Hector Martin "marcan" + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ +#ifndef __TYPES_H__ +#define __TYPES_H__ + +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; +typedef unsigned long long u64; + +typedef signed char s8; +typedef signed short s16; +typedef signed int s32; +typedef signed long long s64; + +typedef volatile unsigned char vu8; +typedef volatile unsigned short vu16; +typedef volatile unsigned int vu32; +typedef volatile unsigned long long vu64; + +typedef volatile signed char vs8; +typedef volatile signed short vs16; +typedef volatile signed int vs32; +typedef volatile signed long long vs64; + +typedef s32 size_t; + +#define NULL ((void *)0) + +#endif + diff --git a/elfloader/utils.c b/elfloader/utils.c new file mode 100644 index 0000000..df3426b --- /dev/null +++ b/elfloader/utils.c @@ -0,0 +1,45 @@ +/* + mini - a Free Software replacement for the Nintendo/BroadOn IOS. + + ELF loader: random utilities + +Copyright (C) 2008, 2009 Hector Martin "marcan" + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ +#include "types.h" +#include "utils.h" +#include "hollywood.h" +#include "start.h" + +void udelay(u32 d) +{ + // should be good to max .2% error + u32 ticks = d * 19 / 10; + + write32(HW_TIMER, 0); + while(read32(HW_TIMER) < ticks); +} + +void panic(u8 v) +{ + while(1) { + debug_output(v); + udelay(500000); + debug_output(0); + udelay(500000); + } +} + diff --git a/elfloader/utils.h b/elfloader/utils.h new file mode 100644 index 0000000..698c95b --- /dev/null +++ b/elfloader/utils.h @@ -0,0 +1,188 @@ +/* + mini - a Free Software replacement for the Nintendo/BroadOn IOS. + + ELF loader: random utilities + +Copyright (C) 2008, 2009 Hector Martin "marcan" + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +*/ +#ifndef __UTILS_H__ +#define __UTILS_H__ + +static inline u32 read32(u32 addr) +{ + u32 data; + __asm__ volatile ("ldr\t%0, [%1]" : "=r" (data) : "r" (addr)); + return data; +} + +static inline void write32(u32 addr, u32 data) +{ + __asm__ volatile ("str\t%0, [%1]" : : "r" (data), "r" (addr)); +} + +static inline u32 set32(u32 addr, u32 set) +{ + u32 data; + __asm__ volatile ( + "ldr\t%0, [%1]\n" + "\torr\t%0, %2\n" + "\tstr\t%0, [%1]" + : "=&r" (data) + : "r" (addr), "r" (set) + ); + return data; +} + +static inline u32 clear32(u32 addr, u32 clear) +{ + u32 data; + __asm__ volatile ( + "ldr\t%0, [%1]\n" + "\tbic\t%0, %2\n" + "\tstr\t%0, [%1]" + : "=&r" (data) + : "r" (addr), "r" (clear) + ); + return data; +} + + +static inline u32 mask32(u32 addr, u32 clear, u32 set) +{ + u32 data; + __asm__ volatile ( + "ldr\t%0, [%1]\n" + "\tbic\t%0, %3\n" + "\torr\t%0, %2\n" + "\tstr\t%0, [%1]" + : "=&r" (data) + : "r" (addr), "r" (set), "r" (clear) + ); + return data; +} + +static inline u16 read16(u32 addr) +{ + u32 data; + __asm__ volatile ("ldrh\t%0, [%1]" : "=r" (data) : "r" (addr)); + return data; +} + +static inline void write16(u32 addr, u16 data) +{ + __asm__ volatile ("strh\t%0, [%1]" : : "r" (data), "r" (addr)); +} + +static inline u16 set16(u32 addr, u16 set) +{ + u16 data; + __asm__ volatile ( + "ldrh\t%0, [%1]\n" + "\torr\t%0, %2\n" + "\tstrh\t%0, [%1]" + : "=&r" (data) + : "r" (addr), "r" (set) + ); + return data; +} + +static inline u16 clear16(u32 addr, u16 clear) +{ + u16 data; + __asm__ volatile ( + "ldrh\t%0, [%1]\n" + "\tbic\t%0, %2\n" + "\tstrh\t%0, [%1]" + : "=&r" (data) + : "r" (addr), "r" (clear) + ); + return data; +} + + +static inline u16 mask16(u32 addr, u16 clear, u16 set) +{ + u16 data; + __asm__ volatile ( + "ldrh\t%0, [%1]\n" + "\tbic\t%0, %3\n" + "\torr\t%0, %2\n" + "\tstrh\t%0, [%1]" + : "=&r" (data) + : "r" (addr), "r" (set), "r" (clear) + ); + return data; +} + +static inline u8 read8(u32 addr) +{ + u32 data; + __asm__ volatile ("ldrb\t%0, [%1]" : "=r" (data) : "r" (addr)); + return data; +} + +static inline void write8(u32 addr, u8 data) +{ + __asm__ volatile ("strb\t%0, [%1]" : : "r" (data), "r" (addr)); +} + +static inline u8 set8(u32 addr, u8 set) +{ + u8 data; + __asm__ volatile ( + "ldrb\t%0, [%1]\n" + "\torr\t%0, %2\n" + "\tstrb\t%0, [%1]" + : "=&r" (data) + : "r" (addr), "r" (set) + ); + return data; +} + +static inline u8 clear8(u32 addr, u8 clear) +{ + u8 data; + __asm__ volatile ( + "ldrb\t%0, [%1]\n" + "\tbic\t%0, %2\n" + "\tstrb\t%0, [%1]" + : "=&r" (data) + : "r" (addr), "r" (clear) + ); + return data; +} + + +static inline u8 mask8(u32 addr, u8 clear, u8 set) +{ + u8 data; + __asm__ volatile ( + "ldrb\t%0, [%1]\n" + "\tbic\t%0, %3\n" + "\torr\t%0, %2\n" + "\tstrb\t%0, [%1]" + : "=&r" (data) + : "r" (addr), "r" (set), "r" (clear) + ); + return data; +} + +void udelay(u32 d); +void panic(u8 v); + +#endif + diff --git a/elfloader/utils.o b/elfloader/utils.o new file mode 100644 index 0000000..f93734e Binary files /dev/null and b/elfloader/utils.o differ diff --git a/tools/bin2h/bin2h.exe b/tools/bin2h/bin2h.exe new file mode 100644 index 0000000..bbd62bb Binary files /dev/null and b/tools/bin2h/bin2h.exe differ diff --git a/tools/bin2h/main.cpp b/tools/bin2h/main.cpp new file mode 100644 index 0000000..61216ef --- /dev/null +++ b/tools/bin2h/main.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include +#include + +int main( int argc, char *argv[] ) +{ + if( argc != 2 ) + { + return 0; + } + + FILE *in = fopen( argv[1], "rb" ); + if( in == NULL ) + { + printf("couldn't open:\"%s\"\n", argv[1] ); + return -1; + } + + fseek( in, 0, SEEK_END ); + unsigned int size = ftell(in); + fseek( in, 0, 0 ); + + int point=0; + int slash=0; + + for( int i = strlen(argv[1]) - 1; i > 0; --i ) + { + if( argv[1][i] == '.' && point == 0 ) + point = strlen(argv[1]) - i; + + if( argv[1][i] == '\\' || argv[1][i] == '/' ) + { + slash = i + 1; + break; + } + } + + char * str = new char[ strlen(argv[1]) - slash - point + 3 ]; // '.' 'h' '\0' + + memcpy( str, argv[1]+slash, strlen(argv[1]) - slash - point ); + + str[ strlen(argv[1]) - slash - point + 0 ] = '.'; + str[ strlen(argv[1]) - slash - point + 1 ] = 'h'; + str[ strlen(argv[1]) - slash - point + 2 ] = '\0'; + + printf("Using:\"%s\" for name\n", str ); + + FILE *out = fopen( str, "wb" ); + + str[ strlen(argv[1]) - slash - point ] = '\0'; + + fprintf( out, "/*\n\tFilename : %s\n", argv[1] ); + + time_t t; + time(&t); + + fprintf( out, "\tDate created: %s*/\n\n", ctime (&t) ); + + fprintf( out, "#define %s_size 0x%x\n\n", str, size ); + + fprintf( out, "unsigned char %s[] = {\n\n\t", str ); + + for( int i=0; i < size; ++i ) + { + fprintf( out, "0x%02X", fgetc(in) ); + + if( i+1 != size ) + fprintf( out, ", " ); + + if( ((i+1) & 0xF) == 0 ) + fprintf( out, "\n" ); + + if( ((i+1) & 0x3) == 0 ) + fprintf( out, "\t" ); + } + + fprintf( out, "\n};\n" ); + + fclose( out ); + free( str ); + + return 0; +} \ No newline at end of file