blob: 46a707ade65a67fb720da31953cb98d2fe426a58 [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>
GiridhariKrishnan63639102023-03-02 05:55:47 -060010#include <variant>
SunnySrivastava1984d076da82020-03-05 05:33:35 -060011
Deepak Kodihalli76794492017-02-16 23:48:18 -060012namespace openpower
13{
14namespace vpd
15{
SunnySrivastava1984945a02d2020-05-06 01:55:41 -050016
Priyanga Ramasamy952d6c52022-11-07 07:20:24 -060017// Map which holds system vpd keywords which can be restored at standby and via
18// vpd-tool and also can be used to reset keywords to its defaults at
19// manufacturing. The list of keywords for VSYS record is as per the S0 system.
20// Should be updated for another type of systems For those keywords whose
21// default value is system specific, the default value field is left empty.
22// Record : {Keyword, Default value, Is PEL required on restore failure, Is MFG
23// reset required}
24static const inventory::SystemKeywordsMap svpdKwdMap{
25 {"VSYS",
26 {inventory::SystemKeywordInfo("BR", Binary(2, 0x20), true, true),
27 inventory::SystemKeywordInfo("TM", Binary(8, 0x20), true, true),
28 inventory::SystemKeywordInfo("SE", Binary(7, 0x20), true, true),
29 inventory::SystemKeywordInfo("SU", Binary(6, 0x20), true, true),
30 inventory::SystemKeywordInfo("RB", Binary(4, 0x20), true, true),
31 inventory::SystemKeywordInfo("WN", Binary(12, 0x20), true, true),
32 inventory::SystemKeywordInfo("RG", Binary(4, 0x20), true, true),
33 inventory::SystemKeywordInfo("FV", Binary(32, 0x20), false, true)}},
34 {"VCEN",
35 {inventory::SystemKeywordInfo("FC", Binary(), true, false),
36 inventory::SystemKeywordInfo("SE", Binary(7, 0x20), true, true)}},
37 {"LXR0", {inventory::SystemKeywordInfo("LX", Binary(), true, false)}},
38 {"UTIL",
Priyanga Ramasamy6d5c6b52023-01-27 09:06:26 -060039 {inventory::SystemKeywordInfo("D0", Binary(1, 0x00), true, true),
40 inventory::SystemKeywordInfo("F5", Binary(16, 0x00), false, true),
41 inventory::SystemKeywordInfo("F6", Binary(16, 0x00), false, true)}}};
Santosh Puranik6b2b5372022-06-02 20:49:02 +053042
Santosh Puranikbd011b22020-01-23 04:05:25 -060043/** @brief Return the hex representation of the incoming byte
44 *
45 * @param [in] c - The input byte
46 * @returns The hex representation of the byte as a character.
47 */
48constexpr auto toHex(size_t c)
49{
50 constexpr auto map = "0123456789abcdef";
51 return map[c];
52}
Deepak Kodihalli76794492017-02-16 23:48:18 -060053
54namespace inventory
55{
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -060056/** @brief API to obtain a dictionary of path -> services
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050057 * where path is in subtree and services is of the type
58 * returned by the GetObject method.
59 *
60 * @param [in] root - Root path for object subtree
61 * @param [in] depth - Maximum subtree depth required
62 * @param [in] interfaces - Array to interfaces for which
63 * result is required.
64 * @return A dictionary of Path -> services
65 */
66MapperResponse
67 getObjectSubtreeForInterfaces(const std::string& root, const int32_t depth,
68 const std::vector<std::string>& interfaces);
69
Deepak Kodihalli76794492017-02-16 23:48:18 -060070} // namespace inventory
71
SunnySrivastava1984f6d541e2020-02-04 12:50:40 -060072/**@brief This API reads 2 Bytes of data and swap the read data
73 * @param[in] iterator- Pointer pointing to the data to be read
74 * @return returns 2 Byte data read at the given pointer
75 */
76openpower::vpd::constants::LE2ByteData
77 readUInt16LE(Binary::const_iterator iterator);
78
SunnySrivastava1984d076da82020-03-05 05:33:35 -060079/** @brief Encodes a keyword for D-Bus.
80 * @param[in] kw - kwd data in string format
81 * @param[in] encoding - required for kwd data
82 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -050083std::string encodeKeyword(const std::string& kw, const std::string& encoding);
SunnySrivastava198443306542020-04-01 02:50:20 -050084
85/** @brief Reads a property from the inventory manager given object path,
86 * intreface and property.
87 * @param[in] obj - object path
88 * @param[in] inf - interface
89 * @param[in] prop - property whose value is fetched
90 * @return [out] - value of the property
91 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -050092std::string readBusProperty(const std::string& obj, const std::string& inf,
93 const std::string& prop);
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050094
Priyanga Ramasamyf1449cd2022-07-13 04:28:30 -050095/** @brief A templated function to read D-Bus properties.
96 *
97 * @param[in] service - Service path
98 * @param[in] object - object path
99 * @param[in] inf - interface
100 * @param[in] prop - property whose value is fetched
101 * @return The property value of its own type.
102 */
103template <typename T>
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500104T readDBusProperty(const std::string& service, const std::string& object,
105 const std::string& inf, const std::string& prop)
Priyanga Ramasamyf1449cd2022-07-13 04:28:30 -0500106{
107 T retVal{};
108 try
109 {
110 auto bus = sdbusplus::bus::new_default();
111 auto properties =
112 bus.new_method_call(service.c_str(), object.c_str(),
113 "org.freedesktop.DBus.Properties", "Get");
114 properties.append(inf);
115 properties.append(prop);
116 auto result = bus.call(properties);
117 result.read(retVal);
118 }
119 catch (const sdbusplus::exception::SdBusError& e)
120 {
121 std::cerr << e.what();
122 }
123 return retVal;
124}
125
Priyanga Ramasamy43ffcf72022-06-08 14:10:11 -0500126/** @brief A templated method to get all D-Bus properties
127 *
128 * @param[in] service - Service path
129 * @param[in] object - Object path
130 * @param[in] inf - Interface
131 *
132 * @return All properties under the given interface.
133 */
134template <typename T>
135T getAllDBusProperty(const std::string& service, const std::string& object,
136 const std::string& inf)
137{
138 T retVal{};
139 try
140 {
141 auto bus = sdbusplus::bus::new_default();
142 auto allProperties =
143 bus.new_method_call(service.c_str(), object.c_str(),
144 "org.freedesktop.DBus.Properties", "GetAll");
145 allProperties.append(inf);
146
147 auto result = bus.call(allProperties);
148 result.read(retVal);
149 }
150 catch (const sdbusplus::exception::SdBusError& e)
151 {
152 std::cerr << e.what();
153 }
154 return retVal;
155}
156
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500157/**
158 * @brief API to create PEL entry
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500159 * The api makes synchronous call to phosphor-logging create api.
Sunny Srivastava0746eee2021-03-22 13:36:54 -0500160 * @param[in] additionalData - Map holding the additional data
161 * @param[in] sev - Severity
162 * @param[in] errIntf - error interface
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500163 */
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500164void createSyncPEL(const std::map<std::string, std::string>& additionalData,
165 const constants::PelSeverity& sev,
166 const std::string& errIntf);
167
168/**
169 * @brief Api to create PEL.
170 * A wrapper api through which sync/async call to phosphor-logging create api
171 * can be made as and when required.
172 * sdBus as nullptr will result in sync call else async call will be made with
173 * just "DESCRIPTION" key/value pair in additional data.
174 * To make asyn call with more fields in additional data call
175 * "sd_bus_call_method_async" in place.
176 *
177 * @param[in] additionalData - Map of additional data.
178 * @param[in] sev - severity of the PEL.
179 * @param[in] errIntf - Error interface to be used in PEL.
180 * @param[in] sdBus - Pointer to Sd-Bus
181 */
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -0500182void createPEL(const std::map<std::string, std::string>& additionalData,
Sunny Srivastavaa2ddc962022-06-29 08:53:16 -0500183 const constants::PelSeverity& sev, const std::string& errIntf,
184 sd_bus* sdBus);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -0500185
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530186/**
187 * @brief getVpdFilePath
188 * Get vpd file path corresponding to the given object path.
189 * @param[in] - json file path
190 * @param[in] - Object path
191 * @return - Vpd file path
192 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500193inventory::VPDfilepath getVpdFilePath(const std::string& jsonFile,
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530194 const std::string& ObjPath);
195
196/**
197 * @brief isPathInJson
198 * API which checks for the presence of the given eeprom path in the given json.
199 * @param[in] - eepromPath
200 * @return - true if the eeprom is present in the json; false otherwise
201 */
202bool isPathInJson(const std::string& eepromPath);
203
204/**
205 * @brief isRecKwInDbusJson
206 * API which checks whether the given keyword under the given record is to be
207 * published on dbus or not. Checks against the keywords present in
208 * dbus_property.json.
209 * @param[in] - record name
210 * @param[in] - keyword name
211 * @return - true if the record-keyword pair is present in dbus_property.json;
212 * false otherwise.
213 */
214bool isRecKwInDbusJson(const std::string& record, const std::string& keyword);
215
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -0500216/**
217 * @brief Check the type of VPD.
218 *
219 * Checks the type of vpd based on the start tag.
220 * @param[in] vector - Vpd data in vector format
221 *
222 * @return enum of type vpdType
223 */
224constants::vpdType vpdTypeCheck(const Binary& vector);
225
SunnySrivastava19849a195542020-09-07 06:04:50 -0500226/*
227 * @brief This method does nothing. Just an empty function to return null
228 * at the end of variadic template args
229 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500230inline std::string getCommand()
SunnySrivastava19849a195542020-09-07 06:04:50 -0500231{
232 return "";
233}
234
235/**
236 * @brief This function to arrange all arguments to make commandy
237 * @param[in] arguments to create the command
238 * @return cmd - command string
239 */
240template <typename T, typename... Types>
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500241inline std::string getCommand(T arg1, Types... args)
SunnySrivastava19849a195542020-09-07 06:04:50 -0500242{
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500243 std::string cmd = " " + arg1 + getCommand(args...);
SunnySrivastava19849a195542020-09-07 06:04:50 -0500244
245 return cmd;
246}
247
248/**
249 * @brief This API takes arguments, creates a shell command line and executes
250 * them.
251 * @param[in] arguments for command
252 * @returns output of that command
253 */
254template <typename T, typename... Types>
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500255inline std::vector<std::string> executeCmd(T&& path, Types... args)
SunnySrivastava19849a195542020-09-07 06:04:50 -0500256{
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500257 std::vector<std::string> stdOutput;
258 std::array<char, 128> buffer;
SunnySrivastava19849a195542020-09-07 06:04:50 -0500259
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500260 std::string cmd = path + getCommand(args...);
SunnySrivastava19849a195542020-09-07 06:04:50 -0500261
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500262 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"),
263 pclose);
SunnySrivastava19849a195542020-09-07 06:04:50 -0500264 if (!pipe)
265 {
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500266 throw std::runtime_error("popen() failed!");
SunnySrivastava19849a195542020-09-07 06:04:50 -0500267 }
268 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
269 {
270 stdOutput.emplace_back(buffer.data());
271 }
272
273 return stdOutput;
274}
275
Alpana Kumarif05effd2021-04-07 07:32:53 -0500276/** @brief This API checks for IM and HW keywords, and based
277 * on these values decides which system json to be used.
278 * @param[in] vpdMap - parsed vpd
279 * @returns System json path
280 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500281std::string getSystemsJson(const Parsed& vpdMap);
Alpana Kumarif05effd2021-04-07 07:32:53 -0500282
283/** @brief Reads HW Keyword from the vpd
284 * @param[in] vpdMap - parsed vpd
285 * @returns value of HW Keyword
286 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500287const std::string getHW(const Parsed& vpdMap);
Alpana Kumarif05effd2021-04-07 07:32:53 -0500288
289/** @brief Reads IM Keyword from the vpd
290 * @param[in] vpdMap - parsed vpd
291 * @returns value of IM Keyword
292 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500293const std::string getIM(const Parsed& vpdMap);
Alpana Kumarif05effd2021-04-07 07:32:53 -0500294
PriyangaRamasamy647868e2020-09-08 17:03:19 +0530295/** @brief Translate udev event generated path to a generic /sys/bus eeprom path
296 * @param[io] file - path generated from udev event.
jinuthomasf457a3e2023-04-13 12:22:48 -0500297 * @param[in] driver - kernel driver used by the device.
PriyangaRamasamy647868e2020-09-08 17:03:19 +0530298 */
jinuthomasf457a3e2023-04-13 12:22:48 -0500299void udevToGenericPath(std::string& file, const std::string& driver);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -0600300
301/**
302 * @brief API to generate a vpd name in some pattern.
303 * This vpd-name denotes name of the bad vpd file.
304 * For i2c eeproms - the pattern of the vpd-name will be
305 * i2c-<bus-number>-<eeprom-address>. For spi eeproms - the pattern of the
306 * vpd-name will be spi-<spi-number>.
307 *
308 * @param[in] file - file path of the vpd
309 * @return the vpd-name.
310 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500311std::string getBadVpdName(const std::string& file);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -0600312
313/**
314 * @brief API which dumps the broken/bad vpd in a directory
315 * When the vpd is bad, this api places the bad vpd file inside
316 * "/tmp/bad-vpd" in BMC, in order to collect bad VPD data as a part of user
317 * initiated BMC dump.
318 *
319 * @param[in] file - bad vpd file path
320 * @param[in] vpdVector - bad vpd vector
321 */
322void dumpBadVpd(const std::string& file, const Binary& vpdVector);
alpana077ce68722021-07-25 13:23:59 -0500323
324/*
325 * @brief This function fetches the value for given keyword in the given record
326 * from vpd data and returns this value.
327 *
328 * @param[in] vpdMap - vpd to find out the data
329 * @param[in] rec - Record under which desired keyword exists
330 * @param[in] kwd - keyword to read the data from
331 *
332 * @returns keyword value if record/keyword combination found
333 * empty string if record or keyword is not found.
334 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500335const std::string getKwVal(const Parsed& vpdMap, const std::string& rec,
336 const std::string& kwd);
alpana077ce68722021-07-25 13:23:59 -0500337
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500338/** @brief This creates a complete command using all it's input parameters,
339 * to bind or unbind the driver.
340 * @param[in] devNameAddr - device address on that bus
341 * @param[in] busType - i2c, spi
342 * @param[in] driverType - type of driver like at24
343 * @param[in] bindOrUnbind - either bind or unbind
344 * @returns Command to bind or unbind the driver.
345 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500346inline std::string createBindUnbindDriverCmnd(const std::string& devNameAddr,
347 const std::string& busType,
348 const std::string& driverType,
349 const std::string& bindOrUnbind)
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500350{
351 return ("echo " + devNameAddr + " > /sys/bus/" + busType + "/drivers/" +
352 driverType + "/" + bindOrUnbind);
353}
354
Priyanga Ramasamy02434932021-10-07 16:26:05 -0500355/**
356 * @brief Get Printable Value
357 *
GiridhariKrishnan63639102023-03-02 05:55:47 -0600358 * Checks if the value has non printable characters.
Priyanga Ramasamy02434932021-10-07 16:26:05 -0500359 * Returns hex value if non printable char is found else
360 * returns ascii value.
361 *
GiridhariKrishnan63639102023-03-02 05:55:47 -0600362 * @param[in] kwVal - Reference of the input data, Keyword value
Priyanga Ramasamy02434932021-10-07 16:26:05 -0500363 * @return printable value - either in hex or in ascii.
364 */
GiridhariKrishnan63639102023-03-02 05:55:47 -0600365std::string getPrintableValue(const std::variant<Binary, std::string>& kwVal);
Priyanga Ramasamyc9ecf8e2021-10-08 02:28:52 -0500366
367/**
GiridhariKrishnan63639102023-03-02 05:55:47 -0600368 * @brief Convert array to hex string.
369 * @param[in] kwVal - input data, Keyword value
Priyanga Ramasamyc9ecf8e2021-10-08 02:28:52 -0500370 * @return hexadecimal string of bytes.
371 */
GiridhariKrishnan63639102023-03-02 05:55:47 -0600372std::string hexString(const std::variant<Binary, std::string>& kwVal);
Priyanga Ramasamy02434932021-10-07 16:26:05 -0500373
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -0600374/**
Santosh Puranik53b38ed2022-04-10 23:15:22 +0530375 * @brief Return presence of the FRU.
376 *
377 * This API returns the presence information of the FRU corresponding to the
378 * given EEPROM. If the JSON contains no information about presence detect, this
379 * will return an empty optional. Else it will get the presence GPIO information
380 * from the JSON and return the appropriate present status.
381 * In case of GPIO find/read errors, it will return false.
382 *
383 * @param[in] json - The VPD JSON
384 * @param[in] file - EEPROM file path
385 * @return Empty optional if there is no presence info. Else returns presence
386 * based on the GPIO read.
387 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500388std::optional<bool> isPresent(const nlohmann::json& json,
389 const std::string& file);
Santosh Puranik53b38ed2022-04-10 23:15:22 +0530390
391/**
392 * @brief Performs any pre-action needed to get the FRU setup for
393 * collection.
Alpana Kumari735dee92022-03-25 01:24:40 -0500394 *
395 * @param[in] json - json object
396 * @param[in] file - eeprom file path
397 * @return - success or failure
398 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500399bool executePreAction(const nlohmann::json& json, const std::string& file);
Alpana Kumari735dee92022-03-25 01:24:40 -0500400
401/**
402 * @brief This API will be called at the end of VPD collection to perform any
403 * post actions.
404 *
405 * @param[in] json - json object
406 * @param[in] file - eeprom file path
407 */
Priyanga Ramasamye0084322022-09-27 06:28:33 -0500408void executePostFailAction(const nlohmann::json& json, const std::string& file);
Alpana Kumari735dee92022-03-25 01:24:40 -0500409
410/**
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -0600411 * @brief Helper function to insert or merge in map.
412 *
413 * This method checks in the given inventory::InterfaceMap if the given
414 * interface key is existing or not. If the interface key already exists, given
415 * property map is inserted into it. If the key does'nt exist then given
416 * interface and property map pair is newly created. If the property present in
417 * propertymap already exist in the InterfaceMap, then the new property value is
418 * ignored.
419 *
420 * @param[in,out] map - map object of type inventory::InterfaceMap only.
421 * @param[in] interface - Interface name.
422 * @param[in] property - new property map that needs to be emplaced.
423 */
424void insertOrMerge(inventory::InterfaceMap& map,
425 const inventory::Interface& interface,
426 inventory::PropertyMap&& property);
427
Santosh Puranikf2d3b532022-04-19 06:44:07 -0500428/**
429 * @brief Utility API to set a D-Bus property
430 *
431 * This calls org.freedesktop.DBus.Properties;Set method with the supplied
432 * arguments
433 *
434 * @tparam T Template type of the D-Bus property
435 * @param service[in] - The D-Bus service name.
436 * @param object[in] - The D-Bus object on which the property is to be set.
437 * @param interface[in] - The D-Bus interface to which the property belongs.
438 * @param propertyName[in] - The name of the property to set.
439 * @param propertyValue[in] - The value of the property.
440 */
441template <typename T>
442void setBusProperty(const std::string& service, const std::string& object,
443 const std::string& interface,
444 const std::string& propertyName,
445 const std::variant<T>& propertyValue)
446{
447 try
448 {
449 auto bus = sdbusplus::bus::new_default();
450 auto method =
451 bus.new_method_call(service.c_str(), object.c_str(),
452 "org.freedesktop.DBus.Properties", "Set");
453 method.append(interface);
454 method.append(propertyName);
455 method.append(propertyValue);
456
457 bus.call(method);
458 }
459 catch (const sdbusplus::exception::SdBusError& e)
460 {
461 std::cerr << e.what() << std::endl;
462 }
463}
464
465/**
466 * @brief Reads BIOS Attribute by name.
467 *
468 * @param attrName[in] - The BIOS attribute name.
469 * @return std::variant<int64_t, std::string> - The BIOS attribute value.
470 */
471std::variant<int64_t, std::string>
472 readBIOSAttribute(const std::string& attrName);
Priyanga Ramasamy335873f2022-05-18 01:31:54 -0500473
474/**
475 * @brief Returns the power state for chassis0
476 * @return The chassis power state.
477 */
478std::string getPowerState();
Santosh Puranik6b2b5372022-06-02 20:49:02 +0530479
480/**
481 * @brief Reads VPD from the supplied EEPROM
482 *
483 * This function reads the given VPD EEPROM file and returns its contents as a
484 * byte array. It handles any offsets into the file that need to be taken care
485 * of by looking up the VPD JSON for a possible offset key.
486 *
487 * @param js[in] - The VPD JSON Object
488 * @param file[in] - The path to the EEPROM to read
489 * @return A byte array containing the raw VPD.
490 */
491Binary getVpdDataInVector(const nlohmann::json& js, const std::string& file);
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700492} // namespace vpd
Santosh Puranikf2d3b532022-04-19 06:44:07 -0500493} // namespace openpower