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"
52 /* Maximum allowed firmware image size. 10MB is more than enough */
53 #define MAX_LOADSIZE (10*1024*1024)
55 #define DRAM_START 0x31000000
57 #ifdef HAVE_EEPROM_SETTINGS
58 static bool recovery_mode
= false;
61 char version
[] = APPSVERSION
;
63 /* Reset the cookie for the crt0 crash check */
64 inline void __reset_cookie(void)
66 asm(" move.l #0,%d0");
67 asm(" move.l %d0,0x10017ffc");
70 void start_iriver_fw(void)
72 asm(" move.w #0x2700,%sr");
74 asm(" movec.l %d0,%vbr");
80 void start_firmware(void)
82 asm(" move.w #0x2700,%sr");
84 asm(" move.l %0,%%d0" :: "i"(DRAM_START
));
85 asm(" movec.l %d0,%vbr");
86 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START
));
87 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START
+4)));
91 #ifdef IRIVER_H100_SERIES
92 void start_flashed_romimage(void)
94 uint8_t *src
= (uint8_t *)FLASH_ROMIMAGE_ENTRY
;
97 if (!detect_flashed_romimage())
100 reset_vector
= (int *)(&src
[sizeof(struct flash_header
)+4]);
102 asm(" move.w #0x2700,%sr");
105 asm(" move.l %0,%%d0" :: "i"(DRAM_START
));
106 asm(" movec.l %d0,%vbr");
107 asm(" move.l %0,%%sp" :: "m"(reset_vector
[0]));
108 asm(" move.l %0,%%a0" :: "m"(reset_vector
[1]));
115 void start_flashed_ramimage(void)
117 struct flash_header hdr
;
118 unsigned char *buf
= (unsigned char *)DRAM_START
;
119 uint8_t *src
= (uint8_t *)FLASH_RAMIMAGE_ENTRY
;
121 if (!detect_flashed_ramimage())
124 /* Load firmware from flash */
126 memcpy(&hdr
, src
, sizeof(struct flash_header
));
127 src
+= sizeof(struct flash_header
);
128 memcpy(buf
, src
, hdr
.length
);
136 #endif /* IRIVER_H100_SERIES */
140 printf("Shutting down...");
141 #ifdef HAVE_EEPROM_SETTINGS
142 /* Reset the rockbox crash check. */
143 firmware_settings
.bl_version
= 0;
144 eeprom_settings_store();
147 /* We need to gracefully spin down the disk to prevent clicks. */
150 /* Make sure ATA has been initialized. */
153 /* And put the disk into sleep immediately. */
161 #ifdef HAVE_REMOTE_LCD
162 _remote_backlight_off();
169 /* Print the battery voltage (and a warning message). */
170 void check_battery(void)
172 int battery_voltage
, batt_int
, batt_frac
;
174 battery_voltage
= battery_adc_voltage();
175 batt_int
= battery_voltage
/ 1000;
176 batt_frac
= (battery_voltage
% 1000) / 10;
178 printf("Batt: %d.%02dV", batt_int
, batt_frac
);
180 if (battery_voltage
<= 310)
182 printf("WARNING! BATTERY LOW!!");
187 #ifdef HAVE_EEPROM_SETTINGS
188 void initialize_eeprom(void)
190 if (detect_original_firmware())
193 if (!eeprom_settings_init())
195 recovery_mode
= true;
199 /* If bootloader version has not been reset, disk might
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
)
216 switch (firmware_settings
.bootmethod
)
222 start_flashed_romimage();
223 recovery_mode
= true;
227 start_flashed_ramimage();
228 recovery_mode
= true;
232 recovery_mode
= true;
237 static const char *options
[] = {
244 #define FAILSAFE_OPTIONS 4
245 #define TIMEOUT (15*HZ)
246 void failsafe_menu(void)
248 long start_tick
= current_tick
;
257 printf("Bootloader %s", version
);
259 printf("=========================");
260 line
+= FAILSAFE_OPTIONS
;
262 printf(" [NAVI] to confirm.");
263 printf(" [REC] to set as default.");
266 if (firmware_settings
.initialized
)
268 defopt
= firmware_settings
.bootmethod
;
269 if (defopt
< 0 || defopt
>= FAILSAFE_OPTIONS
)
273 while (current_tick
- start_tick
< TIMEOUT
)
277 for (i
= 0; i
< FAILSAFE_OPTIONS
; i
++)
287 printf("%s %s %s", arrow
, options
[i
], def
);
290 snprintf(buf
, sizeof(buf
), "Time left: %ds",
291 (TIMEOUT
- (current_tick
- start_tick
)) / HZ
);
292 lcd_puts(0, 10, buf
);
294 button
= button_get_w_tmo(HZ
);
296 if (button
== BUTTON_NONE
|| button
& SYS_EVENT
)
299 start_tick
= current_tick
;
301 /* Ignore the ON/PLAY -button because it can cause trouble
302 with the RTC alarm mod. */
303 switch (button
& ~(BUTTON_ON
))
313 if (option
< FAILSAFE_OPTIONS
-1)
323 if (firmware_settings
.initialized
)
325 firmware_settings
.bootmethod
= option
;
326 eeprom_settings_store();
335 lcd_puts(0, 10, "Executing command...");
346 start_flashed_ramimage();
347 printf("Image not found");
351 start_flashed_romimage();
352 printf("Image not found");
360 /* get rid of a nasty humming sound during boot
362 inline static void __uda1380_reset_hi(void)
365 or_l(1<<29, &GPIO_OUT
);
366 or_l(1<<29, &GPIO_ENABLE
);
367 or_l(1<<29, &GPIO_FUNCTION
);
371 inline static void __uda1380_reset_lo(void)
374 and_l(~(1<<29), &GPIO_OUT
);
382 bool rc_on_button
= false;
383 bool on_button
= false;
384 bool rec_button
= false;
385 bool hold_status
= false;
387 extern int line
; /* From common.c */
388 extern int remote_line
; /* From common.c */
390 /* We want to read the buttons as early as possible, before the user
391 releases the ON button */
393 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
394 (The ON and Hold buttons on the main unit and the remote) */
395 or_l(0x00100062, &GPIO1_FUNCTION
);
396 and_l(~0x00100062, &GPIO1_ENABLE
);
399 if ((data
& 0x20) == 0)
402 if ((data
& 0x40) == 0)
405 /* Set the default state of the hard drive power to OFF */
406 ide_power_enable(false);
410 /* Turn off if neither ON button is pressed */
411 if (!(on_button
|| rc_on_button
))
417 __uda1380_reset_hi();
419 /* Start with the main backlight OFF. */
423 /* Remote backlight ON */
424 #ifdef HAVE_REMOTE_LCD
425 _remote_backlight_on();
431 __uda1380_reset_lo();
433 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
434 /* Set up waitstates for the peripherals */
435 set_cpu_frequency(0); /* PLL off */
437 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS
);
442 #ifdef HAVE_EEPROM_SETTINGS
447 /* A small delay after usb_init is necessary to read the I/O port correctly
448 (if ports are read _immediately_ after the init). */
454 /* Only check remote hold status if remote power button was actually used. */
459 /* Allow the button driver to check the buttons */
462 if (remote_button_hold())
466 /* Check main hold switch status too. */
467 if (on_button
&& button_hold())
472 /* Power on the hard drive early, to speed up the loading. */
474 # ifdef HAVE_EEPROM_SETTINGS
479 ide_power_enable(true);
482 # ifdef HAVE_EEPROM_SETTINGS
483 if (!hold_status
&& (usb_detect() != USB_INSERTED
) && !recovery_mode
)
495 /* Bootloader uses simplified backlight thread, so we need to enable
496 remote display here. */
502 lcd_setfont(FONT_SYSFIXED
);
504 printf("Rockbox boot loader");
505 printf("Version %s", version
);
507 /* No need to wait here more because lcd_init and others already do that. */
508 // sleep(HZ/50); /* Allow the button driver to check the buttons */
509 rec_button
= ((button_status() & BUTTON_REC
) == BUTTON_REC
)
510 || ((button_status() & BUTTON_RC_REC
) == BUTTON_RC_REC
);
514 /* Don't start if the Hold button is active on the device you
516 if ((usb_detect() != USB_INSERTED
) && (hold_status
517 #ifdef HAVE_EEPROM_SETTINGS
522 if (detect_original_firmware())
524 printf("Hold switch on");
528 #ifdef HAVE_EEPROM_SETTINGS
533 /* Holding REC while starting runs the original firmware */
534 if (detect_original_firmware() && rec_button
)
536 printf("Starting original firmware...");
540 /* A hack to enter USB mode without using the USB thread */
541 if(usb_detect() == USB_INSERTED
)
543 const char msg
[] = "Bootloader USB mode";
545 font_getstringsize(msg
, &w
, &h
, FONT_SYSFIXED
);
547 lcd_putsxy((LCD_WIDTH
-w
)/2, (LCD_HEIGHT
-h
)/2, msg
);
550 #ifdef HAVE_REMOTE_LCD
551 lcd_remote_puts(0, 3, msg
);
555 #ifdef HAVE_EEPROM_SETTINGS
556 if (firmware_settings
.initialized
)
558 firmware_settings
.disk_clean
= false;
559 eeprom_settings_store();
562 ide_power_enable(true);
567 while (usb_detect() == USB_INSERTED
)
569 /* Print the battery status. */
574 ata_spin(); /* Prevent the drive from spinning down */
581 cpu_idle_mode(false);
592 printf("ATA error: %d", rc
);
593 printf("Insert USB cable and press");
595 while(!(button_get(true) & BUTTON_REL
));
601 rc
= disk_mount_all();
605 printf("No partition found");
606 while(button_get(true) != SYS_USB_CONNECTED
) {};
609 printf("Loading firmware");
610 i
= load_firmware((unsigned char *)DRAM_START
, BOOTFILE
, MAX_LOADSIZE
);
612 printf("Error: %s", strerror(i
));
617 if (!detect_original_firmware())
619 printf("No firmware found on disk");
629 /* These functions are present in the firmware library, but we reimplement
630 them here because the originals do a lot more than we want */
631 void screen_dump(void)
640 unsigned short *bidi_l2v(const unsigned char *str
, int orientation
)
642 static unsigned short utf16_buf
[SCROLL_LINE_SIZE
];
643 unsigned short *target
;
649 str
= utf8decode(str
, target
++);