VirtualBox

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

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

Main/Settings: Drop global iomgr and iobackend settings

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette