blob: dcff26c4dec8b6013f73b742e687e9adf1ba581c [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 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
Will Robertson590501c2007-09-21 15:51:53 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include "config.h"
Will Robertson590501c2007-09-21 15:51:53 +000022#include "system.h"
Frank Gevaertsab4a1622010-05-06 22:13:54 +000023#include <stdio.h>
Will Robertson590501c2007-09-21 15:51:53 +000024#include "kernel.h"
Michael Sevakisc41caca2008-05-07 07:14:39 +000025#include "string.h"
Michael Sevakis80278e42008-05-10 18:00:11 +000026#include "adc.h"
27#include "powermgmt.h"
Frank Gevaerts2f8a0082008-11-01 16:14:28 +000028#include "storage.h"
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +000029#include "dir.h"
Will Robertson590501c2007-09-21 15:51:53 +000030#include "disk.h"
Will Robertson590501c2007-09-21 15:51:53 +000031#include "common.h"
Michael Sevakis80278e42008-05-10 18:00:11 +000032#include "backlight.h"
Will Robertson590501c2007-09-21 15:51:53 +000033#include "usb.h"
Michael Sevakis80278e42008-05-10 18:00:11 +000034#include "button.h"
Michael Sevakisc41caca2008-05-07 07:14:39 +000035#include "font.h"
36#include "lcd.h"
Michael Sevakis94f7d0f2008-04-18 16:42:50 +000037#include "usb-target.h"
Rafaël Carré5d236b22010-05-27 09:41:46 +000038#include "version.h"
Will Robertson590501c2007-09-21 15:51:53 +000039
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000040#define TAR_CHUNK 512
41#define TAR_HEADER_SIZE 157
42
Michael Sevakisc41caca2008-05-07 07:14:39 +000043/* Where files sent via MTP are stored */
44static const char basedir[] = "/Content/0b00/00/";
45/* Can use memory after vector table up to 0x01f00000 */
46static char * const tarbuf = (char *)0x00000040;
47static const size_t tarbuf_size = 0x01f00000 - 0x00000040;
Michael Sevakis80278e42008-05-10 18:00:11 +000048/* Firmware data */
49static void * const load_buf = 0x00000000;
50static const size_t load_buf_size = 0x20000000 - 0x100000;
51static const void * const start_addr = 0x00000000;
Michael Sevakisa07c0342008-02-08 02:20:05 +000052
Michael Sevakisc41caca2008-05-07 07:14:39 +000053static void show_splash(int timeout, const char *msg)
Michael Sevakis94f7d0f2008-04-18 16:42:50 +000054{
Michael Sevakis80278e42008-05-10 18:00:11 +000055 backlight_on();
56 reset_screen();
Michael Sevakis94f7d0f2008-04-18 16:42:50 +000057 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2,
58 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
59 lcd_update();
60
61 sleep(timeout);
62}
63
Michael Sevakis80278e42008-05-10 18:00:11 +000064static bool pause_if_button_pressed(bool pre_usb)
65{
66 while (1)
67 {
68 int button = button_read_device();
69
70 if (pre_usb && !usb_plugged())
71 return false;
72
73 /* Exit if no button or only the menu (settings reset) button */
74 switch (button)
75 {
76 case BUTTON_MENU:
77 case BUTTON_NONE:
78 return true;
79 }
80
81 sleep(HZ/5);
82
83 /* If the disk powers off, the firmware will lock at startup */
Frank Gevaerts2f8a0082008-11-01 16:14:28 +000084 storage_spin();
Michael Sevakis80278e42008-05-10 18:00:11 +000085 }
86}
87
88/* TODO: Handle charging while connected */
89static void handle_usb(void)
90{
91 int button;
92
93 /* Check if plugged and pause to look at messages. If the cable was pulled
94 * while waiting, proceed as if it never was plugged. */
95 if (!usb_plugged() || !pause_if_button_pressed(true))
Michael Sevakis80278e42008-05-10 18:00:11 +000096 return;
Michael Sevakis80278e42008-05-10 18:00:11 +000097
98 /** Enter USB mode **/
99
100 /* We need full button and backlight handling now */
101 backlight_init();
102 button_init();
103
104 /* Start the USB driver */
105 usb_init();
106 usb_start_monitoring();
107
108 /* Wait for threads to connect or cable is pulled */
109 show_splash(HZ/2, "Waiting for USB");
110
111 while (1)
112 {
113 button = button_get_w_tmo(HZ/2);
114
115 if (button == SYS_USB_CONNECTED)
116 break; /* Hit */
117
118 if (!usb_plugged())
119 break; /* Cable pulled */
120 }
121
122 if (button == SYS_USB_CONNECTED)
123 {
124 /* Got the message - wait for disconnect */
125 show_splash(0, "Bootloader USB mode");
126
127 usb_acknowledge(SYS_USB_CONNECTED_ACK);
128
129 while (1)
130 {
131 button = button_get(true);
132 if (button == SYS_USB_DISCONNECTED)
133 {
134 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
135 break;
136 }
137 }
138 }
139
140 /* Put drivers initialized for USB connection into a known state */
141 backlight_on();
142 usb_close();
143 button_close();
144 backlight_close();
145
Michael Sevakis0defb842010-05-06 04:45:05 +0000146 /* Sleep a little to let the backlight ramp up */
147 sleep(HZ*5/4);
148
Michael Sevakis80278e42008-05-10 18:00:11 +0000149 reset_screen();
150}
151
Michael Sevakisc41caca2008-05-07 07:14:39 +0000152static void untar(int tar_fd)
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000153{
154 char header[TAR_HEADER_SIZE];
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000155 char *ptr;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000156 char path[102];
Michael Sevakisc41caca2008-05-07 07:14:39 +0000157 int fd, i;
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000158 int ret;
Michael Sevakisc41caca2008-05-07 07:14:39 +0000159 size_t size = filesize(tar_fd);
160
Michael Sevakis80278e42008-05-10 18:00:11 +0000161 if (size > tarbuf_size)
162 {
Michael Sevakisc41caca2008-05-07 07:14:39 +0000163 printf("tar file too large"); /* Paranoid but proper */
164 return;
165 }
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000166
167 ret = read(tar_fd, tarbuf, filesize(tar_fd));
Michael Sevakis80278e42008-05-10 18:00:11 +0000168 if (ret < 0)
169 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000170 printf("couldn't read tar file (%d)", ret);
171 return;
172 }
173 ptr = tarbuf;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000174
175 while (1)
176 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000177 memcpy(header, ptr, TAR_HEADER_SIZE);
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000178
179 if (*header == '\0') /* Check for EOF */
180 break;
181
182 /* Parse the size field */
183 size = 0;
184 for (i = 124 ; i < 124 + 11 ; i++) {
185 size = (8 * size) + header[i] - '0';
186 }
187
188 /* Skip rest of header */
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000189 ptr += TAR_CHUNK;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000190
191 /* Make the path absolute */
192 strcpy(path, "/");
193 strcat(path, header);
194
195 if (header[156] == '0') /* file */
196 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000197 int wc;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000198
Thomas Martitzc61e89c2010-05-06 17:35:04 +0000199 fd = creat(path, 0666);
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000200 if (fd < 0)
201 {
202 printf("failed to create file (%d)", fd);
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000203 }
204 else
205 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000206 wc = write(fd, ptr, size);
207 if (wc < 0)
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000208 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000209 printf("write failed (%d)", wc);
210 break;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000211 }
212 close(fd);
213 }
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000214 ptr += (size + TAR_CHUNK-1) & (~(TAR_CHUNK-1));
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000215 }
216 else if (header[156] == '5') /* directory */
217 {
218 int ret;
219
220 /* Remove the trailing slash */
221 if (path[strlen(path) - 1] == '/')
222 path[strlen(path) - 1] = '\0';
223
224 /* Create the dir */
225 ret = mkdir(path);
226 if (ret < 0 && ret != -4)
227 {
228 printf("failed to create dir (%d)", ret);
229 }
230 }
231 }
232}
233
Michael Sevakis80278e42008-05-10 18:00:11 +0000234/* Look for a tar file or rockbox binary in the MTP directory */
235static void handle_untar(void)
Will Robertson590501c2007-09-21 15:51:53 +0000236{
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000237 char buf[MAX_PATH];
238 char tarstring[6];
Nicolas Pennequin620e6b42008-04-15 23:17:03 +0000239 char model[5];
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000240 struct dirent_uncached* entry;
241 DIR_UNCACHED* dir;
242 int fd;
Michael Sevakis80278e42008-05-10 18:00:11 +0000243 int rc;
244
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000245 dir = opendir_uncached(basedir);
Michael Sevakis80278e42008-05-10 18:00:11 +0000246
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000247 while ((entry = readdir_uncached(dir)))
248 {
Michael Sevakis80278e42008-05-10 18:00:11 +0000249 if (*entry->d_name == '.')
250 continue;
Nicolas Pennequin620e6b42008-04-15 23:17:03 +0000251
Michael Sevakis80278e42008-05-10 18:00:11 +0000252 snprintf(buf, sizeof(buf), "%s%s", basedir, entry->d_name);
253 fd = open(buf, O_RDONLY);
254
255 if (fd < 0)
256 continue;
257
258 /* Check whether the file is a rockbox binary. */
259 lseek(fd, 4, SEEK_SET);
260 rc = read(fd, model, 4);
261 if (rc == 4)
262 {
263 model[4] = 0;
264 if (strcmp(model, "gigs") == 0)
265 {
266 printf("Found rockbox binary. Moving...");
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000267 close(fd);
Björn Stenbergb69be102008-11-23 22:07:48 +0000268 remove( BOOTDIR "/" BOOTFILE);
269 int ret = rename(buf, BOOTDIR "/" BOOTFILE);
Michael Sevakis80278e42008-05-10 18:00:11 +0000270 printf("returned %d", ret);
271 sleep(HZ);
272 break;
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000273 }
274 }
Will Robertson590501c2007-09-21 15:51:53 +0000275
Michael Sevakis80278e42008-05-10 18:00:11 +0000276 /* Check whether the file is a tar file. */
277 lseek(fd, 257, SEEK_SET);
278 rc = read(fd, tarstring, 5);
279 if (rc == 5)
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000280 {
Michael Sevakis80278e42008-05-10 18:00:11 +0000281 tarstring[5] = 0;
282 if (strcmp(tarstring, "ustar") == 0)
283 {
284 printf("Found tar file. Unarchiving...");
285 lseek(fd, 0, SEEK_SET);
286 untar(fd);
287 close(fd);
288 printf("Removing tar file");
289 remove(buf);
290 break;
291 }
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000292 }
293
Michael Sevakis80278e42008-05-10 18:00:11 +0000294 close(fd);
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000295 }
Michael Sevakis80278e42008-05-10 18:00:11 +0000296}
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000297
Michael Sevakis80278e42008-05-10 18:00:11 +0000298/* Try to load the firmware and run it */
299static void __attribute__((noreturn)) handle_firmware_load(void)
300{
Björn Stenbergb69be102008-11-23 22:07:48 +0000301 int rc = load_firmware(load_buf, BOOTFILE,
Michael Sevakis80278e42008-05-10 18:00:11 +0000302 load_buf_size);
Will Robertson590501c2007-09-21 15:51:53 +0000303
Will Robertson590501c2007-09-21 15:51:53 +0000304 if(rc < 0)
Michael Sevakis75fe8872008-05-04 03:44:49 +0000305 error(EBOOTFILE, rc);
Michael Sevakisa07c0342008-02-08 02:20:05 +0000306
Michael Sevakis80278e42008-05-10 18:00:11 +0000307 /* Pause to look at messages */
308 pause_if_button_pressed(false);
309
310 /* Put drivers into a known state */
311 button_close_device();
Frank Gevaerts2f8a0082008-11-01 16:14:28 +0000312 storage_close();
Michael Sevakisa07c0342008-02-08 02:20:05 +0000313 system_prepare_fw_start();
Will Robertson590501c2007-09-21 15:51:53 +0000314
315 if (rc == EOK)
316 {
Michael Sevakis21f0c9a2009-02-11 12:55:51 +0000317 cpucache_invalidate();
Michael Sevakis80278e42008-05-10 18:00:11 +0000318 asm volatile ("bx %0": : "r"(start_addr));
Will Robertson590501c2007-09-21 15:51:53 +0000319 }
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000320
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000321 /* Halt */
322 while (1)
323 core_idle();
Will Robertson590501c2007-09-21 15:51:53 +0000324}
325
Michael Sevakis80278e42008-05-10 18:00:11 +0000326static void check_battery(void)
327{
328 int batt = battery_adc_voltage();
329 printf("Battery: %d.%03d V", batt / 1000, batt % 1000);
330 /* TODO: warn on low battery or shut down */
331}
332
333void main(void)
334{
335 int rc;
336
337 /* Flush and invalidate all caches (because vectors were written) */
Michael Sevakis21f0c9a2009-02-11 12:55:51 +0000338 cpucache_invalidate();
Michael Sevakis80278e42008-05-10 18:00:11 +0000339
Michael Sevakis80278e42008-05-10 18:00:11 +0000340 system_init();
341 kernel_init();
342
343 enable_interrupt(IRQ_FIQ_STATUS);
344
Michael Sevakis4d3a0202009-02-07 10:09:13 +0000345 lcd_init_device();
346 lcd_clear_display();
347
348 printf("Gigabeat S Rockbox Bootloader");
Rafaël Carré5d236b22010-05-27 09:41:46 +0000349 printf("Version " RBVERSION);
Michael Sevakis4d3a0202009-02-07 10:09:13 +0000350
Michael Sevakis80278e42008-05-10 18:00:11 +0000351 /* Initialize KPP so we can poll the button states */
352 button_init_device();
353
354 adc_init();
355
356 check_battery();
357
Frank Gevaerts2f8a0082008-11-01 16:14:28 +0000358 rc = storage_init();
Michael Sevakis80278e42008-05-10 18:00:11 +0000359 if(rc)
360 {
361 reset_screen();
362 error(EATA, rc);
363 }
364
365 disk_init();
366
367 rc = disk_mount_all();
368 if (rc<=0)
369 {
370 error(EDISK,rc);
371 }
372
373 printf("Init complete");
374
375 /* Do USB first since a tar or binary could be added to the MTP directory
376 * at the time and we can untar or move after unplugging. */
377 handle_usb();
378 handle_untar();
379 handle_firmware_load(); /* No return */
380}