blob: 44117aeb9d5ec2cd69abe2c5c7704144a81e0bfc [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
Manojkiran Edabc5725d2024-06-17 11:50:54 +053065 * @param[in] key - key for section
Patrick Venture6cc89042018-10-21 14:26:39 -070066 * @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 Williamsa3cc2282024-08-16 15:20:14 -040096 auto itr = std::find_if(first, last, [&key](const auto& e) {
97 return key == e.first;
98 });
Patrick Venturede8ea562018-10-20 09:44:19 -070099
100 if (itr != last)
101 {
102 fruValue = itr->second;
103 }
104
105 // if the key is custom property then the value could be in two formats.
106 // 1) custom field 2 = "value".
107 // 2) custom field 2 = "key:value".
108 // if delimiter length = 0 i.e custom field 2 = "value"
109
110 constexpr auto customProp = "Custom Field";
111 if (key.find(customProp) != std::string::npos)
112 {
113 if (delimiter.length() > 0)
114 {
115 size_t delimiterpos = fruValue.find(delimiter);
116 if (delimiterpos != std::string::npos)
Patrick Venture740d8c02018-10-21 12:53:05 -0700117 {
Patrick Venturede8ea562018-10-20 09:44:19 -0700118 fruValue = fruValue.substr(delimiterpos + 1);
Patrick Venture740d8c02018-10-21 12:53:05 -0700119 }
Patrick Venturede8ea562018-10-20 09:44:19 -0700120 }
121 }
122 return fruValue;
123}
124
Patrick Venture6cc89042018-10-21 14:26:39 -0700125/**
126 * Get the inventory service from the mapper.
127 *
128 * @param[in] bus - sdbusplus handle to use for dbus call
129 * @param[in] intf - interface
130 * @param[in] path - the object path
131 * @return the dbus service that owns the interface for that path
132 */
Patrick Williams5e8829e2022-07-22 19:26:53 -0500133auto getService(sdbusplus::bus_t& bus, const std::string& intf,
Patrick Venture17baa272018-10-20 09:48:08 -0700134 const std::string& path)
135{
Patrick Williamsa3cc2282024-08-16 15:20:14 -0400136 auto mapperCall =
137 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
138 "/xyz/openbmc_project/object_mapper",
139 "xyz.openbmc_project.ObjectMapper", "GetObject");
Patrick Venture17baa272018-10-20 09:48:08 -0700140
141 mapperCall.append(path);
142 mapperCall.append(std::vector<std::string>({intf}));
143 std::map<std::string, std::vector<std::string>> mapperResponse;
144
145 try
146 {
147 auto mapperResponseMsg = bus.call(mapperCall);
148 mapperResponseMsg.read(mapperResponse);
149 }
Patrick Williams5e8829e2022-07-22 19:26:53 -0500150 catch (const sdbusplus::exception_t& ex)
Patrick Venture17baa272018-10-20 09:48:08 -0700151 {
152 log<level::ERR>("Exception from sdbus call",
153 entry("WHAT=%s", ex.what()));
154 throw;
155 }
156
157 if (mapperResponse.begin() == mapperResponse.end())
158 {
159 throw std::runtime_error("ERROR in reading the mapper response");
160 }
161
162 return mapperResponse.begin()->first;
163}
164
Patrick Venture6cc89042018-10-21 14:26:39 -0700165/**
166 * Takes FRU data, invokes Parser for each FRU record area and updates
167 * inventory.
168 *
169 * @param[in] areaVector - vector of FRU areas
170 * @param[in] bus - handle to sdbus for calling methods, etc
171 * @return return non-zero of failure
172 */
Patrick Williams5e8829e2022-07-22 19:26:53 -0500173int updateInventory(FruAreaVector& areaVector, sdbusplus::bus_t& bus)
Patrick Venture44a957d2018-10-20 09:41:29 -0700174{
175 // Generic error reporter
176 int rc = 0;
177 uint8_t fruid = 0;
178 IPMIFruInfo fruData;
179
180 // For each FRU area, extract the needed data , get it parsed and update
181 // the Inventory.
Patrick Ventureef83b992018-10-21 09:32:44 -0700182 for (const auto& fruArea : areaVector)
Patrick Venture44a957d2018-10-20 09:41:29 -0700183 {
Patrick Venturef22b36a2018-10-20 20:59:07 -0700184 fruid = fruArea->getFruID();
Patrick Venture44a957d2018-10-20 09:41:29 -0700185 // Fill the container with information
Patrick Venture355c5612018-10-22 08:49:36 -0700186 rc = parse_fru_area(fruArea->getType(),
187 static_cast<const void*>(fruArea->getData()),
188 fruArea->getLength(), fruData);
Patrick Venture44a957d2018-10-20 09:41:29 -0700189 if (rc < 0)
190 {
191 log<level::ERR>("Error parsing FRU records");
192 return rc;
193 }
194 } // END walking the vector of areas and updating
195
Patrick Venture6cc89042018-10-21 14:26:39 -0700196 // For each FRU we have the list of instances which needs to be updated.
Patrick Venture44a957d2018-10-20 09:41:29 -0700197 // Each instance object implements certain interfaces.
198 // Each Interface is having Dbus properties.
199 // Each Dbus Property would be having metaData(eg section,VpdPropertyName).
200
201 // Here we are just printing the object,interface and the properties.
202 // which needs to be called with the new inventory manager implementation.
Patrick Venture44a957d2018-10-20 09:41:29 -0700203 using namespace std::string_literals;
204 static const auto intf = "xyz.openbmc_project.Inventory.Manager"s;
205 static const auto path = "/xyz/openbmc_project/inventory"s;
206 std::string service;
207 try
208 {
209 service = getService(bus, intf, path);
210 }
211 catch (const std::exception& e)
212 {
213 std::cerr << e.what() << "\n";
214 return -1;
215 }
216
217 auto iter = frus.find(fruid);
218 if (iter == frus.end())
219 {
Patrick Venturede0a59e2019-06-18 09:49:27 -0700220 log<level::ERR>("Unable to find FRUID in generated list",
Patrick Venture44a957d2018-10-20 09:41:29 -0700221 entry("FRU=%d", static_cast<int>(fruid)));
222 return -1;
223 }
224
225 auto& instanceList = iter->second;
226 if (instanceList.size() <= 0)
227 {
228 log<level::DEBUG>("Object list empty for this FRU",
229 entry("FRU=%d", static_cast<int>(fruid)));
230 }
231
232 ObjectMap objects;
Patrick Venturec7eecc12018-10-21 08:55:44 -0700233 for (const auto& instance : instanceList)
Patrick Venture44a957d2018-10-20 09:41:29 -0700234 {
235 InterfaceMap interfaces;
236 const auto& extrasIter = extras.find(instance.path);
237
Patrick Venturec7eecc12018-10-21 08:55:44 -0700238 for (const auto& interfaceList : instance.interfaces)
Patrick Venture44a957d2018-10-20 09:41:29 -0700239 {
240 PropertyMap props; // store all the properties
Patrick Venturec7eecc12018-10-21 08:55:44 -0700241 for (const auto& properties : interfaceList.second)
Patrick Venture44a957d2018-10-20 09:41:29 -0700242 {
243 std::string value;
244 decltype(auto) pdata = properties.second;
245
246 if (!pdata.section.empty() && !pdata.property.empty())
247 {
248 value = getFRUValue(pdata.section, pdata.property,
249 pdata.delimiter, fruData);
250 }
251 props.emplace(std::move(properties.first), std::move(value));
252 }
253 // Check and update extra properties
254 if (extras.end() != extrasIter)
255 {
256 const auto& propsIter =
257 (extrasIter->second).find(interfaceList.first);
258 if ((extrasIter->second).end() != propsIter)
259 {
260 for (const auto& map : propsIter->second)
261 {
262 props.emplace(map.first, map.second);
263 }
264 }
265 }
266 interfaces.emplace(std::move(interfaceList.first),
267 std::move(props));
268 }
269
270 // Call the inventory manager
Patrick Venture9a528f22018-10-20 13:31:28 -0700271 sdbusplus::message::object_path objectPath = instance.path;
Patrick Venture44a957d2018-10-20 09:41:29 -0700272 // Check and update extra properties
273 if (extras.end() != extrasIter)
274 {
275 for (const auto& entry : extrasIter->second)
276 {
277 if (interfaces.end() == interfaces.find(entry.first))
278 {
279 interfaces.emplace(entry.first, entry.second);
280 }
281 }
282 }
Patrick Venture9a528f22018-10-20 13:31:28 -0700283 objects.emplace(objectPath, interfaces);
Patrick Venture44a957d2018-10-20 09:41:29 -0700284 }
285
286 auto pimMsg = bus.new_method_call(service.c_str(), path.c_str(),
287 intf.c_str(), "Notify");
288 pimMsg.append(std::move(objects));
289
290 try
291 {
292 auto inventoryMgrResponseMsg = bus.call(pimMsg);
293 }
Patrick Williams5e8829e2022-07-22 19:26:53 -0500294 catch (const sdbusplus::exception_t& ex)
Patrick Venture44a957d2018-10-20 09:41:29 -0700295 {
Patrick Venture5b31e6b2019-08-05 07:37:19 -0700296 log<level::ERR>("Error in notify call", entry("WHAT=%s", ex.what()),
297 entry("SERVICE=%s", service.c_str()),
298 entry("PATH=%s", path.c_str()));
Patrick Venture44a957d2018-10-20 09:41:29 -0700299 return -1;
300 }
301
302 return rc;
303}
304
Patrick Venture234b7352018-10-20 09:37:09 -0700305} // namespace
306
Patrick Venture6cc89042018-10-21 14:26:39 -0700307/**
308 * Takes the pointer to stream of bytes and length and returns the 8 bit
309 * checksum. This algo is per IPMI V2.0 spec
310 *
311 * @param[in] data - data for running crc
312 * @param[in] len - the length over which to run the crc
313 * @return the CRC value
314 */
Patrick Venture062e1452018-10-21 09:16:47 -0700315unsigned char calculateCRC(const unsigned char* data, size_t len)
Vishwa4be4b7a2015-10-31 22:55:50 -0500316{
317 char crc = 0;
vishwac93d6d42015-12-16 11:55:16 -0600318 size_t byte = 0;
Vishwa4be4b7a2015-10-31 22:55:50 -0500319
Patrick Venturec9508db2018-10-16 17:18:43 -0700320 for (byte = 0; byte < len; byte++)
Vishwa4be4b7a2015-10-31 22:55:50 -0500321 {
322 crc += *data++;
323 }
vishwaf3ca3522015-12-02 10:35:13 -0600324
Patrick Venturec9508db2018-10-16 17:18:43 -0700325 return (-crc);
Vishwa4be4b7a2015-10-31 22:55:50 -0500326}
327
Patrick Venture6cc89042018-10-21 14:26:39 -0700328/**
Manojkiran Edabc5725d2024-06-17 11:50:54 +0530329 * Accepts a FRU area offset into a common header and tells which area it is.
Patrick Venture6cc89042018-10-21 14:26:39 -0700330 *
331 * @param[in] areaOffset - offset to lookup the area type
332 * @return the ipmi_fru_area_type
333 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700334ipmi_fru_area_type getFruAreaType(uint8_t areaOffset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500335{
336 ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
337
Patrick Ventureef83b992018-10-21 09:32:44 -0700338 switch (areaOffset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500339 {
340 case IPMI_FRU_INTERNAL_OFFSET:
341 type = IPMI_FRU_AREA_INTERNAL_USE;
342 break;
343
344 case IPMI_FRU_CHASSIS_OFFSET:
345 type = IPMI_FRU_AREA_CHASSIS_INFO;
346 break;
347
348 case IPMI_FRU_BOARD_OFFSET:
349 type = IPMI_FRU_AREA_BOARD_INFO;
350 break;
351
352 case IPMI_FRU_PRODUCT_OFFSET:
353 type = IPMI_FRU_AREA_PRODUCT_INFO;
354 break;
355
356 case IPMI_FRU_MULTI_OFFSET:
357 type = IPMI_FRU_AREA_MULTI_RECORD;
358 break;
359
360 default:
361 type = IPMI_FRU_AREA_TYPE_MAX;
362 }
363
364 return type;
365}
366
Patrick Venture6cc89042018-10-21 14:26:39 -0700367/**
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000368 * Validates the data for multirecord fields and CRC if selected
369 *
370 * @param[in] data - the data to verify
371 * @param[in] len - the length of the region to verify
372 * @param[in] validateCrc - whether to validate the CRC
373 * @return non-zero on failure
374 */
375int verifyFruMultiRecData(const uint8_t* data, const size_t len,
376 bool validateCrc)
377{
378 uint8_t checksum = 0;
379 int rc = -1;
380
381 if (!validateCrc)
382 {
383 // There's nothing else to do for this area.
384 return EXIT_SUCCESS;
385 }
386
387 // As per the IPMI platform spec, byte[3] is the record checksum.
388 checksum = calculateCRC(data, len);
389 if (checksum != data[3])
390 {
391#ifdef __IPMI_DEBUG__
392 log<level::ERR>(
393 "Checksum mismatch",
394 entry("Calculated=0x%X", static_cast<uint32_t>(checksum)),
395 entry("Embedded=0x%X", static_cast<uint32_t>(data[3])));
396#endif
397 return rc;
398 }
399#ifdef __IPMI_DEBUG__
400 else
401 {
402 log<level::DEBUG>("Checksum matches");
403 }
404#endif
405
406 return EXIT_SUCCESS;
407}
408
409/**
Oskar Senft69540862018-12-04 15:52:31 -0500410 * Validates the data for mandatory fields and CRC if selected.
Patrick Venture6cc89042018-10-21 14:26:39 -0700411 *
412 * @param[in] data - the data to verify
413 * @param[in] len - the length of the region to verify
Oskar Senft69540862018-12-04 15:52:31 -0500414 * @param[in] validateCrc - whether to validate the CRC
Patrick Venture6cc89042018-10-21 14:26:39 -0700415 * @return non-zero on failure
416 */
Oskar Senft69540862018-12-04 15:52:31 -0500417int verifyFruData(const uint8_t* data, const size_t len, bool validateCrc)
vishwac93d6d42015-12-16 11:55:16 -0600418{
419 uint8_t checksum = 0;
420 int rc = -1;
421
422 // Validate for first byte to always have a value of [1]
Patrick Venturec9508db2018-10-16 17:18:43 -0700423 if (data[0] != IPMI_FRU_HDR_BYTE_ZERO)
vishwac93d6d42015-12-16 11:55:16 -0600424 {
Patrick Venture5c787212018-10-17 13:48:23 -0700425 log<level::ERR>("Invalid entry in byte-0",
426 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600427 return rc;
428 }
429#ifdef __IPMI_DEBUG__
430 else
431 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700432 log<level::DEBUG>("Validated in entry_1 of fruData",
Patrick Venture5c787212018-10-17 13:48:23 -0700433 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600434 }
435#endif
436
Oskar Senft69540862018-12-04 15:52:31 -0500437 if (!validateCrc)
438 {
439 // There's nothing else to do for this area.
440 return EXIT_SUCCESS;
441 }
442
vishwac93d6d42015-12-16 11:55:16 -0600443 // See if the calculated CRC matches with the embedded one.
444 // CRC to be calculated on all except the last one that is CRC itself.
Patrick Venture062e1452018-10-21 09:16:47 -0700445 checksum = calculateCRC(data, len - 1);
Patrick Venturec9508db2018-10-16 17:18:43 -0700446 if (checksum != data[len - 1])
vishwac93d6d42015-12-16 11:55:16 -0600447 {
448#ifdef __IPMI_DEBUG__
Patrick Venture5c787212018-10-17 13:48:23 -0700449 log<level::ERR>(
450 "Checksum mismatch",
451 entry("Calculated=0x%X", static_cast<uint32_t>(checksum)),
452 entry("Embedded=0x%X", static_cast<uint32_t>(data[len])));
vishwac93d6d42015-12-16 11:55:16 -0600453#endif
454 return rc;
455 }
456#ifdef __IPMI_DEBUG__
457 else
458 {
Patrick Venture5c787212018-10-17 13:48:23 -0700459 log<level::DEBUG>("Checksum matches");
vishwac93d6d42015-12-16 11:55:16 -0600460 }
461#endif
462
463 return EXIT_SUCCESS;
464}
465
Patrick Venture6cc89042018-10-21 14:26:39 -0700466/**
467 * Checks if a particular FRU area is populated or not.
468 *
469 * @param[in] reference to FRU area pointer
470 * @return true if the area is empty
471 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700472bool removeInvalidArea(const std::unique_ptr<IPMIFruArea>& fruArea)
Vishwa4be4b7a2015-10-31 22:55:50 -0500473{
Deepak Kodihalli89ededd2017-02-11 01:59:32 -0600474 // Filter the ones that are empty
Patrick Ventureef83b992018-10-21 09:32:44 -0700475 if (!(fruArea->getLength()))
vishwac93d6d42015-12-16 11:55:16 -0600476 {
477 return true;
478 }
479 return false;
480}
Vishwa4be4b7a2015-10-31 22:55:50 -0500481
Patrick Venture6cc89042018-10-21 14:26:39 -0700482/**
483 * Populates various FRU areas.
484 *
485 * @prereq : This must be called only after validating common header
486 * @param[in] fruData - pointer to the FRU bytes
487 * @param[in] dataLen - the length of the FRU data
488 * @param[in] fruAreaVec - the FRU area vector to update
489 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700490int ipmiPopulateFruAreas(uint8_t* fruData, const size_t dataLen,
491 FruAreaVector& fruAreaVec)
vishwac93d6d42015-12-16 11:55:16 -0600492{
Manojkiran Edabc5725d2024-06-17 11:50:54 +0530493 // Now walk the common header and see if the file size has at least the last
Patrick Ventureef83b992018-10-21 09:32:44 -0700494 // offset mentioned by the struct common_header. If the file size is less
Patrick Venture6cc89042018-10-21 14:26:39 -0700495 // than the offset of any if the FRU areas mentioned in the common header,
Patrick Ventureef83b992018-10-21 09:32:44 -0700496 // then we do not have a complete file.
497 for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET;
498 fruEntry < (sizeof(struct common_header) - 2); fruEntry++)
vishwac93d6d42015-12-16 11:55:16 -0600499 {
Patrick Venture2cdb0e32019-08-05 07:47:01 -0700500 int rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600501 // Actual offset in the payload is the offset mentioned in common header
Gunnar Millsc19b8132018-06-14 08:56:17 -0500502 // multiplied by 8. Common header is always the first 8 bytes.
Patrick Ventureef83b992018-10-21 09:32:44 -0700503 size_t areaOffset = fruData[fruEntry] * IPMI_EIGHT_BYTES;
504 if (areaOffset && (dataLen < (areaOffset + 2)))
vishwac93d6d42015-12-16 11:55:16 -0600505 {
506 // Our file size is less than what it needs to be. +2 because we are
Patrick Ventureef83b992018-10-21 09:32:44 -0700507 // using area len that is at 2 byte off areaOffset
Patrick Venture6cc89042018-10-21 14:26:39 -0700508 log<level::ERR>("FRU file is incomplete",
Patrick Ventureef83b992018-10-21 09:32:44 -0700509 entry("SIZE=%d", dataLen));
vishwac93d6d42015-12-16 11:55:16 -0600510 return rc;
511 }
Patrick Ventureef83b992018-10-21 09:32:44 -0700512 else if (areaOffset)
vishwac93d6d42015-12-16 11:55:16 -0600513 {
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000514 // Read 3 bytes to know the actual size of area.
515 uint8_t areaHeader[3] = {0};
Patrick Ventureef83b992018-10-21 09:32:44 -0700516 std::memcpy(areaHeader, &((uint8_t*)fruData)[areaOffset],
517 sizeof(areaHeader));
Vishwa4be4b7a2015-10-31 22:55:50 -0500518
Patrick Venture6cc89042018-10-21 14:26:39 -0700519 // Size of this area will be the 2nd byte in the FRU area header.
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000520 size_t areaLen;
521 if (fruEntry == IPMI_FRU_MULTI_OFFSET)
522 {
523 areaLen = areaHeader[2] + IPMI_FRU_MULTIREC_HDR_BYTES;
524 }
525 else
526 {
527 areaLen = areaHeader[1] * IPMI_EIGHT_BYTES;
528 }
529
Patrick Venture6cc89042018-10-21 14:26:39 -0700530 log<level::DEBUG>("FRU Data", entry("SIZE=%d", dataLen),
Patrick Ventureef83b992018-10-21 09:32:44 -0700531 entry("AREA OFFSET=%d", areaOffset),
532 entry("AREA_SIZE=%d", areaLen));
Vishwa4be4b7a2015-10-31 22:55:50 -0500533
vishwac93d6d42015-12-16 11:55:16 -0600534 // See if we really have that much buffer. We have area offset amd
535 // from there, the actual len.
Patrick Ventureef83b992018-10-21 09:32:44 -0700536 if (dataLen < (areaLen + areaOffset))
vishwac93d6d42015-12-16 11:55:16 -0600537 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700538 log<level::ERR>("Incomplete FRU file",
Patrick Ventureef83b992018-10-21 09:32:44 -0700539 entry("SIZE=%d", dataLen));
vishwac93d6d42015-12-16 11:55:16 -0600540 return rc;
541 }
542
Patrick Williamsa3cc2282024-08-16 15:20:14 -0400543 auto fruDataView =
544 std::span<uint8_t>(&fruData[areaOffset], areaLen);
545 auto areaData =
546 std::vector<uint8_t>(fruDataView.begin(), fruDataView.end());
vishwac93d6d42015-12-16 11:55:16 -0600547
Oskar Senft69540862018-12-04 15:52:31 -0500548 // Validate the CRC, but not for the internal use area, since its
549 // contents beyond the first byte are not defined in the spec and
550 // it may not end with a CRC byte.
551 bool validateCrc = fruEntry != IPMI_FRU_INTERNAL_OFFSET;
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000552
553 if (fruEntry == IPMI_FRU_MULTI_OFFSET)
554 {
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500555 rc = verifyFruMultiRecData(areaData.data(), areaLen,
556 validateCrc);
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000557 }
558 else
559 {
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500560 rc = verifyFruData(areaData.data(), areaLen, validateCrc);
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000561 }
562
Patrick Venturec9508db2018-10-16 17:18:43 -0700563 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600564 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700565 log<level::ERR>("Err validating FRU area",
Patrick Ventureef83b992018-10-21 09:32:44 -0700566 entry("OFFSET=%d", areaOffset));
vishwac93d6d42015-12-16 11:55:16 -0600567 return rc;
568 }
569 else
570 {
Oskar Senft69540862018-12-04 15:52:31 -0500571 log<level::DEBUG>("Successfully verified area.",
Patrick Ventureef83b992018-10-21 09:32:44 -0700572 entry("OFFSET=%d", areaOffset));
vishwac93d6d42015-12-16 11:55:16 -0600573 }
574
575 // We already have a vector that is passed to us containing all
576 // of the fields populated. Update the data portion now.
Patrick Ventureef83b992018-10-21 09:32:44 -0700577 for (auto& iter : fruAreaVec)
vishwac93d6d42015-12-16 11:55:16 -0600578 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700579 if (iter->getType() == getFruAreaType(fruEntry))
vishwac93d6d42015-12-16 11:55:16 -0600580 {
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500581 iter->setData(areaData.data(), areaLen);
vishwac93d6d42015-12-16 11:55:16 -0600582 }
583 }
Patrick Venture6cc89042018-10-21 14:26:39 -0700584 } // If we have FRU data present
Patrick Williamsa3cc2282024-08-16 15:20:14 -0400585 } // Walk struct common_header
vishwac93d6d42015-12-16 11:55:16 -0600586
Patrick Venture6cc89042018-10-21 14:26:39 -0700587 // Not all the fields will be populated in a FRU data. Mostly all cases will
vishwac93d6d42015-12-16 11:55:16 -0600588 // not have more than 2 or 3.
Patrick Ventureef83b992018-10-21 09:32:44 -0700589 fruAreaVec.erase(
590 std::remove_if(fruAreaVec.begin(), fruAreaVec.end(), removeInvalidArea),
591 fruAreaVec.end());
vishwac93d6d42015-12-16 11:55:16 -0600592
593 return EXIT_SUCCESS;
594}
595
Patrick Venture6cc89042018-10-21 14:26:39 -0700596/**
597 * Validates the FRU data per ipmi common header constructs.
598 * Returns with updated struct common_header and also file_size
599 *
600 * @param[in] fruData - the FRU data
601 * @param[in] dataLen - the length of the data
602 * @return non-zero on failure
603 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700604int ipmiValidateCommonHeader(const uint8_t* fruData, const size_t dataLen)
vishwac93d6d42015-12-16 11:55:16 -0600605{
606 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500607
Patrick Ventureef83b992018-10-21 09:32:44 -0700608 uint8_t commonHdr[sizeof(struct common_header)] = {0};
609 if (dataLen >= sizeof(commonHdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500610 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700611 std::memcpy(commonHdr, fruData, sizeof(commonHdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500612 }
613 else
614 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700615 log<level::ERR>("Incomplete FRU data file", entry("SIZE=%d", dataLen));
vishwac93d6d42015-12-16 11:55:16 -0600616 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500617 }
618
Patrick Venture6cc89042018-10-21 14:26:39 -0700619 // Verify the CRC and size
Oskar Senft69540862018-12-04 15:52:31 -0500620 rc = verifyFruData(commonHdr, sizeof(commonHdr), true);
Patrick Venturec9508db2018-10-16 17:18:43 -0700621 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500622 {
Patrick Venture5c787212018-10-17 13:48:23 -0700623 log<level::ERR>("Failed to validate common header");
vishwac93d6d42015-12-16 11:55:16 -0600624 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500625 }
626
vishwac93d6d42015-12-16 11:55:16 -0600627 return EXIT_SUCCESS;
628}
Vishwa4be4b7a2015-10-31 22:55:50 -0500629
Patrick Ventureef83b992018-10-21 09:32:44 -0700630int validateFRUArea(const uint8_t fruid, const char* fruFilename,
Patrick Williams5e8829e2022-07-22 19:26:53 -0500631 sdbusplus::bus_t& bus, const bool bmcOnlyFru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500632{
Patrick Ventureef83b992018-10-21 09:32:44 -0700633 size_t dataLen = 0;
634 size_t bytesRead = 0;
vishwac93d6d42015-12-16 11:55:16 -0600635 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500636
vishwac93d6d42015-12-16 11:55:16 -0600637 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
638 // are not used, keeping it here for completeness.
Patrick Ventureef83b992018-10-21 09:32:44 -0700639 FruAreaVector fruAreaVec;
Yi Li75c2d462016-04-11 16:57:46 +0800640
Patrick Ventureef83b992018-10-21 09:32:44 -0700641 for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET;
642 fruEntry < (sizeof(struct common_header) - 2); fruEntry++)
vishwac93d6d42015-12-16 11:55:16 -0600643 {
644 // Create an object and push onto a vector.
Patrick Ventureef83b992018-10-21 09:32:44 -0700645 std::unique_ptr<IPMIFruArea> fruArea = std::make_unique<IPMIFruArea>(
646 fruid, getFruAreaType(fruEntry), bmcOnlyFru);
vishwac93d6d42015-12-16 11:55:16 -0600647
648 // Physically being present
Patrick Ventureef83b992018-10-21 09:32:44 -0700649 bool present = access(fruFilename, F_OK) == 0;
650 fruArea->setPresent(present);
vishwac93d6d42015-12-16 11:55:16 -0600651
Patrick Ventureef83b992018-10-21 09:32:44 -0700652 fruAreaVec.emplace_back(std::move(fruArea));
vishwac93d6d42015-12-16 11:55:16 -0600653 }
654
Patrick Ventureef83b992018-10-21 09:32:44 -0700655 FILE* fruFilePointer = std::fopen(fruFilename, "rb");
656 if (fruFilePointer == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500657 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700658 log<level::ERR>("Unable to open FRU file",
Patrick Ventureef83b992018-10-21 09:32:44 -0700659 entry("FILE=%s", fruFilename),
Patrick Venture5c787212018-10-17 13:48:23 -0700660 entry("ERRNO=%s", std::strerror(errno)));
Patrick Ventureef83b992018-10-21 09:32:44 -0700661 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500662 }
663
vishwac93d6d42015-12-16 11:55:16 -0600664 // Get the size of the file to see if it meets minimum requirement
Patrick Ventureef83b992018-10-21 09:32:44 -0700665 if (std::fseek(fruFilePointer, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500666 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700667 log<level::ERR>("Unable to seek FRU file",
Patrick Ventureef83b992018-10-21 09:32:44 -0700668 entry("FILE=%s", fruFilename),
Patrick Venture5c787212018-10-17 13:48:23 -0700669 entry("ERRNO=%s", std::strerror(errno)));
Patrick Ventureef83b992018-10-21 09:32:44 -0700670 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500671 }
672
vishwac93d6d42015-12-16 11:55:16 -0600673 // Allocate a buffer to hold entire file content
Patrick Ventureef83b992018-10-21 09:32:44 -0700674 dataLen = std::ftell(fruFilePointer);
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500675
676 auto fruData = std::vector<uint8_t>(dataLen, 0);
Vishwa4be4b7a2015-10-31 22:55:50 -0500677
Patrick Ventureef83b992018-10-21 09:32:44 -0700678 std::rewind(fruFilePointer);
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500679 bytesRead = std::fread(fruData.data(), dataLen, 1, fruFilePointer);
Patrick Ventureef83b992018-10-21 09:32:44 -0700680 if (bytesRead != 1)
Vishwa4be4b7a2015-10-31 22:55:50 -0500681 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700682 log<level::ERR>("Failed reading FRU data.",
Patrick Ventureef83b992018-10-21 09:32:44 -0700683 entry("BYTESREAD=%d", bytesRead),
Patrick Venture5c787212018-10-17 13:48:23 -0700684 entry("ERRNO=%s", std::strerror(errno)));
Patrick Ventureef83b992018-10-21 09:32:44 -0700685 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500686 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500687
vishwac93d6d42015-12-16 11:55:16 -0600688 // We are done reading.
Patrick Ventureef83b992018-10-21 09:32:44 -0700689 std::fclose(fruFilePointer);
690 fruFilePointer = NULL;
vishwac93d6d42015-12-16 11:55:16 -0600691
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500692 rc = ipmiValidateCommonHeader(fruData.data(), dataLen);
Patrick Venturec9508db2018-10-16 17:18:43 -0700693 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500694 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700695 return cleanupError(fruFilePointer, fruAreaVec);
vishwac93d6d42015-12-16 11:55:16 -0600696 }
697
Patrick Venture6cc89042018-10-21 14:26:39 -0700698 // Now that we validated the common header, populate various FRU sections if
Patrick Venturec9508db2018-10-16 17:18:43 -0700699 // we have them here.
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500700 rc = ipmiPopulateFruAreas(fruData.data(), dataLen, fruAreaVec);
Patrick Venturec9508db2018-10-16 17:18:43 -0700701 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600702 {
Patrick Venture5c787212018-10-17 13:48:23 -0700703 log<level::ERR>("Populating FRU areas failed", entry("FRU=%d", fruid));
Patrick Ventureef83b992018-10-21 09:32:44 -0700704 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500705 }
706 else
707 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700708 log<level::DEBUG>("Populated FRU areas", entry("FILE=%s", fruFilename));
Vishwa4be4b7a2015-10-31 22:55:50 -0500709 }
710
vishwac93d6d42015-12-16 11:55:16 -0600711#ifdef __IPMI_DEBUG__
Patrick Ventureef83b992018-10-21 09:32:44 -0700712 for (const auto& iter : fruAreaVec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500713 {
Patrick Venture0e3a1c42018-10-20 21:15:41 -0700714 std::printf("FRU ID : [%d]\n", iter->getFruID());
715 std::printf("AREA NAME : [%s]\n", iter->getName());
716 std::printf("TYPE : [%d]\n", iter->getType());
717 std::printf("LEN : [%d]\n", iter->getLength());
Vishwa4be4b7a2015-10-31 22:55:50 -0500718 }
vishwac93d6d42015-12-16 11:55:16 -0600719#endif
720
721 // If the vector is populated with everything, then go ahead and update the
722 // inventory.
Patrick Ventureef83b992018-10-21 09:32:44 -0700723 if (!(fruAreaVec.empty()))
vishwac93d6d42015-12-16 11:55:16 -0600724 {
vishwac93d6d42015-12-16 11:55:16 -0600725#ifdef __IPMI_DEBUG__
Patrick Ventureef83b992018-10-21 09:32:44 -0700726 std::printf("\n SIZE of vector is : [%d] \n", fruAreaVec.size());
vishwac93d6d42015-12-16 11:55:16 -0600727#endif
Patrick Ventureef83b992018-10-21 09:32:44 -0700728 rc = updateInventory(fruAreaVec, bus);
Patrick Venturec9508db2018-10-16 17:18:43 -0700729 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600730 {
Patrick Venture5c787212018-10-17 13:48:23 -0700731 log<level::ERR>("Error updating inventory.");
vishwac93d6d42015-12-16 11:55:16 -0600732 }
733 }
734
735 // we are done with all that we wanted to do. This will do the job of
736 // calling any destructors too.
Patrick Ventureef83b992018-10-21 09:32:44 -0700737 fruAreaVec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500738
739 return rc;
740}