VirtualBox

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

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

Blessed the USB 2.0 config hack and fixed the assertion in ExtPackManager::Find() caused by it.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 188.8 KB
Line 
1/* $Id: ConsoleImpl2.cpp 34897 2010-12-09 15:11:06Z vboxsync $ */
2/** @file
3 * VBox Console COM Class implementation
4 *
5 * @remark We've split out the code that the 64-bit VC++ v8 compiler finds
6 * problematic to optimize so we can disable optimizations and later,
7 * perhaps, find a real solution for it (like rewriting the code and
8 * to stop resemble a tonne of spaghetti).
9 */
10
11/*
12 * Copyright (C) 2006-2010 Oracle Corporation
13 *
14 * This file is part of VirtualBox Open Source Edition (OSE), as
15 * available from http://www.virtualbox.org. This file is free software;
16 * you can redistribute it and/or modify it under the terms of the GNU
17 * General Public License (GPL) as published by the Free Software
18 * Foundation, in version 2 as it comes in the "COPYING" file of the
19 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
20 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
21 */
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26// for some reason Windows burns in sdk\...\winsock.h if this isn't included first
27#include "VBox/com/ptr.h"
28
29#include "ConsoleImpl.h"
30#include "DisplayImpl.h"
31#ifdef VBOX_WITH_GUEST_CONTROL
32# include "GuestImpl.h"
33#endif
34#include "VMMDev.h"
35#include "Global.h"
36
37// generated header
38#include "SchemaDefs.h"
39
40#include "AutoCaller.h"
41#include "Logging.h"
42
43#include <iprt/buildconfig.h>
44#include <iprt/ctype.h>
45#include <iprt/dir.h>
46#include <iprt/file.h>
47#include <iprt/param.h>
48#include <iprt/path.h>
49#include <iprt/string.h>
50#include <iprt/system.h>
51#include <iprt/cpp/exception.h>
52#if 0 /* enable to play with lots of memory. */
53# include <iprt/env.h>
54#endif
55#include <iprt/stream.h>
56
57#include <VBox/vmapi.h>
58#include <VBox/err.h>
59#include <VBox/param.h>
60#include <VBox/pdmapi.h> /* For PDMR3DriverAttach/PDMR3DriverDetach */
61#include <VBox/version.h>
62#include <VBox/HostServices/VBoxClipboardSvc.h>
63#ifdef VBOX_WITH_CROGL
64# include <VBox/HostServices/VBoxCrOpenGLSvc.h>
65#endif
66#ifdef VBOX_WITH_GUEST_PROPS
67# include <VBox/HostServices/GuestPropertySvc.h>
68# include <VBox/com/defs.h>
69# include <VBox/com/array.h>
70# include <hgcm/HGCM.h> /** @todo it should be possible to register a service
71 * extension using a VMMDev callback. */
72# include <vector>
73#endif /* VBOX_WITH_GUEST_PROPS */
74#include <VBox/intnet.h>
75
76#include <VBox/com/com.h>
77#include <VBox/com/string.h>
78#include <VBox/com/array.h>
79
80#ifdef VBOX_WITH_NETFLT
81# if defined(RT_OS_SOLARIS)
82# include <zone.h>
83# elif defined(RT_OS_LINUX)
84# include <unistd.h>
85# include <sys/ioctl.h>
86# include <sys/socket.h>
87# include <linux/types.h>
88# include <linux/if.h>
89# include <linux/wireless.h>
90# elif defined(RT_OS_FREEBSD)
91# include <unistd.h>
92# include <sys/types.h>
93# include <sys/ioctl.h>
94# include <sys/socket.h>
95# include <net/if.h>
96# include <net80211/ieee80211_ioctl.h>
97# endif
98# if defined(RT_OS_WINDOWS)
99# include <VBox/WinNetConfig.h>
100# include <Ntddndis.h>
101# include <devguid.h>
102# else
103# include <HostNetworkInterfaceImpl.h>
104# include <netif.h>
105# include <stdlib.h>
106# endif
107#endif /* VBOX_WITH_NETFLT */
108
109#include "DHCPServerRunner.h"
110#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 /*
2059 * USB 2.0 is only available if the proper ExtPack is installed.
2060 *
2061 * Note. Configuring EHCI here and providing messages about
2062 * the missing extpack isn't exactly clean, but it is a
2063 * necessary evil to patch over legacy compatability issues
2064 * introduced by the new distribution model.
2065 */
2066 static const char *s_pszUsbExtPackName = "Oracle VM VirtualBox Extension Pack";
2067 if (pConsole->mptrExtPackManager->isExtPackUsable(s_pszUsbExtPackName))
2068 {
2069 InsertConfigNode(pDevices, "usb-ehci", &pDev);
2070 InsertConfigNode(pDev, "0", &pInst);
2071 InsertConfigNode(pInst, "Config", &pCfg);
2072 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
2073 hrc = BusMgr->assignPciDevice("usb-ehci", pInst); H();
2074
2075 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2076 InsertConfigString(pLunL0, "Driver", "VUSBRootHub");
2077 InsertConfigNode(pLunL0, "Config", &pCfg);
2078
2079 /*
2080 * Attach the status driver.
2081 */
2082 InsertConfigNode(pInst, "LUN#999", &pLunL0);
2083 InsertConfigString(pLunL0, "Driver", "MainStatus");
2084 InsertConfigNode(pLunL0, "Config", &pCfg);
2085 InsertConfigInteger(pCfg, "papLeds", (uintptr_t)&pConsole->mapUSBLed[1]);
2086 InsertConfigInteger(pCfg, "First", 0);
2087 InsertConfigInteger(pCfg, "Last", 0);
2088 }
2089 else
2090 {
2091 /* Fatal if a saved state is being restored, otherwise ignorable. */
2092 if (pConsole->mMachineState == MachineState_Restoring)
2093 return VMSetError(pVM, VERR_NOT_FOUND, RT_SRC_POS,
2094 N_("Implementation of the USB 2.0 controller not found!\n"
2095 "Because the USB 2.0 controller state is part of the saved "
2096 "VM state, the VM cannot be started. To fix "
2097 "this problem, either install the '%s' or disable USB 2.0 "
2098 "support in the VM settings"),
2099 s_pszUsbExtPackName);
2100 setVMRuntimeErrorCallbackF(pVM, pConsole, 0, "ExtPackNoEhci",
2101 N_("Implementation of the USB 2.0 controller not found!\n"
2102 "The device will be disabled. You can ignore this warning "
2103 "but there will be no USB 2.0 support in your VM. To fix "
2104 "this issue, either install the '%s' or disable USB 2.0 "
2105 "support in the VM settings"),
2106 s_pszUsbExtPackName);
2107 }
2108 }
2109#endif
2110
2111 /*
2112 * Virtual USB Devices.
2113 */
2114 PCFGMNODE pUsbDevices = NULL;
2115 InsertConfigNode(pRoot, "USB", &pUsbDevices);
2116
2117#ifdef VBOX_WITH_USB
2118 {
2119 /*
2120 * Global USB options, currently unused as we'll apply the 2.0 -> 1.1 morphing
2121 * on a per device level now.
2122 */
2123 InsertConfigNode(pUsbDevices, "USBProxy", &pCfg);
2124 InsertConfigNode(pCfg, "GlobalConfig", &pCfg);
2125 // This globally enables the 2.0 -> 1.1 device morphing of proxied devices to keep windows quiet.
2126 //InsertConfigInteger(pCfg, "Force11Device", true);
2127 // The following breaks stuff, but it makes MSDs work in vista. (I include it here so
2128 // that it's documented somewhere.) Users needing it can use:
2129 // VBoxManage setextradata "myvm" "VBoxInternal/USB/USBProxy/GlobalConfig/Force11PacketSize" 1
2130 //InsertConfigInteger(pCfg, "Force11PacketSize", true);
2131 }
2132#endif
2133
2134# if 0 /* Virtual MSD*/
2135
2136 InsertConfigNode(pUsbDevices, "Msd", &pDev);
2137 InsertConfigNode(pDev, "0", &pInst);
2138 InsertConfigNode(pInst, "Config", &pCfg);
2139 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2140
2141 InsertConfigString(pLunL0, "Driver", "SCSI");
2142 InsertConfigNode(pLunL0, "Config", &pCfg);
2143
2144 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
2145 InsertConfigString(pLunL1, "Driver", "Block");
2146 InsertConfigNode(pLunL1, "Config", &pCfg);
2147 InsertConfigString(pCfg, "Type", "HardDisk");
2148 InsertConfigInteger(pCfg, "Mountable", 0);
2149
2150 InsertConfigNode(pLunL1, "AttachedDriver", &pLunL2);
2151 InsertConfigString(pLunL2, "Driver", "VD");
2152 InsertConfigNode(pLunL2, "Config", &pCfg);
2153 InsertConfigString(pCfg, "Path", "/Volumes/DataHFS/bird/VDIs/linux.vdi");
2154 InsertConfigString(pCfg, "Format", "VDI");
2155# endif
2156
2157 /* Virtual USB Mouse/Tablet */
2158 PointingHidType_T aPointingHid;
2159 hrc = pMachine->COMGETTER(PointingHidType)(&aPointingHid); H();
2160 if (aPointingHid == PointingHidType_USBMouse || aPointingHid == PointingHidType_USBTablet)
2161 {
2162 InsertConfigNode(pUsbDevices, "HidMouse", &pDev);
2163 InsertConfigNode(pDev, "0", &pInst);
2164 InsertConfigNode(pInst, "Config", &pCfg);
2165
2166 if (aPointingHid == PointingHidType_USBTablet)
2167 {
2168 InsertConfigInteger(pCfg, "Absolute", 1);
2169 }
2170 else
2171 {
2172 InsertConfigInteger(pCfg, "Absolute", 0);
2173 }
2174 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2175 InsertConfigString(pLunL0, "Driver", "MouseQueue");
2176 InsertConfigNode(pLunL0, "Config", &pCfg);
2177 InsertConfigInteger(pCfg, "QueueSize", 128);
2178
2179 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
2180 InsertConfigString(pLunL1, "Driver", "MainMouse");
2181 InsertConfigNode(pLunL1, "Config", &pCfg);
2182 pMouse = pConsole->mMouse;
2183 InsertConfigInteger(pCfg, "Object", (uintptr_t)pMouse);
2184 }
2185
2186 /* Virtual USB Keyboard */
2187 KeyboardHidType_T aKbdHid;
2188 hrc = pMachine->COMGETTER(KeyboardHidType)(&aKbdHid); H();
2189 if (aKbdHid == KeyboardHidType_USBKeyboard)
2190 {
2191 InsertConfigNode(pUsbDevices, "HidKeyboard", &pDev);
2192 InsertConfigNode(pDev, "0", &pInst);
2193 InsertConfigNode(pInst, "Config", &pCfg);
2194
2195 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2196 InsertConfigString(pLunL0, "Driver", "KeyboardQueue");
2197 InsertConfigNode(pLunL0, "Config", &pCfg);
2198 InsertConfigInteger(pCfg, "QueueSize", 64);
2199
2200 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
2201 InsertConfigString(pLunL1, "Driver", "MainKeyboard");
2202 InsertConfigNode(pLunL1, "Config", &pCfg);
2203 pKeyboard = pConsole->mKeyboard;
2204 InsertConfigInteger(pCfg, "Object", (uintptr_t)pKeyboard);
2205 }
2206 }
2207 }
2208
2209 /*
2210 * Clipboard
2211 */
2212 {
2213 ClipboardMode_T mode = ClipboardMode_Disabled;
2214 hrc = pMachine->COMGETTER(ClipboardMode)(&mode); H();
2215
2216 if (mode != ClipboardMode_Disabled)
2217 {
2218 /* Load the service */
2219 rc = pVMMDev->hgcmLoadService("VBoxSharedClipboard", "VBoxSharedClipboard");
2220
2221 if (RT_FAILURE(rc))
2222 {
2223 LogRel(("VBoxSharedClipboard is not available. rc = %Rrc\n", rc));
2224 /* That is not a fatal failure. */
2225 rc = VINF_SUCCESS;
2226 }
2227 else
2228 {
2229 /* Setup the service. */
2230 VBOXHGCMSVCPARM parm;
2231
2232 parm.type = VBOX_HGCM_SVC_PARM_32BIT;
2233
2234 switch (mode)
2235 {
2236 default:
2237 case ClipboardMode_Disabled:
2238 {
2239 LogRel(("VBoxSharedClipboard mode: Off\n"));
2240 parm.u.uint32 = VBOX_SHARED_CLIPBOARD_MODE_OFF;
2241 break;
2242 }
2243 case ClipboardMode_GuestToHost:
2244 {
2245 LogRel(("VBoxSharedClipboard mode: Guest to Host\n"));
2246 parm.u.uint32 = VBOX_SHARED_CLIPBOARD_MODE_GUEST_TO_HOST;
2247 break;
2248 }
2249 case ClipboardMode_HostToGuest:
2250 {
2251 LogRel(("VBoxSharedClipboard mode: Host to Guest\n"));
2252 parm.u.uint32 = VBOX_SHARED_CLIPBOARD_MODE_HOST_TO_GUEST;
2253 break;
2254 }
2255 case ClipboardMode_Bidirectional:
2256 {
2257 LogRel(("VBoxSharedClipboard mode: Bidirectional\n"));
2258 parm.u.uint32 = VBOX_SHARED_CLIPBOARD_MODE_BIDIRECTIONAL;
2259 break;
2260 }
2261 }
2262
2263 pVMMDev->hgcmHostCall("VBoxSharedClipboard", VBOX_SHARED_CLIPBOARD_HOST_FN_SET_MODE, 1, &parm);
2264
2265 Log(("Set VBoxSharedClipboard mode\n"));
2266 }
2267 }
2268 }
2269
2270#ifdef VBOX_WITH_CROGL
2271 /*
2272 * crOpenGL
2273 */
2274 {
2275 BOOL fEnabled = false;
2276 hrc = pMachine->COMGETTER(Accelerate3DEnabled)(&fEnabled); H();
2277
2278 if (fEnabled)
2279 {
2280 /* Load the service */
2281 rc = pVMMDev->hgcmLoadService("VBoxSharedCrOpenGL", "VBoxSharedCrOpenGL");
2282 if (RT_FAILURE(rc))
2283 {
2284 LogRel(("Failed to load Shared OpenGL service %Rrc\n", rc));
2285 /* That is not a fatal failure. */
2286 rc = VINF_SUCCESS;
2287 }
2288 else
2289 {
2290 LogRel(("Shared crOpenGL service loaded.\n"));
2291
2292 /* Setup the service. */
2293 VBOXHGCMSVCPARM parm;
2294 parm.type = VBOX_HGCM_SVC_PARM_PTR;
2295
2296 parm.u.pointer.addr = (IConsole*) (Console*) pConsole;
2297 parm.u.pointer.size = sizeof(IConsole *);
2298
2299 rc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_SET_CONSOLE, SHCRGL_CPARMS_SET_CONSOLE, &parm);
2300 if (!RT_SUCCESS(rc))
2301 AssertMsgFailed(("SHCRGL_HOST_FN_SET_CONSOLE failed with %Rrc\n", rc));
2302
2303 parm.u.pointer.addr = pVM;
2304 parm.u.pointer.size = sizeof(pVM);
2305 rc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_SET_VM, SHCRGL_CPARMS_SET_VM, &parm);
2306 if (!RT_SUCCESS(rc))
2307 AssertMsgFailed(("SHCRGL_HOST_FN_SET_VM failed with %Rrc\n", rc));
2308 }
2309
2310 }
2311 }
2312#endif
2313
2314#ifdef VBOX_WITH_GUEST_PROPS
2315 /*
2316 * Guest property service
2317 */
2318
2319 rc = configGuestProperties(pConsole);
2320#endif /* VBOX_WITH_GUEST_PROPS defined */
2321
2322#ifdef VBOX_WITH_GUEST_CONTROL
2323 /*
2324 * Guest control service
2325 */
2326
2327 rc = configGuestControl(pConsole);
2328#endif /* VBOX_WITH_GUEST_CONTROL defined */
2329
2330 /*
2331 * ACPI
2332 */
2333 BOOL fACPI;
2334 hrc = biosSettings->COMGETTER(ACPIEnabled)(&fACPI); H();
2335 if (fACPI)
2336 {
2337 BOOL fCpuHotPlug = false;
2338 BOOL fShowCpu = fOsXGuest;
2339 /* Always show the CPU leafs when we have multiple VCPUs or when the IO-APIC is enabled.
2340 * The Windows SMP kernel needs a CPU leaf or else its idle loop will burn cpu cycles; the
2341 * intelppm driver refuses to register an idle state handler.
2342 */
2343 if ((cCpus > 1) || fIOAPIC)
2344 fShowCpu = true;
2345
2346 hrc = pMachine->COMGETTER(CPUHotPlugEnabled)(&fCpuHotPlug); H();
2347
2348 InsertConfigNode(pDevices, "acpi", &pDev);
2349 InsertConfigNode(pDev, "0", &pInst);
2350 InsertConfigInteger(pInst, "Trusted", 1); /* boolean */
2351 InsertConfigNode(pInst, "Config", &pCfg);
2352 hrc = BusMgr->assignPciDevice("acpi", pInst); H();
2353
2354 InsertConfigInteger(pCfg, "RamSize", cbRam);
2355 InsertConfigInteger(pCfg, "RamHoleSize", cbRamHole);
2356 InsertConfigInteger(pCfg, "NumCPUs", cCpus);
2357
2358 InsertConfigInteger(pCfg, "IOAPIC", fIOAPIC);
2359 InsertConfigInteger(pCfg, "FdcEnabled", fFdcEnabled);
2360 InsertConfigInteger(pCfg, "HpetEnabled", fHpetEnabled);
2361 InsertConfigInteger(pCfg, "SmcEnabled", fSmcEnabled);
2362 InsertConfigInteger(pCfg, "ShowRtc", fShowRtc);
2363 if (fOsXGuest && !llBootNics.empty())
2364 {
2365 BootNic aNic = llBootNics.front();
2366 uint32_t u32NicPciAddr = (aNic.mPciAddress.iDevice << 16) | aNic.mPciAddress.iFn;
2367 InsertConfigInteger(pCfg, "NicPciAddress", u32NicPciAddr);
2368 }
2369 if (fOsXGuest && fAudioEnabled)
2370 {
2371 PciBusAddress Address;
2372 if (BusMgr->findPciAddress("hda", 0, Address))
2373 {
2374 uint32_t u32AudioPciAddr = (Address.iDevice << 16) | Address.iFn;
2375 InsertConfigInteger(pCfg, "AudioPciAddress", u32AudioPciAddr);
2376 }
2377 }
2378 InsertConfigInteger(pCfg, "IocPciAddress", u32IocPciAddress);
2379 if (chipsetType == ChipsetType_ICH9)
2380 {
2381 InsertConfigInteger(pCfg, "McfgBase", u64McfgBase);
2382 InsertConfigInteger(pCfg, "McfgLength", u32McfgLength);
2383 }
2384 InsertConfigInteger(pCfg, "HostBusPciAddress", u32HbcPciAddress);
2385 InsertConfigInteger(pCfg, "ShowCpu", fShowCpu);
2386 InsertConfigInteger(pCfg, "CpuHotPlug", fCpuHotPlug);
2387
2388 InsertConfigNode(pInst, "LUN#0", &pLunL0);
2389 InsertConfigString(pLunL0, "Driver", "ACPIHost");
2390 InsertConfigNode(pLunL0, "Config", &pCfg);
2391
2392 /* Attach the dummy CPU drivers */
2393 for (ULONG iCpuCurr = 1; iCpuCurr < cCpus; iCpuCurr++)
2394 {
2395 BOOL fCpuAttached = true;
2396
2397 if (fCpuHotPlug)
2398 {
2399 hrc = pMachine->GetCPUStatus(iCpuCurr, &fCpuAttached); H();
2400 }
2401
2402 if (fCpuAttached)
2403 {
2404 InsertConfigNode(pInst, Utf8StrFmt("LUN#%u", iCpuCurr).c_str(), &pLunL0);
2405 InsertConfigString(pLunL0, "Driver", "ACPICpu");
2406 InsertConfigNode(pLunL0, "Config", &pCfg);
2407 }
2408 }
2409 }
2410 }
2411 catch (ConfigError &x)
2412 {
2413 // InsertConfig threw something:
2414 return x.m_vrc;
2415 }
2416
2417#ifdef VBOX_WITH_EXTPACK
2418 /*
2419 * Call the extension pack hooks if everything went well thus far.
2420 */
2421 if (RT_SUCCESS(rc))
2422 {
2423 alock.release();
2424 rc = pConsole->mptrExtPackManager->callAllVmConfigureVmmHooks(pConsole, pVM);
2425 alock.acquire();
2426 }
2427#endif
2428
2429 /*
2430 * Apply the CFGM overlay.
2431 */
2432 if (RT_SUCCESS(rc))
2433 rc = pConsole->configCfgmOverlay(pVM, virtualBox, pMachine);
2434
2435#undef H
2436
2437 /*
2438 * Register VM state change handler.
2439 */
2440 int rc2 = VMR3AtStateRegister(pVM, Console::vmstateChangeCallback, pConsole);
2441 AssertRC(rc2);
2442 if (RT_SUCCESS(rc))
2443 rc = rc2;
2444
2445 /*
2446 * Register VM runtime error handler.
2447 */
2448 rc2 = VMR3AtRuntimeErrorRegister(pVM, Console::setVMRuntimeErrorCallback, pConsole);
2449 AssertRC(rc2);
2450 if (RT_SUCCESS(rc))
2451 rc = rc2;
2452
2453 LogFlowFunc(("vrc = %Rrc\n", rc));
2454 LogFlowFuncLeave();
2455
2456 return rc;
2457}
2458
2459/**
2460 * Applies the CFGM overlay as specified by /VBoxInternal/XXX extra data
2461 * values.
2462 *
2463 * @returns VBox status code.
2464 * @param pVM The VM handle.
2465 * @param pVirtualBox Pointer to the IVirtualBox interface.
2466 * @param pMachine Pointer to the IMachine interface.
2467 */
2468/* static */
2469int Console::configCfgmOverlay(PVM pVM, IVirtualBox *pVirtualBox, IMachine *pMachine)
2470{
2471 /*
2472 * CFGM overlay handling.
2473 *
2474 * Here we check the extra data entries for CFGM values
2475 * and create the nodes and insert the values on the fly. Existing
2476 * values will be removed and reinserted. CFGM is typed, so by default
2477 * we will guess whether it's a string or an integer (byte arrays are
2478 * not currently supported). It's possible to override this autodetection
2479 * by adding "string:", "integer:" or "bytes:" (future).
2480 *
2481 * We first perform a run on global extra data, then on the machine
2482 * extra data to support global settings with local overrides.
2483 */
2484 PCFGMNODE pRoot = CFGMR3GetRoot(pVM);
2485 int rc = VINF_SUCCESS;
2486 try
2487 {
2488 /** @todo add support for removing nodes and byte blobs. */
2489 /*
2490 * Get the next key
2491 */
2492 SafeArray<BSTR> aGlobalExtraDataKeys;
2493 SafeArray<BSTR> aMachineExtraDataKeys;
2494 HRESULT hrc = pVirtualBox->GetExtraDataKeys(ComSafeArrayAsOutParam(aGlobalExtraDataKeys));
2495 AssertMsg(SUCCEEDED(hrc), ("VirtualBox::GetExtraDataKeys failed with %Rhrc\n", hrc));
2496
2497 // remember the no. of global values so we can call the correct method below
2498 size_t cGlobalValues = aGlobalExtraDataKeys.size();
2499
2500 hrc = pMachine->GetExtraDataKeys(ComSafeArrayAsOutParam(aMachineExtraDataKeys));
2501 AssertMsg(SUCCEEDED(hrc), ("VirtualBox::GetExtraDataKeys failed with %Rhrc\n", hrc));
2502
2503 // build a combined list from global keys...
2504 std::list<Utf8Str> llExtraDataKeys;
2505
2506 for (size_t i = 0; i < aGlobalExtraDataKeys.size(); ++i)
2507 llExtraDataKeys.push_back(Utf8Str(aGlobalExtraDataKeys[i]));
2508 // ... and machine keys
2509 for (size_t i = 0; i < aMachineExtraDataKeys.size(); ++i)
2510 llExtraDataKeys.push_back(Utf8Str(aMachineExtraDataKeys[i]));
2511
2512 size_t i2 = 0;
2513 for (std::list<Utf8Str>::const_iterator it = llExtraDataKeys.begin();
2514 it != llExtraDataKeys.end();
2515 ++it, ++i2)
2516 {
2517 const Utf8Str &strKey = *it;
2518
2519 /*
2520 * We only care about keys starting with "VBoxInternal/" (skip "G:" or "M:")
2521 */
2522 if (!strKey.startsWith("VBoxInternal/"))
2523 continue;
2524
2525 const char *pszExtraDataKey = strKey.c_str() + sizeof("VBoxInternal/") - 1;
2526
2527 // get the value
2528 Bstr bstrExtraDataValue;
2529 if (i2 < cGlobalValues)
2530 // this is still one of the global values:
2531 hrc = pVirtualBox->GetExtraData(Bstr(strKey).raw(),
2532 bstrExtraDataValue.asOutParam());
2533 else
2534 hrc = pMachine->GetExtraData(Bstr(strKey).raw(),
2535 bstrExtraDataValue.asOutParam());
2536 if (FAILED(hrc))
2537 LogRel(("Warning: Cannot get extra data key %s, rc = %Rrc\n", strKey.c_str(), hrc));
2538
2539 /*
2540 * The key will be in the format "Node1/Node2/Value" or simply "Value".
2541 * Split the two and get the node, delete the value and create the node
2542 * if necessary.
2543 */
2544 PCFGMNODE pNode;
2545 const char *pszCFGMValueName = strrchr(pszExtraDataKey, '/');
2546 if (pszCFGMValueName)
2547 {
2548 /* terminate the node and advance to the value (Utf8Str might not
2549 offically like this but wtf) */
2550 *(char*)pszCFGMValueName = '\0';
2551 ++pszCFGMValueName;
2552
2553 /* does the node already exist? */
2554 pNode = CFGMR3GetChild(pRoot, pszExtraDataKey);
2555 if (pNode)
2556 CFGMR3RemoveValue(pNode, pszCFGMValueName);
2557 else
2558 {
2559 /* create the node */
2560 rc = CFGMR3InsertNode(pRoot, pszExtraDataKey, &pNode);
2561 if (RT_FAILURE(rc))
2562 {
2563 AssertLogRelMsgRC(rc, ("failed to insert node '%s'\n", pszExtraDataKey));
2564 continue;
2565 }
2566 Assert(pNode);
2567 }
2568 }
2569 else
2570 {
2571 /* root value (no node path). */
2572 pNode = pRoot;
2573 pszCFGMValueName = pszExtraDataKey;
2574 pszExtraDataKey--;
2575 CFGMR3RemoveValue(pNode, pszCFGMValueName);
2576 }
2577
2578 /*
2579 * Now let's have a look at the value.
2580 * Empty strings means that we should remove the value, which we've
2581 * already done above.
2582 */
2583 Utf8Str strCFGMValueUtf8(bstrExtraDataValue);
2584 if (!strCFGMValueUtf8.isEmpty())
2585 {
2586 uint64_t u64Value;
2587
2588 /* check for type prefix first. */
2589 if (!strncmp(strCFGMValueUtf8.c_str(), "string:", sizeof("string:") - 1))
2590 InsertConfigString(pNode, pszCFGMValueName, strCFGMValueUtf8.c_str() + sizeof("string:") - 1);
2591 else if (!strncmp(strCFGMValueUtf8.c_str(), "integer:", sizeof("integer:") - 1))
2592 {
2593 rc = RTStrToUInt64Full(strCFGMValueUtf8.c_str() + sizeof("integer:") - 1, 0, &u64Value);
2594 if (RT_SUCCESS(rc))
2595 rc = CFGMR3InsertInteger(pNode, pszCFGMValueName, u64Value);
2596 }
2597 else if (!strncmp(strCFGMValueUtf8.c_str(), "bytes:", sizeof("bytes:") - 1))
2598 rc = VERR_NOT_IMPLEMENTED;
2599 /* auto detect type. */
2600 else if (RT_SUCCESS(RTStrToUInt64Full(strCFGMValueUtf8.c_str(), 0, &u64Value)))
2601 rc = CFGMR3InsertInteger(pNode, pszCFGMValueName, u64Value);
2602 else
2603 InsertConfigString(pNode, pszCFGMValueName, strCFGMValueUtf8);
2604 AssertLogRelMsgRCBreak(rc, ("failed to insert CFGM value '%s' to key '%s'\n", strCFGMValueUtf8.c_str(), pszExtraDataKey));
2605 }
2606 }
2607 }
2608 catch (ConfigError &x)
2609 {
2610 // InsertConfig threw something:
2611 return x.m_vrc;
2612 }
2613 return rc;
2614}
2615
2616/**
2617 * Ellipsis to va_list wrapper for calling setVMRuntimeErrorCallback.
2618 */
2619/*static*/
2620void Console::setVMRuntimeErrorCallbackF(PVM pVM, void *pvConsole, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, ...)
2621{
2622 va_list va;
2623 va_start(va, pszFormat);
2624 setVMRuntimeErrorCallback(pVM, pvConsole, fFlags, pszErrorId, pszFormat, va);
2625 va_end(va);
2626}
2627
2628/* XXX introduce RT format specifier */
2629static uint64_t formatDiskSize(uint64_t u64Size, const char **pszUnit)
2630{
2631 if (u64Size > INT64_C(5000)*_1G)
2632 {
2633 *pszUnit = "TB";
2634 return u64Size / _1T;
2635 }
2636 else if (u64Size > INT64_C(5000)*_1M)
2637 {
2638 *pszUnit = "GB";
2639 return u64Size / _1G;
2640 }
2641 else
2642 {
2643 *pszUnit = "MB";
2644 return u64Size / _1M;
2645 }
2646}
2647
2648int Console::configMediumAttachment(PCFGMNODE pCtlInst,
2649 const char *pcszDevice,
2650 unsigned uInstance,
2651 StorageBus_T enmBus,
2652 bool fUseHostIOCache,
2653 bool fBuiltinIoCache,
2654 bool fSetupMerge,
2655 unsigned uMergeSource,
2656 unsigned uMergeTarget,
2657 IMediumAttachment *pMediumAtt,
2658 MachineState_T aMachineState,
2659 HRESULT *phrc,
2660 bool fAttachDetach,
2661 bool fForceUnmount,
2662 PVM pVM,
2663 DeviceType_T *paLedDevType)
2664{
2665 // InsertConfig* throws
2666 try
2667 {
2668 int rc = VINF_SUCCESS;
2669 HRESULT hrc;
2670 Bstr bstr;
2671
2672// #define RC_CHECK() AssertMsgReturn(RT_SUCCESS(rc), ("rc=%Rrc\n", rc), rc)
2673#define H() AssertMsgReturn(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), VERR_GENERAL_FAILURE)
2674
2675 LONG lDev;
2676 hrc = pMediumAtt->COMGETTER(Device)(&lDev); H();
2677 LONG lPort;
2678 hrc = pMediumAtt->COMGETTER(Port)(&lPort); H();
2679 DeviceType_T lType;
2680 hrc = pMediumAtt->COMGETTER(Type)(&lType); H();
2681
2682 unsigned uLUN;
2683 PCFGMNODE pLunL0 = NULL;
2684 PCFGMNODE pCfg = NULL;
2685 hrc = Console::convertBusPortDeviceToLun(enmBus, lPort, lDev, uLUN); H();
2686
2687 /* First check if the LUN already exists. */
2688 pLunL0 = CFGMR3GetChildF(pCtlInst, "LUN#%u", uLUN);
2689 if (pLunL0)
2690 {
2691 if (fAttachDetach)
2692 {
2693 if (lType != DeviceType_HardDisk)
2694 {
2695 /* Unmount existing media only for floppy and DVD drives. */
2696 PPDMIBASE pBase;
2697 rc = PDMR3QueryLun(pVM, pcszDevice, uInstance, uLUN, &pBase);
2698 if (RT_FAILURE(rc))
2699 {
2700 if (rc == VERR_PDM_LUN_NOT_FOUND || rc == VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN)
2701 rc = VINF_SUCCESS;
2702 AssertRC(rc);
2703 }
2704 else
2705 {
2706 PPDMIMOUNT pIMount = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMOUNT);
2707 AssertReturn(pIMount, VERR_INVALID_POINTER);
2708
2709 /* Unmount the media. */
2710 rc = pIMount->pfnUnmount(pIMount, fForceUnmount);
2711 if (rc == VERR_PDM_MEDIA_NOT_MOUNTED)
2712 rc = VINF_SUCCESS;
2713 }
2714 }
2715
2716 rc = PDMR3DeviceDetach(pVM, pcszDevice, 0, uLUN, PDM_TACH_FLAGS_NOT_HOT_PLUG);
2717 if (rc == VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN)
2718 rc = VINF_SUCCESS;
2719 AssertMsgReturn(RT_SUCCESS(rc), ("rc=%Rrc\n", rc), rc);
2720
2721 CFGMR3RemoveNode(pLunL0);
2722 }
2723 else
2724 AssertFailedReturn(VERR_INTERNAL_ERROR);
2725 }
2726
2727 InsertConfigNode(pCtlInst, Utf8StrFmt("LUN#%u", uLUN).c_str(), &pLunL0);
2728
2729 /* SCSI has a another driver between device and block. */
2730 if (enmBus == StorageBus_SCSI || enmBus == StorageBus_SAS)
2731 {
2732 InsertConfigString(pLunL0, "Driver", "SCSI");
2733 InsertConfigNode(pLunL0, "Config", &pCfg);
2734
2735 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL0);
2736 }
2737
2738 ComPtr<IMedium> pMedium;
2739 hrc = pMediumAtt->COMGETTER(Medium)(pMedium.asOutParam()); H();
2740
2741 /*
2742 * 1. Only check this for hard disk images.
2743 * 2. Only check during VM creation and not later, especially not during
2744 * taking an online snapshot!
2745 */
2746 if ( lType == DeviceType_HardDisk
2747 && ( aMachineState == MachineState_Starting
2748 || aMachineState == MachineState_Restoring))
2749 {
2750 /*
2751 * Some sanity checks.
2752 */
2753 ComPtr<IMediumFormat> pMediumFormat;
2754 hrc = pMedium->COMGETTER(MediumFormat)(pMediumFormat.asOutParam()); H();
2755 ULONG uCaps;
2756 hrc = pMediumFormat->COMGETTER(Capabilities)(&uCaps); H();
2757 if (uCaps & MediumFormatCapabilities_File)
2758 {
2759 Bstr strFile;
2760 hrc = pMedium->COMGETTER(Location)(strFile.asOutParam()); H();
2761 Utf8Str utfFile = Utf8Str(strFile);
2762 Bstr strSnap;
2763 ComPtr<IMachine> pMachine = machine();
2764 hrc = pMachine->COMGETTER(SnapshotFolder)(strSnap.asOutParam()); H();
2765 Utf8Str utfSnap = Utf8Str(strSnap);
2766 RTFSTYPE enmFsTypeFile = RTFSTYPE_UNKNOWN;
2767 RTFSTYPE enmFsTypeSnap = RTFSTYPE_UNKNOWN;
2768 int rc2 = RTFsQueryType(utfFile.c_str(), &enmFsTypeFile);
2769 AssertMsgRCReturn(rc2, ("Querying the file type of '%s' failed!\n", utfFile.c_str()), rc2);
2770 /* Ignore the error code. On error, the file system type is still 'unknown' so
2771 * none of the following paths are taken. This can happen for new VMs which
2772 * still don't have a snapshot folder. */
2773 (void)RTFsQueryType(utfSnap.c_str(), &enmFsTypeSnap);
2774 LogRel(("File system of '%s' is %s\n", utfFile.c_str(), RTFsTypeName(enmFsTypeFile)));
2775 LONG64 i64Size;
2776 hrc = pMedium->COMGETTER(LogicalSize)(&i64Size); H();
2777#ifdef RT_OS_WINDOWS
2778 if ( enmFsTypeFile == RTFSTYPE_FAT
2779 && i64Size >= _4G)
2780 {
2781 const char *pszUnit;
2782 uint64_t u64Print = formatDiskSize((uint64_t)i64Size, &pszUnit);
2783 setVMRuntimeErrorCallbackF(pVM, this, 0,
2784 "FatPartitionDetected",
2785 N_("The medium '%ls' has a logical size of %RU64%s "
2786 "but the file system the medium is located on seems "
2787 "to be FAT(32) which cannot handle files bigger than 4GB.\n"
2788 "We strongly recommend to put all your virtual disk images and "
2789 "the snapshot folder onto an NTFS partition"),
2790 strFile.raw(), u64Print, pszUnit);
2791 }
2792#else /* !RT_OS_WINDOWS */
2793 if ( enmFsTypeFile == RTFSTYPE_FAT
2794 || enmFsTypeFile == RTFSTYPE_EXT
2795 || enmFsTypeFile == RTFSTYPE_EXT2
2796 || enmFsTypeFile == RTFSTYPE_EXT3
2797 || enmFsTypeFile == RTFSTYPE_EXT4)
2798 {
2799 RTFILE file;
2800 rc = RTFileOpen(&file, utfFile.c_str(), RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
2801 if (RT_SUCCESS(rc))
2802 {
2803 RTFOFF maxSize;
2804 /* Careful: This function will work only on selected local file systems! */
2805 rc = RTFileGetMaxSizeEx(file, &maxSize);
2806 RTFileClose(file);
2807 if ( RT_SUCCESS(rc)
2808 && maxSize > 0
2809 && i64Size > (LONG64)maxSize)
2810 {
2811 const char *pszUnitSiz;
2812 const char *pszUnitMax;
2813 uint64_t u64PrintSiz = formatDiskSize((LONG64)i64Size, &pszUnitSiz);
2814 uint64_t u64PrintMax = formatDiskSize(maxSize, &pszUnitMax);
2815 setVMRuntimeErrorCallbackF(pVM, this, 0,
2816 "FatPartitionDetected", /* <= not exact but ... */
2817 N_("The medium '%ls' has a logical size of %RU64%s "
2818 "but the file system the medium is located on can "
2819 "only handle files up to %RU64%s in theory.\n"
2820 "We strongly recommend to put all your virtual disk "
2821 "images and the snapshot folder onto a proper "
2822 "file system (e.g. ext3) with a sufficient size"),
2823 strFile.raw(), u64PrintSiz, pszUnitSiz, u64PrintMax, pszUnitMax);
2824 }
2825 }
2826 }
2827#endif /* !RT_OS_WINDOWS */
2828
2829 /*
2830 * Snapshot folder:
2831 * Here we test only for a FAT partition as we had to create a dummy file otherwise
2832 */
2833 if ( enmFsTypeSnap == RTFSTYPE_FAT
2834 && i64Size >= _4G
2835 && !mfSnapshotFolderSizeWarningShown)
2836 {
2837 const char *pszUnit;
2838 uint64_t u64Print = formatDiskSize(i64Size, &pszUnit);
2839 setVMRuntimeErrorCallbackF(pVM, this, 0,
2840 "FatPartitionDetected",
2841#ifdef RT_OS_WINDOWS
2842 N_("The snapshot folder of this VM '%ls' seems to be located on "
2843 "a FAT(32) file system. The logical size of the medium '%ls' "
2844 "(%RU64%s) is bigger than the maximum file size this file "
2845 "system can handle (4GB).\n"
2846 "We strongly recommend to put all your virtual disk images and "
2847 "the snapshot folder onto an NTFS partition"),
2848#else
2849 N_("The snapshot folder of this VM '%ls' seems to be located on "
2850 "a FAT(32) file system. The logical size of the medium '%ls' "
2851 "(%RU64%s) is bigger than the maximum file size this file "
2852 "system can handle (4GB).\n"
2853 "We strongly recommend to put all your virtual disk images and "
2854 "the snapshot folder onto a proper file system (e.g. ext3)"),
2855#endif
2856 strSnap.raw(), strFile.raw(), u64Print, pszUnit);
2857 /* Show this particular warning only once */
2858 mfSnapshotFolderSizeWarningShown = true;
2859 }
2860
2861#ifdef RT_OS_LINUX
2862 /*
2863 * Ext4 bug: Check if the host I/O cache is disabled and the disk image is located
2864 * on an ext4 partition. Later we have to check the Linux kernel version!
2865 * This bug apparently applies to the XFS file system as well.
2866 * Linux 2.6.36 is known to be fixed (tested with 2.6.36-rc4).
2867 */
2868
2869 char szOsRelease[128];
2870 rc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szOsRelease, sizeof(szOsRelease));
2871 bool fKernelHasODirectBug = RT_FAILURE(rc)
2872 || (RTStrVersionCompare(szOsRelease, "2.6.36-rc4") < 0);
2873
2874 if ( (uCaps & MediumFormatCapabilities_Asynchronous)
2875 && !fUseHostIOCache
2876 && fKernelHasODirectBug)
2877 {
2878 if ( enmFsTypeFile == RTFSTYPE_EXT4
2879 || enmFsTypeFile == RTFSTYPE_XFS)
2880 {
2881 setVMRuntimeErrorCallbackF(pVM, this, 0,
2882 "Ext4PartitionDetected",
2883 N_("The host I/O cache for at least one controller is disabled "
2884 "and the medium '%ls' for this VM "
2885 "is located on an %s partition. There is a known Linux "
2886 "kernel bug which can lead to the corruption of the virtual "
2887 "disk image under these conditions.\n"
2888 "Either enable the host I/O cache permanently in the VM "
2889 "settings or put the disk image and the snapshot folder "
2890 "onto a different file system.\n"
2891 "The host I/O cache will now be enabled for this medium"),
2892 strFile.raw(), enmFsTypeFile == RTFSTYPE_EXT4 ? "ext4" : "xfs");
2893 fUseHostIOCache = true;
2894 }
2895 else if ( ( enmFsTypeSnap == RTFSTYPE_EXT4
2896 || enmFsTypeSnap == RTFSTYPE_XFS)
2897 && !mfSnapshotFolderExt4WarningShown)
2898 {
2899 setVMRuntimeErrorCallbackF(pVM, this, 0,
2900 "Ext4PartitionDetected",
2901 N_("The host I/O cache for at least one controller is disabled "
2902 "and the snapshot folder for this VM "
2903 "is located on an %s partition. There is a known Linux "
2904 "kernel bug which can lead to the corruption of the virtual "
2905 "disk image under these conditions.\n"
2906 "Either enable the host I/O cache permanently in the VM "
2907 "settings or put the disk image and the snapshot folder "
2908 "onto a different file system.\n"
2909 "The host I/O cache will now be enabled for this medium"),
2910 enmFsTypeSnap == RTFSTYPE_EXT4 ? "ext4" : "xfs");
2911 fUseHostIOCache = true;
2912 mfSnapshotFolderExt4WarningShown = true;
2913 }
2914 }
2915#endif
2916 }
2917 }
2918
2919 BOOL fPassthrough;
2920 hrc = pMediumAtt->COMGETTER(Passthrough)(&fPassthrough); H();
2921
2922 ComObjPtr<IBandwidthGroup> pBwGroup;
2923 Bstr strBwGroup;
2924 hrc = pMediumAtt->COMGETTER(BandwidthGroup)(pBwGroup.asOutParam()); H();
2925
2926 if (!pBwGroup.isNull())
2927 {
2928 hrc = pBwGroup->COMGETTER(Name)(strBwGroup.asOutParam()); H();
2929 }
2930
2931 rc = configMedium(pLunL0,
2932 !!fPassthrough,
2933 lType,
2934 fUseHostIOCache,
2935 fBuiltinIoCache,
2936 fSetupMerge,
2937 uMergeSource,
2938 uMergeTarget,
2939 strBwGroup.isEmpty() ? NULL : Utf8Str(strBwGroup).c_str(),
2940 pMedium,
2941 aMachineState,
2942 phrc);
2943 if (RT_FAILURE(rc))
2944 return rc;
2945
2946 if (fAttachDetach)
2947 {
2948 /* Attach the new driver. */
2949 rc = PDMR3DeviceAttach(pVM, pcszDevice, 0, uLUN,
2950 PDM_TACH_FLAGS_NOT_HOT_PLUG, NULL /*ppBase*/);
2951 AssertMsgReturn(RT_SUCCESS(rc), ("rc=%Rrc\n", rc), rc);
2952
2953 /* There is no need to handle removable medium mounting, as we
2954 * unconditionally replace everthing including the block driver level.
2955 * This means the new medium will be picked up automatically. */
2956 }
2957
2958 if (paLedDevType)
2959 paLedDevType[uLUN] = lType;
2960 }
2961 catch (ConfigError &x)
2962 {
2963 // InsertConfig threw something:
2964 return x.m_vrc;
2965 }
2966
2967#undef H
2968
2969 return VINF_SUCCESS;;
2970}
2971
2972int Console::configMedium(PCFGMNODE pLunL0,
2973 bool fPassthrough,
2974 DeviceType_T enmType,
2975 bool fUseHostIOCache,
2976 bool fBuiltinIoCache,
2977 bool fSetupMerge,
2978 unsigned uMergeSource,
2979 unsigned uMergeTarget,
2980 const char *pcszBwGroup,
2981 IMedium *pMedium,
2982 MachineState_T aMachineState,
2983 HRESULT *phrc)
2984{
2985 // InsertConfig* throws
2986 try
2987 {
2988 int rc = VINF_SUCCESS;
2989 HRESULT hrc;
2990 Bstr bstr;
2991
2992#define H() AssertMsgReturnStmt(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), if (phrc) *phrc = hrc, VERR_GENERAL_FAILURE)
2993
2994 PCFGMNODE pLunL1 = NULL;
2995 PCFGMNODE pCfg = NULL;
2996
2997 BOOL fHostDrive = FALSE;
2998 MediumType_T mediumType = MediumType_Normal;
2999 if (pMedium)
3000 {
3001 hrc = pMedium->COMGETTER(HostDrive)(&fHostDrive); H();
3002 hrc = pMedium->COMGETTER(Type)(&mediumType); H();
3003 }
3004
3005 if (fHostDrive)
3006 {
3007 Assert(pMedium);
3008 if (enmType == DeviceType_DVD)
3009 {
3010 InsertConfigString(pLunL0, "Driver", "HostDVD");
3011 InsertConfigNode(pLunL0, "Config", &pCfg);
3012
3013 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
3014 InsertConfigString(pCfg, "Path", bstr);
3015
3016 InsertConfigInteger(pCfg, "Passthrough", fPassthrough);
3017 }
3018 else if (enmType == DeviceType_Floppy)
3019 {
3020 InsertConfigString(pLunL0, "Driver", "HostFloppy");
3021 InsertConfigNode(pLunL0, "Config", &pCfg);
3022
3023 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
3024 InsertConfigString(pCfg, "Path", bstr);
3025 }
3026 }
3027 else
3028 {
3029 InsertConfigString(pLunL0, "Driver", "Block");
3030 InsertConfigNode(pLunL0, "Config", &pCfg);
3031 switch (enmType)
3032 {
3033 case DeviceType_DVD:
3034 InsertConfigString(pCfg, "Type", "DVD");
3035 InsertConfigInteger(pCfg, "Mountable", 1);
3036 break;
3037 case DeviceType_Floppy:
3038 InsertConfigString(pCfg, "Type", "Floppy 1.44");
3039 InsertConfigInteger(pCfg, "Mountable", 1);
3040 break;
3041 case DeviceType_HardDisk:
3042 default:
3043 InsertConfigString(pCfg, "Type", "HardDisk");
3044 InsertConfigInteger(pCfg, "Mountable", 0);
3045 }
3046
3047 if ( pMedium
3048 && ( enmType == DeviceType_DVD
3049 || enmType == DeviceType_Floppy
3050 ))
3051 {
3052 // if this medium represents an ISO image and this image is inaccessible,
3053 // the ignore it instead of causing a failure; this can happen when we
3054 // restore a VM state and the ISO has disappeared, e.g. because the Guest
3055 // Additions were mounted and the user upgraded VirtualBox. Previously
3056 // we failed on startup, but that's not good because the only way out then
3057 // would be to discard the VM state...
3058 MediumState_T mediumState;
3059 rc = pMedium->RefreshState(&mediumState);
3060 AssertMsgReturn(RT_SUCCESS(rc), ("rc=%Rrc\n", rc), rc);
3061
3062 if (mediumState == MediumState_Inaccessible)
3063 {
3064 Bstr loc;
3065 rc = pMedium->COMGETTER(Location)(loc.asOutParam());
3066 if (FAILED(rc)) return rc;
3067
3068 setVMRuntimeErrorCallbackF(mpVM,
3069 this,
3070 0,
3071 "DvdOrFloppyImageInaccessible",
3072 "The image file '%ls' is inaccessible and is being ignored. Please select a different image file for the virtual %s drive.",
3073 loc.raw(),
3074 (enmType == DeviceType_DVD) ? "DVD" : "floppy");
3075 pMedium = NULL;
3076 }
3077 }
3078
3079 if (pMedium)
3080 {
3081 /* Start with length of parent chain, as the list is reversed */
3082 unsigned uImage = 0;
3083 IMedium *pTmp = pMedium;
3084 while (pTmp)
3085 {
3086 uImage++;
3087 hrc = pTmp->COMGETTER(Parent)(&pTmp); H();
3088 }
3089 /* Index of last image */
3090 uImage--;
3091
3092#if 0 /* Enable for I/O debugging */
3093 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL0);
3094 InsertConfigString(pLunL0, "Driver", "DiskIntegrity");
3095 InsertConfigNode(pLunL0, "Config", &pCfg);
3096 InsertConfigInteger(pCfg, "CheckConsistency", 0);
3097 InsertConfigInteger(pCfg, "CheckDoubleCompletions", 1);
3098#endif
3099
3100 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL1);
3101 InsertConfigString(pLunL1, "Driver", "VD");
3102 InsertConfigNode(pLunL1, "Config", &pCfg);
3103
3104 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
3105 InsertConfigString(pCfg, "Path", bstr);
3106
3107 hrc = pMedium->COMGETTER(Format)(bstr.asOutParam()); H();
3108 InsertConfigString(pCfg, "Format", bstr);
3109
3110 if (mediumType == MediumType_Readonly)
3111 {
3112 InsertConfigInteger(pCfg, "ReadOnly", 1);
3113 }
3114 else if (enmType == DeviceType_Floppy)
3115 {
3116 InsertConfigInteger(pCfg, "MaybeReadOnly", 1);
3117 }
3118
3119 /* Start without exclusive write access to the images. */
3120 /** @todo Live Migration: I don't quite like this, we risk screwing up when
3121 * we're resuming the VM if some 3rd dude have any of the VDIs open
3122 * with write sharing denied. However, if the two VMs are sharing a
3123 * image it really is necessary....
3124 *
3125 * So, on the "lock-media" command, the target teleporter should also
3126 * make DrvVD undo TempReadOnly. It gets interesting if we fail after
3127 * that. Grumble. */
3128 if ( enmType == DeviceType_HardDisk
3129 && ( aMachineState == MachineState_TeleportingIn
3130 || aMachineState == MachineState_FaultTolerantSyncing))
3131 {
3132 InsertConfigInteger(pCfg, "TempReadOnly", 1);
3133 }
3134
3135 /* Flag for opening the medium for sharing between VMs. This
3136 * is done at the moment only for the first (and only) medium
3137 * in the chain, as shared media can have no diffs. */
3138 if (mediumType == MediumType_Shareable)
3139 {
3140 InsertConfigInteger(pCfg, "Shareable", 1);
3141 }
3142
3143 if (!fUseHostIOCache)
3144 {
3145 InsertConfigInteger(pCfg, "UseNewIo", 1);
3146 if (fBuiltinIoCache)
3147 InsertConfigInteger(pCfg, "BlockCache", 1);
3148 }
3149
3150 if (fSetupMerge)
3151 {
3152 InsertConfigInteger(pCfg, "SetupMerge", 1);
3153 if (uImage == uMergeSource)
3154 {
3155 InsertConfigInteger(pCfg, "MergeSource", 1);
3156 }
3157 else if (uImage == uMergeTarget)
3158 {
3159 InsertConfigInteger(pCfg, "MergeTarget", 1);
3160 }
3161 }
3162
3163 switch (enmType)
3164 {
3165 case DeviceType_DVD:
3166 InsertConfigString(pCfg, "Type", "DVD");
3167 break;
3168 case DeviceType_Floppy:
3169 InsertConfigString(pCfg, "Type", "Floppy");
3170 break;
3171 case DeviceType_HardDisk:
3172 default:
3173 InsertConfigString(pCfg, "Type", "HardDisk");
3174 }
3175
3176 if (pcszBwGroup)
3177 InsertConfigString(pCfg, "BwGroup", pcszBwGroup);
3178
3179 /* Pass all custom parameters. */
3180 bool fHostIP = true;
3181 SafeArray<BSTR> names;
3182 SafeArray<BSTR> values;
3183 hrc = pMedium->GetProperties(NULL,
3184 ComSafeArrayAsOutParam(names),
3185 ComSafeArrayAsOutParam(values)); H();
3186
3187 if (names.size() != 0)
3188 {
3189 PCFGMNODE pVDC;
3190 InsertConfigNode(pCfg, "VDConfig", &pVDC);
3191 for (size_t ii = 0; ii < names.size(); ++ii)
3192 {
3193 if (values[ii] && *values[ii])
3194 {
3195 Utf8Str name = names[ii];
3196 Utf8Str value = values[ii];
3197 InsertConfigString(pVDC, name.c_str(), value);
3198 if ( name.compare("HostIPStack") == 0
3199 && value.compare("0") == 0)
3200 fHostIP = false;
3201 }
3202 }
3203 }
3204
3205 /* Create an inverted list of parents. */
3206 uImage--;
3207 IMedium *pParentMedium = pMedium;
3208 for (PCFGMNODE pParent = pCfg;; uImage--)
3209 {
3210 hrc = pParentMedium->COMGETTER(Parent)(&pMedium); H();
3211 if (!pMedium)
3212 break;
3213
3214 PCFGMNODE pCur;
3215 InsertConfigNode(pParent, "Parent", &pCur);
3216 hrc = pMedium->COMGETTER(Location)(bstr.asOutParam()); H();
3217 InsertConfigString(pCur, "Path", bstr);
3218
3219 hrc = pMedium->COMGETTER(Format)(bstr.asOutParam()); H();
3220 InsertConfigString(pCur, "Format", bstr);
3221
3222 if (fSetupMerge)
3223 {
3224 if (uImage == uMergeSource)
3225 {
3226 InsertConfigInteger(pCur, "MergeSource", 1);
3227 }
3228 else if (uImage == uMergeTarget)
3229 {
3230 InsertConfigInteger(pCur, "MergeTarget", 1);
3231 }
3232 }
3233
3234 /* Pass all custom parameters. */
3235 SafeArray<BSTR> aNames;
3236 SafeArray<BSTR> aValues;
3237 hrc = pMedium->GetProperties(NULL,
3238 ComSafeArrayAsOutParam(aNames),
3239 ComSafeArrayAsOutParam(aValues)); H();
3240
3241 if (aNames.size() != 0)
3242 {
3243 PCFGMNODE pVDC;
3244 InsertConfigNode(pCur, "VDConfig", &pVDC);
3245 for (size_t ii = 0; ii < aNames.size(); ++ii)
3246 {
3247 if (aValues[ii] && *aValues[ii])
3248 {
3249 Utf8Str name = aNames[ii];
3250 Utf8Str value = aValues[ii];
3251 InsertConfigString(pVDC, name.c_str(), value);
3252 if ( name.compare("HostIPStack") == 0
3253 && value.compare("0") == 0)
3254 fHostIP = false;
3255 }
3256 }
3257 }
3258
3259 /* Custom code: put marker to not use host IP stack to driver
3260 * configuration node. Simplifies life of DrvVD a bit. */
3261 if (!fHostIP)
3262 {
3263 InsertConfigInteger(pCfg, "HostIPStack", 0);
3264 }
3265
3266 /* next */
3267 pParent = pCur;
3268 pParentMedium = pMedium;
3269 }
3270 }
3271 }
3272 }
3273 catch (ConfigError &x)
3274 {
3275 // InsertConfig threw something:
3276 return x.m_vrc;
3277 }
3278
3279#undef H
3280
3281 return VINF_SUCCESS;
3282}
3283
3284/**
3285 * Construct the Network configuration tree
3286 *
3287 * @returns VBox status code.
3288 *
3289 * @param pszDevice The PDM device name.
3290 * @param uInstance The PDM device instance.
3291 * @param uLun The PDM LUN number of the drive.
3292 * @param aNetworkAdapter The network adapter whose attachment needs to be changed
3293 * @param pCfg Configuration node for the device
3294 * @param pLunL0 To store the pointer to the LUN#0.
3295 * @param pInst The instance CFGM node
3296 * @param fAttachDetach To determine if the network attachment should
3297 * be attached/detached after/before
3298 * configuration.
3299 * @param fIgnoreConnectFailure
3300 * True if connection failures should be ignored
3301 * (makes only sense for bridged/host-only networks).
3302 *
3303 * @note Locks this object for writing.
3304 */
3305int Console::configNetwork(const char *pszDevice,
3306 unsigned uInstance,
3307 unsigned uLun,
3308 INetworkAdapter *aNetworkAdapter,
3309 PCFGMNODE pCfg,
3310 PCFGMNODE pLunL0,
3311 PCFGMNODE pInst,
3312 bool fAttachDetach,
3313 bool fIgnoreConnectFailure)
3314{
3315 AutoCaller autoCaller(this);
3316 AssertComRCReturn(autoCaller.rc(), VERR_ACCESS_DENIED);
3317
3318 // InsertConfig* throws
3319 try
3320 {
3321 int rc = VINF_SUCCESS;
3322 HRESULT hrc;
3323 Bstr bstr;
3324
3325#define H() AssertMsgReturn(!FAILED(hrc), ("hrc=%Rhrc\n", hrc), VERR_GENERAL_FAILURE)
3326
3327 /*
3328 * Locking the object before doing VMR3* calls is quite safe here, since
3329 * we're on EMT. Write lock is necessary because we indirectly modify the
3330 * meAttachmentType member.
3331 */
3332 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3333
3334 PVM pVM = mpVM;
3335
3336 ComPtr<IMachine> pMachine = machine();
3337
3338 ComPtr<IVirtualBox> virtualBox;
3339 hrc = pMachine->COMGETTER(Parent)(virtualBox.asOutParam());
3340 H();
3341
3342 ComPtr<IHost> host;
3343 hrc = virtualBox->COMGETTER(Host)(host.asOutParam());
3344 H();
3345
3346 BOOL fSniffer;
3347 hrc = aNetworkAdapter->COMGETTER(TraceEnabled)(&fSniffer);
3348 H();
3349
3350 if (fAttachDetach && fSniffer)
3351 {
3352 const char *pszNetDriver = "IntNet";
3353 if (meAttachmentType[uInstance] == NetworkAttachmentType_NAT)
3354 pszNetDriver = "NAT";
3355#if !defined(VBOX_WITH_NETFLT) && defined(RT_OS_LINUX)
3356 if (meAttachmentType[uInstance] == NetworkAttachmentType_Bridged)
3357 pszNetDriver = "HostInterface";
3358#endif
3359
3360 rc = PDMR3DriverDetach(pVM, pszDevice, uInstance, uLun, pszNetDriver, 0, 0 /*fFlags*/);
3361 if (rc == VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN)
3362 rc = VINF_SUCCESS;
3363 AssertLogRelRCReturn(rc, rc);
3364
3365 pLunL0 = CFGMR3GetChildF(pInst, "LUN#%u", uLun);
3366 PCFGMNODE pLunAD = CFGMR3GetChildF(pLunL0, "AttachedDriver");
3367 if (pLunAD)
3368 {
3369 CFGMR3RemoveNode(pLunAD);
3370 }
3371 else
3372 {
3373 CFGMR3RemoveNode(pLunL0);
3374 InsertConfigNode(pInst, "LUN#0", &pLunL0);
3375 InsertConfigString(pLunL0, "Driver", "NetSniffer");
3376 InsertConfigNode(pLunL0, "Config", &pCfg);
3377 hrc = aNetworkAdapter->COMGETTER(TraceFile)(bstr.asOutParam()); H();
3378 if (!bstr.isEmpty()) /* check convention for indicating default file. */
3379 InsertConfigString(pCfg, "File", bstr);
3380 }
3381 }
3382 else if (fAttachDetach && !fSniffer)
3383 {
3384 rc = PDMR3DeviceDetach(pVM, pszDevice, uInstance, uLun, 0 /*fFlags*/);
3385 if (rc == VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN)
3386 rc = VINF_SUCCESS;
3387 AssertLogRelRCReturn(rc, rc);
3388
3389 /* nuke anything which might have been left behind. */
3390 CFGMR3RemoveNode(CFGMR3GetChildF(pInst, "LUN#%u", uLun));
3391 }
3392 else if (!fAttachDetach && fSniffer)
3393 {
3394 /* insert the sniffer filter driver. */
3395 InsertConfigNode(pInst, "LUN#0", &pLunL0);
3396 InsertConfigString(pLunL0, "Driver", "NetSniffer");
3397 InsertConfigNode(pLunL0, "Config", &pCfg);
3398 hrc = aNetworkAdapter->COMGETTER(TraceFile)(bstr.asOutParam()); H();
3399 if (!bstr.isEmpty()) /* check convention for indicating default file. */
3400 InsertConfigString(pCfg, "File", bstr);
3401 }
3402
3403 Bstr networkName, trunkName, trunkType;
3404 NetworkAttachmentType_T eAttachmentType;
3405 hrc = aNetworkAdapter->COMGETTER(AttachmentType)(&eAttachmentType); H();
3406 switch (eAttachmentType)
3407 {
3408 case NetworkAttachmentType_Null:
3409 break;
3410
3411 case NetworkAttachmentType_NAT:
3412 {
3413 ComPtr<INATEngine> natDriver;
3414 hrc = aNetworkAdapter->COMGETTER(NatDriver)(natDriver.asOutParam()); H();
3415 if (fSniffer)
3416 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL0);
3417 else
3418 InsertConfigNode(pInst, "LUN#0", &pLunL0);
3419 InsertConfigString(pLunL0, "Driver", "NAT");
3420 InsertConfigNode(pLunL0, "Config", &pCfg);
3421
3422 /* Configure TFTP prefix and boot filename. */
3423 hrc = virtualBox->COMGETTER(HomeFolder)(bstr.asOutParam()); H();
3424 if (!bstr.isEmpty())
3425 InsertConfigString(pCfg, "TFTPPrefix", Utf8StrFmt("%ls%c%s", bstr.raw(), RTPATH_DELIMITER, "TFTP"));
3426 hrc = pMachine->COMGETTER(Name)(bstr.asOutParam()); H();
3427 InsertConfigString(pCfg, "BootFile", Utf8StrFmt("%ls.pxe", bstr.raw()));
3428
3429 hrc = natDriver->COMGETTER(Network)(bstr.asOutParam()); H();
3430 if (!bstr.isEmpty())
3431 InsertConfigString(pCfg, "Network", bstr);
3432 else
3433 {
3434 ULONG uSlot;
3435 hrc = aNetworkAdapter->COMGETTER(Slot)(&uSlot); H();
3436 InsertConfigString(pCfg, "Network", Utf8StrFmt("10.0.%d.0/24", uSlot+2));
3437 }
3438 hrc = natDriver->COMGETTER(HostIP)(bstr.asOutParam()); H();
3439 if (!bstr.isEmpty())
3440 InsertConfigString(pCfg, "BindIP", bstr);
3441 ULONG mtu = 0;
3442 ULONG sockSnd = 0;
3443 ULONG sockRcv = 0;
3444 ULONG tcpSnd = 0;
3445 ULONG tcpRcv = 0;
3446 hrc = natDriver->GetNetworkSettings(&mtu, &sockSnd, &sockRcv, &tcpSnd, &tcpRcv); H();
3447 if (mtu)
3448 InsertConfigInteger(pCfg, "SlirpMTU", mtu);
3449 if (sockRcv)
3450 InsertConfigInteger(pCfg, "SockRcv", sockRcv);
3451 if (sockSnd)
3452 InsertConfigInteger(pCfg, "SockSnd", sockSnd);
3453 if (tcpRcv)
3454 InsertConfigInteger(pCfg, "TcpRcv", tcpRcv);
3455 if (tcpSnd)
3456 InsertConfigInteger(pCfg, "TcpSnd", tcpSnd);
3457 hrc = natDriver->COMGETTER(TftpPrefix)(bstr.asOutParam()); H();
3458 if (!bstr.isEmpty())
3459 {
3460 RemoveConfigValue(pCfg, "TFTPPrefix");
3461 InsertConfigString(pCfg, "TFTPPrefix", bstr);
3462 }
3463 hrc = natDriver->COMGETTER(TftpBootFile)(bstr.asOutParam()); H();
3464 if (!bstr.isEmpty())
3465 {
3466 RemoveConfigValue(pCfg, "BootFile");
3467 InsertConfigString(pCfg, "BootFile", bstr);
3468 }
3469 hrc = natDriver->COMGETTER(TftpNextServer)(bstr.asOutParam()); H();
3470 if (!bstr.isEmpty())
3471 InsertConfigString(pCfg, "NextServer", bstr);
3472 BOOL fDnsFlag;
3473 hrc = natDriver->COMGETTER(DnsPassDomain)(&fDnsFlag); H();
3474 InsertConfigInteger(pCfg, "PassDomain", fDnsFlag);
3475 hrc = natDriver->COMGETTER(DnsProxy)(&fDnsFlag); H();
3476 InsertConfigInteger(pCfg, "DNSProxy", fDnsFlag);
3477 hrc = natDriver->COMGETTER(DnsUseHostResolver)(&fDnsFlag); H();
3478 InsertConfigInteger(pCfg, "UseHostResolver", fDnsFlag);
3479
3480 ULONG aliasMode;
3481 hrc = natDriver->COMGETTER(AliasMode)(&aliasMode); H();
3482 InsertConfigInteger(pCfg, "AliasMode", aliasMode);
3483
3484 /* port-forwarding */
3485 SafeArray<BSTR> pfs;
3486 hrc = natDriver->COMGETTER(Redirects)(ComSafeArrayAsOutParam(pfs)); H();
3487 PCFGMNODE pPF = NULL; /* /Devices/Dev/.../Config/PF#0/ */
3488 for (unsigned int i = 0; i < pfs.size(); ++i)
3489 {
3490 uint16_t port = 0;
3491 BSTR r = pfs[i];
3492 Utf8Str utf = Utf8Str(r);
3493 Utf8Str strName;
3494 Utf8Str strProto;
3495 Utf8Str strHostPort;
3496 Utf8Str strHostIP;
3497 Utf8Str strGuestPort;
3498 Utf8Str strGuestIP;
3499 size_t pos, ppos;
3500 pos = ppos = 0;
3501#define ITERATE_TO_NEXT_TERM(res, str, pos, ppos) \
3502 do { \
3503 pos = str.find(",", ppos); \
3504 if (pos == Utf8Str::npos) \
3505 { \
3506 Log(( #res " extracting from %s is failed\n", str.c_str())); \
3507 continue; \
3508 } \
3509 res = str.substr(ppos, pos - ppos); \
3510 Log2((#res " %s pos:%d, ppos:%d\n", res.c_str(), pos, ppos)); \
3511 ppos = pos + 1; \
3512 } while (0)
3513 ITERATE_TO_NEXT_TERM(strName, utf, pos, ppos);
3514 ITERATE_TO_NEXT_TERM(strProto, utf, pos, ppos);
3515 ITERATE_TO_NEXT_TERM(strHostIP, utf, pos, ppos);
3516 ITERATE_TO_NEXT_TERM(strHostPort, utf, pos, ppos);
3517 ITERATE_TO_NEXT_TERM(strGuestIP, utf, pos, ppos);
3518 strGuestPort = utf.substr(ppos, utf.length() - ppos);
3519#undef ITERATE_TO_NEXT_TERM
3520
3521 uint32_t proto = strProto.toUInt32();
3522 bool fValid = true;
3523 switch (proto)
3524 {
3525 case NATProtocol_UDP:
3526 strProto = "UDP";
3527 break;
3528 case NATProtocol_TCP:
3529 strProto = "TCP";
3530 break;
3531 default:
3532 fValid = false;
3533 }
3534 /* continue with next rule if no valid proto was passed */
3535 if (!fValid)
3536 continue;
3537
3538 InsertConfigNode(pCfg, strName.c_str(), &pPF);
3539 InsertConfigString(pPF, "Protocol", strProto);
3540
3541 if (!strHostIP.isEmpty())
3542 InsertConfigString(pPF, "BindIP", strHostIP);
3543
3544 if (!strGuestIP.isEmpty())
3545 InsertConfigString(pPF, "GuestIP", strGuestIP);
3546
3547 port = RTStrToUInt16(strHostPort.c_str());
3548 if (port)
3549 InsertConfigInteger(pPF, "HostPort", port);
3550
3551 port = RTStrToUInt16(strGuestPort.c_str());
3552 if (port)
3553 InsertConfigInteger(pPF, "GuestPort", port);
3554 }
3555 break;
3556 }
3557
3558 case NetworkAttachmentType_Bridged:
3559 {
3560#if (defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)) && !defined(VBOX_WITH_NETFLT)
3561 hrc = attachToTapInterface(aNetworkAdapter);
3562 if (FAILED(hrc))
3563 {
3564 switch (hrc)
3565 {
3566 case VERR_ACCESS_DENIED:
3567 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
3568 "Failed to open '/dev/net/tun' for read/write access. Please check the "
3569 "permissions of that node. Either run 'chmod 0666 /dev/net/tun' or "
3570 "change the group of that node and make yourself a member of that group. Make "
3571 "sure that these changes are permanent, especially if you are "
3572 "using udev"));
3573 default:
3574 AssertMsgFailed(("Could not attach to host interface! Bad!\n"));
3575 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
3576 "Failed to initialize Host Interface Networking"));
3577 }
3578 }
3579
3580 Assert((int)maTapFD[uInstance] >= 0);
3581 if ((int)maTapFD[uInstance] >= 0)
3582 {
3583 if (fSniffer)
3584 {
3585 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL0);
3586 }
3587 else
3588 {
3589 InsertConfigNode(pInst, "LUN#0", &pLunL0);
3590 }
3591 InsertConfigString(pLunL0, "Driver", "HostInterface");
3592 InsertConfigNode(pLunL0, "Config", &pCfg);
3593 InsertConfigInteger(pCfg, "FileHandle", maTapFD[uInstance]);
3594 }
3595
3596#elif defined(VBOX_WITH_NETFLT)
3597 /*
3598 * This is the new VBoxNetFlt+IntNet stuff.
3599 */
3600 if (fSniffer)
3601 {
3602 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL0);
3603 }
3604 else
3605 {
3606 InsertConfigNode(pInst, "LUN#0", &pLunL0);
3607 }
3608
3609 Bstr HifName;
3610 hrc = aNetworkAdapter->COMGETTER(HostInterface)(HifName.asOutParam());
3611 if (FAILED(hrc))
3612 {
3613 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(HostInterface) failed, hrc (0x%x)", hrc));
3614 H();
3615 }
3616
3617 Utf8Str HifNameUtf8(HifName);
3618 const char *pszHifName = HifNameUtf8.c_str();
3619
3620# if defined(RT_OS_DARWIN)
3621 /* The name is on the form 'ifX: long name', chop it off at the colon. */
3622 char szTrunk[8];
3623 strncpy(szTrunk, pszHifName, sizeof(szTrunk));
3624 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk));
3625 if (!pszColon)
3626 {
3627 /*
3628 * Dynamic changing of attachment causes an attempt to configure
3629 * network with invalid host adapter (as it is must be changed before
3630 * the attachment), calling Detach here will cause a deadlock.
3631 * See #4750.
3632 * hrc = aNetworkAdapter->Detach(); H();
3633 */
3634 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
3635 N_("Malformed host interface networking name '%ls'"),
3636 HifName.raw());
3637 }
3638 *pszColon = '\0';
3639 const char *pszTrunk = szTrunk;
3640
3641# elif defined(RT_OS_SOLARIS)
3642 /* The name is on the form format 'ifX[:1] - long name, chop it off at space. */
3643 char szTrunk[256];
3644 strlcpy(szTrunk, pszHifName, sizeof(szTrunk));
3645 char *pszSpace = (char *)memchr(szTrunk, ' ', sizeof(szTrunk));
3646
3647 /*
3648 * Currently don't bother about malformed names here for the sake of people using
3649 * VBoxManage and setting only the NIC name from there. If there is a space we
3650 * chop it off and proceed, otherwise just use whatever we've got.
3651 */
3652 if (pszSpace)
3653 *pszSpace = '\0';
3654
3655 /* Chop it off at the colon (zone naming eg: e1000g:1 we need only the e1000g) */
3656 char *pszColon = (char *)memchr(szTrunk, ':', sizeof(szTrunk));
3657 if (pszColon)
3658 *pszColon = '\0';
3659
3660 const char *pszTrunk = szTrunk;
3661
3662# elif defined(RT_OS_WINDOWS)
3663 ComPtr<IHostNetworkInterface> hostInterface;
3664 hrc = host->FindHostNetworkInterfaceByName(HifName.raw(),
3665 hostInterface.asOutParam());
3666 if (!SUCCEEDED(hrc))
3667 {
3668 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: FindByName failed, rc=%Rhrc (0x%x)", hrc, hrc));
3669 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
3670 N_("Nonexistent host networking interface, name '%ls'"),
3671 HifName.raw());
3672 }
3673
3674 HostNetworkInterfaceType_T eIfType;
3675 hrc = hostInterface->COMGETTER(InterfaceType)(&eIfType);
3676 if (FAILED(hrc))
3677 {
3678 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(InterfaceType) failed, hrc (0x%x)", hrc));
3679 H();
3680 }
3681
3682 if (eIfType != HostNetworkInterfaceType_Bridged)
3683 {
3684 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
3685 N_("Interface ('%ls') is not a Bridged Adapter interface"),
3686 HifName.raw());
3687 }
3688
3689 hrc = hostInterface->COMGETTER(Id)(bstr.asOutParam());
3690 if (FAILED(hrc))
3691 {
3692 LogRel(("NetworkAttachmentType_Bridged: COMGETTER(Id) failed, hrc (0x%x)", hrc));
3693 H();
3694 }
3695 Guid hostIFGuid(bstr);
3696
3697 INetCfg *pNc;
3698 ComPtr<INetCfgComponent> pAdaptorComponent;
3699 LPWSTR pszApp;
3700 int rc = VERR_INTNET_FLT_IF_NOT_FOUND;
3701
3702 hrc = VBoxNetCfgWinQueryINetCfg(FALSE /*fGetWriteLock*/,
3703 L"VirtualBox",
3704 &pNc,
3705 &pszApp);
3706 Assert(hrc == S_OK);
3707 if (hrc == S_OK)
3708 {
3709 /* get the adapter's INetCfgComponent*/
3710 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.raw(), pAdaptorComponent.asOutParam());
3711 if (hrc != S_OK)
3712 {
3713 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3714 LogRel(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc));
3715 H();
3716 }
3717 }
3718#define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\"
3719 char szTrunkName[INTNET_MAX_TRUNK_NAME];
3720 char *pszTrunkName = szTrunkName;
3721 wchar_t * pswzBindName;
3722 hrc = pAdaptorComponent->GetBindName(&pswzBindName);
3723 Assert(hrc == S_OK);
3724 if (hrc == S_OK)
3725 {
3726 int cwBindName = (int)wcslen(pswzBindName) + 1;
3727 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX);
3728 if (sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName)
3729 {
3730 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX);
3731 pszTrunkName += cbFullBindNamePrefix-1;
3732 if (!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName,
3733 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL))
3734 {
3735 DWORD err = GetLastError();
3736 hrc = HRESULT_FROM_WIN32(err);
3737 AssertMsgFailed(("%hrc=%Rhrc %#x\n", hrc, hrc));
3738 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: WideCharToMultiByte failed, hr=%Rhrc (0x%x) err=%u\n", hrc, hrc, err));
3739 }
3740 }
3741 else
3742 {
3743 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: insufficient szTrunkName buffer space\n"));
3744 /** @todo set appropriate error code */
3745 hrc = E_FAIL;
3746 }
3747
3748 if (hrc != S_OK)
3749 {
3750 AssertFailed();
3751 CoTaskMemFree(pswzBindName);
3752 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3753 H();
3754 }
3755
3756 /* we're not freeing the bind name since we'll use it later for detecting wireless*/
3757 }
3758 else
3759 {
3760 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3761 AssertLogRelMsgFailed(("NetworkAttachmentType_Bridged: VBoxNetCfgWinGetComponentByGuid failed, hrc (0x%x)", hrc));
3762 H();
3763 }
3764 const char *pszTrunk = szTrunkName;
3765 /* we're not releasing the INetCfg stuff here since we use it later to figure out whether it is wireless */
3766
3767# elif defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)
3768# if defined(RT_OS_FREEBSD)
3769 /*
3770 * If we bridge to a tap interface open it the `old' direct way.
3771 * This works and performs better than bridging a physical
3772 * interface via the current FreeBSD vboxnetflt implementation.
3773 */
3774 if (!strncmp(pszHifName, "tap", sizeof "tap" - 1)) {
3775 hrc = attachToTapInterface(aNetworkAdapter);
3776 if (FAILED(hrc))
3777 {
3778 switch (hrc)
3779 {
3780 case VERR_ACCESS_DENIED:
3781 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
3782 "Failed to open '/dev/%s' for read/write access. Please check the "
3783 "permissions of that node, and that the net.link.tap.user_open "
3784 "sysctl is set. Either run 'chmod 0666 /dev/%s' or "
3785 "change the group of that node to vboxusers and make yourself "
3786 "a member of that group. Make sure that these changes are permanent."), pszHifName, pszHifName);
3787 default:
3788 AssertMsgFailed(("Could not attach to tap interface! Bad!\n"));
3789 return VMSetError(pVM, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS, N_(
3790 "Failed to initialize Host Interface Networking"));
3791 }
3792 }
3793
3794 Assert((int)maTapFD[uInstance] >= 0);
3795 if ((int)maTapFD[uInstance] >= 0)
3796 {
3797 InsertConfigString(pLunL0, "Driver", "HostInterface");
3798 InsertConfigNode(pLunL0, "Config", &pCfg);
3799 InsertConfigInteger(pCfg, "FileHandle", maTapFD[uInstance]);
3800 }
3801 break;
3802 }
3803# endif
3804 /** @todo Check for malformed names. */
3805 const char *pszTrunk = pszHifName;
3806
3807 /* Issue a warning if the interface is down */
3808 {
3809 int iSock = socket(AF_INET, SOCK_DGRAM, 0);
3810 if (iSock >= 0)
3811 {
3812 struct ifreq Req;
3813
3814 memset(&Req, 0, sizeof(Req));
3815 strncpy(Req.ifr_name, pszHifName, sizeof(Req.ifr_name) - 1);
3816 if (ioctl(iSock, SIOCGIFFLAGS, &Req) >= 0)
3817 if ((Req.ifr_flags & IFF_UP) == 0)
3818 {
3819 setVMRuntimeErrorCallbackF(pVM, this, 0, "BridgedInterfaceDown", "Bridged interface %s is down. Guest will not be able to use this interface", pszHifName);
3820 }
3821
3822 close(iSock);
3823 }
3824 }
3825
3826# else
3827# error "PORTME (VBOX_WITH_NETFLT)"
3828# endif
3829
3830 InsertConfigString(pLunL0, "Driver", "IntNet");
3831 InsertConfigNode(pLunL0, "Config", &pCfg);
3832 InsertConfigString(pCfg, "Trunk", pszTrunk);
3833 InsertConfigInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt);
3834 InsertConfigInteger(pCfg, "IgnoreConnectFailure", (uint64_t)fIgnoreConnectFailure);
3835 char szNetwork[INTNET_MAX_NETWORK_NAME];
3836 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHifName);
3837 InsertConfigString(pCfg, "Network", szNetwork);
3838 networkName = Bstr(szNetwork);
3839 trunkName = Bstr(pszTrunk);
3840 trunkType = Bstr(TRUNKTYPE_NETFLT);
3841
3842# if defined(RT_OS_DARWIN)
3843 /** @todo Come up with a better deal here. Problem is that IHostNetworkInterface is completely useless here. */
3844 if ( strstr(pszHifName, "Wireless")
3845 || strstr(pszHifName, "AirPort" ))
3846 InsertConfigInteger(pCfg, "SharedMacOnWire", true);
3847# elif defined(RT_OS_LINUX)
3848 int iSock = socket(AF_INET, SOCK_DGRAM, 0);
3849 if (iSock >= 0)
3850 {
3851 struct iwreq WRq;
3852
3853 memset(&WRq, 0, sizeof(WRq));
3854 strncpy(WRq.ifr_name, pszHifName, IFNAMSIZ);
3855 bool fSharedMacOnWire = ioctl(iSock, SIOCGIWNAME, &WRq) >= 0;
3856 close(iSock);
3857 if (fSharedMacOnWire)
3858 {
3859 InsertConfigInteger(pCfg, "SharedMacOnWire", true);
3860 Log(("Set SharedMacOnWire\n"));
3861 }
3862 else
3863 Log(("Failed to get wireless name\n"));
3864 }
3865 else
3866 Log(("Failed to open wireless socket\n"));
3867# elif defined(RT_OS_FREEBSD)
3868 int iSock = socket(AF_INET, SOCK_DGRAM, 0);
3869 if (iSock >= 0)
3870 {
3871 struct ieee80211req WReq;
3872 uint8_t abData[32];
3873
3874 memset(&WReq, 0, sizeof(WReq));
3875 strncpy(WReq.i_name, pszHifName, sizeof(WReq.i_name));
3876 WReq.i_type = IEEE80211_IOC_SSID;
3877 WReq.i_val = -1;
3878 WReq.i_data = abData;
3879 WReq.i_len = sizeof(abData);
3880
3881 bool fSharedMacOnWire = ioctl(iSock, SIOCG80211, &WReq) >= 0;
3882 close(iSock);
3883 if (fSharedMacOnWire)
3884 {
3885 InsertConfigInteger(pCfg, "SharedMacOnWire", true);
3886 Log(("Set SharedMacOnWire\n"));
3887 }
3888 else
3889 Log(("Failed to get wireless name\n"));
3890 }
3891 else
3892 Log(("Failed to open wireless socket\n"));
3893# elif defined(RT_OS_WINDOWS)
3894# define DEVNAME_PREFIX L"\\\\.\\"
3895 /* we are getting the medium type via IOCTL_NDIS_QUERY_GLOBAL_STATS Io Control
3896 * there is a pretty long way till there though since we need to obtain the symbolic link name
3897 * for the adapter device we are going to query given the device Guid */
3898
3899
3900 /* prepend the "\\\\.\\" to the bind name to obtain the link name */
3901
3902 wchar_t FileName[MAX_PATH];
3903 wcscpy(FileName, DEVNAME_PREFIX);
3904 wcscpy((wchar_t*)(((char*)FileName) + sizeof(DEVNAME_PREFIX) - sizeof(FileName[0])), pswzBindName);
3905
3906 /* open the device */
3907 HANDLE hDevice = CreateFile(FileName,
3908 GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
3909 NULL,
3910 OPEN_EXISTING,
3911 FILE_ATTRIBUTE_NORMAL,
3912 NULL);
3913
3914 if (hDevice != INVALID_HANDLE_VALUE)
3915 {
3916 bool fSharedMacOnWire = false;
3917
3918 /* now issue the OID_GEN_PHYSICAL_MEDIUM query */
3919 DWORD Oid = OID_GEN_PHYSICAL_MEDIUM;
3920 NDIS_PHYSICAL_MEDIUM PhMedium;
3921 DWORD cbResult;
3922 if (DeviceIoControl(hDevice,
3923 IOCTL_NDIS_QUERY_GLOBAL_STATS,
3924 &Oid,
3925 sizeof(Oid),
3926 &PhMedium,
3927 sizeof(PhMedium),
3928 &cbResult,
3929 NULL))
3930 {
3931 /* that was simple, now examine PhMedium */
3932 if ( PhMedium == NdisPhysicalMediumWirelessWan
3933 || PhMedium == NdisPhysicalMediumWirelessLan
3934 || PhMedium == NdisPhysicalMediumNative802_11
3935 || PhMedium == NdisPhysicalMediumBluetooth)
3936 fSharedMacOnWire = true;
3937 }
3938 else
3939 {
3940 int winEr = GetLastError();
3941 LogRel(("Console::configNetwork: DeviceIoControl failed, err (0x%x), ignoring\n", winEr));
3942 Assert(winEr == ERROR_INVALID_PARAMETER || winEr == ERROR_NOT_SUPPORTED || winEr == ERROR_BAD_COMMAND);
3943 }
3944 CloseHandle(hDevice);
3945
3946 if (fSharedMacOnWire)
3947 {
3948 Log(("this is a wireless adapter"));
3949 InsertConfigInteger(pCfg, "SharedMacOnWire", true);
3950 Log(("Set SharedMacOnWire\n"));
3951 }
3952 else
3953 Log(("this is NOT a wireless adapter"));
3954 }
3955 else
3956 {
3957 int winEr = GetLastError();
3958 AssertLogRelMsgFailed(("Console::configNetwork: CreateFile failed, err (0x%x), ignoring\n", winEr));
3959 }
3960
3961 CoTaskMemFree(pswzBindName);
3962
3963 pAdaptorComponent.setNull();
3964 /* release the pNc finally */
3965 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
3966# else
3967 /** @todo PORTME: wireless detection */
3968# endif
3969
3970# if defined(RT_OS_SOLARIS)
3971# if 0 /* bird: this is a bit questionable and might cause more trouble than its worth. */
3972 /* Zone access restriction, don't allow snooping the global zone. */
3973 zoneid_t ZoneId = getzoneid();
3974 if (ZoneId != GLOBAL_ZONEID)
3975 {
3976 InsertConfigInteger(pCfg, "IgnoreAllPromisc", true);
3977 }
3978# endif
3979# endif
3980
3981#elif defined(RT_OS_WINDOWS) /* not defined NetFlt */
3982 /* NOTHING TO DO HERE */
3983#elif defined(RT_OS_LINUX)
3984/// @todo aleksey: is there anything to be done here?
3985#elif defined(RT_OS_FREEBSD)
3986/** @todo FreeBSD: Check out this later (HIF networking). */
3987#else
3988# error "Port me"
3989#endif
3990 break;
3991 }
3992
3993 case NetworkAttachmentType_Internal:
3994 {
3995 hrc = aNetworkAdapter->COMGETTER(InternalNetwork)(bstr.asOutParam()); H();
3996 if (!bstr.isEmpty())
3997 {
3998 if (fSniffer)
3999 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL0);
4000 else
4001 InsertConfigNode(pInst, "LUN#0", &pLunL0);
4002 InsertConfigString(pLunL0, "Driver", "IntNet");
4003 InsertConfigNode(pLunL0, "Config", &pCfg);
4004 InsertConfigString(pCfg, "Network", bstr);
4005 InsertConfigInteger(pCfg, "TrunkType", kIntNetTrunkType_WhateverNone);
4006 networkName = bstr;
4007 trunkType = Bstr(TRUNKTYPE_WHATEVER);
4008 }
4009 break;
4010 }
4011
4012 case NetworkAttachmentType_HostOnly:
4013 {
4014 if (fSniffer)
4015 InsertConfigNode(pLunL0, "AttachedDriver", &pLunL0);
4016 else
4017 InsertConfigNode(pInst, "LUN#0", &pLunL0);
4018
4019 InsertConfigString(pLunL0, "Driver", "IntNet");
4020 InsertConfigNode(pLunL0, "Config", &pCfg);
4021
4022 Bstr HifName;
4023 hrc = aNetworkAdapter->COMGETTER(HostInterface)(HifName.asOutParam());
4024 if (FAILED(hrc))
4025 {
4026 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(HostInterface) failed, hrc (0x%x)\n", hrc));
4027 H();
4028 }
4029
4030 Utf8Str HifNameUtf8(HifName);
4031 const char *pszHifName = HifNameUtf8.c_str();
4032 ComPtr<IHostNetworkInterface> hostInterface;
4033 rc = host->FindHostNetworkInterfaceByName(HifName.raw(),
4034 hostInterface.asOutParam());
4035 if (!SUCCEEDED(rc))
4036 {
4037 LogRel(("NetworkAttachmentType_HostOnly: FindByName failed, rc (0x%x)\n", rc));
4038 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
4039 N_("Nonexistent host networking interface, name '%ls'"),
4040 HifName.raw());
4041 }
4042
4043 char szNetwork[INTNET_MAX_NETWORK_NAME];
4044 RTStrPrintf(szNetwork, sizeof(szNetwork), "HostInterfaceNetworking-%s", pszHifName);
4045
4046#if defined(RT_OS_WINDOWS)
4047# ifndef VBOX_WITH_NETFLT
4048 hrc = E_NOTIMPL;
4049 LogRel(("NetworkAttachmentType_HostOnly: Not Implemented\n"));
4050 H();
4051# else /* defined VBOX_WITH_NETFLT*/
4052 /** @todo r=bird: Put this in a function. */
4053
4054 HostNetworkInterfaceType_T eIfType;
4055 hrc = hostInterface->COMGETTER(InterfaceType)(&eIfType);
4056 if (FAILED(hrc))
4057 {
4058 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(InterfaceType) failed, hrc (0x%x)\n", hrc));
4059 H();
4060 }
4061
4062 if (eIfType != HostNetworkInterfaceType_HostOnly)
4063 return VMSetError(pVM, VERR_INTERNAL_ERROR, RT_SRC_POS,
4064 N_("Interface ('%ls') is not a Host-Only Adapter interface"),
4065 HifName.raw());
4066
4067 hrc = hostInterface->COMGETTER(Id)(bstr.asOutParam());
4068 if (FAILED(hrc))
4069 {
4070 LogRel(("NetworkAttachmentType_HostOnly: COMGETTER(Id) failed, hrc (0x%x)\n", hrc));
4071 H();
4072 }
4073 Guid hostIFGuid(bstr);
4074
4075 INetCfg *pNc;
4076 ComPtr<INetCfgComponent> pAdaptorComponent;
4077 LPWSTR pszApp;
4078 rc = VERR_INTNET_FLT_IF_NOT_FOUND;
4079
4080 hrc = VBoxNetCfgWinQueryINetCfg(FALSE,
4081 L"VirtualBox",
4082 &pNc,
4083 &pszApp);
4084 Assert(hrc == S_OK);
4085 if (hrc == S_OK)
4086 {
4087 /* get the adapter's INetCfgComponent*/
4088 hrc = VBoxNetCfgWinGetComponentByGuid(pNc, &GUID_DEVCLASS_NET, (GUID*)hostIFGuid.raw(), pAdaptorComponent.asOutParam());
4089 if (hrc != S_OK)
4090 {
4091 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
4092 LogRel(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc=%Rhrc (0x%x)\n", hrc, hrc));
4093 H();
4094 }
4095 }
4096#define VBOX_WIN_BINDNAME_PREFIX "\\DEVICE\\"
4097 char szTrunkName[INTNET_MAX_TRUNK_NAME];
4098 char *pszTrunkName = szTrunkName;
4099 wchar_t * pswzBindName;
4100 hrc = pAdaptorComponent->GetBindName(&pswzBindName);
4101 Assert(hrc == S_OK);
4102 if (hrc == S_OK)
4103 {
4104 int cwBindName = (int)wcslen(pswzBindName) + 1;
4105 int cbFullBindNamePrefix = sizeof(VBOX_WIN_BINDNAME_PREFIX);
4106 if (sizeof(szTrunkName) > cbFullBindNamePrefix + cwBindName)
4107 {
4108 strcpy(szTrunkName, VBOX_WIN_BINDNAME_PREFIX);
4109 pszTrunkName += cbFullBindNamePrefix-1;
4110 if (!WideCharToMultiByte(CP_ACP, 0, pswzBindName, cwBindName, pszTrunkName,
4111 sizeof(szTrunkName) - cbFullBindNamePrefix + 1, NULL, NULL))
4112 {
4113 DWORD err = GetLastError();
4114 hrc = HRESULT_FROM_WIN32(err);
4115 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: WideCharToMultiByte failed, hr=%Rhrc (0x%x) err=%u\n", hrc, hrc, err));
4116 }
4117 }
4118 else
4119 {
4120 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: insufficient szTrunkName buffer space\n"));
4121 /** @todo set appropriate error code */
4122 hrc = E_FAIL;
4123 }
4124
4125 if (hrc != S_OK)
4126 {
4127 AssertFailed();
4128 CoTaskMemFree(pswzBindName);
4129 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
4130 H();
4131 }
4132 }
4133 else
4134 {
4135 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
4136 AssertLogRelMsgFailed(("NetworkAttachmentType_HostOnly: VBoxNetCfgWinGetComponentByGuid failed, hrc=%Rhrc (0x%x)\n", hrc, hrc));
4137 H();
4138 }
4139
4140
4141 CoTaskMemFree(pswzBindName);
4142
4143 pAdaptorComponent.setNull();
4144 /* release the pNc finally */
4145 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE /*fHasWriteLock*/);
4146
4147 const char *pszTrunk = szTrunkName;
4148
4149 InsertConfigInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp);
4150 InsertConfigString(pCfg, "Trunk", pszTrunk);
4151 InsertConfigString(pCfg, "Network", szNetwork);
4152 InsertConfigInteger(pCfg, "IgnoreConnectFailure", (uint64_t)fIgnoreConnectFailure);
4153 networkName = Bstr(szNetwork);
4154 trunkName = Bstr(pszTrunk);
4155 trunkType = TRUNKTYPE_NETADP;
4156# endif /* defined VBOX_WITH_NETFLT*/
4157#elif defined(RT_OS_DARWIN)
4158 InsertConfigString(pCfg, "Trunk", pszHifName);
4159 InsertConfigString(pCfg, "Network", szNetwork);
4160 InsertConfigInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp);
4161 networkName = Bstr(szNetwork);
4162 trunkName = Bstr(pszHifName);
4163 trunkType = TRUNKTYPE_NETADP;
4164#else
4165 InsertConfigString(pCfg, "Trunk", pszHifName);
4166 InsertConfigString(pCfg, "Network", szNetwork);
4167 InsertConfigInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt);
4168 networkName = Bstr(szNetwork);
4169 trunkName = Bstr(pszHifName);
4170 trunkType = TRUNKTYPE_NETFLT;
4171#endif
4172#if !defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT)
4173
4174 Bstr tmpAddr, tmpMask;
4175
4176 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPAddress",
4177 pszHifName).raw(),
4178 tmpAddr.asOutParam());
4179 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty())
4180 {
4181 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPNetMask",
4182 pszHifName).raw(),
4183 tmpMask.asOutParam());
4184 if (SUCCEEDED(hrc) && !tmpMask.isEmpty())
4185 hrc = hostInterface->EnableStaticIpConfig(tmpAddr.raw(),
4186 tmpMask.raw());
4187 else
4188 hrc = hostInterface->EnableStaticIpConfig(tmpAddr.raw(),
4189 Bstr(VBOXNET_IPV4MASK_DEFAULT).raw());
4190 }
4191 else
4192 {
4193 /* Grab the IP number from the 'vboxnetX' instance number (see netif.h) */
4194 hrc = hostInterface->EnableStaticIpConfig(getDefaultIPv4Address(Bstr(pszHifName)).raw(),
4195 Bstr(VBOXNET_IPV4MASK_DEFAULT).raw());
4196 }
4197
4198 ComAssertComRC(hrc); /** @todo r=bird: Why this isn't fatal? (H()) */
4199
4200 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPV6Address",
4201 pszHifName).raw(),
4202 tmpAddr.asOutParam());
4203 if (SUCCEEDED(hrc))
4204 hrc = virtualBox->GetExtraData(BstrFmt("HostOnly/%s/IPV6NetMask", pszHifName).raw(),
4205 tmpMask.asOutParam());
4206 if (SUCCEEDED(hrc) && !tmpAddr.isEmpty() && !tmpMask.isEmpty())
4207 {
4208 hrc = hostInterface->EnableStaticIpConfigV6(tmpAddr.raw(),
4209 Utf8Str(tmpMask).toUInt32());
4210 ComAssertComRC(hrc); /** @todo r=bird: Why this isn't fatal? (H()) */
4211 }
4212#endif
4213 break;
4214 }
4215
4216#if defined(VBOX_WITH_VDE)
4217 case NetworkAttachmentType_VDE:
4218 {
4219 hrc = aNetworkAdapter->COMGETTER(VDENetwork)(bstr.asOutParam()); H();
4220 InsertConfigNode(pInst, "LUN#0", &pLunL0);
4221 InsertConfigString(pLunL0, "Driver", "VDE");
4222 InsertConfigNode(pLunL0, "Config", &pCfg);
4223 if (!bstr.isEmpty())
4224 {
4225 InsertConfigString(pCfg, "Network", bstr);
4226 networkName = bstr;
4227 }
4228 break;
4229 }
4230#endif
4231
4232 default:
4233 AssertMsgFailed(("should not get here!\n"));
4234 break;
4235 }
4236
4237 /*
4238 * Attempt to attach the driver.
4239 */
4240 switch (eAttachmentType)
4241 {
4242 case NetworkAttachmentType_Null:
4243 break;
4244
4245 case NetworkAttachmentType_Bridged:
4246 case NetworkAttachmentType_Internal:
4247 case NetworkAttachmentType_HostOnly:
4248 case NetworkAttachmentType_NAT:
4249#if defined(VBOX_WITH_VDE)
4250 case NetworkAttachmentType_VDE:
4251#endif
4252 {
4253 if (SUCCEEDED(hrc) && SUCCEEDED(rc))
4254 {
4255 if (fAttachDetach)
4256 {
4257 rc = PDMR3DriverAttach(pVM, pszDevice, uInstance, uLun, 0 /*fFlags*/, NULL /* ppBase */);
4258 //AssertRC(rc);
4259 }
4260
4261 {
4262 /** @todo pritesh: get the dhcp server name from the
4263 * previous network configuration and then stop the server
4264 * else it may conflict with the dhcp server running with
4265 * the current attachment type
4266 */
4267 /* Stop the hostonly DHCP Server */
4268 }
4269
4270 if (!networkName.isEmpty())
4271 {
4272 /*
4273 * Until we implement service reference counters DHCP Server will be stopped
4274 * by DHCPServerRunner destructor.
4275 */
4276 ComPtr<IDHCPServer> dhcpServer;
4277 hrc = virtualBox->FindDHCPServerByNetworkName(networkName.raw(),
4278 dhcpServer.asOutParam());
4279 if (SUCCEEDED(hrc))
4280 {
4281 /* there is a DHCP server available for this network */
4282 BOOL fEnabled;
4283 hrc = dhcpServer->COMGETTER(Enabled)(&fEnabled);
4284 if (FAILED(hrc))
4285 {
4286 LogRel(("DHCP svr: COMGETTER(Enabled) failed, hrc (%Rhrc)", hrc));
4287 H();
4288 }
4289
4290 if (fEnabled)
4291 hrc = dhcpServer->Start(networkName.raw(),
4292 trunkName.raw(),
4293 trunkType.raw());
4294 }
4295 else
4296 hrc = S_OK;
4297 }
4298 }
4299
4300 break;
4301 }
4302
4303 default:
4304 AssertMsgFailed(("should not get here!\n"));
4305 break;
4306 }
4307
4308 meAttachmentType[uInstance] = eAttachmentType;
4309 }
4310 catch (ConfigError &x)
4311 {
4312 // InsertConfig threw something:
4313 return x.m_vrc;
4314 }
4315
4316#undef H
4317
4318 return VINF_SUCCESS;
4319}
4320
4321#ifdef VBOX_WITH_GUEST_PROPS
4322/**
4323 * Set an array of guest properties
4324 */
4325static void configSetProperties(VMMDev * const pVMMDev,
4326 void *names,
4327 void *values,
4328 void *timestamps,
4329 void *flags)
4330{
4331 VBOXHGCMSVCPARM parms[4];
4332
4333 parms[0].type = VBOX_HGCM_SVC_PARM_PTR;
4334 parms[0].u.pointer.addr = names;
4335 parms[0].u.pointer.size = 0; /* We don't actually care. */
4336 parms[1].type = VBOX_HGCM_SVC_PARM_PTR;
4337 parms[1].u.pointer.addr = values;
4338 parms[1].u.pointer.size = 0; /* We don't actually care. */
4339 parms[2].type = VBOX_HGCM_SVC_PARM_PTR;
4340 parms[2].u.pointer.addr = timestamps;
4341 parms[2].u.pointer.size = 0; /* We don't actually care. */
4342 parms[3].type = VBOX_HGCM_SVC_PARM_PTR;
4343 parms[3].u.pointer.addr = flags;
4344 parms[3].u.pointer.size = 0; /* We don't actually care. */
4345
4346 pVMMDev->hgcmHostCall("VBoxGuestPropSvc",
4347 guestProp::SET_PROPS_HOST,
4348 4,
4349 &parms[0]);
4350}
4351
4352/**
4353 * Set a single guest property
4354 */
4355static void configSetProperty(VMMDev * const pVMMDev,
4356 const char *pszName,
4357 const char *pszValue,
4358 const char *pszFlags)
4359{
4360 VBOXHGCMSVCPARM parms[4];
4361
4362 AssertPtrReturnVoid(pszName);
4363 AssertPtrReturnVoid(pszValue);
4364 AssertPtrReturnVoid(pszFlags);
4365 parms[0].type = VBOX_HGCM_SVC_PARM_PTR;
4366 parms[0].u.pointer.addr = (void *)pszName;
4367 parms[0].u.pointer.size = strlen(pszName) + 1;
4368 parms[1].type = VBOX_HGCM_SVC_PARM_PTR;
4369 parms[1].u.pointer.addr = (void *)pszValue;
4370 parms[1].u.pointer.size = strlen(pszValue) + 1;
4371 parms[2].type = VBOX_HGCM_SVC_PARM_PTR;
4372 parms[2].u.pointer.addr = (void *)pszFlags;
4373 parms[2].u.pointer.size = strlen(pszFlags) + 1;
4374 pVMMDev->hgcmHostCall("VBoxGuestPropSvc", guestProp::SET_PROP_HOST, 3,
4375 &parms[0]);
4376}
4377
4378/**
4379 * Set the global flags value by calling the service
4380 * @returns the status returned by the call to the service
4381 *
4382 * @param pTable the service instance handle
4383 * @param eFlags the flags to set
4384 */
4385int configSetGlobalPropertyFlags(VMMDev * const pVMMDev,
4386 guestProp::ePropFlags eFlags)
4387{
4388 VBOXHGCMSVCPARM paParm;
4389 paParm.setUInt32(eFlags);
4390 int rc = pVMMDev->hgcmHostCall("VBoxGuestPropSvc",
4391 guestProp::SET_GLOBAL_FLAGS_HOST, 1,
4392 &paParm);
4393 if (RT_FAILURE(rc))
4394 {
4395 char szFlags[guestProp::MAX_FLAGS_LEN];
4396 if (RT_FAILURE(writeFlags(eFlags, szFlags)))
4397 Log(("Failed to set the global flags.\n"));
4398 else
4399 Log(("Failed to set the global flags \"%s\".\n", szFlags));
4400 }
4401 return rc;
4402}
4403#endif /* VBOX_WITH_GUEST_PROPS */
4404
4405/**
4406 * Set up the Guest Property service, populate it with properties read from
4407 * the machine XML and set a couple of initial properties.
4408 */
4409/* static */ int Console::configGuestProperties(void *pvConsole)
4410{
4411#ifdef VBOX_WITH_GUEST_PROPS
4412 AssertReturn(pvConsole, VERR_GENERAL_FAILURE);
4413 ComObjPtr<Console> pConsole = static_cast<Console *>(pvConsole);
4414 AssertReturn(pConsole->m_pVMMDev, VERR_GENERAL_FAILURE);
4415
4416 /* Load the service */
4417 int rc = pConsole->m_pVMMDev->hgcmLoadService("VBoxGuestPropSvc", "VBoxGuestPropSvc");
4418
4419 if (RT_FAILURE(rc))
4420 {
4421 LogRel(("VBoxGuestPropSvc is not available. rc = %Rrc\n", rc));
4422 /* That is not a fatal failure. */
4423 rc = VINF_SUCCESS;
4424 }
4425 else
4426 {
4427 /*
4428 * Initialize built-in properties that can be changed and saved.
4429 *
4430 * These are typically transient properties that the guest cannot
4431 * change.
4432 */
4433
4434 /* Sysprep execution by VBoxService. */
4435 configSetProperty(pConsole->m_pVMMDev,
4436 "/VirtualBox/HostGuest/SysprepExec", "",
4437 "TRANSIENT, RDONLYGUEST");
4438 configSetProperty(pConsole->m_pVMMDev,
4439 "/VirtualBox/HostGuest/SysprepArgs", "",
4440 "TRANSIENT, RDONLYGUEST");
4441
4442 /*
4443 * Pull over the properties from the server.
4444 */
4445 SafeArray<BSTR> namesOut;
4446 SafeArray<BSTR> valuesOut;
4447 SafeArray<LONG64> timestampsOut;
4448 SafeArray<BSTR> flagsOut;
4449 HRESULT hrc;
4450 hrc = pConsole->mControl->PullGuestProperties(ComSafeArrayAsOutParam(namesOut),
4451 ComSafeArrayAsOutParam(valuesOut),
4452 ComSafeArrayAsOutParam(timestampsOut),
4453 ComSafeArrayAsOutParam(flagsOut));
4454 AssertMsgReturn(SUCCEEDED(hrc), ("hrc=%Rrc\n", hrc), VERR_GENERAL_FAILURE);
4455 size_t cProps = namesOut.size();
4456 size_t cAlloc = cProps + 1;
4457 if ( valuesOut.size() != cProps
4458 || timestampsOut.size() != cProps
4459 || flagsOut.size() != cProps
4460 )
4461 AssertFailedReturn(VERR_INVALID_PARAMETER);
4462
4463 char **papszNames, **papszValues, **papszFlags;
4464 char szEmpty[] = "";
4465 LONG64 *pai64Timestamps;
4466 papszNames = (char **)RTMemTmpAllocZ(sizeof(void *) * cAlloc);
4467 papszValues = (char **)RTMemTmpAllocZ(sizeof(void *) * cAlloc);
4468 pai64Timestamps = (LONG64 *)RTMemTmpAllocZ(sizeof(LONG64) * cAlloc);
4469 papszFlags = (char **)RTMemTmpAllocZ(sizeof(void *) * cAlloc);
4470 if (papszNames && papszValues && pai64Timestamps && papszFlags)
4471 {
4472 for (unsigned i = 0; RT_SUCCESS(rc) && i < cProps; ++i)
4473 {
4474 AssertPtrReturn(namesOut[i], VERR_INVALID_PARAMETER);
4475 rc = RTUtf16ToUtf8(namesOut[i], &papszNames[i]);
4476 if (RT_FAILURE(rc))
4477 break;
4478 if (valuesOut[i])
4479 rc = RTUtf16ToUtf8(valuesOut[i], &papszValues[i]);
4480 else
4481 papszValues[i] = szEmpty;
4482 if (RT_FAILURE(rc))
4483 break;
4484 pai64Timestamps[i] = timestampsOut[i];
4485 if (flagsOut[i])
4486 rc = RTUtf16ToUtf8(flagsOut[i], &papszFlags[i]);
4487 else
4488 papszFlags[i] = szEmpty;
4489 }
4490 if (RT_SUCCESS(rc))
4491 configSetProperties(pConsole->m_pVMMDev,
4492 (void *)papszNames,
4493 (void *)papszValues,
4494 (void *)pai64Timestamps,
4495 (void *)papszFlags);
4496 for (unsigned i = 0; i < cProps; ++i)
4497 {
4498 RTStrFree(papszNames[i]);
4499 if (valuesOut[i])
4500 RTStrFree(papszValues[i]);
4501 if (flagsOut[i])
4502 RTStrFree(papszFlags[i]);
4503 }
4504 }
4505 else
4506 rc = VERR_NO_MEMORY;
4507 RTMemTmpFree(papszNames);
4508 RTMemTmpFree(papszValues);
4509 RTMemTmpFree(pai64Timestamps);
4510 RTMemTmpFree(papszFlags);
4511 AssertRCReturn(rc, rc);
4512
4513 /*
4514 * These properties have to be set before pulling over the properties
4515 * from the machine XML, to ensure that properties saved in the XML
4516 * will override them.
4517 */
4518 /* Set the VBox version string as a guest property */
4519 configSetProperty(pConsole->m_pVMMDev, "/VirtualBox/HostInfo/VBoxVer",
4520 VBOX_VERSION_STRING, "TRANSIENT, RDONLYGUEST");
4521 /* Set the VBox SVN revision as a guest property */
4522 configSetProperty(pConsole->m_pVMMDev, "/VirtualBox/HostInfo/VBoxRev",
4523 RTBldCfgRevisionStr(), "TRANSIENT, RDONLYGUEST");
4524
4525 /*
4526 * Register the host notification callback
4527 */
4528 HGCMSVCEXTHANDLE hDummy;
4529 HGCMHostRegisterServiceExtension(&hDummy, "VBoxGuestPropSvc",
4530 Console::doGuestPropNotification,
4531 pvConsole);
4532
4533#ifdef VBOX_WITH_GUEST_PROPS_RDONLY_GUEST
4534 rc = configSetGlobalPropertyFlags(pConsole->mVMMDev,
4535 guestProp::RDONLYGUEST);
4536 AssertRCReturn(rc, rc);
4537#endif
4538
4539 Log(("Set VBoxGuestPropSvc property store\n"));
4540 }
4541 return VINF_SUCCESS;
4542#else /* !VBOX_WITH_GUEST_PROPS */
4543 return VERR_NOT_SUPPORTED;
4544#endif /* !VBOX_WITH_GUEST_PROPS */
4545}
4546
4547/**
4548 * Set up the Guest Control service.
4549 */
4550/* static */ int Console::configGuestControl(void *pvConsole)
4551{
4552#ifdef VBOX_WITH_GUEST_CONTROL
4553 AssertReturn(pvConsole, VERR_GENERAL_FAILURE);
4554 ComObjPtr<Console> pConsole = static_cast<Console *>(pvConsole);
4555
4556 /* Load the service */
4557 int rc = pConsole->m_pVMMDev->hgcmLoadService("VBoxGuestControlSvc", "VBoxGuestControlSvc");
4558
4559 if (RT_FAILURE(rc))
4560 {
4561 LogRel(("VBoxGuestControlSvc is not available. rc = %Rrc\n", rc));
4562 /* That is not a fatal failure. */
4563 rc = VINF_SUCCESS;
4564 }
4565 else
4566 {
4567 HGCMSVCEXTHANDLE hDummy;
4568 rc = HGCMHostRegisterServiceExtension(&hDummy, "VBoxGuestControlSvc",
4569 &Guest::doGuestCtrlNotification,
4570 pConsole->getGuest());
4571 if (RT_FAILURE(rc))
4572 Log(("Cannot register VBoxGuestControlSvc extension!\n"));
4573 else
4574 Log(("VBoxGuestControlSvc loaded\n"));
4575 }
4576
4577 return rc;
4578#else /* !VBOX_WITH_GUEST_CONTROL */
4579 return VERR_NOT_SUPPORTED;
4580#endif /* !VBOX_WITH_GUEST_CONTROL */
4581}
Note: See TracBrowser for help on using the repository browser.

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