blob: d42b84384cd01220be59b7082a5d84582bb0b7ec [file] [log] [blame]
Brandon Kimfcbc3db2022-06-06 21:26:18 -07001#pragma once
2
3#include "pci_handler.hpp"
4
Brandon Kim17ee1a92022-06-07 21:04:08 -07005#include <boost/endian/arithmetic.hpp>
6
Brandon Kimfcbc3db2022-06-06 21:26:18 -07007#include <array>
8#include <cstdint>
9#include <memory>
Brandon Kim17ee1a92022-06-07 21:04:08 -070010#include <tuple>
Brandon Kimfcbc3db2022-06-06 21:26:18 -070011
12namespace bios_bmc_smm_error_logger
13{
14
Brandon Kim17ee1a92022-06-07 21:04:08 -070015/* Adding endianness */
16using boost::endian::little_uint16_t;
17using boost::endian::little_uint32_t;
18using boost::endian::little_uint64_t;
19
Brandon Kim40ce08e2022-06-15 16:58:44 -070020// EntryPair.first = QueueEntryHeader
21// EntryPair.second = Error entry in vector of bytes
22using EntryPair = std::pair<struct QueueEntryHeader, std::vector<uint8_t>>;
23
Brandon Kimfcbc3db2022-06-06 21:26:18 -070024struct CircularBufferHeader
25{
Brandon Kim17ee1a92022-06-07 21:04:08 -070026 little_uint32_t bmcInterfaceVersion; // Offset 0x0
27 little_uint32_t biosInterfaceVersion; // Offset 0x4
28 std::array<little_uint32_t, 4> magicNumber; // Offset 0x8
29 little_uint16_t queueSize; // Offset 0x18
30 little_uint16_t ueRegionSize; // Offset 0x1a
31 little_uint32_t bmcFlags; // Offset 0x1c
32 little_uint16_t bmcReadPtr; // Offset 0x20
33 std::array<uint8_t, 6> reserved1; // Offset 0x22
34 little_uint32_t biosFlags; // Offset 0x28
35 little_uint16_t biosWritePtr; // Offset 0x2c
36 std::array<uint8_t, 2> reserved2; // Offset 0x2e
37 // UE reserved region: Offset 0x30
38 // Error log queue: Offset 0x30 + UE reserved region
Brandon Kimfcbc3db2022-06-06 21:26:18 -070039
40 bool operator==(const CircularBufferHeader& other) const
41 {
Brandon Kim17ee1a92022-06-07 21:04:08 -070042 /* Skip comparing reserved1 and reserved2 */
43 return std::tie(this->bmcInterfaceVersion, this->biosInterfaceVersion,
44 this->magicNumber, this->queueSize, this->ueRegionSize,
45 this->bmcFlags, this->bmcReadPtr, this->biosFlags,
46 this->biosWritePtr) ==
47 std::tie(other.bmcInterfaceVersion, other.biosInterfaceVersion,
48 other.magicNumber, other.queueSize, other.ueRegionSize,
49 other.bmcFlags, other.bmcReadPtr, other.biosFlags,
50 other.biosWritePtr);
Brandon Kimfcbc3db2022-06-06 21:26:18 -070051 }
Brandon Kim17ee1a92022-06-07 21:04:08 -070052};
53static_assert(sizeof(CircularBufferHeader) == 0x30,
54 "Size of CircularBufferHeader struct is incorrect.");
Brandon Kimfcbc3db2022-06-06 21:26:18 -070055
Brandon Kim7bac2d62022-06-07 21:37:51 -070056struct QueueEntryHeader
57{
58 little_uint16_t sequenceId; // Offset 0x0
59 little_uint16_t entrySize; // Offset 0x2
60 uint8_t checksum; // Offset 0x4
61 uint8_t rdeCommandType; // Offset 0x5
62 // RDE Command Offset 0x6
63 bool operator==(const QueueEntryHeader& other) const
64 {
65 return std::tie(this->sequenceId, this->entrySize, this->checksum,
66 this->rdeCommandType) ==
67 std::tie(other.sequenceId, other.entrySize, other.checksum,
68 other.rdeCommandType);
69 }
70};
71static_assert(sizeof(QueueEntryHeader) == 0x6,
72 "Size of QueueEntryHeader struct is incorrect.");
73
Brandon Kimfcbc3db2022-06-06 21:26:18 -070074/**
75 * An interface class for the buffer helper APIs
76 */
77class BufferInterface
78{
79 public:
80 virtual ~BufferInterface() = default;
81
82 /**
83 * Zero out the buffer first before populating the header
84 *
85 * @param[in] bmcInterfaceVersion - Used to initialize the header
86 * @param[in] queueSize - Used to initialize the header
87 * @param[in] ueRegionSize - Used to initialize the header
88 * @param[in] magicNumber - Used to initialize the header
89 * @return true if successful
90 */
91 virtual void initialize(uint32_t bmcInterfaceVersion, uint16_t queueSize,
92 uint16_t ueRegionSize,
93 const std::array<uint32_t, 4>& magicNumber) = 0;
Brandon Kim17ee1a92022-06-07 21:04:08 -070094
95 /**
96 * Read the buffer header from shared buffer
97 */
98 virtual void readBufferHeader() = 0;
99
100 /**
101 * Getter API for the cached buffer header
102 * @return cached CircularBufferHeader
103 */
104 virtual struct CircularBufferHeader getCachedBufferHeader() const = 0;
Brandon Kimcf0b9752022-06-15 10:32:21 -0700105
106 /**
107 * Write to the bufferHeader and update the read pointer
Brandon Kim35d43352022-06-16 11:13:36 -0700108 * @param[in] newReadPtr - read pointer to update to
Brandon Kimcf0b9752022-06-15 10:32:21 -0700109 */
110 virtual void updateReadPtr(const uint32_t newReadPtr) = 0;
Brandon Kim9836cfa2022-06-15 11:21:11 -0700111
112 /**
113 * Wrapper for the dataInterface->read, performs wraparound read
114 *
Brandon Kim35d43352022-06-16 11:13:36 -0700115 * @param[in] relativeOffset - offset relative the "Error Log
116 * Queue region" = (sizeof(CircularBufferHeader) + UE reserved region)
Brandon Kim9836cfa2022-06-15 11:21:11 -0700117 * @param[in] length - bytes to read
Brandon Kim9836cfa2022-06-15 11:21:11 -0700118 * @return the bytes read
119 */
Brandon Kim35d43352022-06-16 11:13:36 -0700120 virtual std::vector<uint8_t> wraparoundRead(const uint32_t relativeOffset,
121 const uint32_t length) = 0;
Brandon Kim7bac2d62022-06-07 21:37:51 -0700122 /**
123 * Read the entry header from shared buffer
124 *
Brandon Kim35d43352022-06-16 11:13:36 -0700125 * @param[in] relativeOffset - offset relative the "Error Log
126 * Queue region" = (sizeof(CircularBufferHeader) + UE reserved region)
Brandon Kim7bac2d62022-06-07 21:37:51 -0700127 * @return the entry header
128 */
Brandon Kim35d43352022-06-16 11:13:36 -0700129 virtual struct QueueEntryHeader readEntryHeader(size_t relativeOffset) = 0;
Brandon Kim40ce08e2022-06-15 16:58:44 -0700130
131 /**
132 * Read the queue entry from the error log queue
133 *
Brandon Kim35d43352022-06-16 11:13:36 -0700134 * @param[in] relativeOffset - offset relative the "Error Log
135 * Queue region" = (sizeof(CircularBufferHeader) + UE reserved region)
136 * * @return entry header and entry pair read from buffer
Brandon Kim40ce08e2022-06-15 16:58:44 -0700137 */
Brandon Kim35d43352022-06-16 11:13:36 -0700138 virtual EntryPair readEntry(size_t relativeOffset) = 0;
Brandon Kimfcbc3db2022-06-06 21:26:18 -0700139};
140
141/**
142 * Buffer implementation class
143 */
144class BufferImpl : public BufferInterface
145{
146 public:
147 /** @brief Constructor for BufferImpl
148 * @param[in] dataInterface - DataInterface for this object
149 */
150 explicit BufferImpl(std::unique_ptr<DataInterface> dataInterface);
151 void initialize(uint32_t bmcInterfaceVersion, uint16_t queueSize,
152 uint16_t ueRegionSize,
153 const std::array<uint32_t, 4>& magicNumber) override;
Brandon Kim17ee1a92022-06-07 21:04:08 -0700154 void readBufferHeader() override;
155 struct CircularBufferHeader getCachedBufferHeader() const override;
Brandon Kimcf0b9752022-06-15 10:32:21 -0700156 void updateReadPtr(const uint32_t newReadPtr) override;
Brandon Kim35d43352022-06-16 11:13:36 -0700157 std::vector<uint8_t> wraparoundRead(const uint32_t relativeOffset,
158 const uint32_t length) override;
159 struct QueueEntryHeader readEntryHeader(size_t relativeOffset) override;
160 EntryPair readEntry(size_t relativeOffset) override;
Brandon Kimfcbc3db2022-06-06 21:26:18 -0700161
162 private:
Brandon Kim9836cfa2022-06-15 11:21:11 -0700163 /** @brief The Error log queue starts after the UE region, which is where
164 * the read and write pointers are offset from relatively
165 * @return relative offset for read and write pointers
166 */
167 size_t getQueueOffset();
Brandon Kimf0e4adc2022-06-16 23:14:25 -0700168 /** @brief Calculate the checksum by XOR each bytes in the span
169 * @param[in] entry - Span to calculate the checksum on
170 * @return calculated checksum
171 */
172 uint8_t calculateChecksum(std::span<uint8_t> entry);
Brandon Kim9836cfa2022-06-15 11:21:11 -0700173
Brandon Kimfcbc3db2022-06-06 21:26:18 -0700174 std::unique_ptr<DataInterface> dataInterface;
Brandon Kim17ee1a92022-06-07 21:04:08 -0700175 struct CircularBufferHeader cachedBufferHeader = {};
Brandon Kimfcbc3db2022-06-06 21:26:18 -0700176};
177
178} // namespace bios_bmc_smm_error_logger