2 Provides library functions for common SMBIOS operations. Only available to DXE
6 Copyright (c) 2012, Apple Inc. All rights reserved.
7 Portitions Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Library/BaseLib.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/DebugLib.h>
16 #include <Library/MemoryAllocationLib.h>
17 #include <Library/UefiBootServicesTableLib.h>
18 #include <Library/UefiLib.h>
19 #include <Library/SmbiosLib.h>
22 EFI_SMBIOS_PROTOCOL
*gSmbios
= NULL
;
26 Create an initial SMBIOS Table from an array of SMBIOS_TEMPLATE_ENTRY
27 entries. SMBIOS_TEMPLATE_ENTRY.NULL indicates the end of the table.
29 @param Template Array of SMBIOS_TEMPLATE_ENTRY entries.
31 @retval EFI_SUCCESS New SMBIOS tables were created.
32 @retval EFI_OUT_OF_RESOURCES New SMBIOS tables were not created.
36 SmbiosLibInitializeFromTemplate (
37 IN SMBIOS_TEMPLATE_ENTRY
*Template
43 if (Template
== NULL
) {
44 return EFI_INVALID_PARAMETER
;
49 for (Index
= 0; Template
[Index
].Entry
!= NULL
; Index
++) {
50 Status
= SmbiosLibCreateEntry (Template
[Index
].Entry
, Template
[Index
].StringArray
);
61 Converts a fixed SMBIOS structure and an array of pointers to strings into
62 an SMBIOS record where the strings are cat'ed on the end of the fixed record
63 and terminated via a double NULL and add to SMBIOS table.
65 SMBIOS_TABLE_TYPE32 gSmbiosType12 = {
66 { EFI_SMBIOS_TYPE_SYSTEM_CONFIGURATION_OPTIONS, sizeof (SMBIOS_TABLE_TYPE12), 0 },
69 CHAR8 *gSmbiosType12Strings[] = {
76 (EFI_SMBIOS_TABLE_HEADER*)&gSmbiosType12,
80 @param SmbiosEntry Fixed SMBIOS structure
81 @param StringArray Array of strings to convert to an SMBIOS string pack.
87 SmbiosLibCreateEntry (
88 IN SMBIOS_STRUCTURE
*SmbiosEntry
,
89 IN CHAR8
**StringArray
93 EFI_SMBIOS_HANDLE SmbiosHandle
;
94 EFI_SMBIOS_TABLE_HEADER
*Record
;
100 // Calculate the size of the fixed record and optional string pack
101 Size
= SmbiosEntry
->Length
;
102 if (StringArray
== NULL
) {
103 Size
+= 2; // Min string section is double null
104 } else if (StringArray
[0] == NULL
) {
105 Size
+= 2; // Min string section is double null
107 for (Index
= 0; StringArray
[Index
] != NULL
; Index
++) {
108 StringSize
= AsciiStrSize (StringArray
[Index
]);
111 // Don't forget the terminating double null
115 // Copy over Template
116 Record
= (EFI_SMBIOS_TABLE_HEADER
*)AllocateZeroPool (Size
);
117 if (Record
== NULL
) {
118 return EFI_OUT_OF_RESOURCES
;
120 CopyMem (Record
, SmbiosEntry
, SmbiosEntry
->Length
);
122 if (StringArray
!= NULL
) {
123 // Append string pack
124 Str
= ((CHAR8
*)Record
) + Record
->Length
;
125 for (Index
= 0; StringArray
[Index
] != NULL
; Index
++) {
126 StringSize
= AsciiStrSize (StringArray
[Index
]);
127 CopyMem (Str
, StringArray
[Index
], StringSize
);
133 SmbiosHandle
= SMBIOS_HANDLE_PI_RESERVED
;
134 Status
= gSmbios
->Add (
148 Update the string associated with an existing SMBIOS record.
150 This function allows the update of specific SMBIOS strings. The number of valid strings for any
151 SMBIOS record is defined by how many strings were present when Add() was called.
153 @param[in] SmbiosHandle SMBIOS Handle of structure that will have its string updated.
154 @param[in] StringNumber The non-zero string number of the string to update.
155 @param[in] String Update the StringNumber string with String.
157 @retval EFI_SUCCESS SmbiosHandle had its StringNumber String updated.
158 @retval EFI_INVALID_PARAMETER SmbiosHandle does not exist. Or String is invalid.
159 @retval EFI_UNSUPPORTED String was not added because it is longer than the SMBIOS Table supports.
160 @retval EFI_NOT_FOUND The StringNumber.is not valid for this SMBIOS record.
164 SmbiosLibUpdateString (
165 IN EFI_SMBIOS_HANDLE SmbiosHandle
,
166 IN SMBIOS_TABLE_STRING StringNumber
,
172 if (String
== NULL
) {
173 return EFI_INVALID_PARAMETER
;
176 if (*String
== '\0') {
177 // A string with no data is not legal in SMBIOS
178 return EFI_INVALID_PARAMETER
;
181 StringIndex
= StringNumber
;
182 return gSmbios
->UpdateString (gSmbios
, &SmbiosHandle
, &StringIndex
, String
);
187 Update the string associated with an existing SMBIOS record.
189 This function allows the update of specific SMBIOS strings. The number of valid strings for any
190 SMBIOS record is defined by how many strings were present when Add() was called.
192 @param[in] SmbiosHandle SMBIOS Handle of structure that will have its string updated.
193 @param[in] StringNumber The non-zero string number of the string to update.
194 @param[in] String Update the StringNumber string with String.
196 @retval EFI_SUCCESS SmbiosHandle had its StringNumber String updated.
197 @retval EFI_INVALID_PARAMETER SmbiosHandle does not exist. Or String is invalid.
198 @retval EFI_UNSUPPORTED String was not added because it is longer than the SMBIOS Table supports.
199 @retval EFI_NOT_FOUND The StringNumber.is not valid for this SMBIOS record.
203 SmbiosLibUpdateUnicodeString (
204 IN EFI_SMBIOS_HANDLE SmbiosHandle
,
205 IN SMBIOS_TABLE_STRING StringNumber
,
213 if (String
== NULL
) {
214 return EFI_INVALID_PARAMETER
;
217 if (*String
== '\0') {
218 // A string with no data is not legal in SMBIOS
219 return EFI_INVALID_PARAMETER
;
222 Ascii
= AllocateZeroPool (StrSize (String
));
224 return EFI_OUT_OF_RESOURCES
;
226 UnicodeStrToAsciiStr (String
, Ascii
);
228 StringIndex
= StringNumber
;
229 Status
= gSmbios
->UpdateString (gSmbios
, &SmbiosHandle
, &StringIndex
, Ascii
);
237 Allow caller to read a specific SMBIOS string
239 @param[in] Header SMBIOS record that contains the string.
240 @param[in[ StringNumber Instance of SMBIOS string 1 - N.
242 @retval NULL Instance of Type SMBIOS string was not found.
243 @retval Other Pointer to matching SMBIOS string.
247 SmbiosLibReadString (
248 IN SMBIOS_STRUCTURE
*Header
,
249 IN EFI_SMBIOS_STRING StringNumber
255 Data
= (CHAR8
*)Header
+ Header
->Length
;
256 for (Match
= 1;!(*Data
== 0 && *(Data
+1) == 0); ) {
257 if (StringNumber
== Match
) {
261 if (*(Data
- 1) == '\0') {
271 Allow the caller to discover a specific SMBIOS entry, and patch it if necissary.
273 @param[in] Type Type of the next SMBIOS record to return.
274 @param[in[ Instance Instance of SMBIOS record 0 - N-1.
275 @param[out] SmbiosHandle Returns SMBIOS handle for the matching record.
277 @retval NULL Instance of Type SMBIOS record was not found.
278 @retval Other Pointer to matching SMBIOS record.
283 IN EFI_SMBIOS_TYPE Type
,
285 OUT EFI_SMBIOS_HANDLE
*SmbiosHandle
289 EFI_SMBIOS_TABLE_HEADER
*Record
;
293 *SmbiosHandle
= SMBIOS_HANDLE_PI_RESERVED
;
295 Status
= gSmbios
->GetNext (gSmbios
, SmbiosHandle
, &Type
, &Record
, NULL
);
296 if (!EFI_ERROR (Status
)) {
297 if (Match
== Instance
) {
298 return (SMBIOS_STRUCTURE
*)Record
;
302 } while (!EFI_ERROR (Status
));
309 Remove an SMBIOS record.
311 This function removes an SMBIOS record using the handle specified by SmbiosHandle.
313 @param[in] SmbiosHandle The handle of the SMBIOS record to remove.
315 @retval EFI_SUCCESS SMBIOS record was removed.
316 @retval EFI_INVALID_PARAMETER SmbiosHandle does not specify a valid SMBIOS record.
321 OUT EFI_SMBIOS_HANDLE SmbiosHandle
324 return gSmbios
->Remove (gSmbios
, SmbiosHandle
);
331 @param ImageHandle ImageHandle of the loaded driver.
332 @param SystemTable Pointer to the EFI System Table.
334 @retval EFI_SUCCESS Register successfully.
335 @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.
339 SmbiosLibConstructor (
340 IN EFI_HANDLE ImageHandle
,
341 IN EFI_SYSTEM_TABLE
*SystemTable
344 return gBS
->LocateProtocol (&gEfiSmbiosProtocolGuid
, NULL
, (VOID
**)&gSmbios
);