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