avb_property_descriptor.h revision 21e95266704e572ced1c633bbc4aea9f42afa0a5
1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION) 18#error "Never include this file directly, include libavb.h instead." 19#endif 20 21#ifndef AVB_PROPERTY_DESCRIPTOR_H_ 22#define AVB_PROPERTY_DESCRIPTOR_H_ 23 24#include "avb_descriptor.h" 25 26#ifdef __cplusplus 27extern "C" { 28#endif 29 30/* A descriptor for properties (free-form key/value pairs). 31 * 32 * Following this struct are |key_num_bytes| bytes of key data, 33 * followed by a NUL byte, then |value_num_bytes| bytes of value data, 34 * followed by a NUL byte and then enough padding to make the combined 35 * size a multiple of 8. 36 */ 37typedef struct AvbPropertyDescriptor { 38 AvbDescriptor parent_descriptor; 39 uint64_t key_num_bytes; 40 uint64_t value_num_bytes; 41} AVB_ATTR_PACKED AvbPropertyDescriptor; 42 43/* Copies |src| to |dest| and validates, byte-swapping fields in the 44 * process if needed. Returns true if valid, false if invalid. 45 * 46 * Data following the struct is not validated nor copied. 47 */ 48bool avb_property_descriptor_validate_and_byteswap( 49 const AvbPropertyDescriptor* src, 50 AvbPropertyDescriptor* dest) AVB_ATTR_WARN_UNUSED_RESULT; 51 52/* Convenience function for looking up the value for a property with 53 * name |key| in a vbmeta image. If |key_size| is 0, |key| must be 54 * NUL-terminated. 55 * 56 * The |image_data| parameter must be a pointer to a vbmeta image of 57 * size |image_size|. 58 * 59 * This function returns a pointer to the value inside the passed-in 60 * image or NULL if not found. Note that the value is always 61 * guaranteed to be followed by a NUL byte. 62 * 63 * If the value was found and |out_value_size| is not NULL, the size 64 * of the value is returned there. 65 * 66 * This function is O(n) in number of descriptors so if you need to 67 * look up a lot of values, you may want to build a more efficient 68 * lookup-table by manually walking all descriptors using 69 * avb_descriptor_foreach(). 70 * 71 * Before using this function, you MUST verify |image_data| with 72 * avb_vbmeta_image_verify() and reject it unless it's signed by a 73 * known good public key. 74 */ 75const char* avb_property_lookup( 76 const uint8_t* image_data, size_t image_size, const char* key, 77 size_t key_size, size_t* out_value_size) AVB_ATTR_WARN_UNUSED_RESULT; 78 79/* Like avb_property_lookup() but parses the intial portions of the 80 * value as an unsigned 64-bit integer. Both decimal and hexadecimal 81 * representations (e.g. "0x2a") are supported. Returns false on 82 * failure and true on success. On success, the parsed value is 83 * returned in |out_value|. 84 */ 85bool avb_property_lookup_uint64( 86 const uint8_t* image_data, size_t image_size, const char* key, 87 size_t key_size, uint64_t* out_value) AVB_ATTR_WARN_UNUSED_RESULT; 88 89#ifdef __cplusplus 90} 91#endif 92 93#endif /* AVB_PROPERTY_DESCRIPTOR_H_ */ 94