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