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