VirtualBox

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

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

Main: live snapshot merging. initially limited to forward merging (i.e. everything but the first snapshot can be deleted)

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