blob: dbf96cbf2f8bff2922b41d4db0f0b27cd69cc53d [file] [log] [blame]
Patrick Venture02ba8842018-10-20 13:37:54 -07001#include "writefrudata.hpp"
2
Patrick Venture5c2bd5e2018-10-20 19:45:48 -07003#include "fru_area.hpp"
Patrick Venturec9508db2018-10-16 17:18:43 -07004#include "frup.hpp"
5#include "types.hpp"
6
Patrick Venturec9508db2018-10-16 17:18:43 -07007#include <host-ipmid/ipmid-api.h>
Chris Austenb45c4cb2015-11-01 06:34:56 -06008#include <unistd.h>
Patrick Venturec9508db2018-10-16 17:18:43 -07009
10#include <algorithm>
Patrick Venture5c787212018-10-17 13:48:23 -070011#include <cstdio>
12#include <cstring>
Patrick Venturec9508db2018-10-16 17:18:43 -070013#include <exception>
14#include <fstream>
vishwac93d6d42015-12-16 11:55:16 -060015#include <iostream>
Patrick Ventureb390c0e2018-10-20 13:36:48 -070016#include <map>
vishwac93d6d42015-12-16 11:55:16 -060017#include <memory>
Patrick Venture5739ac32018-10-16 19:17:41 -070018#include <phosphor-logging/log.hpp>
Patrick Venture71918492018-10-20 19:40:13 -070019#include <sdbusplus/bus.hpp>
Patrick Venturec9508db2018-10-16 17:18:43 -070020#include <sstream>
21#include <vector>
Vishwa4be4b7a2015-10-31 22:55:50 -050022
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060023using namespace ipmi::vpd;
Patrick Venture5739ac32018-10-16 19:17:41 -070024using namespace phosphor::logging;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060025
Ratan Guptacb0d4e52016-12-22 19:05:57 +053026extern const FruMap frus;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060027extern const std::map<Path, InterfaceMap> extras;
Ratan Guptacb0d4e52016-12-22 19:05:57 +053028
Patrick Venture19bea9a2018-10-20 19:54:27 -070029using FruAreaVector = std::vector<std::unique_ptr<IPMIFruArea>>;
30
Patrick Venture234b7352018-10-20 09:37:09 -070031namespace
32{
33
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 }
147 catch (const sdbusplus::exception::SdBusError& ex)
148 {
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 Venture6cc89042018-10-21 14:26:39 -0700217 log<level::ERR>("Unable to get the FRU info",
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 }
291 catch (const sdbusplus::exception::SdBusError& ex)
292 {
293 log<level::ERR>("Error in notify call", entry("WHAT=%s", ex.what()));
294 return -1;
295 }
296
297 return rc;
298}
299
Patrick Venture234b7352018-10-20 09:37:09 -0700300} // namespace
301
Patrick Venture6cc89042018-10-21 14:26:39 -0700302/**
303 * Takes the pointer to stream of bytes and length and returns the 8 bit
304 * checksum. This algo is per IPMI V2.0 spec
305 *
306 * @param[in] data - data for running crc
307 * @param[in] len - the length over which to run the crc
308 * @return the CRC value
309 */
Patrick Venture062e1452018-10-21 09:16:47 -0700310unsigned char calculateCRC(const unsigned char* data, size_t len)
Vishwa4be4b7a2015-10-31 22:55:50 -0500311{
312 char crc = 0;
vishwac93d6d42015-12-16 11:55:16 -0600313 size_t byte = 0;
Vishwa4be4b7a2015-10-31 22:55:50 -0500314
Patrick Venturec9508db2018-10-16 17:18:43 -0700315 for (byte = 0; byte < len; byte++)
Vishwa4be4b7a2015-10-31 22:55:50 -0500316 {
317 crc += *data++;
318 }
vishwaf3ca3522015-12-02 10:35:13 -0600319
Patrick Venturec9508db2018-10-16 17:18:43 -0700320 return (-crc);
Vishwa4be4b7a2015-10-31 22:55:50 -0500321}
322
Patrick Venture6cc89042018-10-21 14:26:39 -0700323/**
324 * Accepts a FRU area offset into a commom header and tells which area it is.
325 *
326 * @param[in] areaOffset - offset to lookup the area type
327 * @return the ipmi_fru_area_type
328 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700329ipmi_fru_area_type getFruAreaType(uint8_t areaOffset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500330{
331 ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
332
Patrick Ventureef83b992018-10-21 09:32:44 -0700333 switch (areaOffset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500334 {
335 case IPMI_FRU_INTERNAL_OFFSET:
336 type = IPMI_FRU_AREA_INTERNAL_USE;
337 break;
338
339 case IPMI_FRU_CHASSIS_OFFSET:
340 type = IPMI_FRU_AREA_CHASSIS_INFO;
341 break;
342
343 case IPMI_FRU_BOARD_OFFSET:
344 type = IPMI_FRU_AREA_BOARD_INFO;
345 break;
346
347 case IPMI_FRU_PRODUCT_OFFSET:
348 type = IPMI_FRU_AREA_PRODUCT_INFO;
349 break;
350
351 case IPMI_FRU_MULTI_OFFSET:
352 type = IPMI_FRU_AREA_MULTI_RECORD;
353 break;
354
355 default:
356 type = IPMI_FRU_AREA_TYPE_MAX;
357 }
358
359 return type;
360}
361
Patrick Venture6cc89042018-10-21 14:26:39 -0700362/**
Oskar Senft69540862018-12-04 15:52:31 -0500363 * Validates the data for mandatory fields and CRC if selected.
Patrick Venture6cc89042018-10-21 14:26:39 -0700364 *
365 * @param[in] data - the data to verify
366 * @param[in] len - the length of the region to verify
Oskar Senft69540862018-12-04 15:52:31 -0500367 * @param[in] validateCrc - whether to validate the CRC
Patrick Venture6cc89042018-10-21 14:26:39 -0700368 * @return non-zero on failure
369 */
Oskar Senft69540862018-12-04 15:52:31 -0500370int verifyFruData(const uint8_t* data, const size_t len, bool validateCrc)
vishwac93d6d42015-12-16 11:55:16 -0600371{
372 uint8_t checksum = 0;
373 int rc = -1;
374
375 // Validate for first byte to always have a value of [1]
Patrick Venturec9508db2018-10-16 17:18:43 -0700376 if (data[0] != IPMI_FRU_HDR_BYTE_ZERO)
vishwac93d6d42015-12-16 11:55:16 -0600377 {
Patrick Venture5c787212018-10-17 13:48:23 -0700378 log<level::ERR>("Invalid entry in byte-0",
379 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600380 return rc;
381 }
382#ifdef __IPMI_DEBUG__
383 else
384 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700385 log<level::DEBUG>("Validated in entry_1 of fruData",
Patrick Venture5c787212018-10-17 13:48:23 -0700386 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600387 }
388#endif
389
Oskar Senft69540862018-12-04 15:52:31 -0500390 if (!validateCrc)
391 {
392 // There's nothing else to do for this area.
393 return EXIT_SUCCESS;
394 }
395
vishwac93d6d42015-12-16 11:55:16 -0600396 // See if the calculated CRC matches with the embedded one.
397 // CRC to be calculated on all except the last one that is CRC itself.
Patrick Venture062e1452018-10-21 09:16:47 -0700398 checksum = calculateCRC(data, len - 1);
Patrick Venturec9508db2018-10-16 17:18:43 -0700399 if (checksum != data[len - 1])
vishwac93d6d42015-12-16 11:55:16 -0600400 {
401#ifdef __IPMI_DEBUG__
Patrick Venture5c787212018-10-17 13:48:23 -0700402 log<level::ERR>(
403 "Checksum mismatch",
404 entry("Calculated=0x%X", static_cast<uint32_t>(checksum)),
405 entry("Embedded=0x%X", static_cast<uint32_t>(data[len])));
vishwac93d6d42015-12-16 11:55:16 -0600406#endif
407 return rc;
408 }
409#ifdef __IPMI_DEBUG__
410 else
411 {
Patrick Venture5c787212018-10-17 13:48:23 -0700412 log<level::DEBUG>("Checksum matches");
vishwac93d6d42015-12-16 11:55:16 -0600413 }
414#endif
415
416 return EXIT_SUCCESS;
417}
418
Patrick Venture6cc89042018-10-21 14:26:39 -0700419/**
420 * Checks if a particular FRU area is populated or not.
421 *
422 * @param[in] reference to FRU area pointer
423 * @return true if the area is empty
424 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700425bool removeInvalidArea(const std::unique_ptr<IPMIFruArea>& fruArea)
Vishwa4be4b7a2015-10-31 22:55:50 -0500426{
Deepak Kodihalli89ededd2017-02-11 01:59:32 -0600427 // Filter the ones that are empty
Patrick Ventureef83b992018-10-21 09:32:44 -0700428 if (!(fruArea->getLength()))
vishwac93d6d42015-12-16 11:55:16 -0600429 {
430 return true;
431 }
432 return false;
433}
Vishwa4be4b7a2015-10-31 22:55:50 -0500434
Patrick Venture6cc89042018-10-21 14:26:39 -0700435/**
436 * Populates various FRU areas.
437 *
438 * @prereq : This must be called only after validating common header
439 * @param[in] fruData - pointer to the FRU bytes
440 * @param[in] dataLen - the length of the FRU data
441 * @param[in] fruAreaVec - the FRU area vector to update
442 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700443int ipmiPopulateFruAreas(uint8_t* fruData, const size_t dataLen,
444 FruAreaVector& fruAreaVec)
vishwac93d6d42015-12-16 11:55:16 -0600445{
vishwac93d6d42015-12-16 11:55:16 -0600446 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500447
vishwac93d6d42015-12-16 11:55:16 -0600448 // Now walk the common header and see if the file size has atleast the last
Patrick Ventureef83b992018-10-21 09:32:44 -0700449 // offset mentioned by the struct common_header. If the file size is less
Patrick Venture6cc89042018-10-21 14:26:39 -0700450 // than the offset of any if the FRU areas mentioned in the common header,
Patrick Ventureef83b992018-10-21 09:32:44 -0700451 // then we do not have a complete file.
452 for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET;
453 fruEntry < (sizeof(struct common_header) - 2); fruEntry++)
vishwac93d6d42015-12-16 11:55:16 -0600454 {
Yi Li75c2d462016-04-11 16:57:46 +0800455 rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600456 // Actual offset in the payload is the offset mentioned in common header
Gunnar Millsc19b8132018-06-14 08:56:17 -0500457 // multiplied by 8. Common header is always the first 8 bytes.
Patrick Ventureef83b992018-10-21 09:32:44 -0700458 size_t areaOffset = fruData[fruEntry] * IPMI_EIGHT_BYTES;
459 if (areaOffset && (dataLen < (areaOffset + 2)))
vishwac93d6d42015-12-16 11:55:16 -0600460 {
461 // Our file size is less than what it needs to be. +2 because we are
Patrick Ventureef83b992018-10-21 09:32:44 -0700462 // using area len that is at 2 byte off areaOffset
Patrick Venture6cc89042018-10-21 14:26:39 -0700463 log<level::ERR>("FRU file is incomplete",
Patrick Ventureef83b992018-10-21 09:32:44 -0700464 entry("SIZE=%d", dataLen));
vishwac93d6d42015-12-16 11:55:16 -0600465 return rc;
466 }
Patrick Ventureef83b992018-10-21 09:32:44 -0700467 else if (areaOffset)
vishwac93d6d42015-12-16 11:55:16 -0600468 {
469 // Read 2 bytes to know the actual size of area.
Patrick Ventureef83b992018-10-21 09:32:44 -0700470 uint8_t areaHeader[2] = {0};
471 std::memcpy(areaHeader, &((uint8_t*)fruData)[areaOffset],
472 sizeof(areaHeader));
Vishwa4be4b7a2015-10-31 22:55:50 -0500473
Patrick Venture6cc89042018-10-21 14:26:39 -0700474 // Size of this area will be the 2nd byte in the FRU area header.
Patrick Ventureef83b992018-10-21 09:32:44 -0700475 size_t areaLen = areaHeader[1] * IPMI_EIGHT_BYTES;
476 uint8_t areaData[areaLen] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500477
Patrick Venture6cc89042018-10-21 14:26:39 -0700478 log<level::DEBUG>("FRU Data", entry("SIZE=%d", dataLen),
Patrick Ventureef83b992018-10-21 09:32:44 -0700479 entry("AREA OFFSET=%d", areaOffset),
480 entry("AREA_SIZE=%d", areaLen));
Vishwa4be4b7a2015-10-31 22:55:50 -0500481
vishwac93d6d42015-12-16 11:55:16 -0600482 // See if we really have that much buffer. We have area offset amd
483 // from there, the actual len.
Patrick Ventureef83b992018-10-21 09:32:44 -0700484 if (dataLen < (areaLen + areaOffset))
vishwac93d6d42015-12-16 11:55:16 -0600485 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700486 log<level::ERR>("Incomplete FRU file",
Patrick Ventureef83b992018-10-21 09:32:44 -0700487 entry("SIZE=%d", dataLen));
vishwac93d6d42015-12-16 11:55:16 -0600488 return rc;
489 }
490
491 // Save off the data.
Patrick Ventureef83b992018-10-21 09:32:44 -0700492 std::memcpy(areaData, &((uint8_t*)fruData)[areaOffset], areaLen);
vishwac93d6d42015-12-16 11:55:16 -0600493
Oskar Senft69540862018-12-04 15:52:31 -0500494 // Validate the CRC, but not for the internal use area, since its
495 // contents beyond the first byte are not defined in the spec and
496 // it may not end with a CRC byte.
497 bool validateCrc = fruEntry != IPMI_FRU_INTERNAL_OFFSET;
498 rc = verifyFruData(areaData, areaLen, validateCrc);
Patrick Venturec9508db2018-10-16 17:18:43 -0700499 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600500 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700501 log<level::ERR>("Err validating FRU area",
Patrick Ventureef83b992018-10-21 09:32:44 -0700502 entry("OFFSET=%d", areaOffset));
vishwac93d6d42015-12-16 11:55:16 -0600503 return rc;
504 }
505 else
506 {
Oskar Senft69540862018-12-04 15:52:31 -0500507 log<level::DEBUG>("Successfully verified area.",
Patrick Ventureef83b992018-10-21 09:32:44 -0700508 entry("OFFSET=%d", areaOffset));
vishwac93d6d42015-12-16 11:55:16 -0600509 }
510
511 // We already have a vector that is passed to us containing all
512 // of the fields populated. Update the data portion now.
Patrick Ventureef83b992018-10-21 09:32:44 -0700513 for (auto& iter : fruAreaVec)
vishwac93d6d42015-12-16 11:55:16 -0600514 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700515 if (iter->getType() == getFruAreaType(fruEntry))
vishwac93d6d42015-12-16 11:55:16 -0600516 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700517 iter->setData(areaData, areaLen);
vishwac93d6d42015-12-16 11:55:16 -0600518 }
519 }
Patrick Venture6cc89042018-10-21 14:26:39 -0700520 } // If we have FRU data present
Patrick Ventureef83b992018-10-21 09:32:44 -0700521 } // Walk struct common_header
vishwac93d6d42015-12-16 11:55:16 -0600522
Patrick Venture6cc89042018-10-21 14:26:39 -0700523 // Not all the fields will be populated in a FRU data. Mostly all cases will
vishwac93d6d42015-12-16 11:55:16 -0600524 // not have more than 2 or 3.
Patrick Ventureef83b992018-10-21 09:32:44 -0700525 fruAreaVec.erase(
526 std::remove_if(fruAreaVec.begin(), fruAreaVec.end(), removeInvalidArea),
527 fruAreaVec.end());
vishwac93d6d42015-12-16 11:55:16 -0600528
529 return EXIT_SUCCESS;
530}
531
Patrick Venture6cc89042018-10-21 14:26:39 -0700532/**
533 * Validates the FRU data per ipmi common header constructs.
534 * Returns with updated struct common_header and also file_size
535 *
536 * @param[in] fruData - the FRU data
537 * @param[in] dataLen - the length of the data
538 * @return non-zero on failure
539 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700540int ipmiValidateCommonHeader(const uint8_t* fruData, const size_t dataLen)
vishwac93d6d42015-12-16 11:55:16 -0600541{
542 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500543
Patrick Ventureef83b992018-10-21 09:32:44 -0700544 uint8_t commonHdr[sizeof(struct common_header)] = {0};
545 if (dataLen >= sizeof(commonHdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500546 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700547 std::memcpy(commonHdr, fruData, sizeof(commonHdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500548 }
549 else
550 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700551 log<level::ERR>("Incomplete FRU data file", entry("SIZE=%d", dataLen));
vishwac93d6d42015-12-16 11:55:16 -0600552 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500553 }
554
Patrick Venture6cc89042018-10-21 14:26:39 -0700555 // Verify the CRC and size
Oskar Senft69540862018-12-04 15:52:31 -0500556 rc = verifyFruData(commonHdr, sizeof(commonHdr), true);
Patrick Venturec9508db2018-10-16 17:18:43 -0700557 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500558 {
Patrick Venture5c787212018-10-17 13:48:23 -0700559 log<level::ERR>("Failed to validate common header");
vishwac93d6d42015-12-16 11:55:16 -0600560 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500561 }
562
vishwac93d6d42015-12-16 11:55:16 -0600563 return EXIT_SUCCESS;
564}
Vishwa4be4b7a2015-10-31 22:55:50 -0500565
Patrick Ventureef83b992018-10-21 09:32:44 -0700566int validateFRUArea(const uint8_t fruid, const char* fruFilename,
567 sdbusplus::bus::bus& bus, const bool bmcOnlyFru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500568{
Patrick Ventureef83b992018-10-21 09:32:44 -0700569 size_t dataLen = 0;
570 size_t bytesRead = 0;
vishwac93d6d42015-12-16 11:55:16 -0600571 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500572
vishwac93d6d42015-12-16 11:55:16 -0600573 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
574 // are not used, keeping it here for completeness.
Patrick Ventureef83b992018-10-21 09:32:44 -0700575 FruAreaVector fruAreaVec;
Yi Li75c2d462016-04-11 16:57:46 +0800576
Patrick Ventureef83b992018-10-21 09:32:44 -0700577 for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET;
578 fruEntry < (sizeof(struct common_header) - 2); fruEntry++)
vishwac93d6d42015-12-16 11:55:16 -0600579 {
580 // Create an object and push onto a vector.
Patrick Ventureef83b992018-10-21 09:32:44 -0700581 std::unique_ptr<IPMIFruArea> fruArea = std::make_unique<IPMIFruArea>(
582 fruid, getFruAreaType(fruEntry), bmcOnlyFru);
vishwac93d6d42015-12-16 11:55:16 -0600583
584 // Physically being present
Patrick Ventureef83b992018-10-21 09:32:44 -0700585 bool present = access(fruFilename, F_OK) == 0;
586 fruArea->setPresent(present);
vishwac93d6d42015-12-16 11:55:16 -0600587
Patrick Ventureef83b992018-10-21 09:32:44 -0700588 fruAreaVec.emplace_back(std::move(fruArea));
vishwac93d6d42015-12-16 11:55:16 -0600589 }
590
Patrick Ventureef83b992018-10-21 09:32:44 -0700591 FILE* fruFilePointer = std::fopen(fruFilename, "rb");
592 if (fruFilePointer == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500593 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700594 log<level::ERR>("Unable to open FRU file",
Patrick Ventureef83b992018-10-21 09:32:44 -0700595 entry("FILE=%s", fruFilename),
Patrick Venture5c787212018-10-17 13:48:23 -0700596 entry("ERRNO=%s", std::strerror(errno)));
Patrick Ventureef83b992018-10-21 09:32:44 -0700597 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500598 }
599
vishwac93d6d42015-12-16 11:55:16 -0600600 // Get the size of the file to see if it meets minimum requirement
Patrick Ventureef83b992018-10-21 09:32:44 -0700601 if (std::fseek(fruFilePointer, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500602 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700603 log<level::ERR>("Unable to seek FRU file",
Patrick Ventureef83b992018-10-21 09:32:44 -0700604 entry("FILE=%s", fruFilename),
Patrick Venture5c787212018-10-17 13:48:23 -0700605 entry("ERRNO=%s", std::strerror(errno)));
Patrick Ventureef83b992018-10-21 09:32:44 -0700606 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500607 }
608
vishwac93d6d42015-12-16 11:55:16 -0600609 // Allocate a buffer to hold entire file content
Patrick Ventureef83b992018-10-21 09:32:44 -0700610 dataLen = std::ftell(fruFilePointer);
611 uint8_t fruData[dataLen] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500612
Patrick Ventureef83b992018-10-21 09:32:44 -0700613 std::rewind(fruFilePointer);
614 bytesRead = std::fread(fruData, dataLen, 1, fruFilePointer);
615 if (bytesRead != 1)
Vishwa4be4b7a2015-10-31 22:55:50 -0500616 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700617 log<level::ERR>("Failed reading FRU data.",
Patrick Ventureef83b992018-10-21 09:32:44 -0700618 entry("BYTESREAD=%d", bytesRead),
Patrick Venture5c787212018-10-17 13:48:23 -0700619 entry("ERRNO=%s", std::strerror(errno)));
Patrick Ventureef83b992018-10-21 09:32:44 -0700620 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500621 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500622
vishwac93d6d42015-12-16 11:55:16 -0600623 // We are done reading.
Patrick Ventureef83b992018-10-21 09:32:44 -0700624 std::fclose(fruFilePointer);
625 fruFilePointer = NULL;
vishwac93d6d42015-12-16 11:55:16 -0600626
Patrick Ventureef83b992018-10-21 09:32:44 -0700627 rc = ipmiValidateCommonHeader(fruData, dataLen);
Patrick Venturec9508db2018-10-16 17:18:43 -0700628 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500629 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700630 return cleanupError(fruFilePointer, fruAreaVec);
vishwac93d6d42015-12-16 11:55:16 -0600631 }
632
Patrick Venture6cc89042018-10-21 14:26:39 -0700633 // Now that we validated the common header, populate various FRU sections if
Patrick Venturec9508db2018-10-16 17:18:43 -0700634 // we have them here.
Patrick Ventureef83b992018-10-21 09:32:44 -0700635 rc = ipmiPopulateFruAreas(fruData, dataLen, fruAreaVec);
Patrick Venturec9508db2018-10-16 17:18:43 -0700636 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600637 {
Patrick Venture5c787212018-10-17 13:48:23 -0700638 log<level::ERR>("Populating FRU areas failed", entry("FRU=%d", fruid));
Patrick Ventureef83b992018-10-21 09:32:44 -0700639 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500640 }
641 else
642 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700643 log<level::DEBUG>("Populated FRU areas", entry("FILE=%s", fruFilename));
Vishwa4be4b7a2015-10-31 22:55:50 -0500644 }
645
vishwac93d6d42015-12-16 11:55:16 -0600646#ifdef __IPMI_DEBUG__
Patrick Ventureef83b992018-10-21 09:32:44 -0700647 for (const auto& iter : fruAreaVec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500648 {
Patrick Venture0e3a1c42018-10-20 21:15:41 -0700649 std::printf("FRU ID : [%d]\n", iter->getFruID());
650 std::printf("AREA NAME : [%s]\n", iter->getName());
651 std::printf("TYPE : [%d]\n", iter->getType());
652 std::printf("LEN : [%d]\n", iter->getLength());
Vishwa4be4b7a2015-10-31 22:55:50 -0500653 }
vishwac93d6d42015-12-16 11:55:16 -0600654#endif
655
656 // If the vector is populated with everything, then go ahead and update the
657 // inventory.
Patrick Ventureef83b992018-10-21 09:32:44 -0700658 if (!(fruAreaVec.empty()))
vishwac93d6d42015-12-16 11:55:16 -0600659 {
660
661#ifdef __IPMI_DEBUG__
Patrick Ventureef83b992018-10-21 09:32:44 -0700662 std::printf("\n SIZE of vector is : [%d] \n", fruAreaVec.size());
vishwac93d6d42015-12-16 11:55:16 -0600663#endif
Patrick Ventureef83b992018-10-21 09:32:44 -0700664 rc = updateInventory(fruAreaVec, bus);
Patrick Venturec9508db2018-10-16 17:18:43 -0700665 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600666 {
Patrick Venture5c787212018-10-17 13:48:23 -0700667 log<level::ERR>("Error updating inventory.");
vishwac93d6d42015-12-16 11:55:16 -0600668 }
669 }
670
671 // we are done with all that we wanted to do. This will do the job of
672 // calling any destructors too.
Patrick Ventureef83b992018-10-21 09:32:44 -0700673 fruAreaVec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500674
675 return rc;
676}