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