blob: d5ba736aac86ae0671787ae75f087f47b4b5cdd0 [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
288int decode_get_bios_attribute_current_value_by_handle_req(
289 const struct pldm_msg *msg, size_t payload_length,
290 uint32_t *transfer_handle, uint8_t *transfer_op_flag,
291 uint16_t *attribute_handle)
292{
293 if (msg == NULL || transfer_handle == NULL ||
294 transfer_op_flag == NULL || attribute_handle == NULL) {
295 return PLDM_ERROR_INVALID_DATA;
296 }
297
298 if (payload_length != PLDM_GET_BIOS_ATTR_CURR_VAL_BY_HANDLE_REQ_BYTES) {
299 return PLDM_ERROR_INVALID_LENGTH;
300 }
301
302 struct pldm_get_bios_attribute_current_value_by_handle_req *request =
303 (struct pldm_get_bios_attribute_current_value_by_handle_req *)
304 msg->payload;
305 *transfer_handle = le32toh(request->transfer_handle);
306 *transfer_op_flag = request->transfer_op_flag;
307 *attribute_handle = le16toh(request->attribute_handle);
308
309 return PLDM_SUCCESS;
310}
311
312int encode_get_bios_current_value_by_handle_resp(
313 uint8_t instance_id, uint8_t completion_code, uint32_t next_transfer_handle,
314 uint8_t transfer_flag, const uint8_t *attribute_data,
315 size_t attribute_length, struct pldm_msg *msg)
316{
317 struct pldm_header_info header = {0};
318 int rc = PLDM_SUCCESS;
319
320 if (msg == NULL || attribute_data == NULL) {
321 return PLDM_ERROR_INVALID_DATA;
322 }
323
324 struct pldm_get_bios_attribute_current_value_by_handle_resp *response =
325 (struct pldm_get_bios_attribute_current_value_by_handle_resp *)
326 msg->payload;
327
328 response->completion_code = completion_code;
329 header.msg_type = PLDM_RESPONSE;
330 header.instance = instance_id;
331 header.pldm_type = PLDM_BIOS;
332 header.command = PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE;
333 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
334 return rc;
335 }
336 if (response->completion_code == PLDM_SUCCESS) {
337
338 response->next_transfer_handle = htole32(next_transfer_handle);
339 response->transfer_flag = transfer_flag;
340 if (attribute_data != NULL) {
341 memcpy(response->attribute_data, attribute_data,
342 attribute_length);
343 }
344 }
345 return PLDM_SUCCESS;
346}
John Wang4d844792019-08-15 15:51:40 +0800347int encode_set_bios_attribute_current_value_req(
348 uint8_t instance_id, uint32_t transfer_handle, uint8_t transfer_flag,
349 const uint8_t *attribute_data, size_t attribute_length,
350 struct pldm_msg *msg, size_t payload_lenth)
351{
352 if (msg == NULL || attribute_data == NULL) {
353 return PLDM_ERROR_INVALID_DATA;
354 }
355 if (PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES + attribute_length !=
356 payload_lenth) {
357 return PLDM_ERROR_INVALID_LENGTH;
358 }
359 struct pldm_header_info header = {0};
360 header.instance = instance_id;
361 header.msg_type = PLDM_REQUEST;
362 header.pldm_type = PLDM_BIOS;
363 header.command = PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE;
364 pack_pldm_header(&header, &msg->hdr);
365
366 struct pldm_set_bios_attribute_current_value_req *request =
367 (struct pldm_set_bios_attribute_current_value_req *)msg->payload;
368 request->transfer_handle = htole32(transfer_handle);
369 request->transfer_flag = transfer_flag;
370 memcpy(request->attribute_data, attribute_data, attribute_length);
371
372 return PLDM_SUCCESS;
373}
374
375int decode_set_bios_attribute_current_value_resp(const struct pldm_msg *msg,
376 size_t payload_length,
377 uint8_t *completion_code,
378 uint32_t *next_transfer_handle)
379{
380 if (msg == NULL || completion_code == NULL ||
381 next_transfer_handle == NULL) {
382 return PLDM_ERROR_INVALID_DATA;
383 }
384 if (payload_length != PLDM_SET_BIOS_ATTR_CURR_VAL_RESP_BYTES) {
385 return PLDM_ERROR_INVALID_LENGTH;
386 }
387
388 struct pldm_set_bios_attribute_current_value_resp *response =
389 (struct pldm_set_bios_attribute_current_value_resp *)msg->payload;
390
391 *completion_code = response->completion_code;
392 if (PLDM_SUCCESS != *completion_code) {
393 return PLDM_SUCCESS;
394 }
395 *next_transfer_handle = le32toh(response->next_transfer_handle);
396
397 return PLDM_SUCCESS;
398}
399
400int decode_set_bios_attribute_current_value_req(const struct pldm_msg *msg,
401 size_t payload_length,
402 uint32_t *transfer_handle,
403 uint8_t *transfer_flag,
404 uint8_t *attribute_data,
405 size_t *attribute_length)
406{
407 if (msg == NULL || transfer_handle == NULL || transfer_flag == NULL ||
408 attribute_data == NULL || attribute_length == NULL) {
409 return PLDM_ERROR_INVALID_DATA;
410 }
411 if (payload_length < PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES) {
412 return PLDM_ERROR_INVALID_LENGTH;
413 }
414
415 struct pldm_set_bios_attribute_current_value_req *request =
416 (struct pldm_set_bios_attribute_current_value_req *)msg->payload;
417 *transfer_handle = le32toh(request->transfer_handle);
418 *transfer_flag = request->transfer_flag;
419 *attribute_length =
420 payload_length - PLDM_SET_BIOS_ATTR_CURR_VAL_MIN_REQ_BYTES;
421 memcpy(attribute_data, request->attribute_data, *attribute_length);
422
423 return PLDM_SUCCESS;
424}
425
426int encode_set_bios_attribute_current_value_resp(uint8_t instance_id,
427 uint8_t completion_code,
428 uint32_t next_transfer_handle,
429 struct pldm_msg *msg)
430{
431 if (msg == NULL) {
432 return PLDM_ERROR_INVALID_DATA;
433 }
434 struct pldm_header_info header = {0};
435 header.instance = instance_id;
436 header.msg_type = PLDM_RESPONSE;
437 header.pldm_type = PLDM_BIOS;
438 header.command = PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE;
439
440 int rc = pack_pldm_header(&header, &msg->hdr);
441 if (rc != PLDM_SUCCESS) {
442 return rc;
443 }
444
445 struct pldm_set_bios_attribute_current_value_resp *response =
446 (struct pldm_set_bios_attribute_current_value_resp *)msg->payload;
447 response->completion_code = completion_code;
448 response->next_transfer_handle = htole32(next_transfer_handle);
449
450 return PLDM_SUCCESS;
451}