blob: 116f16878abe3d13161137c2b855d7a07646dd27 [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
William A. Kennington III8ab57842019-02-12 12:49:51 -08007#include <ipmid/api.h>
Chris Austenb45c4cb2015-11-01 06:34:56 -06008#include <unistd.h>
Patrick Venturec9508db2018-10-16 17:18:43 -07009
Patrick Williamscfa96af2023-05-10 07:50:26 -050010#include <phosphor-logging/log.hpp>
11#include <sdbusplus/bus.hpp>
12
Patrick Venturec9508db2018-10-16 17:18:43 -070013#include <algorithm>
Patrick Williamsbd2d8692023-07-19 10:59:27 -050014#include <array>
Patrick Venture5c787212018-10-17 13:48:23 -070015#include <cstdio>
16#include <cstring>
Patrick Venturec9508db2018-10-16 17:18:43 -070017#include <exception>
18#include <fstream>
vishwac93d6d42015-12-16 11:55:16 -060019#include <iostream>
Patrick Ventureb390c0e2018-10-20 13:36:48 -070020#include <map>
vishwac93d6d42015-12-16 11:55:16 -060021#include <memory>
Patrick Williamsbd2d8692023-07-19 10:59:27 -050022#include <span>
Patrick Venturec9508db2018-10-16 17:18:43 -070023#include <sstream>
24#include <vector>
Vishwa4be4b7a2015-10-31 22:55:50 -050025
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060026using namespace ipmi::vpd;
Patrick Venture5739ac32018-10-16 19:17:41 -070027using namespace phosphor::logging;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060028
Ratan Guptacb0d4e52016-12-22 19:05:57 +053029extern const FruMap frus;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060030extern const std::map<Path, InterfaceMap> extras;
Ratan Guptacb0d4e52016-12-22 19:05:57 +053031
Patrick Venture19bea9a2018-10-20 19:54:27 -070032using FruAreaVector = std::vector<std::unique_ptr<IPMIFruArea>>;
33
Patrick Venture234b7352018-10-20 09:37:09 -070034namespace
35{
36
Patrick Venture6cc89042018-10-21 14:26:39 -070037/**
38 * Cleanup routine
39 * Must always be called as last reference to fruFilePointer.
40 *
41 * @param[in] fruFilePointer - FRU file pointer to close
42 * @param[in] fruAreaVec - vector of FRU areas
43 * @return -1
44 */
Patrick Ventureef83b992018-10-21 09:32:44 -070045int cleanupError(FILE* fruFilePointer, FruAreaVector& fruAreaVec)
Patrick Venture234b7352018-10-20 09:37:09 -070046{
Patrick Ventureef83b992018-10-21 09:32:44 -070047 if (fruFilePointer != NULL)
Patrick Venture234b7352018-10-20 09:37:09 -070048 {
Patrick Ventureef83b992018-10-21 09:32:44 -070049 std::fclose(fruFilePointer);
Patrick Venture234b7352018-10-20 09:37:09 -070050 }
51
Patrick Ventureef83b992018-10-21 09:32:44 -070052 if (!(fruAreaVec.empty()))
Patrick Venture234b7352018-10-20 09:37:09 -070053 {
Patrick Ventureef83b992018-10-21 09:32:44 -070054 fruAreaVec.clear();
Patrick Venture234b7352018-10-20 09:37:09 -070055 }
56
57 return -1;
58}
59
Patrick Venture6cc89042018-10-21 14:26:39 -070060/**
61 * Gets the value of the key from the FRU dictionary of the given section.
62 * FRU dictionary is parsed FRU data for all the sections.
63 *
64 * @param[in] section - FRU section name
65 * @param[in] key - key for secion
66 * @param[in] delimiter - delimiter for parsing custom fields
67 * @param[in] fruData - the FRU data to search for the section
68 * @return FRU value
69 */
Patrick Venturede8ea562018-10-20 09:44:19 -070070std::string getFRUValue(const std::string& section, const std::string& key,
71 const std::string& delimiter, IPMIFruInfo& fruData)
72{
Patrick Venturede8ea562018-10-20 09:44:19 -070073 auto minIndexValue = 0;
74 auto maxIndexValue = 0;
75 std::string fruValue = "";
76
77 if (section == "Board")
78 {
79 minIndexValue = OPENBMC_VPD_KEY_BOARD_MFG_DATE;
80 maxIndexValue = OPENBMC_VPD_KEY_BOARD_MAX;
81 }
82 else if (section == "Product")
83 {
84 minIndexValue = OPENBMC_VPD_KEY_PRODUCT_MFR;
85 maxIndexValue = OPENBMC_VPD_KEY_PRODUCT_MAX;
86 }
87 else if (section == "Chassis")
88 {
89 minIndexValue = OPENBMC_VPD_KEY_CHASSIS_TYPE;
90 maxIndexValue = OPENBMC_VPD_KEY_CHASSIS_MAX;
91 }
92
93 auto first = fruData.cbegin() + minIndexValue;
94 auto last = first + (maxIndexValue - minIndexValue) + 1;
95
Patrick Venturec7eecc12018-10-21 08:55:44 -070096 auto itr = std::find_if(first, last,
97 [&key](const auto& e) { return key == e.first; });
Patrick Venturede8ea562018-10-20 09:44:19 -070098
99 if (itr != last)
100 {
101 fruValue = itr->second;
102 }
103
104 // if the key is custom property then the value could be in two formats.
105 // 1) custom field 2 = "value".
106 // 2) custom field 2 = "key:value".
107 // if delimiter length = 0 i.e custom field 2 = "value"
108
109 constexpr auto customProp = "Custom Field";
110 if (key.find(customProp) != std::string::npos)
111 {
112 if (delimiter.length() > 0)
113 {
114 size_t delimiterpos = fruValue.find(delimiter);
115 if (delimiterpos != std::string::npos)
Patrick Venture740d8c02018-10-21 12:53:05 -0700116 {
Patrick Venturede8ea562018-10-20 09:44:19 -0700117 fruValue = fruValue.substr(delimiterpos + 1);
Patrick Venture740d8c02018-10-21 12:53:05 -0700118 }
Patrick Venturede8ea562018-10-20 09:44:19 -0700119 }
120 }
121 return fruValue;
122}
123
Patrick Venture6cc89042018-10-21 14:26:39 -0700124/**
125 * Get the inventory service from the mapper.
126 *
127 * @param[in] bus - sdbusplus handle to use for dbus call
128 * @param[in] intf - interface
129 * @param[in] path - the object path
130 * @return the dbus service that owns the interface for that path
131 */
Patrick Williams5e8829e2022-07-22 19:26:53 -0500132auto getService(sdbusplus::bus_t& bus, const std::string& intf,
Patrick Venture17baa272018-10-20 09:48:08 -0700133 const std::string& path)
134{
Patrick Williamscfa96af2023-05-10 07:50:26 -0500135 auto mapperCall = bus.new_method_call("xyz.openbmc_project.ObjectMapper",
136 "/xyz/openbmc_project/object_mapper",
137 "xyz.openbmc_project.ObjectMapper",
138 "GetObject");
Patrick Venture17baa272018-10-20 09:48:08 -0700139
140 mapperCall.append(path);
141 mapperCall.append(std::vector<std::string>({intf}));
142 std::map<std::string, std::vector<std::string>> mapperResponse;
143
144 try
145 {
146 auto mapperResponseMsg = bus.call(mapperCall);
147 mapperResponseMsg.read(mapperResponse);
148 }
Patrick Williams5e8829e2022-07-22 19:26:53 -0500149 catch (const sdbusplus::exception_t& ex)
Patrick Venture17baa272018-10-20 09:48:08 -0700150 {
151 log<level::ERR>("Exception from sdbus call",
152 entry("WHAT=%s", ex.what()));
153 throw;
154 }
155
156 if (mapperResponse.begin() == mapperResponse.end())
157 {
158 throw std::runtime_error("ERROR in reading the mapper response");
159 }
160
161 return mapperResponse.begin()->first;
162}
163
Patrick Venture6cc89042018-10-21 14:26:39 -0700164/**
165 * Takes FRU data, invokes Parser for each FRU record area and updates
166 * inventory.
167 *
168 * @param[in] areaVector - vector of FRU areas
169 * @param[in] bus - handle to sdbus for calling methods, etc
170 * @return return non-zero of failure
171 */
Patrick Williams5e8829e2022-07-22 19:26:53 -0500172int updateInventory(FruAreaVector& areaVector, sdbusplus::bus_t& bus)
Patrick Venture44a957d2018-10-20 09:41:29 -0700173{
174 // Generic error reporter
175 int rc = 0;
176 uint8_t fruid = 0;
177 IPMIFruInfo fruData;
178
179 // For each FRU area, extract the needed data , get it parsed and update
180 // the Inventory.
Patrick Ventureef83b992018-10-21 09:32:44 -0700181 for (const auto& fruArea : areaVector)
Patrick Venture44a957d2018-10-20 09:41:29 -0700182 {
Patrick Venturef22b36a2018-10-20 20:59:07 -0700183 fruid = fruArea->getFruID();
Patrick Venture44a957d2018-10-20 09:41:29 -0700184 // Fill the container with information
Patrick Venture355c5612018-10-22 08:49:36 -0700185 rc = parse_fru_area(fruArea->getType(),
186 static_cast<const void*>(fruArea->getData()),
187 fruArea->getLength(), fruData);
Patrick Venture44a957d2018-10-20 09:41:29 -0700188 if (rc < 0)
189 {
190 log<level::ERR>("Error parsing FRU records");
191 return rc;
192 }
193 } // END walking the vector of areas and updating
194
Patrick Venture6cc89042018-10-21 14:26:39 -0700195 // For each FRU we have the list of instances which needs to be updated.
Patrick Venture44a957d2018-10-20 09:41:29 -0700196 // Each instance object implements certain interfaces.
197 // Each Interface is having Dbus properties.
198 // Each Dbus Property would be having metaData(eg section,VpdPropertyName).
199
200 // Here we are just printing the object,interface and the properties.
201 // which needs to be called with the new inventory manager implementation.
Patrick Venture44a957d2018-10-20 09:41:29 -0700202 using namespace std::string_literals;
203 static const auto intf = "xyz.openbmc_project.Inventory.Manager"s;
204 static const auto path = "/xyz/openbmc_project/inventory"s;
205 std::string service;
206 try
207 {
208 service = getService(bus, intf, path);
209 }
210 catch (const std::exception& e)
211 {
212 std::cerr << e.what() << "\n";
213 return -1;
214 }
215
216 auto iter = frus.find(fruid);
217 if (iter == frus.end())
218 {
Patrick Venturede0a59e2019-06-18 09:49:27 -0700219 log<level::ERR>("Unable to find FRUID in generated list",
Patrick Venture44a957d2018-10-20 09:41:29 -0700220 entry("FRU=%d", static_cast<int>(fruid)));
221 return -1;
222 }
223
224 auto& instanceList = iter->second;
225 if (instanceList.size() <= 0)
226 {
227 log<level::DEBUG>("Object list empty for this FRU",
228 entry("FRU=%d", static_cast<int>(fruid)));
229 }
230
231 ObjectMap objects;
Patrick Venturec7eecc12018-10-21 08:55:44 -0700232 for (const auto& instance : instanceList)
Patrick Venture44a957d2018-10-20 09:41:29 -0700233 {
234 InterfaceMap interfaces;
235 const auto& extrasIter = extras.find(instance.path);
236
Patrick Venturec7eecc12018-10-21 08:55:44 -0700237 for (const auto& interfaceList : instance.interfaces)
Patrick Venture44a957d2018-10-20 09:41:29 -0700238 {
239 PropertyMap props; // store all the properties
Patrick Venturec7eecc12018-10-21 08:55:44 -0700240 for (const auto& properties : interfaceList.second)
Patrick Venture44a957d2018-10-20 09:41:29 -0700241 {
242 std::string value;
243 decltype(auto) pdata = properties.second;
244
245 if (!pdata.section.empty() && !pdata.property.empty())
246 {
247 value = getFRUValue(pdata.section, pdata.property,
248 pdata.delimiter, fruData);
249 }
250 props.emplace(std::move(properties.first), std::move(value));
251 }
252 // Check and update extra properties
253 if (extras.end() != extrasIter)
254 {
255 const auto& propsIter =
256 (extrasIter->second).find(interfaceList.first);
257 if ((extrasIter->second).end() != propsIter)
258 {
259 for (const auto& map : propsIter->second)
260 {
261 props.emplace(map.first, map.second);
262 }
263 }
264 }
265 interfaces.emplace(std::move(interfaceList.first),
266 std::move(props));
267 }
268
269 // Call the inventory manager
Patrick Venture9a528f22018-10-20 13:31:28 -0700270 sdbusplus::message::object_path objectPath = instance.path;
Patrick Venture44a957d2018-10-20 09:41:29 -0700271 // Check and update extra properties
272 if (extras.end() != extrasIter)
273 {
274 for (const auto& entry : extrasIter->second)
275 {
276 if (interfaces.end() == interfaces.find(entry.first))
277 {
278 interfaces.emplace(entry.first, entry.second);
279 }
280 }
281 }
Patrick Venture9a528f22018-10-20 13:31:28 -0700282 objects.emplace(objectPath, interfaces);
Patrick Venture44a957d2018-10-20 09:41:29 -0700283 }
284
285 auto pimMsg = bus.new_method_call(service.c_str(), path.c_str(),
286 intf.c_str(), "Notify");
287 pimMsg.append(std::move(objects));
288
289 try
290 {
291 auto inventoryMgrResponseMsg = bus.call(pimMsg);
292 }
Patrick Williams5e8829e2022-07-22 19:26:53 -0500293 catch (const sdbusplus::exception_t& ex)
Patrick Venture44a957d2018-10-20 09:41:29 -0700294 {
Patrick Venture5b31e6b2019-08-05 07:37:19 -0700295 log<level::ERR>("Error in notify call", entry("WHAT=%s", ex.what()),
296 entry("SERVICE=%s", service.c_str()),
297 entry("PATH=%s", path.c_str()));
Patrick Venture44a957d2018-10-20 09:41:29 -0700298 return -1;
299 }
300
301 return rc;
302}
303
Patrick Venture234b7352018-10-20 09:37:09 -0700304} // namespace
305
Patrick Venture6cc89042018-10-21 14:26:39 -0700306/**
307 * Takes the pointer to stream of bytes and length and returns the 8 bit
308 * checksum. This algo is per IPMI V2.0 spec
309 *
310 * @param[in] data - data for running crc
311 * @param[in] len - the length over which to run the crc
312 * @return the CRC value
313 */
Patrick Venture062e1452018-10-21 09:16:47 -0700314unsigned char calculateCRC(const unsigned char* data, size_t len)
Vishwa4be4b7a2015-10-31 22:55:50 -0500315{
316 char crc = 0;
vishwac93d6d42015-12-16 11:55:16 -0600317 size_t byte = 0;
Vishwa4be4b7a2015-10-31 22:55:50 -0500318
Patrick Venturec9508db2018-10-16 17:18:43 -0700319 for (byte = 0; byte < len; byte++)
Vishwa4be4b7a2015-10-31 22:55:50 -0500320 {
321 crc += *data++;
322 }
vishwaf3ca3522015-12-02 10:35:13 -0600323
Patrick Venturec9508db2018-10-16 17:18:43 -0700324 return (-crc);
Vishwa4be4b7a2015-10-31 22:55:50 -0500325}
326
Patrick Venture6cc89042018-10-21 14:26:39 -0700327/**
328 * Accepts a FRU area offset into a commom header and tells which area it is.
329 *
330 * @param[in] areaOffset - offset to lookup the area type
331 * @return the ipmi_fru_area_type
332 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700333ipmi_fru_area_type getFruAreaType(uint8_t areaOffset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500334{
335 ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
336
Patrick Ventureef83b992018-10-21 09:32:44 -0700337 switch (areaOffset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500338 {
339 case IPMI_FRU_INTERNAL_OFFSET:
340 type = IPMI_FRU_AREA_INTERNAL_USE;
341 break;
342
343 case IPMI_FRU_CHASSIS_OFFSET:
344 type = IPMI_FRU_AREA_CHASSIS_INFO;
345 break;
346
347 case IPMI_FRU_BOARD_OFFSET:
348 type = IPMI_FRU_AREA_BOARD_INFO;
349 break;
350
351 case IPMI_FRU_PRODUCT_OFFSET:
352 type = IPMI_FRU_AREA_PRODUCT_INFO;
353 break;
354
355 case IPMI_FRU_MULTI_OFFSET:
356 type = IPMI_FRU_AREA_MULTI_RECORD;
357 break;
358
359 default:
360 type = IPMI_FRU_AREA_TYPE_MAX;
361 }
362
363 return type;
364}
365
Patrick Venture6cc89042018-10-21 14:26:39 -0700366/**
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000367 * Validates the data for multirecord fields and CRC if selected
368 *
369 * @param[in] data - the data to verify
370 * @param[in] len - the length of the region to verify
371 * @param[in] validateCrc - whether to validate the CRC
372 * @return non-zero on failure
373 */
374int verifyFruMultiRecData(const uint8_t* data, const size_t len,
375 bool validateCrc)
376{
377 uint8_t checksum = 0;
378 int rc = -1;
379
380 if (!validateCrc)
381 {
382 // There's nothing else to do for this area.
383 return EXIT_SUCCESS;
384 }
385
386 // As per the IPMI platform spec, byte[3] is the record checksum.
387 checksum = calculateCRC(data, len);
388 if (checksum != data[3])
389 {
390#ifdef __IPMI_DEBUG__
391 log<level::ERR>(
392 "Checksum mismatch",
393 entry("Calculated=0x%X", static_cast<uint32_t>(checksum)),
394 entry("Embedded=0x%X", static_cast<uint32_t>(data[3])));
395#endif
396 return rc;
397 }
398#ifdef __IPMI_DEBUG__
399 else
400 {
401 log<level::DEBUG>("Checksum matches");
402 }
403#endif
404
405 return EXIT_SUCCESS;
406}
407
408/**
Oskar Senft69540862018-12-04 15:52:31 -0500409 * Validates the data for mandatory fields and CRC if selected.
Patrick Venture6cc89042018-10-21 14:26:39 -0700410 *
411 * @param[in] data - the data to verify
412 * @param[in] len - the length of the region to verify
Oskar Senft69540862018-12-04 15:52:31 -0500413 * @param[in] validateCrc - whether to validate the CRC
Patrick Venture6cc89042018-10-21 14:26:39 -0700414 * @return non-zero on failure
415 */
Oskar Senft69540862018-12-04 15:52:31 -0500416int verifyFruData(const uint8_t* data, const size_t len, bool validateCrc)
vishwac93d6d42015-12-16 11:55:16 -0600417{
418 uint8_t checksum = 0;
419 int rc = -1;
420
421 // Validate for first byte to always have a value of [1]
Patrick Venturec9508db2018-10-16 17:18:43 -0700422 if (data[0] != IPMI_FRU_HDR_BYTE_ZERO)
vishwac93d6d42015-12-16 11:55:16 -0600423 {
Patrick Venture5c787212018-10-17 13:48:23 -0700424 log<level::ERR>("Invalid entry in byte-0",
425 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600426 return rc;
427 }
428#ifdef __IPMI_DEBUG__
429 else
430 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700431 log<level::DEBUG>("Validated in entry_1 of fruData",
Patrick Venture5c787212018-10-17 13:48:23 -0700432 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600433 }
434#endif
435
Oskar Senft69540862018-12-04 15:52:31 -0500436 if (!validateCrc)
437 {
438 // There's nothing else to do for this area.
439 return EXIT_SUCCESS;
440 }
441
vishwac93d6d42015-12-16 11:55:16 -0600442 // See if the calculated CRC matches with the embedded one.
443 // CRC to be calculated on all except the last one that is CRC itself.
Patrick Venture062e1452018-10-21 09:16:47 -0700444 checksum = calculateCRC(data, len - 1);
Patrick Venturec9508db2018-10-16 17:18:43 -0700445 if (checksum != data[len - 1])
vishwac93d6d42015-12-16 11:55:16 -0600446 {
447#ifdef __IPMI_DEBUG__
Patrick Venture5c787212018-10-17 13:48:23 -0700448 log<level::ERR>(
449 "Checksum mismatch",
450 entry("Calculated=0x%X", static_cast<uint32_t>(checksum)),
451 entry("Embedded=0x%X", static_cast<uint32_t>(data[len])));
vishwac93d6d42015-12-16 11:55:16 -0600452#endif
453 return rc;
454 }
455#ifdef __IPMI_DEBUG__
456 else
457 {
Patrick Venture5c787212018-10-17 13:48:23 -0700458 log<level::DEBUG>("Checksum matches");
vishwac93d6d42015-12-16 11:55:16 -0600459 }
460#endif
461
462 return EXIT_SUCCESS;
463}
464
Patrick Venture6cc89042018-10-21 14:26:39 -0700465/**
466 * Checks if a particular FRU area is populated or not.
467 *
468 * @param[in] reference to FRU area pointer
469 * @return true if the area is empty
470 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700471bool removeInvalidArea(const std::unique_ptr<IPMIFruArea>& fruArea)
Vishwa4be4b7a2015-10-31 22:55:50 -0500472{
Deepak Kodihalli89ededd2017-02-11 01:59:32 -0600473 // Filter the ones that are empty
Patrick Ventureef83b992018-10-21 09:32:44 -0700474 if (!(fruArea->getLength()))
vishwac93d6d42015-12-16 11:55:16 -0600475 {
476 return true;
477 }
478 return false;
479}
Vishwa4be4b7a2015-10-31 22:55:50 -0500480
Patrick Venture6cc89042018-10-21 14:26:39 -0700481/**
482 * Populates various FRU areas.
483 *
484 * @prereq : This must be called only after validating common header
485 * @param[in] fruData - pointer to the FRU bytes
486 * @param[in] dataLen - the length of the FRU data
487 * @param[in] fruAreaVec - the FRU area vector to update
488 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700489int ipmiPopulateFruAreas(uint8_t* fruData, const size_t dataLen,
490 FruAreaVector& fruAreaVec)
vishwac93d6d42015-12-16 11:55:16 -0600491{
vishwac93d6d42015-12-16 11:55:16 -0600492 // Now walk the common header and see if the file size has atleast the last
Patrick Ventureef83b992018-10-21 09:32:44 -0700493 // offset mentioned by the struct common_header. If the file size is less
Patrick Venture6cc89042018-10-21 14:26:39 -0700494 // than the offset of any if the FRU areas mentioned in the common header,
Patrick Ventureef83b992018-10-21 09:32:44 -0700495 // then we do not have a complete file.
496 for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET;
497 fruEntry < (sizeof(struct common_header) - 2); fruEntry++)
vishwac93d6d42015-12-16 11:55:16 -0600498 {
Patrick Venture2cdb0e32019-08-05 07:47:01 -0700499 int rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600500 // Actual offset in the payload is the offset mentioned in common header
Gunnar Millsc19b8132018-06-14 08:56:17 -0500501 // multiplied by 8. Common header is always the first 8 bytes.
Patrick Ventureef83b992018-10-21 09:32:44 -0700502 size_t areaOffset = fruData[fruEntry] * IPMI_EIGHT_BYTES;
503 if (areaOffset && (dataLen < (areaOffset + 2)))
vishwac93d6d42015-12-16 11:55:16 -0600504 {
505 // Our file size is less than what it needs to be. +2 because we are
Patrick Ventureef83b992018-10-21 09:32:44 -0700506 // using area len that is at 2 byte off areaOffset
Patrick Venture6cc89042018-10-21 14:26:39 -0700507 log<level::ERR>("FRU file is incomplete",
Patrick Ventureef83b992018-10-21 09:32:44 -0700508 entry("SIZE=%d", dataLen));
vishwac93d6d42015-12-16 11:55:16 -0600509 return rc;
510 }
Patrick Ventureef83b992018-10-21 09:32:44 -0700511 else if (areaOffset)
vishwac93d6d42015-12-16 11:55:16 -0600512 {
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000513 // Read 3 bytes to know the actual size of area.
514 uint8_t areaHeader[3] = {0};
Patrick Ventureef83b992018-10-21 09:32:44 -0700515 std::memcpy(areaHeader, &((uint8_t*)fruData)[areaOffset],
516 sizeof(areaHeader));
Vishwa4be4b7a2015-10-31 22:55:50 -0500517
Patrick Venture6cc89042018-10-21 14:26:39 -0700518 // Size of this area will be the 2nd byte in the FRU area header.
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000519 size_t areaLen;
520 if (fruEntry == IPMI_FRU_MULTI_OFFSET)
521 {
522 areaLen = areaHeader[2] + IPMI_FRU_MULTIREC_HDR_BYTES;
523 }
524 else
525 {
526 areaLen = areaHeader[1] * IPMI_EIGHT_BYTES;
527 }
528
Patrick Venture6cc89042018-10-21 14:26:39 -0700529 log<level::DEBUG>("FRU Data", entry("SIZE=%d", dataLen),
Patrick Ventureef83b992018-10-21 09:32:44 -0700530 entry("AREA OFFSET=%d", areaOffset),
531 entry("AREA_SIZE=%d", areaLen));
Vishwa4be4b7a2015-10-31 22:55:50 -0500532
vishwac93d6d42015-12-16 11:55:16 -0600533 // See if we really have that much buffer. We have area offset amd
534 // from there, the actual len.
Patrick Ventureef83b992018-10-21 09:32:44 -0700535 if (dataLen < (areaLen + areaOffset))
vishwac93d6d42015-12-16 11:55:16 -0600536 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700537 log<level::ERR>("Incomplete FRU file",
Patrick Ventureef83b992018-10-21 09:32:44 -0700538 entry("SIZE=%d", dataLen));
vishwac93d6d42015-12-16 11:55:16 -0600539 return rc;
540 }
541
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500542 auto fruDataView = std::span<uint8_t>(&fruData[areaOffset],
543 areaLen);
544 auto areaData = std::vector<uint8_t>(fruDataView.begin(),
545 fruDataView.end());
vishwac93d6d42015-12-16 11:55:16 -0600546
Oskar Senft69540862018-12-04 15:52:31 -0500547 // Validate the CRC, but not for the internal use area, since its
548 // contents beyond the first byte are not defined in the spec and
549 // it may not end with a CRC byte.
550 bool validateCrc = fruEntry != IPMI_FRU_INTERNAL_OFFSET;
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000551
552 if (fruEntry == IPMI_FRU_MULTI_OFFSET)
553 {
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500554 rc = verifyFruMultiRecData(areaData.data(), areaLen,
555 validateCrc);
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000556 }
557 else
558 {
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500559 rc = verifyFruData(areaData.data(), areaLen, validateCrc);
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000560 }
561
Patrick Venturec9508db2018-10-16 17:18:43 -0700562 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600563 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700564 log<level::ERR>("Err validating FRU area",
Patrick Ventureef83b992018-10-21 09:32:44 -0700565 entry("OFFSET=%d", areaOffset));
vishwac93d6d42015-12-16 11:55:16 -0600566 return rc;
567 }
568 else
569 {
Oskar Senft69540862018-12-04 15:52:31 -0500570 log<level::DEBUG>("Successfully verified area.",
Patrick Ventureef83b992018-10-21 09:32:44 -0700571 entry("OFFSET=%d", areaOffset));
vishwac93d6d42015-12-16 11:55:16 -0600572 }
573
574 // We already have a vector that is passed to us containing all
575 // of the fields populated. Update the data portion now.
Patrick Ventureef83b992018-10-21 09:32:44 -0700576 for (auto& iter : fruAreaVec)
vishwac93d6d42015-12-16 11:55:16 -0600577 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700578 if (iter->getType() == getFruAreaType(fruEntry))
vishwac93d6d42015-12-16 11:55:16 -0600579 {
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500580 iter->setData(areaData.data(), areaLen);
vishwac93d6d42015-12-16 11:55:16 -0600581 }
582 }
Patrick Venture6cc89042018-10-21 14:26:39 -0700583 } // If we have FRU data present
Patrick Ventureef83b992018-10-21 09:32:44 -0700584 } // Walk struct common_header
vishwac93d6d42015-12-16 11:55:16 -0600585
Patrick Venture6cc89042018-10-21 14:26:39 -0700586 // Not all the fields will be populated in a FRU data. Mostly all cases will
vishwac93d6d42015-12-16 11:55:16 -0600587 // not have more than 2 or 3.
Patrick Ventureef83b992018-10-21 09:32:44 -0700588 fruAreaVec.erase(
589 std::remove_if(fruAreaVec.begin(), fruAreaVec.end(), removeInvalidArea),
590 fruAreaVec.end());
vishwac93d6d42015-12-16 11:55:16 -0600591
592 return EXIT_SUCCESS;
593}
594
Patrick Venture6cc89042018-10-21 14:26:39 -0700595/**
596 * Validates the FRU data per ipmi common header constructs.
597 * Returns with updated struct common_header and also file_size
598 *
599 * @param[in] fruData - the FRU data
600 * @param[in] dataLen - the length of the data
601 * @return non-zero on failure
602 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700603int ipmiValidateCommonHeader(const uint8_t* fruData, const size_t dataLen)
vishwac93d6d42015-12-16 11:55:16 -0600604{
605 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500606
Patrick Ventureef83b992018-10-21 09:32:44 -0700607 uint8_t commonHdr[sizeof(struct common_header)] = {0};
608 if (dataLen >= sizeof(commonHdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500609 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700610 std::memcpy(commonHdr, fruData, sizeof(commonHdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500611 }
612 else
613 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700614 log<level::ERR>("Incomplete FRU data file", entry("SIZE=%d", dataLen));
vishwac93d6d42015-12-16 11:55:16 -0600615 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500616 }
617
Patrick Venture6cc89042018-10-21 14:26:39 -0700618 // Verify the CRC and size
Oskar Senft69540862018-12-04 15:52:31 -0500619 rc = verifyFruData(commonHdr, sizeof(commonHdr), true);
Patrick Venturec9508db2018-10-16 17:18:43 -0700620 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500621 {
Patrick Venture5c787212018-10-17 13:48:23 -0700622 log<level::ERR>("Failed to validate common header");
vishwac93d6d42015-12-16 11:55:16 -0600623 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500624 }
625
vishwac93d6d42015-12-16 11:55:16 -0600626 return EXIT_SUCCESS;
627}
Vishwa4be4b7a2015-10-31 22:55:50 -0500628
Patrick Ventureef83b992018-10-21 09:32:44 -0700629int validateFRUArea(const uint8_t fruid, const char* fruFilename,
Patrick Williams5e8829e2022-07-22 19:26:53 -0500630 sdbusplus::bus_t& bus, const bool bmcOnlyFru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500631{
Patrick Ventureef83b992018-10-21 09:32:44 -0700632 size_t dataLen = 0;
633 size_t bytesRead = 0;
vishwac93d6d42015-12-16 11:55:16 -0600634 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500635
vishwac93d6d42015-12-16 11:55:16 -0600636 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
637 // are not used, keeping it here for completeness.
Patrick Ventureef83b992018-10-21 09:32:44 -0700638 FruAreaVector fruAreaVec;
Yi Li75c2d462016-04-11 16:57:46 +0800639
Patrick Ventureef83b992018-10-21 09:32:44 -0700640 for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET;
641 fruEntry < (sizeof(struct common_header) - 2); fruEntry++)
vishwac93d6d42015-12-16 11:55:16 -0600642 {
643 // Create an object and push onto a vector.
Patrick Ventureef83b992018-10-21 09:32:44 -0700644 std::unique_ptr<IPMIFruArea> fruArea = std::make_unique<IPMIFruArea>(
645 fruid, getFruAreaType(fruEntry), bmcOnlyFru);
vishwac93d6d42015-12-16 11:55:16 -0600646
647 // Physically being present
Patrick Ventureef83b992018-10-21 09:32:44 -0700648 bool present = access(fruFilename, F_OK) == 0;
649 fruArea->setPresent(present);
vishwac93d6d42015-12-16 11:55:16 -0600650
Patrick Ventureef83b992018-10-21 09:32:44 -0700651 fruAreaVec.emplace_back(std::move(fruArea));
vishwac93d6d42015-12-16 11:55:16 -0600652 }
653
Patrick Ventureef83b992018-10-21 09:32:44 -0700654 FILE* fruFilePointer = std::fopen(fruFilename, "rb");
655 if (fruFilePointer == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500656 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700657 log<level::ERR>("Unable to open FRU file",
Patrick Ventureef83b992018-10-21 09:32:44 -0700658 entry("FILE=%s", fruFilename),
Patrick Venture5c787212018-10-17 13:48:23 -0700659 entry("ERRNO=%s", std::strerror(errno)));
Patrick Ventureef83b992018-10-21 09:32:44 -0700660 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500661 }
662
vishwac93d6d42015-12-16 11:55:16 -0600663 // Get the size of the file to see if it meets minimum requirement
Patrick Ventureef83b992018-10-21 09:32:44 -0700664 if (std::fseek(fruFilePointer, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500665 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700666 log<level::ERR>("Unable to seek FRU file",
Patrick Ventureef83b992018-10-21 09:32:44 -0700667 entry("FILE=%s", fruFilename),
Patrick Venture5c787212018-10-17 13:48:23 -0700668 entry("ERRNO=%s", std::strerror(errno)));
Patrick Ventureef83b992018-10-21 09:32:44 -0700669 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500670 }
671
vishwac93d6d42015-12-16 11:55:16 -0600672 // Allocate a buffer to hold entire file content
Patrick Ventureef83b992018-10-21 09:32:44 -0700673 dataLen = std::ftell(fruFilePointer);
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500674
675 auto fruData = std::vector<uint8_t>(dataLen, 0);
Vishwa4be4b7a2015-10-31 22:55:50 -0500676
Patrick Ventureef83b992018-10-21 09:32:44 -0700677 std::rewind(fruFilePointer);
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500678 bytesRead = std::fread(fruData.data(), dataLen, 1, fruFilePointer);
Patrick Ventureef83b992018-10-21 09:32:44 -0700679 if (bytesRead != 1)
Vishwa4be4b7a2015-10-31 22:55:50 -0500680 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700681 log<level::ERR>("Failed reading FRU data.",
Patrick Ventureef83b992018-10-21 09:32:44 -0700682 entry("BYTESREAD=%d", bytesRead),
Patrick Venture5c787212018-10-17 13:48:23 -0700683 entry("ERRNO=%s", std::strerror(errno)));
Patrick Ventureef83b992018-10-21 09:32:44 -0700684 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500685 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500686
vishwac93d6d42015-12-16 11:55:16 -0600687 // We are done reading.
Patrick Ventureef83b992018-10-21 09:32:44 -0700688 std::fclose(fruFilePointer);
689 fruFilePointer = NULL;
vishwac93d6d42015-12-16 11:55:16 -0600690
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500691 rc = ipmiValidateCommonHeader(fruData.data(), dataLen);
Patrick Venturec9508db2018-10-16 17:18:43 -0700692 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500693 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700694 return cleanupError(fruFilePointer, fruAreaVec);
vishwac93d6d42015-12-16 11:55:16 -0600695 }
696
Patrick Venture6cc89042018-10-21 14:26:39 -0700697 // Now that we validated the common header, populate various FRU sections if
Patrick Venturec9508db2018-10-16 17:18:43 -0700698 // we have them here.
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500699 rc = ipmiPopulateFruAreas(fruData.data(), dataLen, fruAreaVec);
Patrick Venturec9508db2018-10-16 17:18:43 -0700700 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600701 {
Patrick Venture5c787212018-10-17 13:48:23 -0700702 log<level::ERR>("Populating FRU areas failed", entry("FRU=%d", fruid));
Patrick Ventureef83b992018-10-21 09:32:44 -0700703 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500704 }
705 else
706 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700707 log<level::DEBUG>("Populated FRU areas", entry("FILE=%s", fruFilename));
Vishwa4be4b7a2015-10-31 22:55:50 -0500708 }
709
vishwac93d6d42015-12-16 11:55:16 -0600710#ifdef __IPMI_DEBUG__
Patrick Ventureef83b992018-10-21 09:32:44 -0700711 for (const auto& iter : fruAreaVec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500712 {
Patrick Venture0e3a1c42018-10-20 21:15:41 -0700713 std::printf("FRU ID : [%d]\n", iter->getFruID());
714 std::printf("AREA NAME : [%s]\n", iter->getName());
715 std::printf("TYPE : [%d]\n", iter->getType());
716 std::printf("LEN : [%d]\n", iter->getLength());
Vishwa4be4b7a2015-10-31 22:55:50 -0500717 }
vishwac93d6d42015-12-16 11:55:16 -0600718#endif
719
720 // If the vector is populated with everything, then go ahead and update the
721 // inventory.
Patrick Ventureef83b992018-10-21 09:32:44 -0700722 if (!(fruAreaVec.empty()))
vishwac93d6d42015-12-16 11:55:16 -0600723 {
vishwac93d6d42015-12-16 11:55:16 -0600724#ifdef __IPMI_DEBUG__
Patrick Ventureef83b992018-10-21 09:32:44 -0700725 std::printf("\n SIZE of vector is : [%d] \n", fruAreaVec.size());
vishwac93d6d42015-12-16 11:55:16 -0600726#endif
Patrick Ventureef83b992018-10-21 09:32:44 -0700727 rc = updateInventory(fruAreaVec, bus);
Patrick Venturec9508db2018-10-16 17:18:43 -0700728 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600729 {
Patrick Venture5c787212018-10-17 13:48:23 -0700730 log<level::ERR>("Error updating inventory.");
vishwac93d6d42015-12-16 11:55:16 -0600731 }
732 }
733
734 // we are done with all that we wanted to do. This will do the job of
735 // calling any destructors too.
Patrick Ventureef83b992018-10-21 09:32:44 -0700736 fruAreaVec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500737
738 return rc;
739}