blob: 0f3d706d7b8cd2d9290787e3b67b15e329708360 [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"
28#include "kernel.h"
29#include "thread.h"
30#include "ata.h"
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +000031#include "usb.h"
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000032#include "disk.h"
33#include "font.h"
34#include "adc.h"
35#include "backlight.h"
Rani Hod78f425d2006-07-29 13:31:43 +000036#ifdef IAUDIO_X5
37#include "backlight-target.h"
38#endif
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000039#include "button.h"
40#include "panic.h"
41#include "power.h"
42#include "file.h"
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +000043#include "uda1380.h"
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +000044#include "eeprom_settings.h"
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000045
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +000046#include "pcf50606.h"
47
48#include <stdarg.h>
49
Linus Nielsen Feltzing62c768c2005-07-09 07:46:42 +000050#define DRAM_START 0x31000000
51
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000052int line = 0;
53
54int usb_screen(void)
55{
56 return 0;
57}
58
Linus Nielsen Feltzinga2e8cf72005-07-15 07:42:25 +000059char version[] = APPSVERSION;
60
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +000061char printfbuf[256];
62
63void printf(const char *format, ...)
64{
65 int len;
66 unsigned char *ptr;
67 va_list ap;
68 va_start(ap, format);
69
70 ptr = printfbuf;
71 len = vsnprintf(ptr, sizeof(printfbuf), format, ap);
72 va_end(ap);
73
74 lcd_puts(0, line++, ptr);
75 lcd_update();
76 if(line >= 16)
77 line = 0;
78}
79
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000080void start_iriver_fw(void)
81{
82 asm(" move.w #0x2700,%sr");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +000083 /* Reset the cookie for the crt0 crash check */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000084 asm(" move.l #0,%d0");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +000085 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000086 asm(" movec.l %d0,%vbr");
87 asm(" move.l 0,%sp");
88 asm(" lea.l 8,%a0");
89 asm(" jmp (%a0)");
90}
91
92int load_firmware(void)
93{
94 int fd;
95 int rc;
96 int len;
97 unsigned long chksum;
Linus Nielsen Feltzinga75f0e52005-07-09 09:04:37 +000098 char model[5];
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000099 unsigned long sum;
100 int i;
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000101 unsigned char *buf = (unsigned char *)DRAM_START;
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000102
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000103 fd = open("/.rockbox/" BOOTFILE, O_RDONLY);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000104 if(fd < 0)
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000105 {
106 fd = open("/" BOOTFILE, O_RDONLY);
107 if(fd < 0)
108 return -1;
109 }
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000110
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000111 len = filesize(fd) - 8;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000112
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000113 printf("Length: %x", len);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000114 lcd_update();
115
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000116 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000117
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000118 rc = read(fd, &chksum, 4);
119 if(rc < 4)
120 return -2;
121
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000122 printf("Checksum: %x", chksum);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000123 lcd_update();
124
Linus Nielsen Feltzinga75f0e52005-07-09 09:04:37 +0000125 rc = read(fd, model, 4);
126 if(rc < 4)
127 return -3;
128
129 model[4] = 0;
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000130
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000131 printf("Model name: %s", model);
Linus Nielsen Feltzinga75f0e52005-07-09 09:04:37 +0000132 lcd_update();
133
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000134 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000135
136 rc = read(fd, buf, len);
137 if(rc < len)
138 return -4;
139
140 close(fd);
141
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000142 sum = MODEL_NUMBER;
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000143
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000144 for(i = 0;i < len;i++) {
145 sum += buf[i];
146 }
147
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000148 printf("Sum: %x", sum);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000149 lcd_update();
150
151 if(sum != chksum)
152 return -5;
153
154 return 0;
155}
156
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000157int load_flashed_rockbox(void)
158{
159 struct flash_header hdr;
160 unsigned char *buf = (unsigned char *)DRAM_START;
161 uint8_t *src = (uint8_t *)FLASH_ENTRYPOINT;
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000162
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000163 cpu_boost(true);
164 memcpy(&hdr, src, sizeof(struct flash_header));
165 src += sizeof(struct flash_header);
166 memcpy(buf, src, hdr.length);
167 cpu_boost(false);
168
169 return 0;
170}
171
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000172
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000173void start_firmware(void)
174{
175 asm(" move.w #0x2700,%sr");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +0000176 /* Reset the cookie for the crt0 crash check */
177 asm(" move.l #0,%d0");
178 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000179 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000180 asm(" movec.l %d0,%vbr");
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000181 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
182 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000183 asm(" jmp (%a0)");
184}
185
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000186void main(void)
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000187{
188 int i;
189 int rc;
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000190 bool rc_on_button = false;
191 bool on_button = false;
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000192 bool rec_button = false;
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000193 int data;
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000194 int adc_battery, battery_voltage, batt_int, batt_frac;
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000195
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000196#ifdef IAUDIO_X5
197 (void)rc_on_button;
198 (void)on_button;
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000199 (void)rec_button;
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000200 (void)data;
201 power_init();
202
203 system_init();
204 kernel_init();
205
206 set_cpu_frequency(CPUFREQ_NORMAL);
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000207 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000208
209 set_irq_level(0);
210 lcd_init();
Rani Hod78f425d2006-07-29 13:31:43 +0000211 __backlight_on();
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000212 font_init();
213 adc_init();
214 button_init();
215
216 printf("Rockbox boot loader");
217 printf("Version %s", version);
218 lcd_update();
219
220 adc_battery = adc_read(ADC_BATTERY);
221
222 battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000;
223 batt_int = battery_voltage / 100;
224 batt_frac = battery_voltage % 100;
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000225
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000226 printf("Batt: %d.%02dV", batt_int, batt_frac);
227 lcd_update();
228
229 rc = ata_init();
230 if(rc)
231 {
232 printf("ATA error: %d", rc);
233 sleep(HZ*5);
234 power_off();
235 }
236
237 disk_init();
238
239 rc = disk_mount_all();
240 if (rc<=0)
241 {
242 printf("No partition found");
243 sleep(HZ*5);
244 power_off();
245 }
246
247 printf("Loading firmware");
248 lcd_update();
249 i = load_firmware();
250 printf("Result: %d", i);
251 lcd_update();
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000252
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000253 if(i == 0)
254 start_firmware();
255
256 power_off();
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000257
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000258#else
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000259 /* We want to read the buttons as early as possible, before the user
260 releases the ON button */
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000261
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000262 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
263 (The ON and Hold buttons on the main unit and the remote) */
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000264 or_l(0x00100062, &GPIO1_FUNCTION);
265 and_l(~0x00100062, &GPIO1_ENABLE);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000266
267 data = GPIO1_READ;
268 if ((data & 0x20) == 0)
269 on_button = true;
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000270
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000271 if ((data & 0x40) == 0)
272 rc_on_button = true;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000273
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000274 /* Set the default state of the hard drive power to OFF */
275 ide_power_enable(false);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000276
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000277 power_init();
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000278
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000279 /* Turn off if neither ON button is pressed */
Linus Nielsen Feltzing54da9242006-01-10 23:11:57 +0000280 if(!(on_button || rc_on_button || usb_detect()))
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000281 power_off();
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000282
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000283 /* Backlight ON */
284 or_l(0x00020000, &GPIO1_ENABLE);
285 or_l(0x00020000, &GPIO1_FUNCTION);
286 and_l(~0x00020000, &GPIO1_OUT);
287
288 /* Remote backlight ON */
289#ifdef HAVE_REMOTE_LCD
290#ifdef IRIVER_H300_SERIES
291 or_l(0x00000002, &GPIO1_ENABLE);
292 and_l(~0x00000002, &GPIO1_OUT);
293#else
294 or_l(0x00000800, &GPIO_ENABLE);
295 or_l(0x00000800, &GPIO_FUNCTION);
296 and_l(~0x00000800, &GPIO_OUT);
297#endif
298#endif
299
Linus Nielsen Feltzingb68cb9b2005-12-03 00:54:59 +0000300 /* Power on the hard drive early, to speed up the loading.
301 Some H300 don't like this, so we only do it for the H100 */
302#ifndef IRIVER_H300_SERIES
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000303 if(!((on_button && button_hold()) ||
304 (rc_on_button && remote_button_hold()))) {
305 ide_power_enable(true);
306 }
Linus Nielsen Feltzingb68cb9b2005-12-03 00:54:59 +0000307#endif
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000308
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000309 system_init();
310 kernel_init();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000311
312#ifdef HAVE_ADJUSTABLE_CPU_FREQ
313 /* Set up waitstates for the peripherals */
314 set_cpu_frequency(0); /* PLL off */
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000315#ifdef CPU_COLDFIRE
316 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
317#endif
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000318#endif
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000319
Linus Nielsen Feltzing2fcd1b02006-02-04 23:09:56 +0000320#ifdef HAVE_UDA1380
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000321 uda1380_reset();
Linus Nielsen Feltzing2fcd1b02006-02-04 23:09:56 +0000322#endif
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000323
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000324 backlight_init();
325 set_irq_level(0);
326 lcd_init();
327 font_init();
328 adc_init();
329 button_init();
330
331 lcd_setfont(FONT_SYSFIXED);
332
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000333 printf("Rockbox boot loader");
334 printf("Version %s", version);
Linus Nielsen Feltzingd1af08f2005-07-09 19:48:22 +0000335 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000336
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000337 sleep(HZ/50); /* Allow the button driver to check the buttons */
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000338 rec_button = ((button_status() & BUTTON_REC) == BUTTON_REC)
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000339 || ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000340
Miika Pekkarinen22860a92006-08-11 19:59:16 +0000341 /* Don't start if the Hold button is active on the device you
342 are starting with */
343 if (!usb_detect() && ((on_button && button_hold()) ||
344 (rc_on_button && remote_button_hold())))
345 {
346 printf("HOLD switch on, power off...");
347 lcd_update();
348 sleep(HZ*2);
349
350 /* Backlight OFF */
351#ifdef HAVE_REMOTE_LCD
352#ifdef IRIVER_H300_SERIES
353 or_l(0x00000002, &GPIO1_OUT);
354#else
355 or_l(0x00000800, &GPIO_OUT);
356#endif
357#endif
358 /* Reset the cookie for the crt0 crash check */
359 asm(" move.l #0,%d0");
360 asm(" move.l %d0,0x10017ffc");
361 power_off();
362 }
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000363
Peter D'Hoyec4a59a22006-08-15 22:54:06 +0000364#ifdef HAVE_EEPROM_SETTINGS
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000365 firmware_settings.initialized = false;
366#endif
367 if (detect_flashed_rockbox())
368 {
369 bool load_from_flash;
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000370
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000371 load_from_flash = !rec_button;
Peter D'Hoyec4a59a22006-08-15 22:54:06 +0000372#ifdef HAVE_EEPROM_SETTINGS
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000373 if (eeprom_settings_init())
374 {
375 /* If bootloader version has not been reset, disk might
376 * not be intact. */
377 if (firmware_settings.bl_version)
378 firmware_settings.disk_clean = false;
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000379
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000380 firmware_settings.bl_version = 7;
381 /* Invert the record button if we want to load from disk
382 * by default. */
383 if (firmware_settings.boot_disk)
384 load_from_flash = rec_button;
385 }
386#endif
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000387
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000388 if (load_from_flash)
389 {
390 /* Load firmware from flash */
391 i = load_flashed_rockbox();
392 printf("Result: %d", i);
393 lcd_update();
394 if (i == 0)
395 {
Peter D'Hoyec4a59a22006-08-15 22:54:06 +0000396#ifdef HAVE_EEPROM_SETTINGS
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000397 eeprom_settings_store();
398#endif
399 start_firmware();
400 printf("Fatal: Corrupted firmware");
401 printf("Hold down REC on next boot");
402 lcd_update();
403 sleep(HZ*2);
404 power_off();
405 }
406 }
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000407
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000408 printf("Loading from disk...");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000409 lcd_update();
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000410 }
411 else
412 {
413 /* Holding REC while starting runs the original firmware */
414 if (rec_button)
415 {
416 printf("Starting original firmware...");
417 lcd_update();
418 start_iriver_fw();
419 }
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000420 }
421
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000422 usb_init();
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000423
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000424 adc_battery = adc_read(ADC_BATTERY);
425
426 battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000;
427 batt_int = battery_voltage / 100;
428 batt_frac = battery_voltage % 100;
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000429
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000430 printf("Batt: %d.%02dV", batt_int, batt_frac);
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000431 lcd_update();
432
433 if(battery_voltage <= 300) {
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000434 printf("WARNING! BATTERY LOW!!");
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000435 lcd_update();
436 sleep(HZ*2);
437 }
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000438
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000439 rc = ata_init();
440 if(rc)
441 {
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000442 lcd_clear_display();
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000443 printf("ATA error: %d", rc);
444 printf("Insert USB cable and press");
445 printf("a button");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000446 lcd_update();
447 while(!(button_get(true) & BUTTON_REL));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000448 }
449
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000450 /* A hack to enter USB mode without using the USB thread */
451 if(usb_detect())
452 {
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000453 const char msg[] = "Bootloader USB mode";
454 int w, h;
455 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000456 lcd_clear_display();
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000457 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000458 lcd_update();
459
Linus Nielsen Feltzing780cfff2006-01-11 15:37:08 +0000460#ifdef IRIVER_H300_SERIES
461 sleep(HZ);
462#endif
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000463
Peter D'Hoyec4a59a22006-08-15 22:54:06 +0000464#ifdef HAVE_EEPROM_SETTINGS
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000465 if (firmware_settings.initialized)
466 {
467 firmware_settings.disk_clean = false;
468 eeprom_settings_store();
469 }
470#endif
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000471 ata_spin();
472 ata_enable(false);
473 usb_enable(true);
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000474 cpu_idle_mode(true);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000475 while(usb_detect())
476 {
477 ata_spin(); /* Prevent the drive from spinning down */
478 sleep(HZ);
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000479
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000480 /* Backlight OFF */
481 or_l(0x00020000, &GPIO1_OUT);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000482 }
483
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000484 cpu_idle_mode(false);
Linus Nielsen Feltzing13103902005-07-19 11:03:00 +0000485 usb_enable(false);
486 ata_init(); /* Reinitialize ATA and continue booting */
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000487
Linus Nielsen Feltzing13103902005-07-19 11:03:00 +0000488 lcd_clear_display();
489 line = 0;
490 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000491 }
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000492
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000493 disk_init();
494
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000495 rc = disk_mount_all();
496 if (rc<=0)
497 {
498 lcd_clear_display();
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000499 printf("No partition found");
500 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000501 while(button_get(true) != SYS_USB_CONNECTED) {};
502 }
503
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000504 printf("Loading firmware");
Linus Nielsen Feltzing2a3856f2005-02-07 01:47:47 +0000505 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000506 i = load_firmware();
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000507 printf("Result: %d", i);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000508 lcd_update();
509
Peter D'Hoyec4a59a22006-08-15 22:54:06 +0000510#ifdef HAVE_EEPROM_SETTINGS
Miika Pekkarinene1eb91b2006-08-09 12:04:13 +0000511 if (firmware_settings.initialized)
512 eeprom_settings_store();
513#endif
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000514
Miika Pekkarinen973ee5d2006-09-04 16:06:11 +0000515 if (i == 0)
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000516 start_firmware();
Michael Sevakis9e8fe0e2006-10-30 11:33:38 +0000517
Miika Pekkarinen973ee5d2006-09-04 16:06:11 +0000518 if (detect_flashed_rockbox())
519 {
520 printf("No firmware found on disk");
521 printf("Powering off...");
522 lcd_update();
523 ata_sleep();
524 sleep(HZ*4);
525 power_off();
526 }
527 else
528 start_iriver_fw();
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000529#endif /* IAUDIO_X5 */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000530}
531
532/* These functions are present in the firmware library, but we reimplement
533 them here because the originals do a lot more than we want */
534
535void reset_poweroff_timer(void)
536{
537}
538
539void screen_dump(void)
540{
541}
542
543int dbg_ports(void)
544{
545 return 0;
546}
547
548void mpeg_stop(void)
549{
550}
551
Linus Nielsen Feltzinge244f7f2005-09-14 09:44:45 +0000552void sys_poweroff(void)
553{
554}