blob: 12197f24c15043e754724331afd6870187cf1f61 [file] [log] [blame]
Anupama B Rb5bfcbc2025-03-03 03:00:04 -06001#include "config.h"
2
Anupama B R08fa59e2025-03-06 22:55:11 -06003#include "single_fab.hpp"
4
5#include "constants.hpp"
RekhaAparna01d3e693e2025-03-04 05:08:30 -06006#include "parser.hpp"
Anupama B R08fa59e2025-03-06 22:55:11 -06007#include "types.hpp"
8
RekhaAparna01d3e693e2025-03-04 05:08:30 -06009#include <nlohmann/json.hpp>
RekhaAparna01f05f3542025-03-02 22:25:23 -060010#include <utility/common_utility.hpp>
Anupama B R08fa59e2025-03-06 22:55:11 -060011#include <utility/json_utility.hpp>
12
13namespace vpd
14{
15constexpr auto pimPersistVsbpPath =
16 "/var/lib/phosphor-inventory-manager/xyz/openbmc_project/inventory/system/chassis/motherboard/com.ibm.ipzvpd.VSBP";
Anupama B Rb5bfcbc2025-03-03 03:00:04 -060017constexpr auto IM_SIZE_IN_BYTES = 0x04;
18constexpr auto IM_KW_VALUE_OFFSET = 0x000005fb;
Anupama B R08fa59e2025-03-06 22:55:11 -060019
20std::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)
RekhaAparna01d3e693e2025-03-04 05:08:30 -060047 {}
Anupama B R08fa59e2025-03-06 22:55:11 -060048
49 return std::string();
50}
Anupama B Rb5bfcbc2025-03-03 03:00:04 -060051
52std::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}
RekhaAparna01d3e693e2025-03-04 05:08:30 -060086
87bool 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}
RekhaAparna01f05f3542025-03-02 22:25:23 -0600115
116bool 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
RekhaAparna0157fdd942025-03-24 09:36:28 -0500127 // Remove the new line character from the string.
128 l_cmdOutput[0].erase(l_cmdOutput[0].length() - 1);
129
RekhaAparna01f05f3542025-03-02 22:25:23 -0600130 return l_cmdOutput[0] == "false" ? false : true;
131 }
132 }
133 catch (const std::exception& l_ex)
134 {}
135
136 return false;
137}
Souvik Roycd828d42025-03-24 02:29:45 -0500138
139bool SingleFab::updateSystemImValueInVpdToP11Series(
140 std::string i_currentImValuePlanar) const noexcept
141{
142 bool l_retVal{false};
143 if (!i_currentImValuePlanar.empty())
144 {
145 // update the IM value to P11 series(6000x). Replace the first character
146 // of IM value string with '6'
147 l_retVal = setImOnPlanar(i_currentImValuePlanar.replace(
148 constants::VALUE_0, constants::VALUE_1,
149 std::to_string(constants::VALUE_6)));
150 }
151 return l_retVal;
152}
Anupama B R08fa59e2025-03-06 22:55:11 -0600153} // namespace vpd