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 | |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 150 | lcd_clear_display(); |
| 151 | printf("Hello world!"); |
Michael Sevakis | 8ec1dca | 2008-04-21 21:45:58 +0000 | [diff] [blame] | 152 | printf("Gigabeat S Rockbox Bootloader v.00000010"); |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 153 | system_init(); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 154 | kernel_init(); |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 155 | printf("kernel init done"); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 156 | int rc; |
| 157 | |
Michael Sevakis | ec05b66 | 2008-03-31 06:05:16 +0000 | [diff] [blame] | 158 | enable_interrupt(IRQ_FIQ_STATUS); |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 159 | |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 160 | rc = ata_init(); |
| 161 | if(rc) |
| 162 | { |
| 163 | reset_screen(); |
| 164 | error(EATA, rc); |
| 165 | } |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 166 | printf("ata init done"); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 167 | |
| 168 | disk_init(); |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 169 | printf("disk init done"); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 170 | |
| 171 | rc = disk_mount_all(); |
| 172 | if (rc<=0) |
| 173 | { |
| 174 | error(EDISK,rc); |
| 175 | } |
| 176 | |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 177 | /* Look for a tar file */ |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 178 | struct dirent_uncached* entry; |
| 179 | DIR_UNCACHED* dir; |
| 180 | int fd; |
| 181 | dir = opendir_uncached(basedir); |
| 182 | while ((entry = readdir_uncached(dir))) |
| 183 | { |
| 184 | if (*entry->d_name != '.') |
| 185 | { |
| 186 | snprintf(buf, sizeof(buf), "%s%s", basedir, entry->d_name); |
| 187 | fd = open(buf, O_RDONLY); |
| 188 | if (fd >= 0) |
| 189 | { |
Nicolas Pennequin | 620e6b4 | 2008-04-15 23:17:03 +0000 | [diff] [blame] | 190 | /* Check whether the file is a rockbox binary. */ |
| 191 | lseek(fd, 4, SEEK_SET); |
| 192 | rc = read(fd, model, 4); |
| 193 | if (rc == 4) |
| 194 | { |
| 195 | model[4] = 0; |
| 196 | if (strcmp(model, "gigs") == 0) |
| 197 | { |
| 198 | printf("Found rockbox binary. Moving..."); |
| 199 | close(fd); |
| 200 | remove("/.rockbox/rockbox.gigabeat"); |
| 201 | int ret = rename(buf, "/.rockbox/rockbox.gigabeat"); |
| 202 | printf("returned %d", ret); |
Nicolas Pennequin | 90a3d58 | 2008-04-16 00:38:06 +0000 | [diff] [blame] | 203 | sleep(HZ); |
Nicolas Pennequin | 620e6b4 | 2008-04-15 23:17:03 +0000 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /* Check whether the file is a tar file. */ |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 209 | lseek(fd, 257, SEEK_SET); |
| 210 | rc = read(fd, tarstring, 5); |
| 211 | if (rc == 5) |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 212 | { |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 213 | tarstring[5] = 0; |
| 214 | if (strcmp(tarstring, "ustar") == 0) |
| 215 | { |
| 216 | printf("Found tar file. Unarchiving..."); |
| 217 | lseek(fd, 0, SEEK_SET); |
| 218 | untar(fd); |
| 219 | close(fd); |
| 220 | printf("Removing tar file"); |
| 221 | remove(buf); |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 222 | break; |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 223 | } |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 224 | } |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 225 | close(fd); |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | } |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 229 | |
Michael Sevakis | 94f7d0f | 2008-04-18 16:42:50 +0000 | [diff] [blame] | 230 | if (usb_plugged()) |
| 231 | { |
| 232 | /* Enter USB mode */ |
| 233 | struct queue_event ev; |
| 234 | queue_init(&usb_wait_queue, true); |
| 235 | |
| 236 | /* Start the USB driver */ |
| 237 | usb_init(); |
| 238 | usb_start_monitoring(); |
| 239 | |
| 240 | /* Wait for threads to connect or cable is pulled */ |
| 241 | reset_screen(); |
| 242 | show_splash(0, "Waiting for USB"); |
| 243 | |
| 244 | while (1) |
| 245 | { |
| 246 | queue_wait_w_tmo(&usb_wait_queue, &ev, HZ/2); |
| 247 | |
| 248 | if (ev.id == SYS_USB_CONNECTED) |
| 249 | break; /* Hit */ |
| 250 | |
| 251 | if (!usb_plugged()) |
| 252 | break; /* Cable pulled */ |
| 253 | } |
| 254 | |
| 255 | if (ev.id == SYS_USB_CONNECTED) |
| 256 | { |
| 257 | /* Got the message - wait for disconnect */ |
| 258 | reset_screen(); |
| 259 | show_splash(0, "Bootloader USB mode"); |
| 260 | |
| 261 | usb_acknowledge(SYS_USB_CONNECTED_ACK); |
| 262 | usb_wait_for_disconnect(&usb_wait_queue); |
| 263 | } |
| 264 | |
| 265 | /* No more monitoring */ |
| 266 | usb_stop_monitoring(); |
| 267 | |
| 268 | reset_screen(); |
| 269 | } |
Michael Sevakis | 9003dbe | 2008-04-20 03:30:57 +0000 | [diff] [blame] | 270 | else |
| 271 | { |
| 272 | /* Bang on the controller */ |
| 273 | usb_init_device(); |
| 274 | } |
Michael Sevakis | 94f7d0f | 2008-04-18 16:42:50 +0000 | [diff] [blame] | 275 | |
Will Robertson | 32f6109 | 2007-12-23 12:19:40 +0000 | [diff] [blame] | 276 | unsigned char *loadbuffer = (unsigned char *)0x0; |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 277 | int buffer_size = 31*1024*1024; |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 278 | |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame] | 279 | rc = load_firmware(loadbuffer, "/.rockbox/rockbox.gigabeat", buffer_size); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 280 | if(rc < 0) |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 281 | error((int)buf, rc); |
| 282 | |
| 283 | system_prepare_fw_start(); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 284 | |
| 285 | if (rc == EOK) |
| 286 | { |
| 287 | kernel_entry = (void*) loadbuffer; |
Michael Sevakis | 1f021af | 2008-02-05 04:43:19 +0000 | [diff] [blame] | 288 | invalidate_icache(); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 289 | rc = kernel_entry(); |
| 290 | } |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 291 | |
Michael Sevakis | 94f7d0f | 2008-04-18 16:42:50 +0000 | [diff] [blame] | 292 | /* Halt */ |
| 293 | while (1) |
| 294 | core_idle(); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 295 | } |
| 296 | |