2secdata.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* Copyright (c) 2014 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. * Secure non-volatile storage routines
  6. */
  7. #ifndef VBOOT_REFERENCE_VBOOT_SECDATA_H_
  8. #define VBOOT_REFERENCE_VBOOT_SECDATA_H_
  9. /*****************************************************************************/
  10. /* Firmware version space */
  11. /* Expected value of vb2_secdata.version */
  12. #define VB2_SECDATA_VERSION 2
  13. /* Flags for firmware space */
  14. enum vb2_secdata_flags {
  15. /*
  16. * Last boot was developer mode. TPM ownership is cleared when
  17. * transitioning to/from developer mode. Set/cleared by
  18. * vb2_check_dev_switch().
  19. */
  20. VB2_SECDATA_FLAG_LAST_BOOT_DEVELOPER = (1 << 0),
  21. /*
  22. * Virtual developer mode switch is on. Set/cleared by the
  23. * keyboard-controlled dev screens in recovery mode. Cleared by
  24. * vb2_check_dev_switch().
  25. */
  26. VB2_SECDATA_FLAG_DEV_MODE = (1 << 1),
  27. };
  28. /* Secure data area (firmware space) */
  29. struct vb2_secdata {
  30. /* Struct version, for backwards compatibility */
  31. uint8_t struct_version;
  32. /* Flags; see vb2_secdata_flags */
  33. uint8_t flags;
  34. /* Firmware versions */
  35. uint32_t fw_versions;
  36. /* Reserved for future expansion */
  37. uint8_t reserved[3];
  38. /* CRC; must be last field in struct */
  39. uint8_t crc8;
  40. } __attribute__((packed));
  41. /* Which param to get/set for vb2_secdata_get() / vb2_secdata_set() */
  42. enum vb2_secdata_param {
  43. /* Flags; see vb2_secdata_flags */
  44. VB2_SECDATA_FLAGS = 0,
  45. /* Firmware versions */
  46. VB2_SECDATA_VERSIONS,
  47. };
  48. /*****************************************************************************/
  49. /* Kernel version space */
  50. /* Kernel space - KERNEL_NV_INDEX, locked with physical presence. */
  51. #define VB2_SECDATAK_VERSION 2
  52. #define VB2_SECDATAK_UID 0x4752574c /* 'GRWL' */
  53. struct vb2_secdatak {
  54. /* Struct version, for backwards compatibility */
  55. uint8_t struct_version;
  56. /* Unique ID to detect space redefinition */
  57. uint32_t uid;
  58. /* Kernel versions */
  59. uint32_t kernel_versions;
  60. /* Reserved for future expansion */
  61. uint8_t reserved[3];
  62. /* CRC; must be last field in struct */
  63. uint8_t crc8;
  64. } __attribute__((packed));
  65. /* Which param to get/set for vb2_secdatak_get() / vb2_secdatak_set() */
  66. enum vb2_secdatak_param {
  67. /* Kernel versions */
  68. VB2_SECDATAK_VERSIONS = 0,
  69. };
  70. /*****************************************************************************/
  71. /* Firmware version space functions */
  72. /**
  73. * Check the CRC of the secure storage context.
  74. *
  75. * Use this if reading from secure storage may be flaky, and you want to retry
  76. * reading it several times.
  77. *
  78. * This may be called before vb2_context_init().
  79. *
  80. * @param ctx Context pointer
  81. * @return VB2_SUCCESS, or non-zero error code if error.
  82. */
  83. int vb2_secdata_check_crc(const struct vb2_context *ctx);
  84. /**
  85. * Create fresh data in the secure storage context.
  86. *
  87. * Use this only when initializing the secure storage context on a new machine
  88. * the first time it boots. Do NOT simply use this if vb2_secdata_check_crc()
  89. * (or any other API in this library) fails; that could allow the secure data
  90. * to be rolled back to an insecure state.
  91. *
  92. * This may be called before vb2_context_init().
  93. */
  94. int vb2_secdata_create(struct vb2_context *ctx);
  95. /**
  96. * Initialize the secure storage context and verify its CRC.
  97. *
  98. * This must be called before vb2_secdata_get() or vb2_secdata_set().
  99. *
  100. * @param ctx Context pointer
  101. * @return VB2_SUCCESS, or non-zero error code if error.
  102. */
  103. int vb2_secdata_init(struct vb2_context *ctx);
  104. /**
  105. * Read a secure storage value.
  106. *
  107. * @param ctx Context pointer
  108. * @param param Parameter to read
  109. * @param dest Destination for value
  110. * @return VB2_SUCCESS, or non-zero error code if error.
  111. */
  112. int vb2_secdata_get(struct vb2_context *ctx,
  113. enum vb2_secdata_param param,
  114. uint32_t *dest);
  115. /**
  116. * Write a secure storage value.
  117. *
  118. * @param ctx Context pointer
  119. * @param param Parameter to write
  120. * @param value New value
  121. * @return VB2_SUCCESS, or non-zero error code if error.
  122. */
  123. int vb2_secdata_set(struct vb2_context *ctx,
  124. enum vb2_secdata_param param,
  125. uint32_t value);
  126. /*****************************************************************************/
  127. /* Kernel version space functions.
  128. *
  129. * These are separate functions so that they don't bloat the size of the early
  130. * boot code which uses the firmware version space functions.
  131. */
  132. /**
  133. * Check the CRC of the kernel version secure storage context.
  134. *
  135. * Use this if reading from secure storage may be flaky, and you want to retry
  136. * reading it several times.
  137. *
  138. * This may be called before vb2_context_init().
  139. *
  140. * @param ctx Context pointer
  141. * @return VB2_SUCCESS, or non-zero error code if error.
  142. */
  143. int vb2_secdatak_check_crc(const struct vb2_context *ctx);
  144. /**
  145. * Create fresh data in the secure storage context.
  146. *
  147. * Use this only when initializing the secure storage context on a new machine
  148. * the first time it boots. Do NOT simply use this if vb2_secdatak_check_crc()
  149. * (or any other API in this library) fails; that could allow the secure data
  150. * to be rolled back to an insecure state.
  151. *
  152. * This may be called before vb2_context_init().
  153. */
  154. int vb2_secdatak_create(struct vb2_context *ctx);
  155. /**
  156. * Initialize the secure storage context and verify its CRC.
  157. *
  158. * This must be called before vb2_secdatak_get() or vb2_secdatak_set().
  159. *
  160. * @param ctx Context pointer
  161. * @return VB2_SUCCESS, or non-zero error code if error.
  162. */
  163. int vb2_secdatak_init(struct vb2_context *ctx);
  164. /**
  165. * Read a secure storage value.
  166. *
  167. * @param ctx Context pointer
  168. * @param param Parameter to read
  169. * @param dest Destination for value
  170. * @return VB2_SUCCESS, or non-zero error code if error.
  171. */
  172. int vb2_secdatak_get(struct vb2_context *ctx,
  173. enum vb2_secdatak_param param,
  174. uint32_t *dest);
  175. /**
  176. * Write a secure storage value.
  177. *
  178. * @param ctx Context pointer
  179. * @param param Parameter to write
  180. * @param value New value
  181. * @return VB2_SUCCESS, or non-zero error code if error.
  182. */
  183. int vb2_secdatak_set(struct vb2_context *ctx,
  184. enum vb2_secdatak_param param,
  185. uint32_t value);
  186. #endif /* VBOOT_REFERENCE_VBOOT_2SECDATA_H_ */