1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * mkamsboot - 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 ****************************************************************************/
28 #include <sys/types.h>
36 #include "mkamsboot.h"
38 /* Header for ARM code binaries */
41 /* Win32 compatibility */
46 /* standalone executable */
47 int main(int argc
, char* argv
[])
49 char *infile
, *bootfile
, *outfile
;
56 unsigned char* of_packed
;
58 unsigned char* rb_packed
;
65 char md5sum
[33]; /* 32 digits + \0 */
69 /* VERSION comes frome the Makefile */
71 "mkamsboot Version " VERSION
"\n"
72 "This is free software; see the source for copying conditions. There is NO\n"
73 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
77 printf("Usage: mkamsboot <firmware file> <boot file> <output file>\n");
85 /* Load bootloader file */
86 rb_packed
= load_rockbox_file(bootfile
, &model
, &bootloader_size
,
87 &rb_packedsize
, errstr
, sizeof(errstr
));
88 if (rb_packed
== NULL
) {
89 fprintf(stderr
, "%s", errstr
);
90 fprintf(stderr
, "[ERR] Could not load %s\n", bootfile
);
94 /* Load original firmware file */
95 buf
= load_of_file(infile
, model
, &len
, &sum
,
96 &firmware_size
, &of_packed
, &of_packedsize
, errstr
, sizeof(errstr
));
100 fprintf(stderr
, "%s", errstr
);
101 fprintf(stderr
, "[ERR] Could not load %s\n", infile
);
105 fprintf(stderr
, "[INFO] Original firmware MD5 checksum match\n");
106 fprintf(stderr
, "[INFO] Model: Sansa %s v%d - Firmware version: %s\n",
107 model_names
[sum
.model
], hw_revisions
[sum
.model
], sum
.version
);
110 printf("[INFO] Firmware patching has begun !\n\n");
112 fprintf(stderr
, "[INFO] Original firmware size: %8d bytes\n",
114 fprintf(stderr
, "[INFO] Packed OF size: %8d bytes\n",
116 fprintf(stderr
, "[INFO] Bootloader size: %8d bytes\n",
117 (int)bootloader_size
);
118 fprintf(stderr
, "[INFO] Packed bootloader size: %8d bytes\n",
120 fprintf(stderr
, "[INFO] Dual-boot function size: %8d bytes\n",
121 bootloader_sizes
[sum
.model
]);
122 fprintf(stderr
, "[INFO] UCL unpack function size: %8u bytes\n",
123 (unsigned int)sizeof(nrv2e_d8
));
124 fprintf(stderr
, "[INFO] Original firmware version: %8u bytes\n",
127 patchable
= check_sizes(sum
.model
, rb_packedsize
, bootloader_size
,
128 of_packedsize
, firmware_size
, &totalsize
, errstr
, sizeof(errstr
));
130 fprintf(stderr
, "[INFO] Total size of new image: %8d bytes\n", totalsize
);
133 fprintf(stderr
, "%s", errstr
);
140 patch_firmware(sum
.model
, fw_revisions
[sum
.model
], firmware_size
, buf
, len
,
141 of_packed
, of_packedsize
, rb_packed
, rb_packedsize
);
143 /* Write the new firmware */
144 fdout
= open(outfile
, O_CREAT
|O_TRUNC
|O_WRONLY
|O_BINARY
, 0666);
147 fprintf(stderr
, "[ERR] Could not open %s for writing\n", outfile
);
154 n
= write(fdout
, buf
, len
);
156 if (n
!= (unsigned)len
) {
157 fprintf(stderr
, "[ERR] Could not write firmware file\n");
168 fprintf(stderr
, "\n[INFO] Patching succeeded!\n");