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 - 2011, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials are licensed and made available under
9 the terms and conditions of the BSD License that accompanies this distribution.
10 The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php.
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include <Library/BaseLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
24 #include <Library/UefiLib.h>
25 #include <Library/SmbiosLib.h>
28 EFI_SMBIOS_PROTOCOL
*gSmbios
= NULL
;
32 Create an initial SMBIOS Table from an array of SMBIOS_TEMPLATE_ENTRY
33 entries. SMBIOS_TEMPLATE_ENTRY.NULL indicates the end of the table.
35 @param Template Array of SMBIOS_TEMPLATE_ENTRY entries.
37 @retval EFI_SUCCESS New SMBIOS tables were created.
38 @retval EFI_OUT_OF_RESOURCES New SMBIOS tables were not created.
42 SmbiosLibInitializeFromTemplate (
43 IN SMBIOS_TEMPLATE_ENTRY
*Template
49 if (Template
== NULL
) {
50 return EFI_INVALID_PARAMETER
;
53 for (Index
= 0; Template
[Index
].Entry
!= NULL
; Index
++) {
54 Status
= SmbiosLibCreateEntry (Template
[Index
].Entry
, Template
[Index
].StringArray
);
65 Converts a fixed SMBIOS structure and an array of pointers to strings into
66 an SMBIOS record where the strings are cat'ed on the end of the fixed record
67 and terminated via a double NULL and add to SMBIOS table.
69 SMBIOS_TABLE_TYPE32 gSmbiosType12 = {
70 { EFI_SMBIOS_TYPE_SYSTEM_CONFIGURATION_OPTIONS, sizeof (SMBIOS_TABLE_TYPE12), 0 },
73 CHAR8 *gSmbiosType12Strings[] = {
80 (EFI_SMBIOS_TABLE_HEADER*)&gSmbiosType12,
84 @param SmbiosEntry Fixed SMBIOS structure
85 @param StringArray Array of strings to convert to an SMBIOS string pack.
91 SmbiosLibCreateEntry (
92 IN SMBIOS_STRUCTURE
*SmbiosEntry
,
93 IN CHAR8
**StringArray
97 EFI_SMBIOS_HANDLE SmbiosHandle
;
98 EFI_SMBIOS_TABLE_HEADER
*Record
;
104 // Calculate the size of the fixed record and optional string pack
105 Size
= SmbiosEntry
->Length
;
106 if (StringArray
== NULL
) {
107 Size
+= 2; // Min string section is double null
108 } else if (StringArray
[0] == NULL
) {
109 Size
+= 2; // Min string section is double null
111 for (Index
= 0; StringArray
[Index
] != NULL
; Index
++) {
112 StringSize
= AsciiStrSize (StringArray
[Index
]);
115 // Don't forget the terminating double null
119 // Copy over Template
120 Record
= (EFI_SMBIOS_TABLE_HEADER
*)AllocateZeroPool (Size
);
121 if (Record
== NULL
) {
122 return EFI_OUT_OF_RESOURCES
;
124 CopyMem (Record
, SmbiosEntry
, SmbiosEntry
->Length
);
126 if (StringArray
!= NULL
) {
127 // Append string pack
128 Str
= ((CHAR8
*)Record
) + Record
->Length
;
129 for (Index
= 0; StringArray
[Index
] != NULL
; Index
++) {
130 StringSize
= AsciiStrSize (StringArray
[Index
]);
131 CopyMem (Str
, StringArray
[Index
], StringSize
);
137 SmbiosHandle
= SMBIOS_HANDLE_PI_RESERVED
;
138 Status
= gSmbios
->Add (
152 Update the string associated with an existing SMBIOS record.
154 This function allows the update of specific SMBIOS strings. The number of valid strings for any
155 SMBIOS record is defined by how many strings were present when Add() was called.
157 @param[in] SmbiosHandle SMBIOS Handle of structure that will have its string updated.
158 @param[in] StringNumber The non-zero string number of the string to update.
159 @param[in] String Update the StringNumber string with String.
161 @retval EFI_SUCCESS SmbiosHandle had its StringNumber String updated.
162 @retval EFI_INVALID_PARAMETER SmbiosHandle does not exist. Or String is invalid.
163 @retval EFI_UNSUPPORTED String was not added because it is longer than the SMBIOS Table supports.
164 @retval EFI_NOT_FOUND The StringNumber.is not valid for this SMBIOS record.
168 SmbiosLibUpdateString (
169 IN EFI_SMBIOS_HANDLE SmbiosHandle
,
170 IN SMBIOS_TABLE_STRING StringNumber
,
176 if (String
== NULL
) {
177 return EFI_INVALID_PARAMETER
;
180 if (*String
== '\0') {
181 // A string with no data is not legal in SMBIOS
182 return EFI_INVALID_PARAMETER
;
185 StringIndex
= StringNumber
;
186 return gSmbios
->UpdateString (gSmbios
, &SmbiosHandle
, &StringIndex
, String
);
191 Update the string associated with an existing SMBIOS record.
193 This function allows the update of specific SMBIOS strings. The number of valid strings for any
194 SMBIOS record is defined by how many strings were present when Add() was called.
196 @param[in] SmbiosHandle SMBIOS Handle of structure that will have its string updated.
197 @param[in] StringNumber The non-zero string number of the string to update.
198 @param[in] String Update the StringNumber string with String.
200 @retval EFI_SUCCESS SmbiosHandle had its StringNumber String updated.
201 @retval EFI_INVALID_PARAMETER SmbiosHandle does not exist. Or String is invalid.
202 @retval EFI_UNSUPPORTED String was not added because it is longer than the SMBIOS Table supports.
203 @retval EFI_NOT_FOUND The StringNumber.is not valid for this SMBIOS record.
207 SmbiosLibUpdateUnicodeString (
208 IN EFI_SMBIOS_HANDLE SmbiosHandle
,
209 IN SMBIOS_TABLE_STRING StringNumber
,
217 if (String
== NULL
) {
218 return EFI_INVALID_PARAMETER
;
221 if (*String
== '\0') {
222 // A string with no data is not legal in SMBIOS
223 return EFI_INVALID_PARAMETER
;
226 Ascii
= AllocateZeroPool (StrSize (String
));
228 return EFI_OUT_OF_RESOURCES
;
230 UnicodeStrToAsciiStr (String
, Ascii
);
232 StringIndex
= StringNumber
;
233 Status
= gSmbios
->UpdateString (gSmbios
, &SmbiosHandle
, &StringIndex
, Ascii
);
241 Allow caller to read a specific SMBIOS string
243 @param[in] Header SMBIOS record that contains the string.
244 @param[in[ StringNumber Instance of SMBIOS string 1 - N.
246 @retval NULL Instance of Type SMBIOS string was not found.
247 @retval Other Pointer to matching SMBIOS string.
251 SmbiosLibReadString (
252 IN SMBIOS_STRUCTURE
*Header
,
253 IN EFI_SMBIOS_STRING StringNumber
259 Data
= (CHAR8
*)Header
+ Header
->Length
;
260 for (Match
= 1;!(*Data
== 0 && *(Data
+1) == 0); ) {
261 if (StringNumber
== Match
) {
265 if (*(Data
- 1) == '\0') {
275 Allow the caller to discover a specific SMBIOS entry, and patch it if necissary.
277 @param[in] Type Type of the next SMBIOS record to return.
278 @param[in[ Instance Instance of SMBIOS record 0 - N-1.
279 @param[out] SmbiosHandle Returns SMBIOS handle for the matching record.
281 @retval NULL Instance of Type SMBIOS record was not found.
282 @retval Other Pointer to matching SMBIOS record.
287 IN EFI_SMBIOS_TYPE Type
,
289 OUT EFI_SMBIOS_HANDLE
*SmbiosHandle
293 EFI_SMBIOS_TABLE_HEADER
*Record
;
297 *SmbiosHandle
= SMBIOS_HANDLE_PI_RESERVED
;
299 Status
= gSmbios
->GetNext (gSmbios
, SmbiosHandle
, &Type
, &Record
, NULL
);
300 if (!EFI_ERROR (Status
)) {
301 if (Match
== Instance
) {
302 return (SMBIOS_STRUCTURE
*)Record
;
306 } while (!EFI_ERROR (Status
));
313 Remove an SMBIOS record.
315 This function removes an SMBIOS record using the handle specified by SmbiosHandle.
317 @param[in] SmbiosHandle The handle of the SMBIOS record to remove.
319 @retval EFI_SUCCESS SMBIOS record was removed.
320 @retval EFI_INVALID_PARAMETER SmbiosHandle does not specify a valid SMBIOS record.
325 OUT EFI_SMBIOS_HANDLE SmbiosHandle
328 return gSmbios
->Remove (gSmbios
, SmbiosHandle
);
335 @param ImageHandle ImageHandle of the loaded driver.
336 @param SystemTable Pointer to the EFI System Table.
338 @retval EFI_SUCCESS Register successfully.
339 @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.
343 SmbiosLibConstructor (
344 IN EFI_HANDLE ImageHandle
,
345 IN EFI_SYSTEM_TABLE
*SystemTable
348 return gBS
->LocateProtocol (&gEfiSmbiosProtocolGuid
, NULL
, (VOID
**)&gSmbios
);