Front SD ELF loader added
[svpe-wii.git] / sdelfloader / loader / source / main.c
blob6b1e595976e2062dd47f7b2d26600d5ed92101af
1 /*
2 * Copyright (C) 2008 svpe, #wiidev at efnet
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <malloc.h>
21 #include <ogcsys.h>
22 #include <gccore.h>
23 #include <ogc/ios.h>
25 #define FILES_PER_PAGE 24
27 #include "sdio.h"
28 #include "tff.h"
29 #include "elf.h"
30 #include "dol.h"
32 #include "asm.h"
33 #include "processor.h"
35 static void *xfb = NULL;
36 static GXRModeObj *rmode = NULL;
38 extern void __exception_closeall();
39 extern s32 __IOS_ShutdownSubsystems();
41 #define USB_ALIGN __attribute__((aligned(32)))
42 static const char *usb_path USB_ALIGN = "/dev/usb/oh1/57e/305";
43 char bluetoothResetData1[] USB_ALIGN = {
44 /* bmRequestType */
45 0x20
48 char bluetoothResetData2[] USB_ALIGN = {
49 /* bmRequest */
50 0x00
53 char bluetoothResetData3[] USB_ALIGN = {
54 /* wValue */
55 0x00, 0x00
58 char bluetoothResetData4[] USB_ALIGN = {
59 /* wIndex */
60 0x00, 0x00
63 char bluetoothResetData5[] USB_ALIGN = {
64 /* wLength */
65 0x03, 0x00
68 char bluetoothResetData6[] USB_ALIGN = {
69 /* unknown; set to zero. */
70 0x00
73 char bluetoothResetData7[] USB_ALIGN = {
74 /* Mesage payload. */
75 0x03, 0x0c, 0x00
78 /** Vectors of data transfered. */
79 ioctlv bluetoothReset[] USB_ALIGN = {
81 bluetoothResetData1,
82 sizeof(bluetoothResetData1)
85 bluetoothResetData2,
86 sizeof(bluetoothResetData2)
89 bluetoothResetData3,
90 sizeof(bluetoothResetData3)
93 bluetoothResetData4,
94 sizeof(bluetoothResetData4)
97 bluetoothResetData5,
98 sizeof(bluetoothResetData5)
101 bluetoothResetData6,
102 sizeof(bluetoothResetData6)
105 bluetoothResetData7,
106 sizeof(bluetoothResetData7)
112 int main(int argc, char **argv) {
114 FATFS ffs;
115 DIR elfdir;
116 FILINFO finfo;
118 char filename[4+13+1];
120 char filelist[999][13]; // FIXME: use a dynamic filelist here
121 u32 files;
123 FIL fp;
124 WORD bytes_read;
125 u32 bytes_read_total;
126 u8 *data = (u8 *)0x92000000;
128 u32 offset;
129 u32 selection;
130 u32 i;
131 u32 level;
132 u32 buttons;
134 s32 res;
136 s32 usb_fd = -1;
138 void (*ep)();
141 VIDEO_Init();
142 PAD_Init();
143 AUDIO_Init (NULL);
144 DSP_Init ();
145 AUDIO_StopDMA();
146 AUDIO_RegisterDMACallback(NULL);
148 switch(VIDEO_GetCurrentTvMode()) {
149 case VI_NTSC:
150 rmode = &TVNtsc480IntDf;
151 break;
152 case VI_PAL:
153 rmode = &TVPal528IntDf;
154 break;
155 case VI_MPAL:
156 rmode = &TVMpal480IntDf;
157 break;
158 default:
159 rmode = &TVNtsc480IntDf;
160 break;
163 xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
164 console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
166 VIDEO_Configure(rmode);
167 VIDEO_SetNextFramebuffer(xfb);
168 VIDEO_SetBlack(FALSE);
169 VIDEO_Flush();
170 VIDEO_WaitVSync();
171 if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
174 printf("\n\n");
175 printf("Front SD ELF Loader\n");
176 printf("(c) 2008 Sven Peter (svpe, #wiidev at blitzed/efnet)\n");
177 printf("Internal SD stuff by bushing, marcan and maybe some more ppl i don't know about.\n");
178 printf("TinyFAT FS by elm-chan.org\n");
179 printf("Some ELF loader code by dhewg and tmbinc\n");
181 redo_top:
182 printf("Press A to continue\n");
183 while(1)
185 VIDEO_WaitVSync();
186 PAD_ScanPads();
187 if(PAD_ButtonsDown(0) & PAD_BUTTON_A)
188 break;
191 // shutdown wiimote
192 printf("Shutting down wiimote\n");
193 // printf("Open Bluetooth Dongle\n");
194 usb_fd = IOS_Open(usb_path, 2 /* 2 = write, 1 = read */);
195 printf("fd = %d\n", usb_fd);
197 // printf("Closing connection to blutooth\n");
198 res = IOS_Ioctlv(usb_fd, 0, 6, 1, bluetoothReset);
199 // printf("ret = %d\n", res);
201 IOS_Close(usb_fd);
204 files = 0;
205 if(f_mount(0, &ffs) != FR_OK)
207 printf("f_mount() failed. No FAT filesystem on the SD card?\n");
208 goto redo_top;
211 res = f_opendir(&elfdir, "elf");
212 if(res != FR_OK)
214 printf("f_opendir() failed with %d. Does elf/ exist?\n", res);
215 goto redo_top;
218 printf("Loading filelist...\n");
219 files = 0;
220 memset(&finfo, 0, sizeof(finfo));
221 f_readdir(&elfdir, &finfo);
222 while(strlen(finfo.fname) != 0)
224 if(!(finfo.fattrib & AM_DIR))
226 printf("Adding %s\n", finfo.fname);
227 memcpy(filelist[files], finfo.fname, 13);
228 files++;
230 f_readdir(&elfdir, &finfo);
233 goto while_loop;
235 redo:
237 printf("Press A..\n");
238 while(1)
240 VIDEO_WaitVSync();
241 PAD_ScanPads();
242 if(PAD_ButtonsDown(0) & PAD_BUTTON_A)
243 break;
246 while_loop:
247 offset = selection = 0;
248 while(1)
250 printf("\x1b[2J\n");
252 for(i = offset; i < (offset + FILES_PER_PAGE) && i < files; i++)
254 if(selection == i)
255 printf(">>%s\n", filelist[i]);
256 else
257 printf("%s\n", filelist[i]);
259 printf("\n\n");
261 while(1)
263 PAD_ScanPads();
264 buttons = PAD_ButtonsDown(0);
265 if(buttons & PAD_BUTTON_A)
267 snprintf(filename, 4+13, "elf/%s", filelist[selection]);
268 if(f_stat(filename, &finfo) != FR_OK)
270 printf("error: f_stat() failed :/\n");
271 goto redo;
273 if(f_open(&fp, filename, FA_READ) != FR_OK)
275 printf("error: f_open() failed\n");
276 goto redo;
278 printf("Reading %u bytes\n", (unsigned int)finfo.fsize);
279 bytes_read = bytes_read_total = 0;
280 while(bytes_read_total < finfo.fsize)
282 if(f_read(&fp, data + bytes_read_total, 0x200, &bytes_read) != FR_OK)
284 printf("error: f_read failed.\n");
285 goto redo;
287 if(bytes_read == 0)
288 break;
289 bytes_read_total += bytes_read;
290 // printf("%d/%d bytes read (%d this time)\n", bytes_read_total, (unsigned int)finfo.fsize, bytes_read);
292 if(bytes_read_total < finfo.fsize)
294 printf("error: read %u of %u bytes.\n", bytes_read_total, (unsigned int)finfo.fsize);
295 goto redo;
297 res = valid_elf_image(data);
298 if(res == 1)
300 printf("ELF image found.\n");
301 ep = (void(*)())load_elf_image(data);
303 else
305 printf("Assuming that this is a DOL file. Your Wii will crash if it isn't...\n");
306 ep = (void(*)())load_dol_image(data);
308 f_close(&fp);
309 sd_deinit();
311 printf("entry point: 0x%X\n", (unsigned int)ep);
312 VIDEO_WaitVSync();
314 // code from geckoloader
315 __IOS_ShutdownSubsystems ();
316 printf("IOS_ShutdownSubsystems() done\n");
317 _CPU_ISR_Disable (level);
318 printf("_CPU_ISR_Disable() done\n");
319 __exception_closeall ();
320 printf("__exception_closeall() done. Jumping to ep now...\n");
321 ep();
322 _CPU_ISR_Restore (level);
324 printf("binaries really shouldn't return here...\n");
325 while(1)
326 VIDEO_WaitVSync();
328 else if(buttons & PAD_BUTTON_DOWN)
330 if(selection+1 < files)
331 selection++;
332 if(selection >= offset + FILES_PER_PAGE)
333 offset += FILES_PER_PAGE;
334 break;
336 else if(buttons & PAD_BUTTON_UP)
338 if(selection > 0)
339 selection--;
340 if(selection < offset)
342 if(FILES_PER_PAGE > offset)
343 offset = 0;
344 else
345 offset -= FILES_PER_PAGE;
347 break;
350 VIDEO_WaitVSync();
353 printf("this should not happen\n");
354 while(1)
355 VIDEO_WaitVSync();
356 return 0;