vb1_helper.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /*
  2. * Copyright 2014 The Chromium OS Authors. All rights reserved.
  3. * Use of this source code is governed by a BSD-style license that can be
  4. * found in the LICENSE file.
  5. */
  6. #include <errno.h>
  7. #include <inttypes.h> /* For PRIu64 */
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <openssl/rsa.h>
  12. #include "2sysincludes.h"
  13. #include "2api.h"
  14. #include "2common.h"
  15. #include "2rsa.h"
  16. #include "2sha.h"
  17. #include "file_type.h"
  18. #include "futility.h"
  19. #include "host_common.h"
  20. #include "kernel_blob.h"
  21. #include "util_misc.h"
  22. #include "vb1_helper.h"
  23. #include "vb2_common.h"
  24. /****************************************************************************/
  25. /* Here are globals containing all the bits & pieces I'm working on.
  26. *
  27. * kernel vblock = keyblock + kernel preamble + padding to 64K (or whatever)
  28. * kernel blob = 32-bit kernel + config file + params + bootloader stub +
  29. * vmlinuz_header
  30. * kernel partition = kernel vblock + kernel blob
  31. *
  32. * The vb2_kernel_preamble.preamble_size includes the padding.
  33. */
  34. /* The keyblock, preamble, and kernel blob are kept in separate places. */
  35. static struct vb2_keyblock *g_keyblock;
  36. static struct vb2_kernel_preamble *g_preamble;
  37. static uint8_t *g_kernel_blob_data;
  38. static uint32_t g_kernel_blob_size;
  39. /* These refer to individual parts within the kernel blob. */
  40. static uint8_t *g_kernel_data;
  41. static uint32_t g_kernel_size;
  42. static uint8_t *g_config_data;
  43. static uint32_t g_config_size;
  44. static uint8_t *g_param_data;
  45. static uint32_t g_param_size;
  46. static uint8_t *g_bootloader_data;
  47. static uint32_t g_bootloader_size;
  48. static uint8_t *g_vmlinuz_header_data;
  49. static uint32_t g_vmlinuz_header_size;
  50. static uint64_t g_ondisk_bootloader_addr;
  51. static uint64_t g_ondisk_vmlinuz_header_addr;
  52. /*
  53. * Read the kernel command line from a file. Get rid of \n characters along
  54. * the way and verify that the line fits into a 4K buffer.
  55. *
  56. * Return the buffer contaning the line on success (and set the line length
  57. * using the passed in parameter), or NULL in case something goes wrong.
  58. */
  59. uint8_t *ReadConfigFile(const char *config_file, uint32_t *config_size)
  60. {
  61. uint8_t *config_buf;
  62. int i;
  63. if (VB2_SUCCESS != vb2_read_file(config_file, &config_buf, config_size))
  64. return NULL;
  65. Debug(" config file size=0x%x\n", *config_size);
  66. if (CROS_CONFIG_SIZE <= *config_size) { /* room for trailing '\0' */
  67. fprintf(stderr, "Config file %s is too large (>= %d bytes)\n",
  68. config_file, CROS_CONFIG_SIZE);
  69. free(config_buf);
  70. return NULL;
  71. }
  72. /* Replace newlines with spaces */
  73. for (i = 0; i < *config_size; i++)
  74. if ('\n' == config_buf[i])
  75. config_buf[i] = ' ';
  76. return config_buf;
  77. }
  78. /****************************************************************************/
  79. /* Return the smallest integral multiple of [alignment] that is equal
  80. * to or greater than [val]. Used to determine the number of
  81. * pages/sectors/blocks/whatever needed to contain [val]
  82. * items/bytes/etc. */
  83. static uint32_t roundup(uint32_t val, uint32_t alignment)
  84. {
  85. uint32_t rem = val % alignment;
  86. if (rem)
  87. return val + (alignment - rem);
  88. return val;
  89. }
  90. /* Match regexp /\b--\b/ to delimit the start of the kernel commandline. If we
  91. * don't find one, we'll use the whole thing. */
  92. static unsigned int find_cmdline_start(uint8_t *buf_ptr, unsigned int max_len)
  93. {
  94. char *input = (char *)buf_ptr;
  95. int start = 0;
  96. int i;
  97. for (i = 0; i < max_len - 1 && input[i]; i++) {
  98. if ('-' == input[i] && '-' == input[i + 1]) {
  99. if ((i == 0 || ' ' == input[i - 1]) &&
  100. (i + 2 >= max_len || ' ' == input[i + 2])) {
  101. /* found "--" with nothing before or after it */
  102. start = i + 2; /* hope for a trailing '\0' */
  103. break;
  104. }
  105. }
  106. }
  107. while (' ' == input[start]) /* skip leading spaces */
  108. start++;
  109. return start;
  110. }
  111. /* Offset of kernel command line string from the start of the kernel blob */
  112. uint64_t kernel_cmd_line_offset(const struct vb2_kernel_preamble *preamble)
  113. {
  114. return preamble->bootloader_address - preamble->body_load_address -
  115. CROS_CONFIG_SIZE - CROS_PARAMS_SIZE;
  116. }
  117. /* Returns the size of the 32-bit kernel, or negative on error. */
  118. static int KernelSize(uint8_t *kernel_buf,
  119. uint32_t kernel_size,
  120. enum arch_t arch)
  121. {
  122. uint32_t kernel32_start = 0;
  123. struct linux_kernel_params *lh;
  124. /* Except for x86, the kernel is the kernel. */
  125. if (arch != ARCH_X86)
  126. return kernel_size;
  127. /* The first part of the x86 vmlinuz is a header, followed by
  128. * a real-mode boot stub. We only want the 32-bit part. */
  129. lh = (struct linux_kernel_params *)kernel_buf;
  130. kernel32_start = (lh->setup_sects + 1) << 9;
  131. if (kernel32_start >= kernel_size) {
  132. fprintf(stderr, "Malformed kernel\n");
  133. return -1;
  134. }
  135. return kernel_size - kernel32_start;
  136. }
  137. /* This extracts g_kernel_* and g_param_* from a standard vmlinuz file.
  138. * It returns nonzero on error. */
  139. static int PickApartVmlinuz(uint8_t *kernel_buf,
  140. uint32_t kernel_size,
  141. enum arch_t arch,
  142. uint64_t kernel_body_load_address)
  143. {
  144. uint32_t kernel32_start = 0;
  145. uint32_t kernel32_size = kernel_size;
  146. struct linux_kernel_params *lh, *params;
  147. /* Except for x86, the kernel is the kernel. */
  148. if (arch == ARCH_X86) {
  149. /* The first part of the x86 vmlinuz is a header, followed by
  150. * a real-mode boot stub. We only want the 32-bit part. */
  151. lh = (struct linux_kernel_params *)kernel_buf;
  152. kernel32_start = (lh->setup_sects + 1) << 9;
  153. if (kernel32_start >= kernel_size) {
  154. fprintf(stderr, "Malformed kernel\n");
  155. return -1;
  156. }
  157. kernel32_size = kernel_size - kernel32_start;
  158. Debug(" kernel16_start=0x%" PRIx64 "\n", 0);
  159. Debug(" kernel16_size=0x%" PRIx64 "\n", kernel32_start);
  160. /* Copy the original zeropage data from kernel_buf into
  161. * g_param_data, then tweak a few fields for our purposes */
  162. params = (struct linux_kernel_params *)(g_param_data);
  163. memcpy(&(params->setup_sects), &(lh->setup_sects),
  164. offsetof(struct linux_kernel_params, e820_entries)
  165. - offsetof(struct linux_kernel_params, setup_sects));
  166. params->boot_flag = 0;
  167. params->ramdisk_image = 0; /* we don't support initrd */
  168. params->ramdisk_size = 0;
  169. params->type_of_loader = 0xff;
  170. /* We need to point to the kernel commandline arg. On disk, it
  171. * will come right after the 32-bit part of the kernel. */
  172. params->cmd_line_ptr = kernel_body_load_address +
  173. roundup(kernel32_size, CROS_ALIGN) +
  174. find_cmdline_start(g_config_data, g_config_size);
  175. Debug(" cmdline_addr=0x%x\n", params->cmd_line_ptr);
  176. Debug(" version=0x%x\n", params->version);
  177. Debug(" kernel_alignment=0x%x\n", params->kernel_alignment);
  178. Debug(" relocatable_kernel=0x%x\n", params->relocatable_kernel);
  179. /* Add a fake e820 memory map with 2 entries. */
  180. params->n_e820_entry = 2;
  181. params->e820_entries[0].start_addr = 0x00000000;
  182. params->e820_entries[0].segment_size = 0x00001000;
  183. params->e820_entries[0].segment_type = E820_TYPE_RAM;
  184. params->e820_entries[1].start_addr = 0xfffff000;
  185. params->e820_entries[1].segment_size = 0x00001000;
  186. params->e820_entries[1].segment_type = E820_TYPE_RESERVED;
  187. }
  188. Debug(" kernel32_start=0x%" PRIx64 "\n", kernel32_start);
  189. Debug(" kernel32_size=0x%" PRIx64 "\n", kernel32_size);
  190. /* Keep just the 32-bit kernel. */
  191. if (kernel32_size) {
  192. g_kernel_size = kernel32_size;
  193. memcpy(g_kernel_data, kernel_buf + kernel32_start,
  194. g_kernel_size);
  195. }
  196. /* done */
  197. return 0;
  198. }
  199. /* Split a kernel blob into separate g_kernel, g_param, g_config,
  200. * g_bootloader, and g_vmlinuz_header parts. */
  201. static void UnpackKernelBlob(uint8_t *kernel_blob_data)
  202. {
  203. uint32_t now;
  204. uint32_t vmlinuz_header_size = 0;
  205. uint64_t vmlinuz_header_address = 0;
  206. /* We have to work backwards from the end, because the preamble
  207. only describes the bootloader and vmlinuz stubs. */
  208. /* Vmlinuz Header is at the end */
  209. vb2_kernel_get_vmlinuz_header(g_preamble,
  210. &vmlinuz_header_address,
  211. &vmlinuz_header_size);
  212. if (vmlinuz_header_size) {
  213. now = vmlinuz_header_address - g_preamble->body_load_address;
  214. g_vmlinuz_header_size = vmlinuz_header_size;
  215. g_vmlinuz_header_data = kernel_blob_data + now;
  216. Debug("vmlinuz_header_size = 0x%x\n",
  217. g_vmlinuz_header_size);
  218. Debug("vmlinuz_header_ofs = 0x%x\n", now);
  219. }
  220. /* Where does the bootloader stub begin? */
  221. now = g_preamble->bootloader_address - g_preamble->body_load_address;
  222. /* Bootloader is at the end */
  223. g_bootloader_size = g_preamble->bootloader_size;
  224. g_bootloader_data = kernel_blob_data + now;
  225. /* TODO: What to do if this is beyond the end of the blob? */
  226. Debug("bootloader_size = 0x%x\n", g_bootloader_size);
  227. Debug("bootloader_ofs = 0x%x\n", now);
  228. /* Before that is the params */
  229. now -= CROS_PARAMS_SIZE;
  230. g_param_size = CROS_PARAMS_SIZE;
  231. g_param_data = kernel_blob_data + now;
  232. Debug("param_ofs = 0x%x\n", now);
  233. /* Before that is the config */
  234. now -= CROS_CONFIG_SIZE;
  235. g_config_size = CROS_CONFIG_SIZE;
  236. g_config_data = kernel_blob_data + now;
  237. Debug("config_ofs = 0x%x\n", now);
  238. /* The kernel starts at offset 0 and extends up to the config */
  239. g_kernel_data = kernel_blob_data;
  240. g_kernel_size = now;
  241. Debug("kernel_size = 0x%x\n", g_kernel_size);
  242. }
  243. /* Replaces the config section of the specified kernel blob.
  244. * Return nonzero on error. */
  245. int UpdateKernelBlobConfig(uint8_t *kblob_data, uint32_t kblob_size,
  246. uint8_t *config_data, uint32_t config_size)
  247. {
  248. /* We should have already examined this blob. If not, we could do it
  249. * again, but it's more likely due to an error. */
  250. if (kblob_data != g_kernel_blob_data ||
  251. kblob_size != g_kernel_blob_size) {
  252. fprintf(stderr, "Trying to update some other blob\n");
  253. return -1;
  254. }
  255. memset(g_config_data, 0, g_config_size);
  256. memcpy(g_config_data, config_data, config_size);
  257. return 0;
  258. }
  259. /* Split a kernel partition into separate vblock and blob parts. */
  260. uint8_t *unpack_kernel_partition(uint8_t *kpart_data,
  261. uint32_t kpart_size,
  262. uint32_t padding,
  263. struct vb2_keyblock **keyblock_ptr,
  264. struct vb2_kernel_preamble **preamble_ptr,
  265. uint32_t *blob_size_ptr)
  266. {
  267. struct vb2_kernel_preamble *preamble;
  268. uint32_t vmlinuz_header_size = 0;
  269. uint64_t vmlinuz_header_address = 0;
  270. uint32_t now = 0;
  271. /* Sanity-check the keyblock */
  272. struct vb2_keyblock *keyblock = (struct vb2_keyblock *)kpart_data;
  273. Debug("Keyblock is 0x%x bytes\n", keyblock->keyblock_size);
  274. now += keyblock->keyblock_size;
  275. if (now > kpart_size) {
  276. fprintf(stderr,
  277. "keyblock_size advances past the end of the blob\n");
  278. return NULL;
  279. }
  280. if (now > padding) {
  281. fprintf(stderr,
  282. "keyblock_size advances past %u byte padding\n",
  283. padding);
  284. return NULL;
  285. }
  286. /* LGTM */
  287. g_keyblock = keyblock;
  288. /* And the preamble */
  289. preamble = (struct vb2_kernel_preamble *)(kpart_data + now);
  290. Debug("Preamble is 0x%x bytes\n", preamble->preamble_size);
  291. now += preamble->preamble_size;
  292. if (now > kpart_size) {
  293. fprintf(stderr,
  294. "preamble_size advances past the end of the blob\n");
  295. return NULL;
  296. }
  297. if (now > padding) {
  298. fprintf(stderr, "preamble_size advances past %u"
  299. " byte padding\n", padding);
  300. return NULL;
  301. }
  302. /* LGTM */
  303. Debug(" kernel_version = %d\n", preamble->kernel_version);
  304. Debug(" bootloader_address = 0x%" PRIx64 "\n",
  305. preamble->bootloader_address);
  306. Debug(" bootloader_size = 0x%x\n", preamble->bootloader_size);
  307. Debug(" kern_blob_size = 0x%x\n", preamble->body_signature.data_size);
  308. uint32_t flags = vb2_kernel_get_flags(preamble);
  309. Debug(" flags = 0x%x\n", flags);
  310. g_preamble = preamble;
  311. g_ondisk_bootloader_addr = g_preamble->bootloader_address;
  312. vb2_kernel_get_vmlinuz_header(preamble,
  313. &vmlinuz_header_address,
  314. &vmlinuz_header_size);
  315. if (vmlinuz_header_size) {
  316. Debug(" vmlinuz_header_address = 0x%" PRIx64 "\n",
  317. vmlinuz_header_address);
  318. Debug(" vmlinuz_header_size = 0x%x\n", vmlinuz_header_size);
  319. g_ondisk_vmlinuz_header_addr = vmlinuz_header_address;
  320. }
  321. Debug("kernel blob is at offset 0x%x\n", now);
  322. g_kernel_blob_data = kpart_data + now;
  323. g_kernel_blob_size = preamble->body_signature.data_size;
  324. /* Sanity check */
  325. if (g_kernel_blob_size < preamble->body_signature.data_size)
  326. fprintf(stderr,
  327. "Warning: kernel file only has 0x%x bytes\n",
  328. g_kernel_blob_size);
  329. /* Update the blob pointers */
  330. UnpackKernelBlob(g_kernel_blob_data);
  331. if (keyblock_ptr)
  332. *keyblock_ptr = keyblock;
  333. if (preamble_ptr)
  334. *preamble_ptr = preamble;
  335. if (blob_size_ptr)
  336. *blob_size_ptr = g_kernel_blob_size;
  337. return g_kernel_blob_data;
  338. }
  339. uint8_t *SignKernelBlob(uint8_t *kernel_blob,
  340. uint32_t kernel_size,
  341. uint32_t padding,
  342. int version,
  343. uint64_t kernel_body_load_address,
  344. struct vb2_keyblock *keyblock,
  345. struct vb2_private_key *signpriv_key,
  346. uint32_t flags,
  347. uint32_t *vblock_size_ptr)
  348. {
  349. /* Make sure the preamble fills up the rest of the required padding */
  350. uint32_t min_size = padding > keyblock->keyblock_size
  351. ? padding - keyblock->keyblock_size : 0;
  352. /* Sign the kernel data */
  353. struct vb2_signature *body_sig = vb2_calculate_signature(kernel_blob,
  354. kernel_size,
  355. signpriv_key);
  356. if (!body_sig) {
  357. fprintf(stderr, "Error calculating body signature\n");
  358. return NULL;
  359. }
  360. /* Create preamble */
  361. struct vb2_kernel_preamble *preamble =
  362. vb2_create_kernel_preamble(version,
  363. kernel_body_load_address,
  364. g_ondisk_bootloader_addr,
  365. g_bootloader_size,
  366. body_sig,
  367. g_ondisk_vmlinuz_header_addr,
  368. g_vmlinuz_header_size,
  369. flags,
  370. min_size,
  371. signpriv_key);
  372. if (!preamble) {
  373. fprintf(stderr, "Error creating preamble.\n");
  374. return 0;
  375. }
  376. uint32_t outsize = keyblock->keyblock_size + preamble->preamble_size;
  377. void *outbuf = calloc(outsize, 1);
  378. memcpy(outbuf, keyblock, keyblock->keyblock_size);
  379. memcpy(outbuf + keyblock->keyblock_size,
  380. preamble, preamble->preamble_size);
  381. if (vblock_size_ptr)
  382. *vblock_size_ptr = outsize;
  383. return outbuf;
  384. }
  385. /* Returns zero on success */
  386. int WriteSomeParts(const char *outfile,
  387. void *part1_data, uint32_t part1_size,
  388. void *part2_data, uint32_t part2_size)
  389. {
  390. FILE *f;
  391. /* Write the output file */
  392. Debug("writing %s with 0x%" PRIx64 ", 0x%" PRIx64 "\n",
  393. outfile, part1_size, part2_size);
  394. f = fopen(outfile, "wb");
  395. if (!f) {
  396. fprintf(stderr, "Can't open output file %s: %s\n",
  397. outfile, strerror(errno));
  398. return -1;
  399. }
  400. if (part1_data && part1_size) {
  401. if (1 != fwrite(part1_data, part1_size, 1, f)) {
  402. fprintf(stderr, "Can't write output file %s: %s\n",
  403. outfile, strerror(errno));
  404. fclose(f);
  405. unlink(outfile);
  406. return -1;
  407. }
  408. }
  409. if (part2_data && part2_size) {
  410. if (1 != fwrite(part2_data, part2_size, 1, f)) {
  411. fprintf(stderr, "Can't write output file %s: %s\n",
  412. outfile, strerror(errno));
  413. fclose(f);
  414. unlink(outfile);
  415. return -1;
  416. }
  417. }
  418. fclose(f);
  419. /* Success */
  420. return 0;
  421. }
  422. /* Returns 0 on success */
  423. int VerifyKernelBlob(uint8_t *kernel_blob,
  424. uint32_t kernel_size,
  425. struct vb2_packed_key *signpub_key,
  426. const char *keyblock_outfile,
  427. uint32_t min_version)
  428. {
  429. int rv = -1;
  430. uint32_t vmlinuz_header_size = 0;
  431. uint64_t vmlinuz_header_address = 0;
  432. uint8_t workbuf[VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE];
  433. struct vb2_workbuf wb;
  434. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  435. if (signpub_key) {
  436. struct vb2_public_key pubkey;
  437. if (VB2_SUCCESS != vb2_unpack_key(&pubkey, signpub_key)) {
  438. fprintf(stderr, "Error unpacking signing key.\n");
  439. goto done;
  440. }
  441. if (VB2_SUCCESS !=
  442. vb2_verify_keyblock(g_keyblock, g_keyblock->keyblock_size,
  443. &pubkey, &wb)) {
  444. fprintf(stderr, "Error verifying key block.\n");
  445. goto done;
  446. }
  447. } else if (VB2_SUCCESS !=
  448. vb2_verify_keyblock_hash(g_keyblock,
  449. g_keyblock->keyblock_size,
  450. &wb)) {
  451. fprintf(stderr, "Error verifying key block.\n");
  452. goto done;
  453. }
  454. printf("Key block:\n");
  455. struct vb2_packed_key *data_key = &g_keyblock->data_key;
  456. printf(" Signature: %s\n",
  457. signpub_key ? "valid" : "ignored");
  458. printf(" Size: 0x%x\n", g_keyblock->keyblock_size);
  459. printf(" Flags: %u ", g_keyblock->keyblock_flags);
  460. if (g_keyblock->keyblock_flags & KEY_BLOCK_FLAG_DEVELOPER_0)
  461. printf(" !DEV");
  462. if (g_keyblock->keyblock_flags & KEY_BLOCK_FLAG_DEVELOPER_1)
  463. printf(" DEV");
  464. if (g_keyblock->keyblock_flags & KEY_BLOCK_FLAG_RECOVERY_0)
  465. printf(" !REC");
  466. if (g_keyblock->keyblock_flags & KEY_BLOCK_FLAG_RECOVERY_1)
  467. printf(" REC");
  468. printf("\n");
  469. printf(" Data key algorithm: %u %s\n", data_key->algorithm,
  470. vb2_get_crypto_algorithm_name(data_key->algorithm));
  471. printf(" Data key version: %u\n", data_key->key_version);
  472. printf(" Data key sha1sum: %s\n",
  473. packed_key_sha1_string(data_key));
  474. if (keyblock_outfile) {
  475. FILE *f = NULL;
  476. f = fopen(keyblock_outfile, "wb");
  477. if (!f) {
  478. fprintf(stderr, "Can't open key block file %s: %s\n",
  479. keyblock_outfile, strerror(errno));
  480. goto done;
  481. }
  482. if (1 != fwrite(g_keyblock, g_keyblock->keyblock_size, 1, f)) {
  483. fprintf(stderr, "Can't write key block file %s: %s\n",
  484. keyblock_outfile, strerror(errno));
  485. fclose(f);
  486. goto done;
  487. }
  488. fclose(f);
  489. }
  490. if (data_key->key_version < (min_version >> 16)) {
  491. fprintf(stderr, "Data key version %u < minimum %u.\n",
  492. data_key->key_version, (min_version >> 16));
  493. goto done;
  494. }
  495. struct vb2_public_key pubkey;
  496. if (VB2_SUCCESS != vb2_unpack_key(&pubkey, data_key)) {
  497. fprintf(stderr, "Error parsing data key.\n");
  498. goto done;
  499. }
  500. /* Verify preamble */
  501. if (VB2_SUCCESS != vb2_verify_kernel_preamble(
  502. (struct vb2_kernel_preamble *)g_preamble,
  503. g_preamble->preamble_size, &pubkey, &wb)) {
  504. fprintf(stderr, "Error verifying preamble.\n");
  505. goto done;
  506. }
  507. printf("Preamble:\n");
  508. printf(" Size: 0x%x\n", g_preamble->preamble_size);
  509. printf(" Header version: %u.%u\n",
  510. g_preamble->header_version_major,
  511. g_preamble->header_version_minor);
  512. printf(" Kernel version: %u\n", g_preamble->kernel_version);
  513. printf(" Body load address: 0x%" PRIx64 "\n",
  514. g_preamble->body_load_address);
  515. printf(" Body size: 0x%x\n",
  516. g_preamble->body_signature.data_size);
  517. printf(" Bootloader address: 0x%" PRIx64 "\n",
  518. g_preamble->bootloader_address);
  519. printf(" Bootloader size: 0x%x\n", g_preamble->bootloader_size);
  520. vb2_kernel_get_vmlinuz_header(g_preamble,
  521. &vmlinuz_header_address,
  522. &vmlinuz_header_size);
  523. if (vmlinuz_header_size) {
  524. printf(" Vmlinuz header address: 0x%" PRIx64 "\n",
  525. vmlinuz_header_address);
  526. printf(" Vmlinuz header size: 0x%x\n",
  527. (uint32_t)vmlinuz_header_size);
  528. }
  529. printf(" Flags : 0x%x\n",
  530. vb2_kernel_get_flags(g_preamble));
  531. if (g_preamble->kernel_version < (min_version & 0xFFFF)) {
  532. fprintf(stderr,
  533. "Kernel version %u is lower than minimum %u.\n",
  534. g_preamble->kernel_version, (min_version & 0xFFFF));
  535. goto done;
  536. }
  537. /* Verify body */
  538. if (VB2_SUCCESS !=
  539. vb2_verify_data(kernel_blob, kernel_size,
  540. &g_preamble->body_signature,
  541. &pubkey, &wb)) {
  542. fprintf(stderr, "Error verifying kernel body.\n");
  543. goto done;
  544. }
  545. printf("Body verification succeeded.\n");
  546. printf("Config:\n%s\n",
  547. kernel_blob + kernel_cmd_line_offset(g_preamble));
  548. rv = 0;
  549. done:
  550. return rv;
  551. }
  552. uint8_t *CreateKernelBlob(uint8_t *vmlinuz_buf, uint32_t vmlinuz_size,
  553. enum arch_t arch, uint64_t kernel_body_load_address,
  554. uint8_t *config_data, uint32_t config_size,
  555. uint8_t *bootloader_data, uint32_t bootloader_size,
  556. uint32_t *blob_size_ptr)
  557. {
  558. uint32_t now = 0;
  559. int tmp;
  560. /* We have all the parts. How much room do we need? */
  561. tmp = KernelSize(vmlinuz_buf, vmlinuz_size, arch);
  562. if (tmp < 0)
  563. return NULL;
  564. g_kernel_size = tmp;
  565. g_config_size = CROS_CONFIG_SIZE;
  566. g_param_size = CROS_PARAMS_SIZE;
  567. g_bootloader_size = roundup(bootloader_size, CROS_ALIGN);
  568. g_vmlinuz_header_size = vmlinuz_size-g_kernel_size;
  569. g_kernel_blob_size =
  570. roundup(g_kernel_size, CROS_ALIGN) +
  571. g_config_size +
  572. g_param_size +
  573. g_bootloader_size +
  574. g_vmlinuz_header_size;
  575. Debug("g_kernel_blob_size 0x%" PRIx64 "\n", g_kernel_blob_size);
  576. /* Allocate space for the blob. */
  577. g_kernel_blob_data = malloc(g_kernel_blob_size);
  578. memset(g_kernel_blob_data, 0, g_kernel_blob_size);
  579. /* Assign the sub-pointers */
  580. g_kernel_data = g_kernel_blob_data + now;
  581. Debug("g_kernel_size 0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
  582. g_kernel_size, now);
  583. now += roundup(g_kernel_size, CROS_ALIGN);
  584. g_config_data = g_kernel_blob_data + now;
  585. Debug("g_config_size 0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
  586. g_config_size, now);
  587. now += g_config_size;
  588. g_param_data = g_kernel_blob_data + now;
  589. Debug("g_param_size 0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
  590. g_param_size, now);
  591. now += g_param_size;
  592. g_bootloader_data = g_kernel_blob_data + now;
  593. Debug("g_bootloader_size 0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
  594. g_bootloader_size, now);
  595. g_ondisk_bootloader_addr = kernel_body_load_address + now;
  596. Debug("g_ondisk_bootloader_addr 0x%" PRIx64 "\n",
  597. g_ondisk_bootloader_addr);
  598. now += g_bootloader_size;
  599. if (g_vmlinuz_header_size) {
  600. g_vmlinuz_header_data = g_kernel_blob_data + now;
  601. Debug("g_vmlinuz_header_size 0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
  602. g_vmlinuz_header_size, now);
  603. g_ondisk_vmlinuz_header_addr = kernel_body_load_address + now;
  604. Debug("g_ondisk_vmlinuz_header_addr 0x%" PRIx64 "\n",
  605. g_ondisk_vmlinuz_header_addr);
  606. }
  607. Debug("end of kern_blob at kern_blob+0x%" PRIx64 "\n", now);
  608. /* Copy the kernel and params bits into the correct places */
  609. if (0 != PickApartVmlinuz(vmlinuz_buf, vmlinuz_size,
  610. arch, kernel_body_load_address)) {
  611. fprintf(stderr, "Error picking apart kernel file.\n");
  612. free(g_kernel_blob_data);
  613. g_kernel_blob_data = NULL;
  614. g_kernel_blob_size = 0;
  615. return NULL;
  616. }
  617. /* Copy the other bits too */
  618. memcpy(g_config_data, config_data, config_size);
  619. memcpy(g_bootloader_data, bootloader_data, bootloader_size);
  620. if (g_vmlinuz_header_size) {
  621. memcpy(g_vmlinuz_header_data,
  622. vmlinuz_buf,
  623. g_vmlinuz_header_size);
  624. }
  625. if (blob_size_ptr)
  626. *blob_size_ptr = g_kernel_blob_size;
  627. return g_kernel_blob_data;
  628. }
  629. enum futil_file_type ft_recognize_vblock1(uint8_t *buf, uint32_t len)
  630. {
  631. uint8_t workbuf[VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE];
  632. struct vb2_workbuf wb;
  633. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  634. /* Vboot 2.0 signature checks destroy the buffer, so make a copy */
  635. uint8_t *buf2 = malloc(len);
  636. memcpy(buf2, buf, len);
  637. struct vb2_keyblock *keyblock = (struct vb2_keyblock *)buf2;
  638. if (VB2_SUCCESS != vb2_verify_keyblock_hash(keyblock, len, &wb)) {
  639. free(buf2);
  640. return FILE_TYPE_UNKNOWN;
  641. }
  642. /* Try unpacking the data key from the keyblock */
  643. struct vb2_public_key data_key;
  644. if (VB2_SUCCESS !=
  645. vb2_unpack_key(&data_key, &keyblock->data_key)) {
  646. /* It looks like a bad keyblock, but still a keyblock */
  647. free(buf2);
  648. return FILE_TYPE_KEYBLOCK;
  649. }
  650. uint32_t more = keyblock->keyblock_size;
  651. /* Followed by firmware preamble too? */
  652. struct vb2_fw_preamble *pre2 = (struct vb2_fw_preamble *)(buf2 + more);
  653. if (VB2_SUCCESS ==
  654. vb2_verify_fw_preamble(pre2, len - more, &data_key, &wb)) {
  655. free(buf2);
  656. return FILE_TYPE_FW_PREAMBLE;
  657. }
  658. /* Recopy since firmware preamble check destroyed the buffer */
  659. memcpy(buf2, buf, len);
  660. /* Or maybe kernel preamble? */
  661. struct vb2_kernel_preamble *kern_preamble =
  662. (struct vb2_kernel_preamble *)(buf2 + more);
  663. if (VB2_SUCCESS ==
  664. vb2_verify_kernel_preamble(kern_preamble, len - more,
  665. &data_key, &wb)) {
  666. free(buf2);
  667. return FILE_TYPE_KERN_PREAMBLE;
  668. }
  669. free(buf2);
  670. /* No, just keyblock */
  671. return FILE_TYPE_KEYBLOCK;
  672. }
  673. enum futil_file_type ft_recognize_vb1_key(uint8_t *buf, uint32_t len)
  674. {
  675. /* Maybe just a packed public key? */
  676. const struct vb2_packed_key *pubkey = (struct vb2_packed_key *)buf;
  677. if (packed_key_looks_ok(pubkey, len))
  678. return FILE_TYPE_PUBKEY;
  679. /* How about a private key? */
  680. if (len < sizeof(uint64_t))
  681. return FILE_TYPE_UNKNOWN;
  682. const unsigned char *start = buf + sizeof(uint64_t);
  683. struct rsa_st *rsa =
  684. d2i_RSAPrivateKey(NULL, &start, len - sizeof(uint64_t));
  685. if (rsa) {
  686. RSA_free(rsa);
  687. return FILE_TYPE_PRIVKEY;
  688. }
  689. return FILE_TYPE_UNKNOWN;
  690. }