Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2006 by Greg White |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 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. |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | #include "config.h" |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 22 | #include "system.h" |
Michael Sevakis | c41caca | 2008-05-07 07:14:39 +0000 | [diff] [blame] | 23 | #include <sprintf.h> |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 24 | #include "kernel.h" |
Michael Sevakis | c41caca | 2008-05-07 07:14:39 +0000 | [diff] [blame] | 25 | #include "string.h" |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 26 | #include "adc.h" |
| 27 | #include "powermgmt.h" |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 28 | #include "ata.h" |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 29 | #include "dir.h" |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 30 | #include "disk.h" |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 31 | #include "common.h" |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 32 | #include "backlight.h" |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 33 | #include "usb.h" |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 34 | #include "button.h" |
Michael Sevakis | c41caca | 2008-05-07 07:14:39 +0000 | [diff] [blame] | 35 | #include "font.h" |
| 36 | #include "lcd.h" |
Michael Sevakis | 94f7d0f | 2008-04-18 16:42:50 +0000 | [diff] [blame] | 37 | #include "usb-target.h" |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 38 | |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 39 | #define TAR_CHUNK 512 |
| 40 | #define TAR_HEADER_SIZE 157 |
| 41 | |
Michael Sevakis | c41caca | 2008-05-07 07:14:39 +0000 | [diff] [blame] | 42 | const char version[] = APPSVERSION; |
| 43 | /* Where files sent via MTP are stored */ |
| 44 | static const char basedir[] = "/Content/0b00/00/"; |
| 45 | /* Can use memory after vector table up to 0x01f00000 */ |
| 46 | static char * const tarbuf = (char *)0x00000040; |
| 47 | static const size_t tarbuf_size = 0x01f00000 - 0x00000040; |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 48 | /* Firmware data */ |
| 49 | static void * const load_buf = 0x00000000; |
| 50 | static const size_t load_buf_size = 0x20000000 - 0x100000; |
| 51 | static const void * const start_addr = 0x00000000; |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 52 | |
Michael Sevakis | c41caca | 2008-05-07 07:14:39 +0000 | [diff] [blame] | 53 | static void show_splash(int timeout, const char *msg) |
Michael Sevakis | 94f7d0f | 2008-04-18 16:42:50 +0000 | [diff] [blame] | 54 | { |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 55 | backlight_on(); |
| 56 | reset_screen(); |
Michael Sevakis | 94f7d0f | 2008-04-18 16:42:50 +0000 | [diff] [blame] | 57 | lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2, |
| 58 | (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg); |
| 59 | lcd_update(); |
| 60 | |
| 61 | sleep(timeout); |
| 62 | } |
| 63 | |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 64 | static bool pause_if_button_pressed(bool pre_usb) |
| 65 | { |
| 66 | while (1) |
| 67 | { |
| 68 | int button = button_read_device(); |
| 69 | |
| 70 | if (pre_usb && !usb_plugged()) |
| 71 | return false; |
| 72 | |
| 73 | /* Exit if no button or only the menu (settings reset) button */ |
| 74 | switch (button) |
| 75 | { |
| 76 | case BUTTON_MENU: |
| 77 | case BUTTON_NONE: |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | sleep(HZ/5); |
| 82 | |
| 83 | /* If the disk powers off, the firmware will lock at startup */ |
| 84 | ata_spin(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /* TODO: Handle charging while connected */ |
| 89 | static void handle_usb(void) |
| 90 | { |
| 91 | int button; |
| 92 | |
| 93 | /* Check if plugged and pause to look at messages. If the cable was pulled |
| 94 | * while waiting, proceed as if it never was plugged. */ |
| 95 | if (!usb_plugged() || !pause_if_button_pressed(true)) |
| 96 | { |
| 97 | /* Bang on the controller */ |
| 98 | usb_init_device(); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | /** Enter USB mode **/ |
| 103 | |
| 104 | /* We need full button and backlight handling now */ |
| 105 | backlight_init(); |
| 106 | button_init(); |
| 107 | |
| 108 | /* Start the USB driver */ |
| 109 | usb_init(); |
| 110 | usb_start_monitoring(); |
| 111 | |
| 112 | /* Wait for threads to connect or cable is pulled */ |
| 113 | show_splash(HZ/2, "Waiting for USB"); |
| 114 | |
| 115 | while (1) |
| 116 | { |
| 117 | button = button_get_w_tmo(HZ/2); |
| 118 | |
| 119 | if (button == SYS_USB_CONNECTED) |
| 120 | break; /* Hit */ |
| 121 | |
| 122 | if (!usb_plugged()) |
| 123 | break; /* Cable pulled */ |
| 124 | } |
| 125 | |
| 126 | if (button == SYS_USB_CONNECTED) |
| 127 | { |
| 128 | /* Got the message - wait for disconnect */ |
| 129 | show_splash(0, "Bootloader USB mode"); |
| 130 | |
| 131 | usb_acknowledge(SYS_USB_CONNECTED_ACK); |
| 132 | |
| 133 | while (1) |
| 134 | { |
| 135 | button = button_get(true); |
| 136 | if (button == SYS_USB_DISCONNECTED) |
| 137 | { |
| 138 | usb_acknowledge(SYS_USB_DISCONNECTED_ACK); |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /* Put drivers initialized for USB connection into a known state */ |
| 145 | backlight_on(); |
| 146 | usb_close(); |
| 147 | button_close(); |
| 148 | backlight_close(); |
| 149 | |
| 150 | reset_screen(); |
| 151 | } |
| 152 | |
Michael Sevakis | c41caca | 2008-05-07 07:14:39 +0000 | [diff] [blame] | 153 | static void untar(int tar_fd) |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 154 | { |
| 155 | char header[TAR_HEADER_SIZE]; |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 156 | char *ptr; |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 157 | char path[102]; |
Michael Sevakis | c41caca | 2008-05-07 07:14:39 +0000 | [diff] [blame] | 158 | int fd, i; |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 159 | int ret; |
Michael Sevakis | c41caca | 2008-05-07 07:14:39 +0000 | [diff] [blame] | 160 | size_t size = filesize(tar_fd); |
| 161 | |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 162 | if (size > tarbuf_size) |
| 163 | { |
Michael Sevakis | c41caca | 2008-05-07 07:14:39 +0000 | [diff] [blame] | 164 | printf("tar file too large"); /* Paranoid but proper */ |
| 165 | return; |
| 166 | } |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 167 | |
| 168 | ret = read(tar_fd, tarbuf, filesize(tar_fd)); |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 169 | if (ret < 0) |
| 170 | { |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 171 | printf("couldn't read tar file (%d)", ret); |
| 172 | return; |
| 173 | } |
| 174 | ptr = tarbuf; |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 175 | |
| 176 | while (1) |
| 177 | { |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 178 | memcpy(header, ptr, TAR_HEADER_SIZE); |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 179 | |
| 180 | if (*header == '\0') /* Check for EOF */ |
| 181 | break; |
| 182 | |
| 183 | /* Parse the size field */ |
| 184 | size = 0; |
| 185 | for (i = 124 ; i < 124 + 11 ; i++) { |
| 186 | size = (8 * size) + header[i] - '0'; |
| 187 | } |
| 188 | |
| 189 | /* Skip rest of header */ |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 190 | ptr += TAR_CHUNK; |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 191 | |
| 192 | /* Make the path absolute */ |
| 193 | strcpy(path, "/"); |
| 194 | strcat(path, header); |
| 195 | |
| 196 | if (header[156] == '0') /* file */ |
| 197 | { |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 198 | int wc; |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 199 | |
| 200 | fd = creat(path); |
| 201 | if (fd < 0) |
| 202 | { |
| 203 | printf("failed to create file (%d)", fd); |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 204 | } |
| 205 | else |
| 206 | { |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 207 | wc = write(fd, ptr, size); |
| 208 | if (wc < 0) |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 209 | { |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 210 | printf("write failed (%d)", wc); |
| 211 | break; |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 212 | } |
| 213 | close(fd); |
| 214 | } |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 215 | ptr += (size + TAR_CHUNK-1) & (~(TAR_CHUNK-1)); |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 216 | } |
| 217 | else if (header[156] == '5') /* directory */ |
| 218 | { |
| 219 | int ret; |
| 220 | |
| 221 | /* Remove the trailing slash */ |
| 222 | if (path[strlen(path) - 1] == '/') |
| 223 | path[strlen(path) - 1] = '\0'; |
| 224 | |
| 225 | /* Create the dir */ |
| 226 | ret = mkdir(path); |
| 227 | if (ret < 0 && ret != -4) |
| 228 | { |
| 229 | printf("failed to create dir (%d)", ret); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 235 | /* Look for a tar file or rockbox binary in the MTP directory */ |
| 236 | static void handle_untar(void) |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 237 | { |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 238 | char buf[MAX_PATH]; |
| 239 | char tarstring[6]; |
Nicolas Pennequin | 620e6b4 | 2008-04-15 23:17:03 +0000 | [diff] [blame] | 240 | char model[5]; |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 241 | struct dirent_uncached* entry; |
| 242 | DIR_UNCACHED* dir; |
| 243 | int fd; |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 244 | int rc; |
| 245 | |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 246 | dir = opendir_uncached(basedir); |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 247 | |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 248 | while ((entry = readdir_uncached(dir))) |
| 249 | { |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 250 | if (*entry->d_name == '.') |
| 251 | continue; |
Nicolas Pennequin | 620e6b4 | 2008-04-15 23:17:03 +0000 | [diff] [blame] | 252 | |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 253 | snprintf(buf, sizeof(buf), "%s%s", basedir, entry->d_name); |
| 254 | fd = open(buf, O_RDONLY); |
| 255 | |
| 256 | if (fd < 0) |
| 257 | continue; |
| 258 | |
| 259 | /* Check whether the file is a rockbox binary. */ |
| 260 | lseek(fd, 4, SEEK_SET); |
| 261 | rc = read(fd, model, 4); |
| 262 | if (rc == 4) |
| 263 | { |
| 264 | model[4] = 0; |
| 265 | if (strcmp(model, "gigs") == 0) |
| 266 | { |
| 267 | printf("Found rockbox binary. Moving..."); |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 268 | close(fd); |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 269 | remove("/.rockbox/rockbox.gigabeat"); |
| 270 | int ret = rename(buf, "/.rockbox/rockbox.gigabeat"); |
| 271 | printf("returned %d", ret); |
| 272 | sleep(HZ); |
| 273 | break; |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 276 | |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 277 | /* Check whether the file is a tar file. */ |
| 278 | lseek(fd, 257, SEEK_SET); |
| 279 | rc = read(fd, tarstring, 5); |
| 280 | if (rc == 5) |
Michael Sevakis | 94f7d0f | 2008-04-18 16:42:50 +0000 | [diff] [blame] | 281 | { |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 282 | tarstring[5] = 0; |
| 283 | if (strcmp(tarstring, "ustar") == 0) |
| 284 | { |
| 285 | printf("Found tar file. Unarchiving..."); |
| 286 | lseek(fd, 0, SEEK_SET); |
| 287 | untar(fd); |
| 288 | close(fd); |
| 289 | printf("Removing tar file"); |
| 290 | remove(buf); |
| 291 | break; |
| 292 | } |
Michael Sevakis | 94f7d0f | 2008-04-18 16:42:50 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 295 | close(fd); |
Michael Sevakis | 94f7d0f | 2008-04-18 16:42:50 +0000 | [diff] [blame] | 296 | } |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 297 | } |
Michael Sevakis | 94f7d0f | 2008-04-18 16:42:50 +0000 | [diff] [blame] | 298 | |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 299 | /* Try to load the firmware and run it */ |
| 300 | static void __attribute__((noreturn)) handle_firmware_load(void) |
| 301 | { |
| 302 | int rc = load_firmware(load_buf, "/.rockbox/rockbox.gigabeat", |
| 303 | load_buf_size); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 304 | |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 305 | if(rc < 0) |
Michael Sevakis | 75fe887 | 2008-05-04 03:44:49 +0000 | [diff] [blame] | 306 | error(EBOOTFILE, rc); |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 307 | |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 308 | /* Pause to look at messages */ |
| 309 | pause_if_button_pressed(false); |
| 310 | |
| 311 | /* Put drivers into a known state */ |
| 312 | button_close_device(); |
| 313 | ata_close(); |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 314 | system_prepare_fw_start(); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 315 | |
| 316 | if (rc == EOK) |
| 317 | { |
Michael Sevakis | 1f021af | 2008-02-05 04:43:19 +0000 | [diff] [blame] | 318 | invalidate_icache(); |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 319 | asm volatile ("bx %0": : "r"(start_addr)); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 320 | } |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 321 | |
Michael Sevakis | 94f7d0f | 2008-04-18 16:42:50 +0000 | [diff] [blame] | 322 | /* Halt */ |
| 323 | while (1) |
| 324 | core_idle(); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 327 | static void check_battery(void) |
| 328 | { |
| 329 | int batt = battery_adc_voltage(); |
| 330 | printf("Battery: %d.%03d V", batt / 1000, batt % 1000); |
| 331 | /* TODO: warn on low battery or shut down */ |
| 332 | } |
| 333 | |
| 334 | void main(void) |
| 335 | { |
| 336 | int rc; |
| 337 | |
| 338 | /* Flush and invalidate all caches (because vectors were written) */ |
| 339 | invalidate_icache(); |
| 340 | |
| 341 | lcd_clear_display(); |
| 342 | printf("Gigabeat S Rockbox Bootloader"); |
| 343 | printf("Version %s", version); |
| 344 | system_init(); |
| 345 | kernel_init(); |
| 346 | |
| 347 | enable_interrupt(IRQ_FIQ_STATUS); |
| 348 | |
| 349 | /* Initialize KPP so we can poll the button states */ |
| 350 | button_init_device(); |
| 351 | |
| 352 | adc_init(); |
| 353 | |
| 354 | check_battery(); |
| 355 | |
| 356 | rc = ata_init(); |
| 357 | if(rc) |
| 358 | { |
| 359 | reset_screen(); |
| 360 | error(EATA, rc); |
| 361 | } |
| 362 | |
| 363 | disk_init(); |
| 364 | |
| 365 | rc = disk_mount_all(); |
| 366 | if (rc<=0) |
| 367 | { |
| 368 | error(EDISK,rc); |
| 369 | } |
| 370 | |
| 371 | printf("Init complete"); |
| 372 | |
| 373 | /* Do USB first since a tar or binary could be added to the MTP directory |
| 374 | * at the time and we can untar or move after unplugging. */ |
| 375 | handle_usb(); |
| 376 | handle_untar(); |
| 377 | handle_firmware_load(); /* No return */ |
| 378 | } |