VirtualBox

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

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

windows build fix

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