Coverage Report

Created: 2026-10-16 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ipmi-fru-parser/writefrudata.cpp
Line
Count
Source
1
#include "writefrudata.hpp"
2
3
#include "fru_area.hpp"
4
#include "frup.hpp"
5
#include "types.hpp"
6
7
#include <ipmid/api.h>
8
#include <unistd.h>
9
10
#include <phosphor-logging/lg2.hpp>
11
#include <sdbusplus/bus.hpp>
12
13
#include <algorithm>
14
#include <array>
15
#include <cstdio>
16
#include <cstring>
17
#include <exception>
18
#include <fstream>
19
#include <iostream>
20
#include <map>
21
#include <memory>
22
#include <span>
23
#include <sstream>
24
#include <vector>
25
26
using namespace ipmi::vpd;
27
28
extern const FruMap frus;
29
extern const std::map<Path, InterfaceMap> extras;
30
31
using FruAreaVector = std::vector<std::unique_ptr<IPMIFruArea>>;
32
33
namespace
34
{
35
36
/**
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
 */
44
int cleanupError(FILE* fruFilePointer, FruAreaVector& fruAreaVec)
45
0
{
46
0
    if (fruFilePointer != nullptr)
47
0
    {
48
0
        std::fclose(fruFilePointer);
49
0
    }
50
51
0
    if (!(fruAreaVec.empty()))
52
0
    {
53
0
        fruAreaVec.clear();
54
0
    }
55
56
0
    return -1;
57
0
}
58
59
/**
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
64
 * @param[in] key - key for section
65
 * @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
 */
69
std::string getFRUValue(const std::string& section, const std::string& key,
70
                        const std::string& delimiter, IPMIFruInfo& fruData)
71
0
{
72
0
    auto minIndexValue = 0;
73
0
    auto maxIndexValue = 0;
74
0
    std::string fruValue = "";
75
76
0
    if (section == "Board")
77
0
    {
78
0
        minIndexValue = OPENBMC_VPD_KEY_BOARD_MFG_DATE;
79
0
        maxIndexValue = OPENBMC_VPD_KEY_BOARD_MAX;
80
0
    }
81
0
    else if (section == "Product")
82
0
    {
83
0
        minIndexValue = OPENBMC_VPD_KEY_PRODUCT_MFR;
84
0
        maxIndexValue = OPENBMC_VPD_KEY_PRODUCT_MAX;
85
0
    }
86
0
    else if (section == "Chassis")
87
0
    {
88
0
        minIndexValue = OPENBMC_VPD_KEY_CHASSIS_TYPE;
89
0
        maxIndexValue = OPENBMC_VPD_KEY_CHASSIS_MAX;
90
0
    }
91
92
0
    auto first = fruData.cbegin() + minIndexValue;
93
0
    auto last = first + (maxIndexValue - minIndexValue) + 1;
94
95
0
    auto itr = std::find_if(first, last, [&key](const auto& e) {
96
0
        return key == e.first;
97
0
    });
98
99
0
    if (itr != last)
100
0
    {
101
0
        fruValue = itr->second;
102
0
    }
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
0
    constexpr auto customProp = "Custom Field";
110
0
    if (key.find(customProp) != std::string::npos)
111
0
    {
112
0
        if (delimiter.length() > 0)
113
0
        {
114
0
            size_t delimiterpos = fruValue.find(delimiter);
115
0
            if (delimiterpos != std::string::npos)
116
0
            {
117
0
                fruValue = fruValue.substr(delimiterpos + 1);
118
0
            }
119
0
        }
120
0
    }
121
0
    return fruValue;
122
0
}
123
124
/**
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
 */
132
auto getService(sdbusplus::bus_t& bus, const std::string& intf,
133
                const std::string& path)
134
0
{
135
0
    auto mapperCall =
136
0
        bus.new_method_call("xyz.openbmc_project.ObjectMapper",
137
0
                            "/xyz/openbmc_project/object_mapper",
138
0
                            "xyz.openbmc_project.ObjectMapper", "GetObject");
139
140
0
    mapperCall.append(path);
141
0
    mapperCall.append(std::vector<std::string>({intf}));
142
0
    std::map<std::string, std::vector<std::string>> mapperResponse;
143
144
0
    try
145
0
    {
146
0
        auto mapperResponseMsg = bus.call(mapperCall);
147
0
        mapperResponseMsg.read(mapperResponse);
148
0
    }
149
0
    catch (const sdbusplus::exception_t& ex)
150
0
    {
151
0
        lg2::error("Exception from sdbus call: {ERROR}", "ERROR", ex);
152
0
        throw;
153
0
    }
154
155
0
    if (mapperResponse.begin() == mapperResponse.end())
156
0
    {
157
0
        throw std::runtime_error("ERROR in reading the mapper response");
158
0
    }
159
160
0
    return mapperResponse.begin()->first;
161
0
}
162
163
/**
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
 */
171
int updateInventory(FruAreaVector& areaVector, sdbusplus::bus_t& bus)
172
0
{
173
    // Generic error reporter
174
0
    int rc = 0;
175
0
    uint8_t fruid = 0;
176
0
    IPMIFruInfo fruData;
177
178
    // For each FRU area, extract the needed data , get it parsed and update
179
    // the Inventory.
180
0
    for (const auto& fruArea : areaVector)
181
0
    {
182
0
        fruid = fruArea->getFruID();
183
        // Fill the container with information
184
0
        rc = parse_fru_area(fruArea->getType(),
185
0
                            static_cast<const void*>(fruArea->getData()),
186
0
                            fruArea->getLength(), fruData);
187
0
        if (rc < 0)
188
0
        {
189
0
            lg2::error("Error parsing FRU records: {RC}", "RC", rc);
190
0
            return rc;
191
0
        }
192
0
    } // END walking the vector of areas and updating
193
194
    // For each FRU we have the list of instances which needs to be updated.
195
    // 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.
201
0
    using namespace std::string_literals;
202
0
    static const auto intf = "xyz.openbmc_project.Inventory.Manager"s;
203
0
    static const auto path = "/xyz/openbmc_project/inventory"s;
204
0
    std::string service;
205
0
    try
206
0
    {
207
0
        service = getService(bus, intf, path);
208
0
    }
209
0
    catch (const std::exception& e)
210
0
    {
211
0
        lg2::error("Failed to get service: {ERROR}", "ERROR", e);
212
0
        return -1;
213
0
    }
214
215
0
    auto iter = frus.find(fruid);
216
0
    if (iter == frus.end())
217
0
    {
218
0
        lg2::error("Unable to find fru id:({FRUID}) in generated list", "FRUID",
219
0
                   fruid);
220
0
        return -1;
221
0
    }
222
223
0
    auto& instanceList = iter->second;
224
0
    if (instanceList.size() <= 0)
225
0
    {
226
0
        lg2::debug("Object list empty for this fru id:({FRUID})", "FRUID",
227
0
                   fruid);
228
0
    }
229
230
0
    ObjectMap objects;
231
0
    for (const auto& instance : instanceList)
232
0
    {
233
0
        InterfaceMap interfaces;
234
0
        const auto& extrasIter = extras.find(instance.path);
235
236
0
        for (const auto& interfaceList : instance.interfaces)
237
0
        {
238
0
            PropertyMap props; // store all the properties
239
0
            for (const auto& properties : interfaceList.second)
240
0
            {
241
0
                std::string value;
242
0
                decltype(auto) pdata = properties.second;
243
244
0
                if (!pdata.section.empty() && !pdata.property.empty())
245
0
                {
246
0
                    value = getFRUValue(pdata.section, pdata.property,
247
0
                                        pdata.delimiter, fruData);
248
0
                }
249
0
                props.emplace(std::move(properties.first), std::move(value));
250
0
            }
251
            // Check and update extra properties
252
0
            if (extras.end() != extrasIter)
253
0
            {
254
0
                const auto& propsIter =
255
0
                    (extrasIter->second).find(interfaceList.first);
256
0
                if ((extrasIter->second).end() != propsIter)
257
0
                {
258
0
                    for (const auto& map : propsIter->second)
259
0
                    {
260
0
                        props.emplace(map.first, map.second);
261
0
                    }
262
0
                }
263
0
            }
264
0
            interfaces.emplace(std::move(interfaceList.first),
265
0
                               std::move(props));
266
0
        }
267
268
        // Call the inventory manager
269
0
        sdbusplus::object_path objectPath = instance.path;
270
        // Check and update extra properties
271
0
        if (extras.end() != extrasIter)
272
0
        {
273
0
            for (const auto& entry : extrasIter->second)
274
0
            {
275
0
                if (interfaces.end() == interfaces.find(entry.first))
276
0
                {
277
0
                    interfaces.emplace(entry.first, entry.second);
278
0
                }
279
0
            }
280
0
        }
281
0
        objects.emplace(objectPath, interfaces);
282
0
    }
283
284
0
    auto pimMsg = bus.new_method_call(service.c_str(), path.c_str(),
285
0
                                      intf.c_str(), "Notify");
286
0
    pimMsg.append(std::move(objects));
287
288
0
    try
289
0
    {
290
0
        auto inventoryMgrResponseMsg = bus.call(pimMsg);
291
0
    }
292
0
    catch (const sdbusplus::exception_t& ex)
293
0
    {
294
0
        lg2::error(
295
0
            "Error in notify call, service: {SERVICE}, path: {PATH}, error: {ERROR}",
296
0
            "SERVICE", service, "PATH", path, "ERROR", ex);
297
0
        return -1;
298
0
    }
299
300
0
    return rc;
301
0
}
302
303
} // namespace
304
305
/**
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
 */
313
unsigned char calculateCRC(const unsigned char* data, size_t len)
314
673
{
315
673
    char crc = 0;
316
673
    size_t byte = 0;
317
318
16.4k
    for (byte = 0; byte < len; byte++)
319
15.7k
    {
320
15.7k
        crc += *data++;
321
15.7k
    }
322
323
673
    return (-crc);
324
673
}
325
326
/**
327
 * Accepts a FRU area offset into a common header and tells which area it is.
328
 *
329
 * @param[in] areaOffset - offset to lookup the area type
330
 * @return the ipmi_fru_area_type
331
 */
332
ipmi_fru_area_type getFruAreaType(uint8_t areaOffset)
333
3.61k
{
334
3.61k
    ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
335
336
3.61k
    switch (areaOffset)
337
3.61k
    {
338
1.03k
        case IPMI_FRU_INTERNAL_OFFSET:
339
1.03k
            type = IPMI_FRU_AREA_INTERNAL_USE;
340
1.03k
            break;
341
342
1.04k
        case IPMI_FRU_CHASSIS_OFFSET:
343
1.04k
            type = IPMI_FRU_AREA_CHASSIS_INFO;
344
1.04k
            break;
345
346
591
        case IPMI_FRU_BOARD_OFFSET:
347
591
            type = IPMI_FRU_AREA_BOARD_INFO;
348
591
            break;
349
350
596
        case IPMI_FRU_PRODUCT_OFFSET:
351
596
            type = IPMI_FRU_AREA_PRODUCT_INFO;
352
596
            break;
353
354
356
        case IPMI_FRU_MULTI_OFFSET:
355
356
            type = IPMI_FRU_AREA_MULTI_RECORD;
356
356
            break;
357
358
0
        default:
359
0
            type = IPMI_FRU_AREA_TYPE_MAX;
360
3.61k
    }
361
362
3.61k
    return type;
363
3.61k
}
364
365
/**
366
 * 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
 */
373
int verifyFruMultiRecData(const uint8_t* data, const size_t len,
374
                          bool validateCrc)
375
123
{
376
123
    uint8_t checksum = 0;
377
123
    int rc = -1;
378
379
123
    if (!validateCrc)
380
0
    {
381
        // There's nothing else to do for this area.
382
0
        return EXIT_SUCCESS;
383
0
    }
384
385
    // As per the IPMI platform spec, byte[3] is the record checksum.
386
123
    checksum = calculateCRC(data, len);
387
123
    if (checksum != data[3])
388
104
    {
389
104
        lg2::debug("Checksum mismatch, Calculated={CALC}, Embedded={EMBED}",
390
104
                   "CALC", lg2::hex, checksum, "EMBED", lg2::hex, data[3]);
391
104
        return rc;
392
104
    }
393
394
19
    return EXIT_SUCCESS;
395
123
}
396
397
/**
398
 * Validates the data for mandatory fields and CRC if selected.
399
 *
400
 * @param[in] data - the data to verify
401
 * @param[in] len - the length of the region to verify
402
 * @param[in] validateCrc - whether to validate the CRC
403
 * @return non-zero on failure
404
 */
405
int verifyFruData(const uint8_t* data, const size_t len, bool validateCrc)
406
1.02k
{
407
1.02k
    uint8_t checksum = 0;
408
1.02k
    int rc = -1;
409
410
    // Validate for first byte to always have a value of [1]
411
1.02k
    if (data[0] != IPMI_FRU_HDR_BYTE_ZERO)
412
318
    {
413
318
        lg2::error("Invalid entry in byte-0, entry: {ENTRY}", "ENTRY", lg2::hex,
414
318
                   data[0]);
415
318
        return rc;
416
318
    }
417
704
    lg2::debug("Validated in entry_1 of fruData,entry: {ENTRY}", "ENTRY",
418
704
               lg2::hex, data[0]);
419
420
704
    if (!validateCrc)
421
154
    {
422
        // There's nothing else to do for this area.
423
154
        return EXIT_SUCCESS;
424
154
    }
425
426
    // 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.
428
550
    checksum = calculateCRC(data, len - 1);
429
550
    if (checksum != data[len - 1])
430
0
    {
431
0
        lg2::debug("Checksum mismatch, Calculated={CALC}, Embedded={EMBED}",
432
0
                   "CALC", lg2::hex, checksum, "EMBED", lg2::hex, data[len]);
433
0
        return rc;
434
0
    }
435
436
550
    return EXIT_SUCCESS;
437
550
}
438
439
/**
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
 */
445
bool removeInvalidArea(const std::unique_ptr<IPMIFruArea>& fruArea)
446
215
{
447
    // Filter the ones that are empty
448
215
    if (!(fruArea->getLength()))
449
82
    {
450
82
        return true;
451
82
    }
452
133
    return false;
453
215
}
454
455
/**
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
 */
463
int ipmiPopulateFruAreas(uint8_t* fruData, const size_t dataLen,
464
                         FruAreaVector& fruAreaVec)
465
261
{
466
    // Now walk the common header and see if the file size has at least the last
467
    // offset mentioned by the struct common_header. If the file size is less
468
    // than the offset of any if the FRU areas mentioned in the common header,
469
    // then we do not have a complete file.
470
261
    for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET;
471
1.09k
         fruEntry < (sizeof(struct common_header) - 2); fruEntry++)
472
1.05k
    {
473
1.05k
        int rc = -1;
474
        // Actual offset in the payload is the offset mentioned in common header
475
        // multiplied by 8. Common header is always the first 8 bytes.
476
1.05k
        size_t areaOffset = fruData[fruEntry] * IPMI_EIGHT_BYTES;
477
1.05k
        if (areaOffset && (dataLen < (areaOffset + 2)))
478
37
        {
479
            // Our file size is less than what it needs to be. +2 because we are
480
            // using area len that is at 2 byte off areaOffset
481
37
            lg2::error("FRU file is incomplete, size: {SIZE}", "SIZE", dataLen);
482
37
            return rc;
483
37
        }
484
1.01k
        else if (areaOffset)
485
643
        {
486
            // Read 3 bytes to know the actual size of area.
487
643
            uint8_t areaHeader[3] = {0};
488
643
            std::memcpy(areaHeader, &((uint8_t*)fruData)[areaOffset],
489
643
                        sizeof(areaHeader));
490
491
            // Size of this area will be the 2nd byte in the FRU area header.
492
643
            size_t areaLen;
493
643
            if (fruEntry == IPMI_FRU_MULTI_OFFSET)
494
132
            {
495
132
                areaLen = areaHeader[2] + IPMI_FRU_MULTIREC_HDR_BYTES;
496
132
            }
497
511
            else
498
511
            {
499
511
                areaLen = areaHeader[1] * IPMI_EIGHT_BYTES;
500
511
            }
501
502
643
            lg2::debug(
503
643
                "FRU Data, size: {SIZE}, area offset: {OFFSET}, area size: {AREA_SIZE}",
504
643
                "SIZE", dataLen, "OFFSET", areaOffset, "AREA_SIZE", areaLen);
505
506
            // See if we really have that much buffer. We have area offset amd
507
            // from there, the actual len.
508
643
            if (dataLen < (areaLen + areaOffset))
509
43
            {
510
43
                lg2::error("Incomplete FRU file, size: {SIZE}", "SIZE",
511
43
                           dataLen);
512
43
                return rc;
513
43
            }
514
515
600
            auto fruDataView =
516
600
                std::span<uint8_t>(&fruData[areaOffset], areaLen);
517
600
            auto areaData =
518
600
                std::vector<uint8_t>(fruDataView.begin(), fruDataView.end());
519
520
            // 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
600
            bool validateCrc = fruEntry != IPMI_FRU_INTERNAL_OFFSET;
524
525
600
            if (fruEntry == IPMI_FRU_MULTI_OFFSET)
526
123
            {
527
123
                rc = verifyFruMultiRecData(areaData.data(), areaLen,
528
123
                                           validateCrc);
529
123
            }
530
477
            else
531
477
            {
532
477
                rc = verifyFruData(areaData.data(), areaLen, validateCrc);
533
477
            }
534
535
600
            if (rc < 0)
536
138
            {
537
138
                lg2::error("Err validating FRU area, offset: {OFFSET}",
538
138
                           "OFFSET", areaOffset);
539
138
                return rc;
540
138
            }
541
462
            lg2::debug("Successfully verified area, offset: {OFFSET}", "OFFSET",
542
462
                       areaOffset);
543
544
            // We already have a vector that is passed to us containing all
545
            // of the fields populated. Update the data portion now.
546
462
            for (auto& iter : fruAreaVec)
547
2.31k
            {
548
2.31k
                if (iter->getType() == getFruAreaType(fruEntry))
549
462
                {
550
462
                    iter->setData(areaData.data(), areaLen);
551
462
                }
552
2.31k
            }
553
462
        } // If we have FRU data present
554
1.05k
    } // Walk struct common_header
555
556
    // Not all the fields will be populated in a FRU data. Mostly all cases will
557
    // not have more than 2 or 3.
558
43
    fruAreaVec.erase(
559
43
        std::remove_if(fruAreaVec.begin(), fruAreaVec.end(), removeInvalidArea),
560
43
        fruAreaVec.end());
561
562
43
    return EXIT_SUCCESS;
563
261
}
564
565
/**
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
 */
573
int ipmiValidateCommonHeader(const uint8_t* fruData, const size_t dataLen)
574
545
{
575
545
    int rc = -1;
576
577
545
    uint8_t commonHdr[sizeof(struct common_header)] = {0};
578
545
    if (dataLen >= sizeof(commonHdr))
579
545
    {
580
545
        std::memcpy(commonHdr, fruData, sizeof(commonHdr));
581
545
    }
582
0
    else
583
0
    {
584
0
        lg2::error("Incomplete FRU data file, size: {SIZE}", "SIZE", dataLen);
585
0
        return rc;
586
0
    }
587
588
    // Verify the CRC and size
589
545
    rc = verifyFruData(commonHdr, sizeof(commonHdr), true);
590
545
    if (rc < 0)
591
284
    {
592
284
        lg2::error("Failed to validate common header");
593
284
        return rc;
594
284
    }
595
596
261
    return EXIT_SUCCESS;
597
545
}
598
599
int validateFRUArea(const uint8_t fruid, const char* fruFilename,
600
                    sdbusplus::bus_t& bus)
601
0
{
602
0
    size_t dataLen = 0;
603
0
    size_t bytesRead = 0;
604
0
    int rc = -1;
605
606
    // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
607
    // are not used, keeping it here for completeness.
608
0
    FruAreaVector fruAreaVec;
609
610
0
    for (uint8_t fruEntry = IPMI_FRU_INTERNAL_OFFSET;
611
0
         fruEntry < (sizeof(struct common_header) - 2); fruEntry++)
612
0
    {
613
        // Create an object and push onto a vector.
614
0
        std::unique_ptr<IPMIFruArea> fruArea =
615
0
            std::make_unique<IPMIFruArea>(fruid, getFruAreaType(fruEntry));
616
617
        // Physically being present
618
0
        bool present = access(fruFilename, F_OK) == 0;
619
0
        fruArea->setPresent(present);
620
621
0
        fruAreaVec.emplace_back(std::move(fruArea));
622
0
    }
623
624
0
    FILE* fruFilePointer = std::fopen(fruFilename, "rb");
625
0
    if (fruFilePointer == nullptr)
626
0
    {
627
0
        lg2::error("Unable to open {FILE}, error: {ERRNO}", "FILE", fruFilename,
628
0
                   "ERRNO", std::strerror(errno));
629
0
        return cleanupError(fruFilePointer, fruAreaVec);
630
0
    }
631
632
    // Get the size of the file to see if it meets minimum requirement
633
0
    if (std::fseek(fruFilePointer, 0, SEEK_END))
634
0
    {
635
0
        lg2::error("Unable to seek {FILE}, error: {ERRNO}", "FILE", fruFilename,
636
0
                   "ERRNO", std::strerror(errno));
637
0
        return cleanupError(fruFilePointer, fruAreaVec);
638
0
    }
639
640
    // Allocate a buffer to hold entire file content
641
0
    dataLen = std::ftell(fruFilePointer);
642
643
0
    auto fruData = std::vector<uint8_t>(dataLen, 0);
644
645
0
    std::rewind(fruFilePointer);
646
0
    bytesRead = std::fread(fruData.data(), dataLen, 1, fruFilePointer);
647
0
    if (bytesRead != 1)
648
0
    {
649
0
        lg2::error(
650
0
            "Failed to reading FRU data, bytesRead: {BYTESREAD}, errno: {ERRNO}",
651
0
            "BYTESREAD", bytesRead, "ERRNO", std::strerror(errno));
652
0
        return cleanupError(fruFilePointer, fruAreaVec);
653
0
    }
654
655
    // We are done reading.
656
0
    std::fclose(fruFilePointer);
657
0
    fruFilePointer = nullptr;
658
659
0
    rc = ipmiValidateCommonHeader(fruData.data(), dataLen);
660
0
    if (rc < 0)
661
0
    {
662
0
        return cleanupError(fruFilePointer, fruAreaVec);
663
0
    }
664
665
    // Now that we validated the common header, populate various FRU sections if
666
    // we have them here.
667
0
    rc = ipmiPopulateFruAreas(fruData.data(), dataLen, fruAreaVec);
668
0
    if (rc < 0)
669
0
    {
670
0
        lg2::error("Populating fru id:({FRUID}) areas failed", "FRUID", fruid);
671
0
        return cleanupError(fruFilePointer, fruAreaVec);
672
0
    }
673
0
    lg2::debug("Populated FRU areas, file name: {FILE}", "FILE", fruFilename);
674
675
0
    for (const auto& iter : fruAreaVec)
676
0
    {
677
0
        lg2::debug("fru id: {FRUID}", "FRUID", iter->getFruID());
678
0
        lg2::debug("area name: {AREA}", "AREA", iter->getName());
679
0
        lg2::debug("type: {TYPE}", "TYPE", iter->getType());
680
0
        lg2::debug("length: {LEN}", "LEN", iter->getLength());
681
0
    }
682
683
    // If the vector is populated with everything, then go ahead and update the
684
    // inventory.
685
0
    if (!(fruAreaVec.empty()))
686
0
    {
687
0
        lg2::debug("fruAreaVec size: {SIZE}", "SIZE", fruAreaVec.size());
688
0
        rc = updateInventory(fruAreaVec, bus);
689
0
        if (rc < 0)
690
0
        {
691
0
            lg2::error("Error updating inventory.");
692
0
        }
693
0
    }
694
695
    // we are done with all that we wanted to do. This will do the job of
696
    // calling any destructors too.
697
0
    fruAreaVec.clear();
698
699
0
    return rc;
700
0
}