blob: 7b903770a68461e48e8ddf5e31d3172cafdf2172 [file] [log] [blame]
Alex Elderb09c94a2014-10-01 21:54:16 -05001/*
Viresh Kumara93db2d2015-04-01 20:32:02 +05302 * Greybus manifest parsing
Alex Elderb09c94a2014-10-01 21:54:16 -05003 *
Alex Elderd8187aa2015-03-27 15:06:24 -05004 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
Alex Elderb09c94a2014-10-01 21:54:16 -05006 *
7 * Released under the GPLv2 only.
8 */
9
Alex Elderb09c94a2014-10-01 21:54:16 -050010#include "greybus.h"
11
Viresh Kumar19b3b2c2015-03-24 17:08:13 +053012static const char *get_descriptor_type_string(u8 type)
13{
Quentin Lambert3dd22262016-09-27 11:42:08 +020014 switch (type) {
Viresh Kumar19b3b2c2015-03-24 17:08:13 +053015 case GREYBUS_TYPE_INVALID:
16 return "invalid";
Viresh Kumar19b3b2c2015-03-24 17:08:13 +053017 case GREYBUS_TYPE_STRING:
18 return "string";
19 case GREYBUS_TYPE_INTERFACE:
20 return "interface";
21 case GREYBUS_TYPE_CPORT:
22 return "cport";
Viresh Kumar83a0cb52015-04-01 20:31:59 +053023 case GREYBUS_TYPE_BUNDLE:
24 return "bundle";
Viresh Kumar19b3b2c2015-03-24 17:08:13 +053025 default:
26 WARN_ON(1);
27 return "unknown";
28 }
29}
30
Alex Elderb09c94a2014-10-01 21:54:16 -050031/*
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
Viresh Kumara93db2d2015-04-01 20:32:02 +053035 * the interface descriptor). As each is processed we remove it from
Alex Elderb09c94a2014-10-01 21:54:16 -050036 * the list. When we're done the list should (probably) be empty.
37 */
38struct manifest_desc {
39 struct list_head links;
40
41 size_t size;
42 void *data;
43 enum greybus_descriptor_type type;
44};
45
Alex Elderb09c94a2014-10-01 21:54:16 -050046static void release_manifest_descriptor(struct manifest_desc *descriptor)
47{
48 list_del(&descriptor->links);
49 kfree(descriptor);
50}
51
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -080052static void release_manifest_descriptors(struct gb_interface *intf)
Alex Elderb09c94a2014-10-01 21:54:16 -050053{
54 struct manifest_desc *descriptor;
55 struct manifest_desc *next;
56
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -080057 list_for_each_entry_safe(descriptor, next, &intf->manifest_descs, links)
Alex Elderb09c94a2014-10-01 21:54:16 -050058 release_manifest_descriptor(descriptor);
59}
60
Johan Hovoldf2152eb2015-11-25 15:59:25 +010061static void release_cport_descriptors(struct list_head *head, u8 bundle_id)
62{
63 struct manifest_desc *desc, *tmp;
64 struct greybus_descriptor_cport *desc_cport;
65
66 list_for_each_entry_safe(desc, tmp, head, links) {
67 desc_cport = desc->data;
68
69 if (desc->type != GREYBUS_TYPE_CPORT)
70 continue;
71
72 if (desc_cport->bundle == bundle_id)
73 release_manifest_descriptor(desc);
74 }
75}
76
Rui Miguel Silva5c864e72015-11-16 19:23:25 +000077static struct manifest_desc *get_next_bundle_desc(struct gb_interface *intf)
78{
79 struct manifest_desc *descriptor;
80 struct manifest_desc *next;
81
82 list_for_each_entry_safe(descriptor, next, &intf->manifest_descs, links)
83 if (descriptor->type == GREYBUS_TYPE_BUNDLE)
84 return descriptor;
85
86 return NULL;
87}
88
Alex Elderb09c94a2014-10-01 21:54:16 -050089/*
90 * Validate the given descriptor. Its reported size must fit within
Viresh Kumar696e0cc2014-11-21 11:26:30 +053091 * the number of bytes remaining, and it must have a recognized
Alex Elderb09c94a2014-10-01 21:54:16 -050092 * 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
94 * of the format.)
95 *
Alex Elderd393c982015-06-09 17:42:53 -050096 * Returns the (non-zero) number of bytes consumed by the descriptor,
97 * or a negative errno.
Alex Elderb09c94a2014-10-01 21:54:16 -050098 */
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -080099static int identify_descriptor(struct gb_interface *intf,
100 struct greybus_descriptor *desc, size_t size)
Alex Elderb09c94a2014-10-01 21:54:16 -0500101{
102 struct greybus_descriptor_header *desc_header = &desc->header;
103 struct manifest_desc *descriptor;
Alex Elderd8187aa2015-03-27 15:06:24 -0500104 size_t desc_size;
Alex Elderb09c94a2014-10-01 21:54:16 -0500105 size_t expected_size;
106
107 if (size < sizeof(*desc_header)) {
Johan Hovolda0b55422016-02-11 13:52:46 +0100108 dev_err(&intf->dev, "manifest too small (%zu < %zu)\n",
Alex Elderd51c0ff2015-06-08 12:05:14 -0500109 size, sizeof(*desc_header));
Alex Elderb09c94a2014-10-01 21:54:16 -0500110 return -EINVAL; /* Must at least have header */
111 }
112
Alex Elderd8187aa2015-03-27 15:06:24 -0500113 desc_size = le16_to_cpu(desc_header->size);
114 if (desc_size > size) {
Johan Hovolda0b55422016-02-11 13:52:46 +0100115 dev_err(&intf->dev, "descriptor too big (%zu > %zu)\n",
116 desc_size, size);
Alex Elderb09c94a2014-10-01 21:54:16 -0500117 return -EINVAL;
118 }
119
Viresh Kumar19b3b2c2015-03-24 17:08:13 +0530120 /* Descriptor needs to at least have a header */
121 expected_size = sizeof(*desc_header);
122
Alex Elderb09c94a2014-10-01 21:54:16 -0500123 switch (desc_header->type) {
Alex Elderb09c94a2014-10-01 21:54:16 -0500124 case GREYBUS_TYPE_STRING:
Alex Elderb09c94a2014-10-01 21:54:16 -0500125 expected_size += sizeof(struct greybus_descriptor_string);
Viresh Kumar19b3b2c2015-03-24 17:08:13 +0530126 expected_size += desc->string.length;
Viresh Kumarfa2fbf12015-04-28 19:51:35 +0530127
128 /* String descriptors are padded to 4 byte boundaries */
129 expected_size = ALIGN(expected_size, 4);
Alex Elderb09c94a2014-10-01 21:54:16 -0500130 break;
Alex Elder63cc9322014-10-02 12:30:02 -0500131 case GREYBUS_TYPE_INTERFACE:
Viresh Kumara93db2d2015-04-01 20:32:02 +0530132 expected_size += sizeof(struct greybus_descriptor_interface);
Alex Elder63cc9322014-10-02 12:30:02 -0500133 break;
Viresh Kumar7c183f72015-04-01 20:32:00 +0530134 case GREYBUS_TYPE_BUNDLE:
135 expected_size += sizeof(struct greybus_descriptor_bundle);
136 break;
Alex Elderb09c94a2014-10-01 21:54:16 -0500137 case GREYBUS_TYPE_CPORT:
Viresh Kumar19b3b2c2015-03-24 17:08:13 +0530138 expected_size += sizeof(struct greybus_descriptor_cport);
Alex Elderb09c94a2014-10-01 21:54:16 -0500139 break;
140 case GREYBUS_TYPE_INVALID:
141 default:
Johan Hovolda0b55422016-02-11 13:52:46 +0100142 dev_err(&intf->dev, "invalid descriptor type (%u)\n",
143 desc_header->type);
Alex Elderb09c94a2014-10-01 21:54:16 -0500144 return -EINVAL;
145 }
146
Viresh Kumar19b3b2c2015-03-24 17:08:13 +0530147 if (desc_size < expected_size) {
Johan Hovolda0b55422016-02-11 13:52:46 +0100148 dev_err(&intf->dev, "%s descriptor too small (%zu < %zu)\n",
149 get_descriptor_type_string(desc_header->type),
150 desc_size, expected_size);
Viresh Kumar19b3b2c2015-03-24 17:08:13 +0530151 return -EINVAL;
152 }
153
Viresh Kumar55b930c2015-04-29 11:02:08 +0530154 /* Descriptor bigger than what we expect */
155 if (desc_size > expected_size) {
Johan Hovolda0b55422016-02-11 13:52:46 +0100156 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);
Viresh Kumar55b930c2015-04-29 11:02:08 +0530159 }
160
Alex Elderb09c94a2014-10-01 21:54:16 -0500161 descriptor = kzalloc(sizeof(*descriptor), GFP_KERNEL);
162 if (!descriptor)
163 return -ENOMEM;
164
165 descriptor->size = desc_size;
Alex Elderd393c982015-06-09 17:42:53 -0500166 descriptor->data = (char *)desc + sizeof(*desc_header);
Alex Elderb09c94a2014-10-01 21:54:16 -0500167 descriptor->type = desc_header->type;
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -0800168 list_add_tail(&descriptor->links, &intf->manifest_descs);
Alex Elderb09c94a2014-10-01 21:54:16 -0500169
Alex Elderd393c982015-06-09 17:42:53 -0500170 /* desc_size is positive and is known to fit in a signed int */
Alex Elderd8187aa2015-03-27 15:06:24 -0500171
Alex Elderb09c94a2014-10-01 21:54:16 -0500172 return desc_size;
173}
174
175/*
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.
179 *
180 * String index 0 is valid (it represents "no string"), and for
181 * that a null pointer is returned.
182 *
183 * Otherwise returns a pointer to a newly-allocated copy of the
184 * descriptor string, or an error-coded pointer on failure.
185 */
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -0800186static char *gb_string_get(struct gb_interface *intf, u8 string_id)
Alex Elderb09c94a2014-10-01 21:54:16 -0500187{
188 struct greybus_descriptor_string *desc_string;
189 struct manifest_desc *descriptor;
190 bool found = false;
191 char *string;
192
193 /* A zero string id means no string (but no error) */
194 if (!string_id)
195 return NULL;
196
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -0800197 list_for_each_entry(descriptor, &intf->manifest_descs, links) {
Alex Elderb09c94a2014-10-01 21:54:16 -0500198 if (descriptor->type != GREYBUS_TYPE_STRING)
199 continue;
200
Matt Porter7a13e2f2014-10-06 09:58:44 -0400201 desc_string = descriptor->data;
Alex Elderb09c94a2014-10-01 21:54:16 -0500202 if (desc_string->id == string_id) {
203 found = true;
204 break;
205 }
206 }
207 if (!found)
208 return ERR_PTR(-ENOENT);
209
210 /* Allocate an extra byte so we can guarantee it's NUL-terminated */
Alex Elderd393c982015-06-09 17:42:53 -0500211 string = kmemdup(&desc_string->string, desc_string->length + 1,
Alex Elderb09c94a2014-10-01 21:54:16 -0500212 GFP_KERNEL);
213 if (!string)
214 return ERR_PTR(-ENOMEM);
215 string[desc_string->length] = '\0';
216
217 /* Ok we've used this string, so we're done with it */
218 release_manifest_descriptor(descriptor);
219
220 return string;
221}
222
Alex Elderd88bfb52014-10-01 21:54:17 -0500223/*
Alex Elderd393c982015-06-09 17:42:53 -0500224 * 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.
Alex Elderc095bbc2014-10-01 21:54:18 -0500228 */
Alex Elderc46839d2015-06-09 17:42:54 -0500229static u32 gb_manifest_parse_cports(struct gb_bundle *bundle)
Alex Elderc095bbc2014-10-01 21:54:18 -0500230{
Alex Elderc46839d2015-06-09 17:42:54 -0500231 struct gb_interface *intf = bundle->intf;
Johan Hovold98fdf5a2016-01-21 17:34:09 +0100232 struct greybus_descriptor_cport *desc_cport;
Johan Hovoldd6fba3d2016-01-21 17:34:10 +0100233 struct manifest_desc *desc, *next, *tmp;
Johan Hovold98fdf5a2016-01-21 17:34:09 +0100234 LIST_HEAD(list);
Alex Eldera6b13eb2015-06-09 17:42:55 -0500235 u8 bundle_id = bundle->id;
Bryan O'Donoghueb38fe342015-07-21 09:10:27 +0100236 u16 cport_id;
Alex Elderc095bbc2014-10-01 21:54:18 -0500237 u32 count = 0;
Johan Hovold98fdf5a2016-01-21 17:34:09 +0100238 int i;
Alex Elderc095bbc2014-10-01 21:54:18 -0500239
Alex Eldera6b13eb2015-06-09 17:42:55 -0500240 /* Set up all cport descriptors associated with this bundle */
241 list_for_each_entry_safe(desc, next, &intf->manifest_descs, links) {
Alex Eldera6b13eb2015-06-09 17:42:55 -0500242 if (desc->type != GREYBUS_TYPE_CPORT)
243 continue;
244
245 desc_cport = desc->data;
246 if (desc_cport->bundle != bundle_id)
247 continue;
Alex Elderc095bbc2014-10-01 21:54:18 -0500248
Alex Elderfb690ca2015-06-13 11:02:09 -0500249 cport_id = le16_to_cpu(desc_cport->id);
250 if (cport_id > CPORT_ID_MAX)
Bryan O'Donoghueb38fe342015-07-21 09:10:27 +0100251 goto exit;
Alex Elderfb690ca2015-06-13 11:02:09 -0500252
Viresh Kumar42830f72016-06-11 08:01:01 +0530253 /* 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",
256 cport_id);
257 goto exit;
258 }
259
Johan Hovoldd6fba3d2016-01-21 17:34:10 +0100260 /*
261 * Found one, move it to our temporary list after checking for
262 * duplicates.
263 */
264 list_for_each_entry(tmp, &list, links) {
265 desc_cport = tmp->data;
Greg Kroah-Hartmand1a9c052016-02-02 21:31:19 -0800266 if (cport_id == le16_to_cpu(desc_cport->id)) {
Johan Hovoldd6fba3d2016-01-21 17:34:10 +0100267 dev_err(&bundle->dev,
268 "duplicate CPort %u found\n",
269 cport_id);
270 goto exit;
271 }
272 }
Viresh Kumar4a7908c2016-02-12 21:48:03 +0530273 list_move_tail(&desc->links, &list);
Alex Elderc095bbc2014-10-01 21:54:18 -0500274 count++;
Johan Hovold98fdf5a2016-01-21 17:34:09 +0100275 }
276
277 if (!count)
278 return 0;
279
280 bundle->cport_desc = kcalloc(count, sizeof(*bundle->cport_desc),
281 GFP_KERNEL);
282 if (!bundle->cport_desc)
283 goto exit;
284
285 bundle->num_cports = count;
286
287 i = 0;
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));
Alex Eldera6b13eb2015-06-09 17:42:55 -0500292
Alex Elderc095bbc2014-10-01 21:54:18 -0500293 /* Release the cport descriptor */
Alex Eldera6b13eb2015-06-09 17:42:55 -0500294 release_manifest_descriptor(desc);
Alex Elderc095bbc2014-10-01 21:54:18 -0500295 }
296
297 return count;
Bryan O'Donoghueb38fe342015-07-21 09:10:27 +0100298exit:
Johan Hovold98fdf5a2016-01-21 17:34:09 +0100299 release_cport_descriptors(&list, bundle_id);
Viresh Kumar4317f872015-09-07 16:01:20 +0530300 /*
301 * Free all cports for this bundle to avoid 'excess descriptors'
302 * warnings.
303 */
Johan Hovoldf2152eb2015-11-25 15:59:25 +0100304 release_cport_descriptors(&intf->manifest_descs, bundle_id);
Viresh Kumar4317f872015-09-07 16:01:20 +0530305
Alex Elder52e8ce32015-06-12 10:21:09 -0500306 return 0; /* Error; count should also be 0 */
Alex Elderc095bbc2014-10-01 21:54:18 -0500307}
308
309/*
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500310 * Find bundle descriptors in the manifest and set up their data
311 * structures. Returns the number of bundles set up for the
Viresh Kumar7c183f72015-04-01 20:32:00 +0530312 * given interface.
Alex Elderd88bfb52014-10-01 21:54:17 -0500313 */
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800314static u32 gb_manifest_parse_bundles(struct gb_interface *intf)
Alex Elderd88bfb52014-10-01 21:54:17 -0500315{
Alex Elderc27a2532015-06-09 17:42:56 -0500316 struct manifest_desc *desc;
Alex Elder2a64fb02015-06-12 10:21:12 -0500317 struct gb_bundle *bundle;
318 struct gb_bundle *bundle_next;
Alex Elderd88bfb52014-10-01 21:54:17 -0500319 u32 count = 0;
Viresh Kumar98d7fbc2015-09-07 16:01:19 +0530320 u8 bundle_id;
Johan Hovold47091af2015-11-25 15:59:26 +0100321 u8 class;
Alex Elderd88bfb52014-10-01 21:54:17 -0500322
Rui Miguel Silva5c864e72015-11-16 19:23:25 +0000323 while ((desc = get_next_bundle_desc(intf))) {
Viresh Kumar7c183f72015-04-01 20:32:00 +0530324 struct greybus_descriptor_bundle *desc_bundle;
Alex Elderd88bfb52014-10-01 21:54:17 -0500325
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500326 /* Found one. Set up its bundle structure*/
Alex Elderc27a2532015-06-09 17:42:56 -0500327 desc_bundle = desc->data;
Viresh Kumar98d7fbc2015-09-07 16:01:19 +0530328 bundle_id = desc_bundle->id;
Johan Hovold47091af2015-11-25 15:59:26 +0100329 class = desc_bundle->class;
Viresh Kumar6c68da22015-06-22 16:42:27 +0530330
Johan Hovold47091af2015-11-25 15:59:26 +0100331 /* Done with this bundle descriptor */
332 release_manifest_descriptor(desc);
333
334 /* Ignore any legacy control bundles */
Viresh Kumar98d7fbc2015-09-07 16:01:19 +0530335 if (bundle_id == GB_CONTROL_BUNDLE_ID) {
Johan Hovold47091af2015-11-25 15:59:26 +0100336 dev_dbg(&intf->dev, "%s - ignoring control bundle\n",
337 __func__);
338 release_cport_descriptors(&intf->manifest_descs,
339 bundle_id);
340 continue;
Viresh Kumar6c68da22015-06-22 16:42:27 +0530341 }
342
Viresh Kumar730a2f62015-06-22 16:42:30 +0530343 /* Nothing else should have its class set to control class */
Johan Hovold47091af2015-11-25 15:59:26 +0100344 if (class == GREYBUS_CLASS_CONTROL) {
Bryan O'Donoghueb38fe342015-07-21 09:10:27 +0100345 dev_err(&intf->dev,
Johan Hovold100e9002015-12-07 15:05:38 +0100346 "bundle %u cannot use control class\n",
Viresh Kumar98d7fbc2015-09-07 16:01:19 +0530347 bundle_id);
Bryan O'Donoghueb38fe342015-07-21 09:10:27 +0100348 goto cleanup;
349 }
Viresh Kumar730a2f62015-06-22 16:42:30 +0530350
Johan Hovold47091af2015-11-25 15:59:26 +0100351 bundle = gb_bundle_create(intf, bundle_id, class);
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500352 if (!bundle)
Alex Elder2a64fb02015-06-12 10:21:12 -0500353 goto cleanup;
Alex Elderc095bbc2014-10-01 21:54:18 -0500354
Viresh Kumar98d7fbc2015-09-07 16:01:19 +0530355 /*
356 * Now go set up this bundle's functions and cports.
357 *
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.
362 *
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.
366 *
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.
Viresh Kumar98d7fbc2015-09-07 16:01:19 +0530371 */
372 if (!gb_manifest_parse_cports(bundle)) {
Viresh Kumar98d7fbc2015-09-07 16:01:19 +0530373 gb_bundle_destroy(bundle);
374 continue;
375 }
376
Bryan O'Donoghueb38fe342015-07-21 09:10:27 +0100377 count++;
Alex Elderd88bfb52014-10-01 21:54:17 -0500378 }
379
380 return count;
Alex Elder2a64fb02015-06-12 10:21:12 -0500381cleanup:
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);
385 count--;
386 }
387 return 0; /* Error; count should also be 0 */
Alex Elderd88bfb52014-10-01 21:54:17 -0500388}
389
Viresh Kumara93db2d2015-04-01 20:32:02 +0530390static bool gb_manifest_parse_interface(struct gb_interface *intf,
391 struct manifest_desc *interface_desc)
Alex Elderb09c94a2014-10-01 21:54:16 -0500392{
Viresh Kumara93db2d2015-04-01 20:32:02 +0530393 struct greybus_descriptor_interface *desc_intf = interface_desc->data;
Johan Hovold7c8eb122016-04-13 19:19:04 +0200394 struct gb_control *control = intf->control;
Johan Hovold7d963cb2016-04-13 19:18:55 +0200395 char *str;
Alex Elderb09c94a2014-10-01 21:54:16 -0500396
397 /* Handle the strings first--they can fail */
Johan Hovold7d963cb2016-04-13 19:18:55 +0200398 str = gb_string_get(intf, desc_intf->vendor_stringid);
399 if (IS_ERR(str))
Alex Elder937d0da2014-10-03 14:14:25 -0500400 return false;
Johan Hovold7c8eb122016-04-13 19:19:04 +0200401 control->vendor_string = str;
Alex Elder937d0da2014-10-03 14:14:25 -0500402
Johan Hovold7d963cb2016-04-13 19:18:55 +0200403 str = gb_string_get(intf, desc_intf->product_stringid);
404 if (IS_ERR(str))
Viresh Kumar50fc08f2014-11-13 18:14:32 +0530405 goto out_free_vendor_string;
Johan Hovold7c8eb122016-04-13 19:19:04 +0200406 control->product_string = str;
Alex Elderb09c94a2014-10-01 21:54:16 -0500407
Bryan O'Donoghue8c81d462016-05-15 19:37:49 +0100408 /* Assign feature flags communicated via manifest */
409 intf->features = desc_intf->features;
410
Viresh Kumara93db2d2015-04-01 20:32:02 +0530411 /* Release the interface descriptor, now that we're done with it */
412 release_manifest_descriptor(interface_desc);
Alex Elderb09c94a2014-10-01 21:54:16 -0500413
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500414 /* An interface must have at least one bundle descriptor */
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800415 if (!gb_manifest_parse_bundles(intf)) {
Bryan O'Donoghue09fb10f2015-07-21 09:10:28 +0100416 dev_err(&intf->dev, "manifest bundle descriptors not valid\n");
Alex Elder937d0da2014-10-03 14:14:25 -0500417 goto out_err;
Alex Elderd88bfb52014-10-01 21:54:17 -0500418 }
419
Alex Elder937d0da2014-10-03 14:14:25 -0500420 return true;
421out_err:
Johan Hovold7c8eb122016-04-13 19:19:04 +0200422 kfree(control->product_string);
423 control->product_string = NULL;
Viresh Kumar50fc08f2014-11-13 18:14:32 +0530424out_free_vendor_string:
Johan Hovold7c8eb122016-04-13 19:19:04 +0200425 kfree(control->vendor_string);
426 control->vendor_string = NULL;
Alex Elder937d0da2014-10-03 14:14:25 -0500427
428 return false;
Alex Elderb09c94a2014-10-01 21:54:16 -0500429}
430
431/*
Alex Elderd393c982015-06-09 17:42:53 -0500432 * Parse a buffer containing an interface manifest.
Alex Elderb09c94a2014-10-01 21:54:16 -0500433 *
434 * If we find anything wrong with the content/format of the buffer
435 * we reject it.
436 *
437 * The first requirement is that the manifest's version is
438 * one we can parse.
439 *
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.
443 *
Alex Elderd393c982015-06-09 17:42:53 -0500444 * Next we scan the descriptors, looking for an interface descriptor;
Alex Elderb09c94a2014-10-01 21:54:16 -0500445 * 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.
448 *
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800449 * After that we look for the interface's bundles--there must be at
Alex Elderb09c94a2014-10-01 21:54:16 -0500450 * least one of those.
451 *
Alex Elder937d0da2014-10-03 14:14:25 -0500452 * Returns true if parsing was successful, false otherwise.
Alex Elderb09c94a2014-10-01 21:54:16 -0500453 */
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800454bool gb_manifest_parse(struct gb_interface *intf, void *data, size_t size)
Alex Elderb09c94a2014-10-01 21:54:16 -0500455{
456 struct greybus_manifest *manifest;
457 struct greybus_manifest_header *header;
458 struct greybus_descriptor *desc;
459 struct manifest_desc *descriptor;
Viresh Kumara93db2d2015-04-01 20:32:02 +0530460 struct manifest_desc *interface_desc = NULL;
Alex Elderb09c94a2014-10-01 21:54:16 -0500461 u16 manifest_size;
462 u32 found = 0;
Viresh Kumar43d94312014-11-13 18:14:30 +0530463 bool result;
Alex Elderb09c94a2014-10-01 21:54:16 -0500464
Viresh Kumar1dd90df2014-11-14 17:25:03 +0530465 /* Manifest descriptor list should be empty here */
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -0800466 if (WARN_ON(!list_empty(&intf->manifest_descs)))
Viresh Kumar1dd90df2014-11-14 17:25:03 +0530467 return false;
468
Alex Elderb09c94a2014-10-01 21:54:16 -0500469 /* we have to have at _least_ the manifest header */
Alex Elderd393c982015-06-09 17:42:53 -0500470 if (size < sizeof(*header)) {
Johan Hovolda0b55422016-02-11 13:52:46 +0100471 dev_err(&intf->dev, "short manifest (%zu < %zu)\n",
472 size, sizeof(*header));
Alex Elder937d0da2014-10-03 14:14:25 -0500473 return false;
Alex Elderb09c94a2014-10-01 21:54:16 -0500474 }
475
476 /* Make sure the size is right */
477 manifest = data;
478 header = &manifest->header;
479 manifest_size = le16_to_cpu(header->size);
480 if (manifest_size != size) {
Johan Hovolda0b55422016-02-11 13:52:46 +0100481 dev_err(&intf->dev, "manifest size mismatch (%zu != %u)\n",
482 size, manifest_size);
Alex Elder937d0da2014-10-03 14:14:25 -0500483 return false;
Alex Elderb09c94a2014-10-01 21:54:16 -0500484 }
485
486 /* Validate major/minor number */
487 if (header->version_major > GREYBUS_VERSION_MAJOR) {
Johan Hovolda0b55422016-02-11 13:52:46 +0100488 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);
Alex Elder937d0da2014-10-03 14:14:25 -0500491 return false;
Alex Elderb09c94a2014-10-01 21:54:16 -0500492 }
493
494 /* OK, find all the descriptors */
Sachin Pandharefc25d902015-11-24 07:59:10 +0530495 desc = manifest->descriptors;
Alex Elderb09c94a2014-10-01 21:54:16 -0500496 size -= sizeof(*header);
497 while (size) {
498 int desc_size;
499
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -0800500 desc_size = identify_descriptor(intf, desc, size);
Viresh Kumar13fe6a92015-03-24 17:08:14 +0530501 if (desc_size < 0) {
Matt Porterff8aed52014-10-06 13:46:36 -0400502 result = false;
Alex Elder937d0da2014-10-03 14:14:25 -0500503 goto out;
Alex Elderb09c94a2014-10-01 21:54:16 -0500504 }
505 desc = (struct greybus_descriptor *)((char *)desc + desc_size);
506 size -= desc_size;
Greg Kroah-Hartman86bf33a2014-11-14 14:37:56 -0800507 }
Alex Elderb09c94a2014-10-01 21:54:16 -0500508
Viresh Kumara93db2d2015-04-01 20:32:02 +0530509 /* There must be a single interface descriptor */
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -0800510 list_for_each_entry(descriptor, &intf->manifest_descs, links) {
Viresh Kumara93db2d2015-04-01 20:32:02 +0530511 if (descriptor->type == GREYBUS_TYPE_INTERFACE)
Greg Kroah-Hartman86bf33a2014-11-14 14:37:56 -0800512 if (!found++)
Viresh Kumara93db2d2015-04-01 20:32:02 +0530513 interface_desc = descriptor;
Greg Kroah-Hartman86bf33a2014-11-14 14:37:56 -0800514 }
515 if (found != 1) {
Johan Hovolda0b55422016-02-11 13:52:46 +0100516 dev_err(&intf->dev, "manifest must have 1 interface descriptor (%u found)\n",
517 found);
Greg Kroah-Hartman86bf33a2014-11-14 14:37:56 -0800518 result = false;
519 goto out;
Alex Elderb09c94a2014-10-01 21:54:16 -0500520 }
521
Viresh Kumara93db2d2015-04-01 20:32:02 +0530522 /* Parse the manifest, starting with the interface descriptor */
523 result = gb_manifest_parse_interface(intf, interface_desc);
Alex Elderb09c94a2014-10-01 21:54:16 -0500524
525 /*
526 * We really should have no remaining descriptors, but we
527 * don't know what newer format manifests might leave.
528 */
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -0800529 if (result && !list_empty(&intf->manifest_descs))
Johan Hovolda0b55422016-02-11 13:52:46 +0100530 dev_info(&intf->dev, "excess descriptors in interface manifest\n");
Alex Elder937d0da2014-10-03 14:14:25 -0500531out:
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -0800532 release_manifest_descriptors(intf);
Alex Elderb09c94a2014-10-01 21:54:16 -0500533
Matt Porterff8aed52014-10-06 13:46:36 -0400534 return result;
Alex Elderb09c94a2014-10-01 21:54:16 -0500535}