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
24FILE_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 */
76struct 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 */
92static 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 */
107static int serial_getchar ( void ) {
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 */
134static 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 */
145struct console_driver serial_console_driver __console_driver = {
146 .putchar = serial_putchar,
147 .getchar = serial_getchar,
148 .iskey = serial_iskey,
149 .usage = CONSOLE_SERIAL,
150};
151
152/** Initialise serial console */
153static 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 */
179static 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 */
192struct init_fn serial_console_init_fn __init_fn ( INIT_CONSOLE ) = {
193 .name = "serial",
194 .initialise = serial_init,
195};
196
197/** Serial console startup function */
198struct startup_fn serial_startup_fn __startup_fn ( STARTUP_EARLY ) = {
199 .name = "serial",
200 .shutdown = serial_shutdown,
201};
202
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
unsigned char uint8_t
Definition stdint.h:10
Console configuration.
Serial port configuration.
#define COMCONSOLE
Definition serial.h:18
#define COMSPEED
Definition serial.h:26
struct eltorito_descriptor_fixed fixed
Fixed portion.
Definition eltorito.h:1
uint8_t data[48]
Additional event data.
Definition ena.h:11
uint8_t flags
Flags.
Definition ena.h:7
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:573
#define DBGC(...)
Definition compiler.h:505
#define INIT_CONSOLE
Console initialisation.
Definition init.h:31
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define STARTUP_EARLY
Early startup.
Definition init.h:64
User interaction.
#define __console_driver
Mark a struct console_driver as being part of the console drivers table.
Definition console.h:134
16550-compatible UART
Serial console.
#define PROVIDE_SERIAL_INLINE(_subsys, _api_func)
Provide a static inline serial API implementation.
Definition serial.h:48
struct uart * default_serial_console(void)
Get serial console UART.
#define PROVIDE_SERIAL(_subsys, _api_func, _func)
Provide a serial API implementation.
Definition serial.h:39
String functions.
#define __init_fn(init_order)
Declare an initialisation functon.
Definition init.h:24
#define __startup_fn(startup_order)
Declare a startup/shutdown function.
Definition init.h:53
static void serial_putchar(int character)
Print a character to serial console.
Definition serial.c:92
static int serial_iskey(void)
Check for character ready to read from serial console.
Definition serial.c:134
static int serial_getchar(void)
Get character from serial console.
Definition serial.c:107
struct uart * serial_console
Active serial console UART.
Definition serial.c:69
static void serial_shutdown(int flags __unused)
Shut down serial console.
Definition serial.c:179
#define CONSOLE_SERIAL
Definition serial.c:45
struct uart * fixed_serial_console(void)
Get fixed serial console UART.
Definition serial.c:76
static void serial_init(void)
Initialise serial console.
Definition serial.c:153
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
A console driver.
Definition console.h:56
An initialisation function.
Definition init.h:15
A startup/shutdown function.
Definition init.h:43
A generic UART.
Definition uart.h:17
const char * name
Name.
Definition uart.h:21
unsigned int baud
Baud rate (if specified)
Definition uart.h:26
Generic UART.