102ba8843SPatrick Venture #include "writefrudata.hpp"
202ba8843SPatrick Venture
35c2bd5edSPatrick Venture #include "fru_area.hpp"
4c9508db8SPatrick Venture #include "frup.hpp"
5c9508db8SPatrick Venture #include "types.hpp"
6c9508db8SPatrick Venture
78ab5784aSWilliam A. Kennington III #include <ipmid/api.h>
8155c34fbSMatthew Barth #include <unistd.h>
9c9508db8SPatrick Venture
10cfa96afaSPatrick Williams #include <phosphor-logging/log.hpp>
11cfa96afaSPatrick Williams #include <sdbusplus/bus.hpp>
12cfa96afaSPatrick Williams
13c9508db8SPatrick Venture #include <algorithm>
14bd2d8690SPatrick Williams #include <array>
155c787214SPatrick Venture #include <cstdio>
165c787214SPatrick Venture #include <cstring>
17c9508db8SPatrick Venture #include <exception>
18c9508db8SPatrick Venture #include <fstream>
19155c34fbSMatthew Barth #include <iostream>
20b390c0eaSPatrick Venture #include <map>
21155c34fbSMatthew Barth #include <memory>
22bd2d8690SPatrick Williams #include <span>
23c9508db8SPatrick Venture #include <sstream>
24c9508db8SPatrick Venture #include <vector>
25155c34fbSMatthew Barth
26788bd1f5SDeepak Kodihalli using namespace ipmi::vpd;
275739ac3bSPatrick Venture using namespace phosphor::logging;
28788bd1f5SDeepak Kodihalli
29cb0d4e5eSRatan Gupta extern const FruMap frus;
30788bd1f5SDeepak Kodihalli extern const std::map<Path, InterfaceMap> extras;
31cb0d4e5eSRatan Gupta
3219bea9aeSPatrick Venture using FruAreaVector = std::vector<std::unique_ptr<IPMIFruArea>>;
3319bea9aeSPatrick Venture
34234b735dSPatrick Venture namespace
35234b735dSPatrick Venture {
36234b735dSPatrick Venture
376cc89044SPatrick Venture /**
386cc89044SPatrick Venture * Cleanup routine
396cc89044SPatrick Venture * Must always be called as last reference to fruFilePointer.
406cc89044SPatrick Venture *
416cc89044SPatrick Venture * @param[in] fruFilePointer - FRU file pointer to close
426cc89044SPatrick Venture * @param[in] fruAreaVec - vector of FRU areas
436cc89044SPatrick Venture * @return -1
446cc89044SPatrick Venture */
cleanupError(FILE * fruFilePointer,FruAreaVector & fruAreaVec)45ef83b993SPatrick Venture int cleanupError(FILE* fruFilePointer, FruAreaVector& fruAreaVec)
46234b735dSPatrick Venture {
47*883aa420SJayanth Othayoth if (fruFilePointer != nullptr)
48234b735dSPatrick Venture {
49ef83b993SPatrick Venture std::fclose(fruFilePointer);
50234b735dSPatrick Venture }
51234b735dSPatrick Venture
52ef83b993SPatrick Venture if (!(fruAreaVec.empty()))
53234b735dSPatrick Venture {
54ef83b993SPatrick Venture fruAreaVec.clear();
55234b735dSPatrick Venture }
56234b735dSPatrick Venture
57234b735dSPatrick Venture return -1;
58234b735dSPatrick Venture }
59234b735dSPatrick Venture
606cc89044SPatrick Venture /**
616cc89044SPatrick Venture * Gets the value of the key from the FRU dictionary of the given section.
626cc89044SPatrick Venture * FRU dictionary is parsed FRU data for all the sections.
636cc89044SPatrick Venture *
646cc89044SPatrick Venture * @param[in] section - FRU section name
65bc5725d0SManojkiran Eda * @param[in] key - key for section
666cc89044SPatrick Venture * @param[in] delimiter - delimiter for parsing custom fields
676cc89044SPatrick Venture * @param[in] fruData - the FRU data to search for the section
686cc89044SPatrick Venture * @return FRU value
696cc89044SPatrick Venture */
getFRUValue(const std::string & section,const std::string & key,const std::string & delimiter,IPMIFruInfo & fruData)70de8ea568SPatrick Venture std::string getFRUValue(const std::string& section, const std::string& key,
71de8ea568SPatrick Venture const std::string& delimiter, IPMIFruInfo& fruData)
72de8ea568SPatrick Venture {
73de8ea568SPatrick Venture auto minIndexValue = 0;
74de8ea568SPatrick Venture auto maxIndexValue = 0;
75de8ea568SPatrick Venture std::string fruValue = "";
76de8ea568SPatrick Venture
77de8ea568SPatrick Venture if (section == "Board")
78de8ea568SPatrick Venture {
79de8ea568SPatrick Venture minIndexValue = OPENBMC_VPD_KEY_BOARD_MFG_DATE;
80de8ea568SPatrick Venture maxIndexValue = OPENBMC_VPD_KEY_BOARD_MAX;
81de8ea568SPatrick Venture }
82de8ea568SPatrick Venture else if (section == "Product")
83de8ea568SPatrick Venture {
84de8ea568SPatrick Venture minIndexValue = OPENBMC_VPD_KEY_PRODUCT_MFR;
85de8ea568SPatrick Venture maxIndexValue = OPENBMC_VPD_KEY_PRODUCT_MAX;
86de8ea568SPatrick Venture }
87de8ea568SPatrick Venture else if (section == "Chassis")
88de8ea568SPatrick Venture {
89de8ea568SPatrick Venture minIndexValue = OPENBMC_VPD_KEY_CHASSIS_TYPE;
90de8ea568SPatrick Venture maxIndexValue = OPENBMC_VPD_KEY_CHASSIS_MAX;
91de8ea568SPatrick Venture }
92de8ea568SPatrick Venture
93de8ea568SPatrick Venture auto first = fruData.cbegin() + minIndexValue;
94de8ea568SPatrick Venture auto last = first + (maxIndexValue - minIndexValue) + 1;
95de8ea568SPatrick Venture
96a3cc2286SPatrick Williams auto itr = std::find_if(first, last, [&key](const auto& e) {
97a3cc2286SPatrick Williams return key == e.first;
98a3cc2286SPatrick Williams });
99de8ea568SPatrick Venture
100de8ea568SPatrick Venture if (itr != last)
101de8ea568SPatrick Venture {
102de8ea568SPatrick Venture fruValue = itr->second;
103de8ea568SPatrick Venture }
104de8ea568SPatrick Venture
105de8ea568SPatrick Venture // if the key is custom property then the value could be in two formats.
106de8ea568SPatrick Venture // 1) custom field 2 = "value".
107de8ea568SPatrick Venture // 2) custom field 2 = "key:value".
108de8ea568SPatrick Venture // if delimiter length = 0 i.e custom field 2 = "value"
109de8ea568SPatrick Venture
110de8ea568SPatrick Venture constexpr auto customProp = "Custom Field";
111de8ea568SPatrick Venture if (key.find(customProp) != std::string::npos)
112de8ea568SPatrick Venture {
113de8ea568SPatrick Venture if (delimiter.length() > 0)
114de8ea568SPatrick Venture {
115de8ea568SPatrick Venture size_t delimiterpos = fruValue.find(delimiter);
116de8ea568SPatrick Venture if (delimiterpos != std::string::npos)
117740d8c00SPatrick Venture {
118de8ea568SPatrick Venture fruValue = fruValue.substr(delimiterpos + 1);
119de8ea568SPatrick Venture }
120de8ea568SPatrick Venture }
121740d8c00SPatrick Venture }
122de8ea568SPatrick Venture return fruValue;
123de8ea568SPatrick Venture }
124de8ea568SPatrick Venture
1256cc89044SPatrick Venture /**
1266cc89044SPatrick Venture * Get the inventory service from the mapper.
1276cc89044SPatrick Venture *
1286cc89044SPatrick Venture * @param[in] bus - sdbusplus handle to use for dbus call
1296cc89044SPatrick Venture * @param[in] intf - interface
1306cc89044SPatrick Venture * @param[in] path - the object path
1316cc89044SPatrick Venture * @return the dbus service that owns the interface for that path
1326cc89044SPatrick Venture */
getService(sdbusplus::bus_t & bus,const std::string & intf,const std::string & path)1335e8829e8SPatrick Williams auto getService(sdbusplus::bus_t& bus, const std::string& intf,
13417baa271SPatrick Venture const std::string& path)
13517baa271SPatrick Venture {
136a3cc2286SPatrick Williams auto mapperCall =
137a3cc2286SPatrick Williams bus.new_method_call("xyz.openbmc_project.ObjectMapper",
13817baa271SPatrick Venture "/xyz/openbmc_project/object_mapper",
139a3cc2286SPatrick Williams "xyz.openbmc_project.ObjectMapper", "GetObject");
14017baa271SPatrick Venture
14117baa271SPatrick Venture mapperCall.append(path);
14217baa271SPatrick Venture mapperCall.append(std::vector<std::string>({intf}));
14317baa271SPatrick Venture std::map<std::string, std::vector<std::string>> mapperResponse;
14417baa271SPatrick Venture
14517baa271SPatrick Venture try
14617baa271SPatrick Venture {
14717baa271SPatrick Venture auto mapperResponseMsg = bus.call(mapperCall);
14817baa271SPatrick Venture mapperResponseMsg.read(mapperResponse);
14917baa271SPatrick Venture }
1505e8829e8SPatrick Williams catch (const sdbusplus::exception_t& ex)
15117baa271SPatrick Venture {
15217baa271SPatrick Venture log<level::ERR>("Exception from sdbus call",
15317baa271SPatrick Venture entry("WHAT=%s", ex.what()));
15417baa271SPatrick Venture throw;
15517baa271SPatrick Venture }
15617baa271SPatrick Venture
15717baa271SPatrick Venture if (mapperResponse.begin() == mapperResponse.end())
15817baa271SPatrick Venture {
15917baa271SPatrick Venture throw std::runtime_error("ERROR in reading the mapper response");
16017baa271SPatrick Venture }
16117baa271SPatrick Venture
16217baa271SPatrick Venture return mapperResponse.begin()->first;
16317baa271SPatrick Venture }
16417baa271SPatrick Venture
1656cc89044SPatrick Venture /**
1666cc89044SPatrick Venture * Takes FRU data, invokes Parser for each FRU record area and updates
1676cc89044SPatrick Venture * inventory.
1686cc89044SPatrick Venture *
1696cc89044SPatrick Venture * @param[in] areaVector - vector of FRU areas
1706cc89044SPatrick Venture * @param[in] bus - handle to sdbus for calling methods, etc
1716cc89044SPatrick Venture * @return return non-zero of failure
1726cc89044SPatrick Venture */
updateInventory(FruAreaVector & areaVector,sdbusplus::bus_t & bus)1735e8829e8SPatrick Williams int updateInventory(FruAreaVector& areaVector, sdbusplus::bus_t& bus)
17444a957d8SPatrick Venture {
17544a957d8SPatrick Venture // Generic error reporter
17644a957d8SPatrick Venture int rc = 0;
17744a957d8SPatrick Venture uint8_t fruid = 0;
17844a957d8SPatrick Venture IPMIFruInfo fruData;
17944a957d8SPatrick Venture
18044a957d8SPatrick Venture // For each FRU area, extract the needed data , get it parsed and update
18144a957d8SPatrick Venture // the Inventory.
182ef83b993SPatrick Venture for (const auto& fruArea : areaVector)
18344a957d8SPatrick Venture {
184f22b36a0SPatrick Venture fruid = fruArea->getFruID();
18544a957d8SPatrick Venture // Fill the container with information
186355c561dSPatrick Venture rc = parse_fru_area(fruArea->getType(),
187355c561dSPatrick Venture static_cast<const void*>(fruArea->getData()),
188355c561dSPatrick Venture fruArea->getLength(), fruData);
18944a957d8SPatrick Venture if (rc < 0)
19044a957d8SPatrick Venture {
19144a957d8SPatrick Venture log<level::ERR>("Error parsing FRU records");
19244a957d8SPatrick Venture return rc;
19344a957d8SPatrick Venture }
19444a957d8SPatrick Venture } // END walking the vector of areas and updating
19544a957d8SPatrick Venture
1966cc89044SPatrick Venture // For each FRU we have the list of instances which needs to be updated.
19744a957d8SPatrick Venture // Each instance object implements certain interfaces.
19844a957d8SPatrick Venture // Each Interface is having Dbus properties.
19944a957d8SPatrick Venture // Each Dbus Property would be having metaData(eg section,VpdPropertyName).
20044a957d8SPatrick Venture
20144a957d8SPatrick Venture // Here we are just printing the object,interface and the properties.
20244a957d8SPatrick Venture // which needs to be called with the new inventory manager implementation.
20344a957d8SPatrick Venture using namespace std::string_literals;
20444a957d8SPatrick Venture static const auto intf = "xyz.openbmc_project.Inventory.Manager"s;
20544a957d8SPatrick Venture static const auto path = "/xyz/openbmc_project/inventory"s;
20644a957d8SPatrick Venture std::string service;
20744a957d8SPatrick Venture try
20844a957d8SPatrick Venture {
20944a957d8SPatrick Venture service = getService(bus, intf, path);
21044a957d8SPatrick Venture }
21144a957d8SPatrick Venture catch (const std::exception& e)
21244a957d8SPatrick Venture {
21344a957d8SPatrick Venture std::cerr << e.what() << "\n";
21444a957d8SPatrick Venture return -1;
21544a957d8SPatrick Venture }
21644a957d8SPatrick Venture
21744a957d8SPatrick Venture auto iter = frus.find(fruid);
21844a957d8SPatrick Venture if (iter == frus.end())
21944a957d8SPatrick Venture {
220de0a59eeSPatrick Venture log<level::ERR>("Unable to find FRUID in generated list",
22144a957d8SPatrick Venture entry("FRU=%d", static_cast<int>(fruid)));
22244a957d8SPatrick Venture return -1;
22344a957d8SPatrick Venture }
22444a957d8SPatrick Venture
22544a957d8SPatrick Venture auto& instanceList = iter->second;
22644a957d8SPatrick Venture if (instanceList.size() <= 0)
22744a957d8SPatrick Venture {
22844a957d8SPatrick Venture log<level::DEBUG>("Object list empty for this FRU",
22944a957d8SPatrick Venture entry("FRU=%d", static_cast<int>(fruid)));
23044a957d8SPatrick Venture }
23144a957d8SPatrick Venture
23244a957d8SPatrick Venture ObjectMap objects;
233c7eecc19SPatrick Venture for (const auto& instance : instanceList)
23444a957d8SPatrick Venture {
23544a957d8SPatrick Venture InterfaceMap interfaces;
23644a957d8SPatrick Venture const auto& extrasIter = extras.find(instance.path);
23744a957d8SPatrick Venture
238c7eecc19SPatrick Venture for (const auto& interfaceList : instance.interfaces)
23944a957d8SPatrick Venture {
24044a957d8SPatrick Venture PropertyMap props; // store all the properties
241c7eecc19SPatrick Venture for (const auto& properties : interfaceList.second)
24244a957d8SPatrick Venture {
24344a957d8SPatrick Venture std::string value;
24444a957d8SPatrick Venture decltype(auto) pdata = properties.second;
24544a957d8SPatrick Venture
24644a957d8SPatrick Venture if (!pdata.section.empty() && !pdata.property.empty())
24744a957d8SPatrick Venture {
24844a957d8SPatrick Venture value = getFRUValue(pdata.section, pdata.property,
24944a957d8SPatrick Venture pdata.delimiter, fruData);
25044a957d8SPatrick Venture }
25144a957d8SPatrick Venture props.emplace(std::move(properties.first), std::move(value));
25244a957d8SPatrick Venture }
25344a957d8SPatrick Venture // Check and update extra properties
25444a957d8SPatrick Venture if (extras.end() != extrasIter)
25544a957d8SPatrick Venture {
25644a957d8SPatrick Venture const auto& propsIter =
25744a957d8SPatrick Venture (extrasIter->second).find(interfaceList.first);
25844a957d8SPatrick Venture if ((extrasIter->second).end() != propsIter)
25944a957d8SPatrick Venture {
26044a957d8SPatrick Venture for (const auto& map : propsIter->second)
26144a957d8SPatrick Venture {
26244a957d8SPatrick Venture props.emplace(map.first, map.second);
26344a957d8SPatrick Venture }
26444a957d8SPatrick Venture }
26544a957d8SPatrick Venture }
26644a957d8SPatrick Venture interfaces.emplace(std::move(interfaceList.first),
26744a957d8SPatrick Venture std::move(props));
26844a957d8SPatrick Venture }
26944a957d8SPatrick Venture
27044a957d8SPatrick Venture // Call the inventory manager
2719a528f24SPatrick Venture sdbusplus::message::object_path objectPath = instance.path;
27244a957d8SPatrick Venture // Check and update extra properties
27344a957d8SPatrick Venture if (extras.end() != extrasIter)
27444a957d8SPatrick Venture {
27544a957d8SPatrick Venture for (const auto& entry : extrasIter->second)
27644a957d8SPatrick Venture {
27744a957d8SPatrick Venture if (interfaces.end() == interfaces.find(entry.first))
27844a957d8SPatrick Venture {
27944a957d8SPatrick Venture interfaces.emplace(entry.first, entry.second);
28044a957d8SPatrick Venture }
28144a957d8SPatrick Venture }
28244a957d8SPatrick Venture }
2839a528f24SPatrick Venture objects.emplace(objectPath, interfaces);
28444a957d8SPatrick Venture }
28544a957d8SPatrick Venture
28644a957d8SPatrick Venture auto pimMsg = bus.new_method_call(service.c_str(), path.c_str(),
28744a957d8SPatrick Venture intf.c_str(), "Notify");
28844a957d8SPatrick Venture pimMsg.append(std::move(objects));
28944a957d8SPatrick Venture
29044a957d8SPatrick Venture try
29144a957d8SPatrick Venture {
29244a957d8SPatrick Venture auto inventoryMgrResponseMsg = bus.call(pimMsg);
29344a957d8SPatrick Venture }
2945e8829e8SPatrick Williams catch (const sdbusplus::exception_t& ex)
29544a957d8SPatrick Venture {
2965b31e6b0SPatrick Venture log<level::ERR>("Error in notify call", entry("WHAT=%s", ex.what()),
2975b31e6b0SPatrick Venture entry("SERVICE=%s", service.c_str()),
2985b31e6b0SPatrick Venture entry("PATH=%s", path.c_str()));
29944a957d8SPatrick Venture return -1;
30044a957d8SPatrick Venture }
30144a957d8SPatrick Venture
30244a957d8SPatrick Venture return rc;
30344a957d8SPatrick Venture }
30444a957d8SPatrick Venture
305234b735dSPatrick Venture } // namespace
306234b735dSPatrick Venture
3076cc89044SPatrick Venture /**
3086cc89044SPatrick Venture * Takes the pointer to stream of bytes and length and returns the 8 bit
3096cc89044SPatrick Venture * checksum. This algo is per IPMI V2.0 spec
3106cc89044SPatrick Venture *
3116cc89044SPatrick Venture * @param[in] data - data for running crc
3126cc89044SPatrick Venture * @param[in] len - the length over which to run the crc
3136cc89044SPatrick Venture * @return the CRC value
3146cc89044SPatrick Venture */
calculateCRC(const unsigned char * data,size_t len)315062e1451SPatrick Venture unsigned char calculateCRC(const unsigned char* data, size_t len)
316155c34fbSMatthew Barth {
317155c34fbSMatthew Barth char crc = 0;
318155c34fbSMatthew Barth size_t byte = 0;
319155c34fbSMatthew Barth
320155c34fbSMatthew Barth for (byte = 0; byte < len; byte++)
321155c34fbSMatthew Barth {
322155c34fbSMatthew Barth crc += *data++;
323155c34fbSMatthew Barth }
324155c34fbSMatthew Barth
325155c34fbSMatthew Barth return (-crc);
326155c34fbSMatthew Barth }
327155c34fbSMatthew Barth
3286cc89044SPatrick Venture /**
329bc5725d0SManojkiran Eda * Accepts a FRU area offset into a common header and tells which area it is.
3306cc89044SPatrick Venture *
3316cc89044SPatrick Venture * @param[in] areaOffset - offset to lookup the area type
3326cc89044SPatrick Venture * @return the ipmi_fru_area_type
3336cc89044SPatrick Venture */
getFruAreaType(uint8_t areaOffset)334ef83b993SPatrick Venture ipmi_fru_area_type getFruAreaType(uint8_t areaOffset)
335155c34fbSMatthew Barth {
336155c34fbSMatthew Barth ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
337155c34fbSMatthew Barth
338ef83b993SPatrick Venture switch (areaOffset)
339155c34fbSMatthew Barth {
340155c34fbSMatthew Barth case IPMI_FRU_INTERNAL_OFFSET:
341155c34fbSMatthew Barth type = IPMI_FRU_AREA_INTERNAL_USE;
342155c34fbSMatthew Barth break;
343155c34fbSMatthew Barth
344155c34fbSMatthew Barth case IPMI_FRU_CHASSIS_OFFSET:
345155c34fbSMatthew Barth type = IPMI_FRU_AREA_CHASSIS_INFO;
346155c34fbSMatthew Barth break;
347155c34fbSMatthew Barth
348155c34fbSMatthew Barth case IPMI_FRU_BOARD_OFFSET:
349155c34fbSMatthew Barth type = IPMI_FRU_AREA_BOARD_INFO;
350155c34fbSMatthew Barth break;
351155c34fbSMatthew Barth
352155c34fbSMatthew Barth case IPMI_FRU_PRODUCT_OFFSET:
353155c34fbSMatthew Barth type = IPMI_FRU_AREA_PRODUCT_INFO;
354155c34fbSMatthew Barth break;
355155c34fbSMatthew Barth
356155c34fbSMatthew Barth case IPMI_FRU_MULTI_OFFSET:
357155c34fbSMatthew Barth type = IPMI_FRU_AREA_MULTI_RECORD;
358155c34fbSMatthew Barth break;
359155c34fbSMatthew Barth
360155c34fbSMatthew Barth default:
361155c34fbSMatthew Barth type = IPMI_FRU_AREA_TYPE_MAX;
362155c34fbSMatthew Barth }
363155c34fbSMatthew Barth
364155c34fbSMatthew Barth return type;
365155c34fbSMatthew Barth }
366155c34fbSMatthew Barth
3676cc89044SPatrick Venture /**
368472d3e2fSThang Q. Nguyen * Validates the data for multirecord fields and CRC if selected
369472d3e2fSThang Q. Nguyen *
370472d3e2fSThang Q. Nguyen * @param[in] data - the data to verify
371472d3e2fSThang Q. Nguyen * @param[in] len - the length of the region to verify
372472d3e2fSThang Q. Nguyen * @param[in] validateCrc - whether to validate the CRC
373472d3e2fSThang Q. Nguyen * @return non-zero on failure
374472d3e2fSThang Q. Nguyen */
verifyFruMultiRecData(const uint8_t * data,const size_t len,bool validateCrc)375472d3e2fSThang Q. Nguyen int verifyFruMultiRecData(const uint8_t* data, const size_t len,
376472d3e2fSThang Q. Nguyen bool validateCrc)
377472d3e2fSThang Q. Nguyen {
378472d3e2fSThang Q. Nguyen uint8_t checksum = 0;
379472d3e2fSThang Q. Nguyen int rc = -1;
380472d3e2fSThang Q. Nguyen
381472d3e2fSThang Q. Nguyen if (!validateCrc)
382472d3e2fSThang Q. Nguyen {
383472d3e2fSThang Q. Nguyen // There's nothing else to do for this area.
384472d3e2fSThang Q. Nguyen return EXIT_SUCCESS;
385472d3e2fSThang Q. Nguyen }
386472d3e2fSThang Q. Nguyen
387472d3e2fSThang Q. Nguyen // As per the IPMI platform spec, byte[3] is the record checksum.
388472d3e2fSThang Q. Nguyen checksum = calculateCRC(data, len);
389472d3e2fSThang Q. Nguyen if (checksum != data[3])
390472d3e2fSThang Q. Nguyen {
391472d3e2fSThang Q. Nguyen #ifdef __IPMI_DEBUG__
392472d3e2fSThang Q. Nguyen log<level::ERR>(
393472d3e2fSThang Q. Nguyen "Checksum mismatch",
394472d3e2fSThang Q. Nguyen entry("Calculated=0x%X", static_cast<uint32_t>(checksum)),
395472d3e2fSThang Q. Nguyen entry("Embedded=0x%X", static_cast<uint32_t>(data[3])));
396472d3e2fSThang Q. Nguyen #endif
397472d3e2fSThang Q. Nguyen return rc;
398472d3e2fSThang Q. Nguyen }
399472d3e2fSThang Q. Nguyen #ifdef __IPMI_DEBUG__
400472d3e2fSThang Q. Nguyen else
401472d3e2fSThang Q. Nguyen {
402472d3e2fSThang Q. Nguyen log<level::DEBUG>("Checksum matches");
403472d3e2fSThang Q. Nguyen }
404472d3e2fSThang Q. Nguyen #endif
405472d3e2fSThang Q. Nguyen
406472d3e2fSThang Q. Nguyen return EXIT_SUCCESS;
407472d3e2fSThang Q. Nguyen }
408472d3e2fSThang Q. Nguyen
409472d3e2fSThang Q. Nguyen /**
41069540861SOskar Senft * Validates the data for mandatory fields and CRC if selected.
4116cc89044SPatrick Venture *
4126cc89044SPatrick Venture * @param[in] data - the data to verify
4136cc89044SPatrick Venture * @param[in] len - the length of the region to verify
41469540861SOskar Senft * @param[in] validateCrc - whether to validate the CRC
4156cc89044SPatrick Venture * @return non-zero on failure
4166cc89044SPatrick Venture */
verifyFruData(const uint8_t * data,const size_t len,bool validateCrc)41769540861SOskar Senft int verifyFruData(const uint8_t* data, const size_t len, bool validateCrc)
418155c34fbSMatthew Barth {
419155c34fbSMatthew Barth uint8_t checksum = 0;
420155c34fbSMatthew Barth int rc = -1;
421155c34fbSMatthew Barth
422155c34fbSMatthew Barth // Validate for first byte to always have a value of [1]
423155c34fbSMatthew Barth if (data[0] != IPMI_FRU_HDR_BYTE_ZERO)
424155c34fbSMatthew Barth {
4255c787214SPatrick Venture log<level::ERR>("Invalid entry in byte-0",
4265c787214SPatrick Venture entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
427155c34fbSMatthew Barth return rc;
428155c34fbSMatthew Barth }
429155c34fbSMatthew Barth #ifdef __IPMI_DEBUG__
430155c34fbSMatthew Barth else
431155c34fbSMatthew Barth {
432ef83b993SPatrick Venture log<level::DEBUG>("Validated in entry_1 of fruData",
4335c787214SPatrick Venture entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
434155c34fbSMatthew Barth }
435155c34fbSMatthew Barth #endif
436155c34fbSMatthew Barth
43769540861SOskar Senft if (!validateCrc)
43869540861SOskar Senft {
43969540861SOskar Senft // There's nothing else to do for this area.
44069540861SOskar Senft return EXIT_SUCCESS;
44169540861SOskar Senft }
44269540861SOskar Senft
443155c34fbSMatthew Barth // See if the calculated CRC matches with the embedded one.
444155c34fbSMatthew Barth // CRC to be calculated on all except the last one that is CRC itself.
445062e1451SPatrick Venture checksum = calculateCRC(data, len - 1);
446155c34fbSMatthew Barth if (checksum != data[len - 1])
447155c34fbSMatthew Barth {
448155c34fbSMatthew Barth #ifdef __IPMI_DEBUG__
4495c787214SPatrick Venture log<level::ERR>(
4505c787214SPatrick Venture "Checksum mismatch",
4515c787214SPatrick Venture entry("Calculated=0x%X", static_cast<uint32_t>(checksum)),
4525c787214SPatrick Venture entry("Embedded=0x%X", static_cast<uint32_t>(data[len])));
453155c34fbSMatthew Barth #endif
454155c34fbSMatthew Barth return rc;
455155c34fbSMatthew Barth }
456155c34fbSMatthew Barth #ifdef __IPMI_DEBUG__
457155c34fbSMatthew Barth else
458155c34fbSMatthew Barth {
4595c787214SPatrick Venture log<level::DEBUG>("Checksum matches");
460155c34fbSMatthew Barth }
461155c34fbSMatthew Barth #endif
462155c34fbSMatthew Barth
463155c34fbSMatthew Barth return EXIT_SUCCESS;
464155c34fbSMatthew Barth }
465155c34fbSMatthew Barth
4666cc89044SPatrick Venture /**
4676cc89044SPatrick Venture * Checks if a particular FRU area is populated or not.
4686cc89044SPatrick Venture *
4696cc89044SPatrick Venture * @param[in] reference to FRU area pointer
4706cc89044SPatrick Venture * @return true if the area is empty
4716cc89044SPatrick Venture */
removeInvalidArea(const std::unique_ptr<IPMIFruArea> & fruArea)472ef83b993SPatrick Venture bool removeInvalidArea(const std::unique_ptr<IPMIFruArea>& fruArea)
473155c34fbSMatthew Barth {
47489ededd0SDeepak Kodihalli // Filter the ones that are empty
475ef83b993SPatrick Venture if (!(fruArea->getLength()))
476155c34fbSMatthew Barth {
477155c34fbSMatthew Barth return true;
478155c34fbSMatthew Barth }
479155c34fbSMatthew Barth return false;
480155c34fbSMatthew Barth }
481155c34fbSMatthew Barth
4826cc89044SPatrick Venture /**
4836cc89044SPatrick Venture * Populates various FRU areas.
4846cc89044SPatrick Venture *
4856cc89044SPatrick Venture * @prereq : This must be called only after validating common header
4866cc89044SPatrick Venture * @param[in] fruData - pointer to the FRU bytes
4876cc89044SPatrick Venture * @param[in] dataLen - the length of the FRU data
4886cc89044SPatrick Venture * @param[in] fruAreaVec - the FRU area vector to update
4896cc89044SPatrick Venture */
ipmiPopulateFruAreas(uint8_t * fruData,const size_t dataLen,FruAreaVector & fruAreaVec)490ef83b993SPatrick Venture int ipmiPopulateFruAreas(uint8_t* fruData, const size_t dataLen,
491ef83b993SPatrick Venture FruAreaVector& fruAreaVec)
492155c34fbSMatthew Barth {
493155c34fbSMatthew Barth // Now walk the common header and see if the file size has at least the last
494ef83b993SPatrick Venture // offset mentioned by the struct common_header. If the file size is less
4956cc89044SPatrick Venture // than the offset of any if the FRU areas mentioned in the common header,
496ef83b993SPatrick Venture // then we do not have a complete file.
497ef83b993SPatrick Venture for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET;
498ef83b993SPatrick Venture fruEntry < (sizeof(struct common_header) - 2); fruEntry++)
499155c34fbSMatthew Barth {
5002cdb0e3fSPatrick Venture int rc = -1;
501155c34fbSMatthew Barth // Actual offset in the payload is the offset mentioned in common header
502c19b813eSGunnar Mills // multiplied by 8. Common header is always the first 8 bytes.
503ef83b993SPatrick Venture size_t areaOffset = fruData[fruEntry] * IPMI_EIGHT_BYTES;
504ef83b993SPatrick Venture if (areaOffset && (dataLen < (areaOffset + 2)))
505155c34fbSMatthew Barth {
506155c34fbSMatthew Barth // Our file size is less than what it needs to be. +2 because we are
507ef83b993SPatrick Venture // using area len that is at 2 byte off areaOffset
5086cc89044SPatrick Venture log<level::ERR>("FRU file is incomplete",
509ef83b993SPatrick Venture entry("SIZE=%d", dataLen));
510155c34fbSMatthew Barth return rc;
511155c34fbSMatthew Barth }
512ef83b993SPatrick Venture else if (areaOffset)
513155c34fbSMatthew Barth {
514472d3e2fSThang Q. Nguyen // Read 3 bytes to know the actual size of area.
515472d3e2fSThang Q. Nguyen uint8_t areaHeader[3] = {0};
516ef83b993SPatrick Venture std::memcpy(areaHeader, &((uint8_t*)fruData)[areaOffset],
517ef83b993SPatrick Venture sizeof(areaHeader));
518155c34fbSMatthew Barth
5196cc89044SPatrick Venture // Size of this area will be the 2nd byte in the FRU area header.
520472d3e2fSThang Q. Nguyen size_t areaLen;
521472d3e2fSThang Q. Nguyen if (fruEntry == IPMI_FRU_MULTI_OFFSET)
522472d3e2fSThang Q. Nguyen {
523472d3e2fSThang Q. Nguyen areaLen = areaHeader[2] + IPMI_FRU_MULTIREC_HDR_BYTES;
524472d3e2fSThang Q. Nguyen }
525472d3e2fSThang Q. Nguyen else
526472d3e2fSThang Q. Nguyen {
527472d3e2fSThang Q. Nguyen areaLen = areaHeader[1] * IPMI_EIGHT_BYTES;
528472d3e2fSThang Q. Nguyen }
529472d3e2fSThang Q. Nguyen
5306cc89044SPatrick Venture log<level::DEBUG>("FRU Data", entry("SIZE=%d", dataLen),
531ef83b993SPatrick Venture entry("AREA OFFSET=%d", areaOffset),
532ef83b993SPatrick Venture entry("AREA_SIZE=%d", areaLen));
533155c34fbSMatthew Barth
534155c34fbSMatthew Barth // See if we really have that much buffer. We have area offset amd
535155c34fbSMatthew Barth // from there, the actual len.
536ef83b993SPatrick Venture if (dataLen < (areaLen + areaOffset))
537155c34fbSMatthew Barth {
5386cc89044SPatrick Venture log<level::ERR>("Incomplete FRU file",
539ef83b993SPatrick Venture entry("SIZE=%d", dataLen));
540155c34fbSMatthew Barth return rc;
541155c34fbSMatthew Barth }
542155c34fbSMatthew Barth
543a3cc2286SPatrick Williams auto fruDataView =
544a3cc2286SPatrick Williams std::span<uint8_t>(&fruData[areaOffset], areaLen);
545a3cc2286SPatrick Williams auto areaData =
546a3cc2286SPatrick Williams std::vector<uint8_t>(fruDataView.begin(), fruDataView.end());
547155c34fbSMatthew Barth
54869540861SOskar Senft // Validate the CRC, but not for the internal use area, since its
54969540861SOskar Senft // contents beyond the first byte are not defined in the spec and
55069540861SOskar Senft // it may not end with a CRC byte.
55169540861SOskar Senft bool validateCrc = fruEntry != IPMI_FRU_INTERNAL_OFFSET;
552472d3e2fSThang Q. Nguyen
553472d3e2fSThang Q. Nguyen if (fruEntry == IPMI_FRU_MULTI_OFFSET)
554472d3e2fSThang Q. Nguyen {
555bd2d8690SPatrick Williams rc = verifyFruMultiRecData(areaData.data(), areaLen,
556bd2d8690SPatrick Williams validateCrc);
557472d3e2fSThang Q. Nguyen }
558472d3e2fSThang Q. Nguyen else
559472d3e2fSThang Q. Nguyen {
560bd2d8690SPatrick Williams rc = verifyFruData(areaData.data(), areaLen, validateCrc);
561472d3e2fSThang Q. Nguyen }
562472d3e2fSThang Q. Nguyen
563155c34fbSMatthew Barth if (rc < 0)
564155c34fbSMatthew Barth {
5656cc89044SPatrick Venture log<level::ERR>("Err validating FRU area",
566ef83b993SPatrick Venture entry("OFFSET=%d", areaOffset));
567155c34fbSMatthew Barth return rc;
568155c34fbSMatthew Barth }
569155c34fbSMatthew Barth else
570155c34fbSMatthew Barth {
57169540861SOskar Senft log<level::DEBUG>("Successfully verified area.",
572ef83b993SPatrick Venture entry("OFFSET=%d", areaOffset));
573155c34fbSMatthew Barth }
574155c34fbSMatthew Barth
575155c34fbSMatthew Barth // We already have a vector that is passed to us containing all
576155c34fbSMatthew Barth // of the fields populated. Update the data portion now.
577ef83b993SPatrick Venture for (auto& iter : fruAreaVec)
578155c34fbSMatthew Barth {
579ef83b993SPatrick Venture if (iter->getType() == getFruAreaType(fruEntry))
580155c34fbSMatthew Barth {
581bd2d8690SPatrick Williams iter->setData(areaData.data(), areaLen);
582155c34fbSMatthew Barth }
583155c34fbSMatthew Barth }
5846cc89044SPatrick Venture } // If we have FRU data present
585ef83b993SPatrick Venture } // Walk struct common_header
586155c34fbSMatthew Barth
5876cc89044SPatrick Venture // Not all the fields will be populated in a FRU data. Mostly all cases will
588155c34fbSMatthew Barth // not have more than 2 or 3.
589ef83b993SPatrick Venture fruAreaVec.erase(
590ef83b993SPatrick Venture std::remove_if(fruAreaVec.begin(), fruAreaVec.end(), removeInvalidArea),
591ef83b993SPatrick Venture fruAreaVec.end());
592155c34fbSMatthew Barth
593155c34fbSMatthew Barth return EXIT_SUCCESS;
594155c34fbSMatthew Barth }
595155c34fbSMatthew Barth
5966cc89044SPatrick Venture /**
5976cc89044SPatrick Venture * Validates the FRU data per ipmi common header constructs.
5986cc89044SPatrick Venture * Returns with updated struct common_header and also file_size
5996cc89044SPatrick Venture *
6006cc89044SPatrick Venture * @param[in] fruData - the FRU data
6016cc89044SPatrick Venture * @param[in] dataLen - the length of the data
6026cc89044SPatrick Venture * @return non-zero on failure
6036cc89044SPatrick Venture */
ipmiValidateCommonHeader(const uint8_t * fruData,const size_t dataLen)604ef83b993SPatrick Venture int ipmiValidateCommonHeader(const uint8_t* fruData, const size_t dataLen)
605155c34fbSMatthew Barth {
606155c34fbSMatthew Barth int rc = -1;
607155c34fbSMatthew Barth
608ef83b993SPatrick Venture uint8_t commonHdr[sizeof(struct common_header)] = {0};
609ef83b993SPatrick Venture if (dataLen >= sizeof(commonHdr))
610155c34fbSMatthew Barth {
611ef83b993SPatrick Venture std::memcpy(commonHdr, fruData, sizeof(commonHdr));
612155c34fbSMatthew Barth }
613155c34fbSMatthew Barth else
614155c34fbSMatthew Barth {
6156cc89044SPatrick Venture log<level::ERR>("Incomplete FRU data file", entry("SIZE=%d", dataLen));
616155c34fbSMatthew Barth return rc;
617155c34fbSMatthew Barth }
618155c34fbSMatthew Barth
6196cc89044SPatrick Venture // Verify the CRC and size
62069540861SOskar Senft rc = verifyFruData(commonHdr, sizeof(commonHdr), true);
621155c34fbSMatthew Barth if (rc < 0)
622155c34fbSMatthew Barth {
6235c787214SPatrick Venture log<level::ERR>("Failed to validate common header");
624155c34fbSMatthew Barth return rc;
625155c34fbSMatthew Barth }
626155c34fbSMatthew Barth
627155c34fbSMatthew Barth return EXIT_SUCCESS;
628155c34fbSMatthew Barth }
629155c34fbSMatthew Barth
validateFRUArea(const uint8_t fruid,const char * fruFilename,sdbusplus::bus_t & bus,const bool bmcOnlyFru)630ef83b993SPatrick Venture int validateFRUArea(const uint8_t fruid, const char* fruFilename,
6315e8829e8SPatrick Williams sdbusplus::bus_t& bus, const bool bmcOnlyFru)
632155c34fbSMatthew Barth {
633ef83b993SPatrick Venture size_t dataLen = 0;
634ef83b993SPatrick Venture size_t bytesRead = 0;
635155c34fbSMatthew Barth int rc = -1;
636155c34fbSMatthew Barth
637155c34fbSMatthew Barth // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
638155c34fbSMatthew Barth // are not used, keeping it here for completeness.
639ef83b993SPatrick Venture FruAreaVector fruAreaVec;
640155c34fbSMatthew Barth
641ef83b993SPatrick Venture for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET;
642ef83b993SPatrick Venture fruEntry < (sizeof(struct common_header) - 2); fruEntry++)
643155c34fbSMatthew Barth {
644155c34fbSMatthew Barth // Create an object and push onto a vector.
645ef83b993SPatrick Venture std::unique_ptr<IPMIFruArea> fruArea = std::make_unique<IPMIFruArea>(
646ef83b993SPatrick Venture fruid, getFruAreaType(fruEntry), bmcOnlyFru);
647155c34fbSMatthew Barth
648155c34fbSMatthew Barth // Physically being present
649ef83b993SPatrick Venture bool present = access(fruFilename, F_OK) == 0;
650ef83b993SPatrick Venture fruArea->setPresent(present);
651155c34fbSMatthew Barth
652ef83b993SPatrick Venture fruAreaVec.emplace_back(std::move(fruArea));
653155c34fbSMatthew Barth }
654155c34fbSMatthew Barth
655ef83b993SPatrick Venture FILE* fruFilePointer = std::fopen(fruFilename, "rb");
656*883aa420SJayanth Othayoth if (fruFilePointer == nullptr)
657155c34fbSMatthew Barth {
6586cc89044SPatrick Venture log<level::ERR>("Unable to open FRU file",
659ef83b993SPatrick Venture entry("FILE=%s", fruFilename),
6605c787214SPatrick Venture entry("ERRNO=%s", std::strerror(errno)));
661ef83b993SPatrick Venture return cleanupError(fruFilePointer, fruAreaVec);
662155c34fbSMatthew Barth }
663155c34fbSMatthew Barth
664155c34fbSMatthew Barth // Get the size of the file to see if it meets minimum requirement
665ef83b993SPatrick Venture if (std::fseek(fruFilePointer, 0, SEEK_END))
666155c34fbSMatthew Barth {
6676cc89044SPatrick Venture log<level::ERR>("Unable to seek FRU file",
668ef83b993SPatrick Venture entry("FILE=%s", fruFilename),
6695c787214SPatrick Venture entry("ERRNO=%s", std::strerror(errno)));
670ef83b993SPatrick Venture return cleanupError(fruFilePointer, fruAreaVec);
671155c34fbSMatthew Barth }
672155c34fbSMatthew Barth
673155c34fbSMatthew Barth // Allocate a buffer to hold entire file content
674ef83b993SPatrick Venture dataLen = std::ftell(fruFilePointer);
675bd2d8690SPatrick Williams
676bd2d8690SPatrick Williams auto fruData = std::vector<uint8_t>(dataLen, 0);
677155c34fbSMatthew Barth
678ef83b993SPatrick Venture std::rewind(fruFilePointer);
679bd2d8690SPatrick Williams bytesRead = std::fread(fruData.data(), dataLen, 1, fruFilePointer);
680ef83b993SPatrick Venture if (bytesRead != 1)
681155c34fbSMatthew Barth {
6826cc89044SPatrick Venture log<level::ERR>("Failed reading FRU data.",
683ef83b993SPatrick Venture entry("BYTESREAD=%d", bytesRead),
6845c787214SPatrick Venture entry("ERRNO=%s", std::strerror(errno)));
685ef83b993SPatrick Venture return cleanupError(fruFilePointer, fruAreaVec);
686155c34fbSMatthew Barth }
687155c34fbSMatthew Barth
688155c34fbSMatthew Barth // We are done reading.
689ef83b993SPatrick Venture std::fclose(fruFilePointer);
690*883aa420SJayanth Othayoth fruFilePointer = nullptr;
691155c34fbSMatthew Barth
692bd2d8690SPatrick Williams rc = ipmiValidateCommonHeader(fruData.data(), dataLen);
693155c34fbSMatthew Barth if (rc < 0)
694155c34fbSMatthew Barth {
695ef83b993SPatrick Venture return cleanupError(fruFilePointer, fruAreaVec);
696155c34fbSMatthew Barth }
697155c34fbSMatthew Barth
6986cc89044SPatrick Venture // Now that we validated the common header, populate various FRU sections if
699c9508db8SPatrick Venture // we have them here.
700bd2d8690SPatrick Williams rc = ipmiPopulateFruAreas(fruData.data(), dataLen, fruAreaVec);
701155c34fbSMatthew Barth if (rc < 0)
702155c34fbSMatthew Barth {
7035c787214SPatrick Venture log<level::ERR>("Populating FRU areas failed", entry("FRU=%d", fruid));
704ef83b993SPatrick Venture return cleanupError(fruFilePointer, fruAreaVec);
705155c34fbSMatthew Barth }
706155c34fbSMatthew Barth else
707155c34fbSMatthew Barth {
708ef83b993SPatrick Venture log<level::DEBUG>("Populated FRU areas", entry("FILE=%s", fruFilename));
709155c34fbSMatthew Barth }
710155c34fbSMatthew Barth
711155c34fbSMatthew Barth #ifdef __IPMI_DEBUG__
712ef83b993SPatrick Venture for (const auto& iter : fruAreaVec)
713155c34fbSMatthew Barth {
7140e3a1c4eSPatrick Venture std::printf("FRU ID : [%d]\n", iter->getFruID());
7150e3a1c4eSPatrick Venture std::printf("AREA NAME : [%s]\n", iter->getName());
7160e3a1c4eSPatrick Venture std::printf("TYPE : [%d]\n", iter->getType());
7170e3a1c4eSPatrick Venture std::printf("LEN : [%d]\n", iter->getLength());
718155c34fbSMatthew Barth }
719155c34fbSMatthew Barth #endif
720155c34fbSMatthew Barth
721155c34fbSMatthew Barth // If the vector is populated with everything, then go ahead and update the
722155c34fbSMatthew Barth // inventory.
723ef83b993SPatrick Venture if (!(fruAreaVec.empty()))
724155c34fbSMatthew Barth {
725155c34fbSMatthew Barth #ifdef __IPMI_DEBUG__
726ef83b993SPatrick Venture std::printf("\n SIZE of vector is : [%d] \n", fruAreaVec.size());
727155c34fbSMatthew Barth #endif
728ef83b993SPatrick Venture rc = updateInventory(fruAreaVec, bus);
729155c34fbSMatthew Barth if (rc < 0)
730155c34fbSMatthew Barth {
7315c787214SPatrick Venture log<level::ERR>("Error updating inventory.");
732155c34fbSMatthew Barth }
733155c34fbSMatthew Barth }
734155c34fbSMatthew Barth
735155c34fbSMatthew Barth // we are done with all that we wanted to do. This will do the job of
736155c34fbSMatthew Barth // calling any destructors too.
737ef83b993SPatrick Venture fruAreaVec.clear();
738155c34fbSMatthew Barth
739155c34fbSMatthew Barth return rc;
740155c34fbSMatthew Barth }
741