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 | |
| 17 | #include <unistd.h> |
| 18 | #include <iostream> |
| 19 | #include <sstream> |
| 20 | #include <iomanip> |
| 21 | #include "pfr.hpp" |
| 22 | #include "file.hpp" |
| 23 | |
| 24 | namespace intel |
| 25 | { |
| 26 | namespace pfr |
| 27 | { |
| 28 | // TODO: Dynamically pull these values from configuration |
| 29 | // entity-manager, when needed |
| 30 | static constexpr int i2cBusNumber = 4; |
| 31 | static constexpr int i2cSlaveAddress = 0x70; |
| 32 | |
| 33 | // CPLD mailbox registers |
| 34 | static constexpr uint8_t cpldROTVersion = 0x01; |
| 35 | static constexpr uint8_t cpldROTSvn = 0x02; |
| 36 | static constexpr uint8_t platformState = 0x03; |
| 37 | static constexpr uint8_t recoveryCount = 0x04; |
| 38 | static constexpr uint8_t lastRecoveryReason = 0x05; |
| 39 | static constexpr uint8_t panicEventCount = 0x06; |
| 40 | static constexpr uint8_t panicEventReason = 0x07; |
| 41 | static constexpr uint8_t majorErrorCode = 0x08; |
| 42 | static constexpr uint8_t minorErrorCode = 0x09; |
| 43 | static constexpr uint8_t provisioningStatus = 0x0A; |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 44 | static constexpr uint8_t bmcBootCheckpoint = 0x0F; |
AppaRao Puli | a04c825 | 2019-07-31 22:59:28 +0530 | [diff] [blame] | 45 | static constexpr uint8_t pchActiveMajorVersion = 0x15; |
| 46 | static constexpr uint8_t pchActiveMinorVersion = 0x16; |
| 47 | static constexpr uint8_t bmcActiveMajorVersion = 0x18; |
| 48 | static constexpr uint8_t bmcActiveMinorVersion = 0x19; |
| 49 | static constexpr uint8_t pchRecoveryMajorVersion = 0x1B; |
| 50 | static constexpr uint8_t pchRecoveryMinorVersion = 0x1C; |
AppaRao Puli | c532f55 | 2019-07-05 15:23:50 +0530 | [diff] [blame] | 51 | static constexpr uint8_t bmcRecoveryMajorVersion = 0x1E; |
| 52 | static constexpr uint8_t bmcRecoveryMinorVersion = 0x1F; |
| 53 | |
| 54 | static constexpr uint8_t ufmLockedMask = (0x1 << 0x04); |
| 55 | static constexpr uint8_t ufmProvisionedMask = (0x1 << 0x05); |
| 56 | |
| 57 | template <typename T> std::string int_to_hexstring(T i) |
| 58 | { |
| 59 | std::stringstream stream; |
| 60 | stream << std::setfill('0') << std::setw(sizeof(T) * 2) << std::hex |
| 61 | << static_cast<int>(i); |
| 62 | return stream.str(); |
| 63 | } |
| 64 | |
| 65 | std::string getVersionInfoCPLD(ImageType& imgType) |
| 66 | { |
| 67 | try |
| 68 | { |
| 69 | uint8_t majorReg; |
| 70 | uint8_t minorReg; |
| 71 | |
| 72 | switch (imgType) |
| 73 | { |
| 74 | case (ImageType::cpld): |
| 75 | { |
| 76 | majorReg = cpldROTVersion; |
| 77 | minorReg = cpldROTSvn; |
| 78 | break; |
| 79 | } |
| 80 | case (ImageType::biosActive): |
| 81 | { |
| 82 | majorReg = pchActiveMajorVersion; |
| 83 | minorReg = pchActiveMinorVersion; |
| 84 | break; |
| 85 | } |
| 86 | case (ImageType::biosRecovery): |
| 87 | { |
| 88 | majorReg = pchRecoveryMajorVersion; |
| 89 | minorReg = pchRecoveryMinorVersion; |
| 90 | break; |
| 91 | } |
| 92 | case (ImageType::bmcActive): |
| 93 | { |
| 94 | majorReg = bmcActiveMajorVersion; |
| 95 | minorReg = bmcActiveMinorVersion; |
| 96 | break; |
| 97 | } |
| 98 | case (ImageType::bmcRecovery): |
| 99 | { |
| 100 | majorReg = bmcRecoveryMajorVersion; |
| 101 | minorReg = bmcRecoveryMinorVersion; |
| 102 | break; |
| 103 | } |
| 104 | default: |
| 105 | // Invalid image Type. |
| 106 | return ""; |
| 107 | } |
| 108 | |
| 109 | I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC); |
| 110 | uint8_t majorVer = cpldDev.i2cReadByteData(majorReg); |
| 111 | uint8_t minorVer = cpldDev.i2cReadByteData(minorReg); |
| 112 | std::string version = |
| 113 | int_to_hexstring(majorVer) + "." + int_to_hexstring(minorVer); |
| 114 | return version; |
| 115 | } |
| 116 | catch (const std::exception& e) |
| 117 | { |
| 118 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 119 | "Exception caught in getVersionInfoCPLD.", |
| 120 | phosphor::logging::entry("MSG=%s", e.what())); |
| 121 | return ""; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | int getProvisioningStatus(bool& ufmLocked, bool& ufmProvisioned) |
| 126 | { |
| 127 | try |
| 128 | { |
| 129 | I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC); |
| 130 | uint8_t provStatus = cpldDev.i2cReadByteData(provisioningStatus); |
| 131 | ufmLocked = (provStatus & ufmLockedMask); |
| 132 | ufmProvisioned = (provStatus & ufmProvisionedMask); |
| 133 | return 0; |
| 134 | } |
| 135 | catch (const std::exception& e) |
| 136 | { |
| 137 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 138 | "Exception caught in getProvisioningStatus.", |
| 139 | phosphor::logging::entry("MSG=%s", e.what())); |
| 140 | return -1; |
| 141 | } |
| 142 | } |
| 143 | |
AppaRao Puli | 88aa33b | 2019-07-18 23:49:55 +0530 | [diff] [blame] | 144 | int readCpldReg(const ActionType& action, uint8_t value) |
| 145 | { |
| 146 | uint8_t cpldReg; |
| 147 | |
| 148 | switch (action) |
| 149 | { |
| 150 | case (ActionType::recoveryCount): |
| 151 | cpldReg = recoveryCount; |
| 152 | break; |
| 153 | case (ActionType::recoveryReason): |
| 154 | cpldReg = lastRecoveryReason; |
| 155 | break; |
| 156 | case (ActionType::panicCount): |
| 157 | cpldReg = panicEventCount; |
| 158 | break; |
| 159 | case (ActionType::panicReason): |
| 160 | cpldReg = panicEventReason; |
| 161 | break; |
| 162 | case (ActionType::majorError): |
| 163 | cpldReg = majorErrorCode; |
| 164 | break; |
| 165 | case (ActionType::minorError): |
| 166 | cpldReg = minorErrorCode; |
| 167 | break; |
| 168 | |
| 169 | default: |
| 170 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 171 | "Invalid CPLD read action."); |
| 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | try |
| 176 | { |
| 177 | I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC); |
| 178 | value = cpldDev.i2cReadByteData(cpldReg); |
| 179 | return 0; |
| 180 | } |
| 181 | catch (const std::exception& e) |
| 182 | { |
| 183 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 184 | "Exception caught in readCpldReg.", |
| 185 | phosphor::logging::entry("MSG=%s", e.what())); |
| 186 | return -1; |
| 187 | } |
| 188 | } |
| 189 | |
AppaRao Puli | 46cead9 | 2019-07-22 16:50:09 +0530 | [diff] [blame] | 190 | int setBMCBootCheckpoint(const uint8_t checkPoint) |
| 191 | { |
| 192 | try |
| 193 | { |
| 194 | I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC); |
| 195 | cpldDev.i2cWriteByteData(bmcBootCheckpoint, checkPoint); |
| 196 | return 0; |
| 197 | } |
| 198 | catch (const std::exception& e) |
| 199 | { |
| 200 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 201 | "Exception caught in setBMCBootCheckout.", |
| 202 | phosphor::logging::entry("MSG=%s", e.what())); |
| 203 | return -1; |
| 204 | } |
| 205 | } |
| 206 | |
AppaRao Puli | c532f55 | 2019-07-05 15:23:50 +0530 | [diff] [blame] | 207 | } // namespace pfr |
| 208 | } // namespace intel |