Small optimisation
[Rockbox.git] / bootloader / main.c
blobd78b4adc69cb0f92fdb043389bb54ab0db40e43e
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 "file.h"
43 #include "uda1380.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 adc_battery, battery_voltage, batt_int, batt_frac;
172 adc_battery = adc_read(ADC_BATTERY);
174 battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000;
175 batt_int = battery_voltage / 100;
176 batt_frac = battery_voltage % 100;
178 printf("Batt: %d.%02dV", batt_int, batt_frac);
180 if (battery_voltage <= 310)
182 printf("WARNING! BATTERY LOW!!");
183 sleep(HZ*2);
187 #ifdef HAVE_EEPROM_SETTINGS
188 void initialize_eeprom(void)
190 if (detect_original_firmware())
191 return ;
193 if (!eeprom_settings_init())
195 recovery_mode = true;
196 return ;
199 /* If bootloader version has not been reset, disk might
200 * not be intact. */
201 if (firmware_settings.bl_version || !firmware_settings.disk_clean)
203 firmware_settings.disk_clean = false;
204 recovery_mode = true;
207 firmware_settings.bl_version = EEPROM_SETTINGS_BL_MINVER;
208 eeprom_settings_store();
211 void try_flashboot(void)
213 if (!firmware_settings.initialized)
214 return ;
216 switch (firmware_settings.bootmethod)
218 case BOOT_DISK:
219 return;
221 case BOOT_ROM:
222 start_flashed_romimage();
223 recovery_mode = true;
224 break;
226 case BOOT_RAM:
227 start_flashed_ramimage();
228 recovery_mode = true;
229 break;
231 default:
232 recovery_mode = true;
233 return;
237 static const char *options[] = {
238 "Boot from disk",
239 "Boot RAM image",
240 "Boot ROM image",
241 "Shutdown"
244 #define FAILSAFE_OPTIONS 4
245 void failsafe_menu(void)
247 int timeout = 15;
248 int option = 3;
249 int button;
250 int defopt = -1;
251 char buf[32];
252 int i;
253 extern int line;
255 reset_screen();
256 printf("Bootloader %s", version);
257 check_battery();
258 printf("=========================");
259 line += FAILSAFE_OPTIONS;
260 printf("");
261 printf(" [NAVI] to confirm.");
262 printf(" [REC] to set as default.");
263 printf("");
265 if (firmware_settings.initialized)
267 defopt = firmware_settings.bootmethod;
268 if (defopt < 0 || defopt >= FAILSAFE_OPTIONS)
269 defopt = option;
272 while (timeout > 0)
274 /* Draw the menu. */
275 line = 3;
276 for (i = 0; i < FAILSAFE_OPTIONS; i++)
278 char *def = "[DEF]";
279 char *arrow = "->";
281 if (i != defopt)
282 def = "";
283 if (i != option)
284 arrow = " ";
286 printf("%s %s %s", arrow, options[i], def);
289 snprintf(buf, sizeof(buf), "Time left: %ds", timeout);
290 lcd_puts(0, 10, buf);
291 lcd_update();
292 button = button_get_w_tmo(HZ);
294 if (button == BUTTON_NONE)
296 timeout--;
297 continue ;
300 timeout = 15;
301 /* Ignore the ON/PLAY -button because it can cause trouble
302 with the RTC alarm mod. */
303 switch (button & ~(BUTTON_ON))
305 case BUTTON_UP:
306 case BUTTON_RC_REW:
307 if (option > 0)
308 option--;
309 break ;
311 case BUTTON_DOWN:
312 case BUTTON_RC_FF:
313 if (option < FAILSAFE_OPTIONS-1)
314 option++;
315 break ;
317 case BUTTON_SELECT:
318 case BUTTON_RC_ON:
319 timeout = 0;
320 break ;
322 case BUTTON_REC:
323 case BUTTON_RC_REC:
324 if (firmware_settings.initialized)
326 firmware_settings.bootmethod = option;
327 eeprom_settings_store();
328 defopt = option;
330 break ;
334 lcd_puts(0, 10, "Executing command...");
335 lcd_update();
336 sleep(HZ);
337 reset_screen();
339 switch (option)
341 case BOOT_DISK:
342 return ;
344 case BOOT_RAM:
345 start_flashed_ramimage();
346 printf("Image not found");
347 break;
349 case BOOT_ROM:
350 start_flashed_romimage();
351 printf("Image not found");
352 break;
355 shutdown();
357 #endif
359 void main(void)
361 int i;
362 int rc;
363 bool rc_on_button = false;
364 bool on_button = false;
365 bool rec_button = false;
366 bool hold_status = false;
367 int data;
368 extern int line; /* From common.c */
369 extern int remote_line; /* From common.c */
371 /* We want to read the buttons as early as possible, before the user
372 releases the ON button */
374 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
375 (The ON and Hold buttons on the main unit and the remote) */
376 or_l(0x00100062, &GPIO1_FUNCTION);
377 and_l(~0x00100062, &GPIO1_ENABLE);
379 data = GPIO1_READ;
380 if ((data & 0x20) == 0)
381 on_button = true;
383 if ((data & 0x40) == 0)
384 rc_on_button = true;
386 /* Set the default state of the hard drive power to OFF */
387 ide_power_enable(false);
389 power_init();
391 /* Turn off if neither ON button is pressed */
392 if(!(on_button || rc_on_button || usb_detect()))
394 __reset_cookie();
395 power_off();
398 /* Start with the main backlight OFF. */
399 __backlight_init();
400 __backlight_off();
402 /* Remote backlight ON */
403 #ifdef HAVE_REMOTE_LCD
404 __remote_backlight_on();
405 #endif
407 system_init();
408 kernel_init();
410 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
411 /* Set up waitstates for the peripherals */
412 set_cpu_frequency(0); /* PLL off */
413 #ifdef CPU_COLDFIRE
414 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
415 #endif
416 #endif
417 set_irq_level(0);
419 #ifdef HAVE_EEPROM_SETTINGS
420 initialize_eeprom();
421 #endif
423 adc_init();
424 button_init();
426 if ((on_button && button_hold()) ||
427 (rc_on_button && remote_button_hold()))
429 hold_status = true;
432 /* Power on the hard drive early, to speed up the loading. */
433 if (!hold_status
434 # ifdef HAVE_EEPROM_SETTINGS
435 && !recovery_mode
436 # endif
439 ide_power_enable(true);
442 # ifdef EEPROM_SETTINGS
443 if (!hold_status && !usb_detect() && !recovery_mode)
444 try_flashboot();
445 # endif
447 backlight_init();
448 #ifdef HAVE_UDA1380
449 audiohw_reset();
450 #endif
452 lcd_init();
453 #ifdef HAVE_REMOTE_LCD
454 lcd_remote_init();
455 #endif
456 font_init();
458 lcd_setfont(FONT_SYSFIXED);
460 printf("Rockbox boot loader");
461 printf("Version %s", version);
463 sleep(HZ/50); /* Allow the button driver to check the buttons */
464 rec_button = ((button_status() & BUTTON_REC) == BUTTON_REC)
465 || ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC);
467 check_battery();
469 /* Don't start if the Hold button is active on the device you
470 are starting with */
471 if (!usb_detect() && (hold_status
472 #ifdef HAVE_EEPROM_SETTINGS
473 || recovery_mode
474 #endif
477 if (detect_original_firmware())
479 printf("Hold switch on");
480 shutdown();
483 #ifdef HAVE_EEPROM_SETTINGS
484 failsafe_menu();
485 #endif
488 /* Holding REC while starting runs the original firmware */
489 if (detect_original_firmware() && rec_button)
491 printf("Starting original firmware...");
492 start_iriver_fw();
495 usb_init();
497 /* A hack to enter USB mode without using the USB thread */
498 if(usb_detect())
500 const char msg[] = "Bootloader USB mode";
501 int w, h;
502 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
503 reset_screen();
504 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
505 lcd_update();
507 #ifdef HAVE_REMOTE_LCD
508 lcd_remote_puts(0, 3, msg);
509 lcd_remote_update();
510 #endif
512 #ifdef HAVE_EEPROM_SETTINGS
513 if (firmware_settings.initialized)
515 firmware_settings.disk_clean = false;
516 eeprom_settings_store();
518 #endif
519 ide_power_enable(true);
520 ata_enable(false);
521 sleep(HZ/20);
522 usb_enable(true);
523 cpu_idle_mode(true);
524 while (usb_detect())
526 /* Print the battery status. */
527 line = 0;
528 remote_line = 0;
529 check_battery();
531 ata_spin(); /* Prevent the drive from spinning down */
532 sleep(HZ);
534 /* Backlight OFF */
535 __backlight_off();
538 cpu_idle_mode(false);
539 usb_enable(false);
541 reset_screen();
542 lcd_update();
545 rc = ata_init();
546 if(rc)
548 reset_screen();
549 printf("ATA error: %d", rc);
550 printf("Insert USB cable and press");
551 printf("a button");
552 while(!(button_get(true) & BUTTON_REL));
556 disk_init();
558 rc = disk_mount_all();
559 if (rc<=0)
561 reset_screen();
562 printf("No partition found");
563 while(button_get(true) != SYS_USB_CONNECTED) {};
566 printf("Loading firmware");
567 i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE);
568 if(i < 0)
569 printf("Error: %s", strerror(i));
571 if (i == EOK)
572 start_firmware();
574 if (!detect_original_firmware())
576 printf("No firmware found on disk");
577 sleep(HZ*2);
578 shutdown();
580 else {
581 sleep(HZ*2);
582 start_iriver_fw();
586 /* These functions are present in the firmware library, but we reimplement
587 them here because the originals do a lot more than we want */
588 void screen_dump(void)
592 int usb_screen(void)
594 return 0;
597 unsigned short *bidi_l2v(const unsigned char *str, int orientation)
599 static unsigned short utf16_buf[SCROLL_LINE_SIZE];
600 unsigned short *target;
601 (void)orientation;
603 target = utf16_buf;
605 while (*str)
606 str = utf8decode(str, target++);
607 *target = 0;
608 return utf16_buf;