blob: 508f9d338f4733ccb30774a805c9673dec681f4d [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
Deepak Kodihalli76794492017-02-16 23:48:18 -060011namespace openpower
12{
13namespace vpd
14{
SunnySrivastava1984945a02d2020-05-06 01:55:41 -050015
Priyanga Ramasamy952d6c52022-11-07 07:20:24 -060016// Map which holds system vpd keywords which can be restored at standby and via
17// vpd-tool and also can be used to reset keywords to its defaults at
18// manufacturing. The list of keywords for VSYS record is as per the S0 system.
19// Should be updated for another type of systems For those keywords whose
20// default value is system specific, the default value field is left empty.
21// Record : {Keyword, Default value, Is PEL required on restore failure, Is MFG
22// reset required}
23static const inventory::SystemKeywordsMap svpdKwdMap{
24 {"VSYS",
25 {inventory::SystemKeywordInfo("BR", Binary(2, 0x20), true, true),
26 inventory::SystemKeywordInfo("TM", Binary(8, 0x20), true, true),
27 inventory::SystemKeywordInfo("SE", Binary(7, 0x20), true, true),
28 inventory::SystemKeywordInfo("SU", Binary(6, 0x20), true, true),
29 inventory::SystemKeywordInfo("RB", Binary(4, 0x20), true, true),
30 inventory::SystemKeywordInfo("WN", Binary(12, 0x20), true, true),
31 inventory::SystemKeywordInfo("RG", Binary(4, 0x20), true, true),
32 inventory::SystemKeywordInfo("FV", Binary(32, 0x20), false, true)}},
33 {"VCEN",
34 {inventory::SystemKeywordInfo("FC", Binary(), true, false),
35 inventory::SystemKeywordInfo("SE", Binary(7, 0x20), true, true)}},
36 {"LXR0", {inventory::SystemKeywordInfo("LX", Binary(), true, false)}},
37 {"UTIL",
38 {inventory::SystemKeywordInfo("D0", Binary(1, 0x00), true, true)}}};
Santosh Puranik6b2b5372022-06-02 20:49:02 +053039
Santosh Puranikbd011b22020-01-23 04:05:25 -060040/** @brief Return the hex representation of the incoming byte
41 *
42 * @param [in] c - The input byte
43 * @returns The hex representation of the byte as a character.
44 */
45constexpr auto toHex(size_t c)
46{
47 constexpr auto map = "0123456789abcdef";
48 return map[c];
49}
Deepak Kodihalli76794492017-02-16 23:48:18 -060050
51namespace inventory
52{
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -060053/** @brief API to obtain a dictionary of path -> services
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050054 * where path is in subtree and services is of the type
55 * returned by the GetObject method.
56 *
57 * @param [in] root - Root path for object subtree
58 * @param [in] depth - Maximum subtree depth required
59 * @param [in] interfaces - Array to interfaces for which
60 * result is required.
61 * @return A dictionary of Path -> services
62 */
63MapperResponse
64 getObjectSubtreeForInterfaces(const std::string& root, const int32_t depth,
65 const std::vector<std::string>& interfaces);
66
Deepak Kodihalli76794492017-02-16 23:48:18 -060067} // namespace inventory
68
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060069/**@brief This API reads 2 Bytes of data and swap the read data
70 * @param[in] iterator- Pointer pointing to the data to be read
71 * @return returns 2 Byte data read at the given pointer
72 */
73openpower::vpd::constants::LE2ByteData
74 readUInt16LE(Binary::const_iterator iterator);
75
SunnySrivastava1984d076da82020-03-05 05:33:35 -060076/** @brief Encodes a keyword for D-Bus.
77 * @param[in] kw - kwd data in string format
78 * @param[in] encoding - required for kwd data
79 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -050080std::string encodeKeyword(const std::string& kw, const std::string& encoding);
SunnySrivastava198443306542020-04-01 02:50:20 -050081
82/** @brief Reads a property from the inventory manager given object path,
83 * intreface and property.
84 * @param[in] obj - object path
85 * @param[in] inf - interface
86 * @param[in] prop - property whose value is fetched
87 * @return [out] - value of the property
88 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -050089std::string readBusProperty(const std::string& obj, const std::string& inf,
90 const std::string& prop);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050091
Priyanga Ramasamyf1449cd2022-07-13 04:28:30 -050092/** @brief A templated function to read D-Bus properties.
93 *
94 * @param[in] service - Service path
95 * @param[in] object - object path
96 * @param[in] inf - interface
97 * @param[in] prop - property whose value is fetched
98 * @return The property value of its own type.
99 */
100template <typename T>
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500101T readDBusProperty(const std::string& service, const std::string& object,
102 const std::string& inf, const std::string& prop)
Priyanga Ramasamyf1449cd2022-07-13 04:28:30 -0500103{
104 T retVal{};
105 try
106 {
107 auto bus = sdbusplus::bus::new_default();
108 auto properties =
109 bus.new_method_call(service.c_str(), object.c_str(),
110 "org.freedesktop.DBus.Properties", "Get");
111 properties.append(inf);
112 properties.append(prop);
113 auto result = bus.call(properties);
114 result.read(retVal);
115 }
116 catch (const sdbusplus::exception::SdBusError& e)
117 {
118 std::cerr << e.what();
119 }
120 return retVal;
121}
122
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500123/** @brief A templated method to get all D-Bus properties
124 *
125 * @param[in] service - Service path
126 * @param[in] object - Object path
127 * @param[in] inf - Interface
128 *
129 * @return All properties under the given interface.
130 */
131template <typename T>
132T getAllDBusProperty(const std::string& service, const std::string& object,
133 const std::string& inf)
134{
135 T retVal{};
136 try
137 {
138 auto bus = sdbusplus::bus::new_default();
139 auto allProperties =
140 bus.new_method_call(service.c_str(), object.c_str(),
141 "org.freedesktop.DBus.Properties", "GetAll");
142 allProperties.append(inf);
143
144 auto result = bus.call(allProperties);
145 result.read(retVal);
146 }
147 catch (const sdbusplus::exception::SdBusError& e)
148 {
149 std::cerr << e.what();
150 }
151 return retVal;
152}
153
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500154/**
155 * @brief API to create PEL entry
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500156 * The api makes synchronous call to phosphor-logging create api.
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500157 * @param[in] additionalData - Map holding the additional data
158 * @param[in] sev - Severity
159 * @param[in] errIntf - error interface
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500160 */
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500161void createSyncPEL(const std::map<std::string, std::string>& additionalData,
162 const constants::PelSeverity& sev,
163 const std::string& errIntf);
164
165/**
166 * @brief Api to create PEL.
167 * A wrapper api through which sync/async call to phosphor-logging create api
168 * can be made as and when required.
169 * sdBus as nullptr will result in sync call else async call will be made with
170 * just "DESCRIPTION" key/value pair in additional data.
171 * To make asyn call with more fields in additional data call
172 * "sd_bus_call_method_async" in place.
173 *
174 * @param[in] additionalData - Map of additional data.
175 * @param[in] sev - severity of the PEL.
176 * @param[in] errIntf - Error interface to be used in PEL.
177 * @param[in] sdBus - Pointer to Sd-Bus
178 */
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500179void createPEL(const std::map<std::string, std::string>& additionalData,
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500180 const constants::PelSeverity& sev, const std::string& errIntf,
181 sd_bus* sdBus);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500182
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530183/**
184 * @brief getVpdFilePath
185 * Get vpd file path corresponding to the given object path.
186 * @param[in] - json file path
187 * @param[in] - Object path
188 * @return - Vpd file path
189 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500190inventory::VPDfilepath getVpdFilePath(const std::string& jsonFile,
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530191 const std::string& ObjPath);
192
193/**
194 * @brief isPathInJson
195 * API which checks for the presence of the given eeprom path in the given json.
196 * @param[in] - eepromPath
197 * @return - true if the eeprom is present in the json; false otherwise
198 */
199bool isPathInJson(const std::string& eepromPath);
200
201/**
202 * @brief isRecKwInDbusJson
203 * API which checks whether the given keyword under the given record is to be
204 * published on dbus or not. Checks against the keywords present in
205 * dbus_property.json.
206 * @param[in] - record name
207 * @param[in] - keyword name
208 * @return - true if the record-keyword pair is present in dbus_property.json;
209 * false otherwise.
210 */
211bool isRecKwInDbusJson(const std::string& record, const std::string& keyword);
212
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -0500213/**
214 * @brief Check the type of VPD.
215 *
216 * Checks the type of vpd based on the start tag.
217 * @param[in] vector - Vpd data in vector format
218 *
219 * @return enum of type vpdType
220 */
221constants::vpdType vpdTypeCheck(const Binary& vector);
222
SunnySrivastava19849a195542020-09-07 06:04:50 -0500223/*
224 * @brief This method does nothing. Just an empty function to return null
225 * at the end of variadic template args
226 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500227inline std::string getCommand()
SunnySrivastava19849a195542020-09-07 06:04:50 -0500228{
229 return "";
230}
231
232/**
233 * @brief This function to arrange all arguments to make commandy
234 * @param[in] arguments to create the command
235 * @return cmd - command string
236 */
237template <typename T, typename... Types>
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500238inline std::string getCommand(T arg1, Types... args)
SunnySrivastava19849a195542020-09-07 06:04:50 -0500239{
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500240 std::string cmd = " " + arg1 + getCommand(args...);
SunnySrivastava19849a195542020-09-07 06:04:50 -0500241
242 return cmd;
243}
244
245/**
246 * @brief This API takes arguments, creates a shell command line and executes
247 * them.
248 * @param[in] arguments for command
249 * @returns output of that command
250 */
251template <typename T, typename... Types>
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500252inline std::vector<std::string> executeCmd(T&& path, Types... args)
SunnySrivastava19849a195542020-09-07 06:04:50 -0500253{
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500254 std::vector<std::string> stdOutput;
255 std::array<char, 128> buffer;
SunnySrivastava19849a195542020-09-07 06:04:50 -0500256
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500257 std::string cmd = path + getCommand(args...);
SunnySrivastava19849a195542020-09-07 06:04:50 -0500258
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500259 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"),
260 pclose);
SunnySrivastava19849a195542020-09-07 06:04:50 -0500261 if (!pipe)
262 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500263 throw std::runtime_error("popen() failed!");
SunnySrivastava19849a195542020-09-07 06:04:50 -0500264 }
265 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
266 {
267 stdOutput.emplace_back(buffer.data());
268 }
269
270 return stdOutput;
271}
272
Alpana Kumarif05effd2021-04-07 07:32:53 -0500273/** @brief This API checks for IM and HW keywords, and based
274 * on these values decides which system json to be used.
275 * @param[in] vpdMap - parsed vpd
276 * @returns System json path
277 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500278std::string getSystemsJson(const Parsed& vpdMap);
Alpana Kumarif05effd2021-04-07 07:32:53 -0500279
280/** @brief Reads HW Keyword from the vpd
281 * @param[in] vpdMap - parsed vpd
282 * @returns value of HW Keyword
283 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500284const std::string getHW(const Parsed& vpdMap);
Alpana Kumarif05effd2021-04-07 07:32:53 -0500285
286/** @brief Reads IM Keyword from the vpd
287 * @param[in] vpdMap - parsed vpd
288 * @returns value of IM Keyword
289 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500290const std::string getIM(const Parsed& vpdMap);
Alpana Kumarif05effd2021-04-07 07:32:53 -0500291
PriyangaRamasamy647868e2020-09-08 17:03:19 +0530292/** @brief Translate udev event generated path to a generic /sys/bus eeprom path
293 * @param[io] file - path generated from udev event.
294 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500295void udevToGenericPath(std::string& file);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -0600296
297/**
298 * @brief API to generate a vpd name in some pattern.
299 * This vpd-name denotes name of the bad vpd file.
300 * For i2c eeproms - the pattern of the vpd-name will be
301 * i2c-<bus-number>-<eeprom-address>. For spi eeproms - the pattern of the
302 * vpd-name will be spi-<spi-number>.
303 *
304 * @param[in] file - file path of the vpd
305 * @return the vpd-name.
306 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500307std::string getBadVpdName(const std::string& file);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -0600308
309/**
310 * @brief API which dumps the broken/bad vpd in a directory
311 * When the vpd is bad, this api places the bad vpd file inside
312 * "/tmp/bad-vpd" in BMC, in order to collect bad VPD data as a part of user
313 * initiated BMC dump.
314 *
315 * @param[in] file - bad vpd file path
316 * @param[in] vpdVector - bad vpd vector
317 */
318void dumpBadVpd(const std::string& file, const Binary& vpdVector);
alpana077ce68722021-07-25 13:23:59 -0500319
320/*
321 * @brief This function fetches the value for given keyword in the given record
322 * from vpd data and returns this value.
323 *
324 * @param[in] vpdMap - vpd to find out the data
325 * @param[in] rec - Record under which desired keyword exists
326 * @param[in] kwd - keyword to read the data from
327 *
328 * @returns keyword value if record/keyword combination found
329 * empty string if record or keyword is not found.
330 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500331const std::string getKwVal(const Parsed& vpdMap, const std::string& rec,
332 const std::string& kwd);
alpana077ce68722021-07-25 13:23:59 -0500333
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500334/** @brief This creates a complete command using all it's input parameters,
335 * to bind or unbind the driver.
336 * @param[in] devNameAddr - device address on that bus
337 * @param[in] busType - i2c, spi
338 * @param[in] driverType - type of driver like at24
339 * @param[in] bindOrUnbind - either bind or unbind
340 * @returns Command to bind or unbind the driver.
341 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500342inline std::string createBindUnbindDriverCmnd(const std::string& devNameAddr,
343 const std::string& busType,
344 const std::string& driverType,
345 const std::string& bindOrUnbind)
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500346{
347 return ("echo " + devNameAddr + " > /sys/bus/" + busType + "/drivers/" +
348 driverType + "/" + bindOrUnbind);
349}
350
Priyanga Ramasamy02434932021-10-07 16:26:05 -0500351/**
352 * @brief Get Printable Value
353 *
354 * Checks if the vector value has non printable characters.
355 * Returns hex value if non printable char is found else
356 * returns ascii value.
357 *
358 * @param[in] vector - Reference of the Binary vector
359 * @return printable value - either in hex or in ascii.
360 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500361std::string getPrintableValue(const Binary& vec);
Priyanga Ramasamyc9ecf8e2021-10-08 02:28:52 -0500362
363/**
364 * @brief Convert byte array to hex string.
365 * @param[in] vec - byte array
366 * @return hexadecimal string of bytes.
367 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500368std::string byteArrayToHexString(const Binary& vec);
Priyanga Ramasamy02434932021-10-07 16:26:05 -0500369
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -0600370/**
Santosh Puranik53b38ed2022-04-10 23:15:22 +0530371 * @brief Return presence of the FRU.
372 *
373 * This API returns the presence information of the FRU corresponding to the
374 * given EEPROM. If the JSON contains no information about presence detect, this
375 * will return an empty optional. Else it will get the presence GPIO information
376 * from the JSON and return the appropriate present status.
377 * In case of GPIO find/read errors, it will return false.
378 *
379 * @param[in] json - The VPD JSON
380 * @param[in] file - EEPROM file path
381 * @return Empty optional if there is no presence info. Else returns presence
382 * based on the GPIO read.
383 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500384std::optional<bool> isPresent(const nlohmann::json& json,
385 const std::string& file);
Santosh Puranik53b38ed2022-04-10 23:15:22 +0530386
387/**
388 * @brief Performs any pre-action needed to get the FRU setup for
389 * collection.
Alpana Kumari735dee92022-03-25 01:24:40 -0500390 *
391 * @param[in] json - json object
392 * @param[in] file - eeprom file path
393 * @return - success or failure
394 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500395bool executePreAction(const nlohmann::json& json, const std::string& file);
Alpana Kumari735dee92022-03-25 01:24:40 -0500396
397/**
398 * @brief This API will be called at the end of VPD collection to perform any
399 * post actions.
400 *
401 * @param[in] json - json object
402 * @param[in] file - eeprom file path
403 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500404void executePostFailAction(const nlohmann::json& json, const std::string& file);
Alpana Kumari735dee92022-03-25 01:24:40 -0500405
406/**
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -0600407 * @brief Helper function to insert or merge in map.
408 *
409 * This method checks in the given inventory::InterfaceMap if the given
410 * interface key is existing or not. If the interface key already exists, given
411 * property map is inserted into it. If the key does'nt exist then given
412 * interface and property map pair is newly created. If the property present in
413 * propertymap already exist in the InterfaceMap, then the new property value is
414 * ignored.
415 *
416 * @param[in,out] map - map object of type inventory::InterfaceMap only.
417 * @param[in] interface - Interface name.
418 * @param[in] property - new property map that needs to be emplaced.
419 */
420void insertOrMerge(inventory::InterfaceMap& map,
421 const inventory::Interface& interface,
422 inventory::PropertyMap&& property);
423
Santosh Puranikf2d3b532022-04-19 06:44:07 -0500424/**
425 * @brief Utility API to set a D-Bus property
426 *
427 * This calls org.freedesktop.DBus.Properties;Set method with the supplied
428 * arguments
429 *
430 * @tparam T Template type of the D-Bus property
431 * @param service[in] - The D-Bus service name.
432 * @param object[in] - The D-Bus object on which the property is to be set.
433 * @param interface[in] - The D-Bus interface to which the property belongs.
434 * @param propertyName[in] - The name of the property to set.
435 * @param propertyValue[in] - The value of the property.
436 */
437template <typename T>
438void setBusProperty(const std::string& service, const std::string& object,
439 const std::string& interface,
440 const std::string& propertyName,
441 const std::variant<T>& propertyValue)
442{
443 try
444 {
445 auto bus = sdbusplus::bus::new_default();
446 auto method =
447 bus.new_method_call(service.c_str(), object.c_str(),
448 "org.freedesktop.DBus.Properties", "Set");
449 method.append(interface);
450 method.append(propertyName);
451 method.append(propertyValue);
452
453 bus.call(method);
454 }
455 catch (const sdbusplus::exception::SdBusError& e)
456 {
457 std::cerr << e.what() << std::endl;
458 }
459}
460
461/**
462 * @brief Reads BIOS Attribute by name.
463 *
464 * @param attrName[in] - The BIOS attribute name.
465 * @return std::variant<int64_t, std::string> - The BIOS attribute value.
466 */
467std::variant<int64_t, std::string>
468 readBIOSAttribute(const std::string& attrName);
Priyanga Ramasamy335873f2022-05-18 01:31:54 -0500469
470/**
471 * @brief Returns the power state for chassis0
472 * @return The chassis power state.
473 */
474std::string getPowerState();
Santosh Puranik6b2b5372022-06-02 20:49:02 +0530475
476/**
477 * @brief Reads VPD from the supplied EEPROM
478 *
479 * This function reads the given VPD EEPROM file and returns its contents as a
480 * byte array. It handles any offsets into the file that need to be taken care
481 * of by looking up the VPD JSON for a possible offset key.
482 *
483 * @param js[in] - The VPD JSON Object
484 * @param file[in] - The path to the EEPROM to read
485 * @return A byte array containing the raw VPD.
486 */
487Binary getVpdDataInVector(const nlohmann::json& js, const std::string& file);
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700488} // namespace vpd
Santosh Puranikf2d3b532022-04-19 06:44:07 -0500489} // namespace openpower