Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / interface / vmware / vmconsole.c
1 /*
2  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /** @file
23  *
24  * VMware logfile console
25  *
26  */
27
28 #include <string.h>
29 #include <ipxe/console.h>
30 #include <ipxe/lineconsole.h>
31 #include <ipxe/init.h>
32 #include <ipxe/guestrpc.h>
33 #include <config/console.h>
34
35 /** VMware logfile console buffer size */
36 #define VMCONSOLE_BUFSIZE 128
37
38 /* Set default console usage if applicable */
39 #if ! ( defined ( CONSOLE_VMWARE ) && CONSOLE_EXPLICIT ( CONSOLE_VMWARE ) )
40 #undef CONSOLE_VMWARE
41 #define CONSOLE_VMWARE ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_TUI )
42 #endif
43
44 /** VMware logfile console GuestRPC channel */
45 static int vmconsole_channel;
46
47 /** VMware logfile console line buffer */
48 static struct {
49         char prefix[4];
50         char message[VMCONSOLE_BUFSIZE];
51 } vmconsole_buffer = {
52         .prefix = "log ",
53 };
54
55 /** VMware logfile console ANSI escape sequence handlers */
56 static struct ansiesc_handler vmconsole_handlers[] = {
57         { 0, NULL }
58 };
59
60 /** VMware logfile line console */
61 static struct line_console vmconsole_line = {
62         .buffer = vmconsole_buffer.message,
63         .len = sizeof ( vmconsole_buffer.message ),
64         .ctx = {
65                 .handlers = vmconsole_handlers,
66         },
67 };
68
69 /** VMware logfile console recursion marker */
70 static int vmconsole_entered;
71
72 /**
73  * Print a character to VMware logfile console
74  *
75  * @v character         Character to be printed
76  */
77 static void vmconsole_putchar ( int character ) {
78         int rc;
79
80         /* Ignore if we are already mid-logging */
81         if ( vmconsole_entered )
82                 return;
83
84         /* Fill line buffer */
85         if ( line_putchar ( &vmconsole_line, character ) == 0 )
86                 return;
87
88         /* Guard against re-entry */
89         vmconsole_entered = 1;
90
91         /* Send log message */
92         if ( ( rc = guestrpc_command ( vmconsole_channel,
93                                        vmconsole_buffer.prefix, NULL, 0 ) ) <0){
94                 DBG ( "VMware console could not send log message: %s\n",
95                       strerror ( rc ) );
96         }
97
98         /* Clear re-entry flag */
99         vmconsole_entered = 0;
100 }
101
102 /** VMware logfile console driver */
103 struct console_driver vmconsole __console_driver = {
104         .putchar = vmconsole_putchar,
105         .disabled = CONSOLE_DISABLED,
106         .usage = CONSOLE_VMWARE,
107 };
108
109 /**
110  * Initialise VMware logfile console
111  *
112  */
113 static void vmconsole_init ( void ) {
114         int rc;
115
116         /* Attempt to open console */
117         vmconsole_channel = guestrpc_open();
118         if ( vmconsole_channel < 0 ) {
119                 rc = vmconsole_channel;
120                 DBG ( "VMware console could not be initialised: %s\n",
121                       strerror ( rc ) );
122                 return;
123         }
124
125         /* Mark console as available */
126         vmconsole.disabled = 0;
127 }
128
129 /**
130  * VMware logfile console initialisation function
131  */
132 struct init_fn vmconsole_init_fn __init_fn ( INIT_CONSOLE ) = {
133         .initialise = vmconsole_init,
134 };