VirtualBox

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

最後變更 在這個檔案從32908是 32908,由 vboxsync 提交於 14 年 前

VMM: moved hyper heap configuration to console, as it knows more about VM config, and logic getting complicated

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

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