xref: /openbmc/smbios-mdr/src/mdrv2.cpp (revision a3f5b385)
1 /*
2 // Copyright (c) 2018 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 
17 #include "mdrv2.hpp"
18 
19 #include "pcieslot.hpp"
20 
21 #include <sys/mman.h>
22 
23 #include <phosphor-logging/elog-errors.hpp>
24 #include <sdbusplus/exception.hpp>
25 #include <xyz/openbmc_project/Smbios/MDR_V2/error.hpp>
26 
27 #include <fstream>
28 
29 namespace phosphor
30 {
31 namespace smbios
32 {
33 
34 std::vector<uint8_t> MDRV2::getDirectoryInformation(uint8_t dirIndex)
35 {
36     std::vector<uint8_t> responseDir;
37 
38     std::ifstream smbiosFile(mdrType2File, std::ios_base::binary);
39     if (!smbiosFile.good())
40     {
41         phosphor::logging::log<phosphor::logging::level::ERR>(
42             "Read data from flash error - Open MDRV2 table file failure");
43         throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
44             InvalidParameter();
45     }
46     if (dirIndex > smbiosDir.dirEntries)
47     {
48         responseDir.push_back(0);
49         throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
50             InvalidParameter();
51     }
52     responseDir.push_back(mdr2Version);
53     responseDir.push_back(smbiosDir.dirVersion);
54     uint8_t returnedEntries = smbiosDir.dirEntries - dirIndex;
55     responseDir.push_back(returnedEntries);
56     if ((dirIndex + returnedEntries) >= smbiosDir.dirEntries)
57     {
58         responseDir.push_back(0);
59     }
60     else
61     {
62         responseDir.push_back(smbiosDir.dirEntries - dirIndex -
63                               returnedEntries);
64     }
65     for (uint8_t index = dirIndex; index < smbiosDir.dirEntries; index++)
66     {
67         for (uint8_t indexId = 0; indexId < sizeof(DataIdStruct); indexId++)
68         {
69             responseDir.push_back(
70                 smbiosDir.dir[index].common.id.dataInfo[indexId]);
71         }
72     }
73 
74     return responseDir;
75 }
76 
77 bool MDRV2::smbiosIsAvailForUpdate(uint8_t index)
78 {
79     bool ret = false;
80     if (index >= maxDirEntries)
81     {
82         return ret;
83     }
84 
85     switch (smbiosDir.dir[index].stage)
86     {
87         case MDR2SMBIOSStatusEnum::mdr2Updating:
88             ret = false;
89             break;
90 
91         case MDR2SMBIOSStatusEnum::mdr2Init:
92             // This *looks* like there should be a break statement here,
93             // as the effects of the previous statement are a noop given
94             // the following code that this falls through to.
95             // We've elected not to change it, though, since it's been
96             // this way since the old generation, and it would affect
97             // the way the code functions.
98             // If it ain't broke, don't fix it.
99 
100         case MDR2SMBIOSStatusEnum::mdr2Loaded:
101         case MDR2SMBIOSStatusEnum::mdr2Updated:
102             if (smbiosDir.dir[index].lock == MDR2DirLockEnum::mdr2DirLock)
103             {
104                 ret = false;
105             }
106             else
107             {
108                 ret = true;
109             }
110             break;
111 
112         default:
113             break;
114     }
115 
116     return ret;
117 }
118 
119 std::vector<uint8_t> MDRV2::getDataOffer()
120 {
121     std::vector<uint8_t> offer(sizeof(DataIdStruct));
122     if (smbiosIsAvailForUpdate(0))
123     {
124         std::copy(smbiosDir.dir[0].common.id.dataInfo,
125                   &smbiosDir.dir[0].common.id.dataInfo[16], offer.data());
126     }
127     else
128     {
129         phosphor::logging::log<phosphor::logging::level::ERR>(
130             "smbios is not ready for update");
131         throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
132             UpdateInProgress();
133     }
134     return offer;
135 }
136 
137 inline uint8_t MDRV2::smbiosValidFlag(uint8_t index)
138 {
139     FlagStatus ret = FlagStatus::flagIsInvalid;
140     MDR2SMBIOSStatusEnum stage = smbiosDir.dir[index].stage;
141     MDR2DirLockEnum lock = smbiosDir.dir[index].lock;
142 
143     switch (stage)
144     {
145         case MDR2SMBIOSStatusEnum::mdr2Loaded:
146         case MDR2SMBIOSStatusEnum::mdr2Updated:
147             if (lock == MDR2DirLockEnum::mdr2DirLock)
148             {
149                 ret = FlagStatus::flagIsLocked; // locked
150             }
151             else
152             {
153                 ret = FlagStatus::flagIsValid; // valid
154             }
155             break;
156 
157         case MDR2SMBIOSStatusEnum::mdr2Updating:
158         case MDR2SMBIOSStatusEnum::mdr2Init:
159             ret = FlagStatus::flagIsInvalid; // invalid
160             break;
161 
162         default:
163             break;
164     }
165 
166     return static_cast<uint8_t>(ret);
167 }
168 
169 // If source variable size is 4 bytes (uint32_t) and destination is Vector type
170 // is 1 byte (uint8_t), then by using this API can copy data byte by byte. For
171 // Example copying data from smbiosDir.dir[idIndex].common.size and
172 // smbiosDir.dir[idIndex].common.timestamp to vector variable responseInfo
173 template <typename T>
174 void appendReversed(std::vector<uint8_t>& vector, const T& value)
175 {
176     auto data = reinterpret_cast<const uint8_t*>(&value);
177     std::reverse_copy(data, data + sizeof(value), std::back_inserter(vector));
178 }
179 
180 std::vector<uint8_t> MDRV2::getDataInformation(uint8_t idIndex)
181 {
182     std::vector<uint8_t> responseInfo;
183     responseInfo.push_back(mdr2Version);
184 
185     if (idIndex >= maxDirEntries)
186     {
187         throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
188             InvalidParameter();
189     }
190 
191     for (uint8_t index = 0; index < sizeof(DataIdStruct); index++)
192     {
193         responseInfo.push_back(
194             smbiosDir.dir[idIndex].common.id.dataInfo[index]);
195     }
196 
197     responseInfo.push_back(smbiosValidFlag(idIndex));
198     appendReversed(responseInfo, smbiosDir.dir[idIndex].common.size);
199     responseInfo.push_back(smbiosDir.dir[idIndex].common.dataVersion);
200     appendReversed(responseInfo, smbiosDir.dir[idIndex].common.timestamp);
201 
202     return responseInfo;
203 }
204 
205 bool MDRV2::readDataFromFlash(MDRSMBIOSHeader* mdrHdr, uint8_t* data)
206 {
207     if (mdrHdr == nullptr)
208     {
209         phosphor::logging::log<phosphor::logging::level::ERR>(
210             "Read data from flash error - Invalid mdr header");
211         return false;
212     }
213     if (data == nullptr)
214     {
215         phosphor::logging::log<phosphor::logging::level::ERR>(
216             "Read data from flash error - Invalid data point");
217         return false;
218     }
219     std::ifstream smbiosFile(mdrType2File, std::ios_base::binary);
220     if (!smbiosFile.good())
221     {
222         phosphor::logging::log<phosphor::logging::level::ERR>(
223             "Read data from flash error - Open MDRV2 table file failure");
224         return false;
225     }
226     smbiosFile.clear();
227     smbiosFile.seekg(0, std::ios_base::end);
228     int fileLength = smbiosFile.tellg();
229     smbiosFile.seekg(0, std::ios_base::beg);
230     if (fileLength < sizeof(MDRSMBIOSHeader))
231     {
232         phosphor::logging::log<phosphor::logging::level::ERR>(
233             "MDR V2 file size is smaller than mdr header");
234         return false;
235     }
236     smbiosFile.read(reinterpret_cast<char*>(mdrHdr), sizeof(MDRSMBIOSHeader));
237     if (mdrHdr->dataSize > smbiosTableStorageSize)
238     {
239         phosphor::logging::log<phosphor::logging::level::ERR>(
240             "Data size out of limitation");
241         smbiosFile.close();
242         return false;
243     }
244     fileLength -= sizeof(MDRSMBIOSHeader);
245     if (fileLength < mdrHdr->dataSize)
246     {
247         smbiosFile.read(reinterpret_cast<char*>(data), fileLength);
248     }
249     else
250     {
251         smbiosFile.read(reinterpret_cast<char*>(data), mdrHdr->dataSize);
252     }
253     smbiosFile.close();
254     return true;
255 }
256 
257 bool MDRV2::sendDirectoryInformation(uint8_t dirVersion, uint8_t dirIndex,
258                                      uint8_t returnedEntries,
259                                      uint8_t remainingEntries,
260                                      std::vector<uint8_t> dirEntry)
261 {
262     bool teminate = false;
263     if ((dirIndex >= maxDirEntries) || (returnedEntries < 1))
264     {
265         phosphor::logging::log<phosphor::logging::level::ERR>(
266             "Send Dir info failed - input parameter invalid");
267         throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
268             InvalidParameter();
269     }
270     if ((static_cast<size_t>(returnedEntries) * sizeof(DataIdStruct)) !=
271         dirEntry.size())
272     {
273         phosphor::logging::log<phosphor::logging::level::ERR>(
274             "Directory size invalid");
275         throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
276             InvalidParameter();
277     }
278     if (dirVersion == smbiosDir.dirVersion)
279     {
280         teminate = true;
281     }
282     else
283     {
284         if (remainingEntries > 0)
285         {
286             teminate = false;
287         }
288         else
289         {
290             teminate = true;
291             smbiosDir.dirVersion = dirVersion;
292         }
293         uint8_t idIndex = dirIndex;
294         smbiosDir.dirEntries = returnedEntries;
295 
296         uint8_t* pData = dirEntry.data();
297         if (pData == nullptr)
298         {
299             return false;
300         }
301         for (uint8_t index = 0; index < returnedEntries; index++)
302         {
303             auto data = reinterpret_cast<const DataIdStruct*>(pData);
304             std::copy(data->dataInfo, data->dataInfo + sizeof(DataIdStruct),
305                       smbiosDir.dir[idIndex + index].common.id.dataInfo);
306             pData += sizeof(DataIdStruct);
307         }
308     }
309     return teminate;
310 }
311 
312 bool MDRV2::sendDataInformation(uint8_t idIndex, uint8_t flag, uint32_t dataLen,
313                                 uint32_t dataVer, uint32_t timeStamp)
314 {
315     if (idIndex >= maxDirEntries)
316     {
317         throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
318             InvalidParameter();
319     }
320     int entryChanged = 0;
321     if (smbiosDir.dir[idIndex].common.dataSetSize != dataLen)
322     {
323         entryChanged++;
324         smbiosDir.dir[idIndex].common.dataSetSize = dataLen;
325     }
326 
327     if (smbiosDir.dir[idIndex].common.dataVersion != dataVer)
328     {
329         entryChanged++;
330         smbiosDir.dir[idIndex].common.dataVersion = dataVer;
331     }
332 
333     if (smbiosDir.dir[idIndex].common.timestamp != timeStamp)
334     {
335         entryChanged++;
336         smbiosDir.dir[idIndex].common.timestamp = timeStamp;
337     }
338     if (entryChanged == 0)
339     {
340         return false;
341     }
342     return true;
343 }
344 
345 int MDRV2::findIdIndex(std::vector<uint8_t> dataInfo)
346 {
347     if (dataInfo.size() != sizeof(DataIdStruct))
348     {
349         phosphor::logging::log<phosphor::logging::level::ERR>(
350             "Length of dataInfo invalid");
351         throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
352             InvalidId();
353     }
354     std::array<uint8_t, 16> arrayDataInfo;
355 
356     std::copy(dataInfo.begin(), dataInfo.end(), arrayDataInfo.begin());
357 
358     for (int index = 0; index < smbiosDir.dirEntries; index++)
359     {
360         int info = 0;
361         for (; info < arrayDataInfo.size(); info++)
362         {
363             if (arrayDataInfo[info] !=
364                 smbiosDir.dir[index].common.id.dataInfo[info])
365             {
366                 break;
367             }
368         }
369         if (info == arrayDataInfo.size())
370         {
371             return index;
372         }
373     }
374     throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::InvalidId();
375 }
376 
377 uint8_t MDRV2::directoryEntries(uint8_t value)
378 {
379     std::ifstream smbiosFile(mdrType2File, std::ios_base::binary);
380     if (!smbiosFile.good())
381     {
382         phosphor::logging::log<phosphor::logging::level::ERR>(
383             "Read data from flash error - Open MDRV2 table file failure");
384         value = 0;
385     }
386     else
387     {
388         value = smbiosDir.dirEntries;
389     }
390     return sdbusplus::xyz::openbmc_project::Smbios::server::MDRV2::
391         directoryEntries(value);
392 }
393 
394 void MDRV2::systemInfoUpdate()
395 {
396     std::string motherboardPath;
397     auto method = bus.new_method_call(mapperBusName, mapperPath,
398                                       mapperInterface, "GetSubTreePaths");
399     method.append(systemInterfacePath);
400     method.append(0);
401     method.append(std::vector<std::string>({systemInterface}));
402 
403     try
404     {
405         std::vector<std::string> paths;
406         sdbusplus::message_t reply = bus.call(method);
407         reply.read(paths);
408         if (paths.size() < 1)
409         {
410             phosphor::logging::log<phosphor::logging::level::ERR>(
411                 "Failed to get system motherboard dbus path. Setting up a "
412                 "match rule");
413             // Add match rule if motherboard dbus path is not yet created
414             static std::unique_ptr<sdbusplus::bus::match_t>
415                 motherboardConfigMatch =
416                     std::make_unique<sdbusplus::bus::match_t>(
417                         bus,
418                         sdbusplus::bus::match::rules::interfacesAdded() +
419                             sdbusplus::bus::match::rules::argNpath(
420                                 0,
421                                 "/xyz/openbmc_project/inventory/system/board/"),
422                         [this, systemInterface](sdbusplus::message_t& msg) {
423                             sdbusplus::message::object_path objectName;
424                             boost::container::flat_map<
425                                 std::string,
426                                 boost::container::flat_map<
427                                     std::string,
428                                     std::variant<std::string, uint64_t>>>
429                                 msgData;
430                             msg.read(objectName, msgData);
431                             if (msgData.contains(systemInterface))
432                             {
433                                 this->systemInfoUpdate();
434                             }
435                         });
436         }
437         else
438         {
439             motherboardPath = std::move(paths[0]);
440         }
441     }
442     catch (const sdbusplus::exception_t& e)
443     {
444         phosphor::logging::log<phosphor::logging::level::ERR>(
445             "Failed to query system motherboard",
446             phosphor::logging::entry("ERROR=%s", e.what()));
447     }
448 
449     cpus.clear();
450     int num = getTotalCpuSlot();
451     if (num == -1)
452     {
453         phosphor::logging::log<phosphor::logging::level::ERR>(
454             "get cpu total slot failed");
455         return;
456     }
457 
458     for (int index = 0; index < num; index++)
459     {
460         std::string path = cpuPath + std::to_string(index);
461         cpus.emplace_back(std::make_unique<phosphor::smbios::Cpu>(
462             bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
463             motherboardPath));
464     }
465 
466 #ifdef DIMM_DBUS
467 
468     dimms.clear();
469 
470     num = getTotalDimmSlot();
471     if (num == -1)
472     {
473         phosphor::logging::log<phosphor::logging::level::ERR>(
474             "get dimm total slot failed");
475         return;
476     }
477 
478     for (int index = 0; index < num; index++)
479     {
480         std::string path = dimmPath + std::to_string(index);
481         dimms.emplace_back(std::make_unique<phosphor::smbios::Dimm>(
482             bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
483             motherboardPath));
484     }
485 
486 #endif
487 
488     pcies.clear();
489     num = getTotalPcieSlot();
490     if (num == -1)
491     {
492         phosphor::logging::log<phosphor::logging::level::ERR>(
493             "get pcie total slot failed");
494         return;
495     }
496 
497     for (int index = 0; index < num; index++)
498     {
499         std::string path = pciePath + std::to_string(index);
500         pcies.emplace_back(std::make_unique<phosphor::smbios::Pcie>(
501             bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
502             motherboardPath));
503     }
504 
505     system.reset();
506     system = std::make_unique<System>(
507         bus, systemPath, smbiosDir.dir[smbiosDirIndex].dataStorage);
508 }
509 
510 int MDRV2::getTotalCpuSlot()
511 {
512     uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
513     int num = 0;
514 
515     if (dataIn == nullptr)
516     {
517         phosphor::logging::log<phosphor::logging::level::ERR>(
518             "get cpu total slot failed - no storage data");
519         return -1;
520     }
521 
522     while (1)
523     {
524         dataIn = getSMBIOSTypePtr(dataIn, processorsType);
525         if (dataIn == nullptr)
526         {
527             break;
528         }
529         num++;
530         dataIn = smbiosNextPtr(dataIn);
531         if (dataIn == nullptr)
532         {
533             break;
534         }
535         if (num >= limitEntryLen)
536         {
537             break;
538         }
539     }
540 
541     return num;
542 }
543 
544 int MDRV2::getTotalDimmSlot()
545 {
546     uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
547     uint8_t num = 0;
548 
549     if (dataIn == nullptr)
550     {
551         phosphor::logging::log<phosphor::logging::level::ERR>(
552             "Fail to get dimm total slot - no storage data");
553         return -1;
554     }
555 
556     while (1)
557     {
558         dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType);
559         if (dataIn == nullptr)
560         {
561             break;
562         }
563         num++;
564         dataIn = smbiosNextPtr(dataIn);
565         if (dataIn == nullptr)
566         {
567             break;
568         }
569         if (num >= limitEntryLen)
570         {
571             break;
572         }
573     }
574 
575     return num;
576 }
577 
578 int MDRV2::getTotalPcieSlot()
579 {
580     uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
581     int num = 0;
582 
583     if (dataIn == nullptr)
584     {
585         phosphor::logging::log<phosphor::logging::level::ERR>(
586             "Fail to get total system slot - no storage data");
587         return -1;
588     }
589 
590     while (1)
591     {
592         dataIn = getSMBIOSTypePtr(dataIn, systemSlots);
593         if (dataIn == nullptr)
594         {
595             break;
596         }
597 
598         /* System slot type offset. Check if the slot is a PCIE slots. All
599          * PCIE slot type are hardcoded in a table.
600          */
601         if (pcieSmbiosType.find(*(dataIn + 5)) != pcieSmbiosType.end())
602         {
603             num++;
604         }
605         dataIn = smbiosNextPtr(dataIn);
606         if (dataIn == nullptr)
607         {
608             break;
609         }
610         if (num >= limitEntryLen)
611         {
612             break;
613         }
614     }
615 
616     return num;
617 }
618 
619 bool MDRV2::checkSMBIOSVersion(uint8_t* dataIn)
620 {
621     const std::string anchorString21 = "_SM_";
622     const std::string anchorString30 = "_SM3_";
623     std::string buffer(dataIn, dataIn + smbiosTableStorageSize);
624 
625     auto it = std::search(std::begin(buffer), std::end(buffer),
626                           std::begin(anchorString21), std::end(anchorString21));
627     bool smbios21Found = it != std::end(buffer);
628     if (!smbios21Found)
629     {
630         phosphor::logging::log<phosphor::logging::level::INFO>(
631             "SMBIOS 2.1 Anchor String not found. Looking for SMBIOS 3.0");
632         it = std::search(std::begin(buffer), std::end(buffer),
633                          std::begin(anchorString30), std::end(anchorString30));
634         if (it == std::end(buffer))
635         {
636             phosphor::logging::log<phosphor::logging::level::ERR>(
637                 "SMBIOS 2.1 and 3.0 Anchor Strings not found");
638             return false;
639         }
640     }
641 
642     auto pos = std::distance(std::begin(buffer), it);
643     auto length = smbiosTableStorageSize - pos;
644     uint8_t foundMajorVersion;
645     uint8_t foundMinorVersion;
646 
647     if (smbios21Found)
648     {
649         if (length < sizeof(EntryPointStructure21))
650         {
651             phosphor::logging::log<phosphor::logging::level::ERR>(
652                 "Invalid entry point structure for SMBIOS 2.1");
653             return false;
654         }
655 
656         auto epStructure =
657             reinterpret_cast<const EntryPointStructure21*>(&dataIn[pos]);
658         foundMajorVersion = epStructure->smbiosVersion.majorVersion;
659         foundMinorVersion = epStructure->smbiosVersion.minorVersion;
660     }
661     else
662     {
663         if (length < sizeof(EntryPointStructure30))
664         {
665             phosphor::logging::log<phosphor::logging::level::ERR>(
666                 "Invalid entry point structure for SMBIOS 3.0");
667             return false;
668         }
669 
670         auto epStructure =
671             reinterpret_cast<const EntryPointStructure30*>(&dataIn[pos]);
672         foundMajorVersion = epStructure->smbiosVersion.majorVersion;
673         foundMinorVersion = epStructure->smbiosVersion.minorVersion;
674     }
675     lg2::info("SMBIOS VERSION - {MAJOR}.{MINOR}", "MAJOR", foundMajorVersion,
676               "MINOR", foundMinorVersion);
677 
678     auto itr = std::find_if(
679         std::begin(supportedSMBIOSVersions), std::end(supportedSMBIOSVersions),
680         [&](SMBIOSVersion versionItr) {
681             return versionItr.majorVersion == foundMajorVersion &&
682                    versionItr.minorVersion == foundMinorVersion;
683         });
684     if (itr == std::end(supportedSMBIOSVersions))
685     {
686         return false;
687     }
688     return true;
689 }
690 
691 bool MDRV2::agentSynchronizeData()
692 {
693     struct MDRSMBIOSHeader mdr2SMBIOS;
694     bool status = readDataFromFlash(&mdr2SMBIOS,
695                                     smbiosDir.dir[smbiosDirIndex].dataStorage);
696     if (!status)
697     {
698         phosphor::logging::log<phosphor::logging::level::ERR>(
699             "agent data sync failed - read data from flash failed");
700         return false;
701     }
702 
703     if (!checkSMBIOSVersion(smbiosDir.dir[smbiosDirIndex].dataStorage))
704     {
705         phosphor::logging::log<phosphor::logging::level::ERR>(
706             "Unsupported SMBIOS table version");
707         return false;
708     }
709 
710     systemInfoUpdate();
711     smbiosDir.dir[smbiosDirIndex].common.dataVersion = mdr2SMBIOS.dirVer;
712     smbiosDir.dir[smbiosDirIndex].common.timestamp = mdr2SMBIOS.timestamp;
713     smbiosDir.dir[smbiosDirIndex].common.size = mdr2SMBIOS.dataSize;
714     smbiosDir.dir[smbiosDirIndex].stage = MDR2SMBIOSStatusEnum::mdr2Loaded;
715     smbiosDir.dir[smbiosDirIndex].lock = MDR2DirLockEnum::mdr2DirUnlock;
716 
717     return true;
718 }
719 
720 std::vector<uint32_t> MDRV2::synchronizeDirectoryCommonData(uint8_t idIndex,
721                                                             uint32_t size)
722 {
723     std::chrono::microseconds usec(
724         defaultTimeout); // default lock time out is 2s
725     std::vector<uint32_t> result;
726     smbiosDir.dir[idIndex].common.size = size;
727     result.push_back(smbiosDir.dir[idIndex].common.dataSetSize);
728     result.push_back(smbiosDir.dir[idIndex].common.dataVersion);
729     result.push_back(smbiosDir.dir[idIndex].common.timestamp);
730 
731     timer.expires_after(usec);
732     timer.async_wait([this](boost::system::error_code ec) {
733         if (ec || this == nullptr)
734         {
735             phosphor::logging::log<phosphor::logging::level::ERR>(
736                 "Timer Error!");
737             return;
738         }
739         agentSynchronizeData();
740     });
741     return result;
742 }
743 
744 std::vector<boost::container::flat_map<std::string, RecordVariant>>
745     MDRV2::getRecordType(size_t type)
746 {
747 
748     std::vector<boost::container::flat_map<std::string, RecordVariant>> ret;
749     if (type == memoryDeviceType)
750     {
751 
752         uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
753 
754         if (dataIn == nullptr)
755         {
756             throw std::runtime_error("Data not populated");
757         }
758 
759         do
760         {
761             dataIn =
762                 getSMBIOSTypePtr(dataIn, memoryDeviceType, sizeof(MemoryInfo));
763             if (dataIn == nullptr)
764             {
765                 break;
766             }
767             boost::container::flat_map<std::string, RecordVariant>& record =
768                 ret.emplace_back();
769 
770             auto memoryInfo = reinterpret_cast<MemoryInfo*>(dataIn);
771 
772             record["Type"] = memoryInfo->type;
773             record["Length"] = memoryInfo->length;
774             record["Handle"] = uint16_t(memoryInfo->handle);
775             record["Physical Memory Array Handle"] =
776                 uint16_t(memoryInfo->phyArrayHandle);
777             record["Memory Error Information Handle"] =
778                 uint16_t(memoryInfo->errInfoHandle);
779             record["Total Width"] = uint16_t(memoryInfo->totalWidth);
780             record["Data Width"] = uint16_t(memoryInfo->dataWidth);
781             record["Size"] = uint16_t(memoryInfo->size);
782             record["Form Factor"] = memoryInfo->formFactor;
783             record["Device Set"] = memoryInfo->deviceSet;
784             record["Device Locator"] = positionToString(
785                 memoryInfo->deviceLocator, memoryInfo->length, dataIn);
786             record["Bank Locator"] = positionToString(
787                 memoryInfo->bankLocator, memoryInfo->length, dataIn);
788             record["Memory Type"] = memoryInfo->memoryType;
789             record["Type Detail"] = uint16_t(memoryInfo->typeDetail);
790             record["Speed"] = uint16_t(memoryInfo->speed);
791             record["Manufacturer"] = positionToString(
792                 memoryInfo->manufacturer, memoryInfo->length, dataIn);
793             record["Serial Number"] = positionToString(
794                 memoryInfo->serialNum, memoryInfo->length, dataIn);
795             record["Asset Tag"] = positionToString(memoryInfo->assetTag,
796                                                    memoryInfo->length, dataIn);
797             record["Part Number"] = positionToString(
798                 memoryInfo->partNum, memoryInfo->length, dataIn);
799             record["Attributes"] = memoryInfo->attributes;
800             record["Extended Size"] = uint32_t(memoryInfo->extendedSize);
801             record["Configured Memory Speed"] =
802                 uint32_t(memoryInfo->confClockSpeed);
803             record["Minimum voltage"] = uint16_t(memoryInfo->minimumVoltage);
804             record["Maximum voltage"] = uint16_t(memoryInfo->maximumVoltage);
805             record["Configured voltage"] =
806                 uint16_t(memoryInfo->configuredVoltage);
807             record["Memory Technology"] = memoryInfo->memoryTechnology;
808             record["Memory Operating Mode Capabilty"] =
809                 uint16_t(memoryInfo->memoryOperatingModeCap);
810             record["Firmare Version"] = memoryInfo->firwareVersion;
811             record["Module Manufacturer ID"] =
812                 uint16_t(memoryInfo->modelManufId);
813             record["Module Product ID"] = uint16_t(memoryInfo->modelProdId);
814             record["Memory Subsystem Controller Manufacturer ID"] =
815                 uint16_t(memoryInfo->memSubConManufId);
816             record["Memory Subsystem Controller Product Id"] =
817                 uint16_t(memoryInfo->memSubConProdId);
818             record["Non-volatile Size"] = uint64_t(memoryInfo->nvSize);
819             record["Volatile Size"] = uint64_t(memoryInfo->volatileSize);
820             record["Cache Size"] = uint64_t(memoryInfo->cacheSize);
821             record["Logical Size"] = uint64_t(memoryInfo->logicalSize);
822         } while ((dataIn = smbiosNextPtr(dataIn)) != nullptr);
823 
824         return ret;
825     }
826 
827     throw std::invalid_argument("Invalid record type");
828     return ret;
829 }
830 
831 } // namespace smbios
832 } // namespace phosphor
833