David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
David Zeuthen | c612e2e | 2016-09-16 16:44:08 -0400 | [diff] [blame^] | 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: |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 11 | * |
David Zeuthen | c612e2e | 2016-09-16 16:44:08 -0400 | [diff] [blame^] | 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 14 | * |
David Zeuthen | c612e2e | 2016-09-16 16:44:08 -0400 | [diff] [blame^] | 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. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION) |
| 26 | #error "Never include this file directly, include libavb.h instead." |
| 27 | #endif |
| 28 | |
| 29 | #ifndef AVB_PROPERTY_DESCRIPTOR_H_ |
| 30 | #define AVB_PROPERTY_DESCRIPTOR_H_ |
| 31 | |
| 32 | #include "avb_descriptor.h" |
| 33 | |
| 34 | #ifdef __cplusplus |
| 35 | extern "C" { |
| 36 | #endif |
| 37 | |
| 38 | /* A descriptor for properties (free-form key/value pairs). |
| 39 | * |
| 40 | * Following this struct are |key_num_bytes| bytes of key data, |
| 41 | * followed by a NUL byte, then |value_num_bytes| bytes of value data, |
| 42 | * followed by a NUL byte and then enough padding to make the combined |
| 43 | * size a multiple of 8. |
| 44 | */ |
| 45 | typedef struct AvbPropertyDescriptor { |
| 46 | AvbDescriptor parent_descriptor; |
| 47 | uint64_t key_num_bytes; |
| 48 | uint64_t value_num_bytes; |
| 49 | } AVB_ATTR_PACKED AvbPropertyDescriptor; |
| 50 | |
| 51 | /* Copies |src| to |dest| and validates, byte-swapping fields in the |
| 52 | * process if needed. Returns true if valid, false if invalid. |
| 53 | * |
| 54 | * Data following the struct is not validated nor copied. |
| 55 | */ |
| 56 | bool avb_property_descriptor_validate_and_byteswap( |
| 57 | const AvbPropertyDescriptor* src, |
| 58 | AvbPropertyDescriptor* dest) AVB_ATTR_WARN_UNUSED_RESULT; |
| 59 | |
| 60 | /* Convenience function for looking up the value for a property with |
| 61 | * name |key| in a vbmeta image. If |key_size| is 0, |key| must be |
| 62 | * NUL-terminated. |
| 63 | * |
| 64 | * The |image_data| parameter must be a pointer to a vbmeta image of |
| 65 | * size |image_size|. |
| 66 | * |
| 67 | * This function returns a pointer to the value inside the passed-in |
| 68 | * image or NULL if not found. Note that the value is always |
| 69 | * guaranteed to be followed by a NUL byte. |
| 70 | * |
| 71 | * If the value was found and |out_value_size| is not NULL, the size |
| 72 | * of the value is returned there. |
| 73 | * |
| 74 | * This function is O(n) in number of descriptors so if you need to |
| 75 | * look up a lot of values, you may want to build a more efficient |
| 76 | * lookup-table by manually walking all descriptors using |
| 77 | * avb_descriptor_foreach(). |
| 78 | * |
| 79 | * Before using this function, you MUST verify |image_data| with |
| 80 | * avb_vbmeta_image_verify() and reject it unless it's signed by a |
| 81 | * known good public key. |
| 82 | */ |
| 83 | const char* avb_property_lookup( |
| 84 | const uint8_t* image_data, size_t image_size, const char* key, |
| 85 | size_t key_size, size_t* out_value_size) AVB_ATTR_WARN_UNUSED_RESULT; |
| 86 | |
| 87 | /* Like avb_property_lookup() but parses the intial portions of the |
| 88 | * value as an unsigned 64-bit integer. Both decimal and hexadecimal |
| 89 | * representations (e.g. "0x2a") are supported. Returns false on |
| 90 | * failure and true on success. On success, the parsed value is |
| 91 | * returned in |out_value|. |
| 92 | */ |
| 93 | bool avb_property_lookup_uint64( |
| 94 | const uint8_t* image_data, size_t image_size, const char* key, |
| 95 | size_t key_size, uint64_t* out_value) AVB_ATTR_WARN_UNUSED_RESULT; |
| 96 | |
| 97 | #ifdef __cplusplus |
| 98 | } |
| 99 | #endif |
| 100 | |
| 101 | #endif /* AVB_PROPERTY_DESCRIPTOR_H_ */ |