libmpeg2: Change a couple style points for consistency and some spacing.
[Rockbox.git] / bootloader / main.c
blobb1a29f992ac04a707553210abc0f6c927208a1d9
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 "scroll_engine.h"
30 #include "kernel.h"
31 #include "thread.h"
32 #include "ata.h"
33 #include "usb.h"
34 #include "disk.h"
35 #include "font.h"
36 #include "adc.h"
37 #include "backlight.h"
38 #include "backlight-target.h"
39 #include "button.h"
40 #include "panic.h"
41 #include "power.h"
42 #include "powermgmt.h"
43 #include "file.h"
44 #include "eeprom_settings.h"
45 #include "rbunicode.h"
46 #include "common.h"
48 #include <stdarg.h>
50 /* Maximum allowed firmware image size. 10MB is more than enough */
51 #define MAX_LOADSIZE (10*1024*1024)
53 #define DRAM_START 0x31000000
55 #ifdef HAVE_EEPROM_SETTINGS
56 static bool recovery_mode = false;
57 #endif
59 char version[] = APPSVERSION;
61 /* Reset the cookie for the crt0 crash check */
62 inline void __reset_cookie(void)
64 asm(" move.l #0,%d0");
65 asm(" move.l %d0,0x10017ffc");
68 void start_iriver_fw(void)
70 asm(" move.w #0x2700,%sr");
71 __reset_cookie();
72 asm(" movec.l %d0,%vbr");
73 asm(" move.l 0,%sp");
74 asm(" lea.l 8,%a0");
75 asm(" jmp (%a0)");
78 void start_firmware(void)
80 asm(" move.w #0x2700,%sr");
81 __reset_cookie();
82 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
83 asm(" movec.l %d0,%vbr");
84 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
85 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
86 asm(" jmp (%a0)");
89 #ifdef IRIVER_H100_SERIES
90 void start_flashed_romimage(void)
92 uint8_t *src = (uint8_t *)FLASH_ROMIMAGE_ENTRY;
93 int *reset_vector;
95 if (!detect_flashed_romimage())
96 return ;
98 reset_vector = (int *)(&src[sizeof(struct flash_header)+4]);
100 asm(" move.w #0x2700,%sr");
101 __reset_cookie();
103 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
104 asm(" movec.l %d0,%vbr");
105 asm(" move.l %0,%%sp" :: "m"(reset_vector[0]));
106 asm(" move.l %0,%%a0" :: "m"(reset_vector[1]));
107 asm(" jmp (%a0)");
109 /* Failure */
110 power_off();
113 void start_flashed_ramimage(void)
115 struct flash_header hdr;
116 unsigned char *buf = (unsigned char *)DRAM_START;
117 uint8_t *src = (uint8_t *)FLASH_RAMIMAGE_ENTRY;
119 if (!detect_flashed_ramimage())
120 return;
122 /* Load firmware from flash */
123 cpu_boost(true);
124 memcpy(&hdr, src, sizeof(struct flash_header));
125 src += sizeof(struct flash_header);
126 memcpy(buf, src, hdr.length);
127 cpu_boost(false);
129 start_firmware();
131 /* Failure */
132 power_off();
134 #endif /* IRIVER_H100_SERIES */
136 void shutdown(void)
138 printf("Shutting down...");
139 #ifdef HAVE_EEPROM_SETTINGS
140 /* Reset the rockbox crash check. */
141 firmware_settings.bl_version = 0;
142 eeprom_settings_store();
143 #endif
145 /* We need to gracefully spin down the disk to prevent clicks. */
146 if (ide_powered())
148 /* Make sure ATA has been initialized. */
149 ata_init();
151 /* And put the disk into sleep immediately. */
152 ata_sleepnow();
155 sleep(HZ*2);
157 /* Backlight OFF */
158 _backlight_off();
159 #ifdef HAVE_REMOTE_LCD
160 _remote_backlight_off();
161 #endif
163 __reset_cookie();
164 power_off();
167 /* Print the battery voltage (and a warning message). */
168 void check_battery(void)
170 int battery_voltage, batt_int, batt_frac;
172 battery_voltage = battery_adc_voltage();
173 batt_int = battery_voltage / 1000;
174 batt_frac = (battery_voltage % 1000) / 10;
176 printf("Batt: %d.%02dV", batt_int, batt_frac);
178 if (battery_voltage <= 310)
180 printf("WARNING! BATTERY LOW!!");
181 sleep(HZ*2);
185 #ifdef HAVE_EEPROM_SETTINGS
186 void initialize_eeprom(void)
188 if (detect_original_firmware())
189 return ;
191 if (!eeprom_settings_init())
193 recovery_mode = true;
194 return ;
197 /* If bootloader version has not been reset, disk might
198 * not be intact. */
199 if (firmware_settings.bl_version || !firmware_settings.disk_clean)
201 firmware_settings.disk_clean = false;
202 recovery_mode = true;
205 firmware_settings.bl_version = EEPROM_SETTINGS_BL_MINVER;
206 eeprom_settings_store();
209 void try_flashboot(void)
211 if (!firmware_settings.initialized)
212 return ;
214 switch (firmware_settings.bootmethod)
216 case BOOT_DISK:
217 return;
219 case BOOT_ROM:
220 start_flashed_romimage();
221 recovery_mode = true;
222 break;
224 case BOOT_RAM:
225 start_flashed_ramimage();
226 recovery_mode = true;
227 break;
229 default:
230 recovery_mode = true;
231 return;
235 static const char *options[] = {
236 "Boot from disk",
237 "Boot RAM image",
238 "Boot ROM image",
239 "Shutdown"
242 #define FAILSAFE_OPTIONS 4
243 void failsafe_menu(void)
245 int timeout = 15;
246 int option = 3;
247 int button;
248 int defopt = -1;
249 char buf[32];
250 int i;
251 extern int line;
253 reset_screen();
254 printf("Bootloader %s", version);
255 check_battery();
256 printf("=========================");
257 line += FAILSAFE_OPTIONS;
258 printf("");
259 printf(" [NAVI] to confirm.");
260 printf(" [REC] to set as default.");
261 printf("");
263 if (firmware_settings.initialized)
265 defopt = firmware_settings.bootmethod;
266 if (defopt < 0 || defopt >= FAILSAFE_OPTIONS)
267 defopt = option;
270 while (timeout > 0)
272 /* Draw the menu. */
273 line = 3;
274 for (i = 0; i < FAILSAFE_OPTIONS; i++)
276 char *def = "[DEF]";
277 char *arrow = "->";
279 if (i != defopt)
280 def = "";
281 if (i != option)
282 arrow = " ";
284 printf("%s %s %s", arrow, options[i], def);
287 snprintf(buf, sizeof(buf), "Time left: %ds", timeout);
288 lcd_puts(0, 10, buf);
289 lcd_update();
290 button = button_get_w_tmo(HZ);
292 if (button == BUTTON_NONE)
294 timeout--;
295 continue ;
298 timeout = 15;
299 /* Ignore the ON/PLAY -button because it can cause trouble
300 with the RTC alarm mod. */
301 switch (button & ~(BUTTON_ON))
303 case BUTTON_UP:
304 case BUTTON_RC_REW:
305 if (option > 0)
306 option--;
307 break ;
309 case BUTTON_DOWN:
310 case BUTTON_RC_FF:
311 if (option < FAILSAFE_OPTIONS-1)
312 option++;
313 break ;
315 case BUTTON_SELECT:
316 case BUTTON_RC_ON:
317 timeout = 0;
318 break ;
320 case BUTTON_REC:
321 case BUTTON_RC_REC:
322 if (firmware_settings.initialized)
324 firmware_settings.bootmethod = option;
325 eeprom_settings_store();
326 defopt = option;
328 break ;
332 lcd_puts(0, 10, "Executing command...");
333 lcd_update();
334 sleep(HZ);
335 reset_screen();
337 switch (option)
339 case BOOT_DISK:
340 return ;
342 case BOOT_RAM:
343 start_flashed_ramimage();
344 printf("Image not found");
345 break;
347 case BOOT_ROM:
348 start_flashed_romimage();
349 printf("Image not found");
350 break;
353 shutdown();
355 #endif
357 void main(void)
359 int i;
360 int rc;
361 bool rc_on_button = false;
362 bool on_button = false;
363 bool rec_button = false;
364 bool hold_status = false;
365 int data;
366 extern int line; /* From common.c */
367 extern int remote_line; /* From common.c */
369 /* We want to read the buttons as early as possible, before the user
370 releases the ON button */
372 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
373 (The ON and Hold buttons on the main unit and the remote) */
374 or_l(0x00100062, &GPIO1_FUNCTION);
375 and_l(~0x00100062, &GPIO1_ENABLE);
377 data = GPIO1_READ;
378 if ((data & 0x20) == 0)
379 on_button = true;
381 if ((data & 0x40) == 0)
382 rc_on_button = true;
384 /* Set the default state of the hard drive power to OFF */
385 ide_power_enable(false);
387 power_init();
389 /* Turn off if neither ON button is pressed */
390 if(!(on_button || rc_on_button || (usb_detect() == USB_INSERTED)))
392 __reset_cookie();
393 power_off();
396 /* Start with the main backlight OFF. */
397 _backlight_init();
398 _backlight_off();
400 /* Remote backlight ON */
401 #ifdef HAVE_REMOTE_LCD
402 _remote_backlight_on();
403 #endif
405 system_init();
406 kernel_init();
408 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
409 /* Set up waitstates for the peripherals */
410 set_cpu_frequency(0); /* PLL off */
411 #ifdef CPU_COLDFIRE
412 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
413 #endif
414 #endif
415 set_irq_level(0);
417 #ifdef HAVE_EEPROM_SETTINGS
418 initialize_eeprom();
419 #endif
421 adc_init();
422 button_init();
424 if ((on_button && button_hold()) ||
425 (rc_on_button && remote_button_hold()))
427 hold_status = true;
430 /* Power on the hard drive early, to speed up the loading. */
431 if (!hold_status
432 # ifdef HAVE_EEPROM_SETTINGS
433 && !recovery_mode
434 # endif
437 ide_power_enable(true);
440 # ifdef EEPROM_SETTINGS
441 if (!hold_status && (usb_detect() != USB_INSERTED) && !recovery_mode)
442 try_flashboot();
443 # endif
445 backlight_init();
447 #ifdef HAVE_UDA1380
448 /* get rid of a nasty humming sound during boot
449 -> RESET signal */
450 or_l(1<<29, &GPIO_OUT);
451 or_l(1<<29, &GPIO_ENABLE);
452 or_l(1<<29, &GPIO_FUNCTION);
453 sleep(HZ/100);
454 and_l(~(1<<29), &GPIO_OUT);
455 #endif
457 lcd_init();
458 #ifdef HAVE_REMOTE_LCD
459 lcd_remote_init();
460 #endif
461 font_init();
463 lcd_setfont(FONT_SYSFIXED);
465 printf("Rockbox boot loader");
466 printf("Version %s", version);
468 sleep(HZ/50); /* Allow the button driver to check the buttons */
469 rec_button = ((button_status() & BUTTON_REC) == BUTTON_REC)
470 || ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC);
472 check_battery();
474 /* Don't start if the Hold button is active on the device you
475 are starting with */
476 if ((usb_detect() != USB_INSERTED) && (hold_status
477 #ifdef HAVE_EEPROM_SETTINGS
478 || recovery_mode
479 #endif
482 if (detect_original_firmware())
484 printf("Hold switch on");
485 shutdown();
488 #ifdef HAVE_EEPROM_SETTINGS
489 failsafe_menu();
490 #endif
493 /* Holding REC while starting runs the original firmware */
494 if (detect_original_firmware() && rec_button)
496 printf("Starting original firmware...");
497 start_iriver_fw();
500 usb_init();
502 /* A hack to enter USB mode without using the USB thread */
503 if(usb_detect() == USB_INSERTED)
505 const char msg[] = "Bootloader USB mode";
506 int w, h;
507 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
508 reset_screen();
509 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
510 lcd_update();
512 #ifdef HAVE_REMOTE_LCD
513 lcd_remote_puts(0, 3, msg);
514 lcd_remote_update();
515 #endif
517 #ifdef HAVE_EEPROM_SETTINGS
518 if (firmware_settings.initialized)
520 firmware_settings.disk_clean = false;
521 eeprom_settings_store();
523 #endif
524 ide_power_enable(true);
525 ata_enable(false);
526 sleep(HZ/20);
527 usb_enable(true);
528 cpu_idle_mode(true);
529 while (usb_detect() == USB_INSERTED)
531 /* Print the battery status. */
532 line = 0;
533 remote_line = 0;
534 check_battery();
536 ata_spin(); /* Prevent the drive from spinning down */
537 sleep(HZ);
539 /* Backlight OFF */
540 _backlight_off();
543 cpu_idle_mode(false);
544 usb_enable(false);
546 reset_screen();
547 lcd_update();
550 rc = ata_init();
551 if(rc)
553 reset_screen();
554 printf("ATA error: %d", rc);
555 printf("Insert USB cable and press");
556 printf("a button");
557 while(!(button_get(true) & BUTTON_REL));
561 disk_init();
563 rc = disk_mount_all();
564 if (rc<=0)
566 reset_screen();
567 printf("No partition found");
568 while(button_get(true) != SYS_USB_CONNECTED) {};
571 printf("Loading firmware");
572 i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE);
573 if(i < 0)
574 printf("Error: %s", strerror(i));
576 if (i == EOK)
577 start_firmware();
579 if (!detect_original_firmware())
581 printf("No firmware found on disk");
582 sleep(HZ*2);
583 shutdown();
585 else {
586 sleep(HZ*2);
587 start_iriver_fw();
591 /* These functions are present in the firmware library, but we reimplement
592 them here because the originals do a lot more than we want */
593 void screen_dump(void)
597 int usb_screen(void)
599 return 0;
602 unsigned short *bidi_l2v(const unsigned char *str, int orientation)
604 static unsigned short utf16_buf[SCROLL_LINE_SIZE];
605 unsigned short *target;
606 (void)orientation;
608 target = utf16_buf;
610 while (*str)
611 str = utf8decode(str, target++);
612 *target = 0;
613 return utf16_buf;