xref: /rk3399_rockchip-uboot/include/android_avb/avb_slot_verify.h (revision 69fdc5969fe4d637d490fddd72ffd26ea0d356fe)
15b69db07SJason Zhu /*
25b69db07SJason Zhu  * Copyright (C) 2016 The Android Open Source Project
35b69db07SJason Zhu  *
45b69db07SJason Zhu  * Permission is hereby granted, free of charge, to any person
55b69db07SJason Zhu  * obtaining a copy of this software and associated documentation
65b69db07SJason Zhu  * files (the "Software"), to deal in the Software without
75b69db07SJason Zhu  * restriction, including without limitation the rights to use, copy,
85b69db07SJason Zhu  * modify, merge, publish, distribute, sublicense, and/or sell copies
95b69db07SJason Zhu  * of the Software, and to permit persons to whom the Software is
105b69db07SJason Zhu  * furnished to do so, subject to the following conditions:
115b69db07SJason Zhu  *
125b69db07SJason Zhu  * The above copyright notice and this permission notice shall be
135b69db07SJason Zhu  * included in all copies or substantial portions of the Software.
145b69db07SJason Zhu  *
155b69db07SJason Zhu  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
165b69db07SJason Zhu  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
175b69db07SJason Zhu  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
185b69db07SJason Zhu  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
195b69db07SJason Zhu  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
205b69db07SJason Zhu  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
215b69db07SJason Zhu  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
225b69db07SJason Zhu  * SOFTWARE.
235b69db07SJason Zhu  */
245b69db07SJason Zhu 
255b69db07SJason Zhu /*
265b69db07SJason Zhu #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
275b69db07SJason Zhu #error "Never include this file directly, include libavb.h instead."
285b69db07SJason Zhu #endif
295b69db07SJason Zhu */
305b69db07SJason Zhu 
315b69db07SJason Zhu #ifndef AVB_SLOT_VERIFY_H_
325b69db07SJason Zhu #define AVB_SLOT_VERIFY_H_
335b69db07SJason Zhu 
345b69db07SJason Zhu #include <android_avb/avb_ops.h>
355b69db07SJason Zhu #include <android_avb/avb_vbmeta_image.h>
365b69db07SJason Zhu 
375b69db07SJason Zhu #ifdef __cplusplus
385b69db07SJason Zhu extern "C" {
395b69db07SJason Zhu #endif
405b69db07SJason Zhu 
415b69db07SJason Zhu /* Return codes used in avb_slot_verify(), see that function for
425b69db07SJason Zhu  * documentation for each field.
435b69db07SJason Zhu  *
445b69db07SJason Zhu  * Use avb_slot_verify_result_to_string() to get a textual
455b69db07SJason Zhu  * representation usable for error/debug output.
465b69db07SJason Zhu  */
475b69db07SJason Zhu typedef enum {
485b69db07SJason Zhu   AVB_SLOT_VERIFY_RESULT_OK,
495b69db07SJason Zhu   AVB_SLOT_VERIFY_RESULT_ERROR_OOM,
505b69db07SJason Zhu   AVB_SLOT_VERIFY_RESULT_ERROR_IO,
515b69db07SJason Zhu   AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION,
525b69db07SJason Zhu   AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX,
535b69db07SJason Zhu   AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED,
545b69db07SJason Zhu   AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA,
555b69db07SJason Zhu   AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION,
565b69db07SJason Zhu   AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT
575b69db07SJason Zhu } AvbSlotVerifyResult;
585b69db07SJason Zhu 
595b69db07SJason Zhu /* Various error handling modes for when verification fails using a
605b69db07SJason Zhu  * hashtree at runtime inside the HLOS.
615b69db07SJason Zhu  *
625b69db07SJason Zhu  * AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE means that the OS
635b69db07SJason Zhu  * will invalidate the current slot and restart.
645b69db07SJason Zhu  *
655b69db07SJason Zhu  * AVB_HASHTREE_ERROR_MODE_RESTART means that the OS will restart.
665b69db07SJason Zhu  *
675b69db07SJason Zhu  * AVB_HASHTREE_ERROR_MODE_EIO means that an EIO error will be
685b69db07SJason Zhu  * returned to applications.
695b69db07SJason Zhu  *
705b69db07SJason Zhu  * AVB_HASHTREE_ERROR_MODE_LOGGING means that errors will be logged
715b69db07SJason Zhu  * and corrupt data may be returned to applications. This mode should
725b69db07SJason Zhu  * be used ONLY for diagnostics and debugging. It cannot be used
735b69db07SJason Zhu  * unless AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is also
745b69db07SJason Zhu  * used.
75*69fdc596SJason Zhu  *
76*69fdc596SJason Zhu  * AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO means that either
77*69fdc596SJason Zhu  * AVB_HASHTREE_ERROR_MODE_RESTART or AVB_HASHTREE_ERROR_MODE_EIO is used
78*69fdc596SJason Zhu  * depending on state. This mode implements a state machine whereby
79*69fdc596SJason Zhu  * AVB_HASHTREE_ERROR_MODE_RESTART is used by default and when
80*69fdc596SJason Zhu  * AVB_SLOT_VERIFY_FLAGS_RESTART_CAUSED_BY_HASHTREE_CORRUPTION is passed the
81*69fdc596SJason Zhu  * mode transitions to AVB_HASHTREE_ERROR_MODE_EIO. When a new OS has been
82*69fdc596SJason Zhu  * detected the device transitions back to the AVB_HASHTREE_ERROR_MODE_RESTART
83*69fdc596SJason Zhu  * mode. To do this persistent storage is needed - specifically this means that
84*69fdc596SJason Zhu  * the passed in AvbOps will need to have the read_persistent_value() and
85*69fdc596SJason Zhu  * write_persistent_value() operations implemented. The name of the persistent
86*69fdc596SJason Zhu  * value used is "avb.managed_verity_mode" and 32 bytes of storage is needed.
875b69db07SJason Zhu  */
885b69db07SJason Zhu typedef enum {
895b69db07SJason Zhu   AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE,
905b69db07SJason Zhu   AVB_HASHTREE_ERROR_MODE_RESTART,
915b69db07SJason Zhu   AVB_HASHTREE_ERROR_MODE_EIO,
92*69fdc596SJason Zhu   AVB_HASHTREE_ERROR_MODE_LOGGING,
93*69fdc596SJason Zhu   AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO
945b69db07SJason Zhu } AvbHashtreeErrorMode;
955b69db07SJason Zhu 
965b69db07SJason Zhu /* Flags that influence how avb_slot_verify() works.
975b69db07SJason Zhu  *
985b69db07SJason Zhu  * If AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is NOT set then
995b69db07SJason Zhu  * avb_slot_verify() will bail out as soon as an error is encountered
1005b69db07SJason Zhu  * and |out_data| is set only if AVB_SLOT_VERIFY_RESULT_OK is
1015b69db07SJason Zhu  * returned.
1025b69db07SJason Zhu  *
1035b69db07SJason Zhu  * Otherwise if AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is set
1045b69db07SJason Zhu  * avb_slot_verify() will continue verification efforts and |out_data|
1055b69db07SJason Zhu  * is also set if AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED,
1065b69db07SJason Zhu  * AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, or
1075b69db07SJason Zhu  * AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX is returned. It is
1085b69db07SJason Zhu  * undefined which error is returned if more than one distinct error
1095b69db07SJason Zhu  * is encountered. It is guaranteed that AVB_SLOT_VERIFY_RESULT_OK is
1105b69db07SJason Zhu  * returned if, and only if, there are no errors. This mode is needed
1115b69db07SJason Zhu  * to boot valid but unverified slots when the device is unlocked.
1125b69db07SJason Zhu  *
1135b69db07SJason Zhu  * Also, if AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is set the
1145b69db07SJason Zhu  * contents loaded from |requested_partition| will be the contents of
1155b69db07SJason Zhu  * the entire partition instead of just the size specified in the hash
1165b69db07SJason Zhu  * descriptor.
117*69fdc596SJason Zhu  *
118*69fdc596SJason Zhu  * The AVB_SLOT_VERIFY_FLAGS_RESTART_CAUSED_BY_HASHTREE_CORRUPTION flag
119*69fdc596SJason Zhu  * should be set if using AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO
120*69fdc596SJason Zhu  * and the reason the boot loader is running is because the device
121*69fdc596SJason Zhu  * was restarted by the dm-verity driver.
122*69fdc596SJason Zhu  *
123*69fdc596SJason Zhu  * If the AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION flag is set then
124*69fdc596SJason Zhu  * data won't be loaded from the "vbmeta" partition and the
125*69fdc596SJason Zhu  * |validate_vbmeta_public_key| operation is never called. Instead, the
126*69fdc596SJason Zhu  * vbmeta structs in |requested_partitions| are loaded and processed and the
127*69fdc596SJason Zhu  * |validate_public_key_for_partition| operation is called for each of these
128*69fdc596SJason Zhu  * vbmeta structs. This flag is useful when booting into recovery on a device
129*69fdc596SJason Zhu  * not using A/B - see section "Booting into recovery" in README.md for
130*69fdc596SJason Zhu  * more information.
1315b69db07SJason Zhu  */
1325b69db07SJason Zhu typedef enum {
1335b69db07SJason Zhu   AVB_SLOT_VERIFY_FLAGS_NONE = 0,
134*69fdc596SJason Zhu   AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR = (1 << 0),
135*69fdc596SJason Zhu   AVB_SLOT_VERIFY_FLAGS_RESTART_CAUSED_BY_HASHTREE_CORRUPTION = (1 << 1),
136*69fdc596SJason Zhu   AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION = (1 << 2),
1375b69db07SJason Zhu } AvbSlotVerifyFlags;
1385b69db07SJason Zhu 
1395b69db07SJason Zhu /* Get a textual representation of |result|. */
1405b69db07SJason Zhu const char* avb_slot_verify_result_to_string(AvbSlotVerifyResult result);
1415b69db07SJason Zhu 
1425b69db07SJason Zhu /* Maximum number of rollback index locations supported. */
1435b69db07SJason Zhu #define AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS 32
1445b69db07SJason Zhu 
1455b69db07SJason Zhu /* AvbPartitionData contains data loaded from partitions when using
1465b69db07SJason Zhu  * avb_slot_verify(). The |partition_name| field contains the name of
1475b69db07SJason Zhu  * the partition (without A/B suffix), |data| points to the loaded
14837a7bc39SJason Zhu  * data which is |data_size| bytes long. If |preloaded| is set to true,
14937a7bc39SJason Zhu  * this structure dose not own |data|. The caller of |avb_slot_verify|
15037a7bc39SJason Zhu  * needs to make sure that the preloaded data outlives this
15137a7bc39SJason Zhu  * |AvbPartitionData| structure.
1525b69db07SJason Zhu  *
1535b69db07SJason Zhu  * Note that this is strictly less than the partition size - it's only
1545b69db07SJason Zhu  * the image stored there, not the entire partition nor any of the
1555b69db07SJason Zhu  * metadata.
1565b69db07SJason Zhu  */
1575b69db07SJason Zhu typedef struct {
1585b69db07SJason Zhu   char* partition_name;
1595b69db07SJason Zhu   uint8_t* data;
1605b69db07SJason Zhu   size_t data_size;
16137a7bc39SJason Zhu   bool preloaded;
1625b69db07SJason Zhu } AvbPartitionData;
1635b69db07SJason Zhu 
1645b69db07SJason Zhu /* AvbVBMetaData contains a vbmeta struct loaded from a partition when
1655b69db07SJason Zhu  * using avb_slot_verify(). The |partition_name| field contains the
1665b69db07SJason Zhu  * name of the partition (without A/B suffix), |vbmeta_data| points to
1675b69db07SJason Zhu  * the loaded data which is |vbmeta_size| bytes long.
1685b69db07SJason Zhu  *
1695b69db07SJason Zhu  * The |verify_result| field contains the result of
1705b69db07SJason Zhu  * avb_vbmeta_image_verify() on the data. This is guaranteed to be
1715b69db07SJason Zhu  * AVB_VBMETA_VERIFY_RESULT_OK for all vbmeta images if
1725b69db07SJason Zhu  * avb_slot_verify() returns AVB_SLOT_VERIFY_RESULT_OK.
1735b69db07SJason Zhu  *
1745b69db07SJason Zhu  * You can use avb_descriptor_get_all(), avb_descriptor_foreach(), and
1755b69db07SJason Zhu  * avb_vbmeta_image_header_to_host_byte_order() with this data.
1765b69db07SJason Zhu  */
1775b69db07SJason Zhu typedef struct {
1785b69db07SJason Zhu   char* partition_name;
1795b69db07SJason Zhu   uint8_t* vbmeta_data;
1805b69db07SJason Zhu   size_t vbmeta_size;
1815b69db07SJason Zhu   AvbVBMetaVerifyResult verify_result;
1825b69db07SJason Zhu } AvbVBMetaData;
1835b69db07SJason Zhu 
1845b69db07SJason Zhu /* AvbSlotVerifyData contains data needed to boot a particular slot
1855b69db07SJason Zhu  * and is returned by avb_slot_verify() if partitions in a slot are
1865b69db07SJason Zhu  * successfully verified.
1875b69db07SJason Zhu  *
1885b69db07SJason Zhu  * All data pointed to by this struct - including data in each item in
1895b69db07SJason Zhu  * the |partitions| array - will be freed when the
1905b69db07SJason Zhu  * avb_slot_verify_data_free() function is called.
1915b69db07SJason Zhu  *
1925b69db07SJason Zhu  * The |ab_suffix| field is the copy of the of |ab_suffix| field
1935b69db07SJason Zhu  * passed to avb_slot_verify(). It is the A/B suffix of the slot. This
1945b69db07SJason Zhu  * value includes the leading underscore - typical values are "" (if
1955b69db07SJason Zhu  * no slots are in use), "_a" (for the first slot), and "_b" (for the
1965b69db07SJason Zhu  * second slot).
1975b69db07SJason Zhu  *
1985b69db07SJason Zhu  * The VBMeta images that were checked are available in the
1995b69db07SJason Zhu  * |vbmeta_images| field. The field |num_vbmeta_images| contains the
2005b69db07SJason Zhu  * number of elements in this array. The first element -
2015b69db07SJason Zhu  * vbmeta_images[0] - is guaranteed to be from the partition with the
2025b69db07SJason Zhu  * top-level vbmeta struct. This is usually the "vbmeta" partition in
2035b69db07SJason Zhu  * the requested slot but if there is no "vbmeta" partition it can
2045b69db07SJason Zhu  * also be the "boot" partition.
2055b69db07SJason Zhu  *
2065b69db07SJason Zhu  * The partitions loaded and verified from from the slot are
2075b69db07SJason Zhu  * accessible in the |loaded_partitions| array. The field
2085b69db07SJason Zhu  * |num_loaded_partitions| contains the number of elements in this
2095b69db07SJason Zhu  * array. The order of partitions in this array may not necessarily be
2105b69db07SJason Zhu  * the same order as in the passed-in |requested_partitions| array.
2115b69db07SJason Zhu  *
2125b69db07SJason Zhu  * Rollback indexes for the verified slot are stored in the
2135b69db07SJason Zhu  * |rollback_indexes| field. Note that avb_slot_verify() will NEVER
2145b69db07SJason Zhu  * modify stored_rollback_index[n] locations e.g. it will never use
2155b69db07SJason Zhu  * the write_rollback_index() AvbOps operation. Instead it is the job
2165b69db07SJason Zhu  * of the caller of avb_slot_verify() to do this based on e.g. A/B
2175b69db07SJason Zhu  * policy and other factors. See libavb_ab/avb_ab_flow.c for an
2185b69db07SJason Zhu  * example of how to do this.
2195b69db07SJason Zhu  *
2205b69db07SJason Zhu  * The |cmdline| field is a NUL-terminated string in UTF-8 resulting
2215b69db07SJason Zhu  * from concatenating all |AvbKernelCmdlineDescriptor| and then
2225b69db07SJason Zhu  * performing proper substitution of the variables
2235b69db07SJason Zhu  * $(ANDROID_SYSTEM_PARTUUID), $(ANDROID_BOOT_PARTUUID), and
2245b69db07SJason Zhu  * $(ANDROID_VBMETA_PARTUUID) using the
2255b69db07SJason Zhu  * get_unique_guid_for_partition() operation in |AvbOps|. Additionally
2265b69db07SJason Zhu  * $(ANDROID_VERITY_MODE) will be replaced with the proper dm-verity
2275b69db07SJason Zhu  * option depending on the value of |hashtree_error_mode|.
2285b69db07SJason Zhu  *
2295b69db07SJason Zhu  * Additionally, the |cmdline| field will have the following kernel
23037a7bc39SJason Zhu  * command-line options set (unless verification is disabled, see
23137a7bc39SJason Zhu  * below):
2325b69db07SJason Zhu  *
2335b69db07SJason Zhu  *   androidboot.veritymode: This is set to 'disabled' if the
2345b69db07SJason Zhu  *   AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED flag is set in top-level
2355b69db07SJason Zhu  *   vbmeta struct. Otherwise it is set to 'enforcing' if the
2365b69db07SJason Zhu  *   passed-in hashtree error mode is AVB_HASHTREE_ERROR_MODE_RESTART
2375b69db07SJason Zhu  *   or AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, 'eio' if it's
2385b69db07SJason Zhu  *   set to AVB_HASHTREE_ERROR_MODE_EIO, and 'logging' if it's set to
2395b69db07SJason Zhu  *   AVB_HASHTREE_ERROR_MODE_LOGGING.
2405b69db07SJason Zhu  *
241*69fdc596SJason Zhu  *   androidboot.veritymode.managed: This is set to 'yes' only
242*69fdc596SJason Zhu  *   if hashtree validation isn't disabled and the passed-in hashtree
243*69fdc596SJason Zhu  *   error mode is AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO.
244*69fdc596SJason Zhu  *
2455b69db07SJason Zhu  *   androidboot.vbmeta.invalidate_on_error: This is set to 'yes' only
2465b69db07SJason Zhu  *   if hashtree validation isn't disabled and the passed-in hashtree
2475b69db07SJason Zhu  *   error mode is AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE.
2485b69db07SJason Zhu  *
2495b69db07SJason Zhu  *   androidboot.vbmeta.device_state: set to "locked" or "unlocked"
2505b69db07SJason Zhu  *   depending on the result of the result of AvbOps's
2515b69db07SJason Zhu  *   read_is_unlocked() function.
2525b69db07SJason Zhu  *
2535b69db07SJason Zhu  *   androidboot.vbmeta.{hash_alg, size, digest}: Will be set to
2545b69db07SJason Zhu  *   the digest of all images in |vbmeta_images|.
2555b69db07SJason Zhu  *
2565b69db07SJason Zhu  *   androidboot.vbmeta.device: This is set to the value
2575b69db07SJason Zhu  *   PARTUUID=$(ANDROID_VBMETA_PARTUUID) before substitution so it
2585b69db07SJason Zhu  *   will end up pointing to the vbmeta partition for the verified
2595b69db07SJason Zhu  *   slot. If there is no vbmeta partition it will point to the boot
260*69fdc596SJason Zhu  *   partition of the verified slot. If the flag
261*69fdc596SJason Zhu  *   AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION is used, this is not
262*69fdc596SJason Zhu  *   set.
2635b69db07SJason Zhu  *
2645b69db07SJason Zhu  *   androidboot.vbmeta.avb_version: This is set to the decimal value
2655b69db07SJason Zhu  *   of AVB_VERSION_MAJOR followed by a dot followed by the decimal
2665b69db07SJason Zhu  *   value of AVB_VERSION_MINOR, for example "1.0" or "1.4". This
2675b69db07SJason Zhu  *   version number represents the vbmeta file format version
2685b69db07SJason Zhu  *   supported by libavb copy used in the boot loader. This is not
2695b69db07SJason Zhu  *   necessarily the same version number of the on-disk metadata for
2705b69db07SJason Zhu  *   the slot that was verified.
2715b69db07SJason Zhu  *
27237a7bc39SJason Zhu  * Note that androidboot.slot_suffix is not set in the |cmdline| field
27337a7bc39SJason Zhu  * in |AvbSlotVerifyData| - you will have to set this yourself.
2745b69db07SJason Zhu  *
27537a7bc39SJason Zhu  * If the |AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED| flag is set
27637a7bc39SJason Zhu  * in the top-level vbmeta struct then only the top-level vbmeta
27737a7bc39SJason Zhu  * struct is verified and descriptors will not processed. The return
27837a7bc39SJason Zhu  * value will be set accordingly (if this flag is set via 'avbctl
27937a7bc39SJason Zhu  * disable-verification' then the return value will be
28037a7bc39SJason Zhu  * |AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION|) and
28137a7bc39SJason Zhu  * |AvbSlotVerifyData| is returned. Additionally all partitions in the
28237a7bc39SJason Zhu  * |requested_partitions| are loaded and the |cmdline| field is set to
28337a7bc39SJason Zhu  * "root=PARTUUID=$(ANDROID_SYSTEM_PARTUUID)" and the GUID for the
28437a7bc39SJason Zhu  * appropriate system partition is substituted in. Note that none of
28537a7bc39SJason Zhu  * the androidboot.* options mentioned above will be set.
2865b69db07SJason Zhu  *
287*69fdc596SJason Zhu  * The |resolved_hashtree_error_mode| is the the value of the passed
288*69fdc596SJason Zhu  * avb_slot_verify()'s |hashtree_error_mode| parameter except that it never has
289*69fdc596SJason Zhu  * the value AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO. If this value was
290*69fdc596SJason Zhu  * passed in, then the restart/eio state machine is used resulting in
291*69fdc596SJason Zhu  * |resolved_hashtree_error_mode| being set to either
292*69fdc596SJason Zhu  * AVB_HASHTREE_ERROR_MODE_RESTART or AVB_HASHTREE_ERROR_MODE_EIO.  If set to
293*69fdc596SJason Zhu  * AVB_HASHTREE_ERROR_MODE_EIO the boot loader should present a RED warning
294*69fdc596SJason Zhu  * screen for the user to click through before continuing to boot.
295*69fdc596SJason Zhu  *
2965b69db07SJason Zhu  * This struct may grow in the future without it being considered an
2975b69db07SJason Zhu  * ABI break.
2985b69db07SJason Zhu  */
2995b69db07SJason Zhu typedef struct {
3005b69db07SJason Zhu   char* ab_suffix;
3015b69db07SJason Zhu   AvbVBMetaData* vbmeta_images;
3025b69db07SJason Zhu   size_t num_vbmeta_images;
3035b69db07SJason Zhu   AvbPartitionData* loaded_partitions;
3045b69db07SJason Zhu   size_t num_loaded_partitions;
3055b69db07SJason Zhu   char* cmdline;
3065b69db07SJason Zhu   uint64_t rollback_indexes[AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS];
307*69fdc596SJason Zhu   AvbHashtreeErrorMode resolved_hashtree_error_mode;
3085b69db07SJason Zhu } AvbSlotVerifyData;
3095b69db07SJason Zhu 
310ab608f80SJason Zhu /* Calculates a digest of all vbmeta images in |data| using
311ab608f80SJason Zhu  * the digest indicated by |digest_type|. Stores the result
312ab608f80SJason Zhu  * in |out_digest| which must be large enough to hold a digest
313ab608f80SJason Zhu  * of the requested type.
314ab608f80SJason Zhu  */
315ab608f80SJason Zhu void avb_slot_verify_data_calculate_vbmeta_digest(AvbSlotVerifyData* data,
316ab608f80SJason Zhu                                                   AvbDigestType digest_type,
317ab608f80SJason Zhu                                                   uint8_t* out_digest);
318ab608f80SJason Zhu 
3195b69db07SJason Zhu /* Frees a |AvbSlotVerifyData| including all data it points to. */
3205b69db07SJason Zhu void avb_slot_verify_data_free(AvbSlotVerifyData* data);
3215b69db07SJason Zhu 
3225b69db07SJason Zhu /* Performs a full verification of the slot identified by |ab_suffix|
3235b69db07SJason Zhu  * and load and verify the contents of the partitions whose name is in
3245b69db07SJason Zhu  * the NULL-terminated string array |requested_partitions| (each
3255b69db07SJason Zhu  * partition must use hash verification). If not using A/B, pass an
3265b69db07SJason Zhu  * empty string (e.g. "", not NULL) for |ab_suffix|. This parameter
3275b69db07SJason Zhu  * must include the leading underscore, for example "_a" should be
3285b69db07SJason Zhu  * used to refer to the first slot.
3295b69db07SJason Zhu  *
3305b69db07SJason Zhu  * Typically the |requested_partitions| array only contains a single
3315b69db07SJason Zhu  * item for the boot partition, 'boot'.
3325b69db07SJason Zhu  *
3335b69db07SJason Zhu  * Verification includes loading and verifying data from the 'vbmeta',
3345b69db07SJason Zhu  * the requested hash partitions, and possibly other partitions (with
3355b69db07SJason Zhu  * |ab_suffix| appended), inspecting rollback indexes, and checking if
3365b69db07SJason Zhu  * the public key used to sign the data is acceptable. The functions
3375b69db07SJason Zhu  * in |ops| will be used to do this.
3385b69db07SJason Zhu  *
3395b69db07SJason Zhu  * If |out_data| is not NULL, it will be set to a newly allocated
3405b69db07SJason Zhu  * |AvbSlotVerifyData| struct containing all the data needed to
3415b69db07SJason Zhu  * actually boot the slot. This data structure should be freed with
3425b69db07SJason Zhu  * avb_slot_verify_data_free() when you are done with it. See below
3435b69db07SJason Zhu  * for when this is returned.
3445b69db07SJason Zhu  *
3455b69db07SJason Zhu  * The |flags| parameter is used to influence the semantics of
3465b69db07SJason Zhu  * avb_slot_verify() - for example the
3475b69db07SJason Zhu  * AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR flag can be used to
3485b69db07SJason Zhu  * ignore verification errors which is something needed in the
3495b69db07SJason Zhu  * UNLOCKED state. See the AvbSlotVerifyFlags enumeration for details.
3505b69db07SJason Zhu  *
351*69fdc596SJason Zhu  * The |hashtree_error_mode| parameter should be set to the desired error
352*69fdc596SJason Zhu  * handling mode. See the AvbHashtreeErrorMode enumeration for details.
3535b69db07SJason Zhu  *
3545b69db07SJason Zhu  * Also note that |out_data| is never set if
3555b69db07SJason Zhu  * AVB_SLOT_VERIFY_RESULT_ERROR_OOM, AVB_SLOT_VERIFY_RESULT_ERROR_IO,
3565b69db07SJason Zhu  * or AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA is returned.
3575b69db07SJason Zhu  *
3585b69db07SJason Zhu  * AVB_SLOT_VERIFY_RESULT_OK is returned if everything is verified
3595b69db07SJason Zhu  * correctly and all public keys are accepted.
3605b69db07SJason Zhu  *
3615b69db07SJason Zhu  * AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED is returned if
3625b69db07SJason Zhu  * everything is verified correctly out but one or more public keys
3635b69db07SJason Zhu  * are not accepted. This includes the case where integrity data is
3645b69db07SJason Zhu  * not signed.
3655b69db07SJason Zhu  *
3665b69db07SJason Zhu  * AVB_SLOT_VERIFY_RESULT_ERROR_OOM is returned if unable to
3675b69db07SJason Zhu  * allocate memory.
3685b69db07SJason Zhu  *
3695b69db07SJason Zhu  * AVB_SLOT_VERIFY_RESULT_ERROR_IO is returned if an I/O error
3705b69db07SJason Zhu  * occurred while trying to load data or get a rollback index.
3715b69db07SJason Zhu  *
3725b69db07SJason Zhu  * AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION is returned if the data
3735b69db07SJason Zhu  * did not verify, e.g. the digest didn't match or signature checks
3745b69db07SJason Zhu  * failed.
3755b69db07SJason Zhu  *
3765b69db07SJason Zhu  * AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX is returned if a
3775b69db07SJason Zhu  * rollback index was less than its stored value.
3785b69db07SJason Zhu  *
3795b69db07SJason Zhu  * AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA is returned if some
3805b69db07SJason Zhu  * of the metadata is invalid or inconsistent.
3815b69db07SJason Zhu  *
3825b69db07SJason Zhu  * AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION is returned if
3835b69db07SJason Zhu  * some of the metadata requires a newer version of libavb than what
3845b69db07SJason Zhu  * is in use.
3855b69db07SJason Zhu  *
3865b69db07SJason Zhu  * AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT is returned if the
3875b69db07SJason Zhu  * caller passed invalid parameters, for example trying to use
3885b69db07SJason Zhu  * AVB_HASHTREE_ERROR_MODE_LOGGING without
3895b69db07SJason Zhu  * AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR.
3905b69db07SJason Zhu  */
3915b69db07SJason Zhu AvbSlotVerifyResult avb_slot_verify(AvbOps* ops,
3925b69db07SJason Zhu                                     const char* const* requested_partitions,
3935b69db07SJason Zhu                                     const char* ab_suffix,
3945b69db07SJason Zhu                                     AvbSlotVerifyFlags flags,
3955b69db07SJason Zhu                                     AvbHashtreeErrorMode hashtree_error_mode,
3965b69db07SJason Zhu                                     AvbSlotVerifyData** out_data);
3975b69db07SJason Zhu 
3985b69db07SJason Zhu #ifdef __cplusplus
3995b69db07SJason Zhu }
4005b69db07SJason Zhu #endif
4015b69db07SJason Zhu 
4025b69db07SJason Zhu #endif /* AVB_SLOT_VERIFY_H_ */
403