blob: a366efc30605b6877d35f5735a5a4ba9e77f6c8b [file] [log] [blame]
Channagoud Kadabic3fad882011-03-31 16:45:11 +05301/*
2 * Copyright (c) 2007, Google Inc.
3 * All rights reserved.
4 *
Duy Truongf3ac7b32013-02-13 01:07:28 -08005 * Copyright (c) 2009-2011, The Linux Foundation. All rights reserved.
Channagoud Kadabic3fad882011-03-31 16:45:11 +05306 *
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>
Channagoud Kadabic3fad882011-03-31 16:45:11 +053038#include <string.h>
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -080039
40#include <sys/stat.h>
41
Channagoud Kadabic3fad882011-03-31 16:45:11 +053042int 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;
David Ngbf2eb3c2009-12-09 23:48:07 -080079 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;
Channagoud Kadabic3fad882011-03-31 16:45:11 +053084 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
David Ngbf2eb3c2009-12-09 23:48:07 -080089 if(argc < 3) {
Channagoud Kadabic3fad882011-03-31 16:45:11 +053090 return print_usage();
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -080091 }
92
David Ngbf2eb3c2009-12-09 23:48:07 -080093 if (argc == 4) {
94 if(!strcmp("unified-boot",argv[3])) {
95 unified_boot = 1;
Channagoud Kadabic3fad882011-03-31 16:45:11 +053096 }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();
100 }else if(!strcmp("unsecure-boot",argv[3])){
101 fprintf(stderr,"ERROR: Missing arguments: outbin directory\n");
102 return print_usage();
David Ngbf2eb3c2009-12-09 23:48:07 -0800103 }
104 }
105
Channagoud Kadabic3fad882011-03-31 16:45:11 +0530106 if (argc > 4) {
107 if(!strcmp("secure-boot",argv[3])) {
108 if(argc < 9 && argc != 6){
109 fprintf(stderr,
110 "ERROR: Missing argument(s): [outbin maxsize] | [outbin, maxsize, certchain, signature + certifcate(s)]\n");
111 return print_usage();
112 }
113 secure_boot = 1;
114 signature_size = 256; //Support SHA 256
115 cert_chain_size = atoi(argv[5]);
116 }
117 }
118
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -0800119 if(stat(argv[1], &s)) {
120 perror("cannot stat binary");
121 return -1;
122 }
123
David Ngbf2eb3c2009-12-09 23:48:07 -0800124 if(unified_boot) {
125 magic = unified_boot_magic;
126 magic_len = sizeof(unified_boot_magic);
127 } else {
128 magic = non_unified_boot_magic;
129 magic_len = sizeof(non_unified_boot_magic);
130 }
131
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -0800132 size = s.st_size;
133 base = 0;
134
135 magic[0] = 0x00000005; /* appsbl */
Channagoud Kadabic3fad882011-03-31 16:45:11 +0530136 magic[1] = 0x00000003; //Flash_partition_version /* nand */
137 magic[2] = 0x00000000; //image source pointer
138 magic[3] = base; //image destination pointer
139 magic[4] = size + cert_chain_size + signature_size; //image size
140 magic[5] = size; //code size
141 magic[6] = base + size;
142 magic[7] = signature_size;
143 magic[8] = size + base + signature_size;
144 magic[9] = cert_chain_size;
David Ngbf2eb3c2009-12-09 23:48:07 -0800145
146 if (unified_boot == 1)
147 {
148 magic[10] = 0x33836685; /* cookie magic number */
149 magic[11] = 0x00000001; /* cookie version */
150 magic[12] = 0x00000002; /* file formats */
151 magic[13] = 0x00000000;
Subbaraman Narayanamurthy6e5ba422010-08-25 19:21:51 -0700152 magic[14] = 0x00000000; /* not setting size for boot.img */
David Ngbf2eb3c2009-12-09 23:48:07 -0800153 magic[15] = 0x00000000;
154 magic[16] = 0x00000000;
155 magic[17] = 0x00000000;
156 magic[18] = 0x00000000;
157 magic[19] = 0x00000000;
158 }
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -0800159
160 fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
161 if(fd < 0) {
162 perror("cannot open header for writing");
163 return -1;
164 }
David Ngbf2eb3c2009-12-09 23:48:07 -0800165 if(write(fd, magic, magic_len) != magic_len) {
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -0800166 perror("cannot write header");
167 close(fd);
168 unlink(argv[2]);
169 return -1;
170 }
171 close(fd);
172
Channagoud Kadabic3fad882011-03-31 16:45:11 +0530173 if (secure_boot && argc > 6){
174 FILE * input_file;
175 FILE * output_file;
176 unsigned buff_size = 1;
177 char buf[buff_size];
178 unsigned bytes_left;
179 unsigned current_cert_chain_size = 0;
180 int padding_size = 0;
181 int i;
182
183 if((output_file = fopen(argv[6], "wb"))==NULL){
184 perror("ERROR: Occured during fopen");
185 return -1;
186 }
187
188 for (i = 8; i < argc; i++){
189 if((input_file = fopen(argv[i], "rb"))==NULL){
190 perror("ERROR: Occured during fopen");
191 return -1;
192 }
193 stat(argv[i], &s);
194 bytes_left = s.st_size;
195 current_cert_chain_size += bytes_left;
196 if (cat(input_file, output_file, bytes_left, buff_size))
197 return -1;
198 fclose(input_file);
199 }
200
201 //Pad certifcate chain to the max expected size from input
202 memset(buf, 0xFF, sizeof(buf));
203 padding_size = cert_chain_size - current_cert_chain_size;
204 bytes_left = (padding_size > 0) ? padding_size : 0;
205 while(bytes_left){
206 if(!ferror(output_file))
207 bytes_left -= fwrite(buf, sizeof(buf), buff_size, output_file);
208 else{
209 fprintf(stderr, "ERROR: Occured during certifcate chain padding\n");
210 return -1;
211 }
212 }
213 fclose(output_file);
214
215 //Concat and combine to signed image. Format [HDR][RAW APPSBOOT][PADDED CERT CHAIN]
216 if((output_file = fopen(argv[4], "wb"))==NULL){
217 perror("ERROR: Occured during fopen");
218 return -1;
219 }
220
221 //Header
222 if((input_file = fopen(argv[2], "rb"))==NULL){
223 perror("ERROR: Occured during fopen");
224 return -1;
225 }
226 stat(argv[2], &s);
227 if (cat(input_file, output_file, s.st_size, buff_size))
228 return -1;
229 fclose(input_file);
230
231 //Raw Appsbl
232 if((input_file = fopen(argv[1], "rb"))==NULL){
233 perror("ERROR: Occured during fopen");
234 return -1;
235 }
236 stat(argv[1], &s);
237 if(cat(input_file, output_file, s.st_size, buff_size))
238 return -1;
239 fclose(input_file);
240
241 //Signature
242 if((input_file = fopen(argv[7], "rb"))==NULL){
243 perror("ERROR: Occured during fopen");
244 return -1;
245 }
246 stat(argv[7], &s);
247 if(cat(input_file, output_file, s.st_size, buff_size))
248 return -1;
249 fclose(input_file);
250
251 //Certifcate Chain
252 if((input_file = fopen(argv[6], "rb"))==NULL){
253 perror("ERROR: Occured during fopen");
254 return -1;
255 }
256 if(cat(input_file, output_file, (current_cert_chain_size + padding_size), buff_size))
257 return -1;
258 fclose(input_file);
259
260 fclose(output_file);
261
262 }else if(argc == 5 || argc == 6){
263 FILE * input_file;
264 FILE * output_file;
265 unsigned buff_size = 1;
266 char buf[buff_size];
267
268 //Concat and combine to unsigned image. Format [HDR][RAW APPSBOOT]
269 if((output_file = fopen(argv[4], "wb"))==NULL){
270 perror("ERROR: Occured during fopen");
271 return -1;
272 }
273
274 //Header
275 if((input_file = fopen(argv[2], "rb"))==NULL){
276 perror("ERROR: Occured during fopen");
277 return -1;
278 }
279 stat(argv[2], &s);
280 if (cat(input_file, output_file, s.st_size, buff_size))
281 return -1;
282 fclose(input_file);
283
284 //Raw Appsbl
285 if((input_file = fopen(argv[1], "rb"))==NULL){
286 perror("ERROR: Occured during fopen");
287 return -1;
288 }
289 stat(argv[1], &s);
290 if(cat(input_file, output_file, s.st_size, buff_size))
291 return -1;
292 fclose(input_file);
293 fclose(output_file);
294 }
295
Chandan Uddarajua9b07bb2009-11-21 12:22:02 -0800296 return 0;
297}