bdb_api.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_FIRMWARE_BDB_BDB_API_H
  6. #define VBOOT_REFERENCE_FIRMWARE_BDB_BDB_API_H
  7. #include <stdint.h>
  8. #include "vboot_register.h"
  9. #include "nvm.h"
  10. #include "secrets.h"
  11. #include "bdb_flag.h"
  12. struct vba_context {
  13. /* Indicate which slot is being tried: 0 - primary, 1 - secondary */
  14. uint8_t slot;
  15. /* Defined by VBA_CONTEXT_FLAG_* in bdb_flag.h */
  16. uint32_t flags;
  17. /* BDB */
  18. uint8_t *bdb;
  19. /* Secrets */
  20. struct bdb_secrets *secrets;
  21. /* NVM-RW buffer */
  22. struct nvmrw nvmrw;
  23. };
  24. /**
  25. * Initialize vboot process
  26. *
  27. * @param ctx
  28. * @return enum bdb_return_code
  29. */
  30. int vba_bdb_init(struct vba_context *ctx);
  31. /**
  32. * Finalize vboot process
  33. *
  34. * @param ctx
  35. * @return enum bdb_return_code
  36. */
  37. int vba_bdb_finalize(struct vba_context *ctx);
  38. /**
  39. * Log failed boot attempt and reset the chip
  40. *
  41. * @param ctx
  42. */
  43. void vba_bdb_fail(struct vba_context *ctx);
  44. /**
  45. * Update kernel and its data key version in NVM
  46. *
  47. * This is the function called from SP-RW, which receives a kernel version
  48. * from an AP-RW after successful verification of a kernel.
  49. *
  50. * It checks whether the version in NVM-RW is older than the reported version
  51. * or not. If so, it updates the version in NVM-RW.
  52. *
  53. * @param ctx
  54. * @param kernel_data_key_version
  55. * @param kernel_version
  56. * @return BDB_SUCCESS or BDB_ERROR_*
  57. */
  58. int vba_update_kernel_version(struct vba_context *ctx,
  59. uint32_t kernel_data_key_version,
  60. uint32_t kernel_version);
  61. /**
  62. * Write new boot unlock code to NVM-RW
  63. *
  64. * @param ctx
  65. * @param new_buc New BUC to be written
  66. * @return BDB_SUCCESS or BDB_ERROR_*
  67. */
  68. int vba_update_buc(struct vba_context *ctx, uint8_t *new_buc);
  69. /**
  70. * Derive a secret
  71. *
  72. * This derives a new secret from a secret passed from SP-RO.
  73. *
  74. * @param ctx
  75. * @param type Type of secret to derive
  76. * @param buf Buffer containing data to derive secret from
  77. * @param buf_size Size of <buf>
  78. * @return BDB_SUCCESS or BDB_ERROR_*
  79. */
  80. int vba_derive_secret(struct vba_context *ctx, enum bdb_secret_type type,
  81. uint8_t *wsr, const uint8_t *buf, uint32_t buf_size);
  82. /**
  83. * Clear a secret
  84. *
  85. * @param ctx
  86. * @param type Type of secret to clear
  87. * @return BDB_SUCCESS or BDB_ERROR_*
  88. */
  89. int vba_clear_secret(struct vba_context *ctx, enum bdb_secret_type type);
  90. /**
  91. * Extend secrets for SP-RO
  92. *
  93. * @param ctx struct vba_context
  94. * @param bdb BDB
  95. * @param wsr Pointer to working secret register contents
  96. * @param extend Function to be called for extending a secret
  97. * @return BDB_SUCCESS or BDB_ERROR_*
  98. */
  99. typedef void (*f_extend)(const uint8_t *from, const uint8_t *by, uint8_t *to);
  100. int vba_extend_secrets_ro(struct vba_context *ctx, const uint8_t *bdb,
  101. uint8_t *wsr, f_extend extend);
  102. /**
  103. * Get vboot register value
  104. *
  105. * Implemented by each chip
  106. *
  107. * @param type Type of register to get
  108. * @return Register value
  109. */
  110. uint32_t vbe_get_vboot_register(enum vboot_register type);
  111. /**
  112. * Set vboot register value
  113. *
  114. * Implemented by each chip
  115. *
  116. * @param type Type of register to set
  117. * @param val Value to set
  118. */
  119. void vbe_set_vboot_register(enum vboot_register type, uint32_t val);
  120. /**
  121. * Reset the SoC
  122. *
  123. * Implemented by each chip. This is different from reboot (a.k.a. board reset,
  124. * cold reset).
  125. */
  126. void vbe_reset(void);
  127. /**
  128. * Read contents from Non-Volatile Memory
  129. *
  130. * Implemented by each chip.
  131. *
  132. * @param type Type of NVM
  133. * @param buf Buffer where the data will be read to
  134. * @param size Size of data to read
  135. * @return Zero if success or non-zero otherwise
  136. */
  137. int vbe_read_nvm(enum nvm_type type, uint8_t *buf, uint32_t size);
  138. /**
  139. * Write contents to Non-Volatile Memory
  140. *
  141. * Implemented by each chip.
  142. *
  143. * @param type Type of NVM
  144. * @param buf Buffer where the data will be written from
  145. * @param size Size of data to write
  146. * @return Zero if success or non-zero otherwise
  147. */
  148. int vbe_write_nvm(enum nvm_type type, void *buf, uint32_t size);
  149. /**
  150. * Encrypt data by AES-256
  151. *
  152. * @param msg Message to be encrypted
  153. * @param len Length of <msg> in bytes
  154. * @param key Key used for encryption
  155. * @param out Buffer where encrypted message is stored
  156. * @return BDB_SUCCESS or BDB_ERROR_*
  157. */
  158. int vbe_aes256_encrypt(const uint8_t *msg, uint32_t len, const uint8_t *key,
  159. uint8_t *out);
  160. /**
  161. * Decrypt data by AES-256
  162. *
  163. * @param msg Message to be decrypted
  164. * @param len Length of <msg> in bytes
  165. * @param key Key used for decryption
  166. * @param out Buffer where decrypted message is stored
  167. * @return BDB_SUCCESS or BDB_ERROR_*
  168. */
  169. int vbe_aes256_decrypt(const uint8_t *msg, uint32_t len, const uint8_t *key,
  170. uint8_t *out);
  171. #endif