avb_ab_flow.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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_AB_H) && !defined(AVB_COMPILATION)
  25. #error \
  26. "Never include this file directly, include libavb_ab/libavb_ab.h instead."
  27. #endif
  28. #ifndef AVB_AB_FLOW_H_
  29. #define AVB_AB_FLOW_H_
  30. #include "avb_ab_ops.h"
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /* Magic for the A/B struct when serialized. */
  35. #define AVB_AB_MAGIC "\0AB0"
  36. #define AVB_AB_MAGIC_LEN 4
  37. /* Versioning for the on-disk A/B metadata - keep in sync with avbtool. */
  38. #define AVB_AB_MAJOR_VERSION 1
  39. #define AVB_AB_MINOR_VERSION 0
  40. /* Size of AvbABData struct. */
  41. #define AVB_AB_DATA_SIZE 32
  42. /* Maximum values for slot data */
  43. #define AVB_AB_MAX_PRIORITY 15
  44. #define AVB_AB_MAX_TRIES_REMAINING 7
  45. /* Struct used for recording per-slot metadata.
  46. *
  47. * When serialized, data is stored in network byte-order.
  48. */
  49. typedef struct AvbABSlotData {
  50. /* Slot priority. Valid values range from 0 to AVB_AB_MAX_PRIORITY,
  51. * both inclusive with 1 being the lowest and AVB_AB_MAX_PRIORITY
  52. * being the highest. The special value 0 is used to indicate the
  53. * slot is unbootable.
  54. */
  55. uint8_t priority;
  56. /* Number of times left attempting to boot this slot ranging from 0
  57. * to AVB_AB_MAX_TRIES_REMAINING.
  58. */
  59. uint8_t tries_remaining;
  60. /* Non-zero if this slot has booted successfully, 0 otherwise. */
  61. uint8_t successful_boot;
  62. /* Reserved for future use. */
  63. uint8_t reserved[1];
  64. } AVB_ATTR_PACKED AvbABSlotData;
  65. /* Struct used for recording A/B metadata.
  66. *
  67. * When serialized, data is stored in network byte-order.
  68. */
  69. typedef struct AvbABData {
  70. /* Magic number used for identification - see AVB_AB_MAGIC. */
  71. uint8_t magic[AVB_AB_MAGIC_LEN];
  72. /* Version of on-disk struct - see AVB_AB_{MAJOR,MINOR}_VERSION. */
  73. uint8_t version_major;
  74. uint8_t version_minor;
  75. /* Padding to ensure |slots| field start eight bytes in. */
  76. uint8_t reserved1[2];
  77. /* Per-slot metadata. */
  78. AvbABSlotData slots[2];
  79. /* Reserved for future use. */
  80. uint8_t reserved2[12];
  81. /* CRC32 of all 28 bytes preceding this field. */
  82. uint32_t crc32;
  83. } AVB_ATTR_PACKED AvbABData;
  84. /* Copies |src| to |dest|, byte-swapping fields in the
  85. * process. Returns false if the data is invalid (e.g. wrong magic,
  86. * wrong CRC32 etc.), true otherwise.
  87. */
  88. bool avb_ab_data_verify_and_byteswap(const AvbABData* src, AvbABData* dest);
  89. /* Copies |src| to |dest|, byte-swapping fields in the process. Also
  90. * updates the |crc32| field in |dest|.
  91. */
  92. void avb_ab_data_update_crc_and_byteswap(const AvbABData* src, AvbABData* dest);
  93. /* Initializes |data| such that it has two slots and both slots have
  94. * maximum tries remaining. The CRC is not set.
  95. */
  96. void avb_ab_data_init(AvbABData* data);
  97. /* Reads A/B metadata from the 'misc' partition using |ops|. Returned
  98. * data is properly byteswapped. Returns AVB_IO_RESULT_OK on
  99. * success, error code otherwise.
  100. *
  101. * If the data read from disk is invalid (e.g. wrong magic or CRC
  102. * checksum failure), the metadata will be reset using
  103. * avb_ab_data_init() and then written to disk.
  104. */
  105. AvbIOResult avb_ab_data_read(AvbABOps* ab_ops, AvbABData* data);
  106. /* Writes A/B metadata to the 'misc' partition using |ops|. This will
  107. * byteswap and update the CRC as needed. Returns AVB_IO_RESULT_OK on
  108. * success, error code otherwise.
  109. */
  110. AvbIOResult avb_ab_data_write(AvbABOps* ab_ops, const AvbABData* data);
  111. /* Return codes used in avb_ab_flow(), see that function for
  112. * documentation of each value.
  113. */
  114. typedef enum {
  115. AVB_AB_FLOW_RESULT_OK,
  116. AVB_AB_FLOW_RESULT_OK_WITH_VERIFICATION_ERROR,
  117. AVB_AB_FLOW_RESULT_ERROR_OOM,
  118. AVB_AB_FLOW_RESULT_ERROR_IO,
  119. AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS,
  120. AVB_AB_FLOW_RESULT_ERROR_INVALID_ARGUMENT
  121. } AvbABFlowResult;
  122. /* Get a textual representation of |result|. */
  123. const char* avb_ab_flow_result_to_string(AvbABFlowResult result);
  124. /* High-level function to select a slot to boot. The following
  125. * algorithm is used:
  126. *
  127. * 1. A/B metadata is loaded and validated using the
  128. * read_ab_metadata() operation. Typically this means it's read from
  129. * the 'misc' partition and if it's invalid then it's reset using
  130. * avb_ab_data_init() and this reset metadata is returned.
  131. *
  132. * 2. All bootable slots listed in the A/B metadata are verified using
  133. * avb_slot_verify(). If a slot is invalid or if it fails verification
  134. * (and AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is not set, see
  135. * below), it will be marked as unbootable in the A/B metadata and the
  136. * metadata will be saved to disk before returning.
  137. *
  138. * 3. If there are no bootable slots, the value
  139. * AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS is returned.
  140. *
  141. * 4. For each bootable slot, the Stored Rollback Indexes are updated
  142. * such that for each rollback index location, the Stored Rollback
  143. * Index is the largest number smaller than or equal to the Rollback
  144. * Index of each slot.
  145. *
  146. * 5. The bootable slot with the highest priority is selected and
  147. * returned in |out_data|. If this slot is already marked as
  148. * successful, the A/B metadata is not modified. However, if the slot
  149. * is not marked as bootable its |tries_remaining| count is
  150. * decremented and the A/B metadata is saved to disk before returning.
  151. * In either case the value AVB_AB_FLOW_RESULT_OK is returning.
  152. *
  153. * The partitions to load is given in |requested_partitions| as a
  154. * NULL-terminated array of NUL-terminated strings. Typically the
  155. * |requested_partitions| array only contains a single item for the
  156. * boot partition, 'boot'.
  157. *
  158. * If the device is unlocked (and _only_ if it's unlocked), the
  159. * AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR flag should be set
  160. * in the |flags| parameter. This will allow considering slots as
  161. * verified even when avb_slot_verify() returns
  162. * AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED,
  163. * AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, or
  164. * AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX for the slot in
  165. * question.
  166. *
  167. * Note that neither androidboot.slot_suffix nor androidboot.slot are
  168. * set in the |cmdline| field in |AvbSlotVerifyData| - you will have
  169. * to pass these yourself.
  170. *
  171. * If a slot was selected and it verified then AVB_AB_FLOW_RESULT_OK
  172. * is returned.
  173. *
  174. * If a slot was selected but it didn't verify then
  175. * AVB_AB_FLOW_RESULT_OK_WITH_VERIFICATION_ERROR is returned. This can
  176. * only happen when the AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR
  177. * flag is set.
  178. *
  179. * If an I/O operation - such as loading/saving metadata or checking
  180. * rollback indexes - fail, the value AVB_AB_FLOW_RESULT_ERROR_IO is
  181. * returned.
  182. *
  183. * If memory allocation fails, AVB_AB_FLOW_RESULT_ERROR_OOM is
  184. * returned.
  185. *
  186. * If invalid arguments are passed,
  187. * AVB_AB_FLOW_RESULT_ERROR_INVALID_ARGUMENT is returned. For example
  188. * this can happen if using AVB_HASHTREE_ERROR_MODE_LOGGING without
  189. * AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR.
  190. *
  191. * Reasonable behavior for handling AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS
  192. * is to initiate device repair (which is device-dependent).
  193. */
  194. AvbABFlowResult avb_ab_flow(AvbABOps* ab_ops,
  195. const char* const* requested_partitions,
  196. AvbSlotVerifyFlags flags,
  197. AvbHashtreeErrorMode hashtree_error_mode,
  198. AvbSlotVerifyData** out_data);
  199. /* Marks the slot with the given slot number as active. Returns
  200. * AVB_IO_RESULT_OK on success, error code otherwise.
  201. *
  202. * This function is typically used by the OS updater when completing
  203. * an update. It can also used by the firmware for implementing the
  204. * "set_active" command.
  205. */
  206. AvbIOResult avb_ab_mark_slot_active(AvbABOps* ab_ops, unsigned int slot_number);
  207. /* Marks the slot with the given slot number as unbootable. Returns
  208. * AVB_IO_RESULT_OK on success, error code otherwise.
  209. *
  210. * This function is typically used by the OS updater before writing to
  211. * a slot.
  212. */
  213. AvbIOResult avb_ab_mark_slot_unbootable(AvbABOps* ab_ops,
  214. unsigned int slot_number);
  215. /* Marks the slot with the given slot number as having booted
  216. * successfully. Returns AVB_IO_RESULT_OK on success, error code
  217. * otherwise.
  218. *
  219. * Calling this on an unbootable slot is an error - AVB_IO_RESULT_OK
  220. * will be returned yet the function will have no side-effects.
  221. *
  222. * This function is typically used by the OS updater after having
  223. * confirmed that the slot works as intended.
  224. */
  225. AvbIOResult avb_ab_mark_slot_successful(AvbABOps* ab_ops,
  226. unsigned int slot_number);
  227. #ifdef __cplusplus
  228. }
  229. #endif
  230. #endif /* AVB_AB_FLOW_H_ */