blob: abfe57ce1f73239b83742487450dda0847a626c0 [file] [log] [blame]
Will Robertson590501c2007-09-21 15:51:53 +00001/***************************************************************************
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 Pennequinc2ca8c72007-11-27 15:40:29 +000031#include "dir.h"
Will Robertson590501c2007-09-21 15:51:53 +000032#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 Sevakis94f7d0f2008-04-18 16:42:50 +000049#include "usb-target.h"
Will Robertson590501c2007-09-21 15:51:53 +000050
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000051#define TAR_CHUNK 512
52#define TAR_HEADER_SIZE 157
53
Will Robertson590501c2007-09-21 15:51:53 +000054char version[] = APPSVERSION;
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +000055char basedir[] = "/Content/0b00/00/"; /* Where files sent via MTP are stored */
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +000056int (*kernel_entry)(void);
Nicolas Pennequin90a3d582008-04-16 00:38:06 +000057char *tarbuf = (char *)0x00000040;
Michael Sevakisa07c0342008-02-08 02:20:05 +000058extern void reference_system_c(void);
Michael Sevakis94f7d0f2008-04-18 16:42:50 +000059static struct event_queue usb_wait_queue;
Michael Sevakisa07c0342008-02-08 02:20:05 +000060
Michael Sevakis94f7d0f2008-04-18 16:42:50 +000061void 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 Pennequine2f5f212008-02-17 23:17:08 +000070void untar(int tar_fd)
71{
72 char header[TAR_HEADER_SIZE];
Nicolas Pennequin90a3d582008-04-16 00:38:06 +000073 char *ptr;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000074 char path[102];
Nicolas Pennequin90a3d582008-04-16 00:38:06 +000075 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 Pennequine2f5f212008-02-17 23:17:08 +000084
85 while (1)
86 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +000087 memcpy(header, ptr, TAR_HEADER_SIZE);
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000088
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 Pennequin90a3d582008-04-16 00:38:06 +000099 ptr += TAR_CHUNK;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000100
101 /* Make the path absolute */
102 strcpy(path, "/");
103 strcat(path, header);
104
105 if (header[156] == '0') /* file */
106 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000107 int wc;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000108
109 fd = creat(path);
110 if (fd < 0)
111 {
112 printf("failed to create file (%d)", fd);
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000113 }
114 else
115 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000116 wc = write(fd, ptr, size);
117 if (wc < 0)
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000118 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000119 printf("write failed (%d)", wc);
120 break;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000121 }
122 close(fd);
123 }
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000124 ptr += (size + TAR_CHUNK-1) & (~(TAR_CHUNK-1));
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000125 }
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 Robertson590501c2007-09-21 15:51:53 +0000144void main(void)
145{
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000146 char buf[MAX_PATH];
147 char tarstring[6];
Nicolas Pennequin620e6b42008-04-15 23:17:03 +0000148 char model[5];
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000149
Michael Sevakis02183972008-05-05 13:30:29 +0000150 /* Flush and invalidate all caches (because vectors were written) */
151 invalidate_icache();
152
Will Robertson590501c2007-09-21 15:51:53 +0000153 lcd_clear_display();
Michael Sevakis02183972008-05-05 13:30:29 +0000154 printf("Gigabeat S Rockbox Bootloader v.00000012");
Michael Sevakisa07c0342008-02-08 02:20:05 +0000155 system_init();
Will Robertson590501c2007-09-21 15:51:53 +0000156 kernel_init();
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000157 printf("kernel init done");
Will Robertson590501c2007-09-21 15:51:53 +0000158 int rc;
159
Michael Sevakisec05b662008-03-31 06:05:16 +0000160 enable_interrupt(IRQ_FIQ_STATUS);
Michael Sevakisa07c0342008-02-08 02:20:05 +0000161
Will Robertson590501c2007-09-21 15:51:53 +0000162 rc = ata_init();
163 if(rc)
164 {
165 reset_screen();
166 error(EATA, rc);
167 }
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000168 printf("ata init done");
Will Robertson590501c2007-09-21 15:51:53 +0000169
170 disk_init();
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000171 printf("disk init done");
Will Robertson590501c2007-09-21 15:51:53 +0000172
173 rc = disk_mount_all();
174 if (rc<=0)
175 {
176 error(EDISK,rc);
177 }
178
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000179 /* Look for a tar file */
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000180 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 Pennequin620e6b42008-04-15 23:17:03 +0000192 /* 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 Pennequin90a3d582008-04-16 00:38:06 +0000205 sleep(HZ);
Nicolas Pennequin620e6b42008-04-15 23:17:03 +0000206 break;
207 }
208 }
209
210 /* Check whether the file is a tar file. */
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000211 lseek(fd, 257, SEEK_SET);
212 rc = read(fd, tarstring, 5);
213 if (rc == 5)
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000214 {
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000215 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 Pennequinc2ca8c72007-11-27 15:40:29 +0000224 break;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000225 }
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000226 }
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000227 close(fd);
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000228 }
229 }
230 }
Will Robertson590501c2007-09-21 15:51:53 +0000231
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000232 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 Sevakis9003dbe2008-04-20 03:30:57 +0000272 else
273 {
274 /* Bang on the controller */
275 usb_init_device();
276 }
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000277
Will Robertson32f61092007-12-23 12:19:40 +0000278 unsigned char *loadbuffer = (unsigned char *)0x0;
Michael Sevakisa07c0342008-02-08 02:20:05 +0000279 int buffer_size = 31*1024*1024;
Will Robertson590501c2007-09-21 15:51:53 +0000280
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000281 rc = load_firmware(loadbuffer, "/.rockbox/rockbox.gigabeat", buffer_size);
Will Robertson590501c2007-09-21 15:51:53 +0000282 if(rc < 0)
Michael Sevakis75fe8872008-05-04 03:44:49 +0000283 error(EBOOTFILE, rc);
Michael Sevakisa07c0342008-02-08 02:20:05 +0000284
285 system_prepare_fw_start();
Will Robertson590501c2007-09-21 15:51:53 +0000286
287 if (rc == EOK)
288 {
289 kernel_entry = (void*) loadbuffer;
Michael Sevakis1f021af2008-02-05 04:43:19 +0000290 invalidate_icache();
Will Robertson590501c2007-09-21 15:51:53 +0000291 rc = kernel_entry();
292 }
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000293
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000294 /* Halt */
295 while (1)
296 core_idle();
Will Robertson590501c2007-09-21 15:51:53 +0000297}
298