blob: a9411a766db4df549c89ce9e9c98c68b6b74fccf [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 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 */
158 __backlight_off();
159#ifdef HAVE_REMOTE_LCD
160 __remote_backlight_off();
161#endif
162
163 __reset_cookie();
164 power_off();
165}
166
167/* Print the battery voltage (and a warning message). */
168void check_battery(void)
169{
170 int adc_battery, battery_voltage, batt_int, batt_frac;
171
172 adc_battery = adc_read(ADC_BATTERY);
173
174 battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000;
175 batt_int = battery_voltage / 100;
176 batt_frac = battery_voltage % 100;
177
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
245void failsafe_menu(void)
246{
247 int timeout = 15;
248 int option = 3;
249 int button;
250 int defopt = -1;
251 char buf[32];
252 int i;
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000253 extern int line;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000254
255 reset_screen();
256 printf("Bootloader %s", version);
257 check_battery();
258 printf("=========================");
259 line += FAILSAFE_OPTIONS;
260 printf("");
261 printf(" [NAVI] to confirm.");
262 printf(" [REC] to set as default.");
263 printf("");
264
265 if (firmware_settings.initialized)
266 {
267 defopt = firmware_settings.bootmethod;
268 if (defopt < 0 || defopt >= FAILSAFE_OPTIONS)
269 defopt = option;
270 }
271
272 while (timeout > 0)
273 {
274 /* Draw the menu. */
275 line = 3;
276 for (i = 0; i < FAILSAFE_OPTIONS; i++)
277 {
278 char *def = "[DEF]";
279 char *arrow = "->";
280
281 if (i != defopt)
282 def = "";
283 if (i != option)
284 arrow = " ";
285
286 printf("%s %s %s", arrow, options[i], def);
287 }
288
289 snprintf(buf, sizeof(buf), "Time left: %ds", timeout);
290 lcd_puts(0, 10, buf);
291 lcd_update();
292 button = button_get_w_tmo(HZ);
293
294 if (button == BUTTON_NONE)
295 {
296 timeout--;
297 continue ;
298 }
299
300 timeout = 15;
301 /* 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:
319 timeout = 0;
320 break ;
321
322 case BUTTON_REC:
323 case BUTTON_RC_REC:
324 if (firmware_settings.initialized)
325 {
326 firmware_settings.bootmethod = option;
327 eeprom_settings_store();
328 defopt = option;
329 }
330 break ;
331 }
332 }
333
334 lcd_puts(0, 10, "Executing command...");
335 lcd_update();
336 sleep(HZ);
337 reset_screen();
338
339 switch (option)
340 {
341 case BOOT_DISK:
342 return ;
343
344 case BOOT_RAM:
345 start_flashed_ramimage();
346 printf("Image not found");
347 break;
348
349 case BOOT_ROM:
350 start_flashed_romimage();
351 printf("Image not found");
352 break;
353 }
354
355 shutdown();
356}
357#endif
358
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000359void main(void)
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000360{
361 int i;
362 int rc;
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000363 bool rc_on_button = false;
364 bool on_button = false;
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000365 bool rec_button = false;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000366 bool hold_status = false;
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000367 int data;
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000368 extern int line; /* From common.c */
369 extern int remote_line; /* From common.c */
Linus Nielsen Feltzingec549ec2007-02-22 21:38:59 +0000370
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000371 /* We want to read the buttons as early as possible, before the user
372 releases the ON button */
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000373
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000374 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
375 (The ON and Hold buttons on the main unit and the remote) */
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000376 or_l(0x00100062, &GPIO1_FUNCTION);
377 and_l(~0x00100062, &GPIO1_ENABLE);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000378
379 data = GPIO1_READ;
380 if ((data & 0x20) == 0)
381 on_button = true;
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000382
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000383 if ((data & 0x40) == 0)
384 rc_on_button = true;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000385
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000386 /* Set the default state of the hard drive power to OFF */
387 ide_power_enable(false);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000388
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000389 power_init();
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000390
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000391 /* Turn off if neither ON button is pressed */
Linus Nielsen Feltzing54da9242006-01-10 23:11:57 +0000392 if(!(on_button || rc_on_button || usb_detect()))
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000393 {
394 __reset_cookie();
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000395 power_off();
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000396 }
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000397
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000398 /* Start with the main backlight OFF. */
399 __backlight_init();
400 __backlight_off();
401
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000402 /* Remote backlight ON */
403#ifdef HAVE_REMOTE_LCD
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000404 __remote_backlight_on();
Linus Nielsen Feltzingb68cb9b2005-12-03 00:54:59 +0000405#endif
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000406
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000407 system_init();
408 kernel_init();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000409
410#ifdef HAVE_ADJUSTABLE_CPU_FREQ
411 /* Set up waitstates for the peripherals */
412 set_cpu_frequency(0); /* PLL off */
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000413#ifdef CPU_COLDFIRE
414 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
415#endif
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000416#endif
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000417 set_irq_level(0);
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000418
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000419#ifdef HAVE_EEPROM_SETTINGS
420 initialize_eeprom();
421#endif
422
423 adc_init();
424 button_init();
425
426 if ((on_button && button_hold()) ||
427 (rc_on_button && remote_button_hold()))
428 {
429 hold_status = true;
430 }
431
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000432 /* Power on the hard drive early, to speed up the loading. */
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000433 if (!hold_status
434# ifdef HAVE_EEPROM_SETTINGS
435 && !recovery_mode
436# endif
437 )
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000438 {
439 ide_power_enable(true);
440 }
441
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000442# ifdef EEPROM_SETTINGS
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000443 if (!hold_status && !usb_detect() && !recovery_mode)
444 try_flashboot();
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000445# endif
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000446
447 backlight_init();
Linus Nielsen Feltzing2fcd1b02006-02-04 23:09:56 +0000448#ifdef HAVE_UDA1380
Marcoen Hirschberg6c2e1b82006-12-06 10:36:20 +0000449 audiohw_reset();
Linus Nielsen Feltzing2fcd1b02006-02-04 23:09:56 +0000450#endif
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000451
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000452 lcd_init();
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000453#ifdef HAVE_REMOTE_LCD
454 lcd_remote_init();
455#endif
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000456 font_init();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000457
458 lcd_setfont(FONT_SYSFIXED);
459
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000460 printf("Rockbox boot loader");
461 printf("Version %s", version);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000462
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000463 sleep(HZ/50); /* Allow the button driver to check the buttons */
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000464 rec_button = ((button_status() & BUTTON_REC) == BUTTON_REC)
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000465 || ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000466
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000467 check_battery();
468
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000469 /* Don't start if the Hold button is active on the device you
470 are starting with */
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000471 if (!usb_detect() && (hold_status
472#ifdef HAVE_EEPROM_SETTINGS
473 || recovery_mode
474#endif
475 ))
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000476 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000477 if (detect_original_firmware())
478 {
479 printf("Hold switch on");
480 shutdown();
481 }
482
Miika Pekkarinen20171c12007-01-13 09:08:18 +0000483#ifdef HAVE_EEPROM_SETTINGS
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000484 failsafe_menu();
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000485#endif
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000486 }
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000487
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000488 /* Holding REC while starting runs the original firmware */
489 if (detect_original_firmware() && rec_button)
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000490 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000491 printf("Starting original firmware...");
492 start_iriver_fw();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000493 }
494
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000495 usb_init();
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000496
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000497 /* A hack to enter USB mode without using the USB thread */
498 if(usb_detect())
499 {
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000500 const char msg[] = "Bootloader USB mode";
501 int w, h;
502 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000503 reset_screen();
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000504 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000505 lcd_update();
506
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000507#ifdef HAVE_REMOTE_LCD
508 lcd_remote_puts(0, 3, msg);
509 lcd_remote_update();
510#endif
511
Peter D'Hoyec4a59a22006-08-15 22:54:06 +0000512#ifdef HAVE_EEPROM_SETTINGS
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000513 if (firmware_settings.initialized)
514 {
515 firmware_settings.disk_clean = false;
516 eeprom_settings_store();
517 }
518#endif
Linus Nielsen Feltzing237ff9e2007-02-28 00:06:35 +0000519 ide_power_enable(true);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000520 ata_enable(false);
Linus Nielsen Feltzing237ff9e2007-02-28 00:06:35 +0000521 sleep(HZ/20);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000522 usb_enable(true);
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000523 cpu_idle_mode(true);
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000524 while (usb_detect())
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000525 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000526 /* Print the battery status. */
527 line = 0;
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000528 remote_line = 0;
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000529 check_battery();
530
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000531 ata_spin(); /* Prevent the drive from spinning down */
532 sleep(HZ);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000533
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000534 /* Backlight OFF */
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000535 __backlight_off();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000536 }
537
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000538 cpu_idle_mode(false);
Linus Nielsen Feltzing13103902005-07-19 11:03:00 +0000539 usb_enable(false);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000540
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000541 reset_screen();
Linus Nielsen Feltzing13103902005-07-19 11:03:00 +0000542 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000543 }
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000544
Linus Nielsen Feltzing237ff9e2007-02-28 00:06:35 +0000545 rc = ata_init();
546 if(rc)
547 {
548 reset_screen();
549 printf("ATA error: %d", rc);
550 printf("Insert USB cable and press");
551 printf("a button");
552 while(!(button_get(true) & BUTTON_REL));
553 }
554
555
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000556 disk_init();
557
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000558 rc = disk_mount_all();
559 if (rc<=0)
560 {
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000561 reset_screen();
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000562 printf("No partition found");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000563 while(button_get(true) != SYS_USB_CONNECTED) {};
564 }
565
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000566 printf("Loading firmware");
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000567 i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE);
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000568 if(i < 0)
569 printf("Error: %d", strerror(i));
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000570
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000571 if (i == EOK)
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000572 start_firmware();
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000573
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000574 if (!detect_original_firmware())
Miika Pekkarinen973ee5d2006-09-04 16:06:11 +0000575 {
576 printf("No firmware found on disk");
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +0000577 sleep(HZ*2);
Miika Pekkarinen35b0c3f2007-01-12 20:45:37 +0000578 shutdown();
Miika Pekkarinen973ee5d2006-09-04 16:06:11 +0000579 }
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000580 else {
581 sleep(HZ*2);
Miika Pekkarinen973ee5d2006-09-04 16:06:11 +0000582 start_iriver_fw();
Linus Nielsen Feltzing343c4282007-02-28 08:40:37 +0000583 }
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000584}
585
586/* These functions are present in the firmware library, but we reimplement
587 them here because the originals do a lot more than we want */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000588void screen_dump(void)
589{
590}
Linus Nielsen Feltzingff179052007-03-02 11:28:23 +0000591
592int usb_screen(void)
593{
594 return 0;
595}
596
597unsigned short *bidi_l2v(const unsigned char *str, int orientation)
598{
599 static unsigned short utf16_buf[SCROLL_LINE_SIZE];
600 unsigned short *target;
601 (void)orientation;
602
603 target = utf16_buf;
604
605 while (*str)
606 str = utf8decode(str, target++);
607 *target = 0;
608 return utf16_buf;
609}