blob: b1028d007c6cfb003b4e2628524b06c38f1ef6b7 [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 }
202 std::cout << "Parsed Response Msg: " << std::endl;
203 std::cout << "TID : " << static_cast<uint32_t>(tid) << std::endl;
204 }
205};
206
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600207class GetPLDMCommands : public CommandInterface
208{
209 public:
210 ~GetPLDMCommands() = default;
211 GetPLDMCommands() = delete;
212 GetPLDMCommands(const GetPLDMCommands&) = delete;
213 GetPLDMCommands(GetPLDMCommands&&) = default;
214 GetPLDMCommands& operator=(const GetPLDMCommands&) = delete;
215 GetPLDMCommands& operator=(GetPLDMCommands&&) = default;
216
217 explicit GetPLDMCommands(const char* type, const char* name,
218 CLI::App* app) :
219 CommandInterface(type, name, app)
220 {
221 app->add_option("-t,--type", pldmType, "pldm supported type")
222 ->required()
223 ->transform(CLI::CheckedTransformer(pldmTypes, CLI::ignore_case));
224 }
225
226 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
227 {
228 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
229 PLDM_GET_COMMANDS_REQ_BYTES);
230 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
231 ver32_t version{0xFF, 0xFF, 0xFF, 0xFF};
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -0600232 auto rc =
233 encode_get_commands_req(instanceId, pldmType, version, request);
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600234 return {rc, requestMsg};
235 }
236
237 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
238 {
239 uint8_t cc = 0;
240 std::vector<bitfield8_t> cmdTypes(32);
241 auto rc = decode_get_commands_resp(responsePtr, payloadLength, &cc,
242 cmdTypes.data());
243 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
244 {
245 std::cerr << "Response Message Error: "
246 << "rc=" << rc << ",cc=" << (int)cc << "\n";
247 return;
248 }
249 printPldmCommands(cmdTypes, pldmType);
250 }
251
252 private:
253 pldm_supported_types pldmType;
254
255 template <typename CommandMap>
256 void printCommand(CommandMap& commandMap, int i)
257 {
258 auto it = std::find_if(
259 commandMap.begin(), commandMap.end(),
260 [i](const auto& typePair) { return typePair.second == i; });
261 if (it != commandMap.end())
262 {
263 std::cout << "(" << it->first << ")";
264 }
265 }
266
267 void printPldmCommands(std::vector<bitfield8_t>& cmdTypes,
268 pldm_supported_types pldmType)
269 {
270 std::cout << "Parsed Response Msg:" << std::endl;
271 std::cout << "Supported Commands :";
272 for (int i = 0; i < PLDM_MAX_CMDS_PER_TYPE; i++)
273 {
274 bitfield8_t b = cmdTypes[i / 8];
275 if (b.byte & (1 << i % 8))
276 {
277 std::cout << " " << i;
278 switch (pldmType)
279 {
280 case PLDM_BASE:
281 printCommand(pldmBaseCmds, i);
282 break;
283 case PLDM_PLATFORM:
284 printCommand(pldmPlatformCmds, i);
285 break;
286 case PLDM_BIOS:
287 printCommand(pldmBiosCmds, i);
288 break;
289 case PLDM_FRU:
290 printCommand(pldmFruCmds, i);
291 break;
292 default:
293 break;
294 }
295 }
296 }
297 std::cout << std::endl;
298 }
299};
300
John Wang58a0e062019-11-08 15:38:15 +0800301void registerCommand(CLI::App& app)
302{
303 auto base = app.add_subcommand("base", "base type command");
304 base->require_subcommand(1);
Sridevi Rameshadd56f02020-01-20 23:44:22 -0600305
John Wang58a0e062019-11-08 15:38:15 +0800306 auto getPLDMTypes =
307 base->add_subcommand("GetPLDMTypes", "get pldm supported types");
308 commands.push_back(
309 std::make_unique<GetPLDMTypes>("base", "GetPLDMTypes", getPLDMTypes));
310
311 auto getPLDMVersion =
312 base->add_subcommand("GetPLDMVersion", "get version of a certain type");
313 commands.push_back(std::make_unique<GetPLDMVersion>(
314 "base", "GetPLDMVersion", getPLDMVersion));
Sridevi Rameshadd56f02020-01-20 23:44:22 -0600315
316 auto getPLDMTID = base->add_subcommand("GetTID", "get Terminus ID (TID)");
317 commands.push_back(std::make_unique<GetTID>("base", "GetTID", getPLDMTID));
Sridevi Ramesh66294d32020-02-21 03:52:07 -0600318
319 auto getPLDMCommands = base->add_subcommand(
320 "GetPLDMCommands", "get supported commands of pldm type");
321 commands.push_back(std::make_unique<GetPLDMCommands>(
322 "base", "GetPLDMCommands", getPLDMCommands));
Lakshminarayana R. Kammath58229b22019-08-09 06:30:04 -0500323}
John Wang58a0e062019-11-08 15:38:15 +0800324
325} // namespace base
326} // namespace pldmtool