Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2006 by Barry Wardell |
| 11 | * |
| 12 | * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing |
| 13 | * and the ipodlinux bootloader by Daniel Palffy and Bernard Leach |
| 14 | * |
| 15 | * All files in this archive are subject to the GNU General Public License. |
| 16 | * See the file COPYING in the source tree root for full license agreement. |
| 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | ****************************************************************************/ |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 22 | #include "common.h" |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 23 | #include "cpu.h" |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 24 | #include "file.h" |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 25 | #include "system.h" |
| 26 | #include "kernel.h" |
| 27 | #include "lcd.h" |
| 28 | #include "font.h" |
| 29 | #include "ata.h" |
| 30 | #include "button.h" |
| 31 | #include "disk.h" |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 32 | #include <string.h> |
| 33 | |
Barry Wardell | 3de41f5 | 2007-03-20 20:45:25 +0000 | [diff] [blame] | 34 | /* Locations and sizes in hidden partition on Sansa */ |
Barry Wardell | 7cd559d | 2007-03-20 22:18:35 +0000 | [diff] [blame] | 35 | #ifdef SANSA_E200 |
Barry Wardell | 3de41f5 | 2007-03-20 20:45:25 +0000 | [diff] [blame] | 36 | #define PPMI_SECTOR_OFFSET 1024 |
| 37 | #define PPMI_SECTORS 1 |
| 38 | #define MI4_HEADER_SECTORS 1 |
Barry Wardell | 7cd559d | 2007-03-20 22:18:35 +0000 | [diff] [blame] | 39 | #define NUM_PARTITIONS 2 |
| 40 | |
| 41 | #else |
Barry Wardell | 7cd559d | 2007-03-20 22:18:35 +0000 | [diff] [blame] | 42 | #define NUM_PARTITIONS 1 |
| 43 | |
| 44 | #endif |
Barry Wardell | 3de41f5 | 2007-03-20 20:45:25 +0000 | [diff] [blame] | 45 | |
Barry Wardell | 7e03b0a | 2007-03-20 22:56:51 +0000 | [diff] [blame] | 46 | #define MI4_HEADER_SIZE 0x200 |
| 47 | |
Barry Wardell | 3de41f5 | 2007-03-20 20:45:25 +0000 | [diff] [blame] | 48 | /* mi4 header structure */ |
| 49 | struct mi4header_t { |
| 50 | unsigned char magic[4]; |
| 51 | uint32_t version; |
| 52 | uint32_t length; |
| 53 | uint32_t crc32; |
| 54 | uint32_t enctype; |
| 55 | uint32_t mi4size; |
| 56 | uint32_t plaintext; |
| 57 | uint32_t dsa_key[10]; |
| 58 | uint32_t pad[109]; |
| 59 | unsigned char type[4]; |
| 60 | unsigned char model[4]; |
| 61 | }; |
| 62 | |
| 63 | /* PPMI header structure */ |
| 64 | struct ppmi_header_t { |
| 65 | unsigned char magic[4]; |
| 66 | uint32_t length; |
| 67 | uint32_t pad[126]; |
| 68 | }; |
| 69 | |
| 70 | inline unsigned int le2int(unsigned char* buf) |
| 71 | { |
| 72 | int32_t res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; |
| 73 | |
| 74 | return res; |
| 75 | } |
| 76 | |
| 77 | inline void int2le(unsigned int val, unsigned char* addr) |
| 78 | { |
| 79 | addr[0] = val & 0xFF; |
| 80 | addr[1] = (val >> 8) & 0xff; |
| 81 | addr[2] = (val >> 16) & 0xff; |
| 82 | addr[3] = (val >> 24) & 0xff; |
| 83 | } |
| 84 | |
| 85 | struct tea_key { |
| 86 | const char * name; |
| 87 | uint32_t key[4]; |
| 88 | }; |
| 89 | |
| 90 | #define NUM_KEYS 11 |
| 91 | struct tea_key tea_keytable[] = { |
| 92 | { "default" , { 0x20d36cc0, 0x10e8c07d, 0xc0e7dcaa, 0x107eb080 } }, |
| 93 | { "sansa", { 0xe494e96e, 0x3ee32966, 0x6f48512b, 0xa93fbb42 } }, |
| 94 | { "sansa_gh", { 0xd7b10538, 0xc662945b, 0x1b3fce68, 0xf389c0e6 } }, |
| 95 | { "rhapsody", { 0x7aa9c8dc, 0xbed0a82a, 0x16204cc7, 0x5904ef38 } }, |
| 96 | { "p610", { 0x950e83dc, 0xec4907f9, 0x023734b9, 0x10cfb7c7 } }, |
| 97 | { "p640", { 0x220c5f23, 0xd04df68e, 0x431b5e25, 0x4dcc1fa1 } }, |
| 98 | { "virgin", { 0xe83c29a1, 0x04862973, 0xa9b3f0d4, 0x38be2a9c } }, |
| 99 | { "20gc_eng", { 0x0240772c, 0x6f3329b5, 0x3ec9a6c5, 0xb0c9e493 } }, |
| 100 | { "20gc_fre", { 0xbede8817, 0xb23bfe4f, 0x80aa682d, 0xd13f598c } }, |
| 101 | { "elio_p722", { 0x6af3b9f8, 0x777483f5, 0xae8181cc, 0xfa6d8a84 } }, |
| 102 | { "c200", { 0xbf2d06fa, 0xf0e23d59, 0x29738132, 0xe2d04ca7 } }, |
| 103 | }; |
| 104 | |
| 105 | /* |
| 106 | |
| 107 | tea_decrypt() from http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm |
| 108 | |
| 109 | "Following is an adaptation of the reference encryption and decryption |
| 110 | routines in C, released into the public domain by David Wheeler and |
| 111 | Roger Needham:" |
| 112 | |
| 113 | */ |
| 114 | |
| 115 | /* NOTE: The mi4 version of TEA uses a different initial value to sum compared |
| 116 | to the reference implementation and the main loop is 8 iterations, not |
| 117 | 32. |
| 118 | */ |
| 119 | |
| 120 | static void tea_decrypt(uint32_t* v0, uint32_t* v1, uint32_t* k) { |
| 121 | uint32_t sum=0xF1BBCDC8, i; /* set up */ |
| 122 | uint32_t delta=0x9E3779B9; /* a key schedule constant */ |
| 123 | uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* cache key */ |
| 124 | for(i=0; i<8; i++) { /* basic cycle start */ |
| 125 | *v1 -= ((*v0<<4) + k2) ^ (*v0 + sum) ^ ((*v0>>5) + k3); |
| 126 | *v0 -= ((*v1<<4) + k0) ^ (*v1 + sum) ^ ((*v1>>5) + k1); |
| 127 | sum -= delta; /* end cycle */ |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /* mi4 files are encrypted in 64-bit blocks (two little-endian 32-bit |
| 132 | integers) and the key is incremented after each block |
| 133 | */ |
| 134 | |
| 135 | static void tea_decrypt_buf(unsigned char* src, unsigned char* dest, size_t n, uint32_t * key) |
| 136 | { |
| 137 | uint32_t v0, v1; |
| 138 | unsigned int i; |
| 139 | |
| 140 | for (i = 0; i < (n / 8); i++) { |
| 141 | v0 = le2int(src); |
| 142 | v1 = le2int(src+4); |
| 143 | |
| 144 | tea_decrypt(&v0, &v1, key); |
| 145 | |
| 146 | int2le(v0, dest); |
| 147 | int2le(v1, dest+4); |
| 148 | |
| 149 | src += 8; |
| 150 | dest += 8; |
| 151 | |
| 152 | /* Now increment the key */ |
| 153 | key[0]++; |
| 154 | if (key[0]==0) { |
| 155 | key[1]++; |
| 156 | if (key[1]==0) { |
| 157 | key[2]++; |
| 158 | if (key[2]==0) { |
| 159 | key[3]++; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | static inline bool tea_test_key(unsigned char magic_enc[8], uint32_t * key, int unaligned) |
| 167 | { |
| 168 | unsigned char magic_dec[8]; |
| 169 | tea_decrypt_buf(magic_enc, magic_dec, 8, key); |
| 170 | |
| 171 | return (le2int(&magic_dec[4*unaligned]) == 0xaa55aa55); |
| 172 | } |
| 173 | |
| 174 | static int tea_find_key(struct mi4header_t *mi4header, int fd) |
| 175 | { |
| 176 | int i, rc; |
| 177 | unsigned int j; |
| 178 | uint32_t key[4]; |
| 179 | unsigned char magic_enc[8]; |
| 180 | int key_found = -1; |
| 181 | unsigned int magic_location = mi4header->length-4; |
| 182 | int unaligned = 0; |
| 183 | |
| 184 | if ( (magic_location % 8) != 0 ) |
| 185 | { |
| 186 | unaligned = 1; |
| 187 | magic_location -= 4; |
| 188 | } |
| 189 | |
| 190 | /* Load encrypted magic 0xaa55aa55 to check key */ |
| 191 | lseek(fd, MI4_HEADER_SIZE + magic_location, SEEK_SET); |
| 192 | rc = read(fd, magic_enc, 8); |
| 193 | if(rc < 8 ) |
| 194 | return EREAD_IMAGE_FAILED; |
| 195 | |
Barry Wardell | 7cd559d | 2007-03-20 22:18:35 +0000 | [diff] [blame] | 196 | printf("Searching for key:"); |
Barry Wardell | 3de41f5 | 2007-03-20 20:45:25 +0000 | [diff] [blame] | 197 | |
| 198 | for (i=0; i < NUM_KEYS && (key_found<0) ; i++) { |
| 199 | key[0] = tea_keytable[i].key[0]; |
| 200 | key[1] = tea_keytable[i].key[1]; |
| 201 | key[2] = tea_keytable[i].key[2]; |
| 202 | key[3] = tea_keytable[i].key[3]; |
| 203 | |
| 204 | /* Now increment the key */ |
| 205 | for(j=0; j<((magic_location-mi4header->plaintext)/8); j++){ |
| 206 | key[0]++; |
| 207 | if (key[0]==0) { |
| 208 | key[1]++; |
| 209 | if (key[1]==0) { |
| 210 | key[2]++; |
| 211 | if (key[2]==0) { |
| 212 | key[3]++; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if (tea_test_key(magic_enc,key,unaligned)) |
| 219 | { |
| 220 | key_found = i; |
| 221 | printf("%s...found", tea_keytable[i].name); |
| 222 | } else { |
Barry Wardell | 7cd559d | 2007-03-20 22:18:35 +0000 | [diff] [blame] | 223 | /* printf("%s...failed", tea_keytable[i].name); */ |
Barry Wardell | 3de41f5 | 2007-03-20 20:45:25 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | return key_found; |
| 228 | } |
| 229 | |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 230 | /* |
Barry Wardell | 9a9aebb | 2007-03-20 22:02:26 +0000 | [diff] [blame] | 231 | * We can't use the CRC32 implementation in the firmware library as it uses a |
| 232 | * different polynomial. The polynomial needed is 0xEDB88320L |
| 233 | * |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 234 | * CRC32 implementation taken from: |
| 235 | * |
| 236 | * efone - Distributed internet phone system. |
| 237 | * |
| 238 | * (c) 1999,2000 Krzysztof Dabrowski |
| 239 | * (c) 1999,2000 ElysiuM deeZine |
| 240 | * |
| 241 | * This program is free software; you can redistribute it and/or |
| 242 | * modify it under the terms of the GNU General Public License |
| 243 | * as published by the Free Software Foundation; either version |
| 244 | * 2 of the License, or (at your option) any later version. |
| 245 | * |
| 246 | */ |
| 247 | |
| 248 | /* based on implementation by Finn Yannick Jacobs */ |
| 249 | |
| 250 | #include <stdio.h> |
| 251 | #include <stdlib.h> |
Jonathan Gordon | a76947f | 2007-03-22 12:45:19 +0000 | [diff] [blame^] | 252 | #ifdef SANSA_E200 |
| 253 | #include "usb.h" |
| 254 | #endif |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 255 | |
| 256 | /* crc_tab[] -- this crcTable is being build by chksum_crc32GenTab(). |
| 257 | * so make sure, you call it before using the other |
| 258 | * functions! |
| 259 | */ |
| 260 | static unsigned int crc_tab[256]; |
| 261 | |
| 262 | /* chksum_crc() -- to a given block, this one calculates the |
| 263 | * crc32-checksum until the length is |
| 264 | * reached. the crc32-checksum will be |
| 265 | * the result. |
| 266 | */ |
| 267 | unsigned int chksum_crc32 (unsigned char *block, unsigned int length) |
| 268 | { |
| 269 | register unsigned long crc; |
| 270 | unsigned long i; |
| 271 | |
| 272 | crc = 0; |
| 273 | for (i = 0; i < length; i++) |
| 274 | { |
| 275 | crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_tab[(crc ^ *block++) & 0xFF]; |
| 276 | } |
| 277 | return (crc); |
| 278 | } |
| 279 | |
| 280 | /* chksum_crc32gentab() -- to a global crc_tab[256], this one will |
| 281 | * calculate the crcTable for crc32-checksums. |
| 282 | * it is generated to the polynom [..] |
| 283 | */ |
| 284 | |
| 285 | static void chksum_crc32gentab (void) |
| 286 | { |
| 287 | unsigned long crc, poly; |
| 288 | int i, j; |
| 289 | |
| 290 | poly = 0xEDB88320L; |
| 291 | for (i = 0; i < 256; i++) |
| 292 | { |
| 293 | crc = i; |
| 294 | for (j = 8; j > 0; j--) |
| 295 | { |
| 296 | if (crc & 1) |
| 297 | { |
| 298 | crc = (crc >> 1) ^ poly; |
| 299 | } |
| 300 | else |
| 301 | { |
| 302 | crc >>= 1; |
| 303 | } |
| 304 | } |
| 305 | crc_tab[i] = crc; |
| 306 | } |
| 307 | } |
Barry Wardell | 2370998 | 2007-03-12 22:12:20 +0000 | [diff] [blame] | 308 | |
| 309 | /* Button definitions */ |
| 310 | #if CONFIG_KEYPAD == IRIVER_H10_PAD |
Barry Wardell | 2370998 | 2007-03-12 22:12:20 +0000 | [diff] [blame] | 311 | #define BOOTLOADER_BOOT_OF BUTTON_LEFT |
| 312 | |
| 313 | #elif CONFIG_KEYPAD == SANSA_E200_PAD |
Barry Wardell | 2370998 | 2007-03-12 22:12:20 +0000 | [diff] [blame] | 314 | #define BOOTLOADER_BOOT_OF BUTTON_LEFT |
| 315 | |
| 316 | #endif |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 317 | |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 318 | /* Maximum allowed firmware image size. 10MB is more than enough */ |
Barry Wardell | 2f16d4f | 2006-12-19 11:33:53 +0000 | [diff] [blame] | 319 | #define MAX_LOADSIZE (10*1024*1024) |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 320 | |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 321 | /* A buffer to load the original firmware or Rockbox into */ |
| 322 | unsigned char *loadbuffer = (unsigned char *)DRAM_START; |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 323 | |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 324 | /* Bootloader version */ |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 325 | char version[] = APPSVERSION; |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 326 | |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 327 | /* Load mi4 format firmware image */ |
| 328 | int load_mi4(unsigned char* buf, char* firmware, unsigned int buffer_size) |
| 329 | { |
| 330 | int fd; |
| 331 | struct mi4header_t mi4header; |
| 332 | int rc; |
| 333 | unsigned long sum; |
| 334 | char filename[MAX_PATH]; |
| 335 | |
| 336 | snprintf(filename,sizeof(filename),"/.rockbox/%s",firmware); |
| 337 | fd = open(filename, O_RDONLY); |
| 338 | if(fd < 0) |
| 339 | { |
| 340 | snprintf(filename,sizeof(filename),"/%s",firmware); |
| 341 | fd = open(filename, O_RDONLY); |
| 342 | if(fd < 0) |
| 343 | return EFILE_NOT_FOUND; |
| 344 | } |
| 345 | |
Barry Wardell | e293bbb | 2007-03-17 19:07:20 +0000 | [diff] [blame] | 346 | read(fd, &mi4header, MI4_HEADER_SIZE); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 347 | |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 348 | /* MI4 file size */ |
Barry Wardell | bada0b7 | 2007-03-20 12:35:45 +0000 | [diff] [blame] | 349 | printf("mi4 size: %x", mi4header.mi4size); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 350 | |
Barry Wardell | bada0b7 | 2007-03-20 12:35:45 +0000 | [diff] [blame] | 351 | if ((mi4header.mi4size-MI4_HEADER_SIZE) > buffer_size) |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 352 | return EFILE_TOO_BIG; |
| 353 | |
| 354 | /* CRC32 */ |
| 355 | printf("CRC32: %x", mi4header.crc32); |
| 356 | |
| 357 | /* Rockbox model id */ |
Barry Wardell | ca8f7bf | 2007-03-19 11:20:22 +0000 | [diff] [blame] | 358 | printf("Model id: %.4s", mi4header.model); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 359 | |
| 360 | /* Read binary type (RBOS, RBBL) */ |
Barry Wardell | ca8f7bf | 2007-03-19 11:20:22 +0000 | [diff] [blame] | 361 | printf("Binary type: %.4s", mi4header.type); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 362 | |
Barry Wardell | 9a9aebb | 2007-03-20 22:02:26 +0000 | [diff] [blame] | 363 | /* Load firmware file */ |
| 364 | lseek(fd, MI4_HEADER_SIZE, SEEK_SET); |
| 365 | rc = read(fd, buf, mi4header.mi4size-MI4_HEADER_SIZE); |
| 366 | if(rc < (int)mi4header.mi4size-MI4_HEADER_SIZE) |
| 367 | return EREAD_IMAGE_FAILED; |
| 368 | |
| 369 | /* Check CRC32 to see if we have a valid file */ |
| 370 | sum = chksum_crc32 (buf, mi4header.mi4size - MI4_HEADER_SIZE); |
| 371 | |
| 372 | printf("Calculated CRC32: %x", sum); |
| 373 | |
| 374 | if(sum != mi4header.crc32) |
| 375 | return EBAD_CHKSUM; |
| 376 | |
| 377 | if( (mi4header.plaintext + MI4_HEADER_SIZE) != mi4header.mi4size) |
Barry Wardell | 3de41f5 | 2007-03-20 20:45:25 +0000 | [diff] [blame] | 378 | { |
| 379 | /* Load encrypted firmware */ |
| 380 | int key_index = tea_find_key(&mi4header, fd); |
Barry Wardell | 3de41f5 | 2007-03-20 20:45:25 +0000 | [diff] [blame] | 381 | |
| 382 | if (key_index < 0) |
| 383 | return EINVALID_FORMAT; |
| 384 | |
Barry Wardell | 9a9aebb | 2007-03-20 22:02:26 +0000 | [diff] [blame] | 385 | /* Plaintext part is already loaded */ |
Barry Wardell | 3de41f5 | 2007-03-20 20:45:25 +0000 | [diff] [blame] | 386 | buf += mi4header.plaintext; |
| 387 | |
Barry Wardell | 9a9aebb | 2007-03-20 22:02:26 +0000 | [diff] [blame] | 388 | /* Decrypt in-place */ |
| 389 | tea_decrypt_buf(buf, buf, |
| 390 | mi4header.mi4size-(mi4header.plaintext+MI4_HEADER_SIZE), |
| 391 | tea_keytable[key_index].key); |
| 392 | |
Barry Wardell | 3de41f5 | 2007-03-20 20:45:25 +0000 | [diff] [blame] | 393 | printf("%s key used", tea_keytable[key_index].name); |
Barry Wardell | 9a9aebb | 2007-03-20 22:02:26 +0000 | [diff] [blame] | 394 | |
| 395 | /* Check decryption was successfull */ |
Barry Wardell | 7cd559d | 2007-03-20 22:18:35 +0000 | [diff] [blame] | 396 | if(le2int(&buf[mi4header.length-mi4header.plaintext-4]) != 0xaa55aa55) |
Barry Wardell | 9a9aebb | 2007-03-20 22:02:26 +0000 | [diff] [blame] | 397 | { |
Barry Wardell | 3de41f5 | 2007-03-20 20:45:25 +0000 | [diff] [blame] | 398 | return EREAD_IMAGE_FAILED; |
Barry Wardell | 9a9aebb | 2007-03-20 22:02:26 +0000 | [diff] [blame] | 399 | } |
Barry Wardell | 3de41f5 | 2007-03-20 20:45:25 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 402 | return EOK; |
| 403 | } |
| 404 | |
Barry Wardell | a91a35b | 2007-03-16 14:36:14 +0000 | [diff] [blame] | 405 | #ifdef SANSA_E200 |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 406 | /* Load mi4 firmware from a hidden disk partition */ |
Jonathan Gordon | a76947f | 2007-03-22 12:45:19 +0000 | [diff] [blame^] | 407 | int load_mi4_part(unsigned char* buf, struct partinfo* pinfo, |
| 408 | unsigned int buffer_size, bool disable_rebuild) |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 409 | { |
| 410 | struct mi4header_t mi4header; |
| 411 | struct ppmi_header_t ppmi_header; |
| 412 | unsigned long sum; |
| 413 | |
| 414 | /* Read header to find out how long the mi4 file is. */ |
Barry Wardell | e293bbb | 2007-03-17 19:07:20 +0000 | [diff] [blame] | 415 | ata_read_sectors(pinfo->start + PPMI_SECTOR_OFFSET, |
| 416 | PPMI_SECTORS, &ppmi_header); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 417 | |
| 418 | /* The first four characters at 0x80000 (sector 1024) should be PPMI*/ |
| 419 | if( memcmp(ppmi_header.magic, "PPMI", 4) ) |
| 420 | return EFILE_NOT_FOUND; |
| 421 | |
| 422 | printf("BL mi4 size: %x", ppmi_header.length); |
| 423 | |
| 424 | /* Read mi4 header of the OF */ |
Barry Wardell | e293bbb | 2007-03-17 19:07:20 +0000 | [diff] [blame] | 425 | ata_read_sectors(pinfo->start + PPMI_SECTOR_OFFSET + PPMI_SECTORS |
| 426 | + (ppmi_header.length/512), MI4_HEADER_SECTORS, &mi4header); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 427 | |
| 428 | /* We don't support encrypted mi4 files yet */ |
Barry Wardell | bada0b7 | 2007-03-20 12:35:45 +0000 | [diff] [blame] | 429 | if( (mi4header.plaintext) != (mi4header.mi4size-MI4_HEADER_SIZE)) |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 430 | return EINVALID_FORMAT; |
| 431 | |
| 432 | /* MI4 file size */ |
Barry Wardell | bada0b7 | 2007-03-20 12:35:45 +0000 | [diff] [blame] | 433 | printf("OF mi4 size: %x", mi4header.mi4size); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 434 | |
Barry Wardell | bada0b7 | 2007-03-20 12:35:45 +0000 | [diff] [blame] | 435 | if ((mi4header.mi4size-MI4_HEADER_SIZE) > buffer_size) |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 436 | return EFILE_TOO_BIG; |
| 437 | |
| 438 | /* CRC32 */ |
| 439 | printf("CRC32: %x", mi4header.crc32); |
| 440 | |
| 441 | /* Rockbox model id */ |
Barry Wardell | ca8f7bf | 2007-03-19 11:20:22 +0000 | [diff] [blame] | 442 | printf("Model id: %.4s", mi4header.model); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 443 | |
| 444 | /* Read binary type (RBOS, RBBL) */ |
Barry Wardell | ca8f7bf | 2007-03-19 11:20:22 +0000 | [diff] [blame] | 445 | printf("Binary type: %.4s", mi4header.type); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 446 | |
| 447 | /* Load firmware */ |
Barry Wardell | e293bbb | 2007-03-17 19:07:20 +0000 | [diff] [blame] | 448 | ata_read_sectors(pinfo->start + PPMI_SECTOR_OFFSET + PPMI_SECTORS |
| 449 | + (ppmi_header.length/512) + MI4_HEADER_SECTORS, |
Barry Wardell | bada0b7 | 2007-03-20 12:35:45 +0000 | [diff] [blame] | 450 | (mi4header.mi4size-MI4_HEADER_SIZE)/512, buf); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 451 | |
| 452 | /* Check CRC32 to see if we have a valid file */ |
Barry Wardell | e293bbb | 2007-03-17 19:07:20 +0000 | [diff] [blame] | 453 | sum = chksum_crc32 (buf,mi4header.mi4size-MI4_HEADER_SIZE); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 454 | |
| 455 | printf("Calculated CRC32: %x", sum); |
| 456 | |
| 457 | if(sum != mi4header.crc32) |
| 458 | return EBAD_CHKSUM; |
| 459 | |
Jonathan Gordon | a76947f | 2007-03-22 12:45:19 +0000 | [diff] [blame^] | 460 | if (disable_rebuild) |
| 461 | { |
| 462 | char block[512]; |
| 463 | int sector = 0, offset = 0; |
| 464 | |
| 465 | /* check which known version we have */ |
| 466 | /* These are taken from the PPPS section, 0x00780240 */ |
| 467 | ata_read_sectors(pinfo->start + 0x3C01, 1, block); |
| 468 | if (!memcmp(&block[0x40], |
| 469 | "PP5022AF-05.51-S301-01.11-S301.01.11A-D", 39)) |
| 470 | { /* American e200, OF version 1.01.11A */ |
| 471 | sector = pinfo->start + 0x3c08; |
| 472 | offset = 0xe1; |
| 473 | } |
| 474 | else if (!memcmp(&block[0x40], |
| 475 | "PP5022AF-05.51-S301-00.12-S301.00.12E-D", 39)) |
| 476 | { /* European e200, OF version 1.00.12 */ |
| 477 | sector = pinfo->start + 0x3c5c; |
| 478 | offset = 0x2; |
| 479 | } |
| 480 | else return EOK; |
| 481 | ata_read_sectors(sector, 1, block); |
| 482 | block[offset] = 0; |
| 483 | ata_write_sectors(sector, 1, block); |
| 484 | } |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 485 | return EOK; |
| 486 | } |
Barry Wardell | a91a35b | 2007-03-16 14:36:14 +0000 | [diff] [blame] | 487 | #endif |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 488 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 489 | void* main(void) |
| 490 | { |
| 491 | char buf[256]; |
| 492 | int i; |
Barry Wardell | 2370998 | 2007-03-12 22:12:20 +0000 | [diff] [blame] | 493 | int btn; |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 494 | int rc; |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 495 | int num_partitions; |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 496 | unsigned short* identify_info; |
| 497 | struct partinfo* pinfo; |
Jonathan Gordon | a76947f | 2007-03-22 12:45:19 +0000 | [diff] [blame^] | 498 | #ifdef SANSA_E200 |
| 499 | int usb_retry = 0; |
| 500 | bool usb = false; |
| 501 | #endif |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 502 | |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 503 | chksum_crc32gentab (); |
| 504 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 505 | system_init(); |
| 506 | kernel_init(); |
| 507 | lcd_init(); |
| 508 | font_init(); |
Barry Wardell | 2f16d4f | 2006-12-19 11:33:53 +0000 | [diff] [blame] | 509 | button_init(); |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 510 | |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 511 | lcd_set_foreground(LCD_WHITE); |
| 512 | lcd_set_background(LCD_BLACK); |
| 513 | lcd_clear_display(); |
| 514 | |
Barry Wardell | 2370998 | 2007-03-12 22:12:20 +0000 | [diff] [blame] | 515 | btn = button_read_device(); |
Jonathan Gordon | a76947f | 2007-03-22 12:45:19 +0000 | [diff] [blame^] | 516 | #ifdef SANSA_E200 |
| 517 | usb_init(); |
| 518 | while (usb_retry < 5 && !usb) |
| 519 | { |
| 520 | usb_retry++; |
| 521 | sleep(HZ/4); |
| 522 | usb = usb_detect(); |
| 523 | } |
| 524 | if (usb) |
| 525 | btn |= BOOTLOADER_BOOT_OF; |
| 526 | #endif |
Barry Wardell | 0c1a304 | 2007-03-20 10:36:26 +0000 | [diff] [blame] | 527 | /* Enable bootloader messages if any button is pressed */ |
| 528 | if (btn) |
| 529 | verbose = true; |
Barry Wardell | 2370998 | 2007-03-12 22:12:20 +0000 | [diff] [blame] | 530 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 531 | lcd_setfont(FONT_SYSFIXED); |
| 532 | |
Barry Wardell | f4709d0 | 2007-01-17 12:20:38 +0000 | [diff] [blame] | 533 | printf("Rockbox boot loader"); |
Barry Wardell | ca8f7bf | 2007-03-19 11:20:22 +0000 | [diff] [blame] | 534 | printf("Version: %s", version); |
Barry Wardell | f4709d0 | 2007-01-17 12:20:38 +0000 | [diff] [blame] | 535 | printf(MODEL_NAME); |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 536 | |
| 537 | i=ata_init(); |
| 538 | if (i==0) { |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 539 | identify_info=ata_get_identify(); |
| 540 | /* Show model */ |
| 541 | for (i=0; i < 20; i++) { |
| 542 | ((unsigned short*)buf)[i]=htobe16(identify_info[i+27]); |
| 543 | } |
| 544 | buf[40]=0; |
| 545 | for (i=39; i && buf[i]==' '; i--) { |
| 546 | buf[i]=0; |
| 547 | } |
| 548 | printf(buf); |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 549 | } else { |
Barry Wardell | 2370998 | 2007-03-12 22:12:20 +0000 | [diff] [blame] | 550 | error(EATA, i); |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | disk_init(); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 554 | num_partitions = disk_mount_all(); |
| 555 | if (num_partitions<=0) |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 556 | { |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 557 | error(EDISK,num_partitions); |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 560 | /* Just list the first 2 partitions since we don't have any devices yet |
| 561 | that have more than that */ |
Barry Wardell | 7cd559d | 2007-03-20 22:18:35 +0000 | [diff] [blame] | 562 | for(i=0; i<NUM_PARTITIONS; i++) |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 563 | { |
| 564 | pinfo = disk_partinfo(i); |
| 565 | printf("Partition %d: 0x%02x %ld MB", |
| 566 | i, pinfo->type, pinfo->size / 2048); |
| 567 | } |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 568 | |
Barry Wardell | a42070d | 2007-03-15 22:32:58 +0000 | [diff] [blame] | 569 | if(btn & BOOTLOADER_BOOT_OF) |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 570 | { |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 571 | /* Load original mi4 firmware in to a memory buffer called loadbuffer. |
| 572 | The rest of the loading is done in crt0.S. |
| 573 | 1) First try reading from the hidden partition (on Sansa only). |
| 574 | 2) Next try a decrypted mi4 file in /System/OF.mi4 |
| 575 | 3) Finally, try a raw firmware binary in /System/OF.mi4. It should be |
| 576 | a mi4 firmware decrypted and header stripped using mi4code. |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 577 | */ |
Barry Wardell | f4709d0 | 2007-01-17 12:20:38 +0000 | [diff] [blame] | 578 | printf("Loading original firmware..."); |
Barry Wardell | a91a35b | 2007-03-16 14:36:14 +0000 | [diff] [blame] | 579 | |
| 580 | #ifdef SANSA_E200 |
| 581 | /* First try a (hidden) firmware partition */ |
| 582 | printf("Trying firmware partition"); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 583 | pinfo = disk_partinfo(1); |
Barry Wardell | b8a5adf | 2007-03-16 14:41:55 +0000 | [diff] [blame] | 584 | if(pinfo->type == PARTITION_TYPE_OS2_HIDDEN_C_DRIVE) |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 585 | { |
Jonathan Gordon | a76947f | 2007-03-22 12:45:19 +0000 | [diff] [blame^] | 586 | rc = load_mi4_part(loadbuffer, pinfo, MAX_LOADSIZE, usb); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 587 | if (rc < EOK) { |
| 588 | printf("Can't load from partition"); |
| 589 | printf(strerror(rc)); |
| 590 | } else { |
| 591 | return (void*)loadbuffer; |
| 592 | } |
| 593 | } else { |
| 594 | printf("No hidden partition found."); |
| 595 | } |
Barry Wardell | a91a35b | 2007-03-16 14:36:14 +0000 | [diff] [blame] | 596 | #endif |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 597 | |
| 598 | printf("Trying /System/OF.mi4"); |
| 599 | rc=load_mi4(loadbuffer, "/System/OF.mi4", MAX_LOADSIZE); |
| 600 | if (rc < EOK) { |
| 601 | printf("Can't load /System/OF.mi4"); |
| 602 | printf(strerror(rc)); |
| 603 | } else { |
| 604 | return (void*)loadbuffer; |
| 605 | } |
| 606 | |
| 607 | printf("Trying /System/OF.bin"); |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 608 | rc=load_raw_firmware(loadbuffer, "/System/OF.bin", MAX_LOADSIZE); |
| 609 | if (rc < EOK) { |
Barry Wardell | 2370998 | 2007-03-12 22:12:20 +0000 | [diff] [blame] | 610 | printf("Can't load /System/OF.bin"); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 611 | printf(strerror(rc)); |
| 612 | } else { |
| 613 | return (void*)loadbuffer; |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 614 | } |
Barry Wardell | 0c1a304 | 2007-03-20 10:36:26 +0000 | [diff] [blame] | 615 | |
Barry Wardell | 3de41f5 | 2007-03-20 20:45:25 +0000 | [diff] [blame] | 616 | error(0, 0); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 617 | |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 618 | } else { |
Barry Wardell | f4709d0 | 2007-01-17 12:20:38 +0000 | [diff] [blame] | 619 | printf("Loading Rockbox..."); |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 620 | rc=load_mi4(loadbuffer, BOOTFILE, MAX_LOADSIZE); |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 621 | if (rc < EOK) { |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 622 | printf("Can't load %s:", BOOTFILE); |
Barry Wardell | e293bbb | 2007-03-17 19:07:20 +0000 | [diff] [blame] | 623 | printf(strerror(rc)); |
| 624 | |
| 625 | /* Try loading rockbox from old rockbox.e200/rockbox.h10 format */ |
| 626 | rc=load_firmware(loadbuffer, OLD_BOOTFILE, MAX_LOADSIZE); |
| 627 | if (rc < EOK) { |
| 628 | printf("Can't load %s:", OLD_BOOTFILE); |
Barry Wardell | 0c1a304 | 2007-03-20 10:36:26 +0000 | [diff] [blame] | 629 | error(EBOOTFILE, rc); |
Barry Wardell | e293bbb | 2007-03-17 19:07:20 +0000 | [diff] [blame] | 630 | } |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 631 | } |
Barry Wardell | 1920df3 | 2006-08-28 08:11:32 +0000 | [diff] [blame] | 632 | } |
Hristo Kovachev | 1204136 | 2006-08-11 09:51:04 +0000 | [diff] [blame] | 633 | |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 634 | return (void*)loadbuffer; |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Jonathan Gordon | a76947f | 2007-03-22 12:45:19 +0000 | [diff] [blame^] | 637 | #ifndef SANSA_E200 |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 638 | /* These functions are present in the firmware library, but we reimplement |
| 639 | them here because the originals do a lot more than we want */ |
Hristo Kovachev | 9dc0e62 | 2006-08-11 08:35:27 +0000 | [diff] [blame] | 640 | void usb_acknowledge(void) |
| 641 | { |
| 642 | } |
| 643 | |
| 644 | void usb_wait_for_disconnect(void) |
| 645 | { |
| 646 | } |
Jonathan Gordon | a76947f | 2007-03-22 12:45:19 +0000 | [diff] [blame^] | 647 | #endif |
| 648 | |