VirtualBox

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

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

Initial API changes for resource control (storage/network/cpu)

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