blob: 4904610ac88e15b2b10ffc9c327b1d1b9d5a0cb7 [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 Spanglerae87b922011-05-12 13:27:28 -0700132 " --minversion <number> Minimum combined kernel key version\n"
133 " and kernel version\n"
Bill Richardsona08b5c92010-06-30 21:59:43 -0700134 "\n",
135 progname);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700136 return 1;
137}
138
Bill Richardson249677d2010-06-23 11:16:37 -0700139static void Debug(const char *format, ...) {
140 if (!opt_debug)
141 return;
142
143 va_list ap;
144 va_start(ap, format);
145 fprintf(stderr, "DEBUG: ");
146 vfprintf(stderr, format, ap);
147 va_end(ap);
148}
149
Bill Richardson2f6a71f2010-10-14 09:25:39 -0700150/* Return an explanation when fread() fails. */
151static const char *error_fread(FILE *fp) {
152 const char *retval = "beats me why";
153 if (feof(fp))
154 retval = "EOF";
155 else if (ferror(fp))
156 retval = strerror(errno);
157 clearerr(fp);
158 return retval;
159}
Randall Spangler7d6898d2010-06-11 09:22:13 -0700160
161/* Return the smallest integral multiple of [alignment] that is equal
162 * to or greater than [val]. Used to determine the number of
163 * pages/sectors/blocks/whatever needed to contain [val]
164 * items/bytes/etc. */
165static uint64_t roundup(uint64_t val, uint64_t alignment) {
166 uint64_t rem = val % alignment;
167 if ( rem )
168 return val + (alignment - rem);
169 return val;
170}
171
172
173/* Match regexp /\b--\b/ to delimit the start of the kernel commandline. If we
174 * don't find one, we'll use the whole thing. */
175static unsigned int find_cmdline_start(char *input, unsigned int max_len) {
176 int start = 0;
177 int i;
178 for(i = 0; i < max_len - 1 && input[i]; i++) {
179 if ('-' == input[i] && '-' == input[i + 1]) { /* found a "--" */
180 if ((i == 0 || ' ' == input[i - 1]) && /* nothing before it */
181 (i + 2 >= max_len || ' ' == input[i+2])) { /* nothing after it */
182 start = i+2; /* note: hope there's a trailing '\0' */
183 break;
184 }
185 }
186 }
187 while(' ' == input[start]) /* skip leading spaces */
188 start++;
189
190 return start;
191}
192
193
Bill Richardsona08b5c92010-06-30 21:59:43 -0700194typedef struct blob_s {
195 /* Stuff needed by VbKernelPreambleHeader */
196 uint64_t kernel_version;
197 uint64_t bootloader_address;
198 uint64_t bootloader_size;
199 /* Raw kernel blob data */
200 uint64_t blob_size;
201 uint8_t *blob;
vbendebb2b0fcc2010-07-15 15:09:47 -0700202
203 /* these fields are not always initialized */
204 VbKernelPreambleHeader* preamble;
205 VbKeyBlockHeader* key_block;
206 uint8_t *buf;
207
Bill Richardsona08b5c92010-06-30 21:59:43 -0700208} blob_t;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700209
vbendebb2b0fcc2010-07-15 15:09:47 -0700210/* Given a blob return the location of the kernel command line buffer. */
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800211static char* BpCmdLineLocation(blob_t *bp, uint64_t kernel_body_load_address)
vbendebb2b0fcc2010-07-15 15:09:47 -0700212{
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800213 return (char*)(bp->blob + bp->bootloader_address - kernel_body_load_address -
vbendebb2b0fcc2010-07-15 15:09:47 -0700214 CROS_CONFIG_SIZE - CROS_PARAMS_SIZE);
215}
Bill Richardsona08b5c92010-06-30 21:59:43 -0700216
217static void FreeBlob(blob_t *bp) {
218 if (bp) {
219 if (bp->blob)
220 Free(bp->blob);
vbendebb2b0fcc2010-07-15 15:09:47 -0700221 if (bp->buf)
222 Free(bp->buf);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700223 Free(bp);
224 }
225}
226
vbendebb2b0fcc2010-07-15 15:09:47 -0700227/*
228 * Read the kernel command line from a file. Get rid of \n characters along
229 * the way and verify that the line fits into a 4K buffer.
230 *
231 * Return the buffer contaning the line on success (and set the line length
232 * using the passed in parameter), or NULL in case something goes wrong.
233 */
234static uint8_t* ReadConfigFile(const char* config_file, uint64_t* config_size)
235{
236 uint8_t* config_buf;
237 int ii;
238
239 config_buf = ReadFile(config_file, config_size);
240 Debug(" config file size=0x%" PRIx64 "\n", *config_size);
241 if (CROS_CONFIG_SIZE <= *config_size) { /* need room for trailing '\0' */
242 error("Config file %s is too large (>= %d bytes)\n",
243 config_file, CROS_CONFIG_SIZE);
244 return NULL;
245 }
246
247 /* Replace newlines with spaces */
248 for (ii = 0; ii < *config_size; ii++) {
249 if ('\n' == config_buf[ii]) {
250 config_buf[ii] = ' ';
251 }
252 }
253 return config_buf;
254}
255
Bill Richardsona08b5c92010-06-30 21:59:43 -0700256/* Create a blob from its components */
257static blob_t *NewBlob(uint64_t version,
258 const char* vmlinuz,
259 const char* bootloader_file,
Che-Liang Chiou03762032011-02-22 11:16:51 +0800260 const char* config_file,
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800261 int arch,
262 uint64_t kernel_body_load_address) {
Che-Liang Chiou03762032011-02-22 11:16:51 +0800263 blob_t* bp;
264 struct linux_kernel_header* lh = 0;
265 struct linux_kernel_params* params = 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700266 uint8_t* config_buf;
267 uint64_t config_size;
268 uint8_t* bootloader_buf;
269 uint64_t bootloader_size;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700270 uint8_t* kernel_buf;
271 uint64_t kernel_size;
272 uint64_t kernel32_start = 0;
273 uint64_t kernel32_size = 0;
274 uint32_t cmdline_addr;
275 uint8_t* blob = NULL;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700276 uint64_t now = 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700277
Randall Spangler7d6898d2010-06-11 09:22:13 -0700278 if (!vmlinuz || !bootloader_file || !config_file) {
279 error("Must specify all input files\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700280 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700281 }
282
Bill Richardsona08b5c92010-06-30 21:59:43 -0700283 bp = (blob_t *)Malloc(sizeof(blob_t));
284 if (!bp) {
285 error("Couldn't allocate bytes for blob_t.\n");
286 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700287 }
vbendebb2b0fcc2010-07-15 15:09:47 -0700288
289 Memset(bp, 0, sizeof(*bp));
Bill Richardsona08b5c92010-06-30 21:59:43 -0700290 bp->kernel_version = version;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700291
292 /* Read the config file */
Bill Richardson249677d2010-06-23 11:16:37 -0700293 Debug("Reading %s\n", config_file);
vbendebb2b0fcc2010-07-15 15:09:47 -0700294 config_buf = ReadConfigFile(config_file, &config_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700295 if (!config_buf)
Bill Richardsona08b5c92010-06-30 21:59:43 -0700296 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700297
298 /* Read the bootloader */
Bill Richardson249677d2010-06-23 11:16:37 -0700299 Debug("Reading %s\n", bootloader_file);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700300 bootloader_buf = ReadFile(bootloader_file, &bootloader_size);
301 if (!bootloader_buf)
Bill Richardsona08b5c92010-06-30 21:59:43 -0700302 return 0;
Bill Richardson249677d2010-06-23 11:16:37 -0700303 Debug(" bootloader file size=0x%" PRIx64 "\n", bootloader_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700304
305 /* Read the kernel */
Bill Richardson249677d2010-06-23 11:16:37 -0700306 Debug("Reading %s\n", vmlinuz);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700307 kernel_buf = ReadFile(vmlinuz, &kernel_size);
308 if (!kernel_buf)
Bill Richardsona08b5c92010-06-30 21:59:43 -0700309 return 0;
Bill Richardson249677d2010-06-23 11:16:37 -0700310 Debug(" kernel file size=0x%" PRIx64 "\n", kernel_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700311 if (!kernel_size) {
312 error("Empty kernel file\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700313 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700314 }
315
Che-Liang Chiou03762032011-02-22 11:16:51 +0800316 if (arch == ARCH_X86) {
317 /* The first part of vmlinuz is a header, followed by a real-mode
318 * boot stub. We only want the 32-bit part. */
319 lh = (struct linux_kernel_header *)kernel_buf;
320 kernel32_start = (lh->setup_sects + 1) << 9;
321 if (kernel32_start >= kernel_size) {
322 error("Malformed kernel\n");
323 return 0;
324 }
325 } else
326 kernel32_start = 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700327 kernel32_size = kernel_size - kernel32_start;
Bill Richardson249677d2010-06-23 11:16:37 -0700328 Debug(" kernel32_start=0x%" PRIx64 "\n", kernel32_start);
329 Debug(" kernel32_size=0x%" PRIx64 "\n", kernel32_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700330
331 /* Allocate and zero the blob we need. */
Bill Richardsona08b5c92010-06-30 21:59:43 -0700332 bp->blob_size = roundup(kernel32_size, CROS_ALIGN) +
Randall Spangler7d6898d2010-06-11 09:22:13 -0700333 CROS_CONFIG_SIZE +
334 CROS_PARAMS_SIZE +
335 roundup(bootloader_size, CROS_ALIGN);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700336 blob = (uint8_t *)Malloc(bp->blob_size);
337 Debug("blob_size=0x%" PRIx64 "\n", bp->blob_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700338 if (!blob) {
Bill Richardsona08b5c92010-06-30 21:59:43 -0700339 error("Couldn't allocate %ld bytes.\n", bp->blob_size);
340 return 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700341 }
Bill Richardsona08b5c92010-06-30 21:59:43 -0700342 Memset(blob, 0, bp->blob_size);
343 bp->blob = blob;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700344
345 /* Copy the 32-bit kernel. */
Bill Richardson249677d2010-06-23 11:16:37 -0700346 Debug("kernel goes at blob+=0x%" PRIx64 "\n", now);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700347 if (kernel32_size)
348 Memcpy(blob + now, kernel_buf + kernel32_start, kernel32_size);
349 now += roundup(now + kernel32_size, CROS_ALIGN);
350
Bill Richardson249677d2010-06-23 11:16:37 -0700351 Debug("config goes at blob+0x%" PRIx64 "\n", now);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700352 /* Find the load address of the commandline. We'll need it later. */
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800353 cmdline_addr = kernel_body_load_address + now +
Randall Spangler7d6898d2010-06-11 09:22:13 -0700354 find_cmdline_start((char *)config_buf, config_size);
Bill Richardson249677d2010-06-23 11:16:37 -0700355 Debug(" cmdline_addr=0x%" PRIx64 "\n", cmdline_addr);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700356
357 /* Copy the config. */
358 if (config_size)
359 Memcpy(blob + now, config_buf, config_size);
360 now += CROS_CONFIG_SIZE;
361
362 /* The zeropage data is next. Overlay the linux_kernel_header onto it, and
363 * tweak a few fields. */
Bill Richardson249677d2010-06-23 11:16:37 -0700364 Debug("params goes at blob+=0x%" PRIx64 "\n", now);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700365 params = (struct linux_kernel_params *)(blob + now);
Che-Liang Chiou03762032011-02-22 11:16:51 +0800366 if (arch == ARCH_X86)
367 Memcpy(&(params->setup_sects), &(lh->setup_sects),
368 sizeof(*lh) - offsetof(struct linux_kernel_header, setup_sects));
369 else
370 Memset(&(params->setup_sects), 0,
371 sizeof(*lh) - offsetof(struct linux_kernel_header, setup_sects));
Randall Spangler7d6898d2010-06-11 09:22:13 -0700372 params->boot_flag = 0;
373 params->ramdisk_image = 0; /* we don't support initrd */
374 params->ramdisk_size = 0;
375 params->type_of_loader = 0xff;
376 params->cmd_line_ptr = cmdline_addr;
Che-Liang Chiou475bf442010-08-23 11:20:44 +0800377 /* A fake e820 memory map with 2 entries */
378 params->n_e820_entry = 2;
379 params->e820_entries[0].start_addr = 0x00000000;
380 params->e820_entries[0].segment_size = 0x00001000;
381 params->e820_entries[0].segment_type = E820_TYPE_RAM;
382 params->e820_entries[1].start_addr = 0xfffff000;
383 params->e820_entries[1].segment_size = 0x00001000;
384 params->e820_entries[1].segment_type = E820_TYPE_RESERVED;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700385 now += CROS_PARAMS_SIZE;
386
387 /* Finally, append the bootloader. Remember where it will load in
388 * memory, too. */
Bill Richardson249677d2010-06-23 11:16:37 -0700389 Debug("bootloader goes at blob+=0x%" PRIx64 "\n", now);
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800390 bp->bootloader_address = kernel_body_load_address + now;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700391 bp->bootloader_size = roundup(bootloader_size, CROS_ALIGN);
392 Debug(" bootloader_address=0x%" PRIx64 "\n", bp->bootloader_address);
393 Debug(" bootloader_size=0x%" PRIx64 "\n", bp->bootloader_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700394 if (bootloader_size)
395 Memcpy(blob + now, bootloader_buf, bootloader_size);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700396 now += bp->bootloader_size;
Bill Richardson249677d2010-06-23 11:16:37 -0700397 Debug("end of blob is 0x%" PRIx64 "\n", now);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700398
399 /* Free input buffers */
400 Free(kernel_buf);
401 Free(config_buf);
402 Free(bootloader_buf);
403
Bill Richardsona08b5c92010-06-30 21:59:43 -0700404 /* Success */
405 return bp;
406}
407
408
409/* Pull the blob_t stuff out of a prepacked kernel blob file */
410static blob_t *OldBlob(const char* filename) {
vbendebb2b0fcc2010-07-15 15:09:47 -0700411 FILE* fp = NULL;
412 blob_t *bp = NULL;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700413 struct stat statbuf;
414 VbKeyBlockHeader* key_block;
415 VbKernelPreambleHeader* preamble;
416 uint64_t now = 0;
vbendebb2b0fcc2010-07-15 15:09:47 -0700417 uint8_t* buf = NULL;
418 int ret_error = 1;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700419
420 if (!filename) {
421 error("Must specify prepacked blob to read\n");
422 return 0;
423 }
424
425 if (0 != stat(filename, &statbuf)) {
Bill Richardson2f6a71f2010-10-14 09:25:39 -0700426 error("Unable to stat %s: %s\n", filename, strerror(errno));
Bill Richardsona08b5c92010-06-30 21:59:43 -0700427 return 0;
428 }
429
430 Debug("%s size is 0x%" PRIx64 "\n", filename, statbuf.st_size);
431 if (statbuf.st_size < DEFAULT_PADDING) {
432 error("%s is too small to be a valid kernel blob\n");
433 return 0;
434 }
435
436 Debug("Reading %s\n", filename);
437 fp = fopen(filename, "rb");
438 if (!fp) {
439 error("Unable to open file %s: %s\n", filename, strerror(errno));
440 return 0;
441 }
442
vbendebb2b0fcc2010-07-15 15:09:47 -0700443 buf = Malloc(DEFAULT_PADDING);
444 if (!buf) {
445 error("Unable to allocate padding\n");
446 goto unwind_oldblob;
447 }
448
449 if (1 != fread(buf, DEFAULT_PADDING, 1, fp)) {
Bill Richardson2f6a71f2010-10-14 09:25:39 -0700450 error("Unable to read header from %s: %s\n", filename, error_fread(fp));
vbendebb2b0fcc2010-07-15 15:09:47 -0700451 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700452 }
453
454 /* Skip the key block */
455 key_block = (VbKeyBlockHeader*)buf;
456 Debug("Keyblock is 0x%" PRIx64 " bytes\n", key_block->key_block_size);
457 now += key_block->key_block_size;
458 if (now > statbuf.st_size) {
459 error("key_block_size advances past the end of the blob\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700460 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700461 }
462
463 /* Skip the preamble */
464 preamble = (VbKernelPreambleHeader*)(buf + now);
465 Debug("Preamble is 0x%" PRIx64 " bytes\n", preamble->preamble_size);
466 now += preamble->preamble_size;
467 if (now > statbuf.st_size) {
468 error("preamble_size advances past the end of the blob\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700469 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700470 }
471
472 /* Go find the kernel blob */
473 Debug("kernel blob is at offset 0x%" PRIx64 "\n", now);
474 if (0 != fseek(fp, now, SEEK_SET)) {
475 error("Unable to seek to 0x%" PRIx64 " in %s: %s\n", now, filename,
476 strerror(errno));
vbendebb2b0fcc2010-07-15 15:09:47 -0700477 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700478 }
479
480 /* Remember what we've got */
481 bp = (blob_t *)Malloc(sizeof(blob_t));
482 if (!bp) {
483 error("Couldn't allocate bytes for blob_t.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700484 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700485 }
486
vbendebb2b0fcc2010-07-15 15:09:47 -0700487 bp->buf = buf;
488 bp->key_block = key_block;
489 bp->preamble = preamble;
490
Bill Richardsona08b5c92010-06-30 21:59:43 -0700491 bp->kernel_version = preamble->kernel_version;
492 bp->bootloader_address = preamble->bootloader_address;
493 bp->bootloader_size = preamble->bootloader_size;
494 bp->blob_size = preamble->body_signature.data_size;
495
496 Debug(" kernel_version = %d\n", bp->kernel_version);
497 Debug(" bootloader_address = 0x%" PRIx64 "\n", bp->bootloader_address);
498 Debug(" bootloader_size = 0x%" PRIx64 "\n", bp->bootloader_size);
499 Debug(" blob_size = 0x%" PRIx64 "\n", bp->blob_size);
500
Bill Richardson2f6a71f2010-10-14 09:25:39 -0700501 if (!bp->blob_size) {
502 error("No kernel blob found\n");
503 goto unwind_oldblob;
504 }
505
Bill Richardsona08b5c92010-06-30 21:59:43 -0700506 bp->blob = (uint8_t *)Malloc(bp->blob_size);
507 if (!bp->blob) {
508 error("Couldn't allocate 0x%" PRIx64 " bytes for blob_t.\n", bp->blob_size);
vbendebb2b0fcc2010-07-15 15:09:47 -0700509 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700510 }
511
512 /* read it in */
513 if (1 != fread(bp->blob, bp->blob_size, 1, fp)) {
Bill Richardson2f6a71f2010-10-14 09:25:39 -0700514 error("Unable to read kernel blob from %s: %s\n", filename, error_fread(fp));
vbendebb2b0fcc2010-07-15 15:09:47 -0700515 goto unwind_oldblob;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700516 }
517
vbendebb2b0fcc2010-07-15 15:09:47 -0700518 ret_error = 0;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700519
vbendebb2b0fcc2010-07-15 15:09:47 -0700520 /* done */
521unwind_oldblob:
522 fclose(fp);
523 if (ret_error) {
524 if (bp) {
525 FreeBlob(bp);
526 bp = NULL;
527 } else if (buf) {
528 Free(buf);
529 }
530 }
Bill Richardsona08b5c92010-06-30 21:59:43 -0700531 return bp;
532}
533
534
535/* Pack a .kernel */
536static int Pack(const char* outfile, const char* keyblock_file,
537 const char* signprivate, blob_t *bp, uint64_t pad,
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800538 int vblockonly,
539 uint64_t kernel_body_load_address) {
Bill Richardsona08b5c92010-06-30 21:59:43 -0700540 VbPrivateKey* signing_key;
541 VbSignature* body_sig;
542 VbKernelPreambleHeader* preamble;
543 VbKeyBlockHeader* key_block;
544 uint64_t key_block_size;
545 FILE* f;
546 uint64_t i;
547
548 if (!outfile) {
549 error("Must specify output filename\n");
550 return 1;
551 }
vbendebb2b0fcc2010-07-15 15:09:47 -0700552 if ((!keyblock_file && !bp->key_block) || !signprivate) {
Bill Richardsona08b5c92010-06-30 21:59:43 -0700553 error("Must specify all keys\n");
554 return 1;
555 }
556 if (!bp) {
557 error("Refusing to pack invalid kernel blob\n");
558 return 1;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700559 }
vbendebb2b0fcc2010-07-15 15:09:47 -0700560
561 /* Get the key block and read the private key. */
562 if (keyblock_file) {
563 key_block = (VbKeyBlockHeader*)ReadFile(keyblock_file, &key_block_size);
564 if (!key_block) {
565 error("Error reading key block.\n");
566 return 1;
567 }
568 } else {
569 key_block = bp->key_block;
570 key_block_size = key_block->key_block_size;
571 }
572
Bill Richardsona08b5c92010-06-30 21:59:43 -0700573 if (pad < key_block->key_block_size) {
574 error("Pad too small\n");
575 return 1;
576 }
577
Bill Richardsonabf05502010-07-01 10:22:06 -0700578 signing_key = PrivateKeyRead(signprivate);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700579 if (!signing_key) {
580 error("Error reading signing key.\n");
581 return 1;
582 }
583
Randall Spangler7d6898d2010-06-11 09:22:13 -0700584 /* Sign the kernel data */
Bill Richardsona08b5c92010-06-30 21:59:43 -0700585 body_sig = CalculateSignature(bp->blob, bp->blob_size, signing_key);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700586 if (!body_sig) {
587 error("Error calculating body signature\n");
588 return 1;
589 }
590
591 /* Create preamble */
Bill Richardsona08b5c92010-06-30 21:59:43 -0700592 preamble = CreateKernelPreamble(bp->kernel_version,
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800593 kernel_body_load_address,
Bill Richardsona08b5c92010-06-30 21:59:43 -0700594 bp->bootloader_address,
595 bp->bootloader_size,
Randall Spangler7d6898d2010-06-11 09:22:13 -0700596 body_sig,
597 pad - key_block_size,
598 signing_key);
599 if (!preamble) {
600 error("Error creating preamble.\n");
601 return 1;
602 }
603
604 /* Write the output file */
Bill Richardson249677d2010-06-23 11:16:37 -0700605 Debug("writing %s...\n", outfile);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700606 f = fopen(outfile, "wb");
607 if (!f) {
608 error("Can't open output file %s\n", outfile);
609 return 1;
610 }
Bill Richardson249677d2010-06-23 11:16:37 -0700611 Debug("0x%" PRIx64 " bytes of key_block\n", key_block_size);
612 Debug("0x%" PRIx64 " bytes of preamble\n", preamble->preamble_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700613 i = ((1 != fwrite(key_block, key_block_size, 1, f)) ||
Bill Richardsona08b5c92010-06-30 21:59:43 -0700614 (1 != fwrite(preamble, preamble->preamble_size, 1, f)));
Randall Spangler7d6898d2010-06-11 09:22:13 -0700615 if (i) {
616 error("Can't write output file %s\n", outfile);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700617 fclose(f);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700618 unlink(outfile);
619 return 1;
620 }
621
Bill Richardsona08b5c92010-06-30 21:59:43 -0700622 if (!vblockonly) {
623 Debug("0x%" PRIx64 " bytes of blob\n", bp->blob_size);
624 i = (1 != fwrite(bp->blob, bp->blob_size, 1, f));
625 if (i) {
626 error("Can't write output file %s\n", outfile);
627 fclose(f);
628 unlink(outfile);
629 return 1;
630 }
631 }
632
633 fclose(f);
634
Randall Spangler7d6898d2010-06-11 09:22:13 -0700635 /* Success */
636 return 0;
637}
638
vbendebb2b0fcc2010-07-15 15:09:47 -0700639/*
640 * Replace kernel command line in a blob representing a kernel.
641 */
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800642static int ReplaceConfig(blob_t* bp, const char* config_file,
643 uint64_t kernel_body_load_address)
vbendebb2b0fcc2010-07-15 15:09:47 -0700644{
645 uint8_t* new_conf;
646 uint64_t config_size;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700647
vbendebb2b0fcc2010-07-15 15:09:47 -0700648 if (!config_file) {
649 return 0;
650 }
651
652 new_conf = ReadConfigFile(config_file, &config_size);
653 if (!new_conf) {
654 return 1;
655 }
656
657 /* fill the config buffer with zeros */
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800658 Memset(BpCmdLineLocation(bp, kernel_body_load_address), 0, CROS_CONFIG_SIZE);
659 Memcpy(BpCmdLineLocation(bp, kernel_body_load_address),
660 new_conf, config_size);
vbendebb2b0fcc2010-07-15 15:09:47 -0700661 Free(new_conf);
662 return 0;
663}
664
Will Drewry9342f882010-10-26 10:22:05 -0500665static int Verify(const char* infile, const char* signpubkey, int verbose,
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800666 const char* key_block_file,
Randall Spanglerae87b922011-05-12 13:27:28 -0700667 uint64_t kernel_body_load_address, uint64_t min_version) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700668
669 VbKeyBlockHeader* key_block;
670 VbKernelPreambleHeader* preamble;
671 VbPublicKey* data_key;
Bill Richardson4f36ef32010-08-09 17:50:14 -0700672 VbPublicKey* sign_key = NULL;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700673 RSAPublicKey* rsa;
vbendebb2b0fcc2010-07-15 15:09:47 -0700674 blob_t* bp;
675 uint64_t now;
676 int rv = 1;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700677
Bill Richardson4f36ef32010-08-09 17:50:14 -0700678 if (!infile) {
679 error("Must specify filename\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700680 return 1;
681 }
682
683 /* Read public signing key */
Bill Richardson4f36ef32010-08-09 17:50:14 -0700684 if (signpubkey) {
685 sign_key = PublicKeyRead(signpubkey);
686 if (!sign_key) {
687 error("Error reading signpubkey.\n");
688 return 1;
689 }
Randall Spangler7d6898d2010-06-11 09:22:13 -0700690 }
691
692 /* Read blob */
vbendebb2b0fcc2010-07-15 15:09:47 -0700693 bp = OldBlob(infile);
694 if (!bp) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700695 error("Error reading input file\n");
696 return 1;
697 }
698
699 /* Verify key block */
vbendebb2b0fcc2010-07-15 15:09:47 -0700700 key_block = bp->key_block;
Randall Spangler138acfe2010-08-17 15:45:21 -0700701 if (0 != KeyBlockVerify(key_block, bp->blob_size, sign_key,
702 (sign_key ? 0 : 1))) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700703 error("Error verifying key block.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700704 goto verify_exit;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700705 }
vbendebb2b0fcc2010-07-15 15:09:47 -0700706 now = key_block->key_block_size;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700707
Will Drewry9342f882010-10-26 10:22:05 -0500708 if (key_block_file) {
709 FILE* f = NULL;
710 f = fopen(key_block_file, "wb");
711 if (!f) {
712 error("Can't open key block file %s\n", key_block_file);
713 return 1;
714 }
715 if (1 != fwrite(key_block, key_block->key_block_size, 1, f)) {
716 error("Can't write key block file %s\n", key_block_file);
717 return 1;
718 }
719 fclose(f);
720 }
721
Randall Spangler7d6898d2010-06-11 09:22:13 -0700722 printf("Key block:\n");
723 data_key = &key_block->data_key;
Bill Richardson4f36ef32010-08-09 17:50:14 -0700724 if (verbose)
725 printf(" Signature: %s\n", sign_key ? "valid" : "ignored");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700726 printf(" Size: 0x%" PRIx64 "\n", key_block->key_block_size);
Bill Richardson60bcbe32010-09-09 14:53:56 -0700727 printf(" Flags: %" PRIu64 " ", key_block->key_block_flags);
728 if (key_block->key_block_flags & KEY_BLOCK_FLAG_DEVELOPER_0)
729 printf(" !DEV");
730 if (key_block->key_block_flags & KEY_BLOCK_FLAG_DEVELOPER_1)
731 printf(" DEV");
732 if (key_block->key_block_flags & KEY_BLOCK_FLAG_RECOVERY_0)
733 printf(" !REC");
734 if (key_block->key_block_flags & KEY_BLOCK_FLAG_RECOVERY_1)
735 printf(" REC");
736 printf("\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700737 printf(" Data key algorithm: %" PRIu64 " %s\n", data_key->algorithm,
738 (data_key->algorithm < kNumAlgorithms ?
739 algo_strings[data_key->algorithm] : "(invalid)"));
740 printf(" Data key version: %" PRIu64 "\n", data_key->key_version);
Bill Richardson60bcbe32010-09-09 14:53:56 -0700741 printf(" Data key sha1sum: ");
742 PrintPubKeySha1Sum(data_key);
743 printf("\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700744
Randall Spanglerae87b922011-05-12 13:27:28 -0700745 if (data_key->key_version < (min_version >> 16)) {
746 error("Data key version %" PRIu64 " is lower than minimum %" PRIu64".\n",
747 data_key->key_version, (min_version >> 16));
748 goto verify_exit;
749 }
750
Randall Spangler7d6898d2010-06-11 09:22:13 -0700751 rsa = PublicKeyToRSA(&key_block->data_key);
752 if (!rsa) {
753 error("Error parsing data key.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700754 goto verify_exit;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700755 }
756
757 /* Verify preamble */
vbendebb2b0fcc2010-07-15 15:09:47 -0700758 preamble = bp->preamble;
Randall Spangler87c13d82010-07-19 10:35:40 -0700759 if (0 != VerifyKernelPreamble(
Bill Richardson4f36ef32010-08-09 17:50:14 -0700760 preamble, bp->blob_size - key_block->key_block_size, rsa)) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700761 error("Error verifying preamble.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700762 goto verify_exit;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700763 }
764 now += preamble->preamble_size;
765
766 printf("Preamble:\n");
Bill Richardsona08b5c92010-06-30 21:59:43 -0700767 printf(" Size: 0x%" PRIx64 "\n", preamble->preamble_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700768 printf(" Header version: %" PRIu32 ".%" PRIu32"\n",
769 preamble->header_version_major, preamble->header_version_minor);
770 printf(" Kernel version: %" PRIu64 "\n", preamble->kernel_version);
Bill Richardson249677d2010-06-23 11:16:37 -0700771 printf(" Body load address: 0x%" PRIx64 "\n", preamble->body_load_address);
772 printf(" Body size: 0x%" PRIx64 "\n",
Randall Spangler7d6898d2010-06-11 09:22:13 -0700773 preamble->body_signature.data_size);
Randall Spangler87c13d82010-07-19 10:35:40 -0700774 printf(" Bootloader address: 0x%" PRIx64 "\n",
775 preamble->bootloader_address);
Bill Richardson249677d2010-06-23 11:16:37 -0700776 printf(" Bootloader size: 0x%" PRIx64 "\n", preamble->bootloader_size);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700777
Randall Spanglerae87b922011-05-12 13:27:28 -0700778 if (preamble->kernel_version < (min_version & 0xFFFF)) {
779 error("Kernel version %" PRIu64 " is lower than minimum %" PRIu64 ".\n",
780 preamble->kernel_version, (min_version & 0xFFFF));
781 goto verify_exit;
782 }
783
Randall Spangler7d6898d2010-06-11 09:22:13 -0700784 /* Verify body */
Randall Spangler87c13d82010-07-19 10:35:40 -0700785 if (0 != VerifyData(bp->blob, bp->blob_size, &preamble->body_signature,
786 rsa)) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700787 error("Error verifying kernel body.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700788 goto verify_exit;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700789 }
790 printf("Body verification succeeded.\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700791
792 rv = 0;
793
794 if (!verbose) {
795 goto verify_exit;
796 }
797
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800798 printf("Config:\n%s\n", BpCmdLineLocation(bp, kernel_body_load_address));
vbendebb2b0fcc2010-07-15 15:09:47 -0700799
800verify_exit:
801 FreeBlob(bp);
802 return rv;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700803}
804
805
806int main(int argc, char* argv[]) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700807 char* filename = NULL;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700808 char* oldfile = NULL;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700809 char* key_block_file = NULL;
810 char* signpubkey = NULL;
811 char* signprivate = NULL;
vbendeb00b90882010-10-21 13:46:16 -0700812 int version = -1;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700813 char* vmlinuz = NULL;
814 char* bootloader = NULL;
815 char* config_file = NULL;
Che-Liang Chiou03762032011-02-22 11:16:51 +0800816 int arch = ARCH_X86;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700817 int vblockonly = 0;
vbendebb2b0fcc2010-07-15 15:09:47 -0700818 int verbose = 0;
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800819 uint64_t kernel_body_load_address = CROS_32BIT_ENTRY_ADDR;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700820 uint64_t pad = DEFAULT_PADDING;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700821 int mode = 0;
822 int parse_error = 0;
Randall Spanglerae87b922011-05-12 13:27:28 -0700823 uint64_t min_version = 0;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700824 char* e;
Bill Richardsona08b5c92010-06-30 21:59:43 -0700825 int i,r;
826 blob_t *bp;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700827
Bill Richardsona08b5c92010-06-30 21:59:43 -0700828
829 char *progname = strrchr(argv[0], '/');
830 if (progname)
831 progname++;
832 else
833 progname = argv[0];
834
vbendebb2b0fcc2010-07-15 15:09:47 -0700835 while (((i = getopt_long(argc, argv, ":", long_opts, NULL)) != -1) &&
836 !parse_error) {
Randall Spangler7d6898d2010-06-11 09:22:13 -0700837 switch (i) {
vbendebb2b0fcc2010-07-15 15:09:47 -0700838 default:
Randall Spangler7d6898d2010-06-11 09:22:13 -0700839 case '?':
840 /* Unhandled option */
Randall Spangler7d6898d2010-06-11 09:22:13 -0700841 parse_error = 1;
842 break;
843
Bill Richardson4f36ef32010-08-09 17:50:14 -0700844 case 0:
845 /* silently handled option */
846 break;
847
Randall Spangler7d6898d2010-06-11 09:22:13 -0700848 case OPT_MODE_PACK:
Bill Richardsona08b5c92010-06-30 21:59:43 -0700849 case OPT_MODE_REPACK:
Randall Spangler7d6898d2010-06-11 09:22:13 -0700850 case OPT_MODE_VERIFY:
vbendebb2b0fcc2010-07-15 15:09:47 -0700851 if (mode && (mode != i)) {
852 fprintf(stderr, "Only single mode can be specified\n");
853 parse_error = 1;
854 break;
855 }
Randall Spangler7d6898d2010-06-11 09:22:13 -0700856 mode = i;
857 filename = optarg;
858 break;
859
Che-Liang Chiou03762032011-02-22 11:16:51 +0800860 case OPT_ARCH:
861 if (!strcasecmp(optarg, "x86"))
862 arch = ARCH_X86;
863 else if (!strcasecmp(optarg, "arm"))
864 arch = ARCH_ARM;
865 else {
866 fprintf(stderr, "Unknown architecture string: %s\n", optarg);
867 parse_error = 1;
868 }
869 break;
870
Bill Richardsona08b5c92010-06-30 21:59:43 -0700871 case OPT_OLDBLOB:
872 oldfile = optarg;
873 break;
874
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800875 case OPT_KLOADADDR:
876 kernel_body_load_address = strtoul(optarg, &e, 0);
877 if (!*optarg || (e && *e)) {
878 fprintf(stderr, "Invalid --kloadaddr\n");
879 parse_error = 1;
880 }
881 break;
882
Randall Spangler7d6898d2010-06-11 09:22:13 -0700883 case OPT_KEYBLOCK:
884 key_block_file = optarg;
885 break;
886
887 case OPT_SIGNPUBKEY:
888 signpubkey = optarg;
889 break;
890
891 case OPT_SIGNPRIVATE:
892 signprivate = optarg;
893 break;
894
895 case OPT_VMLINUZ:
896 vmlinuz = optarg;
897 break;
898
899 case OPT_BOOTLOADER:
900 bootloader = optarg;
901 break;
902
903 case OPT_CONFIG:
904 config_file = optarg;
905 break;
906
Bill Richardsona08b5c92010-06-30 21:59:43 -0700907 case OPT_VBLOCKONLY:
908 vblockonly = 1;
909 break;
910
Randall Spangler7d6898d2010-06-11 09:22:13 -0700911 case OPT_VERSION:
912 version = strtoul(optarg, &e, 0);
913 if (!*optarg || (e && *e)) {
Bill Richardsona08b5c92010-06-30 21:59:43 -0700914 fprintf(stderr, "Invalid --version\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700915 parse_error = 1;
916 }
917 break;
918
Randall Spanglerae87b922011-05-12 13:27:28 -0700919 case OPT_MINVERSION:
920 min_version = strtoul(optarg, &e, 0);
921 if (!*optarg || (e && *e)) {
922 fprintf(stderr, "Invalid --minversion\n");
923 parse_error = 1;
924 }
925 break;
926
Randall Spangler7d6898d2010-06-11 09:22:13 -0700927 case OPT_PAD:
928 pad = strtoul(optarg, &e, 0);
929 if (!*optarg || (e && *e)) {
Bill Richardsona08b5c92010-06-30 21:59:43 -0700930 fprintf(stderr, "Invalid --pad\n");
Randall Spangler7d6898d2010-06-11 09:22:13 -0700931 parse_error = 1;
932 }
933 break;
vbendebb2b0fcc2010-07-15 15:09:47 -0700934
935 case OPT_VERBOSE:
936 verbose = 1;
937 break;
Randall Spangler7d6898d2010-06-11 09:22:13 -0700938 }
939 }
940
941 if (parse_error)
Bill Richardsona08b5c92010-06-30 21:59:43 -0700942 return PrintHelp(progname);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700943
944 switch(mode) {
945 case OPT_MODE_PACK:
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800946 bp = NewBlob(version, vmlinuz, bootloader, config_file, arch,
947 kernel_body_load_address);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700948 if (!bp)
949 return 1;
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800950 r = Pack(filename, key_block_file, signprivate, bp, pad, vblockonly,
951 kernel_body_load_address);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700952 FreeBlob(bp);
953 return r;
954
955 case OPT_MODE_REPACK:
vbendeb00b90882010-10-21 13:46:16 -0700956 if (!config_file && !key_block_file && (version<0)) {
vbendebb2b0fcc2010-07-15 15:09:47 -0700957 fprintf(stderr,
vbendeb858fffb2010-10-06 09:51:44 -0700958 "You must supply at least one of "
959 "--config, --keyblock or --version\n");
vbendebb2b0fcc2010-07-15 15:09:47 -0700960 return 1;
961 }
962
Bill Richardsona08b5c92010-06-30 21:59:43 -0700963 bp = OldBlob(oldfile);
964 if (!bp)
965 return 1;
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800966 r = ReplaceConfig(bp, config_file, kernel_body_load_address);
vbendebb2b0fcc2010-07-15 15:09:47 -0700967 if (!r) {
vbendeb00b90882010-10-21 13:46:16 -0700968 if (version >= 0) {
Che-Liang Chiou03762032011-02-22 11:16:51 +0800969 bp->kernel_version = (uint64_t) version;
vbendeb858fffb2010-10-06 09:51:44 -0700970 }
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800971 r = Pack(filename, key_block_file, signprivate, bp, pad, vblockonly,
972 kernel_body_load_address);
vbendebb2b0fcc2010-07-15 15:09:47 -0700973 }
Bill Richardsona08b5c92010-06-30 21:59:43 -0700974 FreeBlob(bp);
975 return r;
976
Randall Spangler7d6898d2010-06-11 09:22:13 -0700977 case OPT_MODE_VERIFY:
Che-Liang Chiou2c0711b2011-03-22 13:15:19 +0800978 return Verify(filename, signpubkey, verbose, key_block_file,
Randall Spanglerae87b922011-05-12 13:27:28 -0700979 kernel_body_load_address, min_version);
Bill Richardsona08b5c92010-06-30 21:59:43 -0700980
Randall Spangler7d6898d2010-06-11 09:22:13 -0700981 default:
Bill Richardsona08b5c92010-06-30 21:59:43 -0700982 fprintf(stderr,
983 "You must specify a mode: --pack, --repack or --verify\n");
984 return PrintHelp(progname);
Randall Spangler7d6898d2010-06-11 09:22:13 -0700985 }
986}