pldmfw.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2018-2019, Intel Corporation. */
  3. #include <linux/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_obj(*desc);
  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_obj(*record);
  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_obj(*component);
  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. if (data->context->mode == PLDMFW_UPDATE_MODE_SINGLE_COMPONENT &&
  405. data->context->component_identifier != component->identifier)
  406. continue;
  407. list_add_tail(&component->entry, &data->components);
  408. }
  409. if (data->context->mode == PLDMFW_UPDATE_MODE_SINGLE_COMPONENT &&
  410. list_empty(&data->components))
  411. return -ENOENT;
  412. header_crc_ptr = data->fw->data + data->offset;
  413. err = pldm_move_fw_offset(data, sizeof(data->header_crc));
  414. if (err)
  415. return err;
  416. /* Make sure that we reached the expected offset */
  417. if (data->offset != data->total_header_size) {
  418. dev_dbg(dev, "Invalid firmware header size. Expected %u but got %zu\n",
  419. data->total_header_size, data->offset);
  420. return -EFAULT;
  421. }
  422. data->header_crc = get_unaligned_le32(header_crc_ptr);
  423. return 0;
  424. }
  425. /**
  426. * pldm_verify_header_crc - Verify that the CRC in the header matches
  427. * @data: pointer to private data
  428. *
  429. * Calculates the 32-bit CRC using the standard IEEE 802.3 CRC polynomial and
  430. * compares it to the value stored in the header.
  431. *
  432. * Returns: zero on success if the CRC matches, or -EBADMSG on an invalid CRC.
  433. */
  434. static int pldm_verify_header_crc(struct pldmfw_priv *data)
  435. {
  436. struct device *dev = data->context->dev;
  437. u32 calculated_crc;
  438. size_t length;
  439. /* Calculate the 32-bit CRC of the header header contents up to but
  440. * not including the checksum. Note that the Linux crc32_le function
  441. * does not perform an expected final XOR.
  442. */
  443. length = data->offset - sizeof(data->header_crc);
  444. calculated_crc = crc32_le(~0, data->fw->data, length) ^ ~0;
  445. if (calculated_crc != data->header_crc) {
  446. dev_dbg(dev, "Invalid CRC in firmware header. Got 0x%08x but expected 0x%08x\n",
  447. calculated_crc, data->header_crc);
  448. return -EBADMSG;
  449. }
  450. return 0;
  451. }
  452. /**
  453. * pldmfw_free_priv - Free memory allocated while parsing the PLDM image
  454. * @data: pointer to the PLDM data structure
  455. *
  456. * Loops through and clears all allocated memory associated with each
  457. * allocated descriptor, record, and component.
  458. */
  459. static void pldmfw_free_priv(struct pldmfw_priv *data)
  460. {
  461. struct pldmfw_component *component, *c_safe;
  462. struct pldmfw_record *record, *r_safe;
  463. struct pldmfw_desc_tlv *desc, *d_safe;
  464. list_for_each_entry_safe(component, c_safe, &data->components, entry) {
  465. list_del(&component->entry);
  466. kfree(component);
  467. }
  468. list_for_each_entry_safe(record, r_safe, &data->records, entry) {
  469. list_for_each_entry_safe(desc, d_safe, &record->descs, entry) {
  470. list_del(&desc->entry);
  471. kfree(desc);
  472. }
  473. if (record->component_bitmap) {
  474. bitmap_free(record->component_bitmap);
  475. record->component_bitmap = NULL;
  476. }
  477. list_del(&record->entry);
  478. kfree(record);
  479. }
  480. }
  481. /**
  482. * pldm_parse_image - parse and extract details from PLDM image
  483. * @data: pointer to private data
  484. *
  485. * Verify that the firmware file contains valid data for a PLDM firmware
  486. * file. Extract useful pointers and data from the firmware file and store
  487. * them in the data structure.
  488. *
  489. * The PLDM firmware file format is defined in DMTF DSP0267 1.0.0. Care
  490. * should be taken to use get_unaligned_le* when accessing data from the
  491. * pointers in data.
  492. *
  493. * Returns: zero on success, or a negative error code on failure.
  494. */
  495. static int pldm_parse_image(struct pldmfw_priv *data)
  496. {
  497. int err;
  498. if (WARN_ON(!(data->context->dev && data->fw->data && data->fw->size)))
  499. return -EINVAL;
  500. err = pldm_parse_header(data);
  501. if (err)
  502. return err;
  503. err = pldm_parse_records(data);
  504. if (err)
  505. return err;
  506. err = pldm_parse_components(data);
  507. if (err)
  508. return err;
  509. return pldm_verify_header_crc(data);
  510. }
  511. /* these are u32 so that we can store PCI_ANY_ID */
  512. struct pldm_pci_record_id {
  513. int vendor;
  514. int device;
  515. int subsystem_vendor;
  516. int subsystem_device;
  517. };
  518. /**
  519. * pldmfw_op_pci_match_record - Check if a PCI device matches the record
  520. * @context: PLDM fw update structure
  521. * @record: list of records extracted from the PLDM image
  522. *
  523. * Determine of the PCI device associated with this device matches the record
  524. * data provided.
  525. *
  526. * Searches the descriptor TLVs and extracts the relevant descriptor data into
  527. * a pldm_pci_record_id. This is then compared against the PCI device ID
  528. * information.
  529. *
  530. * Returns: true if the device matches the record, false otherwise.
  531. */
  532. bool pldmfw_op_pci_match_record(struct pldmfw *context, struct pldmfw_record *record)
  533. {
  534. struct pci_dev *pdev = to_pci_dev(context->dev);
  535. struct pldm_pci_record_id id = {
  536. .vendor = PCI_ANY_ID,
  537. .device = PCI_ANY_ID,
  538. .subsystem_vendor = PCI_ANY_ID,
  539. .subsystem_device = PCI_ANY_ID,
  540. };
  541. struct pldmfw_desc_tlv *desc;
  542. list_for_each_entry(desc, &record->descs, entry) {
  543. u16 value;
  544. int *ptr;
  545. switch (desc->type) {
  546. case PLDM_DESC_ID_PCI_VENDOR_ID:
  547. ptr = &id.vendor;
  548. break;
  549. case PLDM_DESC_ID_PCI_DEVICE_ID:
  550. ptr = &id.device;
  551. break;
  552. case PLDM_DESC_ID_PCI_SUBVENDOR_ID:
  553. ptr = &id.subsystem_vendor;
  554. break;
  555. case PLDM_DESC_ID_PCI_SUBDEV_ID:
  556. ptr = &id.subsystem_device;
  557. break;
  558. default:
  559. /* Skip unrelated TLVs */
  560. continue;
  561. }
  562. value = get_unaligned_le16(desc->data);
  563. /* A value of zero for one of the descriptors is sometimes
  564. * used when the record should ignore this field when matching
  565. * device. For example if the record applies to any subsystem
  566. * device or vendor.
  567. */
  568. if (value)
  569. *ptr = (int)value;
  570. else
  571. *ptr = PCI_ANY_ID;
  572. }
  573. if ((id.vendor == PCI_ANY_ID || id.vendor == pdev->vendor) &&
  574. (id.device == PCI_ANY_ID || id.device == pdev->device) &&
  575. (id.subsystem_vendor == PCI_ANY_ID || id.subsystem_vendor == pdev->subsystem_vendor) &&
  576. (id.subsystem_device == PCI_ANY_ID || id.subsystem_device == pdev->subsystem_device))
  577. return true;
  578. else
  579. return false;
  580. }
  581. EXPORT_SYMBOL(pldmfw_op_pci_match_record);
  582. /**
  583. * pldm_find_matching_record - Find the first matching PLDM record
  584. * @data: pointer to private data
  585. *
  586. * Search through PLDM records and find the first matching entry. It is
  587. * expected that only one entry matches.
  588. *
  589. * Store a pointer to the matching record, if found.
  590. *
  591. * Returns: zero on success, or -ENOENT if no matching record is found.
  592. */
  593. static int pldm_find_matching_record(struct pldmfw_priv *data)
  594. {
  595. struct pldmfw_record *record;
  596. list_for_each_entry(record, &data->records, entry) {
  597. if (data->context->ops->match_record(data->context, record)) {
  598. data->matching_record = record;
  599. return 0;
  600. }
  601. }
  602. return -ENOENT;
  603. }
  604. /**
  605. * pldm_send_package_data - Send firmware the package data for the record
  606. * @data: pointer to private data
  607. *
  608. * Send the package data associated with the matching record to the firmware,
  609. * using the send_pkg_data operation.
  610. *
  611. * Returns: zero on success, or a negative error code on failure.
  612. */
  613. static int
  614. pldm_send_package_data(struct pldmfw_priv *data)
  615. {
  616. struct pldmfw_record *record = data->matching_record;
  617. const struct pldmfw_ops *ops = data->context->ops;
  618. if (!ops->send_package_data)
  619. return 0;
  620. return ops->send_package_data(data->context, record->package_data,
  621. record->package_data_len);
  622. }
  623. /**
  624. * pldm_send_component_tables - Send component table information to firmware
  625. * @data: pointer to private data
  626. *
  627. * Loop over each component, sending the applicable components to the firmware
  628. * via the send_component_table operation.
  629. *
  630. * Returns: zero on success, or a negative error code on failure.
  631. */
  632. static int
  633. pldm_send_component_tables(struct pldmfw_priv *data)
  634. {
  635. unsigned long *bitmap = data->matching_record->component_bitmap;
  636. struct pldmfw_component *component;
  637. int err;
  638. list_for_each_entry(component, &data->components, entry) {
  639. u8 index = component->index, transfer_flag = 0;
  640. /* Skip components which are not intended for this device */
  641. if (!test_bit(index, bitmap))
  642. continue;
  643. if (!data->context->ops->send_component_table)
  644. continue;
  645. /* determine whether this is the start, middle, end, or both
  646. * the start and end of the component tables
  647. */
  648. if (index == find_first_bit(bitmap, data->component_bitmap_len))
  649. transfer_flag |= PLDM_TRANSFER_FLAG_START;
  650. if (index == find_last_bit(bitmap, data->component_bitmap_len))
  651. transfer_flag |= PLDM_TRANSFER_FLAG_END;
  652. if (!transfer_flag)
  653. transfer_flag = PLDM_TRANSFER_FLAG_MIDDLE;
  654. err = data->context->ops->send_component_table(data->context,
  655. component,
  656. transfer_flag);
  657. if (err)
  658. return err;
  659. }
  660. return 0;
  661. }
  662. /**
  663. * pldm_flash_components - Program each component to device flash
  664. * @data: pointer to private data
  665. *
  666. * Loop through each component that is active for the matching device record,
  667. * and send it to the device driver for flashing.
  668. *
  669. * Returns: zero on success, or a negative error code on failure.
  670. */
  671. static int pldm_flash_components(struct pldmfw_priv *data)
  672. {
  673. unsigned long *bitmap = data->matching_record->component_bitmap;
  674. struct pldmfw_component *component;
  675. int err;
  676. list_for_each_entry(component, &data->components, entry) {
  677. u8 index = component->index;
  678. /* Skip components which are not intended for this device */
  679. if (!test_bit(index, bitmap))
  680. continue;
  681. err = data->context->ops->flash_component(data->context, component);
  682. if (err)
  683. return err;
  684. }
  685. return 0;
  686. }
  687. /**
  688. * pldm_finalize_update - Finalize the device flash update
  689. * @data: pointer to private data
  690. *
  691. * Tell the device driver to perform any remaining logic to complete the
  692. * device update.
  693. *
  694. * Returns: zero on success, or a PLFM_FWU error indicating the reason for
  695. * failure.
  696. */
  697. static int pldm_finalize_update(struct pldmfw_priv *data)
  698. {
  699. if (data->context->ops->finalize_update)
  700. return data->context->ops->finalize_update(data->context);
  701. return 0;
  702. }
  703. /**
  704. * pldmfw_flash_image - Write a PLDM-formatted firmware image to the device
  705. * @context: ops and data for firmware update
  706. * @fw: firmware object pointing to the relevant firmware file to program
  707. *
  708. * Parse the data for a given firmware file, verifying that it is a valid PLDM
  709. * formatted image that matches this device.
  710. *
  711. * Extract the device record Package Data and Component Tables and send them
  712. * to the device firmware. Extract and write the flash data for each of the
  713. * components indicated in the firmware file.
  714. *
  715. * Returns: zero on success, or a negative error code on failure.
  716. */
  717. int pldmfw_flash_image(struct pldmfw *context, const struct firmware *fw)
  718. {
  719. struct pldmfw_priv *data;
  720. int err;
  721. data = kzalloc_obj(*data);
  722. if (!data)
  723. return -ENOMEM;
  724. INIT_LIST_HEAD(&data->records);
  725. INIT_LIST_HEAD(&data->components);
  726. data->fw = fw;
  727. data->context = context;
  728. err = pldm_parse_image(data);
  729. if (err)
  730. goto out_release_data;
  731. err = pldm_find_matching_record(data);
  732. if (err)
  733. goto out_release_data;
  734. err = pldm_send_package_data(data);
  735. if (err)
  736. goto out_release_data;
  737. err = pldm_send_component_tables(data);
  738. if (err)
  739. goto out_release_data;
  740. err = pldm_flash_components(data);
  741. if (err)
  742. goto out_release_data;
  743. err = pldm_finalize_update(data);
  744. out_release_data:
  745. pldmfw_free_priv(data);
  746. kfree(data);
  747. return err;
  748. }
  749. EXPORT_SYMBOL(pldmfw_flash_image);
  750. MODULE_AUTHOR("Jacob Keller <jacob.e.keller@intel.com>");
  751. MODULE_DESCRIPTION("PLDM firmware flash update library");