1 // SPDX-License-Identifier: MIT
3 * Copyright (C) 2016 The Android Open Source Project
6 #include "avb_property_descriptor.h"
9 bool avb_property_descriptor_validate_and_byteswap(
10 const AvbPropertyDescriptor* src, AvbPropertyDescriptor* dest) {
11 uint64_t expected_size;
13 avb_memcpy(dest, src, sizeof(AvbPropertyDescriptor));
15 if (!avb_descriptor_validate_and_byteswap((const AvbDescriptor*)src,
16 (AvbDescriptor*)dest))
19 if (dest->parent_descriptor.tag != AVB_DESCRIPTOR_TAG_PROPERTY) {
20 avb_error("Invalid tag for property descriptor.\n");
24 dest->key_num_bytes = avb_be64toh(dest->key_num_bytes);
25 dest->value_num_bytes = avb_be64toh(dest->value_num_bytes);
27 /* Check that key and value are fully contained. */
28 expected_size = sizeof(AvbPropertyDescriptor) - sizeof(AvbDescriptor) + 2;
29 if (!avb_safe_add_to(&expected_size, dest->key_num_bytes) ||
30 !avb_safe_add_to(&expected_size, dest->value_num_bytes)) {
31 avb_error("Overflow while adding up sizes.\n");
34 if (expected_size > dest->parent_descriptor.num_bytes_following) {
35 avb_error("Descriptor payload size overflow.\n");
45 const char* ret_value;
46 size_t ret_value_size;
47 } PropertyIteratorData;
49 static bool property_lookup_desc_foreach(const AvbDescriptor* header,
51 PropertyIteratorData* data = (PropertyIteratorData*)user_data;
52 AvbPropertyDescriptor prop_desc;
56 if (header->tag != AVB_DESCRIPTOR_TAG_PROPERTY) {
60 if (!avb_property_descriptor_validate_and_byteswap(
61 (const AvbPropertyDescriptor*)header, &prop_desc)) {
65 p = (const uint8_t*)header;
66 if (p[sizeof(AvbPropertyDescriptor) + prop_desc.key_num_bytes] != 0) {
67 avb_error("No terminating NUL byte in key.\n");
71 if (data->key_size == prop_desc.key_num_bytes) {
72 if (avb_memcmp(p + sizeof(AvbPropertyDescriptor),
74 data->key_size) == 0) {
75 data->ret_value = (const char*)(p + sizeof(AvbPropertyDescriptor) +
76 prop_desc.key_num_bytes + 1);
77 data->ret_value_size = prop_desc.value_num_bytes;
88 const char* avb_property_lookup(const uint8_t* image_data,
92 size_t* out_value_size) {
93 PropertyIteratorData data;
96 key_size = avb_strlen(key);
100 data.key_size = key_size;
102 if (avb_descriptor_foreach(
103 image_data, image_size, property_lookup_desc_foreach, &data) == 0) {
104 if (out_value_size != NULL) {
105 *out_value_size = data.ret_value_size;
107 return data.ret_value;
110 if (out_value_size != NULL) {
116 bool avb_property_lookup_uint64(const uint8_t* image_data,
120 uint64_t* out_value) {
127 value = avb_property_lookup(image_data, image_size, key, key_size, NULL);
133 if (avb_memcmp(value, "0x", 2) == 0) {
139 for (n = 0; value[n] != '\0'; n++) {
145 if (c >= '0' && c <= '9') {
147 } else if (base == 16 && c >= 'a' && c <= 'f') {
148 digit = c - 'a' + 10;
149 } else if (base == 16 && c >= 'A' && c <= 'F') {
150 digit = c - 'A' + 10;
152 avb_error("Invalid digit.\n");
160 if (out_value != NULL) {
161 *out_value = parsed_val;