diff options
Diffstat (limited to 'rbutil/mkamsboot/main.c')
-rw-r--r-- | rbutil/mkamsboot/main.c | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/rbutil/mkamsboot/main.c b/rbutil/mkamsboot/main.c new file mode 100644 index 0000000000..5715248685 --- /dev/null +++ b/rbutil/mkamsboot/main.c | |||
@@ -0,0 +1,168 @@ | |||
1 | /*************************************************************************** | ||
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 */ | ||
47 | int 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; | ||
60 | int totalsize; | ||
61 | char errstr[200]; | ||
62 | struct md5sums sum; | ||
63 | char md5sum[33]; /* 32 digits + \0 */ | ||
64 | |||
65 | sum.md5 = md5sum; | ||
66 | |||
67 | /* VERSION comes frome the Makefile */ | ||
68 | fprintf(stderr, | ||
69 | "mkamsboot Version " VERSION "\n" | ||
70 | "This is free software; see the source for copying conditions. There is NO\n" | ||
71 | "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" | ||
72 | "\n"); | ||
73 | |||
74 | if(argc != 4) { | ||
75 | printf("Usage: mkamsboot <firmware file> <boot file> <output file>\n"); | ||
76 | return 1; | ||
77 | } | ||
78 | |||
79 | infile = argv[1]; | ||
80 | bootfile = argv[2]; | ||
81 | outfile = argv[3]; | ||
82 | |||
83 | /* Load original firmware file */ | ||
84 | buf = load_of_file(infile, &len, &sum, | ||
85 | &firmware_size, &of_packed, &of_packedsize, errstr, sizeof(errstr)); | ||
86 | |||
87 | if (buf == NULL) { | ||
88 | fprintf(stderr, "%s", errstr); | ||
89 | fprintf(stderr, "[ERR] Could not load %s\n", infile); | ||
90 | return 1; | ||
91 | } | ||
92 | |||
93 | fprintf(stderr, "[INFO] Original firmware MD5 checksum match\n"); | ||
94 | fprintf(stderr, "[INFO] Model: Sansa %s v%d - Firmware version: %s\n", | ||
95 | model_names[sum.model], hw_revisions[sum.model], sum.version); | ||
96 | |||
97 | |||
98 | /* Load bootloader file */ | ||
99 | rb_packed = load_rockbox_file(bootfile, sum.model, &bootloader_size, | ||
100 | &rb_packedsize, errstr, sizeof(errstr)); | ||
101 | if (rb_packed == NULL) { | ||
102 | fprintf(stderr, "%s", errstr); | ||
103 | fprintf(stderr, "[ERR] Could not load %s\n", bootfile); | ||
104 | free(buf); | ||
105 | free(of_packed); | ||
106 | return 1; | ||
107 | } | ||
108 | |||
109 | printf("[INFO] Firmware patching has begun !\n\n"); | ||
110 | |||
111 | fprintf(stderr, "[INFO] Original firmware size: %d bytes\n", | ||
112 | firmware_size); | ||
113 | fprintf(stderr, "[INFO] Packed OF size: %d bytes\n", | ||
114 | of_packedsize); | ||
115 | fprintf(stderr, "[INFO] Bootloader size: %d bytes\n", | ||
116 | (int)bootloader_size); | ||
117 | fprintf(stderr, "[INFO] Packed bootloader size: %d bytes\n", | ||
118 | rb_packedsize); | ||
119 | fprintf(stderr, "[INFO] Dual-boot function size: %d bytes\n", | ||
120 | bootloader_sizes[sum.model]); | ||
121 | fprintf(stderr, "[INFO] UCL unpack function size: %u bytes\n", | ||
122 | (unsigned int)sizeof(nrv2e_d8)); | ||
123 | |||
124 | totalsize = total_size(sum.model, of_packedsize, rb_packedsize); | ||
125 | |||
126 | fprintf(stderr, "[INFO] Total size of new image: %d bytes\n", totalsize); | ||
127 | |||
128 | if (totalsize > firmware_size) { | ||
129 | fprintf(stderr, "[ERR] No room to insert bootloader, aborting\n"); | ||
130 | free(buf); | ||
131 | free(of_packed); | ||
132 | free(rb_packed); | ||
133 | return 1; | ||
134 | } | ||
135 | |||
136 | patch_firmware(sum.model, fw_revisions[sum.model], firmware_size, buf, len, | ||
137 | of_packed, of_packedsize, rb_packed, rb_packedsize); | ||
138 | |||
139 | /* Write the new firmware */ | ||
140 | fdout = open(outfile, O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, 0666); | ||
141 | |||
142 | if (fdout < 0) { | ||
143 | fprintf(stderr, "[ERR] Could not open %s for writing\n", outfile); | ||
144 | free(buf); | ||
145 | free(of_packed); | ||
146 | free(rb_packed); | ||
147 | return 1; | ||
148 | } | ||
149 | |||
150 | n = write(fdout, buf, len); | ||
151 | |||
152 | if (n != (unsigned)len) { | ||
153 | fprintf(stderr, "[ERR] Could not write firmware file\n"); | ||
154 | free(buf); | ||
155 | free(of_packed); | ||
156 | free(rb_packed); | ||
157 | return 1; | ||
158 | } | ||
159 | |||
160 | close(fdout); | ||
161 | free(buf); | ||
162 | free(of_packed); | ||
163 | free(rb_packed); | ||
164 | fprintf(stderr, "\n[INFO] Patching succeeded!\n"); | ||
165 | |||
166 | return 0; | ||
167 | } | ||
168 | |||