nvm.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Copyright 2016 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. #ifndef VBOOT_REFERENCE_BDB_NVM_H_
  6. #define VBOOT_REFERENCE_BDB_NVM_H_
  7. #include <stdint.h>
  8. #include "bdb_struct.h"
  9. #include "bdb_api.h"
  10. enum nvm_type {
  11. NVM_TYPE_WP_PRIMARY,
  12. NVM_TYPE_WP_SECONDARY,
  13. NVM_TYPE_RW_PRIMARY,
  14. NVM_TYPE_RW_SECONDARY,
  15. };
  16. #define NVM_RW_MAGIC 0x3052766e
  17. /* Size in bytes of encrypted BUC (Boot Unlock Code) */
  18. #define BUC_ENC_DIGEST_SIZE 32
  19. /* Size in bytes of HMAC of struct NVM-RW */
  20. #define NVM_HMAC_SIZE BDB_SHA256_DIGEST_SIZE
  21. #define NVM_RW_FLAG_BUC_PRESENT (1 << 0)
  22. #define NVM_RW_FLAG_DFM_DISABLE (1 << 1)
  23. #define NVM_RW_FLAG_DOSM (1 << 2)
  24. /* This is the minimum size of the data needed to learn the actual size */
  25. #define NVM_MIN_STRUCT_SIZE 8
  26. #define NVM_HEADER_VERSION_MAJOR 1
  27. #define NVM_HEADER_VERSION_MINOR 1
  28. /* Maximum number of retries for writing NVM */
  29. #define NVM_MAX_WRITE_RETRY 2
  30. struct nvmrw {
  31. /* Magic number to identify struct */
  32. uint32_t struct_magic;
  33. /* Structure version */
  34. uint8_t struct_major_version;
  35. uint8_t struct_minor_version;
  36. /* Size of struct in bytes. 96 for version 1.0 */
  37. uint16_t struct_size;
  38. /* Number of updates to structure contents */
  39. uint32_t update_count;
  40. /* Flags: NVM_RW_FLAG_* */
  41. uint32_t flags;
  42. /* Minimum valid kernel data key version */
  43. uint32_t min_kernel_data_key_version;
  44. /* Minimum valid kernel version */
  45. uint32_t min_kernel_version;
  46. /* Type of BUC */
  47. uint8_t buc_type;
  48. uint8_t reserved0[7];
  49. /* Encrypted BUC */
  50. uint8_t buc_enc_digest[BUC_ENC_DIGEST_SIZE];
  51. /* SHA-256 HMAC of the struct contents. Add new fields before this. */
  52. uint8_t hmac[NVM_HMAC_SIZE];
  53. } __attribute__((packed));
  54. /*
  55. * List of variables stored in NVM-RW. This should be exported and used by
  56. * firmware and futility to access data in NVM-RW.
  57. */
  58. enum nvmrw_var {
  59. NVMRW_VAR_UPDATE_COUNT,
  60. NVMRW_VAR_FLAGS,
  61. NVMRW_VAR_MIN_KERNEL_DATA_KEY_VERSION,
  62. NVMRW_VAR_MIN_KERNEL_VERSION,
  63. NVMRW_VAR_BUC_TYPE,
  64. NVMRW_VAR_FLAG_BUC_PRESENT,
  65. NVMRW_VAR_FLAG_DFM_DISABLE,
  66. NVMRW_VAR_FLAG_DOSM,
  67. };
  68. /* Size of the version 1.0 */
  69. #define NVM_RW_MIN_STRUCT_SIZE 96
  70. /* 4 Kbit EEPROM divided by 4 regions (RO,RW) x (1st,2nd) = 128 KB */
  71. #define NVM_RW_MAX_STRUCT_SIZE 128
  72. /* For nvm_rw_read and nvm_write */
  73. struct vba_context;
  74. /**
  75. * Read NVM-RW contents into the context
  76. *
  77. * @param ctx struct vba_context
  78. * @return BDB_SUCCESS or BDB_ERROR_NVM_*
  79. */
  80. int nvmrw_read(struct vba_context *ctx);
  81. /**
  82. * Write to NVM-RW from the context
  83. *
  84. * @param ctx struct vba_context
  85. * @param type NVM_TYPE_RW_*
  86. * @return BDB_SUCCESS or BDB_ERROR_NVM_*
  87. */
  88. int nvmrw_write(struct vba_context *ctx, enum nvm_type type);
  89. /**
  90. * Get a value of NVM-RW variable
  91. *
  92. * Callers are responsible for init and verify of ctx->nvmrw.
  93. *
  94. * @param ctx struct vba_context
  95. * @param var Index of the variable
  96. * @param val Destination where the value is stored
  97. * @return BDB_SUCCESS or BDB_ERROR_NVM_*
  98. */
  99. int nvmrw_get(struct vba_context *ctx, enum nvmrw_var var, uint32_t *val);
  100. /**
  101. * Set a value in NVM-RW variable
  102. *
  103. * Callers are responsible for init and verify of ctx->nvmrw.
  104. *
  105. * @param ctx struct vba_context
  106. * @param var Index of the variable
  107. * @param val Value to be set
  108. * @return BDB_SUCCESS or BDB_ERROR_NVM_*
  109. */
  110. int nvmrw_set(struct vba_context *ctx, enum nvmrw_var var, uint32_t val);
  111. #endif