1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
28 #include "lcd-remote.h"
29 #include "scroll_engine.h"
37 #include "backlight.h"
38 #include "backlight-target.h"
42 #include "powermgmt.h"
44 #include "eeprom_settings.h"
45 #include "rbunicode.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;
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");
72 asm(" movec.l %d0,%vbr");
78 void start_firmware(void)
80 asm(" move.w #0x2700,%sr");
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)));
89 #ifdef IRIVER_H100_SERIES
90 void start_flashed_romimage(void)
92 uint8_t *src
= (uint8_t *)FLASH_ROMIMAGE_ENTRY
;
95 if (!detect_flashed_romimage())
98 reset_vector
= (int *)(&src
[sizeof(struct flash_header
)+4]);
100 asm(" move.w #0x2700,%sr");
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]));
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())
122 /* Load firmware from flash */
124 memcpy(&hdr
, src
, sizeof(struct flash_header
));
125 src
+= sizeof(struct flash_header
);
126 memcpy(buf
, src
, hdr
.length
);
134 #endif /* IRIVER_H100_SERIES */
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();
145 /* We need to gracefully spin down the disk to prevent clicks. */
148 /* Make sure ATA has been initialized. */
151 /* And put the disk into sleep immediately. */
159 #ifdef HAVE_REMOTE_LCD
160 _remote_backlight_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!!");
185 #ifdef HAVE_EEPROM_SETTINGS
186 void initialize_eeprom(void)
188 if (detect_original_firmware())
191 if (!eeprom_settings_init())
193 recovery_mode
= true;
197 /* If bootloader version has not been reset, disk might
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
)
214 switch (firmware_settings
.bootmethod
)
220 start_flashed_romimage();
221 recovery_mode
= true;
225 start_flashed_ramimage();
226 recovery_mode
= true;
230 recovery_mode
= true;
235 static const char *options
[] = {
242 #define FAILSAFE_OPTIONS 4
243 #define TIMEOUT (15*HZ)
244 void failsafe_menu(void)
246 long start_tick
= current_tick
;
255 printf("Bootloader %s", version
);
257 printf("=========================");
258 line
+= FAILSAFE_OPTIONS
;
260 printf(" [NAVI] to confirm.");
261 printf(" [REC] to set as default.");
264 if (firmware_settings
.initialized
)
266 defopt
= firmware_settings
.bootmethod
;
267 if (defopt
< 0 || defopt
>= FAILSAFE_OPTIONS
)
271 while (current_tick
- start_tick
< TIMEOUT
)
275 for (i
= 0; i
< FAILSAFE_OPTIONS
; i
++)
285 printf("%s %s %s", arrow
, options
[i
], def
);
288 snprintf(buf
, sizeof(buf
), "Time left: %ds",
289 (TIMEOUT
- (current_tick
- start_tick
)) / HZ
);
290 lcd_puts(0, 10, buf
);
292 button
= button_get_w_tmo(HZ
);
294 if (button
== BUTTON_NONE
|| button
& SYS_EVENT
)
297 start_tick
= current_tick
;
299 /* Ignore the ON/PLAY -button because it can cause trouble
300 with the RTC alarm mod. */
301 switch (button
& ~(BUTTON_ON
))
311 if (option
< FAILSAFE_OPTIONS
-1)
321 if (firmware_settings
.initialized
)
323 firmware_settings
.bootmethod
= option
;
324 eeprom_settings_store();
333 lcd_puts(0, 10, "Executing command...");
344 start_flashed_ramimage();
345 printf("Image not found");
349 start_flashed_romimage();
350 printf("Image not found");
358 /* get rid of a nasty humming sound during boot
360 inline static void __uda1380_reset_hi(void)
363 or_l(1<<29, &GPIO_OUT
);
364 or_l(1<<29, &GPIO_ENABLE
);
365 or_l(1<<29, &GPIO_FUNCTION
);
369 inline static void __uda1380_reset_lo(void)
372 and_l(~(1<<29), &GPIO_OUT
);
380 bool rc_on_button
= false;
381 bool on_button
= false;
382 bool rec_button
= false;
383 bool hold_status
= false;
385 extern int line
; /* From common.c */
386 extern int remote_line
; /* From common.c */
388 /* We want to read the buttons as early as possible, before the user
389 releases the ON button */
391 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
392 (The ON and Hold buttons on the main unit and the remote) */
393 or_l(0x00100062, &GPIO1_FUNCTION
);
394 and_l(~0x00100062, &GPIO1_ENABLE
);
397 if ((data
& 0x20) == 0)
400 if ((data
& 0x40) == 0)
403 /* Set the default state of the hard drive power to OFF */
404 ide_power_enable(false);
408 /* Turn off if neither ON button is pressed */
409 if (!(on_button
|| rc_on_button
))
415 __uda1380_reset_hi();
417 /* Start with the main backlight OFF. */
421 /* Remote backlight ON */
422 #ifdef HAVE_REMOTE_LCD
423 _remote_backlight_on();
429 __uda1380_reset_lo();
431 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
432 /* Set up waitstates for the peripherals */
433 set_cpu_frequency(0); /* PLL off */
435 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS
);
440 #ifdef HAVE_EEPROM_SETTINGS
445 /* A small delay after usb_init is necessary to read the I/O port correctly
446 (if ports are read _immediately_ after the init). */
452 /* Only check remote hold status if remote power button was actually used. */
457 /* Allow the button driver to check the buttons */
460 if (remote_button_hold())
464 /* Check main hold switch status too. */
465 if (on_button
&& button_hold())
470 /* Power on the hard drive early, to speed up the loading. */
472 # ifdef HAVE_EEPROM_SETTINGS
477 ide_power_enable(true);
480 # ifdef HAVE_EEPROM_SETTINGS
481 if (!hold_status
&& (usb_detect() != USB_INSERTED
) && !recovery_mode
)
493 /* Bootloader uses simplified backlight thread, so we need to enable
494 remote display here. */
500 lcd_setfont(FONT_SYSFIXED
);
502 printf("Rockbox boot loader");
503 printf("Version %s", version
);
505 /* No need to wait here more because lcd_init and others already do that. */
506 // sleep(HZ/50); /* Allow the button driver to check the buttons */
507 rec_button
= ((button_status() & BUTTON_REC
) == BUTTON_REC
)
508 || ((button_status() & BUTTON_RC_REC
) == BUTTON_RC_REC
);
512 /* Don't start if the Hold button is active on the device you
514 if ((usb_detect() != USB_INSERTED
) && (hold_status
515 #ifdef HAVE_EEPROM_SETTINGS
520 if (detect_original_firmware())
522 printf("Hold switch on");
526 #ifdef HAVE_EEPROM_SETTINGS
531 /* Holding REC while starting runs the original firmware */
532 if (detect_original_firmware() && rec_button
)
534 printf("Starting original firmware...");
538 /* A hack to enter USB mode without using the USB thread */
539 if(usb_detect() == USB_INSERTED
)
541 const char msg
[] = "Bootloader USB mode";
543 font_getstringsize(msg
, &w
, &h
, FONT_SYSFIXED
);
545 lcd_putsxy((LCD_WIDTH
-w
)/2, (LCD_HEIGHT
-h
)/2, msg
);
548 #ifdef HAVE_REMOTE_LCD
549 lcd_remote_puts(0, 3, msg
);
553 #ifdef HAVE_EEPROM_SETTINGS
554 if (firmware_settings
.initialized
)
556 firmware_settings
.disk_clean
= false;
557 eeprom_settings_store();
560 ide_power_enable(true);
565 while (usb_detect() == USB_INSERTED
)
567 /* Print the battery status. */
572 ata_spin(); /* Prevent the drive from spinning down */
579 cpu_idle_mode(false);
590 printf("ATA error: %d", rc
);
591 printf("Insert USB cable and press");
593 while(!(button_get(true) & BUTTON_REL
));
599 rc
= disk_mount_all();
603 printf("No partition found");
604 while(button_get(true) != SYS_USB_CONNECTED
) {};
607 printf("Loading firmware");
608 i
= load_firmware((unsigned char *)DRAM_START
, BOOTFILE
, MAX_LOADSIZE
);
610 printf("Error: %s", strerror(i
));
615 if (!detect_original_firmware())
617 printf("No firmware found on disk");
627 /* These functions are present in the firmware library, but we reimplement
628 them here because the originals do a lot more than we want */
629 void screen_dump(void)
638 unsigned short *bidi_l2v(const unsigned char *str
, int orientation
)
640 static unsigned short utf16_buf
[SCROLL_LINE_SIZE
];
641 unsigned short *target
;
647 str
= utf8decode(str
, target
++);