TianoCore EDK2 master
Loading...
Searching...
No Matches
SmbiosStringTableLib.c
Go to the documentation of this file.
1
12#include <Library/BaseLib.h>
14#include <Library/DebugLib.h>
17
30EFIAPI
32 IN STRING_TABLE *CONST StrTable,
33 IN CONST CHAR8 *Str,
34 OUT UINT8 *StrRef OPTIONAL
35 )
36{
37 UINTN StrLength;
38 STRING_ELEMENT *StrElement;
39
40 if ((StrTable == NULL) || (Str == NULL)) {
41 return EFI_INVALID_PARAMETER;
42 }
43
44 if (StrTable->StrCount >= StrTable->MaxStringElements) {
45 return EFI_BUFFER_TOO_SMALL;
46 }
47
48 StrLength = AsciiStrLen (Str);
49 if (StrLength == 0) {
50 return EFI_INVALID_PARAMETER;
51 }
52
53 // Update the string element
54 StrElement = &StrTable->Elements[StrTable->StrCount];
55 StrElement->StringLen = StrLength;
56 StrElement->String = Str;
57
58 // Update String table information
59 StrTable->TotalStrLen += StrLength;
60 StrTable->StrCount++;
61
62 // Return the index of the string in the string table if requested
63 if (StrRef != NULL) {
64 // Note: SMBIOS string field references start at 1. So, return the
65 // StrCount as the string reference after it is updated.
66 *StrRef = StrTable->StrCount;
67 }
68
69 return EFI_SUCCESS;
70}
71
80EFIAPI
82 IN STRING_TABLE *CONST StrTable
83 )
84{
85 if (StrTable == NULL) {
86 ASSERT (0);
87 return 0;
88 }
89
90 // See Section 6.1.3 Text strings, SMBIOS Specification Version 3.6.0
91 // - If the formatted portion of the structure contains string-reference
92 // fields and all the string fields are set to 0 (no string references),
93 // the formatted section of the structure is followed by two null (00h)
94 // BYTES.
95 // - Each string is terminated with a null (00h) BYTE
96 // - and the set of strings is terminated with an additional null (00h) BYTE.
97
98 // Therefore, if string count = 0, return 2
99 // if string count > 0, the string set size =
100 // StrTable->TotalStrLen (total length of the strings in the string table)
101 // + StrTable->StrCount (add string count to include '\0' for each string)
102 // +1 (an additional '\0' is required at the end of the string set).
103 return (StrTable->StrCount == 0) ? 2 :
104 (StrTable->TotalStrLen + StrTable->StrCount + 1);
105}
106
119EFIAPI
121 IN STRING_TABLE *CONST StrTable,
122 IN CHAR8 *CONST SmbiosStringAreaStart,
123 IN CONST UINTN SmbiosStringAreaSize
124 )
125{
126 UINT8 Index;
127 STRING_ELEMENT *StrElement;
128 CHAR8 *SmbiosString;
129 UINTN BytesRemaining;
130 UINTN BytesCopied;
131
132 if ((StrTable == NULL) || (SmbiosStringAreaStart == NULL)) {
133 return EFI_INVALID_PARAMETER;
134 }
135
136 if (SmbiosStringAreaSize < StringTableGetStringSetSize (StrTable)) {
137 return EFI_BUFFER_TOO_SMALL;
138 }
139
140 SmbiosString = SmbiosStringAreaStart;
141 BytesRemaining = SmbiosStringAreaSize;
142
143 if (StrTable->StrCount == 0) {
144 // See Section 6.1.3 Text strings, SMBIOS Specification Version 3.6.0
145 // If the formatted portion of the structure contains string-reference
146 // fields and all the string fields are set to 0 (no string references),
147 // the formatted section of the structure is followed by two null (00h)
148 // BYTES.
149 *SmbiosString++ = '\0';
150 } else {
151 for (Index = 0; Index < StrTable->StrCount; Index++) {
152 StrElement = &StrTable->Elements[Index];
153 AsciiStrCpyS (SmbiosString, BytesRemaining, StrElement->String);
154
155 // See Section 6.1.3 Text strings, SMBIOS Specification Version 3.6.0
156 // - Each string is terminated with a null (00h) BYTE
157 // Bytes Copied = String length + 1 for the string NULL terminator.
158 BytesCopied = StrElement->StringLen + 1;
159 BytesRemaining -= BytesCopied;
160 SmbiosString += BytesCopied;
161 }
162 }
163
164 // See Section 6.1.3 Text strings, SMBIOS Specification Version 3.6.0
165 // - the set of strings is terminated with an additional null (00h) BYTE.
166 *SmbiosString = '\0';
167 return EFI_SUCCESS;
168}
169
181EFIAPI
183 IN STRING_TABLE *CONST StrTable,
184 IN UINTN MaxStringElements
185 )
186{
187 STRING_ELEMENT *Elements;
188
189 if ((StrTable == NULL) || (MaxStringElements > MAX_UINT8)) {
190 return EFI_INVALID_PARAMETER;
191 }
192
193 ZeroMem (StrTable, sizeof (STRING_TABLE));
194
195 Elements = (STRING_ELEMENT *)AllocateZeroPool (
196 sizeof (STRING_ELEMENT) * MaxStringElements
197 );
198 if (Elements == NULL) {
199 return EFI_OUT_OF_RESOURCES;
200 }
201
202 StrTable->Elements = Elements;
203 StrTable->MaxStringElements = (UINT8)MaxStringElements;
204 return EFI_SUCCESS;
205}
206
215EFIAPI
217 IN STRING_TABLE *CONST StrTable
218 )
219{
220 if ((StrTable == NULL) || (StrTable->Elements == NULL)) {
221 return EFI_INVALID_PARAMETER;
222 }
223
224 FreePool (StrTable->Elements);
225 ZeroMem (StrTable, sizeof (STRING_TABLE));
226 return EFI_SUCCESS;
227}
UINT64 UINTN
UINTN EFIAPI AsciiStrLen(IN CONST CHAR8 *String)
Definition: String.c:641
RETURN_STATUS EFIAPI AsciiStrCpyS(OUT CHAR8 *Destination, IN UINTN DestMax, IN CONST CHAR8 *Source)
Definition: SafeString.c:1797
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
UINTN EFIAPI StringTableGetStringSetSize(IN STRING_TABLE *CONST StrTable)
EFI_STATUS EFIAPI StringTableInitialize(IN STRING_TABLE *CONST StrTable, IN UINTN MaxStringElements)
EFI_STATUS EFIAPI StringTableFree(IN STRING_TABLE *CONST StrTable)
EFI_STATUS EFIAPI StringTablePublishStringSet(IN STRING_TABLE *CONST StrTable, IN CHAR8 *CONST SmbiosStringAreaStart, IN CONST UINTN SmbiosStringAreaSize)
EFI_STATUS EFIAPI StringTableAddString(IN STRING_TABLE *CONST StrTable, IN CONST CHAR8 *Str, OUT UINT8 *StrRef OPTIONAL)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
UINTN StringLen
Length of the string (does not include the NULL termination)
CONST CHAR8 * String
Reference to the string.