AppaRao Puli | e63eeda | 2019-07-05 16:25:38 +0530 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2019 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | |
AppaRao Puli | 67d184c | 2020-05-29 00:48:33 +0530 | [diff] [blame] | 17 | #include "pfr.hpp" |
| 18 | #include "pfr_mgr.hpp" |
| 19 | |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 20 | #include <systemd/sd-journal.h> |
| 21 | |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 22 | #include <boost/asio.hpp> |
AppaRao Puli | e63eeda | 2019-07-05 16:25:38 +0530 | [diff] [blame] | 23 | |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame^] | 24 | namespace pfr |
| 25 | { |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 26 | // Caches the last Recovery/Panic Count to |
| 27 | // identify any new Recovery/panic actions. |
| 28 | /* TODO: When BMC Reset's, these values will be lost |
| 29 | * Persist this info using settingsd */ |
| 30 | static uint8_t lastRecoveryCount = 0; |
| 31 | static uint8_t lastPanicCount = 0; |
| 32 | static uint8_t lastMajorErr = 0; |
| 33 | static uint8_t lastMinorErr = 0; |
| 34 | |
| 35 | static bool stateTimerRunning = false; |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 36 | bool finishedSettingChkPoint = false; |
| 37 | static constexpr uint8_t bmcBootFinishedChkPoint = 0x09; |
| 38 | |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 39 | std::unique_ptr<boost::asio::steady_timer> stateTimer = nullptr; |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 40 | std::unique_ptr<boost::asio::steady_timer> initTimer = nullptr; |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 41 | |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame^] | 42 | std::vector<std::unique_ptr<PfrVersion>> pfrVersionObjects; |
| 43 | std::unique_ptr<PfrConfig> pfrConfigObject; |
AppaRao Puli | e4e9565 | 2019-07-19 16:52:01 +0530 | [diff] [blame] | 44 | |
AppaRao Puli | e4e9565 | 2019-07-19 16:52:01 +0530 | [diff] [blame] | 45 | // List holds <ObjPath> <ImageType> <VersionPurpose> |
| 46 | static std::vector<std::tuple<std::string, ImageType, std::string>> |
| 47 | verComponentList = { |
| 48 | std::make_tuple("bmc_active", ImageType::bmcActive, versionPurposeBMC), |
| 49 | std::make_tuple("bmc_recovery", ImageType::bmcRecovery, |
| 50 | versionPurposeBMC), |
| 51 | std::make_tuple("bios_active", ImageType::biosActive, |
| 52 | versionPurposeHost), |
| 53 | std::make_tuple("bios_recovery", ImageType::biosRecovery, |
| 54 | versionPurposeHost), |
Vikram Bodireddy | 3c6c8c3 | 2019-12-05 11:06:15 +0530 | [diff] [blame] | 55 | std::make_tuple("cpld_active", ImageType::cpldActive, |
| 56 | versionPurposeOther), |
| 57 | std::make_tuple("cpld_recovery", ImageType::cpldRecovery, |
| 58 | versionPurposeOther), |
| 59 | }; |
AppaRao Puli | e4e9565 | 2019-07-19 16:52:01 +0530 | [diff] [blame] | 60 | |
AppaRao Puli | e90f128 | 2019-11-05 01:07:05 +0530 | [diff] [blame] | 61 | // Recovery reason map. |
| 62 | // {<CPLD association>,{<Redfish MessageID>, <Recovery Reason>}} |
| 63 | static const boost::container::flat_map<uint8_t, |
| 64 | std::pair<std::string, std::string>> |
| 65 | recoveryReasonMap = { |
| 66 | {0x01, |
| 67 | {"BIOSFirmwareRecoveryReason", |
Chalapathi | 3fb544b | 2020-02-14 15:43:49 +0000 | [diff] [blame] | 68 | "BIOS active image authentication failure"}}, |
AppaRao Puli | e90f128 | 2019-11-05 01:07:05 +0530 | [diff] [blame] | 69 | {0x02, |
| 70 | {"BIOSFirmwareRecoveryReason", |
Chalapathi | 3fb544b | 2020-02-14 15:43:49 +0000 | [diff] [blame] | 71 | "BIOS recovery image authentication failure"}}, |
AppaRao Puli | e90f128 | 2019-11-05 01:07:05 +0530 | [diff] [blame] | 72 | {0x03, {"MEFirmwareRecoveryReason", "ME launch failure"}}, |
| 73 | {0x04, {"BIOSFirmwareRecoveryReason", "ACM launch failure"}}, |
| 74 | {0x05, {"BIOSFirmwareRecoveryReason", "IBB launch failure"}}, |
| 75 | {0x06, {"BIOSFirmwareRecoveryReason", "OBB launch failure"}}, |
| 76 | {0x07, |
| 77 | {"BMCFirmwareRecoveryReason", |
| 78 | "BMC active image authentication failure"}}, |
| 79 | {0x08, |
| 80 | {"BMCFirmwareRecoveryReason", |
| 81 | "BMC recovery image authentication failure"}}, |
| 82 | {0x09, {"BMCFirmwareRecoveryReason", "BMC launch failure"}}, |
| 83 | {0x0A, {"CPLDFirmwareRecoveryReason", "CPLD watchdog expired"}}}; |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 84 | |
AppaRao Puli | e90f128 | 2019-11-05 01:07:05 +0530 | [diff] [blame] | 85 | // Panic Reason map. |
| 86 | // {<CPLD association>, {<Redfish MessageID>, <Panic reason> }) |
| 87 | static const boost::container::flat_map<uint8_t, |
| 88 | std::pair<std::string, std::string>> |
| 89 | panicReasonMap = { |
Chalapathi | 3fb544b | 2020-02-14 15:43:49 +0000 | [diff] [blame] | 90 | {0x01, {"BIOSFirmwarePanicReason", "BIOS update intent"}}, |
| 91 | {0x02, {"BMCFirmwarePanicReason", "BMC update intent"}}, |
| 92 | {0x03, {"BMCFirmwarePanicReason", "BMC reset detected"}}, |
| 93 | {0x04, {"BMCFirmwarePanicReason", "BMC watchdog expired"}}, |
| 94 | {0x05, {"MEFirmwarePanicReason", "ME watchdog expired"}}, |
| 95 | {0x06, {"BIOSFirmwarePanicReason", "ACM watchdog expired"}}, |
| 96 | {0x07, {"BIOSFirmwarePanicReason", "IBB watchdog expired"}}, |
| 97 | {0x08, {"BIOSFirmwarePanicReason", "OBB watchdog expired"}}, |
AppaRao Puli | e90f128 | 2019-11-05 01:07:05 +0530 | [diff] [blame] | 98 | {0x09, |
| 99 | {"BIOSFirmwarePanicReason", |
Chalapathi | 3fb544b | 2020-02-14 15:43:49 +0000 | [diff] [blame] | 100 | "ACM or IBB or OBB authentication failure"}}}; |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 101 | |
AppaRao Puli | 2476694 | 2019-11-13 19:27:08 +0530 | [diff] [blame] | 102 | // Firmware resiliency major map. |
| 103 | // {<CPLD association>, {<Redfish MessageID>, <Error reason> }) |
| 104 | static const boost::container::flat_map<uint8_t, |
| 105 | std::pair<std::string, std::string>> |
| 106 | majorErrorCodeMap = { |
| 107 | {0x01, |
| 108 | {"BMCFirmwareResiliencyError", "BMC image authentication failed"}}, |
| 109 | {0x02, |
| 110 | {"BIOSFirmwareResiliencyError", "BIOS image authentication failed"}}, |
Chalapathi | 3fb544b | 2020-02-14 15:43:49 +0000 | [diff] [blame] | 111 | {0x03, {"BIOSFirmwareResiliencyError", "Update from BIOS failed"}}, |
| 112 | {0x04, {"BMCFirmwareResiliencyError", "Update from BMC failed"}}}; |
AppaRao Puli | 2476694 | 2019-11-13 19:27:08 +0530 | [diff] [blame] | 113 | |
AppaRao Puli | e4e9565 | 2019-07-19 16:52:01 +0530 | [diff] [blame] | 114 | static void updateDbusPropertiesCache() |
| 115 | { |
| 116 | for (const auto& pfrVerObj : pfrVersionObjects) |
| 117 | { |
| 118 | pfrVerObj->updateVersion(); |
| 119 | } |
| 120 | |
| 121 | // Update provisoningStatus properties |
| 122 | pfrConfigObject->updateProvisioningStatus(); |
| 123 | |
| 124 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 125 | "PFR Manager service cache data updated."); |
| 126 | } |
| 127 | |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 128 | static void logLastRecoveryEvent() |
| 129 | { |
| 130 | uint8_t reason = 0; |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame^] | 131 | if (0 != readCpldReg(ActionType::recoveryReason, reason)) |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 132 | { |
| 133 | return; |
| 134 | } |
| 135 | |
AppaRao Puli | e90f128 | 2019-11-05 01:07:05 +0530 | [diff] [blame] | 136 | auto it = recoveryReasonMap.find(reason); |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 137 | if (it == recoveryReasonMap.end()) |
| 138 | { |
| 139 | // No matching found. So just return without logging event. |
| 140 | return; |
| 141 | } |
AppaRao Puli | e90f128 | 2019-11-05 01:07:05 +0530 | [diff] [blame] | 142 | std::string msgId = "OpenBMC.0.1." + it->second.first; |
| 143 | sd_journal_send("MESSAGE=%s", "Platform firmware recovery occurred.", |
| 144 | "PRIORITY=%i", LOG_WARNING, "REDFISH_MESSAGE_ID=%s", |
| 145 | msgId.c_str(), "REDFISH_MESSAGE_ARGS=%s", |
| 146 | it->second.second.c_str(), NULL); |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static void logLastPanicEvent() |
| 150 | { |
| 151 | uint8_t reason = 0; |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame^] | 152 | if (0 != readCpldReg(ActionType::panicReason, reason)) |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 153 | { |
| 154 | return; |
| 155 | } |
| 156 | |
AppaRao Puli | e90f128 | 2019-11-05 01:07:05 +0530 | [diff] [blame] | 157 | auto it = panicReasonMap.find(reason); |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 158 | if (it == panicReasonMap.end()) |
| 159 | { |
| 160 | // No matching found. So just return without logging event. |
| 161 | return; |
| 162 | } |
| 163 | |
AppaRao Puli | e90f128 | 2019-11-05 01:07:05 +0530 | [diff] [blame] | 164 | std::string msgId = "OpenBMC.0.1." + it->second.first; |
| 165 | sd_journal_send("MESSAGE=%s", "Platform firmware panic occurred.", |
| 166 | "PRIORITY=%i", LOG_WARNING, "REDFISH_MESSAGE_ID=%s", |
| 167 | msgId.c_str(), "REDFISH_MESSAGE_ARGS=%s", |
| 168 | it->second.second.c_str(), NULL); |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 169 | } |
| 170 | |
AppaRao Puli | 2476694 | 2019-11-13 19:27:08 +0530 | [diff] [blame] | 171 | static void logResiliencyErrorEvent(const uint8_t majorErrorCode, |
| 172 | const uint8_t minorErrorCode) |
| 173 | { |
| 174 | auto it = majorErrorCodeMap.find(majorErrorCode); |
| 175 | if (it == majorErrorCodeMap.end()) |
| 176 | { |
| 177 | // No matching found. So just return without logging event. |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | std::string errorStr = |
| 182 | it->second.second + "(MinorCode:0x" + toHexString(minorErrorCode) + ")"; |
| 183 | std::string msgId = "OpenBMC.0.1." + it->second.first; |
| 184 | sd_journal_send( |
| 185 | "MESSAGE=%s", "Platform firmware resiliency error occurred.", |
| 186 | "PRIORITY=%i", LOG_ERR, "REDFISH_MESSAGE_ID=%s", msgId.c_str(), |
| 187 | "REDFISH_MESSAGE_ARGS=%s", errorStr.c_str(), NULL); |
| 188 | } |
| 189 | |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 190 | static void checkAndLogEvents() |
| 191 | { |
| 192 | uint8_t currPanicCount = 0; |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame^] | 193 | if (0 == readCpldReg(ActionType::panicCount, currPanicCount)) |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 194 | { |
| 195 | if (lastPanicCount != currPanicCount) |
| 196 | { |
| 197 | // Update cached data and log redfish event by reading reason. |
| 198 | lastPanicCount = currPanicCount; |
| 199 | logLastPanicEvent(); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | uint8_t currRecoveryCount = 0; |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame^] | 204 | if (0 == readCpldReg(ActionType::recoveryCount, currRecoveryCount)) |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 205 | { |
| 206 | if (lastRecoveryCount != currRecoveryCount) |
| 207 | { |
| 208 | // Update cached data and log redfish event by reading reason. |
| 209 | lastRecoveryCount = currRecoveryCount; |
| 210 | logLastRecoveryEvent(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | uint8_t majorErr = 0; |
| 215 | uint8_t minorErr = 0; |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame^] | 216 | if ((0 == readCpldReg(ActionType::majorError, majorErr)) && |
| 217 | (0 == readCpldReg(ActionType::minorError, minorErr))) |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 218 | { |
| 219 | if ((lastMajorErr != majorErr) || (lastMinorErr != minorErr)) |
| 220 | { |
| 221 | lastMajorErr = majorErr; |
| 222 | lastMinorErr = minorErr; |
| 223 | |
AppaRao Puli | 2476694 | 2019-11-13 19:27:08 +0530 | [diff] [blame] | 224 | logResiliencyErrorEvent(majorErr, minorErr); |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | static void monitorPlatformStateChange( |
| 230 | sdbusplus::asio::object_server& server, |
| 231 | std::shared_ptr<sdbusplus::asio::connection>& conn) |
| 232 | { |
| 233 | constexpr size_t pollTimeout = 10; // seconds |
| 234 | stateTimer->expires_after(std::chrono::seconds(pollTimeout)); |
| 235 | stateTimer->async_wait( |
| 236 | [&server, &conn](const boost::system::error_code& ec) { |
| 237 | if (ec == boost::asio::error::operation_aborted) |
| 238 | { |
| 239 | // Timer reset. |
| 240 | return; |
| 241 | } |
| 242 | if (ec) |
| 243 | { |
| 244 | // Platform State Monitor - Timer cancelled. |
| 245 | return; |
| 246 | } |
| 247 | checkAndLogEvents(); |
| 248 | monitorPlatformStateChange(server, conn); |
| 249 | }); |
| 250 | } |
| 251 | |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 252 | void checkAndSetCheckpoint(sdbusplus::asio::object_server& server, |
| 253 | std::shared_ptr<sdbusplus::asio::connection>& conn) |
| 254 | { |
| 255 | // Check whether systemd completed all the loading. |
| 256 | conn->async_method_call( |
| 257 | [&server, &conn](boost::system::error_code ec, |
| 258 | const std::variant<uint64_t>& value) { |
AppaRao Puli | b7e172c | 2019-12-13 14:46:25 +0530 | [diff] [blame] | 259 | if (!ec) |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 260 | { |
AppaRao Puli | b7e172c | 2019-12-13 14:46:25 +0530 | [diff] [blame] | 261 | if (std::get<uint64_t>(value)) |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 262 | { |
AppaRao Puli | b7e172c | 2019-12-13 14:46:25 +0530 | [diff] [blame] | 263 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 264 | "PFR: BMC boot completed. Setting checkpoint 9."); |
| 265 | if (!finishedSettingChkPoint) |
| 266 | { |
| 267 | finishedSettingChkPoint = true; |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame^] | 268 | setBMCBootCheckpoint(bmcBootFinishedChkPoint); |
AppaRao Puli | b7e172c | 2019-12-13 14:46:25 +0530 | [diff] [blame] | 269 | } |
| 270 | return; |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | else |
| 274 | { |
AppaRao Puli | b7e172c | 2019-12-13 14:46:25 +0530 | [diff] [blame] | 275 | // Failed to get data from systemd. System might not |
| 276 | // be ready yet. Attempt again for data. |
| 277 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 278 | "PFR: aync call failed to get FinishTimestamp.", |
| 279 | phosphor::logging::entry("MSG=%s", ec.message().c_str())); |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 280 | } |
AppaRao Puli | b7e172c | 2019-12-13 14:46:25 +0530 | [diff] [blame] | 281 | // FIX-ME: Latest up-stream sync caused issue in receiving |
| 282 | // StartupFinished signal. Unable to get StartupFinished signal |
| 283 | // from systemd1 hence using poll method too, to trigger it |
| 284 | // properly. |
| 285 | constexpr size_t pollTimeout = 10; // seconds |
| 286 | initTimer->expires_after(std::chrono::seconds(pollTimeout)); |
| 287 | initTimer->async_wait([&server, |
| 288 | &conn](const boost::system::error_code& ec) { |
| 289 | if (ec == boost::asio::error::operation_aborted) |
| 290 | { |
| 291 | // Timer reset. |
| 292 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 293 | "PFR: Set boot Checkpoint - Timer aborted or stopped."); |
| 294 | return; |
| 295 | } |
| 296 | if (ec) |
| 297 | { |
| 298 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 299 | "PFR: Set boot Checkpoint - async wait error."); |
| 300 | return; |
| 301 | } |
| 302 | checkAndSetCheckpoint(server, conn); |
| 303 | }); |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 304 | }, |
| 305 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1", |
| 306 | "org.freedesktop.DBus.Properties", "Get", |
| 307 | "org.freedesktop.systemd1.Manager", "FinishTimestamp"); |
| 308 | } |
| 309 | |
AppaRao Puli | a9bf971 | 2020-01-12 05:45:48 +0530 | [diff] [blame] | 310 | void monitorSignals(sdbusplus::asio::object_server& server, |
| 311 | std::shared_ptr<sdbusplus::asio::connection>& conn) |
AppaRao Puli | e63eeda | 2019-07-05 16:25:38 +0530 | [diff] [blame] | 312 | { |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 313 | // Monitor Boot finished signal and set the checkpoint 9 to |
| 314 | // notify CPLD about BMC boot finish. |
| 315 | auto bootFinishedSignal = std::make_unique<sdbusplus::bus::match::match>( |
| 316 | static_cast<sdbusplus::bus::bus&>(*conn), |
| 317 | "type='signal'," |
| 318 | "member='StartupFinished',path='/org/freedesktop/systemd1'," |
| 319 | "interface='org.freedesktop.systemd1.Manager'", |
| 320 | [&server, &conn](sdbusplus::message::message& msg) { |
| 321 | if (!finishedSettingChkPoint) |
| 322 | { |
AppaRao Puli | b7e172c | 2019-12-13 14:46:25 +0530 | [diff] [blame] | 323 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 324 | "PFR: BMC boot completed(StartupFinished). Setting " |
| 325 | "checkpoint 9."); |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 326 | finishedSettingChkPoint = true; |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame^] | 327 | setBMCBootCheckpoint(bmcBootFinishedChkPoint); |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 328 | } |
| 329 | }); |
| 330 | checkAndSetCheckpoint(server, conn); |
| 331 | |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 332 | // Capture the Chassis state and Start the monitor timer |
| 333 | // if state changed to 'On'. Run timer until OS boot. |
| 334 | // Stop timer if state changed to 'Off'. |
| 335 | static auto matchChassisState = sdbusplus::bus::match::match( |
| 336 | static_cast<sdbusplus::bus::bus&>(*conn), |
| 337 | "type='signal',member='PropertiesChanged', " |
| 338 | "interface='org.freedesktop.DBus.Properties', " |
| 339 | "sender='xyz.openbmc_project.State.Chassis', " |
| 340 | "arg0namespace='xyz.openbmc_project.State.Chassis'", |
| 341 | [&server, &conn](sdbusplus::message::message& message) { |
| 342 | std::string intfName; |
| 343 | std::map<std::string, std::variant<std::string>> properties; |
| 344 | message.read(intfName, properties); |
| 345 | |
| 346 | const auto it = properties.find("CurrentPowerState"); |
| 347 | if (it != properties.end()) |
| 348 | { |
| 349 | const std::string* state = |
| 350 | std::get_if<std::string>(&it->second); |
| 351 | if (state != nullptr) |
| 352 | { |
| 353 | if ((*state == |
| 354 | "xyz.openbmc_project.State.Chassis.PowerState.On") && |
| 355 | (!stateTimerRunning)) |
| 356 | { |
| 357 | stateTimerRunning = true; |
| 358 | monitorPlatformStateChange(server, conn); |
| 359 | } |
| 360 | else if ((*state == "xyz.openbmc_project.State.Chassis." |
| 361 | "PowerState.Off") && |
| 362 | (stateTimerRunning)) |
| 363 | { |
| 364 | stateTimer->cancel(); |
| 365 | checkAndLogEvents(); |
| 366 | stateTimerRunning = false; |
| 367 | } |
| 368 | } |
AppaRao Puli | e4e9565 | 2019-07-19 16:52:01 +0530 | [diff] [blame] | 369 | |
| 370 | // Update the D-Bus properties when chassis state changes. |
| 371 | updateDbusPropertiesCache(); |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 372 | } |
| 373 | }); |
| 374 | |
| 375 | // Capture the Host state and Start the monitor timer |
| 376 | // if state changed to 'Running'. Run timer until OS boot. |
| 377 | // Stop timer if state changed to 'Off'. |
| 378 | static auto matchHostState = sdbusplus::bus::match::match( |
| 379 | static_cast<sdbusplus::bus::bus&>(*conn), |
| 380 | "type='signal',member='PropertiesChanged', " |
| 381 | "interface='org.freedesktop.DBus.Properties', " |
| 382 | "sender='xyz.openbmc_project.State.Chassis', " |
| 383 | "arg0namespace='xyz.openbmc_project.State.Host'", |
| 384 | [&server, &conn](sdbusplus::message::message& message) { |
| 385 | std::string intfName; |
| 386 | std::map<std::string, std::variant<std::string>> properties; |
| 387 | message.read(intfName, properties); |
| 388 | |
| 389 | const auto it = properties.find("CurrentHostState"); |
| 390 | if (it != properties.end()) |
| 391 | { |
| 392 | const std::string* state = |
| 393 | std::get_if<std::string>(&it->second); |
| 394 | if (state != nullptr) |
| 395 | { |
| 396 | if ((*state == |
| 397 | "xyz.openbmc_project.State.Host.HostState.Running") && |
| 398 | (!stateTimerRunning)) |
| 399 | { |
| 400 | stateTimerRunning = true; |
| 401 | monitorPlatformStateChange(server, conn); |
| 402 | } |
| 403 | else if (((*state == "xyz.openbmc_project.State.Host." |
| 404 | "HostState.Off") || |
| 405 | (*state == "xyz.openbmc_project.State.Host." |
| 406 | "HostState.Quiesced")) && |
| 407 | (stateTimerRunning)) |
| 408 | { |
| 409 | stateTimer->cancel(); |
| 410 | checkAndLogEvents(); |
| 411 | stateTimerRunning = false; |
| 412 | } |
| 413 | } |
AppaRao Puli | e4e9565 | 2019-07-19 16:52:01 +0530 | [diff] [blame] | 414 | |
| 415 | // Update the D-Bus properties when host state changes. |
| 416 | updateDbusPropertiesCache(); |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 417 | } |
| 418 | }); |
| 419 | |
| 420 | // Capture the OS state change and stop monitor timer |
| 421 | // if OS boots completly or becomes Inactive. |
| 422 | // start timer in other cases to mnitor states. |
| 423 | static auto matchOsState = sdbusplus::bus::match::match( |
| 424 | static_cast<sdbusplus::bus::bus&>(*conn), |
| 425 | "type='signal',member='PropertiesChanged', " |
| 426 | "interface='org.freedesktop.DBus.Properties', " |
| 427 | "sender='xyz.openbmc_project.State.Chassis', " |
| 428 | "arg0namespace='xyz.openbmc_project.State.OperatingSystem.Status'", |
| 429 | [&server, &conn](sdbusplus::message::message& message) { |
| 430 | std::string intfName; |
| 431 | std::map<std::string, std::variant<std::string>> properties; |
| 432 | message.read(intfName, properties); |
| 433 | |
| 434 | const auto it = properties.find("OperatingSystemState"); |
| 435 | if (it != properties.end()) |
| 436 | { |
| 437 | const std::string* state = |
| 438 | std::get_if<std::string>(&it->second); |
| 439 | if (state != nullptr) |
| 440 | { |
| 441 | if (((*state == "BootComplete") || |
| 442 | (*state == "Inactive")) && |
| 443 | (stateTimerRunning)) |
| 444 | { |
| 445 | stateTimer->cancel(); |
| 446 | checkAndLogEvents(); |
| 447 | stateTimerRunning = false; |
| 448 | } |
| 449 | else if (!stateTimerRunning) |
| 450 | { |
| 451 | stateTimerRunning = true; |
| 452 | monitorPlatformStateChange(server, conn); |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | }); |
| 457 | |
| 458 | // First time, check and log events if any. |
| 459 | checkAndLogEvents(); |
AppaRao Puli | a9bf971 | 2020-01-12 05:45:48 +0530 | [diff] [blame] | 460 | } |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 461 | |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame^] | 462 | } // namespace pfr |
| 463 | |
AppaRao Puli | a9bf971 | 2020-01-12 05:45:48 +0530 | [diff] [blame] | 464 | int main() |
| 465 | { |
| 466 | // setup connection to dbus |
| 467 | boost::asio::io_service io; |
| 468 | auto conn = std::make_shared<sdbusplus::asio::connection>(io); |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame^] | 469 | pfr::stateTimer = std::make_unique<boost::asio::steady_timer>(io); |
| 470 | pfr::initTimer = std::make_unique<boost::asio::steady_timer>(io); |
AppaRao Puli | a9bf971 | 2020-01-12 05:45:48 +0530 | [diff] [blame] | 471 | auto server = sdbusplus::asio::object_server(conn, true); |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame^] | 472 | pfr::monitorSignals(server, conn); |
AppaRao Puli | a9bf971 | 2020-01-12 05:45:48 +0530 | [diff] [blame] | 473 | |
| 474 | auto rootInterface = server.add_interface("/xyz/openbmc_project/pfr", ""); |
| 475 | rootInterface->initialize(); |
| 476 | server.add_manager("/xyz/openbmc_project/pfr"); |
| 477 | |
| 478 | // Create PFR attributes object and interface |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame^] | 479 | pfr::pfrConfigObject = std::make_unique<pfr::PfrConfig>(server, conn); |
AppaRao Puli | a9bf971 | 2020-01-12 05:45:48 +0530 | [diff] [blame] | 480 | |
| 481 | // Create Software objects using Versions interface |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame^] | 482 | for (const auto& entry : pfr::verComponentList) |
AppaRao Puli | a9bf971 | 2020-01-12 05:45:48 +0530 | [diff] [blame] | 483 | { |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame^] | 484 | pfr::pfrVersionObjects.emplace_back(std::make_unique<pfr::PfrVersion>( |
AppaRao Puli | a9bf971 | 2020-01-12 05:45:48 +0530 | [diff] [blame] | 485 | server, conn, std::get<0>(entry), std::get<1>(entry), |
| 486 | std::get<2>(entry))); |
| 487 | } |
| 488 | |
| 489 | conn->request_name("xyz.openbmc_project.PFR.Manager"); |
AppaRao Puli | e63eeda | 2019-07-05 16:25:38 +0530 | [diff] [blame] | 490 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 491 | "Intel PFR service started successfully"); |
AppaRao Puli | e63eeda | 2019-07-05 16:25:38 +0530 | [diff] [blame] | 492 | io.run(); |
| 493 | |
| 494 | return 0; |
| 495 | } |