vboot_nvstorage.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. /* Non-volatile storage routines for verified boot. */
  6. #ifndef VBOOT_REFERENCE_NVSTORAGE_H_
  7. #define VBOOT_REFERENCE_NVSTORAGE_H_
  8. #include <stdint.h>
  9. #define VBNV_BLOCK_SIZE 16 /* Size of NV storage block in bytes */
  10. typedef struct VbNvContext {
  11. /* Raw NV data. Caller must fill this before calling VbNvSetup(). */
  12. uint8_t raw[VBNV_BLOCK_SIZE];
  13. /*
  14. * Flag indicating whether raw data has changed. Set by VbNvTeardown()
  15. * if the raw data has changed and needs to be stored to the underlying
  16. * non-volatile data store.
  17. */
  18. int raw_changed;
  19. /*
  20. * Internal data for NV storage routines. Caller should not touch
  21. * these fields.
  22. */
  23. int regenerate_crc;
  24. } VbNvContext;
  25. /* Parameter type for VbNvGet(), VbNvSet(). */
  26. typedef enum VbNvParam {
  27. /*
  28. * Parameter values have been reset to defaults (flag for firmware).
  29. * 0=clear; 1=set.
  30. */
  31. VBNV_FIRMWARE_SETTINGS_RESET = 0,
  32. /*
  33. * Parameter values have been reset to defaults (flag for kernel).
  34. * 0=clear; 1=set.
  35. */
  36. VBNV_KERNEL_SETTINGS_RESET,
  37. /* Request debug reset on next S3->S0 transition. 0=clear; 1=set. */
  38. VBNV_DEBUG_RESET_MODE,
  39. /*
  40. * Number of times to try booting RW firmware slot B before slot A.
  41. * Valid range: 0-15.
  42. *
  43. * Vboot2: Number of times to try the firmware in VBNV_FW_TRY_NEXT.
  44. *
  45. * These refer to the same field, but have different enum values so
  46. * case statement don't complain about duplicates.
  47. */
  48. VBNV_TRY_B_COUNT,
  49. VBNV_FW_TRY_COUNT,
  50. /*
  51. * Request recovery mode on next boot; see VBNB_RECOVERY_* below for
  52. * currently defined reason codes. 8-bit value.
  53. */
  54. VBNV_RECOVERY_REQUEST,
  55. /*
  56. * Localization index for screen bitmaps displayed by firmware.
  57. * 8-bit value.
  58. */
  59. VBNV_LOCALIZATION_INDEX,
  60. /* Field reserved for kernel/user-mode use; 32-bit value. */
  61. VBNV_KERNEL_FIELD,
  62. /* Allow booting from USB in developer mode. 0=no, 1=yes. */
  63. VBNV_DEV_BOOT_USB,
  64. /* Allow booting of legacy OSes in developer mode. 0=no, 1=yes. */
  65. VBNV_DEV_BOOT_LEGACY,
  66. /* Only boot Google-signed images in developer mode. 0=no, 1=yes. */
  67. VBNV_DEV_BOOT_SIGNED_ONLY,
  68. /*
  69. * Allow full fastboot capability in firmware in developer mode.
  70. * 0=no, 1=yes.
  71. */
  72. VBNV_DEV_BOOT_FASTBOOT_FULL_CAP,
  73. /* Set default boot mode (see VbDevDefaultBoot) */
  74. VBNV_DEV_DEFAULT_BOOT,
  75. /*
  76. * Set by userspace to request that RO firmware disable dev-mode on the
  77. * next boot. This is likely only possible if the dev-switch is
  78. * virtual.
  79. */
  80. VBNV_DISABLE_DEV_REQUEST,
  81. /*
  82. * Set and cleared by vboot to request that the video Option ROM be
  83. * loaded at boot time, so that BIOS screens can be displayed. 0=no,
  84. * 1=yes.
  85. */
  86. VBNV_OPROM_NEEDED,
  87. /* Request that the firmware clear the TPM owner on the next boot. */
  88. VBNV_CLEAR_TPM_OWNER_REQUEST,
  89. /* Flag that TPM owner was cleared on request. */
  90. VBNV_CLEAR_TPM_OWNER_DONE,
  91. /* TPM requested a reboot */
  92. VBNV_TPM_REQUESTED_REBOOT,
  93. /* More details on recovery reason */
  94. VBNV_RECOVERY_SUBCODE,
  95. /* Request that NVRAM be backed up at next boot if possible. */
  96. VBNV_BACKUP_NVRAM_REQUEST,
  97. /* Vboot2: Firmware slot to try next. 0=A, 1=B */
  98. VBNV_FW_TRY_NEXT,
  99. /* Vboot2: Firmware slot tried this boot (0=A, 1=B) */
  100. VBNV_FW_TRIED,
  101. /* Vboot2: Result of trying that firmware (see vb2_fw_result) */
  102. VBNV_FW_RESULT,
  103. /* Firmware slot tried previous boot (0=A, 1=B) */
  104. VBNV_FW_PREV_TRIED,
  105. /* Result of trying that firmware (see vb2_fw_result) */
  106. VBNV_FW_PREV_RESULT,
  107. /* Wipeout request from firmware present. */
  108. VBNV_FW_REQ_WIPEOUT,
  109. /* Fastboot: Unlock in firmware, 0=disabled, 1=enabled. */
  110. VBNV_FASTBOOT_UNLOCK_IN_FW,
  111. /* Boot system when AC detected (0=no, 1=yes). */
  112. VBNV_BOOT_ON_AC_DETECT,
  113. /* Try to update the EC-RO image (0=no, 1=yes). */
  114. VBNV_TRY_RO_SYNC,
  115. /*
  116. * Finish mode transition (if requested), perform battery cut-off and
  117. * shutdown in next boot. */
  118. VBNV_BATTERY_CUTOFF_REQUEST,
  119. } VbNvParam;
  120. /* Set default boot in developer mode */
  121. typedef enum VbDevDefaultBoot {
  122. /* Default to boot from disk*/
  123. VBNV_DEV_DEFAULT_BOOT_DISK = 0,
  124. /* Default to boot from USB */
  125. VBNV_DEV_DEFAULT_BOOT_USB = 1,
  126. /* Default to boot legacy OS */
  127. VBNV_DEV_DEFAULT_BOOT_LEGACY = 2,
  128. } VbDevDefaultBoot;
  129. /* Result of trying the firmware in VBNV_FW_TRIED */
  130. typedef enum VbFwResult {
  131. /* Unknown */
  132. VBNV_FW_RESULT_UNKNOWN = 0,
  133. /* Trying a new slot, but haven't reached success/failure */
  134. VBNV_FW_RESULT_TRYING = 1,
  135. /* Successfully booted to the OS */
  136. VBNV_FW_RESULT_SUCCESS = 2,
  137. /* Known failure */
  138. VBNV_FW_RESULT_FAILURE = 3,
  139. } VbFwResult;
  140. /* Recovery reason codes for VBNV_RECOVERY_REQUEST */
  141. /* Recovery not requested. */
  142. #define VBNV_RECOVERY_NOT_REQUESTED 0x00
  143. /*
  144. * Recovery requested from legacy utility. (Prior to the NV storage spec,
  145. * recovery mode was a single bitfield; this value is reserved so that scripts
  146. * which wrote 1 to the recovery field are distinguishable from scripts whch
  147. * use the recovery reasons listed here.
  148. */
  149. #define VBNV_RECOVERY_LEGACY 0x01
  150. /* User manually requested recovery via recovery button */
  151. #define VBNV_RECOVERY_RO_MANUAL 0x02
  152. /* RW firmware failed signature check (neither RW firmware slot was valid) */
  153. #define VBNV_RECOVERY_RO_INVALID_RW 0x03
  154. /* S3 resume failed */
  155. #define VBNV_RECOVERY_RO_S3_RESUME 0x04
  156. /* TPM error in read-only firmware (deprecated) */
  157. #define VBNV_RECOVERY_DEP_RO_TPM_ERROR 0x05
  158. /* Shared data error in read-only firmware */
  159. #define VBNV_RECOVERY_RO_SHARED_DATA 0x06
  160. /* Test error from S3Resume() */
  161. #define VBNV_RECOVERY_RO_TEST_S3 0x07
  162. /* Test error from LoadFirmwareSetup() */
  163. #define VBNV_RECOVERY_RO_TEST_LFS 0x08
  164. /* Test error from LoadFirmware() */
  165. #define VBNV_RECOVERY_RO_TEST_LF 0x09
  166. /*
  167. * RW firmware failed signature check (neither RW firmware slot was valid).
  168. * Recovery reason is VBNV_RECOVERY_RO_INVALID_RW_CHECK_MIN + the check value
  169. * for the slot which came closest to validating; see VBSD_LF_CHECK_* in
  170. * vboot_struct.h.
  171. */
  172. #define VBNV_RECOVERY_RO_INVALID_RW_CHECK_MIN 0x10
  173. #define VBNV_RECOVERY_RO_INVALID_RW_CHECK_MAX 0x1F
  174. /*
  175. * Firmware boot failure outside of verified boot (RAM init, missing SSD,
  176. * etc.).
  177. */
  178. #define VBNV_RECOVERY_RO_FIRMWARE 0x20
  179. /*
  180. * Recovery mode TPM initialization requires a system reboot. The system was
  181. * already in recovery mode for some other reason when this happened.
  182. */
  183. #define VBNV_RECOVERY_RO_TPM_REBOOT 0x21
  184. /* EC software sync - other error */
  185. #define VBNV_RECOVERY_EC_SOFTWARE_SYNC 0x22
  186. /* EC software sync - unable to determine active EC image */
  187. #define VBNV_RECOVERY_EC_UNKNOWN_IMAGE 0x23
  188. /* EC software sync - error obtaining EC image hash (deprecated) */
  189. #define VBNV_RECOVERY_DEP_EC_HASH 0x24
  190. /* EC software sync - error obtaining expected EC image */
  191. #define VBNV_RECOVERY_EC_EXPECTED_IMAGE 0x25
  192. /* EC software sync - error updating EC */
  193. #define VBNV_RECOVERY_EC_UPDATE 0x26
  194. /* EC software sync - unable to jump to EC-RW */
  195. #define VBNV_RECOVERY_EC_JUMP_RW 0x27
  196. /* EC software sync - unable to protect / unprotect EC-RW */
  197. #define VBNV_RECOVERY_EC_PROTECT 0x28
  198. /* EC software sync - error obtaining expected EC hash */
  199. #define VBNV_RECOVERY_EC_EXPECTED_HASH 0x29
  200. /* EC software sync - expected EC image doesn't match hash */
  201. #define VBNV_RECOVERY_EC_HASH_MISMATCH 0x2A
  202. /* VB2: Secure data inititalization error */
  203. #define VBNV_RECOVERY_VB2_SECDATA_INIT 0x2B
  204. /* VB2: GBB header is bad */
  205. #define VBNV_RECOVERY_VB2_GBB_HEADER 0x2C
  206. /* VB2: Unable to clear TPM owner */
  207. #define VBNV_RECOVERY_VB2_TPM_CLEAR_OWNER 0x2D
  208. /* VB2: Error determining/updating virtual dev switch */
  209. #define VBNV_RECOVERY_VB2_DEV_SWITCH 0x2E
  210. /* VB2: Error determining firmware slot */
  211. #define VBNV_RECOVERY_VB2_FW_SLOT 0x2F
  212. /* Unspecified/unknown error in read-only firmware */
  213. #define VBNV_RECOVERY_RO_UNSPECIFIED 0x3F
  214. /*
  215. * User manually requested recovery by pressing a key at developer
  216. * warning screen
  217. */
  218. #define VBNV_RECOVERY_RW_DEV_SCREEN 0x41
  219. /* No OS kernel detected */
  220. #define VBNV_RECOVERY_RW_NO_OS 0x42
  221. /* OS kernel failed signature check */
  222. #define VBNV_RECOVERY_RW_INVALID_OS 0x43
  223. /* TPM error in rewritable firmware (deprecated) */
  224. #define VBNV_RECOVERY_DEP_RW_TPM_ERROR 0x44
  225. /* RW firmware in dev mode, but dev switch is off */
  226. #define VBNV_RECOVERY_RW_DEV_MISMATCH 0x45
  227. /* Shared data error in rewritable firmware */
  228. #define VBNV_RECOVERY_RW_SHARED_DATA 0x46
  229. /* Test error from LoadKernel() */
  230. #define VBNV_RECOVERY_RW_TEST_LK 0x47
  231. /* No bootable disk found (deprecated)*/
  232. #define VBNV_RECOVERY_DEP_RW_NO_DISK 0x48
  233. /* Rebooting did not correct TPM_E_FAIL or TPM_E_FAILEDSELFTEST */
  234. #define VBNV_RECOVERY_TPM_E_FAIL 0x49
  235. /* TPM setup error in read-only firmware */
  236. #define VBNV_RECOVERY_RO_TPM_S_ERROR 0x50
  237. /* TPM write error in read-only firmware */
  238. #define VBNV_RECOVERY_RO_TPM_W_ERROR 0x51
  239. /* TPM lock error in read-only firmware */
  240. #define VBNV_RECOVERY_RO_TPM_L_ERROR 0x52
  241. /* TPM update error in read-only firmware */
  242. #define VBNV_RECOVERY_RO_TPM_U_ERROR 0x53
  243. /* TPM read error in rewritable firmware */
  244. #define VBNV_RECOVERY_RW_TPM_R_ERROR 0x54
  245. /* TPM write error in rewritable firmware */
  246. #define VBNV_RECOVERY_RW_TPM_W_ERROR 0x55
  247. /* TPM lock error in rewritable firmware */
  248. #define VBNV_RECOVERY_RW_TPM_L_ERROR 0x56
  249. /* EC software sync unable to get EC image hash */
  250. #define VBNV_RECOVERY_EC_HASH_FAILED 0x57
  251. /* EC software sync invalid image hash size */
  252. #define VBNV_RECOVERY_EC_HASH_SIZE 0x58
  253. /* Unspecified error while trying to load kernel */
  254. #define VBNV_RECOVERY_LK_UNSPECIFIED 0x59
  255. /* No bootable storage device in system */
  256. #define VBNV_RECOVERY_RW_NO_DISK 0x5A
  257. /* No bootable kernel found on disk */
  258. #define VBNV_RECOVERY_RW_NO_KERNEL 0x5B
  259. /* BCB-related error in RW firmware */
  260. #define VBNV_RECOVERY_RW_BCB_ERROR 0x5C
  261. /* Fastboot mode requested in firmware */
  262. #define VBNV_RECOVERY_FW_FASTBOOT 0x5E
  263. /* Recovery hash space lock error in RO firmware */
  264. #define VBNV_RECOVERY_RO_TPM_REC_HASH_L_ERROR 0x5F
  265. /* Unspecified/unknown error in rewritable firmware */
  266. #define VBNV_RECOVERY_RW_UNSPECIFIED 0x7F
  267. /* DM-verity error */
  268. #define VBNV_RECOVERY_KE_DM_VERITY 0x81
  269. /* Unspecified/unknown error in kernel */
  270. #define VBNV_RECOVERY_KE_UNSPECIFIED 0xBF
  271. /* Recovery mode test from user-mode */
  272. #define VBNV_RECOVERY_US_TEST 0xC1
  273. /* Recovery requested by user-mode via BCB */
  274. #define VBNV_RECOVERY_BCB_USER_MODE 0xC2
  275. /* Fastboot mode requested by user-mode */
  276. #define VBNV_RECOVERY_US_FASTBOOT 0xC3
  277. /* User requested recovery for training memory and rebooting. */
  278. #define VBNV_RECOVERY_TRAIN_AND_REBOOT 0xC4
  279. /* Unspecified/unknown error in user-mode */
  280. #define VBNV_RECOVERY_US_UNSPECIFIED 0xFF
  281. /**
  282. * Initialize the NV storage library.
  283. *
  284. * This must be called before any other functions in this library. Returns 0
  285. * if success, non-zero if error.
  286. *
  287. * Proper calling procedure:
  288. * 1) Allocate a context struct.
  289. * 2) If multi-threaded/multi-process, acquire a lock to prevent
  290. * other processes from modifying the underlying storage.
  291. * 3) Read underlying storage and fill in context->raw.
  292. * 4) Call VbNvSetup().
  293. *
  294. * If you have access to global variables, you may want to wrap all that in
  295. * your own VbNvOpen() function. We don't do that in here because there are no
  296. * global variables in UEFI BIOS during the PEI phase (that's also why we have
  297. * to pass around a context pointer).
  298. */
  299. int VbNvSetup(VbNvContext *context);
  300. /**
  301. * Clean up and flush changes back to the raw data.
  302. *
  303. * This must be called after other functions in this library. Returns 0 if
  304. * success, non-zero if error.
  305. *
  306. * Proper calling procedure:
  307. * 1) Call VbNvExit().
  308. * 2) If context.raw_changed, write data back to underlying storage.
  309. * 3) Release any lock you acquired before calling VbNvSetup().
  310. * 4) Free the context struct.
  311. *
  312. * If you have access to global variables, you may want to wrap this
  313. * in your own VbNvClose() function.
  314. */
  315. int VbNvTeardown(VbNvContext *context);
  316. /**
  317. * Read a NV storage parameter into *dest.
  318. *
  319. * Returns 0 if success, non-zero if error.
  320. *
  321. * This may only be called between VbNvSetup() and VbNvTeardown().
  322. */
  323. int VbNvGet(VbNvContext *context, VbNvParam param, uint32_t *dest);
  324. /**
  325. * Set a NV storage param to a new value.
  326. *
  327. * Returns 0 if success, non-zero if error.
  328. *
  329. * This may only be called between VbNvSetup() and VbNvTeardown().
  330. */
  331. int VbNvSet(VbNvContext *context, VbNvParam param, uint32_t value);
  332. #endif /* VBOOT_REFERENCE_NVSTORAGE_H_ */