Remove tabs
[maemo-rb.git] / bootloader / gigabeat-s.c
blob9c5ad93d1ff62c84d7305de3483929c285e45914
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Greg White
12 * 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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
22 #include "system.h"
23 #include <stdio.h>
24 #include "kernel.h"
25 #include "gcc_extensions.h"
26 #include "string.h"
27 #include "adc.h"
28 #include "powermgmt.h"
29 #include "storage.h"
30 #include "dir.h"
31 #include "disk.h"
32 #include "common.h"
33 #include "backlight.h"
34 #include "usb.h"
35 #include "button.h"
36 #include "font.h"
37 #include "lcd.h"
38 #include "usb-target.h"
39 #include "version.h"
41 #define TAR_CHUNK 512
42 #define TAR_HEADER_SIZE 157
44 /* Where files sent via MTP are stored */
45 static const char basedir[] = "/Content/0b00/00/";
46 /* Can use memory after vector table up to 0x01f00000 */
47 static char * const tarbuf = (char *)0x00000040;
48 static const size_t tarbuf_size = 0x01f00000 - 0x00000040;
49 /* Firmware data */
50 static void * const load_buf = 0x00000000;
51 static const size_t load_buf_size = 0x20000000 - 0x100000;
52 static const void * const start_addr = 0x00000000;
54 static void show_splash(int timeout, const char *msg)
56 backlight_on();
57 reset_screen();
58 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2,
59 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
60 lcd_update();
62 sleep(timeout);
65 static bool pause_if_button_pressed(bool pre_usb)
67 while (1)
69 int button = button_read_device();
71 if (pre_usb && !usb_plugged())
72 return false;
74 /* Exit if no button or only the menu (settings reset) button */
75 switch (button)
77 case BUTTON_MENU:
78 case BUTTON_NONE:
79 return true;
82 sleep(HZ/5);
84 /* If the disk powers off, the firmware will lock at startup */
85 storage_spin();
89 /* TODO: Handle charging while connected */
90 static void handle_usb(void)
92 int button;
94 /* Check if plugged and pause to look at messages. If the cable was pulled
95 * while waiting, proceed as if it never was plugged. */
96 if (!usb_plugged() || !pause_if_button_pressed(true))
97 return;
99 /** Enter USB mode **/
101 /* We need full button and backlight handling now */
102 backlight_init();
103 button_init();
105 /* Start the USB driver */
106 usb_init();
107 usb_start_monitoring();
109 /* Wait for threads to connect or cable is pulled */
110 show_splash(HZ/2, "Waiting for USB");
112 while (1)
114 button = button_get_w_tmo(HZ/2);
116 if (button == SYS_USB_CONNECTED)
117 break; /* Hit */
119 if (!usb_plugged())
120 break; /* Cable pulled */
123 if (button == SYS_USB_CONNECTED)
125 /* Got the message - wait for disconnect */
126 show_splash(0, "Bootloader USB mode");
128 usb_acknowledge(SYS_USB_CONNECTED_ACK);
130 while (1)
132 button = button_get(true);
133 if (button == SYS_USB_DISCONNECTED)
135 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
136 break;
141 /* Put drivers initialized for USB connection into a known state */
142 backlight_on();
143 usb_close();
144 button_close();
145 backlight_close();
147 /* Sleep a little to let the backlight ramp up */
148 sleep(HZ*5/4);
150 reset_screen();
153 static void untar(int tar_fd)
155 char header[TAR_HEADER_SIZE];
156 char *ptr;
157 char path[102];
158 int fd, i;
159 int ret;
160 size_t size = filesize(tar_fd);
162 if (size > tarbuf_size)
164 printf("tar file too large"); /* Paranoid but proper */
165 return;
168 ret = read(tar_fd, tarbuf, filesize(tar_fd));
169 if (ret < 0)
171 printf("couldn't read tar file (%d)", ret);
172 return;
174 ptr = tarbuf;
176 while (1)
178 memcpy(header, ptr, TAR_HEADER_SIZE);
180 if (*header == '\0') /* Check for EOF */
181 break;
183 /* Parse the size field */
184 size = 0;
185 for (i = 124 ; i < 124 + 11 ; i++) {
186 size = (8 * size) + header[i] - '0';
189 /* Skip rest of header */
190 ptr += TAR_CHUNK;
192 /* Make the path absolute */
193 strcpy(path, "/");
194 strcat(path, header);
196 if (header[156] == '0') /* file */
198 int wc;
200 fd = creat(path, 0666);
201 if (fd < 0)
203 printf("failed to create file (%d)", fd);
205 else
207 wc = write(fd, ptr, size);
208 if (wc < 0)
210 printf("write failed (%d)", wc);
211 break;
213 close(fd);
215 ptr += (size + TAR_CHUNK-1) & (~(TAR_CHUNK-1));
217 else if (header[156] == '5') /* directory */
219 int ret;
221 /* Remove the trailing slash */
222 if (path[strlen(path) - 1] == '/')
223 path[strlen(path) - 1] = '\0';
225 /* Create the dir */
226 ret = mkdir(path);
227 if (ret < 0 && ret != -4)
229 printf("failed to create dir (%d)", ret);
235 /* Look for a tar file or rockbox binary in the MTP directory */
236 static void handle_untar(void)
238 char buf[MAX_PATH];
239 char tarstring[6];
240 char model[5];
241 struct dirent_uncached* entry;
242 DIR_UNCACHED* dir;
243 int fd;
244 int rc;
246 dir = opendir_uncached(basedir);
248 while ((entry = readdir_uncached(dir)))
250 if (*entry->d_name == '.')
251 continue;
253 snprintf(buf, sizeof(buf), "%s%s", basedir, entry->d_name);
254 fd = open(buf, O_RDONLY);
256 if (fd < 0)
257 continue;
259 /* Check whether the file is a rockbox binary. */
260 lseek(fd, 4, SEEK_SET);
261 rc = read(fd, model, 4);
262 if (rc == 4)
264 model[4] = 0;
265 if (strcmp(model, "gigs") == 0)
267 printf("Found rockbox binary. Moving...");
268 close(fd);
269 remove( BOOTDIR "/" BOOTFILE);
270 int ret = rename(buf, BOOTDIR "/" BOOTFILE);
271 printf("returned %d", ret);
272 sleep(HZ);
273 break;
277 /* Check whether the file is a tar file. */
278 lseek(fd, 257, SEEK_SET);
279 rc = read(fd, tarstring, 5);
280 if (rc == 5)
282 tarstring[5] = 0;
283 if (strcmp(tarstring, "ustar") == 0)
285 printf("Found tar file. Unarchiving...");
286 lseek(fd, 0, SEEK_SET);
287 untar(fd);
288 close(fd);
289 printf("Removing tar file");
290 remove(buf);
291 break;
295 close(fd);
299 /* Try to load the firmware and run it */
300 static void NORETURN_ATTR handle_firmware_load(void)
302 int rc = load_firmware(load_buf, BOOTFILE,
303 load_buf_size);
305 if(rc < 0)
306 error(EBOOTFILE, rc, true);
308 /* Pause to look at messages */
309 pause_if_button_pressed(false);
311 /* Put drivers into a known state */
312 button_close_device();
313 storage_close();
314 system_prepare_fw_start();
316 if (rc == EOK)
318 cpucache_invalidate();
319 asm volatile ("bx %0": : "r"(start_addr));
322 /* Halt */
323 while (1)
324 core_idle();
327 static void check_battery(void)
329 int batt = battery_adc_voltage();
330 printf("Battery: %d.%03d V", batt / 1000, batt % 1000);
331 /* TODO: warn on low battery or shut down */
334 void main(void)
336 int rc;
338 /* Flush and invalidate all caches (because vectors were written) */
339 cpucache_invalidate();
341 system_init();
342 kernel_init();
344 enable_interrupt(IRQ_FIQ_STATUS);
346 lcd_init_device();
347 lcd_clear_display();
349 printf("Gigabeat S Rockbox Bootloader");
350 printf("Version " RBVERSION);
352 /* Initialize KPP so we can poll the button states */
353 button_init_device();
355 adc_init();
357 check_battery();
359 rc = storage_init();
360 if(rc)
362 reset_screen();
363 error(EATA, rc, true);
366 disk_init();
368 rc = disk_mount_all();
369 if (rc<=0)
371 error(EDISK, rc, true);
374 printf("Init complete");
376 /* Do USB first since a tar or binary could be added to the MTP directory
377 * at the time and we can untar or move after unplugging. */
378 handle_usb();
379 handle_untar();
380 handle_firmware_load(); /* No return */