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"
34 #include "backlight.h"
39 #include "usb-target.h"
42 /* Show the Rockbox logo - in show_logo.c */
43 extern int show_logo(void);
46 #define TAR_HEADER_SIZE 157
48 /* Where files sent via MTP are stored */
49 static const char basedir
[] = "/Content/0b00/00/";
50 /* Can use memory after vector table up to 0x01f00000 */
51 static char * const tarbuf
= (char *)0x00000040;
52 static const size_t tarbuf_size
= 0x01f00000 - 0x00000040;
54 static void * const load_buf
= 0x00000000;
55 static const size_t load_buf_size
= 0x20000000 - 0x100000;
56 static const void * const start_addr
= 0x00000000;
58 /* Show a message + "Shutting down...", then power off the device */
59 static void display_message_and_power_off(int timeout
, const char *msg
)
63 printf("Shutting down...");
68 static void check_battery_safe(void)
70 if (battery_level_safe())
73 display_message_and_power_off(HZ
, "Battery low");
76 /* TODO: Handle charging while connected */
77 static void handle_usb(int connect_timeout
)
82 /* We need full button and backlight handling now */
87 /* Start the USB driver */
89 usb_start_monitoring();
91 /* Wait for threads to connect or cable is pulled */
92 printf("USB: Connecting");
94 if (connect_timeout
!= TIMEOUT_BLOCK
)
95 end_tick
= current_tick
+ connect_timeout
;
99 button
= button_get_w_tmo(HZ
/2);
101 if (button
== SYS_USB_CONNECTED
)
104 if (connect_timeout
!= TIMEOUT_BLOCK
&&
105 TIME_AFTER(current_tick
, end_tick
))
107 /* Timed out waiting for the connect - will happen when connected
108 * to a charger through the USB port */
109 printf("USB: Timed out");
114 break; /* Cable pulled */
117 if (button
== SYS_USB_CONNECTED
)
119 /* Switch to verbose mode if not in it so that the status updates
122 /* Got the message - wait for disconnect */
123 printf("Bootloader USB mode");
125 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
129 button
= button_get_w_tmo(HZ
/2);
130 if (button
== SYS_USB_DISCONNECTED
)
133 check_battery_safe();
137 /* Sleep a little to let the backlight ramp up */
141 /* Put drivers initialized for USB connection into a known state */
147 static void untar(int tar_fd
)
149 char header
[TAR_HEADER_SIZE
];
154 size_t size
= filesize(tar_fd
);
156 if (size
> tarbuf_size
)
158 printf("tar file too large"); /* Paranoid but proper */
162 ret
= read(tar_fd
, tarbuf
, filesize(tar_fd
));
165 printf("couldn't read tar file (%d)", ret
);
172 memcpy(header
, ptr
, TAR_HEADER_SIZE
);
174 if (*header
== '\0') /* Check for EOF */
177 /* Parse the size field */
179 for (i
= 124 ; i
< 124 + 11 ; i
++) {
180 size
= (8 * size
) + header
[i
] - '0';
183 /* Skip rest of header */
186 /* Make the path absolute */
188 strcat(path
, header
);
190 if (header
[156] == '0') /* file */
194 fd
= creat(path
, 0666);
197 printf("failed to create file (%d)", fd
);
201 wc
= write(fd
, ptr
, size
);
204 printf("write failed (%d)", wc
);
209 ptr
+= (size
+ TAR_CHUNK
-1) & (~(TAR_CHUNK
-1));
211 else if (header
[156] == '5') /* directory */
215 /* Remove the trailing slash */
216 if (path
[strlen(path
) - 1] == '/')
217 path
[strlen(path
) - 1] = '\0';
221 if (ret
< 0 && ret
!= -4)
223 printf("failed to create dir (%d)", ret
);
229 /* Look for a tar file or rockbox binary in the MTP directory */
230 static void handle_untar(void)
235 struct dirent_uncached
* entry
;
240 dir
= opendir_uncached(basedir
);
242 while ((entry
= readdir_uncached(dir
)))
244 if (*entry
->d_name
== '.')
247 snprintf(buf
, sizeof(buf
), "%s%s", basedir
, entry
->d_name
);
248 fd
= open(buf
, O_RDONLY
);
253 /* Check whether the file is a rockbox binary. */
254 lseek(fd
, 4, SEEK_SET
);
255 rc
= read(fd
, model
, 4);
259 if (strcmp(model
, "gigs") == 0)
262 printf("Found rockbox binary. Moving...");
264 remove( BOOTDIR
"/" BOOTFILE
);
265 int ret
= rename(buf
, BOOTDIR
"/" BOOTFILE
);
266 printf("returned %d", ret
);
272 /* Check whether the file is a tar file. */
273 lseek(fd
, 257, SEEK_SET
);
274 rc
= read(fd
, tarstring
, 5);
278 if (strcmp(tarstring
, "ustar") == 0)
281 printf("Found tar file. Unarchiving...");
282 lseek(fd
, 0, SEEK_SET
);
285 printf("Removing tar file");
295 /* Try to load the firmware and run it */
296 static void NORETURN_ATTR
handle_firmware_load(void)
298 int rc
= load_firmware(load_buf
, BOOTFILE
, load_buf_size
);
301 error(EBOOTFILE
, rc
, true);
303 /* Pause to look at messages */
306 int button
= button_read_device();
308 /* Ignore settings reset */
309 if (button
== BUTTON_NONE
|| button
== BUTTON_MENU
)
314 check_battery_safe();
316 /* If the disk powers off, the firmware will lock at startup */
320 /* Put drivers into a known state */
321 button_close_device();
323 system_prepare_fw_start();
327 cpucache_commit_discard();
328 asm volatile ("bx %0": : "r"(start_addr
));
344 /* Keep button_device_init early to delay calls to button_read_device */
345 button_init_device();
353 display_message_and_power_off(HZ
, "Hold switch on");
355 if (button_read_device() != BUTTON_NONE
)
358 printf("Gigabeat S Rockbox Bootloader");
359 printf("Version " RBVERSION
);
362 batt
= battery_adc_voltage();
363 printf("Battery: %d.%03d V", batt
/ 1000, batt
% 1000);
364 check_battery_safe();
368 error(EATA
, rc
, true);
372 rc
= disk_mount_all();
374 error(EDISK
, rc
, true);
376 printf("Init complete");
378 /* Do USB first since a tar or binary could be added to the MTP directory
379 * at the time and we can untar or move after unplugging. */
384 handle_firmware_load(); /* No return */