VirtualBox

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

Last change on this file since 30715 was 30715, checked in by vboxsync, 14 years ago

VMMDev: Adding an optional (disabled by default) testing side to the device to assist simple guest benchmarks and tests. Started on a MMIO and IOPort benchmark (for comparison with network performance numbers).

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