blob: 1aa0ed1757ad16800c9d428deefacdc61bafa3b6 [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);
785 if (entry->active_comp_ver_str_len == 0) {
786 return PLDM_ERROR_INVALID_LENGTH;
787 }
788
789 size_t entry_length = sizeof(struct pldm_component_parameter_entry) +
790 entry->active_comp_ver_str_len +
791 entry->pending_comp_ver_str_len;
792
793 if (length < entry_length) {
794 return PLDM_ERROR_INVALID_LENGTH;
795 }
796
797 component_data->comp_classification =
798 le16toh(entry->comp_classification);
799 component_data->comp_identifier = le16toh(entry->comp_identifier);
800 component_data->comp_classification_index =
801 entry->comp_classification_index;
802 component_data->active_comp_comparison_stamp =
803 le32toh(entry->active_comp_comparison_stamp);
804 component_data->active_comp_ver_str_type =
805 entry->active_comp_ver_str_type;
806 component_data->active_comp_ver_str_len =
807 entry->active_comp_ver_str_len;
808 memcpy(component_data->active_comp_release_date,
809 entry->active_comp_release_date,
810 sizeof(entry->active_comp_release_date));
811 component_data->pending_comp_comparison_stamp =
812 le32toh(entry->pending_comp_comparison_stamp);
813 component_data->pending_comp_ver_str_type =
814 entry->pending_comp_ver_str_type;
815 component_data->pending_comp_ver_str_len =
816 entry->pending_comp_ver_str_len;
817 memcpy(component_data->pending_comp_release_date,
818 entry->pending_comp_release_date,
819 sizeof(entry->pending_comp_release_date));
820 component_data->comp_activation_methods.value =
821 le16toh(entry->comp_activation_methods.value);
822 component_data->capabilities_during_update.value =
823 le32toh(entry->capabilities_during_update.value);
824
825 active_comp_ver_str->ptr =
826 data + sizeof(struct pldm_component_parameter_entry);
827 active_comp_ver_str->length = entry->active_comp_ver_str_len;
828
829 if (entry->pending_comp_ver_str_len != 0) {
830
831 pending_comp_ver_str->ptr =
832 data + sizeof(struct pldm_component_parameter_entry) +
833 entry->active_comp_ver_str_len;
834 pending_comp_ver_str->length = entry->pending_comp_ver_str_len;
835 } else {
836 pending_comp_ver_str->ptr = NULL;
837 pending_comp_ver_str->length = 0;
838 }
839 return PLDM_SUCCESS;
840}
841
842int encode_request_update_req(uint8_t instance_id, uint32_t max_transfer_size,
843 uint16_t num_of_comp,
844 uint8_t max_outstanding_transfer_req,
845 uint16_t pkg_data_len,
846 uint8_t comp_image_set_ver_str_type,
847 uint8_t comp_image_set_ver_str_len,
848 const struct variable_field *comp_img_set_ver_str,
849 struct pldm_msg *msg, size_t payload_length)
850{
851 if (comp_img_set_ver_str == NULL || comp_img_set_ver_str->ptr == NULL ||
852 msg == NULL) {
853 return PLDM_ERROR_INVALID_DATA;
854 }
855
856 if (payload_length != sizeof(struct pldm_request_update_req) +
857 comp_img_set_ver_str->length) {
858 return PLDM_ERROR_INVALID_LENGTH;
859 }
860
861 if ((comp_image_set_ver_str_len == 0) ||
862 (comp_image_set_ver_str_len != comp_img_set_ver_str->length)) {
863 return PLDM_ERROR_INVALID_DATA;
864 }
865
866 if ((max_transfer_size < PLDM_FWUP_BASELINE_TRANSFER_SIZE) ||
867 (max_outstanding_transfer_req < PLDM_FWUP_MIN_OUTSTANDING_REQ)) {
868 return PLDM_ERROR_INVALID_DATA;
869 }
870
871 if (!is_string_type_valid(comp_image_set_ver_str_type)) {
872 return PLDM_ERROR_INVALID_DATA;
873 }
874
875 struct pldm_header_info header = {0};
876 header.instance = instance_id;
877 header.msg_type = PLDM_REQUEST;
878 header.pldm_type = PLDM_FWUP;
879 header.command = PLDM_REQUEST_UPDATE;
880 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
881 if (rc) {
882 return rc;
883 }
884
885 struct pldm_request_update_req *request =
886 (struct pldm_request_update_req *)msg->payload;
887
888 request->max_transfer_size = htole32(max_transfer_size);
889 request->num_of_comp = htole16(num_of_comp);
890 request->max_outstanding_transfer_req = max_outstanding_transfer_req;
891 request->pkg_data_len = htole16(pkg_data_len);
892 request->comp_image_set_ver_str_type = comp_image_set_ver_str_type;
893 request->comp_image_set_ver_str_len = comp_image_set_ver_str_len;
894
895 memcpy(msg->payload + sizeof(struct pldm_request_update_req),
896 comp_img_set_ver_str->ptr, comp_img_set_ver_str->length);
897
898 return PLDM_SUCCESS;
899}
900
901int decode_request_update_resp(const struct pldm_msg *msg,
902 size_t payload_length, uint8_t *completion_code,
903 uint16_t *fd_meta_data_len,
904 uint8_t *fd_will_send_pkg_data)
905{
906 if (msg == NULL || completion_code == NULL ||
907 fd_meta_data_len == NULL || fd_will_send_pkg_data == NULL ||
908 !payload_length) {
909 return PLDM_ERROR_INVALID_DATA;
910 }
911
912 *completion_code = msg->payload[0];
913 if (*completion_code != PLDM_SUCCESS) {
914 return PLDM_SUCCESS;
915 }
916
917 if (payload_length != sizeof(struct pldm_request_update_resp)) {
918 return PLDM_ERROR_INVALID_LENGTH;
919 }
920
921 struct pldm_request_update_resp *response =
922 (struct pldm_request_update_resp *)msg->payload;
923
924 *fd_meta_data_len = le16toh(response->fd_meta_data_len);
925 *fd_will_send_pkg_data = response->fd_will_send_pkg_data;
926
927 return PLDM_SUCCESS;
928}
929
930int encode_pass_component_table_req(
931 uint8_t instance_id, uint8_t transfer_flag, uint16_t comp_classification,
932 uint16_t comp_identifier, uint8_t comp_classification_index,
933 uint32_t comp_comparison_stamp, uint8_t comp_ver_str_type,
934 uint8_t comp_ver_str_len, const struct variable_field *comp_ver_str,
935 struct pldm_msg *msg, size_t payload_length)
936{
937 if (comp_ver_str == NULL || comp_ver_str->ptr == NULL || msg == NULL) {
938 return PLDM_ERROR_INVALID_DATA;
939 }
940
941 if (payload_length != sizeof(struct pldm_pass_component_table_req) +
942 comp_ver_str->length) {
943 return PLDM_ERROR_INVALID_LENGTH;
944 }
945
946 if ((comp_ver_str_len == 0) ||
947 (comp_ver_str_len != comp_ver_str->length)) {
948 return PLDM_ERROR_INVALID_DATA;
949 }
950
951 if (!is_transfer_flag_valid(transfer_flag)) {
952 return PLDM_INVALID_TRANSFER_OPERATION_FLAG;
953 }
954
955 if (!is_string_type_valid(comp_ver_str_type)) {
956 return PLDM_ERROR_INVALID_DATA;
957 }
958
959 struct pldm_header_info header = {0};
960 header.instance = instance_id;
961 header.msg_type = PLDM_REQUEST;
962 header.pldm_type = PLDM_FWUP;
963 header.command = PLDM_PASS_COMPONENT_TABLE;
964 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
965 if (rc) {
966 return rc;
967 }
968
969 struct pldm_pass_component_table_req *request =
970 (struct pldm_pass_component_table_req *)msg->payload;
971
972 request->transfer_flag = transfer_flag;
973 request->comp_classification = htole16(comp_classification);
974 request->comp_identifier = htole16(comp_identifier);
975 request->comp_classification_index = comp_classification_index;
976 request->comp_comparison_stamp = htole32(comp_comparison_stamp);
977 request->comp_ver_str_type = comp_ver_str_type;
978 request->comp_ver_str_len = comp_ver_str_len;
979
980 memcpy(msg->payload + sizeof(struct pldm_pass_component_table_req),
981 comp_ver_str->ptr, comp_ver_str->length);
982
983 return PLDM_SUCCESS;
984}
985
986int decode_pass_component_table_resp(const struct pldm_msg *msg,
987 const size_t payload_length,
988 uint8_t *completion_code,
989 uint8_t *comp_resp,
990 uint8_t *comp_resp_code)
991{
992 if (msg == NULL || completion_code == NULL || comp_resp == NULL ||
993 comp_resp_code == NULL || !payload_length) {
994 return PLDM_ERROR_INVALID_DATA;
995 }
996
997 *completion_code = msg->payload[0];
998 if (*completion_code != PLDM_SUCCESS) {
999 return PLDM_SUCCESS;
1000 }
1001
1002 if (payload_length != sizeof(struct pldm_pass_component_table_resp)) {
1003 return PLDM_ERROR_INVALID_LENGTH;
1004 }
1005
1006 struct pldm_pass_component_table_resp *response =
1007 (struct pldm_pass_component_table_resp *)msg->payload;
1008
1009 if (!is_comp_resp_valid(response->comp_resp)) {
1010 return PLDM_ERROR_INVALID_DATA;
1011 }
1012
1013 if (!is_comp_resp_code_valid(response->comp_resp_code)) {
1014 return PLDM_ERROR_INVALID_DATA;
1015 }
1016
1017 *comp_resp = response->comp_resp;
1018 *comp_resp_code = response->comp_resp_code;
1019
1020 return PLDM_SUCCESS;
1021}
1022
1023int encode_update_component_req(
1024 uint8_t instance_id, uint16_t comp_classification, uint16_t comp_identifier,
1025 uint8_t comp_classification_index, uint32_t comp_comparison_stamp,
1026 uint32_t comp_image_size, bitfield32_t update_option_flags,
1027 uint8_t comp_ver_str_type, uint8_t comp_ver_str_len,
1028 const struct variable_field *comp_ver_str, struct pldm_msg *msg,
1029 size_t payload_length)
1030{
1031 if (comp_ver_str == NULL || comp_ver_str->ptr == NULL || msg == NULL) {
1032 return PLDM_ERROR_INVALID_DATA;
1033 }
1034
1035 if (payload_length !=
1036 sizeof(struct pldm_update_component_req) + comp_ver_str->length) {
1037 return PLDM_ERROR_INVALID_LENGTH;
1038 }
1039
1040 if (!comp_image_size) {
1041 return PLDM_ERROR_INVALID_DATA;
1042 }
1043
1044 if ((comp_ver_str_len == 0) ||
1045 (comp_ver_str_len != comp_ver_str->length)) {
1046 return PLDM_ERROR_INVALID_DATA;
1047 }
1048
1049 if (!is_string_type_valid(comp_ver_str_type)) {
1050 return PLDM_ERROR_INVALID_DATA;
1051 }
1052
1053 struct pldm_header_info header = {0};
1054 header.instance = instance_id;
1055 header.msg_type = PLDM_REQUEST;
1056 header.pldm_type = PLDM_FWUP;
1057 header.command = PLDM_UPDATE_COMPONENT;
1058 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1059 if (rc) {
1060 return rc;
1061 }
1062
1063 struct pldm_update_component_req *request =
1064 (struct pldm_update_component_req *)msg->payload;
1065
1066 request->comp_classification = htole16(comp_classification);
1067 request->comp_identifier = htole16(comp_identifier);
1068 request->comp_classification_index = comp_classification_index;
1069 request->comp_comparison_stamp = htole32(comp_comparison_stamp);
1070 request->comp_image_size = htole32(comp_image_size);
1071 request->update_option_flags.value = htole32(update_option_flags.value);
1072 request->comp_ver_str_type = comp_ver_str_type;
1073 request->comp_ver_str_len = comp_ver_str_len;
1074
1075 memcpy(msg->payload + sizeof(struct pldm_update_component_req),
1076 comp_ver_str->ptr, comp_ver_str->length);
1077
1078 return PLDM_SUCCESS;
1079}
1080
1081int decode_update_component_resp(const struct pldm_msg *msg,
1082 size_t payload_length,
1083 uint8_t *completion_code,
1084 uint8_t *comp_compatability_resp,
1085 uint8_t *comp_compatability_resp_code,
1086 bitfield32_t *update_option_flags_enabled,
1087 uint16_t *time_before_req_fw_data)
1088{
1089 if (msg == NULL || completion_code == NULL ||
1090 comp_compatability_resp == NULL ||
1091 comp_compatability_resp_code == NULL ||
1092 update_option_flags_enabled == NULL ||
1093 time_before_req_fw_data == NULL || !payload_length) {
1094 return PLDM_ERROR_INVALID_DATA;
1095 }
1096
1097 *completion_code = msg->payload[0];
1098 if (*completion_code != PLDM_SUCCESS) {
1099 return PLDM_SUCCESS;
1100 }
1101
1102 if (payload_length != sizeof(struct pldm_update_component_resp)) {
1103 return PLDM_ERROR_INVALID_LENGTH;
1104 }
1105
1106 struct pldm_update_component_resp *response =
1107 (struct pldm_update_component_resp *)msg->payload;
1108
1109 if (!is_comp_compatibility_resp_valid(
1110 response->comp_compatability_resp)) {
1111 return PLDM_ERROR_INVALID_DATA;
1112 }
1113
1114 if (!is_comp_compatibility_resp_code_valid(
1115 response->comp_compatability_resp_code)) {
1116 return PLDM_ERROR_INVALID_DATA;
1117 }
1118
1119 *comp_compatability_resp = response->comp_compatability_resp;
1120 *comp_compatability_resp_code = response->comp_compatability_resp_code;
1121 update_option_flags_enabled->value =
1122 le32toh(response->update_option_flags_enabled.value);
1123 *time_before_req_fw_data = le16toh(response->time_before_req_fw_data);
1124
1125 return PLDM_SUCCESS;
1126}
1127
1128int decode_request_firmware_data_req(const struct pldm_msg *msg,
1129 size_t payload_length, uint32_t *offset,
1130 uint32_t *length)
1131{
1132 if (msg == NULL || offset == NULL || length == NULL) {
1133 return PLDM_ERROR_INVALID_DATA;
1134 }
1135 if (payload_length != sizeof(struct pldm_request_firmware_data_req)) {
1136 return PLDM_ERROR_INVALID_LENGTH;
1137 }
1138 struct pldm_request_firmware_data_req *request =
1139 (struct pldm_request_firmware_data_req *)msg->payload;
1140 *offset = le32toh(request->offset);
1141 *length = le32toh(request->length);
1142
1143 if (*length < PLDM_FWUP_BASELINE_TRANSFER_SIZE) {
1144 return PLDM_FWUP_INVALID_TRANSFER_LENGTH;
1145 }
1146
1147 return PLDM_SUCCESS;
1148}
1149
1150int encode_request_firmware_data_resp(uint8_t instance_id,
1151 uint8_t completion_code,
1152 struct pldm_msg *msg,
1153 size_t payload_length)
1154{
1155 if (msg == NULL || !payload_length) {
1156 return PLDM_ERROR_INVALID_DATA;
1157 }
1158
1159 struct pldm_header_info header = {0};
1160 header.instance = instance_id;
1161 header.msg_type = PLDM_RESPONSE;
1162 header.pldm_type = PLDM_FWUP;
1163 header.command = PLDM_REQUEST_FIRMWARE_DATA;
1164 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1165 if (rc) {
1166 return rc;
1167 }
1168
1169 msg->payload[0] = completion_code;
1170
1171 return PLDM_SUCCESS;
1172}
1173
1174int decode_transfer_complete_req(const struct pldm_msg *msg,
1175 size_t payload_length,
1176 uint8_t *transfer_result)
1177{
1178 if (msg == NULL || transfer_result == NULL) {
1179 return PLDM_ERROR_INVALID_DATA;
1180 }
1181
1182 if (payload_length != sizeof(*transfer_result)) {
1183 return PLDM_ERROR_INVALID_LENGTH;
1184 }
1185
1186 *transfer_result = msg->payload[0];
1187 return PLDM_SUCCESS;
1188}
1189
1190int encode_transfer_complete_resp(uint8_t instance_id, uint8_t completion_code,
1191 struct pldm_msg *msg, size_t payload_length)
1192{
1193 if (msg == NULL) {
1194 return PLDM_ERROR_INVALID_DATA;
1195 }
1196
1197 if (payload_length != sizeof(completion_code)) {
1198 return PLDM_ERROR_INVALID_LENGTH;
1199 }
1200
1201 struct pldm_header_info header = {0};
1202 header.instance = instance_id;
1203 header.msg_type = PLDM_RESPONSE;
1204 header.pldm_type = PLDM_FWUP;
1205 header.command = PLDM_TRANSFER_COMPLETE;
1206 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1207 if (rc) {
1208 return rc;
1209 }
1210
1211 msg->payload[0] = completion_code;
1212
1213 return PLDM_SUCCESS;
1214}
1215
1216int decode_verify_complete_req(const struct pldm_msg *msg,
1217 size_t payload_length, uint8_t *verify_result)
1218{
1219 if (msg == NULL || verify_result == NULL) {
1220 return PLDM_ERROR_INVALID_DATA;
1221 }
1222
1223 if (payload_length != sizeof(*verify_result)) {
1224 return PLDM_ERROR_INVALID_LENGTH;
1225 }
1226
1227 *verify_result = msg->payload[0];
1228 return PLDM_SUCCESS;
1229}
1230
1231int encode_verify_complete_resp(uint8_t instance_id, uint8_t completion_code,
1232 struct pldm_msg *msg, size_t payload_length)
1233{
1234 if (msg == NULL) {
1235 return PLDM_ERROR_INVALID_DATA;
1236 }
1237
1238 if (payload_length != sizeof(completion_code)) {
1239 return PLDM_ERROR_INVALID_LENGTH;
1240 }
1241
1242 struct pldm_header_info header = {0};
1243 header.instance = instance_id;
1244 header.msg_type = PLDM_RESPONSE;
1245 header.pldm_type = PLDM_FWUP;
1246 header.command = PLDM_VERIFY_COMPLETE;
1247 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1248 if (rc) {
1249 return rc;
1250 }
1251
1252 msg->payload[0] = completion_code;
1253
1254 return PLDM_SUCCESS;
1255}
1256
1257int decode_apply_complete_req(
1258 const struct pldm_msg *msg, size_t payload_length, uint8_t *apply_result,
1259 bitfield16_t *comp_activation_methods_modification)
1260{
1261 if (msg == NULL || apply_result == NULL ||
1262 comp_activation_methods_modification == NULL) {
1263 return PLDM_ERROR_INVALID_DATA;
1264 }
1265
1266 if (payload_length != sizeof(struct pldm_apply_complete_req)) {
1267 return PLDM_ERROR_INVALID_LENGTH;
1268 }
1269
1270 struct pldm_apply_complete_req *request =
1271 (struct pldm_apply_complete_req *)msg->payload;
1272
1273 *apply_result = request->apply_result;
1274 comp_activation_methods_modification->value =
1275 le16toh(request->comp_activation_methods_modification.value);
1276
1277 if ((*apply_result != PLDM_FWUP_APPLY_SUCCESS_WITH_ACTIVATION_METHOD) &&
1278 comp_activation_methods_modification->value) {
1279 return PLDM_ERROR_INVALID_DATA;
1280 }
1281
1282 return PLDM_SUCCESS;
1283}
1284
1285int encode_apply_complete_resp(uint8_t instance_id, uint8_t completion_code,
1286 struct pldm_msg *msg, size_t payload_length)
1287{
1288 if (msg == NULL) {
1289 return PLDM_ERROR_INVALID_DATA;
1290 }
1291
1292 if (payload_length != sizeof(completion_code)) {
1293 return PLDM_ERROR_INVALID_LENGTH;
1294 }
1295
1296 struct pldm_header_info header = {0};
1297 header.instance = instance_id;
1298 header.msg_type = PLDM_RESPONSE;
1299 header.pldm_type = PLDM_FWUP;
1300 header.command = PLDM_APPLY_COMPLETE;
1301 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1302 if (rc) {
1303 return rc;
1304 }
1305
1306 msg->payload[0] = completion_code;
1307
1308 return PLDM_SUCCESS;
1309}
1310
1311int encode_activate_firmware_req(uint8_t instance_id,
1312 bool8_t self_contained_activation_req,
1313 struct pldm_msg *msg, size_t payload_length)
1314{
1315 if (msg == NULL) {
1316 return PLDM_ERROR_INVALID_DATA;
1317 }
1318
1319 if (payload_length != sizeof(struct pldm_activate_firmware_req)) {
1320 return PLDM_ERROR_INVALID_LENGTH;
1321 }
1322
1323 if (!is_self_contained_activation_req_valid(
1324 self_contained_activation_req)) {
1325 return PLDM_ERROR_INVALID_DATA;
1326 }
1327
1328 struct pldm_header_info header = {0};
1329 header.instance = instance_id;
1330 header.msg_type = PLDM_REQUEST;
1331 header.pldm_type = PLDM_FWUP;
1332 header.command = PLDM_ACTIVATE_FIRMWARE;
1333 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1334 if (rc) {
1335 return rc;
1336 }
1337
1338 struct pldm_activate_firmware_req *request =
1339 (struct pldm_activate_firmware_req *)msg->payload;
1340
1341 request->self_contained_activation_req = self_contained_activation_req;
1342
1343 return PLDM_SUCCESS;
1344}
1345
1346int decode_activate_firmware_resp(const struct pldm_msg *msg,
1347 size_t payload_length,
1348 uint8_t *completion_code,
1349 uint16_t *estimated_time_activation)
1350{
1351 if (msg == NULL || completion_code == NULL ||
1352 estimated_time_activation == NULL || !payload_length) {
1353 return PLDM_ERROR_INVALID_DATA;
1354 }
1355
1356 *completion_code = msg->payload[0];
1357 if (*completion_code != PLDM_SUCCESS) {
1358 return PLDM_SUCCESS;
1359 }
1360
1361 if (payload_length != sizeof(struct pldm_activate_firmware_resp)) {
1362 return PLDM_ERROR_INVALID_LENGTH;
1363 }
1364
1365 struct pldm_activate_firmware_resp *response =
1366 (struct pldm_activate_firmware_resp *)msg->payload;
1367
1368 *estimated_time_activation =
1369 le16toh(response->estimated_time_activation);
1370
1371 return PLDM_SUCCESS;
1372}
1373
1374int encode_get_status_req(uint8_t instance_id, struct pldm_msg *msg,
1375 size_t payload_length)
1376{
1377 if (msg == NULL) {
1378 return PLDM_ERROR_INVALID_DATA;
1379 }
1380
1381 if (payload_length != PLDM_GET_STATUS_REQ_BYTES) {
1382 return PLDM_ERROR_INVALID_LENGTH;
1383 }
1384
1385 struct pldm_header_info header = {0};
1386 header.instance = instance_id;
1387 header.msg_type = PLDM_REQUEST;
1388 header.pldm_type = PLDM_FWUP;
1389 header.command = PLDM_GET_STATUS;
1390 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1391 if (rc) {
1392 return rc;
1393 }
1394
1395 return PLDM_SUCCESS;
1396}
1397
1398int decode_get_status_resp(const struct pldm_msg *msg, size_t payload_length,
1399 uint8_t *completion_code, uint8_t *current_state,
1400 uint8_t *previous_state, uint8_t *aux_state,
1401 uint8_t *aux_state_status, uint8_t *progress_percent,
1402 uint8_t *reason_code,
1403 bitfield32_t *update_option_flags_enabled)
1404{
1405 if (msg == NULL || completion_code == NULL || current_state == NULL ||
1406 previous_state == NULL || aux_state == NULL ||
1407 aux_state_status == NULL || progress_percent == NULL ||
1408 reason_code == NULL || update_option_flags_enabled == NULL ||
1409 !payload_length) {
1410 return PLDM_ERROR_INVALID_DATA;
1411 }
1412
1413 *completion_code = msg->payload[0];
1414 if (*completion_code != PLDM_SUCCESS) {
1415 return PLDM_SUCCESS;
1416 }
1417
1418 if (payload_length != sizeof(struct pldm_get_status_resp)) {
1419 return PLDM_ERROR_INVALID_LENGTH;
1420 }
1421 struct pldm_get_status_resp *response =
1422 (struct pldm_get_status_resp *)msg->payload;
1423
1424 if (!is_state_valid(response->current_state)) {
1425 return PLDM_ERROR_INVALID_DATA;
1426 }
1427 if (!is_state_valid(response->previous_state)) {
1428 return PLDM_ERROR_INVALID_DATA;
1429 }
1430 if (!is_aux_state_valid(response->aux_state)) {
1431 return PLDM_ERROR_INVALID_DATA;
1432 }
1433 if (!is_aux_state_status_valid(response->aux_state_status)) {
1434 return PLDM_ERROR_INVALID_DATA;
1435 }
1436 if (response->progress_percent > PLDM_FWUP_MAX_PROGRESS_PERCENT) {
1437 return PLDM_ERROR_INVALID_DATA;
1438 }
1439 if (!is_reason_code_valid(response->reason_code)) {
1440 return PLDM_ERROR_INVALID_DATA;
1441 }
1442
1443 if ((response->current_state == PLDM_FD_STATE_IDLE) ||
1444 (response->current_state == PLDM_FD_STATE_LEARN_COMPONENTS) ||
1445 (response->current_state == PLDM_FD_STATE_READY_XFER)) {
1446 if (response->aux_state !=
1447 PLDM_FD_IDLE_LEARN_COMPONENTS_READ_XFER) {
1448 return PLDM_ERROR_INVALID_DATA;
1449 }
1450 }
1451
1452 *current_state = response->current_state;
1453 *previous_state = response->previous_state;
1454 *aux_state = response->aux_state;
1455 *aux_state_status = response->aux_state_status;
1456 *progress_percent = response->progress_percent;
1457 *reason_code = response->reason_code;
1458 update_option_flags_enabled->value =
1459 le32toh(response->update_option_flags_enabled.value);
1460
1461 return PLDM_SUCCESS;
1462}
1463
1464int encode_cancel_update_component_req(uint8_t instance_id,
1465 struct pldm_msg *msg,
1466 size_t payload_length)
1467{
1468 if (msg == NULL) {
1469 return PLDM_ERROR_INVALID_DATA;
1470 }
1471
1472 if (payload_length != PLDM_CANCEL_UPDATE_COMPONENT_REQ_BYTES) {
1473 return PLDM_ERROR_INVALID_LENGTH;
1474 }
1475
1476 struct pldm_header_info header = {0};
1477 header.instance = instance_id;
1478 header.msg_type = PLDM_REQUEST;
1479 header.pldm_type = PLDM_FWUP;
1480 header.command = PLDM_CANCEL_UPDATE_COMPONENT;
1481 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1482 if (rc) {
1483 return rc;
1484 }
1485
1486 return PLDM_SUCCESS;
1487}
1488
1489int decode_cancel_update_component_resp(const struct pldm_msg *msg,
1490 size_t payload_length,
1491 uint8_t *completion_code)
1492{
1493 if (msg == NULL || completion_code == NULL) {
1494 return PLDM_ERROR_INVALID_DATA;
1495 }
1496
1497 if (payload_length != sizeof(*completion_code)) {
1498 return PLDM_ERROR_INVALID_LENGTH;
1499 }
1500
1501 *completion_code = msg->payload[0];
1502 return PLDM_SUCCESS;
1503}
1504
1505int encode_cancel_update_req(uint8_t instance_id, struct pldm_msg *msg,
1506 size_t payload_length)
1507{
1508 if (msg == NULL) {
1509 return PLDM_ERROR_INVALID_DATA;
1510 }
1511
1512 if (payload_length != PLDM_CANCEL_UPDATE_REQ_BYTES) {
1513 return PLDM_ERROR_INVALID_LENGTH;
1514 }
1515
1516 struct pldm_header_info header = {0};
1517 header.instance = instance_id;
1518 header.msg_type = PLDM_REQUEST;
1519 header.pldm_type = PLDM_FWUP;
1520 header.command = PLDM_CANCEL_UPDATE;
1521 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1522 if (rc) {
1523 return rc;
1524 }
1525
1526 return PLDM_SUCCESS;
1527}
1528
1529int decode_cancel_update_resp(const struct pldm_msg *msg, size_t payload_length,
1530 uint8_t *completion_code,
1531 bool8_t *non_functioning_component_indication,
1532 bitfield64_t *non_functioning_component_bitmap)
1533{
1534 if (msg == NULL || completion_code == NULL ||
1535 non_functioning_component_indication == NULL ||
1536 non_functioning_component_bitmap == NULL || !payload_length) {
1537 return PLDM_ERROR_INVALID_DATA;
1538 }
1539
1540 *completion_code = msg->payload[0];
1541 if (*completion_code != PLDM_SUCCESS) {
1542 return PLDM_SUCCESS;
1543 }
1544
1545 if (payload_length != sizeof(struct pldm_cancel_update_resp)) {
1546 return PLDM_ERROR_INVALID_LENGTH;
1547 }
1548 struct pldm_cancel_update_resp *response =
1549 (struct pldm_cancel_update_resp *)msg->payload;
1550
1551 if (!is_non_functioning_component_indication_valid(
1552 response->non_functioning_component_indication)) {
1553 return PLDM_ERROR_INVALID_DATA;
1554 }
1555
1556 *non_functioning_component_indication =
1557 response->non_functioning_component_indication;
1558
1559 if (*non_functioning_component_indication) {
1560 non_functioning_component_bitmap->value =
1561 le64toh(response->non_functioning_component_bitmap);
1562 }
1563
1564 return PLDM_SUCCESS;
1565}