VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

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