1*4882a593Smuzhiyun /* 2*4882a593Smuzhiyun * Copyright (C) 2016 The Android Open Source Project 3*4882a593Smuzhiyun * 4*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person 5*4882a593Smuzhiyun * obtaining a copy of this software and associated documentation 6*4882a593Smuzhiyun * files (the "Software"), to deal in the Software without 7*4882a593Smuzhiyun * restriction, including without limitation the rights to use, copy, 8*4882a593Smuzhiyun * modify, merge, publish, distribute, sublicense, and/or sell copies 9*4882a593Smuzhiyun * of the Software, and to permit persons to whom the Software is 10*4882a593Smuzhiyun * furnished to do so, subject to the following conditions: 11*4882a593Smuzhiyun * 12*4882a593Smuzhiyun * The above copyright notice and this permission notice shall be 13*4882a593Smuzhiyun * included in all copies or substantial portions of the Software. 14*4882a593Smuzhiyun * 15*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16*4882a593Smuzhiyun * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17*4882a593Smuzhiyun * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18*4882a593Smuzhiyun * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19*4882a593Smuzhiyun * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20*4882a593Smuzhiyun * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21*4882a593Smuzhiyun * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22*4882a593Smuzhiyun * SOFTWARE. 23*4882a593Smuzhiyun */ 24*4882a593Smuzhiyun 25*4882a593Smuzhiyun /* 26*4882a593Smuzhiyun #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION) 27*4882a593Smuzhiyun #error "Never include this file directly, include libavb.h instead." 28*4882a593Smuzhiyun #endif 29*4882a593Smuzhiyun */ 30*4882a593Smuzhiyun 31*4882a593Smuzhiyun #ifndef AVB_SLOT_VERIFY_H_ 32*4882a593Smuzhiyun #define AVB_SLOT_VERIFY_H_ 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun #include <android_avb/avb_ops.h> 35*4882a593Smuzhiyun #include <android_avb/avb_vbmeta_image.h> 36*4882a593Smuzhiyun 37*4882a593Smuzhiyun #ifdef __cplusplus 38*4882a593Smuzhiyun extern "C" { 39*4882a593Smuzhiyun #endif 40*4882a593Smuzhiyun 41*4882a593Smuzhiyun /* Return codes used in avb_slot_verify(), see that function for 42*4882a593Smuzhiyun * documentation for each field. 43*4882a593Smuzhiyun * 44*4882a593Smuzhiyun * Use avb_slot_verify_result_to_string() to get a textual 45*4882a593Smuzhiyun * representation usable for error/debug output. 46*4882a593Smuzhiyun */ 47*4882a593Smuzhiyun typedef enum { 48*4882a593Smuzhiyun AVB_SLOT_VERIFY_RESULT_OK, 49*4882a593Smuzhiyun AVB_SLOT_VERIFY_RESULT_ERROR_OOM, 50*4882a593Smuzhiyun AVB_SLOT_VERIFY_RESULT_ERROR_IO, 51*4882a593Smuzhiyun AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, 52*4882a593Smuzhiyun AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX, 53*4882a593Smuzhiyun AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED, 54*4882a593Smuzhiyun AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA, 55*4882a593Smuzhiyun AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION, 56*4882a593Smuzhiyun AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT 57*4882a593Smuzhiyun } AvbSlotVerifyResult; 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun /* Various error handling modes for when verification fails using a 60*4882a593Smuzhiyun * hashtree at runtime inside the HLOS. 61*4882a593Smuzhiyun * 62*4882a593Smuzhiyun * AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE means that the OS 63*4882a593Smuzhiyun * will invalidate the current slot and restart. 64*4882a593Smuzhiyun * 65*4882a593Smuzhiyun * AVB_HASHTREE_ERROR_MODE_RESTART means that the OS will restart. 66*4882a593Smuzhiyun * 67*4882a593Smuzhiyun * AVB_HASHTREE_ERROR_MODE_EIO means that an EIO error will be 68*4882a593Smuzhiyun * returned to applications. 69*4882a593Smuzhiyun * 70*4882a593Smuzhiyun * AVB_HASHTREE_ERROR_MODE_LOGGING means that errors will be logged 71*4882a593Smuzhiyun * and corrupt data may be returned to applications. This mode should 72*4882a593Smuzhiyun * be used ONLY for diagnostics and debugging. It cannot be used 73*4882a593Smuzhiyun * unless AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is also 74*4882a593Smuzhiyun * used. 75*4882a593Smuzhiyun * 76*4882a593Smuzhiyun * AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO means that either 77*4882a593Smuzhiyun * AVB_HASHTREE_ERROR_MODE_RESTART or AVB_HASHTREE_ERROR_MODE_EIO is used 78*4882a593Smuzhiyun * depending on state. This mode implements a state machine whereby 79*4882a593Smuzhiyun * AVB_HASHTREE_ERROR_MODE_RESTART is used by default and when 80*4882a593Smuzhiyun * AVB_SLOT_VERIFY_FLAGS_RESTART_CAUSED_BY_HASHTREE_CORRUPTION is passed the 81*4882a593Smuzhiyun * mode transitions to AVB_HASHTREE_ERROR_MODE_EIO. When a new OS has been 82*4882a593Smuzhiyun * detected the device transitions back to the AVB_HASHTREE_ERROR_MODE_RESTART 83*4882a593Smuzhiyun * mode. To do this persistent storage is needed - specifically this means that 84*4882a593Smuzhiyun * the passed in AvbOps will need to have the read_persistent_value() and 85*4882a593Smuzhiyun * write_persistent_value() operations implemented. The name of the persistent 86*4882a593Smuzhiyun * value used is "avb.managed_verity_mode" and 32 bytes of storage is needed. 87*4882a593Smuzhiyun */ 88*4882a593Smuzhiyun typedef enum { 89*4882a593Smuzhiyun AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, 90*4882a593Smuzhiyun AVB_HASHTREE_ERROR_MODE_RESTART, 91*4882a593Smuzhiyun AVB_HASHTREE_ERROR_MODE_EIO, 92*4882a593Smuzhiyun AVB_HASHTREE_ERROR_MODE_LOGGING, 93*4882a593Smuzhiyun AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO 94*4882a593Smuzhiyun } AvbHashtreeErrorMode; 95*4882a593Smuzhiyun 96*4882a593Smuzhiyun /* Flags that influence how avb_slot_verify() works. 97*4882a593Smuzhiyun * 98*4882a593Smuzhiyun * If AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is NOT set then 99*4882a593Smuzhiyun * avb_slot_verify() will bail out as soon as an error is encountered 100*4882a593Smuzhiyun * and |out_data| is set only if AVB_SLOT_VERIFY_RESULT_OK is 101*4882a593Smuzhiyun * returned. 102*4882a593Smuzhiyun * 103*4882a593Smuzhiyun * Otherwise if AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is set 104*4882a593Smuzhiyun * avb_slot_verify() will continue verification efforts and |out_data| 105*4882a593Smuzhiyun * is also set if AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED, 106*4882a593Smuzhiyun * AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, or 107*4882a593Smuzhiyun * AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX is returned. It is 108*4882a593Smuzhiyun * undefined which error is returned if more than one distinct error 109*4882a593Smuzhiyun * is encountered. It is guaranteed that AVB_SLOT_VERIFY_RESULT_OK is 110*4882a593Smuzhiyun * returned if, and only if, there are no errors. This mode is needed 111*4882a593Smuzhiyun * to boot valid but unverified slots when the device is unlocked. 112*4882a593Smuzhiyun * 113*4882a593Smuzhiyun * Also, if AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR is set the 114*4882a593Smuzhiyun * contents loaded from |requested_partition| will be the contents of 115*4882a593Smuzhiyun * the entire partition instead of just the size specified in the hash 116*4882a593Smuzhiyun * descriptor. 117*4882a593Smuzhiyun * 118*4882a593Smuzhiyun * The AVB_SLOT_VERIFY_FLAGS_RESTART_CAUSED_BY_HASHTREE_CORRUPTION flag 119*4882a593Smuzhiyun * should be set if using AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO 120*4882a593Smuzhiyun * and the reason the boot loader is running is because the device 121*4882a593Smuzhiyun * was restarted by the dm-verity driver. 122*4882a593Smuzhiyun * 123*4882a593Smuzhiyun * If the AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION flag is set then 124*4882a593Smuzhiyun * data won't be loaded from the "vbmeta" partition and the 125*4882a593Smuzhiyun * |validate_vbmeta_public_key| operation is never called. Instead, the 126*4882a593Smuzhiyun * vbmeta structs in |requested_partitions| are loaded and processed and the 127*4882a593Smuzhiyun * |validate_public_key_for_partition| operation is called for each of these 128*4882a593Smuzhiyun * vbmeta structs. This flag is useful when booting into recovery on a device 129*4882a593Smuzhiyun * not using A/B - see section "Booting into recovery" in README.md for 130*4882a593Smuzhiyun * more information. 131*4882a593Smuzhiyun */ 132*4882a593Smuzhiyun typedef enum { 133*4882a593Smuzhiyun AVB_SLOT_VERIFY_FLAGS_NONE = 0, 134*4882a593Smuzhiyun AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR = (1 << 0), 135*4882a593Smuzhiyun AVB_SLOT_VERIFY_FLAGS_RESTART_CAUSED_BY_HASHTREE_CORRUPTION = (1 << 1), 136*4882a593Smuzhiyun AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION = (1 << 2), 137*4882a593Smuzhiyun } AvbSlotVerifyFlags; 138*4882a593Smuzhiyun 139*4882a593Smuzhiyun /* Get a textual representation of |result|. */ 140*4882a593Smuzhiyun const char* avb_slot_verify_result_to_string(AvbSlotVerifyResult result); 141*4882a593Smuzhiyun 142*4882a593Smuzhiyun /* Maximum number of rollback index locations supported. */ 143*4882a593Smuzhiyun #define AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS 32 144*4882a593Smuzhiyun 145*4882a593Smuzhiyun /* AvbPartitionData contains data loaded from partitions when using 146*4882a593Smuzhiyun * avb_slot_verify(). The |partition_name| field contains the name of 147*4882a593Smuzhiyun * the partition (without A/B suffix), |data| points to the loaded 148*4882a593Smuzhiyun * data which is |data_size| bytes long. If |preloaded| is set to true, 149*4882a593Smuzhiyun * this structure dose not own |data|. The caller of |avb_slot_verify| 150*4882a593Smuzhiyun * needs to make sure that the preloaded data outlives this 151*4882a593Smuzhiyun * |AvbPartitionData| structure. 152*4882a593Smuzhiyun * 153*4882a593Smuzhiyun * Note that this is strictly less than the partition size - it's only 154*4882a593Smuzhiyun * the image stored there, not the entire partition nor any of the 155*4882a593Smuzhiyun * metadata. 156*4882a593Smuzhiyun */ 157*4882a593Smuzhiyun typedef struct { 158*4882a593Smuzhiyun char* partition_name; 159*4882a593Smuzhiyun uint8_t* data; 160*4882a593Smuzhiyun size_t data_size; 161*4882a593Smuzhiyun bool preloaded; 162*4882a593Smuzhiyun } AvbPartitionData; 163*4882a593Smuzhiyun 164*4882a593Smuzhiyun /* AvbVBMetaData contains a vbmeta struct loaded from a partition when 165*4882a593Smuzhiyun * using avb_slot_verify(). The |partition_name| field contains the 166*4882a593Smuzhiyun * name of the partition (without A/B suffix), |vbmeta_data| points to 167*4882a593Smuzhiyun * the loaded data which is |vbmeta_size| bytes long. 168*4882a593Smuzhiyun * 169*4882a593Smuzhiyun * The |verify_result| field contains the result of 170*4882a593Smuzhiyun * avb_vbmeta_image_verify() on the data. This is guaranteed to be 171*4882a593Smuzhiyun * AVB_VBMETA_VERIFY_RESULT_OK for all vbmeta images if 172*4882a593Smuzhiyun * avb_slot_verify() returns AVB_SLOT_VERIFY_RESULT_OK. 173*4882a593Smuzhiyun * 174*4882a593Smuzhiyun * You can use avb_descriptor_get_all(), avb_descriptor_foreach(), and 175*4882a593Smuzhiyun * avb_vbmeta_image_header_to_host_byte_order() with this data. 176*4882a593Smuzhiyun */ 177*4882a593Smuzhiyun typedef struct { 178*4882a593Smuzhiyun char* partition_name; 179*4882a593Smuzhiyun uint8_t* vbmeta_data; 180*4882a593Smuzhiyun size_t vbmeta_size; 181*4882a593Smuzhiyun AvbVBMetaVerifyResult verify_result; 182*4882a593Smuzhiyun } AvbVBMetaData; 183*4882a593Smuzhiyun 184*4882a593Smuzhiyun /* AvbSlotVerifyData contains data needed to boot a particular slot 185*4882a593Smuzhiyun * and is returned by avb_slot_verify() if partitions in a slot are 186*4882a593Smuzhiyun * successfully verified. 187*4882a593Smuzhiyun * 188*4882a593Smuzhiyun * All data pointed to by this struct - including data in each item in 189*4882a593Smuzhiyun * the |partitions| array - will be freed when the 190*4882a593Smuzhiyun * avb_slot_verify_data_free() function is called. 191*4882a593Smuzhiyun * 192*4882a593Smuzhiyun * The |ab_suffix| field is the copy of the of |ab_suffix| field 193*4882a593Smuzhiyun * passed to avb_slot_verify(). It is the A/B suffix of the slot. This 194*4882a593Smuzhiyun * value includes the leading underscore - typical values are "" (if 195*4882a593Smuzhiyun * no slots are in use), "_a" (for the first slot), and "_b" (for the 196*4882a593Smuzhiyun * second slot). 197*4882a593Smuzhiyun * 198*4882a593Smuzhiyun * The VBMeta images that were checked are available in the 199*4882a593Smuzhiyun * |vbmeta_images| field. The field |num_vbmeta_images| contains the 200*4882a593Smuzhiyun * number of elements in this array. The first element - 201*4882a593Smuzhiyun * vbmeta_images[0] - is guaranteed to be from the partition with the 202*4882a593Smuzhiyun * top-level vbmeta struct. This is usually the "vbmeta" partition in 203*4882a593Smuzhiyun * the requested slot but if there is no "vbmeta" partition it can 204*4882a593Smuzhiyun * also be the "boot" partition. 205*4882a593Smuzhiyun * 206*4882a593Smuzhiyun * The partitions loaded and verified from from the slot are 207*4882a593Smuzhiyun * accessible in the |loaded_partitions| array. The field 208*4882a593Smuzhiyun * |num_loaded_partitions| contains the number of elements in this 209*4882a593Smuzhiyun * array. The order of partitions in this array may not necessarily be 210*4882a593Smuzhiyun * the same order as in the passed-in |requested_partitions| array. 211*4882a593Smuzhiyun * 212*4882a593Smuzhiyun * Rollback indexes for the verified slot are stored in the 213*4882a593Smuzhiyun * |rollback_indexes| field. Note that avb_slot_verify() will NEVER 214*4882a593Smuzhiyun * modify stored_rollback_index[n] locations e.g. it will never use 215*4882a593Smuzhiyun * the write_rollback_index() AvbOps operation. Instead it is the job 216*4882a593Smuzhiyun * of the caller of avb_slot_verify() to do this based on e.g. A/B 217*4882a593Smuzhiyun * policy and other factors. See libavb_ab/avb_ab_flow.c for an 218*4882a593Smuzhiyun * example of how to do this. 219*4882a593Smuzhiyun * 220*4882a593Smuzhiyun * The |cmdline| field is a NUL-terminated string in UTF-8 resulting 221*4882a593Smuzhiyun * from concatenating all |AvbKernelCmdlineDescriptor| and then 222*4882a593Smuzhiyun * performing proper substitution of the variables 223*4882a593Smuzhiyun * $(ANDROID_SYSTEM_PARTUUID), $(ANDROID_BOOT_PARTUUID), and 224*4882a593Smuzhiyun * $(ANDROID_VBMETA_PARTUUID) using the 225*4882a593Smuzhiyun * get_unique_guid_for_partition() operation in |AvbOps|. Additionally 226*4882a593Smuzhiyun * $(ANDROID_VERITY_MODE) will be replaced with the proper dm-verity 227*4882a593Smuzhiyun * option depending on the value of |hashtree_error_mode|. 228*4882a593Smuzhiyun * 229*4882a593Smuzhiyun * Additionally, the |cmdline| field will have the following kernel 230*4882a593Smuzhiyun * command-line options set (unless verification is disabled, see 231*4882a593Smuzhiyun * below): 232*4882a593Smuzhiyun * 233*4882a593Smuzhiyun * androidboot.veritymode: This is set to 'disabled' if the 234*4882a593Smuzhiyun * AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED flag is set in top-level 235*4882a593Smuzhiyun * vbmeta struct. Otherwise it is set to 'enforcing' if the 236*4882a593Smuzhiyun * passed-in hashtree error mode is AVB_HASHTREE_ERROR_MODE_RESTART 237*4882a593Smuzhiyun * or AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE, 'eio' if it's 238*4882a593Smuzhiyun * set to AVB_HASHTREE_ERROR_MODE_EIO, and 'logging' if it's set to 239*4882a593Smuzhiyun * AVB_HASHTREE_ERROR_MODE_LOGGING. 240*4882a593Smuzhiyun * 241*4882a593Smuzhiyun * androidboot.veritymode.managed: This is set to 'yes' only 242*4882a593Smuzhiyun * if hashtree validation isn't disabled and the passed-in hashtree 243*4882a593Smuzhiyun * error mode is AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO. 244*4882a593Smuzhiyun * 245*4882a593Smuzhiyun * androidboot.vbmeta.invalidate_on_error: This is set to 'yes' only 246*4882a593Smuzhiyun * if hashtree validation isn't disabled and the passed-in hashtree 247*4882a593Smuzhiyun * error mode is AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE. 248*4882a593Smuzhiyun * 249*4882a593Smuzhiyun * androidboot.vbmeta.device_state: set to "locked" or "unlocked" 250*4882a593Smuzhiyun * depending on the result of the result of AvbOps's 251*4882a593Smuzhiyun * read_is_unlocked() function. 252*4882a593Smuzhiyun * 253*4882a593Smuzhiyun * androidboot.vbmeta.{hash_alg, size, digest}: Will be set to 254*4882a593Smuzhiyun * the digest of all images in |vbmeta_images|. 255*4882a593Smuzhiyun * 256*4882a593Smuzhiyun * androidboot.vbmeta.device: This is set to the value 257*4882a593Smuzhiyun * PARTUUID=$(ANDROID_VBMETA_PARTUUID) before substitution so it 258*4882a593Smuzhiyun * will end up pointing to the vbmeta partition for the verified 259*4882a593Smuzhiyun * slot. If there is no vbmeta partition it will point to the boot 260*4882a593Smuzhiyun * partition of the verified slot. If the flag 261*4882a593Smuzhiyun * AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION is used, this is not 262*4882a593Smuzhiyun * set. 263*4882a593Smuzhiyun * 264*4882a593Smuzhiyun * androidboot.vbmeta.avb_version: This is set to the decimal value 265*4882a593Smuzhiyun * of AVB_VERSION_MAJOR followed by a dot followed by the decimal 266*4882a593Smuzhiyun * value of AVB_VERSION_MINOR, for example "1.0" or "1.4". This 267*4882a593Smuzhiyun * version number represents the vbmeta file format version 268*4882a593Smuzhiyun * supported by libavb copy used in the boot loader. This is not 269*4882a593Smuzhiyun * necessarily the same version number of the on-disk metadata for 270*4882a593Smuzhiyun * the slot that was verified. 271*4882a593Smuzhiyun * 272*4882a593Smuzhiyun * Note that androidboot.slot_suffix is not set in the |cmdline| field 273*4882a593Smuzhiyun * in |AvbSlotVerifyData| - you will have to set this yourself. 274*4882a593Smuzhiyun * 275*4882a593Smuzhiyun * If the |AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED| flag is set 276*4882a593Smuzhiyun * in the top-level vbmeta struct then only the top-level vbmeta 277*4882a593Smuzhiyun * struct is verified and descriptors will not processed. The return 278*4882a593Smuzhiyun * value will be set accordingly (if this flag is set via 'avbctl 279*4882a593Smuzhiyun * disable-verification' then the return value will be 280*4882a593Smuzhiyun * |AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION|) and 281*4882a593Smuzhiyun * |AvbSlotVerifyData| is returned. Additionally all partitions in the 282*4882a593Smuzhiyun * |requested_partitions| are loaded and the |cmdline| field is set to 283*4882a593Smuzhiyun * "root=PARTUUID=$(ANDROID_SYSTEM_PARTUUID)" and the GUID for the 284*4882a593Smuzhiyun * appropriate system partition is substituted in. Note that none of 285*4882a593Smuzhiyun * the androidboot.* options mentioned above will be set. 286*4882a593Smuzhiyun * 287*4882a593Smuzhiyun * The |resolved_hashtree_error_mode| is the the value of the passed 288*4882a593Smuzhiyun * avb_slot_verify()'s |hashtree_error_mode| parameter except that it never has 289*4882a593Smuzhiyun * the value AVB_HASHTREE_ERROR_MODE_MANAGED_RESTART_AND_EIO. If this value was 290*4882a593Smuzhiyun * passed in, then the restart/eio state machine is used resulting in 291*4882a593Smuzhiyun * |resolved_hashtree_error_mode| being set to either 292*4882a593Smuzhiyun * AVB_HASHTREE_ERROR_MODE_RESTART or AVB_HASHTREE_ERROR_MODE_EIO. If set to 293*4882a593Smuzhiyun * AVB_HASHTREE_ERROR_MODE_EIO the boot loader should present a RED warning 294*4882a593Smuzhiyun * screen for the user to click through before continuing to boot. 295*4882a593Smuzhiyun * 296*4882a593Smuzhiyun * This struct may grow in the future without it being considered an 297*4882a593Smuzhiyun * ABI break. 298*4882a593Smuzhiyun */ 299*4882a593Smuzhiyun typedef struct { 300*4882a593Smuzhiyun char* ab_suffix; 301*4882a593Smuzhiyun AvbVBMetaData* vbmeta_images; 302*4882a593Smuzhiyun size_t num_vbmeta_images; 303*4882a593Smuzhiyun AvbPartitionData* loaded_partitions; 304*4882a593Smuzhiyun size_t num_loaded_partitions; 305*4882a593Smuzhiyun char* cmdline; 306*4882a593Smuzhiyun uint64_t rollback_indexes[AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS]; 307*4882a593Smuzhiyun AvbHashtreeErrorMode resolved_hashtree_error_mode; 308*4882a593Smuzhiyun } AvbSlotVerifyData; 309*4882a593Smuzhiyun 310*4882a593Smuzhiyun /* Calculates a digest of all vbmeta images in |data| using 311*4882a593Smuzhiyun * the digest indicated by |digest_type|. Stores the result 312*4882a593Smuzhiyun * in |out_digest| which must be large enough to hold a digest 313*4882a593Smuzhiyun * of the requested type. 314*4882a593Smuzhiyun */ 315*4882a593Smuzhiyun void avb_slot_verify_data_calculate_vbmeta_digest(AvbSlotVerifyData* data, 316*4882a593Smuzhiyun AvbDigestType digest_type, 317*4882a593Smuzhiyun uint8_t* out_digest); 318*4882a593Smuzhiyun 319*4882a593Smuzhiyun /* Frees a |AvbSlotVerifyData| including all data it points to. */ 320*4882a593Smuzhiyun void avb_slot_verify_data_free(AvbSlotVerifyData* data); 321*4882a593Smuzhiyun 322*4882a593Smuzhiyun /* Performs a full verification of the slot identified by |ab_suffix| 323*4882a593Smuzhiyun * and load and verify the contents of the partitions whose name is in 324*4882a593Smuzhiyun * the NULL-terminated string array |requested_partitions| (each 325*4882a593Smuzhiyun * partition must use hash verification). If not using A/B, pass an 326*4882a593Smuzhiyun * empty string (e.g. "", not NULL) for |ab_suffix|. This parameter 327*4882a593Smuzhiyun * must include the leading underscore, for example "_a" should be 328*4882a593Smuzhiyun * used to refer to the first slot. 329*4882a593Smuzhiyun * 330*4882a593Smuzhiyun * Typically the |requested_partitions| array only contains a single 331*4882a593Smuzhiyun * item for the boot partition, 'boot'. 332*4882a593Smuzhiyun * 333*4882a593Smuzhiyun * Verification includes loading and verifying data from the 'vbmeta', 334*4882a593Smuzhiyun * the requested hash partitions, and possibly other partitions (with 335*4882a593Smuzhiyun * |ab_suffix| appended), inspecting rollback indexes, and checking if 336*4882a593Smuzhiyun * the public key used to sign the data is acceptable. The functions 337*4882a593Smuzhiyun * in |ops| will be used to do this. 338*4882a593Smuzhiyun * 339*4882a593Smuzhiyun * If |out_data| is not NULL, it will be set to a newly allocated 340*4882a593Smuzhiyun * |AvbSlotVerifyData| struct containing all the data needed to 341*4882a593Smuzhiyun * actually boot the slot. This data structure should be freed with 342*4882a593Smuzhiyun * avb_slot_verify_data_free() when you are done with it. See below 343*4882a593Smuzhiyun * for when this is returned. 344*4882a593Smuzhiyun * 345*4882a593Smuzhiyun * The |flags| parameter is used to influence the semantics of 346*4882a593Smuzhiyun * avb_slot_verify() - for example the 347*4882a593Smuzhiyun * AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR flag can be used to 348*4882a593Smuzhiyun * ignore verification errors which is something needed in the 349*4882a593Smuzhiyun * UNLOCKED state. See the AvbSlotVerifyFlags enumeration for details. 350*4882a593Smuzhiyun * 351*4882a593Smuzhiyun * The |hashtree_error_mode| parameter should be set to the desired error 352*4882a593Smuzhiyun * handling mode. See the AvbHashtreeErrorMode enumeration for details. 353*4882a593Smuzhiyun * 354*4882a593Smuzhiyun * Also note that |out_data| is never set if 355*4882a593Smuzhiyun * AVB_SLOT_VERIFY_RESULT_ERROR_OOM, AVB_SLOT_VERIFY_RESULT_ERROR_IO, 356*4882a593Smuzhiyun * or AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA is returned. 357*4882a593Smuzhiyun * 358*4882a593Smuzhiyun * AVB_SLOT_VERIFY_RESULT_OK is returned if everything is verified 359*4882a593Smuzhiyun * correctly and all public keys are accepted. 360*4882a593Smuzhiyun * 361*4882a593Smuzhiyun * AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED is returned if 362*4882a593Smuzhiyun * everything is verified correctly out but one or more public keys 363*4882a593Smuzhiyun * are not accepted. This includes the case where integrity data is 364*4882a593Smuzhiyun * not signed. 365*4882a593Smuzhiyun * 366*4882a593Smuzhiyun * AVB_SLOT_VERIFY_RESULT_ERROR_OOM is returned if unable to 367*4882a593Smuzhiyun * allocate memory. 368*4882a593Smuzhiyun * 369*4882a593Smuzhiyun * AVB_SLOT_VERIFY_RESULT_ERROR_IO is returned if an I/O error 370*4882a593Smuzhiyun * occurred while trying to load data or get a rollback index. 371*4882a593Smuzhiyun * 372*4882a593Smuzhiyun * AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION is returned if the data 373*4882a593Smuzhiyun * did not verify, e.g. the digest didn't match or signature checks 374*4882a593Smuzhiyun * failed. 375*4882a593Smuzhiyun * 376*4882a593Smuzhiyun * AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX is returned if a 377*4882a593Smuzhiyun * rollback index was less than its stored value. 378*4882a593Smuzhiyun * 379*4882a593Smuzhiyun * AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA is returned if some 380*4882a593Smuzhiyun * of the metadata is invalid or inconsistent. 381*4882a593Smuzhiyun * 382*4882a593Smuzhiyun * AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION is returned if 383*4882a593Smuzhiyun * some of the metadata requires a newer version of libavb than what 384*4882a593Smuzhiyun * is in use. 385*4882a593Smuzhiyun * 386*4882a593Smuzhiyun * AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT is returned if the 387*4882a593Smuzhiyun * caller passed invalid parameters, for example trying to use 388*4882a593Smuzhiyun * AVB_HASHTREE_ERROR_MODE_LOGGING without 389*4882a593Smuzhiyun * AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR. 390*4882a593Smuzhiyun */ 391*4882a593Smuzhiyun AvbSlotVerifyResult avb_slot_verify(AvbOps* ops, 392*4882a593Smuzhiyun const char* const* requested_partitions, 393*4882a593Smuzhiyun const char* ab_suffix, 394*4882a593Smuzhiyun AvbSlotVerifyFlags flags, 395*4882a593Smuzhiyun AvbHashtreeErrorMode hashtree_error_mode, 396*4882a593Smuzhiyun AvbSlotVerifyData** out_data); 397*4882a593Smuzhiyun 398*4882a593Smuzhiyun #ifdef __cplusplus 399*4882a593Smuzhiyun } 400*4882a593Smuzhiyun #endif 401*4882a593Smuzhiyun 402*4882a593Smuzhiyun #endif /* AVB_SLOT_VERIFY_H_ */ 403