blob: 2c0b64fa824d936e92faf33fc08d67cdbb15417f [file] [log] [blame]
Patrick Venture02ba8842018-10-20 13:37:54 -07001#include "writefrudata.hpp"
2
Patrick Venture5c2bd5e2018-10-20 19:45:48 -07003#include "fru_area.hpp"
Patrick Venturec9508db2018-10-16 17:18:43 -07004#include "frup.hpp"
5#include "types.hpp"
6
William A. Kennington III8ab57842019-02-12 12:49:51 -08007#include <ipmid/api.h>
Chris Austenb45c4cb2015-11-01 06:34:56 -06008#include <unistd.h>
Patrick Venturec9508db2018-10-16 17:18:43 -07009
George Liu98de42a2025-07-30 09:54:21 +080010#include <phosphor-logging/lg2.hpp>
Patrick Williamscfa96af2023-05-10 07:50:26 -050011#include <sdbusplus/bus.hpp>
12
Patrick Venturec9508db2018-10-16 17:18:43 -070013#include <algorithm>
Patrick Williamsbd2d8692023-07-19 10:59:27 -050014#include <array>
Patrick Venture5c787212018-10-17 13:48:23 -070015#include <cstdio>
16#include <cstring>
Patrick Venturec9508db2018-10-16 17:18:43 -070017#include <exception>
18#include <fstream>
vishwac93d6d42015-12-16 11:55:16 -060019#include <iostream>
Patrick Ventureb390c0e2018-10-20 13:36:48 -070020#include <map>
vishwac93d6d42015-12-16 11:55:16 -060021#include <memory>
Patrick Williamsbd2d8692023-07-19 10:59:27 -050022#include <span>
Patrick Venturec9508db2018-10-16 17:18:43 -070023#include <sstream>
24#include <vector>
Vishwa4be4b7a2015-10-31 22:55:50 -050025
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060026using namespace ipmi::vpd;
27
Ratan Guptacb0d4e52016-12-22 19:05:57 +053028extern const FruMap frus;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060029extern const std::map<Path, InterfaceMap> extras;
Ratan Guptacb0d4e52016-12-22 19:05:57 +053030
Patrick Venture19bea9a2018-10-20 19:54:27 -070031using FruAreaVector = std::vector<std::unique_ptr<IPMIFruArea>>;
32
Patrick Venture234b7352018-10-20 09:37:09 -070033namespace
34{
35
Patrick Venture6cc89042018-10-21 14:26:39 -070036/**
37 * Cleanup routine
38 * Must always be called as last reference to fruFilePointer.
39 *
40 * @param[in] fruFilePointer - FRU file pointer to close
41 * @param[in] fruAreaVec - vector of FRU areas
42 * @return -1
43 */
Patrick Ventureef83b992018-10-21 09:32:44 -070044int cleanupError(FILE* fruFilePointer, FruAreaVector& fruAreaVec)
Patrick Venture234b7352018-10-20 09:37:09 -070045{
Jayanth Othayoth883aa422024-12-18 10:33:06 -060046 if (fruFilePointer != nullptr)
Patrick Venture234b7352018-10-20 09:37:09 -070047 {
Patrick Ventureef83b992018-10-21 09:32:44 -070048 std::fclose(fruFilePointer);
Patrick Venture234b7352018-10-20 09:37:09 -070049 }
50
Patrick Ventureef83b992018-10-21 09:32:44 -070051 if (!(fruAreaVec.empty()))
Patrick Venture234b7352018-10-20 09:37:09 -070052 {
Patrick Ventureef83b992018-10-21 09:32:44 -070053 fruAreaVec.clear();
Patrick Venture234b7352018-10-20 09:37:09 -070054 }
55
56 return -1;
57}
58
Patrick Venture6cc89042018-10-21 14:26:39 -070059/**
60 * Gets the value of the key from the FRU dictionary of the given section.
61 * FRU dictionary is parsed FRU data for all the sections.
62 *
63 * @param[in] section - FRU section name
Manojkiran Edabc5725d2024-06-17 11:50:54 +053064 * @param[in] key - key for section
Patrick Venture6cc89042018-10-21 14:26:39 -070065 * @param[in] delimiter - delimiter for parsing custom fields
66 * @param[in] fruData - the FRU data to search for the section
67 * @return FRU value
68 */
Patrick Venturede8ea562018-10-20 09:44:19 -070069std::string getFRUValue(const std::string& section, const std::string& key,
70 const std::string& delimiter, IPMIFruInfo& fruData)
71{
Patrick Venturede8ea562018-10-20 09:44:19 -070072 auto minIndexValue = 0;
73 auto maxIndexValue = 0;
74 std::string fruValue = "";
75
76 if (section == "Board")
77 {
78 minIndexValue = OPENBMC_VPD_KEY_BOARD_MFG_DATE;
79 maxIndexValue = OPENBMC_VPD_KEY_BOARD_MAX;
80 }
81 else if (section == "Product")
82 {
83 minIndexValue = OPENBMC_VPD_KEY_PRODUCT_MFR;
84 maxIndexValue = OPENBMC_VPD_KEY_PRODUCT_MAX;
85 }
86 else if (section == "Chassis")
87 {
88 minIndexValue = OPENBMC_VPD_KEY_CHASSIS_TYPE;
89 maxIndexValue = OPENBMC_VPD_KEY_CHASSIS_MAX;
90 }
91
92 auto first = fruData.cbegin() + minIndexValue;
93 auto last = first + (maxIndexValue - minIndexValue) + 1;
94
Patrick Williamsa3cc2282024-08-16 15:20:14 -040095 auto itr = std::find_if(first, last, [&key](const auto& e) {
96 return key == e.first;
97 });
Patrick Venturede8ea562018-10-20 09:44:19 -070098
99 if (itr != last)
100 {
101 fruValue = itr->second;
102 }
103
104 // if the key is custom property then the value could be in two formats.
105 // 1) custom field 2 = "value".
106 // 2) custom field 2 = "key:value".
107 // if delimiter length = 0 i.e custom field 2 = "value"
108
109 constexpr auto customProp = "Custom Field";
110 if (key.find(customProp) != std::string::npos)
111 {
112 if (delimiter.length() > 0)
113 {
114 size_t delimiterpos = fruValue.find(delimiter);
115 if (delimiterpos != std::string::npos)
Patrick Venture740d8c02018-10-21 12:53:05 -0700116 {
Patrick Venturede8ea562018-10-20 09:44:19 -0700117 fruValue = fruValue.substr(delimiterpos + 1);
Patrick Venture740d8c02018-10-21 12:53:05 -0700118 }
Patrick Venturede8ea562018-10-20 09:44:19 -0700119 }
120 }
121 return fruValue;
122}
123
Patrick Venture6cc89042018-10-21 14:26:39 -0700124/**
125 * Get the inventory service from the mapper.
126 *
127 * @param[in] bus - sdbusplus handle to use for dbus call
128 * @param[in] intf - interface
129 * @param[in] path - the object path
130 * @return the dbus service that owns the interface for that path
131 */
Patrick Williams5e8829e2022-07-22 19:26:53 -0500132auto getService(sdbusplus::bus_t& bus, const std::string& intf,
Patrick Venture17baa272018-10-20 09:48:08 -0700133 const std::string& path)
134{
Patrick Williamsa3cc2282024-08-16 15:20:14 -0400135 auto mapperCall =
136 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
137 "/xyz/openbmc_project/object_mapper",
138 "xyz.openbmc_project.ObjectMapper", "GetObject");
Patrick Venture17baa272018-10-20 09:48:08 -0700139
140 mapperCall.append(path);
141 mapperCall.append(std::vector<std::string>({intf}));
142 std::map<std::string, std::vector<std::string>> mapperResponse;
143
144 try
145 {
146 auto mapperResponseMsg = bus.call(mapperCall);
147 mapperResponseMsg.read(mapperResponse);
148 }
Patrick Williams5e8829e2022-07-22 19:26:53 -0500149 catch (const sdbusplus::exception_t& ex)
Patrick Venture17baa272018-10-20 09:48:08 -0700150 {
George Liu98de42a2025-07-30 09:54:21 +0800151 lg2::error("Exception from sdbus call: {ERROR}", "ERROR", ex);
Patrick Venture17baa272018-10-20 09:48:08 -0700152 throw;
153 }
154
155 if (mapperResponse.begin() == mapperResponse.end())
156 {
157 throw std::runtime_error("ERROR in reading the mapper response");
158 }
159
160 return mapperResponse.begin()->first;
161}
162
Patrick Venture6cc89042018-10-21 14:26:39 -0700163/**
164 * Takes FRU data, invokes Parser for each FRU record area and updates
165 * inventory.
166 *
167 * @param[in] areaVector - vector of FRU areas
168 * @param[in] bus - handle to sdbus for calling methods, etc
169 * @return return non-zero of failure
170 */
Patrick Williams5e8829e2022-07-22 19:26:53 -0500171int updateInventory(FruAreaVector& areaVector, sdbusplus::bus_t& bus)
Patrick Venture44a957d2018-10-20 09:41:29 -0700172{
173 // Generic error reporter
174 int rc = 0;
175 uint8_t fruid = 0;
176 IPMIFruInfo fruData;
177
178 // For each FRU area, extract the needed data , get it parsed and update
179 // the Inventory.
Patrick Ventureef83b992018-10-21 09:32:44 -0700180 for (const auto& fruArea : areaVector)
Patrick Venture44a957d2018-10-20 09:41:29 -0700181 {
Patrick Venturef22b36a2018-10-20 20:59:07 -0700182 fruid = fruArea->getFruID();
Patrick Venture44a957d2018-10-20 09:41:29 -0700183 // Fill the container with information
Patrick Venture355c5612018-10-22 08:49:36 -0700184 rc = parse_fru_area(fruArea->getType(),
185 static_cast<const void*>(fruArea->getData()),
186 fruArea->getLength(), fruData);
Patrick Venture44a957d2018-10-20 09:41:29 -0700187 if (rc < 0)
188 {
George Liu98de42a2025-07-30 09:54:21 +0800189 lg2::error("Error parsing FRU records: {RC}", "RC", rc);
Patrick Venture44a957d2018-10-20 09:41:29 -0700190 return rc;
191 }
192 } // END walking the vector of areas and updating
193
Patrick Venture6cc89042018-10-21 14:26:39 -0700194 // For each FRU we have the list of instances which needs to be updated.
Patrick Venture44a957d2018-10-20 09:41:29 -0700195 // Each instance object implements certain interfaces.
196 // Each Interface is having Dbus properties.
197 // Each Dbus Property would be having metaData(eg section,VpdPropertyName).
198
199 // Here we are just printing the object,interface and the properties.
200 // which needs to be called with the new inventory manager implementation.
Patrick Venture44a957d2018-10-20 09:41:29 -0700201 using namespace std::string_literals;
202 static const auto intf = "xyz.openbmc_project.Inventory.Manager"s;
203 static const auto path = "/xyz/openbmc_project/inventory"s;
204 std::string service;
205 try
206 {
207 service = getService(bus, intf, path);
208 }
209 catch (const std::exception& e)
210 {
George Liu98de42a2025-07-30 09:54:21 +0800211 lg2::error("Failed to get service: {ERROR}", "ERROR", e);
Patrick Venture44a957d2018-10-20 09:41:29 -0700212 return -1;
213 }
214
215 auto iter = frus.find(fruid);
216 if (iter == frus.end())
217 {
George Liu98de42a2025-07-30 09:54:21 +0800218 lg2::error("Unable to find fru id:({FRUID}) in generated list", "FRUID",
219 fruid);
Patrick Venture44a957d2018-10-20 09:41:29 -0700220 return -1;
221 }
222
223 auto& instanceList = iter->second;
224 if (instanceList.size() <= 0)
225 {
George Liu98de42a2025-07-30 09:54:21 +0800226 lg2::debug("Object list empty for this fru id:({FRUID})", "FRUID",
227 fruid);
Patrick Venture44a957d2018-10-20 09:41:29 -0700228 }
229
230 ObjectMap objects;
Patrick Venturec7eecc12018-10-21 08:55:44 -0700231 for (const auto& instance : instanceList)
Patrick Venture44a957d2018-10-20 09:41:29 -0700232 {
233 InterfaceMap interfaces;
234 const auto& extrasIter = extras.find(instance.path);
235
Patrick Venturec7eecc12018-10-21 08:55:44 -0700236 for (const auto& interfaceList : instance.interfaces)
Patrick Venture44a957d2018-10-20 09:41:29 -0700237 {
238 PropertyMap props; // store all the properties
Patrick Venturec7eecc12018-10-21 08:55:44 -0700239 for (const auto& properties : interfaceList.second)
Patrick Venture44a957d2018-10-20 09:41:29 -0700240 {
241 std::string value;
242 decltype(auto) pdata = properties.second;
243
244 if (!pdata.section.empty() && !pdata.property.empty())
245 {
246 value = getFRUValue(pdata.section, pdata.property,
247 pdata.delimiter, fruData);
248 }
249 props.emplace(std::move(properties.first), std::move(value));
250 }
251 // Check and update extra properties
252 if (extras.end() != extrasIter)
253 {
254 const auto& propsIter =
255 (extrasIter->second).find(interfaceList.first);
256 if ((extrasIter->second).end() != propsIter)
257 {
258 for (const auto& map : propsIter->second)
259 {
260 props.emplace(map.first, map.second);
261 }
262 }
263 }
264 interfaces.emplace(std::move(interfaceList.first),
265 std::move(props));
266 }
267
268 // Call the inventory manager
Patrick Venture9a528f22018-10-20 13:31:28 -0700269 sdbusplus::message::object_path objectPath = instance.path;
Patrick Venture44a957d2018-10-20 09:41:29 -0700270 // Check and update extra properties
271 if (extras.end() != extrasIter)
272 {
273 for (const auto& entry : extrasIter->second)
274 {
275 if (interfaces.end() == interfaces.find(entry.first))
276 {
277 interfaces.emplace(entry.first, entry.second);
278 }
279 }
280 }
Patrick Venture9a528f22018-10-20 13:31:28 -0700281 objects.emplace(objectPath, interfaces);
Patrick Venture44a957d2018-10-20 09:41:29 -0700282 }
283
284 auto pimMsg = bus.new_method_call(service.c_str(), path.c_str(),
285 intf.c_str(), "Notify");
286 pimMsg.append(std::move(objects));
287
288 try
289 {
290 auto inventoryMgrResponseMsg = bus.call(pimMsg);
291 }
Patrick Williams5e8829e2022-07-22 19:26:53 -0500292 catch (const sdbusplus::exception_t& ex)
Patrick Venture44a957d2018-10-20 09:41:29 -0700293 {
George Liu98de42a2025-07-30 09:54:21 +0800294 lg2::error(
295 "Error in notify call, service: {SERVICE}, path: {PATH}, error: {ERROR}",
296 "SERVICE", service, "PATH", path, "ERROR", ex);
Patrick Venture44a957d2018-10-20 09:41:29 -0700297 return -1;
298 }
299
300 return rc;
301}
302
Patrick Venture234b7352018-10-20 09:37:09 -0700303} // namespace
304
Patrick Venture6cc89042018-10-21 14:26:39 -0700305/**
306 * Takes the pointer to stream of bytes and length and returns the 8 bit
307 * checksum. This algo is per IPMI V2.0 spec
308 *
309 * @param[in] data - data for running crc
310 * @param[in] len - the length over which to run the crc
311 * @return the CRC value
312 */
Patrick Venture062e1452018-10-21 09:16:47 -0700313unsigned char calculateCRC(const unsigned char* data, size_t len)
Vishwa4be4b7a2015-10-31 22:55:50 -0500314{
315 char crc = 0;
vishwac93d6d42015-12-16 11:55:16 -0600316 size_t byte = 0;
Vishwa4be4b7a2015-10-31 22:55:50 -0500317
Patrick Venturec9508db2018-10-16 17:18:43 -0700318 for (byte = 0; byte < len; byte++)
Vishwa4be4b7a2015-10-31 22:55:50 -0500319 {
320 crc += *data++;
321 }
vishwaf3ca3522015-12-02 10:35:13 -0600322
Patrick Venturec9508db2018-10-16 17:18:43 -0700323 return (-crc);
Vishwa4be4b7a2015-10-31 22:55:50 -0500324}
325
Patrick Venture6cc89042018-10-21 14:26:39 -0700326/**
Manojkiran Edabc5725d2024-06-17 11:50:54 +0530327 * Accepts a FRU area offset into a common header and tells which area it is.
Patrick Venture6cc89042018-10-21 14:26:39 -0700328 *
329 * @param[in] areaOffset - offset to lookup the area type
330 * @return the ipmi_fru_area_type
331 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700332ipmi_fru_area_type getFruAreaType(uint8_t areaOffset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500333{
334 ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
335
Patrick Ventureef83b992018-10-21 09:32:44 -0700336 switch (areaOffset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500337 {
338 case IPMI_FRU_INTERNAL_OFFSET:
339 type = IPMI_FRU_AREA_INTERNAL_USE;
340 break;
341
342 case IPMI_FRU_CHASSIS_OFFSET:
343 type = IPMI_FRU_AREA_CHASSIS_INFO;
344 break;
345
346 case IPMI_FRU_BOARD_OFFSET:
347 type = IPMI_FRU_AREA_BOARD_INFO;
348 break;
349
350 case IPMI_FRU_PRODUCT_OFFSET:
351 type = IPMI_FRU_AREA_PRODUCT_INFO;
352 break;
353
354 case IPMI_FRU_MULTI_OFFSET:
355 type = IPMI_FRU_AREA_MULTI_RECORD;
356 break;
357
358 default:
359 type = IPMI_FRU_AREA_TYPE_MAX;
360 }
361
362 return type;
363}
364
Patrick Venture6cc89042018-10-21 14:26:39 -0700365/**
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000366 * Validates the data for multirecord fields and CRC if selected
367 *
368 * @param[in] data - the data to verify
369 * @param[in] len - the length of the region to verify
370 * @param[in] validateCrc - whether to validate the CRC
371 * @return non-zero on failure
372 */
373int verifyFruMultiRecData(const uint8_t* data, const size_t len,
374 bool validateCrc)
375{
376 uint8_t checksum = 0;
377 int rc = -1;
378
379 if (!validateCrc)
380 {
381 // There's nothing else to do for this area.
382 return EXIT_SUCCESS;
383 }
384
385 // As per the IPMI platform spec, byte[3] is the record checksum.
386 checksum = calculateCRC(data, len);
387 if (checksum != data[3])
388 {
George Liu98de42a2025-07-30 09:54:21 +0800389 lg2::debug("Checksum mismatch, Calculated={CALC}, Embedded={EMBED}",
390 "CALC", lg2::hex, checksum, "EMBED", lg2::hex, data[3]);
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000391 return rc;
392 }
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000393
394 return EXIT_SUCCESS;
395}
396
397/**
Oskar Senft69540862018-12-04 15:52:31 -0500398 * Validates the data for mandatory fields and CRC if selected.
Patrick Venture6cc89042018-10-21 14:26:39 -0700399 *
400 * @param[in] data - the data to verify
401 * @param[in] len - the length of the region to verify
Oskar Senft69540862018-12-04 15:52:31 -0500402 * @param[in] validateCrc - whether to validate the CRC
Patrick Venture6cc89042018-10-21 14:26:39 -0700403 * @return non-zero on failure
404 */
Oskar Senft69540862018-12-04 15:52:31 -0500405int verifyFruData(const uint8_t* data, const size_t len, bool validateCrc)
vishwac93d6d42015-12-16 11:55:16 -0600406{
407 uint8_t checksum = 0;
408 int rc = -1;
409
410 // Validate for first byte to always have a value of [1]
Patrick Venturec9508db2018-10-16 17:18:43 -0700411 if (data[0] != IPMI_FRU_HDR_BYTE_ZERO)
vishwac93d6d42015-12-16 11:55:16 -0600412 {
George Liu98de42a2025-07-30 09:54:21 +0800413 lg2::error("Invalid entry in byte-0, entry: {ENTRY}", "ENTRY", lg2::hex,
414 data[0]);
vishwac93d6d42015-12-16 11:55:16 -0600415 return rc;
416 }
George Liu98de42a2025-07-30 09:54:21 +0800417 lg2::debug("Validated in entry_1 of fruData,entry: {ENTRY}", "ENTRY",
418 lg2::hex, data[0]);
vishwac93d6d42015-12-16 11:55:16 -0600419
Oskar Senft69540862018-12-04 15:52:31 -0500420 if (!validateCrc)
421 {
422 // There's nothing else to do for this area.
423 return EXIT_SUCCESS;
424 }
425
vishwac93d6d42015-12-16 11:55:16 -0600426 // See if the calculated CRC matches with the embedded one.
427 // CRC to be calculated on all except the last one that is CRC itself.
Patrick Venture062e1452018-10-21 09:16:47 -0700428 checksum = calculateCRC(data, len - 1);
Patrick Venturec9508db2018-10-16 17:18:43 -0700429 if (checksum != data[len - 1])
vishwac93d6d42015-12-16 11:55:16 -0600430 {
George Liu98de42a2025-07-30 09:54:21 +0800431 lg2::debug("Checksum mismatch, Calculated={CALC}, Embedded={EMBED}",
432 "CALC", lg2::hex, checksum, "EMBED", lg2::hex, data[len]);
vishwac93d6d42015-12-16 11:55:16 -0600433 return rc;
434 }
vishwac93d6d42015-12-16 11:55:16 -0600435
436 return EXIT_SUCCESS;
437}
438
Patrick Venture6cc89042018-10-21 14:26:39 -0700439/**
440 * Checks if a particular FRU area is populated or not.
441 *
442 * @param[in] reference to FRU area pointer
443 * @return true if the area is empty
444 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700445bool removeInvalidArea(const std::unique_ptr<IPMIFruArea>& fruArea)
Vishwa4be4b7a2015-10-31 22:55:50 -0500446{
Deepak Kodihalli89ededd2017-02-11 01:59:32 -0600447 // Filter the ones that are empty
Patrick Ventureef83b992018-10-21 09:32:44 -0700448 if (!(fruArea->getLength()))
vishwac93d6d42015-12-16 11:55:16 -0600449 {
450 return true;
451 }
452 return false;
453}
Vishwa4be4b7a2015-10-31 22:55:50 -0500454
Patrick Venture6cc89042018-10-21 14:26:39 -0700455/**
456 * Populates various FRU areas.
457 *
458 * @prereq : This must be called only after validating common header
459 * @param[in] fruData - pointer to the FRU bytes
460 * @param[in] dataLen - the length of the FRU data
461 * @param[in] fruAreaVec - the FRU area vector to update
462 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700463int ipmiPopulateFruAreas(uint8_t* fruData, const size_t dataLen,
464 FruAreaVector& fruAreaVec)
vishwac93d6d42015-12-16 11:55:16 -0600465{
Manojkiran Edabc5725d2024-06-17 11:50:54 +0530466 // Now walk the common header and see if the file size has at least the last
Patrick Ventureef83b992018-10-21 09:32:44 -0700467 // offset mentioned by the struct common_header. If the file size is less
Patrick Venture6cc89042018-10-21 14:26:39 -0700468 // than the offset of any if the FRU areas mentioned in the common header,
Patrick Ventureef83b992018-10-21 09:32:44 -0700469 // then we do not have a complete file.
470 for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET;
471 fruEntry < (sizeof(struct common_header) - 2); fruEntry++)
vishwac93d6d42015-12-16 11:55:16 -0600472 {
Patrick Venture2cdb0e32019-08-05 07:47:01 -0700473 int rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600474 // Actual offset in the payload is the offset mentioned in common header
Gunnar Millsc19b8132018-06-14 08:56:17 -0500475 // multiplied by 8. Common header is always the first 8 bytes.
Patrick Ventureef83b992018-10-21 09:32:44 -0700476 size_t areaOffset = fruData[fruEntry] * IPMI_EIGHT_BYTES;
477 if (areaOffset && (dataLen < (areaOffset + 2)))
vishwac93d6d42015-12-16 11:55:16 -0600478 {
479 // Our file size is less than what it needs to be. +2 because we are
Patrick Ventureef83b992018-10-21 09:32:44 -0700480 // using area len that is at 2 byte off areaOffset
George Liu98de42a2025-07-30 09:54:21 +0800481 lg2::error("FRU file is incomplete, size: {SIZE}", "SIZE", dataLen);
vishwac93d6d42015-12-16 11:55:16 -0600482 return rc;
483 }
Patrick Ventureef83b992018-10-21 09:32:44 -0700484 else if (areaOffset)
vishwac93d6d42015-12-16 11:55:16 -0600485 {
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000486 // Read 3 bytes to know the actual size of area.
487 uint8_t areaHeader[3] = {0};
Patrick Ventureef83b992018-10-21 09:32:44 -0700488 std::memcpy(areaHeader, &((uint8_t*)fruData)[areaOffset],
489 sizeof(areaHeader));
Vishwa4be4b7a2015-10-31 22:55:50 -0500490
Patrick Venture6cc89042018-10-21 14:26:39 -0700491 // Size of this area will be the 2nd byte in the FRU area header.
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000492 size_t areaLen;
493 if (fruEntry == IPMI_FRU_MULTI_OFFSET)
494 {
495 areaLen = areaHeader[2] + IPMI_FRU_MULTIREC_HDR_BYTES;
496 }
497 else
498 {
499 areaLen = areaHeader[1] * IPMI_EIGHT_BYTES;
500 }
501
George Liu98de42a2025-07-30 09:54:21 +0800502 lg2::debug(
503 "FRU Data, size: {SIZE}, area offset: {OFFSET}, area size: {AREA_SIZE}",
504 "SIZE", dataLen, "OFFSET", areaOffset, "AREA_SIZE", areaLen);
Vishwa4be4b7a2015-10-31 22:55:50 -0500505
vishwac93d6d42015-12-16 11:55:16 -0600506 // See if we really have that much buffer. We have area offset amd
507 // from there, the actual len.
Patrick Ventureef83b992018-10-21 09:32:44 -0700508 if (dataLen < (areaLen + areaOffset))
vishwac93d6d42015-12-16 11:55:16 -0600509 {
George Liu98de42a2025-07-30 09:54:21 +0800510 lg2::error("Incomplete FRU file, size: {SIZE}", "SIZE",
511 dataLen);
vishwac93d6d42015-12-16 11:55:16 -0600512 return rc;
513 }
514
Patrick Williamsa3cc2282024-08-16 15:20:14 -0400515 auto fruDataView =
516 std::span<uint8_t>(&fruData[areaOffset], areaLen);
517 auto areaData =
518 std::vector<uint8_t>(fruDataView.begin(), fruDataView.end());
vishwac93d6d42015-12-16 11:55:16 -0600519
Oskar Senft69540862018-12-04 15:52:31 -0500520 // Validate the CRC, but not for the internal use area, since its
521 // contents beyond the first byte are not defined in the spec and
522 // it may not end with a CRC byte.
523 bool validateCrc = fruEntry != IPMI_FRU_INTERNAL_OFFSET;
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000524
525 if (fruEntry == IPMI_FRU_MULTI_OFFSET)
526 {
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500527 rc = verifyFruMultiRecData(areaData.data(), areaLen,
528 validateCrc);
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000529 }
530 else
531 {
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500532 rc = verifyFruData(areaData.data(), areaLen, validateCrc);
Thang Q. Nguyen472d3e22021-01-06 04:17:40 +0000533 }
534
Patrick Venturec9508db2018-10-16 17:18:43 -0700535 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600536 {
George Liu98de42a2025-07-30 09:54:21 +0800537 lg2::error("Err validating FRU area, offset: {OFFSET}",
538 "OFFSET", areaOffset);
vishwac93d6d42015-12-16 11:55:16 -0600539 return rc;
540 }
George Liu98de42a2025-07-30 09:54:21 +0800541 lg2::debug("Successfully verified area, offset: {OFFSET}", "OFFSET",
542 areaOffset);
vishwac93d6d42015-12-16 11:55:16 -0600543
544 // We already have a vector that is passed to us containing all
545 // of the fields populated. Update the data portion now.
Patrick Ventureef83b992018-10-21 09:32:44 -0700546 for (auto& iter : fruAreaVec)
vishwac93d6d42015-12-16 11:55:16 -0600547 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700548 if (iter->getType() == getFruAreaType(fruEntry))
vishwac93d6d42015-12-16 11:55:16 -0600549 {
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500550 iter->setData(areaData.data(), areaLen);
vishwac93d6d42015-12-16 11:55:16 -0600551 }
552 }
Patrick Venture6cc89042018-10-21 14:26:39 -0700553 } // If we have FRU data present
Patrick Williamsa3cc2282024-08-16 15:20:14 -0400554 } // Walk struct common_header
vishwac93d6d42015-12-16 11:55:16 -0600555
Patrick Venture6cc89042018-10-21 14:26:39 -0700556 // Not all the fields will be populated in a FRU data. Mostly all cases will
vishwac93d6d42015-12-16 11:55:16 -0600557 // not have more than 2 or 3.
Patrick Ventureef83b992018-10-21 09:32:44 -0700558 fruAreaVec.erase(
559 std::remove_if(fruAreaVec.begin(), fruAreaVec.end(), removeInvalidArea),
560 fruAreaVec.end());
vishwac93d6d42015-12-16 11:55:16 -0600561
562 return EXIT_SUCCESS;
563}
564
Patrick Venture6cc89042018-10-21 14:26:39 -0700565/**
566 * Validates the FRU data per ipmi common header constructs.
567 * Returns with updated struct common_header and also file_size
568 *
569 * @param[in] fruData - the FRU data
570 * @param[in] dataLen - the length of the data
571 * @return non-zero on failure
572 */
Patrick Ventureef83b992018-10-21 09:32:44 -0700573int ipmiValidateCommonHeader(const uint8_t* fruData, const size_t dataLen)
vishwac93d6d42015-12-16 11:55:16 -0600574{
575 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500576
Patrick Ventureef83b992018-10-21 09:32:44 -0700577 uint8_t commonHdr[sizeof(struct common_header)] = {0};
578 if (dataLen >= sizeof(commonHdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500579 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700580 std::memcpy(commonHdr, fruData, sizeof(commonHdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500581 }
582 else
583 {
George Liu98de42a2025-07-30 09:54:21 +0800584 lg2::error("Incomplete FRU data file, size: {SIZE}", "SIZE", dataLen);
vishwac93d6d42015-12-16 11:55:16 -0600585 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500586 }
587
Patrick Venture6cc89042018-10-21 14:26:39 -0700588 // Verify the CRC and size
Oskar Senft69540862018-12-04 15:52:31 -0500589 rc = verifyFruData(commonHdr, sizeof(commonHdr), true);
Patrick Venturec9508db2018-10-16 17:18:43 -0700590 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500591 {
George Liu98de42a2025-07-30 09:54:21 +0800592 lg2::error("Failed to validate common header");
vishwac93d6d42015-12-16 11:55:16 -0600593 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500594 }
595
vishwac93d6d42015-12-16 11:55:16 -0600596 return EXIT_SUCCESS;
597}
Vishwa4be4b7a2015-10-31 22:55:50 -0500598
Patrick Ventureef83b992018-10-21 09:32:44 -0700599int validateFRUArea(const uint8_t fruid, const char* fruFilename,
Jayanth Othayoth70cb0672025-06-07 02:13:23 -0500600 sdbusplus::bus_t& bus)
Vishwa4be4b7a2015-10-31 22:55:50 -0500601{
Patrick Ventureef83b992018-10-21 09:32:44 -0700602 size_t dataLen = 0;
603 size_t bytesRead = 0;
vishwac93d6d42015-12-16 11:55:16 -0600604 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500605
vishwac93d6d42015-12-16 11:55:16 -0600606 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
607 // are not used, keeping it here for completeness.
Patrick Ventureef83b992018-10-21 09:32:44 -0700608 FruAreaVector fruAreaVec;
Yi Li75c2d462016-04-11 16:57:46 +0800609
Patrick Ventureef83b992018-10-21 09:32:44 -0700610 for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET;
611 fruEntry < (sizeof(struct common_header) - 2); fruEntry++)
vishwac93d6d42015-12-16 11:55:16 -0600612 {
613 // Create an object and push onto a vector.
Jayanth Othayoth70cb0672025-06-07 02:13:23 -0500614 std::unique_ptr<IPMIFruArea> fruArea =
615 std::make_unique<IPMIFruArea>(fruid, getFruAreaType(fruEntry));
vishwac93d6d42015-12-16 11:55:16 -0600616
617 // Physically being present
Patrick Ventureef83b992018-10-21 09:32:44 -0700618 bool present = access(fruFilename, F_OK) == 0;
619 fruArea->setPresent(present);
vishwac93d6d42015-12-16 11:55:16 -0600620
Patrick Ventureef83b992018-10-21 09:32:44 -0700621 fruAreaVec.emplace_back(std::move(fruArea));
vishwac93d6d42015-12-16 11:55:16 -0600622 }
623
Patrick Ventureef83b992018-10-21 09:32:44 -0700624 FILE* fruFilePointer = std::fopen(fruFilename, "rb");
Jayanth Othayoth883aa422024-12-18 10:33:06 -0600625 if (fruFilePointer == nullptr)
Vishwa4be4b7a2015-10-31 22:55:50 -0500626 {
George Liu98de42a2025-07-30 09:54:21 +0800627 lg2::error("Unable to open {FILE}, error: {ERRNO}", "FILE", fruFilename,
628 "ERRNO", std::strerror(errno));
Patrick Ventureef83b992018-10-21 09:32:44 -0700629 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500630 }
631
vishwac93d6d42015-12-16 11:55:16 -0600632 // Get the size of the file to see if it meets minimum requirement
Patrick Ventureef83b992018-10-21 09:32:44 -0700633 if (std::fseek(fruFilePointer, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500634 {
George Liu98de42a2025-07-30 09:54:21 +0800635 lg2::error("Unable to seek {FILE}, error: {ERRNO}", "FILE", fruFilename,
636 "ERRNO", std::strerror(errno));
Patrick Ventureef83b992018-10-21 09:32:44 -0700637 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500638 }
639
vishwac93d6d42015-12-16 11:55:16 -0600640 // Allocate a buffer to hold entire file content
Patrick Ventureef83b992018-10-21 09:32:44 -0700641 dataLen = std::ftell(fruFilePointer);
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500642
643 auto fruData = std::vector<uint8_t>(dataLen, 0);
Vishwa4be4b7a2015-10-31 22:55:50 -0500644
Patrick Ventureef83b992018-10-21 09:32:44 -0700645 std::rewind(fruFilePointer);
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500646 bytesRead = std::fread(fruData.data(), dataLen, 1, fruFilePointer);
Patrick Ventureef83b992018-10-21 09:32:44 -0700647 if (bytesRead != 1)
Vishwa4be4b7a2015-10-31 22:55:50 -0500648 {
George Liu98de42a2025-07-30 09:54:21 +0800649 lg2::error(
650 "Failed to reading FRU data, bytesRead: {BYTESREAD}, errno: {ERRNO}",
651 "BYTESREAD", bytesRead, "ERRNO", std::strerror(errno));
Patrick Ventureef83b992018-10-21 09:32:44 -0700652 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500653 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500654
vishwac93d6d42015-12-16 11:55:16 -0600655 // We are done reading.
Patrick Ventureef83b992018-10-21 09:32:44 -0700656 std::fclose(fruFilePointer);
Jayanth Othayoth883aa422024-12-18 10:33:06 -0600657 fruFilePointer = nullptr;
vishwac93d6d42015-12-16 11:55:16 -0600658
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500659 rc = ipmiValidateCommonHeader(fruData.data(), dataLen);
Patrick Venturec9508db2018-10-16 17:18:43 -0700660 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500661 {
Patrick Ventureef83b992018-10-21 09:32:44 -0700662 return cleanupError(fruFilePointer, fruAreaVec);
vishwac93d6d42015-12-16 11:55:16 -0600663 }
664
Patrick Venture6cc89042018-10-21 14:26:39 -0700665 // Now that we validated the common header, populate various FRU sections if
Patrick Venturec9508db2018-10-16 17:18:43 -0700666 // we have them here.
Patrick Williamsbd2d8692023-07-19 10:59:27 -0500667 rc = ipmiPopulateFruAreas(fruData.data(), dataLen, fruAreaVec);
Patrick Venturec9508db2018-10-16 17:18:43 -0700668 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600669 {
George Liu98de42a2025-07-30 09:54:21 +0800670 lg2::error("Populating fru id:({FRUID}) areas failed", "FRUID", fruid);
Patrick Ventureef83b992018-10-21 09:32:44 -0700671 return cleanupError(fruFilePointer, fruAreaVec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500672 }
George Liu98de42a2025-07-30 09:54:21 +0800673 lg2::debug("Populated FRU areas, file name: {FILE}", "FILE", fruFilename);
Vishwa4be4b7a2015-10-31 22:55:50 -0500674
Patrick Ventureef83b992018-10-21 09:32:44 -0700675 for (const auto& iter : fruAreaVec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500676 {
George Liu98de42a2025-07-30 09:54:21 +0800677 lg2::debug("fru id: {FRUID}", "FRUID", iter->getFruID());
678 lg2::debug("area name: {AREA}", "AREA", iter->getName());
679 lg2::debug("type: {TYPE}", "TYPE", iter->getType());
680 lg2::debug("length: {LEN}", "LEN", iter->getLength());
Vishwa4be4b7a2015-10-31 22:55:50 -0500681 }
vishwac93d6d42015-12-16 11:55:16 -0600682
683 // If the vector is populated with everything, then go ahead and update the
684 // inventory.
Patrick Ventureef83b992018-10-21 09:32:44 -0700685 if (!(fruAreaVec.empty()))
vishwac93d6d42015-12-16 11:55:16 -0600686 {
George Liu98de42a2025-07-30 09:54:21 +0800687 lg2::debug("fruAreaVec size: {SIZE}", "SIZE", fruAreaVec.size());
Patrick Ventureef83b992018-10-21 09:32:44 -0700688 rc = updateInventory(fruAreaVec, bus);
Patrick Venturec9508db2018-10-16 17:18:43 -0700689 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600690 {
George Liu98de42a2025-07-30 09:54:21 +0800691 lg2::error("Error updating inventory.");
vishwac93d6d42015-12-16 11:55:16 -0600692 }
693 }
694
695 // we are done with all that we wanted to do. This will do the job of
696 // calling any destructors too.
Patrick Ventureef83b992018-10-21 09:32:44 -0700697 fruAreaVec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500698
699 return rc;
700}