AppaRao Puli | c532f55 | 2019-07-05 15:23:50 +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 | c532f55 | 2019-07-05 15:23:50 +0530 | [diff] [blame] | 17 | #include "pfr.hpp" |
AppaRao Puli | 67d184c | 2020-05-29 00:48:33 +0530 | [diff] [blame] | 18 | |
AppaRao Puli | c532f55 | 2019-07-05 15:23:50 +0530 | [diff] [blame] | 19 | #include "file.hpp" |
AppaRao Puli | 4b639a2 | 2019-10-01 18:12:59 +0530 | [diff] [blame] | 20 | #include "spiDev.hpp" |
AppaRao Puli | c532f55 | 2019-07-05 15:23:50 +0530 | [diff] [blame] | 21 | |
Chalapathi Venkataramashetty | 53b8700 | 2020-07-03 09:52:44 +0000 | [diff] [blame] | 22 | #include <gpiod.hpp> |
| 23 | |
AppaRao Puli | 67d184c | 2020-05-29 00:48:33 +0530 | [diff] [blame] | 24 | #include <iomanip> |
| 25 | #include <iostream> |
| 26 | #include <sstream> |
| 27 | |
AppaRao Puli | c532f55 | 2019-07-05 15:23:50 +0530 | [diff] [blame] | 28 | namespace pfr |
| 29 | { |
Chalapathi Venkataramashetty | 55e7934 | 2021-03-29 10:17:48 +0000 | [diff] [blame] | 30 | |
| 31 | using GetSubTreeType = std::vector< |
| 32 | std::pair<std::string, |
| 33 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
| 34 | |
| 35 | static int i2cBusNumber = 4; |
| 36 | static int i2cSlaveAddress = 56; |
AppaRao Puli | c532f55 | 2019-07-05 15:23:50 +0530 | [diff] [blame] | 37 | |
| 38 | // CPLD mailbox registers |
Chalapathi Venkataramashetty | 53b8700 | 2020-07-03 09:52:44 +0000 | [diff] [blame] | 39 | static constexpr uint8_t pfrROTId = 0x00; |
AppaRao Puli | c532f55 | 2019-07-05 15:23:50 +0530 | [diff] [blame] | 40 | static constexpr uint8_t cpldROTVersion = 0x01; |
| 41 | static constexpr uint8_t cpldROTSvn = 0x02; |
| 42 | static constexpr uint8_t platformState = 0x03; |
| 43 | static constexpr uint8_t recoveryCount = 0x04; |
| 44 | static constexpr uint8_t lastRecoveryReason = 0x05; |
| 45 | static constexpr uint8_t panicEventCount = 0x06; |
| 46 | static constexpr uint8_t panicEventReason = 0x07; |
| 47 | static constexpr uint8_t majorErrorCode = 0x08; |
| 48 | static constexpr uint8_t minorErrorCode = 0x09; |
| 49 | static constexpr uint8_t provisioningStatus = 0x0A; |
Vikram Bodireddy | 0884191 | 2020-08-31 18:11:45 +0530 | [diff] [blame] | 50 | static constexpr uint8_t bmcBootCheckpointRev1 = 0x0F; |
| 51 | static constexpr uint8_t bmcBootCheckpoint = 0x60; |
AppaRao Puli | a04c825 | 2019-07-31 22:59:28 +0530 | [diff] [blame] | 52 | static constexpr uint8_t pchActiveMajorVersion = 0x15; |
| 53 | static constexpr uint8_t pchActiveMinorVersion = 0x16; |
AppaRao Puli | a04c825 | 2019-07-31 22:59:28 +0530 | [diff] [blame] | 54 | static constexpr uint8_t pchRecoveryMajorVersion = 0x1B; |
| 55 | static constexpr uint8_t pchRecoveryMinorVersion = 0x1C; |
Chalapathi Venkataramashetty | 53b8700 | 2020-07-03 09:52:44 +0000 | [diff] [blame] | 56 | static constexpr uint8_t CPLDHashRegStart = 0x20; |
| 57 | static constexpr uint8_t pfrRoTValue = 0xDE; |
Vikram Bodireddy | 8292dc6 | 2021-05-26 13:31:47 +0530 | [diff] [blame] | 58 | static constexpr uint8_t afmActiveMajorVersion = 0x75; |
| 59 | static constexpr uint8_t afmActiveMinorVersion = 0x76; |
| 60 | static constexpr uint8_t afmRecoveryMajorVersion = 0x78; |
| 61 | static constexpr uint8_t afmRecoveryMinorVersion = 0x79; |
AppaRao Puli | c532f55 | 2019-07-05 15:23:50 +0530 | [diff] [blame] | 62 | |
| 63 | static constexpr uint8_t ufmLockedMask = (0x1 << 0x04); |
| 64 | static constexpr uint8_t ufmProvisionedMask = (0x1 << 0x05); |
| 65 | |
AppaRao Puli | 4b639a2 | 2019-10-01 18:12:59 +0530 | [diff] [blame] | 66 | // PFR MTD devices |
| 67 | static constexpr const char* bmcActiveImgPfmMTDDev = "/dev/mtd/pfm"; |
| 68 | static constexpr const char* bmcRecoveryImgMTDDev = "/dev/mtd/rc-image"; |
| 69 | |
| 70 | // PFM offset in full image |
| 71 | static constexpr const uint32_t pfmBaseOffsetInImage = 0x400; |
| 72 | |
| 73 | // OFFSET values in PFM |
| 74 | static constexpr const uint32_t verOffsetInPFM = 0x406; |
| 75 | static constexpr const uint32_t buildNumOffsetInPFM = 0x40C; |
| 76 | static constexpr const uint32_t buildHashOffsetInPFM = 0x40D; |
| 77 | |
Chalapathi Venkataramashetty | 00acaff | 2020-12-24 11:01:17 +0000 | [diff] [blame] | 78 | bool exceptionFlag = true; |
sri anjaneyulu alapati | be13259 | 2023-07-17 10:16:39 +0000 | [diff] [blame] | 79 | extern bool bmcBootCompleteChkPointDone; |
| 80 | extern bool unProvChkPointStatus; |
Chalapathi Venkataramashetty | 00acaff | 2020-12-24 11:01:17 +0000 | [diff] [blame] | 81 | |
Chalapathi Venkataramashetty | 55e7934 | 2021-03-29 10:17:48 +0000 | [diff] [blame] | 82 | void init(std::shared_ptr<sdbusplus::asio::connection> conn, |
| 83 | bool& i2cConfigLoaded) |
| 84 | { |
| 85 | conn->async_method_call( |
| 86 | [conn, &i2cConfigLoaded](const boost::system::error_code ec, |
| 87 | const GetSubTreeType& resp) { |
Patrick Williams | 304e82e | 2023-05-10 07:51:13 -0500 | [diff] [blame] | 88 | if (ec || resp.size() != 1) |
| 89 | { |
| 90 | return; |
| 91 | } |
| 92 | if (resp[0].second.begin() == resp[0].second.end()) |
| 93 | return; |
| 94 | const std::string& objPath = resp[0].first; |
| 95 | const std::string& serviceName = resp[0].second.begin()->first; |
Vikram Bodireddy | 1c06ae4 | 2021-07-08 15:26:51 +0530 | [diff] [blame] | 96 | |
Patrick Williams | 304e82e | 2023-05-10 07:51:13 -0500 | [diff] [blame] | 97 | const std::string match = "Baseboard/PFR"; |
| 98 | if (boost::ends_with(objPath, match)) |
| 99 | { |
| 100 | // PFR object found.. check for PFR support |
| 101 | conn->async_method_call( |
| 102 | [objPath, serviceName, conn, &i2cConfigLoaded]( |
| 103 | boost::system::error_code ec, |
| 104 | const std::vector<std::pair< |
| 105 | std::string, std::variant<std::string, uint64_t>>>& |
| 106 | propertiesList) { |
| 107 | if (ec) |
| 108 | { |
| 109 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 110 | "Error to Get PFR properties.", |
| 111 | phosphor::logging::entry("MSG=%s", |
| 112 | ec.message().c_str())); |
| 113 | return; |
| 114 | } |
Chalapathi Venkataramashetty | 55e7934 | 2021-03-29 10:17:48 +0000 | [diff] [blame] | 115 | |
Patrick Williams | 304e82e | 2023-05-10 07:51:13 -0500 | [diff] [blame] | 116 | const uint64_t* i2cBus = nullptr; |
| 117 | const uint64_t* address = nullptr; |
Chalapathi Venkataramashetty | 55e7934 | 2021-03-29 10:17:48 +0000 | [diff] [blame] | 118 | |
Patrick Williams | 304e82e | 2023-05-10 07:51:13 -0500 | [diff] [blame] | 119 | for (const auto& [propName, propVariant] : propertiesList) |
| 120 | { |
| 121 | if (propName == "Address") |
| 122 | { |
| 123 | address = std::get_if<uint64_t>(&propVariant); |
| 124 | } |
| 125 | else if (propName == "Bus") |
| 126 | { |
| 127 | i2cBus = std::get_if<uint64_t>(&propVariant); |
| 128 | } |
| 129 | } |
Chalapathi Venkataramashetty | 55e7934 | 2021-03-29 10:17:48 +0000 | [diff] [blame] | 130 | |
Patrick Williams | 304e82e | 2023-05-10 07:51:13 -0500 | [diff] [blame] | 131 | if ((address == nullptr) || (i2cBus == nullptr)) |
| 132 | { |
| 133 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 134 | "Unable to read the pfr properties"); |
| 135 | return; |
| 136 | } |
Chalapathi Venkataramashetty | 55e7934 | 2021-03-29 10:17:48 +0000 | [diff] [blame] | 137 | |
Patrick Williams | 304e82e | 2023-05-10 07:51:13 -0500 | [diff] [blame] | 138 | i2cBusNumber = static_cast<int>(*i2cBus); |
| 139 | i2cSlaveAddress = static_cast<int>(*address); |
| 140 | i2cConfigLoaded = true; |
Patrick Williams | 1388f6c | 2023-10-20 11:19:41 -0500 | [diff] [blame] | 141 | }, |
Patrick Williams | 304e82e | 2023-05-10 07:51:13 -0500 | [diff] [blame] | 142 | serviceName, objPath, "org.freedesktop.DBus.Properties", |
| 143 | "GetAll", "xyz.openbmc_project.Configuration.PFR"); |
| 144 | } |
Patrick Williams | 1388f6c | 2023-10-20 11:19:41 -0500 | [diff] [blame] | 145 | }, |
Chalapathi Venkataramashetty | 55e7934 | 2021-03-29 10:17:48 +0000 | [diff] [blame] | 146 | "xyz.openbmc_project.ObjectMapper", |
| 147 | "/xyz/openbmc_project/object_mapper", |
| 148 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", |
| 149 | "/xyz/openbmc_project/inventory/system", 0, |
| 150 | std::array<const char*, 1>{"xyz.openbmc_project.Configuration.PFR"}); |
| 151 | return; |
| 152 | } |
| 153 | |
AppaRao Puli | dbe184d | 2019-10-09 18:04:22 +0530 | [diff] [blame] | 154 | std::string toHexString(const uint8_t val) |
AppaRao Puli | c532f55 | 2019-07-05 15:23:50 +0530 | [diff] [blame] | 155 | { |
| 156 | std::stringstream stream; |
AppaRao Puli | dbe184d | 2019-10-09 18:04:22 +0530 | [diff] [blame] | 157 | stream << std::setfill('0') << std::setw(2) << std::hex |
| 158 | << static_cast<int>(val); |
AppaRao Puli | c532f55 | 2019-07-05 15:23:50 +0530 | [diff] [blame] | 159 | return stream.str(); |
| 160 | } |
| 161 | |
Chalapathi Venkataramashetty | 53b8700 | 2020-07-03 09:52:44 +0000 | [diff] [blame] | 162 | static std::string readCPLDHash() |
| 163 | { |
| 164 | std::stringstream hashStrStream; |
| 165 | static constexpr uint8_t hashLength = 32; |
| 166 | std::array<uint8_t, hashLength> hashValue = {0}; |
| 167 | try |
| 168 | { |
| 169 | I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC); |
| 170 | if (cpldDev.i2cReadBlockData(CPLDHashRegStart, hashLength, |
| 171 | hashValue.data())) |
| 172 | { |
| 173 | for (const auto& i : hashValue) |
| 174 | { |
| 175 | hashStrStream << std::setfill('0') << std::setw(2) << std::hex |
| 176 | << static_cast<int>(i); |
| 177 | } |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | // read failed |
| 182 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 183 | "Failed to read CPLD Hash string"); |
| 184 | return ""; |
| 185 | } |
| 186 | } |
| 187 | catch (const std::exception& e) |
| 188 | { |
| 189 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 190 | "Exception caught in readCPLDHash.", |
| 191 | phosphor::logging::entry("MSG=%s", e.what())); |
| 192 | return ""; |
| 193 | } |
| 194 | return hashStrStream.str(); |
| 195 | } |
| 196 | |
AppaRao Puli | 4b639a2 | 2019-10-01 18:12:59 +0530 | [diff] [blame] | 197 | static std::string readVersionFromCPLD(const uint8_t majorReg, |
| 198 | const uint8_t minorReg) |
AppaRao Puli | c532f55 | 2019-07-05 15:23:50 +0530 | [diff] [blame] | 199 | { |
| 200 | try |
| 201 | { |
AppaRao Puli | c532f55 | 2019-07-05 15:23:50 +0530 | [diff] [blame] | 202 | I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC); |
| 203 | uint8_t majorVer = cpldDev.i2cReadByteData(majorReg); |
| 204 | uint8_t minorVer = cpldDev.i2cReadByteData(minorReg); |
AppaRao Puli | 90fb45c | 2020-01-21 16:50:53 +0530 | [diff] [blame] | 205 | // Major and Minor versions should be binary encoded strings. |
Patrick Williams | 304e82e | 2023-05-10 07:51:13 -0500 | [diff] [blame] | 206 | std::string version = std::to_string(majorVer) + "." + |
| 207 | std::to_string(minorVer); |
AppaRao Puli | c532f55 | 2019-07-05 15:23:50 +0530 | [diff] [blame] | 208 | return version; |
| 209 | } |
| 210 | catch (const std::exception& e) |
| 211 | { |
| 212 | phosphor::logging::log<phosphor::logging::level::ERR>( |
AppaRao Puli | 4b639a2 | 2019-10-01 18:12:59 +0530 | [diff] [blame] | 213 | "Exception caught in readVersionFromCPLD.", |
AppaRao Puli | c532f55 | 2019-07-05 15:23:50 +0530 | [diff] [blame] | 214 | phosphor::logging::entry("MSG=%s", e.what())); |
| 215 | return ""; |
| 216 | } |
| 217 | } |
| 218 | |
AppaRao Puli | 4b639a2 | 2019-10-01 18:12:59 +0530 | [diff] [blame] | 219 | static std::string readBMCVersionFromSPI(const ImageType& imgType) |
| 220 | { |
| 221 | std::string mtdDev; |
| 222 | uint32_t verOffset = verOffsetInPFM; |
| 223 | uint32_t bldNumOffset = buildNumOffsetInPFM; |
| 224 | uint32_t bldHashOffset = buildHashOffsetInPFM; |
| 225 | |
| 226 | if (imgType == ImageType::bmcActive) |
| 227 | { |
| 228 | // For Active image, PFM is emulated as separate MTD device. |
| 229 | mtdDev = bmcActiveImgPfmMTDDev; |
| 230 | } |
| 231 | else if (imgType == ImageType::bmcRecovery) |
| 232 | { |
| 233 | // For Recovery image, PFM is part of compressed Image |
| 234 | // at offset 0x400. |
| 235 | mtdDev = bmcRecoveryImgMTDDev; |
| 236 | verOffset += pfmBaseOffsetInImage; |
| 237 | bldNumOffset += pfmBaseOffsetInImage; |
| 238 | bldHashOffset += pfmBaseOffsetInImage; |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 243 | "Invalid image type passed to readBMCVersionFromSPI."); |
| 244 | return ""; |
| 245 | } |
| 246 | |
| 247 | uint8_t buildNo = 0; |
| 248 | std::array<uint8_t, 2> ver; |
| 249 | std::array<uint8_t, 3> buildHash; |
| 250 | |
| 251 | try |
| 252 | { |
| 253 | SPIDev spiDev(mtdDev); |
| 254 | spiDev.spiReadData(verOffset, ver.size(), |
| 255 | reinterpret_cast<void*>(ver.data())); |
| 256 | spiDev.spiReadData(bldNumOffset, sizeof(buildNo), |
| 257 | reinterpret_cast<void*>(&buildNo)); |
| 258 | spiDev.spiReadData(bldHashOffset, buildHash.size(), |
| 259 | reinterpret_cast<void*>(buildHash.data())); |
| 260 | } |
| 261 | catch (const std::exception& e) |
| 262 | { |
| 263 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 264 | "Exception caught in readBMCVersionFromSPI.", |
| 265 | phosphor::logging::entry("MSG=%s", e.what())); |
| 266 | return ""; |
| 267 | } |
AppaRao Puli | 90fb45c | 2020-01-21 16:50:53 +0530 | [diff] [blame] | 268 | |
| 269 | // Version format: <major>.<minor>-<build bum>-g<build hash> |
| 270 | // Example: 0.11-7-g1e5c2d |
| 271 | // Major, minor and build numberare BCD encoded. |
| 272 | std::string version = |
| 273 | std::to_string(ver[0]) + "." + std::to_string(ver[1]) + "-" + |
| 274 | std::to_string(buildNo) + "-g" + toHexString(buildHash[0]) + |
| 275 | toHexString(buildHash[1]) + toHexString(buildHash[2]); |
AppaRao Puli | 4b639a2 | 2019-10-01 18:12:59 +0530 | [diff] [blame] | 276 | return version; |
| 277 | } |
| 278 | |
Chalapathi Venkataramashetty | 53b8700 | 2020-07-03 09:52:44 +0000 | [diff] [blame] | 279 | static bool getGPIOInput(const std::string& name, gpiod::line& gpioLine, |
| 280 | uint8_t* value) |
| 281 | { |
| 282 | // Find the GPIO line |
| 283 | gpioLine = gpiod::find_line(name); |
| 284 | if (!gpioLine) |
| 285 | { |
| 286 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 287 | "Failed to find the GPIO line: ", |
| 288 | phosphor::logging::entry("MSG=%s", name.c_str())); |
| 289 | return false; |
| 290 | } |
| 291 | try |
| 292 | { |
| 293 | gpioLine.request({__FUNCTION__, gpiod::line_request::DIRECTION_INPUT}); |
| 294 | } |
Patrick Williams | 4990c9e | 2021-10-06 14:40:02 -0500 | [diff] [blame] | 295 | catch (const std::exception& e) |
Chalapathi Venkataramashetty | 53b8700 | 2020-07-03 09:52:44 +0000 | [diff] [blame] | 296 | { |
| 297 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 298 | "Failed to request the GPIO line", |
| 299 | phosphor::logging::entry("MSG=%s", e.what())); |
| 300 | gpioLine.release(); |
| 301 | return false; |
| 302 | } |
| 303 | try |
| 304 | { |
| 305 | *value = gpioLine.get_value(); |
| 306 | } |
Patrick Williams | 4990c9e | 2021-10-06 14:40:02 -0500 | [diff] [blame] | 307 | catch (const std::exception& e) |
Chalapathi Venkataramashetty | 53b8700 | 2020-07-03 09:52:44 +0000 | [diff] [blame] | 308 | { |
| 309 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 310 | "Failed to get the value of GPIO line", |
| 311 | phosphor::logging::entry("MSG=%s", e.what())); |
| 312 | gpioLine.release(); |
| 313 | return false; |
| 314 | } |
| 315 | gpioLine.release(); |
| 316 | return true; |
| 317 | } |
| 318 | |
Chalapathi Venkataramashetty | e6fb18e | 2021-02-03 14:51:00 +0000 | [diff] [blame] | 319 | std::string readCPLDVersion() |
Chalapathi Venkataramashetty | 53b8700 | 2020-07-03 09:52:44 +0000 | [diff] [blame] | 320 | { |
Chalapathi Venkataramashetty | 53b8700 | 2020-07-03 09:52:44 +0000 | [diff] [blame] | 321 | std::string svnRoTHash = ""; |
ankita prasad | 56b0e66 | 2023-08-09 12:46:54 +0000 | [diff] [blame] | 322 | std::string version = "unknown"; |
Chalapathi Venkataramashetty | 53b8700 | 2020-07-03 09:52:44 +0000 | [diff] [blame] | 323 | |
| 324 | // check if reg 0x00 read 0xde |
| 325 | uint8_t cpldRoTValue = 0; |
Zhikui Ren | ace48a7 | 2020-08-12 15:54:42 -0700 | [diff] [blame] | 326 | try |
| 327 | { |
| 328 | I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC); |
| 329 | cpldRoTValue = cpldDev.i2cReadByteData(pfrROTId); |
| 330 | } |
| 331 | catch (const std::exception& e) |
| 332 | { |
| 333 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 334 | "Exception caught in readCPLDVersion.", |
| 335 | phosphor::logging::entry("MSG=%s", e.what())); |
| 336 | } |
Chalapathi Venkataramashetty | 53b8700 | 2020-07-03 09:52:44 +0000 | [diff] [blame] | 337 | |
| 338 | if (cpldRoTValue == pfrRoTValue) |
| 339 | { |
ankita prasad | 56b0e66 | 2023-08-09 12:46:54 +0000 | [diff] [blame] | 340 | // read RoT Rev and RoT SVN |
| 341 | std::string rotRevSVN = readVersionFromCPLD(cpldROTVersion, cpldROTSvn); |
Chalapathi Venkataramashetty | 53b8700 | 2020-07-03 09:52:44 +0000 | [diff] [blame] | 342 | |
| 343 | // read CPLD hash |
| 344 | std::string cpldHash = readCPLDHash(); |
ankita prasad | 56b0e66 | 2023-08-09 12:46:54 +0000 | [diff] [blame] | 345 | version = rotRevSVN + "-" + cpldHash; |
Chalapathi Venkataramashetty | 53b8700 | 2020-07-03 09:52:44 +0000 | [diff] [blame] | 346 | } |
| 347 | else |
| 348 | { |
| 349 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 350 | "PFR-CPLD not present."); |
| 351 | } |
| 352 | |
ankita prasad | 56b0e66 | 2023-08-09 12:46:54 +0000 | [diff] [blame] | 353 | // ROT FW Version format: |
Chalapathi Venkataramashetty | 53b8700 | 2020-07-03 09:52:44 +0000 | [diff] [blame] | 354 | // When PFR CPLD is present |
ankita prasad | 56b0e66 | 2023-08-09 12:46:54 +0000 | [diff] [blame] | 355 | // <RoTRev.RoTSVN>-<RoT HASH> |
| 356 | // Example: 3.0-<Hash string> |
Chalapathi Venkataramashetty | 53b8700 | 2020-07-03 09:52:44 +0000 | [diff] [blame] | 357 | |
Chalapathi Venkataramashetty | 53b8700 | 2020-07-03 09:52:44 +0000 | [diff] [blame] | 358 | return version; |
| 359 | } |
| 360 | |
AppaRao Puli | 4b639a2 | 2019-10-01 18:12:59 +0530 | [diff] [blame] | 361 | std::string getFirmwareVersion(const ImageType& imgType) |
| 362 | { |
| 363 | switch (imgType) |
| 364 | { |
Vikram Bodireddy | 3c6c8c3 | 2019-12-05 11:06:15 +0530 | [diff] [blame] | 365 | case (ImageType::cpldActive): |
Chalapathi Venkataramashetty | 53b8700 | 2020-07-03 09:52:44 +0000 | [diff] [blame] | 366 | { |
| 367 | return readCPLDVersion(); |
| 368 | } |
Vikram Bodireddy | 3c6c8c3 | 2019-12-05 11:06:15 +0530 | [diff] [blame] | 369 | case (ImageType::cpldRecovery): |
AppaRao Puli | 4b639a2 | 2019-10-01 18:12:59 +0530 | [diff] [blame] | 370 | { |
Vikram Bodireddy | 3c6c8c3 | 2019-12-05 11:06:15 +0530 | [diff] [blame] | 371 | // TO-DO: Need to update once CPLD supported Firmware is available |
AppaRao Puli | 4b639a2 | 2019-10-01 18:12:59 +0530 | [diff] [blame] | 372 | return readVersionFromCPLD(cpldROTVersion, cpldROTSvn); |
| 373 | } |
| 374 | case (ImageType::biosActive): |
| 375 | { |
| 376 | return readVersionFromCPLD(pchActiveMajorVersion, |
| 377 | pchActiveMinorVersion); |
| 378 | } |
| 379 | case (ImageType::biosRecovery): |
| 380 | { |
| 381 | return readVersionFromCPLD(pchRecoveryMajorVersion, |
| 382 | pchRecoveryMinorVersion); |
| 383 | } |
| 384 | case (ImageType::bmcActive): |
| 385 | case (ImageType::bmcRecovery): |
| 386 | { |
| 387 | return readBMCVersionFromSPI(imgType); |
| 388 | } |
Vikram Bodireddy | 8292dc6 | 2021-05-26 13:31:47 +0530 | [diff] [blame] | 389 | case (ImageType::afmActive): |
| 390 | { |
| 391 | return readVersionFromCPLD(afmActiveMajorVersion, |
| 392 | afmActiveMinorVersion); |
| 393 | } |
| 394 | case (ImageType::afmRecovery): |
| 395 | { |
| 396 | return readVersionFromCPLD(afmRecoveryMajorVersion, |
| 397 | afmRecoveryMinorVersion); |
| 398 | } |
AppaRao Puli | 4b639a2 | 2019-10-01 18:12:59 +0530 | [diff] [blame] | 399 | default: |
| 400 | // Invalid image Type. |
| 401 | return ""; |
| 402 | } |
| 403 | } |
| 404 | |
Chalapathi Venkataramashetty | f881970 | 2021-02-03 09:43:46 +0000 | [diff] [blame] | 405 | int getProvisioningStatus(bool& ufmLocked, bool& ufmProvisioned, |
| 406 | bool& ufmSupport) |
AppaRao Puli | c532f55 | 2019-07-05 15:23:50 +0530 | [diff] [blame] | 407 | { |
| 408 | try |
| 409 | { |
| 410 | I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC); |
| 411 | uint8_t provStatus = cpldDev.i2cReadByteData(provisioningStatus); |
Chalapathi Venkataramashetty | f881970 | 2021-02-03 09:43:46 +0000 | [diff] [blame] | 412 | uint8_t pfrRoT = cpldDev.i2cReadByteData(pfrROTId); |
AppaRao Puli | c532f55 | 2019-07-05 15:23:50 +0530 | [diff] [blame] | 413 | ufmLocked = (provStatus & ufmLockedMask); |
| 414 | ufmProvisioned = (provStatus & ufmProvisionedMask); |
Chalapathi Venkataramashetty | f881970 | 2021-02-03 09:43:46 +0000 | [diff] [blame] | 415 | ufmSupport = (pfrRoT & pfrRoTValue); |
AppaRao Puli | c532f55 | 2019-07-05 15:23:50 +0530 | [diff] [blame] | 416 | return 0; |
| 417 | } |
| 418 | catch (const std::exception& e) |
| 419 | { |
| 420 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 421 | "Exception caught in getProvisioningStatus.", |
| 422 | phosphor::logging::entry("MSG=%s", e.what())); |
| 423 | return -1; |
| 424 | } |
| 425 | } |
| 426 | |
Zhikui Ren | c96f37d | 2021-12-08 15:40:51 -0800 | [diff] [blame] | 427 | int getPlatformState(uint8_t& state) |
| 428 | { |
| 429 | try |
| 430 | { |
| 431 | I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC); |
| 432 | state = cpldDev.i2cReadByteData(platformState); |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | catch (const std::exception& e) |
| 437 | { |
| 438 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 439 | "Exception caught in getPlatformState.", |
| 440 | phosphor::logging::entry("MSG=%s", e.what())); |
| 441 | return -1; |
| 442 | } |
| 443 | } |
| 444 | |
AppaRao Puli | dbe184d | 2019-10-09 18:04:22 +0530 | [diff] [blame] | 445 | int readCpldReg(const ActionType& action, uint8_t& value) |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 446 | { |
| 447 | uint8_t cpldReg; |
| 448 | |
| 449 | switch (action) |
| 450 | { |
Chalapathi Venkataramashetty | bcc7ce1 | 2021-05-17 04:27:21 +0000 | [diff] [blame] | 451 | case (ActionType::readRoTRev): |
| 452 | cpldReg = cpldROTVersion; |
| 453 | break; |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 454 | case (ActionType::recoveryCount): |
| 455 | cpldReg = recoveryCount; |
| 456 | break; |
| 457 | case (ActionType::recoveryReason): |
| 458 | cpldReg = lastRecoveryReason; |
| 459 | break; |
| 460 | case (ActionType::panicCount): |
| 461 | cpldReg = panicEventCount; |
| 462 | break; |
| 463 | case (ActionType::panicReason): |
| 464 | cpldReg = panicEventReason; |
| 465 | break; |
| 466 | case (ActionType::majorError): |
| 467 | cpldReg = majorErrorCode; |
| 468 | break; |
| 469 | case (ActionType::minorError): |
| 470 | cpldReg = minorErrorCode; |
| 471 | break; |
| 472 | |
| 473 | default: |
| 474 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 475 | "Invalid CPLD read action."); |
| 476 | return -1; |
| 477 | } |
| 478 | |
| 479 | try |
| 480 | { |
| 481 | I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC); |
| 482 | value = cpldDev.i2cReadByteData(cpldReg); |
| 483 | return 0; |
| 484 | } |
| 485 | catch (const std::exception& e) |
| 486 | { |
Chalapathi Venkataramashetty | 00acaff | 2020-12-24 11:01:17 +0000 | [diff] [blame] | 487 | if (exceptionFlag) |
| 488 | { |
| 489 | exceptionFlag = false; |
| 490 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 491 | "Exception caught in readCpldReg.", |
| 492 | phosphor::logging::entry("MSG=%s", e.what())); |
| 493 | } |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 494 | return -1; |
| 495 | } |
| 496 | } |
| 497 | |
sri anjaneyulu alapati | be13259 | 2023-07-17 10:16:39 +0000 | [diff] [blame] | 498 | int setBMCBootCompleteChkPoint(const uint8_t checkPoint) |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 499 | { |
Vikram Bodireddy | 0884191 | 2020-08-31 18:11:45 +0530 | [diff] [blame] | 500 | uint8_t bmcBootCheckpointReg = bmcBootCheckpoint; |
| 501 | |
| 502 | // check if reg 0x01(RoTRev) is 1 or 2. |
| 503 | // checkpoint register changes for RoT Rev 1 and 2 |
| 504 | uint8_t cpldRoTRev = 0; |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 505 | try |
| 506 | { |
| 507 | I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC); |
Vikram Bodireddy | 0884191 | 2020-08-31 18:11:45 +0530 | [diff] [blame] | 508 | cpldRoTRev = cpldDev.i2cReadByteData(cpldROTVersion); |
| 509 | } |
| 510 | catch (const std::exception& e) |
| 511 | { |
| 512 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 513 | "Exception caught in reading RoT rev.", |
| 514 | phosphor::logging::entry("MSG=%s", e.what())); |
| 515 | return -1; |
| 516 | } |
| 517 | |
| 518 | // latest PFR has different check point register |
| 519 | if (cpldRoTRev <= 1) |
| 520 | { |
| 521 | bmcBootCheckpointReg = bmcBootCheckpointRev1; |
| 522 | } |
| 523 | |
| 524 | try |
| 525 | { |
| 526 | I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC); |
| 527 | cpldDev.i2cWriteByteData(bmcBootCheckpointReg, checkPoint); |
AppaRao Puli | b7e172c | 2019-12-13 14:46:25 +0530 | [diff] [blame] | 528 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 529 | "Successfully set the PFR CPLD checkpoint 9."); |
sri anjaneyulu alapati | be13259 | 2023-07-17 10:16:39 +0000 | [diff] [blame] | 530 | bmcBootCompleteChkPointDone = true; |
| 531 | if (unProvChkPointStatus) |
| 532 | { |
| 533 | unProvChkPointStatus = false; |
| 534 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 535 | "PFR is not provisioned, hence exit the service."); |
| 536 | std::exit(EXIT_SUCCESS); |
| 537 | } |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 538 | return 0; |
| 539 | } |
| 540 | catch (const std::exception& e) |
| 541 | { |
| 542 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 543 | "Exception caught in setBMCBootCheckout.", |
| 544 | phosphor::logging::entry("MSG=%s", e.what())); |
| 545 | return -1; |
| 546 | } |
| 547 | } |
| 548 | |
deepak kumar agrawal | b29a15c | 2022-03-25 07:45:57 +0000 | [diff] [blame] | 549 | static bool setMBRegister(uint32_t regOffset, uint8_t regValue) |
| 550 | { |
| 551 | try |
| 552 | { |
| 553 | I2CFile mailDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC); |
| 554 | mailDev.i2cWriteByteData(regOffset, regValue); |
| 555 | return true; |
| 556 | } |
| 557 | catch (const std::exception& e) |
| 558 | { |
| 559 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 560 | "Exception caught in setting PFR Mailbox.", |
| 561 | phosphor::logging::entry("MSG=%s", e.what())); |
| 562 | return false; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | int setBMCBusy(bool setValue) |
| 567 | { |
| 568 | uint32_t bmcBusyReg = 0x63; |
Jayaprakash Mutyala | ca8aacc | 2024-05-07 07:16:20 +0000 | [diff] [blame] | 569 | uint8_t valHigh = 0x80; |
deepak kumar agrawal | b29a15c | 2022-03-25 07:45:57 +0000 | [diff] [blame] | 570 | uint8_t mailBoxReply = 0; |
| 571 | |
| 572 | if (getMBRegister(bmcBusyReg, mailBoxReply)) |
| 573 | { |
| 574 | return -1; |
| 575 | } |
| 576 | uint8_t readValue = mailBoxReply | valHigh; |
| 577 | if (setValue == false) |
| 578 | { |
Jayaprakash Mutyala | ca8aacc | 2024-05-07 07:16:20 +0000 | [diff] [blame] | 579 | readValue &= 0b01111111; |
deepak kumar agrawal | b29a15c | 2022-03-25 07:45:57 +0000 | [diff] [blame] | 580 | } |
| 581 | if (!setMBRegister(bmcBusyReg, readValue)) |
| 582 | { |
| 583 | return -1; |
| 584 | } |
| 585 | if (setValue == false) |
| 586 | { |
| 587 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 588 | "Successfully reset the PFR MailBox register."); |
| 589 | } |
| 590 | else |
| 591 | { |
| 592 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 593 | "Successfully set the PFR MailBox to BMCBusy."); |
| 594 | } |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | int getMBRegister(uint32_t regAddr, uint8_t& mailBoxReply) |
| 599 | { |
| 600 | // Read from PFR CPLD's mailbox register |
| 601 | try |
| 602 | { |
| 603 | I2CFile mailReadDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC); |
| 604 | |
| 605 | mailBoxReply = mailReadDev.i2cReadByteData(regAddr); |
| 606 | } |
| 607 | catch (const std::exception& e) |
| 608 | { |
| 609 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 610 | "Exception caught in mailbox reading.", |
| 611 | phosphor::logging::entry("MSG=%s", e.what())); |
| 612 | throw; |
| 613 | } |
| 614 | return 0; |
| 615 | } |
| 616 | |
AppaRao Puli | c532f55 | 2019-07-05 15:23:50 +0530 | [diff] [blame] | 617 | } // namespace pfr |