blob: 173b34faf630824233060bec95c8fdb40a8bd039 [file] [log] [blame]
Cheng C Yangeecaf822019-12-19 00:34:23 +08001/*
Zhikui Ren18a5ab92020-09-01 21:35:20 -07002// Copyright (c) 2018 Intel Corporation
Cheng C Yangeecaf822019-12-19 00:34:23 +08003//
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
Jie Yang08e4a6d2021-08-23 13:07:41 -070019#include "pcieslot.hpp"
20
Cheng C Yangeecaf822019-12-19 00:34:23 +080021#include <sys/mman.h>
22
Cheng C Yangeecaf822019-12-19 00:34:23 +080023#include <phosphor-logging/elog-errors.hpp>
24#include <sdbusplus/exception.hpp>
25#include <xyz/openbmc_project/Smbios/MDR_V2/error.hpp>
26
Zhikui Ren18a5ab92020-09-01 21:35:20 -070027#include <fstream>
28
Cheng C Yangeecaf822019-12-19 00:34:23 +080029namespace phosphor
30{
31namespace smbios
32{
33
34std::vector<uint8_t> MDR_V2::getDirectoryInformation(uint8_t dirIndex)
35{
Cheng C Yang608e52d2019-12-19 00:39:50 +080036 std::vector<uint8_t> responseDir;
Prithvi A Paibc924d02021-12-29 04:54:43 +000037
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 }
Cheng C Yang608e52d2019-12-19 00:39:50 +080046 if (dirIndex > smbiosDir.dirEntries)
47 {
48 responseDir.push_back(0);
Kuiying Wanga4c08702020-11-18 19:27:20 +080049 throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
50 InvalidParameter();
Cheng C Yang608e52d2019-12-19 00:39:50 +080051 }
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;
Cheng C Yangeecaf822019-12-19 00:34:23 +080075}
76
77bool MDR_V2::smbiosIsAvailForUpdate(uint8_t index)
78{
79 bool ret = false;
Chen Yugangad0e7652020-12-01 14:49:54 +080080 if (index >= maxDirEntries)
Cheng C Yangeecaf822019-12-19 00:34:23 +080081 {
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
119std::vector<uint8_t> MDR_V2::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");
Kuiying Wanga4c08702020-11-18 19:27:20 +0800131 throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
132 UpdateInProgress();
Cheng C Yangeecaf822019-12-19 00:34:23 +0800133 }
134 return offer;
135}
136
137inline uint8_t MDR_V2::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
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700169// 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
173template <typename T>
174void 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
Cheng C Yangeecaf822019-12-19 00:34:23 +0800180std::vector<uint8_t> MDR_V2::getDataInformation(uint8_t idIndex)
181{
182 std::vector<uint8_t> responseInfo;
183 responseInfo.push_back(mdr2Version);
184
185 if (idIndex >= maxDirEntries)
186 {
Kuiying Wanga4c08702020-11-18 19:27:20 +0800187 throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
188 InvalidParameter();
Cheng C Yangeecaf822019-12-19 00:34:23 +0800189 }
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 }
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700196
Cheng C Yangeecaf822019-12-19 00:34:23 +0800197 responseInfo.push_back(smbiosValidFlag(idIndex));
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700198 appendReversed(responseInfo, smbiosDir.dir[idIndex].common.size);
Cheng C Yangeecaf822019-12-19 00:34:23 +0800199 responseInfo.push_back(smbiosDir.dir[idIndex].common.dataVersion);
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700200 appendReversed(responseInfo, smbiosDir.dir[idIndex].common.timestamp);
Cheng C Yangeecaf822019-12-19 00:34:23 +0800201
202 return responseInfo;
203}
204
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700205bool MDR_V2::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
Cheng C Yangeecaf822019-12-19 00:34:23 +0800257bool MDR_V2::sendDirectoryInformation(uint8_t dirVersion, uint8_t dirIndex,
258 uint8_t returnedEntries,
259 uint8_t remainingEntries,
260 std::vector<uint8_t> dirEntry)
261{
Cheng C Yang608e52d2019-12-19 00:39:50 +0800262 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");
Kuiying Wanga4c08702020-11-18 19:27:20 +0800267 throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
268 InvalidParameter();
Cheng C Yang608e52d2019-12-19 00:39:50 +0800269 }
Mansi Joshiecbd71b2021-08-10 22:14:08 +0530270 if ((static_cast<size_t>(returnedEntries) * sizeof(DataIdStruct)) !=
271 dirEntry.size())
Cheng C Yang608e52d2019-12-19 00:39:50 +0800272 {
273 phosphor::logging::log<phosphor::logging::level::ERR>(
274 "Directory size invalid");
Kuiying Wanga4c08702020-11-18 19:27:20 +0800275 throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
276 InvalidParameter();
Cheng C Yang608e52d2019-12-19 00:39:50 +0800277 }
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;
Mansi Joshiecbd71b2021-08-10 22:14:08 +0530294 smbiosDir.dirEntries = returnedEntries;
Cheng C Yang608e52d2019-12-19 00:39:50 +0800295
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 {
Mansi Joshiecbd71b2021-08-10 22:14:08 +0530303 auto data = reinterpret_cast<const DataIdStruct*>(pData);
304 std::copy(data->dataInfo, data->dataInfo + sizeof(DataIdStruct),
Cheng C Yang608e52d2019-12-19 00:39:50 +0800305 smbiosDir.dir[idIndex + index].common.id.dataInfo);
Mansi Joshiecbd71b2021-08-10 22:14:08 +0530306 pData += sizeof(DataIdStruct);
Cheng C Yang608e52d2019-12-19 00:39:50 +0800307 }
308 }
309 return teminate;
Cheng C Yangeecaf822019-12-19 00:34:23 +0800310}
311
312bool MDR_V2::sendDataInformation(uint8_t idIndex, uint8_t flag,
313 uint32_t dataLen, uint32_t dataVer,
314 uint32_t timeStamp)
315{
316 if (idIndex >= maxDirEntries)
317 {
Kuiying Wanga4c08702020-11-18 19:27:20 +0800318 throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
319 InvalidParameter();
Cheng C Yangeecaf822019-12-19 00:34:23 +0800320 }
321 int entryChanged = 0;
322 if (smbiosDir.dir[idIndex].common.dataSetSize != dataLen)
323 {
324 entryChanged++;
325 smbiosDir.dir[idIndex].common.dataSetSize = dataLen;
326 }
327
328 if (smbiosDir.dir[idIndex].common.dataVersion != dataVer)
329 {
330 entryChanged++;
331 smbiosDir.dir[idIndex].common.dataVersion = dataVer;
332 }
333
334 if (smbiosDir.dir[idIndex].common.timestamp != timeStamp)
335 {
336 entryChanged++;
337 smbiosDir.dir[idIndex].common.timestamp = timeStamp;
338 }
339 if (entryChanged == 0)
340 {
341 return false;
342 }
343 return true;
344}
345
346int MDR_V2::findIdIndex(std::vector<uint8_t> dataInfo)
347{
348 if (dataInfo.size() != sizeof(DataIdStruct))
349 {
350 phosphor::logging::log<phosphor::logging::level::ERR>(
351 "Length of dataInfo invalid");
Kuiying Wanga4c08702020-11-18 19:27:20 +0800352 throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
353 InvalidId();
Cheng C Yangeecaf822019-12-19 00:34:23 +0800354 }
355 std::array<uint8_t, 16> arrayDataInfo;
356
357 std::copy(dataInfo.begin(), dataInfo.end(), arrayDataInfo.begin());
358
359 for (int index = 0; index < smbiosDir.dirEntries; index++)
360 {
361 int info = 0;
362 for (; info < arrayDataInfo.size(); info++)
363 {
364 if (arrayDataInfo[info] !=
365 smbiosDir.dir[index].common.id.dataInfo[info])
366 {
367 break;
368 }
369 }
370 if (info == arrayDataInfo.size())
371 {
372 return index;
373 }
374 }
Kuiying Wanga4c08702020-11-18 19:27:20 +0800375 throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::InvalidId();
Cheng C Yangeecaf822019-12-19 00:34:23 +0800376}
377
378uint8_t MDR_V2::directoryEntries(uint8_t value)
379{
Prithvi A Paibc924d02021-12-29 04:54:43 +0000380 std::ifstream smbiosFile(mdrType2File, std::ios_base::binary);
381 if (!smbiosFile.good())
382 {
383 phosphor::logging::log<phosphor::logging::level::ERR>(
384 "Read data from flash error - Open MDRV2 table file failure");
385 value = 0;
386 }
387 else
388 {
389 value = smbiosDir.dirEntries;
390 }
Cheng C Yangeecaf822019-12-19 00:34:23 +0800391 return sdbusplus::xyz::openbmc_project::Smbios::server::MDR_V2::
392 directoryEntries(value);
393}
394
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800395void MDR_V2::systemInfoUpdate()
396{
Jie Yange7cf3192021-08-20 11:21:43 -0700397 std::string motherboardPath;
Gobinath Krishnamoorthybb9c6222022-11-03 20:08:01 +0000398 auto method = bus.new_method_call(mapperBusName, mapperPath,
399 mapperInterface, "GetSubTreePaths");
400 method.append(systemInterfacePath);
401 method.append(0);
402 method.append(std::vector<std::string>({systemInterface}));
Nikhil Namjoshidd1811c2023-03-14 22:51:48 +0000403
Jie Yange7cf3192021-08-20 11:21:43 -0700404 try
405 {
Gobinath Krishnamoorthybb9c6222022-11-03 20:08:01 +0000406 std::vector<std::string> paths;
Patrick Williams77b9c472022-07-22 19:26:57 -0500407 sdbusplus::message_t reply = bus.call(method);
Gobinath Krishnamoorthybb9c6222022-11-03 20:08:01 +0000408 reply.read(paths);
409 if (paths.size() < 1)
410 {
411 phosphor::logging::log<phosphor::logging::level::ERR>(
Nikhil Namjoshidd1811c2023-03-14 22:51:48 +0000412 "Failed to get system motherboard dbus path. Setting up a "
413 "match rule");
414 // Add match rule if motherboard dbus path is not yet created
415 static std::unique_ptr<sdbusplus::bus::match_t>
416 motherboardConfigMatch =
417 std::make_unique<sdbusplus::bus::match_t>(
418 bus,
419 sdbusplus::bus::match::rules::interfacesAdded() +
420 sdbusplus::bus::match::rules::argNpath(
421 0,
422 "/xyz/openbmc_project/inventory/system/board/"),
423 [this,
424 systemInterface](sdbusplus::message::message& msg) {
425 sdbusplus::message::object_path objectName;
426 boost::container::flat_map<
427 std::string,
428 boost::container::flat_map<
429 std::string,
430 std::variant<std::string, uint64_t>>>
431 msgData;
432 msg.read(objectName, msgData);
433 if (msgData.contains(systemInterface))
434 {
435 this->systemInfoUpdate();
436 }
437 });
Gobinath Krishnamoorthybb9c6222022-11-03 20:08:01 +0000438 }
439 else
440 {
441 motherboardPath = std::move(paths[0]);
442 }
Jie Yange7cf3192021-08-20 11:21:43 -0700443 }
444 catch (const sdbusplus::exception_t& e)
445 {
446 phosphor::logging::log<phosphor::logging::level::ERR>(
447 "Failed to query system motherboard",
448 phosphor::logging::entry("ERROR=%s", e.what()));
449 }
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800450
Jie Yange7cf3192021-08-20 11:21:43 -0700451 cpus.clear();
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800452 int num = getTotalCpuSlot();
453 if (num == -1)
454 {
455 phosphor::logging::log<phosphor::logging::level::ERR>(
456 "get cpu total slot failed");
457 return;
458 }
459
460 for (int index = 0; index < num; index++)
461 {
462 std::string path = cpuPath + std::to_string(index);
463 cpus.emplace_back(std::make_unique<phosphor::smbios::Cpu>(
Jie Yange7cf3192021-08-20 11:21:43 -0700464 bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
465 motherboardPath));
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800466 }
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800467
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700468#ifdef DIMM_DBUS
469
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800470 dimms.clear();
471
472 num = getTotalDimmSlot();
473 if (num == -1)
474 {
475 phosphor::logging::log<phosphor::logging::level::ERR>(
476 "get dimm total slot failed");
477 return;
478 }
479
480 for (int index = 0; index < num; index++)
481 {
482 std::string path = dimmPath + std::to_string(index);
483 dimms.emplace_back(std::make_unique<phosphor::smbios::Dimm>(
Jie Yange7cf3192021-08-20 11:21:43 -0700484 bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
485 motherboardPath));
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800486 }
Cheng C Yangb4651b92019-12-19 00:59:01 +0800487
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700488#endif
489
Jie Yang08e4a6d2021-08-23 13:07:41 -0700490 pcies.clear();
491 num = getTotalPcieSlot();
492 if (num == -1)
493 {
494 phosphor::logging::log<phosphor::logging::level::ERR>(
495 "get pcie total slot failed");
496 return;
497 }
498
499 for (int index = 0; index < num; index++)
500 {
501 std::string path = pciePath + std::to_string(index);
502 pcies.emplace_back(std::make_unique<phosphor::smbios::Pcie>(
Jie Yange7cf3192021-08-20 11:21:43 -0700503 bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
504 motherboardPath));
Jie Yang08e4a6d2021-08-23 13:07:41 -0700505 }
506
Cheng C Yangb4651b92019-12-19 00:59:01 +0800507 system.reset();
508 system = std::make_unique<System>(
509 bus, systemPath, smbiosDir.dir[smbiosDirIndex].dataStorage);
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800510}
511
512int MDR_V2::getTotalCpuSlot()
513{
514 uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
515 int num = 0;
516
517 if (dataIn == nullptr)
518 {
519 phosphor::logging::log<phosphor::logging::level::ERR>(
520 "get cpu total slot failed - no storage data");
521 return -1;
522 }
523
524 while (1)
525 {
526 dataIn = getSMBIOSTypePtr(dataIn, processorsType);
527 if (dataIn == nullptr)
528 {
529 break;
530 }
531 num++;
532 dataIn = smbiosNextPtr(dataIn);
533 if (dataIn == nullptr)
534 {
535 break;
536 }
537 if (num >= limitEntryLen)
538 {
539 break;
540 }
541 }
542
543 return num;
544}
545
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800546int MDR_V2::getTotalDimmSlot()
547{
548 uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
549 uint8_t num = 0;
550
551 if (dataIn == nullptr)
552 {
553 phosphor::logging::log<phosphor::logging::level::ERR>(
554 "Fail to get dimm total slot - no storage data");
555 return -1;
556 }
557
558 while (1)
559 {
560 dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType);
561 if (dataIn == nullptr)
562 {
563 break;
564 }
565 num++;
566 dataIn = smbiosNextPtr(dataIn);
567 if (dataIn == nullptr)
568 {
569 break;
570 }
571 if (num >= limitEntryLen)
572 {
573 break;
574 }
575 }
576
577 return num;
578}
579
Jie Yang08e4a6d2021-08-23 13:07:41 -0700580int MDR_V2::getTotalPcieSlot()
581{
582 uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
583 int num = 0;
584
585 if (dataIn == nullptr)
586 {
587 phosphor::logging::log<phosphor::logging::level::ERR>(
588 "Fail to get total system slot - no storage data");
589 return -1;
590 }
591
592 while (1)
593 {
594 dataIn = getSMBIOSTypePtr(dataIn, systemSlots);
595 if (dataIn == nullptr)
596 {
597 break;
598 }
599
600 /* System slot type offset. Check if the slot is a PCIE slots. All
601 * PCIE slot type are hardcoded in a table.
602 */
603 if (pcieSmbiosType.find(*(dataIn + 5)) != pcieSmbiosType.end())
604 {
605 num++;
606 }
607 dataIn = smbiosNextPtr(dataIn);
608 if (dataIn == nullptr)
609 {
610 break;
611 }
612 if (num >= limitEntryLen)
613 {
614 break;
615 }
616 }
617
618 return num;
619}
620
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530621bool MDR_V2::checkSMBIOSVersion(uint8_t* dataIn)
622{
Brandon Kime4ea3772022-02-23 20:21:10 -0800623 const std::string anchorString21 = "_SM_";
624 const std::string anchorString30 = "_SM3_";
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530625 std::string buffer(dataIn, dataIn + smbiosTableStorageSize);
626
627 auto it = std::search(std::begin(buffer), std::end(buffer),
Brandon Kime4ea3772022-02-23 20:21:10 -0800628 std::begin(anchorString21), std::end(anchorString21));
629 bool smbios21Found = it != std::end(buffer);
630 if (!smbios21Found)
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530631 {
Brandon Kime4ea3772022-02-23 20:21:10 -0800632 phosphor::logging::log<phosphor::logging::level::INFO>(
633 "SMBIOS 2.1 Anchor String not found. Looking for SMBIOS 3.0");
634 it = std::search(std::begin(buffer), std::end(buffer),
635 std::begin(anchorString30), std::end(anchorString30));
636 if (it == std::end(buffer))
637 {
638 phosphor::logging::log<phosphor::logging::level::ERR>(
639 "SMBIOS 2.1 and 3.0 Anchor Strings not found");
640 return false;
641 }
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530642 }
643
644 auto pos = std::distance(std::begin(buffer), it);
645 auto length = smbiosTableStorageSize - pos;
Brandon Kime4ea3772022-02-23 20:21:10 -0800646 uint8_t foundMajorVersion;
647 uint8_t foundMinorVersion;
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530648
Brandon Kime4ea3772022-02-23 20:21:10 -0800649 if (smbios21Found)
650 {
651 if (length < sizeof(EntryPointStructure21))
652 {
653 phosphor::logging::log<phosphor::logging::level::ERR>(
654 "Invalid entry point structure for SMBIOS 2.1");
655 return false;
656 }
657
658 auto epStructure =
659 reinterpret_cast<const EntryPointStructure21*>(&dataIn[pos]);
660 foundMajorVersion = epStructure->smbiosVersion.majorVersion;
661 foundMinorVersion = epStructure->smbiosVersion.minorVersion;
662 }
663 else
664 {
665 if (length < sizeof(EntryPointStructure30))
666 {
667 phosphor::logging::log<phosphor::logging::level::ERR>(
668 "Invalid entry point structure for SMBIOS 3.0");
669 return false;
670 }
671
672 auto epStructure =
673 reinterpret_cast<const EntryPointStructure30*>(&dataIn[pos]);
674 foundMajorVersion = epStructure->smbiosVersion.majorVersion;
675 foundMinorVersion = epStructure->smbiosVersion.minorVersion;
676 }
677 lg2::info("SMBIOS VERSION - {MAJOR}.{MINOR}", "MAJOR", foundMajorVersion,
678 "MINOR", foundMinorVersion);
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530679
680 auto itr = std::find_if(
681 std::begin(supportedSMBIOSVersions), std::end(supportedSMBIOSVersions),
682 [&](SMBIOSVersion versionItr) {
Brandon Kime4ea3772022-02-23 20:21:10 -0800683 return versionItr.majorVersion == foundMajorVersion &&
684 versionItr.minorVersion == foundMinorVersion;
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530685 });
686 if (itr == std::end(supportedSMBIOSVersions))
687 {
688 return false;
689 }
690 return true;
691}
692
Cheng C Yangeecaf822019-12-19 00:34:23 +0800693bool MDR_V2::agentSynchronizeData()
694{
Cheng C Yangec634252019-12-19 00:42:36 +0800695 struct MDRSMBIOSHeader mdr2SMBIOS;
696 bool status = readDataFromFlash(&mdr2SMBIOS,
697 smbiosDir.dir[smbiosDirIndex].dataStorage);
698 if (!status)
699 {
700 phosphor::logging::log<phosphor::logging::level::ERR>(
701 "agent data sync failed - read data from flash failed");
702 return false;
703 }
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530704
705 if (!checkSMBIOSVersion(smbiosDir.dir[smbiosDirIndex].dataStorage))
Cheng C Yangec634252019-12-19 00:42:36 +0800706 {
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530707 phosphor::logging::log<phosphor::logging::level::ERR>(
708 "Unsupported SMBIOS table version");
709 return false;
Cheng C Yangec634252019-12-19 00:42:36 +0800710 }
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530711
712 systemInfoUpdate();
713 smbiosDir.dir[smbiosDirIndex].common.dataVersion = mdr2SMBIOS.dirVer;
714 smbiosDir.dir[smbiosDirIndex].common.timestamp = mdr2SMBIOS.timestamp;
715 smbiosDir.dir[smbiosDirIndex].common.size = mdr2SMBIOS.dataSize;
716 smbiosDir.dir[smbiosDirIndex].stage = MDR2SMBIOSStatusEnum::mdr2Loaded;
717 smbiosDir.dir[smbiosDirIndex].lock = MDR2DirLockEnum::mdr2DirUnlock;
718
Cheng C Yangec634252019-12-19 00:42:36 +0800719 return true;
Cheng C Yangeecaf822019-12-19 00:34:23 +0800720}
721
722std::vector<uint32_t> MDR_V2::synchronizeDirectoryCommonData(uint8_t idIndex,
723 uint32_t size)
724{
Cheng C Yangec634252019-12-19 00:42:36 +0800725 std::chrono::microseconds usec(
726 defaultTimeout); // default lock time out is 2s
727 std::vector<uint32_t> result;
728 smbiosDir.dir[idIndex].common.size = size;
729 result.push_back(smbiosDir.dir[idIndex].common.dataSetSize);
730 result.push_back(smbiosDir.dir[idIndex].common.dataVersion);
731 result.push_back(smbiosDir.dir[idIndex].common.timestamp);
732
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700733 timer.expires_after(usec);
734 timer.async_wait([this](boost::system::error_code ec) {
735 if (ec || this == nullptr)
736 {
737 phosphor::logging::log<phosphor::logging::level::ERR>(
738 "Timer Error!");
739 return;
740 }
741 agentSynchronizeData();
742 });
Cheng C Yangec634252019-12-19 00:42:36 +0800743 return result;
Cheng C Yangeecaf822019-12-19 00:34:23 +0800744}
745
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700746std::vector<boost::container::flat_map<std::string, RecordVariant>>
747 MDR_V2::getRecordType(size_t type)
748{
749
750 std::vector<boost::container::flat_map<std::string, RecordVariant>> ret;
751 if (type == memoryDeviceType)
752 {
753
754 uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
755
756 if (dataIn == nullptr)
757 {
Kuiying Wanga4c08702020-11-18 19:27:20 +0800758 throw std::runtime_error("Data not populated");
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700759 }
760
761 do
762 {
763 dataIn =
764 getSMBIOSTypePtr(dataIn, memoryDeviceType, sizeof(MemoryInfo));
765 if (dataIn == nullptr)
766 {
767 break;
768 }
769 boost::container::flat_map<std::string, RecordVariant>& record =
770 ret.emplace_back();
771
772 auto memoryInfo = reinterpret_cast<MemoryInfo*>(dataIn);
773
774 record["Type"] = memoryInfo->type;
775 record["Length"] = memoryInfo->length;
776 record["Handle"] = uint16_t(memoryInfo->handle);
777 record["Physical Memory Array Handle"] =
778 uint16_t(memoryInfo->phyArrayHandle);
779 record["Memory Error Information Handle"] =
780 uint16_t(memoryInfo->errInfoHandle);
781 record["Total Width"] = uint16_t(memoryInfo->totalWidth);
782 record["Data Width"] = uint16_t(memoryInfo->dataWidth);
783 record["Size"] = uint16_t(memoryInfo->size);
784 record["Form Factor"] = memoryInfo->formFactor;
785 record["Device Set"] = memoryInfo->deviceSet;
786 record["Device Locator"] = positionToString(
787 memoryInfo->deviceLocator, memoryInfo->length, dataIn);
788 record["Bank Locator"] = positionToString(
789 memoryInfo->bankLocator, memoryInfo->length, dataIn);
790 record["Memory Type"] = memoryInfo->memoryType;
791 record["Type Detail"] = uint16_t(memoryInfo->typeDetail);
792 record["Speed"] = uint16_t(memoryInfo->speed);
793 record["Manufacturer"] = positionToString(
794 memoryInfo->manufacturer, memoryInfo->length, dataIn);
795 record["Serial Number"] = positionToString(
796 memoryInfo->serialNum, memoryInfo->length, dataIn);
797 record["Asset Tag"] = positionToString(memoryInfo->assetTag,
798 memoryInfo->length, dataIn);
799 record["Part Number"] = positionToString(
800 memoryInfo->partNum, memoryInfo->length, dataIn);
801 record["Attributes"] = memoryInfo->attributes;
802 record["Extended Size"] = uint32_t(memoryInfo->extendedSize);
803 record["Configured Memory Speed"] =
804 uint32_t(memoryInfo->confClockSpeed);
805 record["Minimum voltage"] = uint16_t(memoryInfo->minimumVoltage);
806 record["Maximum voltage"] = uint16_t(memoryInfo->maximumVoltage);
807 record["Configured voltage"] =
808 uint16_t(memoryInfo->configuredVoltage);
809 record["Memory Technology"] = memoryInfo->memoryTechnology;
810 record["Memory Operating Mode Capabilty"] =
811 uint16_t(memoryInfo->memoryOperatingModeCap);
812 record["Firmare Version"] = memoryInfo->firwareVersion;
813 record["Module Manufacturer ID"] =
814 uint16_t(memoryInfo->modelManufId);
815 record["Module Product ID"] = uint16_t(memoryInfo->modelProdId);
816 record["Memory Subsystem Controller Manufacturer ID"] =
817 uint16_t(memoryInfo->memSubConManufId);
818 record["Memory Subsystem Controller Product Id"] =
819 uint16_t(memoryInfo->memSubConProdId);
820 record["Non-volatile Size"] = uint64_t(memoryInfo->nvSize);
821 record["Volatile Size"] = uint64_t(memoryInfo->volatileSize);
822 record["Cache Size"] = uint64_t(memoryInfo->cacheSize);
823 record["Logical Size"] = uint64_t(memoryInfo->logicalSize);
824 } while ((dataIn = smbiosNextPtr(dataIn)) != nullptr);
825
826 return ret;
827 }
828
Kuiying Wanga4c08702020-11-18 19:27:20 +0800829 throw std::invalid_argument("Invalid record type");
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700830 return ret;
831}
832
Cheng C Yangeecaf822019-12-19 00:34:23 +0800833} // namespace smbios
834} // namespace phosphor