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