blob: 87152cd6e5617d386c25ffc5c3bdfb19f0df4759 [file] [log] [blame]
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -05001#include "pldm_base_cmd.hpp"
2
George Liu6492f522020-06-16 10:34:05 +08003#include "pldm_cmd_helper.hpp"
4
George Liuc453e162022-12-21 17:16:23 +08005#include <libpldm/utils.h>
6
Sridevi Ramesh45e52542020-04-03 07:54:18 -05007#ifdef OEM_IBM
Andrew Jeffery21f128d2024-01-15 15:34:26 +10308#include <libpldm/oem/ibm/file_io.h>
9#include <libpldm/oem/ibm/host.h>
Sridevi Ramesh45e52542020-04-03 07:54:18 -050010#endif
11
Sridevi Rameshca4a8152020-08-11 09:26:19 -050012#include <string>
13
John Wang58a0e062019-11-08 15:38:15 +080014namespace pldmtool
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050015{
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050016
John Wang58a0e062019-11-08 15:38:15 +080017namespace base
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050018{
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -050019
John Wang58a0e062019-11-08 15:38:15 +080020namespace
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -050021{
John Wang58a0e062019-11-08 15:38:15 +080022
23using namespace pldmtool::helper;
24
25std::vector<std::unique_ptr<CommandInterface>> commands;
26const std::map<const char*, pldm_supported_types> pldmTypes{
Sridevi Ramesh45e52542020-04-03 07:54:18 -050027 {"base", PLDM_BASE}, {"platform", PLDM_PLATFORM},
28 {"bios", PLDM_BIOS}, {"fru", PLDM_FRU},
29#ifdef OEM_IBM
30 {"oem-ibm", PLDM_OEM},
31#endif
John Wang58a0e062019-11-08 15:38:15 +080032};
33
Sridevi Ramesh66294d32020-02-21 03:52:07 -060034const std::map<const char*, pldm_supported_commands> pldmBaseCmds{
35 {"GetTID", PLDM_GET_TID},
36 {"GetPLDMVersion", PLDM_GET_PLDM_VERSION},
37 {"GetPLDMTypes", PLDM_GET_PLDM_TYPES},
38 {"GetPLDMCommands", PLDM_GET_PLDM_COMMANDS}};
39
40const std::map<const char*, pldm_bios_commands> pldmBiosCmds{
41 {"GetBIOSTable", PLDM_GET_BIOS_TABLE},
Sridevi Ramesh8fb11c92021-06-08 08:45:01 -050042 {"SetBIOSTable", PLDM_SET_BIOS_TABLE},
Sridevi Ramesh66294d32020-02-21 03:52:07 -060043 {"SetBIOSAttributeCurrentValue", PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE},
44 {"GetBIOSAttributeCurrentValueByHandle",
45 PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE},
46 {"GetDateTime", PLDM_GET_DATE_TIME},
47 {"SetDateTime", PLDM_SET_DATE_TIME}};
48
49const std::map<const char*, pldm_platform_commands> pldmPlatformCmds{
50 {"SetNumericEffecterValue", PLDM_SET_NUMERIC_EFFECTER_VALUE},
51 {"SetStateEffecterStates", PLDM_SET_STATE_EFFECTER_STATES},
Sridevi Ramesh8fb11c92021-06-08 08:45:01 -050052 {"GetPDR", PLDM_GET_PDR},
53 {"GetNumericEffecterValue", PLDM_GET_NUMERIC_EFFECTER_VALUE},
54 {"SetEventReceiver", PLDM_SET_EVENT_RECEIVER},
55 {"GetSensorReading", PLDM_GET_SENSOR_READING},
56 {"GetStateSensorReadings", PLDM_GET_STATE_SENSOR_READINGS},
57 {"PlatformEventMessage", PLDM_PLATFORM_EVENT_MESSAGE}};
Sridevi Ramesh66294d32020-02-21 03:52:07 -060058
59const std::map<const char*, pldm_fru_commands> pldmFruCmds{
60 {"GetFRURecordTableMetadata", PLDM_GET_FRU_RECORD_TABLE_METADATA},
John Wang5bdde3a2020-06-16 15:02:33 +080061 {"GetFRURecordTable", PLDM_GET_FRU_RECORD_TABLE},
62 {"GetFRURecordByOption", PLDM_GET_FRU_RECORD_BY_OPTION}};
Sridevi Ramesh66294d32020-02-21 03:52:07 -060063
Sridevi Ramesh45e52542020-04-03 07:54:18 -050064#ifdef OEM_IBM
65const std::map<const char*, pldm_host_commands> pldmIBMHostCmds{
66 {"GetAlertStatus", PLDM_HOST_GET_ALERT_STATUS}};
67
68const std::map<const char*, pldm_fileio_commands> pldmIBMFileIOCmds{
69 {"GetFileTable", PLDM_GET_FILE_TABLE},
70 {"ReadFile", PLDM_READ_FILE},
71 {"WriteFile", PLDM_WRITE_FILE},
72 {"ReadFileInToMemory", PLDM_READ_FILE_INTO_MEMORY},
73 {"WriteFileFromMemory", PLDM_WRITE_FILE_FROM_MEMORY},
74 {"ReadFileByTypeIntoMemory", PLDM_READ_FILE_BY_TYPE_INTO_MEMORY},
75 {"WriteFileByTypeFromMemory", PLDM_WRITE_FILE_BY_TYPE_FROM_MEMORY},
76 {"NewFileAvailable", PLDM_NEW_FILE_AVAILABLE},
77 {"ReadFileByType", PLDM_READ_FILE_BY_TYPE},
78 {"WriteFileByType", PLDM_WRITE_FILE_BY_TYPE},
79 {"FileAck", PLDM_FILE_ACK}};
80#endif
81
John Wang58a0e062019-11-08 15:38:15 +080082} // namespace
83
84class GetPLDMTypes : public CommandInterface
85{
86 public:
87 ~GetPLDMTypes() = default;
88 GetPLDMTypes() = delete;
89 GetPLDMTypes(const GetPLDMTypes&) = delete;
90 GetPLDMTypes(GetPLDMTypes&&) = default;
91 GetPLDMTypes& operator=(const GetPLDMTypes&) = delete;
Pavithra Barithayaa7dbca52023-07-07 04:19:37 -050092 GetPLDMTypes& operator=(GetPLDMTypes&&) = delete;
John Wang58a0e062019-11-08 15:38:15 +080093
94 using CommandInterface::CommandInterface;
95
96 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -050097 {
John Wang58a0e062019-11-08 15:38:15 +080098 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr));
99 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -0600100 auto rc = encode_get_types_req(instanceId, request);
John Wang58a0e062019-11-08 15:38:15 +0800101 return {rc, requestMsg};
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -0500102 }
103
John Wang58a0e062019-11-08 15:38:15 +0800104 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -0500105 {
John Wang58a0e062019-11-08 15:38:15 +0800106 uint8_t cc = 0;
107 std::vector<bitfield8_t> types(8);
108 auto rc = decode_get_types_resp(responsePtr, payloadLength, &cc,
109 types.data());
110 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
111 {
112 std::cerr << "Response Message Error: "
Sampa Misraaa8ae722019-12-12 03:20:40 -0600113 << "rc=" << rc << ",cc=" << (int)cc << "\n";
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -0500114 return;
John Wang58a0e062019-11-08 15:38:15 +0800115 }
116
117 printPldmTypes(types);
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -0500118 }
119
John Wang58a0e062019-11-08 15:38:15 +0800120 private:
121 void printPldmTypes(std::vector<bitfield8_t>& types)
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -0500122 {
Sridevi Rameshca4a8152020-08-11 09:26:19 -0500123 ordered_json data;
124 ordered_json jarray;
John Wang58a0e062019-11-08 15:38:15 +0800125 for (int i = 0; i < PLDM_MAX_TYPES; i++)
126 {
127 bitfield8_t b = types[i / 8];
128 if (b.byte & (1 << i % 8))
129 {
Patrick Williamsa6756622023-10-20 11:19:15 -0500130 auto it = std::find_if(
131 pldmTypes.begin(), pldmTypes.end(),
132 [i](const auto& typePair) { return typePair.second == i; });
John Wang58a0e062019-11-08 15:38:15 +0800133 if (it != pldmTypes.end())
134 {
Sridevi Rameshca4a8152020-08-11 09:26:19 -0500135 jarray["PLDM Type"] = it->first;
136 jarray["PLDM Type Code"] = i;
137 data.emplace_back(jarray);
John Wang58a0e062019-11-08 15:38:15 +0800138 }
139 }
140 }
Sridevi Rameshca4a8152020-08-11 09:26:19 -0500141 pldmtool::helper::DisplayInJson(data);
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -0500142 }
John Wang58a0e062019-11-08 15:38:15 +0800143};
144
145class GetPLDMVersion : public CommandInterface
146{
147 public:
148 ~GetPLDMVersion() = default;
149 GetPLDMVersion() = delete;
150 GetPLDMVersion(const GetPLDMVersion&) = delete;
151 GetPLDMVersion(GetPLDMVersion&&) = default;
152 GetPLDMVersion& operator=(const GetPLDMVersion&) = delete;
Pavithra Barithayaa7dbca52023-07-07 04:19:37 -0500153 GetPLDMVersion& operator=(GetPLDMVersion&&) = delete;
John Wang58a0e062019-11-08 15:38:15 +0800154
155 explicit GetPLDMVersion(const char* type, const char* name, CLI::App* app) :
156 CommandInterface(type, name, app)
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -0500157 {
John Wang58a0e062019-11-08 15:38:15 +0800158 app->add_option("-t,--type", pldmType, "pldm supported type")
159 ->required()
160 ->transform(CLI::CheckedTransformer(pldmTypes, CLI::ignore_case));
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -0500161 }
John Wang58a0e062019-11-08 15:38:15 +0800162 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
163 {
164 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
165 PLDM_GET_VERSION_REQ_BYTES);
166 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
167
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -0600168 auto rc = encode_get_version_req(instanceId, 0, PLDM_GET_FIRSTPART,
169 pldmType, request);
John Wang58a0e062019-11-08 15:38:15 +0800170 return {rc, requestMsg};
171 }
172
173 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
174 {
175 uint8_t cc = 0, transferFlag = 0;
176 uint32_t transferHandle = 0;
177 ver32_t version;
Patrick Williams6da4f912023-05-10 07:50:53 -0500178 auto rc = decode_get_version_resp(responsePtr, payloadLength, &cc,
179 &transferHandle, &transferFlag,
180 &version);
John Wang58a0e062019-11-08 15:38:15 +0800181 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
182 {
183 std::cerr << "Response Message Error: "
Sampa Misraaa8ae722019-12-12 03:20:40 -0600184 << "rc=" << rc << ",cc=" << (int)cc << "\n";
John Wang58a0e062019-11-08 15:38:15 +0800185 return;
186 }
187 char buffer[16] = {0};
188 ver2str(&version, buffer, sizeof(buffer));
Sridevi Rameshca4a8152020-08-11 09:26:19 -0500189 ordered_json data;
Patrick Williamsa6756622023-10-20 11:19:15 -0500190 auto it = std::find_if(
191 pldmTypes.begin(), pldmTypes.end(),
192 [&](const auto& typePair) { return typePair.second == pldmType; });
John Wang58a0e062019-11-08 15:38:15 +0800193
194 if (it != pldmTypes.end())
195 {
Sridevi Rameshca4a8152020-08-11 09:26:19 -0500196 data["Response"] = buffer;
John Wang58a0e062019-11-08 15:38:15 +0800197 }
Sridevi Rameshca4a8152020-08-11 09:26:19 -0500198 pldmtool::helper::DisplayInJson(data);
John Wang58a0e062019-11-08 15:38:15 +0800199 }
200
201 private:
202 pldm_supported_types pldmType;
203};
204
Sridevi Rameshadd56f02020-01-20 23:44:22 -0600205class GetTID : public CommandInterface
206{
207 public:
208 ~GetTID() = default;
209 GetTID() = delete;
210 GetTID(const GetTID&) = delete;
211 GetTID(GetTID&&) = default;
212 GetTID& operator=(const GetTID&) = delete;
Pavithra Barithayaa7dbca52023-07-07 04:19:37 -0500213 GetTID& operator=(GetTID&&) = delete;
Sridevi Rameshadd56f02020-01-20 23:44:22 -0600214
215 using CommandInterface::CommandInterface;
216
217 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
218 {
219 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr));
220 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -0600221 auto rc = encode_get_tid_req(instanceId, request);
Sridevi Rameshadd56f02020-01-20 23:44:22 -0600222 return {rc, requestMsg};
223 }
224
225 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
226 {
227 uint8_t cc = 0;
228 uint8_t tid = 0;
229 std::vector<bitfield8_t> types(8);
230 auto rc = decode_get_tid_resp(responsePtr, payloadLength, &cc, &tid);
231 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
232 {
233 std::cerr << "Response Message Error: "
234 << "rc=" << rc << ",cc=" << (int)cc << "\n";
235 return;
236 }
Sridevi Rameshca4a8152020-08-11 09:26:19 -0500237 ordered_json data;
238 data["Response"] = static_cast<uint32_t>(tid);
239 pldmtool::helper::DisplayInJson(data);
Sridevi Rameshadd56f02020-01-20 23:44:22 -0600240 }
241};
242
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600243class GetPLDMCommands : public CommandInterface
244{
245 public:
246 ~GetPLDMCommands() = default;
247 GetPLDMCommands() = delete;
248 GetPLDMCommands(const GetPLDMCommands&) = delete;
249 GetPLDMCommands(GetPLDMCommands&&) = default;
250 GetPLDMCommands& operator=(const GetPLDMCommands&) = delete;
Pavithra Barithayaa7dbca52023-07-07 04:19:37 -0500251 GetPLDMCommands& operator=(GetPLDMCommands&&) = delete;
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600252
253 explicit GetPLDMCommands(const char* type, const char* name,
254 CLI::App* app) :
255 CommandInterface(type, name, app)
256 {
257 app->add_option("-t,--type", pldmType, "pldm supported type")
258 ->required()
259 ->transform(CLI::CheckedTransformer(pldmTypes, CLI::ignore_case));
260 }
261
262 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
263 {
264 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
265 PLDM_GET_COMMANDS_REQ_BYTES);
266 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
267 ver32_t version{0xFF, 0xFF, 0xFF, 0xFF};
Patrick Williams6da4f912023-05-10 07:50:53 -0500268 auto rc = encode_get_commands_req(instanceId, pldmType, version,
269 request);
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600270 return {rc, requestMsg};
271 }
272
273 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
274 {
275 uint8_t cc = 0;
276 std::vector<bitfield8_t> cmdTypes(32);
277 auto rc = decode_get_commands_resp(responsePtr, payloadLength, &cc,
278 cmdTypes.data());
279 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
280 {
281 std::cerr << "Response Message Error: "
282 << "rc=" << rc << ",cc=" << (int)cc << "\n";
283 return;
284 }
285 printPldmCommands(cmdTypes, pldmType);
286 }
287
288 private:
289 pldm_supported_types pldmType;
290
291 template <typename CommandMap>
Sridevi Rameshca4a8152020-08-11 09:26:19 -0500292 void printCommand(CommandMap& commandMap, int i, ordered_json& jarray)
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600293 {
Patrick Williamsa6756622023-10-20 11:19:15 -0500294 auto it = std::find_if(
295 commandMap.begin(), commandMap.end(),
296 [i](const auto& typePair) { return typePair.second == i; });
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600297 if (it != commandMap.end())
298 {
Sridevi Rameshca4a8152020-08-11 09:26:19 -0500299 jarray["PLDM Command Code"] = i;
300 jarray["PLDM Command"] = it->first;
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600301 }
302 }
303
304 void printPldmCommands(std::vector<bitfield8_t>& cmdTypes,
305 pldm_supported_types pldmType)
306 {
Sridevi Rameshca4a8152020-08-11 09:26:19 -0500307 ordered_json output;
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600308 for (int i = 0; i < PLDM_MAX_CMDS_PER_TYPE; i++)
309 {
Sridevi Rameshca4a8152020-08-11 09:26:19 -0500310 ordered_json cmdinfo;
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600311 bitfield8_t b = cmdTypes[i / 8];
312 if (b.byte & (1 << i % 8))
313 {
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600314 switch (pldmType)
315 {
316 case PLDM_BASE:
Sridevi Rameshca4a8152020-08-11 09:26:19 -0500317 printCommand(pldmBaseCmds, i, cmdinfo);
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600318 break;
319 case PLDM_PLATFORM:
Sridevi Rameshca4a8152020-08-11 09:26:19 -0500320 printCommand(pldmPlatformCmds, i, cmdinfo);
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600321 break;
322 case PLDM_BIOS:
Sridevi Rameshca4a8152020-08-11 09:26:19 -0500323 printCommand(pldmBiosCmds, i, cmdinfo);
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600324 break;
325 case PLDM_FRU:
Sridevi Rameshca4a8152020-08-11 09:26:19 -0500326 printCommand(pldmFruCmds, i, cmdinfo);
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600327 break;
Sridevi Ramesh45e52542020-04-03 07:54:18 -0500328 case PLDM_OEM:
Pavithra Barithaya6f4e9042020-04-20 23:33:46 -0500329#ifdef OEM_IBM
Sridevi Rameshca4a8152020-08-11 09:26:19 -0500330 printCommand(pldmIBMHostCmds, i, cmdinfo);
331 printCommand(pldmIBMFileIOCmds, i, cmdinfo);
Pavithra Barithaya6f4e9042020-04-20 23:33:46 -0500332#endif
Sridevi Ramesh45e52542020-04-03 07:54:18 -0500333 break;
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600334 default:
335 break;
336 }
Sridevi Rameshca4a8152020-08-11 09:26:19 -0500337 output.emplace_back(cmdinfo);
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600338 }
339 }
Sridevi Rameshca4a8152020-08-11 09:26:19 -0500340 pldmtool::helper::DisplayInJson(output);
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600341 }
342};
343
John Wang58a0e062019-11-08 15:38:15 +0800344void registerCommand(CLI::App& app)
345{
346 auto base = app.add_subcommand("base", "base type command");
347 base->require_subcommand(1);
Sridevi Rameshadd56f02020-01-20 23:44:22 -0600348
Patrick Williams6da4f912023-05-10 07:50:53 -0500349 auto getPLDMTypes = base->add_subcommand("GetPLDMTypes",
350 "get pldm supported types");
John Wang58a0e062019-11-08 15:38:15 +0800351 commands.push_back(
352 std::make_unique<GetPLDMTypes>("base", "GetPLDMTypes", getPLDMTypes));
353
Patrick Williams6da4f912023-05-10 07:50:53 -0500354 auto getPLDMVersion = base->add_subcommand("GetPLDMVersion",
355 "get version of a certain type");
John Wang58a0e062019-11-08 15:38:15 +0800356 commands.push_back(std::make_unique<GetPLDMVersion>(
357 "base", "GetPLDMVersion", getPLDMVersion));
Sridevi Rameshadd56f02020-01-20 23:44:22 -0600358
359 auto getPLDMTID = base->add_subcommand("GetTID", "get Terminus ID (TID)");
360 commands.push_back(std::make_unique<GetTID>("base", "GetTID", getPLDMTID));
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600361
362 auto getPLDMCommands = base->add_subcommand(
363 "GetPLDMCommands", "get supported commands of pldm type");
364 commands.push_back(std::make_unique<GetPLDMCommands>(
365 "base", "GetPLDMCommands", getPLDMCommands));
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -0500366}
John Wang58a0e062019-11-08 15:38:15 +0800367
368} // namespace base
369} // namespace pldmtool