VirtualBox

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

最後變更 在這個檔案從34878是 34878,由 vboxsync 提交於 14 年 前

Main/ConsoleImpl: check if the USB 2.0 controller was enabled and refuse to start (restore state) or warn (start from scratch)

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

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