forked from iPodLinux/ipodloader2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.c
299 lines (263 loc) · 9.57 KB
/
config.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
#include "bootloader.h"
#include "ipodhw.h"
#include "minilibc.h"
#include "console.h"
#include "menu.h"
#include "vfs.h"
#include "fb.h"
#include "config.h"
#define DEBUGPRINT(x) { mlc_printf (x); mlc_delay_ms (2000); }
static config_t config;
static const char * find_somewhere (const char **names, const char *what, int *fdOut)
{
int fd = -1;
#if DEBUG
mlc_printf (">> Looking for a %s...\n", what);
#endif
for (; *names; names++) {
#if DEBUG
mlc_printf (">> Trying |%s|...\n", *names);
#endif
fd = vfs_open ((char *)*names);
if (fd >= 0) break;
}
#if DEBUG
if (*names) {
mlc_printf (">> Found it at %s.\n", *names);
} else {
mlc_printf (">>! Not found. :-(\n");
}
#endif
if (fdOut) *fdOut = fd;
return *names;
}
const char *confnames[] = {
// the following are for the music partition:
"(hd0,1)/ipodloader.conf",
"(hd0,1)/Notes/ipodloader.conf",
"(hd0,1)/boot/ipodloader.conf",
"(hd0,1)/loader.cfg",
"(hd0,1)/Notes/loader.cfg",
"(hd0,1)/boot/loader.cfg",
// now, more of the same for people who haven't figured out file renaming
"(hd0,1)/ipodloader.conf.txt",
"(hd0,1)/Notes/ipodloader.conf.txt",
"(hd0,1)/boot/ipodloader.conf.txt",
"(hd0,1)/loader.cfg.txt",
"(hd0,1)/Notes/loader.cfg.txt",
"(hd0,1)/boot/loader.cfg.txt",
// and some plain ones
"(hd0,1)/ipodloader.txt",
"(hd0,1)/Notes/ipodloader.txt",
"(hd0,1)/boot/ipodloader.txt",
"(hd0,1)/loader.txt",
"(hd0,1)/Notes/loader.txt",
"(hd0,1)/boot/loader.txt",
// the following are for the ext2 partition (WinPods only):
"(hd0,2)/ipodloader.conf",
"(hd0,2)/boot/ipodloader.conf",
"(hd0,2)/loader.cfg",
"(hd0,2)/boot/loader.cfg",
// and again...
"(hd0,2)/ipodloader.conf.txt",
"(hd0,2)/boot/ipodloader.conf.txt",
"(hd0,2)/loader.cfg.txt",
"(hd0,2)/boot/loader.cfg.txt",
// and againer...
"(hd0,2)/ipodloader.txt",
"(hd0,2)/Notes/ipodloader.txt",
"(hd0,2)/loader.txt",
"(hd0,2)/Notes/loader.txt",
// we can also read from the firmware partition:
"(hd0,0)/lcnf",
0 };
const char *kernnames[] = {
// the following are for the music partition:
"(hd0,1)/kernel.bin",
"(hd0,1)/Notes/kernel.bin",
"(hd0,1)/boot/kernel.bin",
"(hd0,1)/linux.bin",
"(hd0,1)/Notes/linux.bin",
"(hd0,1)/boot/linux.bin",
"(hd0,1)/vmlinux",
"(hd0,1)/Notes/vmlinux",
"(hd0,1)/boot/vmlinux",
// the following are for the ext2 partition (WinPods only):
"(hd0,2)/kernel.bin",
"(hd0,2)/boot/kernel.bin",
"(hd0,2)/linux.bin",
"(hd0,2)/boot/linux.bin",
"(hd0,2)/vmlinux",
"(hd0,2)/boot/vmlinux",
// we can also read the kernel from the firmware partition:
"(hd0,0)/linx",
0 };
const char *tunenames[] = {
"(hd0,1)/boot.pzm",
"(hd0,1)/Notes/boot.pzm",
"(hd0,1)/boot/boot.pzm",
"(hd0,1)/boot_tune.pzm",
"(hd0,1)/Notes/boot_tune.pzm",
"(hd0,1)/boot/boot_tune.pzm",
"(hd0,2)/boot.pzm",
"(hd0,2)/boot/boot.pzm",
"(hd0,2)/boot_tune.pzm",
"(hd0,2)/boot/boot_tune.pzm",
0 };
int is_applefw_img (char *firstblock);
static config_image_t configimgs[MAX_MENU_ITEMS];
void config_init(void)
{
char *configdata, *p;
int fd, len, firstitem = 1;
mlc_memset (&config, 0, sizeof (config));
mlc_memset (&configimgs, 0, sizeof (configimgs));
config.image = configimgs;
config.timeout = 15;
config.def = 1; // default item index in menu, 1-based
config.backlight = 1;
config.usegradient = 1;
config.bgcolor = fb_rgb(0,0,255);
config.hicolor = fb_rgb(64,128,0);
config.beep_time = 50;
config.beep_period = 30;
config.disable_boot_tune = 0;
config.boot_tune = (char *)find_somewhere (tunenames, "boot tune", NULL);
{
// preset default menu items
int i = 0;
config.image[i].title = "Apple OS";
if (is_applefw_img ((void*)(ipod_get_hwinfo()->mem_base))) {
config.image[i].type = CONFIG_IMAGE_SPECIAL;
config.image[i].path = "ramimg";
} else {
config.image[i].type = CONFIG_IMAGE_BINARY;
config.image[i].path = "(hd0,0)/aple";
if (vfs_open (config.image[i].path) < 0) {
config.image[i].path = "(hd0,0)/osos";
}
}
i++;
config.image[i].type = CONFIG_IMAGE_BINARY;
config.image[i].title = "iPodLinux";
config.image[i].path = (char *)find_somewhere (kernnames, "kernel", NULL);
if (config.image[i].path) { i++; }
config.image[i].type = CONFIG_IMAGE_ROCKBOX;
config.image[i].title = "Rockbox";
config.image[i].path = "(hd0,1)/.rockbox/rockbox.ipod";
if (vfs_open (config.image[i].path) >= 0) {
i++;
}
config.image[i].type = CONFIG_IMAGE_SPECIAL;
config.image[i].title = "Disk Mode";
config.image[i].path = "diskmode";
i++;
config.image[i].type = CONFIG_IMAGE_SPECIAL;
config.image[i].title = "Sleep";
config.image[i].path = "standby";
i++;
config.items = i;
}
if (find_somewhere (confnames, "configuration file", &fd)) {
// read the config file into the buffer at 'configdata'
configdata = mlc_malloc (4096);
mlc_memset (configdata, 0, 4096);
if ((len = vfs_read (configdata, 1, 4096, fd)) == 4096) {
mlc_printf ("Config file is too long, reading only first 4k\n");
--len;
}
configdata[len] = 0;
// change all CRs into LFs (for Windows and Mac users)
p = configdata;
while (*p) {
if (*p == '\r') *p = '\n';
++p;
}
// now parse the file contents
p = configdata;
while (p && *p) {
char *nextline = mlc_strchr (p, '\n');
char *value, *keyend, *white;
if (nextline) {
*nextline = 0;
do { nextline++; } while (*nextline == '\n'); // skips empty lines
}
while (*p == ' ' || *p == '\t') p++;
if (*p == ';' || *p == '#') {
// a comment line - ignore it
value = 0;
} else {
if ((value = mlc_strchr (p, '@')) != 0 || (value = mlc_strchr (p, '=')) != 0 || (value = mlc_strchr (p, ' ')) != 0) {
*value = 0;
do value++; while (*value == ' ' || *value == '\t' || *value == '=' || *value == '@');
}
}
if (!value) {
p = nextline;
continue;
}
keyend = p;
while (*keyend) keyend++; // goes to the NUL
do keyend--; while (*keyend == ' ' || *keyend == '\t'); // goes to last nonblank
*++keyend = 0; // first blank at end becomes end of string
if (!mlc_strcmp (p, "default")) {
config.def = mlc_atoi (value);
} else if (!mlc_strcmp (p, "timeout")) {
config.timeout = 0;
config.timeout = mlc_atoi (value);
if (config.timeout != 0 && config.timeout < 2) config.timeout = 2; // less than 2s is quite unusable
} else if (!mlc_strcmp (p, "debug")) {
config.debug = mlc_atoi (value);
} else if (!mlc_strcmp (p, "backlight")) {
config.backlight = mlc_atoi (value);
} else if (!mlc_strcmp (p, "contrast")) {
config.contrast = mlc_atoi (value);
} else if (!mlc_strcmp (p, "bg_gradient")) {
config.usegradient = mlc_atoi (value);
} else if (!mlc_strcmp (p, "bg_color")) {
config.bgcolor = mlc_atorgb (value, config.bgcolor);
} else if (!mlc_strcmp (p, "hilight_color")) {
config.hicolor = mlc_atorgb (value, config.hicolor);
} else if (!mlc_strcmp (p, "beep_duration")) {
config.beep_time = mlc_atoi (value);
} else if (!mlc_strcmp (p, "beep_period")) {
config.beep_period = mlc_atoi (value);
} else if (!mlc_strcmp (p, "disable_boot_tune")) {
config.disable_boot_tune = mlc_atoi (value);
} else if (!mlc_strcmp (p, "boot_tune")) {
config.boot_tune = value;
} else if (!mlc_strcmp (p, "ata_standby_code")) {
config.ata_standby_code = mlc_atoi (value);
} else {
// it's a menu item
if (firstitem) {
// the user wants to list the files explicitly - discard the default list
firstitem = 0;
config.items = 0;
}
while( *(white = (value + mlc_strlen(value) - 1)) == ' '
|| *(white = (value + mlc_strlen(value) - 1)) == '\t')
*white = '\0'; // Remove trailing whitespace
config.image[config.items].type = CONFIG_IMAGE_BINARY;
config.image[config.items].title = p;
config.image[config.items].path = value;
if (!mlc_strncasecmp (value, "rb:", 3)) {
config.image[config.items].path += 3;
config.image[config.items].type = CONFIG_IMAGE_ROCKBOX;
} else if (value[0] != '(' && value[0] != '[') { // no partition specifier -> it's not a path but command
config.image[config.items].type = CONFIG_IMAGE_SPECIAL;
}
config.items++;
if (config.items >= MAX_MENU_ITEMS) {
break;
}
}
p = nextline;
}
}
// finally, some sanity checks:
if (config.def > config.items) config.def = config.items;
}
config_t *config_get(void) {
return &config;
}