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; |
| 338 | now += CROS_PARAMS_SIZE; |
| 339 | |
| 340 | /* Finally, append the bootloader. Remember where it will load in |
| 341 | * memory, too. */ |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 342 | Debug("bootloader goes at blob+=0x%" PRIx64 "\n", now); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 343 | bp->bootloader_address = CROS_32BIT_ENTRY_ADDR + now; |
| 344 | bp->bootloader_size = roundup(bootloader_size, CROS_ALIGN); |
| 345 | Debug(" bootloader_address=0x%" PRIx64 "\n", bp->bootloader_address); |
| 346 | Debug(" bootloader_size=0x%" PRIx64 "\n", bp->bootloader_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 347 | if (bootloader_size) |
| 348 | Memcpy(blob + now, bootloader_buf, bootloader_size); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 349 | now += bp->bootloader_size; |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 350 | Debug("end of blob is 0x%" PRIx64 "\n", now); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 351 | |
| 352 | /* Free input buffers */ |
| 353 | Free(kernel_buf); |
| 354 | Free(config_buf); |
| 355 | Free(bootloader_buf); |
| 356 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 357 | /* Success */ |
| 358 | return bp; |
| 359 | } |
| 360 | |
| 361 | |
| 362 | /* Pull the blob_t stuff out of a prepacked kernel blob file */ |
| 363 | static blob_t *OldBlob(const char* filename) { |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 364 | FILE* fp = NULL; |
| 365 | blob_t *bp = NULL; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 366 | struct stat statbuf; |
| 367 | VbKeyBlockHeader* key_block; |
| 368 | VbKernelPreambleHeader* preamble; |
| 369 | uint64_t now = 0; |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 370 | uint8_t* buf = NULL; |
| 371 | int ret_error = 1; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 372 | |
| 373 | if (!filename) { |
| 374 | error("Must specify prepacked blob to read\n"); |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | if (0 != stat(filename, &statbuf)) { |
| 379 | error("unable to stat %s: %s\n", filename, strerror(errno)); |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | Debug("%s size is 0x%" PRIx64 "\n", filename, statbuf.st_size); |
| 384 | if (statbuf.st_size < DEFAULT_PADDING) { |
| 385 | error("%s is too small to be a valid kernel blob\n"); |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | Debug("Reading %s\n", filename); |
| 390 | fp = fopen(filename, "rb"); |
| 391 | if (!fp) { |
| 392 | error("Unable to open file %s: %s\n", filename, strerror(errno)); |
| 393 | return 0; |
| 394 | } |
| 395 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 396 | buf = Malloc(DEFAULT_PADDING); |
| 397 | if (!buf) { |
| 398 | error("Unable to allocate padding\n"); |
| 399 | goto unwind_oldblob; |
| 400 | } |
| 401 | |
| 402 | if (1 != fread(buf, DEFAULT_PADDING, 1, fp)) { |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 403 | error("Unable to read header from %s: %s\n", filename, strerror(errno)); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 404 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /* Skip the key block */ |
| 408 | key_block = (VbKeyBlockHeader*)buf; |
| 409 | Debug("Keyblock is 0x%" PRIx64 " bytes\n", key_block->key_block_size); |
| 410 | now += key_block->key_block_size; |
| 411 | if (now > statbuf.st_size) { |
| 412 | error("key_block_size advances past the end of the blob\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 413 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /* Skip the preamble */ |
| 417 | preamble = (VbKernelPreambleHeader*)(buf + now); |
| 418 | Debug("Preamble is 0x%" PRIx64 " bytes\n", preamble->preamble_size); |
| 419 | now += preamble->preamble_size; |
| 420 | if (now > statbuf.st_size) { |
| 421 | error("preamble_size advances past the end of the blob\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 422 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /* Go find the kernel blob */ |
| 426 | Debug("kernel blob is at offset 0x%" PRIx64 "\n", now); |
| 427 | if (0 != fseek(fp, now, SEEK_SET)) { |
| 428 | error("Unable to seek to 0x%" PRIx64 " in %s: %s\n", now, filename, |
| 429 | strerror(errno)); |
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 | /* Remember what we've got */ |
| 434 | bp = (blob_t *)Malloc(sizeof(blob_t)); |
| 435 | if (!bp) { |
| 436 | error("Couldn't allocate bytes for blob_t.\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 437 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 438 | } |
| 439 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 440 | bp->buf = buf; |
| 441 | bp->key_block = key_block; |
| 442 | bp->preamble = preamble; |
| 443 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 444 | bp->kernel_version = preamble->kernel_version; |
| 445 | bp->bootloader_address = preamble->bootloader_address; |
| 446 | bp->bootloader_size = preamble->bootloader_size; |
| 447 | bp->blob_size = preamble->body_signature.data_size; |
| 448 | |
| 449 | Debug(" kernel_version = %d\n", bp->kernel_version); |
| 450 | Debug(" bootloader_address = 0x%" PRIx64 "\n", bp->bootloader_address); |
| 451 | Debug(" bootloader_size = 0x%" PRIx64 "\n", bp->bootloader_size); |
| 452 | Debug(" blob_size = 0x%" PRIx64 "\n", bp->blob_size); |
| 453 | |
| 454 | bp->blob = (uint8_t *)Malloc(bp->blob_size); |
| 455 | if (!bp->blob) { |
| 456 | 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] | 457 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /* read it in */ |
| 461 | if (1 != fread(bp->blob, bp->blob_size, 1, fp)) { |
| 462 | error("Unable to read kernel blob from %s: %s\n", filename, strerror(errno)); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 463 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 464 | } |
| 465 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 466 | ret_error = 0; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 467 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 468 | /* done */ |
| 469 | unwind_oldblob: |
| 470 | fclose(fp); |
| 471 | if (ret_error) { |
| 472 | if (bp) { |
| 473 | FreeBlob(bp); |
| 474 | bp = NULL; |
| 475 | } else if (buf) { |
| 476 | Free(buf); |
| 477 | } |
| 478 | } |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 479 | return bp; |
| 480 | } |
| 481 | |
| 482 | |
| 483 | /* Pack a .kernel */ |
| 484 | static int Pack(const char* outfile, const char* keyblock_file, |
| 485 | const char* signprivate, blob_t *bp, uint64_t pad, |
| 486 | int vblockonly) { |
| 487 | VbPrivateKey* signing_key; |
| 488 | VbSignature* body_sig; |
| 489 | VbKernelPreambleHeader* preamble; |
| 490 | VbKeyBlockHeader* key_block; |
| 491 | uint64_t key_block_size; |
| 492 | FILE* f; |
| 493 | uint64_t i; |
| 494 | |
| 495 | if (!outfile) { |
| 496 | error("Must specify output filename\n"); |
| 497 | return 1; |
| 498 | } |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 499 | if ((!keyblock_file && !bp->key_block) || !signprivate) { |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 500 | error("Must specify all keys\n"); |
| 501 | return 1; |
| 502 | } |
| 503 | if (!bp) { |
| 504 | error("Refusing to pack invalid kernel blob\n"); |
| 505 | return 1; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 506 | } |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 507 | |
| 508 | /* Get the key block and read the private key. */ |
| 509 | if (keyblock_file) { |
| 510 | key_block = (VbKeyBlockHeader*)ReadFile(keyblock_file, &key_block_size); |
| 511 | if (!key_block) { |
| 512 | error("Error reading key block.\n"); |
| 513 | return 1; |
| 514 | } |
| 515 | } else { |
| 516 | key_block = bp->key_block; |
| 517 | key_block_size = key_block->key_block_size; |
| 518 | } |
| 519 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 520 | if (pad < key_block->key_block_size) { |
| 521 | error("Pad too small\n"); |
| 522 | return 1; |
| 523 | } |
| 524 | |
Bill Richardson | abf0550 | 2010-07-01 10:22:06 -0700 | [diff] [blame] | 525 | signing_key = PrivateKeyRead(signprivate); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 526 | if (!signing_key) { |
| 527 | error("Error reading signing key.\n"); |
| 528 | return 1; |
| 529 | } |
| 530 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 531 | /* Sign the kernel data */ |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 532 | body_sig = CalculateSignature(bp->blob, bp->blob_size, signing_key); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 533 | if (!body_sig) { |
| 534 | error("Error calculating body signature\n"); |
| 535 | return 1; |
| 536 | } |
| 537 | |
| 538 | /* Create preamble */ |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 539 | preamble = CreateKernelPreamble(bp->kernel_version, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 540 | CROS_32BIT_ENTRY_ADDR, |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 541 | bp->bootloader_address, |
| 542 | bp->bootloader_size, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 543 | body_sig, |
| 544 | pad - key_block_size, |
| 545 | signing_key); |
| 546 | if (!preamble) { |
| 547 | error("Error creating preamble.\n"); |
| 548 | return 1; |
| 549 | } |
| 550 | |
| 551 | /* Write the output file */ |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 552 | Debug("writing %s...\n", outfile); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 553 | f = fopen(outfile, "wb"); |
| 554 | if (!f) { |
| 555 | error("Can't open output file %s\n", outfile); |
| 556 | return 1; |
| 557 | } |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 558 | Debug("0x%" PRIx64 " bytes of key_block\n", key_block_size); |
| 559 | Debug("0x%" PRIx64 " bytes of preamble\n", preamble->preamble_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 560 | i = ((1 != fwrite(key_block, key_block_size, 1, f)) || |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 561 | (1 != fwrite(preamble, preamble->preamble_size, 1, f))); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 562 | if (i) { |
| 563 | error("Can't write output file %s\n", outfile); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 564 | fclose(f); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 565 | unlink(outfile); |
| 566 | return 1; |
| 567 | } |
| 568 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 569 | if (!vblockonly) { |
| 570 | Debug("0x%" PRIx64 " bytes of blob\n", bp->blob_size); |
| 571 | i = (1 != fwrite(bp->blob, bp->blob_size, 1, f)); |
| 572 | if (i) { |
| 573 | error("Can't write output file %s\n", outfile); |
| 574 | fclose(f); |
| 575 | unlink(outfile); |
| 576 | return 1; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | fclose(f); |
| 581 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 582 | /* Success */ |
| 583 | return 0; |
| 584 | } |
| 585 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 586 | /* |
| 587 | * Replace kernel command line in a blob representing a kernel. |
| 588 | */ |
| 589 | static int ReplaceConfig(blob_t* bp, const char* config_file) |
| 590 | { |
| 591 | uint8_t* new_conf; |
| 592 | uint64_t config_size; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 593 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 594 | if (!config_file) { |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | new_conf = ReadConfigFile(config_file, &config_size); |
| 599 | if (!new_conf) { |
| 600 | return 1; |
| 601 | } |
| 602 | |
| 603 | /* fill the config buffer with zeros */ |
| 604 | Memset(BpCmdLineLocation(bp), 0, CROS_CONFIG_SIZE); |
| 605 | Memcpy(BpCmdLineLocation(bp), new_conf, config_size); |
| 606 | Free(new_conf); |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | static int Verify(const char* infile, const char* signpubkey, int verbose) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 611 | |
| 612 | VbKeyBlockHeader* key_block; |
| 613 | VbKernelPreambleHeader* preamble; |
| 614 | VbPublicKey* data_key; |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame^] | 615 | VbPublicKey* sign_key = NULL; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 616 | RSAPublicKey* rsa; |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 617 | blob_t* bp; |
| 618 | uint64_t now; |
| 619 | int rv = 1; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 620 | |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame^] | 621 | if (!infile) { |
| 622 | error("Must specify filename\n"); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 623 | return 1; |
| 624 | } |
| 625 | |
| 626 | /* Read public signing key */ |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame^] | 627 | if (signpubkey) { |
| 628 | sign_key = PublicKeyRead(signpubkey); |
| 629 | if (!sign_key) { |
| 630 | error("Error reading signpubkey.\n"); |
| 631 | return 1; |
| 632 | } |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /* Read blob */ |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 636 | bp = OldBlob(infile); |
| 637 | if (!bp) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 638 | error("Error reading input file\n"); |
| 639 | return 1; |
| 640 | } |
| 641 | |
| 642 | /* Verify key block */ |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 643 | key_block = bp->key_block; |
| 644 | if (0 != KeyBlockVerify(key_block, bp->blob_size, sign_key)) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 645 | error("Error verifying key block.\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 646 | goto verify_exit; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 647 | } |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 648 | now = key_block->key_block_size; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 649 | |
| 650 | printf("Key block:\n"); |
| 651 | data_key = &key_block->data_key; |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame^] | 652 | if (verbose) |
| 653 | printf(" Signature: %s\n", sign_key ? "valid" : "ignored"); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 654 | printf(" Size: 0x%" PRIx64 "\n", key_block->key_block_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 655 | printf(" Data key algorithm: %" PRIu64 " %s\n", data_key->algorithm, |
| 656 | (data_key->algorithm < kNumAlgorithms ? |
| 657 | algo_strings[data_key->algorithm] : "(invalid)")); |
| 658 | printf(" Data key version: %" PRIu64 "\n", data_key->key_version); |
| 659 | printf(" Flags: %" PRIu64 "\n", key_block->key_block_flags); |
| 660 | |
| 661 | rsa = PublicKeyToRSA(&key_block->data_key); |
| 662 | if (!rsa) { |
| 663 | error("Error parsing data key.\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 664 | goto verify_exit; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /* Verify preamble */ |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 668 | preamble = bp->preamble; |
Randall Spangler | 87c13d8 | 2010-07-19 10:35:40 -0700 | [diff] [blame] | 669 | if (0 != VerifyKernelPreamble( |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame^] | 670 | preamble, bp->blob_size - key_block->key_block_size, rsa)) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 671 | error("Error verifying preamble.\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 672 | goto verify_exit; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 673 | } |
| 674 | now += preamble->preamble_size; |
| 675 | |
| 676 | printf("Preamble:\n"); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 677 | printf(" Size: 0x%" PRIx64 "\n", preamble->preamble_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 678 | printf(" Header version: %" PRIu32 ".%" PRIu32"\n", |
| 679 | preamble->header_version_major, preamble->header_version_minor); |
| 680 | printf(" Kernel version: %" PRIu64 "\n", preamble->kernel_version); |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 681 | printf(" Body load address: 0x%" PRIx64 "\n", preamble->body_load_address); |
| 682 | printf(" Body size: 0x%" PRIx64 "\n", |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 683 | preamble->body_signature.data_size); |
Randall Spangler | 87c13d8 | 2010-07-19 10:35:40 -0700 | [diff] [blame] | 684 | printf(" Bootloader address: 0x%" PRIx64 "\n", |
| 685 | preamble->bootloader_address); |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 686 | printf(" Bootloader size: 0x%" PRIx64 "\n", preamble->bootloader_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 687 | |
| 688 | /* Verify body */ |
Randall Spangler | 87c13d8 | 2010-07-19 10:35:40 -0700 | [diff] [blame] | 689 | if (0 != VerifyData(bp->blob, bp->blob_size, &preamble->body_signature, |
| 690 | rsa)) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 691 | error("Error verifying kernel body.\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 692 | goto verify_exit; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 693 | } |
| 694 | printf("Body verification succeeded.\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 695 | |
| 696 | rv = 0; |
| 697 | |
| 698 | if (!verbose) { |
| 699 | goto verify_exit; |
| 700 | } |
| 701 | |
| 702 | printf("Config:\n%s\n", BpCmdLineLocation(bp)); |
| 703 | |
| 704 | verify_exit: |
| 705 | FreeBlob(bp); |
| 706 | return rv; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | |
| 710 | int main(int argc, char* argv[]) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 711 | char* filename = NULL; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 712 | char* oldfile = NULL; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 713 | char* key_block_file = NULL; |
| 714 | char* signpubkey = NULL; |
| 715 | char* signprivate = NULL; |
| 716 | uint64_t version = 0; |
| 717 | char* vmlinuz = NULL; |
| 718 | char* bootloader = NULL; |
| 719 | char* config_file = NULL; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 720 | int vblockonly = 0; |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 721 | int verbose = 0; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 722 | uint64_t pad = DEFAULT_PADDING; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 723 | int mode = 0; |
| 724 | int parse_error = 0; |
| 725 | char* e; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 726 | int i,r; |
| 727 | blob_t *bp; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 728 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 729 | |
| 730 | char *progname = strrchr(argv[0], '/'); |
| 731 | if (progname) |
| 732 | progname++; |
| 733 | else |
| 734 | progname = argv[0]; |
| 735 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 736 | while (((i = getopt_long(argc, argv, ":", long_opts, NULL)) != -1) && |
| 737 | !parse_error) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 738 | switch (i) { |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 739 | default: |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 740 | case '?': |
| 741 | /* Unhandled option */ |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 742 | parse_error = 1; |
| 743 | break; |
| 744 | |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame^] | 745 | case 0: |
| 746 | /* silently handled option */ |
| 747 | break; |
| 748 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 749 | case OPT_MODE_PACK: |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 750 | case OPT_MODE_REPACK: |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 751 | case OPT_MODE_VERIFY: |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 752 | if (mode && (mode != i)) { |
| 753 | fprintf(stderr, "Only single mode can be specified\n"); |
| 754 | parse_error = 1; |
| 755 | break; |
| 756 | } |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 757 | mode = i; |
| 758 | filename = optarg; |
| 759 | break; |
| 760 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 761 | case OPT_OLDBLOB: |
| 762 | oldfile = optarg; |
| 763 | break; |
| 764 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 765 | case OPT_KEYBLOCK: |
| 766 | key_block_file = optarg; |
| 767 | break; |
| 768 | |
| 769 | case OPT_SIGNPUBKEY: |
| 770 | signpubkey = optarg; |
| 771 | break; |
| 772 | |
| 773 | case OPT_SIGNPRIVATE: |
| 774 | signprivate = optarg; |
| 775 | break; |
| 776 | |
| 777 | case OPT_VMLINUZ: |
| 778 | vmlinuz = optarg; |
| 779 | break; |
| 780 | |
| 781 | case OPT_BOOTLOADER: |
| 782 | bootloader = optarg; |
| 783 | break; |
| 784 | |
| 785 | case OPT_CONFIG: |
| 786 | config_file = optarg; |
| 787 | break; |
| 788 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 789 | case OPT_VBLOCKONLY: |
| 790 | vblockonly = 1; |
| 791 | break; |
| 792 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 793 | case OPT_VERSION: |
| 794 | version = strtoul(optarg, &e, 0); |
| 795 | if (!*optarg || (e && *e)) { |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 796 | fprintf(stderr, "Invalid --version\n"); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 797 | parse_error = 1; |
| 798 | } |
| 799 | break; |
| 800 | |
| 801 | case OPT_PAD: |
| 802 | pad = strtoul(optarg, &e, 0); |
| 803 | if (!*optarg || (e && *e)) { |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 804 | fprintf(stderr, "Invalid --pad\n"); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 805 | parse_error = 1; |
| 806 | } |
| 807 | break; |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 808 | |
| 809 | case OPT_VERBOSE: |
| 810 | verbose = 1; |
| 811 | break; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | |
| 815 | if (parse_error) |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 816 | return PrintHelp(progname); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 817 | |
| 818 | switch(mode) { |
| 819 | case OPT_MODE_PACK: |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 820 | bp = NewBlob(version, vmlinuz, bootloader, config_file); |
| 821 | if (!bp) |
| 822 | return 1; |
| 823 | r = Pack(filename, key_block_file, signprivate, bp, pad, vblockonly); |
| 824 | FreeBlob(bp); |
| 825 | return r; |
| 826 | |
| 827 | case OPT_MODE_REPACK: |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 828 | if (!config_file && !key_block_file) { |
| 829 | fprintf(stderr, |
| 830 | "You must supply at least one of --config and --keyblock\n"); |
| 831 | return 1; |
| 832 | } |
| 833 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 834 | bp = OldBlob(oldfile); |
| 835 | if (!bp) |
| 836 | return 1; |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 837 | r = ReplaceConfig(bp, config_file); |
| 838 | if (!r) { |
| 839 | r = Pack(filename, key_block_file, signprivate, bp, pad, vblockonly); |
| 840 | } |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 841 | FreeBlob(bp); |
| 842 | return r; |
| 843 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 844 | case OPT_MODE_VERIFY: |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 845 | return Verify(filename, signpubkey, verbose); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 846 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 847 | default: |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 848 | fprintf(stderr, |
| 849 | "You must specify a mode: --pack, --repack or --verify\n"); |
| 850 | return PrintHelp(progname); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 851 | } |
| 852 | } |