blob: e1f3445d5de172aba5cb98d94f3380d32ae9158d [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
79/**
80 * @brief API to create PEL entry
Sunny Srivastava0746eee2021-03-22 13:36:54 -050081 * @param[in] additionalData - Map holding the additional data
82 * @param[in] sev - Severity
83 * @param[in] errIntf - error interface
SunnySrivastava1984a20be8e2020-08-26 02:00:50 -050084 */
85void createPEL(const std::map<std::string, std::string>& additionalData,
Sunny Srivastava0746eee2021-03-22 13:36:54 -050086 const constants::PelSeverity& sev, const std::string& errIntf);
SunnySrivastava19849094d4f2020-08-05 09:32:29 -050087
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053088/**
89 * @brief getVpdFilePath
90 * Get vpd file path corresponding to the given object path.
91 * @param[in] - json file path
92 * @param[in] - Object path
93 * @return - Vpd file path
94 */
95inventory::VPDfilepath getVpdFilePath(const string& jsonFile,
96 const std::string& ObjPath);
97
98/**
99 * @brief isPathInJson
100 * API which checks for the presence of the given eeprom path in the given json.
101 * @param[in] - eepromPath
102 * @return - true if the eeprom is present in the json; false otherwise
103 */
104bool isPathInJson(const std::string& eepromPath);
105
106/**
107 * @brief isRecKwInDbusJson
108 * API which checks whether the given keyword under the given record is to be
109 * published on dbus or not. Checks against the keywords present in
110 * dbus_property.json.
111 * @param[in] - record name
112 * @param[in] - keyword name
113 * @return - true if the record-keyword pair is present in dbus_property.json;
114 * false otherwise.
115 */
116bool isRecKwInDbusJson(const std::string& record, const std::string& keyword);
117
Sunny Srivastava6c71c9d2021-04-15 04:43:54 -0500118/**
119 * @brief Check the type of VPD.
120 *
121 * Checks the type of vpd based on the start tag.
122 * @param[in] vector - Vpd data in vector format
123 *
124 * @return enum of type vpdType
125 */
126constants::vpdType vpdTypeCheck(const Binary& vector);
127
SunnySrivastava19849a195542020-09-07 06:04:50 -0500128/*
129 * @brief This method does nothing. Just an empty function to return null
130 * at the end of variadic template args
131 */
132inline string getCommand()
133{
134 return "";
135}
136
137/**
138 * @brief This function to arrange all arguments to make commandy
139 * @param[in] arguments to create the command
140 * @return cmd - command string
141 */
142template <typename T, typename... Types>
143inline string getCommand(T arg1, Types... args)
144{
145 string cmd = " " + arg1 + getCommand(args...);
146
147 return cmd;
148}
149
150/**
151 * @brief This API takes arguments, creates a shell command line and executes
152 * them.
153 * @param[in] arguments for command
154 * @returns output of that command
155 */
156template <typename T, typename... Types>
157inline vector<string> executeCmd(T&& path, Types... args)
158{
159 vector<string> stdOutput;
160 array<char, 128> buffer;
161
162 string cmd = path + getCommand(args...);
163
164 unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
165 if (!pipe)
166 {
167 throw runtime_error("popen() failed!");
168 }
169 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
170 {
171 stdOutput.emplace_back(buffer.data());
172 }
173
174 return stdOutput;
175}
176
Alpana Kumarif05effd2021-04-07 07:32:53 -0500177/** @brief This API checks for IM and HW keywords, and based
178 * on these values decides which system json to be used.
179 * @param[in] vpdMap - parsed vpd
180 * @returns System json path
181 */
182string getSystemsJson(const Parsed& vpdMap);
183
184/** @brief Reads HW Keyword from the vpd
185 * @param[in] vpdMap - parsed vpd
186 * @returns value of HW Keyword
187 */
188const string getHW(const Parsed& vpdMap);
189
190/** @brief Reads IM Keyword from the vpd
191 * @param[in] vpdMap - parsed vpd
192 * @returns value of IM Keyword
193 */
194const string getIM(const Parsed& vpdMap);
195
PriyangaRamasamy647868e2020-09-08 17:03:19 +0530196/** @brief Translate udev event generated path to a generic /sys/bus eeprom path
197 * @param[io] file - path generated from udev event.
198 */
199void udevToGenericPath(string& file);
PriyangaRamasamyc2fe40f2021-03-02 06:27:33 -0600200
201/**
202 * @brief API to generate a vpd name in some pattern.
203 * This vpd-name denotes name of the bad vpd file.
204 * For i2c eeproms - the pattern of the vpd-name will be
205 * i2c-<bus-number>-<eeprom-address>. For spi eeproms - the pattern of the
206 * vpd-name will be spi-<spi-number>.
207 *
208 * @param[in] file - file path of the vpd
209 * @return the vpd-name.
210 */
211string getBadVpdName(const string& file);
212
213/**
214 * @brief API which dumps the broken/bad vpd in a directory
215 * When the vpd is bad, this api places the bad vpd file inside
216 * "/tmp/bad-vpd" in BMC, in order to collect bad VPD data as a part of user
217 * initiated BMC dump.
218 *
219 * @param[in] file - bad vpd file path
220 * @param[in] vpdVector - bad vpd vector
221 */
222void dumpBadVpd(const std::string& file, const Binary& vpdVector);
alpana077ce68722021-07-25 13:23:59 -0500223
224/*
225 * @brief This function fetches the value for given keyword in the given record
226 * from vpd data and returns this value.
227 *
228 * @param[in] vpdMap - vpd to find out the data
229 * @param[in] rec - Record under which desired keyword exists
230 * @param[in] kwd - keyword to read the data from
231 *
232 * @returns keyword value if record/keyword combination found
233 * empty string if record or keyword is not found.
234 */
235const string getKwVal(const Parsed& vpdMap, const string& rec,
236 const string& kwd);
237
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500238/** @brief This creates a complete command using all it's input parameters,
239 * to bind or unbind the driver.
240 * @param[in] devNameAddr - device address on that bus
241 * @param[in] busType - i2c, spi
242 * @param[in] driverType - type of driver like at24
243 * @param[in] bindOrUnbind - either bind or unbind
244 * @returns Command to bind or unbind the driver.
245 */
246inline string createBindUnbindDriverCmnd(const string& devNameAddr,
247 const string& busType,
248 const string& driverType,
249 const string& bindOrUnbind)
250{
251 return ("echo " + devNameAddr + " > /sys/bus/" + busType + "/drivers/" +
252 driverType + "/" + bindOrUnbind);
253}
254
Priyanga Ramasamy02434932021-10-07 16:26:05 -0500255/**
256 * @brief Get Printable Value
257 *
258 * Checks if the vector value has non printable characters.
259 * Returns hex value if non printable char is found else
260 * returns ascii value.
261 *
262 * @param[in] vector - Reference of the Binary vector
263 * @return printable value - either in hex or in ascii.
264 */
Priyanga Ramasamyc9ecf8e2021-10-08 02:28:52 -0500265string getPrintableValue(const Binary& vec);
266
267/**
268 * @brief Convert byte array to hex string.
269 * @param[in] vec - byte array
270 * @return hexadecimal string of bytes.
271 */
272string byteArrayToHexString(const Binary& vec);
Priyanga Ramasamy02434932021-10-07 16:26:05 -0500273
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -0600274/**
Santosh Puranik53b38ed2022-04-10 23:15:22 +0530275 * @brief Return presence of the FRU.
276 *
277 * This API returns the presence information of the FRU corresponding to the
278 * given EEPROM. If the JSON contains no information about presence detect, this
279 * will return an empty optional. Else it will get the presence GPIO information
280 * from the JSON and return the appropriate present status.
281 * In case of GPIO find/read errors, it will return false.
282 *
283 * @param[in] json - The VPD JSON
284 * @param[in] file - EEPROM file path
285 * @return Empty optional if there is no presence info. Else returns presence
286 * based on the GPIO read.
287 */
288std::optional<bool> isPresent(const nlohmann::json& json, const string& file);
289
290/**
291 * @brief Performs any pre-action needed to get the FRU setup for
292 * collection.
Alpana Kumari735dee92022-03-25 01:24:40 -0500293 *
294 * @param[in] json - json object
295 * @param[in] file - eeprom file path
296 * @return - success or failure
297 */
298bool executePreAction(const nlohmann::json& json, const string& file);
299
300/**
301 * @brief This API will be called at the end of VPD collection to perform any
302 * post actions.
303 *
304 * @param[in] json - json object
305 * @param[in] file - eeprom file path
306 */
307void executePostFailAction(const nlohmann::json& json, const string& file);
308
309/**
Priyanga Ramasamyaa8a8932022-01-27 09:12:41 -0600310 * @brief Helper function to insert or merge in map.
311 *
312 * This method checks in the given inventory::InterfaceMap if the given
313 * interface key is existing or not. If the interface key already exists, given
314 * property map is inserted into it. If the key does'nt exist then given
315 * interface and property map pair is newly created. If the property present in
316 * propertymap already exist in the InterfaceMap, then the new property value is
317 * ignored.
318 *
319 * @param[in,out] map - map object of type inventory::InterfaceMap only.
320 * @param[in] interface - Interface name.
321 * @param[in] property - new property map that needs to be emplaced.
322 */
323void insertOrMerge(inventory::InterfaceMap& map,
324 const inventory::Interface& interface,
325 inventory::PropertyMap&& property);
326
Santosh Puranikf2d3b532022-04-19 06:44:07 -0500327/**
328 * @brief Utility API to set a D-Bus property
329 *
330 * This calls org.freedesktop.DBus.Properties;Set method with the supplied
331 * arguments
332 *
333 * @tparam T Template type of the D-Bus property
334 * @param service[in] - The D-Bus service name.
335 * @param object[in] - The D-Bus object on which the property is to be set.
336 * @param interface[in] - The D-Bus interface to which the property belongs.
337 * @param propertyName[in] - The name of the property to set.
338 * @param propertyValue[in] - The value of the property.
339 */
340template <typename T>
341void setBusProperty(const std::string& service, const std::string& object,
342 const std::string& interface,
343 const std::string& propertyName,
344 const std::variant<T>& propertyValue)
345{
346 try
347 {
348 auto bus = sdbusplus::bus::new_default();
349 auto method =
350 bus.new_method_call(service.c_str(), object.c_str(),
351 "org.freedesktop.DBus.Properties", "Set");
352 method.append(interface);
353 method.append(propertyName);
354 method.append(propertyValue);
355
356 bus.call(method);
357 }
358 catch (const sdbusplus::exception::SdBusError& e)
359 {
360 std::cerr << e.what() << std::endl;
361 }
362}
363
364/**
365 * @brief Reads BIOS Attribute by name.
366 *
367 * @param attrName[in] - The BIOS attribute name.
368 * @return std::variant<int64_t, std::string> - The BIOS attribute value.
369 */
370std::variant<int64_t, std::string>
371 readBIOSAttribute(const std::string& attrName);
Priyanga Ramasamy335873f2022-05-18 01:31:54 -0500372
373/**
374 * @brief Returns the power state for chassis0
375 * @return The chassis power state.
376 */
377std::string getPowerState();
Santosh Puranik6b2b5372022-06-02 20:49:02 +0530378
379/**
380 * @brief Reads VPD from the supplied EEPROM
381 *
382 * This function reads the given VPD EEPROM file and returns its contents as a
383 * byte array. It handles any offsets into the file that need to be taken care
384 * of by looking up the VPD JSON for a possible offset key.
385 *
386 * @param js[in] - The VPD JSON Object
387 * @param file[in] - The path to the EEPROM to read
388 * @return A byte array containing the raw VPD.
389 */
390Binary getVpdDataInVector(const nlohmann::json& js, const std::string& file);
Patrick Venturec83c4dc2018-11-01 16:29:18 -0700391} // namespace vpd
Santosh Puranikf2d3b532022-04-19 06:44:07 -0500392} // namespace openpower