]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/greybus/manifest.c
greybus: manifest: Remove vendor, product and unique-id from interface 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 bigger than what we expect */
133         if (desc_size > expected_size) {
134                 pr_warn("%s descriptor size mismatch (want %zu got %zu)\n",
135                         get_descriptor_type_string(desc_header->type),
136                         expected_size, desc_size);
137         }
138
139         descriptor = kzalloc(sizeof(*descriptor), GFP_KERNEL);
140         if (!descriptor)
141                 return -ENOMEM;
142
143         descriptor->size = desc_size;
144         descriptor->data = (u8 *)desc + sizeof(*desc_header);
145         descriptor->type = desc_header->type;
146         list_add_tail(&descriptor->links, &intf->manifest_descs);
147
148         /* desc_size is is positive and is known to fit in a signed int */
149
150         return desc_size;
151 }
152
153 /*
154  * Find the string descriptor having the given id, validate it, and
155  * allocate a duplicate copy of it.  The duplicate has an extra byte
156  * which guarantees the returned string is NUL-terminated.
157  *
158  * String index 0 is valid (it represents "no string"), and for
159  * that a null pointer is returned.
160  *
161  * Otherwise returns a pointer to a newly-allocated copy of the
162  * descriptor string, or an error-coded pointer on failure.
163  */
164 static char *gb_string_get(struct gb_interface *intf, u8 string_id)
165 {
166         struct greybus_descriptor_string *desc_string;
167         struct manifest_desc *descriptor;
168         bool found = false;
169         char *string;
170
171         /* A zero string id means no string (but no error) */
172         if (!string_id)
173                 return NULL;
174
175         list_for_each_entry(descriptor, &intf->manifest_descs, links) {
176
177                 if (descriptor->type != GREYBUS_TYPE_STRING)
178                         continue;
179
180                 desc_string = descriptor->data;
181                 if (desc_string->id == string_id) {
182                         found = true;
183                         break;
184                 }
185         }
186         if (!found)
187                 return ERR_PTR(-ENOENT);
188
189         /* Allocate an extra byte so we can guarantee it's NUL-terminated */
190         string = kmemdup(&desc_string->string, (size_t)desc_string->length + 1,
191                                 GFP_KERNEL);
192         if (!string)
193                 return ERR_PTR(-ENOMEM);
194         string[desc_string->length] = '\0';
195
196         /* Ok we've used this string, so we're done with it */
197         release_manifest_descriptor(descriptor);
198
199         return string;
200 }
201
202 /*
203  * Find cport descriptors in the manifest and set up data structures
204  * for the functions that use them.  Returns the number of bundles
205  * set up for the given interface, or 0 if there is an error.
206  */
207 static u32 gb_manifest_parse_cports(struct gb_interface *intf,
208                                     struct gb_bundle *bundle)
209 {
210         u32 count = 0;
211
212         while (true) {
213                 struct manifest_desc *descriptor;
214                 struct greybus_descriptor_cport *desc_cport;
215                 u8 protocol_id;
216                 u16 cport_id;
217                 bool found = false;
218
219                 /* Find a cport descriptor */
220                 list_for_each_entry(descriptor, &intf->manifest_descs, links) {
221                         if (descriptor->type == GREYBUS_TYPE_CPORT) {
222                                 desc_cport = descriptor->data;
223                                 if (desc_cport->bundle == bundle->id) {
224                                         found = true;
225                                         break;
226                                 }
227                         }
228                 }
229                 if (!found)
230                         break;
231
232                 /* Found one.  Set up its function structure */
233                 protocol_id = desc_cport->protocol_id;
234                 cport_id = le16_to_cpu(desc_cport->id);
235                 if (!gb_connection_create(bundle, cport_id, protocol_id))
236                         return 0;       /* Error */
237
238                 count++;
239                 /* Release the cport descriptor */
240                 release_manifest_descriptor(descriptor);
241         }
242
243         return count;
244 }
245
246 /*
247  * Find bundle descriptors in the manifest and set up their data
248  * structures.  Returns the number of bundles set up for the
249  * given interface.
250  */
251 static u32 gb_manifest_parse_bundles(struct gb_interface *intf)
252 {
253         u32 count = 0;
254
255         while (true) {
256                 struct manifest_desc *descriptor;
257                 struct greybus_descriptor_bundle *desc_bundle;
258                 struct gb_bundle *bundle;
259                 bool found = false;
260
261                 /* Find an bundle descriptor */
262                 list_for_each_entry(descriptor, &intf->manifest_descs, links) {
263                         if (descriptor->type == GREYBUS_TYPE_BUNDLE) {
264                                 found = true;
265                                 break;
266                         }
267                 }
268                 if (!found)
269                         break;
270
271                 /* Found one.  Set up its bundle structure*/
272                 desc_bundle = descriptor->data;
273                 bundle = gb_bundle_create(intf, desc_bundle->id,
274                                           desc_bundle->class);
275                 if (!bundle)
276                         return 0;       /* Error */
277
278                 /* Now go set up this bundle's functions and cports */
279                 if (!gb_manifest_parse_cports(intf, bundle))
280                         return 0;       /* Error parsing cports */
281
282                 count++;
283
284                 /* Done with this bundle descriptor */
285                 release_manifest_descriptor(descriptor);
286         }
287
288         return count;
289 }
290
291 static bool gb_manifest_parse_interface(struct gb_interface *intf,
292                                         struct manifest_desc *interface_desc)
293 {
294         struct greybus_descriptor_interface *desc_intf = interface_desc->data;
295
296         /* Handle the strings first--they can fail */
297         intf->vendor_string = gb_string_get(intf, desc_intf->vendor_stringid);
298         if (IS_ERR(intf->vendor_string))
299                 return false;
300
301         intf->product_string = gb_string_get(intf,
302                                              desc_intf->product_stringid);
303         if (IS_ERR(intf->product_string)) {
304                 goto out_free_vendor_string;
305         }
306
307         // FIXME
308         // Vendor, Product and Unique id must come via control protocol
309         intf->vendor = 0xffff;
310         intf->product = 0x0001;
311         intf->unique_id = 0;
312
313         /* Release the interface descriptor, now that we're done with it */
314         release_manifest_descriptor(interface_desc);
315
316         /* An interface must have at least one bundle descriptor */
317         if (!gb_manifest_parse_bundles(intf)) {
318                 pr_err("manifest bundle descriptors not valid\n");
319                 goto out_err;
320         }
321
322         return true;
323 out_err:
324         kfree(intf->product_string);
325         intf->product_string = NULL;
326 out_free_vendor_string:
327         kfree(intf->vendor_string);
328         intf->vendor_string = NULL;
329
330         return false;
331 }
332
333 /*
334  * Parse a buffer containing a Interface manifest.
335  *
336  * If we find anything wrong with the content/format of the buffer
337  * we reject it.
338  *
339  * The first requirement is that the manifest's version is
340  * one we can parse.
341  *
342  * We make an initial pass through the buffer and identify all of
343  * the descriptors it contains, keeping track for each its type
344  * and the location size of its data in the buffer.
345  *
346  * Next we scan the descriptors, looking for a interface descriptor;
347  * there must be exactly one of those.  When found, we record the
348  * information it contains, and then remove that descriptor (and any
349  * string descriptors it refers to) from further consideration.
350  *
351  * After that we look for the interface's bundles--there must be at
352  * least one of those.
353  *
354  * Returns true if parsing was successful, false otherwise.
355  */
356 bool gb_manifest_parse(struct gb_interface *intf, void *data, size_t size)
357 {
358         struct greybus_manifest *manifest;
359         struct greybus_manifest_header *header;
360         struct greybus_descriptor *desc;
361         struct manifest_desc *descriptor;
362         struct manifest_desc *interface_desc = NULL;
363         u16 manifest_size;
364         u32 found = 0;
365         bool result;
366
367         /* Manifest descriptor list should be empty here */
368         if (WARN_ON(!list_empty(&intf->manifest_descs)))
369                 return false;
370
371         /* we have to have at _least_ the manifest header */
372         if (size <= sizeof(manifest->header)) {
373                 pr_err("short manifest (%zu)\n", size);
374                 return false;
375         }
376
377         /* Make sure the size is right */
378         manifest = data;
379         header = &manifest->header;
380         manifest_size = le16_to_cpu(header->size);
381         if (manifest_size != size) {
382                 pr_err("manifest size mismatch %zu != %hu\n",
383                         size, manifest_size);
384                 return false;
385         }
386
387         /* Validate major/minor number */
388         if (header->version_major > GREYBUS_VERSION_MAJOR) {
389                 pr_err("manifest version too new (%hhu.%hhu > %hhu.%hhu)\n",
390                         header->version_major, header->version_minor,
391                         GREYBUS_VERSION_MAJOR, GREYBUS_VERSION_MINOR);
392                 return false;
393         }
394
395         /* OK, find all the descriptors */
396         desc = (struct greybus_descriptor *)(header + 1);
397         size -= sizeof(*header);
398         while (size) {
399                 int desc_size;
400
401                 desc_size = identify_descriptor(intf, desc, size);
402                 if (desc_size < 0) {
403                         result = false;
404                         goto out;
405                 }
406                 desc = (struct greybus_descriptor *)((char *)desc + desc_size);
407                 size -= desc_size;
408         }
409
410         /* There must be a single interface descriptor */
411         list_for_each_entry(descriptor, &intf->manifest_descs, links) {
412                 if (descriptor->type == GREYBUS_TYPE_INTERFACE)
413                         if (!found++)
414                                 interface_desc = descriptor;
415         }
416         if (found != 1) {
417                 pr_err("manifest must have 1 interface descriptor (%u found)\n",
418                         found);
419                 result = false;
420                 goto out;
421         }
422
423         /* Parse the manifest, starting with the interface descriptor */
424         result = gb_manifest_parse_interface(intf, interface_desc);
425
426         /*
427          * We really should have no remaining descriptors, but we
428          * don't know what newer format manifests might leave.
429          */
430         if (result && !list_empty(&intf->manifest_descs))
431                 pr_info("excess descriptors in interface manifest\n");
432 out:
433         release_manifest_descriptors(intf);
434
435         return result;
436 }