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
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2021-2022 Aidan MacDonald
*
* 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "x1000bootloader.h"
#include "core_alloc.h"
#include "system.h"
#include "kernel.h"
#include "power.h"
#include "file.h"
#include "linuxboot.h"
#include "boot-x1000.h"
#include <ctype.h>
#include <sys/types.h>
void boot_rockbox(void)
{
size_t length;
int handle = load_rockbox(BOOTFILE, &length);
if(handle < 0)
return;
gui_shutdown();
x1000_boot_rockbox(core_get_data(handle), length);
}
void shutdown(void)
{
splash(HZ, "Shutting down");
power_off();
while(1);
}
void reboot(void)
{
splash(HZ, "Rebooting");
system_reboot();
while(1);
}
/*
* boot_linux() is intended for mainline kernels, and as such it
* should not need any major target-specific modifications.
*/
static int read_linux_args(const char* filename)
{
if(check_disk(false) != DISK_PRESENT)
return -1;
int ret;
size_t max_size;
int handle = core_alloc_maximum("args", &max_size, &buflib_ops_locked);
if(handle <= 0) {
splash(5*HZ, "Out of memory");
return -2;
}
int fd = open(filename, O_RDONLY);
if(fd < 0) {
splash2(5*HZ, "Can't open args file", filename);
ret = -3;
goto err_free;
}
/* this isn't 100% correct but will be good enough */
off_t fsize = filesize(fd);
if(fsize < 0 || fsize+1 > (off_t)max_size) {
splash(5*HZ, "Arguments too long");
ret = -4;
goto err_close;
}
char* buf = core_get_data(handle);
core_shrink(handle, buf, fsize+1);
ssize_t rdres = read(fd, buf, fsize);
close(fd);
if(rdres != (ssize_t)fsize) {
splash(5*HZ, "Can't read args file");
ret = -5;
goto err_free;
}
/* append a null terminator */
char* end = buf + fsize;
*end = 0;
/* change all newlines, etc, to spaces */
for(; buf != end; ++buf)
if(isspace(*buf))
*buf = ' ';
return handle;
err_close:
close(fd);
err_free:
core_free(handle);
return ret;
}
/*
* Provisional linux loading function: kernel is at "/uImage",
* contents of "/linux_cmdline.txt" are used as kernel arguments.
*/
void boot_linux(void)
{
struct uimage_header uh;
size_t img_length;
int handle = load_uimage_file("/uImage", &uh, &img_length);
if(handle < 0)
return;
int args_handle = read_linux_args("/linux_cmdline.txt");
if(args_handle < 0) {
core_free(handle);
return;
}
x1000_boot_linux(core_get_data(handle), img_length,
(void*)uimage_get_load(&uh),
(void*)uimage_get_ep(&uh),
core_get_data(args_handle));
}
/*
* WARNING: Original firmware can be finicky.
* Be careful when modifying this code.
*/
#if defined(FIIO_M3K) || defined(SHANLING_Q1)
uint32_t saved_kernel_entry __attribute__((section(".idata")));
void kernel_thunk(long, long, long, long) __attribute__((section(".icode")));
void kernel_thunk(long a0, long a1, long a2, long a3)
{
/* cache flush */
commit_discard_idcache();
/* now we can jump to the kernel */
typedef void(*entry_fn)(long, long, long, long);
entry_fn fn = (entry_fn)saved_kernel_entry;
fn(a0, a1, a2, a3);
while(1);
}
static void patch_stub_call(void* patch_addr)
{
uint32_t* code = patch_addr;
uint32_t stub_addr = (uint32_t)(void*)kernel_thunk;
/* generate call to stub */
code[0] = 0x3c190000 | (stub_addr >> 16); /* lui t9, stub_hi */
code[1] = 0x37390000 | (stub_addr & 0xffff); /* ori t9, t9, stub_lo */
code[2] = 0x0320f809; /* jalr t9 */
code[3] = 0x00000000; /* nop */
}
#endif
static __attribute__((unused))
void boot_of_helper(uint32_t addr, uint32_t flash_size, const char* args)
{
struct uimage_header uh;
size_t img_length;
int handle = load_uimage_flash(addr, flash_size, &uh, &img_length);
if(handle < 0)
return;
#if defined(FIIO_M3K) || defined(SHANLING_Q1)
/* Fix for targets that use self-extracting kernel images */
void* jump_addr = core_get_data(handle);
uint32_t entry_addr = mips_linux_stub_get_entry(&jump_addr, img_length);
if(entry_addr >= 0xa0000000 || entry_addr < 0x80000000) {
splash2(5*HZ, "Kernel patch failed", "Please send bugreport");
return;
}
saved_kernel_entry = entry_addr;
patch_stub_call(jump_addr);
#endif
gui_shutdown();
x1000_dualboot_load_pdma_fw();
x1000_dualboot_cleanup();
x1000_dualboot_init_clocktree();
x1000_dualboot_init_uart2();
x1000_boot_linux(core_get_data(handle), img_length,
(void*)uimage_get_load(&uh),
(void*)uimage_get_ep(&uh), args);
}
void boot_of_player(void)
{
#if defined(OF_PLAYER_ADDR)
boot_of_helper(OF_PLAYER_ADDR, OF_PLAYER_LENGTH, OF_PLAYER_ARGS);
#else
splash(HZ, "Not supported");
#endif
}
void boot_of_recovery(void)
{
#if defined(OF_RECOVERY_ADDR)
boot_of_helper(OF_RECOVERY_ADDR, OF_RECOVERY_LENGTH, OF_RECOVERY_ARGS);
#else
splash(HZ, "Not supported");
#endif
}
|