Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 1 | /* 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 Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 8 | #include <errno.h> |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 9 | #include <getopt.h> |
| 10 | #include <inttypes.h> /* For PRIu64 */ |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 11 | #include <stdarg.h> |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 12 | #include <stddef.h> |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 15 | #include <string.h> |
| 16 | #include <sys/stat.h> |
| 17 | #include <sys/types.h> |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 18 | #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 Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 26 | /* Global opt */ |
| 27 | static int opt_debug = 0; |
| 28 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 29 | static const int DEFAULT_PADDING = 65536; |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 30 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 31 | /* Command line options */ |
| 32 | enum { |
| 33 | OPT_MODE_PACK = 1000, |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 34 | OPT_MODE_REPACK, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 35 | OPT_MODE_VERIFY, |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 36 | OPT_OLDBLOB, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 37 | OPT_KEYBLOCK, |
| 38 | OPT_SIGNPUBKEY, |
| 39 | OPT_SIGNPRIVATE, |
| 40 | OPT_VERSION, |
| 41 | OPT_VMLINUZ, |
| 42 | OPT_BOOTLOADER, |
| 43 | OPT_CONFIG, |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 44 | OPT_VBLOCKONLY, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 45 | OPT_PAD, |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 46 | OPT_VERBOSE, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static struct option long_opts[] = { |
| 50 | {"pack", 1, 0, OPT_MODE_PACK }, |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 51 | {"repack", 1, 0, OPT_MODE_REPACK }, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 52 | {"verify", 1, 0, OPT_MODE_VERIFY }, |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 53 | {"oldblob", 1, 0, OPT_OLDBLOB }, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 54 | {"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 Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 61 | {"vblockonly", 0, 0, OPT_VBLOCKONLY }, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 62 | {"pad", 1, 0, OPT_PAD }, |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 63 | {"verbose", 0, 0, OPT_VERBOSE }, |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 64 | {"debug", 0, &opt_debug, 1 }, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 65 | {NULL, 0, 0, 0} |
| 66 | }; |
| 67 | |
| 68 | |
| 69 | /* Print help and return error */ |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 70 | static 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 Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame] | 79 | " --signprivate <file>" |
| 80 | " Private key to sign kernel data, in .vbprivk format\n" |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 81 | " --version <number> Kernel version\n" |
| 82 | " --vmlinuz <file> Linux kernel bzImage file\n" |
| 83 | " --bootloader <file> Bootloader stub\n" |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 84 | " --config <file> Command line file\n" |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 85 | "\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" |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 94 | " Required parameters (of --keyblock and --config at least " |
| 95 | "one is required):\n" |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 96 | " --keyblock <file> Key block in .keyblock format\n" |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame] | 97 | " --signprivate <file>" |
| 98 | " Private key to sign kernel data, in .vbprivk format\n" |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 99 | " --oldblob <file> Previously packed kernel blob\n" |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 100 | " --config <file> New command line file\n" |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 101 | "\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" |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 110 | " Optional:\n" |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame] | 111 | " --signpubkey <file>" |
| 112 | " Public key to verify kernel keyblock, in .vbpubk format\n" |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 113 | " --verbose Print a more detailed report\n" |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 114 | "\n", |
| 115 | progname); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 116 | return 1; |
| 117 | } |
| 118 | |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 119 | static 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 Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 130 | |
| 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. */ |
| 135 | static 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. */ |
| 145 | static 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 Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 164 | typedef 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; |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 172 | |
| 173 | /* these fields are not always initialized */ |
| 174 | VbKernelPreambleHeader* preamble; |
| 175 | VbKeyBlockHeader* key_block; |
| 176 | uint8_t *buf; |
| 177 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 178 | } blob_t; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 179 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 180 | /* Given a blob return the location of the kernel command line buffer. */ |
| 181 | static 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 Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 186 | |
| 187 | static void FreeBlob(blob_t *bp) { |
| 188 | if (bp) { |
| 189 | if (bp->blob) |
| 190 | Free(bp->blob); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 191 | if (bp->buf) |
| 192 | Free(bp->buf); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 193 | Free(bp); |
| 194 | } |
| 195 | } |
| 196 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 197 | /* |
| 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 | */ |
| 204 | static 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 Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 226 | /* Create a blob from its components */ |
| 227 | static 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 Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 232 | struct linux_kernel_header *lh = 0; |
| 233 | struct linux_kernel_params *params = 0; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 234 | uint8_t* config_buf; |
| 235 | uint64_t config_size; |
| 236 | uint8_t* bootloader_buf; |
| 237 | uint64_t bootloader_size; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 238 | 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 Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 244 | uint64_t now = 0; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 245 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 246 | if (!vmlinuz || !bootloader_file || !config_file) { |
| 247 | error("Must specify all input files\n"); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 248 | return 0; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 251 | bp = (blob_t *)Malloc(sizeof(blob_t)); |
| 252 | if (!bp) { |
| 253 | error("Couldn't allocate bytes for blob_t.\n"); |
| 254 | return 0; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 255 | } |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 256 | |
| 257 | Memset(bp, 0, sizeof(*bp)); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 258 | bp->kernel_version = version; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 259 | |
| 260 | /* Read the config file */ |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 261 | Debug("Reading %s\n", config_file); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 262 | config_buf = ReadConfigFile(config_file, &config_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 263 | if (!config_buf) |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 264 | return 0; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 265 | |
| 266 | /* Read the bootloader */ |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 267 | Debug("Reading %s\n", bootloader_file); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 268 | bootloader_buf = ReadFile(bootloader_file, &bootloader_size); |
| 269 | if (!bootloader_buf) |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 270 | return 0; |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 271 | Debug(" bootloader file size=0x%" PRIx64 "\n", bootloader_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 272 | |
| 273 | /* Read the kernel */ |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 274 | Debug("Reading %s\n", vmlinuz); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 275 | kernel_buf = ReadFile(vmlinuz, &kernel_size); |
| 276 | if (!kernel_buf) |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 277 | return 0; |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 278 | Debug(" kernel file size=0x%" PRIx64 "\n", kernel_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 279 | if (!kernel_size) { |
| 280 | error("Empty kernel file\n"); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 281 | return 0; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 282 | } |
| 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 Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 290 | return 0; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 291 | } |
| 292 | kernel32_size = kernel_size - kernel32_start; |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 293 | Debug(" kernel32_start=0x%" PRIx64 "\n", kernel32_start); |
| 294 | Debug(" kernel32_size=0x%" PRIx64 "\n", kernel32_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 295 | |
| 296 | /* Allocate and zero the blob we need. */ |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 297 | bp->blob_size = roundup(kernel32_size, CROS_ALIGN) + |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 298 | CROS_CONFIG_SIZE + |
| 299 | CROS_PARAMS_SIZE + |
| 300 | roundup(bootloader_size, CROS_ALIGN); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 301 | blob = (uint8_t *)Malloc(bp->blob_size); |
| 302 | Debug("blob_size=0x%" PRIx64 "\n", bp->blob_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 303 | if (!blob) { |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 304 | error("Couldn't allocate %ld bytes.\n", bp->blob_size); |
| 305 | return 0; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 306 | } |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 307 | Memset(blob, 0, bp->blob_size); |
| 308 | bp->blob = blob; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 309 | |
| 310 | /* Copy the 32-bit kernel. */ |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 311 | Debug("kernel goes at blob+=0x%" PRIx64 "\n", now); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 312 | if (kernel32_size) |
| 313 | Memcpy(blob + now, kernel_buf + kernel32_start, kernel32_size); |
| 314 | now += roundup(now + kernel32_size, CROS_ALIGN); |
| 315 | |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 316 | Debug("config goes at blob+0x%" PRIx64 "\n", now); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 317 | /* 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 Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 320 | Debug(" cmdline_addr=0x%" PRIx64 "\n", cmdline_addr); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 321 | |
| 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 Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 329 | Debug("params goes at blob+=0x%" PRIx64 "\n", now); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 330 | 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 Chiou | 475bf44 | 2010-08-23 11:20:44 +0800 | [diff] [blame] | 338 | /* 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 Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 346 | now += CROS_PARAMS_SIZE; |
| 347 | |
| 348 | /* Finally, append the bootloader. Remember where it will load in |
| 349 | * memory, too. */ |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 350 | Debug("bootloader goes at blob+=0x%" PRIx64 "\n", now); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 351 | 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 Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 355 | if (bootloader_size) |
| 356 | Memcpy(blob + now, bootloader_buf, bootloader_size); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 357 | now += bp->bootloader_size; |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 358 | Debug("end of blob is 0x%" PRIx64 "\n", now); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 359 | |
| 360 | /* Free input buffers */ |
| 361 | Free(kernel_buf); |
| 362 | Free(config_buf); |
| 363 | Free(bootloader_buf); |
| 364 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 365 | /* Success */ |
| 366 | return bp; |
| 367 | } |
| 368 | |
| 369 | |
| 370 | /* Pull the blob_t stuff out of a prepacked kernel blob file */ |
| 371 | static blob_t *OldBlob(const char* filename) { |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 372 | FILE* fp = NULL; |
| 373 | blob_t *bp = NULL; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 374 | struct stat statbuf; |
| 375 | VbKeyBlockHeader* key_block; |
| 376 | VbKernelPreambleHeader* preamble; |
| 377 | uint64_t now = 0; |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 378 | uint8_t* buf = NULL; |
| 379 | int ret_error = 1; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 380 | |
| 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 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 404 | 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 Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 411 | error("Unable to read header from %s: %s\n", filename, strerror(errno)); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 412 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 413 | } |
| 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"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 421 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 422 | } |
| 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"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 430 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 431 | } |
| 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)); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 438 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 439 | } |
| 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"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 445 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 446 | } |
| 447 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 448 | bp->buf = buf; |
| 449 | bp->key_block = key_block; |
| 450 | bp->preamble = preamble; |
| 451 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 452 | 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); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 465 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 466 | } |
| 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)); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 471 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 472 | } |
| 473 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 474 | ret_error = 0; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 475 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 476 | /* done */ |
| 477 | unwind_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 Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 487 | return bp; |
| 488 | } |
| 489 | |
| 490 | |
| 491 | /* Pack a .kernel */ |
| 492 | static 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 | } |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 507 | if ((!keyblock_file && !bp->key_block) || !signprivate) { |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 508 | 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 Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 514 | } |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 515 | |
| 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 Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 528 | if (pad < key_block->key_block_size) { |
| 529 | error("Pad too small\n"); |
| 530 | return 1; |
| 531 | } |
| 532 | |
Bill Richardson | abf0550 | 2010-07-01 10:22:06 -0700 | [diff] [blame] | 533 | signing_key = PrivateKeyRead(signprivate); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 534 | if (!signing_key) { |
| 535 | error("Error reading signing key.\n"); |
| 536 | return 1; |
| 537 | } |
| 538 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 539 | /* Sign the kernel data */ |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 540 | body_sig = CalculateSignature(bp->blob, bp->blob_size, signing_key); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 541 | if (!body_sig) { |
| 542 | error("Error calculating body signature\n"); |
| 543 | return 1; |
| 544 | } |
| 545 | |
| 546 | /* Create preamble */ |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 547 | preamble = CreateKernelPreamble(bp->kernel_version, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 548 | CROS_32BIT_ENTRY_ADDR, |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 549 | bp->bootloader_address, |
| 550 | bp->bootloader_size, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 551 | 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 Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 560 | Debug("writing %s...\n", outfile); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 561 | f = fopen(outfile, "wb"); |
| 562 | if (!f) { |
| 563 | error("Can't open output file %s\n", outfile); |
| 564 | return 1; |
| 565 | } |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 566 | Debug("0x%" PRIx64 " bytes of key_block\n", key_block_size); |
| 567 | Debug("0x%" PRIx64 " bytes of preamble\n", preamble->preamble_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 568 | i = ((1 != fwrite(key_block, key_block_size, 1, f)) || |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 569 | (1 != fwrite(preamble, preamble->preamble_size, 1, f))); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 570 | if (i) { |
| 571 | error("Can't write output file %s\n", outfile); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 572 | fclose(f); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 573 | unlink(outfile); |
| 574 | return 1; |
| 575 | } |
| 576 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 577 | 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 Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 590 | /* Success */ |
| 591 | return 0; |
| 592 | } |
| 593 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 594 | /* |
| 595 | * Replace kernel command line in a blob representing a kernel. |
| 596 | */ |
| 597 | static int ReplaceConfig(blob_t* bp, const char* config_file) |
| 598 | { |
| 599 | uint8_t* new_conf; |
| 600 | uint64_t config_size; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 601 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 602 | 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 | |
| 618 | static int Verify(const char* infile, const char* signpubkey, int verbose) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 619 | |
| 620 | VbKeyBlockHeader* key_block; |
| 621 | VbKernelPreambleHeader* preamble; |
| 622 | VbPublicKey* data_key; |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame] | 623 | VbPublicKey* sign_key = NULL; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 624 | RSAPublicKey* rsa; |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 625 | blob_t* bp; |
| 626 | uint64_t now; |
| 627 | int rv = 1; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 628 | |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame] | 629 | if (!infile) { |
| 630 | error("Must specify filename\n"); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 631 | return 1; |
| 632 | } |
| 633 | |
| 634 | /* Read public signing key */ |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame] | 635 | if (signpubkey) { |
| 636 | sign_key = PublicKeyRead(signpubkey); |
| 637 | if (!sign_key) { |
| 638 | error("Error reading signpubkey.\n"); |
| 639 | return 1; |
| 640 | } |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /* Read blob */ |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 644 | bp = OldBlob(infile); |
| 645 | if (!bp) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 646 | error("Error reading input file\n"); |
| 647 | return 1; |
| 648 | } |
| 649 | |
| 650 | /* Verify key block */ |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 651 | key_block = bp->key_block; |
Randall Spangler | 138acfe | 2010-08-17 15:45:21 -0700 | [diff] [blame] | 652 | if (0 != KeyBlockVerify(key_block, bp->blob_size, sign_key, |
| 653 | (sign_key ? 0 : 1))) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 654 | error("Error verifying key block.\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 655 | goto verify_exit; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 656 | } |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 657 | now = key_block->key_block_size; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 658 | |
| 659 | printf("Key block:\n"); |
| 660 | data_key = &key_block->data_key; |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame] | 661 | if (verbose) |
| 662 | printf(" Signature: %s\n", sign_key ? "valid" : "ignored"); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 663 | printf(" Size: 0x%" PRIx64 "\n", key_block->key_block_size); |
Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame^] | 664 | printf(" Flags: %" PRIu64 " ", key_block->key_block_flags); |
| 665 | if (key_block->key_block_flags & KEY_BLOCK_FLAG_DEVELOPER_0) |
| 666 | printf(" !DEV"); |
| 667 | if (key_block->key_block_flags & KEY_BLOCK_FLAG_DEVELOPER_1) |
| 668 | printf(" DEV"); |
| 669 | if (key_block->key_block_flags & KEY_BLOCK_FLAG_RECOVERY_0) |
| 670 | printf(" !REC"); |
| 671 | if (key_block->key_block_flags & KEY_BLOCK_FLAG_RECOVERY_1) |
| 672 | printf(" REC"); |
| 673 | printf("\n"); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 674 | printf(" Data key algorithm: %" PRIu64 " %s\n", data_key->algorithm, |
| 675 | (data_key->algorithm < kNumAlgorithms ? |
| 676 | algo_strings[data_key->algorithm] : "(invalid)")); |
| 677 | printf(" Data key version: %" PRIu64 "\n", data_key->key_version); |
Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame^] | 678 | printf(" Data key sha1sum: "); |
| 679 | PrintPubKeySha1Sum(data_key); |
| 680 | printf("\n"); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 681 | |
| 682 | rsa = PublicKeyToRSA(&key_block->data_key); |
| 683 | if (!rsa) { |
| 684 | error("Error parsing data key.\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 685 | goto verify_exit; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | /* Verify preamble */ |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 689 | preamble = bp->preamble; |
Randall Spangler | 87c13d8 | 2010-07-19 10:35:40 -0700 | [diff] [blame] | 690 | if (0 != VerifyKernelPreamble( |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame] | 691 | preamble, bp->blob_size - key_block->key_block_size, rsa)) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 692 | error("Error verifying preamble.\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 693 | goto verify_exit; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 694 | } |
| 695 | now += preamble->preamble_size; |
| 696 | |
| 697 | printf("Preamble:\n"); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 698 | printf(" Size: 0x%" PRIx64 "\n", preamble->preamble_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 699 | printf(" Header version: %" PRIu32 ".%" PRIu32"\n", |
| 700 | preamble->header_version_major, preamble->header_version_minor); |
| 701 | printf(" Kernel version: %" PRIu64 "\n", preamble->kernel_version); |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 702 | printf(" Body load address: 0x%" PRIx64 "\n", preamble->body_load_address); |
| 703 | printf(" Body size: 0x%" PRIx64 "\n", |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 704 | preamble->body_signature.data_size); |
Randall Spangler | 87c13d8 | 2010-07-19 10:35:40 -0700 | [diff] [blame] | 705 | printf(" Bootloader address: 0x%" PRIx64 "\n", |
| 706 | preamble->bootloader_address); |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 707 | printf(" Bootloader size: 0x%" PRIx64 "\n", preamble->bootloader_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 708 | |
| 709 | /* Verify body */ |
Randall Spangler | 87c13d8 | 2010-07-19 10:35:40 -0700 | [diff] [blame] | 710 | if (0 != VerifyData(bp->blob, bp->blob_size, &preamble->body_signature, |
| 711 | rsa)) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 712 | error("Error verifying kernel body.\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 713 | goto verify_exit; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 714 | } |
| 715 | printf("Body verification succeeded.\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 716 | |
| 717 | rv = 0; |
| 718 | |
| 719 | if (!verbose) { |
| 720 | goto verify_exit; |
| 721 | } |
| 722 | |
| 723 | printf("Config:\n%s\n", BpCmdLineLocation(bp)); |
| 724 | |
| 725 | verify_exit: |
| 726 | FreeBlob(bp); |
| 727 | return rv; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | |
| 731 | int main(int argc, char* argv[]) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 732 | char* filename = NULL; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 733 | char* oldfile = NULL; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 734 | char* key_block_file = NULL; |
| 735 | char* signpubkey = NULL; |
| 736 | char* signprivate = NULL; |
| 737 | uint64_t version = 0; |
| 738 | char* vmlinuz = NULL; |
| 739 | char* bootloader = NULL; |
| 740 | char* config_file = NULL; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 741 | int vblockonly = 0; |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 742 | int verbose = 0; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 743 | uint64_t pad = DEFAULT_PADDING; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 744 | int mode = 0; |
| 745 | int parse_error = 0; |
| 746 | char* e; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 747 | int i,r; |
| 748 | blob_t *bp; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 749 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 750 | |
| 751 | char *progname = strrchr(argv[0], '/'); |
| 752 | if (progname) |
| 753 | progname++; |
| 754 | else |
| 755 | progname = argv[0]; |
| 756 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 757 | while (((i = getopt_long(argc, argv, ":", long_opts, NULL)) != -1) && |
| 758 | !parse_error) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 759 | switch (i) { |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 760 | default: |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 761 | case '?': |
| 762 | /* Unhandled option */ |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 763 | parse_error = 1; |
| 764 | break; |
| 765 | |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame] | 766 | case 0: |
| 767 | /* silently handled option */ |
| 768 | break; |
| 769 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 770 | case OPT_MODE_PACK: |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 771 | case OPT_MODE_REPACK: |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 772 | case OPT_MODE_VERIFY: |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 773 | if (mode && (mode != i)) { |
| 774 | fprintf(stderr, "Only single mode can be specified\n"); |
| 775 | parse_error = 1; |
| 776 | break; |
| 777 | } |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 778 | mode = i; |
| 779 | filename = optarg; |
| 780 | break; |
| 781 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 782 | case OPT_OLDBLOB: |
| 783 | oldfile = optarg; |
| 784 | break; |
| 785 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 786 | case OPT_KEYBLOCK: |
| 787 | key_block_file = optarg; |
| 788 | break; |
| 789 | |
| 790 | case OPT_SIGNPUBKEY: |
| 791 | signpubkey = optarg; |
| 792 | break; |
| 793 | |
| 794 | case OPT_SIGNPRIVATE: |
| 795 | signprivate = optarg; |
| 796 | break; |
| 797 | |
| 798 | case OPT_VMLINUZ: |
| 799 | vmlinuz = optarg; |
| 800 | break; |
| 801 | |
| 802 | case OPT_BOOTLOADER: |
| 803 | bootloader = optarg; |
| 804 | break; |
| 805 | |
| 806 | case OPT_CONFIG: |
| 807 | config_file = optarg; |
| 808 | break; |
| 809 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 810 | case OPT_VBLOCKONLY: |
| 811 | vblockonly = 1; |
| 812 | break; |
| 813 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 814 | case OPT_VERSION: |
| 815 | version = strtoul(optarg, &e, 0); |
| 816 | if (!*optarg || (e && *e)) { |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 817 | fprintf(stderr, "Invalid --version\n"); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 818 | parse_error = 1; |
| 819 | } |
| 820 | break; |
| 821 | |
| 822 | case OPT_PAD: |
| 823 | pad = strtoul(optarg, &e, 0); |
| 824 | if (!*optarg || (e && *e)) { |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 825 | fprintf(stderr, "Invalid --pad\n"); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 826 | parse_error = 1; |
| 827 | } |
| 828 | break; |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 829 | |
| 830 | case OPT_VERBOSE: |
| 831 | verbose = 1; |
| 832 | break; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 833 | } |
| 834 | } |
| 835 | |
| 836 | if (parse_error) |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 837 | return PrintHelp(progname); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 838 | |
| 839 | switch(mode) { |
| 840 | case OPT_MODE_PACK: |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 841 | bp = NewBlob(version, vmlinuz, bootloader, config_file); |
| 842 | if (!bp) |
| 843 | return 1; |
| 844 | r = Pack(filename, key_block_file, signprivate, bp, pad, vblockonly); |
| 845 | FreeBlob(bp); |
| 846 | return r; |
| 847 | |
| 848 | case OPT_MODE_REPACK: |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 849 | if (!config_file && !key_block_file) { |
| 850 | fprintf(stderr, |
| 851 | "You must supply at least one of --config and --keyblock\n"); |
| 852 | return 1; |
| 853 | } |
| 854 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 855 | bp = OldBlob(oldfile); |
| 856 | if (!bp) |
| 857 | return 1; |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 858 | r = ReplaceConfig(bp, config_file); |
| 859 | if (!r) { |
| 860 | r = Pack(filename, key_block_file, signprivate, bp, pad, vblockonly); |
| 861 | } |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 862 | FreeBlob(bp); |
| 863 | return r; |
| 864 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 865 | case OPT_MODE_VERIFY: |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 866 | return Verify(filename, signpubkey, verbose); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 867 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 868 | default: |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 869 | fprintf(stderr, |
| 870 | "You must specify a mode: --pack, --repack or --verify\n"); |
| 871 | return PrintHelp(progname); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 872 | } |
| 873 | } |