VirtualBox

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

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

Main: Add code for debugging I/O problems, disabled by default of course

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 171.0 KB
Line 
1/* $Id: ConsoleImpl2.cpp 29444 2010-05-13 10:11:37Z 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#if 0 /* Enable for I/O debugging */
2469 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK();
2470 rc = CFGMR3InsertString(pLunL0, "Driver", "DiskIntegrity"); RC_CHECK();
2471 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2472 rc = CFGMR3InsertInteger(pCfg, "CheckConsistency", 0); RC_CHECK();
2473 rc = CFGMR3InsertInteger(pCfg, "CheckDoubleCompletions", 1); RC_CHECK();
2474#endif
2475
2476 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); RC_CHECK();
2477 rc = CFGMR3InsertString(pLunL1, "Driver", "VD"); RC_CHECK();
2478 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); RC_CHECK();
2479
2480 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
2481 rc = CFGMR3InsertStringW(pCfg, "Path", bstr.raw()); RC_CHECK();
2482
2483 hrc = pMedium->COMGETTER(Format)(bstr.asOutParam()); H();
2484 rc = CFGMR3InsertStringW(pCfg, "Format", bstr.raw()); RC_CHECK();
2485
2486 /* DVDs are always readonly */
2487 if (enmType == DeviceType_DVD)
2488 {
2489 rc = CFGMR3InsertInteger(pCfg, "ReadOnly", 1); RC_CHECK();
2490 }
2491
2492 /* Start without exclusive write access to the images. */
2493 /** @todo Live Migration: I don't quite like this, we risk screwing up when
2494 * we're resuming the VM if some 3rd dude have any of the VDIs open
2495 * with write sharing denied. However, if the two VMs are sharing a
2496 * image it really is necessary....
2497 *
2498 * So, on the "lock-media" command, the target teleporter should also
2499 * make DrvVD undo TempReadOnly. It gets interesting if we fail after
2500 * that. Grumble. */
2501 else if (aMachineState == MachineState_TeleportingIn)
2502 {
2503 rc = CFGMR3InsertInteger(pCfg, "TempReadOnly", 1); RC_CHECK();
2504 }
2505
2506 if (enmIoBackend == IoBackendType_Unbuffered)
2507 {
2508 rc = CFGMR3InsertInteger(pCfg, "UseNewIo", 1); RC_CHECK();
2509 }
2510
2511 if (fSetupMerge)
2512 {
2513 rc = CFGMR3InsertInteger(pCfg, "SetupMerge", 1); RC_CHECK();
2514 if (uImage == uMergeSource)
2515 {
2516 rc = CFGMR3InsertInteger(pCfg, "MergeSource", 1); RC_CHECK();
2517 }
2518 else if (uImage == uMergeTarget)
2519 {
2520 rc = CFGMR3InsertInteger(pCfg, "MergeTarget", 1); RC_CHECK();
2521 }
2522 }
2523
2524 /* Pass all custom parameters. */
2525 bool fHostIP = true;
2526 SafeArray<BSTR> names;
2527 SafeArray<BSTR> values;
2528 hrc = pMedium->GetProperties(NULL,
2529 ComSafeArrayAsOutParam(names),
2530 ComSafeArrayAsOutParam(values)); H();
2531
2532 if (names.size() != 0)
2533 {
2534 PCFGMNODE pVDC;
2535 rc = CFGMR3InsertNode(pCfg, "VDConfig", &pVDC); RC_CHECK();
2536 for (size_t ii = 0; ii < names.size(); ++ii)
2537 {
2538 if (values[ii] && *values[ii])
2539 {
2540 Utf8Str name = names[ii];
2541 Utf8Str value = values[ii];
2542 rc = CFGMR3InsertString(pVDC, name.c_str(), value.c_str()); RC_CHECK();
2543 if ( name.compare("HostIPStack") == 0
2544 && value.compare("0") == 0)
2545 fHostIP = false;
2546 }
2547 }
2548 }
2549
2550 /* Create an inversed list of parents. */
2551 uImage--;
2552 IMedium *pParentMedium = pMedium;
2553 for (PCFGMNODE pParent = pCfg;; uImage--)
2554 {
2555 hrc = pParentMedium->COMGETTER(Parent)(&pMedium); H();
2556 if (!pMedium)
2557 break;
2558
2559 PCFGMNODE pCur;
2560 rc = CFGMR3InsertNode(pParent, "Parent", &pCur); RC_CHECK();
2561 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
2562 rc = CFGMR3InsertStringW(pCur, "Path", bstr.raw()); RC_CHECK();
2563
2564 hrc = pMedium->COMGETTER(Format)(bstr.asOutParam()); H();
2565 rc = CFGMR3InsertStringW(pCur, "Format", bstr.raw()); RC_CHECK();
2566
2567 if (fSetupMerge)
2568 {
2569 if (uImage == uMergeSource)
2570 {
2571 rc = CFGMR3InsertInteger(pCur, "MergeSource", 1); RC_CHECK();
2572 }
2573 else if (uImage == uMergeTarget)
2574 {
2575 rc = CFGMR3InsertInteger(pCur, "MergeTarget", 1); RC_CHECK();
2576 }
2577 }
2578
2579 /* Pass all custom parameters. */
2580 SafeArray<BSTR> aNames;
2581 SafeArray<BSTR> aValues;
2582 hrc = pMedium->GetProperties(NULL,
2583 ComSafeArrayAsOutParam(aNames),
2584 ComSafeArrayAsOutParam(aValues)); H();
2585
2586 if (aNames.size() != 0)
2587 {
2588 PCFGMNODE pVDC;
2589 rc = CFGMR3InsertNode(pCur, "VDConfig", &pVDC); RC_CHECK();
2590 for (size_t ii = 0; ii < aNames.size(); ++ii)
2591 {
2592 if (aValues[ii] && *aValues[ii])
2593 {
2594 Utf8Str name = aNames[ii];
2595 Utf8Str value = aValues[ii];
2596 rc = CFGMR3InsertString(pVDC, name.c_str(), value.c_str()); RC_CHECK();
2597 if ( name.compare("HostIPStack") == 0
2598 && value.compare("0") == 0)
2599 fHostIP = false;
2600 }
2601 }
2602 }
2603
2604 /* Custom code: put marker to not use host IP stack to driver
2605 * configuration node. Simplifies life of DrvVD a bit. */
2606 if (!fHostIP)
2607 {
2608 rc = CFGMR3InsertInteger(pCfg, "HostIPStack", 0); RC_CHECK();
2609 }
2610
2611 /* next */
2612 pParent = pCur;
2613 pParentMedium = pMedium;
2614 }
2615 }
2616 }
2617
2618#undef H
2619#undef RC_CHECK
2620
2621 return VINF_SUCCESS;
2622}
2623
2624/**
2625 * Construct the Network configuration tree
2626 *
2627 * @returns VBox status code.
2628 *
2629 * @param pszDevice The PDM device name.
2630 * @param uInstance The PDM device instance.
2631 * @param uLun The PDM LUN number of the drive.
2632 * @param aNetworkAdapter The network adapter whose attachment needs to be changed
2633 * @param pCfg Configuration node for the device
2634 * @param pLunL0 To store the pointer to the LUN#0.
2635 * @param pInst The instance CFGM node
2636 * @param fAttachDetach To determine if the network attachment should
2637 * be attached/detached after/before
2638 * configuration.
2639 *
2640 * @note Locks this object for writing.
2641 */
2642int Console::configNetwork(const char *pszDevice, unsigned uInstance,
2643 unsigned uLun, INetworkAdapter *aNetworkAdapter,
2644 PCFGMNODE pCfg, PCFGMNODE pLunL0, PCFGMNODE pInst,
2645 bool fAttachDetach)
2646{
2647 AutoCaller autoCaller(this);
2648 AssertComRCReturn(autoCaller.rc(), VERR_ACCESS_DENIED);
2649
2650 int rc = VINF_SUCCESS;
2651 HRESULT hrc;
2652 Bstr bstr;
2653
2654#define RC_CHECK() AssertMsgReturn(RT_SUCCESS(rc), ("rc=%Rrc\n", rc), rc)
2655#define H() AssertMsgReturn(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), VERR_GENERAL_FAILURE)
2656
2657 /*
2658 * Locking the object before doing VMR3* calls is quite safe here, since
2659 * we're on EMT. Write lock is necessary because we indirectly modify the
2660 * meAttachmentType member.
2661 */
2662 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2663
2664 PVM pVM = mpVM;
2665
2666 ComPtr<IMachine> pMachine = machine();
2667
2668 ComPtr<IVirtualBox> virtualBox;
2669 hrc = pMachine->COMGETTER(Parent)(virtualBox.asOutParam());
2670 H();
2671
2672 ComPtr<IHost> host;
2673 hrc = virtualBox->COMGETTER(Host)(host.asOutParam());
2674 H();
2675
2676 BOOL fSniffer;
2677 hrc = aNetworkAdapter->COMGETTER(TraceEnabled)(&fSniffer);
2678 H();
2679
2680 if (fAttachDetach && fSniffer)
2681 {
2682 const char *pszNetDriver = "IntNet";
2683 if (meAttachmentType[uInstance] == NetworkAttachmentType_NAT)
2684 pszNetDriver = "NAT";
2685#if !defined(VBOX_WITH_NETFLT) && defined(RT_OS_LINUX)
2686 if (meAttachmentType[uInstance] == NetworkAttachmentType_Bridged)
2687 pszNetDriver = "HostInterface";
2688#endif
2689
2690 rc = PDMR3DriverDetach(pVM, pszDevice, uInstance, uLun, pszNetDriver, 0, 0 /*fFlags*/);
2691 if (rc == VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN)
2692 rc = VINF_SUCCESS;
2693 AssertLogRelRCReturn(rc, rc);
2694
2695 pLunL0 = CFGMR3GetChildF(pInst, "LUN#%u", uLun);
2696 PCFGMNODE pLunAD = CFGMR3GetChildF(pLunL0, "AttachedDriver");
2697 if (pLunAD)
2698 {
2699 CFGMR3RemoveNode(pLunAD);
2700 }
2701 else
2702 {
2703 CFGMR3RemoveNode(pLunL0);
2704 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
2705 rc = CFGMR3InsertString(pLunL0, "Driver", "NetSniffer"); RC_CHECK();
2706 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2707 hrc = aNetworkAdapter->COMGETTER(TraceFile)(bstr.asOutParam()); H();
2708 if (!bstr.isEmpty()) /* check convention for indicating default file. */
2709 {
2710 rc = CFGMR3InsertStringW(pCfg, "File", bstr.raw()); RC_CHECK();
2711 }
2712 }
2713 }
2714 else if (fAttachDetach && !fSniffer)
2715 {
2716 rc = PDMR3DeviceDetach(pVM, pszDevice, uInstance, uLun, 0 /*fFlags*/);
2717 if (rc == VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN)
2718 rc = VINF_SUCCESS;
2719 AssertLogRelRCReturn(rc, rc);
2720
2721 /* nuke anything which might have been left behind. */
2722 CFGMR3RemoveNode(CFGMR3GetChildF(pInst, "LUN#%u", uLun));
2723 }
2724 else if (!fAttachDetach && fSniffer)
2725 {
2726 /* insert the sniffer filter driver. */
2727 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
2728 rc = CFGMR3InsertString(pLunL0, "Driver", "NetSniffer"); RC_CHECK();
2729 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2730 hrc = aNetworkAdapter->COMGETTER(TraceFile)(bstr.asOutParam()); H();
2731 if (!bstr.isEmpty()) /* check convention for indicating default file. */
2732 {
2733 rc = CFGMR3InsertStringW(pCfg, "File", bstr.raw()); RC_CHECK();
2734 }
2735 }
2736
2737 Bstr networkName, trunkName, trunkType;
2738 NetworkAttachmentType_T eAttachmentType;
2739 hrc = aNetworkAdapter->COMGETTER(AttachmentType)(&eAttachmentType); H();
2740 switch (eAttachmentType)
2741 {
2742 case NetworkAttachmentType_Null:
2743 break;
2744
2745 case NetworkAttachmentType_NAT:
2746 {
2747 ComPtr<INATEngine> natDriver;
2748 hrc = aNetworkAdapter->COMGETTER(NatDriver)(natDriver.asOutParam()); H();
2749 if (fSniffer)
2750 {
2751 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK();
2752 }
2753 else
2754 {
2755 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
2756 }
2757 rc = CFGMR3InsertString(pLunL0, "Driver", "NAT"); RC_CHECK();
2758 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2759
2760 /* Configure TFTP prefix and boot filename. */
2761 hrc = virtualBox->COMGETTER(HomeFolder)(bstr.asOutParam()); H();
2762 if (!bstr.isEmpty())
2763 {
2764 rc = CFGMR3InsertStringF(pCfg, "TFTPPrefix", "%ls%c%s", bstr.raw(), RTPATH_DELIMITER, "TFTP"); RC_CHECK();
2765 }
2766 hrc = pMachine->COMGETTER(Name)(bstr.asOutParam()); H();
2767 rc = CFGMR3InsertStringF(pCfg, "BootFile", "%ls.pxe", bstr.raw()); RC_CHECK();
2768
2769 hrc = natDriver->COMGETTER(Network)(bstr.asOutParam()); H();
2770 if (!bstr.isEmpty())
2771 {
2772 rc = CFGMR3InsertStringW(pCfg, "Network", bstr.raw()); RC_CHECK();
2773 }
2774 else
2775 {
2776 ULONG uSlot;
2777 hrc = aNetworkAdapter->COMGETTER(Slot)(&uSlot); H();
2778 rc = CFGMR3InsertStringF(pCfg, "Network", "10.0.%d.0/24", uSlot+2); RC_CHECK();
2779 }
2780 hrc = natDriver->COMGETTER(HostIP)(bstr.asOutParam()); H();
2781 if (!bstr.isEmpty())
2782 {
2783 rc = CFGMR3InsertStringW(pCfg, "BindIP", bstr.raw()); RC_CHECK();
2784 }
2785 ULONG mtu = 0;
2786 ULONG sockSnd = 0;
2787 ULONG sockRcv = 0;
2788 ULONG tcpSnd = 0;
2789 ULONG tcpRcv = 0;
2790 hrc = natDriver->GetNetworkSettings(&mtu, &sockSnd, &sockRcv, &tcpSnd, &tcpRcv); H();
2791 if (mtu)
2792 {
2793 rc = CFGMR3InsertInteger(pCfg, "SlirpMTU", mtu); RC_CHECK();
2794 }
2795 if (sockRcv)
2796 {
2797 rc = CFGMR3InsertInteger(pCfg, "SockRcv", sockRcv); RC_CHECK();
2798 }
2799 if (sockSnd)
2800 {
2801 rc = CFGMR3InsertInteger(pCfg, "SockSnd", sockSnd); RC_CHECK();
2802 }
2803 if (tcpRcv)
2804 {
2805 rc = CFGMR3InsertInteger(pCfg, "TcpRcv", tcpRcv); RC_CHECK();
2806 }
2807 if (tcpSnd)
2808 {
2809 rc = CFGMR3InsertInteger(pCfg, "TcpSnd", tcpSnd); RC_CHECK();
2810 }
2811 hrc = natDriver->COMGETTER(TftpPrefix)(bstr.asOutParam()); H();
2812 if (!bstr.isEmpty())
2813 {
2814 rc = CFGMR3RemoveValue(pCfg, "TFTPPrefix"); RC_CHECK();
2815 rc = CFGMR3InsertStringW(pCfg, "TFTPPrefix", bstr); RC_CHECK();
2816 }
2817 hrc = natDriver->COMGETTER(TftpBootFile)(bstr.asOutParam()); H();
2818 if (!bstr.isEmpty())
2819 {
2820 rc = CFGMR3RemoveValue(pCfg, "BootFile"); RC_CHECK();
2821 rc = CFGMR3InsertStringW(pCfg, "BootFile", bstr); RC_CHECK();
2822 }
2823 hrc = natDriver->COMGETTER(TftpNextServer)(bstr.asOutParam()); H();
2824 if (!bstr.isEmpty())
2825 {
2826 rc = CFGMR3InsertStringW(pCfg, "NextServer", bstr); RC_CHECK();
2827 }
2828 BOOL fDnsFlag;
2829 hrc = natDriver->COMGETTER(DnsPassDomain)(&fDnsFlag); H();
2830 rc = CFGMR3InsertInteger(pCfg, "PassDomain", fDnsFlag); RC_CHECK();
2831 hrc = natDriver->COMGETTER(DnsProxy)(&fDnsFlag); H();
2832 rc = CFGMR3InsertInteger(pCfg, "DNSProxy", fDnsFlag); RC_CHECK();
2833 hrc = natDriver->COMGETTER(DnsUseHostResolver)(&fDnsFlag); H();
2834 rc = CFGMR3InsertInteger(pCfg, "UseHostResolver", fDnsFlag); RC_CHECK();
2835
2836 ULONG aliasMode;
2837 hrc = natDriver->COMGETTER(AliasMode)(&aliasMode); H();
2838 rc = CFGMR3InsertInteger(pCfg, "AliasMode", aliasMode); RC_CHECK();
2839
2840 /* port-forwarding */
2841 SafeArray<BSTR> pfs;
2842 hrc = natDriver->COMGETTER(Redirects)(ComSafeArrayAsOutParam(pfs)); H();
2843 PCFGMNODE pPF = NULL; /* /Devices/Dev/.../Config/PF#0/ */
2844 for (unsigned int i = 0; i < pfs.size(); ++i)
2845 {
2846 uint16_t port = 0;
2847 BSTR r = pfs[i];
2848 Utf8Str utf = Utf8Str(r);
2849 Utf8Str strName;
2850 Utf8Str strProto;
2851 Utf8Str strHostPort;
2852 Utf8Str strHostIP;
2853 Utf8Str strGuestPort;
2854 Utf8Str strGuestIP;
2855 size_t pos, ppos;
2856 pos = ppos = 0;
2857#define ITERATE_TO_NEXT_TERM(res, str, pos, ppos) \
2858 do { \
2859 pos = str.find(",", ppos); \
2860 if (pos == Utf8Str::npos) \
2861 { \
2862 Log(( #res " extracting from %s is failed\n", str.raw())); \
2863 continue; \
2864 } \
2865 res = str.substr(ppos, pos - ppos); \
2866 Log2((#res " %s pos:%d, ppos:%d\n", res.raw(), pos, ppos)); \
2867 ppos = pos + 1; \
2868 } while (0)
2869 ITERATE_TO_NEXT_TERM(strName, utf, pos, ppos);
2870 ITERATE_TO_NEXT_TERM(strProto, utf, pos, ppos);
2871 ITERATE_TO_NEXT_TERM(strHostIP, utf, pos, ppos);
2872 ITERATE_TO_NEXT_TERM(strHostPort, utf, pos, ppos);
2873 ITERATE_TO_NEXT_TERM(strGuestIP, utf, pos, ppos);
2874 strGuestPort = utf.substr(ppos, utf.length() - ppos);
2875#undef ITERATE_TO_NEXT_TERM
2876
2877 uint32_t proto = strProto.toUInt32();
2878 bool fValid = true;
2879 switch (proto)
2880 {
2881 case NATProtocol_UDP:
2882 strProto = "UDP";
2883 break;
2884 case NATProtocol_TCP:
2885 strProto = "TCP";
2886 break;
2887 default:
2888 fValid = false;
2889 }
2890 /* continue with next rule if no valid proto was passed */
2891 if (!fValid)
2892 continue;
2893
2894 rc = CFGMR3InsertNode(pCfg, strName.raw(), &pPF); RC_CHECK();
2895 rc = CFGMR3InsertString(pPF, "Protocol", strProto.raw()); RC_CHECK();
2896
2897 if (!strHostIP.isEmpty())
2898 {
2899 rc = CFGMR3InsertString(pPF, "BindIP", strHostIP.raw()); RC_CHECK();
2900 }
2901
2902 if (!strGuestIP.isEmpty())
2903 {
2904 rc = CFGMR3InsertString(pPF, "GuestIP", strGuestIP.raw()); RC_CHECK();
2905 }
2906
2907 port = RTStrToUInt16(strHostPort.raw());
2908 if (port)
2909 {
2910 rc = CFGMR3InsertInteger(pPF, "HostPort", port); RC_CHECK();
2911 }
2912
2913 port = RTStrToUInt16(strGuestPort.raw());
2914 if (port)
2915 {
2916 rc = CFGMR3InsertInteger(pPF, "GuestPort", port); RC_CHECK();
2917 }
2918 }
2919 break;
2920 }
2921
2922 case NetworkAttachmentType_Bridged:
2923 {
2924#if (defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)) && !defined(VBOX_WITH_NETFLT)
2925 hrc = attachToTapInterface(aNetworkAdapter);
2926 if (FAILED(hrc))
2927 {
2928 switch (hrc)
2929 {
2930 case VERR_ACCESS_DENIED:
2931 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
2932 "Failed to open '/dev/net/tun' for read/write access. Please check the "
2933 "permissions of that node. Either run 'chmod 0666 /dev/net/tun' or "
2934 "change the group of that node and make yourself a member of that group. Make "
2935 "sure that these changes are permanent, especially if you are "
2936 "using udev"));
2937 default:
2938 AssertMsgFailed(("Could not attach to host interface! Bad!\n"));
2939 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
2940 "Failed to initialize Host Interface Networking"));
2941 }
2942 }
2943
2944 Assert((int)maTapFD[uInstance] >= 0);
2945 if ((int)maTapFD[uInstance] >= 0)
2946 {
2947 if (fSniffer)
2948 {
2949 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK();
2950 }
2951 else
2952 {
2953 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
2954 }
2955 rc = CFGMR3InsertString(pLunL0, "Driver", "HostInterface"); RC_CHECK();
2956 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2957 rc = CFGMR3InsertInteger(pCfg, "FileHandle", maTapFD[uInstance]); RC_CHECK();
2958 }
2959
2960#elif defined(VBOX_WITH_NETFLT)
2961 /*
2962 * This is the new VBoxNetFlt+IntNet stuff.
2963 */
2964 if (fSniffer)
2965 {
2966 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK();
2967 }
2968 else
2969 {
2970 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
2971 }
2972
2973 Bstr HifName;
2974 hrc = aNetworkAdapter->COMGETTER(HostInterface)(HifName.asOutParam());
2975 if (FAILED(hrc))
2976 {
2977 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(HostInterface) failed, hrc (0x%x)", hrc));
2978 H();
2979 }
2980
2981 Utf8Str HifNameUtf8(HifName);
2982 const char *pszHifName = HifNameUtf8.raw();
2983
2984# if defined(RT_OS_DARWIN)
2985 /* The name is on the form 'ifX: long name', chop it off at the colon. */
2986 char szTrunk[8];
2987 strncpy(szTrunk, pszHifName, sizeof(szTrunk));
2988 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk));
2989 if (!pszColon)
2990 {
2991 /*
2992 * Dynamic changing of attachment causes an attempt to configure
2993 * network with invalid host adapter (as it is must be changed before
2994 * the attachment), calling Detach here will cause a deadlock.
2995 * See #4750.
2996 * hrc = aNetworkAdapter->Detach(); H();
2997 */
2998 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
2999 N_("Malformed host interface networking name '%ls'"),
3000 HifName.raw());
3001 }
3002 *pszColon = '\0';
3003 const char *pszTrunk = szTrunk;
3004
3005# elif defined(RT_OS_SOLARIS)
3006 /* The name is on the form format 'ifX[:1] - long name, chop it off at space. */
3007 char szTrunk[256];
3008 strlcpy(szTrunk, pszHifName, sizeof(szTrunk));
3009 char *pszSpace = (char *)memchr(szTrunk, ' ', sizeof(szTrunk));
3010
3011 /*
3012 * Currently don't bother about malformed names here for the sake of people using
3013 * VBoxManage and setting only the NIC name from there. If there is a space we
3014 * chop it off and proceed, otherwise just use whatever we've got.
3015 */
3016 if (pszSpace)
3017 *pszSpace = '\0';
3018
3019 /* Chop it off at the colon (zone naming eg: e1000g:1 we need only the e1000g) */
3020 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk));
3021 if (pszColon)
3022 *pszColon = '\0';
3023
3024 const char *pszTrunk = szTrunk;
3025
3026# elif defined(RT_OS_WINDOWS)
3027 ComPtr<IHostNetworkInterface> hostInterface;
3028 hrc = host->FindHostNetworkInterfaceByName(HifName, hostInterface.asOutParam());
3029 if (!SUCCEEDED(hrc))
3030 {
3031 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: FindByName failed, rc=%Rhrc (0x%x)", hrc, hrc));
3032 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
3033 N_("Inexistent host networking interface, name '%ls'"),
3034 HifName.raw());
3035 }
3036
3037 HostNetworkInterfaceType_T eIfType;
3038 hrc = hostInterface->COMGETTER(InterfaceType)(&eIfType);
3039 if (FAILED(hrc))
3040 {
3041 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(InterfaceType) failed, hrc (0x%x)", hrc));
3042 H();
3043 }
3044
3045 if (eIfType != HostNetworkInterfaceType_Bridged)
3046 {
3047 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
3048 N_("Interface ('%ls') is not a Bridged Adapter interface"),
3049 HifName.raw());
3050 }
3051
3052 hrc = hostInterface->COMGETTER(Id)(bstr.asOutParam());
3053 if (FAILED(hrc))
3054 {
3055 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(Id) failed, hrc (0x%x)", hrc));
3056 H();
3057 }
3058 Guid hostIFGuid(bstr);
3059
3060 INetCfg *pNc;
3061 ComPtr<INetCfgComponent> pAdaptorComponent;
3062 LPWSTR pszApp;
3063 int rc = VERR_INTNET_FLT_IF_NOT_FOUND;
3064
3065 hrc = VBoxNetCfgWinQueryINetCfg(FALSE /*fGetWriteLock*/,
3066 L"VirtualBox",
3067 &pNc,
3068 &pszApp);
3069 Assert(hrc == S_OK);
3070 if (hrc == S_OK)
3071 {
3072 /* get the adapter's INetCfgComponent*/
3073 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.ptr(), pAdaptorComponent.asOutParam());
3074 if (hrc != S_OK)
3075 {
3076 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3077 LogRel(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc));
3078 H();
3079 }
3080 }
3081#define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\"
3082 char szTrunkName[INTNET_MAX_TRUNK_NAME];
3083 char *pszTrunkName = szTrunkName;
3084 wchar_t * pswzBindName;
3085 hrc = pAdaptorComponent->GetBindName(&pswzBindName);
3086 Assert(hrc == S_OK);
3087 if (hrc == S_OK)
3088 {
3089 int cwBindName = (int)wcslen(pswzBindName) + 1;
3090 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX);
3091 if (sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName)
3092 {
3093 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX);
3094 pszTrunkName += cbFullBindNamePrefix-1;
3095 if (!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName,
3096 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL))
3097 {
3098 DWORD err = GetLastError();
3099 hrc = HRESULT_FROM_WIN32(err);
3100 AssertMsgFailed(("%hrc=%Rhrc %#x\n", hrc, hrc));
3101 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: WideCharToMultiByte failed, hr=%Rhrc (0x%x) err=%u\n", hrc, hrc, err));
3102 }
3103 }
3104 else
3105 {
3106 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: insufficient szTrunkName buffer space\n"));
3107 /** @todo set appropriate error code */
3108 hrc = E_FAIL;
3109 }
3110
3111 if (hrc != S_OK)
3112 {
3113 AssertFailed();
3114 CoTaskMemFree(pswzBindName);
3115 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3116 H();
3117 }
3118
3119 /* we're not freeing the bind name since we'll use it later for detecting wireless*/
3120 }
3121 else
3122 {
3123 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3124 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc));
3125 H();
3126 }
3127 const char *pszTrunk = szTrunkName;
3128 /* we're not releasing the INetCfg stuff here since we use it later to figure out whether it is wireless */
3129
3130# elif defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)
3131# if defined(RT_OS_FREEBSD)
3132 /*
3133 * If we bridge to a tap interface open it the `old' direct way.
3134 * This works and performs better than bridging a physical
3135 * interface via the current FreeBSD vboxnetflt implementation.
3136 */
3137 if (!strncmp(pszHifName, "tap", sizeof "tap" - 1)) {
3138 hrc = attachToTapInterface(aNetworkAdapter);
3139 if (FAILED(hrc))
3140 {
3141 switch (hrc)
3142 {
3143 case VERR_ACCESS_DENIED:
3144 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
3145 "Failed to open '/dev/%s' for read/write access. Please check the "
3146 "permissions of that node, and that the net.link.tap.user_open "
3147 "sysctl is set. Either run 'chmod 0666 /dev/%s' or "
3148 "change the group of that node to vboxusers and make yourself "
3149 "a member of that group. Make sure that these changes are permanent."), pszHifName, pszHifName);
3150 default:
3151 AssertMsgFailed(("Could not attach to tap interface! Bad!\n"));
3152 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
3153 "Failed to initialize Host Interface Networking"));
3154 }
3155 }
3156
3157 Assert((int)maTapFD[uInstance] >= 0);
3158 if ((int)maTapFD[uInstance] >= 0)
3159 {
3160 rc = CFGMR3InsertString(pLunL0, "Driver", "HostInterface"); RC_CHECK();
3161 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
3162 rc = CFGMR3InsertInteger(pCfg, "FileHandle", maTapFD[uInstance]); RC_CHECK();
3163 }
3164 break;
3165 }
3166# endif
3167 /** @todo Check for malformed names. */
3168 const char *pszTrunk = pszHifName;
3169
3170 /* Issue a warning if the interface is down */
3171 {
3172 int iSock = socket(AF_INET, SOCK_DGRAM, 0);
3173 if (iSock >= 0)
3174 {
3175 struct ifreq Req;
3176
3177 memset(&Req, 0, sizeof(Req));
3178 strncpy(Req.ifr_name, pszHifName, sizeof(Req.ifr_name) - 1);
3179 if (ioctl(iSock, SIOCGIFFLAGS, &Req) >= 0)
3180 if ((Req.ifr_flags & IFF_UP) == 0)
3181 {
3182 setVMRuntimeErrorCallbackF(pVM, this, 0, "BridgedInterfaceDown", "Bridged interface %s is down. Guest will not be able to use this interface", pszHifName);
3183 }
3184
3185 close(iSock);
3186 }
3187 }
3188
3189# else
3190# error "PORTME (VBOX_WITH_NETFLT)"
3191# endif
3192
3193 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); RC_CHECK();
3194 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
3195 rc = CFGMR3InsertString(pCfg, "Trunk", pszTrunk); RC_CHECK();
3196 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt);
3197 RC_CHECK();
3198 char szNetwork[INTNET_MAX_NETWORK_NAME];
3199 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHifName);
3200 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); RC_CHECK();
3201 networkName = Bstr(szNetwork);
3202 trunkName = Bstr(pszTrunk);
3203 trunkType = Bstr(TRUNKTYPE_NETFLT);
3204
3205# if defined(RT_OS_DARWIN)
3206 /** @todo Come up with a better deal here. Problem is that IHostNetworkInterface is completely useless here. */
3207 if ( strstr(pszHifName, "Wireless")
3208 || strstr(pszHifName, "AirPort" ))
3209 {
3210 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); RC_CHECK();
3211 }
3212# elif defined(RT_OS_LINUX)
3213 int iSock = socket(AF_INET, SOCK_DGRAM, 0);
3214 if (iSock >= 0)
3215 {
3216 struct iwreq WRq;
3217
3218 memset(&WRq, 0, sizeof(WRq));
3219 strncpy(WRq.ifr_name, pszHifName, IFNAMSIZ);
3220 bool fSharedMacOnWire = ioctl(iSock, SIOCGIWNAME, &WRq) >= 0;
3221 close(iSock);
3222 if (fSharedMacOnWire)
3223 {
3224 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true);
3225 RC_CHECK();
3226 Log(("Set SharedMacOnWire\n"));
3227 }
3228 else
3229 Log(("Failed to get wireless name\n"));
3230 }
3231 else
3232 Log(("Failed to open wireless socket\n"));
3233# elif defined(RT_OS_FREEBSD)
3234 int iSock = socket(AF_INET, SOCK_DGRAM, 0);
3235 if (iSock >= 0)
3236 {
3237 struct ieee80211req WReq;
3238 uint8_t abData[32];
3239
3240 memset(&WReq, 0, sizeof(WReq));
3241 strncpy(WReq.i_name, pszHifName, sizeof(WReq.i_name));
3242 WReq.i_type = IEEE80211_IOC_SSID;
3243 WReq.i_val = -1;
3244 WReq.i_data = abData;
3245 WReq.i_len = sizeof(abData);
3246
3247 bool fSharedMacOnWire = ioctl(iSock, SIOCG80211, &WReq) >= 0;
3248 close(iSock);
3249 if (fSharedMacOnWire)
3250 {
3251 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true);
3252 RC_CHECK();
3253 Log(("Set SharedMacOnWire\n"));
3254 }
3255 else
3256 Log(("Failed to get wireless name\n"));
3257 }
3258 else
3259 Log(("Failed to open wireless socket\n"));
3260# elif defined(RT_OS_WINDOWS)
3261# define DEVNAME_PREFIX L"\\\\.\\"
3262 /* we are getting the medium type via IOCTL_NDIS_QUERY_GLOBAL_STATS Io Control
3263 * there is a pretty long way till there though since we need to obtain the symbolic link name
3264 * for the adapter device we are going to query given the device Guid */
3265
3266
3267 /* prepend the "\\\\.\\" to the bind name to obtain the link name */
3268
3269 wchar_t FileName[MAX_PATH];
3270 wcscpy(FileName, DEVNAME_PREFIX);
3271 wcscpy((wchar_t*)(((char*)FileName) + sizeof(DEVNAME_PREFIX) - sizeof(FileName[0])), pswzBindName);
3272
3273 /* open the device */
3274 HANDLE hDevice = CreateFile(FileName,
3275 GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
3276 NULL,
3277 OPEN_EXISTING,
3278 FILE_ATTRIBUTE_NORMAL,
3279 NULL);
3280
3281 if (hDevice != INVALID_HANDLE_VALUE)
3282 {
3283 bool fSharedMacOnWire = false;
3284
3285 /* now issue the OID_GEN_PHYSICAL_MEDIUM query */
3286 DWORD Oid = OID_GEN_PHYSICAL_MEDIUM;
3287 NDIS_PHYSICAL_MEDIUM PhMedium;
3288 DWORD cbResult;
3289 if (DeviceIoControl(hDevice,
3290 IOCTL_NDIS_QUERY_GLOBAL_STATS,
3291 &Oid,
3292 sizeof(Oid),
3293 &PhMedium,
3294 sizeof(PhMedium),
3295 &cbResult,
3296 NULL))
3297 {
3298 /* that was simple, now examine PhMedium */
3299 if ( PhMedium == NdisPhysicalMediumWirelessWan
3300 || PhMedium == NdisPhysicalMediumWirelessLan
3301 || PhMedium == NdisPhysicalMediumNative802_11
3302 || PhMedium == NdisPhysicalMediumBluetooth)
3303 fSharedMacOnWire = true;
3304 }
3305 else
3306 {
3307 int winEr = GetLastError();
3308 LogRel(("Console::configConstructor: DeviceIoControl failed, err (0x%x), ignoring\n", winEr));
3309 Assert(winEr == ERROR_INVALID_PARAMETER || winEr == ERROR_NOT_SUPPORTED || winEr == ERROR_BAD_COMMAND);
3310 }
3311 CloseHandle(hDevice);
3312
3313 if (fSharedMacOnWire)
3314 {
3315 Log(("this is a wireless adapter"));
3316 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); RC_CHECK();
3317 Log(("Set SharedMacOnWire\n"));
3318 }
3319 else
3320 Log(("this is NOT a wireless adapter"));
3321 }
3322 else
3323 {
3324 int winEr = GetLastError();
3325 AssertLogRelMsgFailed(("Console::configConstructor: CreateFile failed, err (0x%x), ignoring\n", winEr));
3326 }
3327
3328 CoTaskMemFree(pswzBindName);
3329
3330 pAdaptorComponent.setNull();
3331 /* release the pNc finally */
3332 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3333# else
3334 /** @todo PORTME: wireless detection */
3335# endif
3336
3337# if defined(RT_OS_SOLARIS)
3338# if 0 /* bird: this is a bit questionable and might cause more trouble than its worth. */
3339 /* Zone access restriction, don't allow snopping the global zone. */
3340 zoneid_t ZoneId = getzoneid();
3341 if (ZoneId != GLOBAL_ZONEID)
3342 {
3343 rc = CFGMR3InsertInteger(pCfg, "IgnoreAllPromisc", true); RC_CHECK();
3344 }
3345# endif
3346# endif
3347
3348#elif defined(RT_OS_WINDOWS) /* not defined NetFlt */
3349 /* NOTHING TO DO HERE */
3350#elif defined(RT_OS_LINUX)
3351/// @todo aleksey: is there anything to be done here?
3352#elif defined(RT_OS_FREEBSD)
3353/** @todo FreeBSD: Check out this later (HIF networking). */
3354#else
3355# error "Port me"
3356#endif
3357 break;
3358 }
3359
3360 case NetworkAttachmentType_Internal:
3361 {
3362 hrc = aNetworkAdapter->COMGETTER(InternalNetwork)(bstr.asOutParam()); H();
3363 if (!bstr.isEmpty())
3364 {
3365 if (fSniffer)
3366 {
3367 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0);
3368 RC_CHECK();
3369 }
3370 else
3371 {
3372 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0);
3373 RC_CHECK();
3374 }
3375 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); RC_CHECK();
3376 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
3377 rc = CFGMR3InsertStringW(pCfg, "Network", bstr); RC_CHECK();
3378 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_WhateverNone); RC_CHECK();
3379 networkName = bstr;
3380 trunkType = Bstr(TRUNKTYPE_WHATEVER);
3381 }
3382 break;
3383 }
3384
3385 case NetworkAttachmentType_HostOnly:
3386 {
3387 if (fSniffer)
3388 {
3389 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0);
3390 RC_CHECK();
3391 }
3392 else
3393 {
3394 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0);
3395 RC_CHECK();
3396 }
3397
3398 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); RC_CHECK();
3399 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
3400
3401 Bstr HifName;
3402 hrc = aNetworkAdapter->COMGETTER(HostInterface)(HifName.asOutParam());
3403 if (FAILED(hrc))
3404 {
3405 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(HostInterface) failed, hrc (0x%x)\n", hrc));
3406 H();
3407 }
3408
3409 Utf8Str HifNameUtf8(HifName);
3410 const char *pszHifName = HifNameUtf8.raw();
3411 ComPtr<IHostNetworkInterface> hostInterface;
3412 rc = host->FindHostNetworkInterfaceByName(HifName, hostInterface.asOutParam());
3413 if (!SUCCEEDED(rc))
3414 {
3415 LogRel(("NetworkAttachmentType_HostOnly: FindByName failed, rc (0x%x)\n", rc));
3416 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
3417 N_("Inexistent host networking interface, name '%ls'"),
3418 HifName.raw());
3419 }
3420
3421 char szNetwork[INTNET_MAX_NETWORK_NAME];
3422 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHifName);
3423
3424#if defined(RT_OS_WINDOWS)
3425# ifndef VBOX_WITH_NETFLT
3426 hrc = E_NOTIMPL;
3427 LogRel(("NetworkAttachmentType_HostOnly: Not Implemented\n"));
3428 H();
3429# else /* defined VBOX_WITH_NETFLT*/
3430 /** @todo r=bird: Put this in a function. */
3431
3432 HostNetworkInterfaceType_T eIfType;
3433 hrc = hostInterface->COMGETTER(InterfaceType)(&eIfType);
3434 if (FAILED(hrc))
3435 {
3436 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(InterfaceType) failed, hrc (0x%x)\n", hrc));
3437 H();
3438 }
3439
3440 if (eIfType != HostNetworkInterfaceType_HostOnly)
3441 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
3442 N_("Interface ('%ls') is not a Host-Only Adapter interface"),
3443 HifName.raw());
3444
3445 hrc = hostInterface->COMGETTER(Id)(bstr.asOutParam());
3446 if (FAILED(hrc))
3447 {
3448 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(Id) failed, hrc (0x%x)\n", hrc));
3449 H();
3450 }
3451 Guid hostIFGuid(bstr);
3452
3453 INetCfg *pNc;
3454 ComPtr<INetCfgComponent> pAdaptorComponent;
3455 LPWSTR pszApp;
3456 rc = VERR_INTNET_FLT_IF_NOT_FOUND;
3457
3458 hrc = VBoxNetCfgWinQueryINetCfg(FALSE,
3459 L"VirtualBox",
3460 &pNc,
3461 &pszApp);
3462 Assert(hrc == S_OK);
3463 if (hrc == S_OK)
3464 {
3465 /* get the adapter's INetCfgComponent*/
3466 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.ptr(), pAdaptorComponent.asOutParam());
3467 if (hrc != S_OK)
3468 {
3469 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3470 LogRel(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc=%Rhrc (0x%x)\n", hrc, hrc));
3471 H();
3472 }
3473 }
3474#define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\"
3475 char szTrunkName[INTNET_MAX_TRUNK_NAME];
3476 char *pszTrunkName = szTrunkName;
3477 wchar_t * pswzBindName;
3478 hrc = pAdaptorComponent->GetBindName(&pswzBindName);
3479 Assert(hrc == S_OK);
3480 if (hrc == S_OK)
3481 {
3482 int cwBindName = (int)wcslen(pswzBindName) + 1;
3483 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX);
3484 if (sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName)
3485 {
3486 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX);
3487 pszTrunkName += cbFullBindNamePrefix-1;
3488 if (!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName,
3489 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL))
3490 {
3491 DWORD err = GetLastError();
3492 hrc = HRESULT_FROM_WIN32(err);
3493 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: WideCharToMultiByte failed, hr=%Rhrc (0x%x) err=%u\n", hrc, hrc, err));
3494 }
3495 }
3496 else
3497 {
3498 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: insufficient szTrunkName buffer space\n"));
3499 /** @todo set appropriate error code */
3500 hrc = E_FAIL;
3501 }
3502
3503 if (hrc != S_OK)
3504 {
3505 AssertFailed();
3506 CoTaskMemFree(pswzBindName);
3507 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3508 H();
3509 }
3510 }
3511 else
3512 {
3513 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3514 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc=%Rhrc (0x%x)\n", hrc, hrc));
3515 H();
3516 }
3517
3518
3519 CoTaskMemFree(pswzBindName);
3520
3521 pAdaptorComponent.setNull();
3522 /* release the pNc finally */
3523 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3524
3525 const char *pszTrunk = szTrunkName;
3526
3527 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp); RC_CHECK();
3528 rc = CFGMR3InsertString(pCfg, "Trunk", pszTrunk); RC_CHECK();
3529 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); RC_CHECK();
3530 networkName = Bstr(szNetwork);
3531 trunkName = Bstr(pszTrunk);
3532 trunkType = TRUNKTYPE_NETADP;
3533# endif /* defined VBOX_WITH_NETFLT*/
3534#elif defined(RT_OS_DARWIN)
3535 rc = CFGMR3InsertString(pCfg, "Trunk", pszHifName); RC_CHECK();
3536 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); RC_CHECK();
3537 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp); RC_CHECK();
3538 networkName = Bstr(szNetwork);
3539 trunkName = Bstr(pszHifName);
3540 trunkType = TRUNKTYPE_NETADP;
3541#else
3542 rc = CFGMR3InsertString(pCfg, "Trunk", pszHifName); RC_CHECK();
3543 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); RC_CHECK();
3544 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt); RC_CHECK();
3545 networkName = Bstr(szNetwork);
3546 trunkName = Bstr(pszHifName);
3547 trunkType = TRUNKTYPE_NETFLT;
3548#endif
3549#if !defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT)
3550
3551 Bstr tmpAddr, tmpMask;
3552
3553 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPAddress", pszHifName), tmpAddr.asOutParam());
3554 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty())
3555 {
3556 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPNetMask", pszHifName), tmpMask.asOutParam());
3557 if (SUCCEEDED(hrc) && !tmpMask.isEmpty())
3558 hrc = hostInterface->EnableStaticIpConfig(tmpAddr, tmpMask);
3559 else
3560 hrc = hostInterface->EnableStaticIpConfig(tmpAddr,
3561 Bstr(VBOXNET_IPV4MASK_DEFAULT));
3562 }
3563 else
3564 {
3565 /* Grab the IP number from the 'vboxnetX' instance number (see netif.h) */
3566 hrc = hostInterface->EnableStaticIpConfig(getDefaultIPv4Address(Bstr(pszHifName)),
3567 Bstr(VBOXNET_IPV4MASK_DEFAULT));
3568 }
3569
3570 ComAssertComRC(hrc); /** @todo r=bird: Why this isn't fatal? (H()) */
3571
3572 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPV6Address", pszHifName), tmpAddr.asOutParam());
3573 if (SUCCEEDED(hrc))
3574 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPV6NetMask", pszHifName), tmpMask.asOutParam());
3575 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty() && !tmpMask.isEmpty())
3576 {
3577 hrc = hostInterface->EnableStaticIpConfigV6(tmpAddr, Utf8Str(tmpMask).toUInt32());
3578 ComAssertComRC(hrc); /** @todo r=bird: Why this isn't fatal? (H()) */
3579 }
3580#endif
3581 break;
3582 }
3583
3584#if defined(VBOX_WITH_VDE)
3585 case NetworkAttachmentType_VDE:
3586 {
3587 hrc = aNetworkAdapter->COMGETTER(VDENetwork)(bstr.asOutParam()); H();
3588 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
3589 rc = CFGMR3InsertString(pLunL0, "Driver", "VDE"); RC_CHECK();
3590 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
3591 if (!bstr.isEmpty())
3592 {
3593 rc = CFGMR3InsertStringW(pCfg, "Network", bstr); RC_CHECK();
3594 networkName = bstr;
3595 }
3596 break;
3597 }
3598#endif
3599
3600 default:
3601 AssertMsgFailed(("should not get here!\n"));
3602 break;
3603 }
3604
3605 /*
3606 * Attempt to attach the driver.
3607 */
3608 switch (eAttachmentType)
3609 {
3610 case NetworkAttachmentType_Null:
3611 break;
3612
3613 case NetworkAttachmentType_Bridged:
3614 case NetworkAttachmentType_Internal:
3615 case NetworkAttachmentType_HostOnly:
3616 case NetworkAttachmentType_NAT:
3617#if defined(VBOX_WITH_VDE)
3618 case NetworkAttachmentType_VDE:
3619#endif
3620 {
3621 if (SUCCEEDED(hrc) && SUCCEEDED(rc))
3622 {
3623 if (fAttachDetach)
3624 {
3625 rc = PDMR3DriverAttach(pVM, pszDevice, uInstance, uLun, 0 /*fFlags*/, NULL /* ppBase */);
3626 //AssertRC(rc);
3627 }
3628
3629 {
3630 /** @todo pritesh: get the dhcp server name from the
3631 * previous network configuration and then stop the server
3632 * else it may conflict with the dhcp server running with
3633 * the current attachment type
3634 */
3635 /* Stop the hostonly DHCP Server */
3636 }
3637
3638 if (!networkName.isEmpty())
3639 {
3640 /*
3641 * Until we implement service reference counters DHCP Server will be stopped
3642 * by DHCPServerRunner destructor.
3643 */
3644 ComPtr<IDHCPServer> dhcpServer;
3645 hrc = virtualBox->FindDHCPServerByNetworkName(networkName, dhcpServer.asOutParam());
3646 if (SUCCEEDED(hrc))
3647 {
3648 /* there is a DHCP server available for this network */
3649 BOOL fEnabled;
3650 hrc = dhcpServer->COMGETTER(Enabled)(&fEnabled);
3651 if (FAILED(hrc))
3652 {
3653 LogRel(("DHCP svr: COMGETTER(Enabled) failed, hrc (%Rhrc)", hrc));
3654 H();
3655 }
3656
3657 if (fEnabled)
3658 hrc = dhcpServer->Start(networkName, trunkName, trunkType);
3659 }
3660 else
3661 hrc = S_OK;
3662 }
3663 }
3664
3665 break;
3666 }
3667
3668 default:
3669 AssertMsgFailed(("should not get here!\n"));
3670 break;
3671 }
3672
3673 meAttachmentType[uInstance] = eAttachmentType;
3674
3675#undef H
3676#undef RC_CHECK
3677
3678 return VINF_SUCCESS;
3679}
3680
3681#ifdef VBOX_WITH_GUEST_PROPS
3682/**
3683 * Set an array of guest properties
3684 */
3685static void configSetProperties(VMMDev * const pVMMDev, void *names,
3686 void *values, void *timestamps, void *flags)
3687{
3688 VBOXHGCMSVCPARM parms[4];
3689
3690 parms[0].type = VBOX_HGCM_SVC_PARM_PTR;
3691 parms[0].u.pointer.addr = names;
3692 parms[0].u.pointer.size = 0; /* We don't actually care. */
3693 parms[1].type = VBOX_HGCM_SVC_PARM_PTR;
3694 parms[1].u.pointer.addr = values;
3695 parms[1].u.pointer.size = 0; /* We don't actually care. */
3696 parms[2].type = VBOX_HGCM_SVC_PARM_PTR;
3697 parms[2].u.pointer.addr = timestamps;
3698 parms[2].u.pointer.size = 0; /* We don't actually care. */
3699 parms[3].type = VBOX_HGCM_SVC_PARM_PTR;
3700 parms[3].u.pointer.addr = flags;
3701 parms[3].u.pointer.size = 0; /* We don't actually care. */
3702
3703 pVMMDev->hgcmHostCall ("VBoxGuestPropSvc", guestProp::SET_PROPS_HOST, 4,
3704 &parms[0]);
3705}
3706
3707/**
3708 * Set a single guest property
3709 */
3710static void configSetProperty(VMMDev * const pVMMDev, const char *pszName,
3711 const char *pszValue, const char *pszFlags)
3712{
3713 VBOXHGCMSVCPARM parms[4];
3714
3715 AssertPtrReturnVoid(pszName);
3716 AssertPtrReturnVoid(pszValue);
3717 AssertPtrReturnVoid(pszFlags);
3718 parms[0].type = VBOX_HGCM_SVC_PARM_PTR;
3719 parms[0].u.pointer.addr = (void *)pszName;
3720 parms[0].u.pointer.size = strlen(pszName) + 1;
3721 parms[1].type = VBOX_HGCM_SVC_PARM_PTR;
3722 parms[1].u.pointer.addr = (void *)pszValue;
3723 parms[1].u.pointer.size = strlen(pszValue) + 1;
3724 parms[2].type = VBOX_HGCM_SVC_PARM_PTR;
3725 parms[2].u.pointer.addr = (void *)pszFlags;
3726 parms[2].u.pointer.size = strlen(pszFlags) + 1;
3727 pVMMDev->hgcmHostCall("VBoxGuestPropSvc", guestProp::SET_PROP_HOST, 3,
3728 &parms[0]);
3729}
3730
3731/**
3732 * Set the global flags value by calling the service
3733 * @returns the status returned by the call to the service
3734 *
3735 * @param pTable the service instance handle
3736 * @param eFlags the flags to set
3737 */
3738int configSetGlobalPropertyFlags(VMMDev * const pVMMDev,
3739 guestProp::ePropFlags eFlags)
3740{
3741 VBOXHGCMSVCPARM paParm;
3742 paParm.setUInt32(eFlags);
3743 int rc = pVMMDev->hgcmHostCall("VBoxGuestPropSvc",
3744 guestProp::SET_GLOBAL_FLAGS_HOST, 1,
3745 &paParm);
3746 if (RT_FAILURE(rc))
3747 {
3748 char szFlags[guestProp::MAX_FLAGS_LEN];
3749 if (RT_FAILURE(writeFlags(eFlags, szFlags)))
3750 Log(("Failed to set the global flags.\n"));
3751 else
3752 Log(("Failed to set the global flags \"%s\".\n", szFlags));
3753 }
3754 return rc;
3755}
3756#endif /* VBOX_WITH_GUEST_PROPS */
3757
3758/**
3759 * Set up the Guest Property service, populate it with properties read from
3760 * the machine XML and set a couple of initial properties.
3761 */
3762/* static */ int Console::configGuestProperties(void *pvConsole)
3763{
3764#ifdef VBOX_WITH_GUEST_PROPS
3765 AssertReturn(pvConsole, VERR_GENERAL_FAILURE);
3766 ComObjPtr<Console> pConsole = static_cast<Console *>(pvConsole);
3767
3768 /* Load the service */
3769 int rc = pConsole->mVMMDev->hgcmLoadService("VBoxGuestPropSvc", "VBoxGuestPropSvc");
3770
3771 if (RT_FAILURE(rc))
3772 {
3773 LogRel(("VBoxGuestPropSvc is not available. rc = %Rrc\n", rc));
3774 /* That is not a fatal failure. */
3775 rc = VINF_SUCCESS;
3776 }
3777 else
3778 {
3779 /*
3780 * Initialize built-in properties that can be changed and saved.
3781 *
3782 * These are typically transient properties that the guest cannot
3783 * change.
3784 */
3785
3786 /* Sysprep execution by VBoxService. */
3787 configSetProperty(pConsole->mVMMDev,
3788 "/VirtualBox/HostGuest/SysprepExec", "",
3789 "TRANSIENT, RDONLYGUEST");
3790 configSetProperty(pConsole->mVMMDev,
3791 "/VirtualBox/HostGuest/SysprepArgs", "",
3792 "TRANSIENT, RDONLYGUEST");
3793
3794 /*
3795 * Pull over the properties from the server.
3796 */
3797 SafeArray<BSTR> namesOut;
3798 SafeArray<BSTR> valuesOut;
3799 SafeArray<ULONG64> timestampsOut;
3800 SafeArray<BSTR> flagsOut;
3801 HRESULT hrc;
3802 hrc = pConsole->mControl->PullGuestProperties(ComSafeArrayAsOutParam(namesOut),
3803 ComSafeArrayAsOutParam(valuesOut),
3804 ComSafeArrayAsOutParam(timestampsOut),
3805 ComSafeArrayAsOutParam(flagsOut));
3806 AssertMsgReturn(SUCCEEDED(hrc), ("hrc=%Rrc\n", hrc), VERR_GENERAL_FAILURE);
3807 size_t cProps = namesOut.size();
3808 size_t cAlloc = cProps + 1;
3809 if ( valuesOut.size() != cProps
3810 || timestampsOut.size() != cProps
3811 || flagsOut.size() != cProps
3812 )
3813 AssertFailedReturn(VERR_INVALID_PARAMETER);
3814
3815 char **papszNames, **papszValues, **papszFlags;
3816 char szEmpty[] = "";
3817 ULONG64 *pau64Timestamps;
3818 papszNames = (char **)RTMemTmpAllocZ(sizeof(void *) * cAlloc);
3819 papszValues = (char **)RTMemTmpAllocZ(sizeof(void *) * cAlloc);
3820 pau64Timestamps = (ULONG64 *)RTMemTmpAllocZ(sizeof(ULONG64) * cAlloc);
3821 papszFlags = (char **)RTMemTmpAllocZ(sizeof(void *) * cAlloc);
3822 if (papszNames && papszValues && pau64Timestamps && papszFlags)
3823 {
3824 for (unsigned i = 0; RT_SUCCESS(rc) && i < cProps; ++i)
3825 {
3826 AssertPtrReturn(namesOut[i], VERR_INVALID_PARAMETER);
3827 rc = RTUtf16ToUtf8(namesOut[i], &papszNames[i]);
3828 if (RT_FAILURE(rc))
3829 break;
3830 if (valuesOut[i])
3831 rc = RTUtf16ToUtf8(valuesOut[i], &papszValues[i]);
3832 else
3833 papszValues[i] = szEmpty;
3834 if (RT_FAILURE(rc))
3835 break;
3836 pau64Timestamps[i] = timestampsOut[i];
3837 if (flagsOut[i])
3838 rc = RTUtf16ToUtf8(flagsOut[i], &papszFlags[i]);
3839 else
3840 papszFlags[i] = szEmpty;
3841 }
3842 if (RT_SUCCESS(rc))
3843 configSetProperties(pConsole->mVMMDev,
3844 (void *)papszNames,
3845 (void *)papszValues,
3846 (void *)pau64Timestamps,
3847 (void *)papszFlags);
3848 for (unsigned i = 0; i < cProps; ++i)
3849 {
3850 RTStrFree(papszNames[i]);
3851 if (valuesOut[i])
3852 RTStrFree(papszValues[i]);
3853 if (flagsOut[i])
3854 RTStrFree(papszFlags[i]);
3855 }
3856 }
3857 else
3858 rc = VERR_NO_MEMORY;
3859 RTMemTmpFree(papszNames);
3860 RTMemTmpFree(papszValues);
3861 RTMemTmpFree(pau64Timestamps);
3862 RTMemTmpFree(papszFlags);
3863 AssertRCReturn(rc, rc);
3864
3865 /*
3866 * These properties have to be set before pulling over the properties
3867 * from the machine XML, to ensure that properties saved in the XML
3868 * will override them.
3869 */
3870 /* Set the VBox version string as a guest property */
3871 configSetProperty(pConsole->mVMMDev, "/VirtualBox/HostInfo/VBoxVer",
3872 VBOX_VERSION_STRING, "TRANSIENT, RDONLYGUEST");
3873 /* Set the VBox SVN revision as a guest property */
3874 configSetProperty(pConsole->mVMMDev, "/VirtualBox/HostInfo/VBoxRev",
3875 RTBldCfgRevisionStr(), "TRANSIENT, RDONLYGUEST");
3876
3877 /*
3878 * Register the host notification callback
3879 */
3880 HGCMSVCEXTHANDLE hDummy;
3881 HGCMHostRegisterServiceExtension(&hDummy, "VBoxGuestPropSvc",
3882 Console::doGuestPropNotification,
3883 pvConsole);
3884
3885#ifdef VBOX_WITH_GUEST_PROPS_RDONLY_GUEST
3886 rc = configSetGlobalPropertyFlags(pConsole->mVMMDev,
3887 guestProp::RDONLYGUEST);
3888 AssertRCReturn(rc, rc);
3889#endif
3890
3891 Log(("Set VBoxGuestPropSvc property store\n"));
3892 }
3893 return VINF_SUCCESS;
3894#else /* !VBOX_WITH_GUEST_PROPS */
3895 return VERR_NOT_SUPPORTED;
3896#endif /* !VBOX_WITH_GUEST_PROPS */
3897}
3898
3899/**
3900 * Set up the Guest Control service.
3901 */
3902/* static */ int Console::configGuestControl(void *pvConsole)
3903{
3904#ifdef VBOX_WITH_GUEST_CONTROL
3905 AssertReturn(pvConsole, VERR_GENERAL_FAILURE);
3906 ComObjPtr<Console> pConsole = static_cast<Console *>(pvConsole);
3907
3908 /* Load the service */
3909 int rc = pConsole->mVMMDev->hgcmLoadService("VBoxGuestControlSvc", "VBoxGuestControlSvc");
3910
3911 if (RT_FAILURE(rc))
3912 {
3913 LogRel(("VBoxGuestControlSvc is not available. rc = %Rrc\n", rc));
3914 /* That is not a fatal failure. */
3915 rc = VINF_SUCCESS;
3916 }
3917 else
3918 {
3919 HGCMSVCEXTHANDLE hDummy;
3920 rc = HGCMHostRegisterServiceExtension(&hDummy, "VBoxGuestControlSvc",
3921 &Guest::doGuestCtrlNotification,
3922 pConsole->getGuest());
3923 if (RT_FAILURE(rc))
3924 Log(("Cannot register VBoxGuestControlSvc extension!\n"));
3925 else
3926 Log(("VBoxGuestControlSvc loaded\n"));
3927 }
3928
3929 return rc;
3930#else /* !VBOX_WITH_GUEST_CONTROL */
3931 return VERR_NOT_SUPPORTED;
3932#endif /* !VBOX_WITH_GUEST_CONTROL */
3933}
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