The Section 6.1.3, SMBIOS specification version 3.6.0 describes the
handling of test strings in SMBIOS tables.
Text strings are added at the end of the formatted portion of the SMBIOS
structure and are referenced by index in the SMBIOS structure.
Therefore, introduce a SmbiosStringTableLib to simplify the publishing
of the string set.
SmbiosStringTableLib introduces a concept of string table which records
the references to the SMBIOS strings as they are added and returns an
string reference which is then assigned to the string field in the
formatted portion of the SMBIOS structure. Once all strings are added,
the library provides an interface to get the required size for the string
set. This allows sufficient memory to be allocated for the SMBIOS table.
The library also provides an interface to publish the string set in
accordance with the SMBIOS specification.
Example:
EFI_STATUS
BuildSmbiosType17Table () {
  STRING_TABLE         StrTable;
  UINT8                DevLocatorRef;
  UINT8                BankLocatorRef;
  SMBIOS_TABLE_TYPE17  *SmbiosRecord;
  CHAR8                *StringSet;
  ...
  // Initialize string table for 7 strings
  StringTableInitialize (&StrTable, 7);
  StringTableAddString (&StrTable, "SIMM 3", &DevLocatorRef);
  StringTableAddString (&StrTable, "Bank 0", &BankLocatorRef);
  ...
  SmbiosRecord = AllocateZeroPool (
                   sizeof (SMBIOS_TABLE_TYPE17) +
                     StringTableGetStringSetSize (&StrTable)
                   );
  ...
  SmbiosRecord->DeviceLocator = DevLocatorRef;
  SmbiosRecord->BankLocator = BankLocatorRef;
  ...
  // get the string set area
  StringSet = (CHAR8*)(SmbiosRecord + 1);
  // publish the string set
  StringTablePublishStringSet (
    &StrTable,
    StringSet,
    StringTableGetStringSetSize (&StrTable)
    );
  // free string table
  StringTableFree (&StrTable);
  return EFI_SUCCESS;
}
Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
Reviewed-by: Pierre Gondois <pierre.gondois@arm.com>
Cc: Alexei Fedorov <Alexei.Fedorov@arm.com>
Cc: Pierre Gondois <pierre.gondois@arm.com>
Cc: Girish Mahadevan <gmahadevan@nvidia.com>
Cc: Jeff Brasen <jbrasen@nvidia.com>
Cc: Ashish Singhal <ashishsingha@nvidia.com>
Cc: Nick Ramirez <nramirez@nvidia.com>
Cc: William Watson <wwatson@nvidia.com>
Cc: Samer El-Haj-Mahmoud <Samer.El-Haj-Mahmoud@arm.com>
		
	
		
			
				
	
	
		
			120 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			120 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| /** @file
 | |
|   SMBIOS String Table Helper library.
 | |
| 
 | |
|   Copyright (c) 2022, Arm Limited. All rights reserved.<BR>
 | |
|   SPDX-License-Identifier: BSD-2-Clause-Patent
 | |
| **/
 | |
| 
 | |
| #ifndef SMBIOS_STRING_TABLE_H_
 | |
| #define SMBIOS_STRING_TABLE_H_
 | |
| 
 | |
| /** A structure representing a string in the string table.
 | |
| */
 | |
| typedef struct StringElement {
 | |
|   /// Length of the string (does not include the NULL termination)
 | |
|   UINTN          StringLen;
 | |
| 
 | |
|   /// Reference to the string
 | |
|   CONST CHAR8    *String;
 | |
| } STRING_ELEMENT;
 | |
| 
 | |
| /** A structure representing a string table.
 | |
| */
 | |
| typedef struct StringTable {
 | |
|   /// Count of strings in the table
 | |
|   UINT8             StrCount;
 | |
| 
 | |
|   /// Total length of all strings in the table (does not include
 | |
|   // the NULL termination for each string)
 | |
|   UINTN             TotalStrLen;
 | |
| 
 | |
|   /// Maximum string count
 | |
|   UINT8             MaxStringElements;
 | |
| 
 | |
|   /// Pointer to the string table elements
 | |
|   STRING_ELEMENT    *Elements;
 | |
| } STRING_TABLE;
 | |
| 
 | |
| /** Add a string to the string table
 | |
| 
 | |
|   @param[in]   StrTable  Pointer to the string table
 | |
|   @param[in]   Str       Pointer to the string
 | |
|   @param[out]  StrRef    Optional pointer to retrieve the string field
 | |
|                          reference of the string in the string table
 | |
| 
 | |
|   @return EFI_SUCCESS            Success
 | |
|   @return EFI_INVALID_PARAMETER  Invalid string table pointer
 | |
|   @return EFI_BUFFER_TOO_SMALL   Insufficient space to add string
 | |
| **/
 | |
| EFI_STATUS
 | |
| EFIAPI
 | |
| StringTableAddString (
 | |
|   IN        STRING_TABLE *CONST  StrTable,
 | |
|   IN  CONST CHAR8                *Str,
 | |
|   OUT       UINT8                *StrRef      OPTIONAL
 | |
|   );
 | |
| 
 | |
| /** Returns the total size required to publish the strings to the SMBIOS
 | |
|     string area.
 | |
| 
 | |
|   @param[in] StrTable              Pointer to the string table
 | |
| 
 | |
|   @return Total size required to publish the strings in the SMBIOS string area.
 | |
| **/
 | |
| UINTN
 | |
| EFIAPI
 | |
| StringTableGetStringSetSize (
 | |
|   IN  STRING_TABLE *CONST  StrTable
 | |
|   );
 | |
| 
 | |
| /** Iterate through the string table and publish the strings in the SMBIOS
 | |
|     string area.
 | |
| 
 | |
|   @param[in] StrTable              Pointer to the string table
 | |
|   @param[in] SmbiosStringAreaStart Start address of the SMBIOS string area.
 | |
|   @param[in] SmbiosStringAreaSize  Size of the SMBIOS string area.
 | |
| 
 | |
|   @return EFI_SUCCESS            Success
 | |
|   @return EFI_INVALID_PARAMETER  Invalid string table pointer
 | |
|   @return EFI_BUFFER_TOO_SMALL   Insufficient space to publish strings
 | |
| **/
 | |
| EFI_STATUS
 | |
| EFIAPI
 | |
| StringTablePublishStringSet (
 | |
|   IN        STRING_TABLE  *CONST  StrTable,
 | |
|   IN        CHAR8         *CONST  SmbiosStringAreaStart,
 | |
|   IN  CONST UINTN                 SmbiosStringAreaSize
 | |
|   );
 | |
| 
 | |
| /** Initialise the string table and allocate memory for the string elements.
 | |
| 
 | |
|   @param[in] StrTable           Pointer to the string table
 | |
|   @param[in] MaxStringElements  Maximum number of strings that the string
 | |
|                                  table can hold.
 | |
| 
 | |
|   @return EFI_SUCCESS            Success
 | |
|   @return EFI_INVALID_PARAMETER  Invalid string table pointer
 | |
|   @return EFI_OUT_OF_RESOURCES   Failed to allocate memory for string elements
 | |
| **/
 | |
| EFI_STATUS
 | |
| EFIAPI
 | |
| StringTableInitialize (
 | |
|   IN STRING_TABLE *CONST  StrTable,
 | |
|   IN UINTN                MaxStringElements
 | |
|   );
 | |
| 
 | |
| /** Free memory allocated for the string elements in the string table.
 | |
| 
 | |
|   @param[in] StrTable           Pointer to the string table
 | |
| 
 | |
|   @return EFI_SUCCESS            Success
 | |
|   @return EFI_INVALID_PARAMETER  Invalid string table pointer or string elements
 | |
| **/
 | |
| EFI_STATUS
 | |
| EFIAPI
 | |
| StringTableFree (
 | |
|   IN STRING_TABLE *CONST  StrTable
 | |
|   );
 | |
| 
 | |
| #endif // SMBIOS_STRING_TABLE_H_
 |