VirtualBox

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

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

Main: defer creation of the VMMDev instance in Console to Console::powerUpThread so that the VMMDev with the HGCM thread only gets created for session/console pairs any more which actually power up a VM; this avoids creating dozens of HGCM threads for every single session even if it never starts a VM; some more Console code cleanup while we're at it

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