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