This repository has been archived by the owner on Aug 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firmware_update.c
1594 lines (1344 loc) · 43.2 KB
/
firmware_update.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <endian.h>
#include <string.h>
#include "firmware_update.h"
static uint32_t maximum_transfer_size;
static uint32_t component_image_size;
// TODO: need to maintain maximum_transfer_size and component_image_size using a
// context structure
static struct request_firmware_data_req requested_comp_image_seg;
void initialize_fw_update(const uint32_t max_transfer_size,
const uint32_t comp_image_size)
{
maximum_transfer_size = max_transfer_size;
component_image_size = comp_image_size;
}
static int check_request_firmware_data_req_validity()
{
if (!((PLDM_FWU_BASELINE_TRANSFER_SIZE <=
requested_comp_image_seg.length) &&
(requested_comp_image_seg.length <= maximum_transfer_size))) {
return INVALID_TRANSFER_LENGTH;
}
uint64_t requestedPayload = (uint64_t)requested_comp_image_seg.length +
requested_comp_image_seg.offset;
uint64_t maxPayload =
(uint64_t)component_image_size + PLDM_FWU_BASELINE_TRANSFER_SIZE;
if (!(requestedPayload <= maxPayload)) {
return DATA_OUT_OF_RANGE;
}
return PLDM_SUCCESS;
}
static int get_padding_length(uint32_t *padding_length)
{
if (padding_length == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
*padding_length = 0;
uint64_t requestedPayload = (uint64_t)requested_comp_image_seg.length +
requested_comp_image_seg.offset;
int64_t padding = (requestedPayload - component_image_size);
if (padding > PLDM_FWU_BASELINE_TRANSFER_SIZE) {
return DATA_OUT_OF_RANGE;
}
*padding_length = (uint32_t)((padding > 0) ? padding : 0);
return PLDM_SUCCESS;
}
int decode_request_firmware_data_req(const struct pldm_msg *msg,
const size_t payload_length,
uint32_t *offset, uint32_t *length)
{
if (msg == NULL || offset == NULL || length == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (payload_length != sizeof(struct request_firmware_data_req)) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct request_firmware_data_req *request =
(struct request_firmware_data_req *)msg->payload;
*offset = le32toh(request->offset);
*length = le32toh(request->length);
requested_comp_image_seg = *request;
return check_request_firmware_data_req_validity();
}
int encode_request_firmware_data_resp(
const uint8_t instance_id, struct pldm_msg *msg,
const size_t payload_length, const uint8_t completion_code,
struct variable_field *component_image_portion)
{
if (msg == NULL || component_image_portion == NULL ||
component_image_portion->ptr == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (payload_length != requested_comp_image_seg.length + 1 ||
payload_length < PLDM_FWU_BASELINE_TRANSFER_SIZE ||
component_image_portion->length == 0) {
return PLDM_ERROR_INVALID_LENGTH;
}
int rc =
encode_pldm_header(instance_id, PLDM_FWUP,
PLDM_REQUEST_FIRMWARE_DATA, PLDM_RESPONSE, msg);
if (PLDM_SUCCESS != rc) {
return rc;
}
uint32_t padding_length;
rc = get_padding_length(&padding_length);
if (PLDM_SUCCESS != rc) {
msg->payload[0] = rc;
return rc;
}
msg->payload[0] = completion_code;
if (!padding_length) {
memcpy(msg->payload + 1, component_image_portion->ptr,
requested_comp_image_seg.length);
} else {
memcpy(msg->payload + 1, component_image_portion->ptr,
requested_comp_image_seg.length - padding_length);
memset(msg->payload + 1 + requested_comp_image_seg.length -
padding_length,
0x00, padding_length);
}
return PLDM_SUCCESS;
}
/** @brief Check whether Component Version String Type or Component Image Set
* Version String Type is valid
*
* @return true if it is from below mentioned values, false if not
*/
static bool check_comp_ver_str_type_valid(const uint8_t comp_type)
{
switch (comp_type) {
case PLDM_COMP_VER_STR_TYPE_UNKNOWN:
case PLDM_COMP_ASCII:
case PLDM_COMP_UTF_8:
case PLDM_COMP_UTF_16:
case PLDM_COMP_UTF_16LE:
case PLDM_COMP_UTF_16BE:
return true;
default:
return false;
}
}
/** @brief Check whether Component Classification is valid
*
* @return true if it is from below mentioned values, false if not
*/
static bool check_comp_classification_valid(const uint16_t comp_classification)
{
switch (comp_classification) {
case COMP_UNKNOWN:
case COMP_OTHER:
case COMP_DRIVER:
case COMP_CONFIGURATION_SOFTWARE:
case COMP_APPLICATION_SOFTWARE:
case COMP_INSTRUMENTATION:
case COMP_FIRMWARE_OR_BIOS:
case COMP_DIAGNOSTIC_SOFTWARE:
case COMP_OPERATING_SYSTEM:
case COMP_MIDDLEWARE:
case COMP_FIRMWARE:
case COMP_BIOS_OR_FCODE:
case COMP_SUPPORT_OR_SERVICEPACK:
case COMP_SOFTWARE_BUNDLE:
return true;
default:
return false;
}
}
/** @brief Check whether Component Response Code is valid
*
* @return true if it is from below mentioned values, false if not
*/
static bool check_resp_code_valid(const uint8_t comp_resp_code)
{
switch (comp_resp_code) {
case COMP_CAN_BE_UPDATED:
case COMP_COMPARISON_STAMP_IDENTICAL:
case COMP_COMPARISON_STAMP_LOWER:
case INVALID_COMP_COMPARISON_STAMP:
case COMP_CONFLICT:
case COMP_PREREQUISITES:
case COMP_NOT_SUPPORTED:
case COMP_SECURITY_RESTRICTIONS:
case INCOMPLETE_COMP_IMAGE_SET:
case COMP_VER_STR_IDENTICAL:
case COMP_VER_STR_LOWER:
return true;
default:
if (comp_resp_code >= FD_VENDOR_COMP_STATUS_CODE_RANGE_MIN &&
comp_resp_code <= FD_VENDOR_COMP_STATUS_CODE_RANGE_MAX) {
return true;
}
return false;
}
}
/** @brief Check whether Component Response is valid
*
* @return true if it is from below mentioned values, false if not
*/
static bool check_comp_resp_valid(const uint8_t comp_resp)
{
switch (comp_resp) {
case PLDM_COMP_CAN_BE_UPDATEABLE:
case PLDM_COMP_MAY_BE_UPDATEABLE:
return true;
default:
return false;
}
}
/** @brief Check validity of verify result in VerifyComplete request
*
* @param[in] verify_result - Indicate the result of the Verify stage
* @return validity
*/
static bool validate_verify_result(const uint8_t verify_result)
{
switch (verify_result) {
case PLDM_FWU_VERIFY_SUCCESS:
case PLDM_FWU_VERIFY_COMPLETED_WITH_FAILURE:
case PLDM_FWU_VERIFY_COMPLETED_WITH_ERROR:
case PLDM_FWU_TIME_OUT:
case PLDM_FWU_GENERIC_ERROR:
return true;
default:
if (verify_result >= PLDM_FWU_VENDOR_SPEC_STATUS_RANGE_MIN &&
verify_result <= PLDM_FWU_VENDOR_SPEC_STATUS_RANGE_MAX) {
return true;
}
return false;
}
}
/** @brief Check validity of transfer result in TransferComplete
*
* @param[in] transfer_result - Indicate the result of the Download stage
* @return validity
*/
static bool validate_transfer_result(const uint8_t transfer_result)
{
switch (transfer_result) {
case PLDM_FWU_TRASFER_SUCCESS:
case PLDM_FWU_TRANSFER_COMPLETE_WITH_ERROR:
case PLDM_FWU_FD_ABORTED_TRANSFER:
case PLDM_FWU_TIME_OUT:
case PLDM_FWU_GENERIC_ERROR:
return true;
default:
if (transfer_result >=
PLDM_FWU_VENDOR_TRANSFER_RESULT_RANGE_MIN &&
transfer_result <=
PLDM_FWU_VENDOR_TRANSFER_RESULT_RANGE_MAX) {
return true;
}
return false;
}
}
int encode_query_device_identifiers_req(const uint8_t instance_id,
struct pldm_msg *msg,
const size_t payload_length)
{
if (msg == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (payload_length != PLDM_QUERY_DEVICE_IDENTIFIERS_REQ_BYTES) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct pldm_header_info header = {0};
header.msg_type = PLDM_REQUEST;
header.instance = instance_id;
header.pldm_type = PLDM_FWUP;
header.command = PLDM_QUERY_DEVICE_IDENTIFIERS;
int rc = pack_pldm_header(&header, &(msg->hdr));
if (PLDM_SUCCESS != rc) {
return rc;
}
return PLDM_SUCCESS;
}
int decode_query_device_identifiers_resp(const struct pldm_msg *msg,
const size_t payload_length,
uint8_t *completion_code,
uint32_t *device_identifiers_len,
uint8_t *descriptor_count,
uint8_t **descriptor_data)
{
if (msg == NULL || completion_code == NULL ||
device_identifiers_len == NULL || descriptor_count == NULL ||
descriptor_data == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
*completion_code = msg->payload[0];
if (PLDM_SUCCESS != *completion_code) {
return DECODE_SUCCESS;
}
if (payload_length < sizeof(struct query_device_identifiers_resp)) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct query_device_identifiers_resp *response =
(struct query_device_identifiers_resp *)msg->payload;
*device_identifiers_len = le32toh(response->device_identifiers_len);
if (*device_identifiers_len < PLDM_FWU_MIN_DESCRIPTOR_IDENTIFIERS_LEN) {
return PLDM_ERROR_INVALID_LENGTH;
}
if (payload_length != sizeof(struct query_device_identifiers_resp) +
*device_identifiers_len) {
return PLDM_ERROR_INVALID_LENGTH;
}
*descriptor_count = response->descriptor_count;
if (*descriptor_count == 0) {
return PLDM_ERROR_INVALID_DATA;
}
*descriptor_data =
(uint8_t *)(msg->payload +
sizeof(struct query_device_identifiers_resp));
return PLDM_SUCCESS;
}
int encode_get_firmware_parameters_req(const uint8_t instance_id,
struct pldm_msg *msg,
const size_t payload_length)
{
if (msg == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (payload_length != PLDM_GET_FIRMWARE_PARAMETERS_REQ_BYTES) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct pldm_header_info header = {0};
header.msg_type = PLDM_REQUEST;
header.instance = instance_id;
header.pldm_type = PLDM_FWUP;
header.command = PLDM_GET_FIRMWARE_PARAMETERS;
int rc = pack_pldm_header(&header, &(msg->hdr));
if (PLDM_SUCCESS != rc) {
return rc;
}
return PLDM_SUCCESS;
}
int decode_get_firmware_parameters_comp_img_set_resp(
const struct pldm_msg *msg, const size_t payload_length,
struct get_firmware_parameters_resp *resp_data,
struct variable_field *active_comp_image_set_ver_str,
struct variable_field *pending_comp_image_set_ver_str)
{
if (msg == NULL || resp_data == NULL ||
active_comp_image_set_ver_str == NULL ||
pending_comp_image_set_ver_str == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (payload_length < sizeof(struct get_firmware_parameters_resp)) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct get_firmware_parameters_resp *response =
(struct get_firmware_parameters_resp *)msg->payload;
if (PLDM_SUCCESS != response->completion_code) {
return DECODE_SUCCESS;
}
if (response->active_comp_image_set_ver_str_len == 0) {
return PLDM_ERROR_INVALID_DATA;
}
size_t resp_len = sizeof(struct get_firmware_parameters_resp) +
response->active_comp_image_set_ver_str_len +
response->pending_comp_image_set_ver_str_len;
if (payload_length < resp_len) {
return PLDM_ERROR_INVALID_LENGTH;
}
resp_data->capabilities_during_update.value =
le32toh(response->capabilities_during_update.value);
resp_data->comp_count = le16toh(response->comp_count);
if (resp_data->comp_count == 0) {
return PLDM_ERROR;
}
resp_data->active_comp_image_set_ver_str_type =
response->active_comp_image_set_ver_str_type;
resp_data->active_comp_image_set_ver_str_len =
response->active_comp_image_set_ver_str_len;
resp_data->pending_comp_image_set_ver_str_type =
response->pending_comp_image_set_ver_str_type;
resp_data->pending_comp_image_set_ver_str_len =
response->pending_comp_image_set_ver_str_len;
active_comp_image_set_ver_str->ptr =
msg->payload + sizeof(struct get_firmware_parameters_resp);
active_comp_image_set_ver_str->length =
resp_data->active_comp_image_set_ver_str_len;
if (resp_data->pending_comp_image_set_ver_str_len != 0) {
pending_comp_image_set_ver_str->ptr =
msg->payload + sizeof(struct get_firmware_parameters_resp) +
resp_data->active_comp_image_set_ver_str_len;
pending_comp_image_set_ver_str->length =
resp_data->pending_comp_image_set_ver_str_len;
} else {
pending_comp_image_set_ver_str->ptr = NULL;
pending_comp_image_set_ver_str->length = 0;
}
return PLDM_SUCCESS;
}
int decode_get_firmware_parameters_comp_resp(
const uint8_t *msg, const size_t payload_length,
struct component_parameter_table *component_data,
struct variable_field *active_comp_ver_str,
struct variable_field *pending_comp_ver_str)
{
if (msg == NULL || component_data == NULL ||
active_comp_ver_str == NULL || pending_comp_ver_str == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (payload_length < sizeof(struct component_parameter_table)) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct component_parameter_table *component_resp =
(struct component_parameter_table *)(msg);
if (component_resp->active_comp_ver_str_len == 0) {
return PLDM_ERROR_INVALID_LENGTH;
}
size_t resp_len = sizeof(struct component_parameter_table) +
component_resp->active_comp_ver_str_len +
component_resp->pending_comp_ver_str_len;
if (payload_length < resp_len) {
return PLDM_ERROR_INVALID_LENGTH;
}
component_data->comp_classification =
le16toh(component_resp->comp_classification);
component_data->comp_identifier =
le16toh(component_resp->comp_identifier);
component_data->comp_classification_index =
component_resp->comp_classification_index;
component_data->active_comp_comparison_stamp =
le32toh(component_resp->active_comp_comparison_stamp);
component_data->active_comp_ver_str_type =
component_resp->active_comp_ver_str_type;
component_data->active_comp_ver_str_len =
component_resp->active_comp_ver_str_len;
memcpy(component_data->active_comp_release_date,
component_resp->active_comp_release_date,
sizeof(component_resp->active_comp_release_date));
component_data->pending_comp_comparison_stamp =
le32toh(component_resp->pending_comp_comparison_stamp);
component_data->pending_comp_ver_str_type =
component_resp->pending_comp_ver_str_type;
component_data->pending_comp_ver_str_len =
component_resp->pending_comp_ver_str_len;
memcpy(component_data->pending_comp_release_date,
component_resp->pending_comp_release_date,
sizeof(component_resp->pending_comp_release_date));
component_data->comp_activation_methods.value =
le16toh(component_resp->comp_activation_methods.value);
component_data->capabilities_during_update.value =
le16toh(component_resp->capabilities_during_update.value);
active_comp_ver_str->ptr =
msg + sizeof(struct component_parameter_table);
active_comp_ver_str->length = component_resp->active_comp_ver_str_len;
if (component_resp->pending_comp_ver_str_len != 0) {
pending_comp_ver_str->ptr =
msg + sizeof(struct component_parameter_table) +
component_resp->active_comp_ver_str_len;
pending_comp_ver_str->length =
component_resp->pending_comp_ver_str_len;
} else {
pending_comp_ver_str->ptr = NULL;
pending_comp_ver_str->length = 0;
}
return PLDM_SUCCESS;
}
int encode_request_update_req(const uint8_t instance_id, struct pldm_msg *msg,
const size_t payload_length,
const struct request_update_req *data,
struct variable_field *comp_img_set_ver_str)
{
if (msg == NULL || data == NULL || comp_img_set_ver_str == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (payload_length < sizeof(struct request_update_req)) {
return PLDM_ERROR_INVALID_LENGTH;
}
if (data->max_outstand_transfer_req < PLDM_FWUP_MIN_OUTSTANDING_REQ) {
return PLDM_ERROR_INVALID_DATA;
}
if (!check_comp_ver_str_type_valid(data->comp_image_set_ver_str_type)) {
return PLDM_ERROR_INVALID_DATA;
}
int rc = encode_pldm_header(instance_id, PLDM_FWUP, PLDM_REQUEST_UPDATE,
PLDM_REQUEST, msg);
if (PLDM_SUCCESS != rc) {
return rc;
}
struct request_update_req *request =
(struct request_update_req *)msg->payload;
if (request == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (data->max_transfer_size < PLDM_FWU_BASELINE_TRANSFER_SIZE) {
return PLDM_ERROR_INVALID_DATA;
}
request->max_transfer_size = htole32(data->max_transfer_size);
request->no_of_comp = htole16(data->no_of_comp);
request->max_outstand_transfer_req = data->max_outstand_transfer_req;
request->pkg_data_len = htole16(data->pkg_data_len);
request->comp_image_set_ver_str_type =
data->comp_image_set_ver_str_type;
request->comp_image_set_ver_str_len = data->comp_image_set_ver_str_len;
if (payload_length !=
sizeof(struct request_update_req) + comp_img_set_ver_str->length) {
return PLDM_ERROR_INVALID_LENGTH;
}
if (comp_img_set_ver_str->ptr == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
memcpy(msg->payload + sizeof(struct request_update_req),
comp_img_set_ver_str->ptr, comp_img_set_ver_str->length);
return PLDM_SUCCESS;
}
int decode_request_update_resp(const struct pldm_msg *msg,
const size_t payload_length,
uint8_t *completion_code,
uint16_t *fd_meta_data_len, uint8_t *fd_pkg_data)
{
if (msg == NULL || completion_code == NULL ||
fd_meta_data_len == NULL || fd_pkg_data == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
*completion_code = msg->payload[0];
if (*completion_code != PLDM_SUCCESS) {
return DECODE_SUCCESS;
}
if (payload_length != sizeof(struct pldm_request_update_resp)) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct pldm_request_update_resp *response =
(struct pldm_request_update_resp *)msg->payload;
if (response == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
*fd_meta_data_len = le16toh(response->fd_meta_data_len);
*fd_pkg_data = response->fd_pkg_data;
return PLDM_SUCCESS;
}
int encode_get_device_meta_data_req(const uint8_t instance_id,
struct pldm_msg *msg,
const size_t payload_length,
const uint32_t data_transfer_handle,
const uint8_t transfer_operation_flag)
{
if (msg == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (payload_length != sizeof(struct get_device_meta_data_req)) {
return PLDM_ERROR_INVALID_LENGTH;
}
int rc =
encode_pldm_header(instance_id, PLDM_FWUP,
PLDM_GET_DEVICE_META_DATA, PLDM_REQUEST, msg);
if (PLDM_SUCCESS != rc) {
return rc;
}
struct get_device_meta_data_req *request =
(struct get_device_meta_data_req *)msg->payload;
if (request == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
request->data_transfer_handle = htole32(data_transfer_handle);
if (!check_transfer_operation_flag_valid(transfer_operation_flag)) {
return PLDM_INVALID_TRANSFER_OPERATION_FLAG;
}
request->transfer_operation_flag = transfer_operation_flag;
return PLDM_SUCCESS;
}
int decode_get_device_meta_data_resp(
const struct pldm_msg *msg, const size_t payload_length,
uint8_t *completion_code, uint32_t *next_data_transfer_handle,
uint8_t *transfer_flag, struct variable_field *portion_of_meta_data)
{
if (msg == NULL || completion_code == NULL ||
next_data_transfer_handle == NULL || transfer_flag == NULL ||
portion_of_meta_data == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
*completion_code = msg->payload[0];
if (*completion_code != PLDM_SUCCESS) {
return DECODE_SUCCESS;
}
if (payload_length < sizeof(struct get_device_meta_data_resp)) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct get_device_meta_data_resp *response =
(struct get_device_meta_data_resp *)msg->payload;
if (response == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
*next_data_transfer_handle =
le32toh(response->next_data_transfer_handle);
if (!check_transfer_flag_valid(response->transfer_flag)) {
return PLDM_INVALID_TRANSFER_OPERATION_FLAG;
}
*transfer_flag = response->transfer_flag;
portion_of_meta_data->ptr =
msg->payload + sizeof(struct get_device_meta_data_resp);
portion_of_meta_data->length =
payload_length - sizeof(struct get_device_meta_data_resp);
return PLDM_SUCCESS;
}
/** @brief Check whether Self Contained Activation Request is valid
*
* @return true if it is from below mentioned values, false if not
*/
static bool check_self_contained_activation_req_valid(
const bool8_t self_contained_activation_req)
{
switch (self_contained_activation_req) {
case NOT_CONTAINING_SELF_ACTIVATED_COMPONENTS:
case CONTAINS_SELF_ACTIVATED_COMPONENTS:
return true;
default:
return false;
}
}
int encode_activate_firmware_req(const uint8_t instance_id,
struct pldm_msg *msg,
const size_t payload_length,
const bool8_t self_contained_activation_req)
{
if (msg == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (payload_length != sizeof(struct activate_firmware_req)) {
return PLDM_ERROR_INVALID_LENGTH;
}
int rc = encode_pldm_header(instance_id, PLDM_FWUP,
PLDM_ACTIVATE_FIRMWARE, PLDM_REQUEST, msg);
if (PLDM_SUCCESS != rc) {
return rc;
}
struct activate_firmware_req *request =
(struct activate_firmware_req *)msg->payload;
if (request == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (!check_self_contained_activation_req_valid(
self_contained_activation_req)) {
return PLDM_ERROR_INVALID_DATA;
}
request->self_contained_activation_req = self_contained_activation_req;
return PLDM_SUCCESS;
}
int decode_activate_firmware_resp(const struct pldm_msg *msg,
const size_t payload_length,
uint8_t *completion_code,
uint16_t *estimated_time_activation)
{
if (msg == NULL || completion_code == NULL ||
estimated_time_activation == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
*completion_code = msg->payload[0];
if (*completion_code != PLDM_SUCCESS) {
return DECODE_SUCCESS;
}
if (payload_length != sizeof(struct activate_firmware_resp)) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct activate_firmware_resp *response =
(struct activate_firmware_resp *)msg->payload;
if (response == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
*estimated_time_activation =
le16toh(response->estimated_time_activation);
return PLDM_SUCCESS;
}
int encode_pass_component_table_req(const uint8_t instance_id,
struct pldm_msg *msg,
const size_t payload_length,
const struct pass_component_table_req *data,
struct variable_field *comp_ver_str)
{
if (msg == NULL || data == NULL || comp_ver_str == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (payload_length < sizeof(struct pass_component_table_req)) {
return PLDM_ERROR_INVALID_LENGTH;
}
if (comp_ver_str->length != data->comp_ver_str_len) {
return PLDM_ERROR_INVALID_LENGTH;
}
int rc =
encode_pldm_header(instance_id, PLDM_FWUP,
PLDM_PASS_COMPONENT_TABLE, PLDM_REQUEST, msg);
if (PLDM_SUCCESS != rc) {
return rc;
}
struct pass_component_table_req *request =
(struct pass_component_table_req *)msg->payload;
if (request == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (!check_transfer_flag_valid(data->transfer_flag)) {
return PLDM_INVALID_TRANSFER_OPERATION_FLAG;
}
request->transfer_flag = data->transfer_flag;
if (!check_comp_classification_valid(
htole16(data->comp_classification))) {
return PLDM_ERROR_INVALID_DATA;
}
request->comp_classification = htole16(data->comp_classification);
request->comp_identifier = htole16(data->comp_identifier);
request->comp_classification_index = data->comp_classification_index;
request->comp_comparison_stamp = htole32(data->comp_comparison_stamp);
if (!check_comp_ver_str_type_valid(data->comp_ver_str_type)) {
return PLDM_ERROR_INVALID_DATA;
}
request->comp_ver_str_type = data->comp_ver_str_type;
request->comp_ver_str_len = data->comp_ver_str_len;
if (payload_length !=
sizeof(struct pass_component_table_req) + comp_ver_str->length) {
return PLDM_ERROR_INVALID_LENGTH;
}
if (comp_ver_str->ptr == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
memcpy(msg->payload + sizeof(struct pass_component_table_req),
comp_ver_str->ptr, comp_ver_str->length);
return PLDM_SUCCESS;
}
int decode_pass_component_table_resp(const struct pldm_msg *msg,
const size_t payload_length,
uint8_t *completion_code,
uint8_t *comp_resp,
uint8_t *comp_resp_code)
{
if (msg == NULL || completion_code == NULL || comp_resp == NULL ||
comp_resp_code == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
*completion_code = msg->payload[0];
if (*completion_code != PLDM_SUCCESS) {
return DECODE_SUCCESS;
}
if (payload_length != sizeof(struct pass_component_table_resp)) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct pass_component_table_resp *response =
(struct pass_component_table_resp *)msg->payload;
if (response == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (!check_comp_resp_valid(response->comp_resp)) {
return PLDM_ERROR_INVALID_DATA;
}
*comp_resp = response->comp_resp;
if (!check_resp_code_valid(response->comp_resp_code)) {
return PLDM_ERROR_INVALID_DATA;
}
*comp_resp_code = response->comp_resp_code;
return PLDM_SUCCESS;
}
/** @brief Check whether component compatibility response code is valid
*
* @return true if it is from below mentioned values, false if not
*/
static bool
check_compatability_resp_code_valid(const uint8_t comp_compatability_resp_code)
{
switch (comp_compatability_resp_code) {
case NO_RESPONSE_CODE:
case COMP_COMPARISON_STAMP_IDENTICAL:
case COMP_COMPARISON_STAMP_LOWER:
case INVALID_COMP_COMPARISON_STAMP:
case COMP_CONFLICT:
case COMP_PREREQUISITES:
case COMP_NOT_SUPPORTED:
case COMP_SECURITY_RESTRICTIONS:
case INCOMPLETE_COMP_IMAGE_SET:
case COMPATABILITY_NO_MATCH:
case COMP_VER_STR_IDENTICAL:
case COMP_VER_STR_LOWER:
return true;
default:
if (comp_compatability_resp_code >=
FD_VENDOR_COMP_STATUS_CODE_RANGE_MIN &&
comp_compatability_resp_code <=
FD_VENDOR_COMP_STATUS_CODE_RANGE_MAX) {
return true;
}
return false;
}
}
/** @brief Check whether component compatibility response is valid
*
* @return true if it is from below mentioned values, false if not
*/
static bool
check_compatability_resp_valid(const uint8_t comp_compatability_resp)
{
switch (comp_compatability_resp) {
case COMPONENT_CAN_BE_UPDATED:
case COMPONENT_CANNOT_BE_UPDATED:
return true;
default:
return false;
}
}
int encode_update_component_req(const uint8_t instance_id, struct pldm_msg *msg,
const size_t payload_length,
const struct update_component_req *data,
struct variable_field *comp_ver_str)
{
if (msg == NULL || data == NULL || comp_ver_str == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (payload_length < sizeof(struct update_component_req)) {
return PLDM_ERROR_INVALID_LENGTH;
}
int rc = encode_pldm_header(instance_id, PLDM_FWUP,
PLDM_UPDATE_COMPONENT, PLDM_REQUEST, msg);
if (PLDM_SUCCESS != rc) {
return rc;
}
struct update_component_req *request =
(struct update_component_req *)msg->payload;
if (request == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (!check_comp_classification_valid(
htole16(data->comp_classification))) {
return PLDM_ERROR_INVALID_DATA;
}
request->comp_classification = htole16(data->comp_classification);
request->comp_identifier = htole16(data->comp_identifier);
request->comp_classification_index = data->comp_classification_index;
request->comp_comparison_stamp = htole32(data->comp_comparison_stamp);
request->comp_image_size = htole32(data->comp_image_size);
request->update_option_flags.value =
htole32(data->update_option_flags.value);
if (!check_comp_ver_str_type_valid(data->comp_ver_str_type)) {
return PLDM_ERROR_INVALID_DATA;
}
request->comp_ver_str_type = data->comp_ver_str_type;
request->comp_ver_str_len = data->comp_ver_str_len;
if (payload_length !=
sizeof(struct update_component_req) + comp_ver_str->length) {
return PLDM_ERROR_INVALID_LENGTH;
}
if (comp_ver_str->ptr == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
memcpy(msg->payload + sizeof(struct update_component_req),
comp_ver_str->ptr, comp_ver_str->length);
return PLDM_SUCCESS;
}
int decode_update_component_resp(const struct pldm_msg *msg,
const size_t payload_length,
uint8_t *completion_code,
uint8_t *comp_compatability_resp,
uint8_t *comp_compatability_resp_code,