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> |
Anupama B R | 08fa59e | 2025-03-06 22:55:11 -0600 | [diff] [blame] | 10 | #include <utility/json_utility.hpp> |
| 11 | |
| 12 | namespace vpd |
| 13 | { |
| 14 | constexpr auto pimPersistVsbpPath = |
| 15 | "/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] | 16 | constexpr auto IM_SIZE_IN_BYTES = 0x04; |
| 17 | constexpr auto IM_KW_VALUE_OFFSET = 0x000005fb; |
Anupama B R | 08fa59e | 2025-03-06 22:55:11 -0600 | [diff] [blame] | 18 | |
| 19 | std::string SingleFab::getImFromPersistedLocation() const noexcept |
| 20 | { |
| 21 | try |
| 22 | { |
| 23 | auto l_parsedVsbpJsonObj = |
| 24 | jsonUtility::getParsedJson(pimPersistVsbpPath); |
| 25 | if (!l_parsedVsbpJsonObj.contains("value0") || |
| 26 | !l_parsedVsbpJsonObj["value0"].contains(constants::kwdIM) || |
| 27 | !l_parsedVsbpJsonObj["value0"][constants::kwdIM].is_array()) |
| 28 | { |
| 29 | throw std::runtime_error( |
| 30 | "Json is empty or mandatory tag(s) missing from JSON"); |
| 31 | } |
| 32 | |
| 33 | const types::BinaryVector l_imValue = |
| 34 | l_parsedVsbpJsonObj["value0"][constants::kwdIM] |
| 35 | .get<types::BinaryVector>(); |
| 36 | |
| 37 | std::ostringstream l_imData; |
| 38 | for (const auto& l_byte : l_imValue) |
| 39 | { |
| 40 | l_imData << std::setw(2) << std::setfill('0') << std::hex |
| 41 | << static_cast<int>(l_byte); |
| 42 | } |
| 43 | return l_imData.str(); |
| 44 | } |
| 45 | catch (const std::exception& l_ex) |
RekhaAparna01 | d3e693e | 2025-03-04 05:08:30 -0600 | [diff] [blame^] | 46 | {} |
Anupama B R | 08fa59e | 2025-03-06 22:55:11 -0600 | [diff] [blame] | 47 | |
| 48 | return std::string(); |
| 49 | } |
Anupama B R | b5bfcbc | 2025-03-03 03:00:04 -0600 | [diff] [blame] | 50 | |
| 51 | std::string SingleFab::getImFromPlanar() const noexcept |
| 52 | { |
| 53 | try |
| 54 | { |
| 55 | types::BinaryVector l_imValue(IM_SIZE_IN_BYTES); |
| 56 | std::fstream l_vpdFileStream; |
| 57 | |
| 58 | l_vpdFileStream.exceptions( |
| 59 | std::ifstream::badbit | std::ifstream::failbit); |
| 60 | |
| 61 | l_vpdFileStream.open(SYSTEM_VPD_FILE_PATH, |
| 62 | std::ios::in | std::ios::binary); |
| 63 | |
| 64 | l_vpdFileStream.seekg(IM_KW_VALUE_OFFSET, std::ios_base::beg); |
| 65 | |
| 66 | // Read keyword value |
| 67 | l_vpdFileStream.read(reinterpret_cast<char*>(&l_imValue[0]), |
| 68 | IM_SIZE_IN_BYTES); |
| 69 | |
| 70 | l_vpdFileStream.clear(std::ios_base::eofbit); |
| 71 | |
| 72 | std::ostringstream l_imData; |
| 73 | for (const auto& l_byte : l_imValue) |
| 74 | { |
| 75 | l_imData << std::setw(2) << std::setfill('0') << std::hex |
| 76 | << static_cast<int>(l_byte); |
| 77 | } |
| 78 | return l_imData.str(); |
| 79 | } |
| 80 | catch (const std::ifstream::failure& l_ex) |
| 81 | {} |
| 82 | |
| 83 | return std::string(); |
| 84 | } |
RekhaAparna01 | d3e693e | 2025-03-04 05:08:30 -0600 | [diff] [blame^] | 85 | |
| 86 | bool SingleFab::setImOnPlanar(const std::string& i_imValue) const noexcept |
| 87 | { |
| 88 | try |
| 89 | { |
| 90 | types::BinaryVector l_imValue; |
| 91 | const std::string l_systemPlanarEepromPath = SYSTEM_VPD_FILE_PATH; |
| 92 | |
| 93 | // Convert string to vector of bytes |
| 94 | for (auto l_value : i_imValue | std::views::chunk(2)) |
| 95 | { |
| 96 | std::string l_byteString(l_value.begin(), l_value.end()); |
| 97 | l_imValue.push_back( |
| 98 | static_cast<uint8_t>(std::stoi(l_byteString, nullptr, 16))); |
| 99 | } |
| 100 | |
| 101 | std::shared_ptr<Parser> l_parserObj = std::make_shared<Parser>( |
| 102 | l_systemPlanarEepromPath, nlohmann::json{}); |
| 103 | |
| 104 | int l_bytes_updated = l_parserObj->updateVpdKeywordOnHardware( |
| 105 | std::make_tuple(constants::recVSBP, constants::kwdIM, l_imValue)); |
| 106 | |
| 107 | return l_bytes_updated > 0 ? true : false; |
| 108 | } |
| 109 | catch (const std::exception& l_ex) |
| 110 | { |
| 111 | return false; |
| 112 | } |
| 113 | } |
Anupama B R | 08fa59e | 2025-03-06 22:55:11 -0600 | [diff] [blame] | 114 | } // namespace vpd |