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);
148 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
149 return (colors[idx].r << 16) |
150 (colors[idx].g << 8) |
151 (colors[idx].b << 0);
155 * For unknown bit arrangements just support
159 return 0xffffff; /* white */
161 return 0x000000; /* black */
165 static char *parsenum(char *s, int *num)
168 *num = simple_strtol(s, &end, 10);
173 * set_cursor_position() - set cursor position
175 * @priv: private data of the video console
179 static void set_cursor_position(struct vidconsole_priv *priv, int row, int col)
182 * Ensure we stay in the bounds of the screen.
184 if (row >= priv->rows)
185 row = priv->rows - 1;
186 if (col >= priv->cols)
187 col = priv->cols - 1;
189 priv->ycur = row * priv->y_charsize;
190 priv->xcur_frac = priv->xstart_frac +
191 VID_TO_POS(col * priv->x_charsize);
195 * get_cursor_position() - get cursor position
197 * @priv: private data of the video console
201 static void get_cursor_position(struct vidconsole_priv *priv,
204 *row = priv->ycur / priv->y_charsize;
205 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
210 * Process a character while accumulating an escape string. Chars are
211 * accumulated into escape_buf until the end of escape sequence is
212 * found, at which point the sequence is parsed and processed.
214 static void vidconsole_escape_char(struct udevice *dev, char ch)
216 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
218 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
221 /* Sanity checking for bogus ESC sequences: */
222 if (priv->escape_len >= sizeof(priv->escape_buf))
224 if (priv->escape_len == 0) {
227 /* Save cursor position */
228 get_cursor_position(priv, &priv->row_saved,
234 /* Restore cursor position */
235 int row = priv->row_saved;
236 int col = priv->col_saved;
238 set_cursor_position(priv, row, col);
249 priv->escape_buf[priv->escape_len++] = ch;
252 * Escape sequences are terminated by a letter, so keep
253 * accumulating until we get one:
259 * clear escape mode first, otherwise things will get highly
260 * surprising if you hit any debug prints that come back to
273 char *s = priv->escape_buf;
276 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
277 * Cursor left/right: [%dD, [%dC
280 s = parsenum(s, &num);
281 if (num == 0) /* No digit in sequence ... */
282 num = 1; /* ... means "move by 1". */
284 get_cursor_position(priv, &row, &col);
285 if (ch == 'A' || ch == 'F')
291 if (ch == 'B' || ch == 'E')
293 if (ch == 'E' || ch == 'F')
299 /* Right and bottom overflows are handled in the callee. */
300 set_cursor_position(priv, row, col);
306 char *s = priv->escape_buf;
309 * Set cursor position: [%d;%df or [%d;%dH
312 s = parsenum(s, &row);
314 s = parsenum(s, &col);
317 * Video origin is [0, 0], terminal origin is [1, 1].
324 set_cursor_position(priv, row, col);
332 * Clear part/all screen:
333 * [J or [0J - clear screen from cursor down
334 * [1J - clear screen from cursor up
335 * [2J - clear entire screen
337 * TODO we really only handle entire-screen case, others
338 * probably require some additions to video-uclass (and
339 * are not really needed yet by efi_console)
341 parsenum(priv->escape_buf + 1, &mode);
344 video_clear(dev->parent);
345 video_sync(dev->parent, false);
347 priv->xcur_frac = priv->xstart_frac;
349 debug("unsupported clear mode: %d\n", mode);
354 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
358 * Clear (parts of) current line
359 * [0K - clear line to end
360 * [2K - clear entire line
362 parsenum(priv->escape_buf + 1, &mode);
367 get_cursor_position(priv, &row, &col);
368 vidconsole_set_row(dev, row, vid_priv->colour_bg);
373 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
374 char *s = priv->escape_buf;
375 char *end = &priv->escape_buf[priv->escape_len];
378 * Set graphics mode: [%d;...;%dm
380 * Currently only supports the color attributes:
409 s = parsenum(s, &val);
414 /* all attributes off */
415 video_set_default_colors(dev->parent, false);
419 vid_priv->fg_col_idx |= 8;
420 vid_priv->colour_fg = vid_console_color(
421 vid_priv, vid_priv->fg_col_idx);
425 vid_priv->colour_fg = vid_console_color(
426 vid_priv, vid_priv->bg_col_idx);
427 vid_priv->colour_bg = vid_console_color(
428 vid_priv, vid_priv->fg_col_idx);
431 /* foreground color */
432 vid_priv->fg_col_idx &= ~7;
433 vid_priv->fg_col_idx |= val - 30;
434 vid_priv->colour_fg = vid_console_color(
435 vid_priv, vid_priv->fg_col_idx);
438 /* background color, also mask the bold bit */
439 vid_priv->bg_col_idx &= ~0xf;
440 vid_priv->bg_col_idx |= val - 40;
441 vid_priv->colour_bg = vid_console_color(
442 vid_priv, vid_priv->bg_col_idx);
445 /* ignore unsupported SGR parameter */
453 debug("unrecognized escape sequence: %*s\n",
454 priv->escape_len, priv->escape_buf);
460 /* something went wrong, just revert to normal mode: */
464 /* Put that actual character on the screen (using the CP437 code page). */
465 static int vidconsole_output_glyph(struct udevice *dev, char ch)
467 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
471 * Failure of this function normally indicates an unsupported
472 * colour depth. Check this and return an error to help with
475 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
476 if (ret == -EAGAIN) {
477 vidconsole_newline(dev);
478 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
482 priv->xcur_frac += ret;
484 if (priv->xcur_frac >= priv->xsize_frac)
485 vidconsole_newline(dev);
490 int vidconsole_put_char(struct udevice *dev, char ch)
492 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
496 vidconsole_escape_char(dev, ch);
502 priv->escape_len = 0;
509 priv->xcur_frac = priv->xstart_frac;
512 vidconsole_newline(dev);
513 vidconsole_entry_start(dev);
515 case '\t': /* Tab (8 chars alignment) */
516 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
517 + 1) * priv->tab_width_frac;
519 if (priv->xcur_frac >= priv->xsize_frac)
520 vidconsole_newline(dev);
523 vidconsole_back(dev);
527 ret = vidconsole_output_glyph(dev, ch);
536 int vidconsole_put_string(struct udevice *dev, const char *str)
541 for (s = str; *s; s++) {
542 ret = vidconsole_put_char(dev, *s);
550 static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
552 struct udevice *dev = sdev->priv;
554 vidconsole_put_char(dev, ch);
555 video_sync(dev->parent, false);
558 static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
560 struct udevice *dev = sdev->priv;
562 vidconsole_put_string(dev, s);
563 video_sync(dev->parent, false);
566 /* Set up the number of rows and colours (rotated drivers override this) */
567 static int vidconsole_pre_probe(struct udevice *dev)
569 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
570 struct udevice *vid = dev->parent;
571 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
573 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
578 /* Register the device with stdio */
579 static int vidconsole_post_probe(struct udevice *dev)
581 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
582 struct stdio_dev *sdev = &priv->sdev;
584 if (!priv->tab_width_frac)
585 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
588 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
591 strcpy(sdev->name, "vidconsole");
594 sdev->flags = DEV_FLAGS_OUTPUT;
595 sdev->putc = vidconsole_putc;
596 sdev->puts = vidconsole_puts;
599 return stdio_register(sdev);
602 UCLASS_DRIVER(vidconsole) = {
603 .id = UCLASS_VIDEO_CONSOLE,
604 .name = "vidconsole0",
605 .pre_probe = vidconsole_pre_probe,
606 .post_probe = vidconsole_post_probe,
607 .per_device_auto_alloc_size = sizeof(struct vidconsole_priv),
610 void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
612 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
613 struct udevice *vid_dev = dev->parent;
614 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
616 col *= priv->x_charsize;
617 row *= priv->y_charsize;
618 priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
619 priv->ycur = min_t(short, row, vid_priv->ysize - 1);
622 static int do_video_setcursor(cmd_tbl_t *cmdtp, int flag, int argc,
625 unsigned int col, row;
629 return CMD_RET_USAGE;
631 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
632 return CMD_RET_FAILURE;
633 col = simple_strtoul(argv[1], NULL, 10);
634 row = simple_strtoul(argv[2], NULL, 10);
635 vidconsole_position_cursor(dev, col, row);
640 static int do_video_puts(cmd_tbl_t *cmdtp, int flag, int argc,
647 return CMD_RET_USAGE;
649 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
650 return CMD_RET_FAILURE;
651 for (s = argv[1]; *s; s++)
652 vidconsole_put_char(dev, *s);
654 video_sync(dev->parent, false);
660 setcurs, 3, 1, do_video_setcursor,
661 "set cursor position within screen",
662 " <col> <row> in character"
666 lcdputs, 2, 1, do_video_puts,
667 "print string on video framebuffer",