vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 1 | #include "apphandler.h" |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 2 | |
Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 3 | #include "app/channel.hpp" |
| 4 | #include "app/watchdog.hpp" |
| 5 | #include "ipmid.hpp" |
| 6 | #include "nlohmann/json.hpp" |
| 7 | #include "sys_info_param.hpp" |
| 8 | #include "transporthandler.hpp" |
| 9 | #include "types.hpp" |
| 10 | #include "utils.hpp" |
| 11 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 12 | #include <arpa/inet.h> |
Xo Wang | 8765133 | 2017-08-11 10:17:59 -0700 | [diff] [blame^] | 13 | #include <limits.h> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 14 | #include <mapper.h> |
| 15 | #include <stdint.h> |
| 16 | #include <stdio.h> |
| 17 | #include <systemd/sd-bus.h> |
Xo Wang | 8765133 | 2017-08-11 10:17:59 -0700 | [diff] [blame^] | 18 | #include <unistd.h> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 19 | |
Patrick Williams | 37af733 | 2016-09-02 21:21:42 -0500 | [diff] [blame] | 20 | #include "host-ipmid/ipmid-api.h" |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 21 | |
Vernon Mauery | 185b9f8 | 2018-07-20 10:52:36 -0700 | [diff] [blame] | 22 | #if __has_include(<filesystem>) |
| 23 | #include <filesystem> |
| 24 | #elif __has_include(<experimental/filesystem>) |
| 25 | #include <experimental/filesystem> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 26 | namespace std |
| 27 | { |
| 28 | // splice experimental::filesystem into std |
| 29 | namespace filesystem = std::experimental::filesystem; |
| 30 | } // namespace std |
Vernon Mauery | 185b9f8 | 2018-07-20 10:52:36 -0700 | [diff] [blame] | 31 | #else |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 32 | #error filesystem not available |
Vernon Mauery | 185b9f8 | 2018-07-20 10:52:36 -0700 | [diff] [blame] | 33 | #endif |
Ratan Gupta | 62736ec | 2017-09-02 12:02:47 +0530 | [diff] [blame] | 34 | |
Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 35 | #include <algorithm> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 36 | #include <array> |
| 37 | #include <cstddef> |
| 38 | #include <fstream> |
Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 39 | #include <memory> |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 40 | #include <phosphor-logging/elog-errors.hpp> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 41 | #include <phosphor-logging/log.hpp> |
| 42 | #include <string> |
Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 43 | #include <tuple> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 44 | #include <vector> |
| 45 | #include <xyz/openbmc_project/Common/error.hpp> |
| 46 | #include <xyz/openbmc_project/Software/Activation/server.hpp> |
| 47 | #include <xyz/openbmc_project/Software/Version/server.hpp> |
Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 48 | #include <xyz/openbmc_project/State/BMC/server.hpp> |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 49 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 50 | extern sd_bus* bus; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 51 | |
Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 52 | constexpr auto bmc_state_interface = "xyz.openbmc_project.State.BMC"; |
| 53 | constexpr auto bmc_state_property = "CurrentBMCState"; |
Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 54 | constexpr auto bmc_interface = "xyz.openbmc_project.Inventory.Item.Bmc"; |
| 55 | constexpr auto bmc_guid_interface = "xyz.openbmc_project.Common.UUID"; |
| 56 | constexpr auto bmc_guid_property = "UUID"; |
| 57 | constexpr auto bmc_guid_len = 16; |
| 58 | |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 59 | static constexpr auto redundancyIntf = |
| 60 | "xyz.openbmc_project.Software.RedundancyPriority"; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 61 | static constexpr auto versionIntf = "xyz.openbmc_project.Software.Version"; |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 62 | static constexpr auto activationIntf = |
| 63 | "xyz.openbmc_project.Software.Activation"; |
| 64 | static constexpr auto softwareRoot = "/xyz/openbmc_project/software"; |
| 65 | |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 66 | void register_netfn_app_functions() __attribute__((constructor)); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 67 | |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 68 | using namespace phosphor::logging; |
| 69 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 70 | using Version = sdbusplus::xyz::openbmc_project::Software::server::Version; |
| 71 | using Activation = |
| 72 | sdbusplus::xyz::openbmc_project::Software::server::Activation; |
Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 73 | using BMC = sdbusplus::xyz::openbmc_project::State::server::BMC; |
Vernon Mauery | 185b9f8 | 2018-07-20 10:52:36 -0700 | [diff] [blame] | 74 | namespace fs = std::filesystem; |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 75 | |
Nan Li | ee0cb90 | 2016-07-11 15:38:03 +0800 | [diff] [blame] | 76 | // Offset in get device id command. |
| 77 | typedef struct |
| 78 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 79 | uint8_t id; |
| 80 | uint8_t revision; |
| 81 | uint8_t fw[2]; |
| 82 | uint8_t ipmi_ver; |
| 83 | uint8_t addn_dev_support; |
| 84 | uint8_t manuf_id[3]; |
| 85 | uint8_t prod_id[2]; |
| 86 | uint8_t aux[4]; |
| 87 | } __attribute__((packed)) ipmi_device_id_t; |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 88 | |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 89 | /** |
| 90 | * @brief Returns the Version info from primary s/w object |
| 91 | * |
| 92 | * Get the Version info from the active s/w object which is having high |
| 93 | * "Priority" value(a smaller number is a higher priority) and "Purpose" |
| 94 | * is "BMC" from the list of all s/w objects those are implementing |
| 95 | * RedundancyPriority interface from the given softwareRoot path. |
| 96 | * |
| 97 | * @return On success returns the Version info from primary s/w object. |
| 98 | * |
| 99 | */ |
| 100 | std::string getActiveSoftwareVersionInfo() |
| 101 | { |
| 102 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 103 | |
| 104 | std::string revision{}; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 105 | auto objectTree = |
| 106 | ipmi::getAllDbusObjects(bus, softwareRoot, redundancyIntf, ""); |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 107 | if (objectTree.empty()) |
| 108 | { |
| 109 | log<level::ERR>("No Obj has implemented the s/w redundancy interface", |
| 110 | entry("INTERFACE=%s", redundancyIntf)); |
| 111 | elog<InternalFailure>(); |
| 112 | } |
| 113 | |
| 114 | auto objectFound = false; |
| 115 | for (auto& softObject : objectTree) |
| 116 | { |
| 117 | auto service = ipmi::getService(bus, redundancyIntf, softObject.first); |
| 118 | auto objValueTree = ipmi::getManagedObjects(bus, service, softwareRoot); |
| 119 | |
| 120 | auto minPriority = 0xFF; |
| 121 | for (const auto& objIter : objValueTree) |
| 122 | { |
| 123 | try |
| 124 | { |
| 125 | auto& intfMap = objIter.second; |
| 126 | auto& redundancyPriorityProps = intfMap.at(redundancyIntf); |
| 127 | auto& versionProps = intfMap.at(versionIntf); |
| 128 | auto& activationProps = intfMap.at(activationIntf); |
| 129 | auto priority = |
| 130 | redundancyPriorityProps.at("Priority").get<uint8_t>(); |
| 131 | auto purpose = versionProps.at("Purpose").get<std::string>(); |
| 132 | auto activation = |
| 133 | activationProps.at("Activation").get<std::string>(); |
| 134 | auto version = versionProps.at("Version").get<std::string>(); |
| 135 | if ((Version::convertVersionPurposeFromString(purpose) == |
| 136 | Version::VersionPurpose::BMC) && |
| 137 | (Activation::convertActivationsFromString(activation) == |
| 138 | Activation::Activations::Active)) |
| 139 | { |
| 140 | if (priority < minPriority) |
| 141 | { |
| 142 | minPriority = priority; |
| 143 | objectFound = true; |
| 144 | revision = std::move(version); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | catch (const std::exception& e) |
| 149 | { |
| 150 | log<level::ERR>(e.what()); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if (!objectFound) |
| 156 | { |
| 157 | log<level::ERR>("Could not found an BMC software Object"); |
| 158 | elog<InternalFailure>(); |
| 159 | } |
| 160 | |
| 161 | return revision; |
| 162 | } |
| 163 | |
Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 164 | bool getCurrentBmcState() |
| 165 | { |
| 166 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 167 | |
| 168 | // Get the Inventory object implementing the BMC interface |
| 169 | ipmi::DbusObjectInfo bmcObject = |
| 170 | ipmi::getDbusObject(bus, bmc_state_interface); |
| 171 | auto variant = |
| 172 | ipmi::getDbusProperty(bus, bmcObject.second, bmcObject.first, |
| 173 | bmc_state_interface, bmc_state_property); |
| 174 | |
| 175 | return variant.is<std::string>() && |
| 176 | BMC::convertBMCStateFromString(variant.get<std::string>()) == |
| 177 | BMC::BMCState::Ready; |
| 178 | } |
| 179 | |
Adriana Kobylak | 3a552e1 | 2015-10-19 16:11:00 -0500 | [diff] [blame] | 180 | ipmi_ret_t ipmi_app_set_acpi_power_state(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 181 | ipmi_request_t request, |
| 182 | ipmi_response_t response, |
| 183 | ipmi_data_len_t data_len, |
| 184 | ipmi_context_t context) |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 185 | { |
| 186 | ipmi_ret_t rc = IPMI_CC_OK; |
| 187 | *data_len = 0; |
| 188 | |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 189 | log<level::DEBUG>("IPMI SET ACPI STATE Ignoring for now\n"); |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 190 | return rc; |
| 191 | } |
| 192 | |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 193 | typedef struct |
| 194 | { |
| 195 | char major; |
| 196 | char minor; |
Chris Austen | 176c965 | 2016-04-30 16:32:17 -0500 | [diff] [blame] | 197 | uint16_t d[2]; |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 198 | } rev_t; |
| 199 | |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 200 | /* Currently supports the vx.x-x-[-x] and v1.x.x-x-[-x] format. It will */ |
| 201 | /* return -1 if not in those formats, this routine knows how to parse */ |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 202 | /* version = v0.6-19-gf363f61-dirty */ |
| 203 | /* ^ ^ ^^ ^ */ |
| 204 | /* | | |----------|-- additional details */ |
| 205 | /* | |---------------- Minor */ |
| 206 | /* |------------------ Major */ |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 207 | /* and version = v1.99.10-113-g65edf7d-r3-0-g9e4f715 */ |
| 208 | /* ^ ^ ^^ ^ */ |
| 209 | /* | | |--|---------- additional details */ |
| 210 | /* | |---------------- Minor */ |
| 211 | /* |------------------ Major */ |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 212 | /* Additional details : If the option group exists it will force Auxiliary */ |
| 213 | /* Firmware Revision Information 4th byte to 1 indicating the build was */ |
| 214 | /* derived with additional edits */ |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 215 | int convert_version(const char* p, rev_t* rev) |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 216 | { |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 217 | std::string s(p); |
| 218 | std::string token; |
Chris Austen | 176c965 | 2016-04-30 16:32:17 -0500 | [diff] [blame] | 219 | uint16_t commits; |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 220 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 221 | auto location = s.find_first_of('v'); |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 222 | if (location != std::string::npos) |
| 223 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 224 | s = s.substr(location + 1); |
Chris Austen | 176c965 | 2016-04-30 16:32:17 -0500 | [diff] [blame] | 225 | } |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 226 | |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 227 | if (!s.empty()) |
| 228 | { |
| 229 | location = s.find_first_of("."); |
| 230 | if (location != std::string::npos) |
| 231 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 232 | rev->major = |
| 233 | static_cast<char>(std::stoi(s.substr(0, location), 0, 16)); |
| 234 | token = s.substr(location + 1); |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 235 | } |
Chris Austen | 176c965 | 2016-04-30 16:32:17 -0500 | [diff] [blame] | 236 | |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 237 | if (!token.empty()) |
| 238 | { |
| 239 | location = token.find_first_of(".-"); |
| 240 | if (location != std::string::npos) |
| 241 | { |
Patrick Venture | d211702 | 2018-02-06 08:54:37 -0800 | [diff] [blame] | 242 | rev->minor = static_cast<char>( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 243 | std::stoi(token.substr(0, location), 0, 16)); |
| 244 | token = token.substr(location + 1); |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 245 | } |
| 246 | } |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 247 | |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 248 | // Capture the number of commits on top of the minor tag. |
| 249 | // I'm using BE format like the ipmi spec asked for |
| 250 | location = token.find_first_of(".-"); |
| 251 | if (!token.empty()) |
| 252 | { |
| 253 | commits = std::stoi(token.substr(0, location), 0, 16); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 254 | rev->d[0] = (commits >> 8) | (commits << 8); |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 255 | |
| 256 | // commit number we skip |
| 257 | location = token.find_first_of(".-"); |
| 258 | if (location != std::string::npos) |
| 259 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 260 | token = token.substr(location + 1); |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 261 | } |
| 262 | } |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 263 | else |
| 264 | { |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 265 | rev->d[0] = 0; |
| 266 | } |
| 267 | |
| 268 | if (location != std::string::npos) |
| 269 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 270 | token = token.substr(location + 1); |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | // Any value of the optional parameter forces it to 1 |
| 274 | location = token.find_first_of(".-"); |
| 275 | if (location != std::string::npos) |
| 276 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 277 | token = token.substr(location + 1); |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 278 | } |
| 279 | commits = (!token.empty()) ? 1 : 0; |
| 280 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 281 | // We do this operation to get this displayed in least significant bytes |
| 282 | // of ipmitool device id command. |
| 283 | rev->d[1] = (commits >> 8) | (commits << 8); |
Dinesh Chinari | 2b7e07d | 2017-11-08 15:38:50 -0600 | [diff] [blame] | 284 | } |
| 285 | |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 286 | return 0; |
| 287 | } |
| 288 | |
Adriana Kobylak | 3a552e1 | 2015-10-19 16:11:00 -0500 | [diff] [blame] | 289 | ipmi_ret_t ipmi_app_get_device_id(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 290 | ipmi_request_t request, |
| 291 | ipmi_response_t response, |
| 292 | ipmi_data_len_t data_len, |
| 293 | ipmi_context_t context) |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 294 | { |
| 295 | ipmi_ret_t rc = IPMI_CC_OK; |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 296 | int r = -1; |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 297 | rev_t rev = {0}; |
David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 298 | static ipmi_device_id_t dev_id{}; |
| 299 | static bool dev_id_initialized = false; |
| 300 | const char* filename = "/usr/share/ipmi-providers/dev_id.json"; |
Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 301 | constexpr auto ipmiDevIdStateShift = 7; |
| 302 | constexpr auto ipmiDevIdFw1Mask = ~(1 << ipmiDevIdStateShift); |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 303 | |
| 304 | // Data length |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 305 | *data_len = sizeof(dev_id); |
| 306 | |
David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 307 | if (!dev_id_initialized) |
| 308 | { |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 309 | try |
| 310 | { |
| 311 | auto version = getActiveSoftwareVersionInfo(); |
| 312 | r = convert_version(version.c_str(), &rev); |
David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 313 | } |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 314 | catch (const std::exception& e) |
| 315 | { |
| 316 | log<level::ERR>(e.what()); |
| 317 | } |
Nan Li | ee0cb90 | 2016-07-11 15:38:03 +0800 | [diff] [blame] | 318 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 319 | if (r >= 0) |
| 320 | { |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 321 | // bit7 identifies if the device is available |
| 322 | // 0=normal operation |
| 323 | // 1=device firmware, SDR update, |
| 324 | // or self-initialization in progress. |
Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 325 | // The availability may change in run time, so mask here |
| 326 | // and initialize later. |
| 327 | dev_id.fw[0] = rev.major & ipmiDevIdFw1Mask; |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 328 | |
| 329 | rev.minor = (rev.minor > 99 ? 99 : rev.minor); |
| 330 | dev_id.fw[1] = rev.minor % 10 + (rev.minor / 10) * 16; |
| 331 | memcpy(&dev_id.aux, rev.d, 4); |
David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 332 | } |
Nan Li | ee0cb90 | 2016-07-11 15:38:03 +0800 | [diff] [blame] | 333 | |
David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 334 | // IPMI Spec version 2.0 |
| 335 | dev_id.ipmi_ver = 2; |
Adriana Kobylak | 0e91264 | 2016-06-22 16:54:39 -0500 | [diff] [blame] | 336 | |
David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 337 | std::ifstream dev_id_file(filename); |
| 338 | if (dev_id_file.is_open()) |
| 339 | { |
| 340 | auto data = nlohmann::json::parse(dev_id_file, nullptr, false); |
| 341 | if (!data.is_discarded()) |
| 342 | { |
| 343 | dev_id.id = data.value("id", 0); |
| 344 | dev_id.revision = data.value("revision", 0); |
| 345 | dev_id.addn_dev_support = data.value("addn_dev_support", 0); |
| 346 | dev_id.manuf_id[2] = data.value("manuf_id", 0) >> 16; |
| 347 | dev_id.manuf_id[1] = data.value("manuf_id", 0) >> 8; |
| 348 | dev_id.manuf_id[0] = data.value("manuf_id", 0); |
| 349 | dev_id.prod_id[1] = data.value("prod_id", 0) >> 8; |
| 350 | dev_id.prod_id[0] = data.value("prod_id", 0); |
Tom Joseph | af8a098 | 2018-03-09 07:54:02 -0600 | [diff] [blame] | 351 | dev_id.aux[3] = data.value("aux", 0); |
| 352 | dev_id.aux[2] = data.value("aux", 0) >> 8; |
| 353 | dev_id.aux[1] = data.value("aux", 0) >> 16; |
| 354 | dev_id.aux[0] = data.value("aux", 0) >> 24; |
David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 355 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 356 | // Don't read the file every time if successful |
David Cobbley | a1adb07 | 2017-11-21 15:58:13 -0800 | [diff] [blame] | 357 | dev_id_initialized = true; |
| 358 | } |
| 359 | else |
| 360 | { |
| 361 | log<level::ERR>("Device ID JSON parser failure"); |
| 362 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 363 | } |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | log<level::ERR>("Device ID file not found"); |
| 368 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 369 | } |
| 370 | } |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 371 | |
Alexander Amelkin | ba19c18 | 2018-09-04 15:49:36 +0300 | [diff] [blame] | 372 | // Set availability to the actual current BMC state |
| 373 | dev_id.fw[0] &= ipmiDevIdFw1Mask; |
| 374 | if (!getCurrentBmcState()) |
| 375 | { |
| 376 | dev_id.fw[0] |= (1 << ipmiDevIdStateShift); |
| 377 | } |
| 378 | |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 379 | // Pack the actual response |
Chris Austen | 7303bdc | 2016-04-17 11:50:54 -0500 | [diff] [blame] | 380 | memcpy(response, &dev_id, *data_len); |
Nagaraju Goruganti | 744398d | 2018-02-20 09:52:00 -0600 | [diff] [blame] | 381 | |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 382 | return rc; |
| 383 | } |
| 384 | |
Nan Li | 41fa24a | 2016-11-10 20:12:37 +0800 | [diff] [blame] | 385 | ipmi_ret_t ipmi_app_get_self_test_results(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 386 | ipmi_request_t request, |
| 387 | ipmi_response_t response, |
| 388 | ipmi_data_len_t data_len, |
| 389 | ipmi_context_t context) |
Nan Li | 41fa24a | 2016-11-10 20:12:37 +0800 | [diff] [blame] | 390 | { |
| 391 | ipmi_ret_t rc = IPMI_CC_OK; |
| 392 | |
| 393 | // Byte 2: |
| 394 | // 55h - No error. |
Gunnar Mills | 8991dd6 | 2017-10-25 17:11:29 -0500 | [diff] [blame] | 395 | // 56h - Self Test function not implemented in this controller. |
Nan Li | 41fa24a | 2016-11-10 20:12:37 +0800 | [diff] [blame] | 396 | // 57h - Corrupted or inaccesssible data or devices. |
| 397 | // 58h - Fatal hardware error. |
| 398 | // FFh - reserved. |
| 399 | // all other: Device-specific 'internal failure'. |
| 400 | // Byte 3: |
| 401 | // For byte 2 = 55h, 56h, FFh: 00h |
| 402 | // For byte 2 = 58h, all other: Device-specific |
| 403 | // For byte 2 = 57h: self-test error bitfield. |
| 404 | // Note: returning 57h does not imply that all test were run. |
| 405 | // [7] 1b = Cannot access SEL device. |
| 406 | // [6] 1b = Cannot access SDR Repository. |
| 407 | // [5] 1b = Cannot access BMC FRU device. |
| 408 | // [4] 1b = IPMB signal lines do not respond. |
| 409 | // [3] 1b = SDR Repository empty. |
| 410 | // [2] 1b = Internal Use Area of BMC FRU corrupted. |
| 411 | // [1] 1b = controller update 'boot block' firmware corrupted. |
| 412 | // [0] 1b = controller operational firmware corrupted. |
| 413 | |
| 414 | char selftestresults[2] = {0}; |
| 415 | |
| 416 | *data_len = 2; |
| 417 | |
| 418 | selftestresults[0] = 0x56; |
| 419 | selftestresults[1] = 0; |
| 420 | |
| 421 | memcpy(response, selftestresults, *data_len); |
| 422 | |
| 423 | return rc; |
| 424 | } |
| 425 | |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 426 | ipmi_ret_t ipmi_app_get_device_guid(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 427 | ipmi_request_t request, |
| 428 | ipmi_response_t response, |
| 429 | ipmi_data_len_t data_len, |
| 430 | ipmi_context_t context) |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 431 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 432 | const char* objname = "/org/openbmc/control/chassis0"; |
| 433 | const char* iface = "org.freedesktop.DBus.Properties"; |
| 434 | const char* chassis_iface = "org.openbmc.control.Chassis"; |
| 435 | sd_bus_message* reply = NULL; |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 436 | sd_bus_error error = SD_BUS_ERROR_NULL; |
| 437 | int r = 0; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 438 | char* uuid = NULL; |
| 439 | char* busname = NULL; |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 440 | |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 441 | // UUID is in RFC4122 format. Ex: 61a39523-78f2-11e5-9862-e6402cfc3223 |
Patrick Venture | d211702 | 2018-02-06 08:54:37 -0800 | [diff] [blame] | 442 | // Per IPMI Spec 2.0 need to convert to 16 hex bytes and reverse the byte |
| 443 | // order |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 444 | // Ex: 0x2332fc2c40e66298e511f2782395a361 |
| 445 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 446 | const int resp_size = 16; // Response is 16 hex bytes per IPMI Spec |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 447 | uint8_t resp_uuid[resp_size]; // Array to hold the formatted response |
Patrick Venture | d211702 | 2018-02-06 08:54:37 -0800 | [diff] [blame] | 448 | // Point resp end of array to save in reverse order |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 449 | int resp_loc = resp_size - 1; |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 450 | int i = 0; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 451 | char* tokptr = NULL; |
| 452 | char* id_octet = NULL; |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 453 | |
| 454 | // Status code. |
| 455 | ipmi_ret_t rc = IPMI_CC_OK; |
| 456 | *data_len = 0; |
| 457 | |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 458 | // Call Get properties method with the interface and property name |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 459 | r = mapper_get_service(bus, objname, &busname); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 460 | if (r < 0) |
| 461 | { |
| 462 | log<level::ERR>("Failed to get bus name", entry("BUS=%s", objname), |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 463 | entry("ERRNO=0x%X", -r)); |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 464 | goto finish; |
| 465 | } |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 466 | r = sd_bus_call_method(bus, busname, objname, iface, "Get", &error, &reply, |
| 467 | "ss", chassis_iface, "uuid"); |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 468 | if (r < 0) |
| 469 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 470 | log<level::ERR>("Failed to call Get Method", entry("ERRNO=0x%X", -r)); |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 471 | rc = IPMI_CC_UNSPECIFIED_ERROR; |
| 472 | goto finish; |
| 473 | } |
| 474 | |
| 475 | r = sd_bus_message_read(reply, "v", "s", &uuid); |
| 476 | if (r < 0 || uuid == NULL) |
| 477 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 478 | log<level::ERR>("Failed to get a response", entry("ERRNO=0x%X", -r)); |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 479 | rc = IPMI_CC_RESPONSE_ERROR; |
| 480 | goto finish; |
| 481 | } |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 482 | |
| 483 | // Traverse the UUID |
Patrick Venture | d211702 | 2018-02-06 08:54:37 -0800 | [diff] [blame] | 484 | // Get the UUID octects separated by dash |
| 485 | id_octet = strtok_r(uuid, "-", &tokptr); |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 486 | |
| 487 | if (id_octet == NULL) |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 488 | { |
| 489 | // Error |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 490 | log<level::ERR>("Unexpected UUID format", entry("UUID=%s", uuid)); |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 491 | rc = IPMI_CC_RESPONSE_ERROR; |
| 492 | goto finish; |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | while (id_octet != NULL) |
| 496 | { |
| 497 | // Calculate the octet string size since it varies |
| 498 | // Divide it by 2 for the array size since 1 byte is built from 2 chars |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 499 | int tmp_size = strlen(id_octet) / 2; |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 500 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 501 | for (i = 0; i < tmp_size; i++) |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 502 | { |
Patrick Venture | d211702 | 2018-02-06 08:54:37 -0800 | [diff] [blame] | 503 | // Holder of the 2 chars that will become a byte |
| 504 | char tmp_array[3] = {0}; |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 505 | strncpy(tmp_array, id_octet, 2); // 2 chars at a time |
| 506 | |
| 507 | int resp_byte = strtoul(tmp_array, NULL, 16); // Convert to hex byte |
Patrick Venture | d211702 | 2018-02-06 08:54:37 -0800 | [diff] [blame] | 508 | // Copy end to first |
| 509 | memcpy((void*)&resp_uuid[resp_loc], &resp_byte, 1); |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 510 | resp_loc--; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 511 | id_octet += 2; // Finished with the 2 chars, advance |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 512 | } |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 513 | id_octet = strtok_r(NULL, "-", &tokptr); // Get next octet |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | // Data length |
| 517 | *data_len = resp_size; |
| 518 | |
| 519 | // Pack the actual response |
| 520 | memcpy(response, &resp_uuid, *data_len); |
| 521 | |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 522 | finish: |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 523 | sd_bus_error_free(&error); |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 524 | reply = sd_bus_message_unref(reply); |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 525 | free(busname); |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 526 | |
| 527 | return rc; |
| 528 | } |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 529 | |
Adriana Kobylak | 3a552e1 | 2015-10-19 16:11:00 -0500 | [diff] [blame] | 530 | ipmi_ret_t ipmi_app_get_bt_capabilities(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 531 | ipmi_request_t request, |
| 532 | ipmi_response_t response, |
| 533 | ipmi_data_len_t data_len, |
| 534 | ipmi_context_t context) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 535 | { |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 536 | |
| 537 | // Status code. |
| 538 | ipmi_ret_t rc = IPMI_CC_OK; |
| 539 | |
Adriana Kobylak | 88ad815 | 2016-12-13 10:09:08 -0600 | [diff] [blame] | 540 | // Per IPMI 2.0 spec, the input and output buffer size must be the max |
| 541 | // buffer size minus one byte to allocate space for the length byte. |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 542 | uint8_t str[] = {0x01, MAX_IPMI_BUFFER - 1, MAX_IPMI_BUFFER - 1, 0x0A, |
| 543 | 0x01}; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 544 | |
| 545 | // Data length |
| 546 | *data_len = sizeof(str); |
| 547 | |
| 548 | // Pack the actual response |
| 549 | memcpy(response, &str, *data_len); |
| 550 | |
| 551 | return rc; |
| 552 | } |
| 553 | |
Adriana Kobylak | 3a552e1 | 2015-10-19 16:11:00 -0500 | [diff] [blame] | 554 | ipmi_ret_t ipmi_app_wildcard_handler(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 555 | ipmi_request_t request, |
| 556 | ipmi_response_t response, |
| 557 | ipmi_data_len_t data_len, |
| 558 | ipmi_context_t context) |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 559 | { |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 560 | // Status code. |
Nan Li | 70aa8d9 | 2016-08-29 00:11:10 +0800 | [diff] [blame] | 561 | ipmi_ret_t rc = IPMI_CC_INVALID; |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 562 | |
| 563 | *data_len = strlen("THIS IS WILDCARD"); |
| 564 | |
| 565 | // Now pack actual response |
| 566 | memcpy(response, "THIS IS WILDCARD", *data_len); |
| 567 | |
| 568 | return rc; |
| 569 | } |
| 570 | |
Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 571 | ipmi_ret_t ipmi_app_get_sys_guid(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 572 | ipmi_request_t request, |
| 573 | ipmi_response_t response, |
| 574 | ipmi_data_len_t data_len, |
| 575 | ipmi_context_t context) |
Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 576 | |
| 577 | { |
| 578 | ipmi_ret_t rc = IPMI_CC_OK; |
| 579 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 580 | |
| 581 | try |
| 582 | { |
| 583 | // Get the Inventory object implementing BMC interface |
| 584 | ipmi::DbusObjectInfo bmcObject = |
| 585 | ipmi::getDbusObject(bus, bmc_interface); |
| 586 | |
| 587 | // Read UUID property value from bmcObject |
| 588 | // UUID is in RFC4122 format Ex: 61a39523-78f2-11e5-9862-e6402cfc3223 |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 589 | auto variant = |
| 590 | ipmi::getDbusProperty(bus, bmcObject.second, bmcObject.first, |
| 591 | bmc_guid_interface, bmc_guid_property); |
Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 592 | std::string guidProp = variant.get<std::string>(); |
| 593 | |
| 594 | // Erase "-" characters from the property value |
| 595 | guidProp.erase(std::remove(guidProp.begin(), guidProp.end(), '-'), |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 596 | guidProp.end()); |
Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 597 | |
| 598 | auto guidPropLen = guidProp.length(); |
| 599 | // Validate UUID data |
| 600 | // Divide by 2 as 1 byte is built from 2 chars |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 601 | if ((guidPropLen <= 0) || ((guidPropLen / 2) != bmc_guid_len)) |
Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 602 | |
| 603 | { |
| 604 | log<level::ERR>("Invalid UUID property value", |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 605 | entry("UUID_LENGTH=%d", guidPropLen)); |
Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 606 | return IPMI_CC_RESPONSE_ERROR; |
| 607 | } |
| 608 | |
| 609 | // Convert data in RFC4122(MSB) format to LSB format |
| 610 | // Get 2 characters at a time as 1 byte is built from 2 chars and |
| 611 | // convert to hex byte |
| 612 | // TODO: Data printed for GUID command is not as per the |
| 613 | // GUID format defined in IPMI specification 2.0 section 20.8 |
| 614 | // Ticket raised: https://sourceforge.net/p/ipmitool/bugs/501/ |
| 615 | uint8_t respGuid[bmc_guid_len]; |
| 616 | for (size_t i = 0, respLoc = (bmc_guid_len - 1); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 617 | i < guidPropLen && respLoc >= 0; i += 2, respLoc--) |
Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 618 | { |
| 619 | auto value = static_cast<uint8_t>( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 620 | std::stoi(guidProp.substr(i, 2).c_str(), NULL, 16)); |
Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 621 | respGuid[respLoc] = value; |
| 622 | } |
| 623 | |
| 624 | *data_len = bmc_guid_len; |
| 625 | memcpy(response, &respGuid, bmc_guid_len); |
| 626 | } |
| 627 | catch (const InternalFailure& e) |
| 628 | { |
| 629 | log<level::ERR>("Failed in reading BMC UUID property", |
| 630 | entry("INTERFACE=%s", bmc_interface), |
| 631 | entry("PROPERTY_INTERFACE=%s", bmc_guid_interface), |
| 632 | entry("PROPERTY=%s", bmc_guid_property)); |
| 633 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 634 | } |
| 635 | return rc; |
| 636 | } |
| 637 | |
Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 638 | static std::unique_ptr<SysInfoParamStore> sysInfoParamStore; |
| 639 | |
Xo Wang | 8765133 | 2017-08-11 10:17:59 -0700 | [diff] [blame^] | 640 | static std::string sysInfoReadSystemName() |
| 641 | { |
| 642 | // Use the BMC hostname as the "System Name." |
| 643 | char hostname[HOST_NAME_MAX + 1] = {}; |
| 644 | if (gethostname(hostname, HOST_NAME_MAX) != 0) |
| 645 | { |
| 646 | perror("System info parameter: system name"); |
| 647 | } |
| 648 | return hostname; |
| 649 | } |
| 650 | |
Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 651 | struct IpmiSysInfoResp |
| 652 | { |
| 653 | uint8_t paramRevision; |
| 654 | uint8_t setSelector; |
| 655 | union |
| 656 | { |
| 657 | struct |
| 658 | { |
| 659 | uint8_t encoding; |
| 660 | uint8_t stringLen; |
| 661 | uint8_t stringData0[14]; |
| 662 | } __attribute__((packed)); |
| 663 | uint8_t stringDataN[16]; |
| 664 | uint8_t byteData; |
| 665 | }; |
| 666 | } __attribute__((packed)); |
| 667 | |
| 668 | /** |
| 669 | * Split a string into (up to) 16-byte chunks as expected in response for get |
| 670 | * system info parameter. |
| 671 | * |
| 672 | * @param[in] fullString: Input string to be split |
| 673 | * @param[in] chunkIndex: Index of the chunk to be written out |
| 674 | * @param[in,out] chunk: Output data buffer; must have 14 byte capacity if |
| 675 | * chunk_index = 0 and 16-byte capacity otherwise |
| 676 | * @return the number of bytes written into the output buffer, or -EINVAL for |
| 677 | * invalid arguments. |
| 678 | */ |
| 679 | static int splitStringParam(const std::string& fullString, int chunkIndex, |
| 680 | uint8_t* chunk) |
| 681 | { |
| 682 | constexpr int maxChunk = 255; |
| 683 | constexpr int smallChunk = 14; |
| 684 | constexpr int chunkSize = 16; |
| 685 | if (chunkIndex > maxChunk || chunk == nullptr) |
| 686 | { |
| 687 | return -EINVAL; |
| 688 | } |
| 689 | try |
| 690 | { |
| 691 | std::string output; |
| 692 | if (chunkIndex == 0) |
| 693 | { |
| 694 | // Output must have 14 byte capacity. |
| 695 | output = fullString.substr(0, smallChunk); |
| 696 | } |
| 697 | else |
| 698 | { |
| 699 | // Output must have 16 byte capacity. |
| 700 | output = fullString.substr((chunkIndex * chunkSize) - 2, chunkSize); |
| 701 | } |
| 702 | |
| 703 | std::memcpy(chunk, output.c_str(), output.length()); |
| 704 | return output.length(); |
| 705 | } |
| 706 | catch (const std::out_of_range& e) |
| 707 | { |
| 708 | // The position was beyond the end. |
| 709 | return -EINVAL; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * Packs the Get Sys Info Request Item into the response. |
| 715 | * |
| 716 | * @param[in] paramString - the parameter. |
| 717 | * @param[in] setSelector - the selector |
| 718 | * @param[in,out] resp - the System info response. |
| 719 | * @return The number of bytes packed or failure from splitStringParam(). |
| 720 | */ |
| 721 | static int packGetSysInfoResp(const std::string& paramString, |
| 722 | uint8_t setSelector, IpmiSysInfoResp* resp) |
| 723 | { |
| 724 | uint8_t* dataBuffer = resp->stringDataN; |
| 725 | resp->setSelector = setSelector; |
| 726 | if (resp->setSelector == 0) // First chunk has only 14 bytes. |
| 727 | { |
| 728 | resp->encoding = 0; |
| 729 | resp->stringLen = paramString.length(); |
| 730 | dataBuffer = resp->stringData0; |
| 731 | } |
| 732 | return splitStringParam(paramString, resp->setSelector, dataBuffer); |
| 733 | } |
| 734 | |
| 735 | ipmi_ret_t ipmi_app_get_system_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 736 | ipmi_request_t request, |
| 737 | ipmi_response_t response, |
| 738 | ipmi_data_len_t dataLen, |
| 739 | ipmi_context_t context) |
| 740 | { |
| 741 | IpmiSysInfoResp resp = {}; |
| 742 | size_t respLen = 0; |
| 743 | uint8_t* const reqData = static_cast<uint8_t*>(request); |
| 744 | std::string paramString; |
| 745 | bool found; |
| 746 | std::tuple<bool, std::string> ret; |
| 747 | constexpr int minRequestSize = 4; |
| 748 | constexpr int paramSelector = 1; |
| 749 | constexpr uint8_t revisionOnly = 0x80; |
| 750 | const uint8_t paramRequested = reqData[paramSelector]; |
| 751 | int rc; |
| 752 | |
| 753 | if (*dataLen < minRequestSize) |
| 754 | { |
| 755 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 756 | } |
| 757 | |
| 758 | *dataLen = 0; // default to 0. |
| 759 | |
| 760 | // Parameters revision as of IPMI spec v2.0 rev. 1.1 (Feb 11, 2014 E6) |
| 761 | resp.paramRevision = 0x11; |
| 762 | if (reqData[0] & revisionOnly) // Get parameter revision only |
| 763 | { |
| 764 | respLen = 1; |
| 765 | goto writeResponse; |
| 766 | } |
| 767 | |
| 768 | // The "Set In Progress" parameter can be used for rollback of parameter |
| 769 | // data and is not implemented. |
| 770 | if (paramRequested == 0) |
| 771 | { |
| 772 | resp.byteData = 0; |
| 773 | respLen = 2; |
| 774 | goto writeResponse; |
| 775 | } |
| 776 | |
| 777 | if (sysInfoParamStore == nullptr) |
| 778 | { |
| 779 | sysInfoParamStore = std::make_unique<SysInfoParamStore>(); |
Xo Wang | 8765133 | 2017-08-11 10:17:59 -0700 | [diff] [blame^] | 780 | sysInfoParamStore->update(IPMI_SYSINFO_SYSTEM_NAME, |
| 781 | sysInfoReadSystemName); |
Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | // Parameters other than Set In Progress are assumed to be strings. |
| 785 | ret = sysInfoParamStore->lookup(paramRequested); |
| 786 | found = std::get<0>(ret); |
| 787 | paramString = std::get<1>(ret); |
| 788 | if (!found) |
| 789 | { |
| 790 | return IPMI_CC_SYSTEM_INFO_PARAMETER_NOT_SUPPORTED; |
| 791 | } |
| 792 | // TODO: Cache each parameter across multiple calls, until the whole string |
| 793 | // has been read out. Otherwise, it's possible for a parameter to change |
| 794 | // between requests for its chunks, returning chunks incoherent with each |
| 795 | // other. For now, the parameter store is simply required to have only |
| 796 | // idempotent callbacks. |
| 797 | rc = packGetSysInfoResp(paramString, reqData[2], &resp); |
| 798 | if (rc == -EINVAL) |
| 799 | { |
| 800 | return IPMI_CC_RESPONSE_ERROR; |
| 801 | } |
| 802 | |
| 803 | respLen = sizeof(resp); // Write entire string data chunk in response. |
| 804 | |
| 805 | writeResponse: |
| 806 | std::memcpy(response, &resp, sizeof(resp)); |
| 807 | *dataLen = respLen; |
| 808 | return IPMI_CC_OK; |
| 809 | } |
| 810 | |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 811 | void register_netfn_app_functions() |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 812 | { |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 813 | // <Get BT Interface Capabilities> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 814 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CAP_BIT, NULL, |
| 815 | ipmi_app_get_bt_capabilities, PRIVILEGE_USER); |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 816 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 817 | // <Wildcard Command> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 818 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_WILDCARD, NULL, |
| 819 | ipmi_app_wildcard_handler, PRIVILEGE_USER); |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 820 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 821 | // <Reset Watchdog Timer> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 822 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_RESET_WD, NULL, |
| 823 | ipmi_app_watchdog_reset, PRIVILEGE_OPERATOR); |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 824 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 825 | // <Set Watchdog Timer> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 826 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_WD, NULL, |
| 827 | ipmi_app_watchdog_set, PRIVILEGE_OPERATOR); |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 828 | |
William A. Kennington III | 73f4451 | 2018-02-09 15:28:46 -0800 | [diff] [blame] | 829 | // <Get Watchdog Timer> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 830 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_WD, NULL, |
| 831 | ipmi_app_watchdog_get, PRIVILEGE_OPERATOR); |
William A. Kennington III | 73f4451 | 2018-02-09 15:28:46 -0800 | [diff] [blame] | 832 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 833 | // <Get Device ID> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 834 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_ID, NULL, |
| 835 | ipmi_app_get_device_id, PRIVILEGE_USER); |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 836 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 837 | // <Get Self Test Results> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 838 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_SELF_TEST_RESULTS, NULL, |
| 839 | ipmi_app_get_self_test_results, PRIVILEGE_USER); |
Nan Li | 41fa24a | 2016-11-10 20:12:37 +0800 | [diff] [blame] | 840 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 841 | // <Get Device GUID> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 842 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_DEVICE_GUID, NULL, |
| 843 | ipmi_app_get_device_guid, PRIVILEGE_USER); |
Adriana Kobylak | d100ee5 | 2015-10-20 17:02:37 -0500 | [diff] [blame] | 844 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 845 | // <Set ACPI Power State> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 846 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_ACPI, NULL, |
| 847 | ipmi_app_set_acpi_power_state, PRIVILEGE_ADMIN); |
Chris Austen | 6caf28b | 2015-10-13 12:40:40 -0500 | [diff] [blame] | 848 | |
Tom Joseph | 69fabfe | 2017-08-04 10:15:01 +0530 | [diff] [blame] | 849 | // <Get Channel Access> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 850 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CHANNEL_ACCESS, NULL, |
| 851 | ipmi_get_channel_access, PRIVILEGE_USER); |
Tom Joseph | 69fabfe | 2017-08-04 10:15:01 +0530 | [diff] [blame] | 852 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 853 | // <Get Channel Info Command> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 854 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CHAN_INFO, NULL, |
| 855 | ipmi_app_channel_info, PRIVILEGE_USER); |
Chris Austen | c2cd29d | 2016-02-05 20:02:29 -0600 | [diff] [blame] | 856 | |
Marri Devender Rao | 5e007a5 | 2018-01-08 06:18:36 -0600 | [diff] [blame] | 857 | // <Get System GUID Command> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 858 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_SYS_GUID, NULL, |
| 859 | ipmi_app_get_sys_guid, PRIVILEGE_USER); |
Tom Joseph | 7cbe228 | 2018-03-21 21:17:33 +0530 | [diff] [blame] | 860 | |
| 861 | // <Get Channel Cipher Suites Command> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 862 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CHAN_CIPHER_SUITES, NULL, |
| 863 | getChannelCipherSuites, PRIVILEGE_CALLBACK); |
Tom Joseph | 1322768 | 2018-08-10 01:05:21 +0530 | [diff] [blame] | 864 | |
| 865 | // <Set Channel Access Command> |
| 866 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_CHAN_ACCESS, NULL, |
| 867 | ipmi_set_channel_access, PRIVILEGE_ADMIN); |
Xo Wang | f542e8b | 2017-08-09 15:34:16 -0700 | [diff] [blame] | 868 | |
| 869 | // <Get System Info Command> |
| 870 | ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_SYSTEM_INFO, NULL, |
| 871 | ipmi_app_get_system_info, PRIVILEGE_USER); |
vishwabmc | ba0bd5f | 2015-09-30 16:50:23 +0530 | [diff] [blame] | 872 | return; |
| 873 | } |