1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 by Linus Nielsen Feltzing
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 ****************************************************************************/
30 #include "lcd-remote.h"
31 #include "scroll_engine.h"
39 #include "backlight.h"
40 #include "backlight-target.h"
44 #include "powermgmt.h"
46 #include "eeprom_settings.h"
47 #include "rbunicode.h"
53 /* Maximum allowed firmware image size. 10MB is more than enough */
54 #define MAX_LOADSIZE (10*1024*1024)
56 #define DRAM_START 0x31000000
58 #ifdef HAVE_EEPROM_SETTINGS
59 static bool recovery_mode
= false;
62 /* Reset the cookie for the crt0 crash check */
63 inline void __reset_cookie(void)
65 asm(" move.l #0,%d0");
66 asm(" move.l %d0,0x10017ffc");
69 void start_iriver_fw(void)
71 asm(" move.w #0x2700,%sr");
73 asm(" movec.l %d0,%vbr");
79 void start_firmware(void)
81 asm(" move.w #0x2700,%sr");
83 asm(" move.l %0,%%d0" :: "i"(DRAM_START
));
84 asm(" movec.l %d0,%vbr");
85 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START
));
86 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START
+4)));
90 #ifdef IRIVER_H100_SERIES
91 void start_flashed_romimage(void)
93 uint8_t *src
= (uint8_t *)FLASH_ROMIMAGE_ENTRY
;
96 if (!detect_flashed_romimage())
99 reset_vector
= (int *)(&src
[sizeof(struct flash_header
)+4]);
101 asm(" move.w #0x2700,%sr");
104 asm(" move.l %0,%%d0" :: "i"(DRAM_START
));
105 asm(" movec.l %d0,%vbr");
106 asm(" move.l %0,%%sp" :: "m"(reset_vector
[0]));
107 asm(" move.l %0,%%a0" :: "m"(reset_vector
[1]));
114 void start_flashed_ramimage(void)
116 struct flash_header hdr
;
117 unsigned char *buf
= (unsigned char *)DRAM_START
;
118 uint8_t *src
= (uint8_t *)FLASH_RAMIMAGE_ENTRY
;
120 if (!detect_flashed_ramimage())
123 /* Load firmware from flash */
125 memcpy(&hdr
, src
, sizeof(struct flash_header
));
126 src
+= sizeof(struct flash_header
);
127 memcpy(buf
, src
, hdr
.length
);
135 #endif /* IRIVER_H100_SERIES */
139 printf("Shutting down...");
140 #ifdef HAVE_EEPROM_SETTINGS
141 /* Reset the rockbox crash check. */
142 firmware_settings
.bl_version
= 0;
143 eeprom_settings_store();
146 /* We need to gracefully spin down the disk to prevent clicks. */
149 /* Make sure ATA has been initialized. */
152 /* And put the disk into sleep immediately. */
160 #ifdef HAVE_REMOTE_LCD
161 _remote_backlight_off();
168 /* Print the battery voltage (and a warning message). */
169 void check_battery(void)
171 int battery_voltage
, batt_int
, batt_frac
;
173 battery_voltage
= battery_adc_voltage();
174 batt_int
= battery_voltage
/ 1000;
175 batt_frac
= (battery_voltage
% 1000) / 10;
177 printf("Batt: %d.%02dV", batt_int
, batt_frac
);
179 if (battery_voltage
<= 310)
181 printf("WARNING! BATTERY LOW!!");
186 #ifdef HAVE_EEPROM_SETTINGS
187 void initialize_eeprom(void)
189 if (detect_original_firmware())
192 if (!eeprom_settings_init())
194 recovery_mode
= true;
198 /* If bootloader version has not been reset, disk might
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
)
215 switch (firmware_settings
.bootmethod
)
221 start_flashed_romimage();
222 recovery_mode
= true;
226 start_flashed_ramimage();
227 recovery_mode
= true;
231 recovery_mode
= true;
236 static const char *options
[] = {
243 #define FAILSAFE_OPTIONS 4
244 #define TIMEOUT (15*HZ)
245 void failsafe_menu(void)
247 long start_tick
= current_tick
;
256 printf("Bootloader " RBVERSION
);
258 printf("=========================");
259 line
+= FAILSAFE_OPTIONS
;
261 printf(" [NAVI] to confirm.");
262 printf(" [REC] to set as default.");
265 if (firmware_settings
.initialized
)
267 defopt
= firmware_settings
.bootmethod
;
268 if (defopt
< 0 || defopt
>= FAILSAFE_OPTIONS
)
272 while (current_tick
- start_tick
< TIMEOUT
)
276 for (i
= 0; i
< FAILSAFE_OPTIONS
; i
++)
286 printf("%s %s %s", arrow
, options
[i
], def
);
289 snprintf(buf
, sizeof(buf
), "Time left: %lds",
290 (TIMEOUT
- (current_tick
- start_tick
)) / HZ
);
291 lcd_puts(0, 10, buf
);
293 button
= button_get_w_tmo(HZ
);
295 if (button
== BUTTON_NONE
|| button
& SYS_EVENT
)
298 start_tick
= current_tick
;
300 /* Ignore the ON/PLAY -button because it can cause trouble
301 with the RTC alarm mod. */
302 switch (button
& ~(BUTTON_ON
))
312 if (option
< FAILSAFE_OPTIONS
-1)
322 if (firmware_settings
.initialized
)
324 firmware_settings
.bootmethod
= option
;
325 eeprom_settings_store();
334 lcd_puts(0, 10, "Executing command...");
345 start_flashed_ramimage();
346 printf("Image not found");
350 start_flashed_romimage();
351 printf("Image not found");
359 /* get rid of a nasty humming sound during boot
361 inline static void __uda1380_reset_hi(void)
364 or_l(1<<29, &GPIO_OUT
);
365 or_l(1<<29, &GPIO_ENABLE
);
366 or_l(1<<29, &GPIO_FUNCTION
);
370 inline static void __uda1380_reset_lo(void)
373 and_l(~(1<<29), &GPIO_OUT
);
381 bool rc_on_button
= false;
382 bool on_button
= false;
383 bool rec_button
= false;
384 bool hold_status
= false;
386 extern int line
; /* From common.c */
387 extern int remote_line
; /* From common.c */
389 /* We want to read the buttons as early as possible, before the user
390 releases the ON button */
392 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
393 (The ON and Hold buttons on the main unit and the remote) */
394 or_l(0x00100062, &GPIO1_FUNCTION
);
395 and_l(~0x00100062, &GPIO1_ENABLE
);
398 if ((data
& 0x20) == 0)
401 if ((data
& 0x40) == 0)
404 /* Set the default state of the hard drive power to OFF */
405 ide_power_enable(false);
409 /* Turn off if neither ON button is pressed */
410 if (!(on_button
|| rc_on_button
))
416 __uda1380_reset_hi();
418 /* Start with the main backlight OFF. */
422 /* Remote backlight ON */
423 #ifdef HAVE_REMOTE_LCD
424 _remote_backlight_on();
430 __uda1380_reset_lo();
432 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
433 /* Set up waitstates for the peripherals */
434 set_cpu_frequency(0); /* PLL off */
436 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS
);
441 #ifdef HAVE_EEPROM_SETTINGS
446 /* A small delay after usb_init is necessary to read the I/O port correctly
447 (if ports are read _immediately_ after the init). */
453 /* Only check remote hold status if remote power button was actually used. */
458 /* Allow the button driver to check the buttons */
461 if (remote_button_hold())
465 /* Check main hold switch status too. */
466 if (on_button
&& button_hold())
471 /* Power on the hard drive early, to speed up the loading. */
473 # ifdef HAVE_EEPROM_SETTINGS
478 ide_power_enable(true);
481 # ifdef HAVE_EEPROM_SETTINGS
482 if (!hold_status
&& (usb_detect() != USB_INSERTED
) && !recovery_mode
)
494 /* Bootloader uses simplified backlight thread, so we need to enable
495 remote display here. */
501 lcd_setfont(FONT_SYSFIXED
);
503 printf("Rockbox boot loader");
504 printf("Version " RBVERSION
);
506 /* No need to wait here more because lcd_init and others already do that. */
507 // sleep(HZ/50); /* Allow the button driver to check the buttons */
508 rec_button
= ((button_status() & BUTTON_REC
) == BUTTON_REC
)
509 || ((button_status() & BUTTON_RC_REC
) == BUTTON_RC_REC
);
513 /* Don't start if the Hold button is active on the device you
515 if ((usb_detect() != USB_INSERTED
) && (hold_status
516 #ifdef HAVE_EEPROM_SETTINGS
521 if (detect_original_firmware())
523 printf("Hold switch on");
527 #ifdef HAVE_EEPROM_SETTINGS
532 /* Holding REC while starting runs the original firmware */
533 if (detect_original_firmware() && rec_button
)
535 printf("Starting original firmware...");
539 /* A hack to enter USB mode without using the USB thread */
540 if(usb_detect() == USB_INSERTED
)
542 const char msg
[] = "Bootloader USB mode";
544 font_getstringsize(msg
, &w
, &h
, FONT_SYSFIXED
);
546 lcd_putsxy((LCD_WIDTH
-w
)/2, (LCD_HEIGHT
-h
)/2, msg
);
549 #ifdef HAVE_REMOTE_LCD
550 lcd_remote_puts(0, 3, msg
);
554 #ifdef HAVE_EEPROM_SETTINGS
555 if (firmware_settings
.initialized
)
557 firmware_settings
.disk_clean
= false;
558 eeprom_settings_store();
561 ide_power_enable(true);
562 storage_enable(false);
566 while (usb_detect() == USB_INSERTED
)
568 /* Print the battery status. */
573 storage_spin(); /* Prevent the drive from spinning down */
580 cpu_idle_mode(false);
591 printf("ATA error: %d", rc
);
592 printf("Insert USB cable and press");
594 while(!(button_get(true) & BUTTON_REL
));
600 rc
= disk_mount_all();
604 printf("No partition found");
605 while(button_get(true) != SYS_USB_CONNECTED
) {};
608 printf("Loading firmware");
609 i
= load_firmware((unsigned char *)DRAM_START
, BOOTFILE
, MAX_LOADSIZE
);
611 printf("Error: %s", strerror(i
));
616 if (!detect_original_firmware())
618 printf("No firmware found on disk");
628 /* These functions are present in the firmware library, but we reimplement
629 them here because the originals do a lot more than we want */
630 void screen_dump(void)
639 unsigned short *bidi_l2v(const unsigned char *str
, int orientation
)
641 static unsigned short utf16_buf
[SCROLL_LINE_SIZE
];
642 unsigned short *target
;
648 str
= utf8decode(str
, target
++);