blob: 126642f8e253cd2717e5c22403e3ab7553a85278 [file] [log] [blame]
Sridevi Ramesh98576432019-11-27 10:10:28 -06001#include "pldm_bios_cmd.hpp"
2
John Wangb754eee2020-02-15 16:10:25 +08003#include "bios_utils.hpp"
Sridevi Ramesh98576432019-11-27 10:10:28 -06004#include "pldm_cmd_helper.hpp"
John Wangb754eee2020-02-15 16:10:25 +08005#include "utils.hpp"
6
7#include <map>
8#include <optional>
Sridevi Ramesh98576432019-11-27 10:10:28 -06009
Sridevi Rameshcdfe1142020-01-31 05:42:50 -060010#include "libpldm/bios_table.h"
Sridevi Ramesh98576432019-11-27 10:10:28 -060011#include "libpldm/utils.h"
12
13namespace pldmtool
14{
15
16namespace bios
17{
18
19namespace
20{
21
22using namespace pldmtool::helper;
John Wangb754eee2020-02-15 16:10:25 +080023using namespace pldm::bios::utils;
Sridevi Ramesh98576432019-11-27 10:10:28 -060024
25std::vector<std::unique_ptr<CommandInterface>> commands;
26
Sridevi Rameshcdfe1142020-01-31 05:42:50 -060027const std::map<const char*, pldm_bios_table_types> pldmBIOSTableTypes{
28 {"StringTable", PLDM_BIOS_STRING_TABLE},
29 {"AttributeTable", PLDM_BIOS_ATTR_TABLE},
30 {"AttributeValueTable", PLDM_BIOS_ATTR_VAL_TABLE},
31};
32
Sridevi Ramesh98576432019-11-27 10:10:28 -060033} // namespace
34
35class GetDateTime : public CommandInterface
36{
37 public:
38 ~GetDateTime() = default;
39 GetDateTime() = delete;
40 GetDateTime(const GetDateTime&) = delete;
41 GetDateTime(GetDateTime&&) = default;
42 GetDateTime& operator=(const GetDateTime&) = delete;
43 GetDateTime& operator=(GetDateTime&&) = default;
44
45 using CommandInterface::CommandInterface;
46
47 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
48 {
49 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr));
50 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
51
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -060052 auto rc = encode_get_date_time_req(instanceId, request);
Sridevi Ramesh98576432019-11-27 10:10:28 -060053 return {rc, requestMsg};
54 }
55
56 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
57 {
58 uint8_t cc = 0;
59
60 uint8_t seconds, minutes, hours, day, month;
61 uint16_t year;
62 auto rc =
63 decode_get_date_time_resp(responsePtr, payloadLength, &cc, &seconds,
64 &minutes, &hours, &day, &month, &year);
65 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
66 {
67 std::cerr << "Response Message Error: "
68 << "rc=" << rc << ",cc=" << (int)cc << std::endl;
69 return;
70 }
71 std::cout << "Date & Time : " << std::endl;
Sridevi Ramesh213a8552020-02-03 00:59:30 -060072 std::cout << "YYYY-MM-DD HH:MM:SS - ";
73 std::cout << bcd2dec16(year);
Sridevi Ramesh98576432019-11-27 10:10:28 -060074 std::cout << "-";
75 setWidth(month);
Sridevi Ramesh213a8552020-02-03 00:59:30 -060076 std::cout << "-";
77 setWidth(day);
78 std::cout << " ";
Sridevi Ramesh98576432019-11-27 10:10:28 -060079 setWidth(hours);
80 std::cout << ":";
81 setWidth(minutes);
82 std::cout << ":";
83 setWidth(seconds);
84 std::cout << std::endl;
85 }
86
87 private:
88 void setWidth(uint8_t data)
89 {
90 std::cout << std::setfill('0') << std::setw(2)
91 << static_cast<uint32_t>(bcd2dec8(data));
92 }
93};
94
George Liud6649362019-11-27 19:06:51 +080095class SetDateTime : public CommandInterface
96{
97 public:
98 ~SetDateTime() = default;
99 SetDateTime() = delete;
100 SetDateTime(const SetDateTime&) = delete;
101 SetDateTime(SetDateTime&&) = default;
102 SetDateTime& operator=(const SetDateTime&) = delete;
103 SetDateTime& operator=(SetDateTime&&) = default;
104
105 explicit SetDateTime(const char* type, const char* name, CLI::App* app) :
106 CommandInterface(type, name, app)
107 {
108 app->add_option("-d,--data", tmData,
109 "set date time data\n"
110 "eg: YYYYMMDDHHMMSS")
111 ->required();
112 }
113
114 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
115 {
116 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
117 sizeof(struct pldm_set_date_time_req));
118 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
119 uint16_t year = 0;
120 uint8_t month = 0;
121 uint8_t day = 0;
122 uint8_t hours = 0;
123 uint8_t minutes = 0;
124 uint8_t seconds = 0;
125
126 if (!uintToDate(tmData, &year, &month, &day, &hours, &minutes,
127 &seconds))
128 {
129 std::cerr << "decode date Error: "
130 << "tmData=" << tmData << std::endl;
131
132 return {PLDM_ERROR_INVALID_DATA, requestMsg};
133 }
134
135 auto rc = encode_set_date_time_req(
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -0600136 instanceId, seconds, minutes, hours, day, month, year, request,
137 sizeof(struct pldm_set_date_time_req));
George Liud6649362019-11-27 19:06:51 +0800138
139 return {rc, requestMsg};
140 }
141
142 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
143 {
144 uint8_t completionCode = 0;
145 auto rc = decode_set_date_time_resp(responsePtr, payloadLength,
146 &completionCode);
147
148 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
149 {
150 std::cerr << "Response Message Error: "
151 << "rc=" << rc << ",cc=" << (int)completionCode
152 << std::endl;
153 return;
154 }
155
156 std::cout << "SetDateTime: SUCCESS" << std::endl;
157 }
158
159 private:
160 uint64_t tmData;
161};
162
Adair Li4dd11a72020-04-24 14:52:59 +0800163class GetBIOSTableHandler : public CommandInterface
Sridevi Rameshcdfe1142020-01-31 05:42:50 -0600164{
165 public:
Adair Li4dd11a72020-04-24 14:52:59 +0800166 ~GetBIOSTableHandler() = default;
167 GetBIOSTableHandler() = delete;
168 GetBIOSTableHandler(const GetBIOSTableHandler&) = delete;
169 GetBIOSTableHandler(GetBIOSTableHandler&&) = delete;
170 GetBIOSTableHandler& operator=(const GetBIOSTableHandler&) = delete;
171 GetBIOSTableHandler& operator=(GetBIOSTableHandler&&) = delete;
Sridevi Rameshcdfe1142020-01-31 05:42:50 -0600172
John Wangb754eee2020-02-15 16:10:25 +0800173 using Table = std::vector<uint8_t>;
Sridevi Rameshcdfe1142020-01-31 05:42:50 -0600174
Adair Li4dd11a72020-04-24 14:52:59 +0800175 using CommandInterface::CommandInterface;
176
177 static inline const std::map<pldm_bios_attribute_type, const char*>
178 attrTypeMap = {
179 {PLDM_BIOS_ENUMERATION, "BIOSEnumeration"},
180 {PLDM_BIOS_ENUMERATION_READ_ONLY, "BIOSEnumerationReadOnly"},
181 {PLDM_BIOS_STRING, "BIOSString"},
182 {PLDM_BIOS_STRING_READ_ONLY, "BIOSStringReadOnly"},
183 {PLDM_BIOS_PASSWORD, "BIOSPassword"},
184 {PLDM_BIOS_PASSWORD_READ_ONLY, "BIOSPasswordReadOnly"},
185 {PLDM_BIOS_INTEGER, "BIOSInteger"},
186 {PLDM_BIOS_INTEGER_READ_ONLY, "BIOSIntegerReadOnly"},
187
188 };
Sridevi Rameshcdfe1142020-01-31 05:42:50 -0600189
190 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
191 {
John Wangb754eee2020-02-15 16:10:25 +0800192 return {PLDM_ERROR, {}};
193 }
Adair Li4dd11a72020-04-24 14:52:59 +0800194
John Wangb754eee2020-02-15 16:10:25 +0800195 void parseResponseMsg(pldm_msg*, size_t) override
196 {
197 }
Sridevi Rameshcdfe1142020-01-31 05:42:50 -0600198
John Wangb754eee2020-02-15 16:10:25 +0800199 std::optional<Table> getBIOSTable(pldm_bios_table_types tableType)
200 {
Sridevi Rameshcdfe1142020-01-31 05:42:50 -0600201 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
202 PLDM_GET_BIOS_TABLE_REQ_BYTES);
203 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
204
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -0600205 auto rc = encode_get_bios_table_req(instanceId, 0, PLDM_GET_FIRSTPART,
206 tableType, request);
John Wangb754eee2020-02-15 16:10:25 +0800207 if (rc != PLDM_SUCCESS)
208 {
209 std::cerr << "Encode GetBIOSTable Error, tableType=," << tableType
210 << " ,rc=" << rc << std::endl;
211 return std::nullopt;
212 }
213 std::vector<uint8_t> responseMsg;
214 rc = pldmSendRecv(requestMsg, responseMsg);
215 if (rc != PLDM_SUCCESS)
216 {
217 std::cerr << "PLDM: Communication Error, rc =" << rc << std::endl;
218 return std::nullopt;
219 }
Sridevi Rameshcdfe1142020-01-31 05:42:50 -0600220
Sridevi Rameshcdfe1142020-01-31 05:42:50 -0600221 uint8_t cc = 0, transferFlag = 0;
222 uint32_t nextTransferHandle = 0;
223 size_t bios_table_offset;
John Wangb754eee2020-02-15 16:10:25 +0800224 auto responsePtr =
225 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
226 auto payloadLength = responseMsg.size() - sizeof(pldm_msg_hdr);
Sridevi Rameshcdfe1142020-01-31 05:42:50 -0600227
John Wangb754eee2020-02-15 16:10:25 +0800228 rc = decode_get_bios_table_resp(responsePtr, payloadLength, &cc,
229 &nextTransferHandle, &transferFlag,
230 &bios_table_offset);
Sridevi Rameshcdfe1142020-01-31 05:42:50 -0600231
232 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
233 {
John Wangb754eee2020-02-15 16:10:25 +0800234 std::cerr << "GetBIOSTable Response Error: tableType=" << tableType
235 << ", rc=" << rc << ", cc=" << (int)cc << std::endl;
236 return std::nullopt;
Sridevi Rameshcdfe1142020-01-31 05:42:50 -0600237 }
238 auto tableData =
239 reinterpret_cast<char*>((responsePtr->payload) + bios_table_offset);
John Wangb754eee2020-02-15 16:10:25 +0800240 auto tableSize = payloadLength - sizeof(nextTransferHandle) -
241 sizeof(transferFlag) - sizeof(cc);
242 return std::make_optional<Table>(tableData, tableData + tableSize);
243 }
244
Adair Li4dd11a72020-04-24 14:52:59 +0800245 std::optional<uint16_t> findAttrHandleByName(const std::string& name,
246 const Table& attrTable,
247 const Table& stringTable)
John Wangb754eee2020-02-15 16:10:25 +0800248 {
Adair Li4dd11a72020-04-24 14:52:59 +0800249 auto stringEntry = pldm_bios_table_string_find_by_string(
250 stringTable.data(), stringTable.size(), name.c_str());
251 if (stringEntry == nullptr)
John Wangb754eee2020-02-15 16:10:25 +0800252 {
Adair Li4dd11a72020-04-24 14:52:59 +0800253 return std::nullopt;
254 }
John Wangb754eee2020-02-15 16:10:25 +0800255
Adair Li4dd11a72020-04-24 14:52:59 +0800256 auto nameHandle =
257 pldm_bios_table_string_entry_decode_handle(stringEntry);
John Wangb754eee2020-02-15 16:10:25 +0800258
Adair Li4dd11a72020-04-24 14:52:59 +0800259 for (auto attr : BIOSTableIter<PLDM_BIOS_ATTR_TABLE>(attrTable.data(),
260 attrTable.size()))
261 {
262 auto attrNameHandle =
263 pldm_bios_table_attr_entry_decode_string_handle(attr);
264 if (attrNameHandle == nameHandle)
265 {
266 return pldm_bios_table_attr_entry_decode_attribute_handle(attr);
John Wangb754eee2020-02-15 16:10:25 +0800267 }
268 }
Adair Li4dd11a72020-04-24 14:52:59 +0800269 return std::nullopt;
Sridevi Rameshcdfe1142020-01-31 05:42:50 -0600270 }
271
John Wangb754eee2020-02-15 16:10:25 +0800272 std::string decodeStringFromStringEntry(
273 const pldm_bios_string_table_entry* stringEntry)
274 {
275 auto strLength =
276 pldm_bios_table_string_entry_decode_string_length(stringEntry);
277 std::vector<char> buffer(strLength + 1 /* sizeof '\0' */);
278 pldm_bios_table_string_entry_decode_string(stringEntry, buffer.data(),
279 buffer.size());
280
281 return std::string(buffer.data(), buffer.data() + strLength);
282 }
283
284 std::string displayStringHandle(uint16_t handle,
Adair Li4dd11a72020-04-24 14:52:59 +0800285 const std::optional<Table>& stringTable,
286 bool displayHandle = true)
John Wangb754eee2020-02-15 16:10:25 +0800287 {
288 std::string displayString = std::to_string(handle);
289 if (!stringTable)
290 {
291 return displayString;
292 }
293 auto stringEntry = pldm_bios_table_string_find_by_handle(
294 stringTable->data(), stringTable->size(), handle);
295 if (stringEntry == nullptr)
296 {
297 return displayString;
298 }
299
Adair Li4dd11a72020-04-24 14:52:59 +0800300 auto decodedStr = decodeStringFromStringEntry(stringEntry);
301 if (!displayHandle)
302 {
303 return decodedStr;
304 }
305
306 return displayString + "(" + decodedStr + ")";
John Wangb754eee2020-02-15 16:10:25 +0800307 }
308
309 std::string displayEnumValueByIndex(uint16_t attrHandle, uint8_t index,
310 const std::optional<Table>& attrTable,
311 const std::optional<Table>& stringTable)
312 {
313 std::string displayString;
314 if (!attrTable)
315 {
316 return displayString;
317 }
318
319 auto attrEntry = pldm_bios_table_attr_find_by_handle(
320 attrTable->data(), attrTable->size(), attrHandle);
321 if (attrEntry == nullptr)
322 {
323 return displayString;
324 }
325 auto pvNum = pldm_bios_table_attr_entry_enum_decode_pv_num(attrEntry);
326 std::vector<uint16_t> pvHandls(pvNum);
327 pldm_bios_table_attr_entry_enum_decode_pv_hdls(
328 attrEntry, pvHandls.data(), pvHandls.size());
Adair Li4dd11a72020-04-24 14:52:59 +0800329 return displayStringHandle(pvHandls[index], stringTable, false);
John Wangb754eee2020-02-15 16:10:25 +0800330 }
331
Adair Li4dd11a72020-04-24 14:52:59 +0800332 void displayAttributeValueEntry(
333 const pldm_bios_attr_val_table_entry* tableEntry,
334 const std::optional<Table>& attrTable,
335 const std::optional<Table>& stringTable)
336 {
337 auto attrHandle =
338 pldm_bios_table_attr_value_entry_decode_attribute_handle(
339 tableEntry);
340 auto attrType = static_cast<pldm_bios_attribute_type>(
341 pldm_bios_table_attr_value_entry_decode_attribute_type(tableEntry));
342 switch (attrType)
343 {
344 case PLDM_BIOS_ENUMERATION:
345 case PLDM_BIOS_ENUMERATION_READ_ONLY:
346 {
347 auto count =
348 pldm_bios_table_attr_value_entry_enum_decode_number(
349 tableEntry);
350 std::vector<uint8_t> handles(count);
351 pldm_bios_table_attr_value_entry_enum_decode_handles(
352 tableEntry, handles.data(), handles.size());
353 for (size_t i = 0; i < handles.size(); i++)
354 {
355 std::cout << "CurrentValue: "
356 << displayEnumValueByIndex(attrHandle, handles[i],
357 attrTable, stringTable)
358 << std::endl;
359 }
360 break;
361 }
362 case PLDM_BIOS_INTEGER:
363 case PLDM_BIOS_INTEGER_READ_ONLY:
364 {
365 auto cv = pldm_bios_table_attr_value_entry_integer_decode_cv(
366 tableEntry);
367 std::cout << "CurrentValue: " << cv << std::endl;
368 break;
369 }
370 case PLDM_BIOS_STRING:
371 case PLDM_BIOS_STRING_READ_ONLY:
372 {
373 variable_field currentString;
374 pldm_bios_table_attr_value_entry_string_decode_string(
375 tableEntry, &currentString);
376 std::cout << "CurrentValue: "
377 << std::string(reinterpret_cast<const char*>(
378 currentString.ptr),
379 currentString.length)
380 << std::endl;
381
382 break;
383 }
384 case PLDM_BIOS_PASSWORD:
385 case PLDM_BIOS_PASSWORD_READ_ONLY:
386 {
387 std::cout << "Password attribute: Not Supported" << std::endl;
388 break;
389 }
390 }
391 }
392};
393
394class GetBIOSTable : public GetBIOSTableHandler
395{
396 public:
397 ~GetBIOSTable() = default;
398 GetBIOSTable() = delete;
399 GetBIOSTable(const GetBIOSTable&) = delete;
400 GetBIOSTable(GetBIOSTable&&) = default;
401 GetBIOSTable& operator=(const GetBIOSTable&) = delete;
402 GetBIOSTable& operator=(GetBIOSTable&&) = default;
403
404 using Table = std::vector<uint8_t>;
405
406 explicit GetBIOSTable(const char* type, const char* name, CLI::App* app) :
407 GetBIOSTableHandler(type, name, app)
408 {
409 app->add_option("-t,--type", pldmBIOSTableType, "pldm bios table type")
410 ->required()
411 ->transform(
412 CLI::CheckedTransformer(pldmBIOSTableTypes, CLI::ignore_case));
413 }
414
415 void exec() override
416 {
417 switch (pldmBIOSTableType)
418 {
419 case PLDM_BIOS_STRING_TABLE:
420 {
421 auto stringTable = getBIOSTable(PLDM_BIOS_STRING_TABLE);
422 decodeStringTable(stringTable);
423 break;
424 }
425 case PLDM_BIOS_ATTR_TABLE:
426 {
427 auto stringTable = getBIOSTable(PLDM_BIOS_STRING_TABLE);
428 auto attrTable = getBIOSTable(PLDM_BIOS_ATTR_TABLE);
429
430 decodeAttributeTable(attrTable, stringTable);
431 break;
432 }
433 case PLDM_BIOS_ATTR_VAL_TABLE:
434 {
435 auto stringTable = getBIOSTable(PLDM_BIOS_STRING_TABLE);
436 auto attrTable = getBIOSTable(PLDM_BIOS_ATTR_TABLE);
437 auto attrValTable = getBIOSTable(PLDM_BIOS_ATTR_VAL_TABLE);
438
439 decodeAttributeValueTable(attrValTable, attrTable, stringTable);
440 break;
441 }
442 }
443 }
444
445 private:
446 pldm_bios_table_types pldmBIOSTableType;
447
John Wangb754eee2020-02-15 16:10:25 +0800448 void decodeStringTable(const std::optional<Table>& stringTable)
Sridevi Rameshcdfe1142020-01-31 05:42:50 -0600449 {
John Wangb754eee2020-02-15 16:10:25 +0800450 if (!stringTable)
Sridevi Rameshcdfe1142020-01-31 05:42:50 -0600451 {
John Wangb754eee2020-02-15 16:10:25 +0800452 std::cerr << "GetBIOSStringTable Error" << std::endl;
Sridevi Rameshcdfe1142020-01-31 05:42:50 -0600453 return;
454 }
John Wangb754eee2020-02-15 16:10:25 +0800455 std::cout << "PLDM StringTable: " << std::endl;
456 std::cout << "BIOSStringHandle : BIOSString" << std::endl;
457
458 for (auto tableEntry : BIOSTableIter<PLDM_BIOS_STRING_TABLE>(
459 stringTable->data(), stringTable->size()))
Sridevi Rameshcdfe1142020-01-31 05:42:50 -0600460 {
John Wangb754eee2020-02-15 16:10:25 +0800461 auto strHandle =
462 pldm_bios_table_string_entry_decode_handle(tableEntry);
463 auto strTableData = decodeStringFromStringEntry(tableEntry);
464 std::cout << strHandle << " : " << strTableData << std::endl;
465 }
466 }
467 void decodeAttributeTable(const std::optional<Table>& attrTable,
468 const std::optional<Table>& stringTable)
469 {
John Wangb754eee2020-02-15 16:10:25 +0800470 if (!stringTable)
471 {
472 std::cerr << "GetBIOSAttributeTable Error" << std::endl;
473 return;
474 }
475 std::cout << "PLDM AttributeTable: " << std::endl;
476 for (auto entry : BIOSTableIter<PLDM_BIOS_ATTR_TABLE>(
477 attrTable->data(), attrTable->size()))
478 {
479 auto attrHandle =
480 pldm_bios_table_attr_entry_decode_attribute_handle(entry);
481 auto attrNameHandle =
482 pldm_bios_table_attr_entry_decode_string_handle(entry);
483 auto attrType = static_cast<pldm_bios_attribute_type>(
484 pldm_bios_table_attr_entry_decode_attribute_type(entry));
485 std::cout << "AttributeHandle: " << attrHandle
486 << ", AttributeNameHandle: "
487 << displayStringHandle(attrNameHandle, stringTable)
488 << std::endl;
489 std::cout << "\tAttributeType: " << attrTypeMap.at(attrType)
490 << std::endl;
491 switch (attrType)
492 {
493 case PLDM_BIOS_ENUMERATION:
494 case PLDM_BIOS_ENUMERATION_READ_ONLY:
495 {
496 auto pvNum =
497 pldm_bios_table_attr_entry_enum_decode_pv_num(entry);
498 std::vector<uint16_t> pvHandls(pvNum);
499 pldm_bios_table_attr_entry_enum_decode_pv_hdls(
500 entry, pvHandls.data(), pvHandls.size());
501 auto defNum =
502 pldm_bios_table_attr_entry_enum_decode_def_num(entry);
503 std::vector<uint8_t> defIndices(defNum);
504 pldm_bios_table_attr_entry_enum_decode_def_indices(
505 entry, defIndices.data(), defIndices.size());
506 std::cout << "\tNumberOfPossibleValues: " << (int)pvNum
507 << std::endl;
508
509 for (size_t i = 0; i < pvHandls.size(); i++)
510 {
511 std::cout
512 << "\t\tPossibleValueStringHandle"
513 << "[" << i << "] = "
514 << displayStringHandle(pvHandls[i], stringTable)
515 << std::endl;
516 }
517 std::cout << "\tNumberOfDefaultValues: " << (int)defNum
518 << std::endl;
519 for (size_t i = 0; i < defIndices.size(); i++)
520 {
521 std::cout << "\t\tDefaultValueStringHandleIndex"
522 << "[" << i << "] = " << (int)defIndices[i]
523 << ", StringHandle = "
524 << displayStringHandle(
525 pvHandls[defIndices[i]], stringTable)
526 << std::endl;
527 }
528 break;
529 }
530 case PLDM_BIOS_INTEGER:
531 case PLDM_BIOS_INTEGER_READ_ONLY:
532 {
533 uint64_t lower, upper, def;
534 uint32_t scalar;
535 pldm_bios_table_attr_entry_integer_decode(
536 entry, &lower, &upper, &scalar, &def);
537 std::cout << "\tLowerBound: " << lower << std::endl
538 << "\tUpperBound: " << upper << std::endl
539 << "\tScalarIncrement: " << scalar << std::endl
540 << "\tDefaultValue: " << def << std::endl;
541 break;
542 }
543 case PLDM_BIOS_STRING:
544 case PLDM_BIOS_STRING_READ_ONLY:
545 {
546 auto strType =
547 pldm_bios_table_attr_entry_string_decode_string_type(
548 entry);
549 auto min =
550 pldm_bios_table_attr_entry_string_decode_min_length(
551 entry);
552 auto max =
553 pldm_bios_table_attr_entry_string_decode_max_length(
554 entry);
555 auto def =
556 pldm_bios_table_attr_entry_string_decode_def_string_length(
557 entry);
558 std::vector<char> defString(def + 1);
559 pldm_bios_table_attr_entry_string_decode_def_string(
560 entry, defString.data(), defString.size());
561 std::cout
562 << "\tStringType: 0x" << std::hex << std::setw(2)
563 << std::setfill('0') << (int)strType << std::dec
564 << std::setw(0) << std::endl
565 << "\tMinimumStringLength: " << (int)min << std::endl
566 << "\tMaximumStringLength: " << (int)max << std::endl
567 << "\tDefaultStringLength: " << (int)def << std::endl
568 << "\tDefaultString: " << defString.data() << std::endl;
569 break;
570 }
571 case PLDM_BIOS_PASSWORD:
572 case PLDM_BIOS_PASSWORD_READ_ONLY:
573 std::cout << "Password attribute: Not Supported"
574 << std::endl;
575 }
576 }
577 }
578 void decodeAttributeValueTable(const std::optional<Table>& attrValTable,
579 const std::optional<Table>& attrTable,
580 const std::optional<Table>& stringTable)
581 {
John Wangb754eee2020-02-15 16:10:25 +0800582 if (!attrValTable)
583 {
584 std::cerr << "GetBIOSAttributeValueTable Error" << std::endl;
585 return;
586 }
587 std::cout << "PLDM AttributeValueTable: " << std::endl;
588 for (auto tableEntry : BIOSTableIter<PLDM_BIOS_ATTR_VAL_TABLE>(
589 attrValTable->data(), attrValTable->size()))
590 {
Adair Li4dd11a72020-04-24 14:52:59 +0800591 displayAttributeValueEntry(tableEntry, attrTable, stringTable);
Sridevi Rameshcdfe1142020-01-31 05:42:50 -0600592 }
593 }
594};
595
Adair Li4dd11a72020-04-24 14:52:59 +0800596class GetBIOSAttributeCurrentValueByHandle : public GetBIOSTableHandler
597{
598 public:
599 ~GetBIOSAttributeCurrentValueByHandle() = default;
600 GetBIOSAttributeCurrentValueByHandle(
601 const GetBIOSAttributeCurrentValueByHandle&) = delete;
602 GetBIOSAttributeCurrentValueByHandle(
603 GetBIOSAttributeCurrentValueByHandle&&) = delete;
604 GetBIOSAttributeCurrentValueByHandle&
605 operator=(const GetBIOSAttributeCurrentValueByHandle&) = delete;
606 GetBIOSAttributeCurrentValueByHandle&
607 operator=(GetBIOSAttributeCurrentValueByHandle&&) = delete;
608
609 explicit GetBIOSAttributeCurrentValueByHandle(const char* type,
610 const char* name,
611 CLI::App* app) :
612 GetBIOSTableHandler(type, name, app)
613 {
614 app->add_option("-a, --attribute", attrName, "pldm bios attribute name")
615 ->required();
616 }
617
618 void exec()
619 {
620 auto stringTable = getBIOSTable(PLDM_BIOS_STRING_TABLE);
621 auto attrTable = getBIOSTable(PLDM_BIOS_ATTR_TABLE);
622
623 if (!stringTable || !attrTable)
624 {
625 std::cout << "StringTable/AttrTable Unavaliable" << std::endl;
626 return;
627 }
628
629 auto handle = findAttrHandleByName(attrName, *attrTable, *stringTable);
630
631 std::vector<uint8_t> requestMsg(
632 sizeof(pldm_msg_hdr) +
633 PLDM_GET_BIOS_ATTR_CURR_VAL_BY_HANDLE_REQ_BYTES);
634 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
635
636 auto rc = encode_get_bios_attribute_current_value_by_handle_req(
637 instanceId, 0, PLDM_GET_FIRSTPART, *handle, request);
638 if (rc != PLDM_SUCCESS)
639 {
640 std::cerr << "PLDM: Request Message Error, rc =" << rc << std::endl;
641 return;
642 }
643
644 std::vector<uint8_t> responseMsg;
645 rc = pldmSendRecv(requestMsg, responseMsg);
646 if (rc != PLDM_SUCCESS)
647 {
648 std::cerr << "PLDM: Communication Error, rc =" << rc << std::endl;
649 return;
650 }
651
652 uint8_t cc = 0, transferFlag = 0;
653 uint32_t nextTransferHandle = 0;
654 struct variable_field attributeData;
655 auto responsePtr =
656 reinterpret_cast<struct pldm_msg*>(responseMsg.data());
657 auto payloadLength = responseMsg.size() - sizeof(pldm_msg_hdr);
658
659 rc = decode_get_bios_attribute_current_value_by_handle_resp(
660 responsePtr, payloadLength, &cc, &nextTransferHandle, &transferFlag,
661 &attributeData);
662 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
663 {
664 std::cerr << "Response Message Error: "
665 << "rc=" << rc << ",cc=" << (int)cc << std::endl;
666 return;
667 }
668
669 auto tableEntry =
670 reinterpret_cast<const struct pldm_bios_attr_val_table_entry*>(
671 attributeData.ptr);
672
673 displayAttributeValueEntry(tableEntry, attrTable, stringTable);
674 }
675
676 private:
677 std::string attrName;
678};
679
Sridevi Ramesh98576432019-11-27 10:10:28 -0600680void registerCommand(CLI::App& app)
681{
682 auto bios = app.add_subcommand("bios", "bios type command");
683 bios->require_subcommand(1);
684 auto getDateTime = bios->add_subcommand("GetDateTime", "get date time");
685 commands.push_back(
686 std::make_unique<GetDateTime>("bios", "GetDateTime", getDateTime));
George Liud6649362019-11-27 19:06:51 +0800687
688 auto setDateTime =
689 bios->add_subcommand("SetDateTime", "set host date time");
690 commands.push_back(
691 std::make_unique<SetDateTime>("bios", "setDateTime", setDateTime));
Sridevi Rameshcdfe1142020-01-31 05:42:50 -0600692
693 auto getBIOSTable = bios->add_subcommand("GetBIOSTable", "get bios table");
694 commands.push_back(
695 std::make_unique<GetBIOSTable>("bios", "GetBIOSTable", getBIOSTable));
Adair Li4dd11a72020-04-24 14:52:59 +0800696
697 auto getBIOSAttributeCurrentValueByHandle =
698 bios->add_subcommand("GetBIOSAttributeCurrentValueByHandle",
699 "get bios attribute current value by handle");
700 commands.push_back(std::make_unique<GetBIOSAttributeCurrentValueByHandle>(
701 "bios", "GetBIOSAttributeCurrentValueByHandle",
702 getBIOSAttributeCurrentValueByHandle));
Sridevi Ramesh98576432019-11-27 10:10:28 -0600703}
704
705} // namespace bios
706
707} // namespace pldmtool