Code police. Add some explanation for playlist catalog.
[Rockbox.git] / bootloader / main.c
blob1117b649d092d555d49a655255c8d8862cf75427
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Linus Nielsen Feltzing
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"
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include "inttypes.h"
24 #include "string.h"
25 #include "cpu.h"
26 #include "system.h"
27 #include "lcd.h"
28 #include "lcd-remote.h"
29 #include "kernel.h"
30 #include "thread.h"
31 #include "ata.h"
32 #include "usb.h"
33 #include "disk.h"
34 #include "font.h"
35 #include "adc.h"
36 #include "backlight.h"
37 #include "backlight-target.h"
38 #include "button.h"
39 #include "panic.h"
40 #include "power.h"
41 #include "file.h"
42 #include "uda1380.h"
43 #include "eeprom_settings.h"
44 #include "rbunicode.h"
45 #include "common.h"
47 #include <stdarg.h>
49 /* Maximum allowed firmware image size. 10MB is more than enough */
50 #define MAX_LOADSIZE (10*1024*1024)
52 #define DRAM_START 0x31000000
54 #ifdef HAVE_EEPROM_SETTINGS
55 static bool recovery_mode = false;
56 #endif
58 char version[] = APPSVERSION;
60 /* Reset the cookie for the crt0 crash check */
61 inline void __reset_cookie(void)
63 asm(" move.l #0,%d0");
64 asm(" move.l %d0,0x10017ffc");
67 void start_iriver_fw(void)
69 asm(" move.w #0x2700,%sr");
70 __reset_cookie();
71 asm(" movec.l %d0,%vbr");
72 asm(" move.l 0,%sp");
73 asm(" lea.l 8,%a0");
74 asm(" jmp (%a0)");
77 void start_firmware(void)
79 asm(" move.w #0x2700,%sr");
80 __reset_cookie();
81 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
82 asm(" movec.l %d0,%vbr");
83 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
84 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
85 asm(" jmp (%a0)");
88 #ifdef IRIVER_H100_SERIES
89 void start_flashed_romimage(void)
91 uint8_t *src = (uint8_t *)FLASH_ROMIMAGE_ENTRY;
92 int *reset_vector;
94 if (!detect_flashed_romimage())
95 return ;
97 reset_vector = (int *)(&src[sizeof(struct flash_header)+4]);
99 asm(" move.w #0x2700,%sr");
100 __reset_cookie();
102 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
103 asm(" movec.l %d0,%vbr");
104 asm(" move.l %0,%%sp" :: "m"(reset_vector[0]));
105 asm(" move.l %0,%%a0" :: "m"(reset_vector[1]));
106 asm(" jmp (%a0)");
108 /* Failure */
109 power_off();
112 void start_flashed_ramimage(void)
114 struct flash_header hdr;
115 unsigned char *buf = (unsigned char *)DRAM_START;
116 uint8_t *src = (uint8_t *)FLASH_RAMIMAGE_ENTRY;
118 if (!detect_flashed_ramimage())
119 return;
121 /* Load firmware from flash */
122 cpu_boost(true);
123 memcpy(&hdr, src, sizeof(struct flash_header));
124 src += sizeof(struct flash_header);
125 memcpy(buf, src, hdr.length);
126 cpu_boost(false);
128 start_firmware();
130 /* Failure */
131 power_off();
133 #endif /* IRIVER_H100_SERIES */
135 void shutdown(void)
137 printf("Shutting down...");
138 #ifdef HAVE_EEPROM_SETTINGS
139 /* Reset the rockbox crash check. */
140 firmware_settings.bl_version = 0;
141 eeprom_settings_store();
142 #endif
144 /* We need to gracefully spin down the disk to prevent clicks. */
145 if (ide_powered())
147 /* Make sure ATA has been initialized. */
148 ata_init();
150 /* And put the disk into sleep immediately. */
151 ata_sleepnow();
154 sleep(HZ*2);
156 /* Backlight OFF */
157 __backlight_off();
158 #ifdef HAVE_REMOTE_LCD
159 __remote_backlight_off();
160 #endif
162 __reset_cookie();
163 power_off();
166 /* Print the battery voltage (and a warning message). */
167 void check_battery(void)
169 int adc_battery, battery_voltage, batt_int, batt_frac;
171 adc_battery = adc_read(ADC_BATTERY);
173 battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000;
174 batt_int = battery_voltage / 100;
175 batt_frac = battery_voltage % 100;
177 printf("Batt: %d.%02dV", batt_int, batt_frac);
179 if (battery_voltage <= 310)
181 printf("WARNING! BATTERY LOW!!");
182 sleep(HZ*2);
186 #ifdef HAVE_EEPROM_SETTINGS
187 void initialize_eeprom(void)
189 if (detect_original_firmware())
190 return ;
192 if (!eeprom_settings_init())
194 recovery_mode = true;
195 return ;
198 /* If bootloader version has not been reset, disk might
199 * not be intact. */
200 if (firmware_settings.bl_version || !firmware_settings.disk_clean)
202 firmware_settings.disk_clean = false;
203 recovery_mode = true;
206 firmware_settings.bl_version = EEPROM_SETTINGS_BL_MINVER;
207 eeprom_settings_store();
210 void try_flashboot(void)
212 if (!firmware_settings.initialized)
213 return ;
215 switch (firmware_settings.bootmethod)
217 case BOOT_DISK:
218 return;
220 case BOOT_ROM:
221 start_flashed_romimage();
222 recovery_mode = true;
223 break;
225 case BOOT_RAM:
226 start_flashed_ramimage();
227 recovery_mode = true;
228 break;
230 default:
231 recovery_mode = true;
232 return;
236 static const char *options[] = {
237 "Boot from disk",
238 "Boot RAM image",
239 "Boot ROM image",
240 "Shutdown"
243 #define FAILSAFE_OPTIONS 4
244 void failsafe_menu(void)
246 int timeout = 15;
247 int option = 3;
248 int button;
249 int defopt = -1;
250 char buf[32];
251 int i;
252 extern int line;
254 reset_screen();
255 printf("Bootloader %s", version);
256 check_battery();
257 printf("=========================");
258 line += FAILSAFE_OPTIONS;
259 printf("");
260 printf(" [NAVI] to confirm.");
261 printf(" [REC] to set as default.");
262 printf("");
264 if (firmware_settings.initialized)
266 defopt = firmware_settings.bootmethod;
267 if (defopt < 0 || defopt >= FAILSAFE_OPTIONS)
268 defopt = option;
271 while (timeout > 0)
273 /* Draw the menu. */
274 line = 3;
275 for (i = 0; i < FAILSAFE_OPTIONS; i++)
277 char *def = "[DEF]";
278 char *arrow = "->";
280 if (i != defopt)
281 def = "";
282 if (i != option)
283 arrow = " ";
285 printf("%s %s %s", arrow, options[i], def);
288 snprintf(buf, sizeof(buf), "Time left: %ds", timeout);
289 lcd_puts(0, 10, buf);
290 lcd_update();
291 button = button_get_w_tmo(HZ);
293 if (button == BUTTON_NONE)
295 timeout--;
296 continue ;
299 timeout = 15;
300 /* Ignore the ON/PLAY -button because it can cause trouble
301 with the RTC alarm mod. */
302 switch (button & ~(BUTTON_ON))
304 case BUTTON_UP:
305 case BUTTON_RC_REW:
306 if (option > 0)
307 option--;
308 break ;
310 case BUTTON_DOWN:
311 case BUTTON_RC_FF:
312 if (option < FAILSAFE_OPTIONS-1)
313 option++;
314 break ;
316 case BUTTON_SELECT:
317 case BUTTON_RC_ON:
318 timeout = 0;
319 break ;
321 case BUTTON_REC:
322 case BUTTON_RC_REC:
323 if (firmware_settings.initialized)
325 firmware_settings.bootmethod = option;
326 eeprom_settings_store();
327 defopt = option;
329 break ;
333 lcd_puts(0, 10, "Executing command...");
334 lcd_update();
335 sleep(HZ);
336 reset_screen();
338 switch (option)
340 case BOOT_DISK:
341 return ;
343 case BOOT_RAM:
344 start_flashed_ramimage();
345 printf("Image not found");
346 break;
348 case BOOT_ROM:
349 start_flashed_romimage();
350 printf("Image not found");
351 break;
354 shutdown();
356 #endif
358 void main(void)
360 int i;
361 int rc;
362 bool rc_on_button = false;
363 bool on_button = false;
364 bool rec_button = false;
365 bool hold_status = false;
366 int data;
367 extern int line; /* From common.c */
368 extern int remote_line; /* From common.c */
370 /* We want to read the buttons as early as possible, before the user
371 releases the ON button */
373 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
374 (The ON and Hold buttons on the main unit and the remote) */
375 or_l(0x00100062, &GPIO1_FUNCTION);
376 and_l(~0x00100062, &GPIO1_ENABLE);
378 data = GPIO1_READ;
379 if ((data & 0x20) == 0)
380 on_button = true;
382 if ((data & 0x40) == 0)
383 rc_on_button = true;
385 /* Set the default state of the hard drive power to OFF */
386 ide_power_enable(false);
388 power_init();
390 /* Turn off if neither ON button is pressed */
391 if(!(on_button || rc_on_button || usb_detect()))
393 __reset_cookie();
394 power_off();
397 /* Start with the main backlight OFF. */
398 __backlight_init();
399 __backlight_off();
401 /* Remote backlight ON */
402 #ifdef HAVE_REMOTE_LCD
403 __remote_backlight_on();
404 #endif
406 system_init();
407 kernel_init();
409 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
410 /* Set up waitstates for the peripherals */
411 set_cpu_frequency(0); /* PLL off */
412 #ifdef CPU_COLDFIRE
413 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
414 #endif
415 #endif
416 set_irq_level(0);
418 #ifdef HAVE_EEPROM_SETTINGS
419 initialize_eeprom();
420 #endif
422 adc_init();
423 button_init();
425 if ((on_button && button_hold()) ||
426 (rc_on_button && remote_button_hold()))
428 hold_status = true;
431 /* Power on the hard drive early, to speed up the loading. */
432 if (!hold_status
433 # ifdef HAVE_EEPROM_SETTINGS
434 && !recovery_mode
435 # endif
438 ide_power_enable(true);
441 # ifdef EEPROM_SETTINGS
442 if (!hold_status && !usb_detect() && !recovery_mode)
443 try_flashboot();
444 # endif
446 backlight_init();
447 #ifdef HAVE_UDA1380
448 audiohw_reset();
449 #endif
451 lcd_init();
452 #ifdef HAVE_REMOTE_LCD
453 lcd_remote_init();
454 #endif
455 font_init();
457 lcd_setfont(FONT_SYSFIXED);
459 printf("Rockbox boot loader");
460 printf("Version %s", version);
462 sleep(HZ/50); /* Allow the button driver to check the buttons */
463 rec_button = ((button_status() & BUTTON_REC) == BUTTON_REC)
464 || ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC);
466 check_battery();
468 /* Don't start if the Hold button is active on the device you
469 are starting with */
470 if (!usb_detect() && (hold_status
471 #ifdef HAVE_EEPROM_SETTINGS
472 || recovery_mode
473 #endif
476 if (detect_original_firmware())
478 printf("Hold switch on");
479 shutdown();
482 #ifdef HAVE_EEPROM_SETTINGS
483 failsafe_menu();
484 #endif
487 /* Holding REC while starting runs the original firmware */
488 if (detect_original_firmware() && rec_button)
490 printf("Starting original firmware...");
491 start_iriver_fw();
494 usb_init();
496 /* A hack to enter USB mode without using the USB thread */
497 if(usb_detect())
499 const char msg[] = "Bootloader USB mode";
500 int w, h;
501 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
502 reset_screen();
503 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
504 lcd_update();
506 #ifdef HAVE_REMOTE_LCD
507 lcd_remote_puts(0, 3, msg);
508 lcd_remote_update();
509 #endif
511 #ifdef HAVE_EEPROM_SETTINGS
512 if (firmware_settings.initialized)
514 firmware_settings.disk_clean = false;
515 eeprom_settings_store();
517 #endif
518 ide_power_enable(true);
519 ata_enable(false);
520 sleep(HZ/20);
521 usb_enable(true);
522 cpu_idle_mode(true);
523 while (usb_detect())
525 /* Print the battery status. */
526 line = 0;
527 remote_line = 0;
528 check_battery();
530 ata_spin(); /* Prevent the drive from spinning down */
531 sleep(HZ);
533 /* Backlight OFF */
534 __backlight_off();
537 cpu_idle_mode(false);
538 usb_enable(false);
540 reset_screen();
541 lcd_update();
544 rc = ata_init();
545 if(rc)
547 reset_screen();
548 printf("ATA error: %d", rc);
549 printf("Insert USB cable and press");
550 printf("a button");
551 while(!(button_get(true) & BUTTON_REL));
555 disk_init();
557 rc = disk_mount_all();
558 if (rc<=0)
560 reset_screen();
561 printf("No partition found");
562 while(button_get(true) != SYS_USB_CONNECTED) {};
565 printf("Loading firmware");
566 i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE);
567 if(i < 0)
568 printf("Error: %d", strerror(i));
570 if (i == EOK)
571 start_firmware();
573 if (!detect_original_firmware())
575 printf("No firmware found on disk");
576 sleep(HZ*2);
577 shutdown();
579 else {
580 sleep(HZ*2);
581 start_iriver_fw();
585 /* These functions are present in the firmware library, but we reimplement
586 them here because the originals do a lot more than we want */
587 void screen_dump(void)
591 int usb_screen(void)
593 return 0;
596 unsigned short *bidi_l2v(const unsigned char *str, int orientation)
598 static unsigned short utf16_buf[SCROLL_LINE_SIZE];
599 unsigned short *target;
600 (void)orientation;
602 target = utf16_buf;
604 while (*str)
605 str = utf8decode(str, target++);
606 *target = 0;
607 return utf16_buf;