xref: /openbmc/openpower-vpd-parser/vpd-manager/src/single_fab.cpp (revision 565e1140c9fbefa79b4fdd9fa16f24adbf30602c)
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         // 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     }
151 
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 
162 int 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;
290 }
291 } // namespace vpd
292