Cheng C Yang | 3e3269a | 2019-12-02 15:11:30 +0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2019 Intel Corporation |
| 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 | |
| 19 | #include <array> |
| 20 | |
| 21 | static constexpr uint16_t smbiosAgentId = 0x0101; |
| 22 | static constexpr int smbiosDirIndex = 0; |
| 23 | static constexpr int firstAgentIndex = 1; |
| 24 | static constexpr uint8_t maxDirEntries = 4; |
| 25 | static constexpr uint32_t pageMask = 0xf000; |
| 26 | static constexpr uint8_t smbiosAgentVersion = 1; |
| 27 | static constexpr uint32_t defaultTimeout = 20000; |
| 28 | static constexpr uint32_t smbiosTableVersion = 15; |
| 29 | static constexpr uint32_t smbiosSMMemoryOffset = 0; |
| 30 | static constexpr uint32_t smbiosSMMemorySize = 1024 * 1024; |
| 31 | static constexpr uint32_t smbiosTableTimestamp = 0x45464748; |
| 32 | static constexpr uint32_t smbiosTableStorageSize = 64 * 1024; |
| 33 | static constexpr const char* smbiosPath = "/var/lib/smbios"; |
Cheng C Yang | eecaf82 | 2019-12-19 00:34:23 +0800 | [diff] [blame] | 34 | static constexpr uint16_t mdrSMBIOSSize = 32 * 1024; |
Cheng C Yang | 3e3269a | 2019-12-02 15:11:30 +0800 | [diff] [blame] | 35 | |
| 36 | enum class DirDataRequestEnum |
| 37 | { |
| 38 | dirDataNotRequested = 0x00, |
| 39 | dirDataRequested = 0x01 |
| 40 | }; |
| 41 | |
| 42 | enum class FlagStatus |
| 43 | { |
| 44 | flagIsInvalid = 0, |
| 45 | flagIsValid = 1, |
| 46 | flagIsLocked = 2 |
| 47 | }; |
| 48 | |
| 49 | typedef enum |
| 50 | { |
| 51 | biosType = 0, |
| 52 | systemType = 1, |
| 53 | baseboardType = 2, |
| 54 | chassisType = 3, |
| 55 | processorsType = 4, |
| 56 | memoryControllerType = 5, |
| 57 | memoryModuleInformationType = 6, |
| 58 | cacheType = 7, |
| 59 | portConnectorType = 8, |
| 60 | systemSlots = 9, |
| 61 | onBoardDevicesType = 10, |
| 62 | oemStringsType = 11, |
| 63 | systemCconfigurationOptionsType = 12, |
| 64 | biosLanguageType = 13, |
| 65 | groupAssociatonsType = 14, |
| 66 | systemEventLogType = 15, |
| 67 | physicalMemoryArrayType = 16, |
| 68 | memoryDeviceType = 17, |
| 69 | } SmbiosType; |
Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [diff] [blame] | 70 | |
| 71 | static constexpr uint8_t separateLen = 2; |
| 72 | |
| 73 | static inline uint8_t* smbiosNextPtr(uint8_t* smbiosDataIn) |
| 74 | { |
| 75 | if (smbiosDataIn == nullptr) |
| 76 | { |
| 77 | return nullptr; |
| 78 | } |
| 79 | uint8_t* smbiosData = smbiosDataIn + *(smbiosDataIn + 1); |
| 80 | int len = 0; |
| 81 | while ((*smbiosData | *(smbiosData + 1)) != 0) |
| 82 | { |
| 83 | smbiosData++; |
| 84 | len++; |
| 85 | if (len >= mdrSMBIOSSize) // To avoid endless loop |
| 86 | { |
| 87 | return nullptr; |
| 88 | } |
| 89 | } |
| 90 | return smbiosData + separateLen; |
| 91 | } |
| 92 | |
| 93 | // When first time run getSMBIOSTypePtr, need to send the RegionS[].regionData |
| 94 | // to smbiosDataIn |
| 95 | static inline uint8_t* getSMBIOSTypePtr(uint8_t* smbiosDataIn, uint8_t typeId) |
| 96 | { |
| 97 | if (smbiosDataIn == nullptr) |
| 98 | { |
| 99 | return nullptr; |
| 100 | } |
| 101 | char* smbiosData = reinterpret_cast<char*>(smbiosDataIn); |
| 102 | while ((*smbiosData != '\0') || (*(smbiosData + 1) != '\0')) |
| 103 | { |
| 104 | if (*smbiosData != typeId) |
| 105 | { |
| 106 | uint32_t len = *(smbiosData + 1); |
| 107 | smbiosData += len; |
| 108 | while ((*smbiosData != '\0') || (*(smbiosData + 1) != '\0')) |
| 109 | { |
| 110 | smbiosData++; |
| 111 | len++; |
| 112 | if (len >= mdrSMBIOSSize) // To avoid endless loop |
| 113 | { |
| 114 | return nullptr; |
| 115 | } |
| 116 | } |
| 117 | smbiosData += separateLen; |
| 118 | continue; |
| 119 | } |
| 120 | return reinterpret_cast<uint8_t*>(smbiosData); |
| 121 | } |
| 122 | return nullptr; |
| 123 | } |
| 124 | |
| 125 | static inline std::string positionToString(uint8_t positionNum, |
| 126 | uint8_t structLen, uint8_t* dataIn) |
| 127 | { |
| 128 | if (dataIn == nullptr) |
| 129 | { |
| 130 | return ""; |
| 131 | } |
| 132 | uint16_t limit = mdrSMBIOSSize; // set a limit to avoid endless loop |
| 133 | |
| 134 | char* target = reinterpret_cast<char*>(dataIn + structLen); |
| 135 | for (uint8_t index = 1; index < positionNum; index++) |
| 136 | { |
| 137 | for (; *target != '\0'; target++) |
| 138 | { |
| 139 | limit--; |
| 140 | if (limit < 1) |
| 141 | { |
| 142 | return ""; |
| 143 | } |
| 144 | } |
| 145 | target++; |
| 146 | if (*target == '\0') |
| 147 | { |
| 148 | return ""; // 0x00 0x00 means end of the entry. |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | std::string result = target; |
| 153 | return result; |
| 154 | } |