2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc.
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.
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.
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/>.
19 #include <grub/machine/memory.h>
20 #include <grub/serial.h>
21 #include <grub/term.h>
22 #include <grub/types.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>
31 #define TEXT_HEIGHT 24
33 static unsigned int xpos
, ypos
;
34 static unsigned int keep_track
= 1;
35 static unsigned int registered
= 0;
37 /* An input buffer. */
38 static char input_buf
[8];
39 static unsigned int npending
= 0;
41 static struct grub_term_output grub_serial_term_output
;
43 /* Argument options. */
44 static const struct grub_arg_option options
[] =
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
},
58 /* Serial port settings. */
62 unsigned short divisor
;
63 unsigned short word_len
;
65 unsigned short stop_bits
;
68 /* Serial port settings. */
69 static struct serial_port serial_settings
;
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
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))
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
)
84 if (unit
< GRUB_SERIAL_PORT_NUM
)
85 return serial_hw_io_addr
[unit
];
92 serial_hw_fetch (void)
94 if (grub_inb (serial_settings
.port
+ UART_LSR
) & UART_DATA_READY
)
95 return grub_inb (serial_settings
.port
+ UART_RX
);
100 /* Put a character. */
102 serial_hw_put (const int c
)
104 unsigned int timeout
= 100000;
106 /* Wait until the transmitter holding register is empty. */
107 while ((grub_inb (serial_settings
.port
+ UART_LSR
) & UART_EMPTY_TRANSMITTER
) == 0)
110 /* There is something wrong. But what can I do? */
114 grub_outb (c
, serial_settings
.port
+ UART_TX
);
118 serial_translate_key_sequence (void)
144 {('1' | ('~' << 8)), 1},
145 {('3' | ('~' << 8)), 4},
146 {('5' | ('~' << 8)), 7},
147 {('6' | ('~' << 8)), 3}
153 /* The buffer must start with "ESC [". */
154 if (input_buf
[0] != '\e' || input_buf
[1] != '[')
157 for (i
= 0; i
< ARRAY_SIZE (three_code_table
); i
++)
158 if (three_code_table
[i
].key
== input_buf
[2])
160 input_buf
[0] = three_code_table
[i
].ascii
;
162 grub_memmove (input_buf
+ 1, input_buf
+ 3, npending
- 1);
168 short key
= input_buf
[3] | (input_buf
[4] << 8);
170 for (i
= 0; i
< ARRAY_SIZE (four_code_table
); i
++)
171 if (four_code_table
[i
].key
== key
)
173 input_buf
[0] = four_code_table
[i
].ascii
;
175 grub_memmove (input_buf
+ 1, input_buf
+ 4, npending
- 1);
182 fill_input_buf (const int nowait
)
186 for (i
= 0; i
< 10000 && npending
< sizeof (input_buf
); i
++)
190 c
= serial_hw_fetch ();
193 input_buf
[npending
++] = c
;
195 /* Reset the counter to zero, to wait for the same interval. */
203 /* Translate some key sequences. */
204 serial_translate_key_sequence ();
209 /* Convert speed to divisor. */
210 static unsigned short
211 serial_get_divisor (unsigned int speed
)
215 /* The structure for speed vs. divisor. */
222 /* The table which lists common configurations. */
223 /* 1843200 / (speed * 16) */
224 static struct divisor divisor_tab
[] =
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
;
242 return divisor_tab
[i
].div
;
247 /* The serial version of checkkey. */
249 grub_serial_checkkey (void)
251 if (fill_input_buf (1))
257 /* The serial version of getkey. */
259 grub_serial_getkey (void)
263 while (! fill_input_buf (0))
268 c
= GRUB_TERM_BACKSPACE
;
270 grub_memmove (input_buf
, input_buf
+ 1, --npending
);
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
283 serial_hw_init (void)
285 unsigned char status
= 0;
287 /* Turn off the interrupt. */
288 grub_outb (0, serial_settings
.port
+ UART_IER
);
291 grub_outb (UART_DLAB
, serial_settings
.port
+ UART_LCR
);
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
);
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
);
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
);
308 /* Turn on DTR, RTS, and OUT2. */
309 grub_outb (UART_ENABLE_MODEM
, serial_settings
.port
+ UART_MCR
);
312 /* Drain the input buffer. */
313 while (grub_serial_checkkey () != -1)
314 (void) grub_serial_getkey ();
316 /* FIXME: should check if the serial terminal was found. */
318 return GRUB_ERR_NONE
;
321 /* The serial version of putchar. */
323 grub_serial_putchar (const struct grub_unicode_glyph
*c
)
325 /* Keep track of the cursor. */
340 if (ypos
< TEXT_HEIGHT
- 1)
349 if ((c
->base
& 0xC0) == 0x80)
351 if (xpos
>= TEXT_WIDTH
)
354 if (ypos
< TEXT_HEIGHT
- 1)
356 serial_hw_put ('\r');
357 serial_hw_put ('\n');
364 serial_hw_put (c
->base
);
368 grub_serial_getcharwidth (const struct grub_unicode_glyph
*c
__attribute__ ((unused
)))
374 grub_serial_getwh (void)
376 return (TEXT_WIDTH
<< 8) | TEXT_HEIGHT
;
380 grub_serial_getxy (void)
382 return ((xpos
<< 8) | ypos
);
386 grub_serial_gotoxy (grub_uint8_t x
, grub_uint8_t y
)
388 if (x
> TEXT_WIDTH
|| y
> TEXT_HEIGHT
)
390 grub_error (GRUB_ERR_OUT_OF_RANGE
, "invalid point (%u,%u)", x
, y
);
395 grub_terminfo_gotoxy (x
, y
, &grub_serial_term_output
);
404 grub_serial_cls (void)
407 grub_terminfo_cls (&grub_serial_term_output
);
414 grub_serial_setcolorstate (const grub_term_color_state state
)
419 case GRUB_TERM_COLOR_STANDARD
:
420 case GRUB_TERM_COLOR_NORMAL
:
421 grub_terminfo_reverse_video_off (&grub_serial_term_output
);
423 case GRUB_TERM_COLOR_HIGHLIGHT
:
424 grub_terminfo_reverse_video_on (&grub_serial_term_output
);
433 grub_serial_setcursor (const int on
)
436 grub_terminfo_cursor_on (&grub_serial_term_output
);
438 grub_terminfo_cursor_off (&grub_serial_term_output
);
441 static struct grub_term_input grub_serial_term_input
=
444 .checkkey
= grub_serial_checkkey
,
445 .getkey
= grub_serial_getkey
,
448 static struct grub_term_output grub_serial_term_output
=
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
,
465 grub_cmd_serial (grub_extcmd_t cmd
,
466 int argc
__attribute__ ((unused
)),
467 char **args
__attribute__ ((unused
)))
469 struct grub_arg_list
*state
= cmd
->state
;
470 struct serial_port backup_settings
= serial_settings
;
471 grub_err_t hwiniterr
;
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");
484 serial_settings
.port
= (grub_port_t
) grub_strtoul (state
[1].arg
, 0, 0);
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)
494 serial_settings
= backup_settings
;
495 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "bad speed");
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
;
511 serial_settings
= backup_settings
;
512 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "bad word length");
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
;
526 serial_settings
= backup_settings
;
527 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "bad parity");
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
;
539 serial_settings
= backup_settings
;
540 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "bad number of stop bits");
544 grub_serial_term_output
.flags
&= ~GRUB_TERM_CODE_TYPE_MASK
;
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
;
551 grub_serial_term_output
.flags
|= GRUB_TERM_CODE_TYPE_ASCII
;
553 /* Initialize with new settings. */
554 hwiniterr
= serial_hw_init ();
556 if (hwiniterr
== GRUB_ERR_NONE
)
558 /* Register terminal if not yet registered. */
561 grub_term_register_input ("serial", &grub_serial_term_input
);
562 grub_term_register_output ("serial", &grub_serial_term_output
);
568 /* Initialization with new settings failed. */
571 /* If the terminal is registered, attempt to restore previous
573 serial_settings
= backup_settings
;
574 if (serial_hw_init () != GRUB_ERR_NONE
)
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
);
587 static grub_extcmd_t cmd
;
589 GRUB_MOD_INIT(serial
)
591 cmd
= grub_register_extcmd ("serial", grub_cmd_serial
,
592 GRUB_COMMAND_FLAG_BOTH
,
593 "serial [OPTIONS...]",
594 "Configure serial port.", options
);
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);
601 serial_settings
.divisor
= serial_get_divisor (9600);
603 serial_settings
.word_len
= UART_8BITS_WORD
;
604 serial_settings
.parity
= UART_NO_PARITY
;
605 serial_settings
.stop_bits
= UART_1_STOP_BIT
;
608 GRUB_MOD_FINI(serial
)
610 grub_unregister_extcmd (cmd
);
611 if (registered
== 1) /* Unregister terminal only if registered. */
613 grub_term_unregister_input (&grub_serial_term_input
);
614 grub_term_unregister_output (&grub_serial_term_output
);