blob: 023fc4f64675c6e7905da2a41de7b9f2f19519d1 [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);
339 memcpy(package_header_info->timestamp104, data_header->timestamp104,
340 sizeof(data_header->timestamp104));
341 package_header_info->component_bitmap_bit_length =
342 le16toh(data_header->component_bitmap_bit_length);
343 package_header_info->package_version_string_type =
344 data_header->package_version_string_type;
345 package_header_info->package_version_string_length =
346 data_header->package_version_string_length;
347 package_version_str->ptr =
348 data + sizeof(struct pldm_package_header_information);
349 package_version_str->length =
350 package_header_info->package_version_string_length;
351
352 return PLDM_SUCCESS;
353}
354
355int decode_firmware_device_id_record(
356 const uint8_t *data, size_t length, uint16_t component_bitmap_bit_length,
357 struct pldm_firmware_device_id_record *fw_device_id_record,
358 struct variable_field *applicable_components,
359 struct variable_field *comp_image_set_version_str,
360 struct variable_field *record_descriptors,
361 struct variable_field *fw_device_pkg_data)
362{
363 if (data == NULL || fw_device_id_record == NULL ||
364 applicable_components == NULL ||
365 comp_image_set_version_str == NULL || record_descriptors == NULL ||
366 fw_device_pkg_data == NULL) {
367 return PLDM_ERROR_INVALID_DATA;
368 }
369
370 if (length < sizeof(struct pldm_firmware_device_id_record)) {
371 return PLDM_ERROR_INVALID_LENGTH;
372 }
373
374 if ((component_bitmap_bit_length %
375 PLDM_FWUP_COMPONENT_BITMAP_MULTIPLE) != 0) {
376 return PLDM_ERROR_INVALID_DATA;
377 }
378
379 struct pldm_firmware_device_id_record *data_record =
380 (struct pldm_firmware_device_id_record *)(data);
381
382 if (!is_string_type_valid(
383 data_record->comp_image_set_version_string_type) ||
384 (data_record->comp_image_set_version_string_length == 0)) {
385 return PLDM_ERROR_INVALID_DATA;
386 }
387
388 fw_device_id_record->record_length =
389 le16toh(data_record->record_length);
390 fw_device_id_record->descriptor_count = data_record->descriptor_count;
391 fw_device_id_record->device_update_option_flags.value =
392 le32toh(data_record->device_update_option_flags.value);
393 fw_device_id_record->comp_image_set_version_string_type =
394 data_record->comp_image_set_version_string_type;
395 fw_device_id_record->comp_image_set_version_string_length =
396 data_record->comp_image_set_version_string_length;
397 fw_device_id_record->fw_device_pkg_data_length =
398 le16toh(data_record->fw_device_pkg_data_length);
399
400 if (length < fw_device_id_record->record_length) {
401 return PLDM_ERROR_INVALID_LENGTH;
402 }
403
404 uint16_t applicable_components_length =
405 component_bitmap_bit_length / PLDM_FWUP_COMPONENT_BITMAP_MULTIPLE;
406 uint16_t calc_min_record_length =
407 sizeof(struct pldm_firmware_device_id_record) +
408 applicable_components_length +
409 data_record->comp_image_set_version_string_length +
410 PLDM_FWUP_DEVICE_DESCRIPTOR_MIN_LEN +
411 fw_device_id_record->fw_device_pkg_data_length;
412
413 if (fw_device_id_record->record_length < calc_min_record_length) {
414 return PLDM_ERROR_INVALID_LENGTH;
415 }
416
417 applicable_components->ptr =
418 data + sizeof(struct pldm_firmware_device_id_record);
419 applicable_components->length = applicable_components_length;
420
421 comp_image_set_version_str->ptr =
422 applicable_components->ptr + applicable_components->length;
423 comp_image_set_version_str->length =
424 fw_device_id_record->comp_image_set_version_string_length;
425
426 record_descriptors->ptr = comp_image_set_version_str->ptr +
427 comp_image_set_version_str->length;
428 record_descriptors->length =
429 fw_device_id_record->record_length -
430 sizeof(struct pldm_firmware_device_id_record) -
431 applicable_components_length -
432 fw_device_id_record->comp_image_set_version_string_length -
433 fw_device_id_record->fw_device_pkg_data_length;
434
435 if (fw_device_id_record->fw_device_pkg_data_length) {
436 fw_device_pkg_data->ptr =
437 record_descriptors->ptr + record_descriptors->length;
438 fw_device_pkg_data->length =
439 fw_device_id_record->fw_device_pkg_data_length;
440 }
441
442 return PLDM_SUCCESS;
443}
444
445int decode_descriptor_type_length_value(const uint8_t *data, size_t length,
446 uint16_t *descriptor_type,
447 struct variable_field *descriptor_data)
448{
449 uint16_t descriptor_length = 0;
450
451 if (data == NULL || descriptor_type == NULL ||
452 descriptor_data == NULL) {
453 return PLDM_ERROR_INVALID_DATA;
454 }
455
456 if (length < PLDM_FWUP_DEVICE_DESCRIPTOR_MIN_LEN) {
457 return PLDM_ERROR_INVALID_LENGTH;
458 }
459
460 struct pldm_descriptor_tlv *entry =
461 (struct pldm_descriptor_tlv *)(data);
462
463 *descriptor_type = le16toh(entry->descriptor_type);
464 descriptor_length = le16toh(entry->descriptor_length);
465 if (*descriptor_type != PLDM_FWUP_VENDOR_DEFINED) {
466 if (descriptor_length !=
467 get_descriptor_type_length(*descriptor_type)) {
468 return PLDM_ERROR_INVALID_LENGTH;
469 }
470 }
471
472 if (length < (sizeof(*descriptor_type) + sizeof(descriptor_length) +
473 descriptor_length)) {
474 return PLDM_ERROR_INVALID_LENGTH;
475 }
476
477 descriptor_data->ptr = entry->descriptor_data;
478 descriptor_data->length = descriptor_length;
479
480 return PLDM_SUCCESS;
481}
482
483int decode_vendor_defined_descriptor_value(
484 const uint8_t *data, size_t length, uint8_t *descriptor_title_str_type,
485 struct variable_field *descriptor_title_str,
486 struct variable_field *descriptor_data)
487{
488 if (data == NULL || descriptor_title_str_type == NULL ||
489 descriptor_title_str == NULL || descriptor_data == NULL) {
490 return PLDM_ERROR_INVALID_DATA;
491 }
492
493 if (length < sizeof(struct pldm_vendor_defined_descriptor_title_data)) {
494 return PLDM_ERROR_INVALID_LENGTH;
495 }
496
497 struct pldm_vendor_defined_descriptor_title_data *entry =
498 (struct pldm_vendor_defined_descriptor_title_data *)(data);
499 if (!is_string_type_valid(
500 entry->vendor_defined_descriptor_title_str_type) ||
501 (entry->vendor_defined_descriptor_title_str_len == 0)) {
502 return PLDM_ERROR_INVALID_DATA;
503 }
504
505 // Assuming atleast 1 byte of VendorDefinedDescriptorData
506 if (length < (sizeof(struct pldm_vendor_defined_descriptor_title_data) +
507 entry->vendor_defined_descriptor_title_str_len)) {
508 return PLDM_ERROR_INVALID_LENGTH;
509 }
510
511 *descriptor_title_str_type =
512 entry->vendor_defined_descriptor_title_str_type;
513 descriptor_title_str->ptr = entry->vendor_defined_descriptor_title_str;
514 descriptor_title_str->length =
515 entry->vendor_defined_descriptor_title_str_len;
516
517 descriptor_data->ptr =
518 descriptor_title_str->ptr + descriptor_title_str->length;
519 descriptor_data->length =
520 length - sizeof(entry->vendor_defined_descriptor_title_str_type) -
521 sizeof(entry->vendor_defined_descriptor_title_str_len) -
522 descriptor_title_str->length;
523
524 return PLDM_SUCCESS;
525}
526
527int decode_pldm_comp_image_info(
528 const uint8_t *data, size_t length,
529 struct pldm_component_image_information *pldm_comp_image_info,
530 struct variable_field *comp_version_str)
531{
532 if (data == NULL || pldm_comp_image_info == NULL ||
533 comp_version_str == NULL) {
534 return PLDM_ERROR_INVALID_DATA;
535 }
536
537 if (length < sizeof(struct pldm_component_image_information)) {
538 return PLDM_ERROR_INVALID_LENGTH;
539 }
540
541 struct pldm_component_image_information *data_header =
542 (struct pldm_component_image_information *)(data);
543
544 if (!is_string_type_valid(data_header->comp_version_string_type) ||
545 (data_header->comp_version_string_length == 0)) {
546 return PLDM_ERROR_INVALID_DATA;
547 }
548
549 if (length < sizeof(struct pldm_component_image_information) +
550 data_header->comp_version_string_length) {
551 return PLDM_ERROR_INVALID_LENGTH;
552 }
553
554 pldm_comp_image_info->comp_classification =
555 le16toh(data_header->comp_classification);
556 pldm_comp_image_info->comp_identifier =
557 le16toh(data_header->comp_identifier);
558 pldm_comp_image_info->comp_comparison_stamp =
559 le32toh(data_header->comp_comparison_stamp);
560 pldm_comp_image_info->comp_options.value =
561 le16toh(data_header->comp_options.value);
562 pldm_comp_image_info->requested_comp_activation_method.value =
563 le16toh(data_header->requested_comp_activation_method.value);
564 pldm_comp_image_info->comp_location_offset =
565 le32toh(data_header->comp_location_offset);
566 pldm_comp_image_info->comp_size = le32toh(data_header->comp_size);
567 pldm_comp_image_info->comp_version_string_type =
568 data_header->comp_version_string_type;
569 pldm_comp_image_info->comp_version_string_length =
570 data_header->comp_version_string_length;
571
572 if ((pldm_comp_image_info->comp_options.bits.bit1 == false &&
573 pldm_comp_image_info->comp_comparison_stamp !=
574 PLDM_FWUP_INVALID_COMPONENT_COMPARISON_TIMESTAMP)) {
575 return PLDM_ERROR_INVALID_DATA;
576 }
577
578 if (pldm_comp_image_info->comp_location_offset == 0 ||
579 pldm_comp_image_info->comp_size == 0) {
580 return PLDM_ERROR_INVALID_DATA;
581 }
582
583 comp_version_str->ptr =
584 data + sizeof(struct pldm_component_image_information);
585 comp_version_str->length =
586 pldm_comp_image_info->comp_version_string_length;
587
588 return PLDM_SUCCESS;
589}
590
591int encode_query_device_identifiers_req(uint8_t instance_id,
592 size_t payload_length,
593 struct pldm_msg *msg)
594{
595 if (msg == NULL) {
596 return PLDM_ERROR_INVALID_DATA;
597 }
598
599 if (payload_length != PLDM_QUERY_DEVICE_IDENTIFIERS_REQ_BYTES) {
600 return PLDM_ERROR_INVALID_LENGTH;
601 }
602
603 return encode_pldm_header_only(PLDM_REQUEST, instance_id, PLDM_FWUP,
604 PLDM_QUERY_DEVICE_IDENTIFIERS, msg);
605}
606
607int decode_query_device_identifiers_resp(const struct pldm_msg *msg,
608 size_t payload_length,
609 uint8_t *completion_code,
610 uint32_t *device_identifiers_len,
611 uint8_t *descriptor_count,
612 uint8_t **descriptor_data)
613{
614 if (msg == NULL || completion_code == NULL ||
615 device_identifiers_len == NULL || descriptor_count == NULL ||
616 descriptor_data == NULL) {
617 return PLDM_ERROR_INVALID_DATA;
618 }
619
620 *completion_code = msg->payload[0];
621 if (PLDM_SUCCESS != *completion_code) {
622 return PLDM_SUCCESS;
623 }
624
625 if (payload_length <
626 sizeof(struct pldm_query_device_identifiers_resp)) {
627 return PLDM_ERROR_INVALID_LENGTH;
628 }
629
630 struct pldm_query_device_identifiers_resp *response =
631 (struct pldm_query_device_identifiers_resp *)msg->payload;
632 *device_identifiers_len = le32toh(response->device_identifiers_len);
633
634 if (*device_identifiers_len < PLDM_FWUP_DEVICE_DESCRIPTOR_MIN_LEN) {
635 return PLDM_ERROR_INVALID_LENGTH;
636 }
637
638 if (payload_length !=
639 sizeof(struct pldm_query_device_identifiers_resp) +
640 *device_identifiers_len) {
641 return PLDM_ERROR_INVALID_LENGTH;
642 }
643 *descriptor_count = response->descriptor_count;
644
645 if (*descriptor_count == 0) {
646 return PLDM_ERROR_INVALID_DATA;
647 }
648 *descriptor_data =
649 (uint8_t *)(msg->payload +
650 sizeof(struct pldm_query_device_identifiers_resp));
651 return PLDM_SUCCESS;
652}
653
654int encode_get_firmware_parameters_req(uint8_t instance_id,
655 size_t payload_length,
656 struct pldm_msg *msg)
657{
658 if (msg == NULL) {
659 return PLDM_ERROR_INVALID_DATA;
660 }
661
662 if (payload_length != PLDM_GET_FIRMWARE_PARAMETERS_REQ_BYTES) {
663 return PLDM_ERROR_INVALID_LENGTH;
664 }
665
666 return encode_pldm_header_only(PLDM_REQUEST, instance_id, PLDM_FWUP,
667 PLDM_GET_FIRMWARE_PARAMETERS, msg);
668}
669
670int decode_get_firmware_parameters_resp(
671 const struct pldm_msg *msg, size_t payload_length,
672 struct pldm_get_firmware_parameters_resp *resp_data,
673 struct variable_field *active_comp_image_set_ver_str,
674 struct variable_field *pending_comp_image_set_ver_str,
675 struct variable_field *comp_parameter_table)
676{
677 if (msg == NULL || resp_data == NULL ||
678 active_comp_image_set_ver_str == NULL ||
679 pending_comp_image_set_ver_str == NULL ||
680 comp_parameter_table == NULL || !payload_length) {
681 return PLDM_ERROR_INVALID_DATA;
682 }
683
684 resp_data->completion_code = msg->payload[0];
685 if (PLDM_SUCCESS != resp_data->completion_code) {
686 return PLDM_SUCCESS;
687 }
688
689 if (payload_length < sizeof(struct pldm_get_firmware_parameters_resp)) {
690 return PLDM_ERROR_INVALID_LENGTH;
691 }
692
693 struct pldm_get_firmware_parameters_resp *response =
694 (struct pldm_get_firmware_parameters_resp *)msg->payload;
695
696 if (!is_string_type_valid(
697 response->active_comp_image_set_ver_str_type) ||
698 (response->active_comp_image_set_ver_str_len == 0)) {
699 return PLDM_ERROR_INVALID_DATA;
700 }
701
702 if (response->pending_comp_image_set_ver_str_len == 0) {
703 if (response->pending_comp_image_set_ver_str_type !=
704 PLDM_STR_TYPE_UNKNOWN) {
705 return PLDM_ERROR_INVALID_DATA;
706 }
707 } else {
708 if (!is_string_type_valid(
709 response->pending_comp_image_set_ver_str_type)) {
710 return PLDM_ERROR_INVALID_DATA;
711 }
712 }
713
714 size_t partial_response_length =
715 sizeof(struct pldm_get_firmware_parameters_resp) +
716 response->active_comp_image_set_ver_str_len +
717 response->pending_comp_image_set_ver_str_len;
718
719 if (payload_length < partial_response_length) {
720 return PLDM_ERROR_INVALID_LENGTH;
721 }
722
723 resp_data->capabilities_during_update.value =
724 le32toh(response->capabilities_during_update.value);
725 resp_data->comp_count = le16toh(response->comp_count);
726 resp_data->active_comp_image_set_ver_str_type =
727 response->active_comp_image_set_ver_str_type;
728 resp_data->active_comp_image_set_ver_str_len =
729 response->active_comp_image_set_ver_str_len;
730 resp_data->pending_comp_image_set_ver_str_type =
731 response->pending_comp_image_set_ver_str_type;
732 resp_data->pending_comp_image_set_ver_str_len =
733 response->pending_comp_image_set_ver_str_len;
734
735 active_comp_image_set_ver_str->ptr =
736 msg->payload + sizeof(struct pldm_get_firmware_parameters_resp);
737 active_comp_image_set_ver_str->length =
738 resp_data->active_comp_image_set_ver_str_len;
739
740 if (resp_data->pending_comp_image_set_ver_str_len != 0) {
741 pending_comp_image_set_ver_str->ptr =
742 msg->payload +
743 sizeof(struct pldm_get_firmware_parameters_resp) +
744 resp_data->active_comp_image_set_ver_str_len;
745 pending_comp_image_set_ver_str->length =
746 resp_data->pending_comp_image_set_ver_str_len;
747 } else {
748 pending_comp_image_set_ver_str->ptr = NULL;
749 pending_comp_image_set_ver_str->length = 0;
750 }
751
752 if (payload_length > partial_response_length && resp_data->comp_count) {
753 comp_parameter_table->ptr =
754 msg->payload +
755 sizeof(struct pldm_get_firmware_parameters_resp) +
756 resp_data->active_comp_image_set_ver_str_len +
757 resp_data->pending_comp_image_set_ver_str_len;
758 comp_parameter_table->length =
759 payload_length - partial_response_length;
760 } else {
761 comp_parameter_table->ptr = NULL;
762 comp_parameter_table->length = 0;
763 }
764
765 return PLDM_SUCCESS;
766}
767
768int decode_get_firmware_parameters_resp_comp_entry(
769 const uint8_t *data, size_t length,
770 struct pldm_component_parameter_entry *component_data,
771 struct variable_field *active_comp_ver_str,
772 struct variable_field *pending_comp_ver_str)
773{
774 if (data == NULL || component_data == NULL ||
775 active_comp_ver_str == NULL || pending_comp_ver_str == NULL) {
776 return PLDM_ERROR_INVALID_DATA;
777 }
778
779 if (length < sizeof(struct pldm_component_parameter_entry)) {
780 return PLDM_ERROR_INVALID_LENGTH;
781 }
782
783 struct pldm_component_parameter_entry *entry =
784 (struct pldm_component_parameter_entry *)(data);
Patrick Williams70a47ba2021-09-02 09:53:31 -0500785
786 size_t entry_length = sizeof(struct pldm_component_parameter_entry) +
787 entry->active_comp_ver_str_len +
788 entry->pending_comp_ver_str_len;
789
790 if (length < entry_length) {
791 return PLDM_ERROR_INVALID_LENGTH;
792 }
793
794 component_data->comp_classification =
795 le16toh(entry->comp_classification);
796 component_data->comp_identifier = le16toh(entry->comp_identifier);
797 component_data->comp_classification_index =
798 entry->comp_classification_index;
799 component_data->active_comp_comparison_stamp =
800 le32toh(entry->active_comp_comparison_stamp);
801 component_data->active_comp_ver_str_type =
802 entry->active_comp_ver_str_type;
803 component_data->active_comp_ver_str_len =
804 entry->active_comp_ver_str_len;
805 memcpy(component_data->active_comp_release_date,
806 entry->active_comp_release_date,
807 sizeof(entry->active_comp_release_date));
808 component_data->pending_comp_comparison_stamp =
809 le32toh(entry->pending_comp_comparison_stamp);
810 component_data->pending_comp_ver_str_type =
811 entry->pending_comp_ver_str_type;
812 component_data->pending_comp_ver_str_len =
813 entry->pending_comp_ver_str_len;
814 memcpy(component_data->pending_comp_release_date,
815 entry->pending_comp_release_date,
816 sizeof(entry->pending_comp_release_date));
817 component_data->comp_activation_methods.value =
818 le16toh(entry->comp_activation_methods.value);
819 component_data->capabilities_during_update.value =
820 le32toh(entry->capabilities_during_update.value);
821
Tom Joseph3a6846c2021-09-08 17:34:29 +0530822 if (entry->active_comp_ver_str_len != 0) {
823 active_comp_ver_str->ptr =
824 data + sizeof(struct pldm_component_parameter_entry);
825 active_comp_ver_str->length = entry->active_comp_ver_str_len;
826 } else {
827 active_comp_ver_str->ptr = NULL;
828 active_comp_ver_str->length = 0;
829 }
Patrick Williams70a47ba2021-09-02 09:53:31 -0500830
831 if (entry->pending_comp_ver_str_len != 0) {
832
833 pending_comp_ver_str->ptr =
834 data + sizeof(struct pldm_component_parameter_entry) +
835 entry->active_comp_ver_str_len;
836 pending_comp_ver_str->length = entry->pending_comp_ver_str_len;
837 } else {
838 pending_comp_ver_str->ptr = NULL;
839 pending_comp_ver_str->length = 0;
840 }
841 return PLDM_SUCCESS;
842}
843
844int encode_request_update_req(uint8_t instance_id, uint32_t max_transfer_size,
845 uint16_t num_of_comp,
846 uint8_t max_outstanding_transfer_req,
847 uint16_t pkg_data_len,
848 uint8_t comp_image_set_ver_str_type,
849 uint8_t comp_image_set_ver_str_len,
850 const struct variable_field *comp_img_set_ver_str,
851 struct pldm_msg *msg, size_t payload_length)
852{
853 if (comp_img_set_ver_str == NULL || comp_img_set_ver_str->ptr == NULL ||
854 msg == NULL) {
855 return PLDM_ERROR_INVALID_DATA;
856 }
857
858 if (payload_length != sizeof(struct pldm_request_update_req) +
859 comp_img_set_ver_str->length) {
860 return PLDM_ERROR_INVALID_LENGTH;
861 }
862
863 if ((comp_image_set_ver_str_len == 0) ||
864 (comp_image_set_ver_str_len != comp_img_set_ver_str->length)) {
865 return PLDM_ERROR_INVALID_DATA;
866 }
867
868 if ((max_transfer_size < PLDM_FWUP_BASELINE_TRANSFER_SIZE) ||
869 (max_outstanding_transfer_req < PLDM_FWUP_MIN_OUTSTANDING_REQ)) {
870 return PLDM_ERROR_INVALID_DATA;
871 }
872
873 if (!is_string_type_valid(comp_image_set_ver_str_type)) {
874 return PLDM_ERROR_INVALID_DATA;
875 }
876
877 struct pldm_header_info header = {0};
878 header.instance = instance_id;
879 header.msg_type = PLDM_REQUEST;
880 header.pldm_type = PLDM_FWUP;
881 header.command = PLDM_REQUEST_UPDATE;
882 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
883 if (rc) {
884 return rc;
885 }
886
887 struct pldm_request_update_req *request =
888 (struct pldm_request_update_req *)msg->payload;
889
890 request->max_transfer_size = htole32(max_transfer_size);
891 request->num_of_comp = htole16(num_of_comp);
892 request->max_outstanding_transfer_req = max_outstanding_transfer_req;
893 request->pkg_data_len = htole16(pkg_data_len);
894 request->comp_image_set_ver_str_type = comp_image_set_ver_str_type;
895 request->comp_image_set_ver_str_len = comp_image_set_ver_str_len;
896
897 memcpy(msg->payload + sizeof(struct pldm_request_update_req),
898 comp_img_set_ver_str->ptr, comp_img_set_ver_str->length);
899
900 return PLDM_SUCCESS;
901}
902
903int decode_request_update_resp(const struct pldm_msg *msg,
904 size_t payload_length, uint8_t *completion_code,
905 uint16_t *fd_meta_data_len,
906 uint8_t *fd_will_send_pkg_data)
907{
908 if (msg == NULL || completion_code == NULL ||
909 fd_meta_data_len == NULL || fd_will_send_pkg_data == NULL ||
910 !payload_length) {
911 return PLDM_ERROR_INVALID_DATA;
912 }
913
914 *completion_code = msg->payload[0];
915 if (*completion_code != PLDM_SUCCESS) {
916 return PLDM_SUCCESS;
917 }
918
919 if (payload_length != sizeof(struct pldm_request_update_resp)) {
920 return PLDM_ERROR_INVALID_LENGTH;
921 }
922
923 struct pldm_request_update_resp *response =
924 (struct pldm_request_update_resp *)msg->payload;
925
926 *fd_meta_data_len = le16toh(response->fd_meta_data_len);
927 *fd_will_send_pkg_data = response->fd_will_send_pkg_data;
928
929 return PLDM_SUCCESS;
930}
931
932int encode_pass_component_table_req(
933 uint8_t instance_id, uint8_t transfer_flag, uint16_t comp_classification,
934 uint16_t comp_identifier, uint8_t comp_classification_index,
935 uint32_t comp_comparison_stamp, uint8_t comp_ver_str_type,
936 uint8_t comp_ver_str_len, const struct variable_field *comp_ver_str,
937 struct pldm_msg *msg, size_t payload_length)
938{
939 if (comp_ver_str == NULL || comp_ver_str->ptr == NULL || msg == NULL) {
940 return PLDM_ERROR_INVALID_DATA;
941 }
942
943 if (payload_length != sizeof(struct pldm_pass_component_table_req) +
944 comp_ver_str->length) {
945 return PLDM_ERROR_INVALID_LENGTH;
946 }
947
948 if ((comp_ver_str_len == 0) ||
949 (comp_ver_str_len != comp_ver_str->length)) {
950 return PLDM_ERROR_INVALID_DATA;
951 }
952
953 if (!is_transfer_flag_valid(transfer_flag)) {
954 return PLDM_INVALID_TRANSFER_OPERATION_FLAG;
955 }
956
957 if (!is_string_type_valid(comp_ver_str_type)) {
958 return PLDM_ERROR_INVALID_DATA;
959 }
960
961 struct pldm_header_info header = {0};
962 header.instance = instance_id;
963 header.msg_type = PLDM_REQUEST;
964 header.pldm_type = PLDM_FWUP;
965 header.command = PLDM_PASS_COMPONENT_TABLE;
966 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
967 if (rc) {
968 return rc;
969 }
970
971 struct pldm_pass_component_table_req *request =
972 (struct pldm_pass_component_table_req *)msg->payload;
973
974 request->transfer_flag = transfer_flag;
975 request->comp_classification = htole16(comp_classification);
976 request->comp_identifier = htole16(comp_identifier);
977 request->comp_classification_index = comp_classification_index;
978 request->comp_comparison_stamp = htole32(comp_comparison_stamp);
979 request->comp_ver_str_type = comp_ver_str_type;
980 request->comp_ver_str_len = comp_ver_str_len;
981
982 memcpy(msg->payload + sizeof(struct pldm_pass_component_table_req),
983 comp_ver_str->ptr, comp_ver_str->length);
984
985 return PLDM_SUCCESS;
986}
987
988int decode_pass_component_table_resp(const struct pldm_msg *msg,
989 const size_t payload_length,
990 uint8_t *completion_code,
991 uint8_t *comp_resp,
992 uint8_t *comp_resp_code)
993{
994 if (msg == NULL || completion_code == NULL || comp_resp == NULL ||
995 comp_resp_code == NULL || !payload_length) {
996 return PLDM_ERROR_INVALID_DATA;
997 }
998
999 *completion_code = msg->payload[0];
1000 if (*completion_code != PLDM_SUCCESS) {
1001 return PLDM_SUCCESS;
1002 }
1003
1004 if (payload_length != sizeof(struct pldm_pass_component_table_resp)) {
1005 return PLDM_ERROR_INVALID_LENGTH;
1006 }
1007
1008 struct pldm_pass_component_table_resp *response =
1009 (struct pldm_pass_component_table_resp *)msg->payload;
1010
1011 if (!is_comp_resp_valid(response->comp_resp)) {
1012 return PLDM_ERROR_INVALID_DATA;
1013 }
1014
1015 if (!is_comp_resp_code_valid(response->comp_resp_code)) {
1016 return PLDM_ERROR_INVALID_DATA;
1017 }
1018
1019 *comp_resp = response->comp_resp;
1020 *comp_resp_code = response->comp_resp_code;
1021
1022 return PLDM_SUCCESS;
1023}
1024
1025int encode_update_component_req(
1026 uint8_t instance_id, uint16_t comp_classification, uint16_t comp_identifier,
1027 uint8_t comp_classification_index, uint32_t comp_comparison_stamp,
1028 uint32_t comp_image_size, bitfield32_t update_option_flags,
1029 uint8_t comp_ver_str_type, uint8_t comp_ver_str_len,
1030 const struct variable_field *comp_ver_str, struct pldm_msg *msg,
1031 size_t payload_length)
1032{
1033 if (comp_ver_str == NULL || comp_ver_str->ptr == NULL || msg == NULL) {
1034 return PLDM_ERROR_INVALID_DATA;
1035 }
1036
1037 if (payload_length !=
1038 sizeof(struct pldm_update_component_req) + comp_ver_str->length) {
1039 return PLDM_ERROR_INVALID_LENGTH;
1040 }
1041
1042 if (!comp_image_size) {
1043 return PLDM_ERROR_INVALID_DATA;
1044 }
1045
1046 if ((comp_ver_str_len == 0) ||
1047 (comp_ver_str_len != comp_ver_str->length)) {
1048 return PLDM_ERROR_INVALID_DATA;
1049 }
1050
1051 if (!is_string_type_valid(comp_ver_str_type)) {
1052 return PLDM_ERROR_INVALID_DATA;
1053 }
1054
1055 struct pldm_header_info header = {0};
1056 header.instance = instance_id;
1057 header.msg_type = PLDM_REQUEST;
1058 header.pldm_type = PLDM_FWUP;
1059 header.command = PLDM_UPDATE_COMPONENT;
1060 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1061 if (rc) {
1062 return rc;
1063 }
1064
1065 struct pldm_update_component_req *request =
1066 (struct pldm_update_component_req *)msg->payload;
1067
1068 request->comp_classification = htole16(comp_classification);
1069 request->comp_identifier = htole16(comp_identifier);
1070 request->comp_classification_index = comp_classification_index;
1071 request->comp_comparison_stamp = htole32(comp_comparison_stamp);
1072 request->comp_image_size = htole32(comp_image_size);
1073 request->update_option_flags.value = htole32(update_option_flags.value);
1074 request->comp_ver_str_type = comp_ver_str_type;
1075 request->comp_ver_str_len = comp_ver_str_len;
1076
1077 memcpy(msg->payload + sizeof(struct pldm_update_component_req),
1078 comp_ver_str->ptr, comp_ver_str->length);
1079
1080 return PLDM_SUCCESS;
1081}
1082
1083int decode_update_component_resp(const struct pldm_msg *msg,
1084 size_t payload_length,
1085 uint8_t *completion_code,
1086 uint8_t *comp_compatability_resp,
1087 uint8_t *comp_compatability_resp_code,
1088 bitfield32_t *update_option_flags_enabled,
1089 uint16_t *time_before_req_fw_data)
1090{
1091 if (msg == NULL || completion_code == NULL ||
1092 comp_compatability_resp == NULL ||
1093 comp_compatability_resp_code == NULL ||
1094 update_option_flags_enabled == NULL ||
1095 time_before_req_fw_data == NULL || !payload_length) {
1096 return PLDM_ERROR_INVALID_DATA;
1097 }
1098
1099 *completion_code = msg->payload[0];
1100 if (*completion_code != PLDM_SUCCESS) {
1101 return PLDM_SUCCESS;
1102 }
1103
1104 if (payload_length != sizeof(struct pldm_update_component_resp)) {
1105 return PLDM_ERROR_INVALID_LENGTH;
1106 }
1107
1108 struct pldm_update_component_resp *response =
1109 (struct pldm_update_component_resp *)msg->payload;
1110
1111 if (!is_comp_compatibility_resp_valid(
1112 response->comp_compatability_resp)) {
1113 return PLDM_ERROR_INVALID_DATA;
1114 }
1115
1116 if (!is_comp_compatibility_resp_code_valid(
1117 response->comp_compatability_resp_code)) {
1118 return PLDM_ERROR_INVALID_DATA;
1119 }
1120
1121 *comp_compatability_resp = response->comp_compatability_resp;
1122 *comp_compatability_resp_code = response->comp_compatability_resp_code;
1123 update_option_flags_enabled->value =
1124 le32toh(response->update_option_flags_enabled.value);
1125 *time_before_req_fw_data = le16toh(response->time_before_req_fw_data);
1126
1127 return PLDM_SUCCESS;
1128}
1129
1130int decode_request_firmware_data_req(const struct pldm_msg *msg,
1131 size_t payload_length, uint32_t *offset,
1132 uint32_t *length)
1133{
1134 if (msg == NULL || offset == NULL || length == NULL) {
1135 return PLDM_ERROR_INVALID_DATA;
1136 }
1137 if (payload_length != sizeof(struct pldm_request_firmware_data_req)) {
1138 return PLDM_ERROR_INVALID_LENGTH;
1139 }
1140 struct pldm_request_firmware_data_req *request =
1141 (struct pldm_request_firmware_data_req *)msg->payload;
1142 *offset = le32toh(request->offset);
1143 *length = le32toh(request->length);
1144
1145 if (*length < PLDM_FWUP_BASELINE_TRANSFER_SIZE) {
1146 return PLDM_FWUP_INVALID_TRANSFER_LENGTH;
1147 }
1148
1149 return PLDM_SUCCESS;
1150}
1151
1152int encode_request_firmware_data_resp(uint8_t instance_id,
1153 uint8_t completion_code,
1154 struct pldm_msg *msg,
1155 size_t payload_length)
1156{
1157 if (msg == NULL || !payload_length) {
1158 return PLDM_ERROR_INVALID_DATA;
1159 }
1160
1161 struct pldm_header_info header = {0};
1162 header.instance = instance_id;
1163 header.msg_type = PLDM_RESPONSE;
1164 header.pldm_type = PLDM_FWUP;
1165 header.command = PLDM_REQUEST_FIRMWARE_DATA;
1166 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1167 if (rc) {
1168 return rc;
1169 }
1170
1171 msg->payload[0] = completion_code;
1172
1173 return PLDM_SUCCESS;
1174}
1175
1176int decode_transfer_complete_req(const struct pldm_msg *msg,
1177 size_t payload_length,
1178 uint8_t *transfer_result)
1179{
1180 if (msg == NULL || transfer_result == NULL) {
1181 return PLDM_ERROR_INVALID_DATA;
1182 }
1183
1184 if (payload_length != sizeof(*transfer_result)) {
1185 return PLDM_ERROR_INVALID_LENGTH;
1186 }
1187
1188 *transfer_result = msg->payload[0];
1189 return PLDM_SUCCESS;
1190}
1191
1192int encode_transfer_complete_resp(uint8_t instance_id, uint8_t completion_code,
1193 struct pldm_msg *msg, size_t payload_length)
1194{
1195 if (msg == NULL) {
1196 return PLDM_ERROR_INVALID_DATA;
1197 }
1198
1199 if (payload_length != sizeof(completion_code)) {
1200 return PLDM_ERROR_INVALID_LENGTH;
1201 }
1202
1203 struct pldm_header_info header = {0};
1204 header.instance = instance_id;
1205 header.msg_type = PLDM_RESPONSE;
1206 header.pldm_type = PLDM_FWUP;
1207 header.command = PLDM_TRANSFER_COMPLETE;
1208 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1209 if (rc) {
1210 return rc;
1211 }
1212
1213 msg->payload[0] = completion_code;
1214
1215 return PLDM_SUCCESS;
1216}
1217
1218int decode_verify_complete_req(const struct pldm_msg *msg,
1219 size_t payload_length, uint8_t *verify_result)
1220{
1221 if (msg == NULL || verify_result == NULL) {
1222 return PLDM_ERROR_INVALID_DATA;
1223 }
1224
1225 if (payload_length != sizeof(*verify_result)) {
1226 return PLDM_ERROR_INVALID_LENGTH;
1227 }
1228
1229 *verify_result = msg->payload[0];
1230 return PLDM_SUCCESS;
1231}
1232
1233int encode_verify_complete_resp(uint8_t instance_id, uint8_t completion_code,
1234 struct pldm_msg *msg, size_t payload_length)
1235{
1236 if (msg == NULL) {
1237 return PLDM_ERROR_INVALID_DATA;
1238 }
1239
1240 if (payload_length != sizeof(completion_code)) {
1241 return PLDM_ERROR_INVALID_LENGTH;
1242 }
1243
1244 struct pldm_header_info header = {0};
1245 header.instance = instance_id;
1246 header.msg_type = PLDM_RESPONSE;
1247 header.pldm_type = PLDM_FWUP;
1248 header.command = PLDM_VERIFY_COMPLETE;
1249 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1250 if (rc) {
1251 return rc;
1252 }
1253
1254 msg->payload[0] = completion_code;
1255
1256 return PLDM_SUCCESS;
1257}
1258
1259int decode_apply_complete_req(
1260 const struct pldm_msg *msg, size_t payload_length, uint8_t *apply_result,
1261 bitfield16_t *comp_activation_methods_modification)
1262{
1263 if (msg == NULL || apply_result == NULL ||
1264 comp_activation_methods_modification == NULL) {
1265 return PLDM_ERROR_INVALID_DATA;
1266 }
1267
1268 if (payload_length != sizeof(struct pldm_apply_complete_req)) {
1269 return PLDM_ERROR_INVALID_LENGTH;
1270 }
1271
1272 struct pldm_apply_complete_req *request =
1273 (struct pldm_apply_complete_req *)msg->payload;
1274
1275 *apply_result = request->apply_result;
1276 comp_activation_methods_modification->value =
1277 le16toh(request->comp_activation_methods_modification.value);
1278
1279 if ((*apply_result != PLDM_FWUP_APPLY_SUCCESS_WITH_ACTIVATION_METHOD) &&
1280 comp_activation_methods_modification->value) {
1281 return PLDM_ERROR_INVALID_DATA;
1282 }
1283
1284 return PLDM_SUCCESS;
1285}
1286
1287int encode_apply_complete_resp(uint8_t instance_id, uint8_t completion_code,
1288 struct pldm_msg *msg, size_t payload_length)
1289{
1290 if (msg == NULL) {
1291 return PLDM_ERROR_INVALID_DATA;
1292 }
1293
1294 if (payload_length != sizeof(completion_code)) {
1295 return PLDM_ERROR_INVALID_LENGTH;
1296 }
1297
1298 struct pldm_header_info header = {0};
1299 header.instance = instance_id;
1300 header.msg_type = PLDM_RESPONSE;
1301 header.pldm_type = PLDM_FWUP;
1302 header.command = PLDM_APPLY_COMPLETE;
1303 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1304 if (rc) {
1305 return rc;
1306 }
1307
1308 msg->payload[0] = completion_code;
1309
1310 return PLDM_SUCCESS;
1311}
1312
1313int encode_activate_firmware_req(uint8_t instance_id,
1314 bool8_t self_contained_activation_req,
1315 struct pldm_msg *msg, size_t payload_length)
1316{
1317 if (msg == NULL) {
1318 return PLDM_ERROR_INVALID_DATA;
1319 }
1320
1321 if (payload_length != sizeof(struct pldm_activate_firmware_req)) {
1322 return PLDM_ERROR_INVALID_LENGTH;
1323 }
1324
1325 if (!is_self_contained_activation_req_valid(
1326 self_contained_activation_req)) {
1327 return PLDM_ERROR_INVALID_DATA;
1328 }
1329
1330 struct pldm_header_info header = {0};
1331 header.instance = instance_id;
1332 header.msg_type = PLDM_REQUEST;
1333 header.pldm_type = PLDM_FWUP;
1334 header.command = PLDM_ACTIVATE_FIRMWARE;
1335 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1336 if (rc) {
1337 return rc;
1338 }
1339
1340 struct pldm_activate_firmware_req *request =
1341 (struct pldm_activate_firmware_req *)msg->payload;
1342
1343 request->self_contained_activation_req = self_contained_activation_req;
1344
1345 return PLDM_SUCCESS;
1346}
1347
1348int decode_activate_firmware_resp(const struct pldm_msg *msg,
1349 size_t payload_length,
1350 uint8_t *completion_code,
1351 uint16_t *estimated_time_activation)
1352{
1353 if (msg == NULL || completion_code == NULL ||
1354 estimated_time_activation == NULL || !payload_length) {
1355 return PLDM_ERROR_INVALID_DATA;
1356 }
1357
1358 *completion_code = msg->payload[0];
1359 if (*completion_code != PLDM_SUCCESS) {
1360 return PLDM_SUCCESS;
1361 }
1362
1363 if (payload_length != sizeof(struct pldm_activate_firmware_resp)) {
1364 return PLDM_ERROR_INVALID_LENGTH;
1365 }
1366
1367 struct pldm_activate_firmware_resp *response =
1368 (struct pldm_activate_firmware_resp *)msg->payload;
1369
1370 *estimated_time_activation =
1371 le16toh(response->estimated_time_activation);
1372
1373 return PLDM_SUCCESS;
1374}
1375
1376int encode_get_status_req(uint8_t instance_id, struct pldm_msg *msg,
1377 size_t payload_length)
1378{
1379 if (msg == NULL) {
1380 return PLDM_ERROR_INVALID_DATA;
1381 }
1382
1383 if (payload_length != PLDM_GET_STATUS_REQ_BYTES) {
1384 return PLDM_ERROR_INVALID_LENGTH;
1385 }
1386
1387 struct pldm_header_info header = {0};
1388 header.instance = instance_id;
1389 header.msg_type = PLDM_REQUEST;
1390 header.pldm_type = PLDM_FWUP;
1391 header.command = PLDM_GET_STATUS;
1392 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1393 if (rc) {
1394 return rc;
1395 }
1396
1397 return PLDM_SUCCESS;
1398}
1399
1400int decode_get_status_resp(const struct pldm_msg *msg, size_t payload_length,
1401 uint8_t *completion_code, uint8_t *current_state,
1402 uint8_t *previous_state, uint8_t *aux_state,
1403 uint8_t *aux_state_status, uint8_t *progress_percent,
1404 uint8_t *reason_code,
1405 bitfield32_t *update_option_flags_enabled)
1406{
1407 if (msg == NULL || completion_code == NULL || current_state == NULL ||
1408 previous_state == NULL || aux_state == NULL ||
1409 aux_state_status == NULL || progress_percent == NULL ||
1410 reason_code == NULL || update_option_flags_enabled == NULL ||
1411 !payload_length) {
1412 return PLDM_ERROR_INVALID_DATA;
1413 }
1414
1415 *completion_code = msg->payload[0];
1416 if (*completion_code != PLDM_SUCCESS) {
1417 return PLDM_SUCCESS;
1418 }
1419
1420 if (payload_length != sizeof(struct pldm_get_status_resp)) {
1421 return PLDM_ERROR_INVALID_LENGTH;
1422 }
1423 struct pldm_get_status_resp *response =
1424 (struct pldm_get_status_resp *)msg->payload;
1425
1426 if (!is_state_valid(response->current_state)) {
1427 return PLDM_ERROR_INVALID_DATA;
1428 }
1429 if (!is_state_valid(response->previous_state)) {
1430 return PLDM_ERROR_INVALID_DATA;
1431 }
1432 if (!is_aux_state_valid(response->aux_state)) {
1433 return PLDM_ERROR_INVALID_DATA;
1434 }
1435 if (!is_aux_state_status_valid(response->aux_state_status)) {
1436 return PLDM_ERROR_INVALID_DATA;
1437 }
1438 if (response->progress_percent > PLDM_FWUP_MAX_PROGRESS_PERCENT) {
1439 return PLDM_ERROR_INVALID_DATA;
1440 }
1441 if (!is_reason_code_valid(response->reason_code)) {
1442 return PLDM_ERROR_INVALID_DATA;
1443 }
1444
1445 if ((response->current_state == PLDM_FD_STATE_IDLE) ||
1446 (response->current_state == PLDM_FD_STATE_LEARN_COMPONENTS) ||
1447 (response->current_state == PLDM_FD_STATE_READY_XFER)) {
1448 if (response->aux_state !=
1449 PLDM_FD_IDLE_LEARN_COMPONENTS_READ_XFER) {
1450 return PLDM_ERROR_INVALID_DATA;
1451 }
1452 }
1453
1454 *current_state = response->current_state;
1455 *previous_state = response->previous_state;
1456 *aux_state = response->aux_state;
1457 *aux_state_status = response->aux_state_status;
1458 *progress_percent = response->progress_percent;
1459 *reason_code = response->reason_code;
1460 update_option_flags_enabled->value =
1461 le32toh(response->update_option_flags_enabled.value);
1462
1463 return PLDM_SUCCESS;
1464}
1465
1466int encode_cancel_update_component_req(uint8_t instance_id,
1467 struct pldm_msg *msg,
1468 size_t payload_length)
1469{
1470 if (msg == NULL) {
1471 return PLDM_ERROR_INVALID_DATA;
1472 }
1473
1474 if (payload_length != PLDM_CANCEL_UPDATE_COMPONENT_REQ_BYTES) {
1475 return PLDM_ERROR_INVALID_LENGTH;
1476 }
1477
1478 struct pldm_header_info header = {0};
1479 header.instance = instance_id;
1480 header.msg_type = PLDM_REQUEST;
1481 header.pldm_type = PLDM_FWUP;
1482 header.command = PLDM_CANCEL_UPDATE_COMPONENT;
1483 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1484 if (rc) {
1485 return rc;
1486 }
1487
1488 return PLDM_SUCCESS;
1489}
1490
1491int decode_cancel_update_component_resp(const struct pldm_msg *msg,
1492 size_t payload_length,
1493 uint8_t *completion_code)
1494{
1495 if (msg == NULL || completion_code == NULL) {
1496 return PLDM_ERROR_INVALID_DATA;
1497 }
1498
1499 if (payload_length != sizeof(*completion_code)) {
1500 return PLDM_ERROR_INVALID_LENGTH;
1501 }
1502
1503 *completion_code = msg->payload[0];
1504 return PLDM_SUCCESS;
1505}
1506
1507int encode_cancel_update_req(uint8_t instance_id, struct pldm_msg *msg,
1508 size_t payload_length)
1509{
1510 if (msg == NULL) {
1511 return PLDM_ERROR_INVALID_DATA;
1512 }
1513
1514 if (payload_length != PLDM_CANCEL_UPDATE_REQ_BYTES) {
1515 return PLDM_ERROR_INVALID_LENGTH;
1516 }
1517
1518 struct pldm_header_info header = {0};
1519 header.instance = instance_id;
1520 header.msg_type = PLDM_REQUEST;
1521 header.pldm_type = PLDM_FWUP;
1522 header.command = PLDM_CANCEL_UPDATE;
1523 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1524 if (rc) {
1525 return rc;
1526 }
1527
1528 return PLDM_SUCCESS;
1529}
1530
1531int decode_cancel_update_resp(const struct pldm_msg *msg, size_t payload_length,
1532 uint8_t *completion_code,
1533 bool8_t *non_functioning_component_indication,
1534 bitfield64_t *non_functioning_component_bitmap)
1535{
1536 if (msg == NULL || completion_code == NULL ||
1537 non_functioning_component_indication == NULL ||
1538 non_functioning_component_bitmap == NULL || !payload_length) {
1539 return PLDM_ERROR_INVALID_DATA;
1540 }
1541
1542 *completion_code = msg->payload[0];
1543 if (*completion_code != PLDM_SUCCESS) {
1544 return PLDM_SUCCESS;
1545 }
1546
1547 if (payload_length != sizeof(struct pldm_cancel_update_resp)) {
1548 return PLDM_ERROR_INVALID_LENGTH;
1549 }
1550 struct pldm_cancel_update_resp *response =
1551 (struct pldm_cancel_update_resp *)msg->payload;
1552
1553 if (!is_non_functioning_component_indication_valid(
1554 response->non_functioning_component_indication)) {
1555 return PLDM_ERROR_INVALID_DATA;
1556 }
1557
1558 *non_functioning_component_indication =
1559 response->non_functioning_component_indication;
1560
1561 if (*non_functioning_component_indication) {
1562 non_functioning_component_bitmap->value =
1563 le64toh(response->non_functioning_component_bitmap);
1564 }
1565
1566 return PLDM_SUCCESS;
1567}