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
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2006 by Greg White
*
* 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 "config.h"
#include "system.h"
#include <sprintf.h>
#include "kernel.h"
#include "string.h"
#include "adc.h"
#include "powermgmt.h"
#include "storage.h"
#include "dir.h"
#include "disk.h"
#include "common.h"
#include "backlight.h"
#include "usb.h"
#include "button.h"
#include "font.h"
#include "lcd.h"
#include "usb-target.h"
#define TAR_CHUNK 512
#define TAR_HEADER_SIZE 157
const char version[] = APPSVERSION;
/* Where files sent via MTP are stored */
static const char basedir[] = "/Content/0b00/00/";
/* Can use memory after vector table up to 0x01f00000 */
static char * const tarbuf = (char *)0x00000040;
static const size_t tarbuf_size = 0x01f00000 - 0x00000040;
/* Firmware data */
static void * const load_buf = 0x00000000;
static const size_t load_buf_size = 0x20000000 - 0x100000;
static const void * const start_addr = 0x00000000;
static void show_splash(int timeout, const char *msg)
{
backlight_on();
reset_screen();
lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2,
(LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
lcd_update();
sleep(timeout);
}
static bool pause_if_button_pressed(bool pre_usb)
{
while (1)
{
int button = button_read_device();
if (pre_usb && !usb_plugged())
return false;
/* Exit if no button or only the menu (settings reset) button */
switch (button)
{
case BUTTON_MENU:
case BUTTON_NONE:
return true;
}
sleep(HZ/5);
/* If the disk powers off, the firmware will lock at startup */
storage_spin();
}
}
/* TODO: Handle charging while connected */
static void handle_usb(void)
{
int button;
/* Check if plugged and pause to look at messages. If the cable was pulled
* while waiting, proceed as if it never was plugged. */
if (!usb_plugged() || !pause_if_button_pressed(true))
return;
/** Enter USB mode **/
/* We need full button and backlight handling now */
backlight_init();
button_init();
/* Start the USB driver */
usb_init();
usb_start_monitoring();
/* Wait for threads to connect or cable is pulled */
show_splash(HZ/2, "Waiting for USB");
while (1)
{
button = button_get_w_tmo(HZ/2);
if (button == SYS_USB_CONNECTED)
break; /* Hit */
if (!usb_plugged())
break; /* Cable pulled */
}
if (button == SYS_USB_CONNECTED)
{
/* Got the message - wait for disconnect */
show_splash(0, "Bootloader USB mode");
usb_acknowledge(SYS_USB_CONNECTED_ACK);
while (1)
{
button = button_get(true);
if (button == SYS_USB_DISCONNECTED)
{
usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
break;
}
}
}
/* Put drivers initialized for USB connection into a known state */
backlight_on();
usb_close();
button_close();
backlight_close();
reset_screen();
}
static void untar(int tar_fd)
{
char header[TAR_HEADER_SIZE];
char *ptr;
char path[102];
int fd, i;
int ret;
size_t size = filesize(tar_fd);
if (size > tarbuf_size)
{
printf("tar file too large"); /* Paranoid but proper */
return;
}
ret = read(tar_fd, tarbuf, filesize(tar_fd));
if (ret < 0)
{
printf("couldn't read tar file (%d)", ret);
return;
}
ptr = tarbuf;
while (1)
{
memcpy(header, ptr, TAR_HEADER_SIZE);
if (*header == '\0') /* Check for EOF */
break;
/* Parse the size field */
size = 0;
for (i = 124 ; i < 124 + 11 ; i++) {
size = (8 * size) + header[i] - '0';
}
/* Skip rest of header */
ptr += TAR_CHUNK;
/* Make the path absolute */
strcpy(path, "/");
strcat(path, header);
if (header[156] == '0') /* file */
{
int wc;
fd = creat(path);
if (fd < 0)
{
printf("failed to create file (%d)", fd);
}
else
{
wc = write(fd, ptr, size);
if (wc < 0)
{
printf("write failed (%d)", wc);
break;
}
close(fd);
}
ptr += (size + TAR_CHUNK-1) & (~(TAR_CHUNK-1));
}
else if (header[156] == '5') /* directory */
{
int ret;
/* Remove the trailing slash */
if (path[strlen(path) - 1] == '/')
path[strlen(path) - 1] = '\0';
/* Create the dir */
ret = mkdir(path);
if (ret < 0 && ret != -4)
{
printf("failed to create dir (%d)", ret);
}
}
}
}
/* Look for a tar file or rockbox binary in the MTP directory */
static void handle_untar(void)
{
char buf[MAX_PATH];
char tarstring[6];
char model[5];
struct dirent_uncached* entry;
DIR_UNCACHED* dir;
int fd;
int rc;
dir = opendir_uncached(basedir);
while ((entry = readdir_uncached(dir)))
{
if (*entry->d_name == '.')
continue;
snprintf(buf, sizeof(buf), "%s%s", basedir, entry->d_name);
fd = open(buf, O_RDONLY);
if (fd < 0)
continue;
/* Check whether the file is a rockbox binary. */
lseek(fd, 4, SEEK_SET);
rc = read(fd, model, 4);
if (rc == 4)
{
model[4] = 0;
if (strcmp(model, "gigs") == 0)
{
printf("Found rockbox binary. Moving...");
close(fd);
remove( BOOTDIR "/" BOOTFILE);
int ret = rename(buf, BOOTDIR "/" BOOTFILE);
printf("returned %d", ret);
sleep(HZ);
break;
}
}
/* Check whether the file is a tar file. */
lseek(fd, 257, SEEK_SET);
rc = read(fd, tarstring, 5);
if (rc == 5)
{
tarstring[5] = 0;
if (strcmp(tarstring, "ustar") == 0)
{
printf("Found tar file. Unarchiving...");
lseek(fd, 0, SEEK_SET);
untar(fd);
close(fd);
printf("Removing tar file");
remove(buf);
break;
}
}
close(fd);
}
}
/* Try to load the firmware and run it */
static void __attribute__((noreturn)) handle_firmware_load(void)
{
int rc = load_firmware(load_buf, BOOTFILE,
load_buf_size);
if(rc < 0)
error(EBOOTFILE, rc);
/* Pause to look at messages */
pause_if_button_pressed(false);
/* Put drivers into a known state */
button_close_device();
storage_close();
system_prepare_fw_start();
if (rc == EOK)
{
cpucache_invalidate();
asm volatile ("bx %0": : "r"(start_addr));
}
/* Halt */
while (1)
core_idle();
}
static void check_battery(void)
{
int batt = battery_adc_voltage();
printf("Battery: %d.%03d V", batt / 1000, batt % 1000);
/* TODO: warn on low battery or shut down */
}
void main(void)
{
int rc;
/* Flush and invalidate all caches (because vectors were written) */
cpucache_invalidate();
system_init();
kernel_init();
enable_interrupt(IRQ_FIQ_STATUS);
lcd_init_device();
lcd_clear_display();
printf("Gigabeat S Rockbox Bootloader");
printf("Version %s", version);
/* Initialize KPP so we can poll the button states */
button_init_device();
adc_init();
check_battery();
rc = storage_init();
if(rc)
{
reset_screen();
error(EATA, rc);
}
disk_init();
rc = disk_mount_all();
if (rc<=0)
{
error(EDISK,rc);
}
printf("Init complete");
/* Do USB first since a tar or binary could be added to the MTP directory
* at the time and we can untar or move after unplugging. */
handle_usb();
handle_untar();
handle_firmware_load(); /* No return */
}
|