2 * Greybus manifest parsing
4 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
7 * Released under the GPLv2 only.
12 static const char *get_descriptor_type_string(u8 type
)
15 case GREYBUS_TYPE_INVALID
:
17 case GREYBUS_TYPE_STRING
:
19 case GREYBUS_TYPE_INTERFACE
:
21 case GREYBUS_TYPE_CPORT
:
23 case GREYBUS_TYPE_BUNDLE
:
32 * We scan the manifest once to identify where all the descriptors
33 * are. The result is a list of these manifest_desc structures. We
34 * then pick through them for what we're looking for (starting with
35 * the interface descriptor). As each is processed we remove it from
36 * the list. When we're done the list should (probably) be empty.
38 struct manifest_desc
{
39 struct list_head links
;
43 enum greybus_descriptor_type type
;
46 static void release_manifest_descriptor(struct manifest_desc
*descriptor
)
48 list_del(&descriptor
->links
);
52 static void release_manifest_descriptors(struct gb_interface
*intf
)
54 struct manifest_desc
*descriptor
;
55 struct manifest_desc
*next
;
57 list_for_each_entry_safe(descriptor
, next
, &intf
->manifest_descs
, links
)
58 release_manifest_descriptor(descriptor
);
61 static void release_cport_descriptors(struct list_head
*head
, u8 bundle_id
)
63 struct manifest_desc
*desc
, *tmp
;
64 struct greybus_descriptor_cport
*desc_cport
;
66 list_for_each_entry_safe(desc
, tmp
, head
, links
) {
67 desc_cport
= desc
->data
;
69 if (desc
->type
!= GREYBUS_TYPE_CPORT
)
72 if (desc_cport
->bundle
== bundle_id
)
73 release_manifest_descriptor(desc
);
77 static struct manifest_desc
*get_next_bundle_desc(struct gb_interface
*intf
)
79 struct manifest_desc
*descriptor
;
80 struct manifest_desc
*next
;
82 list_for_each_entry_safe(descriptor
, next
, &intf
->manifest_descs
, links
)
83 if (descriptor
->type
== GREYBUS_TYPE_BUNDLE
)
90 * Validate the given descriptor. Its reported size must fit within
91 * the number of bytes remaining, and it must have a recognized
92 * type. Check that the reported size is at least as big as what
93 * we expect to see. (It could be bigger, perhaps for a new version
96 * Returns the (non-zero) number of bytes consumed by the descriptor,
97 * or a negative errno.
99 static int identify_descriptor(struct gb_interface
*intf
,
100 struct greybus_descriptor
*desc
, size_t size
)
102 struct greybus_descriptor_header
*desc_header
= &desc
->header
;
103 struct manifest_desc
*descriptor
;
105 size_t expected_size
;
107 if (size
< sizeof(*desc_header
)) {
108 dev_err(&intf
->dev
, "manifest too small (%zu < %zu)\n",
109 size
, sizeof(*desc_header
));
110 return -EINVAL
; /* Must at least have header */
113 desc_size
= le16_to_cpu(desc_header
->size
);
114 if (desc_size
> size
) {
115 dev_err(&intf
->dev
, "descriptor too big (%zu > %zu)\n",
120 /* Descriptor needs to at least have a header */
121 expected_size
= sizeof(*desc_header
);
123 switch (desc_header
->type
) {
124 case GREYBUS_TYPE_STRING
:
125 expected_size
+= sizeof(struct greybus_descriptor_string
);
126 expected_size
+= desc
->string
.length
;
128 /* String descriptors are padded to 4 byte boundaries */
129 expected_size
= ALIGN(expected_size
, 4);
131 case GREYBUS_TYPE_INTERFACE
:
132 expected_size
+= sizeof(struct greybus_descriptor_interface
);
134 case GREYBUS_TYPE_BUNDLE
:
135 expected_size
+= sizeof(struct greybus_descriptor_bundle
);
137 case GREYBUS_TYPE_CPORT
:
138 expected_size
+= sizeof(struct greybus_descriptor_cport
);
140 case GREYBUS_TYPE_INVALID
:
142 dev_err(&intf
->dev
, "invalid descriptor type (%u)\n",
147 if (desc_size
< expected_size
) {
148 dev_err(&intf
->dev
, "%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 dev_warn(&intf
->dev
, "%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 greybus_descriptor_cport
*desc_cport
;
233 struct manifest_desc
*desc
, *next
, *tmp
;
235 u8 bundle_id
= bundle
->id
;
240 /* Set up all cport descriptors associated with this bundle */
241 list_for_each_entry_safe(desc
, next
, &intf
->manifest_descs
, links
) {
242 if (desc
->type
!= GREYBUS_TYPE_CPORT
)
245 desc_cport
= desc
->data
;
246 if (desc_cport
->bundle
!= bundle_id
)
249 cport_id
= le16_to_cpu(desc_cport
->id
);
250 if (cport_id
> CPORT_ID_MAX
)
253 /* Nothing else should have its cport_id as control cport id */
254 if (cport_id
== GB_CONTROL_CPORT_ID
) {
255 dev_err(&bundle
->dev
, "invalid cport id found (%02u)\n",
261 * Found one, move it to our temporary list after checking for
264 list_for_each_entry(tmp
, &list
, links
) {
265 desc_cport
= tmp
->data
;
266 if (cport_id
== le16_to_cpu(desc_cport
->id
)) {
267 dev_err(&bundle
->dev
,
268 "duplicate CPort %u found\n",
273 list_move_tail(&desc
->links
, &list
);
280 bundle
->cport_desc
= kcalloc(count
, sizeof(*bundle
->cport_desc
),
282 if (!bundle
->cport_desc
)
285 bundle
->num_cports
= count
;
288 list_for_each_entry_safe(desc
, next
, &list
, links
) {
289 desc_cport
= desc
->data
;
290 memcpy(&bundle
->cport_desc
[i
++], desc_cport
,
291 sizeof(*desc_cport
));
293 /* Release the cport descriptor */
294 release_manifest_descriptor(desc
);
299 release_cport_descriptors(&list
, bundle_id
);
301 * Free all cports for this bundle to avoid 'excess descriptors'
304 release_cport_descriptors(&intf
->manifest_descs
, bundle_id
);
306 return 0; /* Error; count should also be 0 */
310 * Find bundle descriptors in the manifest and set up their data
311 * structures. Returns the number of bundles set up for the
314 static u32
gb_manifest_parse_bundles(struct gb_interface
*intf
)
316 struct manifest_desc
*desc
;
317 struct gb_bundle
*bundle
;
318 struct gb_bundle
*bundle_next
;
323 while ((desc
= get_next_bundle_desc(intf
))) {
324 struct greybus_descriptor_bundle
*desc_bundle
;
326 /* Found one. Set up its bundle structure*/
327 desc_bundle
= desc
->data
;
328 bundle_id
= desc_bundle
->id
;
329 class = desc_bundle
->class;
331 /* Done with this bundle descriptor */
332 release_manifest_descriptor(desc
);
334 /* Ignore any legacy control bundles */
335 if (bundle_id
== GB_CONTROL_BUNDLE_ID
) {
336 dev_dbg(&intf
->dev
, "%s - ignoring control bundle\n",
338 release_cport_descriptors(&intf
->manifest_descs
,
343 /* Nothing else should have its class set to control class */
344 if (class == GREYBUS_CLASS_CONTROL
) {
346 "bundle %u cannot use control class\n",
351 bundle
= gb_bundle_create(intf
, bundle_id
, class);
356 * Now go set up this bundle's functions and cports.
358 * A 'bundle' represents a device in greybus. It may require
359 * multiple cports for its functioning. If we fail to setup any
360 * cport of a bundle, we better reject the complete bundle as
361 * the device may not be able to function properly then.
363 * But, failing to setup a cport of bundle X doesn't mean that
364 * the device corresponding to bundle Y will not work properly.
365 * Bundles should be treated as separate independent devices.
367 * While parsing manifest for an interface, treat bundles as
368 * separate entities and don't reject entire interface and its
369 * bundles on failing to initialize a cport. But make sure the
370 * bundle which needs the cport, gets destroyed properly.
372 if (!gb_manifest_parse_cports(bundle
)) {
373 gb_bundle_destroy(bundle
);
382 /* An error occurred; undo any changes we've made */
383 list_for_each_entry_safe(bundle
, bundle_next
, &intf
->bundles
, links
) {
384 gb_bundle_destroy(bundle
);
387 return 0; /* Error; count should also be 0 */
390 static bool gb_manifest_parse_interface(struct gb_interface
*intf
,
391 struct manifest_desc
*interface_desc
)
393 struct greybus_descriptor_interface
*desc_intf
= interface_desc
->data
;
394 struct gb_control
*control
= intf
->control
;
397 /* Handle the strings first--they can fail */
398 str
= gb_string_get(intf
, desc_intf
->vendor_stringid
);
401 control
->vendor_string
= str
;
403 str
= gb_string_get(intf
, desc_intf
->product_stringid
);
405 goto out_free_vendor_string
;
406 control
->product_string
= str
;
408 /* Assign feature flags communicated via manifest */
409 intf
->features
= desc_intf
->features
;
411 /* Release the interface descriptor, now that we're done with it */
412 release_manifest_descriptor(interface_desc
);
414 /* An interface must have at least one bundle descriptor */
415 if (!gb_manifest_parse_bundles(intf
)) {
416 dev_err(&intf
->dev
, "manifest bundle descriptors not valid\n");
422 kfree(control
->product_string
);
423 control
->product_string
= NULL
;
424 out_free_vendor_string
:
425 kfree(control
->vendor_string
);
426 control
->vendor_string
= NULL
;
432 * Parse a buffer containing an interface manifest.
434 * If we find anything wrong with the content/format of the buffer
437 * The first requirement is that the manifest's version is
440 * We make an initial pass through the buffer and identify all of
441 * the descriptors it contains, keeping track for each its type
442 * and the location size of its data in the buffer.
444 * Next we scan the descriptors, looking for an interface descriptor;
445 * there must be exactly one of those. When found, we record the
446 * information it contains, and then remove that descriptor (and any
447 * string descriptors it refers to) from further consideration.
449 * After that we look for the interface's bundles--there must be at
450 * least one of those.
452 * Returns true if parsing was successful, false otherwise.
454 bool gb_manifest_parse(struct gb_interface
*intf
, void *data
, size_t size
)
456 struct greybus_manifest
*manifest
;
457 struct greybus_manifest_header
*header
;
458 struct greybus_descriptor
*desc
;
459 struct manifest_desc
*descriptor
;
460 struct manifest_desc
*interface_desc
= NULL
;
465 /* Manifest descriptor list should be empty here */
466 if (WARN_ON(!list_empty(&intf
->manifest_descs
)))
469 /* we have to have at _least_ the manifest header */
470 if (size
< sizeof(*header
)) {
471 dev_err(&intf
->dev
, "short manifest (%zu < %zu)\n",
472 size
, sizeof(*header
));
476 /* Make sure the size is right */
478 header
= &manifest
->header
;
479 manifest_size
= le16_to_cpu(header
->size
);
480 if (manifest_size
!= size
) {
481 dev_err(&intf
->dev
, "manifest size mismatch (%zu != %u)\n",
482 size
, manifest_size
);
486 /* Validate major/minor number */
487 if (header
->version_major
> GREYBUS_VERSION_MAJOR
) {
488 dev_err(&intf
->dev
, "manifest version too new (%u.%u > %u.%u)\n",
489 header
->version_major
, header
->version_minor
,
490 GREYBUS_VERSION_MAJOR
, GREYBUS_VERSION_MINOR
);
494 /* OK, find all the descriptors */
495 desc
= manifest
->descriptors
;
496 size
-= sizeof(*header
);
500 desc_size
= identify_descriptor(intf
, desc
, size
);
505 desc
= (struct greybus_descriptor
*)((char *)desc
+ desc_size
);
509 /* There must be a single interface descriptor */
510 list_for_each_entry(descriptor
, &intf
->manifest_descs
, links
) {
511 if (descriptor
->type
== GREYBUS_TYPE_INTERFACE
)
513 interface_desc
= descriptor
;
516 dev_err(&intf
->dev
, "manifest must have 1 interface descriptor (%u found)\n",
522 /* Parse the manifest, starting with the interface descriptor */
523 result
= gb_manifest_parse_interface(intf
, interface_desc
);
526 * We really should have no remaining descriptors, but we
527 * don't know what newer format manifests might leave.
529 if (result
&& !list_empty(&intf
->manifest_descs
))
530 dev_info(&intf
->dev
, "excess descriptors in interface manifest\n");
532 release_manifest_descriptors(intf
);