1*d289c2baSAndroid Build Coastguard Worker /* 2*d289c2baSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project 3*d289c2baSAndroid Build Coastguard Worker * 4*d289c2baSAndroid Build Coastguard Worker * Permission is hereby granted, free of charge, to any person 5*d289c2baSAndroid Build Coastguard Worker * obtaining a copy of this software and associated documentation 6*d289c2baSAndroid Build Coastguard Worker * files (the "Software"), to deal in the Software without 7*d289c2baSAndroid Build Coastguard Worker * restriction, including without limitation the rights to use, copy, 8*d289c2baSAndroid Build Coastguard Worker * modify, merge, publish, distribute, sublicense, and/or sell copies 9*d289c2baSAndroid Build Coastguard Worker * of the Software, and to permit persons to whom the Software is 10*d289c2baSAndroid Build Coastguard Worker * furnished to do so, subject to the following conditions: 11*d289c2baSAndroid Build Coastguard Worker * 12*d289c2baSAndroid Build Coastguard Worker * The above copyright notice and this permission notice shall be 13*d289c2baSAndroid Build Coastguard Worker * included in all copies or substantial portions of the Software. 14*d289c2baSAndroid Build Coastguard Worker * 15*d289c2baSAndroid Build Coastguard Worker * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16*d289c2baSAndroid Build Coastguard Worker * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17*d289c2baSAndroid Build Coastguard Worker * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18*d289c2baSAndroid Build Coastguard Worker * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19*d289c2baSAndroid Build Coastguard Worker * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20*d289c2baSAndroid Build Coastguard Worker * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21*d289c2baSAndroid Build Coastguard Worker * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22*d289c2baSAndroid Build Coastguard Worker * SOFTWARE. 23*d289c2baSAndroid Build Coastguard Worker */ 24*d289c2baSAndroid Build Coastguard Worker 25*d289c2baSAndroid Build Coastguard Worker #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION) 26*d289c2baSAndroid Build Coastguard Worker #error "Never include this file directly, include libavb.h instead." 27*d289c2baSAndroid Build Coastguard Worker #endif 28*d289c2baSAndroid Build Coastguard Worker 29*d289c2baSAndroid Build Coastguard Worker #ifndef AVB_DESCRIPTOR_H_ 30*d289c2baSAndroid Build Coastguard Worker #define AVB_DESCRIPTOR_H_ 31*d289c2baSAndroid Build Coastguard Worker 32*d289c2baSAndroid Build Coastguard Worker #include "avb_sysdeps.h" 33*d289c2baSAndroid Build Coastguard Worker 34*d289c2baSAndroid Build Coastguard Worker #ifdef __cplusplus 35*d289c2baSAndroid Build Coastguard Worker extern "C" { 36*d289c2baSAndroid Build Coastguard Worker #endif 37*d289c2baSAndroid Build Coastguard Worker 38*d289c2baSAndroid Build Coastguard Worker /* Well-known descriptor tags. 39*d289c2baSAndroid Build Coastguard Worker * 40*d289c2baSAndroid Build Coastguard Worker * AVB_DESCRIPTOR_TAG_PROPERTY: see |AvbPropertyDescriptor| struct. 41*d289c2baSAndroid Build Coastguard Worker * AVB_DESCRIPTOR_TAG_HASHTREE: see |AvbHashtreeDescriptor| struct. 42*d289c2baSAndroid Build Coastguard Worker * AVB_DESCRIPTOR_TAG_HASH: see |AvbHashDescriptor| struct. 43*d289c2baSAndroid Build Coastguard Worker * AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE: see |AvbKernelCmdlineDescriptor| struct. 44*d289c2baSAndroid Build Coastguard Worker * AVB_DESCRIPTOR_TAG_CHAIN_PARTITION: see |AvbChainPartitionDescriptor| struct. 45*d289c2baSAndroid Build Coastguard Worker */ 46*d289c2baSAndroid Build Coastguard Worker typedef enum { 47*d289c2baSAndroid Build Coastguard Worker AVB_DESCRIPTOR_TAG_PROPERTY, 48*d289c2baSAndroid Build Coastguard Worker AVB_DESCRIPTOR_TAG_HASHTREE, 49*d289c2baSAndroid Build Coastguard Worker AVB_DESCRIPTOR_TAG_HASH, 50*d289c2baSAndroid Build Coastguard Worker AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE, 51*d289c2baSAndroid Build Coastguard Worker AVB_DESCRIPTOR_TAG_CHAIN_PARTITION, 52*d289c2baSAndroid Build Coastguard Worker } AvbDescriptorTag; 53*d289c2baSAndroid Build Coastguard Worker 54*d289c2baSAndroid Build Coastguard Worker /* The header for a serialized descriptor. 55*d289c2baSAndroid Build Coastguard Worker * 56*d289c2baSAndroid Build Coastguard Worker * A descriptor always have two fields, a |tag| (denoting its type, 57*d289c2baSAndroid Build Coastguard Worker * see the |AvbDescriptorTag| enumeration) and the size of the bytes 58*d289c2baSAndroid Build Coastguard Worker * following, |num_bytes_following|. 59*d289c2baSAndroid Build Coastguard Worker * 60*d289c2baSAndroid Build Coastguard Worker * For padding, |num_bytes_following| is always a multiple of 8. 61*d289c2baSAndroid Build Coastguard Worker */ 62*d289c2baSAndroid Build Coastguard Worker typedef struct AvbDescriptor { 63*d289c2baSAndroid Build Coastguard Worker uint64_t tag; 64*d289c2baSAndroid Build Coastguard Worker uint64_t num_bytes_following; 65*d289c2baSAndroid Build Coastguard Worker } AVB_ATTR_PACKED AvbDescriptor; 66*d289c2baSAndroid Build Coastguard Worker 67*d289c2baSAndroid Build Coastguard Worker /* Copies |src| to |dest| and validates, byte-swapping fields in the 68*d289c2baSAndroid Build Coastguard Worker * process if needed. Returns true if valid, false if invalid. 69*d289c2baSAndroid Build Coastguard Worker * 70*d289c2baSAndroid Build Coastguard Worker * Data following the struct is not validated nor copied. 71*d289c2baSAndroid Build Coastguard Worker */ 72*d289c2baSAndroid Build Coastguard Worker bool avb_descriptor_validate_and_byteswap( 73*d289c2baSAndroid Build Coastguard Worker const AvbDescriptor* src, AvbDescriptor* dest) AVB_ATTR_WARN_UNUSED_RESULT; 74*d289c2baSAndroid Build Coastguard Worker 75*d289c2baSAndroid Build Coastguard Worker /* Signature for callback function used in avb_descriptor_foreach(). 76*d289c2baSAndroid Build Coastguard Worker * The passed in descriptor is given by |descriptor| and the 77*d289c2baSAndroid Build Coastguard Worker * |user_data| passed to avb_descriptor_foreach() function is in 78*d289c2baSAndroid Build Coastguard Worker * |user_data|. Return true to continue iterating, false to stop 79*d289c2baSAndroid Build Coastguard Worker * iterating. 80*d289c2baSAndroid Build Coastguard Worker * 81*d289c2baSAndroid Build Coastguard Worker * Note that |descriptor| points into the image passed to 82*d289c2baSAndroid Build Coastguard Worker * avb_descriptor_foreach() - all fields need to be byteswapped! 83*d289c2baSAndroid Build Coastguard Worker */ 84*d289c2baSAndroid Build Coastguard Worker typedef bool AvbDescriptorForeachFunc(const AvbDescriptor* descriptor, 85*d289c2baSAndroid Build Coastguard Worker void* user_data); 86*d289c2baSAndroid Build Coastguard Worker 87*d289c2baSAndroid Build Coastguard Worker /* Convenience function to iterate over all descriptors in an vbmeta 88*d289c2baSAndroid Build Coastguard Worker * image. 89*d289c2baSAndroid Build Coastguard Worker * 90*d289c2baSAndroid Build Coastguard Worker * The function given by |foreach_func| will be called for each 91*d289c2baSAndroid Build Coastguard Worker * descriptor. The given function should return true to continue 92*d289c2baSAndroid Build Coastguard Worker * iterating, false to stop. 93*d289c2baSAndroid Build Coastguard Worker * 94*d289c2baSAndroid Build Coastguard Worker * The |user_data| parameter will be passed to |foreach_func|. 95*d289c2baSAndroid Build Coastguard Worker * 96*d289c2baSAndroid Build Coastguard Worker * Returns false if the iteration was short-circuited, that is if 97*d289c2baSAndroid Build Coastguard Worker * an invocation of |foreach_func| returned false. 98*d289c2baSAndroid Build Coastguard Worker * 99*d289c2baSAndroid Build Coastguard Worker * Before using this function, you MUST verify |image_data| with 100*d289c2baSAndroid Build Coastguard Worker * avb_vbmeta_image_verify() and reject it unless it's signed by a known 101*d289c2baSAndroid Build Coastguard Worker * good public key. Additionally, |image_data| must be word-aligned. 102*d289c2baSAndroid Build Coastguard Worker */ 103*d289c2baSAndroid Build Coastguard Worker bool avb_descriptor_foreach(const uint8_t* image_data, 104*d289c2baSAndroid Build Coastguard Worker size_t image_size, 105*d289c2baSAndroid Build Coastguard Worker AvbDescriptorForeachFunc foreach_func, 106*d289c2baSAndroid Build Coastguard Worker void* user_data); 107*d289c2baSAndroid Build Coastguard Worker 108*d289c2baSAndroid Build Coastguard Worker /* Gets all descriptors in a vbmeta image. 109*d289c2baSAndroid Build Coastguard Worker * 110*d289c2baSAndroid Build Coastguard Worker * The return value is a NULL-pointer terminated array of 111*d289c2baSAndroid Build Coastguard Worker * AvbDescriptor pointers. Free with avb_free() when you are done with 112*d289c2baSAndroid Build Coastguard Worker * it. If |out_num_descriptors| is non-NULL, the number of descriptors 113*d289c2baSAndroid Build Coastguard Worker * will be returned there. 114*d289c2baSAndroid Build Coastguard Worker * 115*d289c2baSAndroid Build Coastguard Worker * Note that each AvbDescriptor pointer in the array points into 116*d289c2baSAndroid Build Coastguard Worker * |image_data| - all fields need to be byteswapped! 117*d289c2baSAndroid Build Coastguard Worker * 118*d289c2baSAndroid Build Coastguard Worker * Before using this function, you MUST verify |image_data| with 119*d289c2baSAndroid Build Coastguard Worker * avb_vbmeta_image_verify() and reject it unless it's signed by a known 120*d289c2baSAndroid Build Coastguard Worker * good public key. Additionally, |image_data| must be word-aligned. 121*d289c2baSAndroid Build Coastguard Worker */ 122*d289c2baSAndroid Build Coastguard Worker const AvbDescriptor** avb_descriptor_get_all(const uint8_t* image_data, 123*d289c2baSAndroid Build Coastguard Worker size_t image_size, 124*d289c2baSAndroid Build Coastguard Worker size_t* out_num_descriptors) 125*d289c2baSAndroid Build Coastguard Worker AVB_ATTR_WARN_UNUSED_RESULT; 126*d289c2baSAndroid Build Coastguard Worker 127*d289c2baSAndroid Build Coastguard Worker #ifdef __cplusplus 128*d289c2baSAndroid Build Coastguard Worker } 129*d289c2baSAndroid Build Coastguard Worker #endif 130*d289c2baSAndroid Build Coastguard Worker 131*d289c2baSAndroid Build Coastguard Worker #endif /* AVB_DESCRIPTOR_H_ */ 132