xref: /DragonStub/lib/smbios.c (revision 530d68ba191850edafc6da22cb2df55bec0c5fa5)
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 #pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
26 
27 EFI_STATUS
28 LibGetSmbiosSystemGuidAndSerialNumber (
29     IN  EFI_GUID    *SystemGuid,
30     OUT CHAR8       **SystemSerialNumber
31     )
32 {
33     EFI_STATUS                  Status;
34     SMBIOS_STRUCTURE_TABLE      *SmbiosTable;
35     SMBIOS_STRUCTURE_POINTER    Smbios;
36     SMBIOS_STRUCTURE_POINTER    SmbiosEnd;
37     UINT16                      Index;
38 
39     Status = LibGetSystemConfigurationTable(&SMBIOSTableGuid, (VOID**)&SmbiosTable);
40     if (EFI_ERROR(Status)) {
41         return EFI_NOT_FOUND;
42     }
43 
44     Smbios.Hdr = (SMBIOS_HEADER *)SmbiosTable->TableAddress;
45     SmbiosEnd.Raw = (UINT8 *)(SmbiosTable->TableAddress + SmbiosTable->TableLength);
46     for (Index = 0; Index < SmbiosTable->TableLength ; Index++) {
47         if (Smbios.Hdr->Type == 1) {
48             if (Smbios.Hdr->Length < 0x19) {
49                 //
50                 // Older version did not support Guid and Serial number
51                 //
52                 continue;
53             }
54 
55             //
56             // SMBIOS tables are byte packed so we need to do a byte copy to
57             //  prevend alignment faults on IA-64.
58 
59             CopyMem (SystemGuid, &Smbios.Type1->Uuid, sizeof(EFI_GUID));
60             *SystemSerialNumber = LibGetSmbiosString(&Smbios, Smbios.Type1->SerialNumber);
61             return EFI_SUCCESS;
62         }
63 
64         //
65         // Make Smbios point to the next record
66         //
67         LibGetSmbiosString (&Smbios, -1);
68 
69         if (Smbios.Raw >= SmbiosEnd.Raw) {
70             //
71             // SMBIOS 2.1 incorrectly stated the length of SmbiosTable as 0x1e.
72             //  given this we must double check against the lenght of
73             /// the structure. My home PC has this bug.ruthard
74             //
75             return EFI_SUCCESS;
76         }
77     }
78 
79     return EFI_SUCCESS;
80 }
81 
82 CHAR8*
83 LibGetSmbiosString (
84     IN  SMBIOS_STRUCTURE_POINTER    *Smbios,
85     IN  UINT16                      StringNumber
86     )
87 /*++
88 
89     Return SMBIOS string given the string number.
90 
91     Arguments:
92         Smbios - Pointer to SMBIOS structure
93         StringNumber - String number to return. -1 is used to skip all strings and
94             point to the next SMBIOS structure.
95 
96     Returns:
97         Pointer to string, or pointer to next SMBIOS strcuture if StringNumber == -1
98 --*/
99 {
100     UINT16  Index;
101     CHAR8   *String;
102 
103     //
104     // Skip over formatted section
105     //
106     String = (CHAR8 *)(Smbios->Raw + Smbios->Hdr->Length);
107 
108     //
109     // Look through unformated section
110     //
111     for (Index = 1; Index <= StringNumber; Index++) {
112         if (StringNumber == Index) {
113             return String;
114         }
115 
116         //
117         // Skip string
118         //
119         for (; *String != 0; String++);
120         String++;
121 
122         if (*String == 0) {
123             //
124             // If double NULL then we are done.
125             //  Retrun pointer to next structure in Smbios.
126             //  if you pass in a -1 you will always get here
127             //
128             Smbios->Raw = (UINT8 *)++String;
129             return NULL;
130         }
131     }
132     return NULL;
133 }
134