summaryrefslogtreecommitdiff
path: root/rbutil/mkamsboot/mkamsboot.c
diff options
context:
space:
mode:
authorThomas Martitz <kugel@rockbox.org>2009-07-05 02:30:33 +0000
committerThomas Martitz <kugel@rockbox.org>2009-07-05 02:30:33 +0000
commitf9eeab2b6ce93ada34f9197a84db275acce0a050 (patch)
tree998abe3db476228eb2560fe754851939d1cf3352 /rbutil/mkamsboot/mkamsboot.c
parent7131ddb105fe4f3f8f9f3d500a12ac24cf051c9a (diff)
downloadrockbox-f9eeab2b6ce93ada34f9197a84db275acce0a050.tar.gz
rockbox-f9eeab2b6ce93ada34f9197a84db275acce0a050.zip
Slightly rewrite some parts of mkamsboot for better output and some sanity in the early array declarations. No functional change.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21648 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'rbutil/mkamsboot/mkamsboot.c')
-rw-r--r--rbutil/mkamsboot/mkamsboot.c203
1 files changed, 106 insertions, 97 deletions
diff --git a/rbutil/mkamsboot/mkamsboot.c b/rbutil/mkamsboot/mkamsboot.c
index 9a80d4892a..ddecc96a88 100644
--- a/rbutil/mkamsboot/mkamsboot.c
+++ b/rbutil/mkamsboot/mkamsboot.c
@@ -122,63 +122,76 @@ enum {
122 MODEL_C200V2, 122 MODEL_C200V2,
123}; 123};
124 124
125/* 4 for m200, 2 for e200/c200, 1 or 2 for fuze/clop */
126static const unsigned short hw_revisions[] = {
127 [MODEL_FUZE] = 1,
128 [MODEL_CLIP] = 1,
129 [MODEL_CLIPV2] = 2,
130 [MODEL_E200V2] = 2,
131 [MODEL_M200V4] = 4,
132 [MODEL_C200V2] = 2,
133};
134
135/* version 2 is used in Clipv2 and Fuzev2 firmwares */
136static const unsigned short fw_revisions[] = {
137 [MODEL_FUZE] = 1,
138 [MODEL_CLIP] = 1,
139 [MODEL_CLIPV2] = 2,
140 [MODEL_E200V2] = 1,
141 [MODEL_M200V4] = 1,
142 [MODEL_C200V2] = 1,
143};
144
125/* Descriptive name of these models */ 145/* Descriptive name of these models */
126static const char* model_names[] = { 146static const char* model_names[] = {
127 "Fuze", 147 [MODEL_FUZE] = "Fuze",
128 "Clip", 148 [MODEL_CLIP] = "Clip",
129 "Clip v2", 149 [MODEL_CLIPV2] = "Clip",
130 "e200 v2", 150 [MODEL_E200V2] = "e200",
131 "m200 v4", 151 [MODEL_M200V4] = "m200",
132 "c200 v2" 152 [MODEL_C200V2] = "c200",
133}; 153};
134 154
135/* Dualboot functions for these models */ 155/* Dualboot functions for these models */
136static const unsigned char* bootloaders[] = { 156static const unsigned char* bootloaders[] = {
137 dualboot_fuze, 157 [MODEL_FUZE] = dualboot_fuze,
138 dualboot_clip, 158 [MODEL_CLIP] = dualboot_clip,
139 dualboot_clipv2, 159 [MODEL_CLIPV2] = dualboot_clipv2,
140 dualboot_e200v2, 160 [MODEL_E200V2] = dualboot_e200v2,
141 dualboot_m200v4, 161 [MODEL_M200V4] = dualboot_m200v4,
142 dualboot_c200v2, 162 [MODEL_C200V2] = dualboot_c200v2,
143}; 163};
144 164
145/* Size of dualboot functions for these models */ 165/* Size of dualboot functions for these models */
146static const int bootloader_sizes[] = { 166static const int bootloader_sizes[] = {
147 sizeof(dualboot_fuze), 167 [MODEL_FUZE] = sizeof(dualboot_fuze),
148 sizeof(dualboot_clip), 168 [MODEL_CLIP] = sizeof(dualboot_clip),
149 sizeof(dualboot_clipv2), 169 [MODEL_CLIPV2] = sizeof(dualboot_clipv2),
150 sizeof(dualboot_e200v2), 170 [MODEL_E200V2] = sizeof(dualboot_e200v2),
151 sizeof(dualboot_m200v4), 171 [MODEL_M200V4] = sizeof(dualboot_m200v4),
152 sizeof(dualboot_c200v2), 172 [MODEL_C200V2] = sizeof(dualboot_c200v2),
153}; 173};
154 174
155/* Model names used in the Rockbox header in ".sansa" files - these match the 175/* Model names used in the Rockbox header in ".sansa" files - these match the
156 -add parameter to the "scramble" tool */ 176 -add parameter to the "scramble" tool */
157static const char* rb_model_names[] = { 177static const char* rb_model_names[] = {
158 "fuze", 178 [MODEL_FUZE] = "fuze",
159 "clip", 179 [MODEL_CLIP] = "clip",
160 "clv2", 180 [MODEL_CLIPV2] = "clv2",
161 "e2v2", 181 [MODEL_E200V2] = "e2v2",
162 "m2v4", 182 [MODEL_M200V4] = "m2v4",
163 "c2v2", 183 [MODEL_C200V2] = "c2v2",
164}; 184};
165 185
166/* Model numbers used to initialise the checksum in the Rockbox header in 186/* Model numbers used to initialise the checksum in the Rockbox header in
167 ".sansa" files - these are the same as MODEL_NUMBER in config-target.h */ 187 ".sansa" files - these are the same as MODEL_NUMBER in config-target.h */
168static const int rb_model_num[] = { 188static const int rb_model_num[] = {
169 43, 189 [MODEL_FUZE] = 43,
170 40, 190 [MODEL_CLIP] = 40,
171 60, 191 [MODEL_CLIPV2] = 60,
172 41, 192 [MODEL_E200V2] = 41,
173 42, 193 [MODEL_M200V4] = 42,
174 44 194 [MODEL_C200V2] = 44
175};
176
177struct md5sums {
178 int model;
179 char *version;
180 int fw_version; /* version 2 is used in Clipv2 and Fuzev2 firmwares */
181 char *md5;
182}; 195};
183 196
184/* Checksums of unmodified original firmwares - for safety, and device 197/* Checksums of unmodified original firmwares - for safety, and device
@@ -187,31 +200,31 @@ static struct md5sums sansasums[] = {
187 /* NOTE: Different regional versions of the firmware normally only 200 /* NOTE: Different regional versions of the firmware normally only
188 differ in the filename - the md5sums are identical */ 201 differ in the filename - the md5sums are identical */
189 202
190 /* model version fw_version md5 */ 203 /* model version md5 */
191 { MODEL_E200V2, "3.01.11", 1, "e622ca8cb6df423f54b8b39628a1f0a3" }, 204 { MODEL_E200V2, "3.01.11", "e622ca8cb6df423f54b8b39628a1f0a3" },
192 { MODEL_E200V2, "3.01.14", 1, "2c1d0383fc3584b2cc83ba8cc2243af6" }, 205 { MODEL_E200V2, "3.01.14", "2c1d0383fc3584b2cc83ba8cc2243af6" },
193 { MODEL_E200V2, "3.01.16", 1, "12563ad71b25a1034cf2092d1e0218c4" }, 206 { MODEL_E200V2, "3.01.16", "12563ad71b25a1034cf2092d1e0218c4" },
194 207
195 { MODEL_FUZE, "1.01.11", 1, "cac8ffa03c599330ac02c4d41de66166" }, 208 { MODEL_FUZE, "1.01.11", "cac8ffa03c599330ac02c4d41de66166" },
196 { MODEL_FUZE, "1.01.15", 1, "df0e2c1612727f722c19a3c764cff7f2" }, 209 { MODEL_FUZE, "1.01.15", "df0e2c1612727f722c19a3c764cff7f2" },
197 { MODEL_FUZE, "1.01.22", 1, "5aff5486fe8dd64239cc71eac470af98" }, 210 { MODEL_FUZE, "1.01.22", "5aff5486fe8dd64239cc71eac470af98" },
198 { MODEL_FUZE, "1.02.26", 1, "7c632c479461c48c8833baed74eb5e4f" }, 211 { MODEL_FUZE, "1.02.26", "7c632c479461c48c8833baed74eb5e4f" },
199 212
200 { MODEL_C200V2, "3.02.05", 1, "b6378ebd720b0ade3fad4dc7ab61c1a5" }, 213 { MODEL_C200V2, "3.02.05", "b6378ebd720b0ade3fad4dc7ab61c1a5" },
201 214
202 { MODEL_M200V4, "4.00.45", 1, "82e3194310d1514e3bbcd06e84c4add3" }, 215 { MODEL_M200V4, "4.00.45", "82e3194310d1514e3bbcd06e84c4add3" },
203 { MODEL_M200V4, "4.01.08-A", 1, "fc9dd6116001b3e6a150b898f1b091f0" }, 216 { MODEL_M200V4, "4.01.08-A", "fc9dd6116001b3e6a150b898f1b091f0" },
204 { MODEL_M200V4, "4.01.08-E", 1, "d3fb7d8ec8624ee65bc99f8dab0e2369" }, 217 { MODEL_M200V4, "4.01.08-E", "d3fb7d8ec8624ee65bc99f8dab0e2369" },
205 218
206 { MODEL_CLIP, "1.01.17", 1, "12caad785d506219d73f538772afd99e" }, 219 { MODEL_CLIP, "1.01.17", "12caad785d506219d73f538772afd99e" },
207 { MODEL_CLIP, "1.01.18", 1, "d720b266bd5afa38a198986ef0508a45" }, 220 { MODEL_CLIP, "1.01.18", "d720b266bd5afa38a198986ef0508a45" },
208 { MODEL_CLIP, "1.01.20", 1, "236d8f75189f468462c03f6d292cf2ac" }, 221 { MODEL_CLIP, "1.01.20", "236d8f75189f468462c03f6d292cf2ac" },
209 { MODEL_CLIP, "1.01.29", 1, "c12711342169c66e209540cd1f27cd26" }, 222 { MODEL_CLIP, "1.01.29", "c12711342169c66e209540cd1f27cd26" },
210 { MODEL_CLIP, "1.01.30", 1, "f2974d47c536549c9d8259170f1dbe4d" }, 223 { MODEL_CLIP, "1.01.30", "f2974d47c536549c9d8259170f1dbe4d" },
211 { MODEL_CLIP, "1.01.32", 1, "d835d12342500732ffb9c4ee54abec15" }, 224 { MODEL_CLIP, "1.01.32", "d835d12342500732ffb9c4ee54abec15" },
212 225
213 { MODEL_CLIPV2, "2.01.16", 2, "c57fb3fcbe07c2c9b360f060938f80cb" }, 226 { MODEL_CLIPV2, "2.01.16", "c57fb3fcbe07c2c9b360f060938f80cb" },
214 { MODEL_CLIPV2, "2.01.32", 2, "0ad3723e52022509089d938d0fbbf8c5" } 227 { MODEL_CLIPV2, "2.01.32", "0ad3723e52022509089d938d0fbbf8c5" }
215}; 228};
216 229
217#define NUM_MD5S (sizeof(sansasums)/sizeof(sansasums[0])) 230#define NUM_MD5S (sizeof(sansasums)/sizeof(sansasums[0]))
@@ -333,8 +346,8 @@ static unsigned char* uclpack(unsigned char* inbuf, int insize, int* outsize)
333 346
334/* Loads a Sansa AMS Original Firmware file into memory */ 347/* Loads a Sansa AMS Original Firmware file into memory */
335unsigned char* load_of_file( 348unsigned char* load_of_file(
336 char* filename, off_t* bufsize, char* md5sum, int* model, 349 char* filename, off_t* bufsize, struct md5sums *sum,
337 int* fw_version, int* firmware_size, unsigned char** of_packed, 350 int* firmware_size, unsigned char** of_packed,
338 int* of_packedsize, char* errstr, int errstrsize) 351 int* of_packedsize, char* errstr, int errstrsize)
339{ 352{
340 int fd; 353 int fd;
@@ -363,34 +376,28 @@ unsigned char* load_of_file(
363 /* check the file */ 376 /* check the file */
364 377
365 /* Calculate MD5 checksum of OF */ 378 /* Calculate MD5 checksum of OF */
366 calc_MD5(buf, *bufsize, md5sum); 379 calc_MD5(buf, *bufsize, sum->md5);
367 380
368 while ((i < NUM_MD5S) && (strcmp(sansasums[i].md5, md5sum) != 0)) 381 while ((i < NUM_MD5S) && (strcmp(sansasums[i].md5, sum->md5) != 0))
369 i++; 382 i++;
370 383
371 if (i < NUM_MD5S) { 384 if (i < NUM_MD5S) {
372 *model = sansasums[i].model; 385 *sum = sansasums[i];
373 *fw_version = sansasums[i].fw_version;
374 } else { 386 } else {
375 if (get_uint32le(&buf[0x204])==0x0000f000) { 387 int fw_version = (get_uint32le(&buf[0x204]) == 0x0000f000) ? 2 : 1;
376 *fw_version = 2; 388 model_id = buf[(fw_version == 2) ? 0x219 : 0x215];
377 model_id = buf[0x219]; 389 sum->model = get_model(model_id);
378 } else { 390
379 *fw_version = 1; 391 if (sum->model == MODEL_UNKNOWN)
380 model_id = buf[0x215];
381 }
382 *model = get_model(model_id);
383
384 if (*model == MODEL_UNKNOWN)
385 ERROR("[ERR] Unknown firmware model (v%d) - model id 0x%02x\n", 392 ERROR("[ERR] Unknown firmware model (v%d) - model id 0x%02x\n",
386 *fw_version, model_id); 393 fw_version, model_id);
387 394
388#if 1 /* comment to test new OFs */ 395#if 1 /* comment to test new OFs */
389 char tested_versions[100]; 396 char tested_versions[100];
390 tested_versions[0] = '\0'; 397 tested_versions[0] = '\0';
391 398
392 for (i = 0; i < NUM_MD5S ; i++) 399 for (i = 0; i < NUM_MD5S ; i++)
393 if (sansasums[i].model == *model) { 400 if (sansasums[i].model == sum->model) {
394 if (tested_versions[0] != '\0') { 401 if (tested_versions[0] != '\0') {
395 strncat(tested_versions, ", ", 402 strncat(tested_versions, ", ",
396 sizeof(tested_versions) - strlen(tested_versions) - 1); 403 sizeof(tested_versions) - strlen(tested_versions) - 1);
@@ -401,7 +408,7 @@ unsigned char* load_of_file(
401 408
402 ERROR("[ERR] Original firmware unknown, please try an other version." \ 409 ERROR("[ERR] Original firmware unknown, please try an other version." \
403 " Tested %s versions are : %s\n", 410 " Tested %s versions are : %s\n",
404 model_names[*model], tested_versions); 411 model_names[sum->model], tested_versions);
405#endif 412#endif
406 } 413 }
407 414
@@ -412,14 +419,13 @@ unsigned char* load_of_file(
412 if (checksum != 0xefbeadde && checksum != calc_checksum(buf, last_word)) 419 if (checksum != 0xefbeadde && checksum != calc_checksum(buf, last_word))
413 ERROR("%s", "[ERR] Whole file checksum failed\n"); 420 ERROR("%s", "[ERR] Whole file checksum failed\n");
414 421
415 if (bootloaders[*model] == NULL) 422 if (bootloaders[sum->model] == NULL)
416 ERROR("[ERR] Unsupported model - \"%s\"\n", model_names[*model]); 423 ERROR("[ERR] Unsupported model - \"%s\"\n", model_names[sum->model]);
417
418 424
419 /* Get the firmware size */ 425 /* Get the firmware size */
420 if (*fw_version == 1) 426 if (fw_revisions[sum->model] == 1)
421 *firmware_size = get_uint32le(&buf[0x0c]); 427 *firmware_size = get_uint32le(&buf[0x0c]);
422 else /* fw_version == 2 */ 428 else if (fw_revisions[sum->model] == 2)
423 *firmware_size = get_uint32le(&buf[0x10]); 429 *firmware_size = get_uint32le(&buf[0x10]);
424 430
425 /* Compress the original firmware image */ 431 /* Compress the original firmware image */
@@ -498,7 +504,7 @@ error:
498 504
499/* Patches a Sansa AMS Original Firmware file */ 505/* Patches a Sansa AMS Original Firmware file */
500void patch_firmware( 506void patch_firmware(
501 int model, int fw_version, int firmware_size, unsigned char* buf, 507 int model, int fw_revision, int firmware_size, unsigned char* buf,
502 int len, unsigned char* of_packed, int of_packedsize, 508 int len, unsigned char* of_packed, int of_packedsize,
503 unsigned char* rb_packed, int rb_packedsize) 509 unsigned char* rb_packed, int rb_packedsize)
504{ 510{
@@ -548,10 +554,10 @@ void patch_firmware(
548 /* Update the firmware block checksum */ 554 /* Update the firmware block checksum */
549 sum = calc_checksum(buf + 0x400, firmware_size); 555 sum = calc_checksum(buf + 0x400, firmware_size);
550 556
551 if (fw_version == 1) { 557 if (fw_revision == 1) {
552 put_uint32le(&buf[0x04], sum); 558 put_uint32le(&buf[0x04], sum);
553 put_uint32le(&buf[0x204], sum); 559 put_uint32le(&buf[0x204], sum);
554 } else { 560 } else if (fw_revision == 2) {
555 put_uint32le(&buf[0x08], sum); 561 put_uint32le(&buf[0x08], sum);
556 put_uint32le(&buf[0x208], sum); 562 put_uint32le(&buf[0x208], sum);
557 563
@@ -586,15 +592,16 @@ int main(int argc, char* argv[])
586 unsigned char* buf; 592 unsigned char* buf;
587 int firmware_size; 593 int firmware_size;
588 int bootloader_size; 594 int bootloader_size;
589 int model;
590 unsigned char* of_packed; 595 unsigned char* of_packed;
591 int of_packedsize; 596 int of_packedsize;
592 unsigned char* rb_packed; 597 unsigned char* rb_packed;
593 int rb_packedsize; 598 int rb_packedsize;
594 int fw_version;
595 int totalsize; 599 int totalsize;
596 char md5sum[33]; /* 32 hex digits, plus terminating zero */
597 char errstr[200]; 600 char errstr[200];
601 struct md5sums sum;
602 char md5sum[33]; /* 32 digits + \0 */
603
604 sum.md5 = md5sum;
598 605
599 fprintf(stderr, 606 fprintf(stderr,
600"mkamsboot v" VERSION " - (C) Dave Chapman and Rafaël Carré 2008\n" 607"mkamsboot v" VERSION " - (C) Dave Chapman and Rafaël Carré 2008\n"
@@ -612,7 +619,7 @@ int main(int argc, char* argv[])
612 outfile = argv[3]; 619 outfile = argv[3];
613 620
614 /* Load original firmware file */ 621 /* Load original firmware file */
615 buf = load_of_file(infile, &len, md5sum, &model, &fw_version, 622 buf = load_of_file(infile, &len, &sum,
616 &firmware_size, &of_packed, &of_packedsize, errstr, sizeof(errstr)); 623 &firmware_size, &of_packed, &of_packedsize, errstr, sizeof(errstr));
617 624
618 if (buf == NULL) { 625 if (buf == NULL) {
@@ -621,12 +628,13 @@ int main(int argc, char* argv[])
621 return 1; 628 return 1;
622 } 629 }
623 630
624 fprintf(stderr, "[INFO] Original firmware MD5 checksum match - %s\n", 631 fprintf(stderr, "[INFO] Original firmware MD5 checksum match\n");
625 model_names[model]); 632 fprintf(stderr, "[INFO] Model: Sansa %s v%d - Firmware version: %s\n",
633 model_names[sum.model], hw_revisions[sum.model], sum.version);
626 634
627 635
628 /* Load bootloader file */ 636 /* Load bootloader file */
629 rb_packed = load_rockbox_file(bootfile, model, &bootloader_size, 637 rb_packed = load_rockbox_file(bootfile, sum.model, &bootloader_size,
630 &rb_packedsize, errstr, sizeof(errstr)); 638 &rb_packedsize, errstr, sizeof(errstr));
631 if (rb_packed == NULL) { 639 if (rb_packed == NULL) {
632 fprintf(stderr, "%s", errstr); 640 fprintf(stderr, "%s", errstr);
@@ -636,7 +644,7 @@ int main(int argc, char* argv[])
636 return 1; 644 return 1;
637 } 645 }
638 646
639 printf("[INFO] Patching %s firmware\n", model_names[model]); 647 printf("[INFO] Firmware patching has begun !\n\n");
640 648
641 fprintf(stderr, "[INFO] Original firmware size: %d bytes\n", 649 fprintf(stderr, "[INFO] Original firmware size: %d bytes\n",
642 firmware_size); 650 firmware_size);
@@ -647,11 +655,11 @@ int main(int argc, char* argv[])
647 fprintf(stderr, "[INFO] Packed bootloader size: %d bytes\n", 655 fprintf(stderr, "[INFO] Packed bootloader size: %d bytes\n",
648 rb_packedsize); 656 rb_packedsize);
649 fprintf(stderr, "[INFO] Dual-boot function size: %d bytes\n", 657 fprintf(stderr, "[INFO] Dual-boot function size: %d bytes\n",
650 bootloader_sizes[model]); 658 bootloader_sizes[sum.model]);
651 fprintf(stderr, "[INFO] UCL unpack function size: %d bytes\n", 659 fprintf(stderr, "[INFO] UCL unpack function size: %u bytes\n",
652 sizeof(nrv2e_d8)); 660 sizeof(nrv2e_d8));
653 661
654 totalsize = total_size(model, of_packedsize, rb_packedsize); 662 totalsize = total_size(sum.model, of_packedsize, rb_packedsize);
655 663
656 fprintf(stderr, "[INFO] Total size of new image: %d bytes\n", totalsize); 664 fprintf(stderr, "[INFO] Total size of new image: %d bytes\n", totalsize);
657 665
@@ -663,8 +671,8 @@ int main(int argc, char* argv[])
663 return 1; 671 return 1;
664 } 672 }
665 673
666 patch_firmware(model, fw_version, firmware_size, buf, len, of_packed, 674 patch_firmware(sum.model, fw_revisions[sum.model], firmware_size, buf, len,
667 of_packedsize, rb_packed, rb_packedsize); 675 of_packed, of_packedsize, rb_packed, rb_packedsize);
668 676
669 /* Write the new firmware */ 677 /* Write the new firmware */
670 fdout = open(outfile, O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, 0666); 678 fdout = open(outfile, O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, 0666);
@@ -691,6 +699,7 @@ int main(int argc, char* argv[])
691 free(buf); 699 free(buf);
692 free(of_packed); 700 free(of_packed);
693 free(rb_packed); 701 free(rb_packed);
702 fprintf(stderr, "\n[INFO] Patching succeeded!\n");
694 703
695 return 0; 704 return 0;
696} 705}