Randall Spangler | ae87b92 | 2011-05-12 13:27:28 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 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, |
Che-Liang Chiou | 0376203 | 2011-02-22 11:16:51 +0800 | [diff] [blame] | 36 | OPT_ARCH, |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 37 | OPT_OLDBLOB, |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 38 | OPT_KLOADADDR, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 39 | OPT_KEYBLOCK, |
| 40 | OPT_SIGNPUBKEY, |
| 41 | OPT_SIGNPRIVATE, |
| 42 | OPT_VERSION, |
| 43 | OPT_VMLINUZ, |
| 44 | OPT_BOOTLOADER, |
| 45 | OPT_CONFIG, |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 46 | OPT_VBLOCKONLY, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 47 | OPT_PAD, |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 48 | OPT_VERBOSE, |
Randall Spangler | ae87b92 | 2011-05-12 13:27:28 -0700 | [diff] [blame] | 49 | OPT_MINVERSION, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Che-Liang Chiou | 0376203 | 2011-02-22 11:16:51 +0800 | [diff] [blame] | 52 | enum { |
| 53 | ARCH_ARM, |
| 54 | ARCH_X86 /* default */ |
| 55 | }; |
| 56 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 57 | static struct option long_opts[] = { |
| 58 | {"pack", 1, 0, OPT_MODE_PACK }, |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 59 | {"repack", 1, 0, OPT_MODE_REPACK }, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 60 | {"verify", 1, 0, OPT_MODE_VERIFY }, |
Che-Liang Chiou | 0376203 | 2011-02-22 11:16:51 +0800 | [diff] [blame] | 61 | {"arch", 1, 0, OPT_ARCH }, |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 62 | {"oldblob", 1, 0, OPT_OLDBLOB }, |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 63 | {"kloadaddr", 1, 0, OPT_KLOADADDR }, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 64 | {"keyblock", 1, 0, OPT_KEYBLOCK }, |
| 65 | {"signpubkey", 1, 0, OPT_SIGNPUBKEY }, |
| 66 | {"signprivate", 1, 0, OPT_SIGNPRIVATE }, |
| 67 | {"version", 1, 0, OPT_VERSION }, |
Randall Spangler | ae87b92 | 2011-05-12 13:27:28 -0700 | [diff] [blame] | 68 | {"minversion", 1, 0, OPT_MINVERSION }, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 69 | {"vmlinuz", 1, 0, OPT_VMLINUZ }, |
| 70 | {"bootloader", 1, 0, OPT_BOOTLOADER }, |
| 71 | {"config", 1, 0, OPT_CONFIG }, |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 72 | {"vblockonly", 0, 0, OPT_VBLOCKONLY }, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 73 | {"pad", 1, 0, OPT_PAD }, |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 74 | {"verbose", 0, 0, OPT_VERBOSE }, |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 75 | {"debug", 0, &opt_debug, 1 }, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 76 | {NULL, 0, 0, 0} |
| 77 | }; |
| 78 | |
| 79 | |
| 80 | /* Print help and return error */ |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 81 | static int PrintHelp(char *progname) { |
| 82 | fprintf(stderr, |
| 83 | "This program creates, signs, and verifies the kernel blob\n"); |
| 84 | fprintf(stderr, |
| 85 | "\n" |
| 86 | "Usage: %s --pack <file> [PARAMETERS]\n" |
| 87 | "\n" |
| 88 | " Required parameters:\n" |
| 89 | " --keyblock <file> Key block in .keyblock format\n" |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame] | 90 | " --signprivate <file>" |
| 91 | " Private key to sign kernel data, in .vbprivk format\n" |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 92 | " --version <number> Kernel version\n" |
| 93 | " --vmlinuz <file> Linux kernel bzImage file\n" |
| 94 | " --bootloader <file> Bootloader stub\n" |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 95 | " --config <file> Command line file\n" |
Che-Liang Chiou | 0376203 | 2011-02-22 11:16:51 +0800 | [diff] [blame] | 96 | " --arch <arch> Cpu architecture (default x86)\n" |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 97 | "\n" |
| 98 | " Optional:\n" |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 99 | " --kloadaddr <address> Assign kernel body load address\n" |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 100 | " --pad <number> Verification padding size in bytes\n" |
| 101 | " --vblockonly Emit just the verification blob\n", |
| 102 | progname); |
| 103 | fprintf(stderr, |
| 104 | "\nOR\n\n" |
| 105 | "Usage: %s --repack <file> [PARAMETERS]\n" |
| 106 | "\n" |
vbendeb | 858fffb | 2010-10-06 09:51:44 -0700 | [diff] [blame] | 107 | " Required parameters (of --keyblock, --config, and --version \n" |
| 108 | " at least one is required):\n" |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 109 | " --keyblock <file> Key block in .keyblock format\n" |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame] | 110 | " --signprivate <file>" |
| 111 | " Private key to sign kernel data, in .vbprivk format\n" |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 112 | " --oldblob <file> Previously packed kernel blob\n" |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 113 | " --config <file> New command line file\n" |
vbendeb | 858fffb | 2010-10-06 09:51:44 -0700 | [diff] [blame] | 114 | " --version <number> Kernel version\n" |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 115 | "\n" |
| 116 | " Optional:\n" |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 117 | " --kloadaddr <address> Assign kernel body load address\n" |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 118 | " --pad <number> Verification padding size in bytes\n" |
| 119 | " --vblockonly Emit just the verification blob\n", |
| 120 | progname); |
| 121 | fprintf(stderr, |
| 122 | "\nOR\n\n" |
| 123 | "Usage: %s --verify <file> [PARAMETERS]\n" |
| 124 | "\n" |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 125 | " Optional:\n" |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame] | 126 | " --signpubkey <file>" |
| 127 | " Public key to verify kernel keyblock, in .vbpubk format\n" |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 128 | " --verbose Print a more detailed report\n" |
Will Drewry | 9342f88 | 2010-10-26 10:22:05 -0500 | [diff] [blame] | 129 | " --keyblock <file>" |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 130 | " Outputs the verified key block, in .keyblock format\n" |
| 131 | " --kloadaddr <address> Assign kernel body load address\n" |
Randall Spangler | 7ecb39d | 2011-05-12 15:37:45 -0700 | [diff] [blame] | 132 | " --pad <number> Verification padding size in bytes\n" |
Randall Spangler | ae87b92 | 2011-05-12 13:27:28 -0700 | [diff] [blame] | 133 | " --minversion <number> Minimum combined kernel key version\n" |
| 134 | " and kernel version\n" |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 135 | "\n", |
| 136 | progname); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 137 | return 1; |
| 138 | } |
| 139 | |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 140 | static void Debug(const char *format, ...) { |
| 141 | if (!opt_debug) |
| 142 | return; |
| 143 | |
| 144 | va_list ap; |
| 145 | va_start(ap, format); |
| 146 | fprintf(stderr, "DEBUG: "); |
| 147 | vfprintf(stderr, format, ap); |
| 148 | va_end(ap); |
| 149 | } |
| 150 | |
Bill Richardson | 2f6a71f | 2010-10-14 09:25:39 -0700 | [diff] [blame] | 151 | /* Return an explanation when fread() fails. */ |
| 152 | static const char *error_fread(FILE *fp) { |
| 153 | const char *retval = "beats me why"; |
| 154 | if (feof(fp)) |
| 155 | retval = "EOF"; |
| 156 | else if (ferror(fp)) |
| 157 | retval = strerror(errno); |
| 158 | clearerr(fp); |
| 159 | return retval; |
| 160 | } |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 161 | |
| 162 | /* Return the smallest integral multiple of [alignment] that is equal |
| 163 | * to or greater than [val]. Used to determine the number of |
| 164 | * pages/sectors/blocks/whatever needed to contain [val] |
| 165 | * items/bytes/etc. */ |
| 166 | static uint64_t roundup(uint64_t val, uint64_t alignment) { |
| 167 | uint64_t rem = val % alignment; |
| 168 | if ( rem ) |
| 169 | return val + (alignment - rem); |
| 170 | return val; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | /* Match regexp /\b--\b/ to delimit the start of the kernel commandline. If we |
| 175 | * don't find one, we'll use the whole thing. */ |
| 176 | static unsigned int find_cmdline_start(char *input, unsigned int max_len) { |
| 177 | int start = 0; |
| 178 | int i; |
| 179 | for(i = 0; i < max_len - 1 && input[i]; i++) { |
| 180 | if ('-' == input[i] && '-' == input[i + 1]) { /* found a "--" */ |
| 181 | if ((i == 0 || ' ' == input[i - 1]) && /* nothing before it */ |
| 182 | (i + 2 >= max_len || ' ' == input[i+2])) { /* nothing after it */ |
| 183 | start = i+2; /* note: hope there's a trailing '\0' */ |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | while(' ' == input[start]) /* skip leading spaces */ |
| 189 | start++; |
| 190 | |
| 191 | return start; |
| 192 | } |
| 193 | |
| 194 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 195 | typedef struct blob_s { |
| 196 | /* Stuff needed by VbKernelPreambleHeader */ |
| 197 | uint64_t kernel_version; |
| 198 | uint64_t bootloader_address; |
| 199 | uint64_t bootloader_size; |
| 200 | /* Raw kernel blob data */ |
| 201 | uint64_t blob_size; |
| 202 | uint8_t *blob; |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 203 | |
| 204 | /* these fields are not always initialized */ |
| 205 | VbKernelPreambleHeader* preamble; |
| 206 | VbKeyBlockHeader* key_block; |
| 207 | uint8_t *buf; |
| 208 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 209 | } blob_t; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 210 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 211 | /* Given a blob return the location of the kernel command line buffer. */ |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 212 | static char* BpCmdLineLocation(blob_t *bp, uint64_t kernel_body_load_address) |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 213 | { |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 214 | return (char*)(bp->blob + bp->bootloader_address - kernel_body_load_address - |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 215 | CROS_CONFIG_SIZE - CROS_PARAMS_SIZE); |
| 216 | } |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 217 | |
| 218 | static void FreeBlob(blob_t *bp) { |
| 219 | if (bp) { |
| 220 | if (bp->blob) |
| 221 | Free(bp->blob); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 222 | if (bp->buf) |
| 223 | Free(bp->buf); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 224 | Free(bp); |
| 225 | } |
| 226 | } |
| 227 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 228 | /* |
| 229 | * Read the kernel command line from a file. Get rid of \n characters along |
| 230 | * the way and verify that the line fits into a 4K buffer. |
| 231 | * |
| 232 | * Return the buffer contaning the line on success (and set the line length |
| 233 | * using the passed in parameter), or NULL in case something goes wrong. |
| 234 | */ |
| 235 | static uint8_t* ReadConfigFile(const char* config_file, uint64_t* config_size) |
| 236 | { |
| 237 | uint8_t* config_buf; |
| 238 | int ii; |
| 239 | |
| 240 | config_buf = ReadFile(config_file, config_size); |
| 241 | Debug(" config file size=0x%" PRIx64 "\n", *config_size); |
| 242 | if (CROS_CONFIG_SIZE <= *config_size) { /* need room for trailing '\0' */ |
| 243 | error("Config file %s is too large (>= %d bytes)\n", |
| 244 | config_file, CROS_CONFIG_SIZE); |
| 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | /* Replace newlines with spaces */ |
| 249 | for (ii = 0; ii < *config_size; ii++) { |
| 250 | if ('\n' == config_buf[ii]) { |
| 251 | config_buf[ii] = ' '; |
| 252 | } |
| 253 | } |
| 254 | return config_buf; |
| 255 | } |
| 256 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 257 | /* Create a blob from its components */ |
| 258 | static blob_t *NewBlob(uint64_t version, |
| 259 | const char* vmlinuz, |
| 260 | const char* bootloader_file, |
Che-Liang Chiou | 0376203 | 2011-02-22 11:16:51 +0800 | [diff] [blame] | 261 | const char* config_file, |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 262 | int arch, |
| 263 | uint64_t kernel_body_load_address) { |
Che-Liang Chiou | 0376203 | 2011-02-22 11:16:51 +0800 | [diff] [blame] | 264 | blob_t* bp; |
| 265 | struct linux_kernel_header* lh = 0; |
| 266 | struct linux_kernel_params* params = 0; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 267 | uint8_t* config_buf; |
| 268 | uint64_t config_size; |
| 269 | uint8_t* bootloader_buf; |
| 270 | uint64_t bootloader_size; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 271 | uint8_t* kernel_buf; |
| 272 | uint64_t kernel_size; |
| 273 | uint64_t kernel32_start = 0; |
| 274 | uint64_t kernel32_size = 0; |
| 275 | uint32_t cmdline_addr; |
| 276 | uint8_t* blob = NULL; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 277 | uint64_t now = 0; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 278 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 279 | if (!vmlinuz || !bootloader_file || !config_file) { |
| 280 | error("Must specify all input files\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 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 284 | bp = (blob_t *)Malloc(sizeof(blob_t)); |
| 285 | if (!bp) { |
| 286 | error("Couldn't allocate bytes for blob_t.\n"); |
| 287 | return 0; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 288 | } |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 289 | |
| 290 | Memset(bp, 0, sizeof(*bp)); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 291 | bp->kernel_version = version; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 292 | |
| 293 | /* Read the config file */ |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 294 | Debug("Reading %s\n", config_file); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 295 | config_buf = ReadConfigFile(config_file, &config_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 296 | if (!config_buf) |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 297 | return 0; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 298 | |
| 299 | /* Read the bootloader */ |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 300 | Debug("Reading %s\n", bootloader_file); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 301 | bootloader_buf = ReadFile(bootloader_file, &bootloader_size); |
| 302 | if (!bootloader_buf) |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 303 | return 0; |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 304 | Debug(" bootloader file size=0x%" PRIx64 "\n", bootloader_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 305 | |
| 306 | /* Read the kernel */ |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 307 | Debug("Reading %s\n", vmlinuz); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 308 | kernel_buf = ReadFile(vmlinuz, &kernel_size); |
| 309 | if (!kernel_buf) |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 310 | return 0; |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 311 | Debug(" kernel file size=0x%" PRIx64 "\n", kernel_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 312 | if (!kernel_size) { |
| 313 | error("Empty kernel file\n"); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 314 | return 0; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Che-Liang Chiou | 0376203 | 2011-02-22 11:16:51 +0800 | [diff] [blame] | 317 | if (arch == ARCH_X86) { |
| 318 | /* The first part of vmlinuz is a header, followed by a real-mode |
| 319 | * boot stub. We only want the 32-bit part. */ |
| 320 | lh = (struct linux_kernel_header *)kernel_buf; |
| 321 | kernel32_start = (lh->setup_sects + 1) << 9; |
| 322 | if (kernel32_start >= kernel_size) { |
| 323 | error("Malformed kernel\n"); |
| 324 | return 0; |
| 325 | } |
| 326 | } else |
| 327 | kernel32_start = 0; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 328 | kernel32_size = kernel_size - kernel32_start; |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 329 | Debug(" kernel32_start=0x%" PRIx64 "\n", kernel32_start); |
| 330 | Debug(" kernel32_size=0x%" PRIx64 "\n", kernel32_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 331 | |
| 332 | /* Allocate and zero the blob we need. */ |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 333 | bp->blob_size = roundup(kernel32_size, CROS_ALIGN) + |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 334 | CROS_CONFIG_SIZE + |
| 335 | CROS_PARAMS_SIZE + |
| 336 | roundup(bootloader_size, CROS_ALIGN); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 337 | blob = (uint8_t *)Malloc(bp->blob_size); |
| 338 | Debug("blob_size=0x%" PRIx64 "\n", bp->blob_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 339 | if (!blob) { |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 340 | error("Couldn't allocate %ld bytes.\n", bp->blob_size); |
| 341 | return 0; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 342 | } |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 343 | Memset(blob, 0, bp->blob_size); |
| 344 | bp->blob = blob; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 345 | |
| 346 | /* Copy the 32-bit kernel. */ |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 347 | Debug("kernel goes at blob+=0x%" PRIx64 "\n", now); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 348 | if (kernel32_size) |
| 349 | Memcpy(blob + now, kernel_buf + kernel32_start, kernel32_size); |
| 350 | now += roundup(now + kernel32_size, CROS_ALIGN); |
| 351 | |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 352 | Debug("config goes at blob+0x%" PRIx64 "\n", now); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 353 | /* Find the load address of the commandline. We'll need it later. */ |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 354 | cmdline_addr = kernel_body_load_address + now + |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 355 | find_cmdline_start((char *)config_buf, config_size); |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 356 | Debug(" cmdline_addr=0x%" PRIx64 "\n", cmdline_addr); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 357 | |
| 358 | /* Copy the config. */ |
| 359 | if (config_size) |
| 360 | Memcpy(blob + now, config_buf, config_size); |
| 361 | now += CROS_CONFIG_SIZE; |
| 362 | |
| 363 | /* The zeropage data is next. Overlay the linux_kernel_header onto it, and |
| 364 | * tweak a few fields. */ |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 365 | Debug("params goes at blob+=0x%" PRIx64 "\n", now); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 366 | params = (struct linux_kernel_params *)(blob + now); |
Che-Liang Chiou | 0376203 | 2011-02-22 11:16:51 +0800 | [diff] [blame] | 367 | if (arch == ARCH_X86) |
| 368 | Memcpy(&(params->setup_sects), &(lh->setup_sects), |
| 369 | sizeof(*lh) - offsetof(struct linux_kernel_header, setup_sects)); |
| 370 | else |
| 371 | Memset(&(params->setup_sects), 0, |
| 372 | sizeof(*lh) - offsetof(struct linux_kernel_header, setup_sects)); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 373 | params->boot_flag = 0; |
| 374 | params->ramdisk_image = 0; /* we don't support initrd */ |
| 375 | params->ramdisk_size = 0; |
| 376 | params->type_of_loader = 0xff; |
| 377 | params->cmd_line_ptr = cmdline_addr; |
Che-Liang Chiou | 475bf44 | 2010-08-23 11:20:44 +0800 | [diff] [blame] | 378 | /* A fake e820 memory map with 2 entries */ |
| 379 | params->n_e820_entry = 2; |
| 380 | params->e820_entries[0].start_addr = 0x00000000; |
| 381 | params->e820_entries[0].segment_size = 0x00001000; |
| 382 | params->e820_entries[0].segment_type = E820_TYPE_RAM; |
| 383 | params->e820_entries[1].start_addr = 0xfffff000; |
| 384 | params->e820_entries[1].segment_size = 0x00001000; |
| 385 | params->e820_entries[1].segment_type = E820_TYPE_RESERVED; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 386 | now += CROS_PARAMS_SIZE; |
| 387 | |
| 388 | /* Finally, append the bootloader. Remember where it will load in |
| 389 | * memory, too. */ |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 390 | Debug("bootloader goes at blob+=0x%" PRIx64 "\n", now); |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 391 | bp->bootloader_address = kernel_body_load_address + now; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 392 | bp->bootloader_size = roundup(bootloader_size, CROS_ALIGN); |
| 393 | Debug(" bootloader_address=0x%" PRIx64 "\n", bp->bootloader_address); |
| 394 | Debug(" bootloader_size=0x%" PRIx64 "\n", bp->bootloader_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 395 | if (bootloader_size) |
| 396 | Memcpy(blob + now, bootloader_buf, bootloader_size); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 397 | now += bp->bootloader_size; |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 398 | Debug("end of blob is 0x%" PRIx64 "\n", now); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 399 | |
| 400 | /* Free input buffers */ |
| 401 | Free(kernel_buf); |
| 402 | Free(config_buf); |
| 403 | Free(bootloader_buf); |
| 404 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 405 | /* Success */ |
| 406 | return bp; |
| 407 | } |
| 408 | |
| 409 | |
| 410 | /* Pull the blob_t stuff out of a prepacked kernel blob file */ |
Randall Spangler | 7ecb39d | 2011-05-12 15:37:45 -0700 | [diff] [blame] | 411 | static blob_t *OldBlob(const char* filename, uint64_t pad) { |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 412 | FILE* fp = NULL; |
| 413 | blob_t *bp = NULL; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 414 | struct stat statbuf; |
| 415 | VbKeyBlockHeader* key_block; |
| 416 | VbKernelPreambleHeader* preamble; |
| 417 | uint64_t now = 0; |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 418 | uint8_t* buf = NULL; |
| 419 | int ret_error = 1; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 420 | |
| 421 | if (!filename) { |
| 422 | error("Must specify prepacked blob to read\n"); |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | if (0 != stat(filename, &statbuf)) { |
Bill Richardson | 2f6a71f | 2010-10-14 09:25:39 -0700 | [diff] [blame] | 427 | error("Unable to stat %s: %s\n", filename, strerror(errno)); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | Debug("%s size is 0x%" PRIx64 "\n", filename, statbuf.st_size); |
Randall Spangler | 7ecb39d | 2011-05-12 15:37:45 -0700 | [diff] [blame] | 432 | if (statbuf.st_size < pad) { |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 433 | error("%s is too small to be a valid kernel blob\n"); |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | Debug("Reading %s\n", filename); |
| 438 | fp = fopen(filename, "rb"); |
| 439 | if (!fp) { |
| 440 | error("Unable to open file %s: %s\n", filename, strerror(errno)); |
| 441 | return 0; |
| 442 | } |
| 443 | |
Randall Spangler | 7ecb39d | 2011-05-12 15:37:45 -0700 | [diff] [blame] | 444 | buf = Malloc(pad); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 445 | if (!buf) { |
| 446 | error("Unable to allocate padding\n"); |
| 447 | goto unwind_oldblob; |
| 448 | } |
| 449 | |
Randall Spangler | 7ecb39d | 2011-05-12 15:37:45 -0700 | [diff] [blame] | 450 | if (1 != fread(buf, pad, 1, fp)) { |
Bill Richardson | 2f6a71f | 2010-10-14 09:25:39 -0700 | [diff] [blame] | 451 | error("Unable to read header from %s: %s\n", filename, error_fread(fp)); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 452 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /* Skip the key block */ |
| 456 | key_block = (VbKeyBlockHeader*)buf; |
| 457 | Debug("Keyblock is 0x%" PRIx64 " bytes\n", key_block->key_block_size); |
| 458 | now += key_block->key_block_size; |
| 459 | if (now > statbuf.st_size) { |
| 460 | error("key_block_size advances past the end of the blob\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 461 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 462 | } |
Randall Spangler | 7ecb39d | 2011-05-12 15:37:45 -0700 | [diff] [blame] | 463 | if (now > pad) { |
| 464 | error("key_block_size advances past %" PRIu64 " byte padding\n", pad); |
| 465 | goto unwind_oldblob; |
| 466 | } |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 467 | |
| 468 | /* Skip the preamble */ |
| 469 | preamble = (VbKernelPreambleHeader*)(buf + now); |
| 470 | Debug("Preamble is 0x%" PRIx64 " bytes\n", preamble->preamble_size); |
| 471 | now += preamble->preamble_size; |
| 472 | if (now > statbuf.st_size) { |
| 473 | error("preamble_size advances past the end of the blob\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 474 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 475 | } |
Randall Spangler | 7ecb39d | 2011-05-12 15:37:45 -0700 | [diff] [blame] | 476 | if (now > pad) { |
| 477 | error("preamble_size advances past %" PRIu64 " byte padding\n", pad); |
| 478 | goto unwind_oldblob; |
| 479 | } |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 480 | |
| 481 | /* Go find the kernel blob */ |
| 482 | Debug("kernel blob is at offset 0x%" PRIx64 "\n", now); |
| 483 | if (0 != fseek(fp, now, SEEK_SET)) { |
| 484 | error("Unable to seek to 0x%" PRIx64 " in %s: %s\n", now, filename, |
| 485 | strerror(errno)); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 486 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /* Remember what we've got */ |
| 490 | bp = (blob_t *)Malloc(sizeof(blob_t)); |
| 491 | if (!bp) { |
| 492 | error("Couldn't allocate bytes for blob_t.\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 493 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 494 | } |
| 495 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 496 | bp->buf = buf; |
| 497 | bp->key_block = key_block; |
| 498 | bp->preamble = preamble; |
| 499 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 500 | bp->kernel_version = preamble->kernel_version; |
| 501 | bp->bootloader_address = preamble->bootloader_address; |
| 502 | bp->bootloader_size = preamble->bootloader_size; |
| 503 | bp->blob_size = preamble->body_signature.data_size; |
| 504 | |
| 505 | Debug(" kernel_version = %d\n", bp->kernel_version); |
| 506 | Debug(" bootloader_address = 0x%" PRIx64 "\n", bp->bootloader_address); |
| 507 | Debug(" bootloader_size = 0x%" PRIx64 "\n", bp->bootloader_size); |
| 508 | Debug(" blob_size = 0x%" PRIx64 "\n", bp->blob_size); |
| 509 | |
Bill Richardson | 2f6a71f | 2010-10-14 09:25:39 -0700 | [diff] [blame] | 510 | if (!bp->blob_size) { |
| 511 | error("No kernel blob found\n"); |
| 512 | goto unwind_oldblob; |
| 513 | } |
| 514 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 515 | bp->blob = (uint8_t *)Malloc(bp->blob_size); |
| 516 | if (!bp->blob) { |
| 517 | 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] | 518 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | /* read it in */ |
| 522 | if (1 != fread(bp->blob, bp->blob_size, 1, fp)) { |
Bill Richardson | 2f6a71f | 2010-10-14 09:25:39 -0700 | [diff] [blame] | 523 | error("Unable to read kernel blob from %s: %s\n", filename, error_fread(fp)); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 524 | goto unwind_oldblob; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 525 | } |
| 526 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 527 | ret_error = 0; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 528 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 529 | /* done */ |
| 530 | unwind_oldblob: |
| 531 | fclose(fp); |
| 532 | if (ret_error) { |
| 533 | if (bp) { |
| 534 | FreeBlob(bp); |
| 535 | bp = NULL; |
| 536 | } else if (buf) { |
| 537 | Free(buf); |
| 538 | } |
| 539 | } |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 540 | return bp; |
| 541 | } |
| 542 | |
| 543 | |
| 544 | /* Pack a .kernel */ |
| 545 | static int Pack(const char* outfile, const char* keyblock_file, |
| 546 | const char* signprivate, blob_t *bp, uint64_t pad, |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 547 | int vblockonly, |
| 548 | uint64_t kernel_body_load_address) { |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 549 | VbPrivateKey* signing_key; |
| 550 | VbSignature* body_sig; |
| 551 | VbKernelPreambleHeader* preamble; |
| 552 | VbKeyBlockHeader* key_block; |
| 553 | uint64_t key_block_size; |
| 554 | FILE* f; |
| 555 | uint64_t i; |
| 556 | |
| 557 | if (!outfile) { |
| 558 | error("Must specify output filename\n"); |
| 559 | return 1; |
| 560 | } |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 561 | if ((!keyblock_file && !bp->key_block) || !signprivate) { |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 562 | error("Must specify all keys\n"); |
| 563 | return 1; |
| 564 | } |
| 565 | if (!bp) { |
| 566 | error("Refusing to pack invalid kernel blob\n"); |
| 567 | return 1; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 568 | } |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 569 | |
| 570 | /* Get the key block and read the private key. */ |
| 571 | if (keyblock_file) { |
| 572 | key_block = (VbKeyBlockHeader*)ReadFile(keyblock_file, &key_block_size); |
| 573 | if (!key_block) { |
| 574 | error("Error reading key block.\n"); |
| 575 | return 1; |
| 576 | } |
| 577 | } else { |
| 578 | key_block = bp->key_block; |
| 579 | key_block_size = key_block->key_block_size; |
| 580 | } |
| 581 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 582 | if (pad < key_block->key_block_size) { |
| 583 | error("Pad too small\n"); |
| 584 | return 1; |
| 585 | } |
| 586 | |
Bill Richardson | abf0550 | 2010-07-01 10:22:06 -0700 | [diff] [blame] | 587 | signing_key = PrivateKeyRead(signprivate); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 588 | if (!signing_key) { |
| 589 | error("Error reading signing key.\n"); |
| 590 | return 1; |
| 591 | } |
| 592 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 593 | /* Sign the kernel data */ |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 594 | body_sig = CalculateSignature(bp->blob, bp->blob_size, signing_key); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 595 | if (!body_sig) { |
| 596 | error("Error calculating body signature\n"); |
| 597 | return 1; |
| 598 | } |
| 599 | |
| 600 | /* Create preamble */ |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 601 | preamble = CreateKernelPreamble(bp->kernel_version, |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 602 | kernel_body_load_address, |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 603 | bp->bootloader_address, |
| 604 | bp->bootloader_size, |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 605 | body_sig, |
| 606 | pad - key_block_size, |
| 607 | signing_key); |
| 608 | if (!preamble) { |
| 609 | error("Error creating preamble.\n"); |
| 610 | return 1; |
| 611 | } |
| 612 | |
| 613 | /* Write the output file */ |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 614 | Debug("writing %s...\n", outfile); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 615 | f = fopen(outfile, "wb"); |
| 616 | if (!f) { |
| 617 | error("Can't open output file %s\n", outfile); |
| 618 | return 1; |
| 619 | } |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 620 | Debug("0x%" PRIx64 " bytes of key_block\n", key_block_size); |
| 621 | Debug("0x%" PRIx64 " bytes of preamble\n", preamble->preamble_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 622 | i = ((1 != fwrite(key_block, key_block_size, 1, f)) || |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 623 | (1 != fwrite(preamble, preamble->preamble_size, 1, f))); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 624 | if (i) { |
| 625 | error("Can't write output file %s\n", outfile); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 626 | fclose(f); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 627 | unlink(outfile); |
| 628 | return 1; |
| 629 | } |
| 630 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 631 | if (!vblockonly) { |
| 632 | Debug("0x%" PRIx64 " bytes of blob\n", bp->blob_size); |
| 633 | i = (1 != fwrite(bp->blob, bp->blob_size, 1, f)); |
| 634 | if (i) { |
| 635 | error("Can't write output file %s\n", outfile); |
| 636 | fclose(f); |
| 637 | unlink(outfile); |
| 638 | return 1; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | fclose(f); |
| 643 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 644 | /* Success */ |
| 645 | return 0; |
| 646 | } |
| 647 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 648 | /* |
| 649 | * Replace kernel command line in a blob representing a kernel. |
| 650 | */ |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 651 | static int ReplaceConfig(blob_t* bp, const char* config_file, |
| 652 | uint64_t kernel_body_load_address) |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 653 | { |
| 654 | uint8_t* new_conf; |
| 655 | uint64_t config_size; |
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 | if (!config_file) { |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | new_conf = ReadConfigFile(config_file, &config_size); |
| 662 | if (!new_conf) { |
| 663 | return 1; |
| 664 | } |
| 665 | |
| 666 | /* fill the config buffer with zeros */ |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 667 | Memset(BpCmdLineLocation(bp, kernel_body_load_address), 0, CROS_CONFIG_SIZE); |
| 668 | Memcpy(BpCmdLineLocation(bp, kernel_body_load_address), |
| 669 | new_conf, config_size); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 670 | Free(new_conf); |
| 671 | return 0; |
| 672 | } |
| 673 | |
Will Drewry | 9342f88 | 2010-10-26 10:22:05 -0500 | [diff] [blame] | 674 | static int Verify(const char* infile, const char* signpubkey, int verbose, |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 675 | const char* key_block_file, |
Randall Spangler | 7ecb39d | 2011-05-12 15:37:45 -0700 | [diff] [blame] | 676 | uint64_t kernel_body_load_address, uint64_t min_version, |
| 677 | uint64_t pad) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 678 | |
| 679 | VbKeyBlockHeader* key_block; |
| 680 | VbKernelPreambleHeader* preamble; |
| 681 | VbPublicKey* data_key; |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame] | 682 | VbPublicKey* sign_key = NULL; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 683 | RSAPublicKey* rsa; |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 684 | blob_t* bp; |
| 685 | uint64_t now; |
| 686 | int rv = 1; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 687 | |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame] | 688 | if (!infile) { |
| 689 | error("Must specify filename\n"); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 690 | return 1; |
| 691 | } |
| 692 | |
| 693 | /* Read public signing key */ |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame] | 694 | if (signpubkey) { |
| 695 | sign_key = PublicKeyRead(signpubkey); |
| 696 | if (!sign_key) { |
| 697 | error("Error reading signpubkey.\n"); |
| 698 | return 1; |
| 699 | } |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /* Read blob */ |
Randall Spangler | 7ecb39d | 2011-05-12 15:37:45 -0700 | [diff] [blame] | 703 | bp = OldBlob(infile, pad); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 704 | if (!bp) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 705 | error("Error reading input file\n"); |
| 706 | return 1; |
| 707 | } |
| 708 | |
| 709 | /* Verify key block */ |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 710 | key_block = bp->key_block; |
Randall Spangler | 138acfe | 2010-08-17 15:45:21 -0700 | [diff] [blame] | 711 | if (0 != KeyBlockVerify(key_block, bp->blob_size, sign_key, |
| 712 | (sign_key ? 0 : 1))) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 713 | error("Error verifying key block.\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 714 | goto verify_exit; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 715 | } |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 716 | now = key_block->key_block_size; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 717 | |
Will Drewry | 9342f88 | 2010-10-26 10:22:05 -0500 | [diff] [blame] | 718 | if (key_block_file) { |
| 719 | FILE* f = NULL; |
| 720 | f = fopen(key_block_file, "wb"); |
| 721 | if (!f) { |
| 722 | error("Can't open key block file %s\n", key_block_file); |
| 723 | return 1; |
| 724 | } |
| 725 | if (1 != fwrite(key_block, key_block->key_block_size, 1, f)) { |
| 726 | error("Can't write key block file %s\n", key_block_file); |
| 727 | return 1; |
| 728 | } |
| 729 | fclose(f); |
| 730 | } |
| 731 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 732 | printf("Key block:\n"); |
| 733 | data_key = &key_block->data_key; |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame] | 734 | if (verbose) |
| 735 | printf(" Signature: %s\n", sign_key ? "valid" : "ignored"); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 736 | printf(" Size: 0x%" PRIx64 "\n", key_block->key_block_size); |
Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame] | 737 | printf(" Flags: %" PRIu64 " ", key_block->key_block_flags); |
| 738 | if (key_block->key_block_flags & KEY_BLOCK_FLAG_DEVELOPER_0) |
| 739 | printf(" !DEV"); |
| 740 | if (key_block->key_block_flags & KEY_BLOCK_FLAG_DEVELOPER_1) |
| 741 | printf(" DEV"); |
| 742 | if (key_block->key_block_flags & KEY_BLOCK_FLAG_RECOVERY_0) |
| 743 | printf(" !REC"); |
| 744 | if (key_block->key_block_flags & KEY_BLOCK_FLAG_RECOVERY_1) |
| 745 | printf(" REC"); |
| 746 | printf("\n"); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 747 | printf(" Data key algorithm: %" PRIu64 " %s\n", data_key->algorithm, |
| 748 | (data_key->algorithm < kNumAlgorithms ? |
| 749 | algo_strings[data_key->algorithm] : "(invalid)")); |
| 750 | printf(" Data key version: %" PRIu64 "\n", data_key->key_version); |
Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame] | 751 | printf(" Data key sha1sum: "); |
| 752 | PrintPubKeySha1Sum(data_key); |
| 753 | printf("\n"); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 754 | |
Randall Spangler | ae87b92 | 2011-05-12 13:27:28 -0700 | [diff] [blame] | 755 | if (data_key->key_version < (min_version >> 16)) { |
| 756 | error("Data key version %" PRIu64 " is lower than minimum %" PRIu64".\n", |
| 757 | data_key->key_version, (min_version >> 16)); |
| 758 | goto verify_exit; |
| 759 | } |
| 760 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 761 | rsa = PublicKeyToRSA(&key_block->data_key); |
| 762 | if (!rsa) { |
| 763 | error("Error parsing data key.\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 764 | goto verify_exit; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /* Verify preamble */ |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 768 | preamble = bp->preamble; |
Randall Spangler | 87c13d8 | 2010-07-19 10:35:40 -0700 | [diff] [blame] | 769 | if (0 != VerifyKernelPreamble( |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame] | 770 | preamble, bp->blob_size - key_block->key_block_size, rsa)) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 771 | error("Error verifying preamble.\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 772 | goto verify_exit; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 773 | } |
| 774 | now += preamble->preamble_size; |
| 775 | |
| 776 | printf("Preamble:\n"); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 777 | printf(" Size: 0x%" PRIx64 "\n", preamble->preamble_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 778 | printf(" Header version: %" PRIu32 ".%" PRIu32"\n", |
| 779 | preamble->header_version_major, preamble->header_version_minor); |
| 780 | printf(" Kernel version: %" PRIu64 "\n", preamble->kernel_version); |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 781 | printf(" Body load address: 0x%" PRIx64 "\n", preamble->body_load_address); |
| 782 | printf(" Body size: 0x%" PRIx64 "\n", |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 783 | preamble->body_signature.data_size); |
Randall Spangler | 87c13d8 | 2010-07-19 10:35:40 -0700 | [diff] [blame] | 784 | printf(" Bootloader address: 0x%" PRIx64 "\n", |
| 785 | preamble->bootloader_address); |
Bill Richardson | 249677d | 2010-06-23 11:16:37 -0700 | [diff] [blame] | 786 | printf(" Bootloader size: 0x%" PRIx64 "\n", preamble->bootloader_size); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 787 | |
Randall Spangler | ae87b92 | 2011-05-12 13:27:28 -0700 | [diff] [blame] | 788 | if (preamble->kernel_version < (min_version & 0xFFFF)) { |
| 789 | error("Kernel version %" PRIu64 " is lower than minimum %" PRIu64 ".\n", |
| 790 | preamble->kernel_version, (min_version & 0xFFFF)); |
| 791 | goto verify_exit; |
| 792 | } |
| 793 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 794 | /* Verify body */ |
Randall Spangler | 87c13d8 | 2010-07-19 10:35:40 -0700 | [diff] [blame] | 795 | if (0 != VerifyData(bp->blob, bp->blob_size, &preamble->body_signature, |
| 796 | rsa)) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 797 | error("Error verifying kernel body.\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 798 | goto verify_exit; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 799 | } |
| 800 | printf("Body verification succeeded.\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 801 | |
| 802 | rv = 0; |
| 803 | |
| 804 | if (!verbose) { |
| 805 | goto verify_exit; |
| 806 | } |
| 807 | |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 808 | printf("Config:\n%s\n", BpCmdLineLocation(bp, kernel_body_load_address)); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 809 | |
| 810 | verify_exit: |
| 811 | FreeBlob(bp); |
| 812 | return rv; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | |
| 816 | int main(int argc, char* argv[]) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 817 | char* filename = NULL; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 818 | char* oldfile = NULL; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 819 | char* key_block_file = NULL; |
| 820 | char* signpubkey = NULL; |
| 821 | char* signprivate = NULL; |
vbendeb | 00b9088 | 2010-10-21 13:46:16 -0700 | [diff] [blame] | 822 | int version = -1; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 823 | char* vmlinuz = NULL; |
| 824 | char* bootloader = NULL; |
| 825 | char* config_file = NULL; |
Che-Liang Chiou | 0376203 | 2011-02-22 11:16:51 +0800 | [diff] [blame] | 826 | int arch = ARCH_X86; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 827 | int vblockonly = 0; |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 828 | int verbose = 0; |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 829 | uint64_t kernel_body_load_address = CROS_32BIT_ENTRY_ADDR; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 830 | uint64_t pad = DEFAULT_PADDING; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 831 | int mode = 0; |
| 832 | int parse_error = 0; |
Randall Spangler | ae87b92 | 2011-05-12 13:27:28 -0700 | [diff] [blame] | 833 | uint64_t min_version = 0; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 834 | char* e; |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 835 | int i,r; |
| 836 | blob_t *bp; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 837 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 838 | |
| 839 | char *progname = strrchr(argv[0], '/'); |
| 840 | if (progname) |
| 841 | progname++; |
| 842 | else |
| 843 | progname = argv[0]; |
| 844 | |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 845 | while (((i = getopt_long(argc, argv, ":", long_opts, NULL)) != -1) && |
| 846 | !parse_error) { |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 847 | switch (i) { |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 848 | default: |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 849 | case '?': |
| 850 | /* Unhandled option */ |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 851 | parse_error = 1; |
| 852 | break; |
| 853 | |
Bill Richardson | 4f36ef3 | 2010-08-09 17:50:14 -0700 | [diff] [blame] | 854 | case 0: |
| 855 | /* silently handled option */ |
| 856 | break; |
| 857 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 858 | case OPT_MODE_PACK: |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 859 | case OPT_MODE_REPACK: |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 860 | case OPT_MODE_VERIFY: |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 861 | if (mode && (mode != i)) { |
| 862 | fprintf(stderr, "Only single mode can be specified\n"); |
| 863 | parse_error = 1; |
| 864 | break; |
| 865 | } |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 866 | mode = i; |
| 867 | filename = optarg; |
| 868 | break; |
| 869 | |
Che-Liang Chiou | 0376203 | 2011-02-22 11:16:51 +0800 | [diff] [blame] | 870 | case OPT_ARCH: |
| 871 | if (!strcasecmp(optarg, "x86")) |
| 872 | arch = ARCH_X86; |
| 873 | else if (!strcasecmp(optarg, "arm")) |
| 874 | arch = ARCH_ARM; |
| 875 | else { |
| 876 | fprintf(stderr, "Unknown architecture string: %s\n", optarg); |
| 877 | parse_error = 1; |
| 878 | } |
| 879 | break; |
| 880 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 881 | case OPT_OLDBLOB: |
| 882 | oldfile = optarg; |
| 883 | break; |
| 884 | |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 885 | case OPT_KLOADADDR: |
| 886 | kernel_body_load_address = strtoul(optarg, &e, 0); |
| 887 | if (!*optarg || (e && *e)) { |
| 888 | fprintf(stderr, "Invalid --kloadaddr\n"); |
| 889 | parse_error = 1; |
| 890 | } |
| 891 | break; |
| 892 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 893 | case OPT_KEYBLOCK: |
| 894 | key_block_file = optarg; |
| 895 | break; |
| 896 | |
| 897 | case OPT_SIGNPUBKEY: |
| 898 | signpubkey = optarg; |
| 899 | break; |
| 900 | |
| 901 | case OPT_SIGNPRIVATE: |
| 902 | signprivate = optarg; |
| 903 | break; |
| 904 | |
| 905 | case OPT_VMLINUZ: |
| 906 | vmlinuz = optarg; |
| 907 | break; |
| 908 | |
| 909 | case OPT_BOOTLOADER: |
| 910 | bootloader = optarg; |
| 911 | break; |
| 912 | |
| 913 | case OPT_CONFIG: |
| 914 | config_file = optarg; |
| 915 | break; |
| 916 | |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 917 | case OPT_VBLOCKONLY: |
| 918 | vblockonly = 1; |
| 919 | break; |
| 920 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 921 | case OPT_VERSION: |
| 922 | version = strtoul(optarg, &e, 0); |
| 923 | if (!*optarg || (e && *e)) { |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 924 | fprintf(stderr, "Invalid --version\n"); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 925 | parse_error = 1; |
| 926 | } |
| 927 | break; |
| 928 | |
Randall Spangler | ae87b92 | 2011-05-12 13:27:28 -0700 | [diff] [blame] | 929 | case OPT_MINVERSION: |
| 930 | min_version = strtoul(optarg, &e, 0); |
| 931 | if (!*optarg || (e && *e)) { |
| 932 | fprintf(stderr, "Invalid --minversion\n"); |
| 933 | parse_error = 1; |
| 934 | } |
| 935 | break; |
| 936 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 937 | case OPT_PAD: |
| 938 | pad = strtoul(optarg, &e, 0); |
| 939 | if (!*optarg || (e && *e)) { |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 940 | fprintf(stderr, "Invalid --pad\n"); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 941 | parse_error = 1; |
| 942 | } |
| 943 | break; |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 944 | |
| 945 | case OPT_VERBOSE: |
| 946 | verbose = 1; |
| 947 | break; |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 948 | } |
| 949 | } |
| 950 | |
| 951 | if (parse_error) |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 952 | return PrintHelp(progname); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 953 | |
| 954 | switch(mode) { |
| 955 | case OPT_MODE_PACK: |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 956 | bp = NewBlob(version, vmlinuz, bootloader, config_file, arch, |
| 957 | kernel_body_load_address); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 958 | if (!bp) |
| 959 | return 1; |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 960 | r = Pack(filename, key_block_file, signprivate, bp, pad, vblockonly, |
| 961 | kernel_body_load_address); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 962 | FreeBlob(bp); |
| 963 | return r; |
| 964 | |
| 965 | case OPT_MODE_REPACK: |
vbendeb | 00b9088 | 2010-10-21 13:46:16 -0700 | [diff] [blame] | 966 | if (!config_file && !key_block_file && (version<0)) { |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 967 | fprintf(stderr, |
vbendeb | 858fffb | 2010-10-06 09:51:44 -0700 | [diff] [blame] | 968 | "You must supply at least one of " |
| 969 | "--config, --keyblock or --version\n"); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 970 | return 1; |
| 971 | } |
| 972 | |
Randall Spangler | 7ecb39d | 2011-05-12 15:37:45 -0700 | [diff] [blame] | 973 | bp = OldBlob(oldfile, pad); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 974 | if (!bp) |
| 975 | return 1; |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 976 | r = ReplaceConfig(bp, config_file, kernel_body_load_address); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 977 | if (!r) { |
vbendeb | 00b9088 | 2010-10-21 13:46:16 -0700 | [diff] [blame] | 978 | if (version >= 0) { |
Che-Liang Chiou | 0376203 | 2011-02-22 11:16:51 +0800 | [diff] [blame] | 979 | bp->kernel_version = (uint64_t) version; |
vbendeb | 858fffb | 2010-10-06 09:51:44 -0700 | [diff] [blame] | 980 | } |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 981 | r = Pack(filename, key_block_file, signprivate, bp, pad, vblockonly, |
| 982 | kernel_body_load_address); |
vbendeb | b2b0fcc | 2010-07-15 15:09:47 -0700 | [diff] [blame] | 983 | } |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 984 | FreeBlob(bp); |
| 985 | return r; |
| 986 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 987 | case OPT_MODE_VERIFY: |
Che-Liang Chiou | 2c0711b | 2011-03-22 13:15:19 +0800 | [diff] [blame] | 988 | return Verify(filename, signpubkey, verbose, key_block_file, |
Randall Spangler | 7ecb39d | 2011-05-12 15:37:45 -0700 | [diff] [blame] | 989 | kernel_body_load_address, min_version, pad); |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 990 | |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 991 | default: |
Bill Richardson | a08b5c9 | 2010-06-30 21:59:43 -0700 | [diff] [blame] | 992 | fprintf(stderr, |
| 993 | "You must specify a mode: --pack, --repack or --verify\n"); |
| 994 | return PrintHelp(progname); |
Randall Spangler | 7d6898d | 2010-06-11 09:22:13 -0700 | [diff] [blame] | 995 | } |
| 996 | } |