VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/DevFwCommon.cpp@ 100658

Last change on this file since 100658 was 100542, checked in by vboxsync, 16 months ago

DevFwCommon: Add an experimental way for using the DmiSystemUuid (with fallback to HW UUID) in strings. Could make sense with constructing unique serial numbers.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 51.4 KB
Line 
1/* $Id: DevFwCommon.cpp 100542 2023-07-12 11:23:07Z vboxsync $ */
2/** @file
3 * FwCommon - Shared firmware code (used by DevPcBios & DevEFI).
4 */
5
6/*
7 * Copyright (C) 2009-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_DEV
33#include <VBox/vmm/pdmdev.h>
34
35#include <VBox/log.h>
36#include <VBox/err.h>
37#include <VBox/param.h>
38
39#include <iprt/asm.h>
40#include <iprt/assert.h>
41#include <iprt/buildconfig.h>
42#include <iprt/file.h>
43#include <iprt/mem.h>
44#include <iprt/string.h>
45#include <iprt/uuid.h>
46#include <iprt/system.h>
47#include <iprt/cdefs.h>
48#include <iprt/alloca.h>
49
50#include "VBoxDD.h"
51#include "VBoxDD2.h"
52#include "DevFwCommon.h"
53
54
55/*********************************************************************************************************************************
56* Defined Constants And Macros *
57*********************************************************************************************************************************/
58
59/*
60 * Default DMI data (legacy).
61 * Don't change this information otherwise Windows guests might demand re-activation!
62 */
63
64/* type 0 -- DMI BIOS information */
65static const int32_t g_iDefDmiBIOSReleaseMajor = 0;
66static const int32_t g_iDefDmiBIOSReleaseMinor = 0;
67static const int32_t g_iDefDmiBIOSFirmwareMajor = 0;
68static const int32_t g_iDefDmiBIOSFirmwareMinor = 0;
69static const char *g_pszDefDmiBIOSVendor = "innotek GmbH";
70static const char *g_pszDefDmiBIOSVersion = "VirtualBox";
71static const char *g_pszDefDmiBIOSReleaseDate = "12/01/2006";
72/* type 1 -- DMI system information */
73static const char *g_pszDefDmiSystemVendor = "innotek GmbH";
74static const char *g_pszDefDmiSystemProduct = "VirtualBox";
75static const char *g_pszDefDmiSystemVersion = "1.2";
76static const char *g_pszDefDmiSystemSerial = "0";
77static const char *g_pszDefDmiSystemSKU = "";
78static const char *g_pszDefDmiSystemFamily = "Virtual Machine";
79/* type 2 -- DMI board information */
80static const char *g_pszDefDmiBoardVendor = "Oracle Corporation";
81static const char *g_pszDefDmiBoardProduct = "VirtualBox";
82static const char *g_pszDefDmiBoardVersion = "1.2";
83static const char *g_pszDefDmiBoardSerial = "0";
84static const char *g_pszDefDmiBoardAssetTag = "";
85static const char *g_pszDefDmiBoardLocInChass = "";
86static const int32_t g_iDefDmiBoardBoardType = 0x0A; /* Motherboard */
87/* type 3 -- DMI chassis information */
88static const char *g_pszDefDmiChassisVendor = "Oracle Corporation";
89static const int32_t g_iDefDmiChassisType = 0x01; /* ''other'', no chassis lock present */
90static const char *g_pszDefDmiChassisVersion = "";
91static const char *g_pszDefDmiChassisSerial = "";
92static const char *g_pszDefDmiChassisAssetTag = "";
93/* type 4 -- DMI processor information */
94static const char *g_pszDefDmiProcManufacturer= "GenuineIntel";
95static const char *g_pszDefDmiProcVersion = "Pentium(R) III";
96
97/** The host DMI system product value, for DmiUseHostInfo=1. */
98static char g_szHostDmiSystemProduct[64];
99/** The host DMI system version value, for DmiUseHostInfo=1. */
100static char g_szHostDmiSystemVersion[64];
101
102
103/*********************************************************************************************************************************
104* Structures and Typedefs *
105*********************************************************************************************************************************/
106#pragma pack(1)
107
108typedef struct SMBIOSHDR
109{
110 uint8_t au8Signature[4];
111 uint8_t u8Checksum;
112 uint8_t u8Eps;
113 uint8_t u8VersionMajor;
114 uint8_t u8VersionMinor;
115 uint16_t u16MaxStructureSize;
116 uint8_t u8EntryPointRevision;
117 uint8_t u8Pad[5];
118} *SMBIOSHDRPTR;
119AssertCompileSize(SMBIOSHDR, 16);
120
121typedef struct DMIMAINHDR
122{
123 uint8_t au8Signature[5];
124 uint8_t u8Checksum;
125 uint16_t u16TablesLength;
126 uint32_t u32TableBase;
127 uint16_t u16TableEntries;
128 uint8_t u8TableVersion;
129} *DMIMAINHDRPTR;
130AssertCompileSize(DMIMAINHDR, 15);
131
132AssertCompile(sizeof(SMBIOSHDR) + sizeof(DMIMAINHDR) <= VBOX_DMI_HDR_SIZE);
133
134/** DMI header */
135typedef struct DMIHDR
136{
137 uint8_t u8Type;
138 uint8_t u8Length;
139 uint16_t u16Handle;
140} *PDMIHDR;
141AssertCompileSize(DMIHDR, 4);
142
143/** DMI BIOS information (Type 0) */
144typedef struct DMIBIOSINF
145{
146 DMIHDR header;
147 uint8_t u8Vendor;
148 uint8_t u8Version;
149 uint16_t u16Start;
150 uint8_t u8Release;
151 uint8_t u8ROMSize;
152 uint64_t u64Characteristics;
153 uint8_t u8CharacteristicsByte1;
154 uint8_t u8CharacteristicsByte2;
155 uint8_t u8ReleaseMajor;
156 uint8_t u8ReleaseMinor;
157 uint8_t u8FirmwareMajor;
158 uint8_t u8FirmwareMinor;
159} *PDMIBIOSINF;
160AssertCompileSize(DMIBIOSINF, 0x18);
161
162/** DMI system information (Type 1) */
163typedef struct DMISYSTEMINF
164{
165 DMIHDR header;
166 uint8_t u8Manufacturer;
167 uint8_t u8ProductName;
168 uint8_t u8Version;
169 uint8_t u8SerialNumber;
170 uint8_t au8Uuid[16];
171 uint8_t u8WakeupType;
172 uint8_t u8SKUNumber;
173 uint8_t u8Family;
174} *PDMISYSTEMINF;
175AssertCompileSize(DMISYSTEMINF, 0x1b);
176
177/** DMI board (or module) information (Type 2) */
178typedef struct DMIBOARDINF
179{
180 DMIHDR header;
181 uint8_t u8Manufacturer;
182 uint8_t u8Product;
183 uint8_t u8Version;
184 uint8_t u8SerialNumber;
185 uint8_t u8AssetTag;
186 uint8_t u8FeatureFlags;
187 uint8_t u8LocationInChass;
188 uint16_t u16ChassisHandle;
189 uint8_t u8BoardType;
190 uint8_t u8cObjectHandles;
191} *PDMIBOARDINF;
192AssertCompileSize(DMIBOARDINF, 0x0f);
193
194/** DMI system enclosure or chassis type (Type 3) */
195typedef struct DMICHASSIS
196{
197 DMIHDR header;
198 uint8_t u8Manufacturer;
199 uint8_t u8Type;
200 uint8_t u8Version;
201 uint8_t u8SerialNumber;
202 uint8_t u8AssetTag;
203 uint8_t u8BootupState;
204 uint8_t u8PowerSupplyState;
205 uint8_t u8ThermalState;
206 uint8_t u8SecurityStatus;
207 /* v2.3+, currently not supported */
208 uint32_t u32OEMdefined;
209 uint8_t u8Height;
210 uint8_t u8NumPowerChords;
211 uint8_t u8ContElems;
212 uint8_t u8ContElemRecLen;
213} *PDMICHASSIS;
214AssertCompileSize(DMICHASSIS, 0x15);
215
216/** DMI processor information (Type 4) */
217typedef struct DMIPROCESSORINF
218{
219 DMIHDR header;
220 uint8_t u8SocketDesignation;
221 uint8_t u8ProcessorType;
222 uint8_t u8ProcessorFamily;
223 uint8_t u8ProcessorManufacturer;
224 uint64_t u64ProcessorID;
225 uint8_t u8ProcessorVersion;
226 uint8_t u8Voltage;
227 uint16_t u16ExternalClock;
228 uint16_t u16MaxSpeed;
229 uint16_t u16CurrentSpeed;
230 uint8_t u8Status;
231 uint8_t u8ProcessorUpgrade;
232 /* v2.1+ */
233 uint16_t u16L1CacheHandle;
234 uint16_t u16L2CacheHandle;
235 uint16_t u16L3CacheHandle;
236 /* v2.3+ */
237 uint8_t u8SerialNumber;
238 uint8_t u8AssetTag;
239 uint8_t u8PartNumber;
240 /* v2.5+ */
241 uint8_t u8CoreCount;
242 uint8_t u8CoreEnabled;
243 uint8_t u8ThreadCount;
244 uint16_t u16ProcessorCharacteristics;
245 /* v2.6+ */
246 uint16_t u16ProcessorFamily2;
247} *PDMIPROCESSORINF;
248AssertCompileSize(DMIPROCESSORINF, 0x2a);
249
250/** DMI OEM strings (Type 11) */
251typedef struct DMIOEMSTRINGS
252{
253 DMIHDR header;
254 uint8_t u8Count;
255 uint8_t u8VBoxVersion;
256 uint8_t u8VBoxRevision;
257} *PDMIOEMSTRINGS;
258AssertCompileSize(DMIOEMSTRINGS, 0x7);
259
260/** DMI OEM-specific table (Type 128) */
261typedef struct DMIOEMSPECIFIC
262{
263 DMIHDR header;
264 uint32_t u32CpuFreqKHz;
265} *PDMIOEMSPECIFIC;
266AssertCompileSize(DMIOEMSPECIFIC, 0x8);
267
268/** Physical memory array (Type 16) */
269typedef struct DMIRAMARRAY
270{
271 DMIHDR header;
272 uint8_t u8Location;
273 uint8_t u8Use;
274 uint8_t u8MemErrorCorrection;
275 uint32_t u32MaxCapacity;
276 uint16_t u16MemErrorHandle;
277 uint16_t u16NumberOfMemDevices;
278} *PDMIRAMARRAY;
279AssertCompileSize(DMIRAMARRAY, 15);
280
281/** DMI Memory Device (Type 17) */
282typedef struct DMIMEMORYDEV
283{
284 DMIHDR header;
285 uint16_t u16PhysMemArrayHandle;
286 uint16_t u16MemErrHandle;
287 uint16_t u16TotalWidth;
288 uint16_t u16DataWidth;
289 uint16_t u16Size;
290 uint8_t u8FormFactor;
291 uint8_t u8DeviceSet;
292 uint8_t u8DeviceLocator;
293 uint8_t u8BankLocator;
294 uint8_t u8MemoryType;
295 uint16_t u16TypeDetail;
296 uint16_t u16Speed;
297 uint8_t u8Manufacturer;
298 uint8_t u8SerialNumber;
299 uint8_t u8AssetTag;
300 uint8_t u8PartNumber;
301 /* v2.6+ */
302 uint8_t u8Attributes;
303 /* v2.7+ */
304 uint32_t u32ExtendedSize;
305 uint16_t u16CfgSpeed; /* Configured speed in MT/sec. */
306} *PDMIMEMORYDEV;
307AssertCompileSize(DMIMEMORYDEV, 34);
308
309/** MPS floating pointer structure */
310typedef struct MPSFLOATPTR
311{
312 uint8_t au8Signature[4];
313 uint32_t u32MPSAddr;
314 uint8_t u8Length;
315 uint8_t u8SpecRev;
316 uint8_t u8Checksum;
317 uint8_t au8Feature[5];
318} *PMPSFLOATPTR;
319AssertCompileSize(MPSFLOATPTR, 16);
320
321/** MPS config table header */
322typedef struct MPSCFGTBLHEADER
323{
324 uint8_t au8Signature[4];
325 uint16_t u16Length;
326 uint8_t u8SpecRev;
327 uint8_t u8Checksum;
328 uint8_t au8OemId[8];
329 uint8_t au8ProductId[12];
330 uint32_t u32OemTablePtr;
331 uint16_t u16OemTableSize;
332 uint16_t u16EntryCount;
333 uint32_t u32AddrLocalApic;
334 uint16_t u16ExtTableLength;
335 uint8_t u8ExtTableChecksum;
336 uint8_t u8Reserved;
337} *PMPSCFGTBLHEADER;
338AssertCompileSize(MPSCFGTBLHEADER, 0x2c);
339
340/** MPS processor entry */
341typedef struct MPSPROCENTRY
342{
343 uint8_t u8EntryType;
344 uint8_t u8LocalApicId;
345 uint8_t u8LocalApicVersion;
346 uint8_t u8CPUFlags;
347 uint32_t u32CPUSignature;
348 uint32_t u32CPUFeatureFlags;
349 uint32_t u32Reserved[2];
350} *PMPSPROCENTRY;
351AssertCompileSize(MPSPROCENTRY, 20);
352
353/** MPS bus entry */
354typedef struct MPSBUSENTRY
355{
356 uint8_t u8EntryType;
357 uint8_t u8BusId;
358 uint8_t au8BusTypeStr[6];
359} *PMPSBUSENTRY;
360AssertCompileSize(MPSBUSENTRY, 8);
361
362/** MPS I/O-APIC entry */
363typedef struct MPSIOAPICENTRY
364{
365 uint8_t u8EntryType;
366 uint8_t u8Id;
367 uint8_t u8Version;
368 uint8_t u8Flags;
369 uint32_t u32Addr;
370} *PMPSIOAPICENTRY;
371AssertCompileSize(MPSIOAPICENTRY, 8);
372
373/** MPS I/O-Interrupt entry */
374typedef struct MPSIOINTERRUPTENTRY
375{
376 uint8_t u8EntryType;
377 uint8_t u8Type;
378 uint16_t u16Flags;
379 uint8_t u8SrcBusId;
380 uint8_t u8SrcBusIrq;
381 uint8_t u8DstIOAPICId;
382 uint8_t u8DstIOAPICInt;
383} *PMPSIOIRQENTRY;
384AssertCompileSize(MPSIOINTERRUPTENTRY, 8);
385
386#pragma pack()
387
388
389/**
390 * Calculate a simple checksum for the MPS table.
391 *
392 * @param au8Data data
393 * @param u32Length size of data
394 */
395static uint8_t fwCommonChecksum(const uint8_t * const au8Data, uint32_t u32Length)
396{
397 uint8_t u8Sum = 0;
398 for (size_t i = 0; i < u32Length; ++i)
399 u8Sum += au8Data[i];
400 return -u8Sum;
401}
402
403#if 0 /* unused */
404static bool fwCommonChecksumOk(const uint8_t * const au8Data, uint32_t u32Length)
405{
406 uint8_t u8Sum = 0;
407 for (size_t i = 0; i < u32Length; i++)
408 u8Sum += au8Data[i];
409 return (u8Sum == 0);
410}
411#endif
412
413/**
414 * Try fetch the DMI strings from the system.
415 */
416static void fwCommonUseHostDMIStrings(void)
417{
418 int rc;
419
420 rc = RTSystemQueryDmiString(RTSYSDMISTR_PRODUCT_NAME,
421 g_szHostDmiSystemProduct, sizeof(g_szHostDmiSystemProduct));
422 if (RT_SUCCESS(rc))
423 {
424 g_pszDefDmiSystemProduct = g_szHostDmiSystemProduct;
425 LogRel(("DMI: Using DmiSystemProduct from host: %s\n", g_szHostDmiSystemProduct));
426 }
427
428 rc = RTSystemQueryDmiString(RTSYSDMISTR_PRODUCT_VERSION,
429 g_szHostDmiSystemVersion, sizeof(g_szHostDmiSystemVersion));
430 if (RT_SUCCESS(rc))
431 {
432 g_pszDefDmiSystemVersion = g_szHostDmiSystemVersion;
433 LogRel(("DMI: Using DmiSystemVersion from host: %s\n", g_szHostDmiSystemVersion));
434 }
435}
436
437/**
438 * Replace the DmiSystemUuid placeholder with the actual value.
439 *
440 * @param pszBuf Buffer
441 * @param cbBuf Size of buffer
442 * @param pcszPlaceholder Pointer to placeholder, must be in pszBuf
443 * @param cbPlaceholder Length of placeholder
444 * @param pcszDmiSystemUuid DmiSystemUuid value
445 */
446static void fwUseDmiSystemUuidInString(char *pszBuf, size_t cbBuf,
447 const char *pcszPlaceholder, size_t cbPlaceholder,
448 const char *pcszDmiSystemUuid)
449{
450 size_t const cbPrefix = pcszPlaceholder - pszBuf;
451 size_t const cbUuid = strlen(pcszDmiSystemUuid);
452 size_t const cbSuffix = strlen(pcszPlaceholder + cbPlaceholder);
453 if (cbPrefix + cbUuid + cbSuffix < cbBuf)
454 {
455 /* Everything fits, no truncation. */
456 memmove(pszBuf + cbPrefix + cbUuid, pcszPlaceholder + cbPlaceholder, cbSuffix + 1); \
457 memcpy(pszBuf + cbPrefix, pcszDmiSystemUuid, cbUuid); \
458 }
459 else if (cbPrefix + cbUuid < cbBuf)
460 {
461 /* Prefix + DmiSystemUuid fits, truncate suffix. */
462 memmove(pszBuf + cbPrefix + cbUuid, pcszPlaceholder + cbPlaceholder, cbBuf - cbPrefix - cbUuid - 1); \
463 memcpy(pszBuf + cbPrefix, pcszDmiSystemUuid, cbUuid); \
464 pszBuf[cbBuf] = '\0';
465 }
466 else
467 {
468 /* Prefix fits, truncate DmiSystemUuid. */
469 memcpy(pszBuf + cbPrefix, pcszDmiSystemUuid, cbBuf - cbPrefix - 1); \
470 pszBuf[cbBuf] = '\0';
471 }
472}
473
474/**
475 * Construct the DMI table.
476 *
477 * @returns VBox status code.
478 * @param pDevIns The device instance.
479 * @param pTable Where to create the DMI table.
480 * @param cbMax The maximum size of the DMI table.
481 * @param pUuid Pointer to the UUID to use if the DmiUuid
482 * configuration string isn't present.
483 * @param pCfg The handle to our config node.
484 * @param cCpus Number of VCPUs.
485 * @param pcbDmiTables Size of DMI data in bytes.
486 * @param pcDmiTables Number of DMI tables.
487 * @param fUefi Flag whether the UEFI specification is supported.
488 */
489int FwCommonPlantDMITable(PPDMDEVINS pDevIns, uint8_t *pTable, unsigned cbMax, PCRTUUID pUuid, PCFGMNODE pCfg, uint16_t cCpus,
490 uint16_t *pcbDmiTables, uint16_t *pcDmiTables, bool fUefi)
491{
492 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
493
494 /*
495 * CFGM Hint!
496 *
497 * The macros below makes it a bit hard to figure out the config options
498 * available here. To get a quick hint, take a look a the CFGM
499 * validation in the calling code (DevEFI.cpp and DevPcBios.cpp).
500 *
501 * 32-bit signed integer CFGM options are read by DMI_READ_CFG_S32, the 2nd
502 * parameter is the CFGM value name.
503 *
504 * Strings are read by DMI_READ_CFG_STR and DMI_READ_CFG_STR_DEF, the 2nd parameter is
505 * the CFGM value name.
506 */
507#define DMI_CHECK_SIZE(cbWant) \
508 { \
509 size_t cbNeed = (size_t)(pszStr + cbWant - (char *)pTable) + 5; /* +1 for strtab terminator +4 for end-of-table entry */ \
510 if (cbNeed > cbMax) \
511 { \
512 if (fHideErrors) \
513 { \
514 LogRel(("One of the DMI strings is too long -- using default DMI data!\n")); \
515 continue; \
516 } \
517 return PDMDevHlpVMSetError(pDevIns, VERR_TOO_MUCH_DATA, RT_SRC_POS, \
518 N_("One of the DMI strings is too long. Check all bios/Dmi* configuration entries. At least %zu bytes are needed but there is no space for more than %d bytes"), cbNeed, cbMax); \
519 } \
520 }
521
522#define DMI_READ_CFG_STR_DEF(variable, name, default_value) \
523 { \
524 if (fForceDefault) \
525 pszTmp = default_value; \
526 else \
527 { \
528 rc = pHlp->pfnCFGMQueryStringDef(pCfg, name, szBuf, sizeof(szBuf), default_value); \
529 if (RT_FAILURE(rc)) \
530 { \
531 if (fHideErrors) \
532 { \
533 LogRel(("Configuration error: Querying \"" name "\" as a string failed -- using default DMI data!\n")); \
534 continue; \
535 } \
536 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS, \
537 N_("Configuration error: Querying \"" name "\" as a string failed")); \
538 } \
539 if (!strcmp(szBuf, "<EMPTY>")) \
540 pszTmp = ""; \
541 else if ((pszTmp = RTStrStr(szBuf, "<DmiSystemUuid>"))) \
542 { \
543 char *pszUuid = pszDmiSystemUuid; \
544 if (!pszUuid) \
545 { \
546 pszUuid = (char *)alloca(RTUUID_STR_LENGTH); \
547 RTUuidToStr(pUuid, pszUuid, RTUUID_STR_LENGTH); \
548 } \
549 fwUseDmiSystemUuidInString(szBuf, sizeof(szBuf), pszTmp, 15, pszUuid); \
550 pszTmp = szBuf; \
551 } \
552 else \
553 pszTmp = szBuf; \
554 } \
555 if (!pszTmp[0]) \
556 variable = 0; /* empty string */ \
557 else \
558 { \
559 variable = iStrNr++; \
560 size_t const cbStr = strlen(pszTmp) + 1; \
561 DMI_CHECK_SIZE(cbStr); \
562 pszStr = (char *)mempcpy(pszStr, pszTmp, cbStr); \
563 } \
564 }
565
566#define DMI_READ_CFG_STR(variable, name) \
567 DMI_READ_CFG_STR_DEF(variable, # name, g_pszDef ## name)
568
569#define DMI_READ_CFG_S32(variable, name) \
570 { \
571 if (fForceDefault) \
572 variable = g_iDef ## name; \
573 else \
574 { \
575 rc = pHlp->pfnCFGMQueryS32Def(pCfg, # name, & variable, g_iDef ## name); \
576 if (RT_FAILURE(rc)) \
577 { \
578 if (fHideErrors) \
579 { \
580 LogRel(("Configuration error: Querying \"" # name "\" as an int failed -- using default DMI data!\n")); \
581 continue; \
582 } \
583 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS, \
584 N_("Configuration error: Querying \"" # name "\" as an int failed")); \
585 } \
586 } \
587 }
588
589#define DMI_START_STRUCT(a_pTbl) do { \
590 pszStr = (char *)((a_pTbl) + 1); \
591 iStrNr = 1; \
592 } while (0)
593
594#if 0 /* GCC 11.2.1 barfs on this: error: writing 1 byte into a region of size 0 [-Werror=stringop-overflow=] */
595# define DMI_TERM_STRUCT do { \
596 *pszStr++ = '\0'; /* terminate set of text strings */ \
597 if (iStrNr == 1) \
598 *pszStr++ = '\0'; /* terminate a structure without strings */ \
599 } while (0)
600#else
601# define DMI_TERM_STRUCT do { \
602 size_t const cbToZero = iStrNr == 1 ? 2 : 1; \
603 pszStr = (char *)memset(pszStr, 0, cbToZero) + cbToZero; \
604 } while (0)
605#endif
606
607 bool fForceDefault = false;
608#ifdef VBOX_BIOS_DMI_FALLBACK
609 /*
610 * There will be two passes. If an error occurs during the first pass, a
611 * message will be written to the release log and we fall back to default
612 * DMI data and start a second pass.
613 */
614 bool fHideErrors = true;
615#else
616 /*
617 * There will be one pass, every error is fatal and will prevent the VM
618 * from starting.
619 */
620 bool fHideErrors = false;
621#endif
622
623 uint8_t fDmiUseHostInfo;
624 int rc = pHlp->pfnCFGMQueryU8Def(pCfg, "DmiUseHostInfo", &fDmiUseHostInfo, 0);
625 if (RT_FAILURE (rc))
626 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to read \"DmiUseHostInfo\""));
627
628 /* Sync up with host default DMI values */
629 if (fDmiUseHostInfo)
630 fwCommonUseHostDMIStrings();
631
632 uint8_t fDmiExposeMemoryTable;
633 rc = pHlp->pfnCFGMQueryU8Def(pCfg, "DmiExposeMemoryTable", &fDmiExposeMemoryTable, 0);
634 if (RT_FAILURE (rc))
635 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to read \"DmiExposeMemoryTable\""));
636 uint8_t fDmiExposeProcessorInf;
637 rc = pHlp->pfnCFGMQueryU8Def(pCfg, "DmiExposeProcInf", &fDmiExposeProcessorInf, 0);
638 if (RT_FAILURE (rc))
639 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to read \"DmiExposeProcInf\""));
640
641 for (;; fForceDefault = true, fHideErrors = false)
642 {
643 int iStrNr;
644 char szBuf[256];
645 char *pszStr = (char *)pTable;
646 char szDmiSystemUuid[64];
647 char *pszDmiSystemUuid;
648 const char *pszTmp;
649
650 if (fForceDefault)
651 pszDmiSystemUuid = NULL;
652 else
653 {
654 rc = pHlp->pfnCFGMQueryString(pCfg, "DmiSystemUuid", szDmiSystemUuid, sizeof(szDmiSystemUuid));
655 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
656 pszDmiSystemUuid = NULL;
657 else if (RT_FAILURE(rc))
658 {
659 if (fHideErrors)
660 {
661 LogRel(("Configuration error: Querying \"DmiSystemUuid\" as a string failed, using default DMI data\n"));
662 continue;
663 }
664 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
665 N_("Configuration error: Querying \"DmiSystemUuid\" as a string failed"));
666 }
667 else
668 pszDmiSystemUuid = szDmiSystemUuid;
669 }
670
671 /*********************************
672 * DMI BIOS information (Type 0) *
673 *********************************/
674 PDMIBIOSINF pBIOSInf = (PDMIBIOSINF)pszStr;
675 DMI_CHECK_SIZE(sizeof(*pBIOSInf));
676
677 pszStr = (char *)&pBIOSInf->u8ReleaseMajor;
678 pBIOSInf->header.u8Length = RT_OFFSETOF(DMIBIOSINF, u8ReleaseMajor);
679
680 /* don't set these fields by default for legacy compatibility */
681 int iDmiBIOSReleaseMajor, iDmiBIOSReleaseMinor;
682 DMI_READ_CFG_S32(iDmiBIOSReleaseMajor, DmiBIOSReleaseMajor);
683 DMI_READ_CFG_S32(iDmiBIOSReleaseMinor, DmiBIOSReleaseMinor);
684 if (iDmiBIOSReleaseMajor != 0 || iDmiBIOSReleaseMinor != 0)
685 {
686 pszStr = (char *)&pBIOSInf->u8FirmwareMajor;
687 pBIOSInf->header.u8Length = RT_OFFSETOF(DMIBIOSINF, u8FirmwareMajor);
688 pBIOSInf->u8ReleaseMajor = iDmiBIOSReleaseMajor;
689 pBIOSInf->u8ReleaseMinor = iDmiBIOSReleaseMinor;
690
691 int iDmiBIOSFirmwareMajor, iDmiBIOSFirmwareMinor;
692 DMI_READ_CFG_S32(iDmiBIOSFirmwareMajor, DmiBIOSFirmwareMajor);
693 DMI_READ_CFG_S32(iDmiBIOSFirmwareMinor, DmiBIOSFirmwareMinor);
694 if (iDmiBIOSFirmwareMajor != 0 || iDmiBIOSFirmwareMinor != 0)
695 {
696 pszStr = (char *)(pBIOSInf + 1);
697 pBIOSInf->header.u8Length = sizeof(DMIBIOSINF);
698 pBIOSInf->u8FirmwareMajor = iDmiBIOSFirmwareMajor;
699 pBIOSInf->u8FirmwareMinor = iDmiBIOSFirmwareMinor;
700 }
701 }
702
703 iStrNr = 1;
704 pBIOSInf->header.u8Type = 0; /* BIOS Information */
705 pBIOSInf->header.u16Handle = 0x0000;
706 DMI_READ_CFG_STR(pBIOSInf->u8Vendor, DmiBIOSVendor);
707 DMI_READ_CFG_STR(pBIOSInf->u8Version, DmiBIOSVersion);
708 pBIOSInf->u16Start = 0xE000;
709 DMI_READ_CFG_STR(pBIOSInf->u8Release, DmiBIOSReleaseDate);
710 pBIOSInf->u8ROMSize = 1; /* 128K */
711 pBIOSInf->u64Characteristics = RT_BIT(4) /* ISA is supported */
712 | RT_BIT(7) /* PCI is supported */
713 | RT_BIT(15) /* Boot from CD is supported */
714 | RT_BIT(16) /* Selectable Boot is supported */
715 | RT_BIT(27) /* Int 9h, 8042 Keyboard services supported */
716 | RT_BIT(30) /* Int 10h, CGA/Mono Video Services supported */
717 /* any more?? */
718 ;
719 pBIOSInf->u8CharacteristicsByte1 = RT_BIT(0) /* ACPI is supported */
720 /* any more?? */
721 ;
722 pBIOSInf->u8CharacteristicsByte2 = fUefi ? RT_BIT(3) : 0
723 /* any more?? */
724 ;
725 DMI_TERM_STRUCT;
726
727 /***********************************
728 * DMI system information (Type 1) *
729 ***********************************/
730 PDMISYSTEMINF pSystemInf = (PDMISYSTEMINF)pszStr;
731 DMI_CHECK_SIZE(sizeof(*pSystemInf));
732 DMI_START_STRUCT(pSystemInf);
733 pSystemInf->header.u8Type = 1; /* System Information */
734 pSystemInf->header.u8Length = sizeof(*pSystemInf);
735 pSystemInf->header.u16Handle = 0x0001;
736 DMI_READ_CFG_STR(pSystemInf->u8Manufacturer, DmiSystemVendor);
737 DMI_READ_CFG_STR(pSystemInf->u8ProductName, DmiSystemProduct);
738 DMI_READ_CFG_STR(pSystemInf->u8Version, DmiSystemVersion);
739 DMI_READ_CFG_STR(pSystemInf->u8SerialNumber, DmiSystemSerial);
740
741 RTUUID uuid;
742 if (pszDmiSystemUuid)
743 {
744 rc = RTUuidFromStr(&uuid, pszDmiSystemUuid);
745 if (RT_FAILURE(rc))
746 {
747 if (fHideErrors)
748 {
749 LogRel(("Configuration error: Invalid UUID for DMI tables specified, using default DMI data\n"));
750 continue;
751 }
752 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
753 N_("Configuration error: Invalid UUID for DMI tables specified"));
754 }
755 uuid.Gen.u32TimeLow = RT_H2BE_U32(uuid.Gen.u32TimeLow);
756 uuid.Gen.u16TimeMid = RT_H2BE_U16(uuid.Gen.u16TimeMid);
757 uuid.Gen.u16TimeHiAndVersion = RT_H2BE_U16(uuid.Gen.u16TimeHiAndVersion);
758 pUuid = &uuid;
759 }
760 memcpy(pSystemInf->au8Uuid, pUuid, sizeof(RTUUID));
761
762 pSystemInf->u8WakeupType = 6; /* Power Switch */
763 DMI_READ_CFG_STR(pSystemInf->u8SKUNumber, DmiSystemSKU);
764 DMI_READ_CFG_STR(pSystemInf->u8Family, DmiSystemFamily);
765 DMI_TERM_STRUCT;
766
767 /**********************************
768 * DMI board information (Type 2) *
769 **********************************/
770 PDMIBOARDINF pBoardInf = (PDMIBOARDINF)pszStr;
771 DMI_CHECK_SIZE(sizeof(*pBoardInf));
772 DMI_START_STRUCT(pBoardInf);
773 int iDmiBoardBoardType;
774 pBoardInf->header.u8Type = 2; /* Board Information */
775 pBoardInf->header.u8Length = sizeof(*pBoardInf);
776 pBoardInf->header.u16Handle = 0x0008;
777 DMI_READ_CFG_STR(pBoardInf->u8Manufacturer, DmiBoardVendor);
778 DMI_READ_CFG_STR(pBoardInf->u8Product, DmiBoardProduct);
779 DMI_READ_CFG_STR(pBoardInf->u8Version, DmiBoardVersion);
780 DMI_READ_CFG_STR(pBoardInf->u8SerialNumber, DmiBoardSerial);
781 DMI_READ_CFG_STR(pBoardInf->u8AssetTag, DmiBoardAssetTag);
782 pBoardInf->u8FeatureFlags = RT_BIT(0) /* hosting board, e.g. motherboard */
783 ;
784 DMI_READ_CFG_STR(pBoardInf->u8LocationInChass, DmiBoardLocInChass);
785 pBoardInf->u16ChassisHandle = 0x0003; /* see type 3 */
786 DMI_READ_CFG_S32(iDmiBoardBoardType, DmiBoardBoardType);
787 pBoardInf->u8BoardType = iDmiBoardBoardType;
788 pBoardInf->u8cObjectHandles = 0;
789
790 DMI_TERM_STRUCT;
791
792 /********************************************
793 * DMI System Enclosure or Chassis (Type 3) *
794 ********************************************/
795 PDMICHASSIS pChassis = (PDMICHASSIS)pszStr;
796 DMI_CHECK_SIZE(sizeof(*pChassis));
797 pszStr = (char *)&pChassis->u32OEMdefined;
798 iStrNr = 1;
799#ifdef VBOX_WITH_DMI_CHASSIS
800 pChassis->header.u8Type = 3; /* System Enclosure or Chassis */
801#else
802 pChassis->header.u8Type = 0x7e; /* inactive */
803#endif
804 pChassis->header.u8Length = RT_OFFSETOF(DMICHASSIS, u32OEMdefined);
805 pChassis->header.u16Handle = 0x0003;
806 DMI_READ_CFG_STR(pChassis->u8Manufacturer, DmiChassisVendor);
807 int iDmiChassisType;
808 DMI_READ_CFG_S32(iDmiChassisType, DmiChassisType);
809 pChassis->u8Type = iDmiChassisType;
810 DMI_READ_CFG_STR(pChassis->u8Version, DmiChassisVersion);
811 DMI_READ_CFG_STR(pChassis->u8SerialNumber, DmiChassisSerial);
812 DMI_READ_CFG_STR(pChassis->u8AssetTag, DmiChassisAssetTag);
813 pChassis->u8BootupState = 0x03; /* safe */
814 pChassis->u8PowerSupplyState = 0x03; /* safe */
815 pChassis->u8ThermalState = 0x03; /* safe */
816 pChassis->u8SecurityStatus = 0x03; /* none XXX */
817# if 0
818 /* v2.3+, currently not supported */
819 pChassis->u32OEMdefined = 0;
820 pChassis->u8Height = 0; /* unspecified */
821 pChassis->u8NumPowerChords = 0; /* unspecified */
822 pChassis->u8ContElems = 0; /* no contained elements */
823 pChassis->u8ContElemRecLen = 0; /* no contained elements */
824# endif
825 DMI_TERM_STRUCT;
826
827 /**************************************
828 * DMI Processor Information (Type 4) *
829 **************************************/
830
831 /*
832 * This is just a dummy processor. Should we expose the real guest CPU features
833 * here? Accessing this information at this point is difficult.
834 */
835 char szSocket[32];
836 PDMIPROCESSORINF pProcessorInf = (PDMIPROCESSORINF)pszStr;
837 DMI_CHECK_SIZE(sizeof(*pProcessorInf));
838 DMI_START_STRUCT(pProcessorInf);
839 if (fDmiExposeProcessorInf)
840 pProcessorInf->header.u8Type = 4; /* Processor Information */
841 else
842 pProcessorInf->header.u8Type = 126; /* inactive structure */
843 pProcessorInf->header.u8Length = sizeof(*pProcessorInf);
844 pProcessorInf->header.u16Handle = 0x0007;
845 RTStrPrintf(szSocket, sizeof(szSocket), "Socket #%u", 0);
846 pProcessorInf->u8SocketDesignation = iStrNr++;
847 {
848 size_t const cbStr = strlen(szSocket) + 1;
849 DMI_CHECK_SIZE(cbStr);
850 pszStr = (char *)mempcpy(pszStr, szSocket, cbStr);
851 }
852 pProcessorInf->u8ProcessorType = 0x03; /* Central Processor */
853 pProcessorInf->u8ProcessorFamily = 0xB1; /* Pentium III with Intel SpeedStep(TM) */
854 DMI_READ_CFG_STR(pProcessorInf->u8ProcessorManufacturer, DmiProcManufacturer);
855
856 pProcessorInf->u64ProcessorID = UINT64_C(0x0FEBFBFF00010676);
857 /* Ext Family ID = 0
858 * Ext Model ID = 2
859 * Processor Type = 0
860 * Family ID = 6
861 * Model = 7
862 * Stepping = 6
863 * Features: FPU, VME, DE, PSE, TSC, MSR, PAE, MCE, CX8,
864 * APIC, SEP, MTRR, PGE, MCA, CMOV, PAT, PSE-36,
865 * CFLSH, DS, ACPI, MMX, FXSR, SSE, SSE2, SS */
866 DMI_READ_CFG_STR(pProcessorInf->u8ProcessorVersion, DmiProcVersion);
867 pProcessorInf->u8Voltage = 0x02; /* 3.3V */
868 pProcessorInf->u16ExternalClock = 0x00; /* unknown */
869 pProcessorInf->u16MaxSpeed = 3000; /* 3GHz */
870 pProcessorInf->u16CurrentSpeed = 3000; /* 3GHz */
871 pProcessorInf->u8Status = RT_BIT(6) /* CPU socket populated */
872 | RT_BIT(0) /* CPU enabled */
873 ;
874 pProcessorInf->u8ProcessorUpgrade = 0x04; /* ZIF Socket */
875 pProcessorInf->u16L1CacheHandle = 0xFFFF; /* not specified */
876 pProcessorInf->u16L2CacheHandle = 0xFFFF; /* not specified */
877 pProcessorInf->u16L3CacheHandle = 0xFFFF; /* not specified */
878 pProcessorInf->u8SerialNumber = 0; /* not specified */
879 pProcessorInf->u8AssetTag = 0; /* not specified */
880 pProcessorInf->u8PartNumber = 0; /* not specified */
881 pProcessorInf->u8CoreCount = cCpus; /* */
882 pProcessorInf->u8CoreEnabled = cCpus;
883 pProcessorInf->u8ThreadCount = 1;
884 pProcessorInf->u16ProcessorCharacteristics
885 = RT_BIT(2); /* 64-bit capable */
886 pProcessorInf->u16ProcessorFamily2 = 0;
887 DMI_TERM_STRUCT;
888
889 /***************************************
890 * DMI Physical Memory Array (Type 16) *
891 ***************************************/
892 uint64_t const cbRamSize = PDMDevHlpMMPhysGetRamSize(pDevIns);
893
894 PDMIRAMARRAY pMemArray = (PDMIRAMARRAY)pszStr;
895 DMI_CHECK_SIZE(sizeof(*pMemArray));
896 DMI_START_STRUCT(pMemArray);
897 if (fDmiExposeMemoryTable)
898 pMemArray->header.u8Type = 16; /* Physical Memory Array */
899 else
900 pMemArray->header.u8Type = 126; /* inactive structure */
901 pMemArray->header.u8Length = sizeof(*pMemArray);
902 pMemArray->header.u16Handle = 0x0005;
903 pMemArray->u8Location = 0x03; /* Motherboard */
904 pMemArray->u8Use = 0x03; /* System memory */
905 pMemArray->u8MemErrorCorrection = 0x01; /* Other */
906 if (cbRamSize / _1K > INT32_MAX)
907 {
908 /** @todo 2TB-1K limit. In such cases we probably need to provide multiple type-16 descriptors.
909 * Or use 0x8000'0000 = 'capacity unknown'? */
910 AssertLogRelMsgFailed(("DMI: RAM size %#RX64 does not fit into type-16 descriptor, clipping to %#RX64\n",
911 cbRamSize, (uint64_t)INT32_MAX * _1K));
912 pMemArray->u32MaxCapacity = INT32_MAX;
913 }
914 else
915 pMemArray->u32MaxCapacity = (int32_t)(cbRamSize / _1K); /* RAM size in K */
916 pMemArray->u16MemErrorHandle = 0xfffe; /* No error info structure */
917 pMemArray->u16NumberOfMemDevices = 1;
918 DMI_TERM_STRUCT;
919
920 /***************************************
921 * DMI Memory Device (Type 17) *
922 ***************************************/
923 PDMIMEMORYDEV pMemDev = (PDMIMEMORYDEV)pszStr;
924 DMI_CHECK_SIZE(sizeof(*pMemDev));
925 DMI_START_STRUCT(pMemDev);
926 if (fDmiExposeMemoryTable)
927 pMemDev->header.u8Type = 17; /* Memory Device */
928 else
929 pMemDev->header.u8Type = 126; /* inactive structure */
930 pMemDev->header.u8Length = sizeof(*pMemDev);
931 pMemDev->header.u16Handle = 0x0006;
932 pMemDev->u16PhysMemArrayHandle = 0x0005; /* handle of array we belong to */
933 pMemDev->u16MemErrHandle = 0xfffe; /* system doesn't provide this information */
934 pMemDev->u16TotalWidth = 0xffff; /* Unknown */
935 pMemDev->u16DataWidth = 0xffff; /* Unknown */
936 int16_t u16RamSizeM;
937 int32_t u32ExtRamSizeM = 0;
938 if (cbRamSize / _1M > INT16_MAX)
939 {
940 /* The highest bit of u16Size must be 0 to specify 'MB' units / 1 would be 'KB'.
941 * SMBIOS 2.7 introduced a 32-bit extended size. If module size is 32GB or greater,
942 * the old u16Size is set to 7FFFh; old parsers will see 32GB-1MB, new parsers will
943 * look at new u32ExtendedSize which can represent at least 128TB. OS X 10.14+ looks
944 * at the extended size.
945 */
946 LogRel(("DMI: RAM size %#RX64 too big for one type-17 descriptor, clipping to %#RX64\n",
947 cbRamSize, (uint64_t)INT16_MAX * _1M));
948 u16RamSizeM = INT16_MAX;
949 if (cbRamSize / _1M >= 0x8000000) {
950 AssertLogRelMsgFailed(("DMI: RAM size %#RX64 too big for one type-17 descriptor, clipping to %#RX64\n",
951 cbRamSize, (uint64_t)INT32_MAX * _1M));
952 u32ExtRamSizeM = 0x8000000; /* 128TB */
953 }
954 else
955 u32ExtRamSizeM = cbRamSize / _1M;
956 }
957 else
958 u16RamSizeM = (uint16_t)(cbRamSize / _1M);
959 if (u16RamSizeM == 0)
960 u16RamSizeM = 0x400; /* 1G */
961 pMemDev->u16Size = u16RamSizeM; /* RAM size */
962 pMemDev->u32ExtendedSize = u32ExtRamSizeM;
963 pMemDev->u8FormFactor = 0x09; /* DIMM */
964 pMemDev->u8DeviceSet = 0x00; /* Not part of a device set */
965 DMI_READ_CFG_STR_DEF(pMemDev->u8DeviceLocator, " ", "DIMM 0");
966 DMI_READ_CFG_STR_DEF(pMemDev->u8BankLocator, " ", "Bank 0");
967 pMemDev->u8MemoryType = 0x03; /* DRAM */
968 pMemDev->u16TypeDetail = 0; /* Nothing special */
969 pMemDev->u16Speed = 1600; /* Unknown, shall be speed in MHz */
970 DMI_READ_CFG_STR(pMemDev->u8Manufacturer, DmiSystemVendor);
971 DMI_READ_CFG_STR_DEF(pMemDev->u8SerialNumber, " ", "00000000");
972 DMI_READ_CFG_STR_DEF(pMemDev->u8AssetTag, " ", "00000000");
973 DMI_READ_CFG_STR_DEF(pMemDev->u8PartNumber, " ", "00000000");
974 pMemDev->u8Attributes = 0; /* Unknown */
975 DMI_TERM_STRUCT;
976
977 /*****************************
978 * DMI OEM strings (Type 11) *
979 *****************************/
980 PDMIOEMSTRINGS pOEMStrings = (PDMIOEMSTRINGS)pszStr;
981 DMI_CHECK_SIZE(sizeof(*pOEMStrings));
982 DMI_START_STRUCT(pOEMStrings);
983#ifdef VBOX_WITH_DMI_OEMSTRINGS
984 pOEMStrings->header.u8Type = 0xb; /* OEM Strings */
985#else
986 pOEMStrings->header.u8Type = 126; /* inactive structure */
987#endif
988 pOEMStrings->header.u8Length = sizeof(*pOEMStrings);
989 pOEMStrings->header.u16Handle = 0x0002;
990 pOEMStrings->u8Count = 2;
991
992 char szTmp[64];
993 RTStrPrintf(szTmp, sizeof(szTmp), "vboxVer_%u.%u.%u",
994 RTBldCfgVersionMajor(), RTBldCfgVersionMinor(), RTBldCfgVersionBuild());
995 DMI_READ_CFG_STR_DEF(pOEMStrings->u8VBoxVersion, "DmiOEMVBoxVer", szTmp);
996 RTStrPrintf(szTmp, sizeof(szTmp), "vboxRev_%u", RTBldCfgRevision());
997 DMI_READ_CFG_STR_DEF(pOEMStrings->u8VBoxRevision, "DmiOEMVBoxRev", szTmp);
998 DMI_TERM_STRUCT;
999
1000 /*************************************
1001 * DMI OEM specific table (Type 128) *
1002 ************************************/
1003 PDMIOEMSPECIFIC pOEMSpecific = (PDMIOEMSPECIFIC)pszStr;
1004 DMI_CHECK_SIZE(sizeof(*pOEMSpecific));
1005 DMI_START_STRUCT(pOEMSpecific);
1006 pOEMSpecific->header.u8Type = 0x80; /* OEM specific */
1007 pOEMSpecific->header.u8Length = sizeof(*pOEMSpecific);
1008 pOEMSpecific->header.u16Handle = 0x0008; /* Just next free handle */
1009 pOEMSpecific->u32CpuFreqKHz = RT_H2LE_U32((uint32_t)((uint64_t)PDMDevHlpTMCpuTicksPerSecond(pDevIns) / 1000));
1010 DMI_TERM_STRUCT;
1011
1012 /* End-of-table marker - includes padding to account for fixed table size. */
1013 PDMIHDR pEndOfTable = (PDMIHDR)pszStr;
1014 pszStr = (char *)(pEndOfTable + 1);
1015 pEndOfTable->u8Type = 0x7f;
1016
1017 pEndOfTable->u8Length = sizeof(*pEndOfTable);
1018 pEndOfTable->u16Handle = 0xFEFF;
1019 *pcbDmiTables = ((uintptr_t)pszStr - (uintptr_t)pTable) + 2;
1020
1021 /* We currently plant 10 DMI tables. Update this if tables number changed. */
1022 *pcDmiTables = 10;
1023
1024 /* If more fields are added here, fix the size check in DMI_READ_CFG_STR */
1025
1026 /* Success! */
1027 break;
1028 }
1029
1030#undef DMI_READ_CFG_STR
1031#undef DMI_READ_CFG_S32
1032#undef DMI_CHECK_SIZE
1033 return VINF_SUCCESS;
1034}
1035
1036/**
1037 * Construct the SMBIOS and DMI headers table pointer at VM construction and
1038 * reset.
1039 *
1040 * @param pDevIns The device instance data.
1041 * @param pHdr Pointer to the header destination.
1042 * @param cbDmiTables Size of all DMI tables planted in bytes.
1043 * @param cNumDmiTables Number of DMI tables planted.
1044 */
1045void FwCommonPlantSmbiosAndDmiHdrs(PPDMDEVINS pDevIns, uint8_t *pHdr, uint16_t cbDmiTables, uint16_t cNumDmiTables)
1046{
1047 RT_NOREF(pDevIns);
1048
1049 struct
1050 {
1051 struct SMBIOSHDR smbios;
1052 struct DMIMAINHDR dmi;
1053 }
1054 aBiosHeaders =
1055 {
1056 // The SMBIOS header
1057 {
1058 { 0x5f, 0x53, 0x4d, 0x5f}, // "_SM_" signature
1059 0x00, // checksum
1060 0x1f, // EPS length, defined by standard
1061 VBOX_SMBIOS_MAJOR_VER, // SMBIOS major version
1062 VBOX_SMBIOS_MINOR_VER, // SMBIOS minor version
1063 VBOX_SMBIOS_MAXSS, // Maximum structure size
1064 0x00, // Entry point revision
1065 { 0x00, 0x00, 0x00, 0x00, 0x00 } // padding
1066 },
1067 // The DMI header
1068 {
1069 { 0x5f, 0x44, 0x4d, 0x49, 0x5f }, // "_DMI_" signature
1070 0x00, // checksum
1071 0, // DMI tables length
1072 VBOX_DMI_TABLE_BASE, // DMI tables base
1073 0, // DMI tables entries
1074 VBOX_DMI_TABLE_VER, // DMI version
1075 }
1076 };
1077
1078 aBiosHeaders.dmi.u16TablesLength = cbDmiTables;
1079 aBiosHeaders.dmi.u16TableEntries = cNumDmiTables;
1080 /* NB: The _SM_ table checksum technically covers both the _SM_ part (16 bytes) and the _DMI_ part
1081 * (further 15 bytes). However, because the _DMI_ checksum must be zero, the _SM_ checksum can
1082 * be calculated independently.
1083 */
1084 aBiosHeaders.smbios.u8Checksum = fwCommonChecksum((uint8_t*)&aBiosHeaders.smbios, sizeof(aBiosHeaders.smbios));
1085 aBiosHeaders.dmi.u8Checksum = fwCommonChecksum((uint8_t*)&aBiosHeaders.dmi, sizeof(aBiosHeaders.dmi));
1086
1087 memcpy(pHdr, &aBiosHeaders, sizeof(aBiosHeaders));
1088}
1089
1090/**
1091 * Construct the MPS table for implanting as a ROM page.
1092 *
1093 * Only applicable if IOAPIC is active!
1094 *
1095 * See ``MultiProcessor Specification Version 1.4 (May 1997)'':
1096 * ``1.3 Scope
1097 * ...
1098 * The hardware required to implement the MP specification is kept to a
1099 * minimum, as follows:
1100 * * One or more processors that are Intel architecture instruction set
1101 * compatible, such as the CPUs in the Intel486 or Pentium processor
1102 * family.
1103 * * One or more APICs, such as the Intel 82489DX Advanced Programmable
1104 * Interrupt Controller or the integrated APIC, such as that on the
1105 * Intel Pentium 735\\90 and 815\\100 processors, together with a discrete
1106 * I/O APIC unit.''
1107 * and later:
1108 * ``4.3.3 I/O APIC Entries
1109 * The configuration table contains one or more entries for I/O APICs.
1110 * ...
1111 * I/O APIC FLAGS: EN 3:0 1 If zero, this I/O APIC is unusable, and the
1112 * operating system should not attempt to access
1113 * this I/O APIC.
1114 * At least one I/O APIC must be enabled.''
1115 *
1116 * @param pDevIns The device instance data.
1117 * @param pTable Where to write the table.
1118 * @param cbMax The maximum size of the MPS table.
1119 * @param cCpus The number of guest CPUs.
1120 */
1121void FwCommonPlantMpsTable(PPDMDEVINS pDevIns, uint8_t *pTable, unsigned cbMax, uint16_t cCpus)
1122{
1123 RT_NOREF1(cbMax);
1124
1125 /* configuration table */
1126 PMPSCFGTBLHEADER pCfgTab = (MPSCFGTBLHEADER*)pTable;
1127 memcpy(pCfgTab->au8Signature, "PCMP", 4);
1128 pCfgTab->u8SpecRev = 4; /* 1.4 */
1129 memcpy(pCfgTab->au8OemId, "VBOXCPU ", 8);
1130 memcpy(pCfgTab->au8ProductId, "VirtualBox ", 12);
1131 pCfgTab->u32OemTablePtr = 0;
1132 pCfgTab->u16OemTableSize = 0;
1133 pCfgTab->u16EntryCount = 0; /* Incremented as we go. */
1134 pCfgTab->u32AddrLocalApic = 0xfee00000;
1135 pCfgTab->u16ExtTableLength = 0;
1136 pCfgTab->u8ExtTableChecksum = 0;
1137 pCfgTab->u8Reserved = 0;
1138
1139 uint32_t u32Eax, u32Ebx, u32Ecx, u32Edx;
1140 uint32_t u32CPUSignature = 0x0520; /* default: Pentium 100 */
1141 uint32_t u32FeatureFlags = 0x0001; /* default: FPU */
1142 PDMDevHlpGetCpuId(pDevIns, 0, &u32Eax, &u32Ebx, &u32Ecx, &u32Edx);
1143 if (u32Eax >= 1)
1144 {
1145 PDMDevHlpGetCpuId(pDevIns, 1, &u32Eax, &u32Ebx, &u32Ecx, &u32Edx);
1146 u32CPUSignature = u32Eax & 0xfff;
1147 /* Local APIC will be enabled later so override it here. Since we provide
1148 * an MP table we have an IOAPIC and therefore a Local APIC. */
1149 u32FeatureFlags = u32Edx | X86_CPUID_FEATURE_EDX_APIC;
1150 }
1151 /* Construct MPS table for each VCPU. */
1152 PMPSPROCENTRY pProcEntry = (PMPSPROCENTRY)(pCfgTab+1);
1153 for (int i = 0; i < cCpus; i++)
1154 {
1155 pProcEntry->u8EntryType = 0; /* processor entry */
1156 pProcEntry->u8LocalApicId = i;
1157 pProcEntry->u8LocalApicVersion = 0x14;
1158 pProcEntry->u8CPUFlags = (i == 0 ? 2 /* bootstrap processor */ : 0 /* application processor */) | 1 /* enabled */;
1159 pProcEntry->u32CPUSignature = u32CPUSignature;
1160 pProcEntry->u32CPUFeatureFlags = u32FeatureFlags;
1161 pProcEntry->u32Reserved[0] =
1162 pProcEntry->u32Reserved[1] = 0;
1163 pProcEntry++;
1164 pCfgTab->u16EntryCount++;
1165 }
1166
1167 uint32_t iBusIdIsa = 0;
1168 uint32_t iBusIdPci0 = 1;
1169
1170 /* ISA bus */
1171 PMPSBUSENTRY pBusEntry = (PMPSBUSENTRY)pProcEntry;
1172 pBusEntry->u8EntryType = 1; /* bus entry */
1173 pBusEntry->u8BusId = iBusIdIsa; /* this ID is referenced by the interrupt entries */
1174 memcpy(pBusEntry->au8BusTypeStr, "ISA ", 6);
1175 pBusEntry++;
1176 pCfgTab->u16EntryCount++;
1177
1178 /* PCI bus */
1179 pBusEntry->u8EntryType = 1; /* bus entry */
1180 pBusEntry->u8BusId = iBusIdPci0; /* this ID can be referenced by the interrupt entries */
1181 memcpy(pBusEntry->au8BusTypeStr, "PCI ", 6);
1182 pBusEntry++;
1183 pCfgTab->u16EntryCount++;
1184
1185
1186 /* I/O-APIC.
1187 * MP spec: "The configuration table contains one or more entries for I/O APICs.
1188 * ... At least one I/O APIC must be enabled." */
1189 PMPSIOAPICENTRY pIOAPICEntry = (PMPSIOAPICENTRY)(pBusEntry);
1190 uint16_t iApicId = 0;
1191 pIOAPICEntry->u8EntryType = 2; /* I/O-APIC entry */
1192 pIOAPICEntry->u8Id = iApicId; /* this ID is referenced by the interrupt entries */
1193 pIOAPICEntry->u8Version = 0x11;
1194 pIOAPICEntry->u8Flags = 1 /* enable */;
1195 pIOAPICEntry->u32Addr = 0xfec00000;
1196 pCfgTab->u16EntryCount++;
1197
1198 /* Interrupt tables */
1199 /* Bus vectors */
1200 /* Note: The PIC is currently not routed to the I/O APIC. Therefore we skip
1201 * pin 0 on the I/O APIC.
1202 */
1203 PMPSIOIRQENTRY pIrqEntry = (PMPSIOIRQENTRY)(pIOAPICEntry+1);
1204 for (int iPin = 1; iPin < 16; iPin++, pIrqEntry++)
1205 {
1206 pIrqEntry->u8EntryType = 3; /* I/O interrupt entry */
1207 /*
1208 * 0 - INT, vectored interrupt,
1209 * 3 - ExtINT, vectored interrupt provided by PIC
1210 * As we emulate system with both APIC and PIC, it's needed for their coexistence.
1211 */
1212 pIrqEntry->u8Type = (iPin == 0) ? 3 : 0;
1213 pIrqEntry->u16Flags = 0; /* polarity of APIC I/O input signal = conforms to bus,
1214 trigger mode = conforms to bus */
1215 pIrqEntry->u8SrcBusId = iBusIdIsa; /* ISA bus */
1216 /* IRQ0 mapped to pin 2, other are identity mapped */
1217 /* If changing, also update PDMIsaSetIrq() and MADT */
1218 pIrqEntry->u8SrcBusIrq = (iPin == 2) ? 0 : iPin; /* IRQ on the bus */
1219 pIrqEntry->u8DstIOAPICId = iApicId; /* destination IO-APIC */
1220 pIrqEntry->u8DstIOAPICInt = iPin; /* pin on destination IO-APIC */
1221 pCfgTab->u16EntryCount++;
1222 }
1223 /* Local delivery */
1224 pIrqEntry->u8EntryType = 4; /* Local interrupt entry */
1225 pIrqEntry->u8Type = 3; /* ExtINT */
1226 pIrqEntry->u16Flags = (1 << 2) | 1; /* active-high, edge-triggered */
1227 pIrqEntry->u8SrcBusId = iBusIdIsa;
1228 pIrqEntry->u8SrcBusIrq = 0;
1229 pIrqEntry->u8DstIOAPICId = 0xff;
1230 pIrqEntry->u8DstIOAPICInt = 0;
1231 pIrqEntry++;
1232 pCfgTab->u16EntryCount++;
1233 pIrqEntry->u8EntryType = 4; /* Local interrupt entry */
1234 pIrqEntry->u8Type = 1; /* NMI */
1235 pIrqEntry->u16Flags = (1 << 2) | 1; /* active-high, edge-triggered */
1236 pIrqEntry->u8SrcBusId = iBusIdIsa;
1237 pIrqEntry->u8SrcBusIrq = 0;
1238 pIrqEntry->u8DstIOAPICId = 0xff;
1239 pIrqEntry->u8DstIOAPICInt = 1;
1240 pIrqEntry++;
1241 pCfgTab->u16EntryCount++;
1242
1243 pCfgTab->u16Length = (uint8_t*)pIrqEntry - pTable;
1244 pCfgTab->u8Checksum = fwCommonChecksum(pTable, pCfgTab->u16Length);
1245
1246 AssertMsg(pCfgTab->u16Length < cbMax,
1247 ("VBOX_MPS_TABLE_SIZE=%d, maximum allowed size is %d",
1248 pCfgTab->u16Length, cbMax));
1249}
1250
1251/**
1252 * Construct the MPS table pointer at VM construction and reset.
1253 *
1254 * Only applicable if IOAPIC is active!
1255 *
1256 * @param pDevIns The device instance data.
1257 * @param u32MpTableAddr The MP table physical address.
1258 */
1259void FwCommonPlantMpsFloatPtr(PPDMDEVINS pDevIns, uint32_t u32MpTableAddr)
1260{
1261 MPSFLOATPTR floatPtr;
1262 floatPtr.au8Signature[0] = '_';
1263 floatPtr.au8Signature[1] = 'M';
1264 floatPtr.au8Signature[2] = 'P';
1265 floatPtr.au8Signature[3] = '_';
1266 floatPtr.u32MPSAddr = u32MpTableAddr;
1267 floatPtr.u8Length = 1; /* structure size in paragraphs */
1268 floatPtr.u8SpecRev = 4; /* MPS revision 1.4 */
1269 floatPtr.u8Checksum = 0;
1270 floatPtr.au8Feature[0] = 0;
1271 floatPtr.au8Feature[1] = 0;
1272 floatPtr.au8Feature[2] = 0;
1273 floatPtr.au8Feature[3] = 0;
1274 floatPtr.au8Feature[4] = 0;
1275 floatPtr.u8Checksum = fwCommonChecksum((uint8_t*)&floatPtr, 16);
1276 PDMDevHlpPhysWrite(pDevIns, 0x9fff0, &floatPtr, 16);
1277}
1278
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette