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