blob: 1cbf21001ffecdf22087434a5644bdd924741f6f [file] [log] [blame]
Patrick Venture02ba8842018-10-20 13:37:54 -07001#include "writefrudata.hpp"
2
Patrick Venture5c2bd5e2018-10-20 19:45:48 -07003#include "fru_area.hpp"
Patrick Venturec9508db2018-10-16 17:18:43 -07004#include "frup.hpp"
5#include "types.hpp"
6
Patrick Venturec9508db2018-10-16 17:18:43 -07007#include <host-ipmid/ipmid-api.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>
Patrick Ventureb390c0e2018-10-20 13:36:48 -070017#include <map>
vishwac93d6d42015-12-16 11:55:16 -060018#include <memory>
Patrick Venture5739ac32018-10-16 19:17:41 -070019#include <phosphor-logging/log.hpp>
Patrick Venture71918492018-10-20 19:40:13 -070020#include <sdbusplus/bus.hpp>
Patrick Venturec9508db2018-10-16 17:18:43 -070021#include <sstream>
22#include <vector>
Vishwa4be4b7a2015-10-31 22:55:50 -050023
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060024using namespace ipmi::vpd;
Patrick Venture5739ac32018-10-16 19:17:41 -070025using namespace phosphor::logging;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060026
Ratan Guptacb0d4e52016-12-22 19:05:57 +053027extern const FruMap frus;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060028extern const std::map<Path, InterfaceMap> extras;
Ratan Guptacb0d4e52016-12-22 19:05:57 +053029
Patrick Venture19bea9a2018-10-20 19:54:27 -070030using FruAreaVector = std::vector<std::unique_ptr<IPMIFruArea>>;
31
Patrick Venture234b7352018-10-20 09:37:09 -070032namespace
33{
34
35//------------------------------------------------------------
36// Cleanup routine
37// Must always be called as last reference to fru_fp.
38//------------------------------------------------------------
Patrick Venture19bea9a2018-10-20 19:54:27 -070039int cleanupError(FILE* fru_fp, FruAreaVector& fru_area_vec)
Patrick Venture234b7352018-10-20 09:37:09 -070040{
41 if (fru_fp != NULL)
42 {
43 std::fclose(fru_fp);
44 }
45
46 if (!(fru_area_vec.empty()))
47 {
48 fru_area_vec.clear();
49 }
50
51 return -1;
52}
53
Patrick Venturede8ea562018-10-20 09:44:19 -070054//------------------------------------------------------------------------
55// Gets the value of the key from the fru dictionary of the given section.
56// FRU dictionary is parsed fru data for all the sections.
57//------------------------------------------------------------------------
58std::string getFRUValue(const std::string& section, const std::string& key,
59 const std::string& delimiter, IPMIFruInfo& fruData)
60{
61
62 auto minIndexValue = 0;
63 auto maxIndexValue = 0;
64 std::string fruValue = "";
65
66 if (section == "Board")
67 {
68 minIndexValue = OPENBMC_VPD_KEY_BOARD_MFG_DATE;
69 maxIndexValue = OPENBMC_VPD_KEY_BOARD_MAX;
70 }
71 else if (section == "Product")
72 {
73 minIndexValue = OPENBMC_VPD_KEY_PRODUCT_MFR;
74 maxIndexValue = OPENBMC_VPD_KEY_PRODUCT_MAX;
75 }
76 else if (section == "Chassis")
77 {
78 minIndexValue = OPENBMC_VPD_KEY_CHASSIS_TYPE;
79 maxIndexValue = OPENBMC_VPD_KEY_CHASSIS_MAX;
80 }
81
82 auto first = fruData.cbegin() + minIndexValue;
83 auto last = first + (maxIndexValue - minIndexValue) + 1;
84
85 auto itr =
86 std::find_if(first, last, [&key](auto& e) { return key == e.first; });
87
88 if (itr != last)
89 {
90 fruValue = itr->second;
91 }
92
93 // if the key is custom property then the value could be in two formats.
94 // 1) custom field 2 = "value".
95 // 2) custom field 2 = "key:value".
96 // if delimiter length = 0 i.e custom field 2 = "value"
97
98 constexpr auto customProp = "Custom Field";
99 if (key.find(customProp) != std::string::npos)
100 {
101 if (delimiter.length() > 0)
102 {
103 size_t delimiterpos = fruValue.find(delimiter);
104 if (delimiterpos != std::string::npos)
105 fruValue = fruValue.substr(delimiterpos + 1);
106 }
107 }
108 return fruValue;
109}
110
Patrick Venture17baa272018-10-20 09:48:08 -0700111// Get the inventory service from the mapper.
112auto getService(sdbusplus::bus::bus& bus, const std::string& intf,
113 const std::string& path)
114{
115 auto mapperCall =
116 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
117 "/xyz/openbmc_project/object_mapper",
118 "xyz.openbmc_project.ObjectMapper", "GetObject");
119
120 mapperCall.append(path);
121 mapperCall.append(std::vector<std::string>({intf}));
122 std::map<std::string, std::vector<std::string>> mapperResponse;
123
124 try
125 {
126 auto mapperResponseMsg = bus.call(mapperCall);
127 mapperResponseMsg.read(mapperResponse);
128 }
129 catch (const sdbusplus::exception::SdBusError& ex)
130 {
131 log<level::ERR>("Exception from sdbus call",
132 entry("WHAT=%s", ex.what()));
133 throw;
134 }
135
136 if (mapperResponse.begin() == mapperResponse.end())
137 {
138 throw std::runtime_error("ERROR in reading the mapper response");
139 }
140
141 return mapperResponse.begin()->first;
142}
143
Patrick Venture44a957d2018-10-20 09:41:29 -0700144// Takes FRU data, invokes Parser for each fru record area and updates
145// Inventory
146//------------------------------------------------------------------------
Patrick Venture19bea9a2018-10-20 19:54:27 -0700147int updateInventory(FruAreaVector& area_vec, sd_bus* bus_sd)
Patrick Venture44a957d2018-10-20 09:41:29 -0700148{
149 // Generic error reporter
150 int rc = 0;
151 uint8_t fruid = 0;
152 IPMIFruInfo fruData;
153
154 // For each FRU area, extract the needed data , get it parsed and update
155 // the Inventory.
156 for (const auto& fruArea : area_vec)
157 {
158 fruid = fruArea->get_fruid();
159 // Fill the container with information
160 rc = parse_fru_area((fruArea)->get_type(), (void*)(fruArea)->get_data(),
161 (fruArea)->get_len(), fruData);
162 if (rc < 0)
163 {
164 log<level::ERR>("Error parsing FRU records");
165 return rc;
166 }
167 } // END walking the vector of areas and updating
168
169 // For each Fru we have the list of instances which needs to be updated.
170 // Each instance object implements certain interfaces.
171 // Each Interface is having Dbus properties.
172 // Each Dbus Property would be having metaData(eg section,VpdPropertyName).
173
174 // Here we are just printing the object,interface and the properties.
175 // which needs to be called with the new inventory manager implementation.
176 sdbusplus::bus::bus bus{bus_sd};
177 using namespace std::string_literals;
178 static const auto intf = "xyz.openbmc_project.Inventory.Manager"s;
179 static const auto path = "/xyz/openbmc_project/inventory"s;
180 std::string service;
181 try
182 {
183 service = getService(bus, intf, path);
184 }
185 catch (const std::exception& e)
186 {
187 std::cerr << e.what() << "\n";
188 return -1;
189 }
190
191 auto iter = frus.find(fruid);
192 if (iter == frus.end())
193 {
194 log<level::ERR>("Unable to get the fru info",
195 entry("FRU=%d", static_cast<int>(fruid)));
196 return -1;
197 }
198
199 auto& instanceList = iter->second;
200 if (instanceList.size() <= 0)
201 {
202 log<level::DEBUG>("Object list empty for this FRU",
203 entry("FRU=%d", static_cast<int>(fruid)));
204 }
205
206 ObjectMap objects;
207 for (auto& instance : instanceList)
208 {
209 InterfaceMap interfaces;
210 const auto& extrasIter = extras.find(instance.path);
211
212 for (auto& interfaceList : instance.interfaces)
213 {
214 PropertyMap props; // store all the properties
215 for (auto& properties : interfaceList.second)
216 {
217 std::string value;
218 decltype(auto) pdata = properties.second;
219
220 if (!pdata.section.empty() && !pdata.property.empty())
221 {
222 value = getFRUValue(pdata.section, pdata.property,
223 pdata.delimiter, fruData);
224 }
225 props.emplace(std::move(properties.first), std::move(value));
226 }
227 // Check and update extra properties
228 if (extras.end() != extrasIter)
229 {
230 const auto& propsIter =
231 (extrasIter->second).find(interfaceList.first);
232 if ((extrasIter->second).end() != propsIter)
233 {
234 for (const auto& map : propsIter->second)
235 {
236 props.emplace(map.first, map.second);
237 }
238 }
239 }
240 interfaces.emplace(std::move(interfaceList.first),
241 std::move(props));
242 }
243
244 // Call the inventory manager
Patrick Venture9a528f22018-10-20 13:31:28 -0700245 sdbusplus::message::object_path objectPath = instance.path;
Patrick Venture44a957d2018-10-20 09:41:29 -0700246 // Check and update extra properties
247 if (extras.end() != extrasIter)
248 {
249 for (const auto& entry : extrasIter->second)
250 {
251 if (interfaces.end() == interfaces.find(entry.first))
252 {
253 interfaces.emplace(entry.first, entry.second);
254 }
255 }
256 }
Patrick Venture9a528f22018-10-20 13:31:28 -0700257 objects.emplace(objectPath, interfaces);
Patrick Venture44a957d2018-10-20 09:41:29 -0700258 }
259
260 auto pimMsg = bus.new_method_call(service.c_str(), path.c_str(),
261 intf.c_str(), "Notify");
262 pimMsg.append(std::move(objects));
263
264 try
265 {
266 auto inventoryMgrResponseMsg = bus.call(pimMsg);
267 }
268 catch (const sdbusplus::exception::SdBusError& ex)
269 {
270 log<level::ERR>("Error in notify call", entry("WHAT=%s", ex.what()));
271 return -1;
272 }
273
274 return rc;
275}
276
Patrick Venture234b7352018-10-20 09:37:09 -0700277} // namespace
278
vishwac93d6d42015-12-16 11:55:16 -0600279//----------------------------------------------------------------
280// Constructor
281//----------------------------------------------------------------
Patrick Venture9eb82cf2018-10-20 19:36:01 -0700282IPMIFruArea::IPMIFruArea(const uint8_t fruid, const ipmi_fru_area_type type,
283 bool bmc_fru)
vishwac93d6d42015-12-16 11:55:16 -0600284{
285 iv_fruid = fruid;
286 iv_type = type;
287 iv_bmc_fru = bmc_fru;
vishwac93d6d42015-12-16 11:55:16 -0600288 iv_valid = false;
289 iv_data = NULL;
290 iv_present = false;
291
Patrick Venturec9508db2018-10-16 17:18:43 -0700292 if (iv_type == IPMI_FRU_AREA_INTERNAL_USE)
vishwac93d6d42015-12-16 11:55:16 -0600293 {
294 iv_name = "INTERNAL_";
295 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700296 else if (iv_type == IPMI_FRU_AREA_CHASSIS_INFO)
vishwac93d6d42015-12-16 11:55:16 -0600297 {
298 iv_name = "CHASSIS_";
299 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700300 else if (iv_type == IPMI_FRU_AREA_BOARD_INFO)
vishwac93d6d42015-12-16 11:55:16 -0600301 {
302 iv_name = "BOARD_";
303 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700304 else if (iv_type == IPMI_FRU_AREA_PRODUCT_INFO)
vishwac93d6d42015-12-16 11:55:16 -0600305 {
306 iv_name = "PRODUCT_";
307 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700308 else if (iv_type == IPMI_FRU_AREA_MULTI_RECORD)
vishwac93d6d42015-12-16 11:55:16 -0600309 {
310 iv_name = "MULTI_";
311 }
312 else
313 {
314 iv_name = IPMI_FRU_AREA_TYPE_MAX;
Patrick Venture5c787212018-10-17 13:48:23 -0700315 log<level::ERR>("Invalid Area", entry("TYPE=%d", iv_type));
vishwac93d6d42015-12-16 11:55:16 -0600316 }
317}
318
319//-----------------------------------------------------
320// For a FRU area type, accepts the data and updates
321// area specific data.
322//-----------------------------------------------------
Patrick Venture9eb82cf2018-10-20 19:36:01 -0700323void IPMIFruArea::set_data(const uint8_t* data, const size_t len)
vishwac93d6d42015-12-16 11:55:16 -0600324{
325 iv_len = len;
326 iv_data = new uint8_t[len];
Patrick Venture5c787212018-10-17 13:48:23 -0700327 std::memcpy(iv_data, data, len);
vishwac93d6d42015-12-16 11:55:16 -0600328}
329
330//-----------------------------------------------------
331// Sets the dbus parameters
332//-----------------------------------------------------
Patrick Venture9eb82cf2018-10-20 19:36:01 -0700333void IPMIFruArea::update_dbus_paths(const char* bus_name, const char* obj_path,
334 const char* intf_name)
vishwac93d6d42015-12-16 11:55:16 -0600335{
336 iv_bus_name = bus_name;
337 iv_obj_path = obj_path;
338 iv_intf_name = intf_name;
339}
340
341//-------------------
342// Destructor
343//-------------------
Patrick Venture9eb82cf2018-10-20 19:36:01 -0700344IPMIFruArea::~IPMIFruArea()
vishwac93d6d42015-12-16 11:55:16 -0600345{
Patrick Venturec9508db2018-10-16 17:18:43 -0700346 if (iv_data != NULL)
vishwac93d6d42015-12-16 11:55:16 -0600347 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700348 delete[] iv_data;
vishwac93d6d42015-12-16 11:55:16 -0600349 iv_data = NULL;
350 }
vishwac93d6d42015-12-16 11:55:16 -0600351}
352
Vishwa4be4b7a2015-10-31 22:55:50 -0500353//------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600354// Takes the pointer to stream of bytes and length
vishwac93d6d42015-12-16 11:55:16 -0600355// and returns the 8 bit checksum
356// This algo is per IPMI V2.0 spec
Vishwa4be4b7a2015-10-31 22:55:50 -0500357//-------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700358unsigned char calculate_crc(const unsigned char* data, size_t len)
Vishwa4be4b7a2015-10-31 22:55:50 -0500359{
360 char crc = 0;
vishwac93d6d42015-12-16 11:55:16 -0600361 size_t byte = 0;
Vishwa4be4b7a2015-10-31 22:55:50 -0500362
Patrick Venturec9508db2018-10-16 17:18:43 -0700363 for (byte = 0; byte < len; byte++)
Vishwa4be4b7a2015-10-31 22:55:50 -0500364 {
365 crc += *data++;
366 }
vishwaf3ca3522015-12-02 10:35:13 -0600367
Patrick Venturec9508db2018-10-16 17:18:43 -0700368 return (-crc);
Vishwa4be4b7a2015-10-31 22:55:50 -0500369}
370
371//---------------------------------------------------------------------
372// Accepts a fru area offset in commom hdr and tells which area it is.
373//---------------------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600374ipmi_fru_area_type get_fru_area_type(uint8_t area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500375{
376 ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
377
Patrick Venturec9508db2018-10-16 17:18:43 -0700378 switch (area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500379 {
380 case IPMI_FRU_INTERNAL_OFFSET:
381 type = IPMI_FRU_AREA_INTERNAL_USE;
382 break;
383
384 case IPMI_FRU_CHASSIS_OFFSET:
385 type = IPMI_FRU_AREA_CHASSIS_INFO;
386 break;
387
388 case IPMI_FRU_BOARD_OFFSET:
389 type = IPMI_FRU_AREA_BOARD_INFO;
390 break;
391
392 case IPMI_FRU_PRODUCT_OFFSET:
393 type = IPMI_FRU_AREA_PRODUCT_INFO;
394 break;
395
396 case IPMI_FRU_MULTI_OFFSET:
397 type = IPMI_FRU_AREA_MULTI_RECORD;
398 break;
399
400 default:
401 type = IPMI_FRU_AREA_TYPE_MAX;
402 }
403
404 return type;
405}
406
vishwac93d6d42015-12-16 11:55:16 -0600407///-----------------------------------------------
408// Validates the data for crc and mandatory fields
409///-----------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700410int verify_fru_data(const uint8_t* data, const size_t len)
vishwac93d6d42015-12-16 11:55:16 -0600411{
412 uint8_t checksum = 0;
413 int rc = -1;
414
415 // Validate for first byte to always have a value of [1]
Patrick Venturec9508db2018-10-16 17:18:43 -0700416 if (data[0] != IPMI_FRU_HDR_BYTE_ZERO)
vishwac93d6d42015-12-16 11:55:16 -0600417 {
Patrick Venture5c787212018-10-17 13:48:23 -0700418 log<level::ERR>("Invalid entry in byte-0",
419 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600420 return rc;
421 }
422#ifdef __IPMI_DEBUG__
423 else
424 {
Patrick Venture5c787212018-10-17 13:48:23 -0700425 log<level::DEBUG>("Validated in entry_1 of fru_data",
426 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600427 }
428#endif
429
430 // See if the calculated CRC matches with the embedded one.
431 // CRC to be calculated on all except the last one that is CRC itself.
432 checksum = calculate_crc(data, len - 1);
Patrick Venturec9508db2018-10-16 17:18:43 -0700433 if (checksum != data[len - 1])
vishwac93d6d42015-12-16 11:55:16 -0600434 {
435#ifdef __IPMI_DEBUG__
Patrick Venture5c787212018-10-17 13:48:23 -0700436 log<level::ERR>(
437 "Checksum mismatch",
438 entry("Calculated=0x%X", static_cast<uint32_t>(checksum)),
439 entry("Embedded=0x%X", static_cast<uint32_t>(data[len])));
vishwac93d6d42015-12-16 11:55:16 -0600440#endif
441 return rc;
442 }
443#ifdef __IPMI_DEBUG__
444 else
445 {
Patrick Venture5c787212018-10-17 13:48:23 -0700446 log<level::DEBUG>("Checksum matches");
vishwac93d6d42015-12-16 11:55:16 -0600447 }
448#endif
449
450 return EXIT_SUCCESS;
451}
452
vishwac93d6d42015-12-16 11:55:16 -0600453///----------------------------------------------------
454// Checks if a particular fru area is populated or not
455///----------------------------------------------------
Patrick Venture9eb82cf2018-10-20 19:36:01 -0700456bool remove_invalid_area(const std::unique_ptr<IPMIFruArea>& fru_area)
Vishwa4be4b7a2015-10-31 22:55:50 -0500457{
Deepak Kodihalli89ededd2017-02-11 01:59:32 -0600458 // Filter the ones that are empty
Patrick Venturec9508db2018-10-16 17:18:43 -0700459 if (!(fru_area->get_len()))
vishwac93d6d42015-12-16 11:55:16 -0600460 {
461 return true;
462 }
463 return false;
464}
Vishwa4be4b7a2015-10-31 22:55:50 -0500465
vishwac93d6d42015-12-16 11:55:16 -0600466///----------------------------------------------------------------------------------
467// Populates various FRU areas
468// @prereq : This must be called only after validating common header.
469///----------------------------------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700470int ipmi_populate_fru_areas(uint8_t* fru_data, const size_t data_len,
Patrick Venture19bea9a2018-10-20 19:54:27 -0700471 FruAreaVector& fru_area_vec)
vishwac93d6d42015-12-16 11:55:16 -0600472{
vishwac93d6d42015-12-16 11:55:16 -0600473 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500474
vishwac93d6d42015-12-16 11:55:16 -0600475 // Now walk the common header and see if the file size has atleast the last
476 // offset mentioned by the common_hdr. If the file size is less than the
477 // offset of any if the fru areas mentioned in the common header, then we do
478 // not have a complete file.
Patrick Venturec9508db2018-10-16 17:18:43 -0700479 for (uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
480 fru_entry < (sizeof(struct common_header) - 2); fru_entry++)
vishwac93d6d42015-12-16 11:55:16 -0600481 {
Yi Li75c2d462016-04-11 16:57:46 +0800482 rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600483 // Actual offset in the payload is the offset mentioned in common header
Gunnar Millsc19b8132018-06-14 08:56:17 -0500484 // multiplied by 8. Common header is always the first 8 bytes.
Patrick Venture50ddfe52018-10-16 17:16:32 -0700485 size_t area_offset = fru_data[fru_entry] * IPMI_EIGHT_BYTES;
Patrick Venturec9508db2018-10-16 17:18:43 -0700486 if (area_offset && (data_len < (area_offset + 2)))
vishwac93d6d42015-12-16 11:55:16 -0600487 {
488 // Our file size is less than what it needs to be. +2 because we are
489 // using area len that is at 2 byte off area_offset
Patrick Venture5c787212018-10-17 13:48:23 -0700490 log<level::ERR>("fru file is incomplete",
491 entry("SIZE=%d", data_len));
vishwac93d6d42015-12-16 11:55:16 -0600492 return rc;
493 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700494 else if (area_offset)
vishwac93d6d42015-12-16 11:55:16 -0600495 {
496 // Read 2 bytes to know the actual size of area.
497 uint8_t area_hdr[2] = {0};
Patrick Venture5c787212018-10-17 13:48:23 -0700498 std::memcpy(area_hdr, &((uint8_t*)fru_data)[area_offset],
499 sizeof(area_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500500
vishwac93d6d42015-12-16 11:55:16 -0600501 // Size of this area will be the 2nd byte in the fru area header.
Patrick Venturec9508db2018-10-16 17:18:43 -0700502 size_t area_len = area_hdr[1] * IPMI_EIGHT_BYTES;
vishwac93d6d42015-12-16 11:55:16 -0600503 uint8_t area_data[area_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500504
Patrick Venture5c787212018-10-17 13:48:23 -0700505 log<level::DEBUG>("Fru Data", entry("SIZE=%d", data_len),
506 entry("AREA OFFSET=%d", area_offset),
507 entry("AREA_SIZE=%d", area_len));
Vishwa4be4b7a2015-10-31 22:55:50 -0500508
vishwac93d6d42015-12-16 11:55:16 -0600509 // See if we really have that much buffer. We have area offset amd
510 // from there, the actual len.
Patrick Venturec9508db2018-10-16 17:18:43 -0700511 if (data_len < (area_len + area_offset))
vishwac93d6d42015-12-16 11:55:16 -0600512 {
Patrick Venture5c787212018-10-17 13:48:23 -0700513 log<level::ERR>("Incomplete Fru file",
514 entry("SIZE=%d", data_len));
vishwac93d6d42015-12-16 11:55:16 -0600515 return rc;
516 }
517
518 // Save off the data.
Patrick Venture5c787212018-10-17 13:48:23 -0700519 std::memcpy(area_data, &((uint8_t*)fru_data)[area_offset],
520 area_len);
vishwac93d6d42015-12-16 11:55:16 -0600521
522 // Validate the crc
523 rc = verify_fru_data(area_data, area_len);
Patrick Venturec9508db2018-10-16 17:18:43 -0700524 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600525 {
Patrick Venture5c787212018-10-17 13:48:23 -0700526 log<level::ERR>("Err validating fru area",
527 entry("OFFSET=%d", area_offset));
vishwac93d6d42015-12-16 11:55:16 -0600528 return rc;
529 }
530 else
531 {
Patrick Venture5c787212018-10-17 13:48:23 -0700532 log<level::DEBUG>("Successfully verified area checksum.",
533 entry("OFFSET=%d", area_offset));
vishwac93d6d42015-12-16 11:55:16 -0600534 }
535
536 // We already have a vector that is passed to us containing all
537 // of the fields populated. Update the data portion now.
Patrick Venturec9508db2018-10-16 17:18:43 -0700538 for (auto& iter : fru_area_vec)
vishwac93d6d42015-12-16 11:55:16 -0600539 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700540 if ((iter)->get_type() == get_fru_area_type(fru_entry))
vishwac93d6d42015-12-16 11:55:16 -0600541 {
542 (iter)->set_data(area_data, area_len);
543 }
544 }
545 } // If we have fru data present
Patrick Venturec9508db2018-10-16 17:18:43 -0700546 } // Walk common_hdr
vishwac93d6d42015-12-16 11:55:16 -0600547
548 // Not all the fields will be populated in a fru data. Mostly all cases will
549 // not have more than 2 or 3.
550 fru_area_vec.erase(std::remove_if(fru_area_vec.begin(), fru_area_vec.end(),
Patrick Venturec9508db2018-10-16 17:18:43 -0700551 remove_invalid_area),
552 fru_area_vec.end());
vishwac93d6d42015-12-16 11:55:16 -0600553
554 return EXIT_SUCCESS;
555}
556
557///---------------------------------------------------------
558// Validates the fru data per ipmi common header constructs.
559// Returns with updated common_hdr and also file_size
560//----------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700561int ipmi_validate_common_hdr(const uint8_t* fru_data, const size_t data_len)
vishwac93d6d42015-12-16 11:55:16 -0600562{
563 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500564
565 uint8_t common_hdr[sizeof(struct common_header)] = {0};
Patrick Venturec9508db2018-10-16 17:18:43 -0700566 if (data_len >= sizeof(common_hdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500567 {
Patrick Venture5c787212018-10-17 13:48:23 -0700568 std::memcpy(common_hdr, fru_data, sizeof(common_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500569 }
570 else
571 {
Patrick Venture5c787212018-10-17 13:48:23 -0700572 log<level::ERR>("Incomplete fru data file", entry("SIZE=%d", data_len));
vishwac93d6d42015-12-16 11:55:16 -0600573 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500574 }
575
vishwac93d6d42015-12-16 11:55:16 -0600576 // Verify the crc and size
577 rc = verify_fru_data(common_hdr, sizeof(common_hdr));
Patrick Venturec9508db2018-10-16 17:18:43 -0700578 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500579 {
Patrick Venture5c787212018-10-17 13:48:23 -0700580 log<level::ERR>("Failed to validate common header");
vishwac93d6d42015-12-16 11:55:16 -0600581 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500582 }
583
vishwac93d6d42015-12-16 11:55:16 -0600584 return EXIT_SUCCESS;
585}
Vishwa4be4b7a2015-10-31 22:55:50 -0500586
Vishwa4be4b7a2015-10-31 22:55:50 -0500587///-----------------------------------------------------
588// Accepts the filename and validates per IPMI FRU spec
589//----------------------------------------------------
Patrick Venture98072dc2018-10-20 09:31:35 -0700590int validateFRUArea(const uint8_t fruid, const char* fru_file_name,
591 sd_bus* bus_type, const bool bmc_fru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500592{
vishwac93d6d42015-12-16 11:55:16 -0600593 size_t data_len = 0;
594 size_t bytes_read = 0;
595 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500596
vishwac93d6d42015-12-16 11:55:16 -0600597 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
598 // are not used, keeping it here for completeness.
Patrick Venture19bea9a2018-10-20 19:54:27 -0700599 FruAreaVector fru_area_vec;
Yi Li75c2d462016-04-11 16:57:46 +0800600
Patrick Venturec9508db2018-10-16 17:18:43 -0700601 for (uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
602 fru_entry < (sizeof(struct common_header) - 2); fru_entry++)
vishwac93d6d42015-12-16 11:55:16 -0600603 {
604 // Create an object and push onto a vector.
Patrick Venture9eb82cf2018-10-20 19:36:01 -0700605 std::unique_ptr<IPMIFruArea> fru_area = std::make_unique<IPMIFruArea>(
Patrick Ventured9af7b42018-10-20 19:23:36 -0700606 fruid, get_fru_area_type(fru_entry), bmc_fru);
vishwac93d6d42015-12-16 11:55:16 -0600607
608 // Physically being present
vishwa2f5a3cf2016-05-30 02:25:21 -0500609 bool present = access(fru_file_name, F_OK) == 0;
vishwac93d6d42015-12-16 11:55:16 -0600610 fru_area->set_present(present);
611
vishwac93d6d42015-12-16 11:55:16 -0600612 fru_area_vec.emplace_back(std::move(fru_area));
613 }
614
Patrick Venture5c787212018-10-17 13:48:23 -0700615 FILE* fru_fp = std::fopen(fru_file_name, "rb");
Patrick Venturec9508db2018-10-16 17:18:43 -0700616 if (fru_fp == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500617 {
Patrick Venture5c787212018-10-17 13:48:23 -0700618 log<level::ERR>("Unable to open fru file",
619 entry("FILE=%s", fru_file_name),
620 entry("ERRNO=%s", std::strerror(errno)));
Patrick Venture234b7352018-10-20 09:37:09 -0700621 return cleanupError(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500622 }
623
vishwac93d6d42015-12-16 11:55:16 -0600624 // Get the size of the file to see if it meets minimum requirement
Patrick Venture5c787212018-10-17 13:48:23 -0700625 if (std::fseek(fru_fp, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500626 {
Patrick Venture5c787212018-10-17 13:48:23 -0700627 log<level::ERR>("Unable to seek fru file",
628 entry("FILE=%s", fru_file_name),
629 entry("ERRNO=%s", std::strerror(errno)));
Patrick Venture234b7352018-10-20 09:37:09 -0700630 return cleanupError(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500631 }
632
vishwac93d6d42015-12-16 11:55:16 -0600633 // Allocate a buffer to hold entire file content
Patrick Venture5c787212018-10-17 13:48:23 -0700634 data_len = std::ftell(fru_fp);
vishwac93d6d42015-12-16 11:55:16 -0600635 uint8_t fru_data[data_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500636
Patrick Venture5c787212018-10-17 13:48:23 -0700637 std::rewind(fru_fp);
638 bytes_read = std::fread(fru_data, data_len, 1, fru_fp);
Patrick Venturec9508db2018-10-16 17:18:43 -0700639 if (bytes_read != 1)
Vishwa4be4b7a2015-10-31 22:55:50 -0500640 {
Patrick Venture5c787212018-10-17 13:48:23 -0700641 log<level::ERR>("Failed reading fru data.",
642 entry("BYTESREAD=%d", bytes_read),
643 entry("ERRNO=%s", std::strerror(errno)));
Patrick Venture234b7352018-10-20 09:37:09 -0700644 return cleanupError(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500645 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500646
vishwac93d6d42015-12-16 11:55:16 -0600647 // We are done reading.
Patrick Venture5c787212018-10-17 13:48:23 -0700648 std::fclose(fru_fp);
vishwac93d6d42015-12-16 11:55:16 -0600649 fru_fp = NULL;
650
651 rc = ipmi_validate_common_hdr(fru_data, data_len);
Patrick Venturec9508db2018-10-16 17:18:43 -0700652 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500653 {
Patrick Venture234b7352018-10-20 09:37:09 -0700654 return cleanupError(fru_fp, fru_area_vec);
vishwac93d6d42015-12-16 11:55:16 -0600655 }
656
Patrick Venturec9508db2018-10-16 17:18:43 -0700657 // Now that we validated the common header, populate various fru sections if
658 // we have them here.
vishwac93d6d42015-12-16 11:55:16 -0600659 rc = ipmi_populate_fru_areas(fru_data, data_len, fru_area_vec);
Patrick Venturec9508db2018-10-16 17:18:43 -0700660 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600661 {
Patrick Venture5c787212018-10-17 13:48:23 -0700662 log<level::ERR>("Populating FRU areas failed", entry("FRU=%d", fruid));
Patrick Venture234b7352018-10-20 09:37:09 -0700663 return cleanupError(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500664 }
665 else
666 {
Patrick Venture5c787212018-10-17 13:48:23 -0700667 log<level::DEBUG>("Populated FRU areas",
668 entry("FILE=%s", fru_file_name));
Vishwa4be4b7a2015-10-31 22:55:50 -0500669 }
670
vishwac93d6d42015-12-16 11:55:16 -0600671#ifdef __IPMI_DEBUG__
Patrick Venturec9508db2018-10-16 17:18:43 -0700672 for (auto& iter : fru_area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500673 {
Patrick Venture5c787212018-10-17 13:48:23 -0700674 std::printf("FRU ID : [%d]\n", (iter)->get_fruid());
675 std::printf("AREA NAME : [%s]\n", (iter)->get_name());
676 std::printf("TYPE : [%d]\n", (iter)->get_type());
677 std::printf("LEN : [%d]\n", (iter)->get_len());
678 std::printf("BUS NAME : [%s]\n", (iter)->get_bus_name());
679 std::printf("OBJ PATH : [%s]\n", (iter)->get_obj_path());
680 std::printf("INTF NAME :[%s]\n", (iter)->get_intf_name());
Vishwa4be4b7a2015-10-31 22:55:50 -0500681 }
vishwac93d6d42015-12-16 11:55:16 -0600682#endif
683
684 // If the vector is populated with everything, then go ahead and update the
685 // inventory.
Patrick Venturec9508db2018-10-16 17:18:43 -0700686 if (!(fru_area_vec.empty()))
vishwac93d6d42015-12-16 11:55:16 -0600687 {
688
689#ifdef __IPMI_DEBUG__
Patrick Venture5c787212018-10-17 13:48:23 -0700690 std::printf("\n SIZE of vector is : [%d] \n", fru_area_vec.size());
vishwac93d6d42015-12-16 11:55:16 -0600691#endif
Patrick Venture44a957d2018-10-20 09:41:29 -0700692 rc = updateInventory(fru_area_vec, bus_type);
Patrick Venturec9508db2018-10-16 17:18:43 -0700693 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600694 {
Patrick Venture5c787212018-10-17 13:48:23 -0700695 log<level::ERR>("Error updating inventory.");
vishwac93d6d42015-12-16 11:55:16 -0600696 }
697 }
698
699 // we are done with all that we wanted to do. This will do the job of
700 // calling any destructors too.
701 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500702
703 return rc;
704}