Index: apps/main.c
===================================================================
RCS file: /cvsroot/rockbox/apps/main.c,v
retrieving revision 1.181
diff -u -3 -p -r1.181 main.c
--- apps/main.c	5 Aug 2006 20:19:09 -0000	1.181
+++ apps/main.c	6 Aug 2006 18:11:45 -0000
@@ -120,7 +120,9 @@ int init_dircache(void)
 # ifdef HAVE_EEPROM
         if (firmware_settings.initialized && firmware_settings.disk_clean)
         {
-            if (dircache_load(DIRCACHE_FILE) == 0)
+            result = dircache_load(DIRCACHE_FILE);
+            remove(DIRCACHE_FILE);
+            if (result == 0)
                 return 0;
         }
 # endif
Index: apps/tree.c
===================================================================
RCS file: /cvsroot/rockbox/apps/tree.c,v
retrieving revision 1.420
diff -u -3 -p -r1.420 tree.c
--- apps/tree.c	5 Aug 2006 20:19:09 -0000	1.420
+++ apps/tree.c	6 Aug 2006 18:11:46 -0000
@@ -1387,6 +1387,10 @@ void tree_flush(void)
 
 void tree_restore(void)
 {
+#ifdef HAVE_EEPROM
+    firmware_settings.disk_clean = false;
+#endif
+    
 #ifdef HAVE_DIRCACHE
     remove(DIRCACHE_FILE);
     if (global_settings.dircache)
Index: apps/plugins/SOURCES
===================================================================
RCS file: /cvsroot/rockbox/apps/plugins/SOURCES,v
retrieving revision 1.125
diff -u -3 -p -r1.125 SOURCES
--- apps/plugins/SOURCES	19 Jul 2006 20:11:02 -0000	1.125
+++ apps/plugins/SOURCES	6 Aug 2006 18:11:46 -0000
@@ -111,6 +111,7 @@ splitedit.c
 /* Platform-specific plugins */
 #if defined(IRIVER_H100_SERIES) || defined(IRIVER_H300_SERIES)
 iriverify.c
+iriver_flash.c
 #endif
 
 #endif /* iFP7xx */
Index: apps/plugins/iriver_flash.c
===================================================================
RCS file: /cvsroot/rockbox/apps/plugins/iriver_flash.c,v
retrieving revision 1.1
diff -u -3 -p -r1.1 iriver_flash.c
--- apps/plugins/iriver_flash.c	5 Aug 2006 16:11:39 -0000	1.1
+++ apps/plugins/iriver_flash.c	6 Aug 2006 18:11:46 -0000
@@ -381,12 +381,13 @@ void show_fatal_error(void)
 
 int flash_bootloader(const char *filename)
 {
-    // char buf[32];
+    char buf[32];
     int pos, i, len, rc;
     unsigned long checksum, sum, crc32;
     unsigned char *p8;
     uint16_t *p16;
     
+    (void)buf;
     len = load_firmware_file(filename, &checksum);
     if (len < 0)
         return len * 10;
@@ -399,15 +400,16 @@ int flash_bootloader(const char *filenam
     
     /* Verify the crc32 checksum also. */
     crc32 = crc_32(audiobuf, len, 0xffffffff);
-    // rb->snprintf(buf, sizeof buf, "crc32 = 0x%08x", crc32);
-    // rb->splash(HZ*10, true, buf);
-    
-    if (crc32 != 0x5361a679)
+#if 0
+    rb->snprintf(buf, sizeof buf, "crc32 = 0x%08x", crc32);
+    rb->splash(HZ*10, true, buf);
+#else
+    if (crc32 != 0x4b5b54fb)
     {
         rb->splash(HZ*3, true, "Untested bootloader");
         return -2;
     }
-    
+#endif
     rb->lcd_puts(0, 3, "Processing critical sections...");
     rb->lcd_update();
 
Index: apps/plugins/viewers.config
===================================================================
RCS file: /cvsroot/rockbox/apps/plugins/viewers.config,v
retrieving revision 1.32
diff -u -3 -p -r1.32 viewers.config
--- apps/plugins/viewers.config	19 Jul 2006 20:11:02 -0000	1.32
+++ apps/plugins/viewers.config	6 Aug 2006 18:11:46 -0000
@@ -21,4 +21,5 @@ wav,viewers/wav2wv, 00 00 00 00 00 00
 wav,viewers/mp3_encoder, 00 00 00 00 00 00
 wav,viewers/wavplay,60 7F 05 35 3F 00
 bmp,rocks/rockpaint, 01 10 01 10 01 10
+iriver,rocks/iriver_flash,2A 7F 41 41 7F 2A
 
Index: bootloader/main.c
===================================================================
RCS file: /cvsroot/rockbox/bootloader/main.c,v
retrieving revision 1.32
diff -u -3 -p -r1.32 main.c
--- bootloader/main.c	29 Jul 2006 13:31:42 -0000	1.32
+++ bootloader/main.c	6 Aug 2006 18:11:46 -0000
@@ -20,6 +20,8 @@
 
 #include <stdlib.h>
 #include <stdio.h>
+#include "inttypes.h"
+#include "string.h"
 #include "cpu.h"
 #include "system.h"
 #include "lcd.h"
@@ -39,6 +41,7 @@
 #include "power.h"
 #include "file.h"
 #include "uda1380.h"
+#include "eeprom_settings.h"
 
 #include "pcf50606.h"
 
@@ -151,6 +154,19 @@ int load_firmware(void)
     return 0;
 }
 
+int load_flashed_rockbox(void)
+{
+    struct flash_header hdr;
+    unsigned char *buf = (unsigned char *)DRAM_START;
+    uint8_t *src = (uint8_t *)FLASH_ENTRYPOINT;
+    
+    memcpy(&hdr, src, sizeof(struct flash_header));
+    src += sizeof(struct flash_header);
+    memcpy(buf, src, hdr.length);
+
+    return 0;
+}
+
 
 void start_firmware(void)
 {
@@ -312,12 +328,64 @@ void main(void)
 
     sleep(HZ/50); /* Allow the button driver to check the buttons */
 
-    /* Holding REC while starting runs the original firmware */
-    if(((button_status() & BUTTON_REC) == BUTTON_REC) ||
-       ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC)) {
-        printf("Starting original firmware...");
+    firmware_settings.initialized = false;
+    if (detect_flashed_rockbox())
+    {
+        bool rec_button;
+        bool load_from_flash;
+        
+        rec_button = ((button_status() & BUTTON_REC) == BUTTON_REC) ||
+            ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC);
+        
+        load_from_flash = !rec_button;
+#ifdef HAVE_EEPROM
+        if (eeprom_settings_init())
+        {
+            /* If bootloader version has not been reset, disk might
+             * not be intact. */
+            if (firmware_settings.bl_version)
+                firmware_settings.disk_clean = false;
+            
+            firmware_settings.bl_version = 7;
+            /* Invert the record button if we want to load from disk
+             * by default. */
+            if (firmware_settings.boot_disk)
+                load_from_flash = rec_button;
+        }
+#endif
+        
+        if (load_from_flash)
+        {
+            /* Load firmware from flash */
+            i = load_flashed_rockbox();
+            printf("Result: %d", i);
+            lcd_update();
+            if (i == 0)
+            {
+#ifdef HAVE_EEPROM
+                eeprom_settings_store();
+#endif
+                start_firmware();
+                printf("Fatal: Corrupted firmware");
+                printf("Hold down REC on next boot");
+                lcd_update();
+                sleep(HZ*2);
+                power_off();
+            }
+        }
+        
+        printf("Loading from disk...");
         lcd_update();
-        start_iriver_fw();
+    }
+    else
+    {
+        /* Holding REC while starting runs the original firmware */
+        if(((button_status() & BUTTON_REC) == BUTTON_REC) ||
+           ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC)) {
+            printf("Starting original firmware...");
+            lcd_update();
+            start_iriver_fw();
+        }
     }
 
     /* Don't start if the Hold button is active on the device you
@@ -384,6 +452,13 @@ void main(void)
         sleep(HZ);
 #endif
     
+#ifdef HAVE_EEPROM
+        if (firmware_settings.initialized)
+        {
+            firmware_settings.disk_clean = false;
+            eeprom_settings_store();
+        }
+#endif
         ata_spin();
         ata_enable(false);
         usb_enable(true);
@@ -423,6 +498,11 @@ void main(void)
     printf("Result: %d", i);
     lcd_update();
 
+#ifdef HAVE_EEPROM
+    if (firmware_settings.initialized)
+        eeprom_settings_store();
+#endif
+    
     if(i == 0)
         start_firmware();
     
Index: firmware/eeprom_settings.c
===================================================================
RCS file: /cvsroot/rockbox/firmware/eeprom_settings.c,v
retrieving revision 1.1
diff -u -3 -p -r1.1 eeprom_settings.c
--- firmware/eeprom_settings.c	5 Aug 2006 20:19:10 -0000	1.1
+++ firmware/eeprom_settings.c	6 Aug 2006 18:11:46 -0000
@@ -21,6 +21,7 @@
 #include "eeprom_24cxx.h"
 #include "crc32.h"
 
+#include "system.h"
 #include "string.h"
 #include "logf.h"
 
@@ -64,16 +65,16 @@ bool eeprom_settings_init(void)
     sum = crc_32(&firmware_settings, sizeof(struct eeprom_settings)-4,
                  0xffffffff);
     
-    if (firmware_settings.checksum != sum)
+    if (firmware_settings.version != EEPROM_SETTINGS_VERSION)
     {
-        logf("Checksum mismatch");
+        logf("Version mismatch");
         reset_config();
         return true;
     }
     
-    if (firmware_settings.version != EEPROM_SETTINGS_VERSION)
+    if (firmware_settings.checksum != sum)
     {
-        logf("Version mismatch");
+        logf("Checksum mismatch");
         reset_config();
         return true;
     }
Index: firmware/export/eeprom_settings.h
===================================================================
RCS file: /cvsroot/rockbox/firmware/export/eeprom_settings.h,v
retrieving revision 1.1
diff -u -3 -p -r1.1 eeprom_settings.h
--- firmware/export/eeprom_settings.h	5 Aug 2006 20:19:10 -0000	1.1
+++ firmware/export/eeprom_settings.h	6 Aug 2006 18:11:46 -0000
@@ -40,7 +40,6 @@ struct eeprom_settings
 
 extern struct eeprom_settings firmware_settings;
 
-bool detect_flashed_rockbox(void);
 bool eeprom_settings_init(void);
 bool eeprom_settings_store(void);
 
