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