blob: dbacaaa901f3741d81a14538cd81c0190959b1c8 [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
61/* Dummy stub that creates C references for C functions only used by
62 assembly - never called */
63void reference_files(void)
64{
65 reference_system_c();
66}
Will Robertson590501c2007-09-21 15:51:53 +000067
Michael Sevakis94f7d0f2008-04-18 16:42:50 +000068void show_splash(int timeout, const char *msg)
69{
70 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2,
71 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
72 lcd_update();
73
74 sleep(timeout);
75}
76
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000077void untar(int tar_fd)
78{
79 char header[TAR_HEADER_SIZE];
Nicolas Pennequin90a3d582008-04-16 00:38:06 +000080 char *ptr;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000081 char path[102];
Nicolas Pennequin90a3d582008-04-16 00:38:06 +000082 int fd, i, size = 0;
83 int ret;
84
85 ret = read(tar_fd, tarbuf, filesize(tar_fd));
86 if (ret < 0) {
87 printf("couldn't read tar file (%d)", ret);
88 return;
89 }
90 ptr = tarbuf;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000091
92 while (1)
93 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +000094 memcpy(header, ptr, TAR_HEADER_SIZE);
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000095
96 if (*header == '\0') /* Check for EOF */
97 break;
98
99 /* Parse the size field */
100 size = 0;
101 for (i = 124 ; i < 124 + 11 ; i++) {
102 size = (8 * size) + header[i] - '0';
103 }
104
105 /* Skip rest of header */
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000106 ptr += TAR_CHUNK;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000107
108 /* Make the path absolute */
109 strcpy(path, "/");
110 strcat(path, header);
111
112 if (header[156] == '0') /* file */
113 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000114 int wc;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000115
116 fd = creat(path);
117 if (fd < 0)
118 {
119 printf("failed to create file (%d)", fd);
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000120 }
121 else
122 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000123 wc = write(fd, ptr, size);
124 if (wc < 0)
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000125 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000126 printf("write failed (%d)", wc);
127 break;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000128 }
129 close(fd);
130 }
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000131 ptr += (size + TAR_CHUNK-1) & (~(TAR_CHUNK-1));
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000132 }
133 else if (header[156] == '5') /* directory */
134 {
135 int ret;
136
137 /* Remove the trailing slash */
138 if (path[strlen(path) - 1] == '/')
139 path[strlen(path) - 1] = '\0';
140
141 /* Create the dir */
142 ret = mkdir(path);
143 if (ret < 0 && ret != -4)
144 {
145 printf("failed to create dir (%d)", ret);
146 }
147 }
148 }
149}
150
Will Robertson590501c2007-09-21 15:51:53 +0000151void main(void)
152{
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000153 char buf[MAX_PATH];
154 char tarstring[6];
Nicolas Pennequin620e6b42008-04-15 23:17:03 +0000155 char model[5];
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000156
Will Robertson590501c2007-09-21 15:51:53 +0000157 lcd_clear_display();
158 printf("Hello world!");
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000159 printf("Gigabeat S Rockbox Bootloader v.00000007");
Michael Sevakisa07c0342008-02-08 02:20:05 +0000160 system_init();
Will Robertson590501c2007-09-21 15:51:53 +0000161 kernel_init();
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000162 printf("kernel init done");
Will Robertson590501c2007-09-21 15:51:53 +0000163 int rc;
164
Michael Sevakisec05b662008-03-31 06:05:16 +0000165 enable_interrupt(IRQ_FIQ_STATUS);
Michael Sevakisa07c0342008-02-08 02:20:05 +0000166
Will Robertson590501c2007-09-21 15:51:53 +0000167 rc = ata_init();
168 if(rc)
169 {
170 reset_screen();
171 error(EATA, rc);
172 }
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000173 printf("ata init done");
Will Robertson590501c2007-09-21 15:51:53 +0000174
175 disk_init();
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000176 printf("disk init done");
Will Robertson590501c2007-09-21 15:51:53 +0000177
178 rc = disk_mount_all();
179 if (rc<=0)
180 {
181 error(EDISK,rc);
182 }
183
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000184 /* Look for a tar file */
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000185 struct dirent_uncached* entry;
186 DIR_UNCACHED* dir;
187 int fd;
188 dir = opendir_uncached(basedir);
189 while ((entry = readdir_uncached(dir)))
190 {
191 if (*entry->d_name != '.')
192 {
193 snprintf(buf, sizeof(buf), "%s%s", basedir, entry->d_name);
194 fd = open(buf, O_RDONLY);
195 if (fd >= 0)
196 {
Nicolas Pennequin620e6b42008-04-15 23:17:03 +0000197 /* Check whether the file is a rockbox binary. */
198 lseek(fd, 4, SEEK_SET);
199 rc = read(fd, model, 4);
200 if (rc == 4)
201 {
202 model[4] = 0;
203 if (strcmp(model, "gigs") == 0)
204 {
205 printf("Found rockbox binary. Moving...");
206 close(fd);
207 remove("/.rockbox/rockbox.gigabeat");
208 int ret = rename(buf, "/.rockbox/rockbox.gigabeat");
209 printf("returned %d", ret);
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000210 sleep(HZ);
Nicolas Pennequin620e6b42008-04-15 23:17:03 +0000211 break;
212 }
213 }
214
215 /* Check whether the file is a tar file. */
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000216 lseek(fd, 257, SEEK_SET);
217 rc = read(fd, tarstring, 5);
218 if (rc == 5)
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000219 {
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000220 tarstring[5] = 0;
221 if (strcmp(tarstring, "ustar") == 0)
222 {
223 printf("Found tar file. Unarchiving...");
224 lseek(fd, 0, SEEK_SET);
225 untar(fd);
226 close(fd);
227 printf("Removing tar file");
228 remove(buf);
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000229 break;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000230 }
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000231 }
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000232 close(fd);
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000233 }
234 }
235 }
Will Robertson590501c2007-09-21 15:51:53 +0000236
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000237 if (usb_plugged())
238 {
239 /* Enter USB mode */
240 struct queue_event ev;
241 queue_init(&usb_wait_queue, true);
242
243 /* Start the USB driver */
244 usb_init();
245 usb_start_monitoring();
246
247 /* Wait for threads to connect or cable is pulled */
248 reset_screen();
249 show_splash(0, "Waiting for USB");
250
251 while (1)
252 {
253 queue_wait_w_tmo(&usb_wait_queue, &ev, HZ/2);
254
255 if (ev.id == SYS_USB_CONNECTED)
256 break; /* Hit */
257
258 if (!usb_plugged())
259 break; /* Cable pulled */
260 }
261
262 if (ev.id == SYS_USB_CONNECTED)
263 {
264 /* Got the message - wait for disconnect */
265 reset_screen();
266 show_splash(0, "Bootloader USB mode");
267
268 usb_acknowledge(SYS_USB_CONNECTED_ACK);
269 usb_wait_for_disconnect(&usb_wait_queue);
270 }
271
272 /* No more monitoring */
273 usb_stop_monitoring();
274
275 reset_screen();
276 }
277
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 Sevakisa07c0342008-02-08 02:20:05 +0000283 error((int)buf, rc);
284
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