NodeMan97 | 27d1e14 | 2022-07-27 15:10:07 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 3 | #include "utils.hpp" |
| 4 | |
Andrew Geissler | f8ae6a0 | 2022-01-21 17:00:20 -0600 | [diff] [blame] | 5 | #include <gpiod.h> |
| 6 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 7 | #include <phosphor-logging/lg2.hpp> |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 8 | #include <xyz/openbmc_project/Dump/Create/client.hpp> |
| 9 | #include <xyz/openbmc_project/Logging/Create/client.hpp> |
| 10 | #include <xyz/openbmc_project/ObjectMapper/client.hpp> |
| 11 | #include <xyz/openbmc_project/State/BMC/client.hpp> |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 12 | |
Andrew Geissler | 48a4e5e | 2023-02-15 18:23:34 -0600 | [diff] [blame] | 13 | #include <chrono> |
NodeMan97 | 27d1e14 | 2022-07-27 15:10:07 -0500 | [diff] [blame] | 14 | #include <filesystem> |
Patrick Williams | 78c066f | 2024-02-13 12:25:58 -0600 | [diff] [blame] | 15 | #include <format> |
NodeMan97 | 27d1e14 | 2022-07-27 15:10:07 -0500 | [diff] [blame] | 16 | |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 17 | namespace phosphor |
| 18 | { |
| 19 | namespace state |
| 20 | { |
| 21 | namespace manager |
| 22 | { |
| 23 | namespace utils |
| 24 | { |
| 25 | |
Andrew Geissler | 48a4e5e | 2023-02-15 18:23:34 -0600 | [diff] [blame] | 26 | using namespace std::literals::chrono_literals; |
| 27 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 28 | PHOSPHOR_LOG2_USING; |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 29 | |
Andrew Geissler | 928bbf1 | 2023-02-14 13:30:14 -0600 | [diff] [blame] | 30 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 31 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
| 32 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 33 | constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
| 34 | |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 35 | using ObjectMapper = sdbusplus::client::xyz::openbmc_project::ObjectMapper<>; |
| 36 | |
Patrick Williams | 5c4a082 | 2023-02-28 02:40:35 -0600 | [diff] [blame] | 37 | void subscribeToSystemdSignals(sdbusplus::bus_t& bus) |
Andrew Geissler | 928bbf1 | 2023-02-14 13:30:14 -0600 | [diff] [blame] | 38 | { |
| 39 | auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 40 | SYSTEMD_INTERFACE, "Subscribe"); |
| 41 | |
| 42 | try |
| 43 | { |
Andrew Geissler | 48a4e5e | 2023-02-15 18:23:34 -0600 | [diff] [blame] | 44 | // On OpenBMC based systems, systemd has had a few situations where it |
| 45 | // has been unable to respond to this call within the default d-bus |
| 46 | // timeout of 25 seconds. This is due to the large amount of work being |
| 47 | // done by systemd during OpenBMC startup. Set the timeout for this call |
| 48 | // to 60 seconds (worst case seen was around 30s so double it). |
| 49 | bus.call(method, 60s); |
Andrew Geissler | 928bbf1 | 2023-02-14 13:30:14 -0600 | [diff] [blame] | 50 | } |
| 51 | catch (const sdbusplus::exception_t& e) |
| 52 | { |
| 53 | error("Failed to subscribe to systemd signals: {ERROR}", "ERROR", e); |
| 54 | throw std::runtime_error("Unable to subscribe to systemd signals"); |
| 55 | } |
| 56 | return; |
| 57 | } |
| 58 | |
Patrick Williams | 5c4a082 | 2023-02-28 02:40:35 -0600 | [diff] [blame] | 59 | std::string getService(sdbusplus::bus_t& bus, std::string path, |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 60 | std::string interface) |
| 61 | { |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 62 | auto mapper = bus.new_method_call(ObjectMapper::default_service, |
| 63 | ObjectMapper::instance_path, |
| 64 | ObjectMapper::interface, "GetObject"); |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 65 | |
| 66 | mapper.append(path, std::vector<std::string>({interface})); |
| 67 | |
| 68 | std::vector<std::pair<std::string, std::vector<std::string>>> |
| 69 | mapperResponse; |
| 70 | |
| 71 | try |
| 72 | { |
| 73 | auto mapperResponseMsg = bus.call(mapper); |
| 74 | |
| 75 | mapperResponseMsg.read(mapperResponse); |
| 76 | if (mapperResponse.empty()) |
| 77 | { |
Andrew Geissler | ad65b2d | 2021-09-21 12:53:29 -0500 | [diff] [blame] | 78 | error( |
| 79 | "Error no matching service with path {PATH} and interface {INTERFACE}", |
| 80 | "PATH", path, "INTERFACE", interface); |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 81 | throw std::runtime_error("Error no matching service"); |
| 82 | } |
| 83 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 84 | catch (const sdbusplus::exception_t& e) |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 85 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 86 | error("Error in mapper call with path {PATH}, interface " |
| 87 | "{INTERFACE}, and exception {ERROR}", |
| 88 | "PATH", path, "INTERFACE", interface, "ERROR", e); |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 89 | throw; |
| 90 | } |
| 91 | |
| 92 | return mapperResponse.begin()->first; |
| 93 | } |
| 94 | |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 95 | std::string getProperty(sdbusplus::bus_t& bus, const std::string& path, |
Andrew Geissler | 49e6713 | 2022-01-26 14:27:52 -0600 | [diff] [blame] | 96 | const std::string& interface, |
| 97 | const std::string& propertyName) |
| 98 | { |
| 99 | std::variant<std::string> property; |
| 100 | std::string service = getService(bus, path, interface); |
| 101 | |
| 102 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 103 | PROPERTY_INTERFACE, "Get"); |
| 104 | |
| 105 | method.append(interface, propertyName); |
| 106 | |
| 107 | try |
| 108 | { |
| 109 | auto reply = bus.call(method); |
| 110 | reply.read(property); |
| 111 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 112 | catch (const sdbusplus::exception_t& e) |
Andrew Geissler | 49e6713 | 2022-01-26 14:27:52 -0600 | [diff] [blame] | 113 | { |
| 114 | error("Error in property Get, error {ERROR}, property {PROPERTY}", |
| 115 | "ERROR", e, "PROPERTY", propertyName); |
| 116 | throw; |
| 117 | } |
| 118 | |
| 119 | if (std::get<std::string>(property).empty()) |
| 120 | { |
| 121 | error("Error reading property response for {PROPERTY}", "PROPERTY", |
| 122 | propertyName); |
| 123 | throw std::runtime_error("Error reading property response"); |
| 124 | } |
| 125 | |
| 126 | return std::get<std::string>(property); |
| 127 | } |
| 128 | |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 129 | void setProperty(sdbusplus::bus_t& bus, const std::string& path, |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 130 | const std::string& interface, const std::string& property, |
| 131 | const std::string& value) |
| 132 | { |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 133 | std::variant<std::string> variantValue = value; |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 134 | std::string service = getService(bus, path, interface); |
| 135 | |
| 136 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 137 | PROPERTY_INTERFACE, "Set"); |
| 138 | |
| 139 | method.append(interface, property, variantValue); |
| 140 | bus.call_noreply(method); |
| 141 | |
| 142 | return; |
| 143 | } |
| 144 | |
Andrew Geissler | f8ae6a0 | 2022-01-21 17:00:20 -0600 | [diff] [blame] | 145 | int getGpioValue(const std::string& gpioName) |
| 146 | { |
Andrew Geissler | f8ae6a0 | 2022-01-21 17:00:20 -0600 | [diff] [blame] | 147 | int gpioval = -1; |
| 148 | gpiod_line* line = gpiod_line_find(gpioName.c_str()); |
| 149 | |
| 150 | if (nullptr != line) |
| 151 | { |
| 152 | // take ownership of gpio |
| 153 | if (0 != gpiod_line_request_input(line, "state-manager")) |
| 154 | { |
| 155 | error("Failed request for {GPIO_NAME} GPIO", "GPIO_NAME", gpioName); |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | // get gpio value |
| 160 | gpioval = gpiod_line_get_value(line); |
| 161 | |
| 162 | // release ownership of gpio |
| 163 | gpiod_line_close_chip(line); |
| 164 | } |
| 165 | } |
| 166 | return gpioval; |
| 167 | } |
| 168 | |
Andrew Geissler | 9d4d0c9 | 2022-01-26 13:18:12 -0600 | [diff] [blame] | 169 | void createError( |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 170 | sdbusplus::bus_t& bus, const std::string& errorMsg, |
Patrick Williams | 7e969cb | 2023-08-23 16:24:23 -0500 | [diff] [blame] | 171 | sdbusplus::server::xyz::openbmc_project::logging::Entry::Level errLevel, |
Andrew Geissler | d49f51e | 2022-03-07 14:57:07 -0600 | [diff] [blame] | 172 | std::map<std::string, std::string> additionalData) |
Andrew Geissler | 9d4d0c9 | 2022-01-26 13:18:12 -0600 | [diff] [blame] | 173 | { |
Andrew Geissler | 9d4d0c9 | 2022-01-26 13:18:12 -0600 | [diff] [blame] | 174 | try |
| 175 | { |
Andrew Geissler | d49f51e | 2022-03-07 14:57:07 -0600 | [diff] [blame] | 176 | // Always add the _PID on for some extra logging debug |
Andrew Geissler | 9d4d0c9 | 2022-01-26 13:18:12 -0600 | [diff] [blame] | 177 | additionalData.emplace("_PID", std::to_string(getpid())); |
| 178 | |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 179 | using LoggingCreate = |
| 180 | sdbusplus::client::xyz::openbmc_project::logging::Create<>; |
| 181 | |
| 182 | auto method = bus.new_method_call(LoggingCreate::default_service, |
| 183 | LoggingCreate::instance_path, |
| 184 | LoggingCreate::interface, "Create"); |
Andrew Geissler | 9d4d0c9 | 2022-01-26 13:18:12 -0600 | [diff] [blame] | 185 | |
| 186 | method.append(errorMsg, errLevel, additionalData); |
| 187 | auto resp = bus.call(method); |
| 188 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 189 | catch (const sdbusplus::exception_t& e) |
Andrew Geissler | 9d4d0c9 | 2022-01-26 13:18:12 -0600 | [diff] [blame] | 190 | { |
| 191 | error("sdbusplus D-Bus call exception, error {ERROR} trying to create " |
| 192 | "an error with {ERROR_MSG}", |
| 193 | "ERROR", e, "ERROR_MSG", errorMsg); |
| 194 | |
| 195 | throw std::runtime_error( |
| 196 | "Error in invoking D-Bus logging create interface"); |
| 197 | } |
| 198 | catch (const std::exception& e) |
| 199 | { |
| 200 | error("D-bus call exception: {ERROR}", "ERROR", e); |
| 201 | throw e; |
| 202 | } |
| 203 | } |
| 204 | |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 205 | void createBmcDump(sdbusplus::bus_t& bus) |
Andrew Geissler | 55e96ac | 2022-04-19 11:44:53 -0400 | [diff] [blame] | 206 | { |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 207 | using DumpCreate = sdbusplus::client::xyz::openbmc_project::dump::Create<>; |
| 208 | auto dumpPath = |
| 209 | sdbusplus::message::object_path(DumpCreate::namespace_path::value) / |
| 210 | DumpCreate::namespace_path::bmc; |
| 211 | |
Patrick Williams | 1b2c3c0 | 2024-08-16 15:20:29 -0400 | [diff] [blame] | 212 | auto method = |
| 213 | bus.new_method_call(DumpCreate::default_service, dumpPath.str.c_str(), |
| 214 | DumpCreate::interface, "CreateDump"); |
Andrew Geissler | 55e96ac | 2022-04-19 11:44:53 -0400 | [diff] [blame] | 215 | method.append( |
| 216 | std::vector< |
| 217 | std::pair<std::string, std::variant<std::string, uint64_t>>>()); |
| 218 | try |
| 219 | { |
| 220 | bus.call_noreply(method); |
| 221 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 222 | catch (const sdbusplus::exception_t& e) |
Andrew Geissler | 55e96ac | 2022-04-19 11:44:53 -0400 | [diff] [blame] | 223 | { |
| 224 | error("Failed to create BMC dump, exception:{ERROR}", "ERROR", e); |
| 225 | // just continue, this is error path anyway so we're just collecting |
| 226 | // what we can |
| 227 | } |
| 228 | } |
| 229 | |
NodeMan97 | 27d1e14 | 2022-07-27 15:10:07 -0500 | [diff] [blame] | 230 | bool checkACLoss(size_t& chassisId) |
| 231 | { |
Patrick Williams | 1b2c3c0 | 2024-08-16 15:20:29 -0400 | [diff] [blame] | 232 | std::string chassisLostPowerFileFmt = |
| 233 | std::format(CHASSIS_LOST_POWER_FILE, chassisId); |
NodeMan97 | 27d1e14 | 2022-07-27 15:10:07 -0500 | [diff] [blame] | 234 | |
| 235 | std::filesystem::path chassisPowerLossFile{chassisLostPowerFileFmt}; |
Pavithra Barithaya | 44bbf11 | 2024-06-21 11:43:30 -0500 | [diff] [blame] | 236 | return std::filesystem::exists(chassisPowerLossFile); |
NodeMan97 | 27d1e14 | 2022-07-27 15:10:07 -0500 | [diff] [blame] | 237 | } |
| 238 | |
Andrew Geissler | fc1020f | 2023-05-24 17:07:38 -0400 | [diff] [blame] | 239 | bool isBmcReady(sdbusplus::bus_t& bus) |
| 240 | { |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 241 | using BMC = sdbusplus::client::xyz::openbmc_project::state::BMC<>; |
| 242 | auto bmcPath = sdbusplus::message::object_path(BMC::namespace_path::value) / |
| 243 | BMC::namespace_path::bmc; |
| 244 | |
Patrick Williams | 1b2c3c0 | 2024-08-16 15:20:29 -0400 | [diff] [blame] | 245 | auto bmcState = |
| 246 | getProperty(bus, bmcPath.str, BMC::interface, "CurrentBMCState"); |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 247 | |
| 248 | if (sdbusplus::message::convert_from_string<BMC::BMCState>(bmcState) != |
| 249 | BMC::BMCState::Ready) |
Andrew Geissler | fc1020f | 2023-05-24 17:07:38 -0400 | [diff] [blame] | 250 | { |
| 251 | debug("BMC State is {BMC_STATE}", "BMC_STATE", bmcState); |
| 252 | return false; |
| 253 | } |
| 254 | return true; |
| 255 | } |
| 256 | |
Potin Lai | 0886545 | 2023-11-07 23:28:11 +0800 | [diff] [blame] | 257 | bool waitBmcReady(sdbusplus::bus_t& bus, std::chrono::seconds timeout) |
| 258 | { |
| 259 | while (timeout.count() != 0) |
| 260 | { |
| 261 | timeout--; |
| 262 | if (isBmcReady(bus)) |
| 263 | { |
| 264 | return true; |
| 265 | } |
| 266 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 267 | } |
| 268 | return false; |
| 269 | } |
| 270 | |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 271 | } // namespace utils |
| 272 | } // namespace manager |
| 273 | } // namespace state |
| 274 | } // namespace phosphor |