VirtualBox

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

Last change on this file since 2597 was 2333, checked in by vboxsync, 18 years ago

gcc-4.2 warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 59.0 KB
Line 
1/** @file
2 *
3 * VBox frontends: Basic Frontend (BFE):
4 * VBoxBFE main routines
5 *
6 * VBoxBFE is a limited frontend that sits directly on the Virtual Machine
7 * Manager (VMM) and does _not_ use COM to communicate.
8 * On Linux and Windows, VBoxBFE is based on SDL; on L4 it's based on the
9 * L4 console. Much of the code has been copied over from the other frontends
10 * in VBox/Main/ and src/Frontends/VBoxSDL/.
11 */
12
13/*
14 * Copyright (C) 2006 InnoTek Systemberatung GmbH
15 *
16 * This file is part of VirtualBox Open Source Edition (OSE), as
17 * available from http://www.virtualbox.org. This file is free software;
18 * you can redistribute it and/or modify it under the terms of the GNU
19 * General Public License as published by the Free Software Foundation,
20 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
21 * distribution. VirtualBox OSE is distributed in the hope that it will
22 * be useful, but WITHOUT ANY WARRANTY of any kind.
23 *
24 * If you received this file as part of a commercial VirtualBox
25 * distribution, then only the terms of your commercial VirtualBox
26 * license agreement apply instead of the previous paragraph.
27 */
28
29/*******************************************************************************
30* Header Files *
31*******************************************************************************/
32#define LOG_GROUP LOG_GROUP_GUI
33
34#ifndef VBOXBFE_WITHOUT_COM
35# include <VBox/com/Guid.h>
36# include <VBox/com/string.h>
37using namespace com;
38#endif
39
40#include <VBox/types.h>
41#include <VBox/err.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#include <VBox/log.h>
49#include <iprt/path.h>
50#include <iprt/string.h>
51#include <iprt/runtime.h>
52#include <iprt/assert.h>
53#include <iprt/semaphore.h>
54#include <iprt/stream.h>
55#include <iprt/thread.h>
56#include <iprt/uuid.h>
57#include <iprt/file.h>
58#include <iprt/alloca.h>
59#include <iprt/ctype.h>
60
61#include "VBoxBFE.h"
62
63#include <stdio.h>
64#include <stdlib.h> /* putenv */
65#include <errno.h>
66
67#if defined(__LINUX__) || defined(__L4__)
68#include <fcntl.h>
69#include <net/if.h>
70#include <sys/ioctl.h>
71#include <linux/if_tun.h>
72#endif
73
74#ifndef __L4ENV__
75#include <vector>
76#endif
77
78#include "ConsoleImpl.h"
79#include "DisplayImpl.h"
80#include "MouseImpl.h"
81#include "KeyboardImpl.h"
82#include "VMMDevInterface.h"
83#include "StatusImpl.h"
84#include "Framebuffer.h"
85#include "MachineDebuggerImpl.h"
86#ifdef VBOXBFE_WITH_USB
87# include "HostUSBImpl.h"
88#endif
89
90#if defined(USE_SDL) && ! defined(__L4__)
91#include "SDLConsole.h"
92#include "SDLFramebuffer.h"
93#endif
94
95#ifdef __L4__
96#include "L4Console.h"
97#include "L4Framebuffer.h"
98#endif
99
100#ifdef __L4ENV__
101# ifndef L4API_l4v2onv4
102# include <l4/sys/ktrace.h>
103# endif
104# include <l4/vboxserver/file.h>
105#endif
106
107/*******************************************************************************
108* Defined Constants And Macros *
109*******************************************************************************/
110
111#define VBOXSDL_ADVANCED_OPTIONS
112
113
114/*******************************************************************************
115* Internal Functions *
116*******************************************************************************/
117static DECLCALLBACK(int) vboxbfeConfigConstructor(PVM pVM, void *pvUser);
118static DECLCALLBACK(void) vmstateChangeCallback(PVM pVM, VMSTATE enmState, VMSTATE enmOldState, void *pvUser);
119static DECLCALLBACK(void) setVMErrorCallback(PVM pVM, void *pvUser, int rc, RT_SRC_POS_DECL,
120 const char *pszFormat, va_list args);
121static DECLCALLBACK(int) VMPowerUpThread(RTTHREAD Thread, void *pvUser);
122
123
124/*******************************************************************************
125* Global Variables *
126*******************************************************************************/
127
128PVM pVM = NULL;
129Mouse *gMouse = NULL;
130VMDisplay *gDisplay = NULL;
131Keyboard *gKeyboard = NULL;
132VMMDev *gVMMDev = NULL;
133Framebuffer *gFramebuffer = NULL;
134MachineDebugger *gMachineDebugger = NULL;
135VMStatus *gStatus = NULL;
136Console *gConsole = NULL;
137#ifdef VBOXBFE_WITH_USB
138HostUSB *gHostUSB = NULL;
139#endif
140
141VMSTATE machineState = VMSTATE_CREATING;
142
143PPDMLED mapFDLeds[2] = {0};
144PPDMLED mapIDELeds[4] = {0};
145
146/** flag whether keyboard/mouse events are grabbed */
147#ifdef __L4__
148/** see <l4/input/macros.h> for key definitions */
149int gHostKey; /* not used */
150int gHostKeySym = KEY_RIGHTCTRL;
151#elif defined (DEBUG_dmik)
152// my mini kbd doesn't have RCTRL...
153int gHostKey = KMOD_RSHIFT;
154int gHostKeySym = SDLK_RSHIFT;
155#else
156int gHostKey = KMOD_RCTRL;
157int gHostKeySym = SDLK_RCTRL;
158#endif
159bool gfAllowFullscreenToggle = true;
160
161static bool g_fIOAPIC = false;
162static bool fACPI = true;
163static bool fAudio = false;
164#ifdef VBOXBFE_WITH_USB
165static bool fUSB = false;
166#endif
167//static bool fPacketSniffer = false;
168static char *hdaFile = NULL;
169static char *cdromFile = NULL;
170static char *fdaFile = NULL;
171static const char *pszBootDevice = "IDE";
172static uint32_t memorySize = 128;
173static uint32_t vramSize = 4;
174#ifdef VBOXSDL_ADVANCED_OPTIONS
175static unsigned fRawR0 = ~0U;
176static unsigned fRawR3 = ~0U;
177static unsigned fPATM = ~0U;
178static unsigned fCSAM = ~0U;
179#endif
180static bool g_fReleaseLog = true; /**< Set if we should open the release. */
181
182
183/**
184 * Network device config info.
185 */
186typedef struct BFENetworkDevice
187{
188 enum
189 {
190 NOT_CONFIGURED = 0,
191 NONE,
192 NAT,
193 HIF,
194 INTNET
195 } enmType; /**< The type of network driver. */
196 bool fSniff; /**< Set if the network sniffer should be installed. */
197 const char *pszSniff; /**< Output file for the network sniffer. */
198 PDMMAC Mac; /**< The mac address for the device. */
199 const char *pszName; /**< The device name of a HIF device. The name of the internal network. */
200#if 1//defined(__LINUX__)
201 bool fHaveFd; /**< Set if fd is valid. */
202 int32_t fd; /**< The file descriptor of a HIF device.*/
203#endif
204} BFENETDEV, *PBFENETDEV;
205
206/** Array of network device configurations. */
207static BFENETDEV g_aNetDevs[NetworkAdapterCount];
208
209
210/** @todo currently this is only set but never read. */
211static char szError[512];
212
213
214/**
215 * Converts the passed in network option
216 *
217 * @returns Index into g_aNetDevs on success. (positive)
218 * @returns VERR_INVALID_PARAMETER on failure. (negative)
219 * @param pszArg The argument.
220 * @param cchRoot The length of the argument root.
221 */
222static int networkArg2Index(const char *pszArg, int cchRoot)
223{
224 uint32_t n;
225 int rc = RTStrToUInt32Ex(&pszArg[cchRoot], NULL, 10, &n);
226 if (VBOX_FAILURE(rc))
227 {
228 RTPrintf("Error: invalid network device option (rc=%Vrc): %s\n", rc, pszArg);
229 return -1;
230 }
231 if (n < 1 || n > NetworkAdapterCount)
232 {
233 RTPrintf("Error: The network device number is out of range: %RU32 (1 <= 0 <= %u) (%s)\n",
234 n, NetworkAdapterCount, pszArg);
235 return -1;
236 }
237 return n;
238}
239
240
241/**
242 * Print a syntax error.
243 *
244 * @returns return value for main().
245 * @param pszMsg The message format string.
246 * @param ... Format arguments.
247 */
248static int SyntaxError(const char *pszMsg, ...)
249{
250 va_list va;
251 RTPrintf("error: ");
252 va_start(va, pszMsg);
253 RTPrintfV(pszMsg, va);
254 va_end(va);
255 return 1;
256}
257
258
259/**
260 * Print a fatal error.
261 *
262 * @returns return value for main().
263 * @param pszMsg The message format string.
264 * @param ... Format arguments.
265 */
266static int FatalError(const char *pszMsg, ...)
267{
268 va_list va;
269 RTPrintf("fatal error: ");
270 va_start(va, pszMsg);
271 RTPrintfV(pszMsg, va);
272 va_end(va);
273 return 1;
274}
275
276
277/**
278 * Print program usage.
279 */
280static void show_usage()
281{
282 RTPrintf("Usage:\n"
283 " -hda <file> Set first hard disk to file\n"
284 " -fda <file> Set first floppy disk to file\n"
285 " -cdrom <file> Set CDROM to file/device ('none' to unmount)\n"
286 " -boot <a|c|d> Set boot device (a = floppy, c = first hard disk, d = DVD)\n"
287 " -m <size> Set memory size in megabytes (default 128MB)\n"
288 " -vram <size> Set size of video memory in megabytes\n"
289 " -fullscreen Start VM in fullscreen mode\n"
290 " -nofstoggle Forbid switching to/from fullscreen mode\n"
291 " -nohostkey Disable hostkey\n"
292 " -[no]acpi Enable or disable ACPI (default: enabled)\n"
293 " -[no]ioapic Enable or disable the IO-APIC (default: disabled)\n"
294 " -audio Enable audio\n"
295 " -natdev<1-N> Configure NAT for network device N\n"
296 " -hifdev<1-N> <dev> <mac> Use existing Host Interface Network Device with the given name and MAC address\n"
297#if 0
298 " -netsniff<1-N> Enable packet sniffer\n"
299#endif
300#ifdef __LINUX__
301 " -tapfd<1-N> <fd> Use existing TAP device, don't allocate\n"
302#endif
303#ifdef VBOX_VRDP
304 " -vrdp [port] Listen for VRDP connections on port (default if not specified)\n"
305#endif
306#ifdef VBOX_SECURELABEL
307 " -securelabel Display a secure VM label at the top of the screen\n"
308 " -seclabelfnt TrueType (.ttf) font file for secure session label\n"
309 " -seclabelsiz Font point size for secure session label (default 12)\n"
310#endif
311 " -[no]rellog Enable or disable the release log './VBoxBFE.log' (default: enabled)\n"
312#ifdef VBOXSDL_ADVANCED_OPTIONS
313 " -[no]rawr0 Enable or disable raw ring 3\n"
314 " -[no]rawr3 Enable or disable raw ring 0\n"
315 " -[no]patm Enable or disable PATM\n"
316 " -[no]csam Enable or disable CSAM\n"
317#endif
318#ifdef __L4ENV__
319 " -env <var=value> Set the given environment variable to \"value\"\n"
320#endif
321 "\n");
322}
323
324
325/** entry point */
326int main(int argc, char **argv)
327{
328#ifdef __L4ENV__
329#ifndef L4API_l4v2onv4
330 /* clear Fiasco kernel trace buffer */
331 fiasco_tbuf_clear();
332#endif
333 /* set the environment. Must be done before the runtime is
334 initialised. Yes, it really must. */
335 for (int i = 0; i < argc; i++)
336 if (strcmp(argv[i], "-env") == 0)
337 {
338 if (++i >= argc)
339 return SyntaxError("missing argument to -env (format: var=value)!\n");
340 /* add it to the environment */
341 if (putenv(argv[i]) != 0)
342 return SyntaxError("Error setting environment string %s.\n", argv[i]);
343 }
344#endif /* __L4ENV__ */
345
346 /*
347 * Before we do *anything*, we initialize the runtime.
348 */
349 int rc = RTR3Init();
350 if (VBOX_FAILURE(rc))
351 return FatalError("RTR3Init failed rc=%Vrc\n", rc);
352
353
354 bool fFullscreen = false;
355#ifdef VBOX_VRDP
356 int32_t portVRDP = -1;
357#endif
358#ifdef VBOX_SECURELABEL
359 bool fSecureLabel = false;
360 uint32_t secureLabelPointSize = 12;
361 char *secureLabelFontFile = NULL;
362#endif
363 RTPrintf("VirtualBox Simple SDL GUI built %s %s\n", __DATE__, __TIME__);
364
365 // less than one parameter is not possible
366 if (argc < 2)
367 {
368 show_usage();
369 return 1;
370 }
371
372 /*
373 * Parse the command line arguments.
374 */
375 for (int curArg = 1; curArg < argc; curArg++)
376 {
377 const char * const pszArg = argv[curArg];
378 if (strcmp(pszArg, "-boot") == 0)
379 {
380 if (++curArg >= argc)
381 return SyntaxError("missing argument for boot drive!\n");
382 if (strlen(argv[curArg]) != 1)
383 return SyntaxError("invalid argument for boot drive! (%s)\n", argv[curArg]);
384 rc = VINF_SUCCESS;
385 switch (argv[curArg][0])
386 {
387 case 'a':
388 {
389 pszBootDevice = "FLOPPY";
390 break;
391 }
392
393 case 'c':
394 {
395 pszBootDevice = "IDE";
396 break;
397 }
398
399 case 'd':
400 {
401 pszBootDevice = "DVD";
402 break;
403 }
404
405 default:
406 return SyntaxError("wrong argument for boot drive! (%s)\n", argv[curArg]);
407 }
408 }
409 else if (strcmp(pszArg, "-m") == 0)
410 {
411 if (++curArg >= argc)
412 return SyntaxError("missing argument for memory size!\n");
413 rc = RTStrToUInt32Ex(argv[curArg], NULL, 0, &memorySize);
414 if (VBOX_FAILURE(rc))
415 return SyntaxError("cannot grok the memory size: %s (%Vrc)\n",
416 argv[curArg], rc);
417 }
418 else if (strcmp(pszArg, "-vram") == 0)
419 {
420 if (++curArg >= argc)
421 return SyntaxError("missing argument for vram size!\n");
422 rc = RTStrToUInt32Ex(argv[curArg], NULL, 0, &vramSize);
423 if (VBOX_FAILURE(rc))
424 return SyntaxError("cannot grok the vram size: %s (%Vrc)\n",
425 argv[curArg], rc);
426 }
427 else if (strcmp(pszArg, "-fullscreen") == 0)
428 {
429 fFullscreen = true;
430 }
431 else if (strcmp(pszArg, "-nofstoggle") == 0)
432 {
433 gfAllowFullscreenToggle = false;
434 }
435 else if (strcmp(pszArg, "-nohostkey") == 0)
436 {
437 gHostKey = 0;
438 gHostKeySym = 0;
439 }
440 else if (strcmp(pszArg, "-acpi") == 0)
441 {
442 fACPI = true;
443 }
444 else if (strcmp(pszArg, "-noacpi") == 0)
445 {
446 fACPI = false;
447 }
448 else if (strcmp(pszArg, "-ioapic") == 0)
449 {
450 g_fIOAPIC = true;
451 }
452 else if (strcmp(pszArg, "-noioapic") == 0)
453 {
454 g_fIOAPIC = false;
455 }
456 else if (strcmp(pszArg, "-audio") == 0)
457 {
458 fAudio = true;
459 }
460#ifdef VBOXBFE_WITH_USB
461 else if (strcmp(pszArg, "-usb") == 0)
462 {
463 fUSB = true;
464 }
465#endif
466 else if (strcmp(pszArg, "-hda") == 0)
467 {
468 if (++curArg >= argc)
469 return SyntaxError("missing file name for first hard disk!\n");
470
471 /* resolve it. */
472 if (RTPathExists(argv[curArg]))
473 hdaFile = RTPathRealDup(argv[curArg]);
474 if (!hdaFile)
475 return SyntaxError("The path to the specified harddisk, '%s', could not be resolved.\n", argv[curArg]);
476 }
477 else if (strcmp(pszArg, "-fda") == 0)
478 {
479 if (++curArg >= argc)
480 return SyntaxError("missing file/device name for first floppy disk!\n");
481
482 /* resolve it. */
483 if (RTPathExists(argv[curArg]))
484 fdaFile = RTPathRealDup(argv[curArg]);
485 if (!fdaFile)
486 return SyntaxError("The path to the specified floppy disk, '%s', could not be resolved.\n", argv[curArg]);
487 }
488 else if (strcmp(pszArg, "-cdrom") == 0)
489 {
490 if (++curArg >= argc)
491 return SyntaxError("missing file/device name for first hard disk!\n");
492
493 /* resolve it. */
494 if (RTPathExists(argv[curArg]))
495 cdromFile = RTPathRealDup(argv[curArg]);
496 if (!cdromFile)
497 return SyntaxError("The path to the specified cdrom, '%s', could not be resolved.\n", argv[curArg]);
498 }
499 else if ( strncmp(pszArg, "-natdev", 7) == 0
500 || strncmp(pszArg, "-hifdev", 7) == 0
501 || strncmp(pszArg, "-nonetd", 7) == 0
502 || strncmp(pszArg, "-intnet", 7) == 0)
503 {
504 int i = networkArg2Index(pszArg, 7);
505 if (i < 0)
506 return 1;
507 g_aNetDevs[i].enmType = !strncmp(pszArg, "-natdev", 7)
508 ? BFENETDEV::NAT
509 : !strncmp(pszArg, "-hifdev", 7)
510 ? BFENETDEV::HIF
511 : !strncmp(pszArg, "-intnet", 7)
512 ? BFENETDEV::INTNET
513 : BFENETDEV::NONE;
514
515 /* The HIF device name / The Internal Network name. */
516 g_aNetDevs[i].pszName = NULL;
517 if ( g_aNetDevs[i].enmType == BFENETDEV::HIF
518 || g_aNetDevs[i].enmType == BFENETDEV::INTNET)
519 {
520 if (curArg + 1 >= argc)
521 return SyntaxError(g_aNetDevs[i].enmType == BFENETDEV::HIF
522 ? "The TAP network device name is missing! (%s)\n"
523 : "The internal network name is missing! (%s)\n"
524 , pszArg);
525 g_aNetDevs[i].pszName = argv[++curArg];
526 }
527
528 /* The MAC address. */
529 if (++curArg >= argc)
530 return SyntaxError("The network MAC address is missing! (%s)\n", pszArg);
531 if (strlen(argv[curArg]) != 12)
532 return SyntaxError("The network MAC address has an invalid length: %s (%s)\n", argv[curArg], pszArg);
533 const char *pszMac = argv[curArg];
534 for (unsigned j = 0; j < RT_ELEMENTS(g_aNetDevs[i].Mac.au8); j++)
535 {
536 char c1 = toupper(*pszMac++) - '0';
537 if (c1 > 9)
538 c1 -= 7;
539 char c2 = toupper(*pszMac++) - '0';
540 if (c2 > 9)
541 c2 -= 7;
542 if (c2 > 16 || c1 > 16)
543 return SyntaxError("Invalid MAC address: %s\n", argv[curArg]);
544 g_aNetDevs[i].Mac.au8[j] = ((c1 & 0x0f) << 4) | (c2 & 0x0f);
545 }
546 }
547 else if (strncmp(pszArg, "-netsniff", 9) == 0)
548 {
549 int i = networkArg2Index(pszArg, 7);
550 if (rc < 0)
551 return 1;
552 g_aNetDevs[i].fSniff = true;
553 /** @todo filename */
554 }
555#ifdef __LINUX__
556 else if (strncmp(pszArg, "-tapfd", 6) == 0)
557 {
558 int i = networkArg2Index(pszArg, 7);
559 if (++curArg >= argc)
560 return SyntaxError("missing argument for %s!\n", pszArg);
561 rc = RTStrToInt32Ex(argv[curArg], NULL, 0, &g_aNetDevs[i].fd);
562 if (VBOX_FAILURE(rc))
563 return SyntaxError("cannot grok tap fd: %s (%VRc)\n", argv[curArg], rc);
564 g_aNetDevs[i].fHaveFd = true;
565 }
566#endif /* __LINUX__ */
567#ifdef VBOX_VRDP
568 else if (strcmp(pszArg, "-vrdp") == 0)
569 {
570 // -vrdp might take a port number (positive).
571 portVRDP = 0; // indicate that it was encountered.
572 if (curArg + 1 < argc && argv[curArg + 1][0] != '-')
573 {
574 rc = RTStrToInt32Ex(argv[curArg], NULL, 0, &portVRDP);
575 if (VBOX_FAILURE(rc))
576 return SyntaxError("cannot vrpd port: %s (%VRc)\n", argv[curArg], rc);
577 if (portVRDP < 0 || portVRDP >= 0x10000)
578 return SyntaxError("vrdp port number is out of range: %RI32\n", portVRDP);
579 }
580 }
581#endif /* VBOX_VRDP */
582#ifdef VBOX_SECURELABEL
583 else if (strcmp(pszArg, "-securelabel") == 0)
584 {
585 fSecureLabel = true;
586 LogFlow(("Secure labelling turned on\n"));
587 }
588 else if (strcmp(pszArg, "-seclabelfnt") == 0)
589 {
590 if (++curArg >= argc)
591 return SyntaxError("missing font file name for secure label!\n");
592 secureLabelFontFile = argv[curArg];
593 }
594 else if (strcmp(pszArg, "-seclabelsiz") == 0)
595 {
596 if (++curArg >= argc)
597 return SyntaxError("missing font point size for secure label!\n");
598 secureLabelPointSize = atoi(argv[curArg]);
599 }
600#endif
601 else if (strcmp(pszArg, "-rellog") == 0)
602 {
603 g_fReleaseLog = true;
604 }
605 else if (strcmp(pszArg, "-norellog") == 0)
606 {
607 g_fReleaseLog = false;
608 }
609#ifdef VBOXSDL_ADVANCED_OPTIONS
610 else if (strcmp(pszArg, "-rawr0") == 0)
611 {
612 fRawR0 = true;
613 }
614 else if (strcmp(pszArg, "-norawr0") == 0)
615 {
616 fRawR0 = false;
617 }
618 else if (strcmp(pszArg, "-rawr3") == 0)
619 {
620 fRawR3 = true;
621 }
622 else if (strcmp(pszArg, "-norawr3") == 0)
623 {
624 fRawR3 = false;
625 }
626 else if (strcmp(pszArg, "-patm") == 0)
627 {
628 fPATM = true;
629 }
630 else if (strcmp(pszArg, "-nopatm") == 0)
631 {
632 fPATM = false;
633 }
634 else if (strcmp(pszArg, "-csam") == 0)
635 {
636 fCSAM = true;
637 }
638 else if (strcmp(pszArg, "-nocsam") == 0)
639 {
640 fCSAM = false;
641 }
642#endif /* VBOXSDL_ADVANCED_OPTIONS */
643#ifdef __L4__
644 else if (strcmp(pszArg, "-env") == 0)
645 ++curArg;
646#endif /* __L4__ */
647 /* just show the help screen */
648 else
649 {
650 SyntaxError("unrecognized argument '%s'\n", pszArg);
651 show_usage();
652 return 1;
653 }
654 }
655
656 gMachineDebugger = new MachineDebugger();
657 gStatus = new VMStatus();
658 gKeyboard = new Keyboard();
659 gMouse = new Mouse();
660 gVMMDev = new VMMDev();
661 gDisplay = new VMDisplay();
662#if defined(USE_SDL)
663 /* First console, then framebuffer!! */
664 gConsole = new SDLConsole();
665 gFramebuffer = new SDLFramebuffer();
666#elif defined(__L4ENV__)
667 gConsole = new L4Console();
668 gFramebuffer = new L4Framebuffer();
669#else
670#error "todo"
671#endif
672 if (!gConsole->initialized())
673 goto leave;
674 gDisplay->RegisterExternalFramebuffer(gFramebuffer);
675
676 /* start with something in the titlebar */
677 gConsole->updateTitlebar();
678
679 /*
680 * Start the VM execution thread. This has to be done
681 * asynchronously as powering up can take some time
682 * (accessing devices such as the host DVD drive). In
683 * the meantime, we have to service the SDL event loop.
684 */
685
686 RTTHREAD thread;
687 rc = RTThreadCreate(&thread, VMPowerUpThread, 0, 0, RTTHREADTYPE_MAIN_WORKER, 0, "PowerUp");
688 if (VBOX_FAILURE(rc))
689 {
690 RTPrintf("Error: Thread creation failed with %d\n", rc);
691 return -1;
692 }
693
694 /* loop until the powerup processing is done */
695 do
696 {
697#if defined(__LINUX__) && defined(USE_SDL)
698 if ( machineState == VMSTATE_CREATING
699 || machineState == VMSTATE_LOADING)
700 {
701 int event = gConsole->eventWait();
702
703 switch (event)
704 {
705 case CONEVENT_USR_SCREENRESIZE:
706 LogFlow(("CONEVENT_USR_SCREENRESIZE\n"));
707 gFramebuffer->resize();
708 /* notify the display that the resize has been completed */
709 gDisplay->ResizeCompleted();
710 break;
711
712 case CONEVENT_USR_QUIT:
713 RTPrintf("Error: failed to power up VM! No error text available.\n");
714 goto leave;
715 }
716 }
717 else
718#endif
719 RTThreadSleep(1000);
720 }
721 while ( machineState == VMSTATE_CREATING
722 || machineState == VMSTATE_LOADING);
723
724 if (machineState == VMSTATE_TERMINATED)
725 goto leave;
726
727 /* did the power up succeed? */
728 if (machineState != VMSTATE_RUNNING)
729 {
730 RTPrintf("Error: failed to power up VM! No error text available (rc = 0x%x state = %d)\n", rc, machineState);
731 goto leave;
732 }
733
734 gConsole->updateTitlebar();
735
736 /*
737 * Main event loop
738 */
739 LogFlow(("VBoxSDL: Entering big event loop\n"));
740
741 while (1)
742 {
743 int event = gConsole->eventWait();
744
745 switch (event)
746 {
747 case CONEVENT_NONE:
748 /* Handled internally */
749 break;
750
751 case CONEVENT_QUIT:
752 case CONEVENT_USR_QUIT:
753 goto leave;
754
755 case CONEVENT_SCREENUPDATE:
756 /// @todo that somehow doesn't seem to work!
757 gFramebuffer->repaint();
758 break;
759
760 case CONEVENT_USR_TITLEBARUPDATE:
761 gConsole->updateTitlebar();
762 break;
763
764 case CONEVENT_USR_SCREENRESIZE:
765 {
766 LogFlow(("CONEVENT_USR_SCREENRESIZE\n"));
767 gFramebuffer->resize();
768 /* notify the display that the resize has been completed */
769 gDisplay->ResizeCompleted();
770 break;
771 }
772
773#ifdef VBOX_SECURELABEL
774 case CONEVENT_USR_SECURELABELUPDATE:
775 {
776 /*
777 * Query the new label text
778 */
779 Bstr key = VBOXSDL_SECURELABEL_EXTRADATA;
780 Bstr label;
781 gMachine->COMGETTER(ExtraData)(key, label.asOutParam());
782 Utf8Str labelUtf8 = label;
783 /*
784 * Now update the label
785 */
786 gFramebuffer->setSecureLabelText(labelUtf8.raw());
787 break;
788 }
789#endif /* VBOX_SECURELABEL */
790
791 }
792
793 }
794
795leave:
796 LogFlow(("Returning from main()!\n"));
797
798 if (pVM)
799 {
800 /*
801 * If get here because the guest terminated using ACPI off we don't have to
802 * switch off the VM because we were notified via vmstateChangeCallback()
803 * that this already happened. In any other case stop the VM before killing her.
804 */
805 if (machineState != VMSTATE_OFF)
806 {
807 /* Power off VM */
808 PVMREQ pReq;
809 rc = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT, (PFNRT)VMR3PowerOff, 1, pVM);
810 }
811
812 /* And destroy it */
813 rc = VMR3Destroy(pVM);
814 AssertRC(rc);
815 }
816
817 delete gFramebuffer;
818 delete gConsole;
819 delete gDisplay;
820 delete gKeyboard;
821 delete gMouse;
822 delete gStatus;
823 delete gMachineDebugger;
824
825 RTLogFlush(NULL);
826 return VBOX_FAILURE (rc) ? 1 : 0;
827}
828
829
830
831/**
832 * VM state callback function. Called by the VMM
833 * using its state machine states.
834 *
835 * Primarily used to handle VM initiated power off, suspend and state saving,
836 * but also for doing termination completed work (VMSTATE_TERMINATE).
837 *
838 * In general this function is called in the context of the EMT.
839 *
840 * @todo machineState is set to VMSTATE_RUNNING before all devices have received power on events
841 * this can prematurely allow the main thread to enter the event loop
842 *
843 * @param pVM The VM handle.
844 * @param enmState The new state.
845 * @param enmOldState The old state.
846 * @param pvUser The user argument.
847 */
848static DECLCALLBACK(void) vmstateChangeCallback(PVM pVM, VMSTATE enmState, VMSTATE enmOldState, void *pvUser)
849{
850 LogFlow(("vmstateChangeCallback: changing state from %d to %d\n", enmOldState, enmState));
851 machineState = enmState;
852
853 switch (enmState)
854 {
855 /*
856 * The VM has terminated
857 */
858 case VMSTATE_OFF:
859 {
860 gConsole->eventQuit();
861 break;
862 }
863
864 /*
865 * The VM has been completely destroyed.
866 *
867 * Note: This state change can happen at two points:
868 * 1) At the end of VMR3Destroy() if it was not called from EMT.
869 * 2) At the end of vmR3EmulationThread if VMR3Destroy() was called by EMT.
870 */
871 case VMSTATE_TERMINATED:
872 {
873 break;
874 }
875
876 default: /* shut up gcc */
877 break;
878 }
879}
880
881
882/**
883 * VM error callback function. Called by the various VM components.
884 *
885 * @param pVM The VM handle.
886 * @param pvUser The user argument.
887 * @param rc VBox status code.
888 * @param pszError Error message format string.
889 * @param args Error message arguments.
890 * @thread EMT.
891 */
892DECLCALLBACK(void) setVMErrorCallback(PVM pVM, void *pvUser, int rc, RT_SRC_POS_DECL,
893 const char *pszFormat, va_list args)
894{
895 /** @todo accessing shared resource without any kind of synchronization */
896 if (VBOX_SUCCESS(rc))
897 szError[0] = '\0';
898 else
899 RTStrPrintfV(szError, sizeof(szError), pszFormat, args);
900}
901
902
903/** VM asynchronous operations thread */
904DECLCALLBACK(int) VMPowerUpThread(RTTHREAD Thread, void *pvUser)
905{
906 int rc = VINF_SUCCESS;
907 int rc2;
908
909 /*
910 * Setup the release log instance in current directory.
911 */
912 if (g_fReleaseLog)
913 {
914 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
915 PRTLOGGER pLogger;
916 rc2 = RTLogCreate(&pLogger, RTLOGFLAGS_PREFIX_TIME_PROG, "all",
917 "VBOX_RELEASE_LOG", ELEMENTS(s_apszGroups), s_apszGroups,
918 RTLOGDEST_FILE, "./VBoxBFE.log");
919 if (VBOX_SUCCESS(rc2))
920 {
921 /* some introductory information */
922 RTTIMESPEC TimeSpec;
923 char szNowUct[64];
924 RTTimeSpecToString(RTTimeNow(&TimeSpec), szNowUct, sizeof(szNowUct));
925 RTLogRelLogger(pLogger, 0, ~0U,
926 "VBoxBFE %s (%s %s) release log\n"
927 "Log opened %s\n",
928 VBOX_VERSION_STRING, __DATE__, __TIME__,
929 szNowUct);
930
931 /* register this logger as the release logger */
932 RTLogRelSetDefaultInstance(pLogger);
933 }
934 }
935
936 /*
937 * Start VM (also from saved state) and track progress
938 */
939 LogFlow(("VMPowerUp\n"));
940
941 /*
942 * Create empty VM.
943 */
944 rc = VMR3Create(setVMErrorCallback, NULL, vboxbfeConfigConstructor, NULL, &pVM);
945 if (VBOX_FAILURE(rc))
946 {
947 RTPrintf("Error: VM creation failed with %Vrc.\n", rc);
948 goto failure;
949 }
950
951
952 /*
953 * Register VM state change handler
954 */
955 rc = VMR3AtStateRegister(pVM, vmstateChangeCallback, NULL);
956 if (VBOX_FAILURE(rc))
957 {
958 RTPrintf("Error: VMR3AtStateRegister failed with %Vrc.\n", rc);
959 goto failure;
960 }
961
962#ifdef VBOXBFE_WITH_USB
963 /*
964 * Capture USB devices.
965 */
966 if (fUSB)
967 {
968 gHostUSB = new HostUSB();
969 gHostUSB->init(pVM);
970 }
971#endif /* VBOXBFE_WITH_USB */
972
973#ifdef __L4ENV__
974 /* L4 console cannot draw a host cursor */
975 gMouse->setHostCursor(false);
976#else
977 gMouse->setHostCursor(true);
978#endif
979
980 /*
981 * Power on the VM (i.e. start executing).
982 */
983 if (VBOX_SUCCESS(rc))
984 {
985 PVMREQ pReq;
986 rc = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT, (PFNRT)VMR3PowerOn, 1, pVM);
987 if (VBOX_SUCCESS(rc))
988 {
989 rc = pReq->iStatus;
990 AssertRC(rc);
991 VMR3ReqFree(pReq);
992 }
993 else
994 AssertMsgFailed(("VMR3PowerOn failed, rc=%Vrc\n", rc));
995 }
996
997 /*
998 * On failure destroy the VM.
999 */
1000 if (VBOX_FAILURE(rc))
1001 {
1002 goto failure;
1003 }
1004 return 0;
1005
1006
1007failure:
1008 if (pVM)
1009 {
1010 rc2 = VMR3Destroy(pVM);
1011 AssertRC(rc2);
1012 pVM = NULL;
1013 }
1014 machineState = VMSTATE_TERMINATED;
1015 return 0;
1016}
1017
1018/**
1019 * Register the main drivers.
1020 *
1021 * @returns VBox status code.
1022 * @param pCallbacks Pointer to the callback table.
1023 * @param u32Version VBox version number.
1024 */
1025DECLCALLBACK(int) VBoxDriversRegister(PCPDMDRVREGCB pCallbacks, uint32_t u32Version)
1026{
1027 int rc;
1028
1029 LogFlow(("VBoxDriversRegister: u32Version=%#x\n", u32Version));
1030 AssertReleaseMsg(u32Version == VBOX_VERSION, ("u32Version=%#x VBOX_VERSION=%#x\n", u32Version, VBOX_VERSION));
1031
1032 rc = pCallbacks->pfnRegister(pCallbacks, &Mouse::DrvReg);
1033 AssertRC(rc);
1034 if (VBOX_FAILURE(rc))
1035 return rc;
1036 rc = pCallbacks->pfnRegister(pCallbacks, &Keyboard::DrvReg);
1037 AssertRC(rc);
1038 if (VBOX_FAILURE(rc))
1039 return rc;
1040
1041 rc = pCallbacks->pfnRegister(pCallbacks, &VMDisplay::DrvReg);
1042 AssertRC(rc);
1043 if (VBOX_FAILURE(rc))
1044 return rc;
1045 rc = pCallbacks->pfnRegister(pCallbacks, &VMMDev::DrvReg);
1046 AssertRC(rc);
1047 if (VBOX_FAILURE(rc))
1048 return rc;
1049
1050 rc = pCallbacks->pfnRegister(pCallbacks, &VMStatus::DrvReg);
1051 if (VBOX_FAILURE(rc))
1052 return rc;
1053
1054 return VINF_SUCCESS;
1055}
1056
1057
1058/**
1059 * Constructs the VMM configuration tree.
1060 *
1061 * @returns VBox status code.
1062 * @param pVM VM handle.
1063 */
1064static DECLCALLBACK(int) vboxbfeConfigConstructor(PVM pVM, void *pvUser)
1065{
1066 int rcAll = VINF_SUCCESS;
1067 int rc;
1068
1069#define UPDATERC() do { if (VBOX_FAILURE(rc) && VBOX_SUCCESS(rcAll)) rcAll = rc; } while (0)
1070#undef CHECK_RC /** @todo r=bird: clashes with VBox/com/Assert.h. */
1071#define CHECK_RC() UPDATERC()
1072
1073 /*
1074 * Root values.
1075 */
1076 PCFGMNODE pRoot = CFGMR3GetRoot(pVM);
1077 rc = CFGMR3InsertString(pRoot, "Name", "Default VM");
1078 UPDATERC();
1079 rc = CFGMR3InsertInteger(pRoot, "RamSize", memorySize * _1M);
1080 UPDATERC();
1081 rc = CFGMR3InsertInteger(pRoot, "TimerMillies", 10);
1082 UPDATERC();
1083#ifdef VBOXSDL_ADVANCED_OPTIONS
1084 rc = CFGMR3InsertInteger(pRoot, "RawR3Enabled", (fRawR3 != ~0U) ? fRawR3 : 1);
1085 UPDATERC();
1086 rc = CFGMR3InsertInteger(pRoot, "RawR0Enabled", (fRawR0 != ~0U) ? fRawR0 : 1);
1087 UPDATERC();
1088 rc = CFGMR3InsertInteger(pRoot, "PATMEnabled", (fPATM != ~0U) ? fPATM : 1);
1089 UPDATERC();
1090 rc = CFGMR3InsertInteger(pRoot, "CSAMEnabled", (fCSAM != ~0U) ? fCSAM : 1);
1091#else
1092 rc = CFGMR3InsertInteger(pRoot, "RawR3Enabled", 1);
1093 UPDATERC();
1094 rc = CFGMR3InsertInteger(pRoot, "RawR0Enabled", 1);
1095 UPDATERC();
1096 rc = CFGMR3InsertInteger(pRoot, "PATMEnabled", 1);
1097 UPDATERC();
1098 rc = CFGMR3InsertInteger(pRoot, "CSAMEnabled", 1);
1099#endif
1100 UPDATERC();
1101
1102 /*
1103 * PDM.
1104 */
1105 rc = PDMR3RegisterDrivers(pVM, VBoxDriversRegister);
1106 UPDATERC();
1107
1108 /*
1109 * Devices
1110 */
1111 PCFGMNODE pDevices = NULL;
1112 rc = CFGMR3InsertNode(pRoot, "Devices", &pDevices);
1113 UPDATERC();
1114 /* device */
1115 PCFGMNODE pDev = NULL;
1116 PCFGMNODE pInst = NULL;
1117 PCFGMNODE pCfg = NULL;
1118 PCFGMNODE pLunL0 = NULL;
1119 PCFGMNODE pLunL1 = NULL;
1120
1121 /*
1122 * PC Arch.
1123 */
1124 rc = CFGMR3InsertNode(pDevices, "pcarch", &pDev);
1125 UPDATERC();
1126 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1127 UPDATERC();
1128 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
1129 UPDATERC();
1130 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1131 UPDATERC();
1132
1133 /*
1134 * PC Bios.
1135 */
1136 rc = CFGMR3InsertNode(pDevices, "pcbios", &pDev);
1137 UPDATERC();
1138 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1139 UPDATERC();
1140 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
1141 UPDATERC();
1142 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1143 UPDATERC();
1144 rc = CFGMR3InsertInteger(pCfg, "RamSize", memorySize * _1M);
1145 UPDATERC();
1146 rc = CFGMR3InsertString(pCfg, "BootDevice0", pszBootDevice);
1147 UPDATERC();
1148 rc = CFGMR3InsertString(pCfg, "BootDevice1", "NONE");
1149 UPDATERC();
1150 rc = CFGMR3InsertString(pCfg, "BootDevice2", "NONE");
1151 UPDATERC();
1152 rc = CFGMR3InsertString(pCfg, "BootDevice3", "NONE");
1153 UPDATERC();
1154 rc = CFGMR3InsertString(pCfg, "HardDiskDevice", "piix3ide");
1155 UPDATERC();
1156 rc = CFGMR3InsertString(pCfg, "FloppyDevice", "i82078");
1157 UPDATERC();
1158
1159 /* Default: no bios logo. */
1160 rc = CFGMR3InsertInteger(pCfg, "FadeIn", 1);
1161 UPDATERC();
1162 rc = CFGMR3InsertInteger(pCfg, "FadeOut", 0);
1163 UPDATERC();
1164 rc = CFGMR3InsertInteger(pCfg, "LogoTime", 0);
1165 UPDATERC();
1166 rc = CFGMR3InsertString(pCfg, "LogoFile", "");
1167 UPDATERC();
1168
1169 /*
1170 * ACPI
1171 */
1172 if (fACPI)
1173 {
1174 rc = CFGMR3InsertNode(pDevices, "acpi", &pDev); CHECK_RC();
1175 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1176 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1177 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1178 rc = CFGMR3InsertInteger(pCfg, "RamSize", memorySize * _1M); CHECK_RC();
1179 rc = CFGMR3InsertInteger(pCfg, "IOAPIC", g_fIOAPIC); CHECK_RC();
1180 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 7); CHECK_RC();
1181 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); CHECK_RC();
1182
1183 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1184 rc = CFGMR3InsertString(pLunL0, "Driver", "ACPIHost"); CHECK_RC();
1185 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1186 }
1187
1188 /*
1189 * PCI bus.
1190 */
1191 rc = CFGMR3InsertNode(pDevices, "pci", &pDev); /* piix3 */
1192 UPDATERC();
1193 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1194 UPDATERC();
1195 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
1196 UPDATERC();
1197 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1198 UPDATERC();
1199 rc = CFGMR3InsertInteger(pCfg, "IOAPIC", g_fIOAPIC); CHECK_RC();
1200
1201 /*
1202 * DMA
1203 */
1204 rc = CFGMR3InsertNode(pDevices, "8237A", &pDev); CHECK_RC();
1205 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1206 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1207
1208 /*
1209 * PCI bus.
1210 */
1211 rc = CFGMR3InsertNode(pDevices, "pci", &pDev); /* piix3 */ CHECK_RC();
1212 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1213 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1214 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1215
1216 /*
1217 * PS/2 keyboard & mouse.
1218 */
1219 rc = CFGMR3InsertNode(pDevices, "pckbd", &pDev); CHECK_RC();
1220 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1221 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1222 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1223
1224 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1225 rc = CFGMR3InsertString(pLunL0, "Driver", "KeyboardQueue"); CHECK_RC();
1226 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1227 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 64); CHECK_RC();
1228
1229 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); CHECK_RC();
1230 rc = CFGMR3InsertString(pLunL1, "Driver", "MainKeyboard"); CHECK_RC();
1231 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); CHECK_RC();
1232 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)gKeyboard); CHECK_RC();
1233
1234 rc = CFGMR3InsertNode(pInst, "LUN#1", &pLunL0); CHECK_RC();
1235 rc = CFGMR3InsertString(pLunL0, "Driver", "MouseQueue"); CHECK_RC();
1236 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1237 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 128); CHECK_RC();
1238
1239 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); CHECK_RC();
1240 rc = CFGMR3InsertString(pLunL1, "Driver", "MainMouse"); CHECK_RC();
1241 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); CHECK_RC();
1242 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)gMouse); CHECK_RC();
1243
1244
1245 /*
1246 * i82078 Floppy drive controller
1247 */
1248 rc = CFGMR3InsertNode(pDevices, "i82078", &pDev); CHECK_RC();
1249 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1250 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); CHECK_RC();
1251 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1252 rc = CFGMR3InsertInteger(pCfg, "IRQ", 6); CHECK_RC();
1253 rc = CFGMR3InsertInteger(pCfg, "DMA", 2); CHECK_RC();
1254 rc = CFGMR3InsertInteger(pCfg, "MemMapped", 0 ); CHECK_RC();
1255 rc = CFGMR3InsertInteger(pCfg, "IOBase", 0x3f0); CHECK_RC();
1256
1257 /* Attach the status driver */
1258 rc = CFGMR3InsertNode(pInst, "LUN#999", &pLunL0); CHECK_RC();
1259 rc = CFGMR3InsertString(pLunL0, "Driver", "MainStatus"); CHECK_RC();
1260 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1261 rc = CFGMR3InsertInteger(pCfg, "papLeds", (uintptr_t)&mapFDLeds[0]); CHECK_RC();
1262 rc = CFGMR3InsertInteger(pCfg, "First", 0); CHECK_RC();
1263 rc = CFGMR3InsertInteger(pCfg, "Last", 0); CHECK_RC();
1264
1265 if (fdaFile)
1266 {
1267 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1268 rc = CFGMR3InsertString(pLunL0, "Driver", "Block"); CHECK_RC();
1269 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1270 rc = CFGMR3InsertString(pCfg, "Type", "Floppy 1.44"); CHECK_RC();
1271 rc = CFGMR3InsertInteger(pCfg, "Mountable", 1); CHECK_RC();
1272
1273 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); CHECK_RC();
1274 rc = CFGMR3InsertString(pLunL1, "Driver", "RawImage"); CHECK_RC();
1275 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); CHECK_RC();
1276 rc = CFGMR3InsertString(pCfg, "Path", fdaFile); CHECK_RC();
1277 }
1278
1279 /*
1280 * i8254 Programmable Interval Timer And Dummy Speaker
1281 */
1282 rc = CFGMR3InsertNode(pDevices, "i8254", &pDev); CHECK_RC();
1283 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1284 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1285#ifdef DEBUG
1286 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1287#endif
1288
1289 /*
1290 * i8259 Programmable Interrupt Controller.
1291 */
1292 rc = CFGMR3InsertNode(pDevices, "i8259", &pDev); CHECK_RC();
1293 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1294 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1295 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1296
1297 /*
1298 * Advanced Programmable Interrupt Controller.
1299 */
1300 rc = CFGMR3InsertNode(pDevices, "apic", &pDev); CHECK_RC();
1301 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1302 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1303 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1304
1305 /*
1306 * I/O Advanced Programmable Interrupt Controller.
1307 */
1308 if (g_fIOAPIC)
1309 {
1310 rc = CFGMR3InsertNode(pDevices, "ioapic", &pDev); CHECK_RC();
1311 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1312 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1313 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1314 }
1315
1316 /*
1317 * RTC MC146818.
1318 */
1319 rc = CFGMR3InsertNode(pDevices, "mc146818", &pDev); CHECK_RC();
1320 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1321 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1322
1323 /*
1324 * Serial ports
1325 */
1326 rc = CFGMR3InsertNode(pDevices, "serial", &pDev); CHECK_RC();
1327 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1328 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1329 rc = CFGMR3InsertInteger(pCfg, "IRQ", 4); CHECK_RC();
1330 rc = CFGMR3InsertInteger(pCfg, "IOBase", 0x3f8); CHECK_RC();
1331
1332 rc = CFGMR3InsertNode(pDev, "1", &pInst); CHECK_RC();
1333 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1334 rc = CFGMR3InsertInteger(pCfg, "IRQ", 3); CHECK_RC();
1335 rc = CFGMR3InsertInteger(pCfg, "IOBase", 0x2f8); CHECK_RC();
1336
1337 /*
1338 * VGA.
1339 */
1340 rc = CFGMR3InsertNode(pDevices, "vga", &pDev); CHECK_RC();
1341 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1342 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1343 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 2); CHECK_RC();
1344 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); CHECK_RC();
1345 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1346 rc = CFGMR3InsertInteger(pCfg, "VRamSize", vramSize * _1M); CHECK_RC();
1347
1348#ifdef __L4ENV__
1349 /* XXX hard-coded */
1350 rc = CFGMR3InsertInteger(pCfg, "HeightReduction", 18); CHECK_RC();
1351 rc = CFGMR3InsertInteger(pCfg, "CustomVideoModes", 1); CHECK_RC();
1352 char szBuf[64];
1353 /* Tell the guest which is the ideal video mode to use */
1354 RTStrPrintf(szBuf, sizeof(szBuf), "%dx%dx%d",
1355 gFramebuffer->getHostXres(),
1356 gFramebuffer->getHostYres(),
1357 gFramebuffer->getHostBitsPerPixel());
1358 rc = CFGMR3InsertString(pCfg, "CustomVideoMode1", szBuf); CHECK_RC();
1359#endif
1360
1361 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1362 rc = CFGMR3InsertString(pLunL0, "Driver", "MainDisplay"); CHECK_RC();
1363 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1364 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)gDisplay); CHECK_RC();
1365
1366 /*
1367 * IDE (update this when the main interface changes)
1368 */
1369 rc = CFGMR3InsertNode(pDevices, "piix3ide", &pDev); /* piix3 */ CHECK_RC();
1370 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1371 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1372 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 1); CHECK_RC();
1373 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 1); CHECK_RC();
1374 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1375
1376 if (hdaFile)
1377 {
1378 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1379 rc = CFGMR3InsertString(pLunL0, "Driver", "Block"); CHECK_RC();
1380 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1381 rc = CFGMR3InsertString(pCfg, "Type", "HardDisk"); CHECK_RC();
1382 rc = CFGMR3InsertInteger(pCfg, "Mountable", 0); CHECK_RC();
1383
1384 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); CHECK_RC();
1385 rc = CFGMR3InsertString(pLunL1, "Driver", "VBoxHDD"); CHECK_RC();
1386 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); CHECK_RC();
1387 rc = CFGMR3InsertString(pCfg, "Path", hdaFile); CHECK_RC();
1388 }
1389
1390 if (cdromFile)
1391 {
1392 // ASSUME: DVD drive is always attached to LUN#2 (i.e. secondary IDE master)
1393 rc = CFGMR3InsertNode(pInst, "LUN#2", &pLunL0); CHECK_RC();
1394 rc = CFGMR3InsertString(pLunL0, "Driver", "Block"); CHECK_RC();
1395 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1396 rc = CFGMR3InsertString(pCfg, "Type", "DVD"); CHECK_RC();
1397 rc = CFGMR3InsertInteger(pCfg, "Mountable", 1); CHECK_RC();
1398
1399 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); CHECK_RC();
1400 rc = CFGMR3InsertString(pLunL1, "Driver", "MediaISO"); CHECK_RC();
1401 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); CHECK_RC();
1402 rc = CFGMR3InsertString(pCfg, "Path", cdromFile); CHECK_RC();
1403 }
1404
1405 /*
1406 * Network adapters
1407 */
1408 rc = CFGMR3InsertNode(pDevices, "pcnet", &pDev); CHECK_RC();
1409 for (ULONG ulInstance = 0; ulInstance < NetworkAdapterCount; ulInstance++)
1410 {
1411 if (g_aNetDevs[ulInstance].enmType != BFENETDEV::NOT_CONFIGURED)
1412 {
1413 char szInstance[4];
1414 RTStrPrintf(szInstance, sizeof(szInstance), "%lu", ulInstance);
1415 rc = CFGMR3InsertNode(pDev, szInstance, &pInst); CHECK_RC();
1416 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); CHECK_RC();
1417 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo",
1418 !ulInstance ? 3 : ulInstance - 1 + 8); CHECK_RC();
1419 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); CHECK_RC();
1420 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1421 rc = CFGMR3InsertBytes(pCfg, "MAC", &g_aNetDevs[ulInstance].Mac, sizeof(PDMMAC));
1422 CHECK_RC();
1423
1424 /*
1425 * Enable the packet sniffer if requested.
1426 */
1427 if (g_aNetDevs[ulInstance].fSniff)
1428 {
1429 /* insert the sniffer filter driver. */
1430 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1431 rc = CFGMR3InsertString(pLunL0, "Driver", "NetSniffer"); CHECK_RC();
1432 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1433 if (g_aNetDevs[ulInstance].pszSniff)
1434 {
1435 rc = CFGMR3InsertString(pCfg, "File", g_aNetDevs[ulInstance].pszSniff); CHECK_RC();
1436 }
1437 }
1438
1439 /*
1440 * Create the driver config (if any).
1441 */
1442 if (g_aNetDevs[ulInstance].enmType != BFENETDEV::NONE)
1443 {
1444 if (g_aNetDevs[ulInstance].fSniff)
1445 {
1446 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); CHECK_RC();
1447 }
1448 else
1449 {
1450 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1451 }
1452 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1453 }
1454
1455 /*
1456 * Configure the driver.
1457 */
1458 if (g_aNetDevs[ulInstance].enmType == BFENETDEV::NAT)
1459 {
1460 rc = CFGMR3InsertString(pLunL0, "Driver", "NAT"); CHECK_RC();
1461 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1462 /* (Port forwarding goes here.) */
1463 }
1464 else if (g_aNetDevs[ulInstance].enmType == BFENETDEV::HIF)
1465 {
1466 rc = CFGMR3InsertString(pLunL0, "Driver", "HostInterface"); CHECK_RC();
1467 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1468
1469#if defined(__LINUX__)
1470 if (g_aNetDevs[ulInstance].fHaveFd)
1471 {
1472 rc = CFGMR3InsertString(pCfg, "Device", g_aNetDevs[ulInstance].pszName); CHECK_RC();
1473 rc = CFGMR3InsertInteger(pCfg, "FileHandle", g_aNetDevs[ulInstance].fd); CHECK_RC();
1474 }
1475 else
1476#endif
1477 {
1478#if defined (__LINUX__) || defined (__L4__)
1479 /*
1480 * Create/Open the TAP the device.
1481 */
1482 RTFILE tapFD;
1483 rc = RTFileOpen(&tapFD, "/dev/net/tun",
1484 RTFILE_O_READWRITE | RTFILE_O_OPEN |
1485 RTFILE_O_DENY_NONE | RTFILE_O_INHERIT);
1486 if (VBOX_FAILURE(rc))
1487 {
1488 FatalError("Failed to open /dev/net/tun: %Vrc\n", rc);
1489 return rc;
1490 }
1491
1492 struct ifreq IfReq;
1493 memset(&IfReq, 0, sizeof(IfReq));
1494 if (g_aNetDevs[ulInstance].pszName && g_aNetDevs[ulInstance].pszName[0])
1495 {
1496 size_t cch = strlen(g_aNetDevs[ulInstance].pszName);
1497 if (cch >= sizeof(IfReq.ifr_name))
1498 {
1499 FatalError("HIF name too long for device #%d: %s\n",
1500 ulInstance + 1, g_aNetDevs[ulInstance].pszName);
1501 return VERR_BUFFER_OVERFLOW;
1502 }
1503 memcpy(IfReq.ifr_name, g_aNetDevs[ulInstance].pszName, cch + 1);
1504 }
1505 else
1506 strcpy(IfReq.ifr_name, "tun%d");
1507 IfReq.ifr_flags = IFF_TAP | IFF_NO_PI;
1508 rc = ioctl(tapFD, TUNSETIFF, &IfReq);
1509 if (rc)
1510 {
1511 int rc2 = RTErrConvertFromErrno(errno);
1512 FatalError("ioctl TUNSETIFF '%s' failed: errno=%d rc=%d (%Vrc)\n",
1513 IfReq.ifr_name, errno, rc, rc2);
1514 return rc2;
1515 }
1516
1517 rc = fcntl(tapFD, F_SETFL, O_NONBLOCK);
1518 if (rc)
1519 {
1520 int rc2 = RTErrConvertFromErrno(errno);
1521 FatalError("fcntl F_SETFL/O_NONBLOCK '%s' failed: errno=%d rc=%d (%Vrc)\n",
1522 IfReq.ifr_name, errno, rc, rc2);
1523 return rc2;
1524 }
1525
1526 rc = CFGMR3InsertString(pCfg, "Device", g_aNetDevs[ulInstance].pszName); CHECK_RC();
1527 rc = CFGMR3InsertInteger(pCfg, "FileHandle", (RTFILE)tapFD); CHECK_RC();
1528
1529#elif defined(__WIN__)
1530 /*
1531 * We need the GUID too here...
1532 */
1533 rc = CFGMR3InsertString(pCfg, "Device", g_aNetDevs[ulInstance].pszName); CHECK_RC();
1534 rc = CFGMR3InsertString(pCfg, "HostInterfaceName", g_aNetDevs[ulInstance].pszName); CHECK_RC();
1535 rc = CFGMR3InsertString(pCfg, "GUID", g_aNetDevs[ulInstance].pszName /*pszGUID*/); CHECK_RC();
1536
1537
1538#else /* !__LINUX__ && !__L4__ */
1539 FatalError("Name based HIF devices not implemented yet for this host platform\n");
1540 return VERR_NOT_IMPLEMENTED;
1541#endif
1542 }
1543 }
1544 else if (g_aNetDevs[ulInstance].enmType == BFENETDEV::INTNET)
1545 {
1546 /*
1547 * Internal networking.
1548 */
1549 rc = CFGMR3InsertString(pCfg, "Network", g_aNetDevs[ulInstance].pszName); CHECK_RC();
1550 }
1551 }
1552 }
1553
1554 /*
1555 * VMM Device
1556 */
1557 rc = CFGMR3InsertNode(pDevices, "VMMDev", &pDev); CHECK_RC();
1558 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1559 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1560 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1561 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 4); CHECK_RC();
1562 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); CHECK_RC();
1563
1564 /* the VMM device's Main driver */
1565 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1566 rc = CFGMR3InsertString(pLunL0, "Driver", "MainVMMDev"); CHECK_RC();
1567 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1568 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)gVMMDev); CHECK_RC();
1569
1570 /*
1571 * AC'97 ICH audio
1572 */
1573 if (fAudio)
1574 {
1575 rc = CFGMR3InsertNode(pDevices, "ichac97", &pDev);
1576 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1577 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1578 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 5); CHECK_RC();
1579 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); CHECK_RC();
1580 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1581
1582 /* the Audio driver */
1583 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1584 rc = CFGMR3InsertString(pLunL0, "Driver", "AUDIO"); CHECK_RC();
1585 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1586#ifdef __WIN__
1587 rc = CFGMR3InsertString(pCfg, "AudioDriver", "winmm"); CHECK_RC();
1588#elif defined(__DARWIN__)
1589 rc = CFGMR3InsertString(pCfg, "AudioDriver", "coreaudio"); CHECK_RC();
1590#elif defined(__LINUX__)
1591 rc = CFGMR3InsertString(pCfg, "AudioDriver", "oss"); CHECK_RC();
1592#else /* portme */
1593 rc = CFGMR3InsertString(pCfg, "AudioDriver", "none"); CHECK_RC();
1594#endif /* !__WIN__ */
1595 }
1596
1597#ifdef VBOXBFE_WITH_USB
1598 /*
1599 * The USB Controller.
1600 */
1601 if (fUSB)
1602 {
1603 rc = CFGMR3InsertNode(pDevices, "usb-ohci", &pDev); CHECK_RC();
1604 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1605 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1606 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1607 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 6); CHECK_RC();
1608 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); CHECK_RC();
1609
1610 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1611 rc = CFGMR3InsertString(pLunL0, "Driver", "VUSBRootHub"); CHECK_RC();
1612 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1613 }
1614#endif /* VBOXBFE_WITH_USB */
1615
1616#undef UPDATERC
1617#undef CHECK_RC
1618
1619 return rc;
1620}
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