source: grub-pc/trunk/fuentes/grub-core/term/arc/console.c @ 22

Last change on this file since 22 was 22, checked in by mabarracus, 4 years ago

updated version and apply net.ifnames=0 into debian/rules

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