blob: 83e545540b1eeb6f9252069e6b8ebc7cf71111ed [file] [log] [blame]
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 by Linus Nielsen Feltzing
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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.
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include "config.h"
22
23#include <stdlib.h>
24#include <stdio.h>
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +000025#include "inttypes.h"
26#include "string.h"
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000027#include "cpu.h"
28#include "system.h"
29#include "lcd.h"
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000030#include "lcd-remote.h"
Michael Sevakis5832b592007-07-28 08:45:57 +000031#include "scroll_engine.h"
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000032#include "kernel.h"
33#include "thread.h"
Frank Gevaerts2f8a0082008-11-01 16:14:28 +000034#include "storage.h"
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +000035#include "usb.h"
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000036#include "disk.h"
37#include "font.h"
38#include "adc.h"
39#include "backlight.h"
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000040#include "backlight-target.h"
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000041#include "button.h"
42#include "panic.h"
43#include "power.h"
Jens Arnold0b7391e2007-08-17 07:06:54 +000044#include "powermgmt.h"
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000045#include "file.h"
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +000046#include "eeprom_settings.h"
Linus Nielsen Feltzing48556152007-03-02 13:58:00 +000047#include "rbunicode.h"
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +000048#include "common.h"
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +000049
50#include <stdarg.h>
51
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +000052/* Maximum allowed firmware image size. 10MB is more than enough */
53#define MAX_LOADSIZE (10*1024*1024)
54
Linus Nielsen Feltzing62c768c2005-07-09 07:46:42 +000055#define DRAM_START 0x31000000
56
Miika Pekkarinen886c3022007-01-13 08:16:02 +000057#ifdef HAVE_EEPROM_SETTINGS
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000058static bool recovery_mode = false;
Miika Pekkarinen886c3022007-01-13 08:16:02 +000059#endif
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000060
Linus Nielsen Feltzinga2e8cf72005-07-15 07:42:25 +000061char version[] = APPSVERSION;
62
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000063/* Reset the cookie for the crt0 crash check */
64inline void __reset_cookie(void)
65{
66 asm(" move.l #0,%d0");
67 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +000068}
69
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000070void start_iriver_fw(void)
71{
72 asm(" move.w #0x2700,%sr");
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000073 __reset_cookie();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000074 asm(" movec.l %d0,%vbr");
75 asm(" move.l 0,%sp");
76 asm(" lea.l 8,%a0");
77 asm(" jmp (%a0)");
78}
79
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000080void start_firmware(void)
81{
82 asm(" move.w #0x2700,%sr");
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000083 __reset_cookie();
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +000084 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000085 asm(" movec.l %d0,%vbr");
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +000086 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
87 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000088 asm(" jmp (%a0)");
89}
90
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000091#ifdef IRIVER_H100_SERIES
92void start_flashed_romimage(void)
93{
94 uint8_t *src = (uint8_t *)FLASH_ROMIMAGE_ENTRY;
95 int *reset_vector;
96
97 if (!detect_flashed_romimage())
98 return ;
99
100 reset_vector = (int *)(&src[sizeof(struct flash_header)+4]);
101
102 asm(" move.w #0x2700,%sr");
103 __reset_cookie();
104
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]));
109 asm(" jmp (%a0)");
110
111 /* Failure */
112 power_off();
113}
114
115void start_flashed_ramimage(void)
116{
117 struct flash_header hdr;
118 unsigned char *buf = (unsigned char *)DRAM_START;
119 uint8_t *src = (uint8_t *)FLASH_RAMIMAGE_ENTRY;
120
121 if (!detect_flashed_ramimage())
122 return;
123
124 /* Load firmware from flash */
125 cpu_boost(true);
126 memcpy(&hdr, src, sizeof(struct flash_header));
127 src += sizeof(struct flash_header);
128 memcpy(buf, src, hdr.length);
129 cpu_boost(false);
130
131 start_firmware();
132
133 /* Failure */
134 power_off();
135}
136#endif /* IRIVER_H100_SERIES */
137
138void shutdown(void)
139{
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();
145#endif
146
147 /* We need to gracefully spin down the disk to prevent clicks. */
148 if (ide_powered())
149 {
150 /* Make sure ATA has been initialized. */
Frank Gevaerts2f8a0082008-11-01 16:14:28 +0000151 storage_init();
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000152
153 /* And put the disk into sleep immediately. */
Frank Gevaerts2f8a0082008-11-01 16:14:28 +0000154 storage_sleepnow();
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000155 }
156
157 sleep(HZ*2);
158
159 /* Backlight OFF */
Jens Arnoldef12b3b2007-11-12 18:49:53 +0000160 _backlight_off();
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000161#ifdef HAVE_REMOTE_LCD
Jens Arnoldef12b3b2007-11-12 18:49:53 +0000162 _remote_backlight_off();
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000163#endif
164
165 __reset_cookie();
166 power_off();
167}
168
169/* Print the battery voltage (and a warning message). */
170void check_battery(void)
171{
Jens Arnold0fac4922007-08-17 06:45:18 +0000172 int battery_voltage, batt_int, batt_frac;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000173
Jens Arnold0fac4922007-08-17 06:45:18 +0000174 battery_voltage = battery_adc_voltage();
175 batt_int = battery_voltage / 1000;
176 batt_frac = (battery_voltage % 1000) / 10;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000177
178 printf("Batt: %d.%02dV", batt_int, batt_frac);
179
180 if (battery_voltage <= 310)
181 {
182 printf("WARNING! BATTERY LOW!!");
183 sleep(HZ*2);
184 }
185}
186
187#ifdef HAVE_EEPROM_SETTINGS
188void initialize_eeprom(void)
189{
190 if (detect_original_firmware())
191 return ;
192
193 if (!eeprom_settings_init())
194 {
195 recovery_mode = true;
196 return ;
197 }
198
199 /* If bootloader version has not been reset, disk might
200 * not be intact. */
201 if (firmware_settings.bl_version || !firmware_settings.disk_clean)
202 {
203 firmware_settings.disk_clean = false;
204 recovery_mode = true;
205 }
206
207 firmware_settings.bl_version = EEPROM_SETTINGS_BL_MINVER;
208 eeprom_settings_store();
209}
210
211void try_flashboot(void)
212{
213 if (!firmware_settings.initialized)
214 return ;
215
216 switch (firmware_settings.bootmethod)
217 {
218 case BOOT_DISK:
219 return;
220
221 case BOOT_ROM:
222 start_flashed_romimage();
223 recovery_mode = true;
224 break;
225
226 case BOOT_RAM:
227 start_flashed_ramimage();
228 recovery_mode = true;
229 break;
230
231 default:
232 recovery_mode = true;
233 return;
234 }
235}
236
237static const char *options[] = {
238 "Boot from disk",
239 "Boot RAM image",
240 "Boot ROM image",
241 "Shutdown"
242};
243
244#define FAILSAFE_OPTIONS 4
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000245#define TIMEOUT (15*HZ)
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000246void failsafe_menu(void)
247{
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000248 long start_tick = current_tick;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000249 int option = 3;
250 int button;
251 int defopt = -1;
252 char buf[32];
253 int i;
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000254 extern int line;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000255
256 reset_screen();
257 printf("Bootloader %s", version);
258 check_battery();
259 printf("=========================");
260 line += FAILSAFE_OPTIONS;
261 printf("");
262 printf(" [NAVI] to confirm.");
263 printf(" [REC] to set as default.");
264 printf("");
265
266 if (firmware_settings.initialized)
267 {
268 defopt = firmware_settings.bootmethod;
269 if (defopt < 0 || defopt >= FAILSAFE_OPTIONS)
270 defopt = option;
271 }
272
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000273 while (current_tick - start_tick < TIMEOUT)
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000274 {
275 /* Draw the menu. */
276 line = 3;
277 for (i = 0; i < FAILSAFE_OPTIONS; i++)
278 {
279 char *def = "[DEF]";
280 char *arrow = "->";
281
282 if (i != defopt)
283 def = "";
284 if (i != option)
285 arrow = " ";
286
287 printf("%s %s %s", arrow, options[i], def);
288 }
289
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000290 snprintf(buf, sizeof(buf), "Time left: %ds",
291 (TIMEOUT - (current_tick - start_tick)) / HZ);
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000292 lcd_puts(0, 10, buf);
293 lcd_update();
294 button = button_get_w_tmo(HZ);
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000295
296 if (button == BUTTON_NONE || button & SYS_EVENT)
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000297 continue ;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000298
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000299 start_tick = current_tick;
300
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000301 /* Ignore the ON/PLAY -button because it can cause trouble
302 with the RTC alarm mod. */
303 switch (button & ~(BUTTON_ON))
304 {
305 case BUTTON_UP:
306 case BUTTON_RC_REW:
307 if (option > 0)
308 option--;
309 break ;
310
311 case BUTTON_DOWN:
312 case BUTTON_RC_FF:
313 if (option < FAILSAFE_OPTIONS-1)
314 option++;
315 break ;
316
317 case BUTTON_SELECT:
318 case BUTTON_RC_ON:
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000319 goto execute;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000320
321 case BUTTON_REC:
322 case BUTTON_RC_REC:
323 if (firmware_settings.initialized)
324 {
325 firmware_settings.bootmethod = option;
326 eeprom_settings_store();
327 defopt = option;
328 }
329 break ;
330 }
331 }
332
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000333 execute:
334
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000335 lcd_puts(0, 10, "Executing command...");
336 lcd_update();
337 sleep(HZ);
338 reset_screen();
339
340 switch (option)
341 {
342 case BOOT_DISK:
343 return ;
344
345 case BOOT_RAM:
346 start_flashed_ramimage();
347 printf("Image not found");
348 break;
349
350 case BOOT_ROM:
351 start_flashed_romimage();
352 printf("Image not found");
353 break;
354 }
355
356 shutdown();
357}
358#endif
359
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000360/* get rid of a nasty humming sound during boot
361 -> RESET signal */
362inline static void __uda1380_reset_hi(void)
363{
364#ifdef HAVE_UDA1380
365 or_l(1<<29, &GPIO_OUT);
366 or_l(1<<29, &GPIO_ENABLE);
367 or_l(1<<29, &GPIO_FUNCTION);
368#endif
369}
370
371inline static void __uda1380_reset_lo(void)
372{
373#ifdef HAVE_UDA1380
374 and_l(~(1<<29), &GPIO_OUT);
375#endif
376}
377
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000378void main(void)
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000379{
380 int i;
381 int rc;
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000382 bool rc_on_button = false;
383 bool on_button = false;
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000384 bool rec_button = false;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000385 bool hold_status = false;
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000386 int data;
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000387 extern int line; /* From common.c */
388 extern int remote_line; /* From common.c */
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000389
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000390 /* We want to read the buttons as early as possible, before the user
391 releases the ON button */
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000392
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000393 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
394 (The ON and Hold buttons on the main unit and the remote) */
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000395 or_l(0x00100062, &GPIO1_FUNCTION);
396 and_l(~0x00100062, &GPIO1_ENABLE);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000397
398 data = GPIO1_READ;
399 if ((data & 0x20) == 0)
400 on_button = true;
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000401
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000402 if ((data & 0x40) == 0)
403 rc_on_button = true;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000404
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000405 /* Set the default state of the hard drive power to OFF */
406 ide_power_enable(false);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000407
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000408 power_init();
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000409
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000410 /* Turn off if neither ON button is pressed */
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000411 if (!(on_button || rc_on_button))
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000412 {
413 __reset_cookie();
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000414 power_off();
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000415 }
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000416
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000417 __uda1380_reset_hi();
418
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000419 /* Start with the main backlight OFF. */
Jens Arnoldef12b3b2007-11-12 18:49:53 +0000420 _backlight_init();
421 _backlight_off();
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000422
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000423 /* Remote backlight ON */
424#ifdef HAVE_REMOTE_LCD
Jens Arnoldef12b3b2007-11-12 18:49:53 +0000425 _remote_backlight_on();
Linus Nielsen Feltzingb68cb9b2005-12-03 00:54:59 +0000426#endif
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000427
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000428 system_init();
429 kernel_init();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000430
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000431 __uda1380_reset_lo();
432
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000433#ifdef HAVE_ADJUSTABLE_CPU_FREQ
434 /* Set up waitstates for the peripherals */
435 set_cpu_frequency(0); /* PLL off */
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000436#ifdef CPU_COLDFIRE
437 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
438#endif
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000439#endif
Michael Sevakisaf395f42008-03-26 01:50:41 +0000440 enable_irq();
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000441
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000442#ifdef HAVE_EEPROM_SETTINGS
443 initialize_eeprom();
444#endif
445
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000446 usb_init();
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). */
449 /* sleep(1); */
450
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000451 adc_init();
452 button_init();
453
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000454 /* Only check remote hold status if remote power button was actually used. */
455 if (rc_on_button)
456 {
457 lcd_remote_init();
458
459 /* Allow the button driver to check the buttons */
460 sleep(HZ/50);
461
462 if (remote_button_hold())
463 hold_status = true;
464 }
465
466 /* Check main hold switch status too. */
467 if (on_button && button_hold())
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000468 {
469 hold_status = true;
470 }
471
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000472 /* Power on the hard drive early, to speed up the loading. */
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000473 if (!hold_status
474# ifdef HAVE_EEPROM_SETTINGS
475 && !recovery_mode
476# endif
477 )
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000478 {
479 ide_power_enable(true);
480 }
481
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000482# ifdef HAVE_EEPROM_SETTINGS
Dave Chapman16723502007-09-04 08:03:07 +0000483 if (!hold_status && (usb_detect() != USB_INSERTED) && !recovery_mode)
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000484 try_flashboot();
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000485# endif
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000486
487 backlight_init();
Christian Gmeiner21ac3112007-11-20 12:09:59 +0000488
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000489
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000490 lcd_init();
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000491
492 if (!rc_on_button)
493 lcd_remote_init();
494
495 /* Bootloader uses simplified backlight thread, so we need to enable
496 remote display here. */
497 if (remote_detect())
498 lcd_remote_on();
499
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000500 font_init();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000501
502 lcd_setfont(FONT_SYSFIXED);
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000503
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000504 printf("Rockbox boot loader");
505 printf("Version %s", version);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000506
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000507 /* 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 */
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000509 rec_button = ((button_status() & BUTTON_REC) == BUTTON_REC)
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000510 || ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000511
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000512 check_battery();
513
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000514 /* Don't start if the Hold button is active on the device you
515 are starting with */
Dave Chapman16723502007-09-04 08:03:07 +0000516 if ((usb_detect() != USB_INSERTED) && (hold_status
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000517#ifdef HAVE_EEPROM_SETTINGS
518 || recovery_mode
519#endif
520 ))
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000521 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000522 if (detect_original_firmware())
523 {
524 printf("Hold switch on");
525 shutdown();
526 }
527
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000528#ifdef HAVE_EEPROM_SETTINGS
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000529 failsafe_menu();
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000530#endif
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000531 }
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000532
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000533 /* Holding REC while starting runs the original firmware */
534 if (detect_original_firmware() && rec_button)
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000535 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000536 printf("Starting original firmware...");
537 start_iriver_fw();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000538 }
539
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000540 /* A hack to enter USB mode without using the USB thread */
Dave Chapman16723502007-09-04 08:03:07 +0000541 if(usb_detect() == USB_INSERTED)
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000542 {
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000543 const char msg[] = "Bootloader USB mode";
544 int w, h;
545 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000546 reset_screen();
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000547 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000548 lcd_update();
549
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000550#ifdef HAVE_REMOTE_LCD
551 lcd_remote_puts(0, 3, msg);
552 lcd_remote_update();
553#endif
554
Peter D'Hoyec4a59a22006-08-15 22:54:06 +0000555#ifdef HAVE_EEPROM_SETTINGS
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000556 if (firmware_settings.initialized)
557 {
558 firmware_settings.disk_clean = false;
559 eeprom_settings_store();
560 }
561#endif
Linus Nielsen Feltzing237ff9e2007-02-28 00:06:35 +0000562 ide_power_enable(true);
Frank Gevaerts2f8a0082008-11-01 16:14:28 +0000563 storage_enable(false);
Linus Nielsen Feltzing237ff9e2007-02-28 00:06:35 +0000564 sleep(HZ/20);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000565 usb_enable(true);
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000566 cpu_idle_mode(true);
Dave Chapman16723502007-09-04 08:03:07 +0000567 while (usb_detect() == USB_INSERTED)
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000568 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000569 /* Print the battery status. */
570 line = 0;
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000571 remote_line = 0;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000572 check_battery();
573
Frank Gevaerts2f8a0082008-11-01 16:14:28 +0000574 storage_spin(); /* Prevent the drive from spinning down */
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000575 sleep(HZ);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000576
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000577 /* Backlight OFF */
Jens Arnoldef12b3b2007-11-12 18:49:53 +0000578 _backlight_off();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000579 }
580
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000581 cpu_idle_mode(false);
Linus Nielsen Feltzing13103902005-07-19 11:03:00 +0000582 usb_enable(false);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000583
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000584 reset_screen();
Linus Nielsen Feltzing13103902005-07-19 11:03:00 +0000585 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000586 }
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000587
Frank Gevaerts2f8a0082008-11-01 16:14:28 +0000588 rc = storage_init();
Linus Nielsen Feltzing237ff9e2007-02-28 00:06:35 +0000589 if(rc)
590 {
591 reset_screen();
592 printf("ATA error: %d", rc);
593 printf("Insert USB cable and press");
594 printf("a button");
595 while(!(button_get(true) & BUTTON_REL));
596 }
597
598
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000599 disk_init();
600
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000601 rc = disk_mount_all();
602 if (rc<=0)
603 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000604 reset_screen();
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000605 printf("No partition found");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000606 while(button_get(true) != SYS_USB_CONNECTED) {};
607 }
608
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000609 printf("Loading firmware");
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000610 i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE);
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000611 if(i < 0)
Steve Bavin0e16bd32007-06-18 06:52:29 +0000612 printf("Error: %s", strerror(i));
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000613
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000614 if (i == EOK)
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000615 start_firmware();
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000616
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000617 if (!detect_original_firmware())
Miika Pekkarinen973ee5d2006-09-04 16:06:11 +0000618 {
619 printf("No firmware found on disk");
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000620 sleep(HZ*2);
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000621 shutdown();
Miika Pekkarinen973ee5d2006-09-04 16:06:11 +0000622 }
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000623 else {
624 sleep(HZ*2);
Miika Pekkarinen973ee5d2006-09-04 16:06:11 +0000625 start_iriver_fw();
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000626 }
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000627}
628
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 */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000631void screen_dump(void)
632{
633}
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000634
635int usb_screen(void)
636{
637 return 0;
638}
639
640unsigned short *bidi_l2v(const unsigned char *str, int orientation)
641{
642 static unsigned short utf16_buf[SCROLL_LINE_SIZE];
643 unsigned short *target;
644 (void)orientation;
645
646 target = utf16_buf;
647
648 while (*str)
649 str = utf8decode(str, target++);
650 *target = 0;
651 return utf16_buf;
652}