blob: 90e9c1e9e88a6d0353017997f04821b4369fdf62 [file] [log] [blame]
Andrew Jeffery9c766792022-08-10 23:12:49 +09301#include "base.h"
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05302#include "pldm_types.h"
3#include <endian.h>
4#include <stdint.h>
5#include <string.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +09306
7uint8_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
45uint8_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 {
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093055 hdr->msg_type = msg->datagram ? PLDM_ASYNC_REQUEST_NOTIFY :
56 PLDM_REQUEST;
Andrew Jeffery9c766792022-08-10 23:12:49 +093057 }
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
66int 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
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093072 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +093073 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
80int 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
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093087 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +093088 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 =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +093098 (struct pldm_get_commands_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +093099
100 request->type = type;
101 request->version = version;
102
103 return PLDM_SUCCESS;
104}
105
106int 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
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930113 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930114 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 =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930124 (struct pldm_get_types_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930125 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
136int 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 =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930148 (struct pldm_get_commands_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930149 *type = request->type;
150 *version = request->version;
151 return PLDM_SUCCESS;
152}
153
154int 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
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930161 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930162 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 =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930171 (struct pldm_get_commands_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930172 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
184int 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 =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930201 (struct pldm_get_types_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930202
203 memcpy(&(types->byte), response->types, PLDM_MAX_TYPES / 8);
204
205 return PLDM_SUCCESS;
206}
207
208int 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 =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930225 (struct pldm_get_commands_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930226
227 memcpy(&(commands->byte), response->commands,
228 PLDM_MAX_CMDS_PER_TYPE / 8);
229
230 return PLDM_SUCCESS;
231}
232
233int 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
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930241 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930242 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 =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930253 (struct pldm_get_version_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930254 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
262int 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
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930271 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930272 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 =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930283 (struct pldm_get_version_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930284 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
294int 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{
Andrew Jeffery9c766792022-08-10 23:12:49 +0930298 if (payload_length != PLDM_GET_VERSION_REQ_BYTES) {
299 return PLDM_ERROR_INVALID_LENGTH;
300 }
301
302 struct pldm_get_version_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930303 (struct pldm_get_version_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930304 *transfer_handle = le32toh(request->transfer_handle);
305 *transfer_opflag = request->transfer_opflag;
306 *type = request->type;
307 return PLDM_SUCCESS;
308}
309
310int decode_get_version_resp(const struct pldm_msg *msg, size_t payload_length,
311 uint8_t *completion_code,
312 uint32_t *next_transfer_handle,
313 uint8_t *transfer_flag, ver32_t *version)
314{
315 if (msg == NULL || next_transfer_handle == NULL ||
316 transfer_flag == NULL || completion_code == NULL) {
317 return PLDM_ERROR_INVALID_DATA;
318 }
319
320 *completion_code = msg->payload[0];
321 if (PLDM_SUCCESS != *completion_code) {
322 return PLDM_SUCCESS;
323 }
324
325 if (payload_length < PLDM_GET_VERSION_RESP_BYTES) {
326 return PLDM_ERROR_INVALID_LENGTH;
327 }
328
329 struct pldm_get_version_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930330 (struct pldm_get_version_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930331
332 *next_transfer_handle = le32toh(response->next_transfer_handle);
333 *transfer_flag = response->transfer_flag;
334 memcpy(version, (uint8_t *)response->version_data, sizeof(ver32_t));
335
336 return PLDM_SUCCESS;
337}
338
339int encode_get_tid_req(uint8_t instance_id, struct pldm_msg *msg)
340{
341 if (msg == NULL) {
342 return PLDM_ERROR_INVALID_DATA;
343 }
344
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930345 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930346 header.instance = instance_id;
347 header.msg_type = PLDM_REQUEST;
348 header.command = PLDM_GET_TID;
349
350 return pack_pldm_header(&header, &(msg->hdr));
351}
352int encode_get_tid_resp(uint8_t instance_id, uint8_t completion_code,
353 uint8_t tid, struct pldm_msg *msg)
354{
355 if (msg == NULL) {
356 return PLDM_ERROR_INVALID_DATA;
357 }
358
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930359 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930360 header.instance = instance_id;
361 header.msg_type = PLDM_RESPONSE;
362 header.command = PLDM_GET_TID;
363
364 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
365 if (rc != PLDM_SUCCESS) {
366 return rc;
367 }
368
369 struct pldm_get_tid_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930370 (struct pldm_get_tid_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930371 response->completion_code = completion_code;
372 response->tid = tid;
373
374 return PLDM_SUCCESS;
375}
376
377int decode_get_tid_resp(const struct pldm_msg *msg, size_t payload_length,
378 uint8_t *completion_code, uint8_t *tid)
379{
380 if (msg == NULL || tid == NULL || completion_code == NULL) {
381 return PLDM_ERROR_INVALID_DATA;
382 }
383
384 *completion_code = msg->payload[0];
385 if (PLDM_SUCCESS != *completion_code) {
386 return PLDM_SUCCESS;
387 }
388
389 if (payload_length != PLDM_GET_TID_RESP_BYTES) {
390 return PLDM_ERROR_INVALID_LENGTH;
391 }
392
393 struct pldm_get_tid_resp *response =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930394 (struct pldm_get_tid_resp *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930395
396 *tid = response->tid;
397
398 return PLDM_SUCCESS;
399}
400
Gilbert Chen6c9c9172022-10-18 17:07:29 +0800401int encode_set_tid_req(uint8_t instance_id, uint8_t tid, struct pldm_msg *msg)
402{
403 if (msg == NULL) {
404 return PLDM_ERROR_INVALID_DATA;
405 }
406
407 if (tid == 0x0 || tid == 0xff) {
408 return PLDM_ERROR_INVALID_DATA;
409 }
410
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930411 struct pldm_header_info header = { 0 };
Gilbert Chen6c9c9172022-10-18 17:07:29 +0800412 header.instance = instance_id;
413 header.msg_type = PLDM_REQUEST;
414 header.command = PLDM_SET_TID;
415
416 uint8_t rc = pack_pldm_header(&header, &(msg->hdr));
417 if (rc != PLDM_SUCCESS) {
418 return rc;
419 }
420
421 struct pldm_set_tid_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930422 (struct pldm_set_tid_req *)msg->payload;
Gilbert Chen6c9c9172022-10-18 17:07:29 +0800423 request->tid = tid;
424
425 return PLDM_SUCCESS;
426}
427
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930428int decode_multipart_receive_req(const struct pldm_msg *msg,
429 size_t payload_length, uint8_t *pldm_type,
430 uint8_t *transfer_opflag,
431 uint32_t *transfer_ctx,
432 uint32_t *transfer_handle,
433 uint32_t *section_offset,
434 uint32_t *section_length)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930435{
436 if (msg == NULL || pldm_type == NULL || transfer_opflag == NULL ||
437 transfer_ctx == NULL || transfer_handle == NULL ||
438 section_offset == NULL || section_length == NULL) {
439 return PLDM_ERROR_INVALID_DATA;
440 }
441
442 if (payload_length != PLDM_MULTIPART_RECEIVE_REQ_BYTES) {
443 return PLDM_ERROR_INVALID_LENGTH;
444 }
445
446 struct pldm_multipart_receive_req *request =
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930447 (struct pldm_multipart_receive_req *)msg->payload;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930448
449 if (request->pldm_type != PLDM_BASE) {
450 return PLDM_ERROR_INVALID_PLDM_TYPE;
451 }
452
453 // Any enum value above PLDM_XFER_CURRENT_PART is invalid.
454 if (request->transfer_opflag > PLDM_XFER_CURRENT_PART) {
455 return PLDM_INVALID_TRANSFER_OPERATION_FLAG;
456 }
457
458 // A section offset of 0 is only valid on FIRST_PART or COMPLETE Xfers.
459 uint32_t sec_offset = le32toh(request->section_offset);
460 if (sec_offset == 0 &&
461 (request->transfer_opflag != PLDM_XFER_FIRST_PART &&
462 request->transfer_opflag != PLDM_XFER_COMPLETE)) {
463 return PLDM_ERROR_INVALID_DATA;
464 }
465
466 uint32_t handle = le32toh(request->transfer_handle);
467 if (handle == 0 && request->transfer_opflag != PLDM_XFER_COMPLETE) {
468 return PLDM_ERROR_INVALID_DATA;
469 }
470
471 *pldm_type = request->pldm_type;
472 *transfer_opflag = request->transfer_opflag;
473 *transfer_ctx = request->transfer_ctx;
474 *transfer_handle = handle;
475 *section_offset = sec_offset;
476 *section_length = le32toh(request->section_length);
477
478 return PLDM_SUCCESS;
479}
480
481int encode_cc_only_resp(uint8_t instance_id, uint8_t type, uint8_t command,
482 uint8_t cc, struct pldm_msg *msg)
483{
484 if (msg == NULL) {
485 return PLDM_ERROR_INVALID_DATA;
486 }
487
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930488 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930489 header.instance = instance_id;
490 header.msg_type = PLDM_RESPONSE;
491 header.pldm_type = type;
492 header.command = command;
493
494 uint8_t rc = pack_pldm_header(&header, &msg->hdr);
495 if (rc != PLDM_SUCCESS) {
496 return rc;
497 }
498
499 msg->payload[0] = cc;
500
501 return PLDM_SUCCESS;
502}
503
504int encode_pldm_header_only(uint8_t msg_type, uint8_t instance_id,
505 uint8_t pldm_type, uint8_t command,
506 struct pldm_msg *msg)
507{
508 if (msg == NULL) {
509 return PLDM_ERROR_INVALID_DATA;
510 }
511
Andrew Jeffery37dd6a32023-05-12 16:04:06 +0930512 struct pldm_header_info header = { 0 };
Andrew Jeffery9c766792022-08-10 23:12:49 +0930513 header.msg_type = msg_type;
514 header.instance = instance_id;
515 header.pldm_type = pldm_type;
516 header.command = command;
517 return pack_pldm_header(&header, &(msg->hdr));
518}