2009-04-03 16:27:19 +02:00
|
|
|
/*
|
|
|
|
mini - a Free Software replacement for the Nintendo/BroadOn IOS.
|
|
|
|
|
|
|
|
Copyright (C) 2008, 2009 Haxx Enterprises
|
|
|
|
Copyright (C) 2008, 2009 Sven Peter <svenpeter@gmail.com>
|
2009-04-10 18:15:10 +02:00
|
|
|
Copyright (C) 2008, 2009 Hector Martin "marcan" <marcan@marcansoft.com>
|
2009-04-03 16:27:19 +02:00
|
|
|
TODO add yourself here, kthx
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
2008-12-28 14:35:37 +01:00
|
|
|
#include "types.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "start.h"
|
|
|
|
#include "hollywood.h"
|
2009-04-11 18:20:16 +02:00
|
|
|
#include "sdhcvar.h"
|
2008-12-28 14:35:37 +01:00
|
|
|
#include "string.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "elf.h"
|
|
|
|
#include "gecko.h"
|
|
|
|
#include "ff.h"
|
|
|
|
#include "panic.h"
|
|
|
|
#include "powerpc_elf.h"
|
2009-01-08 23:27:22 +01:00
|
|
|
#include "irq.h"
|
2009-01-16 08:51:51 +01:00
|
|
|
#include "ipc.h"
|
2009-01-16 08:53:57 +01:00
|
|
|
#include "exception.h"
|
2009-01-17 15:05:13 +01:00
|
|
|
#include "crypto.h"
|
|
|
|
#include "nand.h"
|
2009-02-24 16:20:28 +01:00
|
|
|
#include "boot2.h"
|
2009-01-17 15:05:13 +01:00
|
|
|
|
2008-12-28 14:35:37 +01:00
|
|
|
typedef struct {
|
|
|
|
u32 hdrsize;
|
|
|
|
u32 loadersize;
|
|
|
|
u32 elfsize;
|
|
|
|
u32 argument;
|
|
|
|
} ioshdr;
|
|
|
|
|
2009-01-06 00:13:39 +01:00
|
|
|
u32 match[] = {
|
|
|
|
0xF7FFFFB8,
|
|
|
|
0xBC024708,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline void mem1_poke(u8 *dst, u8 bv)
|
|
|
|
{
|
|
|
|
u32 *p = (u32*)(((u32)dst) & ~3);
|
|
|
|
u32 val = *p;
|
2009-01-16 08:51:51 +01:00
|
|
|
|
2009-01-06 00:13:39 +01:00
|
|
|
switch(((u32)dst) & 3) {
|
|
|
|
case 0:
|
|
|
|
val = (val & 0x00FFFFFF) | bv << 24;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
val = (val & 0xFF00FFFF) | bv << 16;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
val = (val & 0xFFFF00FF) | bv << 8;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
val = (val & 0xFFFFFF00) | bv;
|
|
|
|
break;
|
|
|
|
}
|
2009-01-16 08:51:51 +01:00
|
|
|
|
2009-01-06 00:13:39 +01:00
|
|
|
*p = val;
|
|
|
|
}
|
2008-12-28 14:35:37 +01:00
|
|
|
|
2009-01-06 00:13:39 +01:00
|
|
|
void *memcpy_mem1(void *dst, const void *src, size_t n)
|
|
|
|
{
|
|
|
|
unsigned char *p;
|
|
|
|
const unsigned char *q;
|
|
|
|
|
|
|
|
for (p = dst, q = src; n; n--) {
|
|
|
|
mem1_poke(p++, *q++);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void patch_mem(u8 *offset, u32 size, u64 titleID)
|
|
|
|
{
|
|
|
|
while(size>sizeof(match)) {
|
|
|
|
if(!memcmp(offset, match, sizeof(match))) {
|
|
|
|
gecko_printf("--> Patching @ %p\n", offset+8);
|
|
|
|
gecko_printf("--> To TitleID %08x%08x\n", (u32)(titleID>>32), (u32)titleID);
|
|
|
|
memcpy_mem1(offset+8, &titleID, sizeof(u64));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
offset++;
|
|
|
|
size -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void boot2_patchelf(u8 *elf, u64 titleID) {
|
2009-04-05 15:43:25 +02:00
|
|
|
gecko_printf("Patching boot2 ELF...\n");
|
2008-12-28 14:35:37 +01:00
|
|
|
|
|
|
|
if(memcmp("\x7F" "ELF\x01\x02\x01\x61\x01",elf,9)) {
|
2009-01-06 00:13:39 +01:00
|
|
|
gecko_printf("Invalid ELF header! 0x%02x 0x%02x 0x%02x 0x%02x\n",elf[0], elf[1], elf[2], elf[3]);
|
|
|
|
return;
|
2008-12-28 14:35:37 +01:00
|
|
|
}
|
2009-01-06 00:13:39 +01:00
|
|
|
|
2008-12-28 14:35:37 +01:00
|
|
|
Elf32_Ehdr *ehdr = (Elf32_Ehdr*)elf;
|
|
|
|
if(ehdr->e_phoff == 0) {
|
2009-01-06 00:13:39 +01:00
|
|
|
gecko_printf("ELF has no program headers!\n");
|
|
|
|
return;
|
2008-12-28 14:35:37 +01:00
|
|
|
}
|
|
|
|
int count = ehdr->e_phnum;
|
|
|
|
Elf32_Phdr *phdr = (Elf32_Phdr*)(elf + ehdr->e_phoff);
|
2009-01-06 00:13:39 +01:00
|
|
|
gecko_printf("PHDRS at %p\n",phdr);
|
2008-12-28 14:35:37 +01:00
|
|
|
while(count--)
|
|
|
|
{
|
|
|
|
if(phdr->p_type != PT_LOAD) {
|
2009-01-06 00:13:39 +01:00
|
|
|
gecko_printf("Skipping PHDR of type %d\n",phdr->p_type);
|
2008-12-28 14:35:37 +01:00
|
|
|
} else {
|
|
|
|
void *src = elf + phdr->p_offset;
|
2009-01-06 00:13:39 +01:00
|
|
|
gecko_printf("PATCH %p -> %p/%p [0x%x]\n",src, phdr->p_paddr, phdr->p_vaddr, phdr->p_filesz);
|
|
|
|
if(phdr->p_vaddr == (void*)0x20100000) {
|
|
|
|
gecko_printf("-> Found ES code PHDR\n");
|
|
|
|
patch_mem(src, phdr->p_filesz, titleID);
|
|
|
|
}
|
2008-12-28 14:35:37 +01:00
|
|
|
}
|
|
|
|
phdr++;
|
|
|
|
}
|
2009-04-05 15:43:25 +02:00
|
|
|
gecko_printf("Done!\n");
|
2008-12-28 14:35:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define PPC_BOOT_FILE "/system/ppcboot.elf"
|
|
|
|
|
|
|
|
FATFS fatfs;
|
|
|
|
|
2009-01-06 00:13:39 +01:00
|
|
|
void *patch_boot2(void *base, u64 titleID)
|
2008-12-28 20:28:33 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
ioshdr *hdr = (ioshdr*)base;
|
|
|
|
ioshdr *parhdr = (ioshdr*)hdr->argument;
|
|
|
|
u8 *elf;
|
|
|
|
|
2009-04-05 15:43:25 +02:00
|
|
|
gecko_printf("Patching BOOT2 for leet hax\n");
|
2008-12-28 20:28:33 +01:00
|
|
|
gecko_printf("Parent BOOT2 header (@%p):\n",parhdr);
|
|
|
|
gecko_printf(" Header size: %08x\n", parhdr->hdrsize);
|
|
|
|
gecko_printf(" Loader size: %08x\n", parhdr->loadersize);
|
|
|
|
gecko_printf(" ELF size: %08x\n", parhdr->elfsize);
|
|
|
|
gecko_printf(" Argument: %08x\n", parhdr->argument);
|
2009-01-06 00:13:39 +01:00
|
|
|
|
2008-12-28 20:28:33 +01:00
|
|
|
elf = (u8*) parhdr;
|
|
|
|
elf += parhdr->hdrsize + parhdr->loadersize;
|
|
|
|
gecko_printf("ELF at %p\n",elf);
|
2009-01-06 00:13:39 +01:00
|
|
|
|
|
|
|
boot2_patchelf(elf, titleID);
|
|
|
|
|
|
|
|
parhdr->argument = 0x42;
|
2009-01-16 08:51:51 +01:00
|
|
|
|
2009-01-06 00:13:39 +01:00
|
|
|
gecko_printf("Vector: %p\n", (void*)(((u32)parhdr) + parhdr->hdrsize));
|
|
|
|
|
|
|
|
return (void*)(((u32)parhdr) + parhdr->hdrsize);
|
2008-12-28 20:28:33 +01:00
|
|
|
}
|
|
|
|
|
2009-04-10 17:25:13 +02:00
|
|
|
u32 _main(void *base)
|
2008-12-28 14:35:37 +01:00
|
|
|
{
|
|
|
|
FRESULT fres;
|
|
|
|
int res;
|
2009-04-10 17:25:13 +02:00
|
|
|
u32 vector;
|
2008-12-28 14:35:37 +01:00
|
|
|
|
2009-01-06 00:13:39 +01:00
|
|
|
gecko_init();
|
2009-04-05 15:43:25 +02:00
|
|
|
gecko_printf("mini v0.2 loading\n");
|
2008-12-28 14:35:37 +01:00
|
|
|
|
2009-04-05 15:43:25 +02:00
|
|
|
gecko_printf("Initializing exceptions...\n");
|
2009-01-16 08:53:57 +01:00
|
|
|
exception_initialize();
|
2009-04-05 15:43:25 +02:00
|
|
|
gecko_printf("Configuring caches and MMU...\n");
|
2009-01-16 08:53:03 +01:00
|
|
|
mem_initialize();
|
|
|
|
|
2009-03-06 05:28:43 +01:00
|
|
|
gecko_printf("IOSflags: %08x %08x %08x\n",
|
|
|
|
read32(0xffffff00), read32(0xffffff04), read32(0xffffff08));
|
|
|
|
gecko_printf(" %08x %08x %08x\n",
|
|
|
|
read32(0xffffff0c), read32(0xffffff10), read32(0xffffff14));
|
|
|
|
|
2009-01-08 23:27:22 +01:00
|
|
|
irq_initialize();
|
|
|
|
irq_enable(IRQ_TIMER);
|
2009-04-11 23:42:53 +02:00
|
|
|
// irq_enable(IRQ_GPIO1B);
|
2009-01-08 23:27:22 +01:00
|
|
|
irq_enable(IRQ_GPIO1);
|
|
|
|
irq_enable(IRQ_RESET);
|
2009-04-10 11:25:10 +02:00
|
|
|
gecko_timer_initialize();
|
2009-04-05 15:43:25 +02:00
|
|
|
gecko_printf("Interrupts initialized\n");
|
2009-01-06 00:13:39 +01:00
|
|
|
|
2009-01-17 15:05:13 +01:00
|
|
|
crypto_initialize();
|
2009-04-05 15:43:25 +02:00
|
|
|
gecko_printf("crypto support initialized\n");
|
2009-01-17 15:05:13 +01:00
|
|
|
|
|
|
|
nand_initialize();
|
2009-04-05 15:43:25 +02:00
|
|
|
gecko_printf("NAND initialized.\n");
|
2009-01-17 15:05:13 +01:00
|
|
|
|
2009-02-24 16:20:28 +01:00
|
|
|
boot2_init();
|
|
|
|
|
2009-04-05 15:43:25 +02:00
|
|
|
gecko_printf("Initializing IPC...\n");
|
2009-01-16 08:51:51 +01:00
|
|
|
ipc_initialize();
|
2009-01-06 00:13:39 +01:00
|
|
|
|
2009-04-11 18:20:16 +02:00
|
|
|
gecko_printf("Initializing SDHC...\n");
|
|
|
|
sdhc_init();
|
|
|
|
// sd_initialize();
|
2009-02-02 12:36:46 +01:00
|
|
|
|
2009-04-05 15:43:25 +02:00
|
|
|
gecko_printf("Mounting SD...\n");
|
2009-01-16 08:51:51 +01:00
|
|
|
fres = f_mount(0, &fatfs);
|
|
|
|
|
2009-04-03 18:04:55 +02:00
|
|
|
if (read32(0x0d800190) & 2) {
|
2009-04-05 15:43:25 +02:00
|
|
|
gecko_printf("GameCube compatibility mode detected...\n");
|
2009-04-10 17:25:13 +02:00
|
|
|
vector = boot2_run(1, 0x101);
|
2009-04-03 18:04:55 +02:00
|
|
|
goto shutdown;
|
|
|
|
}
|
|
|
|
|
2009-01-16 08:51:51 +01:00
|
|
|
if(fres != FR_OK) {
|
|
|
|
gecko_printf("Error %d while trying to mount SD\n", fres);
|
|
|
|
panic2(0, PANIC_MOUNT);
|
|
|
|
}
|
|
|
|
|
2009-04-05 15:43:25 +02:00
|
|
|
gecko_printf("Trying to boot:" PPC_BOOT_FILE "\n");
|
2009-01-06 00:13:39 +01:00
|
|
|
|
2009-04-08 16:30:32 +02:00
|
|
|
res = powerpc_boot_file(PPC_BOOT_FILE);
|
2008-12-28 14:35:37 +01:00
|
|
|
if(res < 0) {
|
|
|
|
gecko_printf("Failed to boot PPC: %d\n", res);
|
2009-04-05 15:43:25 +02:00
|
|
|
gecko_printf("Continuing anyway\n");
|
2008-12-28 14:35:37 +01:00
|
|
|
}
|
|
|
|
|
2009-04-05 15:43:25 +02:00
|
|
|
gecko_printf("Going into IPC mainloop...\n");
|
2009-04-10 17:25:13 +02:00
|
|
|
vector = ipc_process_slow();
|
2009-04-05 15:43:25 +02:00
|
|
|
gecko_printf("IPC mainloop done!\n");
|
|
|
|
gecko_printf("Shutting down IPC...\n");
|
2009-01-16 08:51:51 +01:00
|
|
|
ipc_shutdown();
|
2009-04-03 18:04:55 +02:00
|
|
|
|
|
|
|
shutdown:
|
2009-04-05 15:43:25 +02:00
|
|
|
gecko_printf("Shutting down interrupts...\n");
|
2009-01-16 08:51:51 +01:00
|
|
|
irq_shutdown();
|
2009-04-05 15:43:25 +02:00
|
|
|
gecko_printf("Shutting down caches and MMU...\n");
|
2009-01-16 08:53:03 +01:00
|
|
|
mem_shutdown();
|
2009-01-06 00:13:39 +01:00
|
|
|
|
2009-04-10 17:25:13 +02:00
|
|
|
gecko_printf("Vectoring to 0x%08x...\n", vector);
|
2009-01-16 08:51:51 +01:00
|
|
|
return vector;
|
2008-12-28 14:35:37 +01:00
|
|
|
}
|