blob: fa650cfca7da5b2162c02c5649b9caf1ce4385f2 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -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#include <time.h>
25
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 " -o|--output <filename>\n"
67 );
68 return 1;
69}
70
71
72
73static unsigned char padding[2048] = { 0, };
74
75int write_padding(int fd, unsigned pagesize, unsigned itemsize)
76{
77 unsigned pagemask = pagesize - 1;
78 unsigned count;
79
80 if((itemsize & pagemask) == 0) {
81 return 0;
82 }
83
84 count = pagesize - (itemsize & pagemask);
85
86 if(write(fd, padding, count) != count) {
87 return -1;
88 } else {
89 return 0;
90 }
91}
92
93unsigned checksum(void *_ptr, unsigned len)
94{
95 unsigned chk = 0;
96 unsigned char *ptr = _ptr;
97 while (len > 0) {
98 chk += *ptr++;
99 len--;
100 }
101 return chk;
102}
103
104int main(int argc, char **argv)
105{
106 boot_img_hdr hdr;
107
108 char *kernel_fn = 0;
109 void *kernel_data = 0;
110 char *ramdisk_fn = 0;
111 void *ramdisk_data = 0;
112 char *second_fn = 0;
113 void *second_data = 0;
114 char *cmdline = "";
115 char *bootimg = 0;
116 char *board = "";
117 unsigned pagesize = 2048;
118 unsigned saddr = 0;
119 int fd;
120
121 argc--;
122 argv++;
123
124 memset(&hdr, 0, sizeof(hdr));
125
126 while(argc > 0){
127 char *arg = argv[0];
128 char *val = argv[1];
129 if(argc < 2) {
130 return usage();
131 }
132 argc -= 2;
133 argv += 2;
134 if(!strcmp(arg, "--output") || !strcmp(arg, "-o")) {
135 bootimg = val;
136 } else if(!strcmp(arg, "--kernel")) {
137 kernel_fn = val;
138 } else if(!strcmp(arg, "--ramdisk")) {
139 ramdisk_fn = val;
140 } else if(!strcmp(arg, "--second")) {
141 second_fn = val;
142 } else if(!strcmp(arg, "--cmdline")) {
143 cmdline = val;
144 } else if(!strcmp(arg, "--saddr")) {
145 saddr = strtoul(val, 0, 16);
146 } else if(!strcmp(arg, "--board")) {
147 board = val;
148 } else {
149 return usage();
150 }
151 }
152
153 if(bootimg == 0) {
154 fprintf(stderr,"error: no output filename specified\n");
155 return usage();
156 }
157
158 if(kernel_fn == 0) {
159 fprintf(stderr,"error: no kernel image specified\n");
160 return usage();
161 }
162
163 if(ramdisk_fn == 0) {
164 fprintf(stderr,"error: no ramdisk image specified\n");
165 return usage();
166 }
167
168 if(strlen(board) >= BOOT_NAME_SIZE) {
169 fprintf(stderr,"error: board name too large\n");
170 return usage();
171 }
172
173 strcpy(hdr.name, board);
174
175 hdr.kernel_addr = 0x10008000;
176 hdr.ramdisk_addr = 0x11000000;
177 if(saddr) {
178 hdr.second_addr = 0x00300000;
179 } else {
180 hdr.second_addr = 0x10F00000;
181 }
182 hdr.tags_addr = 0x10000100;
183 hdr.page_size = pagesize;
184
185 memcpy(hdr.magic, BOOT_MAGIC, BOOT_MAGIC_SIZE);
186
187 if(strlen(cmdline) > (BOOT_ARGS_SIZE - 1)) {
188 fprintf(stderr,"error: kernel commandline too large\n");
189 return 1;
190 }
191 strcpy((char*)hdr.cmdline, cmdline);
192
193 kernel_data = load_file(kernel_fn, &hdr.kernel_size);
194 if(kernel_data == 0) {
195 fprintf(stderr,"error: could not load kernel '%s'\n", kernel_fn);
196 return 1;
197 }
198
199 if(!strcmp(ramdisk_fn,"NONE")) {
200 ramdisk_data = 0;
201 hdr.ramdisk_size = 0;
202 } else {
203 ramdisk_data = load_file(ramdisk_fn, &hdr.ramdisk_size);
204 if(ramdisk_data == 0) {
205 fprintf(stderr,"error: could not load ramdisk '%s'\n", ramdisk_fn);
206 return 1;
207 }
208 }
209
210 if(second_fn) {
211 second_data = load_file(second_fn, &hdr.second_size);
212 if(second_data == 0) {
213 fprintf(stderr,"error: could not load secondstage '%s'\n", second_fn);
214 return 1;
215 }
216 }
217
218 /* put some stuff in the header to differentiate between
219 * different boot images. SHA1 would be nicer, but this
220 * isn't for crypto grade anything, just to have a quick
221 * way to compare boot.imgs based on their first 2k
222 */
223 hdr.id[0] = (unsigned) time(0);
224 hdr.id[1] = checksum(kernel_data, hdr.kernel_size);
225 hdr.id[2] = checksum(ramdisk_data, hdr.ramdisk_size);
226 hdr.id[3] = checksum(second_data, hdr.second_size);
227
228 fd = open(bootimg, O_CREAT | O_TRUNC | O_WRONLY, 0644);
229 if(fd < 0) {
230 fprintf(stderr,"error: could not create '%s'\n", bootimg);
231 return 1;
232 }
233
234 if(write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) goto fail;
235 if(write_padding(fd, pagesize, sizeof(hdr))) goto fail;
236
237 if(write(fd, kernel_data, hdr.kernel_size) != hdr.kernel_size) goto fail;
238 if(write_padding(fd, pagesize, hdr.kernel_size)) goto fail;
239
240 if(write(fd, ramdisk_data, hdr.ramdisk_size) != hdr.ramdisk_size) goto fail;
241 if(write_padding(fd, pagesize, hdr.ramdisk_size)) goto fail;
242
243 if(second_data) {
244 if(write(fd, second_data, hdr.second_size) != hdr.second_size) goto fail;
245 if(write_padding(fd, pagesize, hdr.ramdisk_size)) goto fail;
246 }
247
248 return 0;
249
250fail:
251 unlink(bootimg);
252 close(fd);
253 fprintf(stderr,"error: failed writing '%s': %s\n", bootimg,
254 strerror(errno));
255 return 1;
256}
257