xref: /DragonStub/lib/smbios.c (revision 4ef183353cd98e8d0e9ef0c5bbe7783ff10da891)
1530d68baSNigel Croxon /*++
2530d68baSNigel Croxon 
3530d68baSNigel Croxon Copyright (c) 2000  Intel Corporation
4530d68baSNigel Croxon 
5530d68baSNigel Croxon Module Name:
6530d68baSNigel Croxon 
7530d68baSNigel Croxon     Smbios.c
8530d68baSNigel Croxon 
9530d68baSNigel Croxon Abstract:
10530d68baSNigel Croxon 
11530d68baSNigel Croxon     Lib fucntions for SMBIOS. Used to get system serial number and GUID
12530d68baSNigel Croxon 
13530d68baSNigel Croxon Revision History
14530d68baSNigel Croxon 
15530d68baSNigel Croxon --*/
16530d68baSNigel Croxon 
17530d68baSNigel Croxon #include "lib.h"
18530d68baSNigel Croxon 
19530d68baSNigel Croxon /*
20530d68baSNigel Croxon  * We convert 32 bit values to pointers. In 64 bit mode the compiler will issue a
21530d68baSNigel Croxon  * warning stating that the value is too small for the pointer:
22530d68baSNigel Croxon  * "warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]"
23530d68baSNigel Croxon  * we can safely ignore them here.
24530d68baSNigel Croxon  */
2509027207SNigel Croxon #ifdef __GNUC__
26530d68baSNigel Croxon #pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
2709027207SNigel Croxon #endif
28530d68baSNigel Croxon 
29530d68baSNigel Croxon EFI_STATUS
LibGetSmbiosSystemGuidAndSerialNumber(IN EFI_GUID * SystemGuid,OUT CHAR8 ** SystemSerialNumber)30530d68baSNigel Croxon LibGetSmbiosSystemGuidAndSerialNumber (
31530d68baSNigel Croxon     IN  EFI_GUID    *SystemGuid,
32530d68baSNigel Croxon     OUT CHAR8       **SystemSerialNumber
33530d68baSNigel Croxon     )
34530d68baSNigel Croxon {
35530d68baSNigel Croxon     EFI_STATUS                  Status;
36530d68baSNigel Croxon     SMBIOS_STRUCTURE_TABLE      *SmbiosTable;
37530d68baSNigel Croxon     SMBIOS_STRUCTURE_POINTER    Smbios;
38530d68baSNigel Croxon     SMBIOS_STRUCTURE_POINTER    SmbiosEnd;
39530d68baSNigel Croxon     UINT16                      Index;
40530d68baSNigel Croxon 
41530d68baSNigel Croxon     Status = LibGetSystemConfigurationTable(&SMBIOSTableGuid, (VOID**)&SmbiosTable);
42530d68baSNigel Croxon     if (EFI_ERROR(Status)) {
43530d68baSNigel Croxon         return EFI_NOT_FOUND;
44530d68baSNigel Croxon     }
45530d68baSNigel Croxon 
46530d68baSNigel Croxon     Smbios.Hdr = (SMBIOS_HEADER *)SmbiosTable->TableAddress;
47*4ef18335SPete Batard     SmbiosEnd.Raw = (UINT8 *)((UINTN)SmbiosTable->TableAddress + SmbiosTable->TableLength);
48530d68baSNigel Croxon     for (Index = 0; Index < SmbiosTable->TableLength ; Index++) {
49530d68baSNigel Croxon         if (Smbios.Hdr->Type == 1) {
50530d68baSNigel Croxon             if (Smbios.Hdr->Length < 0x19) {
51530d68baSNigel Croxon                 //
52530d68baSNigel Croxon                 // Older version did not support Guid and Serial number
53530d68baSNigel Croxon                 //
54530d68baSNigel Croxon                 continue;
55530d68baSNigel Croxon             }
56530d68baSNigel Croxon 
57530d68baSNigel Croxon             //
58530d68baSNigel Croxon             // SMBIOS tables are byte packed so we need to do a byte copy to
59530d68baSNigel Croxon             //  prevend alignment faults on IA-64.
60530d68baSNigel Croxon 
61530d68baSNigel Croxon             CopyMem (SystemGuid, &Smbios.Type1->Uuid, sizeof(EFI_GUID));
62530d68baSNigel Croxon             *SystemSerialNumber = LibGetSmbiosString(&Smbios, Smbios.Type1->SerialNumber);
63530d68baSNigel Croxon             return EFI_SUCCESS;
64530d68baSNigel Croxon         }
65530d68baSNigel Croxon 
66530d68baSNigel Croxon         //
67530d68baSNigel Croxon         // Make Smbios point to the next record
68530d68baSNigel Croxon         //
69530d68baSNigel Croxon         LibGetSmbiosString (&Smbios, -1);
70530d68baSNigel Croxon 
71530d68baSNigel Croxon         if (Smbios.Raw >= SmbiosEnd.Raw) {
72530d68baSNigel Croxon             //
73530d68baSNigel Croxon             // SMBIOS 2.1 incorrectly stated the length of SmbiosTable as 0x1e.
74530d68baSNigel Croxon             //  given this we must double check against the lenght of
75530d68baSNigel Croxon             /// the structure. My home PC has this bug.ruthard
76530d68baSNigel Croxon             //
77530d68baSNigel Croxon             return EFI_SUCCESS;
78530d68baSNigel Croxon         }
79530d68baSNigel Croxon     }
80530d68baSNigel Croxon 
81530d68baSNigel Croxon     return EFI_SUCCESS;
82530d68baSNigel Croxon }
83530d68baSNigel Croxon 
84530d68baSNigel Croxon CHAR8*
LibGetSmbiosString(IN SMBIOS_STRUCTURE_POINTER * Smbios,IN UINT16 StringNumber)85530d68baSNigel Croxon LibGetSmbiosString (
86530d68baSNigel Croxon     IN  SMBIOS_STRUCTURE_POINTER    *Smbios,
87530d68baSNigel Croxon     IN  UINT16                      StringNumber
88530d68baSNigel Croxon     )
89530d68baSNigel Croxon /*++
90530d68baSNigel Croxon 
91530d68baSNigel Croxon     Return SMBIOS string given the string number.
92530d68baSNigel Croxon 
93530d68baSNigel Croxon     Arguments:
94530d68baSNigel Croxon         Smbios - Pointer to SMBIOS structure
95530d68baSNigel Croxon         StringNumber - String number to return. -1 is used to skip all strings and
96530d68baSNigel Croxon             point to the next SMBIOS structure.
97530d68baSNigel Croxon 
98530d68baSNigel Croxon     Returns:
99530d68baSNigel Croxon         Pointer to string, or pointer to next SMBIOS strcuture if StringNumber == -1
100530d68baSNigel Croxon --*/
101530d68baSNigel Croxon {
102530d68baSNigel Croxon     UINT16  Index;
103530d68baSNigel Croxon     CHAR8   *String;
104530d68baSNigel Croxon 
105530d68baSNigel Croxon     //
106530d68baSNigel Croxon     // Skip over formatted section
107530d68baSNigel Croxon     //
108530d68baSNigel Croxon     String = (CHAR8 *)(Smbios->Raw + Smbios->Hdr->Length);
109530d68baSNigel Croxon 
110530d68baSNigel Croxon     //
111530d68baSNigel Croxon     // Look through unformated section
112530d68baSNigel Croxon     //
113530d68baSNigel Croxon     for (Index = 1; Index <= StringNumber; Index++) {
114530d68baSNigel Croxon         if (StringNumber == Index) {
115530d68baSNigel Croxon             return String;
116530d68baSNigel Croxon         }
117530d68baSNigel Croxon 
118530d68baSNigel Croxon         //
119530d68baSNigel Croxon         // Skip string
120530d68baSNigel Croxon         //
121530d68baSNigel Croxon         for (; *String != 0; String++);
122530d68baSNigel Croxon         String++;
123530d68baSNigel Croxon 
124530d68baSNigel Croxon         if (*String == 0) {
125530d68baSNigel Croxon             //
126530d68baSNigel Croxon             // If double NULL then we are done.
127530d68baSNigel Croxon             //  Retrun pointer to next structure in Smbios.
128530d68baSNigel Croxon             //  if you pass in a -1 you will always get here
129530d68baSNigel Croxon             //
130530d68baSNigel Croxon             Smbios->Raw = (UINT8 *)++String;
131530d68baSNigel Croxon             return NULL;
132530d68baSNigel Croxon         }
133530d68baSNigel Croxon     }
134530d68baSNigel Croxon     return NULL;
135530d68baSNigel Croxon }
136