Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 1 | #include <endian.h> |
| 2 | #include <string.h> |
| 3 | |
| 4 | #include "base.h" |
| 5 | |
Tom Joseph | 4125104 | 2019-02-07 16:17:07 +0530 | [diff] [blame] | 6 | int pack_pldm_header(const struct pldm_header_info *hdr, |
| 7 | struct pldm_msg_hdr *msg) |
| 8 | { |
| 9 | if (msg == NULL || hdr == NULL) { |
| 10 | return PLDM_ERROR_INVALID_DATA; |
| 11 | } |
| 12 | |
| 13 | if (hdr->msg_type != PLDM_RESPONSE && hdr->msg_type != PLDM_REQUEST && |
| 14 | hdr->msg_type != PLDM_ASYNC_REQUEST_NOTIFY) { |
| 15 | return PLDM_ERROR_INVALID_DATA; |
| 16 | } |
| 17 | |
| 18 | if (hdr->instance > PLDM_INSTANCE_MAX) { |
| 19 | return PLDM_ERROR_INVALID_DATA; |
| 20 | } |
| 21 | |
| 22 | if (hdr->pldm_type > (PLDM_MAX_TYPES - 1)) { |
| 23 | return PLDM_ERROR_INVALID_PLDM_TYPE; |
| 24 | } |
| 25 | |
| 26 | uint8_t datagram = (hdr->msg_type == PLDM_ASYNC_REQUEST_NOTIFY) ? 1 : 0; |
| 27 | |
| 28 | if (hdr->msg_type == PLDM_RESPONSE) { |
| 29 | msg->request = PLDM_RESPONSE; |
| 30 | } else if (hdr->msg_type == PLDM_REQUEST || |
| 31 | hdr->msg_type == PLDM_ASYNC_REQUEST_NOTIFY) { |
| 32 | msg->request = PLDM_REQUEST; |
| 33 | } |
| 34 | msg->datagram = datagram; |
| 35 | msg->reserved = 0; |
| 36 | msg->instance_id = hdr->instance; |
| 37 | msg->header_ver = 0; |
| 38 | msg->type = hdr->pldm_type; |
| 39 | msg->command = hdr->command; |
| 40 | |
| 41 | return PLDM_SUCCESS; |
| 42 | } |
| 43 | |
| 44 | int unpack_pldm_header(const struct pldm_msg_hdr *msg, |
| 45 | struct pldm_header_info *hdr) |
| 46 | { |
| 47 | if (msg == NULL) { |
| 48 | return PLDM_ERROR_INVALID_DATA; |
| 49 | } |
| 50 | |
| 51 | if (msg->request == PLDM_RESPONSE) { |
| 52 | hdr->msg_type = PLDM_RESPONSE; |
| 53 | } else { |
| 54 | hdr->msg_type = |
| 55 | msg->datagram ? PLDM_ASYNC_REQUEST_NOTIFY : PLDM_REQUEST; |
| 56 | } |
| 57 | |
| 58 | hdr->instance = msg->instance_id; |
| 59 | hdr->pldm_type = msg->type; |
| 60 | hdr->command = msg->command; |
| 61 | |
| 62 | return PLDM_SUCCESS; |
| 63 | } |
| 64 | |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 65 | int encode_get_types_req(uint8_t instance_id, struct pldm_msg *msg) |
| 66 | { |
| 67 | if (msg == NULL) { |
| 68 | return PLDM_ERROR_INVALID_DATA; |
| 69 | } |
| 70 | |
Deepak Kodihalli | 67ec465 | 2019-02-16 07:00:48 -0600 | [diff] [blame] | 71 | struct pldm_header_info header = {0}; |
| 72 | header.instance = instance_id; |
| 73 | header.msg_type = PLDM_REQUEST; |
| 74 | header.command = PLDM_GET_PLDM_TYPES; |
| 75 | pack_pldm_header(&header, &(msg->hdr)); |
| 76 | |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 77 | return PLDM_SUCCESS; |
| 78 | } |
| 79 | |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 80 | int encode_get_commands_req(uint8_t instance_id, uint8_t type, ver32_t version, |
| 81 | struct pldm_msg *msg) |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 82 | { |
| 83 | if (msg == NULL) { |
| 84 | return PLDM_ERROR_INVALID_DATA; |
| 85 | } |
| 86 | |
Deepak Kodihalli | 67ec465 | 2019-02-16 07:00:48 -0600 | [diff] [blame] | 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 | pack_pldm_header(&header, &(msg->hdr)); |
| 92 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 93 | memcpy(msg->payload, &type, sizeof(type)); |
| 94 | memcpy(msg->payload + sizeof(type), &version, sizeof(version)); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 95 | |
| 96 | return PLDM_SUCCESS; |
| 97 | } |
| 98 | |
| 99 | int encode_get_types_resp(uint8_t instance_id, uint8_t completion_code, |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 100 | const bitfield8_t *types, struct pldm_msg *msg) |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 101 | { |
| 102 | if (msg == NULL) { |
| 103 | return PLDM_ERROR_INVALID_DATA; |
| 104 | } |
| 105 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 106 | msg->payload[0] = completion_code; |
Deepak Kodihalli | 67ec465 | 2019-02-16 07:00:48 -0600 | [diff] [blame] | 107 | |
| 108 | struct pldm_header_info header = {0}; |
| 109 | header.instance = instance_id; |
| 110 | header.msg_type = PLDM_RESPONSE; |
| 111 | header.command = PLDM_GET_PLDM_TYPES; |
| 112 | pack_pldm_header(&header, &(msg->hdr)); |
| 113 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 114 | if (msg->payload[0] == PLDM_SUCCESS) { |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 115 | if (types == NULL) { |
| 116 | return PLDM_ERROR_INVALID_DATA; |
| 117 | } |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 118 | memcpy(msg->payload + sizeof(completion_code), &(types->byte), |
| 119 | PLDM_MAX_TYPES / 8); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | return PLDM_SUCCESS; |
| 123 | } |
| 124 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 125 | int decode_get_commands_req(const uint8_t *msg, size_t payload_length, |
| 126 | uint8_t *type, ver32_t *version) |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 127 | { |
| 128 | if (msg == NULL || type == NULL || version == NULL) { |
| 129 | return PLDM_ERROR_INVALID_DATA; |
| 130 | } |
| 131 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 132 | if (payload_length != PLDM_GET_COMMANDS_REQ_BYTES) { |
| 133 | return PLDM_ERROR_INVALID_LENGTH; |
| 134 | } |
| 135 | |
| 136 | *type = *msg; |
| 137 | memcpy(version, (ver32_t *)(msg + sizeof(*type)), sizeof(*version)); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 138 | |
| 139 | return PLDM_SUCCESS; |
| 140 | } |
| 141 | |
| 142 | int encode_get_commands_resp(uint8_t instance_id, uint8_t completion_code, |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 143 | const bitfield8_t *commands, struct pldm_msg *msg) |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 144 | { |
| 145 | if (msg == NULL) { |
| 146 | return PLDM_ERROR_INVALID_DATA; |
| 147 | } |
| 148 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 149 | msg->payload[0] = completion_code; |
Deepak Kodihalli | 67ec465 | 2019-02-16 07:00:48 -0600 | [diff] [blame] | 150 | |
| 151 | struct pldm_header_info header = {0}; |
| 152 | header.instance = instance_id; |
| 153 | header.msg_type = PLDM_RESPONSE; |
| 154 | header.command = PLDM_GET_PLDM_COMMANDS; |
| 155 | pack_pldm_header(&header, &(msg->hdr)); |
| 156 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 157 | if (msg->payload[0] == PLDM_SUCCESS) { |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 158 | if (commands == NULL) { |
| 159 | return PLDM_ERROR_INVALID_DATA; |
| 160 | } |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 161 | memcpy(msg->payload + sizeof(completion_code), |
| 162 | &(commands->byte), PLDM_MAX_CMDS_PER_TYPE / 8); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | return PLDM_SUCCESS; |
| 166 | } |
| 167 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 168 | int decode_get_types_resp(const uint8_t *msg, size_t payload_length, |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 169 | uint8_t *completion_code, bitfield8_t *types) |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 170 | { |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 171 | if (msg == NULL || types == NULL || completion_code == NULL) { |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 172 | return PLDM_ERROR_INVALID_DATA; |
| 173 | } |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 174 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 175 | if (payload_length != PLDM_GET_TYPES_RESP_BYTES) { |
| 176 | return PLDM_ERROR_INVALID_LENGTH; |
| 177 | } |
| 178 | |
| 179 | *completion_code = msg[0]; |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 180 | if (PLDM_SUCCESS != *completion_code) { |
| 181 | return PLDM_SUCCESS; |
| 182 | }; |
| 183 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 184 | memcpy(&(types->byte), msg + sizeof(*completion_code), |
| 185 | PLDM_MAX_TYPES / 8); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 186 | |
| 187 | return PLDM_SUCCESS; |
| 188 | } |
| 189 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 190 | int decode_get_commands_resp(const uint8_t *msg, size_t payload_length, |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 191 | uint8_t *completion_code, bitfield8_t *commands) |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 192 | { |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 193 | if (msg == NULL || commands == NULL || completion_code == NULL) { |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 194 | return PLDM_ERROR_INVALID_DATA; |
| 195 | } |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 196 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 197 | if (payload_length != PLDM_GET_COMMANDS_RESP_BYTES) { |
| 198 | return PLDM_ERROR_INVALID_LENGTH; |
| 199 | } |
| 200 | |
| 201 | *completion_code = msg[0]; |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 202 | if (PLDM_SUCCESS != *completion_code) { |
| 203 | return PLDM_SUCCESS; |
| 204 | }; |
| 205 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 206 | memcpy(&(commands->byte), msg + sizeof(*completion_code), |
| 207 | PLDM_MAX_CMDS_PER_TYPE / 8); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 208 | |
| 209 | return PLDM_SUCCESS; |
| 210 | } |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 211 | |
| 212 | int encode_get_version_req(uint8_t instance_id, uint32_t transfer_handle, |
| 213 | uint8_t transfer_opflag, uint8_t type, |
| 214 | struct pldm_msg *msg) |
| 215 | { |
| 216 | struct pldm_header_info header = {0}; |
| 217 | int rc = PLDM_SUCCESS; |
| 218 | |
| 219 | if (NULL == msg) { |
| 220 | return PLDM_ERROR_INVALID_DATA; |
| 221 | } |
| 222 | |
| 223 | header.msg_type = PLDM_REQUEST; |
| 224 | header.instance = instance_id; |
| 225 | header.pldm_type = PLDM_BASE; |
| 226 | header.command = PLDM_GET_PLDM_VERSION; |
| 227 | |
| 228 | if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) { |
| 229 | return rc; |
| 230 | } |
| 231 | |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 232 | transfer_handle = htole32(transfer_handle); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 233 | memcpy(msg->payload, &transfer_handle, sizeof(transfer_handle)); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 234 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 235 | memcpy(msg->payload + sizeof(transfer_handle), &transfer_opflag, |
| 236 | sizeof(transfer_opflag)); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 237 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 238 | memcpy(msg->payload + sizeof(transfer_handle) + sizeof(transfer_opflag), |
| 239 | &type, sizeof(type)); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 240 | |
| 241 | return PLDM_SUCCESS; |
| 242 | } |
| 243 | |
| 244 | int encode_get_version_resp(uint8_t instance_id, uint8_t completion_code, |
| 245 | uint32_t next_transfer_handle, |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 246 | uint8_t transfer_flag, const ver32_t *version_data, |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 247 | size_t version_size, struct pldm_msg *msg) |
| 248 | { |
| 249 | struct pldm_header_info header = {0}; |
| 250 | int rc = PLDM_SUCCESS; |
| 251 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 252 | msg->payload[0] = completion_code; |
| 253 | if (msg->payload[0] == PLDM_SUCCESS) { |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 254 | |
| 255 | header.msg_type = PLDM_RESPONSE; |
| 256 | header.instance = instance_id; |
| 257 | header.pldm_type = PLDM_BASE; |
| 258 | header.command = PLDM_GET_PLDM_VERSION; |
| 259 | |
| 260 | if ((rc = pack_pldm_header(&header, &(msg->hdr))) > |
| 261 | PLDM_SUCCESS) { |
| 262 | return rc; |
| 263 | } |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 264 | next_transfer_handle = htole32(next_transfer_handle); |
| 265 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 266 | memcpy(msg->payload + sizeof(msg->payload[0]), |
| 267 | &next_transfer_handle, sizeof(next_transfer_handle)); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 268 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 269 | memcpy(msg->payload + sizeof(msg->payload[0]) + |
| 270 | sizeof(next_transfer_handle), |
| 271 | &transfer_flag, sizeof(transfer_flag)); |
| 272 | |
| 273 | memcpy(msg->payload + sizeof(msg->payload[0]) + |
| 274 | sizeof(next_transfer_handle) + sizeof(transfer_flag), |
| 275 | version_data, version_size); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 276 | } |
| 277 | return PLDM_SUCCESS; |
| 278 | } |
| 279 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 280 | int decode_get_version_req(const uint8_t *msg, size_t payload_length, |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 281 | uint32_t *transfer_handle, uint8_t *transfer_opflag, |
| 282 | uint8_t *type) |
| 283 | { |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 284 | |
| 285 | if (payload_length != PLDM_GET_VERSION_REQ_BYTES) { |
| 286 | return PLDM_ERROR_INVALID_LENGTH; |
| 287 | } |
| 288 | |
| 289 | *transfer_handle = le32toh(*((uint32_t *)msg)); |
| 290 | *transfer_opflag = *(msg + sizeof(*transfer_handle)); |
| 291 | *type = *(msg + sizeof(*transfer_handle) + sizeof(*transfer_opflag)); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 292 | |
| 293 | return PLDM_SUCCESS; |
| 294 | } |
| 295 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 296 | int decode_get_version_resp(const uint8_t *msg, size_t payload_length, |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 297 | uint8_t *completion_code, |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 298 | uint32_t *next_transfer_handle, |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 299 | uint8_t *transfer_flag, ver32_t *version) |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 300 | { |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 301 | if (msg == NULL || next_transfer_handle == NULL || |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 302 | transfer_flag == NULL || completion_code == NULL) { |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 303 | return PLDM_ERROR_INVALID_DATA; |
| 304 | } |
| 305 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 306 | if (payload_length < PLDM_GET_VERSION_RESP_BYTES) { |
| 307 | return PLDM_ERROR_INVALID_LENGTH; |
| 308 | } |
| 309 | |
| 310 | *completion_code = msg[0]; |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 311 | if (PLDM_SUCCESS != *completion_code) { |
| 312 | return PLDM_SUCCESS; |
| 313 | }; |
| 314 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 315 | *next_transfer_handle = |
| 316 | le32toh(*((uint32_t *)(msg + sizeof(*completion_code)))); |
| 317 | *transfer_flag = |
| 318 | *(msg + sizeof(*completion_code) + sizeof(*next_transfer_handle)); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 319 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 320 | *version = *((ver32_t *)(msg + sizeof(*completion_code) + |
| 321 | sizeof(*next_transfer_handle) + |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 322 | sizeof(*transfer_flag))); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 323 | |
| 324 | return PLDM_SUCCESS; |
| 325 | } |