blob: 4aafadc0ee16e6b0e63f32a38077d92580ecdd10 [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
906 rc = pldm_msgbuf_init(buf, PLDM_OPTIONAL_COMMAND_RESP_MIN_LEN,
907 msg->payload, payload_length);
908 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
971 rc = pldm_msgbuf_init(buf, PLDM_QUERY_DOWNSTREAM_IDENTIFIERS_REQ_BYTES,
972 msg->payload, payload_length);
973 if (rc) {
974 return rc;
975 }
976
977 pldm_msgbuf_insert(buf, data_transfer_handle);
978 // Data correctness has been verified, cast it to 1-byte data directly.
979 pldm_msgbuf_insert(buf, (uint8_t)transfer_operation_flag);
980
981 return pldm_msgbuf_destroy(buf);
982}
983
984LIBPLDM_ABI_TESTING
985int decode_query_downstream_identifiers_resp(
986 const struct pldm_msg *msg, size_t payload_length,
987 struct pldm_query_downstream_identifiers_resp *resp_data,
988 struct variable_field *downstream_devices)
989{
990 struct pldm_msgbuf _buf;
991 struct pldm_msgbuf *buf = &_buf;
992 int rc = PLDM_ERROR;
993
994 if (msg == NULL || resp_data == NULL || downstream_devices == NULL ||
995 !payload_length) {
996 return PLDM_ERROR_INVALID_DATA;
997 }
998
999 rc = pldm_msgbuf_init(buf, PLDM_OPTIONAL_COMMAND_RESP_MIN_LEN,
1000 msg->payload, payload_length);
1001 if (rc) {
1002 return rc;
1003 }
1004
1005 rc = pldm_msgbuf_extract(buf, resp_data->completion_code);
1006 if (rc) {
1007 return rc;
1008 }
1009 if (PLDM_SUCCESS != resp_data->completion_code) {
1010 return PLDM_SUCCESS;
1011 }
1012
1013 if (payload_length < PLDM_QUERY_DOWNSTREAM_IDENTIFIERS_RESP_MIN_LEN) {
1014 return PLDM_ERROR_INVALID_LENGTH;
1015 }
1016
1017 pldm_msgbuf_extract(buf, resp_data->next_data_transfer_handle);
1018 pldm_msgbuf_extract(buf, resp_data->transfer_flag);
1019
1020 rc = pldm_msgbuf_extract(buf, resp_data->downstream_devices_length);
1021 if (rc) {
1022 return rc;
1023 }
1024
1025 pldm_msgbuf_extract(buf, resp_data->number_of_downstream_devices);
1026 rc = pldm_msgbuf_span_required(buf,
1027 resp_data->downstream_devices_length,
1028 (void **)&downstream_devices->ptr);
1029 if (rc) {
1030 return rc;
1031 }
1032 downstream_devices->length = resp_data->downstream_devices_length;
1033
1034 return pldm_msgbuf_destroy(buf);
1035}
1036
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301037LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301038int encode_request_update_req(uint8_t instance_id, uint32_t max_transfer_size,
1039 uint16_t num_of_comp,
1040 uint8_t max_outstanding_transfer_req,
1041 uint16_t pkg_data_len,
1042 uint8_t comp_image_set_ver_str_type,
1043 uint8_t comp_image_set_ver_str_len,
1044 const struct variable_field *comp_img_set_ver_str,
1045 struct pldm_msg *msg, size_t payload_length)
1046{
1047 if (comp_img_set_ver_str == NULL || comp_img_set_ver_str->ptr == NULL ||
1048 msg == NULL) {
1049 return PLDM_ERROR_INVALID_DATA;
1050 }
1051
1052 if (payload_length != sizeof(struct pldm_request_update_req) +
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301053 comp_img_set_ver_str->length) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301054 return PLDM_ERROR_INVALID_LENGTH;
1055 }
1056
1057 if ((comp_image_set_ver_str_len == 0) ||
1058 (comp_image_set_ver_str_len != comp_img_set_ver_str->length)) {
1059 return PLDM_ERROR_INVALID_DATA;
1060 }
1061
1062 if ((max_transfer_size < PLDM_FWUP_BASELINE_TRANSFER_SIZE) ||
1063 (max_outstanding_transfer_req < PLDM_FWUP_MIN_OUTSTANDING_REQ)) {
1064 return PLDM_ERROR_INVALID_DATA;
1065 }
1066
1067 if (!is_string_type_valid(comp_image_set_ver_str_type)) {
1068 return PLDM_ERROR_INVALID_DATA;
1069 }
1070
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301071 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301072 header.instance = instance_id;
1073 header.msg_type = PLDM_REQUEST;
1074 header.pldm_type = PLDM_FWUP;
1075 header.command = PLDM_REQUEST_UPDATE;
1076 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1077 if (rc) {
1078 return rc;
1079 }
1080
1081 struct pldm_request_update_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301082 (struct pldm_request_update_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301083
1084 request->max_transfer_size = htole32(max_transfer_size);
1085 request->num_of_comp = htole16(num_of_comp);
1086 request->max_outstanding_transfer_req = max_outstanding_transfer_req;
1087 request->pkg_data_len = htole16(pkg_data_len);
1088 request->comp_image_set_ver_str_type = comp_image_set_ver_str_type;
1089 request->comp_image_set_ver_str_len = comp_image_set_ver_str_len;
1090
1091 memcpy(msg->payload + sizeof(struct pldm_request_update_req),
1092 comp_img_set_ver_str->ptr, comp_img_set_ver_str->length);
1093
1094 return PLDM_SUCCESS;
1095}
1096
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301097LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301098int decode_request_update_resp(const struct pldm_msg *msg,
1099 size_t payload_length, uint8_t *completion_code,
1100 uint16_t *fd_meta_data_len,
1101 uint8_t *fd_will_send_pkg_data)
1102{
1103 if (msg == NULL || completion_code == NULL ||
1104 fd_meta_data_len == NULL || fd_will_send_pkg_data == NULL ||
1105 !payload_length) {
1106 return PLDM_ERROR_INVALID_DATA;
1107 }
1108
1109 *completion_code = msg->payload[0];
1110 if (*completion_code != PLDM_SUCCESS) {
1111 return PLDM_SUCCESS;
1112 }
1113
1114 if (payload_length != sizeof(struct pldm_request_update_resp)) {
1115 return PLDM_ERROR_INVALID_LENGTH;
1116 }
1117
1118 struct pldm_request_update_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301119 (struct pldm_request_update_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301120
1121 *fd_meta_data_len = le16toh(response->fd_meta_data_len);
1122 *fd_will_send_pkg_data = response->fd_will_send_pkg_data;
1123
1124 return PLDM_SUCCESS;
1125}
1126
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301127LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301128int encode_pass_component_table_req(uint8_t instance_id, uint8_t transfer_flag,
1129 uint16_t comp_classification,
1130 uint16_t comp_identifier,
1131 uint8_t comp_classification_index,
1132 uint32_t comp_comparison_stamp,
1133 uint8_t comp_ver_str_type,
1134 uint8_t comp_ver_str_len,
1135 const struct variable_field *comp_ver_str,
1136 struct pldm_msg *msg, size_t payload_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301137{
1138 if (comp_ver_str == NULL || comp_ver_str->ptr == NULL || msg == NULL) {
1139 return PLDM_ERROR_INVALID_DATA;
1140 }
1141
1142 if (payload_length != sizeof(struct pldm_pass_component_table_req) +
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301143 comp_ver_str->length) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301144 return PLDM_ERROR_INVALID_LENGTH;
1145 }
1146
1147 if ((comp_ver_str_len == 0) ||
1148 (comp_ver_str_len != comp_ver_str->length)) {
1149 return PLDM_ERROR_INVALID_DATA;
1150 }
1151
1152 if (!is_transfer_flag_valid(transfer_flag)) {
1153 return PLDM_INVALID_TRANSFER_OPERATION_FLAG;
1154 }
1155
1156 if (!is_string_type_valid(comp_ver_str_type)) {
1157 return PLDM_ERROR_INVALID_DATA;
1158 }
1159
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301160 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301161 header.instance = instance_id;
1162 header.msg_type = PLDM_REQUEST;
1163 header.pldm_type = PLDM_FWUP;
1164 header.command = PLDM_PASS_COMPONENT_TABLE;
1165 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1166 if (rc) {
1167 return rc;
1168 }
1169
1170 struct pldm_pass_component_table_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301171 (struct pldm_pass_component_table_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301172
1173 request->transfer_flag = transfer_flag;
1174 request->comp_classification = htole16(comp_classification);
1175 request->comp_identifier = htole16(comp_identifier);
1176 request->comp_classification_index = comp_classification_index;
1177 request->comp_comparison_stamp = htole32(comp_comparison_stamp);
1178 request->comp_ver_str_type = comp_ver_str_type;
1179 request->comp_ver_str_len = comp_ver_str_len;
1180
1181 memcpy(msg->payload + sizeof(struct pldm_pass_component_table_req),
1182 comp_ver_str->ptr, comp_ver_str->length);
1183
1184 return PLDM_SUCCESS;
1185}
1186
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301187LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301188int decode_pass_component_table_resp(const struct pldm_msg *msg,
1189 const size_t payload_length,
1190 uint8_t *completion_code,
1191 uint8_t *comp_resp,
1192 uint8_t *comp_resp_code)
1193{
1194 if (msg == NULL || completion_code == NULL || comp_resp == NULL ||
1195 comp_resp_code == NULL || !payload_length) {
1196 return PLDM_ERROR_INVALID_DATA;
1197 }
1198
1199 *completion_code = msg->payload[0];
1200 if (*completion_code != PLDM_SUCCESS) {
1201 return PLDM_SUCCESS;
1202 }
1203
1204 if (payload_length != sizeof(struct pldm_pass_component_table_resp)) {
1205 return PLDM_ERROR_INVALID_LENGTH;
1206 }
1207
1208 struct pldm_pass_component_table_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301209 (struct pldm_pass_component_table_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301210
1211 if (!is_comp_resp_valid(response->comp_resp)) {
1212 return PLDM_ERROR_INVALID_DATA;
1213 }
1214
1215 if (!is_comp_resp_code_valid(response->comp_resp_code)) {
1216 return PLDM_ERROR_INVALID_DATA;
1217 }
1218
1219 *comp_resp = response->comp_resp;
1220 *comp_resp_code = response->comp_resp_code;
1221
1222 return PLDM_SUCCESS;
1223}
1224
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301225LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301226int encode_update_component_req(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301227 uint8_t instance_id, uint16_t comp_classification,
1228 uint16_t comp_identifier, uint8_t comp_classification_index,
1229 uint32_t comp_comparison_stamp, uint32_t comp_image_size,
1230 bitfield32_t update_option_flags, uint8_t comp_ver_str_type,
1231 uint8_t comp_ver_str_len, const struct variable_field *comp_ver_str,
1232 struct pldm_msg *msg, size_t payload_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301233{
1234 if (comp_ver_str == NULL || comp_ver_str->ptr == NULL || msg == NULL) {
1235 return PLDM_ERROR_INVALID_DATA;
1236 }
1237
1238 if (payload_length !=
1239 sizeof(struct pldm_update_component_req) + comp_ver_str->length) {
1240 return PLDM_ERROR_INVALID_LENGTH;
1241 }
1242
1243 if (!comp_image_size) {
1244 return PLDM_ERROR_INVALID_DATA;
1245 }
1246
1247 if ((comp_ver_str_len == 0) ||
1248 (comp_ver_str_len != comp_ver_str->length)) {
1249 return PLDM_ERROR_INVALID_DATA;
1250 }
1251
1252 if (!is_string_type_valid(comp_ver_str_type)) {
1253 return PLDM_ERROR_INVALID_DATA;
1254 }
1255
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301256 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301257 header.instance = instance_id;
1258 header.msg_type = PLDM_REQUEST;
1259 header.pldm_type = PLDM_FWUP;
1260 header.command = PLDM_UPDATE_COMPONENT;
1261 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1262 if (rc) {
1263 return rc;
1264 }
1265
1266 struct pldm_update_component_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301267 (struct pldm_update_component_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301268
1269 request->comp_classification = htole16(comp_classification);
1270 request->comp_identifier = htole16(comp_identifier);
1271 request->comp_classification_index = comp_classification_index;
1272 request->comp_comparison_stamp = htole32(comp_comparison_stamp);
1273 request->comp_image_size = htole32(comp_image_size);
1274 request->update_option_flags.value = htole32(update_option_flags.value);
1275 request->comp_ver_str_type = comp_ver_str_type;
1276 request->comp_ver_str_len = comp_ver_str_len;
1277
1278 memcpy(msg->payload + sizeof(struct pldm_update_component_req),
1279 comp_ver_str->ptr, comp_ver_str->length);
1280
1281 return PLDM_SUCCESS;
1282}
1283
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301284LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301285int decode_update_component_resp(const struct pldm_msg *msg,
1286 size_t payload_length,
1287 uint8_t *completion_code,
1288 uint8_t *comp_compatibility_resp,
1289 uint8_t *comp_compatibility_resp_code,
1290 bitfield32_t *update_option_flags_enabled,
1291 uint16_t *time_before_req_fw_data)
1292{
1293 if (msg == NULL || completion_code == NULL ||
1294 comp_compatibility_resp == NULL ||
1295 comp_compatibility_resp_code == NULL ||
1296 update_option_flags_enabled == NULL ||
1297 time_before_req_fw_data == NULL || !payload_length) {
1298 return PLDM_ERROR_INVALID_DATA;
1299 }
1300
1301 *completion_code = msg->payload[0];
1302 if (*completion_code != PLDM_SUCCESS) {
1303 return PLDM_SUCCESS;
1304 }
1305
1306 if (payload_length != sizeof(struct pldm_update_component_resp)) {
1307 return PLDM_ERROR_INVALID_LENGTH;
1308 }
1309
1310 struct pldm_update_component_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301311 (struct pldm_update_component_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301312
1313 if (!is_comp_compatibility_resp_valid(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301314 response->comp_compatibility_resp)) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301315 return PLDM_ERROR_INVALID_DATA;
1316 }
1317
1318 if (!is_comp_compatibility_resp_code_valid(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301319 response->comp_compatibility_resp_code)) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301320 return PLDM_ERROR_INVALID_DATA;
1321 }
1322
1323 *comp_compatibility_resp = response->comp_compatibility_resp;
1324 *comp_compatibility_resp_code = response->comp_compatibility_resp_code;
1325 update_option_flags_enabled->value =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301326 le32toh(response->update_option_flags_enabled.value);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301327 *time_before_req_fw_data = le16toh(response->time_before_req_fw_data);
1328
1329 return PLDM_SUCCESS;
1330}
1331
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301332LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301333int decode_request_firmware_data_req(const struct pldm_msg *msg,
1334 size_t payload_length, uint32_t *offset,
1335 uint32_t *length)
1336{
1337 if (msg == NULL || offset == NULL || length == NULL) {
1338 return PLDM_ERROR_INVALID_DATA;
1339 }
1340 if (payload_length != sizeof(struct pldm_request_firmware_data_req)) {
1341 return PLDM_ERROR_INVALID_LENGTH;
1342 }
1343 struct pldm_request_firmware_data_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301344 (struct pldm_request_firmware_data_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301345 *offset = le32toh(request->offset);
1346 *length = le32toh(request->length);
1347
1348 if (*length < PLDM_FWUP_BASELINE_TRANSFER_SIZE) {
1349 return PLDM_FWUP_INVALID_TRANSFER_LENGTH;
1350 }
1351
1352 return PLDM_SUCCESS;
1353}
1354
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301355LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301356int encode_request_firmware_data_resp(uint8_t instance_id,
1357 uint8_t completion_code,
1358 struct pldm_msg *msg,
1359 size_t payload_length)
1360{
1361 if (msg == NULL || !payload_length) {
1362 return PLDM_ERROR_INVALID_DATA;
1363 }
1364
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301365 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301366 header.instance = instance_id;
1367 header.msg_type = PLDM_RESPONSE;
1368 header.pldm_type = PLDM_FWUP;
1369 header.command = PLDM_REQUEST_FIRMWARE_DATA;
1370 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1371 if (rc) {
1372 return rc;
1373 }
1374
1375 msg->payload[0] = completion_code;
1376
1377 return PLDM_SUCCESS;
1378}
1379
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301380LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301381int decode_transfer_complete_req(const struct pldm_msg *msg,
1382 size_t payload_length,
1383 uint8_t *transfer_result)
1384{
1385 if (msg == NULL || transfer_result == NULL) {
1386 return PLDM_ERROR_INVALID_DATA;
1387 }
1388
1389 if (payload_length != sizeof(*transfer_result)) {
1390 return PLDM_ERROR_INVALID_LENGTH;
1391 }
1392
1393 *transfer_result = msg->payload[0];
1394 return PLDM_SUCCESS;
1395}
1396
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301397LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301398int encode_transfer_complete_resp(uint8_t instance_id, uint8_t completion_code,
1399 struct pldm_msg *msg, size_t payload_length)
1400{
1401 if (msg == NULL) {
1402 return PLDM_ERROR_INVALID_DATA;
1403 }
1404
1405 if (payload_length != sizeof(completion_code)) {
1406 return PLDM_ERROR_INVALID_LENGTH;
1407 }
1408
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301409 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301410 header.instance = instance_id;
1411 header.msg_type = PLDM_RESPONSE;
1412 header.pldm_type = PLDM_FWUP;
1413 header.command = PLDM_TRANSFER_COMPLETE;
1414 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1415 if (rc) {
1416 return rc;
1417 }
1418
1419 msg->payload[0] = completion_code;
1420
1421 return PLDM_SUCCESS;
1422}
1423
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301424LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301425int decode_verify_complete_req(const struct pldm_msg *msg,
1426 size_t payload_length, uint8_t *verify_result)
1427{
1428 if (msg == NULL || verify_result == NULL) {
1429 return PLDM_ERROR_INVALID_DATA;
1430 }
1431
1432 if (payload_length != sizeof(*verify_result)) {
1433 return PLDM_ERROR_INVALID_LENGTH;
1434 }
1435
1436 *verify_result = msg->payload[0];
1437 return PLDM_SUCCESS;
1438}
1439
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301440LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301441int encode_verify_complete_resp(uint8_t instance_id, uint8_t completion_code,
1442 struct pldm_msg *msg, size_t payload_length)
1443{
1444 if (msg == NULL) {
1445 return PLDM_ERROR_INVALID_DATA;
1446 }
1447
1448 if (payload_length != sizeof(completion_code)) {
1449 return PLDM_ERROR_INVALID_LENGTH;
1450 }
1451
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301452 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301453 header.instance = instance_id;
1454 header.msg_type = PLDM_RESPONSE;
1455 header.pldm_type = PLDM_FWUP;
1456 header.command = PLDM_VERIFY_COMPLETE;
1457 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1458 if (rc) {
1459 return rc;
1460 }
1461
1462 msg->payload[0] = completion_code;
1463
1464 return PLDM_SUCCESS;
1465}
1466
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301467LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301468int decode_apply_complete_req(const struct pldm_msg *msg, size_t payload_length,
1469 uint8_t *apply_result,
1470 bitfield16_t *comp_activation_methods_modification)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301471{
1472 if (msg == NULL || apply_result == NULL ||
1473 comp_activation_methods_modification == NULL) {
1474 return PLDM_ERROR_INVALID_DATA;
1475 }
1476
1477 if (payload_length != sizeof(struct pldm_apply_complete_req)) {
1478 return PLDM_ERROR_INVALID_LENGTH;
1479 }
1480
1481 struct pldm_apply_complete_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301482 (struct pldm_apply_complete_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301483
1484 *apply_result = request->apply_result;
1485 comp_activation_methods_modification->value =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301486 le16toh(request->comp_activation_methods_modification.value);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301487
1488 if ((*apply_result != PLDM_FWUP_APPLY_SUCCESS_WITH_ACTIVATION_METHOD) &&
1489 comp_activation_methods_modification->value) {
1490 return PLDM_ERROR_INVALID_DATA;
1491 }
1492
1493 return PLDM_SUCCESS;
1494}
1495
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301496LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301497int encode_apply_complete_resp(uint8_t instance_id, uint8_t completion_code,
1498 struct pldm_msg *msg, size_t payload_length)
1499{
1500 if (msg == NULL) {
1501 return PLDM_ERROR_INVALID_DATA;
1502 }
1503
1504 if (payload_length != sizeof(completion_code)) {
1505 return PLDM_ERROR_INVALID_LENGTH;
1506 }
1507
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301508 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301509 header.instance = instance_id;
1510 header.msg_type = PLDM_RESPONSE;
1511 header.pldm_type = PLDM_FWUP;
1512 header.command = PLDM_APPLY_COMPLETE;
1513 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1514 if (rc) {
1515 return rc;
1516 }
1517
1518 msg->payload[0] = completion_code;
1519
1520 return PLDM_SUCCESS;
1521}
1522
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301523LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301524int encode_activate_firmware_req(uint8_t instance_id,
1525 bool8_t self_contained_activation_req,
1526 struct pldm_msg *msg, size_t payload_length)
1527{
1528 if (msg == NULL) {
1529 return PLDM_ERROR_INVALID_DATA;
1530 }
1531
1532 if (payload_length != sizeof(struct pldm_activate_firmware_req)) {
1533 return PLDM_ERROR_INVALID_LENGTH;
1534 }
1535
1536 if (!is_self_contained_activation_req_valid(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301537 self_contained_activation_req)) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301538 return PLDM_ERROR_INVALID_DATA;
1539 }
1540
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301541 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301542 header.instance = instance_id;
1543 header.msg_type = PLDM_REQUEST;
1544 header.pldm_type = PLDM_FWUP;
1545 header.command = PLDM_ACTIVATE_FIRMWARE;
1546 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1547 if (rc) {
1548 return rc;
1549 }
1550
1551 struct pldm_activate_firmware_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301552 (struct pldm_activate_firmware_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301553
1554 request->self_contained_activation_req = self_contained_activation_req;
1555
1556 return PLDM_SUCCESS;
1557}
1558
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301559LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301560int decode_activate_firmware_resp(const struct pldm_msg *msg,
1561 size_t payload_length,
1562 uint8_t *completion_code,
1563 uint16_t *estimated_time_activation)
1564{
1565 if (msg == NULL || completion_code == NULL ||
1566 estimated_time_activation == NULL || !payload_length) {
1567 return PLDM_ERROR_INVALID_DATA;
1568 }
1569
1570 *completion_code = msg->payload[0];
1571 if (*completion_code != PLDM_SUCCESS) {
1572 return PLDM_SUCCESS;
1573 }
1574
1575 if (payload_length != sizeof(struct pldm_activate_firmware_resp)) {
1576 return PLDM_ERROR_INVALID_LENGTH;
1577 }
1578
1579 struct pldm_activate_firmware_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301580 (struct pldm_activate_firmware_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301581
1582 *estimated_time_activation =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301583 le16toh(response->estimated_time_activation);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301584
1585 return PLDM_SUCCESS;
1586}
1587
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301588LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301589int encode_get_status_req(uint8_t instance_id, struct pldm_msg *msg,
1590 size_t payload_length)
1591{
1592 if (msg == NULL) {
1593 return PLDM_ERROR_INVALID_DATA;
1594 }
1595
1596 if (payload_length != PLDM_GET_STATUS_REQ_BYTES) {
1597 return PLDM_ERROR_INVALID_LENGTH;
1598 }
1599
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301600 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301601 header.instance = instance_id;
1602 header.msg_type = PLDM_REQUEST;
1603 header.pldm_type = PLDM_FWUP;
1604 header.command = PLDM_GET_STATUS;
1605 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1606 if (rc) {
1607 return rc;
1608 }
1609
1610 return PLDM_SUCCESS;
1611}
1612
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301613LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301614int decode_get_status_resp(const struct pldm_msg *msg, size_t payload_length,
1615 uint8_t *completion_code, uint8_t *current_state,
1616 uint8_t *previous_state, uint8_t *aux_state,
1617 uint8_t *aux_state_status, uint8_t *progress_percent,
1618 uint8_t *reason_code,
1619 bitfield32_t *update_option_flags_enabled)
1620{
1621 if (msg == NULL || completion_code == NULL || current_state == NULL ||
1622 previous_state == NULL || aux_state == NULL ||
1623 aux_state_status == NULL || progress_percent == NULL ||
1624 reason_code == NULL || update_option_flags_enabled == NULL ||
1625 !payload_length) {
1626 return PLDM_ERROR_INVALID_DATA;
1627 }
1628
1629 *completion_code = msg->payload[0];
1630 if (*completion_code != PLDM_SUCCESS) {
1631 return PLDM_SUCCESS;
1632 }
1633
1634 if (payload_length != sizeof(struct pldm_get_status_resp)) {
1635 return PLDM_ERROR_INVALID_LENGTH;
1636 }
1637 struct pldm_get_status_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301638 (struct pldm_get_status_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301639
1640 if (!is_state_valid(response->current_state)) {
1641 return PLDM_ERROR_INVALID_DATA;
1642 }
1643 if (!is_state_valid(response->previous_state)) {
1644 return PLDM_ERROR_INVALID_DATA;
1645 }
1646 if (!is_aux_state_valid(response->aux_state)) {
1647 return PLDM_ERROR_INVALID_DATA;
1648 }
1649 if (!is_aux_state_status_valid(response->aux_state_status)) {
1650 return PLDM_ERROR_INVALID_DATA;
1651 }
1652 if (response->progress_percent > PLDM_FWUP_MAX_PROGRESS_PERCENT) {
1653 return PLDM_ERROR_INVALID_DATA;
1654 }
1655 if (!is_reason_code_valid(response->reason_code)) {
1656 return PLDM_ERROR_INVALID_DATA;
1657 }
1658
1659 if ((response->current_state == PLDM_FD_STATE_IDLE) ||
1660 (response->current_state == PLDM_FD_STATE_LEARN_COMPONENTS) ||
1661 (response->current_state == PLDM_FD_STATE_READY_XFER)) {
1662 if (response->aux_state !=
1663 PLDM_FD_IDLE_LEARN_COMPONENTS_READ_XFER) {
1664 return PLDM_ERROR_INVALID_DATA;
1665 }
1666 }
1667
1668 *current_state = response->current_state;
1669 *previous_state = response->previous_state;
1670 *aux_state = response->aux_state;
1671 *aux_state_status = response->aux_state_status;
1672 *progress_percent = response->progress_percent;
1673 *reason_code = response->reason_code;
1674 update_option_flags_enabled->value =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301675 le32toh(response->update_option_flags_enabled.value);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301676
1677 return PLDM_SUCCESS;
1678}
1679
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301680LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301681int encode_cancel_update_component_req(uint8_t instance_id,
1682 struct pldm_msg *msg,
1683 size_t payload_length)
1684{
1685 if (msg == NULL) {
1686 return PLDM_ERROR_INVALID_DATA;
1687 }
1688
1689 if (payload_length != PLDM_CANCEL_UPDATE_COMPONENT_REQ_BYTES) {
1690 return PLDM_ERROR_INVALID_LENGTH;
1691 }
1692
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301693 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301694 header.instance = instance_id;
1695 header.msg_type = PLDM_REQUEST;
1696 header.pldm_type = PLDM_FWUP;
1697 header.command = PLDM_CANCEL_UPDATE_COMPONENT;
1698 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1699 if (rc) {
1700 return rc;
1701 }
1702
1703 return PLDM_SUCCESS;
1704}
1705
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301706LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301707int decode_cancel_update_component_resp(const struct pldm_msg *msg,
1708 size_t payload_length,
1709 uint8_t *completion_code)
1710{
1711 if (msg == NULL || completion_code == NULL) {
1712 return PLDM_ERROR_INVALID_DATA;
1713 }
1714
1715 if (payload_length != sizeof(*completion_code)) {
1716 return PLDM_ERROR_INVALID_LENGTH;
1717 }
1718
1719 *completion_code = msg->payload[0];
1720 return PLDM_SUCCESS;
1721}
1722
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301723LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301724int encode_cancel_update_req(uint8_t instance_id, struct pldm_msg *msg,
1725 size_t payload_length)
1726{
1727 if (msg == NULL) {
1728 return PLDM_ERROR_INVALID_DATA;
1729 }
1730
1731 if (payload_length != PLDM_CANCEL_UPDATE_REQ_BYTES) {
1732 return PLDM_ERROR_INVALID_LENGTH;
1733 }
1734
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301735 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +09301736 header.instance = instance_id;
1737 header.msg_type = PLDM_REQUEST;
1738 header.pldm_type = PLDM_FWUP;
1739 header.command = PLDM_CANCEL_UPDATE;
1740 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
1741 if (rc) {
1742 return rc;
1743 }
1744
1745 return PLDM_SUCCESS;
1746}
1747
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09301748LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +09301749int decode_cancel_update_resp(const struct pldm_msg *msg, size_t payload_length,
1750 uint8_t *completion_code,
1751 bool8_t *non_functioning_component_indication,
1752 bitfield64_t *non_functioning_component_bitmap)
1753{
1754 if (msg == NULL || completion_code == NULL ||
1755 non_functioning_component_indication == NULL ||
1756 non_functioning_component_bitmap == NULL || !payload_length) {
1757 return PLDM_ERROR_INVALID_DATA;
1758 }
1759
1760 *completion_code = msg->payload[0];
1761 if (*completion_code != PLDM_SUCCESS) {
1762 return PLDM_SUCCESS;
1763 }
1764
1765 if (payload_length != sizeof(struct pldm_cancel_update_resp)) {
1766 return PLDM_ERROR_INVALID_LENGTH;
1767 }
1768 struct pldm_cancel_update_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301769 (struct pldm_cancel_update_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301770
1771 if (!is_non_functioning_component_indication_valid(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301772 response->non_functioning_component_indication)) {
Andrew Jeffery9c766792022-08-10 23:12:49 +09301773 return PLDM_ERROR_INVALID_DATA;
1774 }
1775
1776 *non_functioning_component_indication =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301777 response->non_functioning_component_indication;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301778
1779 if (*non_functioning_component_indication) {
1780 non_functioning_component_bitmap->value =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +09301781 le64toh(response->non_functioning_component_bitmap);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301782 }
1783
1784 return PLDM_SUCCESS;
1785}