VirtualBox

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

Last change on this file since 43131 was 43131, checked in by vboxsync, 12 years ago

EFI: permanent NVRAM storage.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 202.2 KB
Line 
1/* $Id: ConsoleImpl2.cpp 43131 2012-08-31 11:34:49Z 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-2012 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* Header Files *
25*******************************************************************************/
26// for some reason Windows burns in sdk\...\winsock.h if this isn't included first
27#include "VBox/com/ptr.h"
28
29#include "ConsoleImpl.h"
30#include "DisplayImpl.h"
31#ifdef VBOX_WITH_GUEST_CONTROL
32# include "GuestImpl.h"
33#endif
34#ifdef VBOX_WITH_DRAG_AND_DROP
35# include "GuestDnDImpl.h"
36#endif
37#include "VMMDev.h"
38#include "Global.h"
39#ifdef VBOX_WITH_PCI_PASSTHROUGH
40# include "PCIRawDevImpl.h"
41#endif
42
43// generated header
44#include "SchemaDefs.h"
45
46#include "AutoCaller.h"
47#include "Logging.h"
48
49#include <iprt/buildconfig.h>
50#include <iprt/ctype.h>
51#include <iprt/dir.h>
52#include <iprt/file.h>
53#include <iprt/param.h>
54#include <iprt/path.h>
55#include <iprt/string.h>
56#include <iprt/system.h>
57#include <iprt/cpp/exception.h>
58#if 0 /* enable to play with lots of memory. */
59# include <iprt/env.h>
60#endif
61#include <iprt/stream.h>
62
63#include <VBox/vmm/vmapi.h>
64#include <VBox/err.h>
65#include <VBox/param.h>
66#include <VBox/vmm/pdmapi.h> /* For PDMR3DriverAttach/PDMR3DriverDetach */
67#include <VBox/version.h>
68#include <VBox/HostServices/VBoxClipboardSvc.h>
69#ifdef VBOX_WITH_CROGL
70# include <VBox/HostServices/VBoxCrOpenGLSvc.h>
71#include <VBox/VBoxOGLTest.h>
72#endif
73#ifdef VBOX_WITH_GUEST_PROPS
74# include <VBox/HostServices/GuestPropertySvc.h>
75# include <VBox/com/defs.h>
76# include <VBox/com/array.h>
77# include "HGCM.h" /** @todo it should be possible to register a service
78 * extension using a VMMDev callback. */
79# include <vector>
80#endif /* VBOX_WITH_GUEST_PROPS */
81#include <VBox/intnet.h>
82
83#include <VBox/com/com.h>
84#include <VBox/com/string.h>
85#include <VBox/com/array.h>
86
87#ifdef VBOX_WITH_NETFLT
88# if defined(RT_OS_SOLARIS)
89# include <zone.h>
90# elif defined(RT_OS_LINUX)
91# include <unistd.h>
92# include <sys/ioctl.h>
93# include <sys/socket.h>
94# include <linux/types.h>
95# include <linux/if.h>
96# include <linux/wireless.h>
97# elif defined(RT_OS_FREEBSD)
98# include <unistd.h>
99# include <sys/types.h>
100# include <sys/ioctl.h>
101# include <sys/socket.h>
102# include <net/if.h>
103# include <net80211/ieee80211_ioctl.h>
104# endif
105# if defined(RT_OS_WINDOWS)
106# include <VBox/VBoxNetCfg-win.h>
107# include <Ntddndis.h>
108# include <devguid.h>
109# else
110# include <HostNetworkInterfaceImpl.h>
111# include <netif.h>
112# include <stdlib.h>
113# endif
114#endif /* VBOX_WITH_NETFLT */
115
116#include "DHCPServerRunner.h"
117#include "BusAssignmentManager.h"
118#ifdef VBOX_WITH_EXTPACK
119# include "ExtPackManagerImpl.h"
120#endif
121
122#if defined(RT_OS_DARWIN)
123
124# include "IOKit/IOKitLib.h"
125
126static int DarwinSmcKey(char *pabKey, uint32_t cbKey)
127{
128 /*
129 * Method as described in Amit Singh's article:
130 * http://osxbook.com/book/bonus/chapter7/tpmdrmmyth/
131 */
132 typedef struct
133 {
134 uint32_t key;
135 uint8_t pad0[22];
136 uint32_t datasize;
137 uint8_t pad1[10];
138 uint8_t cmd;
139 uint32_t pad2;
140 uint8_t data[32];
141 } AppleSMCBuffer;
142
143 AssertReturn(cbKey >= 65, VERR_INTERNAL_ERROR);
144
145 io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault,
146 IOServiceMatching("AppleSMC"));
147 if (!service)
148 return VERR_NOT_FOUND;
149
150 io_connect_t port = (io_connect_t)0;
151 kern_return_t kr = IOServiceOpen(service, mach_task_self(), 0, &port);
152 IOObjectRelease(service);
153
154 if (kr != kIOReturnSuccess)
155 return RTErrConvertFromDarwin(kr);
156
157 AppleSMCBuffer inputStruct = { 0, {0}, 32, {0}, 5, };
158 AppleSMCBuffer outputStruct;
159 size_t cbOutputStruct = sizeof(outputStruct);
160
161 for (int i = 0; i < 2; i++)
162 {
163 inputStruct.key = (uint32_t)((i == 0) ? 'OSK0' : 'OSK1');
164 kr = IOConnectCallStructMethod((mach_port_t)port,
165 (uint32_t)2,
166 (const void *)&inputStruct,
167 sizeof(inputStruct),
168 (void *)&outputStruct,
169 &cbOutputStruct);
170 if (kr != kIOReturnSuccess)
171 {
172 IOServiceClose(port);
173 return RTErrConvertFromDarwin(kr);
174 }
175
176 for (int j = 0; j < 32; j++)
177 pabKey[j + i*32] = outputStruct.data[j];
178 }
179
180 IOServiceClose(port);
181
182 pabKey[64] = 0;
183
184 return VINF_SUCCESS;
185}
186
187#endif /* RT_OS_DARWIN */
188
189/* Darwin compile kludge */
190#undef PVM
191
192/* Comment out the following line to remove VMWare compatibility hack. */
193#define VMWARE_NET_IN_SLOT_11
194
195/**
196 * Translate IDE StorageControllerType_T to string representation.
197 */
198const char* controllerString(StorageControllerType_T enmType)
199{
200 switch (enmType)
201 {
202 case StorageControllerType_PIIX3:
203 return "PIIX3";
204 case StorageControllerType_PIIX4:
205 return "PIIX4";
206 case StorageControllerType_ICH6:
207 return "ICH6";
208 default:
209 return "Unknown";
210 }
211}
212
213/**
214 * Simple class for storing network boot information.
215 */
216struct BootNic
217{
218 ULONG mInstance;
219 PCIBusAddress mPCIAddress;
220
221 ULONG mBootPrio;
222 bool operator < (const BootNic &rhs) const
223 {
224 ULONG lval = mBootPrio - 1; /* 0 will wrap around and get the lowest priority. */
225 ULONG rval = rhs.mBootPrio - 1;
226 return lval < rval; /* Zero compares as highest number (lowest prio). */
227 }
228};
229
230static int findEfiRom(IVirtualBox* vbox, FirmwareType_T aFirmwareType, Utf8Str *pEfiRomFile)
231{
232 Bstr aFilePath, empty;
233 BOOL fPresent = FALSE;
234 HRESULT hrc = vbox->CheckFirmwarePresent(aFirmwareType, empty.raw(),
235 empty.asOutParam(), aFilePath.asOutParam(), &fPresent);
236 AssertComRCReturn(hrc, Global::vboxStatusCodeFromCOM(hrc));
237
238 if (!fPresent)
239 {
240 LogRel(("Failed to find an EFI ROM file.\n"));
241 return VERR_FILE_NOT_FOUND;
242 }
243
244 *pEfiRomFile = Utf8Str(aFilePath);
245
246 return VINF_SUCCESS;
247}
248
249static int getSmcDeviceKey(IMachine *pMachine, BSTR *aKey, bool *pfGetKeyFromRealSMC)
250{
251 *pfGetKeyFromRealSMC = false;
252
253 /*
254 * The extra data takes precedence (if non-zero).
255 */
256 HRESULT hrc = pMachine->GetExtraData(Bstr("VBoxInternal2/SmcDeviceKey").raw(),
257 aKey);
258 if (FAILED(hrc))
259 return Global::vboxStatusCodeFromCOM(hrc);
260 if ( SUCCEEDED(hrc)
261 && *aKey
262 && **aKey)
263 return VINF_SUCCESS;
264
265#ifdef RT_OS_DARWIN
266 /*
267 * Query it here and now.
268 */
269 char abKeyBuf[65];
270 int rc = DarwinSmcKey(abKeyBuf, sizeof(abKeyBuf));
271 if (SUCCEEDED(rc))
272 {
273 Bstr(abKeyBuf).detachTo(aKey);
274 return rc;
275 }
276 LogRel(("Warning: DarwinSmcKey failed with rc=%Rrc!\n", rc));
277
278#else
279 /*
280 * Is it apple hardware in bootcamp?
281 */
282 /** @todo implement + test RTSYSDMISTR_MANUFACTURER on all hosts.
283 * Currently falling back on the product name. */
284 char szManufacturer[256];
285 szManufacturer[0] = '\0';
286 RTSystemQueryDmiString(RTSYSDMISTR_MANUFACTURER, szManufacturer, sizeof(szManufacturer));
287 if (szManufacturer[0] != '\0')
288 {
289 if ( !strcmp(szManufacturer, "Apple Computer, Inc.")
290 || !strcmp(szManufacturer, "Apple Inc.")
291 )
292 *pfGetKeyFromRealSMC = true;
293 }
294 else
295 {
296 char szProdName[256];
297 szProdName[0] = '\0';
298 RTSystemQueryDmiString(RTSYSDMISTR_PRODUCT_NAME, szProdName, sizeof(szProdName));
299 if ( ( !strncmp(szProdName, "Mac", 3)
300 || !strncmp(szProdName, "iMac", 4)
301 || !strncmp(szProdName, "iMac", 4)
302 || !strncmp(szProdName, "Xserve", 6)
303 )
304 && !strchr(szProdName, ' ') /* no spaces */
305 && RT_C_IS_DIGIT(szProdName[strlen(szProdName) - 1]) /* version number */
306 )
307 *pfGetKeyFromRealSMC = true;
308 }
309
310 int rc = VINF_SUCCESS;
311#endif
312
313 return rc;
314}
315
316
317/*
318 * VC++ 8 / amd64 has some serious trouble with the next functions.
319 * As a temporary measure, we'll drop global optimizations.
320 */
321#if defined(_MSC_VER) && defined(RT_ARCH_AMD64)
322# pragma optimize("g", off)
323#endif
324
325static const char *const g_apszIDEDrives[4] =
326 { "PrimaryMaster", "PrimarySlave", "SecondaryMaster", "SecondarySlave" };
327
328class ConfigError : public RTCError
329{
330public:
331
332 ConfigError(const char *pcszFunction,
333 int vrc,
334 const char *pcszName)
335 : RTCError(Utf8StrFmt("%s failed: rc=%Rrc, pcszName=%s", pcszFunction, vrc, pcszName)),
336 m_vrc(vrc)
337 {
338 AssertMsgFailed(("%s\n", what())); // in strict mode, hit a breakpoint here
339 }
340
341 int m_vrc;
342};
343
344
345/**
346 * Helper that calls CFGMR3InsertString and throws an RTCError if that
347 * fails (C-string variant).
348 * @param pParent See CFGMR3InsertStringN.
349 * @param pcszNodeName See CFGMR3InsertStringN.
350 * @param pcszValue The string value.
351 */
352static void InsertConfigString(PCFGMNODE pNode,
353 const char *pcszName,
354 const char *pcszValue)
355{
356 int vrc = CFGMR3InsertString(pNode,
357 pcszName,
358 pcszValue);
359 if (RT_FAILURE(vrc))
360 throw ConfigError("CFGMR3InsertString", vrc, pcszName);
361}
362
363/**
364 * Helper that calls CFGMR3InsertString and throws an RTCError if that
365 * fails (Utf8Str variant).
366 * @param pParent See CFGMR3InsertStringN.
367 * @param pcszNodeName See CFGMR3InsertStringN.
368 * @param rStrValue The string value.
369 */
370static void InsertConfigString(PCFGMNODE pNode,
371 const char *pcszName,
372 const Utf8Str &rStrValue)
373{
374 int vrc = CFGMR3InsertStringN(pNode,
375 pcszName,
376 rStrValue.c_str(),
377 rStrValue.length());
378 if (RT_FAILURE(vrc))
379 throw ConfigError("CFGMR3InsertStringLengthKnown", vrc, pcszName);
380}
381
382/**
383 * Helper that calls CFGMR3InsertString and throws an RTCError if that
384 * fails (Bstr variant).
385 *
386 * @param pParent See CFGMR3InsertStringN.
387 * @param pcszNodeName See CFGMR3InsertStringN.
388 * @param rBstrValue The string value.
389 */
390static void InsertConfigString(PCFGMNODE pNode,
391 const char *pcszName,
392 const Bstr &rBstrValue)
393{
394 InsertConfigString(pNode, pcszName, Utf8Str(rBstrValue));
395}
396
397/**
398 * Helper that calls CFGMR3InsertBytes and throws an RTCError if that fails.
399 *
400 * @param pNode See CFGMR3InsertBytes.
401 * @param pcszName See CFGMR3InsertBytes.
402 * @param pvBytes See CFGMR3InsertBytes.
403 * @param cbBytes See CFGMR3InsertBytes.
404 */
405static void InsertConfigBytes(PCFGMNODE pNode,
406 const char *pcszName,
407 const void *pvBytes,
408 size_t cbBytes)
409{
410 int vrc = CFGMR3InsertBytes(pNode,
411 pcszName,
412 pvBytes,
413 cbBytes);
414 if (RT_FAILURE(vrc))
415 throw ConfigError("CFGMR3InsertBytes", vrc, pcszName);
416}
417
418/**
419 * Helper that calls CFGMR3InsertInteger and throws an RTCError if that
420 * fails.
421 *
422 * @param pNode See CFGMR3InsertInteger.
423 * @param pcszName See CFGMR3InsertInteger.
424 * @param u64Integer See CFGMR3InsertInteger.
425 */
426static void InsertConfigInteger(PCFGMNODE pNode,
427 const char *pcszName,
428 uint64_t u64Integer)
429{
430 int vrc = CFGMR3InsertInteger(pNode,
431 pcszName,
432 u64Integer);
433 if (RT_FAILURE(vrc))
434 throw ConfigError("CFGMR3InsertInteger", vrc, pcszName);
435}
436
437/**
438 * Helper that calls CFGMR3InsertNode and throws an RTCError if that fails.
439 *
440 * @param pNode See CFGMR3InsertNode.
441 * @param pcszName See CFGMR3InsertNode.
442 * @param ppChild See CFGMR3InsertNode.
443 */
444static void InsertConfigNode(PCFGMNODE pNode,
445 const char *pcszName,
446 PCFGMNODE *ppChild)
447{
448 int vrc = CFGMR3InsertNode(pNode, pcszName, ppChild);
449 if (RT_FAILURE(vrc))
450 throw ConfigError("CFGMR3InsertNode", vrc, pcszName);
451}
452
453/**
454 * Helper that calls CFGMR3RemoveValue and throws an RTCError if that fails.
455 *
456 * @param pNode See CFGMR3RemoveValue.
457 * @param pcszName See CFGMR3RemoveValue.
458 */
459static void RemoveConfigValue(PCFGMNODE pNode,
460 const char *pcszName)
461{
462 int vrc = CFGMR3RemoveValue(pNode, pcszName);
463 if (RT_FAILURE(vrc))
464 throw ConfigError("CFGMR3RemoveValue", vrc, pcszName);
465}
466
467#ifdef VBOX_WITH_PCI_PASSTHROUGH
468HRESULT Console::attachRawPCIDevices(PVM pVM,
469 BusAssignmentManager *BusMgr,
470 PCFGMNODE pDevices)
471{
472 HRESULT hrc = S_OK;
473 PCFGMNODE pInst, pCfg, pLunL0, pLunL1;
474
475 SafeIfaceArray<IPCIDeviceAttachment> assignments;
476 ComPtr<IMachine> aMachine = machine();
477
478 hrc = aMachine->COMGETTER(PCIDeviceAssignments)(ComSafeArrayAsOutParam(assignments));
479 if ( hrc != S_OK
480 || assignments.size() < 1)
481 return hrc;
482
483 /*
484 * PCI passthrough is only available if the proper ExtPack is installed.
485 *
486 * Note. Configuring PCI passthrough here and providing messages about
487 * the missing extpack isn't exactly clean, but it is a necessary evil
488 * to patch over legacy compatability issues introduced by the new
489 * distribution model.
490 */
491# ifdef VBOX_WITH_EXTPACK
492 static const char *s_pszPCIRawExtPackName = "Oracle VM VirtualBox Extension Pack";
493 if (!mptrExtPackManager->isExtPackUsable(s_pszPCIRawExtPackName))
494 {
495 /* Always fatal! */
496 return VMSetError(pVM, VERR_NOT_FOUND, RT_SRC_POS,
497 N_("Implementation of the PCI passthrough framework not found!\n"
498 "The VM cannot be started. To fix this problem, either "
499 "install the '%s' or disable PCI passthrough via VBoxManage"),
500 s_pszPCIRawExtPackName);
501 }
502# endif
503
504 PCFGMNODE pBridges = CFGMR3GetChild(pDevices, "ich9pcibridge");
505 Assert(pBridges);
506
507 /* Find required bridges, and add missing ones */
508 for (size_t iDev = 0; iDev < assignments.size(); iDev++)
509 {
510 ComPtr<IPCIDeviceAttachment> assignment = assignments[iDev];
511 LONG guest = 0;
512 PCIBusAddress GuestPCIAddress;
513
514 assignment->COMGETTER(GuestAddress)(&guest);
515 GuestPCIAddress.fromLong(guest);
516 Assert(GuestPCIAddress.valid());
517
518 if (GuestPCIAddress.miBus > 0)
519 {
520 int iBridgesMissed = 0;
521 int iBase = GuestPCIAddress.miBus - 1;
522
523 while (!BusMgr->hasPCIDevice("ich9pcibridge", iBase) && iBase > 0)
524 {
525 iBridgesMissed++; iBase--;
526 }
527 iBase++;
528
529 for (int iBridge = 0; iBridge < iBridgesMissed; iBridge++)
530 {
531 InsertConfigNode(pBridges, Utf8StrFmt("%d", iBase + iBridge).c_str(), &pInst);
532 InsertConfigInteger(pInst, "Trusted", 1);
533 hrc = BusMgr->assignPCIDevice("ich9pcibridge", pInst);
534 }
535 }
536 }
537
538 /* Now actually add devices */
539 PCFGMNODE pPCIDevs = NULL;
540
541 if (assignments.size() > 0)
542 {
543 InsertConfigNode(pDevices, "pciraw", &pPCIDevs);
544
545 PCFGMNODE pRoot = CFGMR3GetParent(pDevices); Assert(pRoot);
546
547 /* Tell PGM to tell GPCIRaw about guest mappings. */
548 CFGMR3InsertNode(pRoot, "PGM", NULL);
549 InsertConfigInteger(CFGMR3GetChild(pRoot, "PGM"), "PciPassThrough", 1);
550
551 /*
552 * Currently, using IOMMU needed for PCI passthrough
553 * requires RAM preallocation.
554 */
555 /** @todo: check if we can lift this requirement */
556 CFGMR3RemoveValue(pRoot, "RamPreAlloc");
557 InsertConfigInteger(pRoot, "RamPreAlloc", 1);
558 }
559
560 for (size_t iDev = 0; iDev < assignments.size(); iDev++)
561 {
562 PCIBusAddress HostPCIAddress, GuestPCIAddress;
563 ComPtr<IPCIDeviceAttachment> assignment = assignments[iDev];
564 LONG host, guest;
565 Bstr aDevName;
566
567 assignment->COMGETTER(HostAddress)(&host);
568 assignment->COMGETTER(GuestAddress)(&guest);
569 assignment->COMGETTER(Name)(aDevName.asOutParam());
570
571 InsertConfigNode(pPCIDevs, Utf8StrFmt("%d", iDev).c_str(), &pInst);
572 InsertConfigInteger(pInst, "Trusted", 1);
573
574 HostPCIAddress.fromLong(host);
575 Assert(HostPCIAddress.valid());
576 InsertConfigNode(pInst, "Config", &pCfg);
577 InsertConfigString(pCfg, "DeviceName", aDevName);
578
579 InsertConfigInteger(pCfg, "DetachHostDriver", 1);
580 InsertConfigInteger(pCfg, "HostPCIBusNo", HostPCIAddress.miBus);
581 InsertConfigInteger(pCfg, "HostPCIDeviceNo", HostPCIAddress.miDevice);
582 InsertConfigInteger(pCfg, "HostPCIFunctionNo", HostPCIAddress.miFn);
583
584 GuestPCIAddress.fromLong(guest);
585 Assert(GuestPCIAddress.valid());
586 hrc = BusMgr->assignHostPCIDevice("pciraw", pInst, HostPCIAddress, GuestPCIAddress, true);
587 if (hrc != S_OK)
588 return hrc;
589
590 InsertConfigInteger(pCfg, "GuestPCIBusNo", GuestPCIAddress.miBus);
591 InsertConfigInteger(pCfg, "GuestPCIDeviceNo", GuestPCIAddress.miDevice);
592 InsertConfigInteger(pCfg, "GuestPCIFunctionNo", GuestPCIAddress.miFn);
593
594 /* the driver */
595 InsertConfigNode(pInst, "LUN#0", &pLunL0);
596 InsertConfigString(pLunL0, "Driver", "pciraw");
597 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
598
599 /* the Main driver */
600 InsertConfigString(pLunL1, "Driver", "MainPciRaw");
601 InsertConfigNode(pLunL1, "Config", &pCfg);
602 PCIRawDev* pMainDev = new PCIRawDev(this);
603 InsertConfigInteger(pCfg, "Object", (uintptr_t)pMainDev);
604 }
605
606 return hrc;
607}
608#endif
609
610
611void Console::attachStatusDriver(PCFGMNODE pCtlInst, PPDMLED *papLeds,
612 uint64_t uFirst, uint64_t uLast,
613 Console::MediumAttachmentMap *pmapMediumAttachments,
614 const char *pcszDevice, unsigned uInstance)
615{
616 PCFGMNODE pLunL0, pCfg;
617 InsertConfigNode(pCtlInst, "LUN#999", &pLunL0);
618 InsertConfigString(pLunL0, "Driver", "MainStatus");
619 InsertConfigNode(pLunL0, "Config", &pCfg);
620 InsertConfigInteger(pCfg, "papLeds", (uintptr_t)papLeds);
621 if (pmapMediumAttachments)
622 {
623 InsertConfigInteger(pCfg, "pmapMediumAttachments", (uintptr_t)pmapMediumAttachments);
624 InsertConfigInteger(pCfg, "pConsole", (uintptr_t)this);
625 AssertPtr(pcszDevice);
626 Utf8Str deviceInstance = Utf8StrFmt("%s/%u", pcszDevice, uInstance);
627 InsertConfigString(pCfg, "DeviceInstance", deviceInstance.c_str());
628 }
629 InsertConfigInteger(pCfg, "First", uFirst);
630 InsertConfigInteger(pCfg, "Last", uLast);
631}
632
633
634/**
635 * Construct the VM configuration tree (CFGM).
636 *
637 * This is a callback for VMR3Create() call. It is called from CFGMR3Init()
638 * in the emulation thread (EMT). Any per thread COM/XPCOM initialization
639 * is done here.
640 *
641 * @param pVM VM handle.
642 * @param pvConsole Pointer to the VMPowerUpTask object.
643 * @return VBox status code.
644 *
645 * @note Locks the Console object for writing.
646 */
647DECLCALLBACK(int) Console::configConstructor(PVM pVM, void *pvConsole)
648{
649 LogFlowFuncEnter();
650
651 AssertReturn(pvConsole, VERR_GENERAL_FAILURE);
652 ComObjPtr<Console> pConsole = static_cast<Console *>(pvConsole);
653
654 AutoCaller autoCaller(pConsole);
655 AssertComRCReturn(autoCaller.rc(), VERR_ACCESS_DENIED);
656
657 /* lock the console because we widely use internal fields and methods */
658 AutoWriteLock alock(pConsole COMMA_LOCKVAL_SRC_POS);
659
660 /*
661 * Set the VM handle and do the rest of the job in an worker method so we
662 * can easily reset the VM handle on failure.
663 */
664 PUVM pUVM = pConsole->mpUVM = VMR3GetUVM(pVM);
665 VMR3RetainUVM(pUVM);
666 int vrc;
667 try
668 {
669 vrc = pConsole->configConstructorInner(pVM, &alock);
670 }
671 catch (...)
672 {
673 vrc = VERR_UNEXPECTED_EXCEPTION;
674 }
675 if (RT_FAILURE(vrc))
676 {
677 pConsole->mpUVM = NULL;
678 VMR3ReleaseUVM(pUVM);
679 }
680
681 return vrc;
682}
683
684
685/**
686 * Worker for configConstructor.
687 *
688 * @return VBox status code.
689 * @param pVM The VM handle.
690 * @param pAlock The automatic lock instance. This is for when we have
691 * to leave it in order to avoid deadlocks (ext packs and
692 * more).
693 */
694int Console::configConstructorInner(PVM pVM, AutoWriteLock *pAlock)
695{
696 VMMDev *pVMMDev = m_pVMMDev;
697 Assert(pVMMDev);
698
699 ComPtr<IMachine> pMachine = machine();
700
701 int rc;
702 HRESULT hrc;
703 Bstr bstr;
704
705#define H() AssertMsgReturn(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), VERR_GENERAL_FAILURE)
706
707 /*
708 * Get necessary objects and frequently used parameters.
709 */
710 ComPtr<IVirtualBox> virtualBox;
711 hrc = pMachine->COMGETTER(Parent)(virtualBox.asOutParam()); H();
712
713 ComPtr<IHost> host;
714 hrc = virtualBox->COMGETTER(Host)(host.asOutParam()); H();
715
716 ComPtr<ISystemProperties> systemProperties;
717 hrc = virtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam()); H();
718
719 ComPtr<IBIOSSettings> biosSettings;
720 hrc = pMachine->COMGETTER(BIOSSettings)(biosSettings.asOutParam()); H();
721
722 hrc = pMachine->COMGETTER(HardwareUUID)(bstr.asOutParam()); H();
723 RTUUID HardwareUuid;
724 rc = RTUuidFromUtf16(&HardwareUuid, bstr.raw());
725 AssertRCReturn(rc, rc);
726
727 ULONG cRamMBs;
728 hrc = pMachine->COMGETTER(MemorySize)(&cRamMBs); H();
729#if 0 /* enable to play with lots of memory. */
730 if (RTEnvExist("VBOX_RAM_SIZE"))
731 cRamMBs = RTStrToUInt64(RTEnvGet("VBOX_RAM_SIZE"));
732#endif
733 uint64_t const cbRam = cRamMBs * (uint64_t)_1M;
734 uint32_t cbRamHole = MM_RAM_HOLE_SIZE_DEFAULT;
735 uint64_t uMcfgBase = 0;
736 uint32_t cbMcfgLength = 0;
737
738 ChipsetType_T chipsetType;
739 hrc = pMachine->COMGETTER(ChipsetType)(&chipsetType); H();
740 if (chipsetType == ChipsetType_ICH9)
741 {
742 /* We'd better have 0x10000000 region, to cover 256 buses
743 but this put too much load on hypervisor heap */
744 cbMcfgLength = 0x4000000; //0x10000000;
745 cbRamHole += cbMcfgLength;
746 uMcfgBase = _4G - cbRamHole;
747 }
748
749 BusAssignmentManager* BusMgr = mBusMgr = BusAssignmentManager::createInstance(chipsetType);
750
751 ULONG cCpus = 1;
752 hrc = pMachine->COMGETTER(CPUCount)(&cCpus); H();
753
754 ULONG ulCpuExecutionCap = 100;
755 hrc = pMachine->COMGETTER(CPUExecutionCap)(&ulCpuExecutionCap); H();
756
757 Bstr osTypeId;
758 hrc = pMachine->COMGETTER(OSTypeId)(osTypeId.asOutParam()); H();
759
760 BOOL fIOAPIC;
761 hrc = biosSettings->COMGETTER(IOAPICEnabled)(&fIOAPIC); H();
762
763 ComPtr<IGuestOSType> guestOSType;
764 hrc = virtualBox->GetGuestOSType(osTypeId.raw(), guestOSType.asOutParam()); H();
765
766 Bstr guestTypeFamilyId;
767 hrc = guestOSType->COMGETTER(FamilyId)(guestTypeFamilyId.asOutParam()); H();
768 BOOL fOsXGuest = guestTypeFamilyId == Bstr("MacOS");
769
770 ULONG maxNetworkAdapters;
771 hrc = systemProperties->GetMaxNetworkAdapters(chipsetType, &maxNetworkAdapters); H();
772 /*
773 * Get root node first.
774 * This is the only node in the tree.
775 */
776 PCFGMNODE pRoot = CFGMR3GetRoot(pVM);
777 Assert(pRoot);
778
779 // InsertConfigString throws
780 try
781 {
782
783 /*
784 * Set the root (and VMM) level values.
785 */
786 hrc = pMachine->COMGETTER(Name)(bstr.asOutParam()); H();
787 InsertConfigString(pRoot, "Name", bstr);
788 InsertConfigBytes(pRoot, "UUID", &HardwareUuid, sizeof(HardwareUuid));
789 InsertConfigInteger(pRoot, "RamSize", cbRam);
790 InsertConfigInteger(pRoot, "RamHoleSize", cbRamHole);
791 InsertConfigInteger(pRoot, "NumCPUs", cCpus);
792 InsertConfigInteger(pRoot, "CpuExecutionCap", ulCpuExecutionCap);
793 InsertConfigInteger(pRoot, "TimerMillies", 10);
794#ifdef VBOX_WITH_RAW_MODE
795 InsertConfigInteger(pRoot, "RawR3Enabled", 1); /* boolean */
796 InsertConfigInteger(pRoot, "RawR0Enabled", 1); /* boolean */
797 /** @todo Config: RawR0, PATMEnabled and CSAMEnabled needs attention later. */
798 InsertConfigInteger(pRoot, "PATMEnabled", 1); /* boolean */
799 InsertConfigInteger(pRoot, "CSAMEnabled", 1); /* boolean */
800#endif
801 /* Not necessary, but to make sure these two settings end up in the release log. */
802 BOOL fPageFusion = FALSE;
803 hrc = pMachine->COMGETTER(PageFusionEnabled)(&fPageFusion); H();
804 InsertConfigInteger(pRoot, "PageFusion", fPageFusion); /* boolean */
805 ULONG ulBalloonSize = 0;
806 hrc = pMachine->COMGETTER(MemoryBalloonSize)(&ulBalloonSize); H();
807 InsertConfigInteger(pRoot, "MemBalloonSize", ulBalloonSize);
808
809 /*
810 * CPUM values.
811 */
812 PCFGMNODE pCPUM;
813 InsertConfigNode(pRoot, "CPUM", &pCPUM);
814
815 /* cpuid leaf overrides. */
816 static uint32_t const s_auCpuIdRanges[] =
817 {
818 UINT32_C(0x00000000), UINT32_C(0x0000000a),
819 UINT32_C(0x80000000), UINT32_C(0x8000000a)
820 };
821 for (unsigned i = 0; i < RT_ELEMENTS(s_auCpuIdRanges); i += 2)
822 for (uint32_t uLeaf = s_auCpuIdRanges[i]; uLeaf < s_auCpuIdRanges[i + 1]; uLeaf++)
823 {
824 ULONG ulEax, ulEbx, ulEcx, ulEdx;
825 hrc = pMachine->GetCPUIDLeaf(uLeaf, &ulEax, &ulEbx, &ulEcx, &ulEdx);
826 if (SUCCEEDED(hrc))
827 {
828 PCFGMNODE pLeaf;
829 InsertConfigNode(pCPUM, Utf8StrFmt("HostCPUID/%RX32", uLeaf).c_str(), &pLeaf);
830
831 InsertConfigInteger(pLeaf, "eax", ulEax);
832 InsertConfigInteger(pLeaf, "ebx", ulEbx);
833 InsertConfigInteger(pLeaf, "ecx", ulEcx);
834 InsertConfigInteger(pLeaf, "edx", ulEdx);
835 }
836 else if (hrc != E_INVALIDARG) H();
837 }
838
839 /* We must limit CPUID count for Windows NT 4, as otherwise it stops
840 with error 0x3e (MULTIPROCESSOR_CONFIGURATION_NOT_SUPPORTED). */
841 if (osTypeId == "WindowsNT4")
842 {
843 LogRel(("Limiting CPUID leaf count for NT4 guests\n"));
844 InsertConfigInteger(pCPUM, "NT4LeafLimit", true);
845 }
846
847 /* Expose extended MWAIT features to Mac OS X guests. */
848 if (fOsXGuest)
849 {
850 LogRel(("Using MWAIT extensions\n"));
851 InsertConfigInteger(pCPUM, "MWaitExtensions", true);
852 }
853
854 /*
855 * Hardware virtualization extensions.
856 */
857 BOOL fHWVirtExEnabled;
858 BOOL fHwVirtExtForced = false;
859#ifdef VBOX_WITH_RAW_MODE
860 hrc = pMachine->GetHWVirtExProperty(HWVirtExPropertyType_Enabled, &fHWVirtExEnabled); H();
861 if (cCpus > 1) /** @todo SMP: This isn't nice, but things won't work on mac otherwise. */
862 fHWVirtExEnabled = TRUE;
863# ifdef RT_OS_DARWIN
864 fHwVirtExtForced = fHWVirtExEnabled;
865# else
866 /* - With more than 4GB PGM will use different RAMRANGE sizes for raw
867 mode and hv mode to optimize lookup times.
868 - With more than one virtual CPU, raw-mode isn't a fallback option. */
869 fHwVirtExtForced = fHWVirtExEnabled
870 && ( cbRam + cbRamHole > _4G
871 || cCpus > 1);
872# endif
873#else /* !VBOX_WITH_RAW_MODE */
874 fHWVirtExEnabled = fHwVirtExtForced = true;
875#endif /* !VBOX_WITH_RAW_MODE */
876 /* only honor the property value if there was no other reason to enable it */
877 if (!fHwVirtExtForced)
878 {
879 hrc = pMachine->GetHWVirtExProperty(HWVirtExPropertyType_Force, &fHwVirtExtForced); H();
880 }
881 InsertConfigInteger(pRoot, "HwVirtExtForced", fHwVirtExtForced);
882
883
884 /*
885 * MM values.
886 */
887 PCFGMNODE pMM;
888 InsertConfigNode(pRoot, "MM", &pMM);
889 InsertConfigInteger(pMM, "CanUseLargerHeap", chipsetType == ChipsetType_ICH9);
890
891 /*
892 * Hardware virtualization settings.
893 */
894 BOOL fIsGuest64Bit = false;
895 PCFGMNODE pHWVirtExt;
896 InsertConfigNode(pRoot, "HWVirtExt", &pHWVirtExt);
897 if (fHWVirtExEnabled)
898 {
899 InsertConfigInteger(pHWVirtExt, "Enabled", 1);
900
901 /* Indicate whether 64-bit guests are supported or not. */
902 /** @todo This is currently only forced off on 32-bit hosts only because it
903 * makes a lof of difference there (REM and Solaris performance).
904 */
905 BOOL fSupportsLongMode = false;
906 hrc = host->GetProcessorFeature(ProcessorFeature_LongMode,
907 &fSupportsLongMode); H();
908 hrc = guestOSType->COMGETTER(Is64Bit)(&fIsGuest64Bit); H();
909
910 if (fSupportsLongMode && fIsGuest64Bit)
911 {
912 InsertConfigInteger(pHWVirtExt, "64bitEnabled", 1);
913#if ARCH_BITS == 32 /* The recompiler must use VBoxREM64 (32-bit host only). */
914 PCFGMNODE pREM;
915 InsertConfigNode(pRoot, "REM", &pREM);
916 InsertConfigInteger(pREM, "64bitEnabled", 1);
917#endif
918 }
919#if ARCH_BITS == 32 /* 32-bit guests only. */
920 else
921 {
922 InsertConfigInteger(pHWVirtExt, "64bitEnabled", 0);
923 }
924#endif
925
926 /** @todo Not exactly pretty to check strings; VBOXOSTYPE would be better, but that requires quite a bit of API change in Main. */
927 if ( !fIsGuest64Bit
928 && fIOAPIC
929 && ( osTypeId == "WindowsNT4"
930 || osTypeId == "Windows2000"
931 || osTypeId == "WindowsXP"
932 || osTypeId == "Windows2003"))
933 {
934 /* Only allow TPR patching for NT, Win2k, XP and Windows Server 2003. (32 bits mode)
935 * We may want to consider adding more guest OSes (Solaris) later on.
936 */
937 InsertConfigInteger(pHWVirtExt, "TPRPatchingEnabled", 1);
938 }
939 }
940
941 /* HWVirtEx exclusive mode */
942 BOOL fHWVirtExExclusive = true;
943 hrc = pMachine->GetHWVirtExProperty(HWVirtExPropertyType_Exclusive, &fHWVirtExExclusive); H();
944 InsertConfigInteger(pHWVirtExt, "Exclusive", fHWVirtExExclusive);
945
946 /* Nested paging (VT-x/AMD-V) */
947 BOOL fEnableNestedPaging = false;
948 hrc = pMachine->GetHWVirtExProperty(HWVirtExPropertyType_NestedPaging, &fEnableNestedPaging); H();
949 InsertConfigInteger(pHWVirtExt, "EnableNestedPaging", fEnableNestedPaging);
950
951 /* Large pages; requires nested paging */
952 BOOL fEnableLargePages = false;
953 hrc = pMachine->GetHWVirtExProperty(HWVirtExPropertyType_LargePages, &fEnableLargePages); H();
954 InsertConfigInteger(pHWVirtExt, "EnableLargePages", fEnableLargePages);
955
956 /* VPID (VT-x) */
957 BOOL fEnableVPID = false;
958 hrc = pMachine->GetHWVirtExProperty(HWVirtExPropertyType_VPID, &fEnableVPID); H();
959 InsertConfigInteger(pHWVirtExt, "EnableVPID", fEnableVPID);
960
961 /* Physical Address Extension (PAE) */
962 BOOL fEnablePAE = false;
963 hrc = pMachine->GetCPUProperty(CPUPropertyType_PAE, &fEnablePAE); H();
964 InsertConfigInteger(pRoot, "EnablePAE", fEnablePAE);
965
966 /* Synthetic CPU */
967 BOOL fSyntheticCpu = false;
968 hrc = pMachine->GetCPUProperty(CPUPropertyType_Synthetic, &fSyntheticCpu); H();
969 InsertConfigInteger(pCPUM, "SyntheticCpu", fSyntheticCpu);
970
971 if (fOsXGuest)
972 InsertConfigInteger(pCPUM, "EnableHVP", 1);
973
974 BOOL fPXEDebug;
975 hrc = biosSettings->COMGETTER(PXEDebugEnabled)(&fPXEDebug); H();
976
977 /*
978 * PDM config.
979 * Load drivers in VBoxC.[so|dll]
980 */
981 PCFGMNODE pPDM;
982 PCFGMNODE pNode;
983 PCFGMNODE pMod;
984 InsertConfigNode(pRoot, "PDM", &pPDM);
985 InsertConfigNode(pPDM, "Devices", &pNode);
986 InsertConfigNode(pPDM, "Drivers", &pNode);
987 InsertConfigNode(pNode, "VBoxC", &pMod);
988#ifdef VBOX_WITH_XPCOM
989 // VBoxC is located in the components subdirectory
990 char szPathVBoxC[RTPATH_MAX];
991 rc = RTPathAppPrivateArch(szPathVBoxC, RTPATH_MAX - sizeof("/components/VBoxC")); AssertRC(rc);
992 strcat(szPathVBoxC, "/components/VBoxC");
993 InsertConfigString(pMod, "Path", szPathVBoxC);
994#else
995 InsertConfigString(pMod, "Path", "VBoxC");
996#endif
997
998
999 /*
1000 * Block cache settings.
1001 */
1002 PCFGMNODE pPDMBlkCache;
1003 InsertConfigNode(pPDM, "BlkCache", &pPDMBlkCache);
1004
1005 /* I/O cache size */
1006 ULONG ioCacheSize = 5;
1007 hrc = pMachine->COMGETTER(IOCacheSize)(&ioCacheSize); H();
1008 InsertConfigInteger(pPDMBlkCache, "CacheSize", ioCacheSize * _1M);
1009
1010 /*
1011 * Bandwidth groups.
1012 */
1013 PCFGMNODE pAc;
1014 PCFGMNODE pAcFile;
1015 PCFGMNODE pAcFileBwGroups;
1016 ComPtr<IBandwidthControl> bwCtrl;
1017 com::SafeIfaceArray<IBandwidthGroup> bwGroups;
1018
1019 hrc = pMachine->COMGETTER(BandwidthControl)(bwCtrl.asOutParam()); H();
1020
1021 hrc = bwCtrl->GetAllBandwidthGroups(ComSafeArrayAsOutParam(bwGroups)); H();
1022
1023 InsertConfigNode(pPDM, "AsyncCompletion", &pAc);
1024 InsertConfigNode(pAc, "File", &pAcFile);
1025 InsertConfigNode(pAcFile, "BwGroups", &pAcFileBwGroups);
1026#ifdef VBOX_WITH_NETSHAPER
1027 PCFGMNODE pNetworkShaper;
1028 PCFGMNODE pNetworkBwGroups;
1029
1030 InsertConfigNode(pPDM, "NetworkShaper", &pNetworkShaper);
1031 InsertConfigNode(pNetworkShaper, "BwGroups", &pNetworkBwGroups);
1032#endif /* VBOX_WITH_NETSHAPER */
1033
1034 for (size_t i = 0; i < bwGroups.size(); i++)
1035 {
1036 Bstr strName;
1037 LONG64 cMaxBytesPerSec;
1038 BandwidthGroupType_T enmType;
1039
1040 hrc = bwGroups[i]->COMGETTER(Name)(strName.asOutParam()); H();
1041 hrc = bwGroups[i]->COMGETTER(Type)(&enmType); H();
1042 hrc = bwGroups[i]->COMGETTER(MaxBytesPerSec)(&cMaxBytesPerSec); H();
1043
1044 if (enmType == BandwidthGroupType_Disk)
1045 {
1046 PCFGMNODE pBwGroup;
1047 InsertConfigNode(pAcFileBwGroups, Utf8Str(strName).c_str(), &pBwGroup);
1048 InsertConfigInteger(pBwGroup, "Max", cMaxBytesPerSec);
1049 InsertConfigInteger(pBwGroup, "Start", cMaxBytesPerSec);
1050 InsertConfigInteger(pBwGroup, "Step", 0);
1051 }
1052#ifdef VBOX_WITH_NETSHAPER
1053 else if (enmType == BandwidthGroupType_Network)
1054 {
1055 /* Network bandwidth groups. */
1056 PCFGMNODE pBwGroup;
1057 InsertConfigNode(pNetworkBwGroups, Utf8Str(strName).c_str(), &pBwGroup);
1058 InsertConfigInteger(pBwGroup, "Max", cMaxBytesPerSec);
1059 }
1060#endif /* VBOX_WITH_NETSHAPER */
1061 }
1062
1063 /*
1064 * Devices
1065 */
1066 PCFGMNODE pDevices = NULL; /* /Devices */
1067 PCFGMNODE pDev = NULL; /* /Devices/Dev/ */
1068 PCFGMNODE pInst = NULL; /* /Devices/Dev/0/ */
1069 PCFGMNODE pCfg = NULL; /* /Devices/Dev/.../Config/ */
1070 PCFGMNODE pLunL0 = NULL; /* /Devices/Dev/0/LUN#0/ */
1071 PCFGMNODE pLunL1 = NULL; /* /Devices/Dev/0/LUN#0/AttachedDriver/ */
1072 PCFGMNODE pLunL2 = NULL; /* /Devices/Dev/0/LUN#0/AttachedDriver/Config/ */
1073 PCFGMNODE pBiosCfg = NULL; /* /Devices/pcbios/0/Config/ */
1074 PCFGMNODE pNetBootCfg = NULL; /* /Devices/pcbios/0/Config/NetBoot/ */
1075
1076 InsertConfigNode(pRoot, "Devices", &pDevices);
1077
1078 /*
1079 * PC Arch.
1080 */
1081 InsertConfigNode(pDevices, "pcarch", &pDev);
1082 InsertConfigNode(pDev, "0", &pInst);
1083 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1084 InsertConfigNode(pInst, "Config", &pCfg);
1085
1086 /*
1087 * The time offset
1088 */
1089 LONG64 timeOffset;
1090 hrc = biosSettings->COMGETTER(TimeOffset)(&timeOffset); H();
1091 PCFGMNODE pTMNode;
1092 InsertConfigNode(pRoot, "TM", &pTMNode);
1093 InsertConfigInteger(pTMNode, "UTCOffset", timeOffset * 1000000);
1094
1095 /*
1096 * DMA
1097 */
1098 InsertConfigNode(pDevices, "8237A", &pDev);
1099 InsertConfigNode(pDev, "0", &pInst);
1100 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1101
1102 /*
1103 * PCI buses.
1104 */
1105 uint32_t uIocPCIAddress, uHbcPCIAddress;
1106 switch (chipsetType)
1107 {
1108 default:
1109 Assert(false);
1110 case ChipsetType_PIIX3:
1111 InsertConfigNode(pDevices, "pci", &pDev);
1112 uHbcPCIAddress = (0x0 << 16) | 0;
1113 uIocPCIAddress = (0x1 << 16) | 0; // ISA controller
1114 break;
1115 case ChipsetType_ICH9:
1116 InsertConfigNode(pDevices, "ich9pci", &pDev);
1117 uHbcPCIAddress = (0x1e << 16) | 0;
1118 uIocPCIAddress = (0x1f << 16) | 0; // LPC controller
1119 break;
1120 }
1121 InsertConfigNode(pDev, "0", &pInst);
1122 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1123 InsertConfigNode(pInst, "Config", &pCfg);
1124 InsertConfigInteger(pCfg, "IOAPIC", fIOAPIC);
1125 if (chipsetType == ChipsetType_ICH9)
1126 {
1127 /* Provide MCFG info */
1128 InsertConfigInteger(pCfg, "McfgBase", uMcfgBase);
1129 InsertConfigInteger(pCfg, "McfgLength", cbMcfgLength);
1130
1131
1132 /* And register 2 bridges */
1133 InsertConfigNode(pDevices, "ich9pcibridge", &pDev);
1134 InsertConfigNode(pDev, "0", &pInst);
1135 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1136 hrc = BusMgr->assignPCIDevice("ich9pcibridge", pInst); H();
1137
1138 InsertConfigNode(pDev, "1", &pInst);
1139 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1140 hrc = BusMgr->assignPCIDevice("ich9pcibridge", pInst); H();
1141
1142#ifdef VBOX_WITH_PCI_PASSTHROUGH
1143 /* Add PCI passthrough devices */
1144 hrc = attachRawPCIDevices(pVM, BusMgr, pDevices); H();
1145#endif
1146 }
1147
1148 /*
1149 * Enable the following devices: HPET, SMC and LPC on MacOS X guests or on ICH9 chipset
1150 */
1151
1152 /*
1153 * High Precision Event Timer (HPET)
1154 */
1155 BOOL fHPETEnabled;
1156 /* Other guests may wish to use HPET too, but MacOS X not functional without it */
1157 hrc = pMachine->COMGETTER(HPETEnabled)(&fHPETEnabled); H();
1158 /* so always enable HPET in extended profile */
1159 fHPETEnabled |= fOsXGuest;
1160 /* HPET is always present on ICH9 */
1161 fHPETEnabled |= (chipsetType == ChipsetType_ICH9);
1162 if (fHPETEnabled)
1163 {
1164 InsertConfigNode(pDevices, "hpet", &pDev);
1165 InsertConfigNode(pDev, "0", &pInst);
1166 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1167 InsertConfigNode(pInst, "Config", &pCfg);
1168 InsertConfigInteger(pCfg, "ICH9", (chipsetType == ChipsetType_ICH9) ? 1 : 0); /* boolean */
1169 }
1170
1171 /*
1172 * System Management Controller (SMC)
1173 */
1174 BOOL fSmcEnabled;
1175 fSmcEnabled = fOsXGuest;
1176 if (fSmcEnabled)
1177 {
1178 InsertConfigNode(pDevices, "smc", &pDev);
1179 InsertConfigNode(pDev, "0", &pInst);
1180 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1181 InsertConfigNode(pInst, "Config", &pCfg);
1182
1183 bool fGetKeyFromRealSMC;
1184 Bstr bstrKey;
1185 rc = getSmcDeviceKey(pMachine, bstrKey.asOutParam(), &fGetKeyFromRealSMC);
1186 AssertRCReturn(rc, rc);
1187
1188 InsertConfigString(pCfg, "DeviceKey", bstrKey);
1189 InsertConfigInteger(pCfg, "GetKeyFromRealSMC", fGetKeyFromRealSMC);
1190 }
1191
1192 /*
1193 * Low Pin Count (LPC) bus
1194 */
1195 BOOL fLpcEnabled;
1196 /** @todo: implement appropriate getter */
1197 fLpcEnabled = fOsXGuest || (chipsetType == ChipsetType_ICH9);
1198 if (fLpcEnabled)
1199 {
1200 InsertConfigNode(pDevices, "lpc", &pDev);
1201 InsertConfigNode(pDev, "0", &pInst);
1202 hrc = BusMgr->assignPCIDevice("lpc", pInst); H();
1203 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1204 }
1205
1206 BOOL fShowRtc;
1207 fShowRtc = fOsXGuest || (chipsetType == ChipsetType_ICH9);
1208
1209 /*
1210 * PS/2 keyboard & mouse.
1211 */
1212 InsertConfigNode(pDevices, "pckbd", &pDev);
1213 InsertConfigNode(pDev, "0", &pInst);
1214 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1215 InsertConfigNode(pInst, "Config", &pCfg);
1216
1217 InsertConfigNode(pInst, "LUN#0", &pLunL0);
1218 InsertConfigString(pLunL0, "Driver", "KeyboardQueue");
1219 InsertConfigNode(pLunL0, "Config", &pCfg);
1220 InsertConfigInteger(pCfg, "QueueSize", 64);
1221
1222 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
1223 InsertConfigString(pLunL1, "Driver", "MainKeyboard");
1224 InsertConfigNode(pLunL1, "Config", &pCfg);
1225 Keyboard *pKeyboard = mKeyboard;
1226 InsertConfigInteger(pCfg, "Object", (uintptr_t)pKeyboard);
1227
1228 InsertConfigNode(pInst, "LUN#1", &pLunL0);
1229 InsertConfigString(pLunL0, "Driver", "MouseQueue");
1230 InsertConfigNode(pLunL0, "Config", &pCfg);
1231 InsertConfigInteger(pCfg, "QueueSize", 128);
1232
1233 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
1234 InsertConfigString(pLunL1, "Driver", "MainMouse");
1235 InsertConfigNode(pLunL1, "Config", &pCfg);
1236 Mouse *pMouse = mMouse;
1237 InsertConfigInteger(pCfg, "Object", (uintptr_t)pMouse);
1238
1239 /*
1240 * i8254 Programmable Interval Timer And Dummy Speaker
1241 */
1242 InsertConfigNode(pDevices, "i8254", &pDev);
1243 InsertConfigNode(pDev, "0", &pInst);
1244 InsertConfigNode(pInst, "Config", &pCfg);
1245#ifdef DEBUG
1246 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1247#endif
1248
1249 /*
1250 * i8259 Programmable Interrupt Controller.
1251 */
1252 InsertConfigNode(pDevices, "i8259", &pDev);
1253 InsertConfigNode(pDev, "0", &pInst);
1254 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1255 InsertConfigNode(pInst, "Config", &pCfg);
1256
1257 /*
1258 * Advanced Programmable Interrupt Controller.
1259 * SMP: Each CPU has a LAPIC, but we have a single device representing all LAPICs states,
1260 * thus only single insert
1261 */
1262 InsertConfigNode(pDevices, "apic", &pDev);
1263 InsertConfigNode(pDev, "0", &pInst);
1264 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1265 InsertConfigNode(pInst, "Config", &pCfg);
1266 InsertConfigInteger(pCfg, "IOAPIC", fIOAPIC);
1267 InsertConfigInteger(pCfg, "NumCPUs", cCpus);
1268
1269 if (fIOAPIC)
1270 {
1271 /*
1272 * I/O Advanced Programmable Interrupt Controller.
1273 */
1274 InsertConfigNode(pDevices, "ioapic", &pDev);
1275 InsertConfigNode(pDev, "0", &pInst);
1276 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1277 InsertConfigNode(pInst, "Config", &pCfg);
1278 InsertConfigInteger(pCfg, "NumCPUs", cCpus);
1279 }
1280
1281 /*
1282 * RTC MC146818.
1283 */
1284 InsertConfigNode(pDevices, "mc146818", &pDev);
1285 InsertConfigNode(pDev, "0", &pInst);
1286 InsertConfigNode(pInst, "Config", &pCfg);
1287 BOOL fRTCUseUTC;
1288 hrc = pMachine->COMGETTER(RTCUseUTC)(&fRTCUseUTC); H();
1289 InsertConfigInteger(pCfg, "UseUTC", fRTCUseUTC ? 1 : 0);
1290
1291 /*
1292 * VGA.
1293 */
1294 InsertConfigNode(pDevices, "vga", &pDev);
1295 InsertConfigNode(pDev, "0", &pInst);
1296 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1297
1298 hrc = BusMgr->assignPCIDevice("vga", pInst); H();
1299 InsertConfigNode(pInst, "Config", &pCfg);
1300 ULONG cVRamMBs;
1301 hrc = pMachine->COMGETTER(VRAMSize)(&cVRamMBs); H();
1302 InsertConfigInteger(pCfg, "VRamSize", cVRamMBs * _1M);
1303 ULONG cMonitorCount;
1304 hrc = pMachine->COMGETTER(MonitorCount)(&cMonitorCount); H();
1305 InsertConfigInteger(pCfg, "MonitorCount", cMonitorCount);
1306#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1307 InsertConfigInteger(pCfg, "R0Enabled", fHWVirtExEnabled);
1308#endif
1309
1310 /*
1311 * BIOS logo
1312 */
1313 BOOL fFadeIn;
1314 hrc = biosSettings->COMGETTER(LogoFadeIn)(&fFadeIn); H();
1315 InsertConfigInteger(pCfg, "FadeIn", fFadeIn ? 1 : 0);
1316 BOOL fFadeOut;
1317 hrc = biosSettings->COMGETTER(LogoFadeOut)(&fFadeOut); H();
1318 InsertConfigInteger(pCfg, "FadeOut", fFadeOut ? 1: 0);
1319 ULONG logoDisplayTime;
1320 hrc = biosSettings->COMGETTER(LogoDisplayTime)(&logoDisplayTime); H();
1321 InsertConfigInteger(pCfg, "LogoTime", logoDisplayTime);
1322 Bstr logoImagePath;
1323 hrc = biosSettings->COMGETTER(LogoImagePath)(logoImagePath.asOutParam()); H();
1324 InsertConfigString(pCfg, "LogoFile", Utf8Str(!logoImagePath.isEmpty() ? logoImagePath : "") );
1325
1326 /*
1327 * Boot menu
1328 */
1329 BIOSBootMenuMode_T eBootMenuMode;
1330 int iShowBootMenu;
1331 biosSettings->COMGETTER(BootMenuMode)(&eBootMenuMode);
1332 switch (eBootMenuMode)
1333 {
1334 case BIOSBootMenuMode_Disabled: iShowBootMenu = 0; break;
1335 case BIOSBootMenuMode_MenuOnly: iShowBootMenu = 1; break;
1336 default: iShowBootMenu = 2; break;
1337 }
1338 InsertConfigInteger(pCfg, "ShowBootMenu", iShowBootMenu);
1339
1340 /* Custom VESA mode list */
1341 unsigned cModes = 0;
1342 for (unsigned iMode = 1; iMode <= 16; ++iMode)
1343 {
1344 char szExtraDataKey[sizeof("CustomVideoModeXX")];
1345 RTStrPrintf(szExtraDataKey, sizeof(szExtraDataKey), "CustomVideoMode%u", iMode);
1346 hrc = pMachine->GetExtraData(Bstr(szExtraDataKey).raw(), bstr.asOutParam()); H();
1347 if (bstr.isEmpty())
1348 break;
1349 InsertConfigString(pCfg, szExtraDataKey, bstr);
1350 ++cModes;
1351 }
1352 InsertConfigInteger(pCfg, "CustomVideoModes", cModes);
1353
1354 /* VESA height reduction */
1355 ULONG ulHeightReduction;
1356 IFramebuffer *pFramebuffer = getDisplay()->getFramebuffer();
1357 if (pFramebuffer)
1358 {
1359 hrc = pFramebuffer->COMGETTER(HeightReduction)(&ulHeightReduction); H();
1360 }
1361 else
1362 {
1363 /* If framebuffer is not available, there is no height reduction. */
1364 ulHeightReduction = 0;
1365 }
1366 InsertConfigInteger(pCfg, "HeightReduction", ulHeightReduction);
1367
1368 /* Attach the display. */
1369 InsertConfigNode(pInst, "LUN#0", &pLunL0);
1370 InsertConfigString(pLunL0, "Driver", "MainDisplay");
1371 InsertConfigNode(pLunL0, "Config", &pCfg);
1372 Display *pDisplay = mDisplay;
1373 InsertConfigInteger(pCfg, "Object", (uintptr_t)pDisplay);
1374
1375
1376 /*
1377 * Firmware.
1378 */
1379 FirmwareType_T eFwType = FirmwareType_BIOS;
1380 hrc = pMachine->COMGETTER(FirmwareType)(&eFwType); H();
1381
1382#ifdef VBOX_WITH_EFI
1383 BOOL fEfiEnabled = (eFwType >= FirmwareType_EFI) && (eFwType <= FirmwareType_EFIDUAL);
1384#else
1385 BOOL fEfiEnabled = false;
1386#endif
1387 if (!fEfiEnabled)
1388 {
1389 /*
1390 * PC Bios.
1391 */
1392 InsertConfigNode(pDevices, "pcbios", &pDev);
1393 InsertConfigNode(pDev, "0", &pInst);
1394 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1395 InsertConfigNode(pInst, "Config", &pBiosCfg);
1396 InsertConfigInteger(pBiosCfg, "RamSize", cbRam);
1397 InsertConfigInteger(pBiosCfg, "RamHoleSize", cbRamHole);
1398 InsertConfigInteger(pBiosCfg, "NumCPUs", cCpus);
1399 InsertConfigString(pBiosCfg, "HardDiskDevice", "piix3ide");
1400 InsertConfigString(pBiosCfg, "FloppyDevice", "i82078");
1401 InsertConfigInteger(pBiosCfg, "IOAPIC", fIOAPIC);
1402 InsertConfigInteger(pBiosCfg, "PXEDebug", fPXEDebug);
1403 InsertConfigBytes(pBiosCfg, "UUID", &HardwareUuid,sizeof(HardwareUuid));
1404 InsertConfigNode(pBiosCfg, "NetBoot", &pNetBootCfg);
1405 InsertConfigInteger(pBiosCfg, "McfgBase", uMcfgBase);
1406 InsertConfigInteger(pBiosCfg, "McfgLength", cbMcfgLength);
1407
1408 DeviceType_T bootDevice;
1409 if (SchemaDefs::MaxBootPosition > 9)
1410 {
1411 AssertMsgFailed(("Too many boot devices %d\n",
1412 SchemaDefs::MaxBootPosition));
1413 return VERR_INVALID_PARAMETER;
1414 }
1415
1416 for (ULONG pos = 1; pos <= SchemaDefs::MaxBootPosition; ++pos)
1417 {
1418 hrc = pMachine->GetBootOrder(pos, &bootDevice); H();
1419
1420 char szParamName[] = "BootDeviceX";
1421 szParamName[sizeof(szParamName) - 2] = ((char (pos - 1)) + '0');
1422
1423 const char *pszBootDevice;
1424 switch (bootDevice)
1425 {
1426 case DeviceType_Null:
1427 pszBootDevice = "NONE";
1428 break;
1429 case DeviceType_HardDisk:
1430 pszBootDevice = "IDE";
1431 break;
1432 case DeviceType_DVD:
1433 pszBootDevice = "DVD";
1434 break;
1435 case DeviceType_Floppy:
1436 pszBootDevice = "FLOPPY";
1437 break;
1438 case DeviceType_Network:
1439 pszBootDevice = "LAN";
1440 break;
1441 default:
1442 AssertMsgFailed(("Invalid bootDevice=%d\n", bootDevice));
1443 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
1444 N_("Invalid boot device '%d'"), bootDevice);
1445 }
1446 InsertConfigString(pBiosCfg, szParamName, pszBootDevice);
1447 }
1448 }
1449 else
1450 {
1451 /* Autodetect firmware type, basing on guest type */
1452 if (eFwType == FirmwareType_EFI)
1453 {
1454 eFwType = fIsGuest64Bit
1455 ? (FirmwareType_T)FirmwareType_EFI64
1456 : (FirmwareType_T)FirmwareType_EFI32;
1457 }
1458 bool const f64BitEntry = eFwType == FirmwareType_EFI64;
1459
1460 Utf8Str efiRomFile;
1461 rc = findEfiRom(virtualBox, eFwType, &efiRomFile);
1462 AssertRCReturn(rc, rc);
1463
1464 /* Get boot args */
1465 Bstr bootArgs;
1466 hrc = pMachine->GetExtraData(Bstr("VBoxInternal2/EfiBootArgs").raw(), bootArgs.asOutParam()); H();
1467
1468 /* Get device props */
1469 Bstr deviceProps;
1470 hrc = pMachine->GetExtraData(Bstr("VBoxInternal2/EfiDeviceProps").raw(), deviceProps.asOutParam()); H();
1471
1472 /* Get GOP mode settings */
1473 uint32_t u32GopMode = UINT32_MAX;
1474 hrc = pMachine->GetExtraData(Bstr("VBoxInternal2/EfiGopMode").raw(), bstr.asOutParam()); H();
1475 if (!bstr.isEmpty())
1476 u32GopMode = Utf8Str(bstr).toUInt32();
1477
1478 /* UGA mode settings */
1479 uint32_t u32UgaHorisontal = 0;
1480 hrc = pMachine->GetExtraData(Bstr("VBoxInternal2/EfiUgaHorizontalResolution").raw(), bstr.asOutParam()); H();
1481 if (!bstr.isEmpty())
1482 u32UgaHorisontal = Utf8Str(bstr).toUInt32();
1483
1484 uint32_t u32UgaVertical = 0;
1485 hrc = pMachine->GetExtraData(Bstr("VBoxInternal2/EfiUgaVerticalResolution").raw(), bstr.asOutParam()); H();
1486 if (!bstr.isEmpty())
1487 u32UgaVertical = Utf8Str(bstr).toUInt32();
1488
1489 /*
1490 * EFI subtree.
1491 */
1492 InsertConfigNode(pDevices, "efi", &pDev);
1493 InsertConfigNode(pDev, "0", &pInst);
1494 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1495 InsertConfigNode(pInst, "Config", &pCfg);
1496 InsertConfigInteger(pCfg, "RamSize", cbRam);
1497 InsertConfigInteger(pCfg, "RamHoleSize", cbRamHole);
1498 InsertConfigInteger(pCfg, "NumCPUs", cCpus);
1499 InsertConfigString(pCfg, "EfiRom", efiRomFile);
1500 InsertConfigString(pCfg, "BootArgs", bootArgs);
1501 InsertConfigString(pCfg, "DeviceProps", deviceProps);
1502 InsertConfigInteger(pCfg, "IOAPIC", fIOAPIC);
1503 InsertConfigBytes(pCfg, "UUID", &HardwareUuid,sizeof(HardwareUuid));
1504 InsertConfigInteger(pCfg, "64BitEntry", f64BitEntry); /* boolean */
1505 InsertConfigInteger(pCfg, "GopMode", u32GopMode);
1506 InsertConfigInteger(pCfg, "UgaHorizontalResolution", u32UgaHorisontal);
1507 InsertConfigInteger(pCfg, "UgaVerticalResolution", u32UgaVertical);
1508
1509 /* For OS X guests we'll force passing host's DMI info to the guest */
1510 if (fOsXGuest)
1511 {
1512 InsertConfigInteger(pCfg, "DmiUseHostInfo", 1);
1513 InsertConfigInteger(pCfg, "DmiExposeMemoryTable", 1);
1514 }
1515 PCFGMNODE pDrv;
1516 InsertConfigNode(pInst, "LUN#0", &pLunL0);
1517 InsertConfigString(pLunL0, "Driver", "NvramStorage");
1518 InsertConfigNode(pLunL0, "Config", &pCfg);
1519 InsertConfigInteger(pCfg, "Object", (uint64_t)mNvram);
1520 }
1521
1522 /*
1523 * Storage controllers.
1524 */
1525 com::SafeIfaceArray<IStorageController> ctrls;
1526 PCFGMNODE aCtrlNodes[StorageControllerType_LsiLogicSas + 1] = {};
1527 hrc = pMachine->COMGETTER(StorageControllers)(ComSafeArrayAsOutParam(ctrls)); H();
1528
1529 bool fFdcEnabled = false;
1530 for (size_t i = 0; i < ctrls.size(); ++i)
1531 {
1532 DeviceType_T *paLedDevType = NULL;
1533
1534 StorageControllerType_T enmCtrlType;
1535 rc = ctrls[i]->COMGETTER(ControllerType)(&enmCtrlType); H();
1536 AssertRelease((unsigned)enmCtrlType < RT_ELEMENTS(aCtrlNodes));
1537
1538 StorageBus_T enmBus;
1539 rc = ctrls[i]->COMGETTER(Bus)(&enmBus); H();
1540
1541 Bstr controllerName;
1542 rc = ctrls[i]->COMGETTER(Name)(controllerName.asOutParam()); H();
1543
1544 ULONG ulInstance = 999;
1545 rc = ctrls[i]->COMGETTER(Instance)(&ulInstance); H();
1546
1547 BOOL fUseHostIOCache;
1548 rc = ctrls[i]->COMGETTER(UseHostIOCache)(&fUseHostIOCache); H();
1549
1550 BOOL fBootable;
1551 rc = ctrls[i]->COMGETTER(Bootable)(&fBootable); H();
1552
1553 /* /Devices/<ctrldev>/ */
1554 const char *pszCtrlDev = convertControllerTypeToDev(enmCtrlType);
1555 pDev = aCtrlNodes[enmCtrlType];
1556 if (!pDev)
1557 {
1558 InsertConfigNode(pDevices, pszCtrlDev, &pDev);
1559 aCtrlNodes[enmCtrlType] = pDev; /* IDE variants are handled in the switch */
1560 }
1561
1562 /* /Devices/<ctrldev>/<instance>/ */
1563 PCFGMNODE pCtlInst = NULL;
1564 InsertConfigNode(pDev, Utf8StrFmt("%u", ulInstance).c_str(), &pCtlInst);
1565
1566 /* Device config: /Devices/<ctrldev>/<instance>/<values> & /ditto/Config/<values> */
1567 InsertConfigInteger(pCtlInst, "Trusted", 1);
1568 InsertConfigNode(pCtlInst, "Config", &pCfg);
1569
1570 switch (enmCtrlType)
1571 {
1572 case StorageControllerType_LsiLogic:
1573 {
1574 hrc = BusMgr->assignPCIDevice("lsilogic", pCtlInst); H();
1575
1576 InsertConfigInteger(pCfg, "Bootable", fBootable);
1577
1578 /* Attach the status driver */
1579 Assert(cLedScsi >= 16);
1580 attachStatusDriver(pCtlInst, &mapStorageLeds[iLedScsi], 0, 15,
1581 &mapMediumAttachments, pszCtrlDev, ulInstance);
1582 paLedDevType = &maStorageDevType[iLedScsi];
1583 break;
1584 }
1585
1586 case StorageControllerType_BusLogic:
1587 {
1588 hrc = BusMgr->assignPCIDevice("buslogic", pCtlInst); H();
1589
1590 InsertConfigInteger(pCfg, "Bootable", fBootable);
1591
1592 /* Attach the status driver */
1593 Assert(cLedScsi >= 16);
1594 attachStatusDriver(pCtlInst, &mapStorageLeds[iLedScsi], 0, 15,
1595 &mapMediumAttachments, pszCtrlDev, ulInstance);
1596 paLedDevType = &maStorageDevType[iLedScsi];
1597 break;
1598 }
1599
1600 case StorageControllerType_IntelAhci:
1601 {
1602 hrc = BusMgr->assignPCIDevice("ahci", pCtlInst); H();
1603
1604 ULONG cPorts = 0;
1605 hrc = ctrls[i]->COMGETTER(PortCount)(&cPorts); H();
1606 InsertConfigInteger(pCfg, "PortCount", cPorts);
1607 InsertConfigInteger(pCfg, "Bootable", fBootable);
1608
1609 /* Needed configuration values for the bios, only first controller. */
1610 if (!BusMgr->hasPCIDevice("ahci", 1))
1611 {
1612 if (pBiosCfg)
1613 {
1614 InsertConfigString(pBiosCfg, "SataHardDiskDevice", "ahci");
1615 }
1616 }
1617
1618 /* Attach the status driver */
1619 AssertRelease(cPorts <= cLedSata);
1620 attachStatusDriver(pCtlInst, &mapStorageLeds[iLedSata], 0, cPorts - 1,
1621 &mapMediumAttachments, pszCtrlDev, ulInstance);
1622 paLedDevType = &maStorageDevType[iLedSata];
1623 break;
1624 }
1625
1626 case StorageControllerType_PIIX3:
1627 case StorageControllerType_PIIX4:
1628 case StorageControllerType_ICH6:
1629 {
1630 /*
1631 * IDE (update this when the main interface changes)
1632 */
1633 hrc = BusMgr->assignPCIDevice("piix3ide", pCtlInst); H();
1634 InsertConfigString(pCfg, "Type", controllerString(enmCtrlType));
1635 /* Attach the status driver */
1636 Assert(cLedIde >= 4);
1637 attachStatusDriver(pCtlInst, &mapStorageLeds[iLedIde], 0, 3,
1638 &mapMediumAttachments, pszCtrlDev, ulInstance);
1639 paLedDevType = &maStorageDevType[iLedIde];
1640
1641 /* IDE flavors */
1642 aCtrlNodes[StorageControllerType_PIIX3] = pDev;
1643 aCtrlNodes[StorageControllerType_PIIX4] = pDev;
1644 aCtrlNodes[StorageControllerType_ICH6] = pDev;
1645 break;
1646 }
1647
1648 case StorageControllerType_I82078:
1649 {
1650 /*
1651 * i82078 Floppy drive controller
1652 */
1653 fFdcEnabled = true;
1654 InsertConfigInteger(pCfg, "IRQ", 6);
1655 InsertConfigInteger(pCfg, "DMA", 2);
1656 InsertConfigInteger(pCfg, "MemMapped", 0 );
1657 InsertConfigInteger(pCfg, "IOBase", 0x3f0);
1658
1659 /* Attach the status driver */
1660 Assert(cLedFloppy >= 2);
1661 attachStatusDriver(pCtlInst, &mapStorageLeds[iLedFloppy], 0, 1,
1662 &mapMediumAttachments, pszCtrlDev, ulInstance);
1663 paLedDevType = &maStorageDevType[iLedFloppy];
1664 break;
1665 }
1666
1667 case StorageControllerType_LsiLogicSas:
1668 {
1669 hrc = BusMgr->assignPCIDevice("lsilogicsas", pCtlInst); H();
1670
1671 InsertConfigString(pCfg, "ControllerType", "SAS1068");
1672 InsertConfigInteger(pCfg, "Bootable", fBootable);
1673
1674 /* Attach the status driver */
1675 Assert(cLedSas >= 8);
1676 attachStatusDriver(pCtlInst, &mapStorageLeds[iLedSas], 0, 7,
1677 &mapMediumAttachments, pszCtrlDev, ulInstance);
1678 paLedDevType = &maStorageDevType[iLedSas];
1679 break;
1680 }
1681
1682 default:
1683 AssertMsgFailedReturn(("invalid storage controller type: %d\n", enmCtrlType), VERR_GENERAL_FAILURE);
1684 }
1685
1686 /* Attach the media to the storage controllers. */
1687 com::SafeIfaceArray<IMediumAttachment> atts;
1688 hrc = pMachine->GetMediumAttachmentsOfController(controllerName.raw(),
1689 ComSafeArrayAsOutParam(atts)); H();
1690
1691 /* Builtin I/O cache - per device setting. */
1692 BOOL fBuiltinIOCache = true;
1693 hrc = pMachine->COMGETTER(IOCacheEnabled)(&fBuiltinIOCache); H();
1694
1695
1696 for (size_t j = 0; j < atts.size(); ++j)
1697 {
1698 IMediumAttachment *pMediumAtt = atts[j];
1699 rc = configMediumAttachment(pCtlInst,
1700 pszCtrlDev,
1701 ulInstance,
1702 enmBus,
1703 !!fUseHostIOCache,
1704 !!fBuiltinIOCache,
1705 false /* fSetupMerge */,
1706 0 /* uMergeSource */,
1707 0 /* uMergeTarget */,
1708 pMediumAtt,
1709 mMachineState,
1710 NULL /* phrc */,
1711 false /* fAttachDetach */,
1712 false /* fForceUnmount */,
1713 false /* fHotplug */,
1714 pVM,
1715 paLedDevType);
1716 if (RT_FAILURE(rc))
1717 return rc;
1718 }
1719 H();
1720 }
1721 H();
1722
1723 /*
1724 * Network adapters
1725 */
1726#ifdef VMWARE_NET_IN_SLOT_11
1727 bool fSwapSlots3and11 = false;
1728#endif
1729 PCFGMNODE pDevPCNet = NULL; /* PCNet-type devices */
1730 InsertConfigNode(pDevices, "pcnet", &pDevPCNet);
1731#ifdef VBOX_WITH_E1000
1732 PCFGMNODE pDevE1000 = NULL; /* E1000-type devices */
1733 InsertConfigNode(pDevices, "e1000", &pDevE1000);
1734#endif
1735#ifdef VBOX_WITH_VIRTIO
1736 PCFGMNODE pDevVirtioNet = NULL; /* Virtio network devices */
1737 InsertConfigNode(pDevices, "virtio-net", &pDevVirtioNet);
1738#endif /* VBOX_WITH_VIRTIO */
1739 std::list<BootNic> llBootNics;
1740 for (ULONG ulInstance = 0; ulInstance < maxNetworkAdapters; ++ulInstance)
1741 {
1742 ComPtr<INetworkAdapter> networkAdapter;
1743 hrc = pMachine->GetNetworkAdapter(ulInstance, networkAdapter.asOutParam()); H();
1744 BOOL fEnabledNetAdapter = FALSE;
1745 hrc = networkAdapter->COMGETTER(Enabled)(&fEnabledNetAdapter); H();
1746 if (!fEnabledNetAdapter)
1747 continue;
1748
1749 /*
1750 * The virtual hardware type. Create appropriate device first.
1751 */
1752 const char *pszAdapterName = "pcnet";
1753 NetworkAdapterType_T adapterType;
1754 hrc = networkAdapter->COMGETTER(AdapterType)(&adapterType); H();
1755 switch (adapterType)
1756 {
1757 case NetworkAdapterType_Am79C970A:
1758 case NetworkAdapterType_Am79C973:
1759 pDev = pDevPCNet;
1760 break;
1761#ifdef VBOX_WITH_E1000
1762 case NetworkAdapterType_I82540EM:
1763 case NetworkAdapterType_I82543GC:
1764 case NetworkAdapterType_I82545EM:
1765 pDev = pDevE1000;
1766 pszAdapterName = "e1000";
1767 break;
1768#endif
1769#ifdef VBOX_WITH_VIRTIO
1770 case NetworkAdapterType_Virtio:
1771 pDev = pDevVirtioNet;
1772 pszAdapterName = "virtio-net";
1773 break;
1774#endif /* VBOX_WITH_VIRTIO */
1775 default:
1776 AssertMsgFailed(("Invalid network adapter type '%d' for slot '%d'",
1777 adapterType, ulInstance));
1778 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
1779 N_("Invalid network adapter type '%d' for slot '%d'"),
1780 adapterType, ulInstance);
1781 }
1782
1783 InsertConfigNode(pDev, Utf8StrFmt("%u", ulInstance).c_str(), &pInst);
1784 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
1785 /* the first network card gets the PCI ID 3, the next 3 gets 8..10,
1786 * next 4 get 16..19. */
1787 int iPCIDeviceNo;
1788 switch (ulInstance)
1789 {
1790 case 0:
1791 iPCIDeviceNo = 3;
1792 break;
1793 case 1: case 2: case 3:
1794 iPCIDeviceNo = ulInstance - 1 + 8;
1795 break;
1796 case 4: case 5: case 6: case 7:
1797 iPCIDeviceNo = ulInstance - 4 + 16;
1798 break;
1799 default:
1800 /* auto assignment */
1801 iPCIDeviceNo = -1;
1802 break;
1803 }
1804#ifdef VMWARE_NET_IN_SLOT_11
1805 /*
1806 * Dirty hack for PCI slot compatibility with VMWare,
1807 * it assigns slot 11 to the first network controller.
1808 */
1809 if (iPCIDeviceNo == 3 && adapterType == NetworkAdapterType_I82545EM)
1810 {
1811 iPCIDeviceNo = 0x11;
1812 fSwapSlots3and11 = true;
1813 }
1814 else if (iPCIDeviceNo == 0x11 && fSwapSlots3and11)
1815 iPCIDeviceNo = 3;
1816#endif
1817 PCIBusAddress PCIAddr = PCIBusAddress(0, iPCIDeviceNo, 0);
1818 hrc = BusMgr->assignPCIDevice(pszAdapterName, pInst, PCIAddr); H();
1819
1820 InsertConfigNode(pInst, "Config", &pCfg);
1821#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE /* not safe here yet. */ /** @todo Make PCNet ring-0 safe on 32-bit mac kernels! */
1822 if (pDev == pDevPCNet)
1823 {
1824 InsertConfigInteger(pCfg, "R0Enabled", false);
1825 }
1826#endif
1827 /*
1828 * Collect information needed for network booting and add it to the list.
1829 */
1830 BootNic nic;
1831
1832 nic.mInstance = ulInstance;
1833 /* Could be updated by reference, if auto assigned */
1834 nic.mPCIAddress = PCIAddr;
1835
1836 hrc = networkAdapter->COMGETTER(BootPriority)(&nic.mBootPrio); H();
1837
1838 llBootNics.push_back(nic);
1839
1840 /*
1841 * The virtual hardware type. PCNet supports two types.
1842 */
1843 switch (adapterType)
1844 {
1845 case NetworkAdapterType_Am79C970A:
1846 InsertConfigInteger(pCfg, "Am79C973", 0);
1847 break;
1848 case NetworkAdapterType_Am79C973:
1849 InsertConfigInteger(pCfg, "Am79C973", 1);
1850 break;
1851 case NetworkAdapterType_I82540EM:
1852 InsertConfigInteger(pCfg, "AdapterType", 0);
1853 break;
1854 case NetworkAdapterType_I82543GC:
1855 InsertConfigInteger(pCfg, "AdapterType", 1);
1856 break;
1857 case NetworkAdapterType_I82545EM:
1858 InsertConfigInteger(pCfg, "AdapterType", 2);
1859 break;
1860 }
1861
1862 /*
1863 * Get the MAC address and convert it to binary representation
1864 */
1865 Bstr macAddr;
1866 hrc = networkAdapter->COMGETTER(MACAddress)(macAddr.asOutParam()); H();
1867 Assert(!macAddr.isEmpty());
1868 Utf8Str macAddrUtf8 = macAddr;
1869 char *macStr = (char*)macAddrUtf8.c_str();
1870 Assert(strlen(macStr) == 12);
1871 RTMAC Mac;
1872 memset(&Mac, 0, sizeof(Mac));
1873 char *pMac = (char*)&Mac;
1874 for (uint32_t i = 0; i < 6; ++i)
1875 {
1876 char c1 = *macStr++ - '0';
1877 if (c1 > 9)
1878 c1 -= 7;
1879 char c2 = *macStr++ - '0';
1880 if (c2 > 9)
1881 c2 -= 7;
1882 *pMac++ = ((c1 & 0x0f) << 4) | (c2 & 0x0f);
1883 }
1884 InsertConfigBytes(pCfg, "MAC", &Mac, sizeof(Mac));
1885
1886 /*
1887 * Check if the cable is supposed to be unplugged
1888 */
1889 BOOL fCableConnected;
1890 hrc = networkAdapter->COMGETTER(CableConnected)(&fCableConnected); H();
1891 InsertConfigInteger(pCfg, "CableConnected", fCableConnected ? 1 : 0);
1892
1893 /*
1894 * Line speed to report from custom drivers
1895 */
1896 ULONG ulLineSpeed;
1897 hrc = networkAdapter->COMGETTER(LineSpeed)(&ulLineSpeed); H();
1898 InsertConfigInteger(pCfg, "LineSpeed", ulLineSpeed);
1899
1900 /*
1901 * Attach the status driver.
1902 */
1903 attachStatusDriver(pInst, &mapNetworkLeds[ulInstance], 0, 0, NULL, NULL, 0);
1904
1905 /*
1906 * Configure the network card now
1907 */
1908 bool fIgnoreConnectFailure = mMachineState == MachineState_Restoring;
1909 rc = configNetwork(pszAdapterName,
1910 ulInstance,
1911 0,
1912 networkAdapter,
1913 pCfg,
1914 pLunL0,
1915 pInst,
1916 false /*fAttachDetach*/,
1917 fIgnoreConnectFailure);
1918 if (RT_FAILURE(rc))
1919 return rc;
1920 }
1921
1922 /*
1923 * Build network boot information and transfer it to the BIOS.
1924 */
1925 if (pNetBootCfg && !llBootNics.empty()) /* NetBoot node doesn't exist for EFI! */
1926 {
1927 llBootNics.sort(); /* Sort the list by boot priority. */
1928
1929 char achBootIdx[] = "0";
1930 unsigned uBootIdx = 0;
1931
1932 for (std::list<BootNic>::iterator it = llBootNics.begin(); it != llBootNics.end(); ++it)
1933 {
1934 /* A NIC with priority 0 is only used if it's first in the list. */
1935 if (it->mBootPrio == 0 && uBootIdx != 0)
1936 break;
1937
1938 PCFGMNODE pNetBtDevCfg;
1939 achBootIdx[0] = '0' + uBootIdx++; /* Boot device order. */
1940 InsertConfigNode(pNetBootCfg, achBootIdx, &pNetBtDevCfg);
1941 InsertConfigInteger(pNetBtDevCfg, "NIC", it->mInstance);
1942 InsertConfigInteger(pNetBtDevCfg, "PCIBusNo", it->mPCIAddress.miBus);
1943 InsertConfigInteger(pNetBtDevCfg, "PCIDeviceNo", it->mPCIAddress.miDevice);
1944 InsertConfigInteger(pNetBtDevCfg, "PCIFunctionNo", it->mPCIAddress.miFn);
1945 }
1946 }
1947
1948 /*
1949 * Serial (UART) Ports
1950 */
1951 /* serial enabled mask to be passed to dev ACPI */
1952 uint16_t auSerialIoPortBase[SchemaDefs::SerialPortCount] = {0};
1953 uint8_t auSerialIrq[SchemaDefs::SerialPortCount] = {0};
1954 InsertConfigNode(pDevices, "serial", &pDev);
1955 for (ULONG ulInstance = 0; ulInstance < SchemaDefs::SerialPortCount; ++ulInstance)
1956 {
1957 ComPtr<ISerialPort> serialPort;
1958 hrc = pMachine->GetSerialPort(ulInstance, serialPort.asOutParam()); H();
1959 BOOL fEnabledSerPort = FALSE;
1960 if (serialPort)
1961 hrc = serialPort->COMGETTER(Enabled)(&fEnabledSerPort); H();
1962 if (!fEnabledSerPort)
1963 continue;
1964
1965 InsertConfigNode(pDev, Utf8StrFmt("%u", ulInstance).c_str(), &pInst);
1966 InsertConfigNode(pInst, "Config", &pCfg);
1967
1968 ULONG ulIRQ;
1969 hrc = serialPort->COMGETTER(IRQ)(&ulIRQ); H();
1970 InsertConfigInteger(pCfg, "IRQ", ulIRQ);
1971 auSerialIrq[ulInstance] = (uint8_t)ulIRQ;
1972
1973 ULONG ulIOBase;
1974 hrc = serialPort->COMGETTER(IOBase)(&ulIOBase); H();
1975 InsertConfigInteger(pCfg, "IOBase", ulIOBase);
1976 auSerialIoPortBase[ulInstance] = (uint16_t)ulIOBase;
1977
1978 BOOL fServer;
1979 hrc = serialPort->COMGETTER(Server)(&fServer); H();
1980 hrc = serialPort->COMGETTER(Path)(bstr.asOutParam()); H();
1981 PortMode_T eHostMode;
1982 hrc = serialPort->COMGETTER(HostMode)(&eHostMode); H();
1983 if (eHostMode != PortMode_Disconnected)
1984 {
1985 InsertConfigNode(pInst, "LUN#0", &pLunL0);
1986 if (eHostMode == PortMode_HostPipe)
1987 {
1988 InsertConfigString(pLunL0, "Driver", "Char");
1989 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
1990 InsertConfigString(pLunL1, "Driver", "NamedPipe");
1991 InsertConfigNode(pLunL1, "Config", &pLunL2);
1992 InsertConfigString(pLunL2, "Location", bstr);
1993 InsertConfigInteger(pLunL2, "IsServer", fServer);
1994 }
1995 else if (eHostMode == PortMode_HostDevice)
1996 {
1997 InsertConfigString(pLunL0, "Driver", "Host Serial");
1998 InsertConfigNode(pLunL0, "Config", &pLunL1);
1999 InsertConfigString(pLunL1, "DevicePath", bstr);
2000 }
2001 else if (eHostMode == PortMode_RawFile)
2002 {
2003 InsertConfigString(pLunL0, "Driver", "Char");
2004 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
2005 InsertConfigString(pLunL1, "Driver", "RawFile");
2006 InsertConfigNode(pLunL1, "Config", &pLunL2);
2007 InsertConfigString(pLunL2, "Location", bstr);
2008 }
2009 }
2010 }
2011
2012 /*
2013 * Parallel (LPT) Ports
2014 */
2015 InsertConfigNode(pDevices, "parallel", &pDev);
2016 for (ULONG ulInstance = 0; ulInstance < SchemaDefs::ParallelPortCount; ++ulInstance)
2017 {
2018 ComPtr<IParallelPort> parallelPort;
2019 hrc = pMachine->GetParallelPort(ulInstance, parallelPort.asOutParam()); H();
2020 BOOL fEnabledParPort = FALSE;
2021 if (parallelPort)
2022 {
2023 hrc = parallelPort->COMGETTER(Enabled)(&fEnabledParPort); H();
2024 }
2025 if (!fEnabledParPort)
2026 continue;
2027
2028 InsertConfigNode(pDev, Utf8StrFmt("%u", ulInstance).c_str(), &pInst);
2029 InsertConfigNode(pInst, "Config", &pCfg);
2030
2031 ULONG ulIRQ;
2032 hrc = parallelPort->COMGETTER(IRQ)(&ulIRQ); H();
2033 InsertConfigInteger(pCfg, "IRQ", ulIRQ);
2034 ULONG ulIOBase;
2035 hrc = parallelPort->COMGETTER(IOBase)(&ulIOBase); H();
2036 InsertConfigInteger(pCfg, "IOBase", ulIOBase);
2037 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2038 InsertConfigString(pLunL0, "Driver", "HostParallel");
2039 InsertConfigNode(pLunL0, "Config", &pLunL1);
2040 hrc = parallelPort->COMGETTER(Path)(bstr.asOutParam()); H();
2041 InsertConfigString(pLunL1, "DevicePath", bstr);
2042 }
2043
2044 /*
2045 * VMM Device
2046 */
2047 InsertConfigNode(pDevices, "VMMDev", &pDev);
2048 InsertConfigNode(pDev, "0", &pInst);
2049 InsertConfigNode(pInst, "Config", &pCfg);
2050 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
2051 hrc = BusMgr->assignPCIDevice("VMMDev", pInst); H();
2052
2053 Bstr hwVersion;
2054 hrc = pMachine->COMGETTER(HardwareVersion)(hwVersion.asOutParam()); H();
2055 InsertConfigInteger(pCfg, "RamSize", cbRam);
2056 if (hwVersion.compare(Bstr("1").raw()) == 0) /* <= 2.0.x */
2057 InsertConfigInteger(pCfg, "HeapEnabled", 0);
2058 Bstr snapshotFolder;
2059 hrc = pMachine->COMGETTER(SnapshotFolder)(snapshotFolder.asOutParam()); H();
2060 InsertConfigString(pCfg, "GuestCoreDumpDir", snapshotFolder);
2061
2062 /* the VMM device's Main driver */
2063 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2064 InsertConfigString(pLunL0, "Driver", "HGCM");
2065 InsertConfigNode(pLunL0, "Config", &pCfg);
2066 InsertConfigInteger(pCfg, "Object", (uintptr_t)pVMMDev);
2067
2068 /*
2069 * Attach the status driver.
2070 */
2071 attachStatusDriver(pInst, &mapSharedFolderLed, 0, 0, NULL, NULL, 0);
2072
2073 /*
2074 * Audio Sniffer Device
2075 */
2076 InsertConfigNode(pDevices, "AudioSniffer", &pDev);
2077 InsertConfigNode(pDev, "0", &pInst);
2078 InsertConfigNode(pInst, "Config", &pCfg);
2079
2080 /* the Audio Sniffer device's Main driver */
2081 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2082 InsertConfigString(pLunL0, "Driver", "MainAudioSniffer");
2083 InsertConfigNode(pLunL0, "Config", &pCfg);
2084 AudioSniffer *pAudioSniffer = mAudioSniffer;
2085 InsertConfigInteger(pCfg, "Object", (uintptr_t)pAudioSniffer);
2086
2087 /*
2088 * AC'97 ICH / SoundBlaster16 audio / Intel HD Audio
2089 */
2090 BOOL fAudioEnabled = FALSE;
2091 ComPtr<IAudioAdapter> audioAdapter;
2092 hrc = pMachine->COMGETTER(AudioAdapter)(audioAdapter.asOutParam()); H();
2093 if (audioAdapter)
2094 hrc = audioAdapter->COMGETTER(Enabled)(&fAudioEnabled); H();
2095
2096 if (fAudioEnabled)
2097 {
2098 AudioControllerType_T audioController;
2099 hrc = audioAdapter->COMGETTER(AudioController)(&audioController); H();
2100 switch (audioController)
2101 {
2102 case AudioControllerType_AC97:
2103 {
2104 /* default: ICH AC97 */
2105 InsertConfigNode(pDevices, "ichac97", &pDev);
2106 InsertConfigNode(pDev, "0", &pInst);
2107 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
2108 hrc = BusMgr->assignPCIDevice("ichac97", pInst); H();
2109 InsertConfigNode(pInst, "Config", &pCfg);
2110 break;
2111 }
2112 case AudioControllerType_SB16:
2113 {
2114 /* legacy SoundBlaster16 */
2115 InsertConfigNode(pDevices, "sb16", &pDev);
2116 InsertConfigNode(pDev, "0", &pInst);
2117 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
2118 InsertConfigNode(pInst, "Config", &pCfg);
2119 InsertConfigInteger(pCfg, "IRQ", 5);
2120 InsertConfigInteger(pCfg, "DMA", 1);
2121 InsertConfigInteger(pCfg, "DMA16", 5);
2122 InsertConfigInteger(pCfg, "Port", 0x220);
2123 InsertConfigInteger(pCfg, "Version", 0x0405);
2124 break;
2125 }
2126 case AudioControllerType_HDA:
2127 {
2128 /* Intel HD Audio */
2129 InsertConfigNode(pDevices, "hda", &pDev);
2130 InsertConfigNode(pDev, "0", &pInst);
2131 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
2132 hrc = BusMgr->assignPCIDevice("hda", pInst); H();
2133 InsertConfigNode(pInst, "Config", &pCfg);
2134 }
2135 }
2136
2137 /* the Audio driver */
2138 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2139 InsertConfigString(pLunL0, "Driver", "AUDIO");
2140 InsertConfigNode(pLunL0, "Config", &pCfg);
2141
2142 AudioDriverType_T audioDriver;
2143 hrc = audioAdapter->COMGETTER(AudioDriver)(&audioDriver); H();
2144 switch (audioDriver)
2145 {
2146 case AudioDriverType_Null:
2147 {
2148 InsertConfigString(pCfg, "AudioDriver", "null");
2149 break;
2150 }
2151#ifdef RT_OS_WINDOWS
2152#ifdef VBOX_WITH_WINMM
2153 case AudioDriverType_WinMM:
2154 {
2155 InsertConfigString(pCfg, "AudioDriver", "winmm");
2156 break;
2157 }
2158#endif
2159 case AudioDriverType_DirectSound:
2160 {
2161 InsertConfigString(pCfg, "AudioDriver", "dsound");
2162 break;
2163 }
2164#endif /* RT_OS_WINDOWS */
2165#ifdef RT_OS_SOLARIS
2166 case AudioDriverType_SolAudio:
2167 {
2168 InsertConfigString(pCfg, "AudioDriver", "solaudio");
2169 break;
2170 }
2171#endif
2172#ifdef RT_OS_LINUX
2173# ifdef VBOX_WITH_ALSA
2174 case AudioDriverType_ALSA:
2175 {
2176 InsertConfigString(pCfg, "AudioDriver", "alsa");
2177 break;
2178 }
2179# endif
2180# ifdef VBOX_WITH_PULSE
2181 case AudioDriverType_Pulse:
2182 {
2183 InsertConfigString(pCfg, "AudioDriver", "pulse");
2184 break;
2185 }
2186# endif
2187#endif /* RT_OS_LINUX */
2188#if defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD) || defined(VBOX_WITH_SOLARIS_OSS)
2189 case AudioDriverType_OSS:
2190 {
2191 InsertConfigString(pCfg, "AudioDriver", "oss");
2192 break;
2193 }
2194#endif
2195#ifdef RT_OS_FREEBSD
2196# ifdef VBOX_WITH_PULSE
2197 case AudioDriverType_Pulse:
2198 {
2199 InsertConfigString(pCfg, "AudioDriver", "pulse");
2200 break;
2201 }
2202# endif
2203#endif
2204#ifdef RT_OS_DARWIN
2205 case AudioDriverType_CoreAudio:
2206 {
2207 InsertConfigString(pCfg, "AudioDriver", "coreaudio");
2208 break;
2209 }
2210#endif
2211 }
2212 hrc = pMachine->COMGETTER(Name)(bstr.asOutParam()); H();
2213 InsertConfigString(pCfg, "StreamName", bstr);
2214 }
2215
2216 /*
2217 * The USB Controller.
2218 */
2219 ComPtr<IUSBController> USBCtlPtr;
2220 hrc = pMachine->COMGETTER(USBController)(USBCtlPtr.asOutParam());
2221 if (USBCtlPtr)
2222 {
2223 BOOL fOhciEnabled;
2224 hrc = USBCtlPtr->COMGETTER(Enabled)(&fOhciEnabled); H();
2225 if (fOhciEnabled)
2226 {
2227 InsertConfigNode(pDevices, "usb-ohci", &pDev);
2228 InsertConfigNode(pDev, "0", &pInst);
2229 InsertConfigNode(pInst, "Config", &pCfg);
2230 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
2231 hrc = BusMgr->assignPCIDevice("usb-ohci", pInst); H();
2232 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2233 InsertConfigString(pLunL0, "Driver", "VUSBRootHub");
2234 InsertConfigNode(pLunL0, "Config", &pCfg);
2235
2236 /*
2237 * Attach the status driver.
2238 */
2239 attachStatusDriver(pInst, &mapUSBLed[0], 0, 0, NULL, NULL, 0);
2240
2241#ifdef VBOX_WITH_EHCI
2242 BOOL fEHCIEnabled;
2243 hrc = USBCtlPtr->COMGETTER(EnabledEHCI)(&fEHCIEnabled); H();
2244 if (fEHCIEnabled)
2245 {
2246 /*
2247 * USB 2.0 is only available if the proper ExtPack is installed.
2248 *
2249 * Note. Configuring EHCI here and providing messages about
2250 * the missing extpack isn't exactly clean, but it is a
2251 * necessary evil to patch over legacy compatability issues
2252 * introduced by the new distribution model.
2253 */
2254 static const char *s_pszUsbExtPackName = "Oracle VM VirtualBox Extension Pack";
2255# ifdef VBOX_WITH_EXTPACK
2256 if (mptrExtPackManager->isExtPackUsable(s_pszUsbExtPackName))
2257# endif
2258 {
2259 InsertConfigNode(pDevices, "usb-ehci", &pDev);
2260 InsertConfigNode(pDev, "0", &pInst);
2261 InsertConfigNode(pInst, "Config", &pCfg);
2262 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
2263 hrc = BusMgr->assignPCIDevice("usb-ehci", pInst); H();
2264
2265 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2266 InsertConfigString(pLunL0, "Driver", "VUSBRootHub");
2267 InsertConfigNode(pLunL0, "Config", &pCfg);
2268
2269 /*
2270 * Attach the status driver.
2271 */
2272 attachStatusDriver(pInst, &mapUSBLed[1], 0, 0, NULL, NULL, 0);
2273 }
2274# ifdef VBOX_WITH_EXTPACK
2275 else
2276 {
2277 /* Always fatal! Up to VBox 4.0.4 we allowed to start the VM anyway
2278 * but this induced problems when the user saved + restored the VM! */
2279 return VMSetError(pVM, VERR_NOT_FOUND, RT_SRC_POS,
2280 N_("Implementation of the USB 2.0 controller not found!\n"
2281 "Because the USB 2.0 controller state is part of the saved "
2282 "VM state, the VM cannot be started. To fix "
2283 "this problem, either install the '%s' or disable USB 2.0 "
2284 "support in the VM settings"),
2285 s_pszUsbExtPackName);
2286 }
2287# endif
2288 }
2289#endif
2290
2291 /*
2292 * Virtual USB Devices.
2293 */
2294 PCFGMNODE pUsbDevices = NULL;
2295 InsertConfigNode(pRoot, "USB", &pUsbDevices);
2296
2297#ifdef VBOX_WITH_USB
2298 {
2299 /*
2300 * Global USB options, currently unused as we'll apply the 2.0 -> 1.1 morphing
2301 * on a per device level now.
2302 */
2303 InsertConfigNode(pUsbDevices, "USBProxy", &pCfg);
2304 InsertConfigNode(pCfg, "GlobalConfig", &pCfg);
2305 // This globally enables the 2.0 -> 1.1 device morphing of proxied devices to keep windows quiet.
2306 //InsertConfigInteger(pCfg, "Force11Device", true);
2307 // The following breaks stuff, but it makes MSDs work in vista. (I include it here so
2308 // that it's documented somewhere.) Users needing it can use:
2309 // VBoxManage setextradata "myvm" "VBoxInternal/USB/USBProxy/GlobalConfig/Force11PacketSize" 1
2310 //InsertConfigInteger(pCfg, "Force11PacketSize", true);
2311 }
2312#endif
2313
2314#ifdef VBOX_WITH_USB_VIDEO
2315
2316 InsertConfigNode(pUsbDevices, "Webcam", &pDev);
2317 InsertConfigNode(pDev, "0", &pInst);
2318 InsertConfigNode(pInst, "Config", &pCfg);
2319# if 0 /* Experiments with attaching */
2320 InsertConfigInteger(pCfg, "USBVER", RT_BIT(2));
2321# endif
2322 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2323# ifdef VBOX_WITH_USB_VIDEO_TEST
2324 InsertConfigString(pLunL0, "Driver", "WebcamFileFeeder");
2325 InsertConfigNode(pLunL0, "Config", &pCfg);
2326 InsertConfigString(pCfg, "DirToFeed", "out");
2327# else
2328 InsertConfigString(pLunL0, "Driver", "UsbWebcamInterface");
2329 InsertConfigNode(pLunL0, "Config", &pCfg);
2330 InsertConfigInteger(pCfg, "Object", mUsbWebcamInterface);
2331# endif
2332#endif
2333#ifdef VBOX_WITH_USB_CARDREADER
2334 BOOL aEmulatedUSBCardReaderEnabled = FALSE;
2335 hrc = pMachine->COMGETTER(EmulatedUSBCardReaderEnabled)(&aEmulatedUSBCardReaderEnabled); H();
2336 if (aEmulatedUSBCardReaderEnabled)
2337 {
2338 InsertConfigNode(pUsbDevices, "CardReader", &pDev);
2339 InsertConfigNode(pDev, "0", &pInst);
2340 InsertConfigNode(pInst, "Config", &pCfg);
2341
2342 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2343# ifdef VBOX_WITH_USB_CARDREADER_TEST
2344 InsertConfigString(pLunL0, "Driver", "DrvDirectCardReader");
2345 InsertConfigNode(pLunL0, "Config", &pCfg);
2346# else
2347 InsertConfigString(pLunL0, "Driver", "UsbCardReader");
2348 InsertConfigNode(pLunL0, "Config", &pCfg);
2349 InsertConfigInteger(pCfg, "Object", (uintptr_t)mUsbCardReader);
2350# endif
2351 }
2352#endif
2353
2354# if 0 /* Virtual MSD*/
2355
2356 InsertConfigNode(pUsbDevices, "Msd", &pDev);
2357 InsertConfigNode(pDev, "0", &pInst);
2358 InsertConfigNode(pInst, "Config", &pCfg);
2359 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2360
2361 InsertConfigString(pLunL0, "Driver", "SCSI");
2362 InsertConfigNode(pLunL0, "Config", &pCfg);
2363
2364 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
2365 InsertConfigString(pLunL1, "Driver", "Block");
2366 InsertConfigNode(pLunL1, "Config", &pCfg);
2367 InsertConfigString(pCfg, "Type", "HardDisk");
2368 InsertConfigInteger(pCfg, "Mountable", 0);
2369
2370 InsertConfigNode(pLunL1, "AttachedDriver", &pLunL2);
2371 InsertConfigString(pLunL2, "Driver", "VD");
2372 InsertConfigNode(pLunL2, "Config", &pCfg);
2373 InsertConfigString(pCfg, "Path", "/Volumes/DataHFS/bird/VDIs/linux.vdi");
2374 InsertConfigString(pCfg, "Format", "VDI");
2375# endif
2376
2377 /* Virtual USB Mouse/Tablet */
2378 PointingHIDType_T aPointingHID;
2379 hrc = pMachine->COMGETTER(PointingHIDType)(&aPointingHID); H();
2380 if (aPointingHID == PointingHIDType_USBMouse || aPointingHID == PointingHIDType_USBTablet)
2381 {
2382 InsertConfigNode(pUsbDevices, "HidMouse", &pDev);
2383 InsertConfigNode(pDev, "0", &pInst);
2384 InsertConfigNode(pInst, "Config", &pCfg);
2385
2386 if (aPointingHID == PointingHIDType_USBTablet)
2387 {
2388 InsertConfigInteger(pCfg, "Absolute", 1);
2389 }
2390 else
2391 {
2392 InsertConfigInteger(pCfg, "Absolute", 0);
2393 }
2394 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2395 InsertConfigString(pLunL0, "Driver", "MouseQueue");
2396 InsertConfigNode(pLunL0, "Config", &pCfg);
2397 InsertConfigInteger(pCfg, "QueueSize", 128);
2398
2399 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
2400 InsertConfigString(pLunL1, "Driver", "MainMouse");
2401 InsertConfigNode(pLunL1, "Config", &pCfg);
2402 pMouse = mMouse;
2403 InsertConfigInteger(pCfg, "Object", (uintptr_t)pMouse);
2404 }
2405
2406 /* Virtual USB Keyboard */
2407 KeyboardHIDType_T aKbdHID;
2408 hrc = pMachine->COMGETTER(KeyboardHIDType)(&aKbdHID); H();
2409 if (aKbdHID == KeyboardHIDType_USBKeyboard)
2410 {
2411 InsertConfigNode(pUsbDevices, "HidKeyboard", &pDev);
2412 InsertConfigNode(pDev, "0", &pInst);
2413 InsertConfigNode(pInst, "Config", &pCfg);
2414
2415 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2416 InsertConfigString(pLunL0, "Driver", "KeyboardQueue");
2417 InsertConfigNode(pLunL0, "Config", &pCfg);
2418 InsertConfigInteger(pCfg, "QueueSize", 64);
2419
2420 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
2421 InsertConfigString(pLunL1, "Driver", "MainKeyboard");
2422 InsertConfigNode(pLunL1, "Config", &pCfg);
2423 pKeyboard = mKeyboard;
2424 InsertConfigInteger(pCfg, "Object", (uintptr_t)pKeyboard);
2425 }
2426 }
2427 }
2428
2429 /*
2430 * Clipboard
2431 */
2432 {
2433 ClipboardMode_T mode = ClipboardMode_Disabled;
2434 hrc = pMachine->COMGETTER(ClipboardMode)(&mode); H();
2435
2436 if (/* mode != ClipboardMode_Disabled */ true)
2437 {
2438 /* Load the service */
2439 rc = pVMMDev->hgcmLoadService("VBoxSharedClipboard", "VBoxSharedClipboard");
2440
2441 if (RT_FAILURE(rc))
2442 {
2443 LogRel(("VBoxSharedClipboard is not available. rc = %Rrc\n", rc));
2444 /* That is not a fatal failure. */
2445 rc = VINF_SUCCESS;
2446 }
2447 else
2448 {
2449 changeClipboardMode(mode);
2450
2451 /* Setup the service. */
2452 VBOXHGCMSVCPARM parm;
2453 parm.type = VBOX_HGCM_SVC_PARM_32BIT;
2454 parm.setUInt32(!useHostClipboard());
2455 pVMMDev->hgcmHostCall("VBoxSharedClipboard", VBOX_SHARED_CLIPBOARD_HOST_FN_SET_HEADLESS, 1, &parm);
2456
2457 Log(("Set VBoxSharedClipboard mode\n"));
2458 }
2459 }
2460 }
2461
2462#ifdef VBOX_WITH_DRAG_AND_DROP
2463 /*
2464 * Drag & Drop
2465 */
2466 {
2467 DragAndDropMode_T mode = DragAndDropMode_Disabled;
2468 hrc = pMachine->COMGETTER(DragAndDropMode)(&mode); H();
2469
2470 /* Load the service */
2471 rc = pVMMDev->hgcmLoadService("VBoxDragAndDropSvc", "VBoxDragAndDropSvc");
2472
2473 if (RT_FAILURE(rc))
2474 {
2475 LogRel(("VBoxDragAndDropService is not available. rc = %Rrc\n", rc));
2476 /* That is not a fatal failure. */
2477 rc = VINF_SUCCESS;
2478 }
2479 else
2480 {
2481 HGCMSVCEXTHANDLE hDummy;
2482 rc = HGCMHostRegisterServiceExtension(&hDummy, "VBoxDragAndDropSvc",
2483 &GuestDnD::notifyGuestDragAndDropEvent,
2484 getGuest());
2485 if (RT_FAILURE(rc))
2486 Log(("Cannot register VBoxDragAndDropSvc extension!\n"));
2487 else
2488 {
2489 changeDragAndDropMode(mode);
2490 Log(("VBoxDragAndDropSvc loaded\n"));
2491 }
2492 }
2493 }
2494#endif /* VBOX_WITH_DRAG_AND_DROP */
2495
2496#ifdef VBOX_WITH_CROGL
2497 /*
2498 * crOpenGL
2499 */
2500 {
2501 BOOL fEnabled3D = false;
2502 hrc = pMachine->COMGETTER(Accelerate3DEnabled)(&fEnabled3D); H();
2503
2504 if (fEnabled3D)
2505 {
2506 BOOL fSupports3D = VBoxOglIs3DAccelerationSupported();
2507 if (!fSupports3D)
2508 return VMSetError(pVM, VERR_NOT_AVAILABLE, RT_SRC_POS,
2509 N_("This VM was configured to use 3D acceleration. However, the "
2510 "3D support of the host is not working properly and the "
2511 "VM cannot be started. To fix this problem, either "
2512 "fix the host 3D support (update the host graphics driver?) "
2513 "or disable 3D acceleration in the VM settings"));
2514
2515 /* Load the service */
2516 rc = pVMMDev->hgcmLoadService("VBoxSharedCrOpenGL", "VBoxSharedCrOpenGL");
2517 if (RT_FAILURE(rc))
2518 {
2519 LogRel(("Failed to load Shared OpenGL service %Rrc\n", rc));
2520 /* That is not a fatal failure. */
2521 rc = VINF_SUCCESS;
2522 }
2523 else
2524 {
2525 LogRel(("Shared crOpenGL service loaded.\n"));
2526
2527 /* Setup the service. */
2528 VBOXHGCMSVCPARM parm;
2529 parm.type = VBOX_HGCM_SVC_PARM_PTR;
2530
2531 parm.u.pointer.addr = (IConsole *)(Console *)this;
2532 parm.u.pointer.size = sizeof(IConsole *);
2533
2534 rc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_SET_CONSOLE, SHCRGL_CPARMS_SET_CONSOLE, &parm);
2535 if (!RT_SUCCESS(rc))
2536 AssertMsgFailed(("SHCRGL_HOST_FN_SET_CONSOLE failed with %Rrc\n", rc));
2537
2538 parm.u.pointer.addr = pVM;
2539 parm.u.pointer.size = sizeof(pVM);
2540 rc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_SET_VM, SHCRGL_CPARMS_SET_VM, &parm);
2541 if (!RT_SUCCESS(rc))
2542 AssertMsgFailed(("SHCRGL_HOST_FN_SET_VM failed with %Rrc\n", rc));
2543 }
2544
2545 }
2546 }
2547#endif
2548
2549#ifdef VBOX_WITH_GUEST_PROPS
2550 /*
2551 * Guest property service
2552 */
2553
2554 rc = configGuestProperties(this, pVM);
2555#endif /* VBOX_WITH_GUEST_PROPS defined */
2556
2557#ifdef VBOX_WITH_GUEST_CONTROL
2558 /*
2559 * Guest control service
2560 */
2561
2562 rc = configGuestControl(this);
2563#endif /* VBOX_WITH_GUEST_CONTROL defined */
2564
2565 /*
2566 * ACPI
2567 */
2568 BOOL fACPI;
2569 hrc = biosSettings->COMGETTER(ACPIEnabled)(&fACPI); H();
2570 if (fACPI)
2571 {
2572 BOOL fCpuHotPlug = false;
2573 BOOL fShowCpu = fOsXGuest;
2574 /* Always show the CPU leafs when we have multiple VCPUs or when the IO-APIC is enabled.
2575 * The Windows SMP kernel needs a CPU leaf or else its idle loop will burn cpu cycles; the
2576 * intelppm driver refuses to register an idle state handler.
2577 */
2578 if ((cCpus > 1) || fIOAPIC)
2579 fShowCpu = true;
2580
2581 hrc = pMachine->COMGETTER(CPUHotPlugEnabled)(&fCpuHotPlug); H();
2582
2583 InsertConfigNode(pDevices, "acpi", &pDev);
2584 InsertConfigNode(pDev, "0", &pInst);
2585 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
2586 InsertConfigNode(pInst, "Config", &pCfg);
2587 hrc = BusMgr->assignPCIDevice("acpi", pInst); H();
2588
2589 InsertConfigInteger(pCfg, "RamSize", cbRam);
2590 InsertConfigInteger(pCfg, "RamHoleSize", cbRamHole);
2591 InsertConfigInteger(pCfg, "NumCPUs", cCpus);
2592
2593 InsertConfigInteger(pCfg, "IOAPIC", fIOAPIC);
2594 InsertConfigInteger(pCfg, "FdcEnabled", fFdcEnabled);
2595 InsertConfigInteger(pCfg, "HpetEnabled", fHPETEnabled);
2596 InsertConfigInteger(pCfg, "SmcEnabled", fSmcEnabled);
2597 InsertConfigInteger(pCfg, "ShowRtc", fShowRtc);
2598 if (fOsXGuest && !llBootNics.empty())
2599 {
2600 BootNic aNic = llBootNics.front();
2601 uint32_t u32NicPCIAddr = (aNic.mPCIAddress.miDevice << 16) | aNic.mPCIAddress.miFn;
2602 InsertConfigInteger(pCfg, "NicPciAddress", u32NicPCIAddr);
2603 }
2604 if (fOsXGuest && fAudioEnabled)
2605 {
2606 PCIBusAddress Address;
2607 if (BusMgr->findPCIAddress("hda", 0, Address))
2608 {
2609 uint32_t u32AudioPCIAddr = (Address.miDevice << 16) | Address.miFn;
2610 InsertConfigInteger(pCfg, "AudioPciAddress", u32AudioPCIAddr);
2611 }
2612 }
2613 InsertConfigInteger(pCfg, "IocPciAddress", uIocPCIAddress);
2614 if (chipsetType == ChipsetType_ICH9)
2615 {
2616 InsertConfigInteger(pCfg, "McfgBase", uMcfgBase);
2617 InsertConfigInteger(pCfg, "McfgLength", cbMcfgLength);
2618 }
2619 InsertConfigInteger(pCfg, "HostBusPciAddress", uHbcPCIAddress);
2620 InsertConfigInteger(pCfg, "ShowCpu", fShowCpu);
2621 InsertConfigInteger(pCfg, "CpuHotPlug", fCpuHotPlug);
2622
2623 InsertConfigInteger(pCfg, "Serial0IoPortBase", auSerialIoPortBase[0]);
2624 InsertConfigInteger(pCfg, "Serial0Irq", auSerialIrq[0]);
2625
2626 InsertConfigInteger(pCfg, "Serial1IoPortBase", auSerialIoPortBase[1]);
2627 InsertConfigInteger(pCfg, "Serial1Irq", auSerialIrq[1]);
2628
2629 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2630 InsertConfigString(pLunL0, "Driver", "ACPIHost");
2631 InsertConfigNode(pLunL0, "Config", &pCfg);
2632
2633 /* Attach the dummy CPU drivers */
2634 for (ULONG iCpuCurr = 1; iCpuCurr < cCpus; iCpuCurr++)
2635 {
2636 BOOL fCpuAttached = true;
2637
2638 if (fCpuHotPlug)
2639 {
2640 hrc = pMachine->GetCPUStatus(iCpuCurr, &fCpuAttached); H();
2641 }
2642
2643 if (fCpuAttached)
2644 {
2645 InsertConfigNode(pInst, Utf8StrFmt("LUN#%u", iCpuCurr).c_str(), &pLunL0);
2646 InsertConfigString(pLunL0, "Driver", "ACPICpu");
2647 InsertConfigNode(pLunL0, "Config", &pCfg);
2648 }
2649 }
2650 }
2651
2652 /*
2653 * Configure DBGF (Debug(ger) Facility).
2654 */
2655 {
2656 PCFGMNODE pDbgf;
2657 InsertConfigNode(pRoot, "DBGF", &pDbgf);
2658
2659 /* Paths to search for debug info and such things. */
2660 hrc = pMachine->COMGETTER(SettingsFilePath)(bstr.asOutParam()); H();
2661 Utf8Str strSettingsPath(bstr);
2662 bstr.setNull();
2663 strSettingsPath.stripFilename();
2664
2665 char szHomeDir[RTPATH_MAX];
2666 rc = RTPathUserHome(szHomeDir, sizeof(szHomeDir));
2667 if (RT_FAILURE(rc))
2668 szHomeDir[0] = '\0';
2669
2670 Utf8Str strPath;
2671 strPath.append(strSettingsPath).append("/debug/;");
2672 strPath.append(strSettingsPath).append("/;");
2673 strPath.append(szHomeDir).append("/");
2674
2675 InsertConfigString(pDbgf, "Path", strPath.c_str());
2676
2677 /* Tracing configuration. */
2678 BOOL fTracingEnabled;
2679 hrc = pMachine->COMGETTER(TracingEnabled)(&fTracingEnabled); H();
2680 if (fTracingEnabled)
2681 InsertConfigInteger(pDbgf, "TracingEnabled", 1);
2682
2683 hrc = pMachine->COMGETTER(TracingConfig)(bstr.asOutParam()); H();
2684 if (fTracingEnabled)
2685 InsertConfigString(pDbgf, "TracingConfig", bstr);
2686
2687 BOOL fAllowTracingToAccessVM;
2688 hrc = pMachine->COMGETTER(AllowTracingToAccessVM)(&fAllowTracingToAccessVM); H();
2689 if (fAllowTracingToAccessVM)
2690 InsertConfigInteger(pPDM, "AllowTracingToAccessVM", 1);
2691 }
2692 }
2693 catch (ConfigError &x)
2694 {
2695 // InsertConfig threw something:
2696 return x.m_vrc;
2697 }
2698
2699#ifdef VBOX_WITH_EXTPACK
2700 /*
2701 * Call the extension pack hooks if everything went well thus far.
2702 */
2703 if (RT_SUCCESS(rc))
2704 {
2705 pAlock->release();
2706 rc = mptrExtPackManager->callAllVmConfigureVmmHooks(this, pVM);
2707 pAlock->acquire();
2708 }
2709#endif
2710
2711 /*
2712 * Apply the CFGM overlay.
2713 */
2714 if (RT_SUCCESS(rc))
2715 rc = configCfgmOverlay(pVM, virtualBox, pMachine);
2716
2717#undef H
2718
2719 pAlock->release(); /* Avoid triggering the lock order inversion check. */
2720
2721 /*
2722 * Register VM state change handler.
2723 */
2724 int rc2 = VMR3AtStateRegister(pVM, Console::vmstateChangeCallback, this);
2725 AssertRC(rc2);
2726 if (RT_SUCCESS(rc))
2727 rc = rc2;
2728
2729 /*
2730 * Register VM runtime error handler.
2731 */
2732 rc2 = VMR3AtRuntimeErrorRegister(pVM, Console::setVMRuntimeErrorCallback, this);
2733 AssertRC(rc2);
2734 if (RT_SUCCESS(rc))
2735 rc = rc2;
2736
2737 pAlock->acquire();
2738
2739 LogFlowFunc(("vrc = %Rrc\n", rc));
2740 LogFlowFuncLeave();
2741
2742 return rc;
2743}
2744
2745/**
2746 * Applies the CFGM overlay as specified by /VBoxInternal/XXX extra data
2747 * values.
2748 *
2749 * @returns VBox status code.
2750 * @param pVM The VM handle.
2751 * @param pVirtualBox Pointer to the IVirtualBox interface.
2752 * @param pMachine Pointer to the IMachine interface.
2753 */
2754/* static */
2755int Console::configCfgmOverlay(PVM pVM, IVirtualBox *pVirtualBox, IMachine *pMachine)
2756{
2757 /*
2758 * CFGM overlay handling.
2759 *
2760 * Here we check the extra data entries for CFGM values
2761 * and create the nodes and insert the values on the fly. Existing
2762 * values will be removed and reinserted. CFGM is typed, so by default
2763 * we will guess whether it's a string or an integer (byte arrays are
2764 * not currently supported). It's possible to override this autodetection
2765 * by adding "string:", "integer:" or "bytes:" (future).
2766 *
2767 * We first perform a run on global extra data, then on the machine
2768 * extra data to support global settings with local overrides.
2769 */
2770 PCFGMNODE pRoot = CFGMR3GetRoot(pVM);
2771 int rc = VINF_SUCCESS;
2772 try
2773 {
2774 /** @todo add support for removing nodes and byte blobs. */
2775 /*
2776 * Get the next key
2777 */
2778 SafeArray<BSTR> aGlobalExtraDataKeys;
2779 SafeArray<BSTR> aMachineExtraDataKeys;
2780 HRESULT hrc = pVirtualBox->GetExtraDataKeys(ComSafeArrayAsOutParam(aGlobalExtraDataKeys));
2781 AssertMsg(SUCCEEDED(hrc), ("VirtualBox::GetExtraDataKeys failed with %Rhrc\n", hrc));
2782
2783 // remember the no. of global values so we can call the correct method below
2784 size_t cGlobalValues = aGlobalExtraDataKeys.size();
2785
2786 hrc = pMachine->GetExtraDataKeys(ComSafeArrayAsOutParam(aMachineExtraDataKeys));
2787 AssertMsg(SUCCEEDED(hrc), ("VirtualBox::GetExtraDataKeys failed with %Rhrc\n", hrc));
2788
2789 // build a combined list from global keys...
2790 std::list<Utf8Str> llExtraDataKeys;
2791
2792 for (size_t i = 0; i < aGlobalExtraDataKeys.size(); ++i)
2793 llExtraDataKeys.push_back(Utf8Str(aGlobalExtraDataKeys[i]));
2794 // ... and machine keys
2795 for (size_t i = 0; i < aMachineExtraDataKeys.size(); ++i)
2796 llExtraDataKeys.push_back(Utf8Str(aMachineExtraDataKeys[i]));
2797
2798 size_t i2 = 0;
2799 for (std::list<Utf8Str>::const_iterator it = llExtraDataKeys.begin();
2800 it != llExtraDataKeys.end();
2801 ++it, ++i2)
2802 {
2803 const Utf8Str &strKey = *it;
2804
2805 /*
2806 * We only care about keys starting with "VBoxInternal/" (skip "G:" or "M:")
2807 */
2808 if (!strKey.startsWith("VBoxInternal/"))
2809 continue;
2810
2811 const char *pszExtraDataKey = strKey.c_str() + sizeof("VBoxInternal/") - 1;
2812
2813 // get the value
2814 Bstr bstrExtraDataValue;
2815 if (i2 < cGlobalValues)
2816 // this is still one of the global values:
2817 hrc = pVirtualBox->GetExtraData(Bstr(strKey).raw(),
2818 bstrExtraDataValue.asOutParam());
2819 else
2820 hrc = pMachine->GetExtraData(Bstr(strKey).raw(),
2821 bstrExtraDataValue.asOutParam());
2822 if (FAILED(hrc))
2823 LogRel(("Warning: Cannot get extra data key %s, rc = %Rrc\n", strKey.c_str(), hrc));
2824
2825 /*
2826 * The key will be in the format "Node1/Node2/Value" or simply "Value".
2827 * Split the two and get the node, delete the value and create the node
2828 * if necessary.
2829 */
2830 PCFGMNODE pNode;
2831 const char *pszCFGMValueName = strrchr(pszExtraDataKey, '/');
2832 if (pszCFGMValueName)
2833 {
2834 /* terminate the node and advance to the value (Utf8Str might not
2835 offically like this but wtf) */
2836 *(char*)pszCFGMValueName = '\0';
2837 ++pszCFGMValueName;
2838
2839 /* does the node already exist? */
2840 pNode = CFGMR3GetChild(pRoot, pszExtraDataKey);
2841 if (pNode)
2842 CFGMR3RemoveValue(pNode, pszCFGMValueName);
2843 else
2844 {
2845 /* create the node */
2846 rc = CFGMR3InsertNode(pRoot, pszExtraDataKey, &pNode);
2847 if (RT_FAILURE(rc))
2848 {
2849 AssertLogRelMsgRC(rc, ("failed to insert node '%s'\n", pszExtraDataKey));
2850 continue;
2851 }
2852 Assert(pNode);
2853 }
2854 }
2855 else
2856 {
2857 /* root value (no node path). */
2858 pNode = pRoot;
2859 pszCFGMValueName = pszExtraDataKey;
2860 pszExtraDataKey--;
2861 CFGMR3RemoveValue(pNode, pszCFGMValueName);
2862 }
2863
2864 /*
2865 * Now let's have a look at the value.
2866 * Empty strings means that we should remove the value, which we've
2867 * already done above.
2868 */
2869 Utf8Str strCFGMValueUtf8(bstrExtraDataValue);
2870 if (!strCFGMValueUtf8.isEmpty())
2871 {
2872 uint64_t u64Value;
2873
2874 /* check for type prefix first. */
2875 if (!strncmp(strCFGMValueUtf8.c_str(), "string:", sizeof("string:") - 1))
2876 InsertConfigString(pNode, pszCFGMValueName, strCFGMValueUtf8.c_str() + sizeof("string:") - 1);
2877 else if (!strncmp(strCFGMValueUtf8.c_str(), "integer:", sizeof("integer:") - 1))
2878 {
2879 rc = RTStrToUInt64Full(strCFGMValueUtf8.c_str() + sizeof("integer:") - 1, 0, &u64Value);
2880 if (RT_SUCCESS(rc))
2881 rc = CFGMR3InsertInteger(pNode, pszCFGMValueName, u64Value);
2882 }
2883 else if (!strncmp(strCFGMValueUtf8.c_str(), "bytes:", sizeof("bytes:") - 1))
2884 rc = VERR_NOT_IMPLEMENTED;
2885 /* auto detect type. */
2886 else if (RT_SUCCESS(RTStrToUInt64Full(strCFGMValueUtf8.c_str(), 0, &u64Value)))
2887 rc = CFGMR3InsertInteger(pNode, pszCFGMValueName, u64Value);
2888 else
2889 InsertConfigString(pNode, pszCFGMValueName, strCFGMValueUtf8);
2890 AssertLogRelMsgRCBreak(rc, ("failed to insert CFGM value '%s' to key '%s'\n", strCFGMValueUtf8.c_str(), pszExtraDataKey));
2891 }
2892 }
2893 }
2894 catch (ConfigError &x)
2895 {
2896 // InsertConfig threw something:
2897 return x.m_vrc;
2898 }
2899 return rc;
2900}
2901
2902/**
2903 * Ellipsis to va_list wrapper for calling setVMRuntimeErrorCallback.
2904 */
2905/*static*/
2906void Console::setVMRuntimeErrorCallbackF(PVM pVM, void *pvConsole, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, ...)
2907{
2908 va_list va;
2909 va_start(va, pszFormat);
2910 setVMRuntimeErrorCallback(pVM, pvConsole, fFlags, pszErrorId, pszFormat, va);
2911 va_end(va);
2912}
2913
2914/* XXX introduce RT format specifier */
2915static uint64_t formatDiskSize(uint64_t u64Size, const char **pszUnit)
2916{
2917 if (u64Size > INT64_C(5000)*_1G)
2918 {
2919 *pszUnit = "TB";
2920 return u64Size / _1T;
2921 }
2922 else if (u64Size > INT64_C(5000)*_1M)
2923 {
2924 *pszUnit = "GB";
2925 return u64Size / _1G;
2926 }
2927 else
2928 {
2929 *pszUnit = "MB";
2930 return u64Size / _1M;
2931 }
2932}
2933
2934int Console::configMediumAttachment(PCFGMNODE pCtlInst,
2935 const char *pcszDevice,
2936 unsigned uInstance,
2937 StorageBus_T enmBus,
2938 bool fUseHostIOCache,
2939 bool fBuiltinIOCache,
2940 bool fSetupMerge,
2941 unsigned uMergeSource,
2942 unsigned uMergeTarget,
2943 IMediumAttachment *pMediumAtt,
2944 MachineState_T aMachineState,
2945 HRESULT *phrc,
2946 bool fAttachDetach,
2947 bool fForceUnmount,
2948 bool fHotplug,
2949 PVM pVM,
2950 DeviceType_T *paLedDevType)
2951{
2952 // InsertConfig* throws
2953 try
2954 {
2955 int rc = VINF_SUCCESS;
2956 HRESULT hrc;
2957 Bstr bstr;
2958
2959// #define RC_CHECK() AssertMsgReturn(RT_SUCCESS(rc), ("rc=%Rrc\n", rc), rc)
2960#define H() AssertMsgReturn(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), VERR_GENERAL_FAILURE)
2961
2962 LONG lDev;
2963 hrc = pMediumAtt->COMGETTER(Device)(&lDev); H();
2964 LONG lPort;
2965 hrc = pMediumAtt->COMGETTER(Port)(&lPort); H();
2966 DeviceType_T lType;
2967 hrc = pMediumAtt->COMGETTER(Type)(&lType); H();
2968 BOOL fNonRotational;
2969 hrc = pMediumAtt->COMGETTER(NonRotational)(&fNonRotational); H();
2970 BOOL fDiscard;
2971 hrc = pMediumAtt->COMGETTER(Discard)(&fDiscard); H();
2972
2973 unsigned uLUN;
2974 PCFGMNODE pLunL0 = NULL;
2975 hrc = Console::convertBusPortDeviceToLun(enmBus, lPort, lDev, uLUN); H();
2976
2977 /* First check if the LUN already exists. */
2978 pLunL0 = CFGMR3GetChildF(pCtlInst, "LUN#%u", uLUN);
2979 if (pLunL0)
2980 {
2981 if (fAttachDetach)
2982 {
2983 if (lType != DeviceType_HardDisk)
2984 {
2985 /* Unmount existing media only for floppy and DVD drives. */
2986 PPDMIBASE pBase;
2987 rc = PDMR3QueryLun(pVM, pcszDevice, uInstance, uLUN, &pBase);
2988 if (RT_FAILURE(rc))
2989 {
2990 if (rc == VERR_PDM_LUN_NOT_FOUND || rc == VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN)
2991 rc = VINF_SUCCESS;
2992 AssertRC(rc);
2993 }
2994 else
2995 {
2996 PPDMIMOUNT pIMount = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMOUNT);
2997 AssertReturn(pIMount, VERR_INVALID_POINTER);
2998
2999 /* Unmount the media (but do not eject the medium!) */
3000 rc = pIMount->pfnUnmount(pIMount, fForceUnmount, false /*=fEject*/);
3001 if (rc == VERR_PDM_MEDIA_NOT_MOUNTED)
3002 rc = VINF_SUCCESS;
3003 /* for example if the medium is locked */
3004 else if (RT_FAILURE(rc))
3005 return rc;
3006 }
3007 }
3008
3009 rc = PDMR3DeviceDetach(pVM, pcszDevice, uInstance, uLUN, fHotplug ? 0 : PDM_TACH_FLAGS_NOT_HOT_PLUG);
3010 if (rc == VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN)
3011 rc = VINF_SUCCESS;
3012 AssertRCReturn(rc, rc);
3013
3014 CFGMR3RemoveNode(pLunL0);
3015 }
3016 else
3017 AssertFailedReturn(VERR_INTERNAL_ERROR);
3018 }
3019
3020 InsertConfigNode(pCtlInst, Utf8StrFmt("LUN#%u", uLUN).c_str(), &pLunL0);
3021
3022 PCFGMNODE pCfg = CFGMR3GetChild(pCtlInst, "Config");
3023 if (pCfg)
3024 {
3025 if (!strcmp(pcszDevice, "piix3ide"))
3026 {
3027 PCFGMNODE pDrive = CFGMR3GetChild(pCfg, g_apszIDEDrives[uLUN]);
3028 if (!pDrive)
3029 InsertConfigNode(pCfg, g_apszIDEDrives[uLUN], &pDrive);
3030 /* Don't use the RemoveConfigValue wrapper above, as we don't
3031 * know if the leaf is present or not. */
3032 CFGMR3RemoveValue(pDrive, "NonRotationalMedium");
3033 InsertConfigInteger(pDrive, "NonRotationalMedium", !!fNonRotational);
3034 }
3035 else if (!strcmp(pcszDevice, "ahci"))
3036 {
3037 Utf8Str strPort = Utf8StrFmt("Port%u", uLUN);
3038 PCFGMNODE pDrive = CFGMR3GetChild(pCfg, strPort.c_str());
3039 if (!pDrive)
3040 InsertConfigNode(pCfg, strPort.c_str(), &pDrive);
3041 /* Don't use the RemoveConfigValue wrapper above, as we don't
3042 * know if the leaf is present or not. */
3043 CFGMR3RemoveValue(pDrive, "NonRotationalMedium");
3044 InsertConfigInteger(pDrive, "NonRotationalMedium", !!fNonRotational);
3045 }
3046 }
3047
3048 Utf8Str devicePath = Utf8StrFmt("%s/%u/LUN#%u", pcszDevice, uInstance, uLUN);
3049 mapMediumAttachments[devicePath] = pMediumAtt;
3050
3051 /* SCSI has a another driver between device and block. */
3052 if (enmBus == StorageBus_SCSI || enmBus == StorageBus_SAS)
3053 {
3054 InsertConfigString(pLunL0, "Driver", "SCSI");
3055 PCFGMNODE pL1Cfg = NULL;
3056 InsertConfigNode(pLunL0, "Config", &pL1Cfg);
3057 InsertConfigInteger(pL1Cfg, "NonRotationalMedium", !!fNonRotational);
3058
3059 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL0);
3060 }
3061
3062 ComPtr<IMedium> pMedium;
3063 hrc = pMediumAtt->COMGETTER(Medium)(pMedium.asOutParam()); H();
3064
3065 /*
3066 * 1. Only check this for hard disk images.
3067 * 2. Only check during VM creation and not later, especially not during
3068 * taking an online snapshot!
3069 */
3070 if ( lType == DeviceType_HardDisk
3071 && ( aMachineState == MachineState_Starting
3072 || aMachineState == MachineState_Restoring))
3073 {
3074 /*
3075 * Some sanity checks.
3076 */
3077 ComPtr<IMediumFormat> pMediumFormat;
3078 hrc = pMedium->COMGETTER(MediumFormat)(pMediumFormat.asOutParam()); H();
3079 ULONG uCaps;
3080 hrc = pMediumFormat->COMGETTER(Capabilities)(&uCaps); H();
3081 if (uCaps & MediumFormatCapabilities_File)
3082 {
3083 Bstr strFile;
3084 hrc = pMedium->COMGETTER(Location)(strFile.asOutParam()); H();
3085 Utf8Str utfFile = Utf8Str(strFile);
3086 Bstr strSnap;
3087 ComPtr<IMachine> pMachine = machine();
3088 hrc = pMachine->COMGETTER(SnapshotFolder)(strSnap.asOutParam()); H();
3089 Utf8Str utfSnap = Utf8Str(strSnap);
3090 RTFSTYPE enmFsTypeFile = RTFSTYPE_UNKNOWN;
3091 RTFSTYPE enmFsTypeSnap = RTFSTYPE_UNKNOWN;
3092 int rc2 = RTFsQueryType(utfFile.c_str(), &enmFsTypeFile);
3093 AssertMsgRCReturn(rc2, ("Querying the file type of '%s' failed!\n", utfFile.c_str()), rc2);
3094 /* Ignore the error code. On error, the file system type is still 'unknown' so
3095 * none of the following paths are taken. This can happen for new VMs which
3096 * still don't have a snapshot folder. */
3097 (void)RTFsQueryType(utfSnap.c_str(), &enmFsTypeSnap);
3098 if (!mfSnapshotFolderDiskTypeShown)
3099 {
3100 LogRel(("File system of '%s' (snapshots) is %s\n",
3101 utfSnap.c_str(), RTFsTypeName(enmFsTypeSnap)));
3102 mfSnapshotFolderDiskTypeShown = true;
3103 }
3104 LogRel(("File system of '%s' is %s\n", utfFile.c_str(), RTFsTypeName(enmFsTypeFile)));
3105 LONG64 i64Size;
3106 hrc = pMedium->COMGETTER(LogicalSize)(&i64Size); H();
3107#ifdef RT_OS_WINDOWS
3108 if ( enmFsTypeFile == RTFSTYPE_FAT
3109 && i64Size >= _4G)
3110 {
3111 const char *pszUnit;
3112 uint64_t u64Print = formatDiskSize((uint64_t)i64Size, &pszUnit);
3113 setVMRuntimeErrorCallbackF(pVM, this, 0,
3114 "FatPartitionDetected",
3115 N_("The medium '%ls' has a logical size of %RU64%s "
3116 "but the file system the medium is located on seems "
3117 "to be FAT(32) which cannot handle files bigger than 4GB.\n"
3118 "We strongly recommend to put all your virtual disk images and "
3119 "the snapshot folder onto an NTFS partition"),
3120 strFile.raw(), u64Print, pszUnit);
3121 }
3122#else /* !RT_OS_WINDOWS */
3123 if ( enmFsTypeFile == RTFSTYPE_FAT
3124 || enmFsTypeFile == RTFSTYPE_EXT
3125 || enmFsTypeFile == RTFSTYPE_EXT2
3126 || enmFsTypeFile == RTFSTYPE_EXT3
3127 || enmFsTypeFile == RTFSTYPE_EXT4)
3128 {
3129 RTFILE file;
3130 rc = RTFileOpen(&file, utfFile.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
3131 if (RT_SUCCESS(rc))
3132 {
3133 RTFOFF maxSize;
3134 /* Careful: This function will work only on selected local file systems! */
3135 rc = RTFileGetMaxSizeEx(file, &maxSize);
3136 RTFileClose(file);
3137 if ( RT_SUCCESS(rc)
3138 && maxSize > 0
3139 && i64Size > (LONG64)maxSize)
3140 {
3141 const char *pszUnitSiz;
3142 const char *pszUnitMax;
3143 uint64_t u64PrintSiz = formatDiskSize((LONG64)i64Size, &pszUnitSiz);
3144 uint64_t u64PrintMax = formatDiskSize(maxSize, &pszUnitMax);
3145 setVMRuntimeErrorCallbackF(pVM, this, 0,
3146 "FatPartitionDetected", /* <= not exact but ... */
3147 N_("The medium '%ls' has a logical size of %RU64%s "
3148 "but the file system the medium is located on can "
3149 "only handle files up to %RU64%s in theory.\n"
3150 "We strongly recommend to put all your virtual disk "
3151 "images and the snapshot folder onto a proper "
3152 "file system (e.g. ext3) with a sufficient size"),
3153 strFile.raw(), u64PrintSiz, pszUnitSiz, u64PrintMax, pszUnitMax);
3154 }
3155 }
3156 }
3157#endif /* !RT_OS_WINDOWS */
3158
3159 /*
3160 * Snapshot folder:
3161 * Here we test only for a FAT partition as we had to create a dummy file otherwise
3162 */
3163 if ( enmFsTypeSnap == RTFSTYPE_FAT
3164 && i64Size >= _4G
3165 && !mfSnapshotFolderSizeWarningShown)
3166 {
3167 const char *pszUnit;
3168 uint64_t u64Print = formatDiskSize(i64Size, &pszUnit);
3169 setVMRuntimeErrorCallbackF(pVM, this, 0,
3170 "FatPartitionDetected",
3171#ifdef RT_OS_WINDOWS
3172 N_("The snapshot folder of this VM '%ls' seems to be located on "
3173 "a FAT(32) file system. The logical size of the medium '%ls' "
3174 "(%RU64%s) is bigger than the maximum file size this file "
3175 "system can handle (4GB).\n"
3176 "We strongly recommend to put all your virtual disk images and "
3177 "the snapshot folder onto an NTFS partition"),
3178#else
3179 N_("The snapshot folder of this VM '%ls' seems to be located on "
3180 "a FAT(32) file system. The logical size of the medium '%ls' "
3181 "(%RU64%s) is bigger than the maximum file size this file "
3182 "system can handle (4GB).\n"
3183 "We strongly recommend to put all your virtual disk images and "
3184 "the snapshot folder onto a proper file system (e.g. ext3)"),
3185#endif
3186 strSnap.raw(), strFile.raw(), u64Print, pszUnit);
3187 /* Show this particular warning only once */
3188 mfSnapshotFolderSizeWarningShown = true;
3189 }
3190
3191#ifdef RT_OS_LINUX
3192 /*
3193 * Ext4 bug: Check if the host I/O cache is disabled and the disk image is located
3194 * on an ext4 partition. Later we have to check the Linux kernel version!
3195 * This bug apparently applies to the XFS file system as well.
3196 * Linux 2.6.36 is known to be fixed (tested with 2.6.36-rc4).
3197 */
3198
3199 char szOsRelease[128];
3200 rc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szOsRelease, sizeof(szOsRelease));
3201 bool fKernelHasODirectBug = RT_FAILURE(rc)
3202 || (RTStrVersionCompare(szOsRelease, "2.6.36-rc4") < 0);
3203
3204 if ( (uCaps & MediumFormatCapabilities_Asynchronous)
3205 && !fUseHostIOCache
3206 && fKernelHasODirectBug)
3207 {
3208 if ( enmFsTypeFile == RTFSTYPE_EXT4
3209 || enmFsTypeFile == RTFSTYPE_XFS)
3210 {
3211 setVMRuntimeErrorCallbackF(pVM, this, 0,
3212 "Ext4PartitionDetected",
3213 N_("The host I/O cache for at least one controller is disabled "
3214 "and the medium '%ls' for this VM "
3215 "is located on an %s partition. There is a known Linux "
3216 "kernel bug which can lead to the corruption of the virtual "
3217 "disk image under these conditions.\n"
3218 "Either enable the host I/O cache permanently in the VM "
3219 "settings or put the disk image and the snapshot folder "
3220 "onto a different file system.\n"
3221 "The host I/O cache will now be enabled for this medium"),
3222 strFile.raw(), enmFsTypeFile == RTFSTYPE_EXT4 ? "ext4" : "xfs");
3223 fUseHostIOCache = true;
3224 }
3225 else if ( ( enmFsTypeSnap == RTFSTYPE_EXT4
3226 || enmFsTypeSnap == RTFSTYPE_XFS)
3227 && !mfSnapshotFolderExt4WarningShown)
3228 {
3229 setVMRuntimeErrorCallbackF(pVM, this, 0,
3230 "Ext4PartitionDetected",
3231 N_("The host I/O cache for at least one controller is disabled "
3232 "and the snapshot folder for this VM "
3233 "is located on an %s partition. There is a known Linux "
3234 "kernel bug which can lead to the corruption of the virtual "
3235 "disk image under these conditions.\n"
3236 "Either enable the host I/O cache permanently in the VM "
3237 "settings or put the disk image and the snapshot folder "
3238 "onto a different file system.\n"
3239 "The host I/O cache will now be enabled for this medium"),
3240 enmFsTypeSnap == RTFSTYPE_EXT4 ? "ext4" : "xfs");
3241 fUseHostIOCache = true;
3242 mfSnapshotFolderExt4WarningShown = true;
3243 }
3244 }
3245#endif
3246 }
3247 }
3248
3249 if ( pMedium
3250 && ( lType == DeviceType_DVD
3251 || lType == DeviceType_Floppy))
3252 {
3253 /*
3254 * Informative logging.
3255 */
3256 ComPtr<IMediumFormat> pMediumFormat;
3257 hrc = pMedium->COMGETTER(MediumFormat)(pMediumFormat.asOutParam()); H();
3258 ULONG uCaps;
3259 hrc = pMediumFormat->COMGETTER(Capabilities)(&uCaps); H();
3260 if (uCaps & MediumFormatCapabilities_File)
3261 {
3262 Bstr strFile;
3263 hrc = pMedium->COMGETTER(Location)(strFile.asOutParam()); H();
3264 Utf8Str utfFile = Utf8Str(strFile);
3265 RTFSTYPE enmFsTypeFile = RTFSTYPE_UNKNOWN;
3266 (void)RTFsQueryType(utfFile.c_str(), &enmFsTypeFile);
3267 LogRel(("File system of '%s' (%s) is %s\n",
3268 utfFile.c_str(), lType == DeviceType_DVD ? "DVD" : "Floppy",
3269 RTFsTypeName(enmFsTypeFile)));
3270 }
3271 }
3272
3273 BOOL fPassthrough;
3274 hrc = pMediumAtt->COMGETTER(Passthrough)(&fPassthrough); H();
3275
3276 ComObjPtr<IBandwidthGroup> pBwGroup;
3277 Bstr strBwGroup;
3278 hrc = pMediumAtt->COMGETTER(BandwidthGroup)(pBwGroup.asOutParam()); H();
3279
3280 if (!pBwGroup.isNull())
3281 {
3282 hrc = pBwGroup->COMGETTER(Name)(strBwGroup.asOutParam()); H();
3283 }
3284
3285 rc = configMedium(pLunL0,
3286 !!fPassthrough,
3287 lType,
3288 fUseHostIOCache,
3289 fBuiltinIOCache,
3290 fSetupMerge,
3291 uMergeSource,
3292 uMergeTarget,
3293 strBwGroup.isEmpty() ? NULL : Utf8Str(strBwGroup).c_str(),
3294 !!fDiscard,
3295 pMedium,
3296 aMachineState,
3297 phrc);
3298 if (RT_FAILURE(rc))
3299 return rc;
3300
3301 if (fAttachDetach)
3302 {
3303 /* Attach the new driver. */
3304 rc = PDMR3DeviceAttach(pVM, pcszDevice, uInstance, uLUN,
3305 fHotplug ? 0 : PDM_TACH_FLAGS_NOT_HOT_PLUG, NULL /*ppBase*/);
3306 AssertRCReturn(rc, rc);
3307
3308 /* There is no need to handle removable medium mounting, as we
3309 * unconditionally replace everthing including the block driver level.
3310 * This means the new medium will be picked up automatically. */
3311 }
3312
3313 if (paLedDevType)
3314 paLedDevType[uLUN] = lType;
3315 }
3316 catch (ConfigError &x)
3317 {
3318 // InsertConfig threw something:
3319 return x.m_vrc;
3320 }
3321
3322#undef H
3323
3324 return VINF_SUCCESS;;
3325}
3326
3327int Console::configMedium(PCFGMNODE pLunL0,
3328 bool fPassthrough,
3329 DeviceType_T enmType,
3330 bool fUseHostIOCache,
3331 bool fBuiltinIOCache,
3332 bool fSetupMerge,
3333 unsigned uMergeSource,
3334 unsigned uMergeTarget,
3335 const char *pcszBwGroup,
3336 bool fDiscard,
3337 IMedium *pMedium,
3338 MachineState_T aMachineState,
3339 HRESULT *phrc)
3340{
3341 // InsertConfig* throws
3342 try
3343 {
3344 int rc = VINF_SUCCESS;
3345 HRESULT hrc;
3346 Bstr bstr;
3347 PCFGMNODE pLunL1 = NULL;
3348 PCFGMNODE pCfg = NULL;
3349
3350#define H() \
3351 AssertMsgReturnStmt(SUCCEEDED(hrc), ("hrc=%Rhrc\n", hrc), if (phrc) *phrc = hrc, Global::vboxStatusCodeFromCOM(hrc))
3352
3353
3354 BOOL fHostDrive = FALSE;
3355 MediumType_T mediumType = MediumType_Normal;
3356 if (pMedium)
3357 {
3358 hrc = pMedium->COMGETTER(HostDrive)(&fHostDrive); H();
3359 hrc = pMedium->COMGETTER(Type)(&mediumType); H();
3360 }
3361
3362 if (fHostDrive)
3363 {
3364 Assert(pMedium);
3365 if (enmType == DeviceType_DVD)
3366 {
3367 InsertConfigString(pLunL0, "Driver", "HostDVD");
3368 InsertConfigNode(pLunL0, "Config", &pCfg);
3369
3370 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
3371 InsertConfigString(pCfg, "Path", bstr);
3372
3373 InsertConfigInteger(pCfg, "Passthrough", fPassthrough);
3374 }
3375 else if (enmType == DeviceType_Floppy)
3376 {
3377 InsertConfigString(pLunL0, "Driver", "HostFloppy");
3378 InsertConfigNode(pLunL0, "Config", &pCfg);
3379
3380 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
3381 InsertConfigString(pCfg, "Path", bstr);
3382 }
3383 }
3384 else
3385 {
3386 InsertConfigString(pLunL0, "Driver", "Block");
3387 InsertConfigNode(pLunL0, "Config", &pCfg);
3388 switch (enmType)
3389 {
3390 case DeviceType_DVD:
3391 InsertConfigString(pCfg, "Type", "DVD");
3392 InsertConfigInteger(pCfg, "Mountable", 1);
3393 break;
3394 case DeviceType_Floppy:
3395 InsertConfigString(pCfg, "Type", "Floppy 1.44");
3396 InsertConfigInteger(pCfg, "Mountable", 1);
3397 break;
3398 case DeviceType_HardDisk:
3399 default:
3400 InsertConfigString(pCfg, "Type", "HardDisk");
3401 InsertConfigInteger(pCfg, "Mountable", 0);
3402 }
3403
3404 if ( pMedium
3405 && ( enmType == DeviceType_DVD
3406 || enmType == DeviceType_Floppy)
3407 )
3408 {
3409 // if this medium represents an ISO image and this image is inaccessible,
3410 // the ignore it instead of causing a failure; this can happen when we
3411 // restore a VM state and the ISO has disappeared, e.g. because the Guest
3412 // Additions were mounted and the user upgraded VirtualBox. Previously
3413 // we failed on startup, but that's not good because the only way out then
3414 // would be to discard the VM state...
3415 MediumState_T mediumState;
3416 hrc = pMedium->RefreshState(&mediumState); H();
3417 if (mediumState == MediumState_Inaccessible)
3418 {
3419 Bstr loc;
3420 hrc = pMedium->COMGETTER(Location)(loc.asOutParam()); H();
3421 setVMRuntimeErrorCallbackF(VMR3GetVM(mpUVM),
3422 this,
3423 0,
3424 "DvdOrFloppyImageInaccessible",
3425 "The image file '%ls' is inaccessible and is being ignored. Please select a different image file for the virtual %s drive.",
3426 loc.raw(),
3427 enmType == DeviceType_DVD ? "DVD" : "floppy");
3428 pMedium = NULL;
3429 }
3430 }
3431
3432 if (pMedium)
3433 {
3434 /* Start with length of parent chain, as the list is reversed */
3435 unsigned uImage = 0;
3436 IMedium *pTmp = pMedium;
3437 while (pTmp)
3438 {
3439 uImage++;
3440 hrc = pTmp->COMGETTER(Parent)(&pTmp); H();
3441 }
3442 /* Index of last image */
3443 uImage--;
3444
3445#if 0 /* Enable for I/O debugging */
3446 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL0);
3447 InsertConfigString(pLunL0, "Driver", "DiskIntegrity");
3448 InsertConfigNode(pLunL0, "Config", &pCfg);
3449 InsertConfigInteger(pCfg, "CheckConsistency", 0);
3450 InsertConfigInteger(pCfg, "CheckDoubleCompletions", 1);
3451#endif
3452
3453 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
3454 InsertConfigString(pLunL1, "Driver", "VD");
3455 InsertConfigNode(pLunL1, "Config", &pCfg);
3456
3457 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
3458 InsertConfigString(pCfg, "Path", bstr);
3459
3460 hrc = pMedium->COMGETTER(Format)(bstr.asOutParam()); H();
3461 InsertConfigString(pCfg, "Format", bstr);
3462
3463 if (mediumType == MediumType_Readonly)
3464 InsertConfigInteger(pCfg, "ReadOnly", 1);
3465 else if (enmType == DeviceType_Floppy)
3466 InsertConfigInteger(pCfg, "MaybeReadOnly", 1);
3467
3468 /* Start without exclusive write access to the images. */
3469 /** @todo Live Migration: I don't quite like this, we risk screwing up when
3470 * we're resuming the VM if some 3rd dude have any of the VDIs open
3471 * with write sharing denied. However, if the two VMs are sharing a
3472 * image it really is necessary....
3473 *
3474 * So, on the "lock-media" command, the target teleporter should also
3475 * make DrvVD undo TempReadOnly. It gets interesting if we fail after
3476 * that. Grumble. */
3477 if ( enmType == DeviceType_HardDisk
3478 && ( aMachineState == MachineState_TeleportingIn
3479 || aMachineState == MachineState_FaultTolerantSyncing))
3480 InsertConfigInteger(pCfg, "TempReadOnly", 1);
3481
3482 /* Flag for opening the medium for sharing between VMs. This
3483 * is done at the moment only for the first (and only) medium
3484 * in the chain, as shared media can have no diffs. */
3485 if (mediumType == MediumType_Shareable)
3486 InsertConfigInteger(pCfg, "Shareable", 1);
3487
3488 if (!fUseHostIOCache)
3489 {
3490 InsertConfigInteger(pCfg, "UseNewIo", 1);
3491 /*
3492 * Activate the builtin I/O cache for harddisks only.
3493 * It caches writes only which doesn't make sense for DVD drives
3494 * and just increases the overhead.
3495 */
3496 if ( fBuiltinIOCache
3497 && (enmType == DeviceType_HardDisk))
3498 InsertConfigInteger(pCfg, "BlockCache", 1);
3499 }
3500
3501 if (fSetupMerge)
3502 {
3503 InsertConfigInteger(pCfg, "SetupMerge", 1);
3504 if (uImage == uMergeSource)
3505 InsertConfigInteger(pCfg, "MergeSource", 1);
3506 else if (uImage == uMergeTarget)
3507 InsertConfigInteger(pCfg, "MergeTarget", 1);
3508 }
3509
3510 switch (enmType)
3511 {
3512 case DeviceType_DVD:
3513 InsertConfigString(pCfg, "Type", "DVD");
3514 break;
3515 case DeviceType_Floppy:
3516 InsertConfigString(pCfg, "Type", "Floppy");
3517 break;
3518 case DeviceType_HardDisk:
3519 default:
3520 InsertConfigString(pCfg, "Type", "HardDisk");
3521 }
3522
3523 if (pcszBwGroup)
3524 InsertConfigString(pCfg, "BwGroup", pcszBwGroup);
3525
3526 if (fDiscard)
3527 InsertConfigInteger(pCfg, "Discard", 1);
3528
3529 /* Pass all custom parameters. */
3530 bool fHostIP = true;
3531 SafeArray<BSTR> names;
3532 SafeArray<BSTR> values;
3533 hrc = pMedium->GetProperties(Bstr().raw(),
3534 ComSafeArrayAsOutParam(names),
3535 ComSafeArrayAsOutParam(values)); H();
3536
3537 if (names.size() != 0)
3538 {
3539 PCFGMNODE pVDC;
3540 InsertConfigNode(pCfg, "VDConfig", &pVDC);
3541 for (size_t ii = 0; ii < names.size(); ++ii)
3542 {
3543 if (values[ii] && *values[ii])
3544 {
3545 Utf8Str name = names[ii];
3546 Utf8Str value = values[ii];
3547 InsertConfigString(pVDC, name.c_str(), value);
3548 if ( name.compare("HostIPStack") == 0
3549 && value.compare("0") == 0)
3550 fHostIP = false;
3551 }
3552 }
3553 }
3554
3555 /* Create an inverted list of parents. */
3556 uImage--;
3557 IMedium *pParentMedium = pMedium;
3558 for (PCFGMNODE pParent = pCfg;; uImage--)
3559 {
3560 hrc = pParentMedium->COMGETTER(Parent)(&pMedium); H();
3561 if (!pMedium)
3562 break;
3563
3564 PCFGMNODE pCur;
3565 InsertConfigNode(pParent, "Parent", &pCur);
3566 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
3567 InsertConfigString(pCur, "Path", bstr);
3568
3569 hrc = pMedium->COMGETTER(Format)(bstr.asOutParam()); H();
3570 InsertConfigString(pCur, "Format", bstr);
3571
3572 if (fSetupMerge)
3573 {
3574 if (uImage == uMergeSource)
3575 InsertConfigInteger(pCur, "MergeSource", 1);
3576 else if (uImage == uMergeTarget)
3577 InsertConfigInteger(pCur, "MergeTarget", 1);
3578 }
3579
3580 /* Pass all custom parameters. */
3581 SafeArray<BSTR> aNames;
3582 SafeArray<BSTR> aValues;
3583 hrc = pMedium->GetProperties(NULL,
3584 ComSafeArrayAsOutParam(aNames),
3585 ComSafeArrayAsOutParam(aValues)); H();
3586
3587 if (aNames.size() != 0)
3588 {
3589 PCFGMNODE pVDC;
3590 InsertConfigNode(pCur, "VDConfig", &pVDC);
3591 for (size_t ii = 0; ii < aNames.size(); ++ii)
3592 {
3593 if (aValues[ii] && *aValues[ii])
3594 {
3595 Utf8Str name = aNames[ii];
3596 Utf8Str value = aValues[ii];
3597 InsertConfigString(pVDC, name.c_str(), value);
3598 if ( name.compare("HostIPStack") == 0
3599 && value.compare("0") == 0)
3600 fHostIP = false;
3601 }
3602 }
3603 }
3604
3605 /* next */
3606 pParent = pCur;
3607 pParentMedium = pMedium;
3608 }
3609
3610 /* Custom code: put marker to not use host IP stack to driver
3611 * configuration node. Simplifies life of DrvVD a bit. */
3612 if (!fHostIP)
3613 InsertConfigInteger(pCfg, "HostIPStack", 0);
3614 }
3615 }
3616#undef H
3617 }
3618 catch (ConfigError &x)
3619 {
3620 // InsertConfig threw something:
3621 return x.m_vrc;
3622 }
3623
3624 return VINF_SUCCESS;
3625}
3626
3627/**
3628 * Construct the Network configuration tree
3629 *
3630 * @returns VBox status code.
3631 *
3632 * @param pszDevice The PDM device name.
3633 * @param uInstance The PDM device instance.
3634 * @param uLun The PDM LUN number of the drive.
3635 * @param aNetworkAdapter The network adapter whose attachment needs to be changed
3636 * @param pCfg Configuration node for the device
3637 * @param pLunL0 To store the pointer to the LUN#0.
3638 * @param pInst The instance CFGM node
3639 * @param fAttachDetach To determine if the network attachment should
3640 * be attached/detached after/before
3641 * configuration.
3642 * @param fIgnoreConnectFailure
3643 * True if connection failures should be ignored
3644 * (makes only sense for bridged/host-only networks).
3645 *
3646 * @note Locks this object for writing.
3647 * @thread EMT
3648 */
3649int Console::configNetwork(const char *pszDevice,
3650 unsigned uInstance,
3651 unsigned uLun,
3652 INetworkAdapter *aNetworkAdapter,
3653 PCFGMNODE pCfg,
3654 PCFGMNODE pLunL0,
3655 PCFGMNODE pInst,
3656 bool fAttachDetach,
3657 bool fIgnoreConnectFailure)
3658{
3659 AutoCaller autoCaller(this);
3660 AssertComRCReturn(autoCaller.rc(), VERR_ACCESS_DENIED);
3661
3662 // InsertConfig* throws
3663 try
3664 {
3665 int rc = VINF_SUCCESS;
3666 HRESULT hrc;
3667 Bstr bstr;
3668
3669#define H() AssertMsgReturn(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), VERR_GENERAL_FAILURE)
3670
3671 /*
3672 * Locking the object before doing VMR3* calls is quite safe here, since
3673 * we're on EMT. Write lock is necessary because we indirectly modify the
3674 * meAttachmentType member.
3675 */
3676 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3677
3678 PVM pVM = VMR3GetVM(mpUVM); /* We're on an EMT, so this is safe. */
3679
3680 ComPtr<IMachine> pMachine = machine();
3681
3682 ComPtr<IVirtualBox> virtualBox;
3683 hrc = pMachine->COMGETTER(Parent)(virtualBox.asOutParam()); H();
3684
3685 ComPtr<IHost> host;
3686 hrc = virtualBox->COMGETTER(Host)(host.asOutParam()); H();
3687
3688 BOOL fSniffer;
3689 hrc = aNetworkAdapter->COMGETTER(TraceEnabled)(&fSniffer); H();
3690
3691 NetworkAdapterPromiscModePolicy_T enmPromiscModePolicy;
3692 hrc = aNetworkAdapter->COMGETTER(PromiscModePolicy)(&enmPromiscModePolicy); H();
3693 const char *pszPromiscuousGuestPolicy;
3694 switch (enmPromiscModePolicy)
3695 {
3696 case NetworkAdapterPromiscModePolicy_Deny: pszPromiscuousGuestPolicy = "deny"; break;
3697 case NetworkAdapterPromiscModePolicy_AllowNetwork: pszPromiscuousGuestPolicy = "allow-network"; break;
3698 case NetworkAdapterPromiscModePolicy_AllowAll: pszPromiscuousGuestPolicy = "allow-all"; break;
3699 default: AssertFailedReturn(VERR_INTERNAL_ERROR_4);
3700 }
3701
3702 if (fAttachDetach)
3703 {
3704 rc = PDMR3DeviceDetach(pVM, pszDevice, uInstance, uLun, 0 /*fFlags*/);
3705 if (rc == VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN)
3706 rc = VINF_SUCCESS;
3707 AssertLogRelRCReturn(rc, rc);
3708
3709 /* nuke anything which might have been left behind. */
3710 CFGMR3RemoveNode(CFGMR3GetChildF(pInst, "LUN#%u", uLun));
3711 }
3712
3713#ifdef VBOX_WITH_NETSHAPER
3714 ComObjPtr<IBandwidthGroup> pBwGroup;
3715 Bstr strBwGroup;
3716 hrc = aNetworkAdapter->COMGETTER(BandwidthGroup)(pBwGroup.asOutParam()); H();
3717
3718 if (!pBwGroup.isNull())
3719 {
3720 hrc = pBwGroup->COMGETTER(Name)(strBwGroup.asOutParam()); H();
3721 }
3722#endif /* VBOX_WITH_NETSHAPER */
3723
3724 Utf8Str strNetDriver;
3725
3726
3727 InsertConfigNode(pInst, "LUN#0", &pLunL0);
3728
3729#ifdef VBOX_WITH_NETSHAPER
3730 if (!strBwGroup.isEmpty())
3731 {
3732 InsertConfigString(pLunL0, "Driver", "NetShaper");
3733 InsertConfigNode(pLunL0, "Config", &pCfg);
3734 InsertConfigString(pCfg, "BwGroup", strBwGroup);
3735 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL0);
3736 }
3737#endif /* VBOX_WITH_NETSHAPER */
3738
3739 if (fSniffer)
3740 {
3741 InsertConfigString(pLunL0, "Driver", "NetSniffer");
3742 InsertConfigNode(pLunL0, "Config", &pCfg);
3743 hrc = aNetworkAdapter->COMGETTER(TraceFile)(bstr.asOutParam()); H();
3744 if (!bstr.isEmpty()) /* check convention for indicating default file. */
3745 InsertConfigString(pCfg, "File", bstr);
3746 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL0);
3747 }
3748
3749
3750 Bstr networkName, trunkName, trunkType;
3751 NetworkAttachmentType_T eAttachmentType;
3752 hrc = aNetworkAdapter->COMGETTER(AttachmentType)(&eAttachmentType); H();
3753 switch (eAttachmentType)
3754 {
3755 case NetworkAttachmentType_Null:
3756 break;
3757
3758 case NetworkAttachmentType_NAT:
3759 {
3760 ComPtr<INATEngine> natEngine;
3761 hrc = aNetworkAdapter->COMGETTER(NATEngine)(natEngine.asOutParam()); H();
3762 InsertConfigString(pLunL0, "Driver", "NAT");
3763 InsertConfigNode(pLunL0, "Config", &pCfg);
3764
3765 /* Configure TFTP prefix and boot filename. */
3766 hrc = virtualBox->COMGETTER(HomeFolder)(bstr.asOutParam()); H();
3767 if (!bstr.isEmpty())
3768 InsertConfigString(pCfg, "TFTPPrefix", Utf8StrFmt("%ls%c%s", bstr.raw(), RTPATH_DELIMITER, "TFTP"));
3769 hrc = pMachine->COMGETTER(Name)(bstr.asOutParam()); H();
3770 InsertConfigString(pCfg, "BootFile", Utf8StrFmt("%ls.pxe", bstr.raw()));
3771
3772 hrc = natEngine->COMGETTER(Network)(bstr.asOutParam()); H();
3773 if (!bstr.isEmpty())
3774 InsertConfigString(pCfg, "Network", bstr);
3775 else
3776 {
3777 ULONG uSlot;
3778 hrc = aNetworkAdapter->COMGETTER(Slot)(&uSlot); H();
3779 InsertConfigString(pCfg, "Network", Utf8StrFmt("10.0.%d.0/24", uSlot+2));
3780 }
3781 hrc = natEngine->COMGETTER(HostIP)(bstr.asOutParam()); H();
3782 if (!bstr.isEmpty())
3783 InsertConfigString(pCfg, "BindIP", bstr);
3784 ULONG mtu = 0;
3785 ULONG sockSnd = 0;
3786 ULONG sockRcv = 0;
3787 ULONG tcpSnd = 0;
3788 ULONG tcpRcv = 0;
3789 hrc = natEngine->GetNetworkSettings(&mtu, &sockSnd, &sockRcv, &tcpSnd, &tcpRcv); H();
3790 if (mtu)
3791 InsertConfigInteger(pCfg, "SlirpMTU", mtu);
3792 if (sockRcv)
3793 InsertConfigInteger(pCfg, "SockRcv", sockRcv);
3794 if (sockSnd)
3795 InsertConfigInteger(pCfg, "SockSnd", sockSnd);
3796 if (tcpRcv)
3797 InsertConfigInteger(pCfg, "TcpRcv", tcpRcv);
3798 if (tcpSnd)
3799 InsertConfigInteger(pCfg, "TcpSnd", tcpSnd);
3800 hrc = natEngine->COMGETTER(TFTPPrefix)(bstr.asOutParam()); H();
3801 if (!bstr.isEmpty())
3802 {
3803 RemoveConfigValue(pCfg, "TFTPPrefix");
3804 InsertConfigString(pCfg, "TFTPPrefix", bstr);
3805 }
3806 hrc = natEngine->COMGETTER(TFTPBootFile)(bstr.asOutParam()); H();
3807 if (!bstr.isEmpty())
3808 {
3809 RemoveConfigValue(pCfg, "BootFile");
3810 InsertConfigString(pCfg, "BootFile", bstr);
3811 }
3812 hrc = natEngine->COMGETTER(TFTPNextServer)(bstr.asOutParam()); H();
3813 if (!bstr.isEmpty())
3814 InsertConfigString(pCfg, "NextServer", bstr);
3815 BOOL fDNSFlag;
3816 hrc = natEngine->COMGETTER(DNSPassDomain)(&fDNSFlag); H();
3817 InsertConfigInteger(pCfg, "PassDomain", fDNSFlag);
3818 hrc = natEngine->COMGETTER(DNSProxy)(&fDNSFlag); H();
3819 InsertConfigInteger(pCfg, "DNSProxy", fDNSFlag);
3820 hrc = natEngine->COMGETTER(DNSUseHostResolver)(&fDNSFlag); H();
3821 InsertConfigInteger(pCfg, "UseHostResolver", fDNSFlag);
3822
3823 ULONG aliasMode;
3824 hrc = natEngine->COMGETTER(AliasMode)(&aliasMode); H();
3825 InsertConfigInteger(pCfg, "AliasMode", aliasMode);
3826
3827 /* port-forwarding */
3828 SafeArray<BSTR> pfs;
3829 hrc = natEngine->COMGETTER(Redirects)(ComSafeArrayAsOutParam(pfs)); H();
3830 PCFGMNODE pPF = NULL; /* /Devices/Dev/.../Config/PF#0/ */
3831 for (unsigned int i = 0; i < pfs.size(); ++i)
3832 {
3833 uint16_t port = 0;
3834 BSTR r = pfs[i];
3835 Utf8Str utf = Utf8Str(r);
3836 Utf8Str strName;
3837 Utf8Str strProto;
3838 Utf8Str strHostPort;
3839 Utf8Str strHostIP;
3840 Utf8Str strGuestPort;
3841 Utf8Str strGuestIP;
3842 size_t pos, ppos;
3843 pos = ppos = 0;
3844#define ITERATE_TO_NEXT_TERM(res, str, pos, ppos) \
3845 do { \
3846 pos = str.find(",", ppos); \
3847 if (pos == Utf8Str::npos) \
3848 { \
3849 Log(( #res " extracting from %s is failed\n", str.c_str())); \
3850 continue; \
3851 } \
3852 res = str.substr(ppos, pos - ppos); \
3853 Log2((#res " %s pos:%d, ppos:%d\n", res.c_str(), pos, ppos)); \
3854 ppos = pos + 1; \
3855 } while (0)
3856 ITERATE_TO_NEXT_TERM(strName, utf, pos, ppos);
3857 ITERATE_TO_NEXT_TERM(strProto, utf, pos, ppos);
3858 ITERATE_TO_NEXT_TERM(strHostIP, utf, pos, ppos);
3859 ITERATE_TO_NEXT_TERM(strHostPort, utf, pos, ppos);
3860 ITERATE_TO_NEXT_TERM(strGuestIP, utf, pos, ppos);
3861 strGuestPort = utf.substr(ppos, utf.length() - ppos);
3862#undef ITERATE_TO_NEXT_TERM
3863
3864 uint32_t proto = strProto.toUInt32();
3865 bool fValid = true;
3866 switch (proto)
3867 {
3868 case NATProtocol_UDP:
3869 strProto = "UDP";
3870 break;
3871 case NATProtocol_TCP:
3872 strProto = "TCP";
3873 break;
3874 default:
3875 fValid = false;
3876 }
3877 /* continue with next rule if no valid proto was passed */
3878 if (!fValid)
3879 continue;
3880
3881 InsertConfigNode(pCfg, strName.c_str(), &pPF);
3882 InsertConfigString(pPF, "Protocol", strProto);
3883
3884 if (!strHostIP.isEmpty())
3885 InsertConfigString(pPF, "BindIP", strHostIP);
3886
3887 if (!strGuestIP.isEmpty())
3888 InsertConfigString(pPF, "GuestIP", strGuestIP);
3889
3890 port = RTStrToUInt16(strHostPort.c_str());
3891 if (port)
3892 InsertConfigInteger(pPF, "HostPort", port);
3893
3894 port = RTStrToUInt16(strGuestPort.c_str());
3895 if (port)
3896 InsertConfigInteger(pPF, "GuestPort", port);
3897 }
3898 break;
3899 }
3900
3901 case NetworkAttachmentType_Bridged:
3902 {
3903#if (defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)) && !defined(VBOX_WITH_NETFLT)
3904 hrc = attachToTapInterface(aNetworkAdapter);
3905 if (FAILED(hrc))
3906 {
3907 switch (hrc)
3908 {
3909 case VERR_ACCESS_DENIED:
3910 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
3911 "Failed to open '/dev/net/tun' for read/write access. Please check the "
3912 "permissions of that node. Either run 'chmod 0666 /dev/net/tun' or "
3913 "change the group of that node and make yourself a member of that group. Make "
3914 "sure that these changes are permanent, especially if you are "
3915 "using udev"));
3916 default:
3917 AssertMsgFailed(("Could not attach to host interface! Bad!\n"));
3918 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
3919 "Failed to initialize Host Interface Networking"));
3920 }
3921 }
3922
3923 Assert((int)maTapFD[uInstance] >= 0);
3924 if ((int)maTapFD[uInstance] >= 0)
3925 {
3926 InsertConfigString(pLunL0, "Driver", "HostInterface");
3927 InsertConfigNode(pLunL0, "Config", &pCfg);
3928 InsertConfigInteger(pCfg, "FileHandle", maTapFD[uInstance]);
3929 }
3930
3931#elif defined(VBOX_WITH_NETFLT)
3932 /*
3933 * This is the new VBoxNetFlt+IntNet stuff.
3934 */
3935 Bstr BridgedIfName;
3936 hrc = aNetworkAdapter->COMGETTER(BridgedInterface)(BridgedIfName.asOutParam());
3937 if (FAILED(hrc))
3938 {
3939 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(BridgedInterface) failed, hrc (0x%x)", hrc));
3940 H();
3941 }
3942
3943 Utf8Str BridgedIfNameUtf8(BridgedIfName);
3944 const char *pszBridgedIfName = BridgedIfNameUtf8.c_str();
3945
3946# if defined(RT_OS_DARWIN)
3947 /* The name is on the form 'ifX: long name', chop it off at the colon. */
3948 char szTrunk[8];
3949 RTStrCopy(szTrunk, sizeof(szTrunk), pszBridgedIfName);
3950 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk));
3951// Quick fix for @bugref{5633}
3952// if (!pszColon)
3953// {
3954// /*
3955// * Dynamic changing of attachment causes an attempt to configure
3956// * network with invalid host adapter (as it is must be changed before
3957// * the attachment), calling Detach here will cause a deadlock.
3958// * See @bugref{4750}.
3959// * hrc = aNetworkAdapter->Detach(); H();
3960// */
3961// return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
3962// N_("Malformed host interface networking name '%ls'"),
3963// BridgedIfName.raw());
3964// }
3965 if (pszColon)
3966 *pszColon = '\0';
3967 const char *pszTrunk = szTrunk;
3968
3969# elif defined(RT_OS_SOLARIS)
3970 /* The name is on the form format 'ifX[:1] - long name, chop it off at space. */
3971 char szTrunk[256];
3972 strlcpy(szTrunk, pszBridgedIfName, sizeof(szTrunk));
3973 char *pszSpace = (char *)memchr(szTrunk, ' ', sizeof(szTrunk));
3974
3975 /*
3976 * Currently don't bother about malformed names here for the sake of people using
3977 * VBoxManage and setting only the NIC name from there. If there is a space we
3978 * chop it off and proceed, otherwise just use whatever we've got.
3979 */
3980 if (pszSpace)
3981 *pszSpace = '\0';
3982
3983 /* Chop it off at the colon (zone naming eg: e1000g:1 we need only the e1000g) */
3984 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk));
3985 if (pszColon)
3986 *pszColon = '\0';
3987
3988 const char *pszTrunk = szTrunk;
3989
3990# elif defined(RT_OS_WINDOWS)
3991 ComPtr<IHostNetworkInterface> hostInterface;
3992 hrc = host->FindHostNetworkInterfaceByName(BridgedIfName.raw(),
3993 hostInterface.asOutParam());
3994 if (!SUCCEEDED(hrc))
3995 {
3996 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: FindByName failed, rc=%Rhrc (0x%x)", hrc, hrc));
3997 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
3998 N_("Nonexistent host networking interface, name '%ls'"),
3999 BridgedIfName.raw());
4000 }
4001
4002 HostNetworkInterfaceType_T eIfType;
4003 hrc = hostInterface->COMGETTER(InterfaceType)(&eIfType);
4004 if (FAILED(hrc))
4005 {
4006 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(InterfaceType) failed, hrc (0x%x)", hrc));
4007 H();
4008 }
4009
4010 if (eIfType != HostNetworkInterfaceType_Bridged)
4011 {
4012 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
4013 N_("Interface ('%ls') is not a Bridged Adapter interface"),
4014 BridgedIfName.raw());
4015 }
4016
4017 hrc = hostInterface->COMGETTER(Id)(bstr.asOutParam());
4018 if (FAILED(hrc))
4019 {
4020 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(Id) failed, hrc (0x%x)", hrc));
4021 H();
4022 }
4023 Guid hostIFGuid(bstr);
4024
4025 INetCfg *pNc;
4026 ComPtr<INetCfgComponent> pAdaptorComponent;
4027 LPWSTR pszApp;
4028
4029 hrc = VBoxNetCfgWinQueryINetCfg(&pNc, FALSE, L"VirtualBox", 10, &pszApp);
4030 Assert(hrc == S_OK);
4031 if (hrc != S_OK)
4032 {
4033 LogRel(("NetworkAttachmentType_Bridged: Failed to get NetCfg, hrc=%Rhrc (0x%x)\n", hrc, hrc));
4034 H();
4035 }
4036
4037 /* get the adapter's INetCfgComponent*/
4038 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.raw(), pAdaptorComponent.asOutParam());
4039 if (hrc != S_OK)
4040 {
4041 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
4042 LogRel(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc));
4043 H();
4044 }
4045#define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\"
4046 char szTrunkName[INTNET_MAX_TRUNK_NAME];
4047 char *pszTrunkName = szTrunkName;
4048 wchar_t * pswzBindName;
4049 hrc = pAdaptorComponent->GetBindName(&pswzBindName);
4050 Assert(hrc == S_OK);
4051 if (hrc == S_OK)
4052 {
4053 int cwBindName = (int)wcslen(pswzBindName) + 1;
4054 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX);
4055 if (sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName)
4056 {
4057 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX);
4058 pszTrunkName += cbFullBindNamePrefix-1;
4059 if (!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName,
4060 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL))
4061 {
4062 DWORD err = GetLastError();
4063 hrc = HRESULT_FROM_WIN32(err);
4064 AssertMsgFailed(("%hrc=%Rhrc %#x\n", hrc, hrc));
4065 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: WideCharToMultiByte failed, hr=%Rhrc (0x%x) err=%u\n", hrc, hrc, err));
4066 }
4067 }
4068 else
4069 {
4070 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: insufficient szTrunkName buffer space\n"));
4071 /** @todo set appropriate error code */
4072 hrc = E_FAIL;
4073 }
4074
4075 if (hrc != S_OK)
4076 {
4077 AssertFailed();
4078 CoTaskMemFree(pswzBindName);
4079 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
4080 H();
4081 }
4082
4083 /* we're not freeing the bind name since we'll use it later for detecting wireless*/
4084 }
4085 else
4086 {
4087 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
4088 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc));
4089 H();
4090 }
4091
4092 const char *pszTrunk = szTrunkName;
4093 /* we're not releasing the INetCfg stuff here since we use it later to figure out whether it is wireless */
4094
4095# elif defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)
4096# if defined(RT_OS_FREEBSD)
4097 /*
4098 * If we bridge to a tap interface open it the `old' direct way.
4099 * This works and performs better than bridging a physical
4100 * interface via the current FreeBSD vboxnetflt implementation.
4101 */
4102 if (!strncmp(pszBridgedIfName, "tap", sizeof "tap" - 1)) {
4103 hrc = attachToTapInterface(aNetworkAdapter);
4104 if (FAILED(hrc))
4105 {
4106 switch (hrc)
4107 {
4108 case VERR_ACCESS_DENIED:
4109 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
4110 "Failed to open '/dev/%s' for read/write access. Please check the "
4111 "permissions of that node, and that the net.link.tap.user_open "
4112 "sysctl is set. Either run 'chmod 0666 /dev/%s' or "
4113 "change the group of that node to vboxusers and make yourself "
4114 "a member of that group. Make sure that these changes are permanent."), pszBridgedIfName, pszBridgedIfName);
4115 default:
4116 AssertMsgFailed(("Could not attach to tap interface! Bad!\n"));
4117 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
4118 "Failed to initialize Host Interface Networking"));
4119 }
4120 }
4121
4122 Assert((int)maTapFD[uInstance] >= 0);
4123 if ((int)maTapFD[uInstance] >= 0)
4124 {
4125 InsertConfigString(pLunL0, "Driver", "HostInterface");
4126 InsertConfigNode(pLunL0, "Config", &pCfg);
4127 InsertConfigInteger(pCfg, "FileHandle", maTapFD[uInstance]);
4128 }
4129 break;
4130 }
4131# endif
4132 /** @todo Check for malformed names. */
4133 const char *pszTrunk = pszBridgedIfName;
4134
4135 /* Issue a warning if the interface is down */
4136 {
4137 int iSock = socket(AF_INET, SOCK_DGRAM, 0);
4138 if (iSock >= 0)
4139 {
4140 struct ifreq Req;
4141 RT_ZERO(Req);
4142 strncpy(Req.ifr_name, pszBridgedIfName, sizeof(Req.ifr_name) - 1);
4143 if (ioctl(iSock, SIOCGIFFLAGS, &Req) >= 0)
4144 if ((Req.ifr_flags & IFF_UP) == 0)
4145 setVMRuntimeErrorCallbackF(pVM, this, 0, "BridgedInterfaceDown",
4146 "Bridged interface %s is down. Guest will not be able to use this interface",
4147 pszBridgedIfName);
4148
4149 close(iSock);
4150 }
4151 }
4152
4153# else
4154# error "PORTME (VBOX_WITH_NETFLT)"
4155# endif
4156
4157 InsertConfigString(pLunL0, "Driver", "IntNet");
4158 InsertConfigNode(pLunL0, "Config", &pCfg);
4159 InsertConfigString(pCfg, "Trunk", pszTrunk);
4160 InsertConfigInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt);
4161 InsertConfigInteger(pCfg, "IgnoreConnectFailure", (uint64_t)fIgnoreConnectFailure);
4162 InsertConfigString(pCfg, "IfPolicyPromisc", pszPromiscuousGuestPolicy);
4163 char szNetwork[INTNET_MAX_NETWORK_NAME];
4164
4165#if defined(RT_OS_SOLARIS) || defined(RT_OS_DARWIN)
4166 /*
4167 * 'pszTrunk' contains just the interface name required in ring-0, while 'pszBridgedIfName' contains
4168 * interface name + optional description. We must not pass any description to the VM as it can differ
4169 * for the same interface name, eg: "nge0 - ethernet" (GUI) vs "nge0" (VBoxManage).
4170 */
4171 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszTrunk);
4172#else
4173 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszBridgedIfName);
4174#endif
4175 InsertConfigString(pCfg, "Network", szNetwork);
4176 networkName = Bstr(szNetwork);
4177 trunkName = Bstr(pszTrunk);
4178 trunkType = Bstr(TRUNKTYPE_NETFLT);
4179
4180# if defined(RT_OS_DARWIN)
4181 /** @todo Come up with a better deal here. Problem is that IHostNetworkInterface is completely useless here. */
4182 if ( strstr(pszBridgedIfName, "Wireless")
4183 || strstr(pszBridgedIfName, "AirPort" ))
4184 InsertConfigInteger(pCfg, "SharedMacOnWire", true);
4185# elif defined(RT_OS_LINUX)
4186 int iSock = socket(AF_INET, SOCK_DGRAM, 0);
4187 if (iSock >= 0)
4188 {
4189 struct iwreq WRq;
4190
4191 memset(&WRq, 0, sizeof(WRq));
4192 strncpy(WRq.ifr_name, pszBridgedIfName, IFNAMSIZ);
4193 bool fSharedMacOnWire = ioctl(iSock, SIOCGIWNAME, &WRq) >= 0;
4194 close(iSock);
4195 if (fSharedMacOnWire)
4196 {
4197 InsertConfigInteger(pCfg, "SharedMacOnWire", true);
4198 Log(("Set SharedMacOnWire\n"));
4199 }
4200 else
4201 Log(("Failed to get wireless name\n"));
4202 }
4203 else
4204 Log(("Failed to open wireless socket\n"));
4205# elif defined(RT_OS_FREEBSD)
4206 int iSock = socket(AF_INET, SOCK_DGRAM, 0);
4207 if (iSock >= 0)
4208 {
4209 struct ieee80211req WReq;
4210 uint8_t abData[32];
4211
4212 memset(&WReq, 0, sizeof(WReq));
4213 strncpy(WReq.i_name, pszBridgedIfName, sizeof(WReq.i_name));
4214 WReq.i_type = IEEE80211_IOC_SSID;
4215 WReq.i_val = -1;
4216 WReq.i_data = abData;
4217 WReq.i_len = sizeof(abData);
4218
4219 bool fSharedMacOnWire = ioctl(iSock, SIOCG80211, &WReq) >= 0;
4220 close(iSock);
4221 if (fSharedMacOnWire)
4222 {
4223 InsertConfigInteger(pCfg, "SharedMacOnWire", true);
4224 Log(("Set SharedMacOnWire\n"));
4225 }
4226 else
4227 Log(("Failed to get wireless name\n"));
4228 }
4229 else
4230 Log(("Failed to open wireless socket\n"));
4231# elif defined(RT_OS_WINDOWS)
4232# define DEVNAME_PREFIX L"\\\\.\\"
4233 /* we are getting the medium type via IOCTL_NDIS_QUERY_GLOBAL_STATS Io Control
4234 * there is a pretty long way till there though since we need to obtain the symbolic link name
4235 * for the adapter device we are going to query given the device Guid */
4236
4237
4238 /* prepend the "\\\\.\\" to the bind name to obtain the link name */
4239
4240 wchar_t FileName[MAX_PATH];
4241 wcscpy(FileName, DEVNAME_PREFIX);
4242 wcscpy((wchar_t*)(((char*)FileName) + sizeof(DEVNAME_PREFIX) - sizeof(FileName[0])), pswzBindName);
4243
4244 /* open the device */
4245 HANDLE hDevice = CreateFile(FileName,
4246 GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
4247 NULL,
4248 OPEN_EXISTING,
4249 FILE_ATTRIBUTE_NORMAL,
4250 NULL);
4251
4252 if (hDevice != INVALID_HANDLE_VALUE)
4253 {
4254 bool fSharedMacOnWire = false;
4255
4256 /* now issue the OID_GEN_PHYSICAL_MEDIUM query */
4257 DWORD Oid = OID_GEN_PHYSICAL_MEDIUM;
4258 NDIS_PHYSICAL_MEDIUM PhMedium;
4259 DWORD cbResult;
4260 if (DeviceIoControl(hDevice,
4261 IOCTL_NDIS_QUERY_GLOBAL_STATS,
4262 &Oid,
4263 sizeof(Oid),
4264 &PhMedium,
4265 sizeof(PhMedium),
4266 &cbResult,
4267 NULL))
4268 {
4269 /* that was simple, now examine PhMedium */
4270 if ( PhMedium == NdisPhysicalMediumWirelessWan
4271 || PhMedium == NdisPhysicalMediumWirelessLan
4272 || PhMedium == NdisPhysicalMediumNative802_11
4273 || PhMedium == NdisPhysicalMediumBluetooth)
4274 fSharedMacOnWire = true;
4275 }
4276 else
4277 {
4278 int winEr = GetLastError();
4279 LogRel(("Console::configNetwork: DeviceIoControl failed, err (0x%x), ignoring\n", winEr));
4280 Assert(winEr == ERROR_INVALID_PARAMETER || winEr == ERROR_NOT_SUPPORTED || winEr == ERROR_BAD_COMMAND);
4281 }
4282 CloseHandle(hDevice);
4283
4284 if (fSharedMacOnWire)
4285 {
4286 Log(("this is a wireless adapter"));
4287 InsertConfigInteger(pCfg, "SharedMacOnWire", true);
4288 Log(("Set SharedMacOnWire\n"));
4289 }
4290 else
4291 Log(("this is NOT a wireless adapter"));
4292 }
4293 else
4294 {
4295 int winEr = GetLastError();
4296 AssertLogRelMsgFailed(("Console::configNetwork: CreateFile failed, err (0x%x), ignoring\n", winEr));
4297 }
4298
4299 CoTaskMemFree(pswzBindName);
4300
4301 pAdaptorComponent.setNull();
4302 /* release the pNc finally */
4303 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
4304# else
4305 /** @todo PORTME: wireless detection */
4306# endif
4307
4308# if defined(RT_OS_SOLARIS)
4309# if 0 /* bird: this is a bit questionable and might cause more trouble than its worth. */
4310 /* Zone access restriction, don't allow snooping the global zone. */
4311 zoneid_t ZoneId = getzoneid();
4312 if (ZoneId != GLOBAL_ZONEID)
4313 {
4314 InsertConfigInteger(pCfg, "IgnoreAllPromisc", true);
4315 }
4316# endif
4317# endif
4318
4319#elif defined(RT_OS_WINDOWS) /* not defined NetFlt */
4320 /* NOTHING TO DO HERE */
4321#elif defined(RT_OS_LINUX)
4322/// @todo aleksey: is there anything to be done here?
4323#elif defined(RT_OS_FREEBSD)
4324/** @todo FreeBSD: Check out this later (HIF networking). */
4325#else
4326# error "Port me"
4327#endif
4328 break;
4329 }
4330
4331 case NetworkAttachmentType_Internal:
4332 {
4333 hrc = aNetworkAdapter->COMGETTER(InternalNetwork)(bstr.asOutParam()); H();
4334 if (!bstr.isEmpty())
4335 {
4336 InsertConfigString(pLunL0, "Driver", "IntNet");
4337 InsertConfigNode(pLunL0, "Config", &pCfg);
4338 InsertConfigString(pCfg, "Network", bstr);
4339 InsertConfigInteger(pCfg, "TrunkType", kIntNetTrunkType_WhateverNone);
4340 InsertConfigString(pCfg, "IfPolicyPromisc", pszPromiscuousGuestPolicy);
4341 networkName = bstr;
4342 trunkType = Bstr(TRUNKTYPE_WHATEVER);
4343 }
4344 break;
4345 }
4346
4347 case NetworkAttachmentType_HostOnly:
4348 {
4349 InsertConfigString(pLunL0, "Driver", "IntNet");
4350 InsertConfigNode(pLunL0, "Config", &pCfg);
4351
4352 Bstr HostOnlyName;
4353 hrc = aNetworkAdapter->COMGETTER(HostOnlyInterface)(HostOnlyName.asOutParam());
4354 if (FAILED(hrc))
4355 {
4356 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(HostOnlyInterface) failed, hrc (0x%x)\n", hrc));
4357 H();
4358 }
4359
4360 Utf8Str HostOnlyNameUtf8(HostOnlyName);
4361 const char *pszHostOnlyName = HostOnlyNameUtf8.c_str();
4362 ComPtr<IHostNetworkInterface> hostInterface;
4363 rc = host->FindHostNetworkInterfaceByName(HostOnlyName.raw(),
4364 hostInterface.asOutParam());
4365 if (!SUCCEEDED(rc))
4366 {
4367 LogRel(("NetworkAttachmentType_HostOnly: FindByName failed, rc (0x%x)\n", rc));
4368 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
4369 N_("Nonexistent host networking interface, name '%ls'"),
4370 HostOnlyName.raw());
4371 }
4372
4373 char szNetwork[INTNET_MAX_NETWORK_NAME];
4374 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHostOnlyName);
4375
4376#if defined(RT_OS_WINDOWS)
4377# ifndef VBOX_WITH_NETFLT
4378 hrc = E_NOTIMPL;
4379 LogRel(("NetworkAttachmentType_HostOnly: Not Implemented\n"));
4380 H();
4381# else /* defined VBOX_WITH_NETFLT*/
4382 /** @todo r=bird: Put this in a function. */
4383
4384 HostNetworkInterfaceType_T eIfType;
4385 hrc = hostInterface->COMGETTER(InterfaceType)(&eIfType);
4386 if (FAILED(hrc))
4387 {
4388 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(InterfaceType) failed, hrc (0x%x)\n", hrc));
4389 H();
4390 }
4391
4392 if (eIfType != HostNetworkInterfaceType_HostOnly)
4393 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
4394 N_("Interface ('%ls') is not a Host-Only Adapter interface"),
4395 HostOnlyName.raw());
4396
4397 hrc = hostInterface->COMGETTER(Id)(bstr.asOutParam());
4398 if (FAILED(hrc))
4399 {
4400 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(Id) failed, hrc (0x%x)\n", hrc));
4401 H();
4402 }
4403 Guid hostIFGuid(bstr);
4404
4405 INetCfg *pNc;
4406 ComPtr<INetCfgComponent> pAdaptorComponent;
4407 LPWSTR pszApp;
4408 hrc = VBoxNetCfgWinQueryINetCfg(&pNc, FALSE, L"VirtualBox", 10, &pszApp);
4409 Assert(hrc == S_OK);
4410 if (hrc != S_OK)
4411 {
4412 LogRel(("NetworkAttachmentType_HostOnly: Failed to get NetCfg, hrc=%Rhrc (0x%x)\n", hrc, hrc));
4413 H();
4414 }
4415
4416 /* get the adapter's INetCfgComponent*/
4417 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.raw(), pAdaptorComponent.asOutParam());
4418 if (hrc != S_OK)
4419 {
4420 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
4421 LogRel(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc=%Rhrc (0x%x)\n", hrc, hrc));
4422 H();
4423 }
4424# define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\"
4425 char szTrunkName[INTNET_MAX_TRUNK_NAME];
4426 char *pszTrunkName = szTrunkName;
4427 wchar_t * pswzBindName;
4428 hrc = pAdaptorComponent->GetBindName(&pswzBindName);
4429 Assert(hrc == S_OK);
4430 if (hrc == S_OK)
4431 {
4432 int cwBindName = (int)wcslen(pswzBindName) + 1;
4433 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX);
4434 if (sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName)
4435 {
4436 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX);
4437 pszTrunkName += cbFullBindNamePrefix-1;
4438 if (!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName,
4439 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL))
4440 {
4441 DWORD err = GetLastError();
4442 hrc = HRESULT_FROM_WIN32(err);
4443 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: WideCharToMultiByte failed, hr=%Rhrc (0x%x) err=%u\n", hrc, hrc, err));
4444 }
4445 }
4446 else
4447 {
4448 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: insufficient szTrunkName buffer space\n"));
4449 /** @todo set appropriate error code */
4450 hrc = E_FAIL;
4451 }
4452
4453 if (hrc != S_OK)
4454 {
4455 AssertFailed();
4456 CoTaskMemFree(pswzBindName);
4457 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
4458 H();
4459 }
4460 }
4461 else
4462 {
4463 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
4464 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc=%Rhrc (0x%x)\n", hrc, hrc));
4465 H();
4466 }
4467
4468
4469 CoTaskMemFree(pswzBindName);
4470
4471 pAdaptorComponent.setNull();
4472 /* release the pNc finally */
4473 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
4474
4475 const char *pszTrunk = szTrunkName;
4476
4477 InsertConfigInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp);
4478 InsertConfigString(pCfg, "Trunk", pszTrunk);
4479 InsertConfigString(pCfg, "Network", szNetwork);
4480 InsertConfigInteger(pCfg, "IgnoreConnectFailure", (uint64_t)fIgnoreConnectFailure); /** @todo why is this windows only?? */
4481 networkName = Bstr(szNetwork);
4482 trunkName = Bstr(pszTrunk);
4483 trunkType = TRUNKTYPE_NETADP;
4484# endif /* defined VBOX_WITH_NETFLT*/
4485#elif defined(RT_OS_DARWIN)
4486 InsertConfigString(pCfg, "Trunk", pszHostOnlyName);
4487 InsertConfigString(pCfg, "Network", szNetwork);
4488 InsertConfigInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp);
4489 networkName = Bstr(szNetwork);
4490 trunkName = Bstr(pszHostOnlyName);
4491 trunkType = TRUNKTYPE_NETADP;
4492#else
4493 InsertConfigString(pCfg, "Trunk", pszHostOnlyName);
4494 InsertConfigString(pCfg, "Network", szNetwork);
4495 InsertConfigInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt);
4496 networkName = Bstr(szNetwork);
4497 trunkName = Bstr(pszHostOnlyName);
4498 trunkType = TRUNKTYPE_NETFLT;
4499#endif
4500 InsertConfigString(pCfg, "IfPolicyPromisc", pszPromiscuousGuestPolicy);
4501
4502#if !defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT)
4503
4504 Bstr tmpAddr, tmpMask;
4505
4506 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPAddress",
4507 pszHostOnlyName).raw(),
4508 tmpAddr.asOutParam());
4509 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty())
4510 {
4511 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPNetMask",
4512 pszHostOnlyName).raw(),
4513 tmpMask.asOutParam());
4514 if (SUCCEEDED(hrc) && !tmpMask.isEmpty())
4515 hrc = hostInterface->EnableStaticIPConfig(tmpAddr.raw(),
4516 tmpMask.raw());
4517 else
4518 hrc = hostInterface->EnableStaticIPConfig(tmpAddr.raw(),
4519 Bstr(VBOXNET_IPV4MASK_DEFAULT).raw());
4520 }
4521 else
4522 {
4523 /* Grab the IP number from the 'vboxnetX' instance number (see netif.h) */
4524 hrc = hostInterface->EnableStaticIPConfig(getDefaultIPv4Address(Bstr(pszHostOnlyName)).raw(),
4525 Bstr(VBOXNET_IPV4MASK_DEFAULT).raw());
4526 }
4527
4528 ComAssertComRC(hrc); /** @todo r=bird: Why this isn't fatal? (H()) */
4529
4530 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPV6Address",
4531 pszHostOnlyName).raw(),
4532 tmpAddr.asOutParam());
4533 if (SUCCEEDED(hrc))
4534 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPV6NetMask", pszHostOnlyName).raw(),
4535 tmpMask.asOutParam());
4536 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty() && !tmpMask.isEmpty())
4537 {
4538 hrc = hostInterface->EnableStaticIPConfigV6(tmpAddr.raw(),
4539 Utf8Str(tmpMask).toUInt32());
4540 ComAssertComRC(hrc); /** @todo r=bird: Why this isn't fatal? (H()) */
4541 }
4542#endif
4543 break;
4544 }
4545
4546 case NetworkAttachmentType_Generic:
4547 {
4548 hrc = aNetworkAdapter->COMGETTER(GenericDriver)(bstr.asOutParam()); H();
4549 SafeArray<BSTR> names;
4550 SafeArray<BSTR> values;
4551 hrc = aNetworkAdapter->GetProperties(Bstr().raw(),
4552 ComSafeArrayAsOutParam(names),
4553 ComSafeArrayAsOutParam(values)); H();
4554
4555 InsertConfigString(pLunL0, "Driver", bstr);
4556 InsertConfigNode(pLunL0, "Config", &pCfg);
4557 for (size_t ii = 0; ii < names.size(); ++ii)
4558 {
4559 if (values[ii] && *values[ii])
4560 {
4561 Utf8Str name = names[ii];
4562 Utf8Str value = values[ii];
4563 InsertConfigString(pCfg, name.c_str(), value);
4564 }
4565 }
4566 break;
4567 }
4568
4569 default:
4570 AssertMsgFailed(("should not get here!\n"));
4571 break;
4572 }
4573
4574 /*
4575 * Attempt to attach the driver.
4576 */
4577 switch (eAttachmentType)
4578 {
4579 case NetworkAttachmentType_Null:
4580 break;
4581
4582 case NetworkAttachmentType_Bridged:
4583 case NetworkAttachmentType_Internal:
4584 case NetworkAttachmentType_HostOnly:
4585 case NetworkAttachmentType_NAT:
4586 case NetworkAttachmentType_Generic:
4587 {
4588 if (SUCCEEDED(hrc) && SUCCEEDED(rc))
4589 {
4590 if (fAttachDetach)
4591 {
4592 rc = PDMR3DriverAttach(pVM, pszDevice, uInstance, uLun, 0 /*fFlags*/, NULL /* ppBase */);
4593 //AssertRC(rc);
4594 }
4595
4596 {
4597 /** @todo pritesh: get the dhcp server name from the
4598 * previous network configuration and then stop the server
4599 * else it may conflict with the dhcp server running with
4600 * the current attachment type
4601 */
4602 /* Stop the hostonly DHCP Server */
4603 }
4604
4605 if (!networkName.isEmpty())
4606 {
4607 /*
4608 * Until we implement service reference counters DHCP Server will be stopped
4609 * by DHCPServerRunner destructor.
4610 */
4611 ComPtr<IDHCPServer> dhcpServer;
4612 hrc = virtualBox->FindDHCPServerByNetworkName(networkName.raw(),
4613 dhcpServer.asOutParam());
4614 if (SUCCEEDED(hrc))
4615 {
4616 /* there is a DHCP server available for this network */
4617 BOOL fEnabledDhcp;
4618 hrc = dhcpServer->COMGETTER(Enabled)(&fEnabledDhcp);
4619 if (FAILED(hrc))
4620 {
4621 LogRel(("DHCP svr: COMGETTER(Enabled) failed, hrc (%Rhrc)", hrc));
4622 H();
4623 }
4624
4625 if (fEnabledDhcp)
4626 hrc = dhcpServer->Start(networkName.raw(),
4627 trunkName.raw(),
4628 trunkType.raw());
4629 }
4630 else
4631 hrc = S_OK;
4632 }
4633 }
4634
4635 break;
4636 }
4637
4638 default:
4639 AssertMsgFailed(("should not get here!\n"));
4640 break;
4641 }
4642
4643 meAttachmentType[uInstance] = eAttachmentType;
4644 }
4645 catch (ConfigError &x)
4646 {
4647 // InsertConfig threw something:
4648 return x.m_vrc;
4649 }
4650
4651#undef H
4652
4653 return VINF_SUCCESS;
4654}
4655
4656#ifdef VBOX_WITH_GUEST_PROPS
4657/**
4658 * Set an array of guest properties
4659 */
4660static void configSetProperties(VMMDev * const pVMMDev,
4661 void *names,
4662 void *values,
4663 void *timestamps,
4664 void *flags)
4665{
4666 VBOXHGCMSVCPARM parms[4];
4667
4668 parms[0].type = VBOX_HGCM_SVC_PARM_PTR;
4669 parms[0].u.pointer.addr = names;
4670 parms[0].u.pointer.size = 0; /* We don't actually care. */
4671 parms[1].type = VBOX_HGCM_SVC_PARM_PTR;
4672 parms[1].u.pointer.addr = values;
4673 parms[1].u.pointer.size = 0; /* We don't actually care. */
4674 parms[2].type = VBOX_HGCM_SVC_PARM_PTR;
4675 parms[2].u.pointer.addr = timestamps;
4676 parms[2].u.pointer.size = 0; /* We don't actually care. */
4677 parms[3].type = VBOX_HGCM_SVC_PARM_PTR;
4678 parms[3].u.pointer.addr = flags;
4679 parms[3].u.pointer.size = 0; /* We don't actually care. */
4680
4681 pVMMDev->hgcmHostCall("VBoxGuestPropSvc",
4682 guestProp::SET_PROPS_HOST,
4683 4,
4684 &parms[0]);
4685}
4686
4687/**
4688 * Set a single guest property
4689 */
4690static void configSetProperty(VMMDev * const pVMMDev,
4691 const char *pszName,
4692 const char *pszValue,
4693 const char *pszFlags)
4694{
4695 VBOXHGCMSVCPARM parms[4];
4696
4697 AssertPtrReturnVoid(pszName);
4698 AssertPtrReturnVoid(pszValue);
4699 AssertPtrReturnVoid(pszFlags);
4700 parms[0].type = VBOX_HGCM_SVC_PARM_PTR;
4701 parms[0].u.pointer.addr = (void *)pszName;
4702 parms[0].u.pointer.size = strlen(pszName) + 1;
4703 parms[1].type = VBOX_HGCM_SVC_PARM_PTR;
4704 parms[1].u.pointer.addr = (void *)pszValue;
4705 parms[1].u.pointer.size = strlen(pszValue) + 1;
4706 parms[2].type = VBOX_HGCM_SVC_PARM_PTR;
4707 parms[2].u.pointer.addr = (void *)pszFlags;
4708 parms[2].u.pointer.size = strlen(pszFlags) + 1;
4709 pVMMDev->hgcmHostCall("VBoxGuestPropSvc", guestProp::SET_PROP_HOST, 3,
4710 &parms[0]);
4711}
4712
4713/**
4714 * Set the global flags value by calling the service
4715 * @returns the status returned by the call to the service
4716 *
4717 * @param pTable the service instance handle
4718 * @param eFlags the flags to set
4719 */
4720int configSetGlobalPropertyFlags(VMMDev * const pVMMDev,
4721 guestProp::ePropFlags eFlags)
4722{
4723 VBOXHGCMSVCPARM paParm;
4724 paParm.setUInt32(eFlags);
4725 int rc = pVMMDev->hgcmHostCall("VBoxGuestPropSvc",
4726 guestProp::SET_GLOBAL_FLAGS_HOST, 1,
4727 &paParm);
4728 if (RT_FAILURE(rc))
4729 {
4730 char szFlags[guestProp::MAX_FLAGS_LEN];
4731 if (RT_FAILURE(writeFlags(eFlags, szFlags)))
4732 Log(("Failed to set the global flags.\n"));
4733 else
4734 Log(("Failed to set the global flags \"%s\".\n", szFlags));
4735 }
4736 return rc;
4737}
4738#endif /* VBOX_WITH_GUEST_PROPS */
4739
4740/**
4741 * Set up the Guest Property service, populate it with properties read from
4742 * the machine XML and set a couple of initial properties.
4743 */
4744/* static */ int Console::configGuestProperties(void *pvConsole, PVM pVM)
4745{
4746#ifdef VBOX_WITH_GUEST_PROPS
4747 AssertReturn(pvConsole, VERR_GENERAL_FAILURE);
4748 ComObjPtr<Console> pConsole = static_cast<Console *>(pvConsole);
4749 AssertReturn(pConsole->m_pVMMDev, VERR_GENERAL_FAILURE);
4750
4751 /* Load the service */
4752 int rc = pConsole->m_pVMMDev->hgcmLoadService("VBoxGuestPropSvc", "VBoxGuestPropSvc");
4753
4754 if (RT_FAILURE(rc))
4755 {
4756 LogRel(("VBoxGuestPropSvc is not available. rc = %Rrc\n", rc));
4757 /* That is not a fatal failure. */
4758 rc = VINF_SUCCESS;
4759 }
4760 else
4761 {
4762 /*
4763 * Initialize built-in properties that can be changed and saved.
4764 *
4765 * These are typically transient properties that the guest cannot
4766 * change.
4767 */
4768
4769 {
4770 VBOXHGCMSVCPARM Params[2];
4771 int rc2 = pConsole->m_pVMMDev->hgcmHostCall("VBoxGuestPropSvc", guestProp::GET_DBGF_INFO_FN, 2, &Params[0]);
4772 if (RT_SUCCESS(rc2))
4773 {
4774 PFNDBGFHANDLEREXT pfnHandler = (PFNDBGFHANDLEREXT)(uintptr_t)Params[0].u.pointer.addr;
4775 void *pService = (void*)Params[1].u.pointer.addr;
4776 DBGFR3InfoRegisterExternal(pVM, "guestprops", "Display the guest properties", pfnHandler, pService);
4777 }
4778 }
4779
4780 /* Sysprep execution by VBoxService. */
4781 configSetProperty(pConsole->m_pVMMDev,
4782 "/VirtualBox/HostGuest/SysprepExec", "",
4783 "TRANSIENT, RDONLYGUEST");
4784 configSetProperty(pConsole->m_pVMMDev,
4785 "/VirtualBox/HostGuest/SysprepArgs", "",
4786 "TRANSIENT, RDONLYGUEST");
4787
4788 /*
4789 * Pull over the properties from the server.
4790 */
4791 SafeArray<BSTR> namesOut;
4792 SafeArray<BSTR> valuesOut;
4793 SafeArray<LONG64> timestampsOut;
4794 SafeArray<BSTR> flagsOut;
4795 HRESULT hrc;
4796 hrc = pConsole->mControl->PullGuestProperties(ComSafeArrayAsOutParam(namesOut),
4797 ComSafeArrayAsOutParam(valuesOut),
4798 ComSafeArrayAsOutParam(timestampsOut),
4799 ComSafeArrayAsOutParam(flagsOut));
4800 AssertMsgReturn(SUCCEEDED(hrc), ("hrc=%Rrc\n", hrc), VERR_GENERAL_FAILURE);
4801 size_t cProps = namesOut.size();
4802 size_t cAlloc = cProps + 1;
4803 if ( valuesOut.size() != cProps
4804 || timestampsOut.size() != cProps
4805 || flagsOut.size() != cProps
4806 )
4807 AssertFailedReturn(VERR_INVALID_PARAMETER);
4808
4809 char **papszNames, **papszValues, **papszFlags;
4810 char szEmpty[] = "";
4811 LONG64 *pai64Timestamps;
4812 papszNames = (char **)RTMemTmpAllocZ(sizeof(void *) * cAlloc);
4813 papszValues = (char **)RTMemTmpAllocZ(sizeof(void *) * cAlloc);
4814 pai64Timestamps = (LONG64 *)RTMemTmpAllocZ(sizeof(LONG64) * cAlloc);
4815 papszFlags = (char **)RTMemTmpAllocZ(sizeof(void *) * cAlloc);
4816 if (papszNames && papszValues && pai64Timestamps && papszFlags)
4817 {
4818 for (unsigned i = 0; RT_SUCCESS(rc) && i < cProps; ++i)
4819 {
4820 AssertPtrReturn(namesOut[i], VERR_INVALID_PARAMETER);
4821 rc = RTUtf16ToUtf8(namesOut[i], &papszNames[i]);
4822 if (RT_FAILURE(rc))
4823 break;
4824 if (valuesOut[i])
4825 rc = RTUtf16ToUtf8(valuesOut[i], &papszValues[i]);
4826 else
4827 papszValues[i] = szEmpty;
4828 if (RT_FAILURE(rc))
4829 break;
4830 pai64Timestamps[i] = timestampsOut[i];
4831 if (flagsOut[i])
4832 rc = RTUtf16ToUtf8(flagsOut[i], &papszFlags[i]);
4833 else
4834 papszFlags[i] = szEmpty;
4835 }
4836 if (RT_SUCCESS(rc))
4837 configSetProperties(pConsole->m_pVMMDev,
4838 (void *)papszNames,
4839 (void *)papszValues,
4840 (void *)pai64Timestamps,
4841 (void *)papszFlags);
4842 for (unsigned i = 0; i < cProps; ++i)
4843 {
4844 RTStrFree(papszNames[i]);
4845 if (valuesOut[i])
4846 RTStrFree(papszValues[i]);
4847 if (flagsOut[i])
4848 RTStrFree(papszFlags[i]);
4849 }
4850 }
4851 else
4852 rc = VERR_NO_MEMORY;
4853 RTMemTmpFree(papszNames);
4854 RTMemTmpFree(papszValues);
4855 RTMemTmpFree(pai64Timestamps);
4856 RTMemTmpFree(papszFlags);
4857 AssertRCReturn(rc, rc);
4858
4859 /*
4860 * These properties have to be set before pulling over the properties
4861 * from the machine XML, to ensure that properties saved in the XML
4862 * will override them.
4863 */
4864 /* Set the raw VBox version string as a guest property. Used for host/guest
4865 * version comparison. */
4866 configSetProperty(pConsole->m_pVMMDev, "/VirtualBox/HostInfo/VBoxVer",
4867 VBOX_VERSION_STRING_RAW, "TRANSIENT, RDONLYGUEST");
4868 /* Set the full VBox version string as a guest property. Can contain vendor-specific
4869 * information/branding and/or pre-release tags. */
4870 configSetProperty(pConsole->m_pVMMDev, "/VirtualBox/HostInfo/VBoxVerExt",
4871 VBOX_VERSION_STRING, "TRANSIENT, RDONLYGUEST");
4872 /* Set the VBox SVN revision as a guest property */
4873 configSetProperty(pConsole->m_pVMMDev, "/VirtualBox/HostInfo/VBoxRev",
4874 RTBldCfgRevisionStr(), "TRANSIENT, RDONLYGUEST");
4875
4876 /*
4877 * Register the host notification callback
4878 */
4879 HGCMSVCEXTHANDLE hDummy;
4880 HGCMHostRegisterServiceExtension(&hDummy, "VBoxGuestPropSvc",
4881 Console::doGuestPropNotification,
4882 pvConsole);
4883
4884#ifdef VBOX_WITH_GUEST_PROPS_RDONLY_GUEST
4885 rc = configSetGlobalPropertyFlags(pConsole->m_pVMMDev,
4886 guestProp::RDONLYGUEST);
4887 AssertRCReturn(rc, rc);
4888#endif
4889
4890 Log(("Set VBoxGuestPropSvc property store\n"));
4891 }
4892 return VINF_SUCCESS;
4893#else /* !VBOX_WITH_GUEST_PROPS */
4894 return VERR_NOT_SUPPORTED;
4895#endif /* !VBOX_WITH_GUEST_PROPS */
4896}
4897
4898/**
4899 * Set up the Guest Control service.
4900 */
4901/* static */ int Console::configGuestControl(void *pvConsole)
4902{
4903#ifdef VBOX_WITH_GUEST_CONTROL
4904 AssertReturn(pvConsole, VERR_GENERAL_FAILURE);
4905 ComObjPtr<Console> pConsole = static_cast<Console *>(pvConsole);
4906
4907 /* Load the service */
4908 int rc = pConsole->m_pVMMDev->hgcmLoadService("VBoxGuestControlSvc", "VBoxGuestControlSvc");
4909
4910 if (RT_FAILURE(rc))
4911 {
4912 LogRel(("VBoxGuestControlSvc is not available. rc = %Rrc\n", rc));
4913 /* That is not a fatal failure. */
4914 rc = VINF_SUCCESS;
4915 }
4916 else
4917 {
4918 HGCMSVCEXTHANDLE hDummy;
4919 rc = HGCMHostRegisterServiceExtension(&hDummy, "VBoxGuestControlSvc",
4920 &Guest::notifyCtrlDispatcher,
4921 pConsole->getGuest());
4922 if (RT_FAILURE(rc))
4923 Log(("Cannot register VBoxGuestControlSvc extension!\n"));
4924 else
4925 Log(("VBoxGuestControlSvc loaded\n"));
4926 }
4927
4928 return rc;
4929#else /* !VBOX_WITH_GUEST_CONTROL */
4930 return VERR_NOT_SUPPORTED;
4931#endif /* !VBOX_WITH_GUEST_CONTROL */
4932}
Note: See TracBrowser for help on using the repository browser.

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