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