load_kernel_test.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. */
  5. /* Routines for verifying a file's signature. Useful in testing the core
  6. * RSA verification implementation.
  7. */
  8. #include <inttypes.h> /* For PRIu64 macro */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <sys/types.h>
  13. #include <unistd.h>
  14. #include "2sysincludes.h"
  15. #include "2api.h"
  16. #include "2misc.h"
  17. #include "gbb_header.h"
  18. #include "host_common.h"
  19. #include "load_kernel_fw.h"
  20. #include "rollback_index.h"
  21. #include "vboot_common.h"
  22. #include "vboot_kernel.h"
  23. #define LBA_BYTES 512
  24. #define KERNEL_BUFFER_SIZE 0xA00000
  25. /* Global variables for stub functions */
  26. static LoadKernelParams lkp;
  27. static VbCommonParams cparams;
  28. static VbNvContext vnc;
  29. static FILE *image_file = NULL;
  30. /* Boot device stub implementations to read from the image file */
  31. VbError_t VbExDiskRead(VbExDiskHandle_t handle, uint64_t lba_start,
  32. uint64_t lba_count, void *buffer) {
  33. printf("Read(%" PRIu64 ", %" PRIu64 ")\n", lba_start, lba_count);
  34. if (lba_start >= lkp.streaming_lba_count ||
  35. lba_start + lba_count > lkp.streaming_lba_count) {
  36. fprintf(stderr, "Read overrun: %" PRIu64 " + %" PRIu64 " > %" PRIu64 "\n",
  37. lba_start, lba_count, lkp.streaming_lba_count);
  38. return 1;
  39. }
  40. if (0 != fseek(image_file, lba_start * lkp.bytes_per_lba, SEEK_SET) ||
  41. 1 != fread(buffer, lba_count * lkp.bytes_per_lba, 1, image_file)) {
  42. fprintf(stderr, "Read error.");
  43. return 1;
  44. }
  45. return VBERROR_SUCCESS;
  46. }
  47. VbError_t VbExDiskWrite(VbExDiskHandle_t handle, uint64_t lba_start,
  48. uint64_t lba_count, const void *buffer) {
  49. printf("Write(%" PRIu64 ", %" PRIu64 ")\n", lba_start, lba_count);
  50. if (lba_start >= lkp.streaming_lba_count ||
  51. lba_start + lba_count > lkp.streaming_lba_count) {
  52. fprintf(stderr, "Read overrun: %" PRIu64 " + %" PRIu64 " > %" PRIu64 "\n",
  53. lba_start, lba_count, lkp.streaming_lba_count);
  54. return 1;
  55. }
  56. /* TODO: enable writes, once we're sure it won't trash our example file */
  57. return VBERROR_SUCCESS;
  58. fseek(image_file, lba_start * lkp.bytes_per_lba, SEEK_SET);
  59. if (1 != fwrite(buffer, lba_count * lkp.bytes_per_lba, 1, image_file)) {
  60. fprintf(stderr, "Read error.");
  61. return 1;
  62. }
  63. return VBERROR_SUCCESS;
  64. }
  65. #define BOOT_FLAG_DEVELOPER (1 << 0)
  66. #define BOOT_FLAG_RECOVERY (1 << 1)
  67. /* Main routine */
  68. int main(int argc, char* argv[]) {
  69. const char* image_name;
  70. uint64_t key_size;
  71. uint8_t* key_blob = NULL;
  72. VbSharedDataHeader* shared;
  73. GoogleBinaryBlockHeader* gbb;
  74. VbError_t rv;
  75. int c, argsleft;
  76. int errorcnt = 0;
  77. char *e = 0;
  78. memset(&lkp, 0, sizeof(LoadKernelParams));
  79. lkp.bytes_per_lba = LBA_BYTES;
  80. int boot_flags = BOOT_FLAG_RECOVERY;
  81. memset(&vnc, 0, sizeof(VbNvContext));
  82. VbNvSetup(&vnc);
  83. lkp.nv_context = &vnc;
  84. memset(&cparams, 0, sizeof(VbCommonParams));
  85. /* Parse options */
  86. opterr = 0;
  87. while ((c=getopt(argc, argv, ":b:")) != -1)
  88. {
  89. switch (c)
  90. {
  91. case 'b':
  92. boot_flags = strtoull(optarg, &e, 0);
  93. if (!*optarg || (e && *e))
  94. {
  95. fprintf(stderr, "Invalid argument to -%c: \"%s\"\n", c, optarg);
  96. errorcnt++;
  97. }
  98. break;
  99. case '?':
  100. fprintf(stderr, "Unrecognized switch: -%c\n", optopt);
  101. errorcnt++;
  102. break;
  103. case ':':
  104. fprintf(stderr, "Missing argument to -%c\n", optopt);
  105. errorcnt++;
  106. break;
  107. default:
  108. errorcnt++;
  109. break;
  110. }
  111. }
  112. /* Update argc */
  113. argsleft = argc - optind;
  114. if (errorcnt || !argsleft)
  115. {
  116. fprintf(stderr, "usage: %s [options] <drive_image> [<sign_key>]\n",
  117. argv[0]);
  118. fprintf(stderr, "\noptions:\n");
  119. /* These cases are because uint64_t isn't necessarily the same as ULL. */
  120. fprintf(stderr, " -b NUM boot flag bits (default %d):\n",
  121. BOOT_FLAG_RECOVERY);
  122. fprintf(stderr, " %d = developer mode on\n",
  123. BOOT_FLAG_DEVELOPER);
  124. fprintf(stderr, " %d = recovery mode on\n",
  125. BOOT_FLAG_RECOVERY);
  126. return 1;
  127. }
  128. image_name = argv[optind];
  129. /* Read header signing key blob */
  130. if (argsleft > 1) {
  131. key_blob = ReadFile(argv[optind+1], &key_size);
  132. if (!key_blob) {
  133. fprintf(stderr, "Unable to read key file %s\n", argv[optind+1]);
  134. return 1;
  135. }
  136. printf("Read %" PRIu64 " bytes of key from %s\n", key_size, argv[optind+1]);
  137. if (key_size > 16*1024*1024) {
  138. fprintf(stderr, "Key blob size=%" PRIu64 " is ridiculous.\n", key_size);
  139. free(key_blob);
  140. return 1;
  141. }
  142. }
  143. /* Initialize the GBB */
  144. cparams.gbb_size = sizeof(GoogleBinaryBlockHeader) + key_size;
  145. cparams.gbb = gbb = (GoogleBinaryBlockHeader*)malloc(cparams.gbb_size);
  146. memset(gbb, 0, cparams.gbb_size);
  147. memcpy(gbb->signature, GBB_SIGNATURE, GBB_SIGNATURE_SIZE);
  148. gbb->major_version = GBB_MAJOR_VER;
  149. gbb->minor_version = GBB_MINOR_VER;
  150. gbb->header_size = sizeof(GoogleBinaryBlockHeader);
  151. /* Fill in the given key, if any, for both root and recovery */
  152. if (key_blob) {
  153. gbb->rootkey_offset = gbb->header_size;
  154. gbb->rootkey_size = key_size;
  155. memcpy((uint8_t*)gbb + gbb->rootkey_offset, key_blob, key_size);
  156. gbb->recovery_key_offset = gbb->rootkey_offset;
  157. gbb->recovery_key_size = key_size;
  158. }
  159. /* Initialize the shared data area */
  160. cparams.shared_data_blob = malloc(VB_SHARED_DATA_REC_SIZE);
  161. cparams.shared_data_size = VB_SHARED_DATA_REC_SIZE;
  162. shared = (VbSharedDataHeader*)cparams.shared_data_blob;
  163. if (0 != VbSharedDataInit(shared, cparams.shared_data_size)) {
  164. fprintf(stderr, "Unable to init shared data\n");
  165. return 1;
  166. }
  167. /* Copy in the key blob, if any */
  168. if (key_blob) {
  169. if (0 != VbSharedDataSetKernelKey(shared, (VbPublicKey*)key_blob)) {
  170. fprintf(stderr, "Unable to set key in shared data\n");
  171. return 1;
  172. }
  173. }
  174. /* Free the key blob, now that we're done with it */
  175. free(key_blob);
  176. printf("bootflags = %d\n", boot_flags);
  177. lkp.boot_flags = boot_flags;
  178. /* Get image size */
  179. printf("Reading from image: %s\n", image_name);
  180. image_file = fopen(image_name, "rb");
  181. if (!image_file) {
  182. fprintf(stderr, "Unable to open image file %s\n", image_name);
  183. return 1;
  184. }
  185. fseek(image_file, 0, SEEK_END);
  186. lkp.streaming_lba_count = (ftell(image_file) / LBA_BYTES);
  187. lkp.gpt_lba_count = lkp.streaming_lba_count;
  188. rewind(image_file);
  189. printf("Streaming LBA count: %" PRIu64 "\n", lkp.streaming_lba_count);
  190. /* Allocate a buffer for the kernel */
  191. lkp.kernel_buffer = malloc(KERNEL_BUFFER_SIZE);
  192. if(!lkp.kernel_buffer) {
  193. fprintf(stderr, "Unable to allocate kernel buffer.\n");
  194. return 1;
  195. }
  196. lkp.kernel_buffer_size = KERNEL_BUFFER_SIZE;
  197. /*
  198. * Set up vboot context.
  199. *
  200. * TODO: Propagate this up to higher API levels
  201. */
  202. struct vb2_context ctx;
  203. memset(&ctx, 0, sizeof(ctx));
  204. /* No need to initialize ctx->nvdata[]; defaults are fine */
  205. /* TODO(chromium:441893): support dev-mode flag and external gpt flag */
  206. ctx.workbuf = malloc(VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE);
  207. if (!ctx.workbuf) {
  208. fprintf(stderr, "Can't allocate workbuf\n");
  209. return 1;
  210. }
  211. ctx.workbuf_size = VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE;
  212. if (boot_flags & BOOT_FLAG_RECOVERY)
  213. ctx.flags |= VB2_CONTEXT_RECOVERY_MODE;
  214. if (boot_flags & BOOT_FLAG_DEVELOPER)
  215. ctx.flags |= VB2_CONTEXT_DEVELOPER_MODE;
  216. if (VB2_SUCCESS != vb2_init_context(&ctx)) {
  217. free(ctx.workbuf);
  218. fprintf(stderr, "Can't init context\n");
  219. return 1;
  220. }
  221. /* Call LoadKernel() */
  222. rv = LoadKernel(&ctx, &lkp, &cparams);
  223. printf("LoadKernel() returned %d\n", rv);
  224. if (VBERROR_SUCCESS == rv) {
  225. printf("Partition number: %u\n", lkp.partition_number);
  226. printf("Bootloader address: %" PRIu64 "\n", lkp.bootloader_address);
  227. printf("Bootloader size: %u\n", lkp.bootloader_size);
  228. printf("Partition guid: "
  229. "%02x%02x%02x%02x-%02x%02x-%02x%02x"
  230. "-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
  231. lkp.partition_guid[3],
  232. lkp.partition_guid[2],
  233. lkp.partition_guid[1],
  234. lkp.partition_guid[0],
  235. lkp.partition_guid[5],
  236. lkp.partition_guid[4],
  237. lkp.partition_guid[7],
  238. lkp.partition_guid[6],
  239. lkp.partition_guid[8],
  240. lkp.partition_guid[9],
  241. lkp.partition_guid[10],
  242. lkp.partition_guid[11],
  243. lkp.partition_guid[12],
  244. lkp.partition_guid[13],
  245. lkp.partition_guid[14],
  246. lkp.partition_guid[15]);
  247. }
  248. fclose(image_file);
  249. free(lkp.kernel_buffer);
  250. return rv != VBERROR_SUCCESS;
  251. }