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