VirtualBox

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

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

Main: use settings struct for machine user data; remove iprt::MiniString::raw() and change all occurences to c_str()

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