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 | * |
| 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. |
| 14 | * |
| 15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 16 | * KIND, either express or implied. |
| 17 | * |
| 18 | ****************************************************************************/ |
| 19 | #include "config.h" |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 20 | #include "system.h" |
Michael Sevakis | c41caca | 2008-05-07 07:14:39 +0000 | [diff] [blame^] | 21 | #include <sprintf.h> |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 22 | #include "kernel.h" |
Michael Sevakis | c41caca | 2008-05-07 07:14:39 +0000 | [diff] [blame^] | 23 | #include "string.h" |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 24 | #include "ata.h" |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 25 | #include "dir.h" |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 26 | #include "disk.h" |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 27 | #include "common.h" |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 28 | #include "usb.h" |
Michael Sevakis | c41caca | 2008-05-07 07:14:39 +0000 | [diff] [blame^] | 29 | #include "font.h" |
| 30 | #include "lcd.h" |
Michael Sevakis | 94f7d0f | 2008-04-18 16:42:50 +0000 | [diff] [blame] | 31 | #include "usb-target.h" |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 32 | |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 33 | #define TAR_CHUNK 512 |
| 34 | #define TAR_HEADER_SIZE 157 |
| 35 | |
Michael Sevakis | c41caca | 2008-05-07 07:14:39 +0000 | [diff] [blame^] | 36 | const char version[] = APPSVERSION; |
| 37 | /* Where files sent via MTP are stored */ |
| 38 | static const char basedir[] = "/Content/0b00/00/"; |
| 39 | /* Can use memory after vector table up to 0x01f00000 */ |
| 40 | static char * const tarbuf = (char *)0x00000040; |
| 41 | static const size_t tarbuf_size = 0x01f00000 - 0x00000040; |
| 42 | /* Queue to get notifications when in USB mode */ |
Michael Sevakis | 94f7d0f | 2008-04-18 16:42:50 +0000 | [diff] [blame] | 43 | static struct event_queue usb_wait_queue; |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 44 | |
Michael Sevakis | c41caca | 2008-05-07 07:14:39 +0000 | [diff] [blame^] | 45 | static void show_splash(int timeout, const char *msg) |
Michael Sevakis | 94f7d0f | 2008-04-18 16:42:50 +0000 | [diff] [blame] | 46 | { |
| 47 | lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2, |
| 48 | (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg); |
| 49 | lcd_update(); |
| 50 | |
| 51 | sleep(timeout); |
| 52 | } |
| 53 | |
Michael Sevakis | c41caca | 2008-05-07 07:14:39 +0000 | [diff] [blame^] | 54 | static void untar(int tar_fd) |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 55 | { |
| 56 | char header[TAR_HEADER_SIZE]; |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 57 | char *ptr; |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 58 | char path[102]; |
Michael Sevakis | c41caca | 2008-05-07 07:14:39 +0000 | [diff] [blame^] | 59 | int fd, i; |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 60 | int ret; |
Michael Sevakis | c41caca | 2008-05-07 07:14:39 +0000 | [diff] [blame^] | 61 | size_t size = filesize(tar_fd); |
| 62 | |
| 63 | if (size > tarbuf_size) { |
| 64 | printf("tar file too large"); /* Paranoid but proper */ |
| 65 | return; |
| 66 | } |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 67 | |
| 68 | ret = read(tar_fd, tarbuf, filesize(tar_fd)); |
| 69 | if (ret < 0) { |
| 70 | printf("couldn't read tar file (%d)", ret); |
| 71 | return; |
| 72 | } |
| 73 | ptr = tarbuf; |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 74 | |
| 75 | while (1) |
| 76 | { |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 77 | memcpy(header, ptr, TAR_HEADER_SIZE); |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 78 | |
| 79 | if (*header == '\0') /* Check for EOF */ |
| 80 | break; |
| 81 | |
| 82 | /* Parse the size field */ |
| 83 | size = 0; |
| 84 | for (i = 124 ; i < 124 + 11 ; i++) { |
| 85 | size = (8 * size) + header[i] - '0'; |
| 86 | } |
| 87 | |
| 88 | /* Skip rest of header */ |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 89 | ptr += TAR_CHUNK; |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 90 | |
| 91 | /* Make the path absolute */ |
| 92 | strcpy(path, "/"); |
| 93 | strcat(path, header); |
| 94 | |
| 95 | if (header[156] == '0') /* file */ |
| 96 | { |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 97 | int wc; |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 98 | |
| 99 | fd = creat(path); |
| 100 | if (fd < 0) |
| 101 | { |
| 102 | printf("failed to create file (%d)", fd); |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 103 | } |
| 104 | else |
| 105 | { |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 106 | wc = write(fd, ptr, size); |
| 107 | if (wc < 0) |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 108 | { |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 109 | printf("write failed (%d)", wc); |
| 110 | break; |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 111 | } |
| 112 | close(fd); |
| 113 | } |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 114 | ptr += (size + TAR_CHUNK-1) & (~(TAR_CHUNK-1)); |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 115 | } |
| 116 | else if (header[156] == '5') /* directory */ |
| 117 | { |
| 118 | int ret; |
| 119 | |
| 120 | /* Remove the trailing slash */ |
| 121 | if (path[strlen(path) - 1] == '/') |
| 122 | path[strlen(path) - 1] = '\0'; |
| 123 | |
| 124 | /* Create the dir */ |
| 125 | ret = mkdir(path); |
| 126 | if (ret < 0 && ret != -4) |
| 127 | { |
| 128 | printf("failed to create dir (%d)", ret); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 134 | void main(void) |
| 135 | { |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 136 | char buf[MAX_PATH]; |
| 137 | char tarstring[6]; |
Nicolas Pennequin | 620e6b4 | 2008-04-15 23:17:03 +0000 | [diff] [blame] | 138 | char model[5]; |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 139 | |
Michael Sevakis | 0218397 | 2008-05-05 13:30:29 +0000 | [diff] [blame] | 140 | /* Flush and invalidate all caches (because vectors were written) */ |
| 141 | invalidate_icache(); |
| 142 | |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 143 | lcd_clear_display(); |
Michael Sevakis | c41caca | 2008-05-07 07:14:39 +0000 | [diff] [blame^] | 144 | printf("Gigabeat S Rockbox Bootloader v.00000013"); |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 145 | system_init(); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 146 | kernel_init(); |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 147 | printf("kernel init done"); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 148 | int rc; |
| 149 | |
Michael Sevakis | ec05b66 | 2008-03-31 06:05:16 +0000 | [diff] [blame] | 150 | enable_interrupt(IRQ_FIQ_STATUS); |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 151 | |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 152 | rc = ata_init(); |
| 153 | if(rc) |
| 154 | { |
| 155 | reset_screen(); |
| 156 | error(EATA, rc); |
| 157 | } |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 158 | printf("ata init done"); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 159 | |
| 160 | disk_init(); |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 161 | printf("disk init done"); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 162 | |
| 163 | rc = disk_mount_all(); |
| 164 | if (rc<=0) |
| 165 | { |
| 166 | error(EDISK,rc); |
| 167 | } |
| 168 | |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 169 | /* Look for a tar file */ |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 170 | struct dirent_uncached* entry; |
| 171 | DIR_UNCACHED* dir; |
| 172 | int fd; |
| 173 | dir = opendir_uncached(basedir); |
| 174 | while ((entry = readdir_uncached(dir))) |
| 175 | { |
| 176 | if (*entry->d_name != '.') |
| 177 | { |
| 178 | snprintf(buf, sizeof(buf), "%s%s", basedir, entry->d_name); |
| 179 | fd = open(buf, O_RDONLY); |
| 180 | if (fd >= 0) |
| 181 | { |
Nicolas Pennequin | 620e6b4 | 2008-04-15 23:17:03 +0000 | [diff] [blame] | 182 | /* Check whether the file is a rockbox binary. */ |
| 183 | lseek(fd, 4, SEEK_SET); |
| 184 | rc = read(fd, model, 4); |
| 185 | if (rc == 4) |
| 186 | { |
| 187 | model[4] = 0; |
| 188 | if (strcmp(model, "gigs") == 0) |
| 189 | { |
| 190 | printf("Found rockbox binary. Moving..."); |
| 191 | close(fd); |
| 192 | remove("/.rockbox/rockbox.gigabeat"); |
| 193 | int ret = rename(buf, "/.rockbox/rockbox.gigabeat"); |
| 194 | printf("returned %d", ret); |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 195 | sleep(HZ); |
Nicolas Pennequin | 620e6b4 | 2008-04-15 23:17:03 +0000 | [diff] [blame] | 196 | break; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /* Check whether the file is a tar file. */ |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 201 | lseek(fd, 257, SEEK_SET); |
| 202 | rc = read(fd, tarstring, 5); |
| 203 | if (rc == 5) |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 204 | { |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 205 | tarstring[5] = 0; |
| 206 | if (strcmp(tarstring, "ustar") == 0) |
| 207 | { |
| 208 | printf("Found tar file. Unarchiving..."); |
| 209 | lseek(fd, 0, SEEK_SET); |
| 210 | untar(fd); |
| 211 | close(fd); |
| 212 | printf("Removing tar file"); |
| 213 | remove(buf); |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 214 | break; |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 215 | } |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 216 | } |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 217 | close(fd); |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | } |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 221 | |
Michael Sevakis | 94f7d0f | 2008-04-18 16:42:50 +0000 | [diff] [blame] | 222 | if (usb_plugged()) |
| 223 | { |
| 224 | /* Enter USB mode */ |
| 225 | struct queue_event ev; |
| 226 | queue_init(&usb_wait_queue, true); |
| 227 | |
| 228 | /* Start the USB driver */ |
| 229 | usb_init(); |
| 230 | usb_start_monitoring(); |
| 231 | |
| 232 | /* Wait for threads to connect or cable is pulled */ |
| 233 | reset_screen(); |
| 234 | show_splash(0, "Waiting for USB"); |
| 235 | |
| 236 | while (1) |
| 237 | { |
| 238 | queue_wait_w_tmo(&usb_wait_queue, &ev, HZ/2); |
| 239 | |
| 240 | if (ev.id == SYS_USB_CONNECTED) |
| 241 | break; /* Hit */ |
| 242 | |
| 243 | if (!usb_plugged()) |
| 244 | break; /* Cable pulled */ |
| 245 | } |
| 246 | |
| 247 | if (ev.id == SYS_USB_CONNECTED) |
| 248 | { |
| 249 | /* Got the message - wait for disconnect */ |
| 250 | reset_screen(); |
| 251 | show_splash(0, "Bootloader USB mode"); |
| 252 | |
| 253 | usb_acknowledge(SYS_USB_CONNECTED_ACK); |
| 254 | usb_wait_for_disconnect(&usb_wait_queue); |
| 255 | } |
| 256 | |
| 257 | /* No more monitoring */ |
| 258 | usb_stop_monitoring(); |
| 259 | |
| 260 | reset_screen(); |
| 261 | } |
Michael Sevakis | 9003dbe | 2008-04-20 03:30:57 +0000 | [diff] [blame] | 262 | else |
| 263 | { |
| 264 | /* Bang on the controller */ |
| 265 | usb_init_device(); |
| 266 | } |
Michael Sevakis | 94f7d0f | 2008-04-18 16:42:50 +0000 | [diff] [blame] | 267 | |
Will Robertson | 32f6109 | 2007-12-23 12:19:40 +0000 | [diff] [blame] | 268 | unsigned char *loadbuffer = (unsigned char *)0x0; |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 269 | int buffer_size = 31*1024*1024; |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 270 | |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 271 | rc = load_firmware(loadbuffer, "/.rockbox/rockbox.gigabeat", buffer_size); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 272 | if(rc < 0) |
Michael Sevakis | 75fe887 | 2008-05-04 03:44:49 +0000 | [diff] [blame] | 273 | error(EBOOTFILE, rc); |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 274 | |
| 275 | system_prepare_fw_start(); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 276 | |
| 277 | if (rc == EOK) |
| 278 | { |
Michael Sevakis | 1f021af | 2008-02-05 04:43:19 +0000 | [diff] [blame] | 279 | invalidate_icache(); |
Michael Sevakis | c41caca | 2008-05-07 07:14:39 +0000 | [diff] [blame^] | 280 | asm volatile ("bx %0": : "r"(loadbuffer)); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 281 | } |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 282 | |
Michael Sevakis | 94f7d0f | 2008-04-18 16:42:50 +0000 | [diff] [blame] | 283 | /* Halt */ |
| 284 | while (1) |
| 285 | core_idle(); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 286 | } |
| 287 | |