123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- /*++
- Copyright (c) 2014 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- smbios.c
- Abstract:
- This module implements support for adding SMBIOS tables to the firmware.
- Author:
- Evan Green 7-May-2014
- Environment:
- Firmware
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include "ueficore.h"
- #include <minoca/fw/smbios.h>
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- VOID
- EfipSmbiosChecksumTable (
- VOID *Buffer,
- UINTN Size,
- UINTN ChecksumOffset
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // Store a pointer to the SMBIOS entry point structure.
- //
- VOID *EfiSmbiosEntryPoint;
- UINTN EfiSmbiosAllocationSize;
- UINTN EfiSmbiosPageCount;
- EFI_GUID EfiSmbiosTableGuid = EFI_SMBIOS_TABLE_GUID;
- SMBIOS_ENTRY_POINT EfiSmbiosEntryPointTemplate = {
- SMBIOS_ANCHOR_STRING_VALUE,
- 0,
- 0x1F,
- 2,
- 8,
- 0,
- 0,
- {0},
- {'_', 'D', 'M', 'I', '_'},
- 0,
- 0,
- 0,
- 0,
- 0x28
- };
- //
- // ------------------------------------------------------------------ Functions
- //
- EFIAPI
- EFI_STATUS
- EfiSmbiosDriverEntry (
- EFI_HANDLE ImageHandle,
- EFI_SYSTEM_TABLE *SystemTable
- )
- /*++
- Routine Description:
- This routine is the entry point into the SMBIOS driver.
- Arguments:
- ImageHandle - Supplies the driver image handle.
- SystemTable - Supplies a pointer to the EFI system table.
- Return Value:
- EFI status code.
- --*/
- {
- return EFI_SUCCESS;
- }
- EFIAPI
- EFI_STATUS
- EfiSmbiosAddStructure (
- VOID *Table,
- ...
- )
- /*++
- Routine Description:
- This routine adds an SMBIOS structure to the SMBIOS table.
- Arguments:
- Table - Supplies a pointer to the table to add. A copy of this data will be
- made. The length of the table must be correctly filled in.
- ... - Supplies an array of pointers to strings to add to the end of the
- table. This list must be terminated with a NULL.
- Return Value:
- EFI_SUCCESS on success.
- EFI_INSUFFICIENT_RESOURCES if a memory allocation failed.
- --*/
- {
- EFI_PHYSICAL_ADDRESS Allocation;
- UINTN AllocationSize;
- CHAR8 *Argument;
- VA_LIST ArgumentList;
- UINTN ChecksumOffset;
- CHAR8 *CurrentPointer;
- PSMBIOS_ENTRY_POINT EntryPoint;
- UINTN Offset;
- UINTN PageCount;
- EFI_STATUS Status;
- UINTN StringLength;
- UINTN StringsLength;
- UINTN StructureSize;
- PSMBIOS_HEADER TableHeader;
- //
- // Loop through the arguments once to count the string lengths.
- //
- StringsLength = 1;
- VA_START(ArgumentList, Table);
- Argument = VA_ARG(ArgumentList, CHAR8 *);
- while (Argument != NULL) {
- StringsLength += RtlStringLength(Argument) + 1;
- Argument = VA_ARG(ArgumentList, CHAR8 *);
- }
- VA_END(ArgumentList);
- if (StringsLength < 2) {
- StringsLength = 2;
- }
- //
- // Compute the total length of the new table.
- //
- TableHeader = Table;
- StructureSize = TableHeader->Length + StringsLength;
- AllocationSize = EfiSmbiosAllocationSize + StructureSize;
- if (EfiSmbiosAllocationSize == 0) {
- AllocationSize += sizeof(SMBIOS_ENTRY_POINT);
- }
- PageCount = EFI_SIZE_TO_PAGES(AllocationSize);
- //
- // Allocate more pages if needed.
- //
- if (PageCount > EfiSmbiosPageCount) {
- Status = EfiAllocatePages(AllocateAnyPages,
- EfiACPIReclaimMemory,
- PageCount,
- &Allocation);
- if (EFI_ERROR(Status)) {
- return Status;
- }
- //
- // If this is the first table, copy the template over.
- //
- if (EfiSmbiosAllocationSize == 0) {
- EfiCopyMem((VOID *)(UINTN)Allocation,
- &EfiSmbiosEntryPointTemplate,
- sizeof(SMBIOS_ENTRY_POINT));
- EfipSmbiosChecksumTable(
- (VOID *)(UINTN)Allocation,
- EfiSmbiosEntryPointTemplate.EntryPointLength,
- OFFSET_OF(SMBIOS_ENTRY_POINT, Checksum));
- EfiSmbiosAllocationSize = sizeof(SMBIOS_ENTRY_POINT);
- //
- // Copy the existing tables, and free the old memory.
- //
- } else {
- EfiCopyMem((VOID *)(UINTN)Allocation,
- EfiSmbiosEntryPoint,
- EfiSmbiosAllocationSize);
- EfiFreePages((EFI_PHYSICAL_ADDRESS)(UINTN)EfiSmbiosEntryPoint,
- EfiSmbiosPageCount);
- }
- EfiSmbiosEntryPoint = (VOID *)(UINTN)Allocation;
- EfiSmbiosPageCount = PageCount;
- //
- // Install the new table.
- //
- Status = EfiInstallConfigurationTable(&EfiSmbiosTableGuid,
- EfiSmbiosEntryPoint);
- if (EFI_ERROR(Status)) {
- return Status;
- }
- }
- //
- // Copy the new structure onto the end.
- //
- RtlCopyMemory(EfiSmbiosEntryPoint + EfiSmbiosAllocationSize,
- TableHeader,
- TableHeader->Length);
- StringsLength = 0;
- CurrentPointer = EfiSmbiosEntryPoint +
- EfiSmbiosAllocationSize +
- TableHeader->Length;
- VA_START(ArgumentList, Table);
- Argument = VA_ARG(ArgumentList, CHAR8 *);
- while (Argument != NULL) {
- StringLength = RtlStringLength(Argument) + 1;
- EfiCopyMem(CurrentPointer, Argument, StringLength);
- StringsLength += StringLength;
- CurrentPointer += StringLength;
- Argument = VA_ARG(ArgumentList, CHAR8 *);
- }
- VA_END(ArgumentList);
- *CurrentPointer = '\0';
- CurrentPointer += 1;
- StringsLength += 1;
- if (StringsLength < 2) {
- *CurrentPointer = '\0';
- StringsLength += 1;
- }
- //
- // Update the header.
- //
- EfiSmbiosAllocationSize += StructureSize;
- EntryPoint = EfiSmbiosEntryPoint;
- EntryPoint->NumberOfStructures += 1;
- if (EntryPoint->MaxStructureSize < StructureSize) {
- EntryPoint->MaxStructureSize = StructureSize;
- }
- EntryPoint->StructureTableLength = EfiSmbiosAllocationSize -
- sizeof(SMBIOS_ENTRY_POINT);
- EntryPoint->StructureTableAddress =
- (UINT32)(UINTN)(EfiSmbiosEntryPoint +
- sizeof(SMBIOS_ENTRY_POINT));
- Offset = OFFSET_OF(SMBIOS_ENTRY_POINT, IntermediateAnchor);
- ChecksumOffset = OFFSET_OF(SMBIOS_ENTRY_POINT, IntermediateChecksum) -
- Offset;
- EfipSmbiosChecksumTable(EfiSmbiosEntryPoint + Offset,
- sizeof(SMBIOS_ENTRY_POINT) - Offset,
- ChecksumOffset);
- return EFI_SUCCESS;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- VOID
- EfipSmbiosChecksumTable (
- VOID *Buffer,
- UINTN Size,
- UINTN ChecksumOffset
- )
- /*++
- Routine Description:
- This routine checksums the SMBIOS entry point.
- Arguments:
- Buffer - Supplies a pointer to the table to checksum.
- Size - Supplies the size of the table in bytes.
- ChecksumOffset - Supplies the offset of the 8 bit checksum field.
- Return Value:
- None.
- --*/
- {
- UINT8 *Pointer;
- UINT8 Sum;
- Sum = 0;
- Pointer = Buffer;
- Pointer[ChecksumOffset] = 0;
- while (Size != 0) {
- Sum = (UINT8)(Sum + *Pointer);
- Pointer += 1;
- Size -= 1;
- }
- Pointer = Buffer;
- Pointer[ChecksumOffset] = (UINT8)(0xFF - Sum + 1);
- return;
- }
|