vboot_struct.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /* Copyright (c) 2013 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. * Data structure definitions for verified boot, for on-disk / in-eeprom
  6. * data.
  7. */
  8. #ifndef VBOOT_REFERENCE_VBOOT_STRUCT_H_
  9. #define VBOOT_REFERENCE_VBOOT_STRUCT_H_
  10. #include <stdint.h>
  11. /* Public key data */
  12. typedef struct VbPublicKey {
  13. /* Offset of key data from start of this struct */
  14. uint64_t key_offset;
  15. /* Size of key data in bytes (NOT strength of key in bits) */
  16. uint64_t key_size;
  17. /* Signature algorithm used by the key */
  18. uint64_t algorithm;
  19. /* Key version */
  20. uint64_t key_version;
  21. } __attribute__((packed)) VbPublicKey;
  22. #define EXPECTED_VBPUBLICKEY_SIZE 32
  23. /* Signature data (a secure hash, possibly signed) */
  24. typedef struct VbSignature {
  25. /* Offset of signature data from start of this struct */
  26. uint64_t sig_offset;
  27. /* Size of signature data in bytes */
  28. uint64_t sig_size;
  29. /* Size of the data block which was signed in bytes */
  30. uint64_t data_size;
  31. } __attribute__((packed)) VbSignature;
  32. #define EXPECTED_VBSIGNATURE_SIZE 24
  33. #define KEY_BLOCK_MAGIC "CHROMEOS"
  34. #define KEY_BLOCK_MAGIC_SIZE 8
  35. #define KEY_BLOCK_HEADER_VERSION_MAJOR 2
  36. #define KEY_BLOCK_HEADER_VERSION_MINOR 1
  37. /* Flags for key_block_flags */
  38. /* The following flags set where the key is valid */
  39. #define KEY_BLOCK_FLAG_DEVELOPER_0 (0x01ULL) /* Developer switch off */
  40. #define KEY_BLOCK_FLAG_DEVELOPER_1 (0x02ULL) /* Developer switch on */
  41. #define KEY_BLOCK_FLAG_RECOVERY_0 (0x04ULL) /* Not recovery mode */
  42. #define KEY_BLOCK_FLAG_RECOVERY_1 (0x08ULL) /* Recovery mode */
  43. /*
  44. * Key block, containing the public key used to sign some other chunk of data.
  45. *
  46. * This should be followed by:
  47. * 1) The data_key key data, pointed to by data_key.key_offset.
  48. * 2) The checksum data for (VBKeyBlockHeader + data_key data), pointed to
  49. * by key_block_checksum.sig_offset.
  50. * 3) The signature data for (VBKeyBlockHeader + data_key data), pointed to
  51. * by key_block_signature.sig_offset.
  52. */
  53. typedef struct VbKeyBlockHeader {
  54. /* Magic number */
  55. uint8_t magic[KEY_BLOCK_MAGIC_SIZE];
  56. /* Version of this header format */
  57. uint32_t header_version_major;
  58. /* Version of this header format */
  59. uint32_t header_version_minor;
  60. /*
  61. * Length of this entire key block, including keys, signatures, and
  62. * padding, in bytes
  63. */
  64. uint64_t key_block_size;
  65. /*
  66. * Signature for this key block (header + data pointed to by data_key)
  67. * For use with signed data keys
  68. */
  69. VbSignature key_block_signature;
  70. /*
  71. * SHA-512 checksum for this key block (header + data pointed to by
  72. * data_key) For use with unsigned data keys
  73. */
  74. VbSignature key_block_checksum;
  75. /* Flags for key (KEY_BLOCK_FLAG_*) */
  76. uint64_t key_block_flags;
  77. /* Key to verify the chunk of data */
  78. VbPublicKey data_key;
  79. } __attribute__((packed)) VbKeyBlockHeader;
  80. #define EXPECTED_VBKEYBLOCKHEADER_SIZE 112
  81. /****************************************************************************/
  82. #define KERNEL_PREAMBLE_HEADER_VERSION_MAJOR 2
  83. #define KERNEL_PREAMBLE_HEADER_VERSION_MINOR 2
  84. /* Preamble block for kernel, version 2.0
  85. *
  86. * This should be followed by:
  87. * 1) The signature data for the kernel body, pointed to by
  88. * body_signature.sig_offset.
  89. * 2) The signature data for (vb2_kernel_preamble + body signature
  90. * data), pointed to by preamble_signature.sig_offset.
  91. */
  92. typedef struct VbKernelPreambleHeader2_0 {
  93. /*
  94. * Size of this preamble, including keys, signatures, and padding, in
  95. * bytes
  96. */
  97. uint64_t preamble_size;
  98. /* Signature for this preamble (header + body signature) */
  99. VbSignature preamble_signature;
  100. /* Version of this header format */
  101. uint32_t header_version_major;
  102. /* Version of this header format */
  103. uint32_t header_version_minor;
  104. /* Kernel version */
  105. uint64_t kernel_version;
  106. /* Load address for kernel body */
  107. uint64_t body_load_address;
  108. /* Address of bootloader, after body is loaded at body_load_address */
  109. uint64_t bootloader_address;
  110. /* Size of bootloader in bytes */
  111. uint64_t bootloader_size;
  112. /* Signature for the kernel body */
  113. VbSignature body_signature;
  114. } __attribute__((packed)) VbKernelPreambleHeader2_0;
  115. #define EXPECTED_VBKERNELPREAMBLEHEADER2_0_SIZE 96
  116. /* Preamble block for kernel, version 2.1
  117. *
  118. * This should be followed by:
  119. * 1) The signature data for the kernel body, pointed to by
  120. * body_signature.sig_offset.
  121. * 2) The signature data for (vb2_fw_preamble + body signature
  122. * data), pointed to by preamble_signature.sig_offset.
  123. * 3) The 16-bit vmlinuz header, which is used for reconstruction of
  124. * vmlinuz image.
  125. */
  126. typedef struct VbKernelPreambleHeader {
  127. /*
  128. * Size of this preamble, including keys, signatures, vmlinuz header,
  129. * and padding, in bytes
  130. */
  131. uint64_t preamble_size;
  132. /* Signature for this preamble (header + body signature) */
  133. VbSignature preamble_signature;
  134. /* Version of this header format */
  135. uint32_t header_version_major;
  136. /* Version of this header format */
  137. uint32_t header_version_minor;
  138. /* Kernel version */
  139. uint64_t kernel_version;
  140. /* Load address for kernel body */
  141. uint64_t body_load_address;
  142. /* Address of bootloader, after body is loaded at body_load_address */
  143. uint64_t bootloader_address;
  144. /* Size of bootloader in bytes */
  145. uint64_t bootloader_size;
  146. /* Signature for the kernel body */
  147. VbSignature body_signature;
  148. /*
  149. * Fields added in header version 2.1. You must verify the header
  150. * version before reading these fields!
  151. */
  152. /* Address of 16-bit header for vmlinuz reassembly. Readers should
  153. return 0 for header version < 2.1 */
  154. uint64_t vmlinuz_header_address;
  155. /* Size of 16-bit header for vmlinuz in bytes. Readers should return 0
  156. for header version < 2.1 */
  157. uint64_t vmlinuz_header_size;
  158. /*
  159. * Flags passed in by the signer. Readers should return 0 for header
  160. * version < 2.2. Flags field is currently defined as:
  161. * [31:2] - Reserved (for future use)
  162. * [1:0] - Kernel image type (0b00 - CrOS, 0b01 - bootimg)
  163. */
  164. uint32_t flags;
  165. } __attribute__((packed)) VbKernelPreambleHeader;
  166. #define EXPECTED_VBKERNELPREAMBLEHEADER2_1_SIZE 112
  167. #define EXPECTED_VBKERNELPREAMBLEHEADER2_2_SIZE 116
  168. /****************************************************************************/
  169. /* Constants and sub-structures for VbSharedDataHeader */
  170. /* Magic number for recognizing VbSharedDataHeader ("VbSD") */
  171. #define VB_SHARED_DATA_MAGIC 0x44536256
  172. /* Minimum and recommended size of shared_data_blob in bytes. */
  173. #define VB_SHARED_DATA_MIN_SIZE 3072
  174. #define VB_SHARED_DATA_REC_SIZE 16384
  175. /* Flags for VbSharedDataHeader */
  176. /* LoadFirmware() tried firmware B because of VbNvStorage firmware B tries */
  177. #define VBSD_FWB_TRIED 0x00000001
  178. /*
  179. * LoadKernel() verified the good kernel keyblock using the kernel subkey from
  180. * the firmware. If this flag is not present, it just used the hash of the
  181. * kernel keyblock.
  182. */
  183. #define VBSD_KERNEL_KEY_VERIFIED 0x00000002
  184. /* LoadFirmware() was told the developer switch was on */
  185. #define VBSD_LF_DEV_SWITCH_ON 0x00000004
  186. /*
  187. * LoadFirmware() is requesting the read only normal/dev code path. This is
  188. * deprecated and unsupported by current firmware.
  189. */
  190. #define VBSD_LF_USE_RO_NORMAL 0x00000008
  191. /* Developer switch was enabled at boot time */
  192. #define VBSD_BOOT_DEV_SWITCH_ON 0x00000010
  193. /* Recovery switch was enabled at boot time */
  194. #define VBSD_BOOT_REC_SWITCH_ON 0x00000020
  195. /* Firmware write protect was enabled at boot time */
  196. #define VBSD_BOOT_FIRMWARE_WP_ENABLED 0x00000040
  197. /* Boot is a S3->S0 resume, not a S5->S0 normal boot */
  198. #define VBSD_BOOT_S3_RESUME 0x00000100
  199. /* Read-only firmware supports the normal/developer code path */
  200. #define VBSD_BOOT_RO_NORMAL_SUPPORT 0x00000200
  201. /* VbInit() was told that the system has a virtual dev-switch */
  202. #define VBSD_HONOR_VIRT_DEV_SWITCH 0x00000400
  203. /* VbInit() was told the system supports EC software sync */
  204. #define VBSD_EC_SOFTWARE_SYNC 0x00000800
  205. /* VbInit() was told that the EC firmware is slow to update */
  206. #define VBSD_EC_SLOW_UPDATE 0x00001000
  207. /* Firmware software write protect was enabled at boot time */
  208. #define VBSD_BOOT_FIRMWARE_SW_WP_ENABLED 0x00002000
  209. /* VbInit() was told that the recovery button is a virtual one */
  210. #define VBSD_BOOT_REC_SWITCH_VIRTUAL 0x00004000
  211. /* Firmware used vboot2 for firmware selection */
  212. #define VBSD_BOOT_FIRMWARE_VBOOT2 0x00008000
  213. /* Firmware needs VGA Option ROM to display screens */
  214. #define VBSD_OPROM_MATTERS 0x00010000
  215. /* Firmware has loaded the VGA Option ROM */
  216. #define VBSD_OPROM_LOADED 0x00020000
  217. /* Don't try for boot failures */
  218. #define VBSD_NOFAIL_BOOT 0x00040000
  219. /*
  220. * Supported flags by header version. It's ok to add new flags while keeping
  221. * struct version 2 as long as flag-NOT-present is the correct value for
  222. * existing hardware (Stumpy/Lumpy).
  223. */
  224. #define VBSD_FLAGS_VERSION_1 0x00000007 /* Alex, ZGB */
  225. #define VBSD_FLAGS_VERSION_2 0x00000F7F
  226. /* Result codes for VbSharedDataHeader.check_fw_a_result (and b_result) */
  227. #define VBSD_LF_CHECK_NOT_DONE 0
  228. #define VBSD_LF_CHECK_DEV_MISMATCH 1
  229. #define VBSD_LF_CHECK_REC_MISMATCH 2
  230. #define VBSD_LF_CHECK_VERIFY_KEYBLOCK 3
  231. #define VBSD_LF_CHECK_KEY_ROLLBACK 4
  232. #define VBSD_LF_CHECK_DATA_KEY_PARSE 5
  233. #define VBSD_LF_CHECK_VERIFY_PREAMBLE 6
  234. #define VBSD_LF_CHECK_FW_ROLLBACK 7
  235. #define VBSD_LF_CHECK_HEADER_VALID 8
  236. #define VBSD_LF_CHECK_GET_FW_BODY 9
  237. #define VBSD_LF_CHECK_HASH_WRONG_SIZE 10
  238. #define VBSD_LF_CHECK_VERIFY_BODY 11
  239. #define VBSD_LF_CHECK_VALID 12
  240. /*
  241. * Read-only normal path requested by firmware preamble, but unsupported by
  242. * firmware.
  243. */
  244. #define VBSD_LF_CHECK_NO_RO_NORMAL 13
  245. /* Boot mode for VbSharedDataHeader.lk_boot_mode */
  246. #define VBSD_LK_BOOT_MODE_RECOVERY 0
  247. #define VBSD_LK_BOOT_MODE_NORMAL 1
  248. #define VBSD_LK_BOOT_MODE_DEVELOPER 2
  249. /* Flags for VbSharedDataKernelPart.flags */
  250. #define VBSD_LKP_FLAG_KEY_BLOCK_VALID 0x01
  251. /* Result codes for VbSharedDataKernelPart.check_result */
  252. #define VBSD_LKP_CHECK_NOT_DONE 0
  253. #define VBSD_LKP_CHECK_TOO_SMALL 1
  254. #define VBSD_LKP_CHECK_READ_START 2
  255. #define VBSD_LKP_CHECK_KEY_BLOCK_SIG 3
  256. #define VBSD_LKP_CHECK_KEY_BLOCK_HASH 4
  257. #define VBSD_LKP_CHECK_DEV_MISMATCH 5
  258. #define VBSD_LKP_CHECK_REC_MISMATCH 6
  259. #define VBSD_LKP_CHECK_KEY_ROLLBACK 7
  260. #define VBSD_LKP_CHECK_DATA_KEY_PARSE 8
  261. #define VBSD_LKP_CHECK_VERIFY_PREAMBLE 9
  262. #define VBSD_LKP_CHECK_KERNEL_ROLLBACK 10
  263. #define VBSD_LKP_CHECK_PREAMBLE_VALID 11
  264. /*
  265. * Body load address check is omitted; this result code is deprecated and not
  266. * used anywhere in the codebase.
  267. */
  268. #define VBSD_LKP_CHECK_BODY_ADDRESS 12
  269. #define VBSD_LKP_CHECK_BODY_OFFSET 13
  270. #define VBSD_LKP_CHECK_SELF_SIGNED 14
  271. #define VBSD_LKP_CHECK_BODY_EXCEEDS_MEM 15
  272. #define VBSD_LKP_CHECK_BODY_EXCEEDS_PART 16
  273. #define VBSD_LKP_CHECK_READ_DATA 17
  274. #define VBSD_LKP_CHECK_VERIFY_DATA 18
  275. #define VBSD_LKP_CHECK_KERNEL_GOOD 19
  276. /* Information about a single kernel partition check in LoadKernel() */
  277. typedef struct VbSharedDataKernelPart {
  278. uint64_t sector_start; /* Start sector of partition */
  279. uint64_t sector_count; /* Sector count of partition */
  280. uint32_t combined_version; /* Combined key+kernel version */
  281. uint8_t gpt_index; /* Index of partition in GPT */
  282. uint8_t check_result; /* Check result; see VBSD_LKP_CHECK_* */
  283. uint8_t flags; /* Flags (see VBSD_LKP_FLAG_* */
  284. uint8_t reserved0; /* Reserved for padding */
  285. } VbSharedDataKernelPart;
  286. /* Number of kernel partitions to track per call. Must be power of 2. */
  287. #define VBSD_MAX_KERNEL_PARTS 8
  288. /* Flags for VbSharedDataKernelCall.flags */
  289. /* Error initializing TPM in recovery mode */
  290. #define VBSD_LK_FLAG_REC_TPM_INIT_ERROR 0x00000001
  291. /* Result codes for VbSharedDataKernelCall.check_result */
  292. #define VBSD_LKC_CHECK_NOT_DONE 0
  293. #define VBSD_LKC_CHECK_DEV_SWITCH_MISMATCH 1
  294. #define VBSD_LKC_CHECK_GPT_READ_ERROR 2
  295. #define VBSD_LKC_CHECK_GPT_PARSE_ERROR 3
  296. #define VBSD_LKC_CHECK_GOOD_PARTITION 4
  297. #define VBSD_LKC_CHECK_INVALID_PARTITIONS 5
  298. #define VBSD_LKC_CHECK_NO_PARTITIONS 6
  299. /* Information about a single call to LoadKernel() */
  300. typedef struct VbSharedDataKernelCall {
  301. /* Bottom 32 bits of flags passed in LoadKernelParams.boot_flags */
  302. uint32_t boot_flags;
  303. /* Debug flags; see VBSD_LK_FLAG_* */
  304. uint32_t flags;
  305. /* Number of sectors on drive */
  306. uint64_t sector_count;
  307. /* Sector size in bytes */
  308. uint32_t sector_size;
  309. /* Check result; see VBSD_LKC_CHECK_* */
  310. uint8_t check_result;
  311. /* Boot mode for LoadKernel(); see VBSD_LK_BOOT_MODE_* constants */
  312. uint8_t boot_mode;
  313. /* Test error number, if non-zero */
  314. uint8_t test_error_num;
  315. /* Return code from LoadKernel() */
  316. uint8_t return_code;
  317. /* Number of kernel partitions found */
  318. uint8_t kernel_parts_found;
  319. /* Reserved for padding */
  320. uint8_t reserved0[7];
  321. /* Data on kernels */
  322. VbSharedDataKernelPart parts[VBSD_MAX_KERNEL_PARTS];
  323. } VbSharedDataKernelCall;
  324. /* Number of kernel calls to track. Must be power of 2. */
  325. #define VBSD_MAX_KERNEL_CALLS 4
  326. /*
  327. * Data shared between LoadFirmware(), LoadKernel(), and OS.
  328. *
  329. * The boot process is:
  330. * 1) Caller allocates buffer, at least VB_SHARED_DATA_MIN bytes, ideally
  331. * VB_SHARED_DATA_REC_SIZE bytes.
  332. * 2) If non-recovery boot, this is passed to LoadFirmware(), which
  333. * initializes the buffer, adding this header and some data.
  334. * 3) Buffer is passed to LoadKernel(). If this is a recovery boot,
  335. * LoadKernel() initializes the buffer, adding this header. Regardless
  336. * of boot type, LoadKernel() adds some data to the buffer.
  337. * 4) Caller makes data available to the OS in a platform-dependent manner.
  338. * For example, via ACPI or ATAGs.
  339. */
  340. typedef struct VbSharedDataHeader {
  341. /* Fields present in version 1 */
  342. /* Magic number for struct (VB_SHARED_DATA_MAGIC) */
  343. uint32_t magic;
  344. /* Version of this structure */
  345. uint32_t struct_version;
  346. /* Size of this structure in bytes */
  347. uint64_t struct_size;
  348. /* Size of shared data buffer in bytes */
  349. uint64_t data_size;
  350. /* Amount of shared data used so far */
  351. uint64_t data_used;
  352. /* Flags */
  353. uint32_t flags;
  354. /* Reserved for padding */
  355. uint32_t reserved0;
  356. /* Kernel subkey, from firmware */
  357. VbPublicKey kernel_subkey;
  358. /* Offset of kernel subkey data from start of this struct */
  359. uint64_t kernel_subkey_data_offset;
  360. /* Size of kernel subkey data */
  361. uint64_t kernel_subkey_data_size;
  362. /*
  363. * Timer values from VbExGetTimer(). Unused values are set to 0. Note
  364. * that these are now the enter/exit times for the wrapper API entry
  365. * points; see crosbug.com/17018. */
  366. /* VbInit() enter/exit */
  367. uint64_t timer_vb_init_enter;
  368. uint64_t timer_vb_init_exit;
  369. /* VbSelectFirmware() enter/exit */
  370. uint64_t timer_vb_select_firmware_enter;
  371. uint64_t timer_vb_select_firmware_exit;
  372. /* VbSelectAndLoadKernel() enter/exit */
  373. uint64_t timer_vb_select_and_load_kernel_enter;
  374. uint64_t timer_vb_select_and_load_kernel_exit;
  375. /* Information stored in TPM, as retrieved by firmware */
  376. /* Current firmware version in TPM */
  377. uint32_t fw_version_tpm;
  378. /* Current kernel version in TPM */
  379. uint32_t kernel_version_tpm;
  380. /* Debugging information from LoadFirmware() */
  381. /* Result of checking RW firmware A and B */
  382. uint8_t check_fw_a_result;
  383. uint8_t check_fw_b_result;
  384. /* Firmware index returned by LoadFirmware() or 0xFF if failure */
  385. uint8_t firmware_index;
  386. /* Reserved for padding */
  387. uint8_t reserved1;
  388. /* Firmware TPM version at start of VbSelectFirmware() */
  389. uint32_t fw_version_tpm_start;
  390. /* Firmware lowest version found */
  391. uint32_t fw_version_lowest;
  392. /* Debugging information from LoadKernel() */
  393. /* Number of times LoadKernel() called */
  394. uint32_t lk_call_count;
  395. /* Info on calls */
  396. VbSharedDataKernelCall lk_calls[VBSD_MAX_KERNEL_CALLS];
  397. /*
  398. * Offset and size of supplemental kernel data. Reserve space for
  399. * these fields now, so that future LoadKernel() versions can store
  400. * information there without needing to shift down whatever data the
  401. * original LoadFirmware() might have put immediately following its
  402. * VbSharedDataHeader.
  403. */
  404. uint64_t kernel_supplemental_offset;
  405. uint64_t kernel_supplemental_size;
  406. /*
  407. * Fields added in version 2. Before accessing, make sure that
  408. * struct_version >= 2
  409. */
  410. /* Recovery reason for current boot */
  411. uint8_t recovery_reason;
  412. /* Reserved for padding */
  413. uint8_t reserved2[7];
  414. /* Flags from firmware keyblock */
  415. uint64_t fw_keyblock_flags;
  416. /* Kernel TPM version at start of VbSelectAndLoadKernel() */
  417. uint32_t kernel_version_tpm_start;
  418. /* Kernel lowest version found */
  419. uint32_t kernel_version_lowest;
  420. /*
  421. * After read-only firmware which uses version 2 is released, any
  422. * additional fields must be added below, and the struct version must
  423. * be increased. Before reading/writing those fields, make sure that
  424. * the struct being accessed is at least version 3.
  425. *
  426. * It's always ok for an older firmware to access a newer struct, since
  427. * all the fields it knows about are present. Newer firmware needs to
  428. * use reasonable defaults when accessing older structs.
  429. */
  430. } __attribute__((packed)) VbSharedDataHeader;
  431. /*
  432. * Size of VbSharedDataheader for each version
  433. *
  434. * TODO: crossystem needs not to fail if called on a v1 system where
  435. * sizeof(VbSharedDataHeader) was smaller
  436. */
  437. #define VB_SHARED_DATA_HEADER_SIZE_V1 1072
  438. #define VB_SHARED_DATA_HEADER_SIZE_V2 1096
  439. #define VB_SHARED_DATA_VERSION 2 /* Version for struct_version */
  440. #endif /* VBOOT_REFERENCE_VBOOT_STRUCT_H_ */