blob: 4be92eb4ac8fb65a7441956dc61495ba02392214 [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 *
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.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19#include "config.h"
20
21#include <stdlib.h>
22#include <stdio.h>
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +000023#include "inttypes.h"
24#include "string.h"
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000025#include "cpu.h"
26#include "system.h"
27#include "lcd.h"
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000028#include "lcd-remote.h"
Michael Sevakis5832b592007-07-28 08:45:57 +000029#include "scroll_engine.h"
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000030#include "kernel.h"
31#include "thread.h"
32#include "ata.h"
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +000033#include "usb.h"
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000034#include "disk.h"
35#include "font.h"
36#include "adc.h"
37#include "backlight.h"
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000038#include "backlight-target.h"
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000039#include "button.h"
40#include "panic.h"
41#include "power.h"
Jens Arnold0b7391e2007-08-17 07:06:54 +000042#include "powermgmt.h"
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000043#include "file.h"
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +000044#include "eeprom_settings.h"
Linus Nielsen Feltzing48556152007-03-02 13:58:00 +000045#include "rbunicode.h"
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +000046#include "common.h"
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +000047
48#include <stdarg.h>
49
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +000050/* Maximum allowed firmware image size. 10MB is more than enough */
51#define MAX_LOADSIZE (10*1024*1024)
52
Linus Nielsen Feltzing62c768c2005-07-09 07:46:42 +000053#define DRAM_START 0x31000000
54
Miika Pekkarinen886c3022007-01-13 08:16:02 +000055#ifdef HAVE_EEPROM_SETTINGS
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000056static bool recovery_mode = false;
Miika Pekkarinen886c3022007-01-13 08:16:02 +000057#endif
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000058
Linus Nielsen Feltzinga2e8cf72005-07-15 07:42:25 +000059char version[] = APPSVERSION;
60
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000061/* Reset the cookie for the crt0 crash check */
62inline void __reset_cookie(void)
63{
64 asm(" move.l #0,%d0");
65 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +000066}
67
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000068void start_iriver_fw(void)
69{
70 asm(" move.w #0x2700,%sr");
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000071 __reset_cookie();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000072 asm(" movec.l %d0,%vbr");
73 asm(" move.l 0,%sp");
74 asm(" lea.l 8,%a0");
75 asm(" jmp (%a0)");
76}
77
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000078void start_firmware(void)
79{
80 asm(" move.w #0x2700,%sr");
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000081 __reset_cookie();
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +000082 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000083 asm(" movec.l %d0,%vbr");
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +000084 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
85 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000086 asm(" jmp (%a0)");
87}
88
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000089#ifdef IRIVER_H100_SERIES
90void start_flashed_romimage(void)
91{
92 uint8_t *src = (uint8_t *)FLASH_ROMIMAGE_ENTRY;
93 int *reset_vector;
94
95 if (!detect_flashed_romimage())
96 return ;
97
98 reset_vector = (int *)(&src[sizeof(struct flash_header)+4]);
99
100 asm(" move.w #0x2700,%sr");
101 __reset_cookie();
102
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]));
107 asm(" jmp (%a0)");
108
109 /* Failure */
110 power_off();
111}
112
113void start_flashed_ramimage(void)
114{
115 struct flash_header hdr;
116 unsigned char *buf = (unsigned char *)DRAM_START;
117 uint8_t *src = (uint8_t *)FLASH_RAMIMAGE_ENTRY;
118
119 if (!detect_flashed_ramimage())
120 return;
121
122 /* Load firmware from flash */
123 cpu_boost(true);
124 memcpy(&hdr, src, sizeof(struct flash_header));
125 src += sizeof(struct flash_header);
126 memcpy(buf, src, hdr.length);
127 cpu_boost(false);
128
129 start_firmware();
130
131 /* Failure */
132 power_off();
133}
134#endif /* IRIVER_H100_SERIES */
135
136void shutdown(void)
137{
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();
143#endif
144
145 /* We need to gracefully spin down the disk to prevent clicks. */
146 if (ide_powered())
147 {
148 /* Make sure ATA has been initialized. */
149 ata_init();
150
151 /* And put the disk into sleep immediately. */
152 ata_sleepnow();
153 }
154
155 sleep(HZ*2);
156
157 /* Backlight OFF */
Jens Arnoldef12b3b2007-11-12 18:49:53 +0000158 _backlight_off();
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000159#ifdef HAVE_REMOTE_LCD
Jens Arnoldef12b3b2007-11-12 18:49:53 +0000160 _remote_backlight_off();
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000161#endif
162
163 __reset_cookie();
164 power_off();
165}
166
167/* Print the battery voltage (and a warning message). */
168void check_battery(void)
169{
Jens Arnold0fac4922007-08-17 06:45:18 +0000170 int battery_voltage, batt_int, batt_frac;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000171
Jens Arnold0fac4922007-08-17 06:45:18 +0000172 battery_voltage = battery_adc_voltage();
173 batt_int = battery_voltage / 1000;
174 batt_frac = (battery_voltage % 1000) / 10;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000175
176 printf("Batt: %d.%02dV", batt_int, batt_frac);
177
178 if (battery_voltage <= 310)
179 {
180 printf("WARNING! BATTERY LOW!!");
181 sleep(HZ*2);
182 }
183}
184
185#ifdef HAVE_EEPROM_SETTINGS
186void initialize_eeprom(void)
187{
188 if (detect_original_firmware())
189 return ;
190
191 if (!eeprom_settings_init())
192 {
193 recovery_mode = true;
194 return ;
195 }
196
197 /* If bootloader version has not been reset, disk might
198 * not be intact. */
199 if (firmware_settings.bl_version || !firmware_settings.disk_clean)
200 {
201 firmware_settings.disk_clean = false;
202 recovery_mode = true;
203 }
204
205 firmware_settings.bl_version = EEPROM_SETTINGS_BL_MINVER;
206 eeprom_settings_store();
207}
208
209void try_flashboot(void)
210{
211 if (!firmware_settings.initialized)
212 return ;
213
214 switch (firmware_settings.bootmethod)
215 {
216 case BOOT_DISK:
217 return;
218
219 case BOOT_ROM:
220 start_flashed_romimage();
221 recovery_mode = true;
222 break;
223
224 case BOOT_RAM:
225 start_flashed_ramimage();
226 recovery_mode = true;
227 break;
228
229 default:
230 recovery_mode = true;
231 return;
232 }
233}
234
235static const char *options[] = {
236 "Boot from disk",
237 "Boot RAM image",
238 "Boot ROM image",
239 "Shutdown"
240};
241
242#define FAILSAFE_OPTIONS 4
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000243#define TIMEOUT (15*HZ)
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000244void failsafe_menu(void)
245{
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000246 long start_tick = current_tick;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000247 int option = 3;
248 int button;
249 int defopt = -1;
250 char buf[32];
251 int i;
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000252 extern int line;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000253
254 reset_screen();
255 printf("Bootloader %s", version);
256 check_battery();
257 printf("=========================");
258 line += FAILSAFE_OPTIONS;
259 printf("");
260 printf(" [NAVI] to confirm.");
261 printf(" [REC] to set as default.");
262 printf("");
263
264 if (firmware_settings.initialized)
265 {
266 defopt = firmware_settings.bootmethod;
267 if (defopt < 0 || defopt >= FAILSAFE_OPTIONS)
268 defopt = option;
269 }
270
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000271 while (current_tick - start_tick < TIMEOUT)
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000272 {
273 /* Draw the menu. */
274 line = 3;
275 for (i = 0; i < FAILSAFE_OPTIONS; i++)
276 {
277 char *def = "[DEF]";
278 char *arrow = "->";
279
280 if (i != defopt)
281 def = "";
282 if (i != option)
283 arrow = " ";
284
285 printf("%s %s %s", arrow, options[i], def);
286 }
287
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000288 snprintf(buf, sizeof(buf), "Time left: %ds",
289 (TIMEOUT - (current_tick - start_tick)) / HZ);
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000290 lcd_puts(0, 10, buf);
291 lcd_update();
292 button = button_get_w_tmo(HZ);
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000293
294 if (button == BUTTON_NONE || button & SYS_EVENT)
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000295 continue ;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000296
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000297 start_tick = current_tick;
298
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000299 /* Ignore the ON/PLAY -button because it can cause trouble
300 with the RTC alarm mod. */
301 switch (button & ~(BUTTON_ON))
302 {
303 case BUTTON_UP:
304 case BUTTON_RC_REW:
305 if (option > 0)
306 option--;
307 break ;
308
309 case BUTTON_DOWN:
310 case BUTTON_RC_FF:
311 if (option < FAILSAFE_OPTIONS-1)
312 option++;
313 break ;
314
315 case BUTTON_SELECT:
316 case BUTTON_RC_ON:
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000317 goto execute;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000318
319 case BUTTON_REC:
320 case BUTTON_RC_REC:
321 if (firmware_settings.initialized)
322 {
323 firmware_settings.bootmethod = option;
324 eeprom_settings_store();
325 defopt = option;
326 }
327 break ;
328 }
329 }
330
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000331 execute:
332
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000333 lcd_puts(0, 10, "Executing command...");
334 lcd_update();
335 sleep(HZ);
336 reset_screen();
337
338 switch (option)
339 {
340 case BOOT_DISK:
341 return ;
342
343 case BOOT_RAM:
344 start_flashed_ramimage();
345 printf("Image not found");
346 break;
347
348 case BOOT_ROM:
349 start_flashed_romimage();
350 printf("Image not found");
351 break;
352 }
353
354 shutdown();
355}
356#endif
357
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000358/* get rid of a nasty humming sound during boot
359 -> RESET signal */
360inline static void __uda1380_reset_hi(void)
361{
362#ifdef HAVE_UDA1380
363 or_l(1<<29, &GPIO_OUT);
364 or_l(1<<29, &GPIO_ENABLE);
365 or_l(1<<29, &GPIO_FUNCTION);
366#endif
367}
368
369inline static void __uda1380_reset_lo(void)
370{
371#ifdef HAVE_UDA1380
372 and_l(~(1<<29), &GPIO_OUT);
373#endif
374}
375
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000376void main(void)
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000377{
378 int i;
379 int rc;
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000380 bool rc_on_button = false;
381 bool on_button = false;
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000382 bool rec_button = false;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000383 bool hold_status = false;
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000384 int data;
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000385 extern int line; /* From common.c */
386 extern int remote_line; /* From common.c */
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000387
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000388 /* We want to read the buttons as early as possible, before the user
389 releases the ON button */
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000390
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000391 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
392 (The ON and Hold buttons on the main unit and the remote) */
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000393 or_l(0x00100062, &GPIO1_FUNCTION);
394 and_l(~0x00100062, &GPIO1_ENABLE);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000395
396 data = GPIO1_READ;
397 if ((data & 0x20) == 0)
398 on_button = true;
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000399
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000400 if ((data & 0x40) == 0)
401 rc_on_button = true;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000402
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000403 /* Set the default state of the hard drive power to OFF */
404 ide_power_enable(false);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000405
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000406 power_init();
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000407
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000408 /* Turn off if neither ON button is pressed */
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000409 if (!(on_button || rc_on_button))
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000410 {
411 __reset_cookie();
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000412 power_off();
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000413 }
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000414
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000415 __uda1380_reset_hi();
416
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000417 /* Start with the main backlight OFF. */
Jens Arnoldef12b3b2007-11-12 18:49:53 +0000418 _backlight_init();
419 _backlight_off();
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000420
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000421 /* Remote backlight ON */
422#ifdef HAVE_REMOTE_LCD
Jens Arnoldef12b3b2007-11-12 18:49:53 +0000423 _remote_backlight_on();
Linus Nielsen Feltzingb68cb9b2005-12-03 00:54:59 +0000424#endif
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000425
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000426 system_init();
427 kernel_init();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000428
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000429 __uda1380_reset_lo();
430
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000431#ifdef HAVE_ADJUSTABLE_CPU_FREQ
432 /* Set up waitstates for the peripherals */
433 set_cpu_frequency(0); /* PLL off */
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000434#ifdef CPU_COLDFIRE
435 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
436#endif
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000437#endif
Michael Sevakisaf395f42008-03-26 01:50:41 +0000438 enable_irq();
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000439
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000440#ifdef HAVE_EEPROM_SETTINGS
441 initialize_eeprom();
442#endif
443
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000444 usb_init();
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). */
447 /* sleep(1); */
448
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000449 adc_init();
450 button_init();
451
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000452 /* Only check remote hold status if remote power button was actually used. */
453 if (rc_on_button)
454 {
455 lcd_remote_init();
456
457 /* Allow the button driver to check the buttons */
458 sleep(HZ/50);
459
460 if (remote_button_hold())
461 hold_status = true;
462 }
463
464 /* Check main hold switch status too. */
465 if (on_button && button_hold())
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000466 {
467 hold_status = true;
468 }
469
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000470 /* Power on the hard drive early, to speed up the loading. */
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000471 if (!hold_status
472# ifdef HAVE_EEPROM_SETTINGS
473 && !recovery_mode
474# endif
475 )
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000476 {
477 ide_power_enable(true);
478 }
479
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000480# ifdef HAVE_EEPROM_SETTINGS
Dave Chapman16723502007-09-04 08:03:07 +0000481 if (!hold_status && (usb_detect() != USB_INSERTED) && !recovery_mode)
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000482 try_flashboot();
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000483# endif
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000484
485 backlight_init();
Christian Gmeiner21ac3112007-11-20 12:09:59 +0000486
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000487
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000488 lcd_init();
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000489
490 if (!rc_on_button)
491 lcd_remote_init();
492
493 /* Bootloader uses simplified backlight thread, so we need to enable
494 remote display here. */
495 if (remote_detect())
496 lcd_remote_on();
497
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000498 font_init();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000499
500 lcd_setfont(FONT_SYSFIXED);
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000501
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000502 printf("Rockbox boot loader");
503 printf("Version %s", version);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000504
Miika Pekkarinen698113d2008-02-09 15:06:09 +0000505 /* 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 */
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000507 rec_button = ((button_status() & BUTTON_REC) == BUTTON_REC)
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000508 || ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000509
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000510 check_battery();
511
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000512 /* Don't start if the Hold button is active on the device you
513 are starting with */
Dave Chapman16723502007-09-04 08:03:07 +0000514 if ((usb_detect() != USB_INSERTED) && (hold_status
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000515#ifdef HAVE_EEPROM_SETTINGS
516 || recovery_mode
517#endif
518 ))
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000519 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000520 if (detect_original_firmware())
521 {
522 printf("Hold switch on");
523 shutdown();
524 }
525
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000526#ifdef HAVE_EEPROM_SETTINGS
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000527 failsafe_menu();
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000528#endif
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000529 }
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000530
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000531 /* Holding REC while starting runs the original firmware */
532 if (detect_original_firmware() && rec_button)
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000533 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000534 printf("Starting original firmware...");
535 start_iriver_fw();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000536 }
537
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000538 /* A hack to enter USB mode without using the USB thread */
Dave Chapman16723502007-09-04 08:03:07 +0000539 if(usb_detect() == USB_INSERTED)
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000540 {
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000541 const char msg[] = "Bootloader USB mode";
542 int w, h;
543 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000544 reset_screen();
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000545 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000546 lcd_update();
547
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000548#ifdef HAVE_REMOTE_LCD
549 lcd_remote_puts(0, 3, msg);
550 lcd_remote_update();
551#endif
552
Peter D'Hoyec4a59a22006-08-15 22:54:06 +0000553#ifdef HAVE_EEPROM_SETTINGS
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000554 if (firmware_settings.initialized)
555 {
556 firmware_settings.disk_clean = false;
557 eeprom_settings_store();
558 }
559#endif
Linus Nielsen Feltzing237ff9e2007-02-28 00:06:35 +0000560 ide_power_enable(true);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000561 ata_enable(false);
Linus Nielsen Feltzing237ff9e2007-02-28 00:06:35 +0000562 sleep(HZ/20);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000563 usb_enable(true);
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000564 cpu_idle_mode(true);
Dave Chapman16723502007-09-04 08:03:07 +0000565 while (usb_detect() == USB_INSERTED)
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000566 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000567 /* Print the battery status. */
568 line = 0;
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000569 remote_line = 0;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000570 check_battery();
571
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000572 ata_spin(); /* Prevent the drive from spinning down */
573 sleep(HZ);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000574
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000575 /* Backlight OFF */
Jens Arnoldef12b3b2007-11-12 18:49:53 +0000576 _backlight_off();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000577 }
578
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000579 cpu_idle_mode(false);
Linus Nielsen Feltzing13103902005-07-19 11:03:00 +0000580 usb_enable(false);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000581
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000582 reset_screen();
Linus Nielsen Feltzing13103902005-07-19 11:03:00 +0000583 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000584 }
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000585
Linus Nielsen Feltzing237ff9e2007-02-28 00:06:35 +0000586 rc = ata_init();
587 if(rc)
588 {
589 reset_screen();
590 printf("ATA error: %d", rc);
591 printf("Insert USB cable and press");
592 printf("a button");
593 while(!(button_get(true) & BUTTON_REL));
594 }
595
596
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000597 disk_init();
598
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000599 rc = disk_mount_all();
600 if (rc<=0)
601 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000602 reset_screen();
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000603 printf("No partition found");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000604 while(button_get(true) != SYS_USB_CONNECTED) {};
605 }
606
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000607 printf("Loading firmware");
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000608 i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE);
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000609 if(i < 0)
Steve Bavin0e16bd32007-06-18 06:52:29 +0000610 printf("Error: %s", strerror(i));
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000611
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000612 if (i == EOK)
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000613 start_firmware();
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000614
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000615 if (!detect_original_firmware())
Miika Pekkarinen973ee5d2006-09-04 16:06:11 +0000616 {
617 printf("No firmware found on disk");
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000618 sleep(HZ*2);
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000619 shutdown();
Miika Pekkarinen973ee5d2006-09-04 16:06:11 +0000620 }
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000621 else {
622 sleep(HZ*2);
Miika Pekkarinen973ee5d2006-09-04 16:06:11 +0000623 start_iriver_fw();
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000624 }
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000625}
626
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 */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000629void screen_dump(void)
630{
631}
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000632
633int usb_screen(void)
634{
635 return 0;
636}
637
638unsigned short *bidi_l2v(const unsigned char *str, int orientation)
639{
640 static unsigned short utf16_buf[SCROLL_LINE_SIZE];
641 unsigned short *target;
642 (void)orientation;
643
644 target = utf16_buf;
645
646 while (*str)
647 str = utf8decode(str, target++);
648 *target = 0;
649 return utf16_buf;
650}