console.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/term.h>
  19. #include <grub/types.h>
  20. #include <grub/misc.h>
  21. #include <grub/mm.h>
  22. #include <grub/time.h>
  23. #include <grub/terminfo.h>
  24. #include <grub/xen.h>
  25. static int
  26. readkey (struct grub_term_input *term __attribute__ ((unused)))
  27. {
  28. grub_size_t prod, cons;
  29. int r;
  30. mb ();
  31. prod = grub_xen_xcons->in_prod;
  32. cons = grub_xen_xcons->in_cons;
  33. if (prod <= cons)
  34. return -1;
  35. r = grub_xen_xcons->in[cons];
  36. cons++;
  37. mb ();
  38. grub_xen_xcons->in_cons = cons;
  39. return r;
  40. }
  41. static int signal_sent = 1;
  42. static void
  43. refresh (struct grub_term_output *term __attribute__ ((unused)))
  44. {
  45. struct evtchn_send send;
  46. send.port = grub_xen_start_page_addr->console.domU.evtchn;
  47. grub_xen_event_channel_op (EVTCHNOP_send, &send);
  48. signal_sent = 1;
  49. while (grub_xen_xcons->out_prod != grub_xen_xcons->out_cons)
  50. {
  51. grub_xen_sched_op (SCHEDOP_yield, 0);
  52. }
  53. }
  54. static void
  55. put (struct grub_term_output *term __attribute__ ((unused)), const int c)
  56. {
  57. grub_size_t prod, cons;
  58. while (1)
  59. {
  60. mb ();
  61. prod = grub_xen_xcons->out_prod;
  62. cons = grub_xen_xcons->out_cons;
  63. if (prod < cons + sizeof (grub_xen_xcons->out))
  64. break;
  65. if (!signal_sent)
  66. refresh (term);
  67. grub_xen_sched_op (SCHEDOP_yield, 0);
  68. }
  69. grub_xen_xcons->out[prod++ & (sizeof (grub_xen_xcons->out) - 1)] = c;
  70. mb ();
  71. grub_xen_xcons->out_prod = prod;
  72. signal_sent = 0;
  73. }
  74. struct grub_terminfo_input_state grub_console_terminfo_input = {
  75. .readkey = readkey
  76. };
  77. struct grub_terminfo_output_state grub_console_terminfo_output = {
  78. .put = put,
  79. .size = {80, 24}
  80. };
  81. static struct grub_term_input grub_console_term_input = {
  82. .name = "console",
  83. .init = 0,
  84. .getkey = grub_terminfo_getkey,
  85. .data = &grub_console_terminfo_input
  86. };
  87. static struct grub_term_output grub_console_term_output = {
  88. .name = "console",
  89. .init = 0,
  90. .putchar = grub_terminfo_putchar,
  91. .getxy = grub_terminfo_getxy,
  92. .getwh = grub_terminfo_getwh,
  93. .gotoxy = grub_terminfo_gotoxy,
  94. .cls = grub_terminfo_cls,
  95. .refresh = refresh,
  96. .setcolorstate = grub_terminfo_setcolorstate,
  97. .setcursor = grub_terminfo_setcursor,
  98. .flags = GRUB_TERM_CODE_TYPE_ASCII,
  99. .data = &grub_console_terminfo_output,
  100. };
  101. void
  102. grub_console_init (void)
  103. {
  104. grub_term_register_input ("console", &grub_console_term_input);
  105. grub_term_register_output ("console", &grub_console_term_output);
  106. grub_terminfo_init ();
  107. grub_terminfo_output_register (&grub_console_term_output, "vt100-color");
  108. }