blob: ad7922aba32da2ad2f8d930dde90b932116bc65e [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;
Alex Elderb09c94a2014-10-01 21:54:16 -050083 case GREYBUS_TYPE_STRING:
84 expected_size = sizeof(struct greybus_descriptor_header);
85 expected_size += sizeof(struct greybus_descriptor_string);
86 expected_size += (size_t)desc->string.length;
87 if (desc_size < expected_size) {
88 pr_err("string descriptor too small (%u)\n",
89 desc_size);
90 return -EINVAL;
91 }
92 break;
Alex Elder63cc9322014-10-02 12:30:02 -050093 case GREYBUS_TYPE_INTERFACE:
94 break;
Alex Elderb09c94a2014-10-01 21:54:16 -050095 case GREYBUS_TYPE_CPORT:
96 if (desc_size < sizeof(struct greybus_descriptor_cport)) {
97 pr_err("cport descriptor too small (%u)\n",
98 desc_size);
99 return -EINVAL;
100 }
101 break;
Alex Elder63cc9322014-10-02 12:30:02 -0500102 case GREYBUS_TYPE_CLASS:
103 pr_warn("class descriptor found (ignoring)\n");
104 break;
Alex Elderb09c94a2014-10-01 21:54:16 -0500105 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/*
Alex Elderc095bbc2014-10-01 21:54:18 -0500175 * Find cport descriptors in the manifest and set up data structures
176 * for the functions that use them. Returns the number of interfaces
177 * set up for the given module, or 0 if there is an error.
178 */
Greg Kroah-Hartman2f30d9f2014-10-02 21:26:26 -0700179static u32 gb_manifest_parse_cports(struct gb_interface *interface)
Alex Elderc095bbc2014-10-01 21:54:18 -0500180{
181 u32 count = 0;
182
183 while (true) {
184 struct manifest_desc *descriptor;
185 struct greybus_descriptor_cport *desc_cport;
Alex Elder63cc9322014-10-02 12:30:02 -0500186 enum greybus_protocol protocol;
Alex Elderc095bbc2014-10-01 21:54:18 -0500187 u16 cport_id;
188 bool found;
189
190 /* Find a cport descriptor */
191 found = false;
192 list_for_each_entry(descriptor, &manifest_descs, links) {
193 if (descriptor->type == GREYBUS_TYPE_CPORT) {
Alex Elder63cc9322014-10-02 12:30:02 -0500194 desc_cport = descriptor->data;
195 if (desc_cport->interface == interface->id) {
196 found = true;
197 break;
198 }
Alex Elderc095bbc2014-10-01 21:54:18 -0500199 }
200 }
201 if (!found)
202 break;
203
204 /* Found one. Set up its function structure */
Alex Elder63cc9322014-10-02 12:30:02 -0500205 protocol = (enum greybus_protocol)desc_cport->protocol;
Alex Elderc095bbc2014-10-01 21:54:18 -0500206 cport_id = le16_to_cpu(desc_cport->id);
Alex Elderad1c4492014-10-02 12:30:06 -0500207 if (!gb_connection_create(interface, cport_id, protocol))
Alex Elderc095bbc2014-10-01 21:54:18 -0500208 return 0; /* Error */
209
210 count++;
211 /* Release the cport descriptor */
212 release_manifest_descriptor(descriptor);
213 }
214
215 return count;
216}
217
218/*
Alex Elderd88bfb52014-10-01 21:54:17 -0500219 * Find interface descriptors in the manifest and set up their data
220 * structures. Returns the number of interfaces set up for the
221 * given module.
222 */
223static u32 gb_manifest_parse_interfaces(struct gb_module *gmod)
224{
225 u32 count = 0;
226
227 while (true) {
228 struct manifest_desc *descriptor;
229 struct greybus_descriptor_interface *desc_interface;
230 struct gb_interface *interface;
231 bool found = false;
232
233 /* Find an interface descriptor */
234 list_for_each_entry(descriptor, &manifest_descs, links) {
Alex Elder63cc9322014-10-02 12:30:02 -0500235 if (descriptor->type == GREYBUS_TYPE_INTERFACE) {
Alex Elderd88bfb52014-10-01 21:54:17 -0500236 found = true;
237 break;
238 }
239 }
240 if (!found)
241 break;
242
243 /* Found one. Set up its interface structure*/
244 desc_interface = descriptor->data;
245 interface = gb_interface_create(gmod, desc_interface->id);
246 if (!interface)
247 return 0; /* Error */
Alex Elderc095bbc2014-10-01 21:54:18 -0500248
249 /* Now go set up this interface's functions and cports */
250 if (!gb_manifest_parse_cports(interface))
251 return 0; /* Error parsing cports */
252
Alex Elderd88bfb52014-10-01 21:54:17 -0500253 count++;
254
255 /* Done with this interface descriptor */
256 release_manifest_descriptor(descriptor);
257 }
258
259 return count;
260}
261
Greg Kroah-Hartman2f30d9f2014-10-02 21:26:26 -0700262static struct gb_module *gb_manifest_parse_module(struct manifest_desc *module_desc)
Alex Elderb09c94a2014-10-01 21:54:16 -0500263{
264 struct greybus_descriptor *desc = module_desc->data;
265 struct greybus_descriptor_module *desc_module = &desc->module;
266 struct gb_module *gmod;
267
268 gmod = kzalloc(sizeof(*gmod), GFP_KERNEL);
269 if (!gmod)
270 return NULL;
271
272 /* Handle the strings first--they can fail */
273 gmod->vendor_string = gb_string_get(desc_module->vendor_stringid);
274 if (IS_ERR(gmod->vendor_string)) {
275 kfree(gmod);
276 return NULL;
277 }
278 gmod->product_string = gb_string_get(desc_module->product_stringid);
279 if (IS_ERR(gmod->product_string)) {
280 kfree(gmod->vendor_string);
281 kfree(gmod);
282 return NULL;
283 }
284
285 gmod->vendor = le16_to_cpu(desc_module->vendor);
286 gmod->product = le16_to_cpu(desc_module->product);
287 gmod->version = le16_to_cpu(desc_module->version);
Alex Elder63cc9322014-10-02 12:30:02 -0500288 gmod->unique_id = le64_to_cpu(desc_module->unique_id);
Alex Elderb09c94a2014-10-01 21:54:16 -0500289
290 /* Release the module descriptor, now that we're done with it */
291 release_manifest_descriptor(module_desc);
292
Alex Elderd88bfb52014-10-01 21:54:17 -0500293 /* A module must have at least one interface descriptor */
294 if (!gb_manifest_parse_interfaces(gmod)) {
295 pr_err("manifest interface descriptors not valid\n");
296 gb_module_destroy(gmod);
297 return NULL;
298 }
299
Alex Elderb09c94a2014-10-01 21:54:16 -0500300 return gmod;
301}
302
303/*
304 * Parse a buffer containing a module manifest.
305 *
306 * If we find anything wrong with the content/format of the buffer
307 * we reject it.
308 *
309 * The first requirement is that the manifest's version is
310 * one we can parse.
311 *
312 * We make an initial pass through the buffer and identify all of
313 * the descriptors it contains, keeping track for each its type
314 * and the location size of its data in the buffer.
315 *
316 * Next we scan the descriptors, looking for a module descriptor;
317 * there must be exactly one of those. When found, we record the
318 * information it contains, and then remove that descriptor (and any
319 * string descriptors it refers to) from further consideration.
320 *
321 * After that we look for the module's interfaces--there must be at
322 * least one of those.
323 *
324 * Return a pointer to an initialized gb_module structure
325 * representing the content of the module manifest, or a null
326 * pointer if an error occurs.
327 */
328struct gb_module *gb_manifest_parse(void *data, size_t size)
329{
330 struct greybus_manifest *manifest;
331 struct greybus_manifest_header *header;
332 struct greybus_descriptor *desc;
333 struct manifest_desc *descriptor;
334 struct manifest_desc *module_desc = NULL;
335 struct gb_module *gmod;
336 u16 manifest_size;
337 u32 found = 0;
338
339 /* we have to have at _least_ the manifest header */
340 if (size <= sizeof(manifest->header)) {
341 pr_err("short manifest (%zu)\n", size);
342 return NULL;
343 }
344
345 /* Make sure the size is right */
346 manifest = data;
347 header = &manifest->header;
348 manifest_size = le16_to_cpu(header->size);
349 if (manifest_size != size) {
350 pr_err("manifest size mismatch %zu != %hu\n",
351 size, manifest_size);
352 return NULL;
353 }
354
355 /* Validate major/minor number */
356 if (header->version_major > GREYBUS_VERSION_MAJOR) {
357 pr_err("manifest version too new (%hhu.%hhu > %hhu.%hhu)\n",
358 header->version_major, header->version_minor,
359 GREYBUS_VERSION_MAJOR, GREYBUS_VERSION_MINOR);
360 return NULL;
361 }
362
363 /* OK, find all the descriptors */
364 desc = (struct greybus_descriptor *)(header + 1);
365 size -= sizeof(*header);
366 while (size) {
367 int desc_size;
368
369 desc_size = identify_descriptor(desc, size);
370 if (desc_size <= 0) {
371 if (!desc_size)
372 pr_err("zero-sized manifest descriptor\n");
373 goto out_err;
374 }
375 desc = (struct greybus_descriptor *)((char *)desc + desc_size);
376 size -= desc_size;
377 }
378
379 /* There must be a single module descriptor */
380 list_for_each_entry(descriptor, &manifest_descs, links) {
381 if (descriptor->type == GREYBUS_TYPE_MODULE)
382 if (!found++)
383 module_desc = descriptor;
384 }
385 if (found != 1) {
386 pr_err("manifest must have 1 module descriptor (%u found)\n",
387 found);
388 goto out_err;
389 }
390
391 /* Parse the module manifest, starting with the module descriptor */
392 gmod = gb_manifest_parse_module(module_desc);
393
394 /*
395 * We really should have no remaining descriptors, but we
396 * don't know what newer format manifests might leave.
397 */
398 if (!list_empty(&manifest_descs)) {
399 pr_info("excess descriptors in module manifest\n");
400 release_manifest_descriptors();
401 }
402
403 return gmod;
404out_err:
405 release_manifest_descriptors();
406
407 return NULL;
408}