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