blob: 23b6eb2f0cffb71156e40aab579185ddd9ea91f8 [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"
vbendeb858fffb2010-10-06 09:51:44 -070094 " Required parameters (of --keyblock, --config, and --version \n"
95 " at least 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"
vbendeb858fffb2010-10-06 09:51:44 -0700101 " --version <number> Kernel version\n"
Bill Richardsona08b5c92010-06-30 21:59:43 -0700102 "\n"
103 " Optional:\n"
104 " --pad <number> Verification padding size in bytes\n"
105 " --vblockonly Emit just the verification blob\n",
106 progname);
107 fprintf(stderr,
108 "\nOR\n\n"
109 "Usage: %s --verify <file> [PARAMETERS]\n"
110 "\n"
vbendebb2b0fcc2010-07-15 15:09:47 -0700111 " Optional:\n"
Bill Richardson4f36ef32010-08-09 17:50:14 -0700112 " --signpubkey <file>"
113 " Public key to verify kernel keyblock, in .vbpubk format\n"
vbendebb2b0fcc2010-07-15 15:09:47 -0700114 " --verbose Print a more detailed report\n"
Bill Richardsona08b5c92010-06-30 21:59:43 -0700115 "\n",
116 progname);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700117 return 1;
118}
119
Bill Richardson249677d2010-06-23 11:16:37 -0700120static void Debug(const char *format, ...) {
121 if (!opt_debug)
122 return;
123
124 va_list ap;
125 va_start(ap, format);
126 fprintf(stderr, "DEBUG: ");
127 vfprintf(stderr, format, ap);
128 va_end(ap);
129}
130
Bill Richardson2f6a71f2010-10-14 09:25:39 -0700131/* Return an explanation when fread() fails. */
132static const char *error_fread(FILE *fp) {
133 const char *retval = "beats me why";
134 if (feof(fp))
135 retval = "EOF";
136 else if (ferror(fp))
137 retval = strerror(errno);
138 clearerr(fp);
139 return retval;
140}
Randall Spangler7d6898d2010-06-11 09:22:13 -0700141
142/* Return the smallest integral multiple of [alignment] that is equal
143 * to or greater than [val]. Used to determine the number of
144 * pages/sectors/blocks/whatever needed to contain [val]
145 * items/bytes/etc. */
146static uint64_t roundup(uint64_t val, uint64_t alignment) {
147 uint64_t rem = val % alignment;
148 if ( rem )
149 return val + (alignment - rem);
150 return val;
151}
152
153
154/* Match regexp /\b--\b/ to delimit the start of the kernel commandline. If we
155 * don't find one, we'll use the whole thing. */
156static unsigned int find_cmdline_start(char *input, unsigned int max_len) {
157 int start = 0;
158 int i;
159 for(i = 0; i < max_len - 1 && input[i]; i++) {
160 if ('-' == input[i] && '-' == input[i + 1]) { /* found a "--" */
161 if ((i == 0 || ' ' == input[i - 1]) && /* nothing before it */
162 (i + 2 >= max_len || ' ' == input[i+2])) { /* nothing after it */
163 start = i+2; /* note: hope there's a trailing '\0' */
164 break;
165 }
166 }
167 }
168 while(' ' == input[start]) /* skip leading spaces */
169 start++;
170
171 return start;
172}
173
174
Bill Richardsona08b5c92010-06-30 21:59:43 -0700175typedef struct blob_s {
176 /* Stuff needed by VbKernelPreambleHeader */
177 uint64_t kernel_version;
178 uint64_t bootloader_address;
179 uint64_t bootloader_size;
180 /* Raw kernel blob data */
181 uint64_t blob_size;
182 uint8_t *blob;
vbendebb2b0fcc2010-07-15 15:09:47 -0700183
184 /* these fields are not always initialized */
185 VbKernelPreambleHeader* preamble;
186 VbKeyBlockHeader* key_block;
187 uint8_t *buf;
188
Bill Richardsona08b5c92010-06-30 21:59:43 -0700189} blob_t;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700190
vbendebb2b0fcc2010-07-15 15:09:47 -0700191/* Given a blob return the location of the kernel command line buffer. */
192static char* BpCmdLineLocation(blob_t *bp)
193{
194 return (char*)(bp->blob + bp->bootloader_address - CROS_32BIT_ENTRY_ADDR -
195 CROS_CONFIG_SIZE - CROS_PARAMS_SIZE);
196}
Bill Richardsona08b5c92010-06-30 21:59:43 -0700197
198static void FreeBlob(blob_t *bp) {
199 if (bp) {
200 if (bp->blob)
201 Free(bp->blob);
vbendebb2b0fcc2010-07-15 15:09:47 -0700202 if (bp->buf)
203 Free(bp->buf);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700204 Free(bp);
205 }
206}
207
vbendebb2b0fcc2010-07-15 15:09:47 -0700208/*
209 * Read the kernel command line from a file. Get rid of \n characters along
210 * the way and verify that the line fits into a 4K buffer.
211 *
212 * Return the buffer contaning the line on success (and set the line length
213 * using the passed in parameter), or NULL in case something goes wrong.
214 */
215static uint8_t* ReadConfigFile(const char* config_file, uint64_t* config_size)
216{
217 uint8_t* config_buf;
218 int ii;
219
220 config_buf = ReadFile(config_file, config_size);
221 Debug(" config file size=0x%" PRIx64 "\n", *config_size);
222 if (CROS_CONFIG_SIZE <= *config_size) { /* need room for trailing '\0' */
223 error("Config file %s is too large (>= %d bytes)\n",
224 config_file, CROS_CONFIG_SIZE);
225 return NULL;
226 }
227
228 /* Replace newlines with spaces */
229 for (ii = 0; ii < *config_size; ii++) {
230 if ('\n' == config_buf[ii]) {
231 config_buf[ii] = ' ';
232 }
233 }
234 return config_buf;
235}
236
Bill Richardsona08b5c92010-06-30 21:59:43 -0700237/* Create a blob from its components */
238static blob_t *NewBlob(uint64_t version,
239 const char* vmlinuz,
240 const char* bootloader_file,
241 const char* config_file) {
242 blob_t *bp;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700243 struct linux_kernel_header *lh = 0;
244 struct linux_kernel_params *params = 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700245 uint8_t* config_buf;
246 uint64_t config_size;
247 uint8_t* bootloader_buf;
248 uint64_t bootloader_size;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700249 uint8_t* kernel_buf;
250 uint64_t kernel_size;
251 uint64_t kernel32_start = 0;
252 uint64_t kernel32_size = 0;
253 uint32_t cmdline_addr;
254 uint8_t* blob = NULL;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700255 uint64_t now = 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700256
Randall Spangler7d6898d2010-06-11 09:22:13 -0700257 if (!vmlinuz || !bootloader_file || !config_file) {
258 error("Must specify all input files\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700259 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700260 }
261
Bill Richardsona08b5c92010-06-30 21:59:43 -0700262 bp = (blob_t *)Malloc(sizeof(blob_t));
263 if (!bp) {
264 error("Couldn't allocate bytes for blob_t.\n");
265 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700266 }
vbendebb2b0fcc2010-07-15 15:09:47 -0700267
268 Memset(bp, 0, sizeof(*bp));
Bill Richardsona08b5c92010-06-30 21:59:43 -0700269 bp->kernel_version = version;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700270
271 /* Read the config file */
Bill Richardson249677d2010-06-23 11:16:37 -0700272 Debug("Reading %s\n", config_file);
vbendebb2b0fcc2010-07-15 15:09:47 -0700273 config_buf = ReadConfigFile(config_file, &config_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700274 if (!config_buf)
Bill Richardsona08b5c92010-06-30 21:59:43 -0700275 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700276
277 /* Read the bootloader */
Bill Richardson249677d2010-06-23 11:16:37 -0700278 Debug("Reading %s\n", bootloader_file);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700279 bootloader_buf = ReadFile(bootloader_file, &bootloader_size);
280 if (!bootloader_buf)
Bill Richardsona08b5c92010-06-30 21:59:43 -0700281 return 0;
Bill Richardson249677d2010-06-23 11:16:37 -0700282 Debug(" bootloader file size=0x%" PRIx64 "\n", bootloader_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700283
284 /* Read the kernel */
Bill Richardson249677d2010-06-23 11:16:37 -0700285 Debug("Reading %s\n", vmlinuz);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700286 kernel_buf = ReadFile(vmlinuz, &kernel_size);
287 if (!kernel_buf)
Bill Richardsona08b5c92010-06-30 21:59:43 -0700288 return 0;
Bill Richardson249677d2010-06-23 11:16:37 -0700289 Debug(" kernel file size=0x%" PRIx64 "\n", kernel_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700290 if (!kernel_size) {
291 error("Empty kernel file\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700292 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700293 }
294
295 /* The first part of vmlinuz is a header, followed by a real-mode
296 * boot stub. We only want the 32-bit part. */
297 lh = (struct linux_kernel_header *)kernel_buf;
298 kernel32_start = (lh->setup_sects + 1) << 9;
299 if (kernel32_start >= kernel_size) {
300 error("Malformed kernel\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700301 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700302 }
303 kernel32_size = kernel_size - kernel32_start;
Bill Richardson249677d2010-06-23 11:16:37 -0700304 Debug(" kernel32_start=0x%" PRIx64 "\n", kernel32_start);
305 Debug(" kernel32_size=0x%" PRIx64 "\n", kernel32_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700306
307 /* Allocate and zero the blob we need. */
Bill Richardsona08b5c92010-06-30 21:59:43 -0700308 bp->blob_size = roundup(kernel32_size, CROS_ALIGN) +
Randall Spangler7d6898d2010-06-11 09:22:13 -0700309 CROS_CONFIG_SIZE +
310 CROS_PARAMS_SIZE +
311 roundup(bootloader_size, CROS_ALIGN);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700312 blob = (uint8_t *)Malloc(bp->blob_size);
313 Debug("blob_size=0x%" PRIx64 "\n", bp->blob_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700314 if (!blob) {
Bill Richardsona08b5c92010-06-30 21:59:43 -0700315 error("Couldn't allocate %ld bytes.\n", bp->blob_size);
316 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700317 }
Bill Richardsona08b5c92010-06-30 21:59:43 -0700318 Memset(blob, 0, bp->blob_size);
319 bp->blob = blob;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700320
321 /* Copy the 32-bit kernel. */
Bill Richardson249677d2010-06-23 11:16:37 -0700322 Debug("kernel goes at blob+=0x%" PRIx64 "\n", now);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700323 if (kernel32_size)
324 Memcpy(blob + now, kernel_buf + kernel32_start, kernel32_size);
325 now += roundup(now + kernel32_size, CROS_ALIGN);
326
Bill Richardson249677d2010-06-23 11:16:37 -0700327 Debug("config goes at blob+0x%" PRIx64 "\n", now);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700328 /* Find the load address of the commandline. We'll need it later. */
329 cmdline_addr = CROS_32BIT_ENTRY_ADDR + now +
330 find_cmdline_start((char *)config_buf, config_size);
Bill Richardson249677d2010-06-23 11:16:37 -0700331 Debug(" cmdline_addr=0x%" PRIx64 "\n", cmdline_addr);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700332
333 /* Copy the config. */
334 if (config_size)
335 Memcpy(blob + now, config_buf, config_size);
336 now += CROS_CONFIG_SIZE;
337
338 /* The zeropage data is next. Overlay the linux_kernel_header onto it, and
339 * tweak a few fields. */
Bill Richardson249677d2010-06-23 11:16:37 -0700340 Debug("params goes at blob+=0x%" PRIx64 "\n", now);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700341 params = (struct linux_kernel_params *)(blob + now);
342 Memcpy(&(params->setup_sects), &(lh->setup_sects),
343 sizeof(*lh) - offsetof(struct linux_kernel_header, setup_sects));
344 params->boot_flag = 0;
345 params->ramdisk_image = 0; /* we don't support initrd */
346 params->ramdisk_size = 0;
347 params->type_of_loader = 0xff;
348 params->cmd_line_ptr = cmdline_addr;
Che-Liang Chiou475bf442010-08-23 11:20:44 +0800349 /* A fake e820 memory map with 2 entries */
350 params->n_e820_entry = 2;
351 params->e820_entries[0].start_addr = 0x00000000;
352 params->e820_entries[0].segment_size = 0x00001000;
353 params->e820_entries[0].segment_type = E820_TYPE_RAM;
354 params->e820_entries[1].start_addr = 0xfffff000;
355 params->e820_entries[1].segment_size = 0x00001000;
356 params->e820_entries[1].segment_type = E820_TYPE_RESERVED;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700357 now += CROS_PARAMS_SIZE;
358
359 /* Finally, append the bootloader. Remember where it will load in
360 * memory, too. */
Bill Richardson249677d2010-06-23 11:16:37 -0700361 Debug("bootloader goes at blob+=0x%" PRIx64 "\n", now);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700362 bp->bootloader_address = CROS_32BIT_ENTRY_ADDR + now;
363 bp->bootloader_size = roundup(bootloader_size, CROS_ALIGN);
364 Debug(" bootloader_address=0x%" PRIx64 "\n", bp->bootloader_address);
365 Debug(" bootloader_size=0x%" PRIx64 "\n", bp->bootloader_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700366 if (bootloader_size)
367 Memcpy(blob + now, bootloader_buf, bootloader_size);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700368 now += bp->bootloader_size;
Bill Richardson249677d2010-06-23 11:16:37 -0700369 Debug("end of blob is 0x%" PRIx64 "\n", now);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700370
371 /* Free input buffers */
372 Free(kernel_buf);
373 Free(config_buf);
374 Free(bootloader_buf);
375
Bill Richardsona08b5c92010-06-30 21:59:43 -0700376 /* Success */
377 return bp;
378}
379
380
381/* Pull the blob_t stuff out of a prepacked kernel blob file */
382static blob_t *OldBlob(const char* filename) {
vbendebb2b0fcc2010-07-15 15:09:47 -0700383 FILE* fp = NULL;
384 blob_t *bp = NULL;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700385 struct stat statbuf;
386 VbKeyBlockHeader* key_block;
387 VbKernelPreambleHeader* preamble;
388 uint64_t now = 0;
vbendebb2b0fcc2010-07-15 15:09:47 -0700389 uint8_t* buf = NULL;
390 int ret_error = 1;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700391
392 if (!filename) {
393 error("Must specify prepacked blob to read\n");
394 return 0;
395 }
396
397 if (0 != stat(filename, &statbuf)) {
Bill Richardson2f6a71f2010-10-14 09:25:39 -0700398 error("Unable to stat %s: %s\n", filename, strerror(errno));
Bill Richardsona08b5c92010-06-30 21:59:43 -0700399 return 0;
400 }
401
402 Debug("%s size is 0x%" PRIx64 "\n", filename, statbuf.st_size);
403 if (statbuf.st_size < DEFAULT_PADDING) {
404 error("%s is too small to be a valid kernel blob\n");
405 return 0;
406 }
407
408 Debug("Reading %s\n", filename);
409 fp = fopen(filename, "rb");
410 if (!fp) {
411 error("Unable to open file %s: %s\n", filename, strerror(errno));
412 return 0;
413 }
414
vbendebb2b0fcc2010-07-15 15:09:47 -0700415 buf = Malloc(DEFAULT_PADDING);
416 if (!buf) {
417 error("Unable to allocate padding\n");
418 goto unwind_oldblob;
419 }
420
421 if (1 != fread(buf, DEFAULT_PADDING, 1, fp)) {
Bill Richardson2f6a71f2010-10-14 09:25:39 -0700422 error("Unable to read header from %s: %s\n", filename, error_fread(fp));
vbendebb2b0fcc2010-07-15 15:09:47 -0700423 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700424 }
425
426 /* Skip the key block */
427 key_block = (VbKeyBlockHeader*)buf;
428 Debug("Keyblock is 0x%" PRIx64 " bytes\n", key_block->key_block_size);
429 now += key_block->key_block_size;
430 if (now > statbuf.st_size) {
431 error("key_block_size advances past the end of the blob\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700432 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700433 }
434
435 /* Skip the preamble */
436 preamble = (VbKernelPreambleHeader*)(buf + now);
437 Debug("Preamble is 0x%" PRIx64 " bytes\n", preamble->preamble_size);
438 now += preamble->preamble_size;
439 if (now > statbuf.st_size) {
440 error("preamble_size advances past the end of the blob\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700441 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700442 }
443
444 /* Go find the kernel blob */
445 Debug("kernel blob is at offset 0x%" PRIx64 "\n", now);
446 if (0 != fseek(fp, now, SEEK_SET)) {
447 error("Unable to seek to 0x%" PRIx64 " in %s: %s\n", now, filename,
448 strerror(errno));
vbendebb2b0fcc2010-07-15 15:09:47 -0700449 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700450 }
451
452 /* Remember what we've got */
453 bp = (blob_t *)Malloc(sizeof(blob_t));
454 if (!bp) {
455 error("Couldn't allocate bytes for blob_t.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700456 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700457 }
458
vbendebb2b0fcc2010-07-15 15:09:47 -0700459 bp->buf = buf;
460 bp->key_block = key_block;
461 bp->preamble = preamble;
462
Bill Richardsona08b5c92010-06-30 21:59:43 -0700463 bp->kernel_version = preamble->kernel_version;
464 bp->bootloader_address = preamble->bootloader_address;
465 bp->bootloader_size = preamble->bootloader_size;
466 bp->blob_size = preamble->body_signature.data_size;
467
468 Debug(" kernel_version = %d\n", bp->kernel_version);
469 Debug(" bootloader_address = 0x%" PRIx64 "\n", bp->bootloader_address);
470 Debug(" bootloader_size = 0x%" PRIx64 "\n", bp->bootloader_size);
471 Debug(" blob_size = 0x%" PRIx64 "\n", bp->blob_size);
472
Bill Richardson2f6a71f2010-10-14 09:25:39 -0700473 if (!bp->blob_size) {
474 error("No kernel blob found\n");
475 goto unwind_oldblob;
476 }
477
Bill Richardsona08b5c92010-06-30 21:59:43 -0700478 bp->blob = (uint8_t *)Malloc(bp->blob_size);
479 if (!bp->blob) {
480 error("Couldn't allocate 0x%" PRIx64 " bytes for blob_t.\n", bp->blob_size);
vbendebb2b0fcc2010-07-15 15:09:47 -0700481 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700482 }
483
484 /* read it in */
485 if (1 != fread(bp->blob, bp->blob_size, 1, fp)) {
Bill Richardson2f6a71f2010-10-14 09:25:39 -0700486 error("Unable to read kernel blob from %s: %s\n", filename, error_fread(fp));
vbendebb2b0fcc2010-07-15 15:09:47 -0700487 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700488 }
489
vbendebb2b0fcc2010-07-15 15:09:47 -0700490 ret_error = 0;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700491
vbendebb2b0fcc2010-07-15 15:09:47 -0700492 /* done */
493unwind_oldblob:
494 fclose(fp);
495 if (ret_error) {
496 if (bp) {
497 FreeBlob(bp);
498 bp = NULL;
499 } else if (buf) {
500 Free(buf);
501 }
502 }
Bill Richardsona08b5c92010-06-30 21:59:43 -0700503 return bp;
504}
505
506
507/* Pack a .kernel */
508static int Pack(const char* outfile, const char* keyblock_file,
509 const char* signprivate, blob_t *bp, uint64_t pad,
510 int vblockonly) {
511 VbPrivateKey* signing_key;
512 VbSignature* body_sig;
513 VbKernelPreambleHeader* preamble;
514 VbKeyBlockHeader* key_block;
515 uint64_t key_block_size;
516 FILE* f;
517 uint64_t i;
518
519 if (!outfile) {
520 error("Must specify output filename\n");
521 return 1;
522 }
vbendebb2b0fcc2010-07-15 15:09:47 -0700523 if ((!keyblock_file && !bp->key_block) || !signprivate) {
Bill Richardsona08b5c92010-06-30 21:59:43 -0700524 error("Must specify all keys\n");
525 return 1;
526 }
527 if (!bp) {
528 error("Refusing to pack invalid kernel blob\n");
529 return 1;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700530 }
vbendebb2b0fcc2010-07-15 15:09:47 -0700531
532 /* Get the key block and read the private key. */
533 if (keyblock_file) {
534 key_block = (VbKeyBlockHeader*)ReadFile(keyblock_file, &key_block_size);
535 if (!key_block) {
536 error("Error reading key block.\n");
537 return 1;
538 }
539 } else {
540 key_block = bp->key_block;
541 key_block_size = key_block->key_block_size;
542 }
543
Bill Richardsona08b5c92010-06-30 21:59:43 -0700544 if (pad < key_block->key_block_size) {
545 error("Pad too small\n");
546 return 1;
547 }
548
Bill Richardsonabf05502010-07-01 10:22:06 -0700549 signing_key = PrivateKeyRead(signprivate);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700550 if (!signing_key) {
551 error("Error reading signing key.\n");
552 return 1;
553 }
554
Randall Spangler7d6898d2010-06-11 09:22:13 -0700555 /* Sign the kernel data */
Bill Richardsona08b5c92010-06-30 21:59:43 -0700556 body_sig = CalculateSignature(bp->blob, bp->blob_size, signing_key);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700557 if (!body_sig) {
558 error("Error calculating body signature\n");
559 return 1;
560 }
561
562 /* Create preamble */
Bill Richardsona08b5c92010-06-30 21:59:43 -0700563 preamble = CreateKernelPreamble(bp->kernel_version,
Randall Spangler7d6898d2010-06-11 09:22:13 -0700564 CROS_32BIT_ENTRY_ADDR,
Bill Richardsona08b5c92010-06-30 21:59:43 -0700565 bp->bootloader_address,
566 bp->bootloader_size,
Randall Spangler7d6898d2010-06-11 09:22:13 -0700567 body_sig,
568 pad - key_block_size,
569 signing_key);
570 if (!preamble) {
571 error("Error creating preamble.\n");
572 return 1;
573 }
574
575 /* Write the output file */
Bill Richardson249677d2010-06-23 11:16:37 -0700576 Debug("writing %s...\n", outfile);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700577 f = fopen(outfile, "wb");
578 if (!f) {
579 error("Can't open output file %s\n", outfile);
580 return 1;
581 }
Bill Richardson249677d2010-06-23 11:16:37 -0700582 Debug("0x%" PRIx64 " bytes of key_block\n", key_block_size);
583 Debug("0x%" PRIx64 " bytes of preamble\n", preamble->preamble_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700584 i = ((1 != fwrite(key_block, key_block_size, 1, f)) ||
Bill Richardsona08b5c92010-06-30 21:59:43 -0700585 (1 != fwrite(preamble, preamble->preamble_size, 1, f)));
Randall Spangler7d6898d2010-06-11 09:22:13 -0700586 if (i) {
587 error("Can't write output file %s\n", outfile);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700588 fclose(f);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700589 unlink(outfile);
590 return 1;
591 }
592
Bill Richardsona08b5c92010-06-30 21:59:43 -0700593 if (!vblockonly) {
594 Debug("0x%" PRIx64 " bytes of blob\n", bp->blob_size);
595 i = (1 != fwrite(bp->blob, bp->blob_size, 1, f));
596 if (i) {
597 error("Can't write output file %s\n", outfile);
598 fclose(f);
599 unlink(outfile);
600 return 1;
601 }
602 }
603
604 fclose(f);
605
Randall Spangler7d6898d2010-06-11 09:22:13 -0700606 /* Success */
607 return 0;
608}
609
vbendebb2b0fcc2010-07-15 15:09:47 -0700610/*
611 * Replace kernel command line in a blob representing a kernel.
612 */
613static int ReplaceConfig(blob_t* bp, const char* config_file)
614{
615 uint8_t* new_conf;
616 uint64_t config_size;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700617
vbendebb2b0fcc2010-07-15 15:09:47 -0700618 if (!config_file) {
619 return 0;
620 }
621
622 new_conf = ReadConfigFile(config_file, &config_size);
623 if (!new_conf) {
624 return 1;
625 }
626
627 /* fill the config buffer with zeros */
628 Memset(BpCmdLineLocation(bp), 0, CROS_CONFIG_SIZE);
629 Memcpy(BpCmdLineLocation(bp), new_conf, config_size);
630 Free(new_conf);
631 return 0;
632}
633
634static int Verify(const char* infile, const char* signpubkey, int verbose) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700635
636 VbKeyBlockHeader* key_block;
637 VbKernelPreambleHeader* preamble;
638 VbPublicKey* data_key;
Bill Richardson4f36ef32010-08-09 17:50:14 -0700639 VbPublicKey* sign_key = NULL;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700640 RSAPublicKey* rsa;
vbendebb2b0fcc2010-07-15 15:09:47 -0700641 blob_t* bp;
642 uint64_t now;
643 int rv = 1;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700644
Bill Richardson4f36ef32010-08-09 17:50:14 -0700645 if (!infile) {
646 error("Must specify filename\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700647 return 1;
648 }
649
650 /* Read public signing key */
Bill Richardson4f36ef32010-08-09 17:50:14 -0700651 if (signpubkey) {
652 sign_key = PublicKeyRead(signpubkey);
653 if (!sign_key) {
654 error("Error reading signpubkey.\n");
655 return 1;
656 }
Randall Spangler7d6898d2010-06-11 09:22:13 -0700657 }
658
659 /* Read blob */
vbendebb2b0fcc2010-07-15 15:09:47 -0700660 bp = OldBlob(infile);
661 if (!bp) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700662 error("Error reading input file\n");
663 return 1;
664 }
665
666 /* Verify key block */
vbendebb2b0fcc2010-07-15 15:09:47 -0700667 key_block = bp->key_block;
Randall Spangler138acfe2010-08-17 15:45:21 -0700668 if (0 != KeyBlockVerify(key_block, bp->blob_size, sign_key,
669 (sign_key ? 0 : 1))) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700670 error("Error verifying key block.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700671 goto verify_exit;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700672 }
vbendebb2b0fcc2010-07-15 15:09:47 -0700673 now = key_block->key_block_size;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700674
675 printf("Key block:\n");
676 data_key = &key_block->data_key;
Bill Richardson4f36ef32010-08-09 17:50:14 -0700677 if (verbose)
678 printf(" Signature: %s\n", sign_key ? "valid" : "ignored");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700679 printf(" Size: 0x%" PRIx64 "\n", key_block->key_block_size);
Bill Richardson60bcbe32010-09-09 14:53:56 -0700680 printf(" Flags: %" PRIu64 " ", key_block->key_block_flags);
681 if (key_block->key_block_flags & KEY_BLOCK_FLAG_DEVELOPER_0)
682 printf(" !DEV");
683 if (key_block->key_block_flags & KEY_BLOCK_FLAG_DEVELOPER_1)
684 printf(" DEV");
685 if (key_block->key_block_flags & KEY_BLOCK_FLAG_RECOVERY_0)
686 printf(" !REC");
687 if (key_block->key_block_flags & KEY_BLOCK_FLAG_RECOVERY_1)
688 printf(" REC");
689 printf("\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700690 printf(" Data key algorithm: %" PRIu64 " %s\n", data_key->algorithm,
691 (data_key->algorithm < kNumAlgorithms ?
692 algo_strings[data_key->algorithm] : "(invalid)"));
693 printf(" Data key version: %" PRIu64 "\n", data_key->key_version);
Bill Richardson60bcbe32010-09-09 14:53:56 -0700694 printf(" Data key sha1sum: ");
695 PrintPubKeySha1Sum(data_key);
696 printf("\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700697
698 rsa = PublicKeyToRSA(&key_block->data_key);
699 if (!rsa) {
700 error("Error parsing data key.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700701 goto verify_exit;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700702 }
703
704 /* Verify preamble */
vbendebb2b0fcc2010-07-15 15:09:47 -0700705 preamble = bp->preamble;
Randall Spangler87c13d82010-07-19 10:35:40 -0700706 if (0 != VerifyKernelPreamble(
Bill Richardson4f36ef32010-08-09 17:50:14 -0700707 preamble, bp->blob_size - key_block->key_block_size, rsa)) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700708 error("Error verifying preamble.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700709 goto verify_exit;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700710 }
711 now += preamble->preamble_size;
712
713 printf("Preamble:\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700714 printf(" Size: 0x%" PRIx64 "\n", preamble->preamble_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700715 printf(" Header version: %" PRIu32 ".%" PRIu32"\n",
716 preamble->header_version_major, preamble->header_version_minor);
717 printf(" Kernel version: %" PRIu64 "\n", preamble->kernel_version);
Bill Richardson249677d2010-06-23 11:16:37 -0700718 printf(" Body load address: 0x%" PRIx64 "\n", preamble->body_load_address);
719 printf(" Body size: 0x%" PRIx64 "\n",
Randall Spangler7d6898d2010-06-11 09:22:13 -0700720 preamble->body_signature.data_size);
Randall Spangler87c13d82010-07-19 10:35:40 -0700721 printf(" Bootloader address: 0x%" PRIx64 "\n",
722 preamble->bootloader_address);
Bill Richardson249677d2010-06-23 11:16:37 -0700723 printf(" Bootloader size: 0x%" PRIx64 "\n", preamble->bootloader_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700724
725 /* Verify body */
Randall Spangler87c13d82010-07-19 10:35:40 -0700726 if (0 != VerifyData(bp->blob, bp->blob_size, &preamble->body_signature,
727 rsa)) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700728 error("Error verifying kernel body.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700729 goto verify_exit;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700730 }
731 printf("Body verification succeeded.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700732
733 rv = 0;
734
735 if (!verbose) {
736 goto verify_exit;
737 }
738
739 printf("Config:\n%s\n", BpCmdLineLocation(bp));
740
741verify_exit:
742 FreeBlob(bp);
743 return rv;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700744}
745
746
747int main(int argc, char* argv[]) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700748 char* filename = NULL;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700749 char* oldfile = NULL;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700750 char* key_block_file = NULL;
751 char* signpubkey = NULL;
752 char* signprivate = NULL;
753 uint64_t version = 0;
754 char* vmlinuz = NULL;
755 char* bootloader = NULL;
756 char* config_file = NULL;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700757 int vblockonly = 0;
vbendebb2b0fcc2010-07-15 15:09:47 -0700758 int verbose = 0;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700759 uint64_t pad = DEFAULT_PADDING;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700760 int mode = 0;
761 int parse_error = 0;
762 char* e;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700763 int i,r;
764 blob_t *bp;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700765
Bill Richardsona08b5c92010-06-30 21:59:43 -0700766
767 char *progname = strrchr(argv[0], '/');
768 if (progname)
769 progname++;
770 else
771 progname = argv[0];
772
vbendebb2b0fcc2010-07-15 15:09:47 -0700773 while (((i = getopt_long(argc, argv, ":", long_opts, NULL)) != -1) &&
774 !parse_error) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700775 switch (i) {
vbendebb2b0fcc2010-07-15 15:09:47 -0700776 default:
Randall Spangler7d6898d2010-06-11 09:22:13 -0700777 case '?':
778 /* Unhandled option */
Randall Spangler7d6898d2010-06-11 09:22:13 -0700779 parse_error = 1;
780 break;
781
Bill Richardson4f36ef32010-08-09 17:50:14 -0700782 case 0:
783 /* silently handled option */
784 break;
785
Randall Spangler7d6898d2010-06-11 09:22:13 -0700786 case OPT_MODE_PACK:
Bill Richardsona08b5c92010-06-30 21:59:43 -0700787 case OPT_MODE_REPACK:
Randall Spangler7d6898d2010-06-11 09:22:13 -0700788 case OPT_MODE_VERIFY:
vbendebb2b0fcc2010-07-15 15:09:47 -0700789 if (mode && (mode != i)) {
790 fprintf(stderr, "Only single mode can be specified\n");
791 parse_error = 1;
792 break;
793 }
Randall Spangler7d6898d2010-06-11 09:22:13 -0700794 mode = i;
795 filename = optarg;
796 break;
797
Bill Richardsona08b5c92010-06-30 21:59:43 -0700798 case OPT_OLDBLOB:
799 oldfile = optarg;
800 break;
801
Randall Spangler7d6898d2010-06-11 09:22:13 -0700802 case OPT_KEYBLOCK:
803 key_block_file = optarg;
804 break;
805
806 case OPT_SIGNPUBKEY:
807 signpubkey = optarg;
808 break;
809
810 case OPT_SIGNPRIVATE:
811 signprivate = optarg;
812 break;
813
814 case OPT_VMLINUZ:
815 vmlinuz = optarg;
816 break;
817
818 case OPT_BOOTLOADER:
819 bootloader = optarg;
820 break;
821
822 case OPT_CONFIG:
823 config_file = optarg;
824 break;
825
Bill Richardsona08b5c92010-06-30 21:59:43 -0700826 case OPT_VBLOCKONLY:
827 vblockonly = 1;
828 break;
829
Randall Spangler7d6898d2010-06-11 09:22:13 -0700830 case OPT_VERSION:
831 version = strtoul(optarg, &e, 0);
832 if (!*optarg || (e && *e)) {
Bill Richardsona08b5c92010-06-30 21:59:43 -0700833 fprintf(stderr, "Invalid --version\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700834 parse_error = 1;
835 }
836 break;
837
838 case OPT_PAD:
839 pad = strtoul(optarg, &e, 0);
840 if (!*optarg || (e && *e)) {
Bill Richardsona08b5c92010-06-30 21:59:43 -0700841 fprintf(stderr, "Invalid --pad\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700842 parse_error = 1;
843 }
844 break;
vbendebb2b0fcc2010-07-15 15:09:47 -0700845
846 case OPT_VERBOSE:
847 verbose = 1;
848 break;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700849 }
850 }
851
852 if (parse_error)
Bill Richardsona08b5c92010-06-30 21:59:43 -0700853 return PrintHelp(progname);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700854
855 switch(mode) {
856 case OPT_MODE_PACK:
Bill Richardsona08b5c92010-06-30 21:59:43 -0700857 bp = NewBlob(version, vmlinuz, bootloader, config_file);
858 if (!bp)
859 return 1;
860 r = Pack(filename, key_block_file, signprivate, bp, pad, vblockonly);
861 FreeBlob(bp);
862 return r;
863
864 case OPT_MODE_REPACK:
vbendeb858fffb2010-10-06 09:51:44 -0700865 if (!config_file && !key_block_file && !version) {
vbendebb2b0fcc2010-07-15 15:09:47 -0700866 fprintf(stderr,
vbendeb858fffb2010-10-06 09:51:44 -0700867 "You must supply at least one of "
868 "--config, --keyblock or --version\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700869 return 1;
870 }
871
Bill Richardsona08b5c92010-06-30 21:59:43 -0700872 bp = OldBlob(oldfile);
873 if (!bp)
874 return 1;
vbendebb2b0fcc2010-07-15 15:09:47 -0700875 r = ReplaceConfig(bp, config_file);
876 if (!r) {
vbendeb858fffb2010-10-06 09:51:44 -0700877 if (version) {
878 bp->kernel_version = version;
879 }
vbendebb2b0fcc2010-07-15 15:09:47 -0700880 r = Pack(filename, key_block_file, signprivate, bp, pad, vblockonly);
881 }
Bill Richardsona08b5c92010-06-30 21:59:43 -0700882 FreeBlob(bp);
883 return r;
884
Randall Spangler7d6898d2010-06-11 09:22:13 -0700885 case OPT_MODE_VERIFY:
vbendebb2b0fcc2010-07-15 15:09:47 -0700886 return Verify(filename, signpubkey, verbose);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700887
Randall Spangler7d6898d2010-06-11 09:22:13 -0700888 default:
Bill Richardsona08b5c92010-06-30 21:59:43 -0700889 fprintf(stderr,
890 "You must specify a mode: --pack, --repack or --verify\n");
891 return PrintHelp(progname);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700892 }
893}