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