VirtualBox

source: vbox/trunk/src/VBox/Main/ConsoleImpl2.cpp@ 29423

Last change on this file since 29423 was 29346, checked in by vboxsync, 15 years ago

Main: better error message for inaccessible ISO

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 170.5 KB
Line 
1/* $Id: ConsoleImpl2.cpp 29346 2010-05-11 12:52:28Z vboxsync $ */
2/** @file
3 * VBox Console COM Class implementation
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-2010 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#include "ConsoleImpl.h"
27#include "DisplayImpl.h"
28#ifdef VBOX_WITH_GUEST_CONTROL
29# include "GuestImpl.h"
30#endif
31#include "VMMDev.h"
32
33// generated header
34#include "SchemaDefs.h"
35
36#include "AutoCaller.h"
37#include "Logging.h"
38
39#include <iprt/buildconfig.h>
40#include <iprt/string.h>
41#include <iprt/path.h>
42#include <iprt/dir.h>
43#include <iprt/param.h>
44#if 0 /* enable to play with lots of memory. */
45# include <iprt/env.h>
46#endif
47#include <iprt/file.h>
48
49#include <VBox/vmapi.h>
50#include <VBox/err.h>
51#include <VBox/param.h>
52#include <VBox/pdmapi.h> /* For PDMR3DriverAttach/PDMR3DriverDetach */
53#include <VBox/version.h>
54#include <VBox/HostServices/VBoxClipboardSvc.h>
55#ifdef VBOX_WITH_CROGL
56# include <VBox/HostServices/VBoxCrOpenGLSvc.h>
57#endif
58#ifdef VBOX_WITH_GUEST_PROPS
59# include <VBox/HostServices/GuestPropertySvc.h>
60# include <VBox/com/defs.h>
61# include <VBox/com/array.h>
62# include <hgcm/HGCM.h> /** @todo it should be possible to register a service
63 * extension using a VMMDev callback. */
64# include <vector>
65#endif /* VBOX_WITH_GUEST_PROPS */
66#include <VBox/intnet.h>
67
68#include <VBox/com/com.h>
69#include <VBox/com/string.h>
70#include <VBox/com/array.h>
71
72#ifdef VBOX_WITH_NETFLT
73# if defined(RT_OS_SOLARIS)
74# include <zone.h>
75# elif defined(RT_OS_LINUX)
76# include <unistd.h>
77# include <sys/ioctl.h>
78# include <sys/socket.h>
79# include <linux/types.h>
80# include <linux/if.h>
81# include <linux/wireless.h>
82# elif defined(RT_OS_FREEBSD)
83# include <unistd.h>
84# include <sys/types.h>
85# include <sys/ioctl.h>
86# include <sys/socket.h>
87# include <net/if.h>
88# include <net80211/ieee80211_ioctl.h>
89# endif
90# if defined(RT_OS_WINDOWS)
91# include <VBox/WinNetConfig.h>
92# include <Ntddndis.h>
93# include <devguid.h>
94# else
95# include <HostNetworkInterfaceImpl.h>
96# include <netif.h>
97# include <stdlib.h>
98# endif
99#endif /* VBOX_WITH_NETFLT */
100
101#include "DHCPServerRunner.h"
102
103#if defined(RT_OS_DARWIN) && !defined(VBOX_OSE)
104
105# include "IOKit/IOKitLib.h"
106
107static int DarwinSmcKey(char *aKey, uint32_t iKeySize)
108{
109 /*
110 * Method as described in Amit Singh's article:
111 * http://osxbook.com/book/bonus/chapter7/tpmdrmmyth/
112 */
113 typedef struct
114 {
115 uint32_t key;
116 uint8_t pad0[22];
117 uint32_t datasize;
118 uint8_t pad1[10];
119 uint8_t cmd;
120 uint32_t pad2;
121 uint8_t data[32];
122 } AppleSMCBuffer;
123
124 AssertReturn(iKeySize >= 65, VERR_INTERNAL_ERROR);
125
126 io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault,
127 IOServiceMatching("AppleSMC"));
128 if (!service)
129 return VERR_INTERNAL_ERROR;
130
131 io_connect_t port = (io_connect_t)0;
132 kern_return_t kr = IOServiceOpen(service, mach_task_self(), 0, &port);
133 IOObjectRelease(service);
134
135 if (kr != kIOReturnSuccess)
136 return VERR_INTERNAL_ERROR;
137
138 AppleSMCBuffer inputStruct = { 0, {0}, 32, {0}, 5, };
139 AppleSMCBuffer outputStruct;
140 size_t cbOutputStruct = sizeof(outputStruct);
141
142 for (int i = 0; i < 2; i++)
143 {
144 inputStruct.key = (uint32_t)((i == 0) ? 'OSK0' : 'OSK1');
145 kr = IOConnectCallStructMethod((mach_port_t)port,
146 (uint32_t)2,
147 (const void *)&inputStruct,
148 sizeof(inputStruct),
149 (void *)&outputStruct,
150 &cbOutputStruct);
151 if (kr != kIOReturnSuccess)
152 {
153 IOServiceClose(port);
154 return VERR_INTERNAL_ERROR;
155 }
156
157 for (int j = 0; j < 32; j++)
158 aKey[j + i*32] = outputStruct.data[j];
159 }
160
161 IOServiceClose(port);
162
163 aKey[64] = 0;
164
165 return VINF_SUCCESS;
166}
167
168#endif /* RT_OS_DARWIN */
169
170/* Darwin compile cludge */
171#undef PVM
172
173/* Comment out the following line to remove VMWare compatibility hack. */
174#define VMWARE_NET_IN_SLOT_11
175
176/**
177 * Translate IDE StorageControllerType_T to string representation.
178 */
179const char* controllerString(StorageControllerType_T enmType)
180{
181 switch (enmType)
182 {
183 case StorageControllerType_PIIX3:
184 return "PIIX3";
185 case StorageControllerType_PIIX4:
186 return "PIIX4";
187 case StorageControllerType_ICH6:
188 return "ICH6";
189 default:
190 return "Unknown";
191 }
192}
193
194/**
195 * Simple class for storing network boot information.
196 */
197struct BootNic
198{
199 ULONG mInstance;
200 unsigned mPciDev;
201 unsigned mPciFn;
202 ULONG mBootPrio;
203 bool operator < (const BootNic &rhs) const
204 {
205 ULONG lval = mBootPrio - 1; /* 0 will wrap around and get the lowest priority. */
206 ULONG rval = rhs.mBootPrio - 1;
207 return lval < rval; /* Zero compares as highest number (lowest prio). */
208 }
209};
210
211/*
212 * VC++ 8 / amd64 has some serious trouble with this function.
213 * As a temporary measure, we'll drop global optimizations.
214 */
215#if defined(_MSC_VER) && defined(RT_ARCH_AMD64)
216# pragma optimize("g", off)
217#endif
218
219static int findEfiRom(IVirtualBox* vbox, FirmwareType_T aFirmwareType, Utf8Str& aEfiRomFile)
220{
221 int rc;
222 BOOL fPresent = FALSE;
223 Bstr aFilePath, empty;
224
225 rc = vbox->CheckFirmwarePresent(aFirmwareType, empty,
226 empty.asOutParam(), aFilePath.asOutParam(), &fPresent);
227 if (RT_FAILURE(rc))
228 AssertComRCReturn(rc, VERR_FILE_NOT_FOUND);
229
230 if (!fPresent)
231 return VERR_FILE_NOT_FOUND;
232
233 aEfiRomFile = Utf8Str(aFilePath);
234
235 return S_OK;
236}
237
238static int getSmcDeviceKey(IMachine* pMachine, BSTR * aKey)
239{
240# if defined(RT_OS_DARWIN) && !defined(VBOX_OSE)
241 int rc;
242 char aKeyBuf[65];
243
244 rc = DarwinSmcKey(aKeyBuf, sizeof aKeyBuf);
245 if (SUCCEEDED(rc))
246 {
247 Bstr(aKeyBuf).detachTo(aKey);
248 return rc;
249 }
250#endif
251
252 return pMachine->GetExtraData(Bstr("VBoxInternal2/SmcDeviceKey"), aKey);
253}
254
255/**
256 * Construct the VM configuration tree (CFGM).
257 *
258 * This is a callback for VMR3Create() call. It is called from CFGMR3Init()
259 * in the emulation thread (EMT). Any per thread COM/XPCOM initialization
260 * is done here.
261 *
262 * @param pVM VM handle.
263 * @param pvConsole Pointer to the VMPowerUpTask object.
264 * @return VBox status code.
265 *
266 * @note Locks the Console object for writing.
267 */
268DECLCALLBACK(int) Console::configConstructor(PVM pVM, void *pvConsole)
269{
270 LogFlowFuncEnter();
271 /* Note: hardcoded assumption about number of slots; see rom bios */
272 bool afPciDeviceNo[32] = {false};
273 bool fFdcEnabled = false;
274 BOOL fIs64BitGuest = false;
275
276#if !defined(VBOX_WITH_XPCOM)
277 {
278 /* initialize COM */
279 HRESULT hrc = CoInitializeEx(NULL,
280 COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE |
281 COINIT_SPEED_OVER_MEMORY);
282 LogFlow(("Console::configConstructor(): CoInitializeEx()=%08X\n", hrc));
283 AssertComRCReturn(hrc, VERR_GENERAL_FAILURE);
284 }
285#endif
286
287 AssertReturn(pvConsole, VERR_GENERAL_FAILURE);
288 ComObjPtr<Console> pConsole = static_cast<Console *>(pvConsole);
289
290 AutoCaller autoCaller(pConsole);
291 AssertComRCReturn(autoCaller.rc(), VERR_ACCESS_DENIED);
292
293 /* lock the console because we widely use internal fields and methods */
294 AutoWriteLock alock(pConsole COMMA_LOCKVAL_SRC_POS);
295
296 /* Save the VM pointer in the machine object */
297 pConsole->mpVM = pVM;
298
299 ComPtr<IMachine> pMachine = pConsole->machine();
300
301 int rc;
302 HRESULT hrc;
303 Bstr bstr;
304
305#define RC_CHECK() AssertMsgReturn(RT_SUCCESS(rc), ("rc=%Rrc\n", rc), rc)
306#define H() AssertMsgReturn(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), VERR_GENERAL_FAILURE)
307
308 /*
309 * Get necessary objects and frequently used parameters.
310 */
311 ComPtr<IVirtualBox> virtualBox;
312 hrc = pMachine->COMGETTER(Parent)(virtualBox.asOutParam()); H();
313
314 ComPtr<IHost> host;
315 hrc = virtualBox->COMGETTER(Host)(host.asOutParam()); H();
316
317 ComPtr<ISystemProperties> systemProperties;
318 hrc = virtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam()); H();
319
320 ComPtr<IBIOSSettings> biosSettings;
321 hrc = pMachine->COMGETTER(BIOSSettings)(biosSettings.asOutParam()); H();
322
323 hrc = pMachine->COMGETTER(HardwareUUID)(bstr.asOutParam()); H();
324 RTUUID HardwareUuid;
325 rc = RTUuidFromUtf16(&HardwareUuid, bstr.raw()); RC_CHECK();
326
327 ULONG cRamMBs;
328 hrc = pMachine->COMGETTER(MemorySize)(&cRamMBs); H();
329#if 0 /* enable to play with lots of memory. */
330 if (RTEnvExist("VBOX_RAM_SIZE"))
331 cRamMBs = RTStrToUInt64(RTEnvGet("VBOX_RAM_SIZE"));
332#endif
333 uint64_t const cbRam = cRamMBs * (uint64_t)_1M;
334 uint32_t const cbRamHole = MM_RAM_HOLE_SIZE_DEFAULT;
335
336 ULONG cCpus = 1;
337 hrc = pMachine->COMGETTER(CPUCount)(&cCpus); H();
338
339 Bstr osTypeId;
340 hrc = pMachine->COMGETTER(OSTypeId)(osTypeId.asOutParam()); H();
341
342 BOOL fIOAPIC;
343 hrc = biosSettings->COMGETTER(IOAPICEnabled)(&fIOAPIC); H();
344
345 ComPtr<IGuestOSType> guestOSType;
346 hrc = virtualBox->GetGuestOSType(osTypeId, guestOSType.asOutParam()); H();
347
348 Bstr guestTypeFamilyId;
349 hrc = guestOSType->COMGETTER(FamilyId)(guestTypeFamilyId.asOutParam()); H();
350 BOOL fOsXGuest = guestTypeFamilyId == Bstr("MacOS");
351
352 /*
353 * Get root node first.
354 * This is the only node in the tree.
355 */
356 PCFGMNODE pRoot = CFGMR3GetRoot(pVM);
357 Assert(pRoot);
358
359 /*
360 * Set the root (and VMM) level values.
361 */
362 hrc = pMachine->COMGETTER(Name)(bstr.asOutParam()); H();
363 rc = CFGMR3InsertStringW(pRoot, "Name", bstr.raw()); RC_CHECK();
364 rc = CFGMR3InsertBytes(pRoot, "UUID", &HardwareUuid, sizeof(HardwareUuid)); RC_CHECK();
365 rc = CFGMR3InsertInteger(pRoot, "RamSize", cbRam); RC_CHECK();
366 rc = CFGMR3InsertInteger(pRoot, "RamHoleSize", cbRamHole); RC_CHECK();
367 rc = CFGMR3InsertInteger(pRoot, "NumCPUs", cCpus); RC_CHECK();
368 rc = CFGMR3InsertInteger(pRoot, "TimerMillies", 10); RC_CHECK();
369#ifdef VBOX_WITH_RAW_MODE
370 rc = CFGMR3InsertInteger(pRoot, "RawR3Enabled", 1); /* boolean */ RC_CHECK();
371 rc = CFGMR3InsertInteger(pRoot, "RawR0Enabled", 1); /* boolean */ RC_CHECK();
372 /** @todo Config: RawR0, PATMEnabled and CSAMEnabled needs attention later. */
373 rc = CFGMR3InsertInteger(pRoot, "PATMEnabled", 1); /* boolean */ RC_CHECK();
374 rc = CFGMR3InsertInteger(pRoot, "CSAMEnabled", 1); /* boolean */ RC_CHECK();
375#endif
376
377 /* cpuid leaf overrides. */
378 static uint32_t const s_auCpuIdRanges[] =
379 {
380 UINT32_C(0x00000000), UINT32_C(0x0000000a),
381 UINT32_C(0x80000000), UINT32_C(0x8000000a)
382 };
383 for (unsigned i = 0; i < RT_ELEMENTS(s_auCpuIdRanges); i += 2)
384 for (uint32_t uLeaf = s_auCpuIdRanges[i]; uLeaf < s_auCpuIdRanges[i + 1]; uLeaf++)
385 {
386 ULONG ulEax, ulEbx, ulEcx, ulEdx;
387 hrc = pMachine->GetCPUIDLeaf(uLeaf, &ulEax, &ulEbx, &ulEcx, &ulEdx);
388 if (SUCCEEDED(hrc))
389 {
390 PCFGMNODE pLeaf;
391 rc = CFGMR3InsertNodeF(pRoot, &pLeaf, "CPUM/HostCPUID/%RX32", uLeaf); RC_CHECK();
392
393 rc = CFGMR3InsertInteger(pLeaf, "eax", ulEax); RC_CHECK();
394 rc = CFGMR3InsertInteger(pLeaf, "ebx", ulEbx); RC_CHECK();
395 rc = CFGMR3InsertInteger(pLeaf, "ecx", ulEcx); RC_CHECK();
396 rc = CFGMR3InsertInteger(pLeaf, "edx", ulEdx); RC_CHECK();
397 }
398 else if (hrc != E_INVALIDARG) H();
399 }
400
401 if (osTypeId == "WindowsNT4")
402 {
403 /*
404 * We must limit CPUID count for Windows NT 4, as otherwise it stops
405 * with error 0x3e (MULTIPROCESSOR_CONFIGURATION_NOT_SUPPORTED).
406 */
407 LogRel(("Limiting CPUID leaf count for NT4 guests\n"));
408 PCFGMNODE pCPUM;
409 rc = CFGMR3InsertNode(pRoot, "CPUM", &pCPUM); RC_CHECK();
410 rc = CFGMR3InsertInteger(pCPUM, "NT4LeafLimit", true); RC_CHECK();
411 }
412
413 if (fOsXGuest)
414 {
415 /*
416 * Expose extended MWAIT features to Mac OS X guests.
417 */
418 LogRel(("Using MWAIT extensions\n"));
419 PCFGMNODE pCPUM;
420 rc = CFGMR3InsertNode(pRoot, "CPUM", &pCPUM); RC_CHECK();
421 rc = CFGMR3InsertInteger(pCPUM, "MWaitExtensions", true); RC_CHECK();
422 }
423
424 /* hardware virtualization extensions */
425 BOOL fHWVirtExEnabled;
426 BOOL fHwVirtExtForced;
427#ifdef VBOX_WITH_RAW_MODE
428 hrc = pMachine->GetHWVirtExProperty(HWVirtExPropertyType_Enabled, &fHWVirtExEnabled); H();
429 if (cCpus > 1) /** @todo SMP: This isn't nice, but things won't work on mac otherwise. */
430 fHWVirtExEnabled = TRUE;
431# ifdef RT_OS_DARWIN
432 fHwVirtExtForced = fHWVirtExEnabled;
433# else
434 /* - With more than 4GB PGM will use different RAMRANGE sizes for raw
435 mode and hv mode to optimize lookup times.
436 - With more than one virtual CPU, raw-mode isn't a fallback option. */
437 fHwVirtExtForced = fHWVirtExEnabled
438 && ( cbRam > (_4G - cbRamHole)
439 || cCpus > 1);
440# endif
441#else /* !VBOX_WITH_RAW_MODE */
442 fHWVirtExEnabled = fHwVirtExtForced = TRUE;
443#endif /* !VBOX_WITH_RAW_MODE */
444 rc = CFGMR3InsertInteger(pRoot, "HwVirtExtForced", fHwVirtExtForced); RC_CHECK();
445
446 PCFGMNODE pHWVirtExt;
447 rc = CFGMR3InsertNode(pRoot, "HWVirtExt", &pHWVirtExt); RC_CHECK();
448 if (fHWVirtExEnabled)
449 {
450 rc = CFGMR3InsertInteger(pHWVirtExt, "Enabled", 1); RC_CHECK();
451
452 /* Indicate whether 64-bit guests are supported or not. */
453 /** @todo This is currently only forced off on 32-bit hosts only because it
454 * makes a lof of difference there (REM and Solaris performance).
455 */
456 BOOL fSupportsLongMode = false;
457 hrc = host->GetProcessorFeature(ProcessorFeature_LongMode,
458 &fSupportsLongMode); H();
459 hrc = guestOSType->COMGETTER(Is64Bit)(&fIs64BitGuest); H();
460
461 if (fSupportsLongMode && fIs64BitGuest)
462 {
463 rc = CFGMR3InsertInteger(pHWVirtExt, "64bitEnabled", 1); RC_CHECK();
464#if ARCH_BITS == 32 /* The recompiler must use VBoxREM64 (32-bit host only). */
465 PCFGMNODE pREM;
466 rc = CFGMR3InsertNode(pRoot, "REM", &pREM); RC_CHECK();
467 rc = CFGMR3InsertInteger(pREM, "64bitEnabled", 1); RC_CHECK();
468#endif
469 }
470#if ARCH_BITS == 32 /* 32-bit guests only. */
471 else
472 {
473 rc = CFGMR3InsertInteger(pHWVirtExt, "64bitEnabled", 0); RC_CHECK();
474 }
475#endif
476
477 /** @todo Not exactly pretty to check strings; VBOXOSTYPE would be better, but that requires quite a bit of API change in Main. */
478 if ( !fIs64BitGuest
479 && fIOAPIC
480 && ( osTypeId == "WindowsNT4"
481 || osTypeId == "Windows2000"
482 || osTypeId == "WindowsXP"
483 || osTypeId == "Windows2003"))
484 {
485 /* Only allow TPR patching for NT, Win2k, XP and Windows Server 2003. (32 bits mode)
486 * We may want to consider adding more guest OSes (Solaris) later on.
487 */
488 rc = CFGMR3InsertInteger(pHWVirtExt, "TPRPatchingEnabled", 1); RC_CHECK();
489 }
490 }
491
492 /* HWVirtEx exclusive mode */
493 BOOL fHWVirtExExclusive = true;
494 hrc = pMachine->GetHWVirtExProperty(HWVirtExPropertyType_Exclusive, &fHWVirtExExclusive); H();
495 rc = CFGMR3InsertInteger(pHWVirtExt, "Exclusive", fHWVirtExExclusive); RC_CHECK();
496
497 /* Nested paging (VT-x/AMD-V) */
498 BOOL fEnableNestedPaging = false;
499 hrc = pMachine->GetHWVirtExProperty(HWVirtExPropertyType_NestedPaging, &fEnableNestedPaging); H();
500 rc = CFGMR3InsertInteger(pHWVirtExt, "EnableNestedPaging", fEnableNestedPaging); RC_CHECK();
501
502 /* Large pages; requires nested paging */
503 BOOL fEnableLargePages = false;
504 hrc = pMachine->GetHWVirtExProperty(HWVirtExPropertyType_LargePages, &fEnableLargePages); H();
505 rc = CFGMR3InsertInteger(pHWVirtExt, "EnableLargePages", fEnableLargePages); RC_CHECK();
506
507 /* VPID (VT-x) */
508 BOOL fEnableVPID = false;
509 hrc = pMachine->GetHWVirtExProperty(HWVirtExPropertyType_VPID, &fEnableVPID); H();
510 rc = CFGMR3InsertInteger(pHWVirtExt, "EnableVPID", fEnableVPID); RC_CHECK();
511
512 /* Physical Address Extension (PAE) */
513 BOOL fEnablePAE = false;
514 hrc = pMachine->GetCPUProperty(CPUPropertyType_PAE, &fEnablePAE); H();
515 rc = CFGMR3InsertInteger(pRoot, "EnablePAE", fEnablePAE); RC_CHECK();
516
517 /* Synthetic CPU */
518 BOOL fSyntheticCpu = false;
519 hrc = pMachine->GetCPUProperty(CPUPropertyType_Synthetic, &fSyntheticCpu); H();
520 rc = CFGMR3InsertInteger(pRoot, "SyntheticCpu", fSyntheticCpu); RC_CHECK();
521
522 BOOL fPXEDebug;
523 hrc = biosSettings->COMGETTER(PXEDebugEnabled)(&fPXEDebug); H();
524
525 /*
526 * PDM config.
527 * Load drivers in VBoxC.[so|dll]
528 */
529 PCFGMNODE pPDM;
530 PCFGMNODE pDrivers;
531 PCFGMNODE pMod;
532 rc = CFGMR3InsertNode(pRoot, "PDM", &pPDM); RC_CHECK();
533 rc = CFGMR3InsertNode(pPDM, "Drivers", &pDrivers); RC_CHECK();
534 rc = CFGMR3InsertNode(pDrivers, "VBoxC", &pMod); RC_CHECK();
535#ifdef VBOX_WITH_XPCOM
536 // VBoxC is located in the components subdirectory
537 char szPathVBoxC[RTPATH_MAX];
538 rc = RTPathAppPrivateArch(szPathVBoxC, RTPATH_MAX - sizeof("/components/VBoxC")); AssertRC(rc);
539 strcat(szPathVBoxC, "/components/VBoxC");
540 rc = CFGMR3InsertString(pMod, "Path", szPathVBoxC); RC_CHECK();
541#else
542 rc = CFGMR3InsertString(pMod, "Path", "VBoxC"); RC_CHECK();
543#endif
544
545 /*
546 * I/O settings (cach, max bandwidth, ...).
547 */
548 PCFGMNODE pPDMAc;
549 PCFGMNODE pPDMAcFile;
550 rc = CFGMR3InsertNode(pPDM, "AsyncCompletion", &pPDMAc); RC_CHECK();
551 rc = CFGMR3InsertNode(pPDMAc, "File", &pPDMAcFile); RC_CHECK();
552
553 /* Builtin I/O cache */
554 BOOL fIoCache = true;
555 hrc = pMachine->COMGETTER(IoCacheEnabled)(&fIoCache); H();
556 rc = CFGMR3InsertInteger(pPDMAcFile, "CacheEnabled", fIoCache); RC_CHECK();
557
558 /* I/O cache size */
559 ULONG ioCacheSize = 5;
560 hrc = pMachine->COMGETTER(IoCacheSize)(&ioCacheSize); H();
561 rc = CFGMR3InsertInteger(pPDMAcFile, "CacheSize", ioCacheSize * _1M); RC_CHECK();
562
563 /* Maximum I/O bandwidth */
564 ULONG ioBandwidthMax = 0;
565 hrc = pMachine->COMGETTER(IoBandwidthMax)(&ioBandwidthMax); H();
566 if (ioBandwidthMax != 0)
567 {
568 rc = CFGMR3InsertInteger(pPDMAcFile, "VMTransferPerSecMax", ioBandwidthMax * _1M); RC_CHECK();
569 }
570
571 /*
572 * Devices
573 */
574 PCFGMNODE pDevices = NULL; /* /Devices */
575 PCFGMNODE pDev = NULL; /* /Devices/Dev/ */
576 PCFGMNODE pInst = NULL; /* /Devices/Dev/0/ */
577 PCFGMNODE pCfg = NULL; /* /Devices/Dev/.../Config/ */
578 PCFGMNODE pLunL0 = NULL; /* /Devices/Dev/0/LUN#0/ */
579 PCFGMNODE pLunL1 = NULL; /* /Devices/Dev/0/LUN#0/AttachedDriver/ */
580 PCFGMNODE pLunL2 = NULL; /* /Devices/Dev/0/LUN#0/AttachedDriver/Config/ */
581 PCFGMNODE pBiosCfg = NULL; /* /Devices/pcbios/0/Config/ */
582 PCFGMNODE pNetBootCfg = NULL; /* /Devices/pcbios/0/Config/NetBoot/ */
583
584 rc = CFGMR3InsertNode(pRoot, "Devices", &pDevices); RC_CHECK();
585
586 /*
587 * PC Arch.
588 */
589 rc = CFGMR3InsertNode(pDevices, "pcarch", &pDev); RC_CHECK();
590 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
591 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
592 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
593
594 /*
595 * The time offset
596 */
597 LONG64 timeOffset;
598 hrc = biosSettings->COMGETTER(TimeOffset)(&timeOffset); H();
599 PCFGMNODE pTMNode;
600 rc = CFGMR3InsertNode(pRoot, "TM", &pTMNode); RC_CHECK();
601 rc = CFGMR3InsertInteger(pTMNode, "UTCOffset", timeOffset * 1000000); RC_CHECK();
602
603 /*
604 * DMA
605 */
606 rc = CFGMR3InsertNode(pDevices, "8237A", &pDev); RC_CHECK();
607 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
608 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
609
610 /*
611 * PCI buses.
612 */
613 rc = CFGMR3InsertNode(pDevices, "pci", &pDev); /* piix3 */ RC_CHECK();
614 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
615 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
616 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
617 rc = CFGMR3InsertInteger(pCfg, "IOAPIC", fIOAPIC); RC_CHECK();
618
619#if 0 /* enable this to test PCI bridging */
620 rc = CFGMR3InsertNode(pDevices, "pcibridge", &pDev); RC_CHECK();
621 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
622 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
623 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
624 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 14); RC_CHECK();
625 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); RC_CHECK();
626 rc = CFGMR3InsertInteger(pInst, "PCIBusNo", 0);/* -> pci[0] */ RC_CHECK();
627
628 rc = CFGMR3InsertNode(pDev, "1", &pInst); RC_CHECK();
629 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
630 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
631 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 1); RC_CHECK();
632 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); RC_CHECK();
633 rc = CFGMR3InsertInteger(pInst, "PCIBusNo", 1);/* ->pcibridge[0] */ RC_CHECK();
634
635 rc = CFGMR3InsertNode(pDev, "2", &pInst); RC_CHECK();
636 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
637 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
638 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 3); RC_CHECK();
639 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); RC_CHECK();
640 rc = CFGMR3InsertInteger(pInst, "PCIBusNo", 1);/* ->pcibridge[0] */ RC_CHECK();
641#endif
642
643 /*
644 * Enable 3 following devices: HPET, SMC, LPC on MacOS X guests
645 */
646 /*
647 * High Precision Event Timer (HPET)
648 */
649 BOOL fHpetEnabled;
650#ifdef VBOX_WITH_HPET
651 /* Other guests may wish to use HPET too, but MacOS X not functional without it */
652 hrc = pMachine->COMGETTER(HpetEnabled)(&fHpetEnabled); H();
653 /* so always enable HPET in extended profile */
654 fHpetEnabled |= fOsXGuest;
655#else
656 fHpetEnabled = false;
657#endif
658 if (fHpetEnabled)
659 {
660 rc = CFGMR3InsertNode(pDevices, "hpet", &pDev); RC_CHECK();
661 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
662 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
663 }
664
665 /*
666 * System Management Controller (SMC)
667 */
668 BOOL fSmcEnabled;
669#ifdef VBOX_WITH_SMC
670 fSmcEnabled = fOsXGuest;
671#else
672 fSmcEnabled = false;
673#endif
674 if (fSmcEnabled)
675 {
676 Bstr tmpStr2;
677 rc = CFGMR3InsertNode(pDevices, "smc", &pDev); RC_CHECK();
678 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
679 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
680 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
681 rc = getSmcDeviceKey(pMachine, tmpStr2.asOutParam()); RC_CHECK();
682 rc = CFGMR3InsertString(pCfg, "DeviceKey", Utf8Str(tmpStr2).raw()); RC_CHECK();
683 }
684
685 /*
686 * Low Pin Count (LPC) bus
687 */
688 BOOL fLpcEnabled;
689 /** @todo: implement appropriate getter */
690#ifdef VBOX_WITH_LPC
691 fLpcEnabled = fOsXGuest;
692#else
693 fLpcEnabled = false;
694#endif
695 if (fLpcEnabled)
696 {
697 rc = CFGMR3InsertNode(pDevices, "lpc", &pDev); RC_CHECK();
698 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
699 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
700 }
701
702 /*
703 * PS/2 keyboard & mouse.
704 */
705 rc = CFGMR3InsertNode(pDevices, "pckbd", &pDev); RC_CHECK();
706 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
707 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
708 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
709
710 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
711 rc = CFGMR3InsertString(pLunL0, "Driver", "KeyboardQueue"); RC_CHECK();
712 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
713 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 64); RC_CHECK();
714
715 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); RC_CHECK();
716 rc = CFGMR3InsertString(pLunL1, "Driver", "MainKeyboard"); RC_CHECK();
717 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); RC_CHECK();
718 Keyboard *pKeyboard = pConsole->mKeyboard;
719 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)pKeyboard); RC_CHECK();
720
721 rc = CFGMR3InsertNode(pInst, "LUN#1", &pLunL0); RC_CHECK();
722 rc = CFGMR3InsertString(pLunL0, "Driver", "MouseQueue"); RC_CHECK();
723 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
724 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 128); RC_CHECK();
725
726 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); RC_CHECK();
727 rc = CFGMR3InsertString(pLunL1, "Driver", "MainMouse"); RC_CHECK();
728 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); RC_CHECK();
729 Mouse *pMouse = pConsole->mMouse;
730 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)pMouse); RC_CHECK();
731
732 /*
733 * i8254 Programmable Interval Timer And Dummy Speaker
734 */
735 rc = CFGMR3InsertNode(pDevices, "i8254", &pDev); RC_CHECK();
736 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
737 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
738#ifdef DEBUG
739 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
740#endif
741
742 /*
743 * i8259 Programmable Interrupt Controller.
744 */
745 rc = CFGMR3InsertNode(pDevices, "i8259", &pDev); RC_CHECK();
746 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
747 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
748 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
749
750 /*
751 * Advanced Programmable Interrupt Controller.
752 * SMP: Each CPU has a LAPIC, but we have a single device representing all LAPICs states,
753 * thus only single insert
754 */
755 rc = CFGMR3InsertNode(pDevices, "apic", &pDev); RC_CHECK();
756 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
757 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
758 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
759 rc = CFGMR3InsertInteger(pCfg, "IOAPIC", fIOAPIC); RC_CHECK();
760 rc = CFGMR3InsertInteger(pCfg, "NumCPUs", cCpus); RC_CHECK();
761
762 if (fIOAPIC)
763 {
764 /*
765 * I/O Advanced Programmable Interrupt Controller.
766 */
767 rc = CFGMR3InsertNode(pDevices, "ioapic", &pDev); RC_CHECK();
768 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
769 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
770 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
771 }
772
773 /*
774 * RTC MC146818.
775 */
776 rc = CFGMR3InsertNode(pDevices, "mc146818", &pDev); RC_CHECK();
777 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
778 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
779 BOOL fRTCUseUTC;
780 hrc = pMachine->COMGETTER(RTCUseUTC)(&fRTCUseUTC); H();
781 rc = CFGMR3InsertInteger(pCfg, "UseUTC", fRTCUseUTC ? 1 : 0); RC_CHECK();
782
783 /*
784 * VGA.
785 */
786 rc = CFGMR3InsertNode(pDevices, "vga", &pDev); RC_CHECK();
787 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
788 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
789 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 2); RC_CHECK();
790 Assert(!afPciDeviceNo[2]);
791 afPciDeviceNo[2] = true;
792 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); RC_CHECK();
793 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
794 ULONG cVRamMBs;
795 hrc = pMachine->COMGETTER(VRAMSize)(&cVRamMBs); H();
796 rc = CFGMR3InsertInteger(pCfg, "VRamSize", cVRamMBs * _1M); RC_CHECK();
797 ULONG cMonitorCount;
798 hrc = pMachine->COMGETTER(MonitorCount)(&cMonitorCount); H();
799 rc = CFGMR3InsertInteger(pCfg, "MonitorCount", cMonitorCount); RC_CHECK();
800#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
801 rc = CFGMR3InsertInteger(pCfg, "R0Enabled", fHWVirtExEnabled); RC_CHECK();
802#endif
803
804 /*
805 * BIOS logo
806 */
807 BOOL fFadeIn;
808 hrc = biosSettings->COMGETTER(LogoFadeIn)(&fFadeIn); H();
809 rc = CFGMR3InsertInteger(pCfg, "FadeIn", fFadeIn ? 1 : 0); RC_CHECK();
810 BOOL fFadeOut;
811 hrc = biosSettings->COMGETTER(LogoFadeOut)(&fFadeOut); H();
812 rc = CFGMR3InsertInteger(pCfg, "FadeOut", fFadeOut ? 1: 0); RC_CHECK();
813 ULONG logoDisplayTime;
814 hrc = biosSettings->COMGETTER(LogoDisplayTime)(&logoDisplayTime); H();
815 rc = CFGMR3InsertInteger(pCfg, "LogoTime", logoDisplayTime); RC_CHECK();
816 Bstr logoImagePath;
817 hrc = biosSettings->COMGETTER(LogoImagePath)(logoImagePath.asOutParam()); H();
818 rc = CFGMR3InsertString(pCfg, "LogoFile", logoImagePath ? Utf8Str(logoImagePath).c_str() : ""); RC_CHECK();
819
820 /*
821 * Boot menu
822 */
823 BIOSBootMenuMode_T eBootMenuMode;
824 int iShowBootMenu;
825 biosSettings->COMGETTER(BootMenuMode)(&eBootMenuMode);
826 switch (eBootMenuMode)
827 {
828 case BIOSBootMenuMode_Disabled: iShowBootMenu = 0; break;
829 case BIOSBootMenuMode_MenuOnly: iShowBootMenu = 1; break;
830 default: iShowBootMenu = 2; break;
831 }
832 rc = CFGMR3InsertInteger(pCfg, "ShowBootMenu", iShowBootMenu); RC_CHECK();
833
834 /* Custom VESA mode list */
835 unsigned cModes = 0;
836 for (unsigned iMode = 1; iMode <= 16; ++iMode)
837 {
838 char szExtraDataKey[sizeof("CustomVideoModeXX")];
839 RTStrPrintf(szExtraDataKey, sizeof(szExtraDataKey), "CustomVideoMode%u", iMode);
840 hrc = pMachine->GetExtraData(Bstr(szExtraDataKey), bstr.asOutParam()); H();
841 if (bstr.isEmpty())
842 break;
843 rc = CFGMR3InsertStringW(pCfg, szExtraDataKey, bstr.raw()); RC_CHECK();
844 ++cModes;
845 }
846 rc = CFGMR3InsertInteger(pCfg, "CustomVideoModes", cModes); RC_CHECK();
847
848 /* VESA height reduction */
849 ULONG ulHeightReduction;
850 IFramebuffer *pFramebuffer = pConsole->getDisplay()->getFramebuffer();
851 if (pFramebuffer)
852 {
853 hrc = pFramebuffer->COMGETTER(HeightReduction)(&ulHeightReduction); H();
854 }
855 else
856 {
857 /* If framebuffer is not available, there is no height reduction. */
858 ulHeightReduction = 0;
859 }
860 rc = CFGMR3InsertInteger(pCfg, "HeightReduction", ulHeightReduction); RC_CHECK();
861
862 /* Attach the display. */
863 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
864 rc = CFGMR3InsertString(pLunL0, "Driver", "MainDisplay"); RC_CHECK();
865 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
866 Display *pDisplay = pConsole->mDisplay;
867 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)pDisplay); RC_CHECK();
868
869
870 /*
871 * Firmware.
872 */
873 FirmwareType_T eFwType = FirmwareType_BIOS;
874 hrc = pMachine->COMGETTER(FirmwareType)(&eFwType); H();
875
876#ifdef VBOX_WITH_EFI
877 BOOL fEfiEnabled = (eFwType >= FirmwareType_EFI) && (eFwType <= FirmwareType_EFIDUAL);
878#else
879 BOOL fEfiEnabled = false;
880#endif
881 if (!fEfiEnabled)
882 {
883 /*
884 * PC Bios.
885 */
886 rc = CFGMR3InsertNode(pDevices, "pcbios", &pDev); RC_CHECK();
887 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
888 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
889 rc = CFGMR3InsertNode(pInst, "Config", &pBiosCfg); RC_CHECK();
890 rc = CFGMR3InsertInteger(pBiosCfg, "RamSize", cbRam); RC_CHECK();
891 rc = CFGMR3InsertInteger(pBiosCfg, "RamHoleSize", cbRamHole); RC_CHECK();
892 rc = CFGMR3InsertInteger(pBiosCfg, "NumCPUs", cCpus); RC_CHECK();
893 rc = CFGMR3InsertString(pBiosCfg, "HardDiskDevice", "piix3ide"); RC_CHECK();
894 rc = CFGMR3InsertString(pBiosCfg, "FloppyDevice", "i82078"); RC_CHECK();
895 rc = CFGMR3InsertInteger(pBiosCfg, "IOAPIC", fIOAPIC); RC_CHECK();
896 rc = CFGMR3InsertInteger(pBiosCfg, "PXEDebug", fPXEDebug); RC_CHECK();
897 rc = CFGMR3InsertBytes(pBiosCfg, "UUID", &HardwareUuid,sizeof(HardwareUuid));RC_CHECK();
898 rc = CFGMR3InsertNode(pBiosCfg, "NetBoot", &pNetBootCfg); RC_CHECK();
899
900 DeviceType_T bootDevice;
901 if (SchemaDefs::MaxBootPosition > 9)
902 {
903 AssertMsgFailed(("Too many boot devices %d\n",
904 SchemaDefs::MaxBootPosition));
905 return VERR_INVALID_PARAMETER;
906 }
907
908 for (ULONG pos = 1; pos <= SchemaDefs::MaxBootPosition; ++pos)
909 {
910 hrc = pMachine->GetBootOrder(pos, &bootDevice); H();
911
912 char szParamName[] = "BootDeviceX";
913 szParamName[sizeof(szParamName) - 2] = ((char (pos - 1)) + '0');
914
915 const char *pszBootDevice;
916 switch (bootDevice)
917 {
918 case DeviceType_Null:
919 pszBootDevice = "NONE";
920 break;
921 case DeviceType_HardDisk:
922 pszBootDevice = "IDE";
923 break;
924 case DeviceType_DVD:
925 pszBootDevice = "DVD";
926 break;
927 case DeviceType_Floppy:
928 pszBootDevice = "FLOPPY";
929 break;
930 case DeviceType_Network:
931 pszBootDevice = "LAN";
932 break;
933 default:
934 AssertMsgFailed(("Invalid bootDevice=%d\n", bootDevice));
935 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
936 N_("Invalid boot device '%d'"), bootDevice);
937 }
938 rc = CFGMR3InsertString(pBiosCfg, szParamName, pszBootDevice); RC_CHECK();
939 }
940 }
941 else
942 {
943 Utf8Str efiRomFile;
944
945 /* Autodetect firmware type, basing on guest type */
946 if (eFwType == FirmwareType_EFI)
947 {
948 eFwType =
949 fIs64BitGuest ?
950 (FirmwareType_T)FirmwareType_EFI64
951 :
952 (FirmwareType_T)FirmwareType_EFI32;
953 }
954 bool f64BitEntry = eFwType == FirmwareType_EFI64;
955
956 rc = findEfiRom(virtualBox, eFwType, efiRomFile); RC_CHECK();
957
958 /* Get boot args */
959 Bstr bootArgs;
960 hrc = pMachine->GetExtraData(Bstr("VBoxInternal2/EfiBootArgs"), bootArgs.asOutParam()); H();
961
962 /* Get device props */
963 Bstr deviceProps;
964 hrc = pMachine->GetExtraData(Bstr("VBoxInternal2/EfiDeviceProps"), deviceProps.asOutParam()); H();
965 /* Get GOP mode settings */
966 uint32_t u32GopMode = UINT32_MAX;
967 hrc = pMachine->GetExtraData(Bstr("VBoxInternal2/EfiGopMode"), bstr.asOutParam()); H();
968 if (!bstr.isEmpty())
969 u32GopMode = Utf8Str(bstr).toUInt32();
970
971 /* UGA mode settings */
972 uint32_t u32UgaHorisontal = 0;
973 hrc = pMachine->GetExtraData(Bstr("VBoxInternal2/EfiUgaHorizontalResolution"), bstr.asOutParam()); H();
974 if (!bstr.isEmpty())
975 u32UgaHorisontal = Utf8Str(bstr).toUInt32();
976
977 uint32_t u32UgaVertical = 0;
978 hrc = pMachine->GetExtraData(Bstr("VBoxInternal2/EfiUgaVerticalResolution"), bstr.asOutParam()); H();
979 if (!bstr.isEmpty())
980 u32UgaVertical = Utf8Str(bstr).toUInt32();
981
982 /*
983 * EFI subtree.
984 */
985 rc = CFGMR3InsertNode(pDevices, "efi", &pDev); RC_CHECK();
986 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
987 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
988 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
989 rc = CFGMR3InsertInteger(pCfg, "RamSize", cbRam); RC_CHECK();
990 rc = CFGMR3InsertInteger(pCfg, "RamHoleSize", cbRamHole); RC_CHECK();
991 rc = CFGMR3InsertInteger(pCfg, "NumCPUs", cCpus); RC_CHECK();
992 rc = CFGMR3InsertString(pCfg, "EfiRom", efiRomFile.raw()); RC_CHECK();
993 rc = CFGMR3InsertString(pCfg, "BootArgs", Utf8Str(bootArgs).raw()); RC_CHECK();
994 rc = CFGMR3InsertString(pCfg, "DeviceProps", Utf8Str(deviceProps).raw());RC_CHECK();
995 rc = CFGMR3InsertInteger(pCfg, "IOAPIC", fIOAPIC); RC_CHECK();
996 rc = CFGMR3InsertBytes(pCfg, "UUID", &HardwareUuid,sizeof(HardwareUuid)); RC_CHECK();
997 rc = CFGMR3InsertInteger(pCfg, "64BitEntry", f64BitEntry); /* boolean */ RC_CHECK();
998 rc = CFGMR3InsertInteger(pCfg, "GopMode", u32GopMode); RC_CHECK();
999 rc = CFGMR3InsertInteger(pCfg, "UgaHorizontalResolution", u32UgaHorisontal); RC_CHECK();
1000 rc = CFGMR3InsertInteger(pCfg, "UgaVerticalResolution", u32UgaVertical); RC_CHECK();
1001
1002 /* For OS X guests we'll force passing host's DMI info to the guest */
1003 if (fOsXGuest)
1004 {
1005 rc = CFGMR3InsertInteger(pCfg, "DmiUseHostInfo", 1); RC_CHECK();
1006 rc = CFGMR3InsertInteger(pCfg, "DmiExposeMemoryTable", 1); RC_CHECK();
1007 }
1008 }
1009
1010 /*
1011 * Storage controllers.
1012 */
1013 com::SafeIfaceArray<IStorageController> ctrls;
1014 PCFGMNODE aCtrlNodes[StorageControllerType_LsiLogicSas + 1] = {};
1015 hrc = pMachine->COMGETTER(StorageControllers)(ComSafeArrayAsOutParam(ctrls)); H();
1016
1017 for (size_t i = 0; i < ctrls.size(); ++i)
1018 {
1019 DeviceType_T *paLedDevType = NULL;
1020
1021 StorageControllerType_T enmCtrlType;
1022 rc = ctrls[i]->COMGETTER(ControllerType)(&enmCtrlType); H();
1023 AssertRelease((unsigned)enmCtrlType < RT_ELEMENTS(aCtrlNodes));
1024
1025 StorageBus_T enmBus;
1026 rc = ctrls[i]->COMGETTER(Bus)(&enmBus); H();
1027
1028 Bstr controllerName;
1029 rc = ctrls[i]->COMGETTER(Name)(controllerName.asOutParam()); H();
1030
1031 ULONG ulInstance = 999;
1032 rc = ctrls[i]->COMGETTER(Instance)(&ulInstance); H();
1033
1034 IoBackendType_T enmIoBackend;
1035 rc = ctrls[i]->COMGETTER(IoBackend)(&enmIoBackend); H();
1036
1037 /* /Devices/<ctrldev>/ */
1038 const char *pszCtrlDev = pConsole->convertControllerTypeToDev(enmCtrlType);
1039 pDev = aCtrlNodes[enmCtrlType];
1040 if (!pDev)
1041 {
1042 rc = CFGMR3InsertNode(pDevices, pszCtrlDev, &pDev); RC_CHECK();
1043 aCtrlNodes[enmCtrlType] = pDev; /* IDE variants are handled in the switch */
1044 }
1045
1046 /* /Devices/<ctrldev>/<instance>/ */
1047 PCFGMNODE pCtlInst = NULL;
1048 rc = CFGMR3InsertNodeF(pDev, &pCtlInst, "%u", ulInstance); RC_CHECK();
1049
1050 /* Device config: /Devices/<ctrldev>/<instance>/<values> & /ditto/Config/<values> */
1051 rc = CFGMR3InsertInteger(pCtlInst, "Trusted", 1); RC_CHECK();
1052 rc = CFGMR3InsertNode(pCtlInst, "Config", &pCfg); RC_CHECK();
1053
1054 switch (enmCtrlType)
1055 {
1056 case StorageControllerType_LsiLogic:
1057 {
1058 rc = CFGMR3InsertInteger(pCtlInst, "PCIDeviceNo", 20); RC_CHECK();
1059 Assert(!afPciDeviceNo[20]);
1060 afPciDeviceNo[20] = true;
1061 rc = CFGMR3InsertInteger(pCtlInst, "PCIFunctionNo", 0); RC_CHECK();
1062
1063 /* Attach the status driver */
1064 rc = CFGMR3InsertNode(pCtlInst, "LUN#999", &pLunL0); RC_CHECK();
1065 rc = CFGMR3InsertString(pLunL0, "Driver", "MainStatus"); RC_CHECK();
1066 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1067 rc = CFGMR3InsertInteger(pCfg, "papLeds", (uintptr_t)&pConsole->mapStorageLeds[iLedScsi]); RC_CHECK();
1068 rc = CFGMR3InsertInteger(pCfg, "First", 0); RC_CHECK();
1069 Assert(cLedScsi >= 16);
1070 rc = CFGMR3InsertInteger(pCfg, "Last", 15); RC_CHECK();
1071 paLedDevType = &pConsole->maStorageDevType[iLedScsi];
1072 break;
1073 }
1074
1075 case StorageControllerType_BusLogic:
1076 {
1077 rc = CFGMR3InsertInteger(pCtlInst, "PCIDeviceNo", 21); RC_CHECK();
1078 Assert(!afPciDeviceNo[21]);
1079 afPciDeviceNo[21] = true;
1080 rc = CFGMR3InsertInteger(pCtlInst, "PCIFunctionNo", 0); RC_CHECK();
1081
1082 /* Attach the status driver */
1083 rc = CFGMR3InsertNode(pCtlInst, "LUN#999", &pLunL0); RC_CHECK();
1084 rc = CFGMR3InsertString(pLunL0, "Driver", "MainStatus"); RC_CHECK();
1085 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1086 rc = CFGMR3InsertInteger(pCfg, "papLeds", (uintptr_t)&pConsole->mapStorageLeds[iLedScsi]); RC_CHECK();
1087 rc = CFGMR3InsertInteger(pCfg, "First", 0); RC_CHECK();
1088 Assert(cLedScsi >= 16);
1089 rc = CFGMR3InsertInteger(pCfg, "Last", 15); RC_CHECK();
1090 paLedDevType = &pConsole->maStorageDevType[iLedScsi];
1091 break;
1092 }
1093
1094 case StorageControllerType_IntelAhci:
1095 {
1096 rc = CFGMR3InsertInteger(pCtlInst, "PCIDeviceNo", 13); RC_CHECK();
1097 Assert(!afPciDeviceNo[13]);
1098 afPciDeviceNo[13] = true;
1099 rc = CFGMR3InsertInteger(pCtlInst, "PCIFunctionNo", 0); RC_CHECK();
1100
1101 ULONG cPorts = 0;
1102 hrc = ctrls[i]->COMGETTER(PortCount)(&cPorts); H();
1103 rc = CFGMR3InsertInteger(pCfg, "PortCount", cPorts); RC_CHECK();
1104
1105 /* Needed configuration values for the bios. */
1106 if (pBiosCfg)
1107 {
1108 rc = CFGMR3InsertString(pBiosCfg, "SataHardDiskDevice", "ahci"); RC_CHECK();
1109 }
1110
1111 for (uint32_t j = 0; j < 4; ++j)
1112 {
1113 static const char * const s_apszConfig[4] =
1114 { "PrimaryMaster", "PrimarySlave", "SecondaryMaster", "SecondarySlave" };
1115 static const char * const s_apszBiosConfig[4] =
1116 { "SataPrimaryMasterLUN", "SataPrimarySlaveLUN", "SataSecondaryMasterLUN", "SataSecondarySlaveLUN" };
1117
1118 LONG lPortNumber = -1;
1119 hrc = ctrls[i]->GetIDEEmulationPort(j, &lPortNumber); H();
1120 rc = CFGMR3InsertInteger(pCfg, s_apszConfig[j], lPortNumber); RC_CHECK();
1121 if (pBiosCfg)
1122 {
1123 rc = CFGMR3InsertInteger(pBiosCfg, s_apszBiosConfig[j], lPortNumber); RC_CHECK();
1124 }
1125 }
1126
1127 /* Attach the status driver */
1128 rc = CFGMR3InsertNode(pCtlInst, "LUN#999", &pLunL0); RC_CHECK();
1129 rc = CFGMR3InsertString(pLunL0, "Driver", "MainStatus"); RC_CHECK();
1130 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1131 AssertRelease(cPorts <= cLedSata);
1132 rc = CFGMR3InsertInteger(pCfg, "papLeds", (uintptr_t)&pConsole->mapStorageLeds[iLedSata]); RC_CHECK();
1133 rc = CFGMR3InsertInteger(pCfg, "First", 0); RC_CHECK();
1134 rc = CFGMR3InsertInteger(pCfg, "Last", cPorts - 1); RC_CHECK();
1135 paLedDevType = &pConsole->maStorageDevType[iLedSata];
1136 break;
1137 }
1138
1139 case StorageControllerType_PIIX3:
1140 case StorageControllerType_PIIX4:
1141 case StorageControllerType_ICH6:
1142 {
1143 /*
1144 * IDE (update this when the main interface changes)
1145 */
1146 rc = CFGMR3InsertInteger(pCtlInst, "PCIDeviceNo", 1); RC_CHECK();
1147 Assert(!afPciDeviceNo[1]);
1148 afPciDeviceNo[1] = true;
1149 rc = CFGMR3InsertInteger(pCtlInst, "PCIFunctionNo", 1); RC_CHECK();
1150 rc = CFGMR3InsertString(pCfg, "Type", controllerString(enmCtrlType)); RC_CHECK();
1151
1152 /* Attach the status driver */
1153 rc = CFGMR3InsertNode(pCtlInst, "LUN#999", &pLunL0); RC_CHECK();
1154 rc = CFGMR3InsertString(pLunL0, "Driver", "MainStatus"); RC_CHECK();
1155 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1156 rc = CFGMR3InsertInteger(pCfg, "papLeds", (uintptr_t)&pConsole->mapStorageLeds[iLedIde]); RC_CHECK();
1157 rc = CFGMR3InsertInteger(pCfg, "First", 0); RC_CHECK();
1158 Assert(cLedIde >= 4);
1159 rc = CFGMR3InsertInteger(pCfg, "Last", 3); RC_CHECK();
1160 paLedDevType = &pConsole->maStorageDevType[iLedIde];
1161
1162 /* IDE flavors */
1163 aCtrlNodes[StorageControllerType_PIIX3] = pDev;
1164 aCtrlNodes[StorageControllerType_PIIX4] = pDev;
1165 aCtrlNodes[StorageControllerType_ICH6] = pDev;
1166 break;
1167 }
1168
1169 case StorageControllerType_I82078:
1170 {
1171 /*
1172 * i82078 Floppy drive controller
1173 */
1174 fFdcEnabled = true;
1175 rc = CFGMR3InsertInteger(pCfg, "IRQ", 6); RC_CHECK();
1176 rc = CFGMR3InsertInteger(pCfg, "DMA", 2); RC_CHECK();
1177 rc = CFGMR3InsertInteger(pCfg, "MemMapped", 0 ); RC_CHECK();
1178 rc = CFGMR3InsertInteger(pCfg, "IOBase", 0x3f0); RC_CHECK();
1179
1180 /* Attach the status driver */
1181 rc = CFGMR3InsertNode(pCtlInst, "LUN#999", &pLunL0); RC_CHECK();
1182 rc = CFGMR3InsertString(pLunL0, "Driver", "MainStatus"); RC_CHECK();
1183 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1184 rc = CFGMR3InsertInteger(pCfg, "papLeds", (uintptr_t)&pConsole->mapStorageLeds[iLedFloppy]); RC_CHECK();
1185 rc = CFGMR3InsertInteger(pCfg, "First", 0); RC_CHECK();
1186 Assert(cLedFloppy >= 1);
1187 rc = CFGMR3InsertInteger(pCfg, "Last", 0); RC_CHECK();
1188 paLedDevType = &pConsole->maStorageDevType[iLedFloppy];
1189 break;
1190 }
1191
1192 case StorageControllerType_LsiLogicSas:
1193 {
1194 rc = CFGMR3InsertInteger(pCtlInst, "PCIDeviceNo", 22); RC_CHECK();
1195 Assert(!afPciDeviceNo[22]);
1196 afPciDeviceNo[22] = true;
1197 rc = CFGMR3InsertInteger(pCtlInst, "PCIFunctionNo", 0); RC_CHECK();
1198
1199 rc = CFGMR3InsertString(pCfg, "ControllerType", "SAS1068"); RC_CHECK();
1200
1201 /* Attach the status driver */
1202 rc = CFGMR3InsertNode(pCtlInst, "LUN#999", &pLunL0); RC_CHECK();
1203 rc = CFGMR3InsertString(pLunL0, "Driver", "MainStatus"); RC_CHECK();
1204 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1205 rc = CFGMR3InsertInteger(pCfg, "papLeds", (uintptr_t)&pConsole->mapStorageLeds[iLedSas]); RC_CHECK();
1206 rc = CFGMR3InsertInteger(pCfg, "First", 0); RC_CHECK();
1207 Assert(cLedSas >= 8);
1208 rc = CFGMR3InsertInteger(pCfg, "Last", 7); RC_CHECK();
1209 paLedDevType = &pConsole->maStorageDevType[iLedSas];
1210 break;
1211 }
1212
1213 default:
1214 AssertMsgFailedReturn(("invalid storage controller type: %d\n", enmCtrlType), VERR_GENERAL_FAILURE);
1215 }
1216
1217 /* Attach the media to the storage controllers. */
1218 com::SafeIfaceArray<IMediumAttachment> atts;
1219 hrc = pMachine->GetMediumAttachmentsOfController(controllerName,
1220 ComSafeArrayAsOutParam(atts)); H();
1221
1222 for (size_t j = 0; j < atts.size(); ++j)
1223 {
1224 rc = pConsole->configMediumAttachment(pCtlInst,
1225 pszCtrlDev,
1226 ulInstance,
1227 enmBus,
1228 enmIoBackend,
1229 false /* fSetupMerge */,
1230 0 /* uMergeSource */,
1231 0 /* uMergeTarget */,
1232 atts[j],
1233 pConsole->mMachineState,
1234 NULL /* phrc */,
1235 false /* fAttachDetach */,
1236 false /* fForceUnmount */,
1237 NULL /* pVM */,
1238 paLedDevType); RC_CHECK();
1239 }
1240 H();
1241 }
1242 H();
1243
1244 /*
1245 * Network adapters
1246 */
1247#ifdef VMWARE_NET_IN_SLOT_11
1248 bool fSwapSlots3and11 = false;
1249#endif
1250 PCFGMNODE pDevPCNet = NULL; /* PCNet-type devices */
1251 rc = CFGMR3InsertNode(pDevices, "pcnet", &pDevPCNet); RC_CHECK();
1252#ifdef VBOX_WITH_E1000
1253 PCFGMNODE pDevE1000 = NULL; /* E1000-type devices */
1254 rc = CFGMR3InsertNode(pDevices, "e1000", &pDevE1000); RC_CHECK();
1255#endif
1256#ifdef VBOX_WITH_VIRTIO
1257 PCFGMNODE pDevVirtioNet = NULL; /* Virtio network devices */
1258 rc = CFGMR3InsertNode(pDevices, "virtio-net", &pDevVirtioNet); RC_CHECK();
1259#endif /* VBOX_WITH_VIRTIO */
1260 std::list<BootNic> llBootNics;
1261 for (ULONG ulInstance = 0; ulInstance < SchemaDefs::NetworkAdapterCount; ++ulInstance)
1262 {
1263 ComPtr<INetworkAdapter> networkAdapter;
1264 hrc = pMachine->GetNetworkAdapter(ulInstance, networkAdapter.asOutParam()); H();
1265 BOOL fEnabled = FALSE;
1266 hrc = networkAdapter->COMGETTER(Enabled)(&fEnabled); H();
1267 if (!fEnabled)
1268 continue;
1269
1270 /*
1271 * The virtual hardware type. Create appropriate device first.
1272 */
1273 const char *pszAdapterName = "pcnet";
1274 NetworkAdapterType_T adapterType;
1275 hrc = networkAdapter->COMGETTER(AdapterType)(&adapterType); H();
1276 switch (adapterType)
1277 {
1278 case NetworkAdapterType_Am79C970A:
1279 case NetworkAdapterType_Am79C973:
1280 pDev = pDevPCNet;
1281 break;
1282#ifdef VBOX_WITH_E1000
1283 case NetworkAdapterType_I82540EM:
1284 case NetworkAdapterType_I82543GC:
1285 case NetworkAdapterType_I82545EM:
1286 pDev = pDevE1000;
1287 pszAdapterName = "e1000";
1288 break;
1289#endif
1290#ifdef VBOX_WITH_VIRTIO
1291 case NetworkAdapterType_Virtio:
1292 pDev = pDevVirtioNet;
1293 pszAdapterName = "virtio-net";
1294 break;
1295#endif /* VBOX_WITH_VIRTIO */
1296 default:
1297 AssertMsgFailed(("Invalid network adapter type '%d' for slot '%d'",
1298 adapterType, ulInstance));
1299 return VMSetError(pVM, VERR_INVALID_PARAMETER, RT_SRC_POS,
1300 N_("Invalid network adapter type '%d' for slot '%d'"),
1301 adapterType, ulInstance);
1302 }
1303
1304 rc = CFGMR3InsertNodeF(pDev, &pInst, "%u", ulInstance); RC_CHECK();
1305 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
1306 /* the first network card gets the PCI ID 3, the next 3 gets 8..10,
1307 * next 4 get 16..19. */
1308 unsigned iPciDeviceNo = 3;
1309 if (ulInstance)
1310 {
1311 if (ulInstance < 4)
1312 iPciDeviceNo = ulInstance - 1 + 8;
1313 else
1314 iPciDeviceNo = ulInstance - 4 + 16;
1315 }
1316#ifdef VMWARE_NET_IN_SLOT_11
1317 /*
1318 * Dirty hack for PCI slot compatibility with VMWare,
1319 * it assigns slot 11 to the first network controller.
1320 */
1321 if (iPciDeviceNo == 3 && adapterType == NetworkAdapterType_I82545EM)
1322 {
1323 iPciDeviceNo = 0x11;
1324 fSwapSlots3and11 = true;
1325 }
1326 else if (iPciDeviceNo == 0x11 && fSwapSlots3and11)
1327 iPciDeviceNo = 3;
1328#endif
1329 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", iPciDeviceNo); RC_CHECK();
1330 Assert(!afPciDeviceNo[iPciDeviceNo]);
1331 afPciDeviceNo[iPciDeviceNo] = true;
1332 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); RC_CHECK();
1333 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1334#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE /* not safe here yet. */
1335 if (pDev == pDevPCNet)
1336 {
1337 rc = CFGMR3InsertInteger(pCfg, "R0Enabled", false); RC_CHECK();
1338 }
1339#endif
1340 /*
1341 * Collect information needed for network booting and add it to the list.
1342 */
1343 BootNic nic;
1344
1345 nic.mInstance = ulInstance;
1346 nic.mPciDev = iPciDeviceNo;
1347 nic.mPciFn = 0;
1348
1349 hrc = networkAdapter->COMGETTER(BootPriority)(&nic.mBootPrio); H();
1350
1351 llBootNics.push_back(nic);
1352
1353 /*
1354 * The virtual hardware type. PCNet supports two types.
1355 */
1356 switch (adapterType)
1357 {
1358 case NetworkAdapterType_Am79C970A:
1359 rc = CFGMR3InsertInteger(pCfg, "Am79C973", 0); RC_CHECK();
1360 break;
1361 case NetworkAdapterType_Am79C973:
1362 rc = CFGMR3InsertInteger(pCfg, "Am79C973", 1); RC_CHECK();
1363 break;
1364 case NetworkAdapterType_I82540EM:
1365 rc = CFGMR3InsertInteger(pCfg, "AdapterType", 0); RC_CHECK();
1366 break;
1367 case NetworkAdapterType_I82543GC:
1368 rc = CFGMR3InsertInteger(pCfg, "AdapterType", 1); RC_CHECK();
1369 break;
1370 case NetworkAdapterType_I82545EM:
1371 rc = CFGMR3InsertInteger(pCfg, "AdapterType", 2); RC_CHECK();
1372 break;
1373 }
1374
1375 /*
1376 * Get the MAC address and convert it to binary representation
1377 */
1378 Bstr macAddr;
1379 hrc = networkAdapter->COMGETTER(MACAddress)(macAddr.asOutParam()); H();
1380 Assert(macAddr);
1381 Utf8Str macAddrUtf8 = macAddr;
1382 char *macStr = (char*)macAddrUtf8.raw();
1383 Assert(strlen(macStr) == 12);
1384 RTMAC Mac;
1385 memset(&Mac, 0, sizeof(Mac));
1386 char *pMac = (char*)&Mac;
1387 for (uint32_t i = 0; i < 6; ++i)
1388 {
1389 char c1 = *macStr++ - '0';
1390 if (c1 > 9)
1391 c1 -= 7;
1392 char c2 = *macStr++ - '0';
1393 if (c2 > 9)
1394 c2 -= 7;
1395 *pMac++ = ((c1 & 0x0f) << 4) | (c2 & 0x0f);
1396 }
1397 rc = CFGMR3InsertBytes(pCfg, "MAC", &Mac, sizeof(Mac)); RC_CHECK();
1398
1399 /*
1400 * Check if the cable is supposed to be unplugged
1401 */
1402 BOOL fCableConnected;
1403 hrc = networkAdapter->COMGETTER(CableConnected)(&fCableConnected); H();
1404 rc = CFGMR3InsertInteger(pCfg, "CableConnected", fCableConnected ? 1 : 0); RC_CHECK();
1405
1406 /*
1407 * Line speed to report from custom drivers
1408 */
1409 ULONG ulLineSpeed;
1410 hrc = networkAdapter->COMGETTER(LineSpeed)(&ulLineSpeed); H();
1411 rc = CFGMR3InsertInteger(pCfg, "LineSpeed", ulLineSpeed); RC_CHECK();
1412
1413 /*
1414 * Attach the status driver.
1415 */
1416 rc = CFGMR3InsertNode(pInst, "LUN#999", &pLunL0); RC_CHECK();
1417 rc = CFGMR3InsertString(pLunL0, "Driver", "MainStatus"); RC_CHECK();
1418 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1419 rc = CFGMR3InsertInteger(pCfg, "papLeds", (uintptr_t)&pConsole->mapNetworkLeds[ulInstance]); RC_CHECK();
1420
1421 /*
1422 * Configure the network card now
1423 */
1424 rc = pConsole->configNetwork(pszAdapterName, ulInstance, 0,
1425 networkAdapter, pCfg, pLunL0, pInst,
1426 false /*fAttachDetach*/); RC_CHECK();
1427 }
1428
1429 /*
1430 * Build network boot information and transfer it to the BIOS.
1431 */
1432 if (pNetBootCfg && !llBootNics.empty()) /* NetBoot node doesn't exist for EFI! */
1433 {
1434 llBootNics.sort(); /* Sort the list by boot priority. */
1435
1436 char achBootIdx[] = "0";
1437 unsigned uBootIdx = 0;
1438
1439 for (std::list<BootNic>::iterator it = llBootNics.begin(); it != llBootNics.end(); ++it)
1440 {
1441 /* A NIC with priority 0 is only used if it's first in the list. */
1442 if (it->mBootPrio == 0 && uBootIdx != 0)
1443 break;
1444
1445 PCFGMNODE pNetBtDevCfg;
1446 achBootIdx[0] = '0' + uBootIdx++; /* Boot device order. */
1447 rc = CFGMR3InsertNode(pNetBootCfg, achBootIdx, &pNetBtDevCfg); RC_CHECK();
1448 rc = CFGMR3InsertInteger(pNetBtDevCfg, "NIC", it->mInstance); RC_CHECK();
1449 rc = CFGMR3InsertInteger(pNetBtDevCfg, "PCIDeviceNo", it->mPciDev); RC_CHECK();
1450 rc = CFGMR3InsertInteger(pNetBtDevCfg, "PCIFunctionNo", it->mPciFn); RC_CHECK();
1451 }
1452 }
1453
1454 /*
1455 * Serial (UART) Ports
1456 */
1457 rc = CFGMR3InsertNode(pDevices, "serial", &pDev); RC_CHECK();
1458 for (ULONG ulInstance = 0; ulInstance < SchemaDefs::SerialPortCount; ++ulInstance)
1459 {
1460 ComPtr<ISerialPort> serialPort;
1461 hrc = pMachine->GetSerialPort(ulInstance, serialPort.asOutParam()); H();
1462 BOOL fEnabled = FALSE;
1463 if (serialPort)
1464 hrc = serialPort->COMGETTER(Enabled)(&fEnabled); H();
1465 if (!fEnabled)
1466 continue;
1467
1468 rc = CFGMR3InsertNodeF(pDev, &pInst, "%u", ulInstance); RC_CHECK();
1469 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1470
1471 ULONG ulIRQ;
1472 hrc = serialPort->COMGETTER(IRQ)(&ulIRQ); H();
1473 rc = CFGMR3InsertInteger(pCfg, "IRQ", ulIRQ); RC_CHECK();
1474 ULONG ulIOBase;
1475 hrc = serialPort->COMGETTER(IOBase)(&ulIOBase); H();
1476 rc = CFGMR3InsertInteger(pCfg, "IOBase", ulIOBase); RC_CHECK();
1477 BOOL fServer;
1478 hrc = serialPort->COMGETTER(Server)(&fServer); H();
1479 hrc = serialPort->COMGETTER(Path)(bstr.asOutParam()); H();
1480 PortMode_T eHostMode;
1481 hrc = serialPort->COMGETTER(HostMode)(&eHostMode); H();
1482 if (eHostMode != PortMode_Disconnected)
1483 {
1484 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
1485 if (eHostMode == PortMode_HostPipe)
1486 {
1487 rc = CFGMR3InsertString(pLunL0, "Driver", "Char"); RC_CHECK();
1488 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); RC_CHECK();
1489 rc = CFGMR3InsertString(pLunL1, "Driver", "NamedPipe"); RC_CHECK();
1490 rc = CFGMR3InsertNode(pLunL1, "Config", &pLunL2); RC_CHECK();
1491 rc = CFGMR3InsertStringW(pLunL2, "Location", bstr.raw()); RC_CHECK();
1492 rc = CFGMR3InsertInteger(pLunL2, "IsServer", fServer); RC_CHECK();
1493 }
1494 else if (eHostMode == PortMode_HostDevice)
1495 {
1496 rc = CFGMR3InsertString(pLunL0, "Driver", "Host Serial"); RC_CHECK();
1497 rc = CFGMR3InsertNode(pLunL0, "Config", &pLunL1); RC_CHECK();
1498 rc = CFGMR3InsertStringW(pLunL1, "DevicePath", bstr.raw()); RC_CHECK();
1499 }
1500 else if (eHostMode == PortMode_RawFile)
1501 {
1502 rc = CFGMR3InsertString(pLunL0, "Driver", "Char"); RC_CHECK();
1503 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); RC_CHECK();
1504 rc = CFGMR3InsertString(pLunL1, "Driver", "RawFile"); RC_CHECK();
1505 rc = CFGMR3InsertNode(pLunL1, "Config", &pLunL2); RC_CHECK();
1506 rc = CFGMR3InsertStringW(pLunL2, "Location", bstr.raw()); RC_CHECK();
1507 }
1508 }
1509 }
1510
1511 /*
1512 * Parallel (LPT) Ports
1513 */
1514 rc = CFGMR3InsertNode(pDevices, "parallel", &pDev); RC_CHECK();
1515 for (ULONG ulInstance = 0; ulInstance < SchemaDefs::ParallelPortCount; ++ulInstance)
1516 {
1517 ComPtr<IParallelPort> parallelPort;
1518 hrc = pMachine->GetParallelPort(ulInstance, parallelPort.asOutParam()); H();
1519 BOOL fEnabled = FALSE;
1520 if (parallelPort)
1521 {
1522 hrc = parallelPort->COMGETTER(Enabled)(&fEnabled); H();
1523 }
1524 if (!fEnabled)
1525 continue;
1526
1527 rc = CFGMR3InsertNodeF(pDev, &pInst, "%u", ulInstance); RC_CHECK();
1528 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1529
1530 ULONG ulIRQ;
1531 hrc = parallelPort->COMGETTER(IRQ)(&ulIRQ); H();
1532 rc = CFGMR3InsertInteger(pCfg, "IRQ", ulIRQ); RC_CHECK();
1533 ULONG ulIOBase;
1534 hrc = parallelPort->COMGETTER(IOBase)(&ulIOBase); H();
1535 rc = CFGMR3InsertInteger(pCfg, "IOBase", ulIOBase); RC_CHECK();
1536 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
1537 rc = CFGMR3InsertString(pLunL0, "Driver", "HostParallel"); RC_CHECK();
1538 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); RC_CHECK();
1539 hrc = parallelPort->COMGETTER(Path)(bstr.asOutParam()); H();
1540 rc = CFGMR3InsertStringW(pLunL1, "DevicePath", bstr.raw()); RC_CHECK();
1541 }
1542
1543 /*
1544 * VMM Device
1545 */
1546 rc = CFGMR3InsertNode(pDevices, "VMMDev", &pDev); RC_CHECK();
1547 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
1548 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1549 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
1550 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 4); RC_CHECK();
1551 Assert(!afPciDeviceNo[4]);
1552 afPciDeviceNo[4] = true;
1553 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); RC_CHECK();
1554 Bstr hwVersion;
1555 hrc = pMachine->COMGETTER(HardwareVersion)(hwVersion.asOutParam()); H();
1556 if (hwVersion.compare(Bstr("1")) == 0) /* <= 2.0.x */
1557 {
1558 CFGMR3InsertInteger(pCfg, "HeapEnabled", 0); RC_CHECK();
1559 }
1560
1561 /* the VMM device's Main driver */
1562 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
1563 rc = CFGMR3InsertString(pLunL0, "Driver", "HGCM"); RC_CHECK();
1564 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1565 VMMDev *pVMMDev = pConsole->mVMMDev;
1566 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)pVMMDev); RC_CHECK();
1567
1568 /*
1569 * Attach the status driver.
1570 */
1571 rc = CFGMR3InsertNode(pInst, "LUN#999", &pLunL0); RC_CHECK();
1572 rc = CFGMR3InsertString(pLunL0, "Driver", "MainStatus"); RC_CHECK();
1573 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1574 rc = CFGMR3InsertInteger(pCfg, "papLeds", (uintptr_t)&pConsole->mapSharedFolderLed); RC_CHECK();
1575 rc = CFGMR3InsertInteger(pCfg, "First", 0); RC_CHECK();
1576 rc = CFGMR3InsertInteger(pCfg, "Last", 0); RC_CHECK();
1577
1578 /*
1579 * Audio Sniffer Device
1580 */
1581 rc = CFGMR3InsertNode(pDevices, "AudioSniffer", &pDev); RC_CHECK();
1582 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
1583 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1584
1585 /* the Audio Sniffer device's Main driver */
1586 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
1587 rc = CFGMR3InsertString(pLunL0, "Driver", "MainAudioSniffer"); RC_CHECK();
1588 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1589 AudioSniffer *pAudioSniffer = pConsole->mAudioSniffer;
1590 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)pAudioSniffer); RC_CHECK();
1591
1592 /*
1593 * AC'97 ICH / SoundBlaster16 audio
1594 */
1595 BOOL enabled;
1596 ComPtr<IAudioAdapter> audioAdapter;
1597 hrc = pMachine->COMGETTER(AudioAdapter)(audioAdapter.asOutParam()); H();
1598 if (audioAdapter)
1599 hrc = audioAdapter->COMGETTER(Enabled)(&enabled); H();
1600
1601 if (enabled)
1602 {
1603 AudioControllerType_T audioController;
1604 hrc = audioAdapter->COMGETTER(AudioController)(&audioController); H();
1605 switch (audioController)
1606 {
1607 case AudioControllerType_AC97:
1608 {
1609 /* default: ICH AC97 */
1610 rc = CFGMR3InsertNode(pDevices, "ichac97", &pDev); RC_CHECK();
1611 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1612 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* bool */ RC_CHECK();
1613 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 5); RC_CHECK();
1614 Assert(!afPciDeviceNo[5]);
1615 afPciDeviceNo[5] = true;
1616 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); RC_CHECK();
1617 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1618 break;
1619 }
1620 case AudioControllerType_SB16:
1621 {
1622 /* legacy SoundBlaster16 */
1623 rc = CFGMR3InsertNode(pDevices, "sb16", &pDev); RC_CHECK();
1624 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
1625 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* bool */ RC_CHECK();
1626 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1627 rc = CFGMR3InsertInteger(pCfg, "IRQ", 5); RC_CHECK();
1628 rc = CFGMR3InsertInteger(pCfg, "DMA", 1); RC_CHECK();
1629 rc = CFGMR3InsertInteger(pCfg, "DMA16", 5); RC_CHECK();
1630 rc = CFGMR3InsertInteger(pCfg, "Port", 0x220); RC_CHECK();
1631 rc = CFGMR3InsertInteger(pCfg, "Version", 0x0405); RC_CHECK();
1632 break;
1633 }
1634 }
1635
1636 /* the Audio driver */
1637 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
1638 rc = CFGMR3InsertString(pLunL0, "Driver", "AUDIO"); RC_CHECK();
1639 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1640
1641 AudioDriverType_T audioDriver;
1642 hrc = audioAdapter->COMGETTER(AudioDriver)(&audioDriver); H();
1643 switch (audioDriver)
1644 {
1645 case AudioDriverType_Null:
1646 {
1647 rc = CFGMR3InsertString(pCfg, "AudioDriver", "null"); RC_CHECK();
1648 break;
1649 }
1650#ifdef RT_OS_WINDOWS
1651#ifdef VBOX_WITH_WINMM
1652 case AudioDriverType_WinMM:
1653 {
1654 rc = CFGMR3InsertString(pCfg, "AudioDriver", "winmm"); RC_CHECK();
1655 break;
1656 }
1657#endif
1658 case AudioDriverType_DirectSound:
1659 {
1660 rc = CFGMR3InsertString(pCfg, "AudioDriver", "dsound"); RC_CHECK();
1661 break;
1662 }
1663#endif /* RT_OS_WINDOWS */
1664#ifdef RT_OS_SOLARIS
1665 case AudioDriverType_SolAudio:
1666 {
1667 rc = CFGMR3InsertString(pCfg, "AudioDriver", "solaudio"); RC_CHECK();
1668 break;
1669 }
1670#endif
1671#ifdef RT_OS_LINUX
1672# ifdef VBOX_WITH_ALSA
1673 case AudioDriverType_ALSA:
1674 {
1675 rc = CFGMR3InsertString(pCfg, "AudioDriver", "alsa"); RC_CHECK();
1676 break;
1677 }
1678# endif
1679# ifdef VBOX_WITH_PULSE
1680 case AudioDriverType_Pulse:
1681 {
1682 rc = CFGMR3InsertString(pCfg, "AudioDriver", "pulse"); RC_CHECK();
1683 break;
1684 }
1685# endif
1686#endif /* RT_OS_LINUX */
1687#if defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD) || defined(VBOX_WITH_SOLARIS_OSS)
1688 case AudioDriverType_OSS:
1689 {
1690 rc = CFGMR3InsertString(pCfg, "AudioDriver", "oss"); RC_CHECK();
1691 break;
1692 }
1693#endif
1694#ifdef RT_OS_FREEBSD
1695# ifdef VBOX_WITH_PULSE
1696 case AudioDriverType_Pulse:
1697 {
1698 rc = CFGMR3InsertString(pCfg, "AudioDriver", "pulse"); RC_CHECK();
1699 break;
1700 }
1701# endif
1702#endif
1703#ifdef RT_OS_DARWIN
1704 case AudioDriverType_CoreAudio:
1705 {
1706 rc = CFGMR3InsertString(pCfg, "AudioDriver", "coreaudio"); RC_CHECK();
1707 break;
1708 }
1709#endif
1710 }
1711 hrc = pMachine->COMGETTER(Name)(bstr.asOutParam()); H();
1712 rc = CFGMR3InsertStringW(pCfg, "StreamName", bstr.raw()); RC_CHECK();
1713 }
1714
1715 /*
1716 * The USB Controller.
1717 */
1718 ComPtr<IUSBController> USBCtlPtr;
1719 hrc = pMachine->COMGETTER(USBController)(USBCtlPtr.asOutParam());
1720 if (USBCtlPtr)
1721 {
1722 BOOL fOhciEnabled;
1723 hrc = USBCtlPtr->COMGETTER(Enabled)(&fOhciEnabled); H();
1724 if (fOhciEnabled)
1725 {
1726 rc = CFGMR3InsertNode(pDevices, "usb-ohci", &pDev); RC_CHECK();
1727 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
1728 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1729 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
1730 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 6); RC_CHECK();
1731 Assert(!afPciDeviceNo[6]);
1732 afPciDeviceNo[6] = true;
1733 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); RC_CHECK();
1734
1735 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
1736 rc = CFGMR3InsertString(pLunL0, "Driver", "VUSBRootHub"); RC_CHECK();
1737 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1738
1739 /*
1740 * Attach the status driver.
1741 */
1742 rc = CFGMR3InsertNode(pInst, "LUN#999", &pLunL0); RC_CHECK();
1743 rc = CFGMR3InsertString(pLunL0, "Driver", "MainStatus"); RC_CHECK();
1744 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1745 rc = CFGMR3InsertInteger(pCfg, "papLeds", (uintptr_t)&pConsole->mapUSBLed[0]);RC_CHECK();
1746 rc = CFGMR3InsertInteger(pCfg, "First", 0); RC_CHECK();
1747 rc = CFGMR3InsertInteger(pCfg, "Last", 0); RC_CHECK();
1748
1749#ifdef VBOX_WITH_EHCI
1750 BOOL fEhciEnabled;
1751 hrc = USBCtlPtr->COMGETTER(EnabledEhci)(&fEhciEnabled); H();
1752 if (fEhciEnabled)
1753 {
1754 rc = CFGMR3InsertNode(pDevices, "usb-ehci", &pDev); RC_CHECK();
1755 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
1756 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1757 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* bool */ RC_CHECK();
1758 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 11); RC_CHECK();
1759 Assert(!afPciDeviceNo[11]);
1760 afPciDeviceNo[11] = true;
1761 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); RC_CHECK();
1762
1763 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
1764 rc = CFGMR3InsertString(pLunL0, "Driver", "VUSBRootHub"); RC_CHECK();
1765 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1766
1767 /*
1768 * Attach the status driver.
1769 */
1770 rc = CFGMR3InsertNode(pInst, "LUN#999", &pLunL0); RC_CHECK();
1771 rc = CFGMR3InsertString(pLunL0, "Driver", "MainStatus"); RC_CHECK();
1772 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1773 rc = CFGMR3InsertInteger(pCfg, "papLeds", (uintptr_t)&pConsole->mapUSBLed[1]);RC_CHECK();
1774 rc = CFGMR3InsertInteger(pCfg, "First", 0); RC_CHECK();
1775 rc = CFGMR3InsertInteger(pCfg, "Last", 0); RC_CHECK();
1776 }
1777#endif
1778
1779 /*
1780 * Virtual USB Devices.
1781 */
1782 PCFGMNODE pUsbDevices = NULL;
1783 rc = CFGMR3InsertNode(pRoot, "USB", &pUsbDevices); RC_CHECK();
1784
1785#ifdef VBOX_WITH_USB
1786 {
1787 /*
1788 * Global USB options, currently unused as we'll apply the 2.0 -> 1.1 morphing
1789 * on a per device level now.
1790 */
1791 rc = CFGMR3InsertNode(pUsbDevices, "USBProxy", &pCfg); RC_CHECK();
1792 rc = CFGMR3InsertNode(pCfg, "GlobalConfig", &pCfg); RC_CHECK();
1793 // This globally enables the 2.0 -> 1.1 device morphing of proxied devies to keep windows quiet.
1794 //rc = CFGMR3InsertInteger(pCfg, "Force11Device", true); RC_CHECK();
1795 // The following breaks stuff, but it makes MSDs work in vista. (I include it here so
1796 // that it's documented somewhere.) Users needing it can use:
1797 // VBoxManage setextradata "myvm" "VBoxInternal/USB/USBProxy/GlobalConfig/Force11PacketSize" 1
1798 //rc = CFGMR3InsertInteger(pCfg, "Force11PacketSize", true); RC_CHECK();
1799 }
1800#endif
1801
1802# if 0 /* Virtual MSD*/
1803
1804 rc = CFGMR3InsertNode(pUsbDevices, "Msd", &pDev); RC_CHECK();
1805 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
1806 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1807 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
1808
1809 rc = CFGMR3InsertString(pLunL0, "Driver", "SCSI"); RC_CHECK();
1810 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1811
1812 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); RC_CHECK();
1813 rc = CFGMR3InsertString(pLunL1, "Driver", "Block"); RC_CHECK();
1814 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); RC_CHECK();
1815 rc = CFGMR3InsertString(pCfg, "Type", "HardDisk"); RC_CHECK();
1816 rc = CFGMR3InsertInteger(pCfg, "Mountable", 0); RC_CHECK();
1817
1818 rc = CFGMR3InsertNode(pLunL1, "AttachedDriver", &pLunL2); RC_CHECK();
1819 rc = CFGMR3InsertString(pLunL2, "Driver", "VD"); RC_CHECK();
1820 rc = CFGMR3InsertNode(pLunL2, "Config", &pCfg); RC_CHECK();
1821 rc = CFGMR3InsertString(pCfg, "Path", "/Volumes/DataHFS/bird/VDIs/linux.vdi"); RC_CHECK();
1822 rc = CFGMR3InsertString(pCfg, "Format", "VDI"); RC_CHECK();
1823# endif
1824
1825 /* Virtual USB Mouse/Tablet */
1826 PointingHidType_T aPointingHid;
1827 hrc = pMachine->COMGETTER(PointingHidType)(&aPointingHid); H();
1828 if (aPointingHid == PointingHidType_USBMouse || aPointingHid == PointingHidType_USBTablet)
1829 {
1830 rc = CFGMR3InsertNode(pUsbDevices, "HidMouse", &pDev); RC_CHECK();
1831 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
1832 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1833
1834 if (aPointingHid == PointingHidType_USBTablet)
1835 {
1836 rc = CFGMR3InsertInteger(pCfg, "Absolute", 1); RC_CHECK();
1837 }
1838 else
1839 {
1840 rc = CFGMR3InsertInteger(pCfg, "Absolute", 0); RC_CHECK();
1841 }
1842 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
1843 rc = CFGMR3InsertString(pLunL0, "Driver", "MouseQueue"); RC_CHECK();
1844 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1845 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 128); RC_CHECK();
1846
1847 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); RC_CHECK();
1848 rc = CFGMR3InsertString(pLunL1, "Driver", "MainMouse"); RC_CHECK();
1849 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); RC_CHECK();
1850 pMouse = pConsole->mMouse;
1851 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)pMouse); RC_CHECK();
1852 }
1853
1854 /* Virtual USB Keyboard */
1855 KeyboardHidType_T aKbdHid;
1856 hrc = pMachine->COMGETTER(KeyboardHidType)(&aKbdHid); H();
1857 if (aKbdHid == KeyboardHidType_USBKeyboard)
1858 {
1859 rc = CFGMR3InsertNode(pUsbDevices, "HidKeyboard", &pDev); RC_CHECK();
1860 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
1861 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1862
1863 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
1864 rc = CFGMR3InsertString(pLunL0, "Driver", "KeyboardQueue"); RC_CHECK();
1865 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1866 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 64); RC_CHECK();
1867
1868 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); RC_CHECK();
1869 rc = CFGMR3InsertString(pLunL1, "Driver", "MainKeyboard"); RC_CHECK();
1870 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); RC_CHECK();
1871 pKeyboard = pConsole->mKeyboard;
1872 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)pKeyboard); RC_CHECK();
1873 }
1874 }
1875 }
1876
1877 /*
1878 * Clipboard
1879 */
1880 {
1881 ClipboardMode_T mode = ClipboardMode_Disabled;
1882 hrc = pMachine->COMGETTER(ClipboardMode)(&mode); H();
1883
1884 if (mode != ClipboardMode_Disabled)
1885 {
1886 /* Load the service */
1887 rc = pConsole->mVMMDev->hgcmLoadService("VBoxSharedClipboard", "VBoxSharedClipboard");
1888
1889 if (RT_FAILURE(rc))
1890 {
1891 LogRel(("VBoxSharedClipboard is not available. rc = %Rrc\n", rc));
1892 /* That is not a fatal failure. */
1893 rc = VINF_SUCCESS;
1894 }
1895 else
1896 {
1897 /* Setup the service. */
1898 VBOXHGCMSVCPARM parm;
1899
1900 parm.type = VBOX_HGCM_SVC_PARM_32BIT;
1901
1902 switch (mode)
1903 {
1904 default:
1905 case ClipboardMode_Disabled:
1906 {
1907 LogRel(("VBoxSharedClipboard mode: Off\n"));
1908 parm.u.uint32 = VBOX_SHARED_CLIPBOARD_MODE_OFF;
1909 break;
1910 }
1911 case ClipboardMode_GuestToHost:
1912 {
1913 LogRel(("VBoxSharedClipboard mode: Guest to Host\n"));
1914 parm.u.uint32 = VBOX_SHARED_CLIPBOARD_MODE_GUEST_TO_HOST;
1915 break;
1916 }
1917 case ClipboardMode_HostToGuest:
1918 {
1919 LogRel(("VBoxSharedClipboard mode: Host to Guest\n"));
1920 parm.u.uint32 = VBOX_SHARED_CLIPBOARD_MODE_HOST_TO_GUEST;
1921 break;
1922 }
1923 case ClipboardMode_Bidirectional:
1924 {
1925 LogRel(("VBoxSharedClipboard mode: Bidirectional\n"));
1926 parm.u.uint32 = VBOX_SHARED_CLIPBOARD_MODE_BIDIRECTIONAL;
1927 break;
1928 }
1929 }
1930
1931 pConsole->mVMMDev->hgcmHostCall("VBoxSharedClipboard", VBOX_SHARED_CLIPBOARD_HOST_FN_SET_MODE, 1, &parm);
1932
1933 Log(("Set VBoxSharedClipboard mode\n"));
1934 }
1935 }
1936 }
1937
1938#ifdef VBOX_WITH_CROGL
1939 /*
1940 * crOpenGL
1941 */
1942 {
1943 BOOL fEnabled = false;
1944 hrc = pMachine->COMGETTER(Accelerate3DEnabled)(&fEnabled); H();
1945
1946 if (fEnabled)
1947 {
1948 /* Load the service */
1949 rc = pConsole->mVMMDev->hgcmLoadService("VBoxSharedCrOpenGL", "VBoxSharedCrOpenGL");
1950 if (RT_FAILURE(rc))
1951 {
1952 LogRel(("Failed to load Shared OpenGL service %Rrc\n", rc));
1953 /* That is not a fatal failure. */
1954 rc = VINF_SUCCESS;
1955 }
1956 else
1957 {
1958 LogRel(("Shared crOpenGL service loaded.\n"));
1959
1960 /* Setup the service. */
1961 VBOXHGCMSVCPARM parm;
1962 parm.type = VBOX_HGCM_SVC_PARM_PTR;
1963
1964 parm.u.pointer.addr = (IConsole*) (Console*) pConsole;
1965 parm.u.pointer.size = sizeof(IConsole *);
1966
1967 rc = pConsole->mVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_SET_CONSOLE,
1968 SHCRGL_CPARMS_SET_CONSOLE, &parm);
1969 if (!RT_SUCCESS(rc))
1970 AssertMsgFailed(("SHCRGL_HOST_FN_SET_CONSOLE failed with %Rrc\n", rc));
1971
1972 parm.u.pointer.addr = pVM;
1973 parm.u.pointer.size = sizeof(pVM);
1974 rc = pConsole->mVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_SET_VM,
1975 SHCRGL_CPARMS_SET_VM, &parm);
1976 if (!RT_SUCCESS(rc))
1977 AssertMsgFailed(("SHCRGL_HOST_FN_SET_VM failed with %Rrc\n", rc));
1978 }
1979
1980 }
1981 }
1982#endif
1983
1984#ifdef VBOX_WITH_GUEST_PROPS
1985 /*
1986 * Guest property service
1987 */
1988
1989 rc = configGuestProperties(pConsole);
1990#endif /* VBOX_WITH_GUEST_PROPS defined */
1991
1992#ifdef VBOX_WITH_GUEST_CONTROL
1993 /*
1994 * Guest control service
1995 */
1996
1997 rc = configGuestControl(pConsole);
1998#endif /* VBOX_WITH_GUEST_CONTROL defined */
1999
2000 /*
2001 * ACPI
2002 */
2003 BOOL fACPI;
2004 hrc = biosSettings->COMGETTER(ACPIEnabled)(&fACPI); H();
2005 if (fACPI)
2006 {
2007 BOOL fCpuHotPlug = false;
2008 BOOL fShowCpu = fOsXGuest;
2009 /* Always show the CPU leafs when we have multiple VCPUs or when the IO-APIC is enabled.
2010 * The Windows SMP kernel needs a CPU leaf or else its idle loop will burn cpu cycles; the
2011 * intelppm driver refuses to register an idle state handler.
2012 */
2013 if ((cCpus > 1) || fIOAPIC)
2014 fShowCpu = true;
2015
2016 hrc = pMachine->COMGETTER(CPUHotPlugEnabled)(&fCpuHotPlug); H();
2017
2018 rc = CFGMR3InsertNode(pDevices, "acpi", &pDev); RC_CHECK();
2019 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
2020 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
2021 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
2022 rc = CFGMR3InsertInteger(pCfg, "RamSize", cbRam); RC_CHECK();
2023 rc = CFGMR3InsertInteger(pCfg, "RamHoleSize", cbRamHole); RC_CHECK();
2024 rc = CFGMR3InsertInteger(pCfg, "NumCPUs", cCpus); RC_CHECK();
2025
2026 rc = CFGMR3InsertInteger(pCfg, "IOAPIC", fIOAPIC); RC_CHECK();
2027 rc = CFGMR3InsertInteger(pCfg, "FdcEnabled", fFdcEnabled); RC_CHECK();
2028 rc = CFGMR3InsertInteger(pCfg, "HpetEnabled", fHpetEnabled); RC_CHECK();
2029 rc = CFGMR3InsertInteger(pCfg, "SmcEnabled", fSmcEnabled); RC_CHECK();
2030 rc = CFGMR3InsertInteger(pCfg, "ShowRtc", fOsXGuest); RC_CHECK();
2031 if (fOsXGuest && !llBootNics.empty())
2032 {
2033 BootNic aNic = llBootNics.front();
2034 uint32_t u32NicPciAddr = (aNic.mPciDev << 16) | aNic.mPciFn;
2035 rc = CFGMR3InsertInteger(pCfg, "NicPciAddress", u32NicPciAddr); RC_CHECK();
2036 }
2037 rc = CFGMR3InsertInteger(pCfg, "ShowCpu", fShowCpu); RC_CHECK();
2038 rc = CFGMR3InsertInteger(pCfg, "CpuHotPlug", fCpuHotPlug); RC_CHECK();
2039 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 7); RC_CHECK();
2040 Assert(!afPciDeviceNo[7]);
2041 afPciDeviceNo[7] = true;
2042 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); RC_CHECK();
2043
2044 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
2045 rc = CFGMR3InsertString(pLunL0, "Driver", "ACPIHost"); RC_CHECK();
2046 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2047
2048 /* Attach the dummy CPU drivers */
2049 for (ULONG iCpuCurr = 1; iCpuCurr < cCpus; iCpuCurr++)
2050 {
2051 BOOL fCpuAttached = true;
2052
2053 if (fCpuHotPlug)
2054 {
2055 hrc = pMachine->GetCPUStatus(iCpuCurr, &fCpuAttached); H();
2056 }
2057
2058 if (fCpuAttached)
2059 {
2060 rc = CFGMR3InsertNodeF(pInst, &pLunL0, "LUN#%u", iCpuCurr); RC_CHECK();
2061 rc = CFGMR3InsertString(pLunL0, "Driver", "ACPICpu"); RC_CHECK();
2062 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2063 }
2064 }
2065 }
2066
2067
2068 /*
2069 * CFGM overlay handling.
2070 *
2071 * Here we check the extra data entries for CFGM values
2072 * and create the nodes and insert the values on the fly. Existing
2073 * values will be removed and reinserted. CFGM is typed, so by default
2074 * we will guess whether it's a string or an integer (byte arrays are
2075 * not currently supported). It's possible to override this autodetection
2076 * by adding "string:", "integer:" or "bytes:" (future).
2077 *
2078 * We first perform a run on global extra data, then on the machine
2079 * extra data to support global settings with local overrides.
2080 *
2081 */
2082 /** @todo add support for removing nodes and byte blobs. */
2083 SafeArray<BSTR> aGlobalExtraDataKeys;
2084 SafeArray<BSTR> aMachineExtraDataKeys;
2085 /*
2086 * Get the next key
2087 */
2088 if (FAILED(hrc = virtualBox->GetExtraDataKeys(ComSafeArrayAsOutParam(aGlobalExtraDataKeys))))
2089 AssertMsgFailed(("VirtualBox::GetExtraDataKeys failed with %Rrc\n", hrc));
2090
2091 // remember the no. of global values so we can call the correct method below
2092 size_t cGlobalValues = aGlobalExtraDataKeys.size();
2093
2094 if (FAILED(hrc = pMachine->GetExtraDataKeys(ComSafeArrayAsOutParam(aMachineExtraDataKeys))))
2095 AssertMsgFailed(("IMachine::GetExtraDataKeys failed with %Rrc\n", hrc));
2096
2097 // build a combined list from global keys...
2098 std::list<Utf8Str> llExtraDataKeys;
2099
2100 for (size_t i = 0; i < aGlobalExtraDataKeys.size(); ++i)
2101 llExtraDataKeys.push_back(Utf8Str(aGlobalExtraDataKeys[i]));
2102 // ... and machine keys
2103 for (size_t i = 0; i < aMachineExtraDataKeys.size(); ++i)
2104 llExtraDataKeys.push_back(Utf8Str(aMachineExtraDataKeys[i]));
2105
2106 size_t i2 = 0;
2107 for (std::list<Utf8Str>::const_iterator it = llExtraDataKeys.begin();
2108 it != llExtraDataKeys.end();
2109 ++it, ++i2)
2110 {
2111 const Utf8Str &strKey = *it;
2112
2113 /*
2114 * We only care about keys starting with "VBoxInternal/" (skip "G:" or "M:")
2115 */
2116 if (!strKey.startsWith("VBoxInternal/"))
2117 continue;
2118
2119 const char *pszExtraDataKey = strKey.raw() + sizeof("VBoxInternal/") - 1;
2120
2121 // get the value
2122 Bstr strExtraDataValue;
2123 if (i2 < cGlobalValues)
2124 // this is still one of the global values:
2125 hrc = virtualBox->GetExtraData(Bstr(strKey), strExtraDataValue.asOutParam());
2126 else
2127 hrc = pMachine->GetExtraData(Bstr(strKey), strExtraDataValue.asOutParam());
2128 if (FAILED(hrc))
2129 LogRel(("Warning: Cannot get extra data key %s, rc = %Rrc\n", strKey.raw(), hrc));
2130
2131 /*
2132 * The key will be in the format "Node1/Node2/Value" or simply "Value".
2133 * Split the two and get the node, delete the value and create the node
2134 * if necessary.
2135 */
2136 PCFGMNODE pNode;
2137 const char *pszCFGMValueName = strrchr(pszExtraDataKey, '/');
2138 if (pszCFGMValueName)
2139 {
2140 /* terminate the node and advance to the value (Utf8Str might not
2141 offically like this but wtf) */
2142 *(char*)pszCFGMValueName = '\0';
2143 ++pszCFGMValueName;
2144
2145 /* does the node already exist? */
2146 pNode = CFGMR3GetChild(pRoot, pszExtraDataKey);
2147 if (pNode)
2148 CFGMR3RemoveValue(pNode, pszCFGMValueName);
2149 else
2150 {
2151 /* create the node */
2152 rc = CFGMR3InsertNode(pRoot, pszExtraDataKey, &pNode);
2153 if (RT_FAILURE(rc))
2154 {
2155 AssertLogRelMsgRC(rc, ("failed to insert node '%s'\n", pszExtraDataKey));
2156 continue;
2157 }
2158 Assert(pNode);
2159 }
2160 }
2161 else
2162 {
2163 /* root value (no node path). */
2164 pNode = pRoot;
2165 pszCFGMValueName = pszExtraDataKey;
2166 pszExtraDataKey--;
2167 CFGMR3RemoveValue(pNode, pszCFGMValueName);
2168 }
2169
2170 /*
2171 * Now let's have a look at the value.
2172 * Empty strings means that we should remove the value, which we've
2173 * already done above.
2174 */
2175 Utf8Str strCFGMValueUtf8(strExtraDataValue);
2176 const char *pszCFGMValue = strCFGMValueUtf8.raw();
2177 if ( pszCFGMValue
2178 && *pszCFGMValue)
2179 {
2180 uint64_t u64Value;
2181
2182 /* check for type prefix first. */
2183 if (!strncmp(pszCFGMValue, "string:", sizeof("string:") - 1))
2184 rc = CFGMR3InsertString(pNode, pszCFGMValueName, pszCFGMValue + sizeof("string:") - 1);
2185 else if (!strncmp(pszCFGMValue, "integer:", sizeof("integer:") - 1))
2186 {
2187 rc = RTStrToUInt64Full(pszCFGMValue + sizeof("integer:") - 1, 0, &u64Value);
2188 if (RT_SUCCESS(rc))
2189 rc = CFGMR3InsertInteger(pNode, pszCFGMValueName, u64Value);
2190 }
2191 else if (!strncmp(pszCFGMValue, "bytes:", sizeof("bytes:") - 1))
2192 rc = VERR_NOT_IMPLEMENTED;
2193 /* auto detect type. */
2194 else if (RT_SUCCESS(RTStrToUInt64Full(pszCFGMValue, 0, &u64Value)))
2195 rc = CFGMR3InsertInteger(pNode, pszCFGMValueName, u64Value);
2196 else
2197 rc = CFGMR3InsertString(pNode, pszCFGMValueName, pszCFGMValue);
2198 AssertLogRelMsgRC(rc, ("failed to insert CFGM value '%s' to key '%s'\n", pszCFGMValue, pszExtraDataKey));
2199 }
2200 }
2201
2202#undef H
2203#undef RC_CHECK
2204
2205 /* Register VM state change handler */
2206 int rc2 = VMR3AtStateRegister(pVM, Console::vmstateChangeCallback, pConsole);
2207 AssertRC(rc2);
2208 if (RT_SUCCESS(rc))
2209 rc = rc2;
2210
2211 /* Register VM runtime error handler */
2212 rc2 = VMR3AtRuntimeErrorRegister(pVM, Console::setVMRuntimeErrorCallback, pConsole);
2213 AssertRC(rc2);
2214 if (RT_SUCCESS(rc))
2215 rc = rc2;
2216
2217 LogFlowFunc(("vrc = %Rrc\n", rc));
2218 LogFlowFuncLeave();
2219
2220 return rc;
2221}
2222
2223/**
2224 * Ellipsis to va_list wrapper for calling setVMRuntimeErrorCallback.
2225 */
2226/*static*/ void Console::setVMRuntimeErrorCallbackF(PVM pVM, void *pvConsole, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, ...)
2227{
2228 va_list va;
2229 va_start(va, pszFormat);
2230 setVMRuntimeErrorCallback(pVM, pvConsole, fFlags, pszErrorId, pszFormat, va);
2231 va_end(va);
2232}
2233
2234int Console::configMediumAttachment(PCFGMNODE pCtlInst,
2235 const char *pcszDevice,
2236 unsigned uInstance,
2237 StorageBus_T enmBus,
2238 IoBackendType_T enmIoBackend,
2239 bool fSetupMerge,
2240 unsigned uMergeSource,
2241 unsigned uMergeTarget,
2242 IMediumAttachment *pMediumAtt,
2243 MachineState_T aMachineState,
2244 HRESULT *phrc,
2245 bool fAttachDetach,
2246 bool fForceUnmount,
2247 PVM pVM,
2248 DeviceType_T *paLedDevType)
2249{
2250 int rc = VINF_SUCCESS;
2251 HRESULT hrc;
2252 Bstr bstr;
2253
2254#define RC_CHECK() AssertMsgReturn(RT_SUCCESS(rc), ("rc=%Rrc\n", rc), rc)
2255#define H() AssertMsgReturn(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), VERR_GENERAL_FAILURE)
2256
2257 LONG lDev;
2258 hrc = pMediumAtt->COMGETTER(Device)(&lDev); H();
2259 LONG lPort;
2260 hrc = pMediumAtt->COMGETTER(Port)(&lPort); H();
2261 DeviceType_T lType;
2262 hrc = pMediumAtt->COMGETTER(Type)(&lType); H();
2263
2264 unsigned uLUN;
2265 PCFGMNODE pLunL0 = NULL;
2266 PCFGMNODE pCfg = NULL;
2267 hrc = Console::convertBusPortDeviceToLun(enmBus, lPort, lDev, uLUN); H();
2268
2269 /* First check if the LUN already exists. */
2270 pLunL0 = CFGMR3GetChildF(pCtlInst, "LUN#%u", uLUN);
2271 if (pLunL0)
2272 {
2273 if (fAttachDetach)
2274 {
2275 if (lType != DeviceType_HardDisk)
2276 {
2277 /* Unmount existing media only for floppy and DVD drives. */
2278 PPDMIBASE pBase;
2279 rc = PDMR3QueryLun(pVM, pcszDevice, uInstance, uLUN, &pBase);
2280 if (RT_FAILURE(rc))
2281 {
2282 if (rc == VERR_PDM_LUN_NOT_FOUND || rc == VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN)
2283 rc = VINF_SUCCESS;
2284 AssertRC(rc);
2285 }
2286 else
2287 {
2288 PPDMIMOUNT pIMount = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMOUNT);
2289 AssertReturn(pIMount, VERR_INVALID_POINTER);
2290
2291 /* Unmount the media. */
2292 rc = pIMount->pfnUnmount(pIMount, fForceUnmount);
2293 if (rc == VERR_PDM_MEDIA_NOT_MOUNTED)
2294 rc = VINF_SUCCESS;
2295 }
2296 }
2297
2298 rc = PDMR3DeviceDetach(pVM, pcszDevice, 0, uLUN, PDM_TACH_FLAGS_NOT_HOT_PLUG);
2299 if (rc == VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN)
2300 rc = VINF_SUCCESS;
2301 RC_CHECK();
2302
2303 CFGMR3RemoveNode(pLunL0);
2304 }
2305 else
2306 AssertFailedReturn(VERR_INTERNAL_ERROR);
2307 }
2308
2309 rc = CFGMR3InsertNodeF(pCtlInst, &pLunL0, "LUN#%u", uLUN); RC_CHECK();
2310
2311 /* SCSI has a another driver between device and block. */
2312 if (enmBus == StorageBus_SCSI || enmBus == StorageBus_SAS)
2313 {
2314 rc = CFGMR3InsertString(pLunL0, "Driver", "SCSI"); RC_CHECK();
2315 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2316
2317 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK();
2318 }
2319
2320 ComPtr<IMedium> pMedium;
2321 hrc = pMediumAtt->COMGETTER(Medium)(pMedium.asOutParam()); H();
2322 BOOL fPassthrough;
2323 hrc = pMediumAtt->COMGETTER(Passthrough)(&fPassthrough); H();
2324 rc = configMedium(pLunL0,
2325 !!fPassthrough,
2326 lType,
2327 enmIoBackend,
2328 fSetupMerge,
2329 uMergeSource,
2330 uMergeTarget,
2331 pMedium,
2332 aMachineState,
2333 phrc); RC_CHECK();
2334
2335 if (fAttachDetach)
2336 {
2337 /* Attach the new driver. */
2338 rc = PDMR3DeviceAttach(pVM, pcszDevice, 0, uLUN,
2339 PDM_TACH_FLAGS_NOT_HOT_PLUG, NULL /*ppBase*/); RC_CHECK();
2340
2341 /* There is no need to handle removable medium mounting, as we
2342 * unconditionally replace everthing including the block driver level.
2343 * This means the new medium will be picked up automatically. */
2344 }
2345
2346 if (paLedDevType)
2347 paLedDevType[uLUN] = lType;
2348
2349#undef H
2350#undef RC_CHECK
2351
2352 return VINF_SUCCESS;;
2353}
2354
2355int Console::configMedium(PCFGMNODE pLunL0,
2356 bool fPassthrough,
2357 DeviceType_T enmType,
2358 IoBackendType_T enmIoBackend,
2359 bool fSetupMerge,
2360 unsigned uMergeSource,
2361 unsigned uMergeTarget,
2362 IMedium *pMedium,
2363 MachineState_T aMachineState,
2364 HRESULT *phrc)
2365{
2366 int rc = VINF_SUCCESS;
2367 HRESULT hrc;
2368 Bstr bstr;
2369
2370#define RC_CHECK() AssertMsgReturn(RT_SUCCESS(rc), ("rc=%Rrc\n", rc), rc)
2371#define H() AssertMsgReturnStmt(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), if (phrc) *phrc = hrc, VERR_GENERAL_FAILURE)
2372
2373 PCFGMNODE pLunL1 = NULL;
2374 PCFGMNODE pCfg = NULL;
2375
2376 BOOL fHostDrive = FALSE;
2377 if (pMedium)
2378 {
2379 hrc = pMedium->COMGETTER(HostDrive)(&fHostDrive); H();
2380 }
2381
2382 if (fHostDrive)
2383 {
2384 Assert(pMedium);
2385 if (enmType == DeviceType_DVD)
2386 {
2387 rc = CFGMR3InsertString(pLunL0, "Driver", "HostDVD"); RC_CHECK();
2388 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2389
2390 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
2391 rc = CFGMR3InsertStringW(pCfg, "Path", bstr.raw()); RC_CHECK();
2392
2393 rc = CFGMR3InsertInteger(pCfg, "Passthrough", fPassthrough); RC_CHECK();
2394 }
2395 else if (enmType == DeviceType_Floppy)
2396 {
2397 rc = CFGMR3InsertString(pLunL0, "Driver", "HostFloppy"); RC_CHECK();
2398 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2399
2400 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
2401 rc = CFGMR3InsertStringW(pCfg, "Path", bstr.raw()); RC_CHECK();
2402 }
2403 }
2404 else
2405 {
2406 rc = CFGMR3InsertString(pLunL0, "Driver", "Block"); RC_CHECK();
2407 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2408 switch (enmType)
2409 {
2410 case DeviceType_DVD:
2411 rc = CFGMR3InsertString(pCfg, "Type", "DVD"); RC_CHECK();
2412 rc = CFGMR3InsertInteger(pCfg, "Mountable", 1); RC_CHECK();
2413 break;
2414 case DeviceType_Floppy:
2415 rc = CFGMR3InsertString(pCfg, "Type", "Floppy 1.44"); RC_CHECK();
2416 rc = CFGMR3InsertInteger(pCfg, "Mountable", 1); RC_CHECK();
2417 break;
2418 case DeviceType_HardDisk:
2419 default:
2420 rc = CFGMR3InsertString(pCfg, "Type", "HardDisk"); RC_CHECK();
2421 rc = CFGMR3InsertInteger(pCfg, "Mountable", 0); RC_CHECK();
2422 }
2423
2424 if ( pMedium
2425 && ( enmType == DeviceType_DVD
2426 || enmType == DeviceType_Floppy
2427 ))
2428 {
2429 // if this medium represents an ISO image and this image is inaccessible,
2430 // the ignore it instead of causing a failure; this can happen when we
2431 // restore a VM state and the ISO has disappeared, e.g. because the Guest
2432 // Additions were mounted and the user upgraded VirtualBox. Previously
2433 // we failed on startup, but that's not good because the only way out then
2434 // would be to discard the VM state...
2435 MediumState_T mediumState;
2436 rc = pMedium->RefreshState(&mediumState);
2437 RC_CHECK();
2438 if (mediumState == MediumState_Inaccessible)
2439 {
2440 Bstr loc;
2441 rc = pMedium->COMGETTER(Location)(loc.asOutParam());
2442 if (FAILED(rc)) return rc;
2443
2444 setVMRuntimeErrorCallbackF(mpVM,
2445 this,
2446 0,
2447 "DvdOrFloppyImageInaccessible",
2448 "The image file '%ls' is inaccessible and is being ignored. Please select a different image file for the virtual %s drive.",
2449 loc.raw(),
2450 (enmType == DeviceType_DVD) ? "DVD" : "floppy");
2451 pMedium = NULL;
2452 }
2453 }
2454
2455 if (pMedium)
2456 {
2457 /* Start with length of parent chain, as the list is reversed */
2458 unsigned uImage = 0;
2459 IMedium *pTmp = pMedium;
2460 while (pTmp)
2461 {
2462 uImage++;
2463 hrc = pTmp->COMGETTER(Parent)(&pTmp); H();
2464 }
2465 /* Index of last image */
2466 uImage--;
2467
2468 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); RC_CHECK();
2469 rc = CFGMR3InsertString(pLunL1, "Driver", "VD"); RC_CHECK();
2470 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); RC_CHECK();
2471
2472 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
2473 rc = CFGMR3InsertStringW(pCfg, "Path", bstr.raw()); RC_CHECK();
2474
2475 hrc = pMedium->COMGETTER(Format)(bstr.asOutParam()); H();
2476 rc = CFGMR3InsertStringW(pCfg, "Format", bstr.raw()); RC_CHECK();
2477
2478 /* DVDs are always readonly */
2479 if (enmType == DeviceType_DVD)
2480 {
2481 rc = CFGMR3InsertInteger(pCfg, "ReadOnly", 1); RC_CHECK();
2482 }
2483
2484 /* Start without exclusive write access to the images. */
2485 /** @todo Live Migration: I don't quite like this, we risk screwing up when
2486 * we're resuming the VM if some 3rd dude have any of the VDIs open
2487 * with write sharing denied. However, if the two VMs are sharing a
2488 * image it really is necessary....
2489 *
2490 * So, on the "lock-media" command, the target teleporter should also
2491 * make DrvVD undo TempReadOnly. It gets interesting if we fail after
2492 * that. Grumble. */
2493 else if (aMachineState == MachineState_TeleportingIn)
2494 {
2495 rc = CFGMR3InsertInteger(pCfg, "TempReadOnly", 1); RC_CHECK();
2496 }
2497
2498 if (enmIoBackend == IoBackendType_Unbuffered)
2499 {
2500 rc = CFGMR3InsertInteger(pCfg, "UseNewIo", 1); RC_CHECK();
2501 }
2502
2503 if (fSetupMerge)
2504 {
2505 rc = CFGMR3InsertInteger(pCfg, "SetupMerge", 1); RC_CHECK();
2506 if (uImage == uMergeSource)
2507 {
2508 rc = CFGMR3InsertInteger(pCfg, "MergeSource", 1); RC_CHECK();
2509 }
2510 else if (uImage == uMergeTarget)
2511 {
2512 rc = CFGMR3InsertInteger(pCfg, "MergeTarget", 1); RC_CHECK();
2513 }
2514 }
2515
2516 /* Pass all custom parameters. */
2517 bool fHostIP = true;
2518 SafeArray<BSTR> names;
2519 SafeArray<BSTR> values;
2520 hrc = pMedium->GetProperties(NULL,
2521 ComSafeArrayAsOutParam(names),
2522 ComSafeArrayAsOutParam(values)); H();
2523
2524 if (names.size() != 0)
2525 {
2526 PCFGMNODE pVDC;
2527 rc = CFGMR3InsertNode(pCfg, "VDConfig", &pVDC); RC_CHECK();
2528 for (size_t ii = 0; ii < names.size(); ++ii)
2529 {
2530 if (values[ii] && *values[ii])
2531 {
2532 Utf8Str name = names[ii];
2533 Utf8Str value = values[ii];
2534 rc = CFGMR3InsertString(pVDC, name.c_str(), value.c_str()); RC_CHECK();
2535 if ( name.compare("HostIPStack") == 0
2536 && value.compare("0") == 0)
2537 fHostIP = false;
2538 }
2539 }
2540 }
2541
2542 /* Create an inversed list of parents. */
2543 uImage--;
2544 IMedium *pParentMedium = pMedium;
2545 for (PCFGMNODE pParent = pCfg;; uImage--)
2546 {
2547 hrc = pParentMedium->COMGETTER(Parent)(&pMedium); H();
2548 if (!pMedium)
2549 break;
2550
2551 PCFGMNODE pCur;
2552 rc = CFGMR3InsertNode(pParent, "Parent", &pCur); RC_CHECK();
2553 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
2554 rc = CFGMR3InsertStringW(pCur, "Path", bstr.raw()); RC_CHECK();
2555
2556 hrc = pMedium->COMGETTER(Format)(bstr.asOutParam()); H();
2557 rc = CFGMR3InsertStringW(pCur, "Format", bstr.raw()); RC_CHECK();
2558
2559 if (fSetupMerge)
2560 {
2561 if (uImage == uMergeSource)
2562 {
2563 rc = CFGMR3InsertInteger(pCur, "MergeSource", 1); RC_CHECK();
2564 }
2565 else if (uImage == uMergeTarget)
2566 {
2567 rc = CFGMR3InsertInteger(pCur, "MergeTarget", 1); RC_CHECK();
2568 }
2569 }
2570
2571 /* Pass all custom parameters. */
2572 SafeArray<BSTR> aNames;
2573 SafeArray<BSTR> aValues;
2574 hrc = pMedium->GetProperties(NULL,
2575 ComSafeArrayAsOutParam(aNames),
2576 ComSafeArrayAsOutParam(aValues)); H();
2577
2578 if (aNames.size() != 0)
2579 {
2580 PCFGMNODE pVDC;
2581 rc = CFGMR3InsertNode(pCur, "VDConfig", &pVDC); RC_CHECK();
2582 for (size_t ii = 0; ii < aNames.size(); ++ii)
2583 {
2584 if (aValues[ii] && *aValues[ii])
2585 {
2586 Utf8Str name = aNames[ii];
2587 Utf8Str value = aValues[ii];
2588 rc = CFGMR3InsertString(pVDC, name.c_str(), value.c_str()); RC_CHECK();
2589 if ( name.compare("HostIPStack") == 0
2590 && value.compare("0") == 0)
2591 fHostIP = false;
2592 }
2593 }
2594 }
2595
2596 /* Custom code: put marker to not use host IP stack to driver
2597 * configuration node. Simplifies life of DrvVD a bit. */
2598 if (!fHostIP)
2599 {
2600 rc = CFGMR3InsertInteger(pCfg, "HostIPStack", 0); RC_CHECK();
2601 }
2602
2603 /* next */
2604 pParent = pCur;
2605 pParentMedium = pMedium;
2606 }
2607 }
2608 }
2609
2610#undef H
2611#undef RC_CHECK
2612
2613 return VINF_SUCCESS;
2614}
2615
2616/**
2617 * Construct the Network configuration tree
2618 *
2619 * @returns VBox status code.
2620 *
2621 * @param pszDevice The PDM device name.
2622 * @param uInstance The PDM device instance.
2623 * @param uLun The PDM LUN number of the drive.
2624 * @param aNetworkAdapter The network adapter whose attachment needs to be changed
2625 * @param pCfg Configuration node for the device
2626 * @param pLunL0 To store the pointer to the LUN#0.
2627 * @param pInst The instance CFGM node
2628 * @param fAttachDetach To determine if the network attachment should
2629 * be attached/detached after/before
2630 * configuration.
2631 *
2632 * @note Locks this object for writing.
2633 */
2634int Console::configNetwork(const char *pszDevice, unsigned uInstance,
2635 unsigned uLun, INetworkAdapter *aNetworkAdapter,
2636 PCFGMNODE pCfg, PCFGMNODE pLunL0, PCFGMNODE pInst,
2637 bool fAttachDetach)
2638{
2639 AutoCaller autoCaller(this);
2640 AssertComRCReturn(autoCaller.rc(), VERR_ACCESS_DENIED);
2641
2642 int rc = VINF_SUCCESS;
2643 HRESULT hrc;
2644 Bstr bstr;
2645
2646#define RC_CHECK() AssertMsgReturn(RT_SUCCESS(rc), ("rc=%Rrc\n", rc), rc)
2647#define H() AssertMsgReturn(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), VERR_GENERAL_FAILURE)
2648
2649 /*
2650 * Locking the object before doing VMR3* calls is quite safe here, since
2651 * we're on EMT. Write lock is necessary because we indirectly modify the
2652 * meAttachmentType member.
2653 */
2654 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2655
2656 PVM pVM = mpVM;
2657
2658 ComPtr<IMachine> pMachine = machine();
2659
2660 ComPtr<IVirtualBox> virtualBox;
2661 hrc = pMachine->COMGETTER(Parent)(virtualBox.asOutParam());
2662 H();
2663
2664 ComPtr<IHost> host;
2665 hrc = virtualBox->COMGETTER(Host)(host.asOutParam());
2666 H();
2667
2668 BOOL fSniffer;
2669 hrc = aNetworkAdapter->COMGETTER(TraceEnabled)(&fSniffer);
2670 H();
2671
2672 if (fAttachDetach && fSniffer)
2673 {
2674 const char *pszNetDriver = "IntNet";
2675 if (meAttachmentType[uInstance] == NetworkAttachmentType_NAT)
2676 pszNetDriver = "NAT";
2677#if !defined(VBOX_WITH_NETFLT) && defined(RT_OS_LINUX)
2678 if (meAttachmentType[uInstance] == NetworkAttachmentType_Bridged)
2679 pszNetDriver = "HostInterface";
2680#endif
2681
2682 rc = PDMR3DriverDetach(pVM, pszDevice, uInstance, uLun, pszNetDriver, 0, 0 /*fFlags*/);
2683 if (rc == VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN)
2684 rc = VINF_SUCCESS;
2685 AssertLogRelRCReturn(rc, rc);
2686
2687 pLunL0 = CFGMR3GetChildF(pInst, "LUN#%u", uLun);
2688 PCFGMNODE pLunAD = CFGMR3GetChildF(pLunL0, "AttachedDriver");
2689 if (pLunAD)
2690 {
2691 CFGMR3RemoveNode(pLunAD);
2692 }
2693 else
2694 {
2695 CFGMR3RemoveNode(pLunL0);
2696 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
2697 rc = CFGMR3InsertString(pLunL0, "Driver", "NetSniffer"); RC_CHECK();
2698 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2699 hrc = aNetworkAdapter->COMGETTER(TraceFile)(bstr.asOutParam()); H();
2700 if (!bstr.isEmpty()) /* check convention for indicating default file. */
2701 {
2702 rc = CFGMR3InsertStringW(pCfg, "File", bstr.raw()); RC_CHECK();
2703 }
2704 }
2705 }
2706 else if (fAttachDetach && !fSniffer)
2707 {
2708 rc = PDMR3DeviceDetach(pVM, pszDevice, uInstance, uLun, 0 /*fFlags*/);
2709 if (rc == VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN)
2710 rc = VINF_SUCCESS;
2711 AssertLogRelRCReturn(rc, rc);
2712
2713 /* nuke anything which might have been left behind. */
2714 CFGMR3RemoveNode(CFGMR3GetChildF(pInst, "LUN#%u", uLun));
2715 }
2716 else if (!fAttachDetach && fSniffer)
2717 {
2718 /* insert the sniffer filter driver. */
2719 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
2720 rc = CFGMR3InsertString(pLunL0, "Driver", "NetSniffer"); RC_CHECK();
2721 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2722 hrc = aNetworkAdapter->COMGETTER(TraceFile)(bstr.asOutParam()); H();
2723 if (!bstr.isEmpty()) /* check convention for indicating default file. */
2724 {
2725 rc = CFGMR3InsertStringW(pCfg, "File", bstr.raw()); RC_CHECK();
2726 }
2727 }
2728
2729 Bstr networkName, trunkName, trunkType;
2730 NetworkAttachmentType_T eAttachmentType;
2731 hrc = aNetworkAdapter->COMGETTER(AttachmentType)(&eAttachmentType); H();
2732 switch (eAttachmentType)
2733 {
2734 case NetworkAttachmentType_Null:
2735 break;
2736
2737 case NetworkAttachmentType_NAT:
2738 {
2739 ComPtr<INATEngine> natDriver;
2740 hrc = aNetworkAdapter->COMGETTER(NatDriver)(natDriver.asOutParam()); H();
2741 if (fSniffer)
2742 {
2743 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK();
2744 }
2745 else
2746 {
2747 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
2748 }
2749 rc = CFGMR3InsertString(pLunL0, "Driver", "NAT"); RC_CHECK();
2750 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2751
2752 /* Configure TFTP prefix and boot filename. */
2753 hrc = virtualBox->COMGETTER(HomeFolder)(bstr.asOutParam()); H();
2754 if (!bstr.isEmpty())
2755 {
2756 rc = CFGMR3InsertStringF(pCfg, "TFTPPrefix", "%ls%c%s", bstr.raw(), RTPATH_DELIMITER, "TFTP"); RC_CHECK();
2757 }
2758 hrc = pMachine->COMGETTER(Name)(bstr.asOutParam()); H();
2759 rc = CFGMR3InsertStringF(pCfg, "BootFile", "%ls.pxe", bstr.raw()); RC_CHECK();
2760
2761 hrc = natDriver->COMGETTER(Network)(bstr.asOutParam()); H();
2762 if (!bstr.isEmpty())
2763 {
2764 rc = CFGMR3InsertStringW(pCfg, "Network", bstr.raw()); RC_CHECK();
2765 }
2766 else
2767 {
2768 ULONG uSlot;
2769 hrc = aNetworkAdapter->COMGETTER(Slot)(&uSlot); H();
2770 rc = CFGMR3InsertStringF(pCfg, "Network", "10.0.%d.0/24", uSlot+2); RC_CHECK();
2771 }
2772 hrc = natDriver->COMGETTER(HostIP)(bstr.asOutParam()); H();
2773 if (!bstr.isEmpty())
2774 {
2775 rc = CFGMR3InsertStringW(pCfg, "BindIP", bstr.raw()); RC_CHECK();
2776 }
2777 ULONG mtu = 0;
2778 ULONG sockSnd = 0;
2779 ULONG sockRcv = 0;
2780 ULONG tcpSnd = 0;
2781 ULONG tcpRcv = 0;
2782 hrc = natDriver->GetNetworkSettings(&mtu, &sockSnd, &sockRcv, &tcpSnd, &tcpRcv); H();
2783 if (mtu)
2784 {
2785 rc = CFGMR3InsertInteger(pCfg, "SlirpMTU", mtu); RC_CHECK();
2786 }
2787 if (sockRcv)
2788 {
2789 rc = CFGMR3InsertInteger(pCfg, "SockRcv", sockRcv); RC_CHECK();
2790 }
2791 if (sockSnd)
2792 {
2793 rc = CFGMR3InsertInteger(pCfg, "SockSnd", sockSnd); RC_CHECK();
2794 }
2795 if (tcpRcv)
2796 {
2797 rc = CFGMR3InsertInteger(pCfg, "TcpRcv", tcpRcv); RC_CHECK();
2798 }
2799 if (tcpSnd)
2800 {
2801 rc = CFGMR3InsertInteger(pCfg, "TcpSnd", tcpSnd); RC_CHECK();
2802 }
2803 hrc = natDriver->COMGETTER(TftpPrefix)(bstr.asOutParam()); H();
2804 if (!bstr.isEmpty())
2805 {
2806 rc = CFGMR3RemoveValue(pCfg, "TFTPPrefix"); RC_CHECK();
2807 rc = CFGMR3InsertStringW(pCfg, "TFTPPrefix", bstr); RC_CHECK();
2808 }
2809 hrc = natDriver->COMGETTER(TftpBootFile)(bstr.asOutParam()); H();
2810 if (!bstr.isEmpty())
2811 {
2812 rc = CFGMR3RemoveValue(pCfg, "BootFile"); RC_CHECK();
2813 rc = CFGMR3InsertStringW(pCfg, "BootFile", bstr); RC_CHECK();
2814 }
2815 hrc = natDriver->COMGETTER(TftpNextServer)(bstr.asOutParam()); H();
2816 if (!bstr.isEmpty())
2817 {
2818 rc = CFGMR3InsertStringW(pCfg, "NextServer", bstr); RC_CHECK();
2819 }
2820 BOOL fDnsFlag;
2821 hrc = natDriver->COMGETTER(DnsPassDomain)(&fDnsFlag); H();
2822 rc = CFGMR3InsertInteger(pCfg, "PassDomain", fDnsFlag); RC_CHECK();
2823 hrc = natDriver->COMGETTER(DnsProxy)(&fDnsFlag); H();
2824 rc = CFGMR3InsertInteger(pCfg, "DNSProxy", fDnsFlag); RC_CHECK();
2825 hrc = natDriver->COMGETTER(DnsUseHostResolver)(&fDnsFlag); H();
2826 rc = CFGMR3InsertInteger(pCfg, "UseHostResolver", fDnsFlag); RC_CHECK();
2827
2828 ULONG aliasMode;
2829 hrc = natDriver->COMGETTER(AliasMode)(&aliasMode); H();
2830 rc = CFGMR3InsertInteger(pCfg, "AliasMode", aliasMode); RC_CHECK();
2831
2832 /* port-forwarding */
2833 SafeArray<BSTR> pfs;
2834 hrc = natDriver->COMGETTER(Redirects)(ComSafeArrayAsOutParam(pfs)); H();
2835 PCFGMNODE pPF = NULL; /* /Devices/Dev/.../Config/PF#0/ */
2836 for (unsigned int i = 0; i < pfs.size(); ++i)
2837 {
2838 uint16_t port = 0;
2839 BSTR r = pfs[i];
2840 Utf8Str utf = Utf8Str(r);
2841 Utf8Str strName;
2842 Utf8Str strProto;
2843 Utf8Str strHostPort;
2844 Utf8Str strHostIP;
2845 Utf8Str strGuestPort;
2846 Utf8Str strGuestIP;
2847 size_t pos, ppos;
2848 pos = ppos = 0;
2849#define ITERATE_TO_NEXT_TERM(res, str, pos, ppos) \
2850 do { \
2851 pos = str.find(",", ppos); \
2852 if (pos == Utf8Str::npos) \
2853 { \
2854 Log(( #res " extracting from %s is failed\n", str.raw())); \
2855 continue; \
2856 } \
2857 res = str.substr(ppos, pos - ppos); \
2858 Log2((#res " %s pos:%d, ppos:%d\n", res.raw(), pos, ppos)); \
2859 ppos = pos + 1; \
2860 } while (0)
2861 ITERATE_TO_NEXT_TERM(strName, utf, pos, ppos);
2862 ITERATE_TO_NEXT_TERM(strProto, utf, pos, ppos);
2863 ITERATE_TO_NEXT_TERM(strHostIP, utf, pos, ppos);
2864 ITERATE_TO_NEXT_TERM(strHostPort, utf, pos, ppos);
2865 ITERATE_TO_NEXT_TERM(strGuestIP, utf, pos, ppos);
2866 strGuestPort = utf.substr(ppos, utf.length() - ppos);
2867#undef ITERATE_TO_NEXT_TERM
2868
2869 uint32_t proto = strProto.toUInt32();
2870 bool fValid = true;
2871 switch (proto)
2872 {
2873 case NATProtocol_UDP:
2874 strProto = "UDP";
2875 break;
2876 case NATProtocol_TCP:
2877 strProto = "TCP";
2878 break;
2879 default:
2880 fValid = false;
2881 }
2882 /* continue with next rule if no valid proto was passed */
2883 if (!fValid)
2884 continue;
2885
2886 rc = CFGMR3InsertNode(pCfg, strName.raw(), &pPF); RC_CHECK();
2887 rc = CFGMR3InsertString(pPF, "Protocol", strProto.raw()); RC_CHECK();
2888
2889 if (!strHostIP.isEmpty())
2890 {
2891 rc = CFGMR3InsertString(pPF, "BindIP", strHostIP.raw()); RC_CHECK();
2892 }
2893
2894 if (!strGuestIP.isEmpty())
2895 {
2896 rc = CFGMR3InsertString(pPF, "GuestIP", strGuestIP.raw()); RC_CHECK();
2897 }
2898
2899 port = RTStrToUInt16(strHostPort.raw());
2900 if (port)
2901 {
2902 rc = CFGMR3InsertInteger(pPF, "HostPort", port); RC_CHECK();
2903 }
2904
2905 port = RTStrToUInt16(strGuestPort.raw());
2906 if (port)
2907 {
2908 rc = CFGMR3InsertInteger(pPF, "GuestPort", port); RC_CHECK();
2909 }
2910 }
2911 break;
2912 }
2913
2914 case NetworkAttachmentType_Bridged:
2915 {
2916#if (defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)) && !defined(VBOX_WITH_NETFLT)
2917 hrc = attachToTapInterface(aNetworkAdapter);
2918 if (FAILED(hrc))
2919 {
2920 switch (hrc)
2921 {
2922 case VERR_ACCESS_DENIED:
2923 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
2924 "Failed to open '/dev/net/tun' for read/write access. Please check the "
2925 "permissions of that node. Either run 'chmod 0666 /dev/net/tun' or "
2926 "change the group of that node and make yourself a member of that group. Make "
2927 "sure that these changes are permanent, especially if you are "
2928 "using udev"));
2929 default:
2930 AssertMsgFailed(("Could not attach to host interface! Bad!\n"));
2931 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
2932 "Failed to initialize Host Interface Networking"));
2933 }
2934 }
2935
2936 Assert((int)maTapFD[uInstance] >= 0);
2937 if ((int)maTapFD[uInstance] >= 0)
2938 {
2939 if (fSniffer)
2940 {
2941 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK();
2942 }
2943 else
2944 {
2945 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
2946 }
2947 rc = CFGMR3InsertString(pLunL0, "Driver", "HostInterface"); RC_CHECK();
2948 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2949 rc = CFGMR3InsertInteger(pCfg, "FileHandle", maTapFD[uInstance]); RC_CHECK();
2950 }
2951
2952#elif defined(VBOX_WITH_NETFLT)
2953 /*
2954 * This is the new VBoxNetFlt+IntNet stuff.
2955 */
2956 if (fSniffer)
2957 {
2958 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK();
2959 }
2960 else
2961 {
2962 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
2963 }
2964
2965 Bstr HifName;
2966 hrc = aNetworkAdapter->COMGETTER(HostInterface)(HifName.asOutParam());
2967 if (FAILED(hrc))
2968 {
2969 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(HostInterface) failed, hrc (0x%x)", hrc));
2970 H();
2971 }
2972
2973 Utf8Str HifNameUtf8(HifName);
2974 const char *pszHifName = HifNameUtf8.raw();
2975
2976# if defined(RT_OS_DARWIN)
2977 /* The name is on the form 'ifX: long name', chop it off at the colon. */
2978 char szTrunk[8];
2979 strncpy(szTrunk, pszHifName, sizeof(szTrunk));
2980 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk));
2981 if (!pszColon)
2982 {
2983 /*
2984 * Dynamic changing of attachment causes an attempt to configure
2985 * network with invalid host adapter (as it is must be changed before
2986 * the attachment), calling Detach here will cause a deadlock.
2987 * See #4750.
2988 * hrc = aNetworkAdapter->Detach(); H();
2989 */
2990 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
2991 N_("Malformed host interface networking name '%ls'"),
2992 HifName.raw());
2993 }
2994 *pszColon = '\0';
2995 const char *pszTrunk = szTrunk;
2996
2997# elif defined(RT_OS_SOLARIS)
2998 /* The name is on the form format 'ifX[:1] - long name, chop it off at space. */
2999 char szTrunk[256];
3000 strlcpy(szTrunk, pszHifName, sizeof(szTrunk));
3001 char *pszSpace = (char *)memchr(szTrunk, ' ', sizeof(szTrunk));
3002
3003 /*
3004 * Currently don't bother about malformed names here for the sake of people using
3005 * VBoxManage and setting only the NIC name from there. If there is a space we
3006 * chop it off and proceed, otherwise just use whatever we've got.
3007 */
3008 if (pszSpace)
3009 *pszSpace = '\0';
3010
3011 /* Chop it off at the colon (zone naming eg: e1000g:1 we need only the e1000g) */
3012 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk));
3013 if (pszColon)
3014 *pszColon = '\0';
3015
3016 const char *pszTrunk = szTrunk;
3017
3018# elif defined(RT_OS_WINDOWS)
3019 ComPtr<IHostNetworkInterface> hostInterface;
3020 hrc = host->FindHostNetworkInterfaceByName(HifName, hostInterface.asOutParam());
3021 if (!SUCCEEDED(hrc))
3022 {
3023 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: FindByName failed, rc=%Rhrc (0x%x)", hrc, hrc));
3024 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
3025 N_("Inexistent host networking interface, name '%ls'"),
3026 HifName.raw());
3027 }
3028
3029 HostNetworkInterfaceType_T eIfType;
3030 hrc = hostInterface->COMGETTER(InterfaceType)(&eIfType);
3031 if (FAILED(hrc))
3032 {
3033 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(InterfaceType) failed, hrc (0x%x)", hrc));
3034 H();
3035 }
3036
3037 if (eIfType != HostNetworkInterfaceType_Bridged)
3038 {
3039 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
3040 N_("Interface ('%ls') is not a Bridged Adapter interface"),
3041 HifName.raw());
3042 }
3043
3044 hrc = hostInterface->COMGETTER(Id)(bstr.asOutParam());
3045 if (FAILED(hrc))
3046 {
3047 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(Id) failed, hrc (0x%x)", hrc));
3048 H();
3049 }
3050 Guid hostIFGuid(bstr);
3051
3052 INetCfg *pNc;
3053 ComPtr<INetCfgComponent> pAdaptorComponent;
3054 LPWSTR pszApp;
3055 int rc = VERR_INTNET_FLT_IF_NOT_FOUND;
3056
3057 hrc = VBoxNetCfgWinQueryINetCfg(FALSE /*fGetWriteLock*/,
3058 L"VirtualBox",
3059 &pNc,
3060 &pszApp);
3061 Assert(hrc == S_OK);
3062 if (hrc == S_OK)
3063 {
3064 /* get the adapter's INetCfgComponent*/
3065 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.ptr(), pAdaptorComponent.asOutParam());
3066 if (hrc != S_OK)
3067 {
3068 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3069 LogRel(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc));
3070 H();
3071 }
3072 }
3073#define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\"
3074 char szTrunkName[INTNET_MAX_TRUNK_NAME];
3075 char *pszTrunkName = szTrunkName;
3076 wchar_t * pswzBindName;
3077 hrc = pAdaptorComponent->GetBindName(&pswzBindName);
3078 Assert(hrc == S_OK);
3079 if (hrc == S_OK)
3080 {
3081 int cwBindName = (int)wcslen(pswzBindName) + 1;
3082 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX);
3083 if (sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName)
3084 {
3085 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX);
3086 pszTrunkName += cbFullBindNamePrefix-1;
3087 if (!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName,
3088 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL))
3089 {
3090 DWORD err = GetLastError();
3091 hrc = HRESULT_FROM_WIN32(err);
3092 AssertMsgFailed(("%hrc=%Rhrc %#x\n", hrc, hrc));
3093 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: WideCharToMultiByte failed, hr=%Rhrc (0x%x) err=%u\n", hrc, hrc, err));
3094 }
3095 }
3096 else
3097 {
3098 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: insufficient szTrunkName buffer space\n"));
3099 /** @todo set appropriate error code */
3100 hrc = E_FAIL;
3101 }
3102
3103 if (hrc != S_OK)
3104 {
3105 AssertFailed();
3106 CoTaskMemFree(pswzBindName);
3107 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3108 H();
3109 }
3110
3111 /* we're not freeing the bind name since we'll use it later for detecting wireless*/
3112 }
3113 else
3114 {
3115 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3116 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc));
3117 H();
3118 }
3119 const char *pszTrunk = szTrunkName;
3120 /* we're not releasing the INetCfg stuff here since we use it later to figure out whether it is wireless */
3121
3122# elif defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)
3123# if defined(RT_OS_FREEBSD)
3124 /*
3125 * If we bridge to a tap interface open it the `old' direct way.
3126 * This works and performs better than bridging a physical
3127 * interface via the current FreeBSD vboxnetflt implementation.
3128 */
3129 if (!strncmp(pszHifName, "tap", sizeof "tap" - 1)) {
3130 hrc = attachToTapInterface(aNetworkAdapter);
3131 if (FAILED(hrc))
3132 {
3133 switch (hrc)
3134 {
3135 case VERR_ACCESS_DENIED:
3136 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
3137 "Failed to open '/dev/%s' for read/write access. Please check the "
3138 "permissions of that node, and that the net.link.tap.user_open "
3139 "sysctl is set. Either run 'chmod 0666 /dev/%s' or "
3140 "change the group of that node to vboxusers and make yourself "
3141 "a member of that group. Make sure that these changes are permanent."), pszHifName, pszHifName);
3142 default:
3143 AssertMsgFailed(("Could not attach to tap interface! Bad!\n"));
3144 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
3145 "Failed to initialize Host Interface Networking"));
3146 }
3147 }
3148
3149 Assert((int)maTapFD[uInstance] >= 0);
3150 if ((int)maTapFD[uInstance] >= 0)
3151 {
3152 rc = CFGMR3InsertString(pLunL0, "Driver", "HostInterface"); RC_CHECK();
3153 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
3154 rc = CFGMR3InsertInteger(pCfg, "FileHandle", maTapFD[uInstance]); RC_CHECK();
3155 }
3156 break;
3157 }
3158# endif
3159 /** @todo Check for malformed names. */
3160 const char *pszTrunk = pszHifName;
3161
3162 /* Issue a warning if the interface is down */
3163 {
3164 int iSock = socket(AF_INET, SOCK_DGRAM, 0);
3165 if (iSock >= 0)
3166 {
3167 struct ifreq Req;
3168
3169 memset(&Req, 0, sizeof(Req));
3170 strncpy(Req.ifr_name, pszHifName, sizeof(Req.ifr_name) - 1);
3171 if (ioctl(iSock, SIOCGIFFLAGS, &Req) >= 0)
3172 if ((Req.ifr_flags & IFF_UP) == 0)
3173 {
3174 setVMRuntimeErrorCallbackF(pVM, this, 0, "BridgedInterfaceDown", "Bridged interface %s is down. Guest will not be able to use this interface", pszHifName);
3175 }
3176
3177 close(iSock);
3178 }
3179 }
3180
3181# else
3182# error "PORTME (VBOX_WITH_NETFLT)"
3183# endif
3184
3185 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); RC_CHECK();
3186 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
3187 rc = CFGMR3InsertString(pCfg, "Trunk", pszTrunk); RC_CHECK();
3188 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt);
3189 RC_CHECK();
3190 char szNetwork[INTNET_MAX_NETWORK_NAME];
3191 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHifName);
3192 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); RC_CHECK();
3193 networkName = Bstr(szNetwork);
3194 trunkName = Bstr(pszTrunk);
3195 trunkType = Bstr(TRUNKTYPE_NETFLT);
3196
3197# if defined(RT_OS_DARWIN)
3198 /** @todo Come up with a better deal here. Problem is that IHostNetworkInterface is completely useless here. */
3199 if ( strstr(pszHifName, "Wireless")
3200 || strstr(pszHifName, "AirPort" ))
3201 {
3202 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); RC_CHECK();
3203 }
3204# elif defined(RT_OS_LINUX)
3205 int iSock = socket(AF_INET, SOCK_DGRAM, 0);
3206 if (iSock >= 0)
3207 {
3208 struct iwreq WRq;
3209
3210 memset(&WRq, 0, sizeof(WRq));
3211 strncpy(WRq.ifr_name, pszHifName, IFNAMSIZ);
3212 bool fSharedMacOnWire = ioctl(iSock, SIOCGIWNAME, &WRq) >= 0;
3213 close(iSock);
3214 if (fSharedMacOnWire)
3215 {
3216 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true);
3217 RC_CHECK();
3218 Log(("Set SharedMacOnWire\n"));
3219 }
3220 else
3221 Log(("Failed to get wireless name\n"));
3222 }
3223 else
3224 Log(("Failed to open wireless socket\n"));
3225# elif defined(RT_OS_FREEBSD)
3226 int iSock = socket(AF_INET, SOCK_DGRAM, 0);
3227 if (iSock >= 0)
3228 {
3229 struct ieee80211req WReq;
3230 uint8_t abData[32];
3231
3232 memset(&WReq, 0, sizeof(WReq));
3233 strncpy(WReq.i_name, pszHifName, sizeof(WReq.i_name));
3234 WReq.i_type = IEEE80211_IOC_SSID;
3235 WReq.i_val = -1;
3236 WReq.i_data = abData;
3237 WReq.i_len = sizeof(abData);
3238
3239 bool fSharedMacOnWire = ioctl(iSock, SIOCG80211, &WReq) >= 0;
3240 close(iSock);
3241 if (fSharedMacOnWire)
3242 {
3243 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true);
3244 RC_CHECK();
3245 Log(("Set SharedMacOnWire\n"));
3246 }
3247 else
3248 Log(("Failed to get wireless name\n"));
3249 }
3250 else
3251 Log(("Failed to open wireless socket\n"));
3252# elif defined(RT_OS_WINDOWS)
3253# define DEVNAME_PREFIX L"\\\\.\\"
3254 /* we are getting the medium type via IOCTL_NDIS_QUERY_GLOBAL_STATS Io Control
3255 * there is a pretty long way till there though since we need to obtain the symbolic link name
3256 * for the adapter device we are going to query given the device Guid */
3257
3258
3259 /* prepend the "\\\\.\\" to the bind name to obtain the link name */
3260
3261 wchar_t FileName[MAX_PATH];
3262 wcscpy(FileName, DEVNAME_PREFIX);
3263 wcscpy((wchar_t*)(((char*)FileName) + sizeof(DEVNAME_PREFIX) - sizeof(FileName[0])), pswzBindName);
3264
3265 /* open the device */
3266 HANDLE hDevice = CreateFile(FileName,
3267 GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
3268 NULL,
3269 OPEN_EXISTING,
3270 FILE_ATTRIBUTE_NORMAL,
3271 NULL);
3272
3273 if (hDevice != INVALID_HANDLE_VALUE)
3274 {
3275 bool fSharedMacOnWire = false;
3276
3277 /* now issue the OID_GEN_PHYSICAL_MEDIUM query */
3278 DWORD Oid = OID_GEN_PHYSICAL_MEDIUM;
3279 NDIS_PHYSICAL_MEDIUM PhMedium;
3280 DWORD cbResult;
3281 if (DeviceIoControl(hDevice,
3282 IOCTL_NDIS_QUERY_GLOBAL_STATS,
3283 &Oid,
3284 sizeof(Oid),
3285 &PhMedium,
3286 sizeof(PhMedium),
3287 &cbResult,
3288 NULL))
3289 {
3290 /* that was simple, now examine PhMedium */
3291 if ( PhMedium == NdisPhysicalMediumWirelessWan
3292 || PhMedium == NdisPhysicalMediumWirelessLan
3293 || PhMedium == NdisPhysicalMediumNative802_11
3294 || PhMedium == NdisPhysicalMediumBluetooth)
3295 fSharedMacOnWire = true;
3296 }
3297 else
3298 {
3299 int winEr = GetLastError();
3300 LogRel(("Console::configConstructor: DeviceIoControl failed, err (0x%x), ignoring\n", winEr));
3301 Assert(winEr == ERROR_INVALID_PARAMETER || winEr == ERROR_NOT_SUPPORTED || winEr == ERROR_BAD_COMMAND);
3302 }
3303 CloseHandle(hDevice);
3304
3305 if (fSharedMacOnWire)
3306 {
3307 Log(("this is a wireless adapter"));
3308 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); RC_CHECK();
3309 Log(("Set SharedMacOnWire\n"));
3310 }
3311 else
3312 Log(("this is NOT a wireless adapter"));
3313 }
3314 else
3315 {
3316 int winEr = GetLastError();
3317 AssertLogRelMsgFailed(("Console::configConstructor: CreateFile failed, err (0x%x), ignoring\n", winEr));
3318 }
3319
3320 CoTaskMemFree(pswzBindName);
3321
3322 pAdaptorComponent.setNull();
3323 /* release the pNc finally */
3324 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3325# else
3326 /** @todo PORTME: wireless detection */
3327# endif
3328
3329# if defined(RT_OS_SOLARIS)
3330# if 0 /* bird: this is a bit questionable and might cause more trouble than its worth. */
3331 /* Zone access restriction, don't allow snopping the global zone. */
3332 zoneid_t ZoneId = getzoneid();
3333 if (ZoneId != GLOBAL_ZONEID)
3334 {
3335 rc = CFGMR3InsertInteger(pCfg, "IgnoreAllPromisc", true); RC_CHECK();
3336 }
3337# endif
3338# endif
3339
3340#elif defined(RT_OS_WINDOWS) /* not defined NetFlt */
3341 /* NOTHING TO DO HERE */
3342#elif defined(RT_OS_LINUX)
3343/// @todo aleksey: is there anything to be done here?
3344#elif defined(RT_OS_FREEBSD)
3345/** @todo FreeBSD: Check out this later (HIF networking). */
3346#else
3347# error "Port me"
3348#endif
3349 break;
3350 }
3351
3352 case NetworkAttachmentType_Internal:
3353 {
3354 hrc = aNetworkAdapter->COMGETTER(InternalNetwork)(bstr.asOutParam()); H();
3355 if (!bstr.isEmpty())
3356 {
3357 if (fSniffer)
3358 {
3359 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0);
3360 RC_CHECK();
3361 }
3362 else
3363 {
3364 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0);
3365 RC_CHECK();
3366 }
3367 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); RC_CHECK();
3368 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
3369 rc = CFGMR3InsertStringW(pCfg, "Network", bstr); RC_CHECK();
3370 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_WhateverNone); RC_CHECK();
3371 networkName = bstr;
3372 trunkType = Bstr(TRUNKTYPE_WHATEVER);
3373 }
3374 break;
3375 }
3376
3377 case NetworkAttachmentType_HostOnly:
3378 {
3379 if (fSniffer)
3380 {
3381 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0);
3382 RC_CHECK();
3383 }
3384 else
3385 {
3386 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0);
3387 RC_CHECK();
3388 }
3389
3390 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); RC_CHECK();
3391 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
3392
3393 Bstr HifName;
3394 hrc = aNetworkAdapter->COMGETTER(HostInterface)(HifName.asOutParam());
3395 if (FAILED(hrc))
3396 {
3397 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(HostInterface) failed, hrc (0x%x)\n", hrc));
3398 H();
3399 }
3400
3401 Utf8Str HifNameUtf8(HifName);
3402 const char *pszHifName = HifNameUtf8.raw();
3403 ComPtr<IHostNetworkInterface> hostInterface;
3404 rc = host->FindHostNetworkInterfaceByName(HifName, hostInterface.asOutParam());
3405 if (!SUCCEEDED(rc))
3406 {
3407 LogRel(("NetworkAttachmentType_HostOnly: FindByName failed, rc (0x%x)\n", rc));
3408 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
3409 N_("Inexistent host networking interface, name '%ls'"),
3410 HifName.raw());
3411 }
3412
3413 char szNetwork[INTNET_MAX_NETWORK_NAME];
3414 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHifName);
3415
3416#if defined(RT_OS_WINDOWS)
3417# ifndef VBOX_WITH_NETFLT
3418 hrc = E_NOTIMPL;
3419 LogRel(("NetworkAttachmentType_HostOnly: Not Implemented\n"));
3420 H();
3421# else /* defined VBOX_WITH_NETFLT*/
3422 /** @todo r=bird: Put this in a function. */
3423
3424 HostNetworkInterfaceType_T eIfType;
3425 hrc = hostInterface->COMGETTER(InterfaceType)(&eIfType);
3426 if (FAILED(hrc))
3427 {
3428 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(InterfaceType) failed, hrc (0x%x)\n", hrc));
3429 H();
3430 }
3431
3432 if (eIfType != HostNetworkInterfaceType_HostOnly)
3433 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
3434 N_("Interface ('%ls') is not a Host-Only Adapter interface"),
3435 HifName.raw());
3436
3437 hrc = hostInterface->COMGETTER(Id)(bstr.asOutParam());
3438 if (FAILED(hrc))
3439 {
3440 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(Id) failed, hrc (0x%x)\n", hrc));
3441 H();
3442 }
3443 Guid hostIFGuid(bstr);
3444
3445 INetCfg *pNc;
3446 ComPtr<INetCfgComponent> pAdaptorComponent;
3447 LPWSTR pszApp;
3448 rc = VERR_INTNET_FLT_IF_NOT_FOUND;
3449
3450 hrc = VBoxNetCfgWinQueryINetCfg(FALSE,
3451 L"VirtualBox",
3452 &pNc,
3453 &pszApp);
3454 Assert(hrc == S_OK);
3455 if (hrc == S_OK)
3456 {
3457 /* get the adapter's INetCfgComponent*/
3458 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.ptr(), pAdaptorComponent.asOutParam());
3459 if (hrc != S_OK)
3460 {
3461 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3462 LogRel(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc=%Rhrc (0x%x)\n", hrc, hrc));
3463 H();
3464 }
3465 }
3466#define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\"
3467 char szTrunkName[INTNET_MAX_TRUNK_NAME];
3468 char *pszTrunkName = szTrunkName;
3469 wchar_t * pswzBindName;
3470 hrc = pAdaptorComponent->GetBindName(&pswzBindName);
3471 Assert(hrc == S_OK);
3472 if (hrc == S_OK)
3473 {
3474 int cwBindName = (int)wcslen(pswzBindName) + 1;
3475 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX);
3476 if (sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName)
3477 {
3478 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX);
3479 pszTrunkName += cbFullBindNamePrefix-1;
3480 if (!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName,
3481 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL))
3482 {
3483 DWORD err = GetLastError();
3484 hrc = HRESULT_FROM_WIN32(err);
3485 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: WideCharToMultiByte failed, hr=%Rhrc (0x%x) err=%u\n", hrc, hrc, err));
3486 }
3487 }
3488 else
3489 {
3490 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: insufficient szTrunkName buffer space\n"));
3491 /** @todo set appropriate error code */
3492 hrc = E_FAIL;
3493 }
3494
3495 if (hrc != S_OK)
3496 {
3497 AssertFailed();
3498 CoTaskMemFree(pswzBindName);
3499 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3500 H();
3501 }
3502 }
3503 else
3504 {
3505 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3506 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc=%Rhrc (0x%x)\n", hrc, hrc));
3507 H();
3508 }
3509
3510
3511 CoTaskMemFree(pswzBindName);
3512
3513 pAdaptorComponent.setNull();
3514 /* release the pNc finally */
3515 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3516
3517 const char *pszTrunk = szTrunkName;
3518
3519 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp); RC_CHECK();
3520 rc = CFGMR3InsertString(pCfg, "Trunk", pszTrunk); RC_CHECK();
3521 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); RC_CHECK();
3522 networkName = Bstr(szNetwork);
3523 trunkName = Bstr(pszTrunk);
3524 trunkType = TRUNKTYPE_NETADP;
3525# endif /* defined VBOX_WITH_NETFLT*/
3526#elif defined(RT_OS_DARWIN)
3527 rc = CFGMR3InsertString(pCfg, "Trunk", pszHifName); RC_CHECK();
3528 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); RC_CHECK();
3529 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp); RC_CHECK();
3530 networkName = Bstr(szNetwork);
3531 trunkName = Bstr(pszHifName);
3532 trunkType = TRUNKTYPE_NETADP;
3533#else
3534 rc = CFGMR3InsertString(pCfg, "Trunk", pszHifName); RC_CHECK();
3535 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); RC_CHECK();
3536 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt); RC_CHECK();
3537 networkName = Bstr(szNetwork);
3538 trunkName = Bstr(pszHifName);
3539 trunkType = TRUNKTYPE_NETFLT;
3540#endif
3541#if !defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT)
3542
3543 Bstr tmpAddr, tmpMask;
3544
3545 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPAddress", pszHifName), tmpAddr.asOutParam());
3546 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty())
3547 {
3548 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPNetMask", pszHifName), tmpMask.asOutParam());
3549 if (SUCCEEDED(hrc) && !tmpMask.isEmpty())
3550 hrc = hostInterface->EnableStaticIpConfig(tmpAddr, tmpMask);
3551 else
3552 hrc = hostInterface->EnableStaticIpConfig(tmpAddr,
3553 Bstr(VBOXNET_IPV4MASK_DEFAULT));
3554 }
3555 else
3556 {
3557 /* Grab the IP number from the 'vboxnetX' instance number (see netif.h) */
3558 hrc = hostInterface->EnableStaticIpConfig(getDefaultIPv4Address(Bstr(pszHifName)),
3559 Bstr(VBOXNET_IPV4MASK_DEFAULT));
3560 }
3561
3562 ComAssertComRC(hrc); /** @todo r=bird: Why this isn't fatal? (H()) */
3563
3564 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPV6Address", pszHifName), tmpAddr.asOutParam());
3565 if (SUCCEEDED(hrc))
3566 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPV6NetMask", pszHifName), tmpMask.asOutParam());
3567 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty() && !tmpMask.isEmpty())
3568 {
3569 hrc = hostInterface->EnableStaticIpConfigV6(tmpAddr, Utf8Str(tmpMask).toUInt32());
3570 ComAssertComRC(hrc); /** @todo r=bird: Why this isn't fatal? (H()) */
3571 }
3572#endif
3573 break;
3574 }
3575
3576#if defined(VBOX_WITH_VDE)
3577 case NetworkAttachmentType_VDE:
3578 {
3579 hrc = aNetworkAdapter->COMGETTER(VDENetwork)(bstr.asOutParam()); H();
3580 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
3581 rc = CFGMR3InsertString(pLunL0, "Driver", "VDE"); RC_CHECK();
3582 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
3583 if (!bstr.isEmpty())
3584 {
3585 rc = CFGMR3InsertStringW(pCfg, "Network", bstr); RC_CHECK();
3586 networkName = bstr;
3587 }
3588 break;
3589 }
3590#endif
3591
3592 default:
3593 AssertMsgFailed(("should not get here!\n"));
3594 break;
3595 }
3596
3597 /*
3598 * Attempt to attach the driver.
3599 */
3600 switch (eAttachmentType)
3601 {
3602 case NetworkAttachmentType_Null:
3603 break;
3604
3605 case NetworkAttachmentType_Bridged:
3606 case NetworkAttachmentType_Internal:
3607 case NetworkAttachmentType_HostOnly:
3608 case NetworkAttachmentType_NAT:
3609#if defined(VBOX_WITH_VDE)
3610 case NetworkAttachmentType_VDE:
3611#endif
3612 {
3613 if (SUCCEEDED(hrc) && SUCCEEDED(rc))
3614 {
3615 if (fAttachDetach)
3616 {
3617 rc = PDMR3DriverAttach(pVM, pszDevice, uInstance, uLun, 0 /*fFlags*/, NULL /* ppBase */);
3618 //AssertRC(rc);
3619 }
3620
3621 {
3622 /** @todo pritesh: get the dhcp server name from the
3623 * previous network configuration and then stop the server
3624 * else it may conflict with the dhcp server running with
3625 * the current attachment type
3626 */
3627 /* Stop the hostonly DHCP Server */
3628 }
3629
3630 if (!networkName.isEmpty())
3631 {
3632 /*
3633 * Until we implement service reference counters DHCP Server will be stopped
3634 * by DHCPServerRunner destructor.
3635 */
3636 ComPtr<IDHCPServer> dhcpServer;
3637 hrc = virtualBox->FindDHCPServerByNetworkName(networkName, dhcpServer.asOutParam());
3638 if (SUCCEEDED(hrc))
3639 {
3640 /* there is a DHCP server available for this network */
3641 BOOL fEnabled;
3642 hrc = dhcpServer->COMGETTER(Enabled)(&fEnabled);
3643 if (FAILED(hrc))
3644 {
3645 LogRel(("DHCP svr: COMGETTER(Enabled) failed, hrc (%Rhrc)", hrc));
3646 H();
3647 }
3648
3649 if (fEnabled)
3650 hrc = dhcpServer->Start(networkName, trunkName, trunkType);
3651 }
3652 else
3653 hrc = S_OK;
3654 }
3655 }
3656
3657 break;
3658 }
3659
3660 default:
3661 AssertMsgFailed(("should not get here!\n"));
3662 break;
3663 }
3664
3665 meAttachmentType[uInstance] = eAttachmentType;
3666
3667#undef H
3668#undef RC_CHECK
3669
3670 return VINF_SUCCESS;
3671}
3672
3673#ifdef VBOX_WITH_GUEST_PROPS
3674/**
3675 * Set an array of guest properties
3676 */
3677static void configSetProperties(VMMDev * const pVMMDev, void *names,
3678 void *values, void *timestamps, void *flags)
3679{
3680 VBOXHGCMSVCPARM parms[4];
3681
3682 parms[0].type = VBOX_HGCM_SVC_PARM_PTR;
3683 parms[0].u.pointer.addr = names;
3684 parms[0].u.pointer.size = 0; /* We don't actually care. */
3685 parms[1].type = VBOX_HGCM_SVC_PARM_PTR;
3686 parms[1].u.pointer.addr = values;
3687 parms[1].u.pointer.size = 0; /* We don't actually care. */
3688 parms[2].type = VBOX_HGCM_SVC_PARM_PTR;
3689 parms[2].u.pointer.addr = timestamps;
3690 parms[2].u.pointer.size = 0; /* We don't actually care. */
3691 parms[3].type = VBOX_HGCM_SVC_PARM_PTR;
3692 parms[3].u.pointer.addr = flags;
3693 parms[3].u.pointer.size = 0; /* We don't actually care. */
3694
3695 pVMMDev->hgcmHostCall ("VBoxGuestPropSvc", guestProp::SET_PROPS_HOST, 4,
3696 &parms[0]);
3697}
3698
3699/**
3700 * Set a single guest property
3701 */
3702static void configSetProperty(VMMDev * const pVMMDev, const char *pszName,
3703 const char *pszValue, const char *pszFlags)
3704{
3705 VBOXHGCMSVCPARM parms[4];
3706
3707 AssertPtrReturnVoid(pszName);
3708 AssertPtrReturnVoid(pszValue);
3709 AssertPtrReturnVoid(pszFlags);
3710 parms[0].type = VBOX_HGCM_SVC_PARM_PTR;
3711 parms[0].u.pointer.addr = (void *)pszName;
3712 parms[0].u.pointer.size = strlen(pszName) + 1;
3713 parms[1].type = VBOX_HGCM_SVC_PARM_PTR;
3714 parms[1].u.pointer.addr = (void *)pszValue;
3715 parms[1].u.pointer.size = strlen(pszValue) + 1;
3716 parms[2].type = VBOX_HGCM_SVC_PARM_PTR;
3717 parms[2].u.pointer.addr = (void *)pszFlags;
3718 parms[2].u.pointer.size = strlen(pszFlags) + 1;
3719 pVMMDev->hgcmHostCall("VBoxGuestPropSvc", guestProp::SET_PROP_HOST, 3,
3720 &parms[0]);
3721}
3722
3723/**
3724 * Set the global flags value by calling the service
3725 * @returns the status returned by the call to the service
3726 *
3727 * @param pTable the service instance handle
3728 * @param eFlags the flags to set
3729 */
3730int configSetGlobalPropertyFlags(VMMDev * const pVMMDev,
3731 guestProp::ePropFlags eFlags)
3732{
3733 VBOXHGCMSVCPARM paParm;
3734 paParm.setUInt32(eFlags);
3735 int rc = pVMMDev->hgcmHostCall("VBoxGuestPropSvc",
3736 guestProp::SET_GLOBAL_FLAGS_HOST, 1,
3737 &paParm);
3738 if (RT_FAILURE(rc))
3739 {
3740 char szFlags[guestProp::MAX_FLAGS_LEN];
3741 if (RT_FAILURE(writeFlags(eFlags, szFlags)))
3742 Log(("Failed to set the global flags.\n"));
3743 else
3744 Log(("Failed to set the global flags \"%s\".\n", szFlags));
3745 }
3746 return rc;
3747}
3748#endif /* VBOX_WITH_GUEST_PROPS */
3749
3750/**
3751 * Set up the Guest Property service, populate it with properties read from
3752 * the machine XML and set a couple of initial properties.
3753 */
3754/* static */ int Console::configGuestProperties(void *pvConsole)
3755{
3756#ifdef VBOX_WITH_GUEST_PROPS
3757 AssertReturn(pvConsole, VERR_GENERAL_FAILURE);
3758 ComObjPtr<Console> pConsole = static_cast<Console *>(pvConsole);
3759
3760 /* Load the service */
3761 int rc = pConsole->mVMMDev->hgcmLoadService("VBoxGuestPropSvc", "VBoxGuestPropSvc");
3762
3763 if (RT_FAILURE(rc))
3764 {
3765 LogRel(("VBoxGuestPropSvc is not available. rc = %Rrc\n", rc));
3766 /* That is not a fatal failure. */
3767 rc = VINF_SUCCESS;
3768 }
3769 else
3770 {
3771 /*
3772 * Initialize built-in properties that can be changed and saved.
3773 *
3774 * These are typically transient properties that the guest cannot
3775 * change.
3776 */
3777
3778 /* Sysprep execution by VBoxService. */
3779 configSetProperty(pConsole->mVMMDev,
3780 "/VirtualBox/HostGuest/SysprepExec", "",
3781 "TRANSIENT, RDONLYGUEST");
3782 configSetProperty(pConsole->mVMMDev,
3783 "/VirtualBox/HostGuest/SysprepArgs", "",
3784 "TRANSIENT, RDONLYGUEST");
3785
3786 /*
3787 * Pull over the properties from the server.
3788 */
3789 SafeArray<BSTR> namesOut;
3790 SafeArray<BSTR> valuesOut;
3791 SafeArray<ULONG64> timestampsOut;
3792 SafeArray<BSTR> flagsOut;
3793 HRESULT hrc;
3794 hrc = pConsole->mControl->PullGuestProperties(ComSafeArrayAsOutParam(namesOut),
3795 ComSafeArrayAsOutParam(valuesOut),
3796 ComSafeArrayAsOutParam(timestampsOut),
3797 ComSafeArrayAsOutParam(flagsOut));
3798 AssertMsgReturn(SUCCEEDED(hrc), ("hrc=%Rrc\n", hrc), VERR_GENERAL_FAILURE);
3799 size_t cProps = namesOut.size();
3800 size_t cAlloc = cProps + 1;
3801 if ( valuesOut.size() != cProps
3802 || timestampsOut.size() != cProps
3803 || flagsOut.size() != cProps
3804 )
3805 AssertFailedReturn(VERR_INVALID_PARAMETER);
3806
3807 char **papszNames, **papszValues, **papszFlags;
3808 char szEmpty[] = "";
3809 ULONG64 *pau64Timestamps;
3810 papszNames = (char **)RTMemTmpAllocZ(sizeof(void *) * cAlloc);
3811 papszValues = (char **)RTMemTmpAllocZ(sizeof(void *) * cAlloc);
3812 pau64Timestamps = (ULONG64 *)RTMemTmpAllocZ(sizeof(ULONG64) * cAlloc);
3813 papszFlags = (char **)RTMemTmpAllocZ(sizeof(void *) * cAlloc);
3814 if (papszNames && papszValues && pau64Timestamps && papszFlags)
3815 {
3816 for (unsigned i = 0; RT_SUCCESS(rc) && i < cProps; ++i)
3817 {
3818 AssertPtrReturn(namesOut[i], VERR_INVALID_PARAMETER);
3819 rc = RTUtf16ToUtf8(namesOut[i], &papszNames[i]);
3820 if (RT_FAILURE(rc))
3821 break;
3822 if (valuesOut[i])
3823 rc = RTUtf16ToUtf8(valuesOut[i], &papszValues[i]);
3824 else
3825 papszValues[i] = szEmpty;
3826 if (RT_FAILURE(rc))
3827 break;
3828 pau64Timestamps[i] = timestampsOut[i];
3829 if (flagsOut[i])
3830 rc = RTUtf16ToUtf8(flagsOut[i], &papszFlags[i]);
3831 else
3832 papszFlags[i] = szEmpty;
3833 }
3834 if (RT_SUCCESS(rc))
3835 configSetProperties(pConsole->mVMMDev,
3836 (void *)papszNames,
3837 (void *)papszValues,
3838 (void *)pau64Timestamps,
3839 (void *)papszFlags);
3840 for (unsigned i = 0; i < cProps; ++i)
3841 {
3842 RTStrFree(papszNames[i]);
3843 if (valuesOut[i])
3844 RTStrFree(papszValues[i]);
3845 if (flagsOut[i])
3846 RTStrFree(papszFlags[i]);
3847 }
3848 }
3849 else
3850 rc = VERR_NO_MEMORY;
3851 RTMemTmpFree(papszNames);
3852 RTMemTmpFree(papszValues);
3853 RTMemTmpFree(pau64Timestamps);
3854 RTMemTmpFree(papszFlags);
3855 AssertRCReturn(rc, rc);
3856
3857 /*
3858 * These properties have to be set before pulling over the properties
3859 * from the machine XML, to ensure that properties saved in the XML
3860 * will override them.
3861 */
3862 /* Set the VBox version string as a guest property */
3863 configSetProperty(pConsole->mVMMDev, "/VirtualBox/HostInfo/VBoxVer",
3864 VBOX_VERSION_STRING, "TRANSIENT, RDONLYGUEST");
3865 /* Set the VBox SVN revision as a guest property */
3866 configSetProperty(pConsole->mVMMDev, "/VirtualBox/HostInfo/VBoxRev",
3867 RTBldCfgRevisionStr(), "TRANSIENT, RDONLYGUEST");
3868
3869 /*
3870 * Register the host notification callback
3871 */
3872 HGCMSVCEXTHANDLE hDummy;
3873 HGCMHostRegisterServiceExtension(&hDummy, "VBoxGuestPropSvc",
3874 Console::doGuestPropNotification,
3875 pvConsole);
3876
3877#ifdef VBOX_WITH_GUEST_PROPS_RDONLY_GUEST
3878 rc = configSetGlobalPropertyFlags(pConsole->mVMMDev,
3879 guestProp::RDONLYGUEST);
3880 AssertRCReturn(rc, rc);
3881#endif
3882
3883 Log(("Set VBoxGuestPropSvc property store\n"));
3884 }
3885 return VINF_SUCCESS;
3886#else /* !VBOX_WITH_GUEST_PROPS */
3887 return VERR_NOT_SUPPORTED;
3888#endif /* !VBOX_WITH_GUEST_PROPS */
3889}
3890
3891/**
3892 * Set up the Guest Control service.
3893 */
3894/* static */ int Console::configGuestControl(void *pvConsole)
3895{
3896#ifdef VBOX_WITH_GUEST_CONTROL
3897 AssertReturn(pvConsole, VERR_GENERAL_FAILURE);
3898 ComObjPtr<Console> pConsole = static_cast<Console *>(pvConsole);
3899
3900 /* Load the service */
3901 int rc = pConsole->mVMMDev->hgcmLoadService("VBoxGuestControlSvc", "VBoxGuestControlSvc");
3902
3903 if (RT_FAILURE(rc))
3904 {
3905 LogRel(("VBoxGuestControlSvc is not available. rc = %Rrc\n", rc));
3906 /* That is not a fatal failure. */
3907 rc = VINF_SUCCESS;
3908 }
3909 else
3910 {
3911 HGCMSVCEXTHANDLE hDummy;
3912 rc = HGCMHostRegisterServiceExtension(&hDummy, "VBoxGuestControlSvc",
3913 &Guest::doGuestCtrlNotification,
3914 pConsole->getGuest());
3915 if (RT_FAILURE(rc))
3916 Log(("Cannot register VBoxGuestControlSvc extension!\n"));
3917 else
3918 Log(("VBoxGuestControlSvc loaded\n"));
3919 }
3920
3921 return rc;
3922#else /* !VBOX_WITH_GUEST_CONTROL */
3923 return VERR_NOT_SUPPORTED;
3924#endif /* !VBOX_WITH_GUEST_CONTROL */
3925}
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