]> git.proxmox.com Git - grub2.git/blob - grub-core/term/serial.c
Import grub2_2.02+dfsg1.orig.tar.xz
[grub2.git] / grub-core / term / serial.c
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 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/serial.h>
20 #include <grub/term.h>
21 #include <grub/types.h>
22 #include <grub/dl.h>
23 #include <grub/misc.h>
24 #include <grub/terminfo.h>
25 #if !defined (GRUB_MACHINE_EMU) && (defined(__mips__) || defined (__i386__) || defined (__x86_64__))
26 #include <grub/cpu/io.h>
27 #endif
28 #include <grub/extcmd.h>
29 #include <grub/i18n.h>
30 #include <grub/list.h>
31 #ifdef GRUB_MACHINE_MIPS_LOONGSON
32 #include <grub/machine/kernel.h>
33 #endif
34 #ifdef GRUB_MACHINE_IEEE1275
35 #include <grub/ieee1275/console.h>
36 #endif
37
38 GRUB_MOD_LICENSE ("GPLv3+");
39
40 #define FOR_SERIAL_PORTS(var) FOR_LIST_ELEMENTS((var), (grub_serial_ports))
41
42 enum
43 {
44 OPTION_UNIT,
45 OPTION_PORT,
46 OPTION_SPEED,
47 OPTION_WORD,
48 OPTION_PARITY,
49 OPTION_STOP,
50 OPTION_BASE_CLOCK,
51 OPTION_RTSCTS
52 };
53
54 /* Argument options. */
55 static const struct grub_arg_option options[] =
56 {
57 {"unit", 'u', 0, N_("Set the serial unit."), 0, ARG_TYPE_INT},
58 {"port", 'p', 0, N_("Set the serial port address."), 0, ARG_TYPE_STRING},
59 {"speed", 's', 0, N_("Set the serial port speed."), 0, ARG_TYPE_INT},
60 {"word", 'w', 0, N_("Set the serial port word length."), 0, ARG_TYPE_INT},
61 {"parity", 'r', 0, N_("Set the serial port parity."), 0, ARG_TYPE_STRING},
62 {"stop", 't', 0, N_("Set the serial port stop bits."), 0, ARG_TYPE_INT},
63 {"base-clock", 'b', 0, N_("Set the base frequency."), 0, ARG_TYPE_STRING},
64 {"rtscts", 'f', 0, N_("Enable/disable RTS/CTS."), "on|off", ARG_TYPE_STRING},
65 {0, 0, 0, 0, 0, 0}
66 };
67
68 static struct grub_serial_port *grub_serial_ports;
69
70 struct grub_serial_output_state
71 {
72 struct grub_terminfo_output_state tinfo;
73 struct grub_serial_port *port;
74 };
75
76 struct grub_serial_input_state
77 {
78 struct grub_terminfo_input_state tinfo;
79 struct grub_serial_port *port;
80 };
81
82 static void
83 serial_put (grub_term_output_t term, const int c)
84 {
85 struct grub_serial_output_state *data = term->data;
86 data->port->driver->put (data->port, c);
87 }
88
89 static int
90 serial_fetch (grub_term_input_t term)
91 {
92 struct grub_serial_input_state *data = term->data;
93 return data->port->driver->fetch (data->port);
94 }
95
96 static const struct grub_serial_input_state grub_serial_terminfo_input_template =
97 {
98 .tinfo =
99 {
100 .readkey = serial_fetch
101 }
102 };
103
104 static const struct grub_serial_output_state grub_serial_terminfo_output_template =
105 {
106 .tinfo =
107 {
108 .put = serial_put,
109 .size = { 80, 24 }
110 }
111 };
112
113 static struct grub_serial_input_state grub_serial_terminfo_input;
114
115 static struct grub_serial_output_state grub_serial_terminfo_output;
116
117 static int registered = 0;
118
119 static struct grub_term_input grub_serial_term_input =
120 {
121 .name = "serial",
122 .init = grub_terminfo_input_init,
123 .getkey = grub_terminfo_getkey,
124 .data = &grub_serial_terminfo_input
125 };
126
127 static struct grub_term_output grub_serial_term_output =
128 {
129 .name = "serial",
130 .init = grub_terminfo_output_init,
131 .putchar = grub_terminfo_putchar,
132 .getwh = grub_terminfo_getwh,
133 .getxy = grub_terminfo_getxy,
134 .gotoxy = grub_terminfo_gotoxy,
135 .cls = grub_terminfo_cls,
136 .setcolorstate = grub_terminfo_setcolorstate,
137 .setcursor = grub_terminfo_setcursor,
138 .flags = GRUB_TERM_CODE_TYPE_ASCII,
139 .data = &grub_serial_terminfo_output,
140 .progress_update_divisor = GRUB_PROGRESS_SLOW
141 };
142
143 \f
144
145 struct grub_serial_port *
146 grub_serial_find (const char *name)
147 {
148 struct grub_serial_port *port;
149
150 FOR_SERIAL_PORTS (port)
151 if (grub_strcmp (port->name, name) == 0)
152 break;
153
154 #if (defined(__mips__) || defined (__i386__) || defined (__x86_64__)) && !defined(GRUB_MACHINE_EMU) && !defined(GRUB_MACHINE_ARC)
155 if (!port && grub_memcmp (name, "port", sizeof ("port") - 1) == 0
156 && grub_isxdigit (name [sizeof ("port") - 1]))
157 {
158 name = grub_serial_ns8250_add_port (grub_strtoul (&name[sizeof ("port") - 1],
159 0, 16));
160 if (!name)
161 return NULL;
162
163 FOR_SERIAL_PORTS (port)
164 if (grub_strcmp (port->name, name) == 0)
165 break;
166 }
167 #endif
168
169 #ifdef GRUB_MACHINE_IEEE1275
170 if (!port && grub_memcmp (name, "ieee1275/", sizeof ("ieee1275/") - 1) == 0)
171 {
172 name = grub_ofserial_add_port (&name[sizeof ("ieee1275/") - 1]);
173 if (!name)
174 return NULL;
175
176 FOR_SERIAL_PORTS (port)
177 if (grub_strcmp (port->name, name) == 0)
178 break;
179 }
180 #endif
181
182 return port;
183 }
184
185 static grub_err_t
186 grub_cmd_serial (grub_extcmd_context_t ctxt, int argc, char **args)
187 {
188 struct grub_arg_list *state = ctxt->state;
189 char pname[40];
190 const char *name = NULL;
191 struct grub_serial_port *port;
192 struct grub_serial_config config;
193 grub_err_t err;
194
195 if (state[OPTION_UNIT].set)
196 {
197 grub_snprintf (pname, sizeof (pname), "com%ld",
198 grub_strtoul (state[0].arg, 0, 0));
199 name = pname;
200 }
201
202 if (state[OPTION_PORT].set)
203 {
204 grub_snprintf (pname, sizeof (pname), "port%lx",
205 grub_strtoul (state[1].arg, 0, 0));
206 name = pname;
207 }
208
209 if (argc >= 1)
210 name = args[0];
211
212 if (!name)
213 name = "com0";
214
215 port = grub_serial_find (name);
216 if (!port)
217 return grub_error (GRUB_ERR_BAD_ARGUMENT,
218 N_("serial port `%s' isn't found"),
219 name);
220
221 config = port->config;
222
223 if (state[OPTION_SPEED].set) {
224 config.speed = grub_strtoul (state[OPTION_SPEED].arg, 0, 0);
225 if (config.speed == 0)
226 return grub_error (GRUB_ERR_BAD_ARGUMENT,
227 N_("unsupported serial port parity"));
228 }
229
230 if (state[OPTION_WORD].set)
231 config.word_len = grub_strtoul (state[OPTION_WORD].arg, 0, 0);
232
233 if (state[OPTION_PARITY].set)
234 {
235 if (! grub_strcmp (state[OPTION_PARITY].arg, "no"))
236 config.parity = GRUB_SERIAL_PARITY_NONE;
237 else if (! grub_strcmp (state[OPTION_PARITY].arg, "odd"))
238 config.parity = GRUB_SERIAL_PARITY_ODD;
239 else if (! grub_strcmp (state[OPTION_PARITY].arg, "even"))
240 config.parity = GRUB_SERIAL_PARITY_EVEN;
241 else
242 return grub_error (GRUB_ERR_BAD_ARGUMENT,
243 N_("unsupported serial port parity"));
244 }
245
246 if (state[OPTION_RTSCTS].set)
247 {
248 if (grub_strcmp (state[OPTION_RTSCTS].arg, "on") == 0)
249 config.rtscts = 1;
250 else if (grub_strcmp (state[OPTION_RTSCTS].arg, "off") == 0)
251 config.rtscts = 0;
252 else
253 return grub_error (GRUB_ERR_BAD_ARGUMENT,
254 N_("unsupported serial port flow control"));
255 }
256
257 if (state[OPTION_STOP].set)
258 {
259 if (! grub_strcmp (state[OPTION_STOP].arg, "1"))
260 config.stop_bits = GRUB_SERIAL_STOP_BITS_1;
261 else if (! grub_strcmp (state[OPTION_STOP].arg, "2"))
262 config.stop_bits = GRUB_SERIAL_STOP_BITS_2;
263 else if (! grub_strcmp (state[OPTION_STOP].arg, "1.5"))
264 config.stop_bits = GRUB_SERIAL_STOP_BITS_1_5;
265 else
266 return grub_error (GRUB_ERR_BAD_ARGUMENT,
267 N_("unsupported serial port stop bits number"));
268 }
269
270 if (state[OPTION_BASE_CLOCK].set)
271 {
272 char *ptr;
273 config.base_clock = grub_strtoull (state[OPTION_BASE_CLOCK].arg, &ptr, 0);
274 if (grub_errno)
275 return grub_errno;
276 if (ptr && *ptr == 'M')
277 config.base_clock *= 1000000;
278 if (ptr && (*ptr == 'k' || *ptr == 'K'))
279 config.base_clock *= 1000;
280 }
281
282 if (config.speed == 0)
283 config.speed = 9600;
284
285 /* Initialize with new settings. */
286 err = port->driver->configure (port, &config);
287 if (err)
288 return err;
289 #if !defined (GRUB_MACHINE_EMU) && !defined(GRUB_MACHINE_ARC) && (defined(__mips__) || defined (__i386__) || defined (__x86_64__))
290
291 /* Compatibility kludge. */
292 if (port->driver == &grub_ns8250_driver)
293 {
294 if (!registered)
295 {
296 grub_terminfo_output_register (&grub_serial_term_output, "vt100");
297
298 grub_term_register_input ("serial", &grub_serial_term_input);
299 grub_term_register_output ("serial", &grub_serial_term_output);
300 }
301 grub_serial_terminfo_output.port = port;
302 grub_serial_terminfo_input.port = port;
303 registered = 1;
304 }
305 #endif
306 return GRUB_ERR_NONE;
307 }
308
309 #ifdef GRUB_MACHINE_MIPS_LOONGSON
310 const char loongson_defserial[][6] =
311 {
312 [GRUB_ARCH_MACHINE_YEELOONG] = "com0",
313 [GRUB_ARCH_MACHINE_FULOONG2F] = "com2",
314 [GRUB_ARCH_MACHINE_FULOONG2E] = "com1"
315 };
316 #endif
317
318 grub_err_t
319 grub_serial_register (struct grub_serial_port *port)
320 {
321 struct grub_term_input *in;
322 struct grub_term_output *out;
323 struct grub_serial_input_state *indata;
324 struct grub_serial_output_state *outdata;
325
326 in = grub_malloc (sizeof (*in));
327 if (!in)
328 return grub_errno;
329
330 indata = grub_malloc (sizeof (*indata));
331 if (!indata)
332 {
333 grub_free (in);
334 return grub_errno;
335 }
336
337 grub_memcpy (in, &grub_serial_term_input, sizeof (*in));
338 in->data = indata;
339 in->name = grub_xasprintf ("serial_%s", port->name);
340 grub_memcpy (indata, &grub_serial_terminfo_input, sizeof (*indata));
341
342 if (!in->name)
343 {
344 grub_free (in);
345 grub_free (indata);
346 return grub_errno;
347 }
348
349 out = grub_zalloc (sizeof (*out));
350 if (!out)
351 {
352 grub_free (indata);
353 grub_free ((char *) in->name);
354 grub_free (in);
355 return grub_errno;
356 }
357
358 outdata = grub_malloc (sizeof (*outdata));
359 if (!outdata)
360 {
361 grub_free (indata);
362 grub_free ((char *) in->name);
363 grub_free (out);
364 grub_free (in);
365 return grub_errno;
366 }
367
368 grub_memcpy (out, &grub_serial_term_output, sizeof (*out));
369 out->data = outdata;
370 out->name = in->name;
371 grub_memcpy (outdata, &grub_serial_terminfo_output, sizeof (*outdata));
372
373 grub_list_push (GRUB_AS_LIST_P (&grub_serial_ports), GRUB_AS_LIST (port));
374 ((struct grub_serial_input_state *) in->data)->port = port;
375 ((struct grub_serial_output_state *) out->data)->port = port;
376 port->term_in = in;
377 port->term_out = out;
378 grub_terminfo_output_register (out, "vt100");
379 #ifdef GRUB_MACHINE_MIPS_LOONGSON
380 if (grub_strcmp (port->name, loongson_defserial[grub_arch_machine]) == 0)
381 {
382 grub_term_register_input_active ("serial_*", in);
383 grub_term_register_output_active ("serial_*", out);
384 }
385 else
386 {
387 grub_term_register_input_inactive ("serial_*", in);
388 grub_term_register_output_inactive ("serial_*", out);
389 }
390 #else
391 grub_term_register_input ("serial_*", in);
392 grub_term_register_output ("serial_*", out);
393 #endif
394
395 return GRUB_ERR_NONE;
396 }
397
398 void
399 grub_serial_unregister (struct grub_serial_port *port)
400 {
401 if (port->driver->fini)
402 port->driver->fini (port);
403
404 if (port->term_in)
405 grub_term_unregister_input (port->term_in);
406 if (port->term_out)
407 grub_term_unregister_output (port->term_out);
408
409 grub_list_remove (GRUB_AS_LIST (port));
410 }
411
412 void
413 grub_serial_unregister_driver (struct grub_serial_driver *driver)
414 {
415 struct grub_serial_port *port, *next;
416 for (port = grub_serial_ports; port; port = next)
417 {
418 next = port->next;
419 if (port->driver == driver)
420 grub_serial_unregister (port);
421 }
422 }
423
424 static grub_extcmd_t cmd;
425
426 GRUB_MOD_INIT(serial)
427 {
428 cmd = grub_register_extcmd ("serial", grub_cmd_serial, 0,
429 N_("[OPTIONS...]"),
430 N_("Configure serial port."), options);
431 grub_memcpy (&grub_serial_terminfo_output,
432 &grub_serial_terminfo_output_template,
433 sizeof (grub_serial_terminfo_output));
434
435 grub_memcpy (&grub_serial_terminfo_input,
436 &grub_serial_terminfo_input_template,
437 sizeof (grub_serial_terminfo_input));
438
439 #if !defined (GRUB_MACHINE_EMU) && !defined(GRUB_MACHINE_ARC) && (defined(__mips__) || defined (__i386__) || defined (__x86_64__))
440 grub_ns8250_init ();
441 #endif
442 #ifdef GRUB_MACHINE_IEEE1275
443 grub_ofserial_init ();
444 #endif
445 #ifdef GRUB_MACHINE_EFI
446 grub_efiserial_init ();
447 #endif
448 #ifdef GRUB_MACHINE_ARC
449 grub_arcserial_init ();
450 #endif
451 }
452
453 GRUB_MOD_FINI(serial)
454 {
455 while (grub_serial_ports)
456 grub_serial_unregister (grub_serial_ports);
457 if (registered)
458 {
459 grub_term_unregister_input (&grub_serial_term_input);
460 grub_term_unregister_output (&grub_serial_term_output);
461 }
462 grub_unregister_extcmd (cmd);
463 }