serial.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2006,2007,2008,2012 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/disk.h>
  19. #include <grub/partition.h>
  20. #include <grub/mm.h>
  21. #include <grub/types.h>
  22. #include <grub/misc.h>
  23. #include <grub/err.h>
  24. #include <grub/term.h>
  25. #include <grub/efi/api.h>
  26. #include <grub/efi/efi.h>
  27. #include <grub/efi/disk.h>
  28. #include <grub/serial.h>
  29. #include <grub/types.h>
  30. #include <grub/i18n.h>
  31. /* GUID. */
  32. static grub_efi_guid_t serial_io_guid = GRUB_EFI_SERIAL_IO_GUID;
  33. static void
  34. do_real_config (struct grub_serial_port *port)
  35. {
  36. grub_efi_status_t status = GRUB_EFI_SUCCESS;
  37. const grub_efi_parity_type_t parities[] = {
  38. [GRUB_SERIAL_PARITY_NONE] = GRUB_EFI_SERIAL_NO_PARITY,
  39. [GRUB_SERIAL_PARITY_ODD] = GRUB_EFI_SERIAL_ODD_PARITY,
  40. [GRUB_SERIAL_PARITY_EVEN] = GRUB_EFI_SERIAL_EVEN_PARITY
  41. };
  42. const grub_efi_stop_bits_t stop_bits[] = {
  43. [GRUB_SERIAL_STOP_BITS_1] = GRUB_EFI_SERIAL_1_STOP_BIT,
  44. [GRUB_SERIAL_STOP_BITS_1_5] = GRUB_EFI_SERIAL_1_5_STOP_BITS,
  45. [GRUB_SERIAL_STOP_BITS_2] = GRUB_EFI_SERIAL_2_STOP_BITS,
  46. };
  47. if (port->configured)
  48. return;
  49. status = efi_call_7 (port->interface->set_attributes, port->interface,
  50. port->config.speed,
  51. 0, 0, parities[port->config.parity],
  52. port->config.word_len,
  53. stop_bits[port->config.stop_bits]);
  54. if (status != GRUB_EFI_SUCCESS)
  55. port->broken = 1;
  56. status = efi_call_2 (port->interface->set_control_bits, port->interface,
  57. port->config.rtscts ? 0x4002 : 0x2);
  58. port->configured = 1;
  59. }
  60. /* Fetch a key. */
  61. static int
  62. serial_hw_fetch (struct grub_serial_port *port)
  63. {
  64. grub_efi_uintn_t bufsize = 1;
  65. char c;
  66. grub_efi_status_t status = GRUB_EFI_SUCCESS;
  67. do_real_config (port);
  68. if (port->broken)
  69. return -1;
  70. status = efi_call_3 (port->interface->read, port->interface, &bufsize, &c);
  71. if (status != GRUB_EFI_SUCCESS || bufsize == 0)
  72. return -1;
  73. return c;
  74. }
  75. /* Put a character. */
  76. static void
  77. serial_hw_put (struct grub_serial_port *port, const int c)
  78. {
  79. grub_efi_uintn_t bufsize = 1;
  80. char c0 = c;
  81. do_real_config (port);
  82. if (port->broken)
  83. return;
  84. efi_call_3 (port->interface->write, port->interface, &bufsize, &c0);
  85. }
  86. /* Initialize a serial device. PORT is the port number for a serial device.
  87. SPEED is a DTE-DTE speed which must be one of these: 2400, 4800, 9600,
  88. 19200, 38400, 57600 and 115200. WORD_LEN is the word length to be used
  89. for the device. Likewise, PARITY is the type of the parity and
  90. STOP_BIT_LEN is the length of the stop bit. The possible values for
  91. WORD_LEN, PARITY and STOP_BIT_LEN are defined in the header file as
  92. macros. */
  93. static grub_err_t
  94. serial_hw_configure (struct grub_serial_port *port,
  95. struct grub_serial_config *config)
  96. {
  97. if (config->parity != GRUB_SERIAL_PARITY_NONE
  98. && config->parity != GRUB_SERIAL_PARITY_ODD
  99. && config->parity != GRUB_SERIAL_PARITY_EVEN)
  100. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  101. N_("unsupported serial port parity"));
  102. if (config->stop_bits != GRUB_SERIAL_STOP_BITS_1
  103. && config->stop_bits != GRUB_SERIAL_STOP_BITS_1_5
  104. && config->stop_bits != GRUB_SERIAL_STOP_BITS_2)
  105. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  106. N_("unsupported serial port stop bits number"));
  107. if (config->word_len < 5 || config->word_len > 8)
  108. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  109. N_("unsupported serial port word length"));
  110. port->config = *config;
  111. port->configured = 0;
  112. /* FIXME: should check if the serial terminal was found. */
  113. return GRUB_ERR_NONE;
  114. }
  115. struct grub_serial_driver grub_efiserial_driver =
  116. {
  117. .configure = serial_hw_configure,
  118. .fetch = serial_hw_fetch,
  119. .put = serial_hw_put
  120. };
  121. void
  122. grub_efiserial_init (void)
  123. {
  124. grub_efi_uintn_t num_handles;
  125. grub_efi_handle_t *handles;
  126. grub_efi_handle_t *handle;
  127. int num_serial = 0;
  128. grub_err_t err;
  129. /* Find handles which support the disk io interface. */
  130. handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL, &serial_io_guid,
  131. 0, &num_handles);
  132. if (! handles)
  133. return;
  134. /* Make a linked list of devices. */
  135. for (handle = handles; num_handles--; handle++)
  136. {
  137. struct grub_serial_port *port;
  138. struct grub_efi_serial_io_interface *sio;
  139. sio = grub_efi_open_protocol (*handle, &serial_io_guid,
  140. GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  141. if (! sio)
  142. /* This should not happen... Why? */
  143. continue;
  144. port = grub_zalloc (sizeof (*port));
  145. if (!port)
  146. return;
  147. port->name = grub_malloc (sizeof ("efiXXXXXXXXXXXXXXXXXXXX"));
  148. if (!port->name)
  149. {
  150. grub_free (port);
  151. return;
  152. }
  153. grub_snprintf (port->name, sizeof ("efiXXXXXXXXXXXXXXXXXXXX"),
  154. "efi%d", num_serial++);
  155. port->driver = &grub_efiserial_driver;
  156. port->interface = sio;
  157. err = grub_serial_config_defaults (port);
  158. if (err)
  159. grub_print_error ();
  160. grub_serial_register (port);
  161. }
  162. grub_free (handles);
  163. return;
  164. }