Add Study Mode to the manual. Closes FS#9118.
[Rockbox.git] / bootloader / gigabeat-s.c
blobdfe13e4540c5f5442bd079c6efcea7cadfb8aa92
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Greg White
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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "config.h"
20 #include "system.h"
21 #include <sprintf.h>
22 #include "kernel.h"
23 #include "string.h"
24 #include "adc.h"
25 #include "powermgmt.h"
26 #include "ata.h"
27 #include "dir.h"
28 #include "disk.h"
29 #include "common.h"
30 #include "backlight.h"
31 #include "usb.h"
32 #include "button.h"
33 #include "font.h"
34 #include "lcd.h"
35 #include "usb-target.h"
37 #define TAR_CHUNK 512
38 #define TAR_HEADER_SIZE 157
40 const char version[] = APPSVERSION;
41 /* Where files sent via MTP are stored */
42 static const char basedir[] = "/Content/0b00/00/";
43 /* Can use memory after vector table up to 0x01f00000 */
44 static char * const tarbuf = (char *)0x00000040;
45 static const size_t tarbuf_size = 0x01f00000 - 0x00000040;
46 /* Firmware data */
47 static void * const load_buf = 0x00000000;
48 static const size_t load_buf_size = 0x20000000 - 0x100000;
49 static const void * const start_addr = 0x00000000;
51 static void show_splash(int timeout, const char *msg)
53 backlight_on();
54 reset_screen();
55 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2,
56 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
57 lcd_update();
59 sleep(timeout);
62 static bool pause_if_button_pressed(bool pre_usb)
64 while (1)
66 int button = button_read_device();
68 if (pre_usb && !usb_plugged())
69 return false;
71 /* Exit if no button or only the menu (settings reset) button */
72 switch (button)
74 case BUTTON_MENU:
75 case BUTTON_NONE:
76 return true;
79 sleep(HZ/5);
81 /* If the disk powers off, the firmware will lock at startup */
82 ata_spin();
86 /* TODO: Handle charging while connected */
87 static void handle_usb(void)
89 int button;
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))
95 /* Bang on the controller */
96 usb_init_device();
97 return;
100 /** Enter USB mode **/
102 /* We need full button and backlight handling now */
103 backlight_init();
104 button_init();
106 /* Start the USB driver */
107 usb_init();
108 usb_start_monitoring();
110 /* Wait for threads to connect or cable is pulled */
111 show_splash(HZ/2, "Waiting for USB");
113 while (1)
115 button = button_get_w_tmo(HZ/2);
117 if (button == SYS_USB_CONNECTED)
118 break; /* Hit */
120 if (!usb_plugged())
121 break; /* Cable pulled */
124 if (button == SYS_USB_CONNECTED)
126 /* Got the message - wait for disconnect */
127 show_splash(0, "Bootloader USB mode");
129 usb_acknowledge(SYS_USB_CONNECTED_ACK);
131 while (1)
133 button = button_get(true);
134 if (button == SYS_USB_DISCONNECTED)
136 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
137 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 reset_screen();
151 static void untar(int tar_fd)
153 char header[TAR_HEADER_SIZE];
154 char *ptr;
155 char path[102];
156 int fd, i;
157 int ret;
158 size_t size = filesize(tar_fd);
160 if (size > tarbuf_size)
162 printf("tar file too large"); /* Paranoid but proper */
163 return;
166 ret = read(tar_fd, tarbuf, filesize(tar_fd));
167 if (ret < 0)
169 printf("couldn't read tar file (%d)", ret);
170 return;
172 ptr = tarbuf;
174 while (1)
176 memcpy(header, ptr, TAR_HEADER_SIZE);
178 if (*header == '\0') /* Check for EOF */
179 break;
181 /* Parse the size field */
182 size = 0;
183 for (i = 124 ; i < 124 + 11 ; i++) {
184 size = (8 * size) + header[i] - '0';
187 /* Skip rest of header */
188 ptr += TAR_CHUNK;
190 /* Make the path absolute */
191 strcpy(path, "/");
192 strcat(path, header);
194 if (header[156] == '0') /* file */
196 int wc;
198 fd = creat(path);
199 if (fd < 0)
201 printf("failed to create file (%d)", fd);
203 else
205 wc = write(fd, ptr, size);
206 if (wc < 0)
208 printf("write failed (%d)", wc);
209 break;
211 close(fd);
213 ptr += (size + TAR_CHUNK-1) & (~(TAR_CHUNK-1));
215 else if (header[156] == '5') /* directory */
217 int ret;
219 /* Remove the trailing slash */
220 if (path[strlen(path) - 1] == '/')
221 path[strlen(path) - 1] = '\0';
223 /* Create the dir */
224 ret = mkdir(path);
225 if (ret < 0 && ret != -4)
227 printf("failed to create dir (%d)", ret);
233 /* Look for a tar file or rockbox binary in the MTP directory */
234 static void handle_untar(void)
236 char buf[MAX_PATH];
237 char tarstring[6];
238 char model[5];
239 struct dirent_uncached* entry;
240 DIR_UNCACHED* dir;
241 int fd;
242 int rc;
244 dir = opendir_uncached(basedir);
246 while ((entry = readdir_uncached(dir)))
248 if (*entry->d_name == '.')
249 continue;
251 snprintf(buf, sizeof(buf), "%s%s", basedir, entry->d_name);
252 fd = open(buf, O_RDONLY);
254 if (fd < 0)
255 continue;
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)
262 model[4] = 0;
263 if (strcmp(model, "gigs") == 0)
265 printf("Found rockbox binary. Moving...");
266 close(fd);
267 remove("/.rockbox/rockbox.gigabeat");
268 int ret = rename(buf, "/.rockbox/rockbox.gigabeat");
269 printf("returned %d", ret);
270 sleep(HZ);
271 break;
275 /* Check whether the file is a tar file. */
276 lseek(fd, 257, SEEK_SET);
277 rc = read(fd, tarstring, 5);
278 if (rc == 5)
280 tarstring[5] = 0;
281 if (strcmp(tarstring, "ustar") == 0)
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;
293 close(fd);
297 /* Try to load the firmware and run it */
298 static void __attribute__((noreturn)) handle_firmware_load(void)
300 int rc = load_firmware(load_buf, "/.rockbox/rockbox.gigabeat",
301 load_buf_size);
303 if(rc < 0)
304 error(EBOOTFILE, rc);
306 /* Pause to look at messages */
307 pause_if_button_pressed(false);
309 /* Put drivers into a known state */
310 button_close_device();
311 ata_close();
312 system_prepare_fw_start();
314 if (rc == EOK)
316 invalidate_icache();
317 asm volatile ("bx %0": : "r"(start_addr));
320 /* Halt */
321 while (1)
322 core_idle();
325 static void check_battery(void)
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 */
332 void main(void)
334 int rc;
336 /* Flush and invalidate all caches (because vectors were written) */
337 invalidate_icache();
339 lcd_clear_display();
340 printf("Gigabeat S Rockbox Bootloader");
341 printf("Version %s", version);
342 system_init();
343 kernel_init();
345 enable_interrupt(IRQ_FIQ_STATUS);
347 /* Initialize KPP so we can poll the button states */
348 button_init_device();
350 adc_init();
352 check_battery();
354 rc = ata_init();
355 if(rc)
357 reset_screen();
358 error(EATA, rc);
361 disk_init();
363 rc = disk_mount_all();
364 if (rc<=0)
366 error(EDISK,rc);
369 printf("Init complete");
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 */