blob: 29b0e18c2a070cdf5bfa481426fd12ace1561edb [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;
212 bytes_left = (padding_size > 0) ? padding_size : 0;
213 while(bytes_left){
214 if(!ferror(output_file))
215 bytes_left -= fwrite(buf, sizeof(buf), buff_size, output_file);
216 else{
217 fprintf(stderr, "ERROR: Occured during certifcate chain padding\n");
218 return -1;
219 }
220 }
221 fclose(output_file);
222
223 //Concat and combine to signed image. Format [HDR][RAW APPSBOOT][PADDED CERT CHAIN]
224 if((output_file = fopen(argv[4], "wb"))==NULL){
225 perror("ERROR: Occured during fopen");
226 return -1;
227 }
Kinson Chikc874a2b2010-11-16 18:24:05 -0800228 printf("Image Output File: %s\n", argv[4]);
Kinson Chik6a7be432010-11-08 17:13:02 -0800229
230 //Header
231 if((input_file = fopen(argv[2], "rb"))==NULL){
232 perror("ERROR: Occured during fopen");
233 return -1;
234 }
235 stat(argv[2], &s);
236 if (cat(input_file, output_file, s.st_size, buff_size))
237 return -1;
238 fclose(input_file);
239
240 //Raw Appsbl
241 if((input_file = fopen(argv[1], "rb"))==NULL){
242 perror("ERROR: Occured during fopen");
243 return -1;
244 }
245 stat(argv[1], &s);
246 if(cat(input_file, output_file, s.st_size, buff_size))
247 return -1;
248 fclose(input_file);
249
250 //Signature
251 if((input_file = fopen(argv[7], "rb"))==NULL){
252 perror("ERROR: Occured during fopen");
253 return -1;
254 }
255 stat(argv[7], &s);
256 if(cat(input_file, output_file, s.st_size, buff_size))
257 return -1;
258 fclose(input_file);
259
260 //Certifcate Chain
261 if((input_file = fopen(argv[6], "rb"))==NULL){
262 perror("ERROR: Occured during fopen");
263 return -1;
264 }
265 if(cat(input_file, output_file, (current_cert_chain_size + padding_size), buff_size))
266 return -1;
267 fclose(input_file);
268
269 fclose(output_file);
270
271 }else if(argc == 5 || argc == 6){
272 FILE * input_file;
273 FILE * output_file;
274 unsigned buff_size = 1;
275 char buf[buff_size];
276
277 //Concat and combine to unsigned image. Format [HDR][RAW APPSBOOT]
278 if((output_file = fopen(argv[4], "wb"))==NULL){
279 perror("ERROR: Occured during fopen");
280 return -1;
281 }
Kinson Chikc874a2b2010-11-16 18:24:05 -0800282 printf("Image Output File: %s\n", argv[4]);
Kinson Chik6a7be432010-11-08 17:13:02 -0800283
284 //Header
285 if((input_file = fopen(argv[2], "rb"))==NULL){
286 perror("ERROR: Occured during fopen");
287 return -1;
288 }
289 stat(argv[2], &s);
290 if (cat(input_file, output_file, s.st_size, buff_size))
291 return -1;
292 fclose(input_file);
293
294 //Raw Appsbl
295 if((input_file = fopen(argv[1], "rb"))==NULL){
296 perror("ERROR: Occured during fopen");
297 return -1;
298 }
299 stat(argv[1], &s);
300 if(cat(input_file, output_file, s.st_size, buff_size))
301 return -1;
302 fclose(input_file);
303 fclose(output_file);
304 }
305
Kinson Chikc874a2b2010-11-16 18:24:05 -0800306 printf("Done execution\n");
307
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -0800308 return 0;
309}