These changes are the raw update to qemu-2.6.
[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  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 /** @file
27  *
28  * VMware logfile console
29  *
30  */
31
32 #include <string.h>
33 #include <ipxe/console.h>
34 #include <ipxe/lineconsole.h>
35 #include <ipxe/init.h>
36 #include <ipxe/guestrpc.h>
37 #include <config/console.h>
38
39 /** VMware logfile console buffer size */
40 #define VMCONSOLE_BUFSIZE 128
41
42 /* Set default console usage if applicable */
43 #if ! ( defined ( CONSOLE_VMWARE ) && CONSOLE_EXPLICIT ( CONSOLE_VMWARE ) )
44 #undef CONSOLE_VMWARE
45 #define CONSOLE_VMWARE ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_TUI )
46 #endif
47
48 /** VMware logfile console GuestRPC channel */
49 static int vmconsole_channel;
50
51 /** VMware logfile console line buffer */
52 static struct {
53         char prefix[4];
54         char message[VMCONSOLE_BUFSIZE];
55 } vmconsole_buffer = {
56         .prefix = "log ",
57 };
58
59 /** VMware logfile console ANSI escape sequence handlers */
60 static struct ansiesc_handler vmconsole_handlers[] = {
61         { 0, NULL }
62 };
63
64 /** VMware logfile line console */
65 static struct line_console vmconsole_line = {
66         .buffer = vmconsole_buffer.message,
67         .len = sizeof ( vmconsole_buffer.message ),
68         .ctx = {
69                 .handlers = vmconsole_handlers,
70         },
71 };
72
73 /** VMware logfile console recursion marker */
74 static int vmconsole_entered;
75
76 /**
77  * Print a character to VMware logfile console
78  *
79  * @v character         Character to be printed
80  */
81 static void vmconsole_putchar ( int character ) {
82         int rc;
83
84         /* Ignore if we are already mid-logging */
85         if ( vmconsole_entered )
86                 return;
87
88         /* Fill line buffer */
89         if ( line_putchar ( &vmconsole_line, character ) == 0 )
90                 return;
91
92         /* Guard against re-entry */
93         vmconsole_entered = 1;
94
95         /* Send log message */
96         if ( ( rc = guestrpc_command ( vmconsole_channel,
97                                        vmconsole_buffer.prefix, NULL, 0 ) ) <0){
98                 DBG ( "VMware console could not send log message: %s\n",
99                       strerror ( rc ) );
100         }
101
102         /* Clear re-entry flag */
103         vmconsole_entered = 0;
104 }
105
106 /** VMware logfile console driver */
107 struct console_driver vmconsole __console_driver = {
108         .putchar = vmconsole_putchar,
109         .disabled = CONSOLE_DISABLED,
110         .usage = CONSOLE_VMWARE,
111 };
112
113 /**
114  * Initialise VMware logfile console
115  *
116  */
117 static void vmconsole_init ( void ) {
118         int rc;
119
120         /* Attempt to open console */
121         vmconsole_channel = guestrpc_open();
122         if ( vmconsole_channel < 0 ) {
123                 rc = vmconsole_channel;
124                 DBG ( "VMware console could not be initialised: %s\n",
125                       strerror ( rc ) );
126                 return;
127         }
128
129         /* Mark console as available */
130         vmconsole.disabled = 0;
131 }
132
133 /**
134  * VMware logfile console initialisation function
135  */
136 struct init_fn vmconsole_init_fn __init_fn ( INIT_CONSOLE ) = {
137         .initialise = vmconsole_init,
138 };