blob: d5fdc332ba87e2de5d5b92ec028dd540819f628a [file] [log] [blame]
Deepak Kodihalli76794492017-02-16 23:48:18 -06001#pragma once
2
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -06003#include "const.hpp"
Alpana Kumarif05effd2021-04-07 07:32:53 -05004#include "store.hpp"
Deepak Kodihalli76794492017-02-16 23:48:18 -06005#include "types.hpp"
6
SunnySrivastava1984d076da82020-03-05 05:33:35 -06007#include <iostream>
Alpana Kumari735dee92022-03-25 01:24:40 -05008#include <nlohmann/json.hpp>
Santosh Puranik53b38ed2022-04-10 23:15:22 +05309#include <optional>
SunnySrivastava1984d076da82020-03-05 05:33:35 -060010
11using namespace std;
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050012
Deepak Kodihalli76794492017-02-16 23:48:18 -060013namespace openpower
14{
15namespace vpd
16{
SunnySrivastava1984945a02d2020-05-06 01:55:41 -050017
Santosh Puranik6b2b5372022-06-02 20:49:02 +053018// Map to hold record, kwd pair which can be re-stored at standby.
19// The list of keywords for VSYS record is as per the S0 system. Should
20// be updated for another type of systems
21static const std::unordered_map<std::string, std::vector<std::string>>
22 svpdKwdMap{{"VSYS", {"BR", "TM", "SE", "SU", "RB", "WN", "RG"}},
23 {"VCEN", {"FC", "SE"}},
24 {"LXR0", {"LX"}},
25 {"UTIL", {"D0"}}};
26
Santosh Puranikbd011b22020-01-23 04:05:25 -060027/** @brief Return the hex representation of the incoming byte
28 *
29 * @param [in] c - The input byte
30 * @returns The hex representation of the byte as a character.
31 */
32constexpr auto toHex(size_t c)
33{
34 constexpr auto map = "0123456789abcdef";
35 return map[c];
36}
Deepak Kodihalli76794492017-02-16 23:48:18 -060037
38namespace inventory
39{
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -060040/** @brief API to obtain a dictionary of path -> services
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050041 * where path is in subtree and services is of the type
42 * returned by the GetObject method.
43 *
44 * @param [in] root - Root path for object subtree
45 * @param [in] depth - Maximum subtree depth required
46 * @param [in] interfaces - Array to interfaces for which
47 * result is required.
48 * @return A dictionary of Path -> services
49 */
50MapperResponse
51 getObjectSubtreeForInterfaces(const std::string& root, const int32_t depth,
52 const std::vector<std::string>& interfaces);
53
Deepak Kodihalli76794492017-02-16 23:48:18 -060054} // namespace inventory
55
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060056/**@brief This API reads 2 Bytes of data and swap the read data
57 * @param[in] iterator- Pointer pointing to the data to be read
58 * @return returns 2 Byte data read at the given pointer
59 */
60openpower::vpd::constants::LE2ByteData
61 readUInt16LE(Binary::const_iterator iterator);
62
SunnySrivastava1984d076da82020-03-05 05:33:35 -060063/** @brief Encodes a keyword for D-Bus.
64 * @param[in] kw - kwd data in string format
65 * @param[in] encoding - required for kwd data
66 */
67string encodeKeyword(const string& kw, const string& encoding);
SunnySrivastava198443306542020-04-01 02:50:20 -050068
69/** @brief Reads a property from the inventory manager given object path,
70 * intreface and property.
71 * @param[in] obj - object path
72 * @param[in] inf - interface
73 * @param[in] prop - property whose value is fetched
74 * @return [out] - value of the property
75 */
76string readBusProperty(const string& obj, const string& inf,
77 const string& prop);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050078
Priyanga Ramasamyf1449cd2022-07-13 04:28:30 -050079/** @brief A templated function to read D-Bus properties.
80 *
81 * @param[in] service - Service path
82 * @param[in] object - object path
83 * @param[in] inf - interface
84 * @param[in] prop - property whose value is fetched
85 * @return The property value of its own type.
86 */
87template <typename T>
88T readDBusProperty(const string& service, const string& object,
89 const string& inf, const string& prop)
90{
91 T retVal{};
92 try
93 {
94 auto bus = sdbusplus::bus::new_default();
95 auto properties =
96 bus.new_method_call(service.c_str(), object.c_str(),
97 "org.freedesktop.DBus.Properties", "Get");
98 properties.append(inf);
99 properties.append(prop);
100 auto result = bus.call(properties);
101 result.read(retVal);
102 }
103 catch (const sdbusplus::exception::SdBusError& e)
104 {
105 std::cerr << e.what();
106 }
107 return retVal;
108}
109
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500110/** @brief A templated method to get all D-Bus properties
111 *
112 * @param[in] service - Service path
113 * @param[in] object - Object path
114 * @param[in] inf - Interface
115 *
116 * @return All properties under the given interface.
117 */
118template <typename T>
119T getAllDBusProperty(const std::string& service, const std::string& object,
120 const std::string& inf)
121{
122 T retVal{};
123 try
124 {
125 auto bus = sdbusplus::bus::new_default();
126 auto allProperties =
127 bus.new_method_call(service.c_str(), object.c_str(),
128 "org.freedesktop.DBus.Properties", "GetAll");
129 allProperties.append(inf);
130
131 auto result = bus.call(allProperties);
132 result.read(retVal);
133 }
134 catch (const sdbusplus::exception::SdBusError& e)
135 {
136 std::cerr << e.what();
137 }
138 return retVal;
139}
140
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500141/**
142 * @brief API to create PEL entry
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500143 * @param[in] additionalData - Map holding the additional data
144 * @param[in] sev - Severity
145 * @param[in] errIntf - error interface
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500146 */
147void createPEL(const std::map<std::string, std::string>& additionalData,
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500148 const constants::PelSeverity& sev, const std::string& errIntf);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500149
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530150/**
151 * @brief getVpdFilePath
152 * Get vpd file path corresponding to the given object path.
153 * @param[in] - json file path
154 * @param[in] - Object path
155 * @return - Vpd file path
156 */
157inventory::VPDfilepath getVpdFilePath(const string& jsonFile,
158 const std::string& ObjPath);
159
160/**
161 * @brief isPathInJson
162 * API which checks for the presence of the given eeprom path in the given json.
163 * @param[in] - eepromPath
164 * @return - true if the eeprom is present in the json; false otherwise
165 */
166bool isPathInJson(const std::string& eepromPath);
167
168/**
169 * @brief isRecKwInDbusJson
170 * API which checks whether the given keyword under the given record is to be
171 * published on dbus or not. Checks against the keywords present in
172 * dbus_property.json.
173 * @param[in] - record name
174 * @param[in] - keyword name
175 * @return - true if the record-keyword pair is present in dbus_property.json;
176 * false otherwise.
177 */
178bool isRecKwInDbusJson(const std::string& record, const std::string& keyword);
179
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -0500180/**
181 * @brief Check the type of VPD.
182 *
183 * Checks the type of vpd based on the start tag.
184 * @param[in] vector - Vpd data in vector format
185 *
186 * @return enum of type vpdType
187 */
188constants::vpdType vpdTypeCheck(const Binary& vector);
189
SunnySrivastava19849a195542020-09-07 06:04:50 -0500190/*
191 * @brief This method does nothing. Just an empty function to return null
192 * at the end of variadic template args
193 */
194inline string getCommand()
195{
196 return "";
197}
198
199/**
200 * @brief This function to arrange all arguments to make commandy
201 * @param[in] arguments to create the command
202 * @return cmd - command string
203 */
204template <typename T, typename... Types>
205inline string getCommand(T arg1, Types... args)
206{
207 string cmd = " " + arg1 + getCommand(args...);
208
209 return cmd;
210}
211
212/**
213 * @brief This API takes arguments, creates a shell command line and executes
214 * them.
215 * @param[in] arguments for command
216 * @returns output of that command
217 */
218template <typename T, typename... Types>
219inline vector<string> executeCmd(T&& path, Types... args)
220{
221 vector<string> stdOutput;
222 array<char, 128> buffer;
223
224 string cmd = path + getCommand(args...);
225
226 unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
227 if (!pipe)
228 {
229 throw runtime_error("popen() failed!");
230 }
231 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
232 {
233 stdOutput.emplace_back(buffer.data());
234 }
235
236 return stdOutput;
237}
238
Alpana Kumarif05effd2021-04-07 07:32:53 -0500239/** @brief This API checks for IM and HW keywords, and based
240 * on these values decides which system json to be used.
241 * @param[in] vpdMap - parsed vpd
242 * @returns System json path
243 */
244string getSystemsJson(const Parsed& vpdMap);
245
246/** @brief Reads HW Keyword from the vpd
247 * @param[in] vpdMap - parsed vpd
248 * @returns value of HW Keyword
249 */
250const string getHW(const Parsed& vpdMap);
251
252/** @brief Reads IM Keyword from the vpd
253 * @param[in] vpdMap - parsed vpd
254 * @returns value of IM Keyword
255 */
256const string getIM(const Parsed& vpdMap);
257
PriyangaRamasamy647868e2020-09-08 17:03:19 +0530258/** @brief Translate udev event generated path to a generic /sys/bus eeprom path
259 * @param[io] file - path generated from udev event.
260 */
261void udevToGenericPath(string& file);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -0600262
263/**
264 * @brief API to generate a vpd name in some pattern.
265 * This vpd-name denotes name of the bad vpd file.
266 * For i2c eeproms - the pattern of the vpd-name will be
267 * i2c-<bus-number>-<eeprom-address>. For spi eeproms - the pattern of the
268 * vpd-name will be spi-<spi-number>.
269 *
270 * @param[in] file - file path of the vpd
271 * @return the vpd-name.
272 */
273string getBadVpdName(const string& file);
274
275/**
276 * @brief API which dumps the broken/bad vpd in a directory
277 * When the vpd is bad, this api places the bad vpd file inside
278 * "/tmp/bad-vpd" in BMC, in order to collect bad VPD data as a part of user
279 * initiated BMC dump.
280 *
281 * @param[in] file - bad vpd file path
282 * @param[in] vpdVector - bad vpd vector
283 */
284void dumpBadVpd(const std::string& file, const Binary& vpdVector);
alpana077ce68722021-07-25 13:23:59 -0500285
286/*
287 * @brief This function fetches the value for given keyword in the given record
288 * from vpd data and returns this value.
289 *
290 * @param[in] vpdMap - vpd to find out the data
291 * @param[in] rec - Record under which desired keyword exists
292 * @param[in] kwd - keyword to read the data from
293 *
294 * @returns keyword value if record/keyword combination found
295 * empty string if record or keyword is not found.
296 */
297const string getKwVal(const Parsed& vpdMap, const string& rec,
298 const string& kwd);
299
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500300/** @brief This creates a complete command using all it's input parameters,
301 * to bind or unbind the driver.
302 * @param[in] devNameAddr - device address on that bus
303 * @param[in] busType - i2c, spi
304 * @param[in] driverType - type of driver like at24
305 * @param[in] bindOrUnbind - either bind or unbind
306 * @returns Command to bind or unbind the driver.
307 */
308inline string createBindUnbindDriverCmnd(const string& devNameAddr,
309 const string& busType,
310 const string& driverType,
311 const string& bindOrUnbind)
312{
313 return ("echo " + devNameAddr + " > /sys/bus/" + busType + "/drivers/" +
314 driverType + "/" + bindOrUnbind);
315}
316
Priyanga Ramasamy02434932021-10-07 16:26:05 -0500317/**
318 * @brief Get Printable Value
319 *
320 * Checks if the vector value has non printable characters.
321 * Returns hex value if non printable char is found else
322 * returns ascii value.
323 *
324 * @param[in] vector - Reference of the Binary vector
325 * @return printable value - either in hex or in ascii.
326 */
Priyanga Ramasamyc9ecf8e2021-10-08 02:28:52 -0500327string getPrintableValue(const Binary& vec);
328
329/**
330 * @brief Convert byte array to hex string.
331 * @param[in] vec - byte array
332 * @return hexadecimal string of bytes.
333 */
334string byteArrayToHexString(const Binary& vec);
Priyanga Ramasamy02434932021-10-07 16:26:05 -0500335
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -0600336/**
Santosh Puranik53b38ed2022-04-10 23:15:22 +0530337 * @brief Return presence of the FRU.
338 *
339 * This API returns the presence information of the FRU corresponding to the
340 * given EEPROM. If the JSON contains no information about presence detect, this
341 * will return an empty optional. Else it will get the presence GPIO information
342 * from the JSON and return the appropriate present status.
343 * In case of GPIO find/read errors, it will return false.
344 *
345 * @param[in] json - The VPD JSON
346 * @param[in] file - EEPROM file path
347 * @return Empty optional if there is no presence info. Else returns presence
348 * based on the GPIO read.
349 */
350std::optional<bool> isPresent(const nlohmann::json& json, const string& file);
351
352/**
353 * @brief Performs any pre-action needed to get the FRU setup for
354 * collection.
Alpana Kumari735dee92022-03-25 01:24:40 -0500355 *
356 * @param[in] json - json object
357 * @param[in] file - eeprom file path
358 * @return - success or failure
359 */
360bool executePreAction(const nlohmann::json& json, const string& file);
361
362/**
363 * @brief This API will be called at the end of VPD collection to perform any
364 * post actions.
365 *
366 * @param[in] json - json object
367 * @param[in] file - eeprom file path
368 */
369void executePostFailAction(const nlohmann::json& json, const string& file);
370
371/**
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -0600372 * @brief Helper function to insert or merge in map.
373 *
374 * This method checks in the given inventory::InterfaceMap if the given
375 * interface key is existing or not. If the interface key already exists, given
376 * property map is inserted into it. If the key does'nt exist then given
377 * interface and property map pair is newly created. If the property present in
378 * propertymap already exist in the InterfaceMap, then the new property value is
379 * ignored.
380 *
381 * @param[in,out] map - map object of type inventory::InterfaceMap only.
382 * @param[in] interface - Interface name.
383 * @param[in] property - new property map that needs to be emplaced.
384 */
385void insertOrMerge(inventory::InterfaceMap& map,
386 const inventory::Interface& interface,
387 inventory::PropertyMap&& property);
388
Santosh Puranikf2d3b532022-04-19 06:44:07 -0500389/**
390 * @brief Utility API to set a D-Bus property
391 *
392 * This calls org.freedesktop.DBus.Properties;Set method with the supplied
393 * arguments
394 *
395 * @tparam T Template type of the D-Bus property
396 * @param service[in] - The D-Bus service name.
397 * @param object[in] - The D-Bus object on which the property is to be set.
398 * @param interface[in] - The D-Bus interface to which the property belongs.
399 * @param propertyName[in] - The name of the property to set.
400 * @param propertyValue[in] - The value of the property.
401 */
402template <typename T>
403void setBusProperty(const std::string& service, const std::string& object,
404 const std::string& interface,
405 const std::string& propertyName,
406 const std::variant<T>& propertyValue)
407{
408 try
409 {
410 auto bus = sdbusplus::bus::new_default();
411 auto method =
412 bus.new_method_call(service.c_str(), object.c_str(),
413 "org.freedesktop.DBus.Properties", "Set");
414 method.append(interface);
415 method.append(propertyName);
416 method.append(propertyValue);
417
418 bus.call(method);
419 }
420 catch (const sdbusplus::exception::SdBusError& e)
421 {
422 std::cerr << e.what() << std::endl;
423 }
424}
425
426/**
427 * @brief Reads BIOS Attribute by name.
428 *
429 * @param attrName[in] - The BIOS attribute name.
430 * @return std::variant<int64_t, std::string> - The BIOS attribute value.
431 */
432std::variant<int64_t, std::string>
433 readBIOSAttribute(const std::string& attrName);
Priyanga Ramasamy335873f2022-05-18 01:31:54 -0500434
435/**
436 * @brief Returns the power state for chassis0
437 * @return The chassis power state.
438 */
439std::string getPowerState();
Santosh Puranik6b2b5372022-06-02 20:49:02 +0530440
441/**
442 * @brief Reads VPD from the supplied EEPROM
443 *
444 * This function reads the given VPD EEPROM file and returns its contents as a
445 * byte array. It handles any offsets into the file that need to be taken care
446 * of by looking up the VPD JSON for a possible offset key.
447 *
448 * @param js[in] - The VPD JSON Object
449 * @param file[in] - The path to the EEPROM to read
450 * @return A byte array containing the raw VPD.
451 */
452Binary getVpdDataInVector(const nlohmann::json& js, const std::string& file);
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700453} // namespace vpd
Santosh Puranikf2d3b532022-04-19 06:44:07 -0500454} // namespace openpower