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; |
| 44 | static constexpr uint8_t pchActiveMajorVersion = 0x17; |
| 45 | static constexpr uint8_t pchActiveMinorVersion = 0x18; |
| 46 | static constexpr uint8_t bmcActiveMajorVersion = 0x19; |
| 47 | static constexpr uint8_t bmcActiveMinorVersion = 0x1A; |
| 48 | static constexpr uint8_t pchRecoveryMajorVersion = 0x1C; |
| 49 | static constexpr uint8_t pchRecoveryMinorVersion = 0x1D; |
| 50 | static constexpr uint8_t bmcRecoveryMajorVersion = 0x1E; |
| 51 | static constexpr uint8_t bmcRecoveryMinorVersion = 0x1F; |
| 52 | |
| 53 | static constexpr uint8_t ufmLockedMask = (0x1 << 0x04); |
| 54 | static constexpr uint8_t ufmProvisionedMask = (0x1 << 0x05); |
| 55 | |
| 56 | template <typename T> std::string int_to_hexstring(T i) |
| 57 | { |
| 58 | std::stringstream stream; |
| 59 | stream << std::setfill('0') << std::setw(sizeof(T) * 2) << std::hex |
| 60 | << static_cast<int>(i); |
| 61 | return stream.str(); |
| 62 | } |
| 63 | |
| 64 | std::string getVersionInfoCPLD(ImageType& imgType) |
| 65 | { |
| 66 | try |
| 67 | { |
| 68 | uint8_t majorReg; |
| 69 | uint8_t minorReg; |
| 70 | |
| 71 | switch (imgType) |
| 72 | { |
| 73 | case (ImageType::cpld): |
| 74 | { |
| 75 | majorReg = cpldROTVersion; |
| 76 | minorReg = cpldROTSvn; |
| 77 | break; |
| 78 | } |
| 79 | case (ImageType::biosActive): |
| 80 | { |
| 81 | majorReg = pchActiveMajorVersion; |
| 82 | minorReg = pchActiveMinorVersion; |
| 83 | break; |
| 84 | } |
| 85 | case (ImageType::biosRecovery): |
| 86 | { |
| 87 | majorReg = pchRecoveryMajorVersion; |
| 88 | minorReg = pchRecoveryMinorVersion; |
| 89 | break; |
| 90 | } |
| 91 | case (ImageType::bmcActive): |
| 92 | { |
| 93 | majorReg = bmcActiveMajorVersion; |
| 94 | minorReg = bmcActiveMinorVersion; |
| 95 | break; |
| 96 | } |
| 97 | case (ImageType::bmcRecovery): |
| 98 | { |
| 99 | majorReg = bmcRecoveryMajorVersion; |
| 100 | minorReg = bmcRecoveryMinorVersion; |
| 101 | break; |
| 102 | } |
| 103 | default: |
| 104 | // Invalid image Type. |
| 105 | return ""; |
| 106 | } |
| 107 | |
| 108 | I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC); |
| 109 | uint8_t majorVer = cpldDev.i2cReadByteData(majorReg); |
| 110 | uint8_t minorVer = cpldDev.i2cReadByteData(minorReg); |
| 111 | std::string version = |
| 112 | int_to_hexstring(majorVer) + "." + int_to_hexstring(minorVer); |
| 113 | return version; |
| 114 | } |
| 115 | catch (const std::exception& e) |
| 116 | { |
| 117 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 118 | "Exception caught in getVersionInfoCPLD.", |
| 119 | phosphor::logging::entry("MSG=%s", e.what())); |
| 120 | return ""; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | int getProvisioningStatus(bool& ufmLocked, bool& ufmProvisioned) |
| 125 | { |
| 126 | try |
| 127 | { |
| 128 | I2CFile cpldDev(i2cBusNumber, i2cSlaveAddress, O_RDWR | O_CLOEXEC); |
| 129 | uint8_t provStatus = cpldDev.i2cReadByteData(provisioningStatus); |
| 130 | ufmLocked = (provStatus & ufmLockedMask); |
| 131 | ufmProvisioned = (provStatus & ufmProvisionedMask); |
| 132 | return 0; |
| 133 | } |
| 134 | catch (const std::exception& e) |
| 135 | { |
| 136 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 137 | "Exception caught in getProvisioningStatus.", |
| 138 | phosphor::logging::entry("MSG=%s", e.what())); |
| 139 | return -1; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | } // namespace pfr |
| 144 | } // namespace intel |