blob: 9ebb9703b58d04487fa87baf04693602c41c5e9e [file] [log] [blame]
Patrick Williams691668f2023-11-01 08:19:10 -05001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
Chris Wang4c1f2c72024-03-21 17:09:44 +08002#include "msgbuf.h"
Andrew Jefferyb0c1d202023-11-07 22:08:44 +10303#include <libpldm/firmware_update.h>
4#include <libpldm/utils.h>
5
Andrew Jeffery9c766792022-08-10 23:12:49 +09306#include <endian.h>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05307#include <stdbool.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +09308#include <string.h>
9
10/** @brief Check whether string type value is valid
11 *
12 * @return true if string type value is valid, false if not
13 */
14static bool is_string_type_valid(uint8_t string_type)
15{
16 switch (string_type) {
17 case PLDM_STR_TYPE_UNKNOWN:
18 return false;
19 case PLDM_STR_TYPE_ASCII:
20 case PLDM_STR_TYPE_UTF_8:
21 case PLDM_STR_TYPE_UTF_16:
22 case PLDM_STR_TYPE_UTF_16LE:
23 case PLDM_STR_TYPE_UTF_16BE:
24 return true;
25 default:
26 return false;
27 }
28}
29
30/** @brief Return the length of the descriptor type described in firmware update
31 * specification
32 *
33 * @return length of the descriptor type if descriptor type is valid else
34 * return 0
35 */
36static uint16_t get_descriptor_type_length(uint16_t descriptor_type)
37{
38 switch (descriptor_type) {
Andrew Jeffery9c766792022-08-10 23:12:49 +093039 case PLDM_FWUP_PCI_VENDOR_ID:
40 return PLDM_FWUP_PCI_VENDOR_ID_LENGTH;
41 case PLDM_FWUP_IANA_ENTERPRISE_ID:
42 return PLDM_FWUP_IANA_ENTERPRISE_ID_LENGTH;
43 case PLDM_FWUP_UUID:
44 return PLDM_FWUP_UUID_LENGTH;
45 case PLDM_FWUP_PNP_VENDOR_ID:
46 return PLDM_FWUP_PNP_VENDOR_ID_LENGTH;
47 case PLDM_FWUP_ACPI_VENDOR_ID:
48 return PLDM_FWUP_ACPI_VENDOR_ID_LENGTH;
49 case PLDM_FWUP_IEEE_ASSIGNED_COMPANY_ID:
50 return PLDM_FWUP_IEEE_ASSIGNED_COMPANY_ID_LENGTH;
51 case PLDM_FWUP_SCSI_VENDOR_ID:
52 return PLDM_FWUP_SCSI_VENDOR_ID_LENGTH;
53 case PLDM_FWUP_PCI_DEVICE_ID:
54 return PLDM_FWUP_PCI_DEVICE_ID_LENGTH;
55 case PLDM_FWUP_PCI_SUBSYSTEM_VENDOR_ID:
56 return PLDM_FWUP_PCI_SUBSYSTEM_VENDOR_ID_LENGTH;
57 case PLDM_FWUP_PCI_SUBSYSTEM_ID:
58 return PLDM_FWUP_PCI_SUBSYSTEM_ID_LENGTH;
59 case PLDM_FWUP_PCI_REVISION_ID:
60 return PLDM_FWUP_PCI_REVISION_ID_LENGTH;
61 case PLDM_FWUP_PNP_PRODUCT_IDENTIFIER:
62 return PLDM_FWUP_PNP_PRODUCT_IDENTIFIER_LENGTH;
63 case PLDM_FWUP_ACPI_PRODUCT_IDENTIFIER:
64 return PLDM_FWUP_ACPI_PRODUCT_IDENTIFIER_LENGTH;
65 case PLDM_FWUP_ASCII_MODEL_NUMBER_LONG_STRING:
66 return PLDM_FWUP_ASCII_MODEL_NUMBER_LONG_STRING_LENGTH;
67 case PLDM_FWUP_ASCII_MODEL_NUMBER_SHORT_STRING:
68 return PLDM_FWUP_ASCII_MODEL_NUMBER_SHORT_STRING_LENGTH;
69 case PLDM_FWUP_SCSI_PRODUCT_ID:
70 return PLDM_FWUP_SCSI_PRODUCT_ID_LENGTH;
71 case PLDM_FWUP_UBM_CONTROLLER_DEVICE_CODE:
72 return PLDM_FWUP_UBM_CONTROLLER_DEVICE_CODE_LENGTH;
73 default:
74 return 0;
75 }
76}
77
Chris Wang4c1f2c72024-03-21 17:09:44 +080078static bool is_downstream_device_update_support_valid(uint8_t resp)
79{
80 switch (resp) {
81 case PLDM_FWUP_DOWNSTREAM_DEVICE_UPDATE_NOT_SUPPORTED:
82 case PLDM_FWUP_DOWNSTREAM_DEVICE_UPDATE_SUPPORTED:
83 return true;
84 default:
85 return false;
86 }
87}
88
Chris Wang458475a2024-03-26 17:59:19 +080089static bool
90is_transfer_operation_flag_valid(enum transfer_op_flag transfer_op_flag)
91{
92 switch (transfer_op_flag) {
93 case PLDM_GET_NEXTPART:
94 case PLDM_GET_FIRSTPART:
95 return true;
96 default:
97 return false;
98 }
99}
100
Andrew Jeffery9c766792022-08-10 23:12:49 +0930101/** @brief Check whether ComponentResponse is valid
102 *
103 * @return true if ComponentResponse is valid, false if not
104 */
105static bool is_comp_resp_valid(uint8_t comp_resp)
106{
107 switch (comp_resp) {
108 case PLDM_CR_COMP_CAN_BE_UPDATED:
109 case PLDM_CR_COMP_MAY_BE_UPDATEABLE:
110 return true;
111
112 default:
113 return false;
114 }
115}
116
117/** @brief Check whether ComponentResponseCode is valid
118 *
119 * @return true if ComponentResponseCode is valid, false if not
120 */
121static bool is_comp_resp_code_valid(uint8_t comp_resp_code)
122{
123 switch (comp_resp_code) {
124 case PLDM_CRC_COMP_CAN_BE_UPDATED:
125 case PLDM_CRC_COMP_COMPARISON_STAMP_IDENTICAL:
126 case PLDM_CRC_COMP_COMPARISON_STAMP_LOWER:
127 case PLDM_CRC_INVALID_COMP_COMPARISON_STAMP:
128 case PLDM_CRC_COMP_CONFLICT:
129 case PLDM_CRC_COMP_PREREQUISITES_NOT_MET:
130 case PLDM_CRC_COMP_NOT_SUPPORTED:
131 case PLDM_CRC_COMP_SECURITY_RESTRICTIONS:
132 case PLDM_CRC_INCOMPLETE_COMP_IMAGE_SET:
133 case PLDM_CRC_ACTIVE_IMAGE_NOT_UPDATEABLE_SUBSEQUENTLY:
134 case PLDM_CRC_COMP_VER_STR_IDENTICAL:
135 case PLDM_CRC_COMP_VER_STR_LOWER:
136 return true;
137
138 default:
139 if (comp_resp_code >=
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930140 PLDM_CRC_VENDOR_COMP_RESP_CODE_RANGE_MIN &&
Andrew Jeffery9c766792022-08-10 23:12:49 +0930141 comp_resp_code <=
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930142 PLDM_CRC_VENDOR_COMP_RESP_CODE_RANGE_MAX) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930143 return true;
144 }
145 return false;
146 }
147}
148
149/** @brief Check whether ComponentCompatibilityResponse is valid
150 *
151 * @return true if ComponentCompatibilityResponse is valid, false if not
152 */
153static bool is_comp_compatibility_resp_valid(uint8_t comp_compatibility_resp)
154{
155 switch (comp_compatibility_resp) {
156 case PLDM_CCR_COMP_CAN_BE_UPDATED:
157 case PLDM_CCR_COMP_CANNOT_BE_UPDATED:
158 return true;
159
160 default:
161 return false;
162 }
163}
164
165/** @brief Check whether ComponentCompatibilityResponse Code is valid
166 *
167 * @return true if ComponentCompatibilityResponse Code is valid, false if not
168 */
169static bool
170is_comp_compatibility_resp_code_valid(uint8_t comp_compatibility_resp_code)
171{
172 switch (comp_compatibility_resp_code) {
173 case PLDM_CCRC_NO_RESPONSE_CODE:
174 case PLDM_CCRC_COMP_COMPARISON_STAMP_IDENTICAL:
175 case PLDM_CCRC_COMP_COMPARISON_STAMP_LOWER:
176 case PLDM_CCRC_INVALID_COMP_COMPARISON_STAMP:
177 case PLDM_CCRC_COMP_CONFLICT:
178 case PLDM_CCRC_COMP_PREREQUISITES_NOT_MET:
179 case PLDM_CCRC_COMP_NOT_SUPPORTED:
180 case PLDM_CCRC_COMP_SECURITY_RESTRICTIONS:
181 case PLDM_CRC_INCOMPLETE_COMP_IMAGE_SET:
182 case PLDM_CCRC_COMP_INFO_NO_MATCH:
183 case PLDM_CCRC_COMP_VER_STR_IDENTICAL:
184 case PLDM_CCRC_COMP_VER_STR_LOWER:
185 return true;
186
187 default:
188 if (comp_compatibility_resp_code >=
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930189 PLDM_CCRC_VENDOR_COMP_RESP_CODE_RANGE_MIN &&
Andrew Jeffery9c766792022-08-10 23:12:49 +0930190 comp_compatibility_resp_code <=
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930191 PLDM_CCRC_VENDOR_COMP_RESP_CODE_RANGE_MAX) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930192 return true;
193 }
194 return false;
195 }
196}
197
198/** @brief Check whether SelfContainedActivationRequest is valid
199 *
200 * @return true if SelfContainedActivationRequest is valid, false if not
201 */
202static bool
203is_self_contained_activation_req_valid(bool8_t self_contained_activation_req)
204{
205 switch (self_contained_activation_req) {
206 case PLDM_NOT_ACTIVATE_SELF_CONTAINED_COMPONENTS:
207 case PLDM_ACTIVATE_SELF_CONTAINED_COMPONENTS:
208 return true;
209
210 default:
211 return false;
212 }
213}
214
215/** @brief Check if current or previous status in GetStatus command response is
216 * valid
217 *
218 * @param[in] state - current or previous different state machine state of
219 * the FD
220 * @return true if state is valid, false if not
221 */
222static bool is_state_valid(uint8_t state)
223{
224 switch (state) {
225 case PLDM_FD_STATE_IDLE:
226 case PLDM_FD_STATE_LEARN_COMPONENTS:
227 case PLDM_FD_STATE_READY_XFER:
228 case PLDM_FD_STATE_DOWNLOAD:
229 case PLDM_FD_STATE_VERIFY:
230 case PLDM_FD_STATE_APPLY:
231 case PLDM_FD_STATE_ACTIVATE:
232 return true;
233
234 default:
235 return false;
236 }
237}
238
239/** @brief Check if aux state in GetStatus command response is valid
240 *
241 * @param[in] aux_state - provides additional information to the UA to describe
242 * the current operation state of the FD/FDP
243 *
244 * @return true if aux state is valid, false if not
245 */
246static bool is_aux_state_valid(uint8_t aux_state)
247{
248 switch (aux_state) {
249 case PLDM_FD_OPERATION_IN_PROGRESS:
250 case PLDM_FD_OPERATION_SUCCESSFUL:
251 case PLDM_FD_OPERATION_FAILED:
252 case PLDM_FD_IDLE_LEARN_COMPONENTS_READ_XFER:
253 return true;
254
255 default:
256 return false;
257 }
258}
259
260/** @brief Check if aux state status in GetStatus command response is valid
261 *
262 * @param[in] aux_state_status - aux state status
263 *
264 * @return true if aux state status is valid, false if not
265 */
266static bool is_aux_state_status_valid(uint8_t aux_state_status)
267{
268 if (aux_state_status == PLDM_FD_AUX_STATE_IN_PROGRESS_OR_SUCCESS ||
269 aux_state_status == PLDM_FD_TIMEOUT ||
Andrew Jeffery67f7ed92023-04-05 14:00:00 +0930270 aux_state_status == PLDM_FD_GENERIC_ERROR ||
271 (aux_state_status >= PLDM_FD_VENDOR_DEFINED_STATUS_CODE_START &&
272 aux_state_status <= PLDM_FD_VENDOR_DEFINED_STATUS_CODE_END)) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930273 return true;
274 }
275
276 return false;
277}
278
279/** @brief Check if reason code in GetStatus command response is valid
280 *
281 * @param[in] reason_code - provides the reason for why the current state
282 * entered the IDLE state
283 *
284 * @return true if reason code is valid, false if not
285 */
286static bool is_reason_code_valid(uint8_t reason_code)
287{
Andrew Jeffery9c766792022-08-10 23:12:49 +0930288 switch (reason_code) {
289 case PLDM_FD_INITIALIZATION:
290 case PLDM_FD_ACTIVATE_FW:
291 case PLDM_FD_CANCEL_UPDATE:
292 case PLDM_FD_TIMEOUT_LEARN_COMPONENT:
293 case PLDM_FD_TIMEOUT_READY_XFER:
294 case PLDM_FD_TIMEOUT_DOWNLOAD:
295 case PLDM_FD_TIMEOUT_VERIFY:
296 case PLDM_FD_TIMEOUT_APPLY:
297 return true;
298
299 default:
300 if (reason_code >= PLDM_FD_STATUS_VENDOR_DEFINED_MIN) {
301 return true;
302 }
303 return false;
304 }
305}
306
307/** @brief Check if non functioning component indication in CancelUpdate
308 * response is valid
309 *
310 * @return true if non functioning component indication is valid, false if not
311 */
312static bool is_non_functioning_component_indication_valid(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930313 bool8_t non_functioning_component_indication)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930314{
315 switch (non_functioning_component_indication) {
316 case PLDM_FWUP_COMPONENTS_FUNCTIONING:
317 case PLDM_FWUP_COMPONENTS_NOT_FUNCTIONING:
318 return true;
319
320 default:
321 return false;
322 }
323}
324
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930325LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930326int decode_pldm_package_header_info(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930327 const uint8_t *data, size_t length,
328 struct pldm_package_header_information *package_header_info,
329 struct variable_field *package_version_str)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930330{
331 if (data == NULL || package_header_info == NULL ||
332 package_version_str == NULL) {
333 return PLDM_ERROR_INVALID_DATA;
334 }
335
336 if (length < sizeof(struct pldm_package_header_information)) {
337 return PLDM_ERROR_INVALID_LENGTH;
338 }
339
340 struct pldm_package_header_information *data_header =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930341 (struct pldm_package_header_information *)(data);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930342
343 if (!is_string_type_valid(data_header->package_version_string_type) ||
344 (data_header->package_version_string_length == 0)) {
345 return PLDM_ERROR_INVALID_DATA;
346 }
347
348 if (length < sizeof(struct pldm_package_header_information) +
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930349 data_header->package_version_string_length) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930350 return PLDM_ERROR_INVALID_LENGTH;
351 }
352
353 if ((data_header->component_bitmap_bit_length %
354 PLDM_FWUP_COMPONENT_BITMAP_MULTIPLE) != 0) {
355 return PLDM_ERROR_INVALID_DATA;
356 }
357
358 memcpy(package_header_info->uuid, data_header->uuid,
359 sizeof(data_header->uuid));
360 package_header_info->package_header_format_version =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930361 data_header->package_header_format_version;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930362 package_header_info->package_header_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930363 le16toh(data_header->package_header_size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930364 memcpy(package_header_info->package_release_date_time,
365 data_header->package_release_date_time,
366 sizeof(data_header->package_release_date_time));
367 package_header_info->component_bitmap_bit_length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930368 le16toh(data_header->component_bitmap_bit_length);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930369 package_header_info->package_version_string_type =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930370 data_header->package_version_string_type;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930371 package_header_info->package_version_string_length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930372 data_header->package_version_string_length;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930373 package_version_str->ptr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930374 data + sizeof(struct pldm_package_header_information);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930375 package_version_str->length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930376 package_header_info->package_version_string_length;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930377
378 return PLDM_SUCCESS;
379}
380
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930381LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930382int decode_firmware_device_id_record(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930383 const uint8_t *data, size_t length,
384 uint16_t component_bitmap_bit_length,
385 struct pldm_firmware_device_id_record *fw_device_id_record,
386 struct variable_field *applicable_components,
387 struct variable_field *comp_image_set_version_str,
388 struct variable_field *record_descriptors,
389 struct variable_field *fw_device_pkg_data)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930390{
391 if (data == NULL || fw_device_id_record == NULL ||
392 applicable_components == NULL ||
393 comp_image_set_version_str == NULL || record_descriptors == NULL ||
394 fw_device_pkg_data == NULL) {
395 return PLDM_ERROR_INVALID_DATA;
396 }
397
398 if (length < sizeof(struct pldm_firmware_device_id_record)) {
399 return PLDM_ERROR_INVALID_LENGTH;
400 }
401
402 if ((component_bitmap_bit_length %
403 PLDM_FWUP_COMPONENT_BITMAP_MULTIPLE) != 0) {
404 return PLDM_ERROR_INVALID_DATA;
405 }
406
407 struct pldm_firmware_device_id_record *data_record =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930408 (struct pldm_firmware_device_id_record *)(data);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930409
410 if (!is_string_type_valid(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930411 data_record->comp_image_set_version_string_type) ||
Andrew Jeffery9c766792022-08-10 23:12:49 +0930412 (data_record->comp_image_set_version_string_length == 0)) {
413 return PLDM_ERROR_INVALID_DATA;
414 }
415
416 fw_device_id_record->record_length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930417 le16toh(data_record->record_length);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930418 fw_device_id_record->descriptor_count = data_record->descriptor_count;
419 fw_device_id_record->device_update_option_flags.value =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930420 le32toh(data_record->device_update_option_flags.value);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930421 fw_device_id_record->comp_image_set_version_string_type =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930422 data_record->comp_image_set_version_string_type;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930423 fw_device_id_record->comp_image_set_version_string_length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930424 data_record->comp_image_set_version_string_length;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930425 fw_device_id_record->fw_device_pkg_data_length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930426 le16toh(data_record->fw_device_pkg_data_length);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930427
428 if (length < fw_device_id_record->record_length) {
429 return PLDM_ERROR_INVALID_LENGTH;
430 }
431
432 uint16_t applicable_components_length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930433 component_bitmap_bit_length /
434 PLDM_FWUP_COMPONENT_BITMAP_MULTIPLE;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930435 uint16_t calc_min_record_length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930436 sizeof(struct pldm_firmware_device_id_record) +
437 applicable_components_length +
438 data_record->comp_image_set_version_string_length +
439 PLDM_FWUP_DEVICE_DESCRIPTOR_MIN_LEN +
440 fw_device_id_record->fw_device_pkg_data_length;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930441
442 if (fw_device_id_record->record_length < calc_min_record_length) {
443 return PLDM_ERROR_INVALID_LENGTH;
444 }
445
446 applicable_components->ptr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930447 data + sizeof(struct pldm_firmware_device_id_record);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930448 applicable_components->length = applicable_components_length;
449
450 comp_image_set_version_str->ptr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930451 applicable_components->ptr + applicable_components->length;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930452 comp_image_set_version_str->length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930453 fw_device_id_record->comp_image_set_version_string_length;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930454
455 record_descriptors->ptr = comp_image_set_version_str->ptr +
456 comp_image_set_version_str->length;
457 record_descriptors->length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930458 fw_device_id_record->record_length -
459 sizeof(struct pldm_firmware_device_id_record) -
460 applicable_components_length -
461 fw_device_id_record->comp_image_set_version_string_length -
462 fw_device_id_record->fw_device_pkg_data_length;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930463
464 if (fw_device_id_record->fw_device_pkg_data_length) {
465 fw_device_pkg_data->ptr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930466 record_descriptors->ptr + record_descriptors->length;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930467 fw_device_pkg_data->length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930468 fw_device_id_record->fw_device_pkg_data_length;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930469 }
470
471 return PLDM_SUCCESS;
472}
473
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930474LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930475int decode_descriptor_type_length_value(const uint8_t *data, size_t length,
476 uint16_t *descriptor_type,
477 struct variable_field *descriptor_data)
478{
479 uint16_t descriptor_length = 0;
480
481 if (data == NULL || descriptor_type == NULL ||
482 descriptor_data == NULL) {
483 return PLDM_ERROR_INVALID_DATA;
484 }
485
486 if (length < PLDM_FWUP_DEVICE_DESCRIPTOR_MIN_LEN) {
487 return PLDM_ERROR_INVALID_LENGTH;
488 }
489
490 struct pldm_descriptor_tlv *entry =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930491 (struct pldm_descriptor_tlv *)(data);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930492
493 *descriptor_type = le16toh(entry->descriptor_type);
494 descriptor_length = le16toh(entry->descriptor_length);
495 if (*descriptor_type != PLDM_FWUP_VENDOR_DEFINED) {
496 if (descriptor_length !=
497 get_descriptor_type_length(*descriptor_type)) {
498 return PLDM_ERROR_INVALID_LENGTH;
499 }
500 }
501
502 if (length < (sizeof(*descriptor_type) + sizeof(descriptor_length) +
503 descriptor_length)) {
504 return PLDM_ERROR_INVALID_LENGTH;
505 }
506
507 descriptor_data->ptr = entry->descriptor_data;
508 descriptor_data->length = descriptor_length;
509
510 return PLDM_SUCCESS;
511}
512
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930513LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930514int decode_vendor_defined_descriptor_value(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930515 const uint8_t *data, size_t length, uint8_t *descriptor_title_str_type,
516 struct variable_field *descriptor_title_str,
517 struct variable_field *descriptor_data)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930518{
519 if (data == NULL || descriptor_title_str_type == NULL ||
520 descriptor_title_str == NULL || descriptor_data == NULL) {
521 return PLDM_ERROR_INVALID_DATA;
522 }
523
524 if (length < sizeof(struct pldm_vendor_defined_descriptor_title_data)) {
525 return PLDM_ERROR_INVALID_LENGTH;
526 }
527
528 struct pldm_vendor_defined_descriptor_title_data *entry =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930529 (struct pldm_vendor_defined_descriptor_title_data *)(data);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930530 if (!is_string_type_valid(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930531 entry->vendor_defined_descriptor_title_str_type) ||
Andrew Jeffery9c766792022-08-10 23:12:49 +0930532 (entry->vendor_defined_descriptor_title_str_len == 0)) {
533 return PLDM_ERROR_INVALID_DATA;
534 }
535
536 // Assuming atleast 1 byte of VendorDefinedDescriptorData
537 if (length < (sizeof(struct pldm_vendor_defined_descriptor_title_data) +
538 entry->vendor_defined_descriptor_title_str_len)) {
539 return PLDM_ERROR_INVALID_LENGTH;
540 }
541
542 *descriptor_title_str_type =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930543 entry->vendor_defined_descriptor_title_str_type;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930544 descriptor_title_str->ptr = entry->vendor_defined_descriptor_title_str;
545 descriptor_title_str->length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930546 entry->vendor_defined_descriptor_title_str_len;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930547
548 descriptor_data->ptr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930549 descriptor_title_str->ptr + descriptor_title_str->length;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930550 descriptor_data->length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930551 length -
552 sizeof(entry->vendor_defined_descriptor_title_str_type) -
553 sizeof(entry->vendor_defined_descriptor_title_str_len) -
554 descriptor_title_str->length;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930555
556 return PLDM_SUCCESS;
557}
558
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930559LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930560int decode_pldm_comp_image_info(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930561 const uint8_t *data, size_t length,
562 struct pldm_component_image_information *pldm_comp_image_info,
563 struct variable_field *comp_version_str)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930564{
565 if (data == NULL || pldm_comp_image_info == NULL ||
566 comp_version_str == NULL) {
567 return PLDM_ERROR_INVALID_DATA;
568 }
569
570 if (length < sizeof(struct pldm_component_image_information)) {
571 return PLDM_ERROR_INVALID_LENGTH;
572 }
573
574 struct pldm_component_image_information *data_header =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930575 (struct pldm_component_image_information *)(data);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930576
577 if (!is_string_type_valid(data_header->comp_version_string_type) ||
578 (data_header->comp_version_string_length == 0)) {
579 return PLDM_ERROR_INVALID_DATA;
580 }
581
582 if (length < sizeof(struct pldm_component_image_information) +
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930583 data_header->comp_version_string_length) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930584 return PLDM_ERROR_INVALID_LENGTH;
585 }
586
587 pldm_comp_image_info->comp_classification =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930588 le16toh(data_header->comp_classification);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930589 pldm_comp_image_info->comp_identifier =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930590 le16toh(data_header->comp_identifier);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930591 pldm_comp_image_info->comp_comparison_stamp =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930592 le32toh(data_header->comp_comparison_stamp);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930593 pldm_comp_image_info->comp_options.value =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930594 le16toh(data_header->comp_options.value);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930595 pldm_comp_image_info->requested_comp_activation_method.value =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930596 le16toh(data_header->requested_comp_activation_method.value);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930597 pldm_comp_image_info->comp_location_offset =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930598 le32toh(data_header->comp_location_offset);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930599 pldm_comp_image_info->comp_size = le32toh(data_header->comp_size);
600 pldm_comp_image_info->comp_version_string_type =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930601 data_header->comp_version_string_type;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930602 pldm_comp_image_info->comp_version_string_length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930603 data_header->comp_version_string_length;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930604
605 if ((pldm_comp_image_info->comp_options.bits.bit1 == false &&
606 pldm_comp_image_info->comp_comparison_stamp !=
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930607 PLDM_FWUP_INVALID_COMPONENT_COMPARISON_TIMESTAMP)) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930608 return PLDM_ERROR_INVALID_DATA;
609 }
610
611 if (pldm_comp_image_info->comp_location_offset == 0 ||
612 pldm_comp_image_info->comp_size == 0) {
613 return PLDM_ERROR_INVALID_DATA;
614 }
615
616 comp_version_str->ptr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930617 data + sizeof(struct pldm_component_image_information);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930618 comp_version_str->length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930619 pldm_comp_image_info->comp_version_string_length;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930620
621 return PLDM_SUCCESS;
622}
623
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930624LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930625int encode_query_device_identifiers_req(uint8_t instance_id,
626 size_t payload_length,
627 struct pldm_msg *msg)
628{
629 if (msg == NULL) {
630 return PLDM_ERROR_INVALID_DATA;
631 }
632
633 if (payload_length != PLDM_QUERY_DEVICE_IDENTIFIERS_REQ_BYTES) {
634 return PLDM_ERROR_INVALID_LENGTH;
635 }
636
637 return encode_pldm_header_only(PLDM_REQUEST, instance_id, PLDM_FWUP,
638 PLDM_QUERY_DEVICE_IDENTIFIERS, msg);
639}
640
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930641LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930642int decode_query_device_identifiers_resp(const struct pldm_msg *msg,
643 size_t payload_length,
644 uint8_t *completion_code,
645 uint32_t *device_identifiers_len,
646 uint8_t *descriptor_count,
647 uint8_t **descriptor_data)
648{
649 if (msg == NULL || completion_code == NULL ||
650 device_identifiers_len == NULL || descriptor_count == NULL ||
651 descriptor_data == NULL) {
652 return PLDM_ERROR_INVALID_DATA;
653 }
654
655 *completion_code = msg->payload[0];
656 if (PLDM_SUCCESS != *completion_code) {
657 return PLDM_SUCCESS;
658 }
659
660 if (payload_length <
661 sizeof(struct pldm_query_device_identifiers_resp)) {
662 return PLDM_ERROR_INVALID_LENGTH;
663 }
664
665 struct pldm_query_device_identifiers_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930666 (struct pldm_query_device_identifiers_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930667 *device_identifiers_len = le32toh(response->device_identifiers_len);
668
669 if (*device_identifiers_len < PLDM_FWUP_DEVICE_DESCRIPTOR_MIN_LEN) {
670 return PLDM_ERROR_INVALID_LENGTH;
671 }
672
673 if (payload_length !=
674 sizeof(struct pldm_query_device_identifiers_resp) +
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930675 *device_identifiers_len) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930676 return PLDM_ERROR_INVALID_LENGTH;
677 }
678 *descriptor_count = response->descriptor_count;
679
680 if (*descriptor_count == 0) {
681 return PLDM_ERROR_INVALID_DATA;
682 }
683 *descriptor_data =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930684 (uint8_t *)(msg->payload +
685 sizeof(struct pldm_query_device_identifiers_resp));
Andrew Jeffery9c766792022-08-10 23:12:49 +0930686 return PLDM_SUCCESS;
687}
688
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930689LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930690int encode_get_firmware_parameters_req(uint8_t instance_id,
691 size_t payload_length,
692 struct pldm_msg *msg)
693{
694 if (msg == NULL) {
695 return PLDM_ERROR_INVALID_DATA;
696 }
697
698 if (payload_length != PLDM_GET_FIRMWARE_PARAMETERS_REQ_BYTES) {
699 return PLDM_ERROR_INVALID_LENGTH;
700 }
701
702 return encode_pldm_header_only(PLDM_REQUEST, instance_id, PLDM_FWUP,
703 PLDM_GET_FIRMWARE_PARAMETERS, msg);
704}
705
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930706LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930707int decode_get_firmware_parameters_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930708 const struct pldm_msg *msg, size_t payload_length,
709 struct pldm_get_firmware_parameters_resp *resp_data,
710 struct variable_field *active_comp_image_set_ver_str,
711 struct variable_field *pending_comp_image_set_ver_str,
712 struct variable_field *comp_parameter_table)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930713{
714 if (msg == NULL || resp_data == NULL ||
715 active_comp_image_set_ver_str == NULL ||
716 pending_comp_image_set_ver_str == NULL ||
717 comp_parameter_table == NULL || !payload_length) {
718 return PLDM_ERROR_INVALID_DATA;
719 }
720
721 resp_data->completion_code = msg->payload[0];
722 if (PLDM_SUCCESS != resp_data->completion_code) {
723 return PLDM_SUCCESS;
724 }
725
726 if (payload_length < sizeof(struct pldm_get_firmware_parameters_resp)) {
727 return PLDM_ERROR_INVALID_LENGTH;
728 }
729
730 struct pldm_get_firmware_parameters_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930731 (struct pldm_get_firmware_parameters_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930732
733 if (!is_string_type_valid(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930734 response->active_comp_image_set_ver_str_type) ||
Andrew Jeffery9c766792022-08-10 23:12:49 +0930735 (response->active_comp_image_set_ver_str_len == 0)) {
736 return PLDM_ERROR_INVALID_DATA;
737 }
738
739 if (response->pending_comp_image_set_ver_str_len == 0) {
740 if (response->pending_comp_image_set_ver_str_type !=
741 PLDM_STR_TYPE_UNKNOWN) {
742 return PLDM_ERROR_INVALID_DATA;
743 }
744 } else {
745 if (!is_string_type_valid(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930746 response->pending_comp_image_set_ver_str_type)) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930747 return PLDM_ERROR_INVALID_DATA;
748 }
749 }
750
751 size_t partial_response_length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930752 sizeof(struct pldm_get_firmware_parameters_resp) +
753 response->active_comp_image_set_ver_str_len +
754 response->pending_comp_image_set_ver_str_len;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930755
756 if (payload_length < partial_response_length) {
757 return PLDM_ERROR_INVALID_LENGTH;
758 }
759
760 resp_data->capabilities_during_update.value =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930761 le32toh(response->capabilities_during_update.value);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930762 resp_data->comp_count = le16toh(response->comp_count);
763 resp_data->active_comp_image_set_ver_str_type =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930764 response->active_comp_image_set_ver_str_type;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930765 resp_data->active_comp_image_set_ver_str_len =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930766 response->active_comp_image_set_ver_str_len;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930767 resp_data->pending_comp_image_set_ver_str_type =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930768 response->pending_comp_image_set_ver_str_type;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930769 resp_data->pending_comp_image_set_ver_str_len =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930770 response->pending_comp_image_set_ver_str_len;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930771
772 active_comp_image_set_ver_str->ptr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930773 msg->payload + sizeof(struct pldm_get_firmware_parameters_resp);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930774 active_comp_image_set_ver_str->length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930775 resp_data->active_comp_image_set_ver_str_len;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930776
777 if (resp_data->pending_comp_image_set_ver_str_len != 0) {
778 pending_comp_image_set_ver_str->ptr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930779 msg->payload +
780 sizeof(struct pldm_get_firmware_parameters_resp) +
781 resp_data->active_comp_image_set_ver_str_len;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930782 pending_comp_image_set_ver_str->length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930783 resp_data->pending_comp_image_set_ver_str_len;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930784 } else {
785 pending_comp_image_set_ver_str->ptr = NULL;
786 pending_comp_image_set_ver_str->length = 0;
787 }
788
789 if (payload_length > partial_response_length && resp_data->comp_count) {
790 comp_parameter_table->ptr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930791 msg->payload +
792 sizeof(struct pldm_get_firmware_parameters_resp) +
793 resp_data->active_comp_image_set_ver_str_len +
794 resp_data->pending_comp_image_set_ver_str_len;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930795 comp_parameter_table->length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930796 payload_length - partial_response_length;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930797 } else {
798 comp_parameter_table->ptr = NULL;
799 comp_parameter_table->length = 0;
800 }
801
802 return PLDM_SUCCESS;
803}
804
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930805LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930806int decode_get_firmware_parameters_resp_comp_entry(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930807 const uint8_t *data, size_t length,
808 struct pldm_component_parameter_entry *component_data,
809 struct variable_field *active_comp_ver_str,
810 struct variable_field *pending_comp_ver_str)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930811{
812 if (data == NULL || component_data == NULL ||
813 active_comp_ver_str == NULL || pending_comp_ver_str == NULL) {
814 return PLDM_ERROR_INVALID_DATA;
815 }
816
817 if (length < sizeof(struct pldm_component_parameter_entry)) {
818 return PLDM_ERROR_INVALID_LENGTH;
819 }
820
821 struct pldm_component_parameter_entry *entry =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930822 (struct pldm_component_parameter_entry *)(data);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930823
824 size_t entry_length = sizeof(struct pldm_component_parameter_entry) +
825 entry->active_comp_ver_str_len +
826 entry->pending_comp_ver_str_len;
827
828 if (length < entry_length) {
829 return PLDM_ERROR_INVALID_LENGTH;
830 }
831
832 component_data->comp_classification =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930833 le16toh(entry->comp_classification);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930834 component_data->comp_identifier = le16toh(entry->comp_identifier);
835 component_data->comp_classification_index =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930836 entry->comp_classification_index;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930837 component_data->active_comp_comparison_stamp =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930838 le32toh(entry->active_comp_comparison_stamp);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930839 component_data->active_comp_ver_str_type =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930840 entry->active_comp_ver_str_type;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930841 component_data->active_comp_ver_str_len =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930842 entry->active_comp_ver_str_len;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930843 memcpy(component_data->active_comp_release_date,
844 entry->active_comp_release_date,
845 sizeof(entry->active_comp_release_date));
846 component_data->pending_comp_comparison_stamp =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930847 le32toh(entry->pending_comp_comparison_stamp);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930848 component_data->pending_comp_ver_str_type =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930849 entry->pending_comp_ver_str_type;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930850 component_data->pending_comp_ver_str_len =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930851 entry->pending_comp_ver_str_len;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930852 memcpy(component_data->pending_comp_release_date,
853 entry->pending_comp_release_date,
854 sizeof(entry->pending_comp_release_date));
855 component_data->comp_activation_methods.value =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930856 le16toh(entry->comp_activation_methods.value);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930857 component_data->capabilities_during_update.value =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930858 le32toh(entry->capabilities_during_update.value);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930859
860 if (entry->active_comp_ver_str_len != 0) {
861 active_comp_ver_str->ptr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930862 data + sizeof(struct pldm_component_parameter_entry);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930863 active_comp_ver_str->length = entry->active_comp_ver_str_len;
864 } else {
865 active_comp_ver_str->ptr = NULL;
866 active_comp_ver_str->length = 0;
867 }
868
869 if (entry->pending_comp_ver_str_len != 0) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930870 pending_comp_ver_str->ptr =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930871 data + sizeof(struct pldm_component_parameter_entry) +
872 entry->active_comp_ver_str_len;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930873 pending_comp_ver_str->length = entry->pending_comp_ver_str_len;
874 } else {
875 pending_comp_ver_str->ptr = NULL;
876 pending_comp_ver_str->length = 0;
877 }
878 return PLDM_SUCCESS;
879}
880
Chris Wang4c1f2c72024-03-21 17:09:44 +0800881LIBPLDM_ABI_TESTING
882int encode_query_downstream_devices_req(uint8_t instance_id,
883 struct pldm_msg *msg)
884{
885 if (msg == NULL) {
886 return PLDM_ERROR_INVALID_DATA;
887 }
888
889 return encode_pldm_header_only(PLDM_REQUEST, instance_id, PLDM_FWUP,
890 PLDM_QUERY_DOWNSTREAM_DEVICES, msg);
891}
892
893LIBPLDM_ABI_TESTING
894int decode_query_downstream_devices_resp(
895 const struct pldm_msg *msg, size_t payload_length,
896 struct pldm_query_downstream_devices_resp *resp_data)
897{
898 struct pldm_msgbuf _buf;
899 struct pldm_msgbuf *buf = &_buf;
900 int rc;
901
902 if (msg == NULL || resp_data == NULL || !payload_length) {
903 return PLDM_ERROR_INVALID_DATA;
904 }
905
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930906 rc = pldm_msgbuf_init_cc(buf, PLDM_OPTIONAL_COMMAND_RESP_MIN_LEN,
907 msg->payload, payload_length);
Chris Wang4c1f2c72024-03-21 17:09:44 +0800908 if (rc) {
909 return rc;
910 }
911
912 rc = pldm_msgbuf_extract(buf, resp_data->completion_code);
913 if (rc) {
914 return rc;
915 }
916 if (PLDM_SUCCESS != resp_data->completion_code) {
917 // Return the CC directly without decoding the rest of the payload
918 return PLDM_SUCCESS;
919 }
920
921 if (payload_length < PLDM_QUERY_DOWNSTREAM_DEVICES_RESP_BYTES) {
922 return PLDM_ERROR_INVALID_LENGTH;
923 }
924
925 rc = pldm_msgbuf_extract(buf,
926 resp_data->downstream_device_update_supported);
927 if (rc) {
928 return rc;
929 }
930
931 if (!is_downstream_device_update_support_valid(
932 resp_data->downstream_device_update_supported)) {
933 return PLDM_ERROR_INVALID_DATA;
934 }
935
936 pldm_msgbuf_extract(buf, resp_data->number_of_downstream_devices);
937 pldm_msgbuf_extract(buf, resp_data->max_number_of_downstream_devices);
938 pldm_msgbuf_extract(buf, resp_data->capabilities.value);
939
940 return pldm_msgbuf_destroy_consumed(buf);
941}
942
Chris Wang458475a2024-03-26 17:59:19 +0800943LIBPLDM_ABI_TESTING
944int encode_query_downstream_identifiers_req(
945 uint8_t instance_id, uint32_t data_transfer_handle,
946 enum transfer_op_flag transfer_operation_flag, struct pldm_msg *msg,
947 size_t payload_length)
948{
949 struct pldm_msgbuf _buf;
950 struct pldm_msgbuf *buf = &_buf;
951 int rc;
952
953 if (msg == NULL) {
954 return PLDM_ERROR_INVALID_DATA;
955 }
956
957 if (!is_transfer_operation_flag_valid(transfer_operation_flag)) {
958 return PLDM_INVALID_TRANSFER_OPERATION_FLAG;
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_QUERY_DOWNSTREAM_IDENTIFIERS;
966 rc = pack_pldm_header(&header, &(msg->hdr));
967 if (rc) {
968 return rc;
969 }
970
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930971 rc = pldm_msgbuf_init_cc(buf,
972 PLDM_QUERY_DOWNSTREAM_IDENTIFIERS_REQ_BYTES,
973 msg->payload, payload_length);
Chris Wang458475a2024-03-26 17:59:19 +0800974 if (rc) {
975 return rc;
976 }
977
978 pldm_msgbuf_insert(buf, data_transfer_handle);
979 // Data correctness has been verified, cast it to 1-byte data directly.
980 pldm_msgbuf_insert(buf, (uint8_t)transfer_operation_flag);
981
982 return pldm_msgbuf_destroy(buf);
983}
984
985LIBPLDM_ABI_TESTING
986int decode_query_downstream_identifiers_resp(
987 const struct pldm_msg *msg, size_t payload_length,
988 struct pldm_query_downstream_identifiers_resp *resp_data,
989 struct variable_field *downstream_devices)
990{
991 struct pldm_msgbuf _buf;
992 struct pldm_msgbuf *buf = &_buf;
993 int rc = PLDM_ERROR;
994
995 if (msg == NULL || resp_data == NULL || downstream_devices == NULL ||
996 !payload_length) {
997 return PLDM_ERROR_INVALID_DATA;
998 }
999
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301000 rc = pldm_msgbuf_init_cc(buf, PLDM_OPTIONAL_COMMAND_RESP_MIN_LEN,
1001 msg->payload, payload_length);
Chris Wang458475a2024-03-26 17:59:19 +08001002 if (rc) {
1003 return rc;
1004 }
1005
1006 rc = pldm_msgbuf_extract(buf, resp_data->completion_code);
1007 if (rc) {
1008 return rc;
1009 }
1010 if (PLDM_SUCCESS != resp_data->completion_code) {
1011 return PLDM_SUCCESS;
1012 }
1013
1014 if (payload_length < PLDM_QUERY_DOWNSTREAM_IDENTIFIERS_RESP_MIN_LEN) {
1015 return PLDM_ERROR_INVALID_LENGTH;
1016 }
1017
1018 pldm_msgbuf_extract(buf, resp_data->next_data_transfer_handle);
1019 pldm_msgbuf_extract(buf, resp_data->transfer_flag);
1020
1021 rc = pldm_msgbuf_extract(buf, resp_data->downstream_devices_length);
1022 if (rc) {
1023 return rc;
1024 }
1025
1026 pldm_msgbuf_extract(buf, resp_data->number_of_downstream_devices);
1027 rc = pldm_msgbuf_span_required(buf,
1028 resp_data->downstream_devices_length,
1029 (void **)&downstream_devices->ptr);
1030 if (rc) {
1031 return rc;
1032 }
1033 downstream_devices->length = resp_data->downstream_devices_length;
1034
1035 return pldm_msgbuf_destroy(buf);
1036}
1037
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301038LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301039int encode_request_update_req(uint8_t instance_id, uint32_t max_transfer_size,
1040 uint16_t num_of_comp,
1041 uint8_t max_outstanding_transfer_req,
1042 uint16_t pkg_data_len,
1043 uint8_t comp_image_set_ver_str_type,
1044 uint8_t comp_image_set_ver_str_len,
1045 const struct variable_field *comp_img_set_ver_str,
1046 struct pldm_msg *msg, size_t payload_length)
1047{
1048 if (comp_img_set_ver_str == NULL || comp_img_set_ver_str->ptr == NULL ||
1049 msg == NULL) {
1050 return PLDM_ERROR_INVALID_DATA;
1051 }
1052
1053 if (payload_length != sizeof(struct pldm_request_update_req) +
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301054 comp_img_set_ver_str->length) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301055 return PLDM_ERROR_INVALID_LENGTH;
1056 }
1057
1058 if ((comp_image_set_ver_str_len == 0) ||
1059 (comp_image_set_ver_str_len != comp_img_set_ver_str->length)) {
1060 return PLDM_ERROR_INVALID_DATA;
1061 }
1062
1063 if ((max_transfer_size < PLDM_FWUP_BASELINE_TRANSFER_SIZE) ||
1064 (max_outstanding_transfer_req < PLDM_FWUP_MIN_OUTSTANDING_REQ)) {
1065 return PLDM_ERROR_INVALID_DATA;
1066 }
1067
1068 if (!is_string_type_valid(comp_image_set_ver_str_type)) {
1069 return PLDM_ERROR_INVALID_DATA;
1070 }
1071
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301072 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301073 header.instance = instance_id;
1074 header.msg_type = PLDM_REQUEST;
1075 header.pldm_type = PLDM_FWUP;
1076 header.command = PLDM_REQUEST_UPDATE;
1077 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1078 if (rc) {
1079 return rc;
1080 }
1081
1082 struct pldm_request_update_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301083 (struct pldm_request_update_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301084
1085 request->max_transfer_size = htole32(max_transfer_size);
1086 request->num_of_comp = htole16(num_of_comp);
1087 request->max_outstanding_transfer_req = max_outstanding_transfer_req;
1088 request->pkg_data_len = htole16(pkg_data_len);
1089 request->comp_image_set_ver_str_type = comp_image_set_ver_str_type;
1090 request->comp_image_set_ver_str_len = comp_image_set_ver_str_len;
1091
1092 memcpy(msg->payload + sizeof(struct pldm_request_update_req),
1093 comp_img_set_ver_str->ptr, comp_img_set_ver_str->length);
1094
1095 return PLDM_SUCCESS;
1096}
1097
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301098LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301099int decode_request_update_resp(const struct pldm_msg *msg,
1100 size_t payload_length, uint8_t *completion_code,
1101 uint16_t *fd_meta_data_len,
1102 uint8_t *fd_will_send_pkg_data)
1103{
1104 if (msg == NULL || completion_code == NULL ||
1105 fd_meta_data_len == NULL || fd_will_send_pkg_data == NULL ||
1106 !payload_length) {
1107 return PLDM_ERROR_INVALID_DATA;
1108 }
1109
1110 *completion_code = msg->payload[0];
1111 if (*completion_code != PLDM_SUCCESS) {
1112 return PLDM_SUCCESS;
1113 }
1114
1115 if (payload_length != sizeof(struct pldm_request_update_resp)) {
1116 return PLDM_ERROR_INVALID_LENGTH;
1117 }
1118
1119 struct pldm_request_update_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301120 (struct pldm_request_update_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301121
1122 *fd_meta_data_len = le16toh(response->fd_meta_data_len);
1123 *fd_will_send_pkg_data = response->fd_will_send_pkg_data;
1124
1125 return PLDM_SUCCESS;
1126}
1127
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301128LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301129int encode_pass_component_table_req(uint8_t instance_id, uint8_t transfer_flag,
1130 uint16_t comp_classification,
1131 uint16_t comp_identifier,
1132 uint8_t comp_classification_index,
1133 uint32_t comp_comparison_stamp,
1134 uint8_t comp_ver_str_type,
1135 uint8_t comp_ver_str_len,
1136 const struct variable_field *comp_ver_str,
1137 struct pldm_msg *msg, size_t payload_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301138{
1139 if (comp_ver_str == NULL || comp_ver_str->ptr == NULL || msg == NULL) {
1140 return PLDM_ERROR_INVALID_DATA;
1141 }
1142
1143 if (payload_length != sizeof(struct pldm_pass_component_table_req) +
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301144 comp_ver_str->length) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301145 return PLDM_ERROR_INVALID_LENGTH;
1146 }
1147
1148 if ((comp_ver_str_len == 0) ||
1149 (comp_ver_str_len != comp_ver_str->length)) {
1150 return PLDM_ERROR_INVALID_DATA;
1151 }
1152
1153 if (!is_transfer_flag_valid(transfer_flag)) {
1154 return PLDM_INVALID_TRANSFER_OPERATION_FLAG;
1155 }
1156
1157 if (!is_string_type_valid(comp_ver_str_type)) {
1158 return PLDM_ERROR_INVALID_DATA;
1159 }
1160
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301161 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301162 header.instance = instance_id;
1163 header.msg_type = PLDM_REQUEST;
1164 header.pldm_type = PLDM_FWUP;
1165 header.command = PLDM_PASS_COMPONENT_TABLE;
1166 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1167 if (rc) {
1168 return rc;
1169 }
1170
1171 struct pldm_pass_component_table_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301172 (struct pldm_pass_component_table_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301173
1174 request->transfer_flag = transfer_flag;
1175 request->comp_classification = htole16(comp_classification);
1176 request->comp_identifier = htole16(comp_identifier);
1177 request->comp_classification_index = comp_classification_index;
1178 request->comp_comparison_stamp = htole32(comp_comparison_stamp);
1179 request->comp_ver_str_type = comp_ver_str_type;
1180 request->comp_ver_str_len = comp_ver_str_len;
1181
1182 memcpy(msg->payload + sizeof(struct pldm_pass_component_table_req),
1183 comp_ver_str->ptr, comp_ver_str->length);
1184
1185 return PLDM_SUCCESS;
1186}
1187
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301188LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301189int decode_pass_component_table_resp(const struct pldm_msg *msg,
1190 const size_t payload_length,
1191 uint8_t *completion_code,
1192 uint8_t *comp_resp,
1193 uint8_t *comp_resp_code)
1194{
1195 if (msg == NULL || completion_code == NULL || comp_resp == NULL ||
1196 comp_resp_code == NULL || !payload_length) {
1197 return PLDM_ERROR_INVALID_DATA;
1198 }
1199
1200 *completion_code = msg->payload[0];
1201 if (*completion_code != PLDM_SUCCESS) {
1202 return PLDM_SUCCESS;
1203 }
1204
1205 if (payload_length != sizeof(struct pldm_pass_component_table_resp)) {
1206 return PLDM_ERROR_INVALID_LENGTH;
1207 }
1208
1209 struct pldm_pass_component_table_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301210 (struct pldm_pass_component_table_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301211
1212 if (!is_comp_resp_valid(response->comp_resp)) {
1213 return PLDM_ERROR_INVALID_DATA;
1214 }
1215
1216 if (!is_comp_resp_code_valid(response->comp_resp_code)) {
1217 return PLDM_ERROR_INVALID_DATA;
1218 }
1219
1220 *comp_resp = response->comp_resp;
1221 *comp_resp_code = response->comp_resp_code;
1222
1223 return PLDM_SUCCESS;
1224}
1225
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301226LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301227int encode_update_component_req(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301228 uint8_t instance_id, uint16_t comp_classification,
1229 uint16_t comp_identifier, uint8_t comp_classification_index,
1230 uint32_t comp_comparison_stamp, uint32_t comp_image_size,
1231 bitfield32_t update_option_flags, uint8_t comp_ver_str_type,
1232 uint8_t comp_ver_str_len, const struct variable_field *comp_ver_str,
1233 struct pldm_msg *msg, size_t payload_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301234{
1235 if (comp_ver_str == NULL || comp_ver_str->ptr == NULL || msg == NULL) {
1236 return PLDM_ERROR_INVALID_DATA;
1237 }
1238
1239 if (payload_length !=
1240 sizeof(struct pldm_update_component_req) + comp_ver_str->length) {
1241 return PLDM_ERROR_INVALID_LENGTH;
1242 }
1243
1244 if (!comp_image_size) {
1245 return PLDM_ERROR_INVALID_DATA;
1246 }
1247
1248 if ((comp_ver_str_len == 0) ||
1249 (comp_ver_str_len != comp_ver_str->length)) {
1250 return PLDM_ERROR_INVALID_DATA;
1251 }
1252
1253 if (!is_string_type_valid(comp_ver_str_type)) {
1254 return PLDM_ERROR_INVALID_DATA;
1255 }
1256
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301257 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301258 header.instance = instance_id;
1259 header.msg_type = PLDM_REQUEST;
1260 header.pldm_type = PLDM_FWUP;
1261 header.command = PLDM_UPDATE_COMPONENT;
1262 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1263 if (rc) {
1264 return rc;
1265 }
1266
1267 struct pldm_update_component_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301268 (struct pldm_update_component_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301269
1270 request->comp_classification = htole16(comp_classification);
1271 request->comp_identifier = htole16(comp_identifier);
1272 request->comp_classification_index = comp_classification_index;
1273 request->comp_comparison_stamp = htole32(comp_comparison_stamp);
1274 request->comp_image_size = htole32(comp_image_size);
1275 request->update_option_flags.value = htole32(update_option_flags.value);
1276 request->comp_ver_str_type = comp_ver_str_type;
1277 request->comp_ver_str_len = comp_ver_str_len;
1278
1279 memcpy(msg->payload + sizeof(struct pldm_update_component_req),
1280 comp_ver_str->ptr, comp_ver_str->length);
1281
1282 return PLDM_SUCCESS;
1283}
1284
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301285LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301286int decode_update_component_resp(const struct pldm_msg *msg,
1287 size_t payload_length,
1288 uint8_t *completion_code,
1289 uint8_t *comp_compatibility_resp,
1290 uint8_t *comp_compatibility_resp_code,
1291 bitfield32_t *update_option_flags_enabled,
1292 uint16_t *time_before_req_fw_data)
1293{
1294 if (msg == NULL || completion_code == NULL ||
1295 comp_compatibility_resp == NULL ||
1296 comp_compatibility_resp_code == NULL ||
1297 update_option_flags_enabled == NULL ||
1298 time_before_req_fw_data == NULL || !payload_length) {
1299 return PLDM_ERROR_INVALID_DATA;
1300 }
1301
1302 *completion_code = msg->payload[0];
1303 if (*completion_code != PLDM_SUCCESS) {
1304 return PLDM_SUCCESS;
1305 }
1306
1307 if (payload_length != sizeof(struct pldm_update_component_resp)) {
1308 return PLDM_ERROR_INVALID_LENGTH;
1309 }
1310
1311 struct pldm_update_component_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301312 (struct pldm_update_component_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301313
1314 if (!is_comp_compatibility_resp_valid(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301315 response->comp_compatibility_resp)) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301316 return PLDM_ERROR_INVALID_DATA;
1317 }
1318
1319 if (!is_comp_compatibility_resp_code_valid(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301320 response->comp_compatibility_resp_code)) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301321 return PLDM_ERROR_INVALID_DATA;
1322 }
1323
1324 *comp_compatibility_resp = response->comp_compatibility_resp;
1325 *comp_compatibility_resp_code = response->comp_compatibility_resp_code;
1326 update_option_flags_enabled->value =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301327 le32toh(response->update_option_flags_enabled.value);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301328 *time_before_req_fw_data = le16toh(response->time_before_req_fw_data);
1329
1330 return PLDM_SUCCESS;
1331}
1332
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301333LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301334int decode_request_firmware_data_req(const struct pldm_msg *msg,
1335 size_t payload_length, uint32_t *offset,
1336 uint32_t *length)
1337{
1338 if (msg == NULL || offset == NULL || length == NULL) {
1339 return PLDM_ERROR_INVALID_DATA;
1340 }
1341 if (payload_length != sizeof(struct pldm_request_firmware_data_req)) {
1342 return PLDM_ERROR_INVALID_LENGTH;
1343 }
1344 struct pldm_request_firmware_data_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301345 (struct pldm_request_firmware_data_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301346 *offset = le32toh(request->offset);
1347 *length = le32toh(request->length);
1348
1349 if (*length < PLDM_FWUP_BASELINE_TRANSFER_SIZE) {
1350 return PLDM_FWUP_INVALID_TRANSFER_LENGTH;
1351 }
1352
1353 return PLDM_SUCCESS;
1354}
1355
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301356LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301357int encode_request_firmware_data_resp(uint8_t instance_id,
1358 uint8_t completion_code,
1359 struct pldm_msg *msg,
1360 size_t payload_length)
1361{
1362 if (msg == NULL || !payload_length) {
1363 return PLDM_ERROR_INVALID_DATA;
1364 }
1365
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301366 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301367 header.instance = instance_id;
1368 header.msg_type = PLDM_RESPONSE;
1369 header.pldm_type = PLDM_FWUP;
1370 header.command = PLDM_REQUEST_FIRMWARE_DATA;
1371 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1372 if (rc) {
1373 return rc;
1374 }
1375
1376 msg->payload[0] = completion_code;
1377
1378 return PLDM_SUCCESS;
1379}
1380
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301381LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301382int decode_transfer_complete_req(const struct pldm_msg *msg,
1383 size_t payload_length,
1384 uint8_t *transfer_result)
1385{
1386 if (msg == NULL || transfer_result == NULL) {
1387 return PLDM_ERROR_INVALID_DATA;
1388 }
1389
1390 if (payload_length != sizeof(*transfer_result)) {
1391 return PLDM_ERROR_INVALID_LENGTH;
1392 }
1393
1394 *transfer_result = msg->payload[0];
1395 return PLDM_SUCCESS;
1396}
1397
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301398LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301399int encode_transfer_complete_resp(uint8_t instance_id, uint8_t completion_code,
1400 struct pldm_msg *msg, size_t payload_length)
1401{
1402 if (msg == NULL) {
1403 return PLDM_ERROR_INVALID_DATA;
1404 }
1405
1406 if (payload_length != sizeof(completion_code)) {
1407 return PLDM_ERROR_INVALID_LENGTH;
1408 }
1409
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301410 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301411 header.instance = instance_id;
1412 header.msg_type = PLDM_RESPONSE;
1413 header.pldm_type = PLDM_FWUP;
1414 header.command = PLDM_TRANSFER_COMPLETE;
1415 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1416 if (rc) {
1417 return rc;
1418 }
1419
1420 msg->payload[0] = completion_code;
1421
1422 return PLDM_SUCCESS;
1423}
1424
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301425LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301426int decode_verify_complete_req(const struct pldm_msg *msg,
1427 size_t payload_length, uint8_t *verify_result)
1428{
1429 if (msg == NULL || verify_result == NULL) {
1430 return PLDM_ERROR_INVALID_DATA;
1431 }
1432
1433 if (payload_length != sizeof(*verify_result)) {
1434 return PLDM_ERROR_INVALID_LENGTH;
1435 }
1436
1437 *verify_result = msg->payload[0];
1438 return PLDM_SUCCESS;
1439}
1440
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301441LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301442int encode_verify_complete_resp(uint8_t instance_id, uint8_t completion_code,
1443 struct pldm_msg *msg, size_t payload_length)
1444{
1445 if (msg == NULL) {
1446 return PLDM_ERROR_INVALID_DATA;
1447 }
1448
1449 if (payload_length != sizeof(completion_code)) {
1450 return PLDM_ERROR_INVALID_LENGTH;
1451 }
1452
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301453 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301454 header.instance = instance_id;
1455 header.msg_type = PLDM_RESPONSE;
1456 header.pldm_type = PLDM_FWUP;
1457 header.command = PLDM_VERIFY_COMPLETE;
1458 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1459 if (rc) {
1460 return rc;
1461 }
1462
1463 msg->payload[0] = completion_code;
1464
1465 return PLDM_SUCCESS;
1466}
1467
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301468LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301469int decode_apply_complete_req(const struct pldm_msg *msg, size_t payload_length,
1470 uint8_t *apply_result,
1471 bitfield16_t *comp_activation_methods_modification)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301472{
1473 if (msg == NULL || apply_result == NULL ||
1474 comp_activation_methods_modification == NULL) {
1475 return PLDM_ERROR_INVALID_DATA;
1476 }
1477
1478 if (payload_length != sizeof(struct pldm_apply_complete_req)) {
1479 return PLDM_ERROR_INVALID_LENGTH;
1480 }
1481
1482 struct pldm_apply_complete_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301483 (struct pldm_apply_complete_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301484
1485 *apply_result = request->apply_result;
1486 comp_activation_methods_modification->value =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301487 le16toh(request->comp_activation_methods_modification.value);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301488
1489 if ((*apply_result != PLDM_FWUP_APPLY_SUCCESS_WITH_ACTIVATION_METHOD) &&
1490 comp_activation_methods_modification->value) {
1491 return PLDM_ERROR_INVALID_DATA;
1492 }
1493
1494 return PLDM_SUCCESS;
1495}
1496
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301497LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301498int encode_apply_complete_resp(uint8_t instance_id, uint8_t completion_code,
1499 struct pldm_msg *msg, size_t payload_length)
1500{
1501 if (msg == NULL) {
1502 return PLDM_ERROR_INVALID_DATA;
1503 }
1504
1505 if (payload_length != sizeof(completion_code)) {
1506 return PLDM_ERROR_INVALID_LENGTH;
1507 }
1508
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301509 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301510 header.instance = instance_id;
1511 header.msg_type = PLDM_RESPONSE;
1512 header.pldm_type = PLDM_FWUP;
1513 header.command = PLDM_APPLY_COMPLETE;
1514 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1515 if (rc) {
1516 return rc;
1517 }
1518
1519 msg->payload[0] = completion_code;
1520
1521 return PLDM_SUCCESS;
1522}
1523
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301524LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301525int encode_activate_firmware_req(uint8_t instance_id,
1526 bool8_t self_contained_activation_req,
1527 struct pldm_msg *msg, size_t payload_length)
1528{
1529 if (msg == NULL) {
1530 return PLDM_ERROR_INVALID_DATA;
1531 }
1532
1533 if (payload_length != sizeof(struct pldm_activate_firmware_req)) {
1534 return PLDM_ERROR_INVALID_LENGTH;
1535 }
1536
1537 if (!is_self_contained_activation_req_valid(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301538 self_contained_activation_req)) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301539 return PLDM_ERROR_INVALID_DATA;
1540 }
1541
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301542 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301543 header.instance = instance_id;
1544 header.msg_type = PLDM_REQUEST;
1545 header.pldm_type = PLDM_FWUP;
1546 header.command = PLDM_ACTIVATE_FIRMWARE;
1547 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1548 if (rc) {
1549 return rc;
1550 }
1551
1552 struct pldm_activate_firmware_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301553 (struct pldm_activate_firmware_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301554
1555 request->self_contained_activation_req = self_contained_activation_req;
1556
1557 return PLDM_SUCCESS;
1558}
1559
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301560LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301561int decode_activate_firmware_resp(const struct pldm_msg *msg,
1562 size_t payload_length,
1563 uint8_t *completion_code,
1564 uint16_t *estimated_time_activation)
1565{
1566 if (msg == NULL || completion_code == NULL ||
1567 estimated_time_activation == NULL || !payload_length) {
1568 return PLDM_ERROR_INVALID_DATA;
1569 }
1570
1571 *completion_code = msg->payload[0];
1572 if (*completion_code != PLDM_SUCCESS) {
1573 return PLDM_SUCCESS;
1574 }
1575
1576 if (payload_length != sizeof(struct pldm_activate_firmware_resp)) {
1577 return PLDM_ERROR_INVALID_LENGTH;
1578 }
1579
1580 struct pldm_activate_firmware_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301581 (struct pldm_activate_firmware_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301582
1583 *estimated_time_activation =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301584 le16toh(response->estimated_time_activation);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301585
1586 return PLDM_SUCCESS;
1587}
1588
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301589LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301590int encode_get_status_req(uint8_t instance_id, struct pldm_msg *msg,
1591 size_t payload_length)
1592{
1593 if (msg == NULL) {
1594 return PLDM_ERROR_INVALID_DATA;
1595 }
1596
1597 if (payload_length != PLDM_GET_STATUS_REQ_BYTES) {
1598 return PLDM_ERROR_INVALID_LENGTH;
1599 }
1600
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301601 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301602 header.instance = instance_id;
1603 header.msg_type = PLDM_REQUEST;
1604 header.pldm_type = PLDM_FWUP;
1605 header.command = PLDM_GET_STATUS;
1606 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1607 if (rc) {
1608 return rc;
1609 }
1610
1611 return PLDM_SUCCESS;
1612}
1613
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301614LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301615int decode_get_status_resp(const struct pldm_msg *msg, size_t payload_length,
1616 uint8_t *completion_code, uint8_t *current_state,
1617 uint8_t *previous_state, uint8_t *aux_state,
1618 uint8_t *aux_state_status, uint8_t *progress_percent,
1619 uint8_t *reason_code,
1620 bitfield32_t *update_option_flags_enabled)
1621{
1622 if (msg == NULL || completion_code == NULL || current_state == NULL ||
1623 previous_state == NULL || aux_state == NULL ||
1624 aux_state_status == NULL || progress_percent == NULL ||
1625 reason_code == NULL || update_option_flags_enabled == NULL ||
1626 !payload_length) {
1627 return PLDM_ERROR_INVALID_DATA;
1628 }
1629
1630 *completion_code = msg->payload[0];
1631 if (*completion_code != PLDM_SUCCESS) {
1632 return PLDM_SUCCESS;
1633 }
1634
1635 if (payload_length != sizeof(struct pldm_get_status_resp)) {
1636 return PLDM_ERROR_INVALID_LENGTH;
1637 }
1638 struct pldm_get_status_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301639 (struct pldm_get_status_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301640
1641 if (!is_state_valid(response->current_state)) {
1642 return PLDM_ERROR_INVALID_DATA;
1643 }
1644 if (!is_state_valid(response->previous_state)) {
1645 return PLDM_ERROR_INVALID_DATA;
1646 }
1647 if (!is_aux_state_valid(response->aux_state)) {
1648 return PLDM_ERROR_INVALID_DATA;
1649 }
1650 if (!is_aux_state_status_valid(response->aux_state_status)) {
1651 return PLDM_ERROR_INVALID_DATA;
1652 }
1653 if (response->progress_percent > PLDM_FWUP_MAX_PROGRESS_PERCENT) {
1654 return PLDM_ERROR_INVALID_DATA;
1655 }
1656 if (!is_reason_code_valid(response->reason_code)) {
1657 return PLDM_ERROR_INVALID_DATA;
1658 }
1659
1660 if ((response->current_state == PLDM_FD_STATE_IDLE) ||
1661 (response->current_state == PLDM_FD_STATE_LEARN_COMPONENTS) ||
1662 (response->current_state == PLDM_FD_STATE_READY_XFER)) {
1663 if (response->aux_state !=
1664 PLDM_FD_IDLE_LEARN_COMPONENTS_READ_XFER) {
1665 return PLDM_ERROR_INVALID_DATA;
1666 }
1667 }
1668
1669 *current_state = response->current_state;
1670 *previous_state = response->previous_state;
1671 *aux_state = response->aux_state;
1672 *aux_state_status = response->aux_state_status;
1673 *progress_percent = response->progress_percent;
1674 *reason_code = response->reason_code;
1675 update_option_flags_enabled->value =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301676 le32toh(response->update_option_flags_enabled.value);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301677
1678 return PLDM_SUCCESS;
1679}
1680
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301681LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301682int encode_cancel_update_component_req(uint8_t instance_id,
1683 struct pldm_msg *msg,
1684 size_t payload_length)
1685{
1686 if (msg == NULL) {
1687 return PLDM_ERROR_INVALID_DATA;
1688 }
1689
1690 if (payload_length != PLDM_CANCEL_UPDATE_COMPONENT_REQ_BYTES) {
1691 return PLDM_ERROR_INVALID_LENGTH;
1692 }
1693
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301694 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301695 header.instance = instance_id;
1696 header.msg_type = PLDM_REQUEST;
1697 header.pldm_type = PLDM_FWUP;
1698 header.command = PLDM_CANCEL_UPDATE_COMPONENT;
1699 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1700 if (rc) {
1701 return rc;
1702 }
1703
1704 return PLDM_SUCCESS;
1705}
1706
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301707LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301708int decode_cancel_update_component_resp(const struct pldm_msg *msg,
1709 size_t payload_length,
1710 uint8_t *completion_code)
1711{
1712 if (msg == NULL || completion_code == NULL) {
1713 return PLDM_ERROR_INVALID_DATA;
1714 }
1715
1716 if (payload_length != sizeof(*completion_code)) {
1717 return PLDM_ERROR_INVALID_LENGTH;
1718 }
1719
1720 *completion_code = msg->payload[0];
1721 return PLDM_SUCCESS;
1722}
1723
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301724LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301725int encode_cancel_update_req(uint8_t instance_id, struct pldm_msg *msg,
1726 size_t payload_length)
1727{
1728 if (msg == NULL) {
1729 return PLDM_ERROR_INVALID_DATA;
1730 }
1731
1732 if (payload_length != PLDM_CANCEL_UPDATE_REQ_BYTES) {
1733 return PLDM_ERROR_INVALID_LENGTH;
1734 }
1735
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301736 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301737 header.instance = instance_id;
1738 header.msg_type = PLDM_REQUEST;
1739 header.pldm_type = PLDM_FWUP;
1740 header.command = PLDM_CANCEL_UPDATE;
1741 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1742 if (rc) {
1743 return rc;
1744 }
1745
1746 return PLDM_SUCCESS;
1747}
1748
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301749LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301750int decode_cancel_update_resp(const struct pldm_msg *msg, size_t payload_length,
1751 uint8_t *completion_code,
1752 bool8_t *non_functioning_component_indication,
1753 bitfield64_t *non_functioning_component_bitmap)
1754{
1755 if (msg == NULL || completion_code == NULL ||
1756 non_functioning_component_indication == NULL ||
1757 non_functioning_component_bitmap == NULL || !payload_length) {
1758 return PLDM_ERROR_INVALID_DATA;
1759 }
1760
1761 *completion_code = msg->payload[0];
1762 if (*completion_code != PLDM_SUCCESS) {
1763 return PLDM_SUCCESS;
1764 }
1765
1766 if (payload_length != sizeof(struct pldm_cancel_update_resp)) {
1767 return PLDM_ERROR_INVALID_LENGTH;
1768 }
1769 struct pldm_cancel_update_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301770 (struct pldm_cancel_update_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301771
1772 if (!is_non_functioning_component_indication_valid(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301773 response->non_functioning_component_indication)) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301774 return PLDM_ERROR_INVALID_DATA;
1775 }
1776
1777 *non_functioning_component_indication =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301778 response->non_functioning_component_indication;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301779
1780 if (*non_functioning_component_indication) {
1781 non_functioning_component_bitmap->value =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301782 le64toh(response->non_functioning_component_bitmap);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301783 }
1784
1785 return PLDM_SUCCESS;
1786}