blob: 06896750aaaad6f2d7d9de296057208ce7be6aa1 [file] [log] [blame]
Randall Spanglerae87b922011-05-12 13:27:28 -07001/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Randall Spangler7d6898d2010-06-11 09:22:13 -07002 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * Verified boot kernel utility
6 */
7
Bill Richardsona08b5c92010-06-30 21:59:43 -07008#include <errno.h>
Randall Spangler7d6898d2010-06-11 09:22:13 -07009#include <getopt.h>
10#include <inttypes.h> /* For PRIu64 */
Bill Richardson249677d2010-06-23 11:16:37 -070011#include <stdarg.h>
Randall Spangler7d6898d2010-06-11 09:22:13 -070012#include <stddef.h>
13#include <stdio.h>
14#include <stdlib.h>
Bill Richardsona08b5c92010-06-30 21:59:43 -070015#include <string.h>
16#include <sys/stat.h>
17#include <sys/types.h>
Randall Spangler7d6898d2010-06-11 09:22:13 -070018#include <unistd.h>
19
20#include "cryptolib.h"
21#include "host_common.h"
22#include "kernel_blob.h"
23#include "vboot_common.h"
24
25
Bill Richardson249677d2010-06-23 11:16:37 -070026/* Global opt */
27static int opt_debug = 0;
28
Bill Richardsona08b5c92010-06-30 21:59:43 -070029static const int DEFAULT_PADDING = 65536;
Bill Richardson249677d2010-06-23 11:16:37 -070030
Randall Spangler7d6898d2010-06-11 09:22:13 -070031/* Command line options */
32enum {
33 OPT_MODE_PACK = 1000,
Bill Richardsona08b5c92010-06-30 21:59:43 -070034 OPT_MODE_REPACK,
Randall Spangler7d6898d2010-06-11 09:22:13 -070035 OPT_MODE_VERIFY,
Che-Liang Chiou03762032011-02-22 11:16:51 +080036 OPT_ARCH,
Bill Richardsona08b5c92010-06-30 21:59:43 -070037 OPT_OLDBLOB,
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +080038 OPT_KLOADADDR,
Randall Spangler7d6898d2010-06-11 09:22:13 -070039 OPT_KEYBLOCK,
40 OPT_SIGNPUBKEY,
41 OPT_SIGNPRIVATE,
42 OPT_VERSION,
43 OPT_VMLINUZ,
44 OPT_BOOTLOADER,
45 OPT_CONFIG,
Bill Richardsona08b5c92010-06-30 21:59:43 -070046 OPT_VBLOCKONLY,
Randall Spangler7d6898d2010-06-11 09:22:13 -070047 OPT_PAD,
vbendebb2b0fcc2010-07-15 15:09:47 -070048 OPT_VERBOSE,
Randall Spanglerae87b922011-05-12 13:27:28 -070049 OPT_MINVERSION,
Randall Spangler7d6898d2010-06-11 09:22:13 -070050};
51
Che-Liang Chiou03762032011-02-22 11:16:51 +080052enum {
53 ARCH_ARM,
54 ARCH_X86 /* default */
55};
56
Randall Spangler7d6898d2010-06-11 09:22:13 -070057static struct option long_opts[] = {
58 {"pack", 1, 0, OPT_MODE_PACK },
Bill Richardsona08b5c92010-06-30 21:59:43 -070059 {"repack", 1, 0, OPT_MODE_REPACK },
Randall Spangler7d6898d2010-06-11 09:22:13 -070060 {"verify", 1, 0, OPT_MODE_VERIFY },
Che-Liang Chiou03762032011-02-22 11:16:51 +080061 {"arch", 1, 0, OPT_ARCH },
Bill Richardsona08b5c92010-06-30 21:59:43 -070062 {"oldblob", 1, 0, OPT_OLDBLOB },
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +080063 {"kloadaddr", 1, 0, OPT_KLOADADDR },
Randall Spangler7d6898d2010-06-11 09:22:13 -070064 {"keyblock", 1, 0, OPT_KEYBLOCK },
65 {"signpubkey", 1, 0, OPT_SIGNPUBKEY },
66 {"signprivate", 1, 0, OPT_SIGNPRIVATE },
67 {"version", 1, 0, OPT_VERSION },
Randall Spanglerae87b922011-05-12 13:27:28 -070068 {"minversion", 1, 0, OPT_MINVERSION },
Randall Spangler7d6898d2010-06-11 09:22:13 -070069 {"vmlinuz", 1, 0, OPT_VMLINUZ },
70 {"bootloader", 1, 0, OPT_BOOTLOADER },
71 {"config", 1, 0, OPT_CONFIG },
Bill Richardsona08b5c92010-06-30 21:59:43 -070072 {"vblockonly", 0, 0, OPT_VBLOCKONLY },
Randall Spangler7d6898d2010-06-11 09:22:13 -070073 {"pad", 1, 0, OPT_PAD },
vbendebb2b0fcc2010-07-15 15:09:47 -070074 {"verbose", 0, 0, OPT_VERBOSE },
Bill Richardson249677d2010-06-23 11:16:37 -070075 {"debug", 0, &opt_debug, 1 },
Randall Spangler7d6898d2010-06-11 09:22:13 -070076 {NULL, 0, 0, 0}
77};
78
79
80/* Print help and return error */
Bill Richardsona08b5c92010-06-30 21:59:43 -070081static int PrintHelp(char *progname) {
82 fprintf(stderr,
83 "This program creates, signs, and verifies the kernel blob\n");
84 fprintf(stderr,
85 "\n"
86 "Usage: %s --pack <file> [PARAMETERS]\n"
87 "\n"
88 " Required parameters:\n"
89 " --keyblock <file> Key block in .keyblock format\n"
Bill Richardson4f36ef32010-08-09 17:50:14 -070090 " --signprivate <file>"
91 " Private key to sign kernel data, in .vbprivk format\n"
Bill Richardsona08b5c92010-06-30 21:59:43 -070092 " --version <number> Kernel version\n"
93 " --vmlinuz <file> Linux kernel bzImage file\n"
94 " --bootloader <file> Bootloader stub\n"
vbendebb2b0fcc2010-07-15 15:09:47 -070095 " --config <file> Command line file\n"
Che-Liang Chiou03762032011-02-22 11:16:51 +080096 " --arch <arch> Cpu architecture (default x86)\n"
Bill Richardsona08b5c92010-06-30 21:59:43 -070097 "\n"
98 " Optional:\n"
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +080099 " --kloadaddr <address> Assign kernel body load address\n"
Bill Richardsona08b5c92010-06-30 21:59:43 -0700100 " --pad <number> Verification padding size in bytes\n"
101 " --vblockonly Emit just the verification blob\n",
102 progname);
103 fprintf(stderr,
104 "\nOR\n\n"
105 "Usage: %s --repack <file> [PARAMETERS]\n"
106 "\n"
vbendeb858fffb2010-10-06 09:51:44 -0700107 " Required parameters (of --keyblock, --config, and --version \n"
108 " at least one is required):\n"
Bill Richardsona08b5c92010-06-30 21:59:43 -0700109 " --keyblock <file> Key block in .keyblock format\n"
Bill Richardson4f36ef32010-08-09 17:50:14 -0700110 " --signprivate <file>"
111 " Private key to sign kernel data, in .vbprivk format\n"
Bill Richardsona08b5c92010-06-30 21:59:43 -0700112 " --oldblob <file> Previously packed kernel blob\n"
vbendebb2b0fcc2010-07-15 15:09:47 -0700113 " --config <file> New command line file\n"
vbendeb858fffb2010-10-06 09:51:44 -0700114 " --version <number> Kernel version\n"
Bill Richardsona08b5c92010-06-30 21:59:43 -0700115 "\n"
116 " Optional:\n"
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800117 " --kloadaddr <address> Assign kernel body load address\n"
Bill Richardsona08b5c92010-06-30 21:59:43 -0700118 " --pad <number> Verification padding size in bytes\n"
119 " --vblockonly Emit just the verification blob\n",
120 progname);
121 fprintf(stderr,
122 "\nOR\n\n"
123 "Usage: %s --verify <file> [PARAMETERS]\n"
124 "\n"
vbendebb2b0fcc2010-07-15 15:09:47 -0700125 " Optional:\n"
Bill Richardson4f36ef32010-08-09 17:50:14 -0700126 " --signpubkey <file>"
127 " Public key to verify kernel keyblock, in .vbpubk format\n"
vbendebb2b0fcc2010-07-15 15:09:47 -0700128 " --verbose Print a more detailed report\n"
Will Drewry9342f882010-10-26 10:22:05 -0500129 " --keyblock <file>"
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800130 " Outputs the verified key block, in .keyblock format\n"
131 " --kloadaddr <address> Assign kernel body load address\n"
Randall Spangler7ecb39d2011-05-12 15:37:45 -0700132 " --pad <number> Verification padding size in bytes\n"
Randall Spanglerae87b922011-05-12 13:27:28 -0700133 " --minversion <number> Minimum combined kernel key version\n"
134 " and kernel version\n"
Bill Richardsona08b5c92010-06-30 21:59:43 -0700135 "\n",
136 progname);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700137 return 1;
138}
139
Bill Richardson249677d2010-06-23 11:16:37 -0700140static void Debug(const char *format, ...) {
141 if (!opt_debug)
142 return;
143
144 va_list ap;
145 va_start(ap, format);
146 fprintf(stderr, "DEBUG: ");
147 vfprintf(stderr, format, ap);
148 va_end(ap);
149}
150
Randall Spangler32a65262011-06-27 10:49:11 -0700151
Bill Richardson2f6a71f2010-10-14 09:25:39 -0700152/* Return an explanation when fread() fails. */
153static const char *error_fread(FILE *fp) {
154 const char *retval = "beats me why";
155 if (feof(fp))
156 retval = "EOF";
157 else if (ferror(fp))
158 retval = strerror(errno);
159 clearerr(fp);
160 return retval;
161}
Randall Spangler7d6898d2010-06-11 09:22:13 -0700162
163/* Return the smallest integral multiple of [alignment] that is equal
164 * to or greater than [val]. Used to determine the number of
165 * pages/sectors/blocks/whatever needed to contain [val]
166 * items/bytes/etc. */
167static uint64_t roundup(uint64_t val, uint64_t alignment) {
168 uint64_t rem = val % alignment;
169 if ( rem )
170 return val + (alignment - rem);
171 return val;
172}
173
174
175/* Match regexp /\b--\b/ to delimit the start of the kernel commandline. If we
176 * don't find one, we'll use the whole thing. */
177static unsigned int find_cmdline_start(char *input, unsigned int max_len) {
178 int start = 0;
179 int i;
180 for(i = 0; i < max_len - 1 && input[i]; i++) {
181 if ('-' == input[i] && '-' == input[i + 1]) { /* found a "--" */
182 if ((i == 0 || ' ' == input[i - 1]) && /* nothing before it */
183 (i + 2 >= max_len || ' ' == input[i+2])) { /* nothing after it */
184 start = i+2; /* note: hope there's a trailing '\0' */
185 break;
186 }
187 }
188 }
189 while(' ' == input[start]) /* skip leading spaces */
190 start++;
191
192 return start;
193}
194
195
Bill Richardsona08b5c92010-06-30 21:59:43 -0700196typedef struct blob_s {
197 /* Stuff needed by VbKernelPreambleHeader */
198 uint64_t kernel_version;
199 uint64_t bootloader_address;
200 uint64_t bootloader_size;
201 /* Raw kernel blob data */
202 uint64_t blob_size;
203 uint8_t *blob;
vbendebb2b0fcc2010-07-15 15:09:47 -0700204
205 /* these fields are not always initialized */
206 VbKernelPreambleHeader* preamble;
207 VbKeyBlockHeader* key_block;
208 uint8_t *buf;
209
Bill Richardsona08b5c92010-06-30 21:59:43 -0700210} blob_t;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700211
vbendebb2b0fcc2010-07-15 15:09:47 -0700212/* Given a blob return the location of the kernel command line buffer. */
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800213static char* BpCmdLineLocation(blob_t *bp, uint64_t kernel_body_load_address)
vbendebb2b0fcc2010-07-15 15:09:47 -0700214{
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800215 return (char*)(bp->blob + bp->bootloader_address - kernel_body_load_address -
vbendebb2b0fcc2010-07-15 15:09:47 -0700216 CROS_CONFIG_SIZE - CROS_PARAMS_SIZE);
217}
Bill Richardsona08b5c92010-06-30 21:59:43 -0700218
219static void FreeBlob(blob_t *bp) {
220 if (bp) {
221 if (bp->blob)
Randall Spangler32a65262011-06-27 10:49:11 -0700222 free(bp->blob);
vbendebb2b0fcc2010-07-15 15:09:47 -0700223 if (bp->buf)
Randall Spangler32a65262011-06-27 10:49:11 -0700224 free(bp->buf);
225 free(bp);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700226 }
227}
228
vbendebb2b0fcc2010-07-15 15:09:47 -0700229/*
230 * Read the kernel command line from a file. Get rid of \n characters along
231 * the way and verify that the line fits into a 4K buffer.
232 *
233 * Return the buffer contaning the line on success (and set the line length
234 * using the passed in parameter), or NULL in case something goes wrong.
235 */
236static uint8_t* ReadConfigFile(const char* config_file, uint64_t* config_size)
237{
238 uint8_t* config_buf;
239 int ii;
240
241 config_buf = ReadFile(config_file, config_size);
242 Debug(" config file size=0x%" PRIx64 "\n", *config_size);
243 if (CROS_CONFIG_SIZE <= *config_size) { /* need room for trailing '\0' */
Randall Spangler32a65262011-06-27 10:49:11 -0700244 VbExError("Config file %s is too large (>= %d bytes)\n",
245 config_file, CROS_CONFIG_SIZE);
vbendebb2b0fcc2010-07-15 15:09:47 -0700246 return NULL;
247 }
248
249 /* Replace newlines with spaces */
250 for (ii = 0; ii < *config_size; ii++) {
251 if ('\n' == config_buf[ii]) {
252 config_buf[ii] = ' ';
253 }
254 }
255 return config_buf;
256}
257
Bill Richardsona08b5c92010-06-30 21:59:43 -0700258/* Create a blob from its components */
259static blob_t *NewBlob(uint64_t version,
260 const char* vmlinuz,
261 const char* bootloader_file,
Che-Liang Chiou03762032011-02-22 11:16:51 +0800262 const char* config_file,
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800263 int arch,
264 uint64_t kernel_body_load_address) {
Che-Liang Chiou03762032011-02-22 11:16:51 +0800265 blob_t* bp;
266 struct linux_kernel_header* lh = 0;
267 struct linux_kernel_params* params = 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700268 uint8_t* config_buf;
269 uint64_t config_size;
270 uint8_t* bootloader_buf;
271 uint64_t bootloader_size;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700272 uint8_t* kernel_buf;
273 uint64_t kernel_size;
274 uint64_t kernel32_start = 0;
275 uint64_t kernel32_size = 0;
276 uint32_t cmdline_addr;
277 uint8_t* blob = NULL;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700278 uint64_t now = 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700279
Randall Spangler7d6898d2010-06-11 09:22:13 -0700280 if (!vmlinuz || !bootloader_file || !config_file) {
Randall Spangler32a65262011-06-27 10:49:11 -0700281 VbExError("Must specify all input files\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700282 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700283 }
284
Randall Spangler32a65262011-06-27 10:49:11 -0700285 bp = (blob_t *)malloc(sizeof(blob_t));
Bill Richardsona08b5c92010-06-30 21:59:43 -0700286 if (!bp) {
Randall Spangler32a65262011-06-27 10:49:11 -0700287 VbExError("Couldn't allocate bytes for blob_t.\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700288 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700289 }
vbendebb2b0fcc2010-07-15 15:09:47 -0700290
291 Memset(bp, 0, sizeof(*bp));
Bill Richardsona08b5c92010-06-30 21:59:43 -0700292 bp->kernel_version = version;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700293
294 /* Read the config file */
Bill Richardson249677d2010-06-23 11:16:37 -0700295 Debug("Reading %s\n", config_file);
vbendebb2b0fcc2010-07-15 15:09:47 -0700296 config_buf = ReadConfigFile(config_file, &config_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700297 if (!config_buf)
Bill Richardsona08b5c92010-06-30 21:59:43 -0700298 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700299
300 /* Read the bootloader */
Bill Richardson249677d2010-06-23 11:16:37 -0700301 Debug("Reading %s\n", bootloader_file);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700302 bootloader_buf = ReadFile(bootloader_file, &bootloader_size);
303 if (!bootloader_buf)
Bill Richardsona08b5c92010-06-30 21:59:43 -0700304 return 0;
Bill Richardson249677d2010-06-23 11:16:37 -0700305 Debug(" bootloader file size=0x%" PRIx64 "\n", bootloader_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700306
307 /* Read the kernel */
Bill Richardson249677d2010-06-23 11:16:37 -0700308 Debug("Reading %s\n", vmlinuz);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700309 kernel_buf = ReadFile(vmlinuz, &kernel_size);
310 if (!kernel_buf)
Bill Richardsona08b5c92010-06-30 21:59:43 -0700311 return 0;
Bill Richardson249677d2010-06-23 11:16:37 -0700312 Debug(" kernel file size=0x%" PRIx64 "\n", kernel_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700313 if (!kernel_size) {
Randall Spangler32a65262011-06-27 10:49:11 -0700314 VbExError("Empty kernel file\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700315 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700316 }
317
Che-Liang Chiou03762032011-02-22 11:16:51 +0800318 if (arch == ARCH_X86) {
319 /* The first part of vmlinuz is a header, followed by a real-mode
320 * boot stub. We only want the 32-bit part. */
321 lh = (struct linux_kernel_header *)kernel_buf;
322 kernel32_start = (lh->setup_sects + 1) << 9;
323 if (kernel32_start >= kernel_size) {
Randall Spangler32a65262011-06-27 10:49:11 -0700324 VbExError("Malformed kernel\n");
Che-Liang Chiou03762032011-02-22 11:16:51 +0800325 return 0;
326 }
327 } else
328 kernel32_start = 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700329 kernel32_size = kernel_size - kernel32_start;
Bill Richardson249677d2010-06-23 11:16:37 -0700330 Debug(" kernel32_start=0x%" PRIx64 "\n", kernel32_start);
331 Debug(" kernel32_size=0x%" PRIx64 "\n", kernel32_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700332
333 /* Allocate and zero the blob we need. */
Bill Richardsona08b5c92010-06-30 21:59:43 -0700334 bp->blob_size = roundup(kernel32_size, CROS_ALIGN) +
Randall Spangler7d6898d2010-06-11 09:22:13 -0700335 CROS_CONFIG_SIZE +
336 CROS_PARAMS_SIZE +
337 roundup(bootloader_size, CROS_ALIGN);
Randall Spangler32a65262011-06-27 10:49:11 -0700338 blob = (uint8_t *)malloc(bp->blob_size);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700339 Debug("blob_size=0x%" PRIx64 "\n", bp->blob_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700340 if (!blob) {
Randall Spangler32a65262011-06-27 10:49:11 -0700341 VbExError("Couldn't allocate %ld bytes.\n", bp->blob_size);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700342 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700343 }
Bill Richardsona08b5c92010-06-30 21:59:43 -0700344 Memset(blob, 0, bp->blob_size);
345 bp->blob = blob;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700346
347 /* Copy the 32-bit kernel. */
Bill Richardson249677d2010-06-23 11:16:37 -0700348 Debug("kernel goes at blob+=0x%" PRIx64 "\n", now);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700349 if (kernel32_size)
350 Memcpy(blob + now, kernel_buf + kernel32_start, kernel32_size);
351 now += roundup(now + kernel32_size, CROS_ALIGN);
352
Bill Richardson249677d2010-06-23 11:16:37 -0700353 Debug("config goes at blob+0x%" PRIx64 "\n", now);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700354 /* Find the load address of the commandline. We'll need it later. */
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800355 cmdline_addr = kernel_body_load_address + now +
Randall Spangler7d6898d2010-06-11 09:22:13 -0700356 find_cmdline_start((char *)config_buf, config_size);
Bill Richardson249677d2010-06-23 11:16:37 -0700357 Debug(" cmdline_addr=0x%" PRIx64 "\n", cmdline_addr);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700358
359 /* Copy the config. */
360 if (config_size)
361 Memcpy(blob + now, config_buf, config_size);
362 now += CROS_CONFIG_SIZE;
363
364 /* The zeropage data is next. Overlay the linux_kernel_header onto it, and
365 * tweak a few fields. */
Bill Richardson249677d2010-06-23 11:16:37 -0700366 Debug("params goes at blob+=0x%" PRIx64 "\n", now);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700367 params = (struct linux_kernel_params *)(blob + now);
Che-Liang Chiou03762032011-02-22 11:16:51 +0800368 if (arch == ARCH_X86)
369 Memcpy(&(params->setup_sects), &(lh->setup_sects),
370 sizeof(*lh) - offsetof(struct linux_kernel_header, setup_sects));
371 else
372 Memset(&(params->setup_sects), 0,
373 sizeof(*lh) - offsetof(struct linux_kernel_header, setup_sects));
Randall Spangler7d6898d2010-06-11 09:22:13 -0700374 params->boot_flag = 0;
375 params->ramdisk_image = 0; /* we don't support initrd */
376 params->ramdisk_size = 0;
377 params->type_of_loader = 0xff;
378 params->cmd_line_ptr = cmdline_addr;
Che-Liang Chiou475bf442010-08-23 11:20:44 +0800379 /* A fake e820 memory map with 2 entries */
380 params->n_e820_entry = 2;
381 params->e820_entries[0].start_addr = 0x00000000;
382 params->e820_entries[0].segment_size = 0x00001000;
383 params->e820_entries[0].segment_type = E820_TYPE_RAM;
384 params->e820_entries[1].start_addr = 0xfffff000;
385 params->e820_entries[1].segment_size = 0x00001000;
386 params->e820_entries[1].segment_type = E820_TYPE_RESERVED;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700387 now += CROS_PARAMS_SIZE;
388
389 /* Finally, append the bootloader. Remember where it will load in
390 * memory, too. */
Bill Richardson249677d2010-06-23 11:16:37 -0700391 Debug("bootloader goes at blob+=0x%" PRIx64 "\n", now);
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800392 bp->bootloader_address = kernel_body_load_address + now;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700393 bp->bootloader_size = roundup(bootloader_size, CROS_ALIGN);
394 Debug(" bootloader_address=0x%" PRIx64 "\n", bp->bootloader_address);
395 Debug(" bootloader_size=0x%" PRIx64 "\n", bp->bootloader_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700396 if (bootloader_size)
397 Memcpy(blob + now, bootloader_buf, bootloader_size);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700398 now += bp->bootloader_size;
Bill Richardson249677d2010-06-23 11:16:37 -0700399 Debug("end of blob is 0x%" PRIx64 "\n", now);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700400
401 /* Free input buffers */
Randall Spangler32a65262011-06-27 10:49:11 -0700402 free(kernel_buf);
403 free(config_buf);
404 free(bootloader_buf);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700405
Bill Richardsona08b5c92010-06-30 21:59:43 -0700406 /* Success */
407 return bp;
408}
409
410
411/* Pull the blob_t stuff out of a prepacked kernel blob file */
Randall Spangler7ecb39d2011-05-12 15:37:45 -0700412static blob_t *OldBlob(const char* filename, uint64_t pad) {
vbendebb2b0fcc2010-07-15 15:09:47 -0700413 FILE* fp = NULL;
414 blob_t *bp = NULL;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700415 struct stat statbuf;
416 VbKeyBlockHeader* key_block;
417 VbKernelPreambleHeader* preamble;
418 uint64_t now = 0;
vbendebb2b0fcc2010-07-15 15:09:47 -0700419 uint8_t* buf = NULL;
420 int ret_error = 1;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700421
422 if (!filename) {
Randall Spangler32a65262011-06-27 10:49:11 -0700423 VbExError("Must specify prepacked blob to read\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700424 return 0;
425 }
426
427 if (0 != stat(filename, &statbuf)) {
Randall Spangler32a65262011-06-27 10:49:11 -0700428 VbExError("Unable to stat %s: %s\n", filename, strerror(errno));
Bill Richardsona08b5c92010-06-30 21:59:43 -0700429 return 0;
430 }
431
432 Debug("%s size is 0x%" PRIx64 "\n", filename, statbuf.st_size);
Randall Spangler7ecb39d2011-05-12 15:37:45 -0700433 if (statbuf.st_size < pad) {
Randall Spangler32a65262011-06-27 10:49:11 -0700434 VbExError("%s is too small to be a valid kernel blob\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700435 return 0;
436 }
437
438 Debug("Reading %s\n", filename);
439 fp = fopen(filename, "rb");
440 if (!fp) {
Randall Spangler32a65262011-06-27 10:49:11 -0700441 VbExError("Unable to open file %s: %s\n", filename, strerror(errno));
Bill Richardsona08b5c92010-06-30 21:59:43 -0700442 return 0;
443 }
444
Randall Spangler32a65262011-06-27 10:49:11 -0700445 buf = malloc(pad);
vbendebb2b0fcc2010-07-15 15:09:47 -0700446 if (!buf) {
Randall Spangler32a65262011-06-27 10:49:11 -0700447 VbExError("Unable to allocate padding\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700448 goto unwind_oldblob;
449 }
450
Randall Spangler7ecb39d2011-05-12 15:37:45 -0700451 if (1 != fread(buf, pad, 1, fp)) {
Randall Spangler32a65262011-06-27 10:49:11 -0700452 VbExError("Unable to read header from %s: %s\n", filename, error_fread(fp));
vbendebb2b0fcc2010-07-15 15:09:47 -0700453 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700454 }
455
456 /* Skip the key block */
457 key_block = (VbKeyBlockHeader*)buf;
458 Debug("Keyblock is 0x%" PRIx64 " bytes\n", key_block->key_block_size);
459 now += key_block->key_block_size;
460 if (now > statbuf.st_size) {
Randall Spangler32a65262011-06-27 10:49:11 -0700461 VbExError("key_block_size advances past the end of the blob\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700462 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700463 }
Randall Spangler7ecb39d2011-05-12 15:37:45 -0700464 if (now > pad) {
Randall Spangler32a65262011-06-27 10:49:11 -0700465 VbExError("key_block_size advances past %" PRIu64 " byte padding\n", pad);
Randall Spangler7ecb39d2011-05-12 15:37:45 -0700466 goto unwind_oldblob;
467 }
Bill Richardsona08b5c92010-06-30 21:59:43 -0700468
469 /* Skip the preamble */
470 preamble = (VbKernelPreambleHeader*)(buf + now);
471 Debug("Preamble is 0x%" PRIx64 " bytes\n", preamble->preamble_size);
472 now += preamble->preamble_size;
473 if (now > statbuf.st_size) {
Randall Spangler32a65262011-06-27 10:49:11 -0700474 VbExError("preamble_size advances past the end of the blob\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700475 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700476 }
Randall Spangler7ecb39d2011-05-12 15:37:45 -0700477 if (now > pad) {
Randall Spangler32a65262011-06-27 10:49:11 -0700478 VbExError("preamble_size advances past %" PRIu64 " byte padding\n", pad);
Randall Spangler7ecb39d2011-05-12 15:37:45 -0700479 goto unwind_oldblob;
480 }
Bill Richardsona08b5c92010-06-30 21:59:43 -0700481
482 /* Go find the kernel blob */
483 Debug("kernel blob is at offset 0x%" PRIx64 "\n", now);
484 if (0 != fseek(fp, now, SEEK_SET)) {
Randall Spangler32a65262011-06-27 10:49:11 -0700485 VbExError("Unable to seek to 0x%" PRIx64 " in %s: %s\n", now, filename,
Bill Richardsona08b5c92010-06-30 21:59:43 -0700486 strerror(errno));
vbendebb2b0fcc2010-07-15 15:09:47 -0700487 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700488 }
489
490 /* Remember what we've got */
Randall Spangler32a65262011-06-27 10:49:11 -0700491 bp = (blob_t *)malloc(sizeof(blob_t));
Bill Richardsona08b5c92010-06-30 21:59:43 -0700492 if (!bp) {
Randall Spangler32a65262011-06-27 10:49:11 -0700493 VbExError("Couldn't allocate bytes for blob_t.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700494 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700495 }
496
vbendebb2b0fcc2010-07-15 15:09:47 -0700497 bp->buf = buf;
498 bp->key_block = key_block;
499 bp->preamble = preamble;
500
Bill Richardsona08b5c92010-06-30 21:59:43 -0700501 bp->kernel_version = preamble->kernel_version;
502 bp->bootloader_address = preamble->bootloader_address;
503 bp->bootloader_size = preamble->bootloader_size;
504 bp->blob_size = preamble->body_signature.data_size;
505
506 Debug(" kernel_version = %d\n", bp->kernel_version);
507 Debug(" bootloader_address = 0x%" PRIx64 "\n", bp->bootloader_address);
508 Debug(" bootloader_size = 0x%" PRIx64 "\n", bp->bootloader_size);
509 Debug(" blob_size = 0x%" PRIx64 "\n", bp->blob_size);
510
Bill Richardson2f6a71f2010-10-14 09:25:39 -0700511 if (!bp->blob_size) {
Randall Spangler32a65262011-06-27 10:49:11 -0700512 VbExError("No kernel blob found\n");
Bill Richardson2f6a71f2010-10-14 09:25:39 -0700513 goto unwind_oldblob;
514 }
515
Randall Spangler32a65262011-06-27 10:49:11 -0700516 bp->blob = (uint8_t *)malloc(bp->blob_size);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700517 if (!bp->blob) {
Randall Spangler32a65262011-06-27 10:49:11 -0700518 VbExError("Couldn't allocate 0x%" PRIx64 " bytes for blob_t.\n",
519 bp->blob_size);
vbendebb2b0fcc2010-07-15 15:09:47 -0700520 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700521 }
522
523 /* read it in */
524 if (1 != fread(bp->blob, bp->blob_size, 1, fp)) {
Randall Spangler32a65262011-06-27 10:49:11 -0700525 VbExError("Unable to read kernel blob from %s: %s\n", filename,
526 error_fread(fp));
vbendebb2b0fcc2010-07-15 15:09:47 -0700527 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700528 }
529
vbendebb2b0fcc2010-07-15 15:09:47 -0700530 ret_error = 0;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700531
vbendebb2b0fcc2010-07-15 15:09:47 -0700532 /* done */
533unwind_oldblob:
534 fclose(fp);
535 if (ret_error) {
536 if (bp) {
537 FreeBlob(bp);
538 bp = NULL;
539 } else if (buf) {
Randall Spangler32a65262011-06-27 10:49:11 -0700540 free(buf);
vbendebb2b0fcc2010-07-15 15:09:47 -0700541 }
542 }
Bill Richardsona08b5c92010-06-30 21:59:43 -0700543 return bp;
544}
545
546
547/* Pack a .kernel */
548static int Pack(const char* outfile, const char* keyblock_file,
549 const char* signprivate, blob_t *bp, uint64_t pad,
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800550 int vblockonly,
551 uint64_t kernel_body_load_address) {
Bill Richardsona08b5c92010-06-30 21:59:43 -0700552 VbPrivateKey* signing_key;
553 VbSignature* body_sig;
554 VbKernelPreambleHeader* preamble;
555 VbKeyBlockHeader* key_block;
556 uint64_t key_block_size;
557 FILE* f;
558 uint64_t i;
559
560 if (!outfile) {
Randall Spangler32a65262011-06-27 10:49:11 -0700561 VbExError("Must specify output filename\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700562 return 1;
563 }
vbendebb2b0fcc2010-07-15 15:09:47 -0700564 if ((!keyblock_file && !bp->key_block) || !signprivate) {
Randall Spangler32a65262011-06-27 10:49:11 -0700565 VbExError("Must specify all keys\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700566 return 1;
567 }
568 if (!bp) {
Randall Spangler32a65262011-06-27 10:49:11 -0700569 VbExError("Refusing to pack invalid kernel blob\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700570 return 1;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700571 }
vbendebb2b0fcc2010-07-15 15:09:47 -0700572
573 /* Get the key block and read the private key. */
574 if (keyblock_file) {
575 key_block = (VbKeyBlockHeader*)ReadFile(keyblock_file, &key_block_size);
576 if (!key_block) {
Randall Spangler32a65262011-06-27 10:49:11 -0700577 VbExError("Error reading key block.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700578 return 1;
579 }
580 } else {
581 key_block = bp->key_block;
582 key_block_size = key_block->key_block_size;
583 }
584
Bill Richardsona08b5c92010-06-30 21:59:43 -0700585 if (pad < key_block->key_block_size) {
Randall Spangler32a65262011-06-27 10:49:11 -0700586 VbExError("Pad too small\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700587 return 1;
588 }
589
Bill Richardsonabf05502010-07-01 10:22:06 -0700590 signing_key = PrivateKeyRead(signprivate);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700591 if (!signing_key) {
Randall Spangler32a65262011-06-27 10:49:11 -0700592 VbExError("Error reading signing key.\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700593 return 1;
594 }
595
Randall Spangler7d6898d2010-06-11 09:22:13 -0700596 /* Sign the kernel data */
Bill Richardsona08b5c92010-06-30 21:59:43 -0700597 body_sig = CalculateSignature(bp->blob, bp->blob_size, signing_key);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700598 if (!body_sig) {
Randall Spangler32a65262011-06-27 10:49:11 -0700599 VbExError("Error calculating body signature\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700600 return 1;
601 }
602
603 /* Create preamble */
Bill Richardsona08b5c92010-06-30 21:59:43 -0700604 preamble = CreateKernelPreamble(bp->kernel_version,
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800605 kernel_body_load_address,
Bill Richardsona08b5c92010-06-30 21:59:43 -0700606 bp->bootloader_address,
607 bp->bootloader_size,
Randall Spangler7d6898d2010-06-11 09:22:13 -0700608 body_sig,
609 pad - key_block_size,
610 signing_key);
611 if (!preamble) {
Randall Spangler32a65262011-06-27 10:49:11 -0700612 VbExError("Error creating preamble.\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700613 return 1;
614 }
615
616 /* Write the output file */
Bill Richardson249677d2010-06-23 11:16:37 -0700617 Debug("writing %s...\n", outfile);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700618 f = fopen(outfile, "wb");
619 if (!f) {
Randall Spangler32a65262011-06-27 10:49:11 -0700620 VbExError("Can't open output file %s\n", outfile);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700621 return 1;
622 }
Bill Richardson249677d2010-06-23 11:16:37 -0700623 Debug("0x%" PRIx64 " bytes of key_block\n", key_block_size);
624 Debug("0x%" PRIx64 " bytes of preamble\n", preamble->preamble_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700625 i = ((1 != fwrite(key_block, key_block_size, 1, f)) ||
Bill Richardsona08b5c92010-06-30 21:59:43 -0700626 (1 != fwrite(preamble, preamble->preamble_size, 1, f)));
Randall Spangler7d6898d2010-06-11 09:22:13 -0700627 if (i) {
Randall Spangler32a65262011-06-27 10:49:11 -0700628 VbExError("Can't write output file %s\n", outfile);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700629 fclose(f);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700630 unlink(outfile);
631 return 1;
632 }
633
Bill Richardsona08b5c92010-06-30 21:59:43 -0700634 if (!vblockonly) {
635 Debug("0x%" PRIx64 " bytes of blob\n", bp->blob_size);
636 i = (1 != fwrite(bp->blob, bp->blob_size, 1, f));
637 if (i) {
Randall Spangler32a65262011-06-27 10:49:11 -0700638 VbExError("Can't write output file %s\n", outfile);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700639 fclose(f);
640 unlink(outfile);
641 return 1;
642 }
643 }
644
645 fclose(f);
646
Randall Spangler7d6898d2010-06-11 09:22:13 -0700647 /* Success */
648 return 0;
649}
650
vbendebb2b0fcc2010-07-15 15:09:47 -0700651/*
652 * Replace kernel command line in a blob representing a kernel.
653 */
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800654static int ReplaceConfig(blob_t* bp, const char* config_file,
655 uint64_t kernel_body_load_address)
vbendebb2b0fcc2010-07-15 15:09:47 -0700656{
657 uint8_t* new_conf;
658 uint64_t config_size;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700659
vbendebb2b0fcc2010-07-15 15:09:47 -0700660 if (!config_file) {
661 return 0;
662 }
663
664 new_conf = ReadConfigFile(config_file, &config_size);
665 if (!new_conf) {
666 return 1;
667 }
668
669 /* fill the config buffer with zeros */
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800670 Memset(BpCmdLineLocation(bp, kernel_body_load_address), 0, CROS_CONFIG_SIZE);
671 Memcpy(BpCmdLineLocation(bp, kernel_body_load_address),
672 new_conf, config_size);
Randall Spangler32a65262011-06-27 10:49:11 -0700673 free(new_conf);
vbendebb2b0fcc2010-07-15 15:09:47 -0700674 return 0;
675}
676
Will Drewry9342f882010-10-26 10:22:05 -0500677static int Verify(const char* infile, const char* signpubkey, int verbose,
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800678 const char* key_block_file,
Randall Spangler7ecb39d2011-05-12 15:37:45 -0700679 uint64_t kernel_body_load_address, uint64_t min_version,
680 uint64_t pad) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700681
682 VbKeyBlockHeader* key_block;
683 VbKernelPreambleHeader* preamble;
684 VbPublicKey* data_key;
Bill Richardson4f36ef32010-08-09 17:50:14 -0700685 VbPublicKey* sign_key = NULL;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700686 RSAPublicKey* rsa;
vbendebb2b0fcc2010-07-15 15:09:47 -0700687 blob_t* bp;
688 uint64_t now;
689 int rv = 1;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700690
Bill Richardson4f36ef32010-08-09 17:50:14 -0700691 if (!infile) {
Randall Spangler32a65262011-06-27 10:49:11 -0700692 VbExError("Must specify filename\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700693 return 1;
694 }
695
696 /* Read public signing key */
Bill Richardson4f36ef32010-08-09 17:50:14 -0700697 if (signpubkey) {
698 sign_key = PublicKeyRead(signpubkey);
699 if (!sign_key) {
Randall Spangler32a65262011-06-27 10:49:11 -0700700 VbExError("Error reading signpubkey.\n");
Bill Richardson4f36ef32010-08-09 17:50:14 -0700701 return 1;
702 }
Randall Spangler7d6898d2010-06-11 09:22:13 -0700703 }
704
705 /* Read blob */
Randall Spangler7ecb39d2011-05-12 15:37:45 -0700706 bp = OldBlob(infile, pad);
vbendebb2b0fcc2010-07-15 15:09:47 -0700707 if (!bp) {
Randall Spangler32a65262011-06-27 10:49:11 -0700708 VbExError("Error reading input file\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700709 return 1;
710 }
711
712 /* Verify key block */
vbendebb2b0fcc2010-07-15 15:09:47 -0700713 key_block = bp->key_block;
Randall Spangler138acfe2010-08-17 15:45:21 -0700714 if (0 != KeyBlockVerify(key_block, bp->blob_size, sign_key,
715 (sign_key ? 0 : 1))) {
Randall Spangler32a65262011-06-27 10:49:11 -0700716 VbExError("Error verifying key block.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700717 goto verify_exit;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700718 }
vbendebb2b0fcc2010-07-15 15:09:47 -0700719 now = key_block->key_block_size;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700720
Will Drewry9342f882010-10-26 10:22:05 -0500721 if (key_block_file) {
722 FILE* f = NULL;
723 f = fopen(key_block_file, "wb");
724 if (!f) {
Randall Spangler32a65262011-06-27 10:49:11 -0700725 VbExError("Can't open key block file %s\n", key_block_file);
Will Drewry9342f882010-10-26 10:22:05 -0500726 return 1;
727 }
728 if (1 != fwrite(key_block, key_block->key_block_size, 1, f)) {
Randall Spangler32a65262011-06-27 10:49:11 -0700729 VbExError("Can't write key block file %s\n", key_block_file);
Will Drewry9342f882010-10-26 10:22:05 -0500730 return 1;
731 }
732 fclose(f);
733 }
734
Randall Spangler7d6898d2010-06-11 09:22:13 -0700735 printf("Key block:\n");
736 data_key = &key_block->data_key;
Bill Richardson4f36ef32010-08-09 17:50:14 -0700737 if (verbose)
738 printf(" Signature: %s\n", sign_key ? "valid" : "ignored");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700739 printf(" Size: 0x%" PRIx64 "\n", key_block->key_block_size);
Bill Richardson60bcbe32010-09-09 14:53:56 -0700740 printf(" Flags: %" PRIu64 " ", key_block->key_block_flags);
741 if (key_block->key_block_flags & KEY_BLOCK_FLAG_DEVELOPER_0)
742 printf(" !DEV");
743 if (key_block->key_block_flags & KEY_BLOCK_FLAG_DEVELOPER_1)
744 printf(" DEV");
745 if (key_block->key_block_flags & KEY_BLOCK_FLAG_RECOVERY_0)
746 printf(" !REC");
747 if (key_block->key_block_flags & KEY_BLOCK_FLAG_RECOVERY_1)
748 printf(" REC");
749 printf("\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700750 printf(" Data key algorithm: %" PRIu64 " %s\n", data_key->algorithm,
751 (data_key->algorithm < kNumAlgorithms ?
752 algo_strings[data_key->algorithm] : "(invalid)"));
753 printf(" Data key version: %" PRIu64 "\n", data_key->key_version);
Bill Richardson60bcbe32010-09-09 14:53:56 -0700754 printf(" Data key sha1sum: ");
755 PrintPubKeySha1Sum(data_key);
756 printf("\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700757
Randall Spanglerae87b922011-05-12 13:27:28 -0700758 if (data_key->key_version < (min_version >> 16)) {
Randall Spangler32a65262011-06-27 10:49:11 -0700759 VbExError("Data key version %" PRIu64
760 " is lower than minimum %" PRIu64".\n",
761 data_key->key_version, (min_version >> 16));
Randall Spanglerae87b922011-05-12 13:27:28 -0700762 goto verify_exit;
763 }
764
Randall Spangler7d6898d2010-06-11 09:22:13 -0700765 rsa = PublicKeyToRSA(&key_block->data_key);
766 if (!rsa) {
Randall Spangler32a65262011-06-27 10:49:11 -0700767 VbExError("Error parsing data key.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700768 goto verify_exit;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700769 }
770
771 /* Verify preamble */
vbendebb2b0fcc2010-07-15 15:09:47 -0700772 preamble = bp->preamble;
Randall Spangler87c13d82010-07-19 10:35:40 -0700773 if (0 != VerifyKernelPreamble(
Bill Richardson4f36ef32010-08-09 17:50:14 -0700774 preamble, bp->blob_size - key_block->key_block_size, rsa)) {
Randall Spangler32a65262011-06-27 10:49:11 -0700775 VbExError("Error verifying preamble.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700776 goto verify_exit;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700777 }
778 now += preamble->preamble_size;
779
780 printf("Preamble:\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700781 printf(" Size: 0x%" PRIx64 "\n", preamble->preamble_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700782 printf(" Header version: %" PRIu32 ".%" PRIu32"\n",
783 preamble->header_version_major, preamble->header_version_minor);
784 printf(" Kernel version: %" PRIu64 "\n", preamble->kernel_version);
Bill Richardson249677d2010-06-23 11:16:37 -0700785 printf(" Body load address: 0x%" PRIx64 "\n", preamble->body_load_address);
786 printf(" Body size: 0x%" PRIx64 "\n",
Randall Spangler7d6898d2010-06-11 09:22:13 -0700787 preamble->body_signature.data_size);
Randall Spangler87c13d82010-07-19 10:35:40 -0700788 printf(" Bootloader address: 0x%" PRIx64 "\n",
789 preamble->bootloader_address);
Bill Richardson249677d2010-06-23 11:16:37 -0700790 printf(" Bootloader size: 0x%" PRIx64 "\n", preamble->bootloader_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700791
Randall Spanglerae87b922011-05-12 13:27:28 -0700792 if (preamble->kernel_version < (min_version & 0xFFFF)) {
Randall Spangler32a65262011-06-27 10:49:11 -0700793 VbExError("Kernel version %" PRIu64 " is lower than minimum %" PRIu64 ".\n",
794 preamble->kernel_version, (min_version & 0xFFFF));
Randall Spanglerae87b922011-05-12 13:27:28 -0700795 goto verify_exit;
796 }
797
Randall Spangler7d6898d2010-06-11 09:22:13 -0700798 /* Verify body */
Randall Spangler87c13d82010-07-19 10:35:40 -0700799 if (0 != VerifyData(bp->blob, bp->blob_size, &preamble->body_signature,
800 rsa)) {
Randall Spangler32a65262011-06-27 10:49:11 -0700801 VbExError("Error verifying kernel body.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700802 goto verify_exit;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700803 }
804 printf("Body verification succeeded.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700805
806 rv = 0;
807
808 if (!verbose) {
809 goto verify_exit;
810 }
811
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800812 printf("Config:\n%s\n", BpCmdLineLocation(bp, kernel_body_load_address));
vbendebb2b0fcc2010-07-15 15:09:47 -0700813
814verify_exit:
815 FreeBlob(bp);
816 return rv;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700817}
818
819
820int main(int argc, char* argv[]) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700821 char* filename = NULL;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700822 char* oldfile = NULL;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700823 char* key_block_file = NULL;
824 char* signpubkey = NULL;
825 char* signprivate = NULL;
vbendeb00b90882010-10-21 13:46:16 -0700826 int version = -1;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700827 char* vmlinuz = NULL;
828 char* bootloader = NULL;
829 char* config_file = NULL;
Che-Liang Chiou03762032011-02-22 11:16:51 +0800830 int arch = ARCH_X86;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700831 int vblockonly = 0;
vbendebb2b0fcc2010-07-15 15:09:47 -0700832 int verbose = 0;
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800833 uint64_t kernel_body_load_address = CROS_32BIT_ENTRY_ADDR;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700834 uint64_t pad = DEFAULT_PADDING;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700835 int mode = 0;
836 int parse_error = 0;
Randall Spanglerae87b922011-05-12 13:27:28 -0700837 uint64_t min_version = 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700838 char* e;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700839 int i,r;
840 blob_t *bp;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700841
Bill Richardsona08b5c92010-06-30 21:59:43 -0700842
843 char *progname = strrchr(argv[0], '/');
844 if (progname)
845 progname++;
846 else
847 progname = argv[0];
848
vbendebb2b0fcc2010-07-15 15:09:47 -0700849 while (((i = getopt_long(argc, argv, ":", long_opts, NULL)) != -1) &&
850 !parse_error) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700851 switch (i) {
vbendebb2b0fcc2010-07-15 15:09:47 -0700852 default:
Randall Spangler7d6898d2010-06-11 09:22:13 -0700853 case '?':
854 /* Unhandled option */
Randall Spangler7d6898d2010-06-11 09:22:13 -0700855 parse_error = 1;
856 break;
857
Bill Richardson4f36ef32010-08-09 17:50:14 -0700858 case 0:
859 /* silently handled option */
860 break;
861
Randall Spangler7d6898d2010-06-11 09:22:13 -0700862 case OPT_MODE_PACK:
Bill Richardsona08b5c92010-06-30 21:59:43 -0700863 case OPT_MODE_REPACK:
Randall Spangler7d6898d2010-06-11 09:22:13 -0700864 case OPT_MODE_VERIFY:
vbendebb2b0fcc2010-07-15 15:09:47 -0700865 if (mode && (mode != i)) {
866 fprintf(stderr, "Only single mode can be specified\n");
867 parse_error = 1;
868 break;
869 }
Randall Spangler7d6898d2010-06-11 09:22:13 -0700870 mode = i;
871 filename = optarg;
872 break;
873
Che-Liang Chiou03762032011-02-22 11:16:51 +0800874 case OPT_ARCH:
Sonny Rao06edfc62011-09-16 11:52:22 -0700875 /* check the first 3 characters to also detect x86_64 */
Sonny Rao74554732011-10-11 18:36:19 -0700876 if ((!strncasecmp(optarg, "x86", 3)) ||
877 (!strcasecmp(optarg, "amd64")))
Che-Liang Chiou03762032011-02-22 11:16:51 +0800878 arch = ARCH_X86;
879 else if (!strcasecmp(optarg, "arm"))
880 arch = ARCH_ARM;
881 else {
882 fprintf(stderr, "Unknown architecture string: %s\n", optarg);
883 parse_error = 1;
884 }
885 break;
886
Bill Richardsona08b5c92010-06-30 21:59:43 -0700887 case OPT_OLDBLOB:
888 oldfile = optarg;
889 break;
890
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800891 case OPT_KLOADADDR:
892 kernel_body_load_address = strtoul(optarg, &e, 0);
893 if (!*optarg || (e && *e)) {
894 fprintf(stderr, "Invalid --kloadaddr\n");
895 parse_error = 1;
896 }
897 break;
898
Randall Spangler7d6898d2010-06-11 09:22:13 -0700899 case OPT_KEYBLOCK:
900 key_block_file = optarg;
901 break;
902
903 case OPT_SIGNPUBKEY:
904 signpubkey = optarg;
905 break;
906
907 case OPT_SIGNPRIVATE:
908 signprivate = optarg;
909 break;
910
911 case OPT_VMLINUZ:
912 vmlinuz = optarg;
913 break;
914
915 case OPT_BOOTLOADER:
916 bootloader = optarg;
917 break;
918
919 case OPT_CONFIG:
920 config_file = optarg;
921 break;
922
Bill Richardsona08b5c92010-06-30 21:59:43 -0700923 case OPT_VBLOCKONLY:
924 vblockonly = 1;
925 break;
926
Randall Spangler7d6898d2010-06-11 09:22:13 -0700927 case OPT_VERSION:
928 version = strtoul(optarg, &e, 0);
929 if (!*optarg || (e && *e)) {
Bill Richardsona08b5c92010-06-30 21:59:43 -0700930 fprintf(stderr, "Invalid --version\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700931 parse_error = 1;
932 }
933 break;
934
Randall Spanglerae87b922011-05-12 13:27:28 -0700935 case OPT_MINVERSION:
936 min_version = strtoul(optarg, &e, 0);
937 if (!*optarg || (e && *e)) {
938 fprintf(stderr, "Invalid --minversion\n");
939 parse_error = 1;
940 }
941 break;
942
Randall Spangler7d6898d2010-06-11 09:22:13 -0700943 case OPT_PAD:
944 pad = strtoul(optarg, &e, 0);
945 if (!*optarg || (e && *e)) {
Bill Richardsona08b5c92010-06-30 21:59:43 -0700946 fprintf(stderr, "Invalid --pad\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700947 parse_error = 1;
948 }
949 break;
vbendebb2b0fcc2010-07-15 15:09:47 -0700950
951 case OPT_VERBOSE:
952 verbose = 1;
953 break;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700954 }
955 }
956
957 if (parse_error)
Bill Richardsona08b5c92010-06-30 21:59:43 -0700958 return PrintHelp(progname);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700959
960 switch(mode) {
961 case OPT_MODE_PACK:
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800962 bp = NewBlob(version, vmlinuz, bootloader, config_file, arch,
963 kernel_body_load_address);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700964 if (!bp)
965 return 1;
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800966 r = Pack(filename, key_block_file, signprivate, bp, pad, vblockonly,
967 kernel_body_load_address);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700968 FreeBlob(bp);
969 return r;
970
971 case OPT_MODE_REPACK:
vbendeb00b90882010-10-21 13:46:16 -0700972 if (!config_file && !key_block_file && (version<0)) {
vbendebb2b0fcc2010-07-15 15:09:47 -0700973 fprintf(stderr,
vbendeb858fffb2010-10-06 09:51:44 -0700974 "You must supply at least one of "
975 "--config, --keyblock or --version\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700976 return 1;
977 }
978
Randall Spangler7ecb39d2011-05-12 15:37:45 -0700979 bp = OldBlob(oldfile, pad);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700980 if (!bp)
981 return 1;
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800982 r = ReplaceConfig(bp, config_file, kernel_body_load_address);
vbendebb2b0fcc2010-07-15 15:09:47 -0700983 if (!r) {
vbendeb00b90882010-10-21 13:46:16 -0700984 if (version >= 0) {
Che-Liang Chiou03762032011-02-22 11:16:51 +0800985 bp->kernel_version = (uint64_t) version;
vbendeb858fffb2010-10-06 09:51:44 -0700986 }
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800987 r = Pack(filename, key_block_file, signprivate, bp, pad, vblockonly,
988 kernel_body_load_address);
vbendebb2b0fcc2010-07-15 15:09:47 -0700989 }
Bill Richardsona08b5c92010-06-30 21:59:43 -0700990 FreeBlob(bp);
991 return r;
992
Randall Spangler7d6898d2010-06-11 09:22:13 -0700993 case OPT_MODE_VERIFY:
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800994 return Verify(filename, signpubkey, verbose, key_block_file,
Randall Spangler7ecb39d2011-05-12 15:37:45 -0700995 kernel_body_load_address, min_version, pad);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700996
Randall Spangler7d6898d2010-06-11 09:22:13 -0700997 default:
Bill Richardsona08b5c92010-06-30 21:59:43 -0700998 fprintf(stderr,
999 "You must specify a mode: --pack, --repack or --verify\n");
1000 return PrintHelp(progname);
Randall Spangler7d6898d2010-06-11 09:22:13 -07001001 }
1002}