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