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