VirtualBox

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

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

Switch to the new block cache and disable the old one for now

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