blob: 439c81edfba18b378a96435bc18c5ce18b4895b9 [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
Will Robertson590501c2007-09-21 15:51:53 +0000150 lcd_clear_display();
151 printf("Hello world!");
Michael Sevakis8ec1dca2008-04-21 21:45:58 +0000152 printf("Gigabeat S Rockbox Bootloader v.00000010");
Michael Sevakisa07c0342008-02-08 02:20:05 +0000153 system_init();
Will Robertson590501c2007-09-21 15:51:53 +0000154 kernel_init();
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000155 printf("kernel init done");
Will Robertson590501c2007-09-21 15:51:53 +0000156 int rc;
157
Michael Sevakisec05b662008-03-31 06:05:16 +0000158 enable_interrupt(IRQ_FIQ_STATUS);
Michael Sevakisa07c0342008-02-08 02:20:05 +0000159
Will Robertson590501c2007-09-21 15:51:53 +0000160 rc = ata_init();
161 if(rc)
162 {
163 reset_screen();
164 error(EATA, rc);
165 }
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000166 printf("ata init done");
Will Robertson590501c2007-09-21 15:51:53 +0000167
168 disk_init();
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000169 printf("disk init done");
Will Robertson590501c2007-09-21 15:51:53 +0000170
171 rc = disk_mount_all();
172 if (rc<=0)
173 {
174 error(EDISK,rc);
175 }
176
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000177 /* Look for a tar file */
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000178 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 Pennequin620e6b42008-04-15 23:17:03 +0000190 /* 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 Pennequin90a3d582008-04-16 00:38:06 +0000203 sleep(HZ);
Nicolas Pennequin620e6b42008-04-15 23:17:03 +0000204 break;
205 }
206 }
207
208 /* Check whether the file is a tar file. */
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000209 lseek(fd, 257, SEEK_SET);
210 rc = read(fd, tarstring, 5);
211 if (rc == 5)
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000212 {
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000213 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 Pennequinc2ca8c72007-11-27 15:40:29 +0000222 break;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000223 }
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000224 }
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000225 close(fd);
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000226 }
227 }
228 }
Will Robertson590501c2007-09-21 15:51:53 +0000229
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000230 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 Sevakis9003dbe2008-04-20 03:30:57 +0000270 else
271 {
272 /* Bang on the controller */
273 usb_init_device();
274 }
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000275
Will Robertson32f61092007-12-23 12:19:40 +0000276 unsigned char *loadbuffer = (unsigned char *)0x0;
Michael Sevakisa07c0342008-02-08 02:20:05 +0000277 int buffer_size = 31*1024*1024;
Will Robertson590501c2007-09-21 15:51:53 +0000278
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000279 rc = load_firmware(loadbuffer, "/.rockbox/rockbox.gigabeat", buffer_size);
Will Robertson590501c2007-09-21 15:51:53 +0000280 if(rc < 0)
Michael Sevakisa07c0342008-02-08 02:20:05 +0000281 error((int)buf, rc);
282
283 system_prepare_fw_start();
Will Robertson590501c2007-09-21 15:51:53 +0000284
285 if (rc == EOK)
286 {
287 kernel_entry = (void*) loadbuffer;
Michael Sevakis1f021af2008-02-05 04:43:19 +0000288 invalidate_icache();
Will Robertson590501c2007-09-21 15:51:53 +0000289 rc = kernel_entry();
290 }
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000291
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000292 /* Halt */
293 while (1)
294 core_idle();
Will Robertson590501c2007-09-21 15:51:53 +0000295}
296