blob: aeb7d50c1573a7d17d6c26ce38e26f309a0acf51 [file] [log] [blame]
Andrew Jeffery9c766792022-08-10 23:12:49 +09301#include <assert.h>
2#include <endian.h>
3#include <stdbool.h>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05304#include <stdint.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +09305#include <string.h>
6
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05307#include "base.h"
Andrew Jeffery9c766792022-08-10 23:12:49 +09308#include "fru.h"
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05309#include "utils.h"
Andrew Jeffery9c766792022-08-10 23:12:49 +093010
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +093011LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +093012int encode_get_fru_record_table_metadata_req(uint8_t instance_id,
13 struct pldm_msg *msg,
14 size_t payload_length)
15{
16 if (msg == NULL) {
17 return PLDM_ERROR_INVALID_DATA;
18 }
19
20 if (payload_length != PLDM_GET_FRU_RECORD_TABLE_METADATA_REQ_BYTES) {
21 return PLDM_ERROR_INVALID_LENGTH;
22 }
23
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093024 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +093025 header.instance = instance_id;
26 header.msg_type = PLDM_REQUEST;
27 header.pldm_type = PLDM_FRU;
28 header.command = PLDM_GET_FRU_RECORD_TABLE_METADATA;
29
30 return pack_pldm_header(&header, &(msg->hdr));
31}
32
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +093033LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +093034int decode_get_fru_record_table_metadata_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093035 const struct pldm_msg *msg, size_t payload_length,
36 uint8_t *completion_code, uint8_t *fru_data_major_version,
37 uint8_t *fru_data_minor_version, uint32_t *fru_table_maximum_size,
38 uint32_t *fru_table_length, uint16_t *total_record_set_identifiers,
39 uint16_t *total_table_records, uint32_t *checksum)
Andrew Jeffery9c766792022-08-10 23:12:49 +093040{
41 if (msg == NULL || completion_code == NULL ||
42 fru_data_major_version == NULL || fru_data_minor_version == NULL ||
43 fru_table_maximum_size == NULL || fru_table_length == NULL ||
44 total_record_set_identifiers == NULL ||
45 total_table_records == NULL || checksum == NULL) {
46 return PLDM_ERROR_INVALID_DATA;
47 }
48
49 *completion_code = msg->payload[0];
50 if (PLDM_SUCCESS != *completion_code) {
51 return PLDM_SUCCESS;
52 }
53
54 if (payload_length != PLDM_GET_FRU_RECORD_TABLE_METADATA_RESP_BYTES) {
55 return PLDM_ERROR_INVALID_LENGTH;
56 }
57
58 struct pldm_get_fru_record_table_metadata_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093059 (struct pldm_get_fru_record_table_metadata_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +093060
61 *fru_data_major_version = response->fru_data_major_version;
62 *fru_data_minor_version = response->fru_data_minor_version;
63 *fru_table_maximum_size = le32toh(response->fru_table_maximum_size);
64 *fru_table_length = le32toh(response->fru_table_length);
65 *total_record_set_identifiers =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093066 le16toh(response->total_record_set_identifiers);
Andrew Jeffery9c766792022-08-10 23:12:49 +093067 *total_table_records = le16toh(response->total_table_records);
68 *checksum = le32toh(response->checksum);
69
70 return PLDM_SUCCESS;
71}
72
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +093073LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +093074int encode_get_fru_record_table_metadata_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093075 uint8_t instance_id, uint8_t completion_code,
76 uint8_t fru_data_major_version, uint8_t fru_data_minor_version,
77 uint32_t fru_table_maximum_size, uint32_t fru_table_length,
78 uint16_t total_record_set_identifiers, uint16_t total_table_records,
79 uint32_t checksum, struct pldm_msg *msg)
Andrew Jeffery9c766792022-08-10 23:12:49 +093080{
81 if (msg == NULL) {
82 return PLDM_ERROR_INVALID_DATA;
83 }
84
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093085 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +093086 header.msg_type = PLDM_RESPONSE;
87 header.instance = instance_id;
88 header.pldm_type = PLDM_FRU;
89 header.command = PLDM_GET_FRU_RECORD_TABLE_METADATA;
90
91 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
92 if (PLDM_SUCCESS != rc) {
93 return rc;
94 }
95
96 struct pldm_get_fru_record_table_metadata_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093097 (struct pldm_get_fru_record_table_metadata_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +093098 response->completion_code = completion_code;
99 if (response->completion_code == PLDM_SUCCESS) {
100 response->fru_data_major_version = fru_data_major_version;
101 response->fru_data_minor_version = fru_data_minor_version;
102 response->fru_table_maximum_size =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930103 htole32(fru_table_maximum_size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930104 response->fru_table_length = htole32(fru_table_length);
105 response->total_record_set_identifiers =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930106 htole16(total_record_set_identifiers);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930107 response->total_table_records = htole16(total_table_records);
108 response->checksum = htole32(checksum);
109 }
110
111 return PLDM_SUCCESS;
112}
113
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930114LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930115int decode_get_fru_record_table_req(const struct pldm_msg *msg,
116 size_t payload_length,
117 uint32_t *data_transfer_handle,
118 uint8_t *transfer_operation_flag)
119{
120 if (msg == NULL || data_transfer_handle == NULL ||
121 transfer_operation_flag == NULL) {
122 return PLDM_ERROR_INVALID_DATA;
123 }
124
125 if (payload_length != PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES) {
126 return PLDM_ERROR_INVALID_LENGTH;
127 }
128
129 struct pldm_get_fru_record_table_req *req =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930130 (struct pldm_get_fru_record_table_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930131
132 *data_transfer_handle = le32toh(req->data_transfer_handle);
133 *transfer_operation_flag = req->transfer_operation_flag;
134
135 return PLDM_SUCCESS;
136}
137
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930138LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930139int encode_get_fru_record_table_resp(uint8_t instance_id,
140 uint8_t completion_code,
141 uint32_t next_data_transfer_handle,
142 uint8_t transfer_flag,
143 struct pldm_msg *msg)
144{
145 if (msg == NULL) {
146 return PLDM_ERROR_INVALID_DATA;
147 }
148
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930149 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930150 header.msg_type = PLDM_RESPONSE;
151 header.instance = instance_id;
152 header.pldm_type = PLDM_FRU;
153 header.command = PLDM_GET_FRU_RECORD_TABLE;
154
155 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
156 if (rc > PLDM_SUCCESS) {
157 return rc;
158 }
159
160 struct pldm_get_fru_record_table_resp *resp =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930161 (struct pldm_get_fru_record_table_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930162
163 resp->completion_code = completion_code;
164
165 if (resp->completion_code == PLDM_SUCCESS) {
Andrew Jeffery9c766792022-08-10 23:12:49 +0930166 resp->next_data_transfer_handle =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930167 htole32(next_data_transfer_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930168 resp->transfer_flag = transfer_flag;
169 }
170
171 return PLDM_SUCCESS;
172}
173
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930174LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930175int encode_fru_record(uint8_t *fru_table, size_t total_size, size_t *curr_size,
176 uint16_t record_set_id, uint8_t record_type,
177 uint8_t num_frus, uint8_t encoding, uint8_t *tlvs,
178 size_t tlvs_size)
179{
180 size_t record_hdr_size = sizeof(struct pldm_fru_record_data_format) -
181 sizeof(struct pldm_fru_record_tlv);
182
183 if (fru_table == NULL || curr_size == NULL || !tlvs_size) {
184 return PLDM_ERROR_INVALID_DATA;
185 }
186 if ((*curr_size + record_hdr_size + tlvs_size) != total_size) {
187 return PLDM_ERROR_INVALID_LENGTH;
188 }
189
190 struct pldm_fru_record_data_format *record =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930191 (struct pldm_fru_record_data_format *)(fru_table + *curr_size);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930192 record->record_set_id = htole16(record_set_id);
193 record->record_type = record_type;
194 record->num_fru_fields = num_frus;
195 record->encoding_type = encoding;
196 *curr_size += record_hdr_size;
197
198 if (tlvs) {
199 memcpy(fru_table + *curr_size, tlvs, tlvs_size);
200 *curr_size += tlvs_size;
201 }
202
203 return PLDM_SUCCESS;
204}
205
206static bool is_table_end(const struct pldm_fru_record_data_format *p,
207 const void *table, size_t table_size)
208{
209 return p ==
210 (const struct pldm_fru_record_data_format *)((uint8_t *)table +
211 table_size);
212}
213
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930214LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930215void get_fru_record_by_option(const uint8_t *table, size_t table_size,
216 uint8_t *record_table, size_t *record_size,
217 uint16_t rsi, uint8_t rt, uint8_t ft)
218{
219 const struct pldm_fru_record_data_format *record_data_src =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930220 (const struct pldm_fru_record_data_format *)table;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930221 struct pldm_fru_record_data_format *record_data_dest;
222 int count = 0;
223
224 const struct pldm_fru_record_tlv *tlv;
225 size_t len;
226 uint8_t *pos = record_table;
227
228 while (!is_table_end(record_data_src, table, table_size)) {
229 if ((record_data_src->record_set_id != htole16(rsi) &&
230 rsi != 0) ||
231 (record_data_src->record_type != rt && rt != 0)) {
232 tlv = record_data_src->tlvs;
233 for (int i = 0; i < record_data_src->num_fru_fields;
234 i++) {
235 len = sizeof(*tlv) - 1 + tlv->length;
236 tlv = (const struct pldm_fru_record_tlv
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930237 *)((char *)tlv + len);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930238 }
239 record_data_src =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930240 (const struct pldm_fru_record_data_format
241 *)(tlv);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930242 continue;
243 }
244
245 len = sizeof(struct pldm_fru_record_data_format) -
246 sizeof(struct pldm_fru_record_tlv);
247
248 assert(pos - record_table + len < *record_size);
249 memcpy(pos, record_data_src, len);
250
251 record_data_dest = (struct pldm_fru_record_data_format *)pos;
252 pos += len;
253
254 tlv = record_data_src->tlvs;
255 count = 0;
256 for (int i = 0; i < record_data_src->num_fru_fields; i++) {
257 len = sizeof(*tlv) - 1 + tlv->length;
258 if (tlv->type == ft || ft == 0) {
259 assert(pos - record_table + len < *record_size);
260 memcpy(pos, tlv, len);
261 pos += len;
262 count++;
263 }
264 tlv = (const struct pldm_fru_record_tlv *)((char *)tlv +
265 len);
266 }
267 record_data_dest->num_fru_fields = count;
268 record_data_src =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930269 (const struct pldm_fru_record_data_format *)(tlv);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930270 }
271
272 *record_size = pos - record_table;
273}
274
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930275LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930276int encode_get_fru_record_by_option_req(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930277 uint8_t instance_id, uint32_t data_transfer_handle,
278 uint16_t fru_table_handle, uint16_t record_set_identifier,
279 uint8_t record_type, uint8_t field_type, uint8_t transfer_op_flag,
280 struct pldm_msg *msg, size_t payload_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930281{
Andrew Jeffery9c766792022-08-10 23:12:49 +0930282 if (msg == NULL) {
283 return PLDM_ERROR_INVALID_DATA;
284 }
285
286 if (payload_length !=
287 sizeof(struct pldm_get_fru_record_by_option_req)) {
288 return PLDM_ERROR_INVALID_LENGTH;
289 }
290
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930291 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930292 header.instance = instance_id;
293 header.msg_type = PLDM_REQUEST;
294 header.pldm_type = PLDM_FRU;
295 header.command = PLDM_GET_FRU_RECORD_BY_OPTION;
296 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
297 if (rc != PLDM_SUCCESS) {
298 return rc;
299 }
300
301 struct pldm_get_fru_record_by_option_req *req =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930302 (struct pldm_get_fru_record_by_option_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930303
304 req->data_transfer_handle = htole32(data_transfer_handle);
305 req->fru_table_handle = htole16(fru_table_handle);
306 req->record_set_identifier = htole16(record_set_identifier);
307 req->record_type = record_type;
308 req->field_type = field_type;
309 req->transfer_op_flag = transfer_op_flag;
310
311 return PLDM_SUCCESS;
312}
313
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930314LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930315int decode_get_fru_record_by_option_req(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930316 const struct pldm_msg *msg, size_t payload_length,
317 uint32_t *data_transfer_handle, uint16_t *fru_table_handle,
318 uint16_t *record_set_identifier, uint8_t *record_type,
319 uint8_t *field_type, uint8_t *transfer_op_flag)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930320{
321 if (msg == NULL || data_transfer_handle == NULL ||
322 fru_table_handle == NULL || record_set_identifier == NULL ||
323 record_type == NULL || field_type == NULL ||
324 transfer_op_flag == NULL) {
325 return PLDM_ERROR_INVALID_DATA;
326 }
327
328 if (payload_length !=
329 sizeof(struct pldm_get_fru_record_by_option_req)) {
330 return PLDM_ERROR_INVALID_LENGTH;
331 }
332
333 struct pldm_get_fru_record_by_option_req *req =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930334 (struct pldm_get_fru_record_by_option_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930335
336 *data_transfer_handle = le32toh(req->data_transfer_handle);
337 *fru_table_handle = le16toh(req->fru_table_handle);
338 *record_set_identifier = le16toh(req->record_set_identifier);
339 *record_type = req->record_type;
340 *field_type = req->field_type;
341 *transfer_op_flag = req->transfer_op_flag;
342 return PLDM_SUCCESS;
343}
344
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930345LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930346int encode_get_fru_record_by_option_resp(uint8_t instance_id,
347 uint8_t completion_code,
348 uint32_t next_data_transfer_handle,
349 uint8_t transfer_flag,
350 const void *fru_structure_data,
351 size_t data_size, struct pldm_msg *msg,
352 size_t payload_length)
353{
354 if (msg == NULL || fru_structure_data == NULL) {
355 return PLDM_ERROR_INVALID_DATA;
356 }
357
358 if (payload_length !=
359 PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES + data_size) {
360 return PLDM_ERROR_INVALID_LENGTH;
361 }
362
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930363 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930364 header.instance = instance_id;
365 header.msg_type = PLDM_RESPONSE;
366 header.pldm_type = PLDM_FRU;
367 header.command = PLDM_GET_FRU_RECORD_BY_OPTION;
368 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
369 if (rc != PLDM_SUCCESS) {
370 return rc;
371 }
372
373 struct pldm_get_fru_record_by_option_resp *resp =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930374 (struct pldm_get_fru_record_by_option_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930375
376 resp->completion_code = completion_code;
377 resp->next_data_transfer_handle = htole32(next_data_transfer_handle);
378 resp->transfer_flag = transfer_flag;
379
380 if (completion_code == PLDM_SUCCESS) {
381 memcpy(resp->fru_structure_data, fru_structure_data, data_size);
382 }
383
384 return PLDM_SUCCESS;
385}
386
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930387LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930388int decode_get_fru_record_by_option_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930389 const struct pldm_msg *msg, size_t payload_length,
390 uint8_t *completion_code, uint32_t *next_transfer_handle,
391 uint8_t *transfer_flag, struct variable_field *fru_structure_data)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930392{
393 if (msg == NULL || completion_code == NULL ||
394 next_transfer_handle == NULL || transfer_flag == NULL ||
395 fru_structure_data == NULL) {
396 return PLDM_ERROR_INVALID_DATA;
397 }
398
399 *completion_code = msg->payload[0];
400 if (PLDM_SUCCESS != *completion_code) {
401 return PLDM_SUCCESS;
402 }
403
404 if (payload_length < PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES) {
405 return PLDM_ERROR_INVALID_LENGTH;
406 }
407
408 struct pldm_get_fru_record_by_option_resp *resp =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930409 (struct pldm_get_fru_record_by_option_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930410
411 *next_transfer_handle = le32toh(resp->next_data_transfer_handle);
412 *transfer_flag = resp->transfer_flag;
413 fru_structure_data->ptr = resp->fru_structure_data;
414 fru_structure_data->length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930415 payload_length - PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930416
417 return PLDM_SUCCESS;
418}
419
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930420LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930421int encode_get_fru_record_table_req(uint8_t instance_id,
422 uint32_t data_transfer_handle,
423 uint8_t transfer_operation_flag,
424 struct pldm_msg *msg, size_t payload_length)
425
426{
427 if (msg == NULL) {
428 return PLDM_ERROR_INVALID_DATA;
429 }
430 if (payload_length != sizeof(struct pldm_get_fru_record_table_req)) {
431 return PLDM_ERROR_INVALID_LENGTH;
432 }
433
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930434 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930435 header.msg_type = PLDM_REQUEST;
436 header.instance = instance_id;
437 header.pldm_type = PLDM_FRU;
438 header.command = PLDM_GET_FRU_RECORD_TABLE;
439
440 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
441 if (rc != PLDM_SUCCESS) {
442 return rc;
443 }
444
445 struct pldm_get_fru_record_table_req *req =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930446 (struct pldm_get_fru_record_table_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930447 req->data_transfer_handle = htole32(data_transfer_handle);
448 req->transfer_operation_flag = transfer_operation_flag;
449
450 return PLDM_SUCCESS;
451}
452
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930453LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930454int decode_get_fru_record_table_resp_safe(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930455 const struct pldm_msg *msg, size_t payload_length,
456 uint8_t *completion_code, uint32_t *next_data_transfer_handle,
457 uint8_t *transfer_flag, uint8_t *fru_record_table_data,
458 size_t *fru_record_table_length, size_t max_fru_record_table_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930459{
460 if (msg == NULL || completion_code == NULL ||
461 next_data_transfer_handle == NULL || transfer_flag == NULL ||
462 fru_record_table_data == NULL || fru_record_table_length == NULL) {
463 return PLDM_ERROR_INVALID_DATA;
464 }
465
466 *completion_code = msg->payload[0];
467 if (PLDM_SUCCESS != *completion_code) {
468 return PLDM_SUCCESS;
469 }
470 if (payload_length <= PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES) {
471 return PLDM_ERROR_INVALID_LENGTH;
472 }
473
474 struct pldm_get_fru_record_table_resp *resp =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930475 (struct pldm_get_fru_record_table_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930476
477 *next_data_transfer_handle = le32toh(resp->next_data_transfer_handle);
478 *transfer_flag = resp->transfer_flag;
479
480 *fru_record_table_length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930481 payload_length - PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930482
483 if (*fru_record_table_length > max_fru_record_table_length) {
484 return PLDM_ERROR_INVALID_LENGTH;
485 }
486
487 memcpy(fru_record_table_data, resp->fru_record_table_data,
488 *fru_record_table_length);
489
490 return PLDM_SUCCESS;
491}
492
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930493LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930494int decode_get_fru_record_table_resp(const struct pldm_msg *msg,
495 size_t payload_length,
496 uint8_t *completion_code,
497 uint32_t *next_data_transfer_handle,
498 uint8_t *transfer_flag,
499 uint8_t *fru_record_table_data,
500 size_t *fru_record_table_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930501{
502 return decode_get_fru_record_table_resp_safe(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930503 msg, payload_length, completion_code, next_data_transfer_handle,
504 transfer_flag, fru_record_table_data, fru_record_table_length,
505 (size_t)-1);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930506}
507
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930508LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930509int decode_set_fru_record_table_req(const struct pldm_msg *msg,
510 size_t payload_length,
511 uint32_t *data_transfer_handle,
512 uint8_t *transfer_flag,
513 struct variable_field *fru_table_data)
514
515{
516 if (msg == NULL || data_transfer_handle == NULL ||
517 transfer_flag == NULL || fru_table_data == NULL) {
518 return PLDM_ERROR_INVALID_DATA;
519 }
520
521 if (payload_length <= PLDM_SET_FRU_RECORD_TABLE_MIN_REQ_BYTES) {
522 return PLDM_ERROR_INVALID_LENGTH;
523 }
524
525 struct pldm_set_fru_record_table_req *req =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930526 (struct pldm_set_fru_record_table_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930527
528 *data_transfer_handle = le32toh(req->data_transfer_handle);
529 *transfer_flag = req->transfer_flag;
530 fru_table_data->length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930531 payload_length - PLDM_SET_FRU_RECORD_TABLE_MIN_REQ_BYTES;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930532 fru_table_data->ptr = req->fru_record_table_data;
533
534 return PLDM_SUCCESS;
535}
536
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930537LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930538int encode_set_fru_record_table_resp(uint8_t instance_id,
539 uint8_t completion_code,
540 uint32_t next_data_transfer_handle,
541 size_t payload_length,
542 struct pldm_msg *msg)
543{
544 if (msg == NULL) {
545 return PLDM_ERROR_INVALID_DATA;
546 }
547 if (payload_length != PLDM_SET_FRU_RECORD_TABLE_RESP_BYTES) {
548 return PLDM_ERROR_INVALID_LENGTH;
549 }
550
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930551 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930552 header.instance = instance_id;
553 header.msg_type = PLDM_RESPONSE;
554 header.pldm_type = PLDM_FRU;
555 header.command = PLDM_SET_FRU_RECORD_TABLE;
556
557 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
558 if (PLDM_SUCCESS != rc) {
559 return rc;
560 }
561
562 struct pldm_set_fru_record_table_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930563 (struct pldm_set_fru_record_table_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930564 response->completion_code = completion_code;
565 response->next_data_transfer_handle =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930566 htole32(next_data_transfer_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930567
568 return PLDM_SUCCESS;
569}