blob: a831b7fbad2753f03205bfdf4dd3a87ca268cc86 [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 Venture17baa272018-10-20 09:48:08 -0700108// Get the inventory service from the mapper.
109auto getService(sdbusplus::bus::bus& bus, const std::string& intf,
110 const std::string& path)
111{
112 auto mapperCall =
113 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
114 "/xyz/openbmc_project/object_mapper",
115 "xyz.openbmc_project.ObjectMapper", "GetObject");
116
117 mapperCall.append(path);
118 mapperCall.append(std::vector<std::string>({intf}));
119 std::map<std::string, std::vector<std::string>> mapperResponse;
120
121 try
122 {
123 auto mapperResponseMsg = bus.call(mapperCall);
124 mapperResponseMsg.read(mapperResponse);
125 }
126 catch (const sdbusplus::exception::SdBusError& ex)
127 {
128 log<level::ERR>("Exception from sdbus call",
129 entry("WHAT=%s", ex.what()));
130 throw;
131 }
132
133 if (mapperResponse.begin() == mapperResponse.end())
134 {
135 throw std::runtime_error("ERROR in reading the mapper response");
136 }
137
138 return mapperResponse.begin()->first;
139}
140
Patrick Venture44a957d2018-10-20 09:41:29 -0700141// Takes FRU data, invokes Parser for each fru record area and updates
142// Inventory
143//------------------------------------------------------------------------
144int updateInventory(fru_area_vec_t& area_vec, sd_bus* bus_sd)
145{
146 // Generic error reporter
147 int rc = 0;
148 uint8_t fruid = 0;
149 IPMIFruInfo fruData;
150
151 // For each FRU area, extract the needed data , get it parsed and update
152 // the Inventory.
153 for (const auto& fruArea : area_vec)
154 {
155 fruid = fruArea->get_fruid();
156 // Fill the container with information
157 rc = parse_fru_area((fruArea)->get_type(), (void*)(fruArea)->get_data(),
158 (fruArea)->get_len(), fruData);
159 if (rc < 0)
160 {
161 log<level::ERR>("Error parsing FRU records");
162 return rc;
163 }
164 } // END walking the vector of areas and updating
165
166 // For each Fru we have the list of instances which needs to be updated.
167 // Each instance object implements certain interfaces.
168 // Each Interface is having Dbus properties.
169 // Each Dbus Property would be having metaData(eg section,VpdPropertyName).
170
171 // Here we are just printing the object,interface and the properties.
172 // which needs to be called with the new inventory manager implementation.
173 sdbusplus::bus::bus bus{bus_sd};
174 using namespace std::string_literals;
175 static const auto intf = "xyz.openbmc_project.Inventory.Manager"s;
176 static const auto path = "/xyz/openbmc_project/inventory"s;
177 std::string service;
178 try
179 {
180 service = getService(bus, intf, path);
181 }
182 catch (const std::exception& e)
183 {
184 std::cerr << e.what() << "\n";
185 return -1;
186 }
187
188 auto iter = frus.find(fruid);
189 if (iter == frus.end())
190 {
191 log<level::ERR>("Unable to get the fru info",
192 entry("FRU=%d", static_cast<int>(fruid)));
193 return -1;
194 }
195
196 auto& instanceList = iter->second;
197 if (instanceList.size() <= 0)
198 {
199 log<level::DEBUG>("Object list empty for this FRU",
200 entry("FRU=%d", static_cast<int>(fruid)));
201 }
202
203 ObjectMap objects;
204 for (auto& instance : instanceList)
205 {
206 InterfaceMap interfaces;
207 const auto& extrasIter = extras.find(instance.path);
208
209 for (auto& interfaceList : instance.interfaces)
210 {
211 PropertyMap props; // store all the properties
212 for (auto& properties : interfaceList.second)
213 {
214 std::string value;
215 decltype(auto) pdata = properties.second;
216
217 if (!pdata.section.empty() && !pdata.property.empty())
218 {
219 value = getFRUValue(pdata.section, pdata.property,
220 pdata.delimiter, fruData);
221 }
222 props.emplace(std::move(properties.first), std::move(value));
223 }
224 // Check and update extra properties
225 if (extras.end() != extrasIter)
226 {
227 const auto& propsIter =
228 (extrasIter->second).find(interfaceList.first);
229 if ((extrasIter->second).end() != propsIter)
230 {
231 for (const auto& map : propsIter->second)
232 {
233 props.emplace(map.first, map.second);
234 }
235 }
236 }
237 interfaces.emplace(std::move(interfaceList.first),
238 std::move(props));
239 }
240
241 // Call the inventory manager
Patrick Venture9a528f22018-10-20 13:31:28 -0700242 sdbusplus::message::object_path objectPath = instance.path;
Patrick Venture44a957d2018-10-20 09:41:29 -0700243 // Check and update extra properties
244 if (extras.end() != extrasIter)
245 {
246 for (const auto& entry : extrasIter->second)
247 {
248 if (interfaces.end() == interfaces.find(entry.first))
249 {
250 interfaces.emplace(entry.first, entry.second);
251 }
252 }
253 }
Patrick Venture9a528f22018-10-20 13:31:28 -0700254 objects.emplace(objectPath, interfaces);
Patrick Venture44a957d2018-10-20 09:41:29 -0700255 }
256
257 auto pimMsg = bus.new_method_call(service.c_str(), path.c_str(),
258 intf.c_str(), "Notify");
259 pimMsg.append(std::move(objects));
260
261 try
262 {
263 auto inventoryMgrResponseMsg = bus.call(pimMsg);
264 }
265 catch (const sdbusplus::exception::SdBusError& ex)
266 {
267 log<level::ERR>("Error in notify call", entry("WHAT=%s", ex.what()));
268 return -1;
269 }
270
271 return rc;
272}
273
Patrick Venture234b7352018-10-20 09:37:09 -0700274} // namespace
275
vishwac93d6d42015-12-16 11:55:16 -0600276//----------------------------------------------------------------
277// Constructor
278//----------------------------------------------------------------
279ipmi_fru::ipmi_fru(const uint8_t fruid, const ipmi_fru_area_type type,
Patrick Venturec9508db2018-10-16 17:18:43 -0700280 sd_bus* bus_type, bool bmc_fru)
vishwac93d6d42015-12-16 11:55:16 -0600281{
282 iv_fruid = fruid;
283 iv_type = type;
284 iv_bmc_fru = bmc_fru;
285 iv_bus_type = bus_type;
286 iv_valid = false;
287 iv_data = NULL;
288 iv_present = false;
289
Patrick Venturec9508db2018-10-16 17:18:43 -0700290 if (iv_type == IPMI_FRU_AREA_INTERNAL_USE)
vishwac93d6d42015-12-16 11:55:16 -0600291 {
292 iv_name = "INTERNAL_";
293 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700294 else if (iv_type == IPMI_FRU_AREA_CHASSIS_INFO)
vishwac93d6d42015-12-16 11:55:16 -0600295 {
296 iv_name = "CHASSIS_";
297 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700298 else if (iv_type == IPMI_FRU_AREA_BOARD_INFO)
vishwac93d6d42015-12-16 11:55:16 -0600299 {
300 iv_name = "BOARD_";
301 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700302 else if (iv_type == IPMI_FRU_AREA_PRODUCT_INFO)
vishwac93d6d42015-12-16 11:55:16 -0600303 {
304 iv_name = "PRODUCT_";
305 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700306 else if (iv_type == IPMI_FRU_AREA_MULTI_RECORD)
vishwac93d6d42015-12-16 11:55:16 -0600307 {
308 iv_name = "MULTI_";
309 }
310 else
311 {
312 iv_name = IPMI_FRU_AREA_TYPE_MAX;
Patrick Venture5c787212018-10-17 13:48:23 -0700313 log<level::ERR>("Invalid Area", entry("TYPE=%d", iv_type));
vishwac93d6d42015-12-16 11:55:16 -0600314 }
315}
316
317//-----------------------------------------------------
318// For a FRU area type, accepts the data and updates
319// area specific data.
320//-----------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700321void ipmi_fru::set_data(const uint8_t* data, const size_t len)
vishwac93d6d42015-12-16 11:55:16 -0600322{
323 iv_len = len;
324 iv_data = new uint8_t[len];
Patrick Venture5c787212018-10-17 13:48:23 -0700325 std::memcpy(iv_data, data, len);
vishwac93d6d42015-12-16 11:55:16 -0600326}
327
328//-----------------------------------------------------
329// Sets the dbus parameters
330//-----------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700331void ipmi_fru::update_dbus_paths(const char* bus_name, const char* obj_path,
332 const char* intf_name)
vishwac93d6d42015-12-16 11:55:16 -0600333{
334 iv_bus_name = bus_name;
335 iv_obj_path = obj_path;
336 iv_intf_name = intf_name;
337}
338
339//-------------------
340// Destructor
341//-------------------
342ipmi_fru::~ipmi_fru()
343{
Patrick Venturec9508db2018-10-16 17:18:43 -0700344 if (iv_data != NULL)
vishwac93d6d42015-12-16 11:55:16 -0600345 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700346 delete[] iv_data;
vishwac93d6d42015-12-16 11:55:16 -0600347 iv_data = NULL;
348 }
vishwac93d6d42015-12-16 11:55:16 -0600349}
350
Vishwa4be4b7a2015-10-31 22:55:50 -0500351//------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600352// Takes the pointer to stream of bytes and length
vishwac93d6d42015-12-16 11:55:16 -0600353// and returns the 8 bit checksum
354// This algo is per IPMI V2.0 spec
Vishwa4be4b7a2015-10-31 22:55:50 -0500355//-------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700356unsigned char calculate_crc(const unsigned char* data, size_t len)
Vishwa4be4b7a2015-10-31 22:55:50 -0500357{
358 char crc = 0;
vishwac93d6d42015-12-16 11:55:16 -0600359 size_t byte = 0;
Vishwa4be4b7a2015-10-31 22:55:50 -0500360
Patrick Venturec9508db2018-10-16 17:18:43 -0700361 for (byte = 0; byte < len; byte++)
Vishwa4be4b7a2015-10-31 22:55:50 -0500362 {
363 crc += *data++;
364 }
vishwaf3ca3522015-12-02 10:35:13 -0600365
Patrick Venturec9508db2018-10-16 17:18:43 -0700366 return (-crc);
Vishwa4be4b7a2015-10-31 22:55:50 -0500367}
368
369//---------------------------------------------------------------------
370// Accepts a fru area offset in commom hdr and tells which area it is.
371//---------------------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600372ipmi_fru_area_type get_fru_area_type(uint8_t area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500373{
374 ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
375
Patrick Venturec9508db2018-10-16 17:18:43 -0700376 switch (area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500377 {
378 case IPMI_FRU_INTERNAL_OFFSET:
379 type = IPMI_FRU_AREA_INTERNAL_USE;
380 break;
381
382 case IPMI_FRU_CHASSIS_OFFSET:
383 type = IPMI_FRU_AREA_CHASSIS_INFO;
384 break;
385
386 case IPMI_FRU_BOARD_OFFSET:
387 type = IPMI_FRU_AREA_BOARD_INFO;
388 break;
389
390 case IPMI_FRU_PRODUCT_OFFSET:
391 type = IPMI_FRU_AREA_PRODUCT_INFO;
392 break;
393
394 case IPMI_FRU_MULTI_OFFSET:
395 type = IPMI_FRU_AREA_MULTI_RECORD;
396 break;
397
398 default:
399 type = IPMI_FRU_AREA_TYPE_MAX;
400 }
401
402 return type;
403}
404
vishwac93d6d42015-12-16 11:55:16 -0600405///-----------------------------------------------
406// Validates the data for crc and mandatory fields
407///-----------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700408int verify_fru_data(const uint8_t* data, const size_t len)
vishwac93d6d42015-12-16 11:55:16 -0600409{
410 uint8_t checksum = 0;
411 int rc = -1;
412
413 // Validate for first byte to always have a value of [1]
Patrick Venturec9508db2018-10-16 17:18:43 -0700414 if (data[0] != IPMI_FRU_HDR_BYTE_ZERO)
vishwac93d6d42015-12-16 11:55:16 -0600415 {
Patrick Venture5c787212018-10-17 13:48:23 -0700416 log<level::ERR>("Invalid entry in byte-0",
417 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600418 return rc;
419 }
420#ifdef __IPMI_DEBUG__
421 else
422 {
Patrick Venture5c787212018-10-17 13:48:23 -0700423 log<level::DEBUG>("Validated in entry_1 of fru_data",
424 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600425 }
426#endif
427
428 // See if the calculated CRC matches with the embedded one.
429 // CRC to be calculated on all except the last one that is CRC itself.
430 checksum = calculate_crc(data, len - 1);
Patrick Venturec9508db2018-10-16 17:18:43 -0700431 if (checksum != data[len - 1])
vishwac93d6d42015-12-16 11:55:16 -0600432 {
433#ifdef __IPMI_DEBUG__
Patrick Venture5c787212018-10-17 13:48:23 -0700434 log<level::ERR>(
435 "Checksum mismatch",
436 entry("Calculated=0x%X", static_cast<uint32_t>(checksum)),
437 entry("Embedded=0x%X", static_cast<uint32_t>(data[len])));
vishwac93d6d42015-12-16 11:55:16 -0600438#endif
439 return rc;
440 }
441#ifdef __IPMI_DEBUG__
442 else
443 {
Patrick Venture5c787212018-10-17 13:48:23 -0700444 log<level::DEBUG>("Checksum matches");
vishwac93d6d42015-12-16 11:55:16 -0600445 }
446#endif
447
448 return EXIT_SUCCESS;
449}
450
vishwac93d6d42015-12-16 11:55:16 -0600451///----------------------------------------------------
452// Checks if a particular fru area is populated or not
453///----------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700454bool remove_invalid_area(const std::unique_ptr<ipmi_fru>& fru_area)
Vishwa4be4b7a2015-10-31 22:55:50 -0500455{
Deepak Kodihalli89ededd2017-02-11 01:59:32 -0600456 // Filter the ones that are empty
Patrick Venturec9508db2018-10-16 17:18:43 -0700457 if (!(fru_area->get_len()))
vishwac93d6d42015-12-16 11:55:16 -0600458 {
459 return true;
460 }
461 return false;
462}
Vishwa4be4b7a2015-10-31 22:55:50 -0500463
vishwac93d6d42015-12-16 11:55:16 -0600464///----------------------------------------------------------------------------------
465// Populates various FRU areas
466// @prereq : This must be called only after validating common header.
467///----------------------------------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700468int ipmi_populate_fru_areas(uint8_t* fru_data, const size_t data_len,
469 fru_area_vec_t& fru_area_vec)
vishwac93d6d42015-12-16 11:55:16 -0600470{
vishwac93d6d42015-12-16 11:55:16 -0600471 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500472
vishwac93d6d42015-12-16 11:55:16 -0600473 // Now walk the common header and see if the file size has atleast the last
474 // offset mentioned by the common_hdr. If the file size is less than the
475 // offset of any if the fru areas mentioned in the common header, then we do
476 // not have a complete file.
Patrick Venturec9508db2018-10-16 17:18:43 -0700477 for (uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
478 fru_entry < (sizeof(struct common_header) - 2); fru_entry++)
vishwac93d6d42015-12-16 11:55:16 -0600479 {
Yi Li75c2d462016-04-11 16:57:46 +0800480 rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600481 // Actual offset in the payload is the offset mentioned in common header
Gunnar Millsc19b8132018-06-14 08:56:17 -0500482 // multiplied by 8. Common header is always the first 8 bytes.
Patrick Venture50ddfe52018-10-16 17:16:32 -0700483 size_t area_offset = fru_data[fru_entry] * IPMI_EIGHT_BYTES;
Patrick Venturec9508db2018-10-16 17:18:43 -0700484 if (area_offset && (data_len < (area_offset + 2)))
vishwac93d6d42015-12-16 11:55:16 -0600485 {
486 // Our file size is less than what it needs to be. +2 because we are
487 // using area len that is at 2 byte off area_offset
Patrick Venture5c787212018-10-17 13:48:23 -0700488 log<level::ERR>("fru file is incomplete",
489 entry("SIZE=%d", data_len));
vishwac93d6d42015-12-16 11:55:16 -0600490 return rc;
491 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700492 else if (area_offset)
vishwac93d6d42015-12-16 11:55:16 -0600493 {
494 // Read 2 bytes to know the actual size of area.
495 uint8_t area_hdr[2] = {0};
Patrick Venture5c787212018-10-17 13:48:23 -0700496 std::memcpy(area_hdr, &((uint8_t*)fru_data)[area_offset],
497 sizeof(area_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500498
vishwac93d6d42015-12-16 11:55:16 -0600499 // Size of this area will be the 2nd byte in the fru area header.
Patrick Venturec9508db2018-10-16 17:18:43 -0700500 size_t area_len = area_hdr[1] * IPMI_EIGHT_BYTES;
vishwac93d6d42015-12-16 11:55:16 -0600501 uint8_t area_data[area_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500502
Patrick Venture5c787212018-10-17 13:48:23 -0700503 log<level::DEBUG>("Fru Data", entry("SIZE=%d", data_len),
504 entry("AREA OFFSET=%d", area_offset),
505 entry("AREA_SIZE=%d", area_len));
Vishwa4be4b7a2015-10-31 22:55:50 -0500506
vishwac93d6d42015-12-16 11:55:16 -0600507 // See if we really have that much buffer. We have area offset amd
508 // from there, the actual len.
Patrick Venturec9508db2018-10-16 17:18:43 -0700509 if (data_len < (area_len + area_offset))
vishwac93d6d42015-12-16 11:55:16 -0600510 {
Patrick Venture5c787212018-10-17 13:48:23 -0700511 log<level::ERR>("Incomplete Fru file",
512 entry("SIZE=%d", data_len));
vishwac93d6d42015-12-16 11:55:16 -0600513 return rc;
514 }
515
516 // Save off the data.
Patrick Venture5c787212018-10-17 13:48:23 -0700517 std::memcpy(area_data, &((uint8_t*)fru_data)[area_offset],
518 area_len);
vishwac93d6d42015-12-16 11:55:16 -0600519
520 // Validate the crc
521 rc = verify_fru_data(area_data, area_len);
Patrick Venturec9508db2018-10-16 17:18:43 -0700522 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600523 {
Patrick Venture5c787212018-10-17 13:48:23 -0700524 log<level::ERR>("Err validating fru area",
525 entry("OFFSET=%d", area_offset));
vishwac93d6d42015-12-16 11:55:16 -0600526 return rc;
527 }
528 else
529 {
Patrick Venture5c787212018-10-17 13:48:23 -0700530 log<level::DEBUG>("Successfully verified area checksum.",
531 entry("OFFSET=%d", area_offset));
vishwac93d6d42015-12-16 11:55:16 -0600532 }
533
534 // We already have a vector that is passed to us containing all
535 // of the fields populated. Update the data portion now.
Patrick Venturec9508db2018-10-16 17:18:43 -0700536 for (auto& iter : fru_area_vec)
vishwac93d6d42015-12-16 11:55:16 -0600537 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700538 if ((iter)->get_type() == get_fru_area_type(fru_entry))
vishwac93d6d42015-12-16 11:55:16 -0600539 {
540 (iter)->set_data(area_data, area_len);
541 }
542 }
543 } // If we have fru data present
Patrick Venturec9508db2018-10-16 17:18:43 -0700544 } // Walk common_hdr
vishwac93d6d42015-12-16 11:55:16 -0600545
546 // Not all the fields will be populated in a fru data. Mostly all cases will
547 // not have more than 2 or 3.
548 fru_area_vec.erase(std::remove_if(fru_area_vec.begin(), fru_area_vec.end(),
Patrick Venturec9508db2018-10-16 17:18:43 -0700549 remove_invalid_area),
550 fru_area_vec.end());
vishwac93d6d42015-12-16 11:55:16 -0600551
552 return EXIT_SUCCESS;
553}
554
555///---------------------------------------------------------
556// Validates the fru data per ipmi common header constructs.
557// Returns with updated common_hdr and also file_size
558//----------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700559int ipmi_validate_common_hdr(const uint8_t* fru_data, const size_t data_len)
vishwac93d6d42015-12-16 11:55:16 -0600560{
561 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500562
563 uint8_t common_hdr[sizeof(struct common_header)] = {0};
Patrick Venturec9508db2018-10-16 17:18:43 -0700564 if (data_len >= sizeof(common_hdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500565 {
Patrick Venture5c787212018-10-17 13:48:23 -0700566 std::memcpy(common_hdr, fru_data, sizeof(common_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500567 }
568 else
569 {
Patrick Venture5c787212018-10-17 13:48:23 -0700570 log<level::ERR>("Incomplete fru data file", entry("SIZE=%d", data_len));
vishwac93d6d42015-12-16 11:55:16 -0600571 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500572 }
573
vishwac93d6d42015-12-16 11:55:16 -0600574 // Verify the crc and size
575 rc = verify_fru_data(common_hdr, sizeof(common_hdr));
Patrick Venturec9508db2018-10-16 17:18:43 -0700576 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500577 {
Patrick Venture5c787212018-10-17 13:48:23 -0700578 log<level::ERR>("Failed to validate common header");
vishwac93d6d42015-12-16 11:55:16 -0600579 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500580 }
581
vishwac93d6d42015-12-16 11:55:16 -0600582 return EXIT_SUCCESS;
583}
Vishwa4be4b7a2015-10-31 22:55:50 -0500584
Vishwa4be4b7a2015-10-31 22:55:50 -0500585///-----------------------------------------------------
586// Accepts the filename and validates per IPMI FRU spec
587//----------------------------------------------------
Patrick Venture98072dc2018-10-20 09:31:35 -0700588int validateFRUArea(const uint8_t fruid, const char* fru_file_name,
589 sd_bus* bus_type, const bool bmc_fru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500590{
vishwac93d6d42015-12-16 11:55:16 -0600591 size_t data_len = 0;
592 size_t bytes_read = 0;
593 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500594
vishwac93d6d42015-12-16 11:55:16 -0600595 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
596 // are not used, keeping it here for completeness.
597 fru_area_vec_t fru_area_vec;
Yi Li75c2d462016-04-11 16:57:46 +0800598
Patrick Venturec9508db2018-10-16 17:18:43 -0700599 for (uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
600 fru_entry < (sizeof(struct common_header) - 2); fru_entry++)
vishwac93d6d42015-12-16 11:55:16 -0600601 {
602 // Create an object and push onto a vector.
Patrick Venturec9508db2018-10-16 17:18:43 -0700603 std::unique_ptr<ipmi_fru> fru_area = std::make_unique<ipmi_fru>(
604 fruid, get_fru_area_type(fru_entry), bus_type, bmc_fru);
vishwac93d6d42015-12-16 11:55:16 -0600605
606 // Physically being present
vishwa2f5a3cf2016-05-30 02:25:21 -0500607 bool present = access(fru_file_name, F_OK) == 0;
vishwac93d6d42015-12-16 11:55:16 -0600608 fru_area->set_present(present);
609
vishwac93d6d42015-12-16 11:55:16 -0600610 fru_area_vec.emplace_back(std::move(fru_area));
611 }
612
Patrick Venture5c787212018-10-17 13:48:23 -0700613 FILE* fru_fp = std::fopen(fru_file_name, "rb");
Patrick Venturec9508db2018-10-16 17:18:43 -0700614 if (fru_fp == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500615 {
Patrick Venture5c787212018-10-17 13:48:23 -0700616 log<level::ERR>("Unable to open fru file",
617 entry("FILE=%s", fru_file_name),
618 entry("ERRNO=%s", std::strerror(errno)));
Patrick Venture234b7352018-10-20 09:37:09 -0700619 return cleanupError(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500620 }
621
vishwac93d6d42015-12-16 11:55:16 -0600622 // Get the size of the file to see if it meets minimum requirement
Patrick Venture5c787212018-10-17 13:48:23 -0700623 if (std::fseek(fru_fp, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500624 {
Patrick Venture5c787212018-10-17 13:48:23 -0700625 log<level::ERR>("Unable to seek fru file",
626 entry("FILE=%s", fru_file_name),
627 entry("ERRNO=%s", std::strerror(errno)));
Patrick Venture234b7352018-10-20 09:37:09 -0700628 return cleanupError(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500629 }
630
vishwac93d6d42015-12-16 11:55:16 -0600631 // Allocate a buffer to hold entire file content
Patrick Venture5c787212018-10-17 13:48:23 -0700632 data_len = std::ftell(fru_fp);
vishwac93d6d42015-12-16 11:55:16 -0600633 uint8_t fru_data[data_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500634
Patrick Venture5c787212018-10-17 13:48:23 -0700635 std::rewind(fru_fp);
636 bytes_read = std::fread(fru_data, data_len, 1, fru_fp);
Patrick Venturec9508db2018-10-16 17:18:43 -0700637 if (bytes_read != 1)
Vishwa4be4b7a2015-10-31 22:55:50 -0500638 {
Patrick Venture5c787212018-10-17 13:48:23 -0700639 log<level::ERR>("Failed reading fru data.",
640 entry("BYTESREAD=%d", bytes_read),
641 entry("ERRNO=%s", std::strerror(errno)));
Patrick Venture234b7352018-10-20 09:37:09 -0700642 return cleanupError(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500643 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500644
vishwac93d6d42015-12-16 11:55:16 -0600645 // We are done reading.
Patrick Venture5c787212018-10-17 13:48:23 -0700646 std::fclose(fru_fp);
vishwac93d6d42015-12-16 11:55:16 -0600647 fru_fp = NULL;
648
649 rc = ipmi_validate_common_hdr(fru_data, data_len);
Patrick Venturec9508db2018-10-16 17:18:43 -0700650 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500651 {
Patrick Venture234b7352018-10-20 09:37:09 -0700652 return cleanupError(fru_fp, fru_area_vec);
vishwac93d6d42015-12-16 11:55:16 -0600653 }
654
Patrick Venturec9508db2018-10-16 17:18:43 -0700655 // Now that we validated the common header, populate various fru sections if
656 // we have them here.
vishwac93d6d42015-12-16 11:55:16 -0600657 rc = ipmi_populate_fru_areas(fru_data, data_len, fru_area_vec);
Patrick Venturec9508db2018-10-16 17:18:43 -0700658 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600659 {
Patrick Venture5c787212018-10-17 13:48:23 -0700660 log<level::ERR>("Populating FRU areas failed", entry("FRU=%d", fruid));
Patrick Venture234b7352018-10-20 09:37:09 -0700661 return cleanupError(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500662 }
663 else
664 {
Patrick Venture5c787212018-10-17 13:48:23 -0700665 log<level::DEBUG>("Populated FRU areas",
666 entry("FILE=%s", fru_file_name));
Vishwa4be4b7a2015-10-31 22:55:50 -0500667 }
668
vishwac93d6d42015-12-16 11:55:16 -0600669#ifdef __IPMI_DEBUG__
Patrick Venturec9508db2018-10-16 17:18:43 -0700670 for (auto& iter : fru_area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500671 {
Patrick Venture5c787212018-10-17 13:48:23 -0700672 std::printf("FRU ID : [%d]\n", (iter)->get_fruid());
673 std::printf("AREA NAME : [%s]\n", (iter)->get_name());
674 std::printf("TYPE : [%d]\n", (iter)->get_type());
675 std::printf("LEN : [%d]\n", (iter)->get_len());
676 std::printf("BUS NAME : [%s]\n", (iter)->get_bus_name());
677 std::printf("OBJ PATH : [%s]\n", (iter)->get_obj_path());
678 std::printf("INTF NAME :[%s]\n", (iter)->get_intf_name());
Vishwa4be4b7a2015-10-31 22:55:50 -0500679 }
vishwac93d6d42015-12-16 11:55:16 -0600680#endif
681
682 // If the vector is populated with everything, then go ahead and update the
683 // inventory.
Patrick Venturec9508db2018-10-16 17:18:43 -0700684 if (!(fru_area_vec.empty()))
vishwac93d6d42015-12-16 11:55:16 -0600685 {
686
687#ifdef __IPMI_DEBUG__
Patrick Venture5c787212018-10-17 13:48:23 -0700688 std::printf("\n SIZE of vector is : [%d] \n", fru_area_vec.size());
vishwac93d6d42015-12-16 11:55:16 -0600689#endif
Patrick Venture44a957d2018-10-20 09:41:29 -0700690 rc = updateInventory(fru_area_vec, bus_type);
Patrick Venturec9508db2018-10-16 17:18:43 -0700691 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600692 {
Patrick Venture5c787212018-10-17 13:48:23 -0700693 log<level::ERR>("Error updating inventory.");
vishwac93d6d42015-12-16 11:55:16 -0600694 }
695 }
696
697 // we are done with all that we wanted to do. This will do the job of
698 // calling any destructors too.
699 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500700
701 return rc;
702}