blob: 80197194b83241f4ed32bdd42c145ea221ae140a [file] [log] [blame]
Randall Spangler7d6898d2010-06-11 09:22:13 -07001/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2 * 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,
Bill Richardsona08b5c92010-06-30 21:59:43 -070036 OPT_OLDBLOB,
Randall Spangler7d6898d2010-06-11 09:22:13 -070037 OPT_KEYBLOCK,
38 OPT_SIGNPUBKEY,
39 OPT_SIGNPRIVATE,
40 OPT_VERSION,
41 OPT_VMLINUZ,
42 OPT_BOOTLOADER,
43 OPT_CONFIG,
Bill Richardsona08b5c92010-06-30 21:59:43 -070044 OPT_VBLOCKONLY,
Randall Spangler7d6898d2010-06-11 09:22:13 -070045 OPT_PAD,
vbendebb2b0fcc2010-07-15 15:09:47 -070046 OPT_VERBOSE,
Randall Spangler7d6898d2010-06-11 09:22:13 -070047};
48
49static struct option long_opts[] = {
50 {"pack", 1, 0, OPT_MODE_PACK },
Bill Richardsona08b5c92010-06-30 21:59:43 -070051 {"repack", 1, 0, OPT_MODE_REPACK },
Randall Spangler7d6898d2010-06-11 09:22:13 -070052 {"verify", 1, 0, OPT_MODE_VERIFY },
Bill Richardsona08b5c92010-06-30 21:59:43 -070053 {"oldblob", 1, 0, OPT_OLDBLOB },
Randall Spangler7d6898d2010-06-11 09:22:13 -070054 {"keyblock", 1, 0, OPT_KEYBLOCK },
55 {"signpubkey", 1, 0, OPT_SIGNPUBKEY },
56 {"signprivate", 1, 0, OPT_SIGNPRIVATE },
57 {"version", 1, 0, OPT_VERSION },
58 {"vmlinuz", 1, 0, OPT_VMLINUZ },
59 {"bootloader", 1, 0, OPT_BOOTLOADER },
60 {"config", 1, 0, OPT_CONFIG },
Bill Richardsona08b5c92010-06-30 21:59:43 -070061 {"vblockonly", 0, 0, OPT_VBLOCKONLY },
Randall Spangler7d6898d2010-06-11 09:22:13 -070062 {"pad", 1, 0, OPT_PAD },
vbendebb2b0fcc2010-07-15 15:09:47 -070063 {"verbose", 0, 0, OPT_VERBOSE },
Bill Richardson249677d2010-06-23 11:16:37 -070064 {"debug", 0, &opt_debug, 1 },
Randall Spangler7d6898d2010-06-11 09:22:13 -070065 {NULL, 0, 0, 0}
66};
67
68
69/* Print help and return error */
Bill Richardsona08b5c92010-06-30 21:59:43 -070070static int PrintHelp(char *progname) {
71 fprintf(stderr,
72 "This program creates, signs, and verifies the kernel blob\n");
73 fprintf(stderr,
74 "\n"
75 "Usage: %s --pack <file> [PARAMETERS]\n"
76 "\n"
77 " Required parameters:\n"
78 " --keyblock <file> Key block in .keyblock format\n"
Bill Richardson4f36ef32010-08-09 17:50:14 -070079 " --signprivate <file>"
80 " Private key to sign kernel data, in .vbprivk format\n"
Bill Richardsona08b5c92010-06-30 21:59:43 -070081 " --version <number> Kernel version\n"
82 " --vmlinuz <file> Linux kernel bzImage file\n"
83 " --bootloader <file> Bootloader stub\n"
vbendebb2b0fcc2010-07-15 15:09:47 -070084 " --config <file> Command line file\n"
Bill Richardsona08b5c92010-06-30 21:59:43 -070085 "\n"
86 " Optional:\n"
87 " --pad <number> Verification padding size in bytes\n"
88 " --vblockonly Emit just the verification blob\n",
89 progname);
90 fprintf(stderr,
91 "\nOR\n\n"
92 "Usage: %s --repack <file> [PARAMETERS]\n"
93 "\n"
vbendebb2b0fcc2010-07-15 15:09:47 -070094 " Required parameters (of --keyblock and --config at least "
95 "one is required):\n"
Bill Richardsona08b5c92010-06-30 21:59:43 -070096 " --keyblock <file> Key block in .keyblock format\n"
Bill Richardson4f36ef32010-08-09 17:50:14 -070097 " --signprivate <file>"
98 " Private key to sign kernel data, in .vbprivk format\n"
Bill Richardsona08b5c92010-06-30 21:59:43 -070099 " --oldblob <file> Previously packed kernel blob\n"
vbendebb2b0fcc2010-07-15 15:09:47 -0700100 " --config <file> New command line file\n"
Bill Richardsona08b5c92010-06-30 21:59:43 -0700101 "\n"
102 " Optional:\n"
103 " --pad <number> Verification padding size in bytes\n"
104 " --vblockonly Emit just the verification blob\n",
105 progname);
106 fprintf(stderr,
107 "\nOR\n\n"
108 "Usage: %s --verify <file> [PARAMETERS]\n"
109 "\n"
vbendebb2b0fcc2010-07-15 15:09:47 -0700110 " Optional:\n"
Bill Richardson4f36ef32010-08-09 17:50:14 -0700111 " --signpubkey <file>"
112 " Public key to verify kernel keyblock, in .vbpubk format\n"
vbendebb2b0fcc2010-07-15 15:09:47 -0700113 " --verbose Print a more detailed report\n"
Bill Richardsona08b5c92010-06-30 21:59:43 -0700114 "\n",
115 progname);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700116 return 1;
117}
118
Bill Richardson249677d2010-06-23 11:16:37 -0700119static void Debug(const char *format, ...) {
120 if (!opt_debug)
121 return;
122
123 va_list ap;
124 va_start(ap, format);
125 fprintf(stderr, "DEBUG: ");
126 vfprintf(stderr, format, ap);
127 va_end(ap);
128}
129
Randall Spangler7d6898d2010-06-11 09:22:13 -0700130
131/* Return the smallest integral multiple of [alignment] that is equal
132 * to or greater than [val]. Used to determine the number of
133 * pages/sectors/blocks/whatever needed to contain [val]
134 * items/bytes/etc. */
135static uint64_t roundup(uint64_t val, uint64_t alignment) {
136 uint64_t rem = val % alignment;
137 if ( rem )
138 return val + (alignment - rem);
139 return val;
140}
141
142
143/* Match regexp /\b--\b/ to delimit the start of the kernel commandline. If we
144 * don't find one, we'll use the whole thing. */
145static unsigned int find_cmdline_start(char *input, unsigned int max_len) {
146 int start = 0;
147 int i;
148 for(i = 0; i < max_len - 1 && input[i]; i++) {
149 if ('-' == input[i] && '-' == input[i + 1]) { /* found a "--" */
150 if ((i == 0 || ' ' == input[i - 1]) && /* nothing before it */
151 (i + 2 >= max_len || ' ' == input[i+2])) { /* nothing after it */
152 start = i+2; /* note: hope there's a trailing '\0' */
153 break;
154 }
155 }
156 }
157 while(' ' == input[start]) /* skip leading spaces */
158 start++;
159
160 return start;
161}
162
163
Bill Richardsona08b5c92010-06-30 21:59:43 -0700164typedef struct blob_s {
165 /* Stuff needed by VbKernelPreambleHeader */
166 uint64_t kernel_version;
167 uint64_t bootloader_address;
168 uint64_t bootloader_size;
169 /* Raw kernel blob data */
170 uint64_t blob_size;
171 uint8_t *blob;
vbendebb2b0fcc2010-07-15 15:09:47 -0700172
173 /* these fields are not always initialized */
174 VbKernelPreambleHeader* preamble;
175 VbKeyBlockHeader* key_block;
176 uint8_t *buf;
177
Bill Richardsona08b5c92010-06-30 21:59:43 -0700178} blob_t;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700179
vbendebb2b0fcc2010-07-15 15:09:47 -0700180/* Given a blob return the location of the kernel command line buffer. */
181static char* BpCmdLineLocation(blob_t *bp)
182{
183 return (char*)(bp->blob + bp->bootloader_address - CROS_32BIT_ENTRY_ADDR -
184 CROS_CONFIG_SIZE - CROS_PARAMS_SIZE);
185}
Bill Richardsona08b5c92010-06-30 21:59:43 -0700186
187static void FreeBlob(blob_t *bp) {
188 if (bp) {
189 if (bp->blob)
190 Free(bp->blob);
vbendebb2b0fcc2010-07-15 15:09:47 -0700191 if (bp->buf)
192 Free(bp->buf);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700193 Free(bp);
194 }
195}
196
vbendebb2b0fcc2010-07-15 15:09:47 -0700197/*
198 * Read the kernel command line from a file. Get rid of \n characters along
199 * the way and verify that the line fits into a 4K buffer.
200 *
201 * Return the buffer contaning the line on success (and set the line length
202 * using the passed in parameter), or NULL in case something goes wrong.
203 */
204static uint8_t* ReadConfigFile(const char* config_file, uint64_t* config_size)
205{
206 uint8_t* config_buf;
207 int ii;
208
209 config_buf = ReadFile(config_file, config_size);
210 Debug(" config file size=0x%" PRIx64 "\n", *config_size);
211 if (CROS_CONFIG_SIZE <= *config_size) { /* need room for trailing '\0' */
212 error("Config file %s is too large (>= %d bytes)\n",
213 config_file, CROS_CONFIG_SIZE);
214 return NULL;
215 }
216
217 /* Replace newlines with spaces */
218 for (ii = 0; ii < *config_size; ii++) {
219 if ('\n' == config_buf[ii]) {
220 config_buf[ii] = ' ';
221 }
222 }
223 return config_buf;
224}
225
Bill Richardsona08b5c92010-06-30 21:59:43 -0700226/* Create a blob from its components */
227static blob_t *NewBlob(uint64_t version,
228 const char* vmlinuz,
229 const char* bootloader_file,
230 const char* config_file) {
231 blob_t *bp;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700232 struct linux_kernel_header *lh = 0;
233 struct linux_kernel_params *params = 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700234 uint8_t* config_buf;
235 uint64_t config_size;
236 uint8_t* bootloader_buf;
237 uint64_t bootloader_size;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700238 uint8_t* kernel_buf;
239 uint64_t kernel_size;
240 uint64_t kernel32_start = 0;
241 uint64_t kernel32_size = 0;
242 uint32_t cmdline_addr;
243 uint8_t* blob = NULL;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700244 uint64_t now = 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700245
Randall Spangler7d6898d2010-06-11 09:22:13 -0700246 if (!vmlinuz || !bootloader_file || !config_file) {
247 error("Must specify all input files\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700248 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700249 }
250
Bill Richardsona08b5c92010-06-30 21:59:43 -0700251 bp = (blob_t *)Malloc(sizeof(blob_t));
252 if (!bp) {
253 error("Couldn't allocate bytes for blob_t.\n");
254 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700255 }
vbendebb2b0fcc2010-07-15 15:09:47 -0700256
257 Memset(bp, 0, sizeof(*bp));
Bill Richardsona08b5c92010-06-30 21:59:43 -0700258 bp->kernel_version = version;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700259
260 /* Read the config file */
Bill Richardson249677d2010-06-23 11:16:37 -0700261 Debug("Reading %s\n", config_file);
vbendebb2b0fcc2010-07-15 15:09:47 -0700262 config_buf = ReadConfigFile(config_file, &config_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700263 if (!config_buf)
Bill Richardsona08b5c92010-06-30 21:59:43 -0700264 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700265
266 /* Read the bootloader */
Bill Richardson249677d2010-06-23 11:16:37 -0700267 Debug("Reading %s\n", bootloader_file);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700268 bootloader_buf = ReadFile(bootloader_file, &bootloader_size);
269 if (!bootloader_buf)
Bill Richardsona08b5c92010-06-30 21:59:43 -0700270 return 0;
Bill Richardson249677d2010-06-23 11:16:37 -0700271 Debug(" bootloader file size=0x%" PRIx64 "\n", bootloader_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700272
273 /* Read the kernel */
Bill Richardson249677d2010-06-23 11:16:37 -0700274 Debug("Reading %s\n", vmlinuz);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700275 kernel_buf = ReadFile(vmlinuz, &kernel_size);
276 if (!kernel_buf)
Bill Richardsona08b5c92010-06-30 21:59:43 -0700277 return 0;
Bill Richardson249677d2010-06-23 11:16:37 -0700278 Debug(" kernel file size=0x%" PRIx64 "\n", kernel_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700279 if (!kernel_size) {
280 error("Empty kernel file\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700281 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700282 }
283
284 /* The first part of vmlinuz is a header, followed by a real-mode
285 * boot stub. We only want the 32-bit part. */
286 lh = (struct linux_kernel_header *)kernel_buf;
287 kernel32_start = (lh->setup_sects + 1) << 9;
288 if (kernel32_start >= kernel_size) {
289 error("Malformed kernel\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700290 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700291 }
292 kernel32_size = kernel_size - kernel32_start;
Bill Richardson249677d2010-06-23 11:16:37 -0700293 Debug(" kernel32_start=0x%" PRIx64 "\n", kernel32_start);
294 Debug(" kernel32_size=0x%" PRIx64 "\n", kernel32_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700295
296 /* Allocate and zero the blob we need. */
Bill Richardsona08b5c92010-06-30 21:59:43 -0700297 bp->blob_size = roundup(kernel32_size, CROS_ALIGN) +
Randall Spangler7d6898d2010-06-11 09:22:13 -0700298 CROS_CONFIG_SIZE +
299 CROS_PARAMS_SIZE +
300 roundup(bootloader_size, CROS_ALIGN);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700301 blob = (uint8_t *)Malloc(bp->blob_size);
302 Debug("blob_size=0x%" PRIx64 "\n", bp->blob_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700303 if (!blob) {
Bill Richardsona08b5c92010-06-30 21:59:43 -0700304 error("Couldn't allocate %ld bytes.\n", bp->blob_size);
305 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700306 }
Bill Richardsona08b5c92010-06-30 21:59:43 -0700307 Memset(blob, 0, bp->blob_size);
308 bp->blob = blob;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700309
310 /* Copy the 32-bit kernel. */
Bill Richardson249677d2010-06-23 11:16:37 -0700311 Debug("kernel goes at blob+=0x%" PRIx64 "\n", now);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700312 if (kernel32_size)
313 Memcpy(blob + now, kernel_buf + kernel32_start, kernel32_size);
314 now += roundup(now + kernel32_size, CROS_ALIGN);
315
Bill Richardson249677d2010-06-23 11:16:37 -0700316 Debug("config goes at blob+0x%" PRIx64 "\n", now);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700317 /* Find the load address of the commandline. We'll need it later. */
318 cmdline_addr = CROS_32BIT_ENTRY_ADDR + now +
319 find_cmdline_start((char *)config_buf, config_size);
Bill Richardson249677d2010-06-23 11:16:37 -0700320 Debug(" cmdline_addr=0x%" PRIx64 "\n", cmdline_addr);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700321
322 /* Copy the config. */
323 if (config_size)
324 Memcpy(blob + now, config_buf, config_size);
325 now += CROS_CONFIG_SIZE;
326
327 /* The zeropage data is next. Overlay the linux_kernel_header onto it, and
328 * tweak a few fields. */
Bill Richardson249677d2010-06-23 11:16:37 -0700329 Debug("params goes at blob+=0x%" PRIx64 "\n", now);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700330 params = (struct linux_kernel_params *)(blob + now);
331 Memcpy(&(params->setup_sects), &(lh->setup_sects),
332 sizeof(*lh) - offsetof(struct linux_kernel_header, setup_sects));
333 params->boot_flag = 0;
334 params->ramdisk_image = 0; /* we don't support initrd */
335 params->ramdisk_size = 0;
336 params->type_of_loader = 0xff;
337 params->cmd_line_ptr = cmdline_addr;
Che-Liang Chiou475bf442010-08-23 11:20:44 +0800338 /* A fake e820 memory map with 2 entries */
339 params->n_e820_entry = 2;
340 params->e820_entries[0].start_addr = 0x00000000;
341 params->e820_entries[0].segment_size = 0x00001000;
342 params->e820_entries[0].segment_type = E820_TYPE_RAM;
343 params->e820_entries[1].start_addr = 0xfffff000;
344 params->e820_entries[1].segment_size = 0x00001000;
345 params->e820_entries[1].segment_type = E820_TYPE_RESERVED;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700346 now += CROS_PARAMS_SIZE;
347
348 /* Finally, append the bootloader. Remember where it will load in
349 * memory, too. */
Bill Richardson249677d2010-06-23 11:16:37 -0700350 Debug("bootloader goes at blob+=0x%" PRIx64 "\n", now);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700351 bp->bootloader_address = CROS_32BIT_ENTRY_ADDR + now;
352 bp->bootloader_size = roundup(bootloader_size, CROS_ALIGN);
353 Debug(" bootloader_address=0x%" PRIx64 "\n", bp->bootloader_address);
354 Debug(" bootloader_size=0x%" PRIx64 "\n", bp->bootloader_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700355 if (bootloader_size)
356 Memcpy(blob + now, bootloader_buf, bootloader_size);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700357 now += bp->bootloader_size;
Bill Richardson249677d2010-06-23 11:16:37 -0700358 Debug("end of blob is 0x%" PRIx64 "\n", now);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700359
360 /* Free input buffers */
361 Free(kernel_buf);
362 Free(config_buf);
363 Free(bootloader_buf);
364
Bill Richardsona08b5c92010-06-30 21:59:43 -0700365 /* Success */
366 return bp;
367}
368
369
370/* Pull the blob_t stuff out of a prepacked kernel blob file */
371static blob_t *OldBlob(const char* filename) {
vbendebb2b0fcc2010-07-15 15:09:47 -0700372 FILE* fp = NULL;
373 blob_t *bp = NULL;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700374 struct stat statbuf;
375 VbKeyBlockHeader* key_block;
376 VbKernelPreambleHeader* preamble;
377 uint64_t now = 0;
vbendebb2b0fcc2010-07-15 15:09:47 -0700378 uint8_t* buf = NULL;
379 int ret_error = 1;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700380
381 if (!filename) {
382 error("Must specify prepacked blob to read\n");
383 return 0;
384 }
385
386 if (0 != stat(filename, &statbuf)) {
387 error("unable to stat %s: %s\n", filename, strerror(errno));
388 return 0;
389 }
390
391 Debug("%s size is 0x%" PRIx64 "\n", filename, statbuf.st_size);
392 if (statbuf.st_size < DEFAULT_PADDING) {
393 error("%s is too small to be a valid kernel blob\n");
394 return 0;
395 }
396
397 Debug("Reading %s\n", filename);
398 fp = fopen(filename, "rb");
399 if (!fp) {
400 error("Unable to open file %s: %s\n", filename, strerror(errno));
401 return 0;
402 }
403
vbendebb2b0fcc2010-07-15 15:09:47 -0700404 buf = Malloc(DEFAULT_PADDING);
405 if (!buf) {
406 error("Unable to allocate padding\n");
407 goto unwind_oldblob;
408 }
409
410 if (1 != fread(buf, DEFAULT_PADDING, 1, fp)) {
Bill Richardsona08b5c92010-06-30 21:59:43 -0700411 error("Unable to read header from %s: %s\n", filename, strerror(errno));
vbendebb2b0fcc2010-07-15 15:09:47 -0700412 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700413 }
414
415 /* Skip the key block */
416 key_block = (VbKeyBlockHeader*)buf;
417 Debug("Keyblock is 0x%" PRIx64 " bytes\n", key_block->key_block_size);
418 now += key_block->key_block_size;
419 if (now > statbuf.st_size) {
420 error("key_block_size advances past the end of the blob\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700421 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700422 }
423
424 /* Skip the preamble */
425 preamble = (VbKernelPreambleHeader*)(buf + now);
426 Debug("Preamble is 0x%" PRIx64 " bytes\n", preamble->preamble_size);
427 now += preamble->preamble_size;
428 if (now > statbuf.st_size) {
429 error("preamble_size advances past the end of the blob\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700430 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700431 }
432
433 /* Go find the kernel blob */
434 Debug("kernel blob is at offset 0x%" PRIx64 "\n", now);
435 if (0 != fseek(fp, now, SEEK_SET)) {
436 error("Unable to seek to 0x%" PRIx64 " in %s: %s\n", now, filename,
437 strerror(errno));
vbendebb2b0fcc2010-07-15 15:09:47 -0700438 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700439 }
440
441 /* Remember what we've got */
442 bp = (blob_t *)Malloc(sizeof(blob_t));
443 if (!bp) {
444 error("Couldn't allocate bytes for blob_t.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700445 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700446 }
447
vbendebb2b0fcc2010-07-15 15:09:47 -0700448 bp->buf = buf;
449 bp->key_block = key_block;
450 bp->preamble = preamble;
451
Bill Richardsona08b5c92010-06-30 21:59:43 -0700452 bp->kernel_version = preamble->kernel_version;
453 bp->bootloader_address = preamble->bootloader_address;
454 bp->bootloader_size = preamble->bootloader_size;
455 bp->blob_size = preamble->body_signature.data_size;
456
457 Debug(" kernel_version = %d\n", bp->kernel_version);
458 Debug(" bootloader_address = 0x%" PRIx64 "\n", bp->bootloader_address);
459 Debug(" bootloader_size = 0x%" PRIx64 "\n", bp->bootloader_size);
460 Debug(" blob_size = 0x%" PRIx64 "\n", bp->blob_size);
461
462 bp->blob = (uint8_t *)Malloc(bp->blob_size);
463 if (!bp->blob) {
464 error("Couldn't allocate 0x%" PRIx64 " bytes for blob_t.\n", bp->blob_size);
vbendebb2b0fcc2010-07-15 15:09:47 -0700465 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700466 }
467
468 /* read it in */
469 if (1 != fread(bp->blob, bp->blob_size, 1, fp)) {
470 error("Unable to read kernel blob from %s: %s\n", filename, strerror(errno));
vbendebb2b0fcc2010-07-15 15:09:47 -0700471 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700472 }
473
vbendebb2b0fcc2010-07-15 15:09:47 -0700474 ret_error = 0;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700475
vbendebb2b0fcc2010-07-15 15:09:47 -0700476 /* done */
477unwind_oldblob:
478 fclose(fp);
479 if (ret_error) {
480 if (bp) {
481 FreeBlob(bp);
482 bp = NULL;
483 } else if (buf) {
484 Free(buf);
485 }
486 }
Bill Richardsona08b5c92010-06-30 21:59:43 -0700487 return bp;
488}
489
490
491/* Pack a .kernel */
492static int Pack(const char* outfile, const char* keyblock_file,
493 const char* signprivate, blob_t *bp, uint64_t pad,
494 int vblockonly) {
495 VbPrivateKey* signing_key;
496 VbSignature* body_sig;
497 VbKernelPreambleHeader* preamble;
498 VbKeyBlockHeader* key_block;
499 uint64_t key_block_size;
500 FILE* f;
501 uint64_t i;
502
503 if (!outfile) {
504 error("Must specify output filename\n");
505 return 1;
506 }
vbendebb2b0fcc2010-07-15 15:09:47 -0700507 if ((!keyblock_file && !bp->key_block) || !signprivate) {
Bill Richardsona08b5c92010-06-30 21:59:43 -0700508 error("Must specify all keys\n");
509 return 1;
510 }
511 if (!bp) {
512 error("Refusing to pack invalid kernel blob\n");
513 return 1;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700514 }
vbendebb2b0fcc2010-07-15 15:09:47 -0700515
516 /* Get the key block and read the private key. */
517 if (keyblock_file) {
518 key_block = (VbKeyBlockHeader*)ReadFile(keyblock_file, &key_block_size);
519 if (!key_block) {
520 error("Error reading key block.\n");
521 return 1;
522 }
523 } else {
524 key_block = bp->key_block;
525 key_block_size = key_block->key_block_size;
526 }
527
Bill Richardsona08b5c92010-06-30 21:59:43 -0700528 if (pad < key_block->key_block_size) {
529 error("Pad too small\n");
530 return 1;
531 }
532
Bill Richardsonabf05502010-07-01 10:22:06 -0700533 signing_key = PrivateKeyRead(signprivate);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700534 if (!signing_key) {
535 error("Error reading signing key.\n");
536 return 1;
537 }
538
Randall Spangler7d6898d2010-06-11 09:22:13 -0700539 /* Sign the kernel data */
Bill Richardsona08b5c92010-06-30 21:59:43 -0700540 body_sig = CalculateSignature(bp->blob, bp->blob_size, signing_key);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700541 if (!body_sig) {
542 error("Error calculating body signature\n");
543 return 1;
544 }
545
546 /* Create preamble */
Bill Richardsona08b5c92010-06-30 21:59:43 -0700547 preamble = CreateKernelPreamble(bp->kernel_version,
Randall Spangler7d6898d2010-06-11 09:22:13 -0700548 CROS_32BIT_ENTRY_ADDR,
Bill Richardsona08b5c92010-06-30 21:59:43 -0700549 bp->bootloader_address,
550 bp->bootloader_size,
Randall Spangler7d6898d2010-06-11 09:22:13 -0700551 body_sig,
552 pad - key_block_size,
553 signing_key);
554 if (!preamble) {
555 error("Error creating preamble.\n");
556 return 1;
557 }
558
559 /* Write the output file */
Bill Richardson249677d2010-06-23 11:16:37 -0700560 Debug("writing %s...\n", outfile);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700561 f = fopen(outfile, "wb");
562 if (!f) {
563 error("Can't open output file %s\n", outfile);
564 return 1;
565 }
Bill Richardson249677d2010-06-23 11:16:37 -0700566 Debug("0x%" PRIx64 " bytes of key_block\n", key_block_size);
567 Debug("0x%" PRIx64 " bytes of preamble\n", preamble->preamble_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700568 i = ((1 != fwrite(key_block, key_block_size, 1, f)) ||
Bill Richardsona08b5c92010-06-30 21:59:43 -0700569 (1 != fwrite(preamble, preamble->preamble_size, 1, f)));
Randall Spangler7d6898d2010-06-11 09:22:13 -0700570 if (i) {
571 error("Can't write output file %s\n", outfile);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700572 fclose(f);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700573 unlink(outfile);
574 return 1;
575 }
576
Bill Richardsona08b5c92010-06-30 21:59:43 -0700577 if (!vblockonly) {
578 Debug("0x%" PRIx64 " bytes of blob\n", bp->blob_size);
579 i = (1 != fwrite(bp->blob, bp->blob_size, 1, f));
580 if (i) {
581 error("Can't write output file %s\n", outfile);
582 fclose(f);
583 unlink(outfile);
584 return 1;
585 }
586 }
587
588 fclose(f);
589
Randall Spangler7d6898d2010-06-11 09:22:13 -0700590 /* Success */
591 return 0;
592}
593
vbendebb2b0fcc2010-07-15 15:09:47 -0700594/*
595 * Replace kernel command line in a blob representing a kernel.
596 */
597static int ReplaceConfig(blob_t* bp, const char* config_file)
598{
599 uint8_t* new_conf;
600 uint64_t config_size;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700601
vbendebb2b0fcc2010-07-15 15:09:47 -0700602 if (!config_file) {
603 return 0;
604 }
605
606 new_conf = ReadConfigFile(config_file, &config_size);
607 if (!new_conf) {
608 return 1;
609 }
610
611 /* fill the config buffer with zeros */
612 Memset(BpCmdLineLocation(bp), 0, CROS_CONFIG_SIZE);
613 Memcpy(BpCmdLineLocation(bp), new_conf, config_size);
614 Free(new_conf);
615 return 0;
616}
617
618static int Verify(const char* infile, const char* signpubkey, int verbose) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700619
620 VbKeyBlockHeader* key_block;
621 VbKernelPreambleHeader* preamble;
622 VbPublicKey* data_key;
Bill Richardson4f36ef32010-08-09 17:50:14 -0700623 VbPublicKey* sign_key = NULL;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700624 RSAPublicKey* rsa;
vbendebb2b0fcc2010-07-15 15:09:47 -0700625 blob_t* bp;
626 uint64_t now;
627 int rv = 1;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700628
Bill Richardson4f36ef32010-08-09 17:50:14 -0700629 if (!infile) {
630 error("Must specify filename\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700631 return 1;
632 }
633
634 /* Read public signing key */
Bill Richardson4f36ef32010-08-09 17:50:14 -0700635 if (signpubkey) {
636 sign_key = PublicKeyRead(signpubkey);
637 if (!sign_key) {
638 error("Error reading signpubkey.\n");
639 return 1;
640 }
Randall Spangler7d6898d2010-06-11 09:22:13 -0700641 }
642
643 /* Read blob */
vbendebb2b0fcc2010-07-15 15:09:47 -0700644 bp = OldBlob(infile);
645 if (!bp) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700646 error("Error reading input file\n");
647 return 1;
648 }
649
650 /* Verify key block */
vbendebb2b0fcc2010-07-15 15:09:47 -0700651 key_block = bp->key_block;
Randall Spangler138acfe2010-08-17 15:45:21 -0700652 if (0 != KeyBlockVerify(key_block, bp->blob_size, sign_key,
653 (sign_key ? 0 : 1))) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700654 error("Error verifying key block.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700655 goto verify_exit;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700656 }
vbendebb2b0fcc2010-07-15 15:09:47 -0700657 now = key_block->key_block_size;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700658
659 printf("Key block:\n");
660 data_key = &key_block->data_key;
Bill Richardson4f36ef32010-08-09 17:50:14 -0700661 if (verbose)
662 printf(" Signature: %s\n", sign_key ? "valid" : "ignored");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700663 printf(" Size: 0x%" PRIx64 "\n", key_block->key_block_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700664 printf(" Data key algorithm: %" PRIu64 " %s\n", data_key->algorithm,
665 (data_key->algorithm < kNumAlgorithms ?
666 algo_strings[data_key->algorithm] : "(invalid)"));
667 printf(" Data key version: %" PRIu64 "\n", data_key->key_version);
668 printf(" Flags: %" PRIu64 "\n", key_block->key_block_flags);
669
670 rsa = PublicKeyToRSA(&key_block->data_key);
671 if (!rsa) {
672 error("Error parsing data key.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700673 goto verify_exit;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700674 }
675
676 /* Verify preamble */
vbendebb2b0fcc2010-07-15 15:09:47 -0700677 preamble = bp->preamble;
Randall Spangler87c13d82010-07-19 10:35:40 -0700678 if (0 != VerifyKernelPreamble(
Bill Richardson4f36ef32010-08-09 17:50:14 -0700679 preamble, bp->blob_size - key_block->key_block_size, rsa)) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700680 error("Error verifying preamble.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700681 goto verify_exit;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700682 }
683 now += preamble->preamble_size;
684
685 printf("Preamble:\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700686 printf(" Size: 0x%" PRIx64 "\n", preamble->preamble_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700687 printf(" Header version: %" PRIu32 ".%" PRIu32"\n",
688 preamble->header_version_major, preamble->header_version_minor);
689 printf(" Kernel version: %" PRIu64 "\n", preamble->kernel_version);
Bill Richardson249677d2010-06-23 11:16:37 -0700690 printf(" Body load address: 0x%" PRIx64 "\n", preamble->body_load_address);
691 printf(" Body size: 0x%" PRIx64 "\n",
Randall Spangler7d6898d2010-06-11 09:22:13 -0700692 preamble->body_signature.data_size);
Randall Spangler87c13d82010-07-19 10:35:40 -0700693 printf(" Bootloader address: 0x%" PRIx64 "\n",
694 preamble->bootloader_address);
Bill Richardson249677d2010-06-23 11:16:37 -0700695 printf(" Bootloader size: 0x%" PRIx64 "\n", preamble->bootloader_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700696
697 /* Verify body */
Randall Spangler87c13d82010-07-19 10:35:40 -0700698 if (0 != VerifyData(bp->blob, bp->blob_size, &preamble->body_signature,
699 rsa)) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700700 error("Error verifying kernel body.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700701 goto verify_exit;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700702 }
703 printf("Body verification succeeded.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700704
705 rv = 0;
706
707 if (!verbose) {
708 goto verify_exit;
709 }
710
711 printf("Config:\n%s\n", BpCmdLineLocation(bp));
712
713verify_exit:
714 FreeBlob(bp);
715 return rv;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700716}
717
718
719int main(int argc, char* argv[]) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700720 char* filename = NULL;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700721 char* oldfile = NULL;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700722 char* key_block_file = NULL;
723 char* signpubkey = NULL;
724 char* signprivate = NULL;
725 uint64_t version = 0;
726 char* vmlinuz = NULL;
727 char* bootloader = NULL;
728 char* config_file = NULL;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700729 int vblockonly = 0;
vbendebb2b0fcc2010-07-15 15:09:47 -0700730 int verbose = 0;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700731 uint64_t pad = DEFAULT_PADDING;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700732 int mode = 0;
733 int parse_error = 0;
734 char* e;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700735 int i,r;
736 blob_t *bp;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700737
Bill Richardsona08b5c92010-06-30 21:59:43 -0700738
739 char *progname = strrchr(argv[0], '/');
740 if (progname)
741 progname++;
742 else
743 progname = argv[0];
744
vbendebb2b0fcc2010-07-15 15:09:47 -0700745 while (((i = getopt_long(argc, argv, ":", long_opts, NULL)) != -1) &&
746 !parse_error) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700747 switch (i) {
vbendebb2b0fcc2010-07-15 15:09:47 -0700748 default:
Randall Spangler7d6898d2010-06-11 09:22:13 -0700749 case '?':
750 /* Unhandled option */
Randall Spangler7d6898d2010-06-11 09:22:13 -0700751 parse_error = 1;
752 break;
753
Bill Richardson4f36ef32010-08-09 17:50:14 -0700754 case 0:
755 /* silently handled option */
756 break;
757
Randall Spangler7d6898d2010-06-11 09:22:13 -0700758 case OPT_MODE_PACK:
Bill Richardsona08b5c92010-06-30 21:59:43 -0700759 case OPT_MODE_REPACK:
Randall Spangler7d6898d2010-06-11 09:22:13 -0700760 case OPT_MODE_VERIFY:
vbendebb2b0fcc2010-07-15 15:09:47 -0700761 if (mode && (mode != i)) {
762 fprintf(stderr, "Only single mode can be specified\n");
763 parse_error = 1;
764 break;
765 }
Randall Spangler7d6898d2010-06-11 09:22:13 -0700766 mode = i;
767 filename = optarg;
768 break;
769
Bill Richardsona08b5c92010-06-30 21:59:43 -0700770 case OPT_OLDBLOB:
771 oldfile = optarg;
772 break;
773
Randall Spangler7d6898d2010-06-11 09:22:13 -0700774 case OPT_KEYBLOCK:
775 key_block_file = optarg;
776 break;
777
778 case OPT_SIGNPUBKEY:
779 signpubkey = optarg;
780 break;
781
782 case OPT_SIGNPRIVATE:
783 signprivate = optarg;
784 break;
785
786 case OPT_VMLINUZ:
787 vmlinuz = optarg;
788 break;
789
790 case OPT_BOOTLOADER:
791 bootloader = optarg;
792 break;
793
794 case OPT_CONFIG:
795 config_file = optarg;
796 break;
797
Bill Richardsona08b5c92010-06-30 21:59:43 -0700798 case OPT_VBLOCKONLY:
799 vblockonly = 1;
800 break;
801
Randall Spangler7d6898d2010-06-11 09:22:13 -0700802 case OPT_VERSION:
803 version = strtoul(optarg, &e, 0);
804 if (!*optarg || (e && *e)) {
Bill Richardsona08b5c92010-06-30 21:59:43 -0700805 fprintf(stderr, "Invalid --version\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700806 parse_error = 1;
807 }
808 break;
809
810 case OPT_PAD:
811 pad = strtoul(optarg, &e, 0);
812 if (!*optarg || (e && *e)) {
Bill Richardsona08b5c92010-06-30 21:59:43 -0700813 fprintf(stderr, "Invalid --pad\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700814 parse_error = 1;
815 }
816 break;
vbendebb2b0fcc2010-07-15 15:09:47 -0700817
818 case OPT_VERBOSE:
819 verbose = 1;
820 break;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700821 }
822 }
823
824 if (parse_error)
Bill Richardsona08b5c92010-06-30 21:59:43 -0700825 return PrintHelp(progname);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700826
827 switch(mode) {
828 case OPT_MODE_PACK:
Bill Richardsona08b5c92010-06-30 21:59:43 -0700829 bp = NewBlob(version, vmlinuz, bootloader, config_file);
830 if (!bp)
831 return 1;
832 r = Pack(filename, key_block_file, signprivate, bp, pad, vblockonly);
833 FreeBlob(bp);
834 return r;
835
836 case OPT_MODE_REPACK:
vbendebb2b0fcc2010-07-15 15:09:47 -0700837 if (!config_file && !key_block_file) {
838 fprintf(stderr,
839 "You must supply at least one of --config and --keyblock\n");
840 return 1;
841 }
842
Bill Richardsona08b5c92010-06-30 21:59:43 -0700843 bp = OldBlob(oldfile);
844 if (!bp)
845 return 1;
vbendebb2b0fcc2010-07-15 15:09:47 -0700846 r = ReplaceConfig(bp, config_file);
847 if (!r) {
848 r = Pack(filename, key_block_file, signprivate, bp, pad, vblockonly);
849 }
Bill Richardsona08b5c92010-06-30 21:59:43 -0700850 FreeBlob(bp);
851 return r;
852
Randall Spangler7d6898d2010-06-11 09:22:13 -0700853 case OPT_MODE_VERIFY:
vbendebb2b0fcc2010-07-15 15:09:47 -0700854 return Verify(filename, signpubkey, verbose);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700855
Randall Spangler7d6898d2010-06-11 09:22:13 -0700856 default:
Bill Richardsona08b5c92010-06-30 21:59:43 -0700857 fprintf(stderr,
858 "You must specify a mode: --pack, --repack or --verify\n");
859 return PrintHelp(progname);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700860 }
861}