From f4b28eb49ed432c7e34ec3f891cface685968ff7 Mon Sep 17 00:00:00 2001 From: Sven Peter Date: Wed, 9 Apr 2008 18:57:53 +0200 Subject: [PATCH] tcploader addded --- tcploader/README | 16 ++ tcploader/pc/Makefile | 35 +++ tcploader/pc/main.c | 151 ++++++++++++ tcploader/wii/Makefile | 48 ++++ tcploader/wii/asm.h | 340 ++++++++++++++++++++++++++ tcploader/wii/dol.c | 77 ++++++ tcploader/wii/dol.h | 29 +++ tcploader/wii/elf.c | 103 ++++++++ tcploader/wii/elf.h | 9 + tcploader/wii/elf_abi.h | 594 ++++++++++++++++++++++++++++++++++++++++++++++ tcploader/wii/main.c | 261 ++++++++++++++++++++ tcploader/wii/preloader.c | 61 +++++ tcploader/wii/processor.h | 115 +++++++++ 13 files changed, 1839 insertions(+) create mode 100644 tcploader/README create mode 100644 tcploader/pc/Makefile create mode 100644 tcploader/pc/main.c create mode 100644 tcploader/wii/Makefile create mode 100644 tcploader/wii/asm.h create mode 100644 tcploader/wii/dol.c create mode 100644 tcploader/wii/dol.h create mode 100644 tcploader/wii/elf.c create mode 100644 tcploader/wii/elf.h create mode 100644 tcploader/wii/elf_abi.h create mode 100644 tcploader/wii/main.c create mode 100644 tcploader/wii/preloader.c create mode 100644 tcploader/wii/processor.h diff --git a/tcploader/README b/tcploader/README new file mode 100644 index 0000000..88494f3 --- /dev/null +++ b/tcploader/README @@ -0,0 +1,16 @@ +TCP Elf Loader v0.1 +(c) 2008 Sven Peter, svpe in #wiidev at efnet/blitzed or svpe [at] gmx (dot) net + +based on bushing's title_lister.c +(c) 2008 bushing + +Launch the included elf or dol using your favourite method of launching homebrew one +your console. It will then start listening on port 8080 and wait for a connection of +the client. + +The win32 version of the client doesn't work currently. You'll have to use linux to test this. + +wii/ contains the wii code +pc/ contains a small client to send your file + +Type 'make' to build it yourself and don't bother me when it doesn't build for you... diff --git a/tcploader/pc/Makefile b/tcploader/pc/Makefile new file mode 100644 index 0000000..f47fa2b --- /dev/null +++ b/tcploader/pc/Makefile @@ -0,0 +1,35 @@ +.PHONY = all clean win32 linux + +OUTPUT = sendelf +CC = gcc + +OBJS = main.o +CFLAGS = -Wall -pedantic -g + +WIN_OUTPUT = sendelf.exe +WIN_CC = i686-mingw32-gcc + +WIN_OBJS = main.wo +WIN_CFLAGS = -Wall -pedantic -g +WIN_LDFLAGS = -lwsock32 + +all: win32 linux + +win32: $(WIN_OUTPUT) + +linux: $(OUTPUT) + +clean: + rm -f $(OBJS) $(WIN_OBJS) $(OUTPUT) $(WIN_OUTPUT) + +$(OUTPUT): $(OBJS) + gcc -o $@ $< + +$(WIN_OUTPUT): $(WIN_OBJS) + $(WIN_CC) -o $@ $< $(WIN_LDFLAGS) + +%.wo: %.c + $(WIN_CC) $(WIN_CFLAGS) -c -o $@ $< + +%.o: %.c + gcc $(CFLAGS) -c -o $@ $< diff --git a/tcploader/pc/main.c b/tcploader/pc/main.c new file mode 100644 index 0000000..f10e813 --- /dev/null +++ b/tcploader/pc/main.c @@ -0,0 +1,151 @@ +#include +#include +#include + +#ifdef __WIN32__ +#include +#include +#define close(s) closesocket(s) +#else +#include +#include +#include +#include +#include +#include +#endif + +#define BUFFER_SIZE 1 << 10 +#define PORT 8080 + +int write_data(int s, char *buf, int n) +{ + int bcount; + int br; + bcount = 0; + br = 0; + while(bcount < n) { + if ((br = send(s,buf,n-bcount,0)) > 0) { + bcount += br; + buf += br; + } + else if (br < 0) + return br; + } + return (bcount); +} + +int main(int argc, char *argv[]) +{ + FILE *fd; + char *buffer; + int i; + int size, size2; + int sock; + struct sockaddr_in addr; +#ifdef __WIN32__ + WSADATA wsa; +#endif + + if(argc != 3) + { + printf("usage: sendelf [wii ip] [file.elf]\n"); + return -1; + } + +#ifdef __WIN32__ + if (WSAStartup(MAKEWORD(1, 1), &wsa)) + { + printf("WSAStartup() failed, %lu\n", (unsigned long)GetLastError()); + return EXIT_FAILURE; + } +#endif + + fd = fopen(argv[2], "rb"); + if(fd == NULL) + { + perror("fopen failed"); + return -1; + } + +#ifdef __WIN32__ + addr.sin_addr.S_un.S_addr = inet_addr(argv[1]); + if(addr.sin_addr.S_un.S_addr == INADDR_NONE) +#else + if(inet_aton(argv[1], &addr.sin_addr) == 0) +#endif + { + perror("inet_aton failed"); + fclose(fd); + return -1; + } + + sock = socket(PF_INET, SOCK_STREAM, 0); + if(sock < 0) + { + perror("socket() failed"); + fclose(fd); + return -1; + } + + addr.sin_port = htons(PORT); + addr.sin_family = AF_INET; + + printf("connecting to %s:%d\n", inet_ntoa(addr.sin_addr), PORT); + if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) + { + perror("connect() failed"); + fclose(fd); + close(sock); + return -1; + } + + fseek(fd, 0, SEEK_END); + size = ftell(fd); + fseek(fd, 0, SEEK_SET); + + printf("size: %08X\n", size); + buffer = malloc(BUFFER_SIZE); + size2 = ((size & 0xFF000000) >> 24) | + ((size & 0x00FF0000) >> 8) | + ((size & 0x0000FF00) << 8) | + ((size & 0x000000FF) << 24); + printf("size2: %08X\n", size2); + memcpy(buffer, &size2, sizeof(size)); + if((i = write_data(sock, buffer, sizeof(size))) < 0) + { + perror("send() failed"); + fclose(fd); + close(sock); + return -1; + } + + i = 0; + while(i < size) + { + memset(buffer, 0, BUFFER_SIZE); + printf("reading chunk #%d\n", i); + if(fread(buffer, size > BUFFER_SIZE ? BUFFER_SIZE : size, 1, fd) < 1) + { + perror("fread returned < 1"); +/* fclose(fd); + close(sock); + return -1;*/ + } + printf("%d/%d\n", i, size); + if(write_data(sock, buffer, BUFFER_SIZE) < 0) + { + perror("send() failed"); + fclose(fd); + close(sock); + return -1; + } + i += BUFFER_SIZE; + } + + printf("file sent successfully(?)\n"); + fclose(fd); + close(sock); + + return 0; +} diff --git a/tcploader/wii/Makefile b/tcploader/wii/Makefile new file mode 100644 index 0000000..28b66a0 --- /dev/null +++ b/tcploader/wii/Makefile @@ -0,0 +1,48 @@ +ifeq ($(strip $(DEVKITPPC)),) +$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=devkitPPC) +endif + +include $(DEVKITPPC)/wii_rules + +.PHONY = all + +DEPSDIR = . +CFLAGS = -g -Os -mrvl -Wall $(MACHDEP) $(INCLUDE) -I$(LIBOGC_INC) + +LDFLAGS = -L$(LIBOGC_LIB) -logc -g $(MACHDEP) -mrvl -Wl,-Map,$(notdir $@).map,--section-start,.header=0x80a00000 + +LOADER_OBJS = main.o elf.o dol.o +LOADER_DEPENDS = $(LOADER_OBJS:.o=.d) + +TCPLOAD_OBJS = loader.dol.o preloader.o dol.o +TCPLOAD_LDFLAGS = -L$(LIBOGC_LIB) -logc -g $(MACHDEP) -mrvl -Wl,-Map,$(notdir $@).map +TCPLOAD_DEPENDS := $(TCPLOAD_OBJS:.o=.d) + +export LD := $(CC) + + +all: tcpload.dol + +clean: + rm -f *.o *.d *.map *.s *.dol *.elf + +tcpload.dol: tcpload.elf + +loader.dol.o: loader.dol +loader.dol: loader.elf +loader.elf: $(LOADER_OBJS) + +# FIXME: these aren't used in the first build.. +-include $(LOADER_DEPENDS) +-include $(TCPLOAD_DEPENDS) + +%.o: %.c + $(CC) -MMD -MP -MF $*.d $(CFLAGS) -c $< -o $@ + +%.dol.o: %.dol + bin2s $< > $@.s + $(CC) -MMD -MP $(CFLAGS) -c $@.s -o $@ + +tcpload.elf: $(TCPLOAD_OBJS) + $(LD) $^ $(TCPLOAD_LDFLAGS) $(LIBPATHS) $(LIBS) -o $@ + diff --git a/tcploader/wii/asm.h b/tcploader/wii/asm.h new file mode 100644 index 0000000..8e335b4 --- /dev/null +++ b/tcploader/wii/asm.h @@ -0,0 +1,340 @@ +// this file was taken from libogc, see http://www.devkitpro.org/ + +#ifndef __ASM_H__ +#define __ASM_H__ + +#ifdef _LANGUAGE_ASSEMBLY +/* Condition Register Bit Fields */ + +#define cr0 0 +#define cr1 1 +#define cr2 2 +#define cr3 3 +#define cr4 4 +#define cr5 5 +#define cr6 6 +#define cr7 7 + + +/* General Purpose Registers (GPRs) */ + +#define r0 0 +#define r1 1 +#define sp 1 +#define r2 2 +#define toc 2 +#define r3 3 +#define r4 4 +#define r5 5 +#define r6 6 +#define r7 7 +#define r8 8 +#define r9 9 +#define r10 10 +#define r11 11 +#define r12 12 +#define r13 13 +#define r14 14 +#define r15 15 +#define r16 16 +#define r17 17 +#define r18 18 +#define r19 19 +#define r20 20 +#define r21 21 +#define r22 22 +#define r23 23 +#define r24 24 +#define r25 25 +#define r26 26 +#define r27 27 +#define r28 28 +#define r29 29 +#define r30 30 +#define r31 31 + + +/* Floating Point Registers (FPRs) */ + +#define fr0 0 +#define fr1 1 +#define fr2 2 +#define fr3 3 +#define fr4 4 +#define fr5 5 +#define fr6 6 +#define fr7 7 +#define fr8 8 +#define fr9 9 +#define fr10 10 +#define fr11 11 +#define fr12 12 +#define fr13 13 +#define fr14 14 +#define fr15 15 +#define fr16 16 +#define fr17 17 +#define fr18 18 +#define fr19 19 +#define fr20 20 +#define fr21 21 +#define fr22 22 +#define fr23 23 +#define fr24 24 +#define fr25 25 +#define fr26 26 +#define fr27 27 +#define fr28 28 +#define fr29 29 +#define fr30 30 +#define fr31 31 + +#define vr0 0 +#define vr1 1 +#define vr2 2 +#define vr3 3 +#define vr4 4 +#define vr5 5 +#define vr6 6 +#define vr7 7 +#define vr8 8 +#define vr9 9 +#define vr10 10 +#define vr11 11 +#define vr12 12 +#define vr13 13 +#define vr14 14 +#define vr15 15 +#define vr16 16 +#define vr17 17 +#define vr18 18 +#define vr19 19 +#define vr20 20 +#define vr21 21 +#define vr22 22 +#define vr23 23 +#define vr24 24 +#define vr25 25 +#define vr26 26 +#define vr27 27 +#define vr28 28 +#define vr29 29 +#define vr30 30 +#define vr31 31 + +#endif //_LANGUAGE_ASSEMBLY + +#define SPRG0 272 +#define SPRG1 273 +#define SPRG2 274 +#define SPRG3 275 + +#define PMC1 953 +#define PMC2 954 +#define PMC3 957 +#define PMC4 958 + +#define MMCR0 952 +#define MMCR1 956 + + +#define LINK_REGISTER_CALLEE_UPDATE_ROOM 4 +#define EXCEPTION_NUMBER 8 +#define SRR0_OFFSET 12 +#define SRR1_OFFSET 16 +#define GPR0_OFFSET 20 +#define GPR1_OFFSET 24 +#define GPR2_OFFSET 28 +#define GPR3_OFFSET 32 +#define GPR4_OFFSET 36 +#define GPR5_OFFSET 40 +#define GPR6_OFFSET 44 +#define GPR7_OFFSET 48 +#define GPR8_OFFSET 52 +#define GPR9_OFFSET 56 +#define GPR10_OFFSET 60 +#define GPR11_OFFSET 64 +#define GPR12_OFFSET 68 +#define GPR13_OFFSET 72 +#define GPR14_OFFSET 76 +#define GPR15_OFFSET 80 +#define GPR16_OFFSET 84 +#define GPR17_OFFSET 88 +#define GPR18_OFFSET 92 +#define GPR19_OFFSET 96 +#define GPR20_OFFSET 100 +#define GPR21_OFFSET 104 +#define GPR22_OFFSET 108 +#define GPR23_OFFSET 112 +#define GPR24_OFFSET 116 +#define GPR25_OFFSET 120 +#define GPR26_OFFSET 124 +#define GPR27_OFFSET 128 +#define GPR28_OFFSET 132 +#define GPR29_OFFSET 136 +#define GPR30_OFFSET 140 +#define GPR31_OFFSET 144 + +#define GQR0_OFFSET 148 +#define GQR1_OFFSET 152 +#define GQR2_OFFSET 156 +#define GQR3_OFFSET 160 +#define GQR4_OFFSET 164 +#define GQR5_OFFSET 168 +#define GQR6_OFFSET 172 +#define GQR7_OFFSET 176 + +#define CR_OFFSET 180 +#define LR_OFFSET 184 +#define CTR_OFFSET 188 +#define XER_OFFSET 192 +#define MSR_OFFSET 196 +#define DAR_OFFSET 200 + +#define STATE_OFFSET 204 +#define MODE_OFFSET 206 + +#define FPR0_OFFSET 208 +#define FPR1_OFFSET 216 +#define FPR2_OFFSET 224 +#define FPR3_OFFSET 232 +#define FPR4_OFFSET 240 +#define FPR5_OFFSET 248 +#define FPR6_OFFSET 256 +#define FPR7_OFFSET 264 +#define FPR8_OFFSET 272 +#define FPR9_OFFSET 280 +#define FPR10_OFFSET 288 +#define FPR11_OFFSET 296 +#define FPR12_OFFSET 304 +#define FPR13_OFFSET 312 +#define FPR14_OFFSET 320 +#define FPR15_OFFSET 328 +#define FPR16_OFFSET 336 +#define FPR17_OFFSET 344 +#define FPR18_OFFSET 352 +#define FPR19_OFFSET 360 +#define FPR20_OFFSET 368 +#define FPR21_OFFSET 376 +#define FPR22_OFFSET 384 +#define FPR23_OFFSET 392 +#define FPR24_OFFSET 400 +#define FPR25_OFFSET 408 +#define FPR26_OFFSET 416 +#define FPR27_OFFSET 424 +#define FPR28_OFFSET 432 +#define FPR29_OFFSET 440 +#define FPR30_OFFSET 448 +#define FPR31_OFFSET 456 + +#define FPSCR_OFFSET 464 + +#define PSR0_OFFSET 472 +#define PSR1_OFFSET 480 +#define PSR2_OFFSET 488 +#define PSR3_OFFSET 496 +#define PSR4_OFFSET 504 +#define PSR5_OFFSET 512 +#define PSR6_OFFSET 520 +#define PSR7_OFFSET 528 +#define PSR8_OFFSET 536 +#define PSR9_OFFSET 544 +#define PSR10_OFFSET 552 +#define PSR11_OFFSET 560 +#define PSR12_OFFSET 568 +#define PSR13_OFFSET 576 +#define PSR14_OFFSET 584 +#define PSR15_OFFSET 592 +#define PSR16_OFFSET 600 +#define PSR17_OFFSET 608 +#define PSR18_OFFSET 616 +#define PSR19_OFFSET 624 +#define PSR20_OFFSET 632 +#define PSR21_OFFSET 640 +#define PSR22_OFFSET 648 +#define PSR23_OFFSET 656 +#define PSR24_OFFSET 664 +#define PSR25_OFFSET 672 +#define PSR26_OFFSET 680 +#define PSR27_OFFSET 688 +#define PSR28_OFFSET 696 +#define PSR29_OFFSET 704 +#define PSR30_OFFSET 712 +#define PSR31_OFFSET 720 +/* + * maintain the EABI requested 8 bytes aligment + * As SVR4 ABI requires 16, make it 16 (as some + * exception may need more registers to be processed...) + */ +#define EXCEPTION_FRAME_END 728 + +#define IBAT0U 528 +#define IBAT0L 529 +#define IBAT1U 530 +#define IBAT1L 531 +#define IBAT2U 532 +#define IBAT2L 533 +#define IBAT3U 534 +#define IBAT3L 535 +#define IBAT4U 560 +#define IBAT4L 561 +#define IBAT5U 562 +#define IBAT5L 563 +#define IBAT6U 564 +#define IBAT6L 565 +#define IBAT7U 566 +#define IBAT7L 567 + +#define DBAT0U 536 +#define DBAT0L 537 +#define DBAT1U 538 +#define DBAT1L 538 +#define DBAT2U 540 +#define DBAT2L 541 +#define DBAT3U 542 +#define DBAT3L 543 +#define DBAT4U 568 +#define DBAT4L 569 +#define DBAT5U 570 +#define DBAT5L 571 +#define DBAT6U 572 +#define DBAT6L 573 +#define DBAT7U 574 +#define DBAT7L 575 + +#define HID0 1008 +#define HID1 1009 +#define HID2 920 +#define HID4 1011 + +#define GQR0 912 +#define GQR1 913 +#define GQR2 914 +#define GQR3 915 +#define GQR4 916 +#define GQR5 917 +#define GQR6 918 +#define GQR7 919 + +#define L2CR 1017 + +#define WPAR 921 + +#define DMAU 922 +#define DMAL 923 + +#define MSR_RI 0x00000002 +#define MSR_DR 0x00000010 +#define MSR_IR 0x00000020 +#define MSR_IP 0x00000040 +#define MSR_SE 0x00000400 +#define MSR_ME 0x00001000 +#define MSR_FP 0x00002000 +#define MSR_POW 0x00004000 +#define MSR_EE 0x00008000 + +#define PPC_ALIGNMENT 8 + +#define PPC_CACHE_ALIGNMENT 32 + +#endif //__ASM_H__ diff --git a/tcploader/wii/dol.c b/tcploader/wii/dol.c new file mode 100644 index 0000000..40fd6b9 --- /dev/null +++ b/tcploader/wii/dol.c @@ -0,0 +1,77 @@ +// this code was contributed by shagkur of the devkitpro team, thx! + +#include +#include + +#include +#include + +typedef struct _dolheader { + u32 text_pos[7]; + u32 data_pos[11]; + u32 text_start[7]; + u32 data_start[11]; + u32 text_size[7]; + u32 data_size[11]; + u32 bss_start; + u32 bss_size; + u32 entry_point; +} dolheader; + +u32 load_dol_image (void *dolstart, int print) { + u32 i; + dolheader *dolfile; + + if (dolstart) { + dolfile = (dolheader *) dolstart; + for (i = 0; i < 7; i++) { + if ((!dolfile->text_size[i]) || + (dolfile->text_start[i] < 0x100)) + continue; + + if(print) + printf ("loading text section %u @ 0x%08x " + "(0x%08x bytes)\n", + i, dolfile->text_start[i], + dolfile->text_size[i]); + VIDEO_WaitVSync(); + + ICInvalidateRange ((void *) dolfile->text_start[i], + dolfile->text_size[i]); + memcpy ((void *) dolfile->text_start[i], + dolstart+dolfile->text_pos[i], + dolfile->text_size[i]); + } + + for(i = 0; i < 11; i++) { + if ((!dolfile->data_size[i]) || + (dolfile->data_start[i] < 0x100)) + continue; + + if(print) + printf ("loading data section %u @ 0x%08x " + "(0x%08x bytes)\n", + i, dolfile->data_start[i], + dolfile->data_size[i]); + VIDEO_WaitVSync(); + + memcpy ((void*) dolfile->data_start[i], + dolstart+dolfile->data_pos[i], + dolfile->data_size[i]); + DCFlushRangeNoSync ((void *) dolfile->data_start[i], + dolfile->data_size[i]); + } + + if(print) + printf ("clearing bss\n"); + VIDEO_WaitVSync(); + + memset ((void *) dolfile->bss_start, 0, dolfile->bss_size); + DCFlushRange((void *) dolfile->bss_start, dolfile->bss_size); + + return dolfile->entry_point; + } + + return 0; +} + diff --git a/tcploader/wii/dol.h b/tcploader/wii/dol.h new file mode 100644 index 0000000..1a21b02 --- /dev/null +++ b/tcploader/wii/dol.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2008 dhewg, #wiidev efnet + * + * this file is part of geckoloader + * http://wiibrew.org/index.php?title=Geckoloader + * + * 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; either version 2 of the License, or + * (at your option) any later version. + * + * 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 _DOL_H_ + +#include + +u32 load_dol_image (void *dolstart, int printf); + +#endif + diff --git a/tcploader/wii/elf.c b/tcploader/wii/elf.c new file mode 100644 index 0000000..8d1e8d7 --- /dev/null +++ b/tcploader/wii/elf.c @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2001 William L. Pitts + * Modifications (c) 2004 Felix Domke + * All rights reserved. + * + * Redistribution and use in source and binary forms are freely + * permitted provided that the above copyright notice and this + * paragraph and the following disclaimer are duplicated in all + * such forms. + * + * This software is provided "AS IS" and without any express or + * implied warranties, including, without limitation, the implied + * warranties of merchantability and fitness for a particular + * purpose. + */ + +#include +#include + +#include + +#include "elf_abi.h" + +/* ====================================================================== + * Determine if a valid ELF image exists at the given memory location. + * First looks at the ELF header magic field, the makes sure that it is + * executable and makes sure that it is for a PowerPC. + * ====================================================================== */ +s32 valid_elf_image (void *addr) +{ + Elf32_Ehdr *ehdr; /* Elf header structure pointer */ + + ehdr = (Elf32_Ehdr *) addr; + + if (!IS_ELF (*ehdr)) + return 0; + + if (ehdr->e_type != ET_EXEC) + return -1; + + if (ehdr->e_machine != EM_PPC) + return -1; + + return 1; +} + + +/* ====================================================================== + * A very simple elf loader, assumes the image is valid, returns the + * entry point address. + * ====================================================================== */ +u32 load_elf_image (void *addr) +{ + Elf32_Ehdr *ehdr; + Elf32_Shdr *shdr; + u8 *strtab = 0; + u8 *image; + int i; + + ehdr = (Elf32_Ehdr *) addr; + /* Find the section header string table for output info */ + shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff + + (ehdr->e_shstrndx * sizeof (Elf32_Shdr))); + + if (shdr->sh_type == SHT_STRTAB) + strtab = (u8 *) (addr + shdr->sh_offset); + + /* Load each appropriate section */ + for (i = 0; i < ehdr->e_shnum; ++i) { + shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff + + (i * sizeof (Elf32_Shdr))); + + if (!(shdr->sh_flags & SHF_ALLOC) + || shdr->sh_addr == 0 || shdr->sh_size == 0) { + continue; + } + + shdr->sh_addr &= 0x3FFFFFFF; + shdr->sh_addr |= 0x80000000; + + if (strtab) { + printf ("%sing section %s @ 0x%08x (0x%08x bytes)\n", + (shdr->sh_type == SHT_NOBITS) ? + "clear" : "load", + &strtab[shdr->sh_name], + (u32) shdr->sh_addr, + (u32) shdr->sh_size); + } + + if (shdr->sh_type == SHT_NOBITS) { + memset ((void *) shdr->sh_addr, 0, shdr->sh_size); + } else { + image = (u8 *) addr + shdr->sh_offset; + memcpy ((void *) shdr->sh_addr, + (const void *) image, + shdr->sh_size); + } + DCFlushRangeNoSync ((void *) shdr->sh_addr, shdr->sh_size); + } + + return (ehdr->e_entry & 0x3FFFFFFF) | 0x80000000; +} + diff --git a/tcploader/wii/elf.h b/tcploader/wii/elf.h new file mode 100644 index 0000000..2bbb7bd --- /dev/null +++ b/tcploader/wii/elf.h @@ -0,0 +1,9 @@ +#ifndef _ELF_H_ +#define _ELF_H_ + +#include + +s32 valid_elf_image (void *addr); +u32 load_elf_image (void *addr); + +#endif diff --git a/tcploader/wii/elf_abi.h b/tcploader/wii/elf_abi.h new file mode 100644 index 0000000..c9e705e --- /dev/null +++ b/tcploader/wii/elf_abi.h @@ -0,0 +1,594 @@ +/* + * Copyright (c) 1995, 1996, 2001, 2002 + * Erik Theisen. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * This is the ELF ABI header file + * formerly known as "elf_abi.h". + */ + +#ifndef _ELF_ABI_H +#define _ELF_ABI_H + +#include + +/* + * This version doesn't work for 64-bit ABIs - Erik. + */ + +/* + * These typedefs need to be handled better. + */ +typedef u32 Elf32_Addr; /* Unsigned program address */ +typedef u32 Elf32_Off; /* Unsigned file offset */ +typedef s32 Elf32_Sword; /* Signed large integer */ +typedef u32 Elf32_Word; /* Unsigned large integer */ +typedef u16 Elf32_Half; /* Unsigned medium integer */ + +/* e_ident[] identification indexes */ +#define EI_MAG0 0 /* file ID */ +#define EI_MAG1 1 /* file ID */ +#define EI_MAG2 2 /* file ID */ +#define EI_MAG3 3 /* file ID */ +#define EI_CLASS 4 /* file class */ +#define EI_DATA 5 /* data encoding */ +#define EI_VERSION 6 /* ELF header version */ +#define EI_OSABI 7 /* OS/ABI specific ELF extensions */ +#define EI_ABIVERSION 8 /* ABI target version */ +#define EI_PAD 9 /* start of pad bytes */ +#define EI_NIDENT 16 /* Size of e_ident[] */ + +/* e_ident[] magic number */ +#define ELFMAG0 0x7f /* e_ident[EI_MAG0] */ +#define ELFMAG1 'E' /* e_ident[EI_MAG1] */ +#define ELFMAG2 'L' /* e_ident[EI_MAG2] */ +#define ELFMAG3 'F' /* e_ident[EI_MAG3] */ +#define ELFMAG "\177ELF" /* magic */ +#define SELFMAG 4 /* size of magic */ + +/* e_ident[] file class */ +#define ELFCLASSNONE 0 /* invalid */ +#define ELFCLASS32 1 /* 32-bit objs */ +#define ELFCLASS64 2 /* 64-bit objs */ +#define ELFCLASSNUM 3 /* number of classes */ + +/* e_ident[] data encoding */ +#define ELFDATANONE 0 /* invalid */ +#define ELFDATA2LSB 1 /* Little-Endian */ +#define ELFDATA2MSB 2 /* Big-Endian */ +#define ELFDATANUM 3 /* number of data encode defines */ + +/* e_ident[] OS/ABI specific ELF extensions */ +#define ELFOSABI_NONE 0 /* No extension specified */ +#define ELFOSABI_HPUX 1 /* Hewlett-Packard HP-UX */ +#define ELFOSABI_NETBSD 2 /* NetBSD */ +#define ELFOSABI_LINUX 3 /* Linux */ +#define ELFOSABI_SOLARIS 6 /* Sun Solaris */ +#define ELFOSABI_AIX 7 /* AIX */ +#define ELFOSABI_IRIX 8 /* IRIX */ +#define ELFOSABI_FREEBSD 9 /* FreeBSD */ +#define ELFOSABI_TRU64 10 /* Compaq TRU64 UNIX */ +#define ELFOSABI_MODESTO 11 /* Novell Modesto */ +#define ELFOSABI_OPENBSD 12 /* OpenBSD */ +/* 64-255 Architecture-specific value range */ + +/* e_ident[] ABI Version */ +#define ELFABIVERSION 0 + +/* e_ident */ +#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \ + (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \ + (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \ + (ehdr).e_ident[EI_MAG3] == ELFMAG3) + +/* ELF Header */ +typedef struct elfhdr{ + unsigned char e_ident[EI_NIDENT]; /* ELF Identification */ + Elf32_Half e_type; /* object file type */ + Elf32_Half e_machine; /* machine */ + Elf32_Word e_version; /* object file version */ + Elf32_Addr e_entry; /* virtual entry point */ + Elf32_Off e_phoff; /* program header table offset */ + Elf32_Off e_shoff; /* section header table offset */ + Elf32_Word e_flags; /* processor-specific flags */ + Elf32_Half e_ehsize; /* ELF header size */ + Elf32_Half e_phentsize; /* program header entry size */ + Elf32_Half e_phnum; /* number of program header entries */ + Elf32_Half e_shentsize; /* section header entry size */ + Elf32_Half e_shnum; /* number of section header entries */ + Elf32_Half e_shstrndx; /* section header table's "section + header string table" entry offset */ +} Elf32_Ehdr; + +/* e_type */ +#define ET_NONE 0 /* No file type */ +#define ET_REL 1 /* relocatable file */ +#define ET_EXEC 2 /* executable file */ +#define ET_DYN 3 /* shared object file */ +#define ET_CORE 4 /* core file */ +#define ET_NUM 5 /* number of types */ +#define ET_LOOS 0xfe00 /* reserved range for operating */ +#define ET_HIOS 0xfeff /* system specific e_type */ +#define ET_LOPROC 0xff00 /* reserved range for processor */ +#define ET_HIPROC 0xffff /* specific e_type */ + +/* e_machine */ +#define EM_NONE 0 /* No Machine */ +#define EM_M32 1 /* AT&T WE 32100 */ +#define EM_SPARC 2 /* SPARC */ +#define EM_386 3 /* Intel 80386 */ +#define EM_68K 4 /* Motorola 68000 */ +#define EM_88K 5 /* Motorola 88000 */ +#if 0 +#define EM_486 6 /* RESERVED - was Intel 80486 */ +#endif +#define EM_860 7 /* Intel 80860 */ +#define EM_MIPS 8 /* MIPS R3000 Big-Endian only */ +#define EM_S370 9 /* IBM System/370 Processor */ +#define EM_MIPS_RS4_BE 10 /* MIPS R4000 Big-Endian */ +#if 0 +#define EM_SPARC64 11 /* RESERVED - was SPARC v9 + 64-bit unoffical */ +#endif +/* RESERVED 11-14 for future use */ +#define EM_PARISC 15 /* HPPA */ +/* RESERVED 16 for future use */ +#define EM_VPP500 17 /* Fujitsu VPP500 */ +#define EM_SPARC32PLUS 18 /* Enhanced instruction set SPARC */ +#define EM_960 19 /* Intel 80960 */ +#define EM_PPC 20 /* PowerPC */ +#define EM_PPC64 21 /* 64-bit PowerPC */ +#define EM_S390 22 /* IBM System/390 Processor */ +/* RESERVED 23-35 for future use */ +#define EM_V800 36 /* NEC V800 */ +#define EM_FR20 37 /* Fujitsu FR20 */ +#define EM_RH32 38 /* TRW RH-32 */ +#define EM_RCE 39 /* Motorola RCE */ +#define EM_ARM 40 /* Advanced Risc Machines ARM */ +#define EM_ALPHA 41 /* Digital Alpha */ +#define EM_SH 42 /* Hitachi SH */ +#define EM_SPARCV9 43 /* SPARC Version 9 */ +#define EM_TRICORE 44 /* Siemens TriCore embedded processor */ +#define EM_ARC 45 /* Argonaut RISC Core */ +#define EM_H8_300 46 /* Hitachi H8/300 */ +#define EM_H8_300H 47 /* Hitachi H8/300H */ +#define EM_H8S 48 /* Hitachi H8S */ +#define EM_H8_500 49 /* Hitachi H8/500 */ +#define EM_IA_64 50 /* Intel Merced */ +#define EM_MIPS_X 51 /* Stanford MIPS-X */ +#define EM_COLDFIRE 52 /* Motorola Coldfire */ +#define EM_68HC12 53 /* Motorola M68HC12 */ +#define EM_MMA 54 /* Fujitsu MMA Multimedia Accelerator*/ +#define EM_PCP 55 /* Siemens PCP */ +#define EM_NCPU 56 /* Sony nCPU embeeded RISC */ +#define EM_NDR1 57 /* Denso NDR1 microprocessor */ +#define EM_STARCORE 58 /* Motorola Start*Core processor */ +#define EM_ME16 59 /* Toyota ME16 processor */ +#define EM_ST100 60 /* STMicroelectronic ST100 processor */ +#define EM_TINYJ 61 /* Advanced Logic Corp. Tinyj emb.fam*/ +#define EM_X86_64 62 /* AMD x86-64 */ +#define EM_PDSP 63 /* Sony DSP Processor */ +/* RESERVED 64,65 for future use */ +#define EM_FX66 66 /* Siemens FX66 microcontroller */ +#define EM_ST9PLUS 67 /* STMicroelectronics ST9+ 8/16 mc */ +#define EM_ST7 68 /* STmicroelectronics ST7 8 bit mc */ +#define EM_68HC16 69 /* Motorola MC68HC16 microcontroller */ +#define EM_68HC11 70 /* Motorola MC68HC11 microcontroller */ +#define EM_68HC08 71 /* Motorola MC68HC08 microcontroller */ +#define EM_68HC05 72 /* Motorola MC68HC05 microcontroller */ +#define EM_SVX 73 /* Silicon Graphics SVx */ +#define EM_ST19 74 /* STMicroelectronics ST19 8 bit mc */ +#define EM_VAX 75 /* Digital VAX */ +#define EM_CHRIS 76 /* Axis Communications embedded proc. */ +#define EM_JAVELIN 77 /* Infineon Technologies emb. proc. */ +#define EM_FIREPATH 78 /* Element 14 64-bit DSP Processor */ +#define EM_ZSP 79 /* LSI Logic 16-bit DSP Processor */ +#define EM_MMIX 80 /* Donald Knuth's edu 64-bit proc. */ +#define EM_HUANY 81 /* Harvard University mach-indep objs */ +#define EM_PRISM 82 /* SiTera Prism */ +#define EM_AVR 83 /* Atmel AVR 8-bit microcontroller */ +#define EM_FR30 84 /* Fujitsu FR30 */ +#define EM_D10V 85 /* Mitsubishi DV10V */ +#define EM_D30V 86 /* Mitsubishi DV30V */ +#define EM_V850 87 /* NEC v850 */ +#define EM_M32R 88 /* Mitsubishi M32R */ +#define EM_MN10300 89 /* Matsushita MN10200 */ +#define EM_MN10200 90 /* Matsushita MN10200 */ +#define EM_PJ 91 /* picoJava */ +#define EM_NUM 92 /* number of machine types */ + +/* Version */ +#define EV_NONE 0 /* Invalid */ +#define EV_CURRENT 1 /* Current */ +#define EV_NUM 2 /* number of versions */ + +/* Section Header */ +typedef struct { + Elf32_Word sh_name; /* name - index into section header + string table section */ + Elf32_Word sh_type; /* type */ + Elf32_Word sh_flags; /* flags */ + Elf32_Addr sh_addr; /* address */ + Elf32_Off sh_offset; /* file offset */ + Elf32_Word sh_size; /* section size */ + Elf32_Word sh_link; /* section header table index link */ + Elf32_Word sh_info; /* extra information */ + Elf32_Word sh_addralign; /* address alignment */ + Elf32_Word sh_entsize; /* section entry size */ +} Elf32_Shdr; + +/* Special Section Indexes */ +#define SHN_UNDEF 0 /* undefined */ +#define SHN_LORESERVE 0xff00 /* lower bounds of reserved indexes */ +#define SHN_LOPROC 0xff00 /* reserved range for processor */ +#define SHN_HIPROC 0xff1f /* specific section indexes */ +#define SHN_LOOS 0xff20 /* reserved range for operating */ +#define SHN_HIOS 0xff3f /* specific semantics */ +#define SHN_ABS 0xfff1 /* absolute value */ +#define SHN_COMMON 0xfff2 /* common symbol */ +#define SHN_XINDEX 0xffff /* Index is an extra table */ +#define SHN_HIRESERVE 0xffff /* upper bounds of reserved indexes */ + +/* sh_type */ +#define SHT_NULL 0 /* inactive */ +#define SHT_PROGBITS 1 /* program defined information */ +#define SHT_SYMTAB 2 /* symbol table section */ +#define SHT_STRTAB 3 /* string table section */ +#define SHT_RELA 4 /* relocation section with addends*/ +#define SHT_HASH 5 /* symbol hash table section */ +#define SHT_DYNAMIC 6 /* dynamic section */ +#define SHT_NOTE 7 /* note section */ +#define SHT_NOBITS 8 /* no space section */ +#define SHT_REL 9 /* relation section without addends */ +#define SHT_SHLIB 10 /* reserved - purpose unknown */ +#define SHT_DYNSYM 11 /* dynamic symbol table section */ +#define SHT_INIT_ARRAY 14 /* Array of constructors */ +#define SHT_FINI_ARRAY 15 /* Array of destructors */ +#define SHT_PREINIT_ARRAY 16 /* Array of pre-constructors */ +#define SHT_GROUP 17 /* Section group */ +#define SHT_SYMTAB_SHNDX 18 /* Extended section indeces */ +#define SHT_NUM 19 /* number of section types */ +#define SHT_LOOS 0x60000000 /* Start OS-specific */ +#define SHT_HIOS 0x6fffffff /* End OS-specific */ +#define SHT_LOPROC 0x70000000 /* reserved range for processor */ +#define SHT_HIPROC 0x7fffffff /* specific section header types */ +#define SHT_LOUSER 0x80000000 /* reserved range for application */ +#define SHT_HIUSER 0xffffffff /* specific indexes */ + +/* Section names */ +#define ELF_BSS ".bss" /* uninitialized data */ +#define ELF_COMMENT ".comment" /* version control information */ +#define ELF_DATA ".data" /* initialized data */ +#define ELF_DATA1 ".data1" /* initialized data */ +#define ELF_DEBUG ".debug" /* debug */ +#define ELF_DYNAMIC ".dynamic" /* dynamic linking information */ +#define ELF_DYNSTR ".dynstr" /* dynamic string table */ +#define ELF_DYNSYM ".dynsym" /* dynamic symbol table */ +#define ELF_FINI ".fini" /* termination code */ +#define ELF_FINI_ARRAY ".fini_array" /* Array of destructors */ +#define ELF_GOT ".got" /* global offset table */ +#define ELF_HASH ".hash" /* symbol hash table */ +#define ELF_INIT ".init" /* initialization code */ +#define ELF_INIT_ARRAY ".init_array" /* Array of constuctors */ +#define ELF_INTERP ".interp" /* Pathname of program interpreter */ +#define ELF_LINE ".line" /* Symbolic line numnber information */ +#define ELF_NOTE ".note" /* Contains note section */ +#define ELF_PLT ".plt" /* Procedure linkage table */ +#define ELF_PREINIT_ARRAY ".preinit_array" /* Array of pre-constructors */ +#define ELF_REL_DATA ".rel.data" /* relocation data */ +#define ELF_REL_FINI ".rel.fini" /* relocation termination code */ +#define ELF_REL_INIT ".rel.init" /* relocation initialization code */ +#define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */ +#define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */ +#define ELF_REL_TEXT ".rel.text" /* relocation code */ +#define ELF_RODATA ".rodata" /* read-only data */ +#define ELF_RODATA1 ".rodata1" /* read-only data */ +#define ELF_SHSTRTAB ".shstrtab" /* section header string table */ +#define ELF_STRTAB ".strtab" /* string table */ +#define ELF_SYMTAB ".symtab" /* symbol table */ +#define ELF_SYMTAB_SHNDX ".symtab_shndx"/* symbol table section index */ +#define ELF_TBSS ".tbss" /* thread local uninit data */ +#define ELF_TDATA ".tdata" /* thread local init data */ +#define ELF_TDATA1 ".tdata1" /* thread local init data */ +#define ELF_TEXT ".text" /* code */ + +/* Section Attribute Flags - sh_flags */ +#define SHF_WRITE 0x1 /* Writable */ +#define SHF_ALLOC 0x2 /* occupies memory */ +#define SHF_EXECINSTR 0x4 /* executable */ +#define SHF_MERGE 0x10 /* Might be merged */ +#define SHF_STRINGS 0x20 /* Contains NULL terminated strings */ +#define SHF_INFO_LINK 0x40 /* sh_info contains SHT index */ +#define SHF_LINK_ORDER 0x80 /* Preserve order after combining*/ +#define SHF_OS_NONCONFORMING 0x100 /* Non-standard OS specific handling */ +#define SHF_GROUP 0x200 /* Member of section group */ +#define SHF_TLS 0x400 /* Thread local storage */ +#define SHF_MASKOS 0x0ff00000 /* OS specific */ +#define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */ + /* specific section attributes */ + +/* Section Group Flags */ +#define GRP_COMDAT 0x1 /* COMDAT group */ +#define GRP_MASKOS 0x0ff00000 /* Mask OS specific flags */ +#define GRP_MASKPROC 0xf0000000 /* Mask processor specific flags */ + +/* Symbol Table Entry */ +typedef struct elf32_sym { + Elf32_Word st_name; /* name - index into string table */ + Elf32_Addr st_value; /* symbol value */ + Elf32_Word st_size; /* symbol size */ + unsigned char st_info; /* type and binding */ + unsigned char st_other; /* 0 - no defined meaning */ + Elf32_Half st_shndx; /* section header index */ +} Elf32_Sym; + +/* Symbol table index */ +#define STN_UNDEF 0 /* undefined */ + +/* Extract symbol info - st_info */ +#define ELF32_ST_BIND(x) ((x) >> 4) +#define ELF32_ST_TYPE(x) (((unsigned int) x) & 0xf) +#define ELF32_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf)) +#define ELF32_ST_VISIBILITY(x) ((x) & 0x3) + +/* Symbol Binding - ELF32_ST_BIND - st_info */ +#define STB_LOCAL 0 /* Local symbol */ +#define STB_GLOBAL 1 /* Global symbol */ +#define STB_WEAK 2 /* like global - lower precedence */ +#define STB_NUM 3 /* number of symbol bindings */ +#define STB_LOOS 10 /* reserved range for operating */ +#define STB_HIOS 12 /* system specific symbol bindings */ +#define STB_LOPROC 13 /* reserved range for processor */ +#define STB_HIPROC 15 /* specific symbol bindings */ + +/* Symbol type - ELF32_ST_TYPE - st_info */ +#define STT_NOTYPE 0 /* not specified */ +#define STT_OBJECT 1 /* data object */ +#define STT_FUNC 2 /* function */ +#define STT_SECTION 3 /* section */ +#define STT_FILE 4 /* file */ +#define STT_NUM 5 /* number of symbol types */ +#define STT_TLS 6 /* Thread local storage symbol */ +#define STT_LOOS 10 /* reserved range for operating */ +#define STT_HIOS 12 /* system specific symbol types */ +#define STT_LOPROC 13 /* reserved range for processor */ +#define STT_HIPROC 15 /* specific symbol types */ + +/* Symbol visibility - ELF32_ST_VISIBILITY - st_other */ +#define STV_DEFAULT 0 /* Normal visibility rules */ +#define STV_INTERNAL 1 /* Processor specific hidden class */ +#define STV_HIDDEN 2 /* Symbol unavailable in other mods */ +#define STV_PROTECTED 3 /* Not preemptible, not exported */ + + +/* Relocation entry with implicit addend */ +typedef struct +{ + Elf32_Addr r_offset; /* offset of relocation */ + Elf32_Word r_info; /* symbol table index and type */ +} Elf32_Rel; + +/* Relocation entry with explicit addend */ +typedef struct +{ + Elf32_Addr r_offset; /* offset of relocation */ + Elf32_Word r_info; /* symbol table index and type */ + Elf32_Sword r_addend; +} Elf32_Rela; + +/* Extract relocation info - r_info */ +#define ELF32_R_SYM(i) ((i) >> 8) +#define ELF32_R_TYPE(i) ((unsigned char) (i)) +#define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t)) + +/* Program Header */ +typedef struct { + Elf32_Word p_type; /* segment type */ + Elf32_Off p_offset; /* segment offset */ + Elf32_Addr p_vaddr; /* virtual address of segment */ + Elf32_Addr p_paddr; /* physical address - ignored? */ + Elf32_Word p_filesz; /* number of bytes in file for seg. */ + Elf32_Word p_memsz; /* number of bytes in mem. for seg. */ + Elf32_Word p_flags; /* flags */ + Elf32_Word p_align; /* memory alignment */ +} Elf32_Phdr; + +/* Segment types - p_type */ +#define PT_NULL 0 /* unused */ +#define PT_LOAD 1 /* loadable segment */ +#define PT_DYNAMIC 2 /* dynamic linking section */ +#define PT_INTERP 3 /* the RTLD */ +#define PT_NOTE 4 /* auxiliary information */ +#define PT_SHLIB 5 /* reserved - purpose undefined */ +#define PT_PHDR 6 /* program header */ +#define PT_TLS 7 /* Thread local storage template */ +#define PT_NUM 8 /* Number of segment types */ +#define PT_LOOS 0x60000000 /* reserved range for operating */ +#define PT_HIOS 0x6fffffff /* system specific segment types */ +#define PT_LOPROC 0x70000000 /* reserved range for processor */ +#define PT_HIPROC 0x7fffffff /* specific segment types */ + +/* Segment flags - p_flags */ +#define PF_X 0x1 /* Executable */ +#define PF_W 0x2 /* Writable */ +#define PF_R 0x4 /* Readable */ +#define PF_MASKOS 0x0ff00000 /* OS specific segment flags */ +#define PF_MASKPROC 0xf0000000 /* reserved bits for processor */ + /* specific segment flags */ +/* Dynamic structure */ +typedef struct +{ + Elf32_Sword d_tag; /* controls meaning of d_val */ + union + { + Elf32_Word d_val; /* Multiple meanings - see d_tag */ + Elf32_Addr d_ptr; /* program virtual address */ + } d_un; +} Elf32_Dyn; + +extern Elf32_Dyn _DYNAMIC[]; + +/* Dynamic Array Tags - d_tag */ +#define DT_NULL 0 /* marks end of _DYNAMIC array */ +#define DT_NEEDED 1 /* string table offset of needed lib */ +#define DT_PLTRELSZ 2 /* size of relocation entries in PLT */ +#define DT_PLTGOT 3 /* address PLT/GOT */ +#define DT_HASH 4 /* address of symbol hash table */ +#define DT_STRTAB 5 /* address of string table */ +#define DT_SYMTAB 6 /* address of symbol table */ +#define DT_RELA 7 /* address of relocation table */ +#define DT_RELASZ 8 /* size of relocation table */ +#define DT_RELAENT 9 /* size of relocation entry */ +#define DT_STRSZ 10 /* size of string table */ +#define DT_SYMENT 11 /* size of symbol table entry */ +#define DT_INIT 12 /* address of initialization func. */ +#define DT_FINI 13 /* address of termination function */ +#define DT_SONAME 14 /* string table offset of shared obj */ +#define DT_RPATH 15 /* string table offset of library + search path */ +#define DT_SYMBOLIC 16 /* start sym search in shared obj. */ +#define DT_REL 17 /* address of rel. tbl. w addends */ +#define DT_RELSZ 18 /* size of DT_REL relocation table */ +#define DT_RELENT 19 /* size of DT_REL relocation entry */ +#define DT_PLTREL 20 /* PLT referenced relocation entry */ +#define DT_DEBUG 21 /* bugger */ +#define DT_TEXTREL 22 /* Allow rel. mod. to unwritable seg */ +#define DT_JMPREL 23 /* add. of PLT's relocation entries */ +#define DT_BIND_NOW 24 /* Process relocations of object */ +#define DT_INIT_ARRAY 25 /* Array with addresses of init fct */ +#define DT_FINI_ARRAY 26 /* Array with addresses of fini fct */ +#define DT_INIT_ARRAYSZ 27 /* Size in bytes of DT_INIT_ARRAY */ +#define DT_FINI_ARRAYSZ 28 /* Size in bytes of DT_FINI_ARRAY */ +#define DT_RUNPATH 29 /* Library search path */ +#define DT_FLAGS 30 /* Flags for the object being loaded */ +#define DT_ENCODING 32 /* Start of encoded range */ +#define DT_PREINIT_ARRAY 32 /* Array with addresses of preinit fct*/ +#define DT_PREINIT_ARRAYSZ 33 /* size in bytes of DT_PREINIT_ARRAY */ +#define DT_NUM 34 /* Number used. */ +#define DT_LOOS 0x60000000 /* reserved range for OS */ +#define DT_HIOS 0x6fffffff /* specific dynamic array tags */ +#define DT_LOPROC 0x70000000 /* reserved range for processor */ +#define DT_HIPROC 0x7fffffff /* specific dynamic array tags */ + +/* Dynamic Tag Flags - d_un.d_val */ +#define DF_ORIGIN 0x01 /* Object may use DF_ORIGIN */ +#define DF_SYMBOLIC 0x02 /* Symbol resolutions starts here */ +#define DF_TEXTREL 0x04 /* Object contains text relocations */ +#define DF_BIND_NOW 0x08 /* No lazy binding for this object */ +#define DF_STATIC_TLS 0x10 /* Static thread local storage */ + +/* Standard ELF hashing function */ +unsigned long elf_hash(const unsigned char *name); + +#define ELF_TARG_VER 1 /* The ver for which this code is intended */ + +/* + * XXX - PowerPC defines really don't belong in here, + * but we'll put them in for simplicity. + */ + +/* Values for Elf32/64_Ehdr.e_flags. */ +#define EF_PPC_EMB 0x80000000 /* PowerPC embedded flag */ + +/* Cygnus local bits below */ +#define EF_PPC_RELOCATABLE 0x00010000 /* PowerPC -mrelocatable flag*/ +#define EF_PPC_RELOCATABLE_LIB 0x00008000 /* PowerPC -mrelocatable-lib + flag */ + +/* PowerPC relocations defined by the ABIs */ +#define R_PPC_NONE 0 +#define R_PPC_ADDR32 1 /* 32bit absolute address */ +#define R_PPC_ADDR24 2 /* 26bit address, 2 bits ignored. */ +#define R_PPC_ADDR16 3 /* 16bit absolute address */ +#define R_PPC_ADDR16_LO 4 /* lower 16bit of absolute address */ +#define R_PPC_ADDR16_HI 5 /* high 16bit of absolute address */ +#define R_PPC_ADDR16_HA 6 /* adjusted high 16bit */ +#define R_PPC_ADDR14 7 /* 16bit address, 2 bits ignored */ +#define R_PPC_ADDR14_BRTAKEN 8 +#define R_PPC_ADDR14_BRNTAKEN 9 +#define R_PPC_REL24 10 /* PC relative 26 bit */ +#define R_PPC_REL14 11 /* PC relative 16 bit */ +#define R_PPC_REL14_BRTAKEN 12 +#define R_PPC_REL14_BRNTAKEN 13 +#define R_PPC_GOT16 14 +#define R_PPC_GOT16_LO 15 +#define R_PPC_GOT16_HI 16 +#define R_PPC_GOT16_HA 17 +#define R_PPC_PLTREL24 18 +#define R_PPC_COPY 19 +#define R_PPC_GLOB_DAT 20 +#define R_PPC_JMP_SLOT 21 +#define R_PPC_RELATIVE 22 +#define R_PPC_LOCAL24PC 23 +#define R_PPC_UADDR32 24 +#define R_PPC_UADDR16 25 +#define R_PPC_REL32 26 +#define R_PPC_PLT32 27 +#define R_PPC_PLTREL32 28 +#define R_PPC_PLT16_LO 29 +#define R_PPC_PLT16_HI 30 +#define R_PPC_PLT16_HA 31 +#define R_PPC_SDAREL16 32 +#define R_PPC_SECTOFF 33 +#define R_PPC_SECTOFF_LO 34 +#define R_PPC_SECTOFF_HI 35 +#define R_PPC_SECTOFF_HA 36 +/* Keep this the last entry. */ +#define R_PPC_NUM 37 + +/* The remaining relocs are from the Embedded ELF ABI, and are not + in the SVR4 ELF ABI. */ +#define R_PPC_EMB_NADDR32 101 +#define R_PPC_EMB_NADDR16 102 +#define R_PPC_EMB_NADDR16_LO 103 +#define R_PPC_EMB_NADDR16_HI 104 +#define R_PPC_EMB_NADDR16_HA 105 +#define R_PPC_EMB_SDAI16 106 +#define R_PPC_EMB_SDA2I16 107 +#define R_PPC_EMB_SDA2REL 108 +#define R_PPC_EMB_SDA21 109 /* 16 bit offset in SDA */ +#define R_PPC_EMB_MRKREF 110 +#define R_PPC_EMB_RELSEC16 111 +#define R_PPC_EMB_RELST_LO 112 +#define R_PPC_EMB_RELST_HI 113 +#define R_PPC_EMB_RELST_HA 114 +#define R_PPC_EMB_BIT_FLD 115 +#define R_PPC_EMB_RELSDA 116 /* 16 bit relative offset in SDA */ + +/* Diab tool relocations. */ +#define R_PPC_DIAB_SDA21_LO 180 /* like EMB_SDA21, but lower 16 bit */ +#define R_PPC_DIAB_SDA21_HI 181 /* like EMB_SDA21, but high 16 bit */ +#define R_PPC_DIAB_SDA21_HA 182 /* like EMB_SDA21, adjusted high 16 */ +#define R_PPC_DIAB_RELSDA_LO 183 /* like EMB_RELSDA, but lower 16 bit */ +#define R_PPC_DIAB_RELSDA_HI 184 /* like EMB_RELSDA, but high 16 bit */ +#define R_PPC_DIAB_RELSDA_HA 185 /* like EMB_RELSDA, adjusted high 16 */ + +/* This is a phony reloc to handle any old fashioned TOC16 references + that may still be in object files. */ +#define R_PPC_TOC16 255 + +#endif /* _ELF_H */ + diff --git a/tcploader/wii/main.c b/tcploader/wii/main.c new file mode 100644 index 0000000..9af3534 --- /dev/null +++ b/tcploader/wii/main.c @@ -0,0 +1,261 @@ +/*------------------------------------------------------------- + +tcploader -- a very small tcp elf loader for the wii + +Copyright (C) 2008 Sven Peter + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1.The origin of this software must not be misrepresented; you +must not claim that you wrote the original software. If you use +this software in a product, an acknowledgment in the product +documentation would be appreciated but is not required. + +2.Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3.This notice may not be removed or altered from any source +distribution. + +-------------------------------------------------------------*/ + +#include +#include +#include +#include +#include +#include +#include + +#define READ_SIZE (1 << 10) + +#include "asm.h" +#include "processor.h" +#include "elf.h" +#include "dol.h" + + +static void *xfb = NULL; +static GXRModeObj *rmode = NULL; + +u8 *data = (u8 *)0x92000000; + +static inline void wait_for(u32 btn) +{ + while(1) + { + PAD_ScanPads(); + if(PAD_ButtonsHeld(0) & btn) + break; + } +} + + +extern s32 __STM_Init(); +extern void __exception_closeall(); +extern s32 __IOS_ShutdownSubsystems(); + +/* code to establish a socket; originally from bzs@bu-cs.bu.edu + */ + +int establish(unsigned short portnum) { + int s; + struct sockaddr_in sa; + + memset(&sa, 0, sizeof(struct sockaddr_in)); /* clear our address */ + sa.sin_family= AF_INET; /* this is our host address */ + sa.sin_port= htons(portnum); /* this is our port number */ + sa.sin_addr.s_addr = net_gethostip(); + sa.sin_len = 8; + if ((s= net_socket(AF_INET, SOCK_STREAM, 0)) < 0) /* create socket */ + return(-1); + if (net_bind(s,(struct sockaddr *)&sa,8) < 0) { + net_close(s); + return(-1); /* bind address to socket */ + } + net_listen(s, 3); /* max # of queued connects */ + return(s); +} + +// wait for a connection to occur on a socket created with establish() + +int get_connection(int s) +{ + int t; /* socket of connection */ + struct sockaddr_in sa; + sa.sin_len = 8; + sa.sin_family = AF_INET; + u32 buflen = 8; + t = net_accept(s,(struct sockaddr *)&sa, &buflen); + printf("Incoming connection from %d.%d.%d.%d\n", + (sa.sin_addr.s_addr >> 24) & 0xFF, + (sa.sin_addr.s_addr >> 16) & 0xFF, + (sa.sin_addr.s_addr >> 8) & 0xFF, + (sa.sin_addr.s_addr) & 0xFF); + + return t; +} + +int read_data(int s, /* connected socket */ + char *buf, /* pointer to the buffer */ + int n /* number of characters (bytes) we want */ + ) +{ int bcount; /* counts bytes read */ + int br; /* bytes read this pass */ + + bcount= 0; + br= 0; + while (bcount < n) { /* loop until full buffer */ + if ((br= net_read(s,buf,n-bcount)) > 0) { + bcount += br; /* increment byte counter */ + buf += br; /* move buffer ptr for next read */ + } + else if (br < 0) /* signal an error to the caller */ + return br; + } + // debug_printf("read_data(%d, %p, %d): \n", s, buf, n); + // printf("read_data(%d, %p, %d): %d\n", s, buf, n, bcount); + return bcount; +} + +int write_data(int s, char *buf, int n) +{ + int bcount; + int br; + bcount = 0; + br = 0; + while(bcount < n) { + if ((br = net_write(s,buf,n-bcount)) > 0) { + bcount += br; + buf += br; + } + else if (br < 0) + return br; + } + // debug_printf("write_data(%d, %p, %d): \n", s, buf, n); + // hexdump(buf, bcount); + return (bcount); +} + +int main(int argc, char **argv) +{ + unsigned long level; + u32 res, offset, read, size, ip; + u8 *oct = (u8 *)&ip; + u8 *bfr[READ_SIZE]; + void (*ep)(); + + VIDEO_Init(); + PAD_Init(); + + switch(VIDEO_GetCurrentTvMode()) + { + case VI_PAL: + rmode = &TVPal528IntDf; + break; + case VI_MPAL: + rmode = &TVMpal480IntDf; + default: + case VI_NTSC: + rmode = &TVNtsc480IntDf; + break; + } + + xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode)); + console_init(xfb, 20, 20, rmode->fbWidth, rmode->xfbHeight, rmode->fbWidth * VI_DISPLAY_PIX_SZ); + + VIDEO_Configure(rmode); + VIDEO_SetBlack(FALSE); + VIDEO_SetNextFramebuffer(xfb); + VIDEO_Flush(); + VIDEO_WaitVSync(); + if(rmode->viTVMode & VI_NON_INTERLACE) + VIDEO_WaitVSync(); + + __STM_Init(); + + printf("\n\nTCP ELF Loader\n"); + printf("(c) 2008 Sven Peter\n"); + printf("Based on bushing's title_listener.c and his networking code.\n"); + printf("(c) 2008 bushing\n\n"); + + redo: + printf("Press A to continue.\n"); + wait_for(PAD_BUTTON_A); + + printf("net_init(), please wait...\n"); + net_init(); + + + printf("Opening socket on port 8080\n"); + s32 listen = establish(8080); + + if(listen < 0) + { + printf("establish() returned < 0.\n"); + goto redo; + } + + ip = net_gethostip(); + printf("fd = %d\n", listen); + printf("You Wii's ip address is %d.%d.%d.%d\n", oct[0], oct[1], oct[2], oct[3]); + printf("Waiting for connection\n"); + s32 client = get_connection(listen); + + if(client > 0) + { + printf("client connected..\n"); + + if(read_data(client, (char *)&size, 4) < 0) + { + printf("read_data() error while reading filesize\n"); + net_close(client); + net_close(listen); + goto redo; + } + + printf("size: %d\n", size); + printf("reading data, please wait...\n"); + + offset = 0; + while(offset < size && (read = read_data(client, (char *)bfr, (size - offset) > READ_SIZE ? READ_SIZE : (size - offset))) > 0) + { + memcpy(data + offset, bfr, READ_SIZE); + offset += read; + } + net_close(client); + net_close(listen); + + res = valid_elf_image(data); + if(res != 1) + { + printf("Invalid ELF image.\n"); + printf("assuming DOL image. your wii will crash if this is just some random file...\n"); + ep = (void(*)())load_dol_image(data, 1); + } + else + { + printf("Loading ELF image...\n"); + ep = (void(*)())load_elf_image(data); + } + printf("entry point: 0x%08X\n", (unsigned int)ep); + printf("shutting down...\n"); + __IOS_ShutdownSubsystems(); + _CPU_ISR_Disable(level); + __exception_closeall(); + printf("jumping to entry point now...\n\n\n\n"); + ep(); + _CPU_ISR_Restore(level); + printf("this should not happen :/\n\n"); + goto redo; + } + + // never reached but fixes a gcc warning + return 0; +} diff --git a/tcploader/wii/preloader.c b/tcploader/wii/preloader.c new file mode 100644 index 0000000..093320a --- /dev/null +++ b/tcploader/wii/preloader.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include "dol.h" +#include "asm.h" +#include "processor.h" + +extern const u8 loader_dol[]; +extern const u32 loader_dol_size; + +extern void __exception_closeall(); +extern s32 __IOS_ShutdownSubsystems(); + +static u32 *xfb; +static GXRModeObj *rmode; + +int main(int argc, char *argv[]) +{ + void (*ep)(); + unsigned long level; + + // doesn't work for some odd reason when this tuff is not done :/ + VIDEO_Init(); + PAD_Init(); + + switch(VIDEO_GetCurrentTvMode()) + { + case VI_PAL: + rmode = &TVPal528IntDf; + break; + case VI_MPAL: + rmode = &TVMpal480IntDf; + default: + case VI_NTSC: + rmode = &TVNtsc480IntDf; + break; + } + + xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode)); + console_init(xfb, 20, 20, rmode->fbWidth, rmode->xfbHeight, rmode->fbWidth * VI_DISPLAY_PIX_SZ); + + VIDEO_Configure(rmode); + VIDEO_SetBlack(FALSE); + VIDEO_SetNextFramebuffer(xfb); + VIDEO_Flush(); + VIDEO_WaitVSync(); + if(rmode->viTVMode & VI_NON_INTERLACE) + VIDEO_WaitVSync(); + + printf("loading, please wait...\n"); + memcpy((void *)0x90000020, loader_dol, loader_dol_size); +// memcpy((void *)0x80001800, stub_bin, stub_bin_size); + + ep = (void(*)())load_dol_image((void *)loader_dol, 1); + __IOS_ShutdownSubsystems(); + _CPU_ISR_Disable(level); + __exception_closeall(); + ep(); + return 0; +} diff --git a/tcploader/wii/processor.h b/tcploader/wii/processor.h new file mode 100644 index 0000000..01618b6 --- /dev/null +++ b/tcploader/wii/processor.h @@ -0,0 +1,115 @@ +// this file was taken from libogc, see http://www.devkitpro.org/ + +#ifndef __PROCESSOR_H__ +#define __PROCESSOR_H__ + +#include + +#define __stringify(rn) #rn +#define ATTRIBUTE_ALIGN(v) __attribute__((aligned(v))) + +#define _sync() asm volatile("sync") +#define _nop() asm volatile("nop") +#define ppcsync() asm volatile("sc") +#define ppchalt() ({ \ + asm volatile("sync"); \ + while(1) { \ + asm volatile("nop"); \ + asm volatile("li 3,0"); \ + asm volatile("nop"); \ + } \ +}) + +#define mfdcr(_rn) ({register u32 _rval; \ + asm volatile("mfdcr %0," __stringify(_rn) \ + : "=r" (_rval)); _rval;}) +#define mtdcr(rn, val) asm volatile("mtdcr " __stringify(rn) ",%0" : : "r" (val)) + +#define mfmsr() ({register u32 _rval; \ + asm volatile("mfmsr %0" : "=r" (_rval)); _rval;}) +#define mtmsr(val) asm volatile("mtmsr %0" : : "r" (val)) + +#define mfdec() ({register u32 _rval; \ + asm volatile("mfdec %0" : "=r" (_rval)); _rval;}) +#define mtdec(_val) asm volatile("mtdec %0" : : "r" (_val)) + +#define mfspr(_rn) \ +({ register u32 _rval = 0; \ + asm volatile("mfspr %0," __stringify(_rn) \ + : "=r" (_rval));\ + _rval; \ +}) + +#define mtspr(_rn, _val) asm volatile("mtspr " __stringify(_rn) ",%0" : : "r" (_val)) + +#define mfwpar() mfspr(WPAR) +#define mtwpar(_val) mtspr(WPAR,_val) + +#define mfmmcr0() mfspr(MMCR0) +#define mtmmcr0(_val) mtspr(MMCR0,_val) +#define mfmmcr1() mfspr(MMCR1) +#define mtmmcr1(_val) mtspr(MMCR1,_val) + +#define mfpmc1() mfspr(PMC1) +#define mtpmc1(_val) mtspr(PMC1,_val) +#define mfpmc2() mfspr(PMC2) +#define mtpmc2(_val) mtspr(PMC2,_val) +#define mfpmc3() mfspr(PMC3) +#define mtpmc3(_val) mtspr(PMC3,_val) +#define mfpmc4() mfspr(PMC4) +#define mtpmc4(_val) mtspr(PMC4,_val) + +#define mfhid0() mfspr(HID0) +#define mthid0(_val) mtspr(HID0,_val) +#define mfhid1() mfspr(HID1) +#define mthid1(_val) mtspr(HID1,_val) +#define mfhid2() mfspr(HID2) +#define mthid2(_val) mtspr(HID2,_val) +#define mfhid4() mfspr(HID4) +#define mthid4(_val) mtspr(HID4,_val) + +#define cntlzw(_val) ({register u32 _rval; \ + asm volatile("cntlzw %0, %1" : "=r"((_rval)) : "r"((_val))); _rval;}) + +#define _CPU_MSR_GET( _msr_value ) \ + do { \ + _msr_value = 0; \ + asm volatile ("mfmsr %0" : "=&r" ((_msr_value)) : "0" ((_msr_value))); \ + } while (0) + +#define _CPU_MSR_SET( _msr_value ) \ +{ asm volatile ("mtmsr %0" : "=&r" ((_msr_value)) : "0" ((_msr_value))); } + +#define _CPU_ISR_Enable() \ + { register u32 _val = 0; \ + asm volatile ("mfmsr %0; ori %0,%0,0x8000; mtmsr %0" : \ + "=&r" (_val) : "0" (_val));\ + } + +#define _CPU_ISR_Disable( _isr_cookie ) \ + { register u32 _disable_mask = MSR_EE; \ + _isr_cookie = 0; \ + asm volatile ( \ + "mfmsr %0; andc %1,%0,%1; mtmsr %1" : \ + "=&r" ((_isr_cookie)), "=&r" ((_disable_mask)) : \ + "0" ((_isr_cookie)), "1" ((_disable_mask)) \ + ); \ + } + +#define _CPU_ISR_Restore( _isr_cookie ) \ + { \ + asm volatile ( "mtmsr %0" : \ + "=r" ((_isr_cookie)) : \ + "0" ((_isr_cookie))); \ + } + +#define _CPU_ISR_Flash( _isr_cookie ) \ + { register u32 _disable_mask = MSR_EE; \ + asm volatile ( \ + "mtmsr %0; andc %1,%0,%1; mtmsr %1" : \ + "=r" ((_isr_cookie)), "=r" ((_disable_mask)) : \ + "0" ((_isr_cookie)), "1" ((_disable_mask)) \ + ); \ + } + +#endif -- 2.11.4.GIT