Fix Gigabeat F manual. Thanks to Marianne Arnold for pointing out it was broken.
[kugel-rb.git] / bootloader / gigabeat-s.c
blob2e95d4cf642a20c45b39dbdf2f3390b6174afe28
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 <sprintf.h>
24 #include "kernel.h"
25 #include "string.h"
26 #include "adc.h"
27 #include "powermgmt.h"
28 #include "storage.h"
29 #include "dir.h"
30 #include "disk.h"
31 #include "common.h"
32 #include "backlight.h"
33 #include "usb.h"
34 #include "button.h"
35 #include "font.h"
36 #include "lcd.h"
37 #include "usb-target.h"
39 #define TAR_CHUNK 512
40 #define TAR_HEADER_SIZE 157
42 const char version[] = APPSVERSION;
43 /* Where files sent via MTP are stored */
44 static const char basedir[] = "/Content/0b00/00/";
45 /* Can use memory after vector table up to 0x01f00000 */
46 static char * const tarbuf = (char *)0x00000040;
47 static const size_t tarbuf_size = 0x01f00000 - 0x00000040;
48 /* Firmware data */
49 static void * const load_buf = 0x00000000;
50 static const size_t load_buf_size = 0x20000000 - 0x100000;
51 static const void * const start_addr = 0x00000000;
53 static void show_splash(int timeout, const char *msg)
55 backlight_on();
56 reset_screen();
57 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2,
58 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
59 lcd_update();
61 sleep(timeout);
64 static bool pause_if_button_pressed(bool pre_usb)
66 while (1)
68 int button = button_read_device();
70 if (pre_usb && !usb_plugged())
71 return false;
73 /* Exit if no button or only the menu (settings reset) button */
74 switch (button)
76 case BUTTON_MENU:
77 case BUTTON_NONE:
78 return true;
81 sleep(HZ/5);
83 /* If the disk powers off, the firmware will lock at startup */
84 storage_spin();
88 /* TODO: Handle charging while connected */
89 static void handle_usb(void)
91 int button;
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))
96 return;
98 /** Enter USB mode **/
100 /* We need full button and backlight handling now */
101 backlight_init();
102 button_init();
104 /* Start the USB driver */
105 usb_init();
106 usb_start_monitoring();
108 /* Wait for threads to connect or cable is pulled */
109 show_splash(HZ/2, "Waiting for USB");
111 while (1)
113 button = button_get_w_tmo(HZ/2);
115 if (button == SYS_USB_CONNECTED)
116 break; /* Hit */
118 if (!usb_plugged())
119 break; /* Cable pulled */
122 if (button == SYS_USB_CONNECTED)
124 /* Got the message - wait for disconnect */
125 show_splash(0, "Bootloader USB mode");
127 usb_acknowledge(SYS_USB_CONNECTED_ACK);
129 while (1)
131 button = button_get(true);
132 if (button == SYS_USB_DISCONNECTED)
134 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
135 break;
140 /* Put drivers initialized for USB connection into a known state */
141 backlight_on();
142 usb_close();
143 button_close();
144 backlight_close();
146 reset_screen();
149 static void untar(int tar_fd)
151 char header[TAR_HEADER_SIZE];
152 char *ptr;
153 char path[102];
154 int fd, i;
155 int ret;
156 size_t size = filesize(tar_fd);
158 if (size > tarbuf_size)
160 printf("tar file too large"); /* Paranoid but proper */
161 return;
164 ret = read(tar_fd, tarbuf, filesize(tar_fd));
165 if (ret < 0)
167 printf("couldn't read tar file (%d)", ret);
168 return;
170 ptr = tarbuf;
172 while (1)
174 memcpy(header, ptr, TAR_HEADER_SIZE);
176 if (*header == '\0') /* Check for EOF */
177 break;
179 /* Parse the size field */
180 size = 0;
181 for (i = 124 ; i < 124 + 11 ; i++) {
182 size = (8 * size) + header[i] - '0';
185 /* Skip rest of header */
186 ptr += TAR_CHUNK;
188 /* Make the path absolute */
189 strcpy(path, "/");
190 strcat(path, header);
192 if (header[156] == '0') /* file */
194 int wc;
196 fd = creat(path);
197 if (fd < 0)
199 printf("failed to create file (%d)", fd);
201 else
203 wc = write(fd, ptr, size);
204 if (wc < 0)
206 printf("write failed (%d)", wc);
207 break;
209 close(fd);
211 ptr += (size + TAR_CHUNK-1) & (~(TAR_CHUNK-1));
213 else if (header[156] == '5') /* directory */
215 int ret;
217 /* Remove the trailing slash */
218 if (path[strlen(path) - 1] == '/')
219 path[strlen(path) - 1] = '\0';
221 /* Create the dir */
222 ret = mkdir(path);
223 if (ret < 0 && ret != -4)
225 printf("failed to create dir (%d)", ret);
231 /* Look for a tar file or rockbox binary in the MTP directory */
232 static void handle_untar(void)
234 char buf[MAX_PATH];
235 char tarstring[6];
236 char model[5];
237 struct dirent_uncached* entry;
238 DIR_UNCACHED* dir;
239 int fd;
240 int rc;
242 dir = opendir_uncached(basedir);
244 while ((entry = readdir_uncached(dir)))
246 if (*entry->d_name == '.')
247 continue;
249 snprintf(buf, sizeof(buf), "%s%s", basedir, entry->d_name);
250 fd = open(buf, O_RDONLY);
252 if (fd < 0)
253 continue;
255 /* Check whether the file is a rockbox binary. */
256 lseek(fd, 4, SEEK_SET);
257 rc = read(fd, model, 4);
258 if (rc == 4)
260 model[4] = 0;
261 if (strcmp(model, "gigs") == 0)
263 printf("Found rockbox binary. Moving...");
264 close(fd);
265 remove( BOOTDIR "/" BOOTFILE);
266 int ret = rename(buf, BOOTDIR "/" BOOTFILE);
267 printf("returned %d", ret);
268 sleep(HZ);
269 break;
273 /* Check whether the file is a tar file. */
274 lseek(fd, 257, SEEK_SET);
275 rc = read(fd, tarstring, 5);
276 if (rc == 5)
278 tarstring[5] = 0;
279 if (strcmp(tarstring, "ustar") == 0)
281 printf("Found tar file. Unarchiving...");
282 lseek(fd, 0, SEEK_SET);
283 untar(fd);
284 close(fd);
285 printf("Removing tar file");
286 remove(buf);
287 break;
291 close(fd);
295 /* Try to load the firmware and run it */
296 static void __attribute__((noreturn)) handle_firmware_load(void)
298 int rc = load_firmware(load_buf, BOOTFILE,
299 load_buf_size);
301 if(rc < 0)
302 error(EBOOTFILE, rc);
304 /* Pause to look at messages */
305 pause_if_button_pressed(false);
307 /* Put drivers into a known state */
308 button_close_device();
309 storage_close();
310 system_prepare_fw_start();
312 if (rc == EOK)
314 cpucache_invalidate();
315 asm volatile ("bx %0": : "r"(start_addr));
318 /* Halt */
319 while (1)
320 core_idle();
323 static void check_battery(void)
325 int batt = battery_adc_voltage();
326 printf("Battery: %d.%03d V", batt / 1000, batt % 1000);
327 /* TODO: warn on low battery or shut down */
330 void main(void)
332 int rc;
334 /* Flush and invalidate all caches (because vectors were written) */
335 cpucache_invalidate();
337 system_init();
338 kernel_init();
340 enable_interrupt(IRQ_FIQ_STATUS);
342 lcd_init_device();
343 lcd_clear_display();
345 printf("Gigabeat S Rockbox Bootloader");
346 printf("Version %s", version);
348 /* Initialize KPP so we can poll the button states */
349 button_init_device();
351 adc_init();
353 check_battery();
355 rc = storage_init();
356 if(rc)
358 reset_screen();
359 error(EATA, rc);
362 disk_init();
364 rc = disk_mount_all();
365 if (rc<=0)
367 error(EDISK,rc);
370 printf("Init complete");
372 /* Do USB first since a tar or binary could be added to the MTP directory
373 * at the time and we can untar or move after unplugging. */
374 handle_usb();
375 handle_untar();
376 handle_firmware_load(); /* No return */