blob: 114e4e98d04618c16af0a723ef8eb071f770e2b3 [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/**
363 * Validates the data for CRC and mandatory fields.
364 *
365 * @param[in] data - the data to verify
366 * @param[in] len - the length of the region to verify
367 * @return non-zero on failure
368 */
Patrick Venture062e1452018-10-21 09:16:47 -0700369int verifyFruData(const uint8_t* data, const size_t len)
vishwac93d6d42015-12-16 11:55:16 -0600370{
371 uint8_t checksum = 0;
372 int rc = -1;
373
374 // Validate for first byte to always have a value of [1]
Patrick Venturec9508db2018-10-16 17:18:43 -0700375 if (data[0] != IPMI_FRU_HDR_BYTE_ZERO)
vishwac93d6d42015-12-16 11:55:16 -0600376 {
Patrick Venture5c787212018-10-17 13:48:23 -0700377 log<level::ERR>("Invalid entry in byte-0",
378 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600379 return rc;
380 }
381#ifdef __IPMI_DEBUG__
382 else
383 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700384 log<level::DEBUG>("Validated in entry_1 of fruData",
Patrick Venture5c787212018-10-17 13:48:23 -0700385 entry("ENTRY=0x%X", static_cast<uint32_t>(data[0])));
vishwac93d6d42015-12-16 11:55:16 -0600386 }
387#endif
388
389 // See if the calculated CRC matches with the embedded one.
390 // CRC to be calculated on all except the last one that is CRC itself.
Patrick Venture062e1452018-10-21 09:16:47 -0700391 checksum = calculateCRC(data, len - 1);
Patrick Venturec9508db2018-10-16 17:18:43 -0700392 if (checksum != data[len - 1])
vishwac93d6d42015-12-16 11:55:16 -0600393 {
394#ifdef __IPMI_DEBUG__
Patrick Venture5c787212018-10-17 13:48:23 -0700395 log<level::ERR>(
396 "Checksum mismatch",
397 entry("Calculated=0x%X", static_cast<uint32_t>(checksum)),
398 entry("Embedded=0x%X", static_cast<uint32_t>(data[len])));
vishwac93d6d42015-12-16 11:55:16 -0600399#endif
400 return rc;
401 }
402#ifdef __IPMI_DEBUG__
403 else
404 {
Patrick Venture5c787212018-10-17 13:48:23 -0700405 log<level::DEBUG>("Checksum matches");
vishwac93d6d42015-12-16 11:55:16 -0600406 }
407#endif
408
409 return EXIT_SUCCESS;
410}
411
Patrick Venture6cc89042018-10-21 14:26:39 -0700412/**
413 * Checks if a particular FRU area is populated or not.
414 *
415 * @param[in] reference to FRU area pointer
416 * @return true if the area is empty
417 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700418bool removeInvalidArea(const std::unique_ptr<IPMIFruArea>& fruArea)
Vishwa4be4b7a2015-10-31 22:55:50 -0500419{
Deepak Kodihalli89ededd2017-02-11 01:59:32 -0600420 // Filter the ones that are empty
Patrick Ventureef83b992018-10-21 09:32:44 -0700421 if (!(fruArea->getLength()))
vishwac93d6d42015-12-16 11:55:16 -0600422 {
423 return true;
424 }
425 return false;
426}
Vishwa4be4b7a2015-10-31 22:55:50 -0500427
Patrick Venture6cc89042018-10-21 14:26:39 -0700428/**
429 * Populates various FRU areas.
430 *
431 * @prereq : This must be called only after validating common header
432 * @param[in] fruData - pointer to the FRU bytes
433 * @param[in] dataLen - the length of the FRU data
434 * @param[in] fruAreaVec - the FRU area vector to update
435 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700436int ipmiPopulateFruAreas(uint8_t* fruData, const size_t dataLen,
437 FruAreaVector& fruAreaVec)
vishwac93d6d42015-12-16 11:55:16 -0600438{
vishwac93d6d42015-12-16 11:55:16 -0600439 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500440
vishwac93d6d42015-12-16 11:55:16 -0600441 // Now walk the common header and see if the file size has atleast the last
Patrick Ventureef83b992018-10-21 09:32:44 -0700442 // offset mentioned by the struct common_header. If the file size is less
Patrick Venture6cc89042018-10-21 14:26:39 -0700443 // than the offset of any if the FRU areas mentioned in the common header,
Patrick Ventureef83b992018-10-21 09:32:44 -0700444 // then we do not have a complete file.
445 for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET;
446 fruEntry < (sizeof(struct common_header) - 2); fruEntry++)
vishwac93d6d42015-12-16 11:55:16 -0600447 {
Yi Li75c2d462016-04-11 16:57:46 +0800448 rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600449 // Actual offset in the payload is the offset mentioned in common header
Gunnar Millsc19b8132018-06-14 08:56:17 -0500450 // multiplied by 8. Common header is always the first 8 bytes.
Patrick Ventureef83b992018-10-21 09:32:44 -0700451 size_t areaOffset = fruData[fruEntry] * IPMI_EIGHT_BYTES;
452 if (areaOffset && (dataLen < (areaOffset + 2)))
vishwac93d6d42015-12-16 11:55:16 -0600453 {
454 // Our file size is less than what it needs to be. +2 because we are
Patrick Ventureef83b992018-10-21 09:32:44 -0700455 // using area len that is at 2 byte off areaOffset
Patrick Venture6cc89042018-10-21 14:26:39 -0700456 log<level::ERR>("FRU file is incomplete",
Patrick Ventureef83b992018-10-21 09:32:44 -0700457 entry("SIZE=%d", dataLen));
vishwac93d6d42015-12-16 11:55:16 -0600458 return rc;
459 }
Patrick Ventureef83b992018-10-21 09:32:44 -0700460 else if (areaOffset)
vishwac93d6d42015-12-16 11:55:16 -0600461 {
462 // Read 2 bytes to know the actual size of area.
Patrick Ventureef83b992018-10-21 09:32:44 -0700463 uint8_t areaHeader[2] = {0};
464 std::memcpy(areaHeader, &((uint8_t*)fruData)[areaOffset],
465 sizeof(areaHeader));
Vishwa4be4b7a2015-10-31 22:55:50 -0500466
Patrick Venture6cc89042018-10-21 14:26:39 -0700467 // Size of this area will be the 2nd byte in the FRU area header.
Patrick Ventureef83b992018-10-21 09:32:44 -0700468 size_t areaLen = areaHeader[1] * IPMI_EIGHT_BYTES;
469 uint8_t areaData[areaLen] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500470
Patrick Venture6cc89042018-10-21 14:26:39 -0700471 log<level::DEBUG>("FRU Data", entry("SIZE=%d", dataLen),
Patrick Ventureef83b992018-10-21 09:32:44 -0700472 entry("AREA OFFSET=%d", areaOffset),
473 entry("AREA_SIZE=%d", areaLen));
Vishwa4be4b7a2015-10-31 22:55:50 -0500474
vishwac93d6d42015-12-16 11:55:16 -0600475 // See if we really have that much buffer. We have area offset amd
476 // from there, the actual len.
Patrick Ventureef83b992018-10-21 09:32:44 -0700477 if (dataLen < (areaLen + areaOffset))
vishwac93d6d42015-12-16 11:55:16 -0600478 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700479 log<level::ERR>("Incomplete FRU file",
Patrick Ventureef83b992018-10-21 09:32:44 -0700480 entry("SIZE=%d", dataLen));
vishwac93d6d42015-12-16 11:55:16 -0600481 return rc;
482 }
483
484 // Save off the data.
Patrick Ventureef83b992018-10-21 09:32:44 -0700485 std::memcpy(areaData, &((uint8_t*)fruData)[areaOffset], areaLen);
vishwac93d6d42015-12-16 11:55:16 -0600486
487 // Validate the crc
Patrick Ventureef83b992018-10-21 09:32:44 -0700488 rc = verifyFruData(areaData, areaLen);
Patrick Venturec9508db2018-10-16 17:18:43 -0700489 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600490 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700491 log<level::ERR>("Err validating FRU area",
Patrick Ventureef83b992018-10-21 09:32:44 -0700492 entry("OFFSET=%d", areaOffset));
vishwac93d6d42015-12-16 11:55:16 -0600493 return rc;
494 }
495 else
496 {
Patrick Venture5c787212018-10-17 13:48:23 -0700497 log<level::DEBUG>("Successfully verified area checksum.",
Patrick Ventureef83b992018-10-21 09:32:44 -0700498 entry("OFFSET=%d", areaOffset));
vishwac93d6d42015-12-16 11:55:16 -0600499 }
500
501 // We already have a vector that is passed to us containing all
502 // of the fields populated. Update the data portion now.
Patrick Ventureef83b992018-10-21 09:32:44 -0700503 for (auto& iter : fruAreaVec)
vishwac93d6d42015-12-16 11:55:16 -0600504 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700505 if (iter->getType() == getFruAreaType(fruEntry))
vishwac93d6d42015-12-16 11:55:16 -0600506 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700507 iter->setData(areaData, areaLen);
vishwac93d6d42015-12-16 11:55:16 -0600508 }
509 }
Patrick Venture6cc89042018-10-21 14:26:39 -0700510 } // If we have FRU data present
Patrick Ventureef83b992018-10-21 09:32:44 -0700511 } // Walk struct common_header
vishwac93d6d42015-12-16 11:55:16 -0600512
Patrick Venture6cc89042018-10-21 14:26:39 -0700513 // Not all the fields will be populated in a FRU data. Mostly all cases will
vishwac93d6d42015-12-16 11:55:16 -0600514 // not have more than 2 or 3.
Patrick Ventureef83b992018-10-21 09:32:44 -0700515 fruAreaVec.erase(
516 std::remove_if(fruAreaVec.begin(), fruAreaVec.end(), removeInvalidArea),
517 fruAreaVec.end());
vishwac93d6d42015-12-16 11:55:16 -0600518
519 return EXIT_SUCCESS;
520}
521
Patrick Venture6cc89042018-10-21 14:26:39 -0700522/**
523 * Validates the FRU data per ipmi common header constructs.
524 * Returns with updated struct common_header and also file_size
525 *
526 * @param[in] fruData - the FRU data
527 * @param[in] dataLen - the length of the data
528 * @return non-zero on failure
529 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700530int ipmiValidateCommonHeader(const uint8_t* fruData, const size_t dataLen)
vishwac93d6d42015-12-16 11:55:16 -0600531{
532 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500533
Patrick Ventureef83b992018-10-21 09:32:44 -0700534 uint8_t commonHdr[sizeof(struct common_header)] = {0};
535 if (dataLen >= sizeof(commonHdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500536 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700537 std::memcpy(commonHdr, fruData, sizeof(commonHdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500538 }
539 else
540 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700541 log<level::ERR>("Incomplete FRU data file", entry("SIZE=%d", dataLen));
vishwac93d6d42015-12-16 11:55:16 -0600542 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500543 }
544
Patrick Venture6cc89042018-10-21 14:26:39 -0700545 // Verify the CRC and size
Patrick Ventureef83b992018-10-21 09:32:44 -0700546 rc = verifyFruData(commonHdr, sizeof(commonHdr));
Patrick Venturec9508db2018-10-16 17:18:43 -0700547 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500548 {
Patrick Venture5c787212018-10-17 13:48:23 -0700549 log<level::ERR>("Failed to validate common header");
vishwac93d6d42015-12-16 11:55:16 -0600550 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500551 }
552
vishwac93d6d42015-12-16 11:55:16 -0600553 return EXIT_SUCCESS;
554}
Vishwa4be4b7a2015-10-31 22:55:50 -0500555
Patrick Ventureef83b992018-10-21 09:32:44 -0700556int validateFRUArea(const uint8_t fruid, const char* fruFilename,
557 sdbusplus::bus::bus& bus, const bool bmcOnlyFru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500558{
Patrick Ventureef83b992018-10-21 09:32:44 -0700559 size_t dataLen = 0;
560 size_t bytesRead = 0;
vishwac93d6d42015-12-16 11:55:16 -0600561 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500562
vishwac93d6d42015-12-16 11:55:16 -0600563 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
564 // are not used, keeping it here for completeness.
Patrick Ventureef83b992018-10-21 09:32:44 -0700565 FruAreaVector fruAreaVec;
Yi Li75c2d462016-04-11 16:57:46 +0800566
Patrick Ventureef83b992018-10-21 09:32:44 -0700567 for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET;
568 fruEntry < (sizeof(struct common_header) - 2); fruEntry++)
vishwac93d6d42015-12-16 11:55:16 -0600569 {
570 // Create an object and push onto a vector.
Patrick Ventureef83b992018-10-21 09:32:44 -0700571 std::unique_ptr<IPMIFruArea> fruArea = std::make_unique<IPMIFruArea>(
572 fruid, getFruAreaType(fruEntry), bmcOnlyFru);
vishwac93d6d42015-12-16 11:55:16 -0600573
574 // Physically being present
Patrick Ventureef83b992018-10-21 09:32:44 -0700575 bool present = access(fruFilename, F_OK) == 0;
576 fruArea->setPresent(present);
vishwac93d6d42015-12-16 11:55:16 -0600577
Patrick Ventureef83b992018-10-21 09:32:44 -0700578 fruAreaVec.emplace_back(std::move(fruArea));
vishwac93d6d42015-12-16 11:55:16 -0600579 }
580
Patrick Ventureef83b992018-10-21 09:32:44 -0700581 FILE* fruFilePointer = std::fopen(fruFilename, "rb");
582 if (fruFilePointer == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500583 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700584 log<level::ERR>("Unable to open FRU file",
Patrick Ventureef83b992018-10-21 09:32:44 -0700585 entry("FILE=%s", fruFilename),
Patrick Venture5c787212018-10-17 13:48:23 -0700586 entry("ERRNO=%s", std::strerror(errno)));
Patrick Ventureef83b992018-10-21 09:32:44 -0700587 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500588 }
589
vishwac93d6d42015-12-16 11:55:16 -0600590 // Get the size of the file to see if it meets minimum requirement
Patrick Ventureef83b992018-10-21 09:32:44 -0700591 if (std::fseek(fruFilePointer, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500592 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700593 log<level::ERR>("Unable to seek FRU file",
Patrick Ventureef83b992018-10-21 09:32:44 -0700594 entry("FILE=%s", fruFilename),
Patrick Venture5c787212018-10-17 13:48:23 -0700595 entry("ERRNO=%s", std::strerror(errno)));
Patrick Ventureef83b992018-10-21 09:32:44 -0700596 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500597 }
598
vishwac93d6d42015-12-16 11:55:16 -0600599 // Allocate a buffer to hold entire file content
Patrick Ventureef83b992018-10-21 09:32:44 -0700600 dataLen = std::ftell(fruFilePointer);
601 uint8_t fruData[dataLen] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500602
Patrick Ventureef83b992018-10-21 09:32:44 -0700603 std::rewind(fruFilePointer);
604 bytesRead = std::fread(fruData, dataLen, 1, fruFilePointer);
605 if (bytesRead != 1)
Vishwa4be4b7a2015-10-31 22:55:50 -0500606 {
Patrick Venture6cc89042018-10-21 14:26:39 -0700607 log<level::ERR>("Failed reading FRU data.",
Patrick Ventureef83b992018-10-21 09:32:44 -0700608 entry("BYTESREAD=%d", bytesRead),
Patrick Venture5c787212018-10-17 13:48:23 -0700609 entry("ERRNO=%s", std::strerror(errno)));
Patrick Ventureef83b992018-10-21 09:32:44 -0700610 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500611 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500612
vishwac93d6d42015-12-16 11:55:16 -0600613 // We are done reading.
Patrick Ventureef83b992018-10-21 09:32:44 -0700614 std::fclose(fruFilePointer);
615 fruFilePointer = NULL;
vishwac93d6d42015-12-16 11:55:16 -0600616
Patrick Ventureef83b992018-10-21 09:32:44 -0700617 rc = ipmiValidateCommonHeader(fruData, dataLen);
Patrick Venturec9508db2018-10-16 17:18:43 -0700618 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500619 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700620 return cleanupError(fruFilePointer, fruAreaVec);
vishwac93d6d42015-12-16 11:55:16 -0600621 }
622
Patrick Venture6cc89042018-10-21 14:26:39 -0700623 // Now that we validated the common header, populate various FRU sections if
Patrick Venturec9508db2018-10-16 17:18:43 -0700624 // we have them here.
Patrick Ventureef83b992018-10-21 09:32:44 -0700625 rc = ipmiPopulateFruAreas(fruData, dataLen, fruAreaVec);
Patrick Venturec9508db2018-10-16 17:18:43 -0700626 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600627 {
Patrick Venture5c787212018-10-17 13:48:23 -0700628 log<level::ERR>("Populating FRU areas failed", entry("FRU=%d", fruid));
Patrick Ventureef83b992018-10-21 09:32:44 -0700629 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500630 }
631 else
632 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700633 log<level::DEBUG>("Populated FRU areas", entry("FILE=%s", fruFilename));
Vishwa4be4b7a2015-10-31 22:55:50 -0500634 }
635
vishwac93d6d42015-12-16 11:55:16 -0600636#ifdef __IPMI_DEBUG__
Patrick Ventureef83b992018-10-21 09:32:44 -0700637 for (const auto& iter : fruAreaVec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500638 {
Patrick Venture0e3a1c42018-10-20 21:15:41 -0700639 std::printf("FRU ID : [%d]\n", iter->getFruID());
640 std::printf("AREA NAME : [%s]\n", iter->getName());
641 std::printf("TYPE : [%d]\n", iter->getType());
642 std::printf("LEN : [%d]\n", iter->getLength());
Vishwa4be4b7a2015-10-31 22:55:50 -0500643 }
vishwac93d6d42015-12-16 11:55:16 -0600644#endif
645
646 // If the vector is populated with everything, then go ahead and update the
647 // inventory.
Patrick Ventureef83b992018-10-21 09:32:44 -0700648 if (!(fruAreaVec.empty()))
vishwac93d6d42015-12-16 11:55:16 -0600649 {
650
651#ifdef __IPMI_DEBUG__
Patrick Ventureef83b992018-10-21 09:32:44 -0700652 std::printf("\n SIZE of vector is : [%d] \n", fruAreaVec.size());
vishwac93d6d42015-12-16 11:55:16 -0600653#endif
Patrick Ventureef83b992018-10-21 09:32:44 -0700654 rc = updateInventory(fruAreaVec, bus);
Patrick Venturec9508db2018-10-16 17:18:43 -0700655 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600656 {
Patrick Venture5c787212018-10-17 13:48:23 -0700657 log<level::ERR>("Error updating inventory.");
vishwac93d6d42015-12-16 11:55:16 -0600658 }
659 }
660
661 // we are done with all that we wanted to do. This will do the job of
662 // calling any destructors too.
Patrick Ventureef83b992018-10-21 09:32:44 -0700663 fruAreaVec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500664
665 return rc;
666}