blob: 1b6ccc7aebb1e80ab7d066c69aadef7a82713ffb [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"
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000029#include "kernel.h"
30#include "thread.h"
31#include "ata.h"
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +000032#include "usb.h"
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000033#include "disk.h"
34#include "font.h"
35#include "adc.h"
36#include "backlight.h"
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000037#include "backlight-target.h"
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000038#include "button.h"
39#include "panic.h"
40#include "power.h"
41#include "file.h"
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +000042#include "uda1380.h"
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +000043#include "eeprom_settings.h"
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000044
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +000045#include "pcf50606.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 Feltzingd3971452005-01-28 12:51:10 +000059int usb_screen(void)
60{
61 return 0;
62}
63
Linus Nielsen Feltzinga2e8cf72005-07-15 07:42:25 +000064char version[] = APPSVERSION;
65
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000066/* Reset the cookie for the crt0 crash check */
67inline void __reset_cookie(void)
68{
69 asm(" move.l #0,%d0");
70 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +000071}
72
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000073void start_iriver_fw(void)
74{
75 asm(" move.w #0x2700,%sr");
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000076 __reset_cookie();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000077 asm(" movec.l %d0,%vbr");
78 asm(" move.l 0,%sp");
79 asm(" lea.l 8,%a0");
80 asm(" jmp (%a0)");
81}
82
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000083void start_firmware(void)
84{
85 asm(" move.w #0x2700,%sr");
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000086 __reset_cookie();
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +000087 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000088 asm(" movec.l %d0,%vbr");
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +000089 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
90 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000091 asm(" jmp (%a0)");
92}
93
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000094#ifdef IRIVER_H100_SERIES
95void start_flashed_romimage(void)
96{
97 uint8_t *src = (uint8_t *)FLASH_ROMIMAGE_ENTRY;
98 int *reset_vector;
99
100 if (!detect_flashed_romimage())
101 return ;
102
103 reset_vector = (int *)(&src[sizeof(struct flash_header)+4]);
104
105 asm(" move.w #0x2700,%sr");
106 __reset_cookie();
107
108 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
109 asm(" movec.l %d0,%vbr");
110 asm(" move.l %0,%%sp" :: "m"(reset_vector[0]));
111 asm(" move.l %0,%%a0" :: "m"(reset_vector[1]));
112 asm(" jmp (%a0)");
113
114 /* Failure */
115 power_off();
116}
117
118void start_flashed_ramimage(void)
119{
120 struct flash_header hdr;
121 unsigned char *buf = (unsigned char *)DRAM_START;
122 uint8_t *src = (uint8_t *)FLASH_RAMIMAGE_ENTRY;
123
124 if (!detect_flashed_ramimage())
125 return;
126
127 /* Load firmware from flash */
128 cpu_boost(true);
129 memcpy(&hdr, src, sizeof(struct flash_header));
130 src += sizeof(struct flash_header);
131 memcpy(buf, src, hdr.length);
132 cpu_boost(false);
133
134 start_firmware();
135
136 /* Failure */
137 power_off();
138}
139#endif /* IRIVER_H100_SERIES */
140
141void shutdown(void)
142{
143 printf("Shutting down...");
144#ifdef HAVE_EEPROM_SETTINGS
145 /* Reset the rockbox crash check. */
146 firmware_settings.bl_version = 0;
147 eeprom_settings_store();
148#endif
149
150 /* We need to gracefully spin down the disk to prevent clicks. */
151 if (ide_powered())
152 {
153 /* Make sure ATA has been initialized. */
154 ata_init();
155
156 /* And put the disk into sleep immediately. */
157 ata_sleepnow();
158 }
159
160 sleep(HZ*2);
161
162 /* Backlight OFF */
163 __backlight_off();
164#ifdef HAVE_REMOTE_LCD
165 __remote_backlight_off();
166#endif
167
168 __reset_cookie();
169 power_off();
170}
171
172/* Print the battery voltage (and a warning message). */
173void check_battery(void)
174{
175 int adc_battery, battery_voltage, batt_int, batt_frac;
176
177 adc_battery = adc_read(ADC_BATTERY);
178
179 battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000;
180 batt_int = battery_voltage / 100;
181 batt_frac = battery_voltage % 100;
182
183 printf("Batt: %d.%02dV", batt_int, batt_frac);
184
185 if (battery_voltage <= 310)
186 {
187 printf("WARNING! BATTERY LOW!!");
188 sleep(HZ*2);
189 }
190}
191
192#ifdef HAVE_EEPROM_SETTINGS
193void initialize_eeprom(void)
194{
195 if (detect_original_firmware())
196 return ;
197
198 if (!eeprom_settings_init())
199 {
200 recovery_mode = true;
201 return ;
202 }
203
204 /* If bootloader version has not been reset, disk might
205 * not be intact. */
206 if (firmware_settings.bl_version || !firmware_settings.disk_clean)
207 {
208 firmware_settings.disk_clean = false;
209 recovery_mode = true;
210 }
211
212 firmware_settings.bl_version = EEPROM_SETTINGS_BL_MINVER;
213 eeprom_settings_store();
214}
215
216void try_flashboot(void)
217{
218 if (!firmware_settings.initialized)
219 return ;
220
221 switch (firmware_settings.bootmethod)
222 {
223 case BOOT_DISK:
224 return;
225
226 case BOOT_ROM:
227 start_flashed_romimage();
228 recovery_mode = true;
229 break;
230
231 case BOOT_RAM:
232 start_flashed_ramimage();
233 recovery_mode = true;
234 break;
235
236 default:
237 recovery_mode = true;
238 return;
239 }
240}
241
242static const char *options[] = {
243 "Boot from disk",
244 "Boot RAM image",
245 "Boot ROM image",
246 "Shutdown"
247};
248
249#define FAILSAFE_OPTIONS 4
250void failsafe_menu(void)
251{
252 int timeout = 15;
253 int option = 3;
254 int button;
255 int defopt = -1;
256 char buf[32];
257 int i;
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000258 extern int line;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000259
260 reset_screen();
261 printf("Bootloader %s", version);
262 check_battery();
263 printf("=========================");
264 line += FAILSAFE_OPTIONS;
265 printf("");
266 printf(" [NAVI] to confirm.");
267 printf(" [REC] to set as default.");
268 printf("");
269
270 if (firmware_settings.initialized)
271 {
272 defopt = firmware_settings.bootmethod;
273 if (defopt < 0 || defopt >= FAILSAFE_OPTIONS)
274 defopt = option;
275 }
276
277 while (timeout > 0)
278 {
279 /* Draw the menu. */
280 line = 3;
281 for (i = 0; i < FAILSAFE_OPTIONS; i++)
282 {
283 char *def = "[DEF]";
284 char *arrow = "->";
285
286 if (i != defopt)
287 def = "";
288 if (i != option)
289 arrow = " ";
290
291 printf("%s %s %s", arrow, options[i], def);
292 }
293
294 snprintf(buf, sizeof(buf), "Time left: %ds", timeout);
295 lcd_puts(0, 10, buf);
296 lcd_update();
297 button = button_get_w_tmo(HZ);
298
299 if (button == BUTTON_NONE)
300 {
301 timeout--;
302 continue ;
303 }
304
305 timeout = 15;
306 /* Ignore the ON/PLAY -button because it can cause trouble
307 with the RTC alarm mod. */
308 switch (button & ~(BUTTON_ON))
309 {
310 case BUTTON_UP:
311 case BUTTON_RC_REW:
312 if (option > 0)
313 option--;
314 break ;
315
316 case BUTTON_DOWN:
317 case BUTTON_RC_FF:
318 if (option < FAILSAFE_OPTIONS-1)
319 option++;
320 break ;
321
322 case BUTTON_SELECT:
323 case BUTTON_RC_ON:
324 timeout = 0;
325 break ;
326
327 case BUTTON_REC:
328 case BUTTON_RC_REC:
329 if (firmware_settings.initialized)
330 {
331 firmware_settings.bootmethod = option;
332 eeprom_settings_store();
333 defopt = option;
334 }
335 break ;
336 }
337 }
338
339 lcd_puts(0, 10, "Executing command...");
340 lcd_update();
341 sleep(HZ);
342 reset_screen();
343
344 switch (option)
345 {
346 case BOOT_DISK:
347 return ;
348
349 case BOOT_RAM:
350 start_flashed_ramimage();
351 printf("Image not found");
352 break;
353
354 case BOOT_ROM:
355 start_flashed_romimage();
356 printf("Image not found");
357 break;
358 }
359
360 shutdown();
361}
362#endif
363
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000364void main(void)
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000365{
366 int i;
367 int rc;
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000368 bool rc_on_button = false;
369 bool on_button = false;
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000370 bool rec_button = false;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000371 bool hold_status = false;
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000372 int data;
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000373 extern int line; /* From common.c */
374 extern int remote_line; /* From common.c */
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000375
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000376 /* We want to read the buttons as early as possible, before the user
377 releases the ON button */
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000378
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000379 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
380 (The ON and Hold buttons on the main unit and the remote) */
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000381 or_l(0x00100062, &GPIO1_FUNCTION);
382 and_l(~0x00100062, &GPIO1_ENABLE);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000383
384 data = GPIO1_READ;
385 if ((data & 0x20) == 0)
386 on_button = true;
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000387
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000388 if ((data & 0x40) == 0)
389 rc_on_button = true;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000390
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000391 /* Set the default state of the hard drive power to OFF */
392 ide_power_enable(false);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000393
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000394 power_init();
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000395
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000396 /* Turn off if neither ON button is pressed */
Linus Nielsen Feltzing54da9242006-01-10 23:11:57 +0000397 if(!(on_button || rc_on_button || usb_detect()))
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000398 {
399 __reset_cookie();
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000400 power_off();
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000401 }
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000402
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000403 /* Start with the main backlight OFF. */
404 __backlight_init();
405 __backlight_off();
406
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000407 /* Remote backlight ON */
408#ifdef HAVE_REMOTE_LCD
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000409 __remote_backlight_on();
Linus Nielsen Feltzingb68cb9b2005-12-03 00:54:59 +0000410#endif
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000411
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000412 system_init();
413 kernel_init();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000414
415#ifdef HAVE_ADJUSTABLE_CPU_FREQ
416 /* Set up waitstates for the peripherals */
417 set_cpu_frequency(0); /* PLL off */
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000418#ifdef CPU_COLDFIRE
419 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
420#endif
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000421#endif
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000422 set_irq_level(0);
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000423
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000424#ifdef HAVE_EEPROM_SETTINGS
425 initialize_eeprom();
426#endif
427
428 adc_init();
429 button_init();
430
431 if ((on_button && button_hold()) ||
432 (rc_on_button && remote_button_hold()))
433 {
434 hold_status = true;
435 }
436
437 /* Power on the hard drive early, to speed up the loading.
438 Some H300 don't like this, so we only do it for the H100 */
439#ifndef IRIVER_H300_SERIES
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000440 if (!hold_status
441# ifdef HAVE_EEPROM_SETTINGS
442 && !recovery_mode
443# endif
444 )
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000445 {
446 ide_power_enable(true);
447 }
448
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000449# ifdef EEPROM_SETTINGS
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000450 if (!hold_status && !usb_detect() && !recovery_mode)
451 try_flashboot();
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000452# endif
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000453#endif
454
455 backlight_init();
Linus Nielsen Feltzing2fcd1b02006-02-04 23:09:56 +0000456#ifdef HAVE_UDA1380
Marcoen Hirschberg6c2e1b82006-12-06 10:36:20 +0000457 audiohw_reset();
Linus Nielsen Feltzing2fcd1b02006-02-04 23:09:56 +0000458#endif
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000459
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000460 lcd_init();
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000461#ifdef HAVE_REMOTE_LCD
462 lcd_remote_init();
463#endif
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000464 font_init();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000465
466 lcd_setfont(FONT_SYSFIXED);
467
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000468 printf("Rockbox boot loader");
469 printf("Version %s", version);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000470
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000471 sleep(HZ/50); /* Allow the button driver to check the buttons */
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000472 rec_button = ((button_status() & BUTTON_REC) == BUTTON_REC)
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000473 || ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000474
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000475 check_battery();
476
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000477 /* Don't start if the Hold button is active on the device you
478 are starting with */
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000479 if (!usb_detect() && (hold_status
480#ifdef HAVE_EEPROM_SETTINGS
481 || recovery_mode
482#endif
483 ))
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000484 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000485 if (detect_original_firmware())
486 {
487 printf("Hold switch on");
488 shutdown();
489 }
490
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000491#ifdef HAVE_EEPROM_SETTINGS
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000492 failsafe_menu();
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000493#endif
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000494 }
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000495
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000496 /* Holding REC while starting runs the original firmware */
497 if (detect_original_firmware() && rec_button)
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000498 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000499 printf("Starting original firmware...");
500 start_iriver_fw();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000501 }
502
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000503 usb_init();
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000504
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000505 /* A hack to enter USB mode without using the USB thread */
506 if(usb_detect())
507 {
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000508 const char msg[] = "Bootloader USB mode";
509 int w, h;
510 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000511 reset_screen();
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000512 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000513 lcd_update();
514
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000515#ifdef HAVE_REMOTE_LCD
516 lcd_remote_puts(0, 3, msg);
517 lcd_remote_update();
518#endif
519
Peter D'Hoyec4a59a22006-08-15 22:54:06 +0000520#ifdef HAVE_EEPROM_SETTINGS
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000521 if (firmware_settings.initialized)
522 {
523 firmware_settings.disk_clean = false;
524 eeprom_settings_store();
525 }
526#endif
Linus Nielsen Feltzing237ff9e2007-02-28 00:06:35 +0000527 ide_power_enable(true);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000528 ata_enable(false);
Linus Nielsen Feltzing237ff9e2007-02-28 00:06:35 +0000529 sleep(HZ/20);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000530 usb_enable(true);
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000531 cpu_idle_mode(true);
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000532 while (usb_detect())
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000533 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000534 /* Print the battery status. */
535 line = 0;
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000536 remote_line = 0;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000537 check_battery();
538
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000539 ata_spin(); /* Prevent the drive from spinning down */
540 sleep(HZ);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000541
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000542 /* Backlight OFF */
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000543 __backlight_off();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000544 }
545
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000546 cpu_idle_mode(false);
Linus Nielsen Feltzing13103902005-07-19 11:03:00 +0000547 usb_enable(false);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000548
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000549 reset_screen();
Linus Nielsen Feltzing13103902005-07-19 11:03:00 +0000550 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000551 }
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000552
Linus Nielsen Feltzing237ff9e2007-02-28 00:06:35 +0000553 rc = ata_init();
554 if(rc)
555 {
556 reset_screen();
557 printf("ATA error: %d", rc);
558 printf("Insert USB cable and press");
559 printf("a button");
560 while(!(button_get(true) & BUTTON_REL));
561 }
562
563
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000564 disk_init();
565
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000566 rc = disk_mount_all();
567 if (rc<=0)
568 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000569 reset_screen();
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000570 printf("No partition found");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000571 while(button_get(true) != SYS_USB_CONNECTED) {};
572 }
573
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000574 printf("Loading firmware");
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000575 i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE);
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000576 if(i < 0)
577 printf("Error: %d", strerror(i));
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000578
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000579 if (i == EOK)
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000580 start_firmware();
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000581
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000582 if (!detect_original_firmware())
Miika Pekkarinen973ee5d2006-09-04 16:06:11 +0000583 {
584 printf("No firmware found on disk");
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000585 sleep(HZ*2);
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000586 shutdown();
Miika Pekkarinen973ee5d2006-09-04 16:06:11 +0000587 }
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000588 else {
589 sleep(HZ*2);
Miika Pekkarinen973ee5d2006-09-04 16:06:11 +0000590 start_iriver_fw();
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000591 }
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000592}
593
594/* These functions are present in the firmware library, but we reimplement
595 them here because the originals do a lot more than we want */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000596void screen_dump(void)
597{
598}