blob: 3aca2c31521cd8e79a542f1203cd2aab46923f3f [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
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060093 uint8_t *dst = msg->body.payload;
94 memcpy(dst, &type, sizeof(type));
95 dst += sizeof(type);
96 memcpy(dst, &version, sizeof(version));
97
98 return PLDM_SUCCESS;
99}
100
101int encode_get_types_resp(uint8_t instance_id, uint8_t completion_code,
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600102 const bitfield8_t *types, struct pldm_msg *msg)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600103{
104 if (msg == NULL) {
105 return PLDM_ERROR_INVALID_DATA;
106 }
107
108 msg->body.payload[0] = completion_code;
Deepak Kodihalli67ec4652019-02-16 07:00:48 -0600109
110 struct pldm_header_info header = {0};
111 header.instance = instance_id;
112 header.msg_type = PLDM_RESPONSE;
113 header.command = PLDM_GET_PLDM_TYPES;
114 pack_pldm_header(&header, &(msg->hdr));
115
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600116 if (msg->body.payload[0] == PLDM_SUCCESS) {
117 if (types == NULL) {
118 return PLDM_ERROR_INVALID_DATA;
119 }
120 uint8_t *dst = msg->body.payload + sizeof(msg->body.payload[0]);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600121 memcpy(dst, &(types->byte), PLDM_MAX_TYPES / 8);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600122 }
123
124 return PLDM_SUCCESS;
125}
126
127int decode_get_commands_req(const struct pldm_msg_payload *msg, uint8_t *type,
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600128 ver32_t *version)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600129{
130 if (msg == NULL || type == NULL || version == NULL) {
131 return PLDM_ERROR_INVALID_DATA;
132 }
133
134 const uint8_t *start = msg->payload;
135 *type = *start;
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600136 memcpy(version, (ver32_t *)(start + sizeof(*type)), sizeof(*version));
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600137
138 return PLDM_SUCCESS;
139}
140
141int encode_get_commands_resp(uint8_t instance_id, uint8_t completion_code,
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600142 const bitfield8_t *commands, struct pldm_msg *msg)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600143{
144 if (msg == NULL) {
145 return PLDM_ERROR_INVALID_DATA;
146 }
147
148 msg->body.payload[0] = completion_code;
Deepak Kodihalli67ec4652019-02-16 07:00:48 -0600149
150 struct pldm_header_info header = {0};
151 header.instance = instance_id;
152 header.msg_type = PLDM_RESPONSE;
153 header.command = PLDM_GET_PLDM_COMMANDS;
154 pack_pldm_header(&header, &(msg->hdr));
155
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600156 if (msg->body.payload[0] == PLDM_SUCCESS) {
157 if (commands == NULL) {
158 return PLDM_ERROR_INVALID_DATA;
159 }
160 uint8_t *dst = msg->body.payload + sizeof(msg->body.payload[0]);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600161 memcpy(dst, &(commands->byte), PLDM_MAX_CMDS_PER_TYPE / 8);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600162 }
163
164 return PLDM_SUCCESS;
165}
166
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600167int decode_get_types_resp(const struct pldm_msg_payload *msg,
168 bitfield8_t *types)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600169{
170 if (msg == NULL || types == NULL) {
171 return PLDM_ERROR_INVALID_DATA;
172 }
173 const uint8_t *src = msg->payload + sizeof(uint8_t);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600174 memcpy(&(types->byte), src, PLDM_MAX_TYPES / 8);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600175
176 return PLDM_SUCCESS;
177}
178
179int decode_get_commands_resp(const struct pldm_msg_payload *msg,
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600180 bitfield8_t *commands)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600181{
182 if (msg == NULL || commands == NULL) {
183 return PLDM_ERROR_INVALID_DATA;
184 }
185 const uint8_t *src = msg->payload + sizeof(uint8_t);
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600186 memcpy(&(commands->byte), src, PLDM_MAX_CMDS_PER_TYPE / 8);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600187
188 return PLDM_SUCCESS;
189}
Sampa Misra432e1872019-02-13 03:49:43 -0600190
191int encode_get_version_req(uint8_t instance_id, uint32_t transfer_handle,
192 uint8_t transfer_opflag, uint8_t type,
193 struct pldm_msg *msg)
194{
195 struct pldm_header_info header = {0};
196 int rc = PLDM_SUCCESS;
197
198 if (NULL == msg) {
199 return PLDM_ERROR_INVALID_DATA;
200 }
201
202 header.msg_type = PLDM_REQUEST;
203 header.instance = instance_id;
204 header.pldm_type = PLDM_BASE;
205 header.command = PLDM_GET_PLDM_VERSION;
206
207 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
208 return rc;
209 }
210
211 uint8_t *dst = msg->body.payload;
212 transfer_handle = htole32(transfer_handle);
213 memcpy(dst, &transfer_handle, sizeof(transfer_handle));
214 dst += sizeof(transfer_handle);
215
216 memcpy(dst, &transfer_opflag, sizeof(transfer_opflag));
217 dst += sizeof(transfer_opflag);
218
219 memcpy(dst, &type, sizeof(type));
220
221 return PLDM_SUCCESS;
222}
223
224int encode_get_version_resp(uint8_t instance_id, uint8_t completion_code,
225 uint32_t next_transfer_handle,
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600226 uint8_t transfer_flag, const ver32_t *version_data,
Sampa Misra432e1872019-02-13 03:49:43 -0600227 size_t version_size, struct pldm_msg *msg)
228{
229 struct pldm_header_info header = {0};
230 int rc = PLDM_SUCCESS;
231
232 msg->body.payload[0] = completion_code;
233 if (msg->body.payload[0] == PLDM_SUCCESS) {
234
235 header.msg_type = PLDM_RESPONSE;
236 header.instance = instance_id;
237 header.pldm_type = PLDM_BASE;
238 header.command = PLDM_GET_PLDM_VERSION;
239
240 if ((rc = pack_pldm_header(&header, &(msg->hdr))) >
241 PLDM_SUCCESS) {
242 return rc;
243 }
244 uint8_t *dst = msg->body.payload + sizeof(msg->body.payload[0]);
245
246 next_transfer_handle = htole32(next_transfer_handle);
247
248 memcpy(dst, &next_transfer_handle,
249 sizeof(next_transfer_handle));
250 dst += sizeof(next_transfer_handle);
251 memcpy(dst, &transfer_flag, sizeof(transfer_flag));
252
253 dst += sizeof(transfer_flag);
254 memcpy(dst, version_data, version_size);
255 }
256 return PLDM_SUCCESS;
257}
258
259int decode_get_version_req(const struct pldm_msg_payload *msg,
260 uint32_t *transfer_handle, uint8_t *transfer_opflag,
261 uint8_t *type)
262{
263 const uint8_t *start = msg->payload;
264 *transfer_handle = le32toh(*((uint32_t *)start));
265 *transfer_opflag = *(start + sizeof(*transfer_handle));
266 *type = *(start + sizeof(*transfer_handle) + sizeof(*transfer_opflag));
267
268 return PLDM_SUCCESS;
269}
270
271int decode_get_version_resp(const struct pldm_msg_payload *msg,
272 uint32_t *next_transfer_handle,
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600273 uint8_t *transfer_flag, ver32_t *version)
Sampa Misra432e1872019-02-13 03:49:43 -0600274{
275 const uint8_t *start = msg->payload + sizeof(uint8_t);
276 *next_transfer_handle = le32toh(*((uint32_t *)start));
277 *transfer_flag = *(start + sizeof(*next_transfer_handle));
278
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -0600279 *version = *((ver32_t *)(start + sizeof(*next_transfer_handle) +
280 sizeof(*transfer_flag)));
Sampa Misra432e1872019-02-13 03:49:43 -0600281
282 return PLDM_SUCCESS;
283}