console.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2011 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/arc/arc.h>
  19. #include <grub/arc/console.h>
  20. #include <grub/term.h>
  21. #include <grub/terminfo.h>
  22. /* FIXME: use unicode. */
  23. static int
  24. readkey (struct grub_term_input *term __attribute__ ((unused)))
  25. {
  26. unsigned long count;
  27. char chr;
  28. if (GRUB_ARC_FIRMWARE_VECTOR->get_read_status (GRUB_ARC_STDIN))
  29. return -1;
  30. if (GRUB_ARC_FIRMWARE_VECTOR->read (GRUB_ARC_STDIN, &chr, 1, &count))
  31. return -1;
  32. if (!count)
  33. return -1;
  34. return chr;
  35. }
  36. static void
  37. put (struct grub_term_output *term __attribute__ ((unused)), const int c)
  38. {
  39. unsigned long count;
  40. char chr = c;
  41. GRUB_ARC_FIRMWARE_VECTOR->write (GRUB_ARC_STDOUT, &chr, 1, &count);
  42. }
  43. static struct grub_terminfo_output_state grub_console_terminfo_output;
  44. int
  45. grub_arc_is_device_serial (const char *name, int alt_names)
  46. {
  47. if (name[0] == '\0')
  48. return 0;
  49. const char *ptr = name + grub_strlen (name) - 1;
  50. int i;
  51. /*
  52. Recognize:
  53. serial(N)
  54. serial(N)line(M)
  55. */
  56. for (i = 0; i < 2; i++)
  57. {
  58. if (!alt_names)
  59. {
  60. if (*ptr != ')')
  61. return 0;
  62. ptr--;
  63. }
  64. for (; ptr >= name && grub_isdigit (*ptr); ptr--);
  65. if (ptr < name)
  66. return 0;
  67. if (!alt_names)
  68. {
  69. if (*ptr != '(')
  70. return 0;
  71. ptr--;
  72. }
  73. if (ptr + 1 >= name + sizeof ("serial") - 1
  74. && grub_memcmp (ptr + 1 - (sizeof ("serial") - 1),
  75. "serial", sizeof ("serial") - 1) == 0)
  76. return 1;
  77. if (!(ptr + 1 >= name + sizeof ("line") - 1
  78. && grub_memcmp (ptr + 1 - (sizeof ("line") - 1),
  79. "line", sizeof ("line") - 1) == 0))
  80. return 0;
  81. ptr -= sizeof ("line") - 1;
  82. if (alt_names)
  83. {
  84. if (*ptr != '/')
  85. return 0;
  86. ptr--;
  87. }
  88. }
  89. return 0;
  90. }
  91. static int
  92. check_is_serial (void)
  93. {
  94. static int is_serial = -1;
  95. if (is_serial != -1)
  96. return is_serial;
  97. const char *consout = 0;
  98. /* Check for serial. It works unless user manually overrides ConsoleOut
  99. variable. If he does there is nothing we can do. Fortunately failure
  100. isn't critical.
  101. */
  102. if (GRUB_ARC_SYSTEM_PARAMETER_BLOCK->firmware_vector_length
  103. >= (unsigned) ((char *) (&GRUB_ARC_FIRMWARE_VECTOR->getenvironmentvariable + 1)
  104. - (char *) GRUB_ARC_FIRMWARE_VECTOR)
  105. && GRUB_ARC_FIRMWARE_VECTOR->getenvironmentvariable)
  106. consout = GRUB_ARC_FIRMWARE_VECTOR->getenvironmentvariable ("ConsoleOut");
  107. if (!consout)
  108. return is_serial = 0;
  109. return is_serial = grub_arc_is_device_serial (consout, 0);
  110. }
  111. static void
  112. set_console_dimensions (void)
  113. {
  114. struct grub_arc_display_status *info = NULL;
  115. if (check_is_serial ())
  116. {
  117. grub_console_terminfo_output.size.x = 80;
  118. grub_console_terminfo_output.size.y = 24;
  119. return;
  120. }
  121. if (GRUB_ARC_SYSTEM_PARAMETER_BLOCK->firmware_vector_length
  122. >= (unsigned) ((char *) (&GRUB_ARC_FIRMWARE_VECTOR->getdisplaystatus + 1)
  123. - (char *) GRUB_ARC_FIRMWARE_VECTOR)
  124. && GRUB_ARC_FIRMWARE_VECTOR->getdisplaystatus)
  125. info = GRUB_ARC_FIRMWARE_VECTOR->getdisplaystatus (GRUB_ARC_STDOUT);
  126. if (info)
  127. {
  128. grub_console_terminfo_output.size.x = info->w + 1;
  129. grub_console_terminfo_output.size.y = info->h + 1;
  130. }
  131. }
  132. static grub_err_t
  133. grub_console_init_output (struct grub_term_output *term)
  134. {
  135. set_console_dimensions ();
  136. grub_terminfo_output_init (term);
  137. return 0;
  138. }
  139. static struct grub_terminfo_input_state grub_console_terminfo_input =
  140. {
  141. .readkey = readkey
  142. };
  143. static struct grub_terminfo_output_state grub_console_terminfo_output =
  144. {
  145. .put = put,
  146. .size = { 80, 20 }
  147. };
  148. static struct grub_term_input grub_console_term_input =
  149. {
  150. .name = "console",
  151. .init = grub_terminfo_input_init,
  152. .getkey = grub_terminfo_getkey,
  153. .data = &grub_console_terminfo_input
  154. };
  155. static struct grub_term_output grub_console_term_output =
  156. {
  157. .name = "console",
  158. .init = grub_console_init_output,
  159. .putchar = grub_terminfo_putchar,
  160. .getxy = grub_terminfo_getxy,
  161. .getwh = grub_terminfo_getwh,
  162. .gotoxy = grub_terminfo_gotoxy,
  163. .cls = grub_terminfo_cls,
  164. .setcolorstate = grub_terminfo_setcolorstate,
  165. .setcursor = grub_terminfo_setcursor,
  166. .flags = GRUB_TERM_CODE_TYPE_ASCII,
  167. .data = &grub_console_terminfo_output,
  168. .progress_update_divisor = GRUB_PROGRESS_FAST
  169. };
  170. void
  171. grub_console_init_early (void)
  172. {
  173. grub_term_register_input ("console", &grub_console_term_input);
  174. grub_term_register_output ("console", &grub_console_term_output);
  175. }
  176. void
  177. grub_console_init_lately (void)
  178. {
  179. grub_terminfo_init ();
  180. if (check_is_serial ())
  181. grub_terminfo_output_register (&grub_console_term_output, "vt100");
  182. else
  183. grub_terminfo_output_register (&grub_console_term_output, "arc");
  184. }