Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 1 | /* |
Viresh Kumar | a93db2d | 2015-04-01 20:32:02 +0530 | [diff] [blame] | 2 | * Greybus manifest parsing |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 3 | * |
Alex Elder | d8187aa | 2015-03-27 15:06:24 -0500 | [diff] [blame] | 4 | * Copyright 2014-2015 Google Inc. |
| 5 | * Copyright 2014-2015 Linaro Ltd. |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 6 | * |
| 7 | * Released under the GPLv2 only. |
| 8 | */ |
| 9 | |
| 10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 11 | |
| 12 | #include <linux/err.h> |
| 13 | |
| 14 | #include "greybus.h" |
| 15 | |
Viresh Kumar | 19b3b2c | 2015-03-24 17:08:13 +0530 | [diff] [blame] | 16 | static const char *get_descriptor_type_string(u8 type) |
| 17 | { |
| 18 | switch(type) { |
| 19 | case GREYBUS_TYPE_INVALID: |
| 20 | return "invalid"; |
Viresh Kumar | 19b3b2c | 2015-03-24 17:08:13 +0530 | [diff] [blame] | 21 | case GREYBUS_TYPE_STRING: |
| 22 | return "string"; |
| 23 | case GREYBUS_TYPE_INTERFACE: |
| 24 | return "interface"; |
| 25 | case GREYBUS_TYPE_CPORT: |
| 26 | return "cport"; |
Viresh Kumar | 83a0cb5 | 2015-04-01 20:31:59 +0530 | [diff] [blame] | 27 | case GREYBUS_TYPE_BUNDLE: |
| 28 | return "bundle"; |
Viresh Kumar | 19b3b2c | 2015-03-24 17:08:13 +0530 | [diff] [blame] | 29 | default: |
| 30 | WARN_ON(1); |
| 31 | return "unknown"; |
| 32 | } |
| 33 | } |
| 34 | |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 35 | /* |
| 36 | * We scan the manifest once to identify where all the descriptors |
| 37 | * are. The result is a list of these manifest_desc structures. We |
| 38 | * then pick through them for what we're looking for (starting with |
Viresh Kumar | a93db2d | 2015-04-01 20:32:02 +0530 | [diff] [blame] | 39 | * the interface descriptor). As each is processed we remove it from |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 40 | * the list. When we're done the list should (probably) be empty. |
| 41 | */ |
| 42 | struct manifest_desc { |
| 43 | struct list_head links; |
| 44 | |
| 45 | size_t size; |
| 46 | void *data; |
| 47 | enum greybus_descriptor_type type; |
| 48 | }; |
| 49 | |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 50 | static void release_manifest_descriptor(struct manifest_desc *descriptor) |
| 51 | { |
| 52 | list_del(&descriptor->links); |
| 53 | kfree(descriptor); |
| 54 | } |
| 55 | |
Greg Kroah-Hartman | 86cad66 | 2014-12-23 15:16:50 -0800 | [diff] [blame] | 56 | static void release_manifest_descriptors(struct gb_interface *intf) |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 57 | { |
| 58 | struct manifest_desc *descriptor; |
| 59 | struct manifest_desc *next; |
| 60 | |
Greg Kroah-Hartman | 86cad66 | 2014-12-23 15:16:50 -0800 | [diff] [blame] | 61 | list_for_each_entry_safe(descriptor, next, &intf->manifest_descs, links) |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 62 | release_manifest_descriptor(descriptor); |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Validate the given descriptor. Its reported size must fit within |
Viresh Kumar | 696e0cc | 2014-11-21 11:26:30 +0530 | [diff] [blame] | 67 | * the number of bytes remaining, and it must have a recognized |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 68 | * type. Check that the reported size is at least as big as what |
| 69 | * we expect to see. (It could be bigger, perhaps for a new version |
| 70 | * of the format.) |
| 71 | * |
| 72 | * Returns the number of bytes consumed by the descriptor, or a |
| 73 | * negative errno. |
| 74 | */ |
Greg Kroah-Hartman | 86cad66 | 2014-12-23 15:16:50 -0800 | [diff] [blame] | 75 | static int identify_descriptor(struct gb_interface *intf, |
| 76 | struct greybus_descriptor *desc, size_t size) |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 77 | { |
| 78 | struct greybus_descriptor_header *desc_header = &desc->header; |
| 79 | struct manifest_desc *descriptor; |
Alex Elder | d8187aa | 2015-03-27 15:06:24 -0500 | [diff] [blame] | 80 | size_t desc_size; |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 81 | size_t expected_size; |
| 82 | |
| 83 | if (size < sizeof(*desc_header)) { |
| 84 | pr_err("manifest too small\n"); |
| 85 | return -EINVAL; /* Must at least have header */ |
| 86 | } |
| 87 | |
Alex Elder | d8187aa | 2015-03-27 15:06:24 -0500 | [diff] [blame] | 88 | desc_size = le16_to_cpu(desc_header->size); |
| 89 | if (desc_size > size) { |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 90 | pr_err("descriptor too big\n"); |
| 91 | return -EINVAL; |
| 92 | } |
| 93 | |
Viresh Kumar | 19b3b2c | 2015-03-24 17:08:13 +0530 | [diff] [blame] | 94 | /* Descriptor needs to at least have a header */ |
| 95 | expected_size = sizeof(*desc_header); |
| 96 | |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 97 | switch (desc_header->type) { |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 98 | case GREYBUS_TYPE_STRING: |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 99 | expected_size += sizeof(struct greybus_descriptor_string); |
Viresh Kumar | 19b3b2c | 2015-03-24 17:08:13 +0530 | [diff] [blame] | 100 | expected_size += desc->string.length; |
Viresh Kumar | fa2fbf1 | 2015-04-28 19:51:35 +0530 | [diff] [blame] | 101 | |
| 102 | /* String descriptors are padded to 4 byte boundaries */ |
| 103 | expected_size = ALIGN(expected_size, 4); |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 104 | break; |
Alex Elder | 63cc932 | 2014-10-02 12:30:02 -0500 | [diff] [blame] | 105 | case GREYBUS_TYPE_INTERFACE: |
Viresh Kumar | a93db2d | 2015-04-01 20:32:02 +0530 | [diff] [blame] | 106 | expected_size += sizeof(struct greybus_descriptor_interface); |
Alex Elder | 63cc932 | 2014-10-02 12:30:02 -0500 | [diff] [blame] | 107 | break; |
Viresh Kumar | 7c183f7 | 2015-04-01 20:32:00 +0530 | [diff] [blame] | 108 | case GREYBUS_TYPE_BUNDLE: |
| 109 | expected_size += sizeof(struct greybus_descriptor_bundle); |
| 110 | break; |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 111 | case GREYBUS_TYPE_CPORT: |
Viresh Kumar | 19b3b2c | 2015-03-24 17:08:13 +0530 | [diff] [blame] | 112 | expected_size += sizeof(struct greybus_descriptor_cport); |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 113 | break; |
| 114 | case GREYBUS_TYPE_INVALID: |
| 115 | default: |
| 116 | pr_err("invalid descriptor type (%hhu)\n", desc_header->type); |
| 117 | return -EINVAL; |
| 118 | } |
| 119 | |
Viresh Kumar | 19b3b2c | 2015-03-24 17:08:13 +0530 | [diff] [blame] | 120 | if (desc_size < expected_size) { |
Alex Elder | d8187aa | 2015-03-27 15:06:24 -0500 | [diff] [blame] | 121 | pr_err("%s descriptor too small (%zu < %zu)\n", |
Viresh Kumar | 19b3b2c | 2015-03-24 17:08:13 +0530 | [diff] [blame] | 122 | get_descriptor_type_string(desc_header->type), |
| 123 | desc_size, expected_size); |
| 124 | return -EINVAL; |
| 125 | } |
| 126 | |
Viresh Kumar | 55b930c | 2015-04-29 11:02:08 +0530 | [diff] [blame] | 127 | /* Descriptor bigger than what we expect */ |
| 128 | if (desc_size > expected_size) { |
| 129 | pr_warn("%s descriptor size mismatch (want %zu got %zu)\n", |
| 130 | get_descriptor_type_string(desc_header->type), |
| 131 | expected_size, desc_size); |
| 132 | } |
| 133 | |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 134 | descriptor = kzalloc(sizeof(*descriptor), GFP_KERNEL); |
| 135 | if (!descriptor) |
| 136 | return -ENOMEM; |
| 137 | |
| 138 | descriptor->size = desc_size; |
Matt Porter | 7a13e2f | 2014-10-06 09:58:44 -0400 | [diff] [blame] | 139 | descriptor->data = (u8 *)desc + sizeof(*desc_header); |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 140 | descriptor->type = desc_header->type; |
Greg Kroah-Hartman | 86cad66 | 2014-12-23 15:16:50 -0800 | [diff] [blame] | 141 | list_add_tail(&descriptor->links, &intf->manifest_descs); |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 142 | |
Alex Elder | d8187aa | 2015-03-27 15:06:24 -0500 | [diff] [blame] | 143 | /* desc_size is is positive and is known to fit in a signed int */ |
| 144 | |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 145 | return desc_size; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Find the string descriptor having the given id, validate it, and |
| 150 | * allocate a duplicate copy of it. The duplicate has an extra byte |
| 151 | * which guarantees the returned string is NUL-terminated. |
| 152 | * |
| 153 | * String index 0 is valid (it represents "no string"), and for |
| 154 | * that a null pointer is returned. |
| 155 | * |
| 156 | * Otherwise returns a pointer to a newly-allocated copy of the |
| 157 | * descriptor string, or an error-coded pointer on failure. |
| 158 | */ |
Greg Kroah-Hartman | 86cad66 | 2014-12-23 15:16:50 -0800 | [diff] [blame] | 159 | static char *gb_string_get(struct gb_interface *intf, u8 string_id) |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 160 | { |
| 161 | struct greybus_descriptor_string *desc_string; |
| 162 | struct manifest_desc *descriptor; |
| 163 | bool found = false; |
| 164 | char *string; |
| 165 | |
| 166 | /* A zero string id means no string (but no error) */ |
| 167 | if (!string_id) |
| 168 | return NULL; |
| 169 | |
Greg Kroah-Hartman | 86cad66 | 2014-12-23 15:16:50 -0800 | [diff] [blame] | 170 | list_for_each_entry(descriptor, &intf->manifest_descs, links) { |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 171 | |
| 172 | if (descriptor->type != GREYBUS_TYPE_STRING) |
| 173 | continue; |
| 174 | |
Matt Porter | 7a13e2f | 2014-10-06 09:58:44 -0400 | [diff] [blame] | 175 | desc_string = descriptor->data; |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 176 | if (desc_string->id == string_id) { |
| 177 | found = true; |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | if (!found) |
| 182 | return ERR_PTR(-ENOENT); |
| 183 | |
| 184 | /* Allocate an extra byte so we can guarantee it's NUL-terminated */ |
| 185 | string = kmemdup(&desc_string->string, (size_t)desc_string->length + 1, |
| 186 | GFP_KERNEL); |
| 187 | if (!string) |
| 188 | return ERR_PTR(-ENOMEM); |
| 189 | string[desc_string->length] = '\0'; |
| 190 | |
| 191 | /* Ok we've used this string, so we're done with it */ |
| 192 | release_manifest_descriptor(descriptor); |
| 193 | |
| 194 | return string; |
| 195 | } |
| 196 | |
Alex Elder | d88bfb5 | 2014-10-01 21:54:17 -0500 | [diff] [blame] | 197 | /* |
Alex Elder | c095bbc | 2014-10-01 21:54:18 -0500 | [diff] [blame] | 198 | * Find cport descriptors in the manifest and set up data structures |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 199 | * for the functions that use them. Returns the number of bundles |
| 200 | * set up for the given interface, or 0 if there is an error. |
Alex Elder | c095bbc | 2014-10-01 21:54:18 -0500 | [diff] [blame] | 201 | */ |
Greg Kroah-Hartman | 86cad66 | 2014-12-23 15:16:50 -0800 | [diff] [blame] | 202 | static u32 gb_manifest_parse_cports(struct gb_interface *intf, |
| 203 | struct gb_bundle *bundle) |
Alex Elder | c095bbc | 2014-10-01 21:54:18 -0500 | [diff] [blame] | 204 | { |
| 205 | u32 count = 0; |
| 206 | |
| 207 | while (true) { |
| 208 | struct manifest_desc *descriptor; |
| 209 | struct greybus_descriptor_cport *desc_cport; |
Alex Elder | 7fba007 | 2014-10-28 19:35:59 -0500 | [diff] [blame] | 210 | u8 protocol_id; |
Alex Elder | c095bbc | 2014-10-01 21:54:18 -0500 | [diff] [blame] | 211 | u16 cport_id; |
Viresh Kumar | 4ed16a8 | 2014-11-13 18:14:31 +0530 | [diff] [blame] | 212 | bool found = false; |
Alex Elder | c095bbc | 2014-10-01 21:54:18 -0500 | [diff] [blame] | 213 | |
| 214 | /* Find a cport descriptor */ |
Greg Kroah-Hartman | 86cad66 | 2014-12-23 15:16:50 -0800 | [diff] [blame] | 215 | list_for_each_entry(descriptor, &intf->manifest_descs, links) { |
Alex Elder | c095bbc | 2014-10-01 21:54:18 -0500 | [diff] [blame] | 216 | if (descriptor->type == GREYBUS_TYPE_CPORT) { |
Alex Elder | 63cc932 | 2014-10-02 12:30:02 -0500 | [diff] [blame] | 217 | desc_cport = descriptor->data; |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 218 | if (desc_cport->bundle == bundle->id) { |
Alex Elder | 63cc932 | 2014-10-02 12:30:02 -0500 | [diff] [blame] | 219 | found = true; |
| 220 | break; |
| 221 | } |
Alex Elder | c095bbc | 2014-10-01 21:54:18 -0500 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | if (!found) |
| 225 | break; |
| 226 | |
| 227 | /* Found one. Set up its function structure */ |
Alex Elder | 7fba007 | 2014-10-28 19:35:59 -0500 | [diff] [blame] | 228 | protocol_id = desc_cport->protocol_id; |
Alex Elder | c095bbc | 2014-10-01 21:54:18 -0500 | [diff] [blame] | 229 | cport_id = le16_to_cpu(desc_cport->id); |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 230 | if (!gb_connection_create(bundle, cport_id, protocol_id)) |
Alex Elder | c095bbc | 2014-10-01 21:54:18 -0500 | [diff] [blame] | 231 | return 0; /* Error */ |
| 232 | |
| 233 | count++; |
| 234 | /* Release the cport descriptor */ |
| 235 | release_manifest_descriptor(descriptor); |
| 236 | } |
| 237 | |
| 238 | return count; |
| 239 | } |
| 240 | |
| 241 | /* |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 242 | * Find bundle descriptors in the manifest and set up their data |
| 243 | * structures. Returns the number of bundles set up for the |
Viresh Kumar | 7c183f7 | 2015-04-01 20:32:00 +0530 | [diff] [blame] | 244 | * given interface. |
Alex Elder | d88bfb5 | 2014-10-01 21:54:17 -0500 | [diff] [blame] | 245 | */ |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 246 | static u32 gb_manifest_parse_bundles(struct gb_interface *intf) |
Alex Elder | d88bfb5 | 2014-10-01 21:54:17 -0500 | [diff] [blame] | 247 | { |
| 248 | u32 count = 0; |
| 249 | |
| 250 | while (true) { |
| 251 | struct manifest_desc *descriptor; |
Viresh Kumar | 7c183f7 | 2015-04-01 20:32:00 +0530 | [diff] [blame] | 252 | struct greybus_descriptor_bundle *desc_bundle; |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 253 | struct gb_bundle *bundle; |
Alex Elder | d88bfb5 | 2014-10-01 21:54:17 -0500 | [diff] [blame] | 254 | bool found = false; |
| 255 | |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 256 | /* Find an bundle descriptor */ |
Greg Kroah-Hartman | 86cad66 | 2014-12-23 15:16:50 -0800 | [diff] [blame] | 257 | list_for_each_entry(descriptor, &intf->manifest_descs, links) { |
Viresh Kumar | 7c183f7 | 2015-04-01 20:32:00 +0530 | [diff] [blame] | 258 | if (descriptor->type == GREYBUS_TYPE_BUNDLE) { |
Alex Elder | d88bfb5 | 2014-10-01 21:54:17 -0500 | [diff] [blame] | 259 | found = true; |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | if (!found) |
| 264 | break; |
| 265 | |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 266 | /* Found one. Set up its bundle structure*/ |
Viresh Kumar | 7c183f7 | 2015-04-01 20:32:00 +0530 | [diff] [blame] | 267 | desc_bundle = descriptor->data; |
| 268 | bundle = gb_bundle_create(intf, desc_bundle->id, |
Viresh Kumar | 88e6d37 | 2015-04-06 15:49:36 +0530 | [diff] [blame] | 269 | desc_bundle->class); |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 270 | if (!bundle) |
Alex Elder | d88bfb5 | 2014-10-01 21:54:17 -0500 | [diff] [blame] | 271 | return 0; /* Error */ |
Alex Elder | c095bbc | 2014-10-01 21:54:18 -0500 | [diff] [blame] | 272 | |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 273 | /* Now go set up this bundle's functions and cports */ |
Greg Kroah-Hartman | 86cad66 | 2014-12-23 15:16:50 -0800 | [diff] [blame] | 274 | if (!gb_manifest_parse_cports(intf, bundle)) |
Alex Elder | c095bbc | 2014-10-01 21:54:18 -0500 | [diff] [blame] | 275 | return 0; /* Error parsing cports */ |
| 276 | |
Alex Elder | d88bfb5 | 2014-10-01 21:54:17 -0500 | [diff] [blame] | 277 | count++; |
| 278 | |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 279 | /* Done with this bundle descriptor */ |
Alex Elder | d88bfb5 | 2014-10-01 21:54:17 -0500 | [diff] [blame] | 280 | release_manifest_descriptor(descriptor); |
| 281 | } |
| 282 | |
| 283 | return count; |
| 284 | } |
| 285 | |
Viresh Kumar | a93db2d | 2015-04-01 20:32:02 +0530 | [diff] [blame] | 286 | static bool gb_manifest_parse_interface(struct gb_interface *intf, |
| 287 | struct manifest_desc *interface_desc) |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 288 | { |
Viresh Kumar | a93db2d | 2015-04-01 20:32:02 +0530 | [diff] [blame] | 289 | struct greybus_descriptor_interface *desc_intf = interface_desc->data; |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 290 | |
| 291 | /* Handle the strings first--they can fail */ |
Viresh Kumar | a93db2d | 2015-04-01 20:32:02 +0530 | [diff] [blame] | 292 | intf->vendor_string = gb_string_get(intf, desc_intf->vendor_stringid); |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 293 | if (IS_ERR(intf->vendor_string)) |
Alex Elder | 937d0da | 2014-10-03 14:14:25 -0500 | [diff] [blame] | 294 | return false; |
| 295 | |
Greg Kroah-Hartman | 86cad66 | 2014-12-23 15:16:50 -0800 | [diff] [blame] | 296 | intf->product_string = gb_string_get(intf, |
Viresh Kumar | a93db2d | 2015-04-01 20:32:02 +0530 | [diff] [blame] | 297 | desc_intf->product_stringid); |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 298 | if (IS_ERR(intf->product_string)) { |
Viresh Kumar | 50fc08f | 2014-11-13 18:14:32 +0530 | [diff] [blame] | 299 | goto out_free_vendor_string; |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 300 | } |
| 301 | |
Viresh Kumar | 8a5286e | 2015-04-28 19:51:37 +0530 | [diff] [blame] | 302 | // FIXME |
| 303 | // Vendor, Product and Unique id must come via control protocol |
| 304 | intf->vendor = 0xffff; |
| 305 | intf->product = 0x0001; |
| 306 | intf->unique_id = 0; |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 307 | |
Viresh Kumar | a93db2d | 2015-04-01 20:32:02 +0530 | [diff] [blame] | 308 | /* Release the interface descriptor, now that we're done with it */ |
| 309 | release_manifest_descriptor(interface_desc); |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 310 | |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 311 | /* An interface must have at least one bundle descriptor */ |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 312 | if (!gb_manifest_parse_bundles(intf)) { |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 313 | pr_err("manifest bundle descriptors not valid\n"); |
Alex Elder | 937d0da | 2014-10-03 14:14:25 -0500 | [diff] [blame] | 314 | goto out_err; |
Alex Elder | d88bfb5 | 2014-10-01 21:54:17 -0500 | [diff] [blame] | 315 | } |
| 316 | |
Alex Elder | 937d0da | 2014-10-03 14:14:25 -0500 | [diff] [blame] | 317 | return true; |
| 318 | out_err: |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 319 | kfree(intf->product_string); |
| 320 | intf->product_string = NULL; |
Viresh Kumar | 50fc08f | 2014-11-13 18:14:32 +0530 | [diff] [blame] | 321 | out_free_vendor_string: |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 322 | kfree(intf->vendor_string); |
| 323 | intf->vendor_string = NULL; |
Alex Elder | 937d0da | 2014-10-03 14:14:25 -0500 | [diff] [blame] | 324 | |
| 325 | return false; |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /* |
Viresh Kumar | a93db2d | 2015-04-01 20:32:02 +0530 | [diff] [blame] | 329 | * Parse a buffer containing a Interface manifest. |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 330 | * |
| 331 | * If we find anything wrong with the content/format of the buffer |
| 332 | * we reject it. |
| 333 | * |
| 334 | * The first requirement is that the manifest's version is |
| 335 | * one we can parse. |
| 336 | * |
| 337 | * We make an initial pass through the buffer and identify all of |
| 338 | * the descriptors it contains, keeping track for each its type |
| 339 | * and the location size of its data in the buffer. |
| 340 | * |
Viresh Kumar | a93db2d | 2015-04-01 20:32:02 +0530 | [diff] [blame] | 341 | * Next we scan the descriptors, looking for a interface descriptor; |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 342 | * there must be exactly one of those. When found, we record the |
| 343 | * information it contains, and then remove that descriptor (and any |
| 344 | * string descriptors it refers to) from further consideration. |
| 345 | * |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 346 | * After that we look for the interface's bundles--there must be at |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 347 | * least one of those. |
| 348 | * |
Alex Elder | 937d0da | 2014-10-03 14:14:25 -0500 | [diff] [blame] | 349 | * Returns true if parsing was successful, false otherwise. |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 350 | */ |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 351 | bool gb_manifest_parse(struct gb_interface *intf, void *data, size_t size) |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 352 | { |
| 353 | struct greybus_manifest *manifest; |
| 354 | struct greybus_manifest_header *header; |
| 355 | struct greybus_descriptor *desc; |
| 356 | struct manifest_desc *descriptor; |
Viresh Kumar | a93db2d | 2015-04-01 20:32:02 +0530 | [diff] [blame] | 357 | struct manifest_desc *interface_desc = NULL; |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 358 | u16 manifest_size; |
| 359 | u32 found = 0; |
Viresh Kumar | 43d9431 | 2014-11-13 18:14:30 +0530 | [diff] [blame] | 360 | bool result; |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 361 | |
Viresh Kumar | 1dd90df | 2014-11-14 17:25:03 +0530 | [diff] [blame] | 362 | /* Manifest descriptor list should be empty here */ |
Greg Kroah-Hartman | 86cad66 | 2014-12-23 15:16:50 -0800 | [diff] [blame] | 363 | if (WARN_ON(!list_empty(&intf->manifest_descs))) |
Viresh Kumar | 1dd90df | 2014-11-14 17:25:03 +0530 | [diff] [blame] | 364 | return false; |
| 365 | |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 366 | /* we have to have at _least_ the manifest header */ |
| 367 | if (size <= sizeof(manifest->header)) { |
| 368 | pr_err("short manifest (%zu)\n", size); |
Alex Elder | 937d0da | 2014-10-03 14:14:25 -0500 | [diff] [blame] | 369 | return false; |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* Make sure the size is right */ |
| 373 | manifest = data; |
| 374 | header = &manifest->header; |
| 375 | manifest_size = le16_to_cpu(header->size); |
| 376 | if (manifest_size != size) { |
| 377 | pr_err("manifest size mismatch %zu != %hu\n", |
| 378 | size, manifest_size); |
Alex Elder | 937d0da | 2014-10-03 14:14:25 -0500 | [diff] [blame] | 379 | return false; |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /* Validate major/minor number */ |
| 383 | if (header->version_major > GREYBUS_VERSION_MAJOR) { |
| 384 | pr_err("manifest version too new (%hhu.%hhu > %hhu.%hhu)\n", |
| 385 | header->version_major, header->version_minor, |
| 386 | GREYBUS_VERSION_MAJOR, GREYBUS_VERSION_MINOR); |
Alex Elder | 937d0da | 2014-10-03 14:14:25 -0500 | [diff] [blame] | 387 | return false; |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /* OK, find all the descriptors */ |
| 391 | desc = (struct greybus_descriptor *)(header + 1); |
| 392 | size -= sizeof(*header); |
| 393 | while (size) { |
| 394 | int desc_size; |
| 395 | |
Greg Kroah-Hartman | 86cad66 | 2014-12-23 15:16:50 -0800 | [diff] [blame] | 396 | desc_size = identify_descriptor(intf, desc, size); |
Viresh Kumar | 13fe6a9 | 2015-03-24 17:08:14 +0530 | [diff] [blame] | 397 | if (desc_size < 0) { |
Matt Porter | ff8aed5 | 2014-10-06 13:46:36 -0400 | [diff] [blame] | 398 | result = false; |
Alex Elder | 937d0da | 2014-10-03 14:14:25 -0500 | [diff] [blame] | 399 | goto out; |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 400 | } |
| 401 | desc = (struct greybus_descriptor *)((char *)desc + desc_size); |
| 402 | size -= desc_size; |
Greg Kroah-Hartman | 86bf33a | 2014-11-14 14:37:56 -0800 | [diff] [blame] | 403 | } |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 404 | |
Viresh Kumar | a93db2d | 2015-04-01 20:32:02 +0530 | [diff] [blame] | 405 | /* There must be a single interface descriptor */ |
Greg Kroah-Hartman | 86cad66 | 2014-12-23 15:16:50 -0800 | [diff] [blame] | 406 | list_for_each_entry(descriptor, &intf->manifest_descs, links) { |
Viresh Kumar | a93db2d | 2015-04-01 20:32:02 +0530 | [diff] [blame] | 407 | if (descriptor->type == GREYBUS_TYPE_INTERFACE) |
Greg Kroah-Hartman | 86bf33a | 2014-11-14 14:37:56 -0800 | [diff] [blame] | 408 | if (!found++) |
Viresh Kumar | a93db2d | 2015-04-01 20:32:02 +0530 | [diff] [blame] | 409 | interface_desc = descriptor; |
Greg Kroah-Hartman | 86bf33a | 2014-11-14 14:37:56 -0800 | [diff] [blame] | 410 | } |
| 411 | if (found != 1) { |
Viresh Kumar | a93db2d | 2015-04-01 20:32:02 +0530 | [diff] [blame] | 412 | pr_err("manifest must have 1 interface descriptor (%u found)\n", |
Greg Kroah-Hartman | 86bf33a | 2014-11-14 14:37:56 -0800 | [diff] [blame] | 413 | found); |
| 414 | result = false; |
| 415 | goto out; |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 416 | } |
| 417 | |
Viresh Kumar | a93db2d | 2015-04-01 20:32:02 +0530 | [diff] [blame] | 418 | /* Parse the manifest, starting with the interface descriptor */ |
| 419 | result = gb_manifest_parse_interface(intf, interface_desc); |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 420 | |
| 421 | /* |
| 422 | * We really should have no remaining descriptors, but we |
| 423 | * don't know what newer format manifests might leave. |
| 424 | */ |
Greg Kroah-Hartman | 86cad66 | 2014-12-23 15:16:50 -0800 | [diff] [blame] | 425 | if (result && !list_empty(&intf->manifest_descs)) |
Viresh Kumar | a93db2d | 2015-04-01 20:32:02 +0530 | [diff] [blame] | 426 | pr_info("excess descriptors in interface manifest\n"); |
Alex Elder | 937d0da | 2014-10-03 14:14:25 -0500 | [diff] [blame] | 427 | out: |
Greg Kroah-Hartman | 86cad66 | 2014-12-23 15:16:50 -0800 | [diff] [blame] | 428 | release_manifest_descriptors(intf); |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 429 | |
Matt Porter | ff8aed5 | 2014-10-06 13:46:36 -0400 | [diff] [blame] | 430 | return result; |
Alex Elder | b09c94a | 2014-10-01 21:54:16 -0500 | [diff] [blame] | 431 | } |