blob: c823e90419f2bb4b2a0c9a07dbad446116dd885d [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 {
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 }
Rekha Aparnaffdff312025-03-25 01:10:56 -0500151
152 if (!l_retVal)
153 {
154 EventLogger::createSyncPel(
155 types::ErrorType::InternalFailure,
156 types::SeverityType::Informational, __FILE__, __FUNCTION__, 0,
157 std::string("Failed to update IM value to P11 series."),
158 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
159 }
160}
161
162int SingleFab::singleFabImOverride() const noexcept
163{
164 const std::string& l_planarImValue = getImFromPlanar();
165 const std::string& l_eBmcImValue = getImFromPersistedLocation();
166 const bool& l_isFieldModeEnabled = isFieldModeEnabled();
167 const bool& l_isLabModeEnabled =
168 !l_isFieldModeEnabled; // used for understanding
169 const bool& l_isPowerVsImage = vpdSpecificUtility::isPowerVsImage();
170 const bool& l_isNormalImage = !l_isPowerVsImage; // used for understanding
171
172 if (!isValidImSeries(l_planarImValue))
173 {
174 // Create Errorlog for invalid IM series encountered
175 EventLogger::createSyncPel(
176 types::ErrorType::InvalidSystem, types::SeverityType::Error,
177 __FILE__, __FUNCTION__, 0,
178 std::string("Invalid IM found on the system planar, IM value : ") +
179 l_planarImValue,
180 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
181
182 return constants::SUCCESS;
183 }
184
185 if (!l_eBmcImValue.empty())
186 {
187 if (isP10System(l_eBmcImValue))
188 {
189 if (isP10System(l_planarImValue))
190 {
191 if (l_isFieldModeEnabled && l_isNormalImage)
192 {
193 EventLogger::createSyncPel(
194 types::ErrorType::SystemTypeMismatch,
195 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
196 std::string("Mismatch in IM value found eBMC IM [") +
197 l_eBmcImValue + std::string("] planar IM [") +
198 l_planarImValue +
199 std::string("] Field mode enabled [") +
200 ((l_isFieldModeEnabled) ? "true" : "false") +
201 std::string("]"),
202 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
203
204 return constants::FAILURE;
205 }
206 }
207 else if (isP11System(l_planarImValue))
208 {
209 if (!(l_isLabModeEnabled && l_isNormalImage))
210 {
211 EventLogger::createSyncPel(
212 types::ErrorType::SystemTypeMismatch,
213 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
214 std::string("Mismatch in IM value found eBMC IM [") +
215 l_eBmcImValue + std::string("] planar IM [") +
216 l_planarImValue +
217 std::string("] Field mode enabled [") +
218 ((l_isFieldModeEnabled) ? "true" : "false") +
219 std::string("]"),
220 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
221
222 return constants::FAILURE;
223 }
224 }
225 }
226 else if (isP11System(l_eBmcImValue))
227 {
228 if (l_isPowerVsImage)
229 {
230 EventLogger::createSyncPel(
231 types::ErrorType::SystemTypeMismatch,
232 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
233 std::string("Mismatch in IM value found eBMC IM [") +
234 l_eBmcImValue + std::string("] planar IM [") +
235 l_planarImValue +
236 std::string("] Field mode enabled [") +
237 ((l_isFieldModeEnabled) ? "true" : "false") +
238 std::string("]"),
239 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
240
241 return constants::FAILURE;
242 }
243 else
244 {
245 if (isP10System(l_planarImValue))
246 {
247 updateSystemImValueInVpdToP11Series(l_planarImValue);
248 }
249 }
250 }
251 }
252 else
253 {
254 if (isP11System(l_planarImValue) && l_isPowerVsImage)
255 {
256 EventLogger::createSyncPel(
257 types::ErrorType::SystemTypeMismatch,
258 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
259 std::string("Mismatch in IM value found eBMC IM [") +
260 l_eBmcImValue + std::string("] planar IM [") +
261 l_planarImValue + std::string("] Field mode enabled [") +
262 ((l_isFieldModeEnabled) ? "true" : "false") +
263 std::string("]"),
264 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
265
266 return constants::FAILURE;
267 }
268 else if (isP10System(l_planarImValue) && l_isNormalImage)
269 {
270 if (l_isLabModeEnabled)
271 {
272 EventLogger::createSyncPel(
273 types::ErrorType::UnknownSystemSettings,
274 types::SeverityType::Warning, __FILE__, __FUNCTION__, 0,
275 std::string("Mismatch in IM value found eBMC IM [") +
276 l_eBmcImValue + std::string("] planar IM [") +
277 l_planarImValue +
278 std::string("] Field mode enabled [") +
279 ((l_isFieldModeEnabled) ? "true" : "false") +
280 std::string("]"),
281 std::nullopt, std::nullopt, std::nullopt, std::nullopt);
282 }
283 else
284 {
285 updateSystemImValueInVpdToP11Series(l_planarImValue);
286 }
287 }
288 }
289 return constants::SUCCESS;
Souvik Roycd828d42025-03-24 02:29:45 -0500290}
Anupama B R08fa59e2025-03-06 22:55:11 -0600291} // namespace vpd