blob: 687a7271a6fbf6a2a35c3e624a7130c17e31b1cd [file] [log] [blame]
Hristo Kovachev9dc0e622006-08-11 08:35:27 +00001/***************************************************************************
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 Wardell84b509d2007-01-28 18:42:11 +000022#include "common.h"
Hristo Kovachev9dc0e622006-08-11 08:35:27 +000023#include "cpu.h"
Hristo Kovachev9dc0e622006-08-11 08:35:27 +000024#include "file.h"
Barry Wardell84b509d2007-01-28 18:42:11 +000025#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 Wardell14ed3ca2007-03-16 14:28:00 +000032#include <string.h>
33
Barry Wardell3de41f52007-03-20 20:45:25 +000034/* 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 */
41struct 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 */
56struct ppmi_header_t {
57 unsigned char magic[4];
58 uint32_t length;
59 uint32_t pad[126];
60};
61
62inline 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
69inline 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
77struct tea_key {
78 const char * name;
79 uint32_t key[4];
80};
81
82#define NUM_KEYS 11
83struct 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
99tea_decrypt() from http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
100
101"Following is an adaptation of the reference encryption and decryption
102routines in C, released into the public domain by David Wheeler and
103Roger 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
112static 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
127static 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
158static 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
166static 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 Wardell14ed3ca2007-03-16 14:28:00 +0000222/*
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 */
246static 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 */
253unsigned 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
271static 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 Wardell23709982007-03-12 22:12:20 +0000294
295/* Button definitions */
296#if CONFIG_KEYPAD == IRIVER_H10_PAD
Barry Wardell23709982007-03-12 22:12:20 +0000297#define BOOTLOADER_BOOT_OF BUTTON_LEFT
298
299#elif CONFIG_KEYPAD == SANSA_E200_PAD
Barry Wardell23709982007-03-12 22:12:20 +0000300#define BOOTLOADER_BOOT_OF BUTTON_LEFT
301
302#endif
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000303
Barry Wardell84b509d2007-01-28 18:42:11 +0000304/* Maximum allowed firmware image size. 10MB is more than enough */
Barry Wardell2f16d4f2006-12-19 11:33:53 +0000305#define MAX_LOADSIZE (10*1024*1024)
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000306
Barry Wardell84b509d2007-01-28 18:42:11 +0000307/* A buffer to load the original firmware or Rockbox into */
308unsigned char *loadbuffer = (unsigned char *)DRAM_START;
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000309
Barry Wardell84b509d2007-01-28 18:42:11 +0000310/* Bootloader version */
Barry Wardell1920df32006-08-28 08:11:32 +0000311char version[] = APPSVERSION;
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000312
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000313/* Load mi4 format firmware image */
314int load_mi4(unsigned char* buf, char* firmware, unsigned int buffer_size)
315{
316 int fd;
317 struct mi4header_t mi4header;
318 int rc;
Barry Wardell3de41f52007-03-20 20:45:25 +0000319 unsigned int i;
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000320 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 Wardelle293bbb2007-03-17 19:07:20 +0000333 read(fd, &mi4header, MI4_HEADER_SIZE);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000334
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000335 /* MI4 file size */
Barry Wardellbada0b72007-03-20 12:35:45 +0000336 printf("mi4 size: %x", mi4header.mi4size);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000337
Barry Wardellbada0b72007-03-20 12:35:45 +0000338 if ((mi4header.mi4size-MI4_HEADER_SIZE) > buffer_size)
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000339 return EFILE_TOO_BIG;
340
341 /* CRC32 */
342 printf("CRC32: %x", mi4header.crc32);
343
344 /* Rockbox model id */
Barry Wardellca8f7bf2007-03-19 11:20:22 +0000345 printf("Model id: %.4s", mi4header.model);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000346
347 /* Read binary type (RBOS, RBBL) */
Barry Wardellca8f7bf2007-03-19 11:20:22 +0000348 printf("Binary type: %.4s", mi4header.type);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000349
Barry Wardell3de41f52007-03-20 20:45:25 +0000350 /* 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 Wardell14ed3ca2007-03-16 14:28:00 +0000391
Barry Wardell3de41f52007-03-20 20:45:25 +0000392 printf("Calculated CRC32: %x", sum);
393
394 if(sum != mi4header.crc32)
395 return EBAD_CHKSUM;
396 }
397
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000398 return EOK;
399}
400
Barry Wardella91a35b2007-03-16 14:36:14 +0000401#ifdef SANSA_E200
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000402/* Load mi4 firmware from a hidden disk partition */
403int 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 Wardelle293bbb2007-03-17 19:07:20 +0000410 ata_read_sectors(pinfo->start + PPMI_SECTOR_OFFSET,
411 PPMI_SECTORS, &ppmi_header);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000412
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 Wardelle293bbb2007-03-17 19:07:20 +0000420 ata_read_sectors(pinfo->start + PPMI_SECTOR_OFFSET + PPMI_SECTORS
421 + (ppmi_header.length/512), MI4_HEADER_SECTORS, &mi4header);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000422
423 /* We don't support encrypted mi4 files yet */
Barry Wardellbada0b72007-03-20 12:35:45 +0000424 if( (mi4header.plaintext) != (mi4header.mi4size-MI4_HEADER_SIZE))
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000425 return EINVALID_FORMAT;
426
427 /* MI4 file size */
Barry Wardellbada0b72007-03-20 12:35:45 +0000428 printf("OF mi4 size: %x", mi4header.mi4size);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000429
Barry Wardellbada0b72007-03-20 12:35:45 +0000430 if ((mi4header.mi4size-MI4_HEADER_SIZE) > buffer_size)
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000431 return EFILE_TOO_BIG;
432
433 /* CRC32 */
434 printf("CRC32: %x", mi4header.crc32);
435
436 /* Rockbox model id */
Barry Wardellca8f7bf2007-03-19 11:20:22 +0000437 printf("Model id: %.4s", mi4header.model);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000438
439 /* Read binary type (RBOS, RBBL) */
Barry Wardellca8f7bf2007-03-19 11:20:22 +0000440 printf("Binary type: %.4s", mi4header.type);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000441
442 /* Load firmware */
Barry Wardelle293bbb2007-03-17 19:07:20 +0000443 ata_read_sectors(pinfo->start + PPMI_SECTOR_OFFSET + PPMI_SECTORS
444 + (ppmi_header.length/512) + MI4_HEADER_SECTORS,
Barry Wardellbada0b72007-03-20 12:35:45 +0000445 (mi4header.mi4size-MI4_HEADER_SIZE)/512, buf);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000446
447 /* Check CRC32 to see if we have a valid file */
Barry Wardelle293bbb2007-03-17 19:07:20 +0000448 sum = chksum_crc32 (buf,mi4header.mi4size-MI4_HEADER_SIZE);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000449
450 printf("Calculated CRC32: %x", sum);
451
452 if(sum != mi4header.crc32)
453 return EBAD_CHKSUM;
454
455 return EOK;
456}
Barry Wardella91a35b2007-03-16 14:36:14 +0000457#endif
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000458
Barry Wardell1920df32006-08-28 08:11:32 +0000459void* main(void)
460{
461 char buf[256];
462 int i;
Barry Wardell23709982007-03-12 22:12:20 +0000463 int btn;
Barry Wardell1920df32006-08-28 08:11:32 +0000464 int rc;
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000465 int num_partitions;
Barry Wardell1920df32006-08-28 08:11:32 +0000466 unsigned short* identify_info;
467 struct partinfo* pinfo;
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000468
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000469 chksum_crc32gentab ();
470
Barry Wardell1920df32006-08-28 08:11:32 +0000471 system_init();
472 kernel_init();
473 lcd_init();
474 font_init();
Barry Wardell2f16d4f2006-12-19 11:33:53 +0000475 button_init();
Barry Wardell1920df32006-08-28 08:11:32 +0000476
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000477 lcd_set_foreground(LCD_WHITE);
478 lcd_set_background(LCD_BLACK);
479 lcd_clear_display();
480
Barry Wardell23709982007-03-12 22:12:20 +0000481 btn = button_read_device();
482
Barry Wardell0c1a3042007-03-20 10:36:26 +0000483 /* Enable bootloader messages if any button is pressed */
484 if (btn)
485 verbose = true;
Barry Wardell23709982007-03-12 22:12:20 +0000486
Barry Wardell1920df32006-08-28 08:11:32 +0000487 lcd_setfont(FONT_SYSFIXED);
488
Barry Wardellf4709d02007-01-17 12:20:38 +0000489 printf("Rockbox boot loader");
Barry Wardellca8f7bf2007-03-19 11:20:22 +0000490 printf("Version: %s", version);
Barry Wardellf4709d02007-01-17 12:20:38 +0000491 printf(MODEL_NAME);
Barry Wardell1920df32006-08-28 08:11:32 +0000492
493 i=ata_init();
494 if (i==0) {
Barry Wardell84b509d2007-01-28 18:42:11 +0000495 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 Wardell1920df32006-08-28 08:11:32 +0000505 } else {
Barry Wardell23709982007-03-12 22:12:20 +0000506 error(EATA, i);
Barry Wardell1920df32006-08-28 08:11:32 +0000507 }
508
509 disk_init();
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000510 num_partitions = disk_mount_all();
511 if (num_partitions<=0)
Barry Wardell1920df32006-08-28 08:11:32 +0000512 {
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000513 error(EDISK,num_partitions);
Barry Wardell1920df32006-08-28 08:11:32 +0000514 }
515
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000516 /* 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 Wardell1920df32006-08-28 08:11:32 +0000524
Barry Wardella42070d2007-03-15 22:32:58 +0000525 if(btn & BOOTLOADER_BOOT_OF)
Barry Wardell1920df32006-08-28 08:11:32 +0000526 {
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000527 /* 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 Wardell84b509d2007-01-28 18:42:11 +0000533 */
Barry Wardellf4709d02007-01-17 12:20:38 +0000534 printf("Loading original firmware...");
Barry Wardella91a35b2007-03-16 14:36:14 +0000535
536#ifdef SANSA_E200
537 /* First try a (hidden) firmware partition */
538 printf("Trying firmware partition");
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000539 pinfo = disk_partinfo(1);
Barry Wardellb8a5adf2007-03-16 14:41:55 +0000540 if(pinfo->type == PARTITION_TYPE_OS2_HIDDEN_C_DRIVE)
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000541 {
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 Wardella91a35b2007-03-16 14:36:14 +0000552#endif
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000553
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 Wardell84b509d2007-01-28 18:42:11 +0000564 rc=load_raw_firmware(loadbuffer, "/System/OF.bin", MAX_LOADSIZE);
565 if (rc < EOK) {
Barry Wardell23709982007-03-12 22:12:20 +0000566 printf("Can't load /System/OF.bin");
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000567 printf(strerror(rc));
568 } else {
569 return (void*)loadbuffer;
Barry Wardell84b509d2007-01-28 18:42:11 +0000570 }
Barry Wardell0c1a3042007-03-20 10:36:26 +0000571
Barry Wardell3de41f52007-03-20 20:45:25 +0000572 error(0, 0);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000573
Barry Wardell1920df32006-08-28 08:11:32 +0000574 } else {
Barry Wardellf4709d02007-01-17 12:20:38 +0000575 printf("Loading Rockbox...");
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000576 rc=load_mi4(loadbuffer, BOOTFILE, MAX_LOADSIZE);
Barry Wardell84b509d2007-01-28 18:42:11 +0000577 if (rc < EOK) {
Barry Wardell84b509d2007-01-28 18:42:11 +0000578 printf("Can't load %s:", BOOTFILE);
Barry Wardelle293bbb2007-03-17 19:07:20 +0000579 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 Wardell0c1a3042007-03-20 10:36:26 +0000585 error(EBOOTFILE, rc);
Barry Wardelle293bbb2007-03-17 19:07:20 +0000586 }
Barry Wardell84b509d2007-01-28 18:42:11 +0000587 }
Barry Wardell1920df32006-08-28 08:11:32 +0000588 }
Hristo Kovachev12041362006-08-11 09:51:04 +0000589
Barry Wardell84b509d2007-01-28 18:42:11 +0000590 return (void*)loadbuffer;
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000591}
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 Kovachev9dc0e622006-08-11 08:35:27 +0000595void usb_acknowledge(void)
596{
597}
598
599void usb_wait_for_disconnect(void)
600{
601}