xref: /openbmc/openpower-vpd-parser/vpd-manager/src/single_fab.cpp (revision 393c0fade4690d37e7dd7227730b2bcaef9214db)
1 #include "config.h"
2 
3 #include "single_fab.hpp"
4 
5 #include "constants.hpp"
6 #include "event_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/json_utility.hpp>
13 #include <utility/vpd_specific_utility.hpp>
14 
15 namespace vpd
16 {
17 constexpr auto pimPersistVsbpPath =
18     "/var/lib/phosphor-inventory-manager/xyz/openbmc_project/inventory/system/chassis/motherboard/com.ibm.ipzvpd.VSBP";
19 constexpr auto IM_SIZE_IN_BYTES = 0x04;
20 constexpr auto IM_KW_VALUE_OFFSET = 0x000005fb;
21 
22 std::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)
49     {}
50 
51     return std::string();
52 }
53 
54 std::string SingleFab::getImFromPlanar() const noexcept
55 {
56     try
57     {
58         const std::string l_systemPlanarPath(SYSTEM_VPD_FILE_PATH);
59         Parser l_parserObj(l_systemPlanarPath, nlohmann::json{});
60 
61         std::shared_ptr<ParserInterface> l_vpdParserInstance =
62             l_parserObj.getVpdParserInstance();
63 
64         auto l_readValue = l_vpdParserInstance->readKeywordFromHardware(
65             std::make_tuple(constants::recVSBP, constants::kwdIM));
66 
67         if (auto l_keywordValue =
68                 std::get_if<types::BinaryVector>(&l_readValue);
69             l_keywordValue && !l_keywordValue->empty())
70         {
71             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();
79         }
80     }
81     catch (const std::ifstream::failure& l_ex)
82     {}
83 
84     return std::string();
85 }
86 
87 bool 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 }
115 
116 bool SingleFab::isFieldModeEnabled() const noexcept
117 {
118     try
119     {
120         std::vector<std::string> l_cmdOutput =
121             commonUtility::executeCmd("/sbin/fw_printenv fieldmode");
122 
123         if (l_cmdOutput.size() > 0)
124         {
125             commonUtility::toLower(l_cmdOutput[0]);
126 
127             // Remove the new line character from the string.
128             l_cmdOutput[0].erase(l_cmdOutput[0].length() - 1);
129 
130             return l_cmdOutput[0] == "fieldmode=true" ? true : false;
131         }
132     }
133     catch (const std::exception& l_ex)
134     {}
135 
136     return false;
137 }
138 
139 void SingleFab::updateSystemImValueInVpdToP11Series(
140     std::string i_currentImValuePlanar) const noexcept
141 {
142     bool l_retVal{false};
143     if (!i_currentImValuePlanar.empty())
144     {
145         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 
155         // 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     }
161 
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 
172 int 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;
300 }
301 } // namespace vpd
302