blob: db465aae6e6abc760b7565b9d6d0c5904c12aed9 [file] [log] [blame]
David Zeuthen21e95262016-07-27 17:58:40 -04001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
David Zeuthenc612e2e2016-09-16 16:44:08 -04004 * 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 Zeuthen21e95262016-07-27 17:58:40 -040011 *
David Zeuthenc612e2e2016-09-16 16:44:08 -040012 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
David Zeuthen21e95262016-07-27 17:58:40 -040014 *
David Zeuthenc612e2e2016-09-16 16:44:08 -040015 * 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 Zeuthen21e95262016-07-27 17:58:40 -040023 */
24
25#include "avb_descriptor.h"
26#include "avb_util.h"
27#include "avb_vbmeta_image.h"
28
29bool avb_descriptor_validate_and_byteswap(const AvbDescriptor* src,
30 AvbDescriptor* dest) {
31 dest->tag = avb_be64toh(src->tag);
32 dest->num_bytes_following = avb_be64toh(src->num_bytes_following);
33
34 if ((dest->num_bytes_following & 0x07) != 0) {
35 avb_error("Descriptor size is not divisible by 8.\n");
36 return false;
37 }
38 return true;
39}
40
41bool avb_descriptor_foreach(const uint8_t* image_data, size_t image_size,
42 AvbDescriptorForeachFunc foreach_func,
43 void* user_data) {
44 const AvbVBMetaImageHeader* header = NULL;
45 bool ret = false;
46 const uint8_t* image_end;
47 const uint8_t* desc_start;
48 const uint8_t* desc_end;
49 const uint8_t* p;
50
51 if (image_data == NULL) {
52 avb_error("image_data is NULL\n.");
53 goto out;
54 }
55
56 if (foreach_func == NULL) {
57 avb_error("foreach_func is NULL\n.");
58 goto out;
59 }
60
61 if (image_size < sizeof(AvbVBMetaImageHeader)) {
62 avb_error("Length is smaller than header.\n");
63 goto out;
64 }
65
66 /* Ensure magic is correct. */
67 if (avb_memcmp(image_data, AVB_MAGIC, AVB_MAGIC_LEN) != 0) {
68 avb_error("Magic is incorrect.\n");
69 goto out;
70 }
71
72 /* Careful, not byteswapped - also ensure it's aligned properly. */
73 avb_assert_word_aligned(image_data);
74 header = (const AvbVBMetaImageHeader*)image_data;
75 image_end = image_data + image_size;
76
77 desc_start = image_data + sizeof(AvbVBMetaImageHeader) +
78 avb_be64toh(header->authentication_data_block_size) +
79 avb_be64toh(header->descriptors_offset);
80
81 desc_end = desc_start + avb_be64toh(header->descriptors_size);
82
83 if (desc_start < image_data || desc_start > image_end ||
84 desc_end < image_data || desc_end > image_end || desc_end < desc_start) {
85 avb_error("Descriptors not inside passed-in data.\n");
86 goto out;
87 }
88
89 for (p = desc_start; p < desc_end;) {
90 const AvbDescriptor* dh = (const AvbDescriptor*)p;
91 avb_assert_word_aligned(dh);
92 uint64_t nb_following = avb_be64toh(dh->num_bytes_following);
93 uint64_t nb_total = sizeof(AvbDescriptor) + nb_following;
94
95 if ((nb_total & 7) != 0) {
96 avb_error("Invalid descriptor length.\n");
97 goto out;
98 }
99
100 if (nb_total + p < desc_start || nb_total + p > desc_end) {
101 avb_error("Invalid data in descriptors array.\n");
102 goto out;
103 }
104
105 if (foreach_func(dh, user_data) == 0) {
106 goto out;
107 }
108
109 p += nb_total;
110 }
111
112 ret = true;
113
114out:
115 return ret;
116}
117
118static bool count_descriptors(const AvbDescriptor* descriptor,
119 void* user_data) {
120 size_t* num_descriptors = user_data;
121 *num_descriptors += 1;
122 return true;
123}
124
125typedef struct {
126 size_t descriptor_number;
127 const AvbDescriptor** descriptors;
128} SetDescriptorData;
129
130static bool set_descriptors(const AvbDescriptor* descriptor, void* user_data) {
131 SetDescriptorData* data = user_data;
132 data->descriptors[data->descriptor_number++] = descriptor;
133 return true;
134}
135
136const AvbDescriptor** avb_descriptor_get_all(const uint8_t* image_data,
137 size_t image_size,
138 size_t* out_num_descriptors) {
139 size_t num_descriptors = 0;
140 SetDescriptorData data;
141
142 avb_descriptor_foreach(image_data, image_size, count_descriptors,
143 &num_descriptors);
144
145 data.descriptor_number = 0;
146 data.descriptors =
147 avb_calloc(sizeof(const AvbDescriptor*) * (num_descriptors + 1));
148 if (data.descriptors == NULL) {
149 return NULL;
150 }
151 avb_descriptor_foreach(image_data, image_size, set_descriptors, &data);
152 avb_assert(data.descriptor_number == num_descriptors);
153
154 if (out_num_descriptors != NULL) {
155 *out_num_descriptors = num_descriptors;
156 }
157
158 return data.descriptors;
159}