Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1 | #include <assert.h> |
| 2 | #include <endian.h> |
| 3 | #include <stdbool.h> |
Manojkiran Eda | 9a8e497 | 2022-11-28 16:38:21 +0530 | [diff] [blame] | 4 | #include <stdint.h> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 5 | #include <string.h> |
| 6 | |
Manojkiran Eda | 9a8e497 | 2022-11-28 16:38:21 +0530 | [diff] [blame] | 7 | #include "base.h" |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 8 | #include "fru.h" |
Manojkiran Eda | 9a8e497 | 2022-11-28 16:38:21 +0530 | [diff] [blame] | 9 | #include "utils.h" |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 10 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 11 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 12 | int 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 24 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 25 | 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 Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 33 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 34 | int decode_get_fru_record_table_metadata_resp( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 35 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 40 | { |
| 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 59 | (struct pldm_get_fru_record_table_metadata_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 60 | |
| 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 66 | le16toh(response->total_record_set_identifiers); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 67 | *total_table_records = le16toh(response->total_table_records); |
| 68 | *checksum = le32toh(response->checksum); |
| 69 | |
| 70 | return PLDM_SUCCESS; |
| 71 | } |
| 72 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 73 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 74 | int encode_get_fru_record_table_metadata_resp( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 75 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 80 | { |
| 81 | if (msg == NULL) { |
| 82 | return PLDM_ERROR_INVALID_DATA; |
| 83 | } |
| 84 | |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 85 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 86 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 97 | (struct pldm_get_fru_record_table_metadata_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 98 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 103 | htole32(fru_table_maximum_size); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 104 | response->fru_table_length = htole32(fru_table_length); |
| 105 | response->total_record_set_identifiers = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 106 | htole16(total_record_set_identifiers); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 107 | response->total_table_records = htole16(total_table_records); |
| 108 | response->checksum = htole32(checksum); |
| 109 | } |
| 110 | |
| 111 | return PLDM_SUCCESS; |
| 112 | } |
| 113 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 114 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 115 | int 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 130 | (struct pldm_get_fru_record_table_req *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 131 | |
| 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 Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 138 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 139 | int 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 149 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 150 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 161 | (struct pldm_get_fru_record_table_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 162 | |
| 163 | resp->completion_code = completion_code; |
| 164 | |
| 165 | if (resp->completion_code == PLDM_SUCCESS) { |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 166 | resp->next_data_transfer_handle = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 167 | htole32(next_data_transfer_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 168 | resp->transfer_flag = transfer_flag; |
| 169 | } |
| 170 | |
| 171 | return PLDM_SUCCESS; |
| 172 | } |
| 173 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 174 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 175 | int 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 191 | (struct pldm_fru_record_data_format *)(fru_table + *curr_size); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 192 | 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 | |
| 206 | static bool is_table_end(const struct pldm_fru_record_data_format *p, |
| 207 | const void *table, size_t table_size) |
| 208 | { |
Andrew Jeffery | a26f227 | 2023-07-14 11:01:43 +0930 | [diff] [blame] | 209 | return p >= |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 210 | (const struct pldm_fru_record_data_format *)((uint8_t *)table + |
| 211 | table_size); |
| 212 | } |
| 213 | |
Andrew Jeffery | 962fcec | 2023-07-07 10:10:19 +0930 | [diff] [blame] | 214 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 7cd16ff | 2023-06-28 21:03:03 +0930 | [diff] [blame] | 215 | int 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 219 | const struct pldm_fru_record_data_format *record_data_src = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 220 | (const struct pldm_fru_record_data_format *)table; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 221 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 237 | *)((char *)tlv + len); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 238 | } |
| 239 | record_data_src = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 240 | (const struct pldm_fru_record_data_format |
| 241 | *)(tlv); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 242 | continue; |
| 243 | } |
| 244 | |
| 245 | len = sizeof(struct pldm_fru_record_data_format) - |
| 246 | sizeof(struct pldm_fru_record_tlv); |
| 247 | |
Andrew Jeffery | 7cd16ff | 2023-06-28 21:03:03 +0930 | [diff] [blame] | 248 | if (pos - record_table + len >= *record_size) { |
| 249 | return PLDM_ERROR_INVALID_LENGTH; |
| 250 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 251 | 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 Jeffery | 7cd16ff | 2023-06-28 21:03:03 +0930 | [diff] [blame] | 261 | if (pos - record_table + len >= *record_size) { |
| 262 | return PLDM_ERROR_INVALID_LENGTH; |
| 263 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 264 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 273 | (const struct pldm_fru_record_data_format *)(tlv); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | *record_size = pos - record_table; |
Andrew Jeffery | 7cd16ff | 2023-06-28 21:03:03 +0930 | [diff] [blame] | 277 | |
| 278 | return PLDM_SUCCESS; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 279 | } |
| 280 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 281 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 282 | int encode_get_fru_record_by_option_req( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 283 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 287 | { |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 288 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 297 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 298 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 308 | (struct pldm_get_fru_record_by_option_req *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 309 | |
| 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 Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 320 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 321 | int decode_get_fru_record_by_option_req( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 322 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 326 | { |
| 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 340 | (struct pldm_get_fru_record_by_option_req *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 341 | |
| 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 Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 351 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 352 | int 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 369 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 370 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 380 | (struct pldm_get_fru_record_by_option_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 381 | |
| 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 Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 393 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 394 | int decode_get_fru_record_by_option_resp( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 395 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 398 | { |
| 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 415 | (struct pldm_get_fru_record_by_option_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 416 | |
| 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 421 | payload_length - PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 422 | |
| 423 | return PLDM_SUCCESS; |
| 424 | } |
| 425 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 426 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 427 | int 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 440 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 441 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 452 | (struct pldm_get_fru_record_table_req *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 453 | req->data_transfer_handle = htole32(data_transfer_handle); |
| 454 | req->transfer_operation_flag = transfer_operation_flag; |
| 455 | |
| 456 | return PLDM_SUCCESS; |
| 457 | } |
| 458 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 459 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 460 | int decode_get_fru_record_table_resp_safe( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 461 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 465 | { |
| 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 481 | (struct pldm_get_fru_record_table_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 482 | |
| 483 | *next_data_transfer_handle = le32toh(resp->next_data_transfer_handle); |
| 484 | *transfer_flag = resp->transfer_flag; |
| 485 | |
| 486 | *fru_record_table_length = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 487 | payload_length - PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 488 | |
| 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 Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 499 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 500 | int 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 507 | { |
| 508 | return decode_get_fru_record_table_resp_safe( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 509 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 512 | } |
| 513 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 514 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 515 | int 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 532 | (struct pldm_set_fru_record_table_req *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 533 | |
| 534 | *data_transfer_handle = le32toh(req->data_transfer_handle); |
| 535 | *transfer_flag = req->transfer_flag; |
| 536 | fru_table_data->length = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 537 | payload_length - PLDM_SET_FRU_RECORD_TABLE_MIN_REQ_BYTES; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 538 | fru_table_data->ptr = req->fru_record_table_data; |
| 539 | |
| 540 | return PLDM_SUCCESS; |
| 541 | } |
| 542 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 543 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 544 | int 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 557 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 558 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 569 | (struct pldm_set_fru_record_table_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 570 | response->completion_code = completion_code; |
| 571 | response->next_data_transfer_handle = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 572 | htole32(next_data_transfer_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 573 | |
| 574 | return PLDM_SUCCESS; |
| 575 | } |