xref: /openbmc/openpower-vpd-parser/vpd-manager/src/single_fab.cpp (revision 57022297e494963ffc04d23d20e9e415971c6577)
1 #include "config.h"
2 
3 #include "single_fab.hpp"
4 
5 #include "constants.hpp"
6 #include "logger.hpp"
7 #include "parser.hpp"
8 #include "types.hpp"
9 
10 #include <nlohmann/json.hpp>
11 #include <utility/common_utility.hpp>
12 #include <utility/event_logger_utility.hpp>
13 #include <utility/json_utility.hpp>
14 #include <utility/vpd_specific_utility.hpp>
15 
16 namespace vpd
17 {
18 constexpr auto pimPersistVsbpPath =
19     "/var/lib/phosphor-inventory-manager/xyz/openbmc_project/inventory/system/chassis/motherboard/com.ibm.ipzvpd.VSBP";
20 constexpr auto IM_SIZE_IN_BYTES = 0x04;
21 constexpr auto IM_KW_VALUE_OFFSET = 0x000005fb;
22 
getImFromPersistedLocation() const23 std::string SingleFab::getImFromPersistedLocation() const noexcept
24 {
25     try
26     {
27         uint16_t l_errCode = 0;
28         auto l_parsedVsbpJsonObj =
29             jsonUtility::getParsedJson(pimPersistVsbpPath, l_errCode);
30 
31         if (l_errCode)
32         {
33             throw JsonException(
34                 "Failed to parse JSON file [ " +
35                     std::string(pimPersistVsbpPath) +
36                     " ], error : " + commonUtility::getErrCodeMsg(l_errCode),
37                 pimPersistVsbpPath);
38         }
39 
40         if (!l_parsedVsbpJsonObj.contains("value0") ||
41             !l_parsedVsbpJsonObj["value0"].contains(constants::kwdIM) ||
42             !l_parsedVsbpJsonObj["value0"][constants::kwdIM].is_array())
43         {
44             throw std::runtime_error("Mandatory tag(s) missing from JSON");
45         }
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)
60     {}
61 
62     return std::string();
63 }
64 
getImFromPlanar() const65 std::string SingleFab::getImFromPlanar() const noexcept
66 {
67     try
68     {
69         const std::string l_systemPlanarPath(SYSTEM_VPD_FILE_PATH);
70         Parser l_parserObj(l_systemPlanarPath, nlohmann::json{});
71 
72         std::shared_ptr<ParserInterface> l_vpdParserInstance =
73             l_parserObj.getVpdParserInstance();
74 
75         auto l_readValue = l_vpdParserInstance->readKeywordFromHardware(
76             std::make_tuple(constants::recVSBP, constants::kwdIM));
77 
78         if (auto l_keywordValue =
79                 std::get_if<types::BinaryVector>(&l_readValue);
80             l_keywordValue && !l_keywordValue->empty())
81         {
82             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();
90         }
91     }
92     catch (const std::ifstream::failure& l_ex)
93     {}
94 
95     return std::string();
96 }
97 
setImOnPlanar(const std::string & i_imValue) const98 bool 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 }
126 
updateSystemImValueInVpdToP11Series(std::string i_currentImValuePlanar) const127 void SingleFab::updateSystemImValueInVpdToP11Series(
128     std::string i_currentImValuePlanar) const noexcept
129 {
130     bool l_retVal{false};
131     if (!i_currentImValuePlanar.empty())
132     {
133         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 
143         // 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     }
149 
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 
singleFabImOverride() const160 int SingleFab::singleFabImOverride() const noexcept
161 {
162     uint16_t l_errCode = 0;
163     const std::string& l_planarImValue = getImFromPlanar();
164     const std::string& l_eBmcImValue = getImFromPersistedLocation();
165     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 
175     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;
298 }
299 } // namespace vpd
300