You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

linux_console.c 3.7KB

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