VirtualBox

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

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

PCI: part 1 of PCI slots assignment logic in Main

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

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