blob: c749013aabcc1d37a355e4fa8b167c883eadb016 [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
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
Patrick Venture6cc89042018-10-21 14:26:39 -070034/**
35 * Cleanup routine
36 * Must always be called as last reference to fruFilePointer.
37 *
38 * @param[in] fruFilePointer - FRU file pointer to close
39 * @param[in] fruAreaVec - vector of FRU areas
40 * @return -1
41 */
Patrick Ventureef83b992018-10-21 09:32:44 -070042int cleanupError(FILE* fruFilePointer, FruAreaVector& fruAreaVec)
Patrick Venture234b7352018-10-20 09:37:09 -070043{
Patrick Ventureef83b992018-10-21 09:32:44 -070044 if (fruFilePointer != NULL)
Patrick Venture234b7352018-10-20 09:37:09 -070045 {
Patrick Ventureef83b992018-10-21 09:32:44 -070046 std::fclose(fruFilePointer);
Patrick Venture234b7352018-10-20 09:37:09 -070047 }
48
Patrick Ventureef83b992018-10-21 09:32:44 -070049 if (!(fruAreaVec.empty()))
Patrick Venture234b7352018-10-20 09:37:09 -070050 {
Patrick Ventureef83b992018-10-21 09:32:44 -070051 fruAreaVec.clear();
Patrick Venture234b7352018-10-20 09:37:09 -070052 }
53
54 return -1;
55}
56
Patrick Venture6cc89042018-10-21 14:26:39 -070057/**
58 * Gets the value of the key from the FRU dictionary of the given section.
59 * FRU dictionary is parsed FRU data for all the sections.
60 *
61 * @param[in] section - FRU section name
62 * @param[in] key - key for secion
63 * @param[in] delimiter - delimiter for parsing custom fields
64 * @param[in] fruData - the FRU data to search for the section
65 * @return FRU value
66 */
Patrick Venturede8ea562018-10-20 09:44:19 -070067std::string getFRUValue(const std::string& section, const std::string& key,
68 const std::string& delimiter, IPMIFruInfo& fruData)
69{
70
71 auto minIndexValue = 0;
72 auto maxIndexValue = 0;
73 std::string fruValue = "";
74
75 if (section == "Board")
76 {
77 minIndexValue = OPENBMC_VPD_KEY_BOARD_MFG_DATE;
78 maxIndexValue = OPENBMC_VPD_KEY_BOARD_MAX;
79 }
80 else if (section == "Product")
81 {
82 minIndexValue = OPENBMC_VPD_KEY_PRODUCT_MFR;
83 maxIndexValue = OPENBMC_VPD_KEY_PRODUCT_MAX;
84 }
85 else if (section == "Chassis")
86 {
87 minIndexValue = OPENBMC_VPD_KEY_CHASSIS_TYPE;
88 maxIndexValue = OPENBMC_VPD_KEY_CHASSIS_MAX;
89 }
90
91 auto first = fruData.cbegin() + minIndexValue;
92 auto last = first + (maxIndexValue - minIndexValue) + 1;
93
Patrick Venturec7eecc12018-10-21 08:55:44 -070094 auto itr = std::find_if(first, last,
95 [&key](const auto& e) { return key == e.first; });
Patrick Venturede8ea562018-10-20 09:44:19 -070096
97 if (itr != last)
98 {
99 fruValue = itr->second;
100 }
101
102 // if the key is custom property then the value could be in two formats.
103 // 1) custom field 2 = "value".
104 // 2) custom field 2 = "key:value".
105 // if delimiter length = 0 i.e custom field 2 = "value"
106
107 constexpr auto customProp = "Custom Field";
108 if (key.find(customProp) != std::string::npos)
109 {
110 if (delimiter.length() > 0)
111 {
112 size_t delimiterpos = fruValue.find(delimiter);
113 if (delimiterpos != std::string::npos)
Patrick Venture740d8c02018-10-21 12:53:05 -0700114 {
Patrick Venturede8ea562018-10-20 09:44:19 -0700115 fruValue = fruValue.substr(delimiterpos + 1);
Patrick Venture740d8c02018-10-21 12:53:05 -0700116 }
Patrick Venturede8ea562018-10-20 09:44:19 -0700117 }
118 }
119 return fruValue;
120}
121
Patrick Venture6cc89042018-10-21 14:26:39 -0700122/**
123 * Get the inventory service from the mapper.
124 *
125 * @param[in] bus - sdbusplus handle to use for dbus call
126 * @param[in] intf - interface
127 * @param[in] path - the object path
128 * @return the dbus service that owns the interface for that path
129 */
Patrick Venture17baa272018-10-20 09:48:08 -0700130auto getService(sdbusplus::bus::bus& bus, const std::string& intf,
131 const std::string& path)
132{
133 auto mapperCall =
134 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
135 "/xyz/openbmc_project/object_mapper",
136 "xyz.openbmc_project.ObjectMapper", "GetObject");
137
138 mapperCall.append(path);
139 mapperCall.append(std::vector<std::string>({intf}));
140 std::map<std::string, std::vector<std::string>> mapperResponse;
141
142 try
143 {
144 auto mapperResponseMsg = bus.call(mapperCall);
145 mapperResponseMsg.read(mapperResponse);
146 }
Patrick Williams5f5e3842021-09-02 09:23:25 -0500147 catch (const sdbusplus::exception::exception& ex)
Patrick Venture17baa272018-10-20 09:48:08 -0700148 {
149 log<level::ERR>("Exception from sdbus call",
150 entry("WHAT=%s", ex.what()));
151 throw;
152 }
153
154 if (mapperResponse.begin() == mapperResponse.end())
155 {
156 throw std::runtime_error("ERROR in reading the mapper response");
157 }
158
159 return mapperResponse.begin()->first;
160}
161
Patrick Venture6cc89042018-10-21 14:26:39 -0700162/**
163 * Takes FRU data, invokes Parser for each FRU record area and updates
164 * inventory.
165 *
166 * @param[in] areaVector - vector of FRU areas
167 * @param[in] bus - handle to sdbus for calling methods, etc
168 * @return return non-zero of failure
169 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700170int updateInventory(FruAreaVector& areaVector, sdbusplus::bus::bus& bus)
Patrick Venture44a957d2018-10-20 09:41:29 -0700171{
172 // Generic error reporter
173 int rc = 0;
174 uint8_t fruid = 0;
175 IPMIFruInfo fruData;
176
177 // For each FRU area, extract the needed data , get it parsed and update
178 // the Inventory.
Patrick Ventureef83b992018-10-21 09:32:44 -0700179 for (const auto& fruArea : areaVector)
Patrick Venture44a957d2018-10-20 09:41:29 -0700180 {
Patrick Venturef22b36a2018-10-20 20:59:07 -0700181 fruid = fruArea->getFruID();
Patrick Venture44a957d2018-10-20 09:41:29 -0700182 // Fill the container with information
Patrick Venture355c5612018-10-22 08:49:36 -0700183 rc = parse_fru_area(fruArea->getType(),
184 static_cast<const void*>(fruArea->getData()),
185 fruArea->getLength(), fruData);
Patrick Venture44a957d2018-10-20 09:41:29 -0700186 if (rc < 0)
187 {
188 log<level::ERR>("Error parsing FRU records");
189 return rc;
190 }
191 } // END walking the vector of areas and updating
192
Patrick Venture6cc89042018-10-21 14:26:39 -0700193 // For each FRU we have the list of instances which needs to be updated.
Patrick Venture44a957d2018-10-20 09:41:29 -0700194 // Each instance object implements certain interfaces.
195 // Each Interface is having Dbus properties.
196 // Each Dbus Property would be having metaData(eg section,VpdPropertyName).
197
198 // Here we are just printing the object,interface and the properties.
199 // which needs to be called with the new inventory manager implementation.
Patrick Venture44a957d2018-10-20 09:41:29 -0700200 using namespace std::string_literals;
201 static const auto intf = "xyz.openbmc_project.Inventory.Manager"s;
202 static const auto path = "/xyz/openbmc_project/inventory"s;
203 std::string service;
204 try
205 {
206 service = getService(bus, intf, path);
207 }
208 catch (const std::exception& e)
209 {
210 std::cerr << e.what() << "\n";
211 return -1;
212 }
213
214 auto iter = frus.find(fruid);
215 if (iter == frus.end())
216 {
Patrick Venturede0a59e2019-06-18 09:49:27 -0700217 log<level::ERR>("Unable to find FRUID in generated list",
Patrick Venture44a957d2018-10-20 09:41:29 -0700218 entry("FRU=%d", static_cast<int>(fruid)));
219 return -1;
220 }
221
222 auto& instanceList = iter->second;
223 if (instanceList.size() <= 0)
224 {
225 log<level::DEBUG>("Object list empty for this FRU",
226 entry("FRU=%d", static_cast<int>(fruid)));
227 }
228
229 ObjectMap objects;
Patrick Venturec7eecc12018-10-21 08:55:44 -0700230 for (const auto& instance : instanceList)
Patrick Venture44a957d2018-10-20 09:41:29 -0700231 {
232 InterfaceMap interfaces;
233 const auto& extrasIter = extras.find(instance.path);
234
Patrick Venturec7eecc12018-10-21 08:55:44 -0700235 for (const auto& interfaceList : instance.interfaces)
Patrick Venture44a957d2018-10-20 09:41:29 -0700236 {
237 PropertyMap props; // store all the properties
Patrick Venturec7eecc12018-10-21 08:55:44 -0700238 for (const auto& properties : interfaceList.second)
Patrick Venture44a957d2018-10-20 09:41:29 -0700239 {
240 std::string value;
241 decltype(auto) pdata = properties.second;
242
243 if (!pdata.section.empty() && !pdata.property.empty())
244 {
245 value = getFRUValue(pdata.section, pdata.property,
246 pdata.delimiter, fruData);
247 }
248 props.emplace(std::move(properties.first), std::move(value));
249 }
250 // Check and update extra properties
251 if (extras.end() != extrasIter)
252 {
253 const auto& propsIter =
254 (extrasIter->second).find(interfaceList.first);
255 if ((extrasIter->second).end() != propsIter)
256 {
257 for (const auto& map : propsIter->second)
258 {
259 props.emplace(map.first, map.second);
260 }
261 }
262 }
263 interfaces.emplace(std::move(interfaceList.first),
264 std::move(props));
265 }
266
267 // Call the inventory manager
Patrick Venture9a528f22018-10-20 13:31:28 -0700268 sdbusplus::message::object_path objectPath = instance.path;
Patrick Venture44a957d2018-10-20 09:41:29 -0700269 // Check and update extra properties
270 if (extras.end() != extrasIter)
271 {
272 for (const auto& entry : extrasIter->second)
273 {
274 if (interfaces.end() == interfaces.find(entry.first))
275 {
276 interfaces.emplace(entry.first, entry.second);
277 }
278 }
279 }
Patrick Venture9a528f22018-10-20 13:31:28 -0700280 objects.emplace(objectPath, interfaces);
Patrick Venture44a957d2018-10-20 09:41:29 -0700281 }
282
283 auto pimMsg = bus.new_method_call(service.c_str(), path.c_str(),
284 intf.c_str(), "Notify");
285 pimMsg.append(std::move(objects));
286
287 try
288 {
289 auto inventoryMgrResponseMsg = bus.call(pimMsg);
290 }
Patrick Williams5f5e3842021-09-02 09:23:25 -0500291 catch (const sdbusplus::exception::exception& ex)
Patrick Venture44a957d2018-10-20 09:41:29 -0700292 {
Patrick Venture5b31e6b2019-08-05 07:37:19 -0700293 log<level::ERR>("Error in notify call", entry("WHAT=%s", ex.what()),
294 entry("SERVICE=%s", service.c_str()),
295 entry("PATH=%s", path.c_str()));
Patrick Venture44a957d2018-10-20 09:41:29 -0700296 return -1;
297 }
298
299 return rc;
300}
301
Patrick Venture234b7352018-10-20 09:37:09 -0700302} // namespace
303
Patrick Venture6cc89042018-10-21 14:26:39 -0700304/**
305 * Takes the pointer to stream of bytes and length and returns the 8 bit
306 * checksum. This algo is per IPMI V2.0 spec
307 *
308 * @param[in] data - data for running crc
309 * @param[in] len - the length over which to run the crc
310 * @return the CRC value
311 */
Patrick Venture062e1452018-10-21 09:16:47 -0700312unsigned char calculateCRC(const unsigned char* data, size_t len)
Vishwa4be4b7a2015-10-31 22:55:50 -0500313{
314 char crc = 0;
vishwac93d6d42015-12-16 11:55:16 -0600315 size_t byte = 0;
Vishwa4be4b7a2015-10-31 22:55:50 -0500316
Patrick Venturec9508db2018-10-16 17:18:43 -0700317 for (byte = 0; byte < len; byte++)
Vishwa4be4b7a2015-10-31 22:55:50 -0500318 {
319 crc += *data++;
320 }
vishwaf3ca3522015-12-02 10:35:13 -0600321
Patrick Venturec9508db2018-10-16 17:18:43 -0700322 return (-crc);
Vishwa4be4b7a2015-10-31 22:55:50 -0500323}
324
Patrick Venture6cc89042018-10-21 14:26:39 -0700325/**
326 * Accepts a FRU area offset into a commom header and tells which area it is.
327 *
328 * @param[in] areaOffset - offset to lookup the area type
329 * @return the ipmi_fru_area_type
330 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700331ipmi_fru_area_type getFruAreaType(uint8_t areaOffset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500332{
333 ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
334
Patrick Ventureef83b992018-10-21 09:32:44 -0700335 switch (areaOffset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500336 {
337 case IPMI_FRU_INTERNAL_OFFSET:
338 type = IPMI_FRU_AREA_INTERNAL_USE;
339 break;
340
341 case IPMI_FRU_CHASSIS_OFFSET:
342 type = IPMI_FRU_AREA_CHASSIS_INFO;
343 break;
344
345 case IPMI_FRU_BOARD_OFFSET:
346 type = IPMI_FRU_AREA_BOARD_INFO;
347 break;
348
349 case IPMI_FRU_PRODUCT_OFFSET:
350 type = IPMI_FRU_AREA_PRODUCT_INFO;
351 break;
352
353 case IPMI_FRU_MULTI_OFFSET:
354 type = IPMI_FRU_AREA_MULTI_RECORD;
355 break;
356
357 default:
358 type = IPMI_FRU_AREA_TYPE_MAX;
359 }
360
361 return type;
362}
363
Patrick Venture6cc89042018-10-21 14:26:39 -0700364/**
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000365 * Validates the data for multirecord fields and CRC if selected
366 *
367 * @param[in] data - the data to verify
368 * @param[in] len - the length of the region to verify
369 * @param[in] validateCrc - whether to validate the CRC
370 * @return non-zero on failure
371 */
372int verifyFruMultiRecData(const uint8_t* data, const size_t len,
373 bool validateCrc)
374{
375 uint8_t checksum = 0;
376 int rc = -1;
377
378 if (!validateCrc)
379 {
380 // There's nothing else to do for this area.
381 return EXIT_SUCCESS;
382 }
383
384 // As per the IPMI platform spec, byte[3] is the record checksum.
385 checksum = calculateCRC(data, len);
386 if (checksum != data[3])
387 {
388#ifdef __IPMI_DEBUG__
389 log<level::ERR>(
390 "Checksum mismatch",
391 entry("Calculated=0x%X", static_cast<uint32_t>(checksum)),
392 entry("Embedded=0x%X", static_cast<uint32_t>(data[3])));
393#endif
394 return rc;
395 }
396#ifdef __IPMI_DEBUG__
397 else
398 {
399 log<level::DEBUG>("Checksum matches");
400 }
401#endif
402
403 return EXIT_SUCCESS;
404}
405
406/**
Oskar Senft69540862018-12-04 15:52:31 -0500407 * Validates the data for mandatory fields and CRC if selected.
Patrick Venture6cc89042018-10-21 14:26:39 -0700408 *
409 * @param[in] data - the data to verify
410 * @param[in] len - the length of the region to verify
Oskar Senft69540862018-12-04 15:52:31 -0500411 * @param[in] validateCrc - whether to validate the CRC
Patrick Venture6cc89042018-10-21 14:26:39 -0700412 * @return non-zero on failure
413 */
Oskar Senft69540862018-12-04 15:52:31 -0500414int verifyFruData(const uint8_t* data, const size_t len, bool validateCrc)
vishwac93d6d42015-12-16 11:55:16 -0600415{
416 uint8_t checksum = 0;
417 int rc = -1;
418
419 // Validate for first byte to always have a value of [1]
Patrick Venturec9508db2018-10-16 17:18:43 -0700420 if (data[0] != IPMI_FRU_HDR_BYTE_ZERO)
vishwac93d6d42015-12-16 11:55:16 -0600421 {
Patrick Venture5c787212018-10-17 13:48:23 -0700422 log<level::ERR>("Invalid entry in byte-0",
423 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600424 return rc;
425 }
426#ifdef __IPMI_DEBUG__
427 else
428 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700429 log<level::DEBUG>("Validated in entry_1 of fruData",
Patrick Venture5c787212018-10-17 13:48:23 -0700430 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600431 }
432#endif
433
Oskar Senft69540862018-12-04 15:52:31 -0500434 if (!validateCrc)
435 {
436 // There's nothing else to do for this area.
437 return EXIT_SUCCESS;
438 }
439
vishwac93d6d42015-12-16 11:55:16 -0600440 // See if the calculated CRC matches with the embedded one.
441 // CRC to be calculated on all except the last one that is CRC itself.
Patrick Venture062e1452018-10-21 09:16:47 -0700442 checksum = calculateCRC(data, len - 1);
Patrick Venturec9508db2018-10-16 17:18:43 -0700443 if (checksum != data[len - 1])
vishwac93d6d42015-12-16 11:55:16 -0600444 {
445#ifdef __IPMI_DEBUG__
Patrick Venture5c787212018-10-17 13:48:23 -0700446 log<level::ERR>(
447 "Checksum mismatch",
448 entry("Calculated=0x%X", static_cast<uint32_t>(checksum)),
449 entry("Embedded=0x%X", static_cast<uint32_t>(data[len])));
vishwac93d6d42015-12-16 11:55:16 -0600450#endif
451 return rc;
452 }
453#ifdef __IPMI_DEBUG__
454 else
455 {
Patrick Venture5c787212018-10-17 13:48:23 -0700456 log<level::DEBUG>("Checksum matches");
vishwac93d6d42015-12-16 11:55:16 -0600457 }
458#endif
459
460 return EXIT_SUCCESS;
461}
462
Patrick Venture6cc89042018-10-21 14:26:39 -0700463/**
464 * Checks if a particular FRU area is populated or not.
465 *
466 * @param[in] reference to FRU area pointer
467 * @return true if the area is empty
468 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700469bool removeInvalidArea(const std::unique_ptr<IPMIFruArea>& fruArea)
Vishwa4be4b7a2015-10-31 22:55:50 -0500470{
Deepak Kodihalli89ededd2017-02-11 01:59:32 -0600471 // Filter the ones that are empty
Patrick Ventureef83b992018-10-21 09:32:44 -0700472 if (!(fruArea->getLength()))
vishwac93d6d42015-12-16 11:55:16 -0600473 {
474 return true;
475 }
476 return false;
477}
Vishwa4be4b7a2015-10-31 22:55:50 -0500478
Patrick Venture6cc89042018-10-21 14:26:39 -0700479/**
480 * Populates various FRU areas.
481 *
482 * @prereq : This must be called only after validating common header
483 * @param[in] fruData - pointer to the FRU bytes
484 * @param[in] dataLen - the length of the FRU data
485 * @param[in] fruAreaVec - the FRU area vector to update
486 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700487int ipmiPopulateFruAreas(uint8_t* fruData, const size_t dataLen,
488 FruAreaVector& fruAreaVec)
vishwac93d6d42015-12-16 11:55:16 -0600489{
vishwac93d6d42015-12-16 11:55:16 -0600490 // Now walk the common header and see if the file size has atleast the last
Patrick Ventureef83b992018-10-21 09:32:44 -0700491 // offset mentioned by the struct common_header. If the file size is less
Patrick Venture6cc89042018-10-21 14:26:39 -0700492 // than the offset of any if the FRU areas mentioned in the common header,
Patrick Ventureef83b992018-10-21 09:32:44 -0700493 // then we do not have a complete file.
494 for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET;
495 fruEntry < (sizeof(struct common_header) - 2); fruEntry++)
vishwac93d6d42015-12-16 11:55:16 -0600496 {
Patrick Venture2cdb0e32019-08-05 07:47:01 -0700497 int rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600498 // Actual offset in the payload is the offset mentioned in common header
Gunnar Millsc19b8132018-06-14 08:56:17 -0500499 // multiplied by 8. Common header is always the first 8 bytes.
Patrick Ventureef83b992018-10-21 09:32:44 -0700500 size_t areaOffset = fruData[fruEntry] * IPMI_EIGHT_BYTES;
501 if (areaOffset && (dataLen < (areaOffset + 2)))
vishwac93d6d42015-12-16 11:55:16 -0600502 {
503 // Our file size is less than what it needs to be. +2 because we are
Patrick Ventureef83b992018-10-21 09:32:44 -0700504 // using area len that is at 2 byte off areaOffset
Patrick Venture6cc89042018-10-21 14:26:39 -0700505 log<level::ERR>("FRU file is incomplete",
Patrick Ventureef83b992018-10-21 09:32:44 -0700506 entry("SIZE=%d", dataLen));
vishwac93d6d42015-12-16 11:55:16 -0600507 return rc;
508 }
Patrick Ventureef83b992018-10-21 09:32:44 -0700509 else if (areaOffset)
vishwac93d6d42015-12-16 11:55:16 -0600510 {
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000511 // Read 3 bytes to know the actual size of area.
512 uint8_t areaHeader[3] = {0};
Patrick Ventureef83b992018-10-21 09:32:44 -0700513 std::memcpy(areaHeader, &((uint8_t*)fruData)[areaOffset],
514 sizeof(areaHeader));
Vishwa4be4b7a2015-10-31 22:55:50 -0500515
Patrick Venture6cc89042018-10-21 14:26:39 -0700516 // Size of this area will be the 2nd byte in the FRU area header.
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000517 size_t areaLen;
518 if (fruEntry == IPMI_FRU_MULTI_OFFSET)
519 {
520 areaLen = areaHeader[2] + IPMI_FRU_MULTIREC_HDR_BYTES;
521 }
522 else
523 {
524 areaLen = areaHeader[1] * IPMI_EIGHT_BYTES;
525 }
526
Patrick Ventureef83b992018-10-21 09:32:44 -0700527 uint8_t areaData[areaLen] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500528
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
542 // Save off the data.
Patrick Ventureef83b992018-10-21 09:32:44 -0700543 std::memcpy(areaData, &((uint8_t*)fruData)[areaOffset], areaLen);
vishwac93d6d42015-12-16 11:55:16 -0600544
Oskar Senft69540862018-12-04 15:52:31 -0500545 // Validate the CRC, but not for the internal use area, since its
546 // contents beyond the first byte are not defined in the spec and
547 // it may not end with a CRC byte.
548 bool validateCrc = fruEntry != IPMI_FRU_INTERNAL_OFFSET;
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000549
550 if (fruEntry == IPMI_FRU_MULTI_OFFSET)
551 {
552 rc = verifyFruMultiRecData(areaData, areaLen, validateCrc);
553 }
554 else
555 {
556 rc = verifyFruData(areaData, areaLen, validateCrc);
557 }
558
Patrick Venturec9508db2018-10-16 17:18:43 -0700559 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600560 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700561 log<level::ERR>("Err validating FRU area",
Patrick Ventureef83b992018-10-21 09:32:44 -0700562 entry("OFFSET=%d", areaOffset));
vishwac93d6d42015-12-16 11:55:16 -0600563 return rc;
564 }
565 else
566 {
Oskar Senft69540862018-12-04 15:52:31 -0500567 log<level::DEBUG>("Successfully verified area.",
Patrick Ventureef83b992018-10-21 09:32:44 -0700568 entry("OFFSET=%d", areaOffset));
vishwac93d6d42015-12-16 11:55:16 -0600569 }
570
571 // We already have a vector that is passed to us containing all
572 // of the fields populated. Update the data portion now.
Patrick Ventureef83b992018-10-21 09:32:44 -0700573 for (auto& iter : fruAreaVec)
vishwac93d6d42015-12-16 11:55:16 -0600574 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700575 if (iter->getType() == getFruAreaType(fruEntry))
vishwac93d6d42015-12-16 11:55:16 -0600576 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700577 iter->setData(areaData, areaLen);
vishwac93d6d42015-12-16 11:55:16 -0600578 }
579 }
Patrick Venture6cc89042018-10-21 14:26:39 -0700580 } // If we have FRU data present
Patrick Ventureef83b992018-10-21 09:32:44 -0700581 } // Walk struct common_header
vishwac93d6d42015-12-16 11:55:16 -0600582
Patrick Venture6cc89042018-10-21 14:26:39 -0700583 // Not all the fields will be populated in a FRU data. Mostly all cases will
vishwac93d6d42015-12-16 11:55:16 -0600584 // not have more than 2 or 3.
Patrick Ventureef83b992018-10-21 09:32:44 -0700585 fruAreaVec.erase(
586 std::remove_if(fruAreaVec.begin(), fruAreaVec.end(), removeInvalidArea),
587 fruAreaVec.end());
vishwac93d6d42015-12-16 11:55:16 -0600588
589 return EXIT_SUCCESS;
590}
591
Patrick Venture6cc89042018-10-21 14:26:39 -0700592/**
593 * Validates the FRU data per ipmi common header constructs.
594 * Returns with updated struct common_header and also file_size
595 *
596 * @param[in] fruData - the FRU data
597 * @param[in] dataLen - the length of the data
598 * @return non-zero on failure
599 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700600int ipmiValidateCommonHeader(const uint8_t* fruData, const size_t dataLen)
vishwac93d6d42015-12-16 11:55:16 -0600601{
602 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500603
Patrick Ventureef83b992018-10-21 09:32:44 -0700604 uint8_t commonHdr[sizeof(struct common_header)] = {0};
605 if (dataLen >= sizeof(commonHdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500606 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700607 std::memcpy(commonHdr, fruData, sizeof(commonHdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500608 }
609 else
610 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700611 log<level::ERR>("Incomplete FRU data file", entry("SIZE=%d", dataLen));
vishwac93d6d42015-12-16 11:55:16 -0600612 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500613 }
614
Patrick Venture6cc89042018-10-21 14:26:39 -0700615 // Verify the CRC and size
Oskar Senft69540862018-12-04 15:52:31 -0500616 rc = verifyFruData(commonHdr, sizeof(commonHdr), true);
Patrick Venturec9508db2018-10-16 17:18:43 -0700617 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500618 {
Patrick Venture5c787212018-10-17 13:48:23 -0700619 log<level::ERR>("Failed to validate common header");
vishwac93d6d42015-12-16 11:55:16 -0600620 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500621 }
622
vishwac93d6d42015-12-16 11:55:16 -0600623 return EXIT_SUCCESS;
624}
Vishwa4be4b7a2015-10-31 22:55:50 -0500625
Patrick Ventureef83b992018-10-21 09:32:44 -0700626int validateFRUArea(const uint8_t fruid, const char* fruFilename,
627 sdbusplus::bus::bus& bus, const bool bmcOnlyFru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500628{
Patrick Ventureef83b992018-10-21 09:32:44 -0700629 size_t dataLen = 0;
630 size_t bytesRead = 0;
vishwac93d6d42015-12-16 11:55:16 -0600631 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500632
vishwac93d6d42015-12-16 11:55:16 -0600633 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
634 // are not used, keeping it here for completeness.
Patrick Ventureef83b992018-10-21 09:32:44 -0700635 FruAreaVector fruAreaVec;
Yi Li75c2d462016-04-11 16:57:46 +0800636
Patrick Ventureef83b992018-10-21 09:32:44 -0700637 for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET;
638 fruEntry < (sizeof(struct common_header) - 2); fruEntry++)
vishwac93d6d42015-12-16 11:55:16 -0600639 {
640 // Create an object and push onto a vector.
Patrick Ventureef83b992018-10-21 09:32:44 -0700641 std::unique_ptr<IPMIFruArea> fruArea = std::make_unique<IPMIFruArea>(
642 fruid, getFruAreaType(fruEntry), bmcOnlyFru);
vishwac93d6d42015-12-16 11:55:16 -0600643
644 // Physically being present
Patrick Ventureef83b992018-10-21 09:32:44 -0700645 bool present = access(fruFilename, F_OK) == 0;
646 fruArea->setPresent(present);
vishwac93d6d42015-12-16 11:55:16 -0600647
Patrick Ventureef83b992018-10-21 09:32:44 -0700648 fruAreaVec.emplace_back(std::move(fruArea));
vishwac93d6d42015-12-16 11:55:16 -0600649 }
650
Patrick Ventureef83b992018-10-21 09:32:44 -0700651 FILE* fruFilePointer = std::fopen(fruFilename, "rb");
652 if (fruFilePointer == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500653 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700654 log<level::ERR>("Unable to open FRU file",
Patrick Ventureef83b992018-10-21 09:32:44 -0700655 entry("FILE=%s", fruFilename),
Patrick Venture5c787212018-10-17 13:48:23 -0700656 entry("ERRNO=%s", std::strerror(errno)));
Patrick Ventureef83b992018-10-21 09:32:44 -0700657 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500658 }
659
vishwac93d6d42015-12-16 11:55:16 -0600660 // Get the size of the file to see if it meets minimum requirement
Patrick Ventureef83b992018-10-21 09:32:44 -0700661 if (std::fseek(fruFilePointer, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500662 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700663 log<level::ERR>("Unable to seek FRU file",
Patrick Ventureef83b992018-10-21 09:32:44 -0700664 entry("FILE=%s", fruFilename),
Patrick Venture5c787212018-10-17 13:48:23 -0700665 entry("ERRNO=%s", std::strerror(errno)));
Patrick Ventureef83b992018-10-21 09:32:44 -0700666 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500667 }
668
vishwac93d6d42015-12-16 11:55:16 -0600669 // Allocate a buffer to hold entire file content
Patrick Ventureef83b992018-10-21 09:32:44 -0700670 dataLen = std::ftell(fruFilePointer);
671 uint8_t fruData[dataLen] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500672
Patrick Ventureef83b992018-10-21 09:32:44 -0700673 std::rewind(fruFilePointer);
674 bytesRead = std::fread(fruData, dataLen, 1, fruFilePointer);
675 if (bytesRead != 1)
Vishwa4be4b7a2015-10-31 22:55:50 -0500676 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700677 log<level::ERR>("Failed reading FRU data.",
Patrick Ventureef83b992018-10-21 09:32:44 -0700678 entry("BYTESREAD=%d", bytesRead),
Patrick Venture5c787212018-10-17 13:48:23 -0700679 entry("ERRNO=%s", std::strerror(errno)));
Patrick Ventureef83b992018-10-21 09:32:44 -0700680 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500681 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500682
vishwac93d6d42015-12-16 11:55:16 -0600683 // We are done reading.
Patrick Ventureef83b992018-10-21 09:32:44 -0700684 std::fclose(fruFilePointer);
685 fruFilePointer = NULL;
vishwac93d6d42015-12-16 11:55:16 -0600686
Patrick Ventureef83b992018-10-21 09:32:44 -0700687 rc = ipmiValidateCommonHeader(fruData, dataLen);
Patrick Venturec9508db2018-10-16 17:18:43 -0700688 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500689 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700690 return cleanupError(fruFilePointer, fruAreaVec);
vishwac93d6d42015-12-16 11:55:16 -0600691 }
692
Patrick Venture6cc89042018-10-21 14:26:39 -0700693 // Now that we validated the common header, populate various FRU sections if
Patrick Venturec9508db2018-10-16 17:18:43 -0700694 // we have them here.
Patrick Ventureef83b992018-10-21 09:32:44 -0700695 rc = ipmiPopulateFruAreas(fruData, dataLen, fruAreaVec);
Patrick Venturec9508db2018-10-16 17:18:43 -0700696 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600697 {
Patrick Venture5c787212018-10-17 13:48:23 -0700698 log<level::ERR>("Populating FRU areas failed", entry("FRU=%d", fruid));
Patrick Ventureef83b992018-10-21 09:32:44 -0700699 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500700 }
701 else
702 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700703 log<level::DEBUG>("Populated FRU areas", entry("FILE=%s", fruFilename));
Vishwa4be4b7a2015-10-31 22:55:50 -0500704 }
705
vishwac93d6d42015-12-16 11:55:16 -0600706#ifdef __IPMI_DEBUG__
Patrick Ventureef83b992018-10-21 09:32:44 -0700707 for (const auto& iter : fruAreaVec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500708 {
Patrick Venture0e3a1c42018-10-20 21:15:41 -0700709 std::printf("FRU ID : [%d]\n", iter->getFruID());
710 std::printf("AREA NAME : [%s]\n", iter->getName());
711 std::printf("TYPE : [%d]\n", iter->getType());
712 std::printf("LEN : [%d]\n", iter->getLength());
Vishwa4be4b7a2015-10-31 22:55:50 -0500713 }
vishwac93d6d42015-12-16 11:55:16 -0600714#endif
715
716 // If the vector is populated with everything, then go ahead and update the
717 // inventory.
Patrick Ventureef83b992018-10-21 09:32:44 -0700718 if (!(fruAreaVec.empty()))
vishwac93d6d42015-12-16 11:55:16 -0600719 {
720
721#ifdef __IPMI_DEBUG__
Patrick Ventureef83b992018-10-21 09:32:44 -0700722 std::printf("\n SIZE of vector is : [%d] \n", fruAreaVec.size());
vishwac93d6d42015-12-16 11:55:16 -0600723#endif
Patrick Ventureef83b992018-10-21 09:32:44 -0700724 rc = updateInventory(fruAreaVec, bus);
Patrick Venturec9508db2018-10-16 17:18:43 -0700725 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600726 {
Patrick Venture5c787212018-10-17 13:48:23 -0700727 log<level::ERR>("Error updating inventory.");
vishwac93d6d42015-12-16 11:55:16 -0600728 }
729 }
730
731 // we are done with all that we wanted to do. This will do the job of
732 // calling any destructors too.
Patrick Ventureef83b992018-10-21 09:32:44 -0700733 fruAreaVec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500734
735 return rc;
736}