Anupama B R | b5bfcbc | 2025-03-03 03:00:04 -0600 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Anupama B R | 08fa59e | 2025-03-06 22:55:11 -0600 | [diff] [blame] | 3 | #include "single_fab.hpp" |
| 4 | |
| 5 | #include "constants.hpp" |
RekhaAparna01 | d3e693e | 2025-03-04 05:08:30 -0600 | [diff] [blame] | 6 | #include "parser.hpp" |
Anupama B R | 08fa59e | 2025-03-06 22:55:11 -0600 | [diff] [blame] | 7 | #include "types.hpp" |
| 8 | |
RekhaAparna01 | d3e693e | 2025-03-04 05:08:30 -0600 | [diff] [blame] | 9 | #include <nlohmann/json.hpp> |
RekhaAparna01 | f05f354 | 2025-03-02 22:25:23 -0600 | [diff] [blame] | 10 | #include <utility/common_utility.hpp> |
Anupama B R | 08fa59e | 2025-03-06 22:55:11 -0600 | [diff] [blame] | 11 | #include <utility/json_utility.hpp> |
| 12 | |
| 13 | namespace vpd |
| 14 | { |
| 15 | constexpr auto pimPersistVsbpPath = |
| 16 | "/var/lib/phosphor-inventory-manager/xyz/openbmc_project/inventory/system/chassis/motherboard/com.ibm.ipzvpd.VSBP"; |
Anupama B R | b5bfcbc | 2025-03-03 03:00:04 -0600 | [diff] [blame] | 17 | constexpr auto IM_SIZE_IN_BYTES = 0x04; |
| 18 | constexpr auto IM_KW_VALUE_OFFSET = 0x000005fb; |
Anupama B R | 08fa59e | 2025-03-06 22:55:11 -0600 | [diff] [blame] | 19 | |
| 20 | std::string SingleFab::getImFromPersistedLocation() const noexcept |
| 21 | { |
| 22 | try |
| 23 | { |
| 24 | auto l_parsedVsbpJsonObj = |
| 25 | jsonUtility::getParsedJson(pimPersistVsbpPath); |
| 26 | if (!l_parsedVsbpJsonObj.contains("value0") || |
| 27 | !l_parsedVsbpJsonObj["value0"].contains(constants::kwdIM) || |
| 28 | !l_parsedVsbpJsonObj["value0"][constants::kwdIM].is_array()) |
| 29 | { |
| 30 | throw std::runtime_error( |
| 31 | "Json is empty or mandatory tag(s) missing from JSON"); |
| 32 | } |
| 33 | |
| 34 | const types::BinaryVector l_imValue = |
| 35 | l_parsedVsbpJsonObj["value0"][constants::kwdIM] |
| 36 | .get<types::BinaryVector>(); |
| 37 | |
| 38 | std::ostringstream l_imData; |
| 39 | for (const auto& l_byte : l_imValue) |
| 40 | { |
| 41 | l_imData << std::setw(2) << std::setfill('0') << std::hex |
| 42 | << static_cast<int>(l_byte); |
| 43 | } |
| 44 | return l_imData.str(); |
| 45 | } |
| 46 | catch (const std::exception& l_ex) |
RekhaAparna01 | d3e693e | 2025-03-04 05:08:30 -0600 | [diff] [blame] | 47 | {} |
Anupama B R | 08fa59e | 2025-03-06 22:55:11 -0600 | [diff] [blame] | 48 | |
| 49 | return std::string(); |
| 50 | } |
Anupama B R | b5bfcbc | 2025-03-03 03:00:04 -0600 | [diff] [blame] | 51 | |
| 52 | std::string SingleFab::getImFromPlanar() const noexcept |
| 53 | { |
| 54 | try |
| 55 | { |
| 56 | types::BinaryVector l_imValue(IM_SIZE_IN_BYTES); |
| 57 | std::fstream l_vpdFileStream; |
| 58 | |
| 59 | l_vpdFileStream.exceptions( |
| 60 | std::ifstream::badbit | std::ifstream::failbit); |
| 61 | |
| 62 | l_vpdFileStream.open(SYSTEM_VPD_FILE_PATH, |
| 63 | std::ios::in | std::ios::binary); |
| 64 | |
| 65 | l_vpdFileStream.seekg(IM_KW_VALUE_OFFSET, std::ios_base::beg); |
| 66 | |
| 67 | // Read keyword value |
| 68 | l_vpdFileStream.read(reinterpret_cast<char*>(&l_imValue[0]), |
| 69 | IM_SIZE_IN_BYTES); |
| 70 | |
| 71 | l_vpdFileStream.clear(std::ios_base::eofbit); |
| 72 | |
| 73 | std::ostringstream l_imData; |
| 74 | for (const auto& l_byte : l_imValue) |
| 75 | { |
| 76 | l_imData << std::setw(2) << std::setfill('0') << std::hex |
| 77 | << static_cast<int>(l_byte); |
| 78 | } |
| 79 | return l_imData.str(); |
| 80 | } |
| 81 | catch (const std::ifstream::failure& l_ex) |
| 82 | {} |
| 83 | |
| 84 | return std::string(); |
| 85 | } |
RekhaAparna01 | d3e693e | 2025-03-04 05:08:30 -0600 | [diff] [blame] | 86 | |
| 87 | bool SingleFab::setImOnPlanar(const std::string& i_imValue) const noexcept |
| 88 | { |
| 89 | try |
| 90 | { |
| 91 | types::BinaryVector l_imValue; |
| 92 | const std::string l_systemPlanarEepromPath = SYSTEM_VPD_FILE_PATH; |
| 93 | |
| 94 | // Convert string to vector of bytes |
| 95 | for (auto l_value : i_imValue | std::views::chunk(2)) |
| 96 | { |
| 97 | std::string l_byteString(l_value.begin(), l_value.end()); |
| 98 | l_imValue.push_back( |
| 99 | static_cast<uint8_t>(std::stoi(l_byteString, nullptr, 16))); |
| 100 | } |
| 101 | |
| 102 | std::shared_ptr<Parser> l_parserObj = std::make_shared<Parser>( |
| 103 | l_systemPlanarEepromPath, nlohmann::json{}); |
| 104 | |
| 105 | int l_bytes_updated = l_parserObj->updateVpdKeywordOnHardware( |
| 106 | std::make_tuple(constants::recVSBP, constants::kwdIM, l_imValue)); |
| 107 | |
| 108 | return l_bytes_updated > 0 ? true : false; |
| 109 | } |
| 110 | catch (const std::exception& l_ex) |
| 111 | { |
| 112 | return false; |
| 113 | } |
| 114 | } |
RekhaAparna01 | f05f354 | 2025-03-02 22:25:23 -0600 | [diff] [blame] | 115 | |
| 116 | bool SingleFab::isFieldModeEnabled() const noexcept |
| 117 | { |
| 118 | try |
| 119 | { |
| 120 | std::vector<std::string> l_cmdOutput = |
| 121 | commonUtility::executeCmd("/sbin/fw_printenv -n fieldmode 2>&1"); |
| 122 | |
| 123 | if (l_cmdOutput.size() > 0) |
| 124 | { |
| 125 | commonUtility::toLower(l_cmdOutput[0]); |
| 126 | |
| 127 | return l_cmdOutput[0] == "false" ? false : true; |
| 128 | } |
| 129 | } |
| 130 | catch (const std::exception& l_ex) |
| 131 | {} |
| 132 | |
| 133 | return false; |
| 134 | } |
Souvik Roy | cd828d4 | 2025-03-24 02:29:45 -0500 | [diff] [blame^] | 135 | |
| 136 | bool SingleFab::updateSystemImValueInVpdToP11Series( |
| 137 | std::string i_currentImValuePlanar) const noexcept |
| 138 | { |
| 139 | bool l_retVal{false}; |
| 140 | if (!i_currentImValuePlanar.empty()) |
| 141 | { |
| 142 | // update the IM value to P11 series(6000x). Replace the first character |
| 143 | // of IM value string with '6' |
| 144 | l_retVal = setImOnPlanar(i_currentImValuePlanar.replace( |
| 145 | constants::VALUE_0, constants::VALUE_1, |
| 146 | std::to_string(constants::VALUE_6))); |
| 147 | } |
| 148 | return l_retVal; |
| 149 | } |
Anupama B R | 08fa59e | 2025-03-06 22:55:11 -0600 | [diff] [blame] | 150 | } // namespace vpd |