2 * Greybus manifest parsing
4 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 static const char *get_descriptor_type_string(u8 type)
17 case GREYBUS_TYPE_INVALID:
19 case GREYBUS_TYPE_STRING:
21 case GREYBUS_TYPE_INTERFACE:
23 case GREYBUS_TYPE_CPORT:
25 case GREYBUS_TYPE_BUNDLE:
34 * We scan the manifest once to identify where all the descriptors
35 * are. The result is a list of these manifest_desc structures. We
36 * then pick through them for what we're looking for (starting with
37 * the interface descriptor). As each is processed we remove it from
38 * the list. When we're done the list should (probably) be empty.
40 struct manifest_desc {
41 struct list_head links;
45 enum greybus_descriptor_type type;
48 static void release_manifest_descriptor(struct manifest_desc *descriptor)
50 list_del(&descriptor->links);
54 static void release_manifest_descriptors(struct gb_interface *intf)
56 struct manifest_desc *descriptor;
57 struct manifest_desc *next;
59 list_for_each_entry_safe(descriptor, next, &intf->manifest_descs, links)
60 release_manifest_descriptor(descriptor);
63 static void release_cport_descriptors(struct list_head *head, u8 bundle_id)
65 struct manifest_desc *desc, *tmp;
66 struct greybus_descriptor_cport *desc_cport;
68 list_for_each_entry_safe(desc, tmp, head, links) {
69 desc_cport = desc->data;
71 if (desc->type != GREYBUS_TYPE_CPORT)
74 if (desc_cport->bundle == bundle_id)
75 release_manifest_descriptor(desc);
79 static struct manifest_desc *get_next_bundle_desc(struct gb_interface *intf)
81 struct manifest_desc *descriptor;
82 struct manifest_desc *next;
84 list_for_each_entry_safe(descriptor, next, &intf->manifest_descs, links)
85 if (descriptor->type == GREYBUS_TYPE_BUNDLE)
92 * Validate the given descriptor. Its reported size must fit within
93 * the number of bytes remaining, and it must have a recognized
94 * type. Check that the reported size is at least as big as what
95 * we expect to see. (It could be bigger, perhaps for a new version
98 * Returns the (non-zero) number of bytes consumed by the descriptor,
99 * or a negative errno.
101 static int identify_descriptor(struct gb_interface *intf,
102 struct greybus_descriptor *desc, size_t size)
104 struct greybus_descriptor_header *desc_header = &desc->header;
105 struct manifest_desc *descriptor;
107 size_t expected_size;
109 if (size < sizeof(*desc_header)) {
110 pr_err("manifest too small (%zu < %zu)\n",
111 size, sizeof(*desc_header));
112 return -EINVAL; /* Must at least have header */
115 desc_size = le16_to_cpu(desc_header->size);
116 if (desc_size > size) {
117 pr_err("descriptor too big (%zu > %zu)\n", desc_size, size);
121 /* Descriptor needs to at least have a header */
122 expected_size = sizeof(*desc_header);
124 switch (desc_header->type) {
125 case GREYBUS_TYPE_STRING:
126 expected_size += sizeof(struct greybus_descriptor_string);
127 expected_size += desc->string.length;
129 /* String descriptors are padded to 4 byte boundaries */
130 expected_size = ALIGN(expected_size, 4);
132 case GREYBUS_TYPE_INTERFACE:
133 expected_size += sizeof(struct greybus_descriptor_interface);
135 case GREYBUS_TYPE_BUNDLE:
136 expected_size += sizeof(struct greybus_descriptor_bundle);
138 case GREYBUS_TYPE_CPORT:
139 expected_size += sizeof(struct greybus_descriptor_cport);
141 case GREYBUS_TYPE_INVALID:
143 pr_err("invalid descriptor type (%hhu)\n", desc_header->type);
147 if (desc_size < expected_size) {
148 pr_err("%s descriptor too small (%zu < %zu)\n",
149 get_descriptor_type_string(desc_header->type),
150 desc_size, expected_size);
154 /* Descriptor bigger than what we expect */
155 if (desc_size > expected_size) {
156 pr_warn("%s descriptor size mismatch (want %zu got %zu)\n",
157 get_descriptor_type_string(desc_header->type),
158 expected_size, desc_size);
161 descriptor = kzalloc(sizeof(*descriptor), GFP_KERNEL);
165 descriptor->size = desc_size;
166 descriptor->data = (char *)desc + sizeof(*desc_header);
167 descriptor->type = desc_header->type;
168 list_add_tail(&descriptor->links, &intf->manifest_descs);
170 /* desc_size is positive and is known to fit in a signed int */
176 * Find the string descriptor having the given id, validate it, and
177 * allocate a duplicate copy of it. The duplicate has an extra byte
178 * which guarantees the returned string is NUL-terminated.
180 * String index 0 is valid (it represents "no string"), and for
181 * that a null pointer is returned.
183 * Otherwise returns a pointer to a newly-allocated copy of the
184 * descriptor string, or an error-coded pointer on failure.
186 static char *gb_string_get(struct gb_interface *intf, u8 string_id)
188 struct greybus_descriptor_string *desc_string;
189 struct manifest_desc *descriptor;
193 /* A zero string id means no string (but no error) */
197 list_for_each_entry(descriptor, &intf->manifest_descs, links) {
198 if (descriptor->type != GREYBUS_TYPE_STRING)
201 desc_string = descriptor->data;
202 if (desc_string->id == string_id) {
208 return ERR_PTR(-ENOENT);
210 /* Allocate an extra byte so we can guarantee it's NUL-terminated */
211 string = kmemdup(&desc_string->string, desc_string->length + 1,
214 return ERR_PTR(-ENOMEM);
215 string[desc_string->length] = '\0';
217 /* Ok we've used this string, so we're done with it */
218 release_manifest_descriptor(descriptor);
224 * Find cport descriptors in the manifest associated with the given
225 * bundle, and set up data structures for the functions that use
226 * them. Returns the number of cports set up for the bundle, or 0
227 * if there is an error.
229 static u32 gb_manifest_parse_cports(struct gb_bundle *bundle)
231 struct gb_interface *intf = bundle->intf;
232 struct manifest_desc *desc;
233 struct manifest_desc *next;
234 u8 bundle_id = bundle->id;
239 /* Set up all cport descriptors associated with this bundle */
240 list_for_each_entry_safe(desc, next, &intf->manifest_descs, links) {
241 struct greybus_descriptor_cport *desc_cport;
243 if (desc->type != GREYBUS_TYPE_CPORT)
246 desc_cport = desc->data;
247 if (desc_cport->bundle != bundle_id)
250 cport_id = le16_to_cpu(desc_cport->id);
251 if (cport_id > CPORT_ID_MAX)
254 /* Found one. Set up its function structure */
255 protocol_id = desc_cport->protocol_id;
257 if (!gb_connection_create_dynamic(intf, bundle, cport_id,
263 /* Release the cport descriptor */
264 release_manifest_descriptor(desc);
271 * Free all cports for this bundle to avoid 'excess descriptors'
274 release_cport_descriptors(&intf->manifest_descs, bundle_id);
276 return 0; /* Error; count should also be 0 */
280 * Find bundle descriptors in the manifest and set up their data
281 * structures. Returns the number of bundles set up for the
284 static u32 gb_manifest_parse_bundles(struct gb_interface *intf)
286 struct manifest_desc *desc;
287 struct gb_bundle *bundle;
288 struct gb_bundle *bundle_next;
293 while ((desc = get_next_bundle_desc(intf))) {
294 struct greybus_descriptor_bundle *desc_bundle;
296 /* Found one. Set up its bundle structure*/
297 desc_bundle = desc->data;
298 bundle_id = desc_bundle->id;
299 class = desc_bundle->class;
301 /* Done with this bundle descriptor */
302 release_manifest_descriptor(desc);
304 /* Ignore any legacy control bundles */
305 if (bundle_id == GB_CONTROL_BUNDLE_ID) {
306 dev_dbg(&intf->dev, "%s - ignoring control bundle\n",
308 release_cport_descriptors(&intf->manifest_descs,
313 /* Nothing else should have its class set to control class */
314 if (class == GREYBUS_CLASS_CONTROL) {
316 "bundle 0x%02x cannot use control class\n",
321 bundle = gb_bundle_create(intf, bundle_id, class);
326 * Now go set up this bundle's functions and cports.
328 * A 'bundle' represents a device in greybus. It may require
329 * multiple cports for its functioning. If we fail to setup any
330 * cport of a bundle, we better reject the complete bundle as
331 * the device may not be able to function properly then.
333 * But, failing to setup a cport of bundle X doesn't mean that
334 * the device corresponding to bundle Y will not work properly.
335 * Bundles should be treated as separate independent devices.
337 * While parsing manifest for an interface, treat bundles as
338 * separate entities and don't reject entire interface and its
339 * bundles on failing to initialize a cport. But make sure the
340 * bundle which needs the cport, gets destroyed properly.
342 if (!gb_manifest_parse_cports(bundle)) {
343 gb_bundle_destroy(bundle);
352 /* An error occurred; undo any changes we've made */
353 list_for_each_entry_safe(bundle, bundle_next, &intf->bundles, links) {
354 gb_bundle_destroy(bundle);
357 return 0; /* Error; count should also be 0 */
360 static bool gb_manifest_parse_interface(struct gb_interface *intf,
361 struct manifest_desc *interface_desc)
363 struct greybus_descriptor_interface *desc_intf = interface_desc->data;
365 /* Handle the strings first--they can fail */
366 intf->vendor_string = gb_string_get(intf, desc_intf->vendor_stringid);
367 if (IS_ERR(intf->vendor_string))
370 intf->product_string = gb_string_get(intf, desc_intf->product_stringid);
371 if (IS_ERR(intf->product_string))
372 goto out_free_vendor_string;
374 /* Release the interface descriptor, now that we're done with it */
375 release_manifest_descriptor(interface_desc);
377 /* An interface must have at least one bundle descriptor */
378 if (!gb_manifest_parse_bundles(intf)) {
379 dev_err(&intf->dev, "manifest bundle descriptors not valid\n");
385 kfree(intf->product_string);
386 intf->product_string = NULL;
387 out_free_vendor_string:
388 kfree(intf->vendor_string);
389 intf->vendor_string = NULL;
395 * Parse a buffer containing an interface manifest.
397 * If we find anything wrong with the content/format of the buffer
400 * The first requirement is that the manifest's version is
403 * We make an initial pass through the buffer and identify all of
404 * the descriptors it contains, keeping track for each its type
405 * and the location size of its data in the buffer.
407 * Next we scan the descriptors, looking for an interface descriptor;
408 * there must be exactly one of those. When found, we record the
409 * information it contains, and then remove that descriptor (and any
410 * string descriptors it refers to) from further consideration.
412 * After that we look for the interface's bundles--there must be at
413 * least one of those.
415 * Returns true if parsing was successful, false otherwise.
417 bool gb_manifest_parse(struct gb_interface *intf, void *data, size_t size)
419 struct greybus_manifest *manifest;
420 struct greybus_manifest_header *header;
421 struct greybus_descriptor *desc;
422 struct manifest_desc *descriptor;
423 struct manifest_desc *interface_desc = NULL;
428 /* Manifest descriptor list should be empty here */
429 if (WARN_ON(!list_empty(&intf->manifest_descs)))
432 /* we have to have at _least_ the manifest header */
433 if (size < sizeof(*header)) {
434 pr_err("short manifest (%zu < %zu)\n", size, sizeof(*header));
438 /* Make sure the size is right */
440 header = &manifest->header;
441 manifest_size = le16_to_cpu(header->size);
442 if (manifest_size != size) {
443 pr_err("manifest size mismatch (%zu != %hu)\n",
444 size, manifest_size);
448 /* Validate major/minor number */
449 if (header->version_major > GREYBUS_VERSION_MAJOR) {
450 pr_err("manifest version too new (%hhu.%hhu > %hhu.%hhu)\n",
451 header->version_major, header->version_minor,
452 GREYBUS_VERSION_MAJOR, GREYBUS_VERSION_MINOR);
456 /* OK, find all the descriptors */
457 desc = manifest->descriptors;
458 size -= sizeof(*header);
462 desc_size = identify_descriptor(intf, desc, size);
467 desc = (struct greybus_descriptor *)((char *)desc + desc_size);
471 /* There must be a single interface descriptor */
472 list_for_each_entry(descriptor, &intf->manifest_descs, links) {
473 if (descriptor->type == GREYBUS_TYPE_INTERFACE)
475 interface_desc = descriptor;
478 pr_err("manifest must have 1 interface descriptor (%u found)\n",
484 /* Parse the manifest, starting with the interface descriptor */
485 result = gb_manifest_parse_interface(intf, interface_desc);
488 * We really should have no remaining descriptors, but we
489 * don't know what newer format manifests might leave.
491 if (result && !list_empty(&intf->manifest_descs))
492 pr_info("excess descriptors in interface manifest\n");
494 release_manifest_descriptors(intf);