blob: 1bf773376b49192ad4eb69dacf18a95f2d07ba08 [file] [log] [blame]
Cheng C Yang3e3269a2019-12-02 15:11:30 +08001/*
Zhikui Ren18a5ab92020-09-01 21:35:20 -07002// Copyright (c) 2018 Intel Corporation
Cheng C Yang3e3269a2019-12-02 15:11:30 +08003//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
17#pragma once
18
Zhikui Ren18a5ab92020-09-01 21:35:20 -070019#include <phosphor-logging/elog-errors.hpp>
20
Cheng C Yang3e3269a2019-12-02 15:11:30 +080021#include <array>
Josh Lehan027277a2023-09-11 05:28:19 -070022#include <string>
Cheng C Yang3e3269a2019-12-02 15:11:30 +080023
Josh Lehan027277a2023-09-11 05:28:19 -070024static constexpr const char* mdrDefaultFile = "/var/lib/smbios/smbios2";
Zhikui Ren18a5ab92020-09-01 21:35:20 -070025
Cheng C Yangeecaf822019-12-19 00:34:23 +080026static constexpr uint16_t mdrSMBIOSSize = 32 * 1024;
Cheng C Yang3e3269a2019-12-02 15:11:30 +080027
Zhikui Ren18a5ab92020-09-01 21:35:20 -070028constexpr uint16_t smbiosAgentId = 0x0101;
29constexpr int firstAgentIndex = 1;
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +080030
Zhikui Ren18a5ab92020-09-01 21:35:20 -070031constexpr uint8_t maxDirEntries = 4;
32constexpr uint32_t mdr2SMSize = 0x00100000;
33constexpr uint32_t mdr2SMBaseAddress = 0x9FF00000;
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +080034
Josh Lehanf079e832023-09-19 15:52:52 -070035constexpr uint8_t mdrDirVersion = 1;
Zhikui Ren18a5ab92020-09-01 21:35:20 -070036constexpr uint8_t mdrTypeII = 2;
37
38constexpr uint8_t mdr2Version = 2;
39constexpr uint8_t smbiosAgentVersion = 1;
Josh Lehanf079e832023-09-19 15:52:52 -070040constexpr uint8_t smbiosDirVersion = 1;
Zhikui Ren18a5ab92020-09-01 21:35:20 -070041
42constexpr uint32_t pageMask = 0xf000;
43constexpr int smbiosDirIndex = 0;
44
45constexpr uint32_t smbiosTableVersion = 15;
46constexpr uint32_t smbiosTableTimestamp = 0x45464748;
47constexpr uint32_t smbiosSMMemoryOffset = 0;
48constexpr uint32_t smbiosSMMemorySize = 1024 * 1024;
49constexpr uint32_t smbiosTableStorageSize = 64 * 1024;
Yaswanth Reddy Md8dd1202023-08-08 11:20:12 +000050constexpr uint32_t defaultTimeout = 2'000'000; // 2-seconds.
Zhikui Ren18a5ab92020-09-01 21:35:20 -070051
52enum class MDR2SMBIOSStatusEnum
53{
54 mdr2Init = 0,
55 mdr2Loaded = 1,
56 mdr2Updated = 2,
57 mdr2Updating = 3
58};
59
60enum class MDR2DirLockEnum
61{
62 mdr2DirUnlock = 0,
63 mdr2DirLock = 1
64};
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +080065
Cheng C Yang3e3269a2019-12-02 15:11:30 +080066enum class DirDataRequestEnum
67{
68 dirDataNotRequested = 0x00,
69 dirDataRequested = 0x01
70};
71
72enum class FlagStatus
73{
74 flagIsInvalid = 0,
75 flagIsValid = 1,
76 flagIsLocked = 2
77};
78
Zhikui Ren18a5ab92020-09-01 21:35:20 -070079typedef struct
80{
81 uint8_t dataInfo[16];
82} DataIdStruct;
83
84typedef struct
85{
86 DataIdStruct id;
87 uint32_t size;
88 uint32_t dataSetSize;
89 uint8_t dataVersion;
90 uint32_t timestamp;
91} Mdr2DirEntry;
92
93typedef struct
94{
95 Mdr2DirEntry common;
96 MDR2SMBIOSStatusEnum stage;
97 MDR2DirLockEnum lock;
98 uint16_t lockHandle;
99 uint32_t xferBuff;
100 uint32_t xferSize;
101 uint32_t maxDataSize;
102 uint8_t* dataStorage;
103} Mdr2DirLocalStruct;
104
105typedef struct
106{
107 uint8_t agentVersion;
108 uint8_t dirVersion;
109 uint8_t dirEntries;
110 uint8_t status; // valid / locked / etc
111 uint8_t remoteDirVersion;
112 uint16_t sessionHandle;
113 Mdr2DirLocalStruct dir[maxDirEntries];
114} Mdr2DirStruct;
115
116struct MDRSMBIOSHeader
117{
118 uint8_t dirVer;
119 uint8_t mdrType;
120 uint32_t timestamp;
121 uint32_t dataSize;
122} __attribute__((packed));
123
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530124typedef struct
125{
126 uint8_t majorVersion;
127 uint8_t minorVersion;
128} SMBIOSVersion;
129
Brandon Kime4ea3772022-02-23 20:21:10 -0800130struct EntryPointStructure21
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530131{
132 uint32_t anchorString;
133 uint8_t epChecksum;
134 uint8_t epLength;
135 SMBIOSVersion smbiosVersion;
136 uint16_t maxStructSize;
137 uint8_t epRevision;
138 uint8_t formattedArea[5];
139 uint8_t intermediateAnchorString[5];
140 uint8_t intermediateChecksum;
141 uint16_t structTableLength;
142 uint32_t structTableAddress;
143 uint16_t noOfSmbiosStruct;
144 uint8_t smbiosBDCRevision;
145} __attribute__((packed));
146
Brandon Kime4ea3772022-02-23 20:21:10 -0800147struct EntryPointStructure30
148{
149 uint8_t anchorString[5];
150 uint8_t epChecksum;
151 uint8_t epLength;
152 SMBIOSVersion smbiosVersion;
153 uint8_t smbiosDocRev;
154 uint8_t epRevision;
155 uint8_t reserved;
156 uint32_t structTableMaxSize;
157 uint64_t structTableAddr;
158} __attribute__((packed));
159
Prithvi Pai6f9e7a72025-03-14 13:05:02 +0530160struct StructureHeader
161{
162 uint8_t type;
163 uint8_t length;
164 uint16_t handle;
165} __attribute__((packed));
166
Josh Lehan027277a2023-09-11 05:28:19 -0700167static constexpr const char* cpuSuffix = "/chassis/motherboard/cpu";
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700168
Josh Lehan027277a2023-09-11 05:28:19 -0700169static constexpr const char* dimmSuffix = "/chassis/motherboard/dimm";
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700170
Josh Lehan027277a2023-09-11 05:28:19 -0700171static constexpr const char* pcieSuffix = "/chassis/motherboard/pcieslot";
Jie Yang08e4a6d2021-08-23 13:07:41 -0700172
Josh Lehan027277a2023-09-11 05:28:19 -0700173static constexpr const char* systemSuffix = "/chassis/motherboard/bios";
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700174
Prithvi Pai5af42bb2025-02-11 17:59:07 +0530175static constexpr const char* tpmSuffix = "/chassis/motherboard/tpm";
176
Prithvi Pai6f9e7a72025-03-14 13:05:02 +0530177static constexpr const char* firmwarePath = "/xyz/openbmc_project/software";
178
Prithvi Pai7a8ef1e2025-06-02 11:59:49 +0530179static constexpr const char* defaultMotherboardPath =
180 "/xyz/openbmc_project/inventory/system/chassis/motherboard";
181
Jayaprakash Mutyala669e4c32025-01-20 06:09:05 +0000182constexpr std::array<SMBIOSVersion, 8> supportedSMBIOSVersions{
Manojkiran Eda7a2cf162024-11-12 11:49:26 +0530183 SMBIOSVersion{3, 0}, SMBIOSVersion{3, 2}, SMBIOSVersion{3, 3},
Quang Nguyen80498222024-12-02 14:39:44 +0700184 SMBIOSVersion{3, 4}, SMBIOSVersion{3, 5}, SMBIOSVersion{3, 6},
Jayaprakash Mutyala669e4c32025-01-20 06:09:05 +0000185 SMBIOSVersion{3, 7}, SMBIOSVersion{3, 8}};
Arun P. Mohanan0435a482022-02-01 21:45:55 +0530186
Cheng C Yang3e3269a2019-12-02 15:11:30 +0800187typedef enum
188{
189 biosType = 0,
190 systemType = 1,
191 baseboardType = 2,
192 chassisType = 3,
193 processorsType = 4,
194 memoryControllerType = 5,
195 memoryModuleInformationType = 6,
196 cacheType = 7,
197 portConnectorType = 8,
198 systemSlots = 9,
199 onBoardDevicesType = 10,
200 oemStringsType = 11,
201 systemCconfigurationOptionsType = 12,
202 biosLanguageType = 13,
203 groupAssociatonsType = 14,
204 systemEventLogType = 15,
205 physicalMemoryArrayType = 16,
206 memoryDeviceType = 17,
Prithvi Pai6f9e7a72025-03-14 13:05:02 +0530207 systemPowerSupply = 39,
208 onboardDevicesExtended = 41,
Prithvi Pai5af42bb2025-02-11 17:59:07 +0530209 tpmDeviceType = 43,
Prithvi Pai6f9e7a72025-03-14 13:05:02 +0530210 firmwareInventoryInformationType = 45,
Cheng C Yang3e3269a2019-12-02 15:11:30 +0800211} SmbiosType;
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800212
213static constexpr uint8_t separateLen = 2;
214
215static inline uint8_t* smbiosNextPtr(uint8_t* smbiosDataIn)
216{
217 if (smbiosDataIn == nullptr)
218 {
219 return nullptr;
220 }
221 uint8_t* smbiosData = smbiosDataIn + *(smbiosDataIn + 1);
222 int len = 0;
223 while ((*smbiosData | *(smbiosData + 1)) != 0)
224 {
225 smbiosData++;
226 len++;
227 if (len >= mdrSMBIOSSize) // To avoid endless loop
228 {
229 return nullptr;
230 }
231 }
232 return smbiosData + separateLen;
233}
234
235// When first time run getSMBIOSTypePtr, need to send the RegionS[].regionData
236// to smbiosDataIn
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700237static inline uint8_t* getSMBIOSTypePtr(uint8_t* smbiosDataIn, uint8_t typeId,
238 size_t size = 0)
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800239{
240 if (smbiosDataIn == nullptr)
241 {
242 return nullptr;
243 }
244 char* smbiosData = reinterpret_cast<char*>(smbiosDataIn);
245 while ((*smbiosData != '\0') || (*(smbiosData + 1) != '\0'))
246 {
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700247 uint32_t len = *(smbiosData + 1);
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800248 if (*smbiosData != typeId)
249 {
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800250 smbiosData += len;
251 while ((*smbiosData != '\0') || (*(smbiosData + 1) != '\0'))
252 {
253 smbiosData++;
254 len++;
255 if (len >= mdrSMBIOSSize) // To avoid endless loop
256 {
257 return nullptr;
258 }
259 }
260 smbiosData += separateLen;
261 continue;
262 }
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700263 if (len < size)
264 {
265 phosphor::logging::log<phosphor::logging::level::ERR>(
266 "Record size mismatch!");
267 return nullptr;
268 }
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800269 return reinterpret_cast<uint8_t*>(smbiosData);
270 }
271 return nullptr;
272}
273
Prithvi Pai6f9e7a72025-03-14 13:05:02 +0530274static inline uint8_t* smbiosSkipEntryPoint(uint8_t* smbiosDataIn)
275{
276 const std::string anchorString30 = "_SM3_";
277 if (smbiosDataIn == nullptr)
278 {
279 return nullptr;
280 }
281
282 // Jump to starting address of the SMBIOS Structure Table from Entry Point
283 auto anchor = reinterpret_cast<const char*>(smbiosDataIn);
284 if (std::string_view(anchor, anchorString30.length())
285 .compare(anchorString30) == 0)
286 {
287 auto epStructure =
288 reinterpret_cast<const EntryPointStructure30*>(smbiosDataIn);
289 if (epStructure->structTableAddr < mdrSMBIOSSize)
290 {
291 smbiosDataIn += epStructure->structTableAddr;
292 }
293 }
294
295 return smbiosDataIn;
296}
297
298static inline uint8_t* smbiosHandlePtr(uint8_t* smbiosDataIn, uint16_t handle)
299{
300 auto ptr = smbiosSkipEntryPoint(smbiosDataIn);
301 struct StructureHeader* header;
302 while (ptr != nullptr)
303 {
304 header = reinterpret_cast<struct StructureHeader*>(ptr);
305 if (header->length < sizeof(StructureHeader))
306 {
307 return nullptr;
308 }
309
310 if (header->handle == handle)
311 {
312 return ptr;
313 }
314 ptr = smbiosNextPtr(ptr);
315 }
316 return nullptr;
317}
318
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800319static inline std::string positionToString(uint8_t positionNum,
320 uint8_t structLen, uint8_t* dataIn)
321{
Gokul Sanker V G473d8902022-02-18 01:13:53 +0530322 if (dataIn == nullptr || positionNum == 0)
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800323 {
324 return "";
325 }
326 uint16_t limit = mdrSMBIOSSize; // set a limit to avoid endless loop
327
328 char* target = reinterpret_cast<char*>(dataIn + structLen);
Kuiying Wang59bb9d92020-10-27 15:30:46 +0800329 if (target == nullptr)
330 {
331 return "";
332 }
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800333 for (uint8_t index = 1; index < positionNum; index++)
334 {
335 for (; *target != '\0'; target++)
336 {
337 limit--;
Kuiying Wang59bb9d92020-10-27 15:30:46 +0800338 // When target = dataIn + structLen + limit,
339 // following target++ will be nullptr
340 if (limit < 1 || target == nullptr)
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800341 {
342 return "";
343 }
344 }
345 target++;
Kuiying Wang59bb9d92020-10-27 15:30:46 +0800346 if (target == nullptr || *target == '\0')
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800347 {
348 return ""; // 0x00 0x00 means end of the entry.
349 }
350 }
351
352 std::string result = target;
353 return result;
354}
Prithvi Paied3d4d12025-05-05 15:23:50 +0530355
356static inline std::string getObjectPath(
357 const std::string& smbiosInventoryPath,
358 [[maybe_unused]] const std::string& motherboardPath,
359 const std::string& suffix, const unsigned int& index)
360{
361 std::string path = smbiosInventoryPath + suffix + std::to_string(index);
362#ifdef CUSTOM_DBUS_PATH
363 if (!motherboardPath.empty() && path.starts_with(defaultMotherboardPath))
364 {
365 path.replace(0, strlen(defaultMotherboardPath), motherboardPath);
366 }
367#endif
368 return path;
369}