blob: 39d8576c7056a93410ecbd08a7c39148781f295c [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{
Andrew Jeffery7cd16ff2023-06-28 21:03:03 +0930219 int rc = get_fru_record_by_option_check(table, table_size, record_table,
220 record_size, rsi, rt, ft);
221 (void)rc;
222 assert(rc == PLDM_SUCCESS);
223}
224
225LIBPLDM_ABI_TESTING
226int get_fru_record_by_option_check(const uint8_t *table, size_t table_size,
227 uint8_t *record_table, size_t *record_size,
228 uint16_t rsi, uint8_t rt, uint8_t ft)
229{
Andrew Jeffery9c766792022-08-10 23:12:49 +0930230 const struct pldm_fru_record_data_format *record_data_src =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930231 (const struct pldm_fru_record_data_format *)table;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930232 struct pldm_fru_record_data_format *record_data_dest;
233 int count = 0;
234
235 const struct pldm_fru_record_tlv *tlv;
236 size_t len;
237 uint8_t *pos = record_table;
238
239 while (!is_table_end(record_data_src, table, table_size)) {
240 if ((record_data_src->record_set_id != htole16(rsi) &&
241 rsi != 0) ||
242 (record_data_src->record_type != rt && rt != 0)) {
243 tlv = record_data_src->tlvs;
244 for (int i = 0; i < record_data_src->num_fru_fields;
245 i++) {
246 len = sizeof(*tlv) - 1 + tlv->length;
247 tlv = (const struct pldm_fru_record_tlv
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930248 *)((char *)tlv + len);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930249 }
250 record_data_src =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930251 (const struct pldm_fru_record_data_format
252 *)(tlv);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930253 continue;
254 }
255
256 len = sizeof(struct pldm_fru_record_data_format) -
257 sizeof(struct pldm_fru_record_tlv);
258
259 assert(pos - record_table + len < *record_size);
Andrew Jeffery7cd16ff2023-06-28 21:03:03 +0930260 if (pos - record_table + len >= *record_size) {
261 return PLDM_ERROR_INVALID_LENGTH;
262 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930263 memcpy(pos, record_data_src, len);
264
265 record_data_dest = (struct pldm_fru_record_data_format *)pos;
266 pos += len;
267
268 tlv = record_data_src->tlvs;
269 count = 0;
270 for (int i = 0; i < record_data_src->num_fru_fields; i++) {
271 len = sizeof(*tlv) - 1 + tlv->length;
272 if (tlv->type == ft || ft == 0) {
273 assert(pos - record_table + len < *record_size);
Andrew Jeffery7cd16ff2023-06-28 21:03:03 +0930274 if (pos - record_table + len >= *record_size) {
275 return PLDM_ERROR_INVALID_LENGTH;
276 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930277 memcpy(pos, tlv, len);
278 pos += len;
279 count++;
280 }
281 tlv = (const struct pldm_fru_record_tlv *)((char *)tlv +
282 len);
283 }
284 record_data_dest->num_fru_fields = count;
285 record_data_src =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930286 (const struct pldm_fru_record_data_format *)(tlv);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930287 }
288
289 *record_size = pos - record_table;
Andrew Jeffery7cd16ff2023-06-28 21:03:03 +0930290
291 return PLDM_SUCCESS;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930292}
293
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930294LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930295int encode_get_fru_record_by_option_req(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930296 uint8_t instance_id, uint32_t data_transfer_handle,
297 uint16_t fru_table_handle, uint16_t record_set_identifier,
298 uint8_t record_type, uint8_t field_type, uint8_t transfer_op_flag,
299 struct pldm_msg *msg, size_t payload_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930300{
Andrew Jeffery9c766792022-08-10 23:12:49 +0930301 if (msg == NULL) {
302 return PLDM_ERROR_INVALID_DATA;
303 }
304
305 if (payload_length !=
306 sizeof(struct pldm_get_fru_record_by_option_req)) {
307 return PLDM_ERROR_INVALID_LENGTH;
308 }
309
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930310 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930311 header.instance = instance_id;
312 header.msg_type = PLDM_REQUEST;
313 header.pldm_type = PLDM_FRU;
314 header.command = PLDM_GET_FRU_RECORD_BY_OPTION;
315 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
316 if (rc != PLDM_SUCCESS) {
317 return rc;
318 }
319
320 struct pldm_get_fru_record_by_option_req *req =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930321 (struct pldm_get_fru_record_by_option_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930322
323 req->data_transfer_handle = htole32(data_transfer_handle);
324 req->fru_table_handle = htole16(fru_table_handle);
325 req->record_set_identifier = htole16(record_set_identifier);
326 req->record_type = record_type;
327 req->field_type = field_type;
328 req->transfer_op_flag = transfer_op_flag;
329
330 return PLDM_SUCCESS;
331}
332
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930333LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930334int decode_get_fru_record_by_option_req(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930335 const struct pldm_msg *msg, size_t payload_length,
336 uint32_t *data_transfer_handle, uint16_t *fru_table_handle,
337 uint16_t *record_set_identifier, uint8_t *record_type,
338 uint8_t *field_type, uint8_t *transfer_op_flag)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930339{
340 if (msg == NULL || data_transfer_handle == NULL ||
341 fru_table_handle == NULL || record_set_identifier == NULL ||
342 record_type == NULL || field_type == NULL ||
343 transfer_op_flag == NULL) {
344 return PLDM_ERROR_INVALID_DATA;
345 }
346
347 if (payload_length !=
348 sizeof(struct pldm_get_fru_record_by_option_req)) {
349 return PLDM_ERROR_INVALID_LENGTH;
350 }
351
352 struct pldm_get_fru_record_by_option_req *req =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930353 (struct pldm_get_fru_record_by_option_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930354
355 *data_transfer_handle = le32toh(req->data_transfer_handle);
356 *fru_table_handle = le16toh(req->fru_table_handle);
357 *record_set_identifier = le16toh(req->record_set_identifier);
358 *record_type = req->record_type;
359 *field_type = req->field_type;
360 *transfer_op_flag = req->transfer_op_flag;
361 return PLDM_SUCCESS;
362}
363
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930364LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930365int encode_get_fru_record_by_option_resp(uint8_t instance_id,
366 uint8_t completion_code,
367 uint32_t next_data_transfer_handle,
368 uint8_t transfer_flag,
369 const void *fru_structure_data,
370 size_t data_size, struct pldm_msg *msg,
371 size_t payload_length)
372{
373 if (msg == NULL || fru_structure_data == NULL) {
374 return PLDM_ERROR_INVALID_DATA;
375 }
376
377 if (payload_length !=
378 PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES + data_size) {
379 return PLDM_ERROR_INVALID_LENGTH;
380 }
381
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930382 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930383 header.instance = instance_id;
384 header.msg_type = PLDM_RESPONSE;
385 header.pldm_type = PLDM_FRU;
386 header.command = PLDM_GET_FRU_RECORD_BY_OPTION;
387 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
388 if (rc != PLDM_SUCCESS) {
389 return rc;
390 }
391
392 struct pldm_get_fru_record_by_option_resp *resp =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930393 (struct pldm_get_fru_record_by_option_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930394
395 resp->completion_code = completion_code;
396 resp->next_data_transfer_handle = htole32(next_data_transfer_handle);
397 resp->transfer_flag = transfer_flag;
398
399 if (completion_code == PLDM_SUCCESS) {
400 memcpy(resp->fru_structure_data, fru_structure_data, data_size);
401 }
402
403 return PLDM_SUCCESS;
404}
405
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930406LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930407int decode_get_fru_record_by_option_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930408 const struct pldm_msg *msg, size_t payload_length,
409 uint8_t *completion_code, uint32_t *next_transfer_handle,
410 uint8_t *transfer_flag, struct variable_field *fru_structure_data)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930411{
412 if (msg == NULL || completion_code == NULL ||
413 next_transfer_handle == NULL || transfer_flag == NULL ||
414 fru_structure_data == NULL) {
415 return PLDM_ERROR_INVALID_DATA;
416 }
417
418 *completion_code = msg->payload[0];
419 if (PLDM_SUCCESS != *completion_code) {
420 return PLDM_SUCCESS;
421 }
422
423 if (payload_length < PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES) {
424 return PLDM_ERROR_INVALID_LENGTH;
425 }
426
427 struct pldm_get_fru_record_by_option_resp *resp =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930428 (struct pldm_get_fru_record_by_option_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930429
430 *next_transfer_handle = le32toh(resp->next_data_transfer_handle);
431 *transfer_flag = resp->transfer_flag;
432 fru_structure_data->ptr = resp->fru_structure_data;
433 fru_structure_data->length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930434 payload_length - PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930435
436 return PLDM_SUCCESS;
437}
438
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930439LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930440int encode_get_fru_record_table_req(uint8_t instance_id,
441 uint32_t data_transfer_handle,
442 uint8_t transfer_operation_flag,
443 struct pldm_msg *msg, size_t payload_length)
444
445{
446 if (msg == NULL) {
447 return PLDM_ERROR_INVALID_DATA;
448 }
449 if (payload_length != sizeof(struct pldm_get_fru_record_table_req)) {
450 return PLDM_ERROR_INVALID_LENGTH;
451 }
452
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930453 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930454 header.msg_type = PLDM_REQUEST;
455 header.instance = instance_id;
456 header.pldm_type = PLDM_FRU;
457 header.command = PLDM_GET_FRU_RECORD_TABLE;
458
459 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
460 if (rc != PLDM_SUCCESS) {
461 return rc;
462 }
463
464 struct pldm_get_fru_record_table_req *req =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930465 (struct pldm_get_fru_record_table_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930466 req->data_transfer_handle = htole32(data_transfer_handle);
467 req->transfer_operation_flag = transfer_operation_flag;
468
469 return PLDM_SUCCESS;
470}
471
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930472LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930473int decode_get_fru_record_table_resp_safe(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930474 const struct pldm_msg *msg, size_t payload_length,
475 uint8_t *completion_code, uint32_t *next_data_transfer_handle,
476 uint8_t *transfer_flag, uint8_t *fru_record_table_data,
477 size_t *fru_record_table_length, size_t max_fru_record_table_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930478{
479 if (msg == NULL || completion_code == NULL ||
480 next_data_transfer_handle == NULL || transfer_flag == NULL ||
481 fru_record_table_data == NULL || fru_record_table_length == NULL) {
482 return PLDM_ERROR_INVALID_DATA;
483 }
484
485 *completion_code = msg->payload[0];
486 if (PLDM_SUCCESS != *completion_code) {
487 return PLDM_SUCCESS;
488 }
489 if (payload_length <= PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES) {
490 return PLDM_ERROR_INVALID_LENGTH;
491 }
492
493 struct pldm_get_fru_record_table_resp *resp =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930494 (struct pldm_get_fru_record_table_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930495
496 *next_data_transfer_handle = le32toh(resp->next_data_transfer_handle);
497 *transfer_flag = resp->transfer_flag;
498
499 *fru_record_table_length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930500 payload_length - PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930501
502 if (*fru_record_table_length > max_fru_record_table_length) {
503 return PLDM_ERROR_INVALID_LENGTH;
504 }
505
506 memcpy(fru_record_table_data, resp->fru_record_table_data,
507 *fru_record_table_length);
508
509 return PLDM_SUCCESS;
510}
511
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930512LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930513int decode_get_fru_record_table_resp(const struct pldm_msg *msg,
514 size_t payload_length,
515 uint8_t *completion_code,
516 uint32_t *next_data_transfer_handle,
517 uint8_t *transfer_flag,
518 uint8_t *fru_record_table_data,
519 size_t *fru_record_table_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930520{
521 return decode_get_fru_record_table_resp_safe(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930522 msg, payload_length, completion_code, next_data_transfer_handle,
523 transfer_flag, fru_record_table_data, fru_record_table_length,
524 (size_t)-1);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930525}
526
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930527LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930528int decode_set_fru_record_table_req(const struct pldm_msg *msg,
529 size_t payload_length,
530 uint32_t *data_transfer_handle,
531 uint8_t *transfer_flag,
532 struct variable_field *fru_table_data)
533
534{
535 if (msg == NULL || data_transfer_handle == NULL ||
536 transfer_flag == NULL || fru_table_data == NULL) {
537 return PLDM_ERROR_INVALID_DATA;
538 }
539
540 if (payload_length <= PLDM_SET_FRU_RECORD_TABLE_MIN_REQ_BYTES) {
541 return PLDM_ERROR_INVALID_LENGTH;
542 }
543
544 struct pldm_set_fru_record_table_req *req =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930545 (struct pldm_set_fru_record_table_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930546
547 *data_transfer_handle = le32toh(req->data_transfer_handle);
548 *transfer_flag = req->transfer_flag;
549 fru_table_data->length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930550 payload_length - PLDM_SET_FRU_RECORD_TABLE_MIN_REQ_BYTES;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930551 fru_table_data->ptr = req->fru_record_table_data;
552
553 return PLDM_SUCCESS;
554}
555
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930556LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930557int encode_set_fru_record_table_resp(uint8_t instance_id,
558 uint8_t completion_code,
559 uint32_t next_data_transfer_handle,
560 size_t payload_length,
561 struct pldm_msg *msg)
562{
563 if (msg == NULL) {
564 return PLDM_ERROR_INVALID_DATA;
565 }
566 if (payload_length != PLDM_SET_FRU_RECORD_TABLE_RESP_BYTES) {
567 return PLDM_ERROR_INVALID_LENGTH;
568 }
569
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930570 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930571 header.instance = instance_id;
572 header.msg_type = PLDM_RESPONSE;
573 header.pldm_type = PLDM_FRU;
574 header.command = PLDM_SET_FRU_RECORD_TABLE;
575
576 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
577 if (PLDM_SUCCESS != rc) {
578 return rc;
579 }
580
581 struct pldm_set_fru_record_table_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930582 (struct pldm_set_fru_record_table_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930583 response->completion_code = completion_code;
584 response->next_data_transfer_handle =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930585 htole32(next_data_transfer_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930586
587 return PLDM_SUCCESS;
588}