1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 by Greg White
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
25 #include "powermgmt.h"
30 #include "backlight.h"
35 #include "usb-target.h"
38 #define TAR_HEADER_SIZE 157
40 const char version
[] = APPSVERSION
;
41 /* Where files sent via MTP are stored */
42 static const char basedir
[] = "/Content/0b00/00/";
43 /* Can use memory after vector table up to 0x01f00000 */
44 static char * const tarbuf
= (char *)0x00000040;
45 static const size_t tarbuf_size
= 0x01f00000 - 0x00000040;
47 static void * const load_buf
= 0x00000000;
48 static const size_t load_buf_size
= 0x20000000 - 0x100000;
49 static const void * const start_addr
= 0x00000000;
51 static void show_splash(int timeout
, const char *msg
)
55 lcd_putsxy( (LCD_WIDTH
- (SYSFONT_WIDTH
* strlen(msg
))) / 2,
56 (LCD_HEIGHT
- SYSFONT_HEIGHT
) / 2, msg
);
62 static bool pause_if_button_pressed(bool pre_usb
)
66 int button
= button_read_device();
68 if (pre_usb
&& !usb_plugged())
71 /* Exit if no button or only the menu (settings reset) button */
81 /* If the disk powers off, the firmware will lock at startup */
86 /* TODO: Handle charging while connected */
87 static void handle_usb(void)
91 /* Check if plugged and pause to look at messages. If the cable was pulled
92 * while waiting, proceed as if it never was plugged. */
93 if (!usb_plugged() || !pause_if_button_pressed(true))
95 /* Bang on the controller */
100 /** Enter USB mode **/
102 /* We need full button and backlight handling now */
106 /* Start the USB driver */
108 usb_start_monitoring();
110 /* Wait for threads to connect or cable is pulled */
111 show_splash(HZ
/2, "Waiting for USB");
115 button
= button_get_w_tmo(HZ
/2);
117 if (button
== SYS_USB_CONNECTED
)
121 break; /* Cable pulled */
124 if (button
== SYS_USB_CONNECTED
)
126 /* Got the message - wait for disconnect */
127 show_splash(0, "Bootloader USB mode");
129 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
133 button
= button_get(true);
134 if (button
== SYS_USB_DISCONNECTED
)
136 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
142 /* Put drivers initialized for USB connection into a known state */
151 static void untar(int tar_fd
)
153 char header
[TAR_HEADER_SIZE
];
158 size_t size
= filesize(tar_fd
);
160 if (size
> tarbuf_size
)
162 printf("tar file too large"); /* Paranoid but proper */
166 ret
= read(tar_fd
, tarbuf
, filesize(tar_fd
));
169 printf("couldn't read tar file (%d)", ret
);
176 memcpy(header
, ptr
, TAR_HEADER_SIZE
);
178 if (*header
== '\0') /* Check for EOF */
181 /* Parse the size field */
183 for (i
= 124 ; i
< 124 + 11 ; i
++) {
184 size
= (8 * size
) + header
[i
] - '0';
187 /* Skip rest of header */
190 /* Make the path absolute */
192 strcat(path
, header
);
194 if (header
[156] == '0') /* file */
201 printf("failed to create file (%d)", fd
);
205 wc
= write(fd
, ptr
, size
);
208 printf("write failed (%d)", wc
);
213 ptr
+= (size
+ TAR_CHUNK
-1) & (~(TAR_CHUNK
-1));
215 else if (header
[156] == '5') /* directory */
219 /* Remove the trailing slash */
220 if (path
[strlen(path
) - 1] == '/')
221 path
[strlen(path
) - 1] = '\0';
225 if (ret
< 0 && ret
!= -4)
227 printf("failed to create dir (%d)", ret
);
233 /* Look for a tar file or rockbox binary in the MTP directory */
234 static void handle_untar(void)
239 struct dirent_uncached
* entry
;
244 dir
= opendir_uncached(basedir
);
246 while ((entry
= readdir_uncached(dir
)))
248 if (*entry
->d_name
== '.')
251 snprintf(buf
, sizeof(buf
), "%s%s", basedir
, entry
->d_name
);
252 fd
= open(buf
, O_RDONLY
);
257 /* Check whether the file is a rockbox binary. */
258 lseek(fd
, 4, SEEK_SET
);
259 rc
= read(fd
, model
, 4);
263 if (strcmp(model
, "gigs") == 0)
265 printf("Found rockbox binary. Moving...");
267 remove("/.rockbox/rockbox.gigabeat");
268 int ret
= rename(buf
, "/.rockbox/rockbox.gigabeat");
269 printf("returned %d", ret
);
275 /* Check whether the file is a tar file. */
276 lseek(fd
, 257, SEEK_SET
);
277 rc
= read(fd
, tarstring
, 5);
281 if (strcmp(tarstring
, "ustar") == 0)
283 printf("Found tar file. Unarchiving...");
284 lseek(fd
, 0, SEEK_SET
);
287 printf("Removing tar file");
297 /* Try to load the firmware and run it */
298 static void __attribute__((noreturn
)) handle_firmware_load(void)
300 int rc
= load_firmware(load_buf
, "/.rockbox/rockbox.gigabeat",
304 error(EBOOTFILE
, rc
);
306 /* Pause to look at messages */
307 pause_if_button_pressed(false);
309 /* Put drivers into a known state */
310 button_close_device();
312 system_prepare_fw_start();
317 asm volatile ("bx %0": : "r"(start_addr
));
325 static void check_battery(void)
327 int batt
= battery_adc_voltage();
328 printf("Battery: %d.%03d V", batt
/ 1000, batt
% 1000);
329 /* TODO: warn on low battery or shut down */
336 /* Flush and invalidate all caches (because vectors were written) */
340 printf("Gigabeat S Rockbox Bootloader");
341 printf("Version %s", version
);
345 enable_interrupt(IRQ_FIQ_STATUS
);
347 /* Initialize KPP so we can poll the button states */
348 button_init_device();
363 rc
= disk_mount_all();
369 printf("Init complete");
371 /* Do USB first since a tar or binary could be added to the MTP directory
372 * at the time and we can untar or move after unplugging. */
375 handle_firmware_load(); /* No return */