blob: 6422293293c57347001ac15916871b83e36b2a50 [file] [log] [blame]
Igor Opaniukd8f9d2a2018-06-03 21:56:36 +03001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7#include "avb_descriptor.h"
8#include "avb_util.h"
9#include "avb_vbmeta_image.h"
10
11bool 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);
15
16 if ((dest->num_bytes_following & 0x07) != 0) {
17 avb_error("Descriptor size is not divisible by 8.\n");
18 return false;
19 }
20 return true;
21}
22
23bool avb_descriptor_foreach(const uint8_t* image_data,
24 size_t image_size,
25 AvbDescriptorForeachFunc foreach_func,
26 void* user_data) {
27 const AvbVBMetaImageHeader* header = NULL;
28 bool ret = false;
29 const uint8_t* image_end;
30 const uint8_t* desc_start;
31 const uint8_t* desc_end;
32 const uint8_t* p;
33
34 if (image_data == NULL) {
35 avb_error("image_data is NULL\n.");
36 goto out;
37 }
38
39 if (foreach_func == NULL) {
40 avb_error("foreach_func is NULL\n.");
41 goto out;
42 }
43
44 if (image_size < sizeof(AvbVBMetaImageHeader)) {
45 avb_error("Length is smaller than header.\n");
46 goto out;
47 }
48
49 /* Ensure magic is correct. */
50 if (avb_memcmp(image_data, AVB_MAGIC, AVB_MAGIC_LEN) != 0) {
51 avb_error("Magic is incorrect.\n");
52 goto out;
53 }
54
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;
59
60 desc_start = image_data + sizeof(AvbVBMetaImageHeader) +
61 avb_be64toh(header->authentication_data_block_size) +
62 avb_be64toh(header->descriptors_offset);
63
64 desc_end = desc_start + avb_be64toh(header->descriptors_size);
65
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");
69 goto out;
70 }
71
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 = sizeof(AvbDescriptor) + nb_following;
77
78 if ((nb_total & 7) != 0) {
79 avb_error("Invalid descriptor length.\n");
80 goto out;
81 }
82
83 if (nb_total + p < desc_start || nb_total + p > desc_end) {
84 avb_error("Invalid data in descriptors array.\n");
85 goto out;
86 }
87
88 if (foreach_func(dh, user_data) == 0) {
89 goto out;
90 }
91
92 p += nb_total;
93 }
94
95 ret = true;
96
97out:
98 return ret;
99}
100
101static bool count_descriptors(const AvbDescriptor* descriptor,
102 void* user_data) {
103 size_t* num_descriptors = user_data;
104 *num_descriptors += 1;
105 return true;
106}
107
108typedef struct {
109 size_t descriptor_number;
110 const AvbDescriptor** descriptors;
111} SetDescriptorData;
112
113static bool set_descriptors(const AvbDescriptor* descriptor, void* user_data) {
114 SetDescriptorData* data = user_data;
115 data->descriptors[data->descriptor_number++] = descriptor;
116 return true;
117}
118
119const AvbDescriptor** avb_descriptor_get_all(const uint8_t* image_data,
120 size_t image_size,
121 size_t* out_num_descriptors) {
122 size_t num_descriptors = 0;
123 SetDescriptorData data;
124
125 avb_descriptor_foreach(
126 image_data, image_size, count_descriptors, &num_descriptors);
127
128 data.descriptor_number = 0;
129 data.descriptors =
130 avb_calloc(sizeof(const AvbDescriptor*) * (num_descriptors + 1));
131 if (data.descriptors == NULL) {
132 return NULL;
133 }
134 avb_descriptor_foreach(image_data, image_size, set_descriptors, &data);
135 avb_assert(data.descriptor_number == num_descriptors);
136
137 if (out_num_descriptors != NULL) {
138 *out_num_descriptors = num_descriptors;
139 }
140
141 return data.descriptors;
142}