summaryrefslogtreecommitdiff
path: root/bootloader/main-pp.c
diff options
context:
space:
mode:
Diffstat (limited to 'bootloader/main-pp.c')
-rw-r--r--bootloader/main-pp.c274
1 files changed, 17 insertions, 257 deletions
diff --git a/bootloader/main-pp.c b/bootloader/main-pp.c
index 72d45712fc..b03a5a75c6 100644
--- a/bootloader/main-pp.c
+++ b/bootloader/main-pp.c
@@ -37,6 +37,8 @@
37#include "button.h" 37#include "button.h"
38#include "disk.h" 38#include "disk.h"
39#include "crc32-mi4.h" 39#include "crc32-mi4.h"
40#include "mi4-loader.h"
41#include "loader_strerror.h"
40#include <string.h> 42#include <string.h>
41#include "power.h" 43#include "power.h"
42#include "version.h" 44#include "version.h"
@@ -110,21 +112,6 @@ unsigned char *loadbuffer = (unsigned char *)DRAM_START;
110 112
111#define MI4_HEADER_SIZE 0x200 113#define MI4_HEADER_SIZE 0x200
112 114
113/* mi4 header structure */
114struct mi4header_t {
115 unsigned char magic[4];
116 uint32_t version;
117 uint32_t length;
118 uint32_t crc32;
119 uint32_t enctype;
120 uint32_t mi4size;
121 uint32_t plaintext;
122 uint32_t dsa_key[10];
123 uint32_t pad[109];
124 unsigned char type[4];
125 unsigned char model[4];
126};
127
128/* PPMI header structure */ 115/* PPMI header structure */
129struct ppmi_header_t { 116struct ppmi_header_t {
130 unsigned char magic[4]; 117 unsigned char magic[4];
@@ -132,236 +119,6 @@ struct ppmi_header_t {
132 uint32_t pad[126]; 119 uint32_t pad[126];
133}; 120};
134 121
135inline unsigned int le2int(unsigned char* buf)
136{
137 int32_t res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
138
139 return res;
140}
141
142inline void int2le(unsigned int val, unsigned char* addr)
143{
144 addr[0] = val & 0xFF;
145 addr[1] = (val >> 8) & 0xff;
146 addr[2] = (val >> 16) & 0xff;
147 addr[3] = (val >> 24) & 0xff;
148}
149
150struct tea_key {
151 const char * name;
152 uint32_t key[4];
153};
154
155#define NUM_KEYS (sizeof(tea_keytable)/sizeof(tea_keytable[0]))
156struct tea_key tea_keytable[] = {
157 { "default" , { 0x20d36cc0, 0x10e8c07d, 0xc0e7dcaa, 0x107eb080 } },
158 { "sansa", { 0xe494e96e, 0x3ee32966, 0x6f48512b, 0xa93fbb42 } },
159 { "sansa_gh", { 0xd7b10538, 0xc662945b, 0x1b3fce68, 0xf389c0e6 } },
160 { "sansa_103", { 0x1d29ddc0, 0x2579c2cd, 0xce339e1a, 0x75465dfe } },
161 { "rhapsody", { 0x7aa9c8dc, 0xbed0a82a, 0x16204cc7, 0x5904ef38 } },
162 { "p610", { 0x950e83dc, 0xec4907f9, 0x023734b9, 0x10cfb7c7 } },
163 { "p640", { 0x220c5f23, 0xd04df68e, 0x431b5e25, 0x4dcc1fa1 } },
164 { "virgin", { 0xe83c29a1, 0x04862973, 0xa9b3f0d4, 0x38be2a9c } },
165 { "20gc_eng", { 0x0240772c, 0x6f3329b5, 0x3ec9a6c5, 0xb0c9e493 } },
166 { "20gc_fre", { 0xbede8817, 0xb23bfe4f, 0x80aa682d, 0xd13f598c } },
167 { "elio_p722", { 0x6af3b9f8, 0x777483f5, 0xae8181cc, 0xfa6d8a84 } },
168 { "c200", { 0xbf2d06fa, 0xf0e23d59, 0x29738132, 0xe2d04ca7 } },
169 { "c200_103", { 0x2a7968de, 0x15127979, 0x142e60a7, 0xe49c1893 } },
170 { "c200_106", { 0xa913d139, 0xf842f398, 0x3e03f1a6, 0x060ee012 } },
171 { "view", { 0x70e19bda, 0x0c69ea7d, 0x2b8b1ad1, 0xe9767ced } },
172 { "sa9200", { 0x33ea0236, 0x9247bdc5, 0xdfaedf9f, 0xd67c9d30 } },
173 { "hdd1630/hdd63x0", { 0x04543ced, 0xcebfdbad, 0xf7477872, 0x0d12342e } },
174 { "vibe500", { 0xe3a66156, 0x77c6b67a, 0xe821dca5, 0xca8ca37c } },
175};
176
177/*
178
179tea_decrypt() from http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
180
181"Following is an adaptation of the reference encryption and decryption
182routines in C, released into the public domain by David Wheeler and
183Roger Needham:"
184
185*/
186
187/* NOTE: The mi4 version of TEA uses a different initial value to sum compared
188 to the reference implementation and the main loop is 8 iterations, not
189 32.
190*/
191
192static void tea_decrypt(uint32_t* v0, uint32_t* v1, uint32_t* k) {
193 uint32_t sum=0xF1BBCDC8, i; /* set up */
194 uint32_t delta=0x9E3779B9; /* a key schedule constant */
195 uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* cache key */
196 for(i=0; i<8; i++) { /* basic cycle start */
197 *v1 -= ((*v0<<4) + k2) ^ (*v0 + sum) ^ ((*v0>>5) + k3);
198 *v0 -= ((*v1<<4) + k0) ^ (*v1 + sum) ^ ((*v1>>5) + k1);
199 sum -= delta; /* end cycle */
200 }
201}
202
203/* mi4 files are encrypted in 64-bit blocks (two little-endian 32-bit
204 integers) and the key is incremented after each block
205 */
206
207static void tea_decrypt_buf(unsigned char* src, unsigned char* dest, size_t n, uint32_t * key)
208{
209 uint32_t v0, v1;
210 unsigned int i;
211
212 for (i = 0; i < (n / 8); i++) {
213 v0 = le2int(src);
214 v1 = le2int(src+4);
215
216 tea_decrypt(&v0, &v1, key);
217
218 int2le(v0, dest);
219 int2le(v1, dest+4);
220
221 src += 8;
222 dest += 8;
223
224 /* Now increment the key */
225 key[0]++;
226 if (key[0]==0) {
227 key[1]++;
228 if (key[1]==0) {
229 key[2]++;
230 if (key[2]==0) {
231 key[3]++;
232 }
233 }
234 }
235 }
236}
237
238static int tea_find_key(struct mi4header_t *mi4header, unsigned char* buf)
239{
240 unsigned int i;
241 uint32_t key[4];
242 uint32_t keyinc;
243 unsigned char magic_dec[8];
244 int key_found = -1;
245 unsigned int magic_location = mi4header->length-4;
246 int unaligned = 0;
247
248 if ( (magic_location % 8) != 0 )
249 {
250 unaligned = 1;
251 magic_location -= 4;
252 }
253
254 printf("Searching for key:");
255
256 for (i=0; i < NUM_KEYS && (key_found<0) ; i++) {
257 key[0] = tea_keytable[i].key[0];
258 key[1] = tea_keytable[i].key[1];
259 key[2] = tea_keytable[i].key[2];
260 key[3] = tea_keytable[i].key[3];
261
262 /* Now increment the key */
263 keyinc = (magic_location-mi4header->plaintext)/8;
264 if ((key[0]+keyinc) < key[0]) key[1]++;
265 key[0] += keyinc;
266 if (key[1]==0) key[2]++;
267 if (key[2]==0) key[3]++;
268
269 /* Decrypt putative magic */
270 tea_decrypt_buf(&buf[magic_location], magic_dec, 8, key);
271
272 if (le2int(&magic_dec[4*unaligned]) == 0xaa55aa55)
273 {
274 key_found = i;
275 printf("%s...found", tea_keytable[i].name);
276 } else {
277 /* printf("%s...failed", tea_keytable[i].name); */
278 }
279 }
280
281 return key_found;
282}
283
284
285/* Load mi4 format firmware image */
286int load_mi4(unsigned char* buf, char* firmware, unsigned int buffer_size)
287{
288 int fd;
289 struct mi4header_t mi4header;
290 int rc;
291 unsigned long sum;
292 char filename[MAX_PATH];
293
294 snprintf(filename,sizeof(filename), BOOTDIR "/%s",firmware);
295 fd = open(filename, O_RDONLY);
296 if(fd < 0)
297 {
298 snprintf(filename,sizeof(filename),"/%s",firmware);
299 fd = open(filename, O_RDONLY);
300 if(fd < 0)
301 return EFILE_NOT_FOUND;
302 }
303
304 read(fd, &mi4header, MI4_HEADER_SIZE);
305
306 /* MI4 file size */
307 printf("mi4 size: %x", mi4header.mi4size);
308
309 if ((mi4header.mi4size-MI4_HEADER_SIZE) > buffer_size)
310 {
311 close(fd);
312 return EFILE_TOO_BIG;
313 }
314
315 /* CRC32 */
316 printf("CRC32: %x", mi4header.crc32);
317
318 /* Rockbox model id */
319 printf("Model id: %.4s", mi4header.model);
320
321 /* Read binary type (RBOS, RBBL) */
322 printf("Binary type: %.4s", mi4header.type);
323
324 /* Load firmware file */
325 lseek(fd, MI4_HEADER_SIZE, SEEK_SET);
326 rc = read(fd, buf, mi4header.mi4size-MI4_HEADER_SIZE);
327 close(fd);
328 if(rc < (int)mi4header.mi4size-MI4_HEADER_SIZE)
329 return EREAD_IMAGE_FAILED;
330
331 /* Check CRC32 to see if we have a valid file */
332 sum = chksum_crc32 (buf, mi4header.mi4size - MI4_HEADER_SIZE);
333
334 printf("Calculated CRC32: %x", sum);
335
336 if(sum != mi4header.crc32)
337 return EBAD_CHKSUM;
338
339 if( (mi4header.plaintext + MI4_HEADER_SIZE) != mi4header.mi4size)
340 {
341 /* Load encrypted firmware */
342 int key_index = tea_find_key(&mi4header, buf);
343
344 if (key_index < 0)
345 return EINVALID_FORMAT;
346
347 /* Plaintext part is already loaded */
348 buf += mi4header.plaintext;
349
350 /* Decrypt in-place */
351 tea_decrypt_buf(buf, buf,
352 mi4header.mi4size-(mi4header.plaintext+MI4_HEADER_SIZE),
353 tea_keytable[key_index].key);
354
355 printf("%s key used", tea_keytable[key_index].name);
356
357 /* Check decryption was successfull */
358 if(le2int(&buf[mi4header.length-mi4header.plaintext-4]) != 0xaa55aa55)
359 return EREAD_IMAGE_FAILED;
360 }
361
362 return EOK;
363}
364
365#if (CONFIG_STORAGE & STORAGE_SD) 122#if (CONFIG_STORAGE & STORAGE_SD)
366/* Load mi4 firmware from a hidden disk partition */ 123/* Load mi4 firmware from a hidden disk partition */
367int load_mi4_part(unsigned char* buf, struct partinfo* pinfo, 124int load_mi4_part(unsigned char* buf, struct partinfo* pinfo,
@@ -432,7 +189,7 @@ int load_mi4_part(unsigned char* buf, struct partinfo* pinfo,
432 (void) disable_rebuild; 189 (void) disable_rebuild;
433#endif 190#endif
434 191
435 return EOK; 192 return mi4header.mi4size-MI4_HEADER_SIZE;
436} 193}
437#endif /* (CONFIG_STORAGE & STORAGE_SD) */ 194#endif /* (CONFIG_STORAGE & STORAGE_SD) */
438 195
@@ -528,6 +285,7 @@ static int handle_usb(int connect_timeout)
528 285
529void* main(void) 286void* main(void)
530{ 287{
288 char filename[MAX_PATH];
531 int i; 289 int i;
532 int btn; 290 int btn;
533 int rc; 291 int rc;
@@ -642,13 +400,15 @@ void* main(void)
642 if((btn & BOOTLOADER_BOOT_OF) == 0) 400 if((btn & BOOTLOADER_BOOT_OF) == 0)
643 { 401 {
644 printf("Loading Rockbox..."); 402 printf("Loading Rockbox...");
645 rc = load_mi4(loadbuffer, BOOTFILE, MAX_LOADSIZE); 403 snprintf(filename,sizeof(filename), BOOTDIR "/%s", BOOTFILE);
646 if (rc < EOK) 404
405 rc = load_mi4(loadbuffer, filename, MAX_LOADSIZE);
406 if (rc <= EFILE_EMPTY)
647 { 407 {
648 bool old_verbose = verbose; 408 bool old_verbose = verbose;
649 verbose = true; 409 verbose = true;
650 printf("Can't load " BOOTFILE ": "); 410 printf("Can't load " BOOTFILE ": ");
651 printf(strerror(rc)); 411 printf(loader_strerror(rc));
652 verbose = old_verbose; 412 verbose = old_verbose;
653 btn |= BOOTLOADER_BOOT_OF; 413 btn |= BOOTLOADER_BOOT_OF;
654 sleep(5*HZ); 414 sleep(5*HZ);
@@ -676,9 +436,9 @@ void* main(void)
676 { 436 {
677 rc = load_mi4_part(loadbuffer, pinfo, MAX_LOADSIZE, 437 rc = load_mi4_part(loadbuffer, pinfo, MAX_LOADSIZE,
678 usb == USB_INSERTED); 438 usb == USB_INSERTED);
679 if (rc < EOK) { 439 if (rc <= EFILE_EMPTY) {
680 printf("Can't load from partition"); 440 printf("Can't load from partition");
681 printf(strerror(rc)); 441 printf(loader_strerror(rc));
682 } else { 442 } else {
683 goto main_exit; 443 goto main_exit;
684 } 444 }
@@ -690,9 +450,9 @@ void* main(void)
690#if defined(PHILIPS_HDD1630) || defined(PHILIPS_HDD6330) || defined(PHILIPS_SA9200) 450#if defined(PHILIPS_HDD1630) || defined(PHILIPS_HDD6330) || defined(PHILIPS_SA9200)
691 printf("Trying /System/OF.ebn"); 451 printf("Trying /System/OF.ebn");
692 rc=load_mi4(loadbuffer, "/System/OF.ebn", MAX_LOADSIZE); 452 rc=load_mi4(loadbuffer, "/System/OF.ebn", MAX_LOADSIZE);
693 if (rc < EOK) { 453 if (rc <= EFILE_EMPTY) {
694 printf("Can't load /System/OF.ebn"); 454 printf("Can't load /System/OF.ebn");
695 printf(strerror(rc)); 455 printf(loader_strerror(rc));
696 } else { 456 } else {
697 goto main_exit; 457 goto main_exit;
698 } 458 }
@@ -700,9 +460,9 @@ void* main(void)
700 460
701 printf("Trying /System/OF.mi4"); 461 printf("Trying /System/OF.mi4");
702 rc=load_mi4(loadbuffer, "/System/OF.mi4", MAX_LOADSIZE); 462 rc=load_mi4(loadbuffer, "/System/OF.mi4", MAX_LOADSIZE);
703 if (rc < EOK) { 463 if (rc <= EFILE_EMPTY) {
704 printf("Can't load /System/OF.mi4"); 464 printf("Can't load /System/OF.mi4");
705 printf(strerror(rc)); 465 printf(loader_strerror(rc));
706 } else { 466 } else {
707#if defined(SAMSUNG_YH925) 467#if defined(SAMSUNG_YH925)
708 lcd_reset(); 468 lcd_reset();
@@ -712,9 +472,9 @@ void* main(void)
712 472
713 printf("Trying /System/OF.bin"); 473 printf("Trying /System/OF.bin");
714 rc=load_raw_firmware(loadbuffer, "/System/OF.bin", MAX_LOADSIZE); 474 rc=load_raw_firmware(loadbuffer, "/System/OF.bin", MAX_LOADSIZE);
715 if (rc < EOK) { 475 if (rc <= EFILE_EMPTY) {
716 printf("Can't load /System/OF.bin"); 476 printf("Can't load /System/OF.bin");
717 printf(strerror(rc)); 477 printf(loader_strerror(rc));
718 } else { 478 } else {
719#if defined(SAMSUNG_YH925) 479#if defined(SAMSUNG_YH925)
720 lcd_reset(); 480 lcd_reset();