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 | { |
| 209 | return p == |
| 210 | (const struct pldm_fru_record_data_format *)((uint8_t *)table + |
| 211 | table_size); |
| 212 | } |
| 213 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 214 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 215 | void 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 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 | |
| 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 269 | (const struct pldm_fru_record_data_format *)(tlv); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | *record_size = pos - record_table; |
| 273 | } |
| 274 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 275 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 276 | int encode_get_fru_record_by_option_req( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 277 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 281 | { |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 282 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 291 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 292 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 302 | (struct pldm_get_fru_record_by_option_req *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 303 | |
| 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 Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 314 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 315 | int decode_get_fru_record_by_option_req( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 316 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 320 | { |
| 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 334 | (struct pldm_get_fru_record_by_option_req *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 335 | |
| 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 Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 345 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 346 | int 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 363 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 364 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 374 | (struct pldm_get_fru_record_by_option_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 375 | |
| 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 Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 387 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 388 | int decode_get_fru_record_by_option_resp( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 389 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 392 | { |
| 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 409 | (struct pldm_get_fru_record_by_option_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 410 | |
| 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 415 | payload_length - PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 416 | |
| 417 | return PLDM_SUCCESS; |
| 418 | } |
| 419 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 420 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 421 | int 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 434 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 435 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 446 | (struct pldm_get_fru_record_table_req *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 447 | req->data_transfer_handle = htole32(data_transfer_handle); |
| 448 | req->transfer_operation_flag = transfer_operation_flag; |
| 449 | |
| 450 | return PLDM_SUCCESS; |
| 451 | } |
| 452 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 453 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 454 | int decode_get_fru_record_table_resp_safe( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 455 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 459 | { |
| 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 475 | (struct pldm_get_fru_record_table_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 476 | |
| 477 | *next_data_transfer_handle = le32toh(resp->next_data_transfer_handle); |
| 478 | *transfer_flag = resp->transfer_flag; |
| 479 | |
| 480 | *fru_record_table_length = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 481 | payload_length - PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 482 | |
| 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 Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 493 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 494 | int 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 501 | { |
| 502 | return decode_get_fru_record_table_resp_safe( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 503 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 506 | } |
| 507 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 508 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 509 | int 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 526 | (struct pldm_set_fru_record_table_req *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 527 | |
| 528 | *data_transfer_handle = le32toh(req->data_transfer_handle); |
| 529 | *transfer_flag = req->transfer_flag; |
| 530 | fru_table_data->length = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 531 | payload_length - PLDM_SET_FRU_RECORD_TABLE_MIN_REQ_BYTES; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 532 | fru_table_data->ptr = req->fru_record_table_data; |
| 533 | |
| 534 | return PLDM_SUCCESS; |
| 535 | } |
| 536 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 537 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 538 | int 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 551 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 552 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 563 | (struct pldm_set_fru_record_table_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 564 | response->completion_code = completion_code; |
| 565 | response->next_data_transfer_handle = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 566 | htole32(next_data_transfer_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 567 | |
| 568 | return PLDM_SUCCESS; |
| 569 | } |