blob: c4000ea2f334ed7a5db5d889efc131b3a3a74b9f [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>
Jonathan Gordona76947f2007-03-22 12:45:19 +0000252#ifdef SANSA_E200
253#include "usb.h"
254#endif
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000255
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 */
260static 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 */
267unsigned 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
285static 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 Wardell23709982007-03-12 22:12:20 +0000308
309/* Button definitions */
310#if CONFIG_KEYPAD == IRIVER_H10_PAD
Barry Wardell23709982007-03-12 22:12:20 +0000311#define BOOTLOADER_BOOT_OF BUTTON_LEFT
312
313#elif CONFIG_KEYPAD == SANSA_E200_PAD
Barry Wardell23709982007-03-12 22:12:20 +0000314#define BOOTLOADER_BOOT_OF BUTTON_LEFT
315
316#endif
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000317
Barry Wardell84b509d2007-01-28 18:42:11 +0000318/* Maximum allowed firmware image size. 10MB is more than enough */
Barry Wardell2f16d4f2006-12-19 11:33:53 +0000319#define MAX_LOADSIZE (10*1024*1024)
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000320
Barry Wardell84b509d2007-01-28 18:42:11 +0000321/* A buffer to load the original firmware or Rockbox into */
322unsigned char *loadbuffer = (unsigned char *)DRAM_START;
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000323
Barry Wardell84b509d2007-01-28 18:42:11 +0000324/* Bootloader version */
Barry Wardell1920df32006-08-28 08:11:32 +0000325char version[] = APPSVERSION;
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000326
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000327/* Load mi4 format firmware image */
328int 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 Wardelle293bbb2007-03-17 19:07:20 +0000346 read(fd, &mi4header, MI4_HEADER_SIZE);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000347
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000348 /* MI4 file size */
Barry Wardellbada0b72007-03-20 12:35:45 +0000349 printf("mi4 size: %x", mi4header.mi4size);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000350
Barry Wardellbada0b72007-03-20 12:35:45 +0000351 if ((mi4header.mi4size-MI4_HEADER_SIZE) > buffer_size)
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000352 return EFILE_TOO_BIG;
353
354 /* CRC32 */
355 printf("CRC32: %x", mi4header.crc32);
356
357 /* Rockbox model id */
Barry Wardellca8f7bf2007-03-19 11:20:22 +0000358 printf("Model id: %.4s", mi4header.model);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000359
360 /* Read binary type (RBOS, RBBL) */
Barry Wardellca8f7bf2007-03-19 11:20:22 +0000361 printf("Binary type: %.4s", mi4header.type);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000362
Barry Wardell9a9aebb2007-03-20 22:02:26 +0000363 /* 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 Wardell3de41f52007-03-20 20:45:25 +0000378 {
379 /* Load encrypted firmware */
380 int key_index = tea_find_key(&mi4header, fd);
Barry Wardell3de41f52007-03-20 20:45:25 +0000381
382 if (key_index < 0)
383 return EINVALID_FORMAT;
384
Barry Wardell9a9aebb2007-03-20 22:02:26 +0000385 /* Plaintext part is already loaded */
Barry Wardell3de41f52007-03-20 20:45:25 +0000386 buf += mi4header.plaintext;
387
Barry Wardell9a9aebb2007-03-20 22:02:26 +0000388 /* 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 Wardell3de41f52007-03-20 20:45:25 +0000393 printf("%s key used", tea_keytable[key_index].name);
Barry Wardell9a9aebb2007-03-20 22:02:26 +0000394
395 /* Check decryption was successfull */
Barry Wardell7cd559d2007-03-20 22:18:35 +0000396 if(le2int(&buf[mi4header.length-mi4header.plaintext-4]) != 0xaa55aa55)
Barry Wardell9a9aebb2007-03-20 22:02:26 +0000397 {
Barry Wardell3de41f52007-03-20 20:45:25 +0000398 return EREAD_IMAGE_FAILED;
Barry Wardell9a9aebb2007-03-20 22:02:26 +0000399 }
Barry Wardell3de41f52007-03-20 20:45:25 +0000400 }
401
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000402 return EOK;
403}
404
Barry Wardella91a35b2007-03-16 14:36:14 +0000405#ifdef SANSA_E200
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000406/* Load mi4 firmware from a hidden disk partition */
Jonathan Gordona76947f2007-03-22 12:45:19 +0000407int load_mi4_part(unsigned char* buf, struct partinfo* pinfo,
408 unsigned int buffer_size, bool disable_rebuild)
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000409{
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 Wardelle293bbb2007-03-17 19:07:20 +0000415 ata_read_sectors(pinfo->start + PPMI_SECTOR_OFFSET,
416 PPMI_SECTORS, &ppmi_header);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000417
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 Wardelle293bbb2007-03-17 19:07:20 +0000425 ata_read_sectors(pinfo->start + PPMI_SECTOR_OFFSET + PPMI_SECTORS
426 + (ppmi_header.length/512), MI4_HEADER_SECTORS, &mi4header);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000427
428 /* We don't support encrypted mi4 files yet */
Barry Wardellbada0b72007-03-20 12:35:45 +0000429 if( (mi4header.plaintext) != (mi4header.mi4size-MI4_HEADER_SIZE))
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000430 return EINVALID_FORMAT;
431
432 /* MI4 file size */
Barry Wardellbada0b72007-03-20 12:35:45 +0000433 printf("OF mi4 size: %x", mi4header.mi4size);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000434
Barry Wardellbada0b72007-03-20 12:35:45 +0000435 if ((mi4header.mi4size-MI4_HEADER_SIZE) > buffer_size)
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000436 return EFILE_TOO_BIG;
437
438 /* CRC32 */
439 printf("CRC32: %x", mi4header.crc32);
440
441 /* Rockbox model id */
Barry Wardellca8f7bf2007-03-19 11:20:22 +0000442 printf("Model id: %.4s", mi4header.model);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000443
444 /* Read binary type (RBOS, RBBL) */
Barry Wardellca8f7bf2007-03-19 11:20:22 +0000445 printf("Binary type: %.4s", mi4header.type);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000446
447 /* Load firmware */
Barry Wardelle293bbb2007-03-17 19:07:20 +0000448 ata_read_sectors(pinfo->start + PPMI_SECTOR_OFFSET + PPMI_SECTORS
449 + (ppmi_header.length/512) + MI4_HEADER_SECTORS,
Barry Wardellbada0b72007-03-20 12:35:45 +0000450 (mi4header.mi4size-MI4_HEADER_SIZE)/512, buf);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000451
452 /* Check CRC32 to see if we have a valid file */
Barry Wardelle293bbb2007-03-17 19:07:20 +0000453 sum = chksum_crc32 (buf,mi4header.mi4size-MI4_HEADER_SIZE);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000454
455 printf("Calculated CRC32: %x", sum);
456
457 if(sum != mi4header.crc32)
458 return EBAD_CHKSUM;
459
Jonathan Gordona76947f2007-03-22 12:45:19 +0000460 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 Wardell14ed3ca2007-03-16 14:28:00 +0000485 return EOK;
486}
Barry Wardella91a35b2007-03-16 14:36:14 +0000487#endif
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000488
Barry Wardell1920df32006-08-28 08:11:32 +0000489void* main(void)
490{
491 char buf[256];
492 int i;
Barry Wardell23709982007-03-12 22:12:20 +0000493 int btn;
Barry Wardell1920df32006-08-28 08:11:32 +0000494 int rc;
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000495 int num_partitions;
Barry Wardell1920df32006-08-28 08:11:32 +0000496 unsigned short* identify_info;
497 struct partinfo* pinfo;
Jonathan Gordona76947f2007-03-22 12:45:19 +0000498#ifdef SANSA_E200
499 int usb_retry = 0;
500 bool usb = false;
501#endif
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000502
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000503 chksum_crc32gentab ();
504
Barry Wardell1920df32006-08-28 08:11:32 +0000505 system_init();
506 kernel_init();
507 lcd_init();
508 font_init();
Barry Wardell2f16d4f2006-12-19 11:33:53 +0000509 button_init();
Barry Wardell1920df32006-08-28 08:11:32 +0000510
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000511 lcd_set_foreground(LCD_WHITE);
512 lcd_set_background(LCD_BLACK);
513 lcd_clear_display();
514
Barry Wardell23709982007-03-12 22:12:20 +0000515 btn = button_read_device();
Jonathan Gordona76947f2007-03-22 12:45:19 +0000516#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 Wardell0c1a3042007-03-20 10:36:26 +0000527 /* Enable bootloader messages if any button is pressed */
528 if (btn)
529 verbose = true;
Barry Wardell23709982007-03-12 22:12:20 +0000530
Barry Wardell1920df32006-08-28 08:11:32 +0000531 lcd_setfont(FONT_SYSFIXED);
532
Barry Wardellf4709d02007-01-17 12:20:38 +0000533 printf("Rockbox boot loader");
Barry Wardellca8f7bf2007-03-19 11:20:22 +0000534 printf("Version: %s", version);
Barry Wardellf4709d02007-01-17 12:20:38 +0000535 printf(MODEL_NAME);
Barry Wardell1920df32006-08-28 08:11:32 +0000536
537 i=ata_init();
538 if (i==0) {
Barry Wardell84b509d2007-01-28 18:42:11 +0000539 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 Wardell1920df32006-08-28 08:11:32 +0000549 } else {
Barry Wardell23709982007-03-12 22:12:20 +0000550 error(EATA, i);
Barry Wardell1920df32006-08-28 08:11:32 +0000551 }
552
553 disk_init();
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000554 num_partitions = disk_mount_all();
555 if (num_partitions<=0)
Barry Wardell1920df32006-08-28 08:11:32 +0000556 {
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000557 error(EDISK,num_partitions);
Barry Wardell1920df32006-08-28 08:11:32 +0000558 }
559
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000560 /* Just list the first 2 partitions since we don't have any devices yet
561 that have more than that */
Barry Wardell7cd559d2007-03-20 22:18:35 +0000562 for(i=0; i<NUM_PARTITIONS; i++)
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000563 {
564 pinfo = disk_partinfo(i);
565 printf("Partition %d: 0x%02x %ld MB",
566 i, pinfo->type, pinfo->size / 2048);
567 }
Barry Wardell1920df32006-08-28 08:11:32 +0000568
Barry Wardella42070d2007-03-15 22:32:58 +0000569 if(btn & BOOTLOADER_BOOT_OF)
Barry Wardell1920df32006-08-28 08:11:32 +0000570 {
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000571 /* 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 Wardell84b509d2007-01-28 18:42:11 +0000577 */
Barry Wardellf4709d02007-01-17 12:20:38 +0000578 printf("Loading original firmware...");
Barry Wardella91a35b2007-03-16 14:36:14 +0000579
580#ifdef SANSA_E200
581 /* First try a (hidden) firmware partition */
582 printf("Trying firmware partition");
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000583 pinfo = disk_partinfo(1);
Barry Wardellb8a5adf2007-03-16 14:41:55 +0000584 if(pinfo->type == PARTITION_TYPE_OS2_HIDDEN_C_DRIVE)
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000585 {
Jonathan Gordona76947f2007-03-22 12:45:19 +0000586 rc = load_mi4_part(loadbuffer, pinfo, MAX_LOADSIZE, usb);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000587 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 Wardella91a35b2007-03-16 14:36:14 +0000596#endif
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000597
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 Wardell84b509d2007-01-28 18:42:11 +0000608 rc=load_raw_firmware(loadbuffer, "/System/OF.bin", MAX_LOADSIZE);
609 if (rc < EOK) {
Barry Wardell23709982007-03-12 22:12:20 +0000610 printf("Can't load /System/OF.bin");
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000611 printf(strerror(rc));
612 } else {
613 return (void*)loadbuffer;
Barry Wardell84b509d2007-01-28 18:42:11 +0000614 }
Barry Wardell0c1a3042007-03-20 10:36:26 +0000615
Barry Wardell3de41f52007-03-20 20:45:25 +0000616 error(0, 0);
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000617
Barry Wardell1920df32006-08-28 08:11:32 +0000618 } else {
Barry Wardellf4709d02007-01-17 12:20:38 +0000619 printf("Loading Rockbox...");
Barry Wardell14ed3ca2007-03-16 14:28:00 +0000620 rc=load_mi4(loadbuffer, BOOTFILE, MAX_LOADSIZE);
Barry Wardell84b509d2007-01-28 18:42:11 +0000621 if (rc < EOK) {
Barry Wardell84b509d2007-01-28 18:42:11 +0000622 printf("Can't load %s:", BOOTFILE);
Barry Wardelle293bbb2007-03-17 19:07:20 +0000623 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 Wardell0c1a3042007-03-20 10:36:26 +0000629 error(EBOOTFILE, rc);
Barry Wardelle293bbb2007-03-17 19:07:20 +0000630 }
Barry Wardell84b509d2007-01-28 18:42:11 +0000631 }
Barry Wardell1920df32006-08-28 08:11:32 +0000632 }
Hristo Kovachev12041362006-08-11 09:51:04 +0000633
Barry Wardell84b509d2007-01-28 18:42:11 +0000634 return (void*)loadbuffer;
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000635}
636
Jonathan Gordona76947f2007-03-22 12:45:19 +0000637#ifndef SANSA_E200
Hristo Kovachev9dc0e622006-08-11 08:35:27 +0000638/* 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 Kovachev9dc0e622006-08-11 08:35:27 +0000640void usb_acknowledge(void)
641{
642}
643
644void usb_wait_for_disconnect(void)
645{
646}
Jonathan Gordona76947f2007-03-22 12:45:19 +0000647#endif
648