blob: dfe13e4540c5f5442bd079c6efcea7cadfb8aa92 [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"
Michael Sevakis80278e42008-05-10 18:00:11 +000024#include "adc.h"
25#include "powermgmt.h"
Will Robertson590501c2007-09-21 15:51:53 +000026#include "ata.h"
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +000027#include "dir.h"
Will Robertson590501c2007-09-21 15:51:53 +000028#include "disk.h"
Will Robertson590501c2007-09-21 15:51:53 +000029#include "common.h"
Michael Sevakis80278e42008-05-10 18:00:11 +000030#include "backlight.h"
Will Robertson590501c2007-09-21 15:51:53 +000031#include "usb.h"
Michael Sevakis80278e42008-05-10 18:00:11 +000032#include "button.h"
Michael Sevakisc41caca2008-05-07 07:14:39 +000033#include "font.h"
34#include "lcd.h"
Michael Sevakis94f7d0f2008-04-18 16:42:50 +000035#include "usb-target.h"
Will Robertson590501c2007-09-21 15:51:53 +000036
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000037#define TAR_CHUNK 512
38#define TAR_HEADER_SIZE 157
39
Michael Sevakisc41caca2008-05-07 07:14:39 +000040const char version[] = APPSVERSION;
41/* Where files sent via MTP are stored */
42static const char basedir[] = "/Content/0b00/00/";
43/* Can use memory after vector table up to 0x01f00000 */
44static char * const tarbuf = (char *)0x00000040;
45static const size_t tarbuf_size = 0x01f00000 - 0x00000040;
Michael Sevakis80278e42008-05-10 18:00:11 +000046/* Firmware data */
47static void * const load_buf = 0x00000000;
48static const size_t load_buf_size = 0x20000000 - 0x100000;
49static const void * const start_addr = 0x00000000;
Michael Sevakisa07c0342008-02-08 02:20:05 +000050
Michael Sevakisc41caca2008-05-07 07:14:39 +000051static void show_splash(int timeout, const char *msg)
Michael Sevakis94f7d0f2008-04-18 16:42:50 +000052{
Michael Sevakis80278e42008-05-10 18:00:11 +000053 backlight_on();
54 reset_screen();
Michael Sevakis94f7d0f2008-04-18 16:42:50 +000055 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2,
56 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
57 lcd_update();
58
59 sleep(timeout);
60}
61
Michael Sevakis80278e42008-05-10 18:00:11 +000062static bool pause_if_button_pressed(bool pre_usb)
63{
64 while (1)
65 {
66 int button = button_read_device();
67
68 if (pre_usb && !usb_plugged())
69 return false;
70
71 /* Exit if no button or only the menu (settings reset) button */
72 switch (button)
73 {
74 case BUTTON_MENU:
75 case BUTTON_NONE:
76 return true;
77 }
78
79 sleep(HZ/5);
80
81 /* If the disk powers off, the firmware will lock at startup */
82 ata_spin();
83 }
84}
85
86/* TODO: Handle charging while connected */
87static void handle_usb(void)
88{
89 int button;
90
91 /* Check if plugged and pause to look at messages. If the cable was pulled
92 * while waiting, proceed as if it never was plugged. */
93 if (!usb_plugged() || !pause_if_button_pressed(true))
94 {
95 /* Bang on the controller */
96 usb_init_device();
97 return;
98 }
99
100 /** Enter USB mode **/
101
102 /* We need full button and backlight handling now */
103 backlight_init();
104 button_init();
105
106 /* Start the USB driver */
107 usb_init();
108 usb_start_monitoring();
109
110 /* Wait for threads to connect or cable is pulled */
111 show_splash(HZ/2, "Waiting for USB");
112
113 while (1)
114 {
115 button = button_get_w_tmo(HZ/2);
116
117 if (button == SYS_USB_CONNECTED)
118 break; /* Hit */
119
120 if (!usb_plugged())
121 break; /* Cable pulled */
122 }
123
124 if (button == SYS_USB_CONNECTED)
125 {
126 /* Got the message - wait for disconnect */
127 show_splash(0, "Bootloader USB mode");
128
129 usb_acknowledge(SYS_USB_CONNECTED_ACK);
130
131 while (1)
132 {
133 button = button_get(true);
134 if (button == SYS_USB_DISCONNECTED)
135 {
136 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
137 break;
138 }
139 }
140 }
141
142 /* Put drivers initialized for USB connection into a known state */
143 backlight_on();
144 usb_close();
145 button_close();
146 backlight_close();
147
148 reset_screen();
149}
150
Michael Sevakisc41caca2008-05-07 07:14:39 +0000151static void untar(int tar_fd)
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000152{
153 char header[TAR_HEADER_SIZE];
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000154 char *ptr;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000155 char path[102];
Michael Sevakisc41caca2008-05-07 07:14:39 +0000156 int fd, i;
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000157 int ret;
Michael Sevakisc41caca2008-05-07 07:14:39 +0000158 size_t size = filesize(tar_fd);
159
Michael Sevakis80278e42008-05-10 18:00:11 +0000160 if (size > tarbuf_size)
161 {
Michael Sevakisc41caca2008-05-07 07:14:39 +0000162 printf("tar file too large"); /* Paranoid but proper */
163 return;
164 }
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000165
166 ret = read(tar_fd, tarbuf, filesize(tar_fd));
Michael Sevakis80278e42008-05-10 18:00:11 +0000167 if (ret < 0)
168 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000169 printf("couldn't read tar file (%d)", ret);
170 return;
171 }
172 ptr = tarbuf;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000173
174 while (1)
175 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000176 memcpy(header, ptr, TAR_HEADER_SIZE);
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000177
178 if (*header == '\0') /* Check for EOF */
179 break;
180
181 /* Parse the size field */
182 size = 0;
183 for (i = 124 ; i < 124 + 11 ; i++) {
184 size = (8 * size) + header[i] - '0';
185 }
186
187 /* Skip rest of header */
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000188 ptr += TAR_CHUNK;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000189
190 /* Make the path absolute */
191 strcpy(path, "/");
192 strcat(path, header);
193
194 if (header[156] == '0') /* file */
195 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000196 int wc;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000197
198 fd = creat(path);
199 if (fd < 0)
200 {
201 printf("failed to create file (%d)", fd);
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000202 }
203 else
204 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000205 wc = write(fd, ptr, size);
206 if (wc < 0)
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000207 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000208 printf("write failed (%d)", wc);
209 break;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000210 }
211 close(fd);
212 }
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000213 ptr += (size + TAR_CHUNK-1) & (~(TAR_CHUNK-1));
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000214 }
215 else if (header[156] == '5') /* directory */
216 {
217 int ret;
218
219 /* Remove the trailing slash */
220 if (path[strlen(path) - 1] == '/')
221 path[strlen(path) - 1] = '\0';
222
223 /* Create the dir */
224 ret = mkdir(path);
225 if (ret < 0 && ret != -4)
226 {
227 printf("failed to create dir (%d)", ret);
228 }
229 }
230 }
231}
232
Michael Sevakis80278e42008-05-10 18:00:11 +0000233/* Look for a tar file or rockbox binary in the MTP directory */
234static void handle_untar(void)
Will Robertson590501c2007-09-21 15:51:53 +0000235{
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000236 char buf[MAX_PATH];
237 char tarstring[6];
Nicolas Pennequin620e6b42008-04-15 23:17:03 +0000238 char model[5];
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000239 struct dirent_uncached* entry;
240 DIR_UNCACHED* dir;
241 int fd;
Michael Sevakis80278e42008-05-10 18:00:11 +0000242 int rc;
243
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000244 dir = opendir_uncached(basedir);
Michael Sevakis80278e42008-05-10 18:00:11 +0000245
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000246 while ((entry = readdir_uncached(dir)))
247 {
Michael Sevakis80278e42008-05-10 18:00:11 +0000248 if (*entry->d_name == '.')
249 continue;
Nicolas Pennequin620e6b42008-04-15 23:17:03 +0000250
Michael Sevakis80278e42008-05-10 18:00:11 +0000251 snprintf(buf, sizeof(buf), "%s%s", basedir, entry->d_name);
252 fd = open(buf, O_RDONLY);
253
254 if (fd < 0)
255 continue;
256
257 /* Check whether the file is a rockbox binary. */
258 lseek(fd, 4, SEEK_SET);
259 rc = read(fd, model, 4);
260 if (rc == 4)
261 {
262 model[4] = 0;
263 if (strcmp(model, "gigs") == 0)
264 {
265 printf("Found rockbox binary. Moving...");
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000266 close(fd);
Michael Sevakis80278e42008-05-10 18:00:11 +0000267 remove("/.rockbox/rockbox.gigabeat");
268 int ret = rename(buf, "/.rockbox/rockbox.gigabeat");
269 printf("returned %d", ret);
270 sleep(HZ);
271 break;
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000272 }
273 }
Will Robertson590501c2007-09-21 15:51:53 +0000274
Michael Sevakis80278e42008-05-10 18:00:11 +0000275 /* Check whether the file is a tar file. */
276 lseek(fd, 257, SEEK_SET);
277 rc = read(fd, tarstring, 5);
278 if (rc == 5)
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000279 {
Michael Sevakis80278e42008-05-10 18:00:11 +0000280 tarstring[5] = 0;
281 if (strcmp(tarstring, "ustar") == 0)
282 {
283 printf("Found tar file. Unarchiving...");
284 lseek(fd, 0, SEEK_SET);
285 untar(fd);
286 close(fd);
287 printf("Removing tar file");
288 remove(buf);
289 break;
290 }
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000291 }
292
Michael Sevakis80278e42008-05-10 18:00:11 +0000293 close(fd);
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000294 }
Michael Sevakis80278e42008-05-10 18:00:11 +0000295}
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000296
Michael Sevakis80278e42008-05-10 18:00:11 +0000297/* Try to load the firmware and run it */
298static void __attribute__((noreturn)) handle_firmware_load(void)
299{
300 int rc = load_firmware(load_buf, "/.rockbox/rockbox.gigabeat",
301 load_buf_size);
Will Robertson590501c2007-09-21 15:51:53 +0000302
Will Robertson590501c2007-09-21 15:51:53 +0000303 if(rc < 0)
Michael Sevakis75fe8872008-05-04 03:44:49 +0000304 error(EBOOTFILE, rc);
Michael Sevakisa07c0342008-02-08 02:20:05 +0000305
Michael Sevakis80278e42008-05-10 18:00:11 +0000306 /* Pause to look at messages */
307 pause_if_button_pressed(false);
308
309 /* Put drivers into a known state */
310 button_close_device();
311 ata_close();
Michael Sevakisa07c0342008-02-08 02:20:05 +0000312 system_prepare_fw_start();
Will Robertson590501c2007-09-21 15:51:53 +0000313
314 if (rc == EOK)
315 {
Michael Sevakis1f021af2008-02-05 04:43:19 +0000316 invalidate_icache();
Michael Sevakis80278e42008-05-10 18:00:11 +0000317 asm volatile ("bx %0": : "r"(start_addr));
Will Robertson590501c2007-09-21 15:51:53 +0000318 }
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000319
Michael Sevakis94f7d0f2008-04-18 16:42:50 +0000320 /* Halt */
321 while (1)
322 core_idle();
Will Robertson590501c2007-09-21 15:51:53 +0000323}
324
Michael Sevakis80278e42008-05-10 18:00:11 +0000325static void check_battery(void)
326{
327 int batt = battery_adc_voltage();
328 printf("Battery: %d.%03d V", batt / 1000, batt % 1000);
329 /* TODO: warn on low battery or shut down */
330}
331
332void main(void)
333{
334 int rc;
335
336 /* Flush and invalidate all caches (because vectors were written) */
337 invalidate_icache();
338
339 lcd_clear_display();
340 printf("Gigabeat S Rockbox Bootloader");
341 printf("Version %s", version);
342 system_init();
343 kernel_init();
344
345 enable_interrupt(IRQ_FIQ_STATUS);
346
347 /* Initialize KPP so we can poll the button states */
348 button_init_device();
349
350 adc_init();
351
352 check_battery();
353
354 rc = ata_init();
355 if(rc)
356 {
357 reset_screen();
358 error(EATA, rc);
359 }
360
361 disk_init();
362
363 rc = disk_mount_all();
364 if (rc<=0)
365 {
366 error(EDISK,rc);
367 }
368
369 printf("Init complete");
370
371 /* Do USB first since a tar or binary could be added to the MTP directory
372 * at the time and we can untar or move after unplugging. */
373 handle_usb();
374 handle_untar();
375 handle_firmware_load(); /* No return */
376}