manifest.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * Greybus manifest parsing
  3. *
  4. * Copyright 2014-2015 Google Inc.
  5. * Copyright 2014-2015 Linaro Ltd.
  6. *
  7. * Released under the GPLv2 only.
  8. */
  9. #include "greybus.h"
  10. static const char *get_descriptor_type_string(u8 type)
  11. {
  12. switch (type) {
  13. case GREYBUS_TYPE_INVALID:
  14. return "invalid";
  15. case GREYBUS_TYPE_STRING:
  16. return "string";
  17. case GREYBUS_TYPE_INTERFACE:
  18. return "interface";
  19. case GREYBUS_TYPE_CPORT:
  20. return "cport";
  21. case GREYBUS_TYPE_BUNDLE:
  22. return "bundle";
  23. default:
  24. WARN_ON(1);
  25. return "unknown";
  26. }
  27. }
  28. /*
  29. * We scan the manifest once to identify where all the descriptors
  30. * are. The result is a list of these manifest_desc structures. We
  31. * then pick through them for what we're looking for (starting with
  32. * the interface descriptor). As each is processed we remove it from
  33. * the list. When we're done the list should (probably) be empty.
  34. */
  35. struct manifest_desc {
  36. struct list_head links;
  37. size_t size;
  38. void *data;
  39. enum greybus_descriptor_type type;
  40. };
  41. static void release_manifest_descriptor(struct manifest_desc *descriptor)
  42. {
  43. list_del(&descriptor->links);
  44. kfree(descriptor);
  45. }
  46. static void release_manifest_descriptors(struct gb_interface *intf)
  47. {
  48. struct manifest_desc *descriptor;
  49. struct manifest_desc *next;
  50. list_for_each_entry_safe(descriptor, next, &intf->manifest_descs, links)
  51. release_manifest_descriptor(descriptor);
  52. }
  53. static void release_cport_descriptors(struct list_head *head, u8 bundle_id)
  54. {
  55. struct manifest_desc *desc, *tmp;
  56. struct greybus_descriptor_cport *desc_cport;
  57. list_for_each_entry_safe(desc, tmp, head, links) {
  58. desc_cport = desc->data;
  59. if (desc->type != GREYBUS_TYPE_CPORT)
  60. continue;
  61. if (desc_cport->bundle == bundle_id)
  62. release_manifest_descriptor(desc);
  63. }
  64. }
  65. static struct manifest_desc *get_next_bundle_desc(struct gb_interface *intf)
  66. {
  67. struct manifest_desc *descriptor;
  68. struct manifest_desc *next;
  69. list_for_each_entry_safe(descriptor, next, &intf->manifest_descs, links)
  70. if (descriptor->type == GREYBUS_TYPE_BUNDLE)
  71. return descriptor;
  72. return NULL;
  73. }
  74. /*
  75. * Validate the given descriptor. Its reported size must fit within
  76. * the number of bytes remaining, and it must have a recognized
  77. * type. Check that the reported size is at least as big as what
  78. * we expect to see. (It could be bigger, perhaps for a new version
  79. * of the format.)
  80. *
  81. * Returns the (non-zero) number of bytes consumed by the descriptor,
  82. * or a negative errno.
  83. */
  84. static int identify_descriptor(struct gb_interface *intf,
  85. struct greybus_descriptor *desc, size_t size)
  86. {
  87. struct greybus_descriptor_header *desc_header = &desc->header;
  88. struct manifest_desc *descriptor;
  89. size_t desc_size;
  90. size_t expected_size;
  91. if (size < sizeof(*desc_header)) {
  92. dev_err(&intf->dev, "manifest too small (%zu < %zu)\n",
  93. size, sizeof(*desc_header));
  94. return -EINVAL; /* Must at least have header */
  95. }
  96. desc_size = le16_to_cpu(desc_header->size);
  97. if (desc_size > size) {
  98. dev_err(&intf->dev, "descriptor too big (%zu > %zu)\n",
  99. desc_size, size);
  100. return -EINVAL;
  101. }
  102. /* Descriptor needs to at least have a header */
  103. expected_size = sizeof(*desc_header);
  104. switch (desc_header->type) {
  105. case GREYBUS_TYPE_STRING:
  106. expected_size += sizeof(struct greybus_descriptor_string);
  107. expected_size += desc->string.length;
  108. /* String descriptors are padded to 4 byte boundaries */
  109. expected_size = ALIGN(expected_size, 4);
  110. break;
  111. case GREYBUS_TYPE_INTERFACE:
  112. expected_size += sizeof(struct greybus_descriptor_interface);
  113. break;
  114. case GREYBUS_TYPE_BUNDLE:
  115. expected_size += sizeof(struct greybus_descriptor_bundle);
  116. break;
  117. case GREYBUS_TYPE_CPORT:
  118. expected_size += sizeof(struct greybus_descriptor_cport);
  119. break;
  120. case GREYBUS_TYPE_INVALID:
  121. default:
  122. dev_err(&intf->dev, "invalid descriptor type (%u)\n",
  123. desc_header->type);
  124. return -EINVAL;
  125. }
  126. if (desc_size < expected_size) {
  127. dev_err(&intf->dev, "%s descriptor too small (%zu < %zu)\n",
  128. get_descriptor_type_string(desc_header->type),
  129. desc_size, expected_size);
  130. return -EINVAL;
  131. }
  132. /* Descriptor bigger than what we expect */
  133. if (desc_size > expected_size) {
  134. dev_warn(&intf->dev, "%s descriptor size mismatch (want %zu got %zu)\n",
  135. get_descriptor_type_string(desc_header->type),
  136. expected_size, desc_size);
  137. }
  138. descriptor = kzalloc(sizeof(*descriptor), GFP_KERNEL);
  139. if (!descriptor)
  140. return -ENOMEM;
  141. descriptor->size = desc_size;
  142. descriptor->data = (char *)desc + sizeof(*desc_header);
  143. descriptor->type = desc_header->type;
  144. list_add_tail(&descriptor->links, &intf->manifest_descs);
  145. /* desc_size is positive and is known to fit in a signed int */
  146. return desc_size;
  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. */
  159. static char *gb_string_get(struct gb_interface *intf, u8 string_id)
  160. {
  161. struct greybus_descriptor_string *desc_string;
  162. struct manifest_desc *descriptor;
  163. bool found = false;
  164. char *string;
  165. /* A zero string id means no string (but no error) */
  166. if (!string_id)
  167. return NULL;
  168. list_for_each_entry(descriptor, &intf->manifest_descs, links) {
  169. if (descriptor->type != GREYBUS_TYPE_STRING)
  170. continue;
  171. desc_string = descriptor->data;
  172. if (desc_string->id == string_id) {
  173. found = true;
  174. break;
  175. }
  176. }
  177. if (!found)
  178. return ERR_PTR(-ENOENT);
  179. /* Allocate an extra byte so we can guarantee it's NUL-terminated */
  180. string = kmemdup(&desc_string->string, desc_string->length + 1,
  181. GFP_KERNEL);
  182. if (!string)
  183. return ERR_PTR(-ENOMEM);
  184. string[desc_string->length] = '\0';
  185. /* Ok we've used this string, so we're done with it */
  186. release_manifest_descriptor(descriptor);
  187. return string;
  188. }
  189. /*
  190. * Find cport descriptors in the manifest associated with the given
  191. * bundle, and set up data structures for the functions that use
  192. * them. Returns the number of cports set up for the bundle, or 0
  193. * if there is an error.
  194. */
  195. static u32 gb_manifest_parse_cports(struct gb_bundle *bundle)
  196. {
  197. struct gb_interface *intf = bundle->intf;
  198. struct greybus_descriptor_cport *desc_cport;
  199. struct manifest_desc *desc, *next, *tmp;
  200. LIST_HEAD(list);
  201. u8 bundle_id = bundle->id;
  202. u16 cport_id;
  203. u32 count = 0;
  204. int i;
  205. /* Set up all cport descriptors associated with this bundle */
  206. list_for_each_entry_safe(desc, next, &intf->manifest_descs, links) {
  207. if (desc->type != GREYBUS_TYPE_CPORT)
  208. continue;
  209. desc_cport = desc->data;
  210. if (desc_cport->bundle != bundle_id)
  211. continue;
  212. cport_id = le16_to_cpu(desc_cport->id);
  213. if (cport_id > CPORT_ID_MAX)
  214. goto exit;
  215. /* Nothing else should have its cport_id as control cport id */
  216. if (cport_id == GB_CONTROL_CPORT_ID) {
  217. dev_err(&bundle->dev, "invalid cport id found (%02u)\n",
  218. cport_id);
  219. goto exit;
  220. }
  221. /*
  222. * Found one, move it to our temporary list after checking for
  223. * duplicates.
  224. */
  225. list_for_each_entry(tmp, &list, links) {
  226. desc_cport = tmp->data;
  227. if (cport_id == le16_to_cpu(desc_cport->id)) {
  228. dev_err(&bundle->dev,
  229. "duplicate CPort %u found\n",
  230. cport_id);
  231. goto exit;
  232. }
  233. }
  234. list_move_tail(&desc->links, &list);
  235. count++;
  236. }
  237. if (!count)
  238. return 0;
  239. bundle->cport_desc = kcalloc(count, sizeof(*bundle->cport_desc),
  240. GFP_KERNEL);
  241. if (!bundle->cport_desc)
  242. goto exit;
  243. bundle->num_cports = count;
  244. i = 0;
  245. list_for_each_entry_safe(desc, next, &list, links) {
  246. desc_cport = desc->data;
  247. memcpy(&bundle->cport_desc[i++], desc_cport,
  248. sizeof(*desc_cport));
  249. /* Release the cport descriptor */
  250. release_manifest_descriptor(desc);
  251. }
  252. return count;
  253. exit:
  254. release_cport_descriptors(&list, bundle_id);
  255. /*
  256. * Free all cports for this bundle to avoid 'excess descriptors'
  257. * warnings.
  258. */
  259. release_cport_descriptors(&intf->manifest_descs, bundle_id);
  260. return 0; /* Error; count should also be 0 */
  261. }
  262. /*
  263. * Find bundle descriptors in the manifest and set up their data
  264. * structures. Returns the number of bundles set up for the
  265. * given interface.
  266. */
  267. static u32 gb_manifest_parse_bundles(struct gb_interface *intf)
  268. {
  269. struct manifest_desc *desc;
  270. struct gb_bundle *bundle;
  271. struct gb_bundle *bundle_next;
  272. u32 count = 0;
  273. u8 bundle_id;
  274. u8 class;
  275. while ((desc = get_next_bundle_desc(intf))) {
  276. struct greybus_descriptor_bundle *desc_bundle;
  277. /* Found one. Set up its bundle structure*/
  278. desc_bundle = desc->data;
  279. bundle_id = desc_bundle->id;
  280. class = desc_bundle->class;
  281. /* Done with this bundle descriptor */
  282. release_manifest_descriptor(desc);
  283. /* Ignore any legacy control bundles */
  284. if (bundle_id == GB_CONTROL_BUNDLE_ID) {
  285. dev_dbg(&intf->dev, "%s - ignoring control bundle\n",
  286. __func__);
  287. release_cport_descriptors(&intf->manifest_descs,
  288. bundle_id);
  289. continue;
  290. }
  291. /* Nothing else should have its class set to control class */
  292. if (class == GREYBUS_CLASS_CONTROL) {
  293. dev_err(&intf->dev,
  294. "bundle %u cannot use control class\n",
  295. bundle_id);
  296. goto cleanup;
  297. }
  298. bundle = gb_bundle_create(intf, bundle_id, class);
  299. if (!bundle)
  300. goto cleanup;
  301. /*
  302. * Now go set up this bundle's functions and cports.
  303. *
  304. * A 'bundle' represents a device in greybus. It may require
  305. * multiple cports for its functioning. If we fail to setup any
  306. * cport of a bundle, we better reject the complete bundle as
  307. * the device may not be able to function properly then.
  308. *
  309. * But, failing to setup a cport of bundle X doesn't mean that
  310. * the device corresponding to bundle Y will not work properly.
  311. * Bundles should be treated as separate independent devices.
  312. *
  313. * While parsing manifest for an interface, treat bundles as
  314. * separate entities and don't reject entire interface and its
  315. * bundles on failing to initialize a cport. But make sure the
  316. * bundle which needs the cport, gets destroyed properly.
  317. */
  318. if (!gb_manifest_parse_cports(bundle)) {
  319. gb_bundle_destroy(bundle);
  320. continue;
  321. }
  322. count++;
  323. }
  324. return count;
  325. cleanup:
  326. /* An error occurred; undo any changes we've made */
  327. list_for_each_entry_safe(bundle, bundle_next, &intf->bundles, links) {
  328. gb_bundle_destroy(bundle);
  329. count--;
  330. }
  331. return 0; /* Error; count should also be 0 */
  332. }
  333. static bool gb_manifest_parse_interface(struct gb_interface *intf,
  334. struct manifest_desc *interface_desc)
  335. {
  336. struct greybus_descriptor_interface *desc_intf = interface_desc->data;
  337. struct gb_control *control = intf->control;
  338. char *str;
  339. /* Handle the strings first--they can fail */
  340. str = gb_string_get(intf, desc_intf->vendor_stringid);
  341. if (IS_ERR(str))
  342. return false;
  343. control->vendor_string = str;
  344. str = gb_string_get(intf, desc_intf->product_stringid);
  345. if (IS_ERR(str))
  346. goto out_free_vendor_string;
  347. control->product_string = str;
  348. /* Assign feature flags communicated via manifest */
  349. intf->features = desc_intf->features;
  350. /* Release the interface descriptor, now that we're done with it */
  351. release_manifest_descriptor(interface_desc);
  352. /* An interface must have at least one bundle descriptor */
  353. if (!gb_manifest_parse_bundles(intf)) {
  354. dev_err(&intf->dev, "manifest bundle descriptors not valid\n");
  355. goto out_err;
  356. }
  357. return true;
  358. out_err:
  359. kfree(control->product_string);
  360. control->product_string = NULL;
  361. out_free_vendor_string:
  362. kfree(control->vendor_string);
  363. control->vendor_string = NULL;
  364. return false;
  365. }
  366. /*
  367. * Parse a buffer containing an interface manifest.
  368. *
  369. * If we find anything wrong with the content/format of the buffer
  370. * we reject it.
  371. *
  372. * The first requirement is that the manifest's version is
  373. * one we can parse.
  374. *
  375. * We make an initial pass through the buffer and identify all of
  376. * the descriptors it contains, keeping track for each its type
  377. * and the location size of its data in the buffer.
  378. *
  379. * Next we scan the descriptors, looking for an interface descriptor;
  380. * there must be exactly one of those. When found, we record the
  381. * information it contains, and then remove that descriptor (and any
  382. * string descriptors it refers to) from further consideration.
  383. *
  384. * After that we look for the interface's bundles--there must be at
  385. * least one of those.
  386. *
  387. * Returns true if parsing was successful, false otherwise.
  388. */
  389. bool gb_manifest_parse(struct gb_interface *intf, void *data, size_t size)
  390. {
  391. struct greybus_manifest *manifest;
  392. struct greybus_manifest_header *header;
  393. struct greybus_descriptor *desc;
  394. struct manifest_desc *descriptor;
  395. struct manifest_desc *interface_desc = NULL;
  396. u16 manifest_size;
  397. u32 found = 0;
  398. bool result;
  399. /* Manifest descriptor list should be empty here */
  400. if (WARN_ON(!list_empty(&intf->manifest_descs)))
  401. return false;
  402. /* we have to have at _least_ the manifest header */
  403. if (size < sizeof(*header)) {
  404. dev_err(&intf->dev, "short manifest (%zu < %zu)\n",
  405. size, sizeof(*header));
  406. return false;
  407. }
  408. /* Make sure the size is right */
  409. manifest = data;
  410. header = &manifest->header;
  411. manifest_size = le16_to_cpu(header->size);
  412. if (manifest_size != size) {
  413. dev_err(&intf->dev, "manifest size mismatch (%zu != %u)\n",
  414. size, manifest_size);
  415. return false;
  416. }
  417. /* Validate major/minor number */
  418. if (header->version_major > GREYBUS_VERSION_MAJOR) {
  419. dev_err(&intf->dev, "manifest version too new (%u.%u > %u.%u)\n",
  420. header->version_major, header->version_minor,
  421. GREYBUS_VERSION_MAJOR, GREYBUS_VERSION_MINOR);
  422. return false;
  423. }
  424. /* OK, find all the descriptors */
  425. desc = manifest->descriptors;
  426. size -= sizeof(*header);
  427. while (size) {
  428. int desc_size;
  429. desc_size = identify_descriptor(intf, desc, size);
  430. if (desc_size < 0) {
  431. result = false;
  432. goto out;
  433. }
  434. desc = (struct greybus_descriptor *)((char *)desc + desc_size);
  435. size -= desc_size;
  436. }
  437. /* There must be a single interface descriptor */
  438. list_for_each_entry(descriptor, &intf->manifest_descs, links) {
  439. if (descriptor->type == GREYBUS_TYPE_INTERFACE)
  440. if (!found++)
  441. interface_desc = descriptor;
  442. }
  443. if (found != 1) {
  444. dev_err(&intf->dev, "manifest must have 1 interface descriptor (%u found)\n",
  445. found);
  446. result = false;
  447. goto out;
  448. }
  449. /* Parse the manifest, starting with the interface descriptor */
  450. result = gb_manifest_parse_interface(intf, interface_desc);
  451. /*
  452. * We really should have no remaining descriptors, but we
  453. * don't know what newer format manifests might leave.
  454. */
  455. if (result && !list_empty(&intf->manifest_descs))
  456. dev_info(&intf->dev, "excess descriptors in interface manifest\n");
  457. out:
  458. release_manifest_descriptors(intf);
  459. return result;
  460. }