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 | |
Jason M. Bills | a3f5b38 | 2023-04-26 08:17:28 -0700 | [diff] [blame] | 34 | std::vector<uint8_t> MDRV2::getDirectoryInformation(uint8_t dirIndex) |
Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame] | 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 | |
Josh Lehan | 027277a | 2023-09-11 05:28:19 -0700 | [diff] [blame] | 38 | std::ifstream smbiosFile(smbiosFilePath, std::ios_base::binary); |
Prithvi A Pai | bc924d0 | 2021-12-29 04:54:43 +0000 | [diff] [blame] | 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 | |
Jason M. Bills | a3f5b38 | 2023-04-26 08:17:28 -0700 | [diff] [blame] | 77 | bool MDRV2::smbiosIsAvailForUpdate(uint8_t index) |
Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame] | 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 | |
Jason M. Bills | a3f5b38 | 2023-04-26 08:17:28 -0700 | [diff] [blame] | 119 | std::vector<uint8_t> MDRV2::getDataOffer() |
Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame] | 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 | |
Jason M. Bills | a3f5b38 | 2023-04-26 08:17:28 -0700 | [diff] [blame] | 137 | inline uint8_t MDRV2::smbiosValidFlag(uint8_t index) |
Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame] | 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 | |
Jason M. Bills | a3f5b38 | 2023-04-26 08:17:28 -0700 | [diff] [blame] | 180 | std::vector<uint8_t> MDRV2::getDataInformation(uint8_t idIndex) |
Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame] | 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 | |
Jason M. Bills | a3f5b38 | 2023-04-26 08:17:28 -0700 | [diff] [blame] | 205 | bool MDRV2::readDataFromFlash(MDRSMBIOSHeader* mdrHdr, uint8_t* data) |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 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 | } |
Josh Lehan | 027277a | 2023-09-11 05:28:19 -0700 | [diff] [blame] | 219 | std::ifstream smbiosFile(smbiosFilePath, std::ios_base::binary); |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 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 Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 228 | size_t fileLength = smbiosFile.tellg(); |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 229 | smbiosFile.seekg(0, std::ios_base::beg); |
| 230 | if (fileLength < sizeof(MDRSMBIOSHeader)) |
| 231 | { |
| 232 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 233 | "MDR V2 file size is smaller than mdr header"); |
| 234 | return false; |
| 235 | } |
| 236 | smbiosFile.read(reinterpret_cast<char*>(mdrHdr), sizeof(MDRSMBIOSHeader)); |
| 237 | if (mdrHdr->dataSize > smbiosTableStorageSize) |
| 238 | { |
| 239 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 240 | "Data size out of limitation"); |
| 241 | smbiosFile.close(); |
| 242 | return false; |
| 243 | } |
| 244 | fileLength -= sizeof(MDRSMBIOSHeader); |
| 245 | if (fileLength < mdrHdr->dataSize) |
| 246 | { |
| 247 | smbiosFile.read(reinterpret_cast<char*>(data), fileLength); |
| 248 | } |
| 249 | else |
| 250 | { |
| 251 | smbiosFile.read(reinterpret_cast<char*>(data), mdrHdr->dataSize); |
| 252 | } |
| 253 | smbiosFile.close(); |
| 254 | return true; |
| 255 | } |
| 256 | |
Jason M. Bills | a3f5b38 | 2023-04-26 08:17:28 -0700 | [diff] [blame] | 257 | bool MDRV2::sendDirectoryInformation(uint8_t dirVersion, uint8_t dirIndex, |
| 258 | uint8_t returnedEntries, |
| 259 | uint8_t remainingEntries, |
| 260 | std::vector<uint8_t> dirEntry) |
Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame] | 261 | { |
Manojkiran Eda | 0fe13ab | 2024-06-17 14:55:10 +0530 | [diff] [blame^] | 262 | bool terminate = false; |
Cheng C Yang | 608e52d | 2019-12-19 00:39:50 +0800 | [diff] [blame] | 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 | { |
Manojkiran Eda | 0fe13ab | 2024-06-17 14:55:10 +0530 | [diff] [blame^] | 280 | terminate = true; |
Cheng C Yang | 608e52d | 2019-12-19 00:39:50 +0800 | [diff] [blame] | 281 | } |
| 282 | else |
| 283 | { |
| 284 | if (remainingEntries > 0) |
| 285 | { |
Manojkiran Eda | 0fe13ab | 2024-06-17 14:55:10 +0530 | [diff] [blame^] | 286 | terminate = false; |
Cheng C Yang | 608e52d | 2019-12-19 00:39:50 +0800 | [diff] [blame] | 287 | } |
| 288 | else |
| 289 | { |
Manojkiran Eda | 0fe13ab | 2024-06-17 14:55:10 +0530 | [diff] [blame^] | 290 | terminate = true; |
Cheng C Yang | 608e52d | 2019-12-19 00:39:50 +0800 | [diff] [blame] | 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 | } |
Manojkiran Eda | 0fe13ab | 2024-06-17 14:55:10 +0530 | [diff] [blame^] | 309 | return terminate; |
Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame] | 310 | } |
| 311 | |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 312 | bool MDRV2::sendDataInformation(uint8_t idIndex, uint8_t /* flag */, |
| 313 | uint32_t dataLen, uint32_t dataVer, |
| 314 | uint32_t timeStamp) |
Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame] | 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 | |
Jason M. Bills | a3f5b38 | 2023-04-26 08:17:28 -0700 | [diff] [blame] | 346 | int MDRV2::findIdIndex(std::vector<uint8_t> dataInfo) |
Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame] | 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 | { |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 361 | size_t info = 0; |
Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame] | 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 | |
Jason M. Bills | a3f5b38 | 2023-04-26 08:17:28 -0700 | [diff] [blame] | 378 | uint8_t MDRV2::directoryEntries(uint8_t value) |
Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame] | 379 | { |
Josh Lehan | 027277a | 2023-09-11 05:28:19 -0700 | [diff] [blame] | 380 | std::ifstream smbiosFile(smbiosFilePath, std::ios_base::binary); |
Prithvi A Pai | bc924d0 | 2021-12-29 04:54:43 +0000 | [diff] [blame] | 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. Bills | 33ae81f | 2023-04-26 09:06:08 -0700 | [diff] [blame] | 391 | return sdbusplus::server::xyz::openbmc_project::smbios::MDRV2:: |
Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame] | 392 | directoryEntries(value); |
| 393 | } |
| 394 | |
Jason M. Bills | a3f5b38 | 2023-04-26 08:17:28 -0700 | [diff] [blame] | 395 | void MDRV2::systemInfoUpdate() |
Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [diff] [blame] | 396 | { |
Josh Lehan | 027277a | 2023-09-11 05:28:19 -0700 | [diff] [blame] | 397 | // 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 Yang | e7cf319 | 2021-08-20 11:21:43 -0700 | [diff] [blame] | 413 | std::string motherboardPath; |
Josh Lehan | 027277a | 2023-09-11 05:28:19 -0700 | [diff] [blame] | 414 | auto method = bus->new_method_call(mapperBusName, mapperPath, |
| 415 | mapperInterface, "GetSubTreePaths"); |
| 416 | method.append(mapperAncestorPath); |
Gobinath Krishnamoorthy | bb9c622 | 2022-11-03 20:08:01 +0000 | [diff] [blame] | 417 | method.append(0); |
Josh Lehan | abdccd3 | 2024-01-26 15:49:50 -0800 | [diff] [blame] | 418 | |
| 419 | // If customized, also accept Board as anchor, not just System |
| 420 | std::vector<std::string> desiredInterfaces{systemInterface}; |
| 421 | if (requireExactMatch) |
| 422 | { |
| 423 | desiredInterfaces.emplace_back(boardInterface); |
| 424 | } |
| 425 | method.append(desiredInterfaces); |
Nikhil Namjoshi | dd1811c | 2023-03-14 22:51:48 +0000 | [diff] [blame] | 426 | |
Jie Yang | e7cf319 | 2021-08-20 11:21:43 -0700 | [diff] [blame] | 427 | try |
| 428 | { |
Gobinath Krishnamoorthy | bb9c622 | 2022-11-03 20:08:01 +0000 | [diff] [blame] | 429 | std::vector<std::string> paths; |
Josh Lehan | 027277a | 2023-09-11 05:28:19 -0700 | [diff] [blame] | 430 | sdbusplus::message_t reply = bus->call(method); |
Gobinath Krishnamoorthy | bb9c622 | 2022-11-03 20:08:01 +0000 | [diff] [blame] | 431 | reply.read(paths); |
Josh Lehan | 027277a | 2023-09-11 05:28:19 -0700 | [diff] [blame] | 432 | |
| 433 | size_t pathsCount = paths.size(); |
| 434 | for (size_t i = 0; i < pathsCount; ++i) |
| 435 | { |
| 436 | if (requireExactMatch && (paths[i] != smbiosInventoryPath)) |
| 437 | { |
| 438 | continue; |
| 439 | } |
| 440 | |
| 441 | motherboardPath = std::move(paths[i]); |
| 442 | break; |
| 443 | } |
Jie Yang | e7cf319 | 2021-08-20 11:21:43 -0700 | [diff] [blame] | 444 | } |
| 445 | catch (const sdbusplus::exception_t& e) |
| 446 | { |
Josh Lehan | 027277a | 2023-09-11 05:28:19 -0700 | [diff] [blame] | 447 | lg2::error( |
| 448 | "Exception while trying to find Inventory anchor object for SMBIOS content {I}: {E}", |
| 449 | "I", smbiosInventoryPath, "E", e.what()); |
Jie Yang | e7cf319 | 2021-08-20 11:21:43 -0700 | [diff] [blame] | 450 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 451 | "Failed to query system motherboard", |
| 452 | phosphor::logging::entry("ERROR=%s", e.what())); |
| 453 | } |
Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [diff] [blame] | 454 | |
Josh Lehan | abdccd3 | 2024-01-26 15:49:50 -0800 | [diff] [blame] | 455 | if (motherboardPath.empty()) |
| 456 | { |
| 457 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 458 | "Failed to get system motherboard dbus path. Setting up a " |
| 459 | "match rule"); |
| 460 | |
| 461 | if (motherboardConfigMatch) |
| 462 | { |
| 463 | lg2::info("Motherboard match rule already exists"); |
| 464 | } |
| 465 | else |
| 466 | { |
| 467 | motherboardConfigMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 468 | *bus, |
| 469 | sdbusplus::bus::match::rules::interfacesAdded() + |
| 470 | sdbusplus::bus::match::rules::argNpath(0, matchParentPath), |
| 471 | [this, requireExactMatch](sdbusplus::message_t& msg) { |
| 472 | sdbusplus::message::object_path objectName; |
| 473 | boost::container::flat_map< |
| 474 | std::string, |
| 475 | boost::container::flat_map< |
| 476 | std::string, std::variant<std::string, uint64_t>>> |
| 477 | msgData; |
| 478 | msg.read(objectName, msgData); |
| 479 | bool gotMatch = false; |
| 480 | |
| 481 | if (msgData.contains(systemInterface)) |
| 482 | { |
| 483 | lg2::info("Successful match on system interface"); |
| 484 | gotMatch = true; |
| 485 | } |
| 486 | |
| 487 | // If customized, also accept Board as anchor, not just System |
| 488 | if (requireExactMatch && msgData.contains(boardInterface)) |
| 489 | { |
| 490 | lg2::info("Successful match on board interface"); |
| 491 | gotMatch = true; |
| 492 | } |
| 493 | |
| 494 | if (gotMatch) |
| 495 | { |
| 496 | // There is a race condition here: our desired interface |
| 497 | // has just been created, triggering the D-Bus callback, |
| 498 | // but Object Mapper has not been told of it yet. The |
| 499 | // mapper must also add it. Stall for time, so it can. |
| 500 | sleep(2); |
| 501 | systemInfoUpdate(); |
| 502 | } |
| 503 | }); |
| 504 | } |
| 505 | } |
| 506 | else |
| 507 | { |
| 508 | #ifdef ASSOC_TRIM_PATH |
| 509 | // When enabled, chop off last component of motherboardPath, to trim one |
| 510 | // layer, so that associations are built to the underlying chassis |
| 511 | // itself, not the system boards in the chassis. This is for |
| 512 | // compatibility with traditional systems which only have one |
| 513 | // motherboard per chassis. |
| 514 | std::filesystem::path foundPath(motherboardPath); |
| 515 | motherboardPath = foundPath.parent_path().string(); |
| 516 | #endif |
| 517 | |
| 518 | lg2::info("Found Inventory anchor object for SMBIOS content {I}: {M}", |
| 519 | "I", smbiosInventoryPath, "M", motherboardPath); |
| 520 | } |
| 521 | |
| 522 | lg2::info("Using Inventory anchor object for SMBIOS content {I}: {M}", "I", |
| 523 | smbiosInventoryPath, "M", motherboardPath); |
| 524 | |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 525 | std::optional<size_t> num = getTotalCpuSlot(); |
| 526 | if (!num) |
Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [diff] [blame] | 527 | { |
| 528 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 529 | "get cpu total slot failed"); |
| 530 | return; |
| 531 | } |
| 532 | |
Brandon Kim | 5a122a6 | 2023-05-04 04:25:03 +0000 | [diff] [blame] | 533 | // In case the new size is smaller than old, trim the vector |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 534 | if (*num < cpus.size()) |
Brandon Kim | 5a122a6 | 2023-05-04 04:25:03 +0000 | [diff] [blame] | 535 | { |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 536 | cpus.resize(*num); |
Brandon Kim | 5a122a6 | 2023-05-04 04:25:03 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 539 | for (unsigned int index = 0; index < *num; index++) |
Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [diff] [blame] | 540 | { |
Josh Lehan | 027277a | 2023-09-11 05:28:19 -0700 | [diff] [blame] | 541 | std::string path = smbiosInventoryPath + cpuSuffix + |
| 542 | std::to_string(index); |
Brandon Kim | 5a122a6 | 2023-05-04 04:25:03 +0000 | [diff] [blame] | 543 | if (index + 1 > cpus.size()) |
| 544 | { |
| 545 | cpus.emplace_back(std::make_unique<phosphor::smbios::Cpu>( |
Josh Lehan | 027277a | 2023-09-11 05:28:19 -0700 | [diff] [blame] | 546 | *bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage, |
Brandon Kim | 5a122a6 | 2023-05-04 04:25:03 +0000 | [diff] [blame] | 547 | motherboardPath)); |
| 548 | } |
| 549 | else |
| 550 | { |
| 551 | cpus[index]->infoUpdate(smbiosDir.dir[smbiosDirIndex].dataStorage, |
| 552 | motherboardPath); |
| 553 | } |
Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [diff] [blame] | 554 | } |
Cheng C Yang | 8c3fab6 | 2019-12-19 00:51:06 +0800 | [diff] [blame] | 555 | |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 556 | #ifdef DIMM_DBUS |
| 557 | |
Cheng C Yang | 8c3fab6 | 2019-12-19 00:51:06 +0800 | [diff] [blame] | 558 | num = getTotalDimmSlot(); |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 559 | if (!num) |
Cheng C Yang | 8c3fab6 | 2019-12-19 00:51:06 +0800 | [diff] [blame] | 560 | { |
| 561 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 562 | "get dimm total slot failed"); |
| 563 | return; |
| 564 | } |
| 565 | |
Brandon Kim | 5a122a6 | 2023-05-04 04:25:03 +0000 | [diff] [blame] | 566 | // In case the new size is smaller than old, trim the vector |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 567 | if (*num < dimms.size()) |
Brandon Kim | 5a122a6 | 2023-05-04 04:25:03 +0000 | [diff] [blame] | 568 | { |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 569 | dimms.resize(*num); |
Brandon Kim | 5a122a6 | 2023-05-04 04:25:03 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 572 | for (unsigned int index = 0; index < *num; index++) |
Cheng C Yang | 8c3fab6 | 2019-12-19 00:51:06 +0800 | [diff] [blame] | 573 | { |
Josh Lehan | 027277a | 2023-09-11 05:28:19 -0700 | [diff] [blame] | 574 | std::string path = smbiosInventoryPath + dimmSuffix + |
| 575 | std::to_string(index); |
Brandon Kim | 5a122a6 | 2023-05-04 04:25:03 +0000 | [diff] [blame] | 576 | if (index + 1 > dimms.size()) |
| 577 | { |
| 578 | dimms.emplace_back(std::make_unique<phosphor::smbios::Dimm>( |
Josh Lehan | 027277a | 2023-09-11 05:28:19 -0700 | [diff] [blame] | 579 | *bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage, |
Brandon Kim | 5a122a6 | 2023-05-04 04:25:03 +0000 | [diff] [blame] | 580 | motherboardPath)); |
| 581 | } |
| 582 | else |
| 583 | { |
| 584 | dimms[index]->memoryInfoUpdate( |
| 585 | smbiosDir.dir[smbiosDirIndex].dataStorage, motherboardPath); |
| 586 | } |
Cheng C Yang | 8c3fab6 | 2019-12-19 00:51:06 +0800 | [diff] [blame] | 587 | } |
Cheng C Yang | b4651b9 | 2019-12-19 00:59:01 +0800 | [diff] [blame] | 588 | |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 589 | #endif |
| 590 | |
Jie Yang | 08e4a6d | 2021-08-23 13:07:41 -0700 | [diff] [blame] | 591 | num = getTotalPcieSlot(); |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 592 | if (!num) |
Jie Yang | 08e4a6d | 2021-08-23 13:07:41 -0700 | [diff] [blame] | 593 | { |
| 594 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 595 | "get pcie total slot failed"); |
| 596 | return; |
| 597 | } |
| 598 | |
Brandon Kim | 5a122a6 | 2023-05-04 04:25:03 +0000 | [diff] [blame] | 599 | // In case the new size is smaller than old, trim the vector |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 600 | if (*num < pcies.size()) |
Brandon Kim | 5a122a6 | 2023-05-04 04:25:03 +0000 | [diff] [blame] | 601 | { |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 602 | pcies.resize(*num); |
Brandon Kim | 5a122a6 | 2023-05-04 04:25:03 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 605 | for (unsigned int index = 0; index < *num; index++) |
Jie Yang | 08e4a6d | 2021-08-23 13:07:41 -0700 | [diff] [blame] | 606 | { |
Josh Lehan | 027277a | 2023-09-11 05:28:19 -0700 | [diff] [blame] | 607 | std::string path = smbiosInventoryPath + pcieSuffix + |
| 608 | std::to_string(index); |
Brandon Kim | 5a122a6 | 2023-05-04 04:25:03 +0000 | [diff] [blame] | 609 | if (index + 1 > pcies.size()) |
| 610 | { |
| 611 | pcies.emplace_back(std::make_unique<phosphor::smbios::Pcie>( |
Josh Lehan | 027277a | 2023-09-11 05:28:19 -0700 | [diff] [blame] | 612 | *bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage, |
Brandon Kim | 5a122a6 | 2023-05-04 04:25:03 +0000 | [diff] [blame] | 613 | motherboardPath)); |
| 614 | } |
| 615 | else |
| 616 | { |
| 617 | pcies[index]->pcieInfoUpdate( |
| 618 | smbiosDir.dir[smbiosDirIndex].dataStorage, motherboardPath); |
| 619 | } |
Jie Yang | 08e4a6d | 2021-08-23 13:07:41 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Cheng C Yang | b4651b9 | 2019-12-19 00:59:01 +0800 | [diff] [blame] | 622 | system.reset(); |
Josh Lehan | 027277a | 2023-09-11 05:28:19 -0700 | [diff] [blame] | 623 | system = std::make_unique<System>(bus, smbiosInventoryPath + systemSuffix, |
| 624 | smbiosDir.dir[smbiosDirIndex].dataStorage, |
| 625 | smbiosFilePath); |
Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [diff] [blame] | 626 | } |
| 627 | |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 628 | std::optional<size_t> MDRV2::getTotalCpuSlot() |
Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [diff] [blame] | 629 | { |
| 630 | uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage; |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 631 | size_t num = 0; |
Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [diff] [blame] | 632 | |
| 633 | if (dataIn == nullptr) |
| 634 | { |
| 635 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 636 | "get cpu total slot failed - no storage data"); |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 637 | return std::nullopt; |
Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | while (1) |
| 641 | { |
| 642 | dataIn = getSMBIOSTypePtr(dataIn, processorsType); |
| 643 | if (dataIn == nullptr) |
| 644 | { |
| 645 | break; |
| 646 | } |
| 647 | num++; |
| 648 | dataIn = smbiosNextPtr(dataIn); |
| 649 | if (dataIn == nullptr) |
| 650 | { |
| 651 | break; |
| 652 | } |
| 653 | if (num >= limitEntryLen) |
| 654 | { |
| 655 | break; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | return num; |
| 660 | } |
| 661 | |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 662 | std::optional<size_t> MDRV2::getTotalDimmSlot() |
Cheng C Yang | 8c3fab6 | 2019-12-19 00:51:06 +0800 | [diff] [blame] | 663 | { |
| 664 | uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage; |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 665 | size_t num = 0; |
Cheng C Yang | 8c3fab6 | 2019-12-19 00:51:06 +0800 | [diff] [blame] | 666 | |
| 667 | if (dataIn == nullptr) |
| 668 | { |
| 669 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 670 | "Fail to get dimm total slot - no storage data"); |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 671 | return std::nullopt; |
Cheng C Yang | 8c3fab6 | 2019-12-19 00:51:06 +0800 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | while (1) |
| 675 | { |
| 676 | dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType); |
| 677 | if (dataIn == nullptr) |
| 678 | { |
| 679 | break; |
| 680 | } |
| 681 | num++; |
| 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 | |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 696 | std::optional<size_t> MDRV2::getTotalPcieSlot() |
Jie Yang | 08e4a6d | 2021-08-23 13:07:41 -0700 | [diff] [blame] | 697 | { |
| 698 | uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage; |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 699 | size_t num = 0; |
Jie Yang | 08e4a6d | 2021-08-23 13:07:41 -0700 | [diff] [blame] | 700 | |
| 701 | if (dataIn == nullptr) |
| 702 | { |
| 703 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 704 | "Fail to get total system slot - no storage data"); |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 705 | return std::nullopt; |
Jie Yang | 08e4a6d | 2021-08-23 13:07:41 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | while (1) |
| 709 | { |
| 710 | dataIn = getSMBIOSTypePtr(dataIn, systemSlots); |
| 711 | if (dataIn == nullptr) |
| 712 | { |
| 713 | break; |
| 714 | } |
| 715 | |
| 716 | /* System slot type offset. Check if the slot is a PCIE slots. All |
| 717 | * PCIE slot type are hardcoded in a table. |
| 718 | */ |
| 719 | if (pcieSmbiosType.find(*(dataIn + 5)) != pcieSmbiosType.end()) |
| 720 | { |
| 721 | num++; |
| 722 | } |
| 723 | dataIn = smbiosNextPtr(dataIn); |
| 724 | if (dataIn == nullptr) |
| 725 | { |
| 726 | break; |
| 727 | } |
| 728 | if (num >= limitEntryLen) |
| 729 | { |
| 730 | break; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | return num; |
| 735 | } |
| 736 | |
Jason M. Bills | a3f5b38 | 2023-04-26 08:17:28 -0700 | [diff] [blame] | 737 | bool MDRV2::checkSMBIOSVersion(uint8_t* dataIn) |
Arun P. Mohanan | 0435a48 | 2022-02-01 21:45:55 +0530 | [diff] [blame] | 738 | { |
Brandon Kim | e4ea377 | 2022-02-23 20:21:10 -0800 | [diff] [blame] | 739 | const std::string anchorString21 = "_SM_"; |
| 740 | const std::string anchorString30 = "_SM3_"; |
Josh Lehan | c6d87a5 | 2024-02-08 19:01:29 -0800 | [diff] [blame] | 741 | std::string buffer(reinterpret_cast<const char*>(dataIn), |
| 742 | smbiosTableStorageSize); |
Arun P. Mohanan | 0435a48 | 2022-02-01 21:45:55 +0530 | [diff] [blame] | 743 | |
| 744 | auto it = std::search(std::begin(buffer), std::end(buffer), |
Brandon Kim | e4ea377 | 2022-02-23 20:21:10 -0800 | [diff] [blame] | 745 | std::begin(anchorString21), std::end(anchorString21)); |
| 746 | bool smbios21Found = it != std::end(buffer); |
| 747 | if (!smbios21Found) |
Arun P. Mohanan | 0435a48 | 2022-02-01 21:45:55 +0530 | [diff] [blame] | 748 | { |
Brandon Kim | e4ea377 | 2022-02-23 20:21:10 -0800 | [diff] [blame] | 749 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 750 | "SMBIOS 2.1 Anchor String not found. Looking for SMBIOS 3.0"); |
| 751 | it = std::search(std::begin(buffer), std::end(buffer), |
| 752 | std::begin(anchorString30), std::end(anchorString30)); |
| 753 | if (it == std::end(buffer)) |
| 754 | { |
| 755 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 756 | "SMBIOS 2.1 and 3.0 Anchor Strings not found"); |
| 757 | return false; |
| 758 | } |
Arun P. Mohanan | 0435a48 | 2022-02-01 21:45:55 +0530 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | auto pos = std::distance(std::begin(buffer), it); |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 762 | size_t length = smbiosTableStorageSize - pos; |
Brandon Kim | e4ea377 | 2022-02-23 20:21:10 -0800 | [diff] [blame] | 763 | uint8_t foundMajorVersion; |
| 764 | uint8_t foundMinorVersion; |
Arun P. Mohanan | 0435a48 | 2022-02-01 21:45:55 +0530 | [diff] [blame] | 765 | |
Brandon Kim | e4ea377 | 2022-02-23 20:21:10 -0800 | [diff] [blame] | 766 | if (smbios21Found) |
| 767 | { |
| 768 | if (length < sizeof(EntryPointStructure21)) |
| 769 | { |
| 770 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 771 | "Invalid entry point structure for SMBIOS 2.1"); |
| 772 | return false; |
| 773 | } |
| 774 | |
| 775 | auto epStructure = |
| 776 | reinterpret_cast<const EntryPointStructure21*>(&dataIn[pos]); |
| 777 | foundMajorVersion = epStructure->smbiosVersion.majorVersion; |
| 778 | foundMinorVersion = epStructure->smbiosVersion.minorVersion; |
| 779 | } |
| 780 | else |
| 781 | { |
| 782 | if (length < sizeof(EntryPointStructure30)) |
| 783 | { |
| 784 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 785 | "Invalid entry point structure for SMBIOS 3.0"); |
| 786 | return false; |
| 787 | } |
| 788 | |
| 789 | auto epStructure = |
| 790 | reinterpret_cast<const EntryPointStructure30*>(&dataIn[pos]); |
| 791 | foundMajorVersion = epStructure->smbiosVersion.majorVersion; |
| 792 | foundMinorVersion = epStructure->smbiosVersion.minorVersion; |
| 793 | } |
| 794 | lg2::info("SMBIOS VERSION - {MAJOR}.{MINOR}", "MAJOR", foundMajorVersion, |
| 795 | "MINOR", foundMinorVersion); |
Arun P. Mohanan | 0435a48 | 2022-02-01 21:45:55 +0530 | [diff] [blame] | 796 | |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 797 | auto itr = std::find_if(std::begin(supportedSMBIOSVersions), |
| 798 | std::end(supportedSMBIOSVersions), |
| 799 | [&](SMBIOSVersion versionItr) { |
| 800 | return versionItr.majorVersion == foundMajorVersion && |
| 801 | versionItr.minorVersion == foundMinorVersion; |
| 802 | }); |
Arun P. Mohanan | 0435a48 | 2022-02-01 21:45:55 +0530 | [diff] [blame] | 803 | if (itr == std::end(supportedSMBIOSVersions)) |
| 804 | { |
| 805 | return false; |
| 806 | } |
| 807 | return true; |
| 808 | } |
| 809 | |
Jason M. Bills | a3f5b38 | 2023-04-26 08:17:28 -0700 | [diff] [blame] | 810 | bool MDRV2::agentSynchronizeData() |
Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame] | 811 | { |
Cheng C Yang | ec63425 | 2019-12-19 00:42:36 +0800 | [diff] [blame] | 812 | struct MDRSMBIOSHeader mdr2SMBIOS; |
| 813 | bool status = readDataFromFlash(&mdr2SMBIOS, |
| 814 | smbiosDir.dir[smbiosDirIndex].dataStorage); |
| 815 | if (!status) |
| 816 | { |
| 817 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 818 | "agent data sync failed - read data from flash failed"); |
| 819 | return false; |
| 820 | } |
Arun P. Mohanan | 0435a48 | 2022-02-01 21:45:55 +0530 | [diff] [blame] | 821 | |
| 822 | if (!checkSMBIOSVersion(smbiosDir.dir[smbiosDirIndex].dataStorage)) |
Cheng C Yang | ec63425 | 2019-12-19 00:42:36 +0800 | [diff] [blame] | 823 | { |
Arun P. Mohanan | 0435a48 | 2022-02-01 21:45:55 +0530 | [diff] [blame] | 824 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 825 | "Unsupported SMBIOS table version"); |
| 826 | return false; |
Cheng C Yang | ec63425 | 2019-12-19 00:42:36 +0800 | [diff] [blame] | 827 | } |
Arun P. Mohanan | 0435a48 | 2022-02-01 21:45:55 +0530 | [diff] [blame] | 828 | |
| 829 | systemInfoUpdate(); |
| 830 | smbiosDir.dir[smbiosDirIndex].common.dataVersion = mdr2SMBIOS.dirVer; |
| 831 | smbiosDir.dir[smbiosDirIndex].common.timestamp = mdr2SMBIOS.timestamp; |
| 832 | smbiosDir.dir[smbiosDirIndex].common.size = mdr2SMBIOS.dataSize; |
| 833 | smbiosDir.dir[smbiosDirIndex].stage = MDR2SMBIOSStatusEnum::mdr2Loaded; |
| 834 | smbiosDir.dir[smbiosDirIndex].lock = MDR2DirLockEnum::mdr2DirUnlock; |
| 835 | |
Cheng C Yang | ec63425 | 2019-12-19 00:42:36 +0800 | [diff] [blame] | 836 | return true; |
Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame] | 837 | } |
| 838 | |
Jason M. Bills | a3f5b38 | 2023-04-26 08:17:28 -0700 | [diff] [blame] | 839 | std::vector<uint32_t> MDRV2::synchronizeDirectoryCommonData(uint8_t idIndex, |
| 840 | uint32_t size) |
Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame] | 841 | { |
Cheng C Yang | ec63425 | 2019-12-19 00:42:36 +0800 | [diff] [blame] | 842 | std::chrono::microseconds usec( |
| 843 | defaultTimeout); // default lock time out is 2s |
| 844 | std::vector<uint32_t> result; |
| 845 | smbiosDir.dir[idIndex].common.size = size; |
| 846 | result.push_back(smbiosDir.dir[idIndex].common.dataSetSize); |
| 847 | result.push_back(smbiosDir.dir[idIndex].common.dataVersion); |
| 848 | result.push_back(smbiosDir.dir[idIndex].common.timestamp); |
| 849 | |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 850 | timer.expires_after(usec); |
| 851 | timer.async_wait([this](boost::system::error_code ec) { |
Josh Lehan | 027277a | 2023-09-11 05:28:19 -0700 | [diff] [blame] | 852 | if (ec) |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 853 | { |
| 854 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 855 | "Timer Error!"); |
| 856 | return; |
| 857 | } |
| 858 | agentSynchronizeData(); |
| 859 | }); |
Cheng C Yang | ec63425 | 2019-12-19 00:42:36 +0800 | [diff] [blame] | 860 | return result; |
Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame] | 861 | } |
| 862 | |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 863 | std::vector<boost::container::flat_map<std::string, RecordVariant>> |
Jason M. Bills | a3f5b38 | 2023-04-26 08:17:28 -0700 | [diff] [blame] | 864 | MDRV2::getRecordType(size_t type) |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 865 | { |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 866 | std::vector<boost::container::flat_map<std::string, RecordVariant>> ret; |
| 867 | if (type == memoryDeviceType) |
| 868 | { |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 869 | uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage; |
| 870 | |
| 871 | if (dataIn == nullptr) |
| 872 | { |
Kuiying Wang | a4c0870 | 2020-11-18 19:27:20 +0800 | [diff] [blame] | 873 | throw std::runtime_error("Data not populated"); |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | do |
| 877 | { |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 878 | dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType, |
| 879 | sizeof(MemoryInfo)); |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 880 | if (dataIn == nullptr) |
| 881 | { |
| 882 | break; |
| 883 | } |
| 884 | boost::container::flat_map<std::string, RecordVariant>& record = |
| 885 | ret.emplace_back(); |
| 886 | |
| 887 | auto memoryInfo = reinterpret_cast<MemoryInfo*>(dataIn); |
| 888 | |
| 889 | record["Type"] = memoryInfo->type; |
| 890 | record["Length"] = memoryInfo->length; |
| 891 | record["Handle"] = uint16_t(memoryInfo->handle); |
| 892 | record["Physical Memory Array Handle"] = |
| 893 | uint16_t(memoryInfo->phyArrayHandle); |
| 894 | record["Memory Error Information Handle"] = |
| 895 | uint16_t(memoryInfo->errInfoHandle); |
| 896 | record["Total Width"] = uint16_t(memoryInfo->totalWidth); |
| 897 | record["Data Width"] = uint16_t(memoryInfo->dataWidth); |
| 898 | record["Size"] = uint16_t(memoryInfo->size); |
| 899 | record["Form Factor"] = memoryInfo->formFactor; |
| 900 | record["Device Set"] = memoryInfo->deviceSet; |
| 901 | record["Device Locator"] = positionToString( |
| 902 | memoryInfo->deviceLocator, memoryInfo->length, dataIn); |
| 903 | record["Bank Locator"] = positionToString( |
| 904 | memoryInfo->bankLocator, memoryInfo->length, dataIn); |
| 905 | record["Memory Type"] = memoryInfo->memoryType; |
| 906 | record["Type Detail"] = uint16_t(memoryInfo->typeDetail); |
| 907 | record["Speed"] = uint16_t(memoryInfo->speed); |
| 908 | record["Manufacturer"] = positionToString( |
| 909 | memoryInfo->manufacturer, memoryInfo->length, dataIn); |
| 910 | record["Serial Number"] = positionToString( |
| 911 | memoryInfo->serialNum, memoryInfo->length, dataIn); |
| 912 | record["Asset Tag"] = positionToString(memoryInfo->assetTag, |
| 913 | memoryInfo->length, dataIn); |
| 914 | record["Part Number"] = positionToString( |
| 915 | memoryInfo->partNum, memoryInfo->length, dataIn); |
George Liu | 036374a | 2023-06-15 08:47:46 +0800 | [diff] [blame] | 916 | record["Attributes"] = uint32_t(memoryInfo->attributes); |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 917 | record["Extended Size"] = uint32_t(memoryInfo->extendedSize); |
| 918 | record["Configured Memory Speed"] = |
| 919 | uint32_t(memoryInfo->confClockSpeed); |
| 920 | record["Minimum voltage"] = uint16_t(memoryInfo->minimumVoltage); |
| 921 | record["Maximum voltage"] = uint16_t(memoryInfo->maximumVoltage); |
| 922 | record["Configured voltage"] = |
| 923 | uint16_t(memoryInfo->configuredVoltage); |
| 924 | record["Memory Technology"] = memoryInfo->memoryTechnology; |
Manojkiran Eda | 0fe13ab | 2024-06-17 14:55:10 +0530 | [diff] [blame^] | 925 | record["Memory Operating Mode Capability"] = |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 926 | uint16_t(memoryInfo->memoryOperatingModeCap); |
Manojkiran Eda | 0fe13ab | 2024-06-17 14:55:10 +0530 | [diff] [blame^] | 927 | record["Firmware Version"] = memoryInfo->firwareVersion; |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 928 | record["Module Manufacturer ID"] = |
| 929 | uint16_t(memoryInfo->modelManufId); |
| 930 | record["Module Product ID"] = uint16_t(memoryInfo->modelProdId); |
| 931 | record["Memory Subsystem Controller Manufacturer ID"] = |
| 932 | uint16_t(memoryInfo->memSubConManufId); |
| 933 | record["Memory Subsystem Controller Product Id"] = |
| 934 | uint16_t(memoryInfo->memSubConProdId); |
| 935 | record["Non-volatile Size"] = uint64_t(memoryInfo->nvSize); |
| 936 | record["Volatile Size"] = uint64_t(memoryInfo->volatileSize); |
| 937 | record["Cache Size"] = uint64_t(memoryInfo->cacheSize); |
| 938 | record["Logical Size"] = uint64_t(memoryInfo->logicalSize); |
| 939 | } while ((dataIn = smbiosNextPtr(dataIn)) != nullptr); |
| 940 | |
| 941 | return ret; |
| 942 | } |
| 943 | |
Kuiying Wang | a4c0870 | 2020-11-18 19:27:20 +0800 | [diff] [blame] | 944 | throw std::invalid_argument("Invalid record type"); |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 945 | return ret; |
| 946 | } |
| 947 | |
Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame] | 948 | } // namespace smbios |
| 949 | } // namespace phosphor |