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
13 #include <linux/ctype.h>
16 #include <video_console.h>
17 #include <video_font.h> /* Bitmap font for code page 437 */
20 * Structure to describe a console color
28 /* By default we scroll by a single line */
29 #ifndef CONFIG_CONSOLE_SCROLL_LINES
30 #define CONFIG_CONSOLE_SCROLL_LINES 1
33 int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
35 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
39 return ops->putc_xy(dev, x, y, ch);
42 int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
45 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
49 return ops->move_rows(dev, rowdst, rowsrc, count);
52 int vidconsole_set_row(struct udevice *dev, uint row, int clr)
54 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
58 return ops->set_row(dev, row, clr);
61 static int vidconsole_entry_start(struct udevice *dev)
63 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
65 if (!ops->entry_start)
67 return ops->entry_start(dev);
70 /* Move backwards one space */
71 static int vidconsole_back(struct udevice *dev)
73 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
74 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
78 ret = ops->backspace(dev);
83 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
84 if (priv->xcur_frac < priv->xstart_frac) {
85 priv->xcur_frac = (priv->cols - 1) *
86 VID_TO_POS(priv->x_charsize);
87 priv->ycur -= priv->y_charsize;
91 video_sync(dev->parent, false);
96 /* Move to a newline, scrolling the display if necessary */
97 static void vidconsole_newline(struct udevice *dev)
99 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
100 struct udevice *vid_dev = dev->parent;
101 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
102 const int rows = CONFIG_CONSOLE_SCROLL_LINES;
105 priv->xcur_frac = priv->xstart_frac;
106 priv->ycur += priv->y_charsize;
108 /* Check if we need to scroll the terminal */
109 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
110 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
111 for (i = 0; i < rows; i++)
112 vidconsole_set_row(dev, priv->rows - i - 1,
113 vid_priv->colour_bg);
114 priv->ycur -= rows * priv->y_charsize;
118 video_sync(dev->parent, false);
121 static const struct vid_rgb colors[VID_COLOR_COUNT] = {
122 { 0x00, 0x00, 0x00 }, /* black */
123 { 0xc0, 0x00, 0x00 }, /* red */
124 { 0x00, 0xc0, 0x00 }, /* green */
125 { 0xc0, 0x60, 0x00 }, /* brown */
126 { 0x00, 0x00, 0xc0 }, /* blue */
127 { 0xc0, 0x00, 0xc0 }, /* magenta */
128 { 0x00, 0xc0, 0xc0 }, /* cyan */
129 { 0xc0, 0xc0, 0xc0 }, /* light gray */
130 { 0x80, 0x80, 0x80 }, /* gray */
131 { 0xff, 0x00, 0x00 }, /* bright red */
132 { 0x00, 0xff, 0x00 }, /* bright green */
133 { 0xff, 0xff, 0x00 }, /* yellow */
134 { 0x00, 0x00, 0xff }, /* bright blue */
135 { 0xff, 0x00, 0xff }, /* bright magenta */
136 { 0x00, 0xff, 0xff }, /* bright cyan */
137 { 0xff, 0xff, 0xff }, /* white */
140 u32 vid_console_color(struct video_priv *priv, unsigned int idx)
142 switch (priv->bpix) {
144 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
145 return ((colors[idx].r >> 3) << 11) |
146 ((colors[idx].g >> 2) << 5) |
147 ((colors[idx].b >> 3) << 0);
151 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
152 return (colors[idx].r << 16) |
153 (colors[idx].g << 8) |
154 (colors[idx].b << 0);
162 * For unknown bit arrangements just support
166 return 0xffffff; /* white */
168 return 0x000000; /* black */
171 static char *parsenum(char *s, int *num)
174 *num = simple_strtol(s, &end, 10);
179 * set_cursor_position() - set cursor position
181 * @priv: private data of the video console
185 static void set_cursor_position(struct vidconsole_priv *priv, int row, int col)
188 * Ensure we stay in the bounds of the screen.
190 if (row >= priv->rows)
191 row = priv->rows - 1;
192 if (col >= priv->cols)
193 col = priv->cols - 1;
195 priv->ycur = row * priv->y_charsize;
196 priv->xcur_frac = priv->xstart_frac +
197 VID_TO_POS(col * priv->x_charsize);
201 * get_cursor_position() - get cursor position
203 * @priv: private data of the video console
207 static void get_cursor_position(struct vidconsole_priv *priv,
210 *row = priv->ycur / priv->y_charsize;
211 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
216 * Process a character while accumulating an escape string. Chars are
217 * accumulated into escape_buf until the end of escape sequence is
218 * found, at which point the sequence is parsed and processed.
220 static void vidconsole_escape_char(struct udevice *dev, char ch)
222 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
224 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
227 /* Sanity checking for bogus ESC sequences: */
228 if (priv->escape_len >= sizeof(priv->escape_buf))
230 if (priv->escape_len == 0) {
233 /* Save cursor position */
234 get_cursor_position(priv, &priv->row_saved,
240 /* Restore cursor position */
241 int row = priv->row_saved;
242 int col = priv->col_saved;
244 set_cursor_position(priv, row, col);
255 priv->escape_buf[priv->escape_len++] = ch;
258 * Escape sequences are terminated by a letter, so keep
259 * accumulating until we get one:
265 * clear escape mode first, otherwise things will get highly
266 * surprising if you hit any debug prints that come back to
279 char *s = priv->escape_buf;
282 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
283 * Cursor left/right: [%dD, [%dC
286 s = parsenum(s, &num);
287 if (num == 0) /* No digit in sequence ... */
288 num = 1; /* ... means "move by 1". */
290 get_cursor_position(priv, &row, &col);
291 if (ch == 'A' || ch == 'F')
297 if (ch == 'B' || ch == 'E')
299 if (ch == 'E' || ch == 'F')
305 /* Right and bottom overflows are handled in the callee. */
306 set_cursor_position(priv, row, col);
312 char *s = priv->escape_buf;
315 * Set cursor position: [%d;%df or [%d;%dH
318 s = parsenum(s, &row);
320 s = parsenum(s, &col);
323 * Video origin is [0, 0], terminal origin is [1, 1].
330 set_cursor_position(priv, row, col);
338 * Clear part/all screen:
339 * [J or [0J - clear screen from cursor down
340 * [1J - clear screen from cursor up
341 * [2J - clear entire screen
343 * TODO we really only handle entire-screen case, others
344 * probably require some additions to video-uclass (and
345 * are not really needed yet by efi_console)
347 parsenum(priv->escape_buf + 1, &mode);
350 video_clear(dev->parent);
351 video_sync(dev->parent, false);
353 priv->xcur_frac = priv->xstart_frac;
355 debug("unsupported clear mode: %d\n", mode);
360 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
364 * Clear (parts of) current line
365 * [0K - clear line to end
366 * [2K - clear entire line
368 parsenum(priv->escape_buf + 1, &mode);
373 get_cursor_position(priv, &row, &col);
374 vidconsole_set_row(dev, row, vid_priv->colour_bg);
379 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
380 char *s = priv->escape_buf;
381 char *end = &priv->escape_buf[priv->escape_len];
384 * Set graphics mode: [%d;...;%dm
386 * Currently only supports the color attributes:
415 s = parsenum(s, &val);
420 /* all attributes off */
421 video_set_default_colors(dev->parent, false);
425 vid_priv->fg_col_idx |= 8;
426 vid_priv->colour_fg = vid_console_color(
427 vid_priv, vid_priv->fg_col_idx);
431 vid_priv->colour_fg = vid_console_color(
432 vid_priv, vid_priv->bg_col_idx);
433 vid_priv->colour_bg = vid_console_color(
434 vid_priv, vid_priv->fg_col_idx);
437 /* foreground color */
438 vid_priv->fg_col_idx &= ~7;
439 vid_priv->fg_col_idx |= val - 30;
440 vid_priv->colour_fg = vid_console_color(
441 vid_priv, vid_priv->fg_col_idx);
444 /* background color, also mask the bold bit */
445 vid_priv->bg_col_idx &= ~0xf;
446 vid_priv->bg_col_idx |= val - 40;
447 vid_priv->colour_bg = vid_console_color(
448 vid_priv, vid_priv->bg_col_idx);
451 /* ignore unsupported SGR parameter */
459 debug("unrecognized escape sequence: %*s\n",
460 priv->escape_len, priv->escape_buf);
466 /* something went wrong, just revert to normal mode: */
470 /* Put that actual character on the screen (using the CP437 code page). */
471 static int vidconsole_output_glyph(struct udevice *dev, char ch)
473 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
477 * Failure of this function normally indicates an unsupported
478 * colour depth. Check this and return an error to help with
481 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
482 if (ret == -EAGAIN) {
483 vidconsole_newline(dev);
484 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
488 priv->xcur_frac += ret;
490 if (priv->xcur_frac >= priv->xsize_frac)
491 vidconsole_newline(dev);
496 int vidconsole_put_char(struct udevice *dev, char ch)
498 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
502 vidconsole_escape_char(dev, ch);
508 priv->escape_len = 0;
515 priv->xcur_frac = priv->xstart_frac;
518 vidconsole_newline(dev);
519 vidconsole_entry_start(dev);
521 case '\t': /* Tab (8 chars alignment) */
522 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
523 + 1) * priv->tab_width_frac;
525 if (priv->xcur_frac >= priv->xsize_frac)
526 vidconsole_newline(dev);
529 vidconsole_back(dev);
533 ret = vidconsole_output_glyph(dev, ch);
542 int vidconsole_put_string(struct udevice *dev, const char *str)
547 for (s = str; *s; s++) {
548 ret = vidconsole_put_char(dev, *s);
556 static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
558 struct udevice *dev = sdev->priv;
560 vidconsole_put_char(dev, ch);
561 video_sync(dev->parent, false);
564 static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
566 struct udevice *dev = sdev->priv;
568 vidconsole_put_string(dev, s);
569 video_sync(dev->parent, false);
572 /* Set up the number of rows and colours (rotated drivers override this) */
573 static int vidconsole_pre_probe(struct udevice *dev)
575 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
576 struct udevice *vid = dev->parent;
577 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
579 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
584 /* Register the device with stdio */
585 static int vidconsole_post_probe(struct udevice *dev)
587 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
588 struct stdio_dev *sdev = &priv->sdev;
590 if (!priv->tab_width_frac)
591 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
594 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
597 strcpy(sdev->name, "vidconsole");
600 sdev->flags = DEV_FLAGS_OUTPUT;
601 sdev->putc = vidconsole_putc;
602 sdev->puts = vidconsole_puts;
605 return stdio_register(sdev);
608 UCLASS_DRIVER(vidconsole) = {
609 .id = UCLASS_VIDEO_CONSOLE,
610 .name = "vidconsole0",
611 .pre_probe = vidconsole_pre_probe,
612 .post_probe = vidconsole_post_probe,
613 .per_device_auto_alloc_size = sizeof(struct vidconsole_priv),
616 #if CONFIG_IS_ENABLED(CMD_VIDCONSOLE)
617 void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
619 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
620 struct udevice *vid_dev = dev->parent;
621 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
623 col *= priv->x_charsize;
624 row *= priv->y_charsize;
625 priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
626 priv->xstart_frac = priv->xcur_frac;
627 priv->ycur = min_t(short, row, vid_priv->ysize - 1);
630 static int do_video_setcursor(struct cmd_tbl *cmdtp, int flag, int argc,
633 unsigned int col, row;
637 return CMD_RET_USAGE;
639 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
640 return CMD_RET_FAILURE;
641 col = simple_strtoul(argv[1], NULL, 10);
642 row = simple_strtoul(argv[2], NULL, 10);
643 vidconsole_position_cursor(dev, col, row);
648 static int do_video_puts(struct cmd_tbl *cmdtp, int flag, int argc,
655 return CMD_RET_USAGE;
657 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
658 return CMD_RET_FAILURE;
659 for (s = argv[1]; *s; s++)
660 vidconsole_put_char(dev, *s);
662 video_sync(dev->parent, false);
668 setcurs, 3, 1, do_video_setcursor,
669 "set cursor position within screen",
670 " <col> <row> in character"
674 lcdputs, 2, 1, do_video_puts,
675 "print string on video framebuffer",
678 #endif /* CONFIG_IS_ENABLED(CMD_VIDCONSOLE) */