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 = kzalloc(sizeof(*descriptor), GFP_KERNEL);
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);
141 /* desc_size is is positive and is known to fit in a signed int */
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.
151 * String index 0 is valid (it represents "no string"), and for
152 * that a null pointer is returned.
154 * Otherwise returns a pointer to a newly-allocated copy of the
155 * descriptor string, or an error-coded pointer on failure.
157 static char *gb_string_get(struct gb_interface *intf, u8 string_id)
159 struct greybus_descriptor_string *desc_string;
160 struct manifest_desc *descriptor;
164 /* A zero string id means no string (but no error) */
168 list_for_each_entry(descriptor, &intf->manifest_descs, links) {
170 if (descriptor->type != GREYBUS_TYPE_STRING)
173 desc_string = descriptor->data;
174 if (desc_string->id == string_id) {
180 return ERR_PTR(-ENOENT);
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,
186 return ERR_PTR(-ENOMEM);
187 string[desc_string->length] = '\0';
189 /* Ok we've used this string, so we're done with it */
190 release_manifest_descriptor(descriptor);
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.
200 static u32 gb_manifest_parse_cports(struct gb_interface *intf,
201 struct gb_bundle *bundle)
206 struct manifest_desc *descriptor;
207 struct greybus_descriptor_cport *desc_cport;
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) {
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 */
232 /* Release the cport descriptor */
233 release_manifest_descriptor(descriptor);
240 * Find bundle descriptors in the manifest and set up their data
241 * structures. Returns the number of bundles set up for the
244 static u32 gb_manifest_parse_bundles(struct gb_interface *intf)
249 struct manifest_desc *descriptor;
250 struct greybus_descriptor_bundle *desc_bundle;
251 struct gb_bundle *bundle;
254 /* Find an bundle descriptor */
255 list_for_each_entry(descriptor, &intf->manifest_descs, links) {
256 if (descriptor->type == GREYBUS_TYPE_BUNDLE) {
264 /* Found one. Set up its bundle structure*/
265 desc_bundle = descriptor->data;
266 bundle = gb_bundle_create(intf, desc_bundle->id,
269 return 0; /* Error */
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 */
277 /* Done with this bundle descriptor */
278 release_manifest_descriptor(descriptor);
284 static bool gb_manifest_parse_interface(struct gb_interface *intf,
285 struct manifest_desc *interface_desc)
287 struct greybus_descriptor_interface *desc_intf = interface_desc->data;
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))
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;
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);
304 /* Release the interface descriptor, now that we're done with it */
305 release_manifest_descriptor(interface_desc);
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");
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;
325 * Parse a buffer containing a Interface manifest.
327 * If we find anything wrong with the content/format of the buffer
330 * The first requirement is that the manifest's version is
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.
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.
342 * After that we look for the interface's bundles--there must be at
343 * least one of those.
345 * Returns true if parsing was successful, false otherwise.
347 bool gb_manifest_parse(struct gb_interface *intf, void *data, size_t size)
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;
358 /* Manifest descriptor list should be empty here */
359 if (WARN_ON(!list_empty(&intf->manifest_descs)))
362 /* we have to have at _least_ the manifest header */
363 if (size <= sizeof(manifest->header)) {
364 pr_err("short manifest (%zu)\n", size);
368 /* Make sure the size is right */
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);
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);
386 /* OK, find all the descriptors */
387 desc = (struct greybus_descriptor *)(header + 1);
388 size -= sizeof(*header);
392 desc_size = identify_descriptor(intf, desc, size);
397 desc = (struct greybus_descriptor *)((char *)desc + desc_size);
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)
405 interface_desc = descriptor;
408 pr_err("manifest must have 1 interface descriptor (%u found)\n",
414 /* Parse the manifest, starting with the interface descriptor */
415 result = gb_manifest_parse_interface(intf, interface_desc);
418 * We really should have no remaining descriptors, but we
419 * don't know what newer format manifests might leave.
421 if (result && !list_empty(&intf->manifest_descs))
422 pr_info("excess descriptors in interface manifest\n");
424 release_manifest_descriptors(intf);