blob: 2c3cf7486c15a73599945bfd25db73f1b5851f34 [file] [log] [blame]
Alex Elderb09c94a2014-10-01 21:54:16 -05001/*
2 * Greybus module manifest parsing
3 *
4 * Copyright 2014 Google Inc.
5 *
6 * Released under the GPLv2 only.
7 */
8
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11#include <linux/err.h>
12
13#include "greybus.h"
14
15/*
16 * We scan the manifest once to identify where all the descriptors
17 * are. The result is a list of these manifest_desc structures. We
18 * then pick through them for what we're looking for (starting with
19 * the module descriptor). As each is processed we remove it from
20 * the list. When we're done the list should (probably) be empty.
21 */
22struct manifest_desc {
23 struct list_head links;
24
25 size_t size;
26 void *data;
27 enum greybus_descriptor_type type;
28};
29
30static LIST_HEAD(manifest_descs);
31
32static void release_manifest_descriptor(struct manifest_desc *descriptor)
33{
34 list_del(&descriptor->links);
35 kfree(descriptor);
36}
37
38static void release_manifest_descriptors(void)
39{
40 struct manifest_desc *descriptor;
41 struct manifest_desc *next;
42
43 list_for_each_entry_safe(descriptor, next, &manifest_descs, links)
44 release_manifest_descriptor(descriptor);
45}
46
47/*
48 * Validate the given descriptor. Its reported size must fit within
49 * the number of bytes reamining, and it must have a recognized
50 * type. Check that the reported size is at least as big as what
51 * we expect to see. (It could be bigger, perhaps for a new version
52 * of the format.)
53 *
54 * Returns the number of bytes consumed by the descriptor, or a
55 * negative errno.
56 */
57static int identify_descriptor(struct greybus_descriptor *desc, size_t size)
58{
59 struct greybus_descriptor_header *desc_header = &desc->header;
60 struct manifest_desc *descriptor;
61 int desc_size;
62 size_t expected_size;
63
64 if (size < sizeof(*desc_header)) {
65 pr_err("manifest too small\n");
66 return -EINVAL; /* Must at least have header */
67 }
68
69 desc_size = (int)le16_to_cpu(desc_header->size);
70 if ((size_t)desc_size > size) {
71 pr_err("descriptor too big\n");
72 return -EINVAL;
73 }
74
75 switch (desc_header->type) {
76 case GREYBUS_TYPE_MODULE:
77 if (desc_size < sizeof(struct greybus_descriptor_module)) {
78 pr_err("module descriptor too small (%u)\n",
79 desc_size);
80 return -EINVAL;
81 }
82 break;
83 case GREYBUS_TYPE_DEVICE:
84 break;
85 case GREYBUS_TYPE_CLASS:
86 pr_err("class descriptor found (ignoring)\n");
87 break;
88 case GREYBUS_TYPE_STRING:
89 expected_size = sizeof(struct greybus_descriptor_header);
90 expected_size += sizeof(struct greybus_descriptor_string);
91 expected_size += (size_t)desc->string.length;
92 if (desc_size < expected_size) {
93 pr_err("string descriptor too small (%u)\n",
94 desc_size);
95 return -EINVAL;
96 }
97 break;
98 case GREYBUS_TYPE_CPORT:
99 if (desc_size < sizeof(struct greybus_descriptor_cport)) {
100 pr_err("cport descriptor too small (%u)\n",
101 desc_size);
102 return -EINVAL;
103 }
104 break;
105 case GREYBUS_TYPE_INVALID:
106 default:
107 pr_err("invalid descriptor type (%hhu)\n", desc_header->type);
108 return -EINVAL;
109 }
110
111 descriptor = kzalloc(sizeof(*descriptor), GFP_KERNEL);
112 if (!descriptor)
113 return -ENOMEM;
114
115 descriptor->size = desc_size;
116 descriptor->data = desc;
117 descriptor->type = desc_header->type;
118 list_add_tail(&descriptor->links, &manifest_descs);
119
120 return desc_size;
121}
122
123/*
124 * Find the string descriptor having the given id, validate it, and
125 * allocate a duplicate copy of it. The duplicate has an extra byte
126 * which guarantees the returned string is NUL-terminated.
127 *
128 * String index 0 is valid (it represents "no string"), and for
129 * that a null pointer is returned.
130 *
131 * Otherwise returns a pointer to a newly-allocated copy of the
132 * descriptor string, or an error-coded pointer on failure.
133 */
134static char *gb_string_get(u8 string_id)
135{
136 struct greybus_descriptor_string *desc_string;
137 struct manifest_desc *descriptor;
138 bool found = false;
139 char *string;
140
141 /* A zero string id means no string (but no error) */
142 if (!string_id)
143 return NULL;
144
145 list_for_each_entry(descriptor, &manifest_descs, links) {
146 struct greybus_descriptor *desc;
147
148 if (descriptor->type != GREYBUS_TYPE_STRING)
149 continue;
150
151 desc = descriptor->data;
152 desc_string = &desc->string;
153 if (desc_string->id == string_id) {
154 found = true;
155 break;
156 }
157 }
158 if (!found)
159 return ERR_PTR(-ENOENT);
160
161 /* Allocate an extra byte so we can guarantee it's NUL-terminated */
162 string = kmemdup(&desc_string->string, (size_t)desc_string->length + 1,
163 GFP_KERNEL);
164 if (!string)
165 return ERR_PTR(-ENOMEM);
166 string[desc_string->length] = '\0';
167
168 /* Ok we've used this string, so we're done with it */
169 release_manifest_descriptor(descriptor);
170
171 return string;
172}
173
Alex Elderd88bfb52014-10-01 21:54:17 -0500174/*
175 * Find interface descriptors in the manifest and set up their data
176 * structures. Returns the number of interfaces set up for the
177 * given module.
178 */
179static u32 gb_manifest_parse_interfaces(struct gb_module *gmod)
180{
181 u32 count = 0;
182
183 while (true) {
184 struct manifest_desc *descriptor;
185 struct greybus_descriptor_interface *desc_interface;
186 struct gb_interface *interface;
187 bool found = false;
188
189 /* Find an interface descriptor */
190 list_for_each_entry(descriptor, &manifest_descs, links) {
191 if (descriptor->type == GREYBUS_TYPE_DEVICE) {
192 found = true;
193 break;
194 }
195 }
196 if (!found)
197 break;
198
199 /* Found one. Set up its interface structure*/
200 desc_interface = descriptor->data;
201 interface = gb_interface_create(gmod, desc_interface->id);
202 if (!interface)
203 return 0; /* Error */
204 count++;
205
206 /* Done with this interface descriptor */
207 release_manifest_descriptor(descriptor);
208 }
209
210 return count;
211}
212
Alex Elderb09c94a2014-10-01 21:54:16 -0500213struct gb_module *gb_manifest_parse_module(struct manifest_desc *module_desc)
214{
215 struct greybus_descriptor *desc = module_desc->data;
216 struct greybus_descriptor_module *desc_module = &desc->module;
217 struct gb_module *gmod;
218
219 gmod = kzalloc(sizeof(*gmod), GFP_KERNEL);
220 if (!gmod)
221 return NULL;
222
223 /* Handle the strings first--they can fail */
224 gmod->vendor_string = gb_string_get(desc_module->vendor_stringid);
225 if (IS_ERR(gmod->vendor_string)) {
226 kfree(gmod);
227 return NULL;
228 }
229 gmod->product_string = gb_string_get(desc_module->product_stringid);
230 if (IS_ERR(gmod->product_string)) {
231 kfree(gmod->vendor_string);
232 kfree(gmod);
233 return NULL;
234 }
235
236 gmod->vendor = le16_to_cpu(desc_module->vendor);
237 gmod->product = le16_to_cpu(desc_module->product);
238 gmod->version = le16_to_cpu(desc_module->version);
239 gmod->serial_number = le64_to_cpu(desc_module->serial_number);
240
241 /* Release the module descriptor, now that we're done with it */
242 release_manifest_descriptor(module_desc);
243
Alex Elderd88bfb52014-10-01 21:54:17 -0500244 /* A module must have at least one interface descriptor */
245 if (!gb_manifest_parse_interfaces(gmod)) {
246 pr_err("manifest interface descriptors not valid\n");
247 gb_module_destroy(gmod);
248 return NULL;
249 }
250
Alex Elderb09c94a2014-10-01 21:54:16 -0500251 return gmod;
252}
253
254/*
255 * Parse a buffer containing a module manifest.
256 *
257 * If we find anything wrong with the content/format of the buffer
258 * we reject it.
259 *
260 * The first requirement is that the manifest's version is
261 * one we can parse.
262 *
263 * We make an initial pass through the buffer and identify all of
264 * the descriptors it contains, keeping track for each its type
265 * and the location size of its data in the buffer.
266 *
267 * Next we scan the descriptors, looking for a module descriptor;
268 * there must be exactly one of those. When found, we record the
269 * information it contains, and then remove that descriptor (and any
270 * string descriptors it refers to) from further consideration.
271 *
272 * After that we look for the module's interfaces--there must be at
273 * least one of those.
274 *
275 * Return a pointer to an initialized gb_module structure
276 * representing the content of the module manifest, or a null
277 * pointer if an error occurs.
278 */
279struct gb_module *gb_manifest_parse(void *data, size_t size)
280{
281 struct greybus_manifest *manifest;
282 struct greybus_manifest_header *header;
283 struct greybus_descriptor *desc;
284 struct manifest_desc *descriptor;
285 struct manifest_desc *module_desc = NULL;
286 struct gb_module *gmod;
287 u16 manifest_size;
288 u32 found = 0;
289
290 /* we have to have at _least_ the manifest header */
291 if (size <= sizeof(manifest->header)) {
292 pr_err("short manifest (%zu)\n", size);
293 return NULL;
294 }
295
296 /* Make sure the size is right */
297 manifest = data;
298 header = &manifest->header;
299 manifest_size = le16_to_cpu(header->size);
300 if (manifest_size != size) {
301 pr_err("manifest size mismatch %zu != %hu\n",
302 size, manifest_size);
303 return NULL;
304 }
305
306 /* Validate major/minor number */
307 if (header->version_major > GREYBUS_VERSION_MAJOR) {
308 pr_err("manifest version too new (%hhu.%hhu > %hhu.%hhu)\n",
309 header->version_major, header->version_minor,
310 GREYBUS_VERSION_MAJOR, GREYBUS_VERSION_MINOR);
311 return NULL;
312 }
313
314 /* OK, find all the descriptors */
315 desc = (struct greybus_descriptor *)(header + 1);
316 size -= sizeof(*header);
317 while (size) {
318 int desc_size;
319
320 desc_size = identify_descriptor(desc, size);
321 if (desc_size <= 0) {
322 if (!desc_size)
323 pr_err("zero-sized manifest descriptor\n");
324 goto out_err;
325 }
326 desc = (struct greybus_descriptor *)((char *)desc + desc_size);
327 size -= desc_size;
328 }
329
330 /* There must be a single module descriptor */
331 list_for_each_entry(descriptor, &manifest_descs, links) {
332 if (descriptor->type == GREYBUS_TYPE_MODULE)
333 if (!found++)
334 module_desc = descriptor;
335 }
336 if (found != 1) {
337 pr_err("manifest must have 1 module descriptor (%u found)\n",
338 found);
339 goto out_err;
340 }
341
342 /* Parse the module manifest, starting with the module descriptor */
343 gmod = gb_manifest_parse_module(module_desc);
344
345 /*
346 * We really should have no remaining descriptors, but we
347 * don't know what newer format manifests might leave.
348 */
349 if (!list_empty(&manifest_descs)) {
350 pr_info("excess descriptors in module manifest\n");
351 release_manifest_descriptors();
352 }
353
354 return gmod;
355out_err:
356 release_manifest_descriptors();
357
358 return NULL;
359}