VirtualBox

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

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

Main: turn ISO not found error into warning to allow restoring VMs with inaccessible ISOs

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

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