VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/ConsoleImpl2.cpp@ 43750

Last change on this file since 43750 was 43750, checked in by vboxsync, 12 years ago

Main/src-client/ConsoleImpl2 & Devices/PC/DevPcBios: Logical Geometry fix (xtracker bug #6433)
Fix for finding the initial 4 SATA ports used (attached HD to them) and creating a CFGM entry for those ports.

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

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