Brandon Kim | 82ab832 | 2023-08-17 00:50:18 +0000 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 3 | #include "buffer.hpp" |
| 4 | |
| 5 | #include "pci_handler.hpp" |
| 6 | |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 7 | #include <boost/endian/arithmetic.hpp> |
| 8 | #include <boost/endian/conversion.hpp> |
| 9 | |
| 10 | #include <algorithm> |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 11 | #include <array> |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 12 | #include <cstddef> |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 13 | #include <cstdint> |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame^] | 14 | #include <format> |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 15 | #include <memory> |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 16 | #include <numeric> |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 17 | #include <span> |
| 18 | #include <vector> |
| 19 | |
| 20 | namespace bios_bmc_smm_error_logger |
| 21 | { |
| 22 | |
| 23 | BufferImpl::BufferImpl(std::unique_ptr<DataInterface> dataInterface) : |
| 24 | dataInterface(std::move(dataInterface)){}; |
| 25 | |
| 26 | void BufferImpl::initialize(uint32_t bmcInterfaceVersion, uint16_t queueSize, |
| 27 | uint16_t ueRegionSize, |
| 28 | const std::array<uint32_t, 4>& magicNumber) |
| 29 | { |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 30 | const size_t memoryRegionSize = dataInterface->getMemoryRegionSize(); |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 31 | if (queueSize > memoryRegionSize) |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 32 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame^] | 33 | throw std::runtime_error(std::format( |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 34 | "[initialize] Proposed queue size '{}' is bigger than the " |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 35 | "BMC's allocated MMIO region of '{}'", |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 36 | queueSize, memoryRegionSize)); |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // Initialize the whole buffer with 0x00 |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 40 | const std::vector<uint8_t> emptyVector(queueSize, 0); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 41 | size_t byteWritten = dataInterface->write(0, emptyVector); |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 42 | if (byteWritten != queueSize) |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 43 | { |
| 44 | throw std::runtime_error( |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame^] | 45 | std::format("[initialize] Only erased '{}'", byteWritten)); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // Create an initial buffer header and write to it |
| 49 | struct CircularBufferHeader initializationHeader = {}; |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 50 | initializationHeader.bmcInterfaceVersion = |
| 51 | boost::endian::native_to_little(bmcInterfaceVersion); |
| 52 | initializationHeader.queueSize = boost::endian::native_to_little(queueSize); |
| 53 | initializationHeader.ueRegionSize = |
| 54 | boost::endian::native_to_little(ueRegionSize); |
| 55 | std::transform(magicNumber.begin(), magicNumber.end(), |
| 56 | initializationHeader.magicNumber.begin(), |
| 57 | [](uint32_t number) -> little_uint32_t { |
Patrick Williams | 589c175 | 2023-10-20 11:20:01 -0500 | [diff] [blame] | 58 | return boost::endian::native_to_little(number); |
| 59 | }); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 60 | |
| 61 | uint8_t* initializationHeaderPtr = |
| 62 | reinterpret_cast<uint8_t*>(&initializationHeader); |
| 63 | size_t initializationHeaderSize = sizeof(initializationHeader); |
| 64 | byteWritten = dataInterface->write( |
| 65 | 0, std::span<const uint8_t>(initializationHeaderPtr, |
| 66 | initializationHeaderPtr + |
| 67 | initializationHeaderSize)); |
| 68 | if (byteWritten != initializationHeaderSize) |
| 69 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame^] | 70 | throw std::runtime_error(std::format( |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 71 | "[initialize] Only wrote '{}' bytes of the header", byteWritten)); |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 72 | } |
Brandon Kim | 60cab57 | 2022-06-15 14:20:05 -0700 | [diff] [blame] | 73 | cachedBufferHeader = initializationHeader; |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 76 | void BufferImpl::readBufferHeader() |
| 77 | { |
| 78 | size_t headerSize = sizeof(struct CircularBufferHeader); |
Patrick Williams | b812576 | 2023-05-10 07:51:26 -0500 | [diff] [blame] | 79 | std::vector<uint8_t> bytesRead = dataInterface->read(/*offset=*/0, |
| 80 | headerSize); |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 81 | |
| 82 | if (bytesRead.size() != headerSize) |
| 83 | { |
| 84 | throw std::runtime_error( |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame^] | 85 | std::format("Buffer header read only read '{}', expected '{}'", |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 86 | bytesRead.size(), headerSize)); |
| 87 | } |
| 88 | |
| 89 | cachedBufferHeader = |
Brandon Kim | 60cab57 | 2022-06-15 14:20:05 -0700 | [diff] [blame] | 90 | *reinterpret_cast<struct CircularBufferHeader*>(bytesRead.data()); |
Brandon Kim | 17ee1a9 | 2022-06-07 21:04:08 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | struct CircularBufferHeader BufferImpl::getCachedBufferHeader() const |
| 94 | { |
| 95 | return cachedBufferHeader; |
| 96 | } |
| 97 | |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 98 | void BufferImpl::updateReadPtr(const uint32_t newReadPtr) |
| 99 | { |
Patrick Williams | b812576 | 2023-05-10 07:51:26 -0500 | [diff] [blame] | 100 | constexpr uint8_t bmcReadPtrOffset = offsetof(struct CircularBufferHeader, |
| 101 | bmcReadPtr); |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 102 | |
Brandon Kim | 271d231 | 2022-09-02 16:34:55 +0000 | [diff] [blame] | 103 | little_uint24_t truncatedReadPtr = |
| 104 | boost::endian::native_to_little(newReadPtr & 0xffffff); |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 105 | uint8_t* truncatedReadPtrPtr = |
| 106 | reinterpret_cast<uint8_t*>(&truncatedReadPtr); |
| 107 | |
| 108 | size_t writtenSize = dataInterface->write( |
| 109 | bmcReadPtrOffset, std::span<const uint8_t>{ |
| 110 | truncatedReadPtrPtr, |
Brandon Kim | 271d231 | 2022-09-02 16:34:55 +0000 | [diff] [blame] | 111 | truncatedReadPtrPtr + sizeof(truncatedReadPtr)}); |
| 112 | if (writtenSize != sizeof(truncatedReadPtr)) |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 113 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame^] | 114 | throw std::runtime_error(std::format( |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 115 | "[updateReadPtr] Wrote '{}' bytes, instead of expected '{}'", |
Brandon Kim | 271d231 | 2022-09-02 16:34:55 +0000 | [diff] [blame] | 116 | writtenSize, sizeof(truncatedReadPtr))); |
Brandon Kim | cf0b975 | 2022-06-15 10:32:21 -0700 | [diff] [blame] | 117 | } |
| 118 | cachedBufferHeader.bmcReadPtr = truncatedReadPtr; |
| 119 | } |
| 120 | |
Brandon Kim | c49284b | 2022-06-17 09:55:26 -0700 | [diff] [blame] | 121 | void BufferImpl::updateBmcFlags(const uint32_t newBmcFlag) |
| 122 | { |
Patrick Williams | b812576 | 2023-05-10 07:51:26 -0500 | [diff] [blame] | 123 | constexpr uint8_t bmcFlagsPtrOffset = offsetof(struct CircularBufferHeader, |
| 124 | bmcFlags); |
Brandon Kim | c49284b | 2022-06-17 09:55:26 -0700 | [diff] [blame] | 125 | |
| 126 | little_uint32_t littleNewBmcFlag = |
| 127 | boost::endian::native_to_little(newBmcFlag); |
| 128 | uint8_t* littleNewBmcFlagPtr = |
| 129 | reinterpret_cast<uint8_t*>(&littleNewBmcFlag); |
| 130 | |
| 131 | size_t writtenSize = dataInterface->write( |
| 132 | bmcFlagsPtrOffset, std::span<const uint8_t>{ |
| 133 | littleNewBmcFlagPtr, |
| 134 | littleNewBmcFlagPtr + sizeof(little_uint32_t)}); |
| 135 | if (writtenSize != sizeof(little_uint32_t)) |
| 136 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame^] | 137 | throw std::runtime_error(std::format( |
Brandon Kim | c49284b | 2022-06-17 09:55:26 -0700 | [diff] [blame] | 138 | "[updateBmcFlags] Wrote '{}' bytes, instead of expected '{}'", |
| 139 | writtenSize, sizeof(little_uint32_t))); |
| 140 | } |
| 141 | cachedBufferHeader.bmcFlags = littleNewBmcFlag; |
| 142 | } |
| 143 | |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 144 | std::vector<uint8_t> BufferImpl::wraparoundRead(const uint32_t relativeOffset, |
| 145 | const uint32_t length) |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 146 | { |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 147 | const size_t maxOffset = getMaxOffset(); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 148 | |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 149 | if (relativeOffset > maxOffset) |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 150 | { |
| 151 | throw std::runtime_error( |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame^] | 152 | std::format("[wraparoundRead] relativeOffset '{}' was bigger " |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 153 | "than maxOffset '{}'", |
| 154 | relativeOffset, maxOffset)); |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 155 | } |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 156 | if (length > maxOffset) |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 157 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame^] | 158 | throw std::runtime_error(std::format( |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 159 | "[wraparoundRead] length '{}' was bigger than maxOffset '{}'", |
| 160 | length, maxOffset)); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 163 | // Do a calculation to see if the read will wraparound |
| 164 | const size_t queueOffset = getQueueOffset(); |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 165 | const size_t writableSpace = maxOffset - relativeOffset; |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 166 | size_t numWraparoundBytesToRead = 0; |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 167 | if (length > writableSpace) |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 168 | { |
| 169 | // This means we will wrap, count the bytes that are left to read |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 170 | numWraparoundBytesToRead = length - writableSpace; |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 171 | } |
| 172 | const size_t numBytesToReadTillQueueEnd = length - numWraparoundBytesToRead; |
| 173 | |
| 174 | std::vector<uint8_t> bytesRead = dataInterface->read( |
| 175 | queueOffset + relativeOffset, numBytesToReadTillQueueEnd); |
| 176 | if (bytesRead.size() != numBytesToReadTillQueueEnd) |
| 177 | { |
| 178 | throw std::runtime_error( |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame^] | 179 | std::format("[wraparoundRead] Read '{}' which was not " |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 180 | "the requested length of '{}'", |
| 181 | bytesRead.size(), numBytesToReadTillQueueEnd)); |
| 182 | } |
| 183 | size_t updatedReadPtr = relativeOffset + numBytesToReadTillQueueEnd; |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 184 | if (updatedReadPtr == maxOffset) |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 185 | { |
| 186 | // If we read all the way up to the end of the queue, we need to |
| 187 | // manually wrap the updateReadPtr around to 0 |
| 188 | updatedReadPtr = 0; |
| 189 | } |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 190 | |
| 191 | // If there are any more bytes to be read beyond the buffer, wrap around and |
| 192 | // read from the beginning of the buffer (offset by the queueOffset) |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 193 | if (numWraparoundBytesToRead > 0) |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 194 | { |
| 195 | std::vector<uint8_t> wrappedBytesRead = |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 196 | dataInterface->read(queueOffset, numWraparoundBytesToRead); |
| 197 | if (numWraparoundBytesToRead != wrappedBytesRead.size()) |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 198 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame^] | 199 | throw std::runtime_error(std::format( |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 200 | "[wraparoundRead] Buffer wrapped around but read '{}' which " |
| 201 | "was not the requested lenght of '{}'", |
| 202 | wrappedBytesRead.size(), numWraparoundBytesToRead)); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 203 | } |
| 204 | bytesRead.insert(bytesRead.end(), wrappedBytesRead.begin(), |
| 205 | wrappedBytesRead.end()); |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 206 | updatedReadPtr = numWraparoundBytesToRead; |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 207 | } |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 208 | updateReadPtr(updatedReadPtr); |
Brandon Kim | 9836cfa | 2022-06-15 11:21:11 -0700 | [diff] [blame] | 209 | |
| 210 | return bytesRead; |
| 211 | } |
| 212 | |
Brandon Kim | 613ba53 | 2022-09-12 20:55:21 +0000 | [diff] [blame] | 213 | struct QueueEntryHeader BufferImpl::readEntryHeader() |
Brandon Kim | 7bac2d6 | 2022-06-07 21:37:51 -0700 | [diff] [blame] | 214 | { |
| 215 | size_t headerSize = sizeof(struct QueueEntryHeader); |
| 216 | // wraparonudRead will throw if it did not read all the bytes, let it |
| 217 | // propagate up the stack |
Brandon Kim | 613ba53 | 2022-09-12 20:55:21 +0000 | [diff] [blame] | 218 | std::vector<uint8_t> bytesRead = wraparoundRead( |
| 219 | boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr), |
| 220 | headerSize); |
Brandon Kim | 7bac2d6 | 2022-06-07 21:37:51 -0700 | [diff] [blame] | 221 | |
| 222 | return *reinterpret_cast<struct QueueEntryHeader*>(bytesRead.data()); |
| 223 | } |
| 224 | |
Brandon Kim | 613ba53 | 2022-09-12 20:55:21 +0000 | [diff] [blame] | 225 | EntryPair BufferImpl::readEntry() |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 226 | { |
Brandon Kim | 613ba53 | 2022-09-12 20:55:21 +0000 | [diff] [blame] | 227 | struct QueueEntryHeader entryHeader = readEntryHeader(); |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 228 | size_t entrySize = boost::endian::little_to_native(entryHeader.entrySize); |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 229 | |
| 230 | // wraparonudRead may throw if entrySize was bigger than the buffer or if it |
Brandon Kim | 26660e9 | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 231 | // was not able to read all the bytes, let it propagate up the stack |
Brandon Kim | 35d4335 | 2022-06-16 11:13:36 -0700 | [diff] [blame] | 232 | std::vector<uint8_t> entry = wraparoundRead( |
Brandon Kim | 613ba53 | 2022-09-12 20:55:21 +0000 | [diff] [blame] | 233 | boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr), |
| 234 | entrySize); |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 235 | |
| 236 | // Calculate the checksum |
| 237 | uint8_t* entryHeaderPtr = reinterpret_cast<uint8_t*>(&entryHeader); |
Brandon Kim | f0e4adc | 2022-06-16 23:14:25 -0700 | [diff] [blame] | 238 | uint8_t checksum = |
| 239 | std::accumulate(entryHeaderPtr, |
| 240 | entryHeaderPtr + sizeof(struct QueueEntryHeader), 0, |
| 241 | std::bit_xor<void>()) ^ |
| 242 | std::accumulate(entry.begin(), entry.end(), 0, std::bit_xor<void>()); |
| 243 | |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 244 | if (checksum != 0) |
| 245 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame^] | 246 | throw std::runtime_error(std::format( |
Brandon Kim | 40ce08e | 2022-06-15 16:58:44 -0700 | [diff] [blame] | 247 | "[readEntry] Checksum was '{}', expected '0'", checksum)); |
| 248 | } |
| 249 | |
| 250 | return {entryHeader, entry}; |
| 251 | } |
| 252 | |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 253 | std::vector<EntryPair> BufferImpl::readErrorLogs() |
| 254 | { |
| 255 | // Reading the buffer header will update the cachedBufferHeader |
| 256 | readBufferHeader(); |
| 257 | |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 258 | const size_t maxOffset = getMaxOffset(); |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 259 | size_t currentBiosWritePtr = |
| 260 | boost::endian::little_to_native(cachedBufferHeader.biosWritePtr); |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 261 | if (currentBiosWritePtr > maxOffset) |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 262 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame^] | 263 | throw std::runtime_error(std::format( |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 264 | "[readErrorLogs] currentBiosWritePtr was '{}' which was bigger " |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 265 | "than maxOffset '{}'", |
| 266 | currentBiosWritePtr, maxOffset)); |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 267 | } |
| 268 | size_t currentReadPtr = |
| 269 | boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr); |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 270 | if (currentReadPtr > maxOffset) |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 271 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame^] | 272 | throw std::runtime_error(std::format( |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 273 | "[readErrorLogs] currentReadPtr was '{}' which was bigger " |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 274 | "than maxOffset '{}'", |
| 275 | currentReadPtr, maxOffset)); |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | size_t bytesToRead; |
| 279 | if (currentBiosWritePtr == currentReadPtr) |
| 280 | { |
| 281 | // No new payload was detected, return an empty vector gracefully |
| 282 | return {}; |
| 283 | } |
| 284 | |
| 285 | if (currentBiosWritePtr > currentReadPtr) |
| 286 | { |
| 287 | // Simply subtract in this case |
| 288 | bytesToRead = currentBiosWritePtr - currentReadPtr; |
| 289 | } |
| 290 | else |
| 291 | { |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 292 | // Calculate the bytes to the "end" (maxOffset - ReadPtr) + |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 293 | // bytes to read from the "beginning" (0 + WritePtr) |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 294 | bytesToRead = (maxOffset - currentReadPtr) + currentBiosWritePtr; |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | size_t byteRead = 0; |
| 298 | std::vector<EntryPair> entryPairs; |
| 299 | while (byteRead < bytesToRead) |
| 300 | { |
Brandon Kim | 613ba53 | 2022-09-12 20:55:21 +0000 | [diff] [blame] | 301 | EntryPair entryPair = readEntry(); |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 302 | byteRead += sizeof(struct QueueEntryHeader) + entryPair.second.size(); |
| 303 | entryPairs.push_back(entryPair); |
| 304 | |
| 305 | // Note: readEntry() will update cachedBufferHeader.bmcReadPtr |
| 306 | currentReadPtr = |
| 307 | boost::endian::little_to_native(cachedBufferHeader.bmcReadPtr); |
| 308 | } |
| 309 | if (currentBiosWritePtr != currentReadPtr) |
| 310 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame^] | 311 | throw std::runtime_error(std::format( |
Brandon Kim | 4662b1b | 2022-06-16 21:38:02 -0700 | [diff] [blame] | 312 | "[readErrorLogs] biosWritePtr '{}' and bmcReaddPtr '{}' " |
| 313 | "are not identical after reading through all the logs", |
| 314 | currentBiosWritePtr, currentReadPtr)); |
| 315 | } |
| 316 | return entryPairs; |
| 317 | } |
| 318 | |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 319 | size_t BufferImpl::getMaxOffset() |
| 320 | { |
| 321 | size_t queueSize = |
| 322 | boost::endian::little_to_native(cachedBufferHeader.queueSize); |
| 323 | size_t ueRegionSize = |
| 324 | boost::endian::little_to_native(cachedBufferHeader.ueRegionSize); |
| 325 | |
Brandon Kim | 82ab832 | 2023-08-17 00:50:18 +0000 | [diff] [blame] | 326 | if (queueSize != QUEUE_REGION_SIZE) |
| 327 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame^] | 328 | throw std::runtime_error(std::format( |
Brandon Kim | 82ab832 | 2023-08-17 00:50:18 +0000 | [diff] [blame] | 329 | "[{}] runtime queueSize '{}' did not match compile-time queueSize " |
| 330 | "'{}'. This indicates that the buffer was corrupted", |
| 331 | __FUNCTION__, queueSize, QUEUE_REGION_SIZE)); |
| 332 | } |
| 333 | if (ueRegionSize != UE_REGION_SIZE) |
| 334 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame^] | 335 | throw std::runtime_error(std::format( |
Brandon Kim | 82ab832 | 2023-08-17 00:50:18 +0000 | [diff] [blame] | 336 | "[{}] runtime ueRegionSize '{}' did not match compile-time " |
| 337 | "ueRegionSize '{}'. This indicates that the buffer was corrupted", |
| 338 | __FUNCTION__, ueRegionSize, UE_REGION_SIZE)); |
| 339 | } |
| 340 | |
Brandon Kim | 3def3c8 | 2022-09-13 05:29:20 +0000 | [diff] [blame] | 341 | return queueSize - ueRegionSize - sizeof(struct CircularBufferHeader); |
| 342 | } |
| 343 | |
Brandon Kim | 82ab832 | 2023-08-17 00:50:18 +0000 | [diff] [blame] | 344 | size_t BufferImpl::getQueueOffset() |
| 345 | { |
| 346 | size_t ueRegionSize = |
| 347 | boost::endian::little_to_native(cachedBufferHeader.ueRegionSize); |
| 348 | |
| 349 | if (ueRegionSize != UE_REGION_SIZE) |
| 350 | { |
Patrick Williams | 5de9061 | 2024-02-13 21:31:53 -0600 | [diff] [blame^] | 351 | throw std::runtime_error(std::format( |
Brandon Kim | 82ab832 | 2023-08-17 00:50:18 +0000 | [diff] [blame] | 352 | "[{}] runtime ueRegionSize '{}' did not match compile-time " |
| 353 | "ueRegionSize '{}'. This indicates that the buffer was corrupted", |
| 354 | __FUNCTION__, ueRegionSize, UE_REGION_SIZE)); |
| 355 | } |
| 356 | return sizeof(struct CircularBufferHeader) + ueRegionSize; |
| 357 | } |
| 358 | |
Brandon Kim | fcbc3db | 2022-06-06 21:26:18 -0700 | [diff] [blame] | 359 | } // namespace bios_bmc_smm_error_logger |