VirtualBox

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

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

small fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 181.0 KB
Line 
1/* $Id: ConsoleImpl2.cpp 30290 2010-06-17 16:48:56Z 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 /* cpuid leaf overrides. */
438 static uint32_t const s_auCpuIdRanges[] =
439 {
440 UINT32_C(0x00000000), UINT32_C(0x0000000a),
441 UINT32_C(0x80000000), UINT32_C(0x8000000a)
442 };
443 for (unsigned i = 0; i < RT_ELEMENTS(s_auCpuIdRanges); i += 2)
444 for (uint32_t uLeaf = s_auCpuIdRanges[i]; uLeaf < s_auCpuIdRanges[i + 1]; uLeaf++)
445 {
446 ULONG ulEax, ulEbx, ulEcx, ulEdx;
447 hrc = pMachine->GetCPUIDLeaf(uLeaf, &ulEax, &ulEbx, &ulEcx, &ulEdx);
448 if (SUCCEEDED(hrc))
449 {
450 PCFGMNODE pLeaf;
451 rc = CFGMR3InsertNodeF(pRoot, &pLeaf, "CPUM/HostCPUID/%RX32", uLeaf); RC_CHECK();
452
453 rc = CFGMR3InsertInteger(pLeaf, "eax", ulEax); RC_CHECK();
454 rc = CFGMR3InsertInteger(pLeaf, "ebx", ulEbx); RC_CHECK();
455 rc = CFGMR3InsertInteger(pLeaf, "ecx", ulEcx); RC_CHECK();
456 rc = CFGMR3InsertInteger(pLeaf, "edx", ulEdx); RC_CHECK();
457 }
458 else if (hrc != E_INVALIDARG) H();
459 }
460
461 if (osTypeId == "WindowsNT4")
462 {
463 /*
464 * We must limit CPUID count for Windows NT 4, as otherwise it stops
465 * with error 0x3e (MULTIPROCESSOR_CONFIGURATION_NOT_SUPPORTED).
466 */
467 LogRel(("Limiting CPUID leaf count for NT4 guests\n"));
468 PCFGMNODE pCPUM;
469 rc = CFGMR3InsertNode(pRoot, "CPUM", &pCPUM); RC_CHECK();
470 rc = CFGMR3InsertInteger(pCPUM, "NT4LeafLimit", true); RC_CHECK();
471 }
472
473 if (fOsXGuest)
474 {
475 /*
476 * Expose extended MWAIT features to Mac OS X guests.
477 */
478 LogRel(("Using MWAIT extensions\n"));
479 PCFGMNODE pCPUM;
480 rc = CFGMR3InsertNode(pRoot, "CPUM", &pCPUM); RC_CHECK();
481 rc = CFGMR3InsertInteger(pCPUM, "MWaitExtensions", true); RC_CHECK();
482 }
483
484 /* hardware virtualization extensions */
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 if (hwVersion.compare(Bstr("1")) == 0) /* <= 2.0.x */
1619 {
1620 CFGMR3InsertInteger(pCfg, "HeapEnabled", 0); RC_CHECK();
1621 }
1622
1623 /* the VMM device's Main driver */
1624 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
1625 rc = CFGMR3InsertString(pLunL0, "Driver", "HGCM"); RC_CHECK();
1626 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1627 VMMDev *pVMMDev = pConsole->mVMMDev;
1628 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)pVMMDev); RC_CHECK();
1629
1630 /*
1631 * Attach the status driver.
1632 */
1633 rc = CFGMR3InsertNode(pInst, "LUN#999", &pLunL0); RC_CHECK();
1634 rc = CFGMR3InsertString(pLunL0, "Driver", "MainStatus"); RC_CHECK();
1635 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1636 rc = CFGMR3InsertInteger(pCfg, "papLeds", (uintptr_t)&pConsole->mapSharedFolderLed); RC_CHECK();
1637 rc = CFGMR3InsertInteger(pCfg, "First", 0); RC_CHECK();
1638 rc = CFGMR3InsertInteger(pCfg, "Last", 0); RC_CHECK();
1639
1640 /*
1641 * Audio Sniffer Device
1642 */
1643 rc = CFGMR3InsertNode(pDevices, "AudioSniffer", &pDev); RC_CHECK();
1644 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
1645 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1646
1647 /* the Audio Sniffer device's Main driver */
1648 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
1649 rc = CFGMR3InsertString(pLunL0, "Driver", "MainAudioSniffer"); RC_CHECK();
1650 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1651 AudioSniffer *pAudioSniffer = pConsole->mAudioSniffer;
1652 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)pAudioSniffer); RC_CHECK();
1653
1654 /*
1655 * AC'97 ICH / SoundBlaster16 audio
1656 */
1657 BOOL enabled;
1658 ComPtr<IAudioAdapter> audioAdapter;
1659 hrc = pMachine->COMGETTER(AudioAdapter)(audioAdapter.asOutParam()); H();
1660 if (audioAdapter)
1661 hrc = audioAdapter->COMGETTER(Enabled)(&enabled); H();
1662
1663 if (enabled)
1664 {
1665 AudioControllerType_T audioController;
1666 hrc = audioAdapter->COMGETTER(AudioController)(&audioController); H();
1667 switch (audioController)
1668 {
1669 case AudioControllerType_AC97:
1670 {
1671 /* default: ICH AC97 */
1672 rc = CFGMR3InsertNode(pDevices, "ichac97", &pDev); RC_CHECK();
1673 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1674 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* bool */ RC_CHECK();
1675 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 5); RC_CHECK();
1676 Assert(!afPciDeviceNo[5]);
1677 afPciDeviceNo[5] = true;
1678 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); RC_CHECK();
1679 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1680 break;
1681 }
1682 case AudioControllerType_SB16:
1683 {
1684 /* legacy SoundBlaster16 */
1685 rc = CFGMR3InsertNode(pDevices, "sb16", &pDev); RC_CHECK();
1686 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
1687 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* bool */ RC_CHECK();
1688 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1689 rc = CFGMR3InsertInteger(pCfg, "IRQ", 5); RC_CHECK();
1690 rc = CFGMR3InsertInteger(pCfg, "DMA", 1); RC_CHECK();
1691 rc = CFGMR3InsertInteger(pCfg, "DMA16", 5); RC_CHECK();
1692 rc = CFGMR3InsertInteger(pCfg, "Port", 0x220); RC_CHECK();
1693 rc = CFGMR3InsertInteger(pCfg, "Version", 0x0405); RC_CHECK();
1694 break;
1695 }
1696 }
1697
1698 /* the Audio driver */
1699 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
1700 rc = CFGMR3InsertString(pLunL0, "Driver", "AUDIO"); RC_CHECK();
1701 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1702
1703 AudioDriverType_T audioDriver;
1704 hrc = audioAdapter->COMGETTER(AudioDriver)(&audioDriver); H();
1705 switch (audioDriver)
1706 {
1707 case AudioDriverType_Null:
1708 {
1709 rc = CFGMR3InsertString(pCfg, "AudioDriver", "null"); RC_CHECK();
1710 break;
1711 }
1712#ifdef RT_OS_WINDOWS
1713#ifdef VBOX_WITH_WINMM
1714 case AudioDriverType_WinMM:
1715 {
1716 rc = CFGMR3InsertString(pCfg, "AudioDriver", "winmm"); RC_CHECK();
1717 break;
1718 }
1719#endif
1720 case AudioDriverType_DirectSound:
1721 {
1722 rc = CFGMR3InsertString(pCfg, "AudioDriver", "dsound"); RC_CHECK();
1723 break;
1724 }
1725#endif /* RT_OS_WINDOWS */
1726#ifdef RT_OS_SOLARIS
1727 case AudioDriverType_SolAudio:
1728 {
1729 rc = CFGMR3InsertString(pCfg, "AudioDriver", "solaudio"); RC_CHECK();
1730 break;
1731 }
1732#endif
1733#ifdef RT_OS_LINUX
1734# ifdef VBOX_WITH_ALSA
1735 case AudioDriverType_ALSA:
1736 {
1737 rc = CFGMR3InsertString(pCfg, "AudioDriver", "alsa"); RC_CHECK();
1738 break;
1739 }
1740# endif
1741# ifdef VBOX_WITH_PULSE
1742 case AudioDriverType_Pulse:
1743 {
1744 rc = CFGMR3InsertString(pCfg, "AudioDriver", "pulse"); RC_CHECK();
1745 break;
1746 }
1747# endif
1748#endif /* RT_OS_LINUX */
1749#if defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD) || defined(VBOX_WITH_SOLARIS_OSS)
1750 case AudioDriverType_OSS:
1751 {
1752 rc = CFGMR3InsertString(pCfg, "AudioDriver", "oss"); RC_CHECK();
1753 break;
1754 }
1755#endif
1756#ifdef RT_OS_FREEBSD
1757# ifdef VBOX_WITH_PULSE
1758 case AudioDriverType_Pulse:
1759 {
1760 rc = CFGMR3InsertString(pCfg, "AudioDriver", "pulse"); RC_CHECK();
1761 break;
1762 }
1763# endif
1764#endif
1765#ifdef RT_OS_DARWIN
1766 case AudioDriverType_CoreAudio:
1767 {
1768 rc = CFGMR3InsertString(pCfg, "AudioDriver", "coreaudio"); RC_CHECK();
1769 break;
1770 }
1771#endif
1772 }
1773 hrc = pMachine->COMGETTER(Name)(bstr.asOutParam()); H();
1774 rc = CFGMR3InsertStringW(pCfg, "StreamName", bstr.raw()); RC_CHECK();
1775 }
1776
1777 /*
1778 * The USB Controller.
1779 */
1780 ComPtr<IUSBController> USBCtlPtr;
1781 hrc = pMachine->COMGETTER(USBController)(USBCtlPtr.asOutParam());
1782 if (USBCtlPtr)
1783 {
1784 BOOL fOhciEnabled;
1785 hrc = USBCtlPtr->COMGETTER(Enabled)(&fOhciEnabled); H();
1786 if (fOhciEnabled)
1787 {
1788 rc = CFGMR3InsertNode(pDevices, "usb-ohci", &pDev); RC_CHECK();
1789 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
1790 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1791 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
1792 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 6); RC_CHECK();
1793 Assert(!afPciDeviceNo[6]);
1794 afPciDeviceNo[6] = true;
1795 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); RC_CHECK();
1796
1797 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
1798 rc = CFGMR3InsertString(pLunL0, "Driver", "VUSBRootHub"); RC_CHECK();
1799 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1800
1801 /*
1802 * Attach the status driver.
1803 */
1804 rc = CFGMR3InsertNode(pInst, "LUN#999", &pLunL0); RC_CHECK();
1805 rc = CFGMR3InsertString(pLunL0, "Driver", "MainStatus"); RC_CHECK();
1806 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1807 rc = CFGMR3InsertInteger(pCfg, "papLeds", (uintptr_t)&pConsole->mapUSBLed[0]);RC_CHECK();
1808 rc = CFGMR3InsertInteger(pCfg, "First", 0); RC_CHECK();
1809 rc = CFGMR3InsertInteger(pCfg, "Last", 0); RC_CHECK();
1810
1811#ifdef VBOX_WITH_EHCI
1812 BOOL fEhciEnabled;
1813 hrc = USBCtlPtr->COMGETTER(EnabledEhci)(&fEhciEnabled); H();
1814 if (fEhciEnabled)
1815 {
1816 rc = CFGMR3InsertNode(pDevices, "usb-ehci", &pDev); RC_CHECK();
1817 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
1818 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1819 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* bool */ RC_CHECK();
1820 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 11); RC_CHECK();
1821 Assert(!afPciDeviceNo[11]);
1822 afPciDeviceNo[11] = true;
1823 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); RC_CHECK();
1824
1825 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
1826 rc = CFGMR3InsertString(pLunL0, "Driver", "VUSBRootHub"); RC_CHECK();
1827 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1828
1829 /*
1830 * Attach the status driver.
1831 */
1832 rc = CFGMR3InsertNode(pInst, "LUN#999", &pLunL0); RC_CHECK();
1833 rc = CFGMR3InsertString(pLunL0, "Driver", "MainStatus"); RC_CHECK();
1834 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1835 rc = CFGMR3InsertInteger(pCfg, "papLeds", (uintptr_t)&pConsole->mapUSBLed[1]);RC_CHECK();
1836 rc = CFGMR3InsertInteger(pCfg, "First", 0); RC_CHECK();
1837 rc = CFGMR3InsertInteger(pCfg, "Last", 0); RC_CHECK();
1838 }
1839#endif
1840
1841 /*
1842 * Virtual USB Devices.
1843 */
1844 PCFGMNODE pUsbDevices = NULL;
1845 rc = CFGMR3InsertNode(pRoot, "USB", &pUsbDevices); RC_CHECK();
1846
1847#ifdef VBOX_WITH_USB
1848 {
1849 /*
1850 * Global USB options, currently unused as we'll apply the 2.0 -> 1.1 morphing
1851 * on a per device level now.
1852 */
1853 rc = CFGMR3InsertNode(pUsbDevices, "USBProxy", &pCfg); RC_CHECK();
1854 rc = CFGMR3InsertNode(pCfg, "GlobalConfig", &pCfg); RC_CHECK();
1855 // This globally enables the 2.0 -> 1.1 device morphing of proxied devies to keep windows quiet.
1856 //rc = CFGMR3InsertInteger(pCfg, "Force11Device", true); RC_CHECK();
1857 // The following breaks stuff, but it makes MSDs work in vista. (I include it here so
1858 // that it's documented somewhere.) Users needing it can use:
1859 // VBoxManage setextradata "myvm" "VBoxInternal/USB/USBProxy/GlobalConfig/Force11PacketSize" 1
1860 //rc = CFGMR3InsertInteger(pCfg, "Force11PacketSize", true); RC_CHECK();
1861 }
1862#endif
1863
1864# if 0 /* Virtual MSD*/
1865
1866 rc = CFGMR3InsertNode(pUsbDevices, "Msd", &pDev); RC_CHECK();
1867 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
1868 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1869 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
1870
1871 rc = CFGMR3InsertString(pLunL0, "Driver", "SCSI"); RC_CHECK();
1872 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1873
1874 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); RC_CHECK();
1875 rc = CFGMR3InsertString(pLunL1, "Driver", "Block"); RC_CHECK();
1876 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); RC_CHECK();
1877 rc = CFGMR3InsertString(pCfg, "Type", "HardDisk"); RC_CHECK();
1878 rc = CFGMR3InsertInteger(pCfg, "Mountable", 0); RC_CHECK();
1879
1880 rc = CFGMR3InsertNode(pLunL1, "AttachedDriver", &pLunL2); RC_CHECK();
1881 rc = CFGMR3InsertString(pLunL2, "Driver", "VD"); RC_CHECK();
1882 rc = CFGMR3InsertNode(pLunL2, "Config", &pCfg); RC_CHECK();
1883 rc = CFGMR3InsertString(pCfg, "Path", "/Volumes/DataHFS/bird/VDIs/linux.vdi"); RC_CHECK();
1884 rc = CFGMR3InsertString(pCfg, "Format", "VDI"); RC_CHECK();
1885# endif
1886
1887 /* Virtual USB Mouse/Tablet */
1888 PointingHidType_T aPointingHid;
1889 hrc = pMachine->COMGETTER(PointingHidType)(&aPointingHid); H();
1890 if (aPointingHid == PointingHidType_USBMouse || aPointingHid == PointingHidType_USBTablet)
1891 {
1892 rc = CFGMR3InsertNode(pUsbDevices, "HidMouse", &pDev); RC_CHECK();
1893 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
1894 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1895
1896 if (aPointingHid == PointingHidType_USBTablet)
1897 {
1898 rc = CFGMR3InsertInteger(pCfg, "Absolute", 1); RC_CHECK();
1899 }
1900 else
1901 {
1902 rc = CFGMR3InsertInteger(pCfg, "Absolute", 0); RC_CHECK();
1903 }
1904 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
1905 rc = CFGMR3InsertString(pLunL0, "Driver", "MouseQueue"); RC_CHECK();
1906 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1907 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 128); RC_CHECK();
1908
1909 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); RC_CHECK();
1910 rc = CFGMR3InsertString(pLunL1, "Driver", "MainMouse"); RC_CHECK();
1911 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); RC_CHECK();
1912 pMouse = pConsole->mMouse;
1913 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)pMouse); RC_CHECK();
1914 }
1915
1916 /* Virtual USB Keyboard */
1917 KeyboardHidType_T aKbdHid;
1918 hrc = pMachine->COMGETTER(KeyboardHidType)(&aKbdHid); H();
1919 if (aKbdHid == KeyboardHidType_USBKeyboard)
1920 {
1921 rc = CFGMR3InsertNode(pUsbDevices, "HidKeyboard", &pDev); RC_CHECK();
1922 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
1923 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
1924
1925 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
1926 rc = CFGMR3InsertString(pLunL0, "Driver", "KeyboardQueue"); RC_CHECK();
1927 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
1928 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 64); RC_CHECK();
1929
1930 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); RC_CHECK();
1931 rc = CFGMR3InsertString(pLunL1, "Driver", "MainKeyboard"); RC_CHECK();
1932 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); RC_CHECK();
1933 pKeyboard = pConsole->mKeyboard;
1934 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)pKeyboard); RC_CHECK();
1935 }
1936 }
1937 }
1938
1939 /*
1940 * Clipboard
1941 */
1942 {
1943 ClipboardMode_T mode = ClipboardMode_Disabled;
1944 hrc = pMachine->COMGETTER(ClipboardMode)(&mode); H();
1945
1946 if (mode != ClipboardMode_Disabled)
1947 {
1948 /* Load the service */
1949 rc = pConsole->mVMMDev->hgcmLoadService("VBoxSharedClipboard", "VBoxSharedClipboard");
1950
1951 if (RT_FAILURE(rc))
1952 {
1953 LogRel(("VBoxSharedClipboard is not available. rc = %Rrc\n", rc));
1954 /* That is not a fatal failure. */
1955 rc = VINF_SUCCESS;
1956 }
1957 else
1958 {
1959 /* Setup the service. */
1960 VBOXHGCMSVCPARM parm;
1961
1962 parm.type = VBOX_HGCM_SVC_PARM_32BIT;
1963
1964 switch (mode)
1965 {
1966 default:
1967 case ClipboardMode_Disabled:
1968 {
1969 LogRel(("VBoxSharedClipboard mode: Off\n"));
1970 parm.u.uint32 = VBOX_SHARED_CLIPBOARD_MODE_OFF;
1971 break;
1972 }
1973 case ClipboardMode_GuestToHost:
1974 {
1975 LogRel(("VBoxSharedClipboard mode: Guest to Host\n"));
1976 parm.u.uint32 = VBOX_SHARED_CLIPBOARD_MODE_GUEST_TO_HOST;
1977 break;
1978 }
1979 case ClipboardMode_HostToGuest:
1980 {
1981 LogRel(("VBoxSharedClipboard mode: Host to Guest\n"));
1982 parm.u.uint32 = VBOX_SHARED_CLIPBOARD_MODE_HOST_TO_GUEST;
1983 break;
1984 }
1985 case ClipboardMode_Bidirectional:
1986 {
1987 LogRel(("VBoxSharedClipboard mode: Bidirectional\n"));
1988 parm.u.uint32 = VBOX_SHARED_CLIPBOARD_MODE_BIDIRECTIONAL;
1989 break;
1990 }
1991 }
1992
1993 pConsole->mVMMDev->hgcmHostCall("VBoxSharedClipboard", VBOX_SHARED_CLIPBOARD_HOST_FN_SET_MODE, 1, &parm);
1994
1995 Log(("Set VBoxSharedClipboard mode\n"));
1996 }
1997 }
1998 }
1999
2000#ifdef VBOX_WITH_CROGL
2001 /*
2002 * crOpenGL
2003 */
2004 {
2005 BOOL fEnabled = false;
2006 hrc = pMachine->COMGETTER(Accelerate3DEnabled)(&fEnabled); H();
2007
2008 if (fEnabled)
2009 {
2010 /* Load the service */
2011 rc = pConsole->mVMMDev->hgcmLoadService("VBoxSharedCrOpenGL", "VBoxSharedCrOpenGL");
2012 if (RT_FAILURE(rc))
2013 {
2014 LogRel(("Failed to load Shared OpenGL service %Rrc\n", rc));
2015 /* That is not a fatal failure. */
2016 rc = VINF_SUCCESS;
2017 }
2018 else
2019 {
2020 LogRel(("Shared crOpenGL service loaded.\n"));
2021
2022 /* Setup the service. */
2023 VBOXHGCMSVCPARM parm;
2024 parm.type = VBOX_HGCM_SVC_PARM_PTR;
2025
2026 parm.u.pointer.addr = (IConsole*) (Console*) pConsole;
2027 parm.u.pointer.size = sizeof(IConsole *);
2028
2029 rc = pConsole->mVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_SET_CONSOLE,
2030 SHCRGL_CPARMS_SET_CONSOLE, &parm);
2031 if (!RT_SUCCESS(rc))
2032 AssertMsgFailed(("SHCRGL_HOST_FN_SET_CONSOLE failed with %Rrc\n", rc));
2033
2034 parm.u.pointer.addr = pVM;
2035 parm.u.pointer.size = sizeof(pVM);
2036 rc = pConsole->mVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_SET_VM,
2037 SHCRGL_CPARMS_SET_VM, &parm);
2038 if (!RT_SUCCESS(rc))
2039 AssertMsgFailed(("SHCRGL_HOST_FN_SET_VM failed with %Rrc\n", rc));
2040 }
2041
2042 }
2043 }
2044#endif
2045
2046#ifdef VBOX_WITH_GUEST_PROPS
2047 /*
2048 * Guest property service
2049 */
2050
2051 rc = configGuestProperties(pConsole);
2052#endif /* VBOX_WITH_GUEST_PROPS defined */
2053
2054#ifdef VBOX_WITH_GUEST_CONTROL
2055 /*
2056 * Guest control service
2057 */
2058
2059 rc = configGuestControl(pConsole);
2060#endif /* VBOX_WITH_GUEST_CONTROL defined */
2061
2062 /*
2063 * ACPI
2064 */
2065 BOOL fACPI;
2066 hrc = biosSettings->COMGETTER(ACPIEnabled)(&fACPI); H();
2067 if (fACPI)
2068 {
2069 BOOL fCpuHotPlug = false;
2070 BOOL fShowCpu = fOsXGuest;
2071 /* Always show the CPU leafs when we have multiple VCPUs or when the IO-APIC is enabled.
2072 * The Windows SMP kernel needs a CPU leaf or else its idle loop will burn cpu cycles; the
2073 * intelppm driver refuses to register an idle state handler.
2074 */
2075 if ((cCpus > 1) || fIOAPIC)
2076 fShowCpu = true;
2077
2078 hrc = pMachine->COMGETTER(CPUHotPlugEnabled)(&fCpuHotPlug); H();
2079
2080 rc = CFGMR3InsertNode(pDevices, "acpi", &pDev); RC_CHECK();
2081 rc = CFGMR3InsertNode(pDev, "0", &pInst); RC_CHECK();
2082 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ RC_CHECK();
2083 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); RC_CHECK();
2084 rc = CFGMR3InsertInteger(pCfg, "RamSize", cbRam); RC_CHECK();
2085 rc = CFGMR3InsertInteger(pCfg, "RamHoleSize", cbRamHole); RC_CHECK();
2086 rc = CFGMR3InsertInteger(pCfg, "NumCPUs", cCpus); RC_CHECK();
2087
2088 rc = CFGMR3InsertInteger(pCfg, "IOAPIC", fIOAPIC); RC_CHECK();
2089 rc = CFGMR3InsertInteger(pCfg, "FdcEnabled", fFdcEnabled); RC_CHECK();
2090 rc = CFGMR3InsertInteger(pCfg, "HpetEnabled", fHpetEnabled); RC_CHECK();
2091 rc = CFGMR3InsertInteger(pCfg, "SmcEnabled", fSmcEnabled); RC_CHECK();
2092 rc = CFGMR3InsertInteger(pCfg, "ShowRtc", fOsXGuest); RC_CHECK();
2093 if (fOsXGuest && !llBootNics.empty())
2094 {
2095 BootNic aNic = llBootNics.front();
2096 uint32_t u32NicPciAddr = (aNic.mPciDev << 16) | aNic.mPciFn;
2097 rc = CFGMR3InsertInteger(pCfg, "NicPciAddress", u32NicPciAddr); RC_CHECK();
2098 }
2099 rc = CFGMR3InsertInteger(pCfg, "ShowCpu", fShowCpu); RC_CHECK();
2100 rc = CFGMR3InsertInteger(pCfg, "CpuHotPlug", fCpuHotPlug); RC_CHECK();
2101 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 7); RC_CHECK();
2102 Assert(!afPciDeviceNo[7]);
2103 afPciDeviceNo[7] = true;
2104 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); RC_CHECK();
2105
2106 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
2107 rc = CFGMR3InsertString(pLunL0, "Driver", "ACPIHost"); RC_CHECK();
2108 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2109
2110 /* Attach the dummy CPU drivers */
2111 for (ULONG iCpuCurr = 1; iCpuCurr < cCpus; iCpuCurr++)
2112 {
2113 BOOL fCpuAttached = true;
2114
2115 if (fCpuHotPlug)
2116 {
2117 hrc = pMachine->GetCPUStatus(iCpuCurr, &fCpuAttached); H();
2118 }
2119
2120 if (fCpuAttached)
2121 {
2122 rc = CFGMR3InsertNodeF(pInst, &pLunL0, "LUN#%u", iCpuCurr); RC_CHECK();
2123 rc = CFGMR3InsertString(pLunL0, "Driver", "ACPICpu"); RC_CHECK();
2124 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2125 }
2126 }
2127 }
2128
2129
2130 /*
2131 * CFGM overlay handling.
2132 *
2133 * Here we check the extra data entries for CFGM values
2134 * and create the nodes and insert the values on the fly. Existing
2135 * values will be removed and reinserted. CFGM is typed, so by default
2136 * we will guess whether it's a string or an integer (byte arrays are
2137 * not currently supported). It's possible to override this autodetection
2138 * by adding "string:", "integer:" or "bytes:" (future).
2139 *
2140 * We first perform a run on global extra data, then on the machine
2141 * extra data to support global settings with local overrides.
2142 *
2143 */
2144 /** @todo add support for removing nodes and byte blobs. */
2145 SafeArray<BSTR> aGlobalExtraDataKeys;
2146 SafeArray<BSTR> aMachineExtraDataKeys;
2147 /*
2148 * Get the next key
2149 */
2150 if (FAILED(hrc = virtualBox->GetExtraDataKeys(ComSafeArrayAsOutParam(aGlobalExtraDataKeys))))
2151 AssertMsgFailed(("VirtualBox::GetExtraDataKeys failed with %Rrc\n", hrc));
2152
2153 // remember the no. of global values so we can call the correct method below
2154 size_t cGlobalValues = aGlobalExtraDataKeys.size();
2155
2156 if (FAILED(hrc = pMachine->GetExtraDataKeys(ComSafeArrayAsOutParam(aMachineExtraDataKeys))))
2157 AssertMsgFailed(("IMachine::GetExtraDataKeys failed with %Rrc\n", hrc));
2158
2159 // build a combined list from global keys...
2160 std::list<Utf8Str> llExtraDataKeys;
2161
2162 for (size_t i = 0; i < aGlobalExtraDataKeys.size(); ++i)
2163 llExtraDataKeys.push_back(Utf8Str(aGlobalExtraDataKeys[i]));
2164 // ... and machine keys
2165 for (size_t i = 0; i < aMachineExtraDataKeys.size(); ++i)
2166 llExtraDataKeys.push_back(Utf8Str(aMachineExtraDataKeys[i]));
2167
2168 size_t i2 = 0;
2169 for (std::list<Utf8Str>::const_iterator it = llExtraDataKeys.begin();
2170 it != llExtraDataKeys.end();
2171 ++it, ++i2)
2172 {
2173 const Utf8Str &strKey = *it;
2174
2175 /*
2176 * We only care about keys starting with "VBoxInternal/" (skip "G:" or "M:")
2177 */
2178 if (!strKey.startsWith("VBoxInternal/"))
2179 continue;
2180
2181 const char *pszExtraDataKey = strKey.raw() + sizeof("VBoxInternal/") - 1;
2182
2183 // get the value
2184 Bstr strExtraDataValue;
2185 if (i2 < cGlobalValues)
2186 // this is still one of the global values:
2187 hrc = virtualBox->GetExtraData(Bstr(strKey), strExtraDataValue.asOutParam());
2188 else
2189 hrc = pMachine->GetExtraData(Bstr(strKey), strExtraDataValue.asOutParam());
2190 if (FAILED(hrc))
2191 LogRel(("Warning: Cannot get extra data key %s, rc = %Rrc\n", strKey.raw(), hrc));
2192
2193 /*
2194 * The key will be in the format "Node1/Node2/Value" or simply "Value".
2195 * Split the two and get the node, delete the value and create the node
2196 * if necessary.
2197 */
2198 PCFGMNODE pNode;
2199 const char *pszCFGMValueName = strrchr(pszExtraDataKey, '/');
2200 if (pszCFGMValueName)
2201 {
2202 /* terminate the node and advance to the value (Utf8Str might not
2203 offically like this but wtf) */
2204 *(char*)pszCFGMValueName = '\0';
2205 ++pszCFGMValueName;
2206
2207 /* does the node already exist? */
2208 pNode = CFGMR3GetChild(pRoot, pszExtraDataKey);
2209 if (pNode)
2210 CFGMR3RemoveValue(pNode, pszCFGMValueName);
2211 else
2212 {
2213 /* create the node */
2214 rc = CFGMR3InsertNode(pRoot, pszExtraDataKey, &pNode);
2215 if (RT_FAILURE(rc))
2216 {
2217 AssertLogRelMsgRC(rc, ("failed to insert node '%s'\n", pszExtraDataKey));
2218 continue;
2219 }
2220 Assert(pNode);
2221 }
2222 }
2223 else
2224 {
2225 /* root value (no node path). */
2226 pNode = pRoot;
2227 pszCFGMValueName = pszExtraDataKey;
2228 pszExtraDataKey--;
2229 CFGMR3RemoveValue(pNode, pszCFGMValueName);
2230 }
2231
2232 /*
2233 * Now let's have a look at the value.
2234 * Empty strings means that we should remove the value, which we've
2235 * already done above.
2236 */
2237 Utf8Str strCFGMValueUtf8(strExtraDataValue);
2238 const char *pszCFGMValue = strCFGMValueUtf8.raw();
2239 if ( pszCFGMValue
2240 && *pszCFGMValue)
2241 {
2242 uint64_t u64Value;
2243
2244 /* check for type prefix first. */
2245 if (!strncmp(pszCFGMValue, "string:", sizeof("string:") - 1))
2246 rc = CFGMR3InsertString(pNode, pszCFGMValueName, pszCFGMValue + sizeof("string:") - 1);
2247 else if (!strncmp(pszCFGMValue, "integer:", sizeof("integer:") - 1))
2248 {
2249 rc = RTStrToUInt64Full(pszCFGMValue + sizeof("integer:") - 1, 0, &u64Value);
2250 if (RT_SUCCESS(rc))
2251 rc = CFGMR3InsertInteger(pNode, pszCFGMValueName, u64Value);
2252 }
2253 else if (!strncmp(pszCFGMValue, "bytes:", sizeof("bytes:") - 1))
2254 rc = VERR_NOT_IMPLEMENTED;
2255 /* auto detect type. */
2256 else if (RT_SUCCESS(RTStrToUInt64Full(pszCFGMValue, 0, &u64Value)))
2257 rc = CFGMR3InsertInteger(pNode, pszCFGMValueName, u64Value);
2258 else
2259 rc = CFGMR3InsertString(pNode, pszCFGMValueName, pszCFGMValue);
2260 AssertLogRelMsgRC(rc, ("failed to insert CFGM value '%s' to key '%s'\n", pszCFGMValue, pszExtraDataKey));
2261 }
2262 }
2263
2264#undef H
2265#undef RC_CHECK
2266
2267 /* Register VM state change handler */
2268 int rc2 = VMR3AtStateRegister(pVM, Console::vmstateChangeCallback, pConsole);
2269 AssertRC(rc2);
2270 if (RT_SUCCESS(rc))
2271 rc = rc2;
2272
2273 /* Register VM runtime error handler */
2274 rc2 = VMR3AtRuntimeErrorRegister(pVM, Console::setVMRuntimeErrorCallback, pConsole);
2275 AssertRC(rc2);
2276 if (RT_SUCCESS(rc))
2277 rc = rc2;
2278
2279 LogFlowFunc(("vrc = %Rrc\n", rc));
2280 LogFlowFuncLeave();
2281
2282 return rc;
2283}
2284
2285/**
2286 * Ellipsis to va_list wrapper for calling setVMRuntimeErrorCallback.
2287 */
2288/*static*/ void Console::setVMRuntimeErrorCallbackF(PVM pVM, void *pvConsole, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, ...)
2289{
2290 va_list va;
2291 va_start(va, pszFormat);
2292 setVMRuntimeErrorCallback(pVM, pvConsole, fFlags, pszErrorId, pszFormat, va);
2293 va_end(va);
2294}
2295
2296/* XXX introduce RT format specifier */
2297static uint64_t formatDiskSize(uint64_t u64Size, const char **pszUnit)
2298{
2299 if (u64Size > INT64_C(5000)*_1G)
2300 {
2301 *pszUnit = "TB";
2302 return u64Size / _1T;
2303 }
2304 else if (u64Size > INT64_C(5000)*_1M)
2305 {
2306 *pszUnit = "GB";
2307 return u64Size / _1G;
2308 }
2309 else
2310 {
2311 *pszUnit = "MB";
2312 return u64Size / _1M;
2313 }
2314}
2315
2316int Console::configMediumAttachment(PCFGMNODE pCtlInst,
2317 const char *pcszDevice,
2318 unsigned uInstance,
2319 StorageBus_T enmBus,
2320 bool fUseHostIOCache,
2321 bool fSetupMerge,
2322 unsigned uMergeSource,
2323 unsigned uMergeTarget,
2324 IMediumAttachment *pMediumAtt,
2325 MachineState_T aMachineState,
2326 HRESULT *phrc,
2327 bool fAttachDetach,
2328 bool fForceUnmount,
2329 PVM pVM,
2330 DeviceType_T *paLedDevType)
2331{
2332 int rc = VINF_SUCCESS;
2333 HRESULT hrc;
2334 Bstr bstr;
2335
2336#define RC_CHECK() AssertMsgReturn(RT_SUCCESS(rc), ("rc=%Rrc\n", rc), rc)
2337#define H() AssertMsgReturn(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), VERR_GENERAL_FAILURE)
2338
2339 LONG lDev;
2340 hrc = pMediumAtt->COMGETTER(Device)(&lDev); H();
2341 LONG lPort;
2342 hrc = pMediumAtt->COMGETTER(Port)(&lPort); H();
2343 DeviceType_T lType;
2344 hrc = pMediumAtt->COMGETTER(Type)(&lType); H();
2345
2346 unsigned uLUN;
2347 PCFGMNODE pLunL0 = NULL;
2348 PCFGMNODE pCfg = NULL;
2349 hrc = Console::convertBusPortDeviceToLun(enmBus, lPort, lDev, uLUN); H();
2350
2351 /* First check if the LUN already exists. */
2352 pLunL0 = CFGMR3GetChildF(pCtlInst, "LUN#%u", uLUN);
2353 if (pLunL0)
2354 {
2355 if (fAttachDetach)
2356 {
2357 if (lType != DeviceType_HardDisk)
2358 {
2359 /* Unmount existing media only for floppy and DVD drives. */
2360 PPDMIBASE pBase;
2361 rc = PDMR3QueryLun(pVM, pcszDevice, uInstance, uLUN, &pBase);
2362 if (RT_FAILURE(rc))
2363 {
2364 if (rc == VERR_PDM_LUN_NOT_FOUND || rc == VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN)
2365 rc = VINF_SUCCESS;
2366 AssertRC(rc);
2367 }
2368 else
2369 {
2370 PPDMIMOUNT pIMount = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMOUNT);
2371 AssertReturn(pIMount, VERR_INVALID_POINTER);
2372
2373 /* Unmount the media. */
2374 rc = pIMount->pfnUnmount(pIMount, fForceUnmount);
2375 if (rc == VERR_PDM_MEDIA_NOT_MOUNTED)
2376 rc = VINF_SUCCESS;
2377 }
2378 }
2379
2380 rc = PDMR3DeviceDetach(pVM, pcszDevice, 0, uLUN, PDM_TACH_FLAGS_NOT_HOT_PLUG);
2381 if (rc == VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN)
2382 rc = VINF_SUCCESS;
2383 RC_CHECK();
2384
2385 CFGMR3RemoveNode(pLunL0);
2386 }
2387 else
2388 AssertFailedReturn(VERR_INTERNAL_ERROR);
2389 }
2390
2391 rc = CFGMR3InsertNodeF(pCtlInst, &pLunL0, "LUN#%u", uLUN); RC_CHECK();
2392
2393 /* SCSI has a another driver between device and block. */
2394 if (enmBus == StorageBus_SCSI || enmBus == StorageBus_SAS)
2395 {
2396 rc = CFGMR3InsertString(pLunL0, "Driver", "SCSI"); RC_CHECK();
2397 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2398
2399 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK();
2400 }
2401
2402 ComPtr<IMedium> pMedium;
2403 hrc = pMediumAtt->COMGETTER(Medium)(pMedium.asOutParam()); H();
2404
2405 if (lType == DeviceType_HardDisk)
2406 {
2407 /*
2408 * Some sanity checks.
2409 */
2410 ComPtr<IMediumFormat> pMediumFormat;
2411 hrc = pMedium->COMGETTER(MediumFormat)(pMediumFormat.asOutParam()); H();
2412 ULONG uCaps;
2413 hrc = pMediumFormat->COMGETTER(Capabilities)(&uCaps); H();
2414 if (uCaps & MediumFormatCapabilities_File)
2415 {
2416 Bstr strFile;
2417 hrc = pMedium->COMGETTER(Location)(strFile.asOutParam()); H();
2418 Utf8Str utfFile = Utf8Str(strFile);
2419 Bstr strSnap;
2420 ComPtr<IMachine> pMachine = machine();
2421 hrc = pMachine->COMGETTER(SnapshotFolder)(strSnap.asOutParam()); H();
2422 Utf8Str utfSnap = Utf8Str(strSnap);
2423 uint32_t typeFile, typeSnap;
2424 rc = RTFsQueryType(utfFile.c_str(), &typeFile);
2425 if (RT_SUCCESS(rc))
2426 rc = RTFsQueryType(utfSnap.c_str(), &typeSnap);
2427 ULONG64 u64Size;
2428 hrc = pMedium->COMGETTER(LogicalSize)(&u64Size); H();
2429 u64Size *= _1M;
2430 if (RT_SUCCESS(rc))
2431 {
2432#ifdef RT_OS_WINDOWS
2433 if ( typeFile == RTFS_FS_TYPE_FAT
2434 && u64Size >= _4G)
2435 {
2436 const char *pszUnit;
2437 uint64_t u64Print = formatDiskSize(u64Size, &pszUnit);
2438 setVMRuntimeErrorCallbackF(pVM, this, 0,
2439 "FatPartitionDetected",
2440 N_("The medium '%ls' has a logical size of %RU64%s "
2441 "but the file system the medium is located on seems "
2442 "to be FAT(32) which cannot handle files bigger than 4GB.\n"
2443 "We strongly recommend to put all your virtual disk images and "
2444 "the snapshot folder onto an NTFS partition"),
2445 strFile.raw(), u64Print, pszUnit);
2446 }
2447#else /* !RT_OS_WINDOWS */
2448 if ( typeFile == RTFS_FS_TYPE_FAT
2449 || typeFile == RTFS_FS_TYPE_EXT
2450 || typeFile == RTFS_FS_TYPE_EXT2
2451 || typeFile == RTFS_FS_TYPE_EXT3
2452 || typeFile == RTFS_FS_TYPE_EXT4)
2453 {
2454 RTFILE file;
2455 rc = RTFileOpen(&file, utfFile.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
2456 if (RT_SUCCESS(rc))
2457 {
2458 RTFOFF maxSize;
2459 /* Careful: This function will work only on selected local file systems! */
2460 rc = RTFileGetMaxSizeEx(file, &maxSize);
2461 RTFileClose(file);
2462 if ( RT_SUCCESS(rc)
2463 && maxSize > 0
2464 && u64Size > (ULONG64)maxSize)
2465 {
2466 const char *pszUnitSiz;
2467 const char *pszUnitMax;
2468 uint64_t u64PrintSiz = formatDiskSize(u64Size, &pszUnitSiz);
2469 uint64_t u64PrintMax = formatDiskSize(maxSize, &pszUnitMax);
2470 setVMRuntimeErrorCallbackF(pVM, this, 0,
2471 "FatPartitionDetected", /* <= not exact but ... */
2472 N_("The medium '%ls' has a logical size of %RU64%s "
2473 "but the file system the medium is located on can "
2474 "only handle files up to %RU64%s in theory.\n"
2475 "We strongly recommend to put all your virtual disk "
2476 "images and the snapshot folder onto a proper "
2477 "file system (e.g. ext3) with a sufficient size"),
2478 strFile.raw(), u64PrintSiz, pszUnitSiz, u64PrintMax, pszUnitMax);
2479 }
2480 }
2481 }
2482#endif /* !RT_OS_WINDOWS */
2483
2484 /*
2485 * Snapshot folder:
2486 * Here we test only for a FAT partition as we had to create a dummy file otherwise
2487 */
2488 if ( typeSnap == RTFS_FS_TYPE_FAT
2489 && u64Size >= _4G
2490 && !mfSnapshotFolderWarningShown)
2491 {
2492 const char *pszUnit;
2493 uint64_t u64Print = formatDiskSize(u64Size, &pszUnit);
2494 setVMRuntimeErrorCallbackF(pVM, this, 0,
2495 "FatPartitionDetected",
2496#ifdef RT_OS_WINDOWS
2497 N_("The snapshot folder of this VM '%ls' seems to be located on "
2498 "a FAT(32) file system. The logical size of the medium '%ls' "
2499 "(%RU64%s) is bigger than the maximum file size this file "
2500 "system can handle (4GB).\n"
2501 "We strongly recommend to put all your virtual disk images and "
2502 "the snapshot folder onto an NTFS partition"),
2503#else
2504 N_("The snapshot folder of this VM '%ls' seems to be located on "
2505 "a FAT(32) file system. The logical size of the medium '%ls' "
2506 "(%RU64%s) is bigger than the maximum file size this file "
2507 "system can handle (4GB).\n"
2508 "We strongly recommend to put all your virtual disk images and "
2509 "the snapshot folder onto a proper file system (e.g. ext3)"),
2510#endif
2511 strSnap.raw(), strFile.raw(), u64Print, pszUnit);
2512 /* Show this particular warning only once */
2513 mfSnapshotFolderWarningShown = true;
2514 }
2515
2516#ifdef RT_OS_LINUX
2517 /*
2518 * Ext4 bug: Check if the host I/O cache is disabled and the disk image is located
2519 * on an ext4 partition. Later we have to check the Linux kernel version!
2520 */
2521 if ( (uCaps & MediumFormatCapabilities_Asynchronous)
2522 && !fUseHostIOCache
2523 && ( typeFile == RTFS_FS_TYPE_EXT4
2524 || typeSnap == RTFS_FS_TYPE_EXT4))
2525 {
2526 setVMRuntimeErrorCallbackF(pVM, this, 0,
2527 "Ext4PartitionDetected",
2528 N_("The host I/O cache for at least one controller is disabled "
2529 "but the medium '%ls' or the snapshot folder for this VM "
2530 "is located on an ext4 partition. There is a known Linux "
2531 "kernel bug which can lead to the corruption of the virtual "
2532 "disk image under these conditions.\n"
2533 "Either enable the host I/O cache permanently in the VM "
2534 "settings or put the disk image and the snapshot folder "
2535 "onto a different file system.\n"
2536 "The host I/O cache will now be enabled for this medium"),
2537 strFile.raw());
2538 fUseHostIOCache = true;
2539 }
2540#endif
2541 }
2542 }
2543 }
2544
2545 BOOL fPassthrough;
2546 hrc = pMediumAtt->COMGETTER(Passthrough)(&fPassthrough); H();
2547 rc = configMedium(pLunL0,
2548 !!fPassthrough,
2549 lType,
2550 fUseHostIOCache,
2551 fSetupMerge,
2552 uMergeSource,
2553 uMergeTarget,
2554 pMedium,
2555 aMachineState,
2556 phrc); RC_CHECK();
2557
2558 if (fAttachDetach)
2559 {
2560 /* Attach the new driver. */
2561 rc = PDMR3DeviceAttach(pVM, pcszDevice, 0, uLUN,
2562 PDM_TACH_FLAGS_NOT_HOT_PLUG, NULL /*ppBase*/); RC_CHECK();
2563
2564 /* There is no need to handle removable medium mounting, as we
2565 * unconditionally replace everthing including the block driver level.
2566 * This means the new medium will be picked up automatically. */
2567 }
2568
2569 if (paLedDevType)
2570 paLedDevType[uLUN] = lType;
2571
2572#undef H
2573#undef RC_CHECK
2574
2575 return VINF_SUCCESS;;
2576}
2577
2578int Console::configMedium(PCFGMNODE pLunL0,
2579 bool fPassthrough,
2580 DeviceType_T enmType,
2581 bool fUseHostIOCache,
2582 bool fSetupMerge,
2583 unsigned uMergeSource,
2584 unsigned uMergeTarget,
2585 IMedium *pMedium,
2586 MachineState_T aMachineState,
2587 HRESULT *phrc)
2588{
2589 int rc = VINF_SUCCESS;
2590 HRESULT hrc;
2591 Bstr bstr;
2592
2593#define RC_CHECK() AssertMsgReturn(RT_SUCCESS(rc), ("rc=%Rrc\n", rc), rc)
2594#define H() AssertMsgReturnStmt(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), if (phrc) *phrc = hrc, VERR_GENERAL_FAILURE)
2595
2596 PCFGMNODE pLunL1 = NULL;
2597 PCFGMNODE pCfg = NULL;
2598
2599 BOOL fHostDrive = FALSE;
2600 if (pMedium)
2601 {
2602 hrc = pMedium->COMGETTER(HostDrive)(&fHostDrive); H();
2603 }
2604
2605 if (fHostDrive)
2606 {
2607 Assert(pMedium);
2608 if (enmType == DeviceType_DVD)
2609 {
2610 rc = CFGMR3InsertString(pLunL0, "Driver", "HostDVD"); RC_CHECK();
2611 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2612
2613 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
2614 rc = CFGMR3InsertStringW(pCfg, "Path", bstr.raw()); RC_CHECK();
2615
2616 rc = CFGMR3InsertInteger(pCfg, "Passthrough", fPassthrough); RC_CHECK();
2617 }
2618 else if (enmType == DeviceType_Floppy)
2619 {
2620 rc = CFGMR3InsertString(pLunL0, "Driver", "HostFloppy"); RC_CHECK();
2621 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2622
2623 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
2624 rc = CFGMR3InsertStringW(pCfg, "Path", bstr.raw()); RC_CHECK();
2625 }
2626 }
2627 else
2628 {
2629 rc = CFGMR3InsertString(pLunL0, "Driver", "Block"); RC_CHECK();
2630 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2631 switch (enmType)
2632 {
2633 case DeviceType_DVD:
2634 rc = CFGMR3InsertString(pCfg, "Type", "DVD"); RC_CHECK();
2635 rc = CFGMR3InsertInteger(pCfg, "Mountable", 1); RC_CHECK();
2636 break;
2637 case DeviceType_Floppy:
2638 rc = CFGMR3InsertString(pCfg, "Type", "Floppy 1.44"); RC_CHECK();
2639 rc = CFGMR3InsertInteger(pCfg, "Mountable", 1); RC_CHECK();
2640 break;
2641 case DeviceType_HardDisk:
2642 default:
2643 rc = CFGMR3InsertString(pCfg, "Type", "HardDisk"); RC_CHECK();
2644 rc = CFGMR3InsertInteger(pCfg, "Mountable", 0); RC_CHECK();
2645 }
2646
2647 if ( pMedium
2648 && ( enmType == DeviceType_DVD
2649 || enmType == DeviceType_Floppy
2650 ))
2651 {
2652 // if this medium represents an ISO image and this image is inaccessible,
2653 // the ignore it instead of causing a failure; this can happen when we
2654 // restore a VM state and the ISO has disappeared, e.g. because the Guest
2655 // Additions were mounted and the user upgraded VirtualBox. Previously
2656 // we failed on startup, but that's not good because the only way out then
2657 // would be to discard the VM state...
2658 MediumState_T mediumState;
2659 rc = pMedium->RefreshState(&mediumState);
2660 RC_CHECK();
2661 if (mediumState == MediumState_Inaccessible)
2662 {
2663 Bstr loc;
2664 rc = pMedium->COMGETTER(Location)(loc.asOutParam());
2665 if (FAILED(rc)) return rc;
2666
2667 setVMRuntimeErrorCallbackF(mpVM,
2668 this,
2669 0,
2670 "DvdOrFloppyImageInaccessible",
2671 "The image file '%ls' is inaccessible and is being ignored. Please select a different image file for the virtual %s drive.",
2672 loc.raw(),
2673 (enmType == DeviceType_DVD) ? "DVD" : "floppy");
2674 pMedium = NULL;
2675 }
2676 }
2677
2678 if (pMedium)
2679 {
2680 /* Start with length of parent chain, as the list is reversed */
2681 unsigned uImage = 0;
2682 IMedium *pTmp = pMedium;
2683 while (pTmp)
2684 {
2685 uImage++;
2686 hrc = pTmp->COMGETTER(Parent)(&pTmp); H();
2687 }
2688 /* Index of last image */
2689 uImage--;
2690
2691#if 0 /* Enable for I/O debugging */
2692 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK();
2693 rc = CFGMR3InsertString(pLunL0, "Driver", "DiskIntegrity"); RC_CHECK();
2694 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2695 rc = CFGMR3InsertInteger(pCfg, "CheckConsistency", 0); RC_CHECK();
2696 rc = CFGMR3InsertInteger(pCfg, "CheckDoubleCompletions", 1); RC_CHECK();
2697#endif
2698
2699 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); RC_CHECK();
2700 rc = CFGMR3InsertString(pLunL1, "Driver", "VD"); RC_CHECK();
2701 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); RC_CHECK();
2702
2703 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
2704 rc = CFGMR3InsertStringW(pCfg, "Path", bstr.raw()); RC_CHECK();
2705
2706 hrc = pMedium->COMGETTER(Format)(bstr.asOutParam()); H();
2707 rc = CFGMR3InsertStringW(pCfg, "Format", bstr.raw()); RC_CHECK();
2708
2709 /* DVDs are always readonly */
2710 if (enmType == DeviceType_DVD)
2711 {
2712 rc = CFGMR3InsertInteger(pCfg, "ReadOnly", 1); RC_CHECK();
2713 }
2714
2715 /* Start without exclusive write access to the images. */
2716 /** @todo Live Migration: I don't quite like this, we risk screwing up when
2717 * we're resuming the VM if some 3rd dude have any of the VDIs open
2718 * with write sharing denied. However, if the two VMs are sharing a
2719 * image it really is necessary....
2720 *
2721 * So, on the "lock-media" command, the target teleporter should also
2722 * make DrvVD undo TempReadOnly. It gets interesting if we fail after
2723 * that. Grumble. */
2724 else if (aMachineState == MachineState_TeleportingIn)
2725 {
2726 rc = CFGMR3InsertInteger(pCfg, "TempReadOnly", 1); RC_CHECK();
2727 }
2728
2729 if (!fUseHostIOCache)
2730 {
2731 rc = CFGMR3InsertInteger(pCfg, "UseNewIo", 1); RC_CHECK();
2732 }
2733
2734 if (fSetupMerge)
2735 {
2736 rc = CFGMR3InsertInteger(pCfg, "SetupMerge", 1); RC_CHECK();
2737 if (uImage == uMergeSource)
2738 {
2739 rc = CFGMR3InsertInteger(pCfg, "MergeSource", 1); RC_CHECK();
2740 }
2741 else if (uImage == uMergeTarget)
2742 {
2743 rc = CFGMR3InsertInteger(pCfg, "MergeTarget", 1); RC_CHECK();
2744 }
2745 }
2746
2747 /* Pass all custom parameters. */
2748 bool fHostIP = true;
2749 SafeArray<BSTR> names;
2750 SafeArray<BSTR> values;
2751 hrc = pMedium->GetProperties(NULL,
2752 ComSafeArrayAsOutParam(names),
2753 ComSafeArrayAsOutParam(values)); H();
2754
2755 if (names.size() != 0)
2756 {
2757 PCFGMNODE pVDC;
2758 rc = CFGMR3InsertNode(pCfg, "VDConfig", &pVDC); RC_CHECK();
2759 for (size_t ii = 0; ii < names.size(); ++ii)
2760 {
2761 if (values[ii] && *values[ii])
2762 {
2763 Utf8Str name = names[ii];
2764 Utf8Str value = values[ii];
2765 rc = CFGMR3InsertString(pVDC, name.c_str(), value.c_str()); RC_CHECK();
2766 if ( name.compare("HostIPStack") == 0
2767 && value.compare("0") == 0)
2768 fHostIP = false;
2769 }
2770 }
2771 }
2772
2773 /* Create an inversed list of parents. */
2774 uImage--;
2775 IMedium *pParentMedium = pMedium;
2776 for (PCFGMNODE pParent = pCfg;; uImage--)
2777 {
2778 hrc = pParentMedium->COMGETTER(Parent)(&pMedium); H();
2779 if (!pMedium)
2780 break;
2781
2782 PCFGMNODE pCur;
2783 rc = CFGMR3InsertNode(pParent, "Parent", &pCur); RC_CHECK();
2784 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
2785 rc = CFGMR3InsertStringW(pCur, "Path", bstr.raw()); RC_CHECK();
2786
2787 hrc = pMedium->COMGETTER(Format)(bstr.asOutParam()); H();
2788 rc = CFGMR3InsertStringW(pCur, "Format", bstr.raw()); RC_CHECK();
2789
2790 if (fSetupMerge)
2791 {
2792 if (uImage == uMergeSource)
2793 {
2794 rc = CFGMR3InsertInteger(pCur, "MergeSource", 1); RC_CHECK();
2795 }
2796 else if (uImage == uMergeTarget)
2797 {
2798 rc = CFGMR3InsertInteger(pCur, "MergeTarget", 1); RC_CHECK();
2799 }
2800 }
2801
2802 /* Pass all custom parameters. */
2803 SafeArray<BSTR> aNames;
2804 SafeArray<BSTR> aValues;
2805 hrc = pMedium->GetProperties(NULL,
2806 ComSafeArrayAsOutParam(aNames),
2807 ComSafeArrayAsOutParam(aValues)); H();
2808
2809 if (aNames.size() != 0)
2810 {
2811 PCFGMNODE pVDC;
2812 rc = CFGMR3InsertNode(pCur, "VDConfig", &pVDC); RC_CHECK();
2813 for (size_t ii = 0; ii < aNames.size(); ++ii)
2814 {
2815 if (aValues[ii] && *aValues[ii])
2816 {
2817 Utf8Str name = aNames[ii];
2818 Utf8Str value = aValues[ii];
2819 rc = CFGMR3InsertString(pVDC, name.c_str(), value.c_str()); RC_CHECK();
2820 if ( name.compare("HostIPStack") == 0
2821 && value.compare("0") == 0)
2822 fHostIP = false;
2823 }
2824 }
2825 }
2826
2827 /* Custom code: put marker to not use host IP stack to driver
2828 * configuration node. Simplifies life of DrvVD a bit. */
2829 if (!fHostIP)
2830 {
2831 rc = CFGMR3InsertInteger(pCfg, "HostIPStack", 0); RC_CHECK();
2832 }
2833
2834 /* next */
2835 pParent = pCur;
2836 pParentMedium = pMedium;
2837 }
2838 }
2839 }
2840
2841#undef H
2842#undef RC_CHECK
2843
2844 return VINF_SUCCESS;
2845}
2846
2847/**
2848 * Construct the Network configuration tree
2849 *
2850 * @returns VBox status code.
2851 *
2852 * @param pszDevice The PDM device name.
2853 * @param uInstance The PDM device instance.
2854 * @param uLun The PDM LUN number of the drive.
2855 * @param aNetworkAdapter The network adapter whose attachment needs to be changed
2856 * @param pCfg Configuration node for the device
2857 * @param pLunL0 To store the pointer to the LUN#0.
2858 * @param pInst The instance CFGM node
2859 * @param fAttachDetach To determine if the network attachment should
2860 * be attached/detached after/before
2861 * configuration.
2862 *
2863 * @note Locks this object for writing.
2864 */
2865int Console::configNetwork(const char *pszDevice, unsigned uInstance,
2866 unsigned uLun, INetworkAdapter *aNetworkAdapter,
2867 PCFGMNODE pCfg, PCFGMNODE pLunL0, PCFGMNODE pInst,
2868 bool fAttachDetach)
2869{
2870 AutoCaller autoCaller(this);
2871 AssertComRCReturn(autoCaller.rc(), VERR_ACCESS_DENIED);
2872
2873 int rc = VINF_SUCCESS;
2874 HRESULT hrc;
2875 Bstr bstr;
2876
2877#define RC_CHECK() AssertMsgReturn(RT_SUCCESS(rc), ("rc=%Rrc\n", rc), rc)
2878#define H() AssertMsgReturn(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), VERR_GENERAL_FAILURE)
2879
2880 /*
2881 * Locking the object before doing VMR3* calls is quite safe here, since
2882 * we're on EMT. Write lock is necessary because we indirectly modify the
2883 * meAttachmentType member.
2884 */
2885 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2886
2887 PVM pVM = mpVM;
2888
2889 ComPtr<IMachine> pMachine = machine();
2890
2891 ComPtr<IVirtualBox> virtualBox;
2892 hrc = pMachine->COMGETTER(Parent)(virtualBox.asOutParam());
2893 H();
2894
2895 ComPtr<IHost> host;
2896 hrc = virtualBox->COMGETTER(Host)(host.asOutParam());
2897 H();
2898
2899 BOOL fSniffer;
2900 hrc = aNetworkAdapter->COMGETTER(TraceEnabled)(&fSniffer);
2901 H();
2902
2903 if (fAttachDetach && fSniffer)
2904 {
2905 const char *pszNetDriver = "IntNet";
2906 if (meAttachmentType[uInstance] == NetworkAttachmentType_NAT)
2907 pszNetDriver = "NAT";
2908#if !defined(VBOX_WITH_NETFLT) && defined(RT_OS_LINUX)
2909 if (meAttachmentType[uInstance] == NetworkAttachmentType_Bridged)
2910 pszNetDriver = "HostInterface";
2911#endif
2912
2913 rc = PDMR3DriverDetach(pVM, pszDevice, uInstance, uLun, pszNetDriver, 0, 0 /*fFlags*/);
2914 if (rc == VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN)
2915 rc = VINF_SUCCESS;
2916 AssertLogRelRCReturn(rc, rc);
2917
2918 pLunL0 = CFGMR3GetChildF(pInst, "LUN#%u", uLun);
2919 PCFGMNODE pLunAD = CFGMR3GetChildF(pLunL0, "AttachedDriver");
2920 if (pLunAD)
2921 {
2922 CFGMR3RemoveNode(pLunAD);
2923 }
2924 else
2925 {
2926 CFGMR3RemoveNode(pLunL0);
2927 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
2928 rc = CFGMR3InsertString(pLunL0, "Driver", "NetSniffer"); RC_CHECK();
2929 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2930 hrc = aNetworkAdapter->COMGETTER(TraceFile)(bstr.asOutParam()); H();
2931 if (!bstr.isEmpty()) /* check convention for indicating default file. */
2932 {
2933 rc = CFGMR3InsertStringW(pCfg, "File", bstr.raw()); RC_CHECK();
2934 }
2935 }
2936 }
2937 else if (fAttachDetach && !fSniffer)
2938 {
2939 rc = PDMR3DeviceDetach(pVM, pszDevice, uInstance, uLun, 0 /*fFlags*/);
2940 if (rc == VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN)
2941 rc = VINF_SUCCESS;
2942 AssertLogRelRCReturn(rc, rc);
2943
2944 /* nuke anything which might have been left behind. */
2945 CFGMR3RemoveNode(CFGMR3GetChildF(pInst, "LUN#%u", uLun));
2946 }
2947 else if (!fAttachDetach && fSniffer)
2948 {
2949 /* insert the sniffer filter driver. */
2950 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
2951 rc = CFGMR3InsertString(pLunL0, "Driver", "NetSniffer"); RC_CHECK();
2952 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2953 hrc = aNetworkAdapter->COMGETTER(TraceFile)(bstr.asOutParam()); H();
2954 if (!bstr.isEmpty()) /* check convention for indicating default file. */
2955 {
2956 rc = CFGMR3InsertStringW(pCfg, "File", bstr.raw()); RC_CHECK();
2957 }
2958 }
2959
2960 Bstr networkName, trunkName, trunkType;
2961 NetworkAttachmentType_T eAttachmentType;
2962 hrc = aNetworkAdapter->COMGETTER(AttachmentType)(&eAttachmentType); H();
2963 switch (eAttachmentType)
2964 {
2965 case NetworkAttachmentType_Null:
2966 break;
2967
2968 case NetworkAttachmentType_NAT:
2969 {
2970 ComPtr<INATEngine> natDriver;
2971 hrc = aNetworkAdapter->COMGETTER(NatDriver)(natDriver.asOutParam()); H();
2972 if (fSniffer)
2973 {
2974 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK();
2975 }
2976 else
2977 {
2978 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
2979 }
2980 rc = CFGMR3InsertString(pLunL0, "Driver", "NAT"); RC_CHECK();
2981 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
2982
2983 /* Configure TFTP prefix and boot filename. */
2984 hrc = virtualBox->COMGETTER(HomeFolder)(bstr.asOutParam()); H();
2985 if (!bstr.isEmpty())
2986 {
2987 rc = CFGMR3InsertStringF(pCfg, "TFTPPrefix", "%ls%c%s", bstr.raw(), RTPATH_DELIMITER, "TFTP"); RC_CHECK();
2988 }
2989 hrc = pMachine->COMGETTER(Name)(bstr.asOutParam()); H();
2990 rc = CFGMR3InsertStringF(pCfg, "BootFile", "%ls.pxe", bstr.raw()); RC_CHECK();
2991
2992 hrc = natDriver->COMGETTER(Network)(bstr.asOutParam()); H();
2993 if (!bstr.isEmpty())
2994 {
2995 rc = CFGMR3InsertStringW(pCfg, "Network", bstr.raw()); RC_CHECK();
2996 }
2997 else
2998 {
2999 ULONG uSlot;
3000 hrc = aNetworkAdapter->COMGETTER(Slot)(&uSlot); H();
3001 rc = CFGMR3InsertStringF(pCfg, "Network", "10.0.%d.0/24", uSlot+2); RC_CHECK();
3002 }
3003 hrc = natDriver->COMGETTER(HostIP)(bstr.asOutParam()); H();
3004 if (!bstr.isEmpty())
3005 {
3006 rc = CFGMR3InsertStringW(pCfg, "BindIP", bstr.raw()); RC_CHECK();
3007 }
3008 ULONG mtu = 0;
3009 ULONG sockSnd = 0;
3010 ULONG sockRcv = 0;
3011 ULONG tcpSnd = 0;
3012 ULONG tcpRcv = 0;
3013 hrc = natDriver->GetNetworkSettings(&mtu, &sockSnd, &sockRcv, &tcpSnd, &tcpRcv); H();
3014 if (mtu)
3015 {
3016 rc = CFGMR3InsertInteger(pCfg, "SlirpMTU", mtu); RC_CHECK();
3017 }
3018 if (sockRcv)
3019 {
3020 rc = CFGMR3InsertInteger(pCfg, "SockRcv", sockRcv); RC_CHECK();
3021 }
3022 if (sockSnd)
3023 {
3024 rc = CFGMR3InsertInteger(pCfg, "SockSnd", sockSnd); RC_CHECK();
3025 }
3026 if (tcpRcv)
3027 {
3028 rc = CFGMR3InsertInteger(pCfg, "TcpRcv", tcpRcv); RC_CHECK();
3029 }
3030 if (tcpSnd)
3031 {
3032 rc = CFGMR3InsertInteger(pCfg, "TcpSnd", tcpSnd); RC_CHECK();
3033 }
3034 hrc = natDriver->COMGETTER(TftpPrefix)(bstr.asOutParam()); H();
3035 if (!bstr.isEmpty())
3036 {
3037 rc = CFGMR3RemoveValue(pCfg, "TFTPPrefix"); RC_CHECK();
3038 rc = CFGMR3InsertStringW(pCfg, "TFTPPrefix", bstr); RC_CHECK();
3039 }
3040 hrc = natDriver->COMGETTER(TftpBootFile)(bstr.asOutParam()); H();
3041 if (!bstr.isEmpty())
3042 {
3043 rc = CFGMR3RemoveValue(pCfg, "BootFile"); RC_CHECK();
3044 rc = CFGMR3InsertStringW(pCfg, "BootFile", bstr); RC_CHECK();
3045 }
3046 hrc = natDriver->COMGETTER(TftpNextServer)(bstr.asOutParam()); H();
3047 if (!bstr.isEmpty())
3048 {
3049 rc = CFGMR3InsertStringW(pCfg, "NextServer", bstr); RC_CHECK();
3050 }
3051 BOOL fDnsFlag;
3052 hrc = natDriver->COMGETTER(DnsPassDomain)(&fDnsFlag); H();
3053 rc = CFGMR3InsertInteger(pCfg, "PassDomain", fDnsFlag); RC_CHECK();
3054 hrc = natDriver->COMGETTER(DnsProxy)(&fDnsFlag); H();
3055 rc = CFGMR3InsertInteger(pCfg, "DNSProxy", fDnsFlag); RC_CHECK();
3056 hrc = natDriver->COMGETTER(DnsUseHostResolver)(&fDnsFlag); H();
3057 rc = CFGMR3InsertInteger(pCfg, "UseHostResolver", fDnsFlag); RC_CHECK();
3058
3059 ULONG aliasMode;
3060 hrc = natDriver->COMGETTER(AliasMode)(&aliasMode); H();
3061 rc = CFGMR3InsertInteger(pCfg, "AliasMode", aliasMode); RC_CHECK();
3062
3063 /* port-forwarding */
3064 SafeArray<BSTR> pfs;
3065 hrc = natDriver->COMGETTER(Redirects)(ComSafeArrayAsOutParam(pfs)); H();
3066 PCFGMNODE pPF = NULL; /* /Devices/Dev/.../Config/PF#0/ */
3067 for (unsigned int i = 0; i < pfs.size(); ++i)
3068 {
3069 uint16_t port = 0;
3070 BSTR r = pfs[i];
3071 Utf8Str utf = Utf8Str(r);
3072 Utf8Str strName;
3073 Utf8Str strProto;
3074 Utf8Str strHostPort;
3075 Utf8Str strHostIP;
3076 Utf8Str strGuestPort;
3077 Utf8Str strGuestIP;
3078 size_t pos, ppos;
3079 pos = ppos = 0;
3080#define ITERATE_TO_NEXT_TERM(res, str, pos, ppos) \
3081 do { \
3082 pos = str.find(",", ppos); \
3083 if (pos == Utf8Str::npos) \
3084 { \
3085 Log(( #res " extracting from %s is failed\n", str.raw())); \
3086 continue; \
3087 } \
3088 res = str.substr(ppos, pos - ppos); \
3089 Log2((#res " %s pos:%d, ppos:%d\n", res.raw(), pos, ppos)); \
3090 ppos = pos + 1; \
3091 } while (0)
3092 ITERATE_TO_NEXT_TERM(strName, utf, pos, ppos);
3093 ITERATE_TO_NEXT_TERM(strProto, utf, pos, ppos);
3094 ITERATE_TO_NEXT_TERM(strHostIP, utf, pos, ppos);
3095 ITERATE_TO_NEXT_TERM(strHostPort, utf, pos, ppos);
3096 ITERATE_TO_NEXT_TERM(strGuestIP, utf, pos, ppos);
3097 strGuestPort = utf.substr(ppos, utf.length() - ppos);
3098#undef ITERATE_TO_NEXT_TERM
3099
3100 uint32_t proto = strProto.toUInt32();
3101 bool fValid = true;
3102 switch (proto)
3103 {
3104 case NATProtocol_UDP:
3105 strProto = "UDP";
3106 break;
3107 case NATProtocol_TCP:
3108 strProto = "TCP";
3109 break;
3110 default:
3111 fValid = false;
3112 }
3113 /* continue with next rule if no valid proto was passed */
3114 if (!fValid)
3115 continue;
3116
3117 rc = CFGMR3InsertNode(pCfg, strName.raw(), &pPF); RC_CHECK();
3118 rc = CFGMR3InsertString(pPF, "Protocol", strProto.raw()); RC_CHECK();
3119
3120 if (!strHostIP.isEmpty())
3121 {
3122 rc = CFGMR3InsertString(pPF, "BindIP", strHostIP.raw()); RC_CHECK();
3123 }
3124
3125 if (!strGuestIP.isEmpty())
3126 {
3127 rc = CFGMR3InsertString(pPF, "GuestIP", strGuestIP.raw()); RC_CHECK();
3128 }
3129
3130 port = RTStrToUInt16(strHostPort.raw());
3131 if (port)
3132 {
3133 rc = CFGMR3InsertInteger(pPF, "HostPort", port); RC_CHECK();
3134 }
3135
3136 port = RTStrToUInt16(strGuestPort.raw());
3137 if (port)
3138 {
3139 rc = CFGMR3InsertInteger(pPF, "GuestPort", port); RC_CHECK();
3140 }
3141 }
3142 break;
3143 }
3144
3145 case NetworkAttachmentType_Bridged:
3146 {
3147#if (defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)) && !defined(VBOX_WITH_NETFLT)
3148 hrc = attachToTapInterface(aNetworkAdapter);
3149 if (FAILED(hrc))
3150 {
3151 switch (hrc)
3152 {
3153 case VERR_ACCESS_DENIED:
3154 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
3155 "Failed to open '/dev/net/tun' for read/write access. Please check the "
3156 "permissions of that node. Either run 'chmod 0666 /dev/net/tun' or "
3157 "change the group of that node and make yourself a member of that group. Make "
3158 "sure that these changes are permanent, especially if you are "
3159 "using udev"));
3160 default:
3161 AssertMsgFailed(("Could not attach to host interface! Bad!\n"));
3162 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
3163 "Failed to initialize Host Interface Networking"));
3164 }
3165 }
3166
3167 Assert((int)maTapFD[uInstance] >= 0);
3168 if ((int)maTapFD[uInstance] >= 0)
3169 {
3170 if (fSniffer)
3171 {
3172 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK();
3173 }
3174 else
3175 {
3176 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
3177 }
3178 rc = CFGMR3InsertString(pLunL0, "Driver", "HostInterface"); RC_CHECK();
3179 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
3180 rc = CFGMR3InsertInteger(pCfg, "FileHandle", maTapFD[uInstance]); RC_CHECK();
3181 }
3182
3183#elif defined(VBOX_WITH_NETFLT)
3184 /*
3185 * This is the new VBoxNetFlt+IntNet stuff.
3186 */
3187 if (fSniffer)
3188 {
3189 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); RC_CHECK();
3190 }
3191 else
3192 {
3193 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
3194 }
3195
3196 Bstr HifName;
3197 hrc = aNetworkAdapter->COMGETTER(HostInterface)(HifName.asOutParam());
3198 if (FAILED(hrc))
3199 {
3200 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(HostInterface) failed, hrc (0x%x)", hrc));
3201 H();
3202 }
3203
3204 Utf8Str HifNameUtf8(HifName);
3205 const char *pszHifName = HifNameUtf8.raw();
3206
3207# if defined(RT_OS_DARWIN)
3208 /* The name is on the form 'ifX: long name', chop it off at the colon. */
3209 char szTrunk[8];
3210 strncpy(szTrunk, pszHifName, sizeof(szTrunk));
3211 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk));
3212 if (!pszColon)
3213 {
3214 /*
3215 * Dynamic changing of attachment causes an attempt to configure
3216 * network with invalid host adapter (as it is must be changed before
3217 * the attachment), calling Detach here will cause a deadlock.
3218 * See #4750.
3219 * hrc = aNetworkAdapter->Detach(); H();
3220 */
3221 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
3222 N_("Malformed host interface networking name '%ls'"),
3223 HifName.raw());
3224 }
3225 *pszColon = '\0';
3226 const char *pszTrunk = szTrunk;
3227
3228# elif defined(RT_OS_SOLARIS)
3229 /* The name is on the form format 'ifX[:1] - long name, chop it off at space. */
3230 char szTrunk[256];
3231 strlcpy(szTrunk, pszHifName, sizeof(szTrunk));
3232 char *pszSpace = (char *)memchr(szTrunk, ' ', sizeof(szTrunk));
3233
3234 /*
3235 * Currently don't bother about malformed names here for the sake of people using
3236 * VBoxManage and setting only the NIC name from there. If there is a space we
3237 * chop it off and proceed, otherwise just use whatever we've got.
3238 */
3239 if (pszSpace)
3240 *pszSpace = '\0';
3241
3242 /* Chop it off at the colon (zone naming eg: e1000g:1 we need only the e1000g) */
3243 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk));
3244 if (pszColon)
3245 *pszColon = '\0';
3246
3247 const char *pszTrunk = szTrunk;
3248
3249# elif defined(RT_OS_WINDOWS)
3250 ComPtr<IHostNetworkInterface> hostInterface;
3251 hrc = host->FindHostNetworkInterfaceByName(HifName, hostInterface.asOutParam());
3252 if (!SUCCEEDED(hrc))
3253 {
3254 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: FindByName failed, rc=%Rhrc (0x%x)", hrc, hrc));
3255 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
3256 N_("Inexistent host networking interface, name '%ls'"),
3257 HifName.raw());
3258 }
3259
3260 HostNetworkInterfaceType_T eIfType;
3261 hrc = hostInterface->COMGETTER(InterfaceType)(&eIfType);
3262 if (FAILED(hrc))
3263 {
3264 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(InterfaceType) failed, hrc (0x%x)", hrc));
3265 H();
3266 }
3267
3268 if (eIfType != HostNetworkInterfaceType_Bridged)
3269 {
3270 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
3271 N_("Interface ('%ls') is not a Bridged Adapter interface"),
3272 HifName.raw());
3273 }
3274
3275 hrc = hostInterface->COMGETTER(Id)(bstr.asOutParam());
3276 if (FAILED(hrc))
3277 {
3278 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(Id) failed, hrc (0x%x)", hrc));
3279 H();
3280 }
3281 Guid hostIFGuid(bstr);
3282
3283 INetCfg *pNc;
3284 ComPtr<INetCfgComponent> pAdaptorComponent;
3285 LPWSTR pszApp;
3286 int rc = VERR_INTNET_FLT_IF_NOT_FOUND;
3287
3288 hrc = VBoxNetCfgWinQueryINetCfg(FALSE /*fGetWriteLock*/,
3289 L"VirtualBox",
3290 &pNc,
3291 &pszApp);
3292 Assert(hrc == S_OK);
3293 if (hrc == S_OK)
3294 {
3295 /* get the adapter's INetCfgComponent*/
3296 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.ptr(), pAdaptorComponent.asOutParam());
3297 if (hrc != S_OK)
3298 {
3299 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3300 LogRel(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc));
3301 H();
3302 }
3303 }
3304#define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\"
3305 char szTrunkName[INTNET_MAX_TRUNK_NAME];
3306 char *pszTrunkName = szTrunkName;
3307 wchar_t * pswzBindName;
3308 hrc = pAdaptorComponent->GetBindName(&pswzBindName);
3309 Assert(hrc == S_OK);
3310 if (hrc == S_OK)
3311 {
3312 int cwBindName = (int)wcslen(pswzBindName) + 1;
3313 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX);
3314 if (sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName)
3315 {
3316 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX);
3317 pszTrunkName += cbFullBindNamePrefix-1;
3318 if (!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName,
3319 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL))
3320 {
3321 DWORD err = GetLastError();
3322 hrc = HRESULT_FROM_WIN32(err);
3323 AssertMsgFailed(("%hrc=%Rhrc %#x\n", hrc, hrc));
3324 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: WideCharToMultiByte failed, hr=%Rhrc (0x%x) err=%u\n", hrc, hrc, err));
3325 }
3326 }
3327 else
3328 {
3329 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: insufficient szTrunkName buffer space\n"));
3330 /** @todo set appropriate error code */
3331 hrc = E_FAIL;
3332 }
3333
3334 if (hrc != S_OK)
3335 {
3336 AssertFailed();
3337 CoTaskMemFree(pswzBindName);
3338 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3339 H();
3340 }
3341
3342 /* we're not freeing the bind name since we'll use it later for detecting wireless*/
3343 }
3344 else
3345 {
3346 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3347 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc));
3348 H();
3349 }
3350 const char *pszTrunk = szTrunkName;
3351 /* we're not releasing the INetCfg stuff here since we use it later to figure out whether it is wireless */
3352
3353# elif defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)
3354# if defined(RT_OS_FREEBSD)
3355 /*
3356 * If we bridge to a tap interface open it the `old' direct way.
3357 * This works and performs better than bridging a physical
3358 * interface via the current FreeBSD vboxnetflt implementation.
3359 */
3360 if (!strncmp(pszHifName, "tap", sizeof "tap" - 1)) {
3361 hrc = attachToTapInterface(aNetworkAdapter);
3362 if (FAILED(hrc))
3363 {
3364 switch (hrc)
3365 {
3366 case VERR_ACCESS_DENIED:
3367 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
3368 "Failed to open '/dev/%s' for read/write access. Please check the "
3369 "permissions of that node, and that the net.link.tap.user_open "
3370 "sysctl is set. Either run 'chmod 0666 /dev/%s' or "
3371 "change the group of that node to vboxusers and make yourself "
3372 "a member of that group. Make sure that these changes are permanent."), pszHifName, pszHifName);
3373 default:
3374 AssertMsgFailed(("Could not attach to tap interface! Bad!\n"));
3375 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
3376 "Failed to initialize Host Interface Networking"));
3377 }
3378 }
3379
3380 Assert((int)maTapFD[uInstance] >= 0);
3381 if ((int)maTapFD[uInstance] >= 0)
3382 {
3383 rc = CFGMR3InsertString(pLunL0, "Driver", "HostInterface"); RC_CHECK();
3384 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
3385 rc = CFGMR3InsertInteger(pCfg, "FileHandle", maTapFD[uInstance]); RC_CHECK();
3386 }
3387 break;
3388 }
3389# endif
3390 /** @todo Check for malformed names. */
3391 const char *pszTrunk = pszHifName;
3392
3393 /* Issue a warning if the interface is down */
3394 {
3395 int iSock = socket(AF_INET, SOCK_DGRAM, 0);
3396 if (iSock >= 0)
3397 {
3398 struct ifreq Req;
3399
3400 memset(&Req, 0, sizeof(Req));
3401 strncpy(Req.ifr_name, pszHifName, sizeof(Req.ifr_name) - 1);
3402 if (ioctl(iSock, SIOCGIFFLAGS, &Req) >= 0)
3403 if ((Req.ifr_flags & IFF_UP) == 0)
3404 {
3405 setVMRuntimeErrorCallbackF(pVM, this, 0, "BridgedInterfaceDown", "Bridged interface %s is down. Guest will not be able to use this interface", pszHifName);
3406 }
3407
3408 close(iSock);
3409 }
3410 }
3411
3412# else
3413# error "PORTME (VBOX_WITH_NETFLT)"
3414# endif
3415
3416 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); RC_CHECK();
3417 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
3418 rc = CFGMR3InsertString(pCfg, "Trunk", pszTrunk); RC_CHECK();
3419 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt);
3420 RC_CHECK();
3421 char szNetwork[INTNET_MAX_NETWORK_NAME];
3422 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHifName);
3423 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); RC_CHECK();
3424 networkName = Bstr(szNetwork);
3425 trunkName = Bstr(pszTrunk);
3426 trunkType = Bstr(TRUNKTYPE_NETFLT);
3427
3428# if defined(RT_OS_DARWIN)
3429 /** @todo Come up with a better deal here. Problem is that IHostNetworkInterface is completely useless here. */
3430 if ( strstr(pszHifName, "Wireless")
3431 || strstr(pszHifName, "AirPort" ))
3432 {
3433 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); RC_CHECK();
3434 }
3435# elif defined(RT_OS_LINUX)
3436 int iSock = socket(AF_INET, SOCK_DGRAM, 0);
3437 if (iSock >= 0)
3438 {
3439 struct iwreq WRq;
3440
3441 memset(&WRq, 0, sizeof(WRq));
3442 strncpy(WRq.ifr_name, pszHifName, IFNAMSIZ);
3443 bool fSharedMacOnWire = ioctl(iSock, SIOCGIWNAME, &WRq) >= 0;
3444 close(iSock);
3445 if (fSharedMacOnWire)
3446 {
3447 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true);
3448 RC_CHECK();
3449 Log(("Set SharedMacOnWire\n"));
3450 }
3451 else
3452 Log(("Failed to get wireless name\n"));
3453 }
3454 else
3455 Log(("Failed to open wireless socket\n"));
3456# elif defined(RT_OS_FREEBSD)
3457 int iSock = socket(AF_INET, SOCK_DGRAM, 0);
3458 if (iSock >= 0)
3459 {
3460 struct ieee80211req WReq;
3461 uint8_t abData[32];
3462
3463 memset(&WReq, 0, sizeof(WReq));
3464 strncpy(WReq.i_name, pszHifName, sizeof(WReq.i_name));
3465 WReq.i_type = IEEE80211_IOC_SSID;
3466 WReq.i_val = -1;
3467 WReq.i_data = abData;
3468 WReq.i_len = sizeof(abData);
3469
3470 bool fSharedMacOnWire = ioctl(iSock, SIOCG80211, &WReq) >= 0;
3471 close(iSock);
3472 if (fSharedMacOnWire)
3473 {
3474 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true);
3475 RC_CHECK();
3476 Log(("Set SharedMacOnWire\n"));
3477 }
3478 else
3479 Log(("Failed to get wireless name\n"));
3480 }
3481 else
3482 Log(("Failed to open wireless socket\n"));
3483# elif defined(RT_OS_WINDOWS)
3484# define DEVNAME_PREFIX L"\\\\.\\"
3485 /* we are getting the medium type via IOCTL_NDIS_QUERY_GLOBAL_STATS Io Control
3486 * there is a pretty long way till there though since we need to obtain the symbolic link name
3487 * for the adapter device we are going to query given the device Guid */
3488
3489
3490 /* prepend the "\\\\.\\" to the bind name to obtain the link name */
3491
3492 wchar_t FileName[MAX_PATH];
3493 wcscpy(FileName, DEVNAME_PREFIX);
3494 wcscpy((wchar_t*)(((char*)FileName) + sizeof(DEVNAME_PREFIX) - sizeof(FileName[0])), pswzBindName);
3495
3496 /* open the device */
3497 HANDLE hDevice = CreateFile(FileName,
3498 GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
3499 NULL,
3500 OPEN_EXISTING,
3501 FILE_ATTRIBUTE_NORMAL,
3502 NULL);
3503
3504 if (hDevice != INVALID_HANDLE_VALUE)
3505 {
3506 bool fSharedMacOnWire = false;
3507
3508 /* now issue the OID_GEN_PHYSICAL_MEDIUM query */
3509 DWORD Oid = OID_GEN_PHYSICAL_MEDIUM;
3510 NDIS_PHYSICAL_MEDIUM PhMedium;
3511 DWORD cbResult;
3512 if (DeviceIoControl(hDevice,
3513 IOCTL_NDIS_QUERY_GLOBAL_STATS,
3514 &Oid,
3515 sizeof(Oid),
3516 &PhMedium,
3517 sizeof(PhMedium),
3518 &cbResult,
3519 NULL))
3520 {
3521 /* that was simple, now examine PhMedium */
3522 if ( PhMedium == NdisPhysicalMediumWirelessWan
3523 || PhMedium == NdisPhysicalMediumWirelessLan
3524 || PhMedium == NdisPhysicalMediumNative802_11
3525 || PhMedium == NdisPhysicalMediumBluetooth)
3526 fSharedMacOnWire = true;
3527 }
3528 else
3529 {
3530 int winEr = GetLastError();
3531 LogRel(("Console::configConstructor: DeviceIoControl failed, err (0x%x), ignoring\n", winEr));
3532 Assert(winEr == ERROR_INVALID_PARAMETER || winEr == ERROR_NOT_SUPPORTED || winEr == ERROR_BAD_COMMAND);
3533 }
3534 CloseHandle(hDevice);
3535
3536 if (fSharedMacOnWire)
3537 {
3538 Log(("this is a wireless adapter"));
3539 rc = CFGMR3InsertInteger(pCfg, "SharedMacOnWire", true); RC_CHECK();
3540 Log(("Set SharedMacOnWire\n"));
3541 }
3542 else
3543 Log(("this is NOT a wireless adapter"));
3544 }
3545 else
3546 {
3547 int winEr = GetLastError();
3548 AssertLogRelMsgFailed(("Console::configConstructor: CreateFile failed, err (0x%x), ignoring\n", winEr));
3549 }
3550
3551 CoTaskMemFree(pswzBindName);
3552
3553 pAdaptorComponent.setNull();
3554 /* release the pNc finally */
3555 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3556# else
3557 /** @todo PORTME: wireless detection */
3558# endif
3559
3560# if defined(RT_OS_SOLARIS)
3561# if 0 /* bird: this is a bit questionable and might cause more trouble than its worth. */
3562 /* Zone access restriction, don't allow snopping the global zone. */
3563 zoneid_t ZoneId = getzoneid();
3564 if (ZoneId != GLOBAL_ZONEID)
3565 {
3566 rc = CFGMR3InsertInteger(pCfg, "IgnoreAllPromisc", true); RC_CHECK();
3567 }
3568# endif
3569# endif
3570
3571#elif defined(RT_OS_WINDOWS) /* not defined NetFlt */
3572 /* NOTHING TO DO HERE */
3573#elif defined(RT_OS_LINUX)
3574/// @todo aleksey: is there anything to be done here?
3575#elif defined(RT_OS_FREEBSD)
3576/** @todo FreeBSD: Check out this later (HIF networking). */
3577#else
3578# error "Port me"
3579#endif
3580 break;
3581 }
3582
3583 case NetworkAttachmentType_Internal:
3584 {
3585 hrc = aNetworkAdapter->COMGETTER(InternalNetwork)(bstr.asOutParam()); H();
3586 if (!bstr.isEmpty())
3587 {
3588 if (fSniffer)
3589 {
3590 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0);
3591 RC_CHECK();
3592 }
3593 else
3594 {
3595 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0);
3596 RC_CHECK();
3597 }
3598 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); RC_CHECK();
3599 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
3600 rc = CFGMR3InsertStringW(pCfg, "Network", bstr); RC_CHECK();
3601 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_WhateverNone); RC_CHECK();
3602 networkName = bstr;
3603 trunkType = Bstr(TRUNKTYPE_WHATEVER);
3604 }
3605 break;
3606 }
3607
3608 case NetworkAttachmentType_HostOnly:
3609 {
3610 if (fSniffer)
3611 {
3612 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0);
3613 RC_CHECK();
3614 }
3615 else
3616 {
3617 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0);
3618 RC_CHECK();
3619 }
3620
3621 rc = CFGMR3InsertString(pLunL0, "Driver", "IntNet"); RC_CHECK();
3622 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
3623
3624 Bstr HifName;
3625 hrc = aNetworkAdapter->COMGETTER(HostInterface)(HifName.asOutParam());
3626 if (FAILED(hrc))
3627 {
3628 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(HostInterface) failed, hrc (0x%x)\n", hrc));
3629 H();
3630 }
3631
3632 Utf8Str HifNameUtf8(HifName);
3633 const char *pszHifName = HifNameUtf8.raw();
3634 ComPtr<IHostNetworkInterface> hostInterface;
3635 rc = host->FindHostNetworkInterfaceByName(HifName, hostInterface.asOutParam());
3636 if (!SUCCEEDED(rc))
3637 {
3638 LogRel(("NetworkAttachmentType_HostOnly: FindByName failed, rc (0x%x)\n", rc));
3639 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
3640 N_("Inexistent host networking interface, name '%ls'"),
3641 HifName.raw());
3642 }
3643
3644 char szNetwork[INTNET_MAX_NETWORK_NAME];
3645 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHifName);
3646
3647#if defined(RT_OS_WINDOWS)
3648# ifndef VBOX_WITH_NETFLT
3649 hrc = E_NOTIMPL;
3650 LogRel(("NetworkAttachmentType_HostOnly: Not Implemented\n"));
3651 H();
3652# else /* defined VBOX_WITH_NETFLT*/
3653 /** @todo r=bird: Put this in a function. */
3654
3655 HostNetworkInterfaceType_T eIfType;
3656 hrc = hostInterface->COMGETTER(InterfaceType)(&eIfType);
3657 if (FAILED(hrc))
3658 {
3659 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(InterfaceType) failed, hrc (0x%x)\n", hrc));
3660 H();
3661 }
3662
3663 if (eIfType != HostNetworkInterfaceType_HostOnly)
3664 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
3665 N_("Interface ('%ls') is not a Host-Only Adapter interface"),
3666 HifName.raw());
3667
3668 hrc = hostInterface->COMGETTER(Id)(bstr.asOutParam());
3669 if (FAILED(hrc))
3670 {
3671 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(Id) failed, hrc (0x%x)\n", hrc));
3672 H();
3673 }
3674 Guid hostIFGuid(bstr);
3675
3676 INetCfg *pNc;
3677 ComPtr<INetCfgComponent> pAdaptorComponent;
3678 LPWSTR pszApp;
3679 rc = VERR_INTNET_FLT_IF_NOT_FOUND;
3680
3681 hrc = VBoxNetCfgWinQueryINetCfg(FALSE,
3682 L"VirtualBox",
3683 &pNc,
3684 &pszApp);
3685 Assert(hrc == S_OK);
3686 if (hrc == S_OK)
3687 {
3688 /* get the adapter's INetCfgComponent*/
3689 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.ptr(), pAdaptorComponent.asOutParam());
3690 if (hrc != S_OK)
3691 {
3692 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3693 LogRel(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc=%Rhrc (0x%x)\n", hrc, hrc));
3694 H();
3695 }
3696 }
3697#define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\"
3698 char szTrunkName[INTNET_MAX_TRUNK_NAME];
3699 char *pszTrunkName = szTrunkName;
3700 wchar_t * pswzBindName;
3701 hrc = pAdaptorComponent->GetBindName(&pswzBindName);
3702 Assert(hrc == S_OK);
3703 if (hrc == S_OK)
3704 {
3705 int cwBindName = (int)wcslen(pswzBindName) + 1;
3706 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX);
3707 if (sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName)
3708 {
3709 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX);
3710 pszTrunkName += cbFullBindNamePrefix-1;
3711 if (!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName,
3712 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL))
3713 {
3714 DWORD err = GetLastError();
3715 hrc = HRESULT_FROM_WIN32(err);
3716 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: WideCharToMultiByte failed, hr=%Rhrc (0x%x) err=%u\n", hrc, hrc, err));
3717 }
3718 }
3719 else
3720 {
3721 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: insufficient szTrunkName buffer space\n"));
3722 /** @todo set appropriate error code */
3723 hrc = E_FAIL;
3724 }
3725
3726 if (hrc != S_OK)
3727 {
3728 AssertFailed();
3729 CoTaskMemFree(pswzBindName);
3730 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3731 H();
3732 }
3733 }
3734 else
3735 {
3736 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3737 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc=%Rhrc (0x%x)\n", hrc, hrc));
3738 H();
3739 }
3740
3741
3742 CoTaskMemFree(pswzBindName);
3743
3744 pAdaptorComponent.setNull();
3745 /* release the pNc finally */
3746 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3747
3748 const char *pszTrunk = szTrunkName;
3749
3750 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp); RC_CHECK();
3751 rc = CFGMR3InsertString(pCfg, "Trunk", pszTrunk); RC_CHECK();
3752 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); RC_CHECK();
3753 networkName = Bstr(szNetwork);
3754 trunkName = Bstr(pszTrunk);
3755 trunkType = TRUNKTYPE_NETADP;
3756# endif /* defined VBOX_WITH_NETFLT*/
3757#elif defined(RT_OS_DARWIN)
3758 rc = CFGMR3InsertString(pCfg, "Trunk", pszHifName); RC_CHECK();
3759 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); RC_CHECK();
3760 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp); RC_CHECK();
3761 networkName = Bstr(szNetwork);
3762 trunkName = Bstr(pszHifName);
3763 trunkType = TRUNKTYPE_NETADP;
3764#else
3765 rc = CFGMR3InsertString(pCfg, "Trunk", pszHifName); RC_CHECK();
3766 rc = CFGMR3InsertString(pCfg, "Network", szNetwork); RC_CHECK();
3767 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt); RC_CHECK();
3768 networkName = Bstr(szNetwork);
3769 trunkName = Bstr(pszHifName);
3770 trunkType = TRUNKTYPE_NETFLT;
3771#endif
3772#if !defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT)
3773
3774 Bstr tmpAddr, tmpMask;
3775
3776 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPAddress", pszHifName), tmpAddr.asOutParam());
3777 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty())
3778 {
3779 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPNetMask", pszHifName), tmpMask.asOutParam());
3780 if (SUCCEEDED(hrc) && !tmpMask.isEmpty())
3781 hrc = hostInterface->EnableStaticIpConfig(tmpAddr, tmpMask);
3782 else
3783 hrc = hostInterface->EnableStaticIpConfig(tmpAddr,
3784 Bstr(VBOXNET_IPV4MASK_DEFAULT));
3785 }
3786 else
3787 {
3788 /* Grab the IP number from the 'vboxnetX' instance number (see netif.h) */
3789 hrc = hostInterface->EnableStaticIpConfig(getDefaultIPv4Address(Bstr(pszHifName)),
3790 Bstr(VBOXNET_IPV4MASK_DEFAULT));
3791 }
3792
3793 ComAssertComRC(hrc); /** @todo r=bird: Why this isn't fatal? (H()) */
3794
3795 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPV6Address", pszHifName), tmpAddr.asOutParam());
3796 if (SUCCEEDED(hrc))
3797 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPV6NetMask", pszHifName), tmpMask.asOutParam());
3798 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty() && !tmpMask.isEmpty())
3799 {
3800 hrc = hostInterface->EnableStaticIpConfigV6(tmpAddr, Utf8Str(tmpMask).toUInt32());
3801 ComAssertComRC(hrc); /** @todo r=bird: Why this isn't fatal? (H()) */
3802 }
3803#endif
3804 break;
3805 }
3806
3807#if defined(VBOX_WITH_VDE)
3808 case NetworkAttachmentType_VDE:
3809 {
3810 hrc = aNetworkAdapter->COMGETTER(VDENetwork)(bstr.asOutParam()); H();
3811 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); RC_CHECK();
3812 rc = CFGMR3InsertString(pLunL0, "Driver", "VDE"); RC_CHECK();
3813 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); RC_CHECK();
3814 if (!bstr.isEmpty())
3815 {
3816 rc = CFGMR3InsertStringW(pCfg, "Network", bstr); RC_CHECK();
3817 networkName = bstr;
3818 }
3819 break;
3820 }
3821#endif
3822
3823 default:
3824 AssertMsgFailed(("should not get here!\n"));
3825 break;
3826 }
3827
3828 /*
3829 * Attempt to attach the driver.
3830 */
3831 switch (eAttachmentType)
3832 {
3833 case NetworkAttachmentType_Null:
3834 break;
3835
3836 case NetworkAttachmentType_Bridged:
3837 case NetworkAttachmentType_Internal:
3838 case NetworkAttachmentType_HostOnly:
3839 case NetworkAttachmentType_NAT:
3840#if defined(VBOX_WITH_VDE)
3841 case NetworkAttachmentType_VDE:
3842#endif
3843 {
3844 if (SUCCEEDED(hrc) && SUCCEEDED(rc))
3845 {
3846 if (fAttachDetach)
3847 {
3848 rc = PDMR3DriverAttach(pVM, pszDevice, uInstance, uLun, 0 /*fFlags*/, NULL /* ppBase */);
3849 //AssertRC(rc);
3850 }
3851
3852 {
3853 /** @todo pritesh: get the dhcp server name from the
3854 * previous network configuration and then stop the server
3855 * else it may conflict with the dhcp server running with
3856 * the current attachment type
3857 */
3858 /* Stop the hostonly DHCP Server */
3859 }
3860
3861 if (!networkName.isEmpty())
3862 {
3863 /*
3864 * Until we implement service reference counters DHCP Server will be stopped
3865 * by DHCPServerRunner destructor.
3866 */
3867 ComPtr<IDHCPServer> dhcpServer;
3868 hrc = virtualBox->FindDHCPServerByNetworkName(networkName, dhcpServer.asOutParam());
3869 if (SUCCEEDED(hrc))
3870 {
3871 /* there is a DHCP server available for this network */
3872 BOOL fEnabled;
3873 hrc = dhcpServer->COMGETTER(Enabled)(&fEnabled);
3874 if (FAILED(hrc))
3875 {
3876 LogRel(("DHCP svr: COMGETTER(Enabled) failed, hrc (%Rhrc)", hrc));
3877 H();
3878 }
3879
3880 if (fEnabled)
3881 hrc = dhcpServer->Start(networkName, trunkName, trunkType);
3882 }
3883 else
3884 hrc = S_OK;
3885 }
3886 }
3887
3888 break;
3889 }
3890
3891 default:
3892 AssertMsgFailed(("should not get here!\n"));
3893 break;
3894 }
3895
3896 meAttachmentType[uInstance] = eAttachmentType;
3897
3898#undef H
3899#undef RC_CHECK
3900
3901 return VINF_SUCCESS;
3902}
3903
3904#ifdef VBOX_WITH_GUEST_PROPS
3905/**
3906 * Set an array of guest properties
3907 */
3908static void configSetProperties(VMMDev * const pVMMDev, void *names,
3909 void *values, void *timestamps, void *flags)
3910{
3911 VBOXHGCMSVCPARM parms[4];
3912
3913 parms[0].type = VBOX_HGCM_SVC_PARM_PTR;
3914 parms[0].u.pointer.addr = names;
3915 parms[0].u.pointer.size = 0; /* We don't actually care. */
3916 parms[1].type = VBOX_HGCM_SVC_PARM_PTR;
3917 parms[1].u.pointer.addr = values;
3918 parms[1].u.pointer.size = 0; /* We don't actually care. */
3919 parms[2].type = VBOX_HGCM_SVC_PARM_PTR;
3920 parms[2].u.pointer.addr = timestamps;
3921 parms[2].u.pointer.size = 0; /* We don't actually care. */
3922 parms[3].type = VBOX_HGCM_SVC_PARM_PTR;
3923 parms[3].u.pointer.addr = flags;
3924 parms[3].u.pointer.size = 0; /* We don't actually care. */
3925
3926 pVMMDev->hgcmHostCall ("VBoxGuestPropSvc", guestProp::SET_PROPS_HOST, 4,
3927 &parms[0]);
3928}
3929
3930/**
3931 * Set a single guest property
3932 */
3933static void configSetProperty(VMMDev * const pVMMDev, const char *pszName,
3934 const char *pszValue, const char *pszFlags)
3935{
3936 VBOXHGCMSVCPARM parms[4];
3937
3938 AssertPtrReturnVoid(pszName);
3939 AssertPtrReturnVoid(pszValue);
3940 AssertPtrReturnVoid(pszFlags);
3941 parms[0].type = VBOX_HGCM_SVC_PARM_PTR;
3942 parms[0].u.pointer.addr = (void *)pszName;
3943 parms[0].u.pointer.size = strlen(pszName) + 1;
3944 parms[1].type = VBOX_HGCM_SVC_PARM_PTR;
3945 parms[1].u.pointer.addr = (void *)pszValue;
3946 parms[1].u.pointer.size = strlen(pszValue) + 1;
3947 parms[2].type = VBOX_HGCM_SVC_PARM_PTR;
3948 parms[2].u.pointer.addr = (void *)pszFlags;
3949 parms[2].u.pointer.size = strlen(pszFlags) + 1;
3950 pVMMDev->hgcmHostCall("VBoxGuestPropSvc", guestProp::SET_PROP_HOST, 3,
3951 &parms[0]);
3952}
3953
3954/**
3955 * Set the global flags value by calling the service
3956 * @returns the status returned by the call to the service
3957 *
3958 * @param pTable the service instance handle
3959 * @param eFlags the flags to set
3960 */
3961int configSetGlobalPropertyFlags(VMMDev * const pVMMDev,
3962 guestProp::ePropFlags eFlags)
3963{
3964 VBOXHGCMSVCPARM paParm;
3965 paParm.setUInt32(eFlags);
3966 int rc = pVMMDev->hgcmHostCall("VBoxGuestPropSvc",
3967 guestProp::SET_GLOBAL_FLAGS_HOST, 1,
3968 &paParm);
3969 if (RT_FAILURE(rc))
3970 {
3971 char szFlags[guestProp::MAX_FLAGS_LEN];
3972 if (RT_FAILURE(writeFlags(eFlags, szFlags)))
3973 Log(("Failed to set the global flags.\n"));
3974 else
3975 Log(("Failed to set the global flags \"%s\".\n", szFlags));
3976 }
3977 return rc;
3978}
3979#endif /* VBOX_WITH_GUEST_PROPS */
3980
3981/**
3982 * Set up the Guest Property service, populate it with properties read from
3983 * the machine XML and set a couple of initial properties.
3984 */
3985/* static */ int Console::configGuestProperties(void *pvConsole)
3986{
3987#ifdef VBOX_WITH_GUEST_PROPS
3988 AssertReturn(pvConsole, VERR_GENERAL_FAILURE);
3989 ComObjPtr<Console> pConsole = static_cast<Console *>(pvConsole);
3990
3991 /* Load the service */
3992 int rc = pConsole->mVMMDev->hgcmLoadService("VBoxGuestPropSvc", "VBoxGuestPropSvc");
3993
3994 if (RT_FAILURE(rc))
3995 {
3996 LogRel(("VBoxGuestPropSvc is not available. rc = %Rrc\n", rc));
3997 /* That is not a fatal failure. */
3998 rc = VINF_SUCCESS;
3999 }
4000 else
4001 {
4002 /*
4003 * Initialize built-in properties that can be changed and saved.
4004 *
4005 * These are typically transient properties that the guest cannot
4006 * change.
4007 */
4008
4009 /* Sysprep execution by VBoxService. */
4010 configSetProperty(pConsole->mVMMDev,
4011 "/VirtualBox/HostGuest/SysprepExec", "",
4012 "TRANSIENT, RDONLYGUEST");
4013 configSetProperty(pConsole->mVMMDev,
4014 "/VirtualBox/HostGuest/SysprepArgs", "",
4015 "TRANSIENT, RDONLYGUEST");
4016
4017 /*
4018 * Pull over the properties from the server.
4019 */
4020 SafeArray<BSTR> namesOut;
4021 SafeArray<BSTR> valuesOut;
4022 SafeArray<ULONG64> timestampsOut;
4023 SafeArray<BSTR> flagsOut;
4024 HRESULT hrc;
4025 hrc = pConsole->mControl->PullGuestProperties(ComSafeArrayAsOutParam(namesOut),
4026 ComSafeArrayAsOutParam(valuesOut),
4027 ComSafeArrayAsOutParam(timestampsOut),
4028 ComSafeArrayAsOutParam(flagsOut));
4029 AssertMsgReturn(SUCCEEDED(hrc), ("hrc=%Rrc\n", hrc), VERR_GENERAL_FAILURE);
4030 size_t cProps = namesOut.size();
4031 size_t cAlloc = cProps + 1;
4032 if ( valuesOut.size() != cProps
4033 || timestampsOut.size() != cProps
4034 || flagsOut.size() != cProps
4035 )
4036 AssertFailedReturn(VERR_INVALID_PARAMETER);
4037
4038 char **papszNames, **papszValues, **papszFlags;
4039 char szEmpty[] = "";
4040 ULONG64 *pau64Timestamps;
4041 papszNames = (char **)RTMemTmpAllocZ(sizeof(void *) * cAlloc);
4042 papszValues = (char **)RTMemTmpAllocZ(sizeof(void *) * cAlloc);
4043 pau64Timestamps = (ULONG64 *)RTMemTmpAllocZ(sizeof(ULONG64) * cAlloc);
4044 papszFlags = (char **)RTMemTmpAllocZ(sizeof(void *) * cAlloc);
4045 if (papszNames && papszValues && pau64Timestamps && papszFlags)
4046 {
4047 for (unsigned i = 0; RT_SUCCESS(rc) && i < cProps; ++i)
4048 {
4049 AssertPtrReturn(namesOut[i], VERR_INVALID_PARAMETER);
4050 rc = RTUtf16ToUtf8(namesOut[i], &papszNames[i]);
4051 if (RT_FAILURE(rc))
4052 break;
4053 if (valuesOut[i])
4054 rc = RTUtf16ToUtf8(valuesOut[i], &papszValues[i]);
4055 else
4056 papszValues[i] = szEmpty;
4057 if (RT_FAILURE(rc))
4058 break;
4059 pau64Timestamps[i] = timestampsOut[i];
4060 if (flagsOut[i])
4061 rc = RTUtf16ToUtf8(flagsOut[i], &papszFlags[i]);
4062 else
4063 papszFlags[i] = szEmpty;
4064 }
4065 if (RT_SUCCESS(rc))
4066 configSetProperties(pConsole->mVMMDev,
4067 (void *)papszNames,
4068 (void *)papszValues,
4069 (void *)pau64Timestamps,
4070 (void *)papszFlags);
4071 for (unsigned i = 0; i < cProps; ++i)
4072 {
4073 RTStrFree(papszNames[i]);
4074 if (valuesOut[i])
4075 RTStrFree(papszValues[i]);
4076 if (flagsOut[i])
4077 RTStrFree(papszFlags[i]);
4078 }
4079 }
4080 else
4081 rc = VERR_NO_MEMORY;
4082 RTMemTmpFree(papszNames);
4083 RTMemTmpFree(papszValues);
4084 RTMemTmpFree(pau64Timestamps);
4085 RTMemTmpFree(papszFlags);
4086 AssertRCReturn(rc, rc);
4087
4088 /*
4089 * These properties have to be set before pulling over the properties
4090 * from the machine XML, to ensure that properties saved in the XML
4091 * will override them.
4092 */
4093 /* Set the VBox version string as a guest property */
4094 configSetProperty(pConsole->mVMMDev, "/VirtualBox/HostInfo/VBoxVer",
4095 VBOX_VERSION_STRING, "TRANSIENT, RDONLYGUEST");
4096 /* Set the VBox SVN revision as a guest property */
4097 configSetProperty(pConsole->mVMMDev, "/VirtualBox/HostInfo/VBoxRev",
4098 RTBldCfgRevisionStr(), "TRANSIENT, RDONLYGUEST");
4099
4100 /*
4101 * Register the host notification callback
4102 */
4103 HGCMSVCEXTHANDLE hDummy;
4104 HGCMHostRegisterServiceExtension(&hDummy, "VBoxGuestPropSvc",
4105 Console::doGuestPropNotification,
4106 pvConsole);
4107
4108#ifdef VBOX_WITH_GUEST_PROPS_RDONLY_GUEST
4109 rc = configSetGlobalPropertyFlags(pConsole->mVMMDev,
4110 guestProp::RDONLYGUEST);
4111 AssertRCReturn(rc, rc);
4112#endif
4113
4114 Log(("Set VBoxGuestPropSvc property store\n"));
4115 }
4116 return VINF_SUCCESS;
4117#else /* !VBOX_WITH_GUEST_PROPS */
4118 return VERR_NOT_SUPPORTED;
4119#endif /* !VBOX_WITH_GUEST_PROPS */
4120}
4121
4122/**
4123 * Set up the Guest Control service.
4124 */
4125/* static */ int Console::configGuestControl(void *pvConsole)
4126{
4127#ifdef VBOX_WITH_GUEST_CONTROL
4128 AssertReturn(pvConsole, VERR_GENERAL_FAILURE);
4129 ComObjPtr<Console> pConsole = static_cast<Console *>(pvConsole);
4130
4131 /* Load the service */
4132 int rc = pConsole->mVMMDev->hgcmLoadService("VBoxGuestControlSvc", "VBoxGuestControlSvc");
4133
4134 if (RT_FAILURE(rc))
4135 {
4136 LogRel(("VBoxGuestControlSvc is not available. rc = %Rrc\n", rc));
4137 /* That is not a fatal failure. */
4138 rc = VINF_SUCCESS;
4139 }
4140 else
4141 {
4142 HGCMSVCEXTHANDLE hDummy;
4143 rc = HGCMHostRegisterServiceExtension(&hDummy, "VBoxGuestControlSvc",
4144 &Guest::doGuestCtrlNotification,
4145 pConsole->getGuest());
4146 if (RT_FAILURE(rc))
4147 Log(("Cannot register VBoxGuestControlSvc extension!\n"));
4148 else
4149 Log(("VBoxGuestControlSvc loaded\n"));
4150 }
4151
4152 return rc;
4153#else /* !VBOX_WITH_GUEST_CONTROL */
4154 return VERR_NOT_SUPPORTED;
4155#endif /* !VBOX_WITH_GUEST_CONTROL */
4156}
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