blob: c602dac091b84bca366d63b913c92769af8026f4 [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 Aparna57022292025-11-25 04:20:41 -06006#include "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>
Rekha Aparnaa39aafa2025-11-04 00:06:12 -060012#include <utility/event_logger_utility.hpp>
Anupama B R08fa59e2025-03-06 22:55:11 -060013#include <utility/json_utility.hpp>
Rekha Aparnaffdff312025-03-25 01:10:56 -050014#include <utility/vpd_specific_utility.hpp>
Anupama B R08fa59e2025-03-06 22:55:11 -060015
16namespace vpd
17{
18constexpr auto pimPersistVsbpPath =
19 "/var/lib/phosphor-inventory-manager/xyz/openbmc_project/inventory/system/chassis/motherboard/com.ibm.ipzvpd.VSBP";
Anupama B Rb5bfcbc2025-03-03 03:00:04 -060020constexpr auto IM_SIZE_IN_BYTES = 0x04;
21constexpr auto IM_KW_VALUE_OFFSET = 0x000005fb;
Anupama B R08fa59e2025-03-06 22:55:11 -060022
23std::string SingleFab::getImFromPersistedLocation() const noexcept
24{
25 try
26 {
Rekha Aparnaca9a0862025-08-29 04:08:33 -050027 uint16_t l_errCode = 0;
Anupama B R08fa59e2025-03-06 22:55:11 -060028 auto l_parsedVsbpJsonObj =
Rekha Aparnaca9a0862025-08-29 04:08:33 -050029 jsonUtility::getParsedJson(pimPersistVsbpPath, l_errCode);
30
31 if (l_errCode)
32 {
33 throw JsonException(
34 "Failed to parse JSON file [ " +
Rekha Aparnac6159a22025-10-09 12:20:20 +053035 std::string(pimPersistVsbpPath) +
36 " ], error : " + commonUtility::getErrCodeMsg(l_errCode),
Rekha Aparnaca9a0862025-08-29 04:08:33 -050037 pimPersistVsbpPath);
38 }
39
Anupama B R08fa59e2025-03-06 22:55:11 -060040 if (!l_parsedVsbpJsonObj.contains("value0") ||
41 !l_parsedVsbpJsonObj["value0"].contains(constants::kwdIM) ||
42 !l_parsedVsbpJsonObj["value0"][constants::kwdIM].is_array())
43 {
Rekha Aparnaca9a0862025-08-29 04:08:33 -050044 throw std::runtime_error("Mandatory tag(s) missing from JSON");
Anupama B R08fa59e2025-03-06 22:55:11 -060045 }
46
47 const types::BinaryVector l_imValue =
48 l_parsedVsbpJsonObj["value0"][constants::kwdIM]
49 .get<types::BinaryVector>();
50
51 std::ostringstream l_imData;
52 for (const auto& l_byte : l_imValue)
53 {
54 l_imData << std::setw(2) << std::setfill('0') << std::hex
55 << static_cast<int>(l_byte);
56 }
57 return l_imData.str();
58 }
59 catch (const std::exception& l_ex)
RekhaAparna01d3e693e2025-03-04 05:08:30 -060060 {}
Anupama B R08fa59e2025-03-06 22:55:11 -060061
62 return std::string();
63}
Anupama B Rb5bfcbc2025-03-03 03:00:04 -060064
65std::string SingleFab::getImFromPlanar() const noexcept
66{
67 try
68 {
Anupama B Re2f09be2025-04-08 04:41:28 -050069 const std::string l_systemPlanarPath(SYSTEM_VPD_FILE_PATH);
70 Parser l_parserObj(l_systemPlanarPath, nlohmann::json{});
Anupama B Rb5bfcbc2025-03-03 03:00:04 -060071
Anupama B Re2f09be2025-04-08 04:41:28 -050072 std::shared_ptr<ParserInterface> l_vpdParserInstance =
73 l_parserObj.getVpdParserInstance();
Anupama B Rb5bfcbc2025-03-03 03:00:04 -060074
Anupama B Re2f09be2025-04-08 04:41:28 -050075 auto l_readValue = l_vpdParserInstance->readKeywordFromHardware(
76 std::make_tuple(constants::recVSBP, constants::kwdIM));
Anupama B Rb5bfcbc2025-03-03 03:00:04 -060077
Anupama B Re2f09be2025-04-08 04:41:28 -050078 if (auto l_keywordValue =
79 std::get_if<types::BinaryVector>(&l_readValue);
80 l_keywordValue && !l_keywordValue->empty())
Anupama B Rb5bfcbc2025-03-03 03:00:04 -060081 {
Anupama B Re2f09be2025-04-08 04:41:28 -050082 std::ostringstream l_imData;
83 for (const auto& l_byte : *l_keywordValue)
84 {
85 l_imData << std::setw(2) << std::setfill('0') << std::hex
86 << static_cast<int>(l_byte);
87 }
88
89 return l_imData.str();
Anupama B Rb5bfcbc2025-03-03 03:00:04 -060090 }
Anupama B Rb5bfcbc2025-03-03 03:00:04 -060091 }
92 catch (const std::ifstream::failure& l_ex)
93 {}
94
95 return std::string();
96}
RekhaAparna01d3e693e2025-03-04 05:08:30 -060097
98bool SingleFab::setImOnPlanar(const std::string& i_imValue) const noexcept
99{
100 try
101 {
102 types::BinaryVector l_imValue;
103 const std::string l_systemPlanarEepromPath = SYSTEM_VPD_FILE_PATH;
104
105 // Convert string to vector of bytes
106 for (auto l_value : i_imValue | std::views::chunk(2))
107 {
108 std::string l_byteString(l_value.begin(), l_value.end());
109 l_imValue.push_back(
110 static_cast<uint8_t>(std::stoi(l_byteString, nullptr, 16)));
111 }
112
113 std::shared_ptr<Parser> l_parserObj = std::make_shared<Parser>(
114 l_systemPlanarEepromPath, nlohmann::json{});
115
116 int l_bytes_updated = l_parserObj->updateVpdKeywordOnHardware(
117 std::make_tuple(constants::recVSBP, constants::kwdIM, l_imValue));
118
119 return l_bytes_updated > 0 ? true : false;
120 }
121 catch (const std::exception& l_ex)
122 {
123 return false;
124 }
125}
RekhaAparna01f05f3542025-03-02 22:25:23 -0600126
Rekha Aparnaffdff312025-03-25 01:10:56 -0500127void SingleFab::updateSystemImValueInVpdToP11Series(
Souvik Roycd828d42025-03-24 02:29:45 -0500128 std::string i_currentImValuePlanar) const noexcept
129{
130 bool l_retVal{false};
131 if (!i_currentImValuePlanar.empty())
132 {
Anupama B R879d4652025-04-21 04:19:02 -0500133 if (i_currentImValuePlanar.compare(
134 constants::VALUE_4, constants::VALUE_1,
135 std::to_string(constants::VALUE_3)) ==
136 constants::STR_CMP_SUCCESS)
137 {
138 i_currentImValuePlanar.replace(constants::VALUE_4,
139 constants::VALUE_1,
140 std::to_string(constants::VALUE_2));
141 }
142
Souvik Roycd828d42025-03-24 02:29:45 -0500143 // update the IM value to P11 series(6000x). Replace the first character
144 // of IM value string with '6'
145 l_retVal = setImOnPlanar(i_currentImValuePlanar.replace(
146 constants::VALUE_0, constants::VALUE_1,
147 std::to_string(constants::VALUE_6)));
148 }
Rekha Aparnaffdff312025-03-25 01:10:56 -0500149
150 if (!l_retVal)
151 {
152 EventLogger::createSyncPel(
153 types::ErrorType::InternalFailure,
154 types::SeverityType::Informational, __FILE__, __FUNCTION__, 0,
155 std::string("Failed to update IM value to P11 series."),
156 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
157 }
158}
159
160int SingleFab::singleFabImOverride() const noexcept
161{
Rekha Aparna57022292025-11-25 04:20:41 -0600162 uint16_t l_errCode = 0;
Rekha Aparnaffdff312025-03-25 01:10:56 -0500163 const std::string& l_planarImValue = getImFromPlanar();
164 const std::string& l_eBmcImValue = getImFromPersistedLocation();
Rekha Aparna57022292025-11-25 04:20:41 -0600165 const bool& l_isFieldModeEnabled =
166 commonUtility::isFieldModeEnabled(l_errCode);
167
168 if (l_errCode)
169 {
170 Logger::getLoggerInstance()->logMessage(
171 "Failed to check is field mode enabled, error : " +
172 commonUtility::getErrCodeMsg(l_errCode));
173 }
174
Rekha Aparnaffdff312025-03-25 01:10:56 -0500175 const bool& l_isLabModeEnabled =
176 !l_isFieldModeEnabled; // used for understanding
177 const bool& l_isPowerVsImage = vpdSpecificUtility::isPowerVsImage();
178 const bool& l_isNormalImage = !l_isPowerVsImage; // used for understanding
179
180 if (!isValidImSeries(l_planarImValue))
181 {
182 // Create Errorlog for invalid IM series encountered
183 EventLogger::createSyncPel(
184 types::ErrorType::InvalidSystem, types::SeverityType::Error,
185 __FILE__, __FUNCTION__, 0,
186 std::string("Invalid IM found on the system planar, IM value : ") +
187 l_planarImValue,
188 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
189
190 return constants::SUCCESS;
191 }
192
193 if (!l_eBmcImValue.empty())
194 {
195 if (isP10System(l_eBmcImValue))
196 {
197 if (isP10System(l_planarImValue))
198 {
199 if (l_isFieldModeEnabled && l_isNormalImage)
200 {
201 EventLogger::createSyncPel(
202 types::ErrorType::SystemTypeMismatch,
203 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
204 std::string("Mismatch in IM value found eBMC IM [") +
205 l_eBmcImValue + std::string("] planar IM [") +
206 l_planarImValue +
207 std::string("] Field mode enabled [") +
208 ((l_isFieldModeEnabled) ? "true" : "false") +
209 std::string("]"),
210 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
211
212 return constants::FAILURE;
213 }
214 }
215 else if (isP11System(l_planarImValue))
216 {
217 if (!(l_isLabModeEnabled && l_isNormalImage))
218 {
219 EventLogger::createSyncPel(
220 types::ErrorType::SystemTypeMismatch,
221 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
222 std::string("Mismatch in IM value found eBMC IM [") +
223 l_eBmcImValue + std::string("] planar IM [") +
224 l_planarImValue +
225 std::string("] Field mode enabled [") +
226 ((l_isFieldModeEnabled) ? "true" : "false") +
227 std::string("]"),
228 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
229
230 return constants::FAILURE;
231 }
232 }
233 }
234 else if (isP11System(l_eBmcImValue))
235 {
236 if (l_isPowerVsImage)
237 {
238 EventLogger::createSyncPel(
239 types::ErrorType::SystemTypeMismatch,
240 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
241 std::string("Mismatch in IM value found eBMC IM [") +
242 l_eBmcImValue + std::string("] planar IM [") +
243 l_planarImValue +
244 std::string("] Field mode enabled [") +
245 ((l_isFieldModeEnabled) ? "true" : "false") +
246 std::string("]"),
247 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
248
249 return constants::FAILURE;
250 }
251 else
252 {
253 if (isP10System(l_planarImValue))
254 {
255 updateSystemImValueInVpdToP11Series(l_planarImValue);
256 }
257 }
258 }
259 }
260 else
261 {
262 if (isP11System(l_planarImValue) && l_isPowerVsImage)
263 {
264 EventLogger::createSyncPel(
265 types::ErrorType::SystemTypeMismatch,
266 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
267 std::string("Mismatch in IM value found eBMC IM [") +
268 l_eBmcImValue + std::string("] planar IM [") +
269 l_planarImValue + std::string("] Field mode enabled [") +
270 ((l_isFieldModeEnabled) ? "true" : "false") +
271 std::string("]"),
272 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
273
274 return constants::FAILURE;
275 }
276 else if (isP10System(l_planarImValue) && l_isNormalImage)
277 {
278 if (l_isLabModeEnabled)
279 {
280 EventLogger::createSyncPel(
281 types::ErrorType::UnknownSystemSettings,
282 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
283 std::string("Mismatch in IM value found eBMC IM [") +
284 l_eBmcImValue + std::string("] planar IM [") +
285 l_planarImValue +
286 std::string("] Field mode enabled [") +
287 ((l_isFieldModeEnabled) ? "true" : "false") +
288 std::string("]"),
289 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
290 }
291 else
292 {
293 updateSystemImValueInVpdToP11Series(l_planarImValue);
294 }
295 }
296 }
297 return constants::SUCCESS;
Souvik Roycd828d42025-03-24 02:29:45 -0500298}
Anupama B R08fa59e2025-03-06 22:55:11 -0600299} // namespace vpd