blob: 1a705ff6e0de107b687dce669e23938ee0e17ec8 [file] [log] [blame]
Sridevi Rameshd3d5fa82019-10-29 11:45:16 -05001#include "bios.h"
Xiaochao Ma39ae2a92019-11-12 20:30:34 +08002#include "utils.h"
Sampa Misra032bd502019-03-06 05:03:22 -06003#include <endian.h>
Xiaochao Ma39ae2a92019-11-12 20:30:34 +08004#include <stdbool.h>
Sampa Misra032bd502019-03-06 05:03:22 -06005#include <string.h>
6
Sampa Misra032bd502019-03-06 05:03:22 -06007int encode_get_date_time_req(uint8_t instance_id, struct pldm_msg *msg)
8{
9 struct pldm_header_info header = {0};
10
11 if (msg == NULL) {
12 return PLDM_ERROR_INVALID_DATA;
13 }
14
15 header.msg_type = PLDM_REQUEST;
16 header.instance = instance_id;
17 header.pldm_type = PLDM_BIOS;
18 header.command = PLDM_GET_DATE_TIME;
19 return pack_pldm_header(&header, &(msg->hdr));
20}
21
22int encode_get_date_time_resp(uint8_t instance_id, uint8_t completion_code,
23 uint8_t seconds, uint8_t minutes, uint8_t hours,
24 uint8_t day, uint8_t month, uint16_t year,
25 struct pldm_msg *msg)
26{
27 struct pldm_header_info header = {0};
28 int rc = PLDM_SUCCESS;
29
30 if (msg == NULL) {
31 return PLDM_ERROR_INVALID_DATA;
32 }
33
Sampa Misra032bd502019-03-06 05:03:22 -060034 header.msg_type = PLDM_RESPONSE;
35 header.instance = instance_id;
36 header.pldm_type = PLDM_BIOS;
37 header.command = PLDM_GET_DATE_TIME;
Zahed Hossain43264522019-06-04 02:21:03 -050038
Priyanga5dcd1802019-06-10 01:50:39 -050039 struct pldm_get_date_time_resp *response =
40 (struct pldm_get_date_time_resp *)msg->payload;
41
Sampa Misra032bd502019-03-06 05:03:22 -060042 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
43 return rc;
44 }
45
Priyanga5dcd1802019-06-10 01:50:39 -050046 response->completion_code = completion_code;
47 if (response->completion_code == PLDM_SUCCESS) {
48 response->completion_code = completion_code;
49 response->seconds = seconds;
50 response->minutes = minutes;
51 response->hours = hours;
52 response->day = day;
53 response->month = month;
54 response->year = htole16(year);
Zahed Hossain43264522019-06-04 02:21:03 -050055 }
Sampa Misra032bd502019-03-06 05:03:22 -060056 return PLDM_SUCCESS;
57}
58
Zahed Hossain223a73d2019-07-04 12:46:18 -050059int decode_get_date_time_resp(const struct pldm_msg *msg, size_t payload_length,
Sampa Misra032bd502019-03-06 05:03:22 -060060 uint8_t *completion_code, uint8_t *seconds,
61 uint8_t *minutes, uint8_t *hours, uint8_t *day,
62 uint8_t *month, uint16_t *year)
63{
64 if (msg == NULL || seconds == NULL || minutes == NULL ||
65 hours == NULL || day == NULL || month == NULL || year == NULL ||
66 completion_code == NULL) {
67 return PLDM_ERROR_INVALID_DATA;
68 }
69
vkaverapa6575b82019-04-03 05:33:52 -050070 if (payload_length != PLDM_GET_DATE_TIME_RESP_BYTES) {
71 return PLDM_ERROR_INVALID_LENGTH;
72 }
73
Priyanga5dcd1802019-06-10 01:50:39 -050074 struct pldm_get_date_time_resp *response =
Zahed Hossain223a73d2019-07-04 12:46:18 -050075 (struct pldm_get_date_time_resp *)msg->payload;
Priyanga5dcd1802019-06-10 01:50:39 -050076 *completion_code = response->completion_code;
77
Sampa Misra032bd502019-03-06 05:03:22 -060078 if (PLDM_SUCCESS != *completion_code) {
79 return PLDM_SUCCESS;
80 }
Priyanga5dcd1802019-06-10 01:50:39 -050081 *seconds = response->seconds;
82 *minutes = response->minutes;
83 *hours = response->hours;
84 *day = response->day;
85 *month = response->month;
86 *year = le16toh(response->year);
Sampa Misra032bd502019-03-06 05:03:22 -060087
88 return PLDM_SUCCESS;
89}
Sampa Misrab37be312019-07-03 02:26:41 -050090
Xiaochao Ma39ae2a92019-11-12 20:30:34 +080091int encode_set_date_time_req(uint8_t instance_id, uint8_t seconds,
92 uint8_t minutes, uint8_t hours, uint8_t day,
93 uint8_t month, uint16_t year, struct pldm_msg *msg,
94 size_t payload_length)
95{
96 struct pldm_header_info header = {0};
97
98 if (msg == NULL) {
99 return PLDM_ERROR_INVALID_DATA;
100 }
101 if (payload_length != sizeof(struct pldm_set_date_time_req)) {
102 return PLDM_ERROR_INVALID_LENGTH;
103 }
104
105 if (!is_time_legal(seconds, minutes, hours, day, month, year)) {
106 return PLDM_ERROR_INVALID_DATA;
107 }
108 header.instance = instance_id;
109 header.msg_type = PLDM_REQUEST;
110 header.pldm_type = PLDM_BIOS;
111 header.command = PLDM_SET_DATE_TIME;
112 pack_pldm_header(&header, &msg->hdr);
113
114 struct pldm_set_date_time_req *request =
115 (struct pldm_set_date_time_req *)msg->payload;
116 request->seconds = dec2bcd8(seconds);
117 request->minutes = dec2bcd8(minutes);
118 request->hours = dec2bcd8(hours);
119 request->day = dec2bcd8(day);
120 request->month = dec2bcd8(month);
121 request->year = htole16(dec2bcd16(year));
122
123 return PLDM_SUCCESS;
124}
125
126int decode_set_date_time_req(const struct pldm_msg *msg, size_t payload_length,
127 uint8_t *seconds, uint8_t *minutes, uint8_t *hours,
128 uint8_t *day, uint8_t *month, uint16_t *year)
129{
130 if (msg == NULL || seconds == NULL || minutes == NULL ||
131 hours == NULL || day == NULL || month == NULL || year == NULL) {
132 return PLDM_ERROR_INVALID_DATA;
133 }
134 if (payload_length != sizeof(struct pldm_set_date_time_req)) {
135 return PLDM_ERROR_INVALID_LENGTH;
136 }
137
138 const struct pldm_set_date_time_req *request =
139 (struct pldm_set_date_time_req *)msg->payload;
140
141 *seconds = bcd2dec8(request->seconds);
142 *minutes = bcd2dec8(request->minutes);
143 *hours = bcd2dec8(request->hours);
144 *day = bcd2dec8(request->day);
145 *month = bcd2dec8(request->month);
146 *year = bcd2dec16(le16toh(request->year));
147
148 if (!is_time_legal(*seconds, *minutes, *hours, *day, *month, *year)) {
149 return PLDM_ERROR_INVALID_DATA;
150 }
151
152 return PLDM_SUCCESS;
153}
154
155int encode_set_date_time_resp(uint8_t instance_id, uint8_t completion_code,
156 struct pldm_msg *msg, size_t payload_length)
157{
158 struct pldm_header_info header = {0};
159
160 if (msg == NULL) {
161 return PLDM_ERROR_INVALID_DATA;
162 }
163 if (payload_length != sizeof(struct pldm_only_cc_resp)) {
164 return PLDM_ERROR_INVALID_LENGTH;
165 }
166
167 header.instance = instance_id;
168 header.msg_type = PLDM_RESPONSE;
169 header.pldm_type = PLDM_BIOS;
170 header.command = PLDM_SET_DATE_TIME;
171 int rc = pack_pldm_header(&header, &msg->hdr);
172 if (rc != PLDM_SUCCESS) {
173 return rc;
174 }
175
176 struct pldm_only_cc_resp *response =
177 (struct pldm_only_cc_resp *)msg->payload;
178 response->completion_code = completion_code;
179
180 return PLDM_SUCCESS;
181}
182
183int decode_set_date_time_resp(const struct pldm_msg *msg, size_t payload_length,
184 uint8_t *completion_code)
185{
186 if (msg == NULL || completion_code == NULL) {
187 return PLDM_ERROR_INVALID_DATA;
188 }
189
190 if (payload_length != sizeof(struct pldm_only_cc_resp)) {
191 return PLDM_ERROR_INVALID_LENGTH;
192 }
193
194 const struct pldm_only_cc_resp *response =
195 (struct pldm_only_cc_resp *)msg->payload;
196 *completion_code = response->completion_code;
197
198 return PLDM_SUCCESS;
199}
200
Sampa Misrab37be312019-07-03 02:26:41 -0500201int encode_get_bios_table_resp(uint8_t instance_id, uint8_t completion_code,
202 uint32_t next_transfer_handle,
203 uint8_t transfer_flag, uint8_t *table_data,
204 size_t payload_length, struct pldm_msg *msg)
205{
206 struct pldm_header_info header = {0};
207 int rc = PLDM_SUCCESS;
208
209 if (msg == NULL) {
210 return PLDM_ERROR_INVALID_DATA;
211 }
212
213 struct pldm_get_bios_table_resp *response =
214 (struct pldm_get_bios_table_resp *)msg->payload;
215
216 response->completion_code = completion_code;
217 header.msg_type = PLDM_RESPONSE;
218 header.instance = instance_id;
219 header.pldm_type = PLDM_BIOS;
220 header.command = PLDM_GET_BIOS_TABLE;
221 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
222 return rc;
223 }
224
225 if (response->completion_code == PLDM_SUCCESS) {
226
227 response->next_transfer_handle = htole32(next_transfer_handle);
228 response->transfer_flag = transfer_flag;
229 if (table_data != NULL &&
230 payload_length > (sizeof(struct pldm_msg_hdr) +
231 PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES)) {
232 memcpy(response->table_data, table_data,
233 payload_length -
234 (sizeof(struct pldm_msg_hdr) +
235 PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES));
236 }
237 }
238 return PLDM_SUCCESS;
239}
240
Sridevi Rameshd3d5fa82019-10-29 11:45:16 -0500241int encode_get_bios_table_req(uint8_t instance_id, uint32_t transfer_handle,
242 uint8_t transfer_op_flag, uint8_t table_type,
243 struct pldm_msg *msg)
244{
245 struct pldm_header_info header = {0};
246
247 if (msg == NULL) {
248 return PLDM_ERROR_INVALID_DATA;
249 }
250
251 header.msg_type = PLDM_REQUEST;
252 header.instance = instance_id;
253 header.pldm_type = PLDM_BIOS;
254 header.command = PLDM_GET_BIOS_TABLE;
255 pack_pldm_header(&header, &(msg->hdr));
256
257 struct pldm_get_bios_table_req *request =
258 (struct pldm_get_bios_table_req *)msg->payload;
259
260 request->transfer_handle = htole32(transfer_handle);
261 request->transfer_op_flag = transfer_op_flag;
262 request->table_type = table_type;
263 return PLDM_SUCCESS;
264}
265
Sampa Misrab37be312019-07-03 02:26:41 -0500266int decode_get_bios_table_req(const struct pldm_msg *msg, size_t payload_length,
267 uint32_t *transfer_handle,
268 uint8_t *transfer_op_flag, uint8_t *table_type)
269{
270 if (msg == NULL || transfer_op_flag == NULL || table_type == NULL ||
271 transfer_handle == NULL) {
272 return PLDM_ERROR_INVALID_DATA;
273 }
274
275 if (payload_length != PLDM_GET_BIOS_TABLE_REQ_BYTES) {
276 return PLDM_ERROR_INVALID_LENGTH;
277 }
278
279 struct pldm_get_bios_table_req *request =
280 (struct pldm_get_bios_table_req *)msg->payload;
281 *transfer_handle = le32toh(request->transfer_handle);
282 *transfer_op_flag = request->transfer_op_flag;
283 *table_type = request->table_type;
284
285 return PLDM_SUCCESS;
286}
Zahed Hossaind69af0b2019-07-30 01:33:31 -0500287
Sridevi Ramesh08efc7b2019-12-05 05:39:46 -0600288int decode_get_bios_table_resp(const struct pldm_msg *msg,
289 size_t payload_length, uint8_t *completion_code,
290 uint32_t *next_transfer_handle,
291 uint8_t *transfer_flag,
292 size_t *bios_table_offset)
293
294{
295 if (msg == NULL || transfer_flag == NULL ||
296 next_transfer_handle == NULL || completion_code == NULL) {
297 return PLDM_ERROR_INVALID_DATA;
298 }
299 if (payload_length <= PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES) {
300 return PLDM_ERROR_INVALID_LENGTH;
301 }
302
303 struct pldm_get_bios_table_resp *response =
304 (struct pldm_get_bios_table_resp *)msg->payload;
305
306 *completion_code = response->completion_code;
307
308 if (PLDM_SUCCESS != *completion_code) {
309 return PLDM_SUCCESS;
310 }
311
312 *next_transfer_handle = le32toh(response->next_transfer_handle);
313 *transfer_flag = response->transfer_flag;
314
315 *bios_table_offset = sizeof(*completion_code) +
316 sizeof(*next_transfer_handle) +
317 sizeof(*transfer_flag);
318
319 return PLDM_SUCCESS;
320}
321
Zahed Hossaind69af0b2019-07-30 01:33:31 -0500322int decode_get_bios_attribute_current_value_by_handle_req(
323 const struct pldm_msg *msg, size_t payload_length,
324 uint32_t *transfer_handle, uint8_t *transfer_op_flag,
325 uint16_t *attribute_handle)
326{
327 if (msg == NULL || transfer_handle == NULL ||
328 transfer_op_flag == NULL || attribute_handle == NULL) {
329 return PLDM_ERROR_INVALID_DATA;
330 }
331
332 if (payload_length != PLDM_GET_BIOS_ATTR_CURR_VAL_BY_HANDLE_REQ_BYTES) {
333 return PLDM_ERROR_INVALID_LENGTH;
334 }
335
336 struct pldm_get_bios_attribute_current_value_by_handle_req *request =
337 (struct pldm_get_bios_attribute_current_value_by_handle_req *)
338 msg->payload;
339 *transfer_handle = le32toh(request->transfer_handle);
340 *transfer_op_flag = request->transfer_op_flag;
341 *attribute_handle = le16toh(request->attribute_handle);
342
343 return PLDM_SUCCESS;
344}
345
346int encode_get_bios_current_value_by_handle_resp(
347 uint8_t instance_id, uint8_t completion_code, uint32_t next_transfer_handle,
348 uint8_t transfer_flag, const uint8_t *attribute_data,
349 size_t attribute_length, struct pldm_msg *msg)
350{
351 struct pldm_header_info header = {0};
352 int rc = PLDM_SUCCESS;
353
354 if (msg == NULL || attribute_data == NULL) {
355 return PLDM_ERROR_INVALID_DATA;
356 }
357
358 struct pldm_get_bios_attribute_current_value_by_handle_resp *response =
359 (struct pldm_get_bios_attribute_current_value_by_handle_resp *)
360 msg->payload;
361
362 response->completion_code = completion_code;
363 header.msg_type = PLDM_RESPONSE;
364 header.instance = instance_id;
365 header.pldm_type = PLDM_BIOS;
366 header.command = PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE;
367 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
368 return rc;
369 }
370 if (response->completion_code == PLDM_SUCCESS) {
371
372 response->next_transfer_handle = htole32(next_transfer_handle);
373 response->transfer_flag = transfer_flag;
374 if (attribute_data != NULL) {
375 memcpy(response->attribute_data, attribute_data,
376 attribute_length);
377 }
378 }
379 return PLDM_SUCCESS;
380}
John Wang4d844792019-08-15 15:51:40 +0800381int encode_set_bios_attribute_current_value_req(
382 uint8_t instance_id, uint32_t transfer_handle, uint8_t transfer_flag,
383 const uint8_t *attribute_data, size_t attribute_length,
384 struct pldm_msg *msg, size_t payload_lenth)
385{
386 if (msg == NULL || attribute_data == NULL) {
387 return PLDM_ERROR_INVALID_DATA;
388 }
389 if (PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES + attribute_length !=
390 payload_lenth) {
391 return PLDM_ERROR_INVALID_LENGTH;
392 }
393 struct pldm_header_info header = {0};
394 header.instance = instance_id;
395 header.msg_type = PLDM_REQUEST;
396 header.pldm_type = PLDM_BIOS;
397 header.command = PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE;
398 pack_pldm_header(&header, &msg->hdr);
399
400 struct pldm_set_bios_attribute_current_value_req *request =
401 (struct pldm_set_bios_attribute_current_value_req *)msg->payload;
402 request->transfer_handle = htole32(transfer_handle);
403 request->transfer_flag = transfer_flag;
404 memcpy(request->attribute_data, attribute_data, attribute_length);
405
406 return PLDM_SUCCESS;
407}
408
409int decode_set_bios_attribute_current_value_resp(const struct pldm_msg *msg,
410 size_t payload_length,
411 uint8_t *completion_code,
412 uint32_t *next_transfer_handle)
413{
414 if (msg == NULL || completion_code == NULL ||
415 next_transfer_handle == NULL) {
416 return PLDM_ERROR_INVALID_DATA;
417 }
418 if (payload_length != PLDM_SET_BIOS_ATTR_CURR_VAL_RESP_BYTES) {
419 return PLDM_ERROR_INVALID_LENGTH;
420 }
421
422 struct pldm_set_bios_attribute_current_value_resp *response =
423 (struct pldm_set_bios_attribute_current_value_resp *)msg->payload;
424
425 *completion_code = response->completion_code;
426 if (PLDM_SUCCESS != *completion_code) {
427 return PLDM_SUCCESS;
428 }
429 *next_transfer_handle = le32toh(response->next_transfer_handle);
430
431 return PLDM_SUCCESS;
432}
433
434int decode_set_bios_attribute_current_value_req(const struct pldm_msg *msg,
435 size_t payload_length,
436 uint32_t *transfer_handle,
437 uint8_t *transfer_flag,
438 uint8_t *attribute_data,
439 size_t *attribute_length)
440{
441 if (msg == NULL || transfer_handle == NULL || transfer_flag == NULL ||
442 attribute_data == NULL || attribute_length == NULL) {
443 return PLDM_ERROR_INVALID_DATA;
444 }
445 if (payload_length < PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES) {
446 return PLDM_ERROR_INVALID_LENGTH;
447 }
448
449 struct pldm_set_bios_attribute_current_value_req *request =
450 (struct pldm_set_bios_attribute_current_value_req *)msg->payload;
451 *transfer_handle = le32toh(request->transfer_handle);
452 *transfer_flag = request->transfer_flag;
453 *attribute_length =
454 payload_length - PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES;
455 memcpy(attribute_data, request->attribute_data, *attribute_length);
456
457 return PLDM_SUCCESS;
458}
459
460int encode_set_bios_attribute_current_value_resp(uint8_t instance_id,
461 uint8_t completion_code,
462 uint32_t next_transfer_handle,
463 struct pldm_msg *msg)
464{
465 if (msg == NULL) {
466 return PLDM_ERROR_INVALID_DATA;
467 }
468 struct pldm_header_info header = {0};
469 header.instance = instance_id;
470 header.msg_type = PLDM_RESPONSE;
471 header.pldm_type = PLDM_BIOS;
472 header.command = PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE;
473
474 int rc = pack_pldm_header(&header, &msg->hdr);
475 if (rc != PLDM_SUCCESS) {
476 return rc;
477 }
478
479 struct pldm_set_bios_attribute_current_value_resp *response =
480 (struct pldm_set_bios_attribute_current_value_resp *)msg->payload;
481 response->completion_code = completion_code;
482 response->next_transfer_handle = htole32(next_transfer_handle);
483
484 return PLDM_SUCCESS;
485}