1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * (C) Copyright 2001-2015
6 * Compulab Ltd - http://compulab.co.il/
7 * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
12 #include <linux/ctype.h>
15 #include <video_console.h>
16 #include <video_font.h> /* Bitmap font for code page 437 */
19 * Structure to describe a console color
27 /* By default we scroll by a single line */
28 #ifndef CONFIG_CONSOLE_SCROLL_LINES
29 #define CONFIG_CONSOLE_SCROLL_LINES 1
32 int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
34 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
38 return ops->putc_xy(dev, x, y, ch);
41 int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
44 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
48 return ops->move_rows(dev, rowdst, rowsrc, count);
51 int vidconsole_set_row(struct udevice *dev, uint row, int clr)
53 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
57 return ops->set_row(dev, row, clr);
60 static int vidconsole_entry_start(struct udevice *dev)
62 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
64 if (!ops->entry_start)
66 return ops->entry_start(dev);
69 /* Move backwards one space */
70 static int vidconsole_back(struct udevice *dev)
72 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
73 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
77 ret = ops->backspace(dev);
82 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
83 if (priv->xcur_frac < priv->xstart_frac) {
84 priv->xcur_frac = (priv->cols - 1) *
85 VID_TO_POS(priv->x_charsize);
86 priv->ycur -= priv->y_charsize;
90 video_sync(dev->parent, false);
95 /* Move to a newline, scrolling the display if necessary */
96 static void vidconsole_newline(struct udevice *dev)
98 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
99 struct udevice *vid_dev = dev->parent;
100 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
101 const int rows = CONFIG_CONSOLE_SCROLL_LINES;
104 priv->xcur_frac = priv->xstart_frac;
105 priv->ycur += priv->y_charsize;
107 /* Check if we need to scroll the terminal */
108 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
109 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
110 for (i = 0; i < rows; i++)
111 vidconsole_set_row(dev, priv->rows - i - 1,
112 vid_priv->colour_bg);
113 priv->ycur -= rows * priv->y_charsize;
117 video_sync(dev->parent, false);
120 static const struct vid_rgb colors[VID_COLOR_COUNT] = {
121 { 0x00, 0x00, 0x00 }, /* black */
122 { 0xc0, 0x00, 0x00 }, /* red */
123 { 0x00, 0xc0, 0x00 }, /* green */
124 { 0xc0, 0x60, 0x00 }, /* brown */
125 { 0x00, 0x00, 0xc0 }, /* blue */
126 { 0xc0, 0x00, 0xc0 }, /* magenta */
127 { 0x00, 0xc0, 0xc0 }, /* cyan */
128 { 0xc0, 0xc0, 0xc0 }, /* light gray */
129 { 0x80, 0x80, 0x80 }, /* gray */
130 { 0xff, 0x00, 0x00 }, /* bright red */
131 { 0x00, 0xff, 0x00 }, /* bright green */
132 { 0xff, 0xff, 0x00 }, /* yellow */
133 { 0x00, 0x00, 0xff }, /* bright blue */
134 { 0xff, 0x00, 0xff }, /* bright magenta */
135 { 0x00, 0xff, 0xff }, /* bright cyan */
136 { 0xff, 0xff, 0xff }, /* white */
139 u32 vid_console_color(struct video_priv *priv, unsigned int idx)
141 switch (priv->bpix) {
143 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
144 return ((colors[idx].r >> 3) << 11) |
145 ((colors[idx].g >> 2) << 5) |
146 ((colors[idx].b >> 3) << 0);
150 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
151 return (colors[idx].r << 16) |
152 (colors[idx].g << 8) |
153 (colors[idx].b << 0);
161 * For unknown bit arrangements just support
165 return 0xffffff; /* white */
167 return 0x000000; /* black */
170 static char *parsenum(char *s, int *num)
173 *num = simple_strtol(s, &end, 10);
178 * set_cursor_position() - set cursor position
180 * @priv: private data of the video console
184 static void set_cursor_position(struct vidconsole_priv *priv, int row, int col)
187 * Ensure we stay in the bounds of the screen.
189 if (row >= priv->rows)
190 row = priv->rows - 1;
191 if (col >= priv->cols)
192 col = priv->cols - 1;
194 priv->ycur = row * priv->y_charsize;
195 priv->xcur_frac = priv->xstart_frac +
196 VID_TO_POS(col * priv->x_charsize);
200 * get_cursor_position() - get cursor position
202 * @priv: private data of the video console
206 static void get_cursor_position(struct vidconsole_priv *priv,
209 *row = priv->ycur / priv->y_charsize;
210 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
215 * Process a character while accumulating an escape string. Chars are
216 * accumulated into escape_buf until the end of escape sequence is
217 * found, at which point the sequence is parsed and processed.
219 static void vidconsole_escape_char(struct udevice *dev, char ch)
221 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
223 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
226 /* Sanity checking for bogus ESC sequences: */
227 if (priv->escape_len >= sizeof(priv->escape_buf))
229 if (priv->escape_len == 0) {
232 /* Save cursor position */
233 get_cursor_position(priv, &priv->row_saved,
239 /* Restore cursor position */
240 int row = priv->row_saved;
241 int col = priv->col_saved;
243 set_cursor_position(priv, row, col);
254 priv->escape_buf[priv->escape_len++] = ch;
257 * Escape sequences are terminated by a letter, so keep
258 * accumulating until we get one:
264 * clear escape mode first, otherwise things will get highly
265 * surprising if you hit any debug prints that come back to
278 char *s = priv->escape_buf;
281 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
282 * Cursor left/right: [%dD, [%dC
285 s = parsenum(s, &num);
286 if (num == 0) /* No digit in sequence ... */
287 num = 1; /* ... means "move by 1". */
289 get_cursor_position(priv, &row, &col);
290 if (ch == 'A' || ch == 'F')
296 if (ch == 'B' || ch == 'E')
298 if (ch == 'E' || ch == 'F')
304 /* Right and bottom overflows are handled in the callee. */
305 set_cursor_position(priv, row, col);
311 char *s = priv->escape_buf;
314 * Set cursor position: [%d;%df or [%d;%dH
317 s = parsenum(s, &row);
319 s = parsenum(s, &col);
322 * Video origin is [0, 0], terminal origin is [1, 1].
329 set_cursor_position(priv, row, col);
337 * Clear part/all screen:
338 * [J or [0J - clear screen from cursor down
339 * [1J - clear screen from cursor up
340 * [2J - clear entire screen
342 * TODO we really only handle entire-screen case, others
343 * probably require some additions to video-uclass (and
344 * are not really needed yet by efi_console)
346 parsenum(priv->escape_buf + 1, &mode);
349 video_clear(dev->parent);
350 video_sync(dev->parent, false);
352 priv->xcur_frac = priv->xstart_frac;
354 debug("unsupported clear mode: %d\n", mode);
359 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
363 * Clear (parts of) current line
364 * [0K - clear line to end
365 * [2K - clear entire line
367 parsenum(priv->escape_buf + 1, &mode);
372 get_cursor_position(priv, &row, &col);
373 vidconsole_set_row(dev, row, vid_priv->colour_bg);
378 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
379 char *s = priv->escape_buf;
380 char *end = &priv->escape_buf[priv->escape_len];
383 * Set graphics mode: [%d;...;%dm
385 * Currently only supports the color attributes:
414 s = parsenum(s, &val);
419 /* all attributes off */
420 video_set_default_colors(dev->parent, false);
424 vid_priv->fg_col_idx |= 8;
425 vid_priv->colour_fg = vid_console_color(
426 vid_priv, vid_priv->fg_col_idx);
430 vid_priv->colour_fg = vid_console_color(
431 vid_priv, vid_priv->bg_col_idx);
432 vid_priv->colour_bg = vid_console_color(
433 vid_priv, vid_priv->fg_col_idx);
436 /* foreground color */
437 vid_priv->fg_col_idx &= ~7;
438 vid_priv->fg_col_idx |= val - 30;
439 vid_priv->colour_fg = vid_console_color(
440 vid_priv, vid_priv->fg_col_idx);
443 /* background color, also mask the bold bit */
444 vid_priv->bg_col_idx &= ~0xf;
445 vid_priv->bg_col_idx |= val - 40;
446 vid_priv->colour_bg = vid_console_color(
447 vid_priv, vid_priv->bg_col_idx);
450 /* ignore unsupported SGR parameter */
458 debug("unrecognized escape sequence: %*s\n",
459 priv->escape_len, priv->escape_buf);
465 /* something went wrong, just revert to normal mode: */
469 /* Put that actual character on the screen (using the CP437 code page). */
470 static int vidconsole_output_glyph(struct udevice *dev, char ch)
472 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
476 * Failure of this function normally indicates an unsupported
477 * colour depth. Check this and return an error to help with
480 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
481 if (ret == -EAGAIN) {
482 vidconsole_newline(dev);
483 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
487 priv->xcur_frac += ret;
489 if (priv->xcur_frac >= priv->xsize_frac)
490 vidconsole_newline(dev);
495 int vidconsole_put_char(struct udevice *dev, char ch)
497 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
501 vidconsole_escape_char(dev, ch);
507 priv->escape_len = 0;
514 priv->xcur_frac = priv->xstart_frac;
517 vidconsole_newline(dev);
518 vidconsole_entry_start(dev);
520 case '\t': /* Tab (8 chars alignment) */
521 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
522 + 1) * priv->tab_width_frac;
524 if (priv->xcur_frac >= priv->xsize_frac)
525 vidconsole_newline(dev);
528 vidconsole_back(dev);
532 ret = vidconsole_output_glyph(dev, ch);
541 int vidconsole_put_string(struct udevice *dev, const char *str)
546 for (s = str; *s; s++) {
547 ret = vidconsole_put_char(dev, *s);
555 static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
557 struct udevice *dev = sdev->priv;
559 vidconsole_put_char(dev, ch);
560 video_sync(dev->parent, false);
563 static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
565 struct udevice *dev = sdev->priv;
567 vidconsole_put_string(dev, s);
568 video_sync(dev->parent, false);
571 /* Set up the number of rows and colours (rotated drivers override this) */
572 static int vidconsole_pre_probe(struct udevice *dev)
574 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
575 struct udevice *vid = dev->parent;
576 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
578 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
583 /* Register the device with stdio */
584 static int vidconsole_post_probe(struct udevice *dev)
586 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
587 struct stdio_dev *sdev = &priv->sdev;
589 if (!priv->tab_width_frac)
590 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
593 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
596 strcpy(sdev->name, "vidconsole");
599 sdev->flags = DEV_FLAGS_OUTPUT;
600 sdev->putc = vidconsole_putc;
601 sdev->puts = vidconsole_puts;
604 return stdio_register(sdev);
607 UCLASS_DRIVER(vidconsole) = {
608 .id = UCLASS_VIDEO_CONSOLE,
609 .name = "vidconsole0",
610 .pre_probe = vidconsole_pre_probe,
611 .post_probe = vidconsole_post_probe,
612 .per_device_auto_alloc_size = sizeof(struct vidconsole_priv),
615 void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
617 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
618 struct udevice *vid_dev = dev->parent;
619 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
621 col *= priv->x_charsize;
622 row *= priv->y_charsize;
623 priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
624 priv->ycur = min_t(short, row, vid_priv->ysize - 1);
627 static int do_video_setcursor(struct cmd_tbl *cmdtp, int flag, int argc,
630 unsigned int col, row;
634 return CMD_RET_USAGE;
636 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
637 return CMD_RET_FAILURE;
638 col = simple_strtoul(argv[1], NULL, 10);
639 row = simple_strtoul(argv[2], NULL, 10);
640 vidconsole_position_cursor(dev, col, row);
645 static int do_video_puts(struct cmd_tbl *cmdtp, int flag, int argc,
652 return CMD_RET_USAGE;
654 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
655 return CMD_RET_FAILURE;
656 for (s = argv[1]; *s; s++)
657 vidconsole_put_char(dev, *s);
659 video_sync(dev->parent, false);
665 setcurs, 3, 1, do_video_setcursor,
666 "set cursor position within screen",
667 " <col> <row> in character"
671 lcdputs, 2, 1, do_video_puts,
672 "print string on video framebuffer",