Cheng C Yang | 3e3269a | 2019-12-02 15:11:30 +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 | 3e3269a | 2019-12-02 15:11:30 +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 | #pragma once |
| 18 | |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 19 | #include <phosphor-logging/elog-errors.hpp> |
| 20 | |
Cheng C Yang | 3e3269a | 2019-12-02 15:11:30 +0800 | [diff] [blame] | 21 | #include <array> |
| 22 | |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 23 | static constexpr const char* mdrType2File = "/var/lib/smbios/smbios2"; |
Cheng C Yang | 3e3269a | 2019-12-02 15:11:30 +0800 | [diff] [blame] | 24 | static constexpr const char* smbiosPath = "/var/lib/smbios"; |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 25 | |
Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame] | 26 | static constexpr uint16_t mdrSMBIOSSize = 32 * 1024; |
Cheng C Yang | 3e3269a | 2019-12-02 15:11:30 +0800 | [diff] [blame] | 27 | |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 28 | constexpr uint16_t smbiosAgentId = 0x0101; |
| 29 | constexpr int firstAgentIndex = 1; |
Cheng C Yang | 2ca7a0f | 2019-12-19 10:46:42 +0800 | [diff] [blame] | 30 | |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 31 | constexpr uint8_t maxDirEntries = 4; |
| 32 | constexpr uint32_t mdr2SMSize = 0x00100000; |
| 33 | constexpr uint32_t mdr2SMBaseAddress = 0x9FF00000; |
Cheng C Yang | 2ca7a0f | 2019-12-19 10:46:42 +0800 | [diff] [blame] | 34 | |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 35 | constexpr uint8_t mdrTypeII = 2; |
| 36 | |
| 37 | constexpr uint8_t mdr2Version = 2; |
| 38 | constexpr uint8_t smbiosAgentVersion = 1; |
| 39 | |
| 40 | constexpr uint32_t pageMask = 0xf000; |
| 41 | constexpr int smbiosDirIndex = 0; |
| 42 | |
| 43 | constexpr uint32_t smbiosTableVersion = 15; |
| 44 | constexpr uint32_t smbiosTableTimestamp = 0x45464748; |
| 45 | constexpr uint32_t smbiosSMMemoryOffset = 0; |
| 46 | constexpr uint32_t smbiosSMMemorySize = 1024 * 1024; |
| 47 | constexpr uint32_t smbiosTableStorageSize = 64 * 1024; |
| 48 | constexpr uint32_t defaultTimeout = 20000; |
| 49 | |
| 50 | enum class MDR2SMBIOSStatusEnum |
| 51 | { |
| 52 | mdr2Init = 0, |
| 53 | mdr2Loaded = 1, |
| 54 | mdr2Updated = 2, |
| 55 | mdr2Updating = 3 |
| 56 | }; |
| 57 | |
| 58 | enum class MDR2DirLockEnum |
| 59 | { |
| 60 | mdr2DirUnlock = 0, |
| 61 | mdr2DirLock = 1 |
| 62 | }; |
Cheng C Yang | 2ca7a0f | 2019-12-19 10:46:42 +0800 | [diff] [blame] | 63 | |
Cheng C Yang | 3e3269a | 2019-12-02 15:11:30 +0800 | [diff] [blame] | 64 | enum class DirDataRequestEnum |
| 65 | { |
| 66 | dirDataNotRequested = 0x00, |
| 67 | dirDataRequested = 0x01 |
| 68 | }; |
| 69 | |
| 70 | enum class FlagStatus |
| 71 | { |
| 72 | flagIsInvalid = 0, |
| 73 | flagIsValid = 1, |
| 74 | flagIsLocked = 2 |
| 75 | }; |
| 76 | |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 77 | typedef struct |
| 78 | { |
| 79 | uint8_t dataInfo[16]; |
| 80 | } DataIdStruct; |
| 81 | |
| 82 | typedef struct |
| 83 | { |
| 84 | DataIdStruct id; |
| 85 | uint32_t size; |
| 86 | uint32_t dataSetSize; |
| 87 | uint8_t dataVersion; |
| 88 | uint32_t timestamp; |
| 89 | } Mdr2DirEntry; |
| 90 | |
| 91 | typedef struct |
| 92 | { |
| 93 | Mdr2DirEntry common; |
| 94 | MDR2SMBIOSStatusEnum stage; |
| 95 | MDR2DirLockEnum lock; |
| 96 | uint16_t lockHandle; |
| 97 | uint32_t xferBuff; |
| 98 | uint32_t xferSize; |
| 99 | uint32_t maxDataSize; |
| 100 | uint8_t* dataStorage; |
| 101 | } Mdr2DirLocalStruct; |
| 102 | |
| 103 | typedef struct |
| 104 | { |
| 105 | uint8_t agentVersion; |
| 106 | uint8_t dirVersion; |
| 107 | uint8_t dirEntries; |
| 108 | uint8_t status; // valid / locked / etc |
| 109 | uint8_t remoteDirVersion; |
| 110 | uint16_t sessionHandle; |
| 111 | Mdr2DirLocalStruct dir[maxDirEntries]; |
| 112 | } Mdr2DirStruct; |
| 113 | |
| 114 | struct MDRSMBIOSHeader |
| 115 | { |
| 116 | uint8_t dirVer; |
| 117 | uint8_t mdrType; |
| 118 | uint32_t timestamp; |
| 119 | uint32_t dataSize; |
| 120 | } __attribute__((packed)); |
| 121 | |
Arun P. Mohanan | 0435a48 | 2022-02-01 21:45:55 +0530 | [diff] [blame] | 122 | typedef struct |
| 123 | { |
| 124 | uint8_t majorVersion; |
| 125 | uint8_t minorVersion; |
| 126 | } SMBIOSVersion; |
| 127 | |
Brandon Kim | e4ea377 | 2022-02-23 20:21:10 -0800 | [diff] [blame] | 128 | struct EntryPointStructure21 |
Arun P. Mohanan | 0435a48 | 2022-02-01 21:45:55 +0530 | [diff] [blame] | 129 | { |
| 130 | uint32_t anchorString; |
| 131 | uint8_t epChecksum; |
| 132 | uint8_t epLength; |
| 133 | SMBIOSVersion smbiosVersion; |
| 134 | uint16_t maxStructSize; |
| 135 | uint8_t epRevision; |
| 136 | uint8_t formattedArea[5]; |
| 137 | uint8_t intermediateAnchorString[5]; |
| 138 | uint8_t intermediateChecksum; |
| 139 | uint16_t structTableLength; |
| 140 | uint32_t structTableAddress; |
| 141 | uint16_t noOfSmbiosStruct; |
| 142 | uint8_t smbiosBDCRevision; |
| 143 | } __attribute__((packed)); |
| 144 | |
Brandon Kim | e4ea377 | 2022-02-23 20:21:10 -0800 | [diff] [blame] | 145 | struct EntryPointStructure30 |
| 146 | { |
| 147 | uint8_t anchorString[5]; |
| 148 | uint8_t epChecksum; |
| 149 | uint8_t epLength; |
| 150 | SMBIOSVersion smbiosVersion; |
| 151 | uint8_t smbiosDocRev; |
| 152 | uint8_t epRevision; |
| 153 | uint8_t reserved; |
| 154 | uint32_t structTableMaxSize; |
| 155 | uint64_t structTableAddr; |
| 156 | } __attribute__((packed)); |
| 157 | |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 158 | static constexpr const char* cpuPath = |
| 159 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/cpu"; |
| 160 | |
| 161 | static constexpr const char* dimmPath = |
| 162 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/dimm"; |
| 163 | |
Jie Yang | 08e4a6d | 2021-08-23 13:07:41 -0700 | [diff] [blame] | 164 | static constexpr const char* pciePath = |
| 165 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/pcieslot"; |
| 166 | |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 167 | static constexpr const char* systemPath = |
| 168 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/bios"; |
| 169 | |
shamim-ali | b492838 | 2022-08-26 15:50:15 +0530 | [diff] [blame] | 170 | constexpr std::array<SMBIOSVersion, 3> supportedSMBIOSVersions{ |
| 171 | SMBIOSVersion{3, 2}, SMBIOSVersion{3, 3}, SMBIOSVersion{3, 5}}; |
Arun P. Mohanan | 0435a48 | 2022-02-01 21:45:55 +0530 | [diff] [blame] | 172 | |
Cheng C Yang | 3e3269a | 2019-12-02 15:11:30 +0800 | [diff] [blame] | 173 | typedef enum |
| 174 | { |
| 175 | biosType = 0, |
| 176 | systemType = 1, |
| 177 | baseboardType = 2, |
| 178 | chassisType = 3, |
| 179 | processorsType = 4, |
| 180 | memoryControllerType = 5, |
| 181 | memoryModuleInformationType = 6, |
| 182 | cacheType = 7, |
| 183 | portConnectorType = 8, |
| 184 | systemSlots = 9, |
| 185 | onBoardDevicesType = 10, |
| 186 | oemStringsType = 11, |
| 187 | systemCconfigurationOptionsType = 12, |
| 188 | biosLanguageType = 13, |
| 189 | groupAssociatonsType = 14, |
| 190 | systemEventLogType = 15, |
| 191 | physicalMemoryArrayType = 16, |
| 192 | memoryDeviceType = 17, |
| 193 | } SmbiosType; |
Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [diff] [blame] | 194 | |
| 195 | static constexpr uint8_t separateLen = 2; |
| 196 | |
| 197 | static inline uint8_t* smbiosNextPtr(uint8_t* smbiosDataIn) |
| 198 | { |
| 199 | if (smbiosDataIn == nullptr) |
| 200 | { |
| 201 | return nullptr; |
| 202 | } |
| 203 | uint8_t* smbiosData = smbiosDataIn + *(smbiosDataIn + 1); |
| 204 | int len = 0; |
| 205 | while ((*smbiosData | *(smbiosData + 1)) != 0) |
| 206 | { |
| 207 | smbiosData++; |
| 208 | len++; |
| 209 | if (len >= mdrSMBIOSSize) // To avoid endless loop |
| 210 | { |
| 211 | return nullptr; |
| 212 | } |
| 213 | } |
| 214 | return smbiosData + separateLen; |
| 215 | } |
| 216 | |
| 217 | // When first time run getSMBIOSTypePtr, need to send the RegionS[].regionData |
| 218 | // to smbiosDataIn |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 219 | static inline uint8_t* getSMBIOSTypePtr(uint8_t* smbiosDataIn, uint8_t typeId, |
| 220 | size_t size = 0) |
Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [diff] [blame] | 221 | { |
| 222 | if (smbiosDataIn == nullptr) |
| 223 | { |
| 224 | return nullptr; |
| 225 | } |
| 226 | char* smbiosData = reinterpret_cast<char*>(smbiosDataIn); |
| 227 | while ((*smbiosData != '\0') || (*(smbiosData + 1) != '\0')) |
| 228 | { |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 229 | uint32_t len = *(smbiosData + 1); |
Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [diff] [blame] | 230 | if (*smbiosData != typeId) |
| 231 | { |
Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [diff] [blame] | 232 | smbiosData += len; |
| 233 | while ((*smbiosData != '\0') || (*(smbiosData + 1) != '\0')) |
| 234 | { |
| 235 | smbiosData++; |
| 236 | len++; |
| 237 | if (len >= mdrSMBIOSSize) // To avoid endless loop |
| 238 | { |
| 239 | return nullptr; |
| 240 | } |
| 241 | } |
| 242 | smbiosData += separateLen; |
| 243 | continue; |
| 244 | } |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 245 | if (len < size) |
| 246 | { |
| 247 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 248 | "Record size mismatch!"); |
| 249 | return nullptr; |
| 250 | } |
Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [diff] [blame] | 251 | return reinterpret_cast<uint8_t*>(smbiosData); |
| 252 | } |
| 253 | return nullptr; |
| 254 | } |
| 255 | |
| 256 | static inline std::string positionToString(uint8_t positionNum, |
| 257 | uint8_t structLen, uint8_t* dataIn) |
| 258 | { |
Gokul Sanker V G | 473d890 | 2022-02-18 01:13:53 +0530 | [diff] [blame] | 259 | if (dataIn == nullptr || positionNum == 0) |
Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [diff] [blame] | 260 | { |
| 261 | return ""; |
| 262 | } |
| 263 | uint16_t limit = mdrSMBIOSSize; // set a limit to avoid endless loop |
| 264 | |
| 265 | char* target = reinterpret_cast<char*>(dataIn + structLen); |
Kuiying Wang | 59bb9d9 | 2020-10-27 15:30:46 +0800 | [diff] [blame] | 266 | if (target == nullptr) |
| 267 | { |
| 268 | return ""; |
| 269 | } |
Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [diff] [blame] | 270 | for (uint8_t index = 1; index < positionNum; index++) |
| 271 | { |
| 272 | for (; *target != '\0'; target++) |
| 273 | { |
| 274 | limit--; |
Kuiying Wang | 59bb9d9 | 2020-10-27 15:30:46 +0800 | [diff] [blame] | 275 | // When target = dataIn + structLen + limit, |
| 276 | // following target++ will be nullptr |
| 277 | if (limit < 1 || target == nullptr) |
Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [diff] [blame] | 278 | { |
| 279 | return ""; |
| 280 | } |
| 281 | } |
| 282 | target++; |
Kuiying Wang | 59bb9d9 | 2020-10-27 15:30:46 +0800 | [diff] [blame] | 283 | if (target == nullptr || *target == '\0') |
Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [diff] [blame] | 284 | { |
| 285 | return ""; // 0x00 0x00 means end of the entry. |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | std::string result = target; |
| 290 | return result; |
| 291 | } |