Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1 | #include "base.h" |
Manojkiran Eda | 9a8e497 | 2022-11-28 16:38:21 +0530 | [diff] [blame] | 2 | #include "pldm_types.h" |
| 3 | #include <endian.h> |
| 4 | #include <stdint.h> |
| 5 | #include <string.h> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 6 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 7 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 8 | uint8_t pack_pldm_header(const struct pldm_header_info *hdr, |
| 9 | struct pldm_msg_hdr *msg) |
| 10 | { |
| 11 | if (msg == NULL || hdr == NULL) { |
| 12 | return PLDM_ERROR_INVALID_DATA; |
| 13 | } |
| 14 | |
| 15 | if (hdr->msg_type != PLDM_RESPONSE && hdr->msg_type != PLDM_REQUEST && |
| 16 | hdr->msg_type != PLDM_ASYNC_REQUEST_NOTIFY) { |
| 17 | return PLDM_ERROR_INVALID_DATA; |
| 18 | } |
| 19 | |
| 20 | if (hdr->instance > PLDM_INSTANCE_MAX) { |
| 21 | return PLDM_ERROR_INVALID_DATA; |
| 22 | } |
| 23 | |
| 24 | if (hdr->pldm_type > (PLDM_MAX_TYPES - 1)) { |
| 25 | return PLDM_ERROR_INVALID_PLDM_TYPE; |
| 26 | } |
| 27 | |
| 28 | uint8_t datagram = (hdr->msg_type == PLDM_ASYNC_REQUEST_NOTIFY) ? 1 : 0; |
| 29 | |
| 30 | if (hdr->msg_type == PLDM_RESPONSE) { |
| 31 | msg->request = PLDM_RESPONSE; |
| 32 | } else if (hdr->msg_type == PLDM_REQUEST || |
| 33 | hdr->msg_type == PLDM_ASYNC_REQUEST_NOTIFY) { |
| 34 | msg->request = PLDM_REQUEST; |
| 35 | } |
| 36 | msg->datagram = datagram; |
| 37 | msg->reserved = 0; |
| 38 | msg->instance_id = hdr->instance; |
| 39 | msg->header_ver = PLDM_CURRENT_VERSION; |
| 40 | msg->type = hdr->pldm_type; |
| 41 | msg->command = hdr->command; |
| 42 | |
| 43 | return PLDM_SUCCESS; |
| 44 | } |
| 45 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 46 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 47 | uint8_t unpack_pldm_header(const struct pldm_msg_hdr *msg, |
| 48 | struct pldm_header_info *hdr) |
| 49 | { |
| 50 | if (msg == NULL) { |
| 51 | return PLDM_ERROR_INVALID_DATA; |
| 52 | } |
| 53 | |
| 54 | if (msg->request == PLDM_RESPONSE) { |
| 55 | hdr->msg_type = PLDM_RESPONSE; |
| 56 | } else { |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 57 | hdr->msg_type = msg->datagram ? PLDM_ASYNC_REQUEST_NOTIFY : |
| 58 | PLDM_REQUEST; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | hdr->instance = msg->instance_id; |
| 62 | hdr->pldm_type = msg->type; |
| 63 | hdr->command = msg->command; |
| 64 | |
| 65 | return PLDM_SUCCESS; |
| 66 | } |
| 67 | |
Andrew Jeffery | 91c06e9 | 2023-09-22 15:22:54 +0930 | [diff] [blame] | 68 | LIBPLDM_ABI_TESTING |
| 69 | bool pldm_msg_hdr_correlate_response(const struct pldm_msg_hdr *req, |
| 70 | const struct pldm_msg_hdr *resp) |
| 71 | { |
| 72 | return req->instance_id == resp->instance_id && req->request && |
| 73 | !resp->request && req->type == resp->type && |
| 74 | req->command == resp->command; |
| 75 | } |
| 76 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 77 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 78 | int encode_get_types_req(uint8_t instance_id, struct pldm_msg *msg) |
| 79 | { |
| 80 | if (msg == NULL) { |
| 81 | return PLDM_ERROR_INVALID_DATA; |
| 82 | } |
| 83 | |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 84 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 85 | header.instance = instance_id; |
| 86 | header.msg_type = PLDM_REQUEST; |
| 87 | header.command = PLDM_GET_PLDM_TYPES; |
| 88 | |
| 89 | return pack_pldm_header(&header, &(msg->hdr)); |
| 90 | } |
| 91 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 92 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 93 | int encode_get_commands_req(uint8_t instance_id, uint8_t type, ver32_t version, |
| 94 | struct pldm_msg *msg) |
| 95 | { |
| 96 | if (msg == NULL) { |
| 97 | return PLDM_ERROR_INVALID_DATA; |
| 98 | } |
| 99 | |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 100 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 101 | header.instance = instance_id; |
| 102 | header.msg_type = PLDM_REQUEST; |
| 103 | header.command = PLDM_GET_PLDM_COMMANDS; |
| 104 | |
| 105 | uint8_t rc = pack_pldm_header(&header, &(msg->hdr)); |
| 106 | if (rc != PLDM_SUCCESS) { |
| 107 | return rc; |
| 108 | } |
| 109 | |
| 110 | struct pldm_get_commands_req *request = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 111 | (struct pldm_get_commands_req *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 112 | |
| 113 | request->type = type; |
| 114 | request->version = version; |
| 115 | |
| 116 | return PLDM_SUCCESS; |
| 117 | } |
| 118 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 119 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 120 | int encode_get_types_resp(uint8_t instance_id, uint8_t completion_code, |
| 121 | const bitfield8_t *types, struct pldm_msg *msg) |
| 122 | { |
| 123 | if (msg == NULL) { |
| 124 | return PLDM_ERROR_INVALID_DATA; |
| 125 | } |
| 126 | |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 127 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 128 | header.instance = instance_id; |
| 129 | header.msg_type = PLDM_RESPONSE; |
| 130 | header.command = PLDM_GET_PLDM_TYPES; |
| 131 | |
| 132 | uint8_t rc = pack_pldm_header(&header, &(msg->hdr)); |
| 133 | if (rc != PLDM_SUCCESS) { |
| 134 | return rc; |
| 135 | } |
| 136 | |
| 137 | struct pldm_get_types_resp *response = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 138 | (struct pldm_get_types_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 139 | response->completion_code = completion_code; |
| 140 | if (response->completion_code == PLDM_SUCCESS) { |
| 141 | if (types == NULL) { |
| 142 | return PLDM_ERROR_INVALID_DATA; |
| 143 | } |
| 144 | memcpy(response->types, &(types->byte), PLDM_MAX_TYPES / 8); |
| 145 | } |
| 146 | |
| 147 | return PLDM_SUCCESS; |
| 148 | } |
| 149 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 150 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 151 | int decode_get_commands_req(const struct pldm_msg *msg, size_t payload_length, |
| 152 | uint8_t *type, ver32_t *version) |
| 153 | { |
| 154 | if (msg == NULL || type == NULL || version == NULL) { |
| 155 | return PLDM_ERROR_INVALID_DATA; |
| 156 | } |
| 157 | |
| 158 | if (payload_length != PLDM_GET_COMMANDS_REQ_BYTES) { |
| 159 | return PLDM_ERROR_INVALID_LENGTH; |
| 160 | } |
| 161 | |
| 162 | struct pldm_get_commands_req *request = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 163 | (struct pldm_get_commands_req *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 164 | *type = request->type; |
| 165 | *version = request->version; |
| 166 | return PLDM_SUCCESS; |
| 167 | } |
| 168 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 169 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 170 | int encode_get_commands_resp(uint8_t instance_id, uint8_t completion_code, |
| 171 | const bitfield8_t *commands, struct pldm_msg *msg) |
| 172 | { |
| 173 | if (msg == NULL) { |
| 174 | return PLDM_ERROR_INVALID_DATA; |
| 175 | } |
| 176 | |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 177 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 178 | header.instance = instance_id; |
| 179 | header.msg_type = PLDM_RESPONSE; |
| 180 | header.command = PLDM_GET_PLDM_COMMANDS; |
| 181 | uint8_t rc = pack_pldm_header(&header, &(msg->hdr)); |
| 182 | if (rc != PLDM_SUCCESS) { |
| 183 | return rc; |
| 184 | } |
| 185 | |
| 186 | struct pldm_get_commands_resp *response = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 187 | (struct pldm_get_commands_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 188 | response->completion_code = completion_code; |
| 189 | if (response->completion_code == PLDM_SUCCESS) { |
| 190 | if (commands == NULL) { |
| 191 | return PLDM_ERROR_INVALID_DATA; |
| 192 | } |
| 193 | memcpy(response->commands, &(commands->byte), |
| 194 | PLDM_MAX_CMDS_PER_TYPE / 8); |
| 195 | } |
| 196 | |
| 197 | return PLDM_SUCCESS; |
| 198 | } |
| 199 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 200 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 201 | int decode_get_types_resp(const struct pldm_msg *msg, size_t payload_length, |
| 202 | uint8_t *completion_code, bitfield8_t *types) |
| 203 | { |
| 204 | if (msg == NULL || types == NULL || completion_code == NULL) { |
| 205 | return PLDM_ERROR_INVALID_DATA; |
| 206 | } |
| 207 | |
| 208 | *completion_code = msg->payload[0]; |
| 209 | if (PLDM_SUCCESS != *completion_code) { |
| 210 | return PLDM_SUCCESS; |
| 211 | } |
| 212 | |
| 213 | if (payload_length != PLDM_GET_TYPES_RESP_BYTES) { |
| 214 | return PLDM_ERROR_INVALID_LENGTH; |
| 215 | } |
| 216 | |
| 217 | struct pldm_get_types_resp *response = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 218 | (struct pldm_get_types_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 219 | |
| 220 | memcpy(&(types->byte), response->types, PLDM_MAX_TYPES / 8); |
| 221 | |
| 222 | return PLDM_SUCCESS; |
| 223 | } |
| 224 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 225 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 226 | int decode_get_commands_resp(const struct pldm_msg *msg, size_t payload_length, |
| 227 | uint8_t *completion_code, bitfield8_t *commands) |
| 228 | { |
| 229 | if (msg == NULL || commands == NULL || completion_code == NULL) { |
| 230 | return PLDM_ERROR_INVALID_DATA; |
| 231 | } |
| 232 | |
| 233 | *completion_code = msg->payload[0]; |
| 234 | if (PLDM_SUCCESS != *completion_code) { |
| 235 | return PLDM_SUCCESS; |
| 236 | } |
| 237 | |
| 238 | if (payload_length != PLDM_GET_COMMANDS_RESP_BYTES) { |
| 239 | return PLDM_ERROR_INVALID_LENGTH; |
| 240 | } |
| 241 | |
| 242 | struct pldm_get_commands_resp *response = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 243 | (struct pldm_get_commands_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 244 | |
| 245 | memcpy(&(commands->byte), response->commands, |
| 246 | PLDM_MAX_CMDS_PER_TYPE / 8); |
| 247 | |
| 248 | return PLDM_SUCCESS; |
| 249 | } |
| 250 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 251 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 252 | int encode_get_version_req(uint8_t instance_id, uint32_t transfer_handle, |
| 253 | uint8_t transfer_opflag, uint8_t type, |
| 254 | struct pldm_msg *msg) |
| 255 | { |
| 256 | if (NULL == msg) { |
| 257 | return PLDM_ERROR_INVALID_DATA; |
| 258 | } |
| 259 | |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 260 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 261 | header.msg_type = PLDM_REQUEST; |
| 262 | header.instance = instance_id; |
| 263 | header.pldm_type = PLDM_BASE; |
| 264 | header.command = PLDM_GET_PLDM_VERSION; |
| 265 | |
| 266 | uint8_t rc = pack_pldm_header(&header, &(msg->hdr)); |
| 267 | if (rc != PLDM_SUCCESS) { |
| 268 | return rc; |
| 269 | } |
| 270 | |
| 271 | struct pldm_get_version_req *request = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 272 | (struct pldm_get_version_req *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 273 | transfer_handle = htole32(transfer_handle); |
| 274 | request->transfer_handle = transfer_handle; |
| 275 | request->transfer_opflag = transfer_opflag; |
| 276 | request->type = type; |
| 277 | |
| 278 | return PLDM_SUCCESS; |
| 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_version_resp(uint8_t instance_id, uint8_t completion_code, |
| 283 | uint32_t next_transfer_handle, |
| 284 | uint8_t transfer_flag, const ver32_t *version_data, |
| 285 | size_t version_size, struct pldm_msg *msg) |
| 286 | { |
| 287 | if (NULL == msg) { |
| 288 | return PLDM_ERROR_INVALID_DATA; |
| 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.msg_type = PLDM_RESPONSE; |
| 293 | header.instance = instance_id; |
| 294 | header.pldm_type = PLDM_BASE; |
| 295 | header.command = PLDM_GET_PLDM_VERSION; |
| 296 | |
| 297 | uint8_t rc = pack_pldm_header(&header, &(msg->hdr)); |
| 298 | if (rc != PLDM_SUCCESS) { |
| 299 | return rc; |
| 300 | } |
| 301 | |
| 302 | struct pldm_get_version_resp *response = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 303 | (struct pldm_get_version_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 304 | response->completion_code = completion_code; |
| 305 | if (response->completion_code == PLDM_SUCCESS) { |
| 306 | response->next_transfer_handle = htole32(next_transfer_handle); |
| 307 | response->transfer_flag = transfer_flag; |
| 308 | memcpy(response->version_data, (uint8_t *)version_data, |
| 309 | version_size); |
| 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_version_req(const struct pldm_msg *msg, size_t payload_length, |
| 316 | uint32_t *transfer_handle, uint8_t *transfer_opflag, |
| 317 | uint8_t *type) |
| 318 | { |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 319 | if (payload_length != PLDM_GET_VERSION_REQ_BYTES) { |
| 320 | return PLDM_ERROR_INVALID_LENGTH; |
| 321 | } |
| 322 | |
| 323 | struct pldm_get_version_req *request = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 324 | (struct pldm_get_version_req *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 325 | *transfer_handle = le32toh(request->transfer_handle); |
| 326 | *transfer_opflag = request->transfer_opflag; |
| 327 | *type = request->type; |
| 328 | return PLDM_SUCCESS; |
| 329 | } |
| 330 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 331 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 332 | int decode_get_version_resp(const struct pldm_msg *msg, size_t payload_length, |
| 333 | uint8_t *completion_code, |
| 334 | uint32_t *next_transfer_handle, |
| 335 | uint8_t *transfer_flag, ver32_t *version) |
| 336 | { |
| 337 | if (msg == NULL || next_transfer_handle == NULL || |
| 338 | transfer_flag == NULL || completion_code == NULL) { |
| 339 | return PLDM_ERROR_INVALID_DATA; |
| 340 | } |
| 341 | |
| 342 | *completion_code = msg->payload[0]; |
| 343 | if (PLDM_SUCCESS != *completion_code) { |
| 344 | return PLDM_SUCCESS; |
| 345 | } |
| 346 | |
| 347 | if (payload_length < PLDM_GET_VERSION_RESP_BYTES) { |
| 348 | return PLDM_ERROR_INVALID_LENGTH; |
| 349 | } |
| 350 | |
| 351 | struct pldm_get_version_resp *response = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 352 | (struct pldm_get_version_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 353 | |
| 354 | *next_transfer_handle = le32toh(response->next_transfer_handle); |
| 355 | *transfer_flag = response->transfer_flag; |
| 356 | memcpy(version, (uint8_t *)response->version_data, sizeof(ver32_t)); |
| 357 | |
| 358 | return PLDM_SUCCESS; |
| 359 | } |
| 360 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 361 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 362 | int encode_get_tid_req(uint8_t instance_id, struct pldm_msg *msg) |
| 363 | { |
| 364 | if (msg == NULL) { |
| 365 | return PLDM_ERROR_INVALID_DATA; |
| 366 | } |
| 367 | |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 368 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 369 | header.instance = instance_id; |
| 370 | header.msg_type = PLDM_REQUEST; |
| 371 | header.command = PLDM_GET_TID; |
| 372 | |
| 373 | return pack_pldm_header(&header, &(msg->hdr)); |
| 374 | } |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 375 | |
| 376 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 377 | int encode_get_tid_resp(uint8_t instance_id, uint8_t completion_code, |
| 378 | uint8_t tid, struct pldm_msg *msg) |
| 379 | { |
| 380 | if (msg == NULL) { |
| 381 | return PLDM_ERROR_INVALID_DATA; |
| 382 | } |
| 383 | |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 384 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 385 | header.instance = instance_id; |
| 386 | header.msg_type = PLDM_RESPONSE; |
| 387 | header.command = PLDM_GET_TID; |
| 388 | |
| 389 | uint8_t rc = pack_pldm_header(&header, &(msg->hdr)); |
| 390 | if (rc != PLDM_SUCCESS) { |
| 391 | return rc; |
| 392 | } |
| 393 | |
| 394 | struct pldm_get_tid_resp *response = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 395 | (struct pldm_get_tid_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 396 | response->completion_code = completion_code; |
| 397 | response->tid = tid; |
| 398 | |
| 399 | return PLDM_SUCCESS; |
| 400 | } |
| 401 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 402 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 403 | int decode_get_tid_resp(const struct pldm_msg *msg, size_t payload_length, |
| 404 | uint8_t *completion_code, uint8_t *tid) |
| 405 | { |
| 406 | if (msg == NULL || tid == NULL || completion_code == NULL) { |
| 407 | return PLDM_ERROR_INVALID_DATA; |
| 408 | } |
| 409 | |
| 410 | *completion_code = msg->payload[0]; |
| 411 | if (PLDM_SUCCESS != *completion_code) { |
| 412 | return PLDM_SUCCESS; |
| 413 | } |
| 414 | |
| 415 | if (payload_length != PLDM_GET_TID_RESP_BYTES) { |
| 416 | return PLDM_ERROR_INVALID_LENGTH; |
| 417 | } |
| 418 | |
| 419 | struct pldm_get_tid_resp *response = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 420 | (struct pldm_get_tid_resp *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 421 | |
| 422 | *tid = response->tid; |
| 423 | |
| 424 | return PLDM_SUCCESS; |
| 425 | } |
| 426 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 427 | LIBPLDM_ABI_STABLE |
Gilbert Chen | 6c9c917 | 2022-10-18 17:07:29 +0800 | [diff] [blame] | 428 | int encode_set_tid_req(uint8_t instance_id, uint8_t tid, struct pldm_msg *msg) |
| 429 | { |
| 430 | if (msg == NULL) { |
| 431 | return PLDM_ERROR_INVALID_DATA; |
| 432 | } |
| 433 | |
| 434 | if (tid == 0x0 || tid == 0xff) { |
| 435 | return PLDM_ERROR_INVALID_DATA; |
| 436 | } |
| 437 | |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 438 | struct pldm_header_info header = { 0 }; |
Gilbert Chen | 6c9c917 | 2022-10-18 17:07:29 +0800 | [diff] [blame] | 439 | header.instance = instance_id; |
| 440 | header.msg_type = PLDM_REQUEST; |
| 441 | header.command = PLDM_SET_TID; |
| 442 | |
| 443 | uint8_t rc = pack_pldm_header(&header, &(msg->hdr)); |
| 444 | if (rc != PLDM_SUCCESS) { |
| 445 | return rc; |
| 446 | } |
| 447 | |
| 448 | struct pldm_set_tid_req *request = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 449 | (struct pldm_set_tid_req *)msg->payload; |
Gilbert Chen | 6c9c917 | 2022-10-18 17:07:29 +0800 | [diff] [blame] | 450 | request->tid = tid; |
| 451 | |
| 452 | return PLDM_SUCCESS; |
| 453 | } |
| 454 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 455 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 456 | int decode_multipart_receive_req(const struct pldm_msg *msg, |
| 457 | size_t payload_length, uint8_t *pldm_type, |
| 458 | uint8_t *transfer_opflag, |
| 459 | uint32_t *transfer_ctx, |
| 460 | uint32_t *transfer_handle, |
| 461 | uint32_t *section_offset, |
| 462 | uint32_t *section_length) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 463 | { |
| 464 | if (msg == NULL || pldm_type == NULL || transfer_opflag == NULL || |
| 465 | transfer_ctx == NULL || transfer_handle == NULL || |
| 466 | section_offset == NULL || section_length == NULL) { |
| 467 | return PLDM_ERROR_INVALID_DATA; |
| 468 | } |
| 469 | |
| 470 | if (payload_length != PLDM_MULTIPART_RECEIVE_REQ_BYTES) { |
| 471 | return PLDM_ERROR_INVALID_LENGTH; |
| 472 | } |
| 473 | |
| 474 | struct pldm_multipart_receive_req *request = |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 475 | (struct pldm_multipart_receive_req *)msg->payload; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 476 | |
| 477 | if (request->pldm_type != PLDM_BASE) { |
| 478 | return PLDM_ERROR_INVALID_PLDM_TYPE; |
| 479 | } |
| 480 | |
| 481 | // Any enum value above PLDM_XFER_CURRENT_PART is invalid. |
| 482 | if (request->transfer_opflag > PLDM_XFER_CURRENT_PART) { |
| 483 | return PLDM_INVALID_TRANSFER_OPERATION_FLAG; |
| 484 | } |
| 485 | |
| 486 | // A section offset of 0 is only valid on FIRST_PART or COMPLETE Xfers. |
| 487 | uint32_t sec_offset = le32toh(request->section_offset); |
| 488 | if (sec_offset == 0 && |
| 489 | (request->transfer_opflag != PLDM_XFER_FIRST_PART && |
| 490 | request->transfer_opflag != PLDM_XFER_COMPLETE)) { |
| 491 | return PLDM_ERROR_INVALID_DATA; |
| 492 | } |
| 493 | |
| 494 | uint32_t handle = le32toh(request->transfer_handle); |
| 495 | if (handle == 0 && request->transfer_opflag != PLDM_XFER_COMPLETE) { |
| 496 | return PLDM_ERROR_INVALID_DATA; |
| 497 | } |
| 498 | |
| 499 | *pldm_type = request->pldm_type; |
| 500 | *transfer_opflag = request->transfer_opflag; |
| 501 | *transfer_ctx = request->transfer_ctx; |
| 502 | *transfer_handle = handle; |
| 503 | *section_offset = sec_offset; |
| 504 | *section_length = le32toh(request->section_length); |
| 505 | |
| 506 | return PLDM_SUCCESS; |
| 507 | } |
| 508 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 509 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 510 | int encode_cc_only_resp(uint8_t instance_id, uint8_t type, uint8_t command, |
| 511 | uint8_t cc, struct pldm_msg *msg) |
| 512 | { |
| 513 | if (msg == NULL) { |
| 514 | return PLDM_ERROR_INVALID_DATA; |
| 515 | } |
| 516 | |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 517 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 518 | header.instance = instance_id; |
| 519 | header.msg_type = PLDM_RESPONSE; |
| 520 | header.pldm_type = type; |
| 521 | header.command = command; |
| 522 | |
| 523 | uint8_t rc = pack_pldm_header(&header, &msg->hdr); |
| 524 | if (rc != PLDM_SUCCESS) { |
| 525 | return rc; |
| 526 | } |
| 527 | |
| 528 | msg->payload[0] = cc; |
| 529 | |
| 530 | return PLDM_SUCCESS; |
| 531 | } |
| 532 | |
Andrew Jeffery | 9d2a1c6 | 2023-06-05 13:02:16 +0930 | [diff] [blame] | 533 | LIBPLDM_ABI_STABLE |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 534 | int encode_pldm_header_only(uint8_t msg_type, uint8_t instance_id, |
| 535 | uint8_t pldm_type, uint8_t command, |
| 536 | struct pldm_msg *msg) |
| 537 | { |
| 538 | if (msg == NULL) { |
| 539 | return PLDM_ERROR_INVALID_DATA; |
| 540 | } |
| 541 | |
Andrew Jeffery | 37dd6a3 | 2023-05-12 16:04:06 +0930 | [diff] [blame] | 542 | struct pldm_header_info header = { 0 }; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 543 | header.msg_type = msg_type; |
| 544 | header.instance = instance_id; |
| 545 | header.pldm_type = pldm_type; |
| 546 | header.command = command; |
| 547 | return pack_pldm_header(&header, &(msg->hdr)); |
| 548 | } |