iPXE
serial.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 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  * Serial console
29  *
30  */
31 
32 #include <stddef.h>
33 #include <string.h>
34 #include <ipxe/init.h>
35 #include <ipxe/uart.h>
36 #include <ipxe/console.h>
37 #include <ipxe/serial.h>
38 #include <ipxe/ns16550.h>
39 #include <config/console.h>
40 #include <config/serial.h>
41 
42 /* Set default console usage if applicable */
43 #if ! ( defined ( CONSOLE_SERIAL ) && CONSOLE_EXPLICIT ( CONSOLE_SERIAL ) )
44 #undef CONSOLE_SERIAL
45 #define CONSOLE_SERIAL ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
46 #endif
47 
48 #ifdef SERIAL_FIXED
49 #define SERIAL_PREFIX_fixed
50 #else
51 #define SERIAL_PREFIX_fixed __fixed_
52 #endif
53 
54 /* Serial console UART */
55 #ifndef COMCONSOLE
56 #define COMCONSOLE NULL
57 #endif
58 
59 /* Serial console baud rate */
60 #ifndef COMSPEED
61 #define COMSPEED 0
62 #endif
63 
64 /** Active serial console UART
65  *
66  * Explicitly initialised to @c NULL since this variable may be
67  * accessed before .bss has been zeroed.
68  */
70 
71 /**
72  * Get fixed serial console UART
73  *
74  * @ret uart Serial console UART, or NULL
75  */
76 struct uart * fixed_serial_console ( void ) {
77  struct uart *uart = COMCONSOLE;
78  unsigned int baud = COMSPEED;
79 
80  /* Set default baud rate, if applicable */
81  if ( uart && baud )
82  uart->baud = baud;
83 
84  return uart;
85 }
86 
87 /**
88  * Print a character to serial console
89  *
90  * @v character Character to be printed
91  */
92 static void serial_putchar ( int character ) {
93 
94  /* Do nothing if we have no UART */
95  if ( ! serial_console )
96  return;
97 
98  /* Transmit character */
99  uart_transmit ( serial_console, character );
100 }
101 
102 /**
103  * Get character from serial console
104  *
105  * @ret character Character read from console
106  */
107 static int serial_getchar ( void ) {
108  uint8_t data;
109 
110  /* Do nothing if we have no UART */
111  if ( ! serial_console )
112  return 0;
113 
114  /* Wait for data to be ready */
115  while ( ! uart_data_ready ( serial_console ) ) {}
116 
117  /* Receive data */
118  data = uart_receive ( serial_console );
119 
120  /* Strip any high bit and convert DEL to backspace */
121  data &= 0x7f;
122  if ( data == 0x7f )
123  data = 0x08;
124 
125  return data;
126 }
127 
128 /**
129  * Check for character ready to read from serial console
130  *
131  * @ret True Character available to read
132  * @ret False No character available to read
133  */
134 static int serial_iskey ( void ) {
135 
136  /* Do nothing if we have no UART */
137  if ( ! serial_console )
138  return 0;
139 
140  /* Check UART */
141  return uart_data_ready ( serial_console );
142 }
143 
144 /** Serial console */
145 struct console_driver serial_console_driver __console_driver = {
147  .getchar = serial_getchar,
148  .iskey = serial_iskey,
149  .usage = CONSOLE_SERIAL,
150 };
151 
152 /** Initialise serial console */
153 static void serial_init ( void ) {
154  struct uart *uart;
155  int rc;
156 
157  /* Get default serial console, if any */
159  if ( ! uart )
160  return;
161 
162  /* Initialise UART */
163  if ( ( rc = uart_init ( uart ) ) != 0 ) {
164  DBGC ( uart, "SERIAL could not initialise %s: %s\n",
165  uart->name, strerror ( rc ) );
166  return;
167  }
168 
169  /* Record UART as serial console */
171  DBGC ( uart, "SERIAL using %s\n", uart->name );
172 }
173 
174 /**
175  * Shut down serial console
176  *
177  * @v flags Shutdown flags
178  */
179 static void serial_shutdown ( int flags __unused ) {
180 
181  /* Do nothing if we have no UART */
182  if ( ! serial_console )
183  return;
184 
185  /* Flush any pending output */
186  uart_flush ( serial_console );
187 
188  /* Leave console enabled; it's still usable */
189 }
190 
191 /** Serial console initialisation function */
192 struct init_fn serial_console_init_fn __init_fn ( INIT_CONSOLE ) = {
193  .name = "serial",
194  .initialise = serial_init,
195 };
196 
197 /** Serial console startup function */
198 struct startup_fn serial_startup_fn __startup_fn ( STARTUP_EARLY ) = {
199  .name = "serial",
200  .shutdown = serial_shutdown,
201 };
202 
static void serial_init(void)
Initialise serial console.
Definition: serial.c:153
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
Generic UART.
A generic UART.
Definition: uart.h:17
PROVIDE_SERIAL_INLINE(null, default_serial_console)
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
struct uart * fixed_serial_console(void)
Get fixed serial console UART.
Definition: serial.c:76
#define STARTUP_EARLY
Early startup.
Definition: init.h:63
#define DBGC(...)
Definition: compiler.h:505
struct eltorito_descriptor_fixed fixed
Fixed portion.
Definition: eltorito.h:13
Serial console.
struct uart * default_serial_console(void)
Get serial console UART.
16550-compatible UART
const char * name
Definition: init.h:43
const char * name
Name.
Definition: uart.h:21
unsigned int baud
Baud rate (if specified)
Definition: uart.h:26
A startup/shutdown function.
Definition: init.h:42
An initialisation function.
Definition: init.h:14
void(* putchar)(int character)
Write a character to the console.
Definition: console.h:68
struct console_driver serial_console_driver __console_driver
Serial console.
Definition: serial.c:145
struct startup_fn serial_startup_fn __startup_fn(STARTUP_EARLY)
Serial console startup function.
struct uart * serial_console
Active serial console UART.
Definition: serial.c:69
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
const char * name
Definition: init.h:15
User interaction.
static int serial_iskey(void)
Check for character ready to read from serial console.
Definition: serial.c:134
uint8_t flags
Flags.
Definition: ena.h:18
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
unsigned char uint8_t
Definition: stdint.h:10
Serial port configuration.
struct init_fn serial_console_init_fn __init_fn(INIT_CONSOLE)
Serial console initialisation function.
static void serial_shutdown(int flags __unused)
Shut down serial console.
Definition: serial.c:179
Console configuration.
static int serial_getchar(void)
Get character from serial console.
Definition: serial.c:107
#define INIT_CONSOLE
Console initialisation.
Definition: init.h:30
PROVIDE_SERIAL(fixed, default_serial_console, fixed_serial_console)
uint8_t data[48]
Additional event data.
Definition: ena.h:22
#define CONSOLE_SERIAL
Definition: serial.c:45
A console driver.
Definition: console.h:55
#define COMSPEED
Definition: serial.c:61
#define COMCONSOLE
Definition: serial.c:56
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
static void serial_putchar(int character)
Print a character to serial console.
Definition: serial.c:92
String functions.