VirtualBox

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

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

PCI: more slot management bits

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