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