VirtualBox

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

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

CFGM,ConsoleImpl2.cpp: CFGMR3InsertStringLengthKnown -> CFGMR3InsertStringN - don't trust the caller to get the termination right. Restore CFGM APIs removed by r63525 as they might come in handy later.

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