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
|
#include <assert.h>
#include <endian.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "base.h"
#include "fru.h"
#include "utils.h"
int encode_get_fru_record_table_metadata_req(uint8_t instance_id,
struct pldm_msg *msg,
size_t payload_length)
{
if (msg == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (payload_length != PLDM_GET_FRU_RECORD_TABLE_METADATA_REQ_BYTES) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct pldm_header_info header = { 0 };
header.instance = instance_id;
header.msg_type = PLDM_REQUEST;
header.pldm_type = PLDM_FRU;
header.command = PLDM_GET_FRU_RECORD_TABLE_METADATA;
return pack_pldm_header(&header, &(msg->hdr));
}
int decode_get_fru_record_table_metadata_resp(
const struct pldm_msg *msg, size_t payload_length,
uint8_t *completion_code, uint8_t *fru_data_major_version,
uint8_t *fru_data_minor_version, uint32_t *fru_table_maximum_size,
uint32_t *fru_table_length, uint16_t *total_record_set_identifiers,
uint16_t *total_table_records, uint32_t *checksum)
{
if (msg == NULL || completion_code == NULL ||
fru_data_major_version == NULL || fru_data_minor_version == NULL ||
fru_table_maximum_size == NULL || fru_table_length == NULL ||
total_record_set_identifiers == NULL ||
total_table_records == NULL || checksum == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
*completion_code = msg->payload[0];
if (PLDM_SUCCESS != *completion_code) {
return PLDM_SUCCESS;
}
if (payload_length != PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct pldm_get_fru_record_table_metadata_resp *response =
(struct pldm_get_fru_record_table_metadata_resp *)msg->payload;
*fru_data_major_version = response->fru_data_major_version;
*fru_data_minor_version = response->fru_data_minor_version;
*fru_table_maximum_size = le32toh(response->fru_table_maximum_size);
*fru_table_length = le32toh(response->fru_table_length);
*total_record_set_identifiers =
le16toh(response->total_record_set_identifiers);
*total_table_records = le16toh(response->total_table_records);
*checksum = le32toh(response->checksum);
return PLDM_SUCCESS;
}
int encode_get_fru_record_table_metadata_resp(
uint8_t instance_id, uint8_t completion_code,
uint8_t fru_data_major_version, uint8_t fru_data_minor_version,
uint32_t fru_table_maximum_size, uint32_t fru_table_length,
uint16_t total_record_set_identifiers, uint16_t total_table_records,
uint32_t checksum, struct pldm_msg *msg)
{
if (msg == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
struct pldm_header_info header = { 0 };
header.msg_type = PLDM_RESPONSE;
header.instance = instance_id;
header.pldm_type = PLDM_FRU;
header.command = PLDM_GET_FRU_RECORD_TABLE_METADATA;
uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
if (PLDM_SUCCESS != rc) {
return rc;
}
struct pldm_get_fru_record_table_metadata_resp *response =
(struct pldm_get_fru_record_table_metadata_resp *)msg->payload;
response->completion_code = completion_code;
if (response->completion_code == PLDM_SUCCESS) {
response->fru_data_major_version = fru_data_major_version;
response->fru_data_minor_version = fru_data_minor_version;
response->fru_table_maximum_size =
htole32(fru_table_maximum_size);
response->fru_table_length = htole32(fru_table_length);
response->total_record_set_identifiers =
htole16(total_record_set_identifiers);
response->total_table_records = htole16(total_table_records);
response->checksum = htole32(checksum);
}
return PLDM_SUCCESS;
}
int decode_get_fru_record_table_req(const struct pldm_msg *msg,
size_t payload_length,
uint32_t *data_transfer_handle,
uint8_t *transfer_operation_flag)
{
if (msg == NULL || data_transfer_handle == NULL ||
transfer_operation_flag == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (payload_length != PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct pldm_get_fru_record_table_req *req =
(struct pldm_get_fru_record_table_req *)msg->payload;
*data_transfer_handle = le32toh(req->data_transfer_handle);
*transfer_operation_flag = req->transfer_operation_flag;
return PLDM_SUCCESS;
}
int encode_get_fru_record_table_resp(uint8_t instance_id,
uint8_t completion_code,
uint32_t next_data_transfer_handle,
uint8_t transfer_flag,
struct pldm_msg *msg)
{
if (msg == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
struct pldm_header_info header = { 0 };
header.msg_type = PLDM_RESPONSE;
header.instance = instance_id;
header.pldm_type = PLDM_FRU;
header.command = PLDM_GET_FRU_RECORD_TABLE;
uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
if (rc > PLDM_SUCCESS) {
return rc;
}
struct pldm_get_fru_record_table_resp *resp =
(struct pldm_get_fru_record_table_resp *)msg->payload;
resp->completion_code = completion_code;
if (resp->completion_code == PLDM_SUCCESS) {
resp->next_data_transfer_handle =
htole32(next_data_transfer_handle);
resp->transfer_flag = transfer_flag;
}
return PLDM_SUCCESS;
}
int encode_fru_record(uint8_t *fru_table, size_t total_size, size_t *curr_size,
uint16_t record_set_id, uint8_t record_type,
uint8_t num_frus, uint8_t encoding, uint8_t *tlvs,
size_t tlvs_size)
{
size_t record_hdr_size = sizeof(struct pldm_fru_record_data_format) -
sizeof(struct pldm_fru_record_tlv);
if (fru_table == NULL || curr_size == NULL || !tlvs_size) {
return PLDM_ERROR_INVALID_DATA;
}
if ((*curr_size + record_hdr_size + tlvs_size) != total_size) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct pldm_fru_record_data_format *record =
(struct pldm_fru_record_data_format *)(fru_table + *curr_size);
record->record_set_id = htole16(record_set_id);
record->record_type = record_type;
record->num_fru_fields = num_frus;
record->encoding_type = encoding;
*curr_size += record_hdr_size;
if (tlvs) {
memcpy(fru_table + *curr_size, tlvs, tlvs_size);
*curr_size += tlvs_size;
}
return PLDM_SUCCESS;
}
static bool is_table_end(const struct pldm_fru_record_data_format *p,
const void *table, size_t table_size)
{
return p >=
(const struct pldm_fru_record_data_format *)((uint8_t *)table +
table_size);
}
void get_fru_record_by_option(const uint8_t *table, size_t table_size,
uint8_t *record_table, size_t *record_size,
uint16_t rsi, uint8_t rt, uint8_t ft)
{
const struct pldm_fru_record_data_format *record_data_src =
(const struct pldm_fru_record_data_format *)table;
struct pldm_fru_record_data_format *record_data_dest;
int count = 0;
const struct pldm_fru_record_tlv *tlv;
size_t len;
uint8_t *pos = record_table;
while (!is_table_end(record_data_src, table, table_size)) {
if ((record_data_src->record_set_id != htole16(rsi) &&
rsi != 0) ||
(record_data_src->record_type != rt && rt != 0)) {
tlv = record_data_src->tlvs;
for (int i = 0; i < record_data_src->num_fru_fields;
i++) {
len = sizeof(*tlv) - 1 + tlv->length;
tlv = (const struct pldm_fru_record_tlv
*)((char *)tlv + len);
}
record_data_src =
(const struct pldm_fru_record_data_format
*)(tlv);
continue;
}
len = sizeof(struct pldm_fru_record_data_format) -
sizeof(struct pldm_fru_record_tlv);
assert(pos - record_table + len < *record_size);
memcpy(pos, record_data_src, len);
record_data_dest = (struct pldm_fru_record_data_format *)pos;
pos += len;
tlv = record_data_src->tlvs;
count = 0;
for (int i = 0; i < record_data_src->num_fru_fields; i++) {
len = sizeof(*tlv) - 1 + tlv->length;
if (tlv->type == ft || ft == 0) {
assert(pos - record_table + len < *record_size);
memcpy(pos, tlv, len);
pos += len;
count++;
}
tlv = (const struct pldm_fru_record_tlv *)((char *)tlv +
len);
}
record_data_dest->num_fru_fields = count;
record_data_src =
(const struct pldm_fru_record_data_format *)(tlv);
}
*record_size = pos - record_table;
}
int encode_get_fru_record_by_option_req(
uint8_t instance_id, uint32_t data_transfer_handle,
uint16_t fru_table_handle, uint16_t record_set_identifier,
uint8_t record_type, uint8_t field_type, uint8_t transfer_op_flag,
struct pldm_msg *msg, size_t payload_length)
{
if (msg == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (payload_length !=
sizeof(struct pldm_get_fru_record_by_option_req)) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct pldm_header_info header = { 0 };
header.instance = instance_id;
header.msg_type = PLDM_REQUEST;
header.pldm_type = PLDM_FRU;
header.command = PLDM_GET_FRU_RECORD_BY_OPTION;
uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
if (rc != PLDM_SUCCESS) {
return rc;
}
struct pldm_get_fru_record_by_option_req *req =
(struct pldm_get_fru_record_by_option_req *)msg->payload;
req->data_transfer_handle = htole32(data_transfer_handle);
req->fru_table_handle = htole16(fru_table_handle);
req->record_set_identifier = htole16(record_set_identifier);
req->record_type = record_type;
req->field_type = field_type;
req->transfer_op_flag = transfer_op_flag;
return PLDM_SUCCESS;
}
int decode_get_fru_record_by_option_req(
const struct pldm_msg *msg, size_t payload_length,
uint32_t *data_transfer_handle, uint16_t *fru_table_handle,
uint16_t *record_set_identifier, uint8_t *record_type,
uint8_t *field_type, uint8_t *transfer_op_flag)
{
if (msg == NULL || data_transfer_handle == NULL ||
fru_table_handle == NULL || record_set_identifier == NULL ||
record_type == NULL || field_type == NULL ||
transfer_op_flag == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (payload_length !=
sizeof(struct pldm_get_fru_record_by_option_req)) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct pldm_get_fru_record_by_option_req *req =
(struct pldm_get_fru_record_by_option_req *)msg->payload;
*data_transfer_handle = le32toh(req->data_transfer_handle);
*fru_table_handle = le16toh(req->fru_table_handle);
*record_set_identifier = le16toh(req->record_set_identifier);
*record_type = req->record_type;
*field_type = req->field_type;
*transfer_op_flag = req->transfer_op_flag;
return PLDM_SUCCESS;
}
int encode_get_fru_record_by_option_resp(uint8_t instance_id,
uint8_t completion_code,
uint32_t next_data_transfer_handle,
uint8_t transfer_flag,
const void *fru_structure_data,
size_t data_size, struct pldm_msg *msg,
size_t payload_length)
{
if (msg == NULL || fru_structure_data == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (payload_length !=
PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES + data_size) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct pldm_header_info header = { 0 };
header.instance = instance_id;
header.msg_type = PLDM_RESPONSE;
header.pldm_type = PLDM_FRU;
header.command = PLDM_GET_FRU_RECORD_BY_OPTION;
uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
if (rc != PLDM_SUCCESS) {
return rc;
}
struct pldm_get_fru_record_by_option_resp *resp =
(struct pldm_get_fru_record_by_option_resp *)msg->payload;
resp->completion_code = completion_code;
resp->next_data_transfer_handle = htole32(next_data_transfer_handle);
resp->transfer_flag = transfer_flag;
if (completion_code == PLDM_SUCCESS) {
memcpy(resp->fru_structure_data, fru_structure_data, data_size);
}
return PLDM_SUCCESS;
}
int decode_get_fru_record_by_option_resp(
const struct pldm_msg *msg, size_t payload_length,
uint8_t *completion_code, uint32_t *next_transfer_handle,
uint8_t *transfer_flag, struct variable_field *fru_structure_data)
{
if (msg == NULL || completion_code == NULL ||
next_transfer_handle == NULL || transfer_flag == NULL ||
fru_structure_data == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
*completion_code = msg->payload[0];
if (PLDM_SUCCESS != *completion_code) {
return PLDM_SUCCESS;
}
if (payload_length < PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct pldm_get_fru_record_by_option_resp *resp =
(struct pldm_get_fru_record_by_option_resp *)msg->payload;
*next_transfer_handle = le32toh(resp->next_data_transfer_handle);
*transfer_flag = resp->transfer_flag;
fru_structure_data->ptr = resp->fru_structure_data;
fru_structure_data->length =
payload_length - PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES;
return PLDM_SUCCESS;
}
int encode_get_fru_record_table_req(uint8_t instance_id,
uint32_t data_transfer_handle,
uint8_t transfer_operation_flag,
struct pldm_msg *msg, size_t payload_length)
{
if (msg == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (payload_length != sizeof(struct pldm_get_fru_record_table_req)) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct pldm_header_info header = { 0 };
header.msg_type = PLDM_REQUEST;
header.instance = instance_id;
header.pldm_type = PLDM_FRU;
header.command = PLDM_GET_FRU_RECORD_TABLE;
uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
if (rc != PLDM_SUCCESS) {
return rc;
}
struct pldm_get_fru_record_table_req *req =
(struct pldm_get_fru_record_table_req *)msg->payload;
req->data_transfer_handle = htole32(data_transfer_handle);
req->transfer_operation_flag = transfer_operation_flag;
return PLDM_SUCCESS;
}
int decode_get_fru_record_table_resp_safe(
const struct pldm_msg *msg, size_t payload_length,
uint8_t *completion_code, uint32_t *next_data_transfer_handle,
uint8_t *transfer_flag, uint8_t *fru_record_table_data,
size_t *fru_record_table_length, size_t max_fru_record_table_length)
{
if (msg == NULL || completion_code == NULL ||
next_data_transfer_handle == NULL || transfer_flag == NULL ||
fru_record_table_data == NULL || fru_record_table_length == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
*completion_code = msg->payload[0];
if (PLDM_SUCCESS != *completion_code) {
return PLDM_SUCCESS;
}
if (payload_length <= PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct pldm_get_fru_record_table_resp *resp =
(struct pldm_get_fru_record_table_resp *)msg->payload;
*next_data_transfer_handle = le32toh(resp->next_data_transfer_handle);
*transfer_flag = resp->transfer_flag;
*fru_record_table_length =
payload_length - PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES;
if (*fru_record_table_length > max_fru_record_table_length) {
return PLDM_ERROR_INVALID_LENGTH;
}
memcpy(fru_record_table_data, resp->fru_record_table_data,
*fru_record_table_length);
return PLDM_SUCCESS;
}
int decode_get_fru_record_table_resp(const struct pldm_msg *msg,
size_t payload_length,
uint8_t *completion_code,
uint32_t *next_data_transfer_handle,
uint8_t *transfer_flag,
uint8_t *fru_record_table_data,
size_t *fru_record_table_length)
{
return decode_get_fru_record_table_resp_safe(
msg, payload_length, completion_code, next_data_transfer_handle,
transfer_flag, fru_record_table_data, fru_record_table_length,
(size_t)-1);
}
int decode_set_fru_record_table_req(const struct pldm_msg *msg,
size_t payload_length,
uint32_t *data_transfer_handle,
uint8_t *transfer_flag,
struct variable_field *fru_table_data)
{
if (msg == NULL || data_transfer_handle == NULL ||
transfer_flag == NULL || fru_table_data == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (payload_length <= PLDM_SET_FRU_RECORD_TABLE_MIN_REQ_BYTES) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct pldm_set_fru_record_table_req *req =
(struct pldm_set_fru_record_table_req *)msg->payload;
*data_transfer_handle = le32toh(req->data_transfer_handle);
*transfer_flag = req->transfer_flag;
fru_table_data->length =
payload_length - PLDM_SET_FRU_RECORD_TABLE_MIN_REQ_BYTES;
fru_table_data->ptr = req->fru_record_table_data;
return PLDM_SUCCESS;
}
int encode_set_fru_record_table_resp(uint8_t instance_id,
uint8_t completion_code,
uint32_t next_data_transfer_handle,
size_t payload_length,
struct pldm_msg *msg)
{
if (msg == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (payload_length != PLDM_SET_FRU_RECORD_TABLE_RESP_BYTES) {
return PLDM_ERROR_INVALID_LENGTH;
}
struct pldm_header_info header = { 0 };
header.instance = instance_id;
header.msg_type = PLDM_RESPONSE;
header.pldm_type = PLDM_FRU;
header.command = PLDM_SET_FRU_RECORD_TABLE;
uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
if (PLDM_SUCCESS != rc) {
return rc;
}
struct pldm_set_fru_record_table_resp *response =
(struct pldm_set_fru_record_table_resp *)msg->payload;
response->completion_code = completion_code;
response->next_data_transfer_handle =
htole32(next_data_transfer_handle);
return PLDM_SUCCESS;
}
|