blob: 0904fad1c0780873d027b59cfdd38a574422e7b1 [file] [log] [blame]
kasunatha1a69f12022-05-27 14:31:38 -07001#include "rde/rde_dictionary_manager.hpp"
2
Patrick Williams5de90612024-02-13 21:31:53 -06003#include <stdplus/print.hpp>
4
5#include <format>
kasunatha1a69f12022-05-27 14:31:38 -07006
7namespace bios_bmc_smm_error_logger
8{
9namespace rde
10{
11
Patrick Williamsb8125762023-05-10 07:51:26 -050012DictionaryManager::DictionaryManager() : validDictionaryCount(0) {}
kasunatha1a69f12022-05-27 14:31:38 -070013
14void DictionaryManager::startDictionaryEntry(
15 uint32_t resourceId, const std::span<const uint8_t> data)
16{
17 // Check whether the resourceId is already available.
18 auto itemIt = dictionaries.find(resourceId);
19 if (itemIt == dictionaries.end())
20 {
Patrick Williamsb8125762023-05-10 07:51:26 -050021 dictionaries[resourceId] = std::make_unique<DictionaryEntry>(false,
22 data);
kasunatha1a69f12022-05-27 14:31:38 -070023 return;
24 }
25
26 // Since we are creating a new dictionary on an existing entry, invalidate
27 // the existing entry.
28 invalidateDictionaryEntry(*itemIt->second);
29
30 // Flush the existing data.
31 itemIt->second->data.clear();
32 itemIt->second->data.insert(itemIt->second->data.begin(), data.begin(),
33 data.end());
34}
35
36bool DictionaryManager::markDataComplete(uint32_t resourceId)
37{
38 auto itemIt = dictionaries.find(resourceId);
39 if (itemIt == dictionaries.end())
40 {
Patrick Williams5de90612024-02-13 21:31:53 -060041 stdplus::print(stderr, "Resource ID {} not found.\n", resourceId);
kasunatha1a69f12022-05-27 14:31:38 -070042 return false;
43 }
44 validateDictionaryEntry(*itemIt->second);
45 return true;
46}
47
48bool DictionaryManager::addDictionaryData(uint32_t resourceId,
49 const std::span<const uint8_t> data)
50{
51 auto itemIt = dictionaries.find(resourceId);
52 if (itemIt == dictionaries.end())
53 {
Patrick Williams5de90612024-02-13 21:31:53 -060054 stdplus::print(stderr, "Resource ID {} not found.\n", resourceId);
kasunatha1a69f12022-05-27 14:31:38 -070055 return false;
56 }
57 // Since we are modifying an existing entry, invalidate the existing entry.
58 invalidateDictionaryEntry(*itemIt->second);
59 itemIt->second->data.insert(itemIt->second->data.end(), data.begin(),
60 data.end());
61 return true;
62}
63
64std::optional<std::span<const uint8_t>>
65 DictionaryManager::getDictionary(uint32_t resourceId)
66{
67 auto itemIt = dictionaries.find(resourceId);
68 if (itemIt == dictionaries.end())
69 {
Patrick Williams5de90612024-02-13 21:31:53 -060070 stdplus::print(stderr, "Resource ID {} not found.\n", resourceId);
kasunatha1a69f12022-05-27 14:31:38 -070071 return std::nullopt;
72 }
73
74 if (!itemIt->second->valid)
75 {
Patrick Williams5de90612024-02-13 21:31:53 -060076 stdplus::print(stderr,
77 "Requested an incomplete dictionary. Resource ID {}\n",
78 resourceId);
kasunatha1a69f12022-05-27 14:31:38 -070079 return std::nullopt;
80 }
81 return itemIt->second->data;
82}
83
84std::optional<std::span<const uint8_t>>
85 DictionaryManager::getAnnotationDictionary()
86{
87 return getDictionary(annotationResourceId);
88}
89
90uint32_t DictionaryManager::getDictionaryCount()
91{
92 return validDictionaryCount;
93}
94
95void DictionaryManager::invalidateDictionaries()
96{
97 // We won't flush the existing data. The data will be flushed if a new entry
98 // is added for an existing resource ID.
99 for (const auto& element : dictionaries)
100 {
101 element.second->valid = false;
102 }
103 validDictionaryCount = 0;
104}
105
106void DictionaryManager::invalidateDictionaryEntry(DictionaryEntry& entry)
107{
108 // If this is a valid entry, reduce the valid dictionary count.
109 if (entry.valid)
110 {
111 --validDictionaryCount;
112 }
113 entry.valid = false;
114}
115
116void DictionaryManager::validateDictionaryEntry(DictionaryEntry& entry)
117{
118 if (!entry.valid)
119 {
120 ++validDictionaryCount;
121 }
122 entry.valid = true;
123}
124
125} // namespace rde
126} // namespace bios_bmc_smm_error_logger