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