• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* 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 
load_file(const char * fn,unsigned * _sz)28 static 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 
52 oops:
53     close(fd);
54     if(data != 0) free(data);
55     return 0;
56 }
57 
usage(void)58 int 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             "       -o|--output <filename>\n"
68             );
69     return 1;
70 }
71 
72 
73 
74 static unsigned char padding[2048] = { 0, };
75 
write_padding(int fd,unsigned pagesize,unsigned itemsize)76 int write_padding(int fd, unsigned pagesize, unsigned itemsize)
77 {
78     unsigned pagemask = pagesize - 1;
79     unsigned count;
80 
81     if((itemsize & pagemask) == 0) {
82         return 0;
83     }
84 
85     count = pagesize - (itemsize & pagemask);
86 
87     if(write(fd, padding, count) != count) {
88         return -1;
89     } else {
90         return 0;
91     }
92 }
93 
main(int argc,char ** argv)94 int main(int argc, char **argv)
95 {
96     boot_img_hdr hdr;
97 
98     char *kernel_fn = 0;
99     void *kernel_data = 0;
100     char *ramdisk_fn = 0;
101     void *ramdisk_data = 0;
102     char *second_fn = 0;
103     void *second_data = 0;
104     char *cmdline = "";
105     char *bootimg = 0;
106     char *board = "";
107     unsigned pagesize = 2048;
108     int fd;
109     SHA_CTX ctx;
110     uint8_t* sha;
111 
112     argc--;
113     argv++;
114 
115     memset(&hdr, 0, sizeof(hdr));
116 
117         /* default load addresses */
118     hdr.kernel_addr =  0x10008000;
119     hdr.ramdisk_addr = 0x11000000;
120     hdr.second_addr =  0x10F00000;
121     hdr.tags_addr =    0x10000100;
122 
123     hdr.page_size = pagesize;
124 
125     while(argc > 0){
126         char *arg = argv[0];
127         char *val = argv[1];
128         if(argc < 2) {
129             return usage();
130         }
131         argc -= 2;
132         argv += 2;
133         if(!strcmp(arg, "--output") || !strcmp(arg, "-o")) {
134             bootimg = val;
135         } else if(!strcmp(arg, "--kernel")) {
136             kernel_fn = val;
137         } else if(!strcmp(arg, "--ramdisk")) {
138             ramdisk_fn = val;
139         } else if(!strcmp(arg, "--second")) {
140             second_fn = val;
141         } else if(!strcmp(arg, "--cmdline")) {
142             cmdline = val;
143         } else if(!strcmp(arg, "--base")) {
144             unsigned base = strtoul(val, 0, 16);
145             hdr.kernel_addr =  base + 0x00008000;
146             hdr.ramdisk_addr = base + 0x01000000;
147             hdr.second_addr =  base + 0x00F00000;
148             hdr.tags_addr =    base + 0x00000100;
149         } else if(!strcmp(arg, "--board")) {
150             board = val;
151         } else {
152             return usage();
153         }
154     }
155 
156     if(bootimg == 0) {
157         fprintf(stderr,"error: no output filename specified\n");
158         return usage();
159     }
160 
161     if(kernel_fn == 0) {
162         fprintf(stderr,"error: no kernel image specified\n");
163         return usage();
164     }
165 
166     if(ramdisk_fn == 0) {
167         fprintf(stderr,"error: no ramdisk image specified\n");
168         return usage();
169     }
170 
171     if(strlen(board) >= BOOT_NAME_SIZE) {
172         fprintf(stderr,"error: board name too large\n");
173         return usage();
174     }
175 
176     strcpy(hdr.name, board);
177 
178     memcpy(hdr.magic, BOOT_MAGIC, BOOT_MAGIC_SIZE);
179 
180     if(strlen(cmdline) > (BOOT_ARGS_SIZE - 1)) {
181         fprintf(stderr,"error: kernel commandline too large\n");
182         return 1;
183     }
184     strcpy((char*)hdr.cmdline, cmdline);
185 
186     kernel_data = load_file(kernel_fn, &hdr.kernel_size);
187     if(kernel_data == 0) {
188         fprintf(stderr,"error: could not load kernel '%s'\n", kernel_fn);
189         return 1;
190     }
191 
192     if(!strcmp(ramdisk_fn,"NONE")) {
193         ramdisk_data = 0;
194         hdr.ramdisk_size = 0;
195     } else {
196         ramdisk_data = load_file(ramdisk_fn, &hdr.ramdisk_size);
197         if(ramdisk_data == 0) {
198             fprintf(stderr,"error: could not load ramdisk '%s'\n", ramdisk_fn);
199             return 1;
200         }
201     }
202 
203     if(second_fn) {
204         second_data = load_file(second_fn, &hdr.second_size);
205         if(second_data == 0) {
206             fprintf(stderr,"error: could not load secondstage '%s'\n", second_fn);
207             return 1;
208         }
209     }
210 
211     /* put a hash of the contents in the header so boot images can be
212      * differentiated based on their first 2k.
213      */
214     SHA_init(&ctx);
215     SHA_update(&ctx, kernel_data, hdr.kernel_size);
216     SHA_update(&ctx, &hdr.kernel_size, sizeof(hdr.kernel_size));
217     SHA_update(&ctx, ramdisk_data, hdr.ramdisk_size);
218     SHA_update(&ctx, &hdr.ramdisk_size, sizeof(hdr.ramdisk_size));
219     SHA_update(&ctx, second_data, hdr.second_size);
220     SHA_update(&ctx, &hdr.second_size, sizeof(hdr.second_size));
221     sha = SHA_final(&ctx);
222     memcpy(hdr.id, sha,
223            SHA_DIGEST_SIZE > sizeof(hdr.id) ? sizeof(hdr.id) : SHA_DIGEST_SIZE);
224 
225     fd = open(bootimg, O_CREAT | O_TRUNC | O_WRONLY, 0644);
226     if(fd < 0) {
227         fprintf(stderr,"error: could not create '%s'\n", bootimg);
228         return 1;
229     }
230 
231     if(write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) goto fail;
232     if(write_padding(fd, pagesize, sizeof(hdr))) goto fail;
233 
234     if(write(fd, kernel_data, hdr.kernel_size) != hdr.kernel_size) goto fail;
235     if(write_padding(fd, pagesize, hdr.kernel_size)) goto fail;
236 
237     if(write(fd, ramdisk_data, hdr.ramdisk_size) != hdr.ramdisk_size) goto fail;
238     if(write_padding(fd, pagesize, hdr.ramdisk_size)) goto fail;
239 
240     if(second_data) {
241         if(write(fd, second_data, hdr.second_size) != hdr.second_size) goto fail;
242         if(write_padding(fd, pagesize, hdr.ramdisk_size)) goto fail;
243     }
244 
245     return 0;
246 
247 fail:
248     unlink(bootimg);
249     close(fd);
250     fprintf(stderr,"error: failed writing '%s': %s\n", bootimg,
251             strerror(errno));
252     return 1;
253 }
254