VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/ConsoleImpl2.cpp@ 65340

Last change on this file since 65340 was 65172, checked in by vboxsync, 8 years ago

Audio/Main: Added a @todo.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 253.6 KB
Line 
1/* $Id: ConsoleImpl2.cpp 65172 2017-01-06 09:58:18Z vboxsync $ */
2/** @file
3 * VBox Console COM Class implementation - VM Configuration Bits.
4 *
5 * @remark We've split out the code that the 64-bit VC++ v8 compiler finds
6 * problematic to optimize so we can disable optimizations and later,
7 * perhaps, find a real solution for it (like rewriting the code and
8 * to stop resemble a tonne of spaghetti).
9 */
10
11/*
12 * Copyright (C) 2006-2017 Oracle Corporation
13 *
14 * This file is part of VirtualBox Open Source Edition (OSE), as
15 * available from http://www.virtualbox.org. This file is free software;
16 * you can redistribute it and/or modify it under the terms of the GNU
17 * General Public License (GPL) as published by the Free Software
18 * Foundation, in version 2 as it comes in the "COPYING" file of the
19 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
20 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
21 */
22
23
24/*********************************************************************************************************************************
25* Header Files *
26*********************************************************************************************************************************/
27
28// VBoxNetCfg-win.h needs winsock2.h and thus MUST be included before any other
29// header file includes Windows.h.
30#if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT)
31# include <VBox/VBoxNetCfg-win.h>
32#endif
33
34#include "ConsoleImpl.h"
35#include "DisplayImpl.h"
36#ifdef VBOX_WITH_GUEST_CONTROL
37# include "GuestImpl.h"
38#endif
39#ifdef VBOX_WITH_DRAG_AND_DROP
40# include "GuestDnDPrivate.h"
41#endif
42#include "VMMDev.h"
43#include "Global.h"
44#ifdef VBOX_WITH_PCI_PASSTHROUGH
45# include "PCIRawDevImpl.h"
46#endif
47
48// generated header
49#include "SchemaDefs.h"
50
51#include "AutoCaller.h"
52#include "Logging.h"
53
54#include <iprt/base64.h>
55#include <iprt/buildconfig.h>
56#include <iprt/ctype.h>
57#include <iprt/dir.h>
58#include <iprt/file.h>
59#include <iprt/param.h>
60#include <iprt/path.h>
61#include <iprt/string.h>
62#include <iprt/system.h>
63#include <iprt/cpp/exception.h>
64#if 0 /* enable to play with lots of memory. */
65# include <iprt/env.h>
66#endif
67#include <iprt/stream.h>
68
69#include <VBox/vmm/vmapi.h>
70#include <VBox/err.h>
71#include <VBox/param.h>
72#include <VBox/vmm/pdmapi.h> /* For PDMR3DriverAttach/PDMR3DriverDetach. */
73#include <VBox/vmm/pdmusb.h> /* For PDMR3UsbCreateEmulatedDevice. */
74#include <VBox/vmm/apic.h> /* For APICMODE enum. */
75#include <VBox/vmm/pdmstorageifs.h>
76#include <VBox/version.h>
77#include <VBox/HostServices/VBoxClipboardSvc.h>
78#ifdef VBOX_WITH_CROGL
79# include <VBox/HostServices/VBoxCrOpenGLSvc.h>
80#include <VBox/VBoxOGL.h>
81#endif
82#ifdef VBOX_WITH_GUEST_PROPS
83# include <VBox/HostServices/GuestPropertySvc.h>
84# include <VBox/com/defs.h>
85# include <VBox/com/array.h>
86# include "HGCM.h" /** @todo It should be possible to register a service
87 * extension using a VMMDev callback. */
88# include <vector>
89#endif /* VBOX_WITH_GUEST_PROPS */
90#include <VBox/intnet.h>
91
92#include <VBox/com/com.h>
93#include <VBox/com/string.h>
94#include <VBox/com/array.h>
95
96#ifdef VBOX_WITH_NETFLT
97# if defined(RT_OS_SOLARIS)
98# include <zone.h>
99# elif defined(RT_OS_LINUX)
100# include <unistd.h>
101# include <sys/ioctl.h>
102# include <sys/socket.h>
103# include <linux/types.h>
104# include <linux/if.h>
105# include <linux/wireless.h>
106# elif defined(RT_OS_FREEBSD)
107# include <unistd.h>
108# include <sys/types.h>
109# include <sys/ioctl.h>
110# include <sys/socket.h>
111# include <net/if.h>
112# include <net80211/ieee80211_ioctl.h>
113# endif
114# if defined(RT_OS_WINDOWS)
115# include <iprt/win/ntddndis.h>
116# include <devguid.h>
117# else
118# include <HostNetworkInterfaceImpl.h>
119# include <netif.h>
120# include <stdlib.h>
121# endif
122#endif /* VBOX_WITH_NETFLT */
123
124#include "NetworkServiceRunner.h"
125#include "BusAssignmentManager.h"
126#ifdef VBOX_WITH_EXTPACK
127# include "ExtPackManagerImpl.h"
128#endif
129
130
131/*********************************************************************************************************************************
132* Internal Functions *
133*********************************************************************************************************************************/
134static Utf8Str *GetExtraDataBoth(IVirtualBox *pVirtualBox, IMachine *pMachine, const char *pszName, Utf8Str *pStrValue);
135
136
137/* Darwin compile kludge */
138#undef PVM
139
140/* Comment out the following line to remove VMWare compatibility hack. */
141#define VMWARE_NET_IN_SLOT_11
142
143/**
144 * Translate IDE StorageControllerType_T to string representation.
145 */
146const char* controllerString(StorageControllerType_T enmType)
147{
148 switch (enmType)
149 {
150 case StorageControllerType_PIIX3:
151 return "PIIX3";
152 case StorageControllerType_PIIX4:
153 return "PIIX4";
154 case StorageControllerType_ICH6:
155 return "ICH6";
156 default:
157 return "Unknown";
158 }
159}
160
161/**
162 * Simple class for storing network boot information.
163 */
164struct BootNic
165{
166 ULONG mInstance;
167 PCIBusAddress mPCIAddress;
168
169 ULONG mBootPrio;
170 bool operator < (const BootNic &rhs) const
171 {
172 ULONG lval = mBootPrio - 1; /* 0 will wrap around and get the lowest priority. */
173 ULONG rval = rhs.mBootPrio - 1;
174 return lval < rval; /* Zero compares as highest number (lowest prio). */
175 }
176};
177
178static int findEfiRom(IVirtualBox* vbox, FirmwareType_T aFirmwareType, Utf8Str *pEfiRomFile)
179{
180 Bstr aFilePath, empty;
181 BOOL fPresent = FALSE;
182 HRESULT hrc = vbox->CheckFirmwarePresent(aFirmwareType, empty.raw(),
183 empty.asOutParam(), aFilePath.asOutParam(), &fPresent);
184 AssertComRCReturn(hrc, Global::vboxStatusCodeFromCOM(hrc));
185
186 if (!fPresent)
187 {
188 LogRel(("Failed to find an EFI ROM file.\n"));
189 return VERR_FILE_NOT_FOUND;
190 }
191
192 *pEfiRomFile = Utf8Str(aFilePath);
193
194 return VINF_SUCCESS;
195}
196
197/**
198 * @throws HRESULT on extra data retrival error.
199 */
200static int getSmcDeviceKey(IVirtualBox *pVirtualBox, IMachine *pMachine, Utf8Str *pStrKey, bool *pfGetKeyFromRealSMC)
201{
202 *pfGetKeyFromRealSMC = false;
203
204 /*
205 * The extra data takes precedence (if non-zero).
206 */
207 GetExtraDataBoth(pVirtualBox, pMachine, "VBoxInternal2/SmcDeviceKey", pStrKey);
208 if (pStrKey->isNotEmpty())
209 return VINF_SUCCESS;
210
211#ifdef RT_OS_DARWIN
212
213 /*
214 * Work done in EFI/DevSmc
215 */
216 *pfGetKeyFromRealSMC = true;
217 int rc = VINF_SUCCESS;
218
219#else
220 /*
221 * Is it apple hardware in bootcamp?
222 */
223 /** @todo implement + test RTSYSDMISTR_MANUFACTURER on all hosts.
224 * Currently falling back on the product name. */
225 char szManufacturer[256];
226 szManufacturer[0] = '\0';
227 RTSystemQueryDmiString(RTSYSDMISTR_MANUFACTURER, szManufacturer, sizeof(szManufacturer));
228 if (szManufacturer[0] != '\0')
229 {
230 if ( !strcmp(szManufacturer, "Apple Computer, Inc.")
231 || !strcmp(szManufacturer, "Apple Inc.")
232 )
233 *pfGetKeyFromRealSMC = true;
234 }
235 else
236 {
237 char szProdName[256];
238 szProdName[0] = '\0';
239 RTSystemQueryDmiString(RTSYSDMISTR_PRODUCT_NAME, szProdName, sizeof(szProdName));
240 if ( ( !strncmp(szProdName, RT_STR_TUPLE("Mac"))
241 || !strncmp(szProdName, RT_STR_TUPLE("iMac"))
242 || !strncmp(szProdName, RT_STR_TUPLE("iMac"))
243 || !strncmp(szProdName, RT_STR_TUPLE("Xserve"))
244 )
245 && !strchr(szProdName, ' ') /* no spaces */
246 && RT_C_IS_DIGIT(szProdName[strlen(szProdName) - 1]) /* version number */
247 )
248 *pfGetKeyFromRealSMC = true;
249 }
250
251 int rc = VINF_SUCCESS;
252#endif
253
254 return rc;
255}
256
257
258/*
259 * VC++ 8 / amd64 has some serious trouble with the next functions.
260 * As a temporary measure, we'll drop global optimizations.
261 */
262#if defined(_MSC_VER) && defined(RT_ARCH_AMD64)
263# if _MSC_VER >= RT_MSC_VER_VC80 && _MSC_VER < RT_MSC_VER_VC100
264# pragma optimize("g", off)
265# endif
266#endif
267
268class ConfigError : public RTCError
269{
270public:
271
272 ConfigError(const char *pcszFunction,
273 int vrc,
274 const char *pcszName)
275 : RTCError(Utf8StrFmt("%s failed: rc=%Rrc, pcszName=%s", pcszFunction, vrc, pcszName)),
276 m_vrc(vrc)
277 {
278 AssertMsgFailed(("%s\n", what())); // in strict mode, hit a breakpoint here
279 }
280
281 int m_vrc;
282};
283
284
285/**
286 * Helper that calls CFGMR3InsertString and throws an RTCError if that
287 * fails (C-string variant).
288 * @param pNode See CFGMR3InsertStringN.
289 * @param pcszName See CFGMR3InsertStringN.
290 * @param pcszValue The string value.
291 */
292static void InsertConfigString(PCFGMNODE pNode,
293 const char *pcszName,
294 const char *pcszValue)
295{
296 int vrc = CFGMR3InsertString(pNode,
297 pcszName,
298 pcszValue);
299 if (RT_FAILURE(vrc))
300 throw ConfigError("CFGMR3InsertString", vrc, pcszName);
301}
302
303/**
304 * Helper that calls CFGMR3InsertString and throws an RTCError if that
305 * fails (Utf8Str variant).
306 * @param pNode See CFGMR3InsertStringN.
307 * @param pcszName See CFGMR3InsertStringN.
308 * @param rStrValue The string value.
309 */
310static void InsertConfigString(PCFGMNODE pNode,
311 const char *pcszName,
312 const Utf8Str &rStrValue)
313{
314 int vrc = CFGMR3InsertStringN(pNode,
315 pcszName,
316 rStrValue.c_str(),
317 rStrValue.length());
318 if (RT_FAILURE(vrc))
319 throw ConfigError("CFGMR3InsertStringLengthKnown", vrc, pcszName);
320}
321
322/**
323 * Helper that calls CFGMR3InsertString and throws an RTCError if that
324 * fails (Bstr variant).
325 *
326 * @param pNode See CFGMR3InsertStringN.
327 * @param pcszName See CFGMR3InsertStringN.
328 * @param rBstrValue The string value.
329 */
330static void InsertConfigString(PCFGMNODE pNode,
331 const char *pcszName,
332 const Bstr &rBstrValue)
333{
334 InsertConfigString(pNode, pcszName, Utf8Str(rBstrValue));
335}
336
337/**
338 * Helper that calls CFGMR3InsertBytes and throws an RTCError if that fails.
339 *
340 * @param pNode See CFGMR3InsertBytes.
341 * @param pcszName See CFGMR3InsertBytes.
342 * @param pvBytes See CFGMR3InsertBytes.
343 * @param cbBytes See CFGMR3InsertBytes.
344 */
345static void InsertConfigBytes(PCFGMNODE pNode,
346 const char *pcszName,
347 const void *pvBytes,
348 size_t cbBytes)
349{
350 int vrc = CFGMR3InsertBytes(pNode,
351 pcszName,
352 pvBytes,
353 cbBytes);
354 if (RT_FAILURE(vrc))
355 throw ConfigError("CFGMR3InsertBytes", vrc, pcszName);
356}
357
358/**
359 * Helper that calls CFGMR3InsertInteger and throws an RTCError if that
360 * fails.
361 *
362 * @param pNode See CFGMR3InsertInteger.
363 * @param pcszName See CFGMR3InsertInteger.
364 * @param u64Integer See CFGMR3InsertInteger.
365 */
366static void InsertConfigInteger(PCFGMNODE pNode,
367 const char *pcszName,
368 uint64_t u64Integer)
369{
370 int vrc = CFGMR3InsertInteger(pNode,
371 pcszName,
372 u64Integer);
373 if (RT_FAILURE(vrc))
374 throw ConfigError("CFGMR3InsertInteger", vrc, pcszName);
375}
376
377/**
378 * Helper that calls CFGMR3InsertNode and throws an RTCError if that fails.
379 *
380 * @param pNode See CFGMR3InsertNode.
381 * @param pcszName See CFGMR3InsertNode.
382 * @param ppChild See CFGMR3InsertNode.
383 */
384static void InsertConfigNode(PCFGMNODE pNode,
385 const char *pcszName,
386 PCFGMNODE *ppChild)
387{
388 int vrc = CFGMR3InsertNode(pNode, pcszName, ppChild);
389 if (RT_FAILURE(vrc))
390 throw ConfigError("CFGMR3InsertNode", vrc, pcszName);
391}
392
393/**
394 * Helper that calls CFGMR3RemoveValue and throws an RTCError if that fails.
395 *
396 * @param pNode See CFGMR3RemoveValue.
397 * @param pcszName See CFGMR3RemoveValue.
398 */
399static void RemoveConfigValue(PCFGMNODE pNode,
400 const char *pcszName)
401{
402 int vrc = CFGMR3RemoveValue(pNode, pcszName);
403 if (RT_FAILURE(vrc))
404 throw ConfigError("CFGMR3RemoveValue", vrc, pcszName);
405}
406
407/**
408 * Gets an extra data value, consulting both machine and global extra data.
409 *
410 * @throws HRESULT on failure
411 * @returns pStrValue for the callers convenience.
412 * @param pVirtualBox Pointer to the IVirtualBox interface.
413 * @param pMachine Pointer to the IMachine interface.
414 * @param pszName The value to get.
415 * @param pStrValue Where to return it's value (empty string if not
416 * found).
417 */
418static Utf8Str *GetExtraDataBoth(IVirtualBox *pVirtualBox, IMachine *pMachine, const char *pszName, Utf8Str *pStrValue)
419{
420 pStrValue->setNull();
421
422 Bstr bstrName(pszName);
423 Bstr bstrValue;
424 HRESULT hrc = pMachine->GetExtraData(bstrName.raw(), bstrValue.asOutParam());
425 if (FAILED(hrc))
426 throw hrc;
427 if (bstrValue.isEmpty())
428 {
429 hrc = pVirtualBox->GetExtraData(bstrName.raw(), bstrValue.asOutParam());
430 if (FAILED(hrc))
431 throw hrc;
432 }
433
434 if (bstrValue.isNotEmpty())
435 *pStrValue = bstrValue;
436 return pStrValue;
437}
438
439
440/** Helper that finds out the next HBA port used
441 */
442static LONG GetNextUsedPort(LONG aPortUsed[30], LONG lBaseVal, uint32_t u32Size)
443{
444 LONG lNextPortUsed = 30;
445 for (size_t j = 0; j < u32Size; ++j)
446 {
447 if ( aPortUsed[j] > lBaseVal
448 && aPortUsed[j] <= lNextPortUsed)
449 lNextPortUsed = aPortUsed[j];
450 }
451 return lNextPortUsed;
452}
453
454#define MAX_BIOS_LUN_COUNT 4
455
456static int SetBiosDiskInfo(ComPtr<IMachine> pMachine, PCFGMNODE pCfg, PCFGMNODE pBiosCfg,
457 Bstr controllerName, const char * const s_apszBiosConfig[4])
458{
459 RT_NOREF(pCfg);
460 HRESULT hrc;
461#define MAX_DEVICES 30
462#define H() AssertLogRelMsgReturn(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), VERR_MAIN_CONFIG_CONSTRUCTOR_COM_ERROR)
463
464 LONG lPortLUN[MAX_BIOS_LUN_COUNT];
465 LONG lPortUsed[MAX_DEVICES];
466 uint32_t u32HDCount = 0;
467
468 /* init to max value */
469 lPortLUN[0] = MAX_DEVICES;
470
471 com::SafeIfaceArray<IMediumAttachment> atts;
472 hrc = pMachine->GetMediumAttachmentsOfController(controllerName.raw(),
473 ComSafeArrayAsOutParam(atts)); H();
474 size_t uNumAttachments = atts.size();
475 if (uNumAttachments > MAX_DEVICES)
476 {
477 LogRel(("Number of Attachments > Max=%d.\n", uNumAttachments));
478 uNumAttachments = MAX_DEVICES;
479 }
480
481 /* Find the relevant ports/IDs, i.e the ones to which a HD is attached. */
482 for (size_t j = 0; j < uNumAttachments; ++j)
483 {
484 IMediumAttachment *pMediumAtt = atts[j];
485 LONG lPortNum = 0;
486 hrc = pMediumAtt->COMGETTER(Port)(&lPortNum); H();
487 if (SUCCEEDED(hrc))
488 {
489 DeviceType_T lType;
490 hrc = pMediumAtt->COMGETTER(Type)(&lType); H();
491 if (SUCCEEDED(hrc) && lType == DeviceType_HardDisk)
492 {
493 /* find min port number used for HD */
494 if (lPortNum < lPortLUN[0])
495 lPortLUN[0] = lPortNum;
496 lPortUsed[u32HDCount++] = lPortNum;
497 LogFlowFunc(("HD port Count=%d\n", u32HDCount));
498 }
499 }
500 }
501
502
503 /* Pick only the top 4 used HD Ports as CMOS doesn't have space
504 * to save details for all 30 ports
505 */
506 uint32_t u32MaxPortCount = MAX_BIOS_LUN_COUNT;
507 if (u32HDCount < MAX_BIOS_LUN_COUNT)
508 u32MaxPortCount = u32HDCount;
509 for (size_t j = 1; j < u32MaxPortCount; j++)
510 lPortLUN[j] = GetNextUsedPort(lPortUsed,
511 lPortLUN[j-1],
512 u32HDCount);
513 if (pBiosCfg)
514 {
515 for (size_t j = 0; j < u32MaxPortCount; j++)
516 {
517 InsertConfigInteger(pBiosCfg, s_apszBiosConfig[j], lPortLUN[j]);
518 LogFlowFunc(("Top %d HBA ports = %s, %d\n", j, s_apszBiosConfig[j], lPortLUN[j]));
519 }
520 }
521 return VINF_SUCCESS;
522}
523
524#ifdef VBOX_WITH_PCI_PASSTHROUGH
525HRESULT Console::i_attachRawPCIDevices(PUVM pUVM, BusAssignmentManager *pBusMgr, PCFGMNODE pDevices)
526{
527# ifndef VBOX_WITH_EXTPACK
528 RT_NOREF(pUVM);
529# endif
530 HRESULT hrc = S_OK;
531 PCFGMNODE pInst, pCfg, pLunL0, pLunL1;
532
533 SafeIfaceArray<IPCIDeviceAttachment> assignments;
534 ComPtr<IMachine> aMachine = i_machine();
535
536 hrc = aMachine->COMGETTER(PCIDeviceAssignments)(ComSafeArrayAsOutParam(assignments));
537 if ( hrc != S_OK
538 || assignments.size() < 1)
539 return hrc;
540
541 /*
542 * PCI passthrough is only available if the proper ExtPack is installed.
543 *
544 * Note. Configuring PCI passthrough here and providing messages about
545 * the missing extpack isn't exactly clean, but it is a necessary evil
546 * to patch over legacy compatability issues introduced by the new
547 * distribution model.
548 */
549# ifdef VBOX_WITH_EXTPACK
550 static const char *s_pszPCIRawExtPackName = "Oracle VM VirtualBox Extension Pack";
551 if (!mptrExtPackManager->i_isExtPackUsable(s_pszPCIRawExtPackName))
552 /* Always fatal! */
553 return VMR3SetError(pUVM, VERR_NOT_FOUND, RT_SRC_POS,
554 N_("Implementation of the PCI passthrough framework not found!\n"
555 "The VM cannot be started. To fix this problem, either "
556 "install the '%s' or disable PCI passthrough via VBoxManage"),
557 s_pszPCIRawExtPackName);
558# endif
559
560 PCFGMNODE pBridges = CFGMR3GetChild(pDevices, "ich9pcibridge");
561 Assert(pBridges);
562
563 /* Find required bridges, and add missing ones */
564 for (size_t iDev = 0; iDev < assignments.size(); iDev++)
565 {
566 ComPtr<IPCIDeviceAttachment> assignment = assignments[iDev];
567 LONG guest = 0;
568 PCIBusAddress GuestPCIAddress;
569
570 hrc = assignment->COMGETTER(GuestAddress)(&guest); H();
571 GuestPCIAddress.fromLong(guest);
572 Assert(GuestPCIAddress.valid());
573
574 if (GuestPCIAddress.miBus > 0)
575 {
576 int iBridgesMissed = 0;
577 int iBase = GuestPCIAddress.miBus - 1;
578
579 while (!pBusMgr->hasPCIDevice("ich9pcibridge", iBase) && iBase > 0)
580 {
581 iBridgesMissed++; iBase--;
582 }
583 iBase++;
584
585 for (int iBridge = 0; iBridge < iBridgesMissed; iBridge++)
586 {
587 InsertConfigNode(pBridges, Utf8StrFmt("%d", iBase + iBridge).c_str(), &pInst);
588 InsertConfigInteger(pInst, "Trusted", 1);
589 hrc = pBusMgr->assignPCIDevice("ich9pcibridge", pInst);
590 }
591 }
592 }
593
594 /* Now actually add devices */
595 PCFGMNODE pPCIDevs = NULL;
596
597 if (assignments.size() > 0)
598 {
599 InsertConfigNode(pDevices, "pciraw", &pPCIDevs);
600
601 PCFGMNODE pRoot = CFGMR3GetParent(pDevices); Assert(pRoot);
602
603 /* Tell PGM to tell GPCIRaw about guest mappings. */
604 CFGMR3InsertNode(pRoot, "PGM", NULL);
605 InsertConfigInteger(CFGMR3GetChild(pRoot, "PGM"), "PciPassThrough", 1);
606
607 /*
608 * Currently, using IOMMU needed for PCI passthrough
609 * requires RAM preallocation.
610 */
611 /** @todo check if we can lift this requirement */
612 CFGMR3RemoveValue(pRoot, "RamPreAlloc");
613 InsertConfigInteger(pRoot, "RamPreAlloc", 1);
614 }
615
616 for (size_t iDev = 0; iDev < assignments.size(); iDev++)
617 {
618 PCIBusAddress HostPCIAddress, GuestPCIAddress;
619 ComPtr<IPCIDeviceAttachment> assignment = assignments[iDev];
620 LONG host, guest;
621 Bstr aDevName;
622
623 hrc = assignment->COMGETTER(HostAddress)(&host); H();
624 hrc = assignment->COMGETTER(GuestAddress)(&guest); H();
625 hrc = assignment->COMGETTER(Name)(aDevName.asOutParam()); H();
626
627 InsertConfigNode(pPCIDevs, Utf8StrFmt("%d", iDev).c_str(), &pInst);
628 InsertConfigInteger(pInst, "Trusted", 1);
629
630 HostPCIAddress.fromLong(host);
631 Assert(HostPCIAddress.valid());
632 InsertConfigNode(pInst, "Config", &pCfg);
633 InsertConfigString(pCfg, "DeviceName", aDevName);
634
635 InsertConfigInteger(pCfg, "DetachHostDriver", 1);
636 InsertConfigInteger(pCfg, "HostPCIBusNo", HostPCIAddress.miBus);
637 InsertConfigInteger(pCfg, "HostPCIDeviceNo", HostPCIAddress.miDevice);
638 InsertConfigInteger(pCfg, "HostPCIFunctionNo", HostPCIAddress.miFn);
639
640 GuestPCIAddress.fromLong(guest);
641 Assert(GuestPCIAddress.valid());
642 hrc = pBusMgr->assignHostPCIDevice("pciraw", pInst, HostPCIAddress, GuestPCIAddress, true);
643 if (hrc != S_OK)
644 return hrc;
645
646 InsertConfigInteger(pCfg, "GuestPCIBusNo", GuestPCIAddress.miBus);
647 InsertConfigInteger(pCfg, "GuestPCIDeviceNo", GuestPCIAddress.miDevice);
648 InsertConfigInteger(pCfg, "GuestPCIFunctionNo", GuestPCIAddress.miFn);
649
650 /* the driver */
651 InsertConfigNode(pInst, "LUN#0", &pLunL0);
652 InsertConfigString(pLunL0, "Driver", "pciraw");
653 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
654
655 /* the Main driver */
656 InsertConfigString(pLunL1, "Driver", "MainPciRaw");
657 InsertConfigNode(pLunL1, "Config", &pCfg);
658 PCIRawDev* pMainDev = new PCIRawDev(this);
659 InsertConfigInteger(pCfg, "Object", (uintptr_t)pMainDev);
660 }
661
662 return hrc;
663}
664#endif
665
666
667void Console::i_attachStatusDriver(PCFGMNODE pCtlInst, PPDMLED *papLeds,
668 uint64_t uFirst, uint64_t uLast,
669 Console::MediumAttachmentMap *pmapMediumAttachments,
670 const char *pcszDevice, unsigned uInstance)
671{
672 PCFGMNODE pLunL0, pCfg;
673 InsertConfigNode(pCtlInst, "LUN#999", &pLunL0);
674 InsertConfigString(pLunL0, "Driver", "MainStatus");
675 InsertConfigNode(pLunL0, "Config", &pCfg);
676 InsertConfigInteger(pCfg, "papLeds", (uintptr_t)papLeds);
677 if (pmapMediumAttachments)
678 {
679 InsertConfigInteger(pCfg, "pmapMediumAttachments", (uintptr_t)pmapMediumAttachments);
680 InsertConfigInteger(pCfg, "pConsole", (uintptr_t)this);
681 AssertPtr(pcszDevice);
682 Utf8Str deviceInstance = Utf8StrFmt("%s/%u", pcszDevice, uInstance);
683 InsertConfigString(pCfg, "DeviceInstance", deviceInstance.c_str());
684 }
685 InsertConfigInteger(pCfg, "First", uFirst);
686 InsertConfigInteger(pCfg, "Last", uLast);
687}
688
689
690/**
691 * Construct the VM configuration tree (CFGM).
692 *
693 * This is a callback for VMR3Create() call. It is called from CFGMR3Init()
694 * in the emulation thread (EMT). Any per thread COM/XPCOM initialization
695 * is done here.
696 *
697 * @param pUVM The user mode VM handle.
698 * @param pVM The cross context VM handle.
699 * @param pvConsole Pointer to the VMPowerUpTask object.
700 * @return VBox status code.
701 *
702 * @note Locks the Console object for writing.
703 */
704DECLCALLBACK(int) Console::i_configConstructor(PUVM pUVM, PVM pVM, void *pvConsole)
705{
706 LogFlowFuncEnter();
707
708 AssertReturn(pvConsole, VERR_INVALID_POINTER);
709 ComObjPtr<Console> pConsole = static_cast<Console *>(pvConsole);
710
711 AutoCaller autoCaller(pConsole);
712 AssertComRCReturn(autoCaller.rc(), VERR_ACCESS_DENIED);
713
714 /* lock the console because we widely use internal fields and methods */
715 AutoWriteLock alock(pConsole COMMA_LOCKVAL_SRC_POS);
716
717 /*
718 * Set the VM handle and do the rest of the job in an worker method so we
719 * can easily reset the VM handle on failure.
720 */
721 pConsole->mpUVM = pUVM;
722 VMR3RetainUVM(pUVM);
723 int vrc;
724 try
725 {
726 vrc = pConsole->i_configConstructorInner(pUVM, pVM, &alock);
727 }
728 catch (...)
729 {
730 vrc = VERR_UNEXPECTED_EXCEPTION;
731 }
732 if (RT_FAILURE(vrc))
733 {
734 pConsole->mpUVM = NULL;
735 VMR3ReleaseUVM(pUVM);
736 }
737
738 return vrc;
739}
740
741
742/**
743 * Worker for configConstructor.
744 *
745 * @return VBox status code.
746 * @param pUVM The user mode VM handle.
747 * @param pVM The cross context VM handle.
748 * @param pAlock The automatic lock instance. This is for when we have
749 * to leave it in order to avoid deadlocks (ext packs and
750 * more).
751 */
752int Console::i_configConstructorInner(PUVM pUVM, PVM pVM, AutoWriteLock *pAlock)
753{
754 RT_NOREF(pVM /* when everything is disabled */);
755 VMMDev *pVMMDev = m_pVMMDev; Assert(pVMMDev);
756 ComPtr<IMachine> pMachine = i_machine();
757
758 int rc;
759 HRESULT hrc;
760 Utf8Str strTmp;
761 Bstr bstr;
762
763#define H() AssertLogRelMsgReturn(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), VERR_MAIN_CONFIG_CONSTRUCTOR_COM_ERROR)
764
765 /*
766 * Get necessary objects and frequently used parameters.
767 */
768 ComPtr<IVirtualBox> virtualBox;
769 hrc = pMachine->COMGETTER(Parent)(virtualBox.asOutParam()); H();
770
771 ComPtr<IHost> host;
772 hrc = virtualBox->COMGETTER(Host)(host.asOutParam()); H();
773
774 ComPtr<ISystemProperties> systemProperties;
775 hrc = virtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam()); H();
776
777 ComPtr<IBIOSSettings> biosSettings;
778 hrc = pMachine->COMGETTER(BIOSSettings)(biosSettings.asOutParam()); H();
779
780 hrc = pMachine->COMGETTER(HardwareUUID)(bstr.asOutParam()); H();
781 RTUUID HardwareUuid;
782 rc = RTUuidFromUtf16(&HardwareUuid, bstr.raw());
783 AssertRCReturn(rc, rc);
784
785 ULONG cRamMBs;
786 hrc = pMachine->COMGETTER(MemorySize)(&cRamMBs); H();
787#if 0 /* enable to play with lots of memory. */
788 if (RTEnvExist("VBOX_RAM_SIZE"))
789 cRamMBs = RTStrToUInt64(RTEnvGet("VBOX_RAM_SIZE"));
790#endif
791 uint64_t const cbRam = cRamMBs * (uint64_t)_1M;
792 uint32_t cbRamHole = MM_RAM_HOLE_SIZE_DEFAULT;
793 uint64_t uMcfgBase = 0;
794 uint32_t cbMcfgLength = 0;
795
796 ParavirtProvider_T paravirtProvider;
797 hrc = pMachine->GetEffectiveParavirtProvider(&paravirtProvider); H();
798
799 Bstr strParavirtDebug;
800 hrc = pMachine->COMGETTER(ParavirtDebug)(strParavirtDebug.asOutParam()); H();
801
802 ChipsetType_T chipsetType;
803 hrc = pMachine->COMGETTER(ChipsetType)(&chipsetType); H();
804 if (chipsetType == ChipsetType_ICH9)
805 {
806 /* We'd better have 0x10000000 region, to cover 256 buses
807 but this put too much load on hypervisor heap */
808 cbMcfgLength = 0x4000000; //0x10000000;
809 cbRamHole += cbMcfgLength;
810 uMcfgBase = _4G - cbRamHole;
811 }
812
813 BusAssignmentManager *pBusMgr = mBusMgr = BusAssignmentManager::createInstance(chipsetType);
814
815 ULONG cCpus = 1;
816 hrc = pMachine->COMGETTER(CPUCount)(&cCpus); H();
817
818 ULONG ulCpuExecutionCap = 100;
819 hrc = pMachine->COMGETTER(CPUExecutionCap)(&ulCpuExecutionCap); H();
820
821 Bstr osTypeId;
822 hrc = pMachine->COMGETTER(OSTypeId)(osTypeId.asOutParam()); H();
823 LogRel(("Guest OS type: '%s'\n", Utf8Str(osTypeId).c_str()));
824
825 BOOL fIOAPIC;
826 hrc = biosSettings->COMGETTER(IOAPICEnabled)(&fIOAPIC); H();
827
828 APICMode_T apicMode;
829 hrc = biosSettings->COMGETTER(APICMode)(&apicMode); H();
830 uint32_t uFwAPIC;
831 switch (apicMode)
832 {
833 case APICMode_Disabled:
834 uFwAPIC = 0;
835 break;
836 case APICMode_APIC:
837 uFwAPIC = 1;
838 break;
839 case APICMode_X2APIC:
840 uFwAPIC = 2;
841 break;
842 default:
843 AssertMsgFailed(("Invalid APICMode=%d\n", apicMode));
844 uFwAPIC = 1;
845 break;
846 }
847
848 ComPtr<IGuestOSType> guestOSType;
849 hrc = virtualBox->GetGuestOSType(osTypeId.raw(), guestOSType.asOutParam()); H();
850
851 Bstr guestTypeFamilyId;
852 hrc = guestOSType->COMGETTER(FamilyId)(guestTypeFamilyId.asOutParam()); H();
853 BOOL fOsXGuest = guestTypeFamilyId == Bstr("MacOS");
854
855 ULONG maxNetworkAdapters;
856 hrc = systemProperties->GetMaxNetworkAdapters(chipsetType, &maxNetworkAdapters); H();
857
858 /*
859 * Get root node first.
860 * This is the only node in the tree.
861 */
862 PCFGMNODE pRoot = CFGMR3GetRootU(pUVM);
863 Assert(pRoot);
864
865 // InsertConfigString throws
866 try
867 {
868
869 /*
870 * Set the root (and VMM) level values.
871 */
872 hrc = pMachine->COMGETTER(Name)(bstr.asOutParam()); H();
873 InsertConfigString(pRoot, "Name", bstr);
874 InsertConfigBytes(pRoot, "UUID", &HardwareUuid, sizeof(HardwareUuid));
875 InsertConfigInteger(pRoot, "RamSize", cbRam);
876 InsertConfigInteger(pRoot, "RamHoleSize", cbRamHole);
877 InsertConfigInteger(pRoot, "NumCPUs", cCpus);
878 InsertConfigInteger(pRoot, "CpuExecutionCap", ulCpuExecutionCap);
879 InsertConfigInteger(pRoot, "TimerMillies", 10);
880#ifdef VBOX_WITH_RAW_MODE
881 InsertConfigInteger(pRoot, "RawR3Enabled", 1); /* boolean */
882 InsertConfigInteger(pRoot, "RawR0Enabled", 1); /* boolean */
883 /** @todo Config: RawR0, PATMEnabled and CSAMEnabled needs attention later. */
884 InsertConfigInteger(pRoot, "PATMEnabled", 1); /* boolean */
885 InsertConfigInteger(pRoot, "CSAMEnabled", 1); /* boolean */
886#endif
887
888#ifdef VBOX_WITH_RAW_RING1
889 if (osTypeId == "QNX")
890 {
891 /* QNX needs special treatment in raw mode due to its use of ring-1. */
892 InsertConfigInteger(pRoot, "RawR1Enabled", 1); /* boolean */
893 }
894#endif
895
896 BOOL fPageFusion = FALSE;
897 hrc = pMachine->COMGETTER(PageFusionEnabled)(&fPageFusion); H();
898 InsertConfigInteger(pRoot, "PageFusionAllowed", fPageFusion); /* boolean */
899
900 /* Not necessary, but makes sure this setting ends up in the release log. */
901 ULONG ulBalloonSize = 0;
902 hrc = pMachine->COMGETTER(MemoryBalloonSize)(&ulBalloonSize); H();
903 InsertConfigInteger(pRoot, "MemBalloonSize", ulBalloonSize);
904
905 /*
906 * EM values (before CPUM as it may need to set IemExecutesAll).
907 */
908 PCFGMNODE pEM;
909 InsertConfigNode(pRoot, "EM", &pEM);
910
911 /* Triple fault behavior. */
912 BOOL fTripleFaultReset = false;
913 hrc = pMachine->GetCPUProperty(CPUPropertyType_TripleFaultReset, &fTripleFaultReset); H();
914 InsertConfigInteger(pEM, "TripleFaultReset", fTripleFaultReset);
915
916 /*
917 * CPUM values.
918 */
919 PCFGMNODE pCPUM;
920 InsertConfigNode(pRoot, "CPUM", &pCPUM);
921
922 /* cpuid leaf overrides. */
923 static uint32_t const s_auCpuIdRanges[] =
924 {
925 UINT32_C(0x00000000), UINT32_C(0x0000000a),
926 UINT32_C(0x80000000), UINT32_C(0x8000000a)
927 };
928 for (unsigned i = 0; i < RT_ELEMENTS(s_auCpuIdRanges); i += 2)
929 for (uint32_t uLeaf = s_auCpuIdRanges[i]; uLeaf < s_auCpuIdRanges[i + 1]; uLeaf++)
930 {
931 ULONG ulEax, ulEbx, ulEcx, ulEdx;
932 hrc = pMachine->GetCPUIDLeaf(uLeaf, &ulEax, &ulEbx, &ulEcx, &ulEdx);
933 if (SUCCEEDED(hrc))
934 {
935 PCFGMNODE pLeaf;
936 InsertConfigNode(pCPUM, Utf8StrFmt("HostCPUID/%RX32", uLeaf).c_str(), &pLeaf);
937
938 InsertConfigInteger(pLeaf, "eax", ulEax);
939 InsertConfigInteger(pLeaf, "ebx", ulEbx);
940 InsertConfigInteger(pLeaf, "ecx", ulEcx);
941 InsertConfigInteger(pLeaf, "edx", ulEdx);
942 }
943 else if (hrc != E_INVALIDARG) H();
944 }
945
946 /* We must limit CPUID count for Windows NT 4, as otherwise it stops
947 with error 0x3e (MULTIPROCESSOR_CONFIGURATION_NOT_SUPPORTED). */
948 if (osTypeId == "WindowsNT4")
949 {
950 LogRel(("Limiting CPUID leaf count for NT4 guests\n"));
951 InsertConfigInteger(pCPUM, "NT4LeafLimit", true);
952 }
953
954 /* Expose CMPXCHG16B. Currently a hack. */
955 if ( osTypeId == "Windows81_64"
956 || osTypeId == "Windows2012_64"
957 || osTypeId == "Windows10_64")
958 {
959 LogRel(("Enabling CMPXCHG16B for Windows 8.1 / 2k12 or newer guests\n"));
960 InsertConfigInteger(pCPUM, "CMPXCHG16B", true);
961 }
962
963 if (fOsXGuest)
964 {
965 /* Expose extended MWAIT features to Mac OS X guests. */
966 LogRel(("Using MWAIT extensions\n"));
967 InsertConfigInteger(pCPUM, "MWaitExtensions", true);
968
969 /* Fake the CPU family/model so the guest works. This is partly
970 because older mac releases really doesn't work on newer cpus,
971 and partly because mac os x expects more from systems with newer
972 cpus (MSRs, power features, whatever). */
973 uint32_t uMaxIntelFamilyModelStep = UINT32_MAX;
974 if ( osTypeId == "MacOS"
975 || osTypeId == "MacOS_64")
976 uMaxIntelFamilyModelStep = RT_MAKE_U32_FROM_U8(1, 23, 6, 0); /* Penryn / X5482. */
977 else if ( osTypeId == "MacOS106"
978 || osTypeId == "MacOS106_64")
979 uMaxIntelFamilyModelStep = RT_MAKE_U32_FROM_U8(1, 23, 6, 0); /* Penryn / X5482 */
980 else if ( osTypeId == "MacOS107"
981 || osTypeId == "MacOS107_64")
982 uMaxIntelFamilyModelStep = RT_MAKE_U32_FROM_U8(1, 23, 6, 0); /* Penryn / X5482 */ /** @todo figure out
983 what is required here. */
984 else if ( osTypeId == "MacOS108"
985 || osTypeId == "MacOS108_64")
986 uMaxIntelFamilyModelStep = RT_MAKE_U32_FROM_U8(1, 23, 6, 0); /* Penryn / X5482 */ /** @todo figure out
987 what is required here. */
988 else if ( osTypeId == "MacOS109"
989 || osTypeId == "MacOS109_64")
990 uMaxIntelFamilyModelStep = RT_MAKE_U32_FROM_U8(1, 23, 6, 0); /* Penryn / X5482 */ /** @todo figure
991 out what is required here. */
992 if (uMaxIntelFamilyModelStep != UINT32_MAX)
993 InsertConfigInteger(pCPUM, "MaxIntelFamilyModelStep", uMaxIntelFamilyModelStep);
994 }
995
996 /* CPU Portability level, */
997 ULONG uCpuIdPortabilityLevel = 0;
998 hrc = pMachine->COMGETTER(CPUIDPortabilityLevel)(&uCpuIdPortabilityLevel); H();
999 InsertConfigInteger(pCPUM, "PortableCpuIdLevel", uCpuIdPortabilityLevel);
1000
1001 /* Physical Address Extension (PAE) */
1002 BOOL fEnablePAE = false;
1003 hrc = pMachine->GetCPUProperty(CPUPropertyType_PAE, &fEnablePAE); H();
1004 InsertConfigInteger(pRoot, "EnablePAE", fEnablePAE);
1005
1006 /* APIC/X2APIC configuration */
1007 BOOL fEnableAPIC = true;
1008 BOOL fEnableX2APIC = true;
1009 hrc = pMachine->GetCPUProperty(CPUPropertyType_APIC, &fEnableAPIC); H();
1010 hrc = pMachine->GetCPUProperty(CPUPropertyType_X2APIC, &fEnableX2APIC); H();
1011 if (fEnableX2APIC)
1012 Assert(fEnableAPIC);
1013
1014 /* CPUM profile name. */
1015 hrc = pMachine->COMGETTER(CPUProfile)(bstr.asOutParam()); H();
1016 InsertConfigString(pCPUM, "GuestCpuName", bstr);
1017
1018 /*
1019 * Temporary(?) hack to make sure we emulate the ancient 16-bit CPUs
1020 * correctly. There are way too many #UDs we'll miss using VT-x,
1021 * raw-mode or qemu for the 186 and 286, while we'll get undefined opcodes
1022 * dead wrong on 8086 (see http://www.os2museum.com/wp/undocumented-8086-opcodes/).
1023 */
1024 if ( bstr.equals("Intel 80386") /* just for now */
1025 || bstr.equals("Intel 80286")
1026 || bstr.equals("Intel 80186")
1027 || bstr.equals("Nec V20")
1028 || bstr.equals("Intel 8086") )
1029 {
1030 InsertConfigInteger(pEM, "IemExecutesAll", true);
1031 if (!bstr.equals("Intel 80386"))
1032 {
1033 fEnableAPIC = false;
1034 fIOAPIC = false;
1035 }
1036 fEnableX2APIC = false;
1037 }
1038
1039 /* Adjust firmware APIC handling to stay within the VCPU limits. */
1040 if (uFwAPIC == 2 && !fEnableX2APIC)
1041 {
1042 if (fEnableAPIC)
1043 uFwAPIC = 1;
1044 else
1045 uFwAPIC = 0;
1046 LogRel(("Limiting the firmware APIC level from x2APIC to %s\n", fEnableAPIC ? "APIC" : "Disabled"));
1047 }
1048 else if (uFwAPIC == 1 && !fEnableAPIC)
1049 {
1050 uFwAPIC = 0;
1051 LogRel(("Limiting the firmware APIC level from APIC to Disabled\n"));
1052 }
1053
1054 /*
1055 * Hardware virtualization extensions.
1056 */
1057 BOOL fSupportsHwVirtEx;
1058 hrc = host->GetProcessorFeature(ProcessorFeature_HWVirtEx, &fSupportsHwVirtEx); H();
1059
1060 BOOL fIsGuest64Bit;
1061 hrc = pMachine->GetCPUProperty(CPUPropertyType_LongMode, &fIsGuest64Bit); H();
1062 if (fIsGuest64Bit)
1063 {
1064 BOOL fSupportsLongMode;
1065 hrc = host->GetProcessorFeature(ProcessorFeature_LongMode, &fSupportsLongMode); H();
1066 if (!fSupportsLongMode)
1067 {
1068 LogRel(("WARNING! 64-bit guest type selected but the host CPU does NOT support 64-bit.\n"));
1069 fIsGuest64Bit = FALSE;
1070 }
1071 if (!fSupportsHwVirtEx)
1072 {
1073 LogRel(("WARNING! 64-bit guest type selected but the host CPU does NOT support HW virtualization.\n"));
1074 fIsGuest64Bit = FALSE;
1075 }
1076 }
1077
1078 BOOL fHMEnabled;
1079 hrc = pMachine->GetHWVirtExProperty(HWVirtExPropertyType_Enabled, &fHMEnabled); H();
1080 if (cCpus > 1 && !fHMEnabled)
1081 {
1082 LogRel(("Forced fHMEnabled to TRUE by SMP guest.\n"));
1083 fHMEnabled = TRUE;
1084 }
1085
1086 BOOL fHMForced;
1087#ifdef VBOX_WITH_RAW_MODE
1088 /* - With more than 4GB PGM will use different RAMRANGE sizes for raw
1089 mode and hv mode to optimize lookup times.
1090 - With more than one virtual CPU, raw-mode isn't a fallback option.
1091 - With a 64-bit guest, raw-mode isn't a fallback option either. */
1092 fHMForced = fHMEnabled
1093 && ( cbRam + cbRamHole > _4G
1094 || cCpus > 1
1095 || fIsGuest64Bit);
1096# ifdef RT_OS_DARWIN
1097 fHMForced = fHMEnabled;
1098# endif
1099 if (fHMForced)
1100 {
1101 if (cbRam + cbRamHole > _4G)
1102 LogRel(("fHMForced=true - Lots of RAM\n"));
1103 if (cCpus > 1)
1104 LogRel(("fHMForced=true - SMP\n"));
1105 if (fIsGuest64Bit)
1106 LogRel(("fHMForced=true - 64-bit guest\n"));
1107# ifdef RT_OS_DARWIN
1108 LogRel(("fHMForced=true - Darwin host\n"));
1109# endif
1110 }
1111#else /* !VBOX_WITH_RAW_MODE */
1112 fHMEnabled = fHMForced = TRUE;
1113 LogRel(("fHMForced=true - No raw-mode support in this build!\n"));
1114#endif /* !VBOX_WITH_RAW_MODE */
1115 if (!fHMForced) /* No need to query if already forced above. */
1116 {
1117 hrc = pMachine->GetHWVirtExProperty(HWVirtExPropertyType_Force, &fHMForced); H();
1118 if (fHMForced)
1119 LogRel(("fHMForced=true - HWVirtExPropertyType_Force\n"));
1120 }
1121 InsertConfigInteger(pRoot, "HMEnabled", fHMEnabled);
1122
1123 /* /HM/xzy */
1124 PCFGMNODE pHM;
1125 InsertConfigNode(pRoot, "HM", &pHM);
1126 InsertConfigInteger(pHM, "HMForced", fHMForced);
1127 if (fHMEnabled)
1128 {
1129 /* Indicate whether 64-bit guests are supported or not. */
1130 InsertConfigInteger(pHM, "64bitEnabled", fIsGuest64Bit);
1131#if ARCH_BITS == 32 /* The recompiler must use VBoxREM64 (32-bit host only). */
1132 PCFGMNODE pREM;
1133 InsertConfigNode(pRoot, "REM", &pREM);
1134 InsertConfigInteger(pREM, "64bitEnabled", 1);
1135#endif
1136
1137 /** @todo Not exactly pretty to check strings; VBOXOSTYPE would be better,
1138 but that requires quite a bit of API change in Main. */
1139 if ( fIOAPIC
1140 && ( osTypeId == "WindowsNT4"
1141 || osTypeId == "Windows2000"
1142 || osTypeId == "WindowsXP"
1143 || osTypeId == "Windows2003"))
1144 {
1145 /* Only allow TPR patching for NT, Win2k, XP and Windows Server 2003. (32 bits mode)
1146 * We may want to consider adding more guest OSes (Solaris) later on.
1147 */
1148 InsertConfigInteger(pHM, "TPRPatchingEnabled", 1);
1149 }
1150 }
1151
1152 /* HWVirtEx exclusive mode */
1153 BOOL fHMExclusive = true;
1154 hrc = systemProperties->COMGETTER(ExclusiveHwVirt)(&fHMExclusive); H();
1155 InsertConfigInteger(pHM, "Exclusive", fHMExclusive);
1156
1157 /* Nested paging (VT-x/AMD-V) */
1158 BOOL fEnableNestedPaging = false;
1159 hrc = pMachine->GetHWVirtExProperty(HWVirtExPropertyType_NestedPaging, &fEnableNestedPaging); H();
1160 InsertConfigInteger(pHM, "EnableNestedPaging", fEnableNestedPaging);
1161
1162 /* Large pages; requires nested paging */
1163 BOOL fEnableLargePages = false;
1164 hrc = pMachine->GetHWVirtExProperty(HWVirtExPropertyType_LargePages, &fEnableLargePages); H();
1165 InsertConfigInteger(pHM, "EnableLargePages", fEnableLargePages);
1166
1167 /* VPID (VT-x) */
1168 BOOL fEnableVPID = false;
1169 hrc = pMachine->GetHWVirtExProperty(HWVirtExPropertyType_VPID, &fEnableVPID); H();
1170 InsertConfigInteger(pHM, "EnableVPID", fEnableVPID);
1171
1172 /* Unrestricted execution aka UX (VT-x) */
1173 BOOL fEnableUX = false;
1174 hrc = pMachine->GetHWVirtExProperty(HWVirtExPropertyType_UnrestrictedExecution, &fEnableUX); H();
1175 InsertConfigInteger(pHM, "EnableUX", fEnableUX);
1176
1177 /* Reset overwrite. */
1178 if (i_isResetTurnedIntoPowerOff())
1179 InsertConfigInteger(pRoot, "PowerOffInsteadOfReset", 1);
1180
1181 /*
1182 * Paravirt. provider.
1183 */
1184 PCFGMNODE pParavirtNode;
1185 InsertConfigNode(pRoot, "GIM", &pParavirtNode);
1186 const char *pcszParavirtProvider;
1187 bool fGimDeviceNeeded = true;
1188 switch (paravirtProvider)
1189 {
1190 case ParavirtProvider_None:
1191 pcszParavirtProvider = "None";
1192 fGimDeviceNeeded = false;
1193 break;
1194
1195 case ParavirtProvider_Minimal:
1196 pcszParavirtProvider = "Minimal";
1197 break;
1198
1199 case ParavirtProvider_HyperV:
1200 pcszParavirtProvider = "HyperV";
1201 break;
1202
1203 case ParavirtProvider_KVM:
1204 pcszParavirtProvider = "KVM";
1205 break;
1206
1207 default:
1208 AssertMsgFailed(("Invalid paravirtProvider=%d\n", paravirtProvider));
1209 return VMR3SetError(pUVM, VERR_INVALID_PARAMETER, RT_SRC_POS, N_("Invalid paravirt. provider '%d'"),
1210 paravirtProvider);
1211 }
1212 InsertConfigString(pParavirtNode, "Provider", pcszParavirtProvider);
1213
1214 /*
1215 * Parse paravirt. debug options.
1216 */
1217 bool fGimDebug = false;
1218 com::Utf8Str strGimDebugAddress = "127.0.0.1";
1219 uint32_t uGimDebugPort = 50000;
1220 if (strParavirtDebug.isNotEmpty())
1221 {
1222 /* Hyper-V debug options. */
1223 if (paravirtProvider == ParavirtProvider_HyperV)
1224 {
1225 bool fGimHvDebug = false;
1226 com::Utf8Str strGimHvVendor;
1227 bool fGimHvVsIf = false;
1228 bool fGimHvHypercallIf = false;
1229
1230 size_t uPos = 0;
1231 com::Utf8Str strDebugOptions = strParavirtDebug;
1232 do
1233 {
1234 com::Utf8Str strKey;
1235 com::Utf8Str strVal;
1236 uPos = strDebugOptions.parseKeyValue(strKey, strVal, uPos);
1237 if (strKey == "enabled")
1238 {
1239 if (strVal.toUInt32() == 1)
1240 {
1241 /* Apply defaults.
1242 The defaults are documented in the user manual,
1243 changes need to be reflected accordingly. */
1244 fGimHvDebug = true;
1245 strGimHvVendor = "Microsoft Hv";
1246 fGimHvVsIf = true;
1247 fGimHvHypercallIf = false;
1248 }
1249 /* else: ignore, i.e. don't assert below with 'enabled=0'. */
1250 }
1251 else if (strKey == "address")
1252 strGimDebugAddress = strVal;
1253 else if (strKey == "port")
1254 uGimDebugPort = strVal.toUInt32();
1255 else if (strKey == "vendor")
1256 strGimHvVendor = strVal;
1257 else if (strKey == "vsinterface")
1258 fGimHvVsIf = RT_BOOL(strVal.toUInt32());
1259 else if (strKey == "hypercallinterface")
1260 fGimHvHypercallIf = RT_BOOL(strVal.toUInt32());
1261 else
1262 {
1263 AssertMsgFailed(("Unrecognized Hyper-V debug option '%s'\n", strKey.c_str()));
1264 return VMR3SetError(pUVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
1265 N_("Unrecognized Hyper-V debug option '%s' in '%s'"), strKey.c_str(),
1266 strDebugOptions.c_str());
1267 }
1268 } while (uPos != com::Utf8Str::npos);
1269
1270 /* Update HyperV CFGM node with active debug options. */
1271 if (fGimHvDebug)
1272 {
1273 PCFGMNODE pHvNode;
1274 InsertConfigNode(pParavirtNode, "HyperV", &pHvNode);
1275 InsertConfigString(pHvNode, "VendorID", strGimHvVendor);
1276 InsertConfigInteger(pHvNode, "VSInterface", fGimHvVsIf ? 1 : 0);
1277 InsertConfigInteger(pHvNode, "HypercallDebugInterface", fGimHvHypercallIf ? 1 : 0);
1278 fGimDebug = true;
1279 }
1280 }
1281 }
1282
1283 /*
1284 * MM values.
1285 */
1286 PCFGMNODE pMM;
1287 InsertConfigNode(pRoot, "MM", &pMM);
1288 InsertConfigInteger(pMM, "CanUseLargerHeap", chipsetType == ChipsetType_ICH9);
1289
1290 /*
1291 * PDM config.
1292 * Load drivers in VBoxC.[so|dll]
1293 */
1294 PCFGMNODE pPDM;
1295 PCFGMNODE pNode;
1296 PCFGMNODE pMod;
1297 InsertConfigNode(pRoot, "PDM", &pPDM);
1298 InsertConfigNode(pPDM, "Devices", &pNode);
1299 InsertConfigNode(pPDM, "Drivers", &pNode);
1300 InsertConfigNode(pNode, "VBoxC", &pMod);
1301#ifdef VBOX_WITH_XPCOM
1302 // VBoxC is located in the components subdirectory
1303 char szPathVBoxC[RTPATH_MAX];
1304 rc = RTPathAppPrivateArch(szPathVBoxC, RTPATH_MAX - sizeof("/components/VBoxC")); AssertRC(rc);
1305 strcat(szPathVBoxC, "/components/VBoxC");
1306 InsertConfigString(pMod, "Path", szPathVBoxC);
1307#else
1308 InsertConfigString(pMod, "Path", "VBoxC");
1309#endif
1310
1311
1312 /*
1313 * Block cache settings.
1314 */
1315 PCFGMNODE pPDMBlkCache;
1316 InsertConfigNode(pPDM, "BlkCache", &pPDMBlkCache);
1317
1318 /* I/O cache size */
1319 ULONG ioCacheSize = 5;
1320 hrc = pMachine->COMGETTER(IOCacheSize)(&ioCacheSize); H();
1321 InsertConfigInteger(pPDMBlkCache, "CacheSize", ioCacheSize * _1M);
1322
1323 /*
1324 * Bandwidth groups.
1325 */
1326 PCFGMNODE pAc;
1327 PCFGMNODE pAcFile;
1328 PCFGMNODE pAcFileBwGroups;
1329 ComPtr<IBandwidthControl> bwCtrl;
1330 com::SafeIfaceArray<IBandwidthGroup> bwGroups;
1331
1332 hrc = pMachine->COMGETTER(BandwidthControl)(bwCtrl.asOutParam()); H();
1333
1334 hrc = bwCtrl->GetAllBandwidthGroups(ComSafeArrayAsOutParam(bwGroups)); H();
1335
1336 InsertConfigNode(pPDM, "AsyncCompletion", &pAc);
1337 InsertConfigNode(pAc, "File", &pAcFile);
1338 InsertConfigNode(pAcFile, "BwGroups", &pAcFileBwGroups);
1339#ifdef VBOX_WITH_NETSHAPER
1340 PCFGMNODE pNetworkShaper;
1341 PCFGMNODE pNetworkBwGroups;
1342
1343 InsertConfigNode(pPDM, "NetworkShaper", &pNetworkShaper);
1344 InsertConfigNode(pNetworkShaper, "BwGroups", &pNetworkBwGroups);
1345#endif /* VBOX_WITH_NETSHAPER */
1346
1347 for (size_t i = 0; i < bwGroups.size(); i++)
1348 {
1349 Bstr strName;
1350 LONG64 cMaxBytesPerSec;
1351 BandwidthGroupType_T enmType;
1352
1353 hrc = bwGroups[i]->COMGETTER(Name)(strName.asOutParam()); H();
1354 hrc = bwGroups[i]->COMGETTER(Type)(&enmType); H();
1355 hrc = bwGroups[i]->COMGETTER(MaxBytesPerSec)(&cMaxBytesPerSec); H();
1356
1357 if (strName.isEmpty())
1358 return VMR3SetError(pUVM, VERR_CFGM_NO_NODE, RT_SRC_POS,
1359 N_("No bandwidth group name specified"));
1360
1361 if (enmType == BandwidthGroupType_Disk)
1362 {
1363 PCFGMNODE pBwGroup;
1364 InsertConfigNode(pAcFileBwGroups, Utf8Str(strName).c_str(), &pBwGroup);
1365 InsertConfigInteger(pBwGroup, "Max", cMaxBytesPerSec);
1366 InsertConfigInteger(pBwGroup, "Start", cMaxBytesPerSec);
1367 InsertConfigInteger(pBwGroup, "Step", 0);
1368 }
1369#ifdef VBOX_WITH_NETSHAPER
1370 else if (enmType == BandwidthGroupType_Network)
1371 {
1372 /* Network bandwidth groups. */
1373 PCFGMNODE pBwGroup;
1374 InsertConfigNode(pNetworkBwGroups, Utf8Str(strName).c_str(), &pBwGroup);
1375 InsertConfigInteger(pBwGroup, "Max", cMaxBytesPerSec);
1376 }
1377#endif /* VBOX_WITH_NETSHAPER */
1378 }
1379
1380 /*
1381 * Devices
1382 */
1383 PCFGMNODE pDevices = NULL; /* /Devices */
1384 PCFGMNODE pDev = NULL; /* /Devices/Dev/ */
1385 PCFGMNODE pInst = NULL; /* /Devices/Dev/0/ */
1386 PCFGMNODE pCfg = NULL; /* /Devices/Dev/.../Config/ */
1387 PCFGMNODE pLunL0 = NULL; /* /Devices/Dev/0/LUN#0/ */
1388 PCFGMNODE pLunL1 = NULL; /* /Devices/Dev/0/LUN#0/AttachedDriver/ */
1389 PCFGMNODE pLunL2 = NULL; /* /Devices/Dev/0/LUN#0/AttachedDriver/Config/ */
1390 PCFGMNODE pBiosCfg = NULL; /* /Devices/pcbios/0/Config/ */
1391 PCFGMNODE pNetBootCfg = NULL; /* /Devices/pcbios/0/Config/NetBoot/ */
1392
1393 InsertConfigNode(pRoot, "Devices", &pDevices);
1394
1395 /*
1396 * GIM Device
1397 */
1398 if (fGimDeviceNeeded)
1399 {
1400 InsertConfigNode(pDevices, "GIMDev", &pDev);
1401 InsertConfigNode(pDev, "0", &pInst);
1402 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1403 //InsertConfigNode(pInst, "Config", &pCfg);
1404
1405 if (fGimDebug)
1406 {
1407 InsertConfigNode(pInst, "LUN#998", &pLunL0);
1408 InsertConfigString(pLunL0, "Driver", "UDP");
1409 InsertConfigNode(pLunL0, "Config", &pLunL1);
1410 InsertConfigString(pLunL1, "ServerAddress", strGimDebugAddress);
1411 InsertConfigInteger(pLunL1, "ServerPort", uGimDebugPort);
1412 }
1413 }
1414
1415 /*
1416 * PC Arch.
1417 */
1418 InsertConfigNode(pDevices, "pcarch", &pDev);
1419 InsertConfigNode(pDev, "0", &pInst);
1420 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1421 InsertConfigNode(pInst, "Config", &pCfg);
1422
1423 /*
1424 * The time offset
1425 */
1426 LONG64 timeOffset;
1427 hrc = biosSettings->COMGETTER(TimeOffset)(&timeOffset); H();
1428 PCFGMNODE pTMNode;
1429 InsertConfigNode(pRoot, "TM", &pTMNode);
1430 InsertConfigInteger(pTMNode, "UTCOffset", timeOffset * 1000000);
1431
1432 /*
1433 * DMA
1434 */
1435 InsertConfigNode(pDevices, "8237A", &pDev);
1436 InsertConfigNode(pDev, "0", &pInst);
1437 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1438
1439 /*
1440 * PCI buses.
1441 */
1442 uint32_t uIocPCIAddress, uHbcPCIAddress;
1443 switch (chipsetType)
1444 {
1445 default:
1446 Assert(false);
1447 case ChipsetType_PIIX3:
1448 InsertConfigNode(pDevices, "pci", &pDev);
1449 uHbcPCIAddress = (0x0 << 16) | 0;
1450 uIocPCIAddress = (0x1 << 16) | 0; // ISA controller
1451 break;
1452 case ChipsetType_ICH9:
1453 InsertConfigNode(pDevices, "ich9pci", &pDev);
1454 uHbcPCIAddress = (0x1e << 16) | 0;
1455 uIocPCIAddress = (0x1f << 16) | 0; // LPC controller
1456 break;
1457 }
1458 InsertConfigNode(pDev, "0", &pInst);
1459 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1460 InsertConfigNode(pInst, "Config", &pCfg);
1461 InsertConfigInteger(pCfg, "IOAPIC", fIOAPIC);
1462 if (chipsetType == ChipsetType_ICH9)
1463 {
1464 /* Provide MCFG info */
1465 InsertConfigInteger(pCfg, "McfgBase", uMcfgBase);
1466 InsertConfigInteger(pCfg, "McfgLength", cbMcfgLength);
1467
1468
1469 /* And register 2 bridges */
1470 InsertConfigNode(pDevices, "ich9pcibridge", &pDev);
1471 InsertConfigNode(pDev, "0", &pInst);
1472 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1473 hrc = pBusMgr->assignPCIDevice("ich9pcibridge", pInst); H();
1474
1475 InsertConfigNode(pDev, "1", &pInst);
1476 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1477 hrc = pBusMgr->assignPCIDevice("ich9pcibridge", pInst); H();
1478
1479#ifdef VBOX_WITH_PCI_PASSTHROUGH
1480 /* Add PCI passthrough devices */
1481 hrc = i_attachRawPCIDevices(pUVM, pBusMgr, pDevices); H();
1482#endif
1483 }
1484
1485 /*
1486 * Enable the following devices: HPET, SMC and LPC on MacOS X guests or on ICH9 chipset
1487 */
1488
1489 /*
1490 * High Precision Event Timer (HPET)
1491 */
1492 BOOL fHPETEnabled;
1493 /* Other guests may wish to use HPET too, but MacOS X not functional without it */
1494 hrc = pMachine->COMGETTER(HPETEnabled)(&fHPETEnabled); H();
1495 /* so always enable HPET in extended profile */
1496 fHPETEnabled |= fOsXGuest;
1497 /* HPET is always present on ICH9 */
1498 fHPETEnabled |= (chipsetType == ChipsetType_ICH9);
1499 if (fHPETEnabled)
1500 {
1501 InsertConfigNode(pDevices, "hpet", &pDev);
1502 InsertConfigNode(pDev, "0", &pInst);
1503 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1504 InsertConfigNode(pInst, "Config", &pCfg);
1505 InsertConfigInteger(pCfg, "ICH9", (chipsetType == ChipsetType_ICH9) ? 1 : 0); /* boolean */
1506 }
1507
1508 /*
1509 * System Management Controller (SMC)
1510 */
1511 BOOL fSmcEnabled;
1512 fSmcEnabled = fOsXGuest;
1513 if (fSmcEnabled)
1514 {
1515 InsertConfigNode(pDevices, "smc", &pDev);
1516 InsertConfigNode(pDev, "0", &pInst);
1517 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1518 InsertConfigNode(pInst, "Config", &pCfg);
1519
1520 bool fGetKeyFromRealSMC;
1521 Utf8Str strKey;
1522 rc = getSmcDeviceKey(virtualBox, pMachine, &strKey, &fGetKeyFromRealSMC);
1523 AssertRCReturn(rc, rc);
1524
1525 if (!fGetKeyFromRealSMC)
1526 InsertConfigString(pCfg, "DeviceKey", strKey);
1527 InsertConfigInteger(pCfg, "GetKeyFromRealSMC", fGetKeyFromRealSMC);
1528 }
1529
1530 /*
1531 * Low Pin Count (LPC) bus
1532 */
1533 BOOL fLpcEnabled;
1534 /** @todo implement appropriate getter */
1535 fLpcEnabled = fOsXGuest || (chipsetType == ChipsetType_ICH9);
1536 if (fLpcEnabled)
1537 {
1538 InsertConfigNode(pDevices, "lpc", &pDev);
1539 InsertConfigNode(pDev, "0", &pInst);
1540 hrc = pBusMgr->assignPCIDevice("lpc", pInst); H();
1541 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1542 }
1543
1544 BOOL fShowRtc;
1545 fShowRtc = fOsXGuest || (chipsetType == ChipsetType_ICH9);
1546
1547 /*
1548 * PS/2 keyboard & mouse.
1549 */
1550 InsertConfigNode(pDevices, "pckbd", &pDev);
1551 InsertConfigNode(pDev, "0", &pInst);
1552 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1553 InsertConfigNode(pInst, "Config", &pCfg);
1554
1555 InsertConfigNode(pInst, "LUN#0", &pLunL0);
1556 InsertConfigString(pLunL0, "Driver", "KeyboardQueue");
1557 InsertConfigNode(pLunL0, "Config", &pCfg);
1558 InsertConfigInteger(pCfg, "QueueSize", 64);
1559
1560 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
1561 InsertConfigString(pLunL1, "Driver", "MainKeyboard");
1562 InsertConfigNode(pLunL1, "Config", &pCfg);
1563 Keyboard *pKeyboard = mKeyboard;
1564 InsertConfigInteger(pCfg, "Object", (uintptr_t)pKeyboard);
1565
1566 Mouse *pMouse = mMouse;
1567 PointingHIDType_T aPointingHID;
1568 hrc = pMachine->COMGETTER(PointingHIDType)(&aPointingHID); H();
1569 InsertConfigNode(pInst, "LUN#1", &pLunL0);
1570 InsertConfigString(pLunL0, "Driver", "MouseQueue");
1571 InsertConfigNode(pLunL0, "Config", &pCfg);
1572 InsertConfigInteger(pCfg, "QueueSize", 128);
1573
1574 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
1575 InsertConfigString(pLunL1, "Driver", "MainMouse");
1576 InsertConfigNode(pLunL1, "Config", &pCfg);
1577 InsertConfigInteger(pCfg, "Object", (uintptr_t)pMouse);
1578
1579 /*
1580 * i8254 Programmable Interval Timer And Dummy Speaker
1581 */
1582 InsertConfigNode(pDevices, "i8254", &pDev);
1583 InsertConfigNode(pDev, "0", &pInst);
1584 InsertConfigNode(pInst, "Config", &pCfg);
1585#ifdef DEBUG
1586 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1587#endif
1588
1589 /*
1590 * i8259 Programmable Interrupt Controller.
1591 */
1592 InsertConfigNode(pDevices, "i8259", &pDev);
1593 InsertConfigNode(pDev, "0", &pInst);
1594 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1595 InsertConfigNode(pInst, "Config", &pCfg);
1596
1597 /*
1598 * Advanced Programmable Interrupt Controller.
1599 * SMP: Each CPU has a LAPIC, but we have a single device representing all LAPICs states,
1600 * thus only single insert
1601 */
1602 if (fEnableAPIC)
1603 {
1604 InsertConfigNode(pDevices, "apic", &pDev);
1605 InsertConfigNode(pDev, "0", &pInst);
1606 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1607 InsertConfigNode(pInst, "Config", &pCfg);
1608 InsertConfigInteger(pCfg, "IOAPIC", fIOAPIC);
1609 PDMAPICMODE enmAPICMode = PDMAPICMODE_APIC;
1610 if (fEnableX2APIC)
1611 enmAPICMode = PDMAPICMODE_X2APIC;
1612 else if (!fEnableAPIC)
1613 enmAPICMode = PDMAPICMODE_NONE;
1614 InsertConfigInteger(pCfg, "Mode", enmAPICMode);
1615 InsertConfigInteger(pCfg, "NumCPUs", cCpus);
1616
1617 if (fIOAPIC)
1618 {
1619 /*
1620 * I/O Advanced Programmable Interrupt Controller.
1621 */
1622 InsertConfigNode(pDevices, "ioapic", &pDev);
1623 InsertConfigNode(pDev, "0", &pInst);
1624 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1625 InsertConfigNode(pInst, "Config", &pCfg);
1626 InsertConfigInteger(pCfg, "NumCPUs", cCpus);
1627 }
1628 }
1629
1630 /*
1631 * RTC MC146818.
1632 */
1633 InsertConfigNode(pDevices, "mc146818", &pDev);
1634 InsertConfigNode(pDev, "0", &pInst);
1635 InsertConfigNode(pInst, "Config", &pCfg);
1636 BOOL fRTCUseUTC;
1637 hrc = pMachine->COMGETTER(RTCUseUTC)(&fRTCUseUTC); H();
1638 InsertConfigInteger(pCfg, "UseUTC", fRTCUseUTC ? 1 : 0);
1639
1640 /*
1641 * VGA.
1642 */
1643 GraphicsControllerType_T enmGraphicsController;
1644 hrc = pMachine->COMGETTER(GraphicsControllerType)(&enmGraphicsController); H();
1645 switch (enmGraphicsController)
1646 {
1647 case GraphicsControllerType_Null:
1648 break;
1649 case GraphicsControllerType_VBoxVGA:
1650#ifdef VBOX_WITH_VMSVGA
1651 case GraphicsControllerType_VMSVGA:
1652#endif
1653 rc = i_configGraphicsController(pDevices, enmGraphicsController, pBusMgr, pMachine, biosSettings,
1654 RT_BOOL(fHMEnabled));
1655 if (FAILED(rc))
1656 return rc;
1657 break;
1658 default:
1659 AssertMsgFailed(("Invalid graphicsController=%d\n", enmGraphicsController));
1660 return VMR3SetError(pUVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
1661 N_("Invalid graphics controller type '%d'"), enmGraphicsController);
1662 }
1663
1664 /*
1665 * Firmware.
1666 */
1667 FirmwareType_T eFwType = FirmwareType_BIOS;
1668 hrc = pMachine->COMGETTER(FirmwareType)(&eFwType); H();
1669
1670#ifdef VBOX_WITH_EFI
1671 BOOL fEfiEnabled = (eFwType >= FirmwareType_EFI) && (eFwType <= FirmwareType_EFIDUAL);
1672#else
1673 BOOL fEfiEnabled = false;
1674#endif
1675 if (!fEfiEnabled)
1676 {
1677 /*
1678 * PC Bios.
1679 */
1680 InsertConfigNode(pDevices, "pcbios", &pDev);
1681 InsertConfigNode(pDev, "0", &pInst);
1682 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1683 InsertConfigNode(pInst, "Config", &pBiosCfg);
1684 InsertConfigInteger(pBiosCfg, "RamSize", cbRam);
1685 InsertConfigInteger(pBiosCfg, "RamHoleSize", cbRamHole);
1686 InsertConfigInteger(pBiosCfg, "NumCPUs", cCpus);
1687 InsertConfigString(pBiosCfg, "HardDiskDevice", "piix3ide");
1688 InsertConfigString(pBiosCfg, "FloppyDevice", "i82078");
1689 InsertConfigInteger(pBiosCfg, "IOAPIC", fIOAPIC);
1690 InsertConfigInteger(pBiosCfg, "APIC", uFwAPIC);
1691 BOOL fPXEDebug;
1692 hrc = biosSettings->COMGETTER(PXEDebugEnabled)(&fPXEDebug); H();
1693 InsertConfigInteger(pBiosCfg, "PXEDebug", fPXEDebug);
1694 InsertConfigBytes(pBiosCfg, "UUID", &HardwareUuid,sizeof(HardwareUuid));
1695 InsertConfigNode(pBiosCfg, "NetBoot", &pNetBootCfg);
1696 InsertConfigInteger(pBiosCfg, "McfgBase", uMcfgBase);
1697 InsertConfigInteger(pBiosCfg, "McfgLength", cbMcfgLength);
1698
1699 DeviceType_T bootDevice;
1700 AssertMsgReturn(SchemaDefs::MaxBootPosition <= 9, ("Too many boot devices %d\n", SchemaDefs::MaxBootPosition),
1701 VERR_INVALID_PARAMETER);
1702
1703 for (ULONG pos = 1; pos <= SchemaDefs::MaxBootPosition; ++pos)
1704 {
1705 hrc = pMachine->GetBootOrder(pos, &bootDevice); H();
1706
1707 char szParamName[] = "BootDeviceX";
1708 szParamName[sizeof(szParamName) - 2] = (char)(pos - 1 + '0');
1709
1710 const char *pszBootDevice;
1711 switch (bootDevice)
1712 {
1713 case DeviceType_Null:
1714 pszBootDevice = "NONE";
1715 break;
1716 case DeviceType_HardDisk:
1717 pszBootDevice = "IDE";
1718 break;
1719 case DeviceType_DVD:
1720 pszBootDevice = "DVD";
1721 break;
1722 case DeviceType_Floppy:
1723 pszBootDevice = "FLOPPY";
1724 break;
1725 case DeviceType_Network:
1726 pszBootDevice = "LAN";
1727 break;
1728 default:
1729 AssertMsgFailed(("Invalid bootDevice=%d\n", bootDevice));
1730 return VMR3SetError(pUVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
1731 N_("Invalid boot device '%d'"), bootDevice);
1732 }
1733 InsertConfigString(pBiosCfg, szParamName, pszBootDevice);
1734 }
1735
1736 /** @todo @bugref{7145}: We might want to enable this by default for new VMs. For now,
1737 * this is required for Windows 2012 guests. */
1738 if (osTypeId == "Windows2012_64")
1739 InsertConfigInteger(pBiosCfg, "DmiExposeMemoryTable", 1); /* boolean */
1740 }
1741 else
1742 {
1743 /* Autodetect firmware type, basing on guest type */
1744 if (eFwType == FirmwareType_EFI)
1745 {
1746 eFwType = fIsGuest64Bit
1747 ? (FirmwareType_T)FirmwareType_EFI64
1748 : (FirmwareType_T)FirmwareType_EFI32;
1749 }
1750 bool const f64BitEntry = eFwType == FirmwareType_EFI64;
1751
1752 Utf8Str efiRomFile;
1753 rc = findEfiRom(virtualBox, eFwType, &efiRomFile);
1754 AssertRCReturn(rc, rc);
1755
1756 /* Get boot args */
1757 Utf8Str bootArgs;
1758 GetExtraDataBoth(virtualBox, pMachine, "VBoxInternal2/EfiBootArgs", &bootArgs);
1759
1760 /* Get device props */
1761 Utf8Str deviceProps;
1762 GetExtraDataBoth(virtualBox, pMachine, "VBoxInternal2/EfiDeviceProps", &deviceProps);
1763
1764 /* Get GOP mode settings */
1765 uint32_t u32GopMode = UINT32_MAX;
1766 GetExtraDataBoth(virtualBox, pMachine, "VBoxInternal2/EfiGopMode", &strTmp);
1767 if (!strTmp.isEmpty())
1768 u32GopMode = strTmp.toUInt32();
1769
1770 /* UGA mode settings */
1771 uint32_t u32UgaHorizontal = 0;
1772 GetExtraDataBoth(virtualBox, pMachine, "VBoxInternal2/EfiUgaHorizontalResolution", &strTmp);
1773 if (!strTmp.isEmpty())
1774 u32UgaHorizontal = strTmp.toUInt32();
1775
1776 uint32_t u32UgaVertical = 0;
1777 GetExtraDataBoth(virtualBox, pMachine, "VBoxInternal2/EfiUgaVerticalResolution", &strTmp);
1778 if (!strTmp.isEmpty())
1779 u32UgaVertical = strTmp.toUInt32();
1780
1781 /*
1782 * EFI subtree.
1783 */
1784 InsertConfigNode(pDevices, "efi", &pDev);
1785 InsertConfigNode(pDev, "0", &pInst);
1786 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1787 InsertConfigNode(pInst, "Config", &pCfg);
1788 InsertConfigInteger(pCfg, "RamSize", cbRam);
1789 InsertConfigInteger(pCfg, "RamHoleSize", cbRamHole);
1790 InsertConfigInteger(pCfg, "NumCPUs", cCpus);
1791 InsertConfigString(pCfg, "EfiRom", efiRomFile);
1792 InsertConfigString(pCfg, "BootArgs", bootArgs);
1793 InsertConfigString(pCfg, "DeviceProps", deviceProps);
1794 InsertConfigInteger(pCfg, "IOAPIC", fIOAPIC);
1795 InsertConfigInteger(pCfg, "APIC", uFwAPIC);
1796 InsertConfigBytes(pCfg, "UUID", &HardwareUuid,sizeof(HardwareUuid));
1797 InsertConfigInteger(pCfg, "64BitEntry", f64BitEntry); /* boolean */
1798 InsertConfigInteger(pCfg, "GopMode", u32GopMode);
1799 InsertConfigInteger(pCfg, "UgaHorizontalResolution", u32UgaHorizontal);
1800 InsertConfigInteger(pCfg, "UgaVerticalResolution", u32UgaVertical);
1801
1802 /* For OS X guests we'll force passing host's DMI info to the guest */
1803 if (fOsXGuest)
1804 {
1805 InsertConfigInteger(pCfg, "DmiUseHostInfo", 1);
1806 InsertConfigInteger(pCfg, "DmiExposeMemoryTable", 1);
1807 }
1808 InsertConfigNode(pInst, "LUN#0", &pLunL0);
1809 InsertConfigString(pLunL0, "Driver", "NvramStorage");
1810 InsertConfigNode(pLunL0, "Config", &pCfg);
1811 InsertConfigInteger(pCfg, "Object", (uintptr_t)mNvram);
1812#ifdef DEBUG_vvl
1813 InsertConfigInteger(pCfg, "PermanentSave", 1);
1814#endif
1815 }
1816
1817 /*
1818 * The USB Controllers.
1819 */
1820 com::SafeIfaceArray<IUSBController> usbCtrls;
1821 hrc = pMachine->COMGETTER(USBControllers)(ComSafeArrayAsOutParam(usbCtrls));
1822 bool fOhciPresent = false; /**< Flag whether at least one OHCI controller is present. */
1823 bool fXhciPresent = false; /**< Flag whether at least one XHCI controller is present. */
1824
1825 if (SUCCEEDED(hrc))
1826 {
1827 for (size_t i = 0; i < usbCtrls.size(); ++i)
1828 {
1829 USBControllerType_T enmCtrlType;
1830 rc = usbCtrls[i]->COMGETTER(Type)(&enmCtrlType); H();
1831 if (enmCtrlType == USBControllerType_OHCI)
1832 {
1833 fOhciPresent = true;
1834 break;
1835 }
1836 else if (enmCtrlType == USBControllerType_XHCI)
1837 {
1838 fXhciPresent = true;
1839 break;
1840 }
1841 }
1842 }
1843 else if (hrc != E_NOTIMPL)
1844 {
1845 H();
1846 }
1847
1848 /*
1849 * Currently EHCI is only enabled when an OHCI or XHCI controller is present as well.
1850 */
1851 if (fOhciPresent || fXhciPresent)
1852 mfVMHasUsbController = true;
1853
1854 PCFGMNODE pUsbDevices = NULL; /**< Required for USB storage controller later. */
1855 if (mfVMHasUsbController)
1856 {
1857 for (size_t i = 0; i < usbCtrls.size(); ++i)
1858 {
1859 USBControllerType_T enmCtrlType;
1860 rc = usbCtrls[i]->COMGETTER(Type)(&enmCtrlType); H();
1861
1862 if (enmCtrlType == USBControllerType_OHCI)
1863 {
1864 InsertConfigNode(pDevices, "usb-ohci", &pDev);
1865 InsertConfigNode(pDev, "0", &pInst);
1866 InsertConfigNode(pInst, "Config", &pCfg);
1867 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1868 hrc = pBusMgr->assignPCIDevice("usb-ohci", pInst); H();
1869 InsertConfigNode(pInst, "LUN#0", &pLunL0);
1870 InsertConfigString(pLunL0, "Driver", "VUSBRootHub");
1871 InsertConfigNode(pLunL0, "Config", &pCfg);
1872
1873 /*
1874 * Attach the status driver.
1875 */
1876 i_attachStatusDriver(pInst, &mapUSBLed[0], 0, 0, NULL, NULL, 0);
1877 }
1878#ifdef VBOX_WITH_EHCI
1879 else if (enmCtrlType == USBControllerType_EHCI)
1880 {
1881 /*
1882 * USB 2.0 is only available if the proper ExtPack is installed.
1883 *
1884 * Note. Configuring EHCI here and providing messages about
1885 * the missing extpack isn't exactly clean, but it is a
1886 * necessary evil to patch over legacy compatability issues
1887 * introduced by the new distribution model.
1888 */
1889# ifdef VBOX_WITH_EXTPACK
1890 static const char *s_pszUsbExtPackName = "Oracle VM VirtualBox Extension Pack";
1891 if (mptrExtPackManager->i_isExtPackUsable(s_pszUsbExtPackName))
1892# endif
1893 {
1894 InsertConfigNode(pDevices, "usb-ehci", &pDev);
1895 InsertConfigNode(pDev, "0", &pInst);
1896 InsertConfigNode(pInst, "Config", &pCfg);
1897 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1898 hrc = pBusMgr->assignPCIDevice("usb-ehci", pInst); H();
1899
1900 InsertConfigNode(pInst, "LUN#0", &pLunL0);
1901 InsertConfigString(pLunL0, "Driver", "VUSBRootHub");
1902 InsertConfigNode(pLunL0, "Config", &pCfg);
1903
1904 /*
1905 * Attach the status driver.
1906 */
1907 i_attachStatusDriver(pInst, &mapUSBLed[1], 0, 0, NULL, NULL, 0);
1908 }
1909# ifdef VBOX_WITH_EXTPACK
1910 else
1911 {
1912 /* Always fatal! Up to VBox 4.0.4 we allowed to start the VM anyway
1913 * but this induced problems when the user saved + restored the VM! */
1914 return VMR3SetError(pUVM, VERR_NOT_FOUND, RT_SRC_POS,
1915 N_("Implementation of the USB 2.0 controller not found!\n"
1916 "Because the USB 2.0 controller state is part of the saved "
1917 "VM state, the VM cannot be started. To fix "
1918 "this problem, either install the '%s' or disable USB 2.0 "
1919 "support in the VM settings.\n"
1920 "Note! This error could also mean that an incompatible version of "
1921 "the '%s' is installed"),
1922 s_pszUsbExtPackName, s_pszUsbExtPackName);
1923 }
1924# endif
1925 }
1926#endif
1927 else if (enmCtrlType == USBControllerType_XHCI)
1928 {
1929 /*
1930 * USB 3.0 is only available if the proper ExtPack is installed.
1931 *
1932 * Note. Configuring EHCI here and providing messages about
1933 * the missing extpack isn't exactly clean, but it is a
1934 * necessary evil to patch over legacy compatability issues
1935 * introduced by the new distribution model.
1936 */
1937# ifdef VBOX_WITH_EXTPACK
1938 static const char *s_pszUsbExtPackName = "Oracle VM VirtualBox Extension Pack";
1939 if (mptrExtPackManager->i_isExtPackUsable(s_pszUsbExtPackName))
1940# endif
1941 {
1942 InsertConfigNode(pDevices, "usb-xhci", &pDev);
1943 InsertConfigNode(pDev, "0", &pInst);
1944 InsertConfigNode(pInst, "Config", &pCfg);
1945 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1946 hrc = pBusMgr->assignPCIDevice("usb-xhci", pInst); H();
1947
1948 InsertConfigNode(pInst, "LUN#0", &pLunL0);
1949 InsertConfigString(pLunL0, "Driver", "VUSBRootHub");
1950 InsertConfigNode(pLunL0, "Config", &pCfg);
1951
1952 InsertConfigNode(pInst, "LUN#1", &pLunL1);
1953 InsertConfigString(pLunL1, "Driver", "VUSBRootHub");
1954 InsertConfigNode(pLunL1, "Config", &pCfg);
1955
1956 /*
1957 * Attach the status driver.
1958 */
1959 i_attachStatusDriver(pInst, &mapUSBLed[0], 0, 1, NULL, NULL, 0);
1960 }
1961# ifdef VBOX_WITH_EXTPACK
1962 else
1963 {
1964 /* Always fatal. */
1965 return VMR3SetError(pUVM, VERR_NOT_FOUND, RT_SRC_POS,
1966 N_("Implementation of the USB 3.0 controller not found!\n"
1967 "Because the USB 3.0 controller state is part of the saved "
1968 "VM state, the VM cannot be started. To fix "
1969 "this problem, either install the '%s' or disable USB 3.0 "
1970 "support in the VM settings"),
1971 s_pszUsbExtPackName);
1972 }
1973# endif
1974 }
1975 } /* for every USB controller. */
1976
1977
1978 /*
1979 * Virtual USB Devices.
1980 */
1981 InsertConfigNode(pRoot, "USB", &pUsbDevices);
1982
1983#ifdef VBOX_WITH_USB
1984 {
1985 /*
1986 * Global USB options, currently unused as we'll apply the 2.0 -> 1.1 morphing
1987 * on a per device level now.
1988 */
1989 InsertConfigNode(pUsbDevices, "USBProxy", &pCfg);
1990 InsertConfigNode(pCfg, "GlobalConfig", &pCfg);
1991 // This globally enables the 2.0 -> 1.1 device morphing of proxied devices to keep windows quiet.
1992 //InsertConfigInteger(pCfg, "Force11Device", true);
1993 // The following breaks stuff, but it makes MSDs work in vista. (I include it here so
1994 // that it's documented somewhere.) Users needing it can use:
1995 // VBoxManage setextradata "myvm" "VBoxInternal/USB/USBProxy/GlobalConfig/Force11PacketSize" 1
1996 //InsertConfigInteger(pCfg, "Force11PacketSize", true);
1997 }
1998#endif
1999
2000#ifdef VBOX_WITH_USB_CARDREADER
2001 BOOL aEmulatedUSBCardReaderEnabled = FALSE;
2002 hrc = pMachine->COMGETTER(EmulatedUSBCardReaderEnabled)(&aEmulatedUSBCardReaderEnabled); H();
2003 if (aEmulatedUSBCardReaderEnabled)
2004 {
2005 InsertConfigNode(pUsbDevices, "CardReader", &pDev);
2006 InsertConfigNode(pDev, "0", &pInst);
2007 InsertConfigNode(pInst, "Config", &pCfg);
2008
2009 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2010# ifdef VBOX_WITH_USB_CARDREADER_TEST
2011 InsertConfigString(pLunL0, "Driver", "DrvDirectCardReader");
2012 InsertConfigNode(pLunL0, "Config", &pCfg);
2013# else
2014 InsertConfigString(pLunL0, "Driver", "UsbCardReader");
2015 InsertConfigNode(pLunL0, "Config", &pCfg);
2016 InsertConfigInteger(pCfg, "Object", (uintptr_t)mUsbCardReader);
2017# endif
2018 }
2019#endif
2020
2021 /* Virtual USB Mouse/Tablet */
2022 if ( aPointingHID == PointingHIDType_USBMouse
2023 || aPointingHID == PointingHIDType_USBTablet
2024 || aPointingHID == PointingHIDType_USBMultiTouch)
2025 {
2026 InsertConfigNode(pUsbDevices, "HidMouse", &pDev);
2027 InsertConfigNode(pDev, "0", &pInst);
2028 InsertConfigNode(pInst, "Config", &pCfg);
2029
2030 if (aPointingHID == PointingHIDType_USBMouse)
2031 InsertConfigString(pCfg, "Mode", "relative");
2032 else
2033 InsertConfigString(pCfg, "Mode", "absolute");
2034 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2035 InsertConfigString(pLunL0, "Driver", "MouseQueue");
2036 InsertConfigNode(pLunL0, "Config", &pCfg);
2037 InsertConfigInteger(pCfg, "QueueSize", 128);
2038
2039 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
2040 InsertConfigString(pLunL1, "Driver", "MainMouse");
2041 InsertConfigNode(pLunL1, "Config", &pCfg);
2042 InsertConfigInteger(pCfg, "Object", (uintptr_t)pMouse);
2043 }
2044 if (aPointingHID == PointingHIDType_USBMultiTouch)
2045 {
2046 InsertConfigNode(pDev, "1", &pInst);
2047 InsertConfigNode(pInst, "Config", &pCfg);
2048
2049 InsertConfigString(pCfg, "Mode", "multitouch");
2050 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2051 InsertConfigString(pLunL0, "Driver", "MouseQueue");
2052 InsertConfigNode(pLunL0, "Config", &pCfg);
2053 InsertConfigInteger(pCfg, "QueueSize", 128);
2054
2055 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
2056 InsertConfigString(pLunL1, "Driver", "MainMouse");
2057 InsertConfigNode(pLunL1, "Config", &pCfg);
2058 InsertConfigInteger(pCfg, "Object", (uintptr_t)pMouse);
2059 }
2060
2061 /* Virtual USB Keyboard */
2062 KeyboardHIDType_T aKbdHID;
2063 hrc = pMachine->COMGETTER(KeyboardHIDType)(&aKbdHID); H();
2064 if (aKbdHID == KeyboardHIDType_USBKeyboard)
2065 {
2066 InsertConfigNode(pUsbDevices, "HidKeyboard", &pDev);
2067 InsertConfigNode(pDev, "0", &pInst);
2068 InsertConfigNode(pInst, "Config", &pCfg);
2069
2070 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2071 InsertConfigString(pLunL0, "Driver", "KeyboardQueue");
2072 InsertConfigNode(pLunL0, "Config", &pCfg);
2073 InsertConfigInteger(pCfg, "QueueSize", 64);
2074
2075 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
2076 InsertConfigString(pLunL1, "Driver", "MainKeyboard");
2077 InsertConfigNode(pLunL1, "Config", &pCfg);
2078 pKeyboard = mKeyboard;
2079 InsertConfigInteger(pCfg, "Object", (uintptr_t)pKeyboard);
2080 }
2081 }
2082
2083 /*
2084 * Storage controllers.
2085 */
2086 com::SafeIfaceArray<IStorageController> ctrls;
2087 PCFGMNODE aCtrlNodes[StorageControllerType_NVMe + 1] = {};
2088 hrc = pMachine->COMGETTER(StorageControllers)(ComSafeArrayAsOutParam(ctrls)); H();
2089
2090 bool fFdcEnabled = false;
2091 for (size_t i = 0; i < ctrls.size(); ++i)
2092 {
2093 DeviceType_T *paLedDevType = NULL;
2094
2095 StorageControllerType_T enmCtrlType;
2096 rc = ctrls[i]->COMGETTER(ControllerType)(&enmCtrlType); H();
2097 AssertRelease((unsigned)enmCtrlType < RT_ELEMENTS(aCtrlNodes)
2098 || enmCtrlType == StorageControllerType_USB);
2099
2100 StorageBus_T enmBus;
2101 rc = ctrls[i]->COMGETTER(Bus)(&enmBus); H();
2102
2103 Bstr controllerName;
2104 rc = ctrls[i]->COMGETTER(Name)(controllerName.asOutParam()); H();
2105
2106 ULONG ulInstance = 999;
2107 rc = ctrls[i]->COMGETTER(Instance)(&ulInstance); H();
2108
2109 BOOL fUseHostIOCache;
2110 rc = ctrls[i]->COMGETTER(UseHostIOCache)(&fUseHostIOCache); H();
2111
2112 BOOL fBootable;
2113 rc = ctrls[i]->COMGETTER(Bootable)(&fBootable); H();
2114
2115 PCFGMNODE pCtlInst = NULL;
2116 const char *pszCtrlDev = i_convertControllerTypeToDev(enmCtrlType);
2117 if (enmCtrlType != StorageControllerType_USB)
2118 {
2119 /* /Devices/<ctrldev>/ */
2120 pDev = aCtrlNodes[enmCtrlType];
2121 if (!pDev)
2122 {
2123 InsertConfigNode(pDevices, pszCtrlDev, &pDev);
2124 aCtrlNodes[enmCtrlType] = pDev; /* IDE variants are handled in the switch */
2125 }
2126
2127 /* /Devices/<ctrldev>/<instance>/ */
2128 InsertConfigNode(pDev, Utf8StrFmt("%u", ulInstance).c_str(), &pCtlInst);
2129
2130 /* Device config: /Devices/<ctrldev>/<instance>/<values> & /ditto/Config/<values> */
2131 InsertConfigInteger(pCtlInst, "Trusted", 1);
2132 InsertConfigNode(pCtlInst, "Config", &pCfg);
2133 }
2134
2135 static const char * const apszBiosConfigScsi[MAX_BIOS_LUN_COUNT] =
2136 { "ScsiLUN1", "ScsiLUN2", "ScsiLUN3", "ScsiLUN4" };
2137
2138 static const char * const apszBiosConfigSata[MAX_BIOS_LUN_COUNT] =
2139 { "SataLUN1", "SataLUN2", "SataLUN3", "SataLUN4" };
2140
2141 switch (enmCtrlType)
2142 {
2143 case StorageControllerType_LsiLogic:
2144 {
2145 hrc = pBusMgr->assignPCIDevice("lsilogic", pCtlInst); H();
2146
2147 InsertConfigInteger(pCfg, "Bootable", fBootable);
2148
2149 /* BIOS configuration values, first SCSI controller only. */
2150 if ( !pBusMgr->hasPCIDevice("lsilogic", 1)
2151 && !pBusMgr->hasPCIDevice("buslogic", 0)
2152 && !pBusMgr->hasPCIDevice("lsilogicsas", 0)
2153 && pBiosCfg)
2154 {
2155 InsertConfigString(pBiosCfg, "ScsiHardDiskDevice", "lsilogicscsi");
2156 hrc = SetBiosDiskInfo(pMachine, pCfg, pBiosCfg, controllerName, apszBiosConfigScsi); H();
2157 }
2158
2159 /* Attach the status driver */
2160 Assert(cLedScsi >= 16);
2161 i_attachStatusDriver(pCtlInst, &mapStorageLeds[iLedScsi], 0, 15,
2162 &mapMediumAttachments, pszCtrlDev, ulInstance);
2163 paLedDevType = &maStorageDevType[iLedScsi];
2164 break;
2165 }
2166
2167 case StorageControllerType_BusLogic:
2168 {
2169 hrc = pBusMgr->assignPCIDevice("buslogic", pCtlInst); H();
2170
2171 InsertConfigInteger(pCfg, "Bootable", fBootable);
2172
2173 /* BIOS configuration values, first SCSI controller only. */
2174 if ( !pBusMgr->hasPCIDevice("lsilogic", 0)
2175 && !pBusMgr->hasPCIDevice("buslogic", 1)
2176 && !pBusMgr->hasPCIDevice("lsilogicsas", 0)
2177 && pBiosCfg)
2178 {
2179 InsertConfigString(pBiosCfg, "ScsiHardDiskDevice", "buslogic");
2180 hrc = SetBiosDiskInfo(pMachine, pCfg, pBiosCfg, controllerName, apszBiosConfigScsi); H();
2181 }
2182
2183 /* Attach the status driver */
2184 Assert(cLedScsi >= 16);
2185 i_attachStatusDriver(pCtlInst, &mapStorageLeds[iLedScsi], 0, 15,
2186 &mapMediumAttachments, pszCtrlDev, ulInstance);
2187 paLedDevType = &maStorageDevType[iLedScsi];
2188 break;
2189 }
2190
2191 case StorageControllerType_IntelAhci:
2192 {
2193 hrc = pBusMgr->assignPCIDevice("ahci", pCtlInst); H();
2194
2195 ULONG cPorts = 0;
2196 hrc = ctrls[i]->COMGETTER(PortCount)(&cPorts); H();
2197 InsertConfigInteger(pCfg, "PortCount", cPorts);
2198 InsertConfigInteger(pCfg, "Bootable", fBootable);
2199
2200 com::SafeIfaceArray<IMediumAttachment> atts;
2201 hrc = pMachine->GetMediumAttachmentsOfController(controllerName.raw(),
2202 ComSafeArrayAsOutParam(atts)); H();
2203
2204 /* Configure the hotpluggable flag for the port. */
2205 for (unsigned idxAtt = 0; idxAtt < atts.size(); ++idxAtt)
2206 {
2207 IMediumAttachment *pMediumAtt = atts[idxAtt];
2208
2209 LONG lPortNum = 0;
2210 hrc = pMediumAtt->COMGETTER(Port)(&lPortNum); H();
2211
2212 BOOL fHotPluggable = FALSE;
2213 hrc = pMediumAtt->COMGETTER(HotPluggable)(&fHotPluggable); H();
2214 if (SUCCEEDED(hrc))
2215 {
2216 PCFGMNODE pPortCfg;
2217 char szName[24];
2218 RTStrPrintf(szName, sizeof(szName), "Port%d", lPortNum);
2219
2220 InsertConfigNode(pCfg, szName, &pPortCfg);
2221 InsertConfigInteger(pPortCfg, "Hotpluggable", fHotPluggable ? 1 : 0);
2222 }
2223 }
2224
2225 /* BIOS configuration values, first AHCI controller only. */
2226 if ( !pBusMgr->hasPCIDevice("ahci", 1)
2227 && pBiosCfg)
2228 {
2229 InsertConfigString(pBiosCfg, "SataHardDiskDevice", "ahci");
2230 hrc = SetBiosDiskInfo(pMachine, pCfg, pBiosCfg, controllerName, apszBiosConfigSata); H();
2231 }
2232
2233 /* Attach the status driver */
2234 AssertRelease(cPorts <= cLedSata);
2235 i_attachStatusDriver(pCtlInst, &mapStorageLeds[iLedSata], 0, cPorts - 1,
2236 &mapMediumAttachments, pszCtrlDev, ulInstance);
2237 paLedDevType = &maStorageDevType[iLedSata];
2238 break;
2239 }
2240
2241 case StorageControllerType_PIIX3:
2242 case StorageControllerType_PIIX4:
2243 case StorageControllerType_ICH6:
2244 {
2245 /*
2246 * IDE (update this when the main interface changes)
2247 */
2248 hrc = pBusMgr->assignPCIDevice("piix3ide", pCtlInst); H();
2249 InsertConfigString(pCfg, "Type", controllerString(enmCtrlType));
2250 /* Attach the status driver */
2251 Assert(cLedIde >= 4);
2252 i_attachStatusDriver(pCtlInst, &mapStorageLeds[iLedIde], 0, 3,
2253 &mapMediumAttachments, pszCtrlDev, ulInstance);
2254 paLedDevType = &maStorageDevType[iLedIde];
2255
2256 /* IDE flavors */
2257 aCtrlNodes[StorageControllerType_PIIX3] = pDev;
2258 aCtrlNodes[StorageControllerType_PIIX4] = pDev;
2259 aCtrlNodes[StorageControllerType_ICH6] = pDev;
2260 break;
2261 }
2262
2263 case StorageControllerType_I82078:
2264 {
2265 /*
2266 * i82078 Floppy drive controller
2267 */
2268 fFdcEnabled = true;
2269 InsertConfigInteger(pCfg, "IRQ", 6);
2270 InsertConfigInteger(pCfg, "DMA", 2);
2271 InsertConfigInteger(pCfg, "MemMapped", 0 );
2272 InsertConfigInteger(pCfg, "IOBase", 0x3f0);
2273
2274 /* Attach the status driver */
2275 Assert(cLedFloppy >= 2);
2276 i_attachStatusDriver(pCtlInst, &mapStorageLeds[iLedFloppy], 0, 1,
2277 &mapMediumAttachments, pszCtrlDev, ulInstance);
2278 paLedDevType = &maStorageDevType[iLedFloppy];
2279 break;
2280 }
2281
2282 case StorageControllerType_LsiLogicSas:
2283 {
2284 hrc = pBusMgr->assignPCIDevice("lsilogicsas", pCtlInst); H();
2285
2286 InsertConfigString(pCfg, "ControllerType", "SAS1068");
2287 InsertConfigInteger(pCfg, "Bootable", fBootable);
2288
2289 /* BIOS configuration values, first SCSI controller only. */
2290 if ( !pBusMgr->hasPCIDevice("lsilogic", 0)
2291 && !pBusMgr->hasPCIDevice("buslogic", 0)
2292 && !pBusMgr->hasPCIDevice("lsilogicsas", 1)
2293 && pBiosCfg)
2294 {
2295 InsertConfigString(pBiosCfg, "ScsiHardDiskDevice", "lsilogicsas");
2296 hrc = SetBiosDiskInfo(pMachine, pCfg, pBiosCfg, controllerName, apszBiosConfigScsi); H();
2297 }
2298
2299 ULONG cPorts = 0;
2300 hrc = ctrls[i]->COMGETTER(PortCount)(&cPorts); H();
2301 InsertConfigInteger(pCfg, "NumPorts", cPorts);
2302
2303 /* Attach the status driver */
2304 Assert(cLedSas >= 8);
2305 i_attachStatusDriver(pCtlInst, &mapStorageLeds[iLedSas], 0, 7,
2306 &mapMediumAttachments, pszCtrlDev, ulInstance);
2307 paLedDevType = &maStorageDevType[iLedSas];
2308 break;
2309 }
2310
2311 case StorageControllerType_USB:
2312 {
2313 if (pUsbDevices)
2314 {
2315 /*
2316 * USB MSDs are handled a bit different as the device instance
2317 * doesn't match the storage controller instance but the port.
2318 */
2319 InsertConfigNode(pUsbDevices, "Msd", &pDev);
2320 pCtlInst = pDev;
2321 }
2322 else
2323 return VMR3SetError(pUVM, VERR_NOT_FOUND, RT_SRC_POS,
2324 N_("There is no USB controller enabled but there\n"
2325 "is at least one USB storage device configured for this VM.\n"
2326 "To fix this problem either enable the USB controller or remove\n"
2327 "the storage device from the VM"));
2328 break;
2329 }
2330
2331 case StorageControllerType_NVMe:
2332 {
2333 hrc = pBusMgr->assignPCIDevice("nvme", pCtlInst); H();
2334
2335 ULONG cPorts = 0;
2336 hrc = ctrls[i]->COMGETTER(PortCount)(&cPorts); H();
2337 InsertConfigInteger(pCfg, "NamespacesMax", cPorts);
2338
2339 /* For ICH9 we need to create a new PCI bridge if there is more than one NVMe instance. */
2340 if ( ulInstance > 0
2341 && chipsetType == ChipsetType_ICH9
2342 && !pBusMgr->hasPCIDevice("ich9pcibridge", 2))
2343 {
2344 PCFGMNODE pBridges = CFGMR3GetChild(pDevices, "ich9pcibridge");
2345 Assert(pBridges);
2346
2347 InsertConfigNode(pBridges, "2", &pInst);
2348 InsertConfigInteger(pInst, "Trusted", 1);
2349 hrc = pBusMgr->assignPCIDevice("ich9pcibridge", pInst);
2350 }
2351
2352 /* Attach the status driver */
2353 AssertRelease(cPorts <= cLedSata);
2354 i_attachStatusDriver(pCtlInst, &mapStorageLeds[iLedNvme], 0, cPorts - 1,
2355 &mapMediumAttachments, pszCtrlDev, ulInstance);
2356 paLedDevType = &maStorageDevType[iLedNvme];
2357 break;
2358 }
2359
2360 default:
2361 AssertLogRelMsgFailedReturn(("invalid storage controller type: %d\n", enmCtrlType), VERR_MAIN_CONFIG_CONSTRUCTOR_IPE);
2362 }
2363
2364 /* Attach the media to the storage controllers. */
2365 com::SafeIfaceArray<IMediumAttachment> atts;
2366 hrc = pMachine->GetMediumAttachmentsOfController(controllerName.raw(),
2367 ComSafeArrayAsOutParam(atts)); H();
2368
2369 /* Builtin I/O cache - per device setting. */
2370 BOOL fBuiltinIOCache = true;
2371 hrc = pMachine->COMGETTER(IOCacheEnabled)(&fBuiltinIOCache); H();
2372
2373
2374 for (size_t j = 0; j < atts.size(); ++j)
2375 {
2376 IMediumAttachment *pMediumAtt = atts[j];
2377 rc = i_configMediumAttachment(pszCtrlDev,
2378 ulInstance,
2379 enmBus,
2380 !!fUseHostIOCache,
2381 enmCtrlType == StorageControllerType_NVMe ? false : !!fBuiltinIOCache,
2382 false /* fSetupMerge */,
2383 0 /* uMergeSource */,
2384 0 /* uMergeTarget */,
2385 pMediumAtt,
2386 mMachineState,
2387 NULL /* phrc */,
2388 false /* fAttachDetach */,
2389 false /* fForceUnmount */,
2390 false /* fHotplug */,
2391 pUVM,
2392 paLedDevType,
2393 NULL /* ppLunL0 */);
2394 if (RT_FAILURE(rc))
2395 return rc;
2396 }
2397 H();
2398 }
2399 H();
2400
2401 /*
2402 * Network adapters
2403 */
2404#ifdef VMWARE_NET_IN_SLOT_11
2405 bool fSwapSlots3and11 = false;
2406#endif
2407 PCFGMNODE pDevPCNet = NULL; /* PCNet-type devices */
2408 InsertConfigNode(pDevices, "pcnet", &pDevPCNet);
2409#ifdef VBOX_WITH_E1000
2410 PCFGMNODE pDevE1000 = NULL; /* E1000-type devices */
2411 InsertConfigNode(pDevices, "e1000", &pDevE1000);
2412#endif
2413#ifdef VBOX_WITH_VIRTIO
2414 PCFGMNODE pDevVirtioNet = NULL; /* Virtio network devices */
2415 InsertConfigNode(pDevices, "virtio-net", &pDevVirtioNet);
2416#endif /* VBOX_WITH_VIRTIO */
2417 std::list<BootNic> llBootNics;
2418 for (ULONG ulInstance = 0; ulInstance < maxNetworkAdapters; ++ulInstance)
2419 {
2420 ComPtr<INetworkAdapter> networkAdapter;
2421 hrc = pMachine->GetNetworkAdapter(ulInstance, networkAdapter.asOutParam()); H();
2422 BOOL fEnabledNetAdapter = FALSE;
2423 hrc = networkAdapter->COMGETTER(Enabled)(&fEnabledNetAdapter); H();
2424 if (!fEnabledNetAdapter)
2425 continue;
2426
2427 /*
2428 * The virtual hardware type. Create appropriate device first.
2429 */
2430 const char *pszAdapterName = "pcnet";
2431 NetworkAdapterType_T adapterType;
2432 hrc = networkAdapter->COMGETTER(AdapterType)(&adapterType); H();
2433 switch (adapterType)
2434 {
2435 case NetworkAdapterType_Am79C970A:
2436 case NetworkAdapterType_Am79C973:
2437 pDev = pDevPCNet;
2438 break;
2439#ifdef VBOX_WITH_E1000
2440 case NetworkAdapterType_I82540EM:
2441 case NetworkAdapterType_I82543GC:
2442 case NetworkAdapterType_I82545EM:
2443 pDev = pDevE1000;
2444 pszAdapterName = "e1000";
2445 break;
2446#endif
2447#ifdef VBOX_WITH_VIRTIO
2448 case NetworkAdapterType_Virtio:
2449 pDev = pDevVirtioNet;
2450 pszAdapterName = "virtio-net";
2451 break;
2452#endif /* VBOX_WITH_VIRTIO */
2453 default:
2454 AssertMsgFailed(("Invalid network adapter type '%d' for slot '%d'",
2455 adapterType, ulInstance));
2456 return VMR3SetError(pUVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
2457 N_("Invalid network adapter type '%d' for slot '%d'"),
2458 adapterType, ulInstance);
2459 }
2460
2461 InsertConfigNode(pDev, Utf8StrFmt("%u", ulInstance).c_str(), &pInst);
2462 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
2463 /* the first network card gets the PCI ID 3, the next 3 gets 8..10,
2464 * next 4 get 16..19. */
2465 int iPCIDeviceNo;
2466 switch (ulInstance)
2467 {
2468 case 0:
2469 iPCIDeviceNo = 3;
2470 break;
2471 case 1: case 2: case 3:
2472 iPCIDeviceNo = ulInstance - 1 + 8;
2473 break;
2474 case 4: case 5: case 6: case 7:
2475 iPCIDeviceNo = ulInstance - 4 + 16;
2476 break;
2477 default:
2478 /* auto assignment */
2479 iPCIDeviceNo = -1;
2480 break;
2481 }
2482#ifdef VMWARE_NET_IN_SLOT_11
2483 /*
2484 * Dirty hack for PCI slot compatibility with VMWare,
2485 * it assigns slot 0x11 to the first network controller.
2486 */
2487 if (iPCIDeviceNo == 3 && adapterType == NetworkAdapterType_I82545EM)
2488 {
2489 iPCIDeviceNo = 0x11;
2490 fSwapSlots3and11 = true;
2491 }
2492 else if (iPCIDeviceNo == 0x11 && fSwapSlots3and11)
2493 iPCIDeviceNo = 3;
2494#endif
2495 PCIBusAddress PCIAddr = PCIBusAddress(0, iPCIDeviceNo, 0);
2496 hrc = pBusMgr->assignPCIDevice(pszAdapterName, pInst, PCIAddr); H();
2497
2498 InsertConfigNode(pInst, "Config", &pCfg);
2499#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE /* not safe here yet. */ /** @todo Make PCNet ring-0 safe on 32-bit mac kernels! */
2500 if (pDev == pDevPCNet)
2501 {
2502 InsertConfigInteger(pCfg, "R0Enabled", false);
2503 }
2504#endif
2505 /*
2506 * Collect information needed for network booting and add it to the list.
2507 */
2508 BootNic nic;
2509
2510 nic.mInstance = ulInstance;
2511 /* Could be updated by reference, if auto assigned */
2512 nic.mPCIAddress = PCIAddr;
2513
2514 hrc = networkAdapter->COMGETTER(BootPriority)(&nic.mBootPrio); H();
2515
2516 llBootNics.push_back(nic);
2517
2518 /*
2519 * The virtual hardware type. PCNet supports two types, E1000 three,
2520 * but VirtIO only one.
2521 */
2522 switch (adapterType)
2523 {
2524 case NetworkAdapterType_Am79C970A:
2525 InsertConfigInteger(pCfg, "Am79C973", 0);
2526 break;
2527 case NetworkAdapterType_Am79C973:
2528 InsertConfigInteger(pCfg, "Am79C973", 1);
2529 break;
2530 case NetworkAdapterType_I82540EM:
2531 InsertConfigInteger(pCfg, "AdapterType", 0);
2532 break;
2533 case NetworkAdapterType_I82543GC:
2534 InsertConfigInteger(pCfg, "AdapterType", 1);
2535 break;
2536 case NetworkAdapterType_I82545EM:
2537 InsertConfigInteger(pCfg, "AdapterType", 2);
2538 break;
2539 case NetworkAdapterType_Virtio:
2540 break;
2541 case NetworkAdapterType_Null: AssertFailedBreak(); /* Shut up MSC */
2542 }
2543
2544 /*
2545 * Get the MAC address and convert it to binary representation
2546 */
2547 Bstr macAddr;
2548 hrc = networkAdapter->COMGETTER(MACAddress)(macAddr.asOutParam()); H();
2549 Assert(!macAddr.isEmpty());
2550 Utf8Str macAddrUtf8 = macAddr;
2551 char *macStr = (char*)macAddrUtf8.c_str();
2552 Assert(strlen(macStr) == 12);
2553 RTMAC Mac;
2554 RT_ZERO(Mac);
2555 char *pMac = (char*)&Mac;
2556 for (uint32_t i = 0; i < 6; ++i)
2557 {
2558 int c1 = *macStr++ - '0';
2559 if (c1 > 9)
2560 c1 -= 7;
2561 int c2 = *macStr++ - '0';
2562 if (c2 > 9)
2563 c2 -= 7;
2564 *pMac++ = (char)(((c1 & 0x0f) << 4) | (c2 & 0x0f));
2565 }
2566 InsertConfigBytes(pCfg, "MAC", &Mac, sizeof(Mac));
2567
2568 /*
2569 * Check if the cable is supposed to be unplugged
2570 */
2571 BOOL fCableConnected;
2572 hrc = networkAdapter->COMGETTER(CableConnected)(&fCableConnected); H();
2573 InsertConfigInteger(pCfg, "CableConnected", fCableConnected ? 1 : 0);
2574
2575 /*
2576 * Line speed to report from custom drivers
2577 */
2578 ULONG ulLineSpeed;
2579 hrc = networkAdapter->COMGETTER(LineSpeed)(&ulLineSpeed); H();
2580 InsertConfigInteger(pCfg, "LineSpeed", ulLineSpeed);
2581
2582 /*
2583 * Attach the status driver.
2584 */
2585 i_attachStatusDriver(pInst, &mapNetworkLeds[ulInstance], 0, 0, NULL, NULL, 0);
2586
2587 /*
2588 * Configure the network card now
2589 */
2590 bool fIgnoreConnectFailure = mMachineState == MachineState_Restoring;
2591 rc = i_configNetwork(pszAdapterName,
2592 ulInstance,
2593 0,
2594 networkAdapter,
2595 pCfg,
2596 pLunL0,
2597 pInst,
2598 false /*fAttachDetach*/,
2599 fIgnoreConnectFailure);
2600 if (RT_FAILURE(rc))
2601 return rc;
2602 }
2603
2604 /*
2605 * Build network boot information and transfer it to the BIOS.
2606 */
2607 if (pNetBootCfg && !llBootNics.empty()) /* NetBoot node doesn't exist for EFI! */
2608 {
2609 llBootNics.sort(); /* Sort the list by boot priority. */
2610
2611 char achBootIdx[] = "0";
2612 unsigned uBootIdx = 0;
2613
2614 for (std::list<BootNic>::iterator it = llBootNics.begin(); it != llBootNics.end(); ++it)
2615 {
2616 /* A NIC with priority 0 is only used if it's first in the list. */
2617 if (it->mBootPrio == 0 && uBootIdx != 0)
2618 break;
2619
2620 PCFGMNODE pNetBtDevCfg;
2621 achBootIdx[0] = (char)('0' + uBootIdx++); /* Boot device order. */
2622 InsertConfigNode(pNetBootCfg, achBootIdx, &pNetBtDevCfg);
2623 InsertConfigInteger(pNetBtDevCfg, "NIC", it->mInstance);
2624 InsertConfigInteger(pNetBtDevCfg, "PCIBusNo", it->mPCIAddress.miBus);
2625 InsertConfigInteger(pNetBtDevCfg, "PCIDeviceNo", it->mPCIAddress.miDevice);
2626 InsertConfigInteger(pNetBtDevCfg, "PCIFunctionNo", it->mPCIAddress.miFn);
2627 }
2628 }
2629
2630 /*
2631 * Serial (UART) Ports
2632 */
2633 /* serial enabled mask to be passed to dev ACPI */
2634 uint16_t auSerialIoPortBase[SchemaDefs::SerialPortCount] = {0};
2635 uint8_t auSerialIrq[SchemaDefs::SerialPortCount] = {0};
2636 InsertConfigNode(pDevices, "serial", &pDev);
2637 for (ULONG ulInstance = 0; ulInstance < SchemaDefs::SerialPortCount; ++ulInstance)
2638 {
2639 ComPtr<ISerialPort> serialPort;
2640 hrc = pMachine->GetSerialPort(ulInstance, serialPort.asOutParam()); H();
2641 BOOL fEnabledSerPort = FALSE;
2642 if (serialPort)
2643 {
2644 hrc = serialPort->COMGETTER(Enabled)(&fEnabledSerPort); H();
2645 }
2646 if (!fEnabledSerPort)
2647 continue;
2648
2649 InsertConfigNode(pDev, Utf8StrFmt("%u", ulInstance).c_str(), &pInst);
2650 InsertConfigNode(pInst, "Config", &pCfg);
2651
2652 ULONG ulIRQ;
2653 hrc = serialPort->COMGETTER(IRQ)(&ulIRQ); H();
2654 InsertConfigInteger(pCfg, "IRQ", ulIRQ);
2655 auSerialIrq[ulInstance] = (uint8_t)ulIRQ;
2656
2657 ULONG ulIOBase;
2658 hrc = serialPort->COMGETTER(IOBase)(&ulIOBase); H();
2659 InsertConfigInteger(pCfg, "IOBase", ulIOBase);
2660 auSerialIoPortBase[ulInstance] = (uint16_t)ulIOBase;
2661
2662 BOOL fServer;
2663 hrc = serialPort->COMGETTER(Server)(&fServer); H();
2664 hrc = serialPort->COMGETTER(Path)(bstr.asOutParam()); H();
2665 PortMode_T eHostMode;
2666 hrc = serialPort->COMGETTER(HostMode)(&eHostMode); H();
2667 if (eHostMode != PortMode_Disconnected)
2668 {
2669 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2670 if (eHostMode == PortMode_HostPipe)
2671 {
2672 InsertConfigString(pLunL0, "Driver", "Char");
2673 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
2674 InsertConfigString(pLunL1, "Driver", "NamedPipe");
2675 InsertConfigNode(pLunL1, "Config", &pLunL2);
2676 InsertConfigString(pLunL2, "Location", bstr);
2677 InsertConfigInteger(pLunL2, "IsServer", fServer);
2678 }
2679 else if (eHostMode == PortMode_HostDevice)
2680 {
2681 InsertConfigString(pLunL0, "Driver", "Host Serial");
2682 InsertConfigNode(pLunL0, "Config", &pLunL1);
2683 InsertConfigString(pLunL1, "DevicePath", bstr);
2684 }
2685 else if (eHostMode == PortMode_TCP)
2686 {
2687 InsertConfigString(pLunL0, "Driver", "Char");
2688 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
2689 InsertConfigString(pLunL1, "Driver", "TCP");
2690 InsertConfigNode(pLunL1, "Config", &pLunL2);
2691 InsertConfigString(pLunL2, "Location", bstr);
2692 InsertConfigInteger(pLunL2, "IsServer", fServer);
2693 }
2694 else if (eHostMode == PortMode_RawFile)
2695 {
2696 InsertConfigString(pLunL0, "Driver", "Char");
2697 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
2698 InsertConfigString(pLunL1, "Driver", "RawFile");
2699 InsertConfigNode(pLunL1, "Config", &pLunL2);
2700 InsertConfigString(pLunL2, "Location", bstr);
2701 }
2702 }
2703 }
2704
2705 /*
2706 * Parallel (LPT) Ports
2707 */
2708 /* parallel enabled mask to be passed to dev ACPI */
2709 uint16_t auParallelIoPortBase[SchemaDefs::ParallelPortCount] = {0};
2710 uint8_t auParallelIrq[SchemaDefs::ParallelPortCount] = {0};
2711 InsertConfigNode(pDevices, "parallel", &pDev);
2712 for (ULONG ulInstance = 0; ulInstance < SchemaDefs::ParallelPortCount; ++ulInstance)
2713 {
2714 ComPtr<IParallelPort> parallelPort;
2715 hrc = pMachine->GetParallelPort(ulInstance, parallelPort.asOutParam()); H();
2716 BOOL fEnabledParPort = FALSE;
2717 if (parallelPort)
2718 {
2719 hrc = parallelPort->COMGETTER(Enabled)(&fEnabledParPort); H();
2720 }
2721 if (!fEnabledParPort)
2722 continue;
2723
2724 InsertConfigNode(pDev, Utf8StrFmt("%u", ulInstance).c_str(), &pInst);
2725 InsertConfigNode(pInst, "Config", &pCfg);
2726
2727 ULONG ulIRQ;
2728 hrc = parallelPort->COMGETTER(IRQ)(&ulIRQ); H();
2729 InsertConfigInteger(pCfg, "IRQ", ulIRQ);
2730 auParallelIrq[ulInstance] = (uint8_t)ulIRQ;
2731 ULONG ulIOBase;
2732 hrc = parallelPort->COMGETTER(IOBase)(&ulIOBase); H();
2733 InsertConfigInteger(pCfg, "IOBase", ulIOBase);
2734 auParallelIoPortBase[ulInstance] = (uint16_t)ulIOBase;
2735
2736 hrc = parallelPort->COMGETTER(Path)(bstr.asOutParam()); H();
2737 if (!bstr.isEmpty())
2738 {
2739 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2740 InsertConfigString(pLunL0, "Driver", "HostParallel");
2741 InsertConfigNode(pLunL0, "Config", &pLunL1);
2742 InsertConfigString(pLunL1, "DevicePath", bstr);
2743 }
2744 }
2745
2746 /*
2747 * VMM Device
2748 */
2749 InsertConfigNode(pDevices, "VMMDev", &pDev);
2750 InsertConfigNode(pDev, "0", &pInst);
2751 InsertConfigNode(pInst, "Config", &pCfg);
2752 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
2753 hrc = pBusMgr->assignPCIDevice("VMMDev", pInst); H();
2754
2755 Bstr hwVersion;
2756 hrc = pMachine->COMGETTER(HardwareVersion)(hwVersion.asOutParam()); H();
2757 InsertConfigInteger(pCfg, "RamSize", cbRam);
2758 if (hwVersion.compare(Bstr("1").raw()) == 0) /* <= 2.0.x */
2759 InsertConfigInteger(pCfg, "HeapEnabled", 0);
2760 Bstr snapshotFolder;
2761 hrc = pMachine->COMGETTER(SnapshotFolder)(snapshotFolder.asOutParam()); H();
2762 InsertConfigString(pCfg, "GuestCoreDumpDir", snapshotFolder);
2763
2764 /* the VMM device's Main driver */
2765 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2766 InsertConfigString(pLunL0, "Driver", "HGCM");
2767 InsertConfigNode(pLunL0, "Config", &pCfg);
2768 InsertConfigInteger(pCfg, "Object", (uintptr_t)pVMMDev);
2769
2770 /*
2771 * Attach the status driver.
2772 */
2773 i_attachStatusDriver(pInst, &mapSharedFolderLed, 0, 0, NULL, NULL, 0);
2774
2775 /*
2776 * AC'97 ICH / SoundBlaster16 audio / Intel HD Audio.
2777 */
2778 BOOL fAudioEnabled = FALSE;
2779 ComPtr<IAudioAdapter> audioAdapter;
2780 hrc = pMachine->COMGETTER(AudioAdapter)(audioAdapter.asOutParam()); H();
2781 if (audioAdapter)
2782 {
2783 hrc = audioAdapter->COMGETTER(Enabled)(&fAudioEnabled); H();
2784 }
2785
2786 if (fAudioEnabled)
2787 {
2788 AudioControllerType_T audioController;
2789 hrc = audioAdapter->COMGETTER(AudioController)(&audioController); H();
2790 AudioCodecType_T audioCodec;
2791 hrc = audioAdapter->COMGETTER(AudioCodec)(&audioCodec); H();
2792 switch (audioController)
2793 {
2794 case AudioControllerType_AC97:
2795 {
2796 /* Default: ICH AC97. */
2797 InsertConfigNode(pDevices, "ichac97", &pDev);
2798 InsertConfigNode(pDev, "0", &pInst);
2799 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
2800 hrc = pBusMgr->assignPCIDevice("ichac97", pInst); H();
2801 InsertConfigNode(pInst, "Config", &pCfg);
2802 switch (audioCodec)
2803 {
2804 case AudioCodecType_STAC9700:
2805 InsertConfigString(pCfg, "Codec", "STAC9700");
2806 break;
2807 case AudioCodecType_AD1980:
2808 InsertConfigString(pCfg, "Codec", "AD1980");
2809 break;
2810 default: AssertFailedBreak();
2811 }
2812 break;
2813 }
2814 case AudioControllerType_SB16:
2815 {
2816 /* Legacy SoundBlaster16. */
2817 InsertConfigNode(pDevices, "sb16", &pDev);
2818 InsertConfigNode(pDev, "0", &pInst);
2819 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
2820 InsertConfigNode(pInst, "Config", &pCfg);
2821 InsertConfigInteger(pCfg, "IRQ", 5);
2822 InsertConfigInteger(pCfg, "DMA", 1);
2823 InsertConfigInteger(pCfg, "DMA16", 5);
2824 InsertConfigInteger(pCfg, "Port", 0x220);
2825 InsertConfigInteger(pCfg, "Version", 0x0405);
2826 break;
2827 }
2828 case AudioControllerType_HDA:
2829 {
2830 /* Intel HD Audio. */
2831 InsertConfigNode(pDevices, "hda", &pDev);
2832 InsertConfigNode(pDev, "0", &pInst);
2833 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
2834 hrc = pBusMgr->assignPCIDevice("hda", pInst); H();
2835 InsertConfigNode(pInst, "Config", &pCfg);
2836 }
2837 }
2838
2839 PCFGMNODE pCfgAudioSettings = NULL;
2840 InsertConfigNode(pInst, "AudioConfig", &pCfgAudioSettings);
2841 SafeArray<BSTR> audioProps;
2842 hrc = audioAdapter->COMGETTER(PropertiesList)(ComSafeArrayAsOutParam(audioProps)); H();
2843
2844 std::list<Utf8Str> audioPropertyNamesList;
2845 for (size_t i = 0; i < audioProps.size(); ++i)
2846 {
2847 Bstr bstrValue;
2848 audioPropertyNamesList.push_back(Utf8Str(audioProps[i]));
2849 hrc = audioAdapter->GetProperty(audioProps[i], bstrValue.asOutParam());
2850 Utf8Str strKey(audioProps[i]);
2851 InsertConfigString(pCfgAudioSettings, strKey.c_str(), bstrValue);
2852 }
2853
2854 /*
2855 * The audio driver.
2856 */
2857 uint8_t u8AudioLUN = 0;
2858
2859 CFGMR3InsertNodeF(pInst, &pLunL0, "LUN#%RU8", u8AudioLUN++);
2860 InsertConfigString(pLunL0, "Driver", "AUDIO");
2861 InsertConfigNode(pLunL0, "Config", &pCfg);
2862
2863 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
2864 InsertConfigNode(pLunL1, "Config", &pCfg);
2865
2866 hrc = pMachine->COMGETTER(Name)(bstr.asOutParam()); H();
2867 InsertConfigString(pCfg, "StreamName", bstr);
2868
2869 AudioDriverType_T audioDriver;
2870 hrc = audioAdapter->COMGETTER(AudioDriver)(&audioDriver); H();
2871 switch (audioDriver)
2872 {
2873 case AudioDriverType_Null:
2874 {
2875 InsertConfigString(pLunL1, "Driver", "NullAudio");
2876 break;
2877 }
2878#ifdef RT_OS_WINDOWS
2879# ifdef VBOX_WITH_WINMM
2880 case AudioDriverType_WinMM:
2881 {
2882 #error "Port WinMM audio backend!" /** @todo Still needed? */
2883 break;
2884 }
2885# endif
2886 case AudioDriverType_DirectSound:
2887 {
2888 InsertConfigString(pLunL1, "Driver", "DSoundAudio");
2889 break;
2890 }
2891#endif /* RT_OS_WINDOWS */
2892#ifdef RT_OS_SOLARIS
2893 case AudioDriverType_SolAudio:
2894 {
2895 /* Should not happen, as the Solaris Audio backend is not around anymore.
2896 * Remove this sometime later. */
2897 LogRel(("Audio: WARNING: Solaris Audio is deprecated, please switch to OSS!\n"));
2898 LogRel(("Audio: Automatically setting host audio backend to OSS\n"));
2899
2900 /* Manually set backend to OSS for now. */
2901 InsertConfigString(pLunL1, "Driver", "OSSAudio");
2902 break;
2903 }
2904#endif
2905#ifdef VBOX_WITH_AUDIO_OSS
2906 case AudioDriverType_OSS:
2907 {
2908 InsertConfigString(pLunL1, "Driver", "OSSAudio");
2909 break;
2910 }
2911#endif
2912#ifdef VBOX_WITH_AUDIO_ALSA
2913 case AudioDriverType_ALSA:
2914 {
2915 InsertConfigString(pLunL1, "Driver", "ALSAAudio");
2916 break;
2917 }
2918#endif
2919#ifdef VBOX_WITH_AUDIO_PULSE
2920 case AudioDriverType_Pulse:
2921 {
2922 InsertConfigString(pLunL1, "Driver", "PulseAudio");
2923 break;
2924 }
2925#endif
2926#ifdef RT_OS_DARWIN
2927 case AudioDriverType_CoreAudio:
2928 {
2929 InsertConfigString(pLunL1, "Driver", "CoreAudio");
2930 break;
2931 }
2932#endif
2933 default: AssertFailedBreak();
2934 }
2935
2936 /** @todo Implement a prioritizing feature here, e.g. which driver to insert in which order? */
2937
2938#ifdef VBOX_WITH_VRDE_AUDIO
2939 /*
2940 * The VRDE audio backend driver.
2941 */
2942 CFGMR3InsertNodeF(pInst, &pLunL1, "LUN#%RU8", u8AudioLUN++);
2943 InsertConfigString(pLunL1, "Driver", "AUDIO");
2944
2945 InsertConfigNode(pLunL1, "AttachedDriver", &pLunL1);
2946 InsertConfigString(pLunL1, "Driver", "AudioVRDE");
2947
2948 InsertConfigNode(pLunL1, "Config", &pCfg);
2949 InsertConfigString(pCfg, "AudioDriver", "AudioVRDE");
2950 InsertConfigString(pCfg, "StreamName", bstr);
2951 InsertConfigInteger(pCfg, "Object", (uintptr_t)mAudioVRDE);
2952 InsertConfigInteger(pCfg, "ObjectVRDPServer", (uintptr_t)mConsoleVRDPServer);
2953#endif /* VBOX_WITH_VRDE_AUDIO */
2954
2955#ifdef VBOX_WITH_AUDIO_VIDEOREC
2956 /*
2957 * The video recording audio backend driver.
2958 * Currently being used with VPX video recording only.
2959 */
2960 CFGMR3InsertNodeF(pInst, &pLunL1, "LUN#%RU8", u8AudioLUN++);
2961 InsertConfigString(pLunL1, "Driver", "AUDIO");
2962
2963 InsertConfigNode(pLunL1, "AttachedDriver", &pLunL1);
2964 InsertConfigString(pLunL1, "Driver", "AudioVideoRec");
2965
2966 InsertConfigNode(pLunL1, "Config", &pCfg);
2967 InsertConfigString(pCfg, "AudioDriver", "AudioVideoRec");
2968 InsertConfigString(pCfg, "StreamName", bstr);
2969 InsertConfigInteger(pCfg, "Object", (uintptr_t)mAudioVideoRec);
2970#endif /* VBOX_WITH_AUDIO_VIDEOREC */
2971
2972#ifdef VBOX_WITH_AUDIO_DEBUG
2973 /*
2974 * The audio debug backend. Only can be used in debug builds.
2975 */
2976 CFGMR3InsertNodeF(pInst, &pLunL1, "LUN#%RU8", u8AudioLUN++);
2977 InsertConfigString(pLunL1, "Driver", "AUDIO");
2978
2979 InsertConfigNode(pLunL1, "AttachedDriver", &pLunL1);
2980 InsertConfigString(pLunL1, "Driver", "DebugAudio");
2981
2982 InsertConfigNode(pLunL1, "Config", &pCfg);
2983 InsertConfigString(pCfg, "AudioDriver", "DebugAudio");
2984 InsertConfigString(pCfg, "StreamName", bstr);
2985#endif /* VBOX_WITH_AUDIO_DEBUG */
2986
2987#ifdef VBOX_WITH_AUDIO_VALIDATIONKIT
2988 /** @todo Make this a runtime-configurable entry! */
2989
2990 /*
2991 * The ValidationKit backend.
2992 */
2993 CFGMR3InsertNodeF(pInst, &pLunL1, "LUN#%RU8", u8AudioLUN++);
2994 InsertConfigString(pLunL1, "Driver", "AUDIO");
2995
2996 InsertConfigNode(pLunL1, "AttachedDriver", &pLunL1);
2997 InsertConfigString(pLunL1, "Driver", "ValidationKitAudio");
2998
2999 InsertConfigNode(pLunL1, "Config", &pCfg);
3000 InsertConfigString(pCfg, "AudioDriver", "ValidationKitAudio");
3001 InsertConfigString(pCfg, "StreamName", bstr);
3002#endif /* VBOX_WITH_AUDIO_VALIDATIONKIT */
3003
3004 /** @todo Add audio video recording driver here. */
3005 }
3006
3007 /*
3008 * Shared Clipboard.
3009 */
3010 {
3011 ClipboardMode_T mode = ClipboardMode_Disabled;
3012 hrc = pMachine->COMGETTER(ClipboardMode)(&mode); H();
3013
3014 if (/* mode != ClipboardMode_Disabled */ true)
3015 {
3016 /* Load the service */
3017 rc = pVMMDev->hgcmLoadService("VBoxSharedClipboard", "VBoxSharedClipboard");
3018 if (RT_FAILURE(rc))
3019 {
3020 LogRel(("Shared clipboard is not available, rc=%Rrc\n", rc));
3021 /* That is not a fatal failure. */
3022 rc = VINF_SUCCESS;
3023 }
3024 else
3025 {
3026 LogRel(("Shared clipboard service loaded\n"));
3027
3028 i_changeClipboardMode(mode);
3029
3030 /* Setup the service. */
3031 VBOXHGCMSVCPARM parm;
3032 parm.type = VBOX_HGCM_SVC_PARM_32BIT;
3033 parm.setUInt32(!i_useHostClipboard());
3034 pVMMDev->hgcmHostCall("VBoxSharedClipboard",
3035 VBOX_SHARED_CLIPBOARD_HOST_FN_SET_HEADLESS, 1, &parm);
3036 }
3037 }
3038 }
3039
3040 /*
3041 * HGCM HostChannel.
3042 */
3043 {
3044 Bstr value;
3045 hrc = pMachine->GetExtraData(Bstr("HGCM/HostChannel").raw(),
3046 value.asOutParam());
3047
3048 if ( hrc == S_OK
3049 && value == "1")
3050 {
3051 rc = pVMMDev->hgcmLoadService("VBoxHostChannel", "VBoxHostChannel");
3052 if (RT_FAILURE(rc))
3053 {
3054 LogRel(("VBoxHostChannel is not available, rc=%Rrc\n", rc));
3055 /* That is not a fatal failure. */
3056 rc = VINF_SUCCESS;
3057 }
3058 }
3059 }
3060
3061#ifdef VBOX_WITH_DRAG_AND_DROP
3062 /*
3063 * Drag and Drop.
3064 */
3065 {
3066 DnDMode_T enmMode = DnDMode_Disabled;
3067 hrc = pMachine->COMGETTER(DnDMode)(&enmMode); H();
3068
3069 /* Load the service */
3070 rc = pVMMDev->hgcmLoadService("VBoxDragAndDropSvc", "VBoxDragAndDropSvc");
3071 if (RT_FAILURE(rc))
3072 {
3073 LogRel(("Drag and drop service is not available, rc=%Rrc\n", rc));
3074 /* That is not a fatal failure. */
3075 rc = VINF_SUCCESS;
3076 }
3077 else
3078 {
3079 HGCMSVCEXTHANDLE hDummy;
3080 rc = HGCMHostRegisterServiceExtension(&hDummy, "VBoxDragAndDropSvc",
3081 &GuestDnD::notifyDnDDispatcher,
3082 GuestDnDInst());
3083 if (RT_FAILURE(rc))
3084 Log(("Cannot register VBoxDragAndDropSvc extension, rc=%Rrc\n", rc));
3085 else
3086 {
3087 LogRel(("Drag and drop service loaded\n"));
3088 rc = i_changeDnDMode(enmMode);
3089 }
3090 }
3091 }
3092#endif /* VBOX_WITH_DRAG_AND_DROP */
3093
3094#ifdef VBOX_WITH_CROGL
3095 /*
3096 * crOpenGL.
3097 */
3098 {
3099 BOOL fEnabled3D = false;
3100 hrc = pMachine->COMGETTER(Accelerate3DEnabled)(&fEnabled3D); H();
3101
3102 if ( fEnabled3D
3103# ifdef VBOX_WITH_VMSVGA3D
3104 && enmGraphicsController == GraphicsControllerType_VBoxVGA
3105# endif
3106 )
3107 {
3108 BOOL fSupports3D = VBoxOglIs3DAccelerationSupported();
3109 if (!fSupports3D)
3110 return VMR3SetError(pUVM, VERR_NOT_AVAILABLE, RT_SRC_POS,
3111 N_("This VM was configured to use 3D acceleration. However, the "
3112 "3D support of the host is not working properly and the "
3113 "VM cannot be started. To fix this problem, either "
3114 "fix the host 3D support (update the host graphics driver?) "
3115 "or disable 3D acceleration in the VM settings"));
3116
3117 /* Load the service. */
3118 rc = pVMMDev->hgcmLoadService("VBoxSharedCrOpenGL", "VBoxSharedCrOpenGL");
3119 if (RT_FAILURE(rc))
3120 {
3121 LogRel(("Failed to load Shared OpenGL service, rc=%Rrc\n", rc));
3122 /* That is not a fatal failure. */
3123 rc = VINF_SUCCESS;
3124 }
3125 else
3126 {
3127 LogRel(("Shared OpenGL service loaded -- 3D enabled\n"));
3128
3129 /* Setup the service. */
3130 VBOXHGCMSVCPARM parm;
3131 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3132
3133 parm.u.pointer.addr = (IConsole *)(Console *)this;
3134 parm.u.pointer.size = sizeof(IConsole *);
3135
3136 rc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_SET_CONSOLE,
3137 SHCRGL_CPARMS_SET_CONSOLE, &parm);
3138 if (!RT_SUCCESS(rc))
3139 AssertMsgFailed(("SHCRGL_HOST_FN_SET_CONSOLE failed with %Rrc\n", rc));
3140
3141 parm.u.pointer.addr = pVM;
3142 parm.u.pointer.size = sizeof(pVM);
3143 rc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL",
3144 SHCRGL_HOST_FN_SET_VM, SHCRGL_CPARMS_SET_VM, &parm);
3145 if (!RT_SUCCESS(rc))
3146 AssertMsgFailed(("SHCRGL_HOST_FN_SET_VM failed with %Rrc\n", rc));
3147 }
3148 }
3149 }
3150#endif
3151
3152#ifdef VBOX_WITH_GUEST_PROPS
3153 /*
3154 * Guest property service.
3155 */
3156 rc = i_configGuestProperties(this, pUVM);
3157#endif /* VBOX_WITH_GUEST_PROPS defined */
3158
3159#ifdef VBOX_WITH_GUEST_CONTROL
3160 /*
3161 * Guest control service.
3162 */
3163 rc = i_configGuestControl(this);
3164#endif /* VBOX_WITH_GUEST_CONTROL defined */
3165
3166 /*
3167 * ACPI
3168 */
3169 BOOL fACPI;
3170 hrc = biosSettings->COMGETTER(ACPIEnabled)(&fACPI); H();
3171 if (fACPI)
3172 {
3173 /* Always show the CPU leafs when we have multiple VCPUs or when the IO-APIC is enabled.
3174 * The Windows SMP kernel needs a CPU leaf or else its idle loop will burn cpu cycles; the
3175 * intelppm driver refuses to register an idle state handler.
3176 * Always show CPU leafs for OS X guests. */
3177 BOOL fShowCpu = fOsXGuest;
3178 if (cCpus > 1 || fIOAPIC)
3179 fShowCpu = true;
3180
3181 BOOL fCpuHotPlug;
3182 hrc = pMachine->COMGETTER(CPUHotPlugEnabled)(&fCpuHotPlug); H();
3183
3184 InsertConfigNode(pDevices, "acpi", &pDev);
3185 InsertConfigNode(pDev, "0", &pInst);
3186 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
3187 InsertConfigNode(pInst, "Config", &pCfg);
3188 hrc = pBusMgr->assignPCIDevice("acpi", pInst); H();
3189
3190 InsertConfigInteger(pCfg, "RamSize", cbRam);
3191 InsertConfigInteger(pCfg, "RamHoleSize", cbRamHole);
3192 InsertConfigInteger(pCfg, "NumCPUs", cCpus);
3193
3194 InsertConfigInteger(pCfg, "IOAPIC", fIOAPIC);
3195 InsertConfigInteger(pCfg, "FdcEnabled", fFdcEnabled);
3196 InsertConfigInteger(pCfg, "HpetEnabled", fHPETEnabled);
3197 InsertConfigInteger(pCfg, "SmcEnabled", fSmcEnabled);
3198 InsertConfigInteger(pCfg, "ShowRtc", fShowRtc);
3199 if (fOsXGuest && !llBootNics.empty())
3200 {
3201 BootNic aNic = llBootNics.front();
3202 uint32_t u32NicPCIAddr = (aNic.mPCIAddress.miDevice << 16) | aNic.mPCIAddress.miFn;
3203 InsertConfigInteger(pCfg, "NicPciAddress", u32NicPCIAddr);
3204 }
3205 if (fOsXGuest && fAudioEnabled)
3206 {
3207 PCIBusAddress Address;
3208 if (pBusMgr->findPCIAddress("hda", 0, Address))
3209 {
3210 uint32_t u32AudioPCIAddr = (Address.miDevice << 16) | Address.miFn;
3211 InsertConfigInteger(pCfg, "AudioPciAddress", u32AudioPCIAddr);
3212 }
3213 }
3214 InsertConfigInteger(pCfg, "IocPciAddress", uIocPCIAddress);
3215 if (chipsetType == ChipsetType_ICH9)
3216 {
3217 InsertConfigInteger(pCfg, "McfgBase", uMcfgBase);
3218 InsertConfigInteger(pCfg, "McfgLength", cbMcfgLength);
3219 }
3220 InsertConfigInteger(pCfg, "HostBusPciAddress", uHbcPCIAddress);
3221 InsertConfigInteger(pCfg, "ShowCpu", fShowCpu);
3222 InsertConfigInteger(pCfg, "CpuHotPlug", fCpuHotPlug);
3223
3224 InsertConfigInteger(pCfg, "Serial0IoPortBase", auSerialIoPortBase[0]);
3225 InsertConfigInteger(pCfg, "Serial0Irq", auSerialIrq[0]);
3226
3227 InsertConfigInteger(pCfg, "Serial1IoPortBase", auSerialIoPortBase[1]);
3228 InsertConfigInteger(pCfg, "Serial1Irq", auSerialIrq[1]);
3229
3230 if (auSerialIoPortBase[2])
3231 {
3232 InsertConfigInteger(pCfg, "Serial2IoPortBase", auSerialIoPortBase[2]);
3233 InsertConfigInteger(pCfg, "Serial2Irq", auSerialIrq[2]);
3234 }
3235
3236 if (auSerialIoPortBase[3])
3237 {
3238 InsertConfigInteger(pCfg, "Serial3IoPortBase", auSerialIoPortBase[3]);
3239 InsertConfigInteger(pCfg, "Serial3Irq", auSerialIrq[3]);
3240 }
3241
3242 InsertConfigInteger(pCfg, "Parallel0IoPortBase", auParallelIoPortBase[0]);
3243 InsertConfigInteger(pCfg, "Parallel0Irq", auParallelIrq[0]);
3244
3245 InsertConfigInteger(pCfg, "Parallel1IoPortBase", auParallelIoPortBase[1]);
3246 InsertConfigInteger(pCfg, "Parallel1Irq", auParallelIrq[1]);
3247
3248 InsertConfigNode(pInst, "LUN#0", &pLunL0);
3249 InsertConfigString(pLunL0, "Driver", "ACPIHost");
3250 InsertConfigNode(pLunL0, "Config", &pCfg);
3251
3252 /* Attach the dummy CPU drivers */
3253 for (ULONG iCpuCurr = 1; iCpuCurr < cCpus; iCpuCurr++)
3254 {
3255 BOOL fCpuAttached = true;
3256
3257 if (fCpuHotPlug)
3258 {
3259 hrc = pMachine->GetCPUStatus(iCpuCurr, &fCpuAttached); H();
3260 }
3261
3262 if (fCpuAttached)
3263 {
3264 InsertConfigNode(pInst, Utf8StrFmt("LUN#%u", iCpuCurr).c_str(), &pLunL0);
3265 InsertConfigString(pLunL0, "Driver", "ACPICpu");
3266 InsertConfigNode(pLunL0, "Config", &pCfg);
3267 }
3268 }
3269 }
3270
3271 /*
3272 * Configure DBGF (Debug(ger) Facility) and DBGC (Debugger Console).
3273 */
3274 {
3275 PCFGMNODE pDbgf;
3276 InsertConfigNode(pRoot, "DBGF", &pDbgf);
3277
3278 /* Paths to search for debug info and such things. */
3279 hrc = pMachine->COMGETTER(SettingsFilePath)(bstr.asOutParam()); H();
3280 Utf8Str strSettingsPath(bstr);
3281 bstr.setNull();
3282 strSettingsPath.stripFilename();
3283 strSettingsPath.append("/");
3284
3285 char szHomeDir[RTPATH_MAX + 1];
3286 int rc2 = RTPathUserHome(szHomeDir, sizeof(szHomeDir) - 1);
3287 if (RT_FAILURE(rc2))
3288 szHomeDir[0] = '\0';
3289 RTPathEnsureTrailingSeparator(szHomeDir, sizeof(szHomeDir));
3290
3291
3292 Utf8Str strPath;
3293 strPath.append(strSettingsPath).append("debug/;");
3294 strPath.append(strSettingsPath).append(";");
3295 strPath.append(szHomeDir);
3296
3297 InsertConfigString(pDbgf, "Path", strPath.c_str());
3298
3299 /* Tracing configuration. */
3300 BOOL fTracingEnabled;
3301 hrc = pMachine->COMGETTER(TracingEnabled)(&fTracingEnabled); H();
3302 if (fTracingEnabled)
3303 InsertConfigInteger(pDbgf, "TracingEnabled", 1);
3304
3305 hrc = pMachine->COMGETTER(TracingConfig)(bstr.asOutParam()); H();
3306 if (fTracingEnabled)
3307 InsertConfigString(pDbgf, "TracingConfig", bstr);
3308
3309 BOOL fAllowTracingToAccessVM;
3310 hrc = pMachine->COMGETTER(AllowTracingToAccessVM)(&fAllowTracingToAccessVM); H();
3311 if (fAllowTracingToAccessVM)
3312 InsertConfigInteger(pPDM, "AllowTracingToAccessVM", 1);
3313
3314 /* Debugger console config. */
3315 PCFGMNODE pDbgc;
3316 InsertConfigNode(pRoot, "DBGC", &pDbgc);
3317
3318 hrc = virtualBox->COMGETTER(HomeFolder)(bstr.asOutParam()); H();
3319 Utf8Str strVBoxHome = bstr;
3320 bstr.setNull();
3321 if (strVBoxHome.isNotEmpty())
3322 strVBoxHome.append("/");
3323 else
3324 {
3325 strVBoxHome = szHomeDir;
3326 strVBoxHome.append("/.vbox");
3327 }
3328
3329 Utf8Str strFile(strVBoxHome);
3330 strFile.append("dbgc-history");
3331 InsertConfigString(pDbgc, "HistoryFile", strFile);
3332
3333 strFile = strSettingsPath;
3334 strFile.append("dbgc-init");
3335 InsertConfigString(pDbgc, "LocalInitScript", strFile);
3336
3337 strFile = strVBoxHome;
3338 strFile.append("dbgc-init");
3339 InsertConfigString(pDbgc, "GlobalInitScript", strFile);
3340 }
3341 }
3342 catch (ConfigError &x)
3343 {
3344 // InsertConfig threw something:
3345 return x.m_vrc;
3346 }
3347 catch (HRESULT hrcXcpt)
3348 {
3349 AssertLogRelMsgFailedReturn(("hrc=%Rhrc\n", hrcXcpt), VERR_MAIN_CONFIG_CONSTRUCTOR_COM_ERROR);
3350 }
3351
3352#ifdef VBOX_WITH_EXTPACK
3353 /*
3354 * Call the extension pack hooks if everything went well thus far.
3355 */
3356 if (RT_SUCCESS(rc))
3357 {
3358 pAlock->release();
3359 rc = mptrExtPackManager->i_callAllVmConfigureVmmHooks(this, pVM);
3360 pAlock->acquire();
3361 }
3362#endif
3363
3364 /*
3365 * Apply the CFGM overlay.
3366 */
3367 if (RT_SUCCESS(rc))
3368 rc = i_configCfgmOverlay(pRoot, virtualBox, pMachine);
3369
3370 /*
3371 * Dump all extradata API settings tweaks, both global and per VM.
3372 */
3373 if (RT_SUCCESS(rc))
3374 rc = i_configDumpAPISettingsTweaks(virtualBox, pMachine);
3375
3376#undef H
3377
3378 pAlock->release(); /* Avoid triggering the lock order inversion check. */
3379
3380 /*
3381 * Register VM state change handler.
3382 */
3383 int rc2 = VMR3AtStateRegister(pUVM, Console::i_vmstateChangeCallback, this);
3384 AssertRC(rc2);
3385 if (RT_SUCCESS(rc))
3386 rc = rc2;
3387
3388 /*
3389 * Register VM runtime error handler.
3390 */
3391 rc2 = VMR3AtRuntimeErrorRegister(pUVM, Console::i_atVMRuntimeErrorCallback, this);
3392 AssertRC(rc2);
3393 if (RT_SUCCESS(rc))
3394 rc = rc2;
3395
3396 pAlock->acquire();
3397
3398 LogFlowFunc(("vrc = %Rrc\n", rc));
3399 LogFlowFuncLeave();
3400
3401 return rc;
3402}
3403
3404/**
3405 * Applies the CFGM overlay as specified by VBoxInternal/XXX extra data
3406 * values.
3407 *
3408 * @returns VBox status code.
3409 * @param pRoot The root of the configuration tree.
3410 * @param pVirtualBox Pointer to the IVirtualBox interface.
3411 * @param pMachine Pointer to the IMachine interface.
3412 */
3413/* static */
3414int Console::i_configCfgmOverlay(PCFGMNODE pRoot, IVirtualBox *pVirtualBox, IMachine *pMachine)
3415{
3416 /*
3417 * CFGM overlay handling.
3418 *
3419 * Here we check the extra data entries for CFGM values
3420 * and create the nodes and insert the values on the fly. Existing
3421 * values will be removed and reinserted. CFGM is typed, so by default
3422 * we will guess whether it's a string or an integer (byte arrays are
3423 * not currently supported). It's possible to override this autodetection
3424 * by adding "string:", "integer:" or "bytes:" (future).
3425 *
3426 * We first perform a run on global extra data, then on the machine
3427 * extra data to support global settings with local overrides.
3428 */
3429 int rc = VINF_SUCCESS;
3430 try
3431 {
3432 /** @todo add support for removing nodes and byte blobs. */
3433 /*
3434 * Get the next key
3435 */
3436 SafeArray<BSTR> aGlobalExtraDataKeys;
3437 SafeArray<BSTR> aMachineExtraDataKeys;
3438 HRESULT hrc = pVirtualBox->GetExtraDataKeys(ComSafeArrayAsOutParam(aGlobalExtraDataKeys));
3439 AssertMsg(SUCCEEDED(hrc), ("VirtualBox::GetExtraDataKeys failed with %Rhrc\n", hrc));
3440
3441 // remember the no. of global values so we can call the correct method below
3442 size_t cGlobalValues = aGlobalExtraDataKeys.size();
3443
3444 hrc = pMachine->GetExtraDataKeys(ComSafeArrayAsOutParam(aMachineExtraDataKeys));
3445 AssertMsg(SUCCEEDED(hrc), ("Machine::GetExtraDataKeys failed with %Rhrc\n", hrc));
3446
3447 // build a combined list from global keys...
3448 std::list<Utf8Str> llExtraDataKeys;
3449
3450 for (size_t i = 0; i < aGlobalExtraDataKeys.size(); ++i)
3451 llExtraDataKeys.push_back(Utf8Str(aGlobalExtraDataKeys[i]));
3452 // ... and machine keys
3453 for (size_t i = 0; i < aMachineExtraDataKeys.size(); ++i)
3454 llExtraDataKeys.push_back(Utf8Str(aMachineExtraDataKeys[i]));
3455
3456 size_t i2 = 0;
3457 for (std::list<Utf8Str>::const_iterator it = llExtraDataKeys.begin();
3458 it != llExtraDataKeys.end();
3459 ++it, ++i2)
3460 {
3461 const Utf8Str &strKey = *it;
3462
3463 /*
3464 * We only care about keys starting with "VBoxInternal/" (skip "G:" or "M:")
3465 */
3466 if (!strKey.startsWith("VBoxInternal/"))
3467 continue;
3468
3469 const char *pszExtraDataKey = strKey.c_str() + sizeof("VBoxInternal/") - 1;
3470
3471 // get the value
3472 Bstr bstrExtraDataValue;
3473 if (i2 < cGlobalValues)
3474 // this is still one of the global values:
3475 hrc = pVirtualBox->GetExtraData(Bstr(strKey).raw(),
3476 bstrExtraDataValue.asOutParam());
3477 else
3478 hrc = pMachine->GetExtraData(Bstr(strKey).raw(),
3479 bstrExtraDataValue.asOutParam());
3480 if (FAILED(hrc))
3481 LogRel(("Warning: Cannot get extra data key %s, rc = %Rhrc\n", strKey.c_str(), hrc));
3482
3483 /*
3484 * The key will be in the format "Node1/Node2/Value" or simply "Value".
3485 * Split the two and get the node, delete the value and create the node
3486 * if necessary.
3487 */
3488 PCFGMNODE pNode;
3489 const char *pszCFGMValueName = strrchr(pszExtraDataKey, '/');
3490 if (pszCFGMValueName)
3491 {
3492 /* terminate the node and advance to the value (Utf8Str might not
3493 offically like this but wtf) */
3494 *(char*)pszCFGMValueName = '\0';
3495 ++pszCFGMValueName;
3496
3497 /* does the node already exist? */
3498 pNode = CFGMR3GetChild(pRoot, pszExtraDataKey);
3499 if (pNode)
3500 CFGMR3RemoveValue(pNode, pszCFGMValueName);
3501 else
3502 {
3503 /* create the node */
3504 rc = CFGMR3InsertNode(pRoot, pszExtraDataKey, &pNode);
3505 if (RT_FAILURE(rc))
3506 {
3507 AssertLogRelMsgRC(rc, ("failed to insert node '%s'\n", pszExtraDataKey));
3508 continue;
3509 }
3510 Assert(pNode);
3511 }
3512 }
3513 else
3514 {
3515 /* root value (no node path). */
3516 pNode = pRoot;
3517 pszCFGMValueName = pszExtraDataKey;
3518 pszExtraDataKey--;
3519 CFGMR3RemoveValue(pNode, pszCFGMValueName);
3520 }
3521
3522 /*
3523 * Now let's have a look at the value.
3524 * Empty strings means that we should remove the value, which we've
3525 * already done above.
3526 */
3527 Utf8Str strCFGMValueUtf8(bstrExtraDataValue);
3528 if (!strCFGMValueUtf8.isEmpty())
3529 {
3530 uint64_t u64Value;
3531
3532 /* check for type prefix first. */
3533 if (!strncmp(strCFGMValueUtf8.c_str(), RT_STR_TUPLE("string:")))
3534 InsertConfigString(pNode, pszCFGMValueName, strCFGMValueUtf8.c_str() + sizeof("string:") - 1);
3535 else if (!strncmp(strCFGMValueUtf8.c_str(), RT_STR_TUPLE("integer:")))
3536 {
3537 rc = RTStrToUInt64Full(strCFGMValueUtf8.c_str() + sizeof("integer:") - 1, 0, &u64Value);
3538 if (RT_SUCCESS(rc))
3539 rc = CFGMR3InsertInteger(pNode, pszCFGMValueName, u64Value);
3540 }
3541 else if (!strncmp(strCFGMValueUtf8.c_str(), RT_STR_TUPLE("bytes:")))
3542 {
3543 char const *pszBase64 = strCFGMValueUtf8.c_str() + sizeof("bytes:") - 1;
3544 ssize_t cbValue = RTBase64DecodedSize(pszBase64, NULL);
3545 if (cbValue > 0)
3546 {
3547 void *pvBytes = RTMemTmpAlloc(cbValue);
3548 if (pvBytes)
3549 {
3550 rc = RTBase64Decode(pszBase64, pvBytes, cbValue, NULL, NULL);
3551 if (RT_SUCCESS(rc))
3552 rc = CFGMR3InsertBytes(pNode, pszCFGMValueName, pvBytes, cbValue);
3553 RTMemTmpFree(pvBytes);
3554 }
3555 else
3556 rc = VERR_NO_TMP_MEMORY;
3557 }
3558 else if (cbValue == 0)
3559 rc = CFGMR3InsertBytes(pNode, pszCFGMValueName, NULL, 0);
3560 else
3561 rc = VERR_INVALID_BASE64_ENCODING;
3562 }
3563 /* auto detect type. */
3564 else if (RT_SUCCESS(RTStrToUInt64Full(strCFGMValueUtf8.c_str(), 0, &u64Value)))
3565 rc = CFGMR3InsertInteger(pNode, pszCFGMValueName, u64Value);
3566 else
3567 InsertConfigString(pNode, pszCFGMValueName, strCFGMValueUtf8);
3568 AssertLogRelMsgRCBreak(rc, ("failed to insert CFGM value '%s' to key '%s'\n",
3569 strCFGMValueUtf8.c_str(), pszExtraDataKey));
3570 }
3571 }
3572 }
3573 catch (ConfigError &x)
3574 {
3575 // InsertConfig threw something:
3576 return x.m_vrc;
3577 }
3578 return rc;
3579}
3580
3581/**
3582 * Dumps the API settings tweaks as specified by VBoxInternal2/XXX extra data
3583 * values.
3584 *
3585 * @returns VBox status code.
3586 * @param pVirtualBox Pointer to the IVirtualBox interface.
3587 * @param pMachine Pointer to the IMachine interface.
3588 */
3589/* static */
3590int Console::i_configDumpAPISettingsTweaks(IVirtualBox *pVirtualBox, IMachine *pMachine)
3591{
3592 {
3593 SafeArray<BSTR> aGlobalExtraDataKeys;
3594 HRESULT hrc = pVirtualBox->GetExtraDataKeys(ComSafeArrayAsOutParam(aGlobalExtraDataKeys));
3595 AssertMsg(SUCCEEDED(hrc), ("VirtualBox::GetExtraDataKeys failed with %Rhrc\n", hrc));
3596 bool hasKey = false;
3597 for (size_t i = 0; i < aGlobalExtraDataKeys.size(); i++)
3598 {
3599 Utf8Str strKey(aGlobalExtraDataKeys[i]);
3600 if (!strKey.startsWith("VBoxInternal2/"))
3601 continue;
3602
3603 Bstr bstrValue;
3604 hrc = pVirtualBox->GetExtraData(Bstr(strKey).raw(),
3605 bstrValue.asOutParam());
3606 if (FAILED(hrc))
3607 continue;
3608 if (!hasKey)
3609 LogRel(("Global extradata API settings:\n"));
3610 LogRel((" %s=\"%ls\"\n", strKey.c_str(), bstrValue.raw()));
3611 hasKey = true;
3612 }
3613 }
3614
3615 {
3616 SafeArray<BSTR> aMachineExtraDataKeys;
3617 HRESULT hrc = pMachine->GetExtraDataKeys(ComSafeArrayAsOutParam(aMachineExtraDataKeys));
3618 AssertMsg(SUCCEEDED(hrc), ("Machine::GetExtraDataKeys failed with %Rhrc\n", hrc));
3619 bool hasKey = false;
3620 for (size_t i = 0; i < aMachineExtraDataKeys.size(); i++)
3621 {
3622 Utf8Str strKey(aMachineExtraDataKeys[i]);
3623 if (!strKey.startsWith("VBoxInternal2/"))
3624 continue;
3625
3626 Bstr bstrValue;
3627 hrc = pMachine->GetExtraData(Bstr(strKey).raw(),
3628 bstrValue.asOutParam());
3629 if (FAILED(hrc))
3630 continue;
3631 if (!hasKey)
3632 LogRel(("Per-VM extradata API settings:\n"));
3633 LogRel((" %s=\"%ls\"\n", strKey.c_str(), bstrValue.raw()));
3634 hasKey = true;
3635 }
3636 }
3637
3638 return VINF_SUCCESS;
3639}
3640
3641int Console::i_configGraphicsController(PCFGMNODE pDevices,
3642 const GraphicsControllerType_T enmGraphicsController,
3643 BusAssignmentManager *pBusMgr,
3644 const ComPtr<IMachine> &ptrMachine,
3645 const ComPtr<IBIOSSettings> &ptrBiosSettings,
3646 bool fHMEnabled)
3647{
3648 // InsertConfig* throws
3649 try
3650 {
3651 PCFGMNODE pDev, pInst, pCfg, pLunL0;
3652 HRESULT hrc;
3653 Bstr bstr;
3654 const char *pcszDevice = "vga";
3655
3656#define H() AssertLogRelMsgReturn(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), VERR_MAIN_CONFIG_CONSTRUCTOR_COM_ERROR)
3657 InsertConfigNode(pDevices, pcszDevice, &pDev);
3658 InsertConfigNode(pDev, "0", &pInst);
3659 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
3660
3661 hrc = pBusMgr->assignPCIDevice(pcszDevice, pInst); H();
3662 InsertConfigNode(pInst, "Config", &pCfg);
3663 ULONG cVRamMBs;
3664 hrc = ptrMachine->COMGETTER(VRAMSize)(&cVRamMBs); H();
3665 InsertConfigInteger(pCfg, "VRamSize", cVRamMBs * _1M);
3666 ULONG cMonitorCount;
3667 hrc = ptrMachine->COMGETTER(MonitorCount)(&cMonitorCount); H();
3668 InsertConfigInteger(pCfg, "MonitorCount", cMonitorCount);
3669#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
3670 InsertConfigInteger(pCfg, "R0Enabled", fHMEnabled);
3671#else
3672 NOREF(fHMEnabled);
3673#endif
3674
3675 i_attachStatusDriver(pInst, &mapCrOglLed, 0, 0, NULL, NULL, 0);
3676
3677#ifdef VBOX_WITH_VMSVGA
3678 if (enmGraphicsController == GraphicsControllerType_VMSVGA)
3679 {
3680 InsertConfigInteger(pCfg, "VMSVGAEnabled", true);
3681#ifdef VBOX_WITH_VMSVGA3D
3682 IFramebuffer *pFramebuffer = NULL;
3683 hrc = i_getDisplay()->QueryFramebuffer(0, &pFramebuffer);
3684 if (SUCCEEDED(hrc) && pFramebuffer)
3685 {
3686 LONG64 winId = 0;
3687 /** @todo deal with multimonitor setup */
3688 Assert(cMonitorCount == 1);
3689 hrc = pFramebuffer->COMGETTER(WinId)(&winId);
3690 InsertConfigInteger(pCfg, "HostWindowId", winId);
3691 pFramebuffer->Release();
3692 }
3693 BOOL f3DEnabled;
3694 hrc = ptrMachine->COMGETTER(Accelerate3DEnabled)(&f3DEnabled); H();
3695 InsertConfigInteger(pCfg, "VMSVGA3dEnabled", f3DEnabled);
3696#else
3697 LogRel(("VMSVGA3d not available in this build!\n"));
3698#endif
3699 }
3700#endif
3701
3702 /* Custom VESA mode list */
3703 unsigned cModes = 0;
3704 for (unsigned iMode = 1; iMode <= 16; ++iMode)
3705 {
3706 char szExtraDataKey[sizeof("CustomVideoModeXX")];
3707 RTStrPrintf(szExtraDataKey, sizeof(szExtraDataKey), "CustomVideoMode%u", iMode);
3708 hrc = ptrMachine->GetExtraData(Bstr(szExtraDataKey).raw(), bstr.asOutParam()); H();
3709 if (bstr.isEmpty())
3710 break;
3711 InsertConfigString(pCfg, szExtraDataKey, bstr);
3712 ++cModes;
3713 }
3714 InsertConfigInteger(pCfg, "CustomVideoModes", cModes);
3715
3716 /* VESA height reduction */
3717 ULONG ulHeightReduction;
3718 IFramebuffer *pFramebuffer = NULL;
3719 hrc = i_getDisplay()->QueryFramebuffer(0, &pFramebuffer);
3720 if (SUCCEEDED(hrc) && pFramebuffer)
3721 {
3722 hrc = pFramebuffer->COMGETTER(HeightReduction)(&ulHeightReduction); H();
3723 pFramebuffer->Release();
3724 pFramebuffer = NULL;
3725 }
3726 else
3727 {
3728 /* If framebuffer is not available, there is no height reduction. */
3729 ulHeightReduction = 0;
3730 }
3731 InsertConfigInteger(pCfg, "HeightReduction", ulHeightReduction);
3732
3733 /*
3734 * BIOS logo
3735 */
3736 BOOL fFadeIn;
3737 hrc = ptrBiosSettings->COMGETTER(LogoFadeIn)(&fFadeIn); H();
3738 InsertConfigInteger(pCfg, "FadeIn", fFadeIn ? 1 : 0);
3739 BOOL fFadeOut;
3740 hrc = ptrBiosSettings->COMGETTER(LogoFadeOut)(&fFadeOut); H();
3741 InsertConfigInteger(pCfg, "FadeOut", fFadeOut ? 1: 0);
3742 ULONG logoDisplayTime;
3743 hrc = ptrBiosSettings->COMGETTER(LogoDisplayTime)(&logoDisplayTime); H();
3744 InsertConfigInteger(pCfg, "LogoTime", logoDisplayTime);
3745 Bstr logoImagePath;
3746 hrc = ptrBiosSettings->COMGETTER(LogoImagePath)(logoImagePath.asOutParam()); H();
3747 InsertConfigString(pCfg, "LogoFile", Utf8Str(!logoImagePath.isEmpty() ? logoImagePath : "") );
3748
3749 /*
3750 * Boot menu
3751 */
3752 BIOSBootMenuMode_T eBootMenuMode;
3753 int iShowBootMenu;
3754 hrc = ptrBiosSettings->COMGETTER(BootMenuMode)(&eBootMenuMode); H();
3755 switch (eBootMenuMode)
3756 {
3757 case BIOSBootMenuMode_Disabled: iShowBootMenu = 0; break;
3758 case BIOSBootMenuMode_MenuOnly: iShowBootMenu = 1; break;
3759 default: iShowBootMenu = 2; break;
3760 }
3761 InsertConfigInteger(pCfg, "ShowBootMenu", iShowBootMenu);
3762
3763 /* Attach the display. */
3764 InsertConfigNode(pInst, "LUN#0", &pLunL0);
3765 InsertConfigString(pLunL0, "Driver", "MainDisplay");
3766 InsertConfigNode(pLunL0, "Config", &pCfg);
3767 Display *pDisplay = mDisplay;
3768 InsertConfigInteger(pCfg, "Object", (uintptr_t)pDisplay);
3769 }
3770 catch (ConfigError &x)
3771 {
3772 // InsertConfig threw something:
3773 return x.m_vrc;
3774 }
3775
3776#undef H
3777
3778 return VINF_SUCCESS;
3779}
3780
3781
3782/**
3783 * Ellipsis to va_list wrapper for calling setVMRuntimeErrorCallback.
3784 */
3785void Console::i_atVMRuntimeErrorCallbackF(uint32_t fFlags, const char *pszErrorId, const char *pszFormat, ...)
3786{
3787 va_list va;
3788 va_start(va, pszFormat);
3789 i_atVMRuntimeErrorCallback(NULL, this, fFlags, pszErrorId, pszFormat, va);
3790 va_end(va);
3791}
3792
3793/* XXX introduce RT format specifier */
3794static uint64_t formatDiskSize(uint64_t u64Size, const char **pszUnit)
3795{
3796 if (u64Size > INT64_C(5000)*_1G)
3797 {
3798 *pszUnit = "TB";
3799 return u64Size / _1T;
3800 }
3801 else if (u64Size > INT64_C(5000)*_1M)
3802 {
3803 *pszUnit = "GB";
3804 return u64Size / _1G;
3805 }
3806 else
3807 {
3808 *pszUnit = "MB";
3809 return u64Size / _1M;
3810 }
3811}
3812
3813/**
3814 * Checks the location of the given medium for known bugs affecting the usage
3815 * of the host I/O cache setting.
3816 *
3817 * @returns VBox status code.
3818 * @param pMedium The medium to check.
3819 * @param pfUseHostIOCache Where to store the suggested host I/O cache setting.
3820 */
3821int Console::i_checkMediumLocation(IMedium *pMedium, bool *pfUseHostIOCache)
3822{
3823#define H() AssertLogRelMsgReturn(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), VERR_MAIN_CONFIG_CONSTRUCTOR_COM_ERROR)
3824 /*
3825 * Some sanity checks.
3826 */
3827 RT_NOREF(pfUseHostIOCache);
3828 ComPtr<IMediumFormat> pMediumFormat;
3829 HRESULT hrc = pMedium->COMGETTER(MediumFormat)(pMediumFormat.asOutParam()); H();
3830 ULONG uCaps = 0;
3831 com::SafeArray <MediumFormatCapabilities_T> mediumFormatCap;
3832 hrc = pMediumFormat->COMGETTER(Capabilities)(ComSafeArrayAsOutParam(mediumFormatCap)); H();
3833
3834 for (ULONG j = 0; j < mediumFormatCap.size(); j++)
3835 uCaps |= mediumFormatCap[j];
3836
3837 if (uCaps & MediumFormatCapabilities_File)
3838 {
3839 Bstr strFile;
3840 hrc = pMedium->COMGETTER(Location)(strFile.asOutParam()); H();
3841 Utf8Str utfFile = Utf8Str(strFile);
3842 Bstr strSnap;
3843 ComPtr<IMachine> pMachine = i_machine();
3844 hrc = pMachine->COMGETTER(SnapshotFolder)(strSnap.asOutParam()); H();
3845 Utf8Str utfSnap = Utf8Str(strSnap);
3846 RTFSTYPE enmFsTypeFile = RTFSTYPE_UNKNOWN;
3847 RTFSTYPE enmFsTypeSnap = RTFSTYPE_UNKNOWN;
3848 int rc2 = RTFsQueryType(utfFile.c_str(), &enmFsTypeFile);
3849 AssertMsgRCReturn(rc2, ("Querying the file type of '%s' failed!\n", utfFile.c_str()), rc2);
3850 /* Ignore the error code. On error, the file system type is still 'unknown' so
3851 * none of the following paths are taken. This can happen for new VMs which
3852 * still don't have a snapshot folder. */
3853 (void)RTFsQueryType(utfSnap.c_str(), &enmFsTypeSnap);
3854 if (!mfSnapshotFolderDiskTypeShown)
3855 {
3856 LogRel(("File system of '%s' (snapshots) is %s\n",
3857 utfSnap.c_str(), RTFsTypeName(enmFsTypeSnap)));
3858 mfSnapshotFolderDiskTypeShown = true;
3859 }
3860 LogRel(("File system of '%s' is %s\n", utfFile.c_str(), RTFsTypeName(enmFsTypeFile)));
3861 LONG64 i64Size;
3862 hrc = pMedium->COMGETTER(LogicalSize)(&i64Size); H();
3863#ifdef RT_OS_WINDOWS
3864 if ( enmFsTypeFile == RTFSTYPE_FAT
3865 && i64Size >= _4G)
3866 {
3867 const char *pszUnit;
3868 uint64_t u64Print = formatDiskSize((uint64_t)i64Size, &pszUnit);
3869 i_atVMRuntimeErrorCallbackF(0, "FatPartitionDetected",
3870 N_("The medium '%ls' has a logical size of %RU64%s "
3871 "but the file system the medium is located on seems "
3872 "to be FAT(32) which cannot handle files bigger than 4GB.\n"
3873 "We strongly recommend to put all your virtual disk images and "
3874 "the snapshot folder onto an NTFS partition"),
3875 strFile.raw(), u64Print, pszUnit);
3876 }
3877#else /* !RT_OS_WINDOWS */
3878 if ( enmFsTypeFile == RTFSTYPE_FAT
3879 || enmFsTypeFile == RTFSTYPE_EXT
3880 || enmFsTypeFile == RTFSTYPE_EXT2
3881 || enmFsTypeFile == RTFSTYPE_EXT3
3882 || enmFsTypeFile == RTFSTYPE_EXT4)
3883 {
3884 RTFILE file;
3885 int rc = RTFileOpen(&file, utfFile.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
3886 if (RT_SUCCESS(rc))
3887 {
3888 RTFOFF maxSize;
3889 /* Careful: This function will work only on selected local file systems! */
3890 rc = RTFileGetMaxSizeEx(file, &maxSize);
3891 RTFileClose(file);
3892 if ( RT_SUCCESS(rc)
3893 && maxSize > 0
3894 && i64Size > (LONG64)maxSize)
3895 {
3896 const char *pszUnitSiz;
3897 const char *pszUnitMax;
3898 uint64_t u64PrintSiz = formatDiskSize((LONG64)i64Size, &pszUnitSiz);
3899 uint64_t u64PrintMax = formatDiskSize(maxSize, &pszUnitMax);
3900 i_atVMRuntimeErrorCallbackF(0, "FatPartitionDetected", /* <= not exact but ... */
3901 N_("The medium '%ls' has a logical size of %RU64%s "
3902 "but the file system the medium is located on can "
3903 "only handle files up to %RU64%s in theory.\n"
3904 "We strongly recommend to put all your virtual disk "
3905 "images and the snapshot folder onto a proper "
3906 "file system (e.g. ext3) with a sufficient size"),
3907 strFile.raw(), u64PrintSiz, pszUnitSiz, u64PrintMax, pszUnitMax);
3908 }
3909 }
3910 }
3911#endif /* !RT_OS_WINDOWS */
3912
3913 /*
3914 * Snapshot folder:
3915 * Here we test only for a FAT partition as we had to create a dummy file otherwise
3916 */
3917 if ( enmFsTypeSnap == RTFSTYPE_FAT
3918 && i64Size >= _4G
3919 && !mfSnapshotFolderSizeWarningShown)
3920 {
3921 const char *pszUnit;
3922 uint64_t u64Print = formatDiskSize(i64Size, &pszUnit);
3923 i_atVMRuntimeErrorCallbackF(0, "FatPartitionDetected",
3924#ifdef RT_OS_WINDOWS
3925 N_("The snapshot folder of this VM '%ls' seems to be located on "
3926 "a FAT(32) file system. The logical size of the medium '%ls' "
3927 "(%RU64%s) is bigger than the maximum file size this file "
3928 "system can handle (4GB).\n"
3929 "We strongly recommend to put all your virtual disk images and "
3930 "the snapshot folder onto an NTFS partition"),
3931#else
3932 N_("The snapshot folder of this VM '%ls' seems to be located on "
3933 "a FAT(32) file system. The logical size of the medium '%ls' "
3934 "(%RU64%s) is bigger than the maximum file size this file "
3935 "system can handle (4GB).\n"
3936 "We strongly recommend to put all your virtual disk images and "
3937 "the snapshot folder onto a proper file system (e.g. ext3)"),
3938#endif
3939 strSnap.raw(), strFile.raw(), u64Print, pszUnit);
3940 /* Show this particular warning only once */
3941 mfSnapshotFolderSizeWarningShown = true;
3942 }
3943
3944#ifdef RT_OS_LINUX
3945 /*
3946 * Ext4 bug: Check if the host I/O cache is disabled and the disk image is located
3947 * on an ext4 partition.
3948 * This bug apparently applies to the XFS file system as well.
3949 * Linux 2.6.36 is known to be fixed (tested with 2.6.36-rc4).
3950 */
3951
3952 char szOsRelease[128];
3953 int rc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szOsRelease, sizeof(szOsRelease));
3954 bool fKernelHasODirectBug = RT_FAILURE(rc)
3955 || (RTStrVersionCompare(szOsRelease, "2.6.36-rc4") < 0);
3956
3957 if ( (uCaps & MediumFormatCapabilities_Asynchronous)
3958 && !*pfUseHostIOCache
3959 && fKernelHasODirectBug)
3960 {
3961 if ( enmFsTypeFile == RTFSTYPE_EXT4
3962 || enmFsTypeFile == RTFSTYPE_XFS)
3963 {
3964 i_atVMRuntimeErrorCallbackF(0, "Ext4PartitionDetected",
3965 N_("The host I/O cache for at least one controller is disabled "
3966 "and the medium '%ls' for this VM "
3967 "is located on an %s partition. There is a known Linux "
3968 "kernel bug which can lead to the corruption of the virtual "
3969 "disk image under these conditions.\n"
3970 "Either enable the host I/O cache permanently in the VM "
3971 "settings or put the disk image and the snapshot folder "
3972 "onto a different file system.\n"
3973 "The host I/O cache will now be enabled for this medium"),
3974 strFile.raw(), enmFsTypeFile == RTFSTYPE_EXT4 ? "ext4" : "xfs");
3975 *pfUseHostIOCache = true;
3976 }
3977 else if ( ( enmFsTypeSnap == RTFSTYPE_EXT4
3978 || enmFsTypeSnap == RTFSTYPE_XFS)
3979 && !mfSnapshotFolderExt4WarningShown)
3980 {
3981 i_atVMRuntimeErrorCallbackF(0, "Ext4PartitionDetected",
3982 N_("The host I/O cache for at least one controller is disabled "
3983 "and the snapshot folder for this VM "
3984 "is located on an %s partition. There is a known Linux "
3985 "kernel bug which can lead to the corruption of the virtual "
3986 "disk image under these conditions.\n"
3987 "Either enable the host I/O cache permanently in the VM "
3988 "settings or put the disk image and the snapshot folder "
3989 "onto a different file system.\n"
3990 "The host I/O cache will now be enabled for this medium"),
3991 enmFsTypeSnap == RTFSTYPE_EXT4 ? "ext4" : "xfs");
3992 *pfUseHostIOCache = true;
3993 mfSnapshotFolderExt4WarningShown = true;
3994 }
3995 }
3996
3997 /*
3998 * 2.6.18 bug: Check if the host I/O cache is disabled and the host is running
3999 * Linux 2.6.18. See @bugref{8690}. Apparently the same problem as
4000 * documented in https://lkml.org/lkml/2007/2/1/14. We saw such
4001 * kernel oopses on Linux 2.6.18-416.el5. We don't know when this
4002 * was fixed but we _know_ that 2.6.18 EL5 kernels are affected.
4003 */
4004 bool fKernelAsyncUnreliable = RT_FAILURE(rc)
4005 || (RTStrVersionCompare(szOsRelease, "2.6.19") < 0);
4006 if ( (uCaps & MediumFormatCapabilities_Asynchronous)
4007 && !*pfUseHostIOCache
4008 && fKernelAsyncUnreliable)
4009 {
4010 i_atVMRuntimeErrorCallbackF(0, "Linux2618TooOld",
4011 N_("The host I/O cache for at least one controller is disabled. "
4012 "There is a known Linux kernel bug which can lead to kernel "
4013 "oopses under heavy load. To our knowledge this bug affects "
4014 "all 2.6.18 kernels.\n"
4015 "Either enable the host I/O cache permanently in the VM "
4016 "settings or switch to a newer host kernel.\n"
4017 "The host I/O cache will now be enabled for this medium"));
4018 *pfUseHostIOCache = true;
4019 }
4020#endif
4021 }
4022#undef H
4023
4024 return VINF_SUCCESS;
4025}
4026
4027/**
4028 * Unmounts the specified medium from the specified device.
4029 *
4030 * @returns VBox status code.
4031 * @param pUVM The usermode VM handle.
4032 * @param enmBus The storage bus.
4033 * @param enmDevType The device type.
4034 * @param pcszDevice The device emulation.
4035 * @param uInstance Instance of the device.
4036 * @param uLUN The LUN on the device.
4037 * @param fForceUnmount Whether to force unmounting.
4038 */
4039int Console::i_unmountMediumFromGuest(PUVM pUVM, StorageBus_T enmBus, DeviceType_T enmDevType,
4040 const char *pcszDevice, unsigned uInstance, unsigned uLUN,
4041 bool fForceUnmount)
4042{
4043 /* Unmount existing media only for floppy and DVD drives. */
4044 int rc = VINF_SUCCESS;
4045 PPDMIBASE pBase;
4046 if (enmBus == StorageBus_USB)
4047 rc = PDMR3UsbQueryDriverOnLun(pUVM, pcszDevice, uInstance, uLUN, "SCSI", &pBase);
4048 else if ( (enmBus == StorageBus_SAS || enmBus == StorageBus_SCSI)
4049 || (enmBus == StorageBus_SATA && enmDevType == DeviceType_DVD))
4050 rc = PDMR3QueryDriverOnLun(pUVM, pcszDevice, uInstance, uLUN, "SCSI", &pBase);
4051 else /* IDE or Floppy */
4052 rc = PDMR3QueryLun(pUVM, pcszDevice, uInstance, uLUN, &pBase);
4053
4054 if (RT_FAILURE(rc))
4055 {
4056 if (rc == VERR_PDM_LUN_NOT_FOUND || rc == VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN)
4057 rc = VINF_SUCCESS;
4058 AssertRC(rc);
4059 }
4060 else
4061 {
4062 PPDMIMOUNT pIMount = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMOUNT);
4063 AssertReturn(pIMount, VERR_INVALID_POINTER);
4064
4065 /* Unmount the media (but do not eject the medium!) */
4066 rc = pIMount->pfnUnmount(pIMount, fForceUnmount, false /*=fEject*/);
4067 if (rc == VERR_PDM_MEDIA_NOT_MOUNTED)
4068 rc = VINF_SUCCESS;
4069 /* for example if the medium is locked */
4070 else if (RT_FAILURE(rc))
4071 return rc;
4072 }
4073
4074 return rc;
4075}
4076
4077/**
4078 * Removes the currently attached medium driver form the specified device
4079 * taking care of the controlelr specific configs wrt. to the attached driver chain.
4080 *
4081 * @returns VBox status code.
4082 * @param pCtlInst The controler instance node in the CFGM tree.
4083 * @param pcszDevice The device name.
4084 * @param uInstance The device instance.
4085 * @param uLUN The device LUN.
4086 * @param enmBus The storage bus.
4087 * @param fAttachDetach Flag whether this is a change while the VM is running
4088 * @param fHotplug Flag whether the guest should be notified about the device change.
4089 * @param fForceUnmount Flag whether to force unmounting the medium even if it is locked.
4090 * @param pUVM The usermode VM handle.
4091 * @param enmDevType The device type.
4092 * @param ppLunL0 Where to store the node to attach the new config to on success.
4093 */
4094int Console::i_removeMediumDriverFromVm(PCFGMNODE pCtlInst,
4095 const char *pcszDevice,
4096 unsigned uInstance,
4097 unsigned uLUN,
4098 StorageBus_T enmBus,
4099 bool fAttachDetach,
4100 bool fHotplug,
4101 bool fForceUnmount,
4102 PUVM pUVM,
4103 DeviceType_T enmDevType,
4104 PCFGMNODE *ppLunL0)
4105{
4106 int rc = VINF_SUCCESS;
4107 bool fAddLun = false;
4108
4109 /* First check if the LUN already exists. */
4110 PCFGMNODE pLunL0 = CFGMR3GetChildF(pCtlInst, "LUN#%u", uLUN);
4111 AssertReturn(!VALID_PTR(pLunL0) || fAttachDetach, VERR_INTERNAL_ERROR);
4112
4113 if (pLunL0)
4114 {
4115 /*
4116 * Unmount the currently mounted medium if we don't just hot remove the
4117 * complete device (SATA) and it supports unmounting (DVD).
4118 */
4119 if ( (enmDevType != DeviceType_HardDisk)
4120 && !fHotplug)
4121 {
4122 rc = i_unmountMediumFromGuest(pUVM, enmBus, enmDevType, pcszDevice,
4123 uInstance, uLUN, fForceUnmount);
4124 if (RT_FAILURE(rc))
4125 return rc;
4126 }
4127
4128 /*
4129 * Don't detach the SCSI driver when unmounting the current medium
4130 * (we are not ripping out the device but only eject the medium).
4131 */
4132 char *pszDriverDetach = NULL;
4133 if ( !fHotplug
4134 && ( (enmBus == StorageBus_SATA && enmDevType == DeviceType_DVD)
4135 || enmBus == StorageBus_SAS
4136 || enmBus == StorageBus_SCSI
4137 || enmBus == StorageBus_USB))
4138 {
4139 /* Get the current attached driver we have to detach. */
4140 PCFGMNODE pDrvLun = CFGMR3GetChildF(pCtlInst, "LUN#%u/AttachedDriver/", uLUN);
4141 if (pDrvLun)
4142 {
4143 char szDriver[128];
4144 RT_ZERO(szDriver);
4145 rc = CFGMR3QueryString(pDrvLun, "Driver", &szDriver[0], sizeof(szDriver));
4146 if (RT_SUCCESS(rc))
4147 pszDriverDetach = RTStrDup(&szDriver[0]);
4148
4149 pLunL0 = pDrvLun;
4150 }
4151 }
4152
4153 if (enmBus == StorageBus_USB)
4154 rc = PDMR3UsbDriverDetach(pUVM, pcszDevice, uInstance, uLUN,
4155 pszDriverDetach, 0 /* iOccurence */,
4156 fHotplug ? 0 : PDM_TACH_FLAGS_NOT_HOT_PLUG);
4157 else
4158 rc = PDMR3DriverDetach(pUVM, pcszDevice, uInstance, uLUN,
4159 pszDriverDetach, 0 /* iOccurence */,
4160 fHotplug ? 0 : PDM_TACH_FLAGS_NOT_HOT_PLUG);
4161
4162 if (pszDriverDetach)
4163 {
4164 RTStrFree(pszDriverDetach);
4165 /* Remove the complete node and create new for the new config. */
4166 CFGMR3RemoveNode(pLunL0);
4167 pLunL0 = CFGMR3GetChildF(pCtlInst, "LUN#%u", uLUN);
4168 if (pLunL0)
4169 {
4170 try
4171 {
4172 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL0);
4173 }
4174 catch (ConfigError &x)
4175 {
4176 // InsertConfig threw something:
4177 return x.m_vrc;
4178 }
4179 }
4180 }
4181 if (rc == VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN)
4182 rc = VINF_SUCCESS;
4183 AssertRCReturn(rc, rc);
4184
4185 /*
4186 * Don't remove the LUN except for IDE/floppy/NVMe (which connects directly to the medium driver
4187 * even for DVD devices) or if there is a hotplug event which rips out the complete device.
4188 */
4189 if ( fHotplug
4190 || enmBus == StorageBus_IDE
4191 || enmBus == StorageBus_Floppy
4192 || enmBus == StorageBus_PCIe
4193 || (enmBus == StorageBus_SATA && enmDevType != DeviceType_DVD))
4194 {
4195 fAddLun = true;
4196 CFGMR3RemoveNode(pLunL0);
4197 }
4198 }
4199 else
4200 fAddLun = true;
4201
4202 try
4203 {
4204 if (fAddLun)
4205 InsertConfigNode(pCtlInst, Utf8StrFmt("LUN#%u", uLUN).c_str(), &pLunL0);
4206 }
4207 catch (ConfigError &x)
4208 {
4209 // InsertConfig threw something:
4210 return x.m_vrc;
4211 }
4212
4213 if (ppLunL0)
4214 *ppLunL0 = pLunL0;
4215
4216 return rc;
4217}
4218
4219int Console::i_configMediumAttachment(const char *pcszDevice,
4220 unsigned uInstance,
4221 StorageBus_T enmBus,
4222 bool fUseHostIOCache,
4223 bool fBuiltinIOCache,
4224 bool fSetupMerge,
4225 unsigned uMergeSource,
4226 unsigned uMergeTarget,
4227 IMediumAttachment *pMediumAtt,
4228 MachineState_T aMachineState,
4229 HRESULT *phrc,
4230 bool fAttachDetach,
4231 bool fForceUnmount,
4232 bool fHotplug,
4233 PUVM pUVM,
4234 DeviceType_T *paLedDevType,
4235 PCFGMNODE *ppLunL0)
4236{
4237 // InsertConfig* throws
4238 try
4239 {
4240 int rc = VINF_SUCCESS;
4241 HRESULT hrc;
4242 Bstr bstr;
4243 PCFGMNODE pCtlInst = NULL;
4244
4245// #define RC_CHECK() AssertMsgReturn(RT_SUCCESS(rc), ("rc=%Rrc\n", rc), rc)
4246#define H() AssertLogRelMsgReturn(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), VERR_MAIN_CONFIG_CONSTRUCTOR_COM_ERROR)
4247
4248 LONG lDev;
4249 hrc = pMediumAtt->COMGETTER(Device)(&lDev); H();
4250 LONG lPort;
4251 hrc = pMediumAtt->COMGETTER(Port)(&lPort); H();
4252 DeviceType_T lType;
4253 hrc = pMediumAtt->COMGETTER(Type)(&lType); H();
4254 BOOL fNonRotational;
4255 hrc = pMediumAtt->COMGETTER(NonRotational)(&fNonRotational); H();
4256 BOOL fDiscard;
4257 hrc = pMediumAtt->COMGETTER(Discard)(&fDiscard); H();
4258
4259 unsigned uLUN;
4260 PCFGMNODE pLunL0 = NULL;
4261 hrc = Console::i_convertBusPortDeviceToLun(enmBus, lPort, lDev, uLUN); H();
4262
4263 /* Determine the base path for the device instance. */
4264 if (enmBus != StorageBus_USB)
4265 pCtlInst = CFGMR3GetChildF(CFGMR3GetRootU(pUVM), "Devices/%s/%u/", pcszDevice, uInstance);
4266 else
4267 {
4268 /* If we hotplug a USB device create a new CFGM tree. */
4269 if (!fHotplug)
4270 pCtlInst = CFGMR3GetChildF(CFGMR3GetRootU(pUVM), "USB/%s/", pcszDevice);
4271 else
4272 pCtlInst = CFGMR3CreateTree(pUVM);
4273 }
4274 AssertReturn(pCtlInst, VERR_INTERNAL_ERROR);
4275
4276 if (enmBus == StorageBus_USB)
4277 {
4278 PCFGMNODE pCfg = NULL;
4279
4280 /* Create correct instance. */
4281 if (!fHotplug)
4282 {
4283 if (!fAttachDetach)
4284 InsertConfigNode(pCtlInst, Utf8StrFmt("%d", lPort).c_str(), &pCtlInst);
4285 else
4286 pCtlInst = CFGMR3GetChildF(pCtlInst, "%d/", lPort);
4287 }
4288
4289 if (!fAttachDetach)
4290 InsertConfigNode(pCtlInst, "Config", &pCfg);
4291
4292 uInstance = lPort; /* Overwrite uInstance with the correct one. */
4293
4294 if (!fHotplug && !fAttachDetach)
4295 {
4296 char aszUuid[RTUUID_STR_LENGTH + 1];
4297 USBStorageDevice UsbMsd = USBStorageDevice();
4298
4299 memset(aszUuid, 0, sizeof(aszUuid));
4300 rc = RTUuidCreate(&UsbMsd.mUuid);
4301 AssertRCReturn(rc, rc);
4302 rc = RTUuidToStr(&UsbMsd.mUuid, aszUuid, sizeof(aszUuid));
4303 AssertRCReturn(rc, rc);
4304
4305 UsbMsd.iPort = uInstance;
4306
4307 InsertConfigString(pCtlInst, "UUID", aszUuid);
4308 mUSBStorageDevices.push_back(UsbMsd);
4309
4310 /** @todo No LED after hotplugging. */
4311 /* Attach the status driver */
4312 Assert(cLedUsb >= 8);
4313 i_attachStatusDriver(pCtlInst, &mapStorageLeds[iLedUsb], 0, 7,
4314 &mapMediumAttachments, pcszDevice, 0);
4315 paLedDevType = &maStorageDevType[iLedUsb];
4316 }
4317 }
4318
4319 rc = i_removeMediumDriverFromVm(pCtlInst, pcszDevice, uInstance, uLUN, enmBus, fAttachDetach,
4320 fHotplug, fForceUnmount, pUVM, lType, &pLunL0);
4321 if (RT_FAILURE(rc))
4322 return rc;
4323 if (ppLunL0)
4324 *ppLunL0 = pLunL0;
4325
4326 Utf8Str devicePath = Utf8StrFmt("%s/%u/LUN#%u", pcszDevice, uInstance, uLUN);
4327 mapMediumAttachments[devicePath] = pMediumAtt;
4328
4329 ComPtr<IMedium> pMedium;
4330 hrc = pMediumAtt->COMGETTER(Medium)(pMedium.asOutParam()); H();
4331
4332 /*
4333 * 1. Only check this for hard disk images.
4334 * 2. Only check during VM creation and not later, especially not during
4335 * taking an online snapshot!
4336 */
4337 if ( lType == DeviceType_HardDisk
4338 && ( aMachineState == MachineState_Starting
4339 || aMachineState == MachineState_Restoring))
4340 {
4341 rc = i_checkMediumLocation(pMedium, &fUseHostIOCache);
4342 if (RT_FAILURE(rc))
4343 return rc;
4344 }
4345
4346 if (pMedium)
4347 {
4348 BOOL fHostDrive;
4349 hrc = pMedium->COMGETTER(HostDrive)(&fHostDrive); H();
4350 if ( ( lType == DeviceType_DVD
4351 || lType == DeviceType_Floppy)
4352 && !fHostDrive)
4353 {
4354 /*
4355 * Informative logging.
4356 */
4357 Bstr strFile;
4358 hrc = pMedium->COMGETTER(Location)(strFile.asOutParam()); H();
4359 Utf8Str utfFile = Utf8Str(strFile);
4360 RTFSTYPE enmFsTypeFile = RTFSTYPE_UNKNOWN;
4361 (void)RTFsQueryType(utfFile.c_str(), &enmFsTypeFile);
4362 LogRel(("File system of '%s' (%s) is %s\n",
4363 utfFile.c_str(), lType == DeviceType_DVD ? "DVD" : "Floppy",
4364 RTFsTypeName(enmFsTypeFile)));
4365 }
4366 }
4367
4368 BOOL fPassthrough;
4369 hrc = pMediumAtt->COMGETTER(Passthrough)(&fPassthrough); H();
4370
4371 ComObjPtr<IBandwidthGroup> pBwGroup;
4372 Bstr strBwGroup;
4373 hrc = pMediumAtt->COMGETTER(BandwidthGroup)(pBwGroup.asOutParam()); H();
4374
4375 if (!pBwGroup.isNull())
4376 {
4377 hrc = pBwGroup->COMGETTER(Name)(strBwGroup.asOutParam()); H();
4378 }
4379
4380 /*
4381 * Insert the SCSI driver for hotplug events on the SCSI/USB based storage controllers
4382 * or for SATA if the new device is a CD/DVD drive.
4383 */
4384 if ( (fHotplug || !fAttachDetach)
4385 && ( (enmBus == StorageBus_SCSI || enmBus == StorageBus_SAS || enmBus == StorageBus_USB)
4386 || (enmBus == StorageBus_SATA && lType == DeviceType_DVD && !fPassthrough)))
4387 {
4388 InsertConfigString(pLunL0, "Driver", "SCSI");
4389 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL0);
4390 }
4391
4392 rc = i_configMedium(pLunL0,
4393 !!fPassthrough,
4394 lType,
4395 fUseHostIOCache,
4396 fBuiltinIOCache,
4397 fSetupMerge,
4398 uMergeSource,
4399 uMergeTarget,
4400 strBwGroup.isEmpty() ? NULL : Utf8Str(strBwGroup).c_str(),
4401 !!fDiscard,
4402 !!fNonRotational,
4403 pMedium,
4404 aMachineState,
4405 phrc);
4406 if (RT_FAILURE(rc))
4407 return rc;
4408
4409 if (fAttachDetach)
4410 {
4411 /* Attach the new driver. */
4412 if (enmBus == StorageBus_USB)
4413 {
4414 if (fHotplug)
4415 {
4416 USBStorageDevice UsbMsd = USBStorageDevice();
4417 RTUuidCreate(&UsbMsd.mUuid);
4418 UsbMsd.iPort = uInstance;
4419 rc = PDMR3UsbCreateEmulatedDevice(pUVM, pcszDevice, pCtlInst, &UsbMsd.mUuid, NULL);
4420 if (RT_SUCCESS(rc))
4421 mUSBStorageDevices.push_back(UsbMsd);
4422 }
4423 else
4424 rc = PDMR3UsbDriverAttach(pUVM, pcszDevice, uInstance, uLUN,
4425 fHotplug ? 0 : PDM_TACH_FLAGS_NOT_HOT_PLUG, NULL /*ppBase*/);
4426 }
4427 else if ( !fHotplug
4428 && ( (enmBus == StorageBus_SAS || enmBus == StorageBus_SCSI)
4429 || (enmBus == StorageBus_SATA && lType == DeviceType_DVD)))
4430 rc = PDMR3DriverAttach(pUVM, pcszDevice, uInstance, uLUN,
4431 fHotplug ? 0 : PDM_TACH_FLAGS_NOT_HOT_PLUG, NULL /*ppBase*/);
4432 else
4433 rc = PDMR3DeviceAttach(pUVM, pcszDevice, uInstance, uLUN,
4434 fHotplug ? 0 : PDM_TACH_FLAGS_NOT_HOT_PLUG, NULL /*ppBase*/);
4435 AssertRCReturn(rc, rc);
4436
4437 /*
4438 * Make the secret key helper interface known to the VD driver if it is attached,
4439 * so we can get notified about missing keys.
4440 */
4441 PPDMIBASE pIBase = NULL;
4442 rc = PDMR3QueryDriverOnLun(pUVM, pcszDevice, uInstance, uLUN, "VD", &pIBase);
4443 if (RT_SUCCESS(rc) && pIBase)
4444 {
4445 PPDMIMEDIA pIMedium = (PPDMIMEDIA)pIBase->pfnQueryInterface(pIBase, PDMIMEDIA_IID);
4446 if (pIMedium)
4447 {
4448 rc = pIMedium->pfnSetSecKeyIf(pIMedium, mpIfSecKey, mpIfSecKeyHlp);
4449 Assert(RT_SUCCESS(rc) || rc == VERR_NOT_SUPPORTED);
4450 }
4451 }
4452
4453 /* There is no need to handle removable medium mounting, as we
4454 * unconditionally replace everthing including the block driver level.
4455 * This means the new medium will be picked up automatically. */
4456 }
4457
4458 if (paLedDevType)
4459 paLedDevType[uLUN] = lType;
4460
4461 /* Dump the changed LUN if possible, dump the complete device otherwise */
4462 if ( aMachineState != MachineState_Starting
4463 && aMachineState != MachineState_Restoring)
4464 CFGMR3Dump(pLunL0 ? pLunL0 : pCtlInst);
4465 }
4466 catch (ConfigError &x)
4467 {
4468 // InsertConfig threw something:
4469 return x.m_vrc;
4470 }
4471
4472#undef H
4473
4474 return VINF_SUCCESS;
4475}
4476
4477int Console::i_configMedium(PCFGMNODE pLunL0,
4478 bool fPassthrough,
4479 DeviceType_T enmType,
4480 bool fUseHostIOCache,
4481 bool fBuiltinIOCache,
4482 bool fSetupMerge,
4483 unsigned uMergeSource,
4484 unsigned uMergeTarget,
4485 const char *pcszBwGroup,
4486 bool fDiscard,
4487 bool fNonRotational,
4488 IMedium *pMedium,
4489 MachineState_T aMachineState,
4490 HRESULT *phrc)
4491{
4492 // InsertConfig* throws
4493 try
4494 {
4495 HRESULT hrc;
4496 Bstr bstr;
4497 PCFGMNODE pCfg = NULL;
4498
4499#define H() \
4500 AssertMsgReturnStmt(SUCCEEDED(hrc), ("hrc=%Rhrc\n", hrc), if (phrc) *phrc = hrc, Global::vboxStatusCodeFromCOM(hrc))
4501
4502
4503 BOOL fHostDrive = FALSE;
4504 MediumType_T mediumType = MediumType_Normal;
4505 if (pMedium)
4506 {
4507 hrc = pMedium->COMGETTER(HostDrive)(&fHostDrive); H();
4508 hrc = pMedium->COMGETTER(Type)(&mediumType); H();
4509 }
4510
4511 if (fHostDrive)
4512 {
4513 Assert(pMedium);
4514 if (enmType == DeviceType_DVD)
4515 {
4516 InsertConfigString(pLunL0, "Driver", "HostDVD");
4517 InsertConfigNode(pLunL0, "Config", &pCfg);
4518
4519 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
4520 InsertConfigString(pCfg, "Path", bstr);
4521
4522 InsertConfigInteger(pCfg, "Passthrough", fPassthrough);
4523 }
4524 else if (enmType == DeviceType_Floppy)
4525 {
4526 InsertConfigString(pLunL0, "Driver", "HostFloppy");
4527 InsertConfigNode(pLunL0, "Config", &pCfg);
4528
4529 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
4530 InsertConfigString(pCfg, "Path", bstr);
4531 }
4532 }
4533 else
4534 {
4535#if 0 /* Enable for I/O debugging */
4536 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL0);
4537 InsertConfigString(pLunL0, "Driver", "DiskIntegrity");
4538 InsertConfigNode(pLunL0, "Config", &pCfg);
4539 InsertConfigInteger(pCfg, "CheckConsistency", 0);
4540 InsertConfigInteger(pCfg, "CheckDoubleCompletions", 1);
4541#endif
4542
4543 InsertConfigString(pLunL0, "Driver", "VD");
4544 InsertConfigNode(pLunL0, "Config", &pCfg);
4545 switch (enmType)
4546 {
4547 case DeviceType_DVD:
4548 InsertConfigString(pCfg, "Type", "DVD");
4549 InsertConfigInteger(pCfg, "Mountable", 1);
4550 break;
4551 case DeviceType_Floppy:
4552 InsertConfigString(pCfg, "Type", "Floppy 1.44");
4553 InsertConfigInteger(pCfg, "Mountable", 1);
4554 break;
4555 case DeviceType_HardDisk:
4556 default:
4557 InsertConfigString(pCfg, "Type", "HardDisk");
4558 InsertConfigInteger(pCfg, "Mountable", 0);
4559 }
4560
4561 if ( pMedium
4562 && ( enmType == DeviceType_DVD
4563 || enmType == DeviceType_Floppy)
4564 )
4565 {
4566 // if this medium represents an ISO image and this image is inaccessible,
4567 // the ignore it instead of causing a failure; this can happen when we
4568 // restore a VM state and the ISO has disappeared, e.g. because the Guest
4569 // Additions were mounted and the user upgraded VirtualBox. Previously
4570 // we failed on startup, but that's not good because the only way out then
4571 // would be to discard the VM state...
4572 MediumState_T mediumState;
4573 hrc = pMedium->RefreshState(&mediumState); H();
4574 if (mediumState == MediumState_Inaccessible)
4575 {
4576 Bstr loc;
4577 hrc = pMedium->COMGETTER(Location)(loc.asOutParam()); H();
4578 i_atVMRuntimeErrorCallbackF(0, "DvdOrFloppyImageInaccessible",
4579 "The image file '%ls' is inaccessible and is being ignored. "
4580 "Please select a different image file for the virtual %s drive.",
4581 loc.raw(),
4582 enmType == DeviceType_DVD ? "DVD" : "floppy");
4583 pMedium = NULL;
4584 }
4585 }
4586
4587 if (pMedium)
4588 {
4589 /* Start with length of parent chain, as the list is reversed */
4590 unsigned uImage = 0;
4591 IMedium *pTmp = pMedium;
4592 while (pTmp)
4593 {
4594 uImage++;
4595 hrc = pTmp->COMGETTER(Parent)(&pTmp); H();
4596 }
4597 /* Index of last image */
4598 uImage--;
4599
4600# ifdef VBOX_WITH_EXTPACK
4601 if (mptrExtPackManager->i_isExtPackUsable(ORACLE_PUEL_EXTPACK_NAME))
4602 {
4603 /* Configure loading the VDPlugin. */
4604 static const char s_szVDPlugin[] = "VDPluginCrypt";
4605 PCFGMNODE pCfgPlugins = NULL;
4606 PCFGMNODE pCfgPlugin = NULL;
4607 Utf8Str strPlugin;
4608 hrc = mptrExtPackManager->i_getLibraryPathForExtPack(s_szVDPlugin, ORACLE_PUEL_EXTPACK_NAME, &strPlugin);
4609 // Don't fail, this is optional!
4610 if (SUCCEEDED(hrc))
4611 {
4612 InsertConfigNode(pCfg, "Plugins", &pCfgPlugins);
4613 InsertConfigNode(pCfgPlugins, s_szVDPlugin, &pCfgPlugin);
4614 InsertConfigString(pCfgPlugin, "Path", strPlugin.c_str());
4615 }
4616 }
4617# endif
4618
4619 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
4620 InsertConfigString(pCfg, "Path", bstr);
4621
4622 hrc = pMedium->COMGETTER(Format)(bstr.asOutParam()); H();
4623 InsertConfigString(pCfg, "Format", bstr);
4624
4625 if (mediumType == MediumType_Readonly)
4626 InsertConfigInteger(pCfg, "ReadOnly", 1);
4627 else if (enmType == DeviceType_Floppy)
4628 InsertConfigInteger(pCfg, "MaybeReadOnly", 1);
4629
4630 /* Start without exclusive write access to the images. */
4631 /** @todo Live Migration: I don't quite like this, we risk screwing up when
4632 * we're resuming the VM if some 3rd dude have any of the VDIs open
4633 * with write sharing denied. However, if the two VMs are sharing a
4634 * image it really is necessary....
4635 *
4636 * So, on the "lock-media" command, the target teleporter should also
4637 * make DrvVD undo TempReadOnly. It gets interesting if we fail after
4638 * that. Grumble. */
4639 if ( enmType == DeviceType_HardDisk
4640 && ( aMachineState == MachineState_TeleportingIn
4641 || aMachineState == MachineState_FaultTolerantSyncing))
4642 InsertConfigInteger(pCfg, "TempReadOnly", 1);
4643
4644 /* Flag for opening the medium for sharing between VMs. This
4645 * is done at the moment only for the first (and only) medium
4646 * in the chain, as shared media can have no diffs. */
4647 if (mediumType == MediumType_Shareable)
4648 InsertConfigInteger(pCfg, "Shareable", 1);
4649
4650 if (!fUseHostIOCache)
4651 {
4652 InsertConfigInteger(pCfg, "UseNewIo", 1);
4653 /*
4654 * Activate the builtin I/O cache for harddisks only.
4655 * It caches writes only which doesn't make sense for DVD drives
4656 * and just increases the overhead.
4657 */
4658 if ( fBuiltinIOCache
4659 && (enmType == DeviceType_HardDisk))
4660 InsertConfigInteger(pCfg, "BlockCache", 1);
4661 }
4662
4663 if (fSetupMerge)
4664 {
4665 InsertConfigInteger(pCfg, "SetupMerge", 1);
4666 if (uImage == uMergeSource)
4667 InsertConfigInteger(pCfg, "MergeSource", 1);
4668 else if (uImage == uMergeTarget)
4669 InsertConfigInteger(pCfg, "MergeTarget", 1);
4670 }
4671
4672 if (pcszBwGroup)
4673 InsertConfigString(pCfg, "BwGroup", pcszBwGroup);
4674
4675 if (fDiscard)
4676 InsertConfigInteger(pCfg, "Discard", 1);
4677
4678 if (fNonRotational)
4679 InsertConfigInteger(pCfg, "NonRotationalMedium", 1);
4680
4681 /* Pass all custom parameters. */
4682 bool fHostIP = true;
4683 bool fEncrypted = false;
4684 hrc = i_configMediumProperties(pCfg, pMedium, &fHostIP, &fEncrypted); H();
4685
4686 /* Create an inverted list of parents. */
4687 uImage--;
4688 IMedium *pParentMedium = pMedium;
4689 for (PCFGMNODE pParent = pCfg;; uImage--)
4690 {
4691 hrc = pParentMedium->COMGETTER(Parent)(&pMedium); H();
4692 if (!pMedium)
4693 break;
4694
4695 PCFGMNODE pCur;
4696 InsertConfigNode(pParent, "Parent", &pCur);
4697 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
4698 InsertConfigString(pCur, "Path", bstr);
4699
4700 hrc = pMedium->COMGETTER(Format)(bstr.asOutParam()); H();
4701 InsertConfigString(pCur, "Format", bstr);
4702
4703 if (fSetupMerge)
4704 {
4705 if (uImage == uMergeSource)
4706 InsertConfigInteger(pCur, "MergeSource", 1);
4707 else if (uImage == uMergeTarget)
4708 InsertConfigInteger(pCur, "MergeTarget", 1);
4709 }
4710
4711 /* Configure medium properties. */
4712 hrc = i_configMediumProperties(pCur, pMedium, &fHostIP, &fEncrypted); H();
4713
4714 /* next */
4715 pParent = pCur;
4716 pParentMedium = pMedium;
4717 }
4718
4719 /* Custom code: put marker to not use host IP stack to driver
4720 * configuration node. Simplifies life of DrvVD a bit. */
4721 if (!fHostIP)
4722 InsertConfigInteger(pCfg, "HostIPStack", 0);
4723
4724 if (fEncrypted)
4725 m_cDisksEncrypted++;
4726 }
4727 else
4728 {
4729 /* Set empty drive flag for DVD or floppy without media. */
4730 if ( enmType == DeviceType_DVD
4731 || enmType == DeviceType_Floppy)
4732 InsertConfigInteger(pCfg, "EmptyDrive", 1);
4733 }
4734 }
4735#undef H
4736 }
4737 catch (ConfigError &x)
4738 {
4739 // InsertConfig threw something:
4740 return x.m_vrc;
4741 }
4742
4743 return VINF_SUCCESS;
4744}
4745
4746/**
4747 * Adds the medium properties to the CFGM tree.
4748 *
4749 * @returns VBox status code.
4750 * @param pCur The current CFGM node.
4751 * @param pMedium The medium object to configure.
4752 * @param pfHostIP Where to return the value of the \"HostIPStack\" property if found.
4753 * @param pfEncrypted Where to return whether the medium is encrypted.
4754 */
4755int Console::i_configMediumProperties(PCFGMNODE pCur, IMedium *pMedium, bool *pfHostIP, bool *pfEncrypted)
4756{
4757 /* Pass all custom parameters. */
4758 SafeArray<BSTR> aNames;
4759 SafeArray<BSTR> aValues;
4760 HRESULT hrc = pMedium->GetProperties(NULL, ComSafeArrayAsOutParam(aNames),
4761 ComSafeArrayAsOutParam(aValues));
4762
4763 if ( SUCCEEDED(hrc)
4764 && aNames.size() != 0)
4765 {
4766 PCFGMNODE pVDC;
4767 InsertConfigNode(pCur, "VDConfig", &pVDC);
4768 for (size_t ii = 0; ii < aNames.size(); ++ii)
4769 {
4770 if (aValues[ii] && *aValues[ii])
4771 {
4772 Utf8Str name = aNames[ii];
4773 Utf8Str value = aValues[ii];
4774 size_t offSlash = name.find("/", 0);
4775 if ( offSlash != name.npos
4776 && !name.startsWith("Special/"))
4777 {
4778 com::Utf8Str strFilter;
4779 com::Utf8Str strKey;
4780
4781 hrc = strFilter.assignEx(name, 0, offSlash);
4782 if (FAILED(hrc))
4783 break;
4784
4785 hrc = strKey.assignEx(name, offSlash + 1, name.length() - offSlash - 1); /* Skip slash */
4786 if (FAILED(hrc))
4787 break;
4788
4789 PCFGMNODE pCfgFilterConfig = CFGMR3GetChild(pVDC, strFilter.c_str());
4790 if (!pCfgFilterConfig)
4791 InsertConfigNode(pVDC, strFilter.c_str(), &pCfgFilterConfig);
4792
4793 InsertConfigString(pCfgFilterConfig, strKey.c_str(), value);
4794 }
4795 else
4796 {
4797 InsertConfigString(pVDC, name.c_str(), value);
4798 if ( name.compare("HostIPStack") == 0
4799 && value.compare("0") == 0)
4800 *pfHostIP = false;
4801 }
4802
4803 if ( name.compare("CRYPT/KeyId") == 0
4804 && pfEncrypted)
4805 *pfEncrypted = true;
4806 }
4807 }
4808 }
4809
4810 return hrc;
4811}
4812
4813
4814#ifdef RT_OS_WINDOWS
4815DECLINLINE(bool) IsNdis6(void)
4816{
4817 LogFlowFunc(("entry\n"));
4818 HANDLE hFile = CreateFile(L"\\\\.\\VBoxNetLwf",
4819 0,
4820 FILE_SHARE_READ | FILE_SHARE_WRITE,
4821 NULL,
4822 OPEN_EXISTING,
4823 0,
4824 NULL);
4825 bool fNdis6 = hFile != INVALID_HANDLE_VALUE;
4826 if (fNdis6)
4827 CloseHandle(hFile);
4828 else
4829 LogFunc(("CreateFile failed with 0x%x\n", GetLastError()));
4830 LogFlowFunc(("return %s\n", fNdis6 ? "true" : "false"));
4831 return fNdis6;
4832}
4833#endif /* RT_OS_WINDOWS */
4834
4835
4836/**
4837 * Construct the Network configuration tree
4838 *
4839 * @returns VBox status code.
4840 *
4841 * @param pszDevice The PDM device name.
4842 * @param uInstance The PDM device instance.
4843 * @param uLun The PDM LUN number of the drive.
4844 * @param aNetworkAdapter The network adapter whose attachment needs to be changed
4845 * @param pCfg Configuration node for the device
4846 * @param pLunL0 To store the pointer to the LUN#0.
4847 * @param pInst The instance CFGM node
4848 * @param fAttachDetach To determine if the network attachment should
4849 * be attached/detached after/before
4850 * configuration.
4851 * @param fIgnoreConnectFailure
4852 * True if connection failures should be ignored
4853 * (makes only sense for bridged/host-only networks).
4854 *
4855 * @note Locks this object for writing.
4856 * @thread EMT
4857 */
4858int Console::i_configNetwork(const char *pszDevice,
4859 unsigned uInstance,
4860 unsigned uLun,
4861 INetworkAdapter *aNetworkAdapter,
4862 PCFGMNODE pCfg,
4863 PCFGMNODE pLunL0,
4864 PCFGMNODE pInst,
4865 bool fAttachDetach,
4866 bool fIgnoreConnectFailure)
4867{
4868 RT_NOREF(fIgnoreConnectFailure);
4869 AutoCaller autoCaller(this);
4870 AssertComRCReturn(autoCaller.rc(), VERR_ACCESS_DENIED);
4871
4872 // InsertConfig* throws
4873 try
4874 {
4875 int rc = VINF_SUCCESS;
4876 HRESULT hrc;
4877 Bstr bstr;
4878
4879#define H() AssertLogRelMsgReturn(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), VERR_MAIN_CONFIG_CONSTRUCTOR_COM_ERROR)
4880
4881 /*
4882 * Locking the object before doing VMR3* calls is quite safe here, since
4883 * we're on EMT. Write lock is necessary because we indirectly modify the
4884 * meAttachmentType member.
4885 */
4886 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
4887
4888 ComPtr<IMachine> pMachine = i_machine();
4889
4890 ComPtr<IVirtualBox> virtualBox;
4891 hrc = pMachine->COMGETTER(Parent)(virtualBox.asOutParam()); H();
4892
4893 ComPtr<IHost> host;
4894 hrc = virtualBox->COMGETTER(Host)(host.asOutParam()); H();
4895
4896 BOOL fSniffer;
4897 hrc = aNetworkAdapter->COMGETTER(TraceEnabled)(&fSniffer); H();
4898
4899 NetworkAdapterPromiscModePolicy_T enmPromiscModePolicy;
4900 hrc = aNetworkAdapter->COMGETTER(PromiscModePolicy)(&enmPromiscModePolicy); H();
4901 const char *pszPromiscuousGuestPolicy;
4902 switch (enmPromiscModePolicy)
4903 {
4904 case NetworkAdapterPromiscModePolicy_Deny: pszPromiscuousGuestPolicy = "deny"; break;
4905 case NetworkAdapterPromiscModePolicy_AllowNetwork: pszPromiscuousGuestPolicy = "allow-network"; break;
4906 case NetworkAdapterPromiscModePolicy_AllowAll: pszPromiscuousGuestPolicy = "allow-all"; break;
4907 default: AssertFailedReturn(VERR_INTERNAL_ERROR_4);
4908 }
4909
4910 if (fAttachDetach)
4911 {
4912 rc = PDMR3DeviceDetach(mpUVM, pszDevice, uInstance, uLun, 0 /*fFlags*/);
4913 if (rc == VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN)
4914 rc = VINF_SUCCESS;
4915 AssertLogRelRCReturn(rc, rc);
4916
4917 /* nuke anything which might have been left behind. */
4918 CFGMR3RemoveNode(CFGMR3GetChildF(pInst, "LUN#%u", uLun));
4919 }
4920
4921#ifdef VBOX_WITH_NETSHAPER
4922 ComObjPtr<IBandwidthGroup> pBwGroup;
4923 Bstr strBwGroup;
4924 hrc = aNetworkAdapter->COMGETTER(BandwidthGroup)(pBwGroup.asOutParam()); H();
4925
4926 if (!pBwGroup.isNull())
4927 {
4928 hrc = pBwGroup->COMGETTER(Name)(strBwGroup.asOutParam()); H();
4929 }
4930#endif /* VBOX_WITH_NETSHAPER */
4931
4932 Utf8Str strNetDriver;
4933
4934
4935 InsertConfigNode(pInst, "LUN#0", &pLunL0);
4936
4937#ifdef VBOX_WITH_NETSHAPER
4938 if (!strBwGroup.isEmpty())
4939 {
4940 InsertConfigString(pLunL0, "Driver", "NetShaper");
4941 InsertConfigNode(pLunL0, "Config", &pCfg);
4942 InsertConfigString(pCfg, "BwGroup", strBwGroup);
4943 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL0);
4944 }
4945#endif /* VBOX_WITH_NETSHAPER */
4946
4947 if (fSniffer)
4948 {
4949 InsertConfigString(pLunL0, "Driver", "NetSniffer");
4950 InsertConfigNode(pLunL0, "Config", &pCfg);
4951 hrc = aNetworkAdapter->COMGETTER(TraceFile)(bstr.asOutParam()); H();
4952 if (!bstr.isEmpty()) /* check convention for indicating default file. */
4953 InsertConfigString(pCfg, "File", bstr);
4954 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL0);
4955 }
4956
4957
4958 Bstr networkName, trunkName, trunkType;
4959 NetworkAttachmentType_T eAttachmentType;
4960 hrc = aNetworkAdapter->COMGETTER(AttachmentType)(&eAttachmentType); H();
4961 switch (eAttachmentType)
4962 {
4963 case NetworkAttachmentType_Null:
4964 break;
4965
4966 case NetworkAttachmentType_NAT:
4967 {
4968 ComPtr<INATEngine> natEngine;
4969 hrc = aNetworkAdapter->COMGETTER(NATEngine)(natEngine.asOutParam()); H();
4970 InsertConfigString(pLunL0, "Driver", "NAT");
4971 InsertConfigNode(pLunL0, "Config", &pCfg);
4972
4973 /* Configure TFTP prefix and boot filename. */
4974 hrc = virtualBox->COMGETTER(HomeFolder)(bstr.asOutParam()); H();
4975 if (!bstr.isEmpty())
4976 InsertConfigString(pCfg, "TFTPPrefix", Utf8StrFmt("%ls%c%s", bstr.raw(), RTPATH_DELIMITER, "TFTP"));
4977 hrc = pMachine->COMGETTER(Name)(bstr.asOutParam()); H();
4978 InsertConfigString(pCfg, "BootFile", Utf8StrFmt("%ls.pxe", bstr.raw()));
4979
4980 hrc = natEngine->COMGETTER(Network)(bstr.asOutParam()); H();
4981 if (!bstr.isEmpty())
4982 InsertConfigString(pCfg, "Network", bstr);
4983 else
4984 {
4985 ULONG uSlot;
4986 hrc = aNetworkAdapter->COMGETTER(Slot)(&uSlot); H();
4987 InsertConfigString(pCfg, "Network", Utf8StrFmt("10.0.%d.0/24", uSlot+2));
4988 }
4989 hrc = natEngine->COMGETTER(HostIP)(bstr.asOutParam()); H();
4990 if (!bstr.isEmpty())
4991 InsertConfigString(pCfg, "BindIP", bstr);
4992 ULONG mtu = 0;
4993 ULONG sockSnd = 0;
4994 ULONG sockRcv = 0;
4995 ULONG tcpSnd = 0;
4996 ULONG tcpRcv = 0;
4997 hrc = natEngine->GetNetworkSettings(&mtu, &sockSnd, &sockRcv, &tcpSnd, &tcpRcv); H();
4998 if (mtu)
4999 InsertConfigInteger(pCfg, "SlirpMTU", mtu);
5000 if (sockRcv)
5001 InsertConfigInteger(pCfg, "SockRcv", sockRcv);
5002 if (sockSnd)
5003 InsertConfigInteger(pCfg, "SockSnd", sockSnd);
5004 if (tcpRcv)
5005 InsertConfigInteger(pCfg, "TcpRcv", tcpRcv);
5006 if (tcpSnd)
5007 InsertConfigInteger(pCfg, "TcpSnd", tcpSnd);
5008 hrc = natEngine->COMGETTER(TFTPPrefix)(bstr.asOutParam()); H();
5009 if (!bstr.isEmpty())
5010 {
5011 RemoveConfigValue(pCfg, "TFTPPrefix");
5012 InsertConfigString(pCfg, "TFTPPrefix", bstr);
5013 }
5014 hrc = natEngine->COMGETTER(TFTPBootFile)(bstr.asOutParam()); H();
5015 if (!bstr.isEmpty())
5016 {
5017 RemoveConfigValue(pCfg, "BootFile");
5018 InsertConfigString(pCfg, "BootFile", bstr);
5019 }
5020 hrc = natEngine->COMGETTER(TFTPNextServer)(bstr.asOutParam()); H();
5021 if (!bstr.isEmpty())
5022 InsertConfigString(pCfg, "NextServer", bstr);
5023 BOOL fDNSFlag;
5024 hrc = natEngine->COMGETTER(DNSPassDomain)(&fDNSFlag); H();
5025 InsertConfigInteger(pCfg, "PassDomain", fDNSFlag);
5026 hrc = natEngine->COMGETTER(DNSProxy)(&fDNSFlag); H();
5027 InsertConfigInteger(pCfg, "DNSProxy", fDNSFlag);
5028 hrc = natEngine->COMGETTER(DNSUseHostResolver)(&fDNSFlag); H();
5029 InsertConfigInteger(pCfg, "UseHostResolver", fDNSFlag);
5030
5031 ULONG aliasMode;
5032 hrc = natEngine->COMGETTER(AliasMode)(&aliasMode); H();
5033 InsertConfigInteger(pCfg, "AliasMode", aliasMode);
5034
5035 /* port-forwarding */
5036 SafeArray<BSTR> pfs;
5037 hrc = natEngine->COMGETTER(Redirects)(ComSafeArrayAsOutParam(pfs)); H();
5038
5039 PCFGMNODE pPFTree = NULL;
5040 if (pfs.size() > 0)
5041 InsertConfigNode(pCfg, "PortForwarding", &pPFTree);
5042
5043 for (unsigned int i = 0; i < pfs.size(); ++i)
5044 {
5045 PCFGMNODE pPF = NULL; /* /Devices/Dev/.../Config/PortForwarding/$n/ */
5046
5047 uint16_t port = 0;
5048 BSTR r = pfs[i];
5049 Utf8Str utf = Utf8Str(r);
5050 Utf8Str strName;
5051 Utf8Str strProto;
5052 Utf8Str strHostPort;
5053 Utf8Str strHostIP;
5054 Utf8Str strGuestPort;
5055 Utf8Str strGuestIP;
5056 size_t pos, ppos;
5057 pos = ppos = 0;
5058#define ITERATE_TO_NEXT_TERM(res, str, pos, ppos) \
5059 { \
5060 pos = str.find(",", ppos); \
5061 if (pos == Utf8Str::npos) \
5062 { \
5063 Log(( #res " extracting from %s is failed\n", str.c_str())); \
5064 continue; \
5065 } \
5066 res = str.substr(ppos, pos - ppos); \
5067 Log2((#res " %s pos:%d, ppos:%d\n", res.c_str(), pos, ppos)); \
5068 ppos = pos + 1; \
5069 } /* no do { ... } while because of 'continue' */
5070 ITERATE_TO_NEXT_TERM(strName, utf, pos, ppos);
5071 ITERATE_TO_NEXT_TERM(strProto, utf, pos, ppos);
5072 ITERATE_TO_NEXT_TERM(strHostIP, utf, pos, ppos);
5073 ITERATE_TO_NEXT_TERM(strHostPort, utf, pos, ppos);
5074 ITERATE_TO_NEXT_TERM(strGuestIP, utf, pos, ppos);
5075 strGuestPort = utf.substr(ppos, utf.length() - ppos);
5076#undef ITERATE_TO_NEXT_TERM
5077
5078 uint32_t proto = strProto.toUInt32();
5079 bool fValid = true;
5080 switch (proto)
5081 {
5082 case NATProtocol_UDP:
5083 strProto = "UDP";
5084 break;
5085 case NATProtocol_TCP:
5086 strProto = "TCP";
5087 break;
5088 default:
5089 fValid = false;
5090 }
5091 /* continue with next rule if no valid proto was passed */
5092 if (!fValid)
5093 continue;
5094
5095 InsertConfigNode(pPFTree, Utf8StrFmt("%u", i).c_str(), &pPF);
5096
5097 if (!strName.isEmpty())
5098 InsertConfigString(pPF, "Name", strName);
5099
5100 InsertConfigString(pPF, "Protocol", strProto);
5101
5102 if (!strHostIP.isEmpty())
5103 InsertConfigString(pPF, "BindIP", strHostIP);
5104
5105 if (!strGuestIP.isEmpty())
5106 InsertConfigString(pPF, "GuestIP", strGuestIP);
5107
5108 port = RTStrToUInt16(strHostPort.c_str());
5109 if (port)
5110 InsertConfigInteger(pPF, "HostPort", port);
5111
5112 port = RTStrToUInt16(strGuestPort.c_str());
5113 if (port)
5114 InsertConfigInteger(pPF, "GuestPort", port);
5115 }
5116 break;
5117 }
5118
5119 case NetworkAttachmentType_Bridged:
5120 {
5121#if (defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)) && !defined(VBOX_WITH_NETFLT)
5122 hrc = i_attachToTapInterface(aNetworkAdapter);
5123 if (FAILED(hrc))
5124 {
5125 switch (hrc)
5126 {
5127 case VERR_ACCESS_DENIED:
5128 return VMSetError(VMR3GetVM(mpUVM), VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
5129 "Failed to open '/dev/net/tun' for read/write access. Please check the "
5130 "permissions of that node. Either run 'chmod 0666 /dev/net/tun' or "
5131 "change the group of that node and make yourself a member of that group. Make "
5132 "sure that these changes are permanent, especially if you are "
5133 "using udev"));
5134 default:
5135 AssertMsgFailed(("Could not attach to host interface! Bad!\n"));
5136 return VMSetError(VMR3GetVM(mpUVM), VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
5137 "Failed to initialize Host Interface Networking"));
5138 }
5139 }
5140
5141 Assert((intptr_t)maTapFD[uInstance] >= 0);
5142 if ((intptr_t)maTapFD[uInstance] >= 0)
5143 {
5144 InsertConfigString(pLunL0, "Driver", "HostInterface");
5145 InsertConfigNode(pLunL0, "Config", &pCfg);
5146 InsertConfigInteger(pCfg, "FileHandle", (intptr_t)maTapFD[uInstance]);
5147 }
5148
5149#elif defined(VBOX_WITH_NETFLT)
5150 /*
5151 * This is the new VBoxNetFlt+IntNet stuff.
5152 */
5153 Bstr BridgedIfName;
5154 hrc = aNetworkAdapter->COMGETTER(BridgedInterface)(BridgedIfName.asOutParam());
5155 if (FAILED(hrc))
5156 {
5157 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(BridgedInterface) failed, hrc (0x%x)\n", hrc));
5158 H();
5159 }
5160
5161 Utf8Str BridgedIfNameUtf8(BridgedIfName);
5162 const char *pszBridgedIfName = BridgedIfNameUtf8.c_str();
5163
5164# if defined(RT_OS_DARWIN)
5165 /* The name is on the form 'ifX: long name', chop it off at the colon. */
5166 char szTrunk[8];
5167 RTStrCopy(szTrunk, sizeof(szTrunk), pszBridgedIfName);
5168 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk));
5169// Quick fix for @bugref{5633}
5170// if (!pszColon)
5171// {
5172// /*
5173// * Dynamic changing of attachment causes an attempt to configure
5174// * network with invalid host adapter (as it is must be changed before
5175// * the attachment), calling Detach here will cause a deadlock.
5176// * See @bugref{4750}.
5177// * hrc = aNetworkAdapter->Detach(); H();
5178// */
5179// return VMSetError(VMR3GetVM(mpUVM), VERR_INTERNAL_ERROR, RT_SRC_POS,
5180// N_("Malformed host interface networking name '%ls'"),
5181// BridgedIfName.raw());
5182// }
5183 if (pszColon)
5184 *pszColon = '\0';
5185 const char *pszTrunk = szTrunk;
5186
5187# elif defined(RT_OS_SOLARIS)
5188 /* The name is on the form format 'ifX[:1] - long name, chop it off at space. */
5189 char szTrunk[256];
5190 strlcpy(szTrunk, pszBridgedIfName, sizeof(szTrunk));
5191 char *pszSpace = (char *)memchr(szTrunk, ' ', sizeof(szTrunk));
5192
5193 /*
5194 * Currently don't bother about malformed names here for the sake of people using
5195 * VBoxManage and setting only the NIC name from there. If there is a space we
5196 * chop it off and proceed, otherwise just use whatever we've got.
5197 */
5198 if (pszSpace)
5199 *pszSpace = '\0';
5200
5201 /* Chop it off at the colon (zone naming eg: e1000g:1 we need only the e1000g) */
5202 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk));
5203 if (pszColon)
5204 *pszColon = '\0';
5205
5206 const char *pszTrunk = szTrunk;
5207
5208# elif defined(RT_OS_WINDOWS)
5209 ComPtr<IHostNetworkInterface> hostInterface;
5210 hrc = host->FindHostNetworkInterfaceByName(BridgedIfName.raw(),
5211 hostInterface.asOutParam());
5212 if (!SUCCEEDED(hrc))
5213 {
5214 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: FindByName failed, rc=%Rhrc (0x%x)", hrc, hrc));
5215 return VMSetError(VMR3GetVM(mpUVM), VERR_INTERNAL_ERROR, RT_SRC_POS,
5216 N_("Nonexistent host networking interface, name '%ls'"),
5217 BridgedIfName.raw());
5218 }
5219
5220 HostNetworkInterfaceType_T eIfType;
5221 hrc = hostInterface->COMGETTER(InterfaceType)(&eIfType);
5222 if (FAILED(hrc))
5223 {
5224 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(InterfaceType) failed, hrc (0x%x)\n", hrc));
5225 H();
5226 }
5227
5228 if (eIfType != HostNetworkInterfaceType_Bridged)
5229 {
5230 return VMSetError(VMR3GetVM(mpUVM), VERR_INTERNAL_ERROR, RT_SRC_POS,
5231 N_("Interface ('%ls') is not a Bridged Adapter interface"),
5232 BridgedIfName.raw());
5233 }
5234
5235 hrc = hostInterface->COMGETTER(Id)(bstr.asOutParam());
5236 if (FAILED(hrc))
5237 {
5238 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(Id) failed, hrc (0x%x)\n", hrc));
5239 H();
5240 }
5241 Guid hostIFGuid(bstr);
5242
5243 INetCfg *pNc;
5244 ComPtr<INetCfgComponent> pAdaptorComponent;
5245 LPWSTR pszApp;
5246
5247 hrc = VBoxNetCfgWinQueryINetCfg(&pNc, FALSE, L"VirtualBox", 10, &pszApp);
5248 Assert(hrc == S_OK);
5249 if (hrc != S_OK)
5250 {
5251 LogRel(("NetworkAttachmentType_Bridged: Failed to get NetCfg, hrc=%Rhrc (0x%x)\n", hrc, hrc));
5252 H();
5253 }
5254
5255 /* get the adapter's INetCfgComponent*/
5256 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.raw(),
5257 pAdaptorComponent.asOutParam());
5258 if (hrc != S_OK)
5259 {
5260 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
5261 LogRel(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)\n", hrc));
5262 H();
5263 }
5264# define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\"
5265 char szTrunkName[INTNET_MAX_TRUNK_NAME];
5266 char *pszTrunkName = szTrunkName;
5267 wchar_t * pswzBindName;
5268 hrc = pAdaptorComponent->GetBindName(&pswzBindName);
5269 Assert(hrc == S_OK);
5270 if (hrc == S_OK)
5271 {
5272 int cwBindName = (int)wcslen(pswzBindName) + 1;
5273 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX);
5274 if (sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName)
5275 {
5276 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX);
5277 pszTrunkName += cbFullBindNamePrefix-1;
5278 if (!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName,
5279 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL))
5280 {
5281 DWORD err = GetLastError();
5282 hrc = HRESULT_FROM_WIN32(err);
5283 AssertMsgFailed(("hrc=%Rhrc %#x\n", hrc, hrc));
5284 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: WideCharToMultiByte failed, hr=%Rhrc (0x%x) err=%u\n",
5285 hrc, hrc, err));
5286 }
5287 }
5288 else
5289 {
5290 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: insufficient szTrunkName buffer space\n"));
5291 /** @todo set appropriate error code */
5292 hrc = E_FAIL;
5293 }
5294
5295 if (hrc != S_OK)
5296 {
5297 AssertFailed();
5298 CoTaskMemFree(pswzBindName);
5299 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
5300 H();
5301 }
5302
5303 /* we're not freeing the bind name since we'll use it later for detecting wireless*/
5304 }
5305 else
5306 {
5307 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
5308 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)",
5309 hrc));
5310 H();
5311 }
5312
5313 const char *pszTrunk = szTrunkName;
5314 /* we're not releasing the INetCfg stuff here since we use it later to figure out whether it is wireless */
5315
5316# elif defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)
5317# if defined(RT_OS_FREEBSD)
5318 /*
5319 * If we bridge to a tap interface open it the `old' direct way.
5320 * This works and performs better than bridging a physical
5321 * interface via the current FreeBSD vboxnetflt implementation.
5322 */
5323 if (!strncmp(pszBridgedIfName, RT_STR_TUPLE("tap"))) {
5324 hrc = i_attachToTapInterface(aNetworkAdapter);
5325 if (FAILED(hrc))
5326 {
5327 switch (hrc)
5328 {
5329 case VERR_ACCESS_DENIED:
5330 return VMSetError(VMR3GetVM(mpUVM), VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
5331 "Failed to open '/dev/%s' for read/write access. Please check the "
5332 "permissions of that node, and that the net.link.tap.user_open "
5333 "sysctl is set. Either run 'chmod 0666 /dev/%s' or "
5334 "change the group of that node to vboxusers and make yourself "
5335 "a member of that group. Make sure that these changes are permanent."),
5336 pszBridgedIfName, pszBridgedIfName);
5337 default:
5338 AssertMsgFailed(("Could not attach to tap interface! Bad!\n"));
5339 return VMSetError(VMR3GetVM(mpUVM), VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
5340 "Failed to initialize Host Interface Networking"));
5341 }
5342 }
5343
5344 Assert((intptr_t)maTapFD[uInstance] >= 0);
5345 if ((intptr_t)maTapFD[uInstance] >= 0)
5346 {
5347 InsertConfigString(pLunL0, "Driver", "HostInterface");
5348 InsertConfigNode(pLunL0, "Config", &pCfg);
5349 InsertConfigInteger(pCfg, "FileHandle", (intptr_t)maTapFD[uInstance]);
5350 }
5351 break;
5352 }
5353# endif
5354 /** @todo Check for malformed names. */
5355 const char *pszTrunk = pszBridgedIfName;
5356
5357 /* Issue a warning if the interface is down */
5358 {
5359 int iSock = socket(AF_INET, SOCK_DGRAM, 0);
5360 if (iSock >= 0)
5361 {
5362 struct ifreq Req;
5363 RT_ZERO(Req);
5364 RTStrCopy(Req.ifr_name, sizeof(Req.ifr_name), pszBridgedIfName);
5365 if (ioctl(iSock, SIOCGIFFLAGS, &Req) >= 0)
5366 if ((Req.ifr_flags & IFF_UP) == 0)
5367 i_atVMRuntimeErrorCallbackF(0, "BridgedInterfaceDown",
5368 N_("Bridged interface %s is down. Guest will not be able to use this interface"),
5369 pszBridgedIfName);
5370
5371 close(iSock);
5372 }
5373 }
5374
5375# else
5376# error "PORTME (VBOX_WITH_NETFLT)"
5377# endif
5378
5379 InsertConfigString(pLunL0, "Driver", "IntNet");
5380 InsertConfigNode(pLunL0, "Config", &pCfg);
5381 InsertConfigString(pCfg, "Trunk", pszTrunk);
5382 InsertConfigInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt);
5383 InsertConfigInteger(pCfg, "IgnoreConnectFailure", (uint64_t)fIgnoreConnectFailure);
5384 InsertConfigString(pCfg, "IfPolicyPromisc", pszPromiscuousGuestPolicy);
5385 char szNetwork[INTNET_MAX_NETWORK_NAME];
5386
5387# if defined(RT_OS_SOLARIS) || defined(RT_OS_DARWIN)
5388 /*
5389 * 'pszTrunk' contains just the interface name required in ring-0, while 'pszBridgedIfName' contains
5390 * interface name + optional description. We must not pass any description to the VM as it can differ
5391 * for the same interface name, eg: "nge0 - ethernet" (GUI) vs "nge0" (VBoxManage).
5392 */
5393 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszTrunk);
5394# else
5395 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszBridgedIfName);
5396# endif
5397 InsertConfigString(pCfg, "Network", szNetwork);
5398 networkName = Bstr(szNetwork);
5399 trunkName = Bstr(pszTrunk);
5400 trunkType = Bstr(TRUNKTYPE_NETFLT);
5401
5402# if defined(RT_OS_DARWIN)
5403 /** @todo Come up with a better deal here. Problem is that IHostNetworkInterface is completely useless here. */
5404 if ( strstr(pszBridgedIfName, "Wireless")
5405 || strstr(pszBridgedIfName, "AirPort" ))
5406 InsertConfigInteger(pCfg, "SharedMacOnWire", true);
5407# elif defined(RT_OS_LINUX)
5408 int iSock = socket(AF_INET, SOCK_DGRAM, 0);
5409 if (iSock >= 0)
5410 {
5411 struct iwreq WRq;
5412
5413 RT_ZERO(WRq);
5414 strncpy(WRq.ifr_name, pszBridgedIfName, IFNAMSIZ);
5415 bool fSharedMacOnWire = ioctl(iSock, SIOCGIWNAME, &WRq) >= 0;
5416 close(iSock);
5417 if (fSharedMacOnWire)
5418 {
5419 InsertConfigInteger(pCfg, "SharedMacOnWire", true);
5420 Log(("Set SharedMacOnWire\n"));
5421 }
5422 else
5423 Log(("Failed to get wireless name\n"));
5424 }
5425 else
5426 Log(("Failed to open wireless socket\n"));
5427# elif defined(RT_OS_FREEBSD)
5428 int iSock = socket(AF_INET, SOCK_DGRAM, 0);
5429 if (iSock >= 0)
5430 {
5431 struct ieee80211req WReq;
5432 uint8_t abData[32];
5433
5434 RT_ZERO(WReq);
5435 strncpy(WReq.i_name, pszBridgedIfName, sizeof(WReq.i_name));
5436 WReq.i_type = IEEE80211_IOC_SSID;
5437 WReq.i_val = -1;
5438 WReq.i_data = abData;
5439 WReq.i_len = sizeof(abData);
5440
5441 bool fSharedMacOnWire = ioctl(iSock, SIOCG80211, &WReq) >= 0;
5442 close(iSock);
5443 if (fSharedMacOnWire)
5444 {
5445 InsertConfigInteger(pCfg, "SharedMacOnWire", true);
5446 Log(("Set SharedMacOnWire\n"));
5447 }
5448 else
5449 Log(("Failed to get wireless name\n"));
5450 }
5451 else
5452 Log(("Failed to open wireless socket\n"));
5453# elif defined(RT_OS_WINDOWS)
5454# define DEVNAME_PREFIX L"\\\\.\\"
5455 /* we are getting the medium type via IOCTL_NDIS_QUERY_GLOBAL_STATS Io Control
5456 * there is a pretty long way till there though since we need to obtain the symbolic link name
5457 * for the adapter device we are going to query given the device Guid */
5458
5459
5460 /* prepend the "\\\\.\\" to the bind name to obtain the link name */
5461
5462 wchar_t FileName[MAX_PATH];
5463 wcscpy(FileName, DEVNAME_PREFIX);
5464 wcscpy((wchar_t*)(((char*)FileName) + sizeof(DEVNAME_PREFIX) - sizeof(FileName[0])), pswzBindName);
5465
5466 /* open the device */
5467 HANDLE hDevice = CreateFile(FileName,
5468 GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
5469 NULL,
5470 OPEN_EXISTING,
5471 FILE_ATTRIBUTE_NORMAL,
5472 NULL);
5473
5474 if (hDevice != INVALID_HANDLE_VALUE)
5475 {
5476 bool fSharedMacOnWire = false;
5477
5478 /* now issue the OID_GEN_PHYSICAL_MEDIUM query */
5479 DWORD Oid = OID_GEN_PHYSICAL_MEDIUM;
5480 NDIS_PHYSICAL_MEDIUM PhMedium;
5481 DWORD cbResult;
5482 if (DeviceIoControl(hDevice,
5483 IOCTL_NDIS_QUERY_GLOBAL_STATS,
5484 &Oid,
5485 sizeof(Oid),
5486 &PhMedium,
5487 sizeof(PhMedium),
5488 &cbResult,
5489 NULL))
5490 {
5491 /* that was simple, now examine PhMedium */
5492 if ( PhMedium == NdisPhysicalMediumWirelessWan
5493 || PhMedium == NdisPhysicalMediumWirelessLan
5494 || PhMedium == NdisPhysicalMediumNative802_11
5495 || PhMedium == NdisPhysicalMediumBluetooth)
5496 fSharedMacOnWire = true;
5497 }
5498 else
5499 {
5500 int winEr = GetLastError();
5501 LogRel(("Console::configNetwork: DeviceIoControl failed, err (0x%x), ignoring\n", winEr));
5502 Assert(winEr == ERROR_INVALID_PARAMETER || winEr == ERROR_NOT_SUPPORTED || winEr == ERROR_BAD_COMMAND);
5503 }
5504 CloseHandle(hDevice);
5505
5506 if (fSharedMacOnWire)
5507 {
5508 Log(("this is a wireless adapter"));
5509 InsertConfigInteger(pCfg, "SharedMacOnWire", true);
5510 Log(("Set SharedMacOnWire\n"));
5511 }
5512 else
5513 Log(("this is NOT a wireless adapter"));
5514 }
5515 else
5516 {
5517 int winEr = GetLastError();
5518 AssertLogRelMsgFailed(("Console::configNetwork: CreateFile failed, err (0x%x), ignoring\n", winEr));
5519 }
5520
5521 CoTaskMemFree(pswzBindName);
5522
5523 pAdaptorComponent.setNull();
5524 /* release the pNc finally */
5525 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
5526# else
5527 /** @todo PORTME: wireless detection */
5528# endif
5529
5530# if defined(RT_OS_SOLARIS)
5531# if 0 /* bird: this is a bit questionable and might cause more trouble than its worth. */
5532 /* Zone access restriction, don't allow snooping the global zone. */
5533 zoneid_t ZoneId = getzoneid();
5534 if (ZoneId != GLOBAL_ZONEID)
5535 {
5536 InsertConfigInteger(pCfg, "IgnoreAllPromisc", true);
5537 }
5538# endif
5539# endif
5540
5541#elif defined(RT_OS_WINDOWS) /* not defined NetFlt */
5542 /* NOTHING TO DO HERE */
5543#elif defined(RT_OS_LINUX)
5544/// @todo aleksey: is there anything to be done here?
5545#elif defined(RT_OS_FREEBSD)
5546/** @todo FreeBSD: Check out this later (HIF networking). */
5547#else
5548# error "Port me"
5549#endif
5550 break;
5551 }
5552
5553 case NetworkAttachmentType_Internal:
5554 {
5555 hrc = aNetworkAdapter->COMGETTER(InternalNetwork)(bstr.asOutParam()); H();
5556 if (!bstr.isEmpty())
5557 {
5558 InsertConfigString(pLunL0, "Driver", "IntNet");
5559 InsertConfigNode(pLunL0, "Config", &pCfg);
5560 InsertConfigString(pCfg, "Network", bstr);
5561 InsertConfigInteger(pCfg, "TrunkType", kIntNetTrunkType_WhateverNone);
5562 InsertConfigString(pCfg, "IfPolicyPromisc", pszPromiscuousGuestPolicy);
5563 networkName = bstr;
5564 trunkType = Bstr(TRUNKTYPE_WHATEVER);
5565 }
5566 break;
5567 }
5568
5569 case NetworkAttachmentType_HostOnly:
5570 {
5571 InsertConfigString(pLunL0, "Driver", "IntNet");
5572 InsertConfigNode(pLunL0, "Config", &pCfg);
5573
5574 Bstr HostOnlyName;
5575 hrc = aNetworkAdapter->COMGETTER(HostOnlyInterface)(HostOnlyName.asOutParam());
5576 if (FAILED(hrc))
5577 {
5578 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(HostOnlyInterface) failed, hrc (0x%x)\n", hrc));
5579 H();
5580 }
5581
5582 Utf8Str HostOnlyNameUtf8(HostOnlyName);
5583 const char *pszHostOnlyName = HostOnlyNameUtf8.c_str();
5584 ComPtr<IHostNetworkInterface> hostInterface;
5585 rc = host->FindHostNetworkInterfaceByName(HostOnlyName.raw(),
5586 hostInterface.asOutParam());
5587 if (!SUCCEEDED(rc))
5588 {
5589 LogRel(("NetworkAttachmentType_HostOnly: FindByName failed, rc (0x%x)\n", rc));
5590 return VMSetError(VMR3GetVM(mpUVM), VERR_INTERNAL_ERROR, RT_SRC_POS,
5591 N_("Nonexistent host networking interface, name '%ls'"),
5592 HostOnlyName.raw());
5593 }
5594
5595 char szNetwork[INTNET_MAX_NETWORK_NAME];
5596 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHostOnlyName);
5597
5598#if defined(RT_OS_WINDOWS)
5599# ifndef VBOX_WITH_NETFLT
5600 hrc = E_NOTIMPL;
5601 LogRel(("NetworkAttachmentType_HostOnly: Not Implemented\n"));
5602 H();
5603# else /* defined VBOX_WITH_NETFLT*/
5604 /** @todo r=bird: Put this in a function. */
5605
5606 HostNetworkInterfaceType_T eIfType;
5607 hrc = hostInterface->COMGETTER(InterfaceType)(&eIfType);
5608 if (FAILED(hrc))
5609 {
5610 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(InterfaceType) failed, hrc (0x%x)\n", hrc));
5611 H();
5612 }
5613
5614 if (eIfType != HostNetworkInterfaceType_HostOnly)
5615 return VMSetError(VMR3GetVM(mpUVM), VERR_INTERNAL_ERROR, RT_SRC_POS,
5616 N_("Interface ('%ls') is not a Host-Only Adapter interface"),
5617 HostOnlyName.raw());
5618
5619 hrc = hostInterface->COMGETTER(Id)(bstr.asOutParam());
5620 if (FAILED(hrc))
5621 {
5622 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(Id) failed, hrc (0x%x)\n", hrc));
5623 H();
5624 }
5625 Guid hostIFGuid(bstr);
5626
5627 INetCfg *pNc;
5628 ComPtr<INetCfgComponent> pAdaptorComponent;
5629 LPWSTR pszApp;
5630 hrc = VBoxNetCfgWinQueryINetCfg(&pNc, FALSE, L"VirtualBox", 10, &pszApp);
5631 Assert(hrc == S_OK);
5632 if (hrc != S_OK)
5633 {
5634 LogRel(("NetworkAttachmentType_HostOnly: Failed to get NetCfg, hrc=%Rhrc (0x%x)\n", hrc, hrc));
5635 H();
5636 }
5637
5638 /* get the adapter's INetCfgComponent*/
5639 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.raw(),
5640 pAdaptorComponent.asOutParam());
5641 if (hrc != S_OK)
5642 {
5643 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
5644 LogRel(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc=%Rhrc (0x%x)\n", hrc, hrc));
5645 H();
5646 }
5647# define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\"
5648 char szTrunkName[INTNET_MAX_TRUNK_NAME];
5649 char *pszTrunkName = szTrunkName;
5650 wchar_t * pswzBindName;
5651 hrc = pAdaptorComponent->GetBindName(&pswzBindName);
5652 Assert(hrc == S_OK);
5653 if (hrc == S_OK)
5654 {
5655 int cwBindName = (int)wcslen(pswzBindName) + 1;
5656 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX);
5657 if (sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName)
5658 {
5659 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX);
5660 pszTrunkName += cbFullBindNamePrefix-1;
5661 if (!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName,
5662 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL))
5663 {
5664 DWORD err = GetLastError();
5665 hrc = HRESULT_FROM_WIN32(err);
5666 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: WideCharToMultiByte failed, hr=%Rhrc (0x%x) err=%u\n",
5667 hrc, hrc, err));
5668 }
5669 }
5670 else
5671 {
5672 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: insufficient szTrunkName buffer space\n"));
5673 /** @todo set appropriate error code */
5674 hrc = E_FAIL;
5675 }
5676
5677 if (hrc != S_OK)
5678 {
5679 AssertFailed();
5680 CoTaskMemFree(pswzBindName);
5681 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
5682 H();
5683 }
5684 }
5685 else
5686 {
5687 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
5688 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc=%Rhrc (0x%x)\n",
5689 hrc, hrc));
5690 H();
5691 }
5692
5693
5694 CoTaskMemFree(pswzBindName);
5695
5696 /* The old NDIS5.1 version of driver uses TRUNKTYPE_NETADP */
5697 trunkType = IsNdis6() ? TRUNKTYPE_NETFLT : TRUNKTYPE_NETADP;
5698 InsertConfigInteger(pCfg, "TrunkType", trunkType == TRUNKTYPE_NETFLT ? kIntNetTrunkType_NetFlt : kIntNetTrunkType_NetAdp);
5699
5700 pAdaptorComponent.setNull();
5701 /* release the pNc finally */
5702 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
5703
5704 const char *pszTrunk = szTrunkName;
5705
5706 InsertConfigString(pCfg, "Trunk", pszTrunk);
5707 InsertConfigString(pCfg, "Network", szNetwork);
5708 InsertConfigInteger(pCfg, "IgnoreConnectFailure", (uint64_t)fIgnoreConnectFailure); /** @todo why is this
5709 windows only?? */
5710 networkName = Bstr(szNetwork);
5711 trunkName = Bstr(pszTrunk);
5712# endif /* defined VBOX_WITH_NETFLT*/
5713#elif defined(RT_OS_DARWIN)
5714 InsertConfigString(pCfg, "Trunk", pszHostOnlyName);
5715 InsertConfigString(pCfg, "Network", szNetwork);
5716 InsertConfigInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp);
5717 networkName = Bstr(szNetwork);
5718 trunkName = Bstr(pszHostOnlyName);
5719 trunkType = TRUNKTYPE_NETADP;
5720#else
5721 InsertConfigString(pCfg, "Trunk", pszHostOnlyName);
5722 InsertConfigString(pCfg, "Network", szNetwork);
5723 InsertConfigInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt);
5724 networkName = Bstr(szNetwork);
5725 trunkName = Bstr(pszHostOnlyName);
5726 trunkType = TRUNKTYPE_NETFLT;
5727#endif
5728 InsertConfigString(pCfg, "IfPolicyPromisc", pszPromiscuousGuestPolicy);
5729
5730#if !defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT)
5731
5732 Bstr tmpAddr, tmpMask;
5733
5734 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPAddress",
5735 pszHostOnlyName).raw(),
5736 tmpAddr.asOutParam());
5737 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty())
5738 {
5739 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPNetMask",
5740 pszHostOnlyName).raw(),
5741 tmpMask.asOutParam());
5742 if (SUCCEEDED(hrc) && !tmpMask.isEmpty())
5743 hrc = hostInterface->EnableStaticIPConfig(tmpAddr.raw(),
5744 tmpMask.raw());
5745 else
5746 hrc = hostInterface->EnableStaticIPConfig(tmpAddr.raw(),
5747 Bstr(VBOXNET_IPV4MASK_DEFAULT).raw());
5748 }
5749 else
5750 {
5751 /* Grab the IP number from the 'vboxnetX' instance number (see netif.h) */
5752 hrc = hostInterface->EnableStaticIPConfig(getDefaultIPv4Address(Bstr(pszHostOnlyName)).raw(),
5753 Bstr(VBOXNET_IPV4MASK_DEFAULT).raw());
5754 }
5755
5756 ComAssertComRC(hrc); /** @todo r=bird: Why this isn't fatal? (H()) */
5757
5758 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPV6Address",
5759 pszHostOnlyName).raw(),
5760 tmpAddr.asOutParam());
5761 if (SUCCEEDED(hrc))
5762 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPV6NetMask", pszHostOnlyName).raw(),
5763 tmpMask.asOutParam());
5764 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty() && !tmpMask.isEmpty())
5765 {
5766 hrc = hostInterface->EnableStaticIPConfigV6(tmpAddr.raw(),
5767 Utf8Str(tmpMask).toUInt32());
5768 ComAssertComRC(hrc); /** @todo r=bird: Why this isn't fatal? (H()) */
5769 }
5770#endif
5771 break;
5772 }
5773
5774 case NetworkAttachmentType_Generic:
5775 {
5776 hrc = aNetworkAdapter->COMGETTER(GenericDriver)(bstr.asOutParam()); H();
5777 SafeArray<BSTR> names;
5778 SafeArray<BSTR> values;
5779 hrc = aNetworkAdapter->GetProperties(Bstr().raw(),
5780 ComSafeArrayAsOutParam(names),
5781 ComSafeArrayAsOutParam(values)); H();
5782
5783 InsertConfigString(pLunL0, "Driver", bstr);
5784 InsertConfigNode(pLunL0, "Config", &pCfg);
5785 for (size_t ii = 0; ii < names.size(); ++ii)
5786 {
5787 if (values[ii] && *values[ii])
5788 {
5789 Utf8Str name = names[ii];
5790 Utf8Str value = values[ii];
5791 InsertConfigString(pCfg, name.c_str(), value);
5792 }
5793 }
5794 break;
5795 }
5796
5797 case NetworkAttachmentType_NATNetwork:
5798 {
5799 hrc = aNetworkAdapter->COMGETTER(NATNetwork)(bstr.asOutParam()); H();
5800 if (!bstr.isEmpty())
5801 {
5802 /** @todo add intnet prefix to separate namespaces, and add trunk if dealing with vboxnatX */
5803 InsertConfigString(pLunL0, "Driver", "IntNet");
5804 InsertConfigNode(pLunL0, "Config", &pCfg);
5805 InsertConfigString(pCfg, "Network", bstr);
5806 InsertConfigInteger(pCfg, "TrunkType", kIntNetTrunkType_WhateverNone);
5807 InsertConfigString(pCfg, "IfPolicyPromisc", pszPromiscuousGuestPolicy);
5808 networkName = bstr;
5809 trunkType = Bstr(TRUNKTYPE_WHATEVER);
5810 }
5811 break;
5812 }
5813
5814 default:
5815 AssertMsgFailed(("should not get here!\n"));
5816 break;
5817 }
5818
5819 /*
5820 * Attempt to attach the driver.
5821 */
5822 switch (eAttachmentType)
5823 {
5824 case NetworkAttachmentType_Null:
5825 break;
5826
5827 case NetworkAttachmentType_Bridged:
5828 case NetworkAttachmentType_Internal:
5829 case NetworkAttachmentType_HostOnly:
5830 case NetworkAttachmentType_NAT:
5831 case NetworkAttachmentType_Generic:
5832 case NetworkAttachmentType_NATNetwork:
5833 {
5834 if (SUCCEEDED(hrc) && SUCCEEDED(rc))
5835 {
5836 if (fAttachDetach)
5837 {
5838 rc = PDMR3DriverAttach(mpUVM, pszDevice, uInstance, uLun, 0 /*fFlags*/, NULL /* ppBase */);
5839 //AssertRC(rc);
5840 }
5841
5842 {
5843 /** @todo pritesh: get the dhcp server name from the
5844 * previous network configuration and then stop the server
5845 * else it may conflict with the dhcp server running with
5846 * the current attachment type
5847 */
5848 /* Stop the hostonly DHCP Server */
5849 }
5850
5851 /*
5852 * NAT networks start their DHCP server theirself, see NATNetwork::Start()
5853 */
5854 if ( !networkName.isEmpty()
5855 && eAttachmentType != NetworkAttachmentType_NATNetwork)
5856 {
5857 /*
5858 * Until we implement service reference counters DHCP Server will be stopped
5859 * by DHCPServerRunner destructor.
5860 */
5861 ComPtr<IDHCPServer> dhcpServer;
5862 hrc = virtualBox->FindDHCPServerByNetworkName(networkName.raw(),
5863 dhcpServer.asOutParam());
5864 if (SUCCEEDED(hrc))
5865 {
5866 /* there is a DHCP server available for this network */
5867 BOOL fEnabledDhcp;
5868 hrc = dhcpServer->COMGETTER(Enabled)(&fEnabledDhcp);
5869 if (FAILED(hrc))
5870 {
5871 LogRel(("DHCP svr: COMGETTER(Enabled) failed, hrc (%Rhrc)\n", hrc));
5872 H();
5873 }
5874
5875 if (fEnabledDhcp)
5876 hrc = dhcpServer->Start(networkName.raw(),
5877 trunkName.raw(),
5878 trunkType.raw());
5879 }
5880 else
5881 hrc = S_OK;
5882 }
5883 }
5884
5885 break;
5886 }
5887
5888 default:
5889 AssertMsgFailed(("should not get here!\n"));
5890 break;
5891 }
5892
5893 meAttachmentType[uInstance] = eAttachmentType;
5894 }
5895 catch (ConfigError &x)
5896 {
5897 // InsertConfig threw something:
5898 return x.m_vrc;
5899 }
5900
5901#undef H
5902
5903 return VINF_SUCCESS;
5904}
5905
5906#ifdef VBOX_WITH_GUEST_PROPS
5907/**
5908 * Set an array of guest properties
5909 */
5910static void configSetProperties(VMMDev * const pVMMDev,
5911 void *names,
5912 void *values,
5913 void *timestamps,
5914 void *flags)
5915{
5916 VBOXHGCMSVCPARM parms[4];
5917
5918 parms[0].type = VBOX_HGCM_SVC_PARM_PTR;
5919 parms[0].u.pointer.addr = names;
5920 parms[0].u.pointer.size = 0; /* We don't actually care. */
5921 parms[1].type = VBOX_HGCM_SVC_PARM_PTR;
5922 parms[1].u.pointer.addr = values;
5923 parms[1].u.pointer.size = 0; /* We don't actually care. */
5924 parms[2].type = VBOX_HGCM_SVC_PARM_PTR;
5925 parms[2].u.pointer.addr = timestamps;
5926 parms[2].u.pointer.size = 0; /* We don't actually care. */
5927 parms[3].type = VBOX_HGCM_SVC_PARM_PTR;
5928 parms[3].u.pointer.addr = flags;
5929 parms[3].u.pointer.size = 0; /* We don't actually care. */
5930
5931 pVMMDev->hgcmHostCall("VBoxGuestPropSvc",
5932 guestProp::SET_PROPS_HOST,
5933 4,
5934 &parms[0]);
5935}
5936
5937/**
5938 * Set a single guest property
5939 */
5940static void configSetProperty(VMMDev * const pVMMDev,
5941 const char *pszName,
5942 const char *pszValue,
5943 const char *pszFlags)
5944{
5945 VBOXHGCMSVCPARM parms[4];
5946
5947 AssertPtrReturnVoid(pszName);
5948 AssertPtrReturnVoid(pszValue);
5949 AssertPtrReturnVoid(pszFlags);
5950 parms[0].type = VBOX_HGCM_SVC_PARM_PTR;
5951 parms[0].u.pointer.addr = (void *)pszName;
5952 parms[0].u.pointer.size = (uint32_t)strlen(pszName) + 1;
5953 parms[1].type = VBOX_HGCM_SVC_PARM_PTR;
5954 parms[1].u.pointer.addr = (void *)pszValue;
5955 parms[1].u.pointer.size = (uint32_t)strlen(pszValue) + 1;
5956 parms[2].type = VBOX_HGCM_SVC_PARM_PTR;
5957 parms[2].u.pointer.addr = (void *)pszFlags;
5958 parms[2].u.pointer.size = (uint32_t)strlen(pszFlags) + 1;
5959 pVMMDev->hgcmHostCall("VBoxGuestPropSvc", guestProp::SET_PROP_HOST, 3,
5960 &parms[0]);
5961}
5962
5963/**
5964 * Set the global flags value by calling the service
5965 * @returns the status returned by the call to the service
5966 *
5967 * @param pTable the service instance handle
5968 * @param eFlags the flags to set
5969 */
5970int configSetGlobalPropertyFlags(VMMDev * const pVMMDev,
5971 guestProp::ePropFlags eFlags)
5972{
5973 VBOXHGCMSVCPARM paParm;
5974 paParm.setUInt32(eFlags);
5975 int rc = pVMMDev->hgcmHostCall("VBoxGuestPropSvc",
5976 guestProp::SET_GLOBAL_FLAGS_HOST, 1,
5977 &paParm);
5978 if (RT_FAILURE(rc))
5979 {
5980 char szFlags[guestProp::MAX_FLAGS_LEN];
5981 if (RT_FAILURE(writeFlags(eFlags, szFlags)))
5982 Log(("Failed to set the global flags.\n"));
5983 else
5984 Log(("Failed to set the global flags \"%s\".\n", szFlags));
5985 }
5986 return rc;
5987}
5988#endif /* VBOX_WITH_GUEST_PROPS */
5989
5990/**
5991 * Set up the Guest Property service, populate it with properties read from
5992 * the machine XML and set a couple of initial properties.
5993 */
5994/* static */ int Console::i_configGuestProperties(void *pvConsole, PUVM pUVM)
5995{
5996#ifdef VBOX_WITH_GUEST_PROPS
5997 AssertReturn(pvConsole, VERR_INVALID_POINTER);
5998 ComObjPtr<Console> pConsole = static_cast<Console *>(pvConsole);
5999 AssertReturn(pConsole->m_pVMMDev, VERR_INVALID_POINTER);
6000
6001 /* Load the service */
6002 int rc = pConsole->m_pVMMDev->hgcmLoadService("VBoxGuestPropSvc", "VBoxGuestPropSvc");
6003
6004 if (RT_FAILURE(rc))
6005 {
6006 LogRel(("VBoxGuestPropSvc is not available. rc = %Rrc\n", rc));
6007 /* That is not a fatal failure. */
6008 rc = VINF_SUCCESS;
6009 }
6010 else
6011 {
6012 /*
6013 * Initialize built-in properties that can be changed and saved.
6014 *
6015 * These are typically transient properties that the guest cannot
6016 * change.
6017 */
6018
6019 {
6020 VBOXHGCMSVCPARM Params[2];
6021 int rc2 = pConsole->m_pVMMDev->hgcmHostCall("VBoxGuestPropSvc", guestProp::GET_DBGF_INFO_FN, 2, &Params[0]);
6022 if (RT_SUCCESS(rc2))
6023 {
6024 PFNDBGFHANDLEREXT pfnHandler = (PFNDBGFHANDLEREXT)(uintptr_t)Params[0].u.pointer.addr;
6025 void *pService = (void*)Params[1].u.pointer.addr;
6026 DBGFR3InfoRegisterExternal(pUVM, "guestprops", "Display the guest properties", pfnHandler, pService);
6027 }
6028 }
6029
6030 /* Sysprep execution by VBoxService. */
6031 configSetProperty(pConsole->m_pVMMDev,
6032 "/VirtualBox/HostGuest/SysprepExec", "",
6033 "TRANSIENT, RDONLYGUEST");
6034 configSetProperty(pConsole->m_pVMMDev,
6035 "/VirtualBox/HostGuest/SysprepArgs", "",
6036 "TRANSIENT, RDONLYGUEST");
6037
6038 /*
6039 * Pull over the properties from the server.
6040 */
6041 SafeArray<BSTR> namesOut;
6042 SafeArray<BSTR> valuesOut;
6043 SafeArray<LONG64> timestampsOut;
6044 SafeArray<BSTR> flagsOut;
6045 HRESULT hrc;
6046 hrc = pConsole->mControl->PullGuestProperties(ComSafeArrayAsOutParam(namesOut),
6047 ComSafeArrayAsOutParam(valuesOut),
6048 ComSafeArrayAsOutParam(timestampsOut),
6049 ComSafeArrayAsOutParam(flagsOut));
6050 AssertLogRelMsgReturn(SUCCEEDED(hrc), ("hrc=%Rhrc\n", hrc), VERR_MAIN_CONFIG_CONSTRUCTOR_COM_ERROR);
6051 size_t cProps = namesOut.size();
6052 size_t cAlloc = cProps + 1;
6053 if ( valuesOut.size() != cProps
6054 || timestampsOut.size() != cProps
6055 || flagsOut.size() != cProps
6056 )
6057 AssertFailedReturn(VERR_INVALID_PARAMETER);
6058
6059 char **papszNames, **papszValues, **papszFlags;
6060 char szEmpty[] = "";
6061 LONG64 *pai64Timestamps;
6062 papszNames = (char **)RTMemTmpAllocZ(sizeof(void *) * cAlloc);
6063 papszValues = (char **)RTMemTmpAllocZ(sizeof(void *) * cAlloc);
6064 pai64Timestamps = (LONG64 *)RTMemTmpAllocZ(sizeof(LONG64) * cAlloc);
6065 papszFlags = (char **)RTMemTmpAllocZ(sizeof(void *) * cAlloc);
6066 if (papszNames && papszValues && pai64Timestamps && papszFlags)
6067 {
6068 for (unsigned i = 0; RT_SUCCESS(rc) && i < cProps; ++i)
6069 {
6070 AssertPtrBreakStmt(namesOut[i], rc = VERR_INVALID_PARAMETER);
6071 rc = RTUtf16ToUtf8(namesOut[i], &papszNames[i]);
6072 if (RT_FAILURE(rc))
6073 break;
6074 if (valuesOut[i])
6075 rc = RTUtf16ToUtf8(valuesOut[i], &papszValues[i]);
6076 else
6077 papszValues[i] = szEmpty;
6078 if (RT_FAILURE(rc))
6079 break;
6080 pai64Timestamps[i] = timestampsOut[i];
6081 if (flagsOut[i])
6082 rc = RTUtf16ToUtf8(flagsOut[i], &papszFlags[i]);
6083 else
6084 papszFlags[i] = szEmpty;
6085 }
6086 if (RT_SUCCESS(rc))
6087 configSetProperties(pConsole->m_pVMMDev,
6088 (void *)papszNames,
6089 (void *)papszValues,
6090 (void *)pai64Timestamps,
6091 (void *)papszFlags);
6092 for (unsigned i = 0; i < cProps; ++i)
6093 {
6094 RTStrFree(papszNames[i]);
6095 if (valuesOut[i])
6096 RTStrFree(papszValues[i]);
6097 if (flagsOut[i])
6098 RTStrFree(papszFlags[i]);
6099 }
6100 }
6101 else
6102 rc = VERR_NO_MEMORY;
6103 RTMemTmpFree(papszNames);
6104 RTMemTmpFree(papszValues);
6105 RTMemTmpFree(pai64Timestamps);
6106 RTMemTmpFree(papszFlags);
6107 AssertRCReturn(rc, rc);
6108
6109 /*
6110 * These properties have to be set before pulling over the properties
6111 * from the machine XML, to ensure that properties saved in the XML
6112 * will override them.
6113 */
6114 /* Set the raw VBox version string as a guest property. Used for host/guest
6115 * version comparison. */
6116 configSetProperty(pConsole->m_pVMMDev, "/VirtualBox/HostInfo/VBoxVer",
6117 VBOX_VERSION_STRING_RAW, "TRANSIENT, RDONLYGUEST");
6118 /* Set the full VBox version string as a guest property. Can contain vendor-specific
6119 * information/branding and/or pre-release tags. */
6120 configSetProperty(pConsole->m_pVMMDev, "/VirtualBox/HostInfo/VBoxVerExt",
6121 VBOX_VERSION_STRING, "TRANSIENT, RDONLYGUEST");
6122 /* Set the VBox SVN revision as a guest property */
6123 configSetProperty(pConsole->m_pVMMDev, "/VirtualBox/HostInfo/VBoxRev",
6124 RTBldCfgRevisionStr(), "TRANSIENT, RDONLYGUEST");
6125
6126 /*
6127 * Register the host notification callback
6128 */
6129 HGCMSVCEXTHANDLE hDummy;
6130 HGCMHostRegisterServiceExtension(&hDummy, "VBoxGuestPropSvc",
6131 Console::i_doGuestPropNotification,
6132 pvConsole);
6133
6134#ifdef VBOX_WITH_GUEST_PROPS_RDONLY_GUEST
6135 rc = configSetGlobalPropertyFlags(pConsole->m_pVMMDev,
6136 guestProp::RDONLYGUEST);
6137 AssertRCReturn(rc, rc);
6138#endif
6139
6140 Log(("Set VBoxGuestPropSvc property store\n"));
6141 }
6142 return VINF_SUCCESS;
6143#else /* !VBOX_WITH_GUEST_PROPS */
6144 return VERR_NOT_SUPPORTED;
6145#endif /* !VBOX_WITH_GUEST_PROPS */
6146}
6147
6148/**
6149 * Set up the Guest Control service.
6150 */
6151/* static */ int Console::i_configGuestControl(void *pvConsole)
6152{
6153#ifdef VBOX_WITH_GUEST_CONTROL
6154 AssertReturn(pvConsole, VERR_INVALID_POINTER);
6155 ComObjPtr<Console> pConsole = static_cast<Console *>(pvConsole);
6156
6157 /* Load the service */
6158 int rc = pConsole->m_pVMMDev->hgcmLoadService("VBoxGuestControlSvc", "VBoxGuestControlSvc");
6159
6160 if (RT_FAILURE(rc))
6161 {
6162 LogRel(("VBoxGuestControlSvc is not available. rc = %Rrc\n", rc));
6163 /* That is not a fatal failure. */
6164 rc = VINF_SUCCESS;
6165 }
6166 else
6167 {
6168 HGCMSVCEXTHANDLE hDummy;
6169 rc = HGCMHostRegisterServiceExtension(&hDummy, "VBoxGuestControlSvc",
6170 &Guest::i_notifyCtrlDispatcher,
6171 pConsole->i_getGuest());
6172 if (RT_FAILURE(rc))
6173 Log(("Cannot register VBoxGuestControlSvc extension!\n"));
6174 else
6175 LogRel(("Guest Control service loaded\n"));
6176 }
6177
6178 return rc;
6179#else /* !VBOX_WITH_GUEST_CONTROL */
6180 return VERR_NOT_SUPPORTED;
6181#endif /* !VBOX_WITH_GUEST_CONTROL */
6182}
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