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"
45 #include "eeprom_settings.h"
46 #include "rbunicode.h"
51 /* Maximum allowed firmware image size. 10MB is more than enough */
52 #define MAX_LOADSIZE (10*1024*1024)
54 #define DRAM_START 0x31000000
56 #ifdef HAVE_EEPROM_SETTINGS
57 static bool recovery_mode
= false;
60 char version
[] = APPSVERSION
;
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 void failsafe_menu(void)
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
)
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", timeout
);
289 lcd_puts(0, 10, buf
);
291 button
= button_get_w_tmo(HZ
);
293 if (button
== BUTTON_NONE
)
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)
323 if (firmware_settings
.initialized
)
325 firmware_settings
.bootmethod
= option
;
326 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");
362 bool rc_on_button
= false;
363 bool on_button
= false;
364 bool rec_button
= false;
365 bool hold_status
= false;
367 extern int line
; /* From common.c */
368 extern int remote_line
; /* From common.c */
370 /* We want to read the buttons as early as possible, before the user
371 releases the ON button */
373 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
374 (The ON and Hold buttons on the main unit and the remote) */
375 or_l(0x00100062, &GPIO1_FUNCTION
);
376 and_l(~0x00100062, &GPIO1_ENABLE
);
379 if ((data
& 0x20) == 0)
382 if ((data
& 0x40) == 0)
385 /* Set the default state of the hard drive power to OFF */
386 ide_power_enable(false);
390 /* Turn off if neither ON button is pressed */
391 if(!(on_button
|| rc_on_button
|| usb_detect()))
397 /* Start with the main backlight OFF. */
401 /* Remote backlight ON */
402 #ifdef HAVE_REMOTE_LCD
403 __remote_backlight_on();
409 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
410 /* Set up waitstates for the peripherals */
411 set_cpu_frequency(0); /* PLL off */
413 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS
);
418 #ifdef HAVE_EEPROM_SETTINGS
425 if ((on_button
&& button_hold()) ||
426 (rc_on_button
&& remote_button_hold()))
431 /* Power on the hard drive early, to speed up the loading. */
433 # ifdef HAVE_EEPROM_SETTINGS
438 ide_power_enable(true);
441 # ifdef EEPROM_SETTINGS
442 if (!hold_status
&& !usb_detect() && !recovery_mode
)
452 #ifdef HAVE_REMOTE_LCD
457 lcd_setfont(FONT_SYSFIXED
);
459 printf("Rockbox boot loader");
460 printf("Version %s", version
);
462 sleep(HZ
/50); /* Allow the button driver to check the buttons */
463 rec_button
= ((button_status() & BUTTON_REC
) == BUTTON_REC
)
464 || ((button_status() & BUTTON_RC_REC
) == BUTTON_RC_REC
);
468 /* Don't start if the Hold button is active on the device you
470 if (!usb_detect() && (hold_status
471 #ifdef HAVE_EEPROM_SETTINGS
476 if (detect_original_firmware())
478 printf("Hold switch on");
482 #ifdef HAVE_EEPROM_SETTINGS
487 /* Holding REC while starting runs the original firmware */
488 if (detect_original_firmware() && rec_button
)
490 printf("Starting original firmware...");
496 /* A hack to enter USB mode without using the USB thread */
499 const char msg
[] = "Bootloader USB mode";
501 font_getstringsize(msg
, &w
, &h
, FONT_SYSFIXED
);
503 lcd_putsxy((LCD_WIDTH
-w
)/2, (LCD_HEIGHT
-h
)/2, msg
);
506 #ifdef HAVE_REMOTE_LCD
507 lcd_remote_puts(0, 3, msg
);
511 #ifdef HAVE_EEPROM_SETTINGS
512 if (firmware_settings
.initialized
)
514 firmware_settings
.disk_clean
= false;
515 eeprom_settings_store();
518 ide_power_enable(true);
525 /* Print the battery status. */
530 ata_spin(); /* Prevent the drive from spinning down */
537 cpu_idle_mode(false);
548 printf("ATA error: %d", rc
);
549 printf("Insert USB cable and press");
551 while(!(button_get(true) & BUTTON_REL
));
557 rc
= disk_mount_all();
561 printf("No partition found");
562 while(button_get(true) != SYS_USB_CONNECTED
) {};
565 printf("Loading firmware");
566 i
= load_firmware((unsigned char *)DRAM_START
, BOOTFILE
, MAX_LOADSIZE
);
568 printf("Error: %s", strerror(i
));
573 if (!detect_original_firmware())
575 printf("No firmware found on disk");
585 /* These functions are present in the firmware library, but we reimplement
586 them here because the originals do a lot more than we want */
587 void screen_dump(void)
596 unsigned short *bidi_l2v(const unsigned char *str
, int orientation
)
598 static unsigned short utf16_buf
[SCROLL_LINE_SIZE
];
599 unsigned short *target
;
605 str
= utf8decode(str
, target
++);