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 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
;
55 for (Index
= 0; Template
[Index
].Entry
!= NULL
; Index
++) {
56 Status
= SmbiosLibCreateEntry (Template
[Index
].Entry
, Template
[Index
].StringArray
);
67 Converts a fixed SMBIOS structure and an array of pointers to strings into
68 an SMBIOS record where the strings are cat'ed on the end of the fixed record
69 and terminated via a double NULL and add to SMBIOS table.
71 SMBIOS_TABLE_TYPE32 gSmbiosType12 = {
72 { EFI_SMBIOS_TYPE_SYSTEM_CONFIGURATION_OPTIONS, sizeof (SMBIOS_TABLE_TYPE12), 0 },
75 CHAR8 *gSmbiosType12Strings[] = {
82 (EFI_SMBIOS_TABLE_HEADER*)&gSmbiosType12,
86 @param SmbiosEntry Fixed SMBIOS structure
87 @param StringArray Array of strings to convert to an SMBIOS string pack.
93 SmbiosLibCreateEntry (
94 IN SMBIOS_STRUCTURE
*SmbiosEntry
,
95 IN CHAR8
**StringArray
99 EFI_SMBIOS_HANDLE SmbiosHandle
;
100 EFI_SMBIOS_TABLE_HEADER
*Record
;
106 // Calculate the size of the fixed record and optional string pack
107 Size
= SmbiosEntry
->Length
;
108 if (StringArray
== NULL
) {
109 Size
+= 2; // Min string section is double null
110 } else if (StringArray
[0] == NULL
) {
111 Size
+= 2; // Min string section is double null
113 for (Index
= 0; StringArray
[Index
] != NULL
; Index
++) {
114 StringSize
= AsciiStrSize (StringArray
[Index
]);
117 // Don't forget the terminating double null
121 // Copy over Template
122 Record
= (EFI_SMBIOS_TABLE_HEADER
*)AllocateZeroPool (Size
);
123 if (Record
== NULL
) {
124 return EFI_OUT_OF_RESOURCES
;
126 CopyMem (Record
, SmbiosEntry
, SmbiosEntry
->Length
);
128 if (StringArray
!= NULL
) {
129 // Append string pack
130 Str
= ((CHAR8
*)Record
) + Record
->Length
;
131 for (Index
= 0; StringArray
[Index
] != NULL
; Index
++) {
132 StringSize
= AsciiStrSize (StringArray
[Index
]);
133 CopyMem (Str
, StringArray
[Index
], StringSize
);
139 SmbiosHandle
= SMBIOS_HANDLE_PI_RESERVED
;
140 Status
= gSmbios
->Add (
154 Update the string associated with an existing SMBIOS record.
156 This function allows the update of specific SMBIOS strings. The number of valid strings for any
157 SMBIOS record is defined by how many strings were present when Add() was called.
159 @param[in] SmbiosHandle SMBIOS Handle of structure that will have its string updated.
160 @param[in] StringNumber The non-zero string number of the string to update.
161 @param[in] String Update the StringNumber string with String.
163 @retval EFI_SUCCESS SmbiosHandle had its StringNumber String updated.
164 @retval EFI_INVALID_PARAMETER SmbiosHandle does not exist. Or String is invalid.
165 @retval EFI_UNSUPPORTED String was not added because it is longer than the SMBIOS Table supports.
166 @retval EFI_NOT_FOUND The StringNumber.is not valid for this SMBIOS record.
170 SmbiosLibUpdateString (
171 IN EFI_SMBIOS_HANDLE SmbiosHandle
,
172 IN SMBIOS_TABLE_STRING StringNumber
,
178 if (String
== NULL
) {
179 return EFI_INVALID_PARAMETER
;
182 if (*String
== '\0') {
183 // A string with no data is not legal in SMBIOS
184 return EFI_INVALID_PARAMETER
;
187 StringIndex
= StringNumber
;
188 return gSmbios
->UpdateString (gSmbios
, &SmbiosHandle
, &StringIndex
, String
);
193 Update the string associated with an existing SMBIOS record.
195 This function allows the update of specific SMBIOS strings. The number of valid strings for any
196 SMBIOS record is defined by how many strings were present when Add() was called.
198 @param[in] SmbiosHandle SMBIOS Handle of structure that will have its string updated.
199 @param[in] StringNumber The non-zero string number of the string to update.
200 @param[in] String Update the StringNumber string with String.
202 @retval EFI_SUCCESS SmbiosHandle had its StringNumber String updated.
203 @retval EFI_INVALID_PARAMETER SmbiosHandle does not exist. Or String is invalid.
204 @retval EFI_UNSUPPORTED String was not added because it is longer than the SMBIOS Table supports.
205 @retval EFI_NOT_FOUND The StringNumber.is not valid for this SMBIOS record.
209 SmbiosLibUpdateUnicodeString (
210 IN EFI_SMBIOS_HANDLE SmbiosHandle
,
211 IN SMBIOS_TABLE_STRING StringNumber
,
219 if (String
== NULL
) {
220 return EFI_INVALID_PARAMETER
;
223 if (*String
== '\0') {
224 // A string with no data is not legal in SMBIOS
225 return EFI_INVALID_PARAMETER
;
228 Ascii
= AllocateZeroPool (StrSize (String
));
230 return EFI_OUT_OF_RESOURCES
;
232 UnicodeStrToAsciiStr (String
, Ascii
);
234 StringIndex
= StringNumber
;
235 Status
= gSmbios
->UpdateString (gSmbios
, &SmbiosHandle
, &StringIndex
, Ascii
);
243 Allow caller to read a specific SMBIOS string
245 @param[in] Header SMBIOS record that contains the string.
246 @param[in[ StringNumber Instance of SMBIOS string 1 - N.
248 @retval NULL Instance of Type SMBIOS string was not found.
249 @retval Other Pointer to matching SMBIOS string.
253 SmbiosLibReadString (
254 IN SMBIOS_STRUCTURE
*Header
,
255 IN EFI_SMBIOS_STRING StringNumber
261 Data
= (CHAR8
*)Header
+ Header
->Length
;
262 for (Match
= 1;!(*Data
== 0 && *(Data
+1) == 0); ) {
263 if (StringNumber
== Match
) {
267 if (*(Data
- 1) == '\0') {
277 Allow the caller to discover a specific SMBIOS entry, and patch it if necissary.
279 @param[in] Type Type of the next SMBIOS record to return.
280 @param[in[ Instance Instance of SMBIOS record 0 - N-1.
281 @param[out] SmbiosHandle Returns SMBIOS handle for the matching record.
283 @retval NULL Instance of Type SMBIOS record was not found.
284 @retval Other Pointer to matching SMBIOS record.
289 IN EFI_SMBIOS_TYPE Type
,
291 OUT EFI_SMBIOS_HANDLE
*SmbiosHandle
295 EFI_SMBIOS_TABLE_HEADER
*Record
;
299 *SmbiosHandle
= SMBIOS_HANDLE_PI_RESERVED
;
301 Status
= gSmbios
->GetNext (gSmbios
, SmbiosHandle
, &Type
, &Record
, NULL
);
302 if (!EFI_ERROR (Status
)) {
303 if (Match
== Instance
) {
304 return (SMBIOS_STRUCTURE
*)Record
;
308 } while (!EFI_ERROR (Status
));
315 Remove an SMBIOS record.
317 This function removes an SMBIOS record using the handle specified by SmbiosHandle.
319 @param[in] SmbiosHandle The handle of the SMBIOS record to remove.
321 @retval EFI_SUCCESS SMBIOS record was removed.
322 @retval EFI_INVALID_PARAMETER SmbiosHandle does not specify a valid SMBIOS record.
327 OUT EFI_SMBIOS_HANDLE SmbiosHandle
330 return gSmbios
->Remove (gSmbios
, SmbiosHandle
);
337 @param ImageHandle ImageHandle of the loaded driver.
338 @param SystemTable Pointer to the EFI System Table.
340 @retval EFI_SUCCESS Register successfully.
341 @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.
345 SmbiosLibConstructor (
346 IN EFI_HANDLE ImageHandle
,
347 IN EFI_SYSTEM_TABLE
*SystemTable
350 return gBS
->LocateProtocol (&gEfiSmbiosProtocolGuid
, NULL
, (VOID
**)&gSmbios
);