VirtualBox

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

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

Main: show RTC in ACPI for ICH9

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