VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxBFE/VBoxBFE.cpp@ 23598

Last change on this file since 23598 was 23598, checked in by vboxsync, 15 years ago

VMM,Main,VBoxBFE: Split up VMR3Load into VMR3LoadFromFile and VMR3LoadFromStream; added VMR3Migrate.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 75.9 KB
Line 
1/* $Id: VBoxBFE.cpp 23598 2009-10-07 15:00:16Z vboxsync $ */
2/** @file
3 * Basic Frontend (BFE): VBoxBFE main routines.
4 *
5 * VBoxBFE is a limited frontend that sits directly on the Virtual Machine
6 * Manager (VMM) and does _not_ use COM to communicate.
7 * On Linux and Windows, VBoxBFE is based on SDL; on L4 it's based on the
8 * L4 console. Much of the code has been copied over from the other frontends
9 * in VBox/Main/ and src/Frontends/VBoxSDL/.
10 */
11
12/*
13 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
14 *
15 * This file is part of VirtualBox Open Source Edition (OSE), as
16 * available from http://www.virtualbox.org. This file is free software;
17 * you can redistribute it and/or modify it under the terms of the GNU
18 * General Public License (GPL) as published by the Free Software
19 * Foundation, in version 2 as it comes in the "COPYING" file of the
20 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
21 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
22 *
23 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
24 * Clara, CA 95054 USA or visit http://www.sun.com if you need
25 * additional information or have any questions.
26 */
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#define LOG_GROUP LOG_GROUP_GUI
32
33#ifndef VBOXBFE_WITHOUT_COM
34# include <VBox/com/Guid.h>
35# include <VBox/com/string.h>
36using namespace com;
37#endif
38
39#include <VBox/types.h>
40#include <VBox/err.h>
41#include <VBox/log.h>
42#include <VBox/param.h>
43#include <VBox/pdm.h>
44#include <VBox/version.h>
45#ifdef VBOXBFE_WITH_USB
46# include <VBox/vusb.h>
47#endif
48#ifdef VBOX_WITH_HGCM
49# include <VBox/shflsvc.h>
50#endif
51#include <iprt/alloc.h>
52#include <iprt/alloca.h>
53#include <iprt/assert.h>
54#include <iprt/ctype.h>
55#include <iprt/file.h>
56#include <iprt/path.h>
57#include <iprt/initterm.h>
58#include <iprt/semaphore.h>
59#include <iprt/stream.h>
60#include <iprt/string.h>
61#include <iprt/thread.h>
62#include <iprt/uuid.h>
63
64#include "VBoxBFE.h"
65
66#include <stdio.h>
67#include <stdlib.h> /* putenv */
68#include <errno.h>
69
70#if defined(RT_OS_LINUX) || defined(RT_OS_L4)
71#include <fcntl.h>
72#include <net/if.h>
73#include <sys/ioctl.h>
74#include <linux/if_tun.h>
75#endif
76
77#include "ConsoleImpl.h"
78#include "DisplayImpl.h"
79#include "MouseImpl.h"
80#include "KeyboardImpl.h"
81#include "VMMDevInterface.h"
82#include "StatusImpl.h"
83#include "Framebuffer.h"
84#include "MachineDebuggerImpl.h"
85#ifdef VBOXBFE_WITH_USB
86# include "HostUSBImpl.h"
87#endif
88
89#if defined(USE_SDL) && ! defined(RT_OS_L4)
90#include "SDLConsole.h"
91#include "SDLFramebuffer.h"
92#endif
93
94#ifdef RT_OS_L4
95#include "L4Console.h"
96#include "L4Framebuffer.h"
97#include "L4IDLInterface.h"
98#endif
99
100#ifdef RT_OS_L4
101# include <l4/sys/ktrace.h>
102# include <l4/vboxserver/file.h>
103#endif
104
105/*******************************************************************************
106* Defined Constants And Macros *
107*******************************************************************************/
108
109#define VBOXSDL_ADVANCED_OPTIONS
110#define MAC_STRING_LEN 12
111
112
113/*******************************************************************************
114* Internal Functions *
115*******************************************************************************/
116static DECLCALLBACK(int) vboxbfeConfigConstructor(PVM pVM, void *pvUser);
117static DECLCALLBACK(void) vmstateChangeCallback(PVM pVM, VMSTATE enmState, VMSTATE enmOldState, void *pvUser);
118static DECLCALLBACK(void) setVMErrorCallback(PVM pVM, void *pvUser, int rc, RT_SRC_POS_DECL,
119 const char *pszFormat, va_list args);
120static DECLCALLBACK(int) VMPowerUpThread(RTTHREAD Thread, void *pvUser);
121
122
123/*******************************************************************************
124* Global Variables *
125*******************************************************************************/
126
127PVM pVM = NULL;
128Mouse *gMouse = NULL;
129VMDisplay *gDisplay = NULL;
130Keyboard *gKeyboard = NULL;
131VMMDev *gVMMDev = NULL;
132Framebuffer *gFramebuffer = NULL;
133MachineDebugger *gMachineDebugger = NULL;
134VMStatus *gStatus = NULL;
135Console *gConsole = NULL;
136#ifdef VBOXBFE_WITH_USB
137HostUSB *gHostUSB = NULL;
138#endif
139
140VMSTATE machineState = VMSTATE_CREATING;
141
142static PPDMLED mapFDLeds[2] = {0};
143
144/** flag whether keyboard/mouse events are grabbed */
145#ifdef RT_OS_L4
146/** see <l4/input/macros.h> for key definitions */
147int gHostKey; /* not used */
148int gHostKeySym = KEY_RIGHTCTRL;
149#elif defined (DEBUG_dmik)
150// my mini kbd doesn't have RCTRL...
151int gHostKey = KMOD_RSHIFT;
152int gHostKeySym = SDLK_RSHIFT;
153#else
154int gHostKey = KMOD_RCTRL;
155int gHostKeySym = SDLK_RCTRL;
156#endif
157bool gfAllowFullscreenToggle = true;
158
159static bool g_fIOAPIC = false;
160static bool g_fACPI = true;
161static bool g_fAudio = false;
162#ifdef VBOXBFE_WITH_USB
163static bool g_fUSB = false;
164#endif
165static char *g_pszHdaFile = NULL;
166static bool g_fHdaSpf = false;
167static char *g_pszHdbFile = NULL;
168static bool g_fHdbSpf = false;
169static char *g_pszCdromFile = NULL;
170static char *g_pszFdaFile = NULL;
171 const char *g_pszStateFile = NULL;
172static const char *g_pszBootDevice = "IDE";
173static uint32_t g_u32MemorySizeMB = 128;
174static uint32_t g_u32VRamSize = 4 * _1M;
175#ifdef VBOXSDL_ADVANCED_OPTIONS
176static bool g_fRawR0 = true;
177static bool g_fRawR3 = true;
178static bool g_fPATM = true;
179static bool g_fCSAM = true;
180#endif
181static bool g_fRestoreState = false;
182static const char *g_pszShareDir[MaxSharedFolders];
183static const char *g_pszShareName[MaxSharedFolders];
184static bool g_fShareReadOnly[MaxSharedFolders];
185static unsigned g_uNumShares;
186static bool g_fPreAllocRam = false;
187static int g_iBootMenu = 2;
188static bool g_fReleaseLog = true; /**< Set if we should open the release. */
189 const char *g_pszProgressString;
190 unsigned g_uProgressPercent = ~0U;
191
192
193/**
194 * Network device config info.
195 */
196typedef struct BFENetworkDevice
197{
198 enum
199 {
200 NOT_CONFIGURED = 0,
201 NONE,
202 NAT,
203 HIF,
204 INTNET
205 } enmType; /**< The type of network driver. */
206 bool fSniff; /**< Set if the network sniffer should be installed. */
207 const char *pszSniff; /**< Output file for the network sniffer. */
208 RTMAC Mac; /**< The mac address for the device. */
209 const char *pszName; /**< The device name of a HIF device. The name of the internal network. */
210#ifdef RT_OS_OS2
211 bool fHaveConnectTo; /**< Whether fConnectTo is set. */
212 int32_t iConnectTo; /**< The lanX to connect to (bridge with). */
213#elif 1//defined(RT_OS_LINUX)
214 bool fHaveFd; /**< Set if fd is valid. */
215 int32_t fd; /**< The file descriptor of a HIF device.*/
216#endif
217} BFENETDEV, *PBFENETDEV;
218
219/** Array of network device configurations. */
220static BFENETDEV g_aNetDevs[NetworkAdapterCount];
221
222
223/** @todo currently this is only set but never read. */
224static char szError[512];
225
226
227/**
228 */
229bool fActivateHGCM()
230{
231 return !!(g_uNumShares > 0);
232}
233
234/**
235 * Converts the passed in network option
236 *
237 * @returns Index into g_aNetDevs on success. (positive)
238 * @returns VERR_INVALID_PARAMETER on failure. (negative)
239 * @param pszArg The argument.
240 * @param cchRoot The length of the argument root.
241 */
242static int networkArg2Index(const char *pszArg, int cchRoot)
243{
244 uint32_t n;
245 int rc = RTStrToUInt32Ex(&pszArg[cchRoot], NULL, 10, &n);
246 if (RT_FAILURE(rc))
247 {
248 RTPrintf("Error: invalid network device option (rc=%Rrc): %s\n", rc, pszArg);
249 return -1;
250 }
251 if (n < 1 || n > NetworkAdapterCount)
252 {
253 RTPrintf("Error: The network device number is out of range: %RU32 (1 <= 0 <= %u) (%s)\n",
254 n, NetworkAdapterCount, pszArg);
255 return -1;
256 }
257 return n;
258}
259
260/**
261 * Generates a new unique MAC address based on our vendor ID and
262 * parts of a GUID.
263 *
264 * @returns iprt status code
265 * @param pAddress An array into which to store the newly generated address
266 */
267int GenerateMACAddress(char pszAddress[MAC_STRING_LEN + 1])
268{
269 /*
270 * Our strategy is as follows: the first three bytes are our fixed
271 * vendor ID (080027). The remaining 3 bytes will be taken from the
272 * start of a GUID. This is a fairly safe algorithm.
273 */
274 LogFlowFunc(("called\n"));
275 RTUUID uuid;
276 int rc = RTUuidCreate(&uuid);
277 if (RT_FAILURE(rc))
278 {
279 LogFlowFunc(("RTUuidCreate failed, returning %Rrc\n", rc));
280 return rc;
281 }
282 RTStrPrintf(pszAddress, MAC_STRING_LEN + 1, "080027%02X%02X%02X",
283 uuid.au8[0], uuid.au8[1], uuid.au8[2]);
284 LogFlowFunc(("generated MAC: '%s'\n", pszAddress));
285 return VINF_SUCCESS;
286}
287
288/**
289 * Print a syntax error.
290 *
291 * @returns return value for main().
292 * @param pszMsg The message format string.
293 * @param ... Format arguments.
294 */
295static int SyntaxError(const char *pszMsg, ...)
296{
297 va_list va;
298 RTPrintf("error: ");
299 va_start(va, pszMsg);
300 RTPrintfV(pszMsg, va);
301 va_end(va);
302 return 1;
303}
304
305
306/**
307 * Print a fatal error.
308 *
309 * @returns return value for main().
310 * @param pszMsg The message format string.
311 * @param ... Format arguments.
312 */
313static int FatalError(const char *pszMsg, ...)
314{
315 va_list va;
316 RTPrintf("fatal error: ");
317 va_start(va, pszMsg);
318 RTPrintfV(pszMsg, va);
319 va_end(va);
320 return 1;
321}
322
323/**
324 * Start progress display.
325 */
326void startProgressInfo(const char *pszStr)
327{
328 g_pszProgressString = pszStr;
329 g_uProgressPercent = 0;
330}
331
332/**
333 * Update progress display.
334 */
335int callProgressInfo(PVM pVM, unsigned uPercent, void *pvUser)
336{
337 if (gConsole)
338 gConsole->progressInfo(pVM, uPercent, pvUser);
339 return VINF_SUCCESS;
340}
341
342/**
343 * End progress display.
344 */
345void endProgressInfo(void)
346{
347 g_uProgressPercent = ~0U;
348}
349
350
351/**
352 * Print program usage.
353 */
354static void show_usage()
355{
356 RTPrintf("Usage:\n"
357 " -hda <file> Set first hard disk to file\n"
358 " -hdb <file> Set second hard disk to file\n"
359 " -fda <file> Set first floppy disk to file\n"
360 " -cdrom <file> Set CDROM to file/device ('none' to unmount)\n"
361 " -boot <a|c|d> Set boot device (a = floppy, c = first hard disk, d = DVD)\n"
362 " -boot menu <0|1|2> Boot menu (0 = disable, 1 = menu only, 2 = message + menu)\n"
363 " -m <size> Set memory size in megabytes (default 128MB)\n"
364 " -vram <size> Set size of video memory in megabytes\n"
365 " -prealloc Force RAM pre-allocation\n"
366 " -fullscreen Start VM in fullscreen mode\n"
367 " -statefile <file> Define the file name for VM save/restore\n"
368 " -restore Restore the VM if the statefile exists, normal start otherwise\n"
369 " -nofstoggle Forbid switching to/from fullscreen mode\n"
370 " -share <dir> <name> [readonly]\n"
371 " Share directory <dir> as name <name>. Optionally read-only.\n"
372 " -nohostkey Disable hostkey\n"
373 " -[no]acpi Enable or disable ACPI (default: enabled)\n"
374 " -[no]ioapic Enable or disable the IO-APIC (default: disabled)\n"
375 " -audio Enable audio\n"
376#ifndef RT_OS_L4
377 " -natdev<1-N> [mac] Use NAT networking on network adapter <N>. Use hardware\n"
378 " address <mac> if specified.\n"
379#endif
380 " -hifdev<1-N> Use Host Interface Networking with host interface <int>\n"
381 " <int> [mac] on network adapter <N>. Use hardware address <mac> if\n"
382 " specified.\n"
383#ifndef RT_OS_L4
384 " -intnet<1-N> Attach network adapter <N> to internal network <net>. Use\n"
385 " <net> [mac] hardware address <mac> if specified.\n"
386#endif
387#if 0
388 " -netsniff<1-N> Enable packet sniffer\n"
389#endif
390#ifdef RT_OS_OS2
391 " -brdev<1-N> lan<X> Bridge network adaptor <N> with the 'lanX' device.\n"
392#endif
393#ifdef RT_OS_LINUX
394 " -tapfd<1-N> <fd> Use existing TAP device, don't allocate\n"
395#endif
396#ifdef VBOX_WITH_VRDP
397 " -vrdp [port] Listen for VRDP connections on port (default if not specified)\n"
398#endif
399#ifdef VBOX_SECURELABEL
400 " -securelabel Display a secure VM label at the top of the screen\n"
401 " -seclabelfnt TrueType (.ttf) font file for secure session label\n"
402 " -seclabelsiz Font point size for secure session label (default 12)\n"
403#endif
404 " -[no]rellog Enable or disable the release log './VBoxBFE.log' (default: enabled)\n"
405#ifdef VBOXSDL_ADVANCED_OPTIONS
406 " -[no]rawr0 Enable or disable raw ring 3\n"
407 " -[no]rawr3 Enable or disable raw ring 0\n"
408 " -[no]patm Enable or disable PATM\n"
409 " -[no]csam Enable or disable CSAM\n"
410#endif
411#ifdef RT_OS_L4
412 " -env <var=value> Set the given environment variable to \"value\"\n"
413#endif
414 "\n");
415}
416
417
418/** entry point */
419extern "C" DECLEXPORT(int) TrustedMain (int argc, char **argv, char **envp)
420{
421 bool fFullscreen = false;
422#ifdef VBOX_WITH_VRDP
423 int32_t portVRDP = -1;
424#endif
425#ifdef VBOX_SECURELABEL
426 bool fSecureLabel = false;
427 uint32_t secureLabelPointSize = 12;
428 char *secureLabelFontFile = NULL;
429#endif
430#ifdef RT_OS_L4
431 uint32_t u32MaxVRAM;
432#endif
433 int rc = VINF_SUCCESS;
434
435 RTPrintf("Sun VirtualBox Simple SDL GUI built %s %s\n", __DATE__, __TIME__);
436
437 // less than one parameter is not possible
438 if (argc < 2)
439 {
440 show_usage();
441 return 1;
442 }
443
444 /*
445 * Parse the command line arguments.
446 */
447 for (int curArg = 1; curArg < argc; curArg++)
448 {
449 const char * const pszArg = argv[curArg];
450 if (strcmp(pszArg, "-boot") == 0)
451 {
452 if (++curArg >= argc)
453 return SyntaxError("missing argument for boot drive!\n");
454 if (strlen(argv[curArg]) != 1)
455 return SyntaxError("invalid argument for boot drive! (%s)\n", argv[curArg]);
456 rc = VINF_SUCCESS;
457 switch (argv[curArg][0])
458 {
459 case 'a':
460 {
461 g_pszBootDevice = "FLOPPY";
462 break;
463 }
464
465 case 'c':
466 {
467 g_pszBootDevice = "IDE";
468 break;
469 }
470
471 case 'd':
472 {
473 g_pszBootDevice = "DVD";
474 break;
475 }
476
477 default:
478 return SyntaxError("wrong argument for boot drive! (%s)\n", argv[curArg]);
479 }
480 }
481 else if (strcmp(pszArg, "-bootmenu") == 0)
482 {
483 if (++curArg >= argc)
484 return SyntaxError("missing argument for boot menu!\n");
485 if (strlen(argv[curArg]) != 1 || *argv[curArg] < '0' || *argv[curArg] > '2')
486 return SyntaxError("invalid argument for boot menu! (%s)\n", argv[curArg]);
487 rc = VINF_SUCCESS;
488 g_iBootMenu = *argv[curArg] - 0;
489 }
490 else if (strcmp(pszArg, "-m") == 0)
491 {
492 if (++curArg >= argc)
493 return SyntaxError("missing argument for memory size!\n");
494 rc = RTStrToUInt32Ex(argv[curArg], NULL, 0, &g_u32MemorySizeMB);
495 if (RT_FAILURE(rc))
496 return SyntaxError("bad memory size: %s (error %Rrc)\n",
497 argv[curArg], rc);
498 }
499 else if (strcmp(pszArg, "-vram") == 0)
500 {
501 if (++curArg >= argc)
502 return SyntaxError("missing argument for vram size!\n");
503 uint32_t uVRAMMB;
504 rc = RTStrToUInt32Ex(argv[curArg], NULL, 0, &uVRAMMB);
505 g_u32VRamSize = uVRAMMB * _1M;
506 if (RT_FAILURE(rc))
507 return SyntaxError("bad video ram size: %s (error %Rrc)\n",
508 argv[curArg], rc);
509 }
510 else if (strcmp(pszArg, "-statefile") == 0)
511 {
512 if (++curArg >= argc)
513 return SyntaxError("missing argument for restore!\n");
514 g_pszStateFile = argv[curArg];
515 }
516 else if (strcmp(pszArg, "-restore") == 0)
517 g_fRestoreState = true;
518 else if (strcmp(pszArg, "-share") == 0)
519 {
520 if (g_uNumShares >= MaxSharedFolders)
521 return SyntaxError("too many shared folders specified!\n");
522 if (++curArg >= argc)
523 return SyntaxError("missing 1s argument for share!\n");
524 g_pszShareDir[g_uNumShares] = argv[curArg];
525 if (++curArg >= argc)
526 return SyntaxError("missing 2nd argument for share!\n");
527 g_pszShareName[g_uNumShares] = argv[curArg];
528 if (curArg < argc-1 && strcmp(argv[curArg+1], "readonly") == 0)
529 {
530 g_fShareReadOnly[g_uNumShares] = true;
531 curArg++;
532 }
533 g_uNumShares++;
534 }
535 else if (strcmp(pszArg, "-fullscreen") == 0)
536 fFullscreen = true;
537 else if (strcmp(pszArg, "-nofstoggle") == 0)
538 gfAllowFullscreenToggle = false;
539 else if (strcmp(pszArg, "-nohostkey") == 0)
540 {
541 gHostKey = 0;
542 gHostKeySym = 0;
543 }
544 else if (strcmp(pszArg, "-acpi") == 0)
545 g_fACPI = true;
546 else if (strcmp(pszArg, "-noacpi") == 0)
547 g_fACPI = false;
548 else if (strcmp(pszArg, "-ioapic") == 0)
549 g_fIOAPIC = true;
550 else if (strcmp(pszArg, "-noioapic") == 0)
551 g_fIOAPIC = false;
552 else if (strcmp(pszArg, "-audio") == 0)
553 g_fAudio = true;
554#ifdef VBOXBFE_WITH_USB
555 else if (strcmp(pszArg, "-usb") == 0)
556 g_fUSB = true;
557#endif
558 else if (strcmp(pszArg, "-hda") == 0)
559 {
560 if (++curArg >= argc)
561 return SyntaxError("missing file name for first hard disk!\n");
562
563 /* resolve it. */
564 if (RTPathExists(argv[curArg]))
565 g_pszHdaFile = RTPathRealDup(argv[curArg]);
566 if (!g_pszHdaFile)
567 return SyntaxError("The path to the specified harddisk, '%s', could not be resolved.\n", argv[curArg]);
568 }
569 else if (strcmp(pszArg, "-hdaspf") == 0)
570 {
571 g_fHdaSpf = true;
572 }
573 else if (strcmp(pszArg, "-hdb") == 0)
574 {
575 if (++curArg >= argc)
576 return SyntaxError("missing file name for second hard disk!\n");
577
578 /* resolve it. */
579 if (RTPathExists(argv[curArg]))
580 g_pszHdbFile = RTPathRealDup(argv[curArg]);
581 if (!g_pszHdbFile)
582 return SyntaxError("The path to the specified harddisk, '%s', could not be resolved.\n", argv[curArg]);
583 }
584 else if (strcmp(pszArg, "-hdbspf") == 0)
585 {
586 g_fHdbSpf = true;
587 }
588 else if (strcmp(pszArg, "-fda") == 0)
589 {
590 if (++curArg >= argc)
591 return SyntaxError("missing file/device name for first floppy disk!\n");
592
593 /* resolve it. */
594 if (RTPathExists(argv[curArg]))
595 g_pszFdaFile = RTPathRealDup(argv[curArg]);
596 if (!g_pszFdaFile)
597 return SyntaxError("The path to the specified floppy disk, '%s', could not be resolved.\n", argv[curArg]);
598 }
599 else if (strcmp(pszArg, "-cdrom") == 0)
600 {
601 if (++curArg >= argc)
602 return SyntaxError("missing file/device name for first hard disk!\n");
603
604 /* resolve it. */
605 if (RTPathExists(argv[curArg]))
606 g_pszCdromFile = RTPathRealDup(argv[curArg]);
607 if (!g_pszCdromFile)
608 return SyntaxError("The path to the specified cdrom, '%s', could not be resolved.\n", argv[curArg]);
609 }
610#ifdef RT_OS_L4
611 /* This is leaving a lot of dead code in the L4 version of course,
612 but I don't think that that is a major problem. We may even
613 activate it sometime... */
614 else if ( strncmp(pszArg, "-hifdev", 7) == 0
615 || strncmp(pszArg, "-nonetd", 7) == 0)
616#else
617 else if ( strncmp(pszArg, "-natdev", 7) == 0
618 || strncmp(pszArg, "-hifdev", 7) == 0
619 || strncmp(pszArg, "-nonetd", 7) == 0
620 || strncmp(pszArg, "-intnet", 7) == 0)
621#endif
622 {
623 int i = networkArg2Index(pszArg, 7);
624 if (i < 0)
625 return 1;
626 g_aNetDevs[i].enmType = !strncmp(pszArg, "-natdev", 7)
627 ? BFENETDEV::NAT
628 : !strncmp(pszArg, "-hifdev", 7)
629 ? BFENETDEV::HIF
630 : !strncmp(pszArg, "-intnet", 7)
631 ? BFENETDEV::INTNET
632 : BFENETDEV::NONE;
633
634 /* The HIF device name / The Internal Network name. */
635 g_aNetDevs[i].pszName = NULL;
636 if ( g_aNetDevs[i].enmType == BFENETDEV::HIF
637 || g_aNetDevs[i].enmType == BFENETDEV::INTNET)
638 {
639 if (curArg + 1 >= argc)
640 return SyntaxError(g_aNetDevs[i].enmType == BFENETDEV::HIF
641 ? "The TAP network device name is missing! (%s)\n"
642 : "The internal network name is missing! (%s)\n"
643 , pszArg);
644 g_aNetDevs[i].pszName = argv[++curArg];
645 }
646
647 /* The MAC address. */
648 const char *pszMac;
649 char szMacGen[MAC_STRING_LEN + 1];
650 if ((curArg + 1 < argc) && (argv[curArg + 1][0] != '-'))
651 pszMac = argv[++curArg];
652 else
653 {
654 rc = GenerateMACAddress(szMacGen);
655 if (RT_FAILURE(rc))
656 return SyntaxError("failed to generate a hardware address for network device %d (error %Rrc)\n",
657 i, rc);
658 pszMac = szMacGen;
659 }
660 if (strlen(pszMac) != MAC_STRING_LEN)
661 return SyntaxError("The network MAC address has an invalid length: %s (%s)\n", pszMac, pszArg);
662 for (unsigned j = 0; j < RT_ELEMENTS(g_aNetDevs[i].Mac.au8); j++)
663 {
664 char c1 = toupper(*pszMac++) - '0';
665 if (c1 > 9)
666 c1 -= 7;
667 char c2 = toupper(*pszMac++) - '0';
668 if (c2 > 9)
669 c2 -= 7;
670 if (c2 > 16 || c1 > 16)
671 return SyntaxError("Invalid MAC address: %s\n", argv[curArg]);
672 g_aNetDevs[i].Mac.au8[j] = ((c1 & 0x0f) << 4) | (c2 & 0x0f);
673 }
674 }
675 else if (strncmp(pszArg, "-netsniff", 9) == 0)
676 {
677 int i = networkArg2Index(pszArg, 9);
678 if (i < 0)
679 return 1;
680 g_aNetDevs[i].fSniff = true;
681 /** @todo filename */
682 }
683#ifdef RT_OS_OS2
684 else if (strncmp(pszArg, "-brdev", 6) == 0)
685 {
686 int i = networkArg2Index(pszArg, 6);
687 if (i < 0)
688 return 1;
689 if (g_aNetDevs[i].enmType != BFENETDEV::HIF)
690 return SyntaxError("%d is not a hif device! Make sure you put the -hifdev argument first.\n", i);
691 if (++curArg >= argc)
692 return SyntaxError("missing argument for %s!\n", pszArg);
693 if ( strncmp(argv[curArg], "lan", 3)
694 || argv[curArg][3] < '0'
695 || argv[curArg][3] >= '8'
696 || argv[curArg][4])
697 return SyntaxError("bad interface name '%s' specified with '%s'. Expected 'lan0', 'lan1' and similar.\n",
698 argv[curArg], pszArg);
699 g_aNetDevs[i].iConnectTo = argv[curArg][3] - '0';
700 g_aNetDevs[i].fHaveConnectTo = true;
701 }
702#endif
703#ifdef RT_OS_LINUX
704 else if (strncmp(pszArg, "-tapfd", 6) == 0)
705 {
706 int i = networkArg2Index(pszArg, 6);
707 if (i < 0)
708 return 1;
709 if (++curArg >= argc)
710 return SyntaxError("missing argument for %s!\n", pszArg);
711 rc = RTStrToInt32Ex(argv[curArg], NULL, 0, &g_aNetDevs[i].fd);
712 if (RT_FAILURE(rc))
713 return SyntaxError("bad tap file descriptor: %s (error %Rrc)\n", argv[curArg], rc);
714 g_aNetDevs[i].fHaveFd = true;
715 }
716#endif /* RT_OS_LINUX */
717#ifdef VBOX_WITH_VRDP
718 else if (strcmp(pszArg, "-vrdp") == 0)
719 {
720 // -vrdp might take a port number (positive).
721 portVRDP = 0; // indicate that it was encountered.
722 if (curArg + 1 < argc && argv[curArg + 1][0] != '-')
723 {
724 rc = RTStrToInt32Ex(argv[curArg], NULL, 0, &portVRDP);
725 if (RT_FAILURE(rc))
726 return SyntaxError("cannot vrpd port: %s (%Rrc)\n", argv[curArg], rc);
727 if (portVRDP < 0 || portVRDP >= 0x10000)
728 return SyntaxError("vrdp port number is out of range: %RI32\n", portVRDP);
729 }
730 }
731#endif /* VBOX_WITH_VRDP */
732#ifdef VBOX_SECURELABEL
733 else if (strcmp(pszArg, "-securelabel") == 0)
734 {
735 fSecureLabel = true;
736 LogFlow(("Secure labelling turned on\n"));
737 }
738 else if (strcmp(pszArg, "-seclabelfnt") == 0)
739 {
740 if (++curArg >= argc)
741 return SyntaxError("missing font file name for secure label!\n");
742 secureLabelFontFile = argv[curArg];
743 }
744 else if (strcmp(pszArg, "-seclabelsiz") == 0)
745 {
746 if (++curArg >= argc)
747 return SyntaxError("missing font point size for secure label!\n");
748 secureLabelPointSize = atoi(argv[curArg]);
749 }
750#endif
751 else if (strcmp(pszArg, "-rellog") == 0)
752 g_fReleaseLog = true;
753 else if (strcmp(pszArg, "-norellog") == 0)
754 g_fReleaseLog = false;
755 else if (strcmp(pszArg, "-prealloc") == 0)
756 g_fPreAllocRam = true;
757#ifdef VBOXSDL_ADVANCED_OPTIONS
758 else if (strcmp(pszArg, "-rawr0") == 0)
759 g_fRawR0 = true;
760 else if (strcmp(pszArg, "-norawr0") == 0)
761 g_fRawR0 = false;
762 else if (strcmp(pszArg, "-rawr3") == 0)
763 g_fRawR3 = true;
764 else if (strcmp(pszArg, "-norawr3") == 0)
765 g_fRawR3 = false;
766 else if (strcmp(pszArg, "-patm") == 0)
767 g_fPATM = true;
768 else if (strcmp(pszArg, "-nopatm") == 0)
769 g_fPATM = false;
770 else if (strcmp(pszArg, "-csam") == 0)
771 g_fCSAM = true;
772 else if (strcmp(pszArg, "-nocsam") == 0)
773 g_fCSAM = false;
774#endif /* VBOXSDL_ADVANCED_OPTIONS */
775#ifdef RT_OS_L4
776 else if (strcmp(pszArg, "-env") == 0)
777 ++curArg;
778#endif /* RT_OS_L4 */
779 /* just show the help screen */
780 else
781 {
782 SyntaxError("unrecognized argument '%s'\n", pszArg);
783 show_usage();
784 return 1;
785 }
786 }
787
788 gMachineDebugger = new MachineDebugger();
789 gStatus = new VMStatus();
790 gKeyboard = new Keyboard();
791 gMouse = new Mouse();
792 gVMMDev = new VMMDev();
793 gDisplay = new VMDisplay();
794#if defined(USE_SDL)
795 /* First console, then framebuffer!! */
796 gConsole = new SDLConsole();
797 gFramebuffer = new SDLFramebuffer();
798#elif defined(RT_OS_L4)
799 gConsole = new L4Console();
800 gFramebuffer = new L4Framebuffer();
801#else
802#error "todo"
803#endif
804 if (!gConsole->initialized())
805 goto leave;
806 gDisplay->SetFramebuffer(0, gFramebuffer);
807
808 /* start with something in the titlebar */
809 gConsole->updateTitlebar();
810
811 /*
812 * Start the VM execution thread. This has to be done
813 * asynchronously as powering up can take some time
814 * (accessing devices such as the host DVD drive). In
815 * the meantime, we have to service the SDL event loop.
816 */
817
818 RTTHREAD thread;
819 rc = RTThreadCreate(&thread, VMPowerUpThread, 0, 0, RTTHREADTYPE_MAIN_WORKER, 0, "PowerUp");
820 if (RT_FAILURE(rc))
821 {
822 RTPrintf("Error: Thread creation failed with %d\n", rc);
823 return -1;
824 }
825
826#ifdef RT_OS_L4
827 /* Start the external IDL interface */
828 L4CtrlInit();
829#endif
830
831 /* loop until the powerup processing is done */
832 do
833 {
834#if defined(VBOXBFE_WITH_X11) && defined(USE_SDL)
835 if ( machineState == VMSTATE_CREATING
836 || machineState == VMSTATE_LOADING)
837 {
838 int event = gConsole->eventWait();
839
840 switch (event)
841 {
842 case CONEVENT_USR_SCREENRESIZE:
843 LogFlow(("CONEVENT_USR_SCREENRESIZE\n"));
844 gFramebuffer->resize();
845 /* notify the display that the resize has been completed */
846 gDisplay->ResizeCompleted();
847 break;
848
849 case CONEVENT_USR_TITLEBARUPDATE:
850 gConsole->updateTitlebar();
851 break;
852
853 case CONEVENT_USR_QUIT:
854 RTPrintf("Error: failed to power up VM! No error text available.\n");
855 goto leave;
856 }
857 }
858 else
859#endif
860 RTThreadSleep(1000);
861 }
862 while ( machineState == VMSTATE_CREATING
863 || machineState == VMSTATE_LOADING);
864
865 if (machineState == VMSTATE_TERMINATED)
866 goto leave;
867
868 /* did the power up succeed? */
869 if (machineState != VMSTATE_RUNNING)
870 {
871 RTPrintf("Error: failed to power up VM! No error text available (rc = 0x%x state = %d)\n", rc, machineState);
872 goto leave;
873 }
874
875 gConsole->updateTitlebar();
876
877#ifdef RT_OS_L4
878 /* The L4 console provides (currently) a fixed resolution. */
879 if (g_u32VRamSize >= gFramebuffer->getHostXres()
880 * gFramebuffer->getHostYres()
881 * (gDisplay->getBitsPerPixel() / 8))
882 gDisplay->SetVideoModeHint(gFramebuffer->getHostXres(), gFramebuffer->getHostYres(), 0, 0);
883
884 /* Limit the VRAM of the guest to the amount of memory we got actually
885 * mapped from the L4 console. */
886 u32MaxVRAM = (gFramebuffer->getHostYres() + 18) /* don't omit the status bar */
887 * gFramebuffer->getHostXres()
888 * (gFramebuffer->getHostBitsPerPixel() / 8);
889 if (g_u32VRamSize > u32MaxVRAM)
890 {
891 RTPrintf("Limiting the video memory to %u bytes\n", u32MaxVRAM);
892 g_u32VRamSize = u32MaxVRAM;
893 }
894#endif
895
896 /*
897 * Main event loop
898 */
899 LogFlow(("VBoxSDL: Entering big event loop\n"));
900
901 while (1)
902 {
903 int event = gConsole->eventWait();
904
905 switch (event)
906 {
907 case CONEVENT_NONE:
908 /* Handled internally */
909 break;
910
911 case CONEVENT_QUIT:
912 case CONEVENT_USR_QUIT:
913 goto leave;
914
915 case CONEVENT_SCREENUPDATE:
916 /// @todo that somehow doesn't seem to work!
917 gFramebuffer->repaint();
918 break;
919
920 case CONEVENT_USR_TITLEBARUPDATE:
921 gConsole->updateTitlebar();
922 break;
923
924 case CONEVENT_USR_SCREENRESIZE:
925 {
926 LogFlow(("CONEVENT_USR_SCREENRESIZE\n"));
927 gFramebuffer->resize();
928 /* notify the display that the resize has been completed */
929 gDisplay->ResizeCompleted();
930 break;
931 }
932
933#ifdef VBOX_SECURELABEL
934 case CONEVENT_USR_SECURELABELUPDATE:
935 {
936 /*
937 * Query the new label text
938 */
939 Bstr key = VBOXSDL_SECURELABEL_EXTRADATA;
940 Bstr label;
941 gMachine->COMGETTER(ExtraData)(key, label.asOutParam());
942 Utf8Str labelUtf8 = label;
943 /*
944 * Now update the label
945 */
946 gFramebuffer->setSecureLabelText(labelUtf8.raw());
947 break;
948 }
949#endif /* VBOX_SECURELABEL */
950
951 }
952
953 }
954
955leave:
956 LogFlow(("Returning from main()!\n"));
957
958 if (pVM)
959 {
960 /*
961 * If get here because the guest terminated using ACPI off we don't have to
962 * switch off the VM because we were notified via vmstateChangeCallback()
963 * that this already happened. In any other case stop the VM before killing her.
964 */
965 if (machineState != VMSTATE_OFF)
966 {
967 /* Power off VM */
968 rc = VMR3PowerOff(pVM);
969 AssertRC(rc);
970 }
971
972 /* And destroy it */
973 rc = VMR3Destroy(pVM);
974 AssertRC(rc);
975 }
976
977 delete gFramebuffer;
978 delete gConsole;
979 delete gDisplay;
980 delete gKeyboard;
981 delete gMouse;
982 delete gStatus;
983 delete gMachineDebugger;
984
985 RTLogFlush(NULL);
986 return RT_FAILURE (rc) ? 1 : 0;
987}
988
989
990#ifndef VBOX_WITH_HARDENING
991/**
992 * Main entry point.
993 */
994int main(int argc, char **argv)
995{
996# ifdef RT_OS_L4
997# ifndef L4API_l4v2onv4
998 /* clear Fiasco kernel trace buffer */
999 fiasco_tbuf_clear();
1000# endif
1001 /* set the environment. Must be done before the runtime is
1002 initialised. Yes, it really must. */
1003 for (int i = 0; i < argc; i++)
1004 if (strcmp(argv[i], "-env") == 0)
1005 {
1006 if (++i >= argc)
1007 return SyntaxError("missing argument to -env (format: var=value)!\n");
1008 /* add it to the environment */
1009 if (putenv(argv[i]) != 0)
1010 return SyntaxError("Error setting environment string %s.\n", argv[i]);
1011 }
1012# endif /* RT_OS_L4 */
1013
1014 /*
1015 * Before we do *anything*, we initialize the runtime.
1016 */
1017 int rc = RTR3Init();
1018 if (RT_FAILURE(rc))
1019 return FatalError("RTR3Init failed rc=%Rrc\n", rc);
1020
1021 return TrustedMain(argc, argv, NULL);
1022}
1023#endif /* !VBOX_WITH_HARDENING */
1024
1025
1026/**
1027 * VM state callback function. Called by the VMM
1028 * using its state machine states.
1029 *
1030 * Primarily used to handle VM initiated power off, suspend and state saving,
1031 * but also for doing termination completed work (VMSTATE_TERMINATE).
1032 *
1033 * In general this function is called in the context of the EMT.
1034 *
1035 * @todo machineState is set to VMSTATE_RUNNING before all devices have received power on events
1036 * this can prematurely allow the main thread to enter the event loop
1037 *
1038 * @param pVM The VM handle.
1039 * @param enmState The new state.
1040 * @param enmOldState The old state.
1041 * @param pvUser The user argument.
1042 */
1043static DECLCALLBACK(void) vmstateChangeCallback(PVM pVM, VMSTATE enmState, VMSTATE enmOldState, void *pvUser)
1044{
1045 LogFlow(("vmstateChangeCallback: changing state from %d to %d\n", enmOldState, enmState));
1046 machineState = enmState;
1047
1048 switch (enmState)
1049 {
1050 /*
1051 * The VM has terminated
1052 */
1053 case VMSTATE_OFF:
1054 {
1055 gConsole->eventQuit();
1056 break;
1057 }
1058
1059 /*
1060 * The VM has been completely destroyed.
1061 *
1062 * Note: This state change can happen at two points:
1063 * 1) At the end of VMR3Destroy() if it was not called from EMT.
1064 * 2) At the end of vmR3EmulationThread if VMR3Destroy() was called by EMT.
1065 */
1066 case VMSTATE_TERMINATED:
1067 {
1068 break;
1069 }
1070
1071 default: /* shut up gcc */
1072 break;
1073 }
1074}
1075
1076
1077/**
1078 * VM error callback function. Called by the various VM components.
1079 *
1080 * @param pVM The VM handle.
1081 * @param pvUser The user argument.
1082 * @param rc VBox status code.
1083 * @param pszError Error message format string.
1084 * @param args Error message arguments.
1085 * @thread EMT.
1086 */
1087DECLCALLBACK(void) setVMErrorCallback(PVM pVM, void *pvUser, int rc, RT_SRC_POS_DECL,
1088 const char *pszFormat, va_list args)
1089{
1090 /** @todo accessing shared resource without any kind of synchronization */
1091 if (RT_SUCCESS(rc))
1092 szError[0] = '\0';
1093 else
1094 {
1095 va_list va2;
1096 va_copy(va2, args); /* Have to make a copy here or GCC will break. */
1097 RTStrPrintf(szError, sizeof(szError),
1098 "%N!\nVBox status code: %d (%Rrc)", pszFormat, &va2, rc, rc);
1099 RTPrintf("%s\n", szError);
1100 va_end(va2);
1101 }
1102}
1103
1104
1105/**
1106 * VM Runtime error callback function. Called by the various VM components.
1107 *
1108 * @param pVM The VM handle.
1109 * @param pvUser The user argument.
1110 * @param fFlags The action flags. See VMSETRTERR_FLAGS_*.
1111 * @param pszErrorId Error ID string.
1112 * @param pszError Error message format string.
1113 * @param va Error message arguments.
1114 * @thread EMT.
1115 */
1116DECLCALLBACK(void) setVMRuntimeErrorCallback(PVM pVM, void *pvUser, uint32_t fFlags,
1117 const char *pszErrorId,
1118 const char *pszFormat, va_list va)
1119{
1120 va_list va2;
1121 va_copy(va2, va); /* Have to make a copy here or GCC/AMD64 will break. */
1122 RTPrintf("%s: %s!\n%N!\n",
1123 fFlags & VMSETRTERR_FLAGS_FATAL ? "Error" : "Warning",
1124 pszErrorId, pszFormat, &va2);
1125 RTStrmFlush(g_pStdErr);
1126 va_end(va2);
1127}
1128
1129
1130/** VM asynchronous operations thread */
1131DECLCALLBACK(int) VMPowerUpThread(RTTHREAD Thread, void *pvUser)
1132{
1133 int rc = VINF_SUCCESS;
1134 int rc2;
1135
1136 /*
1137 * Setup the release log instance in current directory.
1138 */
1139 if (g_fReleaseLog)
1140 {
1141 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
1142 static char szError[RTPATH_MAX + 128] = "";
1143 PRTLOGGER pLogger;
1144 rc2 = RTLogCreateEx(&pLogger, RTLOGFLAGS_PREFIX_TIME_PROG, "all",
1145 "VBOX_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups,
1146 RTLOGDEST_FILE, szError, sizeof(szError), "./VBoxBFE.log");
1147 if (RT_SUCCESS(rc2))
1148 {
1149 /* some introductory information */
1150 RTTIMESPEC TimeSpec;
1151 char szNowUct[64];
1152 RTTimeSpecToString(RTTimeNow(&TimeSpec), szNowUct, sizeof(szNowUct));
1153 RTLogRelLogger(pLogger, 0, ~0U,
1154 "VBoxBFE %s (%s %s) release log\n"
1155 "Log opened %s\n",
1156 VBOX_VERSION_STRING, __DATE__, __TIME__,
1157 szNowUct);
1158
1159 /* register this logger as the release logger */
1160 RTLogRelSetDefaultInstance(pLogger);
1161 }
1162 else
1163 RTPrintf("Could not open release log (%s)\n", szError);
1164 }
1165
1166 /*
1167 * Start VM (also from saved state) and track progress
1168 */
1169 LogFlow(("VMPowerUp\n"));
1170
1171 /*
1172 * Create empty VM.
1173 */
1174 rc = VMR3Create(1, setVMErrorCallback, NULL, vboxbfeConfigConstructor, NULL, &pVM);
1175 if (RT_FAILURE(rc))
1176 {
1177 RTPrintf("Error: VM creation failed with %Rrc.\n", rc);
1178 goto failure;
1179 }
1180
1181
1182 /*
1183 * Register VM state change handler
1184 */
1185 rc = VMR3AtStateRegister(pVM, vmstateChangeCallback, NULL);
1186 if (RT_FAILURE(rc))
1187 {
1188 RTPrintf("Error: VMR3AtStateRegister failed with %Rrc.\n", rc);
1189 goto failure;
1190 }
1191
1192#ifdef VBOX_WITH_HGCM
1193 /*
1194 * Add shared folders to the VM
1195 */
1196 if (fActivateHGCM() && gVMMDev->isShFlActive())
1197 {
1198 for (unsigned i=0; i<g_uNumShares; i++)
1199 {
1200 VBOXHGCMSVCPARM parms[SHFL_CPARMS_ADD_MAPPING];
1201 SHFLSTRING *pFolderName, *pMapName;
1202 int cbString;
1203 PRTUTF16 aHostPath, aMapName;
1204 int rc;
1205
1206 rc = RTStrToUtf16(g_pszShareDir[i], &aHostPath);
1207 AssertRC(rc);
1208 rc = RTStrToUtf16(g_pszShareName[i], &aMapName);
1209 AssertRC(rc);
1210
1211 cbString = (RTUtf16Len (aHostPath) + 1) * sizeof (RTUTF16);
1212 pFolderName = (SHFLSTRING *) RTMemAllocZ (sizeof (SHFLSTRING) + cbString);
1213 Assert (pFolderName);
1214 memcpy (pFolderName->String.ucs2, aHostPath, cbString);
1215
1216 pFolderName->u16Size = cbString;
1217 pFolderName->u16Length = cbString - sizeof(RTUTF16);
1218
1219 parms[0].type = VBOX_HGCM_SVC_PARM_PTR;
1220 parms[0].u.pointer.addr = pFolderName;
1221 parms[0].u.pointer.size = sizeof (SHFLSTRING) + cbString;
1222
1223 cbString = (RTUtf16Len (aMapName) + 1) * sizeof (RTUTF16);
1224 pMapName = (SHFLSTRING *) RTMemAllocZ (sizeof(SHFLSTRING) + cbString);
1225 Assert (pMapName);
1226 memcpy (pMapName->String.ucs2, aMapName, cbString);
1227
1228 pMapName->u16Size = cbString;
1229 pMapName->u16Length = cbString - sizeof (RTUTF16);
1230
1231 parms[1].type = VBOX_HGCM_SVC_PARM_PTR;
1232 parms[1].u.pointer.addr = pMapName;
1233 parms[1].u.pointer.size = sizeof (SHFLSTRING) + cbString;
1234
1235 parms[2].type = VBOX_HGCM_SVC_PARM_32BIT;
1236 parms[2].u.uint32 = !g_fShareReadOnly[i];
1237
1238 rc = gVMMDev->hgcmHostCall ("VBoxSharedFolders",
1239 SHFL_FN_ADD_MAPPING, SHFL_CPARMS_ADD_MAPPING, &parms[0]);
1240 AssertRC(rc);
1241 LogRel(("Added share %s: (%s)\n", g_pszShareName[i], g_pszShareDir[i]));
1242 RTMemFree (pFolderName);
1243 RTMemFree (pMapName);
1244 RTUtf16Free (aHostPath);
1245 RTUtf16Free (aMapName);
1246 }
1247 }
1248#endif
1249
1250#ifdef VBOXBFE_WITH_USB
1251 /*
1252 * Capture USB devices.
1253 */
1254 if (g_fUSB)
1255 {
1256 gHostUSB = new HostUSB();
1257 gHostUSB->init(pVM);
1258 }
1259#endif /* VBOXBFE_WITH_USB */
1260
1261#ifdef RT_OS_L4
1262 /* L4 console cannot draw a host cursor */
1263 gMouse->setHostCursor(false);
1264#else
1265 gMouse->setHostCursor(true);
1266#endif
1267
1268 /*
1269 * Power on the VM (i.e. start executing).
1270 */
1271 if (RT_SUCCESS(rc))
1272 {
1273 if ( g_fRestoreState
1274 && g_pszStateFile
1275 && *g_pszStateFile
1276 && RTPathExists(g_pszStateFile))
1277 {
1278 startProgressInfo("Restoring");
1279 rc = VMR3LoadFromFile(pVM, g_pszStateFile, callProgressInfo, (uintptr_t)NULL);
1280 endProgressInfo();
1281 if (RT_SUCCESS(rc))
1282 {
1283 rc = VMR3Resume(pVM);
1284 AssertRC(rc);
1285 gDisplay->setRunning();
1286 }
1287 else
1288 AssertMsgFailed(("VMR3LoadFromFile failed, rc=%Rrc\n", rc));
1289 }
1290 else
1291 {
1292 rc = VMR3PowerOn(pVM);
1293 if (RT_FAILURE(rc))
1294 AssertMsgFailed(("VMR3PowerOn failed, rc=%Rrc\n", rc));
1295 }
1296 }
1297
1298 /*
1299 * On failure destroy the VM.
1300 */
1301 if (RT_FAILURE(rc))
1302 goto failure;
1303
1304 return 0;
1305
1306failure:
1307 if (pVM)
1308 {
1309 rc2 = VMR3Destroy(pVM);
1310 AssertRC(rc2);
1311 pVM = NULL;
1312 }
1313 machineState = VMSTATE_TERMINATED;
1314
1315 return 0;
1316}
1317
1318/**
1319 * Register the main drivers.
1320 *
1321 * @returns VBox status code.
1322 * @param pCallbacks Pointer to the callback table.
1323 * @param u32Version VBox version number.
1324 */
1325DECLCALLBACK(int) VBoxDriversRegister(PCPDMDRVREGCB pCallbacks, uint32_t u32Version)
1326{
1327 int rc;
1328
1329 LogFlow(("VBoxDriversRegister: u32Version=%#x\n", u32Version));
1330 AssertReleaseMsg(u32Version == VBOX_VERSION, ("u32Version=%#x VBOX_VERSION=%#x\n", u32Version, VBOX_VERSION));
1331
1332 rc = pCallbacks->pfnRegister(pCallbacks, &Mouse::DrvReg);
1333 AssertRC(rc);
1334 if (RT_FAILURE(rc))
1335 return rc;
1336 rc = pCallbacks->pfnRegister(pCallbacks, &Keyboard::DrvReg);
1337 AssertRC(rc);
1338 if (RT_FAILURE(rc))
1339 return rc;
1340
1341 rc = pCallbacks->pfnRegister(pCallbacks, &VMDisplay::DrvReg);
1342 AssertRC(rc);
1343 if (RT_FAILURE(rc))
1344 return rc;
1345 rc = pCallbacks->pfnRegister(pCallbacks, &VMMDev::DrvReg);
1346 AssertRC(rc);
1347 if (RT_FAILURE(rc))
1348 return rc;
1349
1350 rc = pCallbacks->pfnRegister(pCallbacks, &VMStatus::DrvReg);
1351 if (RT_FAILURE(rc))
1352 return rc;
1353
1354 return VINF_SUCCESS;
1355}
1356
1357
1358/**
1359 * Constructs the VMM configuration tree.
1360 *
1361 * @returns VBox status code.
1362 * @param pVM VM handle.
1363 */
1364static DECLCALLBACK(int) vboxbfeConfigConstructor(PVM pVM, void *pvUser)
1365{
1366 int rcAll = VINF_SUCCESS;
1367 int rc;
1368
1369#define UPDATE_RC() do { if (RT_FAILURE(rc) && RT_SUCCESS(rcAll)) rcAll = rc; } while (0)
1370
1371 /*
1372 * Root values.
1373 */
1374 PCFGMNODE pRoot = CFGMR3GetRoot(pVM);
1375 rc = CFGMR3InsertString(pRoot, "Name", "Default VM"); UPDATE_RC();
1376 rc = CFGMR3InsertInteger(pRoot, "RamSize", g_u32MemorySizeMB * _1M); UPDATE_RC();
1377 rc = CFGMR3InsertInteger(pRoot, "RamHoleSize", 512U * _1M); UPDATE_RC();
1378 if (g_fPreAllocRam)
1379 {
1380 rc = CFGMR3InsertInteger(pRoot, "RamPreAlloc", 1); UPDATE_RC();
1381 }
1382 rc = CFGMR3InsertInteger(pRoot, "TimerMillies", 10); UPDATE_RC();
1383#ifdef VBOXSDL_ADVANCED_OPTIONS
1384 rc = CFGMR3InsertInteger(pRoot, "RawR3Enabled", g_fRawR3); UPDATE_RC();
1385 rc = CFGMR3InsertInteger(pRoot, "RawR0Enabled", g_fRawR0); UPDATE_RC();
1386 rc = CFGMR3InsertInteger(pRoot, "PATMEnabled", g_fPATM); UPDATE_RC();
1387 rc = CFGMR3InsertInteger(pRoot, "CSAMEnabled", g_fCSAM); UPDATE_RC();
1388#else
1389 rc = CFGMR3InsertInteger(pRoot, "RawR3Enabled", 1); UPDATE_RC();
1390 rc = CFGMR3InsertInteger(pRoot, "RawR0Enabled", 1); UPDATE_RC();
1391 rc = CFGMR3InsertInteger(pRoot, "PATMEnabled", 1); UPDATE_RC();
1392 rc = CFGMR3InsertInteger(pRoot, "CSAMEnabled", 1); UPDATE_RC();
1393#endif
1394
1395 /*
1396 * PDM.
1397 */
1398 rc = PDMR3RegisterDrivers(pVM, VBoxDriversRegister); UPDATE_RC();
1399
1400 /*
1401 * Devices
1402 */
1403 PCFGMNODE pDevices = NULL;
1404 rc = CFGMR3InsertNode(pRoot, "Devices", &pDevices); UPDATE_RC();
1405 /* device */
1406 PCFGMNODE pDev = NULL;
1407 PCFGMNODE pInst = NULL;
1408 PCFGMNODE pCfg = NULL;
1409 PCFGMNODE pLunL0 = NULL;
1410 PCFGMNODE pLunL1 = NULL;
1411 PCFGMNODE pDrv = NULL;
1412
1413 /*
1414 * PC Arch.
1415 */
1416 rc = CFGMR3InsertNode(pDevices, "pcarch", &pDev); UPDATE_RC();
1417 rc = CFGMR3InsertNode(pDev, "0", &pInst); UPDATE_RC();
1418 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ UPDATE_RC();
1419 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); UPDATE_RC();
1420
1421 /*
1422 * PC Bios.
1423 */
1424 rc = CFGMR3InsertNode(pDevices, "pcbios", &pDev); UPDATE_RC();
1425 rc = CFGMR3InsertNode(pDev, "0", &pInst); UPDATE_RC();
1426 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ UPDATE_RC();
1427 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); UPDATE_RC();
1428 rc = CFGMR3InsertInteger(pCfg, "RamSize", g_u32MemorySizeMB * _1M); UPDATE_RC();
1429 rc = CFGMR3InsertInteger(pCfg, "RamHoleSize", 512U * _1M); UPDATE_RC();
1430 rc = CFGMR3InsertString(pCfg, "BootDevice0", g_pszBootDevice); UPDATE_RC();
1431 rc = CFGMR3InsertString(pCfg, "BootDevice1", "NONE"); UPDATE_RC();
1432 rc = CFGMR3InsertString(pCfg, "BootDevice2", "NONE"); UPDATE_RC();
1433 rc = CFGMR3InsertString(pCfg, "BootDevice3", "NONE"); UPDATE_RC();
1434 rc = CFGMR3InsertString(pCfg, "HardDiskDevice", "piix3ide"); UPDATE_RC();
1435 rc = CFGMR3InsertString(pCfg, "FloppyDevice", "i82078"); UPDATE_RC();
1436 rc = CFGMR3InsertInteger(pCfg, "IOAPIC", g_fIOAPIC); UPDATE_RC();
1437 RTUUID Uuid;
1438 RTUuidClear(&Uuid);
1439 rc = CFGMR3InsertBytes(pCfg, "UUID", &Uuid, sizeof(Uuid)); UPDATE_RC();
1440
1441 /*
1442 * ACPI
1443 */
1444 if (g_fACPI)
1445 {
1446 rc = CFGMR3InsertNode(pDevices, "acpi", &pDev); UPDATE_RC();
1447 rc = CFGMR3InsertNode(pDev, "0", &pInst); UPDATE_RC();
1448 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ UPDATE_RC();
1449 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); UPDATE_RC();
1450 rc = CFGMR3InsertInteger(pCfg, "RamSize", g_u32MemorySizeMB * _1M); UPDATE_RC();
1451 rc = CFGMR3InsertInteger(pCfg, "RamHoleSize", 512U * _1M); UPDATE_RC();
1452 rc = CFGMR3InsertInteger(pCfg, "IOAPIC", g_fIOAPIC); UPDATE_RC();
1453 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 7); UPDATE_RC();
1454 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); UPDATE_RC();
1455
1456 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); UPDATE_RC();
1457 rc = CFGMR3InsertString(pLunL0, "Driver", "ACPIHost"); UPDATE_RC();
1458 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); UPDATE_RC();
1459 }
1460
1461 /*
1462 * PCI bus.
1463 */
1464 rc = CFGMR3InsertNode(pDevices, "pci", &pDev); /* piix3 */ UPDATE_RC();
1465 rc = CFGMR3InsertNode(pDev, "0", &pInst); UPDATE_RC();
1466 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ UPDATE_RC();
1467 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); UPDATE_RC();
1468 rc = CFGMR3InsertInteger(pCfg, "IOAPIC", g_fIOAPIC); UPDATE_RC();
1469
1470 /*
1471 * DMA
1472 */
1473 rc = CFGMR3InsertNode(pDevices, "8237A", &pDev); UPDATE_RC();
1474 rc = CFGMR3InsertNode(pDev, "0", &pInst); UPDATE_RC();
1475 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ UPDATE_RC();
1476
1477 /*
1478 * PCI bus.
1479 */
1480 rc = CFGMR3InsertNode(pDevices, "pci", &pDev); /* piix3 */ UPDATE_RC();
1481 rc = CFGMR3InsertNode(pDev, "0", &pInst); UPDATE_RC();
1482 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ UPDATE_RC();
1483 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); UPDATE_RC();
1484
1485 /*
1486 * PS/2 keyboard & mouse.
1487 */
1488 rc = CFGMR3InsertNode(pDevices, "pckbd", &pDev); UPDATE_RC();
1489 rc = CFGMR3InsertNode(pDev, "0", &pInst); UPDATE_RC();
1490 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ UPDATE_RC();
1491 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); UPDATE_RC();
1492
1493 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); UPDATE_RC();
1494 rc = CFGMR3InsertString(pLunL0, "Driver", "KeyboardQueue"); UPDATE_RC();
1495 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); UPDATE_RC();
1496 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 64); UPDATE_RC();
1497
1498 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pDrv); UPDATE_RC();
1499 rc = CFGMR3InsertString(pDrv, "Driver", "MainKeyboard"); UPDATE_RC();
1500 rc = CFGMR3InsertNode(pDrv, "Config", &pCfg); UPDATE_RC();
1501 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)gKeyboard); UPDATE_RC();
1502
1503 rc = CFGMR3InsertNode(pInst, "LUN#1", &pLunL0); UPDATE_RC();
1504 rc = CFGMR3InsertString(pLunL0, "Driver", "MouseQueue"); UPDATE_RC();
1505 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); UPDATE_RC();
1506 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 128); UPDATE_RC();
1507
1508 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pDrv); UPDATE_RC();
1509 rc = CFGMR3InsertString(pDrv, "Driver", "MainMouse"); UPDATE_RC();
1510 rc = CFGMR3InsertNode(pDrv, "Config", &pCfg); UPDATE_RC();
1511 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)gMouse); UPDATE_RC();
1512
1513
1514 /*
1515 * i82078 Floppy drive controller
1516 */
1517 rc = CFGMR3InsertNode(pDevices, "i82078", &pDev); UPDATE_RC();
1518 rc = CFGMR3InsertNode(pDev, "0", &pInst); UPDATE_RC();
1519 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); UPDATE_RC();
1520 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); UPDATE_RC();
1521 rc = CFGMR3InsertInteger(pCfg, "IRQ", 6); UPDATE_RC();
1522 rc = CFGMR3InsertInteger(pCfg, "DMA", 2); UPDATE_RC();
1523 rc = CFGMR3InsertInteger(pCfg, "MemMapped", 0 ); UPDATE_RC();
1524 rc = CFGMR3InsertInteger(pCfg, "IOBase", 0x3f0); UPDATE_RC();
1525
1526 /* Attach the status driver */
1527 rc = CFGMR3InsertNode(pInst, "LUN#999", &pLunL0); UPDATE_RC();
1528 rc = CFGMR3InsertString(pLunL0, "Driver", "MainStatus"); UPDATE_RC();
1529 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); UPDATE_RC();
1530 rc = CFGMR3InsertInteger(pCfg, "papLeds", (uintptr_t)&mapFDLeds[0]); UPDATE_RC();
1531 rc = CFGMR3InsertInteger(pCfg, "First", 0); UPDATE_RC();
1532 rc = CFGMR3InsertInteger(pCfg, "Last", 0); UPDATE_RC();
1533
1534 if (g_pszFdaFile)
1535 {
1536 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); UPDATE_RC();
1537 rc = CFGMR3InsertString(pLunL0, "Driver", "Block"); UPDATE_RC();
1538 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); UPDATE_RC();
1539 rc = CFGMR3InsertString(pCfg, "Type", "Floppy 1.44"); UPDATE_RC();
1540 rc = CFGMR3InsertInteger(pCfg, "Mountable", 1); UPDATE_RC();
1541
1542 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pDrv); UPDATE_RC();
1543 rc = CFGMR3InsertString(pDrv, "Driver", "RawImage"); UPDATE_RC();
1544 rc = CFGMR3InsertNode(pDrv, "Config", &pCfg); UPDATE_RC();
1545 rc = CFGMR3InsertString(pCfg, "Path", g_pszFdaFile); UPDATE_RC();
1546 }
1547
1548 /*
1549 * i8254 Programmable Interval Timer And Dummy Speaker
1550 */
1551 rc = CFGMR3InsertNode(pDevices, "i8254", &pDev); UPDATE_RC();
1552 rc = CFGMR3InsertNode(pDev, "0", &pInst); UPDATE_RC();
1553 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); UPDATE_RC();
1554#ifdef DEBUG
1555 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ UPDATE_RC();
1556#endif
1557
1558 /*
1559 * i8259 Programmable Interrupt Controller.
1560 */
1561 rc = CFGMR3InsertNode(pDevices, "i8259", &pDev); UPDATE_RC();
1562 rc = CFGMR3InsertNode(pDev, "0", &pInst); UPDATE_RC();
1563 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ UPDATE_RC();
1564 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); UPDATE_RC();
1565
1566 /*
1567 * Advanced Programmable Interrupt Controller.
1568 */
1569 rc = CFGMR3InsertNode(pDevices, "apic", &pDev); UPDATE_RC();
1570 rc = CFGMR3InsertNode(pDev, "0", &pInst); UPDATE_RC();
1571 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ UPDATE_RC();
1572 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); UPDATE_RC();
1573 rc = CFGMR3InsertInteger(pCfg, "IOAPIC", g_fIOAPIC); UPDATE_RC();
1574
1575 /*
1576 * I/O Advanced Programmable Interrupt Controller.
1577 */
1578 if (g_fIOAPIC)
1579 {
1580 rc = CFGMR3InsertNode(pDevices, "ioapic", &pDev); UPDATE_RC();
1581 rc = CFGMR3InsertNode(pDev, "0", &pInst); UPDATE_RC();
1582 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ UPDATE_RC();
1583 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); UPDATE_RC();
1584 }
1585
1586 /*
1587 * RTC MC146818.
1588 */
1589 rc = CFGMR3InsertNode(pDevices, "mc146818", &pDev); UPDATE_RC();
1590 rc = CFGMR3InsertNode(pDev, "0", &pInst); UPDATE_RC();
1591 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); UPDATE_RC();
1592
1593 /*
1594 * Serial ports
1595 */
1596 rc = CFGMR3InsertNode(pDevices, "serial", &pDev); UPDATE_RC();
1597 rc = CFGMR3InsertNode(pDev, "0", &pInst); UPDATE_RC();
1598 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); UPDATE_RC();
1599 rc = CFGMR3InsertInteger(pCfg, "IRQ", 4); UPDATE_RC();
1600 rc = CFGMR3InsertInteger(pCfg, "IOBase", 0x3f8); UPDATE_RC();
1601
1602 rc = CFGMR3InsertNode(pDev, "1", &pInst); UPDATE_RC();
1603 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); UPDATE_RC();
1604 rc = CFGMR3InsertInteger(pCfg, "IRQ", 3); UPDATE_RC();
1605 rc = CFGMR3InsertInteger(pCfg, "IOBase", 0x2f8); UPDATE_RC();
1606
1607 /*
1608 * VGA.
1609 */
1610 rc = CFGMR3InsertNode(pDevices, "vga", &pDev); UPDATE_RC();
1611 rc = CFGMR3InsertNode(pDev, "0", &pInst); UPDATE_RC();
1612 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ UPDATE_RC();
1613 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 2); UPDATE_RC();
1614 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); UPDATE_RC();
1615 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); UPDATE_RC();
1616 rc = CFGMR3InsertInteger(pCfg, "VRamSize", g_u32VRamSize); UPDATE_RC();
1617
1618 /* Default: no bios logo. */
1619 rc = CFGMR3InsertInteger(pCfg, "FadeIn", 1); UPDATE_RC();
1620 rc = CFGMR3InsertInteger(pCfg, "FadeOut", 0); UPDATE_RC();
1621 rc = CFGMR3InsertInteger(pCfg, "LogoTime", 0); UPDATE_RC();
1622 rc = CFGMR3InsertString(pCfg, "LogoFile", ""); UPDATE_RC();
1623
1624 /* Boot menu */
1625 rc = CFGMR3InsertInteger(pCfg, "ShowBootMenu", g_iBootMenu); UPDATE_RC();
1626
1627#ifdef RT_OS_L4
1628 /* XXX hard-coded */
1629 rc = CFGMR3InsertInteger(pCfg, "HeightReduction", 18); UPDATE_RC();
1630 rc = CFGMR3InsertInteger(pCfg, "CustomVideoModes", 1); UPDATE_RC();
1631 char szBuf[64];
1632 /* Tell the guest which is the ideal video mode to use */
1633 RTStrPrintf(szBuf, sizeof(szBuf), "%dx%dx%d",
1634 gFramebuffer->getHostXres(),
1635 gFramebuffer->getHostYres(),
1636 gFramebuffer->getHostBitsPerPixel());
1637 rc = CFGMR3InsertString(pCfg, "CustomVideoMode1", szBuf); UPDATE_RC();
1638#endif
1639
1640 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); UPDATE_RC();
1641 rc = CFGMR3InsertString(pLunL0, "Driver", "MainDisplay"); UPDATE_RC();
1642 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); UPDATE_RC();
1643 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)gDisplay); UPDATE_RC();
1644
1645 /*
1646 * IDE (update this when the main interface changes)
1647 */
1648 rc = CFGMR3InsertNode(pDevices, "piix3ide", &pDev); /* piix3 */ UPDATE_RC();
1649 rc = CFGMR3InsertNode(pDev, "0", &pInst); UPDATE_RC();
1650 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ UPDATE_RC();
1651 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 1); UPDATE_RC();
1652 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 1); UPDATE_RC();
1653 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); UPDATE_RC();
1654
1655 if (g_pszHdaFile)
1656 {
1657 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); UPDATE_RC();
1658 rc = CFGMR3InsertString(pLunL0, "Driver", "Block"); UPDATE_RC();
1659 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); UPDATE_RC();
1660 rc = CFGMR3InsertString(pCfg, "Type", "HardDisk"); UPDATE_RC();
1661 rc = CFGMR3InsertInteger(pCfg, "Mountable", 0); UPDATE_RC();
1662
1663 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pDrv); UPDATE_RC();
1664 rc = CFGMR3InsertString(pDrv, "Driver", "VD"); UPDATE_RC();
1665 rc = CFGMR3InsertNode(pDrv, "Config", &pCfg); UPDATE_RC();
1666 rc = CFGMR3InsertString(pCfg, "Path", g_pszHdaFile); UPDATE_RC();
1667
1668 if (g_fHdaSpf)
1669 {
1670 rc = CFGMR3InsertString(pCfg, "Format", "SPF"); UPDATE_RC();
1671 }
1672 else
1673 {
1674 char *pcExt = RTPathExt(g_pszHdaFile);
1675 if ((pcExt) && (!strcmp(pcExt, ".vdi")))
1676 {
1677 rc = CFGMR3InsertString(pCfg, "Format", "VDI"); UPDATE_RC();
1678 }
1679 else
1680 {
1681 rc = CFGMR3InsertString(pCfg, "Format", "VMDK"); UPDATE_RC();
1682 }
1683 }
1684 }
1685
1686 if (g_pszHdbFile)
1687 {
1688 rc = CFGMR3InsertNode(pInst, "LUN#1", &pLunL1); UPDATE_RC();
1689 rc = CFGMR3InsertString(pLunL1, "Driver", "Block"); UPDATE_RC();
1690 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); UPDATE_RC();
1691 rc = CFGMR3InsertString(pCfg, "Type", "HardDisk"); UPDATE_RC();
1692 rc = CFGMR3InsertInteger(pCfg, "Mountable", 0); UPDATE_RC();
1693
1694 rc = CFGMR3InsertNode(pLunL1, "AttachedDriver", &pDrv); UPDATE_RC();
1695 rc = CFGMR3InsertString(pDrv, "Driver", "VD"); UPDATE_RC();
1696 rc = CFGMR3InsertNode(pDrv, "Config", &pCfg); UPDATE_RC();
1697 rc = CFGMR3InsertString(pCfg, "Path", g_pszHdbFile); UPDATE_RC();
1698
1699 if (g_fHdbSpf)
1700 {
1701 rc = CFGMR3InsertString(pCfg, "Format", "SPF"); UPDATE_RC();
1702 }
1703 else
1704 {
1705 char *pcExt = RTPathExt(g_pszHdbFile);
1706 if ((pcExt) && (!strcmp(pcExt, ".vdi")))
1707 {
1708 rc = CFGMR3InsertString(pCfg, "Format", "VDI"); UPDATE_RC();
1709 }
1710 else
1711 {
1712 rc = CFGMR3InsertString(pCfg, "Format", "VMDK"); UPDATE_RC();
1713 }
1714 }
1715 }
1716
1717 if (g_pszCdromFile)
1718 {
1719 // ASSUME: DVD drive is always attached to LUN#2 (i.e. secondary IDE master)
1720 rc = CFGMR3InsertNode(pInst, "LUN#2", &pLunL0); UPDATE_RC();
1721 rc = CFGMR3InsertString(pLunL0, "Driver", "Block"); UPDATE_RC();
1722 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); UPDATE_RC();
1723 rc = CFGMR3InsertString(pCfg, "Type", "DVD"); UPDATE_RC();
1724 rc = CFGMR3InsertInteger(pCfg, "Mountable", 1); UPDATE_RC();
1725
1726 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pDrv); UPDATE_RC();
1727 rc = CFGMR3InsertString(pDrv, "Driver", "MediaISO"); UPDATE_RC();
1728 rc = CFGMR3InsertNode(pDrv, "Config", &pCfg); UPDATE_RC();
1729 rc = CFGMR3InsertString(pCfg, "Path", g_pszCdromFile); UPDATE_RC();
1730 }
1731
1732 /*
1733 * Network adapters
1734 */
1735 rc = CFGMR3InsertNode(pDevices, "pcnet", &pDev); UPDATE_RC();
1736 for (ULONG ulInstance = 0; ulInstance < NetworkAdapterCount; ulInstance++)
1737 {
1738 if (g_aNetDevs[ulInstance].enmType != BFENETDEV::NOT_CONFIGURED)
1739 {
1740 char szInstance[4];
1741 RTStrPrintf(szInstance, sizeof(szInstance), "%lu", ulInstance);
1742 rc = CFGMR3InsertNode(pDev, szInstance, &pInst); UPDATE_RC();
1743 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); UPDATE_RC();
1744 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo",
1745 !ulInstance ? 3 : ulInstance - 1 + 8); UPDATE_RC();
1746 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); UPDATE_RC();
1747 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); UPDATE_RC();
1748 rc = CFGMR3InsertBytes(pCfg, "MAC", &g_aNetDevs[ulInstance].Mac, sizeof(RTMAC));
1749 UPDATE_RC();
1750
1751 /*
1752 * Enable the packet sniffer if requested.
1753 */
1754 if (g_aNetDevs[ulInstance].fSniff)
1755 {
1756 /* insert the sniffer filter driver. */
1757 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); UPDATE_RC();
1758 rc = CFGMR3InsertString(pLunL0, "Driver", "NetSniffer"); UPDATE_RC();
1759 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); UPDATE_RC();
1760 if (g_aNetDevs[ulInstance].pszSniff)
1761 {
1762 rc = CFGMR3InsertString(pCfg, "File", g_aNetDevs[ulInstance].pszSniff);
1763 UPDATE_RC();
1764 }
1765 }
1766
1767 /*
1768 * Create the driver config (if any).
1769 */
1770 if (g_aNetDevs[ulInstance].enmType != BFENETDEV::NONE)
1771 {
1772 if (g_aNetDevs[ulInstance].fSniff)
1773 {
1774 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); UPDATE_RC();
1775 }
1776 else
1777 {
1778 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); UPDATE_RC();
1779 }
1780 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); UPDATE_RC();
1781 }
1782
1783 /*
1784 * Configure the driver.
1785 */
1786 if (g_aNetDevs[ulInstance].enmType == BFENETDEV::NAT)
1787 {
1788 rc = CFGMR3InsertString(pLunL0, "Driver", "NAT"); UPDATE_RC();
1789 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); UPDATE_RC();
1790 /* (Port forwarding goes here.) */
1791 }
1792 else if (g_aNetDevs[ulInstance].enmType == BFENETDEV::HIF)
1793 {
1794 rc = CFGMR3InsertString(pLunL0, "Driver", "HostInterface"); UPDATE_RC();
1795 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); UPDATE_RC();
1796
1797#if defined(RT_OS_LINUX)
1798 if (g_aNetDevs[ulInstance].fHaveFd)
1799 {
1800 rc = CFGMR3InsertString(pCfg, "Device", g_aNetDevs[ulInstance].pszName); UPDATE_RC();
1801 rc = CFGMR3InsertInteger(pCfg, "FileHandle", g_aNetDevs[ulInstance].fd); UPDATE_RC();
1802 }
1803 else
1804#endif
1805 {
1806#if defined(RT_OS_LINUX) || defined(RT_OS_L4)
1807 /*
1808 * Create/Open the TAP the device.
1809 */
1810 RTFILE tapFD;
1811 rc = RTFileOpen(&tapFD, "/dev/net/tun",
1812 RTFILE_O_READWRITE | RTFILE_O_OPEN |
1813 RTFILE_O_DENY_NONE | RTFILE_O_INHERIT);
1814 if (RT_FAILURE(rc))
1815 {
1816 FatalError("Failed to open /dev/net/tun: %Rrc\n", rc);
1817 return rc;
1818 }
1819
1820 struct ifreq IfReq;
1821 memset(&IfReq, 0, sizeof(IfReq));
1822 if (g_aNetDevs[ulInstance].pszName && g_aNetDevs[ulInstance].pszName[0])
1823 {
1824 size_t cch = strlen(g_aNetDevs[ulInstance].pszName);
1825 if (cch >= sizeof(IfReq.ifr_name))
1826 {
1827 FatalError("HIF name too long for device #%d: %s\n",
1828 ulInstance + 1, g_aNetDevs[ulInstance].pszName);
1829 return VERR_BUFFER_OVERFLOW;
1830 }
1831 memcpy(IfReq.ifr_name, g_aNetDevs[ulInstance].pszName, cch + 1);
1832 }
1833 else
1834 strcpy(IfReq.ifr_name, "tun%d");
1835 IfReq.ifr_flags = IFF_TAP | IFF_NO_PI;
1836 rc = ioctl(tapFD, TUNSETIFF, &IfReq);
1837 if (rc)
1838 {
1839 int rc2 = RTErrConvertFromErrno(errno);
1840 FatalError("ioctl TUNSETIFF '%s' failed: errno=%d rc=%d (%Rrc)\n",
1841 IfReq.ifr_name, errno, rc, rc2);
1842 return rc2;
1843 }
1844
1845 rc = fcntl(tapFD, F_SETFL, O_NONBLOCK);
1846 if (rc)
1847 {
1848 int rc2 = RTErrConvertFromErrno(errno);
1849 FatalError("fcntl F_SETFL/O_NONBLOCK '%s' failed: errno=%d rc=%d (%Rrc)\n",
1850 IfReq.ifr_name, errno, rc, rc2);
1851 return rc2;
1852 }
1853
1854 rc = CFGMR3InsertString(pCfg, "Device", g_aNetDevs[ulInstance].pszName); UPDATE_RC();
1855 rc = CFGMR3InsertInteger(pCfg, "FileHandle", (RTFILE)tapFD); UPDATE_RC();
1856
1857#elif defined(RT_OS_SOLARIS)
1858 rc = CFGMR3InsertString(pCfg, "Device", g_aNetDevs[ulInstance].pszName); UPDATE_RC();
1859# ifdef VBOX_WITH_CROSSBOW
1860 rc = CFGMR3InsertBytes(pCfg, "MAC", &g_aNetDevs[ulInstance].Mac, sizeof(g_aNetDevs[ulInstance].Mac));
1861 UPDATE_RC();
1862# endif
1863
1864#elif defined(RT_OS_OS2)
1865 /*
1866 * The TAP driver does all the opening and setting up,
1867 * as it was originally was ment to be (stupid fork() problems).
1868 */
1869 rc = CFGMR3InsertString(pCfg, "Device", g_aNetDevs[ulInstance].pszName); UPDATE_RC();
1870 if (g_aNetDevs[ulInstance].fHaveConnectTo)
1871 {
1872 rc = CFGMR3InsertInteger(pCfg, "ConnectTo", g_aNetDevs[ulInstance].iConnectTo);
1873 UPDATE_RC();
1874 }
1875#elif defined(RT_OS_WINDOWS)
1876 /*
1877 * We need the GUID too here...
1878 */
1879 rc = CFGMR3InsertString(pCfg, "Device", g_aNetDevs[ulInstance].pszName); UPDATE_RC();
1880 rc = CFGMR3InsertString(pCfg, "HostInterfaceName", g_aNetDevs[ulInstance].pszName); UPDATE_RC();
1881 rc = CFGMR3InsertString(pCfg, "GUID", g_aNetDevs[ulInstance].pszName /*pszGUID*/); UPDATE_RC();
1882
1883
1884#else
1885 FatalError("Name based HIF devices not implemented yet for this host platform\n");
1886 return VERR_NOT_IMPLEMENTED;
1887#endif
1888 }
1889 }
1890 else if (g_aNetDevs[ulInstance].enmType == BFENETDEV::INTNET)
1891 {
1892 /*
1893 * Internal networking.
1894 */
1895 rc = CFGMR3InsertString(pCfg, "Network", g_aNetDevs[ulInstance].pszName); UPDATE_RC();
1896 }
1897 }
1898 }
1899
1900 /*
1901 * VMM Device
1902 */
1903 rc = CFGMR3InsertNode(pDevices, "VMMDev", &pDev); UPDATE_RC();
1904 rc = CFGMR3InsertNode(pDev, "0", &pInst); UPDATE_RC();
1905 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); UPDATE_RC();
1906 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ UPDATE_RC();
1907 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 4); UPDATE_RC();
1908 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); UPDATE_RC();
1909
1910 /* the VMM device's Main driver */
1911 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); UPDATE_RC();
1912 rc = CFGMR3InsertString(pLunL0, "Driver", "MainVMMDev"); UPDATE_RC();
1913 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); UPDATE_RC();
1914 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)gVMMDev); UPDATE_RC();
1915
1916 /*
1917 * AC'97 ICH audio
1918 */
1919 if (g_fAudio)
1920 {
1921 rc = CFGMR3InsertNode(pDevices, "ichac97", &pDev); UPDATE_RC();
1922 rc = CFGMR3InsertNode(pDev, "0", &pInst); UPDATE_RC();
1923 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ UPDATE_RC();
1924 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 5); UPDATE_RC();
1925 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); UPDATE_RC();
1926 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1927
1928 /* the Audio driver */
1929 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); UPDATE_RC();
1930 rc = CFGMR3InsertString(pLunL0, "Driver", "AUDIO"); UPDATE_RC();
1931 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); UPDATE_RC();
1932#ifdef RT_OS_WINDOWS
1933 rc = CFGMR3InsertString(pCfg, "AudioDriver", "winmm"); UPDATE_RC();
1934#elif defined(RT_OS_DARWIN)
1935 rc = CFGMR3InsertString(pCfg, "AudioDriver", "coreaudio"); UPDATE_RC();
1936#elif defined(RT_OS_LINUX)
1937 rc = CFGMR3InsertString(pCfg, "AudioDriver", "oss"); UPDATE_RC();
1938#elif defined(RT_OS_SOLARIS)
1939# ifdef VBOX_WITH_SOLARIS_OSS
1940 rc = CFGMR3InsertString(pCfg, "AudioDriver", "oss"); UPDATE_RC();
1941# else
1942 rc = CFGMR3InsertString(pCfg, "AudioDriver", "solaudio"); UPDATE_RC();
1943# endif
1944#elif defined(RT_OS_L4)
1945 rc = CFGMR3InsertString(pCfg, "AudioDriver", "oss"); UPDATE_RC();
1946#else /* portme */
1947 rc = CFGMR3InsertString(pCfg, "AudioDriver", "none"); UPDATE_RC();
1948#endif /* !RT_OS_WINDOWS */
1949 }
1950
1951#ifdef VBOXBFE_WITH_USB
1952 /*
1953 * The USB Controller.
1954 */
1955 if (g_fUSB)
1956 {
1957 rc = CFGMR3InsertNode(pDevices, "usb-ohci", &pDev); UPDATE_RC();
1958 rc = CFGMR3InsertNode(pDev, "0", &pInst); UPDATE_RC();
1959 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); UPDATE_RC();
1960 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ UPDATE_RC();
1961 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 6); UPDATE_RC();
1962 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); UPDATE_RC();
1963
1964 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); UPDATE_RC();
1965 rc = CFGMR3InsertString(pLunL0, "Driver", "VUSBRootHub"); UPDATE_RC();
1966 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); UPDATE_RC();
1967 }
1968#endif /* VBOXBFE_WITH_USB */
1969
1970#undef UPDATE_RC
1971#undef UPDATE_RC
1972
1973 VMR3AtRuntimeErrorRegister (pVM, setVMRuntimeErrorCallback, NULL);
1974
1975 return rc;
1976}
1977
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