Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "pci_handler.hpp" |
| 4 | |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 5 | #include <boost/endian/arithmetic.hpp> |
| 6 | |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 7 | #include <array> |
| 8 | #include <cstdint> |
| 9 | #include <memory> |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 10 | #include <tuple> |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 11 | |
| 12 | namespace bios_bmc_smm_error_logger |
| 13 | { |
| 14 | |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 15 | /* Adding endianness */ |
| 16 | using boost::endian::little_uint16_t; |
Brandon Kim | 271d231 | 2022-09-02 16:34:55 +0000 | [diff] [blame] | 17 | using boost::endian::little_uint24_t; |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 18 | using boost::endian::little_uint32_t; |
| 19 | using boost::endian::little_uint64_t; |
| 20 | |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 21 | // EntryPair.first = QueueEntryHeader |
| 22 | // EntryPair.second = Error entry in vector of bytes |
| 23 | using EntryPair = std::pair<struct QueueEntryHeader, std::vector<uint8_t>>; |
| 24 | |
Brandon Kim | aec9986 | 2025-06-08 22:41:40 +0000 | [diff] [blame^] | 25 | enum class BufferFlags : uint32_t |
| 26 | { |
| 27 | ueSwitch = 1 << 0, |
| 28 | overflow = 1 << 1, |
| 29 | }; |
| 30 | |
Brandon Kim | c49284b | 2022-06-17 09:55:26 -0700 | [diff] [blame] | 31 | enum class BmcFlags : uint32_t |
| 32 | { |
Brandon Kim | c49284b | 2022-06-17 09:55:26 -0700 | [diff] [blame] | 33 | ready = 1 << 2, |
| 34 | }; |
| 35 | |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 36 | struct CircularBufferHeader |
| 37 | { |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 38 | little_uint32_t bmcInterfaceVersion; // Offset 0x0 |
| 39 | little_uint32_t biosInterfaceVersion; // Offset 0x4 |
| 40 | std::array<little_uint32_t, 4> magicNumber; // Offset 0x8 |
Brandon Kim | 271d231 | 2022-09-02 16:34:55 +0000 | [diff] [blame] | 41 | little_uint24_t queueSize; // Offset 0x18 |
| 42 | little_uint16_t ueRegionSize; // Offset 0x1b |
| 43 | little_uint32_t bmcFlags; // Offset 0x1d |
| 44 | little_uint24_t bmcReadPtr; // Offset 0x21 |
| 45 | std::array<uint8_t, 4> reserved1; // Offset 0x24 |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 46 | little_uint32_t biosFlags; // Offset 0x28 |
Brandon Kim | 271d231 | 2022-09-02 16:34:55 +0000 | [diff] [blame] | 47 | little_uint24_t biosWritePtr; // Offset 0x2c |
| 48 | uint8_t reserved2; // Offset 0x2f |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 49 | // UE reserved region: Offset 0x30 |
| 50 | // Error log queue: Offset 0x30 + UE reserved region |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 51 | |
| 52 | bool operator==(const CircularBufferHeader& other) const |
| 53 | { |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 54 | /* Skip comparing reserved1 and reserved2 */ |
| 55 | return std::tie(this->bmcInterfaceVersion, this->biosInterfaceVersion, |
| 56 | this->magicNumber, this->queueSize, this->ueRegionSize, |
| 57 | this->bmcFlags, this->bmcReadPtr, this->biosFlags, |
| 58 | this->biosWritePtr) == |
| 59 | std::tie(other.bmcInterfaceVersion, other.biosInterfaceVersion, |
| 60 | other.magicNumber, other.queueSize, other.ueRegionSize, |
| 61 | other.bmcFlags, other.bmcReadPtr, other.biosFlags, |
| 62 | other.biosWritePtr); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 63 | } |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 64 | }; |
| 65 | static_assert(sizeof(CircularBufferHeader) == 0x30, |
| 66 | "Size of CircularBufferHeader struct is incorrect."); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 67 | |
Brandon Kim | 7bac2d6 | 2022-06-07 21:37:51 -0700 | [diff] [blame] | 68 | struct QueueEntryHeader |
| 69 | { |
| 70 | little_uint16_t sequenceId; // Offset 0x0 |
| 71 | little_uint16_t entrySize; // Offset 0x2 |
| 72 | uint8_t checksum; // Offset 0x4 |
| 73 | uint8_t rdeCommandType; // Offset 0x5 |
| 74 | // RDE Command Offset 0x6 |
| 75 | bool operator==(const QueueEntryHeader& other) const |
| 76 | { |
| 77 | return std::tie(this->sequenceId, this->entrySize, this->checksum, |
| 78 | this->rdeCommandType) == |
| 79 | std::tie(other.sequenceId, other.entrySize, other.checksum, |
| 80 | other.rdeCommandType); |
| 81 | } |
| 82 | }; |
| 83 | static_assert(sizeof(QueueEntryHeader) == 0x6, |
| 84 | "Size of QueueEntryHeader struct is incorrect."); |
| 85 | |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 86 | /** |
| 87 | * An interface class for the buffer helper APIs |
| 88 | */ |
| 89 | class BufferInterface |
| 90 | { |
| 91 | public: |
| 92 | virtual ~BufferInterface() = default; |
| 93 | |
| 94 | /** |
| 95 | * Zero out the buffer first before populating the header |
| 96 | * |
| 97 | * @param[in] bmcInterfaceVersion - Used to initialize the header |
| 98 | * @param[in] queueSize - Used to initialize the header |
| 99 | * @param[in] ueRegionSize - Used to initialize the header |
| 100 | * @param[in] magicNumber - Used to initialize the header |
| 101 | * @return true if successful |
| 102 | */ |
| 103 | virtual void initialize(uint32_t bmcInterfaceVersion, uint16_t queueSize, |
| 104 | uint16_t ueRegionSize, |
| 105 | const std::array<uint32_t, 4>& magicNumber) = 0; |
Brandon Kim | aec9986 | 2025-06-08 22:41:40 +0000 | [diff] [blame^] | 106 | /** |
| 107 | * Check for unread Uncorrecatble Error (UE) logs and read them if present |
| 108 | */ |
| 109 | virtual std::vector<uint8_t> readUeLogFromReservedRegion() = 0; |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Read the buffer header from shared buffer |
| 113 | */ |
| 114 | virtual void readBufferHeader() = 0; |
| 115 | |
| 116 | /** |
| 117 | * Getter API for the cached buffer header |
| 118 | * @return cached CircularBufferHeader |
| 119 | */ |
| 120 | virtual struct CircularBufferHeader getCachedBufferHeader() const = 0; |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 121 | |
| 122 | /** |
| 123 | * Write to the bufferHeader and update the read pointer |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 124 | * @param[in] newReadPtr - read pointer to update to |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 125 | */ |
| 126 | virtual void updateReadPtr(const uint32_t newReadPtr) = 0; |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 127 | |
| 128 | /** |
Brandon Kim | c49284b | 2022-06-17 09:55:26 -0700 | [diff] [blame] | 129 | * Write to the bufferHeader and update the BMC flags |
| 130 | * @param[in] newBmcFlags - new flag to update to |
| 131 | */ |
| 132 | virtual void updateBmcFlags(const uint32_t newBmcFlags) = 0; |
| 133 | |
| 134 | /** |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 135 | * Wrapper for the dataInterface->read, performs wraparound read |
| 136 | * |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 137 | * @param[in] relativeOffset - offset relative the "Error Log |
| 138 | * Queue region" = (sizeof(CircularBufferHeader) + UE reserved region) |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 139 | * @param[in] length - bytes to read |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 140 | * @return the bytes read |
| 141 | */ |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 142 | virtual std::vector<uint8_t> wraparoundRead(const uint32_t relativeOffset, |
| 143 | const uint32_t length) = 0; |
Brandon Kim | 7bac2d6 | 2022-06-07 21:37:51 -0700 | [diff] [blame] | 144 | /** |
Brandon Kim | 613ba53 | 2022-09-12 20:55:21 +0000 | [diff] [blame] | 145 | * Read the entry header from shared buffer from the read pointer |
Brandon Kim | 7bac2d6 | 2022-06-07 21:37:51 -0700 | [diff] [blame] | 146 | * |
Brandon Kim | 7bac2d6 | 2022-06-07 21:37:51 -0700 | [diff] [blame] | 147 | * @return the entry header |
| 148 | */ |
Brandon Kim | 613ba53 | 2022-09-12 20:55:21 +0000 | [diff] [blame] | 149 | virtual struct QueueEntryHeader readEntryHeader() = 0; |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 150 | |
| 151 | /** |
Brandon Kim | 613ba53 | 2022-09-12 20:55:21 +0000 | [diff] [blame] | 152 | * Read the queue entry from the error log queue from the read pointer |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 153 | * |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 154 | * * @return entry header and entry pair read from buffer |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 155 | */ |
Brandon Kim | 613ba53 | 2022-09-12 20:55:21 +0000 | [diff] [blame] | 156 | virtual EntryPair readEntry() = 0; |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 157 | |
| 158 | /** |
| 159 | * Read the buffer - this API should be used instead of individual functions |
| 160 | * above |
| 161 | * |
| 162 | * @return vector of EntryPair which consists of entry header and entry |
| 163 | */ |
| 164 | virtual std::vector<EntryPair> readErrorLogs() = 0; |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * Get max offset for the queue |
| 168 | * |
| 169 | * * @return Queue size - UE region size - Queue header size |
| 170 | */ |
| 171 | virtual size_t getMaxOffset() = 0; |
Brandon Kim | 82ab832 | 2023-08-17 00:50:18 +0000 | [diff] [blame] | 172 | |
| 173 | /** @brief The Error log queue starts after the UE region, which is where |
| 174 | * the read and write pointers are offset from relatively |
| 175 | * @return relative offset for read and write pointers |
| 176 | */ |
| 177 | virtual size_t getQueueOffset() = 0; |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | /** |
| 181 | * Buffer implementation class |
| 182 | */ |
| 183 | class BufferImpl : public BufferInterface |
| 184 | { |
| 185 | public: |
| 186 | /** @brief Constructor for BufferImpl |
| 187 | * @param[in] dataInterface - DataInterface for this object |
| 188 | */ |
| 189 | explicit BufferImpl(std::unique_ptr<DataInterface> dataInterface); |
| 190 | void initialize(uint32_t bmcInterfaceVersion, uint16_t queueSize, |
| 191 | uint16_t ueRegionSize, |
| 192 | const std::array<uint32_t, 4>& magicNumber) override; |
Brandon Kim | aec9986 | 2025-06-08 22:41:40 +0000 | [diff] [blame^] | 193 | std::vector<uint8_t> readUeLogFromReservedRegion() override; |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 194 | void readBufferHeader() override; |
| 195 | struct CircularBufferHeader getCachedBufferHeader() const override; |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 196 | void updateReadPtr(const uint32_t newReadPtr) override; |
Brandon Kim | c49284b | 2022-06-17 09:55:26 -0700 | [diff] [blame] | 197 | void updateBmcFlags(const uint32_t newBmcFlag) override; |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 198 | std::vector<uint8_t> wraparoundRead(const uint32_t relativeOffset, |
| 199 | const uint32_t length) override; |
Brandon Kim | 613ba53 | 2022-09-12 20:55:21 +0000 | [diff] [blame] | 200 | struct QueueEntryHeader readEntryHeader() override; |
| 201 | EntryPair readEntry() override; |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 202 | std::vector<EntryPair> readErrorLogs() override; |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 203 | size_t getMaxOffset() override; |
Brandon Kim | 82ab832 | 2023-08-17 00:50:18 +0000 | [diff] [blame] | 204 | size_t getQueueOffset() override; |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 205 | |
| 206 | private: |
Brandon Kim | f0e4adc | 2022-06-16 23:14:25 -0700 | [diff] [blame] | 207 | /** @brief Calculate the checksum by XOR each bytes in the span |
| 208 | * @param[in] entry - Span to calculate the checksum on |
| 209 | * @return calculated checksum |
| 210 | */ |
| 211 | uint8_t calculateChecksum(std::span<uint8_t> entry); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 212 | |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 213 | std::unique_ptr<DataInterface> dataInterface; |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 214 | struct CircularBufferHeader cachedBufferHeader = {}; |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | } // namespace bios_bmc_smm_error_logger |