VirtualBox

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

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

NAT: introduces management routines of NAT libalias core. (see xTracker/#4843 for details)

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