Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

linux_console.c 3.4KB

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