blob: 59d1f9d20d76276af904e46fc3b51ecc05dca8da [file] [log] [blame]
Patrick Williams70a47ba2021-09-02 09:53:31 -05001#include "firmware_update.h"
2#include <endian.h>
3#include <string.h>
4
5/** @brief Check whether string type value is valid
6 *
7 * @return true if string type value is valid, false if not
8 */
9static bool is_string_type_valid(uint8_t string_type)
10{
11 switch (string_type) {
12 case PLDM_STR_TYPE_UNKNOWN:
13 return false;
14 case PLDM_STR_TYPE_ASCII:
15 case PLDM_STR_TYPE_UTF_8:
16 case PLDM_STR_TYPE_UTF_16:
17 case PLDM_STR_TYPE_UTF_16LE:
18 case PLDM_STR_TYPE_UTF_16BE:
19 return true;
20 default:
21 return false;
22 }
23}
24
25/** @brief Return the length of the descriptor type described in firmware update
26 * specification
27 *
28 * @return length of the descriptor type if descriptor type is valid else
29 * return 0
30 */
31static uint16_t get_descriptor_type_length(uint16_t descriptor_type)
32{
33 switch (descriptor_type) {
34
35 case PLDM_FWUP_PCI_VENDOR_ID:
36 return PLDM_FWUP_PCI_VENDOR_ID_LENGTH;
37 case PLDM_FWUP_IANA_ENTERPRISE_ID:
38 return PLDM_FWUP_IANA_ENTERPRISE_ID_LENGTH;
39 case PLDM_FWUP_UUID:
40 return PLDM_FWUP_UUID_LENGTH;
41 case PLDM_FWUP_PNP_VENDOR_ID:
42 return PLDM_FWUP_PNP_VENDOR_ID_LENGTH;
43 case PLDM_FWUP_ACPI_VENDOR_ID:
44 return PLDM_FWUP_ACPI_VENDOR_ID_LENGTH;
45 case PLDM_FWUP_IEEE_ASSIGNED_COMPANY_ID:
46 return PLDM_FWUP_IEEE_ASSIGNED_COMPANY_ID_LENGTH;
47 case PLDM_FWUP_SCSI_VENDOR_ID:
48 return PLDM_FWUP_SCSI_VENDOR_ID_LENGTH;
49 case PLDM_FWUP_PCI_DEVICE_ID:
50 return PLDM_FWUP_PCI_DEVICE_ID_LENGTH;
51 case PLDM_FWUP_PCI_SUBSYSTEM_VENDOR_ID:
52 return PLDM_FWUP_PCI_SUBSYSTEM_VENDOR_ID_LENGTH;
53 case PLDM_FWUP_PCI_SUBSYSTEM_ID:
54 return PLDM_FWUP_PCI_SUBSYSTEM_ID_LENGTH;
55 case PLDM_FWUP_PCI_REVISION_ID:
56 return PLDM_FWUP_PCI_REVISION_ID_LENGTH;
57 case PLDM_FWUP_PNP_PRODUCT_IDENTIFIER:
58 return PLDM_FWUP_PNP_PRODUCT_IDENTIFIER_LENGTH;
59 case PLDM_FWUP_ACPI_PRODUCT_IDENTIFIER:
60 return PLDM_FWUP_ACPI_PRODUCT_IDENTIFIER_LENGTH;
61 case PLDM_FWUP_ASCII_MODEL_NUMBER_LONG_STRING:
62 return PLDM_FWUP_ASCII_MODEL_NUMBER_LONG_STRING_LENGTH;
63 case PLDM_FWUP_ASCII_MODEL_NUMBER_SHORT_STRING:
64 return PLDM_FWUP_ASCII_MODEL_NUMBER_SHORT_STRING_LENGTH;
65 case PLDM_FWUP_SCSI_PRODUCT_ID:
66 return PLDM_FWUP_SCSI_PRODUCT_ID_LENGTH;
67 case PLDM_FWUP_UBM_CONTROLLER_DEVICE_CODE:
68 return PLDM_FWUP_UBM_CONTROLLER_DEVICE_CODE_LENGTH;
69 default:
70 return 0;
71 }
72}
73
74/** @brief Check whether ComponentResponse is valid
75 *
76 * @return true if ComponentResponse is valid, false if not
77 */
78static bool is_comp_resp_valid(uint8_t comp_resp)
79{
80 switch (comp_resp) {
81 case PLDM_CR_COMP_CAN_BE_UPDATED:
82 case PLDM_CR_COMP_MAY_BE_UPDATEABLE:
83 return true;
84
85 default:
86 return false;
87 }
88}
89
90/** @brief Check whether ComponentResponseCode is valid
91 *
92 * @return true if ComponentResponseCode is valid, false if not
93 */
94static bool is_comp_resp_code_valid(uint8_t comp_resp_code)
95{
96 switch (comp_resp_code) {
97 case PLDM_CRC_COMP_CAN_BE_UPDATED:
98 case PLDM_CRC_COMP_COMPARISON_STAMP_IDENTICAL:
99 case PLDM_CRC_COMP_COMPARISON_STAMP_LOWER:
100 case PLDM_CRC_INVALID_COMP_COMPARISON_STAMP:
101 case PLDM_CRC_COMP_CONFLICT:
102 case PLDM_CRC_COMP_PREREQUISITES_NOT_MET:
103 case PLDM_CRC_COMP_NOT_SUPPORTED:
104 case PLDM_CRC_COMP_SECURITY_RESTRICTIONS:
105 case PLDM_CRC_INCOMPLETE_COMP_IMAGE_SET:
106 case PLDM_CRC_ACTIVE_IMAGE_NOT_UPDATEABLE_SUBSEQUENTLY:
107 case PLDM_CRC_COMP_VER_STR_IDENTICAL:
108 case PLDM_CRC_COMP_VER_STR_LOWER:
109 return true;
110
111 default:
112 if (comp_resp_code >=
113 PLDM_CRC_VENDOR_COMP_RESP_CODE_RANGE_MIN &&
114 comp_resp_code <=
115 PLDM_CRC_VENDOR_COMP_RESP_CODE_RANGE_MAX) {
116 return true;
117 }
118 return false;
119 }
120}
121
122/** @brief Check whether ComponentCompatibilityResponse is valid
123 *
124 * @return true if ComponentCompatibilityResponse is valid, false if not
125 */
126static bool is_comp_compatibility_resp_valid(uint8_t comp_compatibility_resp)
127{
128 switch (comp_compatibility_resp) {
129 case PLDM_CCR_COMP_CAN_BE_UPDATED:
130 case PLDM_CCR_COMP_CANNOT_BE_UPDATED:
131 return true;
132
133 default:
134 return false;
135 }
136}
137
138/** @brief Check whether ComponentCompatibilityResponse Code is valid
139 *
140 * @return true if ComponentCompatibilityResponse Code is valid, false if not
141 */
142static bool
143is_comp_compatibility_resp_code_valid(uint8_t comp_compatibility_resp_code)
144{
145 switch (comp_compatibility_resp_code) {
146 case PLDM_CCRC_NO_RESPONSE_CODE:
147 case PLDM_CCRC_COMP_COMPARISON_STAMP_IDENTICAL:
148 case PLDM_CCRC_COMP_COMPARISON_STAMP_LOWER:
149 case PLDM_CCRC_INVALID_COMP_COMPARISON_STAMP:
150 case PLDM_CCRC_COMP_CONFLICT:
151 case PLDM_CCRC_COMP_PREREQUISITES_NOT_MET:
152 case PLDM_CCRC_COMP_NOT_SUPPORTED:
153 case PLDM_CCRC_COMP_SECURITY_RESTRICTIONS:
154 case PLDM_CRC_INCOMPLETE_COMP_IMAGE_SET:
155 case PLDM_CCRC_COMP_INFO_NO_MATCH:
156 case PLDM_CCRC_COMP_VER_STR_IDENTICAL:
157 case PLDM_CCRC_COMP_VER_STR_LOWER:
158 return true;
159
160 default:
161 if (comp_compatibility_resp_code >=
162 PLDM_CCRC_VENDOR_COMP_RESP_CODE_RANGE_MIN &&
163 comp_compatibility_resp_code <=
164 PLDM_CCRC_VENDOR_COMP_RESP_CODE_RANGE_MAX) {
165 return true;
166 }
167 return false;
168 }
169}
170
171/** @brief Check whether SelfContainedActivationRequest is valid
172 *
173 * @return true if SelfContainedActivationRequest is valid, false if not
174 */
175static bool
176is_self_contained_activation_req_valid(bool8_t self_contained_activation_req)
177{
178 switch (self_contained_activation_req) {
179 case PLDM_NOT_ACTIVATE_SELF_CONTAINED_COMPONENTS:
180 case PLDM_ACTIVATE_SELF_CONTAINED_COMPONENTS:
181 return true;
182
183 default:
184 return false;
185 }
186}
187
188/** @brief Check if current or previous status in GetStatus command response is
189 * valid
190 *
191 * @param[in] state - current or previous different state machine state of
192 * the FD
193 * @return true if state is valid, false if not
194 */
195static bool is_state_valid(uint8_t state)
196{
197 switch (state) {
198 case PLDM_FD_STATE_IDLE:
199 case PLDM_FD_STATE_LEARN_COMPONENTS:
200 case PLDM_FD_STATE_READY_XFER:
201 case PLDM_FD_STATE_DOWNLOAD:
202 case PLDM_FD_STATE_VERIFY:
203 case PLDM_FD_STATE_APPLY:
204 case PLDM_FD_STATE_ACTIVATE:
205 return true;
206
207 default:
208 return false;
209 }
210}
211
212/** @brief Check if aux state in GetStatus command response is valid
213 *
214 * @param[in] aux_state - provides additional information to the UA to describe
215 * the current operation state of the FD/FDP
216 *
217 * @return true if aux state is valid, false if not
218 */
219static bool is_aux_state_valid(uint8_t aux_state)
220{
221 switch (aux_state) {
222 case PLDM_FD_OPERATION_IN_PROGRESS:
223 case PLDM_FD_OPERATION_SUCCESSFUL:
224 case PLDM_FD_OPERATION_FAILED:
225 case PLDM_FD_IDLE_LEARN_COMPONENTS_READ_XFER:
226 return true;
227
228 default:
229 return false;
230 }
231}
232
233/** @brief Check if aux state status in GetStatus command response is valid
234 *
235 * @param[in] aux_state_status - aux state status
236 *
237 * @return true if aux state status is valid, false if not
238 */
239static bool is_aux_state_status_valid(uint8_t aux_state_status)
240{
241 if (aux_state_status == PLDM_FD_AUX_STATE_IN_PROGRESS_OR_SUCCESS ||
242 aux_state_status == PLDM_FD_TIMEOUT ||
243 aux_state_status == PLDM_FD_GENERIC_ERROR) {
244 return true;
245 } else if (aux_state_status >=
246 PLDM_FD_VENDOR_DEFINED_STATUS_CODE_START &&
247 aux_state_status <= PLDM_FD_VENDOR_DEFINED_STATUS_CODE_END) {
248 return true;
249 }
250
251 return false;
252}
253
254/** @brief Check if reason code in GetStatus command response is valid
255 *
256 * @param[in] reason_code - provides the reason for why the current state
257 * entered the IDLE state
258 *
259 * @return true if reason code is valid, false if not
260 */
261static bool is_reason_code_valid(uint8_t reason_code)
262{
263
264 switch (reason_code) {
265 case PLDM_FD_INITIALIZATION:
266 case PLDM_FD_ACTIVATE_FW:
267 case PLDM_FD_CANCEL_UPDATE:
268 case PLDM_FD_TIMEOUT_LEARN_COMPONENT:
269 case PLDM_FD_TIMEOUT_READY_XFER:
270 case PLDM_FD_TIMEOUT_DOWNLOAD:
271 case PLDM_FD_TIMEOUT_VERIFY:
272 case PLDM_FD_TIMEOUT_APPLY:
273 return true;
274
275 default:
276 if (reason_code >= PLDM_FD_STATUS_VENDOR_DEFINED_MIN) {
277 return true;
278 }
279 return false;
280 }
281}
282
283/** @brief Check if non functioning component indication in CancelUpdate
284 * response is valid
285 *
286 * @return true if non functioning component indication is valid, false if not
287 */
288static bool is_non_functioning_component_indication_valid(
289 bool8_t non_functioning_component_indication)
290{
291 switch (non_functioning_component_indication) {
292 case PLDM_FWUP_COMPONENTS_FUNCTIONING:
293 case PLDM_FWUP_COMPONENTS_NOT_FUNCTIONING:
294 return true;
295
296 default:
297 return false;
298 }
299}
300
301int decode_pldm_package_header_info(
302 const uint8_t *data, size_t length,
303 struct pldm_package_header_information *package_header_info,
304 struct variable_field *package_version_str)
305{
306 if (data == NULL || package_header_info == NULL ||
307 package_version_str == NULL) {
308 return PLDM_ERROR_INVALID_DATA;
309 }
310
311 if (length < sizeof(struct pldm_package_header_information)) {
312 return PLDM_ERROR_INVALID_LENGTH;
313 }
314
315 struct pldm_package_header_information *data_header =
316 (struct pldm_package_header_information *)(data);
317
318 if (!is_string_type_valid(data_header->package_version_string_type) ||
319 (data_header->package_version_string_length == 0)) {
320 return PLDM_ERROR_INVALID_DATA;
321 }
322
323 if (length < sizeof(struct pldm_package_header_information) +
324 data_header->package_version_string_length) {
325 return PLDM_ERROR_INVALID_LENGTH;
326 }
327
328 if ((data_header->component_bitmap_bit_length %
329 PLDM_FWUP_COMPONENT_BITMAP_MULTIPLE) != 0) {
330 return PLDM_ERROR_INVALID_DATA;
331 }
332
333 memcpy(package_header_info->uuid, data_header->uuid,
334 sizeof(data_header->uuid));
335 package_header_info->package_header_format_version =
336 data_header->package_header_format_version;
337 package_header_info->package_header_size =
338 le16toh(data_header->package_header_size);
Manojkiran Edafc0ce972022-06-25 09:38:28 +0530339 memcpy(package_header_info->package_release_date_time,
340 data_header->package_release_date_time,
341 sizeof(data_header->package_release_date_time));
Patrick Williams70a47ba2021-09-02 09:53:31 -0500342 package_header_info->component_bitmap_bit_length =
343 le16toh(data_header->component_bitmap_bit_length);
344 package_header_info->package_version_string_type =
345 data_header->package_version_string_type;
346 package_header_info->package_version_string_length =
347 data_header->package_version_string_length;
348 package_version_str->ptr =
349 data + sizeof(struct pldm_package_header_information);
350 package_version_str->length =
351 package_header_info->package_version_string_length;
352
353 return PLDM_SUCCESS;
354}
355
356int decode_firmware_device_id_record(
357 const uint8_t *data, size_t length, uint16_t component_bitmap_bit_length,
358 struct pldm_firmware_device_id_record *fw_device_id_record,
359 struct variable_field *applicable_components,
360 struct variable_field *comp_image_set_version_str,
361 struct variable_field *record_descriptors,
362 struct variable_field *fw_device_pkg_data)
363{
364 if (data == NULL || fw_device_id_record == NULL ||
365 applicable_components == NULL ||
366 comp_image_set_version_str == NULL || record_descriptors == NULL ||
367 fw_device_pkg_data == NULL) {
368 return PLDM_ERROR_INVALID_DATA;
369 }
370
371 if (length < sizeof(struct pldm_firmware_device_id_record)) {
372 return PLDM_ERROR_INVALID_LENGTH;
373 }
374
375 if ((component_bitmap_bit_length %
376 PLDM_FWUP_COMPONENT_BITMAP_MULTIPLE) != 0) {
377 return PLDM_ERROR_INVALID_DATA;
378 }
379
380 struct pldm_firmware_device_id_record *data_record =
381 (struct pldm_firmware_device_id_record *)(data);
382
383 if (!is_string_type_valid(
384 data_record->comp_image_set_version_string_type) ||
385 (data_record->comp_image_set_version_string_length == 0)) {
386 return PLDM_ERROR_INVALID_DATA;
387 }
388
389 fw_device_id_record->record_length =
390 le16toh(data_record->record_length);
391 fw_device_id_record->descriptor_count = data_record->descriptor_count;
392 fw_device_id_record->device_update_option_flags.value =
393 le32toh(data_record->device_update_option_flags.value);
394 fw_device_id_record->comp_image_set_version_string_type =
395 data_record->comp_image_set_version_string_type;
396 fw_device_id_record->comp_image_set_version_string_length =
397 data_record->comp_image_set_version_string_length;
398 fw_device_id_record->fw_device_pkg_data_length =
399 le16toh(data_record->fw_device_pkg_data_length);
400
401 if (length < fw_device_id_record->record_length) {
402 return PLDM_ERROR_INVALID_LENGTH;
403 }
404
405 uint16_t applicable_components_length =
406 component_bitmap_bit_length / PLDM_FWUP_COMPONENT_BITMAP_MULTIPLE;
407 uint16_t calc_min_record_length =
408 sizeof(struct pldm_firmware_device_id_record) +
409 applicable_components_length +
410 data_record->comp_image_set_version_string_length +
411 PLDM_FWUP_DEVICE_DESCRIPTOR_MIN_LEN +
412 fw_device_id_record->fw_device_pkg_data_length;
413
414 if (fw_device_id_record->record_length < calc_min_record_length) {
415 return PLDM_ERROR_INVALID_LENGTH;
416 }
417
418 applicable_components->ptr =
419 data + sizeof(struct pldm_firmware_device_id_record);
420 applicable_components->length = applicable_components_length;
421
422 comp_image_set_version_str->ptr =
423 applicable_components->ptr + applicable_components->length;
424 comp_image_set_version_str->length =
425 fw_device_id_record->comp_image_set_version_string_length;
426
427 record_descriptors->ptr = comp_image_set_version_str->ptr +
428 comp_image_set_version_str->length;
429 record_descriptors->length =
430 fw_device_id_record->record_length -
431 sizeof(struct pldm_firmware_device_id_record) -
432 applicable_components_length -
433 fw_device_id_record->comp_image_set_version_string_length -
434 fw_device_id_record->fw_device_pkg_data_length;
435
436 if (fw_device_id_record->fw_device_pkg_data_length) {
437 fw_device_pkg_data->ptr =
438 record_descriptors->ptr + record_descriptors->length;
439 fw_device_pkg_data->length =
440 fw_device_id_record->fw_device_pkg_data_length;
441 }
442
443 return PLDM_SUCCESS;
444}
445
446int decode_descriptor_type_length_value(const uint8_t *data, size_t length,
447 uint16_t *descriptor_type,
448 struct variable_field *descriptor_data)
449{
450 uint16_t descriptor_length = 0;
451
452 if (data == NULL || descriptor_type == NULL ||
453 descriptor_data == NULL) {
454 return PLDM_ERROR_INVALID_DATA;
455 }
456
457 if (length < PLDM_FWUP_DEVICE_DESCRIPTOR_MIN_LEN) {
458 return PLDM_ERROR_INVALID_LENGTH;
459 }
460
461 struct pldm_descriptor_tlv *entry =
462 (struct pldm_descriptor_tlv *)(data);
463
464 *descriptor_type = le16toh(entry->descriptor_type);
465 descriptor_length = le16toh(entry->descriptor_length);
466 if (*descriptor_type != PLDM_FWUP_VENDOR_DEFINED) {
467 if (descriptor_length !=
468 get_descriptor_type_length(*descriptor_type)) {
469 return PLDM_ERROR_INVALID_LENGTH;
470 }
471 }
472
473 if (length < (sizeof(*descriptor_type) + sizeof(descriptor_length) +
474 descriptor_length)) {
475 return PLDM_ERROR_INVALID_LENGTH;
476 }
477
478 descriptor_data->ptr = entry->descriptor_data;
479 descriptor_data->length = descriptor_length;
480
481 return PLDM_SUCCESS;
482}
483
484int decode_vendor_defined_descriptor_value(
485 const uint8_t *data, size_t length, uint8_t *descriptor_title_str_type,
486 struct variable_field *descriptor_title_str,
487 struct variable_field *descriptor_data)
488{
489 if (data == NULL || descriptor_title_str_type == NULL ||
490 descriptor_title_str == NULL || descriptor_data == NULL) {
491 return PLDM_ERROR_INVALID_DATA;
492 }
493
494 if (length < sizeof(struct pldm_vendor_defined_descriptor_title_data)) {
495 return PLDM_ERROR_INVALID_LENGTH;
496 }
497
498 struct pldm_vendor_defined_descriptor_title_data *entry =
499 (struct pldm_vendor_defined_descriptor_title_data *)(data);
500 if (!is_string_type_valid(
501 entry->vendor_defined_descriptor_title_str_type) ||
502 (entry->vendor_defined_descriptor_title_str_len == 0)) {
503 return PLDM_ERROR_INVALID_DATA;
504 }
505
506 // Assuming atleast 1 byte of VendorDefinedDescriptorData
507 if (length < (sizeof(struct pldm_vendor_defined_descriptor_title_data) +
508 entry->vendor_defined_descriptor_title_str_len)) {
509 return PLDM_ERROR_INVALID_LENGTH;
510 }
511
512 *descriptor_title_str_type =
513 entry->vendor_defined_descriptor_title_str_type;
514 descriptor_title_str->ptr = entry->vendor_defined_descriptor_title_str;
515 descriptor_title_str->length =
516 entry->vendor_defined_descriptor_title_str_len;
517
518 descriptor_data->ptr =
519 descriptor_title_str->ptr + descriptor_title_str->length;
520 descriptor_data->length =
521 length - sizeof(entry->vendor_defined_descriptor_title_str_type) -
522 sizeof(entry->vendor_defined_descriptor_title_str_len) -
523 descriptor_title_str->length;
524
525 return PLDM_SUCCESS;
526}
527
528int decode_pldm_comp_image_info(
529 const uint8_t *data, size_t length,
530 struct pldm_component_image_information *pldm_comp_image_info,
531 struct variable_field *comp_version_str)
532{
533 if (data == NULL || pldm_comp_image_info == NULL ||
534 comp_version_str == NULL) {
535 return PLDM_ERROR_INVALID_DATA;
536 }
537
538 if (length < sizeof(struct pldm_component_image_information)) {
539 return PLDM_ERROR_INVALID_LENGTH;
540 }
541
542 struct pldm_component_image_information *data_header =
543 (struct pldm_component_image_information *)(data);
544
545 if (!is_string_type_valid(data_header->comp_version_string_type) ||
546 (data_header->comp_version_string_length == 0)) {
547 return PLDM_ERROR_INVALID_DATA;
548 }
549
550 if (length < sizeof(struct pldm_component_image_information) +
551 data_header->comp_version_string_length) {
552 return PLDM_ERROR_INVALID_LENGTH;
553 }
554
555 pldm_comp_image_info->comp_classification =
556 le16toh(data_header->comp_classification);
557 pldm_comp_image_info->comp_identifier =
558 le16toh(data_header->comp_identifier);
559 pldm_comp_image_info->comp_comparison_stamp =
560 le32toh(data_header->comp_comparison_stamp);
561 pldm_comp_image_info->comp_options.value =
562 le16toh(data_header->comp_options.value);
563 pldm_comp_image_info->requested_comp_activation_method.value =
564 le16toh(data_header->requested_comp_activation_method.value);
565 pldm_comp_image_info->comp_location_offset =
566 le32toh(data_header->comp_location_offset);
567 pldm_comp_image_info->comp_size = le32toh(data_header->comp_size);
568 pldm_comp_image_info->comp_version_string_type =
569 data_header->comp_version_string_type;
570 pldm_comp_image_info->comp_version_string_length =
571 data_header->comp_version_string_length;
572
573 if ((pldm_comp_image_info->comp_options.bits.bit1 == false &&
574 pldm_comp_image_info->comp_comparison_stamp !=
575 PLDM_FWUP_INVALID_COMPONENT_COMPARISON_TIMESTAMP)) {
576 return PLDM_ERROR_INVALID_DATA;
577 }
578
579 if (pldm_comp_image_info->comp_location_offset == 0 ||
580 pldm_comp_image_info->comp_size == 0) {
581 return PLDM_ERROR_INVALID_DATA;
582 }
583
584 comp_version_str->ptr =
585 data + sizeof(struct pldm_component_image_information);
586 comp_version_str->length =
587 pldm_comp_image_info->comp_version_string_length;
588
589 return PLDM_SUCCESS;
590}
591
592int encode_query_device_identifiers_req(uint8_t instance_id,
593 size_t payload_length,
594 struct pldm_msg *msg)
595{
596 if (msg == NULL) {
597 return PLDM_ERROR_INVALID_DATA;
598 }
599
600 if (payload_length != PLDM_QUERY_DEVICE_IDENTIFIERS_REQ_BYTES) {
601 return PLDM_ERROR_INVALID_LENGTH;
602 }
603
604 return encode_pldm_header_only(PLDM_REQUEST, instance_id, PLDM_FWUP,
605 PLDM_QUERY_DEVICE_IDENTIFIERS, msg);
606}
607
608int decode_query_device_identifiers_resp(const struct pldm_msg *msg,
609 size_t payload_length,
610 uint8_t *completion_code,
611 uint32_t *device_identifiers_len,
612 uint8_t *descriptor_count,
613 uint8_t **descriptor_data)
614{
615 if (msg == NULL || completion_code == NULL ||
616 device_identifiers_len == NULL || descriptor_count == NULL ||
617 descriptor_data == NULL) {
618 return PLDM_ERROR_INVALID_DATA;
619 }
620
621 *completion_code = msg->payload[0];
622 if (PLDM_SUCCESS != *completion_code) {
623 return PLDM_SUCCESS;
624 }
625
626 if (payload_length <
627 sizeof(struct pldm_query_device_identifiers_resp)) {
628 return PLDM_ERROR_INVALID_LENGTH;
629 }
630
631 struct pldm_query_device_identifiers_resp *response =
632 (struct pldm_query_device_identifiers_resp *)msg->payload;
633 *device_identifiers_len = le32toh(response->device_identifiers_len);
634
635 if (*device_identifiers_len < PLDM_FWUP_DEVICE_DESCRIPTOR_MIN_LEN) {
636 return PLDM_ERROR_INVALID_LENGTH;
637 }
638
639 if (payload_length !=
640 sizeof(struct pldm_query_device_identifiers_resp) +
641 *device_identifiers_len) {
642 return PLDM_ERROR_INVALID_LENGTH;
643 }
644 *descriptor_count = response->descriptor_count;
645
646 if (*descriptor_count == 0) {
647 return PLDM_ERROR_INVALID_DATA;
648 }
649 *descriptor_data =
650 (uint8_t *)(msg->payload +
651 sizeof(struct pldm_query_device_identifiers_resp));
652 return PLDM_SUCCESS;
653}
654
655int encode_get_firmware_parameters_req(uint8_t instance_id,
656 size_t payload_length,
657 struct pldm_msg *msg)
658{
659 if (msg == NULL) {
660 return PLDM_ERROR_INVALID_DATA;
661 }
662
663 if (payload_length != PLDM_GET_FIRMWARE_PARAMETERS_REQ_BYTES) {
664 return PLDM_ERROR_INVALID_LENGTH;
665 }
666
667 return encode_pldm_header_only(PLDM_REQUEST, instance_id, PLDM_FWUP,
668 PLDM_GET_FIRMWARE_PARAMETERS, msg);
669}
670
671int decode_get_firmware_parameters_resp(
672 const struct pldm_msg *msg, size_t payload_length,
673 struct pldm_get_firmware_parameters_resp *resp_data,
674 struct variable_field *active_comp_image_set_ver_str,
675 struct variable_field *pending_comp_image_set_ver_str,
676 struct variable_field *comp_parameter_table)
677{
678 if (msg == NULL || resp_data == NULL ||
679 active_comp_image_set_ver_str == NULL ||
680 pending_comp_image_set_ver_str == NULL ||
681 comp_parameter_table == NULL || !payload_length) {
682 return PLDM_ERROR_INVALID_DATA;
683 }
684
685 resp_data->completion_code = msg->payload[0];
686 if (PLDM_SUCCESS != resp_data->completion_code) {
687 return PLDM_SUCCESS;
688 }
689
690 if (payload_length < sizeof(struct pldm_get_firmware_parameters_resp)) {
691 return PLDM_ERROR_INVALID_LENGTH;
692 }
693
694 struct pldm_get_firmware_parameters_resp *response =
695 (struct pldm_get_firmware_parameters_resp *)msg->payload;
696
697 if (!is_string_type_valid(
698 response->active_comp_image_set_ver_str_type) ||
699 (response->active_comp_image_set_ver_str_len == 0)) {
700 return PLDM_ERROR_INVALID_DATA;
701 }
702
703 if (response->pending_comp_image_set_ver_str_len == 0) {
704 if (response->pending_comp_image_set_ver_str_type !=
705 PLDM_STR_TYPE_UNKNOWN) {
706 return PLDM_ERROR_INVALID_DATA;
707 }
708 } else {
709 if (!is_string_type_valid(
710 response->pending_comp_image_set_ver_str_type)) {
711 return PLDM_ERROR_INVALID_DATA;
712 }
713 }
714
715 size_t partial_response_length =
716 sizeof(struct pldm_get_firmware_parameters_resp) +
717 response->active_comp_image_set_ver_str_len +
718 response->pending_comp_image_set_ver_str_len;
719
720 if (payload_length < partial_response_length) {
721 return PLDM_ERROR_INVALID_LENGTH;
722 }
723
724 resp_data->capabilities_during_update.value =
725 le32toh(response->capabilities_during_update.value);
726 resp_data->comp_count = le16toh(response->comp_count);
727 resp_data->active_comp_image_set_ver_str_type =
728 response->active_comp_image_set_ver_str_type;
729 resp_data->active_comp_image_set_ver_str_len =
730 response->active_comp_image_set_ver_str_len;
731 resp_data->pending_comp_image_set_ver_str_type =
732 response->pending_comp_image_set_ver_str_type;
733 resp_data->pending_comp_image_set_ver_str_len =
734 response->pending_comp_image_set_ver_str_len;
735
736 active_comp_image_set_ver_str->ptr =
737 msg->payload + sizeof(struct pldm_get_firmware_parameters_resp);
738 active_comp_image_set_ver_str->length =
739 resp_data->active_comp_image_set_ver_str_len;
740
741 if (resp_data->pending_comp_image_set_ver_str_len != 0) {
742 pending_comp_image_set_ver_str->ptr =
743 msg->payload +
744 sizeof(struct pldm_get_firmware_parameters_resp) +
745 resp_data->active_comp_image_set_ver_str_len;
746 pending_comp_image_set_ver_str->length =
747 resp_data->pending_comp_image_set_ver_str_len;
748 } else {
749 pending_comp_image_set_ver_str->ptr = NULL;
750 pending_comp_image_set_ver_str->length = 0;
751 }
752
753 if (payload_length > partial_response_length && resp_data->comp_count) {
754 comp_parameter_table->ptr =
755 msg->payload +
756 sizeof(struct pldm_get_firmware_parameters_resp) +
757 resp_data->active_comp_image_set_ver_str_len +
758 resp_data->pending_comp_image_set_ver_str_len;
759 comp_parameter_table->length =
760 payload_length - partial_response_length;
761 } else {
762 comp_parameter_table->ptr = NULL;
763 comp_parameter_table->length = 0;
764 }
765
766 return PLDM_SUCCESS;
767}
768
769int decode_get_firmware_parameters_resp_comp_entry(
770 const uint8_t *data, size_t length,
771 struct pldm_component_parameter_entry *component_data,
772 struct variable_field *active_comp_ver_str,
773 struct variable_field *pending_comp_ver_str)
774{
775 if (data == NULL || component_data == NULL ||
776 active_comp_ver_str == NULL || pending_comp_ver_str == NULL) {
777 return PLDM_ERROR_INVALID_DATA;
778 }
779
780 if (length < sizeof(struct pldm_component_parameter_entry)) {
781 return PLDM_ERROR_INVALID_LENGTH;
782 }
783
784 struct pldm_component_parameter_entry *entry =
785 (struct pldm_component_parameter_entry *)(data);
Patrick Williams70a47ba2021-09-02 09:53:31 -0500786
787 size_t entry_length = sizeof(struct pldm_component_parameter_entry) +
788 entry->active_comp_ver_str_len +
789 entry->pending_comp_ver_str_len;
790
791 if (length < entry_length) {
792 return PLDM_ERROR_INVALID_LENGTH;
793 }
794
795 component_data->comp_classification =
796 le16toh(entry->comp_classification);
797 component_data->comp_identifier = le16toh(entry->comp_identifier);
798 component_data->comp_classification_index =
799 entry->comp_classification_index;
800 component_data->active_comp_comparison_stamp =
801 le32toh(entry->active_comp_comparison_stamp);
802 component_data->active_comp_ver_str_type =
803 entry->active_comp_ver_str_type;
804 component_data->active_comp_ver_str_len =
805 entry->active_comp_ver_str_len;
806 memcpy(component_data->active_comp_release_date,
807 entry->active_comp_release_date,
808 sizeof(entry->active_comp_release_date));
809 component_data->pending_comp_comparison_stamp =
810 le32toh(entry->pending_comp_comparison_stamp);
811 component_data->pending_comp_ver_str_type =
812 entry->pending_comp_ver_str_type;
813 component_data->pending_comp_ver_str_len =
814 entry->pending_comp_ver_str_len;
815 memcpy(component_data->pending_comp_release_date,
816 entry->pending_comp_release_date,
817 sizeof(entry->pending_comp_release_date));
818 component_data->comp_activation_methods.value =
819 le16toh(entry->comp_activation_methods.value);
820 component_data->capabilities_during_update.value =
821 le32toh(entry->capabilities_during_update.value);
822
Tom Joseph3a6846c2021-09-08 17:34:29 +0530823 if (entry->active_comp_ver_str_len != 0) {
824 active_comp_ver_str->ptr =
825 data + sizeof(struct pldm_component_parameter_entry);
826 active_comp_ver_str->length = entry->active_comp_ver_str_len;
827 } else {
828 active_comp_ver_str->ptr = NULL;
829 active_comp_ver_str->length = 0;
830 }
Patrick Williams70a47ba2021-09-02 09:53:31 -0500831
832 if (entry->pending_comp_ver_str_len != 0) {
833
834 pending_comp_ver_str->ptr =
835 data + sizeof(struct pldm_component_parameter_entry) +
836 entry->active_comp_ver_str_len;
837 pending_comp_ver_str->length = entry->pending_comp_ver_str_len;
838 } else {
839 pending_comp_ver_str->ptr = NULL;
840 pending_comp_ver_str->length = 0;
841 }
842 return PLDM_SUCCESS;
843}
844
845int encode_request_update_req(uint8_t instance_id, uint32_t max_transfer_size,
846 uint16_t num_of_comp,
847 uint8_t max_outstanding_transfer_req,
848 uint16_t pkg_data_len,
849 uint8_t comp_image_set_ver_str_type,
850 uint8_t comp_image_set_ver_str_len,
851 const struct variable_field *comp_img_set_ver_str,
852 struct pldm_msg *msg, size_t payload_length)
853{
854 if (comp_img_set_ver_str == NULL || comp_img_set_ver_str->ptr == NULL ||
855 msg == NULL) {
856 return PLDM_ERROR_INVALID_DATA;
857 }
858
859 if (payload_length != sizeof(struct pldm_request_update_req) +
860 comp_img_set_ver_str->length) {
861 return PLDM_ERROR_INVALID_LENGTH;
862 }
863
864 if ((comp_image_set_ver_str_len == 0) ||
865 (comp_image_set_ver_str_len != comp_img_set_ver_str->length)) {
866 return PLDM_ERROR_INVALID_DATA;
867 }
868
869 if ((max_transfer_size < PLDM_FWUP_BASELINE_TRANSFER_SIZE) ||
870 (max_outstanding_transfer_req < PLDM_FWUP_MIN_OUTSTANDING_REQ)) {
871 return PLDM_ERROR_INVALID_DATA;
872 }
873
874 if (!is_string_type_valid(comp_image_set_ver_str_type)) {
875 return PLDM_ERROR_INVALID_DATA;
876 }
877
878 struct pldm_header_info header = {0};
879 header.instance = instance_id;
880 header.msg_type = PLDM_REQUEST;
881 header.pldm_type = PLDM_FWUP;
882 header.command = PLDM_REQUEST_UPDATE;
883 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
884 if (rc) {
885 return rc;
886 }
887
888 struct pldm_request_update_req *request =
889 (struct pldm_request_update_req *)msg->payload;
890
891 request->max_transfer_size = htole32(max_transfer_size);
892 request->num_of_comp = htole16(num_of_comp);
893 request->max_outstanding_transfer_req = max_outstanding_transfer_req;
894 request->pkg_data_len = htole16(pkg_data_len);
895 request->comp_image_set_ver_str_type = comp_image_set_ver_str_type;
896 request->comp_image_set_ver_str_len = comp_image_set_ver_str_len;
897
898 memcpy(msg->payload + sizeof(struct pldm_request_update_req),
899 comp_img_set_ver_str->ptr, comp_img_set_ver_str->length);
900
901 return PLDM_SUCCESS;
902}
903
904int decode_request_update_resp(const struct pldm_msg *msg,
905 size_t payload_length, uint8_t *completion_code,
906 uint16_t *fd_meta_data_len,
907 uint8_t *fd_will_send_pkg_data)
908{
909 if (msg == NULL || completion_code == NULL ||
910 fd_meta_data_len == NULL || fd_will_send_pkg_data == NULL ||
911 !payload_length) {
912 return PLDM_ERROR_INVALID_DATA;
913 }
914
915 *completion_code = msg->payload[0];
916 if (*completion_code != PLDM_SUCCESS) {
917 return PLDM_SUCCESS;
918 }
919
920 if (payload_length != sizeof(struct pldm_request_update_resp)) {
921 return PLDM_ERROR_INVALID_LENGTH;
922 }
923
924 struct pldm_request_update_resp *response =
925 (struct pldm_request_update_resp *)msg->payload;
926
927 *fd_meta_data_len = le16toh(response->fd_meta_data_len);
928 *fd_will_send_pkg_data = response->fd_will_send_pkg_data;
929
930 return PLDM_SUCCESS;
931}
932
933int encode_pass_component_table_req(
934 uint8_t instance_id, uint8_t transfer_flag, uint16_t comp_classification,
935 uint16_t comp_identifier, uint8_t comp_classification_index,
936 uint32_t comp_comparison_stamp, uint8_t comp_ver_str_type,
937 uint8_t comp_ver_str_len, const struct variable_field *comp_ver_str,
938 struct pldm_msg *msg, size_t payload_length)
939{
940 if (comp_ver_str == NULL || comp_ver_str->ptr == NULL || msg == NULL) {
941 return PLDM_ERROR_INVALID_DATA;
942 }
943
944 if (payload_length != sizeof(struct pldm_pass_component_table_req) +
945 comp_ver_str->length) {
946 return PLDM_ERROR_INVALID_LENGTH;
947 }
948
949 if ((comp_ver_str_len == 0) ||
950 (comp_ver_str_len != comp_ver_str->length)) {
951 return PLDM_ERROR_INVALID_DATA;
952 }
953
954 if (!is_transfer_flag_valid(transfer_flag)) {
955 return PLDM_INVALID_TRANSFER_OPERATION_FLAG;
956 }
957
958 if (!is_string_type_valid(comp_ver_str_type)) {
959 return PLDM_ERROR_INVALID_DATA;
960 }
961
962 struct pldm_header_info header = {0};
963 header.instance = instance_id;
964 header.msg_type = PLDM_REQUEST;
965 header.pldm_type = PLDM_FWUP;
966 header.command = PLDM_PASS_COMPONENT_TABLE;
967 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
968 if (rc) {
969 return rc;
970 }
971
972 struct pldm_pass_component_table_req *request =
973 (struct pldm_pass_component_table_req *)msg->payload;
974
975 request->transfer_flag = transfer_flag;
976 request->comp_classification = htole16(comp_classification);
977 request->comp_identifier = htole16(comp_identifier);
978 request->comp_classification_index = comp_classification_index;
979 request->comp_comparison_stamp = htole32(comp_comparison_stamp);
980 request->comp_ver_str_type = comp_ver_str_type;
981 request->comp_ver_str_len = comp_ver_str_len;
982
983 memcpy(msg->payload + sizeof(struct pldm_pass_component_table_req),
984 comp_ver_str->ptr, comp_ver_str->length);
985
986 return PLDM_SUCCESS;
987}
988
989int decode_pass_component_table_resp(const struct pldm_msg *msg,
990 const size_t payload_length,
991 uint8_t *completion_code,
992 uint8_t *comp_resp,
993 uint8_t *comp_resp_code)
994{
995 if (msg == NULL || completion_code == NULL || comp_resp == NULL ||
996 comp_resp_code == NULL || !payload_length) {
997 return PLDM_ERROR_INVALID_DATA;
998 }
999
1000 *completion_code = msg->payload[0];
1001 if (*completion_code != PLDM_SUCCESS) {
1002 return PLDM_SUCCESS;
1003 }
1004
1005 if (payload_length != sizeof(struct pldm_pass_component_table_resp)) {
1006 return PLDM_ERROR_INVALID_LENGTH;
1007 }
1008
1009 struct pldm_pass_component_table_resp *response =
1010 (struct pldm_pass_component_table_resp *)msg->payload;
1011
1012 if (!is_comp_resp_valid(response->comp_resp)) {
1013 return PLDM_ERROR_INVALID_DATA;
1014 }
1015
1016 if (!is_comp_resp_code_valid(response->comp_resp_code)) {
1017 return PLDM_ERROR_INVALID_DATA;
1018 }
1019
1020 *comp_resp = response->comp_resp;
1021 *comp_resp_code = response->comp_resp_code;
1022
1023 return PLDM_SUCCESS;
1024}
1025
1026int encode_update_component_req(
1027 uint8_t instance_id, uint16_t comp_classification, uint16_t comp_identifier,
1028 uint8_t comp_classification_index, uint32_t comp_comparison_stamp,
1029 uint32_t comp_image_size, bitfield32_t update_option_flags,
1030 uint8_t comp_ver_str_type, uint8_t comp_ver_str_len,
1031 const struct variable_field *comp_ver_str, struct pldm_msg *msg,
1032 size_t payload_length)
1033{
1034 if (comp_ver_str == NULL || comp_ver_str->ptr == NULL || msg == NULL) {
1035 return PLDM_ERROR_INVALID_DATA;
1036 }
1037
1038 if (payload_length !=
1039 sizeof(struct pldm_update_component_req) + comp_ver_str->length) {
1040 return PLDM_ERROR_INVALID_LENGTH;
1041 }
1042
1043 if (!comp_image_size) {
1044 return PLDM_ERROR_INVALID_DATA;
1045 }
1046
1047 if ((comp_ver_str_len == 0) ||
1048 (comp_ver_str_len != comp_ver_str->length)) {
1049 return PLDM_ERROR_INVALID_DATA;
1050 }
1051
1052 if (!is_string_type_valid(comp_ver_str_type)) {
1053 return PLDM_ERROR_INVALID_DATA;
1054 }
1055
1056 struct pldm_header_info header = {0};
1057 header.instance = instance_id;
1058 header.msg_type = PLDM_REQUEST;
1059 header.pldm_type = PLDM_FWUP;
1060 header.command = PLDM_UPDATE_COMPONENT;
1061 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1062 if (rc) {
1063 return rc;
1064 }
1065
1066 struct pldm_update_component_req *request =
1067 (struct pldm_update_component_req *)msg->payload;
1068
1069 request->comp_classification = htole16(comp_classification);
1070 request->comp_identifier = htole16(comp_identifier);
1071 request->comp_classification_index = comp_classification_index;
1072 request->comp_comparison_stamp = htole32(comp_comparison_stamp);
1073 request->comp_image_size = htole32(comp_image_size);
1074 request->update_option_flags.value = htole32(update_option_flags.value);
1075 request->comp_ver_str_type = comp_ver_str_type;
1076 request->comp_ver_str_len = comp_ver_str_len;
1077
1078 memcpy(msg->payload + sizeof(struct pldm_update_component_req),
1079 comp_ver_str->ptr, comp_ver_str->length);
1080
1081 return PLDM_SUCCESS;
1082}
1083
1084int decode_update_component_resp(const struct pldm_msg *msg,
1085 size_t payload_length,
1086 uint8_t *completion_code,
Manojkiran Edafc0ce972022-06-25 09:38:28 +05301087 uint8_t *comp_compatibility_resp,
1088 uint8_t *comp_compatibility_resp_code,
Patrick Williams70a47ba2021-09-02 09:53:31 -05001089 bitfield32_t *update_option_flags_enabled,
1090 uint16_t *time_before_req_fw_data)
1091{
1092 if (msg == NULL || completion_code == NULL ||
Manojkiran Edafc0ce972022-06-25 09:38:28 +05301093 comp_compatibility_resp == NULL ||
1094 comp_compatibility_resp_code == NULL ||
Patrick Williams70a47ba2021-09-02 09:53:31 -05001095 update_option_flags_enabled == NULL ||
1096 time_before_req_fw_data == NULL || !payload_length) {
1097 return PLDM_ERROR_INVALID_DATA;
1098 }
1099
1100 *completion_code = msg->payload[0];
1101 if (*completion_code != PLDM_SUCCESS) {
1102 return PLDM_SUCCESS;
1103 }
1104
1105 if (payload_length != sizeof(struct pldm_update_component_resp)) {
1106 return PLDM_ERROR_INVALID_LENGTH;
1107 }
1108
1109 struct pldm_update_component_resp *response =
1110 (struct pldm_update_component_resp *)msg->payload;
1111
1112 if (!is_comp_compatibility_resp_valid(
Manojkiran Edafc0ce972022-06-25 09:38:28 +05301113 response->comp_compatibility_resp)) {
Patrick Williams70a47ba2021-09-02 09:53:31 -05001114 return PLDM_ERROR_INVALID_DATA;
1115 }
1116
1117 if (!is_comp_compatibility_resp_code_valid(
Manojkiran Edafc0ce972022-06-25 09:38:28 +05301118 response->comp_compatibility_resp_code)) {
Patrick Williams70a47ba2021-09-02 09:53:31 -05001119 return PLDM_ERROR_INVALID_DATA;
1120 }
1121
Manojkiran Edafc0ce972022-06-25 09:38:28 +05301122 *comp_compatibility_resp = response->comp_compatibility_resp;
1123 *comp_compatibility_resp_code = response->comp_compatibility_resp_code;
Patrick Williams70a47ba2021-09-02 09:53:31 -05001124 update_option_flags_enabled->value =
1125 le32toh(response->update_option_flags_enabled.value);
1126 *time_before_req_fw_data = le16toh(response->time_before_req_fw_data);
1127
1128 return PLDM_SUCCESS;
1129}
1130
1131int decode_request_firmware_data_req(const struct pldm_msg *msg,
1132 size_t payload_length, uint32_t *offset,
1133 uint32_t *length)
1134{
1135 if (msg == NULL || offset == NULL || length == NULL) {
1136 return PLDM_ERROR_INVALID_DATA;
1137 }
1138 if (payload_length != sizeof(struct pldm_request_firmware_data_req)) {
1139 return PLDM_ERROR_INVALID_LENGTH;
1140 }
1141 struct pldm_request_firmware_data_req *request =
1142 (struct pldm_request_firmware_data_req *)msg->payload;
1143 *offset = le32toh(request->offset);
1144 *length = le32toh(request->length);
1145
1146 if (*length < PLDM_FWUP_BASELINE_TRANSFER_SIZE) {
1147 return PLDM_FWUP_INVALID_TRANSFER_LENGTH;
1148 }
1149
1150 return PLDM_SUCCESS;
1151}
1152
1153int encode_request_firmware_data_resp(uint8_t instance_id,
1154 uint8_t completion_code,
1155 struct pldm_msg *msg,
1156 size_t payload_length)
1157{
1158 if (msg == NULL || !payload_length) {
1159 return PLDM_ERROR_INVALID_DATA;
1160 }
1161
1162 struct pldm_header_info header = {0};
1163 header.instance = instance_id;
1164 header.msg_type = PLDM_RESPONSE;
1165 header.pldm_type = PLDM_FWUP;
1166 header.command = PLDM_REQUEST_FIRMWARE_DATA;
1167 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1168 if (rc) {
1169 return rc;
1170 }
1171
1172 msg->payload[0] = completion_code;
1173
1174 return PLDM_SUCCESS;
1175}
1176
1177int decode_transfer_complete_req(const struct pldm_msg *msg,
1178 size_t payload_length,
1179 uint8_t *transfer_result)
1180{
1181 if (msg == NULL || transfer_result == NULL) {
1182 return PLDM_ERROR_INVALID_DATA;
1183 }
1184
1185 if (payload_length != sizeof(*transfer_result)) {
1186 return PLDM_ERROR_INVALID_LENGTH;
1187 }
1188
1189 *transfer_result = msg->payload[0];
1190 return PLDM_SUCCESS;
1191}
1192
1193int encode_transfer_complete_resp(uint8_t instance_id, uint8_t completion_code,
1194 struct pldm_msg *msg, size_t payload_length)
1195{
1196 if (msg == NULL) {
1197 return PLDM_ERROR_INVALID_DATA;
1198 }
1199
1200 if (payload_length != sizeof(completion_code)) {
1201 return PLDM_ERROR_INVALID_LENGTH;
1202 }
1203
1204 struct pldm_header_info header = {0};
1205 header.instance = instance_id;
1206 header.msg_type = PLDM_RESPONSE;
1207 header.pldm_type = PLDM_FWUP;
1208 header.command = PLDM_TRANSFER_COMPLETE;
1209 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1210 if (rc) {
1211 return rc;
1212 }
1213
1214 msg->payload[0] = completion_code;
1215
1216 return PLDM_SUCCESS;
1217}
1218
1219int decode_verify_complete_req(const struct pldm_msg *msg,
1220 size_t payload_length, uint8_t *verify_result)
1221{
1222 if (msg == NULL || verify_result == NULL) {
1223 return PLDM_ERROR_INVALID_DATA;
1224 }
1225
1226 if (payload_length != sizeof(*verify_result)) {
1227 return PLDM_ERROR_INVALID_LENGTH;
1228 }
1229
1230 *verify_result = msg->payload[0];
1231 return PLDM_SUCCESS;
1232}
1233
1234int encode_verify_complete_resp(uint8_t instance_id, uint8_t completion_code,
1235 struct pldm_msg *msg, size_t payload_length)
1236{
1237 if (msg == NULL) {
1238 return PLDM_ERROR_INVALID_DATA;
1239 }
1240
1241 if (payload_length != sizeof(completion_code)) {
1242 return PLDM_ERROR_INVALID_LENGTH;
1243 }
1244
1245 struct pldm_header_info header = {0};
1246 header.instance = instance_id;
1247 header.msg_type = PLDM_RESPONSE;
1248 header.pldm_type = PLDM_FWUP;
1249 header.command = PLDM_VERIFY_COMPLETE;
1250 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1251 if (rc) {
1252 return rc;
1253 }
1254
1255 msg->payload[0] = completion_code;
1256
1257 return PLDM_SUCCESS;
1258}
1259
1260int decode_apply_complete_req(
1261 const struct pldm_msg *msg, size_t payload_length, uint8_t *apply_result,
1262 bitfield16_t *comp_activation_methods_modification)
1263{
1264 if (msg == NULL || apply_result == NULL ||
1265 comp_activation_methods_modification == NULL) {
1266 return PLDM_ERROR_INVALID_DATA;
1267 }
1268
1269 if (payload_length != sizeof(struct pldm_apply_complete_req)) {
1270 return PLDM_ERROR_INVALID_LENGTH;
1271 }
1272
1273 struct pldm_apply_complete_req *request =
1274 (struct pldm_apply_complete_req *)msg->payload;
1275
1276 *apply_result = request->apply_result;
1277 comp_activation_methods_modification->value =
1278 le16toh(request->comp_activation_methods_modification.value);
1279
1280 if ((*apply_result != PLDM_FWUP_APPLY_SUCCESS_WITH_ACTIVATION_METHOD) &&
1281 comp_activation_methods_modification->value) {
1282 return PLDM_ERROR_INVALID_DATA;
1283 }
1284
1285 return PLDM_SUCCESS;
1286}
1287
1288int encode_apply_complete_resp(uint8_t instance_id, uint8_t completion_code,
1289 struct pldm_msg *msg, size_t payload_length)
1290{
1291 if (msg == NULL) {
1292 return PLDM_ERROR_INVALID_DATA;
1293 }
1294
1295 if (payload_length != sizeof(completion_code)) {
1296 return PLDM_ERROR_INVALID_LENGTH;
1297 }
1298
1299 struct pldm_header_info header = {0};
1300 header.instance = instance_id;
1301 header.msg_type = PLDM_RESPONSE;
1302 header.pldm_type = PLDM_FWUP;
1303 header.command = PLDM_APPLY_COMPLETE;
1304 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1305 if (rc) {
1306 return rc;
1307 }
1308
1309 msg->payload[0] = completion_code;
1310
1311 return PLDM_SUCCESS;
1312}
1313
1314int encode_activate_firmware_req(uint8_t instance_id,
1315 bool8_t self_contained_activation_req,
1316 struct pldm_msg *msg, size_t payload_length)
1317{
1318 if (msg == NULL) {
1319 return PLDM_ERROR_INVALID_DATA;
1320 }
1321
1322 if (payload_length != sizeof(struct pldm_activate_firmware_req)) {
1323 return PLDM_ERROR_INVALID_LENGTH;
1324 }
1325
1326 if (!is_self_contained_activation_req_valid(
1327 self_contained_activation_req)) {
1328 return PLDM_ERROR_INVALID_DATA;
1329 }
1330
1331 struct pldm_header_info header = {0};
1332 header.instance = instance_id;
1333 header.msg_type = PLDM_REQUEST;
1334 header.pldm_type = PLDM_FWUP;
1335 header.command = PLDM_ACTIVATE_FIRMWARE;
1336 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1337 if (rc) {
1338 return rc;
1339 }
1340
1341 struct pldm_activate_firmware_req *request =
1342 (struct pldm_activate_firmware_req *)msg->payload;
1343
1344 request->self_contained_activation_req = self_contained_activation_req;
1345
1346 return PLDM_SUCCESS;
1347}
1348
1349int decode_activate_firmware_resp(const struct pldm_msg *msg,
1350 size_t payload_length,
1351 uint8_t *completion_code,
1352 uint16_t *estimated_time_activation)
1353{
1354 if (msg == NULL || completion_code == NULL ||
1355 estimated_time_activation == NULL || !payload_length) {
1356 return PLDM_ERROR_INVALID_DATA;
1357 }
1358
1359 *completion_code = msg->payload[0];
1360 if (*completion_code != PLDM_SUCCESS) {
1361 return PLDM_SUCCESS;
1362 }
1363
1364 if (payload_length != sizeof(struct pldm_activate_firmware_resp)) {
1365 return PLDM_ERROR_INVALID_LENGTH;
1366 }
1367
1368 struct pldm_activate_firmware_resp *response =
1369 (struct pldm_activate_firmware_resp *)msg->payload;
1370
1371 *estimated_time_activation =
1372 le16toh(response->estimated_time_activation);
1373
1374 return PLDM_SUCCESS;
1375}
1376
1377int encode_get_status_req(uint8_t instance_id, struct pldm_msg *msg,
1378 size_t payload_length)
1379{
1380 if (msg == NULL) {
1381 return PLDM_ERROR_INVALID_DATA;
1382 }
1383
1384 if (payload_length != PLDM_GET_STATUS_REQ_BYTES) {
1385 return PLDM_ERROR_INVALID_LENGTH;
1386 }
1387
1388 struct pldm_header_info header = {0};
1389 header.instance = instance_id;
1390 header.msg_type = PLDM_REQUEST;
1391 header.pldm_type = PLDM_FWUP;
1392 header.command = PLDM_GET_STATUS;
1393 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1394 if (rc) {
1395 return rc;
1396 }
1397
1398 return PLDM_SUCCESS;
1399}
1400
1401int decode_get_status_resp(const struct pldm_msg *msg, size_t payload_length,
1402 uint8_t *completion_code, uint8_t *current_state,
1403 uint8_t *previous_state, uint8_t *aux_state,
1404 uint8_t *aux_state_status, uint8_t *progress_percent,
1405 uint8_t *reason_code,
1406 bitfield32_t *update_option_flags_enabled)
1407{
1408 if (msg == NULL || completion_code == NULL || current_state == NULL ||
1409 previous_state == NULL || aux_state == NULL ||
1410 aux_state_status == NULL || progress_percent == NULL ||
1411 reason_code == NULL || update_option_flags_enabled == NULL ||
1412 !payload_length) {
1413 return PLDM_ERROR_INVALID_DATA;
1414 }
1415
1416 *completion_code = msg->payload[0];
1417 if (*completion_code != PLDM_SUCCESS) {
1418 return PLDM_SUCCESS;
1419 }
1420
1421 if (payload_length != sizeof(struct pldm_get_status_resp)) {
1422 return PLDM_ERROR_INVALID_LENGTH;
1423 }
1424 struct pldm_get_status_resp *response =
1425 (struct pldm_get_status_resp *)msg->payload;
1426
1427 if (!is_state_valid(response->current_state)) {
1428 return PLDM_ERROR_INVALID_DATA;
1429 }
1430 if (!is_state_valid(response->previous_state)) {
1431 return PLDM_ERROR_INVALID_DATA;
1432 }
1433 if (!is_aux_state_valid(response->aux_state)) {
1434 return PLDM_ERROR_INVALID_DATA;
1435 }
1436 if (!is_aux_state_status_valid(response->aux_state_status)) {
1437 return PLDM_ERROR_INVALID_DATA;
1438 }
1439 if (response->progress_percent > PLDM_FWUP_MAX_PROGRESS_PERCENT) {
1440 return PLDM_ERROR_INVALID_DATA;
1441 }
1442 if (!is_reason_code_valid(response->reason_code)) {
1443 return PLDM_ERROR_INVALID_DATA;
1444 }
1445
1446 if ((response->current_state == PLDM_FD_STATE_IDLE) ||
1447 (response->current_state == PLDM_FD_STATE_LEARN_COMPONENTS) ||
1448 (response->current_state == PLDM_FD_STATE_READY_XFER)) {
1449 if (response->aux_state !=
1450 PLDM_FD_IDLE_LEARN_COMPONENTS_READ_XFER) {
1451 return PLDM_ERROR_INVALID_DATA;
1452 }
1453 }
1454
1455 *current_state = response->current_state;
1456 *previous_state = response->previous_state;
1457 *aux_state = response->aux_state;
1458 *aux_state_status = response->aux_state_status;
1459 *progress_percent = response->progress_percent;
1460 *reason_code = response->reason_code;
1461 update_option_flags_enabled->value =
1462 le32toh(response->update_option_flags_enabled.value);
1463
1464 return PLDM_SUCCESS;
1465}
1466
1467int encode_cancel_update_component_req(uint8_t instance_id,
1468 struct pldm_msg *msg,
1469 size_t payload_length)
1470{
1471 if (msg == NULL) {
1472 return PLDM_ERROR_INVALID_DATA;
1473 }
1474
1475 if (payload_length != PLDM_CANCEL_UPDATE_COMPONENT_REQ_BYTES) {
1476 return PLDM_ERROR_INVALID_LENGTH;
1477 }
1478
1479 struct pldm_header_info header = {0};
1480 header.instance = instance_id;
1481 header.msg_type = PLDM_REQUEST;
1482 header.pldm_type = PLDM_FWUP;
1483 header.command = PLDM_CANCEL_UPDATE_COMPONENT;
1484 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1485 if (rc) {
1486 return rc;
1487 }
1488
1489 return PLDM_SUCCESS;
1490}
1491
1492int decode_cancel_update_component_resp(const struct pldm_msg *msg,
1493 size_t payload_length,
1494 uint8_t *completion_code)
1495{
1496 if (msg == NULL || completion_code == NULL) {
1497 return PLDM_ERROR_INVALID_DATA;
1498 }
1499
1500 if (payload_length != sizeof(*completion_code)) {
1501 return PLDM_ERROR_INVALID_LENGTH;
1502 }
1503
1504 *completion_code = msg->payload[0];
1505 return PLDM_SUCCESS;
1506}
1507
1508int encode_cancel_update_req(uint8_t instance_id, struct pldm_msg *msg,
1509 size_t payload_length)
1510{
1511 if (msg == NULL) {
1512 return PLDM_ERROR_INVALID_DATA;
1513 }
1514
1515 if (payload_length != PLDM_CANCEL_UPDATE_REQ_BYTES) {
1516 return PLDM_ERROR_INVALID_LENGTH;
1517 }
1518
1519 struct pldm_header_info header = {0};
1520 header.instance = instance_id;
1521 header.msg_type = PLDM_REQUEST;
1522 header.pldm_type = PLDM_FWUP;
1523 header.command = PLDM_CANCEL_UPDATE;
1524 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1525 if (rc) {
1526 return rc;
1527 }
1528
1529 return PLDM_SUCCESS;
1530}
1531
1532int decode_cancel_update_resp(const struct pldm_msg *msg, size_t payload_length,
1533 uint8_t *completion_code,
1534 bool8_t *non_functioning_component_indication,
1535 bitfield64_t *non_functioning_component_bitmap)
1536{
1537 if (msg == NULL || completion_code == NULL ||
1538 non_functioning_component_indication == NULL ||
1539 non_functioning_component_bitmap == NULL || !payload_length) {
1540 return PLDM_ERROR_INVALID_DATA;
1541 }
1542
1543 *completion_code = msg->payload[0];
1544 if (*completion_code != PLDM_SUCCESS) {
1545 return PLDM_SUCCESS;
1546 }
1547
1548 if (payload_length != sizeof(struct pldm_cancel_update_resp)) {
1549 return PLDM_ERROR_INVALID_LENGTH;
1550 }
1551 struct pldm_cancel_update_resp *response =
1552 (struct pldm_cancel_update_resp *)msg->payload;
1553
1554 if (!is_non_functioning_component_indication_valid(
1555 response->non_functioning_component_indication)) {
1556 return PLDM_ERROR_INVALID_DATA;
1557 }
1558
1559 *non_functioning_component_indication =
1560 response->non_functioning_component_indication;
1561
1562 if (*non_functioning_component_indication) {
1563 non_functioning_component_bitmap->value =
1564 le64toh(response->non_functioning_component_bitmap);
1565 }
1566
1567 return PLDM_SUCCESS;
1568}