Try to get some control over #ifdef hell in usb.c by refactoring and inline function...
[kugel-rb.git] / bootloader / gigabeat-s.c
blob77887f5d4b0696cc3e7126e0ab68a41a15ac887a
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 select buttons that have other
75 * functions */
76 switch (button)
78 case USB_BL_INSTALL_MODE_BTN:
79 if (!pre_usb)
80 break; /* Only before USB detect */
81 case BUTTON_MENU: /* Settings reset */
82 case BUTTON_NONE: /* Nothing pressed */
83 return true;
86 sleep(HZ/5);
88 /* If the disk powers off, the firmware will lock at startup */
89 storage_spin();
93 /* TODO: Handle charging while connected */
94 static void handle_usb(void)
96 int button;
98 /* Check if plugged and pause to look at messages. If the cable was pulled
99 * while waiting, proceed as if it never was plugged. */
100 if (!usb_plugged() || !pause_if_button_pressed(true))
101 return;
103 /** Enter USB mode **/
105 /* We need full button and backlight handling now */
106 backlight_init();
107 button_init();
109 /* Start the USB driver */
110 usb_init();
111 usb_start_monitoring();
113 /* Wait for threads to connect or cable is pulled */
114 show_splash(HZ/2, "Waiting for USB");
116 while (1)
118 button = button_get_w_tmo(HZ/2);
120 if (button == SYS_USB_CONNECTED)
121 break; /* Hit */
123 if (!usb_plugged())
124 break; /* Cable pulled */
127 if (button == SYS_USB_CONNECTED)
129 /* Got the message - wait for disconnect */
130 show_splash(0, "Bootloader USB mode");
132 usb_acknowledge(SYS_USB_CONNECTED_ACK);
134 while (1)
136 button = button_get(true);
137 if (button == SYS_USB_DISCONNECTED)
138 break;
142 /* Put drivers initialized for USB connection into a known state */
143 backlight_on();
144 usb_close();
145 button_close();
146 backlight_close();
148 /* Sleep a little to let the backlight ramp up */
149 sleep(HZ*5/4);
151 reset_screen();
154 static void untar(int tar_fd)
156 char header[TAR_HEADER_SIZE];
157 char *ptr;
158 char path[102];
159 int fd, i;
160 int ret;
161 size_t size = filesize(tar_fd);
163 if (size > tarbuf_size)
165 printf("tar file too large"); /* Paranoid but proper */
166 return;
169 ret = read(tar_fd, tarbuf, filesize(tar_fd));
170 if (ret < 0)
172 printf("couldn't read tar file (%d)", ret);
173 return;
175 ptr = tarbuf;
177 while (1)
179 memcpy(header, ptr, TAR_HEADER_SIZE);
181 if (*header == '\0') /* Check for EOF */
182 break;
184 /* Parse the size field */
185 size = 0;
186 for (i = 124 ; i < 124 + 11 ; i++) {
187 size = (8 * size) + header[i] - '0';
190 /* Skip rest of header */
191 ptr += TAR_CHUNK;
193 /* Make the path absolute */
194 strcpy(path, "/");
195 strcat(path, header);
197 if (header[156] == '0') /* file */
199 int wc;
201 fd = creat(path, 0666);
202 if (fd < 0)
204 printf("failed to create file (%d)", fd);
206 else
208 wc = write(fd, ptr, size);
209 if (wc < 0)
211 printf("write failed (%d)", wc);
212 break;
214 close(fd);
216 ptr += (size + TAR_CHUNK-1) & (~(TAR_CHUNK-1));
218 else if (header[156] == '5') /* directory */
220 int ret;
222 /* Remove the trailing slash */
223 if (path[strlen(path) - 1] == '/')
224 path[strlen(path) - 1] = '\0';
226 /* Create the dir */
227 ret = mkdir(path);
228 if (ret < 0 && ret != -4)
230 printf("failed to create dir (%d)", ret);
236 /* Look for a tar file or rockbox binary in the MTP directory */
237 static void handle_untar(void)
239 char buf[MAX_PATH];
240 char tarstring[6];
241 char model[5];
242 struct dirent_uncached* entry;
243 DIR_UNCACHED* dir;
244 int fd;
245 int rc;
247 dir = opendir_uncached(basedir);
249 while ((entry = readdir_uncached(dir)))
251 if (*entry->d_name == '.')
252 continue;
254 snprintf(buf, sizeof(buf), "%s%s", basedir, entry->d_name);
255 fd = open(buf, O_RDONLY);
257 if (fd < 0)
258 continue;
260 /* Check whether the file is a rockbox binary. */
261 lseek(fd, 4, SEEK_SET);
262 rc = read(fd, model, 4);
263 if (rc == 4)
265 model[4] = 0;
266 if (strcmp(model, "gigs") == 0)
268 printf("Found rockbox binary. Moving...");
269 close(fd);
270 remove( BOOTDIR "/" BOOTFILE);
271 int ret = rename(buf, BOOTDIR "/" BOOTFILE);
272 printf("returned %d", ret);
273 sleep(HZ);
274 break;
278 /* Check whether the file is a tar file. */
279 lseek(fd, 257, SEEK_SET);
280 rc = read(fd, tarstring, 5);
281 if (rc == 5)
283 tarstring[5] = 0;
284 if (strcmp(tarstring, "ustar") == 0)
286 printf("Found tar file. Unarchiving...");
287 lseek(fd, 0, SEEK_SET);
288 untar(fd);
289 close(fd);
290 printf("Removing tar file");
291 remove(buf);
292 break;
296 close(fd);
300 /* Try to load the firmware and run it */
301 static void NORETURN_ATTR handle_firmware_load(void)
303 int rc = load_firmware(load_buf, BOOTFILE,
304 load_buf_size);
306 if(rc < 0)
307 error(EBOOTFILE, rc, true);
309 /* Pause to look at messages */
310 pause_if_button_pressed(false);
312 /* Put drivers into a known state */
313 button_close_device();
314 storage_close();
315 system_prepare_fw_start();
317 if (rc == EOK)
319 cpucache_invalidate();
320 asm volatile ("bx %0": : "r"(start_addr));
323 /* Halt */
324 while (1)
325 core_idle();
328 static void check_battery(void)
330 int batt = battery_adc_voltage();
331 printf("Battery: %d.%03d V", batt / 1000, batt % 1000);
332 /* TODO: warn on low battery or shut down */
335 void main(void)
337 int rc;
339 /* Flush and invalidate all caches (because vectors were written) */
340 cpucache_invalidate();
342 system_init();
343 kernel_init();
345 enable_interrupt(IRQ_FIQ_STATUS);
347 lcd_init_device();
348 lcd_clear_display();
350 printf("Gigabeat S Rockbox Bootloader");
351 printf("Version " RBVERSION);
353 /* Initialize KPP so we can poll the button states */
354 button_init_device();
356 adc_init();
358 check_battery();
360 rc = storage_init();
361 if(rc)
363 reset_screen();
364 error(EATA, rc, true);
367 disk_init();
369 rc = disk_mount_all();
370 if (rc<=0)
372 error(EDISK, rc, true);
375 printf("Init complete");
377 /* Do USB first since a tar or binary could be added to the MTP directory
378 * at the time and we can untar or move after unplugging. */
379 handle_usb();
380 handle_untar();
381 handle_firmware_load(); /* No return */