blob: 1bf2e317aa789753c342b4a1dc3367343be1fbce [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
Jason M. Billsa3f5b382023-04-26 08:17:28 -070034std::vector<uint8_t> MDRV2::getDirectoryInformation(uint8_t dirIndex)
Cheng C Yangeecaf822019-12-19 00:34:23 +080035{
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
Jason M. Billsa3f5b382023-04-26 08:17:28 -070077bool MDRV2::smbiosIsAvailForUpdate(uint8_t index)
Cheng C Yangeecaf822019-12-19 00:34:23 +080078{
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
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700119std::vector<uint8_t> MDRV2::getDataOffer()
Cheng C Yangeecaf822019-12-19 00:34:23 +0800120{
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
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700137inline uint8_t MDRV2::smbiosValidFlag(uint8_t index)
Cheng C Yangeecaf822019-12-19 00:34:23 +0800138{
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
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700180std::vector<uint8_t> MDRV2::getDataInformation(uint8_t idIndex)
Cheng C Yangeecaf822019-12-19 00:34:23 +0800181{
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
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700205bool MDRV2::readDataFromFlash(MDRSMBIOSHeader* mdrHdr, uint8_t* data)
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700206{
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
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700257bool MDRV2::sendDirectoryInformation(uint8_t dirVersion, uint8_t dirIndex,
258 uint8_t returnedEntries,
259 uint8_t remainingEntries,
260 std::vector<uint8_t> dirEntry)
Cheng C Yangeecaf822019-12-19 00:34:23 +0800261{
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
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700312bool MDRV2::sendDataInformation(uint8_t idIndex, uint8_t flag, uint32_t dataLen,
313 uint32_t dataVer, uint32_t timeStamp)
Cheng C Yangeecaf822019-12-19 00:34:23 +0800314{
315 if (idIndex >= maxDirEntries)
316 {
Kuiying Wanga4c08702020-11-18 19:27:20 +0800317 throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
318 InvalidParameter();
Cheng C Yangeecaf822019-12-19 00:34:23 +0800319 }
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
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700345int MDRV2::findIdIndex(std::vector<uint8_t> dataInfo)
Cheng C Yangeecaf822019-12-19 00:34:23 +0800346{
347 if (dataInfo.size() != sizeof(DataIdStruct))
348 {
349 phosphor::logging::log<phosphor::logging::level::ERR>(
350 "Length of dataInfo invalid");
Kuiying Wanga4c08702020-11-18 19:27:20 +0800351 throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::
352 InvalidId();
Cheng C Yangeecaf822019-12-19 00:34:23 +0800353 }
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 }
Kuiying Wanga4c08702020-11-18 19:27:20 +0800374 throw sdbusplus::xyz::openbmc_project::Smbios::MDR_V2::Error::InvalidId();
Cheng C Yangeecaf822019-12-19 00:34:23 +0800375}
376
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700377uint8_t MDRV2::directoryEntries(uint8_t value)
Cheng C Yangeecaf822019-12-19 00:34:23 +0800378{
Prithvi A Paibc924d02021-12-29 04:54:43 +0000379 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 }
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700390 return sdbusplus::server::xyz::openbmc_project::smbios::MDRV2::
Cheng C Yangeecaf822019-12-19 00:34:23 +0800391 directoryEntries(value);
392}
393
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700394void MDRV2::systemInfoUpdate()
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800395{
Jie Yange7cf3192021-08-20 11:21:43 -0700396 std::string motherboardPath;
Gobinath Krishnamoorthybb9c6222022-11-03 20:08:01 +0000397 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}));
Nikhil Namjoshidd1811c2023-03-14 22:51:48 +0000402
Jie Yange7cf3192021-08-20 11:21:43 -0700403 try
404 {
Gobinath Krishnamoorthybb9c6222022-11-03 20:08:01 +0000405 std::vector<std::string> paths;
Patrick Williams77b9c472022-07-22 19:26:57 -0500406 sdbusplus::message_t reply = bus.call(method);
Gobinath Krishnamoorthybb9c6222022-11-03 20:08:01 +0000407 reply.read(paths);
408 if (paths.size() < 1)
409 {
410 phosphor::logging::log<phosphor::logging::level::ERR>(
Nikhil Namjoshidd1811c2023-03-14 22:51:48 +0000411 "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/"),
Patrick Williamsb8c4f332023-03-21 02:40:20 -0500422 [this, systemInterface](sdbusplus::message_t& msg) {
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500423 sdbusplus::message::object_path objectName;
424 boost::container::flat_map<
425 std::string,
426 boost::container::flat_map<
427 std::string, std::variant<std::string, uint64_t>>>
428 msgData;
429 msg.read(objectName, msgData);
430 if (msgData.contains(systemInterface))
431 {
432 this->systemInfoUpdate();
433 }
Nikhil Namjoshidd1811c2023-03-14 22:51:48 +0000434 });
Gobinath Krishnamoorthybb9c6222022-11-03 20:08:01 +0000435 }
436 else
437 {
438 motherboardPath = std::move(paths[0]);
439 }
Jie Yange7cf3192021-08-20 11:21:43 -0700440 }
441 catch (const sdbusplus::exception_t& e)
442 {
443 phosphor::logging::log<phosphor::logging::level::ERR>(
444 "Failed to query system motherboard",
445 phosphor::logging::entry("ERROR=%s", e.what()));
446 }
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800447
448 int num = getTotalCpuSlot();
449 if (num == -1)
450 {
451 phosphor::logging::log<phosphor::logging::level::ERR>(
452 "get cpu total slot failed");
453 return;
454 }
455
Brandon Kim5a122a62023-05-04 04:25:03 +0000456 // In case the new size is smaller than old, trim the vector
457 if (num < cpus.size())
458 {
459 cpus.erase(cpus.begin() + num, cpus.end());
460 }
461
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800462 for (int index = 0; index < num; index++)
463 {
464 std::string path = cpuPath + std::to_string(index);
Brandon Kim5a122a62023-05-04 04:25:03 +0000465 if (index + 1 > cpus.size())
466 {
467 cpus.emplace_back(std::make_unique<phosphor::smbios::Cpu>(
468 bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
469 motherboardPath));
470 }
471 else
472 {
473 cpus[index]->infoUpdate(smbiosDir.dir[smbiosDirIndex].dataStorage,
474 motherboardPath);
475 }
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800476 }
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800477
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700478#ifdef DIMM_DBUS
479
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800480 num = getTotalDimmSlot();
481 if (num == -1)
482 {
483 phosphor::logging::log<phosphor::logging::level::ERR>(
484 "get dimm total slot failed");
485 return;
486 }
487
Brandon Kim5a122a62023-05-04 04:25:03 +0000488 // In case the new size is smaller than old, trim the vector
489 if (num < dimms.size())
490 {
491 dimms.erase(dimms.begin() + num, dimms.end());
492 }
493
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800494 for (int index = 0; index < num; index++)
495 {
496 std::string path = dimmPath + std::to_string(index);
Brandon Kim5a122a62023-05-04 04:25:03 +0000497 if (index + 1 > dimms.size())
498 {
499 dimms.emplace_back(std::make_unique<phosphor::smbios::Dimm>(
500 bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
501 motherboardPath));
502 }
503 else
504 {
505 dimms[index]->memoryInfoUpdate(
506 smbiosDir.dir[smbiosDirIndex].dataStorage, motherboardPath);
507 }
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800508 }
Cheng C Yangb4651b92019-12-19 00:59:01 +0800509
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700510#endif
511
Jie Yang08e4a6d2021-08-23 13:07:41 -0700512 num = getTotalPcieSlot();
513 if (num == -1)
514 {
515 phosphor::logging::log<phosphor::logging::level::ERR>(
516 "get pcie total slot failed");
517 return;
518 }
519
Brandon Kim5a122a62023-05-04 04:25:03 +0000520 // In case the new size is smaller than old, trim the vector
521 if (num < pcies.size())
522 {
523 pcies.erase(pcies.begin() + num, pcies.end());
524 }
525
Jie Yang08e4a6d2021-08-23 13:07:41 -0700526 for (int index = 0; index < num; index++)
527 {
528 std::string path = pciePath + std::to_string(index);
Brandon Kim5a122a62023-05-04 04:25:03 +0000529 if (index + 1 > pcies.size())
530 {
531 pcies.emplace_back(std::make_unique<phosphor::smbios::Pcie>(
532 bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
533 motherboardPath));
534 }
535 else
536 {
537 pcies[index]->pcieInfoUpdate(
538 smbiosDir.dir[smbiosDirIndex].dataStorage, motherboardPath);
539 }
Jie Yang08e4a6d2021-08-23 13:07:41 -0700540 }
541
Cheng C Yangb4651b92019-12-19 00:59:01 +0800542 system.reset();
543 system = std::make_unique<System>(
544 bus, systemPath, smbiosDir.dir[smbiosDirIndex].dataStorage);
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800545}
546
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700547int MDRV2::getTotalCpuSlot()
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800548{
549 uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
550 int num = 0;
551
552 if (dataIn == nullptr)
553 {
554 phosphor::logging::log<phosphor::logging::level::ERR>(
555 "get cpu total slot failed - no storage data");
556 return -1;
557 }
558
559 while (1)
560 {
561 dataIn = getSMBIOSTypePtr(dataIn, processorsType);
562 if (dataIn == nullptr)
563 {
564 break;
565 }
566 num++;
567 dataIn = smbiosNextPtr(dataIn);
568 if (dataIn == nullptr)
569 {
570 break;
571 }
572 if (num >= limitEntryLen)
573 {
574 break;
575 }
576 }
577
578 return num;
579}
580
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700581int MDRV2::getTotalDimmSlot()
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800582{
583 uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
584 uint8_t num = 0;
585
586 if (dataIn == nullptr)
587 {
588 phosphor::logging::log<phosphor::logging::level::ERR>(
589 "Fail to get dimm total slot - no storage data");
590 return -1;
591 }
592
593 while (1)
594 {
595 dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType);
596 if (dataIn == nullptr)
597 {
598 break;
599 }
600 num++;
601 dataIn = smbiosNextPtr(dataIn);
602 if (dataIn == nullptr)
603 {
604 break;
605 }
606 if (num >= limitEntryLen)
607 {
608 break;
609 }
610 }
611
612 return num;
613}
614
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700615int MDRV2::getTotalPcieSlot()
Jie Yang08e4a6d2021-08-23 13:07:41 -0700616{
617 uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
618 int num = 0;
619
620 if (dataIn == nullptr)
621 {
622 phosphor::logging::log<phosphor::logging::level::ERR>(
623 "Fail to get total system slot - no storage data");
624 return -1;
625 }
626
627 while (1)
628 {
629 dataIn = getSMBIOSTypePtr(dataIn, systemSlots);
630 if (dataIn == nullptr)
631 {
632 break;
633 }
634
635 /* System slot type offset. Check if the slot is a PCIE slots. All
636 * PCIE slot type are hardcoded in a table.
637 */
638 if (pcieSmbiosType.find(*(dataIn + 5)) != pcieSmbiosType.end())
639 {
640 num++;
641 }
642 dataIn = smbiosNextPtr(dataIn);
643 if (dataIn == nullptr)
644 {
645 break;
646 }
647 if (num >= limitEntryLen)
648 {
649 break;
650 }
651 }
652
653 return num;
654}
655
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700656bool MDRV2::checkSMBIOSVersion(uint8_t* dataIn)
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530657{
Brandon Kime4ea3772022-02-23 20:21:10 -0800658 const std::string anchorString21 = "_SM_";
659 const std::string anchorString30 = "_SM3_";
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530660 std::string buffer(dataIn, dataIn + smbiosTableStorageSize);
661
662 auto it = std::search(std::begin(buffer), std::end(buffer),
Brandon Kime4ea3772022-02-23 20:21:10 -0800663 std::begin(anchorString21), std::end(anchorString21));
664 bool smbios21Found = it != std::end(buffer);
665 if (!smbios21Found)
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530666 {
Brandon Kime4ea3772022-02-23 20:21:10 -0800667 phosphor::logging::log<phosphor::logging::level::INFO>(
668 "SMBIOS 2.1 Anchor String not found. Looking for SMBIOS 3.0");
669 it = std::search(std::begin(buffer), std::end(buffer),
670 std::begin(anchorString30), std::end(anchorString30));
671 if (it == std::end(buffer))
672 {
673 phosphor::logging::log<phosphor::logging::level::ERR>(
674 "SMBIOS 2.1 and 3.0 Anchor Strings not found");
675 return false;
676 }
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530677 }
678
679 auto pos = std::distance(std::begin(buffer), it);
680 auto length = smbiosTableStorageSize - pos;
Brandon Kime4ea3772022-02-23 20:21:10 -0800681 uint8_t foundMajorVersion;
682 uint8_t foundMinorVersion;
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530683
Brandon Kime4ea3772022-02-23 20:21:10 -0800684 if (smbios21Found)
685 {
686 if (length < sizeof(EntryPointStructure21))
687 {
688 phosphor::logging::log<phosphor::logging::level::ERR>(
689 "Invalid entry point structure for SMBIOS 2.1");
690 return false;
691 }
692
693 auto epStructure =
694 reinterpret_cast<const EntryPointStructure21*>(&dataIn[pos]);
695 foundMajorVersion = epStructure->smbiosVersion.majorVersion;
696 foundMinorVersion = epStructure->smbiosVersion.minorVersion;
697 }
698 else
699 {
700 if (length < sizeof(EntryPointStructure30))
701 {
702 phosphor::logging::log<phosphor::logging::level::ERR>(
703 "Invalid entry point structure for SMBIOS 3.0");
704 return false;
705 }
706
707 auto epStructure =
708 reinterpret_cast<const EntryPointStructure30*>(&dataIn[pos]);
709 foundMajorVersion = epStructure->smbiosVersion.majorVersion;
710 foundMinorVersion = epStructure->smbiosVersion.minorVersion;
711 }
712 lg2::info("SMBIOS VERSION - {MAJOR}.{MINOR}", "MAJOR", foundMajorVersion,
713 "MINOR", foundMinorVersion);
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530714
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500715 auto itr = std::find_if(std::begin(supportedSMBIOSVersions),
716 std::end(supportedSMBIOSVersions),
717 [&](SMBIOSVersion versionItr) {
718 return versionItr.majorVersion == foundMajorVersion &&
719 versionItr.minorVersion == foundMinorVersion;
720 });
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530721 if (itr == std::end(supportedSMBIOSVersions))
722 {
723 return false;
724 }
725 return true;
726}
727
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700728bool MDRV2::agentSynchronizeData()
Cheng C Yangeecaf822019-12-19 00:34:23 +0800729{
Cheng C Yangec634252019-12-19 00:42:36 +0800730 struct MDRSMBIOSHeader mdr2SMBIOS;
731 bool status = readDataFromFlash(&mdr2SMBIOS,
732 smbiosDir.dir[smbiosDirIndex].dataStorage);
733 if (!status)
734 {
735 phosphor::logging::log<phosphor::logging::level::ERR>(
736 "agent data sync failed - read data from flash failed");
737 return false;
738 }
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530739
740 if (!checkSMBIOSVersion(smbiosDir.dir[smbiosDirIndex].dataStorage))
Cheng C Yangec634252019-12-19 00:42:36 +0800741 {
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530742 phosphor::logging::log<phosphor::logging::level::ERR>(
743 "Unsupported SMBIOS table version");
744 return false;
Cheng C Yangec634252019-12-19 00:42:36 +0800745 }
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530746
747 systemInfoUpdate();
748 smbiosDir.dir[smbiosDirIndex].common.dataVersion = mdr2SMBIOS.dirVer;
749 smbiosDir.dir[smbiosDirIndex].common.timestamp = mdr2SMBIOS.timestamp;
750 smbiosDir.dir[smbiosDirIndex].common.size = mdr2SMBIOS.dataSize;
751 smbiosDir.dir[smbiosDirIndex].stage = MDR2SMBIOSStatusEnum::mdr2Loaded;
752 smbiosDir.dir[smbiosDirIndex].lock = MDR2DirLockEnum::mdr2DirUnlock;
753
Cheng C Yangec634252019-12-19 00:42:36 +0800754 return true;
Cheng C Yangeecaf822019-12-19 00:34:23 +0800755}
756
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700757std::vector<uint32_t> MDRV2::synchronizeDirectoryCommonData(uint8_t idIndex,
758 uint32_t size)
Cheng C Yangeecaf822019-12-19 00:34:23 +0800759{
Cheng C Yangec634252019-12-19 00:42:36 +0800760 std::chrono::microseconds usec(
761 defaultTimeout); // default lock time out is 2s
762 std::vector<uint32_t> result;
763 smbiosDir.dir[idIndex].common.size = size;
764 result.push_back(smbiosDir.dir[idIndex].common.dataSetSize);
765 result.push_back(smbiosDir.dir[idIndex].common.dataVersion);
766 result.push_back(smbiosDir.dir[idIndex].common.timestamp);
767
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700768 timer.expires_after(usec);
769 timer.async_wait([this](boost::system::error_code ec) {
770 if (ec || this == nullptr)
771 {
772 phosphor::logging::log<phosphor::logging::level::ERR>(
773 "Timer Error!");
774 return;
775 }
776 agentSynchronizeData();
777 });
Cheng C Yangec634252019-12-19 00:42:36 +0800778 return result;
Cheng C Yangeecaf822019-12-19 00:34:23 +0800779}
780
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700781std::vector<boost::container::flat_map<std::string, RecordVariant>>
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700782 MDRV2::getRecordType(size_t type)
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700783{
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700784 std::vector<boost::container::flat_map<std::string, RecordVariant>> ret;
785 if (type == memoryDeviceType)
786 {
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700787 uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
788
789 if (dataIn == nullptr)
790 {
Kuiying Wanga4c08702020-11-18 19:27:20 +0800791 throw std::runtime_error("Data not populated");
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700792 }
793
794 do
795 {
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500796 dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType,
797 sizeof(MemoryInfo));
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700798 if (dataIn == nullptr)
799 {
800 break;
801 }
802 boost::container::flat_map<std::string, RecordVariant>& record =
803 ret.emplace_back();
804
805 auto memoryInfo = reinterpret_cast<MemoryInfo*>(dataIn);
806
807 record["Type"] = memoryInfo->type;
808 record["Length"] = memoryInfo->length;
809 record["Handle"] = uint16_t(memoryInfo->handle);
810 record["Physical Memory Array Handle"] =
811 uint16_t(memoryInfo->phyArrayHandle);
812 record["Memory Error Information Handle"] =
813 uint16_t(memoryInfo->errInfoHandle);
814 record["Total Width"] = uint16_t(memoryInfo->totalWidth);
815 record["Data Width"] = uint16_t(memoryInfo->dataWidth);
816 record["Size"] = uint16_t(memoryInfo->size);
817 record["Form Factor"] = memoryInfo->formFactor;
818 record["Device Set"] = memoryInfo->deviceSet;
819 record["Device Locator"] = positionToString(
820 memoryInfo->deviceLocator, memoryInfo->length, dataIn);
821 record["Bank Locator"] = positionToString(
822 memoryInfo->bankLocator, memoryInfo->length, dataIn);
823 record["Memory Type"] = memoryInfo->memoryType;
824 record["Type Detail"] = uint16_t(memoryInfo->typeDetail);
825 record["Speed"] = uint16_t(memoryInfo->speed);
826 record["Manufacturer"] = positionToString(
827 memoryInfo->manufacturer, memoryInfo->length, dataIn);
828 record["Serial Number"] = positionToString(
829 memoryInfo->serialNum, memoryInfo->length, dataIn);
830 record["Asset Tag"] = positionToString(memoryInfo->assetTag,
831 memoryInfo->length, dataIn);
832 record["Part Number"] = positionToString(
833 memoryInfo->partNum, memoryInfo->length, dataIn);
George Liu036374a2023-06-15 08:47:46 +0800834 record["Attributes"] = uint32_t(memoryInfo->attributes);
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700835 record["Extended Size"] = uint32_t(memoryInfo->extendedSize);
836 record["Configured Memory Speed"] =
837 uint32_t(memoryInfo->confClockSpeed);
838 record["Minimum voltage"] = uint16_t(memoryInfo->minimumVoltage);
839 record["Maximum voltage"] = uint16_t(memoryInfo->maximumVoltage);
840 record["Configured voltage"] =
841 uint16_t(memoryInfo->configuredVoltage);
842 record["Memory Technology"] = memoryInfo->memoryTechnology;
843 record["Memory Operating Mode Capabilty"] =
844 uint16_t(memoryInfo->memoryOperatingModeCap);
845 record["Firmare Version"] = memoryInfo->firwareVersion;
846 record["Module Manufacturer ID"] =
847 uint16_t(memoryInfo->modelManufId);
848 record["Module Product ID"] = uint16_t(memoryInfo->modelProdId);
849 record["Memory Subsystem Controller Manufacturer ID"] =
850 uint16_t(memoryInfo->memSubConManufId);
851 record["Memory Subsystem Controller Product Id"] =
852 uint16_t(memoryInfo->memSubConProdId);
853 record["Non-volatile Size"] = uint64_t(memoryInfo->nvSize);
854 record["Volatile Size"] = uint64_t(memoryInfo->volatileSize);
855 record["Cache Size"] = uint64_t(memoryInfo->cacheSize);
856 record["Logical Size"] = uint64_t(memoryInfo->logicalSize);
857 } while ((dataIn = smbiosNextPtr(dataIn)) != nullptr);
858
859 return ret;
860 }
861
Kuiying Wanga4c08702020-11-18 19:27:20 +0800862 throw std::invalid_argument("Invalid record type");
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700863 return ret;
864}
865
Cheng C Yangeecaf822019-12-19 00:34:23 +0800866} // namespace smbios
867} // namespace phosphor