1 // SPDX-License-Identifier: MIT
3 * Copyright (C) 2016 The Android Open Source Project
6 #include "avb_descriptor.h"
8 #include "avb_vbmeta_image.h"
11 bool avb_descriptor_validate_and_byteswap(const AvbDescriptor* src,
12 AvbDescriptor* dest) {
13 dest->tag = avb_be64toh(src->tag);
14 dest->num_bytes_following = avb_be64toh(src->num_bytes_following);
16 if ((dest->num_bytes_following & 0x07) != 0) {
17 avb_error("Descriptor size is not divisible by 8.\n");
23 bool avb_descriptor_foreach(const uint8_t* image_data,
25 AvbDescriptorForeachFunc foreach_func,
27 const AvbVBMetaImageHeader* header = NULL;
29 const uint8_t* image_end;
30 const uint8_t* desc_start;
31 const uint8_t* desc_end;
34 if (image_data == NULL) {
35 avb_error("image_data is NULL\n.");
39 if (foreach_func == NULL) {
40 avb_error("foreach_func is NULL\n.");
44 if (image_size < sizeof(AvbVBMetaImageHeader)) {
45 avb_error("Length is smaller than header.\n");
49 /* Ensure magic is correct. */
50 if (avb_memcmp(image_data, AVB_MAGIC, AVB_MAGIC_LEN) != 0) {
51 avb_error("Magic is incorrect.\n");
55 /* Careful, not byteswapped - also ensure it's aligned properly. */
56 avb_assert_aligned(image_data);
57 header = (const AvbVBMetaImageHeader*)image_data;
58 image_end = image_data + image_size;
60 desc_start = image_data + sizeof(AvbVBMetaImageHeader) +
61 avb_be64toh(header->authentication_data_block_size) +
62 avb_be64toh(header->descriptors_offset);
64 desc_end = desc_start + avb_be64toh(header->descriptors_size);
66 if (desc_start < image_data || desc_start > image_end ||
67 desc_end < image_data || desc_end > image_end || desc_end < desc_start) {
68 avb_error("Descriptors not inside passed-in data.\n");
72 for (p = desc_start; p < desc_end;) {
73 const AvbDescriptor* dh = (const AvbDescriptor*)p;
74 avb_assert_aligned(dh);
75 uint64_t nb_following = avb_be64toh(dh->num_bytes_following);
76 uint64_t nb_total = 0;
77 if (!avb_safe_add(&nb_total, sizeof(AvbDescriptor), nb_following)) {
78 avb_error("Invalid descriptor length.\n");
82 if ((nb_total & 7) != 0) {
83 avb_error("Invalid descriptor length.\n");
87 if (nb_total + p < desc_start || nb_total + p > desc_end) {
88 avb_error("Invalid data in descriptors array.\n");
92 if (foreach_func(dh, user_data) == 0) {
96 if (!avb_safe_add_to((uint64_t*)(&p), nb_total)) {
97 avb_error("Invalid descriptor length.\n");
108 static bool count_descriptors(const AvbDescriptor* descriptor,
110 size_t* num_descriptors = user_data;
111 *num_descriptors += 1;
116 size_t descriptor_number;
117 const AvbDescriptor** descriptors;
120 static bool set_descriptors(const AvbDescriptor* descriptor, void* user_data) {
121 SetDescriptorData* data = user_data;
122 data->descriptors[data->descriptor_number++] = descriptor;
126 const AvbDescriptor** avb_descriptor_get_all(const uint8_t* image_data,
128 size_t* out_num_descriptors) {
129 size_t num_descriptors = 0;
130 SetDescriptorData data;
132 avb_descriptor_foreach(
133 image_data, image_size, count_descriptors, &num_descriptors);
135 data.descriptor_number = 0;
137 avb_calloc(sizeof(const AvbDescriptor*) * (num_descriptors + 1));
138 if (data.descriptors == NULL) {
141 avb_descriptor_foreach(image_data, image_size, set_descriptors, &data);
142 avb_assert(data.descriptor_number == num_descriptors);
144 if (out_num_descriptors != NULL) {
145 *out_num_descriptors = num_descriptors;
148 return data.descriptors;