blob: 30c2955c19c3ff9256e48e0cac2d3328a34df866 [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"
Will Robertson590501c2007-09-21 15:51:53 +000020#include "system.h"
Michael Sevakisc41caca2008-05-07 07:14:39 +000021#include <sprintf.h>
Will Robertson590501c2007-09-21 15:51:53 +000022#include "kernel.h"
Michael Sevakisc41caca2008-05-07 07:14:39 +000023#include "string.h"
Will Robertson590501c2007-09-21 15:51:53 +000024#include "ata.h"
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +000025#include "dir.h"
Will Robertson590501c2007-09-21 15:51:53 +000026#include "disk.h"
Will Robertson590501c2007-09-21 15:51:53 +000027#include "common.h"
Will Robertson590501c2007-09-21 15:51:53 +000028#include "usb.h"
Michael Sevakisc41caca2008-05-07 07:14:39 +000029#include "font.h"
30#include "lcd.h"
Michael Sevakis94f7d0f2008-04-18 16:42:50 +000031#include "usb-target.h"
Will Robertson590501c2007-09-21 15:51:53 +000032
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000033#define TAR_CHUNK 512
34#define TAR_HEADER_SIZE 157
35
Michael Sevakisc41caca2008-05-07 07:14:39 +000036const char version[] = APPSVERSION;
37/* Where files sent via MTP are stored */
38static const char basedir[] = "/Content/0b00/00/";
39/* Can use memory after vector table up to 0x01f00000 */
40static char * const tarbuf = (char *)0x00000040;
41static const size_t tarbuf_size = 0x01f00000 - 0x00000040;
42/* Queue to get notifications when in USB mode */
Michael Sevakis94f7d0f2008-04-18 16:42:50 +000043static struct event_queue usb_wait_queue;
Michael Sevakisa07c0342008-02-08 02:20:05 +000044
Michael Sevakisc41caca2008-05-07 07:14:39 +000045static void show_splash(int timeout, const char *msg)
Michael Sevakis94f7d0f2008-04-18 16:42:50 +000046{
47 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2,
48 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
49 lcd_update();
50
51 sleep(timeout);
52}
53
Michael Sevakisc41caca2008-05-07 07:14:39 +000054static void untar(int tar_fd)
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000055{
56 char header[TAR_HEADER_SIZE];
Nicolas Pennequin90a3d582008-04-16 00:38:06 +000057 char *ptr;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000058 char path[102];
Michael Sevakisc41caca2008-05-07 07:14:39 +000059 int fd, i;
Nicolas Pennequin90a3d582008-04-16 00:38:06 +000060 int ret;
Michael Sevakisc41caca2008-05-07 07:14:39 +000061 size_t size = filesize(tar_fd);
62
63 if (size > tarbuf_size) {
64 printf("tar file too large"); /* Paranoid but proper */
65 return;
66 }
Nicolas Pennequin90a3d582008-04-16 00:38:06 +000067
68 ret = read(tar_fd, tarbuf, filesize(tar_fd));
69 if (ret < 0) {
70 printf("couldn't read tar file (%d)", ret);
71 return;
72 }
73 ptr = tarbuf;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000074
75 while (1)
76 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +000077 memcpy(header, ptr, TAR_HEADER_SIZE);
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000078
79 if (*header == '\0') /* Check for EOF */
80 break;
81
82 /* Parse the size field */
83 size = 0;
84 for (i = 124 ; i < 124 + 11 ; i++) {
85 size = (8 * size) + header[i] - '0';
86 }
87
88 /* Skip rest of header */
Nicolas Pennequin90a3d582008-04-16 00:38:06 +000089 ptr += TAR_CHUNK;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000090
91 /* Make the path absolute */
92 strcpy(path, "/");
93 strcat(path, header);
94
95 if (header[156] == '0') /* file */
96 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +000097 int wc;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000098
99 fd = creat(path);
100 if (fd < 0)
101 {
102 printf("failed to create file (%d)", fd);
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000103 }
104 else
105 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000106 wc = write(fd, ptr, size);
107 if (wc < 0)
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000108 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000109 printf("write failed (%d)", wc);
110 break;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000111 }
112 close(fd);
113 }
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000114 ptr += (size + TAR_CHUNK-1) & (~(TAR_CHUNK-1));
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000115 }
116 else if (header[156] == '5') /* directory */
117 {
118 int ret;
119
120 /* Remove the trailing slash */
121 if (path[strlen(path) - 1] == '/')
122 path[strlen(path) - 1] = '\0';
123
124 /* Create the dir */
125 ret = mkdir(path);
126 if (ret < 0 && ret != -4)
127 {
128 printf("failed to create dir (%d)", ret);
129 }
130 }
131 }
132}
133
Will Robertson590501c2007-09-21 15:51:53 +0000134void main(void)
135{
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000136 char buf[MAX_PATH];
137 char tarstring[6];
Nicolas Pennequin620e6b42008-04-15 23:17:03 +0000138 char model[5];
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000139
Michael Sevakis02183972008-05-05 13:30:29 +0000140 /* Flush and invalidate all caches (because vectors were written) */
141 invalidate_icache();
142
Will Robertson590501c2007-09-21 15:51:53 +0000143 lcd_clear_display();
Michael Sevakisc41caca2008-05-07 07:14:39 +0000144 printf("Gigabeat S Rockbox Bootloader v.00000013");
Michael Sevakisa07c0342008-02-08 02:20:05 +0000145 system_init();
Will Robertson590501c2007-09-21 15:51:53 +0000146 kernel_init();
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000147 printf("kernel init done");
Will Robertson590501c2007-09-21 15:51:53 +0000148 int rc;
149
Michael Sevakisec05b662008-03-31 06:05:16 +0000150 enable_interrupt(IRQ_FIQ_STATUS);
Michael Sevakisa07c0342008-02-08 02:20:05 +0000151
Will Robertson590501c2007-09-21 15:51:53 +0000152 rc = ata_init();
153 if(rc)
154 {
155 reset_screen();
156 error(EATA, rc);
157 }
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000158 printf("ata init done");
Will Robertson590501c2007-09-21 15:51:53 +0000159
160 disk_init();
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000161 printf("disk init done");
Will Robertson590501c2007-09-21 15:51:53 +0000162
163 rc = disk_mount_all();
164 if (rc<=0)
165 {
166 error(EDISK,rc);
167 }
168
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000169 /* Look for a tar file */
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000170 struct dirent_uncached* entry;
171 DIR_UNCACHED* dir;
172 int fd;
173 dir = opendir_uncached(basedir);
174 while ((entry = readdir_uncached(dir)))
175 {
176 if (*entry->d_name != '.')
177 {
178 snprintf(buf, sizeof(buf), "%s%s", basedir, entry->d_name);
179 fd = open(buf, O_RDONLY);
180 if (fd >= 0)
181 {
Nicolas Pennequin620e6b42008-04-15 23:17:03 +0000182 /* Check whether the file is a rockbox binary. */
183 lseek(fd, 4, SEEK_SET);
184 rc = read(fd, model, 4);
185 if (rc == 4)
186 {
187 model[4] = 0;
188 if (strcmp(model, "gigs") == 0)
189 {
190 printf("Found rockbox binary. Moving...");
191 close(fd);
192 remove("/.rockbox/rockbox.gigabeat");
193 int ret = rename(buf, "/.rockbox/rockbox.gigabeat");
194 printf("returned %d", ret);
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000195 sleep(HZ);
Nicolas Pennequin620e6b42008-04-15 23:17:03 +0000196 break;
197 }
198 }
199
200 /* Check whether the file is a tar file. */
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000201 lseek(fd, 257, SEEK_SET);
202 rc = read(fd, tarstring, 5);
203 if (rc == 5)
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000204 {
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000205 tarstring[5] = 0;
206 if (strcmp(tarstring, "ustar") == 0)
207 {
208 printf("Found tar file. Unarchiving...");
209 lseek(fd, 0, SEEK_SET);
210 untar(fd);
211 close(fd);
212 printf("Removing tar file");
213 remove(buf);
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000214 break;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000215 }
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000216 }
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000217 close(fd);
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000218 }
219 }
220 }
Will Robertson590501c2007-09-21 15:51:53 +0000221
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000222 if (usb_plugged())
223 {
224 /* Enter USB mode */
225 struct queue_event ev;
226 queue_init(&usb_wait_queue, true);
227
228 /* Start the USB driver */
229 usb_init();
230 usb_start_monitoring();
231
232 /* Wait for threads to connect or cable is pulled */
233 reset_screen();
234 show_splash(0, "Waiting for USB");
235
236 while (1)
237 {
238 queue_wait_w_tmo(&usb_wait_queue, &ev, HZ/2);
239
240 if (ev.id == SYS_USB_CONNECTED)
241 break; /* Hit */
242
243 if (!usb_plugged())
244 break; /* Cable pulled */
245 }
246
247 if (ev.id == SYS_USB_CONNECTED)
248 {
249 /* Got the message - wait for disconnect */
250 reset_screen();
251 show_splash(0, "Bootloader USB mode");
252
253 usb_acknowledge(SYS_USB_CONNECTED_ACK);
254 usb_wait_for_disconnect(&usb_wait_queue);
255 }
256
257 /* No more monitoring */
258 usb_stop_monitoring();
259
260 reset_screen();
261 }
Michael Sevakis9003dbe2008-04-20 03:30:57 +0000262 else
263 {
264 /* Bang on the controller */
265 usb_init_device();
266 }
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000267
Will Robertson32f61092007-12-23 12:19:40 +0000268 unsigned char *loadbuffer = (unsigned char *)0x0;
Michael Sevakisa07c0342008-02-08 02:20:05 +0000269 int buffer_size = 31*1024*1024;
Will Robertson590501c2007-09-21 15:51:53 +0000270
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000271 rc = load_firmware(loadbuffer, "/.rockbox/rockbox.gigabeat", buffer_size);
Will Robertson590501c2007-09-21 15:51:53 +0000272 if(rc < 0)
Michael Sevakis75fe8872008-05-04 03:44:49 +0000273 error(EBOOTFILE, rc);
Michael Sevakisa07c0342008-02-08 02:20:05 +0000274
275 system_prepare_fw_start();
Will Robertson590501c2007-09-21 15:51:53 +0000276
277 if (rc == EOK)
278 {
Michael Sevakis1f021af2008-02-05 04:43:19 +0000279 invalidate_icache();
Michael Sevakisc41caca2008-05-07 07:14:39 +0000280 asm volatile ("bx %0": : "r"(loadbuffer));
Will Robertson590501c2007-09-21 15:51:53 +0000281 }
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000282
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000283 /* Halt */
284 while (1)
285 core_idle();
Will Robertson590501c2007-09-21 15:51:53 +0000286}
287