blob: 28d06d38d2409767ea545cda846d0bbc071d842b [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 */
Barry Wardell7cd559d2007-03-20 22:18:35 +000035#ifdef SANSA_E200
Barry Wardell3de41f52007-03-20 20:45:25 +000036#define PPMI_SECTOR_OFFSET 1024
37#define PPMI_SECTORS 1
38#define MI4_HEADER_SECTORS 1
Barry Wardell7cd559d2007-03-20 22:18:35 +000039#define NUM_PARTITIONS 2
40
41#else
Barry Wardell7cd559d2007-03-20 22:18:35 +000042#define NUM_PARTITIONS 1
43
44#endif
Barry Wardell3de41f52007-03-20 20:45:25 +000045
Barry Wardell7e03b0a2007-03-20 22:56:51 +000046#define MI4_HEADER_SIZE 0x200
47
Barry Wardell3de41f52007-03-20 20:45:25 +000048/* mi4 header structure */
49struct 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 */
64struct ppmi_header_t {
65 unsigned char magic[4];
66 uint32_t length;
67 uint32_t pad[126];
68};
69
70inline 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
77inline 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
85struct tea_key {
86 const char * name;
87 uint32_t key[4];
88};
89
90#define NUM_KEYS 11
91struct 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
107tea_decrypt() from http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
108
109"Following is an adaptation of the reference encryption and decryption
110routines in C, released into the public domain by David Wheeler and
111Roger 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
120static 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
135static 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
166static 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
174static 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 Wardell7cd559d2007-03-20 22:18:35 +0000196 printf("Searching for key:");
Barry Wardell3de41f52007-03-20 20:45:25 +0000197
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 Wardell7cd559d2007-03-20 22:18:35 +0000223 /* printf("%s...failed", tea_keytable[i].name); */
Barry Wardell3de41f52007-03-20 20:45:25 +0000224 }
225 }
226
227 return key_found;
228}
229
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000230/*
Barry Wardell9a9aebb2007-03-20 22:02:26 +0000231 * 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 Wardell14ed3ca2007-03-16 14:28:00 +0000234 * 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>
252
253/* crc_tab[] -- this crcTable is being build by chksum_crc32GenTab().
254 * so make sure, you call it before using the other
255 * functions!
256 */
257static unsigned int crc_tab[256];
258
259/* chksum_crc() -- to a given block, this one calculates the
260 * crc32-checksum until the length is
261 * reached. the crc32-checksum will be
262 * the result.
263 */
264unsigned int chksum_crc32 (unsigned char *block, unsigned int length)
265{
266 register unsigned long crc;
267 unsigned long i;
268
269 crc = 0;
270 for (i = 0; i < length; i++)
271 {
272 crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_tab[(crc ^ *block++) & 0xFF];
273 }
274 return (crc);
275}
276
277/* chksum_crc32gentab() -- to a global crc_tab[256], this one will
278 * calculate the crcTable for crc32-checksums.
279 * it is generated to the polynom [..]
280 */
281
282static void chksum_crc32gentab (void)
283{
284 unsigned long crc, poly;
285 int i, j;
286
287 poly = 0xEDB88320L;
288 for (i = 0; i < 256; i++)
289 {
290 crc = i;
291 for (j = 8; j > 0; j--)
292 {
293 if (crc & 1)
294 {
295 crc = (crc >> 1) ^ poly;
296 }
297 else
298 {
299 crc >>= 1;
300 }
301 }
302 crc_tab[i] = crc;
303 }
304}
Barry Wardell23709982007-03-12 22:12:20 +0000305
306/* Button definitions */
307#if CONFIG_KEYPAD == IRIVER_H10_PAD
Barry Wardell23709982007-03-12 22:12:20 +0000308#define BOOTLOADER_BOOT_OF BUTTON_LEFT
309
310#elif CONFIG_KEYPAD == SANSA_E200_PAD
Barry Wardell23709982007-03-12 22:12:20 +0000311#define BOOTLOADER_BOOT_OF BUTTON_LEFT
312
313#endif
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000314
Barry Wardell84b509d2007-01-28 18:42:11 +0000315/* Maximum allowed firmware image size. 10MB is more than enough */
Barry Wardell2f16d4f2006-12-19 11:33:53 +0000316#define MAX_LOADSIZE (10*1024*1024)
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000317
Barry Wardell84b509d2007-01-28 18:42:11 +0000318/* A buffer to load the original firmware or Rockbox into */
319unsigned char *loadbuffer = (unsigned char *)DRAM_START;
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000320
Barry Wardell84b509d2007-01-28 18:42:11 +0000321/* Bootloader version */
Barry Wardell1920df32006-08-28 08:11:32 +0000322char version[] = APPSVERSION;
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000323
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000324/* Load mi4 format firmware image */
325int load_mi4(unsigned char* buf, char* firmware, unsigned int buffer_size)
326{
327 int fd;
328 struct mi4header_t mi4header;
329 int rc;
330 unsigned long sum;
331 char filename[MAX_PATH];
332
333 snprintf(filename,sizeof(filename),"/.rockbox/%s",firmware);
334 fd = open(filename, O_RDONLY);
335 if(fd < 0)
336 {
337 snprintf(filename,sizeof(filename),"/%s",firmware);
338 fd = open(filename, O_RDONLY);
339 if(fd < 0)
340 return EFILE_NOT_FOUND;
341 }
342
Barry Wardelle293bbb2007-03-17 19:07:20 +0000343 read(fd, &mi4header, MI4_HEADER_SIZE);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000344
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000345 /* MI4 file size */
Barry Wardellbada0b72007-03-20 12:35:45 +0000346 printf("mi4 size: %x", mi4header.mi4size);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000347
Barry Wardellbada0b72007-03-20 12:35:45 +0000348 if ((mi4header.mi4size-MI4_HEADER_SIZE) > buffer_size)
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000349 return EFILE_TOO_BIG;
350
351 /* CRC32 */
352 printf("CRC32: %x", mi4header.crc32);
353
354 /* Rockbox model id */
Barry Wardellca8f7bf2007-03-19 11:20:22 +0000355 printf("Model id: %.4s", mi4header.model);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000356
357 /* Read binary type (RBOS, RBBL) */
Barry Wardellca8f7bf2007-03-19 11:20:22 +0000358 printf("Binary type: %.4s", mi4header.type);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000359
Barry Wardell9a9aebb2007-03-20 22:02:26 +0000360 /* Load firmware file */
361 lseek(fd, MI4_HEADER_SIZE, SEEK_SET);
362 rc = read(fd, buf, mi4header.mi4size-MI4_HEADER_SIZE);
363 if(rc < (int)mi4header.mi4size-MI4_HEADER_SIZE)
364 return EREAD_IMAGE_FAILED;
365
366 /* Check CRC32 to see if we have a valid file */
367 sum = chksum_crc32 (buf, mi4header.mi4size - MI4_HEADER_SIZE);
368
369 printf("Calculated CRC32: %x", sum);
370
371 if(sum != mi4header.crc32)
372 return EBAD_CHKSUM;
373
374 if( (mi4header.plaintext + MI4_HEADER_SIZE) != mi4header.mi4size)
Barry Wardell3de41f52007-03-20 20:45:25 +0000375 {
376 /* Load encrypted firmware */
377 int key_index = tea_find_key(&mi4header, fd);
Barry Wardell3de41f52007-03-20 20:45:25 +0000378
379 if (key_index < 0)
380 return EINVALID_FORMAT;
381
Barry Wardell9a9aebb2007-03-20 22:02:26 +0000382 /* Plaintext part is already loaded */
Barry Wardell3de41f52007-03-20 20:45:25 +0000383 buf += mi4header.plaintext;
384
Barry Wardell9a9aebb2007-03-20 22:02:26 +0000385 /* Decrypt in-place */
386 tea_decrypt_buf(buf, buf,
387 mi4header.mi4size-(mi4header.plaintext+MI4_HEADER_SIZE),
388 tea_keytable[key_index].key);
389
Barry Wardell3de41f52007-03-20 20:45:25 +0000390 printf("%s key used", tea_keytable[key_index].name);
Barry Wardell9a9aebb2007-03-20 22:02:26 +0000391
392 /* Check decryption was successfull */
Barry Wardell7cd559d2007-03-20 22:18:35 +0000393 if(le2int(&buf[mi4header.length-mi4header.plaintext-4]) != 0xaa55aa55)
Barry Wardell9a9aebb2007-03-20 22:02:26 +0000394 {
Barry Wardell3de41f52007-03-20 20:45:25 +0000395 return EREAD_IMAGE_FAILED;
Barry Wardell9a9aebb2007-03-20 22:02:26 +0000396 }
Barry Wardell3de41f52007-03-20 20:45:25 +0000397 }
398
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000399 return EOK;
400}
401
Barry Wardella91a35b2007-03-16 14:36:14 +0000402#ifdef SANSA_E200
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000403/* Load mi4 firmware from a hidden disk partition */
404int load_mi4_part(unsigned char* buf, struct partinfo* pinfo, unsigned int buffer_size)
405{
406 struct mi4header_t mi4header;
407 struct ppmi_header_t ppmi_header;
408 unsigned long sum;
409
410 /* Read header to find out how long the mi4 file is. */
Barry Wardelle293bbb2007-03-17 19:07:20 +0000411 ata_read_sectors(pinfo->start + PPMI_SECTOR_OFFSET,
412 PPMI_SECTORS, &ppmi_header);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000413
414 /* The first four characters at 0x80000 (sector 1024) should be PPMI*/
415 if( memcmp(ppmi_header.magic, "PPMI", 4) )
416 return EFILE_NOT_FOUND;
417
418 printf("BL mi4 size: %x", ppmi_header.length);
419
420 /* Read mi4 header of the OF */
Barry Wardelle293bbb2007-03-17 19:07:20 +0000421 ata_read_sectors(pinfo->start + PPMI_SECTOR_OFFSET + PPMI_SECTORS
422 + (ppmi_header.length/512), MI4_HEADER_SECTORS, &mi4header);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000423
424 /* We don't support encrypted mi4 files yet */
Barry Wardellbada0b72007-03-20 12:35:45 +0000425 if( (mi4header.plaintext) != (mi4header.mi4size-MI4_HEADER_SIZE))
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000426 return EINVALID_FORMAT;
427
428 /* MI4 file size */
Barry Wardellbada0b72007-03-20 12:35:45 +0000429 printf("OF mi4 size: %x", mi4header.mi4size);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000430
Barry Wardellbada0b72007-03-20 12:35:45 +0000431 if ((mi4header.mi4size-MI4_HEADER_SIZE) > buffer_size)
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000432 return EFILE_TOO_BIG;
433
434 /* CRC32 */
435 printf("CRC32: %x", mi4header.crc32);
436
437 /* Rockbox model id */
Barry Wardellca8f7bf2007-03-19 11:20:22 +0000438 printf("Model id: %.4s", mi4header.model);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000439
440 /* Read binary type (RBOS, RBBL) */
Barry Wardellca8f7bf2007-03-19 11:20:22 +0000441 printf("Binary type: %.4s", mi4header.type);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000442
443 /* Load firmware */
Barry Wardelle293bbb2007-03-17 19:07:20 +0000444 ata_read_sectors(pinfo->start + PPMI_SECTOR_OFFSET + PPMI_SECTORS
445 + (ppmi_header.length/512) + MI4_HEADER_SECTORS,
Barry Wardellbada0b72007-03-20 12:35:45 +0000446 (mi4header.mi4size-MI4_HEADER_SIZE)/512, buf);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000447
448 /* Check CRC32 to see if we have a valid file */
Barry Wardelle293bbb2007-03-17 19:07:20 +0000449 sum = chksum_crc32 (buf,mi4header.mi4size-MI4_HEADER_SIZE);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000450
451 printf("Calculated CRC32: %x", sum);
452
453 if(sum != mi4header.crc32)
454 return EBAD_CHKSUM;
455
456 return EOK;
457}
Barry Wardella91a35b2007-03-16 14:36:14 +0000458#endif
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000459
Barry Wardell1920df32006-08-28 08:11:32 +0000460void* main(void)
461{
462 char buf[256];
463 int i;
Barry Wardell23709982007-03-12 22:12:20 +0000464 int btn;
Barry Wardell1920df32006-08-28 08:11:32 +0000465 int rc;
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000466 int num_partitions;
Barry Wardell1920df32006-08-28 08:11:32 +0000467 unsigned short* identify_info;
468 struct partinfo* pinfo;
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000469
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000470 chksum_crc32gentab ();
471
Barry Wardell1920df32006-08-28 08:11:32 +0000472 system_init();
473 kernel_init();
474 lcd_init();
475 font_init();
Barry Wardell2f16d4f2006-12-19 11:33:53 +0000476 button_init();
Barry Wardell1920df32006-08-28 08:11:32 +0000477
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000478 lcd_set_foreground(LCD_WHITE);
479 lcd_set_background(LCD_BLACK);
480 lcd_clear_display();
481
Barry Wardell23709982007-03-12 22:12:20 +0000482 btn = button_read_device();
483
Barry Wardell0c1a3042007-03-20 10:36:26 +0000484 /* Enable bootloader messages if any button is pressed */
485 if (btn)
486 verbose = true;
Barry Wardell23709982007-03-12 22:12:20 +0000487
Barry Wardell1920df32006-08-28 08:11:32 +0000488 lcd_setfont(FONT_SYSFIXED);
489
Barry Wardellf4709d02007-01-17 12:20:38 +0000490 printf("Rockbox boot loader");
Barry Wardellca8f7bf2007-03-19 11:20:22 +0000491 printf("Version: %s", version);
Barry Wardellf4709d02007-01-17 12:20:38 +0000492 printf(MODEL_NAME);
Barry Wardell1920df32006-08-28 08:11:32 +0000493
494 i=ata_init();
495 if (i==0) {
Barry Wardell84b509d2007-01-28 18:42:11 +0000496 identify_info=ata_get_identify();
497 /* Show model */
498 for (i=0; i < 20; i++) {
499 ((unsigned short*)buf)[i]=htobe16(identify_info[i+27]);
500 }
501 buf[40]=0;
502 for (i=39; i && buf[i]==' '; i--) {
503 buf[i]=0;
504 }
505 printf(buf);
Barry Wardell1920df32006-08-28 08:11:32 +0000506 } else {
Barry Wardell23709982007-03-12 22:12:20 +0000507 error(EATA, i);
Barry Wardell1920df32006-08-28 08:11:32 +0000508 }
509
510 disk_init();
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000511 num_partitions = disk_mount_all();
512 if (num_partitions<=0)
Barry Wardell1920df32006-08-28 08:11:32 +0000513 {
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000514 error(EDISK,num_partitions);
Barry Wardell1920df32006-08-28 08:11:32 +0000515 }
516
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000517 /* Just list the first 2 partitions since we don't have any devices yet
518 that have more than that */
Barry Wardell7cd559d2007-03-20 22:18:35 +0000519 for(i=0; i<NUM_PARTITIONS; i++)
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000520 {
521 pinfo = disk_partinfo(i);
522 printf("Partition %d: 0x%02x %ld MB",
523 i, pinfo->type, pinfo->size / 2048);
524 }
Barry Wardell1920df32006-08-28 08:11:32 +0000525
Barry Wardella42070d2007-03-15 22:32:58 +0000526 if(btn & BOOTLOADER_BOOT_OF)
Barry Wardell1920df32006-08-28 08:11:32 +0000527 {
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000528 /* Load original mi4 firmware in to a memory buffer called loadbuffer.
529 The rest of the loading is done in crt0.S.
530 1) First try reading from the hidden partition (on Sansa only).
531 2) Next try a decrypted mi4 file in /System/OF.mi4
532 3) Finally, try a raw firmware binary in /System/OF.mi4. It should be
533 a mi4 firmware decrypted and header stripped using mi4code.
Barry Wardell84b509d2007-01-28 18:42:11 +0000534 */
Barry Wardellf4709d02007-01-17 12:20:38 +0000535 printf("Loading original firmware...");
Barry Wardella91a35b2007-03-16 14:36:14 +0000536
537#ifdef SANSA_E200
538 /* First try a (hidden) firmware partition */
539 printf("Trying firmware partition");
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000540 pinfo = disk_partinfo(1);
Barry Wardellb8a5adf2007-03-16 14:41:55 +0000541 if(pinfo->type == PARTITION_TYPE_OS2_HIDDEN_C_DRIVE)
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000542 {
543 rc = load_mi4_part(loadbuffer, pinfo, MAX_LOADSIZE);
544 if (rc < EOK) {
545 printf("Can't load from partition");
546 printf(strerror(rc));
547 } else {
548 return (void*)loadbuffer;
549 }
550 } else {
551 printf("No hidden partition found.");
552 }
Barry Wardella91a35b2007-03-16 14:36:14 +0000553#endif
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000554
555 printf("Trying /System/OF.mi4");
556 rc=load_mi4(loadbuffer, "/System/OF.mi4", MAX_LOADSIZE);
557 if (rc < EOK) {
558 printf("Can't load /System/OF.mi4");
559 printf(strerror(rc));
560 } else {
561 return (void*)loadbuffer;
562 }
563
564 printf("Trying /System/OF.bin");
Barry Wardell84b509d2007-01-28 18:42:11 +0000565 rc=load_raw_firmware(loadbuffer, "/System/OF.bin", MAX_LOADSIZE);
566 if (rc < EOK) {
Barry Wardell23709982007-03-12 22:12:20 +0000567 printf("Can't load /System/OF.bin");
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000568 printf(strerror(rc));
569 } else {
570 return (void*)loadbuffer;
Barry Wardell84b509d2007-01-28 18:42:11 +0000571 }
Barry Wardell0c1a3042007-03-20 10:36:26 +0000572
Barry Wardell3de41f52007-03-20 20:45:25 +0000573 error(0, 0);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000574
Barry Wardell1920df32006-08-28 08:11:32 +0000575 } else {
Barry Wardellf4709d02007-01-17 12:20:38 +0000576 printf("Loading Rockbox...");
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000577 rc=load_mi4(loadbuffer, BOOTFILE, MAX_LOADSIZE);
Barry Wardell84b509d2007-01-28 18:42:11 +0000578 if (rc < EOK) {
Barry Wardell84b509d2007-01-28 18:42:11 +0000579 printf("Can't load %s:", BOOTFILE);
Barry Wardelle293bbb2007-03-17 19:07:20 +0000580 printf(strerror(rc));
581
582 /* Try loading rockbox from old rockbox.e200/rockbox.h10 format */
583 rc=load_firmware(loadbuffer, OLD_BOOTFILE, MAX_LOADSIZE);
584 if (rc < EOK) {
585 printf("Can't load %s:", OLD_BOOTFILE);
Barry Wardell0c1a3042007-03-20 10:36:26 +0000586 error(EBOOTFILE, rc);
Barry Wardelle293bbb2007-03-17 19:07:20 +0000587 }
Barry Wardell84b509d2007-01-28 18:42:11 +0000588 }
Barry Wardell1920df32006-08-28 08:11:32 +0000589 }
Hristo Kovachev12041362006-08-11 09:51:04 +0000590
Barry Wardell84b509d2007-01-28 18:42:11 +0000591 return (void*)loadbuffer;
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000592}
593
594/* These functions are present in the firmware library, but we reimplement
595 them here because the originals do a lot more than we want */
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000596void usb_acknowledge(void)
597{
598}
599
600void usb_wait_for_disconnect(void)
601{
602}