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-03-22 12:55:51 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
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-06-04 13:48:21 +00:00
|
|
|
#include "crc32-mi4.h"
|
2007-03-16 14:28:00 +00:00
|
|
|
#include <string.h>
|
2007-10-14 18:20:23 +00:00
|
|
|
#if defined(SANSA_E200)
|
|
|
|
#include "i2c.h"
|
|
|
|
#include "backlight-target.h"
|
|
|
|
#endif
|
2007-09-06 03:28:58 +00:00
|
|
|
#if defined(SANSA_E200) || defined(SANSA_C200)
|
2007-03-22 12:55:51 +00:00
|
|
|
#include "usb.h"
|
2007-09-04 08:14:47 +00:00
|
|
|
#include "arcotg_udc.h"
|
2007-03-22 12:55:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* Button definitions */
|
|
|
|
#if CONFIG_KEYPAD == IRIVER_H10_PAD
|
|
|
|
#define BOOTLOADER_BOOT_OF BUTTON_LEFT
|
|
|
|
|
|
|
|
#elif CONFIG_KEYPAD == SANSA_E200_PAD
|
|
|
|
#define BOOTLOADER_BOOT_OF BUTTON_LEFT
|
|
|
|
|
2007-09-06 03:28:58 +00:00
|
|
|
#elif CONFIG_KEYPAD == SANSA_C200_PAD
|
|
|
|
#define BOOTLOADER_BOOT_OF BUTTON_LEFT
|
|
|
|
|
2007-03-22 12:55:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Maximum allowed firmware image size. 10MB is more than enough */
|
|
|
|
#define MAX_LOADSIZE (10*1024*1024)
|
2007-03-16 14:28:00 +00:00
|
|
|
|
2007-03-22 12:55:51 +00:00
|
|
|
/* A buffer to load the original firmware or Rockbox into */
|
|
|
|
unsigned char *loadbuffer = (unsigned char *)DRAM_START;
|
|
|
|
|
|
|
|
/* Bootloader version */
|
|
|
|
char version[] = APPSVERSION;
|
|
|
|
|
2007-03-20 20:45:25 +00:00
|
|
|
/* Locations and sizes in hidden partition on Sansa */
|
2007-09-06 03:28:58 +00:00
|
|
|
#if defined(SANSA_E200) || defined(SANSA_C200)
|
2007-03-20 20:45:25 +00:00
|
|
|
#define PPMI_SECTOR_OFFSET 1024
|
|
|
|
#define PPMI_SECTORS 1
|
|
|
|
#define MI4_HEADER_SECTORS 1
|
2007-03-20 22:18:35 +00:00
|
|
|
#define NUM_PARTITIONS 2
|
|
|
|
|
|
|
|
#else
|
|
|
|
#define NUM_PARTITIONS 1
|
|
|
|
|
|
|
|
#endif
|
2007-03-20 20:45:25 +00:00
|
|
|
|
2007-03-20 22:56:51 +00:00
|
|
|
#define MI4_HEADER_SIZE 0x200
|
|
|
|
|
2007-03-20 20:45:25 +00:00
|
|
|
/* 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];
|
|
|
|
};
|
|
|
|
|
|
|
|
inline unsigned int le2int(unsigned char* buf)
|
|
|
|
{
|
|
|
|
int32_t res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void int2le(unsigned int val, unsigned char* addr)
|
|
|
|
{
|
|
|
|
addr[0] = val & 0xFF;
|
|
|
|
addr[1] = (val >> 8) & 0xff;
|
|
|
|
addr[2] = (val >> 16) & 0xff;
|
|
|
|
addr[3] = (val >> 24) & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tea_key {
|
|
|
|
const char * name;
|
|
|
|
uint32_t key[4];
|
|
|
|
};
|
|
|
|
|
2007-09-03 21:23:57 +00:00
|
|
|
#define NUM_KEYS (sizeof(tea_keytable)/sizeof(tea_keytable[0]))
|
2007-03-20 20:45:25 +00:00
|
|
|
struct tea_key tea_keytable[] = {
|
|
|
|
{ "default" , { 0x20d36cc0, 0x10e8c07d, 0xc0e7dcaa, 0x107eb080 } },
|
|
|
|
{ "sansa", { 0xe494e96e, 0x3ee32966, 0x6f48512b, 0xa93fbb42 } },
|
|
|
|
{ "sansa_gh", { 0xd7b10538, 0xc662945b, 0x1b3fce68, 0xf389c0e6 } },
|
2007-09-03 21:23:57 +00:00
|
|
|
{ "sansa_103", { 0x1d29ddc0, 0x2579c2cd, 0xce339e1a, 0x75465dfe } },
|
2007-03-20 20:45:25 +00:00
|
|
|
{ "rhapsody", { 0x7aa9c8dc, 0xbed0a82a, 0x16204cc7, 0x5904ef38 } },
|
|
|
|
{ "p610", { 0x950e83dc, 0xec4907f9, 0x023734b9, 0x10cfb7c7 } },
|
|
|
|
{ "p640", { 0x220c5f23, 0xd04df68e, 0x431b5e25, 0x4dcc1fa1 } },
|
|
|
|
{ "virgin", { 0xe83c29a1, 0x04862973, 0xa9b3f0d4, 0x38be2a9c } },
|
|
|
|
{ "20gc_eng", { 0x0240772c, 0x6f3329b5, 0x3ec9a6c5, 0xb0c9e493 } },
|
|
|
|
{ "20gc_fre", { 0xbede8817, 0xb23bfe4f, 0x80aa682d, 0xd13f598c } },
|
|
|
|
{ "elio_p722", { 0x6af3b9f8, 0x777483f5, 0xae8181cc, 0xfa6d8a84 } },
|
|
|
|
{ "c200", { 0xbf2d06fa, 0xf0e23d59, 0x29738132, 0xe2d04ca7 } },
|
2007-09-18 13:58:12 +00:00
|
|
|
{ "c200_103", { 0x2a7968de, 0x15127979, 0x142e60a7, 0xe49c1893 } },
|
2007-09-03 21:23:57 +00:00
|
|
|
{ "c200_106", { 0xa913d139, 0xf842f398, 0x3e03f1a6, 0x060ee012 } },
|
2007-03-20 20:45:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
tea_decrypt() from http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
|
|
|
|
|
|
|
|
"Following is an adaptation of the reference encryption and decryption
|
|
|
|
routines in C, released into the public domain by David Wheeler and
|
|
|
|
Roger Needham:"
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* NOTE: The mi4 version of TEA uses a different initial value to sum compared
|
|
|
|
to the reference implementation and the main loop is 8 iterations, not
|
|
|
|
32.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void tea_decrypt(uint32_t* v0, uint32_t* v1, uint32_t* k) {
|
|
|
|
uint32_t sum=0xF1BBCDC8, i; /* set up */
|
|
|
|
uint32_t delta=0x9E3779B9; /* a key schedule constant */
|
|
|
|
uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* cache key */
|
|
|
|
for(i=0; i<8; i++) { /* basic cycle start */
|
|
|
|
*v1 -= ((*v0<<4) + k2) ^ (*v0 + sum) ^ ((*v0>>5) + k3);
|
|
|
|
*v0 -= ((*v1<<4) + k0) ^ (*v1 + sum) ^ ((*v1>>5) + k1);
|
|
|
|
sum -= delta; /* end cycle */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mi4 files are encrypted in 64-bit blocks (two little-endian 32-bit
|
|
|
|
integers) and the key is incremented after each block
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void tea_decrypt_buf(unsigned char* src, unsigned char* dest, size_t n, uint32_t * key)
|
|
|
|
{
|
|
|
|
uint32_t v0, v1;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < (n / 8); i++) {
|
|
|
|
v0 = le2int(src);
|
|
|
|
v1 = le2int(src+4);
|
|
|
|
|
|
|
|
tea_decrypt(&v0, &v1, key);
|
|
|
|
|
|
|
|
int2le(v0, dest);
|
|
|
|
int2le(v1, dest+4);
|
|
|
|
|
|
|
|
src += 8;
|
|
|
|
dest += 8;
|
|
|
|
|
|
|
|
/* Now increment the key */
|
|
|
|
key[0]++;
|
|
|
|
if (key[0]==0) {
|
|
|
|
key[1]++;
|
|
|
|
if (key[1]==0) {
|
|
|
|
key[2]++;
|
|
|
|
if (key[2]==0) {
|
|
|
|
key[3]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool tea_test_key(unsigned char magic_enc[8], uint32_t * key, int unaligned)
|
|
|
|
{
|
|
|
|
unsigned char magic_dec[8];
|
|
|
|
tea_decrypt_buf(magic_enc, magic_dec, 8, key);
|
|
|
|
|
|
|
|
return (le2int(&magic_dec[4*unaligned]) == 0xaa55aa55);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tea_find_key(struct mi4header_t *mi4header, int fd)
|
|
|
|
{
|
2007-09-03 21:26:39 +00:00
|
|
|
unsigned int i;
|
|
|
|
int rc;
|
2007-03-20 20:45:25 +00:00
|
|
|
unsigned int j;
|
|
|
|
uint32_t key[4];
|
|
|
|
unsigned char magic_enc[8];
|
|
|
|
int key_found = -1;
|
|
|
|
unsigned int magic_location = mi4header->length-4;
|
|
|
|
int unaligned = 0;
|
|
|
|
|
|
|
|
if ( (magic_location % 8) != 0 )
|
|
|
|
{
|
|
|
|
unaligned = 1;
|
|
|
|
magic_location -= 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load encrypted magic 0xaa55aa55 to check key */
|
|
|
|
lseek(fd, MI4_HEADER_SIZE + magic_location, SEEK_SET);
|
|
|
|
rc = read(fd, magic_enc, 8);
|
|
|
|
if(rc < 8 )
|
|
|
|
return EREAD_IMAGE_FAILED;
|
|
|
|
|
2007-03-20 22:18:35 +00:00
|
|
|
printf("Searching for key:");
|
2007-03-20 20:45:25 +00:00
|
|
|
|
|
|
|
for (i=0; i < NUM_KEYS && (key_found<0) ; i++) {
|
|
|
|
key[0] = tea_keytable[i].key[0];
|
|
|
|
key[1] = tea_keytable[i].key[1];
|
|
|
|
key[2] = tea_keytable[i].key[2];
|
|
|
|
key[3] = tea_keytable[i].key[3];
|
|
|
|
|
|
|
|
/* Now increment the key */
|
|
|
|
for(j=0; j<((magic_location-mi4header->plaintext)/8); j++){
|
|
|
|
key[0]++;
|
|
|
|
if (key[0]==0) {
|
|
|
|
key[1]++;
|
|
|
|
if (key[1]==0) {
|
|
|
|
key[2]++;
|
|
|
|
if (key[2]==0) {
|
|
|
|
key[3]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tea_test_key(magic_enc,key,unaligned))
|
|
|
|
{
|
|
|
|
key_found = i;
|
|
|
|
printf("%s...found", tea_keytable[i].name);
|
|
|
|
} else {
|
2007-03-20 22:18:35 +00:00
|
|
|
/* printf("%s...failed", tea_keytable[i].name); */
|
2007-03-20 20:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return key_found;
|
|
|
|
}
|
2007-03-12 22:12:20 +00:00
|
|
|
|
2006-08-28 08:11:32 +00:00
|
|
|
|
2007-03-16 14:28:00 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2007-03-17 19:07:20 +00:00
|
|
|
read(fd, &mi4header, MI4_HEADER_SIZE);
|
2007-03-16 14:28:00 +00:00
|
|
|
|
|
|
|
/* MI4 file size */
|
2007-03-20 12:35:45 +00:00
|
|
|
printf("mi4 size: %x", mi4header.mi4size);
|
2007-03-16 14:28:00 +00:00
|
|
|
|
2007-03-20 12:35:45 +00:00
|
|
|
if ((mi4header.mi4size-MI4_HEADER_SIZE) > buffer_size)
|
2007-03-16 14:28:00 +00:00
|
|
|
return EFILE_TOO_BIG;
|
|
|
|
|
|
|
|
/* CRC32 */
|
|
|
|
printf("CRC32: %x", mi4header.crc32);
|
|
|
|
|
|
|
|
/* Rockbox model id */
|
2007-03-19 11:20:22 +00:00
|
|
|
printf("Model id: %.4s", mi4header.model);
|
2007-03-16 14:28:00 +00:00
|
|
|
|
|
|
|
/* Read binary type (RBOS, RBBL) */
|
2007-03-19 11:20:22 +00:00
|
|
|
printf("Binary type: %.4s", mi4header.type);
|
2007-03-16 14:28:00 +00:00
|
|
|
|
2007-03-20 22:02:26 +00:00
|
|
|
/* Load firmware file */
|
|
|
|
lseek(fd, MI4_HEADER_SIZE, SEEK_SET);
|
|
|
|
rc = read(fd, buf, mi4header.mi4size-MI4_HEADER_SIZE);
|
|
|
|
if(rc < (int)mi4header.mi4size-MI4_HEADER_SIZE)
|
|
|
|
return EREAD_IMAGE_FAILED;
|
|
|
|
|
|
|
|
/* Check CRC32 to see if we have a valid file */
|
|
|
|
sum = chksum_crc32 (buf, mi4header.mi4size - MI4_HEADER_SIZE);
|
|
|
|
|
|
|
|
printf("Calculated CRC32: %x", sum);
|
|
|
|
|
|
|
|
if(sum != mi4header.crc32)
|
|
|
|
return EBAD_CHKSUM;
|
|
|
|
|
|
|
|
if( (mi4header.plaintext + MI4_HEADER_SIZE) != mi4header.mi4size)
|
2007-03-20 20:45:25 +00:00
|
|
|
{
|
|
|
|
/* Load encrypted firmware */
|
|
|
|
int key_index = tea_find_key(&mi4header, fd);
|
|
|
|
|
|
|
|
if (key_index < 0)
|
|
|
|
return EINVALID_FORMAT;
|
|
|
|
|
2007-03-20 22:02:26 +00:00
|
|
|
/* Plaintext part is already loaded */
|
2007-03-20 20:45:25 +00:00
|
|
|
buf += mi4header.plaintext;
|
|
|
|
|
2007-03-20 22:02:26 +00:00
|
|
|
/* Decrypt in-place */
|
|
|
|
tea_decrypt_buf(buf, buf,
|
|
|
|
mi4header.mi4size-(mi4header.plaintext+MI4_HEADER_SIZE),
|
|
|
|
tea_keytable[key_index].key);
|
|
|
|
|
2007-03-20 20:45:25 +00:00
|
|
|
printf("%s key used", tea_keytable[key_index].name);
|
2007-03-20 22:02:26 +00:00
|
|
|
|
|
|
|
/* Check decryption was successfull */
|
2007-03-20 22:18:35 +00:00
|
|
|
if(le2int(&buf[mi4header.length-mi4header.plaintext-4]) != 0xaa55aa55)
|
2007-03-20 22:02:26 +00:00
|
|
|
{
|
2007-03-20 20:45:25 +00:00
|
|
|
return EREAD_IMAGE_FAILED;
|
2007-03-20 22:02:26 +00:00
|
|
|
}
|
2007-03-20 20:45:25 +00:00
|
|
|
}
|
|
|
|
|
2007-03-16 14:28:00 +00:00
|
|
|
return EOK;
|
|
|
|
}
|
|
|
|
|
2007-09-06 03:28:58 +00:00
|
|
|
#if defined(SANSA_E200) || defined(SANSA_C200)
|
2007-03-16 14:36:14 +00:00
|
|
|
#ifdef SANSA_E200
|
2007-05-20 03:08:00 +00:00
|
|
|
struct OFDB_info {
|
|
|
|
char *version;
|
|
|
|
int version_length;
|
|
|
|
int sector;
|
|
|
|
int offset;
|
|
|
|
} OFDatabaseOffsets[] = {
|
|
|
|
{ "PP5022AF-05.51-S301-01.11-S301.01.11A-D", 39, 0x3c08, 0xe1 },
|
2007-10-20 10:01:05 +00:00
|
|
|
{ "PP5022AF-05.51-S301-00.12-S301.00.12E-D", 39, 0x3c5c, 0x2 },
|
2007-05-20 03:08:00 +00:00
|
|
|
{ "PP5022AF-05.51-S301-00.12-S301.00.12A-D", 39, 0x3c08, 0xe1 },
|
2007-10-20 10:01:05 +00:00
|
|
|
{ "PP5022AF-05.51-S301-02.15-S301.02.15E-D", 39, 0x3c08, 0xe1 },
|
|
|
|
{ "PP5022AF-05.51-S301-02.18-S301.02.18A-D", 39, 0x3c08, 0xe1 },
|
|
|
|
{ "PP5022AF-05.51-S301-02.18-S301.02.18E-D", 39, 0x3c08, 0xe1 }
|
2007-05-20 03:08:00 +00:00
|
|
|
};
|
2007-09-06 03:28:58 +00:00
|
|
|
#else /* SANSA_C200 */
|
|
|
|
/* TODO: need to determine these for the c200 */
|
|
|
|
struct OFDB_info {
|
|
|
|
char *version;
|
|
|
|
int version_length;
|
|
|
|
int sector;
|
|
|
|
int offset;
|
|
|
|
} OFDatabaseOffsets[] = {
|
|
|
|
{ "PP5022AF-05.51-S301-01.11-S301.01.11A-D", 39, 0x3c08, 0xe1 },
|
|
|
|
};
|
|
|
|
#endif
|
2007-05-20 03:08:00 +00:00
|
|
|
|
2007-03-16 14:28:00 +00:00
|
|
|
/* Load mi4 firmware from a hidden disk partition */
|
2007-03-22 12:45:19 +00:00
|
|
|
int load_mi4_part(unsigned char* buf, struct partinfo* pinfo,
|
|
|
|
unsigned int buffer_size, bool disable_rebuild)
|
2007-03-16 14:28:00 +00:00
|
|
|
{
|
|
|
|
struct mi4header_t mi4header;
|
|
|
|
struct ppmi_header_t ppmi_header;
|
|
|
|
unsigned long sum;
|
|
|
|
|
|
|
|
/* Read header to find out how long the mi4 file is. */
|
2007-06-30 02:08:27 +00:00
|
|
|
ata_read_sectors(IF_MV2(0,) pinfo->start + PPMI_SECTOR_OFFSET,
|
2007-03-17 19:07:20 +00:00
|
|
|
PPMI_SECTORS, &ppmi_header);
|
2007-03-16 14:28:00 +00:00
|
|
|
|
|
|
|
/* 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 */
|
2007-06-30 02:08:27 +00:00
|
|
|
ata_read_sectors(IF_MV2(0,) pinfo->start + PPMI_SECTOR_OFFSET + PPMI_SECTORS
|
2007-03-17 19:07:20 +00:00
|
|
|
+ (ppmi_header.length/512), MI4_HEADER_SECTORS, &mi4header);
|
2007-03-16 14:28:00 +00:00
|
|
|
|
|
|
|
/* We don't support encrypted mi4 files yet */
|
2007-03-20 12:35:45 +00:00
|
|
|
if( (mi4header.plaintext) != (mi4header.mi4size-MI4_HEADER_SIZE))
|
2007-03-16 14:28:00 +00:00
|
|
|
return EINVALID_FORMAT;
|
|
|
|
|
|
|
|
/* MI4 file size */
|
2007-03-20 12:35:45 +00:00
|
|
|
printf("OF mi4 size: %x", mi4header.mi4size);
|
2007-03-16 14:28:00 +00:00
|
|
|
|
2007-03-20 12:35:45 +00:00
|
|
|
if ((mi4header.mi4size-MI4_HEADER_SIZE) > buffer_size)
|
2007-03-16 14:28:00 +00:00
|
|
|
return EFILE_TOO_BIG;
|
|
|
|
|
|
|
|
/* CRC32 */
|
|
|
|
printf("CRC32: %x", mi4header.crc32);
|
|
|
|
|
|
|
|
/* Rockbox model id */
|
2007-03-19 11:20:22 +00:00
|
|
|
printf("Model id: %.4s", mi4header.model);
|
2007-03-16 14:28:00 +00:00
|
|
|
|
|
|
|
/* Read binary type (RBOS, RBBL) */
|
2007-03-19 11:20:22 +00:00
|
|
|
printf("Binary type: %.4s", mi4header.type);
|
2007-03-16 14:28:00 +00:00
|
|
|
|
|
|
|
/* Load firmware */
|
2007-06-30 02:08:27 +00:00
|
|
|
ata_read_sectors(IF_MV2(0,) pinfo->start + PPMI_SECTOR_OFFSET + PPMI_SECTORS
|
2007-03-17 19:07:20 +00:00
|
|
|
+ (ppmi_header.length/512) + MI4_HEADER_SECTORS,
|
2007-03-20 12:35:45 +00:00
|
|
|
(mi4header.mi4size-MI4_HEADER_SIZE)/512, buf);
|
2007-03-16 14:28:00 +00:00
|
|
|
|
|
|
|
/* Check CRC32 to see if we have a valid file */
|
2007-03-17 19:07:20 +00:00
|
|
|
sum = chksum_crc32 (buf,mi4header.mi4size-MI4_HEADER_SIZE);
|
2007-03-16 14:28:00 +00:00
|
|
|
|
|
|
|
printf("Calculated CRC32: %x", sum);
|
|
|
|
|
|
|
|
if(sum != mi4header.crc32)
|
|
|
|
return EBAD_CHKSUM;
|
|
|
|
|
2007-03-22 12:45:19 +00:00
|
|
|
if (disable_rebuild)
|
|
|
|
{
|
|
|
|
char block[512];
|
|
|
|
int sector = 0, offset = 0;
|
2007-05-20 03:08:00 +00:00
|
|
|
unsigned int i;
|
2007-03-22 12:45:19 +00:00
|
|
|
/* check which known version we have */
|
|
|
|
/* These are taken from the PPPS section, 0x00780240 */
|
2007-06-30 02:08:27 +00:00
|
|
|
ata_read_sectors(IF_MV2(0,) pinfo->start + 0x3C01, 1, block);
|
2007-05-20 03:08:00 +00:00
|
|
|
for (i=0; i<sizeof(OFDatabaseOffsets)/sizeof(*OFDatabaseOffsets); i++)
|
|
|
|
{
|
|
|
|
if (!memcmp(&block[0x40], OFDatabaseOffsets[i].version,
|
|
|
|
OFDatabaseOffsets[i].version_length))
|
|
|
|
{
|
|
|
|
sector = pinfo->start + OFDatabaseOffsets[i].sector;
|
|
|
|
offset = OFDatabaseOffsets[i].offset;
|
|
|
|
break;
|
|
|
|
}
|
2007-03-22 12:45:19 +00:00
|
|
|
}
|
2007-05-20 03:08:00 +00:00
|
|
|
if (sector && offset)
|
|
|
|
{
|
2007-06-30 02:08:27 +00:00
|
|
|
ata_read_sectors(IF_MV2(0,) sector, 1, block);
|
2007-05-20 03:08:00 +00:00
|
|
|
block[offset] = 0;
|
2007-06-30 02:08:27 +00:00
|
|
|
ata_write_sectors(IF_MV2(0,) sector, 1, block);
|
2007-03-22 12:45:19 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-16 14:28:00 +00:00
|
|
|
return EOK;
|
|
|
|
}
|
2007-03-16 14:36:14 +00:00
|
|
|
#endif
|
2007-03-16 14:28:00 +00:00
|
|
|
|
2006-08-28 08:11:32 +00:00
|
|
|
void* main(void)
|
|
|
|
{
|
|
|
|
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
|
|
|
struct partinfo* pinfo;
|
2007-09-06 03:28:58 +00:00
|
|
|
#if defined(SANSA_E200) || defined(SANSA_C200)
|
2007-03-22 12:45:19 +00:00
|
|
|
int usb_retry = 0;
|
|
|
|
bool usb = false;
|
2007-09-06 03:28:58 +00:00
|
|
|
#else
|
|
|
|
char buf[256];
|
|
|
|
unsigned short* identify_info;
|
2007-03-22 12:45:19 +00:00
|
|
|
#endif
|
2006-08-28 08:11:32 +00:00
|
|
|
|
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();
|
2007-10-14 18:20:23 +00:00
|
|
|
#if defined(SANSA_E200)
|
|
|
|
i2c_init();
|
|
|
|
__backlight_on();
|
|
|
|
#endif
|
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();
|
2007-09-06 03:28:58 +00:00
|
|
|
#if defined(SANSA_E200) || defined(SANSA_C200)
|
2007-03-22 12:45:19 +00:00
|
|
|
usb_init();
|
2007-09-04 08:14:47 +00:00
|
|
|
while ((UDC_OTGSC&0x800) && usb_retry < 5 && !usb)
|
2007-03-22 12:45:19 +00:00
|
|
|
{
|
|
|
|
usb_retry++;
|
|
|
|
sleep(HZ/4);
|
2007-09-04 08:03:07 +00:00
|
|
|
usb = (usb_detect() == USB_INSERTED);
|
2007-03-22 12:45:19 +00:00
|
|
|
}
|
|
|
|
if (usb)
|
|
|
|
btn |= BOOTLOADER_BOOT_OF;
|
|
|
|
#endif
|
2007-03-20 10:36:26 +00:00
|
|
|
/* Enable bootloader messages if any button is pressed */
|
|
|
|
if (btn)
|
|
|
|
verbose = true;
|
2007-03-12 22:12:20 +00:00
|
|
|
|
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");
|
2007-03-19 11:20:22 +00:00
|
|
|
printf("Version: %s", version);
|
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(MODEL_NAME);
|
2006-08-28 08:11:32 +00:00
|
|
|
|
|
|
|
i=ata_init();
|
2007-09-06 03:28:58 +00:00
|
|
|
#if !defined(SANSA_E200) && !defined(SANSA_C200)
|
2006-08-28 08:11:32 +00:00
|
|
|
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
|
|
|
}
|
2007-06-30 02:08:27 +00:00
|
|
|
#endif
|
2006-08-28 08:11:32 +00:00
|
|
|
|
2007-06-30 02:08:27 +00:00
|
|
|
disk_init(IF_MV(0));
|
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 */
|
2007-03-20 22:18:35 +00:00
|
|
|
for(i=0; i<NUM_PARTITIONS; i++)
|
2007-03-16 14:28:00 +00:00
|
|
|
{
|
|
|
|
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:36:14 +00:00
|
|
|
|
2007-09-06 03:28:58 +00:00
|
|
|
#if defined(SANSA_E200) || defined(SANSA_C200)
|
2007-03-16 14:36:14 +00:00
|
|
|
/* First try a (hidden) firmware partition */
|
|
|
|
printf("Trying firmware partition");
|
2007-03-16 14:28:00 +00:00
|
|
|
pinfo = disk_partinfo(1);
|
2007-03-16 14:41:55 +00:00
|
|
|
if(pinfo->type == PARTITION_TYPE_OS2_HIDDEN_C_DRIVE)
|
2007-03-16 14:28:00 +00:00
|
|
|
{
|
2007-03-22 12:45:19 +00:00
|
|
|
rc = load_mi4_part(loadbuffer, pinfo, MAX_LOADSIZE, usb);
|
2007-03-16 14:28:00 +00:00
|
|
|
if (rc < EOK) {
|
|
|
|
printf("Can't load from partition");
|
|
|
|
printf(strerror(rc));
|
|
|
|
} else {
|
|
|
|
return (void*)loadbuffer;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("No hidden partition found.");
|
|
|
|
}
|
2007-03-16 14:36:14 +00:00
|
|
|
#endif
|
2007-03-16 14:28:00 +00:00
|
|
|
|
|
|
|
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-20 10:36:26 +00:00
|
|
|
|
2007-03-20 20:45:25 +00:00
|
|
|
error(0, 0);
|
2007-03-16 14:28:00 +00:00
|
|
|
|
2006-08-28 08:11:32 +00:00
|
|
|
} else {
|
2007-05-17 11:35:57 +00:00
|
|
|
#if 0 /* e200: enable to be able to dump the hidden partition */
|
|
|
|
if(btn & BUTTON_UP)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
pinfo = disk_partinfo(1);
|
|
|
|
fd = open("/part.bin", O_CREAT|O_RDWR);
|
|
|
|
char sector[512];
|
|
|
|
for(i=0; i<40960; i++){
|
2007-05-20 03:08:00 +00:00
|
|
|
if (!(i%100))
|
|
|
|
{
|
|
|
|
printf("dumping sector %d", i);
|
|
|
|
}
|
2007-10-20 15:41:44 +00:00
|
|
|
ata_read_sectors(IF_MV2(0,) pinfo->start + i, 1, sector);
|
2007-05-17 11:35:57 +00:00
|
|
|
write(fd,sector,512);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
#endif
|
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-17 19:07:20 +00:00
|
|
|
printf(strerror(rc));
|
|
|
|
|
2007-09-06 03:28:58 +00:00
|
|
|
#ifdef OLD_BOOTFILE
|
2007-03-17 19:07:20 +00:00
|
|
|
/* Try loading rockbox from old rockbox.e200/rockbox.h10 format */
|
|
|
|
rc=load_firmware(loadbuffer, OLD_BOOTFILE, MAX_LOADSIZE);
|
|
|
|
if (rc < EOK) {
|
|
|
|
printf("Can't load %s:", OLD_BOOTFILE);
|
2007-03-20 10:36:26 +00:00
|
|
|
error(EBOOTFILE, rc);
|
2007-03-17 19:07:20 +00:00
|
|
|
}
|
2007-09-06 03:28:58 +00:00
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
2007-09-06 03:28:58 +00:00
|
|
|
#if !defined(SANSA_E200) && !defined(SANSA_C200)
|
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)
|
|
|
|
{
|
|
|
|
}
|
2007-03-22 12:45:19 +00:00
|
|
|
#endif
|
|
|
|
|