blob: 02cff5f7cb490e866dfed9c4fcd5cd770365bfb5 [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
Patrick Venturec7eecc12018-10-21 08:55:44 -070085 auto itr = std::find_if(first, last,
86 [&key](const auto& e) { return key == e.first; });
Patrick Venturede8ea562018-10-20 09:44:19 -070087
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 Venturea8093a22018-10-21 09:07:11 -0700147int updateInventory(FruAreaVector& area_vec, sdbusplus::bus::bus& bus)
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 {
Patrick Venturef22b36a2018-10-20 20:59:07 -0700158 fruid = fruArea->getFruID();
Patrick Venture44a957d2018-10-20 09:41:29 -0700159 // Fill the container with information
Patrick Venturef22b36a2018-10-20 20:59:07 -0700160 rc = parse_fru_area((fruArea)->getType(), (void*)(fruArea)->getData(),
161 (fruArea)->getLength(), fruData);
Patrick Venture44a957d2018-10-20 09:41:29 -0700162 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.
Patrick Venture44a957d2018-10-20 09:41:29 -0700176 using namespace std::string_literals;
177 static const auto intf = "xyz.openbmc_project.Inventory.Manager"s;
178 static const auto path = "/xyz/openbmc_project/inventory"s;
179 std::string service;
180 try
181 {
182 service = getService(bus, intf, path);
183 }
184 catch (const std::exception& e)
185 {
186 std::cerr << e.what() << "\n";
187 return -1;
188 }
189
190 auto iter = frus.find(fruid);
191 if (iter == frus.end())
192 {
193 log<level::ERR>("Unable to get the fru info",
194 entry("FRU=%d", static_cast<int>(fruid)));
195 return -1;
196 }
197
198 auto& instanceList = iter->second;
199 if (instanceList.size() <= 0)
200 {
201 log<level::DEBUG>("Object list empty for this FRU",
202 entry("FRU=%d", static_cast<int>(fruid)));
203 }
204
205 ObjectMap objects;
Patrick Venturec7eecc12018-10-21 08:55:44 -0700206 for (const auto& instance : instanceList)
Patrick Venture44a957d2018-10-20 09:41:29 -0700207 {
208 InterfaceMap interfaces;
209 const auto& extrasIter = extras.find(instance.path);
210
Patrick Venturec7eecc12018-10-21 08:55:44 -0700211 for (const auto& interfaceList : instance.interfaces)
Patrick Venture44a957d2018-10-20 09:41:29 -0700212 {
213 PropertyMap props; // store all the properties
Patrick Venturec7eecc12018-10-21 08:55:44 -0700214 for (const auto& properties : interfaceList.second)
Patrick Venture44a957d2018-10-20 09:41:29 -0700215 {
216 std::string value;
217 decltype(auto) pdata = properties.second;
218
219 if (!pdata.section.empty() && !pdata.property.empty())
220 {
221 value = getFRUValue(pdata.section, pdata.property,
222 pdata.delimiter, fruData);
223 }
224 props.emplace(std::move(properties.first), std::move(value));
225 }
226 // Check and update extra properties
227 if (extras.end() != extrasIter)
228 {
229 const auto& propsIter =
230 (extrasIter->second).find(interfaceList.first);
231 if ((extrasIter->second).end() != propsIter)
232 {
233 for (const auto& map : propsIter->second)
234 {
235 props.emplace(map.first, map.second);
236 }
237 }
238 }
239 interfaces.emplace(std::move(interfaceList.first),
240 std::move(props));
241 }
242
243 // Call the inventory manager
Patrick Venture9a528f22018-10-20 13:31:28 -0700244 sdbusplus::message::object_path objectPath = instance.path;
Patrick Venture44a957d2018-10-20 09:41:29 -0700245 // Check and update extra properties
246 if (extras.end() != extrasIter)
247 {
248 for (const auto& entry : extrasIter->second)
249 {
250 if (interfaces.end() == interfaces.find(entry.first))
251 {
252 interfaces.emplace(entry.first, entry.second);
253 }
254 }
255 }
Patrick Venture9a528f22018-10-20 13:31:28 -0700256 objects.emplace(objectPath, interfaces);
Patrick Venture44a957d2018-10-20 09:41:29 -0700257 }
258
259 auto pimMsg = bus.new_method_call(service.c_str(), path.c_str(),
260 intf.c_str(), "Notify");
261 pimMsg.append(std::move(objects));
262
263 try
264 {
265 auto inventoryMgrResponseMsg = bus.call(pimMsg);
266 }
267 catch (const sdbusplus::exception::SdBusError& ex)
268 {
269 log<level::ERR>("Error in notify call", entry("WHAT=%s", ex.what()));
270 return -1;
271 }
272
273 return rc;
274}
275
Patrick Venture234b7352018-10-20 09:37:09 -0700276} // namespace
277
Vishwa4be4b7a2015-10-31 22:55:50 -0500278//------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600279// Takes the pointer to stream of bytes and length
vishwac93d6d42015-12-16 11:55:16 -0600280// and returns the 8 bit checksum
281// This algo is per IPMI V2.0 spec
Vishwa4be4b7a2015-10-31 22:55:50 -0500282//-------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700283unsigned char calculate_crc(const unsigned char* data, size_t len)
Vishwa4be4b7a2015-10-31 22:55:50 -0500284{
285 char crc = 0;
vishwac93d6d42015-12-16 11:55:16 -0600286 size_t byte = 0;
Vishwa4be4b7a2015-10-31 22:55:50 -0500287
Patrick Venturec9508db2018-10-16 17:18:43 -0700288 for (byte = 0; byte < len; byte++)
Vishwa4be4b7a2015-10-31 22:55:50 -0500289 {
290 crc += *data++;
291 }
vishwaf3ca3522015-12-02 10:35:13 -0600292
Patrick Venturec9508db2018-10-16 17:18:43 -0700293 return (-crc);
Vishwa4be4b7a2015-10-31 22:55:50 -0500294}
295
296//---------------------------------------------------------------------
297// Accepts a fru area offset in commom hdr and tells which area it is.
298//---------------------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600299ipmi_fru_area_type get_fru_area_type(uint8_t area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500300{
301 ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
302
Patrick Venturec9508db2018-10-16 17:18:43 -0700303 switch (area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500304 {
305 case IPMI_FRU_INTERNAL_OFFSET:
306 type = IPMI_FRU_AREA_INTERNAL_USE;
307 break;
308
309 case IPMI_FRU_CHASSIS_OFFSET:
310 type = IPMI_FRU_AREA_CHASSIS_INFO;
311 break;
312
313 case IPMI_FRU_BOARD_OFFSET:
314 type = IPMI_FRU_AREA_BOARD_INFO;
315 break;
316
317 case IPMI_FRU_PRODUCT_OFFSET:
318 type = IPMI_FRU_AREA_PRODUCT_INFO;
319 break;
320
321 case IPMI_FRU_MULTI_OFFSET:
322 type = IPMI_FRU_AREA_MULTI_RECORD;
323 break;
324
325 default:
326 type = IPMI_FRU_AREA_TYPE_MAX;
327 }
328
329 return type;
330}
331
vishwac93d6d42015-12-16 11:55:16 -0600332///-----------------------------------------------
333// Validates the data for crc and mandatory fields
334///-----------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700335int verify_fru_data(const uint8_t* data, const size_t len)
vishwac93d6d42015-12-16 11:55:16 -0600336{
337 uint8_t checksum = 0;
338 int rc = -1;
339
340 // Validate for first byte to always have a value of [1]
Patrick Venturec9508db2018-10-16 17:18:43 -0700341 if (data[0] != IPMI_FRU_HDR_BYTE_ZERO)
vishwac93d6d42015-12-16 11:55:16 -0600342 {
Patrick Venture5c787212018-10-17 13:48:23 -0700343 log<level::ERR>("Invalid entry in byte-0",
344 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600345 return rc;
346 }
347#ifdef __IPMI_DEBUG__
348 else
349 {
Patrick Venture5c787212018-10-17 13:48:23 -0700350 log<level::DEBUG>("Validated in entry_1 of fru_data",
351 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600352 }
353#endif
354
355 // See if the calculated CRC matches with the embedded one.
356 // CRC to be calculated on all except the last one that is CRC itself.
357 checksum = calculate_crc(data, len - 1);
Patrick Venturec9508db2018-10-16 17:18:43 -0700358 if (checksum != data[len - 1])
vishwac93d6d42015-12-16 11:55:16 -0600359 {
360#ifdef __IPMI_DEBUG__
Patrick Venture5c787212018-10-17 13:48:23 -0700361 log<level::ERR>(
362 "Checksum mismatch",
363 entry("Calculated=0x%X", static_cast<uint32_t>(checksum)),
364 entry("Embedded=0x%X", static_cast<uint32_t>(data[len])));
vishwac93d6d42015-12-16 11:55:16 -0600365#endif
366 return rc;
367 }
368#ifdef __IPMI_DEBUG__
369 else
370 {
Patrick Venture5c787212018-10-17 13:48:23 -0700371 log<level::DEBUG>("Checksum matches");
vishwac93d6d42015-12-16 11:55:16 -0600372 }
373#endif
374
375 return EXIT_SUCCESS;
376}
377
vishwac93d6d42015-12-16 11:55:16 -0600378///----------------------------------------------------
379// Checks if a particular fru area is populated or not
380///----------------------------------------------------
Patrick Venture9eb82cf2018-10-20 19:36:01 -0700381bool remove_invalid_area(const std::unique_ptr<IPMIFruArea>& fru_area)
Vishwa4be4b7a2015-10-31 22:55:50 -0500382{
Deepak Kodihalli89ededd2017-02-11 01:59:32 -0600383 // Filter the ones that are empty
Patrick Venturef22b36a2018-10-20 20:59:07 -0700384 if (!(fru_area->getLength()))
vishwac93d6d42015-12-16 11:55:16 -0600385 {
386 return true;
387 }
388 return false;
389}
Vishwa4be4b7a2015-10-31 22:55:50 -0500390
vishwac93d6d42015-12-16 11:55:16 -0600391///----------------------------------------------------------------------------------
392// Populates various FRU areas
393// @prereq : This must be called only after validating common header.
394///----------------------------------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700395int ipmi_populate_fru_areas(uint8_t* fru_data, const size_t data_len,
Patrick Venture19bea9a2018-10-20 19:54:27 -0700396 FruAreaVector& fru_area_vec)
vishwac93d6d42015-12-16 11:55:16 -0600397{
vishwac93d6d42015-12-16 11:55:16 -0600398 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500399
vishwac93d6d42015-12-16 11:55:16 -0600400 // Now walk the common header and see if the file size has atleast the last
401 // offset mentioned by the common_hdr. If the file size is less than the
402 // offset of any if the fru areas mentioned in the common header, then we do
403 // not have a complete file.
Patrick Venturec9508db2018-10-16 17:18:43 -0700404 for (uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
405 fru_entry < (sizeof(struct common_header) - 2); fru_entry++)
vishwac93d6d42015-12-16 11:55:16 -0600406 {
Yi Li75c2d462016-04-11 16:57:46 +0800407 rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600408 // Actual offset in the payload is the offset mentioned in common header
Gunnar Millsc19b8132018-06-14 08:56:17 -0500409 // multiplied by 8. Common header is always the first 8 bytes.
Patrick Venture50ddfe52018-10-16 17:16:32 -0700410 size_t area_offset = fru_data[fru_entry] * IPMI_EIGHT_BYTES;
Patrick Venturec9508db2018-10-16 17:18:43 -0700411 if (area_offset && (data_len < (area_offset + 2)))
vishwac93d6d42015-12-16 11:55:16 -0600412 {
413 // Our file size is less than what it needs to be. +2 because we are
414 // using area len that is at 2 byte off area_offset
Patrick Venture5c787212018-10-17 13:48:23 -0700415 log<level::ERR>("fru file is incomplete",
416 entry("SIZE=%d", data_len));
vishwac93d6d42015-12-16 11:55:16 -0600417 return rc;
418 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700419 else if (area_offset)
vishwac93d6d42015-12-16 11:55:16 -0600420 {
421 // Read 2 bytes to know the actual size of area.
422 uint8_t area_hdr[2] = {0};
Patrick Venture5c787212018-10-17 13:48:23 -0700423 std::memcpy(area_hdr, &((uint8_t*)fru_data)[area_offset],
424 sizeof(area_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500425
vishwac93d6d42015-12-16 11:55:16 -0600426 // Size of this area will be the 2nd byte in the fru area header.
Patrick Venturec9508db2018-10-16 17:18:43 -0700427 size_t area_len = area_hdr[1] * IPMI_EIGHT_BYTES;
vishwac93d6d42015-12-16 11:55:16 -0600428 uint8_t area_data[area_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500429
Patrick Venture5c787212018-10-17 13:48:23 -0700430 log<level::DEBUG>("Fru Data", entry("SIZE=%d", data_len),
431 entry("AREA OFFSET=%d", area_offset),
432 entry("AREA_SIZE=%d", area_len));
Vishwa4be4b7a2015-10-31 22:55:50 -0500433
vishwac93d6d42015-12-16 11:55:16 -0600434 // See if we really have that much buffer. We have area offset amd
435 // from there, the actual len.
Patrick Venturec9508db2018-10-16 17:18:43 -0700436 if (data_len < (area_len + area_offset))
vishwac93d6d42015-12-16 11:55:16 -0600437 {
Patrick Venture5c787212018-10-17 13:48:23 -0700438 log<level::ERR>("Incomplete Fru file",
439 entry("SIZE=%d", data_len));
vishwac93d6d42015-12-16 11:55:16 -0600440 return rc;
441 }
442
443 // Save off the data.
Patrick Venture5c787212018-10-17 13:48:23 -0700444 std::memcpy(area_data, &((uint8_t*)fru_data)[area_offset],
445 area_len);
vishwac93d6d42015-12-16 11:55:16 -0600446
447 // Validate the crc
448 rc = verify_fru_data(area_data, area_len);
Patrick Venturec9508db2018-10-16 17:18:43 -0700449 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600450 {
Patrick Venture5c787212018-10-17 13:48:23 -0700451 log<level::ERR>("Err validating fru area",
452 entry("OFFSET=%d", area_offset));
vishwac93d6d42015-12-16 11:55:16 -0600453 return rc;
454 }
455 else
456 {
Patrick Venture5c787212018-10-17 13:48:23 -0700457 log<level::DEBUG>("Successfully verified area checksum.",
458 entry("OFFSET=%d", area_offset));
vishwac93d6d42015-12-16 11:55:16 -0600459 }
460
461 // We already have a vector that is passed to us containing all
462 // of the fields populated. Update the data portion now.
Patrick Venturec9508db2018-10-16 17:18:43 -0700463 for (auto& iter : fru_area_vec)
vishwac93d6d42015-12-16 11:55:16 -0600464 {
Patrick Venture0e3a1c42018-10-20 21:15:41 -0700465 if (iter->getType() == get_fru_area_type(fru_entry))
vishwac93d6d42015-12-16 11:55:16 -0600466 {
Patrick Venture0e3a1c42018-10-20 21:15:41 -0700467 iter->setData(area_data, area_len);
vishwac93d6d42015-12-16 11:55:16 -0600468 }
469 }
470 } // If we have fru data present
Patrick Venturec9508db2018-10-16 17:18:43 -0700471 } // Walk common_hdr
vishwac93d6d42015-12-16 11:55:16 -0600472
473 // Not all the fields will be populated in a fru data. Mostly all cases will
474 // not have more than 2 or 3.
475 fru_area_vec.erase(std::remove_if(fru_area_vec.begin(), fru_area_vec.end(),
Patrick Venturec9508db2018-10-16 17:18:43 -0700476 remove_invalid_area),
477 fru_area_vec.end());
vishwac93d6d42015-12-16 11:55:16 -0600478
479 return EXIT_SUCCESS;
480}
481
482///---------------------------------------------------------
483// Validates the fru data per ipmi common header constructs.
484// Returns with updated common_hdr and also file_size
485//----------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700486int ipmi_validate_common_hdr(const uint8_t* fru_data, const size_t data_len)
vishwac93d6d42015-12-16 11:55:16 -0600487{
488 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500489
490 uint8_t common_hdr[sizeof(struct common_header)] = {0};
Patrick Venturec9508db2018-10-16 17:18:43 -0700491 if (data_len >= sizeof(common_hdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500492 {
Patrick Venture5c787212018-10-17 13:48:23 -0700493 std::memcpy(common_hdr, fru_data, sizeof(common_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500494 }
495 else
496 {
Patrick Venture5c787212018-10-17 13:48:23 -0700497 log<level::ERR>("Incomplete fru data file", entry("SIZE=%d", data_len));
vishwac93d6d42015-12-16 11:55:16 -0600498 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500499 }
500
vishwac93d6d42015-12-16 11:55:16 -0600501 // Verify the crc and size
502 rc = verify_fru_data(common_hdr, sizeof(common_hdr));
Patrick Venturec9508db2018-10-16 17:18:43 -0700503 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500504 {
Patrick Venture5c787212018-10-17 13:48:23 -0700505 log<level::ERR>("Failed to validate common header");
vishwac93d6d42015-12-16 11:55:16 -0600506 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500507 }
508
vishwac93d6d42015-12-16 11:55:16 -0600509 return EXIT_SUCCESS;
510}
Vishwa4be4b7a2015-10-31 22:55:50 -0500511
Vishwa4be4b7a2015-10-31 22:55:50 -0500512///-----------------------------------------------------
513// Accepts the filename and validates per IPMI FRU spec
514//----------------------------------------------------
Patrick Venture98072dc2018-10-20 09:31:35 -0700515int validateFRUArea(const uint8_t fruid, const char* fru_file_name,
Patrick Venturea8093a22018-10-21 09:07:11 -0700516 sdbusplus::bus::bus& bus, const bool bmc_fru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500517{
vishwac93d6d42015-12-16 11:55:16 -0600518 size_t data_len = 0;
519 size_t bytes_read = 0;
520 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500521
vishwac93d6d42015-12-16 11:55:16 -0600522 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
523 // are not used, keeping it here for completeness.
Patrick Venture19bea9a2018-10-20 19:54:27 -0700524 FruAreaVector fru_area_vec;
Yi Li75c2d462016-04-11 16:57:46 +0800525
Patrick Venturec9508db2018-10-16 17:18:43 -0700526 for (uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
527 fru_entry < (sizeof(struct common_header) - 2); fru_entry++)
vishwac93d6d42015-12-16 11:55:16 -0600528 {
529 // Create an object and push onto a vector.
Patrick Venture9eb82cf2018-10-20 19:36:01 -0700530 std::unique_ptr<IPMIFruArea> fru_area = std::make_unique<IPMIFruArea>(
Patrick Ventured9af7b42018-10-20 19:23:36 -0700531 fruid, get_fru_area_type(fru_entry), bmc_fru);
vishwac93d6d42015-12-16 11:55:16 -0600532
533 // Physically being present
vishwa2f5a3cf2016-05-30 02:25:21 -0500534 bool present = access(fru_file_name, F_OK) == 0;
Patrick Venturef22b36a2018-10-20 20:59:07 -0700535 fru_area->setPresent(present);
vishwac93d6d42015-12-16 11:55:16 -0600536
vishwac93d6d42015-12-16 11:55:16 -0600537 fru_area_vec.emplace_back(std::move(fru_area));
538 }
539
Patrick Venture5c787212018-10-17 13:48:23 -0700540 FILE* fru_fp = std::fopen(fru_file_name, "rb");
Patrick Venturec9508db2018-10-16 17:18:43 -0700541 if (fru_fp == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500542 {
Patrick Venture5c787212018-10-17 13:48:23 -0700543 log<level::ERR>("Unable to open fru file",
544 entry("FILE=%s", fru_file_name),
545 entry("ERRNO=%s", std::strerror(errno)));
Patrick Venture234b7352018-10-20 09:37:09 -0700546 return cleanupError(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500547 }
548
vishwac93d6d42015-12-16 11:55:16 -0600549 // Get the size of the file to see if it meets minimum requirement
Patrick Venture5c787212018-10-17 13:48:23 -0700550 if (std::fseek(fru_fp, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500551 {
Patrick Venture5c787212018-10-17 13:48:23 -0700552 log<level::ERR>("Unable to seek fru file",
553 entry("FILE=%s", fru_file_name),
554 entry("ERRNO=%s", std::strerror(errno)));
Patrick Venture234b7352018-10-20 09:37:09 -0700555 return cleanupError(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500556 }
557
vishwac93d6d42015-12-16 11:55:16 -0600558 // Allocate a buffer to hold entire file content
Patrick Venture5c787212018-10-17 13:48:23 -0700559 data_len = std::ftell(fru_fp);
vishwac93d6d42015-12-16 11:55:16 -0600560 uint8_t fru_data[data_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500561
Patrick Venture5c787212018-10-17 13:48:23 -0700562 std::rewind(fru_fp);
563 bytes_read = std::fread(fru_data, data_len, 1, fru_fp);
Patrick Venturec9508db2018-10-16 17:18:43 -0700564 if (bytes_read != 1)
Vishwa4be4b7a2015-10-31 22:55:50 -0500565 {
Patrick Venture5c787212018-10-17 13:48:23 -0700566 log<level::ERR>("Failed reading fru data.",
567 entry("BYTESREAD=%d", bytes_read),
568 entry("ERRNO=%s", std::strerror(errno)));
Patrick Venture234b7352018-10-20 09:37:09 -0700569 return cleanupError(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500570 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500571
vishwac93d6d42015-12-16 11:55:16 -0600572 // We are done reading.
Patrick Venture5c787212018-10-17 13:48:23 -0700573 std::fclose(fru_fp);
vishwac93d6d42015-12-16 11:55:16 -0600574 fru_fp = NULL;
575
576 rc = ipmi_validate_common_hdr(fru_data, data_len);
Patrick Venturec9508db2018-10-16 17:18:43 -0700577 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500578 {
Patrick Venture234b7352018-10-20 09:37:09 -0700579 return cleanupError(fru_fp, fru_area_vec);
vishwac93d6d42015-12-16 11:55:16 -0600580 }
581
Patrick Venturec9508db2018-10-16 17:18:43 -0700582 // Now that we validated the common header, populate various fru sections if
583 // we have them here.
vishwac93d6d42015-12-16 11:55:16 -0600584 rc = ipmi_populate_fru_areas(fru_data, data_len, fru_area_vec);
Patrick Venturec9508db2018-10-16 17:18:43 -0700585 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600586 {
Patrick Venture5c787212018-10-17 13:48:23 -0700587 log<level::ERR>("Populating FRU areas failed", entry("FRU=%d", fruid));
Patrick Venture234b7352018-10-20 09:37:09 -0700588 return cleanupError(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500589 }
590 else
591 {
Patrick Venture5c787212018-10-17 13:48:23 -0700592 log<level::DEBUG>("Populated FRU areas",
593 entry("FILE=%s", fru_file_name));
Vishwa4be4b7a2015-10-31 22:55:50 -0500594 }
595
vishwac93d6d42015-12-16 11:55:16 -0600596#ifdef __IPMI_DEBUG__
Patrick Venturec7eecc12018-10-21 08:55:44 -0700597 for (const auto& iter : fru_area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500598 {
Patrick Venture0e3a1c42018-10-20 21:15:41 -0700599 std::printf("FRU ID : [%d]\n", iter->getFruID());
600 std::printf("AREA NAME : [%s]\n", iter->getName());
601 std::printf("TYPE : [%d]\n", iter->getType());
602 std::printf("LEN : [%d]\n", iter->getLength());
603 std::printf("BUS NAME : [%s]\n", iter->getBusName());
604 std::printf("OBJ PATH : [%s]\n", iter->getObjectPath());
605 std::printf("INTF NAME :[%s]\n", iter->getIntfName());
Vishwa4be4b7a2015-10-31 22:55:50 -0500606 }
vishwac93d6d42015-12-16 11:55:16 -0600607#endif
608
609 // If the vector is populated with everything, then go ahead and update the
610 // inventory.
Patrick Venturec9508db2018-10-16 17:18:43 -0700611 if (!(fru_area_vec.empty()))
vishwac93d6d42015-12-16 11:55:16 -0600612 {
613
614#ifdef __IPMI_DEBUG__
Patrick Venture5c787212018-10-17 13:48:23 -0700615 std::printf("\n SIZE of vector is : [%d] \n", fru_area_vec.size());
vishwac93d6d42015-12-16 11:55:16 -0600616#endif
Patrick Venturea8093a22018-10-21 09:07:11 -0700617 rc = updateInventory(fru_area_vec, bus);
Patrick Venturec9508db2018-10-16 17:18:43 -0700618 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600619 {
Patrick Venture5c787212018-10-17 13:48:23 -0700620 log<level::ERR>("Error updating inventory.");
vishwac93d6d42015-12-16 11:55:16 -0600621 }
622 }
623
624 // we are done with all that we wanted to do. This will do the job of
625 // calling any destructors too.
626 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500627
628 return rc;
629}