blob: dd7040697bde9fcdbb17527b2759a16d4e94d651 [file] [log] [blame]
Greg Kroah-Hartmaneb50fd32017-11-07 14:58:41 +01001// SPDX-License-Identifier: GPL-2.0
Alex Elderb09c94a2014-10-01 21:54:16 -05002/*
Viresh Kumara93db2d2015-04-01 20:32:02 +05303 * Greybus manifest parsing
Alex Elderb09c94a2014-10-01 21:54:16 -05004 *
Alex Elderd8187aa2015-03-27 15:06:24 -05005 * Copyright 2014-2015 Google Inc.
6 * Copyright 2014-2015 Linaro Ltd.
Alex Elderb09c94a2014-10-01 21:54:16 -05007 */
8
Greg Kroah-Hartmanec0ad862019-08-25 07:54:27 +02009#include <linux/greybus.h>
Alex Elderb09c94a2014-10-01 21:54:16 -050010
Viresh Kumar19b3b2c2015-03-24 17:08:13 +053011static const char *get_descriptor_type_string(u8 type)
12{
Quentin Lambert3dd22262016-09-27 11:42:08 +020013 switch (type) {
Viresh Kumar19b3b2c2015-03-24 17:08:13 +053014 case GREYBUS_TYPE_INVALID:
15 return "invalid";
Viresh Kumar19b3b2c2015-03-24 17:08:13 +053016 case GREYBUS_TYPE_STRING:
17 return "string";
18 case GREYBUS_TYPE_INTERFACE:
19 return "interface";
20 case GREYBUS_TYPE_CPORT:
21 return "cport";
Viresh Kumar83a0cb52015-04-01 20:31:59 +053022 case GREYBUS_TYPE_BUNDLE:
23 return "bundle";
Viresh Kumar19b3b2c2015-03-24 17:08:13 +053024 default:
25 WARN_ON(1);
26 return "unknown";
27 }
28}
29
Alex Elderb09c94a2014-10-01 21:54:16 -050030/*
31 * We scan the manifest once to identify where all the descriptors
32 * are. The result is a list of these manifest_desc structures. We
33 * then pick through them for what we're looking for (starting with
Viresh Kumara93db2d2015-04-01 20:32:02 +053034 * the interface descriptor). As each is processed we remove it from
Alex Elderb09c94a2014-10-01 21:54:16 -050035 * the list. When we're done the list should (probably) be empty.
36 */
37struct manifest_desc {
38 struct list_head links;
39
40 size_t size;
41 void *data;
42 enum greybus_descriptor_type type;
43};
44
Alex Elderb09c94a2014-10-01 21:54:16 -050045static void release_manifest_descriptor(struct manifest_desc *descriptor)
46{
47 list_del(&descriptor->links);
48 kfree(descriptor);
49}
50
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -080051static void release_manifest_descriptors(struct gb_interface *intf)
Alex Elderb09c94a2014-10-01 21:54:16 -050052{
53 struct manifest_desc *descriptor;
54 struct manifest_desc *next;
55
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -080056 list_for_each_entry_safe(descriptor, next, &intf->manifest_descs, links)
Alex Elderb09c94a2014-10-01 21:54:16 -050057 release_manifest_descriptor(descriptor);
58}
59
Johan Hovoldf2152eb2015-11-25 15:59:25 +010060static void release_cport_descriptors(struct list_head *head, u8 bundle_id)
61{
62 struct manifest_desc *desc, *tmp;
63 struct greybus_descriptor_cport *desc_cport;
64
65 list_for_each_entry_safe(desc, tmp, head, links) {
66 desc_cport = desc->data;
67
68 if (desc->type != GREYBUS_TYPE_CPORT)
69 continue;
70
71 if (desc_cport->bundle == bundle_id)
72 release_manifest_descriptor(desc);
73 }
74}
75
Rui Miguel Silva5c864e72015-11-16 19:23:25 +000076static struct manifest_desc *get_next_bundle_desc(struct gb_interface *intf)
77{
78 struct manifest_desc *descriptor;
79 struct manifest_desc *next;
80
81 list_for_each_entry_safe(descriptor, next, &intf->manifest_descs, links)
82 if (descriptor->type == GREYBUS_TYPE_BUNDLE)
83 return descriptor;
84
85 return NULL;
86}
87
Alex Elderb09c94a2014-10-01 21:54:16 -050088/*
89 * Validate the given descriptor. Its reported size must fit within
Viresh Kumar696e0cc2014-11-21 11:26:30 +053090 * the number of bytes remaining, and it must have a recognized
Alex Elderb09c94a2014-10-01 21:54:16 -050091 * type. Check that the reported size is at least as big as what
92 * we expect to see. (It could be bigger, perhaps for a new version
93 * of the format.)
94 *
Alex Elderd393c982015-06-09 17:42:53 -050095 * Returns the (non-zero) number of bytes consumed by the descriptor,
96 * or a negative errno.
Alex Elderb09c94a2014-10-01 21:54:16 -050097 */
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -080098static int identify_descriptor(struct gb_interface *intf,
99 struct greybus_descriptor *desc, size_t size)
Alex Elderb09c94a2014-10-01 21:54:16 -0500100{
101 struct greybus_descriptor_header *desc_header = &desc->header;
102 struct manifest_desc *descriptor;
Alex Elderd8187aa2015-03-27 15:06:24 -0500103 size_t desc_size;
Alex Elderb09c94a2014-10-01 21:54:16 -0500104 size_t expected_size;
105
106 if (size < sizeof(*desc_header)) {
Greg Kroah-Hartmanee989b02019-08-25 07:54:24 +0200107 dev_err(&intf->dev, "manifest too small (%zu < %zu)\n", size,
108 sizeof(*desc_header));
Alex Elderb09c94a2014-10-01 21:54:16 -0500109 return -EINVAL; /* Must at least have header */
110 }
111
Alex Elderd8187aa2015-03-27 15:06:24 -0500112 desc_size = le16_to_cpu(desc_header->size);
113 if (desc_size > size) {
Johan Hovolda0b55422016-02-11 13:52:46 +0100114 dev_err(&intf->dev, "descriptor too big (%zu > %zu)\n",
Greg Kroah-Hartmanee989b02019-08-25 07:54:24 +0200115 desc_size, size);
Alex Elderb09c94a2014-10-01 21:54:16 -0500116 return -EINVAL;
117 }
118
Viresh Kumar19b3b2c2015-03-24 17:08:13 +0530119 /* Descriptor needs to at least have a header */
120 expected_size = sizeof(*desc_header);
121
Alex Elderb09c94a2014-10-01 21:54:16 -0500122 switch (desc_header->type) {
Alex Elderb09c94a2014-10-01 21:54:16 -0500123 case GREYBUS_TYPE_STRING:
Alex Elderb09c94a2014-10-01 21:54:16 -0500124 expected_size += sizeof(struct greybus_descriptor_string);
Viresh Kumar19b3b2c2015-03-24 17:08:13 +0530125 expected_size += desc->string.length;
Viresh Kumarfa2fbf12015-04-28 19:51:35 +0530126
127 /* String descriptors are padded to 4 byte boundaries */
128 expected_size = ALIGN(expected_size, 4);
Alex Elderb09c94a2014-10-01 21:54:16 -0500129 break;
Alex Elder63cc9322014-10-02 12:30:02 -0500130 case GREYBUS_TYPE_INTERFACE:
Viresh Kumara93db2d2015-04-01 20:32:02 +0530131 expected_size += sizeof(struct greybus_descriptor_interface);
Alex Elder63cc9322014-10-02 12:30:02 -0500132 break;
Viresh Kumar7c183f72015-04-01 20:32:00 +0530133 case GREYBUS_TYPE_BUNDLE:
134 expected_size += sizeof(struct greybus_descriptor_bundle);
135 break;
Alex Elderb09c94a2014-10-01 21:54:16 -0500136 case GREYBUS_TYPE_CPORT:
Viresh Kumar19b3b2c2015-03-24 17:08:13 +0530137 expected_size += sizeof(struct greybus_descriptor_cport);
Alex Elderb09c94a2014-10-01 21:54:16 -0500138 break;
139 case GREYBUS_TYPE_INVALID:
140 default:
Johan Hovolda0b55422016-02-11 13:52:46 +0100141 dev_err(&intf->dev, "invalid descriptor type (%u)\n",
Greg Kroah-Hartmanee989b02019-08-25 07:54:24 +0200142 desc_header->type);
Alex Elderb09c94a2014-10-01 21:54:16 -0500143 return -EINVAL;
144 }
145
Viresh Kumar19b3b2c2015-03-24 17:08:13 +0530146 if (desc_size < expected_size) {
Johan Hovolda0b55422016-02-11 13:52:46 +0100147 dev_err(&intf->dev, "%s descriptor too small (%zu < %zu)\n",
Greg Kroah-Hartmanee989b02019-08-25 07:54:24 +0200148 get_descriptor_type_string(desc_header->type),
149 desc_size, expected_size);
Viresh Kumar19b3b2c2015-03-24 17:08:13 +0530150 return -EINVAL;
151 }
152
Viresh Kumar55b930c2015-04-29 11:02:08 +0530153 /* Descriptor bigger than what we expect */
154 if (desc_size > expected_size) {
Johan Hovolda0b55422016-02-11 13:52:46 +0100155 dev_warn(&intf->dev, "%s descriptor size mismatch (want %zu got %zu)\n",
Greg Kroah-Hartmanee989b02019-08-25 07:54:24 +0200156 get_descriptor_type_string(desc_header->type),
157 expected_size, desc_size);
Viresh Kumar55b930c2015-04-29 11:02:08 +0530158 }
159
Alex Elderb09c94a2014-10-01 21:54:16 -0500160 descriptor = kzalloc(sizeof(*descriptor), GFP_KERNEL);
161 if (!descriptor)
162 return -ENOMEM;
163
164 descriptor->size = desc_size;
Alex Elderd393c982015-06-09 17:42:53 -0500165 descriptor->data = (char *)desc + sizeof(*desc_header);
Alex Elderb09c94a2014-10-01 21:54:16 -0500166 descriptor->type = desc_header->type;
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -0800167 list_add_tail(&descriptor->links, &intf->manifest_descs);
Alex Elderb09c94a2014-10-01 21:54:16 -0500168
Alex Elderd393c982015-06-09 17:42:53 -0500169 /* desc_size is positive and is known to fit in a signed int */
Alex Elderd8187aa2015-03-27 15:06:24 -0500170
Alex Elderb09c94a2014-10-01 21:54:16 -0500171 return desc_size;
172}
173
174/*
175 * Find the string descriptor having the given id, validate it, and
176 * allocate a duplicate copy of it. The duplicate has an extra byte
177 * which guarantees the returned string is NUL-terminated.
178 *
179 * String index 0 is valid (it represents "no string"), and for
180 * that a null pointer is returned.
181 *
182 * Otherwise returns a pointer to a newly-allocated copy of the
183 * descriptor string, or an error-coded pointer on failure.
184 */
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -0800185static char *gb_string_get(struct gb_interface *intf, u8 string_id)
Alex Elderb09c94a2014-10-01 21:54:16 -0500186{
187 struct greybus_descriptor_string *desc_string;
188 struct manifest_desc *descriptor;
189 bool found = false;
190 char *string;
191
192 /* A zero string id means no string (but no error) */
193 if (!string_id)
194 return NULL;
195
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -0800196 list_for_each_entry(descriptor, &intf->manifest_descs, links) {
Alex Elderb09c94a2014-10-01 21:54:16 -0500197 if (descriptor->type != GREYBUS_TYPE_STRING)
198 continue;
199
Matt Porter7a13e2f2014-10-06 09:58:44 -0400200 desc_string = descriptor->data;
Alex Elderb09c94a2014-10-01 21:54:16 -0500201 if (desc_string->id == string_id) {
202 found = true;
203 break;
204 }
205 }
206 if (!found)
207 return ERR_PTR(-ENOENT);
208
209 /* Allocate an extra byte so we can guarantee it's NUL-terminated */
Alex Elderd393c982015-06-09 17:42:53 -0500210 string = kmemdup(&desc_string->string, desc_string->length + 1,
Greg Kroah-Hartmanee989b02019-08-25 07:54:24 +0200211 GFP_KERNEL);
Alex Elderb09c94a2014-10-01 21:54:16 -0500212 if (!string)
213 return ERR_PTR(-ENOMEM);
214 string[desc_string->length] = '\0';
215
216 /* Ok we've used this string, so we're done with it */
217 release_manifest_descriptor(descriptor);
218
219 return string;
220}
221
Alex Elderd88bfb52014-10-01 21:54:17 -0500222/*
Alex Elderd393c982015-06-09 17:42:53 -0500223 * Find cport descriptors in the manifest associated with the given
224 * bundle, and set up data structures for the functions that use
225 * them. Returns the number of cports set up for the bundle, or 0
226 * if there is an error.
Alex Elderc095bbc2014-10-01 21:54:18 -0500227 */
Alex Elderc46839d2015-06-09 17:42:54 -0500228static u32 gb_manifest_parse_cports(struct gb_bundle *bundle)
Alex Elderc095bbc2014-10-01 21:54:18 -0500229{
Alex Elderc46839d2015-06-09 17:42:54 -0500230 struct gb_interface *intf = bundle->intf;
Johan Hovold98fdf5a2016-01-21 17:34:09 +0100231 struct greybus_descriptor_cport *desc_cport;
Johan Hovoldd6fba3d2016-01-21 17:34:10 +0100232 struct manifest_desc *desc, *next, *tmp;
Johan Hovold98fdf5a2016-01-21 17:34:09 +0100233 LIST_HEAD(list);
Alex Eldera6b13eb2015-06-09 17:42:55 -0500234 u8 bundle_id = bundle->id;
Bryan O'Donoghueb38fe342015-07-21 09:10:27 +0100235 u16 cport_id;
Alex Elderc095bbc2014-10-01 21:54:18 -0500236 u32 count = 0;
Johan Hovold98fdf5a2016-01-21 17:34:09 +0100237 int i;
Alex Elderc095bbc2014-10-01 21:54:18 -0500238
Alex Eldera6b13eb2015-06-09 17:42:55 -0500239 /* Set up all cport descriptors associated with this bundle */
240 list_for_each_entry_safe(desc, next, &intf->manifest_descs, links) {
Alex Eldera6b13eb2015-06-09 17:42:55 -0500241 if (desc->type != GREYBUS_TYPE_CPORT)
242 continue;
243
244 desc_cport = desc->data;
245 if (desc_cport->bundle != bundle_id)
246 continue;
Alex Elderc095bbc2014-10-01 21:54:18 -0500247
Alex Elderfb690ca2015-06-13 11:02:09 -0500248 cport_id = le16_to_cpu(desc_cport->id);
249 if (cport_id > CPORT_ID_MAX)
Bryan O'Donoghueb38fe342015-07-21 09:10:27 +0100250 goto exit;
Alex Elderfb690ca2015-06-13 11:02:09 -0500251
Viresh Kumar42830f72016-06-11 08:01:01 +0530252 /* Nothing else should have its cport_id as control cport id */
253 if (cport_id == GB_CONTROL_CPORT_ID) {
254 dev_err(&bundle->dev, "invalid cport id found (%02u)\n",
255 cport_id);
256 goto exit;
257 }
258
Johan Hovoldd6fba3d2016-01-21 17:34:10 +0100259 /*
260 * Found one, move it to our temporary list after checking for
261 * duplicates.
262 */
263 list_for_each_entry(tmp, &list, links) {
264 desc_cport = tmp->data;
Greg Kroah-Hartmand1a9c052016-02-02 21:31:19 -0800265 if (cport_id == le16_to_cpu(desc_cport->id)) {
Johan Hovoldd6fba3d2016-01-21 17:34:10 +0100266 dev_err(&bundle->dev,
Greg Kroah-Hartmanee989b02019-08-25 07:54:24 +0200267 "duplicate CPort %u found\n", cport_id);
Johan Hovoldd6fba3d2016-01-21 17:34:10 +0100268 goto exit;
269 }
270 }
Viresh Kumar4a7908c2016-02-12 21:48:03 +0530271 list_move_tail(&desc->links, &list);
Alex Elderc095bbc2014-10-01 21:54:18 -0500272 count++;
Johan Hovold98fdf5a2016-01-21 17:34:09 +0100273 }
274
275 if (!count)
276 return 0;
277
278 bundle->cport_desc = kcalloc(count, sizeof(*bundle->cport_desc),
Greg Kroah-Hartmanee989b02019-08-25 07:54:24 +0200279 GFP_KERNEL);
Johan Hovold98fdf5a2016-01-21 17:34:09 +0100280 if (!bundle->cport_desc)
281 goto exit;
282
283 bundle->num_cports = count;
284
285 i = 0;
286 list_for_each_entry_safe(desc, next, &list, links) {
287 desc_cport = desc->data;
288 memcpy(&bundle->cport_desc[i++], desc_cport,
Greg Kroah-Hartmanee989b02019-08-25 07:54:24 +0200289 sizeof(*desc_cport));
Alex Eldera6b13eb2015-06-09 17:42:55 -0500290
Alex Elderc095bbc2014-10-01 21:54:18 -0500291 /* Release the cport descriptor */
Alex Eldera6b13eb2015-06-09 17:42:55 -0500292 release_manifest_descriptor(desc);
Alex Elderc095bbc2014-10-01 21:54:18 -0500293 }
294
295 return count;
Bryan O'Donoghueb38fe342015-07-21 09:10:27 +0100296exit:
Johan Hovold98fdf5a2016-01-21 17:34:09 +0100297 release_cport_descriptors(&list, bundle_id);
Viresh Kumar4317f872015-09-07 16:01:20 +0530298 /*
299 * Free all cports for this bundle to avoid 'excess descriptors'
300 * warnings.
301 */
Johan Hovoldf2152eb2015-11-25 15:59:25 +0100302 release_cport_descriptors(&intf->manifest_descs, bundle_id);
Viresh Kumar4317f872015-09-07 16:01:20 +0530303
Alex Elder52e8ce32015-06-12 10:21:09 -0500304 return 0; /* Error; count should also be 0 */
Alex Elderc095bbc2014-10-01 21:54:18 -0500305}
306
307/*
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500308 * Find bundle descriptors in the manifest and set up their data
309 * structures. Returns the number of bundles set up for the
Viresh Kumar7c183f72015-04-01 20:32:00 +0530310 * given interface.
Alex Elderd88bfb52014-10-01 21:54:17 -0500311 */
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800312static u32 gb_manifest_parse_bundles(struct gb_interface *intf)
Alex Elderd88bfb52014-10-01 21:54:17 -0500313{
Alex Elderc27a2532015-06-09 17:42:56 -0500314 struct manifest_desc *desc;
Alex Elder2a64fb02015-06-12 10:21:12 -0500315 struct gb_bundle *bundle;
316 struct gb_bundle *bundle_next;
Alex Elderd88bfb52014-10-01 21:54:17 -0500317 u32 count = 0;
Viresh Kumar98d7fbc2015-09-07 16:01:19 +0530318 u8 bundle_id;
Johan Hovold47091af2015-11-25 15:59:26 +0100319 u8 class;
Alex Elderd88bfb52014-10-01 21:54:17 -0500320
Rui Miguel Silva5c864e72015-11-16 19:23:25 +0000321 while ((desc = get_next_bundle_desc(intf))) {
Viresh Kumar7c183f72015-04-01 20:32:00 +0530322 struct greybus_descriptor_bundle *desc_bundle;
Alex Elderd88bfb52014-10-01 21:54:17 -0500323
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500324 /* Found one. Set up its bundle structure*/
Alex Elderc27a2532015-06-09 17:42:56 -0500325 desc_bundle = desc->data;
Viresh Kumar98d7fbc2015-09-07 16:01:19 +0530326 bundle_id = desc_bundle->id;
Johan Hovold47091af2015-11-25 15:59:26 +0100327 class = desc_bundle->class;
Viresh Kumar6c68da22015-06-22 16:42:27 +0530328
Johan Hovold47091af2015-11-25 15:59:26 +0100329 /* Done with this bundle descriptor */
330 release_manifest_descriptor(desc);
331
332 /* Ignore any legacy control bundles */
Viresh Kumar98d7fbc2015-09-07 16:01:19 +0530333 if (bundle_id == GB_CONTROL_BUNDLE_ID) {
Johan Hovold47091af2015-11-25 15:59:26 +0100334 dev_dbg(&intf->dev, "%s - ignoring control bundle\n",
Greg Kroah-Hartmanee989b02019-08-25 07:54:24 +0200335 __func__);
Johan Hovold47091af2015-11-25 15:59:26 +0100336 release_cport_descriptors(&intf->manifest_descs,
Greg Kroah-Hartmanee989b02019-08-25 07:54:24 +0200337 bundle_id);
Johan Hovold47091af2015-11-25 15:59:26 +0100338 continue;
Viresh Kumar6c68da22015-06-22 16:42:27 +0530339 }
340
Viresh Kumar730a2f62015-06-22 16:42:30 +0530341 /* Nothing else should have its class set to control class */
Johan Hovold47091af2015-11-25 15:59:26 +0100342 if (class == GREYBUS_CLASS_CONTROL) {
Bryan O'Donoghueb38fe342015-07-21 09:10:27 +0100343 dev_err(&intf->dev,
Johan Hovold100e9002015-12-07 15:05:38 +0100344 "bundle %u cannot use control class\n",
Viresh Kumar98d7fbc2015-09-07 16:01:19 +0530345 bundle_id);
Bryan O'Donoghueb38fe342015-07-21 09:10:27 +0100346 goto cleanup;
347 }
Viresh Kumar730a2f62015-06-22 16:42:30 +0530348
Johan Hovold47091af2015-11-25 15:59:26 +0100349 bundle = gb_bundle_create(intf, bundle_id, class);
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500350 if (!bundle)
Alex Elder2a64fb02015-06-12 10:21:12 -0500351 goto cleanup;
Alex Elderc095bbc2014-10-01 21:54:18 -0500352
Viresh Kumar98d7fbc2015-09-07 16:01:19 +0530353 /*
354 * Now go set up this bundle's functions and cports.
355 *
356 * A 'bundle' represents a device in greybus. It may require
357 * multiple cports for its functioning. If we fail to setup any
358 * cport of a bundle, we better reject the complete bundle as
359 * the device may not be able to function properly then.
360 *
361 * But, failing to setup a cport of bundle X doesn't mean that
362 * the device corresponding to bundle Y will not work properly.
363 * Bundles should be treated as separate independent devices.
364 *
365 * While parsing manifest for an interface, treat bundles as
366 * separate entities and don't reject entire interface and its
367 * bundles on failing to initialize a cport. But make sure the
368 * bundle which needs the cport, gets destroyed properly.
Viresh Kumar98d7fbc2015-09-07 16:01:19 +0530369 */
370 if (!gb_manifest_parse_cports(bundle)) {
Viresh Kumar98d7fbc2015-09-07 16:01:19 +0530371 gb_bundle_destroy(bundle);
372 continue;
373 }
374
Bryan O'Donoghueb38fe342015-07-21 09:10:27 +0100375 count++;
Alex Elderd88bfb52014-10-01 21:54:17 -0500376 }
377
378 return count;
Alex Elder2a64fb02015-06-12 10:21:12 -0500379cleanup:
380 /* An error occurred; undo any changes we've made */
381 list_for_each_entry_safe(bundle, bundle_next, &intf->bundles, links) {
382 gb_bundle_destroy(bundle);
383 count--;
384 }
385 return 0; /* Error; count should also be 0 */
Alex Elderd88bfb52014-10-01 21:54:17 -0500386}
387
Viresh Kumara93db2d2015-04-01 20:32:02 +0530388static bool gb_manifest_parse_interface(struct gb_interface *intf,
389 struct manifest_desc *interface_desc)
Alex Elderb09c94a2014-10-01 21:54:16 -0500390{
Viresh Kumara93db2d2015-04-01 20:32:02 +0530391 struct greybus_descriptor_interface *desc_intf = interface_desc->data;
Johan Hovold7c8eb122016-04-13 19:19:04 +0200392 struct gb_control *control = intf->control;
Johan Hovold7d963cb2016-04-13 19:18:55 +0200393 char *str;
Alex Elderb09c94a2014-10-01 21:54:16 -0500394
395 /* Handle the strings first--they can fail */
Johan Hovold7d963cb2016-04-13 19:18:55 +0200396 str = gb_string_get(intf, desc_intf->vendor_stringid);
397 if (IS_ERR(str))
Alex Elder937d0da2014-10-03 14:14:25 -0500398 return false;
Johan Hovold7c8eb122016-04-13 19:19:04 +0200399 control->vendor_string = str;
Alex Elder937d0da2014-10-03 14:14:25 -0500400
Johan Hovold7d963cb2016-04-13 19:18:55 +0200401 str = gb_string_get(intf, desc_intf->product_stringid);
402 if (IS_ERR(str))
Viresh Kumar50fc08f2014-11-13 18:14:32 +0530403 goto out_free_vendor_string;
Johan Hovold7c8eb122016-04-13 19:19:04 +0200404 control->product_string = str;
Alex Elderb09c94a2014-10-01 21:54:16 -0500405
Bryan O'Donoghue8c81d462016-05-15 19:37:49 +0100406 /* Assign feature flags communicated via manifest */
407 intf->features = desc_intf->features;
408
Viresh Kumara93db2d2015-04-01 20:32:02 +0530409 /* Release the interface descriptor, now that we're done with it */
410 release_manifest_descriptor(interface_desc);
Alex Elderb09c94a2014-10-01 21:54:16 -0500411
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500412 /* An interface must have at least one bundle descriptor */
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800413 if (!gb_manifest_parse_bundles(intf)) {
Bryan O'Donoghue09fb10f2015-07-21 09:10:28 +0100414 dev_err(&intf->dev, "manifest bundle descriptors not valid\n");
Alex Elder937d0da2014-10-03 14:14:25 -0500415 goto out_err;
Alex Elderd88bfb52014-10-01 21:54:17 -0500416 }
417
Alex Elder937d0da2014-10-03 14:14:25 -0500418 return true;
419out_err:
Johan Hovold7c8eb122016-04-13 19:19:04 +0200420 kfree(control->product_string);
421 control->product_string = NULL;
Viresh Kumar50fc08f2014-11-13 18:14:32 +0530422out_free_vendor_string:
Johan Hovold7c8eb122016-04-13 19:19:04 +0200423 kfree(control->vendor_string);
424 control->vendor_string = NULL;
Alex Elder937d0da2014-10-03 14:14:25 -0500425
426 return false;
Alex Elderb09c94a2014-10-01 21:54:16 -0500427}
428
429/*
Alex Elderd393c982015-06-09 17:42:53 -0500430 * Parse a buffer containing an interface manifest.
Alex Elderb09c94a2014-10-01 21:54:16 -0500431 *
432 * If we find anything wrong with the content/format of the buffer
433 * we reject it.
434 *
435 * The first requirement is that the manifest's version is
436 * one we can parse.
437 *
438 * We make an initial pass through the buffer and identify all of
439 * the descriptors it contains, keeping track for each its type
440 * and the location size of its data in the buffer.
441 *
Alex Elderd393c982015-06-09 17:42:53 -0500442 * Next we scan the descriptors, looking for an interface descriptor;
Alex Elderb09c94a2014-10-01 21:54:16 -0500443 * there must be exactly one of those. When found, we record the
444 * information it contains, and then remove that descriptor (and any
445 * string descriptors it refers to) from further consideration.
446 *
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800447 * After that we look for the interface's bundles--there must be at
Alex Elderb09c94a2014-10-01 21:54:16 -0500448 * least one of those.
449 *
Alex Elder937d0da2014-10-03 14:14:25 -0500450 * Returns true if parsing was successful, false otherwise.
Alex Elderb09c94a2014-10-01 21:54:16 -0500451 */
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -0800452bool gb_manifest_parse(struct gb_interface *intf, void *data, size_t size)
Alex Elderb09c94a2014-10-01 21:54:16 -0500453{
454 struct greybus_manifest *manifest;
455 struct greybus_manifest_header *header;
456 struct greybus_descriptor *desc;
457 struct manifest_desc *descriptor;
Viresh Kumara93db2d2015-04-01 20:32:02 +0530458 struct manifest_desc *interface_desc = NULL;
Alex Elderb09c94a2014-10-01 21:54:16 -0500459 u16 manifest_size;
460 u32 found = 0;
Viresh Kumar43d94312014-11-13 18:14:30 +0530461 bool result;
Alex Elderb09c94a2014-10-01 21:54:16 -0500462
Viresh Kumar1dd90df2014-11-14 17:25:03 +0530463 /* Manifest descriptor list should be empty here */
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -0800464 if (WARN_ON(!list_empty(&intf->manifest_descs)))
Viresh Kumar1dd90df2014-11-14 17:25:03 +0530465 return false;
466
Alex Elderb09c94a2014-10-01 21:54:16 -0500467 /* we have to have at _least_ the manifest header */
Alex Elderd393c982015-06-09 17:42:53 -0500468 if (size < sizeof(*header)) {
Johan Hovolda0b55422016-02-11 13:52:46 +0100469 dev_err(&intf->dev, "short manifest (%zu < %zu)\n",
Greg Kroah-Hartmanee989b02019-08-25 07:54:24 +0200470 size, sizeof(*header));
Alex Elder937d0da2014-10-03 14:14:25 -0500471 return false;
Alex Elderb09c94a2014-10-01 21:54:16 -0500472 }
473
474 /* Make sure the size is right */
475 manifest = data;
476 header = &manifest->header;
477 manifest_size = le16_to_cpu(header->size);
478 if (manifest_size != size) {
Johan Hovolda0b55422016-02-11 13:52:46 +0100479 dev_err(&intf->dev, "manifest size mismatch (%zu != %u)\n",
Greg Kroah-Hartmanee989b02019-08-25 07:54:24 +0200480 size, manifest_size);
Alex Elder937d0da2014-10-03 14:14:25 -0500481 return false;
Alex Elderb09c94a2014-10-01 21:54:16 -0500482 }
483
484 /* Validate major/minor number */
485 if (header->version_major > GREYBUS_VERSION_MAJOR) {
Johan Hovolda0b55422016-02-11 13:52:46 +0100486 dev_err(&intf->dev, "manifest version too new (%u.%u > %u.%u)\n",
Greg Kroah-Hartmanee989b02019-08-25 07:54:24 +0200487 header->version_major, header->version_minor,
488 GREYBUS_VERSION_MAJOR, GREYBUS_VERSION_MINOR);
Alex Elder937d0da2014-10-03 14:14:25 -0500489 return false;
Alex Elderb09c94a2014-10-01 21:54:16 -0500490 }
491
492 /* OK, find all the descriptors */
Sachin Pandharefc25d902015-11-24 07:59:10 +0530493 desc = manifest->descriptors;
Alex Elderb09c94a2014-10-01 21:54:16 -0500494 size -= sizeof(*header);
495 while (size) {
496 int desc_size;
497
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -0800498 desc_size = identify_descriptor(intf, desc, size);
Viresh Kumar13fe6a92015-03-24 17:08:14 +0530499 if (desc_size < 0) {
Matt Porterff8aed52014-10-06 13:46:36 -0400500 result = false;
Alex Elder937d0da2014-10-03 14:14:25 -0500501 goto out;
Alex Elderb09c94a2014-10-01 21:54:16 -0500502 }
503 desc = (struct greybus_descriptor *)((char *)desc + desc_size);
504 size -= desc_size;
Greg Kroah-Hartman86bf33a2014-11-14 14:37:56 -0800505 }
Alex Elderb09c94a2014-10-01 21:54:16 -0500506
Viresh Kumara93db2d2015-04-01 20:32:02 +0530507 /* There must be a single interface descriptor */
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -0800508 list_for_each_entry(descriptor, &intf->manifest_descs, links) {
Viresh Kumara93db2d2015-04-01 20:32:02 +0530509 if (descriptor->type == GREYBUS_TYPE_INTERFACE)
Greg Kroah-Hartman86bf33a2014-11-14 14:37:56 -0800510 if (!found++)
Viresh Kumara93db2d2015-04-01 20:32:02 +0530511 interface_desc = descriptor;
Greg Kroah-Hartman86bf33a2014-11-14 14:37:56 -0800512 }
513 if (found != 1) {
Johan Hovolda0b55422016-02-11 13:52:46 +0100514 dev_err(&intf->dev, "manifest must have 1 interface descriptor (%u found)\n",
Greg Kroah-Hartmanee989b02019-08-25 07:54:24 +0200515 found);
Greg Kroah-Hartman86bf33a2014-11-14 14:37:56 -0800516 result = false;
517 goto out;
Alex Elderb09c94a2014-10-01 21:54:16 -0500518 }
519
Viresh Kumara93db2d2015-04-01 20:32:02 +0530520 /* Parse the manifest, starting with the interface descriptor */
521 result = gb_manifest_parse_interface(intf, interface_desc);
Alex Elderb09c94a2014-10-01 21:54:16 -0500522
523 /*
524 * We really should have no remaining descriptors, but we
525 * don't know what newer format manifests might leave.
526 */
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -0800527 if (result && !list_empty(&intf->manifest_descs))
Johan Hovolda0b55422016-02-11 13:52:46 +0100528 dev_info(&intf->dev, "excess descriptors in interface manifest\n");
Alex Elder937d0da2014-10-03 14:14:25 -0500529out:
Greg Kroah-Hartman86cad662014-12-23 15:16:50 -0800530 release_manifest_descriptors(intf);
Alex Elderb09c94a2014-10-01 21:54:16 -0500531
Matt Porterff8aed52014-10-06 13:46:36 -0400532 return result;
Alex Elderb09c94a2014-10-01 21:54:16 -0500533}