VirtualBox

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

Last change on this file since 1572 was 1571, checked in by vboxsync, 18 years ago

audio (darwin).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 58.9 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 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 hdaFile = RTPathRealDup(argv[curArg]);
473 if (!hdaFile)
474 return SyntaxError("The path to the specified harddisk, '%s', could not be resolved.\n", argv[curArg]);
475 }
476 else if (strcmp(pszArg, "-fda") == 0)
477 {
478 if (++curArg >= argc)
479 return SyntaxError("missing file/device name for first floppy disk!\n");
480
481 /* resolve it. */
482 fdaFile = RTPathRealDup(argv[curArg]);
483 if (!fdaFile)
484 return SyntaxError("The path to the specified floppy disk, '%s', could not be resolved.\n", argv[curArg]);
485 }
486 else if (strcmp(pszArg, "-cdrom") == 0)
487 {
488 if (++curArg >= argc)
489 return SyntaxError("missing file/device name for first hard disk!\n");
490
491 /* resolve it. */
492 cdromFile = RTPathRealDup(argv[curArg]);
493 if (!cdromFile)
494 return SyntaxError("The path to the specified cdrom, '%s', could not be resolved.\n", argv[curArg]);
495 }
496 else if ( strncmp(pszArg, "-natdev", 7) == 0
497 || strncmp(pszArg, "-hifdev", 7) == 0
498 || strncmp(pszArg, "-nonetd", 7) == 0
499 || strncmp(pszArg, "-intnet", 7) == 0)
500 {
501 int i = networkArg2Index(pszArg, 7);
502 if (i < 0)
503 return 1;
504 g_aNetDevs[i].enmType = !strncmp(pszArg, "-natdev", 7)
505 ? BFENETDEV::NAT
506 : !strncmp(pszArg, "-hifdev", 7)
507 ? BFENETDEV::HIF
508 : !strncmp(pszArg, "-intnet", 7)
509 ? BFENETDEV::INTNET
510 : BFENETDEV::NONE;
511
512 /* The HIF device name / The Internal Network name. */
513 g_aNetDevs[i].pszName = NULL;
514 if ( g_aNetDevs[i].enmType == BFENETDEV::HIF
515 || g_aNetDevs[i].enmType == BFENETDEV::INTNET)
516 {
517 if (curArg + 1 >= argc)
518 return SyntaxError(g_aNetDevs[i].enmType == BFENETDEV::HIF
519 ? "The TAP network device name is missing! (%s)\n"
520 : "The internal network name is missing! (%s)\n"
521 , pszArg);
522 g_aNetDevs[i].pszName = argv[++curArg];
523 }
524
525 /* The MAC address. */
526 if (++curArg >= argc)
527 return SyntaxError("The network MAC address is missing! (%s)\n", pszArg);
528 if (strlen(argv[curArg]) != 12)
529 return SyntaxError("The network MAC address has an invalid length: %s (%s)\n", argv[curArg], pszArg);
530 const char *pszMac = argv[curArg];
531 for (unsigned j = 0; j < RT_ELEMENTS(g_aNetDevs[i].Mac.au8); j++)
532 {
533 char c1 = toupper(*pszMac++) - '0';
534 if (c1 > 9)
535 c1 -= 7;
536 char c2 = toupper(*pszMac++) - '0';
537 if (c2 > 9)
538 c2 -= 7;
539 if (c2 > 16 || c1 > 16)
540 return SyntaxError("Invalid MAC address: %s\n", argv[curArg]);
541 g_aNetDevs[i].Mac.au8[j] = ((c1 & 0x0f) << 4) | (c2 & 0x0f);
542 }
543 }
544 else if (strncmp(pszArg, "-netsniff", 9) == 0)
545 {
546 int i = networkArg2Index(pszArg, 7);
547 if (rc < 0)
548 return 1;
549 g_aNetDevs[i].fSniff = true;
550 /** @todo filename */
551 }
552#ifdef __LINUX__
553 else if (strncmp(pszArg, "-tapfd", 6) == 0)
554 {
555 int i = networkArg2Index(pszArg, 7);
556 if (++curArg >= argc)
557 return SyntaxError("missing argument for %s!\n", pszArg);
558 rc = RTStrToInt32Ex(argv[curArg], NULL, 0, &g_aNetDevs[i].fd);
559 if (VBOX_FAILURE(rc))
560 return SyntaxError("cannot grok tap fd: %s (%VRc)\n", argv[curArg], rc);
561 g_aNetDevs[i].fHaveFd = true;
562 }
563#endif /* __LINUX__ */
564#ifdef VBOX_VRDP
565 else if (strcmp(pszArg, "-vrdp") == 0)
566 {
567 // -vrdp might take a port number (positive).
568 portVRDP = 0; // indicate that it was encountered.
569 if (curArg + 1 < argc && argv[curArg + 1][0] != '-')
570 {
571 rc = RTStrToInt32Ex(argv[curArg], NULL, 0, &portVRDP);
572 if (VBOX_FAILURE(rc))
573 return SyntaxError("cannot vrpd port: %s (%VRc)\n", argv[curArg], rc);
574 if (portVRDP < 0 || portVRDP >= 0x10000)
575 return SyntaxError("vrdp port number is out of range: %RI32\n", portVRDP);
576 }
577 }
578#endif /* VBOX_VRDP */
579#ifdef VBOX_SECURELABEL
580 else if (strcmp(pszArg, "-securelabel") == 0)
581 {
582 fSecureLabel = true;
583 LogFlow(("Secure labelling turned on\n"));
584 }
585 else if (strcmp(pszArg, "-seclabelfnt") == 0)
586 {
587 if (++curArg >= argc)
588 return SyntaxError("missing font file name for secure label!\n");
589 secureLabelFontFile = argv[curArg];
590 }
591 else if (strcmp(pszArg, "-seclabelsiz") == 0)
592 {
593 if (++curArg >= argc)
594 return SyntaxError("missing font point size for secure label!\n");
595 secureLabelPointSize = atoi(argv[curArg]);
596 }
597#endif
598 else if (strcmp(pszArg, "-rellog") == 0)
599 {
600 g_fReleaseLog = true;
601 }
602 else if (strcmp(pszArg, "-norellog") == 0)
603 {
604 g_fReleaseLog = false;
605 }
606#ifdef VBOXSDL_ADVANCED_OPTIONS
607 else if (strcmp(pszArg, "-rawr0") == 0)
608 {
609 fRawR0 = true;
610 }
611 else if (strcmp(pszArg, "-norawr0") == 0)
612 {
613 fRawR0 = false;
614 }
615 else if (strcmp(pszArg, "-rawr3") == 0)
616 {
617 fRawR3 = true;
618 }
619 else if (strcmp(pszArg, "-norawr3") == 0)
620 {
621 fRawR3 = false;
622 }
623 else if (strcmp(pszArg, "-patm") == 0)
624 {
625 fPATM = true;
626 }
627 else if (strcmp(pszArg, "-nopatm") == 0)
628 {
629 fPATM = false;
630 }
631 else if (strcmp(pszArg, "-csam") == 0)
632 {
633 fCSAM = true;
634 }
635 else if (strcmp(pszArg, "-nocsam") == 0)
636 {
637 fCSAM = false;
638 }
639#endif /* VBOXSDL_ADVANCED_OPTIONS */
640#ifdef __L4__
641 else if (strcmp(pszArg, "-env") == 0)
642 ++curArg;
643#endif /* __L4__ */
644 /* just show the help screen */
645 else
646 {
647 SyntaxError("unrecognized argument '%s'\n", pszArg);
648 show_usage();
649 return 1;
650 }
651 }
652
653 gMachineDebugger = new MachineDebugger();
654 gStatus = new VMStatus();
655 gKeyboard = new Keyboard();
656 gMouse = new Mouse();
657 gVMMDev = new VMMDev();
658 gDisplay = new VMDisplay();
659#if defined(USE_SDL)
660 /* First console, then framebuffer!! */
661 gConsole = new SDLConsole();
662 gFramebuffer = new SDLFramebuffer();
663#elif defined(__L4ENV__)
664 gConsole = new L4Console();
665 gFramebuffer = new L4Framebuffer();
666#else
667#error "todo"
668#endif
669 if (!gConsole->initialized())
670 goto leave;
671 gDisplay->RegisterExternalFramebuffer(gFramebuffer);
672
673 /* start with something in the titlebar */
674 gConsole->updateTitlebar();
675
676 /*
677 * Start the VM execution thread. This has to be done
678 * asynchronously as powering up can take some time
679 * (accessing devices such as the host DVD drive). In
680 * the meantime, we have to service the SDL event loop.
681 */
682
683 RTTHREAD thread;
684 rc = RTThreadCreate(&thread, VMPowerUpThread, 0, 0, RTTHREADTYPE_MAIN_WORKER, 0, "PowerUp");
685 if (VBOX_FAILURE(rc))
686 {
687 RTPrintf("Error: Thread creation failed with %d\n", rc);
688 return -1;
689 }
690
691 /* loop until the powerup processing is done */
692 do
693 {
694#if defined(__LINUX__) && defined(USE_SDL)
695 if ( machineState == VMSTATE_CREATING
696 || machineState == VMSTATE_LOADING)
697 {
698 int event = gConsole->eventWait();
699
700 switch (event)
701 {
702 case CONEVENT_USR_SCREENRESIZE:
703 LogFlow(("CONEVENT_USR_SCREENRESIZE\n"));
704 gFramebuffer->resize();
705 /* notify the display that the resize has been completed */
706 gDisplay->ResizeCompleted();
707 break;
708
709 case CONEVENT_USR_QUIT:
710 RTPrintf("Error: failed to power up VM! No error text available.\n");
711 goto leave;
712 }
713 }
714 else
715#endif
716 RTThreadSleep(1000);
717 }
718 while ( machineState == VMSTATE_CREATING
719 || machineState == VMSTATE_LOADING);
720
721 if (machineState == VMSTATE_TERMINATED)
722 goto leave;
723
724 /* did the power up succeed? */
725 if (machineState != VMSTATE_RUNNING)
726 {
727 RTPrintf("Error: failed to power up VM! No error text available (rc = 0x%x state = %d)\n", rc, machineState);
728 goto leave;
729 }
730
731 gConsole->updateTitlebar();
732
733 /*
734 * Main event loop
735 */
736 LogFlow(("VBoxSDL: Entering big event loop\n"));
737
738 while (1)
739 {
740 int event = gConsole->eventWait();
741
742 switch (event)
743 {
744 case CONEVENT_NONE:
745 /* Handled internally */
746 break;
747
748 case CONEVENT_QUIT:
749 case CONEVENT_USR_QUIT:
750 goto leave;
751
752 case CONEVENT_SCREENUPDATE:
753 /// @todo that somehow doesn't seem to work!
754 gFramebuffer->repaint();
755 break;
756
757 case CONEVENT_USR_TITLEBARUPDATE:
758 gConsole->updateTitlebar();
759 break;
760
761 case CONEVENT_USR_SCREENRESIZE:
762 {
763 LogFlow(("CONEVENT_USR_SCREENRESIZE\n"));
764 gFramebuffer->resize();
765 /* notify the display that the resize has been completed */
766 gDisplay->ResizeCompleted();
767 break;
768 }
769
770#ifdef VBOX_SECURELABEL
771 case CONEVENT_USR_SECURELABELUPDATE:
772 {
773 /*
774 * Query the new label text
775 */
776 Bstr key = VBOXSDL_SECURELABEL_EXTRADATA;
777 Bstr label;
778 gMachine->COMGETTER(ExtraData)(key, label.asOutParam());
779 Utf8Str labelUtf8 = label;
780 /*
781 * Now update the label
782 */
783 gFramebuffer->setSecureLabelText(labelUtf8.raw());
784 break;
785 }
786#endif /* VBOX_SECURELABEL */
787
788 }
789
790 }
791
792leave:
793 LogFlow(("Returning from main()!\n"));
794
795 if (pVM)
796 {
797 /*
798 * If get here because the guest terminated using ACPI off we don't have to
799 * switch off the VM because we were notified via vmstateChangeCallback()
800 * that this already happened. In any other case stop the VM before killing her.
801 */
802 if (machineState != VMSTATE_OFF)
803 {
804 /* Power off VM */
805 PVMREQ pReq;
806 rc = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT, (PFNRT)VMR3PowerOff, 1, pVM);
807 }
808
809 /* And destroy it */
810 rc = VMR3Destroy(pVM);
811 AssertRC(rc);
812 }
813
814 delete gFramebuffer;
815 delete gConsole;
816 delete gDisplay;
817 delete gKeyboard;
818 delete gMouse;
819 delete gStatus;
820 delete gMachineDebugger;
821
822 RTLogFlush(NULL);
823 return VBOX_FAILURE (rc) ? 1 : 0;
824}
825
826
827
828/**
829 * VM state callback function. Called by the VMM
830 * using its state machine states.
831 *
832 * Primarily used to handle VM initiated power off, suspend and state saving,
833 * but also for doing termination completed work (VMSTATE_TERMINATE).
834 *
835 * In general this function is called in the context of the EMT.
836 *
837 * @todo machineState is set to VMSTATE_RUNNING before all devices have received power on events
838 * this can prematurely allow the main thread to enter the event loop
839 *
840 * @param pVM The VM handle.
841 * @param enmState The new state.
842 * @param enmOldState The old state.
843 * @param pvUser The user argument.
844 */
845static DECLCALLBACK(void) vmstateChangeCallback(PVM pVM, VMSTATE enmState, VMSTATE enmOldState, void *pvUser)
846{
847 LogFlow(("vmstateChangeCallback: changing state from %d to %d\n", enmOldState, enmState));
848 machineState = enmState;
849
850 switch (enmState)
851 {
852 /*
853 * The VM has terminated
854 */
855 case VMSTATE_OFF:
856 {
857 gConsole->eventQuit();
858 break;
859 }
860
861 /*
862 * The VM has been completely destroyed.
863 *
864 * Note: This state change can happen at two points:
865 * 1) At the end of VMR3Destroy() if it was not called from EMT.
866 * 2) At the end of vmR3EmulationThread if VMR3Destroy() was called by EMT.
867 */
868 case VMSTATE_TERMINATED:
869 {
870 break;
871 }
872
873 default: /* shut up gcc */
874 break;
875 }
876}
877
878
879/**
880 * VM error callback function. Called by the various VM components.
881 *
882 * @param pVM The VM handle.
883 * @param pvUser The user argument.
884 * @param rc VBox status code.
885 * @param pszError Error message format string.
886 * @param args Error message arguments.
887 * @thread EMT.
888 */
889DECLCALLBACK(void) setVMErrorCallback(PVM pVM, void *pvUser, int rc, RT_SRC_POS_DECL,
890 const char *pszFormat, va_list args)
891{
892 /** @todo accessing shared resource without any kind of synchronization */
893 if (VBOX_SUCCESS(rc))
894 szError[0] = '\0';
895 else
896 RTStrPrintfV(szError, sizeof(szError), pszFormat, args);
897}
898
899
900/** VM asynchronous operations thread */
901DECLCALLBACK(int) VMPowerUpThread(RTTHREAD Thread, void *pvUser)
902{
903 int rc = VINF_SUCCESS;
904 int rc2;
905
906 /*
907 * Setup the release log instance in current directory.
908 */
909 if (g_fReleaseLog)
910 {
911 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
912 PRTLOGGER pLogger;
913 rc2 = RTLogCreate(&pLogger, RTLOGFLAGS_PREFIX_TIME_PROG, "all",
914 "VBOX_RELEASE_LOG", ELEMENTS(s_apszGroups), s_apszGroups,
915 RTLOGDEST_FILE, "./VBoxBFE.log");
916 if (VBOX_SUCCESS(rc2))
917 {
918 /* some introductory information */
919 RTTIMESPEC TimeSpec;
920 char szNowUct[64];
921 RTTimeSpecToString(RTTimeNow(&TimeSpec), szNowUct, sizeof(szNowUct));
922 RTLogRelLogger(pLogger, 0, ~0U,
923 "VBoxBFE %s (%s %s) release log\n"
924 "Log opened %s\n",
925 VBOX_VERSION_STRING, __DATE__, __TIME__,
926 szNowUct);
927
928 /* register this logger as the release logger */
929 RTLogRelSetDefaultInstance(pLogger);
930 }
931 }
932
933 /*
934 * Start VM (also from saved state) and track progress
935 */
936 LogFlow(("VMPowerUp\n"));
937
938 /*
939 * Create empty VM.
940 */
941 rc = VMR3Create(setVMErrorCallback, NULL, vboxbfeConfigConstructor, NULL, &pVM);
942 if (VBOX_FAILURE(rc))
943 {
944 RTPrintf("Error: VM creation failed with %Vrc.\n", rc);
945 goto failure;
946 }
947
948
949 /*
950 * Register VM state change handler
951 */
952 rc = VMR3AtStateRegister(pVM, vmstateChangeCallback, NULL);
953 if (VBOX_FAILURE(rc))
954 {
955 RTPrintf("Error: VMR3AtStateRegister failed with %Vrc.\n", rc);
956 goto failure;
957 }
958
959#ifdef VBOXBFE_WITH_USB
960 /*
961 * Capture USB devices.
962 */
963 if (fUSB)
964 {
965 gHostUSB = new HostUSB();
966 gHostUSB->init(pVM);
967 }
968#endif /* VBOXBFE_WITH_USB */
969
970#ifdef __L4ENV__
971 /* L4 console cannot draw a host cursor */
972 gMouse->setHostCursor(false);
973#else
974 gMouse->setHostCursor(true);
975#endif
976
977 /*
978 * Power on the VM (i.e. start executing).
979 */
980 if (VBOX_SUCCESS(rc))
981 {
982 PVMREQ pReq;
983 rc = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT, (PFNRT)VMR3PowerOn, 1, pVM);
984 if (VBOX_SUCCESS(rc))
985 {
986 rc = pReq->iStatus;
987 AssertRC(rc);
988 VMR3ReqFree(pReq);
989 }
990 else
991 AssertMsgFailed(("VMR3PowerOn failed, rc=%Vrc\n", rc));
992 }
993
994 /*
995 * On failure destroy the VM.
996 */
997 if (VBOX_FAILURE(rc))
998 {
999 goto failure;
1000 }
1001 return 0;
1002
1003
1004failure:
1005 if (pVM)
1006 {
1007 rc2 = VMR3Destroy(pVM);
1008 AssertRC(rc2);
1009 pVM = NULL;
1010 }
1011 machineState = VMSTATE_TERMINATED;
1012 return 0;
1013}
1014
1015/**
1016 * Register the main drivers.
1017 *
1018 * @returns VBox status code.
1019 * @param pCallbacks Pointer to the callback table.
1020 * @param u32Version VBox version number.
1021 */
1022DECLCALLBACK(int) VBoxDriversRegister(PCPDMDRVREGCB pCallbacks, uint32_t u32Version)
1023{
1024 int rc;
1025
1026 LogFlow(("VBoxDriversRegister: u32Version=%#x\n", u32Version));
1027 AssertReleaseMsg(u32Version == VBOX_VERSION, ("u32Version=%#x VBOX_VERSION=%#x\n", u32Version, VBOX_VERSION));
1028
1029 rc = pCallbacks->pfnRegister(pCallbacks, &Mouse::DrvReg);
1030 AssertRC(rc);
1031 if (VBOX_FAILURE(rc))
1032 return rc;
1033 rc = pCallbacks->pfnRegister(pCallbacks, &Keyboard::DrvReg);
1034 AssertRC(rc);
1035 if (VBOX_FAILURE(rc))
1036 return rc;
1037
1038 rc = pCallbacks->pfnRegister(pCallbacks, &VMDisplay::DrvReg);
1039 AssertRC(rc);
1040 if (VBOX_FAILURE(rc))
1041 return rc;
1042 rc = pCallbacks->pfnRegister(pCallbacks, &VMMDev::DrvReg);
1043 AssertRC(rc);
1044 if (VBOX_FAILURE(rc))
1045 return rc;
1046
1047 rc = pCallbacks->pfnRegister(pCallbacks, &VMStatus::DrvReg);
1048 if (VBOX_FAILURE(rc))
1049 return rc;
1050
1051 return VINF_SUCCESS;
1052}
1053
1054
1055/**
1056 * Constructs the VMM configuration tree.
1057 *
1058 * @returns VBox status code.
1059 * @param pVM VM handle.
1060 */
1061static DECLCALLBACK(int) vboxbfeConfigConstructor(PVM pVM, void *pvUser)
1062{
1063 int rcAll = VINF_SUCCESS;
1064 int rc;
1065
1066#define UPDATERC() do { if (VBOX_FAILURE(rc) && VBOX_SUCCESS(rcAll)) rcAll = rc; } while (0)
1067#undef CHECK_RC /** @todo r=bird: clashes with VBox/com/Assert.h. */
1068#define CHECK_RC() UPDATERC()
1069
1070 /*
1071 * Root values.
1072 */
1073 PCFGMNODE pRoot = CFGMR3GetRoot(pVM);
1074 rc = CFGMR3InsertString(pRoot, "Name", "Default VM");
1075 UPDATERC();
1076 rc = CFGMR3InsertInteger(pRoot, "RamSize", memorySize * _1M);
1077 UPDATERC();
1078 rc = CFGMR3InsertInteger(pRoot, "TimerMillies", 10);
1079 UPDATERC();
1080#ifdef VBOXSDL_ADVANCED_OPTIONS
1081 rc = CFGMR3InsertInteger(pRoot, "RawR3Enabled", (fRawR3 != ~0U) ? fRawR3 : 1);
1082 UPDATERC();
1083 rc = CFGMR3InsertInteger(pRoot, "RawR0Enabled", (fRawR0 != ~0U) ? fRawR0 : 1);
1084 UPDATERC();
1085 rc = CFGMR3InsertInteger(pRoot, "PATMEnabled", (fPATM != ~0U) ? fPATM : 1);
1086 UPDATERC();
1087 rc = CFGMR3InsertInteger(pRoot, "CSAMEnabled", (fCSAM != ~0U) ? fCSAM : 1);
1088#else
1089 rc = CFGMR3InsertInteger(pRoot, "RawR3Enabled", 1);
1090 UPDATERC();
1091 rc = CFGMR3InsertInteger(pRoot, "RawR0Enabled", 1);
1092 UPDATERC();
1093 rc = CFGMR3InsertInteger(pRoot, "PATMEnabled", 1);
1094 UPDATERC();
1095 rc = CFGMR3InsertInteger(pRoot, "CSAMEnabled", 1);
1096#endif
1097 UPDATERC();
1098
1099 /*
1100 * PDM.
1101 */
1102 rc = PDMR3RegisterDrivers(pVM, VBoxDriversRegister);
1103 UPDATERC();
1104
1105 /*
1106 * Devices
1107 */
1108 PCFGMNODE pDevices = NULL;
1109 rc = CFGMR3InsertNode(pRoot, "Devices", &pDevices);
1110 UPDATERC();
1111 /* device */
1112 PCFGMNODE pDev = NULL;
1113 PCFGMNODE pInst = NULL;
1114 PCFGMNODE pCfg = NULL;
1115 PCFGMNODE pLunL0 = NULL;
1116 PCFGMNODE pLunL1 = NULL;
1117
1118 /*
1119 * PC Arch.
1120 */
1121 rc = CFGMR3InsertNode(pDevices, "pcarch", &pDev);
1122 UPDATERC();
1123 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1124 UPDATERC();
1125 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
1126 UPDATERC();
1127 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1128 UPDATERC();
1129
1130 /*
1131 * PC Bios.
1132 */
1133 rc = CFGMR3InsertNode(pDevices, "pcbios", &pDev);
1134 UPDATERC();
1135 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1136 UPDATERC();
1137 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
1138 UPDATERC();
1139 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1140 UPDATERC();
1141 rc = CFGMR3InsertInteger(pCfg, "RamSize", memorySize * _1M);
1142 UPDATERC();
1143 rc = CFGMR3InsertString(pCfg, "BootDevice0", pszBootDevice);
1144 UPDATERC();
1145 rc = CFGMR3InsertString(pCfg, "BootDevice1", "NONE");
1146 UPDATERC();
1147 rc = CFGMR3InsertString(pCfg, "BootDevice2", "NONE");
1148 UPDATERC();
1149 rc = CFGMR3InsertString(pCfg, "BootDevice3", "NONE");
1150 UPDATERC();
1151 rc = CFGMR3InsertString(pCfg, "HardDiskDevice", "piix3ide");
1152 UPDATERC();
1153 rc = CFGMR3InsertString(pCfg, "FloppyDevice", "i82078");
1154 UPDATERC();
1155
1156 /* Default: no bios logo. */
1157 rc = CFGMR3InsertInteger(pCfg, "FadeIn", 1);
1158 UPDATERC();
1159 rc = CFGMR3InsertInteger(pCfg, "FadeOut", 0);
1160 UPDATERC();
1161 rc = CFGMR3InsertInteger(pCfg, "LogoTime", 0);
1162 UPDATERC();
1163 rc = CFGMR3InsertString(pCfg, "LogoFile", "");
1164 UPDATERC();
1165
1166 /*
1167 * ACPI
1168 */
1169 if (fACPI)
1170 {
1171 rc = CFGMR3InsertNode(pDevices, "acpi", &pDev); CHECK_RC();
1172 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1173 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1174 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1175 rc = CFGMR3InsertInteger(pCfg, "RamSize", memorySize * _1M); CHECK_RC();
1176 rc = CFGMR3InsertInteger(pCfg, "IOAPIC", g_fIOAPIC); CHECK_RC();
1177 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 7); CHECK_RC();
1178 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); CHECK_RC();
1179
1180 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1181 rc = CFGMR3InsertString(pLunL0, "Driver", "ACPIHost"); CHECK_RC();
1182 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1183 }
1184
1185 /*
1186 * PCI bus.
1187 */
1188 rc = CFGMR3InsertNode(pDevices, "pci", &pDev); /* piix3 */
1189 UPDATERC();
1190 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1191 UPDATERC();
1192 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */
1193 UPDATERC();
1194 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1195 UPDATERC();
1196 rc = CFGMR3InsertInteger(pCfg, "IOAPIC", g_fIOAPIC); CHECK_RC();
1197
1198 /*
1199 * DMA
1200 */
1201 rc = CFGMR3InsertNode(pDevices, "8237A", &pDev); CHECK_RC();
1202 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1203 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1204
1205 /*
1206 * PCI bus.
1207 */
1208 rc = CFGMR3InsertNode(pDevices, "pci", &pDev); /* piix3 */ CHECK_RC();
1209 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1210 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1211 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1212
1213 /*
1214 * PS/2 keyboard & mouse.
1215 */
1216 rc = CFGMR3InsertNode(pDevices, "pckbd", &pDev); CHECK_RC();
1217 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1218 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1219 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1220
1221 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1222 rc = CFGMR3InsertString(pLunL0, "Driver", "KeyboardQueue"); CHECK_RC();
1223 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1224 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 64); CHECK_RC();
1225
1226 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); CHECK_RC();
1227 rc = CFGMR3InsertString(pLunL1, "Driver", "MainKeyboard"); CHECK_RC();
1228 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); CHECK_RC();
1229 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)gKeyboard); CHECK_RC();
1230
1231 rc = CFGMR3InsertNode(pInst, "LUN#1", &pLunL0); CHECK_RC();
1232 rc = CFGMR3InsertString(pLunL0, "Driver", "MouseQueue"); CHECK_RC();
1233 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1234 rc = CFGMR3InsertInteger(pCfg, "QueueSize", 128); CHECK_RC();
1235
1236 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); CHECK_RC();
1237 rc = CFGMR3InsertString(pLunL1, "Driver", "MainMouse"); CHECK_RC();
1238 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); CHECK_RC();
1239 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)gMouse); CHECK_RC();
1240
1241
1242 /*
1243 * i82078 Floppy drive controller
1244 */
1245 rc = CFGMR3InsertNode(pDevices, "i82078", &pDev); CHECK_RC();
1246 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1247 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); CHECK_RC();
1248 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1249 rc = CFGMR3InsertInteger(pCfg, "IRQ", 6); CHECK_RC();
1250 rc = CFGMR3InsertInteger(pCfg, "DMA", 2); CHECK_RC();
1251 rc = CFGMR3InsertInteger(pCfg, "MemMapped", 0 ); CHECK_RC();
1252 rc = CFGMR3InsertInteger(pCfg, "IOBase", 0x3f0); CHECK_RC();
1253
1254 /* Attach the status driver */
1255 rc = CFGMR3InsertNode(pInst, "LUN#999", &pLunL0); CHECK_RC();
1256 rc = CFGMR3InsertString(pLunL0, "Driver", "MainStatus"); CHECK_RC();
1257 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1258 rc = CFGMR3InsertInteger(pCfg, "papLeds", (uintptr_t)&mapFDLeds[0]); CHECK_RC();
1259 rc = CFGMR3InsertInteger(pCfg, "First", 0); CHECK_RC();
1260 rc = CFGMR3InsertInteger(pCfg, "Last", 0); CHECK_RC();
1261
1262 if (fdaFile)
1263 {
1264 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1265 rc = CFGMR3InsertString(pLunL0, "Driver", "Block"); CHECK_RC();
1266 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1267 rc = CFGMR3InsertString(pCfg, "Type", "Floppy 1.44"); CHECK_RC();
1268 rc = CFGMR3InsertInteger(pCfg, "Mountable", 1); CHECK_RC();
1269
1270 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); CHECK_RC();
1271 rc = CFGMR3InsertString(pLunL1, "Driver", "RawImage"); CHECK_RC();
1272 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); CHECK_RC();
1273 rc = CFGMR3InsertString(pCfg, "Path", fdaFile); CHECK_RC();
1274 }
1275
1276 /*
1277 * i8254 Programmable Interval Timer And Dummy Speaker
1278 */
1279 rc = CFGMR3InsertNode(pDevices, "i8254", &pDev); CHECK_RC();
1280 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1281 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1282#ifdef DEBUG
1283 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1284#endif
1285
1286 /*
1287 * i8259 Programmable Interrupt Controller.
1288 */
1289 rc = CFGMR3InsertNode(pDevices, "i8259", &pDev); CHECK_RC();
1290 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1291 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1292 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1293
1294 /*
1295 * Advanced Programmable Interrupt Controller.
1296 */
1297 rc = CFGMR3InsertNode(pDevices, "apic", &pDev); CHECK_RC();
1298 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1299 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1300 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1301
1302 /*
1303 * I/O Advanced Programmable Interrupt Controller.
1304 */
1305 if (g_fIOAPIC)
1306 {
1307 rc = CFGMR3InsertNode(pDevices, "ioapic", &pDev); CHECK_RC();
1308 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1309 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1310 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1311 }
1312
1313 /*
1314 * RTC MC146818.
1315 */
1316 rc = CFGMR3InsertNode(pDevices, "mc146818", &pDev); CHECK_RC();
1317 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1318 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1319
1320 /*
1321 * Serial ports
1322 */
1323 rc = CFGMR3InsertNode(pDevices, "serial", &pDev); CHECK_RC();
1324 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1325 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1326 rc = CFGMR3InsertInteger(pCfg, "IRQ", 4); CHECK_RC();
1327 rc = CFGMR3InsertInteger(pCfg, "IOBase", 0x3f8); CHECK_RC();
1328
1329 rc = CFGMR3InsertNode(pDev, "1", &pInst); CHECK_RC();
1330 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1331 rc = CFGMR3InsertInteger(pCfg, "IRQ", 3); CHECK_RC();
1332 rc = CFGMR3InsertInteger(pCfg, "IOBase", 0x2f8); CHECK_RC();
1333
1334 /*
1335 * VGA.
1336 */
1337 rc = CFGMR3InsertNode(pDevices, "vga", &pDev); CHECK_RC();
1338 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1339 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1340 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 2); CHECK_RC();
1341 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); CHECK_RC();
1342 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1343 rc = CFGMR3InsertInteger(pCfg, "VRamSize", vramSize * _1M); CHECK_RC();
1344
1345#ifdef __L4ENV__
1346 /* XXX hard-coded */
1347 rc = CFGMR3InsertInteger(pCfg, "HeightReduction", 18); CHECK_RC();
1348 rc = CFGMR3InsertInteger(pCfg, "CustomVideoModes", 1); CHECK_RC();
1349 char szBuf[64];
1350 /* Tell the guest which is the ideal video mode to use */
1351 RTStrPrintf(szBuf, sizeof(szBuf), "%dx%dx%d",
1352 gFramebuffer->getHostXres(),
1353 gFramebuffer->getHostYres(),
1354 gFramebuffer->getHostBitsPerPixel());
1355 rc = CFGMR3InsertString(pCfg, "CustomVideoMode1", szBuf); CHECK_RC();
1356#endif
1357
1358 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1359 rc = CFGMR3InsertString(pLunL0, "Driver", "MainDisplay"); CHECK_RC();
1360 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1361 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)gDisplay); CHECK_RC();
1362
1363 /*
1364 * IDE (update this when the main interface changes)
1365 */
1366 rc = CFGMR3InsertNode(pDevices, "piix3ide", &pDev); /* piix3 */ CHECK_RC();
1367 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1368 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1369 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 1); CHECK_RC();
1370 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 1); CHECK_RC();
1371 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1372
1373 if (hdaFile)
1374 {
1375 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1376 rc = CFGMR3InsertString(pLunL0, "Driver", "Block"); CHECK_RC();
1377 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1378 rc = CFGMR3InsertString(pCfg, "Type", "HardDisk"); CHECK_RC();
1379 rc = CFGMR3InsertInteger(pCfg, "Mountable", 0); CHECK_RC();
1380
1381 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); CHECK_RC();
1382 rc = CFGMR3InsertString(pLunL1, "Driver", "VBoxHDD"); CHECK_RC();
1383 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); CHECK_RC();
1384 rc = CFGMR3InsertString(pCfg, "Path", hdaFile); CHECK_RC();
1385 }
1386
1387 if (cdromFile)
1388 {
1389 // ASSUME: DVD drive is always attached to LUN#2 (i.e. secondary IDE master)
1390 rc = CFGMR3InsertNode(pInst, "LUN#2", &pLunL0); CHECK_RC();
1391 rc = CFGMR3InsertString(pLunL0, "Driver", "Block"); CHECK_RC();
1392 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1393 rc = CFGMR3InsertString(pCfg, "Type", "DVD"); CHECK_RC();
1394 rc = CFGMR3InsertInteger(pCfg, "Mountable", 1); CHECK_RC();
1395
1396 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1); CHECK_RC();
1397 rc = CFGMR3InsertString(pLunL1, "Driver", "MediaISO"); CHECK_RC();
1398 rc = CFGMR3InsertNode(pLunL1, "Config", &pCfg); CHECK_RC();
1399 rc = CFGMR3InsertString(pCfg, "Path", cdromFile); CHECK_RC();
1400 }
1401
1402 /*
1403 * Network adapters
1404 */
1405 rc = CFGMR3InsertNode(pDevices, "pcnet", &pDev); CHECK_RC();
1406 for (ULONG ulInstance = 0; ulInstance < NetworkAdapterCount; ulInstance++)
1407 {
1408 if (g_aNetDevs[ulInstance].enmType != BFENETDEV::NOT_CONFIGURED)
1409 {
1410 char szInstance[4];
1411 RTStrPrintf(szInstance, sizeof(szInstance), "%lu", ulInstance);
1412 rc = CFGMR3InsertNode(pDev, szInstance, &pInst); CHECK_RC();
1413 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); CHECK_RC();
1414 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo",
1415 !ulInstance ? 3 : ulInstance - 1 + 8); CHECK_RC();
1416 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); CHECK_RC();
1417 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1418 rc = CFGMR3InsertBytes(pCfg, "MAC", &g_aNetDevs[ulInstance].Mac, sizeof(PDMMAC));
1419 CHECK_RC();
1420
1421 /*
1422 * Enable the packet sniffer if requested.
1423 */
1424 if (g_aNetDevs[ulInstance].fSniff)
1425 {
1426 /* insert the sniffer filter driver. */
1427 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1428 rc = CFGMR3InsertString(pLunL0, "Driver", "NetSniffer"); CHECK_RC();
1429 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1430 if (g_aNetDevs[ulInstance].pszSniff)
1431 {
1432 rc = CFGMR3InsertString(pCfg, "File", g_aNetDevs[ulInstance].pszSniff); CHECK_RC();
1433 }
1434 }
1435
1436 /*
1437 * Create the driver config (if any).
1438 */
1439 if (g_aNetDevs[ulInstance].enmType != BFENETDEV::NONE)
1440 {
1441 if (g_aNetDevs[ulInstance].fSniff)
1442 {
1443 rc = CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL0); CHECK_RC();
1444 }
1445 else
1446 {
1447 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1448 }
1449 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1450 }
1451
1452 /*
1453 * Configure the driver.
1454 */
1455 if (g_aNetDevs[ulInstance].enmType == BFENETDEV::NAT)
1456 {
1457 rc = CFGMR3InsertString(pLunL0, "Driver", "NAT"); CHECK_RC();
1458 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1459 /* (Port forwarding goes here.) */
1460 }
1461 else if (g_aNetDevs[ulInstance].enmType == BFENETDEV::HIF)
1462 {
1463 rc = CFGMR3InsertString(pLunL0, "Driver", "HostInterface"); CHECK_RC();
1464 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1465
1466#if defined(__LINUX__)
1467 if (g_aNetDevs[ulInstance].fHaveFd)
1468 {
1469 rc = CFGMR3InsertString(pCfg, "Device", g_aNetDevs[ulInstance].pszName); CHECK_RC();
1470 rc = CFGMR3InsertInteger(pCfg, "FileHandle", g_aNetDevs[ulInstance].fd); CHECK_RC();
1471 }
1472 else
1473#endif
1474 {
1475#if defined (__LINUX__) || defined (__L4__)
1476 /*
1477 * Create/Open the TAP the device.
1478 */
1479 RTFILE tapFD;
1480 rc = RTFileOpen(&tapFD, "/dev/net/tun",
1481 RTFILE_O_READWRITE | RTFILE_O_OPEN |
1482 RTFILE_O_DENY_NONE | RTFILE_O_INHERIT);
1483 if (VBOX_FAILURE(rc))
1484 {
1485 FatalError("Failed to open /dev/net/tun: %Vrc\n", rc);
1486 return rc;
1487 }
1488
1489 struct ifreq IfReq;
1490 memset(&IfReq, 0, sizeof(IfReq));
1491 if (g_aNetDevs[ulInstance].pszName && g_aNetDevs[ulInstance].pszName[0])
1492 {
1493 size_t cch = strlen(g_aNetDevs[ulInstance].pszName);
1494 if (cch >= sizeof(IfReq.ifr_name))
1495 {
1496 FatalError("HIF name too long for device #%d: %s\n",
1497 ulInstance + 1, g_aNetDevs[ulInstance].pszName);
1498 return VERR_BUFFER_OVERFLOW;
1499 }
1500 memcpy(IfReq.ifr_name, g_aNetDevs[ulInstance].pszName, cch + 1);
1501 }
1502 else
1503 strcpy(IfReq.ifr_name, "tun%d");
1504 IfReq.ifr_flags = IFF_TAP | IFF_NO_PI;
1505 rc = ioctl(tapFD, TUNSETIFF, &IfReq);
1506 if (rc)
1507 {
1508 int rc2 = RTErrConvertFromErrno(errno);
1509 FatalError("ioctl TUNSETIFF '%s' failed: errno=%d rc=%d (%Vrc)\n",
1510 IfReq.ifr_name, errno, rc, rc2);
1511 return rc2;
1512 }
1513
1514 rc = fcntl(tapFD, F_SETFL, O_NONBLOCK);
1515 if (rc)
1516 {
1517 int rc2 = RTErrConvertFromErrno(errno);
1518 FatalError("fcntl F_SETFL/O_NONBLOCK '%s' failed: errno=%d rc=%d (%Vrc)\n",
1519 IfReq.ifr_name, errno, rc, rc2);
1520 return rc2;
1521 }
1522
1523 rc = CFGMR3InsertString(pCfg, "Device", g_aNetDevs[ulInstance].pszName); CHECK_RC();
1524 rc = CFGMR3InsertInteger(pCfg, "FileHandle", (RTFILE)tapFD); CHECK_RC();
1525
1526#elif defined(__WIN__)
1527 /*
1528 * We need the GUID too here...
1529 */
1530 rc = CFGMR3InsertString(pCfg, "Device", g_aNetDevs[ulInstance].pszName); CHECK_RC();
1531 rc = CFGMR3InsertString(pCfg, "HostInterfaceName", g_aNetDevs[ulInstance].pszName); CHECK_RC();
1532 rc = CFGMR3InsertString(pCfg, "GUID", g_aNetDevs[ulInstance].pszName /*pszGUID*/); CHECK_RC();
1533
1534
1535#else /* !__LINUX__ && !__L4__ */
1536 FatalError("Name based HIF devices not implemented yet for this host platform\n");
1537 return VERR_NOT_IMPLEMENTED;
1538#endif
1539 }
1540 }
1541 else if (g_aNetDevs[ulInstance].enmType == BFENETDEV::INTNET)
1542 {
1543 /*
1544 * Internal networking.
1545 */
1546 rc = CFGMR3InsertString(pCfg, "Network", g_aNetDevs[ulInstance].pszName); CHECK_RC();
1547 }
1548 }
1549 }
1550
1551 /*
1552 * VMM Device
1553 */
1554 rc = CFGMR3InsertNode(pDevices, "VMMDev", &pDev); CHECK_RC();
1555 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1556 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1557 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1558 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 4); CHECK_RC();
1559 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); CHECK_RC();
1560
1561 /* the VMM device's Main driver */
1562 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1563 rc = CFGMR3InsertString(pLunL0, "Driver", "MainVMMDev"); CHECK_RC();
1564 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1565 rc = CFGMR3InsertInteger(pCfg, "Object", (uintptr_t)gVMMDev); CHECK_RC();
1566
1567 /*
1568 * AC'97 ICH audio
1569 */
1570 if (fAudio)
1571 {
1572 rc = CFGMR3InsertNode(pDevices, "ichac97", &pDev);
1573 rc = CFGMR3InsertNode(pDev, "0", &pInst);
1574 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1575 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 5); CHECK_RC();
1576 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); CHECK_RC();
1577 rc = CFGMR3InsertNode(pInst, "Config", &pCfg);
1578
1579 /* the Audio driver */
1580 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1581 rc = CFGMR3InsertString(pLunL0, "Driver", "AUDIO"); CHECK_RC();
1582 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1583#ifdef __WIN__
1584 rc = CFGMR3InsertString(pCfg, "AudioDriver", "winmm"); CHECK_RC();
1585#elif defined(__DARWIN__)
1586 rc = CFGMR3InsertString(pCfg, "AudioDriver", "coreaudio"); CHECK_RC();
1587#elif defined(__LINUX__)
1588 rc = CFGMR3InsertString(pCfg, "AudioDriver", "oss"); CHECK_RC();
1589#else /* portme */
1590 rc = CFGMR3InsertString(pCfg, "AudioDriver", "none"); CHECK_RC();
1591#endif /* !__WIN__ */
1592 }
1593
1594#ifdef VBOXBFE_WITH_USB
1595 /*
1596 * The USB Controller.
1597 */
1598 if (fUSB)
1599 {
1600 rc = CFGMR3InsertNode(pDevices, "usb-ohci", &pDev); CHECK_RC();
1601 rc = CFGMR3InsertNode(pDev, "0", &pInst); CHECK_RC();
1602 rc = CFGMR3InsertNode(pInst, "Config", &pCfg); CHECK_RC();
1603 rc = CFGMR3InsertInteger(pInst, "Trusted", 1); /* boolean */ CHECK_RC();
1604 rc = CFGMR3InsertInteger(pInst, "PCIDeviceNo", 6); CHECK_RC();
1605 rc = CFGMR3InsertInteger(pInst, "PCIFunctionNo", 0); CHECK_RC();
1606
1607 rc = CFGMR3InsertNode(pInst, "LUN#0", &pLunL0); CHECK_RC();
1608 rc = CFGMR3InsertString(pLunL0, "Driver", "VUSBRootHub"); CHECK_RC();
1609 rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg); CHECK_RC();
1610 }
1611#endif /* VBOXBFE_WITH_USB */
1612
1613#undef UPDATERC
1614#undef CHECK_RC
1615
1616 return rc;
1617}
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