blob: b0cb864989f02325045b21dc6d2b3d517aac873b [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{
Andrew Jefferya26f2272023-07-14 11:01:43 +0930209 return p >=
Andrew Jeffery9c766792022-08-10 23:12:49 +0930210 (const struct pldm_fru_record_data_format *)((uint8_t *)table +
211 table_size);
212}
213
Andrew Jeffery962fcec2023-07-07 10:10:19 +0930214LIBPLDM_ABI_STABLE
Andrew Jeffery7cd16ff2023-06-28 21:03:03 +0930215int get_fru_record_by_option_check(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 Jeffery9c766792022-08-10 23:12:49 +0930219 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
Andrew Jeffery7cd16ff2023-06-28 21:03:03 +0930248 if (pos - record_table + len >= *record_size) {
249 return PLDM_ERROR_INVALID_LENGTH;
250 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930251 memcpy(pos, record_data_src, len);
252
253 record_data_dest = (struct pldm_fru_record_data_format *)pos;
254 pos += len;
255
256 tlv = record_data_src->tlvs;
257 count = 0;
258 for (int i = 0; i < record_data_src->num_fru_fields; i++) {
259 len = sizeof(*tlv) - 1 + tlv->length;
260 if (tlv->type == ft || ft == 0) {
Andrew Jeffery7cd16ff2023-06-28 21:03:03 +0930261 if (pos - record_table + len >= *record_size) {
262 return PLDM_ERROR_INVALID_LENGTH;
263 }
Andrew Jeffery9c766792022-08-10 23:12:49 +0930264 memcpy(pos, tlv, len);
265 pos += len;
266 count++;
267 }
268 tlv = (const struct pldm_fru_record_tlv *)((char *)tlv +
269 len);
270 }
271 record_data_dest->num_fru_fields = count;
272 record_data_src =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930273 (const struct pldm_fru_record_data_format *)(tlv);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930274 }
275
276 *record_size = pos - record_table;
Andrew Jeffery7cd16ff2023-06-28 21:03:03 +0930277
278 return PLDM_SUCCESS;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930279}
280
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930281LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930282int encode_get_fru_record_by_option_req(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930283 uint8_t instance_id, uint32_t data_transfer_handle,
284 uint16_t fru_table_handle, uint16_t record_set_identifier,
285 uint8_t record_type, uint8_t field_type, uint8_t transfer_op_flag,
286 struct pldm_msg *msg, size_t payload_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930287{
Andrew Jeffery9c766792022-08-10 23:12:49 +0930288 if (msg == NULL) {
289 return PLDM_ERROR_INVALID_DATA;
290 }
291
292 if (payload_length !=
293 sizeof(struct pldm_get_fru_record_by_option_req)) {
294 return PLDM_ERROR_INVALID_LENGTH;
295 }
296
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930297 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930298 header.instance = instance_id;
299 header.msg_type = PLDM_REQUEST;
300 header.pldm_type = PLDM_FRU;
301 header.command = PLDM_GET_FRU_RECORD_BY_OPTION;
302 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
303 if (rc != PLDM_SUCCESS) {
304 return rc;
305 }
306
307 struct pldm_get_fru_record_by_option_req *req =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930308 (struct pldm_get_fru_record_by_option_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930309
310 req->data_transfer_handle = htole32(data_transfer_handle);
311 req->fru_table_handle = htole16(fru_table_handle);
312 req->record_set_identifier = htole16(record_set_identifier);
313 req->record_type = record_type;
314 req->field_type = field_type;
315 req->transfer_op_flag = transfer_op_flag;
316
317 return PLDM_SUCCESS;
318}
319
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930320LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930321int decode_get_fru_record_by_option_req(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930322 const struct pldm_msg *msg, size_t payload_length,
323 uint32_t *data_transfer_handle, uint16_t *fru_table_handle,
324 uint16_t *record_set_identifier, uint8_t *record_type,
325 uint8_t *field_type, uint8_t *transfer_op_flag)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930326{
327 if (msg == NULL || data_transfer_handle == NULL ||
328 fru_table_handle == NULL || record_set_identifier == NULL ||
329 record_type == NULL || field_type == NULL ||
330 transfer_op_flag == NULL) {
331 return PLDM_ERROR_INVALID_DATA;
332 }
333
334 if (payload_length !=
335 sizeof(struct pldm_get_fru_record_by_option_req)) {
336 return PLDM_ERROR_INVALID_LENGTH;
337 }
338
339 struct pldm_get_fru_record_by_option_req *req =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930340 (struct pldm_get_fru_record_by_option_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930341
342 *data_transfer_handle = le32toh(req->data_transfer_handle);
343 *fru_table_handle = le16toh(req->fru_table_handle);
344 *record_set_identifier = le16toh(req->record_set_identifier);
345 *record_type = req->record_type;
346 *field_type = req->field_type;
347 *transfer_op_flag = req->transfer_op_flag;
348 return PLDM_SUCCESS;
349}
350
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930351LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930352int encode_get_fru_record_by_option_resp(uint8_t instance_id,
353 uint8_t completion_code,
354 uint32_t next_data_transfer_handle,
355 uint8_t transfer_flag,
356 const void *fru_structure_data,
357 size_t data_size, struct pldm_msg *msg,
358 size_t payload_length)
359{
360 if (msg == NULL || fru_structure_data == NULL) {
361 return PLDM_ERROR_INVALID_DATA;
362 }
363
364 if (payload_length !=
365 PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES + data_size) {
366 return PLDM_ERROR_INVALID_LENGTH;
367 }
368
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930369 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930370 header.instance = instance_id;
371 header.msg_type = PLDM_RESPONSE;
372 header.pldm_type = PLDM_FRU;
373 header.command = PLDM_GET_FRU_RECORD_BY_OPTION;
374 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
375 if (rc != PLDM_SUCCESS) {
376 return rc;
377 }
378
379 struct pldm_get_fru_record_by_option_resp *resp =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930380 (struct pldm_get_fru_record_by_option_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930381
382 resp->completion_code = completion_code;
383 resp->next_data_transfer_handle = htole32(next_data_transfer_handle);
384 resp->transfer_flag = transfer_flag;
385
386 if (completion_code == PLDM_SUCCESS) {
387 memcpy(resp->fru_structure_data, fru_structure_data, data_size);
388 }
389
390 return PLDM_SUCCESS;
391}
392
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930393LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930394int decode_get_fru_record_by_option_resp(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930395 const struct pldm_msg *msg, size_t payload_length,
396 uint8_t *completion_code, uint32_t *next_transfer_handle,
397 uint8_t *transfer_flag, struct variable_field *fru_structure_data)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930398{
399 if (msg == NULL || completion_code == NULL ||
400 next_transfer_handle == NULL || transfer_flag == NULL ||
401 fru_structure_data == NULL) {
402 return PLDM_ERROR_INVALID_DATA;
403 }
404
405 *completion_code = msg->payload[0];
406 if (PLDM_SUCCESS != *completion_code) {
407 return PLDM_SUCCESS;
408 }
409
410 if (payload_length < PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES) {
411 return PLDM_ERROR_INVALID_LENGTH;
412 }
413
414 struct pldm_get_fru_record_by_option_resp *resp =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930415 (struct pldm_get_fru_record_by_option_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930416
417 *next_transfer_handle = le32toh(resp->next_data_transfer_handle);
418 *transfer_flag = resp->transfer_flag;
419 fru_structure_data->ptr = resp->fru_structure_data;
420 fru_structure_data->length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930421 payload_length - PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930422
423 return PLDM_SUCCESS;
424}
425
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930426LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930427int encode_get_fru_record_table_req(uint8_t instance_id,
428 uint32_t data_transfer_handle,
429 uint8_t transfer_operation_flag,
430 struct pldm_msg *msg, size_t payload_length)
431
432{
433 if (msg == NULL) {
434 return PLDM_ERROR_INVALID_DATA;
435 }
436 if (payload_length != sizeof(struct pldm_get_fru_record_table_req)) {
437 return PLDM_ERROR_INVALID_LENGTH;
438 }
439
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930440 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930441 header.msg_type = PLDM_REQUEST;
442 header.instance = instance_id;
443 header.pldm_type = PLDM_FRU;
444 header.command = PLDM_GET_FRU_RECORD_TABLE;
445
446 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
447 if (rc != PLDM_SUCCESS) {
448 return rc;
449 }
450
451 struct pldm_get_fru_record_table_req *req =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930452 (struct pldm_get_fru_record_table_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930453 req->data_transfer_handle = htole32(data_transfer_handle);
454 req->transfer_operation_flag = transfer_operation_flag;
455
456 return PLDM_SUCCESS;
457}
458
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930459LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930460int decode_get_fru_record_table_resp_safe(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930461 const struct pldm_msg *msg, size_t payload_length,
462 uint8_t *completion_code, uint32_t *next_data_transfer_handle,
463 uint8_t *transfer_flag, uint8_t *fru_record_table_data,
464 size_t *fru_record_table_length, size_t max_fru_record_table_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930465{
466 if (msg == NULL || completion_code == NULL ||
467 next_data_transfer_handle == NULL || transfer_flag == NULL ||
468 fru_record_table_data == NULL || fru_record_table_length == NULL) {
469 return PLDM_ERROR_INVALID_DATA;
470 }
471
472 *completion_code = msg->payload[0];
473 if (PLDM_SUCCESS != *completion_code) {
474 return PLDM_SUCCESS;
475 }
476 if (payload_length <= PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES) {
477 return PLDM_ERROR_INVALID_LENGTH;
478 }
479
480 struct pldm_get_fru_record_table_resp *resp =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930481 (struct pldm_get_fru_record_table_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930482
483 *next_data_transfer_handle = le32toh(resp->next_data_transfer_handle);
484 *transfer_flag = resp->transfer_flag;
485
486 *fru_record_table_length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930487 payload_length - PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930488
489 if (*fru_record_table_length > max_fru_record_table_length) {
490 return PLDM_ERROR_INVALID_LENGTH;
491 }
492
493 memcpy(fru_record_table_data, resp->fru_record_table_data,
494 *fru_record_table_length);
495
496 return PLDM_SUCCESS;
497}
498
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930499LIBPLDM_ABI_STABLE
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930500int decode_get_fru_record_table_resp(const struct pldm_msg *msg,
501 size_t payload_length,
502 uint8_t *completion_code,
503 uint32_t *next_data_transfer_handle,
504 uint8_t *transfer_flag,
505 uint8_t *fru_record_table_data,
506 size_t *fru_record_table_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930507{
508 return decode_get_fru_record_table_resp_safe(
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930509 msg, payload_length, completion_code, next_data_transfer_handle,
510 transfer_flag, fru_record_table_data, fru_record_table_length,
511 (size_t)-1);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930512}
513
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930514LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930515int decode_set_fru_record_table_req(const struct pldm_msg *msg,
516 size_t payload_length,
517 uint32_t *data_transfer_handle,
518 uint8_t *transfer_flag,
519 struct variable_field *fru_table_data)
520
521{
522 if (msg == NULL || data_transfer_handle == NULL ||
523 transfer_flag == NULL || fru_table_data == NULL) {
524 return PLDM_ERROR_INVALID_DATA;
525 }
526
527 if (payload_length <= PLDM_SET_FRU_RECORD_TABLE_MIN_REQ_BYTES) {
528 return PLDM_ERROR_INVALID_LENGTH;
529 }
530
531 struct pldm_set_fru_record_table_req *req =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930532 (struct pldm_set_fru_record_table_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930533
534 *data_transfer_handle = le32toh(req->data_transfer_handle);
535 *transfer_flag = req->transfer_flag;
536 fru_table_data->length =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930537 payload_length - PLDM_SET_FRU_RECORD_TABLE_MIN_REQ_BYTES;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930538 fru_table_data->ptr = req->fru_record_table_data;
539
540 return PLDM_SUCCESS;
541}
542
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +0930543LIBPLDM_ABI_STABLE
Andrew Jeffery9c766792022-08-10 23:12:49 +0930544int encode_set_fru_record_table_resp(uint8_t instance_id,
545 uint8_t completion_code,
546 uint32_t next_data_transfer_handle,
547 size_t payload_length,
548 struct pldm_msg *msg)
549{
550 if (msg == NULL) {
551 return PLDM_ERROR_INVALID_DATA;
552 }
553 if (payload_length != PLDM_SET_FRU_RECORD_TABLE_RESP_BYTES) {
554 return PLDM_ERROR_INVALID_LENGTH;
555 }
556
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930557 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930558 header.instance = instance_id;
559 header.msg_type = PLDM_RESPONSE;
560 header.pldm_type = PLDM_FRU;
561 header.command = PLDM_SET_FRU_RECORD_TABLE;
562
563 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
564 if (PLDM_SUCCESS != rc) {
565 return rc;
566 }
567
568 struct pldm_set_fru_record_table_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930569 (struct pldm_set_fru_record_table_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930570 response->completion_code = completion_code;
571 response->next_data_transfer_handle =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930572 htole32(next_data_transfer_handle);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930573
574 return PLDM_SUCCESS;
575}