VirtualBox

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

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

windows build fix

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