1 #include "pldm_base_cmd.hpp"
2
3 #include "pldm_cmd_helper.hpp"
4
5 #include <libpldm/utils.h>
6
7 #ifdef OEM_IBM
8 #include <libpldm/oem/ibm/file_io.h>
9 #include <libpldm/oem/ibm/host.h>
10 #endif
11
12 #include <string>
13
14 namespace pldmtool
15 {
16
17 namespace base
18 {
19
20 namespace
21 {
22
23 using namespace pldmtool::helper;
24
25 std::vector<std::unique_ptr<CommandInterface>> commands;
26 const std::map<const char*, pldm_supported_types> pldmTypes{
27 {"base", PLDM_BASE}, {"platform", PLDM_PLATFORM},
28 {"bios", PLDM_BIOS}, {"fru", PLDM_FRU},
29 #ifdef OEM_IBM
30 {"oem-ibm", PLDM_OEM},
31 #endif
32 };
33
34 const std::map<const char*, pldm_supported_commands> pldmBaseCmds{
35 {"SetTID", PLDM_SET_TID},
36 {"GetTID", PLDM_GET_TID},
37 {"GetPLDMVersion", PLDM_GET_PLDM_VERSION},
38 {"GetPLDMTypes", PLDM_GET_PLDM_TYPES},
39 {"GetPLDMCommands", PLDM_GET_PLDM_COMMANDS},
40 {"SelectPLDMVersion", PLDM_SELECT_PLDM_VERSION},
41 {"NegotiateTransferParameters", PLDM_NEGOTIATE_TRANSFER_PARAMETERS},
42 {"MultipartSend", PLDM_MULTIPART_SEND},
43 {"MultipartReceive", PLDM_MULTIPART_RECEIVE},
44 {"GetMultipartTransferSupport", PLDM_GET_MULTIPART_TRANSFER_SUPPORT}};
45
46 const std::map<const char*, pldm_bios_commands> pldmBiosCmds{
47 {"GetBIOSTable", PLDM_GET_BIOS_TABLE},
48 {"SetBIOSTable", PLDM_SET_BIOS_TABLE},
49 {"SetBIOSAttributeCurrentValue", PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE},
50 {"GetBIOSAttributeCurrentValueByHandle",
51 PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE},
52 {"GetDateTime", PLDM_GET_DATE_TIME},
53 {"SetDateTime", PLDM_SET_DATE_TIME}};
54
55 const std::map<const char*, pldm_platform_commands> pldmPlatformCmds{
56 {"GetTerminusUID", PLDM_GET_TERMINUS_UID},
57 {"SetEventReceiver", PLDM_SET_EVENT_RECEIVER},
58 {"GetEventReceiver", PLDM_GET_EVENT_RECEIVER},
59 {"PlatformEventMessage", PLDM_PLATFORM_EVENT_MESSAGE},
60 {"PollForPlatformEventMessage", PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE},
61 {"EventMessageSupported", PLDM_EVENT_MESSAGE_SUPPORTED},
62 {"EventMessageBufferSize", PLDM_EVENT_MESSAGE_BUFFER_SIZE},
63 {"SetNumericSensorEnable", PLDM_SET_NUMERIC_SENSOR_ENABLE},
64 {"GetSensorReading", PLDM_GET_SENSOR_READING},
65 {"GetSensorThresholds", PLDM_GET_SENSOR_THRESHOLDS},
66 {"SetSensorThresholds", PLDM_SET_SENSOR_THRESHOLDS},
67 {"RestoreSensorThresholds", PLDM_RESTORE_SENSOR_THRESHOLDS},
68 {"GetSensorHysteresis", PLDM_GET_SENSOR_HYSTERESIS},
69 {"SetSensorHysteresis", PLDM_SET_SENSOR_HYSTERESIS},
70 {"InitNumericSensor", PLDM_INIT_NUMERIC_SENSOR},
71 {"SetStateSensorEnables", PLDM_SET_STATE_SENSOR_ENABLES},
72 {"GetStateSensorReadings", PLDM_GET_STATE_SENSOR_READINGS},
73 {"InitStateSensor", PLDM_INIT_STATE_SENSOR},
74 {"SetNumericEffecterEnable", PLDM_SET_NUMERIC_EFFECTER_ENABLE},
75 {"SetNumericEffecterValue", PLDM_SET_NUMERIC_EFFECTER_VALUE},
76 {"GetNumericEffecterValue", PLDM_GET_NUMERIC_EFFECTER_VALUE},
77 {"SetStateEffecterEnables", PLDM_SET_STATE_EFFECTER_ENABLES},
78 {"SetStateEffecterStates", PLDM_SET_STATE_EFFECTER_STATES},
79 {"GetStateEffecterStates", PLDM_GET_STATE_EFFECTER_STATES},
80 {"GetPLDMEventLogInfo", PLDM_GET_PLDM_EVENT_LOG_INFO},
81 {"EnablePLDMEventLogging", PLDM_ENABLE_PLDM_EVENT_LOGGING},
82 {"ClearPLDMEventLog", PLDM_CLEAR_PLDM_EVENT_LOG},
83 {"GetPLDMEventLogTimestamp", PLDM_GET_PLDM_EVENT_LOG_TIMESTAMP},
84 {"SetPLDMEventLogTimestamp", PLDM_SET_PLDM_EVENT_LOG_TIMESTAMP},
85 {"ReadPLDMEventLog", PLDM_READ_PLDM_EVENT_LOG},
86 {"GetPLDMEventLogPolicyInfo", PLDM_GET_PLDM_EVENT_LOG_POLICY_INFO},
87 {"SetPLDMEventLogPolicy", PLDM_SET_PLDM_EVENT_LOG_POLICY},
88 {"FindPLDMEventLogEntry", PLDM_FIND_PLDM_EVENT_LOG_ENTRY},
89 {"GetPDRRepositoryInfo", PLDM_GET_PDR_REPOSITORY_INFO},
90 {"GetPDR", PLDM_GET_PDR},
91 {"FindPDR", PLDM_FIND_PDR},
92 {"RunInitAgent", PLDM_RUN_INIT_AGENT},
93 {"GetPDRRepositorySignature", PLDM_GET_PDR_REPOSITORY_SIGNATURE}};
94
95 const std::map<const char*, pldm_fru_commands> pldmFruCmds{
96 {"GetFRURecordTableMetadata", PLDM_GET_FRU_RECORD_TABLE_METADATA},
97 {"GetFRURecordTable", PLDM_GET_FRU_RECORD_TABLE},
98 {"GetFRURecordByOption", PLDM_GET_FRU_RECORD_BY_OPTION}};
99
100 #ifdef OEM_IBM
101 const std::map<const char*, pldm_host_commands> pldmIBMHostCmds{
102 {"GetAlertStatus", PLDM_HOST_GET_ALERT_STATUS}};
103
104 const std::map<const char*, pldm_fileio_commands> pldmIBMFileIOCmds{
105 {"GetFileTable", PLDM_GET_FILE_TABLE},
106 {"ReadFile", PLDM_READ_FILE},
107 {"WriteFile", PLDM_WRITE_FILE},
108 {"ReadFileInToMemory", PLDM_READ_FILE_INTO_MEMORY},
109 {"WriteFileFromMemory", PLDM_WRITE_FILE_FROM_MEMORY},
110 {"ReadFileByTypeIntoMemory", PLDM_READ_FILE_BY_TYPE_INTO_MEMORY},
111 {"WriteFileByTypeFromMemory", PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY},
112 {"NewFileAvailable", PLDM_NEW_FILE_AVAILABLE},
113 {"ReadFileByType", PLDM_READ_FILE_BY_TYPE},
114 {"WriteFileByType", PLDM_WRITE_FILE_BY_TYPE},
115 {"FileAck", PLDM_FILE_ACK}};
116 #endif
117
118 } // namespace
119
120 class GetPLDMTypes : public CommandInterface
121 {
122 public:
123 ~GetPLDMTypes() = default;
124 GetPLDMTypes() = delete;
125 GetPLDMTypes(const GetPLDMTypes&) = delete;
126 GetPLDMTypes(GetPLDMTypes&&) = default;
127 GetPLDMTypes& operator=(const GetPLDMTypes&) = delete;
128 GetPLDMTypes& operator=(GetPLDMTypes&&) = delete;
129
130 using CommandInterface::CommandInterface;
131
createRequestMsg()132 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
133 {
134 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr));
135 auto request = new (requestMsg.data()) pldm_msg;
136 auto rc = encode_get_types_req(instanceId, request);
137 return {rc, requestMsg};
138 }
139
parseResponseMsg(pldm_msg * responsePtr,size_t payloadLength)140 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
141 {
142 uint8_t cc = 0;
143 std::vector<bitfield8_t> types(8);
144 auto rc = decode_get_types_resp(responsePtr, payloadLength, &cc,
145 types.data());
146 if (rc != PLDM_SUCCESS)
147 {
148 std::cerr << "Response Message Error: "
149 << "rc=" << rc << ",cc=" << (int)cc << "\n";
150 return;
151 }
152
153 ordered_json data;
154 fillCompletionCode(cc, data, PLDM_BASE);
155 if (cc == PLDM_SUCCESS)
156 {
157 printPLDMTypes(types, data);
158 }
159
160 pldmtool::helper::DisplayInJson(data);
161 }
162
163 private:
printPLDMTypes(std::vector<bitfield8_t> & types,ordered_json & data)164 void printPLDMTypes(std::vector<bitfield8_t>& types, ordered_json& data)
165 {
166 ordered_json jPldmTypes;
167 ordered_json jarray;
168 for (int i = 0; i < PLDM_MAX_TYPES; i++)
169 {
170 bitfield8_t b = types[i / 8];
171 if (b.byte & (1 << i % 8))
172 {
173 auto it = std::find_if(
174 pldmTypes.begin(), pldmTypes.end(),
175 [i](const auto& typePair) { return typePair.second == i; });
176 if (it != pldmTypes.end())
177 {
178 jarray["PLDM Type"] = it->first;
179 jarray["PLDM Type Code"] = i;
180 jPldmTypes.emplace_back(jarray);
181 }
182 }
183 }
184 data["PLDMTypes"] = jPldmTypes;
185 }
186 };
187
188 class GetPLDMVersion : public CommandInterface
189 {
190 public:
191 ~GetPLDMVersion() = default;
192 GetPLDMVersion() = delete;
193 GetPLDMVersion(const GetPLDMVersion&) = delete;
194 GetPLDMVersion(GetPLDMVersion&&) = default;
195 GetPLDMVersion& operator=(const GetPLDMVersion&) = delete;
196 GetPLDMVersion& operator=(GetPLDMVersion&&) = delete;
197
GetPLDMVersion(const char * type,const char * name,CLI::App * app)198 explicit GetPLDMVersion(const char* type, const char* name, CLI::App* app) :
199 CommandInterface(type, name, app)
200 {
201 app->add_option("-t,--type", pldmType, "pldm supported type")
202 ->required()
203 ->transform(CLI::CheckedTransformer(pldmTypes, CLI::ignore_case));
204 }
createRequestMsg()205 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
206 {
207 std::vector<uint8_t> requestMsg(
208 sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES);
209 auto request = new (requestMsg.data()) pldm_msg;
210
211 auto rc = encode_get_version_req(instanceId, 0, PLDM_GET_FIRSTPART,
212 pldmType, request);
213 return {rc, requestMsg};
214 }
215
parseResponseMsg(pldm_msg * responsePtr,size_t payloadLength)216 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
217 {
218 uint8_t cc = 0, transferFlag = 0;
219 uint32_t transferHandle = 0;
220 ver32_t version;
221 auto rc =
222 decode_get_version_resp(responsePtr, payloadLength, &cc,
223 &transferHandle, &transferFlag, &version);
224 if (rc != PLDM_SUCCESS)
225 {
226 std::cerr << "Response Message Error: "
227 << "rc=" << rc << ",cc=" << (int)cc << "\n";
228 return;
229 }
230 ordered_json data;
231 fillCompletionCode(cc, data, PLDM_BASE);
232 if (cc == PLDM_SUCCESS)
233 {
234 char buffer[16] = {0};
235 ver2str(&version, buffer, sizeof(buffer));
236 auto it = std::find_if(pldmTypes.begin(), pldmTypes.end(),
237 [&](const auto& typePair) {
238 return typePair.second == pldmType;
239 });
240
241 if (it != pldmTypes.end())
242 {
243 data["Response"] = buffer;
244 }
245 }
246
247 pldmtool::helper::DisplayInJson(data);
248 }
249
250 private:
251 pldm_supported_types pldmType;
252 };
253
254 class GetTID : public CommandInterface
255 {
256 public:
257 ~GetTID() = default;
258 GetTID() = delete;
259 GetTID(const GetTID&) = delete;
260 GetTID(GetTID&&) = default;
261 GetTID& operator=(const GetTID&) = delete;
262 GetTID& operator=(GetTID&&) = delete;
263
264 using CommandInterface::CommandInterface;
265
createRequestMsg()266 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
267 {
268 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr));
269 auto request = new (requestMsg.data()) pldm_msg;
270 auto rc = encode_get_tid_req(instanceId, request);
271 return {rc, requestMsg};
272 }
273
parseResponseMsg(pldm_msg * responsePtr,size_t payloadLength)274 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
275 {
276 uint8_t cc = 0;
277 uint8_t tid = 0;
278 std::vector<bitfield8_t> types(8);
279 auto rc = decode_get_tid_resp(responsePtr, payloadLength, &cc, &tid);
280 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
281 {
282 std::cerr << "Response Message Error: "
283 << "rc=" << rc << ",cc=" << (int)cc << "\n";
284 return;
285 }
286 ordered_json data;
287 data["Response"] = static_cast<uint32_t>(tid);
288 pldmtool::helper::DisplayInJson(data);
289 }
290 };
291
292 class GetPLDMCommands : public CommandInterface
293 {
294 public:
295 ~GetPLDMCommands() = default;
296 GetPLDMCommands() = delete;
297 GetPLDMCommands(const GetPLDMCommands&) = delete;
298 GetPLDMCommands(GetPLDMCommands&&) = default;
299 GetPLDMCommands& operator=(const GetPLDMCommands&) = delete;
300 GetPLDMCommands& operator=(GetPLDMCommands&&) = delete;
301
GetPLDMCommands(const char * type,const char * name,CLI::App * app)302 explicit GetPLDMCommands(const char* type, const char* name,
303 CLI::App* app) : CommandInterface(type, name, app)
304 {
305 app->add_option("-t,--type", pldmType, "pldm supported type")
306 ->required()
307 ->transform(CLI::CheckedTransformer(pldmTypes, CLI::ignore_case));
308 app->add_option(
309 "-d,--data", inputVersion,
310 "Set PLDM type version. Which is got from GetPLDMVersion\n"
311 "eg: version 1.1.0 then data will be `0xf1 0xf1 0xf0 0x00`");
312 }
313
createRequestMsg()314 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
315 {
316 std::vector<uint8_t> requestMsg(
317 sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES);
318 auto request = new (requestMsg.data()) pldm_msg;
319 ver32_t version{0xFF, 0xFF, 0xFF, 0xFF};
320 if (inputVersion.size() != 0)
321 {
322 if (inputVersion.size() != 4)
323 {
324 std::cerr << "Invalid version format. "
325 << "\n";
326 }
327 else
328 {
329 version.major = inputVersion[3];
330 version.minor = inputVersion[2];
331 version.update = inputVersion[1];
332 version.alpha = inputVersion[0];
333 }
334 }
335 auto rc =
336 encode_get_commands_req(instanceId, pldmType, version, request);
337 return {rc, requestMsg};
338 }
339
parseResponseMsg(pldm_msg * responsePtr,size_t payloadLength)340 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
341 {
342 uint8_t cc = 0;
343 std::vector<bitfield8_t> cmdTypes(32);
344 auto rc = decode_get_commands_resp(responsePtr, payloadLength, &cc,
345 cmdTypes.data());
346 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
347 {
348 std::cerr << "Response Message Error: "
349 << "rc=" << rc << ",cc=" << (int)cc << "\n";
350 return;
351 }
352 printPldmCommands(cmdTypes, pldmType);
353 }
354
355 private:
356 pldm_supported_types pldmType;
357 std::vector<uint8_t> inputVersion;
358
359 template <typename CommandMap>
printCommand(CommandMap & commandMap,int i,ordered_json & jarray)360 void printCommand(CommandMap& commandMap, int i, ordered_json& jarray)
361 {
362 auto it = std::find_if(
363 commandMap.begin(), commandMap.end(),
364 [i](const auto& typePair) { return typePair.second == i; });
365 if (it != commandMap.end())
366 {
367 jarray["PLDM Command Code"] = i;
368 jarray["PLDM Command"] = it->first;
369 }
370 }
371
printPldmCommands(std::vector<bitfield8_t> & cmdTypes,pldm_supported_types pldmType)372 void printPldmCommands(std::vector<bitfield8_t>& cmdTypes,
373 pldm_supported_types pldmType)
374 {
375 ordered_json output;
376 for (int i = 0; i < PLDM_MAX_CMDS_PER_TYPE; i++)
377 {
378 ordered_json cmdinfo;
379 bitfield8_t b = cmdTypes[i / 8];
380 if (b.byte & (1 << i % 8))
381 {
382 switch (pldmType)
383 {
384 case PLDM_BASE:
385 printCommand(pldmBaseCmds, i, cmdinfo);
386 break;
387 case PLDM_PLATFORM:
388 printCommand(pldmPlatformCmds, i, cmdinfo);
389 break;
390 case PLDM_BIOS:
391 printCommand(pldmBiosCmds, i, cmdinfo);
392 break;
393 case PLDM_FRU:
394 printCommand(pldmFruCmds, i, cmdinfo);
395 break;
396 case PLDM_OEM:
397 #ifdef OEM_IBM
398 printCommand(pldmIBMHostCmds, i, cmdinfo);
399 printCommand(pldmIBMFileIOCmds, i, cmdinfo);
400 #endif
401 break;
402 default:
403 break;
404 }
405 output.emplace_back(cmdinfo);
406 }
407 }
408 pldmtool::helper::DisplayInJson(output);
409 }
410 };
411
412 class SetTID : public CommandInterface
413 {
414 public:
415 ~SetTID() = default;
416 SetTID() = delete;
417 SetTID(const SetTID&) = delete;
418 SetTID(SetTID&&) = default;
419 SetTID& operator=(const SetTID&) = delete;
420 SetTID& operator=(SetTID&&) = delete;
421
SetTID(const char * type,const char * name,CLI::App * app)422 explicit SetTID(const char* type, const char* name, CLI::App* app) :
423 CommandInterface(type, name, app)
424 {
425 app->add_option("-t,--tid", tid,
426 "The TID to be set.\n"
427 "Special value: 0x00, 0xFF = reserved. \n")
428 ->required();
429 }
createRequestMsg()430 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
431 {
432 std::vector<uint8_t> requestMsg(
433 sizeof(pldm_msg_hdr) + PLDM_SET_TID_REQ_BYTES);
434 auto request = new (requestMsg.data()) pldm_msg;
435 auto rc = encode_set_tid_req(instanceId, tid, request);
436 if (rc != PLDM_SUCCESS)
437 {
438 std::cerr << "Failed to encode_set_tid_req, rc = " << rc
439 << std::endl;
440 }
441 return {rc, requestMsg};
442 }
parseResponseMsg(pldm_msg * responsePtr,size_t payloadLength)443 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
444 {
445 uint8_t completionCode = pldm_completion_codes::PLDM_SUCCESS;
446 if (payloadLength != PLDM_SET_TID_RESP_BYTES)
447 {
448 completionCode = pldm_completion_codes::PLDM_ERROR_INVALID_LENGTH;
449 }
450 else
451 {
452 completionCode = responsePtr->payload[0];
453 }
454 ordered_json data;
455 data["completionCode"] = completionCode;
456 pldmtool::helper::DisplayInJson(data);
457 }
458
459 private:
460 uint8_t tid;
461 };
462
registerCommand(CLI::App & app)463 void registerCommand(CLI::App& app)
464 {
465 auto base = app.add_subcommand("base", "base type command");
466 base->require_subcommand(1);
467
468 auto getPLDMTypes =
469 base->add_subcommand("GetPLDMTypes", "get pldm supported types");
470 commands.push_back(
471 std::make_unique<GetPLDMTypes>("base", "GetPLDMTypes", getPLDMTypes));
472
473 auto getPLDMVersion =
474 base->add_subcommand("GetPLDMVersion", "get version of a certain type");
475 commands.push_back(std::make_unique<GetPLDMVersion>(
476 "base", "GetPLDMVersion", getPLDMVersion));
477
478 auto getPLDMTID = base->add_subcommand("GetTID", "get Terminus ID (TID)");
479 commands.push_back(std::make_unique<GetTID>("base", "GetTID", getPLDMTID));
480
481 auto getPLDMCommands = base->add_subcommand(
482 "GetPLDMCommands", "get supported commands of pldm type");
483 commands.push_back(std::make_unique<GetPLDMCommands>(
484 "base", "GetPLDMCommands", getPLDMCommands));
485
486 auto setTID = base->add_subcommand(
487 "SetTID", "set the Terminus ID (TID) for a PLDM Terminus.");
488 commands.push_back(std::make_unique<SetTID>("base", "SetTID", setTID));
489 }
490
491 } // namespace base
492 } // namespace pldmtool
493