blob: f2c73f8b25878a978cd37b65ac6d587fb2e6a9c3 [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 {
Anupama B Re2f09be2025-04-08 04:41:28 -050058 const std::string l_systemPlanarPath(SYSTEM_VPD_FILE_PATH);
59 Parser l_parserObj(l_systemPlanarPath, nlohmann::json{});
Anupama B Rb5bfcbc2025-03-03 03:00:04 -060060
Anupama B Re2f09be2025-04-08 04:41:28 -050061 std::shared_ptr<ParserInterface> l_vpdParserInstance =
62 l_parserObj.getVpdParserInstance();
Anupama B Rb5bfcbc2025-03-03 03:00:04 -060063
Anupama B Re2f09be2025-04-08 04:41:28 -050064 auto l_readValue = l_vpdParserInstance->readKeywordFromHardware(
65 std::make_tuple(constants::recVSBP, constants::kwdIM));
Anupama B Rb5bfcbc2025-03-03 03:00:04 -060066
Anupama B Re2f09be2025-04-08 04:41:28 -050067 if (auto l_keywordValue =
68 std::get_if<types::BinaryVector>(&l_readValue);
69 l_keywordValue && !l_keywordValue->empty())
Anupama B Rb5bfcbc2025-03-03 03:00:04 -060070 {
Anupama B Re2f09be2025-04-08 04:41:28 -050071 std::ostringstream l_imData;
72 for (const auto& l_byte : *l_keywordValue)
73 {
74 l_imData << std::setw(2) << std::setfill('0') << std::hex
75 << static_cast<int>(l_byte);
76 }
77
78 return l_imData.str();
Anupama B Rb5bfcbc2025-03-03 03:00:04 -060079 }
Anupama B Rb5bfcbc2025-03-03 03:00:04 -060080 }
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 =
Anupama B Re2f09be2025-04-08 04:41:28 -0500121 commonUtility::executeCmd("/sbin/fw_printenv fieldmode");
RekhaAparna01f05f3542025-03-02 22:25:23 -0600122
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
Anupama B Re2f09be2025-04-08 04:41:28 -0500130 return l_cmdOutput[0] == "fieldmode=true" ? true : false;
RekhaAparna01f05f3542025-03-02 22:25:23 -0600131 }
132 }
133 catch (const std::exception& l_ex)
134 {}
135
136 return false;
137}
Souvik Roycd828d42025-03-24 02:29:45 -0500138
Rekha Aparnaffdff312025-03-25 01:10:56 -0500139void SingleFab::updateSystemImValueInVpdToP11Series(
Souvik Roycd828d42025-03-24 02:29:45 -0500140 std::string i_currentImValuePlanar) const noexcept
141{
142 bool l_retVal{false};
143 if (!i_currentImValuePlanar.empty())
144 {
Anupama B R879d4652025-04-21 04:19:02 -0500145 if (i_currentImValuePlanar.compare(
146 constants::VALUE_4, constants::VALUE_1,
147 std::to_string(constants::VALUE_3)) ==
148 constants::STR_CMP_SUCCESS)
149 {
150 i_currentImValuePlanar.replace(constants::VALUE_4,
151 constants::VALUE_1,
152 std::to_string(constants::VALUE_2));
153 }
154
Souvik Roycd828d42025-03-24 02:29:45 -0500155 // update the IM value to P11 series(6000x). Replace the first character
156 // of IM value string with '6'
157 l_retVal = setImOnPlanar(i_currentImValuePlanar.replace(
158 constants::VALUE_0, constants::VALUE_1,
159 std::to_string(constants::VALUE_6)));
160 }
Rekha Aparnaffdff312025-03-25 01:10:56 -0500161
162 if (!l_retVal)
163 {
164 EventLogger::createSyncPel(
165 types::ErrorType::InternalFailure,
166 types::SeverityType::Informational, __FILE__, __FUNCTION__, 0,
167 std::string("Failed to update IM value to P11 series."),
168 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
169 }
170}
171
172int SingleFab::singleFabImOverride() const noexcept
173{
174 const std::string& l_planarImValue = getImFromPlanar();
175 const std::string& l_eBmcImValue = getImFromPersistedLocation();
176 const bool& l_isFieldModeEnabled = isFieldModeEnabled();
177 const bool& l_isLabModeEnabled =
178 !l_isFieldModeEnabled; // used for understanding
179 const bool& l_isPowerVsImage = vpdSpecificUtility::isPowerVsImage();
180 const bool& l_isNormalImage = !l_isPowerVsImage; // used for understanding
181
182 if (!isValidImSeries(l_planarImValue))
183 {
184 // Create Errorlog for invalid IM series encountered
185 EventLogger::createSyncPel(
186 types::ErrorType::InvalidSystem, types::SeverityType::Error,
187 __FILE__, __FUNCTION__, 0,
188 std::string("Invalid IM found on the system planar, IM value : ") +
189 l_planarImValue,
190 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
191
192 return constants::SUCCESS;
193 }
194
195 if (!l_eBmcImValue.empty())
196 {
197 if (isP10System(l_eBmcImValue))
198 {
199 if (isP10System(l_planarImValue))
200 {
201 if (l_isFieldModeEnabled && l_isNormalImage)
202 {
203 EventLogger::createSyncPel(
204 types::ErrorType::SystemTypeMismatch,
205 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
206 std::string("Mismatch in IM value found eBMC IM [") +
207 l_eBmcImValue + std::string("] planar IM [") +
208 l_planarImValue +
209 std::string("] Field mode enabled [") +
210 ((l_isFieldModeEnabled) ? "true" : "false") +
211 std::string("]"),
212 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
213
214 return constants::FAILURE;
215 }
216 }
217 else if (isP11System(l_planarImValue))
218 {
219 if (!(l_isLabModeEnabled && l_isNormalImage))
220 {
221 EventLogger::createSyncPel(
222 types::ErrorType::SystemTypeMismatch,
223 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
224 std::string("Mismatch in IM value found eBMC IM [") +
225 l_eBmcImValue + std::string("] planar IM [") +
226 l_planarImValue +
227 std::string("] Field mode enabled [") +
228 ((l_isFieldModeEnabled) ? "true" : "false") +
229 std::string("]"),
230 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
231
232 return constants::FAILURE;
233 }
234 }
235 }
236 else if (isP11System(l_eBmcImValue))
237 {
238 if (l_isPowerVsImage)
239 {
240 EventLogger::createSyncPel(
241 types::ErrorType::SystemTypeMismatch,
242 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
243 std::string("Mismatch in IM value found eBMC IM [") +
244 l_eBmcImValue + std::string("] planar IM [") +
245 l_planarImValue +
246 std::string("] Field mode enabled [") +
247 ((l_isFieldModeEnabled) ? "true" : "false") +
248 std::string("]"),
249 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
250
251 return constants::FAILURE;
252 }
253 else
254 {
255 if (isP10System(l_planarImValue))
256 {
257 updateSystemImValueInVpdToP11Series(l_planarImValue);
258 }
259 }
260 }
261 }
262 else
263 {
264 if (isP11System(l_planarImValue) && l_isPowerVsImage)
265 {
266 EventLogger::createSyncPel(
267 types::ErrorType::SystemTypeMismatch,
268 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
269 std::string("Mismatch in IM value found eBMC IM [") +
270 l_eBmcImValue + std::string("] planar IM [") +
271 l_planarImValue + std::string("] Field mode enabled [") +
272 ((l_isFieldModeEnabled) ? "true" : "false") +
273 std::string("]"),
274 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
275
276 return constants::FAILURE;
277 }
278 else if (isP10System(l_planarImValue) && l_isNormalImage)
279 {
280 if (l_isLabModeEnabled)
281 {
282 EventLogger::createSyncPel(
283 types::ErrorType::UnknownSystemSettings,
284 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
285 std::string("Mismatch in IM value found eBMC IM [") +
286 l_eBmcImValue + std::string("] planar IM [") +
287 l_planarImValue +
288 std::string("] Field mode enabled [") +
289 ((l_isFieldModeEnabled) ? "true" : "false") +
290 std::string("]"),
291 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
292 }
293 else
294 {
295 updateSystemImValueInVpdToP11Series(l_planarImValue);
296 }
297 }
298 }
299 return constants::SUCCESS;
Souvik Roycd828d42025-03-24 02:29:45 -0500300}
Anupama B R08fa59e2025-03-06 22:55:11 -0600301} // namespace vpd