summaryrefslogtreecommitdiffstats
path: root/src/VBox/Devices/PC/DevPcBios.cpp
blob: a84f436470f160fc0601d6075b84c18192ac1390 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
/* $Id: DevPcBios.cpp $ */
/** @file
 * DevPcBios - PC BIOS Device.
 */

/*
 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_PC_BIOS
#include <VBox/vmm/pdmdev.h>
#include <VBox/vmm/pdmstorageifs.h>
#include <VBox/vmm/mm.h>
#include <VBox/vmm/pgm.h>
#include <VBox/vmm/cpum.h>

#include <VBox/log.h>
#include <iprt/asm.h>
#include <iprt/assert.h>
#include <iprt/buildconfig.h>
#include <iprt/file.h>
#include <iprt/mem.h>
#include <iprt/string.h>
#include <iprt/uuid.h>
#include <iprt/cdefs.h>
#include <VBox/bios.h>
#include <VBox/err.h>
#include <VBox/param.h>

#include "VBoxDD.h"
#include "VBoxDD2.h"
#include "DevPcBios.h"
#include "DevFwCommon.h"

#define NET_BOOT_DEVS   4


/** @page pg_devbios_cmos_assign    CMOS Assignments (BIOS)
 *
 * The BIOS uses a CMOS to store configuration data.
 * It is currently used as follows:
 *
 * @verbatim
  First CMOS bank (offsets 0x00 to 0x7f):
    Floppy drive type:
         0x10
    Hard disk type (old):
         0x12
    Equipment byte:
         0x14
    Base memory:
         0x15
         0x16
    Extended memory:
         0x17
         0x18
         0x30
         0x31
    First IDE HDD:
         0x19
         0x1e - 0x25
    Second IDE HDD:
         0x1a
         0x26 - 0x2d
    Checksum of 0x10-0x2d:
         0x2e
         0x2f
    Amount of memory above 16M and below 4GB in 64KB units:
         0x34
         0x35
    Boot device (BOCHS BIOS specific):
         0x38
         0x3c
         0x3d
    PXE debug:
         0x3f
    First SATA HDD:
         0x40 - 0x47
    Second SATA HDD:
         0x48 - 0x4f
    Third SATA HDD:
         0x50 - 0x57
    Fourth SATA HDD:
         0x58 - 0x5f
    Number of CPUs:
         0x60
    RAM above 4G in 64KB units:
         0x61 - 0x65
    Third IDE HDD:
         0x67 - 0x6e
    Fourth IDE HDD:
         0x70 - 0x77
    APIC/x2APIC settings:
         0x78

  Second CMOS bank (offsets 0x80 to 0xff):
    Reserved for internal use by PXE ROM:
         0x80 - 0x81
    First net boot device PCI bus/dev/fn:
         0x82 - 0x83
    Second to third net boot devices:
         0x84 - 0x89
    First SCSI HDD:
         0x90 - 0x97
    Second SCSI HDD:
         0x98 - 0x9f
    Third SCSI HDD:
         0xa0 - 0xa7
    Fourth SCSI HDD:
         0xa8 - 0xaf

@endverbatim
 *
 * @todo Mark which bits are compatible with which BIOSes and
 *       which are our own definitions.
 */


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/

/**
 * The boot device.
 */
typedef enum DEVPCBIOSBOOT
{
    DEVPCBIOSBOOT_NONE,
    DEVPCBIOSBOOT_FLOPPY,
    DEVPCBIOSBOOT_HD,
    DEVPCBIOSBOOT_DVD,
    DEVPCBIOSBOOT_LAN
} DEVPCBIOSBOOT;

/**
 * PC Bios instance data structure.
 */
typedef struct DEVPCBIOS
{
    /** Pointer back to the device instance. */
    PPDMDEVINS      pDevIns;

    /** Boot devices (ordered). */
    DEVPCBIOSBOOT   aenmBootDevice[4];
    /** Bochs control string index. */
    uint32_t        iControl;
    /** Floppy device. */
    char           *pszFDDevice;
    /** Harddisk device. */
    char           *pszHDDevice;
    /** Sata harddisk device. */
    char           *pszSataDevice;
    /** LUNs of the four BIOS-accessible SATA disks. */
    uint32_t        iSataHDLUN[4];
    /** SCSI harddisk device. */
    char           *pszScsiDevice;
    /** LUNs of the four BIOS-accessible SCSI disks. */
    uint32_t        iScsiHDLUN[4];
    /** Bios message buffer. */
    char            szMsg[256];
    /** Bios message buffer index. */
    uint32_t        iMsg;
    /** The system BIOS ROM data. */
    uint8_t        *pu8PcBios;
    /** The size of the system BIOS ROM. */
    uint32_t        cbPcBios;
    /** The name of the BIOS ROM file. */
    char           *pszPcBiosFile;
    /** The LAN boot ROM data. */
    uint8_t        *pu8LanBoot;
    /** The name of the LAN boot ROM file. */
    char           *pszLanBootFile;
    /** The size of the LAN boot ROM. */
    uint64_t        cbLanBoot;
    /** The DMI tables. */
    uint8_t         au8DMIPage[0x1000];
    /** The boot countdown (in seconds). */
    uint8_t         uBootDelay;
    /** I/O-APIC enabled? */
    uint8_t         u8IOAPIC;
    /** APIC mode to be set up by BIOS */
    uint8_t         u8APICMode;
    /** PXE debug logging enabled? */
    uint8_t         u8PXEDebug;
    /** Physical address of the MP table. */
    uint32_t        u32MPTableAddr;
    /** PXE boot PCI bus/dev/fn list. */
    uint16_t        au16NetBootDev[NET_BOOT_DEVS];
    /** Number of logical CPUs in guest */
    uint16_t        cCpus;
    /* Physical address of PCI config space MMIO region. Currently unused. */
    uint64_t        u64McfgBase;
    /* Length of PCI config space MMIO region. Currently unused. */
    uint64_t        cbMcfgLength;

    /** Firmware registration structure.   */
    PDMFWREG        FwReg;
    /** Dummy. */
    PCPDMFWHLPR3    pFwHlpR3;
    /** Number of soft resets we've logged. */
    uint32_t        cLoggedSoftResets;
    /** Whether to consult the shutdown status (CMOS[0xf]) for deciding upon soft
     * or hard reset. */
    bool            fCheckShutdownStatusForSoftReset;
    /** Whether to clear the shutdown status on hard reset. */
    bool            fClearShutdownStatusOnHardReset;
    /** Current port number for Bochs shutdown (used by APM). */
    RTIOPORT        ShutdownPort;
    /** True=use new port number for Bochs shutdown (used by APM). */
    bool            fNewShutdownPort;
    bool            afPadding[3+4];
    /** The shudown I/O port, either at 0x040f or 0x8900 (old saved state). */
    IOMMMIOHANDLE   hIoPortShutdown;
} DEVPCBIOS;
/** Pointer to the BIOS device state. */
typedef DEVPCBIOS *PDEVPCBIOS;


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/
/** The saved state version. */
#define PCBIOS_SSM_VERSION 0


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
/** Saved state DEVPCBIOS field descriptors. */
static SSMFIELD const g_aPcBiosFields[] =
{
    SSMFIELD_ENTRY(         DEVPCBIOS, fNewShutdownPort),
    SSMFIELD_ENTRY_TERM()
};


/**
 * @callback_method_impl{FNIOMIOPORTNEWIN, Bochs Debug.}
 */
static DECLCALLBACK(VBOXSTRICTRC)
pcbiosIOPortDebugRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t *pu32, unsigned cb)
{
    RT_NOREF5(pDevIns, pvUser, offPort, pu32, cb);
    return VERR_IOM_IOPORT_UNUSED;
}


/**
 * @callback_method_impl{FNIOMIOPORTNEWOUT, Bochs Debug.}
 */
static DECLCALLBACK(VBOXSTRICTRC)
pcbiosIOPortDebugWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t u32, unsigned cb)
{
    PDEVPCBIOS pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPCBIOS);
    RT_NOREF(pvUser);
    Assert(offPort < 4);

    /*
     * Bochs BIOS char printing.
     */
    if (    cb == 1
        &&  (   offPort == 2
             || offPort == 3))
    {
        /* The raw version. */
        switch (u32)
        {
            case '\r': Log2(("pcbios: <return>\n")); break;
            case '\n': Log2(("pcbios: <newline>\n")); break;
            case '\t': Log2(("pcbios: <tab>\n")); break;
            default:   Log2(("pcbios: %c (%02x)\n", u32, u32)); break;
        }

        /* The readable, buffered version. */
        uint32_t iMsg = pThis->iMsg;
        if (u32 == '\n' || u32 == '\r')
        {
            AssertStmt(iMsg < sizeof(pThis->szMsg), iMsg = sizeof(pThis->szMsg) - 1);
            pThis->szMsg[iMsg] = '\0';
            if (iMsg)
                Log(("pcbios: %s\n", pThis->szMsg));
            iMsg = 0;
        }
        else
        {
            if (iMsg >= sizeof(pThis->szMsg) - 1)
            {
                pThis->szMsg[iMsg] = '\0';
                Log(("pcbios: %s\n", pThis->szMsg));
                iMsg = 0;
            }
            pThis->szMsg[iMsg] = (char)u32;
            pThis->szMsg[++iMsg] = '\0';
        }
        pThis->iMsg = iMsg;
        return VINF_SUCCESS;
    }

    /* not in use. */
    return VINF_SUCCESS;
}


/**
 * @callback_method_impl{FNIOMIOPORTNEWIN, Bochs Shutdown port.}
 */
static DECLCALLBACK(VBOXSTRICTRC)
pcbiosIOPortShutdownRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t *pu32, unsigned cb)
{
    RT_NOREF5(pDevIns, pvUser, offPort, pu32, cb);
    return VERR_IOM_IOPORT_UNUSED;
}


/**
 * @callback_method_impl{FNIOMIOPORTNEWOUT, Bochs Shutdown port.}
 */
static DECLCALLBACK(VBOXSTRICTRC)
pcbiosIOPortShutdownWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t u32, unsigned cb)
{
    PDEVPCBIOS pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPCBIOS);
    RT_NOREF(pvUser, offPort);
    Assert(offPort == 0);

    if (cb == 1)
    {
        static const unsigned char s_szShutdown[] = "Shutdown";
        static const unsigned char s_szBootfail[] = "Bootfail";
        AssertCompile(sizeof(s_szShutdown) == sizeof(s_szBootfail));

        if (pThis->iControl < sizeof(s_szShutdown)) /* paranoia */
        {
            if (u32 == s_szShutdown[pThis->iControl])
            {

                pThis->iControl++;
                if (pThis->iControl >= 8)
                {
                    pThis->iControl = 0;
                    LogRel(("PcBios: APM shutdown request\n"));
                    return PDMDevHlpVMPowerOff(pDevIns);
                }
            }
            else if (u32 == s_szBootfail[pThis->iControl])
            {
                pThis->iControl++;
                if (pThis->iControl >= 8)
                {
                    pThis->iControl = 0;
                    LogRel(("PcBios: Boot failure\n"));
                    int rc = PDMDevHlpVMSetRuntimeError(pDevIns, 0 /*fFlags*/, "VMBootFail",
                                                        N_("The VM failed to boot. This is possibly caused by not having an operating system installed or a misconfigured boot order. Maybe picking a guest OS install DVD will resolve the situation"));
                    AssertRC(rc);
                }
            }
            else
                pThis->iControl = 0;
        }
        else
            pThis->iControl = 0;
    }
    /* else: not in use. */

    return VINF_SUCCESS;
}


/**
 * Register the Bochs shutdown port.
 * This is used by pcbiosConstruct, pcbiosReset and pcbiosLoadExec.
 */
static int pcbiosRegisterShutdown(PPDMDEVINS pDevIns, PDEVPCBIOS pThis, bool fNewShutdownPort)
{
    if (pThis->ShutdownPort != 0)
    {
        int rc = PDMDevHlpIoPortUnmap(pDevIns, pThis->hIoPortShutdown);
        AssertRC(rc);
    }

    pThis->fNewShutdownPort = fNewShutdownPort;
    if (fNewShutdownPort)
        pThis->ShutdownPort = VBOX_BIOS_SHUTDOWN_PORT;
    else
        pThis->ShutdownPort = VBOX_BIOS_OLD_SHUTDOWN_PORT;
    return PDMDevHlpIoPortMap(pDevIns, pThis->hIoPortShutdown, pThis->ShutdownPort);
}


/**
 * @callback_method_impl{FNSSMDEVSAVEEXEC}
 */
static DECLCALLBACK(int) pcbiosSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
{
    PDEVPCBIOS pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPCBIOS);
    return pDevIns->pHlpR3->pfnSSMPutStruct(pSSM, pThis, g_aPcBiosFields);
}


/**
 * @callback_method_impl{FNSSMDEVLOADPREP,
 *      Clears the fNewShutdownPort flag prior to loading the state so that old
 *      saved VM states keeps using the old port address (no pcbios state)}
 */
static DECLCALLBACK(int) pcbiosLoadPrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
{
    RT_NOREF(pSSM);
    PDEVPCBIOS pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPCBIOS);

    /* Since there are legacy saved state files without any SSM data for PCBIOS
     * this is the only way to handle them correctly. */
    pThis->fNewShutdownPort = false;

    return VINF_SUCCESS;
}


/**
 * @callback_method_impl{FNSSMDEVLOADEXEC}
 */
static DECLCALLBACK(int) pcbiosLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
{
    PDEVPCBIOS pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPCBIOS);

    if (uVersion > PCBIOS_SSM_VERSION)
        return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
    Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);

    return pDevIns->pHlpR3->pfnSSMGetStruct(pSSM, pThis, g_aPcBiosFields);
}


/**
 * @callback_method_impl{FNSSMDEVLOADDONE,
 *      Updates the shutdown port registration to match the flag loaded (or not).}
 */
static DECLCALLBACK(int) pcbiosLoadDone(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
{
    RT_NOREF(pSSM);
    PDEVPCBIOS pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPCBIOS);
    return pcbiosRegisterShutdown(pDevIns, pThis, pThis->fNewShutdownPort);
}


/**
 * Write to CMOS memory.
 * This is used by the init complete code.
 */
static void pcbiosCmosWrite(PPDMDEVINS pDevIns, int off, uint32_t u32Val)
{
    Assert(off < 256);
    Assert(u32Val < 256);

    int rc = PDMDevHlpCMOSWrite(pDevIns, off, u32Val);
    AssertRC(rc);
}


/**
 * Read from CMOS memory.
 * This is used by the init complete code.
 */
static uint8_t pcbiosCmosRead(PPDMDEVINS pDevIns, unsigned off)
{
    Assert(off < 256);

    uint8_t u8val;
    int rc = PDMDevHlpCMOSRead(pDevIns, off, &u8val);
    AssertRC(rc);

    return u8val;
}


/**
 * @interface_method_impl{PDMFWREG,pfnIsHardReset}
 */
static DECLCALLBACK(bool) pcbiosFw_IsHardReset(PPDMDEVINS pDevIns, uint32_t fFlags)
{
    RT_NOREF1(fFlags);
    PDEVPCBIOS pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPCBIOS);
    if (pThis->fCheckShutdownStatusForSoftReset)
    {
        uint8_t bShutdownStatus = pcbiosCmosRead(pDevIns, 0xf);
        if (   bShutdownStatus == 0x5
            || bShutdownStatus == 0x9
            || bShutdownStatus == 0xa)
        {
            const uint32_t cMaxLogged = 10;
            if (pThis->cLoggedSoftResets < cMaxLogged)
            {
                RTFAR16 Far16 = { 0xfeed, 0xface };
                PDMDevHlpPhysRead(pDevIns, 0x467, &Far16, sizeof(Far16));
                pThis->cLoggedSoftResets++;
                LogRel(("PcBios: Soft reset #%u - shutdown status %#x, warm reset vector (0040:0067) is %04x:%04x%s\n",
                        pThis->cLoggedSoftResets, bShutdownStatus, Far16.sel, Far16.off,
                        pThis->cLoggedSoftResets < cMaxLogged ? "." : " - won't log any more!"));
            }
            return false;
        }
    }
    return true;
}


/**
 * @interface_method_impl{PDMDEVREG,pfnReset}
 */
static DECLCALLBACK(void) pcbiosReset(PPDMDEVINS pDevIns)
{
    PDEVPCBIOS pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPCBIOS);

    if (pThis->fClearShutdownStatusOnHardReset)
    {
        uint8_t bShutdownStatus = pcbiosCmosRead(pDevIns, 0xf);
        if (bShutdownStatus != 0)
        {
            LogRel(("PcBios: Clearing shutdown status code %02x.\n", bShutdownStatus));
            pcbiosCmosWrite(pDevIns, 0xf, 0);
        }
    }

    /* After reset the new BIOS code is active, use the new shutdown port. */
    pcbiosRegisterShutdown(pDevIns, pThis, true /* fNewShutdownPort */);
}


/**
 * Attempt to guess the LCHS disk geometry from the MS-DOS master boot record
 * (partition table).
 *
 * @returns VBox status code.
 * @param   pMedia          The media device interface of the disk.
 * @param   pLCHSGeometry   Where to return the disk geometry on success
 */
static int biosGuessDiskLCHS(PPDMIMEDIA pMedia, PPDMMEDIAGEOMETRY pLCHSGeometry)
{
    uint8_t aMBR[512], *p;
    int rc;
    uint32_t iEndHead, iEndSector, cLCHSCylinders, cLCHSHeads, cLCHSSectors;

    if (!pMedia)
        return VERR_INVALID_PARAMETER;
    rc = pMedia->pfnReadPcBios(pMedia, 0, aMBR, sizeof(aMBR));
    if (RT_FAILURE(rc))
        return rc;
    /* Test MBR magic number. */
    if (aMBR[510] != 0x55 || aMBR[511] != 0xaa)
        return VERR_INVALID_PARAMETER;
    for (uint32_t i = 0; i < 4; i++)
    {
        /* Figure out the start of a partition table entry. */
        p = &aMBR[0x1be + i * 16];
        iEndHead = p[5];
        iEndSector = p[6] & 63;
        if ((p[12] | p[13] | p[14] | p[15]) && iEndSector && iEndHead)
        {
            /* Assumption: partition terminates on a cylinder boundary. */
            cLCHSHeads = iEndHead + 1;
            cLCHSSectors = iEndSector;
            cLCHSCylinders = RT_MIN(1024, pMedia->pfnGetSize(pMedia) / (512 * cLCHSHeads * cLCHSSectors));
            if (cLCHSCylinders >= 1)
            {
                pLCHSGeometry->cCylinders = cLCHSCylinders;
                pLCHSGeometry->cHeads = cLCHSHeads;
                pLCHSGeometry->cSectors = cLCHSSectors;
                Log(("%s: LCHS=%d %d %d\n", __FUNCTION__, cLCHSCylinders, cLCHSHeads, cLCHSSectors));
                return VINF_SUCCESS;
            }
        }
    }
    return VERR_INVALID_PARAMETER;
}


/**
 * Initializes the CMOS data for one harddisk.
 */
static void pcbiosCmosInitHardDisk(PPDMDEVINS pDevIns, int offType, int offInfo, PCPDMMEDIAGEOMETRY pLCHSGeometry)
{
    Log2(("%s: offInfo=%#x: LCHS=%d/%d/%d\n", __FUNCTION__, offInfo, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
    if (offType)
        pcbiosCmosWrite(pDevIns, offType, 47);
    /* Cylinders low */
    pcbiosCmosWrite(pDevIns, offInfo + 0, RT_MIN(pLCHSGeometry->cCylinders, 1024) & 0xff);
    /* Cylinders high */
    pcbiosCmosWrite(pDevIns, offInfo + 1, RT_MIN(pLCHSGeometry->cCylinders, 1024) >> 8);
    /* Heads */
    pcbiosCmosWrite(pDevIns, offInfo + 2, pLCHSGeometry->cHeads);
    /* Landing zone low */
    pcbiosCmosWrite(pDevIns, offInfo + 3, 0xff);
    /* Landing zone high */
    pcbiosCmosWrite(pDevIns, offInfo + 4, 0xff);
    /* Write precomp low */
    pcbiosCmosWrite(pDevIns, offInfo + 5, 0xff);
    /* Write precomp high */
    pcbiosCmosWrite(pDevIns, offInfo + 6, 0xff);
    /* Sectors */
    pcbiosCmosWrite(pDevIns, offInfo + 7, pLCHSGeometry->cSectors);
}


/**
 * Set logical CHS geometry for a hard disk
 *
 * @returns VBox status code.
 * @param   pBase         Base interface for the device.
 * @param   pHardDisk     The hard disk.
 * @param   pLCHSGeometry Where to store the geometry settings.
 */
static int setLogicalDiskGeometry(PPDMIBASE pBase, PPDMIMEDIA pHardDisk, PPDMMEDIAGEOMETRY pLCHSGeometry)
{
    RT_NOREF1(pBase);

    PDMMEDIAGEOMETRY LCHSGeometry;
    int rc = pHardDisk->pfnBiosGetLCHSGeometry(pHardDisk, &LCHSGeometry);
    if (   rc == VERR_PDM_GEOMETRY_NOT_SET
        || LCHSGeometry.cCylinders == 0
        || LCHSGeometry.cHeads == 0
        || LCHSGeometry.cHeads > 255
        || LCHSGeometry.cSectors == 0
        || LCHSGeometry.cSectors > 63)
    {
        /* No LCHS geometry, autodetect and set. */
        rc = biosGuessDiskLCHS(pHardDisk, &LCHSGeometry);
        if (RT_FAILURE(rc))
        {
            /* Try if PCHS geometry works, otherwise fall back. */
            rc = pHardDisk->pfnBiosGetPCHSGeometry(pHardDisk, &LCHSGeometry);
        }
        if (   RT_FAILURE(rc)
            || LCHSGeometry.cCylinders == 0
            || LCHSGeometry.cCylinders > 1024
            || LCHSGeometry.cHeads == 0
            || LCHSGeometry.cHeads > 255
            || LCHSGeometry.cSectors == 0
            || LCHSGeometry.cSectors > 63)
        {
            uint64_t cSectors = pHardDisk->pfnGetSize(pHardDisk) / 512;
            if (cSectors / 16 / 63 <= 1024)
            {
                LCHSGeometry.cCylinders = RT_MAX(cSectors / 16 / 63, 1);
                LCHSGeometry.cHeads = 16;
            }
            else if (cSectors / 32 / 63 <= 1024)
            {
                LCHSGeometry.cCylinders = RT_MAX(cSectors / 32 / 63, 1);
                LCHSGeometry.cHeads = 32;
            }
            else if (cSectors / 64 / 63 <= 1024)
            {
                LCHSGeometry.cCylinders = cSectors / 64 / 63;
                LCHSGeometry.cHeads = 64;
            }
            else if (cSectors / 128 / 63 <= 1024)
            {
                LCHSGeometry.cCylinders = cSectors / 128 / 63;
                LCHSGeometry.cHeads = 128;
            }
            else
            {
                LCHSGeometry.cCylinders = RT_MIN(cSectors / 255 / 63, 1024);
                LCHSGeometry.cHeads = 255;
            }
            LCHSGeometry.cSectors = 63;

        }
        rc = pHardDisk->pfnBiosSetLCHSGeometry(pHardDisk, &LCHSGeometry);
        if (rc == VERR_VD_IMAGE_READ_ONLY)
        {
            LogRel(("PcBios: ATA failed to update LCHS geometry, read only\n"));
            rc = VINF_SUCCESS;
        }
        else if (rc == VERR_PDM_GEOMETRY_NOT_SET)
        {
            LogRel(("PcBios: ATA failed to update LCHS geometry, backend refused\n"));
            rc = VINF_SUCCESS;
        }
    }

    *pLCHSGeometry = LCHSGeometry;

    return rc;
}


/**
 * Get logical CHS geometry for a hard disk, intended for SCSI/SAS drives
 * with no physical geometry.
 *
 * @returns VBox status code.
 * @param   pHardDisk     The hard disk.
 * @param   pLCHSGeometry Where to store the geometry settings.
 */
static int getLogicalDiskGeometry(PPDMIMEDIA pHardDisk, PPDMMEDIAGEOMETRY pLCHSGeometry)
{
    PDMMEDIAGEOMETRY LCHSGeometry;
    int rc = VINF_SUCCESS;

    rc = pHardDisk->pfnBiosGetLCHSGeometry(pHardDisk, &LCHSGeometry);
    if (   rc == VERR_PDM_GEOMETRY_NOT_SET
        || LCHSGeometry.cCylinders == 0
        || LCHSGeometry.cHeads == 0
        || LCHSGeometry.cHeads > 255
        || LCHSGeometry.cSectors == 0
        || LCHSGeometry.cSectors > 63)
    {
        /* Unlike the ATA case, if the image does not provide valid logical
         * geometry, we leave things alone and let the BIOS decide what the
         * logical geometry should be.
         */
        rc = VERR_PDM_GEOMETRY_NOT_SET;
    }
    else
        *pLCHSGeometry = LCHSGeometry;

    return rc;
}


/**
 * Get BIOS boot code from enmBootDevice in order
 *
 * @todo r=bird: This is a rather silly function since the conversion is 1:1.
 */
static uint8_t getBiosBootCode(PDEVPCBIOS pThis, unsigned iOrder)
{
    switch (pThis->aenmBootDevice[iOrder])
    {
        case DEVPCBIOSBOOT_NONE:
            return 0;
        case DEVPCBIOSBOOT_FLOPPY:
            return 1;
        case DEVPCBIOSBOOT_HD:
            return 2;
        case DEVPCBIOSBOOT_DVD:
            return 3;
        case DEVPCBIOSBOOT_LAN:
            return 4;
        default:
            AssertMsgFailed(("aenmBootDevice[%d]=%d\n", iOrder, pThis->aenmBootDevice[iOrder]));
            return 0;
    }
}


/**
 * @interface_method_impl{PDMDEVREG,pfnInitComplete}
 *
 * This routine will write information needed by the bios to the CMOS.
 *
 * @see     http://www.brl.ntt.co.jp/people/takehiko/interrupt/CMOS.LST.txt for
 *          a description of standard and non-standard CMOS registers.
 */
static DECLCALLBACK(int) pcbiosInitComplete(PPDMDEVINS pDevIns)
{
    PDEVPCBIOS      pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPCBIOS);
    uint32_t        u32;
    unsigned        i;
    PPDMIMEDIA      apHDs[4] = {0};
    LogFlow(("pcbiosInitComplete:\n"));

    uint64_t const  cbRamSize  = PDMDevHlpMMPhysGetRamSize(pDevIns);
    uint32_t const  cbBelow4GB = PDMDevHlpMMPhysGetRamSizeBelow4GB(pDevIns);
    uint64_t const  cbAbove4GB = PDMDevHlpMMPhysGetRamSizeAbove4GB(pDevIns);

    /*
     * Memory sizes.
     */
    /* base memory. */
    u32 = cbRamSize > 640 ? 640 : (uint32_t)cbRamSize / _1K; /* <-- this test is wrong, but it doesn't matter since we never assign less than 1MB */
    pcbiosCmosWrite(pDevIns, 0x15, RT_BYTE1(u32));  /* 15h - Base Memory in K, Low Byte */
    pcbiosCmosWrite(pDevIns, 0x16, RT_BYTE2(u32));  /* 16h - Base Memory in K, High Byte */

    /* Extended memory, up to 65MB */
    u32 = cbRamSize >= 65 * _1M ? 0xffff : ((uint32_t)cbRamSize - _1M) / _1K;
    pcbiosCmosWrite(pDevIns, 0x17, RT_BYTE1(u32));  /* 17h - Extended Memory in K, Low Byte */
    pcbiosCmosWrite(pDevIns, 0x18, RT_BYTE2(u32));  /* 18h - Extended Memory in K, High Byte */
    pcbiosCmosWrite(pDevIns, 0x30, RT_BYTE1(u32));  /* 30h - Extended Memory in K, Low Byte */
    pcbiosCmosWrite(pDevIns, 0x31, RT_BYTE2(u32));  /* 31h - Extended Memory in K, High Byte */

    /* Bochs BIOS specific? Anyway, it's the amount of memory above 16MB
       and below 4GB (as it can only hold 4GB-16M). We have to chop off the
       top 32MB or it conflict with what the ACPI tables return. (Should these
       be adjusted, we still have to chop it at 0xfffc0000 or it'll conflict
       with the high BIOS mapping.) */
    if (cbRamSize > 16 * _1M)
        u32 = (RT_MIN(cbBelow4GB, UINT32_C(0xfe000000)) - 16U * _1M) / _64K;
    else
        u32 = 0;
    pcbiosCmosWrite(pDevIns, 0x34, RT_BYTE1(u32));
    pcbiosCmosWrite(pDevIns, 0x35, RT_BYTE2(u32));

    /* Bochs/VBox BIOS specific way of specifying memory above 4GB in 64KB units.
       Bochs got these in a different location which we've already used for SATA,
       it also lacks the last two. */
    uint64_t c64KBAbove4GB = cbAbove4GB / _64K;
    /* Make sure it doesn't hit the limits of the current BIOS code (RAM limit of ~255TB). */
    AssertLogRelMsgReturn((c64KBAbove4GB >> (3 * 8)) < 255, ("%#RX64\n", c64KBAbove4GB), VERR_OUT_OF_RANGE);
    pcbiosCmosWrite(pDevIns, 0x61, RT_BYTE1(c64KBAbove4GB));
    pcbiosCmosWrite(pDevIns, 0x62, RT_BYTE2(c64KBAbove4GB));
    pcbiosCmosWrite(pDevIns, 0x63, RT_BYTE3(c64KBAbove4GB));
    pcbiosCmosWrite(pDevIns, 0x64, RT_BYTE4(c64KBAbove4GB));
    pcbiosCmosWrite(pDevIns, 0x65, RT_BYTE5(c64KBAbove4GB));

    /*
     * Number of CPUs.
     */
    pcbiosCmosWrite(pDevIns, 0x60, pThis->cCpus & 0xff);

    /*
     * APIC mode.
     */
    pcbiosCmosWrite(pDevIns, 0x78, pThis->u8APICMode);

    /*
     * Bochs BIOS specifics - boot device.
     * We do both new and old (ami-style) settings.
     * See rombios.c line ~7215 (int19_function).
     */

    uint8_t reg3d = getBiosBootCode(pThis, 0) | (getBiosBootCode(pThis, 1) << 4);
    uint8_t reg38 = /* pcbiosCmosRead(pDevIns, 0x38) | */ getBiosBootCode(pThis, 2) << 4;
    /* This is an extension. Bochs BIOS normally supports only 3 boot devices. */
    uint8_t reg3c = getBiosBootCode(pThis, 3) | (pThis->uBootDelay << 4);
    pcbiosCmosWrite(pDevIns, 0x3d, reg3d);
    pcbiosCmosWrite(pDevIns, 0x38, reg38);
    pcbiosCmosWrite(pDevIns, 0x3c, reg3c);

    /*
     * PXE debug option.
     */
    pcbiosCmosWrite(pDevIns, 0x3f, pThis->u8PXEDebug);

    /*
     * Network boot device list.
     */
    for (i = 0; i < NET_BOOT_DEVS; ++i)
    {
        pcbiosCmosWrite(pDevIns, 0x82 + i * 2, RT_BYTE1(pThis->au16NetBootDev[i]));
        pcbiosCmosWrite(pDevIns, 0x83 + i * 2, RT_BYTE2(pThis->au16NetBootDev[i]));
    }

    /*
     * Floppy drive type.
     */
    uint32_t cFDs = 0;
    u32 = 0;
    for (i = 0; i < 2; i++)
    {
        PPDMIBASE pBase;
        int rc = PDMDevHlpQueryLun(pDevIns, pThis->pszFDDevice, 0, i, &pBase);
        if (RT_SUCCESS(rc))
        {
            PPDMIMEDIA pFD = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIA);
            if (pFD)
            {
                cFDs++;
                unsigned cShift = i == 0 ? 4 : 0;
                switch (pFD->pfnGetType(pFD))
                {
                    case PDMMEDIATYPE_FLOPPY_360:       u32 |= 1  << cShift; break;
                    case PDMMEDIATYPE_FLOPPY_1_20:      u32 |= 2  << cShift; break;
                    case PDMMEDIATYPE_FLOPPY_720:       u32 |= 3  << cShift; break;
                    case PDMMEDIATYPE_FLOPPY_1_44:      u32 |= 4  << cShift; break;
                    case PDMMEDIATYPE_FLOPPY_2_88:      u32 |= 5  << cShift; break;
                    case PDMMEDIATYPE_FLOPPY_FAKE_15_6: u32 |= 14 << cShift; break;
                    case PDMMEDIATYPE_FLOPPY_FAKE_63_5: u32 |= 15 << cShift; break;
                    default:                        AssertFailed(); break;
                }
            }
        }
    }
    pcbiosCmosWrite(pDevIns, 0x10, u32);                                        /* 10h - Floppy Drive Type */

    /*
     * Equipment byte.
     */
    if (cFDs > 0)
        u32 = ((cFDs - 1) << 6) | 0x01;    /* floppy installed, additional drives. */
    else
        u32 = 0x00;                        /* floppy not installed. */
    u32 |= RT_BIT(1);                      /* math coprocessor installed  */
    u32 |= RT_BIT(2);                      /* keyboard enabled (or mouse?) */
    u32 |= RT_BIT(3);                      /* display enabled (monitory type is 0, i.e. vga) */
    pcbiosCmosWrite(pDevIns, 0x14, u32);                                        /* 14h - Equipment Byte */

    /*
     * IDE harddisks.
     */
    for (i = 0; i < RT_ELEMENTS(apHDs); i++)
    {
        PPDMIBASE pBase;
        int rc = PDMDevHlpQueryLun(pDevIns, pThis->pszHDDevice, 0, i, &pBase);
        if (RT_SUCCESS(rc))
            apHDs[i] = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIA);
        if (   apHDs[i]
            && (   apHDs[i]->pfnGetType(apHDs[i]) != PDMMEDIATYPE_HARD_DISK
                || !apHDs[i]->pfnBiosIsVisible(apHDs[i])))
            apHDs[i] = NULL;
        if (apHDs[i])
        {
            PDMMEDIAGEOMETRY LCHSGeometry;
            int rc2 = setLogicalDiskGeometry(pBase, apHDs[i], &LCHSGeometry);
            AssertRC(rc2);

            if (i < 4)
            {
                /* Award BIOS extended drive types for first to fourth disk.
                 * Used by the BIOS for setting the logical geometry. */
                int offType, offInfo;
                switch (i)
                {
                    case 0:
                        offType = 0x19;
                        offInfo = 0x1e;
                        break;
                    case 1:
                        offType = 0x1a;
                        offInfo = 0x26;
                        break;
                    case 2:
                        offType = 0x00;
                        offInfo = 0x67;
                        break;
                    case 3:
                    default:
                        offType = 0x00;
                        offInfo = 0x70;
                        break;
                }
                pcbiosCmosInitHardDisk(pDevIns, offType, offInfo,
                                       &LCHSGeometry);
            }
            LogRel(("PcBios: ATA LUN#%d LCHS=%u/%u/%u\n", i, LCHSGeometry.cCylinders, LCHSGeometry.cHeads, LCHSGeometry.cSectors));
        }
    }

    /* 0Fh means extended and points to 19h, 1Ah */
    u32 = (apHDs[0] ? 0xf0 : 0) | (apHDs[1] ? 0x0f : 0);
    pcbiosCmosWrite(pDevIns, 0x12, u32);

    /*
     * SATA harddisks.
     */
    if (pThis->pszSataDevice)
    {
        /* Clear pointers to the block devices. */
        for (i = 0; i < RT_ELEMENTS(apHDs); i++)
            apHDs[i] = NULL;

        for (i = 0; i < RT_ELEMENTS(apHDs); i++)
        {
            PPDMIBASE pBase;
            int rc = PDMDevHlpQueryLun(pDevIns, pThis->pszSataDevice, 0, pThis->iSataHDLUN[i], &pBase);
            if (RT_SUCCESS(rc))
                apHDs[i] = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIA);
            if (   apHDs[i]
                && (   apHDs[i]->pfnGetType(apHDs[i]) != PDMMEDIATYPE_HARD_DISK
                    || !apHDs[i]->pfnBiosIsVisible(apHDs[i])))
                apHDs[i] = NULL;
            if (apHDs[i])
            {
                PDMMEDIAGEOMETRY LCHSGeometry;
                rc = setLogicalDiskGeometry(pBase, apHDs[i], &LCHSGeometry);
                AssertRC(rc);

                if (i < 4)
                {
                    /* Award BIOS extended drive types for first to fourth disk.
                     * Used by the BIOS for setting the logical geometry. */
                    int offInfo;
                    switch (i)
                    {
                        case 0:
                            offInfo = 0x40;
                            break;
                        case 1:
                            offInfo = 0x48;
                            break;
                        case 2:
                            offInfo = 0x50;
                            break;
                        case 3:
                        default:
                            offInfo = 0x58;
                            break;
                    }
                    pcbiosCmosInitHardDisk(pDevIns, 0x00, offInfo,
                                           &LCHSGeometry);
                }
                LogRel(("PcBios: SATA LUN#%d LCHS=%u/%u/%u\n", i, LCHSGeometry.cCylinders, LCHSGeometry.cHeads, LCHSGeometry.cSectors));
            }
        }
    }

    /*
     * SCSI harddisks. Not handled quite the same as SATA.
     */
    if (pThis->pszScsiDevice)
    {
        /* Clear pointers to the block devices. */
        for (i = 0; i < RT_ELEMENTS(apHDs); i++)
            apHDs[i] = NULL;

        for (i = 0; i < RT_ELEMENTS(apHDs); i++)
        {
            PPDMIBASE pBase;
            int rc = PDMDevHlpQueryLun(pDevIns, pThis->pszScsiDevice, 0, pThis->iScsiHDLUN[i], &pBase);
            if (RT_SUCCESS(rc))
                apHDs[i] = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIA);
            if (   apHDs[i]
                && (   apHDs[i]->pfnGetType(apHDs[i]) != PDMMEDIATYPE_HARD_DISK
                    || !apHDs[i]->pfnBiosIsVisible(apHDs[i])))
                apHDs[i] = NULL;
            if (apHDs[i])
            {
                PDMMEDIAGEOMETRY LCHSGeometry;
                rc = getLogicalDiskGeometry(apHDs[i], &LCHSGeometry);

                if (i < 4 && RT_SUCCESS(rc))
                {
                    /* Extended drive information (for SCSI disks).
                     * Used by the BIOS for setting the logical geometry, but
                     * only if the image provided valid data.
                     */
                    int offInfo;
                    switch (i)
                    {
                        case 0:
                            offInfo = 0x90;
                            break;
                        case 1:
                            offInfo = 0x98;
                            break;
                        case 2:
                            offInfo = 0xa0;
                            break;
                        case 3:
                        default:
                            offInfo = 0xa8;
                            break;
                    }
                    pcbiosCmosInitHardDisk(pDevIns, 0x00, offInfo, &LCHSGeometry);
                    LogRel(("PcBios: SCSI LUN#%d LCHS=%u/%u/%u\n", i, LCHSGeometry.cCylinders, LCHSGeometry.cHeads, LCHSGeometry.cSectors));
                }
                else
                    LogRel(("PcBios: SCSI LUN#%d LCHS not provided\n", i));
            }
        }
    }

    /* Calculate and store AT-style CMOS checksum. */
    uint16_t    cksum = 0;
    for (i = 0x10; i < 0x2e; ++i)
        cksum += pcbiosCmosRead(pDevIns, i);
    pcbiosCmosWrite(pDevIns, 0x2e, RT_BYTE1(cksum));
    pcbiosCmosWrite(pDevIns, 0x2f, RT_BYTE2(cksum));

    LogFlow(("%s: returns VINF_SUCCESS\n", __FUNCTION__));
    return VINF_SUCCESS;
}


/**
 * @interface_method_impl{PDMDEVREG,pfnMemSetup}
 */
static DECLCALLBACK(void) pcbiosMemSetup(PPDMDEVINS pDevIns, PDMDEVMEMSETUPCTX enmCtx)
{
    RT_NOREF1(enmCtx);
    PDEVPCBIOS pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPCBIOS);
    LogFlow(("pcbiosMemSetup:\n"));

    if (pThis->u8IOAPIC)
        FwCommonPlantMpsFloatPtr(pDevIns, pThis->u32MPTableAddr);

    /*
     * Re-shadow the LAN ROM image and make it RAM/RAM.
     *
     * This is normally done by the BIOS code, but since we're currently lacking
     * the chipset support for this we do it here (and in the constructor).
     */
    uint32_t    cPages = RT_ALIGN_64(pThis->cbLanBoot, GUEST_PAGE_SIZE) >> GUEST_PAGE_SHIFT;
    RTGCPHYS    GCPhys = VBOX_LANBOOT_SEG << 4;
    while (cPages > 0)
    {
        uint8_t abPage[GUEST_PAGE_SIZE];
        int     rc;

        /* Read the (original) ROM page and write it back to the RAM page. */
        rc = PDMDevHlpROMProtectShadow(pDevIns, GCPhys, GUEST_PAGE_SIZE, PGMROMPROT_READ_ROM_WRITE_RAM);
        AssertLogRelRC(rc);

        rc = PDMDevHlpPhysRead(pDevIns, GCPhys, abPage, GUEST_PAGE_SIZE);
        AssertLogRelRC(rc);
        if (RT_FAILURE(rc))
            memset(abPage, 0xcc, sizeof(abPage));

        rc = PDMDevHlpPhysWrite(pDevIns, GCPhys, abPage, GUEST_PAGE_SIZE);
        AssertLogRelRC(rc);

        /* Switch to the RAM/RAM mode. */
        rc = PDMDevHlpROMProtectShadow(pDevIns, GCPhys, GUEST_PAGE_SIZE, PGMROMPROT_READ_RAM_WRITE_RAM);
        AssertLogRelRC(rc);

        /* Advance */
        GCPhys += GUEST_PAGE_SIZE;
        cPages--;
    }
}


/**
 * @interface_method_impl{PDMDEVREG,pfnDestruct}
 */
static DECLCALLBACK(int) pcbiosDestruct(PPDMDEVINS pDevIns)
{
    PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
    PDEVPCBIOS  pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPCBIOS);
    LogFlow(("pcbiosDestruct:\n"));

    /*
     * Free MM heap pointers.
     */
    if (pThis->pu8PcBios)
    {
        PDMDevHlpMMHeapFree(pDevIns, pThis->pu8PcBios);
        pThis->pu8PcBios = NULL;
    }

    if (pThis->pszPcBiosFile)
    {
        PDMDevHlpMMHeapFree(pDevIns, pThis->pszPcBiosFile);
        pThis->pszPcBiosFile = NULL;
    }

    if (pThis->pu8LanBoot)
    {
        PDMDevHlpMMHeapFree(pDevIns, pThis->pu8LanBoot);
        pThis->pu8LanBoot = NULL;
    }

    if (pThis->pszLanBootFile)
    {
        PDMDevHlpMMHeapFree(pDevIns, pThis->pszLanBootFile);
        pThis->pszLanBootFile = NULL;
    }

    if (pThis->pszHDDevice)
    {
        PDMDevHlpMMHeapFree(pDevIns, pThis->pszHDDevice);
        pThis->pszHDDevice = NULL;
    }

    if (pThis->pszFDDevice)
    {
        PDMDevHlpMMHeapFree(pDevIns, pThis->pszFDDevice);
        pThis->pszFDDevice = NULL;
    }

    if (pThis->pszSataDevice)
    {
        PDMDevHlpMMHeapFree(pDevIns, pThis->pszSataDevice);
        pThis->pszSataDevice = NULL;
    }

    if (pThis->pszScsiDevice)
    {
        PDMDevHlpMMHeapFree(pDevIns, pThis->pszScsiDevice);
        pThis->pszScsiDevice = NULL;
    }

    return VINF_SUCCESS;
}


/**
 * Convert config value to DEVPCBIOSBOOT.
 *
 * @returns VBox status code.
 * @param   pDevIns         Device instance data.
 * @param   pCfg            Configuration handle.
 * @param   pszParam        The name of the value to read.
 * @param   penmBoot        Where to store the boot method.
 */
static int pcbiosBootFromCfg(PPDMDEVINS pDevIns, PCFGMNODE pCfg, const char *pszParam, DEVPCBIOSBOOT *penmBoot)
{
    PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;

    char szBuf[64];
    int rc = pHlp->pfnCFGMQueryString(pCfg, pszParam, szBuf, sizeof(szBuf));
    if (RT_FAILURE(rc))
        return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
                                   N_("Configuration error: Querying \"%s\" as a string failed"), pszParam);

    if (!strcmp(szBuf, "DVD") || !strcmp(szBuf, "CDROM"))
        *penmBoot = DEVPCBIOSBOOT_DVD;
    else if (!strcmp(szBuf, "IDE"))
        *penmBoot = DEVPCBIOSBOOT_HD;
    else if (!strcmp(szBuf, "FLOPPY"))
        *penmBoot = DEVPCBIOSBOOT_FLOPPY;
    else if (!strcmp(szBuf, "LAN"))
        *penmBoot = DEVPCBIOSBOOT_LAN;
    else if (!strcmp(szBuf, "NONE"))
        *penmBoot = DEVPCBIOSBOOT_NONE;
    else
        rc = PDMDevHlpVMSetError(pDevIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
                                 N_("Configuration error: The \"%s\" value \"%s\" is unknown"),  pszParam, szBuf);
    return rc;
}


/**
 * @interface_method_impl{PDMDEVREG,pfnConstruct}
 */
static DECLCALLBACK(int)  pcbiosConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
{
    PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
    PDEVPCBIOS      pThis   = PDMDEVINS_2_DATA(pDevIns, PDEVPCBIOS);
    PCPDMDEVHLPR3   pHlp    = pDevIns->pHlpR3;
    int             rc;
    int             cb;
    Assert(iInstance == 0); RT_NOREF(iInstance);

    /*
     * Validate configuration.
     */
    PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns,
                                  "BootDevice0"
                                  "|BootDevice1"
                                  "|BootDevice2"
                                  "|BootDevice3"
                                  "|HardDiskDevice"
                                  "|SataHardDiskDevice"
                                  "|SataLUN1"
                                  "|SataLUN2"
                                  "|SataLUN3"
                                  "|SataLUN4"
                                  "|ScsiHardDiskDevice"
                                  "|ScsiLUN1"
                                  "|ScsiLUN2"
                                  "|ScsiLUN3"
                                  "|ScsiLUN4"
                                  "|FloppyDevice"
                                  "|DelayBoot"
                                  "|BiosRom"
                                  "|LanBootRom"
                                  "|PXEDebug"
                                  "|UUID"
                                  "|UuidLe"
                                  "|IOAPIC"
                                  "|APIC"
                                  "|NumCPUs"
                                  "|McfgBase"
                                  "|McfgLength"
                                  "|DmiBIOSFirmwareMajor"
                                  "|DmiBIOSFirmwareMinor"
                                  "|DmiBIOSReleaseDate"
                                  "|DmiBIOSReleaseMajor"
                                  "|DmiBIOSReleaseMinor"
                                  "|DmiBIOSVendor"
                                  "|DmiBIOSVersion"
                                  "|DmiSystemFamily"
                                  "|DmiSystemProduct"
                                  "|DmiSystemSerial"
                                  "|DmiSystemSKU"
                                  "|DmiSystemUuid"
                                  "|DmiSystemVendor"
                                  "|DmiSystemVersion"
                                  "|DmiBoardAssetTag"
                                  "|DmiBoardBoardType"
                                  "|DmiBoardLocInChass"
                                  "|DmiBoardProduct"
                                  "|DmiBoardSerial"
                                  "|DmiBoardVendor"
                                  "|DmiBoardVersion"
                                  "|DmiChassisAssetTag"
                                  "|DmiChassisSerial"
                                  "|DmiChassisType"
                                  "|DmiChassisVendor"
                                  "|DmiChassisVersion"
                                  "|DmiProcManufacturer"
                                  "|DmiProcVersion"
                                  "|DmiOEMVBoxVer"
                                  "|DmiOEMVBoxRev"
                                  "|DmiUseHostInfo"
                                  "|DmiExposeMemoryTable"
                                  "|DmiExposeProcInf"
                                  "|CheckShutdownStatusForSoftReset"
                                  "|ClearShutdownStatusOnHardReset"
                                  ,
                                  "NetBoot");
    /*
     * Init the data.
     */
    rc = pHlp->pfnCFGMQueryU16Def(pCfg, "NumCPUs", &pThis->cCpus, 1);
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Querying \"NumCPUs\" as integer failed"));

    rc = pHlp->pfnCFGMQueryU64Def(pCfg, "McfgBase", &pThis->u64McfgBase, 0);
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Querying \"\" as integer failed"));
    rc = pHlp->pfnCFGMQueryU64Def(pCfg, "McfgLength", &pThis->cbMcfgLength, 0);
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Querying \"McfgLength\" as integer failed"));


    LogRel(("PcBios: [SMP] BIOS with %u CPUs\n", pThis->cCpus));

    rc = pHlp->pfnCFGMQueryU8Def(pCfg, "IOAPIC", &pThis->u8IOAPIC, 1);
    if (RT_FAILURE (rc))
        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to read \"IOAPIC\""));

    rc = pHlp->pfnCFGMQueryU8Def(pCfg, "APIC", &pThis->u8APICMode, 1);
    if (RT_FAILURE (rc))
        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to read \"APIC\""));

    static const char * const s_apszBootDevices[] = { "BootDevice0", "BootDevice1", "BootDevice2", "BootDevice3" };
    Assert(RT_ELEMENTS(s_apszBootDevices) == RT_ELEMENTS(pThis->aenmBootDevice));
    for (unsigned i = 0; i < RT_ELEMENTS(pThis->aenmBootDevice); i++)
    {
        rc = pcbiosBootFromCfg(pDevIns, pCfg, s_apszBootDevices[i], &pThis->aenmBootDevice[i]);
        if (RT_FAILURE(rc))
            return rc;
    }

    rc = pHlp->pfnCFGMQueryStringAlloc(pCfg, "HardDiskDevice", &pThis->pszHDDevice);
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Querying \"HardDiskDevice\" as a string failed"));

    rc = pHlp->pfnCFGMQueryStringAlloc(pCfg, "FloppyDevice", &pThis->pszFDDevice);
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Querying \"FloppyDevice\" as a string failed"));

    rc = pHlp->pfnCFGMQueryStringAlloc(pCfg, "SataHardDiskDevice", &pThis->pszSataDevice);
    if (rc == VERR_CFGM_VALUE_NOT_FOUND)
        pThis->pszSataDevice = NULL;
    else if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Querying \"SataHardDiskDevice\" as a string failed"));

    if (pThis->pszSataDevice)
    {
        static const char * const s_apszSataDisks[] = { "SataLUN1", "SataLUN2", "SataLUN3", "SataLUN4" };
        Assert(RT_ELEMENTS(s_apszSataDisks) == RT_ELEMENTS(pThis->iSataHDLUN));
        for (unsigned i = 0; i < RT_ELEMENTS(pThis->iSataHDLUN); i++)
        {
            rc = pHlp->pfnCFGMQueryU32(pCfg, s_apszSataDisks[i], &pThis->iSataHDLUN[i]);
            if (rc == VERR_CFGM_VALUE_NOT_FOUND)
                pThis->iSataHDLUN[i] = i;
            else if (RT_FAILURE(rc))
                return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
                                           N_("Configuration error: Querying \"%s\" as a string failed"), s_apszSataDisks);
        }
    }

    /* Repeat the exercise for SCSI drives. */
    rc = pHlp->pfnCFGMQueryStringAlloc(pCfg, "ScsiHardDiskDevice", &pThis->pszScsiDevice);
    if (rc == VERR_CFGM_VALUE_NOT_FOUND)
        pThis->pszScsiDevice = NULL;
    else if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Querying \"ScsiHardDiskDevice\" as a string failed"));

    if (pThis->pszScsiDevice)
    {
        static const char * const s_apszScsiDisks[] = { "ScsiLUN1", "ScsiLUN2", "ScsiLUN3", "ScsiLUN4" };
        Assert(RT_ELEMENTS(s_apszScsiDisks) == RT_ELEMENTS(pThis->iScsiHDLUN));
        for (unsigned i = 0; i < RT_ELEMENTS(pThis->iScsiHDLUN); i++)
        {
            rc = pHlp->pfnCFGMQueryU32(pCfg, s_apszScsiDisks[i], &pThis->iScsiHDLUN[i]);
            if (rc == VERR_CFGM_VALUE_NOT_FOUND)
                pThis->iScsiHDLUN[i] = i;
            else if (RT_FAILURE(rc))
                return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
                                           N_("Configuration error: Querying \"%s\" as a string failed"), s_apszScsiDisks);
        }
    }

    /* PXE debug logging option. */
    rc = pHlp->pfnCFGMQueryU8Def(pCfg, "PXEDebug", &pThis->u8PXEDebug, false);
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Querying \"PXEDebug\" as integer failed"));


    /*
     * Register the I/O Ports.
     */
    IOMIOPORTHANDLE hIoPorts;
    rc = PDMDevHlpIoPortCreateAndMap(pDevIns, 0x400 /*uPort*/, 4 /*cPorts*/, pcbiosIOPortDebugWrite, pcbiosIOPortDebugRead,
                                     "Bochs PC BIOS - Panic & Debug", NULL, &hIoPorts);
    AssertRCReturn(rc, rc);

    rc = PDMDevHlpIoPortCreateIsa(pDevIns, 1 /*cPorts*/, pcbiosIOPortShutdownWrite, pcbiosIOPortShutdownRead, NULL /*pvUser*/,
                                  "Bochs PC BIOS - Shutdown", NULL /*paExtDescs*/, &pThis->hIoPortShutdown);
    AssertRCReturn(rc, rc);
    rc = pcbiosRegisterShutdown(pDevIns, pThis, true /* fNewShutdownPort */);
    AssertRCReturn(rc, rc);

    /*
     * Register SSM handlers, for remembering which shutdown port to use.
     */
    rc = PDMDevHlpSSMRegisterEx(pDevIns, PCBIOS_SSM_VERSION, 1 /* cbGuess */, NULL,
                                NULL, NULL, NULL,
                                NULL, pcbiosSaveExec, NULL,
                                pcbiosLoadPrep, pcbiosLoadExec, pcbiosLoadDone);

    /* Clear the net boot device list. All bits set invokes old behavior,
     * as if no second CMOS bank was present.
     */
    memset(&pThis->au16NetBootDev, 0xff, sizeof(pThis->au16NetBootDev));

    /*
     * Determine the network boot order.
     */
    PCFGMNODE pCfgNetBoot = pHlp->pfnCFGMGetChild(pCfg, "NetBoot");
    if (pCfgNetBoot == NULL)
    {
        /* Do nothing. */
        rc = VINF_SUCCESS;
    }
    else
    {
        PCFGMNODE   pCfgNetBootDevice;
        uint8_t     u8PciBus;
        uint8_t     u8PciDev;
        uint8_t     u8PciFn;
        uint16_t    u16BusDevFn;
        char        szIndex[] = "?";

        Assert(pCfgNetBoot);
        for (unsigned i = 0; i < NET_BOOT_DEVS; ++i)
        {
            szIndex[0] = '0' + i;
            pCfgNetBootDevice = pHlp->pfnCFGMGetChild(pCfgNetBoot, szIndex);

            rc = pHlp->pfnCFGMQueryU8(pCfgNetBootDevice, "PCIBusNo", &u8PciBus);
            if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
            {
                /* Do nothing and stop iterating. */
                rc = VINF_SUCCESS;
                break;
            }
            else if (RT_FAILURE(rc))
                return PDMDEV_SET_ERROR(pDevIns, rc,
                                        N_("Configuration error: Querying \"Netboot/x/PCIBusNo\" as integer failed"));
            rc = pHlp->pfnCFGMQueryU8(pCfgNetBootDevice, "PCIDeviceNo", &u8PciDev);
            if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
            {
                /* Do nothing and stop iterating. */
                rc = VINF_SUCCESS;
                break;
            }
            else if (RT_FAILURE(rc))
                return PDMDEV_SET_ERROR(pDevIns, rc,
                                        N_("Configuration error: Querying \"Netboot/x/PCIDeviceNo\" as integer failed"));
            rc = pHlp->pfnCFGMQueryU8(pCfgNetBootDevice, "PCIFunctionNo", &u8PciFn);
            if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
            {
                /* Do nothing and stop iterating. */
                rc = VINF_SUCCESS;
                break;
            }
            else if (RT_FAILURE(rc))
                return PDMDEV_SET_ERROR(pDevIns, rc,
                                        N_("Configuration error: Querying \"Netboot/x/PCIFunctionNo\" as integer failed"));
            u16BusDevFn = (((uint16_t)u8PciBus) << 8) | ((u8PciDev & 0x1F) << 3) | (u8PciFn & 0x7);
            pThis->au16NetBootDev[i] = u16BusDevFn;
        }
    }

    /*
     * Get the system BIOS ROM file name.
     */
    rc = pHlp->pfnCFGMQueryStringAlloc(pCfg, "BiosRom", &pThis->pszPcBiosFile);
    if (rc == VERR_CFGM_VALUE_NOT_FOUND)
    {
        pThis->pszPcBiosFile = NULL;
        rc = VINF_SUCCESS;
    }
    else if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc,
                                N_("Configuration error: Querying \"BiosRom\" as a string failed"));
    else if (!*pThis->pszPcBiosFile)
    {
        PDMDevHlpMMHeapFree(pDevIns, pThis->pszPcBiosFile);
        pThis->pszPcBiosFile = NULL;
    }

    /*
     * Get the CPU arch so we can load the appropriate ROMs.
     */
    CPUMMICROARCH const enmMicroarch = PDMDevHlpCpuGetGuestMicroarch(pDevIns);

    if (pThis->pszPcBiosFile)
    {
        /*
         * Load the BIOS ROM.
         */
        RTFILE hFilePcBios;
        rc = RTFileOpen(&hFilePcBios, pThis->pszPcBiosFile,
                        RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
        if (RT_SUCCESS(rc))
        {
            /* Figure the size and check restrictions. */
            uint64_t cbPcBios;
            rc = RTFileQuerySize(hFilePcBios, &cbPcBios);
            if (RT_SUCCESS(rc))
            {
                pThis->cbPcBios = (uint32_t)cbPcBios;
                if (    RT_ALIGN(pThis->cbPcBios, _64K) == pThis->cbPcBios
                    &&  pThis->cbPcBios == cbPcBios
                    &&  pThis->cbPcBios <= 32 * _64K
                    &&  pThis->cbPcBios >= _64K)
                {
                    pThis->pu8PcBios = (uint8_t *)PDMDevHlpMMHeapAlloc(pDevIns, pThis->cbPcBios);
                    if (pThis->pu8PcBios)
                    {
                        rc = RTFileRead(hFilePcBios, pThis->pu8PcBios, pThis->cbPcBios, NULL);
                        if (RT_FAILURE(rc))
                            rc = PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
                                                     N_("Error reading the BIOS image ('%s)"), pThis->pszPcBiosFile);
                    }
                    else
                        rc = PDMDevHlpVMSetError(pDevIns, VERR_NO_MEMORY, RT_SRC_POS,
                                                 N_("Failed to allocate %#x bytes for loading the BIOS image"),
                                                 pThis->cbPcBios);
                }
                else
                    rc = PDMDevHlpVMSetError(pDevIns, VERR_OUT_OF_RANGE, RT_SRC_POS,
                                             N_("Invalid system BIOS file size ('%s'): %#llx (%llu)"),
                                             pThis->pszPcBiosFile, cbPcBios, cbPcBios);
            }
            else
                rc = PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
                                         N_("Failed to query the system BIOS file size ('%s')"),
                                         pThis->pszPcBiosFile);
            RTFileClose(hFilePcBios);
        }
        else
            rc = PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
                                     N_("Failed to open system BIOS file '%s'"), pThis->pszPcBiosFile);
        if (RT_FAILURE(rc))
            return rc;

        LogRel(("PcBios: Using BIOS ROM '%s' with a size of %#x bytes\n", pThis->pszPcBiosFile, pThis->cbPcBios));
    }
    else
    {
        /*
         * Use one of the embedded BIOS ROM images.
         */
        uint8_t const *pbBios;
        uint32_t       cbBios;
        if (   enmMicroarch == kCpumMicroarch_Intel_8086
            || enmMicroarch == kCpumMicroarch_Intel_80186
            || enmMicroarch == kCpumMicroarch_NEC_V20
            || enmMicroarch == kCpumMicroarch_NEC_V30)
        {
            pbBios = g_abPcBiosBinary8086;
            cbBios = g_cbPcBiosBinary8086;
            LogRel(("PcBios: Using the 8086 BIOS image!\n"));
        }
        else if (enmMicroarch == kCpumMicroarch_Intel_80286)
        {
            pbBios = g_abPcBiosBinary286;
            cbBios = g_cbPcBiosBinary286;
            LogRel(("PcBios: Using the 286 BIOS image!\n"));
        }
        else
        {
            pbBios = g_abPcBiosBinary386;
            cbBios = g_cbPcBiosBinary386;
            LogRel(("PcBios: Using the 386+ BIOS image.\n"));
        }
        pThis->pu8PcBios = (uint8_t *)PDMDevHlpMMHeapAlloc(pDevIns, cbBios);
        if (pThis->pu8PcBios)
        {
            pThis->cbPcBios = cbBios;
            memcpy(pThis->pu8PcBios, pbBios, cbBios);
        }
        else
            return PDMDevHlpVMSetError(pDevIns, VERR_NO_MEMORY, RT_SRC_POS,
                                       N_("Failed to allocate %#x bytes for loading the embedded BIOS image"), cbBios);
    }
    const uint8_t *pu8PcBiosBinary = pThis->pu8PcBios;
    uint32_t       cbPcBiosBinary  = pThis->cbPcBios;

    /*
     * Query the machine's UUID for SMBIOS/DMI use.
     */
    RTUUID  uuid;
    rc = pHlp->pfnCFGMQueryBytes(pCfg, "UUID", &uuid, sizeof(uuid));
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc,
                                N_("Configuration error: Querying \"UUID\" failed"));

    bool fUuidLe;
    rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "UuidLe", &fUuidLe, false);
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc,
                                N_("Configuration error: Querying \"UuidLe\" failed"));

    if (!fUuidLe)
    {
        /*
         * UUIDs are stored little endian actually (see chapter 7.2.1 System — UUID
         * of the DMI/SMBIOS spec) but to not force reactivation of existing guests we have
         * to carry this bug along... (see also DevEFI.cpp when changing this)
         *
         * Convert the UUID to network byte order. Not entirely straightforward as
         * parts are MSB already...
         */
        uuid.Gen.u32TimeLow = RT_H2BE_U32(uuid.Gen.u32TimeLow);
        uuid.Gen.u16TimeMid = RT_H2BE_U16(uuid.Gen.u16TimeMid);
        uuid.Gen.u16TimeHiAndVersion = RT_H2BE_U16(uuid.Gen.u16TimeHiAndVersion);
    }

    uint16_t cbDmiTables = 0;
    uint16_t cDmiTables = 0;
    rc = FwCommonPlantDMITable(pDevIns, pThis->au8DMIPage, VBOX_DMI_TABLE_SIZE,
                               &uuid, pCfg, pThis->cCpus, &cbDmiTables, &cDmiTables,
                               false /*fUefi*/);
    if (RT_FAILURE(rc))
        return rc;

    /* Look for _SM_/_DMI_ anchor strings within the BIOS and replace the table headers. */
    unsigned       offAnchor  = ~0U;
    unsigned const cbToSearch = pThis->cbPcBios - 32;
    for (unsigned off = 0; off <= cbToSearch; off += 16)
    {
        if (   pThis->pu8PcBios[off + 0x00] != '_'
            || pThis->pu8PcBios[off + 0x01] != 'S'
            || pThis->pu8PcBios[off + 0x02] != 'M'
            || pThis->pu8PcBios[off + 0x03] != '_'
            || pThis->pu8PcBios[off + 0x10] != '_'
            || pThis->pu8PcBios[off + 0x11] != 'D'
            || pThis->pu8PcBios[off + 0x12] != 'M'
            || pThis->pu8PcBios[off + 0x13] != 'I'
            || pThis->pu8PcBios[off + 0x14] != '_')
        { /* likely */ }
        else
        {
            offAnchor = off;
            FwCommonPlantSmbiosAndDmiHdrs(pDevIns, pThis->pu8PcBios + off, cbDmiTables, cDmiTables);
            break;
        }
    }
    AssertLogRel(offAnchor <= cbToSearch);

    if (pThis->u8IOAPIC)
    {
        pThis->u32MPTableAddr = VBOX_DMI_TABLE_BASE + VBOX_DMI_TABLE_SIZE;
        FwCommonPlantMpsTable(pDevIns, pThis->au8DMIPage /* aka VBOX_DMI_TABLE_BASE */ + VBOX_DMI_TABLE_SIZE,
                              _4K - VBOX_DMI_TABLE_SIZE, pThis->cCpus);
        LogRel(("PcBios: MPS table at %08x\n", pThis->u32MPTableAddr));
    }

    rc = PDMDevHlpROMRegister(pDevIns, VBOX_DMI_TABLE_BASE, _4K, pThis->au8DMIPage, _4K,
                              PGMPHYS_ROM_FLAGS_PERMANENT_BINARY, "DMI tables");
    if (RT_FAILURE(rc))
        return rc;

    /*
     * Map the BIOS into memory.
     * There are two mappings:
     *      1. 0x000e0000 to 0x000fffff contains the last 128 kb of the bios.
     *         The bios code might be 64 kb in size, and will then start at 0xf0000.
     *      2. 0xfffxxxxx to 0xffffffff contains the entire bios.
     */
    AssertReleaseMsg(cbPcBiosBinary >= _64K, ("cbPcBiosBinary=%#x\n", cbPcBiosBinary));
    AssertReleaseMsg(RT_ALIGN_Z(cbPcBiosBinary, _64K) == cbPcBiosBinary,
                     ("cbPcBiosBinary=%#x\n", cbPcBiosBinary));
    cb = RT_MIN(cbPcBiosBinary, 128 * _1K); /* Effectively either 64 or 128K. */
    rc = PDMDevHlpROMRegister(pDevIns, 0x00100000 - cb, cb, &pu8PcBiosBinary[cbPcBiosBinary - cb], cb,
                              PGMPHYS_ROM_FLAGS_PERMANENT_BINARY, "PC BIOS - 0xfffff");
    if (RT_FAILURE(rc))
        return rc;
    rc = PDMDevHlpROMRegister(pDevIns, (uint32_t)-(int32_t)cbPcBiosBinary, cbPcBiosBinary, pu8PcBiosBinary, cbPcBiosBinary,
                              PGMPHYS_ROM_FLAGS_PERMANENT_BINARY, "PC BIOS - 0xffffffff");
    if (RT_FAILURE(rc))
        return rc;

    /*
     * Get the LAN boot ROM file name.
     */
    rc = pHlp->pfnCFGMQueryStringAlloc(pCfg, "LanBootRom", &pThis->pszLanBootFile);
    if (rc == VERR_CFGM_VALUE_NOT_FOUND)
    {
        pThis->pszLanBootFile = NULL;
        rc = VINF_SUCCESS;
    }
    else if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc,
                                N_("Configuration error: Querying \"LanBootRom\" as a string failed"));
    else if (!*pThis->pszLanBootFile)
    {
        PDMDevHlpMMHeapFree(pDevIns, pThis->pszLanBootFile);
        pThis->pszLanBootFile = NULL;
    }

    /*
     * Not loading LAN ROM for old CPUs.
     */
    if (   enmMicroarch != kCpumMicroarch_Intel_8086
        && enmMicroarch != kCpumMicroarch_Intel_80186
        && enmMicroarch != kCpumMicroarch_NEC_V20
        && enmMicroarch != kCpumMicroarch_NEC_V30
        && enmMicroarch != kCpumMicroarch_Intel_80286)
    {
        const uint8_t  *pu8LanBootBinary = NULL;
        uint64_t        cbLanBootBinary;
        uint64_t        cbFileLanBoot = 0;

        /*
         * Open the LAN boot ROM and figure it size.
         * Determine the LAN boot ROM size, open specified ROM file in the process.
         */
        if (pThis->pszLanBootFile)
        {
            RTFILE hFileLanBoot = NIL_RTFILE;
            rc = RTFileOpen(&hFileLanBoot, pThis->pszLanBootFile,
                            RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
            if (RT_SUCCESS(rc))
            {
                rc = RTFileQuerySize(hFileLanBoot, &cbFileLanBoot);
                if (RT_SUCCESS(rc))
                {
                    if (cbFileLanBoot <= _64K - (VBOX_LANBOOT_SEG << 4 & 0xffff))
                    {
                        LogRel(("PcBios: Using LAN ROM '%s' with a size of %#x bytes\n", pThis->pszLanBootFile, cbFileLanBoot));

                        /*
                         * Allocate buffer for the LAN boot ROM data and load it.
                         */
                        pThis->pu8LanBoot = (uint8_t *)PDMDevHlpMMHeapAllocZ(pDevIns, cbFileLanBoot);
                        if (pThis->pu8LanBoot)
                        {
                            rc = RTFileRead(hFileLanBoot, pThis->pu8LanBoot, cbFileLanBoot, NULL);
                            AssertLogRelRCReturnStmt(rc, RTFileClose(hFileLanBoot), rc);
                        }
                        else
                            rc = VERR_NO_MEMORY;
                    }
                    else
                        rc = VERR_TOO_MUCH_DATA;
                }
                RTFileClose(hFileLanBoot);
            }
            if (RT_FAILURE(rc))
            {
                /*
                 * Play stupid and ignore failures, falling back to the built-in LAN boot ROM.
                 */
                /** @todo r=bird: This should have some kind of rational. We don't usually
                 *        ignore the VM configuration.  */
                LogRel(("PcBios: Failed to open LAN boot ROM file '%s', rc=%Rrc!\n", pThis->pszLanBootFile, rc));
                PDMDevHlpMMHeapFree(pDevIns, pThis->pszLanBootFile);
                pThis->pszLanBootFile = NULL;
            }
        }

        /* If we were unable to get the data from file for whatever reason, fall
         * back to the built-in LAN boot ROM image.
         */
        if (pThis->pu8LanBoot == NULL)
        {
#ifdef VBOX_WITH_PXE_ROM
            pu8LanBootBinary = g_abNetBiosBinary;
            cbLanBootBinary  = g_cbNetBiosBinary;
#endif
        }
        else
        {
            pu8LanBootBinary = pThis->pu8LanBoot;
            cbLanBootBinary  = cbFileLanBoot;
        }

        /*
         * Map the Network Boot ROM into memory.
         *
         * Currently there is a fixed mapping: 0x000e2000 to 0x000effff contains
         * the (up to) 56 kb ROM image.  The mapping size is fixed to trouble with
         * the saved state (in PGM).
         */
        if (pu8LanBootBinary)
        {
            pThis->cbLanBoot = cbLanBootBinary;

            rc = PDMDevHlpROMRegister(pDevIns, VBOX_LANBOOT_SEG << 4,
                                      RT_MAX(cbLanBootBinary, _64K - (VBOX_LANBOOT_SEG << 4 & 0xffff)),
                                      pu8LanBootBinary, cbLanBootBinary,
                                      PGMPHYS_ROM_FLAGS_SHADOWED, "Net Boot ROM");
            AssertRCReturn(rc, rc);
        }
    }
    else if (pThis->pszLanBootFile)
        LogRel(("PcBios: Skipping LAN ROM '%s' due to ancient target CPU.\n", pThis->pszLanBootFile));
#ifdef VBOX_WITH_PXE_ROM
    else
        LogRel(("PcBios: Skipping built in ROM due to ancient target CPU.\n"));
#endif

    /*
     * Configure Boot delay.
     */
    rc = pHlp->pfnCFGMQueryU8Def(pCfg, "DelayBoot", &pThis->uBootDelay, 0);
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc,
                                    N_("Configuration error: Querying \"DelayBoot\" as integer failed"));
    if (pThis->uBootDelay > 15)
        pThis->uBootDelay = 15;


    /*
     * Read shutdown status code config and register ourselves as the firmware device.
     */

    /** @cfgm{CheckShutdownStatusForSoftReset, boolean, true}
     * Whether to consult the shutdown status code (CMOS register 0Fh) to
     * determine whether the guest intended a soft or hard reset.  Currently only
     * shutdown status codes 05h, 09h and 0Ah are considered soft reset. */
    rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "CheckShutdownStatusForSoftReset", &pThis->fCheckShutdownStatusForSoftReset, true);
    AssertLogRelRCReturn(rc, rc);

    /** @cfgm{ClearShutdownStatusOnHardReset, boolean, true}
     * Whether to clear the shutdown status code (CMOS register 0Fh) on hard reset. */
    rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "ClearShutdownStatusOnHardReset", &pThis->fClearShutdownStatusOnHardReset, true);
    AssertLogRelRCReturn(rc, rc);

    LogRel(("PcBios: fCheckShutdownStatusForSoftReset=%RTbool  fClearShutdownStatusOnHardReset=%RTbool\n",
            pThis->fCheckShutdownStatusForSoftReset, pThis->fClearShutdownStatusOnHardReset));

    static PDMFWREG const s_FwReg = { PDM_FWREG_VERSION, pcbiosFw_IsHardReset, PDM_FWREG_VERSION };
    rc = PDMDevHlpFirmwareRegister(pDevIns, &s_FwReg, &pThis->pFwHlpR3);
    AssertLogRelRCReturn(rc, rc);

    return VINF_SUCCESS;
}


/**
 * The device registration structure.
 */
const PDMDEVREG g_DevicePcBios =
{
    /* .u32Version = */             PDM_DEVREG_VERSION,
    /* .uReserved0 = */             0,
    /* .szName = */                 "pcbios",
    /* .fFlags = */                 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_NEW_STYLE,
    /* .fClass = */                 PDM_DEVREG_CLASS_ARCH_BIOS,
    /* .cMaxInstances = */          1,
    /* .uSharedVersion = */         42,
    /* .cbInstanceShared = */       sizeof(DEVPCBIOS),
    /* .cbInstanceCC = */           0,
    /* .cbInstanceRC = */           0,
    /* .cMaxPciDevices = */         0,
    /* .cMaxMsixVectors = */        0,
    /* .pszDescription = */         "PC BIOS Device",
#if defined(IN_RING3)
    /* .pszRCMod = */               "",
    /* .pszR0Mod = */               "",
    /* .pfnConstruct = */           pcbiosConstruct,
    /* .pfnDestruct = */            pcbiosDestruct,
    /* .pfnRelocate = */            NULL,
    /* .pfnMemSetup = */            pcbiosMemSetup,
    /* .pfnPowerOn = */             NULL,
    /* .pfnReset = */               pcbiosReset,
    /* .pfnSuspend = */             NULL,
    /* .pfnResume = */              NULL,
    /* .pfnAttach = */              NULL,
    /* .pfnDetach = */              NULL,
    /* .pfnQueryInterface = */      NULL,
    /* .pfnInitComplete = */        pcbiosInitComplete,
    /* .pfnPowerOff = */            NULL,
    /* .pfnSoftReset = */           NULL,
    /* .pfnReserved0 = */           NULL,
    /* .pfnReserved1 = */           NULL,
    /* .pfnReserved2 = */           NULL,
    /* .pfnReserved3 = */           NULL,
    /* .pfnReserved4 = */           NULL,
    /* .pfnReserved5 = */           NULL,
    /* .pfnReserved6 = */           NULL,
    /* .pfnReserved7 = */           NULL,
#elif defined(IN_RING0)
    /* .pfnEarlyConstruct = */      NULL,
    /* .pfnConstruct = */           NULL,
    /* .pfnDestruct = */            NULL,
    /* .pfnFinalDestruct = */       NULL,
    /* .pfnRequest = */             NULL,
    /* .pfnReserved0 = */           NULL,
    /* .pfnReserved1 = */           NULL,
    /* .pfnReserved2 = */           NULL,
    /* .pfnReserved3 = */           NULL,
    /* .pfnReserved4 = */           NULL,
    /* .pfnReserved5 = */           NULL,
    /* .pfnReserved6 = */           NULL,
    /* .pfnReserved7 = */           NULL,
#elif defined(IN_RC)
    /* .pfnConstruct = */           NULL,
    /* .pfnReserved0 = */           NULL,
    /* .pfnReserved1 = */           NULL,
    /* .pfnReserved2 = */           NULL,
    /* .pfnReserved3 = */           NULL,
    /* .pfnReserved4 = */           NULL,
    /* .pfnReserved5 = */           NULL,
    /* .pfnReserved6 = */           NULL,
    /* .pfnReserved7 = */           NULL,
#else
# error "Not in IN_RING3, IN_RING0 or IN_RC!"
#endif
    /* .u32VersionEnd = */          PDM_DEVREG_VERSION
};