-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
457 lines (364 loc) · 10.1 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
* Wii U Linux Launcher
*
* Copyright (C) 2017 Jonathan Neuschäfer <[email protected]>
*
* 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, in the file LICENSE.GPLv2.
*/
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <common/common.h>
#include <os_functions.h>
#include <vpad_functions.h>
#include <fs_functions.h>
#include "keyboard.h"
#include "main.h"
#include "fs.h"
#include "settings.h"
#include "version.h"
#include "hax.h"
/* A physically contiguous memory buffer that contains a small header, the
* kernel, the dtb, and the initrd. Allocated from the end of MEM1. */
static void *contiguous_buffer = NULL;
static char *current_text = NULL;
/* A warning or error message */
char warning[1024];
static int selection = 0;
static struct keyboard keyboard;
static int keyboard_shown = 0;
static int iosuhax = -1;
/* Pointers to the raw framebuffers. [0] is TV, [1] is DRC. */
uint32_t *framebuffers[2];
void *xmalloc(size_t size, size_t alignment)
{
void *(* MEMAllocFromDefaultHeapEx)(int size, int alignment) =
(void *) *pMEMAllocFromDefaultHeapEx;
void *ptr = MEMAllocFromDefaultHeapEx(size, alignment);
if (!ptr)
OSFatal("MEMAllocFromDefaultHeapEx failed");
return ptr;
}
void xfree(void *ptr)
{
void (* MEMFreeToDefaultHeap)(void *addr) = (void *) *pMEMFreeToDefaultHeap;
if (ptr)
MEMFreeToDefaultHeap(ptr);
}
static void enter_keyboard(char *buffer)
{
current_text = buffer;
keyboard_shown = 1;
}
static void exit_keyboard(void)
{
if (keyboard_shown) {
current_text = NULL;
save_settings();
keyboard_shown = 0;
}
}
/* Print some text to both screens */
static void OSScreenPutFontBoth(uint32_t posX, uint32_t posY, const char *str) {
OSScreenPutFontEx(0, posX, posY, str);
OSScreenPutFontEx(1, posX, posY, str);
}
static void OSScreenFlipBuffersBoth(void) {
OSScreenFlipBuffersEx(0);
OSScreenFlipBuffersEx(1);
}
static void OSScreenClearBufferBoth(uint32_t color) {
OSScreenClearBufferEx(0, color);
OSScreenClearBufferEx(1, color);
}
/* Draw the last line of the screen. NOTE: It's at different line number on the
* gamepad and on the TV. */
static void draw_status_line(void)
{
char buf[32];
/* Draw the program version (git commit) in the lower left corner */
snprintf(buf, sizeof buf, "Git: %s", program_version);
OSScreenPutFontEx(0, 0, 27, buf);
OSScreenPutFontEx(1, 0, 17, buf);
/* Draw the firmware version in the lower right corner */
snprintf(buf, sizeof buf, "OS_FIRMWARE: %d", OS_FIRMWARE);
OSScreenPutFontEx(0, 85, 27, buf);
OSScreenPutFontEx(1, 49, 17, buf);
}
/*
* Wii U Linux Launcher
*
* kernel : /vol/external01/linux/image
* dtb : /vol/external01/linux/wiiu.dtb
* initrd : /vol/external01/linux/initrd
* cmdline : root=/dev/mmcblk0p1
* load it!
*
*
* Opening kernel failed: file not found (-6)
*
*
* Git: abcd12345678 OS_FIRMWARE: 550
*/
void draw_gui(void)
{
char line[128];
int y;
OSScreenClearBufferBoth(0x488cd100); /* A nice blue background */
OSScreenPutFontEx(0, 39, 0, "Wii U Linux Launcher");
OSScreenPutFontEx(1, 21, 0, "Wii U Linux Launcher");
y = 2;
OSScreenPrintf(2, y++, line, "kernel : %s", kernel_path);
OSScreenPrintf(2, y++, line, "dtb : %s", dtb_path);
OSScreenPrintf(2, y++, line, "initrd : %s", initrd_path);
OSScreenPrintf(2, y++, line, "cmdline : %s", cmdline);
OSScreenPutFontBoth(2, y++, (contiguous_buffer == NULL)?
"load it!" : "load it! (press start to boot)");
/* What's currently selected for editing? */
OSScreenPutFontBoth(0, 2 + selection, "> ");
/* Show a little cursor, if we're editing a line*/
if (keyboard_shown) {
int len = strlen(current_text);
OSScreenPutFontBoth(12 + len, 2 + selection, "_");
}
OSScreenPutFontBoth(0, 9, warning);
if (keyboard_shown) {
keyboard_draw(&keyboard);
}
draw_status_line();
OSScreenFlipBuffersBoth();
}
extern uint8_t purgatory[];
extern uint8_t purgatory_end[];
struct purgatory_header {
uint32_t jmp; /* A jump instruction, to skip the header */
uint32_t size; /* The size of the whole thing */
uint32_t dtb_phys; /* physical address of the devicetree blob */
uint32_t kern_phys; /* physical address of the kernel */
};
/* Get a chunk of MEM1 */
static void *get_mem1_chunk(size_t size)
{
/* Perform alignment (I *think* this correct) */
size = -((-size) & (~0xfff));
/* Skip the framebuffers */
if (size > 0x02000000 - OSScreenGetBufferSizeEx(0) - OSScreenGetBufferSizeEx(1)) {
warnf("ERROR: Can't allocate %#x bytes from MEM1", size);
return NULL;
}
return (void *) (0xf4000000 + 0x02000000 - size);
}
/* https://www.kernel.org/doc/Documentation/devicetree/booting-without-of.txt */
static int load_stuff(void)
{
size_t purgatory_size = purgatory_end - purgatory;
size_t total_size;
uint8_t *buffer;
int res;
contiguous_buffer = NULL;
if (kernel_path[0] == '\0') {
warn("You need to specify a kernel!");
return 0;
}
size_t kernel_size = get_file_size(kernel_path, "kernel");
/* TODO: determine the size of initrd and dtb */
total_size = purgatory_size + kernel_size;
buffer = get_mem1_chunk(total_size);
if (!buffer)
return -1;
struct purgatory_header *header = (void *)buffer;
memcpy(header, purgatory, purgatory_size);
/* TODO: fill the header with all necessary information */
size_t kernel_offset = purgatory_size;
res = read_file_into_buffer(kernel_path,
(u8 *)buffer + kernel_offset, kernel_size, "kernel");
if (res < 0)
return res;
/* TODO: patch initrd and cmdline into dtb */
/* Let other functions see that we've loaded stuff */
contiguous_buffer = buffer;
return 0;
}
/* ARM code \o/ */
#include "arm/arm.xxd"
/* Make sure that the given framebuffer is using the first half as foreground. */
static void set_framebuffer_foreground(int fb)
{
OSScreenPutPixelEx(fb, 0, 0, 0xa0a0a0a0);
OSScreenFlipBuffersEx(fb);
OSScreenPutPixelEx(fb, 0, 0, 0xb0b0b0b0);
switch(*framebuffers[fb]) {
case 0xa0a0a0a0:
/* do nothing */
break;
case 0xb0b0b0b0:
OSScreenFlipBuffersEx(fb);
break;
default:
OSFatal("set_framebuffer_foreground read a wrong color");
}
}
static void boot(void)
{
const uint32_t arm_code = 0xfffff000;
if (!contiguous_buffer)
return;
iosuhax = iosuhax_open();
if (iosuhax < 0)
return;
if (arm_bin_len > -arm_code) {
warnf("Error: The ARM binary is too big (%#x)", arm_bin_len);
return;
}
void *ancast_addr = (void *)0xf5000000;
void *ppcboot_addr = (void *)0xf4000000;
memcpy(ppcboot_addr, purgatory, 0x38);
/* TODO: load the ancast image directly from the NAND filesystem */
int ret = read_file_into_buffer(
"/vol/external01/wiiu/apps/linux/ancast.img",
ancast_addr, 2 << 20, "ancast image");
if (ret < 0)
return;
/* Flush (part of) the cache to ensure that the ancast image hits RAM
* before we shutdown the PPC (using svc 0x53) */
DCFlushRange(ancast_addr, ret);
warn("loading ARM code into MEM1...");
draw_gui();
iosuhax_kern_write_buf(iosuhax, arm_code, arm_bin, arm_bin_len);
iosuhax_kern_write32(iosuhax, arm_code + 4,
(uint32_t)OSEffectiveToPhysical(ancast_addr));
iosuhax_kern_write32(iosuhax, arm_code + 8,
(uint32_t)OSEffectiveToPhysical(ppcboot_addr));
warn("booting...");
/* Draw the GUI twice to make sure both the foreground
buffer and the background buffer contain the current state */
draw_gui();
draw_gui();
set_framebuffer_foreground(0);
set_framebuffer_foreground(1);
iosuhax_svc_0x53(iosuhax, arm_code);
}
static void action(int what)
{
warning[0] = '\0';
switch (what) {
case 0:
enter_keyboard(kernel_path);
break;
case 1:
enter_keyboard(dtb_path);
break;
case 2:
enter_keyboard(initrd_path);
break;
case 3:
enter_keyboard(cmdline);
break;
case 4:
load_stuff();
break;
}
}
static void append_char(char c)
{
size_t len = strlen(current_text);
if (len < sizeof(kernel_path) - 1) {
current_text[len] = c;
current_text[len + 1] = '\0';
}
}
static void remove_char()
{
size_t len = strlen(current_text);
if (len)
current_text[len - 1] = '\0';
}
static void keyboard_cb(struct keyboard *keyb, int ch)
{
switch (ch) {
case KEYB_BACKSPACE:
remove_char();
break;
case '\n':
exit_keyboard();
break;
default:
append_char(ch);
break;
}
}
static void handle_vpad(const VPADData *vpad)
{
if (vpad->btns_d & VPAD_BUTTON_DOWN) {
exit_keyboard();
selection++;
}
if (vpad->btns_d & VPAD_BUTTON_UP) {
exit_keyboard();
selection--;
}
if (keyboard_shown) {
/* Inform the keyboard about the input event, but ignore it otherwise */
keyboard_handle_vpad(&keyboard, vpad, keyboard_cb);
return;
}
if (vpad->btns_d & VPAD_BUTTON_A)
action(selection);
if (vpad->btns_d & VPAD_BUTTON_PLUS)
boot();
/* some normalization... */
if (selection < 0) selection = 0;
if (selection > 4) selection = 4;
}
static void init_screens(void)
{
OSScreenInit();
framebuffers[0] = (void *)0xf4000000;
framebuffers[1] = (void *)(0xf4000000 + OSScreenGetBufferSizeEx(0));
OSScreenSetBufferEx(0, framebuffers[0]);
OSScreenSetBufferEx(1, framebuffers[1]);
OSScreenEnableEx(0, 1);
OSScreenEnableEx(1, 1);
OSScreenClearBufferBoth(0x00000000);
OSScreenFlipBuffersBoth();
}
int main(void)
{
InitOSFunctionPointers();
InitVPadFunctionPointers();
InitFSFunctionPointers();
init_screens();
keyboard_init(&keyboard, 0, 10);
fs_init();
load_settings();
uint32_t color = 0, i;
for (i = 0; i < 8; i++) {
OSScreenClearBufferBoth(color);
color += 0x11223344;
OSScreenFlipBuffersBoth();
os_usleep(100000);
}
for (;;) {
VPADData vpad;
s32 err;
/* Read input events from the gamepad */
VPADRead(0, &vpad, 1, &err);
if (vpad.btns_h & VPAD_BUTTON_HOME)
break;
handle_vpad(&vpad);
draw_gui();
os_usleep(1000000 / 50);
}
fs_deinit();
return 0;
}