blob: ca07a1a9958ac18d0d712385b5cf396580524692 [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"
Rekha Aparnaffdff312025-03-25 01:10:56 -05006#include "event_logger.hpp"
RekhaAparna01d3e693e2025-03-04 05:08:30 -06007#include "parser.hpp"
Anupama B R08fa59e2025-03-06 22:55:11 -06008#include "types.hpp"
9
RekhaAparna01d3e693e2025-03-04 05:08:30 -060010#include <nlohmann/json.hpp>
RekhaAparna01f05f3542025-03-02 22:25:23 -060011#include <utility/common_utility.hpp>
Anupama B R08fa59e2025-03-06 22:55:11 -060012#include <utility/json_utility.hpp>
Rekha Aparnaffdff312025-03-25 01:10:56 -050013#include <utility/vpd_specific_utility.hpp>
Anupama B R08fa59e2025-03-06 22:55:11 -060014
15namespace vpd
16{
17constexpr auto pimPersistVsbpPath =
18 "/var/lib/phosphor-inventory-manager/xyz/openbmc_project/inventory/system/chassis/motherboard/com.ibm.ipzvpd.VSBP";
Anupama B Rb5bfcbc2025-03-03 03:00:04 -060019constexpr auto IM_SIZE_IN_BYTES = 0x04;
20constexpr auto IM_KW_VALUE_OFFSET = 0x000005fb;
Anupama B R08fa59e2025-03-06 22:55:11 -060021
22std::string SingleFab::getImFromPersistedLocation() const noexcept
23{
24 try
25 {
26 auto l_parsedVsbpJsonObj =
27 jsonUtility::getParsedJson(pimPersistVsbpPath);
28 if (!l_parsedVsbpJsonObj.contains("value0") ||
29 !l_parsedVsbpJsonObj["value0"].contains(constants::kwdIM) ||
30 !l_parsedVsbpJsonObj["value0"][constants::kwdIM].is_array())
31 {
32 throw std::runtime_error(
33 "Json is empty or mandatory tag(s) missing from JSON");
34 }
35
36 const types::BinaryVector l_imValue =
37 l_parsedVsbpJsonObj["value0"][constants::kwdIM]
38 .get<types::BinaryVector>();
39
40 std::ostringstream l_imData;
41 for (const auto& l_byte : l_imValue)
42 {
43 l_imData << std::setw(2) << std::setfill('0') << std::hex
44 << static_cast<int>(l_byte);
45 }
46 return l_imData.str();
47 }
48 catch (const std::exception& l_ex)
RekhaAparna01d3e693e2025-03-04 05:08:30 -060049 {}
Anupama B R08fa59e2025-03-06 22:55:11 -060050
51 return std::string();
52}
Anupama B Rb5bfcbc2025-03-03 03:00:04 -060053
54std::string SingleFab::getImFromPlanar() const noexcept
55{
56 try
57 {
58 types::BinaryVector l_imValue(IM_SIZE_IN_BYTES);
59 std::fstream l_vpdFileStream;
60
61 l_vpdFileStream.exceptions(
62 std::ifstream::badbit | std::ifstream::failbit);
63
64 l_vpdFileStream.open(SYSTEM_VPD_FILE_PATH,
65 std::ios::in | std::ios::binary);
66
67 l_vpdFileStream.seekg(IM_KW_VALUE_OFFSET, std::ios_base::beg);
68
69 // Read keyword value
70 l_vpdFileStream.read(reinterpret_cast<char*>(&l_imValue[0]),
71 IM_SIZE_IN_BYTES);
72
73 l_vpdFileStream.clear(std::ios_base::eofbit);
74
75 std::ostringstream l_imData;
76 for (const auto& l_byte : l_imValue)
77 {
78 l_imData << std::setw(2) << std::setfill('0') << std::hex
79 << static_cast<int>(l_byte);
80 }
81 return l_imData.str();
82 }
83 catch (const std::ifstream::failure& l_ex)
84 {}
85
86 return std::string();
87}
RekhaAparna01d3e693e2025-03-04 05:08:30 -060088
89bool SingleFab::setImOnPlanar(const std::string& i_imValue) const noexcept
90{
91 try
92 {
93 types::BinaryVector l_imValue;
94 const std::string l_systemPlanarEepromPath = SYSTEM_VPD_FILE_PATH;
95
96 // Convert string to vector of bytes
97 for (auto l_value : i_imValue | std::views::chunk(2))
98 {
99 std::string l_byteString(l_value.begin(), l_value.end());
100 l_imValue.push_back(
101 static_cast<uint8_t>(std::stoi(l_byteString, nullptr, 16)));
102 }
103
104 std::shared_ptr<Parser> l_parserObj = std::make_shared<Parser>(
105 l_systemPlanarEepromPath, nlohmann::json{});
106
107 int l_bytes_updated = l_parserObj->updateVpdKeywordOnHardware(
108 std::make_tuple(constants::recVSBP, constants::kwdIM, l_imValue));
109
110 return l_bytes_updated > 0 ? true : false;
111 }
112 catch (const std::exception& l_ex)
113 {
114 return false;
115 }
116}
RekhaAparna01f05f3542025-03-02 22:25:23 -0600117
118bool SingleFab::isFieldModeEnabled() const noexcept
119{
120 try
121 {
122 std::vector<std::string> l_cmdOutput =
123 commonUtility::executeCmd("/sbin/fw_printenv -n fieldmode 2>&1");
124
125 if (l_cmdOutput.size() > 0)
126 {
127 commonUtility::toLower(l_cmdOutput[0]);
128
RekhaAparna0157fdd942025-03-24 09:36:28 -0500129 // Remove the new line character from the string.
130 l_cmdOutput[0].erase(l_cmdOutput[0].length() - 1);
131
RekhaAparna01f05f3542025-03-02 22:25:23 -0600132 return l_cmdOutput[0] == "false" ? false : true;
133 }
134 }
135 catch (const std::exception& l_ex)
136 {}
137
138 return false;
139}
Souvik Roycd828d42025-03-24 02:29:45 -0500140
Rekha Aparnaffdff312025-03-25 01:10:56 -0500141void SingleFab::updateSystemImValueInVpdToP11Series(
Souvik Roycd828d42025-03-24 02:29:45 -0500142 std::string i_currentImValuePlanar) const noexcept
143{
144 bool l_retVal{false};
145 if (!i_currentImValuePlanar.empty())
146 {
147 // update the IM value to P11 series(6000x). Replace the first character
148 // of IM value string with '6'
149 l_retVal = setImOnPlanar(i_currentImValuePlanar.replace(
150 constants::VALUE_0, constants::VALUE_1,
151 std::to_string(constants::VALUE_6)));
152 }
Rekha Aparnaffdff312025-03-25 01:10:56 -0500153
154 if (!l_retVal)
155 {
156 EventLogger::createSyncPel(
157 types::ErrorType::InternalFailure,
158 types::SeverityType::Informational, __FILE__, __FUNCTION__, 0,
159 std::string("Failed to update IM value to P11 series."),
160 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
161 }
162}
163
164int SingleFab::singleFabImOverride() const noexcept
165{
166 const std::string& l_planarImValue = getImFromPlanar();
167 const std::string& l_eBmcImValue = getImFromPersistedLocation();
168 const bool& l_isFieldModeEnabled = isFieldModeEnabled();
169 const bool& l_isLabModeEnabled =
170 !l_isFieldModeEnabled; // used for understanding
171 const bool& l_isPowerVsImage = vpdSpecificUtility::isPowerVsImage();
172 const bool& l_isNormalImage = !l_isPowerVsImage; // used for understanding
173
174 if (!isValidImSeries(l_planarImValue))
175 {
176 // Create Errorlog for invalid IM series encountered
177 EventLogger::createSyncPel(
178 types::ErrorType::InvalidSystem, types::SeverityType::Error,
179 __FILE__, __FUNCTION__, 0,
180 std::string("Invalid IM found on the system planar, IM value : ") +
181 l_planarImValue,
182 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
183
184 return constants::SUCCESS;
185 }
186
187 if (!l_eBmcImValue.empty())
188 {
189 if (isP10System(l_eBmcImValue))
190 {
191 if (isP10System(l_planarImValue))
192 {
193 if (l_isFieldModeEnabled && l_isNormalImage)
194 {
195 EventLogger::createSyncPel(
196 types::ErrorType::SystemTypeMismatch,
197 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
198 std::string("Mismatch in IM value found eBMC IM [") +
199 l_eBmcImValue + std::string("] planar IM [") +
200 l_planarImValue +
201 std::string("] Field mode enabled [") +
202 ((l_isFieldModeEnabled) ? "true" : "false") +
203 std::string("]"),
204 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
205
206 return constants::FAILURE;
207 }
208 }
209 else if (isP11System(l_planarImValue))
210 {
211 if (!(l_isLabModeEnabled && l_isNormalImage))
212 {
213 EventLogger::createSyncPel(
214 types::ErrorType::SystemTypeMismatch,
215 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
216 std::string("Mismatch in IM value found eBMC IM [") +
217 l_eBmcImValue + std::string("] planar IM [") +
218 l_planarImValue +
219 std::string("] Field mode enabled [") +
220 ((l_isFieldModeEnabled) ? "true" : "false") +
221 std::string("]"),
222 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
223
224 return constants::FAILURE;
225 }
226 }
227 }
228 else if (isP11System(l_eBmcImValue))
229 {
230 if (l_isPowerVsImage)
231 {
232 EventLogger::createSyncPel(
233 types::ErrorType::SystemTypeMismatch,
234 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
235 std::string("Mismatch in IM value found eBMC IM [") +
236 l_eBmcImValue + std::string("] planar IM [") +
237 l_planarImValue +
238 std::string("] Field mode enabled [") +
239 ((l_isFieldModeEnabled) ? "true" : "false") +
240 std::string("]"),
241 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
242
243 return constants::FAILURE;
244 }
245 else
246 {
247 if (isP10System(l_planarImValue))
248 {
249 updateSystemImValueInVpdToP11Series(l_planarImValue);
250 }
251 }
252 }
253 }
254 else
255 {
256 if (isP11System(l_planarImValue) && l_isPowerVsImage)
257 {
258 EventLogger::createSyncPel(
259 types::ErrorType::SystemTypeMismatch,
260 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
261 std::string("Mismatch in IM value found eBMC IM [") +
262 l_eBmcImValue + std::string("] planar IM [") +
263 l_planarImValue + std::string("] Field mode enabled [") +
264 ((l_isFieldModeEnabled) ? "true" : "false") +
265 std::string("]"),
266 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
267
268 return constants::FAILURE;
269 }
270 else if (isP10System(l_planarImValue) && l_isNormalImage)
271 {
272 if (l_isLabModeEnabled)
273 {
274 EventLogger::createSyncPel(
275 types::ErrorType::UnknownSystemSettings,
276 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
277 std::string("Mismatch in IM value found eBMC IM [") +
278 l_eBmcImValue + std::string("] planar IM [") +
279 l_planarImValue +
280 std::string("] Field mode enabled [") +
281 ((l_isFieldModeEnabled) ? "true" : "false") +
282 std::string("]"),
283 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
284 }
285 else
286 {
287 updateSystemImValueInVpdToP11Series(l_planarImValue);
288 }
289 }
290 }
291 return constants::SUCCESS;
Souvik Roycd828d42025-03-24 02:29:45 -0500292}
Anupama B R08fa59e2025-03-06 22:55:11 -0600293} // namespace vpd