blob: d598f03952c098f0b2df1327d8b8e21a731c171b [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/* 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"
Brian Swetland2a63bb72009-04-28 16:05:07 -070066 " [ --base <address> ]\n"
Ying Wangfc2f7cb2010-08-23 12:59:23 -070067 " [ --pagesize <pagesize> ]\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068 " -o|--output <filename>\n"
69 );
70 return 1;
71}
72
73
74
Ithamar R. Adema81ce3a72013-03-10 14:11:10 +010075static unsigned char padding[16384] = { 0, };
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076
77int write_padding(int fd, unsigned pagesize, unsigned itemsize)
78{
79 unsigned pagemask = pagesize - 1;
80 unsigned count;
81
82 if((itemsize & pagemask) == 0) {
83 return 0;
84 }
85
86 count = pagesize - (itemsize & pagemask);
87
88 if(write(fd, padding, count) != count) {
89 return -1;
90 } else {
91 return 0;
92 }
93}
94
95int main(int argc, char **argv)
96{
97 boot_img_hdr hdr;
98
99 char *kernel_fn = 0;
100 void *kernel_data = 0;
101 char *ramdisk_fn = 0;
102 void *ramdisk_data = 0;
103 char *second_fn = 0;
104 void *second_data = 0;
105 char *cmdline = "";
106 char *bootimg = 0;
107 char *board = "";
108 unsigned pagesize = 2048;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109 int fd;
110 SHA_CTX ctx;
111 uint8_t* sha;
Doug Zongkerdd7fafb2012-08-02 14:50:24 -0700112 unsigned base = 0x10000000;
113 unsigned kernel_offset = 0x00008000;
114 unsigned ramdisk_offset = 0x01000000;
115 unsigned second_offset = 0x00f00000;
116 unsigned tags_offset = 0x00000100;
Andrew Boied35ce352013-08-21 15:48:40 -0700117 size_t cmdlen;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118
119 argc--;
120 argv++;
121
122 memset(&hdr, 0, sizeof(hdr));
123
124 while(argc > 0){
125 char *arg = argv[0];
126 char *val = argv[1];
127 if(argc < 2) {
128 return usage();
129 }
130 argc -= 2;
131 argv += 2;
132 if(!strcmp(arg, "--output") || !strcmp(arg, "-o")) {
133 bootimg = val;
134 } else if(!strcmp(arg, "--kernel")) {
135 kernel_fn = val;
136 } else if(!strcmp(arg, "--ramdisk")) {
137 ramdisk_fn = val;
138 } else if(!strcmp(arg, "--second")) {
139 second_fn = val;
140 } else if(!strcmp(arg, "--cmdline")) {
141 cmdline = val;
Brian Swetland2a63bb72009-04-28 16:05:07 -0700142 } else if(!strcmp(arg, "--base")) {
Doug Zongkerdd7fafb2012-08-02 14:50:24 -0700143 base = strtoul(val, 0, 16);
144 } else if(!strcmp(arg, "--kernel_offset")) {
145 kernel_offset = strtoul(val, 0, 16);
146 } else if(!strcmp(arg, "--ramdisk_offset")) {
147 ramdisk_offset = strtoul(val, 0, 16);
148 } else if(!strcmp(arg, "--second_offset")) {
149 second_offset = strtoul(val, 0, 16);
150 } else if(!strcmp(arg, "--tags_offset")) {
151 tags_offset = strtoul(val, 0, 16);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152 } else if(!strcmp(arg, "--board")) {
153 board = val;
Brian Swetland8417d692010-08-11 16:56:49 -0700154 } else if(!strcmp(arg,"--pagesize")) {
155 pagesize = strtoul(val, 0, 10);
Ithamar R. Adema81ce3a72013-03-10 14:11:10 +0100156 if ((pagesize != 2048) && (pagesize != 4096)
157 && (pagesize != 8192) && (pagesize != 16384)) {
Brian Swetland8417d692010-08-11 16:56:49 -0700158 fprintf(stderr,"error: unsupported page size %d\n", pagesize);
159 return -1;
160 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161 } else {
162 return usage();
163 }
164 }
Maarten Hooft65e461c2010-08-24 15:24:50 -0700165 hdr.page_size = pagesize;
166
Doug Zongkerdd7fafb2012-08-02 14:50:24 -0700167 hdr.kernel_addr = base + kernel_offset;
168 hdr.ramdisk_addr = base + ramdisk_offset;
169 hdr.second_addr = base + second_offset;
170 hdr.tags_addr = base + tags_offset;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171
172 if(bootimg == 0) {
173 fprintf(stderr,"error: no output filename specified\n");
174 return usage();
175 }
176
177 if(kernel_fn == 0) {
178 fprintf(stderr,"error: no kernel image specified\n");
179 return usage();
180 }
181
182 if(ramdisk_fn == 0) {
183 fprintf(stderr,"error: no ramdisk image specified\n");
184 return usage();
185 }
186
187 if(strlen(board) >= BOOT_NAME_SIZE) {
188 fprintf(stderr,"error: board name too large\n");
189 return usage();
190 }
191
192 strcpy(hdr.name, board);
193
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194 memcpy(hdr.magic, BOOT_MAGIC, BOOT_MAGIC_SIZE);
195
Andrew Boied35ce352013-08-21 15:48:40 -0700196 cmdlen = strlen(cmdline);
197 if(cmdlen > (BOOT_ARGS_SIZE + BOOT_EXTRA_ARGS_SIZE - 2)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198 fprintf(stderr,"error: kernel commandline too large\n");
199 return 1;
200 }
Andrew Boied35ce352013-08-21 15:48:40 -0700201 /* Even if we need to use the supplemental field, ensure we
202 * are still NULL-terminated */
203 strncpy((char *)hdr.cmdline, cmdline, BOOT_ARGS_SIZE - 1);
204 hdr.cmdline[BOOT_ARGS_SIZE - 1] = '\0';
205 if (cmdlen >= (BOOT_ARGS_SIZE - 1)) {
206 cmdline += (BOOT_ARGS_SIZE - 1);
207 strncpy((char *)hdr.extra_cmdline, cmdline, BOOT_EXTRA_ARGS_SIZE);
208 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209
210 kernel_data = load_file(kernel_fn, &hdr.kernel_size);
211 if(kernel_data == 0) {
212 fprintf(stderr,"error: could not load kernel '%s'\n", kernel_fn);
213 return 1;
214 }
215
216 if(!strcmp(ramdisk_fn,"NONE")) {
217 ramdisk_data = 0;
218 hdr.ramdisk_size = 0;
219 } else {
220 ramdisk_data = load_file(ramdisk_fn, &hdr.ramdisk_size);
221 if(ramdisk_data == 0) {
222 fprintf(stderr,"error: could not load ramdisk '%s'\n", ramdisk_fn);
223 return 1;
224 }
225 }
226
227 if(second_fn) {
228 second_data = load_file(second_fn, &hdr.second_size);
229 if(second_data == 0) {
230 fprintf(stderr,"error: could not load secondstage '%s'\n", second_fn);
231 return 1;
232 }
233 }
234
235 /* put a hash of the contents in the header so boot images can be
236 * differentiated based on their first 2k.
237 */
238 SHA_init(&ctx);
239 SHA_update(&ctx, kernel_data, hdr.kernel_size);
240 SHA_update(&ctx, &hdr.kernel_size, sizeof(hdr.kernel_size));
241 SHA_update(&ctx, ramdisk_data, hdr.ramdisk_size);
242 SHA_update(&ctx, &hdr.ramdisk_size, sizeof(hdr.ramdisk_size));
243 SHA_update(&ctx, second_data, hdr.second_size);
244 SHA_update(&ctx, &hdr.second_size, sizeof(hdr.second_size));
245 sha = SHA_final(&ctx);
246 memcpy(hdr.id, sha,
247 SHA_DIGEST_SIZE > sizeof(hdr.id) ? sizeof(hdr.id) : SHA_DIGEST_SIZE);
248
249 fd = open(bootimg, O_CREAT | O_TRUNC | O_WRONLY, 0644);
250 if(fd < 0) {
251 fprintf(stderr,"error: could not create '%s'\n", bootimg);
252 return 1;
253 }
254
255 if(write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) goto fail;
256 if(write_padding(fd, pagesize, sizeof(hdr))) goto fail;
257
258 if(write(fd, kernel_data, hdr.kernel_size) != hdr.kernel_size) goto fail;
259 if(write_padding(fd, pagesize, hdr.kernel_size)) goto fail;
260
261 if(write(fd, ramdisk_data, hdr.ramdisk_size) != hdr.ramdisk_size) goto fail;
262 if(write_padding(fd, pagesize, hdr.ramdisk_size)) goto fail;
263
264 if(second_data) {
265 if(write(fd, second_data, hdr.second_size) != hdr.second_size) goto fail;
266 if(write_padding(fd, pagesize, hdr.ramdisk_size)) goto fail;
267 }
268
269 return 0;
270
271fail:
272 unlink(bootimg);
273 close(fd);
274 fprintf(stderr,"error: failed writing '%s': %s\n", bootimg,
275 strerror(errno));
276 return 1;
277}