blob: 3b881e514a1d4d8c21e57c7a4859b311471d1fc2 [file] [log] [blame]
gokulsanker138ceba2021-04-05 13:25:25 +05301#include "firmware_update.h"
gokulsankereca3e192021-04-05 14:57:41 +05302#include <endian.h>
gokulsankere1fb7a82021-04-05 16:09:29 +05303#include <string.h>
gokulsanker138ceba2021-04-05 13:25:25 +05304
Tom Joseph763b51e2021-06-05 04:50:47 -07005/** @brief Check whether string type value is valid
6 *
7 * @return true if string type value is valid, false if not
8 */
9static bool is_string_type_valid(uint8_t string_type)
10{
11 switch (string_type) {
12 case PLDM_STR_TYPE_UNKNOWN:
Tom Josephbc44b7b2021-06-19 01:13:52 -070013 return false;
Tom Joseph763b51e2021-06-05 04:50:47 -070014 case PLDM_STR_TYPE_ASCII:
15 case PLDM_STR_TYPE_UTF_8:
16 case PLDM_STR_TYPE_UTF_16:
17 case PLDM_STR_TYPE_UTF_16LE:
18 case PLDM_STR_TYPE_UTF_16BE:
19 return true;
20 default:
21 return false;
22 }
23}
24
25/** @brief Return the length of the descriptor type described in firmware update
26 * specification
27 *
28 * @return length of the descriptor type if descriptor type is valid else
29 * return 0
30 */
31static uint16_t get_descriptor_type_length(uint16_t descriptor_type)
32{
33 switch (descriptor_type) {
34
35 case PLDM_FWUP_PCI_VENDOR_ID:
36 return PLDM_FWUP_PCI_VENDOR_ID_LENGTH;
37 case PLDM_FWUP_IANA_ENTERPRISE_ID:
38 return PLDM_FWUP_IANA_ENTERPRISE_ID_LENGTH;
39 case PLDM_FWUP_UUID:
40 return PLDM_FWUP_UUID_LENGTH;
41 case PLDM_FWUP_PNP_VENDOR_ID:
42 return PLDM_FWUP_PNP_VENDOR_ID_LENGTH;
43 case PLDM_FWUP_ACPI_VENDOR_ID:
44 return PLDM_FWUP_ACPI_VENDOR_ID_LENGTH;
45 case PLDM_FWUP_IEEE_ASSIGNED_COMPANY_ID:
46 return PLDM_FWUP_IEEE_ASSIGNED_COMPANY_ID_LENGTH;
47 case PLDM_FWUP_SCSI_VENDOR_ID:
48 return PLDM_FWUP_SCSI_VENDOR_ID_LENGTH;
49 case PLDM_FWUP_PCI_DEVICE_ID:
50 return PLDM_FWUP_PCI_DEVICE_ID_LENGTH;
51 case PLDM_FWUP_PCI_SUBSYSTEM_VENDOR_ID:
52 return PLDM_FWUP_PCI_SUBSYSTEM_VENDOR_ID_LENGTH;
53 case PLDM_FWUP_PCI_SUBSYSTEM_ID:
54 return PLDM_FWUP_PCI_SUBSYSTEM_ID_LENGTH;
55 case PLDM_FWUP_PCI_REVISION_ID:
56 return PLDM_FWUP_PCI_REVISION_ID_LENGTH;
57 case PLDM_FWUP_PNP_PRODUCT_IDENTIFIER:
58 return PLDM_FWUP_PNP_PRODUCT_IDENTIFIER_LENGTH;
59 case PLDM_FWUP_ACPI_PRODUCT_IDENTIFIER:
60 return PLDM_FWUP_ACPI_PRODUCT_IDENTIFIER_LENGTH;
61 case PLDM_FWUP_ASCII_MODEL_NUMBER_LONG_STRING:
62 return PLDM_FWUP_ASCII_MODEL_NUMBER_LONG_STRING_LENGTH;
63 case PLDM_FWUP_ASCII_MODEL_NUMBER_SHORT_STRING:
64 return PLDM_FWUP_ASCII_MODEL_NUMBER_SHORT_STRING_LENGTH;
65 case PLDM_FWUP_SCSI_PRODUCT_ID:
66 return PLDM_FWUP_SCSI_PRODUCT_ID_LENGTH;
67 case PLDM_FWUP_UBM_CONTROLLER_DEVICE_CODE:
68 return PLDM_FWUP_UBM_CONTROLLER_DEVICE_CODE_LENGTH;
69 default:
70 return 0;
71 }
72}
73
gokulsanker566784b2021-04-05 17:47:04 +053074/** @brief Check whether ComponentResponse is valid
75 *
76 * @return true if ComponentResponse is valid, false if not
77 */
78static bool is_comp_resp_valid(uint8_t comp_resp)
79{
80 switch (comp_resp) {
81 case PLDM_CR_COMP_CAN_BE_UPDATED:
82 case PLDM_CR_COMP_MAY_BE_UPDATEABLE:
83 return true;
84
85 default:
86 return false;
87 }
88}
89
90/** @brief Check whether ComponentResponseCode is valid
91 *
92 * @return true if ComponentResponseCode is valid, false if not
93 */
94static bool is_comp_resp_code_valid(uint8_t comp_resp_code)
95{
96 switch (comp_resp_code) {
97 case PLDM_CRC_COMP_CAN_BE_UPDATED:
98 case PLDM_CRC_COMP_COMPARISON_STAMP_IDENTICAL:
99 case PLDM_CRC_COMP_COMPARISON_STAMP_LOWER:
100 case PLDM_CRC_INVALID_COMP_COMPARISON_STAMP:
101 case PLDM_CRC_COMP_CONFLICT:
102 case PLDM_CRC_COMP_PREREQUISITES_NOT_MET:
103 case PLDM_CRC_COMP_NOT_SUPPORTED:
104 case PLDM_CRC_COMP_SECURITY_RESTRICTIONS:
105 case PLDM_CRC_INCOMPLETE_COMP_IMAGE_SET:
106 case PLDM_CRC_ACTIVE_IMAGE_NOT_UPDATEABLE_SUBSEQUENTLY:
107 case PLDM_CRC_COMP_VER_STR_IDENTICAL:
108 case PLDM_CRC_COMP_VER_STR_LOWER:
109 return true;
110
111 default:
112 if (comp_resp_code >=
113 PLDM_CRC_VENDOR_COMP_RESP_CODE_RANGE_MIN &&
114 comp_resp_code <=
115 PLDM_CRC_VENDOR_COMP_RESP_CODE_RANGE_MAX) {
116 return true;
117 }
118 return false;
119 }
120}
121
gokulsanker4b533f22021-04-22 12:53:00 +0530122/** @brief Check whether ComponentCompatibilityResponse is valid
123 *
124 * @return true if ComponentCompatibilityResponse is valid, false if not
125 */
126static bool is_comp_compatibility_resp_valid(uint8_t comp_compatibility_resp)
127{
128 switch (comp_compatibility_resp) {
129 case PLDM_CCR_COMP_CAN_BE_UPDATED:
130 case PLDM_CCR_COMP_CANNOT_BE_UPDATED:
131 return true;
132
133 default:
134 return false;
135 }
136}
137
138/** @brief Check whether ComponentCompatibilityResponse Code is valid
139 *
140 * @return true if ComponentCompatibilityResponse Code is valid, false if not
141 */
142static bool
143is_comp_compatibility_resp_code_valid(uint8_t comp_compatibility_resp_code)
144{
145 switch (comp_compatibility_resp_code) {
146 case PLDM_CCRC_NO_RESPONSE_CODE:
147 case PLDM_CCRC_COMP_COMPARISON_STAMP_IDENTICAL:
148 case PLDM_CCRC_COMP_COMPARISON_STAMP_LOWER:
149 case PLDM_CCRC_INVALID_COMP_COMPARISON_STAMP:
150 case PLDM_CCRC_COMP_CONFLICT:
151 case PLDM_CCRC_COMP_PREREQUISITES_NOT_MET:
152 case PLDM_CCRC_COMP_NOT_SUPPORTED:
153 case PLDM_CCRC_COMP_SECURITY_RESTRICTIONS:
154 case PLDM_CRC_INCOMPLETE_COMP_IMAGE_SET:
155 case PLDM_CCRC_COMP_INFO_NO_MATCH:
156 case PLDM_CCRC_COMP_VER_STR_IDENTICAL:
157 case PLDM_CCRC_COMP_VER_STR_LOWER:
158 return true;
159
160 default:
161 if (comp_compatibility_resp_code >=
162 PLDM_CCRC_VENDOR_COMP_RESP_CODE_RANGE_MIN &&
163 comp_compatibility_resp_code <=
164 PLDM_CCRC_VENDOR_COMP_RESP_CODE_RANGE_MAX) {
165 return true;
166 }
167 return false;
168 }
169}
170
Tom Joseph568e4702021-06-07 22:15:49 -0700171int decode_pldm_package_header_info(
172 const uint8_t *data, size_t length,
173 struct pldm_package_header_information *package_header_info,
174 struct variable_field *package_version_str)
175{
176 if (data == NULL || package_header_info == NULL ||
177 package_version_str == NULL) {
178 return PLDM_ERROR_INVALID_DATA;
179 }
180
181 if (length < sizeof(struct pldm_package_header_information)) {
182 return PLDM_ERROR_INVALID_LENGTH;
183 }
184
185 struct pldm_package_header_information *data_header =
186 (struct pldm_package_header_information *)(data);
187
188 if (!is_string_type_valid(data_header->package_version_string_type) ||
189 (data_header->package_version_string_length == 0)) {
190 return PLDM_ERROR_INVALID_DATA;
191 }
192
193 if (length < sizeof(struct pldm_package_header_information) +
194 data_header->package_version_string_length) {
195 return PLDM_ERROR_INVALID_LENGTH;
196 }
197
198 if ((data_header->component_bitmap_bit_length %
199 PLDM_FWUP_COMPONENT_BITMAP_MULTIPLE) != 0) {
200 return PLDM_ERROR_INVALID_DATA;
201 }
202
203 memcpy(package_header_info->uuid, data_header->uuid,
204 sizeof(data_header->uuid));
205 package_header_info->package_header_format_version =
206 data_header->package_header_format_version;
207 package_header_info->package_header_size =
208 le16toh(data_header->package_header_size);
209 memcpy(package_header_info->timestamp104, data_header->timestamp104,
210 sizeof(data_header->timestamp104));
211 package_header_info->component_bitmap_bit_length =
212 le16toh(data_header->component_bitmap_bit_length);
213 package_header_info->package_version_string_type =
214 data_header->package_version_string_type;
215 package_header_info->package_version_string_length =
216 data_header->package_version_string_length;
217 package_version_str->ptr =
218 data + sizeof(struct pldm_package_header_information);
219 package_version_str->length =
220 package_header_info->package_version_string_length;
221
222 return PLDM_SUCCESS;
223}
224
Tom Joseph64af3452021-06-08 04:05:28 -0700225int decode_firmware_device_id_record(
226 const uint8_t *data, size_t length, uint16_t component_bitmap_bit_length,
227 struct pldm_firmware_device_id_record *fw_device_id_record,
228 struct variable_field *applicable_components,
229 struct variable_field *comp_image_set_version_str,
230 struct variable_field *record_descriptors,
231 struct variable_field *fw_device_pkg_data)
232{
233 if (data == NULL || fw_device_id_record == NULL ||
234 applicable_components == NULL ||
235 comp_image_set_version_str == NULL || record_descriptors == NULL ||
236 fw_device_pkg_data == NULL) {
237 return PLDM_ERROR_INVALID_DATA;
238 }
239
240 if (length < sizeof(struct pldm_firmware_device_id_record)) {
241 return PLDM_ERROR_INVALID_LENGTH;
242 }
243
244 if ((component_bitmap_bit_length %
245 PLDM_FWUP_COMPONENT_BITMAP_MULTIPLE) != 0) {
246 return PLDM_ERROR_INVALID_DATA;
247 }
248
249 struct pldm_firmware_device_id_record *data_record =
250 (struct pldm_firmware_device_id_record *)(data);
251
252 if (!is_string_type_valid(
253 data_record->comp_image_set_version_string_type) ||
254 (data_record->comp_image_set_version_string_length == 0)) {
255 return PLDM_ERROR_INVALID_DATA;
256 }
257
258 fw_device_id_record->record_length =
259 le16toh(data_record->record_length);
260 fw_device_id_record->descriptor_count = data_record->descriptor_count;
261 fw_device_id_record->device_update_option_flags.value =
262 le32toh(data_record->device_update_option_flags.value);
263 fw_device_id_record->comp_image_set_version_string_type =
264 data_record->comp_image_set_version_string_type;
265 fw_device_id_record->comp_image_set_version_string_length =
266 data_record->comp_image_set_version_string_length;
267 fw_device_id_record->fw_device_pkg_data_length =
268 le16toh(data_record->fw_device_pkg_data_length);
269
270 if (length < fw_device_id_record->record_length) {
271 return PLDM_ERROR_INVALID_LENGTH;
272 }
273
274 uint16_t applicable_components_length =
275 component_bitmap_bit_length / PLDM_FWUP_COMPONENT_BITMAP_MULTIPLE;
276 uint16_t calc_min_record_length =
277 sizeof(struct pldm_firmware_device_id_record) +
278 applicable_components_length +
279 data_record->comp_image_set_version_string_length +
280 PLDM_FWUP_DEVICE_DESCRIPTOR_MIN_LEN +
281 fw_device_id_record->fw_device_pkg_data_length;
282
283 if (fw_device_id_record->record_length < calc_min_record_length) {
284 return PLDM_ERROR_INVALID_LENGTH;
285 }
286
287 applicable_components->ptr =
288 data + sizeof(struct pldm_firmware_device_id_record);
289 applicable_components->length = applicable_components_length;
290
291 comp_image_set_version_str->ptr =
292 applicable_components->ptr + applicable_components->length;
293 comp_image_set_version_str->length =
294 fw_device_id_record->comp_image_set_version_string_length;
295
296 record_descriptors->ptr = comp_image_set_version_str->ptr +
297 comp_image_set_version_str->length;
298 record_descriptors->length =
299 fw_device_id_record->record_length -
300 sizeof(struct pldm_firmware_device_id_record) -
301 applicable_components_length -
302 fw_device_id_record->comp_image_set_version_string_length -
303 fw_device_id_record->fw_device_pkg_data_length;
304
305 if (fw_device_id_record->fw_device_pkg_data_length) {
306 fw_device_pkg_data->ptr =
307 record_descriptors->ptr + record_descriptors->length;
308 fw_device_pkg_data->length =
309 fw_device_id_record->fw_device_pkg_data_length;
310 }
311
312 return PLDM_SUCCESS;
313}
314
Tom Joseph763b51e2021-06-05 04:50:47 -0700315int decode_descriptor_type_length_value(const uint8_t *data, size_t length,
316 uint16_t *descriptor_type,
317 struct variable_field *descriptor_data)
318{
319 uint16_t descriptor_length = 0;
320
321 if (data == NULL || descriptor_type == NULL ||
322 descriptor_data == NULL) {
323 return PLDM_ERROR_INVALID_DATA;
324 }
325
326 if (length < PLDM_FWUP_DEVICE_DESCRIPTOR_MIN_LEN) {
327 return PLDM_ERROR_INVALID_LENGTH;
328 }
329
330 struct pldm_descriptor_tlv *entry =
331 (struct pldm_descriptor_tlv *)(data);
332
333 *descriptor_type = le16toh(entry->descriptor_type);
334 descriptor_length = le16toh(entry->descriptor_length);
335 if (*descriptor_type != PLDM_FWUP_VENDOR_DEFINED) {
336 if (descriptor_length !=
337 get_descriptor_type_length(*descriptor_type)) {
338 return PLDM_ERROR_INVALID_LENGTH;
339 }
340 }
341
342 if (length < (sizeof(*descriptor_type) + sizeof(descriptor_length) +
343 descriptor_length)) {
344 return PLDM_ERROR_INVALID_LENGTH;
345 }
346
347 descriptor_data->ptr = entry->descriptor_data;
348 descriptor_data->length = descriptor_length;
349
350 return PLDM_SUCCESS;
351}
352
353int decode_vendor_defined_descriptor_value(
354 const uint8_t *data, size_t length, uint8_t *descriptor_title_str_type,
355 struct variable_field *descriptor_title_str,
356 struct variable_field *descriptor_data)
357{
358 if (data == NULL || descriptor_title_str_type == NULL ||
359 descriptor_title_str == NULL || descriptor_data == NULL) {
360 return PLDM_ERROR_INVALID_DATA;
361 }
362
363 if (length < sizeof(struct pldm_vendor_defined_descriptor_title_data)) {
364 return PLDM_ERROR_INVALID_LENGTH;
365 }
366
367 struct pldm_vendor_defined_descriptor_title_data *entry =
368 (struct pldm_vendor_defined_descriptor_title_data *)(data);
369 if (!is_string_type_valid(
370 entry->vendor_defined_descriptor_title_str_type) ||
371 (entry->vendor_defined_descriptor_title_str_len == 0)) {
372 return PLDM_ERROR_INVALID_DATA;
373 }
374
375 // Assuming atleast 1 byte of VendorDefinedDescriptorData
376 if (length < (sizeof(struct pldm_vendor_defined_descriptor_title_data) +
377 entry->vendor_defined_descriptor_title_str_len)) {
378 return PLDM_ERROR_INVALID_LENGTH;
379 }
380
381 *descriptor_title_str_type =
382 entry->vendor_defined_descriptor_title_str_type;
383 descriptor_title_str->ptr = entry->vendor_defined_descriptor_title_str;
384 descriptor_title_str->length =
385 entry->vendor_defined_descriptor_title_str_len;
386
387 descriptor_data->ptr =
388 descriptor_title_str->ptr + descriptor_title_str->length;
389 descriptor_data->length =
390 length - sizeof(entry->vendor_defined_descriptor_title_str_type) -
391 sizeof(entry->vendor_defined_descriptor_title_str_len) -
392 descriptor_title_str->length;
393
394 return PLDM_SUCCESS;
395}
396
Tom Joseph58cc1722021-06-09 07:15:49 -0700397int decode_pldm_comp_image_info(
398 const uint8_t *data, size_t length,
399 struct pldm_component_image_information *pldm_comp_image_info,
400 struct variable_field *comp_version_str)
401{
402 if (data == NULL || pldm_comp_image_info == NULL ||
403 comp_version_str == NULL) {
404 return PLDM_ERROR_INVALID_DATA;
405 }
406
407 if (length < sizeof(struct pldm_component_image_information)) {
408 return PLDM_ERROR_INVALID_LENGTH;
409 }
410
411 struct pldm_component_image_information *data_header =
412 (struct pldm_component_image_information *)(data);
413
414 if (!is_string_type_valid(data_header->comp_version_string_type) ||
415 (data_header->comp_version_string_length == 0)) {
416 return PLDM_ERROR_INVALID_DATA;
417 }
418
419 if (length < sizeof(struct pldm_component_image_information) +
420 data_header->comp_version_string_length) {
421 return PLDM_ERROR_INVALID_LENGTH;
422 }
423
424 pldm_comp_image_info->comp_classification =
425 le16toh(data_header->comp_classification);
426 pldm_comp_image_info->comp_identifier =
427 le16toh(data_header->comp_identifier);
428 pldm_comp_image_info->comp_comparison_stamp =
429 le32toh(data_header->comp_comparison_stamp);
430 pldm_comp_image_info->comp_options.value =
431 le16toh(data_header->comp_options.value);
432 pldm_comp_image_info->requested_comp_activation_method.value =
433 le16toh(data_header->requested_comp_activation_method.value);
434 pldm_comp_image_info->comp_location_offset =
435 le32toh(data_header->comp_location_offset);
436 pldm_comp_image_info->comp_size = le32toh(data_header->comp_size);
437 pldm_comp_image_info->comp_version_string_type =
438 data_header->comp_version_string_type;
439 pldm_comp_image_info->comp_version_string_length =
440 data_header->comp_version_string_length;
441
442 if ((pldm_comp_image_info->comp_options.bits.bit1 == false &&
443 pldm_comp_image_info->comp_comparison_stamp !=
444 PLDM_FWUP_INVALID_COMPONENT_COMPARISON_TIMESTAMP)) {
445 return PLDM_ERROR_INVALID_DATA;
446 }
447
448 if (pldm_comp_image_info->comp_location_offset == 0 ||
449 pldm_comp_image_info->comp_size == 0) {
450 return PLDM_ERROR_INVALID_DATA;
451 }
452
453 comp_version_str->ptr =
454 data + sizeof(struct pldm_component_image_information);
455 comp_version_str->length =
456 pldm_comp_image_info->comp_version_string_length;
457
458 return PLDM_SUCCESS;
459}
460
gokulsanker138ceba2021-04-05 13:25:25 +0530461int encode_query_device_identifiers_req(uint8_t instance_id,
462 size_t payload_length,
463 struct pldm_msg *msg)
464{
465 if (msg == NULL) {
466 return PLDM_ERROR_INVALID_DATA;
467 }
468
469 if (payload_length != PLDM_QUERY_DEVICE_IDENTIFIERS_REQ_BYTES) {
470 return PLDM_ERROR_INVALID_LENGTH;
471 }
472
473 return encode_pldm_header_only(PLDM_REQUEST, instance_id, PLDM_FWUP,
474 PLDM_QUERY_DEVICE_IDENTIFIERS, msg);
475}
gokulsankereca3e192021-04-05 14:57:41 +0530476
477int decode_query_device_identifiers_resp(const struct pldm_msg *msg,
478 size_t payload_length,
479 uint8_t *completion_code,
480 uint32_t *device_identifiers_len,
481 uint8_t *descriptor_count,
482 uint8_t **descriptor_data)
483{
484 if (msg == NULL || completion_code == NULL ||
485 device_identifiers_len == NULL || descriptor_count == NULL ||
486 descriptor_data == NULL) {
487 return PLDM_ERROR_INVALID_DATA;
488 }
489
490 *completion_code = msg->payload[0];
491 if (PLDM_SUCCESS != *completion_code) {
492 return PLDM_SUCCESS;
493 }
494
495 if (payload_length <
496 sizeof(struct pldm_query_device_identifiers_resp)) {
497 return PLDM_ERROR_INVALID_LENGTH;
498 }
499
500 struct pldm_query_device_identifiers_resp *response =
501 (struct pldm_query_device_identifiers_resp *)msg->payload;
502 *device_identifiers_len = le32toh(response->device_identifiers_len);
503
504 if (*device_identifiers_len < PLDM_FWUP_DEVICE_DESCRIPTOR_MIN_LEN) {
505 return PLDM_ERROR_INVALID_LENGTH;
506 }
507
508 if (payload_length !=
509 sizeof(struct pldm_query_device_identifiers_resp) +
510 *device_identifiers_len) {
511 return PLDM_ERROR_INVALID_LENGTH;
512 }
513 *descriptor_count = response->descriptor_count;
514
515 if (*descriptor_count == 0) {
516 return PLDM_ERROR_INVALID_DATA;
517 }
gokulsankereca3e192021-04-05 14:57:41 +0530518 *descriptor_data =
519 (uint8_t *)(msg->payload +
520 sizeof(struct pldm_query_device_identifiers_resp));
521 return PLDM_SUCCESS;
522}
gokulsanker981fbfb2021-04-05 15:17:25 +0530523
524int encode_get_firmware_parameters_req(uint8_t instance_id,
525 size_t payload_length,
526 struct pldm_msg *msg)
527{
528 if (msg == NULL) {
529 return PLDM_ERROR_INVALID_DATA;
530 }
531
532 if (payload_length != PLDM_GET_FIRMWARE_PARAMETERS_REQ_BYTES) {
533 return PLDM_ERROR_INVALID_LENGTH;
534 }
535
536 return encode_pldm_header_only(PLDM_REQUEST, instance_id, PLDM_FWUP,
537 PLDM_GET_FIRMWARE_PARAMETERS, msg);
538}
gokulsanker22fbb342021-04-05 15:55:06 +0530539
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700540int decode_get_firmware_parameters_resp(
gokulsanker22fbb342021-04-05 15:55:06 +0530541 const struct pldm_msg *msg, size_t payload_length,
542 struct pldm_get_firmware_parameters_resp *resp_data,
543 struct variable_field *active_comp_image_set_ver_str,
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700544 struct variable_field *pending_comp_image_set_ver_str,
545 struct variable_field *comp_parameter_table)
gokulsanker22fbb342021-04-05 15:55:06 +0530546{
547 if (msg == NULL || resp_data == NULL ||
548 active_comp_image_set_ver_str == NULL ||
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700549 pending_comp_image_set_ver_str == NULL ||
Tom Joseph83a644c2021-06-22 22:25:25 -0700550 comp_parameter_table == NULL || !payload_length) {
gokulsanker22fbb342021-04-05 15:55:06 +0530551 return PLDM_ERROR_INVALID_DATA;
552 }
553
Tom Joseph83a644c2021-06-22 22:25:25 -0700554 resp_data->completion_code = msg->payload[0];
555 if (PLDM_SUCCESS != resp_data->completion_code) {
556 return PLDM_SUCCESS;
557 }
558
gokulsanker22fbb342021-04-05 15:55:06 +0530559 if (payload_length < sizeof(struct pldm_get_firmware_parameters_resp)) {
560 return PLDM_ERROR_INVALID_LENGTH;
561 }
562
563 struct pldm_get_firmware_parameters_resp *response =
564 (struct pldm_get_firmware_parameters_resp *)msg->payload;
565
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700566 if (!is_string_type_valid(
567 response->active_comp_image_set_ver_str_type) ||
568 (response->active_comp_image_set_ver_str_len == 0)) {
gokulsanker22fbb342021-04-05 15:55:06 +0530569 return PLDM_ERROR_INVALID_DATA;
570 }
571
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700572 if (response->pending_comp_image_set_ver_str_len == 0) {
573 if (response->pending_comp_image_set_ver_str_type !=
574 PLDM_STR_TYPE_UNKNOWN) {
575 return PLDM_ERROR_INVALID_DATA;
576 }
577 } else {
578 if (!is_string_type_valid(
579 response->pending_comp_image_set_ver_str_type)) {
580 return PLDM_ERROR_INVALID_DATA;
581 }
582 }
gokulsanker22fbb342021-04-05 15:55:06 +0530583
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700584 size_t partial_response_length =
585 sizeof(struct pldm_get_firmware_parameters_resp) +
586 response->active_comp_image_set_ver_str_len +
587 response->pending_comp_image_set_ver_str_len;
588
589 if (payload_length < partial_response_length) {
gokulsanker22fbb342021-04-05 15:55:06 +0530590 return PLDM_ERROR_INVALID_LENGTH;
591 }
592
593 resp_data->capabilities_during_update.value =
594 le32toh(response->capabilities_during_update.value);
gokulsanker22fbb342021-04-05 15:55:06 +0530595 resp_data->comp_count = le16toh(response->comp_count);
gokulsanker22fbb342021-04-05 15:55:06 +0530596 resp_data->active_comp_image_set_ver_str_type =
597 response->active_comp_image_set_ver_str_type;
598 resp_data->active_comp_image_set_ver_str_len =
599 response->active_comp_image_set_ver_str_len;
600 resp_data->pending_comp_image_set_ver_str_type =
601 response->pending_comp_image_set_ver_str_type;
602 resp_data->pending_comp_image_set_ver_str_len =
603 response->pending_comp_image_set_ver_str_len;
604
605 active_comp_image_set_ver_str->ptr =
606 msg->payload + sizeof(struct pldm_get_firmware_parameters_resp);
607 active_comp_image_set_ver_str->length =
608 resp_data->active_comp_image_set_ver_str_len;
609
610 if (resp_data->pending_comp_image_set_ver_str_len != 0) {
611 pending_comp_image_set_ver_str->ptr =
612 msg->payload +
613 sizeof(struct pldm_get_firmware_parameters_resp) +
614 resp_data->active_comp_image_set_ver_str_len;
615 pending_comp_image_set_ver_str->length =
616 resp_data->pending_comp_image_set_ver_str_len;
617 } else {
618 pending_comp_image_set_ver_str->ptr = NULL;
619 pending_comp_image_set_ver_str->length = 0;
620 }
621
Tom Joseph3fd3eb82021-06-18 04:13:29 -0700622 if (payload_length > partial_response_length && resp_data->comp_count) {
623 comp_parameter_table->ptr =
624 msg->payload +
625 sizeof(struct pldm_get_firmware_parameters_resp) +
626 resp_data->active_comp_image_set_ver_str_len +
627 resp_data->pending_comp_image_set_ver_str_len;
628 comp_parameter_table->length =
629 payload_length - partial_response_length;
630 } else {
631 comp_parameter_table->ptr = NULL;
632 comp_parameter_table->length = 0;
633 }
634
gokulsanker22fbb342021-04-05 15:55:06 +0530635 return PLDM_SUCCESS;
636}
gokulsankere1fb7a82021-04-05 16:09:29 +0530637
638int decode_get_firmware_parameters_resp_comp_entry(
639 const uint8_t *data, size_t length,
640 struct pldm_component_parameter_entry *component_data,
641 struct variable_field *active_comp_ver_str,
642 struct variable_field *pending_comp_ver_str)
643{
644 if (data == NULL || component_data == NULL ||
645 active_comp_ver_str == NULL || pending_comp_ver_str == NULL) {
646 return PLDM_ERROR_INVALID_DATA;
647 }
648
649 if (length < sizeof(struct pldm_component_parameter_entry)) {
650 return PLDM_ERROR_INVALID_LENGTH;
651 }
652
653 struct pldm_component_parameter_entry *entry =
654 (struct pldm_component_parameter_entry *)(data);
655 if (entry->active_comp_ver_str_len == 0) {
656 return PLDM_ERROR_INVALID_LENGTH;
657 }
658
659 size_t entry_length = sizeof(struct pldm_component_parameter_entry) +
660 entry->active_comp_ver_str_len +
661 entry->pending_comp_ver_str_len;
662
Tom Josephc33c2f02021-06-22 21:58:56 -0700663 if (length < entry_length) {
gokulsankere1fb7a82021-04-05 16:09:29 +0530664 return PLDM_ERROR_INVALID_LENGTH;
665 }
666
667 component_data->comp_classification =
668 le16toh(entry->comp_classification);
669 component_data->comp_identifier = le16toh(entry->comp_identifier);
670 component_data->comp_classification_index =
671 entry->comp_classification_index;
672 component_data->active_comp_comparison_stamp =
673 le32toh(entry->active_comp_comparison_stamp);
674 component_data->active_comp_ver_str_type =
675 entry->active_comp_ver_str_type;
676 component_data->active_comp_ver_str_len =
677 entry->active_comp_ver_str_len;
678 memcpy(component_data->active_comp_release_date,
679 entry->active_comp_release_date,
680 sizeof(entry->active_comp_release_date));
681 component_data->pending_comp_comparison_stamp =
682 le32toh(entry->pending_comp_comparison_stamp);
683 component_data->pending_comp_ver_str_type =
684 entry->pending_comp_ver_str_type;
685 component_data->pending_comp_ver_str_len =
686 entry->pending_comp_ver_str_len;
687 memcpy(component_data->pending_comp_release_date,
688 entry->pending_comp_release_date,
689 sizeof(entry->pending_comp_release_date));
690 component_data->comp_activation_methods.value =
691 le16toh(entry->comp_activation_methods.value);
692 component_data->capabilities_during_update.value =
693 le32toh(entry->capabilities_during_update.value);
694
695 active_comp_ver_str->ptr =
696 data + sizeof(struct pldm_component_parameter_entry);
697 active_comp_ver_str->length = entry->active_comp_ver_str_len;
698
699 if (entry->pending_comp_ver_str_len != 0) {
700
701 pending_comp_ver_str->ptr =
702 data + sizeof(struct pldm_component_parameter_entry) +
703 entry->active_comp_ver_str_len;
704 pending_comp_ver_str->length = entry->pending_comp_ver_str_len;
705 } else {
706 pending_comp_ver_str->ptr = NULL;
707 pending_comp_ver_str->length = 0;
708 }
709 return PLDM_SUCCESS;
710}
gokulsankerd434edc2021-04-05 16:36:04 +0530711
712int encode_request_update_req(uint8_t instance_id, uint32_t max_transfer_size,
713 uint16_t num_of_comp,
714 uint8_t max_outstanding_transfer_req,
715 uint16_t pkg_data_len,
716 uint8_t comp_image_set_ver_str_type,
717 uint8_t comp_image_set_ver_str_len,
718 const struct variable_field *comp_img_set_ver_str,
719 struct pldm_msg *msg, size_t payload_length)
720{
721 if (comp_img_set_ver_str == NULL || comp_img_set_ver_str->ptr == NULL ||
722 msg == NULL) {
723 return PLDM_ERROR_INVALID_DATA;
724 }
725
726 if (payload_length != sizeof(struct pldm_request_update_req) +
727 comp_img_set_ver_str->length) {
728 return PLDM_ERROR_INVALID_LENGTH;
729 }
730
731 if ((comp_image_set_ver_str_len == 0) ||
732 (comp_image_set_ver_str_len != comp_img_set_ver_str->length)) {
733 return PLDM_ERROR_INVALID_DATA;
734 }
735
736 if ((max_transfer_size < PLDM_FWUP_BASELINE_TRANSFER_SIZE) ||
737 (max_outstanding_transfer_req < PLDM_FWUP_MIN_OUTSTANDING_REQ)) {
738 return PLDM_ERROR_INVALID_DATA;
739 }
740
741 if (!is_string_type_valid(comp_image_set_ver_str_type)) {
742 return PLDM_ERROR_INVALID_DATA;
743 }
744
745 struct pldm_header_info header = {0};
746 header.instance = instance_id;
747 header.msg_type = PLDM_REQUEST;
748 header.pldm_type = PLDM_FWUP;
749 header.command = PLDM_REQUEST_UPDATE;
750 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
751 if (rc) {
752 return rc;
753 }
754
755 struct pldm_request_update_req *request =
756 (struct pldm_request_update_req *)msg->payload;
757
758 request->max_transfer_size = htole32(max_transfer_size);
759 request->num_of_comp = htole16(num_of_comp);
760 request->max_outstanding_transfer_req = max_outstanding_transfer_req;
761 request->pkg_data_len = htole16(pkg_data_len);
762 request->comp_image_set_ver_str_type = comp_image_set_ver_str_type;
763 request->comp_image_set_ver_str_len = comp_image_set_ver_str_len;
764
765 memcpy(msg->payload + sizeof(struct pldm_request_update_req),
766 comp_img_set_ver_str->ptr, comp_img_set_ver_str->length);
767
768 return PLDM_SUCCESS;
gokulsanker611238c2021-04-05 16:50:44 +0530769}
770
771int decode_request_update_resp(const struct pldm_msg *msg,
772 size_t payload_length, uint8_t *completion_code,
773 uint16_t *fd_meta_data_len,
774 uint8_t *fd_will_send_pkg_data)
775{
776 if (msg == NULL || completion_code == NULL ||
777 fd_meta_data_len == NULL || fd_will_send_pkg_data == NULL ||
778 !payload_length) {
779 return PLDM_ERROR_INVALID_DATA;
780 }
781
782 *completion_code = msg->payload[0];
783 if (*completion_code != PLDM_SUCCESS) {
784 return PLDM_SUCCESS;
785 }
786
787 if (payload_length != sizeof(struct pldm_request_update_resp)) {
788 return PLDM_ERROR_INVALID_LENGTH;
789 }
790
791 struct pldm_request_update_resp *response =
792 (struct pldm_request_update_resp *)msg->payload;
793
794 *fd_meta_data_len = le16toh(response->fd_meta_data_len);
795 *fd_will_send_pkg_data = response->fd_will_send_pkg_data;
796
797 return PLDM_SUCCESS;
gokulsanker1b909d82021-04-05 17:26:02 +0530798}
799
800int encode_pass_component_table_req(
801 uint8_t instance_id, uint8_t transfer_flag, uint16_t comp_classification,
802 uint16_t comp_identifier, uint8_t comp_classification_index,
803 uint32_t comp_comparison_stamp, uint8_t comp_ver_str_type,
804 uint8_t comp_ver_str_len, const struct variable_field *comp_ver_str,
805 struct pldm_msg *msg, size_t payload_length)
806{
807 if (comp_ver_str == NULL || comp_ver_str->ptr == NULL || msg == NULL) {
808 return PLDM_ERROR_INVALID_DATA;
809 }
810
811 if (payload_length != sizeof(struct pldm_pass_component_table_req) +
812 comp_ver_str->length) {
813 return PLDM_ERROR_INVALID_LENGTH;
814 }
815
816 if ((comp_ver_str_len == 0) ||
817 (comp_ver_str_len != comp_ver_str->length)) {
818 return PLDM_ERROR_INVALID_DATA;
819 }
820
821 if (!is_transfer_flag_valid(transfer_flag)) {
822 return PLDM_INVALID_TRANSFER_OPERATION_FLAG;
823 }
824
825 if (!is_string_type_valid(comp_ver_str_type)) {
826 return PLDM_ERROR_INVALID_DATA;
827 }
828
829 struct pldm_header_info header = {0};
830 header.instance = instance_id;
831 header.msg_type = PLDM_REQUEST;
832 header.pldm_type = PLDM_FWUP;
833 header.command = PLDM_PASS_COMPONENT_TABLE;
834 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
835 if (rc) {
836 return rc;
837 }
838
839 struct pldm_pass_component_table_req *request =
840 (struct pldm_pass_component_table_req *)msg->payload;
841
842 request->transfer_flag = transfer_flag;
843 request->comp_classification = htole16(comp_classification);
844 request->comp_identifier = htole16(comp_identifier);
845 request->comp_classification_index = comp_classification_index;
846 request->comp_comparison_stamp = htole32(comp_comparison_stamp);
847 request->comp_ver_str_type = comp_ver_str_type;
848 request->comp_ver_str_len = comp_ver_str_len;
849
850 memcpy(msg->payload + sizeof(struct pldm_pass_component_table_req),
851 comp_ver_str->ptr, comp_ver_str->length);
852
853 return PLDM_SUCCESS;
854}
gokulsanker566784b2021-04-05 17:47:04 +0530855
856int decode_pass_component_table_resp(const struct pldm_msg *msg,
857 const size_t payload_length,
858 uint8_t *completion_code,
859 uint8_t *comp_resp,
860 uint8_t *comp_resp_code)
861{
862 if (msg == NULL || completion_code == NULL || comp_resp == NULL ||
863 comp_resp_code == NULL || !payload_length) {
864 return PLDM_ERROR_INVALID_DATA;
865 }
866
867 *completion_code = msg->payload[0];
868 if (*completion_code != PLDM_SUCCESS) {
869 return PLDM_SUCCESS;
870 }
871
872 if (payload_length != sizeof(struct pldm_pass_component_table_resp)) {
873 return PLDM_ERROR_INVALID_LENGTH;
874 }
875
876 struct pldm_pass_component_table_resp *response =
877 (struct pldm_pass_component_table_resp *)msg->payload;
878
879 if (!is_comp_resp_valid(response->comp_resp)) {
880 return PLDM_ERROR_INVALID_DATA;
881 }
882
883 if (!is_comp_resp_code_valid(response->comp_resp_code)) {
884 return PLDM_ERROR_INVALID_DATA;
885 }
886
887 *comp_resp = response->comp_resp;
888 *comp_resp_code = response->comp_resp_code;
889
890 return PLDM_SUCCESS;
891}
gokulsankeraa3a5cd2021-04-22 11:06:42 +0530892
893int encode_update_component_req(
894 uint8_t instance_id, uint16_t comp_classification, uint16_t comp_identifier,
895 uint8_t comp_classification_index, uint32_t comp_comparison_stamp,
896 uint32_t comp_image_size, bitfield32_t update_option_flags,
897 uint8_t comp_ver_str_type, uint8_t comp_ver_str_len,
898 const struct variable_field *comp_ver_str, struct pldm_msg *msg,
899 size_t payload_length)
900{
901 if (comp_ver_str == NULL || comp_ver_str->ptr == NULL || msg == NULL) {
902 return PLDM_ERROR_INVALID_DATA;
903 }
904
905 if (payload_length !=
906 sizeof(struct pldm_update_component_req) + comp_ver_str->length) {
907 return PLDM_ERROR_INVALID_LENGTH;
908 }
909
910 if (!comp_image_size) {
911 return PLDM_ERROR_INVALID_DATA;
912 }
913
914 if ((comp_ver_str_len == 0) ||
915 (comp_ver_str_len != comp_ver_str->length)) {
916 return PLDM_ERROR_INVALID_DATA;
917 }
918
919 if (!is_string_type_valid(comp_ver_str_type)) {
920 return PLDM_ERROR_INVALID_DATA;
921 }
922
923 struct pldm_header_info header = {0};
924 header.instance = instance_id;
925 header.msg_type = PLDM_REQUEST;
926 header.pldm_type = PLDM_FWUP;
927 header.command = PLDM_UPDATE_COMPONENT;
928 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
929 if (rc) {
930 return rc;
931 }
932
933 struct pldm_update_component_req *request =
934 (struct pldm_update_component_req *)msg->payload;
935
936 request->comp_classification = htole16(comp_classification);
937 request->comp_identifier = htole16(comp_identifier);
938 request->comp_classification_index = comp_classification_index;
939 request->comp_comparison_stamp = htole32(comp_comparison_stamp);
940 request->comp_image_size = htole32(comp_image_size);
941 request->update_option_flags.value = htole32(update_option_flags.value);
942 request->comp_ver_str_type = comp_ver_str_type;
943 request->comp_ver_str_len = comp_ver_str_len;
944
945 memcpy(msg->payload + sizeof(struct pldm_update_component_req),
946 comp_ver_str->ptr, comp_ver_str->length);
947
948 return PLDM_SUCCESS;
gokulsanker4b533f22021-04-22 12:53:00 +0530949}
950
951int decode_update_component_resp(const struct pldm_msg *msg,
952 size_t payload_length,
953 uint8_t *completion_code,
954 uint8_t *comp_compatability_resp,
955 uint8_t *comp_compatability_resp_code,
956 bitfield32_t *update_option_flags_enabled,
957 uint16_t *time_before_req_fw_data)
958{
959 if (msg == NULL || completion_code == NULL ||
960 comp_compatability_resp == NULL ||
961 comp_compatability_resp_code == NULL ||
962 update_option_flags_enabled == NULL ||
963 time_before_req_fw_data == NULL || !payload_length) {
964 return PLDM_ERROR_INVALID_DATA;
965 }
966
967 *completion_code = msg->payload[0];
968 if (*completion_code != PLDM_SUCCESS) {
969 return PLDM_SUCCESS;
970 }
971
972 if (payload_length != sizeof(struct pldm_update_component_resp)) {
973 return PLDM_ERROR_INVALID_LENGTH;
974 }
975
976 struct pldm_update_component_resp *response =
977 (struct pldm_update_component_resp *)msg->payload;
978
979 if (!is_comp_compatibility_resp_valid(
980 response->comp_compatability_resp)) {
981 return PLDM_ERROR_INVALID_DATA;
982 }
983
984 if (!is_comp_compatibility_resp_code_valid(
985 response->comp_compatability_resp_code)) {
986 return PLDM_ERROR_INVALID_DATA;
987 }
988
989 *comp_compatability_resp = response->comp_compatability_resp;
990 *comp_compatability_resp_code = response->comp_compatability_resp_code;
991 update_option_flags_enabled->value =
992 le32toh(response->update_option_flags_enabled.value);
993 *time_before_req_fw_data = le16toh(response->time_before_req_fw_data);
994
995 return PLDM_SUCCESS;
gokulsankeraa3a5cd2021-04-22 11:06:42 +0530996}