avb_ops.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* SPDX-License-Identifier: MIT */
  2. /*
  3. * Copyright (C) 2016 The Android Open Source Project
  4. */
  5. #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
  6. #error "Never include this file directly, include libavb.h instead."
  7. #endif
  8. #ifndef AVB_OPS_H_
  9. #define AVB_OPS_H_
  10. #include "avb_sysdeps.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /* Well-known names of named persistent values. */
  15. #define AVB_NPV_PERSISTENT_DIGEST_PREFIX "avb.persistent_digest."
  16. /* Return codes used for I/O operations.
  17. *
  18. * AVB_IO_RESULT_OK is returned if the requested operation was
  19. * successful.
  20. *
  21. * AVB_IO_RESULT_ERROR_IO is returned if the underlying hardware (disk
  22. * or other subsystem) encountered an I/O error.
  23. *
  24. * AVB_IO_RESULT_ERROR_OOM is returned if unable to allocate memory.
  25. *
  26. * AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION is returned if the requested
  27. * partition does not exist.
  28. *
  29. * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION is returned if the
  30. * range of bytes requested to be read or written is outside the range
  31. * of the partition.
  32. *
  33. * AVB_IO_RESULT_ERROR_NO_SUCH_VALUE is returned if a named persistent value
  34. * does not exist.
  35. *
  36. * AVB_IO_RESULT_ERROR_INVALID_VALUE_SIZE is returned if a named persistent
  37. * value size is not supported or does not match the expected size.
  38. *
  39. * AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE is returned if a buffer is too small
  40. * for the requested operation.
  41. */
  42. typedef enum {
  43. AVB_IO_RESULT_OK,
  44. AVB_IO_RESULT_ERROR_OOM,
  45. AVB_IO_RESULT_ERROR_IO,
  46. AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION,
  47. AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION,
  48. AVB_IO_RESULT_ERROR_NO_SUCH_VALUE,
  49. AVB_IO_RESULT_ERROR_INVALID_VALUE_SIZE,
  50. AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE,
  51. } AvbIOResult;
  52. struct AvbOps;
  53. typedef struct AvbOps AvbOps;
  54. /* Forward-declaration of operations in libavb_ab. */
  55. struct AvbABOps;
  56. /* Forward-declaration of operations in libavb_atx. */
  57. struct AvbAtxOps;
  58. /* High-level operations/functions/methods that are platform
  59. * dependent.
  60. *
  61. * Operations may be added in the future so when implementing it
  62. * always make sure to zero out sizeof(AvbOps) bytes of the struct to
  63. * ensure that unimplemented operations are set to NULL.
  64. */
  65. struct AvbOps {
  66. /* This pointer can be used by the application/bootloader using
  67. * libavb and is typically used in each operation to get a pointer
  68. * to platform-specific resources. It cannot be used by libraries.
  69. */
  70. void* user_data;
  71. /* If libavb_ab is used, this should point to the
  72. * AvbABOps. Otherwise it must be set to NULL.
  73. */
  74. struct AvbABOps* ab_ops;
  75. /* If libavb_atx is used, this should point to the
  76. * AvbAtxOps. Otherwise it must be set to NULL.
  77. */
  78. struct AvbAtxOps* atx_ops;
  79. /* Reads |num_bytes| from offset |offset| from partition with name
  80. * |partition| (NUL-terminated UTF-8 string). If |offset| is
  81. * negative, its absolute value should be interpreted as the number
  82. * of bytes from the end of the partition.
  83. *
  84. * This function returns AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION if
  85. * there is no partition with the given name,
  86. * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION if the requested
  87. * |offset| is outside the partition, and AVB_IO_RESULT_ERROR_IO if
  88. * there was an I/O error from the underlying I/O subsystem. If the
  89. * operation succeeds as requested AVB_IO_RESULT_OK is returned and
  90. * the data is available in |buffer|.
  91. *
  92. * The only time partial I/O may occur is if reading beyond the end
  93. * of the partition. In this case the value returned in
  94. * |out_num_read| may be smaller than |num_bytes|.
  95. */
  96. AvbIOResult (*read_from_partition)(AvbOps* ops,
  97. const char* partition,
  98. int64_t offset,
  99. size_t num_bytes,
  100. void* buffer,
  101. size_t* out_num_read);
  102. /* Gets the starting pointer of a partition that is pre-loaded in memory, and
  103. * save it to |out_pointer|. The preloaded partition is expected to be
  104. * |num_bytes|, where the actual preloaded byte count is returned in
  105. * |out_num_bytes_preloaded|. |out_num_bytes_preloaded| must be no larger than
  106. * |num_bytes|.
  107. *
  108. * This provides an alternative way to access a partition that is preloaded
  109. * into memory without a full memory copy. When this function pointer is not
  110. * set (has value NULL), or when the |out_pointer| is set to NULL as a result,
  111. * |read_from_partition| will be used as the fallback. This function is mainly
  112. * used for accessing the entire partition content to calculate its hash.
  113. *
  114. * Preloaded partition data must outlive the lifespan of the
  115. * |AvbSlotVerifyData| structure that |avb_slot_verify| outputs.
  116. */
  117. AvbIOResult (*get_preloaded_partition)(AvbOps* ops,
  118. const char* partition,
  119. size_t num_bytes,
  120. uint8_t** out_pointer,
  121. size_t* out_num_bytes_preloaded);
  122. /* Writes |num_bytes| from |bffer| at offset |offset| to partition
  123. * with name |partition| (NUL-terminated UTF-8 string). If |offset|
  124. * is negative, its absolute value should be interpreted as the
  125. * number of bytes from the end of the partition.
  126. *
  127. * This function returns AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION if
  128. * there is no partition with the given name,
  129. * AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION if the requested
  130. * byterange goes outside the partition, and AVB_IO_RESULT_ERROR_IO
  131. * if there was an I/O error from the underlying I/O subsystem. If
  132. * the operation succeeds as requested AVB_IO_RESULT_OK is
  133. * returned.
  134. *
  135. * This function never does any partial I/O, it either transfers all
  136. * of the requested bytes or returns an error.
  137. */
  138. AvbIOResult (*write_to_partition)(AvbOps* ops,
  139. const char* partition,
  140. int64_t offset,
  141. size_t num_bytes,
  142. const void* buffer);
  143. /* Checks if the given public key used to sign the 'vbmeta'
  144. * partition is trusted. Boot loaders typically compare this with
  145. * embedded key material generated with 'avbtool
  146. * extract_public_key'.
  147. *
  148. * The public key is in the array pointed to by |public_key_data|
  149. * and is of |public_key_length| bytes.
  150. *
  151. * If there is no public key metadata (set with the avbtool option
  152. * --public_key_metadata) then |public_key_metadata| will be set to
  153. * NULL. Otherwise this field points to the data which is
  154. * |public_key_metadata_length| bytes long.
  155. *
  156. * If AVB_IO_RESULT_OK is returned then |out_is_trusted| is set -
  157. * true if trusted or false if untrusted.
  158. */
  159. AvbIOResult (*validate_vbmeta_public_key)(AvbOps* ops,
  160. const uint8_t* public_key_data,
  161. size_t public_key_length,
  162. const uint8_t* public_key_metadata,
  163. size_t public_key_metadata_length,
  164. bool* out_is_trusted);
  165. /* Gets the rollback index corresponding to the location given by
  166. * |rollback_index_location|. The value is returned in
  167. * |out_rollback_index|. Returns AVB_IO_RESULT_OK if the rollback
  168. * index was retrieved, otherwise an error code.
  169. *
  170. * A device may have a limited amount of rollback index locations (say,
  171. * one or four) so may error out if |rollback_index_location| exceeds
  172. * this number.
  173. */
  174. AvbIOResult (*read_rollback_index)(AvbOps* ops,
  175. size_t rollback_index_location,
  176. uint64_t* out_rollback_index);
  177. /* Sets the rollback index corresponding to the location given by
  178. * |rollback_index_location| to |rollback_index|. Returns
  179. * AVB_IO_RESULT_OK if the rollback index was set, otherwise an
  180. * error code.
  181. *
  182. * A device may have a limited amount of rollback index locations (say,
  183. * one or four) so may error out if |rollback_index_location| exceeds
  184. * this number.
  185. */
  186. AvbIOResult (*write_rollback_index)(AvbOps* ops,
  187. size_t rollback_index_location,
  188. uint64_t rollback_index);
  189. /* Gets whether the device is unlocked. The value is returned in
  190. * |out_is_unlocked| (true if unlocked, false otherwise). Returns
  191. * AVB_IO_RESULT_OK if the state was retrieved, otherwise an error
  192. * code.
  193. */
  194. AvbIOResult (*read_is_device_unlocked)(AvbOps* ops, bool* out_is_unlocked);
  195. /* Gets the unique partition GUID for a partition with name in
  196. * |partition| (NUL-terminated UTF-8 string). The GUID is copied as
  197. * a string into |guid_buf| of size |guid_buf_size| and will be NUL
  198. * terminated. The string must be lower-case and properly
  199. * hyphenated. For example:
  200. *
  201. * 527c1c6d-6361-4593-8842-3c78fcd39219
  202. *
  203. * Returns AVB_IO_RESULT_OK on success, otherwise an error code.
  204. */
  205. AvbIOResult (*get_unique_guid_for_partition)(AvbOps* ops,
  206. const char* partition,
  207. char* guid_buf,
  208. size_t guid_buf_size);
  209. /* Gets the size of a partition with the name in |partition|
  210. * (NUL-terminated UTF-8 string). Returns the value in
  211. * |out_size_num_bytes|.
  212. *
  213. * Returns AVB_IO_RESULT_OK on success, otherwise an error code.
  214. */
  215. AvbIOResult (*get_size_of_partition)(AvbOps* ops,
  216. const char* partition,
  217. uint64_t* out_size_num_bytes);
  218. /* Reads a persistent value corresponding to the given |name|. The value is
  219. * returned in |out_buffer| which must point to |buffer_size| bytes. On
  220. * success |out_num_bytes_read| contains the number of bytes read into
  221. * |out_buffer|. If AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE is returned,
  222. * |out_num_bytes_read| contains the number of bytes that would have been read
  223. * which can be used to allocate a buffer.
  224. *
  225. * The |buffer_size| may be zero and the |out_buffer| may be NULL, but if
  226. * |out_buffer| is NULL then |buffer_size| *must* be zero.
  227. *
  228. * Returns AVB_IO_RESULT_OK on success, otherwise an error code.
  229. *
  230. * If the value does not exist, is not supported, or is not populated, returns
  231. * AVB_IO_RESULT_ERROR_NO_SUCH_VALUE. If |buffer_size| is smaller than the
  232. * size of the stored value, returns AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE.
  233. *
  234. * This operation is currently only used to support persistent digests. If a
  235. * device does not use persistent digests this function pointer can be set to
  236. * NULL.
  237. */
  238. AvbIOResult (*read_persistent_value)(AvbOps* ops,
  239. const char* name,
  240. size_t buffer_size,
  241. uint8_t* out_buffer,
  242. size_t* out_num_bytes_read);
  243. /* Writes a persistent value corresponding to the given |name|. The value is
  244. * supplied in |value| which must point to |value_size| bytes. Any existing
  245. * value with the same name is overwritten. If |value_size| is zero, future
  246. * calls to |read_persistent_value| will return
  247. * AVB_IO_RESULT_ERROR_NO_SUCH_VALUE.
  248. *
  249. * Returns AVB_IO_RESULT_OK on success, otherwise an error code.
  250. *
  251. * If the value |name| is not supported, returns
  252. * AVB_IO_RESULT_ERROR_NO_SUCH_VALUE. If the |value_size| is not supported,
  253. * returns AVB_IO_RESULT_ERROR_INVALID_VALUE_SIZE.
  254. *
  255. * This operation is currently only used to support persistent digests. If a
  256. * device does not use persistent digests this function pointer can be set to
  257. * NULL.
  258. */
  259. AvbIOResult (*write_persistent_value)(AvbOps* ops,
  260. const char* name,
  261. size_t value_size,
  262. const uint8_t* value);
  263. };
  264. #ifdef __cplusplus
  265. }
  266. #endif
  267. #endif /* AVB_OPS_H_ */