]> git.proxmox.com Git - grub2.git/blob - term/serial.c
18978911f7554a5fa0ba58c0bb961da4ccc338d2
[grub2.git] / term / serial.c
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009 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/machine/memory.h>
20 #include <grub/serial.h>
21 #include <grub/term.h>
22 #include <grub/types.h>
23 #include <grub/dl.h>
24 #include <grub/misc.h>
25 #include <grub/terminfo.h>
26 #include <grub/cpu/io.h>
27 #include <grub/extcmd.h>
28 #include <grub/i18n.h>
29
30 #define TEXT_WIDTH 80
31 #define TEXT_HEIGHT 24
32
33 static unsigned int xpos, ypos;
34 static unsigned int keep_track = 1;
35 static unsigned int registered = 0;
36
37 /* An input buffer. */
38 static char input_buf[8];
39 static unsigned int npending = 0;
40
41 static struct grub_term_output grub_serial_term_output;
42
43 /* Argument options. */
44 static const struct grub_arg_option options[] =
45 {
46 {"unit", 'u', 0, N_("Set the serial unit."), 0, ARG_TYPE_INT},
47 {"port", 'p', 0, N_("Set the serial port address."), 0, ARG_TYPE_STRING},
48 {"speed", 's', 0, N_("Set the serial port speed."), 0, ARG_TYPE_INT},
49 {"word", 'w', 0, N_("Set the serial port word length."), 0, ARG_TYPE_INT},
50 {"parity", 'r', 0, N_("Set the serial port parity."), 0, ARG_TYPE_STRING},
51 {"stop", 't', 0, N_("Set the serial port stop bits."), 0, ARG_TYPE_INT},
52 {"ascii", 'a', 0, N_("Terminal is ASCII-only."), 0, ARG_TYPE_NONE},
53 {"utf8", 'l', 0, N_("Terminal is logical-ordered UTF-8."), 0, ARG_TYPE_NONE},
54 {"visual-utf8", 'v', 0, N_("Terminal is visually-ordered UTF-8."), 0, ARG_TYPE_NONE},
55 {0, 0, 0, 0, 0, 0}
56 };
57
58 /* Serial port settings. */
59 struct serial_port
60 {
61 grub_port_t port;
62 unsigned short divisor;
63 unsigned short word_len;
64 unsigned int parity;
65 unsigned short stop_bits;
66 };
67
68 /* Serial port settings. */
69 static struct serial_port serial_settings;
70
71 #ifdef GRUB_MACHINE_PCBIOS
72 static const unsigned short *serial_hw_io_addr = (const unsigned short *) GRUB_MEMORY_MACHINE_BIOS_DATA_AREA_ADDR;
73 #define GRUB_SERIAL_PORT_NUM 4
74 #else
75 #include <grub/machine/serial.h>
76 static const grub_port_t serial_hw_io_addr[] = GRUB_MACHINE_SERIAL_PORTS;
77 #define GRUB_SERIAL_PORT_NUM (ARRAY_SIZE(serial_hw_io_addr))
78 #endif
79
80 /* Return the port number for the UNITth serial device. */
81 static inline grub_port_t
82 serial_hw_get_port (const unsigned int unit)
83 {
84 if (unit < GRUB_SERIAL_PORT_NUM)
85 return serial_hw_io_addr[unit];
86 else
87 return 0;
88 }
89
90 /* Fetch a key. */
91 static int
92 serial_hw_fetch (void)
93 {
94 if (grub_inb (serial_settings.port + UART_LSR) & UART_DATA_READY)
95 return grub_inb (serial_settings.port + UART_RX);
96
97 return -1;
98 }
99
100 /* Put a character. */
101 static void
102 serial_hw_put (const int c)
103 {
104 unsigned int timeout = 100000;
105
106 /* Wait until the transmitter holding register is empty. */
107 while ((grub_inb (serial_settings.port + UART_LSR) & UART_EMPTY_TRANSMITTER) == 0)
108 {
109 if (--timeout == 0)
110 /* There is something wrong. But what can I do? */
111 return;
112 }
113
114 grub_outb (c, serial_settings.port + UART_TX);
115 }
116
117 static void
118 serial_translate_key_sequence (void)
119 {
120 unsigned int i;
121 static struct
122 {
123 char key;
124 char ascii;
125 }
126 three_code_table[] =
127 {
128 {'A', 16},
129 {'B', 14},
130 {'C', 6},
131 {'D', 2},
132 {'F', 5},
133 {'H', 1},
134 {'4', 4}
135 };
136
137 static struct
138 {
139 short key;
140 char ascii;
141 }
142 four_code_table[] =
143 {
144 {('1' | ('~' << 8)), 1},
145 {('3' | ('~' << 8)), 4},
146 {('5' | ('~' << 8)), 7},
147 {('6' | ('~' << 8)), 3}
148 };
149
150 if (npending < 3)
151 return;
152
153 /* The buffer must start with "ESC [". */
154 if (input_buf[0] != '\e' || input_buf[1] != '[')
155 return;
156
157 for (i = 0; i < ARRAY_SIZE (three_code_table); i++)
158 if (three_code_table[i].key == input_buf[2])
159 {
160 input_buf[0] = three_code_table[i].ascii;
161 npending -= 2;
162 grub_memmove (input_buf + 1, input_buf + 3, npending - 1);
163 return;
164 }
165
166 if (npending >= 4)
167 {
168 short key = input_buf[3] | (input_buf[4] << 8);
169
170 for (i = 0; i < ARRAY_SIZE (four_code_table); i++)
171 if (four_code_table[i].key == key)
172 {
173 input_buf[0] = four_code_table[i].ascii;
174 npending -= 3;
175 grub_memmove (input_buf + 1, input_buf + 4, npending - 1);
176 return;
177 }
178 }
179 }
180
181 static int
182 fill_input_buf (const int nowait)
183 {
184 int i;
185
186 for (i = 0; i < 10000 && npending < sizeof (input_buf); i++)
187 {
188 int c;
189
190 c = serial_hw_fetch ();
191 if (c >= 0)
192 {
193 input_buf[npending++] = c;
194
195 /* Reset the counter to zero, to wait for the same interval. */
196 i = 0;
197 }
198
199 if (nowait)
200 break;
201 }
202
203 /* Translate some key sequences. */
204 serial_translate_key_sequence ();
205
206 return npending;
207 }
208
209 /* Convert speed to divisor. */
210 static unsigned short
211 serial_get_divisor (unsigned int speed)
212 {
213 unsigned int i;
214
215 /* The structure for speed vs. divisor. */
216 struct divisor
217 {
218 unsigned int speed;
219 unsigned short div;
220 };
221
222 /* The table which lists common configurations. */
223 /* 1843200 / (speed * 16) */
224 static struct divisor divisor_tab[] =
225 {
226 { 2400, 0x0030 },
227 { 4800, 0x0018 },
228 { 9600, 0x000C },
229 { 19200, 0x0006 },
230 { 38400, 0x0003 },
231 { 57600, 0x0002 },
232 { 115200, 0x0001 }
233 };
234
235 /* Set the baud rate. */
236 for (i = 0; i < sizeof (divisor_tab) / sizeof (divisor_tab[0]); i++)
237 if (divisor_tab[i].speed == speed)
238 /* UART in Yeeloong runs twice the usual rate. */
239 #ifdef GRUB_MACHINE_MIPS_YEELOONG
240 return 2 * divisor_tab[i].div;
241 #else
242 return divisor_tab[i].div;
243 #endif
244 return 0;
245 }
246
247 /* The serial version of checkkey. */
248 static int
249 grub_serial_checkkey (void)
250 {
251 if (fill_input_buf (1))
252 return input_buf[0];
253 else
254 return -1;
255 }
256
257 /* The serial version of getkey. */
258 static int
259 grub_serial_getkey (void)
260 {
261 int c;
262
263 while (! fill_input_buf (0))
264 ;
265
266 c = input_buf[0];
267 if (c == 0x7f)
268 c = GRUB_TERM_BACKSPACE;
269
270 grub_memmove (input_buf, input_buf + 1, --npending);
271
272 return c;
273 }
274
275 /* Initialize a serial device. PORT is the port number for a serial device.
276 SPEED is a DTE-DTE speed which must be one of these: 2400, 4800, 9600,
277 19200, 38400, 57600 and 115200. WORD_LEN is the word length to be used
278 for the device. Likewise, PARITY is the type of the parity and
279 STOP_BIT_LEN is the length of the stop bit. The possible values for
280 WORD_LEN, PARITY and STOP_BIT_LEN are defined in the header file as
281 macros. */
282 static grub_err_t
283 serial_hw_init (void)
284 {
285 unsigned char status = 0;
286
287 /* Turn off the interrupt. */
288 grub_outb (0, serial_settings.port + UART_IER);
289
290 /* Set DLAB. */
291 grub_outb (UART_DLAB, serial_settings.port + UART_LCR);
292
293 /* Set the baud rate. */
294 grub_outb (serial_settings.divisor & 0xFF, serial_settings.port + UART_DLL);
295 grub_outb (serial_settings.divisor >> 8, serial_settings.port + UART_DLH);
296
297 /* Set the line status. */
298 status |= (serial_settings.parity
299 | serial_settings.word_len
300 | serial_settings.stop_bits);
301 grub_outb (status, serial_settings.port + UART_LCR);
302
303 /* In Yeeloong serial port has only 3 wires. */
304 #ifndef GRUB_MACHINE_MIPS_YEELOONG
305 /* Enable the FIFO. */
306 grub_outb (UART_ENABLE_FIFO, serial_settings.port + UART_FCR);
307
308 /* Turn on DTR, RTS, and OUT2. */
309 grub_outb (UART_ENABLE_MODEM, serial_settings.port + UART_MCR);
310 #endif
311
312 /* Drain the input buffer. */
313 while (grub_serial_checkkey () != -1)
314 (void) grub_serial_getkey ();
315
316 /* FIXME: should check if the serial terminal was found. */
317
318 return GRUB_ERR_NONE;
319 }
320
321 /* The serial version of putchar. */
322 static void
323 grub_serial_putchar (const struct grub_unicode_glyph *c)
324 {
325 /* Keep track of the cursor. */
326 if (keep_track)
327 {
328 switch (c->base)
329 {
330 case '\a':
331 break;
332
333 case '\b':
334 case 127:
335 if (xpos > 0)
336 xpos--;
337 break;
338
339 case '\n':
340 if (ypos < TEXT_HEIGHT - 1)
341 ypos++;
342 break;
343
344 case '\r':
345 xpos = 0;
346 break;
347
348 default:
349 if ((c->base & 0xC0) == 0x80)
350 break;
351 if (xpos >= TEXT_WIDTH)
352 {
353 xpos = 0;
354 if (ypos < TEXT_HEIGHT - 1)
355 ypos++;
356 serial_hw_put ('\r');
357 serial_hw_put ('\n');
358 }
359 xpos++;
360 break;
361 }
362 }
363
364 serial_hw_put (c->base);
365 }
366
367 static grub_ssize_t
368 grub_serial_getcharwidth (const struct grub_unicode_glyph *c __attribute__ ((unused)))
369 {
370 return 1;
371 }
372
373 static grub_uint16_t
374 grub_serial_getwh (void)
375 {
376 return (TEXT_WIDTH << 8) | TEXT_HEIGHT;
377 }
378
379 static grub_uint16_t
380 grub_serial_getxy (void)
381 {
382 return ((xpos << 8) | ypos);
383 }
384
385 static void
386 grub_serial_gotoxy (grub_uint8_t x, grub_uint8_t y)
387 {
388 if (x > TEXT_WIDTH || y > TEXT_HEIGHT)
389 {
390 grub_error (GRUB_ERR_OUT_OF_RANGE, "invalid point (%u,%u)", x, y);
391 }
392 else
393 {
394 keep_track = 0;
395 grub_terminfo_gotoxy (x, y, &grub_serial_term_output);
396 keep_track = 1;
397
398 xpos = x;
399 ypos = y;
400 }
401 }
402
403 static void
404 grub_serial_cls (void)
405 {
406 keep_track = 0;
407 grub_terminfo_cls (&grub_serial_term_output);
408 keep_track = 1;
409
410 xpos = ypos = 0;
411 }
412
413 static void
414 grub_serial_setcolorstate (const grub_term_color_state state)
415 {
416 keep_track = 0;
417 switch (state)
418 {
419 case GRUB_TERM_COLOR_STANDARD:
420 case GRUB_TERM_COLOR_NORMAL:
421 grub_terminfo_reverse_video_off (&grub_serial_term_output);
422 break;
423 case GRUB_TERM_COLOR_HIGHLIGHT:
424 grub_terminfo_reverse_video_on (&grub_serial_term_output);
425 break;
426 default:
427 break;
428 }
429 keep_track = 1;
430 }
431
432 static void
433 grub_serial_setcursor (const int on)
434 {
435 if (on)
436 grub_terminfo_cursor_on (&grub_serial_term_output);
437 else
438 grub_terminfo_cursor_off (&grub_serial_term_output);
439 }
440
441 static struct grub_term_input grub_serial_term_input =
442 {
443 .name = "serial",
444 .checkkey = grub_serial_checkkey,
445 .getkey = grub_serial_getkey,
446 };
447
448 static struct grub_term_output grub_serial_term_output =
449 {
450 .name = "serial",
451 .putchar = grub_serial_putchar,
452 .getcharwidth = grub_serial_getcharwidth,
453 .getwh = grub_serial_getwh,
454 .getxy = grub_serial_getxy,
455 .gotoxy = grub_serial_gotoxy,
456 .cls = grub_serial_cls,
457 .setcolorstate = grub_serial_setcolorstate,
458 .setcursor = grub_serial_setcursor,
459 .flags = GRUB_TERM_CODE_TYPE_ASCII,
460 };
461
462 \f
463
464 static grub_err_t
465 grub_cmd_serial (grub_extcmd_t cmd,
466 int argc __attribute__ ((unused)),
467 char **args __attribute__ ((unused)))
468 {
469 struct grub_arg_list *state = cmd->state;
470 struct serial_port backup_settings = serial_settings;
471 grub_err_t hwiniterr;
472
473 if (state[0].set)
474 {
475 unsigned int unit;
476
477 unit = grub_strtoul (state[0].arg, 0, 0);
478 serial_settings.port = serial_hw_get_port (unit);
479 if (!serial_settings.port)
480 return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad unit number");
481 }
482
483 if (state[1].set)
484 serial_settings.port = (grub_port_t) grub_strtoul (state[1].arg, 0, 0);
485
486 if (state[2].set)
487 {
488 unsigned long speed;
489
490 speed = grub_strtoul (state[2].arg, 0, 0);
491 serial_settings.divisor = serial_get_divisor ((unsigned int) speed);
492 if (serial_settings.divisor == 0)
493 {
494 serial_settings = backup_settings;
495 return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad speed");
496 }
497 }
498
499 if (state[3].set)
500 {
501 if (! grub_strcmp (state[3].arg, "5"))
502 serial_settings.word_len = UART_5BITS_WORD;
503 else if (! grub_strcmp (state[3].arg, "6"))
504 serial_settings.word_len = UART_6BITS_WORD;
505 else if (! grub_strcmp (state[3].arg, "7"))
506 serial_settings.word_len = UART_7BITS_WORD;
507 else if (! grub_strcmp (state[3].arg, "8"))
508 serial_settings.word_len = UART_8BITS_WORD;
509 else
510 {
511 serial_settings = backup_settings;
512 return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad word length");
513 }
514 }
515
516 if (state[4].set)
517 {
518 if (! grub_strcmp (state[4].arg, "no"))
519 serial_settings.parity = UART_NO_PARITY;
520 else if (! grub_strcmp (state[4].arg, "odd"))
521 serial_settings.parity = UART_ODD_PARITY;
522 else if (! grub_strcmp (state[4].arg, "even"))
523 serial_settings.parity = UART_EVEN_PARITY;
524 else
525 {
526 serial_settings = backup_settings;
527 return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad parity");
528 }
529 }
530
531 if (state[5].set)
532 {
533 if (! grub_strcmp (state[5].arg, "1"))
534 serial_settings.stop_bits = UART_1_STOP_BIT;
535 else if (! grub_strcmp (state[5].arg, "2"))
536 serial_settings.stop_bits = UART_2_STOP_BITS;
537 else
538 {
539 serial_settings = backup_settings;
540 return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad number of stop bits");
541 }
542 }
543
544 grub_serial_term_output.flags &= ~GRUB_TERM_CODE_TYPE_MASK;
545
546 if (state[7].set)
547 grub_serial_term_output.flags |= GRUB_TERM_CODE_TYPE_UTF8_LOGICAL;
548 else if (state[8].set)
549 grub_serial_term_output.flags |= GRUB_TERM_CODE_TYPE_UTF8_VISUAL;
550 else
551 grub_serial_term_output.flags |= GRUB_TERM_CODE_TYPE_ASCII;
552
553 /* Initialize with new settings. */
554 hwiniterr = serial_hw_init ();
555
556 if (hwiniterr == GRUB_ERR_NONE)
557 {
558 /* Register terminal if not yet registered. */
559 if (registered == 0)
560 {
561 grub_term_register_input ("serial", &grub_serial_term_input);
562 grub_term_register_output ("serial", &grub_serial_term_output);
563 registered = 1;
564 }
565 }
566 else
567 {
568 /* Initialization with new settings failed. */
569 if (registered == 1)
570 {
571 /* If the terminal is registered, attempt to restore previous
572 settings. */
573 serial_settings = backup_settings;
574 if (serial_hw_init () != GRUB_ERR_NONE)
575 {
576 /* If unable to restore settings, unregister terminal. */
577 grub_term_unregister_input (&grub_serial_term_input);
578 grub_term_unregister_output (&grub_serial_term_output);
579 registered = 0;
580 }
581 }
582 }
583
584 return hwiniterr;
585 }
586
587 static grub_extcmd_t cmd;
588
589 GRUB_MOD_INIT(serial)
590 {
591 cmd = grub_register_extcmd ("serial", grub_cmd_serial,
592 GRUB_COMMAND_FLAG_BOTH,
593 "serial [OPTIONS...]",
594 "Configure serial port.", options);
595
596 /* Set default settings. */
597 serial_settings.port = serial_hw_get_port (0);
598 #ifdef GRUB_MACHINE_MIPS_YEELOONG
599 serial_settings.divisor = serial_get_divisor (115200);
600 #else
601 serial_settings.divisor = serial_get_divisor (9600);
602 #endif
603 serial_settings.word_len = UART_8BITS_WORD;
604 serial_settings.parity = UART_NO_PARITY;
605 serial_settings.stop_bits = UART_1_STOP_BIT;
606 }
607
608 GRUB_MOD_FINI(serial)
609 {
610 grub_unregister_extcmd (cmd);
611 if (registered == 1) /* Unregister terminal only if registered. */
612 {
613 grub_term_unregister_input (&grub_serial_term_input);
614 grub_term_unregister_output (&grub_serial_term_output);
615 }
616 }