Lines Matching +full:record +full:- +full:size

1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2018-2019, Intel Corporation. */
53 * pldm_check_fw_space - Verify that the firmware image has space left
61 * Returns: zero on success, or -EFAULT if the image does not have enough
68 struct device *dev = data->context->dev; in pldm_check_fw_space()
70 if (data->fw->size < expected_size) { in pldm_check_fw_space()
71 dev_dbg(dev, "Firmware file size smaller than expected. Got %zu bytes, needed %zu bytes\n", in pldm_check_fw_space()
72 data->fw->size, expected_size); in pldm_check_fw_space()
73 return -EFAULT; in pldm_check_fw_space()
80 * pldm_move_fw_offset - Move the current firmware offset forward
87 * Returns: zero on success, or -EFAULT if the image is too small to fit the
95 err = pldm_check_fw_space(data, data->offset, bytes_to_move); in pldm_move_fw_offset()
99 data->offset += bytes_to_move; in pldm_move_fw_offset()
105 * pldm_parse_header - Validate and extract details about the PLDM header
109 * firmware record.
118 * * Extract the size of the component bitmap length
119 * * Extract a pointer to the start of the record area
126 struct device *dev = data->context->dev; in pldm_parse_header()
135 header = (const struct __pldm_header *)data->fw->data; in pldm_parse_header()
136 data->header = header; in pldm_parse_header()
138 if (!uuid_equal(&header->id, &pldm_firmware_header_id)) { in pldm_parse_header()
140 &pldm_firmware_header_id, &header->id); in pldm_parse_header()
141 return -EINVAL; in pldm_parse_header()
144 if (header->revision != PACKAGE_HEADER_FORMAT_REVISION) { in pldm_parse_header()
146 PACKAGE_HEADER_FORMAT_REVISION, header->revision); in pldm_parse_header()
147 return -EOPNOTSUPP; in pldm_parse_header()
150 data->total_header_size = get_unaligned_le16(&header->size); in pldm_parse_header()
151 header_size = data->total_header_size - sizeof(*header); in pldm_parse_header()
153 err = pldm_check_fw_space(data, data->offset, header_size); in pldm_parse_header()
157 data->component_bitmap_len = in pldm_parse_header()
158 get_unaligned_le16(&header->component_bitmap_len); in pldm_parse_header()
160 if (data->component_bitmap_len % 8 != 0) { in pldm_parse_header()
162 data->component_bitmap_len); in pldm_parse_header()
163 return -EINVAL; in pldm_parse_header()
166 data->bitmap_size = data->component_bitmap_len / 8; in pldm_parse_header()
168 err = pldm_move_fw_offset(data, header->version_len); in pldm_parse_header()
172 /* extract a pointer to the record area, which just follows the main in pldm_parse_header()
175 record_area = (const struct __pldmfw_record_area *)(data->fw->data + in pldm_parse_header()
176 data->offset); in pldm_parse_header()
182 data->record_count = record_area->record_count; in pldm_parse_header()
183 data->record_start = record_area->records; in pldm_parse_header()
189 * pldm_check_desc_tlv_len - Check that the length matches expectation
192 * @size: the length from the descriptor header
199 * Returns: zero on success, or -EINVAL if the specified size of a standard
203 pldm_check_desc_tlv_len(struct pldmfw_priv *data, u16 type, u16 size) in pldm_check_desc_tlv_len() argument
205 struct device *dev = data->context->dev; in pldm_check_desc_tlv_len()
238 if (size != expected_size) { in pldm_check_desc_tlv_len()
240 type, size, expected_size); in pldm_check_desc_tlv_len()
241 return -EINVAL; in pldm_check_desc_tlv_len()
248 * pldm_parse_desc_tlvs - Check and skip past a number of TLVs
250 * @record: pointer to the record this TLV belongs too
259 pldm_parse_desc_tlvs(struct pldmfw_priv *data, struct pldmfw_record *record, u8 desc_count) in pldm_parse_desc_tlvs() argument
265 desc_start = data->fw->data + data->offset; in pldm_parse_desc_tlvs()
270 u16 type, size; in pldm_parse_desc_tlvs() local
276 type = get_unaligned_le16(&__desc->type); in pldm_parse_desc_tlvs()
279 size = get_unaligned_le16(&__desc->size); in pldm_parse_desc_tlvs()
281 err = pldm_check_desc_tlv_len(data, type, size); in pldm_parse_desc_tlvs()
286 err = pldm_move_fw_offset(data, size); in pldm_parse_desc_tlvs()
292 return -ENOMEM; in pldm_parse_desc_tlvs()
294 desc->type = type; in pldm_parse_desc_tlvs()
295 desc->size = size; in pldm_parse_desc_tlvs()
296 desc->data = __desc->data; in pldm_parse_desc_tlvs()
298 list_add_tail(&desc->entry, &record->descs); in pldm_parse_desc_tlvs()
305 * pldm_parse_one_record - Verify size of one PLDM record
307 * @__record: pointer to the record to check
309 * This function checks that the record size does not exceed either the size
312 * It also verifies that the recorded length of the start of the record
313 * matches the size calculated by adding the static structure length, the
323 struct pldmfw_record *record; in pldm_parse_one_record() local
330 /* Make a copy and insert it into the record list */ in pldm_parse_one_record()
331 record = kzalloc(sizeof(*record), GFP_KERNEL); in pldm_parse_one_record()
332 if (!record) in pldm_parse_one_record()
333 return -ENOMEM; in pldm_parse_one_record()
335 INIT_LIST_HEAD(&record->descs); in pldm_parse_one_record()
336 list_add_tail(&record->entry, &data->records); in pldm_parse_one_record()
343 record_len = get_unaligned_le16(&__record->record_len); in pldm_parse_one_record()
344 record->package_data_len = get_unaligned_le16(&__record->package_data_len); in pldm_parse_one_record()
345 record->version_len = __record->version_len; in pldm_parse_one_record()
346 record->version_type = __record->version_type; in pldm_parse_one_record()
348 bitmap_ptr = data->fw->data + data->offset; in pldm_parse_one_record()
351 err = pldm_move_fw_offset(data, data->bitmap_size); in pldm_parse_one_record()
355 record->component_bitmap_len = data->component_bitmap_len; in pldm_parse_one_record()
356 record->component_bitmap = bitmap_zalloc(record->component_bitmap_len, in pldm_parse_one_record()
358 if (!record->component_bitmap) in pldm_parse_one_record()
359 return -ENOMEM; in pldm_parse_one_record()
361 for (i = 0; i < data->bitmap_size; i++) in pldm_parse_one_record()
362 bitmap_set_value8(record->component_bitmap, bitmap_ptr[i], i * 8); in pldm_parse_one_record()
364 record->version_string = data->fw->data + data->offset; in pldm_parse_one_record()
366 err = pldm_move_fw_offset(data, record->version_len); in pldm_parse_one_record()
371 err = pldm_parse_desc_tlvs(data, record, __record->descriptor_count); in pldm_parse_one_record()
375 record->package_data = data->fw->data + data->offset; in pldm_parse_one_record()
377 err = pldm_move_fw_offset(data, record->package_data_len); in pldm_parse_one_record()
381 measured_length = data->offset - ((const u8 *)__record - data->fw->data); in pldm_parse_one_record()
383 …dev_dbg(data->context->dev, "Unexpected record length. Measured record length is %zu bytes, expect… in pldm_parse_one_record()
385 return -EFAULT; in pldm_parse_one_record()
392 * pldm_parse_records - Locate the start of the component area
395 * Extract the record count, and loop through each record, searching for the
403 const struct __pldmfw_record_info *record; in pldm_parse_records() local
407 pldm_for_each_record(i, record, data->record_start, data->record_count) { in pldm_parse_records()
408 err = pldm_parse_one_record(data, record); in pldm_parse_records()
414 * PLDM device record data. in pldm_parse_records()
416 component_area = (const struct __pldmfw_component_area *)(data->fw->data + data->offset); in pldm_parse_records()
422 data->component_count = in pldm_parse_records()
423 get_unaligned_le16(&component_area->component_image_count); in pldm_parse_records()
424 data->component_start = component_area->components; in pldm_parse_records()
430 * pldm_parse_components - Locate the CRC header checksum
444 struct device *dev = data->context->dev; in pldm_parse_components()
449 pldm_for_each_component(i, __component, data->component_start, data->component_count) { in pldm_parse_components()
451 u32 offset, size; in pldm_parse_components() local
457 err = pldm_move_fw_offset(data, __component->version_len); in pldm_parse_components()
461 offset = get_unaligned_le32(&__component->location_offset); in pldm_parse_components()
462 size = get_unaligned_le32(&__component->size); in pldm_parse_components()
464 err = pldm_check_fw_space(data, offset, size); in pldm_parse_components()
470 return -ENOMEM; in pldm_parse_components()
472 component->index = i; in pldm_parse_components()
473 component->classification = get_unaligned_le16(&__component->classification); in pldm_parse_components()
474 component->identifier = get_unaligned_le16(&__component->identifier); in pldm_parse_components()
475 component->comparison_stamp = get_unaligned_le32(&__component->comparison_stamp); in pldm_parse_components()
476 component->options = get_unaligned_le16(&__component->options); in pldm_parse_components()
477 component->activation_method = get_unaligned_le16(&__component->activation_method); in pldm_parse_components()
478 component->version_type = __component->version_type; in pldm_parse_components()
479 component->version_len = __component->version_len; in pldm_parse_components()
480 component->version_string = __component->version_string; in pldm_parse_components()
481 component->component_data = data->fw->data + offset; in pldm_parse_components()
482 component->component_size = size; in pldm_parse_components()
484 list_add_tail(&component->entry, &data->components); in pldm_parse_components()
487 header_crc_ptr = data->fw->data + data->offset; in pldm_parse_components()
489 err = pldm_move_fw_offset(data, sizeof(data->header_crc)); in pldm_parse_components()
494 if (data->offset != data->total_header_size) { in pldm_parse_components()
495 dev_dbg(dev, "Invalid firmware header size. Expected %u but got %zu\n", in pldm_parse_components()
496 data->total_header_size, data->offset); in pldm_parse_components()
497 return -EFAULT; in pldm_parse_components()
500 data->header_crc = get_unaligned_le32(header_crc_ptr); in pldm_parse_components()
506 * pldm_verify_header_crc - Verify that the CRC in the header matches
509 * Calculates the 32-bit CRC using the standard IEEE 802.3 CRC polynomial and
512 * Returns: zero on success if the CRC matches, or -EBADMSG on an invalid CRC.
516 struct device *dev = data->context->dev; in pldm_verify_header_crc()
520 /* Calculate the 32-bit CRC of the header header contents up to but in pldm_verify_header_crc()
524 length = data->offset - sizeof(data->header_crc); in pldm_verify_header_crc()
525 calculated_crc = crc32_le(~0, data->fw->data, length) ^ ~0; in pldm_verify_header_crc()
527 if (calculated_crc != data->header_crc) { in pldm_verify_header_crc()
529 calculated_crc, data->header_crc); in pldm_verify_header_crc()
530 return -EBADMSG; in pldm_verify_header_crc()
537 * pldmfw_free_priv - Free memory allocated while parsing the PLDM image
541 * allocated descriptor, record, and component.
546 struct pldmfw_record *record, *r_safe; in pldmfw_free_priv() local
549 list_for_each_entry_safe(component, c_safe, &data->components, entry) { in pldmfw_free_priv()
550 list_del(&component->entry); in pldmfw_free_priv()
554 list_for_each_entry_safe(record, r_safe, &data->records, entry) { in pldmfw_free_priv()
555 list_for_each_entry_safe(desc, d_safe, &record->descs, entry) { in pldmfw_free_priv()
556 list_del(&desc->entry); in pldmfw_free_priv()
560 if (record->component_bitmap) { in pldmfw_free_priv()
561 bitmap_free(record->component_bitmap); in pldmfw_free_priv()
562 record->component_bitmap = NULL; in pldmfw_free_priv()
565 list_del(&record->entry); in pldmfw_free_priv()
566 kfree(record); in pldmfw_free_priv()
571 * pldm_parse_image - parse and extract details from PLDM image
588 if (WARN_ON(!(data->context->dev && data->fw->data && data->fw->size))) in pldm_parse_image()
589 return -EINVAL; in pldm_parse_image()
615 * pldmfw_op_pci_match_record - Check if a PCI device matches the record
617 * @record: list of records extracted from the PLDM image
619 * Determine of the PCI device associated with this device matches the record
626 * Returns: true if the device matches the record, false otherwise.
628 bool pldmfw_op_pci_match_record(struct pldmfw *context, struct pldmfw_record *record) in pldmfw_op_pci_match_record() argument
630 struct pci_dev *pdev = to_pci_dev(context->dev); in pldmfw_op_pci_match_record()
639 list_for_each_entry(desc, &record->descs, entry) { in pldmfw_op_pci_match_record()
643 switch (desc->type) { in pldmfw_op_pci_match_record()
661 value = get_unaligned_le16(desc->data); in pldmfw_op_pci_match_record()
663 * used when the record should ignore this field when matching in pldmfw_op_pci_match_record()
664 * device. For example if the record applies to any subsystem in pldmfw_op_pci_match_record()
673 if ((id.vendor == PCI_ANY_ID || id.vendor == pdev->vendor) && in pldmfw_op_pci_match_record()
674 (id.device == PCI_ANY_ID || id.device == pdev->device) && in pldmfw_op_pci_match_record()
675 (id.subsystem_vendor == PCI_ANY_ID || id.subsystem_vendor == pdev->subsystem_vendor) && in pldmfw_op_pci_match_record()
676 (id.subsystem_device == PCI_ANY_ID || id.subsystem_device == pdev->subsystem_device)) in pldmfw_op_pci_match_record()
684 * pldm_find_matching_record - Find the first matching PLDM record
690 * Store a pointer to the matching record, if found.
692 * Returns: zero on success, or -ENOENT if no matching record is found.
696 struct pldmfw_record *record; in pldm_find_matching_record() local
698 list_for_each_entry(record, &data->records, entry) { in pldm_find_matching_record()
699 if (data->context->ops->match_record(data->context, record)) { in pldm_find_matching_record()
700 data->matching_record = record; in pldm_find_matching_record()
705 return -ENOENT; in pldm_find_matching_record()
709 * pldm_send_package_data - Send firmware the package data for the record
712 * Send the package data associated with the matching record to the firmware,
720 struct pldmfw_record *record = data->matching_record; in pldm_send_package_data() local
721 const struct pldmfw_ops *ops = data->context->ops; in pldm_send_package_data()
723 return ops->send_package_data(data->context, record->package_data, in pldm_send_package_data()
724 record->package_data_len); in pldm_send_package_data()
728 * pldm_send_component_tables - Send component table information to firmware
739 unsigned long *bitmap = data->matching_record->component_bitmap; in pldm_send_component_tables()
743 list_for_each_entry(component, &data->components, entry) { in pldm_send_component_tables()
744 u8 index = component->index, transfer_flag = 0; in pldm_send_component_tables()
753 if (index == find_first_bit(bitmap, data->component_bitmap_len)) in pldm_send_component_tables()
755 if (index == find_last_bit(bitmap, data->component_bitmap_len)) in pldm_send_component_tables()
760 err = data->context->ops->send_component_table(data->context, in pldm_send_component_tables()
771 * pldm_flash_components - Program each component to device flash
774 * Loop through each component that is active for the matching device record,
781 unsigned long *bitmap = data->matching_record->component_bitmap; in pldm_flash_components()
785 list_for_each_entry(component, &data->components, entry) { in pldm_flash_components()
786 u8 index = component->index; in pldm_flash_components()
792 err = data->context->ops->flash_component(data->context, component); in pldm_flash_components()
801 * pldm_finalize_update - Finalize the device flash update
812 if (data->context->ops->finalize_update) in pldm_finalize_update()
813 return data->context->ops->finalize_update(data->context); in pldm_finalize_update()
819 * pldmfw_flash_image - Write a PLDM-formatted firmware image to the device
826 * Extract the device record Package Data and Component Tables and send them
839 return -ENOMEM; in pldmfw_flash_image()
841 INIT_LIST_HEAD(&data->records); in pldmfw_flash_image()
842 INIT_LIST_HEAD(&data->components); in pldmfw_flash_image()
844 data->fw = fw; in pldmfw_flash_image()
845 data->context = context; in pldmfw_flash_image()