1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * mkamsboot.c - a tool for merging bootloader code into an Sansa V2
13 * Copyright (C) 2008 Dave Chapman
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
27 Insert a Rockbox bootloader into a Sansa AMS original firmware file.
29 Layout of a Sansa AMS original firmware file:
31 ---------------------- 0x0
33 |----------------------| 0x400
34 | FIRMWARE BLOCK | (contains the OF version on some fuzev2 firmwares
35 |----------------------| 0x600
37 |----------------------| 0x400 + firmware block size
39 ---------------------- END
41 We replace the main firmware block while preserving the potential OF version
42 (bytes 0x600..0x400+firmware_size) as follows:
45 ---------------------- 0x0
49 |----------------------|
51 |----------------------|
53 | compressed RB image |
55 |----------------------|
57 | compressed OF image |
59 |----------------------|
60 | UCL unpack function |
61 ----------------------
63 This entire block fits into the space previously occupied by the main
64 firmware block - the space saved by compressing the OF image is used
65 to store the compressed version of the Rockbox bootloader.
67 On version 1 firmwares, the OF image is typically about 120KB, which allows
68 us to store a Rockbox bootloader with an uncompressed size of about 60KB-70KB.
69 Version 2 firmwares are bigger and are stored in SDRAM (instead of IRAM).
70 In both cases, the RAM we are using is mapped at offset 0x0.
72 mkamsboot then corrects the checksums and writes a new legal firmware
73 file which can be installed on the device.
75 When the Sansa device boots, this firmware block is loaded to RAM at
76 address 0x0 and executed.
78 Firstly, the dual-boot code will copy the UCL unpack function to the
81 Then, depending on the detection of the dual-boot keypress, either the
82 OF image or the Rockbox image is copied to the end of RAM (just before
83 the ucl unpack function) and uncompressed to the start of RAM.
85 Finally, the ucl unpack function branches to address 0x0, passing
86 execution to the uncompressed firmware.
94 #include <sys/types.h>
102 #include "mkamsboot.h"
106 /* Header for ARM code binaries */
107 #include "dualboot.h"
109 /* Win32 compatibility */
114 /* 4 for m200, 2 for e200/c200, 1 or 2 for fuze/clip, 1 for clip+ */
115 const unsigned short hw_revisions
[] = {
126 /* version 2 is used in Clipv2, Clip+ and Fuzev2 firmwares */
127 const unsigned short fw_revisions
[] = {
138 /* Descriptive name of these models */
139 const char* model_names
[] = {
140 [MODEL_FUZE
] = "Fuze",
141 [MODEL_CLIP
] = "Clip",
142 [MODEL_CLIPV2
] = "Clip",
143 [MODEL_CLIPPLUS
]= "Clip+",
144 [MODEL_E200V2
] = "e200",
145 [MODEL_M200V4
] = "m200",
146 [MODEL_C200V2
] = "c200",
147 [MODEL_FUZEV2
] = "Fuze",
150 /* Dualboot functions for these models */
151 static const unsigned char* bootloaders
[] = {
152 [MODEL_FUZE
] = dualboot_fuze
,
153 [MODEL_CLIP
] = dualboot_clip
,
154 [MODEL_CLIPV2
] = dualboot_clipv2
,
155 [MODEL_E200V2
] = dualboot_e200v2
,
156 [MODEL_M200V4
] = dualboot_m200v4
,
157 [MODEL_C200V2
] = dualboot_c200v2
,
158 [MODEL_CLIPPLUS
]= dualboot_clipplus
,
159 [MODEL_FUZEV2
] = dualboot_fuzev2
,
162 /* Size of dualboot functions for these models */
163 const int bootloader_sizes
[] = {
164 [MODEL_FUZE
] = sizeof(dualboot_fuze
),
165 [MODEL_CLIP
] = sizeof(dualboot_clip
),
166 [MODEL_CLIPV2
] = sizeof(dualboot_clipv2
),
167 [MODEL_E200V2
] = sizeof(dualboot_e200v2
),
168 [MODEL_M200V4
] = sizeof(dualboot_m200v4
),
169 [MODEL_C200V2
] = sizeof(dualboot_c200v2
),
170 [MODEL_CLIPPLUS
]= sizeof(dualboot_clipplus
),
171 [MODEL_FUZEV2
] = sizeof(dualboot_fuzev2
),
174 /* Model names used in the Rockbox header in ".sansa" files - these match the
175 -add parameter to the "scramble" tool */
176 static const char* rb_model_names
[] = {
177 [MODEL_FUZE
] = "fuze",
178 [MODEL_CLIP
] = "clip",
179 [MODEL_CLIPV2
] = "clv2",
180 [MODEL_E200V2
] = "e2v2",
181 [MODEL_M200V4
] = "m2v4",
182 [MODEL_C200V2
] = "c2v2",
183 [MODEL_CLIPPLUS
]= "cli+",
184 [MODEL_FUZEV2
] = "fuz2",
187 /* Model numbers used to initialise the checksum in the Rockbox header in
188 ".sansa" files - these are the same as MODEL_NUMBER in config-target.h */
189 static const int rb_model_num
[] = {
196 [MODEL_CLIPPLUS
]= 66,
200 /* Checksums of unmodified original firmwares - for safety, and device
202 static struct md5sums sansasums
[] = {
203 /* NOTE: Different regional versions of the firmware normally only
204 differ in the filename - the md5sums are identical */
206 /* model version md5 */
207 { MODEL_E200V2
, "3.01.11", "e622ca8cb6df423f54b8b39628a1f0a3" },
208 { MODEL_E200V2
, "3.01.14", "2c1d0383fc3584b2cc83ba8cc2243af6" },
209 { MODEL_E200V2
, "3.01.16", "12563ad71b25a1034cf2092d1e0218c4" },
211 { MODEL_FUZE
, "1.01.11", "cac8ffa03c599330ac02c4d41de66166" },
212 { MODEL_FUZE
, "1.01.15", "df0e2c1612727f722c19a3c764cff7f2" },
213 { MODEL_FUZE
, "1.01.22", "5aff5486fe8dd64239cc71eac470af98" },
214 { MODEL_FUZE
, "1.02.26", "7c632c479461c48c8833baed74eb5e4f" },
215 { MODEL_FUZE
, "1.02.28", "5b34260f6470e75f702a9c6825471752" },
216 { MODEL_FUZE
, "1.02.31", "66d01b37462a5ef7ccc6ad37188b4235" },
218 { MODEL_C200V2
, "3.02.05", "b6378ebd720b0ade3fad4dc7ab61c1a5" },
220 { MODEL_M200V4
, "4.00.45", "82e3194310d1514e3bbcd06e84c4add3" },
221 { MODEL_M200V4
, "4.01.08-A", "fc9dd6116001b3e6a150b898f1b091f0" },
222 { MODEL_M200V4
, "4.01.08-E", "d3fb7d8ec8624ee65bc99f8dab0e2369" },
224 { MODEL_CLIP
, "1.01.17", "12caad785d506219d73f538772afd99e" },
225 { MODEL_CLIP
, "1.01.18", "d720b266bd5afa38a198986ef0508a45" },
226 { MODEL_CLIP
, "1.01.20", "236d8f75189f468462c03f6d292cf2ac" },
227 { MODEL_CLIP
, "1.01.29", "c12711342169c66e209540cd1f27cd26" },
228 { MODEL_CLIP
, "1.01.30", "f2974d47c536549c9d8259170f1dbe4d" },
229 { MODEL_CLIP
, "1.01.32", "d835d12342500732ffb9c4ee54abec15" },
230 { MODEL_CLIP
, "1.01.35", "b4d0edb3b8f2a4e8eee0a344f7f8e480" },
232 { MODEL_CLIPV2
, "2.01.16", "c57fb3fcbe07c2c9b360f060938f80cb" },
233 { MODEL_CLIPV2
, "2.01.32", "0ad3723e52022509089d938d0fbbf8c5" },
234 { MODEL_CLIPV2
, "2.01.35", "a3cbbd22b9508d7f8a9a1a39acc342c2" },
236 { MODEL_CLIPPLUS
, "01.02.09", "656d38114774c2001dc18e6726df3c5d" },
237 { MODEL_CLIPPLUS
, "01.02.13", "5f89872b79ef440b0e5ee3a7a44328b2" },
238 { MODEL_CLIPPLUS
, "01.02.15", "680a4f521e790ad25b93b1b16f3a207d" },
240 { MODEL_FUZEV2
, "2.01.17", "8b85fb05bf645d08a4c8c3e344ec9ebe" },
241 { MODEL_FUZEV2
, "2.02.26", "d4f6f85c3e4a8ea8f2e5acc421641801" },
242 { MODEL_FUZEV2
, "2.03.31", "74fb197ccd51707388f3b233402186a6" },
243 { MODEL_FUZEV2
, "2.03.33", "1599cc73d02ea7fe53fe2d4379c24b66" },
246 #define NUM_MD5S (sizeof(sansasums)/sizeof(sansasums[0]))
248 static unsigned int model_memory_size(int model
)
250 /* The OF boots with IRAM (320kB) mapped at 0x0 */
252 if(model
== MODEL_CLIPV2
)
254 /* The decompressed Clipv2 OF is around 380kB.
255 * Let's use the full IRAM (1MB on AMSv2)
261 /* The IRAM is 320kB on AMSv1, and 320 will be enough on Fuzev1/Clip+ */
266 int firmware_revision(int model
)
268 return fw_revisions
[model
];
271 static off_t
filesize(int fd
)
275 if (fstat(fd
, &buf
) < 0) {
276 perror("[ERR] Checking filesize of input file");
283 static uint32_t get_uint32le(unsigned char* p
)
285 return p
[0] | (p
[1] << 8) | (p
[2] << 16) | (p
[3] << 24);
288 static uint32_t get_uint32be(unsigned char* p
)
290 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
293 static void put_uint32le(unsigned char* p
, uint32_t x
)
296 p
[1] = (x
>> 8) & 0xff;
297 p
[2] = (x
>> 16) & 0xff;
298 p
[3] = (x
>> 24) & 0xff;
301 void calc_MD5(unsigned char* buf
, int len
, char *md5str
)
305 unsigned char md5sum
[16];
308 md5_update(&ctx
, buf
, len
);
309 md5_finish(&ctx
, md5sum
);
311 for (i
= 0; i
< 16; ++i
)
312 sprintf(md5str
+ 2*i
, "%02x", md5sum
[i
]);
315 /* Calculate a simple checksum used in Sansa Original Firmwares */
316 static uint32_t calc_checksum(unsigned char* buf
, uint32_t n
)
322 sum
+= get_uint32le(buf
+ i
);
327 /* Compress using nrv2e algorithm : Thumb decompressor fits in 168 bytes ! */
328 static unsigned char* uclpack(unsigned char* inbuf
, int insize
, int* outsize
)
331 unsigned char* outbuf
;
334 /* The following formula comes from the UCL documentation */
335 maxsize
= insize
+ (insize
/ 8) + 256;
337 /* Allocate some memory for the output buffer */
338 outbuf
= malloc(maxsize
);
343 r
= ucl_nrv2e_99_compress(
344 (const ucl_bytep
) inbuf
,
350 if (r
!= UCL_E_OK
|| *outsize
> maxsize
) {
351 /* this should NEVER happen, and implies memory corruption */
352 fprintf(stderr
, "internal error - compression failed: %d\n", r
);
360 #define ERROR(format, ...) \
362 snprintf(errstr, errstrsize, format, __VA_ARGS__); \
366 /* Loads a Sansa AMS Original Firmware file into memory */
367 unsigned char* load_of_file(
368 char* filename
, int model
, off_t
* bufsize
, struct md5sums
*sum
,
369 int* firmware_size
, unsigned char** of_packed
,
370 int* of_packedsize
, char* errstr
, int errstrsize
)
373 unsigned char* buf
=NULL
;
377 unsigned int last_word
;
379 fd
= open(filename
, O_RDONLY
|O_BINARY
);
381 ERROR("[ERR] Could not open %s for reading\n", filename
);
383 *bufsize
= filesize(fd
);
385 buf
= malloc(*bufsize
);
387 ERROR("[ERR] Could not allocate memory for %s\n", filename
);
389 n
= read(fd
, buf
, *bufsize
);
392 ERROR("[ERR] Could not read file %s\n", filename
);
396 /* Calculate MD5 checksum of OF */
397 calc_MD5(buf
, *bufsize
, sum
->md5
);
399 while ((i
< NUM_MD5S
) && (strcmp(sansasums
[i
].md5
, sum
->md5
) != 0))
404 if(sum
->model
!= model
) {
405 ERROR("[ERR] OF File provided is %sv%d version %s, not for %sv%d\n",
406 model_names
[sum
->model
], hw_revisions
[sum
->model
],
407 sum
->version
, model_names
[model
], hw_revisions
[model
]
411 /* OF unknown, give a list of tested versions for the requested model */
413 char tested_versions
[100];
414 tested_versions
[0] = '\0';
416 for (i
= 0; i
< NUM_MD5S
; i
++)
417 if (sansasums
[i
].model
== model
) {
418 if (tested_versions
[0] != '\0') {
419 strncat(tested_versions
, ", ",
420 sizeof(tested_versions
) - strlen(tested_versions
) - 1);
422 strncat(tested_versions
, sansasums
[i
].version
,
423 sizeof(tested_versions
) - strlen(tested_versions
) - 1);
426 ERROR("[ERR] Original firmware unknown, please try an other version." \
427 " Tested %sv%d versions are : %s\n",
428 model_names
[model
], hw_revisions
[model
], tested_versions
);
431 /* TODO: Do some more sanity checks on the OF image. Some images (like
432 m200v4) dont have a checksum at the end, only padding (0xdeadbeef). */
433 last_word
= *bufsize
- 4;
434 checksum
= get_uint32le(buf
+ last_word
);
435 if (checksum
!= 0xefbeadde && checksum
!= calc_checksum(buf
, last_word
))
436 ERROR("%s", "[ERR] Whole file checksum failed\n");
438 if (bootloaders
[sum
->model
] == NULL
)
439 ERROR("[ERR] Unsupported model - \"%s\"\n", model_names
[sum
->model
]);
441 /* Get the firmware size */
442 if (fw_revisions
[sum
->model
] == 1)
443 *firmware_size
= get_uint32le(&buf
[0x0c]);
444 else if (fw_revisions
[sum
->model
] == 2)
445 *firmware_size
= get_uint32le(&buf
[0x10]);
447 /* Compress the original firmware image */
448 *of_packed
= uclpack(buf
+ 0x400, *firmware_size
, of_packedsize
);
449 if (*of_packed
== NULL
)
450 ERROR("[ERR] Could not compress %s\n", filename
);
459 /* Loads a rockbox bootloader file into memory */
460 unsigned char* load_rockbox_file(
461 char* filename
, int *model
, int* bufsize
, int* rb_packedsize
,
462 char* errstr
, int errstrsize
)
465 unsigned char* buf
= NULL
;
466 unsigned char* packed
= NULL
;
467 unsigned char header
[8];
472 fd
= open(filename
, O_RDONLY
|O_BINARY
);
474 ERROR("[ERR] Could not open %s for reading\n", filename
);
476 /* Read Rockbox header */
477 n
= read(fd
, header
, sizeof(header
));
478 if (n
!= sizeof(header
))
479 ERROR("[ERR] Could not read file %s\n", filename
);
481 for(*model
= 0; *model
< NUM_MODELS
; (*model
)++)
482 if (memcmp(rb_model_names
[*model
], header
+ 4, 4) == 0)
485 if(*model
== NUM_MODELS
)
486 ERROR("[ERR] Model name \"%4.4s\" unknown. Is this really a rockbox bootloader?\n", header
+ 4);
488 *bufsize
= filesize(fd
) - sizeof(header
);
490 buf
= malloc(*bufsize
);
492 ERROR("[ERR] Could not allocate memory for %s\n", filename
);
494 n
= read(fd
, buf
, *bufsize
);
497 ERROR("[ERR] Could not read file %s\n", filename
);
500 sum
= rb_model_num
[*model
];
501 for (i
= 0; i
< *bufsize
; i
++) {
502 /* add 8 unsigned bits but keep a 32 bit sum */
506 if (sum
!= get_uint32be(header
))
507 ERROR("[ERR] Checksum mismatch in %s\n", filename
);
509 packed
= uclpack(buf
, *bufsize
, rb_packedsize
);
511 ERROR("[ERR] Could not compress %s\n", filename
);
523 /* Patches a Sansa AMS Original Firmware file */
525 int model
, int fw_revision
, int firmware_size
, unsigned char* buf
,
526 int len
, unsigned char* of_packed
, int of_packedsize
,
527 unsigned char* rb_packed
, int rb_packedsize
)
530 uint32_t sum
, filesum
;
534 /* Zero the original firmware area - not needed, but helps debugging */
535 memset(buf
+ 0x600, 0, firmware_size
);
537 /* Insert dual-boot bootloader at offset 0x200, we preserve the OF
538 * version string located between 0x0 and 0x200 */
539 memcpy(buf
+ 0x600, bootloaders
[model
], bootloader_sizes
[model
]);
541 /* Insert vectors, they won't overwrite the OF version string */
543 /* Reset vector: branch 0x200 bytes away, to our dualboot code */
544 static const uint8_t b_0x200
[4] = { 0x7e, 0x00, 0x00, 0xea }; // b 0x200
545 memcpy(buf
+ 0x400, b_0x200
, sizeof(b_0x200
));
547 /* Other vectors: infinite loops */
548 static const uint8_t b_1b
[4] = { 0xfe, 0xff, 0xff, 0xea }; // 1: b 1b
549 for (i
=1; i
< 8; i
++)
550 memcpy(buf
+ 0x400 + 4*i
, b_1b
, sizeof(b_1b
));
552 /* We are filling the firmware buffer backwards from the end */
553 p
= buf
+ 0x400 + firmware_size
;
555 /* 1 - UCL unpack function */
556 p
-= sizeof(nrv2e_d8
);
557 memcpy(p
, nrv2e_d8
, sizeof(nrv2e_d8
));
559 /* 2 - Compressed copy of original firmware */
561 memcpy(p
, of_packed
, of_packedsize
);
563 /* 3 - Compressed copy of Rockbox bootloader */
565 memcpy(p
, rb_packed
, rb_packedsize
);
567 /* Write the locations of the various images to the variables at the
568 start of the dualboot image - we save the location of the last byte
569 in each image, along with the size in bytes */
571 /* UCL unpack function */
572 put_uint32le(&buf
[0x604], firmware_size
- 1);
573 put_uint32le(&buf
[0x608], sizeof(nrv2e_d8
));
575 /* Compressed original firmware image */
576 put_uint32le(&buf
[0x60c], firmware_size
- sizeof(nrv2e_d8
) - 1);
577 put_uint32le(&buf
[0x610], of_packedsize
);
579 /* Compressed Rockbox image */
580 put_uint32le(&buf
[0x614], firmware_size
- sizeof(nrv2e_d8
) - of_packedsize
582 put_uint32le(&buf
[0x618], rb_packedsize
);
584 ucl_dest
= model_memory_size(model
) - 1; /* last byte of memory */
585 put_uint32le(&buf
[0x61c], ucl_dest
);
587 /* Update the firmware block checksum */
588 sum
= calc_checksum(buf
+ 0x400, firmware_size
);
590 if (fw_revision
== 1) {
591 put_uint32le(&buf
[0x04], sum
);
592 put_uint32le(&buf
[0x204], sum
);
593 } else if (fw_revision
== 2) {
594 put_uint32le(&buf
[0x08], sum
);
595 put_uint32le(&buf
[0x208], sum
);
597 /* Update the header checksums */
598 put_uint32le(&buf
[0x1fc], calc_checksum(buf
, 0x1fc));
599 put_uint32le(&buf
[0x3fc], calc_checksum(buf
+ 0x200, 0x1fc));
602 /* Update the whole-file checksum */
604 for (i
=0;i
< (unsigned)len
- 4; i
+=4)
605 filesum
+= get_uint32le(&buf
[i
]);
607 put_uint32le(buf
+ len
- 4, filesum
);
610 /* returns != 0 if the firmware can be safely patched */
611 int check_sizes(int model
, int rb_packed_size
, int rb_unpacked_size
,
612 int of_packed_size
, int of_unpacked_size
, int *total_size
,
613 char *errstr
, int errstrsize
)
615 /* XXX: we keep the first 0x200 bytes block unmodified, we just replace
617 unsigned int packed_size
= bootloader_sizes
[model
] + sizeof(nrv2e_d8
) +
618 of_packed_size
+ rb_packed_size
+ 0x200;
620 /* how much memory is available */
621 unsigned int memory_size
= model_memory_size(model
);
623 /* the memory used when unpacking the OF */
624 unsigned int ram_of
= sizeof(nrv2e_d8
) + of_packed_size
+ of_unpacked_size
;
626 /* the memory used when unpacking the bootloader */
627 unsigned int ram_rb
= sizeof(nrv2e_d8
) + rb_packed_size
+ rb_unpacked_size
;
629 *total_size
= packed_size
;
631 #define ERROR(format, ...) \
633 snprintf(errstr, errstrsize, format, __VA_ARGS__); \
637 /* will packed data fit in the OF file ? */
638 if(packed_size
> of_unpacked_size
)
640 "[ERR] Packed data (%d bytes) doesn't fit in the firmware "
641 "(%d bytes)\n", packed_size
, of_unpacked_size
644 else if(ram_rb
> memory_size
)
645 ERROR("[ERR] Rockbox can't be unpacked at runtime, needs %d bytes "
646 "of memory and only %d available\n", ram_rb
, memory_size
649 else if(ram_of
> memory_size
)
650 ERROR("[ERR] OF can't be unpacked at runtime, needs %d bytes "
651 "of memory and only %d available\n", ram_of
, memory_size