blob: 0306eea5dcbed5de3df9c2e410db387868e1d22b [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"
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +000044#include "uda1380.h"
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +000045#include "eeprom_settings.h"
Linus Nielsen Feltzing48556152007-03-02 13:58:00 +000046#include "rbunicode.h"
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +000047#include "common.h"
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +000048
49#include <stdarg.h>
50
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +000051/* Maximum allowed firmware image size. 10MB is more than enough */
52#define MAX_LOADSIZE (10*1024*1024)
53
Linus Nielsen Feltzing62c768c2005-07-09 07:46:42 +000054#define DRAM_START 0x31000000
55
Miika Pekkarinen886c3022007-01-13 08:16:02 +000056#ifdef HAVE_EEPROM_SETTINGS
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000057static bool recovery_mode = false;
Miika Pekkarinen886c3022007-01-13 08:16:02 +000058#endif
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000059
Linus Nielsen Feltzinga2e8cf72005-07-15 07:42:25 +000060char version[] = APPSVERSION;
61
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000062/* Reset the cookie for the crt0 crash check */
63inline void __reset_cookie(void)
64{
65 asm(" move.l #0,%d0");
66 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +000067}
68
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000069void start_iriver_fw(void)
70{
71 asm(" move.w #0x2700,%sr");
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000072 __reset_cookie();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000073 asm(" movec.l %d0,%vbr");
74 asm(" move.l 0,%sp");
75 asm(" lea.l 8,%a0");
76 asm(" jmp (%a0)");
77}
78
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000079void start_firmware(void)
80{
81 asm(" move.w #0x2700,%sr");
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000082 __reset_cookie();
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +000083 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000084 asm(" movec.l %d0,%vbr");
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +000085 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
86 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000087 asm(" jmp (%a0)");
88}
89
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +000090#ifdef IRIVER_H100_SERIES
91void start_flashed_romimage(void)
92{
93 uint8_t *src = (uint8_t *)FLASH_ROMIMAGE_ENTRY;
94 int *reset_vector;
95
96 if (!detect_flashed_romimage())
97 return ;
98
99 reset_vector = (int *)(&src[sizeof(struct flash_header)+4]);
100
101 asm(" move.w #0x2700,%sr");
102 __reset_cookie();
103
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]));
108 asm(" jmp (%a0)");
109
110 /* Failure */
111 power_off();
112}
113
114void start_flashed_ramimage(void)
115{
116 struct flash_header hdr;
117 unsigned char *buf = (unsigned char *)DRAM_START;
118 uint8_t *src = (uint8_t *)FLASH_RAMIMAGE_ENTRY;
119
120 if (!detect_flashed_ramimage())
121 return;
122
123 /* Load firmware from flash */
124 cpu_boost(true);
125 memcpy(&hdr, src, sizeof(struct flash_header));
126 src += sizeof(struct flash_header);
127 memcpy(buf, src, hdr.length);
128 cpu_boost(false);
129
130 start_firmware();
131
132 /* Failure */
133 power_off();
134}
135#endif /* IRIVER_H100_SERIES */
136
137void shutdown(void)
138{
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();
144#endif
145
146 /* We need to gracefully spin down the disk to prevent clicks. */
147 if (ide_powered())
148 {
149 /* Make sure ATA has been initialized. */
150 ata_init();
151
152 /* And put the disk into sleep immediately. */
153 ata_sleepnow();
154 }
155
156 sleep(HZ*2);
157
158 /* Backlight OFF */
159 __backlight_off();
160#ifdef HAVE_REMOTE_LCD
161 __remote_backlight_off();
162#endif
163
164 __reset_cookie();
165 power_off();
166}
167
168/* Print the battery voltage (and a warning message). */
169void check_battery(void)
170{
Jens Arnold0fac4922007-08-17 06:45:18 +0000171 int battery_voltage, batt_int, batt_frac;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000172
Jens Arnold0fac4922007-08-17 06:45:18 +0000173 battery_voltage = battery_adc_voltage();
174 batt_int = battery_voltage / 1000;
175 batt_frac = (battery_voltage % 1000) / 10;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000176
177 printf("Batt: %d.%02dV", batt_int, batt_frac);
178
179 if (battery_voltage <= 310)
180 {
181 printf("WARNING! BATTERY LOW!!");
182 sleep(HZ*2);
183 }
184}
185
186#ifdef HAVE_EEPROM_SETTINGS
187void initialize_eeprom(void)
188{
189 if (detect_original_firmware())
190 return ;
191
192 if (!eeprom_settings_init())
193 {
194 recovery_mode = true;
195 return ;
196 }
197
198 /* If bootloader version has not been reset, disk might
199 * not be intact. */
200 if (firmware_settings.bl_version || !firmware_settings.disk_clean)
201 {
202 firmware_settings.disk_clean = false;
203 recovery_mode = true;
204 }
205
206 firmware_settings.bl_version = EEPROM_SETTINGS_BL_MINVER;
207 eeprom_settings_store();
208}
209
210void try_flashboot(void)
211{
212 if (!firmware_settings.initialized)
213 return ;
214
215 switch (firmware_settings.bootmethod)
216 {
217 case BOOT_DISK:
218 return;
219
220 case BOOT_ROM:
221 start_flashed_romimage();
222 recovery_mode = true;
223 break;
224
225 case BOOT_RAM:
226 start_flashed_ramimage();
227 recovery_mode = true;
228 break;
229
230 default:
231 recovery_mode = true;
232 return;
233 }
234}
235
236static const char *options[] = {
237 "Boot from disk",
238 "Boot RAM image",
239 "Boot ROM image",
240 "Shutdown"
241};
242
243#define FAILSAFE_OPTIONS 4
244void failsafe_menu(void)
245{
246 int timeout = 15;
247 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
271 while (timeout > 0)
272 {
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
288 snprintf(buf, sizeof(buf), "Time left: %ds", timeout);
289 lcd_puts(0, 10, buf);
290 lcd_update();
291 button = button_get_w_tmo(HZ);
292
293 if (button == BUTTON_NONE)
294 {
295 timeout--;
296 continue ;
297 }
298
299 timeout = 15;
300 /* Ignore the ON/PLAY -button because it can cause trouble
301 with the RTC alarm mod. */
302 switch (button & ~(BUTTON_ON))
303 {
304 case BUTTON_UP:
305 case BUTTON_RC_REW:
306 if (option > 0)
307 option--;
308 break ;
309
310 case BUTTON_DOWN:
311 case BUTTON_RC_FF:
312 if (option < FAILSAFE_OPTIONS-1)
313 option++;
314 break ;
315
316 case BUTTON_SELECT:
317 case BUTTON_RC_ON:
318 timeout = 0;
319 break ;
320
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
333 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
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000358void main(void)
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000359{
360 int i;
361 int rc;
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000362 bool rc_on_button = false;
363 bool on_button = false;
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000364 bool rec_button = false;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000365 bool hold_status = false;
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000366 int data;
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000367 extern int line; /* From common.c */
368 extern int remote_line; /* From common.c */
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000369
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000370 /* We want to read the buttons as early as possible, before the user
371 releases the ON button */
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000372
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000373 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
374 (The ON and Hold buttons on the main unit and the remote) */
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000375 or_l(0x00100062, &GPIO1_FUNCTION);
376 and_l(~0x00100062, &GPIO1_ENABLE);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000377
378 data = GPIO1_READ;
379 if ((data & 0x20) == 0)
380 on_button = true;
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000381
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000382 if ((data & 0x40) == 0)
383 rc_on_button = true;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000384
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000385 /* Set the default state of the hard drive power to OFF */
386 ide_power_enable(false);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000387
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000388 power_init();
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000389
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000390 /* Turn off if neither ON button is pressed */
Linus Nielsen Feltzing54da9242006-01-10 23:11:57 +0000391 if(!(on_button || rc_on_button || usb_detect()))
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000392 {
393 __reset_cookie();
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000394 power_off();
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000395 }
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000396
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000397 /* Start with the main backlight OFF. */
398 __backlight_init();
399 __backlight_off();
400
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000401 /* Remote backlight ON */
402#ifdef HAVE_REMOTE_LCD
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000403 __remote_backlight_on();
Linus Nielsen Feltzingb68cb9b2005-12-03 00:54:59 +0000404#endif
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000405
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000406 system_init();
407 kernel_init();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000408
409#ifdef HAVE_ADJUSTABLE_CPU_FREQ
410 /* Set up waitstates for the peripherals */
411 set_cpu_frequency(0); /* PLL off */
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000412#ifdef CPU_COLDFIRE
413 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
414#endif
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000415#endif
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000416 set_irq_level(0);
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000417
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000418#ifdef HAVE_EEPROM_SETTINGS
419 initialize_eeprom();
420#endif
421
422 adc_init();
423 button_init();
424
425 if ((on_button && button_hold()) ||
426 (rc_on_button && remote_button_hold()))
427 {
428 hold_status = true;
429 }
430
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000431 /* Power on the hard drive early, to speed up the loading. */
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000432 if (!hold_status
433# ifdef HAVE_EEPROM_SETTINGS
434 && !recovery_mode
435# endif
436 )
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000437 {
438 ide_power_enable(true);
439 }
440
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000441# ifdef EEPROM_SETTINGS
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000442 if (!hold_status && !usb_detect() && !recovery_mode)
443 try_flashboot();
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000444# endif
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000445
446 backlight_init();
Linus Nielsen Feltzing2fcd1b02006-02-04 23:09:56 +0000447#ifdef HAVE_UDA1380
Marcoen Hirschberg6c2e1b82006-12-06 10:36:20 +0000448 audiohw_reset();
Linus Nielsen Feltzing2fcd1b02006-02-04 23:09:56 +0000449#endif
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000450
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000451 lcd_init();
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000452#ifdef HAVE_REMOTE_LCD
453 lcd_remote_init();
454#endif
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000455 font_init();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000456
457 lcd_setfont(FONT_SYSFIXED);
458
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000459 printf("Rockbox boot loader");
460 printf("Version %s", version);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000461
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000462 sleep(HZ/50); /* Allow the button driver to check the buttons */
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000463 rec_button = ((button_status() & BUTTON_REC) == BUTTON_REC)
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000464 || ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000465
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000466 check_battery();
467
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000468 /* Don't start if the Hold button is active on the device you
469 are starting with */
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000470 if (!usb_detect() && (hold_status
471#ifdef HAVE_EEPROM_SETTINGS
472 || recovery_mode
473#endif
474 ))
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000475 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000476 if (detect_original_firmware())
477 {
478 printf("Hold switch on");
479 shutdown();
480 }
481
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000482#ifdef HAVE_EEPROM_SETTINGS
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000483 failsafe_menu();
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000484#endif
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000485 }
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000486
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000487 /* Holding REC while starting runs the original firmware */
488 if (detect_original_firmware() && rec_button)
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000489 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000490 printf("Starting original firmware...");
491 start_iriver_fw();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000492 }
493
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000494 usb_init();
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000495
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000496 /* A hack to enter USB mode without using the USB thread */
497 if(usb_detect())
498 {
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000499 const char msg[] = "Bootloader USB mode";
500 int w, h;
501 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000502 reset_screen();
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000503 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000504 lcd_update();
505
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000506#ifdef HAVE_REMOTE_LCD
507 lcd_remote_puts(0, 3, msg);
508 lcd_remote_update();
509#endif
510
Peter D'Hoyec4a59a22006-08-15 22:54:06 +0000511#ifdef HAVE_EEPROM_SETTINGS
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000512 if (firmware_settings.initialized)
513 {
514 firmware_settings.disk_clean = false;
515 eeprom_settings_store();
516 }
517#endif
Linus Nielsen Feltzing237ff9e2007-02-28 00:06:35 +0000518 ide_power_enable(true);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000519 ata_enable(false);
Linus Nielsen Feltzing237ff9e2007-02-28 00:06:35 +0000520 sleep(HZ/20);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000521 usb_enable(true);
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000522 cpu_idle_mode(true);
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000523 while (usb_detect())
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000524 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000525 /* Print the battery status. */
526 line = 0;
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000527 remote_line = 0;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000528 check_battery();
529
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000530 ata_spin(); /* Prevent the drive from spinning down */
531 sleep(HZ);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000532
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000533 /* Backlight OFF */
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000534 __backlight_off();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000535 }
536
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000537 cpu_idle_mode(false);
Linus Nielsen Feltzing13103902005-07-19 11:03:00 +0000538 usb_enable(false);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000539
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000540 reset_screen();
Linus Nielsen Feltzing13103902005-07-19 11:03:00 +0000541 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000542 }
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000543
Linus Nielsen Feltzing237ff9e2007-02-28 00:06:35 +0000544 rc = ata_init();
545 if(rc)
546 {
547 reset_screen();
548 printf("ATA error: %d", rc);
549 printf("Insert USB cable and press");
550 printf("a button");
551 while(!(button_get(true) & BUTTON_REL));
552 }
553
554
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000555 disk_init();
556
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000557 rc = disk_mount_all();
558 if (rc<=0)
559 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000560 reset_screen();
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000561 printf("No partition found");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000562 while(button_get(true) != SYS_USB_CONNECTED) {};
563 }
564
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000565 printf("Loading firmware");
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000566 i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE);
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000567 if(i < 0)
Steve Bavin0e16bd32007-06-18 06:52:29 +0000568 printf("Error: %s", strerror(i));
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000569
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000570 if (i == EOK)
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000571 start_firmware();
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000572
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000573 if (!detect_original_firmware())
Miika Pekkarinen973ee5d2006-09-04 16:06:11 +0000574 {
575 printf("No firmware found on disk");
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000576 sleep(HZ*2);
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000577 shutdown();
Miika Pekkarinen973ee5d2006-09-04 16:06:11 +0000578 }
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000579 else {
580 sleep(HZ*2);
Miika Pekkarinen973ee5d2006-09-04 16:06:11 +0000581 start_iriver_fw();
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000582 }
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000583}
584
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 */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000587void screen_dump(void)
588{
589}
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000590
591int usb_screen(void)
592{
593 return 0;
594}
595
596unsigned short *bidi_l2v(const unsigned char *str, int orientation)
597{
598 static unsigned short utf16_buf[SCROLL_LINE_SIZE];
599 unsigned short *target;
600 (void)orientation;
601
602 target = utf16_buf;
603
604 while (*str)
605 str = utf8decode(str, target++);
606 *target = 0;
607 return utf16_buf;
608}