1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 by Greg White
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
25 #include "gcc_extensions.h"
28 #include "powermgmt.h"
33 #include "backlight.h"
38 #include "usb-target.h"
42 #define TAR_HEADER_SIZE 157
44 /* Where files sent via MTP are stored */
45 static const char basedir
[] = "/Content/0b00/00/";
46 /* Can use memory after vector table up to 0x01f00000 */
47 static char * const tarbuf
= (char *)0x00000040;
48 static const size_t tarbuf_size
= 0x01f00000 - 0x00000040;
50 static void * const load_buf
= 0x00000000;
51 static const size_t load_buf_size
= 0x20000000 - 0x100000;
52 static const void * const start_addr
= 0x00000000;
54 static void show_splash(int timeout
, const char *msg
)
58 lcd_putsxy( (LCD_WIDTH
- (SYSFONT_WIDTH
* strlen(msg
))) / 2,
59 (LCD_HEIGHT
- SYSFONT_HEIGHT
) / 2, msg
);
65 static bool pause_if_button_pressed(bool pre_usb
)
69 int button
= button_read_device();
71 if (pre_usb
&& !usb_plugged())
74 /* Exit if no button or only select buttons that have other
78 case USB_BL_INSTALL_MODE_BTN
:
80 break; /* Only before USB detect */
81 case BUTTON_MENU
: /* Settings reset */
82 case BUTTON_NONE
: /* Nothing pressed */
88 /* If the disk powers off, the firmware will lock at startup */
93 /* TODO: Handle charging while connected */
94 static void handle_usb(void)
98 /* Check if plugged and pause to look at messages. If the cable was pulled
99 * while waiting, proceed as if it never was plugged. */
100 if (!usb_plugged() || !pause_if_button_pressed(true))
103 /** Enter USB mode **/
105 /* We need full button and backlight handling now */
109 /* Start the USB driver */
111 usb_start_monitoring();
113 /* Wait for threads to connect or cable is pulled */
114 show_splash(HZ
/2, "Waiting for USB");
118 button
= button_get_w_tmo(HZ
/2);
120 if (button
== SYS_USB_CONNECTED
)
124 break; /* Cable pulled */
127 if (button
== SYS_USB_CONNECTED
)
129 /* Got the message - wait for disconnect */
130 show_splash(0, "Bootloader USB mode");
132 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
136 button
= button_get(true);
137 if (button
== SYS_USB_DISCONNECTED
)
139 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
145 /* Put drivers initialized for USB connection into a known state */
151 /* Sleep a little to let the backlight ramp up */
157 static void untar(int tar_fd
)
159 char header
[TAR_HEADER_SIZE
];
164 size_t size
= filesize(tar_fd
);
166 if (size
> tarbuf_size
)
168 printf("tar file too large"); /* Paranoid but proper */
172 ret
= read(tar_fd
, tarbuf
, filesize(tar_fd
));
175 printf("couldn't read tar file (%d)", ret
);
182 memcpy(header
, ptr
, TAR_HEADER_SIZE
);
184 if (*header
== '\0') /* Check for EOF */
187 /* Parse the size field */
189 for (i
= 124 ; i
< 124 + 11 ; i
++) {
190 size
= (8 * size
) + header
[i
] - '0';
193 /* Skip rest of header */
196 /* Make the path absolute */
198 strcat(path
, header
);
200 if (header
[156] == '0') /* file */
204 fd
= creat(path
, 0666);
207 printf("failed to create file (%d)", fd
);
211 wc
= write(fd
, ptr
, size
);
214 printf("write failed (%d)", wc
);
219 ptr
+= (size
+ TAR_CHUNK
-1) & (~(TAR_CHUNK
-1));
221 else if (header
[156] == '5') /* directory */
225 /* Remove the trailing slash */
226 if (path
[strlen(path
) - 1] == '/')
227 path
[strlen(path
) - 1] = '\0';
231 if (ret
< 0 && ret
!= -4)
233 printf("failed to create dir (%d)", ret
);
239 /* Look for a tar file or rockbox binary in the MTP directory */
240 static void handle_untar(void)
245 struct dirent_uncached
* entry
;
250 dir
= opendir_uncached(basedir
);
252 while ((entry
= readdir_uncached(dir
)))
254 if (*entry
->d_name
== '.')
257 snprintf(buf
, sizeof(buf
), "%s%s", basedir
, entry
->d_name
);
258 fd
= open(buf
, O_RDONLY
);
263 /* Check whether the file is a rockbox binary. */
264 lseek(fd
, 4, SEEK_SET
);
265 rc
= read(fd
, model
, 4);
269 if (strcmp(model
, "gigs") == 0)
271 printf("Found rockbox binary. Moving...");
273 remove( BOOTDIR
"/" BOOTFILE
);
274 int ret
= rename(buf
, BOOTDIR
"/" BOOTFILE
);
275 printf("returned %d", ret
);
281 /* Check whether the file is a tar file. */
282 lseek(fd
, 257, SEEK_SET
);
283 rc
= read(fd
, tarstring
, 5);
287 if (strcmp(tarstring
, "ustar") == 0)
289 printf("Found tar file. Unarchiving...");
290 lseek(fd
, 0, SEEK_SET
);
293 printf("Removing tar file");
303 /* Try to load the firmware and run it */
304 static void NORETURN_ATTR
handle_firmware_load(void)
306 int rc
= load_firmware(load_buf
, BOOTFILE
,
310 error(EBOOTFILE
, rc
, true);
312 /* Pause to look at messages */
313 pause_if_button_pressed(false);
315 /* Put drivers into a known state */
316 button_close_device();
318 system_prepare_fw_start();
322 cpucache_invalidate();
323 asm volatile ("bx %0": : "r"(start_addr
));
331 static void check_battery(void)
333 int batt
= battery_adc_voltage();
334 printf("Battery: %d.%03d V", batt
/ 1000, batt
% 1000);
335 /* TODO: warn on low battery or shut down */
342 /* Flush and invalidate all caches (because vectors were written) */
343 cpucache_invalidate();
348 enable_interrupt(IRQ_FIQ_STATUS
);
353 printf("Gigabeat S Rockbox Bootloader");
354 printf("Version " RBVERSION
);
356 /* Initialize KPP so we can poll the button states */
357 button_init_device();
367 error(EATA
, rc
, true);
372 rc
= disk_mount_all();
375 error(EDISK
, rc
, true);
378 printf("Init complete");
380 /* Do USB first since a tar or binary could be added to the MTP directory
381 * at the time and we can untar or move after unplugging. */
384 handle_firmware_load(); /* No return */