The threading model should be set from configure, not config.h.
[maemo-rb.git] / rbutil / mkamsboot / main.c
blobcf40811ace29624d592cd9604ecfe0326b50613d
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * mkamsboot - a tool for merging bootloader code into an Sansa V2
11 * (AMS) firmware file
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 ****************************************************************************/
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>
34 #include <ucl/ucl.h>
36 #include "mkamsboot.h"
38 /* Header for ARM code binaries */
39 #include "dualboot.h"
41 /* Win32 compatibility */
42 #ifndef O_BINARY
43 #define O_BINARY 0
44 #endif
46 /* standalone executable */
47 int main(int argc, char* argv[])
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;
60 int patchable;
61 int totalsize;
62 int model;
63 char errstr[200];
64 struct md5sums sum;
65 char md5sum[33]; /* 32 digits + \0 */
67 sum.md5 = md5sum;
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");
76 if(argc != 4) {
77 printf("Usage: mkamsboot <firmware file> <boot file> <output file>\n");
78 return 1;
81 infile = argv[1];
82 bootfile = argv[2];
83 outfile = argv[3];
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);
91 return 1;
94 /* Load original firmware file */
95 buf = load_of_file(infile, model, &len, &sum,
96 &firmware_size, &of_packed, &of_packedsize, errstr, sizeof(errstr));
98 if (buf == NULL) {
99 free(rb_packed);
100 fprintf(stderr, "%s", errstr);
101 fprintf(stderr, "[ERR] Could not load %s\n", infile);
102 return 1;
105 fprintf(stderr, "[INFO] Original firmware MD5 checksum match\n");
106 fprintf(stderr, "[INFO] Model: Sansa %s v%d - Firmware version: %s\n",
107 ams_identity[sum.model].model_name,
108 ams_identity[sum.model].hw_revision, sum.version);
111 printf("[INFO] Firmware patching has begun !\n\n");
113 fprintf(stderr, "[INFO] Original firmware size: %8d bytes\n",
114 firmware_size);
115 fprintf(stderr, "[INFO] Packed OF size: %8d bytes\n",
116 of_packedsize);
117 fprintf(stderr, "[INFO] Bootloader size: %8d bytes\n",
118 bootloader_size);
119 fprintf(stderr, "[INFO] Packed bootloader size: %8d bytes\n",
120 rb_packedsize);
121 fprintf(stderr, "[INFO] Dual-boot function size: %8d bytes\n",
122 ams_identity[sum.model].bootloader_size);
123 fprintf(stderr, "[INFO] UCL unpack function size: %8zu bytes\n",
124 sizeof(nrv2e_d8));
125 fprintf(stderr, "[INFO] Original firmware version: %8u bytes\n",
126 0x200);
128 patchable = check_sizes(sum.model, rb_packedsize, bootloader_size,
129 of_packedsize, firmware_size, &totalsize, errstr, sizeof(errstr));
131 fprintf(stderr, "[INFO] Total size of new image: %8d bytes\n", totalsize);
133 if (!patchable) {
134 fprintf(stderr, "%s", errstr);
135 free(buf);
136 free(of_packed);
137 free(rb_packed);
138 return 1;
141 patch_firmware(sum.model, ams_identity[sum.model].fw_revision,
142 firmware_size, buf, len, of_packed, of_packedsize, rb_packed,
143 rb_packedsize);
145 /* Write the new firmware */
146 fdout = open(outfile, O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, 0666);
148 if (fdout < 0) {
149 fprintf(stderr, "[ERR] Could not open %s for writing\n", outfile);
150 free(buf);
151 free(of_packed);
152 free(rb_packed);
153 return 1;
156 n = write(fdout, buf, len);
158 if (n != (unsigned)len) {
159 fprintf(stderr, "[ERR] Could not write firmware file\n");
160 free(buf);
161 free(of_packed);
162 free(rb_packed);
163 return 1;
166 close(fdout);
167 free(buf);
168 free(of_packed);
169 free(rb_packed);
170 fprintf(stderr, "\n[INFO] Patching succeeded!\n");
172 return 0;