]> git.proxmox.com Git - mirror_edk2.git/blame - EmulatorPkg/Library/SmbiosLib/SmbiosLib.c
EmulatorPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / EmulatorPkg / Library / SmbiosLib / SmbiosLib.c
CommitLineData
79e4f2a5
RN
1/** @file\r
2 Provides library functions for common SMBIOS operations. Only available to DXE\r
3 and UEFI module types.\r
4\r
5\r
6Copyright (c) 2012, Apple Inc. All rights reserved.\r
7Portitions Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
e3ba31da 8SPDX-License-Identifier: BSD-2-Clause-Patent\r
79e4f2a5
RN
9\r
10**/\r
11\r
12#include <PiDxe.h>\r
13#include <Library/BaseLib.h>\r
14#include <Library/BaseMemoryLib.h>\r
15#include <Library/DebugLib.h>\r
16#include <Library/MemoryAllocationLib.h>\r
17#include <Library/UefiBootServicesTableLib.h>\r
18#include <Library/UefiLib.h>\r
19#include <Library/SmbiosLib.h>\r
20\r
21\r
22EFI_SMBIOS_PROTOCOL *gSmbios = NULL;\r
23\r
24\r
25/**\r
26 Create an initial SMBIOS Table from an array of SMBIOS_TEMPLATE_ENTRY\r
27 entries. SMBIOS_TEMPLATE_ENTRY.NULL indicates the end of the table.\r
28\r
29 @param Template Array of SMBIOS_TEMPLATE_ENTRY entries.\r
30\r
31 @retval EFI_SUCCESS New SMBIOS tables were created.\r
32 @retval EFI_OUT_OF_RESOURCES New SMBIOS tables were not created.\r
33**/\r
34EFI_STATUS\r
35EFIAPI\r
36SmbiosLibInitializeFromTemplate (\r
37 IN SMBIOS_TEMPLATE_ENTRY *Template\r
38 )\r
39{\r
40 EFI_STATUS Status;\r
41 UINTN Index;\r
42\r
43 if (Template == NULL) {\r
44 return EFI_INVALID_PARAMETER;\r
45 }\r
46\r
47 Status = EFI_SUCCESS;\r
48\r
49 for (Index = 0; Template[Index].Entry != NULL; Index++) {\r
50 Status = SmbiosLibCreateEntry (Template[Index].Entry, Template[Index].StringArray);\r
51 }\r
52\r
53 return Status;\r
54}\r
55\r
56\r
57\r
58/**\r
59 Create SMBIOS record.\r
60\r
61 Converts a fixed SMBIOS structure and an array of pointers to strings into\r
62 an SMBIOS record where the strings are cat'ed on the end of the fixed record\r
63 and terminated via a double NULL and add to SMBIOS table.\r
64\r
65 SMBIOS_TABLE_TYPE32 gSmbiosType12 = {\r
66 { EFI_SMBIOS_TYPE_SYSTEM_CONFIGURATION_OPTIONS, sizeof (SMBIOS_TABLE_TYPE12), 0 },\r
67 1 // StringCount\r
68 };\r
69 CHAR8 *gSmbiosType12Strings[] = {\r
70 "Not Found",\r
71 NULL\r
72 };\r
73\r
74 ...\r
75 CreateSmbiosEntry (\r
76 (EFI_SMBIOS_TABLE_HEADER*)&gSmbiosType12,\r
77 gSmbiosType12Strings\r
78 );\r
79\r
80 @param SmbiosEntry Fixed SMBIOS structure\r
81 @param StringArray Array of strings to convert to an SMBIOS string pack.\r
82 NULL is OK.\r
83\r
84**/\r
85EFI_STATUS\r
86EFIAPI\r
87SmbiosLibCreateEntry (\r
88 IN SMBIOS_STRUCTURE *SmbiosEntry,\r
89 IN CHAR8 **StringArray\r
90 )\r
91{\r
92 EFI_STATUS Status;\r
93 EFI_SMBIOS_HANDLE SmbiosHandle;\r
94 EFI_SMBIOS_TABLE_HEADER *Record;\r
95 UINTN Index;\r
96 UINTN StringSize;\r
97 UINTN Size;\r
98 CHAR8 *Str;\r
99\r
100 // Calculate the size of the fixed record and optional string pack\r
101 Size = SmbiosEntry->Length;\r
102 if (StringArray == NULL) {\r
103 Size += 2; // Min string section is double null\r
104 } else if (StringArray[0] == NULL) {\r
105 Size += 2; // Min string section is double null\r
106 } else {\r
107 for (Index = 0; StringArray[Index] != NULL; Index++) {\r
108 StringSize = AsciiStrSize (StringArray[Index]);\r
109 Size += StringSize;\r
110 }\r
111 // Don't forget the terminating double null\r
112 Size += 1;\r
113 }\r
114\r
115 // Copy over Template\r
116 Record = (EFI_SMBIOS_TABLE_HEADER *)AllocateZeroPool (Size);\r
117 if (Record == NULL) {\r
118 return EFI_OUT_OF_RESOURCES;\r
119 }\r
120 CopyMem (Record, SmbiosEntry, SmbiosEntry->Length);\r
121\r
122 if (StringArray != NULL) {\r
123 // Append string pack\r
124 Str = ((CHAR8 *)Record) + Record->Length;\r
125 for (Index = 0; StringArray[Index] != NULL; Index++) {\r
126 StringSize = AsciiStrSize (StringArray[Index]);\r
127 CopyMem (Str, StringArray[Index], StringSize);\r
128 Str += StringSize;\r
129 }\r
130 *Str = 0;\r
131 }\r
132\r
133 SmbiosHandle = SMBIOS_HANDLE_PI_RESERVED;\r
134 Status = gSmbios->Add (\r
135 gSmbios,\r
136 gImageHandle,\r
137 &SmbiosHandle,\r
138 Record\r
139 );\r
140\r
141 FreePool (Record);\r
142 return Status;\r
143}\r
144\r
145\r
146\r
147/**\r
148 Update the string associated with an existing SMBIOS record.\r
149\r
150 This function allows the update of specific SMBIOS strings. The number of valid strings for any\r
151 SMBIOS record is defined by how many strings were present when Add() was called.\r
152\r
153 @param[in] SmbiosHandle SMBIOS Handle of structure that will have its string updated.\r
154 @param[in] StringNumber The non-zero string number of the string to update.\r
155 @param[in] String Update the StringNumber string with String.\r
156\r
157 @retval EFI_SUCCESS SmbiosHandle had its StringNumber String updated.\r
158 @retval EFI_INVALID_PARAMETER SmbiosHandle does not exist. Or String is invalid.\r
159 @retval EFI_UNSUPPORTED String was not added because it is longer than the SMBIOS Table supports.\r
160 @retval EFI_NOT_FOUND The StringNumber.is not valid for this SMBIOS record.\r
161**/\r
162EFI_STATUS\r
163EFIAPI\r
164SmbiosLibUpdateString (\r
165 IN EFI_SMBIOS_HANDLE SmbiosHandle,\r
166 IN SMBIOS_TABLE_STRING StringNumber,\r
167 IN CHAR8 *String\r
168 )\r
169{\r
170 UINTN StringIndex;\r
171\r
172 if (String == NULL) {\r
173 return EFI_INVALID_PARAMETER;\r
174 }\r
175\r
176 if (*String == '\0') {\r
177 // A string with no data is not legal in SMBIOS\r
178 return EFI_INVALID_PARAMETER;\r
179 }\r
180\r
181 StringIndex = StringNumber;\r
182 return gSmbios->UpdateString (gSmbios, &SmbiosHandle, &StringIndex, String);\r
183}\r
184\r
185\r
186/**\r
187 Update the string associated with an existing SMBIOS record.\r
188\r
189 This function allows the update of specific SMBIOS strings. The number of valid strings for any\r
190 SMBIOS record is defined by how many strings were present when Add() was called.\r
191\r
192 @param[in] SmbiosHandle SMBIOS Handle of structure that will have its string updated.\r
193 @param[in] StringNumber The non-zero string number of the string to update.\r
194 @param[in] String Update the StringNumber string with String.\r
195\r
196 @retval EFI_SUCCESS SmbiosHandle had its StringNumber String updated.\r
197 @retval EFI_INVALID_PARAMETER SmbiosHandle does not exist. Or String is invalid.\r
198 @retval EFI_UNSUPPORTED String was not added because it is longer than the SMBIOS Table supports.\r
199 @retval EFI_NOT_FOUND The StringNumber.is not valid for this SMBIOS record.\r
200**/\r
201EFI_STATUS\r
202EFIAPI\r
203SmbiosLibUpdateUnicodeString (\r
204 IN EFI_SMBIOS_HANDLE SmbiosHandle,\r
205 IN SMBIOS_TABLE_STRING StringNumber,\r
206 IN CHAR16 *String\r
207 )\r
208{\r
209 EFI_STATUS Status;\r
210 UINTN StringIndex;\r
211 CHAR8 *Ascii;\r
212\r
213 if (String == NULL) {\r
214 return EFI_INVALID_PARAMETER;\r
215 }\r
216\r
217 if (*String == '\0') {\r
218 // A string with no data is not legal in SMBIOS\r
219 return EFI_INVALID_PARAMETER;\r
220 }\r
221\r
222 Ascii = AllocateZeroPool (StrSize (String));\r
223 if (Ascii == NULL) {\r
224 return EFI_OUT_OF_RESOURCES;\r
225 }\r
226 UnicodeStrToAsciiStr (String, Ascii);\r
227\r
228 StringIndex = StringNumber;\r
229 Status = gSmbios->UpdateString (gSmbios, &SmbiosHandle, &StringIndex, Ascii);\r
230\r
231 FreePool (Ascii);\r
232 return Status;\r
233}\r
234\r
235\r
236/**\r
237 Allow caller to read a specific SMBIOS string\r
238\r
239 @param[in] Header SMBIOS record that contains the string.\r
240 @param[in[ StringNumber Instance of SMBIOS string 1 - N.\r
241\r
242 @retval NULL Instance of Type SMBIOS string was not found.\r
243 @retval Other Pointer to matching SMBIOS string.\r
244**/\r
245CHAR8 *\r
246EFIAPI\r
247SmbiosLibReadString (\r
248 IN SMBIOS_STRUCTURE *Header,\r
249 IN EFI_SMBIOS_STRING StringNumber\r
250 )\r
251{\r
252 CHAR8 *Data;\r
253 UINTN Match;\r
254\r
255 Data = (CHAR8 *)Header + Header->Length;\r
256 for (Match = 1;!(*Data == 0 && *(Data+1) == 0); ) {\r
257 if (StringNumber == Match) {\r
258 return Data;\r
259 }\r
260 Data++;\r
261 if (*(Data - 1) == '\0') {\r
262 Match++;\r
263 }\r
264 }\r
265\r
266 return NULL;\r
267}\r
268\r
269\r
270/**\r
271 Allow the caller to discover a specific SMBIOS entry, and patch it if necissary.\r
272\r
273 @param[in] Type Type of the next SMBIOS record to return.\r
274 @param[in[ Instance Instance of SMBIOS record 0 - N-1.\r
275 @param[out] SmbiosHandle Returns SMBIOS handle for the matching record.\r
276\r
277 @retval NULL Instance of Type SMBIOS record was not found.\r
278 @retval Other Pointer to matching SMBIOS record.\r
279**/\r
280SMBIOS_STRUCTURE *\r
281EFIAPI\r
282SmbiosLibGetRecord (\r
283 IN EFI_SMBIOS_TYPE Type,\r
284 IN UINTN Instance,\r
285 OUT EFI_SMBIOS_HANDLE *SmbiosHandle\r
286 )\r
287{\r
288 EFI_STATUS Status;\r
289 EFI_SMBIOS_TABLE_HEADER *Record;\r
290 UINTN Match;\r
291\r
292 Match = 0;\r
293 *SmbiosHandle = SMBIOS_HANDLE_PI_RESERVED;\r
294 do {\r
295 Status = gSmbios->GetNext (gSmbios, SmbiosHandle, &Type, &Record, NULL);\r
296 if (!EFI_ERROR (Status)) {\r
297 if (Match == Instance) {\r
298 return (SMBIOS_STRUCTURE *)Record;\r
299 }\r
300 Match++;\r
301 }\r
302 } while (!EFI_ERROR (Status));\r
303\r
304 return NULL;\r
305}\r
306\r
307\r
308/**\r
309 Remove an SMBIOS record.\r
310\r
311 This function removes an SMBIOS record using the handle specified by SmbiosHandle.\r
312\r
313 @param[in] SmbiosHandle The handle of the SMBIOS record to remove.\r
314\r
315 @retval EFI_SUCCESS SMBIOS record was removed.\r
316 @retval EFI_INVALID_PARAMETER SmbiosHandle does not specify a valid SMBIOS record.\r
317**/\r
318EFI_STATUS\r
319EFIAPI\r
320SmbiosLibRemove (\r
321 OUT EFI_SMBIOS_HANDLE SmbiosHandle\r
322 )\r
323{\r
324 return gSmbios->Remove (gSmbios, SmbiosHandle);\r
325}\r
326\r
327\r
328\r
329/**\r
330\r
331 @param ImageHandle ImageHandle of the loaded driver.\r
332 @param SystemTable Pointer to the EFI System Table.\r
333\r
334 @retval EFI_SUCCESS Register successfully.\r
335 @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.\r
336**/\r
337EFI_STATUS\r
338EFIAPI\r
339SmbiosLibConstructor (\r
340 IN EFI_HANDLE ImageHandle,\r
341 IN EFI_SYSTEM_TABLE *SystemTable\r
342 )\r
343{\r
344 return gBS->LocateProtocol (&gEfiSmbiosProtocolGuid, NULL, (VOID **)&gSmbios);\r
345}\r
346\r