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 | 962fcec | 2023-07-07 10:10:19 +0930 | [diff] [blame] | 214 | LIBPLDM_ABI_DEPRECATED |
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 | { |
Andrew Jeffery | 7cd16ff | 2023-06-28 21:03:03 +0930 | [diff] [blame] | 219 | 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 | |
Andrew Jeffery | 962fcec | 2023-07-07 10:10:19 +0930 | [diff] [blame] | 225 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 7cd16ff | 2023-06-28 21:03:03 +0930 | [diff] [blame] | 226 | int 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 230 | const struct pldm_fru_record_data_format *record_data_src = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 231 | (const struct pldm_fru_record_data_format *)table; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 232 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 248 | *)((char *)tlv + len); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 249 | } |
| 250 | record_data_src = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 251 | (const struct pldm_fru_record_data_format |
| 252 | *)(tlv); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 253 | 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 Jeffery | 7cd16ff | 2023-06-28 21:03:03 +0930 | [diff] [blame] | 260 | if (pos - record_table + len >= *record_size) { |
| 261 | return PLDM_ERROR_INVALID_LENGTH; |
| 262 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 263 | 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 Jeffery | 7cd16ff | 2023-06-28 21:03:03 +0930 | [diff] [blame] | 274 | if (pos - record_table + len >= *record_size) { |
| 275 | return PLDM_ERROR_INVALID_LENGTH; |
| 276 | } |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 277 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 286 | (const struct pldm_fru_record_data_format *)(tlv); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | *record_size = pos - record_table; |
Andrew Jeffery | 7cd16ff | 2023-06-28 21:03:03 +0930 | [diff] [blame] | 290 | |
| 291 | return PLDM_SUCCESS; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 292 | } |
| 293 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 294 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 295 | int encode_get_fru_record_by_option_req( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 296 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 300 | { |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 301 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 310 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 311 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 321 | (struct pldm_get_fru_record_by_option_req *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 322 | |
| 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 Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 333 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 334 | int decode_get_fru_record_by_option_req( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 335 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 339 | { |
| 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 353 | (struct pldm_get_fru_record_by_option_req *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 354 | |
| 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 Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 364 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 365 | int 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 382 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 383 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 393 | (struct pldm_get_fru_record_by_option_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 394 | |
| 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 Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 406 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 407 | int decode_get_fru_record_by_option_resp( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 408 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 411 | { |
| 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 428 | (struct pldm_get_fru_record_by_option_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 429 | |
| 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 434 | payload_length - PLDM_GET_FRU_RECORD_BY_OPTION_MIN_RESP_BYTES; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 435 | |
| 436 | return PLDM_SUCCESS; |
| 437 | } |
| 438 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 439 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 440 | int 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 453 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 454 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 465 | (struct pldm_get_fru_record_table_req *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 466 | req->data_transfer_handle = htole32(data_transfer_handle); |
| 467 | req->transfer_operation_flag = transfer_operation_flag; |
| 468 | |
| 469 | return PLDM_SUCCESS; |
| 470 | } |
| 471 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 472 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 473 | int decode_get_fru_record_table_resp_safe( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 474 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 478 | { |
| 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 494 | (struct pldm_get_fru_record_table_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 495 | |
| 496 | *next_data_transfer_handle = le32toh(resp->next_data_transfer_handle); |
| 497 | *transfer_flag = resp->transfer_flag; |
| 498 | |
| 499 | *fru_record_table_length = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 500 | payload_length - PLDM_GET_FRU_RECORD_TABLE_MIN_RESP_BYTES; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 501 | |
| 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 Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 512 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 513 | int 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 520 | { |
| 521 | return decode_get_fru_record_table_resp_safe( |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 522 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 525 | } |
| 526 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 527 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 528 | int 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 545 | (struct pldm_set_fru_record_table_req *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 546 | |
| 547 | *data_transfer_handle = le32toh(req->data_transfer_handle); |
| 548 | *transfer_flag = req->transfer_flag; |
| 549 | fru_table_data->length = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 550 | payload_length - PLDM_SET_FRU_RECORD_TABLE_MIN_REQ_BYTES; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 551 | fru_table_data->ptr = req->fru_record_table_data; |
| 552 | |
| 553 | return PLDM_SUCCESS; |
| 554 | } |
| 555 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 556 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 557 | int 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 570 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 571 | 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 Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 582 | (struct pldm_set_fru_record_table_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 583 | response->completion_code = completion_code; |
| 584 | response->next_data_transfer_handle = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 585 | htole32(next_data_transfer_handle); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 586 | |
| 587 | return PLDM_SUCCESS; |
| 588 | } |