summaryrefslogtreecommitdiff
path: root/bootloader/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'bootloader/main.c')
-rw-r--r--bootloader/main.c95
1 files changed, 90 insertions, 5 deletions
diff --git a/bootloader/main.c b/bootloader/main.c
index 181dbeade3..0ef6d8944e 100644
--- a/bootloader/main.c
+++ b/bootloader/main.c
@@ -20,6 +20,8 @@
20 20
21#include <stdlib.h> 21#include <stdlib.h>
22#include <stdio.h> 22#include <stdio.h>
23#include "inttypes.h"
24#include "string.h"
23#include "cpu.h" 25#include "cpu.h"
24#include "system.h" 26#include "system.h"
25#include "lcd.h" 27#include "lcd.h"
@@ -39,6 +41,7 @@
39#include "power.h" 41#include "power.h"
40#include "file.h" 42#include "file.h"
41#include "uda1380.h" 43#include "uda1380.h"
44#include "eeprom_settings.h"
42 45
43#include "pcf50606.h" 46#include "pcf50606.h"
44 47
@@ -151,6 +154,21 @@ int load_firmware(void)
151 return 0; 154 return 0;
152} 155}
153 156
157int 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;
162
163 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
154 172
155void start_firmware(void) 173void start_firmware(void)
156{ 174{
@@ -171,12 +189,14 @@ void main(void)
171 int rc; 189 int rc;
172 bool rc_on_button = false; 190 bool rc_on_button = false;
173 bool on_button = false; 191 bool on_button = false;
192 bool rec_button = false;
174 int data; 193 int data;
175 int adc_battery, battery_voltage, batt_int, batt_frac; 194 int adc_battery, battery_voltage, batt_int, batt_frac;
176 195
177#ifdef IAUDIO_X5 196#ifdef IAUDIO_X5
178 (void)rc_on_button; 197 (void)rc_on_button;
179 (void)on_button; 198 (void)on_button;
199 (void)rec_button;
180 (void)data; 200 (void)data;
181 power_init(); 201 power_init();
182 202
@@ -311,13 +331,66 @@ void main(void)
311 lcd_update(); 331 lcd_update();
312 332
313 sleep(HZ/50); /* Allow the button driver to check the buttons */ 333 sleep(HZ/50); /* Allow the button driver to check the buttons */
334 rec_button = ((button_status() & BUTTON_REC) == BUTTON_REC)
335 || ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC);
336
314 337
315 /* Holding REC while starting runs the original firmware */ 338#ifdef HAVE_EEPROM
316 if(((button_status() & BUTTON_REC) == BUTTON_REC) || 339 firmware_settings.initialized = false;
317 ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC)) { 340#endif
318 printf("Starting original firmware..."); 341 if (detect_flashed_rockbox())
342 {
343 bool load_from_flash;
344
345 load_from_flash = !rec_button;
346#ifdef HAVE_EEPROM
347 if (eeprom_settings_init())
348 {
349 /* If bootloader version has not been reset, disk might
350 * not be intact. */
351 if (firmware_settings.bl_version)
352 firmware_settings.disk_clean = false;
353
354 firmware_settings.bl_version = 7;
355 /* Invert the record button if we want to load from disk
356 * by default. */
357 if (firmware_settings.boot_disk)
358 load_from_flash = rec_button;
359 }
360#endif
361
362 if (load_from_flash)
363 {
364 /* Load firmware from flash */
365 i = load_flashed_rockbox();
366 printf("Result: %d", i);
367 lcd_update();
368 if (i == 0)
369 {
370#ifdef HAVE_EEPROM
371 eeprom_settings_store();
372#endif
373 start_firmware();
374 printf("Fatal: Corrupted firmware");
375 printf("Hold down REC on next boot");
376 lcd_update();
377 sleep(HZ*2);
378 power_off();
379 }
380 }
381
382 printf("Loading from disk...");
319 lcd_update(); 383 lcd_update();
320 start_iriver_fw(); 384 }
385 else
386 {
387 /* Holding REC while starting runs the original firmware */
388 if (rec_button)
389 {
390 printf("Starting original firmware...");
391 lcd_update();
392 start_iriver_fw();
393 }
321 } 394 }
322 395
323 /* Don't start if the Hold button is active on the device you 396 /* Don't start if the Hold button is active on the device you
@@ -384,6 +457,13 @@ void main(void)
384 sleep(HZ); 457 sleep(HZ);
385#endif 458#endif
386 459
460#ifdef HAVE_EEPROM
461 if (firmware_settings.initialized)
462 {
463 firmware_settings.disk_clean = false;
464 eeprom_settings_store();
465 }
466#endif
387 ata_spin(); 467 ata_spin();
388 ata_enable(false); 468 ata_enable(false);
389 usb_enable(true); 469 usb_enable(true);
@@ -423,6 +503,11 @@ void main(void)
423 printf("Result: %d", i); 503 printf("Result: %d", i);
424 lcd_update(); 504 lcd_update();
425 505
506#ifdef HAVE_EEPROM
507 if (firmware_settings.initialized)
508 eeprom_settings_store();
509#endif
510
426 if(i == 0) 511 if(i == 0)
427 start_firmware(); 512 start_firmware();
428 513