blob: d9de3fec38e250fdbafc930518747c91463cf726 [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>
Chris Austenb45c4cb2015-11-01 06:34:56 -06008#include <unistd.h>
Patrick Venturec9508db2018-10-16 17:18:43 -07009
10#include <algorithm>
Patrick Venture5c787212018-10-17 13:48:23 -070011#include <cstdio>
12#include <cstring>
Patrick Venturec9508db2018-10-16 17:18:43 -070013#include <exception>
14#include <fstream>
vishwac93d6d42015-12-16 11:55:16 -060015#include <iostream>
Patrick Ventureb390c0e2018-10-20 13:36:48 -070016#include <map>
vishwac93d6d42015-12-16 11:55:16 -060017#include <memory>
Patrick Venture5739ac32018-10-16 19:17:41 -070018#include <phosphor-logging/log.hpp>
Patrick Venture71918492018-10-20 19:40:13 -070019#include <sdbusplus/bus.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 Venture19bea9a2018-10-20 19:54:27 -070029using FruAreaVector = std::vector<std::unique_ptr<IPMIFruArea>>;
30
Patrick Venture234b7352018-10-20 09:37:09 -070031namespace
32{
33
34//------------------------------------------------------------
35// Cleanup routine
Patrick Ventureef83b992018-10-21 09:32:44 -070036// Must always be called as last reference to fruFilePointer.
Patrick Venture234b7352018-10-20 09:37:09 -070037//------------------------------------------------------------
Patrick Ventureef83b992018-10-21 09:32:44 -070038int cleanupError(FILE* fruFilePointer, FruAreaVector& fruAreaVec)
Patrick Venture234b7352018-10-20 09:37:09 -070039{
Patrick Ventureef83b992018-10-21 09:32:44 -070040 if (fruFilePointer != NULL)
Patrick Venture234b7352018-10-20 09:37:09 -070041 {
Patrick Ventureef83b992018-10-21 09:32:44 -070042 std::fclose(fruFilePointer);
Patrick Venture234b7352018-10-20 09:37:09 -070043 }
44
Patrick Ventureef83b992018-10-21 09:32:44 -070045 if (!(fruAreaVec.empty()))
Patrick Venture234b7352018-10-20 09:37:09 -070046 {
Patrick Ventureef83b992018-10-21 09:32:44 -070047 fruAreaVec.clear();
Patrick Venture234b7352018-10-20 09:37:09 -070048 }
49
50 return -1;
51}
52
Patrick Venturede8ea562018-10-20 09:44:19 -070053//------------------------------------------------------------------------
54// Gets the value of the key from the fru dictionary of the given section.
55// FRU dictionary is parsed fru data for all the sections.
56//------------------------------------------------------------------------
57std::string getFRUValue(const std::string& section, const std::string& key,
58 const std::string& delimiter, IPMIFruInfo& fruData)
59{
60
61 auto minIndexValue = 0;
62 auto maxIndexValue = 0;
63 std::string fruValue = "";
64
65 if (section == "Board")
66 {
67 minIndexValue = OPENBMC_VPD_KEY_BOARD_MFG_DATE;
68 maxIndexValue = OPENBMC_VPD_KEY_BOARD_MAX;
69 }
70 else if (section == "Product")
71 {
72 minIndexValue = OPENBMC_VPD_KEY_PRODUCT_MFR;
73 maxIndexValue = OPENBMC_VPD_KEY_PRODUCT_MAX;
74 }
75 else if (section == "Chassis")
76 {
77 minIndexValue = OPENBMC_VPD_KEY_CHASSIS_TYPE;
78 maxIndexValue = OPENBMC_VPD_KEY_CHASSIS_MAX;
79 }
80
81 auto first = fruData.cbegin() + minIndexValue;
82 auto last = first + (maxIndexValue - minIndexValue) + 1;
83
Patrick Venturec7eecc12018-10-21 08:55:44 -070084 auto itr = std::find_if(first, last,
85 [&key](const auto& e) { return key == e.first; });
Patrick Venturede8ea562018-10-20 09:44:19 -070086
87 if (itr != last)
88 {
89 fruValue = itr->second;
90 }
91
92 // if the key is custom property then the value could be in two formats.
93 // 1) custom field 2 = "value".
94 // 2) custom field 2 = "key:value".
95 // if delimiter length = 0 i.e custom field 2 = "value"
96
97 constexpr auto customProp = "Custom Field";
98 if (key.find(customProp) != std::string::npos)
99 {
100 if (delimiter.length() > 0)
101 {
102 size_t delimiterpos = fruValue.find(delimiter);
103 if (delimiterpos != std::string::npos)
104 fruValue = fruValue.substr(delimiterpos + 1);
105 }
106 }
107 return fruValue;
108}
109
Patrick Venture17baa272018-10-20 09:48:08 -0700110// Get the inventory service from the mapper.
111auto getService(sdbusplus::bus::bus& bus, const std::string& intf,
112 const std::string& path)
113{
114 auto mapperCall =
115 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
116 "/xyz/openbmc_project/object_mapper",
117 "xyz.openbmc_project.ObjectMapper", "GetObject");
118
119 mapperCall.append(path);
120 mapperCall.append(std::vector<std::string>({intf}));
121 std::map<std::string, std::vector<std::string>> mapperResponse;
122
123 try
124 {
125 auto mapperResponseMsg = bus.call(mapperCall);
126 mapperResponseMsg.read(mapperResponse);
127 }
128 catch (const sdbusplus::exception::SdBusError& ex)
129 {
130 log<level::ERR>("Exception from sdbus call",
131 entry("WHAT=%s", ex.what()));
132 throw;
133 }
134
135 if (mapperResponse.begin() == mapperResponse.end())
136 {
137 throw std::runtime_error("ERROR in reading the mapper response");
138 }
139
140 return mapperResponse.begin()->first;
141}
142
Patrick Venture44a957d2018-10-20 09:41:29 -0700143// Takes FRU data, invokes Parser for each fru record area and updates
144// Inventory
145//------------------------------------------------------------------------
Patrick Ventureef83b992018-10-21 09:32:44 -0700146int updateInventory(FruAreaVector& areaVector, sdbusplus::bus::bus& bus)
Patrick Venture44a957d2018-10-20 09:41:29 -0700147{
148 // Generic error reporter
149 int rc = 0;
150 uint8_t fruid = 0;
151 IPMIFruInfo fruData;
152
153 // For each FRU area, extract the needed data , get it parsed and update
154 // the Inventory.
Patrick Ventureef83b992018-10-21 09:32:44 -0700155 for (const auto& fruArea : areaVector)
Patrick Venture44a957d2018-10-20 09:41:29 -0700156 {
Patrick Venturef22b36a2018-10-20 20:59:07 -0700157 fruid = fruArea->getFruID();
Patrick Venture44a957d2018-10-20 09:41:29 -0700158 // Fill the container with information
Patrick Venturef22b36a2018-10-20 20:59:07 -0700159 rc = parse_fru_area((fruArea)->getType(), (void*)(fruArea)->getData(),
160 (fruArea)->getLength(), fruData);
Patrick Venture44a957d2018-10-20 09:41:29 -0700161 if (rc < 0)
162 {
163 log<level::ERR>("Error parsing FRU records");
164 return rc;
165 }
166 } // END walking the vector of areas and updating
167
168 // For each Fru we have the list of instances which needs to be updated.
169 // Each instance object implements certain interfaces.
170 // Each Interface is having Dbus properties.
171 // Each Dbus Property would be having metaData(eg section,VpdPropertyName).
172
173 // Here we are just printing the object,interface and the properties.
174 // which needs to be called with the new inventory manager implementation.
Patrick Venture44a957d2018-10-20 09:41:29 -0700175 using namespace std::string_literals;
176 static const auto intf = "xyz.openbmc_project.Inventory.Manager"s;
177 static const auto path = "/xyz/openbmc_project/inventory"s;
178 std::string service;
179 try
180 {
181 service = getService(bus, intf, path);
182 }
183 catch (const std::exception& e)
184 {
185 std::cerr << e.what() << "\n";
186 return -1;
187 }
188
189 auto iter = frus.find(fruid);
190 if (iter == frus.end())
191 {
192 log<level::ERR>("Unable to get the fru info",
193 entry("FRU=%d", static_cast<int>(fruid)));
194 return -1;
195 }
196
197 auto& instanceList = iter->second;
198 if (instanceList.size() <= 0)
199 {
200 log<level::DEBUG>("Object list empty for this FRU",
201 entry("FRU=%d", static_cast<int>(fruid)));
202 }
203
204 ObjectMap objects;
Patrick Venturec7eecc12018-10-21 08:55:44 -0700205 for (const auto& instance : instanceList)
Patrick Venture44a957d2018-10-20 09:41:29 -0700206 {
207 InterfaceMap interfaces;
208 const auto& extrasIter = extras.find(instance.path);
209
Patrick Venturec7eecc12018-10-21 08:55:44 -0700210 for (const auto& interfaceList : instance.interfaces)
Patrick Venture44a957d2018-10-20 09:41:29 -0700211 {
212 PropertyMap props; // store all the properties
Patrick Venturec7eecc12018-10-21 08:55:44 -0700213 for (const auto& properties : interfaceList.second)
Patrick Venture44a957d2018-10-20 09:41:29 -0700214 {
215 std::string value;
216 decltype(auto) pdata = properties.second;
217
218 if (!pdata.section.empty() && !pdata.property.empty())
219 {
220 value = getFRUValue(pdata.section, pdata.property,
221 pdata.delimiter, fruData);
222 }
223 props.emplace(std::move(properties.first), std::move(value));
224 }
225 // Check and update extra properties
226 if (extras.end() != extrasIter)
227 {
228 const auto& propsIter =
229 (extrasIter->second).find(interfaceList.first);
230 if ((extrasIter->second).end() != propsIter)
231 {
232 for (const auto& map : propsIter->second)
233 {
234 props.emplace(map.first, map.second);
235 }
236 }
237 }
238 interfaces.emplace(std::move(interfaceList.first),
239 std::move(props));
240 }
241
242 // Call the inventory manager
Patrick Venture9a528f22018-10-20 13:31:28 -0700243 sdbusplus::message::object_path objectPath = instance.path;
Patrick Venture44a957d2018-10-20 09:41:29 -0700244 // Check and update extra properties
245 if (extras.end() != extrasIter)
246 {
247 for (const auto& entry : extrasIter->second)
248 {
249 if (interfaces.end() == interfaces.find(entry.first))
250 {
251 interfaces.emplace(entry.first, entry.second);
252 }
253 }
254 }
Patrick Venture9a528f22018-10-20 13:31:28 -0700255 objects.emplace(objectPath, interfaces);
Patrick Venture44a957d2018-10-20 09:41:29 -0700256 }
257
258 auto pimMsg = bus.new_method_call(service.c_str(), path.c_str(),
259 intf.c_str(), "Notify");
260 pimMsg.append(std::move(objects));
261
262 try
263 {
264 auto inventoryMgrResponseMsg = bus.call(pimMsg);
265 }
266 catch (const sdbusplus::exception::SdBusError& ex)
267 {
268 log<level::ERR>("Error in notify call", entry("WHAT=%s", ex.what()));
269 return -1;
270 }
271
272 return rc;
273}
274
Patrick Venture234b7352018-10-20 09:37:09 -0700275} // namespace
276
Vishwa4be4b7a2015-10-31 22:55:50 -0500277//------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600278// Takes the pointer to stream of bytes and length
vishwac93d6d42015-12-16 11:55:16 -0600279// and returns the 8 bit checksum
280// This algo is per IPMI V2.0 spec
Vishwa4be4b7a2015-10-31 22:55:50 -0500281//-------------------------------------------------
Patrick Venture062e1452018-10-21 09:16:47 -0700282unsigned char calculateCRC(const unsigned char* data, size_t len)
Vishwa4be4b7a2015-10-31 22:55:50 -0500283{
284 char crc = 0;
vishwac93d6d42015-12-16 11:55:16 -0600285 size_t byte = 0;
Vishwa4be4b7a2015-10-31 22:55:50 -0500286
Patrick Venturec9508db2018-10-16 17:18:43 -0700287 for (byte = 0; byte < len; byte++)
Vishwa4be4b7a2015-10-31 22:55:50 -0500288 {
289 crc += *data++;
290 }
vishwaf3ca3522015-12-02 10:35:13 -0600291
Patrick Venturec9508db2018-10-16 17:18:43 -0700292 return (-crc);
Vishwa4be4b7a2015-10-31 22:55:50 -0500293}
294
295//---------------------------------------------------------------------
296// Accepts a fru area offset in commom hdr and tells which area it is.
297//---------------------------------------------------------------------
Patrick Ventureef83b992018-10-21 09:32:44 -0700298ipmi_fru_area_type getFruAreaType(uint8_t areaOffset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500299{
300 ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
301
Patrick Ventureef83b992018-10-21 09:32:44 -0700302 switch (areaOffset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500303 {
304 case IPMI_FRU_INTERNAL_OFFSET:
305 type = IPMI_FRU_AREA_INTERNAL_USE;
306 break;
307
308 case IPMI_FRU_CHASSIS_OFFSET:
309 type = IPMI_FRU_AREA_CHASSIS_INFO;
310 break;
311
312 case IPMI_FRU_BOARD_OFFSET:
313 type = IPMI_FRU_AREA_BOARD_INFO;
314 break;
315
316 case IPMI_FRU_PRODUCT_OFFSET:
317 type = IPMI_FRU_AREA_PRODUCT_INFO;
318 break;
319
320 case IPMI_FRU_MULTI_OFFSET:
321 type = IPMI_FRU_AREA_MULTI_RECORD;
322 break;
323
324 default:
325 type = IPMI_FRU_AREA_TYPE_MAX;
326 }
327
328 return type;
329}
330
vishwac93d6d42015-12-16 11:55:16 -0600331///-----------------------------------------------
332// Validates the data for crc and mandatory fields
333///-----------------------------------------------
Patrick Venture062e1452018-10-21 09:16:47 -0700334int verifyFruData(const uint8_t* data, const size_t len)
vishwac93d6d42015-12-16 11:55:16 -0600335{
336 uint8_t checksum = 0;
337 int rc = -1;
338
339 // Validate for first byte to always have a value of [1]
Patrick Venturec9508db2018-10-16 17:18:43 -0700340 if (data[0] != IPMI_FRU_HDR_BYTE_ZERO)
vishwac93d6d42015-12-16 11:55:16 -0600341 {
Patrick Venture5c787212018-10-17 13:48:23 -0700342 log<level::ERR>("Invalid entry in byte-0",
343 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600344 return rc;
345 }
346#ifdef __IPMI_DEBUG__
347 else
348 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700349 log<level::DEBUG>("Validated in entry_1 of fruData",
Patrick Venture5c787212018-10-17 13:48:23 -0700350 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600351 }
352#endif
353
354 // See if the calculated CRC matches with the embedded one.
355 // CRC to be calculated on all except the last one that is CRC itself.
Patrick Venture062e1452018-10-21 09:16:47 -0700356 checksum = calculateCRC(data, len - 1);
Patrick Venturec9508db2018-10-16 17:18:43 -0700357 if (checksum != data[len - 1])
vishwac93d6d42015-12-16 11:55:16 -0600358 {
359#ifdef __IPMI_DEBUG__
Patrick Venture5c787212018-10-17 13:48:23 -0700360 log<level::ERR>(
361 "Checksum mismatch",
362 entry("Calculated=0x%X", static_cast<uint32_t>(checksum)),
363 entry("Embedded=0x%X", static_cast<uint32_t>(data[len])));
vishwac93d6d42015-12-16 11:55:16 -0600364#endif
365 return rc;
366 }
367#ifdef __IPMI_DEBUG__
368 else
369 {
Patrick Venture5c787212018-10-17 13:48:23 -0700370 log<level::DEBUG>("Checksum matches");
vishwac93d6d42015-12-16 11:55:16 -0600371 }
372#endif
373
374 return EXIT_SUCCESS;
375}
376
vishwac93d6d42015-12-16 11:55:16 -0600377///----------------------------------------------------
378// Checks if a particular fru area is populated or not
379///----------------------------------------------------
Patrick Ventureef83b992018-10-21 09:32:44 -0700380bool removeInvalidArea(const std::unique_ptr<IPMIFruArea>& fruArea)
Vishwa4be4b7a2015-10-31 22:55:50 -0500381{
Deepak Kodihalli89ededd2017-02-11 01:59:32 -0600382 // Filter the ones that are empty
Patrick Ventureef83b992018-10-21 09:32:44 -0700383 if (!(fruArea->getLength()))
vishwac93d6d42015-12-16 11:55:16 -0600384 {
385 return true;
386 }
387 return false;
388}
Vishwa4be4b7a2015-10-31 22:55:50 -0500389
vishwac93d6d42015-12-16 11:55:16 -0600390///----------------------------------------------------------------------------------
391// Populates various FRU areas
392// @prereq : This must be called only after validating common header.
393///----------------------------------------------------------------------------------
Patrick Ventureef83b992018-10-21 09:32:44 -0700394int ipmiPopulateFruAreas(uint8_t* fruData, const size_t dataLen,
395 FruAreaVector& fruAreaVec)
vishwac93d6d42015-12-16 11:55:16 -0600396{
vishwac93d6d42015-12-16 11:55:16 -0600397 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500398
vishwac93d6d42015-12-16 11:55:16 -0600399 // Now walk the common header and see if the file size has atleast the last
Patrick Ventureef83b992018-10-21 09:32:44 -0700400 // offset mentioned by the struct common_header. If the file size is less
401 // than the offset of any if the fru areas mentioned in the common header,
402 // then we do not have a complete file.
403 for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET;
404 fruEntry < (sizeof(struct common_header) - 2); fruEntry++)
vishwac93d6d42015-12-16 11:55:16 -0600405 {
Yi Li75c2d462016-04-11 16:57:46 +0800406 rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600407 // Actual offset in the payload is the offset mentioned in common header
Gunnar Millsc19b8132018-06-14 08:56:17 -0500408 // multiplied by 8. Common header is always the first 8 bytes.
Patrick Ventureef83b992018-10-21 09:32:44 -0700409 size_t areaOffset = fruData[fruEntry] * IPMI_EIGHT_BYTES;
410 if (areaOffset && (dataLen < (areaOffset + 2)))
vishwac93d6d42015-12-16 11:55:16 -0600411 {
412 // Our file size is less than what it needs to be. +2 because we are
Patrick Ventureef83b992018-10-21 09:32:44 -0700413 // using area len that is at 2 byte off areaOffset
Patrick Venture5c787212018-10-17 13:48:23 -0700414 log<level::ERR>("fru file is incomplete",
Patrick Ventureef83b992018-10-21 09:32:44 -0700415 entry("SIZE=%d", dataLen));
vishwac93d6d42015-12-16 11:55:16 -0600416 return rc;
417 }
Patrick Ventureef83b992018-10-21 09:32:44 -0700418 else if (areaOffset)
vishwac93d6d42015-12-16 11:55:16 -0600419 {
420 // Read 2 bytes to know the actual size of area.
Patrick Ventureef83b992018-10-21 09:32:44 -0700421 uint8_t areaHeader[2] = {0};
422 std::memcpy(areaHeader, &((uint8_t*)fruData)[areaOffset],
423 sizeof(areaHeader));
Vishwa4be4b7a2015-10-31 22:55:50 -0500424
vishwac93d6d42015-12-16 11:55:16 -0600425 // Size of this area will be the 2nd byte in the fru area header.
Patrick Ventureef83b992018-10-21 09:32:44 -0700426 size_t areaLen = areaHeader[1] * IPMI_EIGHT_BYTES;
427 uint8_t areaData[areaLen] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500428
Patrick Ventureef83b992018-10-21 09:32:44 -0700429 log<level::DEBUG>("Fru Data", entry("SIZE=%d", dataLen),
430 entry("AREA OFFSET=%d", areaOffset),
431 entry("AREA_SIZE=%d", areaLen));
Vishwa4be4b7a2015-10-31 22:55:50 -0500432
vishwac93d6d42015-12-16 11:55:16 -0600433 // See if we really have that much buffer. We have area offset amd
434 // from there, the actual len.
Patrick Ventureef83b992018-10-21 09:32:44 -0700435 if (dataLen < (areaLen + areaOffset))
vishwac93d6d42015-12-16 11:55:16 -0600436 {
Patrick Venture5c787212018-10-17 13:48:23 -0700437 log<level::ERR>("Incomplete Fru file",
Patrick Ventureef83b992018-10-21 09:32:44 -0700438 entry("SIZE=%d", dataLen));
vishwac93d6d42015-12-16 11:55:16 -0600439 return rc;
440 }
441
442 // Save off the data.
Patrick Ventureef83b992018-10-21 09:32:44 -0700443 std::memcpy(areaData, &((uint8_t*)fruData)[areaOffset], areaLen);
vishwac93d6d42015-12-16 11:55:16 -0600444
445 // Validate the crc
Patrick Ventureef83b992018-10-21 09:32:44 -0700446 rc = verifyFruData(areaData, areaLen);
Patrick Venturec9508db2018-10-16 17:18:43 -0700447 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600448 {
Patrick Venture5c787212018-10-17 13:48:23 -0700449 log<level::ERR>("Err validating fru area",
Patrick Ventureef83b992018-10-21 09:32:44 -0700450 entry("OFFSET=%d", areaOffset));
vishwac93d6d42015-12-16 11:55:16 -0600451 return rc;
452 }
453 else
454 {
Patrick Venture5c787212018-10-17 13:48:23 -0700455 log<level::DEBUG>("Successfully verified area checksum.",
Patrick Ventureef83b992018-10-21 09:32:44 -0700456 entry("OFFSET=%d", areaOffset));
vishwac93d6d42015-12-16 11:55:16 -0600457 }
458
459 // We already have a vector that is passed to us containing all
460 // of the fields populated. Update the data portion now.
Patrick Ventureef83b992018-10-21 09:32:44 -0700461 for (auto& iter : fruAreaVec)
vishwac93d6d42015-12-16 11:55:16 -0600462 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700463 if (iter->getType() == getFruAreaType(fruEntry))
vishwac93d6d42015-12-16 11:55:16 -0600464 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700465 iter->setData(areaData, areaLen);
vishwac93d6d42015-12-16 11:55:16 -0600466 }
467 }
468 } // If we have fru data present
Patrick Ventureef83b992018-10-21 09:32:44 -0700469 } // Walk struct common_header
vishwac93d6d42015-12-16 11:55:16 -0600470
471 // Not all the fields will be populated in a fru data. Mostly all cases will
472 // not have more than 2 or 3.
Patrick Ventureef83b992018-10-21 09:32:44 -0700473 fruAreaVec.erase(
474 std::remove_if(fruAreaVec.begin(), fruAreaVec.end(), removeInvalidArea),
475 fruAreaVec.end());
vishwac93d6d42015-12-16 11:55:16 -0600476
477 return EXIT_SUCCESS;
478}
479
480///---------------------------------------------------------
481// Validates the fru data per ipmi common header constructs.
Patrick Ventureef83b992018-10-21 09:32:44 -0700482// Returns with updated struct common_header and also file_size
vishwac93d6d42015-12-16 11:55:16 -0600483//----------------------------------------------------------
Patrick Ventureef83b992018-10-21 09:32:44 -0700484int ipmiValidateCommonHeader(const uint8_t* fruData, const size_t dataLen)
vishwac93d6d42015-12-16 11:55:16 -0600485{
486 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500487
Patrick Ventureef83b992018-10-21 09:32:44 -0700488 uint8_t commonHdr[sizeof(struct common_header)] = {0};
489 if (dataLen >= sizeof(commonHdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500490 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700491 std::memcpy(commonHdr, fruData, sizeof(commonHdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500492 }
493 else
494 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700495 log<level::ERR>("Incomplete fru data file", entry("SIZE=%d", dataLen));
vishwac93d6d42015-12-16 11:55:16 -0600496 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500497 }
498
vishwac93d6d42015-12-16 11:55:16 -0600499 // Verify the crc and size
Patrick Ventureef83b992018-10-21 09:32:44 -0700500 rc = verifyFruData(commonHdr, sizeof(commonHdr));
Patrick Venturec9508db2018-10-16 17:18:43 -0700501 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500502 {
Patrick Venture5c787212018-10-17 13:48:23 -0700503 log<level::ERR>("Failed to validate common header");
vishwac93d6d42015-12-16 11:55:16 -0600504 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500505 }
506
vishwac93d6d42015-12-16 11:55:16 -0600507 return EXIT_SUCCESS;
508}
Vishwa4be4b7a2015-10-31 22:55:50 -0500509
Vishwa4be4b7a2015-10-31 22:55:50 -0500510///-----------------------------------------------------
511// Accepts the filename and validates per IPMI FRU spec
512//----------------------------------------------------
Patrick Ventureef83b992018-10-21 09:32:44 -0700513int validateFRUArea(const uint8_t fruid, const char* fruFilename,
514 sdbusplus::bus::bus& bus, const bool bmcOnlyFru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500515{
Patrick Ventureef83b992018-10-21 09:32:44 -0700516 size_t dataLen = 0;
517 size_t bytesRead = 0;
vishwac93d6d42015-12-16 11:55:16 -0600518 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500519
vishwac93d6d42015-12-16 11:55:16 -0600520 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
521 // are not used, keeping it here for completeness.
Patrick Ventureef83b992018-10-21 09:32:44 -0700522 FruAreaVector fruAreaVec;
Yi Li75c2d462016-04-11 16:57:46 +0800523
Patrick Ventureef83b992018-10-21 09:32:44 -0700524 for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET;
525 fruEntry < (sizeof(struct common_header) - 2); fruEntry++)
vishwac93d6d42015-12-16 11:55:16 -0600526 {
527 // Create an object and push onto a vector.
Patrick Ventureef83b992018-10-21 09:32:44 -0700528 std::unique_ptr<IPMIFruArea> fruArea = std::make_unique<IPMIFruArea>(
529 fruid, getFruAreaType(fruEntry), bmcOnlyFru);
vishwac93d6d42015-12-16 11:55:16 -0600530
531 // Physically being present
Patrick Ventureef83b992018-10-21 09:32:44 -0700532 bool present = access(fruFilename, F_OK) == 0;
533 fruArea->setPresent(present);
vishwac93d6d42015-12-16 11:55:16 -0600534
Patrick Ventureef83b992018-10-21 09:32:44 -0700535 fruAreaVec.emplace_back(std::move(fruArea));
vishwac93d6d42015-12-16 11:55:16 -0600536 }
537
Patrick Ventureef83b992018-10-21 09:32:44 -0700538 FILE* fruFilePointer = std::fopen(fruFilename, "rb");
539 if (fruFilePointer == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500540 {
Patrick Venture5c787212018-10-17 13:48:23 -0700541 log<level::ERR>("Unable to open fru file",
Patrick Ventureef83b992018-10-21 09:32:44 -0700542 entry("FILE=%s", fruFilename),
Patrick Venture5c787212018-10-17 13:48:23 -0700543 entry("ERRNO=%s", std::strerror(errno)));
Patrick Ventureef83b992018-10-21 09:32:44 -0700544 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500545 }
546
vishwac93d6d42015-12-16 11:55:16 -0600547 // Get the size of the file to see if it meets minimum requirement
Patrick Ventureef83b992018-10-21 09:32:44 -0700548 if (std::fseek(fruFilePointer, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500549 {
Patrick Venture5c787212018-10-17 13:48:23 -0700550 log<level::ERR>("Unable to seek fru file",
Patrick Ventureef83b992018-10-21 09:32:44 -0700551 entry("FILE=%s", fruFilename),
Patrick Venture5c787212018-10-17 13:48:23 -0700552 entry("ERRNO=%s", std::strerror(errno)));
Patrick Ventureef83b992018-10-21 09:32:44 -0700553 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500554 }
555
vishwac93d6d42015-12-16 11:55:16 -0600556 // Allocate a buffer to hold entire file content
Patrick Ventureef83b992018-10-21 09:32:44 -0700557 dataLen = std::ftell(fruFilePointer);
558 uint8_t fruData[dataLen] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500559
Patrick Ventureef83b992018-10-21 09:32:44 -0700560 std::rewind(fruFilePointer);
561 bytesRead = std::fread(fruData, dataLen, 1, fruFilePointer);
562 if (bytesRead != 1)
Vishwa4be4b7a2015-10-31 22:55:50 -0500563 {
Patrick Venture5c787212018-10-17 13:48:23 -0700564 log<level::ERR>("Failed reading fru data.",
Patrick Ventureef83b992018-10-21 09:32:44 -0700565 entry("BYTESREAD=%d", bytesRead),
Patrick Venture5c787212018-10-17 13:48:23 -0700566 entry("ERRNO=%s", std::strerror(errno)));
Patrick Ventureef83b992018-10-21 09:32:44 -0700567 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500568 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500569
vishwac93d6d42015-12-16 11:55:16 -0600570 // We are done reading.
Patrick Ventureef83b992018-10-21 09:32:44 -0700571 std::fclose(fruFilePointer);
572 fruFilePointer = NULL;
vishwac93d6d42015-12-16 11:55:16 -0600573
Patrick Ventureef83b992018-10-21 09:32:44 -0700574 rc = ipmiValidateCommonHeader(fruData, dataLen);
Patrick Venturec9508db2018-10-16 17:18:43 -0700575 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500576 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700577 return cleanupError(fruFilePointer, fruAreaVec);
vishwac93d6d42015-12-16 11:55:16 -0600578 }
579
Patrick Venturec9508db2018-10-16 17:18:43 -0700580 // Now that we validated the common header, populate various fru sections if
581 // we have them here.
Patrick Ventureef83b992018-10-21 09:32:44 -0700582 rc = ipmiPopulateFruAreas(fruData, dataLen, fruAreaVec);
Patrick Venturec9508db2018-10-16 17:18:43 -0700583 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600584 {
Patrick Venture5c787212018-10-17 13:48:23 -0700585 log<level::ERR>("Populating FRU areas failed", entry("FRU=%d", fruid));
Patrick Ventureef83b992018-10-21 09:32:44 -0700586 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500587 }
588 else
589 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700590 log<level::DEBUG>("Populated FRU areas", entry("FILE=%s", fruFilename));
Vishwa4be4b7a2015-10-31 22:55:50 -0500591 }
592
vishwac93d6d42015-12-16 11:55:16 -0600593#ifdef __IPMI_DEBUG__
Patrick Ventureef83b992018-10-21 09:32:44 -0700594 for (const auto& iter : fruAreaVec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500595 {
Patrick Venture0e3a1c42018-10-20 21:15:41 -0700596 std::printf("FRU ID : [%d]\n", iter->getFruID());
597 std::printf("AREA NAME : [%s]\n", iter->getName());
598 std::printf("TYPE : [%d]\n", iter->getType());
599 std::printf("LEN : [%d]\n", iter->getLength());
600 std::printf("BUS NAME : [%s]\n", iter->getBusName());
601 std::printf("OBJ PATH : [%s]\n", iter->getObjectPath());
602 std::printf("INTF NAME :[%s]\n", iter->getIntfName());
Vishwa4be4b7a2015-10-31 22:55:50 -0500603 }
vishwac93d6d42015-12-16 11:55:16 -0600604#endif
605
606 // If the vector is populated with everything, then go ahead and update the
607 // inventory.
Patrick Ventureef83b992018-10-21 09:32:44 -0700608 if (!(fruAreaVec.empty()))
vishwac93d6d42015-12-16 11:55:16 -0600609 {
610
611#ifdef __IPMI_DEBUG__
Patrick Ventureef83b992018-10-21 09:32:44 -0700612 std::printf("\n SIZE of vector is : [%d] \n", fruAreaVec.size());
vishwac93d6d42015-12-16 11:55:16 -0600613#endif
Patrick Ventureef83b992018-10-21 09:32:44 -0700614 rc = updateInventory(fruAreaVec, bus);
Patrick Venturec9508db2018-10-16 17:18:43 -0700615 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600616 {
Patrick Venture5c787212018-10-17 13:48:23 -0700617 log<level::ERR>("Error updating inventory.");
vishwac93d6d42015-12-16 11:55:16 -0600618 }
619 }
620
621 // we are done with all that we wanted to do. This will do the job of
622 // calling any destructors too.
Patrick Ventureef83b992018-10-21 09:32:44 -0700623 fruAreaVec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500624
625 return rc;
626}