2006-08-11 08:35:27 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 by Barry Wardell
|
|
|
|
*
|
|
|
|
* Based on Rockbox iriver bootloader by Linus Nielsen Feltzing
|
|
|
|
* and the ipodlinux bootloader by Daniel Palffy and Bernard Leach
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-01-28 18:42:11 +00:00
|
|
|
#include "common.h"
|
2006-08-11 08:35:27 +00:00
|
|
|
#include "cpu.h"
|
2007-01-28 18:42:11 +00:00
|
|
|
#include "file.h"
|
2006-08-11 08:35:27 +00:00
|
|
|
#include "system.h"
|
|
|
|
#include "kernel.h"
|
2007-01-28 18:42:11 +00:00
|
|
|
#include "lcd.h"
|
2006-08-11 08:35:27 +00:00
|
|
|
#include "font.h"
|
2007-01-28 18:42:11 +00:00
|
|
|
#include "ata.h"
|
2006-08-11 08:35:27 +00:00
|
|
|
#include "button.h"
|
2007-01-28 18:42:11 +00:00
|
|
|
#include "disk.h"
|
2007-03-16 14:28:00 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CRC32 implementation taken from:
|
|
|
|
*
|
|
|
|
* efone - Distributed internet phone system.
|
|
|
|
*
|
|
|
|
* (c) 1999,2000 Krzysztof Dabrowski
|
|
|
|
* (c) 1999,2000 ElysiuM deeZine
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* based on implementation by Finn Yannick Jacobs */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
/* crc_tab[] -- this crcTable is being build by chksum_crc32GenTab().
|
|
|
|
* so make sure, you call it before using the other
|
|
|
|
* functions!
|
|
|
|
*/
|
|
|
|
static unsigned int crc_tab[256];
|
|
|
|
|
|
|
|
/* chksum_crc() -- to a given block, this one calculates the
|
|
|
|
* crc32-checksum until the length is
|
|
|
|
* reached. the crc32-checksum will be
|
|
|
|
* the result.
|
|
|
|
*/
|
|
|
|
unsigned int chksum_crc32 (unsigned char *block, unsigned int length)
|
|
|
|
{
|
|
|
|
register unsigned long crc;
|
|
|
|
unsigned long i;
|
|
|
|
|
|
|
|
crc = 0;
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_tab[(crc ^ *block++) & 0xFF];
|
|
|
|
}
|
|
|
|
return (crc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* chksum_crc32gentab() -- to a global crc_tab[256], this one will
|
|
|
|
* calculate the crcTable for crc32-checksums.
|
|
|
|
* it is generated to the polynom [..]
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void chksum_crc32gentab (void)
|
|
|
|
{
|
|
|
|
unsigned long crc, poly;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
poly = 0xEDB88320L;
|
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
{
|
|
|
|
crc = i;
|
|
|
|
for (j = 8; j > 0; j--)
|
|
|
|
{
|
|
|
|
if (crc & 1)
|
|
|
|
{
|
|
|
|
crc = (crc >> 1) ^ poly;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
crc >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
crc_tab[i] = crc;
|
|
|
|
}
|
|
|
|
}
|
2007-03-12 22:12:20 +00:00
|
|
|
|
|
|
|
/* Button definitions */
|
|
|
|
#if CONFIG_KEYPAD == IRIVER_H10_PAD
|
|
|
|
#define BOOTLOADER_VERBOSE BUTTON_PLAY
|
|
|
|
#define BOOTLOADER_BOOT_OF BUTTON_LEFT
|
|
|
|
|
|
|
|
#elif CONFIG_KEYPAD == SANSA_E200_PAD
|
|
|
|
#define BOOTLOADER_VERBOSE BUTTON_RIGHT
|
|
|
|
#define BOOTLOADER_BOOT_OF BUTTON_LEFT
|
|
|
|
|
|
|
|
#endif
|
2006-08-11 08:35:27 +00:00
|
|
|
|
2007-01-28 18:42:11 +00:00
|
|
|
/* Maximum allowed firmware image size. 10MB is more than enough */
|
2006-12-19 11:33:53 +00:00
|
|
|
#define MAX_LOADSIZE (10*1024*1024)
|
2006-08-11 08:35:27 +00:00
|
|
|
|
2007-01-28 18:42:11 +00:00
|
|
|
/* A buffer to load the original firmware or Rockbox into */
|
|
|
|
unsigned char *loadbuffer = (unsigned char *)DRAM_START;
|
2006-08-28 08:11:32 +00:00
|
|
|
|
2007-01-28 18:42:11 +00:00
|
|
|
/* Bootloader version */
|
2006-08-28 08:11:32 +00:00
|
|
|
char version[] = APPSVERSION;
|
|
|
|
|
2007-03-16 14:28:00 +00:00
|
|
|
/* Locations and sizes in hidden partition on Sansa */
|
|
|
|
#define PPMI_OFFSET 1024
|
|
|
|
#define PPMI_SIZE 1
|
|
|
|
#define MI4_HEADER_SIZE 1
|
|
|
|
|
|
|
|
/* mi4 header structure */
|
|
|
|
struct mi4header_t {
|
|
|
|
unsigned char magic[4];
|
|
|
|
uint32_t version;
|
|
|
|
uint32_t length;
|
|
|
|
uint32_t crc32;
|
|
|
|
uint32_t enctype;
|
|
|
|
uint32_t mi4size;
|
|
|
|
uint32_t plaintext;
|
|
|
|
uint32_t dsa_key[10];
|
|
|
|
uint32_t pad[109];
|
|
|
|
unsigned char type[4];
|
|
|
|
unsigned char model[4];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* PPMI header structure */
|
|
|
|
struct ppmi_header_t {
|
|
|
|
unsigned char magic[4];
|
|
|
|
uint32_t length;
|
|
|
|
uint32_t pad[126];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Load mi4 format firmware image */
|
|
|
|
int load_mi4(unsigned char* buf, char* firmware, unsigned int buffer_size)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct mi4header_t mi4header;
|
|
|
|
int rc;
|
|
|
|
unsigned long sum;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
read(fd, &mi4header, 0x200);
|
|
|
|
|
|
|
|
/* We don't support encrypted mi4 files yet */
|
|
|
|
if( (mi4header.plaintext + 0x200) != mi4header.mi4size)
|
|
|
|
return EINVALID_FORMAT;
|
|
|
|
|
|
|
|
/* MI4 file size */
|
|
|
|
printf("mi4 size: %x", mi4header.length);
|
|
|
|
|
|
|
|
if (mi4header.length > buffer_size)
|
|
|
|
return EFILE_TOO_BIG;
|
|
|
|
|
|
|
|
/* CRC32 */
|
|
|
|
printf("CRC32: %x", mi4header.crc32);
|
|
|
|
|
|
|
|
/* Rockbox model id */
|
|
|
|
printf("Model id: %4s", mi4header.model);
|
|
|
|
|
|
|
|
/* Read binary type (RBOS, RBBL) */
|
|
|
|
printf("Binary type: %4s", mi4header.type);
|
|
|
|
|
|
|
|
/* Load firmware */
|
|
|
|
lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
|
|
|
|
rc = read(fd, buf, mi4header.mi4size-0x200);
|
|
|
|
if(rc < (int)mi4header.mi4size-0x200)
|
|
|
|
return EREAD_IMAGE_FAILED;
|
|
|
|
|
|
|
|
/* Check CRC32 to see if we have a valid file */
|
|
|
|
sum = chksum_crc32 (buf,mi4header.mi4size-0x200);
|
|
|
|
|
|
|
|
printf("Calculated CRC32: %x", sum);
|
|
|
|
|
|
|
|
if(sum != mi4header.crc32)
|
|
|
|
return EBAD_CHKSUM;
|
|
|
|
|
|
|
|
return EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load mi4 firmware from a hidden disk partition */
|
|
|
|
int load_mi4_part(unsigned char* buf, struct partinfo* pinfo, unsigned int buffer_size)
|
|
|
|
{
|
|
|
|
struct mi4header_t mi4header;
|
|
|
|
struct ppmi_header_t ppmi_header;
|
|
|
|
unsigned long sum;
|
|
|
|
|
|
|
|
/* Read header to find out how long the mi4 file is. */
|
|
|
|
ata_read_sectors(pinfo->start + PPMI_OFFSET, PPMI_SIZE, &ppmi_header);
|
|
|
|
|
|
|
|
/* The first four characters at 0x80000 (sector 1024) should be PPMI*/
|
|
|
|
if( memcmp(ppmi_header.magic, "PPMI", 4) )
|
|
|
|
return EFILE_NOT_FOUND;
|
|
|
|
|
|
|
|
printf("BL mi4 size: %x", ppmi_header.length);
|
|
|
|
|
|
|
|
/* Read mi4 header of the OF */
|
|
|
|
ata_read_sectors(pinfo->start + PPMI_OFFSET + PPMI_SIZE
|
|
|
|
+ (ppmi_header.length/512), MI4_HEADER_SIZE, &mi4header);
|
|
|
|
|
|
|
|
/* We don't support encrypted mi4 files yet */
|
|
|
|
if( (mi4header.plaintext + 0x200) != mi4header.mi4size)
|
|
|
|
return EINVALID_FORMAT;
|
|
|
|
|
|
|
|
/* MI4 file size */
|
|
|
|
printf("OF mi4 size: %x", mi4header.length);
|
|
|
|
|
|
|
|
if (mi4header.length > buffer_size)
|
|
|
|
return EFILE_TOO_BIG;
|
|
|
|
|
|
|
|
/* CRC32 */
|
|
|
|
printf("CRC32: %x", mi4header.crc32);
|
|
|
|
|
|
|
|
/* Rockbox model id */
|
|
|
|
printf("Model id: %4s", mi4header.model);
|
|
|
|
|
|
|
|
/* Read binary type (RBOS, RBBL) */
|
|
|
|
printf("Binary type: %4s", mi4header.type);
|
|
|
|
|
|
|
|
/* Load firmware */
|
|
|
|
ata_read_sectors(pinfo->start + PPMI_OFFSET + PPMI_SIZE
|
|
|
|
+ (ppmi_header.length/512) + MI4_HEADER_SIZE,
|
|
|
|
(mi4header.length-0x200)/512, buf);
|
|
|
|
|
|
|
|
/* Check CRC32 to see if we have a valid file */
|
|
|
|
sum = chksum_crc32 (buf,mi4header.mi4size-0x200);
|
|
|
|
|
|
|
|
printf("Calculated CRC32: %x", sum);
|
|
|
|
|
|
|
|
if(sum != mi4header.crc32)
|
|
|
|
return EBAD_CHKSUM;
|
|
|
|
|
|
|
|
return EOK;
|
|
|
|
}
|
|
|
|
|
2006-08-28 08:11:32 +00:00
|
|
|
void* main(void)
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
int i;
|
2007-03-12 22:12:20 +00:00
|
|
|
int btn;
|
2006-08-28 08:11:32 +00:00
|
|
|
int rc;
|
2007-03-16 14:28:00 +00:00
|
|
|
int num_partitions;
|
2006-08-28 08:11:32 +00:00
|
|
|
unsigned short* identify_info;
|
|
|
|
struct partinfo* pinfo;
|
|
|
|
|
2007-03-16 14:28:00 +00:00
|
|
|
chksum_crc32gentab ();
|
|
|
|
|
2006-08-28 08:11:32 +00:00
|
|
|
system_init();
|
|
|
|
kernel_init();
|
|
|
|
lcd_init();
|
|
|
|
font_init();
|
2006-12-19 11:33:53 +00:00
|
|
|
button_init();
|
2006-08-28 08:11:32 +00:00
|
|
|
|
2007-03-16 14:28:00 +00:00
|
|
|
lcd_set_foreground(LCD_WHITE);
|
|
|
|
lcd_set_background(LCD_BLACK);
|
|
|
|
lcd_clear_display();
|
|
|
|
|
2007-03-12 22:12:20 +00:00
|
|
|
btn = button_read_device();
|
|
|
|
|
|
|
|
/* Enable bootloader messages */
|
2007-03-15 22:32:58 +00:00
|
|
|
if (btn & BOOTLOADER_VERBOSE)
|
2007-03-12 22:12:20 +00:00
|
|
|
verbose = true;
|
|
|
|
|
2006-08-28 08:11:32 +00:00
|
|
|
lcd_setfont(FONT_SYSFIXED);
|
|
|
|
|
Make the build system create a sysfont.h which includes font information for the system font. Available #defines are: SYSFONT_NAME, SYSFONT_FACENAME, SYSFONT_WIDTH, SYSFONT_HEIGHT, SYSFONT_SIZE, SYSFONT_ASCENT, SYSFONT_DESCENT, SYSFONT_FIRST_CHAR, SYSFONT_LAST_CHAR, SYSFONT_DEFAULT_CHAR, SYSFONT_PROPORTIONAL, SYSFONT_COPYRIGHT, SYSFONT_BITS_SIZE.
Also fix a small bug in the iPod bootloader printf() code and use printf() for PortalPlayer bootloaders too.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@12041 a1c6a512-1295-4272-9138-f99709370657
2007-01-17 12:20:38 +00:00
|
|
|
printf("Rockbox boot loader");
|
|
|
|
printf("Version: 20%s", version);
|
|
|
|
printf(MODEL_NAME);
|
2006-08-28 08:11:32 +00:00
|
|
|
|
|
|
|
i=ata_init();
|
|
|
|
if (i==0) {
|
2007-01-28 18:42:11 +00:00
|
|
|
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);
|
2006-08-28 08:11:32 +00:00
|
|
|
} else {
|
2007-03-12 22:12:20 +00:00
|
|
|
error(EATA, i);
|
2006-08-28 08:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
disk_init();
|
2007-03-16 14:28:00 +00:00
|
|
|
num_partitions = disk_mount_all();
|
|
|
|
if (num_partitions<=0)
|
2006-08-28 08:11:32 +00:00
|
|
|
{
|
2007-03-16 14:28:00 +00:00
|
|
|
error(EDISK,num_partitions);
|
2006-08-28 08:11:32 +00:00
|
|
|
}
|
|
|
|
|
2007-03-16 14:28:00 +00:00
|
|
|
/* Just list the first 2 partitions since we don't have any devices yet
|
|
|
|
that have more than that */
|
|
|
|
for(i=0; i<2; i++)
|
|
|
|
{
|
|
|
|
pinfo = disk_partinfo(i);
|
|
|
|
printf("Partition %d: 0x%02x %ld MB",
|
|
|
|
i, pinfo->type, pinfo->size / 2048);
|
|
|
|
}
|
2006-08-28 08:11:32 +00:00
|
|
|
|
2007-03-15 22:32:58 +00:00
|
|
|
if(btn & BOOTLOADER_BOOT_OF)
|
2006-08-28 08:11:32 +00:00
|
|
|
{
|
2007-03-16 14:28:00 +00:00
|
|
|
/* Load original mi4 firmware in to a memory buffer called loadbuffer.
|
|
|
|
The rest of the loading is done in crt0.S.
|
|
|
|
1) First try reading from the hidden partition (on Sansa only).
|
|
|
|
2) Next try a decrypted mi4 file in /System/OF.mi4
|
|
|
|
3) Finally, try a raw firmware binary in /System/OF.mi4. It should be
|
|
|
|
a mi4 firmware decrypted and header stripped using mi4code.
|
2007-01-28 18:42:11 +00:00
|
|
|
*/
|
Make the build system create a sysfont.h which includes font information for the system font. Available #defines are: SYSFONT_NAME, SYSFONT_FACENAME, SYSFONT_WIDTH, SYSFONT_HEIGHT, SYSFONT_SIZE, SYSFONT_ASCENT, SYSFONT_DESCENT, SYSFONT_FIRST_CHAR, SYSFONT_LAST_CHAR, SYSFONT_DEFAULT_CHAR, SYSFONT_PROPORTIONAL, SYSFONT_COPYRIGHT, SYSFONT_BITS_SIZE.
Also fix a small bug in the iPod bootloader printf() code and use printf() for PortalPlayer bootloaders too.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@12041 a1c6a512-1295-4272-9138-f99709370657
2007-01-17 12:20:38 +00:00
|
|
|
printf("Loading original firmware...");
|
2007-03-16 14:28:00 +00:00
|
|
|
|
|
|
|
/* First try a hidden partition */
|
|
|
|
printf("Trying hidden partition");
|
|
|
|
pinfo = disk_partinfo(1);
|
|
|
|
if(pinfo->type == PARTITION_TYPE_HIDDEN)
|
|
|
|
{
|
|
|
|
rc = load_mi4_part(loadbuffer, pinfo, MAX_LOADSIZE);
|
|
|
|
if (rc < EOK) {
|
|
|
|
printf("Can't load from partition");
|
|
|
|
printf(strerror(rc));
|
|
|
|
} else {
|
|
|
|
return (void*)loadbuffer;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("No hidden partition found.");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Trying /System/OF.mi4");
|
|
|
|
rc=load_mi4(loadbuffer, "/System/OF.mi4", MAX_LOADSIZE);
|
|
|
|
if (rc < EOK) {
|
|
|
|
printf("Can't load /System/OF.mi4");
|
|
|
|
printf(strerror(rc));
|
|
|
|
} else {
|
|
|
|
return (void*)loadbuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Trying /System/OF.bin");
|
2007-01-28 18:42:11 +00:00
|
|
|
rc=load_raw_firmware(loadbuffer, "/System/OF.bin", MAX_LOADSIZE);
|
|
|
|
if (rc < EOK) {
|
2007-03-12 22:12:20 +00:00
|
|
|
printf("Can't load /System/OF.bin");
|
2007-03-16 14:28:00 +00:00
|
|
|
printf(strerror(rc));
|
|
|
|
} else {
|
|
|
|
return (void*)loadbuffer;
|
2007-01-28 18:42:11 +00:00
|
|
|
}
|
2007-03-16 14:28:00 +00:00
|
|
|
|
2006-08-28 08:11:32 +00:00
|
|
|
} else {
|
Make the build system create a sysfont.h which includes font information for the system font. Available #defines are: SYSFONT_NAME, SYSFONT_FACENAME, SYSFONT_WIDTH, SYSFONT_HEIGHT, SYSFONT_SIZE, SYSFONT_ASCENT, SYSFONT_DESCENT, SYSFONT_FIRST_CHAR, SYSFONT_LAST_CHAR, SYSFONT_DEFAULT_CHAR, SYSFONT_PROPORTIONAL, SYSFONT_COPYRIGHT, SYSFONT_BITS_SIZE.
Also fix a small bug in the iPod bootloader printf() code and use printf() for PortalPlayer bootloaders too.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@12041 a1c6a512-1295-4272-9138-f99709370657
2007-01-17 12:20:38 +00:00
|
|
|
printf("Loading Rockbox...");
|
2007-03-16 14:28:00 +00:00
|
|
|
rc=load_mi4(loadbuffer, BOOTFILE, MAX_LOADSIZE);
|
2007-01-28 18:42:11 +00:00
|
|
|
if (rc < EOK) {
|
|
|
|
printf("Can't load %s:", BOOTFILE);
|
2007-03-12 22:12:20 +00:00
|
|
|
error(EBOOTFILE, rc);
|
2007-01-28 18:42:11 +00:00
|
|
|
}
|
2006-08-28 08:11:32 +00:00
|
|
|
}
|
2006-08-11 09:51:04 +00:00
|
|
|
|
2007-01-28 18:42:11 +00:00
|
|
|
return (void*)loadbuffer;
|
2006-08-11 08:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* These functions are present in the firmware library, but we reimplement
|
|
|
|
them here because the originals do a lot more than we want */
|
|
|
|
void usb_acknowledge(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_wait_for_disconnect(void)
|
|
|
|
{
|
|
|
|
}
|