manifest.c 14 KB

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