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