blob: 06fc95be80622b5ddead166d3d0fea09233f1ac2 [file] [log] [blame]
Ketut Putu Kumajaya63846372014-06-23 20:05:33 +07001/* tools/mkbootimg/mkbootimg.c
2**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <unistd.h>
22#include <fcntl.h>
23#include <errno.h>
24
25#include "mincrypt/sha.h"
26#include "bootimg.h"
27
28static void *load_file(const char *fn, unsigned *_sz)
29{
30 char *data;
31 int sz;
32 int fd;
33
34 data = 0;
35 fd = open(fn, O_RDONLY);
36 if(fd < 0) return 0;
37
38 sz = lseek(fd, 0, SEEK_END);
39 if(sz < 0) goto oops;
40
41 if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
42
43 data = (char*) malloc(sz);
44 if(data == 0) goto oops;
45
46 if(read(fd, data, sz) != sz) goto oops;
47 close(fd);
48
49 if(_sz) *_sz = sz;
50 return data;
51
52oops:
53 close(fd);
54 if(data != 0) free(data);
55 return 0;
56}
57
58int usage(void)
59{
60 fprintf(stderr,"usage: mkbootimg\n"
61 " --kernel <filename>\n"
62 " --ramdisk <filename>\n"
63 " [ --second <2ndbootloader-filename> ]\n"
64 " [ --cmdline <kernel-commandline> ]\n"
65 " [ --board <boardname> ]\n"
66 " [ --base <address> ]\n"
67 " [ --pagesize <pagesize> ]\n"
68 " [ --ramdisk_offset <address> ]\n"
69 " [ --dt <filename> ]\n"
70 " -o|--output <filename>\n"
71 );
72 return 1;
73}
74
75
76
77static unsigned char padding[131072] = { 0, };
78
79int write_padding(int fd, unsigned pagesize, unsigned itemsize)
80{
81 unsigned pagemask = pagesize - 1;
82 unsigned count;
83
84 if((itemsize & pagemask) == 0) {
85 return 0;
86 }
87
88 count = pagesize - (itemsize & pagemask);
89
90 if(write(fd, padding, count) != count) {
91 return -1;
92 } else {
93 return 0;
94 }
95}
96
97int main(int argc, char **argv)
98{
99 boot_img_hdr hdr;
100
101 char *kernel_fn = 0;
102 void *kernel_data = 0;
103 char *ramdisk_fn = 0;
104 void *ramdisk_data = 0;
105 char *second_fn = 0;
106 void *second_data = 0;
107 char *cmdline = "";
108 char *bootimg = 0;
109 char *board = "";
110 char *dt_fn = 0;
111 void *dt_data = 0;
112 unsigned pagesize = 2048;
113 int fd;
114 SHA_CTX ctx;
115 uint8_t* sha;
116 unsigned base = 0x10000000;
117 unsigned kernel_offset = 0x00008000;
118 unsigned ramdisk_offset = 0x01000000;
119 unsigned second_offset = 0x00f00000;
120 unsigned tags_offset = 0x00000100;
121
122 argc--;
123 argv++;
124
125 memset(&hdr, 0, sizeof(hdr));
126
127 while(argc > 0){
128 char *arg = argv[0];
129 char *val = argv[1];
130 if(argc < 2) {
131 return usage();
132 }
133 argc -= 2;
134 argv += 2;
135 if(!strcmp(arg, "--output") || !strcmp(arg, "-o")) {
136 bootimg = val;
137 } else if(!strcmp(arg, "--kernel")) {
138 kernel_fn = val;
139 } else if(!strcmp(arg, "--ramdisk")) {
140 ramdisk_fn = val;
141 } else if(!strcmp(arg, "--second")) {
142 second_fn = val;
143 } else if(!strcmp(arg, "--cmdline")) {
144 cmdline = val;
145 } else if(!strcmp(arg, "--base")) {
146 base = strtoul(val, 0, 16);
147 } else if(!strcmp(arg, "--kernel_offset")) {
148 kernel_offset = strtoul(val, 0, 16);
149 } else if(!strcmp(arg, "--ramdisk_offset")) {
150 ramdisk_offset = strtoul(val, 0, 16);
151 } else if(!strcmp(arg, "--second_offset")) {
152 second_offset = strtoul(val, 0, 16);
153 } else if(!strcmp(arg, "--tags_offset")) {
154 tags_offset = strtoul(val, 0, 16);
155 } else if(!strcmp(arg, "--board")) {
156 board = val;
157 } else if(!strcmp(arg,"--pagesize")) {
158 pagesize = strtoul(val, 0, 10);
159 if ((pagesize != 2048) && (pagesize != 4096) && (pagesize != 8192) && (pagesize != 16384) && (pagesize != 32768) && (pagesize != 65536) && (pagesize != 131072)) {
160 fprintf(stderr,"error: unsupported page size %d\n", pagesize);
161 return -1;
162 }
163 } else if(!strcmp(arg, "--dt")) {
164 dt_fn = val;
165 } else {
166 return usage();
167 }
168 }
169 hdr.page_size = pagesize;
170
171 hdr.kernel_addr = base + kernel_offset;
172 hdr.ramdisk_addr = base + ramdisk_offset;
173 hdr.second_addr = base + second_offset;
174 hdr.tags_addr = base + tags_offset;
175
176 if(bootimg == 0) {
177 fprintf(stderr,"error: no output filename specified\n");
178 return usage();
179 }
180
181 if(kernel_fn == 0) {
182 fprintf(stderr,"error: no kernel image specified\n");
183 return usage();
184 }
185
186 if(ramdisk_fn == 0) {
187 fprintf(stderr,"error: no ramdisk image specified\n");
188 return usage();
189 }
190
191 if(strlen(board) >= BOOT_NAME_SIZE) {
192 fprintf(stderr,"error: board name too large\n");
193 return usage();
194 }
195
196 strcpy(hdr.name, board);
197
198 memcpy(hdr.magic, BOOT_MAGIC, BOOT_MAGIC_SIZE);
199
200 if(strlen(cmdline) > (BOOT_ARGS_SIZE - 1)) {
201 fprintf(stderr,"error: kernel commandline too large\n");
202 return 1;
203 }
204 strcpy((char*)hdr.cmdline, cmdline);
205
206 kernel_data = load_file(kernel_fn, &hdr.kernel_size);
207 if(kernel_data == 0) {
208 fprintf(stderr,"error: could not load kernel '%s'\n", kernel_fn);
209 return 1;
210 }
211
212 if(!strcmp(ramdisk_fn,"NONE")) {
213 ramdisk_data = 0;
214 hdr.ramdisk_size = 0;
215 } else {
216 ramdisk_data = load_file(ramdisk_fn, &hdr.ramdisk_size);
217 if(ramdisk_data == 0) {
218 fprintf(stderr,"error: could not load ramdisk '%s'\n", ramdisk_fn);
219 return 1;
220 }
221 }
222
223 if(second_fn) {
224 second_data = load_file(second_fn, &hdr.second_size);
225 if(second_data == 0) {
226 fprintf(stderr,"error: could not load secondstage '%s'\n", second_fn);
227 return 1;
228 }
229 }
230
231 if(dt_fn) {
232 dt_data = load_file(dt_fn, &hdr.dt_size);
233 if (dt_data == 0) {
234 fprintf(stderr,"error: could not load device tree image '%s'\n", dt_fn);
235 return 1;
236 }
237 }
238
239 /* put a hash of the contents in the header so boot images can be
240 * differentiated based on their first 2k.
241 */
242 SHA_init(&ctx);
243 SHA_update(&ctx, kernel_data, hdr.kernel_size);
244 SHA_update(&ctx, &hdr.kernel_size, sizeof(hdr.kernel_size));
245 SHA_update(&ctx, ramdisk_data, hdr.ramdisk_size);
246 SHA_update(&ctx, &hdr.ramdisk_size, sizeof(hdr.ramdisk_size));
247 SHA_update(&ctx, second_data, hdr.second_size);
248 SHA_update(&ctx, &hdr.second_size, sizeof(hdr.second_size));
249 if(dt_data) {
250 SHA_update(&ctx, dt_data, hdr.dt_size);
251 SHA_update(&ctx, &hdr.dt_size, sizeof(hdr.dt_size));
252 }
253 sha = SHA_final(&ctx);
254 memcpy(hdr.id, sha,
255 SHA_DIGEST_SIZE > sizeof(hdr.id) ? sizeof(hdr.id) : SHA_DIGEST_SIZE);
256
257 fd = open(bootimg, O_CREAT | O_TRUNC | O_WRONLY, 0644);
258 if(fd < 0) {
259 fprintf(stderr,"error: could not create '%s'\n", bootimg);
260 return 1;
261 }
262
263 if(write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) goto fail;
264 if(write_padding(fd, pagesize, sizeof(hdr))) goto fail;
265
266 if(write(fd, kernel_data, hdr.kernel_size) != hdr.kernel_size) goto fail;
267 if(write_padding(fd, pagesize, hdr.kernel_size)) goto fail;
268
269 if(write(fd, ramdisk_data, hdr.ramdisk_size) != hdr.ramdisk_size) goto fail;
270 if(write_padding(fd, pagesize, hdr.ramdisk_size)) goto fail;
271
272 if(second_data) {
273 if(write(fd, second_data, hdr.second_size) != hdr.second_size) goto fail;
274 if(write_padding(fd, pagesize, hdr.ramdisk_size)) goto fail;
275 }
276
277 if(dt_data) {
278 if(write(fd, dt_data, hdr.dt_size) != hdr.dt_size) goto fail;
279 if(write_padding(fd, pagesize, hdr.dt_size)) goto fail;
280 }
281 return 0;
282
283fail:
284 unlink(bootimg);
285 close(fd);
286 fprintf(stderr,"error: failed writing '%s': %s\n", bootimg,
287 strerror(errno));
288 return 1;
289}