blob: 18a12a66832f5d956b56eaaedf6f5b8f45a4801b [file] [log] [blame]
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06001#include <endian.h>
2#include <string.h>
3
4#include "base.h"
5
Tom Joseph41251042019-02-07 16:17:07 +05306int 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
44int 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 Kodihalli1b24f972019-02-01 04:09:13 -060065int 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 Kodihalli67ec4652019-02-16 07:00:48 -060071 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 Kodihalli1b24f972019-02-01 04:09:13 -060077 return PLDM_SUCCESS;
78}
79
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060080int encode_get_commands_req(uint8_t instance_id, uint8_t type, ver32_t version,
81 struct pldm_msg *msg)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060082{
83 if (msg == NULL) {
84 return PLDM_ERROR_INVALID_DATA;
85 }
86
Deepak Kodihalli67ec4652019-02-16 07:00:48 -060087 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
vkaverapa6575b82019-04-03 05:33:52 -050093 memcpy(msg->payload, &type, sizeof(type));
94 memcpy(msg->payload + sizeof(type), &version, sizeof(version));
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060095
96 return PLDM_SUCCESS;
97}
98
99int encode_get_types_resp(uint8_t instance_id, uint8_t completion_code,
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600100 const bitfield8_t *types, struct pldm_msg *msg)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600101{
102 if (msg == NULL) {
103 return PLDM_ERROR_INVALID_DATA;
104 }
105
vkaverapa6575b82019-04-03 05:33:52 -0500106 msg->payload[0] = completion_code;
Deepak Kodihalli67ec4652019-02-16 07:00:48 -0600107
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
vkaverapa6575b82019-04-03 05:33:52 -0500114 if (msg->payload[0] == PLDM_SUCCESS) {
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600115 if (types == NULL) {
116 return PLDM_ERROR_INVALID_DATA;
117 }
vkaverapa6575b82019-04-03 05:33:52 -0500118 memcpy(msg->payload + sizeof(completion_code), &(types->byte),
119 PLDM_MAX_TYPES / 8);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600120 }
121
122 return PLDM_SUCCESS;
123}
124
vkaverapa6575b82019-04-03 05:33:52 -0500125int decode_get_commands_req(const uint8_t *msg, size_t payload_length,
126 uint8_t *type, ver32_t *version)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600127{
128 if (msg == NULL || type == NULL || version == NULL) {
129 return PLDM_ERROR_INVALID_DATA;
130 }
131
vkaverapa6575b82019-04-03 05:33:52 -0500132 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 Kodihalli1b24f972019-02-01 04:09:13 -0600138
139 return PLDM_SUCCESS;
140}
141
142int encode_get_commands_resp(uint8_t instance_id, uint8_t completion_code,
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600143 const bitfield8_t *commands, struct pldm_msg *msg)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600144{
145 if (msg == NULL) {
146 return PLDM_ERROR_INVALID_DATA;
147 }
148
vkaverapa6575b82019-04-03 05:33:52 -0500149 msg->payload[0] = completion_code;
Deepak Kodihalli67ec4652019-02-16 07:00:48 -0600150
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
vkaverapa6575b82019-04-03 05:33:52 -0500157 if (msg->payload[0] == PLDM_SUCCESS) {
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600158 if (commands == NULL) {
159 return PLDM_ERROR_INVALID_DATA;
160 }
vkaverapa6575b82019-04-03 05:33:52 -0500161 memcpy(msg->payload + sizeof(completion_code),
162 &(commands->byte), PLDM_MAX_CMDS_PER_TYPE / 8);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600163 }
164
165 return PLDM_SUCCESS;
166}
167
vkaverapa6575b82019-04-03 05:33:52 -0500168int decode_get_types_resp(const uint8_t *msg, size_t payload_length,
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600169 uint8_t *completion_code, bitfield8_t *types)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600170{
vkaverapa6575b82019-04-03 05:33:52 -0500171 if (msg == NULL || types == NULL || completion_code == NULL) {
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600172 return PLDM_ERROR_INVALID_DATA;
173 }
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600174
vkaverapa6575b82019-04-03 05:33:52 -0500175 if (payload_length != PLDM_GET_TYPES_RESP_BYTES) {
176 return PLDM_ERROR_INVALID_LENGTH;
177 }
178
179 *completion_code = msg[0];
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600180 if (PLDM_SUCCESS != *completion_code) {
181 return PLDM_SUCCESS;
182 };
183
vkaverapa6575b82019-04-03 05:33:52 -0500184 memcpy(&(types->byte), msg + sizeof(*completion_code),
185 PLDM_MAX_TYPES / 8);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600186
187 return PLDM_SUCCESS;
188}
189
vkaverapa6575b82019-04-03 05:33:52 -0500190int decode_get_commands_resp(const uint8_t *msg, size_t payload_length,
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600191 uint8_t *completion_code, bitfield8_t *commands)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600192{
vkaverapa6575b82019-04-03 05:33:52 -0500193 if (msg == NULL || commands == NULL || completion_code == NULL) {
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600194 return PLDM_ERROR_INVALID_DATA;
195 }
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600196
vkaverapa6575b82019-04-03 05:33:52 -0500197 if (payload_length != PLDM_GET_COMMANDS_RESP_BYTES) {
198 return PLDM_ERROR_INVALID_LENGTH;
199 }
200
201 *completion_code = msg[0];
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600202 if (PLDM_SUCCESS != *completion_code) {
203 return PLDM_SUCCESS;
204 };
205
vkaverapa6575b82019-04-03 05:33:52 -0500206 memcpy(&(commands->byte), msg + sizeof(*completion_code),
207 PLDM_MAX_CMDS_PER_TYPE / 8);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600208
209 return PLDM_SUCCESS;
210}
Sampa Misra432e1872019-02-13 03:49:43 -0600211
212int 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 Misra432e1872019-02-13 03:49:43 -0600232 transfer_handle = htole32(transfer_handle);
vkaverapa6575b82019-04-03 05:33:52 -0500233 memcpy(msg->payload, &transfer_handle, sizeof(transfer_handle));
Sampa Misra432e1872019-02-13 03:49:43 -0600234
vkaverapa6575b82019-04-03 05:33:52 -0500235 memcpy(msg->payload + sizeof(transfer_handle), &transfer_opflag,
236 sizeof(transfer_opflag));
Sampa Misra432e1872019-02-13 03:49:43 -0600237
vkaverapa6575b82019-04-03 05:33:52 -0500238 memcpy(msg->payload + sizeof(transfer_handle) + sizeof(transfer_opflag),
239 &type, sizeof(type));
Sampa Misra432e1872019-02-13 03:49:43 -0600240
241 return PLDM_SUCCESS;
242}
243
244int encode_get_version_resp(uint8_t instance_id, uint8_t completion_code,
245 uint32_t next_transfer_handle,
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600246 uint8_t transfer_flag, const ver32_t *version_data,
Sampa Misra432e1872019-02-13 03:49:43 -0600247 size_t version_size, struct pldm_msg *msg)
248{
249 struct pldm_header_info header = {0};
250 int rc = PLDM_SUCCESS;
251
vkaverapa6575b82019-04-03 05:33:52 -0500252 msg->payload[0] = completion_code;
253 if (msg->payload[0] == PLDM_SUCCESS) {
Sampa Misra432e1872019-02-13 03:49:43 -0600254
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 Misra432e1872019-02-13 03:49:43 -0600264 next_transfer_handle = htole32(next_transfer_handle);
265
vkaverapa6575b82019-04-03 05:33:52 -0500266 memcpy(msg->payload + sizeof(msg->payload[0]),
267 &next_transfer_handle, sizeof(next_transfer_handle));
Sampa Misra432e1872019-02-13 03:49:43 -0600268
vkaverapa6575b82019-04-03 05:33:52 -0500269 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 Misra432e1872019-02-13 03:49:43 -0600276 }
277 return PLDM_SUCCESS;
278}
279
vkaverapa6575b82019-04-03 05:33:52 -0500280int decode_get_version_req(const uint8_t *msg, size_t payload_length,
Sampa Misra432e1872019-02-13 03:49:43 -0600281 uint32_t *transfer_handle, uint8_t *transfer_opflag,
282 uint8_t *type)
283{
vkaverapa6575b82019-04-03 05:33:52 -0500284
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 Misra432e1872019-02-13 03:49:43 -0600292
293 return PLDM_SUCCESS;
294}
295
vkaverapa6575b82019-04-03 05:33:52 -0500296int decode_get_version_resp(const uint8_t *msg, size_t payload_length,
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600297 uint8_t *completion_code,
Sampa Misra432e1872019-02-13 03:49:43 -0600298 uint32_t *next_transfer_handle,
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600299 uint8_t *transfer_flag, ver32_t *version)
Sampa Misra432e1872019-02-13 03:49:43 -0600300{
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600301 if (msg == NULL || next_transfer_handle == NULL ||
vkaverapa6575b82019-04-03 05:33:52 -0500302 transfer_flag == NULL || completion_code == NULL) {
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600303 return PLDM_ERROR_INVALID_DATA;
304 }
305
vkaverapa6575b82019-04-03 05:33:52 -0500306 if (payload_length < PLDM_GET_VERSION_RESP_BYTES) {
307 return PLDM_ERROR_INVALID_LENGTH;
308 }
309
310 *completion_code = msg[0];
Deepak Kodihalli8c643462019-02-21 10:43:36 -0600311 if (PLDM_SUCCESS != *completion_code) {
312 return PLDM_SUCCESS;
313 };
314
vkaverapa6575b82019-04-03 05:33:52 -0500315 *next_transfer_handle =
316 le32toh(*((uint32_t *)(msg + sizeof(*completion_code))));
317 *transfer_flag =
318 *(msg + sizeof(*completion_code) + sizeof(*next_transfer_handle));
Sampa Misra432e1872019-02-13 03:49:43 -0600319
vkaverapa6575b82019-04-03 05:33:52 -0500320 *version = *((ver32_t *)(msg + sizeof(*completion_code) +
321 sizeof(*next_transfer_handle) +
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600322 sizeof(*transfer_flag)));
Sampa Misra432e1872019-02-13 03:49:43 -0600323
324 return PLDM_SUCCESS;
325}