blob: 84d7527f2dc2acd8caeee0e39f6f036bfa2f2868 [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
vishwac93d6d42015-12-16 11:55:16 -060029//----------------------------------------------------------------
30// Constructor
31//----------------------------------------------------------------
32ipmi_fru::ipmi_fru(const uint8_t fruid, const ipmi_fru_area_type type,
Patrick Venturec9508db2018-10-16 17:18:43 -070033 sd_bus* bus_type, bool bmc_fru)
vishwac93d6d42015-12-16 11:55:16 -060034{
35 iv_fruid = fruid;
36 iv_type = type;
37 iv_bmc_fru = bmc_fru;
38 iv_bus_type = bus_type;
39 iv_valid = false;
40 iv_data = NULL;
41 iv_present = false;
42
Patrick Venturec9508db2018-10-16 17:18:43 -070043 if (iv_type == IPMI_FRU_AREA_INTERNAL_USE)
vishwac93d6d42015-12-16 11:55:16 -060044 {
45 iv_name = "INTERNAL_";
46 }
Patrick Venturec9508db2018-10-16 17:18:43 -070047 else if (iv_type == IPMI_FRU_AREA_CHASSIS_INFO)
vishwac93d6d42015-12-16 11:55:16 -060048 {
49 iv_name = "CHASSIS_";
50 }
Patrick Venturec9508db2018-10-16 17:18:43 -070051 else if (iv_type == IPMI_FRU_AREA_BOARD_INFO)
vishwac93d6d42015-12-16 11:55:16 -060052 {
53 iv_name = "BOARD_";
54 }
Patrick Venturec9508db2018-10-16 17:18:43 -070055 else if (iv_type == IPMI_FRU_AREA_PRODUCT_INFO)
vishwac93d6d42015-12-16 11:55:16 -060056 {
57 iv_name = "PRODUCT_";
58 }
Patrick Venturec9508db2018-10-16 17:18:43 -070059 else if (iv_type == IPMI_FRU_AREA_MULTI_RECORD)
vishwac93d6d42015-12-16 11:55:16 -060060 {
61 iv_name = "MULTI_";
62 }
63 else
64 {
65 iv_name = IPMI_FRU_AREA_TYPE_MAX;
Patrick Venture5c787212018-10-17 13:48:23 -070066 log<level::ERR>("Invalid Area", entry("TYPE=%d", iv_type));
vishwac93d6d42015-12-16 11:55:16 -060067 }
68}
69
70//-----------------------------------------------------
71// For a FRU area type, accepts the data and updates
72// area specific data.
73//-----------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -070074void ipmi_fru::set_data(const uint8_t* data, const size_t len)
vishwac93d6d42015-12-16 11:55:16 -060075{
76 iv_len = len;
77 iv_data = new uint8_t[len];
Patrick Venture5c787212018-10-17 13:48:23 -070078 std::memcpy(iv_data, data, len);
vishwac93d6d42015-12-16 11:55:16 -060079}
80
81//-----------------------------------------------------
82// Sets the dbus parameters
83//-----------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -070084void ipmi_fru::update_dbus_paths(const char* bus_name, const char* obj_path,
85 const char* intf_name)
vishwac93d6d42015-12-16 11:55:16 -060086{
87 iv_bus_name = bus_name;
88 iv_obj_path = obj_path;
89 iv_intf_name = intf_name;
90}
91
92//-------------------
93// Destructor
94//-------------------
95ipmi_fru::~ipmi_fru()
96{
Patrick Venturec9508db2018-10-16 17:18:43 -070097 if (iv_data != NULL)
vishwac93d6d42015-12-16 11:55:16 -060098 {
Patrick Venturec9508db2018-10-16 17:18:43 -070099 delete[] iv_data;
vishwac93d6d42015-12-16 11:55:16 -0600100 iv_data = NULL;
101 }
vishwac93d6d42015-12-16 11:55:16 -0600102}
103
Vishwa4be4b7a2015-10-31 22:55:50 -0500104//------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600105// Takes the pointer to stream of bytes and length
vishwac93d6d42015-12-16 11:55:16 -0600106// and returns the 8 bit checksum
107// This algo is per IPMI V2.0 spec
Vishwa4be4b7a2015-10-31 22:55:50 -0500108//-------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700109unsigned char calculate_crc(const unsigned char* data, size_t len)
Vishwa4be4b7a2015-10-31 22:55:50 -0500110{
111 char crc = 0;
vishwac93d6d42015-12-16 11:55:16 -0600112 size_t byte = 0;
Vishwa4be4b7a2015-10-31 22:55:50 -0500113
Patrick Venturec9508db2018-10-16 17:18:43 -0700114 for (byte = 0; byte < len; byte++)
Vishwa4be4b7a2015-10-31 22:55:50 -0500115 {
116 crc += *data++;
117 }
vishwaf3ca3522015-12-02 10:35:13 -0600118
Patrick Venturec9508db2018-10-16 17:18:43 -0700119 return (-crc);
Vishwa4be4b7a2015-10-31 22:55:50 -0500120}
121
122//---------------------------------------------------------------------
123// Accepts a fru area offset in commom hdr and tells which area it is.
124//---------------------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600125ipmi_fru_area_type get_fru_area_type(uint8_t area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500126{
127 ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
128
Patrick Venturec9508db2018-10-16 17:18:43 -0700129 switch (area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500130 {
131 case IPMI_FRU_INTERNAL_OFFSET:
132 type = IPMI_FRU_AREA_INTERNAL_USE;
133 break;
134
135 case IPMI_FRU_CHASSIS_OFFSET:
136 type = IPMI_FRU_AREA_CHASSIS_INFO;
137 break;
138
139 case IPMI_FRU_BOARD_OFFSET:
140 type = IPMI_FRU_AREA_BOARD_INFO;
141 break;
142
143 case IPMI_FRU_PRODUCT_OFFSET:
144 type = IPMI_FRU_AREA_PRODUCT_INFO;
145 break;
146
147 case IPMI_FRU_MULTI_OFFSET:
148 type = IPMI_FRU_AREA_MULTI_RECORD;
149 break;
150
151 default:
152 type = IPMI_FRU_AREA_TYPE_MAX;
153 }
154
155 return type;
156}
157
vishwac93d6d42015-12-16 11:55:16 -0600158///-----------------------------------------------
159// Validates the data for crc and mandatory fields
160///-----------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700161int verify_fru_data(const uint8_t* data, const size_t len)
vishwac93d6d42015-12-16 11:55:16 -0600162{
163 uint8_t checksum = 0;
164 int rc = -1;
165
166 // Validate for first byte to always have a value of [1]
Patrick Venturec9508db2018-10-16 17:18:43 -0700167 if (data[0] != IPMI_FRU_HDR_BYTE_ZERO)
vishwac93d6d42015-12-16 11:55:16 -0600168 {
Patrick Venture5c787212018-10-17 13:48:23 -0700169 log<level::ERR>("Invalid entry in byte-0",
170 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600171 return rc;
172 }
173#ifdef __IPMI_DEBUG__
174 else
175 {
Patrick Venture5c787212018-10-17 13:48:23 -0700176 log<level::DEBUG>("Validated in entry_1 of fru_data",
177 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600178 }
179#endif
180
181 // See if the calculated CRC matches with the embedded one.
182 // CRC to be calculated on all except the last one that is CRC itself.
183 checksum = calculate_crc(data, len - 1);
Patrick Venturec9508db2018-10-16 17:18:43 -0700184 if (checksum != data[len - 1])
vishwac93d6d42015-12-16 11:55:16 -0600185 {
186#ifdef __IPMI_DEBUG__
Patrick Venture5c787212018-10-17 13:48:23 -0700187 log<level::ERR>(
188 "Checksum mismatch",
189 entry("Calculated=0x%X", static_cast<uint32_t>(checksum)),
190 entry("Embedded=0x%X", static_cast<uint32_t>(data[len])));
vishwac93d6d42015-12-16 11:55:16 -0600191#endif
192 return rc;
193 }
194#ifdef __IPMI_DEBUG__
195 else
196 {
Patrick Venture5c787212018-10-17 13:48:23 -0700197 log<level::DEBUG>("Checksum matches");
vishwac93d6d42015-12-16 11:55:16 -0600198 }
199#endif
200
201 return EXIT_SUCCESS;
202}
203
Vishwa4be4b7a2015-10-31 22:55:50 -0500204//------------------------------------------------------------------------
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530205// Gets the value of the key from the fru dictionary of the given section.
206// FRU dictionary is parsed fru data for all the sections.
207//------------------------------------------------------------------------
208
Patrick Venturec9508db2018-10-16 17:18:43 -0700209std::string getFRUValue(const std::string& section, const std::string& key,
210 const std::string& delimiter, IPMIFruInfo& fruData)
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530211{
212
213 auto minIndexValue = 0;
214 auto maxIndexValue = 0;
215 std::string fruValue = "";
Patrick Venture5c787212018-10-17 13:48:23 -0700216
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530217 if (section == "Board")
218 {
219 minIndexValue = OPENBMC_VPD_KEY_BOARD_MFG_DATE;
220 maxIndexValue = OPENBMC_VPD_KEY_BOARD_MAX;
221 }
222 else if (section == "Product")
223 {
224 minIndexValue = OPENBMC_VPD_KEY_PRODUCT_MFR;
225 maxIndexValue = OPENBMC_VPD_KEY_PRODUCT_MAX;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530226 }
227 else if (section == "Chassis")
228 {
229 minIndexValue = OPENBMC_VPD_KEY_CHASSIS_TYPE;
230 maxIndexValue = OPENBMC_VPD_KEY_CHASSIS_MAX;
231 }
232
233 auto first = fruData.cbegin() + minIndexValue;
234 auto last = first + (maxIndexValue - minIndexValue) + 1;
235
Patrick Venturec9508db2018-10-16 17:18:43 -0700236 auto itr =
237 std::find_if(first, last, [&key](auto& e) { return key == e.first; });
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530238
239 if (itr != last)
240 {
241 fruValue = itr->second;
242 }
Ratan Guptacdde23d2017-02-15 09:29:54 +0530243
Patrick Venturec9508db2018-10-16 17:18:43 -0700244 // if the key is custom property then the value could be in two formats.
245 // 1) custom field 2 = "value".
246 // 2) custom field 2 = "key:value".
247 // if delimiter length = 0 i.e custom field 2 = "value"
Ratan Guptacdde23d2017-02-15 09:29:54 +0530248
249 constexpr auto customProp = "Custom Field";
250 if (key.find(customProp) != std::string::npos)
251 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700252 if (delimiter.length() > 0)
253 {
254 size_t delimiterpos = fruValue.find(delimiter);
255 if (delimiterpos != std::string::npos)
256 fruValue = fruValue.substr(delimiterpos + 1);
257 }
Ratan Guptacdde23d2017-02-15 09:29:54 +0530258 }
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530259 return fruValue;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530260}
Patrick Venturec9508db2018-10-16 17:18:43 -0700261// Get the inventory service from the mapper.
262auto getService(sdbusplus::bus::bus& bus, const std::string& intf,
263 const std::string& path)
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530264{
Patrick Venturec9508db2018-10-16 17:18:43 -0700265 auto mapperCall =
266 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
267 "/xyz/openbmc_project/object_mapper",
268 "xyz.openbmc_project.ObjectMapper", "GetObject");
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530269
270 mapperCall.append(path);
271 mapperCall.append(std::vector<std::string>({intf}));
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530272 std::map<std::string, std::vector<std::string>> mapperResponse;
Patrick Venture5739ac32018-10-16 19:17:41 -0700273
274 try
275 {
276 auto mapperResponseMsg = bus.call(mapperCall);
277 mapperResponseMsg.read(mapperResponse);
278 }
279 catch (const sdbusplus::exception::SdBusError& ex)
280 {
281 log<level::ERR>("Exception from sdbus call",
282 entry("WHAT=%s", ex.what()));
283 throw;
284 }
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530285
286 if (mapperResponse.begin() == mapperResponse.end())
287 {
288 throw std::runtime_error("ERROR in reading the mapper response");
289 }
290
291 return mapperResponse.begin()->first;
292}
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530293
294//------------------------------------------------------------------------
Vishwa4be4b7a2015-10-31 22:55:50 -0500295// Takes FRU data, invokes Parser for each fru record area and updates
296// Inventory
297//------------------------------------------------------------------------
Patrick Williams21eb0432017-03-31 11:40:48 -0500298int ipmi_update_inventory(fru_area_vec_t& area_vec, sd_bus* bus_sd)
Vishwa4be4b7a2015-10-31 22:55:50 -0500299{
vishwac93d6d42015-12-16 11:55:16 -0600300 // Generic error reporter
Vishwa4be4b7a2015-10-31 22:55:50 -0500301 int rc = 0;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530302 uint8_t fruid = 0;
303 IPMIFruInfo fruData;
Vishwa4be4b7a2015-10-31 22:55:50 -0500304
Vishwa4be4b7a2015-10-31 22:55:50 -0500305 // For each FRU area, extract the needed data , get it parsed and update
306 // the Inventory.
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530307 for (const auto& fruArea : area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500308 {
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530309 fruid = fruArea->get_fruid();
Vishwa4be4b7a2015-10-31 22:55:50 -0500310 // Fill the container with information
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530311 rc = parse_fru_area((fruArea)->get_type(), (void*)(fruArea)->get_data(),
312 (fruArea)->get_len(), fruData);
313 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500314 {
Patrick Venture5c787212018-10-17 13:48:23 -0700315 log<level::ERR>("Error parsing FRU records");
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530316 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500317 }
318 } // END walking the vector of areas and updating
319
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530320 // For each Fru we have the list of instances which needs to be updated.
321 // Each instance object implements certain interfaces.
322 // Each Interface is having Dbus properties.
323 // Each Dbus Property would be having metaData(eg section,VpdPropertyName).
324
325 // Here we are just printing the object,interface and the properties.
326 // which needs to be called with the new inventory manager implementation.
Patrick Williams49a6fcd2017-04-18 11:49:07 -0500327 sdbusplus::bus::bus bus{bus_sd};
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530328 using namespace std::string_literals;
329 static const auto intf = "xyz.openbmc_project.Inventory.Manager"s;
Brad Bishopec7648d2017-02-23 11:30:16 -0500330 static const auto path = "/xyz/openbmc_project/inventory"s;
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530331 std::string service;
332 try
333 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700334 service = getService(bus, intf, path);
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530335 }
Patrick Venture5739ac32018-10-16 19:17:41 -0700336 catch (const std::exception& e)
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530337 {
338 std::cerr << e.what() << "\n";
339 return -1;
340 }
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530341
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530342 auto iter = frus.find(fruid);
343 if (iter == frus.end())
344 {
Patrick Venture5c787212018-10-17 13:48:23 -0700345 log<level::ERR>("Unable to get the fru info",
346 entry("FRU=%d", static_cast<int>(fruid)));
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530347 return -1;
348 }
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530349
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530350 auto& instanceList = iter->second;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530351 if (instanceList.size() <= 0)
352 {
Patrick Venture5c787212018-10-17 13:48:23 -0700353 log<level::DEBUG>("Object list empty for this FRU",
354 entry("FRU=%d", static_cast<int>(fruid)));
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530355 }
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530356
357 ObjectMap objects;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530358 for (auto& instance : instanceList)
359 {
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530360 InterfaceMap interfaces;
Ratan Guptac19c0542018-02-04 23:24:44 +0530361 const auto& extrasIter = extras.find(instance.path);
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530362
Ratan Guptac19c0542018-02-04 23:24:44 +0530363 for (auto& interfaceList : instance.interfaces)
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530364 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700365 PropertyMap props; // store all the properties
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530366 for (auto& properties : interfaceList.second)
367 {
Patrick Williamsaeb726d2017-06-01 19:00:43 -0500368 std::string value;
369 decltype(auto) pdata = properties.second;
Ratan Guptacdde23d2017-02-15 09:29:54 +0530370
Patrick Williamsaeb726d2017-06-01 19:00:43 -0500371 if (!pdata.section.empty() && !pdata.property.empty())
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530372 {
Patrick Williamsaeb726d2017-06-01 19:00:43 -0500373 value = getFRUValue(pdata.section, pdata.property,
374 pdata.delimiter, fruData);
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530375 }
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530376 props.emplace(std::move(properties.first), std::move(value));
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530377 }
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600378 // Check and update extra properties
Patrick Venturec9508db2018-10-16 17:18:43 -0700379 if (extras.end() != extrasIter)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600380 {
381 const auto& propsIter =
382 (extrasIter->second).find(interfaceList.first);
Patrick Venturec9508db2018-10-16 17:18:43 -0700383 if ((extrasIter->second).end() != propsIter)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600384 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700385 for (const auto& map : propsIter->second)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600386 {
387 props.emplace(map.first, map.second);
388 }
389 }
390 }
391 interfaces.emplace(std::move(interfaceList.first),
392 std::move(props));
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530393 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500394
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600395 // Call the inventory manager
Ratan Guptac19c0542018-02-04 23:24:44 +0530396 sdbusplus::message::object_path path = instance.path;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600397 // Check and update extra properties
Patrick Venturec9508db2018-10-16 17:18:43 -0700398 if (extras.end() != extrasIter)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600399 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700400 for (const auto& entry : extrasIter->second)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600401 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700402 if (interfaces.end() == interfaces.find(entry.first))
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600403 {
404 interfaces.emplace(entry.first, entry.second);
405 }
406 }
407 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700408 objects.emplace(path, interfaces);
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530409 }
410
Patrick Venturec9508db2018-10-16 17:18:43 -0700411 auto pimMsg = bus.new_method_call(service.c_str(), path.c_str(),
412 intf.c_str(), "Notify");
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530413 pimMsg.append(std::move(objects));
Patrick Venture5739ac32018-10-16 19:17:41 -0700414
415 try
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530416 {
Patrick Venture5739ac32018-10-16 19:17:41 -0700417 auto inventoryMgrResponseMsg = bus.call(pimMsg);
418 }
419 catch (const sdbusplus::exception::SdBusError& ex)
420 {
421 log<level::ERR>("Error in notify call", entry("WHAT=%s", ex.what()));
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530422 return -1;
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530423 }
Patrick Venture5739ac32018-10-16 19:17:41 -0700424
Vishwa4be4b7a2015-10-31 22:55:50 -0500425 return rc;
426}
427
vishwac93d6d42015-12-16 11:55:16 -0600428///----------------------------------------------------
429// Checks if a particular fru area is populated or not
430///----------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700431bool remove_invalid_area(const std::unique_ptr<ipmi_fru>& fru_area)
Vishwa4be4b7a2015-10-31 22:55:50 -0500432{
Deepak Kodihalli89ededd2017-02-11 01:59:32 -0600433 // Filter the ones that are empty
Patrick Venturec9508db2018-10-16 17:18:43 -0700434 if (!(fru_area->get_len()))
vishwac93d6d42015-12-16 11:55:16 -0600435 {
436 return true;
437 }
438 return false;
439}
Vishwa4be4b7a2015-10-31 22:55:50 -0500440
vishwac93d6d42015-12-16 11:55:16 -0600441///----------------------------------------------------------------------------------
442// Populates various FRU areas
443// @prereq : This must be called only after validating common header.
444///----------------------------------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700445int ipmi_populate_fru_areas(uint8_t* fru_data, const size_t data_len,
446 fru_area_vec_t& fru_area_vec)
vishwac93d6d42015-12-16 11:55:16 -0600447{
vishwac93d6d42015-12-16 11:55:16 -0600448 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500449
vishwac93d6d42015-12-16 11:55:16 -0600450 // Now walk the common header and see if the file size has atleast the last
451 // offset mentioned by the common_hdr. If the file size is less than the
452 // offset of any if the fru areas mentioned in the common header, then we do
453 // not have a complete file.
Patrick Venturec9508db2018-10-16 17:18:43 -0700454 for (uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
455 fru_entry < (sizeof(struct common_header) - 2); fru_entry++)
vishwac93d6d42015-12-16 11:55:16 -0600456 {
Yi Li75c2d462016-04-11 16:57:46 +0800457 rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600458 // Actual offset in the payload is the offset mentioned in common header
Gunnar Millsc19b8132018-06-14 08:56:17 -0500459 // multiplied by 8. Common header is always the first 8 bytes.
Patrick Venture50ddfe52018-10-16 17:16:32 -0700460 size_t area_offset = fru_data[fru_entry] * IPMI_EIGHT_BYTES;
Patrick Venturec9508db2018-10-16 17:18:43 -0700461 if (area_offset && (data_len < (area_offset + 2)))
vishwac93d6d42015-12-16 11:55:16 -0600462 {
463 // Our file size is less than what it needs to be. +2 because we are
464 // using area len that is at 2 byte off area_offset
Patrick Venture5c787212018-10-17 13:48:23 -0700465 log<level::ERR>("fru file is incomplete",
466 entry("SIZE=%d", data_len));
vishwac93d6d42015-12-16 11:55:16 -0600467 return rc;
468 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700469 else if (area_offset)
vishwac93d6d42015-12-16 11:55:16 -0600470 {
471 // Read 2 bytes to know the actual size of area.
472 uint8_t area_hdr[2] = {0};
Patrick Venture5c787212018-10-17 13:48:23 -0700473 std::memcpy(area_hdr, &((uint8_t*)fru_data)[area_offset],
474 sizeof(area_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500475
vishwac93d6d42015-12-16 11:55:16 -0600476 // Size of this area will be the 2nd byte in the fru area header.
Patrick Venturec9508db2018-10-16 17:18:43 -0700477 size_t area_len = area_hdr[1] * IPMI_EIGHT_BYTES;
vishwac93d6d42015-12-16 11:55:16 -0600478 uint8_t area_data[area_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500479
Patrick Venture5c787212018-10-17 13:48:23 -0700480 log<level::DEBUG>("Fru Data", entry("SIZE=%d", data_len),
481 entry("AREA OFFSET=%d", area_offset),
482 entry("AREA_SIZE=%d", area_len));
Vishwa4be4b7a2015-10-31 22:55:50 -0500483
vishwac93d6d42015-12-16 11:55:16 -0600484 // See if we really have that much buffer. We have area offset amd
485 // from there, the actual len.
Patrick Venturec9508db2018-10-16 17:18:43 -0700486 if (data_len < (area_len + area_offset))
vishwac93d6d42015-12-16 11:55:16 -0600487 {
Patrick Venture5c787212018-10-17 13:48:23 -0700488 log<level::ERR>("Incomplete Fru file",
489 entry("SIZE=%d", data_len));
vishwac93d6d42015-12-16 11:55:16 -0600490 return rc;
491 }
492
493 // Save off the data.
Patrick Venture5c787212018-10-17 13:48:23 -0700494 std::memcpy(area_data, &((uint8_t*)fru_data)[area_offset],
495 area_len);
vishwac93d6d42015-12-16 11:55:16 -0600496
497 // Validate the crc
498 rc = verify_fru_data(area_data, area_len);
Patrick Venturec9508db2018-10-16 17:18:43 -0700499 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600500 {
Patrick Venture5c787212018-10-17 13:48:23 -0700501 log<level::ERR>("Err validating fru area",
502 entry("OFFSET=%d", area_offset));
vishwac93d6d42015-12-16 11:55:16 -0600503 return rc;
504 }
505 else
506 {
Patrick Venture5c787212018-10-17 13:48:23 -0700507 log<level::DEBUG>("Successfully verified area checksum.",
508 entry("OFFSET=%d", area_offset));
vishwac93d6d42015-12-16 11:55:16 -0600509 }
510
511 // We already have a vector that is passed to us containing all
512 // of the fields populated. Update the data portion now.
Patrick Venturec9508db2018-10-16 17:18:43 -0700513 for (auto& iter : fru_area_vec)
vishwac93d6d42015-12-16 11:55:16 -0600514 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700515 if ((iter)->get_type() == get_fru_area_type(fru_entry))
vishwac93d6d42015-12-16 11:55:16 -0600516 {
517 (iter)->set_data(area_data, area_len);
518 }
519 }
520 } // If we have fru data present
Patrick Venturec9508db2018-10-16 17:18:43 -0700521 } // Walk common_hdr
vishwac93d6d42015-12-16 11:55:16 -0600522
523 // Not all the fields will be populated in a fru data. Mostly all cases will
524 // not have more than 2 or 3.
525 fru_area_vec.erase(std::remove_if(fru_area_vec.begin(), fru_area_vec.end(),
Patrick Venturec9508db2018-10-16 17:18:43 -0700526 remove_invalid_area),
527 fru_area_vec.end());
vishwac93d6d42015-12-16 11:55:16 -0600528
529 return EXIT_SUCCESS;
530}
531
532///---------------------------------------------------------
533// Validates the fru data per ipmi common header constructs.
534// Returns with updated common_hdr and also file_size
535//----------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700536int ipmi_validate_common_hdr(const uint8_t* fru_data, const size_t data_len)
vishwac93d6d42015-12-16 11:55:16 -0600537{
538 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500539
540 uint8_t common_hdr[sizeof(struct common_header)] = {0};
Patrick Venturec9508db2018-10-16 17:18:43 -0700541 if (data_len >= sizeof(common_hdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500542 {
Patrick Venture5c787212018-10-17 13:48:23 -0700543 std::memcpy(common_hdr, fru_data, sizeof(common_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500544 }
545 else
546 {
Patrick Venture5c787212018-10-17 13:48:23 -0700547 log<level::ERR>("Incomplete fru data file", entry("SIZE=%d", data_len));
vishwac93d6d42015-12-16 11:55:16 -0600548 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500549 }
550
vishwac93d6d42015-12-16 11:55:16 -0600551 // Verify the crc and size
552 rc = verify_fru_data(common_hdr, sizeof(common_hdr));
Patrick Venturec9508db2018-10-16 17:18:43 -0700553 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500554 {
Patrick Venture5c787212018-10-17 13:48:23 -0700555 log<level::ERR>("Failed to validate common header");
vishwac93d6d42015-12-16 11:55:16 -0600556 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500557 }
558
vishwac93d6d42015-12-16 11:55:16 -0600559 return EXIT_SUCCESS;
560}
Vishwa4be4b7a2015-10-31 22:55:50 -0500561
vishwac93d6d42015-12-16 11:55:16 -0600562//------------------------------------------------------------
563// Cleanup routine
Patrick Venture1012b4b2018-10-18 14:56:11 -0700564// Must always be called as last reference to fru_fp.
vishwac93d6d42015-12-16 11:55:16 -0600565//------------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700566int cleanup_error(FILE* fru_fp, fru_area_vec_t& fru_area_vec)
vishwac93d6d42015-12-16 11:55:16 -0600567{
Patrick Venturec9508db2018-10-16 17:18:43 -0700568 if (fru_fp != NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500569 {
Patrick Venture5c787212018-10-17 13:48:23 -0700570 std::fclose(fru_fp);
vishwac93d6d42015-12-16 11:55:16 -0600571 }
vishwaf3ca3522015-12-02 10:35:13 -0600572
Patrick Venturec9508db2018-10-16 17:18:43 -0700573 if (!(fru_area_vec.empty()))
Vishwa4be4b7a2015-10-31 22:55:50 -0500574 {
vishwac93d6d42015-12-16 11:55:16 -0600575 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500576 }
vishwaf3ca3522015-12-02 10:35:13 -0600577
Patrick Venturec9508db2018-10-16 17:18:43 -0700578 return -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500579}
580
581///-----------------------------------------------------
582// Accepts the filename and validates per IPMI FRU spec
583//----------------------------------------------------
Patrick Venture98072dc2018-10-20 09:31:35 -0700584int validateFRUArea(const uint8_t fruid, const char* fru_file_name,
585 sd_bus* bus_type, const bool bmc_fru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500586{
vishwac93d6d42015-12-16 11:55:16 -0600587 size_t data_len = 0;
588 size_t bytes_read = 0;
589 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500590
vishwac93d6d42015-12-16 11:55:16 -0600591 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
592 // are not used, keeping it here for completeness.
593 fru_area_vec_t fru_area_vec;
Yi Li75c2d462016-04-11 16:57:46 +0800594
Patrick Venturec9508db2018-10-16 17:18:43 -0700595 for (uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
596 fru_entry < (sizeof(struct common_header) - 2); fru_entry++)
vishwac93d6d42015-12-16 11:55:16 -0600597 {
598 // Create an object and push onto a vector.
Patrick Venturec9508db2018-10-16 17:18:43 -0700599 std::unique_ptr<ipmi_fru> fru_area = std::make_unique<ipmi_fru>(
600 fruid, get_fru_area_type(fru_entry), bus_type, bmc_fru);
vishwac93d6d42015-12-16 11:55:16 -0600601
602 // Physically being present
vishwa2f5a3cf2016-05-30 02:25:21 -0500603 bool present = access(fru_file_name, F_OK) == 0;
vishwac93d6d42015-12-16 11:55:16 -0600604 fru_area->set_present(present);
605
vishwac93d6d42015-12-16 11:55:16 -0600606 fru_area_vec.emplace_back(std::move(fru_area));
607 }
608
Patrick Venture5c787212018-10-17 13:48:23 -0700609 FILE* fru_fp = std::fopen(fru_file_name, "rb");
Patrick Venturec9508db2018-10-16 17:18:43 -0700610 if (fru_fp == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500611 {
Patrick Venture5c787212018-10-17 13:48:23 -0700612 log<level::ERR>("Unable to open fru file",
613 entry("FILE=%s", fru_file_name),
614 entry("ERRNO=%s", std::strerror(errno)));
vishwac93d6d42015-12-16 11:55:16 -0600615 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500616 }
617
vishwac93d6d42015-12-16 11:55:16 -0600618 // Get the size of the file to see if it meets minimum requirement
Patrick Venture5c787212018-10-17 13:48:23 -0700619 if (std::fseek(fru_fp, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500620 {
Patrick Venture5c787212018-10-17 13:48:23 -0700621 log<level::ERR>("Unable to seek fru file",
622 entry("FILE=%s", fru_file_name),
623 entry("ERRNO=%s", std::strerror(errno)));
vishwac93d6d42015-12-16 11:55:16 -0600624 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500625 }
626
vishwac93d6d42015-12-16 11:55:16 -0600627 // Allocate a buffer to hold entire file content
Patrick Venture5c787212018-10-17 13:48:23 -0700628 data_len = std::ftell(fru_fp);
vishwac93d6d42015-12-16 11:55:16 -0600629 uint8_t fru_data[data_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500630
Patrick Venture5c787212018-10-17 13:48:23 -0700631 std::rewind(fru_fp);
632 bytes_read = std::fread(fru_data, data_len, 1, fru_fp);
Patrick Venturec9508db2018-10-16 17:18:43 -0700633 if (bytes_read != 1)
Vishwa4be4b7a2015-10-31 22:55:50 -0500634 {
Patrick Venture5c787212018-10-17 13:48:23 -0700635 log<level::ERR>("Failed reading fru data.",
636 entry("BYTESREAD=%d", bytes_read),
637 entry("ERRNO=%s", std::strerror(errno)));
vishwac93d6d42015-12-16 11:55:16 -0600638 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500639 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500640
vishwac93d6d42015-12-16 11:55:16 -0600641 // We are done reading.
Patrick Venture5c787212018-10-17 13:48:23 -0700642 std::fclose(fru_fp);
vishwac93d6d42015-12-16 11:55:16 -0600643 fru_fp = NULL;
644
645 rc = ipmi_validate_common_hdr(fru_data, data_len);
Patrick Venturec9508db2018-10-16 17:18:43 -0700646 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500647 {
vishwac93d6d42015-12-16 11:55:16 -0600648 return cleanup_error(fru_fp, fru_area_vec);
649 }
650
Patrick Venturec9508db2018-10-16 17:18:43 -0700651 // Now that we validated the common header, populate various fru sections if
652 // we have them here.
vishwac93d6d42015-12-16 11:55:16 -0600653 rc = ipmi_populate_fru_areas(fru_data, data_len, fru_area_vec);
Patrick Venturec9508db2018-10-16 17:18:43 -0700654 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600655 {
Patrick Venture5c787212018-10-17 13:48:23 -0700656 log<level::ERR>("Populating FRU areas failed", entry("FRU=%d", fruid));
vishwac93d6d42015-12-16 11:55:16 -0600657 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500658 }
659 else
660 {
Patrick Venture5c787212018-10-17 13:48:23 -0700661 log<level::DEBUG>("Populated FRU areas",
662 entry("FILE=%s", fru_file_name));
Vishwa4be4b7a2015-10-31 22:55:50 -0500663 }
664
vishwac93d6d42015-12-16 11:55:16 -0600665#ifdef __IPMI_DEBUG__
Patrick Venturec9508db2018-10-16 17:18:43 -0700666 for (auto& iter : fru_area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500667 {
Patrick Venture5c787212018-10-17 13:48:23 -0700668 std::printf("FRU ID : [%d]\n", (iter)->get_fruid());
669 std::printf("AREA NAME : [%s]\n", (iter)->get_name());
670 std::printf("TYPE : [%d]\n", (iter)->get_type());
671 std::printf("LEN : [%d]\n", (iter)->get_len());
672 std::printf("BUS NAME : [%s]\n", (iter)->get_bus_name());
673 std::printf("OBJ PATH : [%s]\n", (iter)->get_obj_path());
674 std::printf("INTF NAME :[%s]\n", (iter)->get_intf_name());
Vishwa4be4b7a2015-10-31 22:55:50 -0500675 }
vishwac93d6d42015-12-16 11:55:16 -0600676#endif
677
678 // If the vector is populated with everything, then go ahead and update the
679 // inventory.
Patrick Venturec9508db2018-10-16 17:18:43 -0700680 if (!(fru_area_vec.empty()))
vishwac93d6d42015-12-16 11:55:16 -0600681 {
682
683#ifdef __IPMI_DEBUG__
Patrick Venture5c787212018-10-17 13:48:23 -0700684 std::printf("\n SIZE of vector is : [%d] \n", fru_area_vec.size());
vishwac93d6d42015-12-16 11:55:16 -0600685#endif
Patrick Williams21eb0432017-03-31 11:40:48 -0500686 rc = ipmi_update_inventory(fru_area_vec, bus_type);
Patrick Venturec9508db2018-10-16 17:18:43 -0700687 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600688 {
Patrick Venture5c787212018-10-17 13:48:23 -0700689 log<level::ERR>("Error updating inventory.");
vishwac93d6d42015-12-16 11:55:16 -0600690 }
691 }
692
693 // we are done with all that we wanted to do. This will do the job of
694 // calling any destructors too.
695 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500696
697 return rc;
698}