[init] Show startup and shutdown function names in debug messages
[ipxe.git] / src / interface / linux / linux_console.c
1 /*
2 * Copyright (C) 2010 Piotr JaroszyƄski <p.jaroszynski@gmail.com>
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 St, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 FILE_LICENCE(GPL2_OR_LATER);
20
21 /** @file
22 *
23 * Linux console implementation.
24 *
25 */
26
27 #include <ipxe/console.h>
28
29 #include <ipxe/init.h>
30 #include <ipxe/keys.h>
31 #include <linux_api.h>
32
33 #include <linux/termios.h>
34 #include <asm/errno.h>
35
36 #include <config/console.h>
37
38 /* Set default console usage if applicable */
39 #if ! ( defined ( CONSOLE_LINUX ) && CONSOLE_EXPLICIT ( CONSOLE_LINUX ) )
40 #undef CONSOLE_LINUX
41 #define CONSOLE_LINUX ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
42 #endif
43
44 static void linux_console_putchar(int c)
45 {
46 /* write to stdout */
47 if (linux_write(1, &c, 1) != 1)
48 DBG("linux_console write failed (%s)\n", linux_strerror(linux_errno));
49 }
50
51 static int linux_console_getchar()
52 {
53 char c;
54
55 /* read from stdin */
56 if (linux_read(0, &c, 1) < 0) {
57 DBG("linux_console read failed (%s)\n", linux_strerror(linux_errno));
58 return 0;
59 }
60 /* backspace seems to be returned as ascii del, map it here */
61 if (c == 0x7f)
62 return KEY_BACKSPACE;
63 else
64 return c;
65 }
66
67 static int linux_console_iskey()
68 {
69 struct pollfd pfd;
70 pfd.fd = 0;
71 pfd.events = POLLIN;
72
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));
76 return 0;
77 }
78
79 if (pfd.revents & POLLIN)
80 return 1;
81 else
82 return 0;
83 }
84
85 struct console_driver linux_console __console_driver = {
86 .disabled = 0,
87 .putchar = linux_console_putchar,
88 .getchar = linux_console_getchar,
89 .iskey = linux_console_iskey,
90 .usage = CONSOLE_LINUX,
91 };
92
93 static int linux_tcgetattr(int fd, struct termios *termios_p)
94 {
95 return linux_ioctl(fd, TCGETS, termios_p);
96 }
97
98 static int linux_tcsetattr(int fd, int optional_actions, const struct termios *termios_p)
99 {
100 unsigned long int cmd;
101
102 switch (optional_actions)
103 {
104 case TCSANOW:
105 cmd = TCSETS;
106 break;
107 case TCSADRAIN:
108 cmd = TCSETSW;
109 break;
110 case TCSAFLUSH:
111 cmd = TCSETSF;
112 break;
113 default:
114 linux_errno = EINVAL;
115 return -1;
116 }
117
118 return linux_ioctl(fd, cmd, termios_p);
119 }
120
121 /** Saved termios attributes */
122 static struct termios saved_termios;
123
124 /** Setup the terminal for our use */
125 static void linux_console_startup(void)
126 {
127 struct termios t;
128
129 if (linux_tcgetattr(0, &t)) {
130 DBG("linux_console tcgetattr failed (%s)", linux_strerror(linux_errno));
131 return;
132 }
133
134 saved_termios = t;
135
136 /* Disable canonical mode and echo. Let readline handle that */
137 t.c_lflag &= ~(ECHO | ICANON);
138 /* stop ^C from sending a signal */
139 t.c_cc[VINTR] = 0;
140
141 if (linux_tcsetattr(0, TCSAFLUSH, &t))
142 DBG("linux_console tcsetattr failed (%s)", linux_strerror(linux_errno));
143 }
144
145 /** Restores original terminal attributes on shutdown */
146 static void linux_console_shutdown(int flags __unused)
147 {
148 if (linux_tcsetattr(0, TCSAFLUSH, &saved_termios))
149 DBG("linux_console tcsetattr failed (%s)", linux_strerror(linux_errno));
150 }
151
152 struct startup_fn linux_console_startup_fn __startup_fn(STARTUP_EARLY) = {
153 .name = "linux_console",
154 .startup = linux_console_startup,
155 .shutdown = linux_console_shutdown,
156 };