Index: bootloader/main-pp.c
===================================================================
--- bootloader/main-pp.c	(revision 12077)
+++ bootloader/main-pp.c	(working copy)
@@ -19,156 +19,27 @@
  * KIND, either express or implied.
  *
  ****************************************************************************/
-#include "config.h"
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdarg.h>
+#include "common.h"
 #include "cpu.h"
+#include "file.h"
 #include "system.h"
-#include "lcd.h"
 #include "kernel.h"
-#include "thread.h"
-#include "ata.h"
-#include "fat.h"
-#include "disk.h"
+#include "lcd.h"
 #include "font.h"
-#include "adc.h"
-#include "backlight.h"
+#include "ata.h"
 #include "button.h"
-#include "panic.h"
+#include "disk.h"
 #include "power.h"
-#include "file.h"
 
-/* Size of the buffer to store the loaded firmware image */
+/* Maximum allowed firmware image size. 10MB is more than enough */
 #define MAX_LOADSIZE (10*1024*1024)
 
-/* A buffer to load the iriver firmware or Rockbox into */
-unsigned char loadbuffer[MAX_LOADSIZE];
+/* A buffer to load the original firmware or Rockbox into */
+unsigned char *loadbuffer = (unsigned char *)DRAM_START;
 
+/* Bootloader version */
 char version[] = APPSVERSION;
 
-#define DRAM_START              0x10000000
-
-int line=0;
-char printfbuf[256];
-
-void reset_screen(void)
-{
-    lcd_clear_display();
-    line = 0;
-}
-
-void printf(const char *format, ...)
-{
-    int len;
-    unsigned char *ptr;
-    va_list ap;
-    va_start(ap, format);
-
-    
-    ptr = printfbuf;
-    len = vsnprintf(ptr, sizeof(printfbuf), format, ap);
-    va_end(ap);
-
-    lcd_puts(0, line++, ptr);
-    lcd_update();
-    if(line >= (LCD_HEIGHT/SYSFONT_HEIGHT))
-        line = 0;
-}
-
-/* Load original mi4 firmware. This function expects a file called
-   "/System/OF.bin" on the player. It should be a mi4 firmware decrypted 
-   and header stripped using mi4code. It reads the file in to a memory
-   buffer called buf. The rest of the loading is done in main() and crt0.S
-*/
-int load_original_firmware(unsigned char* buf)
-{
-    int fd;
-    int rc;
-    int len;
-    
-    fd = open("/System/OF.bin", O_RDONLY);
-
-    len = filesize(fd);
-    
-    if (len > MAX_LOADSIZE)
-        return -6;
-
-    rc = read(fd, buf, len);
-    if(rc < len)
-        return -4;
-
-    close(fd);
-    return len;
-}
-
-/* Load Rockbox firmware (rockbox.*) */
-int load_rockbox(unsigned char* buf)
-{
-    int fd;
-    int rc;
-    int len;
-    unsigned long chksum;
-    char model[5];
-    unsigned long sum;
-    int i;
-    
-    fd = open("/.rockbox/" BOOTFILE, O_RDONLY);
-    if(fd < 0)
-    {
-        fd = open("/" BOOTFILE, O_RDONLY);
-        if(fd < 0)
-            return -1;
-    }
-
-    len = filesize(fd) - 8;
-
-    printf("Length: %x", len);
-    
-    if (len > MAX_LOADSIZE)
-        return -6;
-
-    lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
-    
-    rc = read(fd, &chksum, 4);
-    chksum=betoh32(chksum); /* Rockbox checksums are big-endian */
-    if(rc < 4)
-        return -2;
-
-    printf("Checksum: %x", chksum);
-
-    rc = read(fd, model, 4);
-    if(rc < 4)
-        return -3;
-
-    model[4] = 0;
-    
-    printf("Model name: %s", model);
-
-    lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
-
-    rc = read(fd, buf, len);
-    if(rc < len)
-        return -4;
-
-    close(fd);
-
-    sum = MODEL_NUMBER;
-    
-    for(i = 0;i < len;i++) {
-        sum += buf[i];
-    }
-
-    printf("Sum: %x", sum);
-    
-    if(sum != chksum)
-        return -5;
-
-    return len;
-}
-
 void* main(void)
 {
     char buf[256];
@@ -183,8 +54,6 @@
     font_init();
     button_init();
 
-    line=0;
-
     lcd_setfont(FONT_SYSFIXED);
 
     printf("Rockbox boot loader");
@@ -193,18 +62,20 @@
 
     i=ata_init();
     if (i==0) {
-      identify_info=ata_get_identify();
-      /* Show model */
-      for (i=0; i < 20; i++) {
-        ((unsigned short*)buf)[i]=htobe16(identify_info[i+27]);
-      }
-      buf[40]=0;
-      for (i=39; i && buf[i]==' '; i--) {
-        buf[i]=0;
-      }
-      printf(buf);
+        identify_info=ata_get_identify();
+        /* Show model */
+        for (i=0; i < 20; i++) {
+            ((unsigned short*)buf)[i]=htobe16(identify_info[i+27]);
+        }
+        buf[40]=0;
+        for (i=39; i && buf[i]==' '; i--) {
+            buf[i]=0;
+        }
+        printf(buf);
     } else {
-      printf("ATA: %d", i);
+        printf("ATA error: %d", i);
+        sleep(HZ*5);
+        power_off();
     }
 
     disk_init();
@@ -212,6 +83,8 @@
     if (rc<=0)
     {
         printf("No partition found");
+        sleep(HZ*5);
+        power_off();
     }
 
     pinfo = disk_partinfo(0);
@@ -220,39 +93,37 @@
     i=button_read_device();
     if(i==BUTTON_LEFT)
     {
+        /* Load original mi4 firmware. This expects a file called 
+           "/System/OF.bin" on the player. It should be a mi4 firmware decrypted 
+           and header stripped using mi4code. It reads the file in to a memory
+           buffer called loadbuffer. The rest of the loading is done in crt0.S
+        */
         printf("Loading original firmware...");
-        rc=load_original_firmware(loadbuffer);
+        rc=load_raw_firmware(loadbuffer, "/System/OF.bin", MAX_LOADSIZE);
+        if (rc < EOK) {
+            printf("Error!");
+            printf("Can't load /System/OF.bin:");
+            printf(strerror(rc));
+            sleep(HZ*5);
+            power_off();
+        }
     } else {
         printf("Loading Rockbox...");
-        rc=load_rockbox(loadbuffer);
+        rc=load_firmware(loadbuffer, BOOTFILE, MAX_LOADSIZE);
+        if (rc < EOK) {
+            printf("Error!");
+            printf("Can't load %s:", BOOTFILE);
+            printf(strerror(rc));
+            sleep(HZ*5);
+            power_off();
+        }
     }
-
-    if (rc < 0) {
-            printf("Rockbox error: %d",rc);
-            while(1) {}
-    }
     
-    memcpy((void*)DRAM_START,loadbuffer,rc);
-    
-    return (void*)DRAM_START;
+    return (void*)loadbuffer;
 }
 
 /* These functions are present in the firmware library, but we reimplement
    them here because the originals do a lot more than we want */
-
-void reset_poweroff_timer(void)
-{
-}
-
-int dbg_ports(void)
-{
-   return 0;
-}
-
-void mpeg_stop(void)
-{
-}
-
 void usb_acknowledge(void)
 {
 }
@@ -260,7 +131,3 @@
 void usb_wait_for_disconnect(void)
 {
 }
-
-void sys_poweroff(void)
-{
-}
Index: bootloader/SOURCES
===================================================================
--- bootloader/SOURCES	(revision 12077)
+++ bootloader/SOURCES	(working copy)
@@ -1,3 +1,5 @@
+common.c
+
 #if defined(IPOD_ARCH)
 ipod.c
 #elif defined(GIGABEAT_F)
Index: bootloader/tpj1022.c
===================================================================
--- bootloader/tpj1022.c	(revision 12077)
+++ bootloader/tpj1022.c	(working copy)
@@ -37,11 +37,10 @@
 #include "panic.h"
 #include "power.h"
 #include "file.h"
+#include "common.h"
 
 char version[] = APPSVERSION;
 
-int line=0;
-
 void* main(void)
 {
     int i;
@@ -54,8 +53,7 @@
     lcd_init();
     font_init();
 
-    lcd_puts(0, line++ ,"Hello World!");
-    lcd_update();
+    printf("Hello World!");
 #endif
 
     i=ata_init();
@@ -108,20 +106,6 @@
 
 /* These functions are present in the firmware library, but we reimplement
    them here because the originals do a lot more than we want */
-
-void reset_poweroff_timer(void)
-{
-}
-
-int dbg_ports(void)
-{
-   return 0;
-}
-
-void mpeg_stop(void)
-{
-}
-
 void usb_acknowledge(void)
 {
 }
@@ -129,7 +113,3 @@
 void usb_wait_for_disconnect(void)
 {
 }
-
-void sys_poweroff(void)
-{
-}
Index: bootloader/gigabeat.c
===================================================================
--- bootloader/gigabeat.c	(revision 12077)
+++ bootloader/gigabeat.c	(working copy)
@@ -18,11 +18,10 @@
 #include "power.h"
 #include "file.h"
 #include "button-target.h"
+#include "common.h"
 
 extern void map_memory(void);
 
-int line = 0;
-
 char version[] = APPSVERSION;
 
 static void go_usb_mode(void)
@@ -55,30 +54,25 @@
     int orig_file = 0, dest_file = 0;
     int size = 0, size_read;
     static char buf[4096];
-    char lcd_buf[64];
-
+    
     orig_file = open("/GBSYSTEM/FWIMG/FWIMG01.DAT.ORIG", O_RDONLY);
     if(orig_file < 0) {
         /* Couldn't open source file */
-        lcd_puts(0, line++, "Couldn't open FWIMG01.DAT.ORIG for reading");
-        lcd_update();
+        printf("Couldn't open FWIMG01.DAT.ORIG for reading");
         return(1);
     }
 
-    lcd_puts(0, line++, "FWIMG01.DAT.ORIG opened for reading");
-    lcd_update();
+    printf("FWIMG01.DAT.ORIG opened for reading");
 
     dest_file = open("/GBSYSTEM/FWIMG/FWIMG01.DAT", O_RDWR);
     if(dest_file < 0) {
         /* Couldn't open destination file */
-        lcd_puts(0, line++, "Couldn't open FWIMG01.DAT.ORIG for writing");
-        lcd_update();
+        printf("Couldn't open FWIMG01.DAT.ORIG for writing");
         close(orig_file);
         return(2);
     }
 
-    lcd_puts(0, line++, "FWIMG01.DAT opened for writing");
-    lcd_update();
+    printf("FWIMG01.DAT opened for writing");
 
     do {
         /* Copy in chunks */
@@ -95,65 +89,22 @@
     close(orig_file);
     close(dest_file);
 
-    snprintf(lcd_buf, sizeof(lcd_buf), "Finished copying %ld bytes from", size);
-    lcd_puts(0, line++, lcd_buf);
-    lcd_puts(0, line++, "FWIMG01.DAT.ORIG to FWIMG01.DAT");
+    printf("Finished copying %ld bytes from", size);
+    printf("FWIMG01.DAT.ORIG to FWIMG01.DAT");
 
     return(0);
 }
 
-
-int load_rockbox(const char* file_name, unsigned char* buf, int buffer_size)
-{
-    int fd;
-    int rc;
-    int len;
-    char str[256];
-
-    fd = open("/.rockbox/" BOOTFILE, O_RDONLY);
-    if(fd < 0) {
-        fd = open("/" BOOTFILE, O_RDONLY);
-        if(fd < 0)
-            return -1;
-    }
-    fd = open(file_name, O_RDONLY);
-    if(fd < 0)
-        return -2;
-
-    len = filesize(fd);
-
-    if(len > buffer_size) {
-        snprintf(str, sizeof(str), "len: %d buf: %d", len, buffer_size);
-        lcd_puts(0, line++, str);
-        lcd_update();
-        return -6;
-    }
-
-    rc = read(fd, buf, len);
-    if(rc < len) {
-        snprintf(str, sizeof(str), "len: %d rc: %d", len, rc);
-        lcd_puts(0, line++, str);
-        lcd_update();
-        return -4;
-    }
-
-    close(fd);
-
-    return len;
-}
-
 char buf[256];
 
 void display_instructions(void)
 {
     lcd_setfont(FONT_SYSFIXED);
-    lcd_puts(0, line++, "Hold MENU when booting for rescue mode.");
-    lcd_puts(0, line++, "  \"VOL+\" button to restore original kernel");
-    lcd_puts(0, line++, "  \"A\" button to load original firmware");
-    line++;
-    snprintf(buf, sizeof(buf), "FRAME %x TTB %x", FRAME, TTB_BASE);
-    lcd_puts(0, line++, buf);
-    lcd_update();
+    printf("Hold MENU when booting for rescue mode.");
+    printf("  \"VOL+\" button to restore original kernel");
+    printf("  \"A\" button to load original firmware");
+    printf("");
+    printf("FRAME %x TTB %x", FRAME, TTB_BASE);
 }
 
 void * main(void)
@@ -179,23 +130,20 @@
     }
     if(GPGDAT & 2) {
         lcd_init();
-        lcd_puts(0, line++, "Entering rescue mode..");
-        lcd_update();
+        printf("Entering rescue mode..");
         go_usb_mode();
         while(1);
     }
     if(GPGDAT & 0x10) {
         lcd_init();
         load_original = true;
-        lcd_puts(0, line++, "Loading original firmware...");
-        lcd_update();
+        printf("Loading original firmware...");
     }
 
     i = ata_init();
     i = disk_mount_all();
     if(!show_bootsplash) {
-        snprintf(buf, sizeof(buf), "disk_mount_all: %d", i);
-        lcd_puts(0, line++, buf);
+        printf("disk_mount_all: %d", i);
     }
     if(show_bootsplash) {
         int fd = open("/bootsplash.raw", O_RDONLY);
@@ -216,17 +164,15 @@
     if(GPGDAT & 4) {
 
         /* Try to restore the original kernel/bootloader if a copy is found */
-        lcd_puts(0, line++, "Restoring FWIMG01.DAT...");
-        lcd_update();
+        printf("Restoring FWIMG01.DAT...");
 
         if(!restore_fwimg01dat()) {
-            lcd_puts(0, line++, "Restoring FWIMG01.DAT successful.");
+            printf("Restoring FWIMG01.DAT successful.");
         } else {
-            lcd_puts(0, line++, "Restoring FWIMG01.DAT failed.");
+            printf("Restoring FWIMG01.DAT failed.");
         }
 
-        lcd_puts(0, line++, "Now power cycle to boot original");
-        lcd_update();
+        printf("Now power cycle to boot original");
         while(1);
     }
 
@@ -242,76 +188,61 @@
         for(i=39; i && buf[i]==' '; i--)
             buf[i] = 0;
 
-        lcd_puts(0, line++, "Model");
-        lcd_puts(0, line++, buf);
+        printf("Model");
+        printf(buf);
 
         for(i=0; i < 4; i++)
             ((unsigned short*)buf)[i]=htobe16(identify_info[i+23]);
 
         buf[8]=0;
 
-        lcd_puts(0, line++, "Firmware");
-        lcd_puts(0, line++, buf);
+        printf("Firmware");
+        printf(buf);
 
         pinfo = disk_partinfo(0);
-        snprintf(buf, sizeof(buf), "Partition 0: 0x%02x %ld MB",
-                 pinfo->type, pinfo->size / 2048);
-        lcd_puts(0, line++, buf);
-        lcd_update();
+        printf("Partition 0: 0x%02x %ld MB", pinfo->type, pinfo->size / 2048);
     }
     /* Load original firmware */
     if(load_original) {
         loadbuffer = (unsigned char*)0x30008000;
         buffer_size =(unsigned char*)0x31000000 - loadbuffer;
-        rc = load_rockbox("/rockbox.gigabeat", loadbuffer, buffer_size);
-        if(rc < 0) {
-            lcd_puts(0, line++, "failed to load original firmware. Loading rockbox");
-            lcd_update();
+        rc = load_firmware("/rockbox.gigabeat", loadbuffer, buffer_size);
+        if(rc < EOK) {
+            printf("failed to load original firmware. Loading rockbox");
             sleep(2*HZ);
             goto load_rockbox;
         }
 
-        snprintf(buf, sizeof(buf), "Loaded: %d", rc);
-        lcd_puts(0, line++, buf);
-        lcd_update();
+        printf("Loaded: %d", rc);
         sleep(2*HZ);
 
         (*((int*)0x7000000)) = 333;
         rc = *((int*)0x7000000+0x8000000);
-        snprintf(buf, sizeof(buf), "Bank0 mem test: %d", rc);
-        lcd_puts(0, line++, buf);
-        lcd_update();
+        printf("Bank0 mem test: %d", rc);
         sleep(3*HZ);
 
-        lcd_puts(0, line++, "Woops, should not return from firmware!");
-        lcd_update();
+        printf("Woops, should not return from firmware!");
         goto usb;
     }
 
 load_rockbox:
     map_memory();
     if(!show_bootsplash) {
-        lcd_puts(0, line, "Loading Rockbox...");
-        lcd_update();
+        printf("Loading Rockbox...");
     }
 
     loadbuffer = (unsigned char*) 0x100;
     buffer_size = (unsigned char*)0x400000 - loadbuffer;
-    rc=load_rockbox("/rockbox.gigabeat", loadbuffer, buffer_size);
-    if(rc < 0) {
-        snprintf(buf, sizeof(buf), "Rockbox error: %d",rc);
-        lcd_puts(0, line++, buf);
-        lcd_update();
+    rc=load_firmware("/rockbox.gigabeat", loadbuffer, buffer_size);
+    if(rc < EOK) {
+        printf("Rockbox error: %d",rc);
     } else {
         if(!show_bootsplash) {
-            lcd_puts(0, line++, "Rockbox loaded.");
-            lcd_update();
+            printf("Rockbox loaded.");
         }
         kernel_entry = (void*) loadbuffer;
         rc = kernel_entry();
-        snprintf(buf, sizeof(buf), "Woops, should not return from firmware: %d", rc);
-        lcd_puts(0, line++, buf);
-        lcd_update();
+        printf("Woops, should not return from firmware: %d", rc);
         goto usb;
     }
 usb:
Index: bootloader/main.c
===================================================================
--- bootloader/main.c	(revision 12077)
+++ bootloader/main.c	(working copy)
@@ -43,20 +43,19 @@
 #include "eeprom_settings.h"
 
 #include "pcf50606.h"
+#include "common.h"
 
 #include <stdarg.h>
 
+/* Maximum allowed firmware image size. 10MB is more than enough */
+#define MAX_LOADSIZE (10*1024*1024)
+
 #define DRAM_START 0x31000000
 
 #ifdef HAVE_EEPROM_SETTINGS
 static bool recovery_mode = false;
 #endif
 
-int line = 0;
-#ifdef HAVE_REMOTE_LCD
-int remote_line = 0;
-#endif
-
 int usb_screen(void)
 {
    return 0;
@@ -64,41 +63,6 @@
 
 char version[] = APPSVERSION;
 
-char printfbuf[256];
-
-void reset_screen(void)
-{
-    lcd_clear_display();
-    line = 0;
-#ifdef HAVE_REMOTE_LCD
-    lcd_remote_clear_display();
-    remote_line = 0;
-#endif
-}
-
-void printf(const char *format, ...)
-{
-    int len;
-    unsigned char *ptr;
-    va_list ap;
-    va_start(ap, format);
-
-    ptr = printfbuf;
-    len = vsnprintf(ptr, sizeof(printfbuf), format, ap);
-    va_end(ap);
-
-    lcd_puts(0, line++, ptr);
-    lcd_update();
-    if(line >= 16)
-        line = 0;
-#ifdef HAVE_REMOTE_LCD
-    lcd_remote_puts(0, remote_line++, ptr);
-    lcd_remote_update();
-    if(remote_line >= 8)
-        remote_line = 0;
-#endif
-}
-
 /* Reset the cookie for the crt0 crash check */
 inline void __reset_cookie(void)
 {
@@ -116,68 +80,6 @@
     asm(" jmp (%a0)");
 }
 
-int load_firmware(void)
-{
-    int fd;
-    int rc;
-    int len;
-    unsigned long chksum;
-    char model[5];
-    unsigned long sum;
-    int i;
-    unsigned char *buf = (unsigned char *)DRAM_START;
-
-    fd = open("/.rockbox/" BOOTFILE, O_RDONLY);
-    if(fd < 0)
-    {
-        fd = open("/" BOOTFILE, O_RDONLY);
-        if(fd < 0)
-            return -1;
-    }
-
-    len = filesize(fd) - 8;
-
-    printf("Length: %x", len);
-
-    lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
-
-    rc = read(fd, &chksum, 4);
-    if(rc < 4)
-        return -2;
-
-    printf("Checksum: %x", chksum);
-
-    rc = read(fd, model, 4);
-    if(rc < 4)
-        return -3;
-
-    model[4] = 0;
-
-    printf("Model name: %s", model);
-    lcd_update();
-
-    lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
-
-    rc = read(fd, buf, len);
-    if(rc < len)
-        return -4;
-
-    close(fd);
-
-    sum = MODEL_NUMBER;
-
-    for(i = 0;i < len;i++) {
-        sum += buf[i];
-    }
-
-    printf("Sum: %x", sum);
-
-    if(sum != chksum)
-        return -5;
-
-    return 0;
-}
-
 void start_firmware(void)
 {
     asm(" move.w #0x2700,%sr");
@@ -353,6 +255,7 @@
     int defopt = -1;
     char buf[32];
     int i;
+    extern int line;
     
     reset_screen();
     printf("Bootloader %s", version);
@@ -516,8 +419,8 @@
     }
 
     printf("Loading firmware");
-    i = load_firmware();
-    printf("Result: %d", i);
+    i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE);
+    printf("Result: %s", strerror(i));
 
     if(i == 0)
         start_firmware();
@@ -525,6 +428,7 @@
     power_off();
 
 #else
+    extern int line;
     /* We want to read the buttons as early as possible, before the user
        releases the ON button */
 
@@ -721,8 +625,8 @@
     }
 
     printf("Loading firmware");
-    i = load_firmware();
-    printf("Result: %d", i);
+    i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE);
+    printf("Result: %d", strerror(i));
 
     if (i == 0)
         start_firmware();
@@ -739,24 +643,6 @@
 
 /* These functions are present in the firmware library, but we reimplement
    them here because the originals do a lot more than we want */
-
-void reset_poweroff_timer(void)
-{
-}
-
 void screen_dump(void)
 {
 }
-
-int dbg_ports(void)
-{
-   return 0;
-}
-
-void mpeg_stop(void)
-{
-}
-
-void sys_poweroff(void)
-{
-}
Index: bootloader/common.c
===================================================================
--- bootloader/common.c	(revision 0)
+++ bootloader/common.c	(revision 0)
@@ -0,0 +1,205 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id: main.c 11997 2007-01-13 09:08:18Z miipekk $
+ *
+ * Copyright (C) 2005 by Linus Nielsen Feltzing
+ *
+ * All files in this archive are subject to the GNU General Public License.
+ * See the file COPYING in the source tree root for full license agreement.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+#include "lcd.h"
+#include "lcd-remote.h"
+#include "font.h"
+#include "system.h"
+#include <stdarg.h>
+#include <stdio.h>
+#include "cpu.h"
+#include "common.h"
+
+int line = 0;
+#ifdef HAVE_REMOTE_LCD
+int remote_line = 0;
+#endif
+
+char printfbuf[256];
+
+void reset_screen(void)
+{
+    lcd_clear_display();
+    line = 0;
+#ifdef HAVE_REMOTE_LCD
+    lcd_remote_clear_display();
+    remote_line = 0;
+#endif
+}
+
+void printf(const char *format, ...)
+{
+    int len;
+    unsigned char *ptr;
+    va_list ap;
+    va_start(ap, format);
+
+    ptr = printfbuf;
+    len = vsnprintf(ptr, sizeof(printfbuf), format, ap);
+    va_end(ap);
+
+    lcd_puts(0, line++, ptr);
+    lcd_update();
+    if(line >= LCD_HEIGHT/SYSFONT_HEIGHT)
+        line = 0;
+#ifdef HAVE_REMOTE_LCD
+    lcd_remote_puts(0, remote_line++, ptr);
+    lcd_remote_update();
+    if(remote_line >= LCD_REMOTE_HEIGHT/SYSFONT_HEIGHT)
+        remote_line = 0;
+#endif
+}
+
+char *strerror(int error)
+{
+    switch(error)
+    {
+    case EOK:
+        return "OK";
+    case EFILE_NOT_FOUND:
+        return "File not found";
+    case EREAD_CHKSUM_FAILED:
+        return "Read failed (chksum)";
+    case EREAD_MODEL_FAILED:
+        return "Read failed (model)";
+    case EREAD_IMAGE_FAILED:
+        return "Read failed (image)";
+    case EBAD_CHKSUM:
+        return "Bad checksum";
+    case EFILE_TOO_BIG:
+        return "File too big";
+    default:
+        return "Unknown";
+    }
+}
+
+/* Load firmware image in a format created by tools/scramble */
+int load_firmware(unsigned char* buf, char* firmware, int buffer_size)
+{
+    int fd;
+    int rc;
+    int len;
+    unsigned long chksum;
+    char model[5];
+    unsigned long sum;
+    int i;
+    char filename[MAX_PATH];
+
+    snprintf(filename,sizeof(filename),"/.rockbox/%s",firmware);
+    fd = open(filename, O_RDONLY);
+    if(fd < 0)
+    {
+        snprintf(filename,sizeof(filename),"/%s",firmware);
+        fd = open(filename, O_RDONLY);
+        if(fd < 0)
+            return EFILE_NOT_FOUND;
+    }
+
+    len = filesize(fd) - 8;
+
+    printf("Length: %x", len);
+
+    if (len > buffer_size)
+        return EFILE_TOO_BIG;
+
+    lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
+
+    rc = read(fd, &chksum, 4);
+    chksum=betoh32(chksum); /* Rockbox checksums are big-endian */
+    if(rc < 4)
+        return EREAD_CHKSUM_FAILED;
+
+    printf("Checksum: %x", chksum);
+
+    rc = read(fd, model, 4);
+    if(rc < 4)
+        return EREAD_MODEL_FAILED;
+
+    model[4] = 0;
+
+    printf("Model name: %s", model);
+    printf("Loading %s", firmware);
+
+    lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
+
+    rc = read(fd, buf, len);
+    if(rc < len)
+        return EREAD_IMAGE_FAILED;
+
+    close(fd);
+
+    sum = MODEL_NUMBER;
+
+    for(i = 0;i < len;i++) {
+        sum += buf[i];
+    }
+
+    printf("Sum: %x", sum);
+
+    if(sum != chksum)
+        return EBAD_CHKSUM;
+
+    return len;
+}
+
+/* Load raw binary image. */
+int load_raw_firmware(unsigned char* buf, char* firmware, int buffer_size)
+{
+    int fd;
+    int rc;
+    int len;
+    char filename[MAX_PATH];
+
+    snprintf(filename,sizeof(filename),"%s",firmware);
+    fd = open(filename, O_RDONLY);
+    if(fd < 0)
+    {
+        return EFILE_NOT_FOUND;
+    }
+
+    len = filesize(fd);
+    
+    if (len > buffer_size)
+        return EFILE_TOO_BIG;
+
+    rc = read(fd, buf, len);
+    if(rc < len)
+        return EREAD_IMAGE_FAILED;
+
+    close(fd);
+    return len;
+}
+
+/* These functions are present in the firmware library, but we reimplement
+   them here because the originals do a lot more than we want */
+void reset_poweroff_timer(void)
+{
+}
+
+int dbg_ports(void)
+{
+   return 0;
+}
+
+void mpeg_stop(void)
+{
+}
+
+void sys_poweroff(void)
+{
+}
Index: bootloader/ipod.c
===================================================================
--- bootloader/ipod.c	(revision 12077)
+++ bootloader/ipod.c	(working copy)
@@ -39,16 +39,22 @@
 #include "panic.h"
 #include "power.h"
 #include "file.h"
+#include "common.h"
 
 #define XSC(X) #X
 #define SC(X) XSC(X)
 
-#if (CONFIG_CPU == PP5020)
-#define DRAM_START              0x10000000
-#else
-#define IPOD_LCD_BASE           0xc0001000
-#define DRAM_START              0x28000000
-#endif
+/* Maximum allowed firmware image size. The largest known current 
+   (December 2006) firmware is about 7.5MB (Apple's firmware for the ipod video)
+   so we set this to 8MB. */
+#define MAX_LOADSIZE (8*1024*1024)
+
+/* A buffer to load the Linux kernel or Rockbox into */
+unsigned char *loadbuffer = (unsigned char *)DRAM_START;
+
+/* Bootloader version */
+char version[] = APPSVERSION;
+
 #define IPOD_HW_REVISION (*((volatile unsigned long*)(0x00002084)))
 
 /* We copy the hardware revision to the last four bytes of SDRAM and then
@@ -61,17 +67,6 @@
 #define BUTTON_PLAY  4
 #define BUTTON_HOLD  5
 
-/* Size of the buffer to store the loaded Rockbox/Linux/AppleOS image */
-
-/* The largest known current (December 2006) firmware is about 7.5MB
-   (Apple's firmware for the ipod video) so we set this to 8MB. */
-
-#define MAX_LOADSIZE (8*1024*1024)
-
-char version[] = APPSVERSION;
-
-int line=0;
-
 #if CONFIG_KEYPAD == IPOD_4G_PAD && !defined(IPOD_MINI)
 /* check if number of seconds has past */
 int timer_check(int clock_start, unsigned int usecs)
@@ -157,54 +152,6 @@
 }
 #endif
 
-char *strerror(int error)
-{
-    switch(error)
-    {
-    case 0:
-        return "OK";
-    case -1:
-        return "File not found";
-    case -2:
-        return "Read failed (chksum)";
-    case -3:
-        return "Read failed (model)";
-    case -4:
-        return "Read failed (image)";
-    case -5:
-        return "Bad checksum";
-    case -6:
-        return "File too big";
-    default:
-        return "Unknown";
-    }
-}
-
-char printfbuf[256];
-
-void reset_screen(void)
-{
-    lcd_clear_display();
-    line = 0;
-}
-
-void printf(const char *format, ...)
-{
-    int len;
-    unsigned char *ptr;
-    va_list ap;
-    va_start(ap, format);
-
-    ptr = printfbuf;
-    len = vsnprintf(ptr, sizeof(printfbuf), format, ap);
-    va_end(ap);
-
-    lcd_puts(0, line++, ptr);
-    lcd_update();
-    if(line >= (LCD_HEIGHT/SYSFONT_HEIGHT))
-        line = 0;
-}
-
 static int key_pressed(void)
 {
     unsigned char state;
@@ -240,101 +187,9 @@
     return (GPIOA_INPUT_VAL & 0x20)?false:true;
 }
 
-int load_rockbox(unsigned char* buf, char* firmware)
-{
-    int fd;
-    int rc;
-    int len;
-    unsigned long chksum;
-    char model[5];
-    unsigned long sum;
-    int i;
-    char filename[MAX_PATH];
-
-    snprintf(filename,sizeof(filename),"/.rockbox/%s",firmware);
-    fd = open(filename, O_RDONLY);
-    if(fd < 0)
-    {
-        snprintf(filename,sizeof(filename),"/%s",firmware);
-        fd = open(filename, O_RDONLY);
-        if(fd < 0)
-            return -1;
-    }
-
-    len = filesize(fd) - 8;
-
-    if (len > MAX_LOADSIZE)
-        return -6;
-
-    lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
-    
-    rc = read(fd, &chksum, 4);
-    chksum=betoh32(chksum); /* Rockbox checksums are big-endian */
-    if(rc < 4)
-        return -2;
-
-    rc = read(fd, model, 4);
-    if(rc < 4)
-        return -3;
-
-    model[4] = 0;
-    
-    printf("Model: %s", model);
-    printf("Checksum: %x", chksum);
-    printf("Loading %s", firmware);
-
-    lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
-
-    rc = read(fd, buf, len);
-    if(rc < len)
-        return -4;
-
-    close(fd);
-
-    sum = MODEL_NUMBER;
-    
-    for(i = 0;i < len;i++) {
-        sum += buf[i];
-    }
-
-    printf("Sum: %x", sum);
-
-    if(sum != chksum)
-        return -5;
-
-    return len;
-}
-
-
-int load_linux(unsigned char* buf) {
-    int fd;
-    int rc;
-    int len;
-
-    fd=open("/linux.bin",O_RDONLY);
-    if (fd < 0)
-        return -1;
-
-    len=filesize(fd);
-    if (len > MAX_LOADSIZE)
-        return -6;
-
-    rc=read(fd,buf,len);
-
-    if (rc < len)
-        return -4;
-
-    printf("Loaded Linux: %d bytes", len);
-
-    return len;
-}
-
-
-/* A buffer to load the Linux kernel or Rockbox into */
-unsigned char loadbuffer[MAX_LOADSIZE];
-
 void fatal_error(void)
 {
+    extern int line;
     bool holdstatus=false;
 
     /* System font is 6 pixels wide */
@@ -423,7 +278,6 @@
     button_init();
 #endif
 
-    line=0;
 
     lcd_setfont(FONT_SYSFIXED);
 
@@ -459,95 +313,74 @@
     printf("Partition 1: 0x%02x %ld MB", 
            pinfo->type, pinfo->size / 2048);
 
-    /* See if there is an Apple firmware image in RAM */
-    haveretailos = (memcmp((void*)(DRAM_START+0x20),"portalplayer",12)==0);
+    
+    /* Check for a keypress */
+    i=key_pressed();
 
-    /* We don't load Rockbox if the hold button is enabled. */
-    if (!button_was_held) {
-        /* Check for a keypress */
-        i=key_pressed();
+    if (button_was_held || (i==BUTTON_MENU)) {
+        /* If either the hold switch was on, or the Menu button was held, then 
+           try the Apple firmware */
 
-        if ((i!=BUTTON_MENU) && (i!=BUTTON_PLAY)) {
-            printf("Loading Rockbox...");
-            rc=load_rockbox(loadbuffer, BOOTFILE);
-            if (rc < 0) {
-                printf("Error!");
-                printf("Can't load rockbox.ipod:");
-                printf(strerror(rc));
-            } else {
-                printf("Rockbox loaded.");
-                memcpy((void*)DRAM_START,loadbuffer,rc);
+        printf("Loading original firmware...");
+    
+        /* First try an apple_os.ipod file on the FAT32 partition
+           (either in .rockbox or the root) 
+         */
+    
+        rc=load_firmware(loadbuffer, "apple_os.ipod", MAX_LOADSIZE);
+    
+        if(rc==EFILE_NOT_FOUND) {
+            /* If apple_os.ipod doesn't exist, then check if there is an Apple 
+               firmware image in RAM  */
+            haveretailos = (memcmp((void*)(DRAM_START+0x20),"portalplayer",12)==0);
+            if (haveretailos) {
+                /* We have a copy of the retailos in RAM, lets just run it. */
                 return (void*)DRAM_START;
             }
+        } else if (rc < EFILE_NOT_FOUND) {
+            printf("Error!");
+            printf("Can't load apple_os.ipod:");
+            printf(strerror(rc));
+        } else if (rc > 0) {
+            printf("apple_os.ipod loaded.");
+            return (void*)DRAM_START;
         }
-
-        if (i==BUTTON_PLAY) {
-            printf("Loading Linux...");
-            rc=load_linux(loadbuffer);
-            if (rc < 0) {
-                printf("Error!");
-                printf("Can't load linux.bin:");
-                printf(strerror(rc));
-            } else {
-                memcpy((void*)DRAM_START,loadbuffer,rc);
-                return (void*)DRAM_START;
-            }
+        
+        /* Everything failed - just loop forever */
+        printf("No RetailOS detected");
+        
+    } else if (i==BUTTON_PLAY) {
+        printf("Loading Linux...");
+        rc=load_raw_firmware(loadbuffer, "/linux.bin", MAX_LOADSIZE);
+        if (rc < EOK) {
+            printf("Error!");
+            printf("Can't load linux.bin:");
+            printf(strerror(rc));
+        } else {
+            return (void*)DRAM_START;
         }
+    } else {
+        printf("Loading Rockbox...");
+        rc=load_firmware(loadbuffer, BOOTFILE, MAX_LOADSIZE);
+        if (rc < EOK) {
+            printf("Error!");
+            printf("Can't load rockbox.ipod:");
+            printf(strerror(rc));
+        } else {
+            printf("Rockbox loaded.");
+            return (void*)DRAM_START;
+        }
     }
-
-
-    /* If either the hold switch was on, or loading Rockbox/IPL
-       failed, then try the Apple firmware */
-
-    printf("Loading original firmware...");
-
-    /* First try an apple_os.ipod file on the FAT32 partition
-       (either in .rockbox or the root) 
-     */
-
-    rc=load_rockbox(loadbuffer, "apple_os.ipod");
-
-    /* Only report errors if the file was found */
-    if (rc < -1) {
-        printf("Error!");
-        printf("Can't load apple_os.ipod:");
-        printf(strerror(rc));
-    } else if (rc > 0) {
-        printf("apple_os.ipod loaded.");
-        memcpy((void*)DRAM_START,loadbuffer,rc);
-        return (void*)DRAM_START;
-    }
-
-    if (haveretailos) {
-        /* We have a copy of the retailos in RAM, lets just run it. */
-        return (void*)DRAM_START;
-    }
-
-    /* Everything failed - just loop forever */
-    printf("No RetailOS detected");
-
+    
+    /* If we get to here, then we haven't been able to load any firmware */
     fatal_error();
-
+    
     /* We never get here, but keep gcc happy */
     return (void*)0;
 }
 
 /* These functions are present in the firmware library, but we reimplement
    them here because the originals do a lot more than we want */
-
-void reset_poweroff_timer(void)
-{
-}
-
-int dbg_ports(void)
-{
-   return 0;
-}
-
-void mpeg_stop(void)
-{
-}
-
 void usb_acknowledge(void)
 {
 }
@@ -555,7 +388,3 @@
 void usb_wait_for_disconnect(void)
 {
 }
-
-void sys_poweroff(void)
-{
-}
Index: bootloader/common.h
===================================================================
--- bootloader/common.h	(revision 0)
+++ bootloader/common.h	(revision 0)
@@ -0,0 +1,34 @@
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id: main.c 11997 2007-01-13 09:08:18Z miipekk $
+ *
+ * Copyright (C) 2005 by Linus Nielsen Feltzing
+ *
+ * All files in this archive are subject to the GNU General Public License.
+ * See the file COPYING in the source tree root for full license agreement.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+
+/* Error codes */
+#define     EOK                      0
+#define     EFILE_NOT_FOUND         -1
+#define     EREAD_CHKSUM_FAILED     -2
+#define     EREAD_MODEL_FAILED      -3
+#define     EREAD_IMAGE_FAILED      -4
+#define     EBAD_CHKSUM             -5
+#define     EFILE_TOO_BIG           -6
+
+/* Functions common to all bootloaders */
+void reset_screen(void);
+void printf(const char *format, ...);
+char *strerror(int error);
+int load_firmware(unsigned char* buf, char* firmware, int buffer_size);
+int load_raw_firmware(unsigned char* buf, char* firmware, int buffer_size);
Index: firmware/export/pp5002.h
===================================================================
--- firmware/export/pp5002.h	(revision 12077)
+++ firmware/export/pp5002.h	(working copy)
@@ -20,6 +20,10 @@
 #define __PP5002_H__
 
 /* All info gleaned and/or copied from the iPodLinux project. */
+#define DRAM_START       0x28000000
+
+#define IPOD_LCD_BASE    0xc0001000
+
 #define CPU_CTL          (*(volatile unsigned char *)(0xcf004054))
 #define COP_CTL          (*(volatile unsigned char *)(0xcf004058))
 
Index: firmware/export/pp5020.h
===================================================================
--- firmware/export/pp5020.h	(revision 12077)
+++ firmware/export/pp5020.h	(working copy)
@@ -20,6 +20,8 @@
 #define __PP5020_H__
 
 /* All info gleaned and/or copied from the iPodLinux project. */
+#define DRAM_START       0x10000000
+
 #define CPU_CTL          (*(volatile unsigned long *)(0x60007000))
 #define COP_CTL          (*(volatile unsigned long *)(0x60007004))
 
