blob: a864c2a5c1cba519c5102a79a2773bfbedc2318a [file] [log] [blame]
Dominik Riebelingea617802009-11-04 20:58:40 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * mkamsboot - a tool for merging bootloader code into an Sansa V2
11 * (AMS) firmware file
12 *
13 * Copyright (C) 2008 Dave Chapman
14 *
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.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 ****************************************************************************/
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <stdint.h>
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <fcntl.h>
31#include <unistd.h>
32#include <string.h>
33
34#include <ucl/ucl.h>
35
36#include "mkamsboot.h"
37
38/* Header for ARM code binaries */
39#include "dualboot.h"
40
41/* Win32 compatibility */
42#ifndef O_BINARY
43#define O_BINARY 0
44#endif
45
46/* standalone executable */
47int main(int argc, char* argv[])
48{
49 char *infile, *bootfile, *outfile;
50 int fdout;
51 off_t len;
52 uint32_t n;
53 unsigned char* buf;
54 int firmware_size;
55 int bootloader_size;
56 unsigned char* of_packed;
57 int of_packedsize;
58 unsigned char* rb_packed;
59 int rb_packedsize;
Rafaël Carréb6c20c12010-02-19 14:10:26 +000060 int patchable;
Dominik Riebelingea617802009-11-04 20:58:40 +000061 int totalsize;
Rafaël Carréff6b0422010-05-24 10:06:52 +000062 int model;
Dominik Riebelingea617802009-11-04 20:58:40 +000063 char errstr[200];
64 struct md5sums sum;
65 char md5sum[33]; /* 32 digits + \0 */
66
67 sum.md5 = md5sum;
68
69/* VERSION comes frome the Makefile */
70 fprintf(stderr,
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"
74"\n");
75
76 if(argc != 4) {
77 printf("Usage: mkamsboot <firmware file> <boot file> <output file>\n");
78 return 1;
79 }
80
81 infile = argv[1];
82 bootfile = argv[2];
83 outfile = argv[3];
84
Rafaël Carréff6b0422010-05-24 10:06:52 +000085 /* 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);
91 return 1;
92 }
93
Dominik Riebelingea617802009-11-04 20:58:40 +000094 /* Load original firmware file */
Rafaël Carréff6b0422010-05-24 10:06:52 +000095 buf = load_of_file(infile, model, &len, &sum,
Dominik Riebelingea617802009-11-04 20:58:40 +000096 &firmware_size, &of_packed, &of_packedsize, errstr, sizeof(errstr));
97
98 if (buf == NULL) {
Rafaël Carréff6b0422010-05-24 10:06:52 +000099 free(rb_packed);
Dominik Riebelingea617802009-11-04 20:58:40 +0000100 fprintf(stderr, "%s", errstr);
101 fprintf(stderr, "[ERR] Could not load %s\n", infile);
102 return 1;
103 }
104
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);
108
109
Dominik Riebelingea617802009-11-04 20:58:40 +0000110 printf("[INFO] Firmware patching has begun !\n\n");
111
112 fprintf(stderr, "[INFO] Original firmware size: %d bytes\n",
113 firmware_size);
114 fprintf(stderr, "[INFO] Packed OF size: %d bytes\n",
115 of_packedsize);
116 fprintf(stderr, "[INFO] Bootloader size: %d bytes\n",
117 (int)bootloader_size);
118 fprintf(stderr, "[INFO] Packed bootloader size: %d bytes\n",
119 rb_packedsize);
120 fprintf(stderr, "[INFO] Dual-boot function size: %d bytes\n",
121 bootloader_sizes[sum.model]);
122 fprintf(stderr, "[INFO] UCL unpack function size: %u bytes\n",
123 (unsigned int)sizeof(nrv2e_d8));
124
Rafaël Carréb6c20c12010-02-19 14:10:26 +0000125 patchable = check_sizes(sum.model, rb_packedsize, bootloader_size,
126 of_packedsize, firmware_size, &totalsize, errstr, sizeof(errstr));
Dominik Riebelingea617802009-11-04 20:58:40 +0000127
128 fprintf(stderr, "[INFO] Total size of new image: %d bytes\n", totalsize);
129
Rafaël Carréb6c20c12010-02-19 14:10:26 +0000130 if (!patchable) {
131 fprintf(stderr, "%s", errstr);
Dominik Riebelingea617802009-11-04 20:58:40 +0000132 free(buf);
133 free(of_packed);
134 free(rb_packed);
135 return 1;
136 }
137
138 patch_firmware(sum.model, fw_revisions[sum.model], firmware_size, buf, len,
139 of_packed, of_packedsize, rb_packed, rb_packedsize);
140
141 /* Write the new firmware */
142 fdout = open(outfile, O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, 0666);
143
144 if (fdout < 0) {
145 fprintf(stderr, "[ERR] Could not open %s for writing\n", outfile);
146 free(buf);
147 free(of_packed);
148 free(rb_packed);
149 return 1;
150 }
151
152 n = write(fdout, buf, len);
153
154 if (n != (unsigned)len) {
155 fprintf(stderr, "[ERR] Could not write firmware file\n");
156 free(buf);
157 free(of_packed);
158 free(rb_packed);
159 return 1;
160 }
161
162 close(fdout);
163 free(buf);
164 free(of_packed);
165 free(rb_packed);
166 fprintf(stderr, "\n[INFO] Patching succeeded!\n");
167
168 return 0;
169}
170