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