Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 1 | #include "pldm_base_cmd.hpp" |
| 2 | |
| 3 | #include "pldm_cmd_helper.hpp" |
| 4 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 5 | #include "libpldm/utils.h" |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 6 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 7 | namespace pldmtool |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 8 | { |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 9 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 10 | namespace base |
Lakshminarayana R. Kammath | 27693a4 | 2019-06-24 00:51:47 -0500 | [diff] [blame] | 11 | { |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 12 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 13 | namespace |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 14 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 15 | |
| 16 | using namespace pldmtool::helper; |
| 17 | |
| 18 | std::vector<std::unique_ptr<CommandInterface>> commands; |
| 19 | const 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 Ramesh | 66294d3 | 2020-02-21 03:52:07 -0600 | [diff] [blame] | 24 | const 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 | |
| 30 | const 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 | |
| 38 | const 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 | |
| 43 | const 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 Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 47 | } // namespace |
| 48 | |
| 49 | class 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. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 62 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 63 | std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr)); |
| 64 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
Pavithra Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 65 | auto rc = encode_get_types_req(instanceId, request); |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 66 | return {rc, requestMsg}; |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 67 | } |
| 68 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 69 | void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 70 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 71 | 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 Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 78 | << "rc=" << rc << ",cc=" << (int)cc << "\n"; |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 79 | return; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | printPldmTypes(types); |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 83 | } |
| 84 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 85 | private: |
| 86 | void printPldmTypes(std::vector<bitfield8_t>& types) |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 87 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 88 | 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. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 107 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | class 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. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 122 | { |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 123 | app->add_option("-t,--type", pldmType, "pldm supported type") |
| 124 | ->required() |
| 125 | ->transform(CLI::CheckedTransformer(pldmTypes, CLI::ignore_case)); |
Lakshminarayana R. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 126 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 127 | 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 Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 133 | auto rc = encode_get_version_req(instanceId, 0, PLDM_GET_FIRSTPART, |
| 134 | pldmType, request); |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 135 | 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 Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 149 | << "rc=" << rc << ",cc=" << (int)cc << "\n"; |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 150 | 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 Ramesh | add56f0 | 2020-01-20 23:44:22 -0600 | [diff] [blame] | 170 | class 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 Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 186 | auto rc = encode_get_tid_req(instanceId, request); |
Sridevi Ramesh | add56f0 | 2020-01-20 23:44:22 -0600 | [diff] [blame] | 187 | 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 Ramesh | add56f0 | 2020-01-20 23:44:22 -0600 | [diff] [blame] | 202 | std::cout << "TID : " << static_cast<uint32_t>(tid) << std::endl; |
| 203 | } |
| 204 | }; |
| 205 | |
Sridevi Ramesh | 66294d3 | 2020-02-21 03:52:07 -0600 | [diff] [blame] | 206 | class 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 Barithaya | ac3c45a | 2020-03-05 02:28:26 -0600 | [diff] [blame] | 231 | auto rc = |
| 232 | encode_get_commands_req(instanceId, pldmType, version, request); |
Sridevi Ramesh | 66294d3 | 2020-02-21 03:52:07 -0600 | [diff] [blame] | 233 | 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 Ramesh | 66294d3 | 2020-02-21 03:52:07 -0600 | [diff] [blame] | 269 | 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 Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 299 | void registerCommand(CLI::App& app) |
| 300 | { |
| 301 | auto base = app.add_subcommand("base", "base type command"); |
| 302 | base->require_subcommand(1); |
Sridevi Ramesh | add56f0 | 2020-01-20 23:44:22 -0600 | [diff] [blame] | 303 | |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 304 | 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 Ramesh | add56f0 | 2020-01-20 23:44:22 -0600 | [diff] [blame] | 313 | |
| 314 | auto getPLDMTID = base->add_subcommand("GetTID", "get Terminus ID (TID)"); |
| 315 | commands.push_back(std::make_unique<GetTID>("base", "GetTID", getPLDMTID)); |
Sridevi Ramesh | 66294d3 | 2020-02-21 03:52:07 -0600 | [diff] [blame] | 316 | |
| 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. Kammath | 58229b2 | 2019-08-09 06:30:04 -0500 | [diff] [blame] | 321 | } |
John Wang | 58a0e06 | 2019-11-08 15:38:15 +0800 | [diff] [blame] | 322 | |
| 323 | } // namespace base |
| 324 | } // namespace pldmtool |