2009-04-10 18:15:10 +02:00
|
|
|
/*
|
|
|
|
mini - a Free Software replacement for the Nintendo/BroadOn IOS.
|
|
|
|
|
|
|
|
boot2 chainloader
|
|
|
|
|
2009-04-13 23:38:37 +02:00
|
|
|
Copyright (c) 2008 Nuke - <wiinuke@gmail.com>
|
2009-04-10 18:15:10 +02:00
|
|
|
Copyright (C) 2008, 2009 Hector Martin "marcan" <marcan@marcansoft.com>
|
2009-04-13 22:13:45 +02:00
|
|
|
Copyright (C) 2008, 2009 Sven Peter <svenpeter@gmail.com>
|
|
|
|
Copyright (C) 2009 Andre Heider "dhewg" <dhewg@wiibrew.org>
|
2009-04-10 18:15:10 +02:00
|
|
|
|
|
|
|
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"
|
2009-04-10 11:25:10 +02:00
|
|
|
#include "irq.h"
|
2008-12-28 14:35:37 +01:00
|
|
|
#include "start.h"
|
|
|
|
#include "vsprintf.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "hollywood.h"
|
2009-04-10 11:25:10 +02:00
|
|
|
#include "powerpc.h"
|
|
|
|
#include "powerpc_elf.h"
|
2009-04-05 15:43:25 +02:00
|
|
|
#include "gecko.h"
|
|
|
|
|
|
|
|
static u8 gecko_console_enabled = 0;
|
2008-12-28 14:35:37 +01:00
|
|
|
|
|
|
|
static u32 _gecko_command(u32 command)
|
|
|
|
{
|
|
|
|
u32 i;
|
|
|
|
// Memory Card Port B (Channel 1, Device 0, Frequency 3 (32Mhz Clock))
|
|
|
|
write32(EXI1_CSR, 0xd0);
|
|
|
|
write32(EXI1_DATA, command);
|
|
|
|
write32(EXI1_CR, 0x19);
|
|
|
|
i = 1000;
|
|
|
|
while ((read32(EXI1_CR) & 1) && (i--));
|
|
|
|
i = read32(EXI1_DATA);
|
|
|
|
write32(EXI1_CSR, 0);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2009-04-10 11:25:10 +02:00
|
|
|
static u32 _gecko_sendbyte(u8 sendbyte)
|
2008-12-28 14:35:37 +01:00
|
|
|
{
|
|
|
|
u32 i = 0;
|
|
|
|
i = _gecko_command(0xB0000000 | (sendbyte<<20));
|
|
|
|
if (i&0x04000000)
|
|
|
|
return 1; // Return 1 if byte was sent
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-10 11:25:10 +02:00
|
|
|
static u32 _gecko_recvbyte(u8 *recvbyte)
|
2008-12-28 14:35:37 +01:00
|
|
|
{
|
|
|
|
u32 i = 0;
|
|
|
|
*recvbyte = 0;
|
|
|
|
i = _gecko_command(0xA0000000);
|
|
|
|
if (i&0x08000000) {
|
|
|
|
// Return 1 if byte was received
|
|
|
|
*recvbyte = (i>>16)&0xff;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-10 11:25:10 +02:00
|
|
|
#if 0
|
2008-12-28 14:35:37 +01:00
|
|
|
static u32 _gecko_checksend(void)
|
|
|
|
{
|
|
|
|
u32 i = 0;
|
|
|
|
i = _gecko_command(0xC0000000);
|
|
|
|
if (i&0x04000000)
|
|
|
|
return 1; // Return 1 if safe to send
|
|
|
|
return 0;
|
|
|
|
}
|
2009-04-10 11:25:10 +02:00
|
|
|
#endif
|
2008-12-28 14:35:37 +01:00
|
|
|
|
|
|
|
static u32 _gecko_checkrecv(void)
|
|
|
|
{
|
|
|
|
u32 i = 0;
|
|
|
|
i = _gecko_command(0xD0000000);
|
|
|
|
if (i&0x04000000)
|
|
|
|
return 1; // Return 1 if safe to recv
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-05 15:43:25 +02:00
|
|
|
static int gecko_isalive(void)
|
2008-12-28 14:35:37 +01:00
|
|
|
{
|
|
|
|
u32 i = 0;
|
|
|
|
i = _gecko_command(0x90000000);
|
|
|
|
if (i&0x04700000)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-05 15:43:25 +02:00
|
|
|
static void gecko_flush(void)
|
|
|
|
{
|
2009-04-13 20:22:32 +02:00
|
|
|
u8 tmp;
|
2009-04-05 15:43:25 +02:00
|
|
|
while(_gecko_recvbyte(&tmp));
|
|
|
|
}
|
|
|
|
|
2009-04-13 20:22:32 +02:00
|
|
|
#if 0
|
2009-04-05 15:43:25 +02:00
|
|
|
static int gecko_recvbuffer(void *buffer, u32 size)
|
2008-12-28 14:35:37 +01:00
|
|
|
{
|
|
|
|
u32 left = size;
|
|
|
|
char *ptr = (char*)buffer;
|
|
|
|
|
|
|
|
while(left>0) {
|
|
|
|
if(!_gecko_recvbyte(ptr))
|
|
|
|
break;
|
|
|
|
ptr++;
|
|
|
|
left--;
|
|
|
|
}
|
|
|
|
return (size - left);
|
|
|
|
}
|
2009-04-05 15:43:25 +02:00
|
|
|
#endif
|
2008-12-28 14:35:37 +01:00
|
|
|
|
2009-04-05 15:43:25 +02:00
|
|
|
static int gecko_sendbuffer(const void *buffer, u32 size)
|
2008-12-28 14:35:37 +01:00
|
|
|
{
|
|
|
|
u32 left = size;
|
|
|
|
char *ptr = (char*)buffer;
|
|
|
|
|
|
|
|
while(left>0) {
|
|
|
|
if(!_gecko_sendbyte(*ptr))
|
|
|
|
break;
|
|
|
|
ptr++;
|
|
|
|
left--;
|
|
|
|
}
|
|
|
|
return (size - left);
|
|
|
|
}
|
|
|
|
|
2009-04-05 15:43:25 +02:00
|
|
|
#if 0
|
|
|
|
static int gecko_recvbuffer_safe(void *buffer, u32 size)
|
2008-12-28 14:35:37 +01:00
|
|
|
{
|
|
|
|
u32 left = size;
|
|
|
|
char *ptr = (char*)buffer;
|
|
|
|
|
|
|
|
while(left>0) {
|
|
|
|
if(_gecko_checkrecv()) {
|
|
|
|
if(!_gecko_recvbyte(ptr))
|
|
|
|
break;
|
|
|
|
ptr++;
|
|
|
|
left--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (size - left);
|
|
|
|
}
|
|
|
|
|
2009-04-05 15:43:25 +02:00
|
|
|
static int gecko_sendbuffer_safe(const void *buffer, u32 size)
|
2008-12-28 14:35:37 +01:00
|
|
|
{
|
|
|
|
u32 left = size;
|
|
|
|
char *ptr = (char*)buffer;
|
|
|
|
|
|
|
|
if((read32(HW_EXICTRL) & EXICTRL_ENABLE_EXI) == 0)
|
|
|
|
return left;
|
|
|
|
|
|
|
|
while(left>0) {
|
|
|
|
if(_gecko_checksend()) {
|
|
|
|
if(!_gecko_sendbyte(*ptr))
|
|
|
|
break;
|
|
|
|
ptr++;
|
|
|
|
left--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (size - left);
|
|
|
|
}
|
2009-04-05 15:43:25 +02:00
|
|
|
#endif
|
2008-12-28 14:35:37 +01:00
|
|
|
|
2009-04-05 15:43:25 +02:00
|
|
|
void gecko_init(void)
|
2008-12-28 14:35:37 +01:00
|
|
|
{
|
2009-04-05 15:43:25 +02:00
|
|
|
write32(EXI0_CSR, 0);
|
|
|
|
write32(EXI1_CSR, 0);
|
|
|
|
write32(EXI2_CSR, 0);
|
|
|
|
write32(EXI0_CSR, 0x2000);
|
|
|
|
write32(EXI0_CSR, 3<<10);
|
|
|
|
write32(EXI1_CSR, 3<<10);
|
2008-12-28 14:35:37 +01:00
|
|
|
|
2009-04-05 15:43:25 +02:00
|
|
|
if (!gecko_isalive())
|
|
|
|
return;
|
|
|
|
|
2009-04-13 20:22:32 +02:00
|
|
|
gecko_flush();
|
2009-04-05 15:43:25 +02:00
|
|
|
gecko_console_enabled = 1;
|
2008-12-28 14:35:37 +01:00
|
|
|
}
|
|
|
|
|
2009-04-05 15:43:25 +02:00
|
|
|
u8 gecko_enable_console(const u8 enable)
|
2008-12-28 14:35:37 +01:00
|
|
|
{
|
2009-04-05 15:43:25 +02:00
|
|
|
if (enable) {
|
|
|
|
if (gecko_isalive())
|
|
|
|
gecko_console_enabled = 1;
|
|
|
|
} else
|
|
|
|
gecko_console_enabled = 0;
|
|
|
|
|
|
|
|
return gecko_console_enabled;
|
2008-12-28 14:35:37 +01:00
|
|
|
}
|
|
|
|
|
2009-04-15 21:35:47 +02:00
|
|
|
#ifndef NDEBUG
|
2009-04-05 15:43:25 +02:00
|
|
|
int gecko_printf(const char *fmt, ...)
|
2008-12-28 14:35:37 +01:00
|
|
|
{
|
2009-04-05 15:43:25 +02:00
|
|
|
if (!gecko_console_enabled)
|
|
|
|
return 0;
|
|
|
|
|
2008-12-28 14:35:37 +01:00
|
|
|
va_list args;
|
2009-03-26 05:15:20 +01:00
|
|
|
char buffer[256];
|
2008-12-28 14:35:37 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
i = vsprintf(buffer, fmt, args);
|
|
|
|
va_end(args);
|
2009-04-05 15:43:25 +02:00
|
|
|
|
|
|
|
return gecko_sendbuffer(buffer, i);
|
2008-12-28 14:35:37 +01:00
|
|
|
}
|
2009-04-15 21:35:47 +02:00
|
|
|
#endif
|
2009-04-10 11:25:10 +02:00
|
|
|
|
|
|
|
// irq context
|
|
|
|
|
|
|
|
#define GECKO_STATE_NONE 0
|
2009-04-10 17:25:13 +02:00
|
|
|
#define GECKO_STATE_RECEIVE_BUFFER_SIZE 1
|
|
|
|
#define GECKO_STATE_RECEIVE_BUFFER 2
|
2009-04-10 11:25:10 +02:00
|
|
|
|
|
|
|
static u32 _gecko_cmd = 0;
|
|
|
|
static u32 _gecko_cmd_start_time = 0;
|
|
|
|
static u32 _gecko_state = GECKO_STATE_NONE;
|
|
|
|
static u32 _gecko_receive_left = 0;
|
|
|
|
static u32 _gecko_receive_len = 0;
|
|
|
|
static u8 *_gecko_receive_buffer = NULL;
|
|
|
|
|
|
|
|
void gecko_timer_initialize(void)
|
|
|
|
{
|
|
|
|
if (!gecko_isalive())
|
|
|
|
return;
|
|
|
|
|
2009-04-13 16:34:44 +02:00
|
|
|
irq_set_alarm(20, 1);
|
2009-04-10 11:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void gecko_timer(void) {
|
|
|
|
u8 b;
|
|
|
|
|
|
|
|
if (_gecko_cmd_start_time && read32(HW_TIMER) >
|
2009-04-13 16:34:44 +02:00
|
|
|
(_gecko_cmd_start_time + IRQ_ALARM_MS2REG(5000)))
|
|
|
|
goto cleanup;
|
2009-04-10 11:25:10 +02:00
|
|
|
|
|
|
|
switch (_gecko_state) {
|
|
|
|
case GECKO_STATE_NONE:
|
|
|
|
if (!_gecko_checkrecv() || !_gecko_recvbyte(&b))
|
|
|
|
return;
|
|
|
|
|
|
|
|
_gecko_cmd <<= 8;
|
|
|
|
_gecko_cmd |= b;
|
|
|
|
|
|
|
|
switch (_gecko_cmd) {
|
|
|
|
// upload powerpc ELF
|
|
|
|
case 0x43524150:
|
2009-04-10 17:25:13 +02:00
|
|
|
_gecko_state = GECKO_STATE_RECEIVE_BUFFER_SIZE;
|
2009-04-10 11:25:10 +02:00
|
|
|
_gecko_receive_len = 0;
|
|
|
|
_gecko_receive_left = 4;
|
2009-04-10 17:25:13 +02:00
|
|
|
_gecko_receive_buffer = (u8 *) 0x10100000;
|
2009-04-10 11:25:10 +02:00
|
|
|
|
|
|
|
irq_set_alarm(1, 0);
|
|
|
|
_gecko_cmd_start_time = read32(HW_TIMER);
|
|
|
|
|
2009-04-10 17:25:13 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
// upload ARM ELF
|
|
|
|
case 0x5a4f4d47:
|
|
|
|
_gecko_state = GECKO_STATE_RECEIVE_BUFFER_SIZE;
|
|
|
|
_gecko_receive_len = 0;
|
|
|
|
_gecko_receive_left = 4;
|
|
|
|
_gecko_receive_buffer = (u8 *) 0x0; // yarly
|
|
|
|
|
|
|
|
irq_set_alarm(1, 0);
|
|
|
|
_gecko_cmd_start_time = read32(HW_TIMER);
|
2009-04-10 11:25:10 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
2009-04-10 17:25:13 +02:00
|
|
|
case GECKO_STATE_RECEIVE_BUFFER_SIZE:
|
2009-04-10 11:25:10 +02:00
|
|
|
if (!_gecko_checkrecv() || !_gecko_recvbyte(&b))
|
|
|
|
return;
|
|
|
|
|
|
|
|
_gecko_receive_len <<= 8;
|
|
|
|
_gecko_receive_len |= b;
|
|
|
|
_gecko_receive_left--;
|
|
|
|
|
|
|
|
if (!_gecko_receive_left) {
|
2009-04-10 17:25:13 +02:00
|
|
|
_gecko_state = GECKO_STATE_RECEIVE_BUFFER;
|
2009-04-10 11:25:10 +02:00
|
|
|
_gecko_receive_left = _gecko_receive_len;
|
|
|
|
|
2009-04-13 20:22:32 +02:00
|
|
|
// sorry pal, that memory is mine now
|
2009-04-10 11:25:10 +02:00
|
|
|
powerpc_hang();
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
2009-04-10 17:25:13 +02:00
|
|
|
case GECKO_STATE_RECEIVE_BUFFER:
|
2009-04-10 11:25:10 +02:00
|
|
|
while (_gecko_receive_left) {
|
|
|
|
if (!_gecko_checkrecv() || !_gecko_recvbyte(_gecko_receive_buffer))
|
|
|
|
return;
|
|
|
|
|
|
|
|
_gecko_receive_buffer++;
|
|
|
|
_gecko_receive_left--;
|
|
|
|
}
|
|
|
|
|
2009-04-10 17:25:13 +02:00
|
|
|
if (!_gecko_receive_left)
|
|
|
|
break;
|
2009-04-10 11:25:10 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
2009-04-13 20:22:32 +02:00
|
|
|
gecko_printf("MINI/GECKO: internal error\n");
|
2009-04-10 17:25:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// done receiving, handle the command
|
|
|
|
switch (_gecko_cmd) {
|
|
|
|
case 0x43524150:
|
2009-04-15 20:23:07 +02:00
|
|
|
ipc_enqueue_slow(IPC_DEV_PPC, IPC_PPC_BOOT, 3,
|
|
|
|
1, (u32) 0x10100000, _gecko_receive_len);
|
2009-04-10 17:25:13 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x5a4f4d47:
|
|
|
|
// skip headerlen, which is stored at u32[0]
|
2009-04-15 20:23:07 +02:00
|
|
|
ipc_enqueue_slow(IPC_DEV_SYS, IPC_SYS_JUMP, 1, ((u32 *) 0x0)[0]);
|
2009-04-10 11:25:10 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-04-10 17:25:13 +02:00
|
|
|
|
2009-04-13 16:34:44 +02:00
|
|
|
cleanup:
|
2009-04-13 20:22:32 +02:00
|
|
|
gecko_flush();
|
|
|
|
|
2009-04-13 16:34:44 +02:00
|
|
|
irq_set_alarm(20, 0);
|
2009-04-10 17:25:13 +02:00
|
|
|
|
|
|
|
_gecko_cmd = 0;
|
|
|
|
_gecko_cmd_start_time = 0;
|
|
|
|
_gecko_state = GECKO_STATE_NONE;
|
2009-04-10 11:25:10 +02:00
|
|
|
}
|
|
|
|
|