]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Bhyve/SmbiosPlatformDxe/SmbiosPlatformDxe.c
OvmfPkg: Apply uncrustify changes
[mirror_edk2.git] / OvmfPkg / Bhyve / SmbiosPlatformDxe / SmbiosPlatformDxe.c
1 /** @file
2 This driver installs SMBIOS information for OVMF
3
4 Copyright (c) 2020, Rebecca Cran <rebecca@bsdio.com>
5 Copyright (c) 2011, Bei Guan <gbtju85@gmail.com>
6 Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>
7
8 SPDX-License-Identifier: BSD-2-Clause-Patent
9
10 **/
11
12 #include "SmbiosPlatformDxe.h"
13
14 #define TYPE0_STRINGS \
15 "EFI Development Kit II / OVMF\0" /* Vendor */ \
16 "0.0.0\0" /* BiosVersion */ \
17 "02/06/2015\0" /* BiosReleaseDate */
18 //
19 // Type definition and contents of the default Type 0 SMBIOS table.
20 //
21 #pragma pack(1)
22 typedef struct {
23 SMBIOS_TABLE_TYPE0 Base;
24 UINT8 Strings[sizeof (TYPE0_STRINGS)];
25 } OVMF_TYPE0;
26 #pragma pack()
27
28 STATIC CONST OVMF_TYPE0 mOvmfDefaultType0 = {
29 {
30 // SMBIOS_STRUCTURE Hdr
31 {
32 EFI_SMBIOS_TYPE_BIOS_INFORMATION, // UINT8 Type
33 sizeof (SMBIOS_TABLE_TYPE0), // UINT8 Length
34 },
35 1, // SMBIOS_TABLE_STRING Vendor
36 2, // SMBIOS_TABLE_STRING BiosVersion
37 0xE800, // UINT16 BiosSegment
38 3, // SMBIOS_TABLE_STRING BiosReleaseDate
39 0, // UINT8 BiosSize
40 { // MISC_BIOS_CHARACTERISTICS BiosCharacteristics
41 0, // Reserved :2
42 0, // Unknown :1
43 1, // BiosCharacteristicsNotSupported :1
44 // Remaining BiosCharacteristics bits left unset :60
45 },
46 { // BIOSCharacteristicsExtensionBytes[2]
47 0, // BiosReserved
48 0x1C // SystemReserved = VirtualMachineSupported |
49 // UefiSpecificationSupported |
50 // TargetContentDistributionEnabled
51 },
52 0, // UINT8 SystemBiosMajorRelease
53 0, // UINT8 SystemBiosMinorRelease
54 0xFF, // UINT8 EmbeddedControllerFirmwareMajorRelease
55 0xFF // UINT8 EmbeddedControllerFirmwareMinorRelease
56 },
57 // Text strings (unformatted area)
58 TYPE0_STRINGS
59 };
60
61 /**
62 Validates the SMBIOS entry point structure
63
64 @param EntryPointStructure SMBIOS entry point structure
65
66 @retval TRUE The entry point structure is valid
67 @retval FALSE The entry point structure is not valid
68
69 **/
70 BOOLEAN
71 IsEntryPointStructureValid (
72 IN SMBIOS_TABLE_ENTRY_POINT *EntryPointStructure
73 )
74 {
75 UINTN Index;
76 UINT8 Length;
77 UINT8 Checksum;
78 UINT8 *BytePtr;
79
80 BytePtr = (UINT8 *)EntryPointStructure;
81 Length = EntryPointStructure->EntryPointLength;
82 Checksum = 0;
83
84 for (Index = 0; Index < Length; Index++) {
85 Checksum = Checksum + (UINT8)BytePtr[Index];
86 }
87
88 if (Checksum != 0) {
89 return FALSE;
90 } else {
91 return TRUE;
92 }
93 }
94
95 /**
96 Get SMBIOS record length.
97
98 @param SmbiosTable SMBIOS pointer.
99
100 **/
101 UINTN
102 SmbiosTableLength (
103 IN SMBIOS_STRUCTURE_POINTER SmbiosTable
104 )
105 {
106 CHAR8 *AChar;
107 UINTN Length;
108
109 AChar = (CHAR8 *)(SmbiosTable.Raw + SmbiosTable.Hdr->Length);
110
111 //
112 // Each structure shall be terminated by a double-null (SMBIOS spec.7.1)
113 //
114 while ((*AChar != 0) || (*(AChar + 1) != 0)) {
115 AChar++;
116 }
117
118 Length = ((UINTN)AChar - (UINTN)SmbiosTable.Raw + 2);
119
120 return Length;
121 }
122
123 /**
124 Install all structures from the given SMBIOS structures block
125
126 @param Smbios SMBIOS protocol
127 @param TableAddress SMBIOS tables starting address
128
129 **/
130 EFI_STATUS
131 InstallAllStructures (
132 IN EFI_SMBIOS_PROTOCOL *Smbios,
133 IN UINT8 *TableAddress
134 )
135 {
136 EFI_STATUS Status;
137 SMBIOS_STRUCTURE_POINTER SmbiosTable;
138 EFI_SMBIOS_HANDLE SmbiosHandle;
139 BOOLEAN NeedSmbiosType0;
140
141 SmbiosTable.Raw = TableAddress;
142 if (SmbiosTable.Raw == NULL) {
143 return EFI_INVALID_PARAMETER;
144 }
145
146 NeedSmbiosType0 = TRUE;
147
148 while (SmbiosTable.Hdr->Type != 127) {
149 //
150 // Log the SMBIOS data for this structure
151 //
152 SmbiosHandle = SmbiosTable.Hdr->Handle;
153 Status = Smbios->Add (
154 Smbios,
155 NULL,
156 &SmbiosHandle,
157 (EFI_SMBIOS_TABLE_HEADER *)SmbiosTable.Raw
158 );
159 ASSERT_EFI_ERROR (Status);
160
161 if (SmbiosTable.Hdr->Type == 0) {
162 NeedSmbiosType0 = FALSE;
163 }
164
165 //
166 // Get the next structure address
167 //
168 SmbiosTable.Raw = (UINT8 *)(SmbiosTable.Raw + SmbiosTableLength (SmbiosTable));
169 }
170
171 if (NeedSmbiosType0) {
172 //
173 // Add OVMF default Type 0 (BIOS Information) table
174 //
175 SmbiosHandle = SMBIOS_HANDLE_PI_RESERVED;
176 Status = Smbios->Add (
177 Smbios,
178 NULL,
179 &SmbiosHandle,
180 (EFI_SMBIOS_TABLE_HEADER *)&mOvmfDefaultType0
181 );
182 ASSERT_EFI_ERROR (Status);
183 }
184
185 return EFI_SUCCESS;
186 }
187
188 /**
189 Installs SMBIOS information for OVMF
190
191 @param ImageHandle Module's image handle
192 @param SystemTable Pointer of EFI_SYSTEM_TABLE
193
194 @retval EFI_SUCCESS Smbios data successfully installed
195 @retval Other Smbios data was not installed
196
197 **/
198 EFI_STATUS
199 EFIAPI
200 SmbiosTablePublishEntry (
201 IN EFI_HANDLE ImageHandle,
202 IN EFI_SYSTEM_TABLE *SystemTable
203 )
204 {
205 EFI_STATUS Status;
206 EFI_SMBIOS_PROTOCOL *Smbios;
207 SMBIOS_TABLE_ENTRY_POINT *EntryPointStructure;
208 UINT8 *SmbiosTables = NULL;
209
210 //
211 // Find the SMBIOS protocol
212 //
213 Status = gBS->LocateProtocol (
214 &gEfiSmbiosProtocolGuid,
215 NULL,
216 (VOID **)&Smbios
217 );
218 if (EFI_ERROR (Status)) {
219 return Status;
220 }
221
222 //
223 // Add bhyve SMBIOS data
224 //
225 EntryPointStructure = GetBhyveSmbiosTables ();
226 if (EntryPointStructure != NULL) {
227 SmbiosTables = (UINT8 *)(UINTN)EntryPointStructure->TableAddress;
228 }
229
230 if (SmbiosTables != NULL) {
231 Status = InstallAllStructures (Smbios, SmbiosTables);
232
233 //
234 // Free SmbiosTables if allocated by Qemu (i.e., NOT by Xen):
235 //
236 if (EntryPointStructure == NULL) {
237 FreePool (SmbiosTables);
238 }
239 }
240
241 return Status;
242 }