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
12 #include <linux/err.h>
16 static const char *get_descriptor_type_string(u8 type)
19 case GREYBUS_TYPE_INVALID:
21 case GREYBUS_TYPE_STRING:
23 case GREYBUS_TYPE_INTERFACE:
25 case GREYBUS_TYPE_CPORT:
27 case GREYBUS_TYPE_BUNDLE:
29 case GREYBUS_TYPE_CLASS:
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.
44 struct manifest_desc {
45 struct list_head links;
49 enum greybus_descriptor_type type;
52 static void release_manifest_descriptor(struct manifest_desc *descriptor)
54 list_del(&descriptor->links);
58 static void release_manifest_descriptors(struct gb_interface *intf)
60 struct manifest_desc *descriptor;
61 struct manifest_desc *next;
63 list_for_each_entry_safe(descriptor, next, &intf->manifest_descs, links)
64 release_manifest_descriptor(descriptor);
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
74 * Returns the number of bytes consumed by the descriptor, or a
77 static int identify_descriptor(struct gb_interface *intf,
78 struct greybus_descriptor *desc, size_t size)
80 struct greybus_descriptor_header *desc_header = &desc->header;
81 struct manifest_desc *descriptor;
85 if (size < sizeof(*desc_header)) {
86 pr_err("manifest too small\n");
87 return -EINVAL; /* Must at least have header */
90 desc_size = le16_to_cpu(desc_header->size);
91 if (desc_size > size) {
92 pr_err("descriptor too big\n");
96 /* Descriptor needs to at least have a header */
97 expected_size = sizeof(*desc_header);
99 switch (desc_header->type) {
100 case GREYBUS_TYPE_STRING:
101 expected_size += sizeof(struct greybus_descriptor_string);
102 expected_size += desc->string.length;
104 /* String descriptors are padded to 4 byte boundaries */
105 expected_size = ALIGN(expected_size, 4);
107 case GREYBUS_TYPE_INTERFACE:
108 expected_size += sizeof(struct greybus_descriptor_interface);
110 case GREYBUS_TYPE_BUNDLE:
111 expected_size += sizeof(struct greybus_descriptor_bundle);
113 case GREYBUS_TYPE_CPORT:
114 expected_size += sizeof(struct greybus_descriptor_cport);
116 case GREYBUS_TYPE_CLASS:
117 pr_warn("class descriptor found (ignoring)\n");
119 case GREYBUS_TYPE_INVALID:
121 pr_err("invalid descriptor type (%hhu)\n", desc_header->type);
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);
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);
139 descriptor = kzalloc(sizeof(*descriptor), GFP_KERNEL);
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);
148 /* desc_size is is positive and is known to fit in a signed int */
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.
158 * String index 0 is valid (it represents "no string"), and for
159 * that a null pointer is returned.
161 * Otherwise returns a pointer to a newly-allocated copy of the
162 * descriptor string, or an error-coded pointer on failure.
164 static char *gb_string_get(struct gb_interface *intf, u8 string_id)
166 struct greybus_descriptor_string *desc_string;
167 struct manifest_desc *descriptor;
171 /* A zero string id means no string (but no error) */
175 list_for_each_entry(descriptor, &intf->manifest_descs, links) {
177 if (descriptor->type != GREYBUS_TYPE_STRING)
180 desc_string = descriptor->data;
181 if (desc_string->id == string_id) {
187 return ERR_PTR(-ENOENT);
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,
193 return ERR_PTR(-ENOMEM);
194 string[desc_string->length] = '\0';
196 /* Ok we've used this string, so we're done with it */
197 release_manifest_descriptor(descriptor);
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.
207 static u32 gb_manifest_parse_cports(struct gb_interface *intf,
208 struct gb_bundle *bundle)
213 struct manifest_desc *descriptor;
214 struct greybus_descriptor_cport *desc_cport;
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) {
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 */
239 /* Release the cport descriptor */
240 release_manifest_descriptor(descriptor);
247 * Find bundle descriptors in the manifest and set up their data
248 * structures. Returns the number of bundles set up for the
251 static u32 gb_manifest_parse_bundles(struct gb_interface *intf)
256 struct manifest_desc *descriptor;
257 struct greybus_descriptor_bundle *desc_bundle;
258 struct gb_bundle *bundle;
261 /* Find an bundle descriptor */
262 list_for_each_entry(descriptor, &intf->manifest_descs, links) {
263 if (descriptor->type == GREYBUS_TYPE_BUNDLE) {
271 /* Found one. Set up its bundle structure*/
272 desc_bundle = descriptor->data;
273 bundle = gb_bundle_create(intf, desc_bundle->id,
276 return 0; /* Error */
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 */
284 /* Done with this bundle descriptor */
285 release_manifest_descriptor(descriptor);
291 static bool gb_manifest_parse_interface(struct gb_interface *intf,
292 struct manifest_desc *interface_desc)
294 struct greybus_descriptor_interface *desc_intf = interface_desc->data;
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))
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;
308 // Vendor, Product and Unique id must come via control protocol
309 intf->vendor = 0xffff;
310 intf->product = 0x0001;
313 /* Release the interface descriptor, now that we're done with it */
314 release_manifest_descriptor(interface_desc);
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");
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;
334 * Parse a buffer containing a Interface manifest.
336 * If we find anything wrong with the content/format of the buffer
339 * The first requirement is that the manifest's version is
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.
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.
351 * After that we look for the interface's bundles--there must be at
352 * least one of those.
354 * Returns true if parsing was successful, false otherwise.
356 bool gb_manifest_parse(struct gb_interface *intf, void *data, size_t size)
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;
367 /* Manifest descriptor list should be empty here */
368 if (WARN_ON(!list_empty(&intf->manifest_descs)))
371 /* we have to have at _least_ the manifest header */
372 if (size <= sizeof(manifest->header)) {
373 pr_err("short manifest (%zu)\n", size);
377 /* Make sure the size is right */
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);
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);
395 /* OK, find all the descriptors */
396 desc = (struct greybus_descriptor *)(header + 1);
397 size -= sizeof(*header);
401 desc_size = identify_descriptor(intf, desc, size);
406 desc = (struct greybus_descriptor *)((char *)desc + desc_size);
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)
414 interface_desc = descriptor;
417 pr_err("manifest must have 1 interface descriptor (%u found)\n",
423 /* Parse the manifest, starting with the interface descriptor */
424 result = gb_manifest_parse_interface(intf, interface_desc);
427 * We really should have no remaining descriptors, but we
428 * don't know what newer format manifests might leave.
430 if (result && !list_empty(&intf->manifest_descs))
431 pr_info("excess descriptors in interface manifest\n");
433 release_manifest_descriptors(intf);