VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/DevPcBios.cpp@ 71809

Last change on this file since 71809 was 71809, checked in by vboxsync, 7 years ago

PC/Dev*: nits

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 64.7 KB
Line 
1/* $Id: DevPcBios.cpp 71809 2018-04-10 11:49:53Z vboxsync $ */
2/** @file
3 * DevPcBios - PC BIOS Device.
4 */
5
6/*
7 * Copyright (C) 2006-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DEV_PC_BIOS
23#include <VBox/vmm/pdmdev.h>
24#include <VBox/vmm/pdmstorageifs.h>
25#include <VBox/vmm/mm.h>
26#include <VBox/vmm/pgm.h>
27#include <VBox/vmm/cpum.h>
28#include <VBox/vmm/vm.h>
29
30#include <VBox/log.h>
31#include <iprt/asm.h>
32#include <iprt/assert.h>
33#include <iprt/buildconfig.h>
34#include <iprt/file.h>
35#include <iprt/mem.h>
36#include <iprt/string.h>
37#include <iprt/uuid.h>
38#include <iprt/cdefs.h>
39#include <VBox/bios.h>
40#include <VBox/err.h>
41#include <VBox/param.h>
42
43#include "VBoxDD.h"
44#include "VBoxDD2.h"
45#include "DevPcBios.h"
46#include "DevFwCommon.h"
47
48#define NET_BOOT_DEVS 4
49
50
51/** @page pg_devbios_cmos_assign CMOS Assignments (BIOS)
52 *
53 * The BIOS uses a CMOS to store configuration data.
54 * It is currently used as follows:
55 *
56 * @verbatim
57 First CMOS bank (offsets 0x00 to 0x7f):
58 Floppy drive type:
59 0x10
60 Hard disk type (old):
61 0x12
62 Equipment byte:
63 0x14
64 Base memory:
65 0x15
66 0x16
67 Extended memory:
68 0x17
69 0x18
70 0x30
71 0x31
72 First IDE HDD:
73 0x19
74 0x1e - 0x25
75 Second IDE HDD:
76 0x1a
77 0x26 - 0x2d
78 Checksum of 0x10-0x2d:
79 0x2e
80 0x2f
81 Amount of memory above 16M and below 4GB in 64KB units:
82 0x34
83 0x35
84 Boot device (BOCHS BIOS specific):
85 0x38
86 0x3c
87 0x3d
88 PXE debug:
89 0x3f
90 First SATA HDD:
91 0x40 - 0x47
92 Second SATA HDD:
93 0x48 - 0x4f
94 Third SATA HDD:
95 0x50 - 0x57
96 Fourth SATA HDD:
97 0x58 - 0x5f
98 Number of CPUs:
99 0x60
100 RAM above 4G in 64KB units:
101 0x61 - 0x65
102 Third IDE HDD:
103 0x67 - 0x6e
104 Fourth IDE HDD:
105 0x70 - 0x77
106 APIC/x2APIC settings:
107 0x78
108
109 Second CMOS bank (offsets 0x80 to 0xff):
110 Reserved for internal use by PXE ROM:
111 0x80 - 0x81
112 First net boot device PCI bus/dev/fn:
113 0x82 - 0x83
114 Second to third net boot devices:
115 0x84 - 0x89
116 First SCSI HDD:
117 0x90 - 0x97
118 Second SCSI HDD:
119 0x98 - 0x9f
120 Third SCSI HDD:
121 0xa0 - 0xa7
122 Fourth SCSI HDD:
123 0xa8 - 0xaf
124
125@endverbatim
126 *
127 * @todo Mark which bits are compatible with which BIOSes and
128 * which are our own definitions.
129 */
130
131
132/*********************************************************************************************************************************
133* Structures and Typedefs *
134*********************************************************************************************************************************/
135
136/**
137 * The boot device.
138 */
139typedef enum DEVPCBIOSBOOT
140{
141 DEVPCBIOSBOOT_NONE,
142 DEVPCBIOSBOOT_FLOPPY,
143 DEVPCBIOSBOOT_HD,
144 DEVPCBIOSBOOT_DVD,
145 DEVPCBIOSBOOT_LAN
146} DEVPCBIOSBOOT;
147
148/**
149 * PC Bios instance data structure.
150 */
151typedef struct DEVPCBIOS
152{
153 /** Pointer back to the device instance. */
154 PPDMDEVINS pDevIns;
155
156 /** Boot devices (ordered). */
157 DEVPCBIOSBOOT aenmBootDevice[4];
158 /** Bochs shutdown index. */
159 uint32_t iShutdown;
160 /** Floppy device. */
161 char *pszFDDevice;
162 /** Harddisk device. */
163 char *pszHDDevice;
164 /** Sata harddisk device. */
165 char *pszSataDevice;
166 /** LUNs of the four BIOS-accessible SATA disks. */
167 uint32_t iSataHDLUN[4];
168 /** SCSI harddisk device. */
169 char *pszScsiDevice;
170 /** LUNs of the four BIOS-accessible SCSI disks. */
171 uint32_t iScsiHDLUN[4];
172 /** Bios message buffer. */
173 char szMsg[256];
174 /** Bios message buffer index. */
175 uint32_t iMsg;
176 /** The system BIOS ROM data. */
177 uint8_t *pu8PcBios;
178 /** The size of the system BIOS ROM. */
179 uint32_t cbPcBios;
180 /** The name of the BIOS ROM file. */
181 char *pszPcBiosFile;
182 /** The LAN boot ROM data. */
183 uint8_t *pu8LanBoot;
184 /** The name of the LAN boot ROM file. */
185 char *pszLanBootFile;
186 /** The size of the LAN boot ROM. */
187 uint64_t cbLanBoot;
188 /** The DMI tables. */
189 uint8_t au8DMIPage[0x1000];
190 /** The boot countdown (in seconds). */
191 uint8_t uBootDelay;
192 /** I/O-APIC enabled? */
193 uint8_t u8IOAPIC;
194 /** APIC mode to be set up by BIOS */
195 uint8_t u8APICMode;
196 /** PXE debug logging enabled? */
197 uint8_t u8PXEDebug;
198 /** PXE boot PCI bus/dev/fn list. */
199 uint16_t au16NetBootDev[NET_BOOT_DEVS];
200 /** Number of logical CPUs in guest */
201 uint16_t cCpus;
202 /* Physical address of PCI config space MMIO region. Currently unused. */
203 uint64_t u64McfgBase;
204 /* Length of PCI config space MMIO region. Currently unused. */
205 uint64_t cbMcfgLength;
206
207 /** Firmware registration structure. */
208 PDMFWREG FwReg;
209 /** Dummy. */
210 PCPDMFWHLPR3 pFwHlpR3;
211 /** Whether to consult the shutdown status (CMOS[0xf]) for deciding upon soft
212 * or hard reset. */
213 bool fCheckShutdownStatusForSoftReset;
214 /** Whether to clear the shutdown status on hard reset. */
215 bool fClearShutdownStatusOnHardReset;
216 /** Number of soft resets we've logged. */
217 uint32_t cLoggedSoftResets;
218 /** Current port number for Bochs shutdown (used by APM). */
219 RTIOPORT ShutdownPort;
220 /** True=use new port number for Bochs shutdown (used by APM). */
221 bool fNewShutdownPort;
222} DEVPCBIOS;
223/** Pointer to the BIOS device state. */
224typedef DEVPCBIOS *PDEVPCBIOS;
225
226
227/*********************************************************************************************************************************
228* Defined Constants And Macros *
229*********************************************************************************************************************************/
230/** The saved state version. */
231#define PCBIOS_SSM_VERSION 0
232
233
234/*********************************************************************************************************************************
235* Global Variables *
236*********************************************************************************************************************************/
237/** Saved state DEVPCBIOS field descriptors. */
238static SSMFIELD const g_aPcBiosFields[] =
239{
240 SSMFIELD_ENTRY( DEVPCBIOS, fNewShutdownPort),
241 SSMFIELD_ENTRY_TERM()
242};
243
244
245/**
246 * @callback_method_impl{FNIOMIOPORTIN, Bochs Debug and Shutdown ports.}
247 */
248static DECLCALLBACK(int) pcbiosIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
249{
250 RT_NOREF5(pDevIns, pvUser, Port, pu32, cb);
251 return VERR_IOM_IOPORT_UNUSED;
252}
253
254
255/**
256 * @callback_method_impl{FNIOMIOPORTOUT, Bochs Debug and Shutdown ports.}
257 */
258static DECLCALLBACK(int) pcbiosIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
259{
260 RT_NOREF1(pvUser);
261 PDEVPCBIOS pThis = PDMINS_2_DATA(pDevIns, PDEVPCBIOS);
262
263 /*
264 * Bochs BIOS char printing.
265 */
266 if ( cb == 1
267 && ( Port == 0x402
268 || Port == 0x403))
269 {
270 /* The raw version. */
271 switch (u32)
272 {
273 case '\r': Log2(("pcbios: <return>\n")); break;
274 case '\n': Log2(("pcbios: <newline>\n")); break;
275 case '\t': Log2(("pcbios: <tab>\n")); break;
276 default: Log2(("pcbios: %c (%02x)\n", u32, u32)); break;
277 }
278
279 /* The readable, buffered version. */
280 if (u32 == '\n' || u32 == '\r')
281 {
282 pThis->szMsg[pThis->iMsg] = '\0';
283 if (pThis->iMsg)
284 Log(("pcbios: %s\n", pThis->szMsg));
285 pThis->iMsg = 0;
286 }
287 else
288 {
289 if (pThis->iMsg >= sizeof(pThis->szMsg)-1)
290 {
291 pThis->szMsg[pThis->iMsg] = '\0';
292 Log(("pcbios: %s\n", pThis->szMsg));
293 pThis->iMsg = 0;
294 }
295 pThis->szMsg[pThis->iMsg] = (char )u32;
296 pThis->szMsg[++pThis->iMsg] = '\0';
297 }
298 return VINF_SUCCESS;
299 }
300
301 /*
302 * Bochs BIOS shutdown request.
303 */
304 if (cb == 1 && Port == pThis->ShutdownPort)
305 {
306 static const unsigned char szShutdown[] = "Shutdown";
307 if (u32 == szShutdown[pThis->iShutdown])
308 {
309 pThis->iShutdown++;
310 if (pThis->iShutdown == 8)
311 {
312 pThis->iShutdown = 0;
313 LogRel(("PcBios: APM shutdown request\n"));
314 return PDMDevHlpVMPowerOff(pDevIns);
315 }
316 }
317 else
318 pThis->iShutdown = 0;
319 return VINF_SUCCESS;
320 }
321
322 /* not in use. */
323 return VINF_SUCCESS;
324}
325
326
327/**
328 * Register the Bochs shutdown port.
329 * This is used by pcbiosConstruct, pcbiosReset and pcbiosLoadExec.
330 */
331static int pcbiosRegisterShutdown(PPDMDEVINS pDevIns, PDEVPCBIOS pThis, bool fNewShutdownPort)
332{
333 if (pThis->ShutdownPort != 0)
334 {
335 int rc = PDMDevHlpIOPortDeregister(pDevIns, pThis->ShutdownPort, 1);
336 AssertRC(rc);
337 }
338 pThis->fNewShutdownPort = fNewShutdownPort;
339 if (fNewShutdownPort)
340 pThis->ShutdownPort = VBOX_BIOS_SHUTDOWN_PORT;
341 else
342 pThis->ShutdownPort = VBOX_BIOS_OLD_SHUTDOWN_PORT;
343 return PDMDevHlpIOPortRegister(pDevIns, pThis->ShutdownPort, 1, NULL,
344 pcbiosIOPortWrite, pcbiosIOPortRead,
345 NULL, NULL, "Bochs PC BIOS - Shutdown");
346}
347
348
349/**
350 * @callback_method_impl{FNSSMDEVSAVEEXEC}
351 */
352static DECLCALLBACK(int) pcbiosSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
353{
354 PDEVPCBIOS pThis = PDMINS_2_DATA(pDevIns, PDEVPCBIOS);
355 SSMR3PutStruct(pSSM, pThis, g_aPcBiosFields);
356 return VINF_SUCCESS;
357}
358
359
360/**
361 * @callback_method_impl{FNSSMDEVLOADPREP,
362 * Clears the fNewShutdownPort flag prior to loading the state so that old
363 * saved VM states keeps using the old port address (no pcbios state)}
364 */
365static DECLCALLBACK(int) pcbiosLoadPrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
366{
367 RT_NOREF(pSSM);
368 PDEVPCBIOS pThis = PDMINS_2_DATA(pDevIns, PDEVPCBIOS);
369
370 /* Since there are legacy saved state files without any SSM data for PCBIOS
371 * this is the only way to handle them correctly. */
372 pThis->fNewShutdownPort = false;
373
374 return VINF_SUCCESS;
375}
376
377
378/**
379 * @callback_method_impl{FNSSMDEVLOADEXEC}
380 */
381static DECLCALLBACK(int) pcbiosLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
382{
383 PDEVPCBIOS pThis = PDMINS_2_DATA(pDevIns, PDEVPCBIOS);
384
385 if (uVersion > PCBIOS_SSM_VERSION)
386 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
387 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
388
389 return SSMR3GetStruct(pSSM, pThis, g_aPcBiosFields);
390}
391
392
393/**
394 * @callback_method_impl{FNSSMDEVLOADDONE,
395 * Updates the shutdown port registration to match the flag loaded (or not).}
396 */
397static DECLCALLBACK(int) pcbiosLoadDone(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
398{
399 RT_NOREF(pSSM);
400 PDEVPCBIOS pThis = PDMINS_2_DATA(pDevIns, PDEVPCBIOS);
401 return pcbiosRegisterShutdown(pDevIns, pThis, pThis->fNewShutdownPort);
402}
403
404
405/**
406 * Write to CMOS memory.
407 * This is used by the init complete code.
408 */
409static void pcbiosCmosWrite(PPDMDEVINS pDevIns, int off, uint32_t u32Val)
410{
411 Assert(off < 256);
412 Assert(u32Val < 256);
413
414 int rc = PDMDevHlpCMOSWrite(pDevIns, off, u32Val);
415 AssertRC(rc);
416}
417
418
419/**
420 * Read from CMOS memory.
421 * This is used by the init complete code.
422 */
423static uint8_t pcbiosCmosRead(PPDMDEVINS pDevIns, unsigned off)
424{
425 Assert(off < 256);
426
427 uint8_t u8val;
428 int rc = PDMDevHlpCMOSRead(pDevIns, off, &u8val);
429 AssertRC(rc);
430
431 return u8val;
432}
433
434
435/**
436 * @interface_method_impl{PDMFWREG,pfnIsHardReset}
437 */
438static DECLCALLBACK(bool) pcbiosFw_IsHardReset(PPDMDEVINS pDevIns, uint32_t fFlags)
439{
440 RT_NOREF1(fFlags);
441 PDEVPCBIOS pThis = PDMINS_2_DATA(pDevIns, PDEVPCBIOS);
442 if (pThis->fCheckShutdownStatusForSoftReset)
443 {
444 uint8_t bShutdownStatus = pcbiosCmosRead(pDevIns, 0xf);
445 if ( bShutdownStatus == 0x5
446 || bShutdownStatus == 0x9
447 || bShutdownStatus == 0xa)
448 {
449 const uint32_t cMaxLogged = 10;
450 if (pThis->cLoggedSoftResets < cMaxLogged)
451 {
452 RTFAR16 Far16 = { 0xfeed, 0xface };
453 PDMDevHlpPhysRead(pDevIns, 0x467, &Far16, sizeof(Far16));
454 pThis->cLoggedSoftResets++;
455 LogRel(("PcBios: Soft reset #%u - shutdown status %#x, warm reset vector (0040:0067) is %04x:%04x%s\n",
456 pThis->cLoggedSoftResets, bShutdownStatus, Far16.sel, Far16.sel,
457 pThis->cLoggedSoftResets < cMaxLogged ? "." : " - won't log any more!"));
458 }
459 return false;
460 }
461 }
462 return true;
463}
464
465
466/**
467 * @interface_method_impl{PDMDEVREG,pfnReset}
468 */
469static DECLCALLBACK(void) pcbiosReset(PPDMDEVINS pDevIns)
470{
471 PDEVPCBIOS pThis = PDMINS_2_DATA(pDevIns, PDEVPCBIOS);
472
473 if (pThis->fClearShutdownStatusOnHardReset)
474 {
475 uint8_t bShutdownStatus = pcbiosCmosRead(pDevIns, 0xf);
476 if (bShutdownStatus != 0)
477 {
478 LogRel(("PcBios: Clearing shutdown status code %02x.\n", bShutdownStatus));
479 pcbiosCmosWrite(pDevIns, 0xf, 0);
480 }
481 }
482
483 /* After reset the new BIOS code is active, use the new shutdown port. */
484 pcbiosRegisterShutdown(pDevIns, pThis, true /* fNewShutdownPort */);
485}
486
487
488/**
489 * Attempt to guess the LCHS disk geometry from the MS-DOS master boot record
490 * (partition table).
491 *
492 * @returns VBox status code.
493 * @param pMedia The media device interface of the disk.
494 * @param pLCHSGeometry Where to return the disk geometry on success
495 */
496static int biosGuessDiskLCHS(PPDMIMEDIA pMedia, PPDMMEDIAGEOMETRY pLCHSGeometry)
497{
498 uint8_t aMBR[512], *p;
499 int rc;
500 uint32_t iEndHead, iEndSector, cLCHSCylinders, cLCHSHeads, cLCHSSectors;
501
502 if (!pMedia)
503 return VERR_INVALID_PARAMETER;
504 rc = pMedia->pfnReadPcBios(pMedia, 0, aMBR, sizeof(aMBR));
505 if (RT_FAILURE(rc))
506 return rc;
507 /* Test MBR magic number. */
508 if (aMBR[510] != 0x55 || aMBR[511] != 0xaa)
509 return VERR_INVALID_PARAMETER;
510 for (uint32_t i = 0; i < 4; i++)
511 {
512 /* Figure out the start of a partition table entry. */
513 p = &aMBR[0x1be + i * 16];
514 iEndHead = p[5];
515 iEndSector = p[6] & 63;
516 if ((p[12] | p[13] | p[14] | p[15]) && iEndSector & iEndHead)
517 {
518 /* Assumption: partition terminates on a cylinder boundary. */
519 cLCHSHeads = iEndHead + 1;
520 cLCHSSectors = iEndSector;
521 cLCHSCylinders = RT_MIN(1024, pMedia->pfnGetSize(pMedia) / (512 * cLCHSHeads * cLCHSSectors));
522 if (cLCHSCylinders >= 1)
523 {
524 pLCHSGeometry->cCylinders = cLCHSCylinders;
525 pLCHSGeometry->cHeads = cLCHSHeads;
526 pLCHSGeometry->cSectors = cLCHSSectors;
527 Log(("%s: LCHS=%d %d %d\n", __FUNCTION__, cLCHSCylinders, cLCHSHeads, cLCHSSectors));
528 return VINF_SUCCESS;
529 }
530 }
531 }
532 return VERR_INVALID_PARAMETER;
533}
534
535
536/**
537 * Initializes the CMOS data for one harddisk.
538 */
539static void pcbiosCmosInitHardDisk(PPDMDEVINS pDevIns, int offType, int offInfo, PCPDMMEDIAGEOMETRY pLCHSGeometry)
540{
541 Log2(("%s: offInfo=%#x: LCHS=%d/%d/%d\n", __FUNCTION__, offInfo, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
542 if (offType)
543 pcbiosCmosWrite(pDevIns, offType, 47);
544 /* Cylinders low */
545 pcbiosCmosWrite(pDevIns, offInfo + 0, RT_MIN(pLCHSGeometry->cCylinders, 1024) & 0xff);
546 /* Cylinders high */
547 pcbiosCmosWrite(pDevIns, offInfo + 1, RT_MIN(pLCHSGeometry->cCylinders, 1024) >> 8);
548 /* Heads */
549 pcbiosCmosWrite(pDevIns, offInfo + 2, pLCHSGeometry->cHeads);
550 /* Landing zone low */
551 pcbiosCmosWrite(pDevIns, offInfo + 3, 0xff);
552 /* Landing zone high */
553 pcbiosCmosWrite(pDevIns, offInfo + 4, 0xff);
554 /* Write precomp low */
555 pcbiosCmosWrite(pDevIns, offInfo + 5, 0xff);
556 /* Write precomp high */
557 pcbiosCmosWrite(pDevIns, offInfo + 6, 0xff);
558 /* Sectors */
559 pcbiosCmosWrite(pDevIns, offInfo + 7, pLCHSGeometry->cSectors);
560}
561
562
563/**
564 * Set logical CHS geometry for a hard disk
565 *
566 * @returns VBox status code.
567 * @param pBase Base interface for the device.
568 * @param pHardDisk The hard disk.
569 * @param pLCHSGeometry Where to store the geometry settings.
570 */
571static int setLogicalDiskGeometry(PPDMIBASE pBase, PPDMIMEDIA pHardDisk, PPDMMEDIAGEOMETRY pLCHSGeometry)
572{
573 RT_NOREF1(pBase);
574
575 PDMMEDIAGEOMETRY LCHSGeometry;
576 int rc = pHardDisk->pfnBiosGetLCHSGeometry(pHardDisk, &LCHSGeometry);
577 if ( rc == VERR_PDM_GEOMETRY_NOT_SET
578 || LCHSGeometry.cCylinders == 0
579 || LCHSGeometry.cHeads == 0
580 || LCHSGeometry.cHeads > 255
581 || LCHSGeometry.cSectors == 0
582 || LCHSGeometry.cSectors > 63)
583 {
584 /* No LCHS geometry, autodetect and set. */
585 rc = biosGuessDiskLCHS(pHardDisk, &LCHSGeometry);
586 if (RT_FAILURE(rc))
587 {
588 /* Try if PCHS geometry works, otherwise fall back. */
589 rc = pHardDisk->pfnBiosGetPCHSGeometry(pHardDisk, &LCHSGeometry);
590 }
591 if ( RT_FAILURE(rc)
592 || LCHSGeometry.cCylinders == 0
593 || LCHSGeometry.cCylinders > 1024
594 || LCHSGeometry.cHeads == 0
595 || LCHSGeometry.cHeads > 16
596 || LCHSGeometry.cSectors == 0
597 || LCHSGeometry.cSectors > 63)
598 {
599 uint64_t cSectors = pHardDisk->pfnGetSize(pHardDisk) / 512;
600 if (cSectors / 16 / 63 <= 1024)
601 {
602 LCHSGeometry.cCylinders = RT_MAX(cSectors / 16 / 63, 1);
603 LCHSGeometry.cHeads = 16;
604 }
605 else if (cSectors / 32 / 63 <= 1024)
606 {
607 LCHSGeometry.cCylinders = RT_MAX(cSectors / 32 / 63, 1);
608 LCHSGeometry.cHeads = 32;
609 }
610 else if (cSectors / 64 / 63 <= 1024)
611 {
612 LCHSGeometry.cCylinders = cSectors / 64 / 63;
613 LCHSGeometry.cHeads = 64;
614 }
615 else if (cSectors / 128 / 63 <= 1024)
616 {
617 LCHSGeometry.cCylinders = cSectors / 128 / 63;
618 LCHSGeometry.cHeads = 128;
619 }
620 else
621 {
622 LCHSGeometry.cCylinders = RT_MIN(cSectors / 255 / 63, 1024);
623 LCHSGeometry.cHeads = 255;
624 }
625 LCHSGeometry.cSectors = 63;
626
627 }
628 rc = pHardDisk->pfnBiosSetLCHSGeometry(pHardDisk, &LCHSGeometry);
629 if (rc == VERR_VD_IMAGE_READ_ONLY)
630 {
631 LogRel(("PcBios: ATA failed to update LCHS geometry, read only\n"));
632 rc = VINF_SUCCESS;
633 }
634 else if (rc == VERR_PDM_GEOMETRY_NOT_SET)
635 {
636 LogRel(("PcBios: ATA failed to update LCHS geometry, backend refused\n"));
637 rc = VINF_SUCCESS;
638 }
639 }
640
641 *pLCHSGeometry = LCHSGeometry;
642
643 return rc;
644}
645
646
647/**
648 * Get logical CHS geometry for a hard disk, intended for SCSI/SAS drives
649 * with no physical geometry.
650 *
651 * @returns VBox status code.
652 * @param pHardDisk The hard disk.
653 * @param pLCHSGeometry Where to store the geometry settings.
654 */
655static int getLogicalDiskGeometry(PPDMIMEDIA pHardDisk, PPDMMEDIAGEOMETRY pLCHSGeometry)
656{
657 PDMMEDIAGEOMETRY LCHSGeometry;
658 int rc = VINF_SUCCESS;
659
660 rc = pHardDisk->pfnBiosGetLCHSGeometry(pHardDisk, &LCHSGeometry);
661 if ( rc == VERR_PDM_GEOMETRY_NOT_SET
662 || LCHSGeometry.cCylinders == 0
663 || LCHSGeometry.cHeads == 0
664 || LCHSGeometry.cHeads > 255
665 || LCHSGeometry.cSectors == 0
666 || LCHSGeometry.cSectors > 63)
667 {
668 /* Unlike the ATA case, if the image does not provide valid logical
669 * geometry, we leave things alone and let the BIOS decide what the
670 * logical geometry should be.
671 */
672 rc = VERR_PDM_GEOMETRY_NOT_SET;
673 }
674 else
675 *pLCHSGeometry = LCHSGeometry;
676
677 return rc;
678}
679
680
681/**
682 * Get BIOS boot code from enmBootDevice in order
683 *
684 * @todo r=bird: This is a rather silly function since the conversion is 1:1.
685 */
686static uint8_t getBiosBootCode(PDEVPCBIOS pThis, unsigned iOrder)
687{
688 switch (pThis->aenmBootDevice[iOrder])
689 {
690 case DEVPCBIOSBOOT_NONE:
691 return 0;
692 case DEVPCBIOSBOOT_FLOPPY:
693 return 1;
694 case DEVPCBIOSBOOT_HD:
695 return 2;
696 case DEVPCBIOSBOOT_DVD:
697 return 3;
698 case DEVPCBIOSBOOT_LAN:
699 return 4;
700 default:
701 AssertMsgFailed(("aenmBootDevice[%d]=%d\n", iOrder, pThis->aenmBootDevice[iOrder]));
702 return 0;
703 }
704}
705
706
707/**
708 * @interface_method_impl{PDMDEVREG,pfnInitComplete}
709 *
710 * This routine will write information needed by the bios to the CMOS.
711 *
712 * @see http://www.brl.ntt.co.jp/people/takehiko/interrupt/CMOS.LST.txt for
713 * a description of standard and non-standard CMOS registers.
714 */
715static DECLCALLBACK(int) pcbiosInitComplete(PPDMDEVINS pDevIns)
716{
717 PDEVPCBIOS pThis = PDMINS_2_DATA(pDevIns, PDEVPCBIOS);
718 uint32_t u32;
719 unsigned i;
720 PUVM pUVM = PDMDevHlpGetUVM(pDevIns); AssertRelease(pUVM);
721 PPDMIMEDIA apHDs[4] = {0};
722 LogFlow(("pcbiosInitComplete:\n"));
723
724 PVM pVM = PDMDevHlpGetVM(pDevIns);
725 uint64_t const cbRamSize = MMR3PhysGetRamSize(pVM);
726 uint32_t const cbBelow4GB = MMR3PhysGetRamSizeBelow4GB(pVM);
727 uint64_t const cbAbove4GB = MMR3PhysGetRamSizeAbove4GB(pVM);
728
729 /*
730 * Memory sizes.
731 */
732 /* base memory. */
733 u32 = cbRamSize > 640 ? 640 : (uint32_t)cbRamSize / _1K; /* <-- this test is wrong, but it doesn't matter since we never assign less than 1MB */
734 pcbiosCmosWrite(pDevIns, 0x15, RT_BYTE1(u32)); /* 15h - Base Memory in K, Low Byte */
735 pcbiosCmosWrite(pDevIns, 0x16, RT_BYTE2(u32)); /* 16h - Base Memory in K, High Byte */
736
737 /* Extended memory, up to 65MB */
738 u32 = cbRamSize >= 65 * _1M ? 0xffff : ((uint32_t)cbRamSize - _1M) / _1K;
739 pcbiosCmosWrite(pDevIns, 0x17, RT_BYTE1(u32)); /* 17h - Extended Memory in K, Low Byte */
740 pcbiosCmosWrite(pDevIns, 0x18, RT_BYTE2(u32)); /* 18h - Extended Memory in K, High Byte */
741 pcbiosCmosWrite(pDevIns, 0x30, RT_BYTE1(u32)); /* 30h - Extended Memory in K, Low Byte */
742 pcbiosCmosWrite(pDevIns, 0x31, RT_BYTE2(u32)); /* 31h - Extended Memory in K, High Byte */
743
744 /* Bochs BIOS specific? Anyway, it's the amount of memory above 16MB
745 and below 4GB (as it can only hold 4GB-16M). We have to chop off the
746 top 32MB or it conflict with what the ACPI tables return. (Should these
747 be adjusted, we still have to chop it at 0xfffc0000 or it'll conflict
748 with the high BIOS mapping.) */
749 if (cbRamSize > 16 * _1M)
750 u32 = (RT_MIN(cbBelow4GB, UINT32_C(0xfe000000)) - 16U * _1M) / _64K;
751 else
752 u32 = 0;
753 pcbiosCmosWrite(pDevIns, 0x34, RT_BYTE1(u32));
754 pcbiosCmosWrite(pDevIns, 0x35, RT_BYTE2(u32));
755
756 /* Bochs/VBox BIOS specific way of specifying memory above 4GB in 64KB units.
757 Bochs got these in a different location which we've already used for SATA,
758 it also lacks the last two. */
759 uint64_t c64KBAbove4GB = cbAbove4GB / _64K;
760 /* Make sure it doesn't hit the limits of the current BIOS code (RAM limit of ~255TB). */
761 AssertLogRelMsgReturn((c64KBAbove4GB >> (3 * 8)) < 255, ("%#RX64\n", c64KBAbove4GB), VERR_OUT_OF_RANGE);
762 pcbiosCmosWrite(pDevIns, 0x61, RT_BYTE1(c64KBAbove4GB));
763 pcbiosCmosWrite(pDevIns, 0x62, RT_BYTE2(c64KBAbove4GB));
764 pcbiosCmosWrite(pDevIns, 0x63, RT_BYTE3(c64KBAbove4GB));
765 pcbiosCmosWrite(pDevIns, 0x64, RT_BYTE4(c64KBAbove4GB));
766 pcbiosCmosWrite(pDevIns, 0x65, RT_BYTE5(c64KBAbove4GB));
767
768 /*
769 * Number of CPUs.
770 */
771 pcbiosCmosWrite(pDevIns, 0x60, pThis->cCpus & 0xff);
772
773 /*
774 * APIC mode.
775 */
776 pcbiosCmosWrite(pDevIns, 0x78, pThis->u8APICMode);
777
778 /*
779 * Bochs BIOS specifics - boot device.
780 * We do both new and old (ami-style) settings.
781 * See rombios.c line ~7215 (int19_function).
782 */
783
784 uint8_t reg3d = getBiosBootCode(pThis, 0) | (getBiosBootCode(pThis, 1) << 4);
785 uint8_t reg38 = /* pcbiosCmosRead(pDevIns, 0x38) | */ getBiosBootCode(pThis, 2) << 4;
786 /* This is an extension. Bochs BIOS normally supports only 3 boot devices. */
787 uint8_t reg3c = getBiosBootCode(pThis, 3) | (pThis->uBootDelay << 4);
788 pcbiosCmosWrite(pDevIns, 0x3d, reg3d);
789 pcbiosCmosWrite(pDevIns, 0x38, reg38);
790 pcbiosCmosWrite(pDevIns, 0x3c, reg3c);
791
792 /*
793 * PXE debug option.
794 */
795 pcbiosCmosWrite(pDevIns, 0x3f, pThis->u8PXEDebug);
796
797 /*
798 * Network boot device list.
799 */
800 for (i = 0; i < NET_BOOT_DEVS; ++i)
801 {
802 pcbiosCmosWrite(pDevIns, 0x82 + i * 2, RT_BYTE1(pThis->au16NetBootDev[i]));
803 pcbiosCmosWrite(pDevIns, 0x83 + i * 2, RT_BYTE2(pThis->au16NetBootDev[i]));
804 }
805
806 /*
807 * Floppy drive type.
808 */
809 uint32_t cFDs = 0;
810 u32 = 0;
811 for (i = 0; i < 2; i++)
812 {
813 PPDMIBASE pBase;
814 int rc = PDMR3QueryLun(pUVM, pThis->pszFDDevice, 0, i, &pBase);
815 if (RT_SUCCESS(rc))
816 {
817 PPDMIMEDIA pFD = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIA);
818 if (pFD)
819 {
820 cFDs++;
821 unsigned cShift = i == 0 ? 4 : 0;
822 switch (pFD->pfnGetType(pFD))
823 {
824 case PDMMEDIATYPE_FLOPPY_360: u32 |= 1 << cShift; break;
825 case PDMMEDIATYPE_FLOPPY_1_20: u32 |= 2 << cShift; break;
826 case PDMMEDIATYPE_FLOPPY_720: u32 |= 3 << cShift; break;
827 case PDMMEDIATYPE_FLOPPY_1_44: u32 |= 4 << cShift; break;
828 case PDMMEDIATYPE_FLOPPY_2_88: u32 |= 5 << cShift; break;
829 case PDMMEDIATYPE_FLOPPY_FAKE_15_6: u32 |= 14 << cShift; break;
830 case PDMMEDIATYPE_FLOPPY_FAKE_63_5: u32 |= 15 << cShift; break;
831 default: AssertFailed(); break;
832 }
833 }
834 }
835 }
836 pcbiosCmosWrite(pDevIns, 0x10, u32); /* 10h - Floppy Drive Type */
837
838 /*
839 * Equipment byte.
840 */
841 if (cFDs > 0)
842 u32 = ((cFDs - 1) << 6) | 0x01; /* floppy installed, additional drives. */
843 else
844 u32 = 0x00; /* floppy not installed. */
845 u32 |= RT_BIT(1); /* math coprocessor installed */
846 u32 |= RT_BIT(2); /* keyboard enabled (or mouse?) */
847 u32 |= RT_BIT(3); /* display enabled (monitory type is 0, i.e. vga) */
848 pcbiosCmosWrite(pDevIns, 0x14, u32); /* 14h - Equipment Byte */
849
850 /*
851 * IDE harddisks.
852 */
853 for (i = 0; i < RT_ELEMENTS(apHDs); i++)
854 {
855 PPDMIBASE pBase;
856 int rc = PDMR3QueryLun(pUVM, pThis->pszHDDevice, 0, i, &pBase);
857 if (RT_SUCCESS(rc))
858 apHDs[i] = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIA);
859 if ( apHDs[i]
860 && ( apHDs[i]->pfnGetType(apHDs[i]) != PDMMEDIATYPE_HARD_DISK
861 || !apHDs[i]->pfnBiosIsVisible(apHDs[i])))
862 apHDs[i] = NULL;
863 if (apHDs[i])
864 {
865 PDMMEDIAGEOMETRY LCHSGeometry;
866 int rc2 = setLogicalDiskGeometry(pBase, apHDs[i], &LCHSGeometry);
867 AssertRC(rc2);
868
869 if (i < 4)
870 {
871 /* Award BIOS extended drive types for first to fourth disk.
872 * Used by the BIOS for setting the logical geometry. */
873 int offType, offInfo;
874 switch (i)
875 {
876 case 0:
877 offType = 0x19;
878 offInfo = 0x1e;
879 break;
880 case 1:
881 offType = 0x1a;
882 offInfo = 0x26;
883 break;
884 case 2:
885 offType = 0x00;
886 offInfo = 0x67;
887 break;
888 case 3:
889 default:
890 offType = 0x00;
891 offInfo = 0x70;
892 break;
893 }
894 pcbiosCmosInitHardDisk(pDevIns, offType, offInfo,
895 &LCHSGeometry);
896 }
897 LogRel(("PcBios: ATA LUN#%d LCHS=%u/%u/%u\n", i, LCHSGeometry.cCylinders, LCHSGeometry.cHeads, LCHSGeometry.cSectors));
898 }
899 }
900
901 /* 0Fh means extended and points to 19h, 1Ah */
902 u32 = (apHDs[0] ? 0xf0 : 0) | (apHDs[1] ? 0x0f : 0);
903 pcbiosCmosWrite(pDevIns, 0x12, u32);
904
905 /*
906 * SATA harddisks.
907 */
908 if (pThis->pszSataDevice)
909 {
910 /* Clear pointers to the block devices. */
911 for (i = 0; i < RT_ELEMENTS(apHDs); i++)
912 apHDs[i] = NULL;
913
914 for (i = 0; i < RT_ELEMENTS(apHDs); i++)
915 {
916 PPDMIBASE pBase;
917 int rc = PDMR3QueryLun(pUVM, pThis->pszSataDevice, 0, pThis->iSataHDLUN[i], &pBase);
918 if (RT_SUCCESS(rc))
919 apHDs[i] = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIA);
920 if ( apHDs[i]
921 && ( apHDs[i]->pfnGetType(apHDs[i]) != PDMMEDIATYPE_HARD_DISK
922 || !apHDs[i]->pfnBiosIsVisible(apHDs[i])))
923 apHDs[i] = NULL;
924 if (apHDs[i])
925 {
926 PDMMEDIAGEOMETRY LCHSGeometry;
927 rc = setLogicalDiskGeometry(pBase, apHDs[i], &LCHSGeometry);
928 AssertRC(rc);
929
930 if (i < 4)
931 {
932 /* Award BIOS extended drive types for first to fourth disk.
933 * Used by the BIOS for setting the logical geometry. */
934 int offInfo;
935 switch (i)
936 {
937 case 0:
938 offInfo = 0x40;
939 break;
940 case 1:
941 offInfo = 0x48;
942 break;
943 case 2:
944 offInfo = 0x50;
945 break;
946 case 3:
947 default:
948 offInfo = 0x58;
949 break;
950 }
951 pcbiosCmosInitHardDisk(pDevIns, 0x00, offInfo,
952 &LCHSGeometry);
953 }
954 LogRel(("PcBios: SATA LUN#%d LCHS=%u/%u/%u\n", i, LCHSGeometry.cCylinders, LCHSGeometry.cHeads, LCHSGeometry.cSectors));
955 }
956 }
957 }
958
959 /*
960 * SCSI harddisks. Not handled quite the same as SATA.
961 */
962 if (pThis->pszScsiDevice)
963 {
964 /* Clear pointers to the block devices. */
965 for (i = 0; i < RT_ELEMENTS(apHDs); i++)
966 apHDs[i] = NULL;
967
968 for (i = 0; i < RT_ELEMENTS(apHDs); i++)
969 {
970 PPDMIBASE pBase;
971 int rc = PDMR3QueryLun(pUVM, pThis->pszScsiDevice, 0, pThis->iScsiHDLUN[i], &pBase);
972 if (RT_SUCCESS(rc))
973 apHDs[i] = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIA);
974 if ( apHDs[i]
975 && ( apHDs[i]->pfnGetType(apHDs[i]) != PDMMEDIATYPE_HARD_DISK
976 || !apHDs[i]->pfnBiosIsVisible(apHDs[i])))
977 apHDs[i] = NULL;
978 if (apHDs[i])
979 {
980 PDMMEDIAGEOMETRY LCHSGeometry;
981 rc = getLogicalDiskGeometry(apHDs[i], &LCHSGeometry);
982
983 if (i < 4 && RT_SUCCESS(rc))
984 {
985 /* Extended drive information (for SCSI disks).
986 * Used by the BIOS for setting the logical geometry, but
987 * only if the image provided valid data.
988 */
989 int offInfo;
990 switch (i)
991 {
992 case 0:
993 offInfo = 0x90;
994 break;
995 case 1:
996 offInfo = 0x98;
997 break;
998 case 2:
999 offInfo = 0xa0;
1000 break;
1001 case 3:
1002 default:
1003 offInfo = 0xa8;
1004 break;
1005 }
1006 pcbiosCmosInitHardDisk(pDevIns, 0x00, offInfo, &LCHSGeometry);
1007 LogRel(("PcBios: SCSI LUN#%d LCHS=%u/%u/%u\n", i, LCHSGeometry.cCylinders, LCHSGeometry.cHeads, LCHSGeometry.cSectors));
1008 }
1009 else
1010 LogRel(("PcBios: SCSI LUN#%d LCHS not provided\n", i));
1011 }
1012 }
1013 }
1014
1015 /* Calculate and store AT-style CMOS checksum. */
1016 uint16_t cksum = 0;
1017 for (i = 0x10; i < 0x2e; ++i)
1018 cksum += pcbiosCmosRead(pDevIns, i);
1019 pcbiosCmosWrite(pDevIns, 0x2e, RT_BYTE1(cksum));
1020 pcbiosCmosWrite(pDevIns, 0x2f, RT_BYTE2(cksum));
1021
1022 LogFlow(("%s: returns VINF_SUCCESS\n", __FUNCTION__));
1023 return VINF_SUCCESS;
1024}
1025
1026
1027/**
1028 * @interface_method_impl{PDMDEVREG,pfnMemSetup}
1029 */
1030static DECLCALLBACK(void) pcbiosMemSetup(PPDMDEVINS pDevIns, PDMDEVMEMSETUPCTX enmCtx)
1031{
1032 RT_NOREF1(enmCtx);
1033 PDEVPCBIOS pThis = PDMINS_2_DATA(pDevIns, PDEVPCBIOS);
1034 LogFlow(("pcbiosMemSetup:\n"));
1035
1036 if (pThis->u8IOAPIC)
1037 FwCommonPlantMpsFloatPtr(pDevIns);
1038
1039 /*
1040 * Re-shadow the LAN ROM image and make it RAM/RAM.
1041 *
1042 * This is normally done by the BIOS code, but since we're currently lacking
1043 * the chipset support for this we do it here (and in the constructor).
1044 */
1045 uint32_t cPages = RT_ALIGN_64(pThis->cbLanBoot, PAGE_SIZE) >> PAGE_SHIFT;
1046 RTGCPHYS GCPhys = VBOX_LANBOOT_SEG << 4;
1047 while (cPages > 0)
1048 {
1049 uint8_t abPage[PAGE_SIZE];
1050 int rc;
1051
1052 /* Read the (original) ROM page and write it back to the RAM page. */
1053 rc = PDMDevHlpROMProtectShadow(pDevIns, GCPhys, PAGE_SIZE, PGMROMPROT_READ_ROM_WRITE_RAM);
1054 AssertLogRelRC(rc);
1055
1056 rc = PDMDevHlpPhysRead(pDevIns, GCPhys, abPage, PAGE_SIZE);
1057 AssertLogRelRC(rc);
1058 if (RT_FAILURE(rc))
1059 memset(abPage, 0xcc, sizeof(abPage));
1060
1061 rc = PDMDevHlpPhysWrite(pDevIns, GCPhys, abPage, PAGE_SIZE);
1062 AssertLogRelRC(rc);
1063
1064 /* Switch to the RAM/RAM mode. */
1065 rc = PDMDevHlpROMProtectShadow(pDevIns, GCPhys, PAGE_SIZE, PGMROMPROT_READ_RAM_WRITE_RAM);
1066 AssertLogRelRC(rc);
1067
1068 /* Advance */
1069 GCPhys += PAGE_SIZE;
1070 cPages--;
1071 }
1072}
1073
1074
1075/**
1076 * @interface_method_impl{PDMDEVREG,pfnDestruct}
1077 */
1078static DECLCALLBACK(int) pcbiosDestruct(PPDMDEVINS pDevIns)
1079{
1080 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
1081 PDEVPCBIOS pThis = PDMINS_2_DATA(pDevIns, PDEVPCBIOS);
1082 LogFlow(("pcbiosDestruct:\n"));
1083
1084 /*
1085 * Free MM heap pointers.
1086 */
1087 if (pThis->pu8PcBios)
1088 {
1089 PDMDevHlpMMHeapFree(pDevIns, pThis->pu8PcBios);
1090 pThis->pu8PcBios = NULL;
1091 }
1092
1093 if (pThis->pszPcBiosFile)
1094 {
1095 PDMDevHlpMMHeapFree(pDevIns, pThis->pszPcBiosFile);
1096 pThis->pszPcBiosFile = NULL;
1097 }
1098
1099 if (pThis->pu8LanBoot)
1100 {
1101 PDMDevHlpMMHeapFree(pDevIns, pThis->pu8LanBoot);
1102 pThis->pu8LanBoot = NULL;
1103 }
1104
1105 if (pThis->pszLanBootFile)
1106 {
1107 PDMDevHlpMMHeapFree(pDevIns, pThis->pszLanBootFile);
1108 pThis->pszLanBootFile = NULL;
1109 }
1110
1111 if (pThis->pszHDDevice)
1112 {
1113 PDMDevHlpMMHeapFree(pDevIns, pThis->pszHDDevice);
1114 pThis->pszHDDevice = NULL;
1115 }
1116
1117 if (pThis->pszFDDevice)
1118 {
1119 PDMDevHlpMMHeapFree(pDevIns, pThis->pszFDDevice);
1120 pThis->pszFDDevice = NULL;
1121 }
1122
1123 if (pThis->pszSataDevice)
1124 {
1125 PDMDevHlpMMHeapFree(pDevIns, pThis->pszSataDevice);
1126 pThis->pszSataDevice = NULL;
1127 }
1128
1129 if (pThis->pszScsiDevice)
1130 {
1131 PDMDevHlpMMHeapFree(pDevIns, pThis->pszScsiDevice);
1132 pThis->pszScsiDevice = NULL;
1133 }
1134
1135 return VINF_SUCCESS;
1136}
1137
1138
1139/**
1140 * Convert config value to DEVPCBIOSBOOT.
1141 *
1142 * @returns VBox status code.
1143 * @param pDevIns Device instance data.
1144 * @param pCfg Configuration handle.
1145 * @param pszParam The name of the value to read.
1146 * @param penmBoot Where to store the boot method.
1147 */
1148static int pcbiosBootFromCfg(PPDMDEVINS pDevIns, PCFGMNODE pCfg, const char *pszParam, DEVPCBIOSBOOT *penmBoot)
1149{
1150 char *psz;
1151 int rc = CFGMR3QueryStringAlloc(pCfg, pszParam, &psz);
1152 if (RT_FAILURE(rc))
1153 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
1154 N_("Configuration error: Querying \"%s\" as a string failed"),
1155 pszParam);
1156 if (!strcmp(psz, "DVD") || !strcmp(psz, "CDROM"))
1157 *penmBoot = DEVPCBIOSBOOT_DVD;
1158 else if (!strcmp(psz, "IDE"))
1159 *penmBoot = DEVPCBIOSBOOT_HD;
1160 else if (!strcmp(psz, "FLOPPY"))
1161 *penmBoot = DEVPCBIOSBOOT_FLOPPY;
1162 else if (!strcmp(psz, "LAN"))
1163 *penmBoot = DEVPCBIOSBOOT_LAN;
1164 else if (!strcmp(psz, "NONE"))
1165 *penmBoot = DEVPCBIOSBOOT_NONE;
1166 else
1167 {
1168 PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
1169 N_("Configuration error: The \"%s\" value \"%s\" is unknown"),
1170 pszParam, psz);
1171 rc = VERR_INTERNAL_ERROR;
1172 }
1173 MMR3HeapFree(psz);
1174 return rc;
1175}
1176
1177/**
1178 * @interface_method_impl{PDMDEVREG,pfnConstruct}
1179 */
1180static DECLCALLBACK(int) pcbiosConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
1181{
1182 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
1183 PDEVPCBIOS pThis = PDMINS_2_DATA(pDevIns, PDEVPCBIOS);
1184 int rc;
1185 int cb;
1186 Assert(iInstance == 0); RT_NOREF(iInstance);
1187
1188 /*
1189 * Validate configuration.
1190 */
1191 if (!CFGMR3AreValuesValid(pCfg,
1192 "BootDevice0\0"
1193 "BootDevice1\0"
1194 "BootDevice2\0"
1195 "BootDevice3\0"
1196 "HardDiskDevice\0"
1197 "SataHardDiskDevice\0"
1198 "SataLUN1\0"
1199 "SataLUN2\0"
1200 "SataLUN3\0"
1201 "SataLUN4\0"
1202 "ScsiHardDiskDevice\0"
1203 "ScsiLUN1\0"
1204 "ScsiLUN2\0"
1205 "ScsiLUN3\0"
1206 "ScsiLUN4\0"
1207 "FloppyDevice\0"
1208 "DelayBoot\0"
1209 "BiosRom\0"
1210 "LanBootRom\0"
1211 "PXEDebug\0"
1212 "UUID\0"
1213 "IOAPIC\0"
1214 "APIC\0"
1215 "NumCPUs\0"
1216 "McfgBase\0"
1217 "McfgLength\0"
1218 "DmiBIOSFirmwareMajor\0"
1219 "DmiBIOSFirmwareMinor\0"
1220 "DmiBIOSReleaseDate\0"
1221 "DmiBIOSReleaseMajor\0"
1222 "DmiBIOSReleaseMinor\0"
1223 "DmiBIOSVendor\0"
1224 "DmiBIOSVersion\0"
1225 "DmiSystemFamily\0"
1226 "DmiSystemProduct\0"
1227 "DmiSystemSerial\0"
1228 "DmiSystemSKU\0"
1229 "DmiSystemUuid\0"
1230 "DmiSystemVendor\0"
1231 "DmiSystemVersion\0"
1232 "DmiBoardAssetTag\0"
1233 "DmiBoardBoardType\0"
1234 "DmiBoardLocInChass\0"
1235 "DmiBoardProduct\0"
1236 "DmiBoardSerial\0"
1237 "DmiBoardVendor\0"
1238 "DmiBoardVersion\0"
1239 "DmiChassisAssetTag\0"
1240 "DmiChassisSerial\0"
1241 "DmiChassisType\0"
1242 "DmiChassisVendor\0"
1243 "DmiChassisVersion\0"
1244 "DmiProcManufacturer\0"
1245 "DmiProcVersion\0"
1246 "DmiOEMVBoxVer\0"
1247 "DmiOEMVBoxRev\0"
1248 "DmiUseHostInfo\0"
1249 "DmiExposeMemoryTable\0"
1250 "DmiExposeProcInf\0"
1251 "CheckShutdownStatusForSoftReset\0"
1252 "ClearShutdownStatusOnHardReset\0"
1253 ))
1254 return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
1255 N_("Invalid configuration for device pcbios device"));
1256
1257 /*
1258 * Init the data.
1259 */
1260 rc = CFGMR3QueryU16Def(pCfg, "NumCPUs", &pThis->cCpus, 1);
1261 if (RT_FAILURE(rc))
1262 return PDMDEV_SET_ERROR(pDevIns, rc,
1263 N_("Configuration error: Querying \"NumCPUs\" as integer failed"));
1264
1265 rc = CFGMR3QueryU64Def(pCfg, "McfgBase", &pThis->u64McfgBase, 0);
1266 if (RT_FAILURE(rc))
1267 return PDMDEV_SET_ERROR(pDevIns, rc,
1268 N_("Configuration error: Querying \"\" as integer failed"));
1269 rc = CFGMR3QueryU64Def(pCfg, "McfgLength", &pThis->cbMcfgLength, 0);
1270 if (RT_FAILURE(rc))
1271 return PDMDEV_SET_ERROR(pDevIns, rc,
1272 N_("Configuration error: Querying \"McfgLength\" as integer failed"));
1273
1274
1275 LogRel(("PcBios: [SMP] BIOS with %u CPUs\n", pThis->cCpus));
1276
1277 rc = CFGMR3QueryU8Def(pCfg, "IOAPIC", &pThis->u8IOAPIC, 1);
1278 if (RT_FAILURE (rc))
1279 return PDMDEV_SET_ERROR(pDevIns, rc,
1280 N_("Configuration error: Failed to read \"IOAPIC\""));
1281
1282 rc = CFGMR3QueryU8Def(pCfg, "APIC", &pThis->u8APICMode, 1);
1283 if (RT_FAILURE (rc))
1284 return PDMDEV_SET_ERROR(pDevIns, rc,
1285 N_("Configuration error: Failed to read \"APIC\""));
1286
1287 static const char * const s_apszBootDevices[] = { "BootDevice0", "BootDevice1", "BootDevice2", "BootDevice3" };
1288 Assert(RT_ELEMENTS(s_apszBootDevices) == RT_ELEMENTS(pThis->aenmBootDevice));
1289 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aenmBootDevice); i++)
1290 {
1291 rc = pcbiosBootFromCfg(pDevIns, pCfg, s_apszBootDevices[i], &pThis->aenmBootDevice[i]);
1292 if (RT_FAILURE(rc))
1293 return rc;
1294 }
1295
1296 rc = CFGMR3QueryStringAlloc(pCfg, "HardDiskDevice", &pThis->pszHDDevice);
1297 if (RT_FAILURE(rc))
1298 return PDMDEV_SET_ERROR(pDevIns, rc,
1299 N_("Configuration error: Querying \"HardDiskDevice\" as a string failed"));
1300
1301 rc = CFGMR3QueryStringAlloc(pCfg, "FloppyDevice", &pThis->pszFDDevice);
1302 if (RT_FAILURE(rc))
1303 return PDMDEV_SET_ERROR(pDevIns, rc,
1304 N_("Configuration error: Querying \"FloppyDevice\" as a string failed"));
1305
1306 rc = CFGMR3QueryStringAlloc(pCfg, "SataHardDiskDevice", &pThis->pszSataDevice);
1307 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1308 pThis->pszSataDevice = NULL;
1309 else if (RT_FAILURE(rc))
1310 return PDMDEV_SET_ERROR(pDevIns, rc,
1311 N_("Configuration error: Querying \"SataHardDiskDevice\" as a string failed"));
1312
1313 if (pThis->pszSataDevice)
1314 {
1315 static const char * const s_apszSataDisks[] =
1316 { "SataLUN1", "SataLUN2", "SataLUN3", "SataLUN4" };
1317 Assert(RT_ELEMENTS(s_apszSataDisks) == RT_ELEMENTS(pThis->iSataHDLUN));
1318 for (unsigned i = 0; i < RT_ELEMENTS(pThis->iSataHDLUN); i++)
1319 {
1320 rc = CFGMR3QueryU32(pCfg, s_apszSataDisks[i], &pThis->iSataHDLUN[i]);
1321 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1322 pThis->iSataHDLUN[i] = i;
1323 else if (RT_FAILURE(rc))
1324 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
1325 N_("Configuration error: Querying \"%s\" as a string failed"), s_apszSataDisks);
1326 }
1327 }
1328
1329 /* Repeat the exercise for SCSI drives. */
1330 rc = CFGMR3QueryStringAlloc(pCfg, "ScsiHardDiskDevice", &pThis->pszScsiDevice);
1331 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1332 pThis->pszScsiDevice = NULL;
1333 else if (RT_FAILURE(rc))
1334 return PDMDEV_SET_ERROR(pDevIns, rc,
1335 N_("Configuration error: Querying \"ScsiHardDiskDevice\" as a string failed"));
1336
1337 if (pThis->pszScsiDevice)
1338 {
1339 static const char * const s_apszScsiDisks[] =
1340 { "ScsiLUN1", "ScsiLUN2", "ScsiLUN3", "ScsiLUN4" };
1341 Assert(RT_ELEMENTS(s_apszScsiDisks) == RT_ELEMENTS(pThis->iScsiHDLUN));
1342 for (unsigned i = 0; i < RT_ELEMENTS(pThis->iScsiHDLUN); i++)
1343 {
1344 rc = CFGMR3QueryU32(pCfg, s_apszScsiDisks[i], &pThis->iScsiHDLUN[i]);
1345 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1346 pThis->iScsiHDLUN[i] = i;
1347 else if (RT_FAILURE(rc))
1348 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
1349 N_("Configuration error: Querying \"%s\" as a string failed"), s_apszScsiDisks);
1350 }
1351 }
1352
1353
1354 /*
1355 * Register I/O Ports and PC BIOS.
1356 */
1357 rc = PDMDevHlpIOPortRegister(pDevIns, 0x400, 4, NULL, pcbiosIOPortWrite, pcbiosIOPortRead,
1358 NULL, NULL, "Bochs PC BIOS - Panic & Debug");
1359 if (RT_FAILURE(rc))
1360 return rc;
1361 rc = pcbiosRegisterShutdown(pDevIns, pThis, true /* fNewShutdownPort */);
1362 if (RT_FAILURE(rc))
1363 return rc;
1364
1365 /*
1366 * Register SSM handlers, for remembering which shutdown port to use.
1367 */
1368 rc = PDMDevHlpSSMRegisterEx(pDevIns, PCBIOS_SSM_VERSION, 1 /* cbGuess */, NULL,
1369 NULL, NULL, NULL,
1370 NULL, pcbiosSaveExec, NULL,
1371 pcbiosLoadPrep, pcbiosLoadExec, pcbiosLoadDone);
1372
1373 /*
1374 * Read the PXE debug logging option.
1375 */
1376 rc = CFGMR3QueryU8Def(pCfg, "PXEDebug", &pThis->u8PXEDebug, false);
1377 if (RT_FAILURE(rc))
1378 return PDMDEV_SET_ERROR(pDevIns, rc,
1379 N_("Configuration error: Querying \"PXEDebug\" as integer failed"));
1380
1381 /* Clear the net boot device list. All bits set invokes old behavior,
1382 * as if no second CMOS bank was present.
1383 */
1384 memset(&pThis->au16NetBootDev, 0xff, sizeof(pThis->au16NetBootDev));
1385
1386 /*
1387 * Determine the network boot order.
1388 */
1389 PCFGMNODE pCfgNetBoot = CFGMR3GetChild(pCfg, "NetBoot");
1390 if (pCfgNetBoot == NULL)
1391 {
1392 /* Do nothing. */
1393 rc = VINF_SUCCESS;
1394 }
1395 else
1396 {
1397 PCFGMNODE pCfgNetBootDevice;
1398 uint8_t u8PciBus;
1399 uint8_t u8PciDev;
1400 uint8_t u8PciFn;
1401 uint16_t u16BusDevFn;
1402 char szIndex[] = "?";
1403
1404 Assert(pCfgNetBoot);
1405 for (unsigned i = 0; i < NET_BOOT_DEVS; ++i)
1406 {
1407 szIndex[0] = '0' + i;
1408 pCfgNetBootDevice = CFGMR3GetChild(pCfgNetBoot, szIndex);
1409
1410 rc = CFGMR3QueryU8(pCfgNetBootDevice, "PCIBusNo", &u8PciBus);
1411 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
1412 {
1413 /* Do nothing and stop iterating. */
1414 rc = VINF_SUCCESS;
1415 break;
1416 }
1417 else if (RT_FAILURE(rc))
1418 return PDMDEV_SET_ERROR(pDevIns, rc,
1419 N_("Configuration error: Querying \"Netboot/x/PCIBusNo\" as integer failed"));
1420 rc = CFGMR3QueryU8(pCfgNetBootDevice, "PCIDeviceNo", &u8PciDev);
1421 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
1422 {
1423 /* Do nothing and stop iterating. */
1424 rc = VINF_SUCCESS;
1425 break;
1426 }
1427 else if (RT_FAILURE(rc))
1428 return PDMDEV_SET_ERROR(pDevIns, rc,
1429 N_("Configuration error: Querying \"Netboot/x/PCIDeviceNo\" as integer failed"));
1430 rc = CFGMR3QueryU8(pCfgNetBootDevice, "PCIFunctionNo", &u8PciFn);
1431 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
1432 {
1433 /* Do nothing and stop iterating. */
1434 rc = VINF_SUCCESS;
1435 break;
1436 }
1437 else if (RT_FAILURE(rc))
1438 return PDMDEV_SET_ERROR(pDevIns, rc,
1439 N_("Configuration error: Querying \"Netboot/x/PCIFunctionNo\" as integer failed"));
1440 u16BusDevFn = (((uint16_t)u8PciBus) << 8) | ((u8PciDev & 0x1F) << 3) | (u8PciFn & 0x7);
1441 pThis->au16NetBootDev[i] = u16BusDevFn;
1442 }
1443 }
1444
1445 /*
1446 * Get the system BIOS ROM file name.
1447 */
1448 rc = CFGMR3QueryStringAlloc(pCfg, "BiosRom", &pThis->pszPcBiosFile);
1449 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1450 {
1451 pThis->pszPcBiosFile = NULL;
1452 rc = VINF_SUCCESS;
1453 }
1454 else if (RT_FAILURE(rc))
1455 return PDMDEV_SET_ERROR(pDevIns, rc,
1456 N_("Configuration error: Querying \"BiosRom\" as a string failed"));
1457 else if (!*pThis->pszPcBiosFile)
1458 {
1459 MMR3HeapFree(pThis->pszPcBiosFile);
1460 pThis->pszPcBiosFile = NULL;
1461 }
1462
1463 /*
1464 * Get the CPU arch so we can load the appropriate ROMs.
1465 */
1466 PVM pVM = PDMDevHlpGetVM(pDevIns);
1467 CPUMMICROARCH const enmMicroarch = pVM ? pVM->cpum.ro.GuestFeatures.enmMicroarch : kCpumMicroarch_Intel_P6;
1468
1469 if (pThis->pszPcBiosFile)
1470 {
1471 /*
1472 * Load the BIOS ROM.
1473 */
1474 RTFILE hFilePcBios;
1475 rc = RTFileOpen(&hFilePcBios, pThis->pszPcBiosFile,
1476 RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
1477 if (RT_SUCCESS(rc))
1478 {
1479 /* Figure the size and check restrictions. */
1480 uint64_t cbPcBios;
1481 rc = RTFileGetSize(hFilePcBios, &cbPcBios);
1482 if (RT_SUCCESS(rc))
1483 {
1484 pThis->cbPcBios = (uint32_t)cbPcBios;
1485 if ( RT_ALIGN(pThis->cbPcBios, _64K) == pThis->cbPcBios
1486 && pThis->cbPcBios == cbPcBios
1487 && pThis->cbPcBios <= 32 * _64K
1488 && pThis->cbPcBios >= _64K)
1489 {
1490 pThis->pu8PcBios = (uint8_t *)PDMDevHlpMMHeapAlloc(pDevIns, pThis->cbPcBios);
1491 if (pThis->pu8PcBios)
1492 {
1493 rc = RTFileRead(hFilePcBios, pThis->pu8PcBios, pThis->cbPcBios, NULL);
1494 if (RT_FAILURE(rc))
1495 rc = PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
1496 N_("Error reading the BIOS image ('%s)"), pThis->pszPcBiosFile);
1497 }
1498 else
1499 rc = PDMDevHlpVMSetError(pDevIns, VERR_NO_MEMORY, RT_SRC_POS,
1500 N_("Failed to allocate %#x bytes for loading the BIOS image"),
1501 pThis->cbPcBios);
1502 }
1503 else
1504 rc = PDMDevHlpVMSetError(pDevIns, VERR_OUT_OF_RANGE, RT_SRC_POS,
1505 N_("Invalid system BIOS file size ('%s'): %#llx (%llu)"),
1506 pThis->pszPcBiosFile, cbPcBios, cbPcBios);
1507 }
1508 else
1509 rc = PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
1510 N_("Failed to query the system BIOS file size ('%s')"),
1511 pThis->pszPcBiosFile);
1512 RTFileClose(hFilePcBios);
1513 }
1514 else
1515 rc = PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
1516 N_("Failed to open system BIOS file '%s'"), pThis->pszPcBiosFile);
1517 if (RT_FAILURE(rc))
1518 return rc;
1519
1520 LogRel(("PcBios: Using BIOS ROM '%s' with a size of %#x bytes\n", pThis->pszPcBiosFile, pThis->cbPcBios));
1521 }
1522 else
1523 {
1524 /*
1525 * Use one of the embedded BIOS ROM images.
1526 */
1527 uint8_t const *pbBios;
1528 uint32_t cbBios;
1529 if ( enmMicroarch == kCpumMicroarch_Intel_8086
1530 || enmMicroarch == kCpumMicroarch_Intel_80186
1531 || enmMicroarch == kCpumMicroarch_NEC_V20
1532 || enmMicroarch == kCpumMicroarch_NEC_V30)
1533 {
1534 pbBios = g_abPcBiosBinary8086;
1535 cbBios = g_cbPcBiosBinary8086;
1536 LogRel(("PcBios: Using the 8086 BIOS image!\n"));
1537 }
1538 else if (enmMicroarch == kCpumMicroarch_Intel_80286)
1539 {
1540 pbBios = g_abPcBiosBinary286;
1541 cbBios = g_cbPcBiosBinary286;
1542 LogRel(("PcBios: Using the 286 BIOS image!\n"));
1543 }
1544 else
1545 {
1546 pbBios = g_abPcBiosBinary386;
1547 cbBios = g_cbPcBiosBinary386;
1548 LogRel(("PcBios: Using the 386+ BIOS image.\n"));
1549 }
1550 pThis->pu8PcBios = (uint8_t *)PDMDevHlpMMHeapAlloc(pDevIns, cbBios);
1551 if (pThis->pu8PcBios)
1552 {
1553 pThis->cbPcBios = cbBios;
1554 memcpy(pThis->pu8PcBios, pbBios, cbBios);
1555 }
1556 else
1557 return PDMDevHlpVMSetError(pDevIns, VERR_NO_MEMORY, RT_SRC_POS,
1558 N_("Failed to allocate %#x bytes for loading the embedded BIOS image"), cbBios);
1559 }
1560 const uint8_t *pu8PcBiosBinary = pThis->pu8PcBios;
1561 uint32_t cbPcBiosBinary = pThis->cbPcBios;
1562
1563 /*
1564 * Query the machine's UUID for SMBIOS/DMI use.
1565 */
1566 RTUUID uuid;
1567 rc = CFGMR3QueryBytes(pCfg, "UUID", &uuid, sizeof(uuid));
1568 if (RT_FAILURE(rc))
1569 return PDMDEV_SET_ERROR(pDevIns, rc,
1570 N_("Configuration error: Querying \"UUID\" failed"));
1571
1572 /* Convert the UUID to network byte order. Not entirely straightforward as parts are MSB already... */
1573 uuid.Gen.u32TimeLow = RT_H2BE_U32(uuid.Gen.u32TimeLow);
1574 uuid.Gen.u16TimeMid = RT_H2BE_U16(uuid.Gen.u16TimeMid);
1575 uuid.Gen.u16TimeHiAndVersion = RT_H2BE_U16(uuid.Gen.u16TimeHiAndVersion);
1576 uint16_t cbDmiTables = 0;
1577 uint16_t cNumDmiTables = 0;
1578 rc = FwCommonPlantDMITable(pDevIns, pThis->au8DMIPage, VBOX_DMI_TABLE_SIZE,
1579 &uuid, pCfg, pThis->cCpus, &cbDmiTables, &cNumDmiTables);
1580 if (RT_FAILURE(rc))
1581 return rc;
1582
1583 for (unsigned i = 0; i < pThis->cbPcBios; i += 16)
1584 {
1585 /* If the DMI table is located at the expected place, patch the DMI table length and the checksum. */
1586 if ( pThis->pu8PcBios[i + 0x00] == '_'
1587 && pThis->pu8PcBios[i + 0x01] == 'D'
1588 && pThis->pu8PcBios[i + 0x02] == 'M'
1589 && pThis->pu8PcBios[i + 0x03] == 'I'
1590 && pThis->pu8PcBios[i + 0x04] == '_'
1591 && *(uint16_t*)&pThis->pu8PcBios[i + 0x06] == 0)
1592 {
1593 *(uint16_t*)&pThis->pu8PcBios[i + 0x06] = RT_H2LE_U16(cbDmiTables);
1594 *(uint16_t*)&pThis->pu8PcBios[i + 0x0C] = RT_H2LE_U16(cNumDmiTables);
1595 uint8_t u8Sum = 0;
1596 for (unsigned j = 0; j < pThis->cbPcBios; j++)
1597 if (j != i + 0x05)
1598 u8Sum += pThis->pu8PcBios[j];
1599 pThis->pu8PcBios[i + 0x05] = -u8Sum;
1600 break;
1601 }
1602 }
1603
1604 if (pThis->u8IOAPIC)
1605 {
1606 FwCommonPlantMpsTable(pDevIns, pThis->au8DMIPage + VBOX_DMI_TABLE_SIZE,
1607 _4K - VBOX_DMI_TABLE_SIZE, pThis->cCpus);
1608 LogRel(("PcBios: MPS table at %08x\n", VBOX_DMI_TABLE_BASE + VBOX_DMI_TABLE_SIZE));
1609 }
1610
1611 rc = PDMDevHlpROMRegister(pDevIns, VBOX_DMI_TABLE_BASE, _4K, pThis->au8DMIPage, _4K,
1612 PGMPHYS_ROM_FLAGS_PERMANENT_BINARY, "DMI tables");
1613 if (RT_FAILURE(rc))
1614 return rc;
1615
1616 /*
1617 * Map the BIOS into memory.
1618 * There are two mappings:
1619 * 1. 0x000e0000 to 0x000fffff contains the last 128 kb of the bios.
1620 * The bios code might be 64 kb in size, and will then start at 0xf0000.
1621 * 2. 0xfffxxxxx to 0xffffffff contains the entire bios.
1622 */
1623 AssertReleaseMsg(cbPcBiosBinary >= _64K, ("cbPcBiosBinary=%#x\n", cbPcBiosBinary));
1624 AssertReleaseMsg(RT_ALIGN_Z(cbPcBiosBinary, _64K) == cbPcBiosBinary,
1625 ("cbPcBiosBinary=%#x\n", cbPcBiosBinary));
1626 cb = RT_MIN(cbPcBiosBinary, 128 * _1K); /* Effectively either 64 or 128K. */
1627 rc = PDMDevHlpROMRegister(pDevIns, 0x00100000 - cb, cb, &pu8PcBiosBinary[cbPcBiosBinary - cb], cb,
1628 PGMPHYS_ROM_FLAGS_PERMANENT_BINARY, "PC BIOS - 0xfffff");
1629 if (RT_FAILURE(rc))
1630 return rc;
1631 rc = PDMDevHlpROMRegister(pDevIns, (uint32_t)-(int32_t)cbPcBiosBinary, cbPcBiosBinary, pu8PcBiosBinary, cbPcBiosBinary,
1632 PGMPHYS_ROM_FLAGS_PERMANENT_BINARY, "PC BIOS - 0xffffffff");
1633 if (RT_FAILURE(rc))
1634 return rc;
1635
1636 /*
1637 * Get the LAN boot ROM file name.
1638 */
1639 rc = CFGMR3QueryStringAlloc(pCfg, "LanBootRom", &pThis->pszLanBootFile);
1640 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1641 {
1642 pThis->pszLanBootFile = NULL;
1643 rc = VINF_SUCCESS;
1644 }
1645 else if (RT_FAILURE(rc))
1646 return PDMDEV_SET_ERROR(pDevIns, rc,
1647 N_("Configuration error: Querying \"LanBootRom\" as a string failed"));
1648 else if (!*pThis->pszLanBootFile)
1649 {
1650 MMR3HeapFree(pThis->pszLanBootFile);
1651 pThis->pszLanBootFile = NULL;
1652 }
1653
1654 /*
1655 * Not loading LAN ROM for old CPUs.
1656 */
1657 if ( enmMicroarch != kCpumMicroarch_Intel_8086
1658 && enmMicroarch != kCpumMicroarch_Intel_80186
1659 && enmMicroarch != kCpumMicroarch_NEC_V20
1660 && enmMicroarch != kCpumMicroarch_NEC_V30
1661 && enmMicroarch != kCpumMicroarch_Intel_80286)
1662 {
1663 const uint8_t *pu8LanBootBinary = NULL;
1664 uint64_t cbLanBootBinary;
1665 uint64_t cbFileLanBoot = 0;
1666
1667 /*
1668 * Open the LAN boot ROM and figure it size.
1669 * Determine the LAN boot ROM size, open specified ROM file in the process.
1670 */
1671 if (pThis->pszLanBootFile)
1672 {
1673 RTFILE hFileLanBoot = NIL_RTFILE;
1674 rc = RTFileOpen(&hFileLanBoot, pThis->pszLanBootFile,
1675 RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
1676 if (RT_SUCCESS(rc))
1677 {
1678 rc = RTFileGetSize(hFileLanBoot, &cbFileLanBoot);
1679 if (RT_SUCCESS(rc))
1680 {
1681 if (cbFileLanBoot <= _64K - (VBOX_LANBOOT_SEG << 4 & 0xffff))
1682 {
1683 LogRel(("PcBios: Using LAN ROM '%s' with a size of %#x bytes\n", pThis->pszLanBootFile, cbFileLanBoot));
1684
1685 /*
1686 * Allocate buffer for the LAN boot ROM data and load it.
1687 */
1688 pThis->pu8LanBoot = (uint8_t *)PDMDevHlpMMHeapAllocZ(pDevIns, cbFileLanBoot);
1689 if (pThis->pu8LanBoot)
1690 {
1691 rc = RTFileRead(hFileLanBoot, pThis->pu8LanBoot, cbFileLanBoot, NULL);
1692 AssertLogRelRCReturnStmt(rc, RTFileClose(hFileLanBoot), rc);
1693 }
1694 else
1695 rc = VERR_NO_MEMORY;
1696 }
1697 else
1698 rc = VERR_TOO_MUCH_DATA;
1699 }
1700 RTFileClose(hFileLanBoot);
1701 }
1702 if (RT_FAILURE(rc))
1703 {
1704 /*
1705 * Play stupid and ignore failures, falling back to the built-in LAN boot ROM.
1706 */
1707 /** @todo r=bird: This should have some kind of rational. We don't usually
1708 * ignore the VM configuration. */
1709 LogRel(("PcBios: Failed to open LAN boot ROM file '%s', rc=%Rrc!\n", pThis->pszLanBootFile, rc));
1710 MMR3HeapFree(pThis->pszLanBootFile);
1711 pThis->pszLanBootFile = NULL;
1712 }
1713 }
1714
1715 /* If we were unable to get the data from file for whatever reason, fall
1716 * back to the built-in LAN boot ROM image.
1717 */
1718 if (pThis->pu8LanBoot == NULL)
1719 {
1720#ifdef VBOX_WITH_PXE_ROM
1721 pu8LanBootBinary = g_abNetBiosBinary;
1722 cbLanBootBinary = g_cbNetBiosBinary;
1723#endif
1724 }
1725 else
1726 {
1727 pu8LanBootBinary = pThis->pu8LanBoot;
1728 cbLanBootBinary = cbFileLanBoot;
1729 }
1730
1731 /*
1732 * Map the Network Boot ROM into memory.
1733 *
1734 * Currently there is a fixed mapping: 0x000e2000 to 0x000effff contains
1735 * the (up to) 56 kb ROM image. The mapping size is fixed to trouble with
1736 * the saved state (in PGM).
1737 */
1738 if (pu8LanBootBinary)
1739 {
1740 pThis->cbLanBoot = cbLanBootBinary;
1741
1742 rc = PDMDevHlpROMRegister(pDevIns, VBOX_LANBOOT_SEG << 4,
1743 RT_MAX(cbLanBootBinary, _64K - (VBOX_LANBOOT_SEG << 4 & 0xffff)),
1744 pu8LanBootBinary, cbLanBootBinary,
1745 PGMPHYS_ROM_FLAGS_SHADOWED, "Net Boot ROM");
1746 AssertRCReturn(rc, rc);
1747 }
1748 }
1749 else if (pThis->pszLanBootFile)
1750 LogRel(("PcBios: Skipping LAN ROM '%s' due to ancient target CPU.\n", pThis->pszLanBootFile));
1751#ifdef VBOX_WITH_PXE_ROM
1752 else
1753 LogRel(("PcBios: Skipping built in ROM due to ancient target CPU.\n"));
1754#endif
1755
1756 /*
1757 * Configure Boot delay.
1758 */
1759 rc = CFGMR3QueryU8Def(pCfg, "DelayBoot", &pThis->uBootDelay, 0);
1760 if (RT_FAILURE(rc))
1761 return PDMDEV_SET_ERROR(pDevIns, rc,
1762 N_("Configuration error: Querying \"DelayBoot\" as integer failed"));
1763 if (pThis->uBootDelay > 15)
1764 pThis->uBootDelay = 15;
1765
1766
1767 /*
1768 * Read shutdown status code config and register ourselves as the firmware device.
1769 */
1770
1771 /** @cfgm{CheckShutdownStatusForSoftReset, boolean, true}
1772 * Whether to consult the shutdown status code (CMOS register 0Fh) to
1773 * determine whether the guest intended a soft or hard reset. Currently only
1774 * shutdown status codes 05h, 09h and 0Ah are considered soft reset. */
1775 rc = CFGMR3QueryBoolDef(pCfg, "CheckShutdownStatusForSoftReset", &pThis->fCheckShutdownStatusForSoftReset, true);
1776 AssertLogRelRCReturn(rc, rc);
1777
1778 /** @cfgm{ClearShutdownStatusOnHardReset, boolean, true}
1779 * Whether to clear the shutdown status code (CMOS register 0Fh) on hard reset. */
1780 rc = CFGMR3QueryBoolDef(pCfg, "ClearShutdownStatusOnHardReset", &pThis->fClearShutdownStatusOnHardReset, true);
1781 AssertLogRelRCReturn(rc, rc);
1782
1783 LogRel(("PcBios: fCheckShutdownStatusForSoftReset=%RTbool fClearShutdownStatusOnHardReset=%RTbool\n",
1784 pThis->fCheckShutdownStatusForSoftReset, pThis->fClearShutdownStatusOnHardReset));
1785
1786 static PDMFWREG const s_FwReg = { PDM_FWREG_VERSION, pcbiosFw_IsHardReset, PDM_FWREG_VERSION };
1787 rc = PDMDevHlpFirmwareRegister(pDevIns, &s_FwReg, &pThis->pFwHlpR3);
1788 AssertLogRelRCReturn(rc, rc);
1789
1790 return VINF_SUCCESS;
1791}
1792
1793
1794/**
1795 * The device registration structure.
1796 */
1797const PDMDEVREG g_DevicePcBios =
1798{
1799 /* u32Version */
1800 PDM_DEVREG_VERSION,
1801 /* szName */
1802 "pcbios",
1803 /* szRCMod */
1804 "",
1805 /* szR0Mod */
1806 "",
1807 /* pszDescription */
1808 "PC BIOS Device",
1809 /* fFlags */
1810 PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64,
1811 /* fClass */
1812 PDM_DEVREG_CLASS_ARCH_BIOS,
1813 /* cMaxInstances */
1814 1,
1815 /* cbInstance */
1816 sizeof(DEVPCBIOS),
1817 /* pfnConstruct */
1818 pcbiosConstruct,
1819 /* pfnDestruct */
1820 pcbiosDestruct,
1821 /* pfnRelocate */
1822 NULL,
1823 /* pfnMemSetup */
1824 pcbiosMemSetup,
1825 /* pfnPowerOn */
1826 NULL,
1827 /* pfnReset */
1828 pcbiosReset,
1829 /* pfnSuspend */
1830 NULL,
1831 /* pfnResume */
1832 NULL,
1833 /* pfnAttach */
1834 NULL,
1835 /* pfnDetach */
1836 NULL,
1837 /* pfnQueryInterface. */
1838 NULL,
1839 /* pfnInitComplete. */
1840 pcbiosInitComplete,
1841 /* pfnPowerOff */
1842 NULL,
1843 /* pfnSoftReset */
1844 NULL,
1845 /* u32VersionEnd */
1846 PDM_DEVREG_VERSION
1847};
1848
Note: See TracBrowser for help on using the repository browser.

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