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