1/*	$NetBSD: smbios.c,v 1.1.1.3 2021/09/30 18:50:09 jmcneill Exp $	*/
2
3/*++
4
5Copyright (c) 2000  Intel Corporation
6
7Module Name:
8
9    Smbios.c
10
11Abstract:
12
13    Lib fucntions for SMBIOS. Used to get system serial number and GUID
14
15Revision History
16
17--*/
18
19#include "lib.h"
20
21/*
22 * We convert 32 bit values to pointers. In 64 bit mode the compiler will issue a
23 * warning stating that the value is too small for the pointer:
24 * "warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]"
25 * we can safely ignore them here.
26 */
27#ifdef __GNUC__
28#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
29#endif
30
31EFI_STATUS
32LibGetSmbiosSystemGuidAndSerialNumber (
33    IN  EFI_GUID    *SystemGuid,
34    OUT CHAR8       **SystemSerialNumber
35    )
36{
37    EFI_STATUS                  Status;
38    SMBIOS_STRUCTURE_TABLE      *SmbiosTable;
39    SMBIOS_STRUCTURE_POINTER    Smbios;
40    SMBIOS_STRUCTURE_POINTER    SmbiosEnd;
41    UINT16                      Index;
42
43    Status = LibGetSystemConfigurationTable(&SMBIOSTableGuid, (VOID**)&SmbiosTable);
44    if (EFI_ERROR(Status)) {
45        return EFI_NOT_FOUND;
46    }
47
48    Smbios.Hdr = (SMBIOS_HEADER *)SmbiosTable->TableAddress;
49    SmbiosEnd.Raw = (UINT8 *)((UINTN)SmbiosTable->TableAddress + SmbiosTable->TableLength);
50    for (Index = 0; Index < SmbiosTable->TableLength ; Index++) {
51        if (Smbios.Hdr->Type == 1) {
52            if (Smbios.Hdr->Length < 0x19) {
53                //
54                // Older version did not support Guid and Serial number
55                //
56                continue;
57            }
58
59            //
60            // SMBIOS tables are byte packed so we need to do a byte copy to
61            //  prevend alignment faults on IA-64.
62
63            CopyMem (SystemGuid, &Smbios.Type1->Uuid, sizeof(EFI_GUID));
64            *SystemSerialNumber = LibGetSmbiosString(&Smbios, Smbios.Type1->SerialNumber);
65            return EFI_SUCCESS;
66        }
67
68        //
69        // Make Smbios point to the next record
70        //
71        LibGetSmbiosString (&Smbios, -1);
72
73        if (Smbios.Raw >= SmbiosEnd.Raw) {
74            //
75            // SMBIOS 2.1 incorrectly stated the length of SmbiosTable as 0x1e.
76            //  given this we must double check against the lenght of
77            /// the structure. My home PC has this bug.ruthard
78            //
79            return EFI_SUCCESS;
80        }
81    }
82
83    return EFI_SUCCESS;
84}
85
86CHAR8*
87LibGetSmbiosString (
88    IN  SMBIOS_STRUCTURE_POINTER    *Smbios,
89    IN  UINT16                      StringNumber
90    )
91/*++
92
93    Return SMBIOS string given the string number.
94
95    Arguments:
96        Smbios - Pointer to SMBIOS structure
97        StringNumber - String number to return. -1 is used to skip all strings and
98            point to the next SMBIOS structure.
99
100    Returns:
101        Pointer to string, or pointer to next SMBIOS strcuture if StringNumber == -1
102--*/
103{
104    UINT16  Index;
105    CHAR8   *String;
106
107    //
108    // Skip over formatted section
109    //
110    String = (CHAR8 *)(Smbios->Raw + Smbios->Hdr->Length);
111
112    //
113    // Look through unformated section
114    //
115    for (Index = 1; Index <= StringNumber; Index++) {
116        if (StringNumber == Index) {
117            return String;
118        }
119
120        //
121        // Skip string
122        //
123        for (; *String != 0; String++);
124        String++;
125
126        if (*String == 0) {
127            //
128            // If double NULL then we are done.
129            //  Retrun pointer to next structure in Smbios.
130            //  if you pass in a -1 you will always get here
131            //
132            Smbios->Raw = (UINT8 *)++String;
133            return NULL;
134        }
135    }
136    return NULL;
137}
138