1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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
8  */
9 
10 #define LOG_CATEGORY UCLASS_VIDEO_CONSOLE
11 
12 #include <common.h>
13 #include <command.h>
14 #include <console.h>
15 #include <log.h>
16 #include <dm.h>
17 #include <video.h>
18 #include <video_console.h>
19 #include <video_font.h>		/* Bitmap font for code page 437 */
20 #include <linux/ctype.h>
21 
vidconsole_putc_xy(struct udevice * dev,uint x,uint y,char ch)22 int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
23 {
24 	struct vidconsole_ops *ops = vidconsole_get_ops(dev);
25 
26 	if (!ops->putc_xy)
27 		return -ENOSYS;
28 	return ops->putc_xy(dev, x, y, ch);
29 }
30 
vidconsole_move_rows(struct udevice * dev,uint rowdst,uint rowsrc,uint count)31 int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
32 			 uint count)
33 {
34 	struct vidconsole_ops *ops = vidconsole_get_ops(dev);
35 
36 	if (!ops->move_rows)
37 		return -ENOSYS;
38 	return ops->move_rows(dev, rowdst, rowsrc, count);
39 }
40 
vidconsole_set_row(struct udevice * dev,uint row,int clr)41 int vidconsole_set_row(struct udevice *dev, uint row, int clr)
42 {
43 	struct vidconsole_ops *ops = vidconsole_get_ops(dev);
44 
45 	if (!ops->set_row)
46 		return -ENOSYS;
47 	return ops->set_row(dev, row, clr);
48 }
49 
vidconsole_entry_start(struct udevice * dev)50 static int vidconsole_entry_start(struct udevice *dev)
51 {
52 	struct vidconsole_ops *ops = vidconsole_get_ops(dev);
53 
54 	if (!ops->entry_start)
55 		return -ENOSYS;
56 	return ops->entry_start(dev);
57 }
58 
59 /* Move backwards one space */
vidconsole_back(struct udevice * dev)60 static int vidconsole_back(struct udevice *dev)
61 {
62 	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
63 	struct vidconsole_ops *ops = vidconsole_get_ops(dev);
64 	int ret;
65 
66 	if (ops->backspace) {
67 		ret = ops->backspace(dev);
68 		if (ret != -ENOSYS)
69 			return ret;
70 	}
71 
72 	priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
73 	if (priv->xcur_frac < priv->xstart_frac) {
74 		priv->xcur_frac = (priv->cols - 1) *
75 			VID_TO_POS(priv->x_charsize);
76 		priv->ycur -= priv->y_charsize;
77 		if (priv->ycur < 0)
78 			priv->ycur = 0;
79 	}
80 	return video_sync(dev->parent, false);
81 }
82 
83 /* Move to a newline, scrolling the display if necessary */
vidconsole_newline(struct udevice * dev)84 static void vidconsole_newline(struct udevice *dev)
85 {
86 	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
87 	struct udevice *vid_dev = dev->parent;
88 	struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
89 	const int rows = CONFIG_VAL(CONSOLE_SCROLL_LINES);
90 	int i, ret;
91 
92 	priv->xcur_frac = priv->xstart_frac;
93 	priv->ycur += priv->y_charsize;
94 
95 	/* Check if we need to scroll the terminal */
96 	if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
97 		vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
98 		for (i = 0; i < rows; i++)
99 			vidconsole_set_row(dev, priv->rows - i - 1,
100 					   vid_priv->colour_bg);
101 		priv->ycur -= rows * priv->y_charsize;
102 	}
103 	priv->last_ch = 0;
104 
105 	ret = video_sync(dev->parent, false);
106 	if (ret) {
107 #ifdef DEBUG
108 		console_puts_select_stderr(true, "[vc err: video_sync]");
109 #endif
110 	}
111 }
112 
parsenum(char * s,int * num)113 static char *parsenum(char *s, int *num)
114 {
115 	char *end;
116 	*num = simple_strtol(s, &end, 10);
117 	return end;
118 }
119 
vidconsole_set_cursor_pos(struct udevice * dev,int x,int y)120 void vidconsole_set_cursor_pos(struct udevice *dev, int x, int y)
121 {
122 	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
123 
124 	priv->xcur_frac = VID_TO_POS(x);
125 	priv->xstart_frac = priv->xcur_frac;
126 	priv->ycur = y;
127 }
128 
129 /**
130  * set_cursor_position() - set cursor position
131  *
132  * @priv:	private data of the video console
133  * @row:	new row
134  * @col:	new column
135  */
set_cursor_position(struct vidconsole_priv * priv,int row,int col)136 static void set_cursor_position(struct vidconsole_priv *priv, int row, int col)
137 {
138 	/*
139 	 * Ensure we stay in the bounds of the screen.
140 	 */
141 	if (row >= priv->rows)
142 		row = priv->rows - 1;
143 	if (col >= priv->cols)
144 		col = priv->cols - 1;
145 
146 	priv->ycur = row * priv->y_charsize;
147 	priv->xcur_frac = priv->xstart_frac +
148 			  VID_TO_POS(col * priv->x_charsize);
149 }
150 
151 /**
152  * get_cursor_position() - get cursor position
153  *
154  * @priv:	private data of the video console
155  * @row:	row
156  * @col:	column
157  */
get_cursor_position(struct vidconsole_priv * priv,int * row,int * col)158 static void get_cursor_position(struct vidconsole_priv *priv,
159 				int *row, int *col)
160 {
161 	*row = priv->ycur / priv->y_charsize;
162 	*col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
163 	       priv->x_charsize;
164 }
165 
166 /*
167  * Process a character while accumulating an escape string.  Chars are
168  * accumulated into escape_buf until the end of escape sequence is
169  * found, at which point the sequence is parsed and processed.
170  */
vidconsole_escape_char(struct udevice * dev,char ch)171 static void vidconsole_escape_char(struct udevice *dev, char ch)
172 {
173 	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
174 
175 	if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
176 		goto error;
177 
178 	/* Sanity checking for bogus ESC sequences: */
179 	if (priv->escape_len >= sizeof(priv->escape_buf))
180 		goto error;
181 	if (priv->escape_len == 0) {
182 		switch (ch) {
183 		case '7':
184 			/* Save cursor position */
185 			get_cursor_position(priv, &priv->row_saved,
186 					    &priv->col_saved);
187 			priv->escape = 0;
188 
189 			return;
190 		case '8': {
191 			/* Restore cursor position */
192 			int row = priv->row_saved;
193 			int col = priv->col_saved;
194 
195 			set_cursor_position(priv, row, col);
196 			priv->escape = 0;
197 			return;
198 		}
199 		case '[':
200 			break;
201 		default:
202 			goto error;
203 		}
204 	}
205 
206 	priv->escape_buf[priv->escape_len++] = ch;
207 
208 	/*
209 	 * Escape sequences are terminated by a letter, so keep
210 	 * accumulating until we get one:
211 	 */
212 	if (!isalpha(ch))
213 		return;
214 
215 	/*
216 	 * clear escape mode first, otherwise things will get highly
217 	 * surprising if you hit any debug prints that come back to
218 	 * this console.
219 	 */
220 	priv->escape = 0;
221 
222 	switch (ch) {
223 	case 'A':
224 	case 'B':
225 	case 'C':
226 	case 'D':
227 	case 'E':
228 	case 'F': {
229 		int row, col, num;
230 		char *s = priv->escape_buf;
231 
232 		/*
233 		 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
234 		 * Cursor left/right: [%dD, [%dC
235 		 */
236 		s++;    /* [ */
237 		s = parsenum(s, &num);
238 		if (num == 0)			/* No digit in sequence ... */
239 			num = 1;		/* ... means "move by 1". */
240 
241 		get_cursor_position(priv, &row, &col);
242 		if (ch == 'A' || ch == 'F')
243 			row -= num;
244 		if (ch == 'C')
245 			col += num;
246 		if (ch == 'D')
247 			col -= num;
248 		if (ch == 'B' || ch == 'E')
249 			row += num;
250 		if (ch == 'E' || ch == 'F')
251 			col = 0;
252 		if (col < 0)
253 			col = 0;
254 		if (row < 0)
255 			row = 0;
256 		/* Right and bottom overflows are handled in the callee. */
257 		set_cursor_position(priv, row, col);
258 		break;
259 	}
260 	case 'H':
261 	case 'f': {
262 		int row, col;
263 		char *s = priv->escape_buf;
264 
265 		/*
266 		 * Set cursor position: [%d;%df or [%d;%dH
267 		 */
268 		s++;    /* [ */
269 		s = parsenum(s, &row);
270 		s++;    /* ; */
271 		s = parsenum(s, &col);
272 
273 		/*
274 		 * Video origin is [0, 0], terminal origin is [1, 1].
275 		 */
276 		if (row)
277 			--row;
278 		if (col)
279 			--col;
280 
281 		set_cursor_position(priv, row, col);
282 
283 		break;
284 	}
285 	case 'J': {
286 		int mode;
287 
288 		/*
289 		 * Clear part/all screen:
290 		 *   [J or [0J - clear screen from cursor down
291 		 *   [1J       - clear screen from cursor up
292 		 *   [2J       - clear entire screen
293 		 *
294 		 * TODO we really only handle entire-screen case, others
295 		 * probably require some additions to video-uclass (and
296 		 * are not really needed yet by efi_console)
297 		 */
298 		parsenum(priv->escape_buf + 1, &mode);
299 
300 		if (mode == 2) {
301 			int ret;
302 
303 			video_clear(dev->parent);
304 			ret = video_sync(dev->parent, false);
305 			if (ret) {
306 #ifdef DEBUG
307 				console_puts_select_stderr(true, "[vc err: video_sync]");
308 #endif
309 			}
310 			priv->ycur = 0;
311 			priv->xcur_frac = priv->xstart_frac;
312 		} else {
313 			debug("unsupported clear mode: %d\n", mode);
314 		}
315 		break;
316 	}
317 	case 'K': {
318 		struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
319 		int mode;
320 
321 		/*
322 		 * Clear (parts of) current line
323 		 *   [0K       - clear line to end
324 		 *   [2K       - clear entire line
325 		 */
326 		parsenum(priv->escape_buf + 1, &mode);
327 
328 		if (mode == 2) {
329 			int row, col;
330 
331 			get_cursor_position(priv, &row, &col);
332 			vidconsole_set_row(dev, row, vid_priv->colour_bg);
333 		}
334 		break;
335 	}
336 	case 'm': {
337 		struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
338 		char *s = priv->escape_buf;
339 		char *end = &priv->escape_buf[priv->escape_len];
340 
341 		/*
342 		 * Set graphics mode: [%d;...;%dm
343 		 *
344 		 * Currently only supports the color attributes:
345 		 *
346 		 * Foreground Colors:
347 		 *
348 		 *   30	Black
349 		 *   31	Red
350 		 *   32	Green
351 		 *   33	Yellow
352 		 *   34	Blue
353 		 *   35	Magenta
354 		 *   36	Cyan
355 		 *   37	White
356 		 *
357 		 * Background Colors:
358 		 *
359 		 *   40	Black
360 		 *   41	Red
361 		 *   42	Green
362 		 *   43	Yellow
363 		 *   44	Blue
364 		 *   45	Magenta
365 		 *   46	Cyan
366 		 *   47	White
367 		 */
368 
369 		s++;    /* [ */
370 		while (s < end) {
371 			int val;
372 
373 			s = parsenum(s, &val);
374 			s++;
375 
376 			switch (val) {
377 			case 0:
378 				/* all attributes off */
379 				video_set_default_colors(dev->parent, false);
380 				break;
381 			case 1:
382 				/* bold */
383 				vid_priv->fg_col_idx |= 8;
384 				vid_priv->colour_fg = video_index_to_colour(
385 						vid_priv, vid_priv->fg_col_idx);
386 				break;
387 			case 7:
388 				/* reverse video */
389 				vid_priv->colour_fg = video_index_to_colour(
390 						vid_priv, vid_priv->bg_col_idx);
391 				vid_priv->colour_bg = video_index_to_colour(
392 						vid_priv, vid_priv->fg_col_idx);
393 				break;
394 			case 30 ... 37:
395 				/* foreground color */
396 				vid_priv->fg_col_idx &= ~7;
397 				vid_priv->fg_col_idx |= val - 30;
398 				vid_priv->colour_fg = video_index_to_colour(
399 						vid_priv, vid_priv->fg_col_idx);
400 				break;
401 			case 40 ... 47:
402 				/* background color, also mask the bold bit */
403 				vid_priv->bg_col_idx &= ~0xf;
404 				vid_priv->bg_col_idx |= val - 40;
405 				vid_priv->colour_bg = video_index_to_colour(
406 						vid_priv, vid_priv->bg_col_idx);
407 				break;
408 			default:
409 				/* ignore unsupported SGR parameter */
410 				break;
411 			}
412 		}
413 
414 		break;
415 	}
416 	default:
417 		debug("unrecognized escape sequence: %*s\n",
418 		      priv->escape_len, priv->escape_buf);
419 	}
420 
421 	return;
422 
423 error:
424 	/* something went wrong, just revert to normal mode: */
425 	priv->escape = 0;
426 }
427 
428 /* Put that actual character on the screen (using the CP437 code page). */
vidconsole_output_glyph(struct udevice * dev,char ch)429 static int vidconsole_output_glyph(struct udevice *dev, char ch)
430 {
431 	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
432 	int ret;
433 
434 	/*
435 	 * Failure of this function normally indicates an unsupported
436 	 * colour depth. Check this and return an error to help with
437 	 * diagnosis.
438 	 */
439 	ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
440 	if (ret == -EAGAIN) {
441 		vidconsole_newline(dev);
442 		ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
443 	}
444 	if (ret < 0)
445 		return ret;
446 	priv->xcur_frac += ret;
447 	priv->last_ch = ch;
448 	if (priv->xcur_frac >= priv->xsize_frac)
449 		vidconsole_newline(dev);
450 
451 	return 0;
452 }
453 
vidconsole_put_char(struct udevice * dev,char ch)454 int vidconsole_put_char(struct udevice *dev, char ch)
455 {
456 	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
457 	int ret;
458 
459 	if (priv->escape) {
460 		vidconsole_escape_char(dev, ch);
461 		return 0;
462 	}
463 
464 	switch (ch) {
465 	case '\x1b':
466 		priv->escape_len = 0;
467 		priv->escape = 1;
468 		break;
469 	case '\a':
470 		/* beep */
471 		break;
472 	case '\r':
473 		priv->xcur_frac = priv->xstart_frac;
474 		break;
475 	case '\n':
476 		vidconsole_newline(dev);
477 		vidconsole_entry_start(dev);
478 		break;
479 	case '\t':	/* Tab (8 chars alignment) */
480 		priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
481 				+ 1) * priv->tab_width_frac;
482 
483 		if (priv->xcur_frac >= priv->xsize_frac)
484 			vidconsole_newline(dev);
485 		break;
486 	case '\b':
487 		vidconsole_back(dev);
488 		priv->last_ch = 0;
489 		break;
490 	default:
491 		ret = vidconsole_output_glyph(dev, ch);
492 		if (ret < 0)
493 			return ret;
494 		break;
495 	}
496 
497 	return 0;
498 }
499 
vidconsole_put_string(struct udevice * dev,const char * str)500 int vidconsole_put_string(struct udevice *dev, const char *str)
501 {
502 	const char *s;
503 	int ret;
504 
505 	for (s = str; *s; s++) {
506 		ret = vidconsole_put_char(dev, *s);
507 		if (ret)
508 			return ret;
509 	}
510 
511 	return 0;
512 }
513 
vidconsole_putc(struct stdio_dev * sdev,const char ch)514 static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
515 {
516 	struct udevice *dev = sdev->priv;
517 	int ret;
518 
519 	ret = vidconsole_put_char(dev, ch);
520 	if (ret) {
521 #ifdef DEBUG
522 		console_puts_select_stderr(true, "[vc err: putc]");
523 #endif
524 	}
525 	ret = video_sync(dev->parent, false);
526 	if (ret) {
527 #ifdef DEBUG
528 		console_puts_select_stderr(true, "[vc err: video_sync]");
529 #endif
530 	}
531 }
532 
vidconsole_puts(struct stdio_dev * sdev,const char * s)533 static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
534 {
535 	struct udevice *dev = sdev->priv;
536 	int ret;
537 
538 	ret = vidconsole_put_string(dev, s);
539 	if (ret) {
540 #ifdef DEBUG
541 		char str[30];
542 
543 		snprintf(str, sizeof(str), "[vc err: puts %d]", ret);
544 		console_puts_select_stderr(true, str);
545 #endif
546 	}
547 	ret = video_sync(dev->parent, false);
548 	if (ret) {
549 #ifdef DEBUG
550 		console_puts_select_stderr(true, "[vc err: video_sync]");
551 #endif
552 	}
553 }
554 
vidconsole_list_fonts(struct udevice * dev)555 void vidconsole_list_fonts(struct udevice *dev)
556 {
557 	struct vidfont_info info;
558 	int ret, i;
559 
560 	for (i = 0, ret = 0; !ret; i++) {
561 		ret = vidconsole_get_font(dev, i, &info);
562 		if (!ret)
563 			printf("%s\n", info.name);
564 	}
565 }
566 
vidconsole_get_font(struct udevice * dev,int seq,struct vidfont_info * info)567 int vidconsole_get_font(struct udevice *dev, int seq,
568 			struct vidfont_info *info)
569 {
570 	struct vidconsole_ops *ops = vidconsole_get_ops(dev);
571 
572 	if (!ops->get_font)
573 		return -ENOSYS;
574 
575 	return ops->get_font(dev, seq, info);
576 }
577 
vidconsole_get_font_size(struct udevice * dev,const char ** name,uint * sizep)578 int vidconsole_get_font_size(struct udevice *dev, const char **name, uint *sizep)
579 {
580 	struct vidconsole_ops *ops = vidconsole_get_ops(dev);
581 
582 	if (!ops->get_font_size)
583 		return -ENOSYS;
584 
585 	*name = ops->get_font_size(dev, sizep);
586 	return 0;
587 }
588 
vidconsole_select_font(struct udevice * dev,const char * name,uint size)589 int vidconsole_select_font(struct udevice *dev, const char *name, uint size)
590 {
591 	struct vidconsole_ops *ops = vidconsole_get_ops(dev);
592 
593 	if (!ops->select_font)
594 		return -ENOSYS;
595 
596 	return ops->select_font(dev, name, size);
597 }
598 
599 /* Set up the number of rows and colours (rotated drivers override this) */
vidconsole_pre_probe(struct udevice * dev)600 static int vidconsole_pre_probe(struct udevice *dev)
601 {
602 	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
603 	struct udevice *vid = dev->parent;
604 	struct video_priv *vid_priv = dev_get_uclass_priv(vid);
605 
606 	priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
607 
608 	return 0;
609 }
610 
611 /* Register the device with stdio */
vidconsole_post_probe(struct udevice * dev)612 static int vidconsole_post_probe(struct udevice *dev)
613 {
614 	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
615 	struct stdio_dev *sdev = &priv->sdev;
616 
617 	if (!priv->tab_width_frac)
618 		priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
619 
620 	if (dev_seq(dev)) {
621 		snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
622 			 dev_seq(dev));
623 	} else {
624 		strcpy(sdev->name, "vidconsole");
625 	}
626 
627 	sdev->flags = DEV_FLAGS_OUTPUT;
628 	sdev->putc = vidconsole_putc;
629 	sdev->puts = vidconsole_puts;
630 	sdev->priv = dev;
631 
632 	return stdio_register(sdev);
633 }
634 
635 UCLASS_DRIVER(vidconsole) = {
636 	.id		= UCLASS_VIDEO_CONSOLE,
637 	.name		= "vidconsole0",
638 	.pre_probe	= vidconsole_pre_probe,
639 	.post_probe	= vidconsole_post_probe,
640 	.per_device_auto	= sizeof(struct vidconsole_priv),
641 };
642 
643 #ifdef CONFIG_VIDEO_COPY
vidconsole_sync_copy(struct udevice * dev,void * from,void * to)644 int vidconsole_sync_copy(struct udevice *dev, void *from, void *to)
645 {
646 	struct udevice *vid = dev_get_parent(dev);
647 
648 	return video_sync_copy(vid, from, to);
649 }
650 
vidconsole_memmove(struct udevice * dev,void * dst,const void * src,int size)651 int vidconsole_memmove(struct udevice *dev, void *dst, const void *src,
652 		       int size)
653 {
654 	memmove(dst, src, size);
655 	return vidconsole_sync_copy(dev, dst, dst + size);
656 }
657 #endif
658 
vidconsole_clear_and_reset(struct udevice * dev)659 int vidconsole_clear_and_reset(struct udevice *dev)
660 {
661 	int ret;
662 
663 	ret = video_clear(dev_get_parent(dev));
664 	if (ret)
665 		return ret;
666 	vidconsole_position_cursor(dev, 0, 0);
667 
668 	return 0;
669 }
670 
vidconsole_position_cursor(struct udevice * dev,unsigned col,unsigned row)671 void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
672 {
673 	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
674 	struct udevice *vid_dev = dev->parent;
675 	struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
676 	short x, y;
677 
678 	x = min_t(short, col * priv->x_charsize, vid_priv->xsize - 1);
679 	y = min_t(short, row * priv->y_charsize, vid_priv->ysize - 1);
680 	vidconsole_set_cursor_pos(dev, x, y);
681 }
682