blob: aba2775f14969c2ae584f54d805b702e65a3e1b9 [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);
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{
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 }
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{
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/"),
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700423 [this](sdbusplus::message_t& msg) {
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500424 sdbusplus::message::object_path objectName;
425 boost::container::flat_map<
426 std::string,
427 boost::container::flat_map<
428 std::string, std::variant<std::string, uint64_t>>>
429 msgData;
430 msg.read(objectName, msgData);
431 if (msgData.contains(systemInterface))
432 {
433 this->systemInfoUpdate();
434 }
Nikhil Namjoshidd1811c2023-03-14 22:51:48 +0000435 });
Gobinath Krishnamoorthybb9c6222022-11-03 20:08:01 +0000436 }
437 else
438 {
439 motherboardPath = std::move(paths[0]);
440 }
Jie Yange7cf3192021-08-20 11:21:43 -0700441 }
442 catch (const sdbusplus::exception_t& e)
443 {
444 phosphor::logging::log<phosphor::logging::level::ERR>(
445 "Failed to query system motherboard",
446 phosphor::logging::entry("ERROR=%s", e.what()));
447 }
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800448
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700449 std::optional<size_t> num = getTotalCpuSlot();
450 if (!num)
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800451 {
452 phosphor::logging::log<phosphor::logging::level::ERR>(
453 "get cpu total slot failed");
454 return;
455 }
456
Brandon Kim5a122a62023-05-04 04:25:03 +0000457 // In case the new size is smaller than old, trim the vector
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700458 if (*num < cpus.size())
Brandon Kim5a122a62023-05-04 04:25:03 +0000459 {
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700460 cpus.resize(*num);
Brandon Kim5a122a62023-05-04 04:25:03 +0000461 }
462
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700463 for (unsigned int index = 0; index < *num; index++)
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800464 {
465 std::string path = cpuPath + std::to_string(index);
Brandon Kim5a122a62023-05-04 04:25:03 +0000466 if (index + 1 > cpus.size())
467 {
468 cpus.emplace_back(std::make_unique<phosphor::smbios::Cpu>(
469 bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
470 motherboardPath));
471 }
472 else
473 {
474 cpus[index]->infoUpdate(smbiosDir.dir[smbiosDirIndex].dataStorage,
475 motherboardPath);
476 }
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800477 }
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800478
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700479#ifdef DIMM_DBUS
480
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800481 num = getTotalDimmSlot();
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700482 if (!num)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800483 {
484 phosphor::logging::log<phosphor::logging::level::ERR>(
485 "get dimm total slot failed");
486 return;
487 }
488
Brandon Kim5a122a62023-05-04 04:25:03 +0000489 // In case the new size is smaller than old, trim the vector
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700490 if (*num < dimms.size())
Brandon Kim5a122a62023-05-04 04:25:03 +0000491 {
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700492 dimms.resize(*num);
Brandon Kim5a122a62023-05-04 04:25:03 +0000493 }
494
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700495 for (unsigned int index = 0; index < *num; index++)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800496 {
497 std::string path = dimmPath + std::to_string(index);
Brandon Kim5a122a62023-05-04 04:25:03 +0000498 if (index + 1 > dimms.size())
499 {
500 dimms.emplace_back(std::make_unique<phosphor::smbios::Dimm>(
501 bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
502 motherboardPath));
503 }
504 else
505 {
506 dimms[index]->memoryInfoUpdate(
507 smbiosDir.dir[smbiosDirIndex].dataStorage, motherboardPath);
508 }
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800509 }
Cheng C Yangb4651b92019-12-19 00:59:01 +0800510
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700511#endif
512
Jie Yang08e4a6d2021-08-23 13:07:41 -0700513 num = getTotalPcieSlot();
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700514 if (!num)
Jie Yang08e4a6d2021-08-23 13:07:41 -0700515 {
516 phosphor::logging::log<phosphor::logging::level::ERR>(
517 "get pcie total slot failed");
518 return;
519 }
520
Brandon Kim5a122a62023-05-04 04:25:03 +0000521 // In case the new size is smaller than old, trim the vector
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700522 if (*num < pcies.size())
Brandon Kim5a122a62023-05-04 04:25:03 +0000523 {
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700524 pcies.resize(*num);
Brandon Kim5a122a62023-05-04 04:25:03 +0000525 }
526
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700527 for (unsigned int index = 0; index < *num; index++)
Jie Yang08e4a6d2021-08-23 13:07:41 -0700528 {
529 std::string path = pciePath + std::to_string(index);
Brandon Kim5a122a62023-05-04 04:25:03 +0000530 if (index + 1 > pcies.size())
531 {
532 pcies.emplace_back(std::make_unique<phosphor::smbios::Pcie>(
533 bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage,
534 motherboardPath));
535 }
536 else
537 {
538 pcies[index]->pcieInfoUpdate(
539 smbiosDir.dir[smbiosDirIndex].dataStorage, motherboardPath);
540 }
Jie Yang08e4a6d2021-08-23 13:07:41 -0700541 }
542
Cheng C Yangb4651b92019-12-19 00:59:01 +0800543 system.reset();
544 system = std::make_unique<System>(
545 bus, systemPath, smbiosDir.dir[smbiosDirIndex].dataStorage);
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800546}
547
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700548std::optional<size_t> MDRV2::getTotalCpuSlot()
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800549{
550 uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700551 size_t num = 0;
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800552
553 if (dataIn == nullptr)
554 {
555 phosphor::logging::log<phosphor::logging::level::ERR>(
556 "get cpu total slot failed - no storage data");
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700557 return std::nullopt;
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800558 }
559
560 while (1)
561 {
562 dataIn = getSMBIOSTypePtr(dataIn, processorsType);
563 if (dataIn == nullptr)
564 {
565 break;
566 }
567 num++;
568 dataIn = smbiosNextPtr(dataIn);
569 if (dataIn == nullptr)
570 {
571 break;
572 }
573 if (num >= limitEntryLen)
574 {
575 break;
576 }
577 }
578
579 return num;
580}
581
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700582std::optional<size_t> MDRV2::getTotalDimmSlot()
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800583{
584 uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700585 size_t num = 0;
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800586
587 if (dataIn == nullptr)
588 {
589 phosphor::logging::log<phosphor::logging::level::ERR>(
590 "Fail to get dimm total slot - no storage data");
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700591 return std::nullopt;
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800592 }
593
594 while (1)
595 {
596 dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType);
597 if (dataIn == nullptr)
598 {
599 break;
600 }
601 num++;
602 dataIn = smbiosNextPtr(dataIn);
603 if (dataIn == nullptr)
604 {
605 break;
606 }
607 if (num >= limitEntryLen)
608 {
609 break;
610 }
611 }
612
613 return num;
614}
615
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700616std::optional<size_t> MDRV2::getTotalPcieSlot()
Jie Yang08e4a6d2021-08-23 13:07:41 -0700617{
618 uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700619 size_t num = 0;
Jie Yang08e4a6d2021-08-23 13:07:41 -0700620
621 if (dataIn == nullptr)
622 {
623 phosphor::logging::log<phosphor::logging::level::ERR>(
624 "Fail to get total system slot - no storage data");
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700625 return std::nullopt;
Jie Yang08e4a6d2021-08-23 13:07:41 -0700626 }
627
628 while (1)
629 {
630 dataIn = getSMBIOSTypePtr(dataIn, systemSlots);
631 if (dataIn == nullptr)
632 {
633 break;
634 }
635
636 /* System slot type offset. Check if the slot is a PCIE slots. All
637 * PCIE slot type are hardcoded in a table.
638 */
639 if (pcieSmbiosType.find(*(dataIn + 5)) != pcieSmbiosType.end())
640 {
641 num++;
642 }
643 dataIn = smbiosNextPtr(dataIn);
644 if (dataIn == nullptr)
645 {
646 break;
647 }
648 if (num >= limitEntryLen)
649 {
650 break;
651 }
652 }
653
654 return num;
655}
656
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700657bool MDRV2::checkSMBIOSVersion(uint8_t* dataIn)
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530658{
Brandon Kime4ea3772022-02-23 20:21:10 -0800659 const std::string anchorString21 = "_SM_";
660 const std::string anchorString30 = "_SM3_";
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530661 std::string buffer(dataIn, dataIn + smbiosTableStorageSize);
662
663 auto it = std::search(std::begin(buffer), std::end(buffer),
Brandon Kime4ea3772022-02-23 20:21:10 -0800664 std::begin(anchorString21), std::end(anchorString21));
665 bool smbios21Found = it != std::end(buffer);
666 if (!smbios21Found)
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530667 {
Brandon Kime4ea3772022-02-23 20:21:10 -0800668 phosphor::logging::log<phosphor::logging::level::INFO>(
669 "SMBIOS 2.1 Anchor String not found. Looking for SMBIOS 3.0");
670 it = std::search(std::begin(buffer), std::end(buffer),
671 std::begin(anchorString30), std::end(anchorString30));
672 if (it == std::end(buffer))
673 {
674 phosphor::logging::log<phosphor::logging::level::ERR>(
675 "SMBIOS 2.1 and 3.0 Anchor Strings not found");
676 return false;
677 }
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530678 }
679
680 auto pos = std::distance(std::begin(buffer), it);
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700681 size_t length = smbiosTableStorageSize - pos;
Brandon Kime4ea3772022-02-23 20:21:10 -0800682 uint8_t foundMajorVersion;
683 uint8_t foundMinorVersion;
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530684
Brandon Kime4ea3772022-02-23 20:21:10 -0800685 if (smbios21Found)
686 {
687 if (length < sizeof(EntryPointStructure21))
688 {
689 phosphor::logging::log<phosphor::logging::level::ERR>(
690 "Invalid entry point structure for SMBIOS 2.1");
691 return false;
692 }
693
694 auto epStructure =
695 reinterpret_cast<const EntryPointStructure21*>(&dataIn[pos]);
696 foundMajorVersion = epStructure->smbiosVersion.majorVersion;
697 foundMinorVersion = epStructure->smbiosVersion.minorVersion;
698 }
699 else
700 {
701 if (length < sizeof(EntryPointStructure30))
702 {
703 phosphor::logging::log<phosphor::logging::level::ERR>(
704 "Invalid entry point structure for SMBIOS 3.0");
705 return false;
706 }
707
708 auto epStructure =
709 reinterpret_cast<const EntryPointStructure30*>(&dataIn[pos]);
710 foundMajorVersion = epStructure->smbiosVersion.majorVersion;
711 foundMinorVersion = epStructure->smbiosVersion.minorVersion;
712 }
713 lg2::info("SMBIOS VERSION - {MAJOR}.{MINOR}", "MAJOR", foundMajorVersion,
714 "MINOR", foundMinorVersion);
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530715
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500716 auto itr = std::find_if(std::begin(supportedSMBIOSVersions),
717 std::end(supportedSMBIOSVersions),
718 [&](SMBIOSVersion versionItr) {
719 return versionItr.majorVersion == foundMajorVersion &&
720 versionItr.minorVersion == foundMinorVersion;
721 });
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530722 if (itr == std::end(supportedSMBIOSVersions))
723 {
724 return false;
725 }
726 return true;
727}
728
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700729bool MDRV2::agentSynchronizeData()
Cheng C Yangeecaf822019-12-19 00:34:23 +0800730{
Cheng C Yangec634252019-12-19 00:42:36 +0800731 struct MDRSMBIOSHeader mdr2SMBIOS;
732 bool status = readDataFromFlash(&mdr2SMBIOS,
733 smbiosDir.dir[smbiosDirIndex].dataStorage);
734 if (!status)
735 {
736 phosphor::logging::log<phosphor::logging::level::ERR>(
737 "agent data sync failed - read data from flash failed");
738 return false;
739 }
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530740
741 if (!checkSMBIOSVersion(smbiosDir.dir[smbiosDirIndex].dataStorage))
Cheng C Yangec634252019-12-19 00:42:36 +0800742 {
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530743 phosphor::logging::log<phosphor::logging::level::ERR>(
744 "Unsupported SMBIOS table version");
745 return false;
Cheng C Yangec634252019-12-19 00:42:36 +0800746 }
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530747
748 systemInfoUpdate();
749 smbiosDir.dir[smbiosDirIndex].common.dataVersion = mdr2SMBIOS.dirVer;
750 smbiosDir.dir[smbiosDirIndex].common.timestamp = mdr2SMBIOS.timestamp;
751 smbiosDir.dir[smbiosDirIndex].common.size = mdr2SMBIOS.dataSize;
752 smbiosDir.dir[smbiosDirIndex].stage = MDR2SMBIOSStatusEnum::mdr2Loaded;
753 smbiosDir.dir[smbiosDirIndex].lock = MDR2DirLockEnum::mdr2DirUnlock;
754
Cheng C Yangec634252019-12-19 00:42:36 +0800755 return true;
Cheng C Yangeecaf822019-12-19 00:34:23 +0800756}
757
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700758std::vector<uint32_t> MDRV2::synchronizeDirectoryCommonData(uint8_t idIndex,
759 uint32_t size)
Cheng C Yangeecaf822019-12-19 00:34:23 +0800760{
Cheng C Yangec634252019-12-19 00:42:36 +0800761 std::chrono::microseconds usec(
762 defaultTimeout); // default lock time out is 2s
763 std::vector<uint32_t> result;
764 smbiosDir.dir[idIndex].common.size = size;
765 result.push_back(smbiosDir.dir[idIndex].common.dataSetSize);
766 result.push_back(smbiosDir.dir[idIndex].common.dataVersion);
767 result.push_back(smbiosDir.dir[idIndex].common.timestamp);
768
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700769 timer.expires_after(usec);
770 timer.async_wait([this](boost::system::error_code ec) {
771 if (ec || this == nullptr)
772 {
773 phosphor::logging::log<phosphor::logging::level::ERR>(
774 "Timer Error!");
775 return;
776 }
777 agentSynchronizeData();
778 });
Cheng C Yangec634252019-12-19 00:42:36 +0800779 return result;
Cheng C Yangeecaf822019-12-19 00:34:23 +0800780}
781
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700782std::vector<boost::container::flat_map<std::string, RecordVariant>>
Jason M. Billsa3f5b382023-04-26 08:17:28 -0700783 MDRV2::getRecordType(size_t type)
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700784{
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700785 std::vector<boost::container::flat_map<std::string, RecordVariant>> ret;
786 if (type == memoryDeviceType)
787 {
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700788 uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
789
790 if (dataIn == nullptr)
791 {
Kuiying Wanga4c08702020-11-18 19:27:20 +0800792 throw std::runtime_error("Data not populated");
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700793 }
794
795 do
796 {
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500797 dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType,
798 sizeof(MemoryInfo));
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700799 if (dataIn == nullptr)
800 {
801 break;
802 }
803 boost::container::flat_map<std::string, RecordVariant>& record =
804 ret.emplace_back();
805
806 auto memoryInfo = reinterpret_cast<MemoryInfo*>(dataIn);
807
808 record["Type"] = memoryInfo->type;
809 record["Length"] = memoryInfo->length;
810 record["Handle"] = uint16_t(memoryInfo->handle);
811 record["Physical Memory Array Handle"] =
812 uint16_t(memoryInfo->phyArrayHandle);
813 record["Memory Error Information Handle"] =
814 uint16_t(memoryInfo->errInfoHandle);
815 record["Total Width"] = uint16_t(memoryInfo->totalWidth);
816 record["Data Width"] = uint16_t(memoryInfo->dataWidth);
817 record["Size"] = uint16_t(memoryInfo->size);
818 record["Form Factor"] = memoryInfo->formFactor;
819 record["Device Set"] = memoryInfo->deviceSet;
820 record["Device Locator"] = positionToString(
821 memoryInfo->deviceLocator, memoryInfo->length, dataIn);
822 record["Bank Locator"] = positionToString(
823 memoryInfo->bankLocator, memoryInfo->length, dataIn);
824 record["Memory Type"] = memoryInfo->memoryType;
825 record["Type Detail"] = uint16_t(memoryInfo->typeDetail);
826 record["Speed"] = uint16_t(memoryInfo->speed);
827 record["Manufacturer"] = positionToString(
828 memoryInfo->manufacturer, memoryInfo->length, dataIn);
829 record["Serial Number"] = positionToString(
830 memoryInfo->serialNum, memoryInfo->length, dataIn);
831 record["Asset Tag"] = positionToString(memoryInfo->assetTag,
832 memoryInfo->length, dataIn);
833 record["Part Number"] = positionToString(
834 memoryInfo->partNum, memoryInfo->length, dataIn);
George Liu036374a2023-06-15 08:47:46 +0800835 record["Attributes"] = uint32_t(memoryInfo->attributes);
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700836 record["Extended Size"] = uint32_t(memoryInfo->extendedSize);
837 record["Configured Memory Speed"] =
838 uint32_t(memoryInfo->confClockSpeed);
839 record["Minimum voltage"] = uint16_t(memoryInfo->minimumVoltage);
840 record["Maximum voltage"] = uint16_t(memoryInfo->maximumVoltage);
841 record["Configured voltage"] =
842 uint16_t(memoryInfo->configuredVoltage);
843 record["Memory Technology"] = memoryInfo->memoryTechnology;
844 record["Memory Operating Mode Capabilty"] =
845 uint16_t(memoryInfo->memoryOperatingModeCap);
846 record["Firmare Version"] = memoryInfo->firwareVersion;
847 record["Module Manufacturer ID"] =
848 uint16_t(memoryInfo->modelManufId);
849 record["Module Product ID"] = uint16_t(memoryInfo->modelProdId);
850 record["Memory Subsystem Controller Manufacturer ID"] =
851 uint16_t(memoryInfo->memSubConManufId);
852 record["Memory Subsystem Controller Product Id"] =
853 uint16_t(memoryInfo->memSubConProdId);
854 record["Non-volatile Size"] = uint64_t(memoryInfo->nvSize);
855 record["Volatile Size"] = uint64_t(memoryInfo->volatileSize);
856 record["Cache Size"] = uint64_t(memoryInfo->cacheSize);
857 record["Logical Size"] = uint64_t(memoryInfo->logicalSize);
858 } while ((dataIn = smbiosNextPtr(dataIn)) != nullptr);
859
860 return ret;
861 }
862
Kuiying Wanga4c08702020-11-18 19:27:20 +0800863 throw std::invalid_argument("Invalid record type");
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700864 return ret;
865}
866
Cheng C Yangeecaf822019-12-19 00:34:23 +0800867} // namespace smbios
868} // namespace phosphor