blob: 42ebd67c73663d3b1adb89daf720b4fc4df8a8eb [file] [log] [blame]
Patrick Venturec9508db2018-10-16 17:18:43 -07001#include "fru-area.hpp"
2#include "frup.hpp"
3#include "types.hpp"
4
Vishwa4be4b7a2015-10-31 22:55:50 -05005#include <dlfcn.h>
Patrick Venturec9508db2018-10-16 17:18:43 -07006#include <host-ipmid/ipmid-api.h>
7#include <mapper.h>
Vishwa4be4b7a2015-10-31 22:55:50 -05008#include <systemd/sd-bus.h>
Chris Austenb45c4cb2015-11-01 06:34:56 -06009#include <unistd.h>
Patrick Venturec9508db2018-10-16 17:18:43 -070010
11#include <algorithm>
Patrick Venture5c787212018-10-17 13:48:23 -070012#include <cstdio>
13#include <cstring>
Patrick Venturec9508db2018-10-16 17:18:43 -070014#include <exception>
15#include <fstream>
vishwac93d6d42015-12-16 11:55:16 -060016#include <iostream>
17#include <memory>
Patrick Venture5739ac32018-10-16 19:17:41 -070018#include <phosphor-logging/log.hpp>
Ratan Gupta0b77cfa2017-01-31 17:32:31 +053019#include <sdbusplus/server.hpp>
Patrick Venturec9508db2018-10-16 17:18:43 -070020#include <sstream>
21#include <vector>
Vishwa4be4b7a2015-10-31 22:55:50 -050022
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060023using namespace ipmi::vpd;
Patrick Venture5739ac32018-10-16 19:17:41 -070024using namespace phosphor::logging;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060025
Ratan Guptacb0d4e52016-12-22 19:05:57 +053026extern const FruMap frus;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060027extern const std::map<Path, InterfaceMap> extras;
Ratan Guptacb0d4e52016-12-22 19:05:57 +053028
Patrick Venture234b7352018-10-20 09:37:09 -070029namespace
30{
31
32//------------------------------------------------------------
33// Cleanup routine
34// Must always be called as last reference to fru_fp.
35//------------------------------------------------------------
36int cleanupError(FILE* fru_fp, fru_area_vec_t& fru_area_vec)
37{
38 if (fru_fp != NULL)
39 {
40 std::fclose(fru_fp);
41 }
42
43 if (!(fru_area_vec.empty()))
44 {
45 fru_area_vec.clear();
46 }
47
48 return -1;
49}
50
Patrick Venturede8ea562018-10-20 09:44:19 -070051//------------------------------------------------------------------------
52// Gets the value of the key from the fru dictionary of the given section.
53// FRU dictionary is parsed fru data for all the sections.
54//------------------------------------------------------------------------
55std::string getFRUValue(const std::string& section, const std::string& key,
56 const std::string& delimiter, IPMIFruInfo& fruData)
57{
58
59 auto minIndexValue = 0;
60 auto maxIndexValue = 0;
61 std::string fruValue = "";
62
63 if (section == "Board")
64 {
65 minIndexValue = OPENBMC_VPD_KEY_BOARD_MFG_DATE;
66 maxIndexValue = OPENBMC_VPD_KEY_BOARD_MAX;
67 }
68 else if (section == "Product")
69 {
70 minIndexValue = OPENBMC_VPD_KEY_PRODUCT_MFR;
71 maxIndexValue = OPENBMC_VPD_KEY_PRODUCT_MAX;
72 }
73 else if (section == "Chassis")
74 {
75 minIndexValue = OPENBMC_VPD_KEY_CHASSIS_TYPE;
76 maxIndexValue = OPENBMC_VPD_KEY_CHASSIS_MAX;
77 }
78
79 auto first = fruData.cbegin() + minIndexValue;
80 auto last = first + (maxIndexValue - minIndexValue) + 1;
81
82 auto itr =
83 std::find_if(first, last, [&key](auto& e) { return key == e.first; });
84
85 if (itr != last)
86 {
87 fruValue = itr->second;
88 }
89
90 // if the key is custom property then the value could be in two formats.
91 // 1) custom field 2 = "value".
92 // 2) custom field 2 = "key:value".
93 // if delimiter length = 0 i.e custom field 2 = "value"
94
95 constexpr auto customProp = "Custom Field";
96 if (key.find(customProp) != std::string::npos)
97 {
98 if (delimiter.length() > 0)
99 {
100 size_t delimiterpos = fruValue.find(delimiter);
101 if (delimiterpos != std::string::npos)
102 fruValue = fruValue.substr(delimiterpos + 1);
103 }
104 }
105 return fruValue;
106}
107
Patrick Venture234b7352018-10-20 09:37:09 -0700108} // namespace
109
vishwac93d6d42015-12-16 11:55:16 -0600110//----------------------------------------------------------------
111// Constructor
112//----------------------------------------------------------------
113ipmi_fru::ipmi_fru(const uint8_t fruid, const ipmi_fru_area_type type,
Patrick Venturec9508db2018-10-16 17:18:43 -0700114 sd_bus* bus_type, bool bmc_fru)
vishwac93d6d42015-12-16 11:55:16 -0600115{
116 iv_fruid = fruid;
117 iv_type = type;
118 iv_bmc_fru = bmc_fru;
119 iv_bus_type = bus_type;
120 iv_valid = false;
121 iv_data = NULL;
122 iv_present = false;
123
Patrick Venturec9508db2018-10-16 17:18:43 -0700124 if (iv_type == IPMI_FRU_AREA_INTERNAL_USE)
vishwac93d6d42015-12-16 11:55:16 -0600125 {
126 iv_name = "INTERNAL_";
127 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700128 else if (iv_type == IPMI_FRU_AREA_CHASSIS_INFO)
vishwac93d6d42015-12-16 11:55:16 -0600129 {
130 iv_name = "CHASSIS_";
131 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700132 else if (iv_type == IPMI_FRU_AREA_BOARD_INFO)
vishwac93d6d42015-12-16 11:55:16 -0600133 {
134 iv_name = "BOARD_";
135 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700136 else if (iv_type == IPMI_FRU_AREA_PRODUCT_INFO)
vishwac93d6d42015-12-16 11:55:16 -0600137 {
138 iv_name = "PRODUCT_";
139 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700140 else if (iv_type == IPMI_FRU_AREA_MULTI_RECORD)
vishwac93d6d42015-12-16 11:55:16 -0600141 {
142 iv_name = "MULTI_";
143 }
144 else
145 {
146 iv_name = IPMI_FRU_AREA_TYPE_MAX;
Patrick Venture5c787212018-10-17 13:48:23 -0700147 log<level::ERR>("Invalid Area", entry("TYPE=%d", iv_type));
vishwac93d6d42015-12-16 11:55:16 -0600148 }
149}
150
151//-----------------------------------------------------
152// For a FRU area type, accepts the data and updates
153// area specific data.
154//-----------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700155void ipmi_fru::set_data(const uint8_t* data, const size_t len)
vishwac93d6d42015-12-16 11:55:16 -0600156{
157 iv_len = len;
158 iv_data = new uint8_t[len];
Patrick Venture5c787212018-10-17 13:48:23 -0700159 std::memcpy(iv_data, data, len);
vishwac93d6d42015-12-16 11:55:16 -0600160}
161
162//-----------------------------------------------------
163// Sets the dbus parameters
164//-----------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700165void ipmi_fru::update_dbus_paths(const char* bus_name, const char* obj_path,
166 const char* intf_name)
vishwac93d6d42015-12-16 11:55:16 -0600167{
168 iv_bus_name = bus_name;
169 iv_obj_path = obj_path;
170 iv_intf_name = intf_name;
171}
172
173//-------------------
174// Destructor
175//-------------------
176ipmi_fru::~ipmi_fru()
177{
Patrick Venturec9508db2018-10-16 17:18:43 -0700178 if (iv_data != NULL)
vishwac93d6d42015-12-16 11:55:16 -0600179 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700180 delete[] iv_data;
vishwac93d6d42015-12-16 11:55:16 -0600181 iv_data = NULL;
182 }
vishwac93d6d42015-12-16 11:55:16 -0600183}
184
Vishwa4be4b7a2015-10-31 22:55:50 -0500185//------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600186// Takes the pointer to stream of bytes and length
vishwac93d6d42015-12-16 11:55:16 -0600187// and returns the 8 bit checksum
188// This algo is per IPMI V2.0 spec
Vishwa4be4b7a2015-10-31 22:55:50 -0500189//-------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700190unsigned char calculate_crc(const unsigned char* data, size_t len)
Vishwa4be4b7a2015-10-31 22:55:50 -0500191{
192 char crc = 0;
vishwac93d6d42015-12-16 11:55:16 -0600193 size_t byte = 0;
Vishwa4be4b7a2015-10-31 22:55:50 -0500194
Patrick Venturec9508db2018-10-16 17:18:43 -0700195 for (byte = 0; byte < len; byte++)
Vishwa4be4b7a2015-10-31 22:55:50 -0500196 {
197 crc += *data++;
198 }
vishwaf3ca3522015-12-02 10:35:13 -0600199
Patrick Venturec9508db2018-10-16 17:18:43 -0700200 return (-crc);
Vishwa4be4b7a2015-10-31 22:55:50 -0500201}
202
203//---------------------------------------------------------------------
204// Accepts a fru area offset in commom hdr and tells which area it is.
205//---------------------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600206ipmi_fru_area_type get_fru_area_type(uint8_t area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500207{
208 ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
209
Patrick Venturec9508db2018-10-16 17:18:43 -0700210 switch (area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500211 {
212 case IPMI_FRU_INTERNAL_OFFSET:
213 type = IPMI_FRU_AREA_INTERNAL_USE;
214 break;
215
216 case IPMI_FRU_CHASSIS_OFFSET:
217 type = IPMI_FRU_AREA_CHASSIS_INFO;
218 break;
219
220 case IPMI_FRU_BOARD_OFFSET:
221 type = IPMI_FRU_AREA_BOARD_INFO;
222 break;
223
224 case IPMI_FRU_PRODUCT_OFFSET:
225 type = IPMI_FRU_AREA_PRODUCT_INFO;
226 break;
227
228 case IPMI_FRU_MULTI_OFFSET:
229 type = IPMI_FRU_AREA_MULTI_RECORD;
230 break;
231
232 default:
233 type = IPMI_FRU_AREA_TYPE_MAX;
234 }
235
236 return type;
237}
238
vishwac93d6d42015-12-16 11:55:16 -0600239///-----------------------------------------------
240// Validates the data for crc and mandatory fields
241///-----------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700242int verify_fru_data(const uint8_t* data, const size_t len)
vishwac93d6d42015-12-16 11:55:16 -0600243{
244 uint8_t checksum = 0;
245 int rc = -1;
246
247 // Validate for first byte to always have a value of [1]
Patrick Venturec9508db2018-10-16 17:18:43 -0700248 if (data[0] != IPMI_FRU_HDR_BYTE_ZERO)
vishwac93d6d42015-12-16 11:55:16 -0600249 {
Patrick Venture5c787212018-10-17 13:48:23 -0700250 log<level::ERR>("Invalid entry in byte-0",
251 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600252 return rc;
253 }
254#ifdef __IPMI_DEBUG__
255 else
256 {
Patrick Venture5c787212018-10-17 13:48:23 -0700257 log<level::DEBUG>("Validated in entry_1 of fru_data",
258 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600259 }
260#endif
261
262 // See if the calculated CRC matches with the embedded one.
263 // CRC to be calculated on all except the last one that is CRC itself.
264 checksum = calculate_crc(data, len - 1);
Patrick Venturec9508db2018-10-16 17:18:43 -0700265 if (checksum != data[len - 1])
vishwac93d6d42015-12-16 11:55:16 -0600266 {
267#ifdef __IPMI_DEBUG__
Patrick Venture5c787212018-10-17 13:48:23 -0700268 log<level::ERR>(
269 "Checksum mismatch",
270 entry("Calculated=0x%X", static_cast<uint32_t>(checksum)),
271 entry("Embedded=0x%X", static_cast<uint32_t>(data[len])));
vishwac93d6d42015-12-16 11:55:16 -0600272#endif
273 return rc;
274 }
275#ifdef __IPMI_DEBUG__
276 else
277 {
Patrick Venture5c787212018-10-17 13:48:23 -0700278 log<level::DEBUG>("Checksum matches");
vishwac93d6d42015-12-16 11:55:16 -0600279 }
280#endif
281
282 return EXIT_SUCCESS;
283}
284
Patrick Venturec9508db2018-10-16 17:18:43 -0700285// Get the inventory service from the mapper.
286auto getService(sdbusplus::bus::bus& bus, const std::string& intf,
287 const std::string& path)
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530288{
Patrick Venturec9508db2018-10-16 17:18:43 -0700289 auto mapperCall =
290 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
291 "/xyz/openbmc_project/object_mapper",
292 "xyz.openbmc_project.ObjectMapper", "GetObject");
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530293
294 mapperCall.append(path);
295 mapperCall.append(std::vector<std::string>({intf}));
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530296 std::map<std::string, std::vector<std::string>> mapperResponse;
Patrick Venture5739ac32018-10-16 19:17:41 -0700297
298 try
299 {
300 auto mapperResponseMsg = bus.call(mapperCall);
301 mapperResponseMsg.read(mapperResponse);
302 }
303 catch (const sdbusplus::exception::SdBusError& ex)
304 {
305 log<level::ERR>("Exception from sdbus call",
306 entry("WHAT=%s", ex.what()));
307 throw;
308 }
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530309
310 if (mapperResponse.begin() == mapperResponse.end())
311 {
312 throw std::runtime_error("ERROR in reading the mapper response");
313 }
314
315 return mapperResponse.begin()->first;
316}
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530317
318//------------------------------------------------------------------------
Vishwa4be4b7a2015-10-31 22:55:50 -0500319// Takes FRU data, invokes Parser for each fru record area and updates
320// Inventory
321//------------------------------------------------------------------------
Patrick Williams21eb0432017-03-31 11:40:48 -0500322int ipmi_update_inventory(fru_area_vec_t& area_vec, sd_bus* bus_sd)
Vishwa4be4b7a2015-10-31 22:55:50 -0500323{
vishwac93d6d42015-12-16 11:55:16 -0600324 // Generic error reporter
Vishwa4be4b7a2015-10-31 22:55:50 -0500325 int rc = 0;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530326 uint8_t fruid = 0;
327 IPMIFruInfo fruData;
Vishwa4be4b7a2015-10-31 22:55:50 -0500328
Vishwa4be4b7a2015-10-31 22:55:50 -0500329 // For each FRU area, extract the needed data , get it parsed and update
330 // the Inventory.
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530331 for (const auto& fruArea : area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500332 {
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530333 fruid = fruArea->get_fruid();
Vishwa4be4b7a2015-10-31 22:55:50 -0500334 // Fill the container with information
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530335 rc = parse_fru_area((fruArea)->get_type(), (void*)(fruArea)->get_data(),
336 (fruArea)->get_len(), fruData);
337 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500338 {
Patrick Venture5c787212018-10-17 13:48:23 -0700339 log<level::ERR>("Error parsing FRU records");
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530340 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500341 }
342 } // END walking the vector of areas and updating
343
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530344 // For each Fru we have the list of instances which needs to be updated.
345 // Each instance object implements certain interfaces.
346 // Each Interface is having Dbus properties.
347 // Each Dbus Property would be having metaData(eg section,VpdPropertyName).
348
349 // Here we are just printing the object,interface and the properties.
350 // which needs to be called with the new inventory manager implementation.
Patrick Williams49a6fcd2017-04-18 11:49:07 -0500351 sdbusplus::bus::bus bus{bus_sd};
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530352 using namespace std::string_literals;
353 static const auto intf = "xyz.openbmc_project.Inventory.Manager"s;
Brad Bishopec7648d2017-02-23 11:30:16 -0500354 static const auto path = "/xyz/openbmc_project/inventory"s;
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530355 std::string service;
356 try
357 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700358 service = getService(bus, intf, path);
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530359 }
Patrick Venture5739ac32018-10-16 19:17:41 -0700360 catch (const std::exception& e)
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530361 {
362 std::cerr << e.what() << "\n";
363 return -1;
364 }
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530365
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530366 auto iter = frus.find(fruid);
367 if (iter == frus.end())
368 {
Patrick Venture5c787212018-10-17 13:48:23 -0700369 log<level::ERR>("Unable to get the fru info",
370 entry("FRU=%d", static_cast<int>(fruid)));
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530371 return -1;
372 }
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530373
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530374 auto& instanceList = iter->second;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530375 if (instanceList.size() <= 0)
376 {
Patrick Venture5c787212018-10-17 13:48:23 -0700377 log<level::DEBUG>("Object list empty for this FRU",
378 entry("FRU=%d", static_cast<int>(fruid)));
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530379 }
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530380
381 ObjectMap objects;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530382 for (auto& instance : instanceList)
383 {
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530384 InterfaceMap interfaces;
Ratan Guptac19c0542018-02-04 23:24:44 +0530385 const auto& extrasIter = extras.find(instance.path);
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530386
Ratan Guptac19c0542018-02-04 23:24:44 +0530387 for (auto& interfaceList : instance.interfaces)
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530388 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700389 PropertyMap props; // store all the properties
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530390 for (auto& properties : interfaceList.second)
391 {
Patrick Williamsaeb726d2017-06-01 19:00:43 -0500392 std::string value;
393 decltype(auto) pdata = properties.second;
Ratan Guptacdde23d2017-02-15 09:29:54 +0530394
Patrick Williamsaeb726d2017-06-01 19:00:43 -0500395 if (!pdata.section.empty() && !pdata.property.empty())
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530396 {
Patrick Williamsaeb726d2017-06-01 19:00:43 -0500397 value = getFRUValue(pdata.section, pdata.property,
398 pdata.delimiter, fruData);
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530399 }
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530400 props.emplace(std::move(properties.first), std::move(value));
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530401 }
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600402 // Check and update extra properties
Patrick Venturec9508db2018-10-16 17:18:43 -0700403 if (extras.end() != extrasIter)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600404 {
405 const auto& propsIter =
406 (extrasIter->second).find(interfaceList.first);
Patrick Venturec9508db2018-10-16 17:18:43 -0700407 if ((extrasIter->second).end() != propsIter)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600408 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700409 for (const auto& map : propsIter->second)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600410 {
411 props.emplace(map.first, map.second);
412 }
413 }
414 }
415 interfaces.emplace(std::move(interfaceList.first),
416 std::move(props));
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530417 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500418
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600419 // Call the inventory manager
Ratan Guptac19c0542018-02-04 23:24:44 +0530420 sdbusplus::message::object_path path = instance.path;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600421 // Check and update extra properties
Patrick Venturec9508db2018-10-16 17:18:43 -0700422 if (extras.end() != extrasIter)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600423 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700424 for (const auto& entry : extrasIter->second)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600425 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700426 if (interfaces.end() == interfaces.find(entry.first))
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600427 {
428 interfaces.emplace(entry.first, entry.second);
429 }
430 }
431 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700432 objects.emplace(path, interfaces);
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530433 }
434
Patrick Venturec9508db2018-10-16 17:18:43 -0700435 auto pimMsg = bus.new_method_call(service.c_str(), path.c_str(),
436 intf.c_str(), "Notify");
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530437 pimMsg.append(std::move(objects));
Patrick Venture5739ac32018-10-16 19:17:41 -0700438
439 try
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530440 {
Patrick Venture5739ac32018-10-16 19:17:41 -0700441 auto inventoryMgrResponseMsg = bus.call(pimMsg);
442 }
443 catch (const sdbusplus::exception::SdBusError& ex)
444 {
445 log<level::ERR>("Error in notify call", entry("WHAT=%s", ex.what()));
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530446 return -1;
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530447 }
Patrick Venture5739ac32018-10-16 19:17:41 -0700448
Vishwa4be4b7a2015-10-31 22:55:50 -0500449 return rc;
450}
451
vishwac93d6d42015-12-16 11:55:16 -0600452///----------------------------------------------------
453// Checks if a particular fru area is populated or not
454///----------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700455bool remove_invalid_area(const std::unique_ptr<ipmi_fru>& fru_area)
Vishwa4be4b7a2015-10-31 22:55:50 -0500456{
Deepak Kodihalli89ededd2017-02-11 01:59:32 -0600457 // Filter the ones that are empty
Patrick Venturec9508db2018-10-16 17:18:43 -0700458 if (!(fru_area->get_len()))
vishwac93d6d42015-12-16 11:55:16 -0600459 {
460 return true;
461 }
462 return false;
463}
Vishwa4be4b7a2015-10-31 22:55:50 -0500464
vishwac93d6d42015-12-16 11:55:16 -0600465///----------------------------------------------------------------------------------
466// Populates various FRU areas
467// @prereq : This must be called only after validating common header.
468///----------------------------------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700469int ipmi_populate_fru_areas(uint8_t* fru_data, const size_t data_len,
470 fru_area_vec_t& fru_area_vec)
vishwac93d6d42015-12-16 11:55:16 -0600471{
vishwac93d6d42015-12-16 11:55:16 -0600472 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500473
vishwac93d6d42015-12-16 11:55:16 -0600474 // Now walk the common header and see if the file size has atleast the last
475 // offset mentioned by the common_hdr. If the file size is less than the
476 // offset of any if the fru areas mentioned in the common header, then we do
477 // not have a complete file.
Patrick Venturec9508db2018-10-16 17:18:43 -0700478 for (uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
479 fru_entry < (sizeof(struct common_header) - 2); fru_entry++)
vishwac93d6d42015-12-16 11:55:16 -0600480 {
Yi Li75c2d462016-04-11 16:57:46 +0800481 rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600482 // Actual offset in the payload is the offset mentioned in common header
Gunnar Millsc19b8132018-06-14 08:56:17 -0500483 // multiplied by 8. Common header is always the first 8 bytes.
Patrick Venture50ddfe52018-10-16 17:16:32 -0700484 size_t area_offset = fru_data[fru_entry] * IPMI_EIGHT_BYTES;
Patrick Venturec9508db2018-10-16 17:18:43 -0700485 if (area_offset && (data_len < (area_offset + 2)))
vishwac93d6d42015-12-16 11:55:16 -0600486 {
487 // Our file size is less than what it needs to be. +2 because we are
488 // using area len that is at 2 byte off area_offset
Patrick Venture5c787212018-10-17 13:48:23 -0700489 log<level::ERR>("fru file is incomplete",
490 entry("SIZE=%d", data_len));
vishwac93d6d42015-12-16 11:55:16 -0600491 return rc;
492 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700493 else if (area_offset)
vishwac93d6d42015-12-16 11:55:16 -0600494 {
495 // Read 2 bytes to know the actual size of area.
496 uint8_t area_hdr[2] = {0};
Patrick Venture5c787212018-10-17 13:48:23 -0700497 std::memcpy(area_hdr, &((uint8_t*)fru_data)[area_offset],
498 sizeof(area_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500499
vishwac93d6d42015-12-16 11:55:16 -0600500 // Size of this area will be the 2nd byte in the fru area header.
Patrick Venturec9508db2018-10-16 17:18:43 -0700501 size_t area_len = area_hdr[1] * IPMI_EIGHT_BYTES;
vishwac93d6d42015-12-16 11:55:16 -0600502 uint8_t area_data[area_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500503
Patrick Venture5c787212018-10-17 13:48:23 -0700504 log<level::DEBUG>("Fru Data", entry("SIZE=%d", data_len),
505 entry("AREA OFFSET=%d", area_offset),
506 entry("AREA_SIZE=%d", area_len));
Vishwa4be4b7a2015-10-31 22:55:50 -0500507
vishwac93d6d42015-12-16 11:55:16 -0600508 // See if we really have that much buffer. We have area offset amd
509 // from there, the actual len.
Patrick Venturec9508db2018-10-16 17:18:43 -0700510 if (data_len < (area_len + area_offset))
vishwac93d6d42015-12-16 11:55:16 -0600511 {
Patrick Venture5c787212018-10-17 13:48:23 -0700512 log<level::ERR>("Incomplete Fru file",
513 entry("SIZE=%d", data_len));
vishwac93d6d42015-12-16 11:55:16 -0600514 return rc;
515 }
516
517 // Save off the data.
Patrick Venture5c787212018-10-17 13:48:23 -0700518 std::memcpy(area_data, &((uint8_t*)fru_data)[area_offset],
519 area_len);
vishwac93d6d42015-12-16 11:55:16 -0600520
521 // Validate the crc
522 rc = verify_fru_data(area_data, area_len);
Patrick Venturec9508db2018-10-16 17:18:43 -0700523 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600524 {
Patrick Venture5c787212018-10-17 13:48:23 -0700525 log<level::ERR>("Err validating fru area",
526 entry("OFFSET=%d", area_offset));
vishwac93d6d42015-12-16 11:55:16 -0600527 return rc;
528 }
529 else
530 {
Patrick Venture5c787212018-10-17 13:48:23 -0700531 log<level::DEBUG>("Successfully verified area checksum.",
532 entry("OFFSET=%d", area_offset));
vishwac93d6d42015-12-16 11:55:16 -0600533 }
534
535 // We already have a vector that is passed to us containing all
536 // of the fields populated. Update the data portion now.
Patrick Venturec9508db2018-10-16 17:18:43 -0700537 for (auto& iter : fru_area_vec)
vishwac93d6d42015-12-16 11:55:16 -0600538 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700539 if ((iter)->get_type() == get_fru_area_type(fru_entry))
vishwac93d6d42015-12-16 11:55:16 -0600540 {
541 (iter)->set_data(area_data, area_len);
542 }
543 }
544 } // If we have fru data present
Patrick Venturec9508db2018-10-16 17:18:43 -0700545 } // Walk common_hdr
vishwac93d6d42015-12-16 11:55:16 -0600546
547 // Not all the fields will be populated in a fru data. Mostly all cases will
548 // not have more than 2 or 3.
549 fru_area_vec.erase(std::remove_if(fru_area_vec.begin(), fru_area_vec.end(),
Patrick Venturec9508db2018-10-16 17:18:43 -0700550 remove_invalid_area),
551 fru_area_vec.end());
vishwac93d6d42015-12-16 11:55:16 -0600552
553 return EXIT_SUCCESS;
554}
555
556///---------------------------------------------------------
557// Validates the fru data per ipmi common header constructs.
558// Returns with updated common_hdr and also file_size
559//----------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700560int ipmi_validate_common_hdr(const uint8_t* fru_data, const size_t data_len)
vishwac93d6d42015-12-16 11:55:16 -0600561{
562 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500563
564 uint8_t common_hdr[sizeof(struct common_header)] = {0};
Patrick Venturec9508db2018-10-16 17:18:43 -0700565 if (data_len >= sizeof(common_hdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500566 {
Patrick Venture5c787212018-10-17 13:48:23 -0700567 std::memcpy(common_hdr, fru_data, sizeof(common_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500568 }
569 else
570 {
Patrick Venture5c787212018-10-17 13:48:23 -0700571 log<level::ERR>("Incomplete fru data file", entry("SIZE=%d", data_len));
vishwac93d6d42015-12-16 11:55:16 -0600572 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500573 }
574
vishwac93d6d42015-12-16 11:55:16 -0600575 // Verify the crc and size
576 rc = verify_fru_data(common_hdr, sizeof(common_hdr));
Patrick Venturec9508db2018-10-16 17:18:43 -0700577 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500578 {
Patrick Venture5c787212018-10-17 13:48:23 -0700579 log<level::ERR>("Failed to validate common header");
vishwac93d6d42015-12-16 11:55:16 -0600580 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500581 }
582
vishwac93d6d42015-12-16 11:55:16 -0600583 return EXIT_SUCCESS;
584}
Vishwa4be4b7a2015-10-31 22:55:50 -0500585
Vishwa4be4b7a2015-10-31 22:55:50 -0500586///-----------------------------------------------------
587// Accepts the filename and validates per IPMI FRU spec
588//----------------------------------------------------
Patrick Venture98072dc2018-10-20 09:31:35 -0700589int validateFRUArea(const uint8_t fruid, const char* fru_file_name,
590 sd_bus* bus_type, const bool bmc_fru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500591{
vishwac93d6d42015-12-16 11:55:16 -0600592 size_t data_len = 0;
593 size_t bytes_read = 0;
594 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500595
vishwac93d6d42015-12-16 11:55:16 -0600596 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
597 // are not used, keeping it here for completeness.
598 fru_area_vec_t fru_area_vec;
Yi Li75c2d462016-04-11 16:57:46 +0800599
Patrick Venturec9508db2018-10-16 17:18:43 -0700600 for (uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
601 fru_entry < (sizeof(struct common_header) - 2); fru_entry++)
vishwac93d6d42015-12-16 11:55:16 -0600602 {
603 // Create an object and push onto a vector.
Patrick Venturec9508db2018-10-16 17:18:43 -0700604 std::unique_ptr<ipmi_fru> fru_area = std::make_unique<ipmi_fru>(
605 fruid, get_fru_area_type(fru_entry), bus_type, bmc_fru);
vishwac93d6d42015-12-16 11:55:16 -0600606
607 // Physically being present
vishwa2f5a3cf2016-05-30 02:25:21 -0500608 bool present = access(fru_file_name, F_OK) == 0;
vishwac93d6d42015-12-16 11:55:16 -0600609 fru_area->set_present(present);
610
vishwac93d6d42015-12-16 11:55:16 -0600611 fru_area_vec.emplace_back(std::move(fru_area));
612 }
613
Patrick Venture5c787212018-10-17 13:48:23 -0700614 FILE* fru_fp = std::fopen(fru_file_name, "rb");
Patrick Venturec9508db2018-10-16 17:18:43 -0700615 if (fru_fp == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500616 {
Patrick Venture5c787212018-10-17 13:48:23 -0700617 log<level::ERR>("Unable to open fru file",
618 entry("FILE=%s", fru_file_name),
619 entry("ERRNO=%s", std::strerror(errno)));
Patrick Venture234b7352018-10-20 09:37:09 -0700620 return cleanupError(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500621 }
622
vishwac93d6d42015-12-16 11:55:16 -0600623 // Get the size of the file to see if it meets minimum requirement
Patrick Venture5c787212018-10-17 13:48:23 -0700624 if (std::fseek(fru_fp, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500625 {
Patrick Venture5c787212018-10-17 13:48:23 -0700626 log<level::ERR>("Unable to seek fru file",
627 entry("FILE=%s", fru_file_name),
628 entry("ERRNO=%s", std::strerror(errno)));
Patrick Venture234b7352018-10-20 09:37:09 -0700629 return cleanupError(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500630 }
631
vishwac93d6d42015-12-16 11:55:16 -0600632 // Allocate a buffer to hold entire file content
Patrick Venture5c787212018-10-17 13:48:23 -0700633 data_len = std::ftell(fru_fp);
vishwac93d6d42015-12-16 11:55:16 -0600634 uint8_t fru_data[data_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500635
Patrick Venture5c787212018-10-17 13:48:23 -0700636 std::rewind(fru_fp);
637 bytes_read = std::fread(fru_data, data_len, 1, fru_fp);
Patrick Venturec9508db2018-10-16 17:18:43 -0700638 if (bytes_read != 1)
Vishwa4be4b7a2015-10-31 22:55:50 -0500639 {
Patrick Venture5c787212018-10-17 13:48:23 -0700640 log<level::ERR>("Failed reading fru data.",
641 entry("BYTESREAD=%d", bytes_read),
642 entry("ERRNO=%s", std::strerror(errno)));
Patrick Venture234b7352018-10-20 09:37:09 -0700643 return cleanupError(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500644 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500645
vishwac93d6d42015-12-16 11:55:16 -0600646 // We are done reading.
Patrick Venture5c787212018-10-17 13:48:23 -0700647 std::fclose(fru_fp);
vishwac93d6d42015-12-16 11:55:16 -0600648 fru_fp = NULL;
649
650 rc = ipmi_validate_common_hdr(fru_data, data_len);
Patrick Venturec9508db2018-10-16 17:18:43 -0700651 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500652 {
Patrick Venture234b7352018-10-20 09:37:09 -0700653 return cleanupError(fru_fp, fru_area_vec);
vishwac93d6d42015-12-16 11:55:16 -0600654 }
655
Patrick Venturec9508db2018-10-16 17:18:43 -0700656 // Now that we validated the common header, populate various fru sections if
657 // we have them here.
vishwac93d6d42015-12-16 11:55:16 -0600658 rc = ipmi_populate_fru_areas(fru_data, data_len, fru_area_vec);
Patrick Venturec9508db2018-10-16 17:18:43 -0700659 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600660 {
Patrick Venture5c787212018-10-17 13:48:23 -0700661 log<level::ERR>("Populating FRU areas failed", entry("FRU=%d", fruid));
Patrick Venture234b7352018-10-20 09:37:09 -0700662 return cleanupError(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500663 }
664 else
665 {
Patrick Venture5c787212018-10-17 13:48:23 -0700666 log<level::DEBUG>("Populated FRU areas",
667 entry("FILE=%s", fru_file_name));
Vishwa4be4b7a2015-10-31 22:55:50 -0500668 }
669
vishwac93d6d42015-12-16 11:55:16 -0600670#ifdef __IPMI_DEBUG__
Patrick Venturec9508db2018-10-16 17:18:43 -0700671 for (auto& iter : fru_area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500672 {
Patrick Venture5c787212018-10-17 13:48:23 -0700673 std::printf("FRU ID : [%d]\n", (iter)->get_fruid());
674 std::printf("AREA NAME : [%s]\n", (iter)->get_name());
675 std::printf("TYPE : [%d]\n", (iter)->get_type());
676 std::printf("LEN : [%d]\n", (iter)->get_len());
677 std::printf("BUS NAME : [%s]\n", (iter)->get_bus_name());
678 std::printf("OBJ PATH : [%s]\n", (iter)->get_obj_path());
679 std::printf("INTF NAME :[%s]\n", (iter)->get_intf_name());
Vishwa4be4b7a2015-10-31 22:55:50 -0500680 }
vishwac93d6d42015-12-16 11:55:16 -0600681#endif
682
683 // If the vector is populated with everything, then go ahead and update the
684 // inventory.
Patrick Venturec9508db2018-10-16 17:18:43 -0700685 if (!(fru_area_vec.empty()))
vishwac93d6d42015-12-16 11:55:16 -0600686 {
687
688#ifdef __IPMI_DEBUG__
Patrick Venture5c787212018-10-17 13:48:23 -0700689 std::printf("\n SIZE of vector is : [%d] \n", fru_area_vec.size());
vishwac93d6d42015-12-16 11:55:16 -0600690#endif
Patrick Williams21eb0432017-03-31 11:40:48 -0500691 rc = ipmi_update_inventory(fru_area_vec, bus_type);
Patrick Venturec9508db2018-10-16 17:18:43 -0700692 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600693 {
Patrick Venture5c787212018-10-17 13:48:23 -0700694 log<level::ERR>("Error updating inventory.");
vishwac93d6d42015-12-16 11:55:16 -0600695 }
696 }
697
698 // we are done with all that we wanted to do. This will do the job of
699 // calling any destructors too.
700 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500701
702 return rc;
703}