blob: 7edc33c7166c9f2c2005da036fc9661d63137803 [file] [log] [blame]
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -05001#include "pldm_base_cmd.hpp"
2
3#include "pldm_cmd_helper.hpp"
4
John Wang58a0e062019-11-08 15:38:15 +08005#include "libpldm/utils.h"
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -05006
John Wang58a0e062019-11-08 15:38:15 +08007namespace pldmtool
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -05008{
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -05009
John Wang58a0e062019-11-08 15:38:15 +080010namespace base
Lakshminarayana R. Kammath27693a42019-06-24 00:51:47 -050011{
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -050012
John Wang58a0e062019-11-08 15:38:15 +080013namespace
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -050014{
John Wang58a0e062019-11-08 15:38:15 +080015
16using namespace pldmtool::helper;
17
18std::vector<std::unique_ptr<CommandInterface>> commands;
19const std::map<const char*, pldm_supported_types> pldmTypes{
20 {"base", PLDM_BASE}, {"platform", PLDM_PLATFORM}, {"bios", PLDM_BIOS},
21 {"fru", PLDM_FRU}, {"oem", PLDM_OEM},
22};
23
Sridevi Ramesh66294d32020-02-21 03:52:07 -060024const std::map<const char*, pldm_supported_commands> pldmBaseCmds{
25 {"GetTID", PLDM_GET_TID},
26 {"GetPLDMVersion", PLDM_GET_PLDM_VERSION},
27 {"GetPLDMTypes", PLDM_GET_PLDM_TYPES},
28 {"GetPLDMCommands", PLDM_GET_PLDM_COMMANDS}};
29
30const std::map<const char*, pldm_bios_commands> pldmBiosCmds{
31 {"GetBIOSTable", PLDM_GET_BIOS_TABLE},
32 {"SetBIOSAttributeCurrentValue", PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE},
33 {"GetBIOSAttributeCurrentValueByHandle",
34 PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE},
35 {"GetDateTime", PLDM_GET_DATE_TIME},
36 {"SetDateTime", PLDM_SET_DATE_TIME}};
37
38const std::map<const char*, pldm_platform_commands> pldmPlatformCmds{
39 {"SetNumericEffecterValue", PLDM_SET_NUMERIC_EFFECTER_VALUE},
40 {"SetStateEffecterStates", PLDM_SET_STATE_EFFECTER_STATES},
41 {"GetPDR", PLDM_GET_PDR}};
42
43const std::map<const char*, pldm_fru_commands> pldmFruCmds{
44 {"GetFRURecordTableMetadata", PLDM_GET_FRU_RECORD_TABLE_METADATA},
45 {"GetFRURecordTable", PLDM_GET_FRU_RECORD_TABLE}};
46
John Wang58a0e062019-11-08 15:38:15 +080047} // namespace
48
49class GetPLDMTypes : public CommandInterface
50{
51 public:
52 ~GetPLDMTypes() = default;
53 GetPLDMTypes() = delete;
54 GetPLDMTypes(const GetPLDMTypes&) = delete;
55 GetPLDMTypes(GetPLDMTypes&&) = default;
56 GetPLDMTypes& operator=(const GetPLDMTypes&) = delete;
57 GetPLDMTypes& operator=(GetPLDMTypes&&) = default;
58
59 using CommandInterface::CommandInterface;
60
61 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -050062 {
John Wang58a0e062019-11-08 15:38:15 +080063 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr));
64 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -060065 auto rc = encode_get_types_req(instanceId, request);
John Wang58a0e062019-11-08 15:38:15 +080066 return {rc, requestMsg};
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -050067 }
68
John Wang58a0e062019-11-08 15:38:15 +080069 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -050070 {
John Wang58a0e062019-11-08 15:38:15 +080071 uint8_t cc = 0;
72 std::vector<bitfield8_t> types(8);
73 auto rc = decode_get_types_resp(responsePtr, payloadLength, &cc,
74 types.data());
75 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
76 {
77 std::cerr << "Response Message Error: "
Sampa Misraaa8ae722019-12-12 03:20:40 -060078 << "rc=" << rc << ",cc=" << (int)cc << "\n";
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -050079 return;
John Wang58a0e062019-11-08 15:38:15 +080080 }
81
82 printPldmTypes(types);
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -050083 }
84
John Wang58a0e062019-11-08 15:38:15 +080085 private:
86 void printPldmTypes(std::vector<bitfield8_t>& types)
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -050087 {
John Wang58a0e062019-11-08 15:38:15 +080088 std::cout << "Supported types:";
89 for (int i = 0; i < PLDM_MAX_TYPES; i++)
90 {
91 bitfield8_t b = types[i / 8];
92 if (b.byte & (1 << i % 8))
93 {
94 std::cout << " " << i;
95 auto it = std::find_if(
96 pldmTypes.begin(), pldmTypes.end(),
97 [i](const auto& typePair) { return typePair.second == i; });
98 if (it != pldmTypes.end())
99 {
100
101 std::cout << "(" << it->first << ")";
102 }
103 }
104 }
105
106 std::cout << std::endl;
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -0500107 }
John Wang58a0e062019-11-08 15:38:15 +0800108};
109
110class GetPLDMVersion : public CommandInterface
111{
112 public:
113 ~GetPLDMVersion() = default;
114 GetPLDMVersion() = delete;
115 GetPLDMVersion(const GetPLDMVersion&) = delete;
116 GetPLDMVersion(GetPLDMVersion&&) = default;
117 GetPLDMVersion& operator=(const GetPLDMVersion&) = delete;
118 GetPLDMVersion& operator=(GetPLDMVersion&&) = default;
119
120 explicit GetPLDMVersion(const char* type, const char* name, CLI::App* app) :
121 CommandInterface(type, name, app)
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -0500122 {
John Wang58a0e062019-11-08 15:38:15 +0800123 app->add_option("-t,--type", pldmType, "pldm supported type")
124 ->required()
125 ->transform(CLI::CheckedTransformer(pldmTypes, CLI::ignore_case));
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -0500126 }
John Wang58a0e062019-11-08 15:38:15 +0800127 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
128 {
129 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
130 PLDM_GET_VERSION_REQ_BYTES);
131 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
132
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -0600133 auto rc = encode_get_version_req(instanceId, 0, PLDM_GET_FIRSTPART,
134 pldmType, request);
John Wang58a0e062019-11-08 15:38:15 +0800135 return {rc, requestMsg};
136 }
137
138 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
139 {
140 uint8_t cc = 0, transferFlag = 0;
141 uint32_t transferHandle = 0;
142 ver32_t version;
143 auto rc =
144 decode_get_version_resp(responsePtr, payloadLength, &cc,
145 &transferHandle, &transferFlag, &version);
146 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
147 {
148 std::cerr << "Response Message Error: "
Sampa Misraaa8ae722019-12-12 03:20:40 -0600149 << "rc=" << rc << ",cc=" << (int)cc << "\n";
John Wang58a0e062019-11-08 15:38:15 +0800150 return;
151 }
152 char buffer[16] = {0};
153 ver2str(&version, buffer, sizeof(buffer));
154 std::cout << "Type " << pldmType;
155 auto it = std::find_if(
156 pldmTypes.begin(), pldmTypes.end(),
157 [&](const auto& typePair) { return typePair.second == pldmType; });
158
159 if (it != pldmTypes.end())
160 {
161 std::cout << "(" << it->first << ")";
162 }
163 std::cout << ": " << buffer << std::endl;
164 }
165
166 private:
167 pldm_supported_types pldmType;
168};
169
Sridevi Rameshadd56f02020-01-20 23:44:22 -0600170class GetTID : public CommandInterface
171{
172 public:
173 ~GetTID() = default;
174 GetTID() = delete;
175 GetTID(const GetTID&) = delete;
176 GetTID(GetTID&&) = default;
177 GetTID& operator=(const GetTID&) = delete;
178 GetTID& operator=(GetTID&&) = default;
179
180 using CommandInterface::CommandInterface;
181
182 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
183 {
184 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr));
185 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -0600186 auto rc = encode_get_tid_req(instanceId, request);
Sridevi Rameshadd56f02020-01-20 23:44:22 -0600187 return {rc, requestMsg};
188 }
189
190 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
191 {
192 uint8_t cc = 0;
193 uint8_t tid = 0;
194 std::vector<bitfield8_t> types(8);
195 auto rc = decode_get_tid_resp(responsePtr, payloadLength, &cc, &tid);
196 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
197 {
198 std::cerr << "Response Message Error: "
199 << "rc=" << rc << ",cc=" << (int)cc << "\n";
200 return;
201 }
Sridevi Rameshadd56f02020-01-20 23:44:22 -0600202 std::cout << "TID : " << static_cast<uint32_t>(tid) << std::endl;
203 }
204};
205
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600206class GetPLDMCommands : public CommandInterface
207{
208 public:
209 ~GetPLDMCommands() = default;
210 GetPLDMCommands() = delete;
211 GetPLDMCommands(const GetPLDMCommands&) = delete;
212 GetPLDMCommands(GetPLDMCommands&&) = default;
213 GetPLDMCommands& operator=(const GetPLDMCommands&) = delete;
214 GetPLDMCommands& operator=(GetPLDMCommands&&) = default;
215
216 explicit GetPLDMCommands(const char* type, const char* name,
217 CLI::App* app) :
218 CommandInterface(type, name, app)
219 {
220 app->add_option("-t,--type", pldmType, "pldm supported type")
221 ->required()
222 ->transform(CLI::CheckedTransformer(pldmTypes, CLI::ignore_case));
223 }
224
225 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
226 {
227 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
228 PLDM_GET_COMMANDS_REQ_BYTES);
229 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
230 ver32_t version{0xFF, 0xFF, 0xFF, 0xFF};
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -0600231 auto rc =
232 encode_get_commands_req(instanceId, pldmType, version, request);
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600233 return {rc, requestMsg};
234 }
235
236 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
237 {
238 uint8_t cc = 0;
239 std::vector<bitfield8_t> cmdTypes(32);
240 auto rc = decode_get_commands_resp(responsePtr, payloadLength, &cc,
241 cmdTypes.data());
242 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
243 {
244 std::cerr << "Response Message Error: "
245 << "rc=" << rc << ",cc=" << (int)cc << "\n";
246 return;
247 }
248 printPldmCommands(cmdTypes, pldmType);
249 }
250
251 private:
252 pldm_supported_types pldmType;
253
254 template <typename CommandMap>
255 void printCommand(CommandMap& commandMap, int i)
256 {
257 auto it = std::find_if(
258 commandMap.begin(), commandMap.end(),
259 [i](const auto& typePair) { return typePair.second == i; });
260 if (it != commandMap.end())
261 {
262 std::cout << "(" << it->first << ")";
263 }
264 }
265
266 void printPldmCommands(std::vector<bitfield8_t>& cmdTypes,
267 pldm_supported_types pldmType)
268 {
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600269 std::cout << "Supported Commands :";
270 for (int i = 0; i < PLDM_MAX_CMDS_PER_TYPE; i++)
271 {
272 bitfield8_t b = cmdTypes[i / 8];
273 if (b.byte & (1 << i % 8))
274 {
275 std::cout << " " << i;
276 switch (pldmType)
277 {
278 case PLDM_BASE:
279 printCommand(pldmBaseCmds, i);
280 break;
281 case PLDM_PLATFORM:
282 printCommand(pldmPlatformCmds, i);
283 break;
284 case PLDM_BIOS:
285 printCommand(pldmBiosCmds, i);
286 break;
287 case PLDM_FRU:
288 printCommand(pldmFruCmds, i);
289 break;
290 default:
291 break;
292 }
293 }
294 }
295 std::cout << std::endl;
296 }
297};
298
John Wang58a0e062019-11-08 15:38:15 +0800299void registerCommand(CLI::App& app)
300{
301 auto base = app.add_subcommand("base", "base type command");
302 base->require_subcommand(1);
Sridevi Rameshadd56f02020-01-20 23:44:22 -0600303
John Wang58a0e062019-11-08 15:38:15 +0800304 auto getPLDMTypes =
305 base->add_subcommand("GetPLDMTypes", "get pldm supported types");
306 commands.push_back(
307 std::make_unique<GetPLDMTypes>("base", "GetPLDMTypes", getPLDMTypes));
308
309 auto getPLDMVersion =
310 base->add_subcommand("GetPLDMVersion", "get version of a certain type");
311 commands.push_back(std::make_unique<GetPLDMVersion>(
312 "base", "GetPLDMVersion", getPLDMVersion));
Sridevi Rameshadd56f02020-01-20 23:44:22 -0600313
314 auto getPLDMTID = base->add_subcommand("GetTID", "get Terminus ID (TID)");
315 commands.push_back(std::make_unique<GetTID>("base", "GetTID", getPLDMTID));
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600316
317 auto getPLDMCommands = base->add_subcommand(
318 "GetPLDMCommands", "get supported commands of pldm type");
319 commands.push_back(std::make_unique<GetPLDMCommands>(
320 "base", "GetPLDMCommands", getPLDMCommands));
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -0500321}
John Wang58a0e062019-11-08 15:38:15 +0800322
323} // namespace base
324} // namespace pldmtool