blob: 1752ad988de8053eafc7546a0f628c7d913195ab [file] [log] [blame]
kasunathbac958d2022-06-07 18:15:24 -07001#include "rde/rde_handler.hpp"
2
Patrick Williams5de90612024-02-13 21:31:53 -06003#include <stdplus/print.hpp>
kasunathbac958d2022-06-07 18:15:24 -07004
Patrick Williams5de90612024-02-13 21:31:53 -06005#include <format>
kasunathbac958d2022-06-07 18:15:24 -07006#include <iostream>
7
8namespace bios_bmc_smm_error_logger
9{
10namespace rde
11{
12
13/**
14 * @brief CRC-32 divisor.
15 *
16 * This is equivalent to the one used by IEEE802.3.
17 */
18constexpr uint32_t crcDevisor = 0xedb88320;
19
20RdeCommandHandler::RdeCommandHandler(
21 std::unique_ptr<ExternalStorerInterface> exStorer) :
22 flagState(RdeDictTransferFlagState::RdeStateIdle),
Brandon Kim90ccfe82025-06-06 20:38:56 +000023 exStorer(std::move(exStorer)), prevDictResourceId(0), crc(0xFFFFFFFF)
kasunathbac958d2022-06-07 18:15:24 -070024{
25 // Initialize CRC table.
26 calcCrcTable();
27}
28
Patrick Williams1a643562024-08-16 15:22:05 -040029RdeDecodeStatus RdeCommandHandler::decodeRdeCommand(
30 std::span<const uint8_t> rdeCommand, RdeCommandType type)
kasunathbac958d2022-06-07 18:15:24 -070031{
32 if (type == RdeCommandType::RdeMultiPartReceiveResponse)
33 {
34 return multiPartReceiveResp(rdeCommand);
35 }
36 if (type == RdeCommandType::RdeOperationInitRequest)
37 {
38 return operationInitRequest(rdeCommand);
39 }
40
Patrick Williams5de90612024-02-13 21:31:53 -060041 stdplus::print(stderr, "Invalid command type\n");
kasunathbac958d2022-06-07 18:15:24 -070042 return RdeDecodeStatus::RdeInvalidCommand;
43}
44
45uint32_t RdeCommandHandler::getDictionaryCount()
46{
47 return dictionaryManager.getDictionaryCount();
48}
49
Patrick Williams99cd49a2025-03-03 11:20:29 -050050RdeDecodeStatus RdeCommandHandler::operationInitRequest(
51 std::span<const uint8_t> rdeCommand)
kasunathbac958d2022-06-07 18:15:24 -070052{
Brandon Kim8540c7d2025-06-08 23:54:24 +000053 // Ensure rdeCommand is large enough for the header.
54 if (rdeCommand.size() < sizeof(RdeOperationInitReqHeader))
55 {
56 stdplus::print(
57 stderr,
58 "RDE OperationInitRequest command is smaller than the expected header size. Received: {}, Expected: {}\n",
59 rdeCommand.size(), sizeof(RdeOperationInitReqHeader));
60 return RdeDecodeStatus::RdeInvalidCommand;
61 }
62
kasunathbac958d2022-06-07 18:15:24 -070063 const RdeOperationInitReqHeader* header =
64 reinterpret_cast<const RdeOperationInitReqHeader*>(rdeCommand.data());
Brandon Kim8540c7d2025-06-08 23:54:24 +000065
kasunathbac958d2022-06-07 18:15:24 -070066 // Check if there is a payload. If not, we are not doing anything.
67 if (!header->containsRequestPayload)
68 {
69 return RdeDecodeStatus::RdeOk;
70 }
71
Brandon Kim8540c7d2025-06-08 23:54:24 +000072 // Ensure rdeCommand is large enough for header + locator + declared
73 // payload.
74 size_t expectedTotalSize =
75 sizeof(RdeOperationInitReqHeader) + header->operationLocatorLength +
76 header->requestPayloadLength;
77 if (rdeCommand.size() < expectedTotalSize)
78 {
79 stdplus::print(
80 stderr,
81 "RDE OperationInitRequest command size is smaller than header + locator + declared payload size. Received: {}, Expected: {}\n",
82 rdeCommand.size(), expectedTotalSize);
83 return RdeDecodeStatus::RdeInvalidCommand;
84 }
85
kasunathaedea9f2022-12-05 11:17:26 -080086 if (header->operationType !=
87 static_cast<uint8_t>(RdeOperationInitType::RdeOpInitOperationUpdate))
kasunathbac958d2022-06-07 18:15:24 -070088 {
Patrick Williams5de90612024-02-13 21:31:53 -060089 stdplus::print(stderr, "Operation not supported\n");
kasunathbac958d2022-06-07 18:15:24 -070090 return RdeDecodeStatus::RdeUnsupportedOperation;
91 }
92
93 // OperationInit payload overflows are not suported.
94 if (header->sendDataTransferHandle != 0)
95 {
Patrick Williams5de90612024-02-13 21:31:53 -060096 stdplus::print(stderr, "Payload should fit in within the request\n");
kasunathbac958d2022-06-07 18:15:24 -070097 return RdeDecodeStatus::RdePayloadOverflow;
98 }
99
100 auto schemaDictOrErr = dictionaryManager.getDictionary(header->resourceID);
101 if (!schemaDictOrErr)
102 {
Patrick Williams5de90612024-02-13 21:31:53 -0600103 stdplus::print(stderr,
104 "Schema Dictionary not found for resourceId: {}\n",
105 header->resourceID);
kasunathbac958d2022-06-07 18:15:24 -0700106 return RdeDecodeStatus::RdeNoDictionary;
107 }
108
109 auto annotationDictOrErr = dictionaryManager.getAnnotationDictionary();
110 if (!annotationDictOrErr)
111 {
Patrick Williams5de90612024-02-13 21:31:53 -0600112 stdplus::print(stderr, "Annotation dictionary not found\n");
kasunathbac958d2022-06-07 18:15:24 -0700113 return RdeDecodeStatus::RdeNoDictionary;
114 }
115
116 BejDictionaries dictionaries = {
117 .schemaDictionary = (*schemaDictOrErr).data(),
Kasun Athukoralae3410a82025-07-16 00:10:23 +0000118 .schemaDictionarySize = (uint32_t)(*schemaDictOrErr).size_bytes(),
kasunathbac958d2022-06-07 18:15:24 -0700119 .annotationDictionary = (*annotationDictOrErr).data(),
Kasun Athukoralae3410a82025-07-16 00:10:23 +0000120 .annotationDictionarySize =
121 (uint32_t)(*annotationDictOrErr).size_bytes(),
kasunathbac958d2022-06-07 18:15:24 -0700122 // We do not use the error dictionary.
123 .errorDictionary = nullptr,
Kasun Athukoralae3410a82025-07-16 00:10:23 +0000124 .errorDictionarySize = 0,
kasunathbac958d2022-06-07 18:15:24 -0700125 };
126
127 // Soon after header, we have bejLocator field. Then we have the encoded
128 // data.
Patrick Williams1a643562024-08-16 15:22:05 -0400129 const uint8_t* encodedPldmBlock =
130 rdeCommand.data() + sizeof(RdeOperationInitReqHeader) +
131 header->operationLocatorLength;
kasunathbac958d2022-06-07 18:15:24 -0700132
133 // Decoded the data.
134 if (decoder.decode(dictionaries, std::span(encodedPldmBlock,
135 header->requestPayloadLength)) !=
136 0)
137 {
Patrick Williams5de90612024-02-13 21:31:53 -0600138 stdplus::print(stderr, "BEJ decoding failed.\n");
kasunathbac958d2022-06-07 18:15:24 -0700139 return RdeDecodeStatus::RdeBejDecodingError;
140 }
141
142 // Post the output.
143 if (!exStorer->publishJson(decoder.getOutput()))
144 {
Patrick Williams5de90612024-02-13 21:31:53 -0600145 stdplus::print(stderr, "Failed to write to ExternalStorer.\n");
kasunathbac958d2022-06-07 18:15:24 -0700146 return RdeDecodeStatus::RdeExternalStorerError;
147 }
148 return RdeDecodeStatus::RdeOk;
149}
150
Patrick Williams99cd49a2025-03-03 11:20:29 -0500151RdeDecodeStatus RdeCommandHandler::multiPartReceiveResp(
152 std::span<const uint8_t> rdeCommand)
kasunathbac958d2022-06-07 18:15:24 -0700153{
Brandon Kim90ccfe82025-06-06 20:38:56 +0000154 if (rdeCommand.size() < sizeof(MultipartReceiveResHeader))
155 {
156 stdplus::print(
157 stderr, "RDE command is smaller than the expected header size.\n");
158 return RdeDecodeStatus::RdeInvalidCommand;
159 }
160
kasunathbac958d2022-06-07 18:15:24 -0700161 const MultipartReceiveResHeader* header =
162 reinterpret_cast<const MultipartReceiveResHeader*>(rdeCommand.data());
163
Brandon Kim90ccfe82025-06-06 20:38:56 +0000164 if (rdeCommand.size() <
165 sizeof(MultipartReceiveResHeader) + header->dataLengthBytes)
166 {
167 stdplus::print(
168 stderr,
169 "RDE command size is smaller than header + declared payload size.\n");
170 return RdeDecodeStatus::RdeInvalidCommand;
171 }
172
kasunathbac958d2022-06-07 18:15:24 -0700173 // This is a hack to get the resource ID for the dictionary data. Even
174 // though nextDataTransferHandle field is supposed to be used for something
175 // else, BIOS is using it to specify the resource ID corresponding to the
176 // dictionary data.
177 uint32_t resourceId = header->nextDataTransferHandle;
178
179 // data points to the payload of the MultipartReceive.
180 const uint8_t* data = rdeCommand.data() + sizeof(MultipartReceiveResHeader);
181 RdeDecodeStatus ret = RdeDecodeStatus::RdeOk;
182
183 switch (header->transferFlag)
184 {
kasunathaedea9f2022-12-05 11:17:26 -0800185 case static_cast<uint8_t>(
186 RdeMultiReceiveTransferFlag::RdeMRecFlagStart):
kasunathbac958d2022-06-07 18:15:24 -0700187 handleFlagStart(header, data, resourceId);
188 break;
kasunathaedea9f2022-12-05 11:17:26 -0800189 case static_cast<uint8_t>(
190 RdeMultiReceiveTransferFlag::RdeMRecFlagMiddle):
kasunathbac958d2022-06-07 18:15:24 -0700191 ret = handleFlagMiddle(header, data, resourceId);
192 break;
kasunathaedea9f2022-12-05 11:17:26 -0800193 case static_cast<uint8_t>(RdeMultiReceiveTransferFlag::RdeMRecFlagEnd):
kasunathbac958d2022-06-07 18:15:24 -0700194 ret = handleFlagEnd(rdeCommand, header, data, resourceId);
195 break;
kasunathaedea9f2022-12-05 11:17:26 -0800196 case static_cast<uint8_t>(
197 RdeMultiReceiveTransferFlag::RdeMRecFlagStartAndEnd):
kasunathbac958d2022-06-07 18:15:24 -0700198 ret = handleFlagStartAndEnd(rdeCommand, header, data, resourceId);
199 break;
200 default:
Patrick Williams5de90612024-02-13 21:31:53 -0600201 stdplus::print(stderr, "Invalid transfer flag: {}\n",
202 header->transferFlag);
kasunathbac958d2022-06-07 18:15:24 -0700203 ret = RdeDecodeStatus::RdeInvalidCommand;
204 }
205
206 // If there is a failure, this assignment is not useful. So we can do it
207 // even if there is a failure.
208 prevDictResourceId = resourceId;
209 return ret;
210}
211
212void RdeCommandHandler::calcCrcTable()
213{
214 for (uint32_t i = 0; i < UINT8_MAX + 1; ++i)
215 {
216 uint32_t rem = i;
217 for (uint8_t k = 0; k < 8; ++k)
218 {
219 rem = (rem & 1) ? (rem >> 1) ^ crcDevisor : rem >> 1;
220 }
221 crcTable[i] = rem;
222 }
223}
224
225void RdeCommandHandler::updateCrc(std::span<const uint8_t> stream)
226{
227 for (uint32_t i = 0; i < stream.size_bytes(); ++i)
228 {
229 crc = crcTable[(crc ^ stream[i]) & 0xff] ^ (crc >> 8);
230 }
231}
232
233uint32_t RdeCommandHandler::finalChecksum()
234{
235 return (crc ^ 0xFFFFFFFF);
236}
237
Patrick Williams99cd49a2025-03-03 11:20:29 -0500238RdeDecodeStatus RdeCommandHandler::handleCrc(
239 std::span<const uint8_t> multiReceiveRespCmd)
kasunathbac958d2022-06-07 18:15:24 -0700240{
241 const MultipartReceiveResHeader* header =
242 reinterpret_cast<const MultipartReceiveResHeader*>(
243 multiReceiveRespCmd.data());
Kasun Athukoralae710a362025-07-16 01:18:49 +0000244
245 // Validate that the total message size (header + data + checksum) does not
246 // exceed the actual size of the received buffer.
247 size_t expectedSize = sizeof(MultipartReceiveResHeader) +
248 header->dataLengthBytes + sizeof(uint32_t);
249 if (expectedSize != multiReceiveRespCmd.size())
250 {
251 stdplus::print(
252 stderr,
253 "Corruption detected: Invalid dataLengthBytes in header or not enough bytes for checksum.\n");
254 return RdeDecodeStatus::RdeInvalidCommand;
255 }
256
Patrick Williams1a643562024-08-16 15:22:05 -0400257 const uint8_t* checksumPtr =
258 multiReceiveRespCmd.data() + sizeof(MultipartReceiveResHeader) +
259 header->dataLengthBytes;
kasunathbac958d2022-06-07 18:15:24 -0700260 uint32_t checksum = checksumPtr[0] | (checksumPtr[1] << 8) |
261 (checksumPtr[2] << 16) | (checksumPtr[3] << 24);
262
263 if (finalChecksum() != checksum)
264 {
Patrick Williams5de90612024-02-13 21:31:53 -0600265 stdplus::print(stderr, "Checksum failed. Ex: {} Calculated: {}\n",
266 checksum, finalChecksum());
kasunathbac958d2022-06-07 18:15:24 -0700267 dictionaryManager.invalidateDictionaries();
268 return RdeDecodeStatus::RdeInvalidChecksum;
269 }
270 return RdeDecodeStatus::RdeOk;
271}
272
273void RdeCommandHandler::handleFlagStart(const MultipartReceiveResHeader* header,
274 const uint8_t* data,
275 uint32_t resourceId)
276{
277 // This is a beginning of a dictionary. Reset CRC.
278 crc = 0xFFFFFFFF;
279 std::span dataS(data, header->dataLengthBytes);
280 dictionaryManager.startDictionaryEntry(resourceId, dataS);
281 // Start checksum calculation only for the data portion.
282 updateCrc(dataS);
283 flagState = RdeDictTransferFlagState::RdeStateStartRecvd;
284}
285
Patrick Williams1a643562024-08-16 15:22:05 -0400286RdeDecodeStatus RdeCommandHandler::handleFlagMiddle(
287 const MultipartReceiveResHeader* header, const uint8_t* data,
288 uint32_t resourceId)
kasunathbac958d2022-06-07 18:15:24 -0700289{
290 if (flagState != RdeDictTransferFlagState::RdeStateStartRecvd)
291 {
Patrick Williams5de90612024-02-13 21:31:53 -0600292 stdplus::print(
kasunathbac958d2022-06-07 18:15:24 -0700293 stderr,
294 "Invalid dictionary packet order. Need start before middle.\n");
295 return RdeDecodeStatus::RdeInvalidPktOrder;
296 }
297
298 std::span dataS(data, header->dataLengthBytes);
299 if (prevDictResourceId != resourceId)
300 {
301 // Start of a new dictionary. Mark previous dictionary as
302 // complete.
303 dictionaryManager.markDataComplete(prevDictResourceId);
304 dictionaryManager.startDictionaryEntry(resourceId, dataS);
305 }
306 else
307 {
308 // Not a new dictionary. Add the received data to the existing
309 // dictionary.
310 if (!dictionaryManager.addDictionaryData(resourceId, dataS))
311 {
Patrick Williams5de90612024-02-13 21:31:53 -0600312 stdplus::print(stderr,
313 "Failed to add dictionary data: ResourceId: {}\n",
314 resourceId);
kasunathbac958d2022-06-07 18:15:24 -0700315 return RdeDecodeStatus::RdeDictionaryError;
316 }
317 }
318 // Continue checksum calculation only for the data portion.
319 updateCrc(dataS);
320 return RdeDecodeStatus::RdeOk;
321}
322
Patrick Williams1a643562024-08-16 15:22:05 -0400323RdeDecodeStatus RdeCommandHandler::handleFlagEnd(
324 std::span<const uint8_t> rdeCommand,
325 const MultipartReceiveResHeader* header, const uint8_t* data,
326 uint32_t resourceId)
kasunathbac958d2022-06-07 18:15:24 -0700327{
328 if (flagState != RdeDictTransferFlagState::RdeStateStartRecvd)
329 {
Patrick Williams5de90612024-02-13 21:31:53 -0600330 stdplus::print(
kasunathbac958d2022-06-07 18:15:24 -0700331 stderr,
332 "Invalid dictionary packet order. Need start before middle.\n");
333 return RdeDecodeStatus::RdeInvalidPktOrder;
334 }
335 flagState = RdeDictTransferFlagState::RdeStateIdle;
336
337 std::span dataS(data, header->dataLengthBytes);
338 if (prevDictResourceId != resourceId)
339 {
340 // Start of a new dictionary. Mark previous dictionary as
341 // complete.
342 dictionaryManager.markDataComplete(prevDictResourceId);
343 dictionaryManager.startDictionaryEntry(resourceId, dataS);
344 }
345 else
346 {
347 if (!dictionaryManager.addDictionaryData(resourceId, dataS))
348 {
Patrick Williams5de90612024-02-13 21:31:53 -0600349 stdplus::print(stderr,
350 "Failed to add dictionary data: ResourceId: {}\n",
351 resourceId);
kasunathbac958d2022-06-07 18:15:24 -0700352 return RdeDecodeStatus::RdeDictionaryError;
353 }
354 }
355 dictionaryManager.markDataComplete(resourceId);
356
357 // Continue checksum calculation only for the data portion. At the end of
358 // data, we will have the DataIntegrityChecksum field. So omit that when
359 // calculating checksum.
360 updateCrc(dataS);
361 auto ret = handleCrc(rdeCommand);
362 if (ret != RdeDecodeStatus::RdeOk)
363 {
364 return ret;
365 }
366 return RdeDecodeStatus::RdeStopFlagReceived;
367}
368
369RdeDecodeStatus RdeCommandHandler::handleFlagStartAndEnd(
370 std::span<const uint8_t> rdeCommand,
371 const MultipartReceiveResHeader* header, const uint8_t* data,
372 uint32_t resourceId)
373{
374 // This is a beginning of a dictionary. Reset CRC.
375 crc = 0xFFFFFFFF;
376 // This is a beginning and end of a dictionary.
377 dictionaryManager.startDictionaryEntry(
378 resourceId, std::span(data, header->dataLengthBytes));
379 dictionaryManager.markDataComplete(resourceId);
380 flagState = RdeDictTransferFlagState::RdeStateIdle;
381
382 // Do checksum calculation only for the data portion. At the end of data, we
383 // will have the DataIntegrityChecksum field. So omit that when calculating
384 // checksum.
385 updateCrc(std::span(data, header->dataLengthBytes));
386
387 auto ret = handleCrc(rdeCommand);
388 if (ret != RdeDecodeStatus::RdeOk)
389 {
390 return ret;
391 }
392 return RdeDecodeStatus::RdeStopFlagReceived;
393}
394
395} // namespace rde
396} // namespace bios_bmc_smm_error_logger