blob: 8d336e50d29a2aa30542724b73008672989eab17 [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
80int encode_get_commands_req(uint8_t instance_id, uint8_t type,
81 struct pldm_version version, struct pldm_msg *msg)
82{
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,
102 const uint8_t *types, struct pldm_msg *msg)
103{
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]);
121 memcpy(dst, types, PLDM_MAX_TYPES / 8);
122 }
123
124 return PLDM_SUCCESS;
125}
126
127int decode_get_commands_req(const struct pldm_msg_payload *msg, uint8_t *type,
128 struct pldm_version *version)
129{
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;
136 memcpy(version, (struct pldm_version *)(start + sizeof(*type)),
137 sizeof(*version));
138
139 return PLDM_SUCCESS;
140}
141
142int encode_get_commands_resp(uint8_t instance_id, uint8_t completion_code,
143 const uint8_t *commands, struct pldm_msg *msg)
144{
145 if (msg == NULL) {
146 return PLDM_ERROR_INVALID_DATA;
147 }
148
149 msg->body.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
Deepak Kodihalli1b24f972019-02-01 04:09:13 -0600157 if (msg->body.payload[0] == PLDM_SUCCESS) {
158 if (commands == NULL) {
159 return PLDM_ERROR_INVALID_DATA;
160 }
161 uint8_t *dst = msg->body.payload + sizeof(msg->body.payload[0]);
162 memcpy(dst, commands, PLDM_MAX_CMDS_PER_TYPE / 8);
163 }
164
165 return PLDM_SUCCESS;
166}
167
168int decode_get_types_resp(const struct pldm_msg_payload *msg, uint8_t *types)
169{
170 if (msg == NULL || types == NULL) {
171 return PLDM_ERROR_INVALID_DATA;
172 }
173 const uint8_t *src = msg->payload + sizeof(uint8_t);
174 memcpy(types, src, PLDM_MAX_TYPES / 8);
175
176 return PLDM_SUCCESS;
177}
178
179int decode_get_commands_resp(const struct pldm_msg_payload *msg,
180 uint8_t *commands)
181{
182 if (msg == NULL || commands == NULL) {
183 return PLDM_ERROR_INVALID_DATA;
184 }
185 const uint8_t *src = msg->payload + sizeof(uint8_t);
186 memcpy(commands, src, PLDM_MAX_CMDS_PER_TYPE / 8);
187
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,
226 uint8_t transfer_flag,
227 const struct pldm_version *version_data,
228 size_t version_size, struct pldm_msg *msg)
229{
230 struct pldm_header_info header = {0};
231 int rc = PLDM_SUCCESS;
232
233 msg->body.payload[0] = completion_code;
234 if (msg->body.payload[0] == PLDM_SUCCESS) {
235
236 header.msg_type = PLDM_RESPONSE;
237 header.instance = instance_id;
238 header.pldm_type = PLDM_BASE;
239 header.command = PLDM_GET_PLDM_VERSION;
240
241 if ((rc = pack_pldm_header(&header, &(msg->hdr))) >
242 PLDM_SUCCESS) {
243 return rc;
244 }
245 uint8_t *dst = msg->body.payload + sizeof(msg->body.payload[0]);
246
247 next_transfer_handle = htole32(next_transfer_handle);
248
249 memcpy(dst, &next_transfer_handle,
250 sizeof(next_transfer_handle));
251 dst += sizeof(next_transfer_handle);
252 memcpy(dst, &transfer_flag, sizeof(transfer_flag));
253
254 dst += sizeof(transfer_flag);
255 memcpy(dst, version_data, version_size);
256 }
257 return PLDM_SUCCESS;
258}
259
260int decode_get_version_req(const struct pldm_msg_payload *msg,
261 uint32_t *transfer_handle, uint8_t *transfer_opflag,
262 uint8_t *type)
263{
264 const uint8_t *start = msg->payload;
265 *transfer_handle = le32toh(*((uint32_t *)start));
266 *transfer_opflag = *(start + sizeof(*transfer_handle));
267 *type = *(start + sizeof(*transfer_handle) + sizeof(*transfer_opflag));
268
269 return PLDM_SUCCESS;
270}
271
272int decode_get_version_resp(const struct pldm_msg_payload *msg,
273 uint32_t *next_transfer_handle,
274 uint8_t *transfer_flag,
275 struct pldm_version *version)
276{
277 const uint8_t *start = msg->payload + sizeof(uint8_t);
278 *next_transfer_handle = le32toh(*((uint32_t *)start));
279 *transfer_flag = *(start + sizeof(*next_transfer_handle));
280
281 *version =
282 *((struct pldm_version *)(start + sizeof(*next_transfer_handle) +
283 sizeof(*transfer_flag)));
284
285 return PLDM_SUCCESS;
286}