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> |
Chalapathi Venkataramashetty | 29b4779 | 2020-09-10 21:43:53 +0000 | [diff] [blame] | 21 | #include <unistd.h> |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 22 | |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 23 | #include <boost/asio.hpp> |
Chalapathi Venkataramashetty | b213487 | 2020-10-02 21:45:36 +0000 | [diff] [blame] | 24 | #include <sdbusplus/asio/property.hpp> |
| 25 | #include <sdbusplus/unpack_properties.hpp> |
AppaRao Puli | e63eeda | 2019-07-05 16:25:38 +0530 | [diff] [blame] | 26 | |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame] | 27 | namespace pfr |
| 28 | { |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 29 | |
Chalapathi Venkataramashetty | 55e7934 | 2021-03-29 10:17:48 +0000 | [diff] [blame] | 30 | static bool i2cConfigLoaded = false; |
Chalapathi Venkataramashetty | 29b4779 | 2020-09-10 21:43:53 +0000 | [diff] [blame] | 31 | static int retrCount = 10; |
Chalapathi Venkataramashetty | 55e7934 | 2021-03-29 10:17:48 +0000 | [diff] [blame] | 32 | |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 33 | static bool stateTimerRunning = false; |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 34 | bool finishedSettingChkPoint = false; |
| 35 | static constexpr uint8_t bmcBootFinishedChkPoint = 0x09; |
| 36 | |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 37 | std::unique_ptr<boost::asio::steady_timer> stateTimer = nullptr; |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 38 | std::unique_ptr<boost::asio::steady_timer> initTimer = nullptr; |
Chalapathi Venkataramashetty | 29b4779 | 2020-09-10 21:43:53 +0000 | [diff] [blame] | 39 | std::unique_ptr<boost::asio::steady_timer> pfrObjTimer = nullptr; |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame] | 40 | std::vector<std::unique_ptr<PfrVersion>> pfrVersionObjects; |
| 41 | std::unique_ptr<PfrConfig> pfrConfigObject; |
AppaRao Puli | e4e9565 | 2019-07-19 16:52:01 +0530 | [diff] [blame] | 42 | |
AppaRao Puli | e4e9565 | 2019-07-19 16:52:01 +0530 | [diff] [blame] | 43 | // List holds <ObjPath> <ImageType> <VersionPurpose> |
| 44 | static std::vector<std::tuple<std::string, ImageType, std::string>> |
| 45 | verComponentList = { |
AppaRao Puli | e4e9565 | 2019-07-19 16:52:01 +0530 | [diff] [blame] | 46 | std::make_tuple("bmc_recovery", ImageType::bmcRecovery, |
| 47 | versionPurposeBMC), |
AppaRao Puli | e4e9565 | 2019-07-19 16:52:01 +0530 | [diff] [blame] | 48 | std::make_tuple("bios_recovery", ImageType::biosRecovery, |
| 49 | versionPurposeHost), |
Vikram Bodireddy | 3c6c8c3 | 2019-12-05 11:06:15 +0530 | [diff] [blame] | 50 | std::make_tuple("cpld_recovery", ImageType::cpldRecovery, |
| 51 | versionPurposeOther), |
Vikram Bodireddy | 8292dc6 | 2021-05-26 13:31:47 +0530 | [diff] [blame] | 52 | std::make_tuple("afm_active", ImageType::afmActive, |
| 53 | versionPurposeOther), |
| 54 | std::make_tuple("afm_recovery", ImageType::afmRecovery, |
| 55 | versionPurposeOther), |
Vikram Bodireddy | 3c6c8c3 | 2019-12-05 11:06:15 +0530 | [diff] [blame] | 56 | }; |
AppaRao Puli | e4e9565 | 2019-07-19 16:52:01 +0530 | [diff] [blame] | 57 | |
AppaRao Puli | e90f128 | 2019-11-05 01:07:05 +0530 | [diff] [blame] | 58 | // Recovery reason map. |
| 59 | // {<CPLD association>,{<Redfish MessageID>, <Recovery Reason>}} |
| 60 | static const boost::container::flat_map<uint8_t, |
| 61 | std::pair<std::string, std::string>> |
| 62 | recoveryReasonMap = { |
| 63 | {0x01, |
| 64 | {"BIOSFirmwareRecoveryReason", |
Chalapathi | 3fb544b | 2020-02-14 15:43:49 +0000 | [diff] [blame] | 65 | "BIOS active image authentication failure"}}, |
AppaRao Puli | e90f128 | 2019-11-05 01:07:05 +0530 | [diff] [blame] | 66 | {0x02, |
| 67 | {"BIOSFirmwareRecoveryReason", |
Chalapathi | 3fb544b | 2020-02-14 15:43:49 +0000 | [diff] [blame] | 68 | "BIOS recovery image authentication failure"}}, |
AppaRao Puli | e90f128 | 2019-11-05 01:07:05 +0530 | [diff] [blame] | 69 | {0x03, {"MEFirmwareRecoveryReason", "ME launch failure"}}, |
| 70 | {0x04, {"BIOSFirmwareRecoveryReason", "ACM launch failure"}}, |
| 71 | {0x05, {"BIOSFirmwareRecoveryReason", "IBB launch failure"}}, |
| 72 | {0x06, {"BIOSFirmwareRecoveryReason", "OBB launch failure"}}, |
| 73 | {0x07, |
| 74 | {"BMCFirmwareRecoveryReason", |
| 75 | "BMC active image authentication failure"}}, |
| 76 | {0x08, |
| 77 | {"BMCFirmwareRecoveryReason", |
| 78 | "BMC recovery image authentication failure"}}, |
| 79 | {0x09, {"BMCFirmwareRecoveryReason", "BMC launch failure"}}, |
| 80 | {0x0A, {"CPLDFirmwareRecoveryReason", "CPLD watchdog expired"}}}; |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 81 | |
AppaRao Puli | e90f128 | 2019-11-05 01:07:05 +0530 | [diff] [blame] | 82 | // Panic Reason map. |
| 83 | // {<CPLD association>, {<Redfish MessageID>, <Panic reason> }) |
| 84 | static const boost::container::flat_map<uint8_t, |
| 85 | std::pair<std::string, std::string>> |
| 86 | panicReasonMap = { |
Chalapathi | 3fb544b | 2020-02-14 15:43:49 +0000 | [diff] [blame] | 87 | {0x01, {"BIOSFirmwarePanicReason", "BIOS update intent"}}, |
| 88 | {0x02, {"BMCFirmwarePanicReason", "BMC update intent"}}, |
| 89 | {0x03, {"BMCFirmwarePanicReason", "BMC reset detected"}}, |
| 90 | {0x04, {"BMCFirmwarePanicReason", "BMC watchdog expired"}}, |
| 91 | {0x05, {"MEFirmwarePanicReason", "ME watchdog expired"}}, |
| 92 | {0x06, {"BIOSFirmwarePanicReason", "ACM watchdog expired"}}, |
AppaRao Puli | e90f128 | 2019-11-05 01:07:05 +0530 | [diff] [blame] | 93 | {0x09, |
| 94 | {"BIOSFirmwarePanicReason", |
Chalapathi | 3fb544b | 2020-02-14 15:43:49 +0000 | [diff] [blame] | 95 | "ACM or IBB or OBB authentication failure"}}}; |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 96 | |
AppaRao Puli | 2476694 | 2019-11-13 19:27:08 +0530 | [diff] [blame] | 97 | // Firmware resiliency major map. |
| 98 | // {<CPLD association>, {<Redfish MessageID>, <Error reason> }) |
| 99 | static const boost::container::flat_map<uint8_t, |
| 100 | std::pair<std::string, std::string>> |
| 101 | majorErrorCodeMap = { |
| 102 | {0x01, |
| 103 | {"BMCFirmwareResiliencyError", "BMC image authentication failed"}}, |
| 104 | {0x02, |
| 105 | {"BIOSFirmwareResiliencyError", "BIOS image authentication failed"}}, |
Chalapathi | 3fb544b | 2020-02-14 15:43:49 +0000 | [diff] [blame] | 106 | {0x03, {"BIOSFirmwareResiliencyError", "Update from BIOS failed"}}, |
| 107 | {0x04, {"BMCFirmwareResiliencyError", "Update from BMC failed"}}}; |
AppaRao Puli | 2476694 | 2019-11-13 19:27:08 +0530 | [diff] [blame] | 108 | |
AppaRao Puli | e4e9565 | 2019-07-19 16:52:01 +0530 | [diff] [blame] | 109 | static void updateDbusPropertiesCache() |
| 110 | { |
| 111 | for (const auto& pfrVerObj : pfrVersionObjects) |
| 112 | { |
| 113 | pfrVerObj->updateVersion(); |
| 114 | } |
| 115 | |
| 116 | // Update provisoningStatus properties |
| 117 | pfrConfigObject->updateProvisioningStatus(); |
| 118 | |
| 119 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 120 | "PFR Manager service cache data updated."); |
| 121 | } |
| 122 | |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 123 | static void logLastRecoveryEvent() |
| 124 | { |
| 125 | uint8_t reason = 0; |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame] | 126 | if (0 != readCpldReg(ActionType::recoveryReason, reason)) |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 127 | { |
| 128 | return; |
| 129 | } |
| 130 | |
AppaRao Puli | e90f128 | 2019-11-05 01:07:05 +0530 | [diff] [blame] | 131 | auto it = recoveryReasonMap.find(reason); |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 132 | if (it == recoveryReasonMap.end()) |
| 133 | { |
| 134 | // No matching found. So just return without logging event. |
| 135 | return; |
| 136 | } |
AppaRao Puli | e90f128 | 2019-11-05 01:07:05 +0530 | [diff] [blame] | 137 | std::string msgId = "OpenBMC.0.1." + it->second.first; |
| 138 | sd_journal_send("MESSAGE=%s", "Platform firmware recovery occurred.", |
| 139 | "PRIORITY=%i", LOG_WARNING, "REDFISH_MESSAGE_ID=%s", |
| 140 | msgId.c_str(), "REDFISH_MESSAGE_ARGS=%s", |
| 141 | it->second.second.c_str(), NULL); |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static void logLastPanicEvent() |
| 145 | { |
| 146 | uint8_t reason = 0; |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame] | 147 | if (0 != readCpldReg(ActionType::panicReason, reason)) |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 148 | { |
| 149 | return; |
| 150 | } |
| 151 | |
AppaRao Puli | e90f128 | 2019-11-05 01:07:05 +0530 | [diff] [blame] | 152 | auto it = panicReasonMap.find(reason); |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 153 | if (it == panicReasonMap.end()) |
| 154 | { |
| 155 | // No matching found. So just return without logging event. |
| 156 | return; |
| 157 | } |
| 158 | |
AppaRao Puli | e90f128 | 2019-11-05 01:07:05 +0530 | [diff] [blame] | 159 | std::string msgId = "OpenBMC.0.1." + it->second.first; |
| 160 | sd_journal_send("MESSAGE=%s", "Platform firmware panic occurred.", |
| 161 | "PRIORITY=%i", LOG_WARNING, "REDFISH_MESSAGE_ID=%s", |
| 162 | msgId.c_str(), "REDFISH_MESSAGE_ARGS=%s", |
| 163 | it->second.second.c_str(), NULL); |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 164 | } |
| 165 | |
AppaRao Puli | 2476694 | 2019-11-13 19:27:08 +0530 | [diff] [blame] | 166 | static void logResiliencyErrorEvent(const uint8_t majorErrorCode, |
| 167 | const uint8_t minorErrorCode) |
| 168 | { |
Chalapathi Venkataramashetty | bcc7ce1 | 2021-05-17 04:27:21 +0000 | [diff] [blame^] | 169 | uint8_t cpldRoTRev = 0; |
| 170 | if (0 != readCpldReg(ActionType::readRoTRev, cpldRoTRev)) |
| 171 | { |
| 172 | return; |
| 173 | } |
| 174 | |
AppaRao Puli | 2476694 | 2019-11-13 19:27:08 +0530 | [diff] [blame] | 175 | auto it = majorErrorCodeMap.find(majorErrorCode); |
Chalapathi Venkataramashetty | bcc7ce1 | 2021-05-17 04:27:21 +0000 | [diff] [blame^] | 176 | if (cpldRoTRev == 0x02) |
| 177 | { |
| 178 | auto itRev2 = majorErrorCodeMapRev2.find(majorErrorCode); |
| 179 | if (itRev2 != majorErrorCodeMapRev2.end()) |
| 180 | { |
| 181 | it = itRev2; |
| 182 | } |
| 183 | else if (it == majorErrorCodeMap.end()) |
| 184 | { |
| 185 | // No matching found. So just return without logging event. |
| 186 | return; |
| 187 | } |
| 188 | } |
| 189 | else if (it == majorErrorCodeMap.end()) |
AppaRao Puli | 2476694 | 2019-11-13 19:27:08 +0530 | [diff] [blame] | 190 | { |
| 191 | // No matching found. So just return without logging event. |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | std::string errorStr = |
| 196 | it->second.second + "(MinorCode:0x" + toHexString(minorErrorCode) + ")"; |
| 197 | std::string msgId = "OpenBMC.0.1." + it->second.first; |
| 198 | sd_journal_send( |
| 199 | "MESSAGE=%s", "Platform firmware resiliency error occurred.", |
| 200 | "PRIORITY=%i", LOG_ERR, "REDFISH_MESSAGE_ID=%s", msgId.c_str(), |
| 201 | "REDFISH_MESSAGE_ARGS=%s", errorStr.c_str(), NULL); |
| 202 | } |
| 203 | |
Chalapathi Venkataramashetty | b213487 | 2020-10-02 21:45:36 +0000 | [diff] [blame] | 204 | static void |
| 205 | handleLastCountChange(std::shared_ptr<sdbusplus::asio::connection> conn, |
| 206 | std::string eventName, uint8_t currentCount) |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 207 | { |
Chalapathi Venkataramashetty | b213487 | 2020-10-02 21:45:36 +0000 | [diff] [blame] | 208 | sdbusplus::asio::setProperty( |
| 209 | *conn, "xyz.openbmc_project.Settings", |
| 210 | "/xyz/openbmc_project/pfr/last_events", |
| 211 | "xyz.openbmc_project.PFR.LastEvents", eventName, currentCount, |
| 212 | [](boost::system::error_code ec) { |
| 213 | if (ec) |
| 214 | { |
| 215 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 216 | "PFR: Unable to update currentCount", |
| 217 | phosphor::logging::entry("MSG=%s", ec.message().c_str())); |
| 218 | return; |
| 219 | } |
| 220 | }); |
| 221 | return; |
| 222 | } |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 223 | |
Chalapathi Venkataramashetty | b213487 | 2020-10-02 21:45:36 +0000 | [diff] [blame] | 224 | static void |
| 225 | checkAndLogEvents(std::shared_ptr<sdbusplus::asio::connection>& conn) |
| 226 | { |
| 227 | sdbusplus::asio::getAllProperties( |
| 228 | *conn, "xyz.openbmc_project.Settings", |
| 229 | "/xyz/openbmc_project/pfr/last_events", |
| 230 | "xyz.openbmc_project.PFR.LastEvents", |
| 231 | [conn]( |
| 232 | boost::system::error_code ec, |
| 233 | std::vector< |
| 234 | std::pair<std::string, std::variant<std::monostate, uint8_t>>>& |
| 235 | properties) { |
| 236 | if (ec) |
| 237 | { |
| 238 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 239 | "PFR: Unable get PFR last events", |
| 240 | phosphor::logging::entry("MSG=%s", ec.message().c_str())); |
| 241 | return; |
| 242 | } |
| 243 | uint8_t lastRecoveryCount = 0; |
| 244 | uint8_t lastPanicCount = 0; |
| 245 | uint8_t lastMajorErr = 0; |
| 246 | uint8_t lastMinorErr = 0; |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 247 | |
Chalapathi Venkataramashetty | b213487 | 2020-10-02 21:45:36 +0000 | [diff] [blame] | 248 | try |
| 249 | { |
| 250 | sdbusplus::unpackProperties( |
| 251 | properties, "lastRecoveryCount", lastRecoveryCount, |
| 252 | "lastPanicCount", lastPanicCount, "lastMajorErr", |
| 253 | lastMajorErr, "lastMinorErr", lastMinorErr); |
| 254 | } |
| 255 | catch (const sdbusplus::exception::UnpackPropertyError& error) |
| 256 | { |
| 257 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 258 | "PFR: Unpack error", |
| 259 | phosphor::logging::entry("MSG=%s", error.what())); |
| 260 | return; |
| 261 | } |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 262 | |
Chalapathi Venkataramashetty | b213487 | 2020-10-02 21:45:36 +0000 | [diff] [blame] | 263 | uint8_t currPanicCount = 0; |
| 264 | if (0 == readCpldReg(ActionType::panicCount, currPanicCount)) |
| 265 | { |
| 266 | if (lastPanicCount != currPanicCount) |
| 267 | { |
| 268 | // Update cached data to dbus and log redfish |
| 269 | // event by reading reason. |
| 270 | handleLastCountChange(conn, "lastPanicCount", |
| 271 | currPanicCount); |
| 272 | if (currPanicCount) |
| 273 | { |
| 274 | logLastPanicEvent(); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | uint8_t currRecoveryCount = 0; |
| 280 | if (0 == readCpldReg(ActionType::recoveryCount, currRecoveryCount)) |
| 281 | { |
| 282 | if (lastRecoveryCount != currRecoveryCount) |
| 283 | { |
| 284 | // Update cached data to dbus and log redfish |
| 285 | // event by reading reason. |
| 286 | handleLastCountChange(conn, "lastRecoveryCount", |
| 287 | currRecoveryCount); |
| 288 | if (currRecoveryCount) |
| 289 | { |
| 290 | logLastRecoveryEvent(); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | uint8_t majorErr = 0; |
| 296 | uint8_t minorErr = 0; |
| 297 | if ((0 == readCpldReg(ActionType::majorError, majorErr)) && |
| 298 | (0 == readCpldReg(ActionType::minorError, minorErr))) |
| 299 | { |
| 300 | if ((lastMajorErr != majorErr) || (lastMinorErr != minorErr)) |
| 301 | { |
| 302 | // Update cached data to dbus and log redfish event by |
| 303 | // reading reason. |
| 304 | handleLastCountChange(conn, "lastMajorErr", majorErr); |
| 305 | handleLastCountChange(conn, "lastMinorErr", minorErr); |
| 306 | if (majorErr && minorErr) |
| 307 | { |
| 308 | logResiliencyErrorEvent(majorErr, minorErr); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | }); |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static void monitorPlatformStateChange( |
| 316 | sdbusplus::asio::object_server& server, |
| 317 | std::shared_ptr<sdbusplus::asio::connection>& conn) |
| 318 | { |
| 319 | constexpr size_t pollTimeout = 10; // seconds |
| 320 | stateTimer->expires_after(std::chrono::seconds(pollTimeout)); |
| 321 | stateTimer->async_wait( |
| 322 | [&server, &conn](const boost::system::error_code& ec) { |
| 323 | if (ec == boost::asio::error::operation_aborted) |
| 324 | { |
| 325 | // Timer reset. |
| 326 | return; |
| 327 | } |
| 328 | if (ec) |
| 329 | { |
| 330 | // Platform State Monitor - Timer cancelled. |
| 331 | return; |
| 332 | } |
Chalapathi Venkataramashetty | b213487 | 2020-10-02 21:45:36 +0000 | [diff] [blame] | 333 | checkAndLogEvents(conn); |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 334 | monitorPlatformStateChange(server, conn); |
| 335 | }); |
| 336 | } |
| 337 | |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 338 | void checkAndSetCheckpoint(sdbusplus::asio::object_server& server, |
| 339 | std::shared_ptr<sdbusplus::asio::connection>& conn) |
| 340 | { |
| 341 | // Check whether systemd completed all the loading. |
| 342 | conn->async_method_call( |
| 343 | [&server, &conn](boost::system::error_code ec, |
| 344 | const std::variant<uint64_t>& value) { |
AppaRao Puli | b7e172c | 2019-12-13 14:46:25 +0530 | [diff] [blame] | 345 | if (!ec) |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 346 | { |
AppaRao Puli | b7e172c | 2019-12-13 14:46:25 +0530 | [diff] [blame] | 347 | if (std::get<uint64_t>(value)) |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 348 | { |
AppaRao Puli | b7e172c | 2019-12-13 14:46:25 +0530 | [diff] [blame] | 349 | phosphor::logging::log<phosphor::logging::level::INFO>( |
Chalapathi Venkataramashetty | 29b4779 | 2020-09-10 21:43:53 +0000 | [diff] [blame] | 350 | "BMC boot completed. Setting checkpoint 9."); |
AppaRao Puli | b7e172c | 2019-12-13 14:46:25 +0530 | [diff] [blame] | 351 | if (!finishedSettingChkPoint) |
| 352 | { |
| 353 | finishedSettingChkPoint = true; |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame] | 354 | setBMCBootCheckpoint(bmcBootFinishedChkPoint); |
AppaRao Puli | b7e172c | 2019-12-13 14:46:25 +0530 | [diff] [blame] | 355 | } |
| 356 | return; |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | else |
| 360 | { |
AppaRao Puli | b7e172c | 2019-12-13 14:46:25 +0530 | [diff] [blame] | 361 | // Failed to get data from systemd. System might not |
| 362 | // be ready yet. Attempt again for data. |
| 363 | phosphor::logging::log<phosphor::logging::level::ERR>( |
Chalapathi Venkataramashetty | 29b4779 | 2020-09-10 21:43:53 +0000 | [diff] [blame] | 364 | "aync call failed to get FinishTimestamp.", |
AppaRao Puli | b7e172c | 2019-12-13 14:46:25 +0530 | [diff] [blame] | 365 | phosphor::logging::entry("MSG=%s", ec.message().c_str())); |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 366 | } |
AppaRao Puli | b7e172c | 2019-12-13 14:46:25 +0530 | [diff] [blame] | 367 | // FIX-ME: Latest up-stream sync caused issue in receiving |
| 368 | // StartupFinished signal. Unable to get StartupFinished signal |
| 369 | // from systemd1 hence using poll method too, to trigger it |
| 370 | // properly. |
| 371 | constexpr size_t pollTimeout = 10; // seconds |
| 372 | initTimer->expires_after(std::chrono::seconds(pollTimeout)); |
Chalapathi Venkataramashetty | 29b4779 | 2020-09-10 21:43:53 +0000 | [diff] [blame] | 373 | initTimer->async_wait( |
| 374 | [&server, &conn](const boost::system::error_code& ec) { |
| 375 | if (ec == boost::asio::error::operation_aborted) |
| 376 | { |
| 377 | // Timer reset. |
| 378 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 379 | "Set boot Checkpoint - Timer aborted or stopped."); |
| 380 | return; |
| 381 | } |
| 382 | if (ec) |
| 383 | { |
| 384 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 385 | "Set boot Checkpoint - async wait error."); |
| 386 | return; |
| 387 | } |
| 388 | checkAndSetCheckpoint(server, conn); |
| 389 | }); |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 390 | }, |
| 391 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1", |
| 392 | "org.freedesktop.DBus.Properties", "Get", |
| 393 | "org.freedesktop.systemd1.Manager", "FinishTimestamp"); |
| 394 | } |
| 395 | |
AppaRao Puli | a9bf971 | 2020-01-12 05:45:48 +0530 | [diff] [blame] | 396 | void monitorSignals(sdbusplus::asio::object_server& server, |
| 397 | std::shared_ptr<sdbusplus::asio::connection>& conn) |
AppaRao Puli | e63eeda | 2019-07-05 16:25:38 +0530 | [diff] [blame] | 398 | { |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 399 | // Monitor Boot finished signal and set the checkpoint 9 to |
| 400 | // notify CPLD about BMC boot finish. |
| 401 | auto bootFinishedSignal = std::make_unique<sdbusplus::bus::match::match>( |
| 402 | static_cast<sdbusplus::bus::bus&>(*conn), |
| 403 | "type='signal'," |
| 404 | "member='StartupFinished',path='/org/freedesktop/systemd1'," |
| 405 | "interface='org.freedesktop.systemd1.Manager'", |
| 406 | [&server, &conn](sdbusplus::message::message& msg) { |
| 407 | if (!finishedSettingChkPoint) |
| 408 | { |
AppaRao Puli | b7e172c | 2019-12-13 14:46:25 +0530 | [diff] [blame] | 409 | phosphor::logging::log<phosphor::logging::level::INFO>( |
Chalapathi Venkataramashetty | 29b4779 | 2020-09-10 21:43:53 +0000 | [diff] [blame] | 410 | "BMC boot completed(StartupFinished). Setting " |
AppaRao Puli | b7e172c | 2019-12-13 14:46:25 +0530 | [diff] [blame] | 411 | "checkpoint 9."); |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 412 | finishedSettingChkPoint = true; |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame] | 413 | setBMCBootCheckpoint(bmcBootFinishedChkPoint); |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 414 | } |
| 415 | }); |
| 416 | checkAndSetCheckpoint(server, conn); |
| 417 | |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 418 | // Capture the Chassis state and Start the monitor timer |
| 419 | // if state changed to 'On'. Run timer until OS boot. |
| 420 | // Stop timer if state changed to 'Off'. |
| 421 | static auto matchChassisState = sdbusplus::bus::match::match( |
| 422 | static_cast<sdbusplus::bus::bus&>(*conn), |
| 423 | "type='signal',member='PropertiesChanged', " |
| 424 | "interface='org.freedesktop.DBus.Properties', " |
| 425 | "sender='xyz.openbmc_project.State.Chassis', " |
| 426 | "arg0namespace='xyz.openbmc_project.State.Chassis'", |
| 427 | [&server, &conn](sdbusplus::message::message& message) { |
| 428 | std::string intfName; |
| 429 | std::map<std::string, std::variant<std::string>> properties; |
| 430 | message.read(intfName, properties); |
| 431 | |
| 432 | const auto it = properties.find("CurrentPowerState"); |
| 433 | if (it != properties.end()) |
| 434 | { |
| 435 | const std::string* state = |
| 436 | std::get_if<std::string>(&it->second); |
| 437 | if (state != nullptr) |
| 438 | { |
| 439 | if ((*state == |
| 440 | "xyz.openbmc_project.State.Chassis.PowerState.On") && |
| 441 | (!stateTimerRunning)) |
| 442 | { |
| 443 | stateTimerRunning = true; |
| 444 | monitorPlatformStateChange(server, conn); |
| 445 | } |
| 446 | else if ((*state == "xyz.openbmc_project.State.Chassis." |
| 447 | "PowerState.Off") && |
| 448 | (stateTimerRunning)) |
| 449 | { |
| 450 | stateTimer->cancel(); |
Chalapathi Venkataramashetty | b213487 | 2020-10-02 21:45:36 +0000 | [diff] [blame] | 451 | checkAndLogEvents(conn); |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 452 | stateTimerRunning = false; |
| 453 | } |
| 454 | } |
AppaRao Puli | e4e9565 | 2019-07-19 16:52:01 +0530 | [diff] [blame] | 455 | |
| 456 | // Update the D-Bus properties when chassis state changes. |
| 457 | updateDbusPropertiesCache(); |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 458 | } |
| 459 | }); |
| 460 | |
| 461 | // Capture the Host state and Start the monitor timer |
| 462 | // if state changed to 'Running'. Run timer until OS boot. |
| 463 | // Stop timer if state changed to 'Off'. |
| 464 | static auto matchHostState = sdbusplus::bus::match::match( |
| 465 | static_cast<sdbusplus::bus::bus&>(*conn), |
| 466 | "type='signal',member='PropertiesChanged', " |
| 467 | "interface='org.freedesktop.DBus.Properties', " |
| 468 | "sender='xyz.openbmc_project.State.Chassis', " |
| 469 | "arg0namespace='xyz.openbmc_project.State.Host'", |
| 470 | [&server, &conn](sdbusplus::message::message& message) { |
| 471 | std::string intfName; |
| 472 | std::map<std::string, std::variant<std::string>> properties; |
| 473 | message.read(intfName, properties); |
| 474 | |
| 475 | const auto it = properties.find("CurrentHostState"); |
| 476 | if (it != properties.end()) |
| 477 | { |
| 478 | const std::string* state = |
| 479 | std::get_if<std::string>(&it->second); |
| 480 | if (state != nullptr) |
| 481 | { |
| 482 | if ((*state == |
| 483 | "xyz.openbmc_project.State.Host.HostState.Running") && |
| 484 | (!stateTimerRunning)) |
| 485 | { |
| 486 | stateTimerRunning = true; |
| 487 | monitorPlatformStateChange(server, conn); |
| 488 | } |
| 489 | else if (((*state == "xyz.openbmc_project.State.Host." |
| 490 | "HostState.Off") || |
| 491 | (*state == "xyz.openbmc_project.State.Host." |
| 492 | "HostState.Quiesced")) && |
| 493 | (stateTimerRunning)) |
| 494 | { |
| 495 | stateTimer->cancel(); |
Chalapathi Venkataramashetty | b213487 | 2020-10-02 21:45:36 +0000 | [diff] [blame] | 496 | checkAndLogEvents(conn); |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 497 | stateTimerRunning = false; |
| 498 | } |
| 499 | } |
AppaRao Puli | e4e9565 | 2019-07-19 16:52:01 +0530 | [diff] [blame] | 500 | |
| 501 | // Update the D-Bus properties when host state changes. |
| 502 | updateDbusPropertiesCache(); |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 503 | } |
| 504 | }); |
| 505 | |
| 506 | // Capture the OS state change and stop monitor timer |
| 507 | // if OS boots completly or becomes Inactive. |
| 508 | // start timer in other cases to mnitor states. |
| 509 | static auto matchOsState = sdbusplus::bus::match::match( |
| 510 | static_cast<sdbusplus::bus::bus&>(*conn), |
| 511 | "type='signal',member='PropertiesChanged', " |
| 512 | "interface='org.freedesktop.DBus.Properties', " |
| 513 | "sender='xyz.openbmc_project.State.Chassis', " |
| 514 | "arg0namespace='xyz.openbmc_project.State.OperatingSystem.Status'", |
| 515 | [&server, &conn](sdbusplus::message::message& message) { |
| 516 | std::string intfName; |
| 517 | std::map<std::string, std::variant<std::string>> properties; |
| 518 | message.read(intfName, properties); |
| 519 | |
| 520 | const auto it = properties.find("OperatingSystemState"); |
| 521 | if (it != properties.end()) |
| 522 | { |
| 523 | const std::string* state = |
| 524 | std::get_if<std::string>(&it->second); |
| 525 | if (state != nullptr) |
| 526 | { |
| 527 | if (((*state == "BootComplete") || |
| 528 | (*state == "Inactive")) && |
| 529 | (stateTimerRunning)) |
| 530 | { |
| 531 | stateTimer->cancel(); |
Chalapathi Venkataramashetty | b213487 | 2020-10-02 21:45:36 +0000 | [diff] [blame] | 532 | checkAndLogEvents(conn); |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 533 | stateTimerRunning = false; |
| 534 | } |
| 535 | else if (!stateTimerRunning) |
| 536 | { |
| 537 | stateTimerRunning = true; |
| 538 | monitorPlatformStateChange(server, conn); |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | }); |
| 543 | |
| 544 | // First time, check and log events if any. |
Chalapathi Venkataramashetty | b213487 | 2020-10-02 21:45:36 +0000 | [diff] [blame] | 545 | checkAndLogEvents(conn); |
AppaRao Puli | a9bf971 | 2020-01-12 05:45:48 +0530 | [diff] [blame] | 546 | } |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 547 | |
Chalapathi Venkataramashetty | e6fb18e | 2021-02-03 14:51:00 +0000 | [diff] [blame] | 548 | static void updateCPLDversion(std::shared_ptr<sdbusplus::asio::connection> conn) |
| 549 | { |
| 550 | std::string cpldVersion = pfr::readCPLDVersion(); |
| 551 | conn->async_method_call( |
| 552 | [](const boost::system::error_code ec) { |
| 553 | if (ec) |
| 554 | { |
| 555 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 556 | "Unable to update cpld_active version", |
| 557 | phosphor::logging::entry("MSG=%s", ec.message().c_str())); |
| 558 | return; |
| 559 | } |
| 560 | }, |
| 561 | "xyz.openbmc_project.Settings", |
| 562 | "/xyz/openbmc_project/software/cpld_active", |
| 563 | "org.freedesktop.DBus.Properties", "Set", |
| 564 | "xyz.openbmc_project.Software.Version", "Version", |
| 565 | std::variant<std::string>(cpldVersion)); |
| 566 | return; |
| 567 | } |
| 568 | |
Chalapathi Venkataramashetty | 29b4779 | 2020-09-10 21:43:53 +0000 | [diff] [blame] | 569 | void checkPfrInterface(std::shared_ptr<sdbusplus::asio::connection> conn) |
| 570 | { |
| 571 | if (!i2cConfigLoaded) |
| 572 | { |
| 573 | init(conn, i2cConfigLoaded); |
| 574 | if (retrCount > 0) |
| 575 | { |
| 576 | // pfr object not loaded yet. query again. |
| 577 | return; |
| 578 | } |
| 579 | else |
| 580 | { |
| 581 | // Platform does not contain pfr object. Stop the service. |
| 582 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 583 | "Platform does not support PFR, hence stop the " |
| 584 | "service."); |
| 585 | std::exit(EXIT_SUCCESS); |
| 586 | return; |
| 587 | } |
| 588 | } |
| 589 | else |
| 590 | { |
| 591 | retrCount = 0; |
| 592 | |
| 593 | bool locked = false; |
| 594 | bool prov = false; |
| 595 | bool support = false; |
| 596 | pfr::getProvisioningStatus(locked, prov, support); |
| 597 | if (support && prov) |
| 598 | { |
| 599 | // pfr provisioned. |
| 600 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 601 | "PFR Supported."); |
| 602 | return; |
| 603 | } |
| 604 | else |
| 605 | { |
| 606 | // pfr not supported, stop the service |
| 607 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 608 | "PFR not Supported. Hence stop the service"); |
| 609 | std::exit(EXIT_SUCCESS); |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | void checkPFRandAddObjects(sdbusplus::asio::object_server& server, |
| 614 | std::shared_ptr<sdbusplus::asio::connection>& conn) |
| 615 | { |
| 616 | checkPfrInterface(conn); |
| 617 | |
| 618 | constexpr size_t timeout = 10; // seconds |
| 619 | pfrObjTimer->expires_after(std::chrono::seconds(timeout)); |
| 620 | pfrObjTimer->async_wait([&conn, |
| 621 | &server](const boost::system::error_code& ec) { |
| 622 | if (ec) |
| 623 | { |
| 624 | if (ec == boost::asio::error::operation_aborted) |
| 625 | { |
| 626 | // Timer reset. |
| 627 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 628 | "pfr object found. Hence Object Timer aborted or stopped."); |
| 629 | } |
| 630 | else |
| 631 | { |
| 632 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 633 | "pfr object timer error."); |
| 634 | } |
| 635 | } |
| 636 | if (retrCount > 0) |
| 637 | { |
| 638 | checkPFRandAddObjects(server, conn); |
| 639 | } |
| 640 | else |
| 641 | { |
| 642 | pfr::monitorSignals(server, conn); |
| 643 | |
| 644 | // Update the D-Bus properties. |
| 645 | updateDbusPropertiesCache(); |
| 646 | // Update CPLD Version to cpld_active object in settings. |
| 647 | updateCPLDversion(conn); |
| 648 | } |
| 649 | retrCount--; |
| 650 | }); |
| 651 | } |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame] | 652 | } // namespace pfr |
| 653 | |
AppaRao Puli | a9bf971 | 2020-01-12 05:45:48 +0530 | [diff] [blame] | 654 | int main() |
| 655 | { |
| 656 | // setup connection to dbus |
| 657 | boost::asio::io_service io; |
| 658 | auto conn = std::make_shared<sdbusplus::asio::connection>(io); |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame] | 659 | pfr::stateTimer = std::make_unique<boost::asio::steady_timer>(io); |
| 660 | pfr::initTimer = std::make_unique<boost::asio::steady_timer>(io); |
Chalapathi Venkataramashetty | 29b4779 | 2020-09-10 21:43:53 +0000 | [diff] [blame] | 661 | pfr::pfrObjTimer = std::make_unique<boost::asio::steady_timer>(io); |
AppaRao Puli | a9bf971 | 2020-01-12 05:45:48 +0530 | [diff] [blame] | 662 | auto server = sdbusplus::asio::object_server(conn, true); |
Chalapathi Venkataramashetty | 55e7934 | 2021-03-29 10:17:48 +0000 | [diff] [blame] | 663 | pfr::init(conn, pfr::i2cConfigLoaded); |
Chalapathi Venkataramashetty | 29b4779 | 2020-09-10 21:43:53 +0000 | [diff] [blame] | 664 | |
| 665 | pfr::checkPFRandAddObjects(server, conn); |
AppaRao Puli | a9bf971 | 2020-01-12 05:45:48 +0530 | [diff] [blame] | 666 | |
Chalapathi Venkataramashetty | e6fb18e | 2021-02-03 14:51:00 +0000 | [diff] [blame] | 667 | // Update CPLD Version to cpld_active object in settings. |
| 668 | pfr::updateCPLDversion(conn); |
| 669 | |
AppaRao Puli | a9bf971 | 2020-01-12 05:45:48 +0530 | [diff] [blame] | 670 | server.add_manager("/xyz/openbmc_project/pfr"); |
| 671 | |
| 672 | // Create PFR attributes object and interface |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame] | 673 | pfr::pfrConfigObject = std::make_unique<pfr::PfrConfig>(server, conn); |
AppaRao Puli | a9bf971 | 2020-01-12 05:45:48 +0530 | [diff] [blame] | 674 | |
| 675 | // Create Software objects using Versions interface |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame] | 676 | for (const auto& entry : pfr::verComponentList) |
AppaRao Puli | a9bf971 | 2020-01-12 05:45:48 +0530 | [diff] [blame] | 677 | { |
AppaRao Puli | dcf1312 | 2020-05-28 01:21:39 +0530 | [diff] [blame] | 678 | pfr::pfrVersionObjects.emplace_back(std::make_unique<pfr::PfrVersion>( |
AppaRao Puli | a9bf971 | 2020-01-12 05:45:48 +0530 | [diff] [blame] | 679 | server, conn, std::get<0>(entry), std::get<1>(entry), |
| 680 | std::get<2>(entry))); |
| 681 | } |
| 682 | |
| 683 | conn->request_name("xyz.openbmc_project.PFR.Manager"); |
AppaRao Puli | e63eeda | 2019-07-05 16:25:38 +0530 | [diff] [blame] | 684 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 685 | "Intel PFR service started successfully"); |
AppaRao Puli | e63eeda | 2019-07-05 16:25:38 +0530 | [diff] [blame] | 686 | io.run(); |
| 687 | |
| 688 | return 0; |
| 689 | } |