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