blob: 99dc302d634fadacf3ff113067301e9fb03d94c7 [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
Josh Lehan027277a2023-09-11 05:28:19 -070038 std::ifstream smbiosFile(smbiosFilePath, std::ios_base::binary);
Prithvi A Paibc924d02021-12-29 04:54:43 +000039 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 }
Josh Lehan027277a2023-09-11 05:28:19 -0700219 std::ifstream smbiosFile(smbiosFilePath, std::ios_base::binary);
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700220 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);
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700228 size_t fileLength = smbiosFile.tellg();
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700229 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
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700312bool MDRV2::sendDataInformation(uint8_t idIndex, uint8_t /* flag */,
313 uint32_t dataLen, uint32_t dataVer,
314 uint32_t timeStamp)
Cheng C Yangeecaf822019-12-19 00:34:23 +0800315{
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
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700346int MDRV2::findIdIndex(std::vector<uint8_t> dataInfo)
Cheng C Yangeecaf822019-12-19 00:34:23 +0800347{
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 {
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700361 size_t info = 0;
Cheng C Yangeecaf822019-12-19 00:34:23 +0800362 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
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700378uint8_t MDRV2::directoryEntries(uint8_t value)
Cheng C Yangeecaf822019-12-19 00:34:23 +0800379{
Josh Lehan027277a2023-09-11 05:28:19 -0700380 std::ifstream smbiosFile(smbiosFilePath, std::ios_base::binary);
Prithvi A Paibc924d02021-12-29 04:54:43 +0000381 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 }
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700391 return sdbusplus::server::xyz::openbmc_project::smbios::MDRV2::
Cheng C Yangeecaf822019-12-19 00:34:23 +0800392 directoryEntries(value);
393}
394
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700395void MDRV2::systemInfoUpdate()
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800396{
Josh Lehan027277a2023-09-11 05:28:19 -0700397 // By default, look for System interface on any system/board/* object
398 std::string mapperAncestorPath = smbiosInventoryPath;
399 std::string matchParentPath = smbiosInventoryPath + "/board/";
400 bool requireExactMatch = false;
401
402 // If customized, look for System on only that custom object
403 if (smbiosInventoryPath != defaultInventoryPath)
404 {
405 std::filesystem::path path(smbiosInventoryPath);
406
407 // Search under parent to find exact match for self
408 mapperAncestorPath = path.parent_path().string();
409 matchParentPath = mapperAncestorPath;
410 requireExactMatch = true;
411 }
412
Jie Yange7cf3192021-08-20 11:21:43 -0700413 std::string motherboardPath;
Josh Lehan027277a2023-09-11 05:28:19 -0700414 auto method = bus->new_method_call(mapperBusName, mapperPath,
415 mapperInterface, "GetSubTreePaths");
416 method.append(mapperAncestorPath);
Gobinath Krishnamoorthybb9c6222022-11-03 20:08:01 +0000417 method.append(0);
418 method.append(std::vector<std::string>({systemInterface}));
Nikhil Namjoshidd1811c2023-03-14 22:51:48 +0000419
Jie Yange7cf3192021-08-20 11:21:43 -0700420 try
421 {
Gobinath Krishnamoorthybb9c6222022-11-03 20:08:01 +0000422 std::vector<std::string> paths;
Josh Lehan027277a2023-09-11 05:28:19 -0700423 sdbusplus::message_t reply = bus->call(method);
Gobinath Krishnamoorthybb9c6222022-11-03 20:08:01 +0000424 reply.read(paths);
Josh Lehan027277a2023-09-11 05:28:19 -0700425
426 size_t pathsCount = paths.size();
427 for (size_t i = 0; i < pathsCount; ++i)
428 {
429 if (requireExactMatch && (paths[i] != smbiosInventoryPath))
430 {
431 continue;
432 }
433
434 motherboardPath = std::move(paths[i]);
435 break;
436 }
437
438 if (motherboardPath.empty())
Gobinath Krishnamoorthybb9c6222022-11-03 20:08:01 +0000439 {
440 phosphor::logging::log<phosphor::logging::level::ERR>(
Nikhil Namjoshidd1811c2023-03-14 22:51:48 +0000441 "Failed to get system motherboard dbus path. Setting up a "
442 "match rule");
Josh Lehan027277a2023-09-11 05:28:19 -0700443
444 if (!motherboardConfigMatch)
445 {
Nikhil Namjoshidd1811c2023-03-14 22:51:48 +0000446 motherboardConfigMatch =
447 std::make_unique<sdbusplus::bus::match_t>(
Josh Lehan027277a2023-09-11 05:28:19 -0700448 *bus,
Nikhil Namjoshidd1811c2023-03-14 22:51:48 +0000449 sdbusplus::bus::match::rules::interfacesAdded() +
450 sdbusplus::bus::match::rules::argNpath(
Josh Lehan027277a2023-09-11 05:28:19 -0700451 0, matchParentPath),
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700452 [this](sdbusplus::message_t& msg) {
Josh Lehan027277a2023-09-11 05:28:19 -0700453 sdbusplus::message::object_path objectName;
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500454 boost::container::flat_map<
Josh Lehan027277a2023-09-11 05:28:19 -0700455 std::string,
456 boost::container::flat_map<
457 std::string, std::variant<std::string, uint64_t>>>
458 msgData;
459 msg.read(objectName, msgData);
460 if (msgData.contains(systemInterface))
461 {
462 systemInfoUpdate();
463 }
Nikhil Namjoshidd1811c2023-03-14 22:51:48 +0000464 });
Josh Lehan027277a2023-09-11 05:28:19 -0700465 }
Gobinath Krishnamoorthybb9c6222022-11-03 20:08:01 +0000466 }
467 else
468 {
Josh Lehan027277a2023-09-11 05:28:19 -0700469 lg2::info(
470 "Found Inventory anchor object for SMBIOS content {I}: {M}",
471 "I", smbiosInventoryPath, "M", motherboardPath);
Gobinath Krishnamoorthybb9c6222022-11-03 20:08:01 +0000472 }
Jie Yange7cf3192021-08-20 11:21:43 -0700473 }
474 catch (const sdbusplus::exception_t& e)
475 {
Josh Lehan027277a2023-09-11 05:28:19 -0700476 lg2::error(
477 "Exception while trying to find Inventory anchor object for SMBIOS content {I}: {E}",
478 "I", smbiosInventoryPath, "E", e.what());
Jie Yange7cf3192021-08-20 11:21:43 -0700479 phosphor::logging::log<phosphor::logging::level::ERR>(
480 "Failed to query system motherboard",
481 phosphor::logging::entry("ERROR=%s", e.what()));
482 }
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800483
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700484 std::optional<size_t> num = getTotalCpuSlot();
485 if (!num)
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800486 {
487 phosphor::logging::log<phosphor::logging::level::ERR>(
488 "get cpu total slot failed");
489 return;
490 }
491
Brandon Kim5a122a62023-05-04 04:25:03 +0000492 // In case the new size is smaller than old, trim the vector
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700493 if (*num < cpus.size())
Brandon Kim5a122a62023-05-04 04:25:03 +0000494 {
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700495 cpus.resize(*num);
Brandon Kim5a122a62023-05-04 04:25:03 +0000496 }
497
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700498 for (unsigned int index = 0; index < *num; index++)
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800499 {
Josh Lehan027277a2023-09-11 05:28:19 -0700500 std::string path = smbiosInventoryPath + cpuSuffix +
501 std::to_string(index);
Brandon Kim5a122a62023-05-04 04:25:03 +0000502 if (index + 1 > cpus.size())
503 {
504 cpus.emplace_back(std::make_unique<phosphor::smbios::Cpu>(
Josh Lehan027277a2023-09-11 05:28:19 -0700505 *bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
Brandon Kim5a122a62023-05-04 04:25:03 +0000506 motherboardPath));
507 }
508 else
509 {
510 cpus[index]->infoUpdate(smbiosDir.dir[smbiosDirIndex].dataStorage,
511 motherboardPath);
512 }
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800513 }
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800514
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700515#ifdef DIMM_DBUS
516
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800517 num = getTotalDimmSlot();
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700518 if (!num)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800519 {
520 phosphor::logging::log<phosphor::logging::level::ERR>(
521 "get dimm total slot failed");
522 return;
523 }
524
Brandon Kim5a122a62023-05-04 04:25:03 +0000525 // In case the new size is smaller than old, trim the vector
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700526 if (*num < dimms.size())
Brandon Kim5a122a62023-05-04 04:25:03 +0000527 {
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700528 dimms.resize(*num);
Brandon Kim5a122a62023-05-04 04:25:03 +0000529 }
530
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700531 for (unsigned int index = 0; index < *num; index++)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800532 {
Josh Lehan027277a2023-09-11 05:28:19 -0700533 std::string path = smbiosInventoryPath + dimmSuffix +
534 std::to_string(index);
Brandon Kim5a122a62023-05-04 04:25:03 +0000535 if (index + 1 > dimms.size())
536 {
537 dimms.emplace_back(std::make_unique<phosphor::smbios::Dimm>(
Josh Lehan027277a2023-09-11 05:28:19 -0700538 *bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
Brandon Kim5a122a62023-05-04 04:25:03 +0000539 motherboardPath));
540 }
541 else
542 {
543 dimms[index]->memoryInfoUpdate(
544 smbiosDir.dir[smbiosDirIndex].dataStorage, motherboardPath);
545 }
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800546 }
Cheng C Yangb4651b92019-12-19 00:59:01 +0800547
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700548#endif
549
Jie Yang08e4a6d2021-08-23 13:07:41 -0700550 num = getTotalPcieSlot();
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700551 if (!num)
Jie Yang08e4a6d2021-08-23 13:07:41 -0700552 {
553 phosphor::logging::log<phosphor::logging::level::ERR>(
554 "get pcie total slot failed");
555 return;
556 }
557
Brandon Kim5a122a62023-05-04 04:25:03 +0000558 // In case the new size is smaller than old, trim the vector
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700559 if (*num < pcies.size())
Brandon Kim5a122a62023-05-04 04:25:03 +0000560 {
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700561 pcies.resize(*num);
Brandon Kim5a122a62023-05-04 04:25:03 +0000562 }
563
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700564 for (unsigned int index = 0; index < *num; index++)
Jie Yang08e4a6d2021-08-23 13:07:41 -0700565 {
Josh Lehan027277a2023-09-11 05:28:19 -0700566 std::string path = smbiosInventoryPath + pcieSuffix +
567 std::to_string(index);
Brandon Kim5a122a62023-05-04 04:25:03 +0000568 if (index + 1 > pcies.size())
569 {
570 pcies.emplace_back(std::make_unique<phosphor::smbios::Pcie>(
Josh Lehan027277a2023-09-11 05:28:19 -0700571 *bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
Brandon Kim5a122a62023-05-04 04:25:03 +0000572 motherboardPath));
573 }
574 else
575 {
576 pcies[index]->pcieInfoUpdate(
577 smbiosDir.dir[smbiosDirIndex].dataStorage, motherboardPath);
578 }
Jie Yang08e4a6d2021-08-23 13:07:41 -0700579 }
580
Cheng C Yangb4651b92019-12-19 00:59:01 +0800581 system.reset();
Josh Lehan027277a2023-09-11 05:28:19 -0700582 system = std::make_unique<System>(bus, smbiosInventoryPath + systemSuffix,
583 smbiosDir.dir[smbiosDirIndex].dataStorage,
584 smbiosFilePath);
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800585}
586
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700587std::optional<size_t> MDRV2::getTotalCpuSlot()
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800588{
589 uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700590 size_t num = 0;
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800591
592 if (dataIn == nullptr)
593 {
594 phosphor::logging::log<phosphor::logging::level::ERR>(
595 "get cpu total slot failed - no storage data");
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700596 return std::nullopt;
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800597 }
598
599 while (1)
600 {
601 dataIn = getSMBIOSTypePtr(dataIn, processorsType);
602 if (dataIn == nullptr)
603 {
604 break;
605 }
606 num++;
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
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700621std::optional<size_t> MDRV2::getTotalDimmSlot()
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800622{
623 uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700624 size_t num = 0;
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800625
626 if (dataIn == nullptr)
627 {
628 phosphor::logging::log<phosphor::logging::level::ERR>(
629 "Fail to get dimm total slot - no storage data");
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700630 return std::nullopt;
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800631 }
632
633 while (1)
634 {
635 dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType);
636 if (dataIn == nullptr)
637 {
638 break;
639 }
640 num++;
641 dataIn = smbiosNextPtr(dataIn);
642 if (dataIn == nullptr)
643 {
644 break;
645 }
646 if (num >= limitEntryLen)
647 {
648 break;
649 }
650 }
651
652 return num;
653}
654
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700655std::optional<size_t> MDRV2::getTotalPcieSlot()
Jie Yang08e4a6d2021-08-23 13:07:41 -0700656{
657 uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700658 size_t num = 0;
Jie Yang08e4a6d2021-08-23 13:07:41 -0700659
660 if (dataIn == nullptr)
661 {
662 phosphor::logging::log<phosphor::logging::level::ERR>(
663 "Fail to get total system slot - no storage data");
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700664 return std::nullopt;
Jie Yang08e4a6d2021-08-23 13:07:41 -0700665 }
666
667 while (1)
668 {
669 dataIn = getSMBIOSTypePtr(dataIn, systemSlots);
670 if (dataIn == nullptr)
671 {
672 break;
673 }
674
675 /* System slot type offset. Check if the slot is a PCIE slots. All
676 * PCIE slot type are hardcoded in a table.
677 */
678 if (pcieSmbiosType.find(*(dataIn + 5)) != pcieSmbiosType.end())
679 {
680 num++;
681 }
682 dataIn = smbiosNextPtr(dataIn);
683 if (dataIn == nullptr)
684 {
685 break;
686 }
687 if (num >= limitEntryLen)
688 {
689 break;
690 }
691 }
692
693 return num;
694}
695
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700696bool MDRV2::checkSMBIOSVersion(uint8_t* dataIn)
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530697{
Brandon Kime4ea3772022-02-23 20:21:10 -0800698 const std::string anchorString21 = "_SM_";
699 const std::string anchorString30 = "_SM3_";
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530700 std::string buffer(dataIn, dataIn + smbiosTableStorageSize);
701
702 auto it = std::search(std::begin(buffer), std::end(buffer),
Brandon Kime4ea3772022-02-23 20:21:10 -0800703 std::begin(anchorString21), std::end(anchorString21));
704 bool smbios21Found = it != std::end(buffer);
705 if (!smbios21Found)
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530706 {
Brandon Kime4ea3772022-02-23 20:21:10 -0800707 phosphor::logging::log<phosphor::logging::level::INFO>(
708 "SMBIOS 2.1 Anchor String not found. Looking for SMBIOS 3.0");
709 it = std::search(std::begin(buffer), std::end(buffer),
710 std::begin(anchorString30), std::end(anchorString30));
711 if (it == std::end(buffer))
712 {
713 phosphor::logging::log<phosphor::logging::level::ERR>(
714 "SMBIOS 2.1 and 3.0 Anchor Strings not found");
715 return false;
716 }
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530717 }
718
719 auto pos = std::distance(std::begin(buffer), it);
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700720 size_t length = smbiosTableStorageSize - pos;
Brandon Kime4ea3772022-02-23 20:21:10 -0800721 uint8_t foundMajorVersion;
722 uint8_t foundMinorVersion;
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530723
Brandon Kime4ea3772022-02-23 20:21:10 -0800724 if (smbios21Found)
725 {
726 if (length < sizeof(EntryPointStructure21))
727 {
728 phosphor::logging::log<phosphor::logging::level::ERR>(
729 "Invalid entry point structure for SMBIOS 2.1");
730 return false;
731 }
732
733 auto epStructure =
734 reinterpret_cast<const EntryPointStructure21*>(&dataIn[pos]);
735 foundMajorVersion = epStructure->smbiosVersion.majorVersion;
736 foundMinorVersion = epStructure->smbiosVersion.minorVersion;
737 }
738 else
739 {
740 if (length < sizeof(EntryPointStructure30))
741 {
742 phosphor::logging::log<phosphor::logging::level::ERR>(
743 "Invalid entry point structure for SMBIOS 3.0");
744 return false;
745 }
746
747 auto epStructure =
748 reinterpret_cast<const EntryPointStructure30*>(&dataIn[pos]);
749 foundMajorVersion = epStructure->smbiosVersion.majorVersion;
750 foundMinorVersion = epStructure->smbiosVersion.minorVersion;
751 }
752 lg2::info("SMBIOS VERSION - {MAJOR}.{MINOR}", "MAJOR", foundMajorVersion,
753 "MINOR", foundMinorVersion);
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530754
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500755 auto itr = std::find_if(std::begin(supportedSMBIOSVersions),
756 std::end(supportedSMBIOSVersions),
757 [&](SMBIOSVersion versionItr) {
758 return versionItr.majorVersion == foundMajorVersion &&
759 versionItr.minorVersion == foundMinorVersion;
760 });
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530761 if (itr == std::end(supportedSMBIOSVersions))
762 {
763 return false;
764 }
765 return true;
766}
767
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700768bool MDRV2::agentSynchronizeData()
Cheng C Yangeecaf822019-12-19 00:34:23 +0800769{
Cheng C Yangec634252019-12-19 00:42:36 +0800770 struct MDRSMBIOSHeader mdr2SMBIOS;
771 bool status = readDataFromFlash(&mdr2SMBIOS,
772 smbiosDir.dir[smbiosDirIndex].dataStorage);
773 if (!status)
774 {
775 phosphor::logging::log<phosphor::logging::level::ERR>(
776 "agent data sync failed - read data from flash failed");
777 return false;
778 }
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530779
780 if (!checkSMBIOSVersion(smbiosDir.dir[smbiosDirIndex].dataStorage))
Cheng C Yangec634252019-12-19 00:42:36 +0800781 {
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530782 phosphor::logging::log<phosphor::logging::level::ERR>(
783 "Unsupported SMBIOS table version");
784 return false;
Cheng C Yangec634252019-12-19 00:42:36 +0800785 }
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530786
787 systemInfoUpdate();
788 smbiosDir.dir[smbiosDirIndex].common.dataVersion = mdr2SMBIOS.dirVer;
789 smbiosDir.dir[smbiosDirIndex].common.timestamp = mdr2SMBIOS.timestamp;
790 smbiosDir.dir[smbiosDirIndex].common.size = mdr2SMBIOS.dataSize;
791 smbiosDir.dir[smbiosDirIndex].stage = MDR2SMBIOSStatusEnum::mdr2Loaded;
792 smbiosDir.dir[smbiosDirIndex].lock = MDR2DirLockEnum::mdr2DirUnlock;
793
Cheng C Yangec634252019-12-19 00:42:36 +0800794 return true;
Cheng C Yangeecaf822019-12-19 00:34:23 +0800795}
796
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700797std::vector<uint32_t> MDRV2::synchronizeDirectoryCommonData(uint8_t idIndex,
798 uint32_t size)
Cheng C Yangeecaf822019-12-19 00:34:23 +0800799{
Cheng C Yangec634252019-12-19 00:42:36 +0800800 std::chrono::microseconds usec(
801 defaultTimeout); // default lock time out is 2s
802 std::vector<uint32_t> result;
803 smbiosDir.dir[idIndex].common.size = size;
804 result.push_back(smbiosDir.dir[idIndex].common.dataSetSize);
805 result.push_back(smbiosDir.dir[idIndex].common.dataVersion);
806 result.push_back(smbiosDir.dir[idIndex].common.timestamp);
807
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700808 timer.expires_after(usec);
809 timer.async_wait([this](boost::system::error_code ec) {
Josh Lehan027277a2023-09-11 05:28:19 -0700810 if (ec)
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700811 {
812 phosphor::logging::log<phosphor::logging::level::ERR>(
813 "Timer Error!");
814 return;
815 }
816 agentSynchronizeData();
817 });
Cheng C Yangec634252019-12-19 00:42:36 +0800818 return result;
Cheng C Yangeecaf822019-12-19 00:34:23 +0800819}
820
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700821std::vector<boost::container::flat_map<std::string, RecordVariant>>
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700822 MDRV2::getRecordType(size_t type)
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700823{
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700824 std::vector<boost::container::flat_map<std::string, RecordVariant>> ret;
825 if (type == memoryDeviceType)
826 {
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700827 uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
828
829 if (dataIn == nullptr)
830 {
Kuiying Wanga4c08702020-11-18 19:27:20 +0800831 throw std::runtime_error("Data not populated");
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700832 }
833
834 do
835 {
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500836 dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType,
837 sizeof(MemoryInfo));
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700838 if (dataIn == nullptr)
839 {
840 break;
841 }
842 boost::container::flat_map<std::string, RecordVariant>& record =
843 ret.emplace_back();
844
845 auto memoryInfo = reinterpret_cast<MemoryInfo*>(dataIn);
846
847 record["Type"] = memoryInfo->type;
848 record["Length"] = memoryInfo->length;
849 record["Handle"] = uint16_t(memoryInfo->handle);
850 record["Physical Memory Array Handle"] =
851 uint16_t(memoryInfo->phyArrayHandle);
852 record["Memory Error Information Handle"] =
853 uint16_t(memoryInfo->errInfoHandle);
854 record["Total Width"] = uint16_t(memoryInfo->totalWidth);
855 record["Data Width"] = uint16_t(memoryInfo->dataWidth);
856 record["Size"] = uint16_t(memoryInfo->size);
857 record["Form Factor"] = memoryInfo->formFactor;
858 record["Device Set"] = memoryInfo->deviceSet;
859 record["Device Locator"] = positionToString(
860 memoryInfo->deviceLocator, memoryInfo->length, dataIn);
861 record["Bank Locator"] = positionToString(
862 memoryInfo->bankLocator, memoryInfo->length, dataIn);
863 record["Memory Type"] = memoryInfo->memoryType;
864 record["Type Detail"] = uint16_t(memoryInfo->typeDetail);
865 record["Speed"] = uint16_t(memoryInfo->speed);
866 record["Manufacturer"] = positionToString(
867 memoryInfo->manufacturer, memoryInfo->length, dataIn);
868 record["Serial Number"] = positionToString(
869 memoryInfo->serialNum, memoryInfo->length, dataIn);
870 record["Asset Tag"] = positionToString(memoryInfo->assetTag,
871 memoryInfo->length, dataIn);
872 record["Part Number"] = positionToString(
873 memoryInfo->partNum, memoryInfo->length, dataIn);
George Liu036374a2023-06-15 08:47:46 +0800874 record["Attributes"] = uint32_t(memoryInfo->attributes);
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700875 record["Extended Size"] = uint32_t(memoryInfo->extendedSize);
876 record["Configured Memory Speed"] =
877 uint32_t(memoryInfo->confClockSpeed);
878 record["Minimum voltage"] = uint16_t(memoryInfo->minimumVoltage);
879 record["Maximum voltage"] = uint16_t(memoryInfo->maximumVoltage);
880 record["Configured voltage"] =
881 uint16_t(memoryInfo->configuredVoltage);
882 record["Memory Technology"] = memoryInfo->memoryTechnology;
883 record["Memory Operating Mode Capabilty"] =
884 uint16_t(memoryInfo->memoryOperatingModeCap);
885 record["Firmare Version"] = memoryInfo->firwareVersion;
886 record["Module Manufacturer ID"] =
887 uint16_t(memoryInfo->modelManufId);
888 record["Module Product ID"] = uint16_t(memoryInfo->modelProdId);
889 record["Memory Subsystem Controller Manufacturer ID"] =
890 uint16_t(memoryInfo->memSubConManufId);
891 record["Memory Subsystem Controller Product Id"] =
892 uint16_t(memoryInfo->memSubConProdId);
893 record["Non-volatile Size"] = uint64_t(memoryInfo->nvSize);
894 record["Volatile Size"] = uint64_t(memoryInfo->volatileSize);
895 record["Cache Size"] = uint64_t(memoryInfo->cacheSize);
896 record["Logical Size"] = uint64_t(memoryInfo->logicalSize);
897 } while ((dataIn = smbiosNextPtr(dataIn)) != nullptr);
898
899 return ret;
900 }
901
Kuiying Wanga4c08702020-11-18 19:27:20 +0800902 throw std::invalid_argument("Invalid record type");
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700903 return ret;
904}
905
Cheng C Yangeecaf822019-12-19 00:34:23 +0800906} // namespace smbios
907} // namespace phosphor