pldmfw.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2018-2019, Intel Corporation. */
  3. #include <asm/unaligned.h>
  4. #include <linux/crc32.h>
  5. #include <linux/device.h>
  6. #include <linux/firmware.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/pci.h>
  10. #include <linux/pldmfw.h>
  11. #include <linux/slab.h>
  12. #include <linux/uuid.h>
  13. #include "pldmfw_private.h"
  14. /* Internal structure used to store details about the PLDM image file as it is
  15. * being validated and processed.
  16. */
  17. struct pldmfw_priv {
  18. struct pldmfw *context;
  19. const struct firmware *fw;
  20. /* current offset of firmware image */
  21. size_t offset;
  22. struct list_head records;
  23. struct list_head components;
  24. /* PLDM Firmware Package Header */
  25. const struct __pldm_header *header;
  26. u16 total_header_size;
  27. /* length of the component bitmap */
  28. u16 component_bitmap_len;
  29. u16 bitmap_size;
  30. /* Start of the component image information */
  31. u16 component_count;
  32. const u8 *component_start;
  33. /* Start pf the firmware device id records */
  34. const u8 *record_start;
  35. u8 record_count;
  36. /* The CRC at the end of the package header */
  37. u32 header_crc;
  38. struct pldmfw_record *matching_record;
  39. };
  40. /**
  41. * pldm_check_fw_space - Verify that the firmware image has space left
  42. * @data: pointer to private data
  43. * @offset: offset to start from
  44. * @length: length to check for
  45. *
  46. * Verify that the firmware data can hold a chunk of bytes with the specified
  47. * offset and length.
  48. *
  49. * Returns: zero on success, or -EFAULT if the image does not have enough
  50. * space left to fit the expected length.
  51. */
  52. static int
  53. pldm_check_fw_space(struct pldmfw_priv *data, size_t offset, size_t length)
  54. {
  55. size_t expected_size = offset + length;
  56. struct device *dev = data->context->dev;
  57. if (data->fw->size < expected_size) {
  58. dev_dbg(dev, "Firmware file size smaller than expected. Got %zu bytes, needed %zu bytes\n",
  59. data->fw->size, expected_size);
  60. return -EFAULT;
  61. }
  62. return 0;
  63. }
  64. /**
  65. * pldm_move_fw_offset - Move the current firmware offset forward
  66. * @data: pointer to private data
  67. * @bytes_to_move: number of bytes to move the offset forward by
  68. *
  69. * Check that there is enough space past the current offset, and then move the
  70. * offset forward by this amount.
  71. *
  72. * Returns: zero on success, or -EFAULT if the image is too small to fit the
  73. * expected length.
  74. */
  75. static int
  76. pldm_move_fw_offset(struct pldmfw_priv *data, size_t bytes_to_move)
  77. {
  78. int err;
  79. err = pldm_check_fw_space(data, data->offset, bytes_to_move);
  80. if (err)
  81. return err;
  82. data->offset += bytes_to_move;
  83. return 0;
  84. }
  85. /**
  86. * pldm_parse_header - Validate and extract details about the PLDM header
  87. * @data: pointer to private data
  88. *
  89. * Performs initial basic verification of the PLDM image, up to the first
  90. * firmware record.
  91. *
  92. * This includes the following checks and extractions
  93. *
  94. * * Verify that the UUID at the start of the header matches the expected
  95. * value as defined in the DSP0267 PLDM specification
  96. * * Check that the revision is 0x01
  97. * * Extract the total header_size and verify that the image is large enough
  98. * to contain at least the length of this header
  99. * * Extract the size of the component bitmap length
  100. * * Extract a pointer to the start of the record area
  101. *
  102. * Returns: zero on success, or a negative error code on failure.
  103. */
  104. static int pldm_parse_header(struct pldmfw_priv *data)
  105. {
  106. const struct __pldmfw_record_area *record_area;
  107. struct device *dev = data->context->dev;
  108. const struct __pldm_header *header;
  109. size_t header_size;
  110. int err;
  111. err = pldm_move_fw_offset(data, sizeof(*header));
  112. if (err)
  113. return err;
  114. header = (const struct __pldm_header *)data->fw->data;
  115. data->header = header;
  116. if (!uuid_equal(&header->id, &pldm_firmware_header_id)) {
  117. dev_dbg(dev, "Invalid package header identifier. Expected UUID %pUB, but got %pUB\n",
  118. &pldm_firmware_header_id, &header->id);
  119. return -EINVAL;
  120. }
  121. if (header->revision != PACKAGE_HEADER_FORMAT_REVISION) {
  122. dev_dbg(dev, "Invalid package header revision. Expected revision %u but got %u\n",
  123. PACKAGE_HEADER_FORMAT_REVISION, header->revision);
  124. return -EOPNOTSUPP;
  125. }
  126. data->total_header_size = get_unaligned_le16(&header->size);
  127. header_size = data->total_header_size - sizeof(*header);
  128. err = pldm_check_fw_space(data, data->offset, header_size);
  129. if (err)
  130. return err;
  131. data->component_bitmap_len =
  132. get_unaligned_le16(&header->component_bitmap_len);
  133. if (data->component_bitmap_len % 8 != 0) {
  134. dev_dbg(dev, "Invalid component bitmap length. The length is %u, which is not a multiple of 8\n",
  135. data->component_bitmap_len);
  136. return -EINVAL;
  137. }
  138. data->bitmap_size = data->component_bitmap_len / 8;
  139. err = pldm_move_fw_offset(data, header->version_len);
  140. if (err)
  141. return err;
  142. /* extract a pointer to the record area, which just follows the main
  143. * PLDM header data.
  144. */
  145. record_area = (const struct __pldmfw_record_area *)(data->fw->data +
  146. data->offset);
  147. err = pldm_move_fw_offset(data, sizeof(*record_area));
  148. if (err)
  149. return err;
  150. data->record_count = record_area->record_count;
  151. data->record_start = record_area->records;
  152. return 0;
  153. }
  154. /**
  155. * pldm_check_desc_tlv_len - Check that the length matches expectation
  156. * @data: pointer to image details
  157. * @type: the descriptor type
  158. * @size: the length from the descriptor header
  159. *
  160. * If the descriptor type is one of the documented descriptor types according
  161. * to the standard, verify that the provided length matches.
  162. *
  163. * If the type is not recognized or is VENDOR_DEFINED, return zero.
  164. *
  165. * Returns: zero on success, or -EINVAL if the specified size of a standard
  166. * TLV does not match the expected value defined for that TLV.
  167. */
  168. static int
  169. pldm_check_desc_tlv_len(struct pldmfw_priv *data, u16 type, u16 size)
  170. {
  171. struct device *dev = data->context->dev;
  172. u16 expected_size;
  173. switch (type) {
  174. case PLDM_DESC_ID_PCI_VENDOR_ID:
  175. case PLDM_DESC_ID_PCI_DEVICE_ID:
  176. case PLDM_DESC_ID_PCI_SUBVENDOR_ID:
  177. case PLDM_DESC_ID_PCI_SUBDEV_ID:
  178. expected_size = 2;
  179. break;
  180. case PLDM_DESC_ID_PCI_REVISION_ID:
  181. expected_size = 1;
  182. break;
  183. case PLDM_DESC_ID_PNP_VENDOR_ID:
  184. expected_size = 3;
  185. break;
  186. case PLDM_DESC_ID_IANA_ENTERPRISE_ID:
  187. case PLDM_DESC_ID_ACPI_VENDOR_ID:
  188. case PLDM_DESC_ID_PNP_PRODUCT_ID:
  189. case PLDM_DESC_ID_ACPI_PRODUCT_ID:
  190. expected_size = 4;
  191. break;
  192. case PLDM_DESC_ID_UUID:
  193. expected_size = 16;
  194. break;
  195. case PLDM_DESC_ID_VENDOR_DEFINED:
  196. return 0;
  197. default:
  198. /* Do not report an error on an unexpected TLV */
  199. dev_dbg(dev, "Found unrecognized TLV type 0x%04x\n", type);
  200. return 0;
  201. }
  202. if (size != expected_size) {
  203. dev_dbg(dev, "Found TLV type 0x%04x with unexpected length. Got %u bytes, but expected %u bytes\n",
  204. type, size, expected_size);
  205. return -EINVAL;
  206. }
  207. return 0;
  208. }
  209. /**
  210. * pldm_parse_desc_tlvs - Check and skip past a number of TLVs
  211. * @data: pointer to private data
  212. * @record: pointer to the record this TLV belongs too
  213. * @desc_count: descriptor count
  214. *
  215. * From the current offset, read and extract the descriptor TLVs, updating the
  216. * current offset each time.
  217. *
  218. * Returns: zero on success, or a negative error code on failure.
  219. */
  220. static int
  221. pldm_parse_desc_tlvs(struct pldmfw_priv *data, struct pldmfw_record *record, u8 desc_count)
  222. {
  223. const struct __pldmfw_desc_tlv *__desc;
  224. const u8 *desc_start;
  225. u8 i;
  226. desc_start = data->fw->data + data->offset;
  227. pldm_for_each_desc_tlv(i, __desc, desc_start, desc_count) {
  228. struct pldmfw_desc_tlv *desc;
  229. int err;
  230. u16 type, size;
  231. err = pldm_move_fw_offset(data, sizeof(*__desc));
  232. if (err)
  233. return err;
  234. type = get_unaligned_le16(&__desc->type);
  235. /* According to DSP0267, this only includes the data field */
  236. size = get_unaligned_le16(&__desc->size);
  237. err = pldm_check_desc_tlv_len(data, type, size);
  238. if (err)
  239. return err;
  240. /* check that we have space and move the offset forward */
  241. err = pldm_move_fw_offset(data, size);
  242. if (err)
  243. return err;
  244. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  245. if (!desc)
  246. return -ENOMEM;
  247. desc->type = type;
  248. desc->size = size;
  249. desc->data = __desc->data;
  250. list_add_tail(&desc->entry, &record->descs);
  251. }
  252. return 0;
  253. }
  254. /**
  255. * pldm_parse_one_record - Verify size of one PLDM record
  256. * @data: pointer to image details
  257. * @__record: pointer to the record to check
  258. *
  259. * This function checks that the record size does not exceed either the size
  260. * of the firmware file or the total length specified in the header section.
  261. *
  262. * It also verifies that the recorded length of the start of the record
  263. * matches the size calculated by adding the static structure length, the
  264. * component bitmap length, the version string length, the length of all
  265. * descriptor TLVs, and the length of the package data.
  266. *
  267. * Returns: zero on success, or a negative error code on failure.
  268. */
  269. static int
  270. pldm_parse_one_record(struct pldmfw_priv *data,
  271. const struct __pldmfw_record_info *__record)
  272. {
  273. struct pldmfw_record *record;
  274. size_t measured_length;
  275. int err;
  276. const u8 *bitmap_ptr;
  277. u16 record_len;
  278. int i;
  279. /* Make a copy and insert it into the record list */
  280. record = kzalloc(sizeof(*record), GFP_KERNEL);
  281. if (!record)
  282. return -ENOMEM;
  283. INIT_LIST_HEAD(&record->descs);
  284. list_add_tail(&record->entry, &data->records);
  285. /* Then check that we have space and move the offset */
  286. err = pldm_move_fw_offset(data, sizeof(*__record));
  287. if (err)
  288. return err;
  289. record_len = get_unaligned_le16(&__record->record_len);
  290. record->package_data_len = get_unaligned_le16(&__record->package_data_len);
  291. record->version_len = __record->version_len;
  292. record->version_type = __record->version_type;
  293. bitmap_ptr = data->fw->data + data->offset;
  294. /* check that we have space for the component bitmap length */
  295. err = pldm_move_fw_offset(data, data->bitmap_size);
  296. if (err)
  297. return err;
  298. record->component_bitmap_len = data->component_bitmap_len;
  299. record->component_bitmap = bitmap_zalloc(record->component_bitmap_len,
  300. GFP_KERNEL);
  301. if (!record->component_bitmap)
  302. return -ENOMEM;
  303. for (i = 0; i < data->bitmap_size; i++)
  304. bitmap_set_value8(record->component_bitmap, bitmap_ptr[i], i * 8);
  305. record->version_string = data->fw->data + data->offset;
  306. err = pldm_move_fw_offset(data, record->version_len);
  307. if (err)
  308. return err;
  309. /* Scan through the descriptor TLVs and find the end */
  310. err = pldm_parse_desc_tlvs(data, record, __record->descriptor_count);
  311. if (err)
  312. return err;
  313. record->package_data = data->fw->data + data->offset;
  314. err = pldm_move_fw_offset(data, record->package_data_len);
  315. if (err)
  316. return err;
  317. measured_length = data->offset - ((const u8 *)__record - data->fw->data);
  318. if (measured_length != record_len) {
  319. dev_dbg(data->context->dev, "Unexpected record length. Measured record length is %zu bytes, expected length is %u bytes\n",
  320. measured_length, record_len);
  321. return -EFAULT;
  322. }
  323. return 0;
  324. }
  325. /**
  326. * pldm_parse_records - Locate the start of the component area
  327. * @data: pointer to private data
  328. *
  329. * Extract the record count, and loop through each record, searching for the
  330. * component area.
  331. *
  332. * Returns: zero on success, or a negative error code on failure.
  333. */
  334. static int pldm_parse_records(struct pldmfw_priv *data)
  335. {
  336. const struct __pldmfw_component_area *component_area;
  337. const struct __pldmfw_record_info *record;
  338. int err;
  339. u8 i;
  340. pldm_for_each_record(i, record, data->record_start, data->record_count) {
  341. err = pldm_parse_one_record(data, record);
  342. if (err)
  343. return err;
  344. }
  345. /* Extract a pointer to the component area, which just follows the
  346. * PLDM device record data.
  347. */
  348. component_area = (const struct __pldmfw_component_area *)(data->fw->data + data->offset);
  349. err = pldm_move_fw_offset(data, sizeof(*component_area));
  350. if (err)
  351. return err;
  352. data->component_count =
  353. get_unaligned_le16(&component_area->component_image_count);
  354. data->component_start = component_area->components;
  355. return 0;
  356. }
  357. /**
  358. * pldm_parse_components - Locate the CRC header checksum
  359. * @data: pointer to private data
  360. *
  361. * Extract the component count, and find the pointer to the component area.
  362. * Scan through each component searching for the end, which should point to
  363. * the package header checksum.
  364. *
  365. * Extract the package header CRC and save it for verification.
  366. *
  367. * Returns: zero on success, or a negative error code on failure.
  368. */
  369. static int pldm_parse_components(struct pldmfw_priv *data)
  370. {
  371. const struct __pldmfw_component_info *__component;
  372. struct device *dev = data->context->dev;
  373. const u8 *header_crc_ptr;
  374. int err;
  375. u8 i;
  376. pldm_for_each_component(i, __component, data->component_start, data->component_count) {
  377. struct pldmfw_component *component;
  378. u32 offset, size;
  379. err = pldm_move_fw_offset(data, sizeof(*__component));
  380. if (err)
  381. return err;
  382. err = pldm_move_fw_offset(data, __component->version_len);
  383. if (err)
  384. return err;
  385. offset = get_unaligned_le32(&__component->location_offset);
  386. size = get_unaligned_le32(&__component->size);
  387. err = pldm_check_fw_space(data, offset, size);
  388. if (err)
  389. return err;
  390. component = kzalloc(sizeof(*component), GFP_KERNEL);
  391. if (!component)
  392. return -ENOMEM;
  393. component->index = i;
  394. component->classification = get_unaligned_le16(&__component->classification);
  395. component->identifier = get_unaligned_le16(&__component->identifier);
  396. component->comparison_stamp = get_unaligned_le32(&__component->comparison_stamp);
  397. component->options = get_unaligned_le16(&__component->options);
  398. component->activation_method = get_unaligned_le16(&__component->activation_method);
  399. component->version_type = __component->version_type;
  400. component->version_len = __component->version_len;
  401. component->version_string = __component->version_string;
  402. component->component_data = data->fw->data + offset;
  403. component->component_size = size;
  404. list_add_tail(&component->entry, &data->components);
  405. }
  406. header_crc_ptr = data->fw->data + data->offset;
  407. err = pldm_move_fw_offset(data, sizeof(data->header_crc));
  408. if (err)
  409. return err;
  410. /* Make sure that we reached the expected offset */
  411. if (data->offset != data->total_header_size) {
  412. dev_dbg(dev, "Invalid firmware header size. Expected %u but got %zu\n",
  413. data->total_header_size, data->offset);
  414. return -EFAULT;
  415. }
  416. data->header_crc = get_unaligned_le32(header_crc_ptr);
  417. return 0;
  418. }
  419. /**
  420. * pldm_verify_header_crc - Verify that the CRC in the header matches
  421. * @data: pointer to private data
  422. *
  423. * Calculates the 32-bit CRC using the standard IEEE 802.3 CRC polynomial and
  424. * compares it to the value stored in the header.
  425. *
  426. * Returns: zero on success if the CRC matches, or -EBADMSG on an invalid CRC.
  427. */
  428. static int pldm_verify_header_crc(struct pldmfw_priv *data)
  429. {
  430. struct device *dev = data->context->dev;
  431. u32 calculated_crc;
  432. size_t length;
  433. /* Calculate the 32-bit CRC of the header header contents up to but
  434. * not including the checksum. Note that the Linux crc32_le function
  435. * does not perform an expected final XOR.
  436. */
  437. length = data->offset - sizeof(data->header_crc);
  438. calculated_crc = crc32_le(~0, data->fw->data, length) ^ ~0;
  439. if (calculated_crc != data->header_crc) {
  440. dev_dbg(dev, "Invalid CRC in firmware header. Got 0x%08x but expected 0x%08x\n",
  441. calculated_crc, data->header_crc);
  442. return -EBADMSG;
  443. }
  444. return 0;
  445. }
  446. /**
  447. * pldmfw_free_priv - Free memory allocated while parsing the PLDM image
  448. * @data: pointer to the PLDM data structure
  449. *
  450. * Loops through and clears all allocated memory associated with each
  451. * allocated descriptor, record, and component.
  452. */
  453. static void pldmfw_free_priv(struct pldmfw_priv *data)
  454. {
  455. struct pldmfw_component *component, *c_safe;
  456. struct pldmfw_record *record, *r_safe;
  457. struct pldmfw_desc_tlv *desc, *d_safe;
  458. list_for_each_entry_safe(component, c_safe, &data->components, entry) {
  459. list_del(&component->entry);
  460. kfree(component);
  461. }
  462. list_for_each_entry_safe(record, r_safe, &data->records, entry) {
  463. list_for_each_entry_safe(desc, d_safe, &record->descs, entry) {
  464. list_del(&desc->entry);
  465. kfree(desc);
  466. }
  467. if (record->component_bitmap) {
  468. bitmap_free(record->component_bitmap);
  469. record->component_bitmap = NULL;
  470. }
  471. list_del(&record->entry);
  472. kfree(record);
  473. }
  474. }
  475. /**
  476. * pldm_parse_image - parse and extract details from PLDM image
  477. * @data: pointer to private data
  478. *
  479. * Verify that the firmware file contains valid data for a PLDM firmware
  480. * file. Extract useful pointers and data from the firmware file and store
  481. * them in the data structure.
  482. *
  483. * The PLDM firmware file format is defined in DMTF DSP0267 1.0.0. Care
  484. * should be taken to use get_unaligned_le* when accessing data from the
  485. * pointers in data.
  486. *
  487. * Returns: zero on success, or a negative error code on failure.
  488. */
  489. static int pldm_parse_image(struct pldmfw_priv *data)
  490. {
  491. int err;
  492. if (WARN_ON(!(data->context->dev && data->fw->data && data->fw->size)))
  493. return -EINVAL;
  494. err = pldm_parse_header(data);
  495. if (err)
  496. return err;
  497. err = pldm_parse_records(data);
  498. if (err)
  499. return err;
  500. err = pldm_parse_components(data);
  501. if (err)
  502. return err;
  503. return pldm_verify_header_crc(data);
  504. }
  505. /* these are u32 so that we can store PCI_ANY_ID */
  506. struct pldm_pci_record_id {
  507. int vendor;
  508. int device;
  509. int subsystem_vendor;
  510. int subsystem_device;
  511. };
  512. /**
  513. * pldmfw_op_pci_match_record - Check if a PCI device matches the record
  514. * @context: PLDM fw update structure
  515. * @record: list of records extracted from the PLDM image
  516. *
  517. * Determine of the PCI device associated with this device matches the record
  518. * data provided.
  519. *
  520. * Searches the descriptor TLVs and extracts the relevant descriptor data into
  521. * a pldm_pci_record_id. This is then compared against the PCI device ID
  522. * information.
  523. *
  524. * Returns: true if the device matches the record, false otherwise.
  525. */
  526. bool pldmfw_op_pci_match_record(struct pldmfw *context, struct pldmfw_record *record)
  527. {
  528. struct pci_dev *pdev = to_pci_dev(context->dev);
  529. struct pldm_pci_record_id id = {
  530. .vendor = PCI_ANY_ID,
  531. .device = PCI_ANY_ID,
  532. .subsystem_vendor = PCI_ANY_ID,
  533. .subsystem_device = PCI_ANY_ID,
  534. };
  535. struct pldmfw_desc_tlv *desc;
  536. list_for_each_entry(desc, &record->descs, entry) {
  537. u16 value;
  538. int *ptr;
  539. switch (desc->type) {
  540. case PLDM_DESC_ID_PCI_VENDOR_ID:
  541. ptr = &id.vendor;
  542. break;
  543. case PLDM_DESC_ID_PCI_DEVICE_ID:
  544. ptr = &id.device;
  545. break;
  546. case PLDM_DESC_ID_PCI_SUBVENDOR_ID:
  547. ptr = &id.subsystem_vendor;
  548. break;
  549. case PLDM_DESC_ID_PCI_SUBDEV_ID:
  550. ptr = &id.subsystem_device;
  551. break;
  552. default:
  553. /* Skip unrelated TLVs */
  554. continue;
  555. }
  556. value = get_unaligned_le16(desc->data);
  557. /* A value of zero for one of the descriptors is sometimes
  558. * used when the record should ignore this field when matching
  559. * device. For example if the record applies to any subsystem
  560. * device or vendor.
  561. */
  562. if (value)
  563. *ptr = (int)value;
  564. else
  565. *ptr = PCI_ANY_ID;
  566. }
  567. if ((id.vendor == PCI_ANY_ID || id.vendor == pdev->vendor) &&
  568. (id.device == PCI_ANY_ID || id.device == pdev->device) &&
  569. (id.subsystem_vendor == PCI_ANY_ID || id.subsystem_vendor == pdev->subsystem_vendor) &&
  570. (id.subsystem_device == PCI_ANY_ID || id.subsystem_device == pdev->subsystem_device))
  571. return true;
  572. else
  573. return false;
  574. }
  575. EXPORT_SYMBOL(pldmfw_op_pci_match_record);
  576. /**
  577. * pldm_find_matching_record - Find the first matching PLDM record
  578. * @data: pointer to private data
  579. *
  580. * Search through PLDM records and find the first matching entry. It is
  581. * expected that only one entry matches.
  582. *
  583. * Store a pointer to the matching record, if found.
  584. *
  585. * Returns: zero on success, or -ENOENT if no matching record is found.
  586. */
  587. static int pldm_find_matching_record(struct pldmfw_priv *data)
  588. {
  589. struct pldmfw_record *record;
  590. list_for_each_entry(record, &data->records, entry) {
  591. if (data->context->ops->match_record(data->context, record)) {
  592. data->matching_record = record;
  593. return 0;
  594. }
  595. }
  596. return -ENOENT;
  597. }
  598. /**
  599. * pldm_send_package_data - Send firmware the package data for the record
  600. * @data: pointer to private data
  601. *
  602. * Send the package data associated with the matching record to the firmware,
  603. * using the send_pkg_data operation.
  604. *
  605. * Returns: zero on success, or a negative error code on failure.
  606. */
  607. static int
  608. pldm_send_package_data(struct pldmfw_priv *data)
  609. {
  610. struct pldmfw_record *record = data->matching_record;
  611. const struct pldmfw_ops *ops = data->context->ops;
  612. return ops->send_package_data(data->context, record->package_data,
  613. record->package_data_len);
  614. }
  615. /**
  616. * pldm_send_component_tables - Send component table information to firmware
  617. * @data: pointer to private data
  618. *
  619. * Loop over each component, sending the applicable components to the firmware
  620. * via the send_component_table operation.
  621. *
  622. * Returns: zero on success, or a negative error code on failure.
  623. */
  624. static int
  625. pldm_send_component_tables(struct pldmfw_priv *data)
  626. {
  627. unsigned long *bitmap = data->matching_record->component_bitmap;
  628. struct pldmfw_component *component;
  629. int err;
  630. list_for_each_entry(component, &data->components, entry) {
  631. u8 index = component->index, transfer_flag = 0;
  632. /* Skip components which are not intended for this device */
  633. if (!test_bit(index, bitmap))
  634. continue;
  635. /* determine whether this is the start, middle, end, or both
  636. * the start and end of the component tables
  637. */
  638. if (index == find_first_bit(bitmap, data->component_bitmap_len))
  639. transfer_flag |= PLDM_TRANSFER_FLAG_START;
  640. if (index == find_last_bit(bitmap, data->component_bitmap_len))
  641. transfer_flag |= PLDM_TRANSFER_FLAG_END;
  642. if (!transfer_flag)
  643. transfer_flag = PLDM_TRANSFER_FLAG_MIDDLE;
  644. err = data->context->ops->send_component_table(data->context,
  645. component,
  646. transfer_flag);
  647. if (err)
  648. return err;
  649. }
  650. return 0;
  651. }
  652. /**
  653. * pldm_flash_components - Program each component to device flash
  654. * @data: pointer to private data
  655. *
  656. * Loop through each component that is active for the matching device record,
  657. * and send it to the device driver for flashing.
  658. *
  659. * Returns: zero on success, or a negative error code on failure.
  660. */
  661. static int pldm_flash_components(struct pldmfw_priv *data)
  662. {
  663. unsigned long *bitmap = data->matching_record->component_bitmap;
  664. struct pldmfw_component *component;
  665. int err;
  666. list_for_each_entry(component, &data->components, entry) {
  667. u8 index = component->index;
  668. /* Skip components which are not intended for this device */
  669. if (!test_bit(index, bitmap))
  670. continue;
  671. err = data->context->ops->flash_component(data->context, component);
  672. if (err)
  673. return err;
  674. }
  675. return 0;
  676. }
  677. /**
  678. * pldm_finalize_update - Finalize the device flash update
  679. * @data: pointer to private data
  680. *
  681. * Tell the device driver to perform any remaining logic to complete the
  682. * device update.
  683. *
  684. * Returns: zero on success, or a PLFM_FWU error indicating the reason for
  685. * failure.
  686. */
  687. static int pldm_finalize_update(struct pldmfw_priv *data)
  688. {
  689. if (data->context->ops->finalize_update)
  690. return data->context->ops->finalize_update(data->context);
  691. return 0;
  692. }
  693. /**
  694. * pldmfw_flash_image - Write a PLDM-formatted firmware image to the device
  695. * @context: ops and data for firmware update
  696. * @fw: firmware object pointing to the relevant firmware file to program
  697. *
  698. * Parse the data for a given firmware file, verifying that it is a valid PLDM
  699. * formatted image that matches this device.
  700. *
  701. * Extract the device record Package Data and Component Tables and send them
  702. * to the device firmware. Extract and write the flash data for each of the
  703. * components indicated in the firmware file.
  704. *
  705. * Returns: zero on success, or a negative error code on failure.
  706. */
  707. int pldmfw_flash_image(struct pldmfw *context, const struct firmware *fw)
  708. {
  709. struct pldmfw_priv *data;
  710. int err;
  711. data = kzalloc(sizeof(*data), GFP_KERNEL);
  712. if (!data)
  713. return -ENOMEM;
  714. INIT_LIST_HEAD(&data->records);
  715. INIT_LIST_HEAD(&data->components);
  716. data->fw = fw;
  717. data->context = context;
  718. err = pldm_parse_image(data);
  719. if (err)
  720. goto out_release_data;
  721. err = pldm_find_matching_record(data);
  722. if (err)
  723. goto out_release_data;
  724. err = pldm_send_package_data(data);
  725. if (err)
  726. goto out_release_data;
  727. err = pldm_send_component_tables(data);
  728. if (err)
  729. goto out_release_data;
  730. err = pldm_flash_components(data);
  731. if (err)
  732. goto out_release_data;
  733. err = pldm_finalize_update(data);
  734. out_release_data:
  735. pldmfw_free_priv(data);
  736. kfree(data);
  737. return err;
  738. }
  739. EXPORT_SYMBOL(pldmfw_flash_image);
  740. MODULE_AUTHOR("Jacob Keller <[email protected]>");
  741. MODULE_LICENSE("GPL v2");
  742. MODULE_DESCRIPTION("PLDM firmware flash update library");