1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * (C) Copyright 2001-2015
5 * DENX Software Engineering -- wd@denx.de
6 * Compulab Ltd - http://compulab.co.il/
7 * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
11 #include <linux/ctype.h>
14 #include <video_console.h>
15 #include <video_font.h> /* Bitmap font for code page 437 */
18 * Structure to describe a console color
26 /* By default we scroll by a single line */
27 #ifndef CONFIG_CONSOLE_SCROLL_LINES
28 #define CONFIG_CONSOLE_SCROLL_LINES 1
31 int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
33 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
37 return ops->putc_xy(dev, x, y, ch);
40 int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
43 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
47 return ops->move_rows(dev, rowdst, rowsrc, count);
50 int vidconsole_set_row(struct udevice *dev, uint row, int clr)
52 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
56 return ops->set_row(dev, row, clr);
59 static int vidconsole_entry_start(struct udevice *dev)
61 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
63 if (!ops->entry_start)
65 return ops->entry_start(dev);
68 /* Move backwards one space */
69 static int vidconsole_back(struct udevice *dev)
71 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
72 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
76 ret = ops->backspace(dev);
81 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
82 if (priv->xcur_frac < priv->xstart_frac) {
83 priv->xcur_frac = (priv->cols - 1) *
84 VID_TO_POS(priv->x_charsize);
85 priv->ycur -= priv->y_charsize;
89 video_sync(dev->parent, false);
94 /* Move to a newline, scrolling the display if necessary */
95 static void vidconsole_newline(struct udevice *dev)
97 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
98 struct udevice *vid_dev = dev->parent;
99 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
100 const int rows = CONFIG_CONSOLE_SCROLL_LINES;
103 priv->xcur_frac = priv->xstart_frac;
104 priv->ycur += priv->y_charsize;
106 /* Check if we need to scroll the terminal */
107 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
108 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
109 for (i = 0; i < rows; i++)
110 vidconsole_set_row(dev, priv->rows - i - 1,
111 vid_priv->colour_bg);
112 priv->ycur -= rows * priv->y_charsize;
116 video_sync(dev->parent, false);
119 static const struct vid_rgb colors[VID_COLOR_COUNT] = {
120 { 0x00, 0x00, 0x00 }, /* black */
121 { 0xc0, 0x00, 0x00 }, /* red */
122 { 0x00, 0xc0, 0x00 }, /* green */
123 { 0xc0, 0x60, 0x00 }, /* brown */
124 { 0x00, 0x00, 0xc0 }, /* blue */
125 { 0xc0, 0x00, 0xc0 }, /* magenta */
126 { 0x00, 0xc0, 0xc0 }, /* cyan */
127 { 0xc0, 0xc0, 0xc0 }, /* light gray */
128 { 0x80, 0x80, 0x80 }, /* gray */
129 { 0xff, 0x00, 0x00 }, /* bright red */
130 { 0x00, 0xff, 0x00 }, /* bright green */
131 { 0xff, 0xff, 0x00 }, /* yellow */
132 { 0x00, 0x00, 0xff }, /* bright blue */
133 { 0xff, 0x00, 0xff }, /* bright magenta */
134 { 0x00, 0xff, 0xff }, /* bright cyan */
135 { 0xff, 0xff, 0xff }, /* white */
138 u32 vid_console_color(struct video_priv *priv, unsigned int idx)
140 switch (priv->bpix) {
142 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
143 return ((colors[idx].r >> 3) << 11) |
144 ((colors[idx].g >> 2) << 5) |
145 ((colors[idx].b >> 3) << 0);
149 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
150 return (colors[idx].r << 16) |
151 (colors[idx].g << 8) |
152 (colors[idx].b << 0);
160 * For unknown bit arrangements just support
164 return 0xffffff; /* white */
166 return 0x000000; /* black */
169 static char *parsenum(char *s, int *num)
172 *num = simple_strtol(s, &end, 10);
177 * set_cursor_position() - set cursor position
179 * @priv: private data of the video console
183 static void set_cursor_position(struct vidconsole_priv *priv, int row, int col)
186 * Ensure we stay in the bounds of the screen.
188 if (row >= priv->rows)
189 row = priv->rows - 1;
190 if (col >= priv->cols)
191 col = priv->cols - 1;
193 priv->ycur = row * priv->y_charsize;
194 priv->xcur_frac = priv->xstart_frac +
195 VID_TO_POS(col * priv->x_charsize);
199 * get_cursor_position() - get cursor position
201 * @priv: private data of the video console
205 static void get_cursor_position(struct vidconsole_priv *priv,
208 *row = priv->ycur / priv->y_charsize;
209 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
214 * Process a character while accumulating an escape string. Chars are
215 * accumulated into escape_buf until the end of escape sequence is
216 * found, at which point the sequence is parsed and processed.
218 static void vidconsole_escape_char(struct udevice *dev, char ch)
220 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
222 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
225 /* Sanity checking for bogus ESC sequences: */
226 if (priv->escape_len >= sizeof(priv->escape_buf))
228 if (priv->escape_len == 0) {
231 /* Save cursor position */
232 get_cursor_position(priv, &priv->row_saved,
238 /* Restore cursor position */
239 int row = priv->row_saved;
240 int col = priv->col_saved;
242 set_cursor_position(priv, row, col);
253 priv->escape_buf[priv->escape_len++] = ch;
256 * Escape sequences are terminated by a letter, so keep
257 * accumulating until we get one:
263 * clear escape mode first, otherwise things will get highly
264 * surprising if you hit any debug prints that come back to
277 char *s = priv->escape_buf;
280 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
281 * Cursor left/right: [%dD, [%dC
284 s = parsenum(s, &num);
285 if (num == 0) /* No digit in sequence ... */
286 num = 1; /* ... means "move by 1". */
288 get_cursor_position(priv, &row, &col);
289 if (ch == 'A' || ch == 'F')
295 if (ch == 'B' || ch == 'E')
297 if (ch == 'E' || ch == 'F')
303 /* Right and bottom overflows are handled in the callee. */
304 set_cursor_position(priv, row, col);
310 char *s = priv->escape_buf;
313 * Set cursor position: [%d;%df or [%d;%dH
316 s = parsenum(s, &row);
318 s = parsenum(s, &col);
321 * Video origin is [0, 0], terminal origin is [1, 1].
328 set_cursor_position(priv, row, col);
336 * Clear part/all screen:
337 * [J or [0J - clear screen from cursor down
338 * [1J - clear screen from cursor up
339 * [2J - clear entire screen
341 * TODO we really only handle entire-screen case, others
342 * probably require some additions to video-uclass (and
343 * are not really needed yet by efi_console)
345 parsenum(priv->escape_buf + 1, &mode);
348 video_clear(dev->parent);
349 video_sync(dev->parent, false);
351 priv->xcur_frac = priv->xstart_frac;
353 debug("unsupported clear mode: %d\n", mode);
358 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
362 * Clear (parts of) current line
363 * [0K - clear line to end
364 * [2K - clear entire line
366 parsenum(priv->escape_buf + 1, &mode);
371 get_cursor_position(priv, &row, &col);
372 vidconsole_set_row(dev, row, vid_priv->colour_bg);
377 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
378 char *s = priv->escape_buf;
379 char *end = &priv->escape_buf[priv->escape_len];
382 * Set graphics mode: [%d;...;%dm
384 * Currently only supports the color attributes:
413 s = parsenum(s, &val);
418 /* all attributes off */
419 video_set_default_colors(dev->parent, false);
423 vid_priv->fg_col_idx |= 8;
424 vid_priv->colour_fg = vid_console_color(
425 vid_priv, vid_priv->fg_col_idx);
429 vid_priv->colour_fg = vid_console_color(
430 vid_priv, vid_priv->bg_col_idx);
431 vid_priv->colour_bg = vid_console_color(
432 vid_priv, vid_priv->fg_col_idx);
435 /* foreground color */
436 vid_priv->fg_col_idx &= ~7;
437 vid_priv->fg_col_idx |= val - 30;
438 vid_priv->colour_fg = vid_console_color(
439 vid_priv, vid_priv->fg_col_idx);
442 /* background color, also mask the bold bit */
443 vid_priv->bg_col_idx &= ~0xf;
444 vid_priv->bg_col_idx |= val - 40;
445 vid_priv->colour_bg = vid_console_color(
446 vid_priv, vid_priv->bg_col_idx);
449 /* ignore unsupported SGR parameter */
457 debug("unrecognized escape sequence: %*s\n",
458 priv->escape_len, priv->escape_buf);
464 /* something went wrong, just revert to normal mode: */
468 /* Put that actual character on the screen (using the CP437 code page). */
469 static int vidconsole_output_glyph(struct udevice *dev, char ch)
471 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
475 * Failure of this function normally indicates an unsupported
476 * colour depth. Check this and return an error to help with
479 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
480 if (ret == -EAGAIN) {
481 vidconsole_newline(dev);
482 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
486 priv->xcur_frac += ret;
488 if (priv->xcur_frac >= priv->xsize_frac)
489 vidconsole_newline(dev);
494 int vidconsole_put_char(struct udevice *dev, char ch)
496 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
500 vidconsole_escape_char(dev, ch);
506 priv->escape_len = 0;
513 priv->xcur_frac = priv->xstart_frac;
516 vidconsole_newline(dev);
517 vidconsole_entry_start(dev);
519 case '\t': /* Tab (8 chars alignment) */
520 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
521 + 1) * priv->tab_width_frac;
523 if (priv->xcur_frac >= priv->xsize_frac)
524 vidconsole_newline(dev);
527 vidconsole_back(dev);
531 ret = vidconsole_output_glyph(dev, ch);
540 int vidconsole_put_string(struct udevice *dev, const char *str)
545 for (s = str; *s; s++) {
546 ret = vidconsole_put_char(dev, *s);
554 static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
556 struct udevice *dev = sdev->priv;
558 vidconsole_put_char(dev, ch);
559 video_sync(dev->parent, false);
562 static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
564 struct udevice *dev = sdev->priv;
566 vidconsole_put_string(dev, s);
567 video_sync(dev->parent, false);
570 /* Set up the number of rows and colours (rotated drivers override this) */
571 static int vidconsole_pre_probe(struct udevice *dev)
573 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
574 struct udevice *vid = dev->parent;
575 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
577 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
582 /* Register the device with stdio */
583 static int vidconsole_post_probe(struct udevice *dev)
585 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
586 struct stdio_dev *sdev = &priv->sdev;
588 if (!priv->tab_width_frac)
589 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
592 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
595 strcpy(sdev->name, "vidconsole");
598 sdev->flags = DEV_FLAGS_OUTPUT;
599 sdev->putc = vidconsole_putc;
600 sdev->puts = vidconsole_puts;
603 return stdio_register(sdev);
606 UCLASS_DRIVER(vidconsole) = {
607 .id = UCLASS_VIDEO_CONSOLE,
608 .name = "vidconsole0",
609 .pre_probe = vidconsole_pre_probe,
610 .post_probe = vidconsole_post_probe,
611 .per_device_auto_alloc_size = sizeof(struct vidconsole_priv),
614 void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
616 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
617 struct udevice *vid_dev = dev->parent;
618 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
620 col *= priv->x_charsize;
621 row *= priv->y_charsize;
622 priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
623 priv->ycur = min_t(short, row, vid_priv->ysize - 1);
626 static int do_video_setcursor(cmd_tbl_t *cmdtp, int flag, int argc,
629 unsigned int col, row;
633 return CMD_RET_USAGE;
635 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
636 return CMD_RET_FAILURE;
637 col = simple_strtoul(argv[1], NULL, 10);
638 row = simple_strtoul(argv[2], NULL, 10);
639 vidconsole_position_cursor(dev, col, row);
644 static int do_video_puts(cmd_tbl_t *cmdtp, int flag, int argc,
651 return CMD_RET_USAGE;
653 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
654 return CMD_RET_FAILURE;
655 for (s = argv[1]; *s; s++)
656 vidconsole_put_char(dev, *s);
658 video_sync(dev->parent, false);
664 setcurs, 3, 1, do_video_setcursor,
665 "set cursor position within screen",
666 " <col> <row> in character"
670 lcdputs, 2, 1, do_video_puts,
671 "print string on video framebuffer",