blob: fda8b67d4ddcb7a130f6796dac33625d987ff9eb [file] [log] [blame]
Kinson Chik6a7be432010-11-08 17:13:02 -08001/*
2 * Copyright (c) 2007, Google Inc.
3 * All rights reserved.
4 *
5 * Copyright (c) 2009-2010, Code Aurora Forum. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google, Inc. nor the names of its contributors
17 * may be used to endorse or promote products derived from this
18 * software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -080033
34#include <stdio.h>
35#include <stdlib.h>
36#include <unistd.h>
37#include <fcntl.h>
Kinson Chik6a7be432010-11-08 17:13:02 -080038#include <string.h>
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -080039
40#include <sys/stat.h>
41
Kinson Chik6a7be432010-11-08 17:13:02 -080042int print_usage(){
43 fprintf(stderr,"usage: mkheader <bin> <hdr> <none|unified-boot>\n");
44 fprintf(stderr," mkheader <bin> <hdr> <unsecure-boot> <outbin>\n");
45 fprintf(stderr," mkheader <bin> <hdr> <secure-boot> <outbin> <maxsize>\n");
46 fprintf(stderr," mkheader <bin> <hdr> <secure-boot> <outbin> <maxsize> <certchain> <files...>\n\n");
47 fprintf(stderr,"bin: Input raw appsbl binary\n");
48 fprintf(stderr,"hdr: Output of appsbl header location\n");
49 fprintf(stderr,"outbin: Output of the signed or unsigned apps boot location\n");
50 fprintf(stderr,"maxsize: Maximum size for certificate chain\n");
51 fprintf(stderr,"certchain: Output of the certchain location\n");
52 fprintf(stderr,"files: Input format <bin signature> <certifcate file(s) for certificate chain>...\n");
53 fprintf(stderr,"certificate chain: Files will be concatenated in order to create the certificate chain\n\n");
54 return -1;
55}
56
57int cat(FILE * in, FILE * out, unsigned size, unsigned buff_size){
58 unsigned bytes_left = size;
59 char buf[buff_size];
60 int ret = 0;
61
62 while(bytes_left){
63 fread(buf, sizeof(char), buff_size, in);
64 if(!feof(in)){
65 bytes_left -= fwrite(buf, sizeof(char), buff_size, out);
66 }else
67 bytes_left = 0;
68 }
69 ret = ferror(in) | ferror(out);
70 if(ret)
71 fprintf(stderr, "ERROR: Occured during file concatenation\n");
72 return ret;
73}
74
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -080075int main(int argc, char *argv[])
76{
77 struct stat s;
78 unsigned size, base;
79 int unified_boot = 0;
80 unsigned unified_boot_magic[20];
81 unsigned non_unified_boot_magic[10];
82 unsigned magic_len = 0;
83 unsigned *magic;
Kinson Chik6a7be432010-11-08 17:13:02 -080084 unsigned cert_chain_size = 0;
85 unsigned signature_size = 0;
86 int secure_boot = 0;
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -080087 int fd;
88
89 if(argc < 3) {
Kinson Chik6a7be432010-11-08 17:13:02 -080090 return print_usage();
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -080091 }
92
93 if (argc == 4) {
94 if(!strcmp("unified-boot",argv[3])) {
95 unified_boot = 1;
Kinson Chik6a7be432010-11-08 17:13:02 -080096 }else if(!strcmp("secure-boot",argv[3])){
97 fprintf(stderr,
98 "ERROR: Missing arguments: [outbin maxsize] | [outbin, maxsize, certchain, signature + certifcate(s)]\n");
99 return print_usage();
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -0800100 }
Kinson Chik6a7be432010-11-08 17:13:02 -0800101 else if(!strcmp("unsecure-boot",argv[3])){
102 fprintf(stderr,"ERROR: Missing arguments: outbin directory\n");
103 return print_usage();
104 }
105 }
106
107 if (argc > 4) {
108 if(!strcmp("secure-boot",argv[3])) {
109 if(argc < 9 && argc != 6){
110 fprintf(stderr,
111 "ERROR: Missing argument(s): [outbin maxsize] | [outbin, maxsize, certchain, signature + certifcate(s)]\n");
112 return print_usage();
113 }
114 secure_boot = 1;
115 signature_size = 256; //Support SHA 256
116 cert_chain_size = atoi(argv[5]);
117 }
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -0800118 }
119
120 if(stat(argv[1], &s)) {
121 perror("cannot stat binary");
122 return -1;
123 }
124
125 if(unified_boot) {
126 magic = unified_boot_magic;
127 magic_len = sizeof(unified_boot_magic);
128 } else {
129 magic = non_unified_boot_magic;
130 magic_len = sizeof(non_unified_boot_magic);
131 }
132
133 size = s.st_size;
David Ng6e1711f2010-01-19 15:27:00 -0800134#if MEMBASE
135 base = MEMBASE;
136#else
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -0800137 base = 0;
David Ng6e1711f2010-01-19 15:27:00 -0800138#endif
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -0800139
Kinson Chikc874a2b2010-11-16 18:24:05 -0800140 printf("Image Destination Pointer: 0x%x\n", base);
141
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -0800142 magic[0] = 0x00000005; /* appsbl */
Kinson Chik6a7be432010-11-08 17:13:02 -0800143 magic[1] = 0x00000003; //Flash_partition_version /* nand */
144 magic[2] = 0x00000000; //image source pointer
145 magic[3] = base; //image destination pointer
146 magic[4] = size + cert_chain_size + signature_size; //image size
147 magic[5] = size; //code size
148 magic[6] = base + size;
149 magic[7] = signature_size;
150 magic[8] = size + base + signature_size;
151 magic[9] = cert_chain_size;
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -0800152
153 if (unified_boot == 1)
154 {
155 magic[10] = 0x33836685; /* cookie magic number */
156 magic[11] = 0x00000001; /* cookie version */
157 magic[12] = 0x00000002; /* file formats */
158 magic[13] = 0x00000000;
Subbaraman Narayanamurthy1ea479e2010-10-08 14:54:16 -0700159 magic[14] = 0x00000000; /* not setting size for boot.img */
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -0800160 magic[15] = 0x00000000;
161 magic[16] = 0x00000000;
162 magic[17] = 0x00000000;
163 magic[18] = 0x00000000;
164 magic[19] = 0x00000000;
165 }
166
167 fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
168 if(fd < 0) {
169 perror("cannot open header for writing");
170 return -1;
171 }
172 if(write(fd, magic, magic_len) != magic_len) {
173 perror("cannot write header");
174 close(fd);
175 unlink(argv[2]);
176 return -1;
177 }
178 close(fd);
179
Kinson Chik6a7be432010-11-08 17:13:02 -0800180 if (secure_boot && argc > 6){
181 FILE * input_file;
182 FILE * output_file;
183 unsigned buff_size = 1;
184 char buf[buff_size];
185 unsigned bytes_left;
186 unsigned current_cert_chain_size = 0;
187 int padding_size = 0;
188 int i;
189
190 if((output_file = fopen(argv[6], "wb"))==NULL){
191 perror("ERROR: Occured during fopen");
192 return -1;
193 }
Kinson Chikc874a2b2010-11-16 18:24:05 -0800194 printf("Certificate Chain Output File: %s\n", argv[6]);
Kinson Chik6a7be432010-11-08 17:13:02 -0800195
196 for (i = 8; i < argc; i++){
197 if((input_file = fopen(argv[i], "rb"))==NULL){
198 perror("ERROR: Occured during fopen");
199 return -1;
200 }
201 stat(argv[i], &s);
202 bytes_left = s.st_size;
203 current_cert_chain_size += bytes_left;
204 if (cat(input_file, output_file, bytes_left, buff_size))
205 return -1;
206 fclose(input_file);
207 }
208
209 //Pad certifcate chain to the max expected size from input
210 memset(buf, 0xFF, sizeof(buf));
211 padding_size = cert_chain_size - current_cert_chain_size;
Kinson Chik kchik@codeaurora.org1cb7b4d2010-12-15 15:00:59 -0800212
213 if(padding_size <0){
214 fprintf(stderr, "ERROR: Input certificate chain (Size=%d) is larger than the maximum specified (Size=%d)\n",
215 current_cert_chain_size, cert_chain_size);
216 return -1;
217 }
218
Kinson Chik6a7be432010-11-08 17:13:02 -0800219 bytes_left = (padding_size > 0) ? padding_size : 0;
220 while(bytes_left){
221 if(!ferror(output_file))
222 bytes_left -= fwrite(buf, sizeof(buf), buff_size, output_file);
223 else{
224 fprintf(stderr, "ERROR: Occured during certifcate chain padding\n");
225 return -1;
226 }
227 }
228 fclose(output_file);
229
230 //Concat and combine to signed image. Format [HDR][RAW APPSBOOT][PADDED CERT CHAIN]
231 if((output_file = fopen(argv[4], "wb"))==NULL){
232 perror("ERROR: Occured during fopen");
233 return -1;
234 }
Kinson Chikc874a2b2010-11-16 18:24:05 -0800235 printf("Image Output File: %s\n", argv[4]);
Kinson Chik6a7be432010-11-08 17:13:02 -0800236
237 //Header
238 if((input_file = fopen(argv[2], "rb"))==NULL){
239 perror("ERROR: Occured during fopen");
240 return -1;
241 }
242 stat(argv[2], &s);
243 if (cat(input_file, output_file, s.st_size, buff_size))
244 return -1;
245 fclose(input_file);
246
247 //Raw Appsbl
248 if((input_file = fopen(argv[1], "rb"))==NULL){
249 perror("ERROR: Occured during fopen");
250 return -1;
251 }
252 stat(argv[1], &s);
253 if(cat(input_file, output_file, s.st_size, buff_size))
254 return -1;
255 fclose(input_file);
256
257 //Signature
258 if((input_file = fopen(argv[7], "rb"))==NULL){
259 perror("ERROR: Occured during fopen");
260 return -1;
261 }
262 stat(argv[7], &s);
263 if(cat(input_file, output_file, s.st_size, buff_size))
264 return -1;
265 fclose(input_file);
266
267 //Certifcate Chain
268 if((input_file = fopen(argv[6], "rb"))==NULL){
269 perror("ERROR: Occured during fopen");
270 return -1;
271 }
272 if(cat(input_file, output_file, (current_cert_chain_size + padding_size), buff_size))
273 return -1;
274 fclose(input_file);
275
276 fclose(output_file);
277
278 }else if(argc == 5 || argc == 6){
279 FILE * input_file;
280 FILE * output_file;
281 unsigned buff_size = 1;
282 char buf[buff_size];
283
284 //Concat and combine to unsigned image. Format [HDR][RAW APPSBOOT]
285 if((output_file = fopen(argv[4], "wb"))==NULL){
286 perror("ERROR: Occured during fopen");
287 return -1;
288 }
Kinson Chikc874a2b2010-11-16 18:24:05 -0800289 printf("Image Output File: %s\n", argv[4]);
Kinson Chik6a7be432010-11-08 17:13:02 -0800290
291 //Header
292 if((input_file = fopen(argv[2], "rb"))==NULL){
293 perror("ERROR: Occured during fopen");
294 return -1;
295 }
296 stat(argv[2], &s);
297 if (cat(input_file, output_file, s.st_size, buff_size))
298 return -1;
299 fclose(input_file);
300
301 //Raw Appsbl
302 if((input_file = fopen(argv[1], "rb"))==NULL){
303 perror("ERROR: Occured during fopen");
304 return -1;
305 }
306 stat(argv[1], &s);
307 if(cat(input_file, output_file, s.st_size, buff_size))
308 return -1;
309 fclose(input_file);
310 fclose(output_file);
311 }
312
Kinson Chikc874a2b2010-11-16 18:24:05 -0800313 printf("Done execution\n");
314
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -0800315 return 0;
316}