2 * Copyright (C) 2010 Piotr JaroszyĆski <p.jaroszynski@gmail.com>
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.
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.
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 St, Fifth Floor, Boston, MA 02110-1301 USA.
19 FILE_LICENCE(GPL2_OR_LATER);
23 * Linux console implementation.
27 #include <ipxe/console.h>
29 #include <ipxe/init.h>
30 #include <ipxe/keys.h>
31 #include <linux_api.h>
33 #include <linux/termios.h>
34 #include <asm/errno.h>
36 #include <config/console.h>
38 /* Set default console usage if applicable */
39 #if ! ( defined ( CONSOLE_LINUX ) && CONSOLE_EXPLICIT ( CONSOLE_LINUX ) )
41 #define CONSOLE_LINUX ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
44 static void linux_console_putchar(int c)
47 if (linux_write(1, &c, 1) != 1)
48 DBG("linux_console write failed (%s)\n", linux_strerror(linux_errno));
51 static int linux_console_getchar()
56 if (linux_read(0, &c, 1) < 0) {
57 DBG("linux_console read failed (%s)\n", linux_strerror(linux_errno));
60 /* backspace seems to be returned as ascii del, map it here */
67 static int linux_console_iskey()
73 /* poll for data to be read on stdin */
74 if (linux_poll(&pfd, 1, 0) == -1) {
75 DBG("linux_console poll failed (%s)\n", linux_strerror(linux_errno));
79 if (pfd.revents & POLLIN)
85 struct console_driver linux_console __console_driver = {
87 .putchar = linux_console_putchar,
88 .getchar = linux_console_getchar,
89 .iskey = linux_console_iskey,
90 .usage = CONSOLE_LINUX,
93 static int linux_tcgetattr(int fd, struct termios *termios_p)
95 return linux_ioctl(fd, TCGETS, termios_p);
98 static int linux_tcsetattr(int fd, int optional_actions, const struct termios *termios_p)
100 unsigned long int cmd;
102 switch (optional_actions)
114 linux_errno = EINVAL;
118 return linux_ioctl(fd, cmd, termios_p);
121 /** Saved termios attributes */
122 static struct termios saved_termios;
124 /** Setup the terminal for our use */
125 static void linux_console_startup(void)
129 if (linux_tcgetattr(0, &t)) {
130 DBG("linux_console tcgetattr failed (%s)", linux_strerror(linux_errno));
136 /* Disable canonical mode and echo. Let readline handle that */
137 t.c_lflag &= ~(ECHO | ICANON);
138 /* stop ^C from sending a signal */
141 if (linux_tcsetattr(0, TCSAFLUSH, &t))
142 DBG("linux_console tcsetattr failed (%s)", linux_strerror(linux_errno));
145 /** Restores original terminal attributes on shutdown */
146 static void linux_console_shutdown(int flags __unused)
148 if (linux_tcsetattr(0, TCSAFLUSH, &saved_termios))
149 DBG("linux_console tcsetattr failed (%s)", linux_strerror(linux_errno));
152 struct startup_fn linux_console_startup_fn __startup_fn(STARTUP_EARLY) = {
153 .startup = linux_console_startup,
154 .shutdown = linux_console_shutdown,