avb_ops.h 13 KB

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