VirtualBox

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

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

Main: allow restoring with broken floppy images as well

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