1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 1991, 1992 Linus Torvalds 4 */ 5 6 /* 7 * Hopefully this will be a rather complete VT102 implementation. 8 * 9 * Beeping thanks to John T Kohl. 10 * 11 * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics 12 * Chars, and VT100 enhancements by Peter MacDonald. 13 * 14 * Copy and paste function by Andrew Haylett, 15 * some enhancements by Alessandro Rubini. 16 * 17 * Code to check for different video-cards mostly by Galen Hunt, 18 * <g-hunt@ee.utah.edu> 19 * 20 * Rudimentary ISO 10646/Unicode/UTF-8 character set support by 21 * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>. 22 * 23 * Dynamic allocation of consoles, aeb@cwi.nl, May 1994 24 * Resizing of consoles, aeb, 940926 25 * 26 * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94 27 * <poe@daimi.aau.dk> 28 * 29 * User-defined bell sound, new setterm control sequences and printk 30 * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95 31 * 32 * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp> 33 * 34 * Merge with the abstract console driver by Geert Uytterhoeven 35 * <geert@linux-m68k.org>, Jan 1997. 36 * 37 * Original m68k console driver modifications by 38 * 39 * - Arno Griffioen <arno@usn.nl> 40 * - David Carter <carter@cs.bris.ac.uk> 41 * 42 * The abstract console driver provides a generic interface for a text 43 * console. It supports VGA text mode, frame buffer based graphical consoles 44 * and special graphics processors that are only accessible through some 45 * registers (e.g. a TMS340x0 GSP). 46 * 47 * The interface to the hardware is specified using a special structure 48 * (struct consw) which contains function pointers to console operations 49 * (see <linux/console.h> for more information). 50 * 51 * Support for changeable cursor shape 52 * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997 53 * 54 * Ported to i386 and con_scrolldelta fixed 55 * by Emmanuel Marty <core@ggi-project.org>, April 1998 56 * 57 * Resurrected character buffers in videoram plus lots of other trickery 58 * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998 59 * 60 * Removed old-style timers, introduced console_timer, made timer 61 * deletion SMP-safe. 17Jun00, Andrew Morton 62 * 63 * Removed console_lock, enabled interrupts across all console operations 64 * 13 March 2001, Andrew Morton 65 * 66 * Fixed UTF-8 mode so alternate charset modes always work according 67 * to control sequences interpreted in do_con_trol function 68 * preserving backward VT100 semigraphics compatibility, 69 * malformed UTF sequences represented as sequences of replacement glyphs, 70 * original codes or '?' as a last resort if replacement glyph is undefined 71 * by Adam Tla/lka <atlka@pg.gda.pl>, Aug 2006 72 */ 73 74 #include <linux/compiler.h> 75 #include <linux/module.h> 76 #include <linux/types.h> 77 #include <linux/sched/signal.h> 78 #include <linux/tty.h> 79 #include <linux/tty_flip.h> 80 #include <linux/kernel.h> 81 #include <linux/string.h> 82 #include <linux/errno.h> 83 #include <linux/hex.h> 84 #include <linux/kd.h> 85 #include <linux/slab.h> 86 #include <linux/vmalloc.h> 87 #include <linux/major.h> 88 #include <linux/mm.h> 89 #include <linux/console.h> 90 #include <linux/init.h> 91 #include <linux/mutex.h> 92 #include <linux/vt_kern.h> 93 #include <linux/selection.h> 94 #include <linux/tiocl.h> 95 #include <linux/kbd_kern.h> 96 #include <linux/consolemap.h> 97 #include <linux/timer.h> 98 #include <linux/interrupt.h> 99 #include <linux/workqueue.h> 100 #include <linux/pm.h> 101 #include <linux/font.h> 102 #include <linux/bitops.h> 103 #include <linux/notifier.h> 104 #include <linux/device.h> 105 #include <linux/io.h> 106 #include <linux/uaccess.h> 107 #include <linux/kdb.h> 108 #include <linux/ctype.h> 109 #include <linux/gcd.h> 110 111 #define MAX_NR_CON_DRIVER 16 112 113 #define CON_DRIVER_FLAG_MODULE 1 114 #define CON_DRIVER_FLAG_INIT 2 115 #define CON_DRIVER_FLAG_ATTR 4 116 #define CON_DRIVER_FLAG_ZOMBIE 8 117 118 struct con_driver { 119 const struct consw *con; 120 const char *desc; 121 struct device *dev; 122 int node; 123 int first; 124 int last; 125 int flag; 126 }; 127 128 static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER]; 129 const struct consw *conswitchp; 130 131 /* 132 * Here is the default bell parameters: 750HZ, 1/8th of a second 133 */ 134 #define DEFAULT_BELL_PITCH 750 135 #define DEFAULT_BELL_DURATION (HZ/8) 136 #define DEFAULT_CURSOR_BLINK_MS 200 137 138 struct vc vc_cons [MAX_NR_CONSOLES]; 139 EXPORT_SYMBOL(vc_cons); 140 141 static const struct consw *con_driver_map[MAX_NR_CONSOLES]; 142 143 static int con_open(struct tty_struct *, struct file *); 144 static void vc_init(struct vc_data *vc, int do_clear); 145 static void gotoxy(struct vc_data *vc, int new_x, int new_y); 146 static void restore_cur(struct vc_data *vc); 147 static void save_cur(struct vc_data *vc); 148 static void reset_terminal(struct vc_data *vc, int do_clear); 149 static void con_flush_chars(struct tty_struct *tty); 150 static int set_vesa_blanking(u8 __user *mode); 151 static void set_cursor(struct vc_data *vc); 152 static void hide_cursor(struct vc_data *vc); 153 static void console_callback(struct work_struct *ignored); 154 static void con_driver_unregister_callback(struct work_struct *ignored); 155 static void blank_screen_t(struct timer_list *unused); 156 static void set_palette(struct vc_data *vc); 157 static void unblank_screen(void); 158 159 #define vt_get_kmsg_redirect() vt_kmsg_redirect(-1) 160 161 int default_utf8 = true; 162 module_param(default_utf8, int, S_IRUGO | S_IWUSR); 163 int global_cursor_default = -1; 164 module_param(global_cursor_default, int, S_IRUGO | S_IWUSR); 165 EXPORT_SYMBOL(global_cursor_default); 166 167 static int cur_default = CUR_UNDERLINE; 168 module_param(cur_default, int, S_IRUGO | S_IWUSR); 169 170 /* 171 * ignore_poke: don't unblank the screen when things are typed. This is 172 * mainly for the privacy of braille terminal users. 173 */ 174 static int ignore_poke; 175 176 int do_poke_blanked_console; 177 int console_blanked; 178 EXPORT_SYMBOL(console_blanked); 179 180 static enum vesa_blank_mode vesa_blank_mode; 181 static int vesa_off_interval; 182 static int blankinterval; 183 core_param(consoleblank, blankinterval, int, 0444); 184 185 static DECLARE_WORK(console_work, console_callback); 186 static DECLARE_WORK(con_driver_unregister_work, con_driver_unregister_callback); 187 188 /* 189 * fg_console is the current virtual console, 190 * last_console is the last used one, 191 * want_console is the console we want to switch to, 192 */ 193 int fg_console; 194 EXPORT_SYMBOL(fg_console); 195 int last_console; 196 int want_console = -1; 197 198 /* 199 * For each existing display, we have a pointer to console currently visible 200 * on that display, allowing consoles other than fg_console to be refreshed 201 * appropriately. Unless the low-level driver supplies its own display_fg 202 * variable, we use this one for the "master display". 203 */ 204 static struct vc_data *master_display_fg; 205 206 /* 207 * Unfortunately, we need to delay tty echo when we're currently writing to the 208 * console since the code is (and always was) not re-entrant, so we schedule 209 * all flip requests to process context with schedule-task() and run it from 210 * console_callback(). 211 */ 212 213 /* 214 * For the same reason, we defer scrollback to the console callback. 215 */ 216 static int scrollback_delta; 217 218 /* 219 * Hook so that the power management routines can (un)blank 220 * the console on our behalf. 221 */ 222 int (*console_blank_hook)(int); 223 EXPORT_SYMBOL(console_blank_hook); 224 225 static DEFINE_TIMER(console_timer, blank_screen_t); 226 static int blank_state; 227 static int blank_timer_expired; 228 enum { 229 blank_off = 0, 230 blank_normal_wait, 231 blank_vesa_wait, 232 }; 233 234 /* 235 * struct vc_font 236 */ 237 238 /** 239 * vc_font_pitch - Calculates the number of bytes between two adjacent scanlines 240 * @font: The VC font 241 * 242 * Returns: 243 * The number of bytes between two adjacent scanlines in the font data 244 */ 245 unsigned int vc_font_pitch(const struct vc_font *font) 246 { 247 return font_glyph_pitch(font->width); 248 } 249 EXPORT_SYMBOL_GPL(vc_font_pitch); 250 251 /** 252 * vc_font_size - Calculates the size of the font data in bytes 253 * @font: The VC font 254 * 255 * vc_font_size() calculates the number of bytes of font data in the 256 * font specified by @font. The function calculates the size from the 257 * font parameters. 258 * 259 * Returns: 260 * The size of the font data in bytes. 261 */ 262 unsigned int vc_font_size(const struct vc_font *font) 263 { 264 return font_glyph_size(font->width, font->height) * font->charcount; 265 } 266 EXPORT_SYMBOL_GPL(vc_font_size); 267 268 /* 269 * Cursors 270 */ 271 272 /** 273 * vc_cursor_valid_size - Computes a valid cursor size from the given value 274 * @cursor_size: The size constant of cursor pattern 275 * 276 * The argument given in @cursor_size is one of the CUR_ constants, as stored 277 * in struct @vc_data.vc_cursor_type. For unknown values, the helper draws the 278 * default cursor or an underline dash. 279 * 280 * Returns: 281 * A valid cursor size for the given size value 282 */ 283 unsigned int vc_cursor_valid_size(unsigned int cursor_size) 284 { 285 const unsigned int cursor_default_size = CUR_SIZE(READ_ONCE(cur_default)); 286 287 do { 288 switch (cursor_size) { 289 case CUR_NONE: 290 case CUR_UNDERLINE: 291 case CUR_LOWER_THIRD: 292 case CUR_LOWER_HALF: 293 case CUR_TWO_THIRDS: 294 case CUR_BLOCK: 295 return cursor_size; 296 default: 297 pr_warn_once("Unknown cursor %u\n", cursor_size); 298 fallthrough; 299 case CUR_DEF: 300 /* 301 * Use user-given default size, or underline if 302 * the given default is invalid. 303 */ 304 if (cursor_size != cursor_default_size) 305 cursor_size = cursor_default_size; 306 else 307 cursor_size = CUR_UNDERLINE; 308 } 309 } while (true); 310 } 311 EXPORT_SYMBOL_GPL(vc_cursor_valid_size); 312 313 /** 314 * vc_cursor_start - Calculates the cursor's first scanline within a character cell 315 * @cell_height: The overall height of the character cell 316 * @cursor_size: The size constant of cursor pattern 317 * 318 * The parameter @cell_height is the height of the character cell as 319 * displayed by the console. The argument given in @cursor_size is one 320 * of the CUR_ constants. See vc_cursor_valid_size() for how to get a 321 * valid cursor-size constant. 322 * 323 * Returns: 324 * The index of the cursor's first scanline within the character cell 325 */ 326 unsigned int vc_cursor_start(unsigned int cell_height, unsigned int cursor_size) 327 { 328 switch (cursor_size) { 329 case CUR_NONE: 330 return cell_height; 331 case CUR_UNDERLINE: 332 default: 333 if (cell_height < 10) 334 return cell_height - 1; 335 else 336 return cell_height - 3; 337 case CUR_LOWER_THIRD: 338 return cell_height - cell_height / 3; 339 case CUR_LOWER_HALF: 340 return cell_height - cell_height / 2; 341 case CUR_TWO_THIRDS: 342 return cell_height - (cell_height * 2) / 3; 343 case CUR_BLOCK: 344 return 0; 345 } 346 } 347 EXPORT_SYMBOL_GPL(vc_cursor_start); 348 349 /** 350 * vc_cursor_end - Calculates the first scanline after the cursor within a character cell 351 * @cell_height: The overall height of the character cell 352 * @cursor_size: The size constant of cursor pattern 353 * 354 * The parameter @cell_height is the height of the character cell as 355 * displayed by the console. The argument given in @cursor_size is one 356 * of the CUR_ constants. See vc_cursor_valid_size() for how to get a 357 * valid cursor-size constant. 358 * 359 * Returns: 360 * The index of the first scanline after the cursor within the character cell 361 */ 362 unsigned int vc_cursor_end(unsigned int cell_height, unsigned int cursor_size) 363 { 364 switch (cursor_size) { 365 case CUR_UNDERLINE: 366 default: 367 if (cell_height < 10) 368 return cell_height; 369 else 370 return cell_height - 1; 371 case CUR_NONE: 372 case CUR_LOWER_THIRD: 373 case CUR_LOWER_HALF: 374 case CUR_TWO_THIRDS: 375 case CUR_BLOCK: 376 return cell_height; 377 } 378 } 379 EXPORT_SYMBOL_GPL(vc_cursor_end); 380 381 /* 382 * /sys/class/tty/tty0/ 383 * 384 * the attribute 'active' contains the name of the current vc 385 * console and it supports poll() to detect vc switches 386 */ 387 static struct device *tty0dev; 388 389 /* 390 * Notifier list for console events. 391 */ 392 static ATOMIC_NOTIFIER_HEAD(vt_notifier_list); 393 394 int register_vt_notifier(struct notifier_block *nb) 395 { 396 return atomic_notifier_chain_register(&vt_notifier_list, nb); 397 } 398 EXPORT_SYMBOL_GPL(register_vt_notifier); 399 400 int unregister_vt_notifier(struct notifier_block *nb) 401 { 402 return atomic_notifier_chain_unregister(&vt_notifier_list, nb); 403 } 404 EXPORT_SYMBOL_GPL(unregister_vt_notifier); 405 406 static void notify_write(struct vc_data *vc, unsigned int unicode) 407 { 408 struct vt_notifier_param param = { .vc = vc, .c = unicode }; 409 atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, ¶m); 410 } 411 412 static void notify_update(struct vc_data *vc) 413 { 414 struct vt_notifier_param param = { .vc = vc }; 415 atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, ¶m); 416 } 417 /* 418 * Low-Level Functions 419 */ 420 421 static inline bool con_is_fg(const struct vc_data *vc) 422 { 423 return vc->vc_num == fg_console; 424 } 425 426 static inline bool con_should_update(const struct vc_data *vc) 427 { 428 return con_is_visible(vc) && !console_blanked; 429 } 430 431 static inline u16 *screenpos(const struct vc_data *vc, unsigned int offset, 432 bool viewed) 433 { 434 unsigned long origin = viewed ? vc->vc_visible_origin : vc->vc_origin; 435 436 return (u16 *)(origin + offset); 437 } 438 439 static void con_putc(struct vc_data *vc, u16 ca, unsigned int y, unsigned int x) 440 { 441 if (vc->vc_sw->con_putc) 442 vc->vc_sw->con_putc(vc, ca, y, x); 443 else 444 vc->vc_sw->con_putcs(vc, &ca, 1, y, x); 445 } 446 447 /* Called from the keyboard irq path.. */ 448 static inline void scrolldelta(int lines) 449 { 450 /* FIXME */ 451 /* scrolldelta needs some kind of consistency lock, but the BKL was 452 and still is not protecting versus the scheduled back end */ 453 scrollback_delta += lines; 454 schedule_console_callback(); 455 } 456 457 void schedule_console_callback(void) 458 { 459 schedule_work(&console_work); 460 } 461 462 /* 463 * Code to manage unicode-based screen buffers 464 */ 465 466 /* 467 * Our screen buffer is preceded by an array of line pointers so that 468 * scrolling only implies some pointer shuffling. 469 */ 470 471 static u32 **vc_uniscr_alloc(unsigned int cols, unsigned int rows) 472 { 473 u32 **uni_lines; 474 void *p; 475 unsigned int memsize, i, col_size = cols * sizeof(**uni_lines); 476 477 /* allocate everything in one go */ 478 memsize = col_size * rows; 479 memsize += rows * sizeof(*uni_lines); 480 uni_lines = vzalloc(memsize); 481 if (!uni_lines) 482 return NULL; 483 484 /* initial line pointers */ 485 p = uni_lines + rows; 486 for (i = 0; i < rows; i++) { 487 uni_lines[i] = p; 488 p += col_size; 489 } 490 491 return uni_lines; 492 } 493 494 static void vc_uniscr_free(u32 **uni_lines) 495 { 496 vfree(uni_lines); 497 } 498 499 static void vc_uniscr_set(struct vc_data *vc, u32 **new_uni_lines) 500 { 501 vc_uniscr_free(vc->vc_uni_lines); 502 vc->vc_uni_lines = new_uni_lines; 503 } 504 505 static void vc_uniscr_putc(struct vc_data *vc, u32 uc) 506 { 507 if (vc->vc_uni_lines) 508 vc->vc_uni_lines[vc->state.y][vc->state.x] = uc; 509 } 510 511 static void vc_uniscr_insert(struct vc_data *vc, unsigned int nr) 512 { 513 if (vc->vc_uni_lines) { 514 u32 *ln = vc->vc_uni_lines[vc->state.y]; 515 unsigned int x = vc->state.x, cols = vc->vc_cols; 516 517 memmove(&ln[x + nr], &ln[x], (cols - x - nr) * sizeof(*ln)); 518 memset32(&ln[x], ' ', nr); 519 } 520 } 521 522 static void vc_uniscr_delete(struct vc_data *vc, unsigned int nr) 523 { 524 if (vc->vc_uni_lines) { 525 u32 *ln = vc->vc_uni_lines[vc->state.y]; 526 unsigned int x = vc->state.x, cols = vc->vc_cols; 527 528 memmove(&ln[x], &ln[x + nr], (cols - x - nr) * sizeof(*ln)); 529 memset32(&ln[cols - nr], ' ', nr); 530 } 531 } 532 533 static void vc_uniscr_clear_line(struct vc_data *vc, unsigned int x, 534 unsigned int nr) 535 { 536 if (vc->vc_uni_lines) 537 memset32(&vc->vc_uni_lines[vc->state.y][x], ' ', nr); 538 } 539 540 static void vc_uniscr_clear_lines(struct vc_data *vc, unsigned int y, 541 unsigned int nr) 542 { 543 if (vc->vc_uni_lines) 544 while (nr--) 545 memset32(vc->vc_uni_lines[y++], ' ', vc->vc_cols); 546 } 547 548 /* juggling array rotation algorithm (complexity O(N), size complexity O(1)) */ 549 static void juggle_array(u32 **array, unsigned int size, unsigned int nr) 550 { 551 unsigned int gcd_idx; 552 553 for (gcd_idx = 0; gcd_idx < gcd(nr, size); gcd_idx++) { 554 u32 *gcd_idx_val = array[gcd_idx]; 555 unsigned int dst_idx = gcd_idx; 556 557 while (1) { 558 unsigned int src_idx = (dst_idx + nr) % size; 559 if (src_idx == gcd_idx) 560 break; 561 562 array[dst_idx] = array[src_idx]; 563 dst_idx = src_idx; 564 } 565 566 array[dst_idx] = gcd_idx_val; 567 } 568 } 569 570 static void vc_uniscr_scroll(struct vc_data *vc, unsigned int top, 571 unsigned int bottom, enum con_scroll dir, 572 unsigned int nr) 573 { 574 u32 **uni_lines = vc->vc_uni_lines; 575 unsigned int size = bottom - top; 576 577 if (!uni_lines) 578 return; 579 580 if (dir == SM_DOWN) { 581 juggle_array(&uni_lines[top], size, size - nr); 582 vc_uniscr_clear_lines(vc, top, nr); 583 } else { 584 juggle_array(&uni_lines[top], size, nr); 585 vc_uniscr_clear_lines(vc, bottom - nr, nr); 586 } 587 } 588 589 static u32 vc_uniscr_getc(struct vc_data *vc, int relative_pos) 590 { 591 int pos = vc->state.x + vc->vc_need_wrap + relative_pos; 592 593 if (vc->vc_uni_lines && in_range(pos, 0, vc->vc_cols)) 594 return vc->vc_uni_lines[vc->state.y][pos]; 595 return 0; 596 } 597 598 static void vc_uniscr_copy_area(u32 **dst_lines, 599 unsigned int dst_cols, 600 unsigned int dst_rows, 601 u32 **src_lines, 602 unsigned int src_cols, 603 unsigned int src_top_row, 604 unsigned int src_bot_row) 605 { 606 unsigned int dst_row = 0; 607 608 if (!dst_lines) 609 return; 610 611 while (src_top_row < src_bot_row) { 612 u32 *src_line = src_lines[src_top_row]; 613 u32 *dst_line = dst_lines[dst_row]; 614 615 memcpy(dst_line, src_line, src_cols * sizeof(*src_line)); 616 if (dst_cols - src_cols) 617 memset32(dst_line + src_cols, ' ', dst_cols - src_cols); 618 src_top_row++; 619 dst_row++; 620 } 621 while (dst_row < dst_rows) { 622 u32 *dst_line = dst_lines[dst_row]; 623 624 memset32(dst_line, ' ', dst_cols); 625 dst_row++; 626 } 627 } 628 629 /* 630 * Called from vcs_read() to make sure unicode screen retrieval is possible. 631 * This will initialize the unicode screen buffer if not already done. 632 * This returns 0 if OK, or a negative error code otherwise. 633 * In particular, -ENODATA is returned if the console is not in UTF-8 mode. 634 */ 635 int vc_uniscr_check(struct vc_data *vc) 636 { 637 u32 **uni_lines; 638 unsigned short *p; 639 int x, y, mask; 640 641 WARN_CONSOLE_UNLOCKED(); 642 643 if (!vc->vc_utf) 644 return -ENODATA; 645 646 if (vc->vc_uni_lines) 647 return 0; 648 649 uni_lines = vc_uniscr_alloc(vc->vc_cols, vc->vc_rows); 650 if (!uni_lines) 651 return -ENOMEM; 652 653 /* 654 * Let's populate it initially with (imperfect) reverse translation. 655 * This is the next best thing we can do short of having it enabled 656 * from the start even when no users rely on this functionality. True 657 * unicode content will be available after a complete screen refresh. 658 */ 659 p = (unsigned short *)vc->vc_origin; 660 mask = vc->vc_hi_font_mask | 0xff; 661 for (y = 0; y < vc->vc_rows; y++) { 662 u32 *line = uni_lines[y]; 663 for (x = 0; x < vc->vc_cols; x++) { 664 u16 glyph = scr_readw(p++) & mask; 665 line[x] = inverse_translate(vc, glyph, true); 666 } 667 } 668 669 vc->vc_uni_lines = uni_lines; 670 671 return 0; 672 } 673 674 /* 675 * Called from vcs_read() to get the unicode data from the screen. 676 * This must be preceded by a successful call to vc_uniscr_check() once 677 * the console lock has been taken. 678 */ 679 void vc_uniscr_copy_line(const struct vc_data *vc, void *dest, bool viewed, 680 unsigned int row, unsigned int col, unsigned int nr) 681 { 682 u32 **uni_lines = vc->vc_uni_lines; 683 int offset = row * vc->vc_size_row + col * 2; 684 unsigned long pos; 685 686 if (WARN_ON_ONCE(!uni_lines)) 687 return; 688 689 pos = (unsigned long)screenpos(vc, offset, viewed); 690 if (pos >= vc->vc_origin && pos < vc->vc_scr_end) { 691 /* 692 * Desired position falls in the main screen buffer. 693 * However the actual row/col might be different if 694 * scrollback is active. 695 */ 696 row = (pos - vc->vc_origin) / vc->vc_size_row; 697 col = ((pos - vc->vc_origin) % vc->vc_size_row) / 2; 698 memcpy(dest, &uni_lines[row][col], nr * sizeof(u32)); 699 } else { 700 /* 701 * Scrollback is active. For now let's simply backtranslate 702 * the screen glyphs until the unicode screen buffer does 703 * synchronize with console display drivers for a scrollback 704 * buffer of its own. 705 */ 706 u16 *p = (u16 *)pos; 707 int mask = vc->vc_hi_font_mask | 0xff; 708 u32 *uni_buf = dest; 709 while (nr--) { 710 u16 glyph = scr_readw(p++) & mask; 711 *uni_buf++ = inverse_translate(vc, glyph, true); 712 } 713 } 714 } 715 716 static void con_scroll(struct vc_data *vc, unsigned int top, 717 unsigned int bottom, enum con_scroll dir, 718 unsigned int nr) 719 { 720 unsigned int rows = bottom - top; 721 u16 *clear, *dst, *src; 722 723 if (top + nr >= bottom) 724 nr = rows - 1; 725 if (bottom > vc->vc_rows || top >= bottom || nr < 1) 726 return; 727 728 vc_uniscr_scroll(vc, top, bottom, dir, nr); 729 if (con_is_visible(vc) && 730 vc->vc_sw->con_scroll(vc, top, bottom, dir, nr)) 731 return; 732 733 src = clear = (u16 *)(vc->vc_origin + vc->vc_size_row * top); 734 dst = (u16 *)(vc->vc_origin + vc->vc_size_row * (top + nr)); 735 736 if (dir == SM_UP) { 737 clear = src + (rows - nr) * vc->vc_cols; 738 swap(src, dst); 739 } 740 scr_memmovew(dst, src, (rows - nr) * vc->vc_size_row); 741 scr_memsetw(clear, vc->vc_video_erase_char, vc->vc_size_row * nr); 742 } 743 744 static void do_update_region(struct vc_data *vc, unsigned long start, int count) 745 { 746 unsigned int xx, yy, offset; 747 u16 *p = (u16 *)start; 748 749 offset = (start - vc->vc_origin) / 2; 750 xx = offset % vc->vc_cols; 751 yy = offset / vc->vc_cols; 752 753 for(;;) { 754 u16 attrib = scr_readw(p) & 0xff00; 755 int startx = xx; 756 u16 *q = p; 757 while (xx < vc->vc_cols && count) { 758 if (attrib != (scr_readw(p) & 0xff00)) { 759 if (p > q) 760 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx); 761 startx = xx; 762 q = p; 763 attrib = scr_readw(p) & 0xff00; 764 } 765 p++; 766 xx++; 767 count--; 768 } 769 if (p > q) 770 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx); 771 if (!count) 772 break; 773 xx = 0; 774 yy++; 775 } 776 } 777 778 void update_region(struct vc_data *vc, unsigned long start, int count) 779 { 780 WARN_CONSOLE_UNLOCKED(); 781 782 if (con_should_update(vc)) { 783 hide_cursor(vc); 784 do_update_region(vc, start, count); 785 set_cursor(vc); 786 } 787 } 788 EXPORT_SYMBOL(update_region); 789 790 /* Structure of attributes is hardware-dependent */ 791 792 static u8 build_attr(struct vc_data *vc, u8 _color, 793 enum vc_intensity _intensity, bool _blink, bool _underline, 794 bool _reverse, bool _italic) 795 { 796 if (vc->vc_sw->con_build_attr) 797 return vc->vc_sw->con_build_attr(vc, _color, _intensity, 798 _blink, _underline, _reverse, _italic); 799 800 /* 801 * ++roman: I completely changed the attribute format for monochrome 802 * mode (!can_do_color). The formerly used MDA (monochrome display 803 * adapter) format didn't allow the combination of certain effects. 804 * Now the attribute is just a bit vector: 805 * Bit 0..1: intensity (0..2) 806 * Bit 2 : underline 807 * Bit 3 : reverse 808 * Bit 7 : blink 809 */ 810 { 811 u8 a = _color; 812 if (!vc->vc_can_do_color) 813 return _intensity | 814 (_italic << 1) | 815 (_underline << 2) | 816 (_reverse << 3) | 817 (_blink << 7); 818 if (_italic) 819 a = (a & 0xF0) | vc->vc_itcolor; 820 else if (_underline) 821 a = (a & 0xf0) | vc->vc_ulcolor; 822 else if (_intensity == VCI_HALF_BRIGHT) 823 a = (a & 0xf0) | vc->vc_halfcolor; 824 if (_reverse) 825 a = (a & 0x88) | (((a >> 4) | (a << 4)) & 0x77); 826 if (_blink) 827 a ^= 0x80; 828 if (_intensity == VCI_BOLD) 829 a ^= 0x08; 830 if (vc->vc_hi_font_mask == 0x100) 831 a <<= 1; 832 return a; 833 } 834 } 835 836 static void update_attr(struct vc_data *vc) 837 { 838 vc->vc_attr = build_attr(vc, vc->state.color, vc->state.intensity, 839 vc->state.blink, vc->state.underline, 840 vc->state.reverse ^ vc->vc_decscnm, vc->state.italic); 841 vc->vc_video_erase_char = ' ' | (build_attr(vc, vc->state.color, 842 VCI_NORMAL, vc->state.blink, false, 843 vc->vc_decscnm, false) << 8); 844 } 845 846 /* Note: inverting the screen twice should revert to the original state */ 847 void invert_screen(struct vc_data *vc, int offset, int count, bool viewed) 848 { 849 u16 *p; 850 851 WARN_CONSOLE_UNLOCKED(); 852 853 count /= 2; 854 p = screenpos(vc, offset, viewed); 855 if (vc->vc_sw->con_invert_region) { 856 vc->vc_sw->con_invert_region(vc, p, count); 857 } else { 858 u16 *q = p; 859 int cnt = count; 860 u16 a; 861 862 if (!vc->vc_can_do_color) { 863 while (cnt--) { 864 a = scr_readw(q); 865 a ^= 0x0800; 866 scr_writew(a, q); 867 q++; 868 } 869 } else if (vc->vc_hi_font_mask == 0x100) { 870 while (cnt--) { 871 a = scr_readw(q); 872 a = (a & 0x11ff) | 873 ((a & 0xe000) >> 4) | 874 ((a & 0x0e00) << 4); 875 scr_writew(a, q); 876 q++; 877 } 878 } else { 879 while (cnt--) { 880 a = scr_readw(q); 881 a = (a & 0x88ff) | 882 ((a & 0x7000) >> 4) | 883 ((a & 0x0700) << 4); 884 scr_writew(a, q); 885 q++; 886 } 887 } 888 } 889 890 if (con_should_update(vc)) 891 do_update_region(vc, (unsigned long) p, count); 892 notify_update(vc); 893 } 894 895 /* used by selection: complement pointer position */ 896 void complement_pos(struct vc_data *vc, int offset) 897 { 898 static int old_offset = -1; 899 static unsigned short old; 900 static unsigned short oldx, oldy; 901 902 WARN_CONSOLE_UNLOCKED(); 903 904 if (old_offset != -1 && old_offset >= 0 && 905 old_offset < vc->vc_screenbuf_size) { 906 scr_writew(old, screenpos(vc, old_offset, true)); 907 if (con_should_update(vc)) 908 con_putc(vc, old, oldy, oldx); 909 notify_update(vc); 910 } 911 912 old_offset = offset; 913 914 if (offset != -1 && offset >= 0 && 915 offset < vc->vc_screenbuf_size) { 916 unsigned short new; 917 u16 *p = screenpos(vc, offset, true); 918 old = scr_readw(p); 919 new = old ^ vc->vc_complement_mask; 920 scr_writew(new, p); 921 if (con_should_update(vc)) { 922 oldx = (offset >> 1) % vc->vc_cols; 923 oldy = (offset >> 1) / vc->vc_cols; 924 con_putc(vc, new, oldy, oldx); 925 } 926 notify_update(vc); 927 } 928 } 929 930 static void insert_char(struct vc_data *vc, unsigned int nr) 931 { 932 unsigned short *p = (unsigned short *) vc->vc_pos; 933 934 vc_uniscr_insert(vc, nr); 935 scr_memmovew(p + nr, p, (vc->vc_cols - vc->state.x - nr) * 2); 936 scr_memsetw(p, vc->vc_video_erase_char, nr * 2); 937 vc->vc_need_wrap = 0; 938 if (con_should_update(vc)) 939 do_update_region(vc, (unsigned long) p, 940 vc->vc_cols - vc->state.x); 941 } 942 943 static void delete_char(struct vc_data *vc, unsigned int nr) 944 { 945 unsigned short *p = (unsigned short *) vc->vc_pos; 946 947 vc_uniscr_delete(vc, nr); 948 scr_memmovew(p, p + nr, (vc->vc_cols - vc->state.x - nr) * 2); 949 scr_memsetw(p + vc->vc_cols - vc->state.x - nr, vc->vc_video_erase_char, 950 nr * 2); 951 vc->vc_need_wrap = 0; 952 if (con_should_update(vc)) 953 do_update_region(vc, (unsigned long) p, 954 vc->vc_cols - vc->state.x); 955 } 956 957 static int softcursor_original = -1; 958 959 static void add_softcursor(struct vc_data *vc) 960 { 961 int i = scr_readw((u16 *) vc->vc_pos); 962 u32 type = vc->vc_cursor_type; 963 964 if (!(type & CUR_SW)) 965 return; 966 if (softcursor_original != -1) 967 return; 968 softcursor_original = i; 969 i |= CUR_SET(type); 970 i ^= CUR_CHANGE(type); 971 if ((type & CUR_ALWAYS_BG) && 972 (softcursor_original & CUR_BG) == (i & CUR_BG)) 973 i ^= CUR_BG; 974 if ((type & CUR_INVERT_FG_BG) && (i & CUR_FG) == ((i & CUR_BG) >> 4)) 975 i ^= CUR_FG; 976 scr_writew(i, (u16 *)vc->vc_pos); 977 if (con_should_update(vc)) 978 con_putc(vc, i, vc->state.y, vc->state.x); 979 } 980 981 static void hide_softcursor(struct vc_data *vc) 982 { 983 if (softcursor_original != -1) { 984 scr_writew(softcursor_original, (u16 *)vc->vc_pos); 985 if (con_should_update(vc)) 986 con_putc(vc, softcursor_original, vc->state.y, 987 vc->state.x); 988 softcursor_original = -1; 989 } 990 } 991 992 static void hide_cursor(struct vc_data *vc) 993 { 994 if (vc_is_sel(vc)) 995 clear_selection(); 996 997 vc->vc_sw->con_cursor(vc, false); 998 hide_softcursor(vc); 999 } 1000 1001 static void set_cursor(struct vc_data *vc) 1002 { 1003 if (!con_is_fg(vc) || console_blanked || vc->vc_mode == KD_GRAPHICS) 1004 return; 1005 if (vc->vc_deccm) { 1006 if (vc_is_sel(vc)) 1007 clear_selection(); 1008 add_softcursor(vc); 1009 if (CUR_SIZE(vc->vc_cursor_type) != CUR_NONE) 1010 vc->vc_sw->con_cursor(vc, true); 1011 } else 1012 hide_cursor(vc); 1013 } 1014 1015 static void set_origin(struct vc_data *vc) 1016 { 1017 WARN_CONSOLE_UNLOCKED(); 1018 1019 if (!con_is_visible(vc) || 1020 !vc->vc_sw->con_set_origin || 1021 !vc->vc_sw->con_set_origin(vc)) 1022 vc->vc_origin = (unsigned long)vc->vc_screenbuf; 1023 vc->vc_visible_origin = vc->vc_origin; 1024 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size; 1025 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->state.y + 1026 2 * vc->state.x; 1027 } 1028 1029 static void save_screen(struct vc_data *vc) 1030 { 1031 WARN_CONSOLE_UNLOCKED(); 1032 1033 if (vc->vc_sw->con_save_screen) 1034 vc->vc_sw->con_save_screen(vc); 1035 } 1036 1037 static void flush_scrollback(struct vc_data *vc) 1038 { 1039 WARN_CONSOLE_UNLOCKED(); 1040 1041 set_origin(vc); 1042 if (!con_is_visible(vc)) 1043 return; 1044 1045 /* 1046 * The legacy way for flushing the scrollback buffer is to use a side 1047 * effect of the con_switch method. We do it only on the foreground 1048 * console as background consoles have no scrollback buffers in that 1049 * case and we obviously don't want to switch to them. 1050 */ 1051 hide_cursor(vc); 1052 vc->vc_sw->con_switch(vc); 1053 set_cursor(vc); 1054 } 1055 1056 /* 1057 * Redrawing of screen 1058 */ 1059 1060 void clear_buffer_attributes(struct vc_data *vc) 1061 { 1062 unsigned short *p = (unsigned short *)vc->vc_origin; 1063 int count = vc->vc_screenbuf_size / 2; 1064 int mask = vc->vc_hi_font_mask | 0xff; 1065 1066 for (; count > 0; count--, p++) { 1067 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p); 1068 } 1069 } 1070 1071 void redraw_screen(struct vc_data *vc, int is_switch) 1072 { 1073 int redraw = 0; 1074 1075 WARN_CONSOLE_UNLOCKED(); 1076 1077 if (!vc) { 1078 /* strange ... */ 1079 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */ 1080 return; 1081 } 1082 1083 if (is_switch) { 1084 struct vc_data *old_vc = vc_cons[fg_console].d; 1085 if (old_vc == vc) 1086 return; 1087 if (!con_is_visible(vc)) 1088 redraw = 1; 1089 *vc->vc_display_fg = vc; 1090 fg_console = vc->vc_num; 1091 hide_cursor(old_vc); 1092 if (!con_is_visible(old_vc)) { 1093 save_screen(old_vc); 1094 set_origin(old_vc); 1095 } 1096 if (tty0dev) 1097 sysfs_notify(&tty0dev->kobj, NULL, "active"); 1098 } else { 1099 hide_cursor(vc); 1100 redraw = 1; 1101 } 1102 1103 if (redraw) { 1104 bool update; 1105 int old_was_color = vc->vc_can_do_color; 1106 1107 set_origin(vc); 1108 update = vc->vc_sw->con_switch(vc); 1109 set_palette(vc); 1110 /* 1111 * If console changed from mono<->color, the best we can do 1112 * is to clear the buffer attributes. As it currently stands, 1113 * rebuilding new attributes from the old buffer is not doable 1114 * without overly complex code. 1115 */ 1116 if (old_was_color != vc->vc_can_do_color) { 1117 update_attr(vc); 1118 clear_buffer_attributes(vc); 1119 } 1120 1121 if (update && vc->vc_mode != KD_GRAPHICS) 1122 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2); 1123 } 1124 set_cursor(vc); 1125 if (is_switch) { 1126 vt_set_leds_compute_shiftstate(); 1127 notify_update(vc); 1128 } 1129 } 1130 EXPORT_SYMBOL(redraw_screen); 1131 1132 /* 1133 * Allocation, freeing and resizing of VTs. 1134 */ 1135 1136 int vc_cons_allocated(unsigned int i) 1137 { 1138 return (i < MAX_NR_CONSOLES && vc_cons[i].d); 1139 } 1140 1141 static void visual_init(struct vc_data *vc, int num, bool init) 1142 { 1143 /* ++Geert: vc->vc_sw->con_init determines console size */ 1144 if (vc->vc_sw) 1145 module_put(vc->vc_sw->owner); 1146 vc->vc_sw = conswitchp; 1147 1148 if (con_driver_map[num]) 1149 vc->vc_sw = con_driver_map[num]; 1150 1151 __module_get(vc->vc_sw->owner); 1152 vc->vc_num = num; 1153 vc->vc_display_fg = &master_display_fg; 1154 if (vc->uni_pagedict_loc) 1155 con_free_unimap(vc); 1156 vc->uni_pagedict_loc = &vc->uni_pagedict; 1157 vc->uni_pagedict = NULL; 1158 vc->vc_hi_font_mask = 0; 1159 vc->vc_complement_mask = 0; 1160 vc->vc_can_do_color = 0; 1161 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS; 1162 vc->vc_sw->con_init(vc, init); 1163 if (!vc->vc_complement_mask) 1164 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800; 1165 vc->vc_s_complement_mask = vc->vc_complement_mask; 1166 vc->vc_size_row = vc->vc_cols << 1; 1167 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row; 1168 } 1169 1170 1171 static void visual_deinit(struct vc_data *vc) 1172 { 1173 vc->vc_sw->con_deinit(vc); 1174 module_put(vc->vc_sw->owner); 1175 } 1176 1177 static void vc_port_destruct(struct tty_port *port) 1178 { 1179 struct vc_data *vc = container_of(port, struct vc_data, port); 1180 1181 kfree(vc); 1182 } 1183 1184 static const struct tty_port_operations vc_port_ops = { 1185 .destruct = vc_port_destruct, 1186 }; 1187 1188 /* 1189 * Change # of rows and columns (0 means unchanged/the size of fg_console) 1190 * [this is to be used together with some user program 1191 * like resize that changes the hardware videomode] 1192 */ 1193 #define VC_MAXCOL (32767) 1194 #define VC_MAXROW (32767) 1195 1196 int vc_allocate(unsigned int currcons) /* return 0 on success */ 1197 { 1198 struct vt_notifier_param param; 1199 struct vc_data *vc; 1200 int err; 1201 1202 WARN_CONSOLE_UNLOCKED(); 1203 1204 if (currcons >= MAX_NR_CONSOLES) 1205 return -ENXIO; 1206 1207 if (vc_cons[currcons].d) 1208 return 0; 1209 1210 /* due to the granularity of kmalloc, we waste some memory here */ 1211 /* the alloc is done in two steps, to optimize the common situation 1212 of a 25x80 console (structsize=216, screenbuf_size=4000) */ 1213 /* although the numbers above are not valid since long ago, the 1214 point is still up-to-date and the comment still has its value 1215 even if only as a historical artifact. --mj, July 1998 */ 1216 param.vc = vc = kzalloc_obj(struct vc_data); 1217 if (!vc) 1218 return -ENOMEM; 1219 1220 vc_cons[currcons].d = vc; 1221 tty_port_init(&vc->port); 1222 vc->port.ops = &vc_port_ops; 1223 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK); 1224 1225 visual_init(vc, currcons, true); 1226 1227 if (!*vc->uni_pagedict_loc) 1228 con_set_default_unimap(vc); 1229 1230 err = -EINVAL; 1231 if (vc->vc_cols > VC_MAXCOL || vc->vc_rows > VC_MAXROW || 1232 vc->vc_screenbuf_size > KMALLOC_MAX_SIZE || !vc->vc_screenbuf_size) 1233 goto err_free; 1234 err = -ENOMEM; 1235 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_KERNEL); 1236 if (!vc->vc_screenbuf) 1237 goto err_free; 1238 1239 /* If no drivers have overridden us and the user didn't pass a 1240 boot option, default to displaying the cursor */ 1241 if (global_cursor_default == -1) 1242 global_cursor_default = 1; 1243 1244 vc_init(vc, 1); 1245 vcs_make_sysfs(currcons); 1246 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, ¶m); 1247 1248 return 0; 1249 err_free: 1250 visual_deinit(vc); 1251 if (*vc->uni_pagedict_loc) 1252 con_free_unimap(vc); 1253 kfree(vc); 1254 vc_cons[currcons].d = NULL; 1255 return err; 1256 } 1257 1258 static inline int resize_screen(struct vc_data *vc, int width, int height, 1259 bool from_user) 1260 { 1261 /* Resizes the resolution of the display adapater */ 1262 int err = 0; 1263 1264 if (vc->vc_sw->con_resize) 1265 err = vc->vc_sw->con_resize(vc, width, height, from_user); 1266 1267 return err; 1268 } 1269 1270 /** 1271 * vc_do_resize - resizing method for the tty 1272 * @tty: tty being resized 1273 * @vc: virtual console private data 1274 * @cols: columns 1275 * @lines: lines 1276 * @from_user: invoked by a user? 1277 * 1278 * Resize a virtual console, clipping according to the actual constraints. If 1279 * the caller passes a tty structure then update the termios winsize 1280 * information and perform any necessary signal handling. 1281 * 1282 * Locking: Caller must hold the console semaphore. Takes the termios rwsem and 1283 * ctrl.lock of the tty IFF a tty is passed. 1284 */ 1285 static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc, 1286 unsigned int cols, unsigned int lines, bool from_user) 1287 { 1288 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0; 1289 unsigned long end; 1290 unsigned int old_rows, old_row_size, first_copied_row; 1291 unsigned int new_cols, new_rows, new_row_size, new_screen_size; 1292 unsigned short *oldscreen, *newscreen; 1293 u32 **new_uniscr = NULL; 1294 1295 WARN_CONSOLE_UNLOCKED(); 1296 1297 if (cols > VC_MAXCOL || lines > VC_MAXROW) 1298 return -EINVAL; 1299 1300 new_cols = (cols ? cols : vc->vc_cols); 1301 new_rows = (lines ? lines : vc->vc_rows); 1302 new_row_size = new_cols << 1; 1303 new_screen_size = new_row_size * new_rows; 1304 1305 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows) { 1306 /* 1307 * This function is being called here to cover the case 1308 * where the userspace calls the FBIOPUT_VSCREENINFO twice, 1309 * passing the same fb_var_screeninfo containing the fields 1310 * yres/xres equal to a number non-multiple of vc_font.height 1311 * and yres_virtual/xres_virtual equal to number lesser than the 1312 * vc_font.height and yres/xres. 1313 * In the second call, the struct fb_var_screeninfo isn't 1314 * being modified by the underlying driver because of the 1315 * if above, and this causes the fbcon_display->vrows to become 1316 * negative and it eventually leads to out-of-bound 1317 * access by the imageblit function. 1318 * To give the correct values to the struct and to not have 1319 * to deal with possible errors from the code below, we call 1320 * the resize_screen here as well. 1321 */ 1322 return resize_screen(vc, new_cols, new_rows, from_user); 1323 } 1324 1325 if (new_screen_size > KMALLOC_MAX_SIZE || !new_screen_size) 1326 return -EINVAL; 1327 newscreen = kzalloc(new_screen_size, GFP_USER); 1328 if (!newscreen) 1329 return -ENOMEM; 1330 1331 if (vc->vc_uni_lines) { 1332 new_uniscr = vc_uniscr_alloc(new_cols, new_rows); 1333 if (!new_uniscr) { 1334 kfree(newscreen); 1335 return -ENOMEM; 1336 } 1337 } 1338 1339 if (vc_is_sel(vc)) 1340 clear_selection(); 1341 1342 old_rows = vc->vc_rows; 1343 old_row_size = vc->vc_size_row; 1344 1345 err = resize_screen(vc, new_cols, new_rows, from_user); 1346 if (err) { 1347 kfree(newscreen); 1348 vc_uniscr_free(new_uniscr); 1349 return err; 1350 } 1351 1352 vc->vc_rows = new_rows; 1353 vc->vc_cols = new_cols; 1354 vc->vc_size_row = new_row_size; 1355 vc->vc_screenbuf_size = new_screen_size; 1356 1357 rlth = min(old_row_size, new_row_size); 1358 rrem = new_row_size - rlth; 1359 old_origin = vc->vc_origin; 1360 new_origin = (long) newscreen; 1361 new_scr_end = new_origin + new_screen_size; 1362 1363 if (vc->state.y > new_rows) { 1364 if (old_rows - vc->state.y < new_rows) { 1365 /* 1366 * Cursor near the bottom, copy contents from the 1367 * bottom of buffer 1368 */ 1369 first_copied_row = (old_rows - new_rows); 1370 } else { 1371 /* 1372 * Cursor is in no man's land, copy 1/2 screenful 1373 * from the top and bottom of cursor position 1374 */ 1375 first_copied_row = (vc->state.y - new_rows/2); 1376 } 1377 old_origin += first_copied_row * old_row_size; 1378 } else 1379 first_copied_row = 0; 1380 end = old_origin + old_row_size * min(old_rows, new_rows); 1381 1382 vc_uniscr_copy_area(new_uniscr, new_cols, new_rows, 1383 vc->vc_uni_lines, rlth/2, first_copied_row, 1384 min(old_rows, new_rows)); 1385 vc_uniscr_set(vc, new_uniscr); 1386 1387 update_attr(vc); 1388 1389 while (old_origin < end) { 1390 scr_memcpyw((unsigned short *) new_origin, 1391 (unsigned short *) old_origin, rlth); 1392 if (rrem) 1393 scr_memsetw((void *)(new_origin + rlth), 1394 vc->vc_video_erase_char, rrem); 1395 old_origin += old_row_size; 1396 new_origin += new_row_size; 1397 } 1398 if (new_scr_end > new_origin) 1399 scr_memsetw((void *)new_origin, vc->vc_video_erase_char, 1400 new_scr_end - new_origin); 1401 oldscreen = vc->vc_screenbuf; 1402 vc->vc_screenbuf = newscreen; 1403 vc->vc_screenbuf_size = new_screen_size; 1404 set_origin(vc); 1405 kfree(oldscreen); 1406 1407 /* do part of a reset_terminal() */ 1408 vc->vc_top = 0; 1409 vc->vc_bottom = vc->vc_rows; 1410 gotoxy(vc, vc->state.x, vc->state.y); 1411 save_cur(vc); 1412 1413 if (tty) { 1414 /* Rewrite the requested winsize data with the actual 1415 resulting sizes */ 1416 struct winsize ws; 1417 memset(&ws, 0, sizeof(ws)); 1418 ws.ws_row = vc->vc_rows; 1419 ws.ws_col = vc->vc_cols; 1420 ws.ws_ypixel = vc->vc_scan_lines; 1421 tty_do_resize(tty, &ws); 1422 } 1423 1424 if (con_is_visible(vc)) 1425 update_screen(vc); 1426 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num); 1427 notify_update(vc); 1428 return err; 1429 } 1430 1431 /** 1432 * __vc_resize - resize a VT 1433 * @vc: virtual console 1434 * @cols: columns 1435 * @rows: rows 1436 * @from_user: invoked by a user? 1437 * 1438 * Resize a virtual console as seen from the console end of things. We use the 1439 * common vc_do_resize() method to update the structures. 1440 * 1441 * Locking: The caller must hold the console sem to protect console internals 1442 * and @vc->port.tty. 1443 */ 1444 int __vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows, 1445 bool from_user) 1446 { 1447 return vc_do_resize(vc->port.tty, vc, cols, rows, from_user); 1448 } 1449 EXPORT_SYMBOL(__vc_resize); 1450 1451 /** 1452 * vt_resize - resize a VT 1453 * @tty: tty to resize 1454 * @ws: winsize attributes 1455 * 1456 * Resize a virtual terminal. This is called by the tty layer as we register 1457 * our own handler for resizing. The mutual helper does all the actual work. 1458 * 1459 * Locking: Takes the console sem and the called methods then take the tty 1460 * termios_rwsem and the tty ctrl.lock in that order. 1461 */ 1462 static int vt_resize(struct tty_struct *tty, struct winsize *ws) 1463 { 1464 struct vc_data *vc = tty->driver_data; 1465 1466 guard(console_lock)(); 1467 return vc_do_resize(tty, vc, ws->ws_col, ws->ws_row, false); 1468 } 1469 1470 struct vc_data *vc_deallocate(unsigned int currcons) 1471 { 1472 struct vc_data *vc = NULL; 1473 1474 WARN_CONSOLE_UNLOCKED(); 1475 1476 if (vc_cons_allocated(currcons)) { 1477 struct vt_notifier_param param; 1478 1479 param.vc = vc = vc_cons[currcons].d; 1480 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, ¶m); 1481 vcs_remove_sysfs(currcons); 1482 visual_deinit(vc); 1483 con_free_unimap(vc); 1484 put_pid(vc->vt_pid); 1485 vc_uniscr_set(vc, NULL); 1486 kfree(vc->vc_screenbuf); 1487 vc_cons[currcons].d = NULL; 1488 if (vc->vc_saved_screen != NULL) { 1489 kfree(vc->vc_saved_screen); 1490 vc->vc_saved_screen = NULL; 1491 } 1492 vc_uniscr_free(vc->vc_saved_uni_lines); 1493 vc->vc_saved_uni_lines = NULL; 1494 } 1495 return vc; 1496 } 1497 1498 /* 1499 * VT102 emulator 1500 */ 1501 1502 enum { EPecma = 0, EPdec, EPeq, EPgt, EPlt}; 1503 1504 #define set_kbd(vc, x) vt_set_kbd_mode_bit((vc)->vc_num, (x)) 1505 #define clr_kbd(vc, x) vt_clr_kbd_mode_bit((vc)->vc_num, (x)) 1506 #define is_kbd(vc, x) vt_get_kbd_mode_bit((vc)->vc_num, (x)) 1507 1508 #define decarm VC_REPEAT 1509 #define decckm VC_CKMODE 1510 #define kbdapplic VC_APPLIC 1511 #define lnm VC_CRLF 1512 1513 const unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7, 1514 8,12,10,14, 9,13,11,15 }; 1515 EXPORT_SYMBOL(color_table); 1516 1517 /* the default colour table, for VGA+ colour systems */ 1518 unsigned char default_red[] = { 1519 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 1520 0x55, 0xff, 0x55, 0xff, 0x55, 0xff, 0x55, 0xff 1521 }; 1522 module_param_array(default_red, byte, NULL, S_IRUGO | S_IWUSR); 1523 EXPORT_SYMBOL(default_red); 1524 1525 unsigned char default_grn[] = { 1526 0x00, 0x00, 0xaa, 0x55, 0x00, 0x00, 0xaa, 0xaa, 1527 0x55, 0x55, 0xff, 0xff, 0x55, 0x55, 0xff, 0xff 1528 }; 1529 module_param_array(default_grn, byte, NULL, S_IRUGO | S_IWUSR); 1530 EXPORT_SYMBOL(default_grn); 1531 1532 unsigned char default_blu[] = { 1533 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa, 1534 0x55, 0x55, 0x55, 0x55, 0xff, 0xff, 0xff, 0xff 1535 }; 1536 module_param_array(default_blu, byte, NULL, S_IRUGO | S_IWUSR); 1537 EXPORT_SYMBOL(default_blu); 1538 1539 /* 1540 * gotoxy() must verify all boundaries, because the arguments 1541 * might also be negative. If the given position is out of 1542 * bounds, the cursor is placed at the nearest margin. 1543 */ 1544 static void gotoxy(struct vc_data *vc, int new_x, int new_y) 1545 { 1546 int min_y, max_y; 1547 1548 if (new_x < 0) 1549 vc->state.x = 0; 1550 else { 1551 if (new_x >= vc->vc_cols) 1552 vc->state.x = vc->vc_cols - 1; 1553 else 1554 vc->state.x = new_x; 1555 } 1556 1557 if (vc->vc_decom) { 1558 min_y = vc->vc_top; 1559 max_y = vc->vc_bottom; 1560 } else { 1561 min_y = 0; 1562 max_y = vc->vc_rows; 1563 } 1564 if (new_y < min_y) 1565 vc->state.y = min_y; 1566 else if (new_y >= max_y) 1567 vc->state.y = max_y - 1; 1568 else 1569 vc->state.y = new_y; 1570 vc->vc_pos = vc->vc_origin + vc->state.y * vc->vc_size_row + 1571 (vc->state.x << 1); 1572 vc->vc_need_wrap = 0; 1573 } 1574 1575 /* for absolute user moves, when decom is set */ 1576 static void gotoxay(struct vc_data *vc, int new_x, int new_y) 1577 { 1578 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y); 1579 } 1580 1581 void scrollback(struct vc_data *vc) 1582 { 1583 scrolldelta(-(vc->vc_rows / 2)); 1584 } 1585 1586 void scrollfront(struct vc_data *vc, int lines) 1587 { 1588 if (!lines) 1589 lines = vc->vc_rows / 2; 1590 scrolldelta(lines); 1591 } 1592 1593 static void lf(struct vc_data *vc) 1594 { 1595 /* don't scroll if above bottom of scrolling region, or 1596 * if below scrolling region 1597 */ 1598 if (vc->state.y + 1 == vc->vc_bottom) 1599 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_UP, 1); 1600 else if (vc->state.y < vc->vc_rows - 1) { 1601 vc->state.y++; 1602 vc->vc_pos += vc->vc_size_row; 1603 } 1604 vc->vc_need_wrap = 0; 1605 notify_write(vc, '\n'); 1606 } 1607 1608 static void ri(struct vc_data *vc) 1609 { 1610 /* don't scroll if below top of scrolling region, or 1611 * if above scrolling region 1612 */ 1613 if (vc->state.y == vc->vc_top) 1614 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_DOWN, 1); 1615 else if (vc->state.y > 0) { 1616 vc->state.y--; 1617 vc->vc_pos -= vc->vc_size_row; 1618 } 1619 vc->vc_need_wrap = 0; 1620 } 1621 1622 static inline void cr(struct vc_data *vc) 1623 { 1624 vc->vc_pos -= vc->state.x << 1; 1625 vc->vc_need_wrap = vc->state.x = 0; 1626 notify_write(vc, '\r'); 1627 } 1628 1629 static inline void bs(struct vc_data *vc) 1630 { 1631 if (vc->state.x) { 1632 vc->vc_pos -= 2; 1633 vc->state.x--; 1634 vc->vc_need_wrap = 0; 1635 notify_write(vc, '\b'); 1636 } 1637 } 1638 1639 static inline void del(struct vc_data *vc) 1640 { 1641 /* ignored */ 1642 } 1643 1644 enum CSI_J { 1645 CSI_J_CURSOR_TO_END = 0, 1646 CSI_J_START_TO_CURSOR = 1, 1647 CSI_J_VISIBLE = 2, 1648 CSI_J_FULL = 3, 1649 }; 1650 1651 static void csi_J(struct vc_data *vc, enum CSI_J vpar) 1652 { 1653 unsigned short *start; 1654 unsigned int count; 1655 1656 switch (vpar) { 1657 case CSI_J_CURSOR_TO_END: 1658 vc_uniscr_clear_line(vc, vc->state.x, 1659 vc->vc_cols - vc->state.x); 1660 vc_uniscr_clear_lines(vc, vc->state.y + 1, 1661 vc->vc_rows - vc->state.y - 1); 1662 count = (vc->vc_scr_end - vc->vc_pos) >> 1; 1663 start = (unsigned short *)vc->vc_pos; 1664 break; 1665 case CSI_J_START_TO_CURSOR: 1666 vc_uniscr_clear_line(vc, 0, vc->state.x + 1); 1667 vc_uniscr_clear_lines(vc, 0, vc->state.y); 1668 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1; 1669 start = (unsigned short *)vc->vc_origin; 1670 break; 1671 case CSI_J_FULL: 1672 flush_scrollback(vc); 1673 fallthrough; 1674 case CSI_J_VISIBLE: 1675 vc_uniscr_clear_lines(vc, 0, vc->vc_rows); 1676 count = vc->vc_cols * vc->vc_rows; 1677 start = (unsigned short *)vc->vc_origin; 1678 break; 1679 default: 1680 return; 1681 } 1682 scr_memsetw(start, vc->vc_video_erase_char, 2 * count); 1683 if (con_should_update(vc)) 1684 do_update_region(vc, (unsigned long) start, count); 1685 vc->vc_need_wrap = 0; 1686 } 1687 1688 enum { 1689 CSI_K_CURSOR_TO_LINEEND = 0, 1690 CSI_K_LINESTART_TO_CURSOR = 1, 1691 CSI_K_LINE = 2, 1692 }; 1693 1694 static void csi_K(struct vc_data *vc) 1695 { 1696 unsigned int count; 1697 unsigned short *start = (unsigned short *)vc->vc_pos; 1698 int offset; 1699 1700 switch (vc->vc_par[0]) { 1701 case CSI_K_CURSOR_TO_LINEEND: 1702 offset = 0; 1703 count = vc->vc_cols - vc->state.x; 1704 break; 1705 case CSI_K_LINESTART_TO_CURSOR: 1706 offset = -vc->state.x; 1707 count = vc->state.x + 1; 1708 break; 1709 case CSI_K_LINE: 1710 offset = -vc->state.x; 1711 count = vc->vc_cols; 1712 break; 1713 default: 1714 return; 1715 } 1716 vc_uniscr_clear_line(vc, vc->state.x + offset, count); 1717 scr_memsetw(start + offset, vc->vc_video_erase_char, 2 * count); 1718 vc->vc_need_wrap = 0; 1719 if (con_should_update(vc)) 1720 do_update_region(vc, (unsigned long)(start + offset), count); 1721 } 1722 1723 /* erase the following count positions */ 1724 static void csi_X(struct vc_data *vc) 1725 { /* not vt100? */ 1726 unsigned int count = clamp(vc->vc_par[0], 1, vc->vc_cols - vc->state.x); 1727 1728 vc_uniscr_clear_line(vc, vc->state.x, count); 1729 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count); 1730 if (con_should_update(vc)) 1731 vc->vc_sw->con_clear(vc, vc->state.y, vc->state.x, count); 1732 vc->vc_need_wrap = 0; 1733 } 1734 1735 static void default_attr(struct vc_data *vc) 1736 { 1737 vc->state.intensity = VCI_NORMAL; 1738 vc->state.italic = false; 1739 vc->state.underline = false; 1740 vc->state.reverse = false; 1741 vc->state.blink = false; 1742 vc->state.color = vc->vc_def_color; 1743 } 1744 1745 struct rgb { u8 r; u8 g; u8 b; }; 1746 1747 static void rgb_from_256(unsigned int i, struct rgb *c) 1748 { 1749 if (i < 8) { /* Standard colours. */ 1750 c->r = i&1 ? 0xaa : 0x00; 1751 c->g = i&2 ? 0xaa : 0x00; 1752 c->b = i&4 ? 0xaa : 0x00; 1753 } else if (i < 16) { 1754 c->r = i&1 ? 0xff : 0x55; 1755 c->g = i&2 ? 0xff : 0x55; 1756 c->b = i&4 ? 0xff : 0x55; 1757 } else if (i < 232) { /* 6x6x6 colour cube. */ 1758 i -= 16; 1759 c->b = i % 6 * 255 / 6; 1760 i /= 6; 1761 c->g = i % 6 * 255 / 6; 1762 i /= 6; 1763 c->r = i * 255 / 6; 1764 } else /* Grayscale ramp. */ 1765 c->r = c->g = c->b = i * 10 - 2312; 1766 } 1767 1768 static void rgb_foreground(struct vc_data *vc, const struct rgb *c) 1769 { 1770 u8 hue = 0, max = max3(c->r, c->g, c->b); 1771 1772 if (c->r > max / 2) 1773 hue |= 4; 1774 if (c->g > max / 2) 1775 hue |= 2; 1776 if (c->b > max / 2) 1777 hue |= 1; 1778 1779 if (hue == 7 && max <= 0x55) { 1780 hue = 0; 1781 vc->state.intensity = VCI_BOLD; 1782 } else if (max > 0xaa) 1783 vc->state.intensity = VCI_BOLD; 1784 else 1785 vc->state.intensity = VCI_NORMAL; 1786 1787 vc->state.color = (vc->state.color & 0xf0) | hue; 1788 } 1789 1790 static void rgb_background(struct vc_data *vc, const struct rgb *c) 1791 { 1792 /* For backgrounds, err on the dark side. */ 1793 vc->state.color = (vc->state.color & 0x0f) 1794 | (c->r&0x80) >> 1 | (c->g&0x80) >> 2 | (c->b&0x80) >> 3; 1795 } 1796 1797 /* 1798 * ITU T.416 Higher colour modes. They break the usual properties of SGR codes 1799 * and thus need to be detected and ignored by hand. 1800 * 1801 * Subcommands 3 (CMY) and 4 (CMYK) are so insane there's no point in 1802 * supporting them. 1803 */ 1804 static int vc_t416_color(struct vc_data *vc, int i, 1805 void(*set_color)(struct vc_data *vc, const struct rgb *c)) 1806 { 1807 struct rgb c; 1808 1809 i++; 1810 if (i > vc->vc_npar) 1811 return i; 1812 1813 if (vc->vc_par[i] == 5 && i + 1 <= vc->vc_npar) { 1814 /* 256 colours */ 1815 i++; 1816 rgb_from_256(vc->vc_par[i], &c); 1817 } else if (vc->vc_par[i] == 2 && i + 3 <= vc->vc_npar) { 1818 /* 24 bit */ 1819 c.r = vc->vc_par[i + 1]; 1820 c.g = vc->vc_par[i + 2]; 1821 c.b = vc->vc_par[i + 3]; 1822 i += 3; 1823 } else 1824 return i; 1825 1826 set_color(vc, &c); 1827 1828 return i; 1829 } 1830 1831 enum { 1832 CSI_m_DEFAULT = 0, 1833 CSI_m_BOLD = 1, 1834 CSI_m_HALF_BRIGHT = 2, 1835 CSI_m_ITALIC = 3, 1836 CSI_m_UNDERLINE = 4, 1837 CSI_m_BLINK = 5, 1838 CSI_m_REVERSE = 7, 1839 CSI_m_PRI_FONT = 10, 1840 CSI_m_ALT_FONT1 = 11, 1841 CSI_m_ALT_FONT2 = 12, 1842 CSI_m_DOUBLE_UNDERLINE = 21, 1843 CSI_m_NORMAL_INTENSITY = 22, 1844 CSI_m_NO_ITALIC = 23, 1845 CSI_m_NO_UNDERLINE = 24, 1846 CSI_m_NO_BLINK = 25, 1847 CSI_m_NO_REVERSE = 27, 1848 CSI_m_FG_COLOR_BEG = 30, 1849 CSI_m_FG_COLOR_END = 37, 1850 CSI_m_FG_COLOR = 38, 1851 CSI_m_DEFAULT_FG_COLOR = 39, 1852 CSI_m_BG_COLOR_BEG = 40, 1853 CSI_m_BG_COLOR_END = 47, 1854 CSI_m_BG_COLOR = 48, 1855 CSI_m_DEFAULT_BG_COLOR = 49, 1856 CSI_m_UNDERLINE_COLOR = 58, 1857 CSI_m_BRIGHT_FG_COLOR_BEG = 90, 1858 CSI_m_BRIGHT_FG_COLOR_END = 97, 1859 CSI_m_BRIGHT_FG_COLOR_OFF = CSI_m_BRIGHT_FG_COLOR_BEG - CSI_m_FG_COLOR_BEG, 1860 CSI_m_BRIGHT_BG_COLOR_BEG = 100, 1861 CSI_m_BRIGHT_BG_COLOR_END = 107, 1862 CSI_m_BRIGHT_BG_COLOR_OFF = CSI_m_BRIGHT_BG_COLOR_BEG - CSI_m_BG_COLOR_BEG, 1863 }; 1864 1865 /* console_lock is held */ 1866 static void csi_m(struct vc_data *vc) 1867 { 1868 int i; 1869 1870 for (i = 0; i <= vc->vc_npar; i++) 1871 switch (vc->vc_par[i]) { 1872 case CSI_m_DEFAULT: /* all attributes off */ 1873 default_attr(vc); 1874 break; 1875 case CSI_m_BOLD: 1876 vc->state.intensity = VCI_BOLD; 1877 break; 1878 case CSI_m_HALF_BRIGHT: 1879 vc->state.intensity = VCI_HALF_BRIGHT; 1880 break; 1881 case CSI_m_ITALIC: 1882 vc->state.italic = true; 1883 break; 1884 case CSI_m_DOUBLE_UNDERLINE: 1885 /* 1886 * No console drivers support double underline, so 1887 * convert it to a single underline. 1888 */ 1889 case CSI_m_UNDERLINE: 1890 vc->state.underline = true; 1891 break; 1892 case CSI_m_BLINK: 1893 vc->state.blink = true; 1894 break; 1895 case CSI_m_REVERSE: 1896 vc->state.reverse = true; 1897 break; 1898 case CSI_m_PRI_FONT: /* ANSI X3.64-1979 (SCO-ish?) 1899 * Select primary font, don't display control chars if 1900 * defined, don't set bit 8 on output. 1901 */ 1902 vc->vc_translate = set_translate(vc->state.Gx_charset[vc->state.charset], vc); 1903 vc->vc_disp_ctrl = 0; 1904 vc->vc_toggle_meta = 0; 1905 break; 1906 case CSI_m_ALT_FONT1: /* ANSI X3.64-1979 (SCO-ish?) 1907 * Select first alternate font, lets chars < 32 be 1908 * displayed as ROM chars. 1909 */ 1910 vc->vc_translate = set_translate(IBMPC_MAP, vc); 1911 vc->vc_disp_ctrl = 1; 1912 vc->vc_toggle_meta = 0; 1913 break; 1914 case CSI_m_ALT_FONT2: /* ANSI X3.64-1979 (SCO-ish?) 1915 * Select second alternate font, toggle high bit 1916 * before displaying as ROM char. 1917 */ 1918 vc->vc_translate = set_translate(IBMPC_MAP, vc); 1919 vc->vc_disp_ctrl = 1; 1920 vc->vc_toggle_meta = 1; 1921 break; 1922 case CSI_m_NORMAL_INTENSITY: 1923 vc->state.intensity = VCI_NORMAL; 1924 break; 1925 case CSI_m_NO_ITALIC: 1926 vc->state.italic = false; 1927 break; 1928 case CSI_m_NO_UNDERLINE: 1929 vc->state.underline = false; 1930 break; 1931 case CSI_m_NO_BLINK: 1932 vc->state.blink = false; 1933 break; 1934 case CSI_m_NO_REVERSE: 1935 vc->state.reverse = false; 1936 break; 1937 case CSI_m_FG_COLOR: 1938 i = vc_t416_color(vc, i, rgb_foreground); 1939 break; 1940 case CSI_m_BG_COLOR: 1941 i = vc_t416_color(vc, i, rgb_background); 1942 break; 1943 case CSI_m_DEFAULT_FG_COLOR: 1944 vc->state.color = (vc->vc_def_color & 0x0f) | 1945 (vc->state.color & 0xf0); 1946 break; 1947 case CSI_m_DEFAULT_BG_COLOR: 1948 vc->state.color = (vc->vc_def_color & 0xf0) | 1949 (vc->state.color & 0x0f); 1950 break; 1951 case CSI_m_BRIGHT_FG_COLOR_BEG ... CSI_m_BRIGHT_FG_COLOR_END: 1952 vc->state.intensity = VCI_BOLD; 1953 vc->vc_par[i] -= CSI_m_BRIGHT_FG_COLOR_OFF; 1954 fallthrough; 1955 case CSI_m_FG_COLOR_BEG ... CSI_m_FG_COLOR_END: 1956 vc->vc_par[i] -= CSI_m_FG_COLOR_BEG; 1957 vc->state.color = color_table[vc->vc_par[i]] | 1958 (vc->state.color & 0xf0); 1959 break; 1960 case CSI_m_BRIGHT_BG_COLOR_BEG ... CSI_m_BRIGHT_BG_COLOR_END: 1961 vc->vc_par[i] -= CSI_m_BRIGHT_BG_COLOR_OFF; 1962 fallthrough; 1963 case CSI_m_BG_COLOR_BEG ... CSI_m_BG_COLOR_END: 1964 vc->vc_par[i] -= CSI_m_BG_COLOR_BEG; 1965 vc->state.color = (color_table[vc->vc_par[i]] << 4) | 1966 (vc->state.color & 0x0f); 1967 break; 1968 } 1969 update_attr(vc); 1970 } 1971 1972 static void respond_string(const char *p, size_t len, struct tty_port *port) 1973 { 1974 tty_insert_flip_string(port, p, len); 1975 tty_flip_buffer_push(port); 1976 } 1977 1978 static void cursor_report(struct vc_data *vc, struct tty_struct *tty) 1979 { 1980 char buf[40]; 1981 int len; 1982 1983 len = sprintf(buf, "\033[%d;%dR", vc->state.y + 1984 (vc->vc_decom ? vc->vc_top + 1 : 1), 1985 vc->state.x + 1); 1986 respond_string(buf, len, tty->port); 1987 } 1988 1989 static inline void status_report(struct tty_struct *tty) 1990 { 1991 static const char teminal_ok[] = "\033[0n"; 1992 1993 respond_string(teminal_ok, strlen(teminal_ok), tty->port); 1994 } 1995 1996 static inline void respond_ID(struct tty_struct *tty) 1997 { 1998 /* terminal answer to an ESC-Z or csi0c query. */ 1999 static const char vt102_id[] = "\033[?6c"; 2000 2001 respond_string(vt102_id, strlen(vt102_id), tty->port); 2002 } 2003 2004 void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry) 2005 { 2006 char buf[8]; 2007 int len; 2008 2009 len = sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), 2010 (char)('!' + mrx), (char)('!' + mry)); 2011 respond_string(buf, len, tty->port); 2012 } 2013 2014 /* invoked via ioctl(TIOCLINUX) and through set_selection_user */ 2015 int mouse_reporting(void) 2016 { 2017 return vc_cons[fg_console].d->vc_report_mouse; 2018 } 2019 2020 /* invoked via ioctl(TIOCLINUX) */ 2021 static int get_bracketed_paste(struct tty_struct *tty) 2022 { 2023 struct vc_data *vc = tty->driver_data; 2024 2025 return vc->vc_bracketed_paste; 2026 } 2027 2028 /* console_lock is held */ 2029 static void enter_alt_screen(struct vc_data *vc) 2030 { 2031 unsigned int size = vc->vc_rows * vc->vc_cols * 2; 2032 2033 if (vc->vc_saved_screen != NULL) 2034 return; /* Already inside an alt-screen */ 2035 vc->vc_saved_screen = kmemdup((u16 *)vc->vc_origin, size, GFP_KERNEL); 2036 if (vc->vc_saved_screen == NULL) 2037 return; 2038 vc->vc_saved_uni_lines = vc->vc_uni_lines; 2039 vc->vc_uni_lines = NULL; 2040 vc->vc_saved_rows = vc->vc_rows; 2041 vc->vc_saved_cols = vc->vc_cols; 2042 save_cur(vc); 2043 /* clear entire screen */ 2044 csi_J(vc, CSI_J_FULL); 2045 } 2046 2047 /* console_lock is held */ 2048 static void leave_alt_screen(struct vc_data *vc) 2049 { 2050 unsigned int rows = min(vc->vc_saved_rows, vc->vc_rows); 2051 unsigned int cols = min(vc->vc_saved_cols, vc->vc_cols); 2052 u16 *src, *dest; 2053 2054 if (vc->vc_saved_screen == NULL) 2055 return; /* Not inside an alt-screen */ 2056 for (unsigned int r = 0; r < rows; r++) { 2057 src = vc->vc_saved_screen + r * vc->vc_saved_cols; 2058 dest = ((u16 *)vc->vc_origin) + r * vc->vc_cols; 2059 memcpy(dest, src, 2 * cols); 2060 } 2061 /* 2062 * If the console was resized while in the alternate screen, 2063 * resize the saved unicode buffer to the current dimensions. 2064 * On allocation failure new_uniscr is NULL, causing the old 2065 * buffer to be freed and vc_uni_lines to be lazily rebuilt 2066 * via vc_uniscr_check() when next needed. 2067 */ 2068 if (vc->vc_saved_uni_lines && 2069 (vc->vc_saved_rows != vc->vc_rows || 2070 vc->vc_saved_cols != vc->vc_cols)) { 2071 u32 **new_uniscr = vc_uniscr_alloc(vc->vc_cols, vc->vc_rows); 2072 2073 if (new_uniscr) 2074 vc_uniscr_copy_area(new_uniscr, vc->vc_cols, vc->vc_rows, 2075 vc->vc_saved_uni_lines, cols, 0, rows); 2076 vc_uniscr_free(vc->vc_saved_uni_lines); 2077 vc->vc_saved_uni_lines = new_uniscr; 2078 } 2079 vc_uniscr_set(vc, vc->vc_saved_uni_lines); 2080 vc->vc_saved_uni_lines = NULL; 2081 restore_cur(vc); 2082 /* Update the entire screen */ 2083 if (con_should_update(vc)) 2084 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2); 2085 kfree(vc->vc_saved_screen); 2086 vc->vc_saved_screen = NULL; 2087 } 2088 2089 enum { 2090 CSI_DEC_hl_CURSOR_KEYS = 1, /* CKM: cursor keys send ^[Ox/^[[x */ 2091 CSI_DEC_hl_132_COLUMNS = 3, /* COLM: 80/132 mode switch */ 2092 CSI_DEC_hl_REVERSE_VIDEO = 5, /* SCNM */ 2093 CSI_DEC_hl_ORIGIN_MODE = 6, /* OM: origin relative/absolute */ 2094 CSI_DEC_hl_AUTOWRAP = 7, /* AWM */ 2095 CSI_DEC_hl_AUTOREPEAT = 8, /* ARM */ 2096 CSI_DEC_hl_MOUSE_X10 = 9, 2097 CSI_DEC_hl_SHOW_CURSOR = 25, /* TCEM */ 2098 CSI_DEC_hl_MOUSE_VT200 = 1000, 2099 CSI_DEC_hl_ALT_SCREEN = 1049, 2100 CSI_DEC_hl_BRACKETED_PASTE = 2004, 2101 }; 2102 2103 /* console_lock is held */ 2104 static void csi_DEC_hl(struct vc_data *vc, bool on_off) 2105 { 2106 unsigned int i; 2107 2108 for (i = 0; i <= vc->vc_npar; i++) 2109 switch (vc->vc_par[i]) { 2110 case CSI_DEC_hl_CURSOR_KEYS: 2111 if (on_off) 2112 set_kbd(vc, decckm); 2113 else 2114 clr_kbd(vc, decckm); 2115 break; 2116 case CSI_DEC_hl_132_COLUMNS: /* unimplemented */ 2117 #if 0 2118 vc_resize(deccolm ? 132 : 80, vc->vc_rows); 2119 /* this alone does not suffice; some user mode 2120 utility has to change the hardware regs */ 2121 #endif 2122 break; 2123 case CSI_DEC_hl_REVERSE_VIDEO: 2124 if (vc->vc_decscnm != on_off) { 2125 vc->vc_decscnm = on_off; 2126 invert_screen(vc, 0, vc->vc_screenbuf_size, 2127 false); 2128 update_attr(vc); 2129 } 2130 break; 2131 case CSI_DEC_hl_ORIGIN_MODE: 2132 vc->vc_decom = on_off; 2133 gotoxay(vc, 0, 0); 2134 break; 2135 case CSI_DEC_hl_AUTOWRAP: 2136 vc->vc_decawm = on_off; 2137 break; 2138 case CSI_DEC_hl_AUTOREPEAT: 2139 if (on_off) 2140 set_kbd(vc, decarm); 2141 else 2142 clr_kbd(vc, decarm); 2143 break; 2144 case CSI_DEC_hl_MOUSE_X10: 2145 vc->vc_report_mouse = on_off ? 1 : 0; 2146 break; 2147 case CSI_DEC_hl_SHOW_CURSOR: 2148 vc->vc_deccm = on_off; 2149 break; 2150 case CSI_DEC_hl_MOUSE_VT200: 2151 vc->vc_report_mouse = on_off ? 2 : 0; 2152 break; 2153 case CSI_DEC_hl_BRACKETED_PASTE: 2154 vc->vc_bracketed_paste = on_off; 2155 break; 2156 case CSI_DEC_hl_ALT_SCREEN: 2157 if (on_off) 2158 enter_alt_screen(vc); 2159 else 2160 leave_alt_screen(vc); 2161 break; 2162 } 2163 } 2164 2165 enum { 2166 CSI_hl_DISPLAY_CTRL = 3, /* handle ansi control chars */ 2167 CSI_hl_INSERT = 4, /* IRM: insert/replace */ 2168 CSI_hl_AUTO_NL = 20, /* LNM: Enter == CrLf/Lf */ 2169 }; 2170 2171 /* console_lock is held */ 2172 static void csi_hl(struct vc_data *vc, bool on_off) 2173 { 2174 unsigned int i; 2175 2176 for (i = 0; i <= vc->vc_npar; i++) 2177 switch (vc->vc_par[i]) { /* ANSI modes set/reset */ 2178 case CSI_hl_DISPLAY_CTRL: 2179 vc->vc_disp_ctrl = on_off; 2180 break; 2181 case CSI_hl_INSERT: 2182 vc->vc_decim = on_off; 2183 break; 2184 case CSI_hl_AUTO_NL: 2185 if (on_off) 2186 set_kbd(vc, lnm); 2187 else 2188 clr_kbd(vc, lnm); 2189 break; 2190 } 2191 } 2192 2193 enum CSI_right_square_bracket { 2194 CSI_RSB_COLOR_FOR_UNDERLINE = 1, 2195 CSI_RSB_COLOR_FOR_HALF_BRIGHT = 2, 2196 CSI_RSB_MAKE_CUR_COLOR_DEFAULT = 8, 2197 CSI_RSB_BLANKING_INTERVAL = 9, 2198 CSI_RSB_BELL_FREQUENCY = 10, 2199 CSI_RSB_BELL_DURATION = 11, 2200 CSI_RSB_BRING_CONSOLE_TO_FRONT = 12, 2201 CSI_RSB_UNBLANK = 13, 2202 CSI_RSB_VESA_OFF_INTERVAL = 14, 2203 CSI_RSB_BRING_PREV_CONSOLE_TO_FRONT = 15, 2204 CSI_RSB_CURSOR_BLINK_INTERVAL = 16, 2205 }; 2206 2207 /* 2208 * csi_RSB - csi+] (Right Square Bracket) handler 2209 * 2210 * These are linux console private sequences. 2211 * 2212 * console_lock is held 2213 */ 2214 static void csi_RSB(struct vc_data *vc) 2215 { 2216 switch (vc->vc_par[0]) { 2217 case CSI_RSB_COLOR_FOR_UNDERLINE: 2218 if (vc->vc_can_do_color && vc->vc_par[1] < 16) { 2219 vc->vc_ulcolor = color_table[vc->vc_par[1]]; 2220 if (vc->state.underline) 2221 update_attr(vc); 2222 } 2223 break; 2224 case CSI_RSB_COLOR_FOR_HALF_BRIGHT: 2225 if (vc->vc_can_do_color && vc->vc_par[1] < 16) { 2226 vc->vc_halfcolor = color_table[vc->vc_par[1]]; 2227 if (vc->state.intensity == VCI_HALF_BRIGHT) 2228 update_attr(vc); 2229 } 2230 break; 2231 case CSI_RSB_MAKE_CUR_COLOR_DEFAULT: 2232 vc->vc_def_color = vc->vc_attr; 2233 if (vc->vc_hi_font_mask == 0x100) 2234 vc->vc_def_color >>= 1; 2235 default_attr(vc); 2236 update_attr(vc); 2237 break; 2238 case CSI_RSB_BLANKING_INTERVAL: 2239 blankinterval = min(vc->vc_par[1], 60U) * 60; 2240 poke_blanked_console(); 2241 break; 2242 case CSI_RSB_BELL_FREQUENCY: 2243 if (vc->vc_npar >= 1) 2244 vc->vc_bell_pitch = vc->vc_par[1]; 2245 else 2246 vc->vc_bell_pitch = DEFAULT_BELL_PITCH; 2247 break; 2248 case CSI_RSB_BELL_DURATION: 2249 if (vc->vc_npar >= 1) 2250 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ? 2251 msecs_to_jiffies(vc->vc_par[1]) : 0; 2252 else 2253 vc->vc_bell_duration = DEFAULT_BELL_DURATION; 2254 break; 2255 case CSI_RSB_BRING_CONSOLE_TO_FRONT: 2256 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1)) 2257 set_console(vc->vc_par[1] - 1); 2258 break; 2259 case CSI_RSB_UNBLANK: 2260 poke_blanked_console(); 2261 break; 2262 case CSI_RSB_VESA_OFF_INTERVAL: 2263 vesa_off_interval = min(vc->vc_par[1], 60U) * 60 * HZ; 2264 break; 2265 case CSI_RSB_BRING_PREV_CONSOLE_TO_FRONT: 2266 set_console(last_console); 2267 break; 2268 case CSI_RSB_CURSOR_BLINK_INTERVAL: 2269 if (vc->vc_npar >= 1 && vc->vc_par[1] >= 50 && 2270 vc->vc_par[1] <= USHRT_MAX) 2271 vc->vc_cur_blink_ms = vc->vc_par[1]; 2272 else 2273 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS; 2274 break; 2275 } 2276 } 2277 2278 /* console_lock is held */ 2279 static void csi_at(struct vc_data *vc, unsigned int nr) 2280 { 2281 nr = clamp(nr, 1, vc->vc_cols - vc->state.x); 2282 insert_char(vc, nr); 2283 } 2284 2285 /* console_lock is held */ 2286 static void csi_L(struct vc_data *vc) 2287 { 2288 unsigned int nr = clamp(vc->vc_par[0], 1, vc->vc_rows - vc->state.y); 2289 2290 con_scroll(vc, vc->state.y, vc->vc_bottom, SM_DOWN, nr); 2291 vc->vc_need_wrap = 0; 2292 } 2293 2294 /* console_lock is held */ 2295 static void csi_P(struct vc_data *vc) 2296 { 2297 unsigned int nr = clamp(vc->vc_par[0], 1, vc->vc_cols - vc->state.x); 2298 2299 delete_char(vc, nr); 2300 } 2301 2302 /* console_lock is held */ 2303 static void csi_M(struct vc_data *vc) 2304 { 2305 unsigned int nr = clamp(vc->vc_par[0], 1, vc->vc_rows - vc->state.y); 2306 2307 con_scroll(vc, vc->state.y, vc->vc_bottom, SM_UP, nr); 2308 vc->vc_need_wrap = 0; 2309 } 2310 2311 /* console_lock is held (except via vc_init->reset_terminal */ 2312 static void save_cur(struct vc_data *vc) 2313 { 2314 memcpy(&vc->saved_state, &vc->state, sizeof(vc->state)); 2315 } 2316 2317 /* console_lock is held */ 2318 static void restore_cur(struct vc_data *vc) 2319 { 2320 memcpy(&vc->state, &vc->saved_state, sizeof(vc->state)); 2321 2322 gotoxy(vc, vc->state.x, vc->state.y); 2323 vc->vc_translate = set_translate(vc->state.Gx_charset[vc->state.charset], 2324 vc); 2325 update_attr(vc); 2326 vc->vc_need_wrap = 0; 2327 } 2328 2329 /** 2330 * enum vc_ctl_state - control characters state of a vt 2331 * 2332 * @ESnormal: initial state, no control characters parsed 2333 * @ESesc: ESC parsed 2334 * @ESsquare: CSI parsed -- modifiers/parameters/ctrl chars expected 2335 * @ESgetpars: CSI parsed -- parameters/ctrl chars expected 2336 * @ESgetsubpars: CSI m parsed -- subparameters expected 2337 * @ESfunckey: CSI [ parsed 2338 * @EShash: ESC # parsed 2339 * @ESsetG0: ESC ( parsed 2340 * @ESsetG1: ESC ) parsed 2341 * @ESpercent: ESC % parsed 2342 * @EScsiignore: CSI [0x20-0x3f] parsed 2343 * @ESnonstd: OSC parsed 2344 * @ESpalette: OSC P parsed 2345 * @ESosc: OSC [0-9] parsed 2346 * @ESANSI_first: first state for ignoring ansi control sequences 2347 * @ESapc: ESC _ parsed 2348 * @ESpm: ESC ^ parsed 2349 * @ESdcs: ESC P parsed 2350 * @ESANSI_last: last state for ignoring ansi control sequences 2351 */ 2352 enum vc_ctl_state { 2353 ESnormal, 2354 ESesc, 2355 ESsquare, 2356 ESgetpars, 2357 ESgetsubpars, 2358 ESfunckey, 2359 EShash, 2360 ESsetG0, 2361 ESsetG1, 2362 ESpercent, 2363 EScsiignore, 2364 ESnonstd, 2365 ESpalette, 2366 ESosc, 2367 ESANSI_first = ESosc, 2368 ESapc, 2369 ESpm, 2370 ESdcs, 2371 ESANSI_last = ESdcs, 2372 }; 2373 2374 /* console_lock is held (except via vc_init()) */ 2375 static void reset_terminal(struct vc_data *vc, int do_clear) 2376 { 2377 unsigned int i; 2378 2379 vc->vc_top = 0; 2380 vc->vc_bottom = vc->vc_rows; 2381 vc->vc_state = ESnormal; 2382 vc->vc_priv = EPecma; 2383 vc->vc_translate = set_translate(LAT1_MAP, vc); 2384 vc->state.Gx_charset[0] = LAT1_MAP; 2385 vc->state.Gx_charset[1] = GRAF_MAP; 2386 vc->state.charset = 0; 2387 vc->vc_need_wrap = 0; 2388 vc->vc_report_mouse = 0; 2389 vc->vc_bracketed_paste = 0; 2390 vc->vc_utf = default_utf8; 2391 vc->vc_utf_count = 0; 2392 2393 vc->vc_disp_ctrl = 0; 2394 vc->vc_toggle_meta = 0; 2395 2396 vc->vc_decscnm = 0; 2397 vc->vc_decom = 0; 2398 vc->vc_decawm = 1; 2399 vc->vc_deccm = global_cursor_default; 2400 vc->vc_decim = 0; 2401 2402 if (vc->vc_saved_screen != NULL) { 2403 kfree(vc->vc_saved_screen); 2404 vc->vc_saved_screen = NULL; 2405 vc_uniscr_free(vc->vc_saved_uni_lines); 2406 vc->vc_saved_uni_lines = NULL; 2407 vc->vc_saved_rows = 0; 2408 vc->vc_saved_cols = 0; 2409 } 2410 2411 vt_reset_keyboard(vc->vc_num); 2412 2413 vc->vc_cursor_type = cur_default; 2414 vc->vc_complement_mask = vc->vc_s_complement_mask; 2415 2416 default_attr(vc); 2417 update_attr(vc); 2418 2419 bitmap_zero(vc->vc_tab_stop, VC_TABSTOPS_COUNT); 2420 for (i = 0; i < VC_TABSTOPS_COUNT; i += 8) 2421 set_bit(i, vc->vc_tab_stop); 2422 2423 vc->vc_bell_pitch = DEFAULT_BELL_PITCH; 2424 vc->vc_bell_duration = DEFAULT_BELL_DURATION; 2425 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS; 2426 2427 gotoxy(vc, 0, 0); 2428 save_cur(vc); 2429 if (do_clear) 2430 csi_J(vc, CSI_J_VISIBLE); 2431 } 2432 2433 static void vc_setGx(struct vc_data *vc, unsigned int which, u8 c) 2434 { 2435 unsigned char *charset = &vc->state.Gx_charset[which]; 2436 2437 switch (c) { 2438 case '0': 2439 *charset = GRAF_MAP; 2440 break; 2441 case 'B': 2442 *charset = LAT1_MAP; 2443 break; 2444 case 'U': 2445 *charset = IBMPC_MAP; 2446 break; 2447 case 'K': 2448 *charset = USER_MAP; 2449 break; 2450 } 2451 2452 if (vc->state.charset == which) 2453 vc->vc_translate = set_translate(*charset, vc); 2454 } 2455 2456 static bool ansi_control_string(enum vc_ctl_state state) 2457 { 2458 return state >= ESANSI_first && state <= ESANSI_last; 2459 } 2460 2461 enum { 2462 ASCII_NULL = 0, 2463 ASCII_BELL = 7, 2464 ASCII_BACKSPACE = 8, 2465 ASCII_IGNORE_FIRST = ASCII_BACKSPACE, 2466 ASCII_HTAB = 9, 2467 ASCII_LINEFEED = 10, 2468 ASCII_VTAB = 11, 2469 ASCII_FORMFEED = 12, 2470 ASCII_CAR_RET = 13, 2471 ASCII_IGNORE_LAST = ASCII_CAR_RET, 2472 ASCII_SHIFTOUT = 14, 2473 ASCII_SHIFTIN = 15, 2474 ASCII_CANCEL = 24, 2475 ASCII_SUBSTITUTE = 26, 2476 ASCII_ESCAPE = 27, 2477 ASCII_CSI_IGNORE_FIRST = ' ', /* 0x2x, 0x3a and 0x3c - 0x3f */ 2478 ASCII_CSI_IGNORE_LAST = '?', 2479 ASCII_DEL = 127, 2480 ASCII_EXT_CSI = 128 + ASCII_ESCAPE, 2481 }; 2482 2483 /* 2484 * Handle ascii characters in control sequences and change states accordingly. 2485 * E.g. ESC sets the state of vc to ESesc. 2486 * 2487 * Returns: true if @c handled. 2488 */ 2489 static bool handle_ascii(struct tty_struct *tty, struct vc_data *vc, u8 c) 2490 { 2491 switch (c) { 2492 case ASCII_NULL: 2493 return true; 2494 case ASCII_BELL: 2495 if (ansi_control_string(vc->vc_state)) 2496 vc->vc_state = ESnormal; 2497 else if (vc->vc_bell_duration) 2498 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration); 2499 return true; 2500 case ASCII_BACKSPACE: 2501 bs(vc); 2502 return true; 2503 case ASCII_HTAB: 2504 vc->vc_pos -= (vc->state.x << 1); 2505 2506 vc->state.x = find_next_bit(vc->vc_tab_stop, 2507 min(vc->vc_cols - 1, VC_TABSTOPS_COUNT), 2508 vc->state.x + 1); 2509 if (vc->state.x >= VC_TABSTOPS_COUNT) 2510 vc->state.x = vc->vc_cols - 1; 2511 2512 vc->vc_pos += (vc->state.x << 1); 2513 notify_write(vc, '\t'); 2514 return true; 2515 case ASCII_LINEFEED: 2516 case ASCII_VTAB: 2517 case ASCII_FORMFEED: 2518 lf(vc); 2519 if (!is_kbd(vc, lnm)) 2520 return true; 2521 fallthrough; 2522 case ASCII_CAR_RET: 2523 cr(vc); 2524 return true; 2525 case ASCII_SHIFTOUT: 2526 vc->state.charset = 1; 2527 vc->vc_translate = set_translate(vc->state.Gx_charset[1], vc); 2528 vc->vc_disp_ctrl = 1; 2529 return true; 2530 case ASCII_SHIFTIN: 2531 vc->state.charset = 0; 2532 vc->vc_translate = set_translate(vc->state.Gx_charset[0], vc); 2533 vc->vc_disp_ctrl = 0; 2534 return true; 2535 case ASCII_CANCEL: 2536 case ASCII_SUBSTITUTE: 2537 vc->vc_state = ESnormal; 2538 return true; 2539 case ASCII_ESCAPE: 2540 vc->vc_state = ESesc; 2541 return true; 2542 case ASCII_DEL: 2543 del(vc); 2544 return true; 2545 case ASCII_EXT_CSI: 2546 vc->vc_state = ESsquare; 2547 return true; 2548 } 2549 2550 return false; 2551 } 2552 2553 /* 2554 * Handle a character (@c) following an ESC (when @vc is in the ESesc state). 2555 * E.g. previous ESC with @c == '[' here yields the ESsquare state (that is: 2556 * CSI). 2557 */ 2558 static void handle_esc(struct tty_struct *tty, struct vc_data *vc, u8 c) 2559 { 2560 vc->vc_state = ESnormal; 2561 switch (c) { 2562 case '[': 2563 vc->vc_state = ESsquare; 2564 break; 2565 case ']': 2566 vc->vc_state = ESnonstd; 2567 break; 2568 case '_': 2569 vc->vc_state = ESapc; 2570 break; 2571 case '^': 2572 vc->vc_state = ESpm; 2573 break; 2574 case '%': 2575 vc->vc_state = ESpercent; 2576 break; 2577 case 'E': 2578 cr(vc); 2579 lf(vc); 2580 break; 2581 case 'M': 2582 ri(vc); 2583 break; 2584 case 'D': 2585 lf(vc); 2586 break; 2587 case 'H': 2588 if (vc->state.x < VC_TABSTOPS_COUNT) 2589 set_bit(vc->state.x, vc->vc_tab_stop); 2590 break; 2591 case 'P': 2592 vc->vc_state = ESdcs; 2593 break; 2594 case 'Z': 2595 respond_ID(tty); 2596 break; 2597 case '7': 2598 save_cur(vc); 2599 break; 2600 case '8': 2601 restore_cur(vc); 2602 break; 2603 case '(': 2604 vc->vc_state = ESsetG0; 2605 break; 2606 case ')': 2607 vc->vc_state = ESsetG1; 2608 break; 2609 case '#': 2610 vc->vc_state = EShash; 2611 break; 2612 case 'c': 2613 reset_terminal(vc, 1); 2614 break; 2615 case '>': /* Numeric keypad */ 2616 clr_kbd(vc, kbdapplic); 2617 break; 2618 case '=': /* Appl. keypad */ 2619 set_kbd(vc, kbdapplic); 2620 break; 2621 } 2622 } 2623 2624 /* 2625 * Handle special DEC control sequences ("ESC [ ? parameters char"). Parameters 2626 * are in @vc->vc_par and the char is in @c here. 2627 */ 2628 static void csi_DEC(struct tty_struct *tty, struct vc_data *vc, u8 c) 2629 { 2630 switch (c) { 2631 case 'h': 2632 csi_DEC_hl(vc, true); 2633 break; 2634 case 'l': 2635 csi_DEC_hl(vc, false); 2636 break; 2637 case 'c': 2638 if (vc->vc_par[0]) 2639 vc->vc_cursor_type = CUR_MAKE(vc->vc_par[0], 2640 vc->vc_par[1], 2641 vc->vc_par[2]); 2642 else 2643 vc->vc_cursor_type = cur_default; 2644 break; 2645 case 'm': 2646 clear_selection(); 2647 if (vc->vc_par[0]) 2648 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1]; 2649 else 2650 vc->vc_complement_mask = vc->vc_s_complement_mask; 2651 break; 2652 case 'n': 2653 if (vc->vc_par[0] == 5) 2654 status_report(tty); 2655 else if (vc->vc_par[0] == 6) 2656 cursor_report(vc, tty); 2657 break; 2658 } 2659 } 2660 2661 /* 2662 * Handle Control Sequence Introducer control characters. That is 2663 * "ESC [ parameters char". Parameters are in @vc->vc_par and the char is in 2664 * @c here. 2665 */ 2666 static void csi_ECMA(struct tty_struct *tty, struct vc_data *vc, u8 c) 2667 { 2668 switch (c) { 2669 case 'G': 2670 case '`': 2671 if (vc->vc_par[0]) 2672 vc->vc_par[0]--; 2673 gotoxy(vc, vc->vc_par[0], vc->state.y); 2674 break; 2675 case 'A': 2676 if (!vc->vc_par[0]) 2677 vc->vc_par[0]++; 2678 gotoxy(vc, vc->state.x, vc->state.y - vc->vc_par[0]); 2679 break; 2680 case 'B': 2681 case 'e': 2682 if (!vc->vc_par[0]) 2683 vc->vc_par[0]++; 2684 gotoxy(vc, vc->state.x, vc->state.y + vc->vc_par[0]); 2685 break; 2686 case 'C': 2687 case 'a': 2688 if (!vc->vc_par[0]) 2689 vc->vc_par[0]++; 2690 gotoxy(vc, vc->state.x + vc->vc_par[0], vc->state.y); 2691 break; 2692 case 'D': 2693 if (!vc->vc_par[0]) 2694 vc->vc_par[0]++; 2695 gotoxy(vc, vc->state.x - vc->vc_par[0], vc->state.y); 2696 break; 2697 case 'E': 2698 if (!vc->vc_par[0]) 2699 vc->vc_par[0]++; 2700 gotoxy(vc, 0, vc->state.y + vc->vc_par[0]); 2701 break; 2702 case 'F': 2703 if (!vc->vc_par[0]) 2704 vc->vc_par[0]++; 2705 gotoxy(vc, 0, vc->state.y - vc->vc_par[0]); 2706 break; 2707 case 'd': 2708 if (vc->vc_par[0]) 2709 vc->vc_par[0]--; 2710 gotoxay(vc, vc->state.x ,vc->vc_par[0]); 2711 break; 2712 case 'H': 2713 case 'f': 2714 if (vc->vc_par[0]) 2715 vc->vc_par[0]--; 2716 if (vc->vc_par[1]) 2717 vc->vc_par[1]--; 2718 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]); 2719 break; 2720 case 'J': 2721 csi_J(vc, vc->vc_par[0]); 2722 break; 2723 case 'K': 2724 csi_K(vc); 2725 break; 2726 case 'L': 2727 csi_L(vc); 2728 break; 2729 case 'M': 2730 csi_M(vc); 2731 break; 2732 case 'P': 2733 csi_P(vc); 2734 break; 2735 case 'c': 2736 if (!vc->vc_par[0]) 2737 respond_ID(tty); 2738 break; 2739 case 'g': 2740 if (!vc->vc_par[0] && vc->state.x < VC_TABSTOPS_COUNT) 2741 set_bit(vc->state.x, vc->vc_tab_stop); 2742 else if (vc->vc_par[0] == 3) 2743 bitmap_zero(vc->vc_tab_stop, VC_TABSTOPS_COUNT); 2744 break; 2745 case 'h': 2746 csi_hl(vc, true); 2747 break; 2748 case 'l': 2749 csi_hl(vc, false); 2750 break; 2751 case 'm': 2752 csi_m(vc); 2753 break; 2754 case 'n': 2755 if (vc->vc_par[0] == 5) 2756 status_report(tty); 2757 else if (vc->vc_par[0] == 6) 2758 cursor_report(vc, tty); 2759 break; 2760 case 'q': /* DECLL - but only 3 leds */ 2761 /* map 0,1,2,3 to 0,1,2,4 */ 2762 if (vc->vc_par[0] < 4) 2763 vt_set_led_state(vc->vc_num, 2764 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4); 2765 break; 2766 case 'r': 2767 if (!vc->vc_par[0]) 2768 vc->vc_par[0]++; 2769 if (!vc->vc_par[1]) 2770 vc->vc_par[1] = vc->vc_rows; 2771 /* Minimum allowed region is 2 lines */ 2772 if (vc->vc_par[0] < vc->vc_par[1] && 2773 vc->vc_par[1] <= vc->vc_rows) { 2774 vc->vc_top = vc->vc_par[0] - 1; 2775 vc->vc_bottom = vc->vc_par[1]; 2776 gotoxay(vc, 0, 0); 2777 } 2778 break; 2779 case 's': 2780 save_cur(vc); 2781 break; 2782 case 'u': 2783 restore_cur(vc); 2784 break; 2785 case 'X': 2786 csi_X(vc); 2787 break; 2788 case '@': 2789 csi_at(vc, vc->vc_par[0]); 2790 break; 2791 case ']': 2792 csi_RSB(vc); 2793 break; 2794 } 2795 2796 } 2797 2798 static void vc_reset_params(struct vc_data *vc) 2799 { 2800 memset(vc->vc_par, 0, sizeof(vc->vc_par)); 2801 vc->vc_npar = 0; 2802 } 2803 2804 /* console_lock is held */ 2805 static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, u8 c) 2806 { 2807 /* 2808 * Control characters can be used in the _middle_ 2809 * of an escape sequence, aside from ANSI control strings. 2810 */ 2811 if (ansi_control_string(vc->vc_state) && c >= ASCII_IGNORE_FIRST && 2812 c <= ASCII_IGNORE_LAST) 2813 return; 2814 2815 if (handle_ascii(tty, vc, c)) 2816 return; 2817 2818 switch(vc->vc_state) { 2819 case ESesc: /* ESC */ 2820 handle_esc(tty, vc, c); 2821 return; 2822 case ESnonstd: /* ESC ] aka OSC */ 2823 switch (c) { 2824 case 'P': /* palette escape sequence */ 2825 vc_reset_params(vc); 2826 vc->vc_state = ESpalette; 2827 return; 2828 case 'R': /* reset palette */ 2829 reset_palette(vc); 2830 break; 2831 case '0' ... '9': 2832 vc->vc_state = ESosc; 2833 return; 2834 } 2835 vc->vc_state = ESnormal; 2836 return; 2837 case ESpalette: /* ESC ] P aka OSC P */ 2838 if (isxdigit(c)) { 2839 vc->vc_par[vc->vc_npar++] = hex_to_bin(c); 2840 if (vc->vc_npar == 7) { 2841 int i = vc->vc_par[0] * 3, j = 1; 2842 vc->vc_palette[i] = 16 * vc->vc_par[j++]; 2843 vc->vc_palette[i++] += vc->vc_par[j++]; 2844 vc->vc_palette[i] = 16 * vc->vc_par[j++]; 2845 vc->vc_palette[i++] += vc->vc_par[j++]; 2846 vc->vc_palette[i] = 16 * vc->vc_par[j++]; 2847 vc->vc_palette[i] += vc->vc_par[j]; 2848 set_palette(vc); 2849 vc->vc_state = ESnormal; 2850 } 2851 } else 2852 vc->vc_state = ESnormal; 2853 return; 2854 case ESsquare: /* ESC [ aka CSI, parameters or modifiers expected */ 2855 vc_reset_params(vc); 2856 2857 vc->vc_state = ESgetpars; 2858 switch (c) { 2859 case '[': /* Function key */ 2860 vc->vc_state = ESfunckey; 2861 return; 2862 case '?': 2863 vc->vc_priv = EPdec; 2864 return; 2865 case '>': 2866 vc->vc_priv = EPgt; 2867 return; 2868 case '=': 2869 vc->vc_priv = EPeq; 2870 return; 2871 case '<': 2872 vc->vc_priv = EPlt; 2873 return; 2874 } 2875 vc->vc_priv = EPecma; 2876 fallthrough; 2877 case ESgetpars: /* ESC [ aka CSI, parameters expected */ 2878 switch (c) { 2879 case ':': /* ITU-T T.416 color subparameters */ 2880 if (vc->vc_par[vc->vc_npar] == CSI_m_FG_COLOR || 2881 vc->vc_par[vc->vc_npar] == CSI_m_BG_COLOR || 2882 vc->vc_par[vc->vc_npar] == CSI_m_UNDERLINE_COLOR) 2883 vc->vc_state = ESgetsubpars; 2884 else 2885 break; 2886 fallthrough; 2887 case ';': 2888 if (vc->vc_npar < NPAR - 1) { 2889 vc->vc_npar++; 2890 return; 2891 } 2892 break; 2893 case '0' ... '9': 2894 vc->vc_par[vc->vc_npar] *= 10; 2895 vc->vc_par[vc->vc_npar] += c - '0'; 2896 return; 2897 } 2898 if (c >= ASCII_CSI_IGNORE_FIRST && c <= ASCII_CSI_IGNORE_LAST) { 2899 vc->vc_state = EScsiignore; 2900 return; 2901 } 2902 2903 /* parameters done, handle the control char @c */ 2904 2905 vc->vc_state = ESnormal; 2906 2907 switch (vc->vc_priv) { 2908 case EPdec: 2909 csi_DEC(tty, vc, c); 2910 return; 2911 case EPecma: 2912 csi_ECMA(tty, vc, c); 2913 return; 2914 default: 2915 return; 2916 } 2917 case ESgetsubpars: /* ESC [ 38/48/58, subparameters expected */ 2918 switch (c) { 2919 case ':': 2920 case ';': 2921 if (vc->vc_npar < NPAR - 1) { 2922 vc->vc_npar++; 2923 return; 2924 } 2925 break; 2926 case '0' ... '9': 2927 vc->vc_par[vc->vc_npar] *= 10; 2928 vc->vc_par[vc->vc_npar] += c - '0'; 2929 return; 2930 } 2931 if (c >= ASCII_CSI_IGNORE_FIRST && c <= ASCII_CSI_IGNORE_LAST) { 2932 vc->vc_state = EScsiignore; 2933 return; 2934 } 2935 2936 /* parameters done, handle the control char @c */ 2937 2938 vc->vc_state = ESnormal; 2939 2940 switch (vc->vc_priv) { 2941 case EPdec: 2942 csi_DEC(tty, vc, c); 2943 return; 2944 case EPecma: 2945 csi_ECMA(tty, vc, c); 2946 return; 2947 default: 2948 return; 2949 } 2950 case EScsiignore: 2951 if (c >= ASCII_CSI_IGNORE_FIRST && c <= ASCII_CSI_IGNORE_LAST) 2952 return; 2953 vc->vc_state = ESnormal; 2954 return; 2955 case ESpercent: /* ESC % */ 2956 vc->vc_state = ESnormal; 2957 switch (c) { 2958 case '@': /* defined in ISO 2022 */ 2959 vc->vc_utf = 0; 2960 return; 2961 case 'G': /* prelim official escape code */ 2962 case '8': /* retained for compatibility */ 2963 vc->vc_utf = 1; 2964 return; 2965 } 2966 return; 2967 case ESfunckey: /* ESC [ [ aka CSI [ */ 2968 vc->vc_state = ESnormal; 2969 return; 2970 case EShash: /* ESC # */ 2971 vc->vc_state = ESnormal; 2972 if (c == '8') { 2973 /* DEC screen alignment test. kludge :-) */ 2974 vc->vc_video_erase_char = 2975 (vc->vc_video_erase_char & 0xff00) | 'E'; 2976 csi_J(vc, CSI_J_VISIBLE); 2977 vc->vc_video_erase_char = 2978 (vc->vc_video_erase_char & 0xff00) | ' '; 2979 if (con_should_update(vc)) 2980 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2); 2981 } 2982 return; 2983 case ESsetG0: /* ESC ( */ 2984 vc_setGx(vc, 0, c); 2985 vc->vc_state = ESnormal; 2986 return; 2987 case ESsetG1: /* ESC ) */ 2988 vc_setGx(vc, 1, c); 2989 vc->vc_state = ESnormal; 2990 return; 2991 case ESapc: /* ESC _ */ 2992 return; 2993 case ESosc: /* ESC ] [0-9] aka OSC [0-9] */ 2994 return; 2995 case ESpm: /* ESC ^ */ 2996 return; 2997 case ESdcs: /* ESC P */ 2998 return; 2999 default: 3000 vc->vc_state = ESnormal; 3001 } 3002 } 3003 3004 struct vc_draw_region { 3005 unsigned long from, to; 3006 int x; 3007 }; 3008 3009 static void con_flush(struct vc_data *vc, struct vc_draw_region *draw) 3010 { 3011 if (draw->x < 0) 3012 return; 3013 3014 vc->vc_sw->con_putcs(vc, (u16 *)draw->from, 3015 (u16 *)draw->to - (u16 *)draw->from, vc->state.y, 3016 draw->x); 3017 draw->x = -1; 3018 } 3019 3020 static inline int vc_translate_ascii(const struct vc_data *vc, int c) 3021 { 3022 if (IS_ENABLED(CONFIG_CONSOLE_TRANSLATIONS)) { 3023 if (vc->vc_toggle_meta) 3024 c |= 0x80; 3025 3026 return vc->vc_translate[c]; 3027 } 3028 3029 return c; 3030 } 3031 3032 3033 /** 3034 * vc_sanitize_unicode - Replace invalid Unicode code points with ``U+FFFD`` 3035 * @c: the received code point 3036 */ 3037 static inline int vc_sanitize_unicode(const int c) 3038 { 3039 if (c >= 0xd800 && c <= 0xdfff) 3040 return 0xfffd; 3041 3042 return c; 3043 } 3044 3045 /** 3046 * vc_translate_unicode - Combine UTF-8 into Unicode in &vc_data.vc_utf_char 3047 * @vc: virtual console 3048 * @c: UTF-8 byte to translate 3049 * @rescan: set to true iff @c wasn't consumed here and needs to be re-processed 3050 * 3051 * * &vc_data.vc_utf_char is the being-constructed Unicode code point. 3052 * * &vc_data.vc_utf_count is the number of continuation bytes still expected to 3053 * arrive. 3054 * * &vc_data.vc_npar is the number of continuation bytes arrived so far. 3055 * 3056 * Return: 3057 * * %-1 - Input OK so far, @c consumed, further bytes expected. 3058 * * %0xFFFD - Possibility 1: input invalid, @c may have been consumed (see 3059 * desc. of @rescan). Possibility 2: input OK, @c consumed, 3060 * ``U+FFFD`` is the resulting code point. ``U+FFFD`` is valid, 3061 * ``REPLACEMENT CHARACTER``. 3062 * * otherwise - Input OK, @c consumed, resulting code point returned. 3063 */ 3064 static int vc_translate_unicode(struct vc_data *vc, int c, bool *rescan) 3065 { 3066 static const u32 utf8_length_changes[] = {0x7f, 0x7ff, 0xffff, 0x10ffff}; 3067 3068 /* Continuation byte received */ 3069 if ((c & 0xc0) == 0x80) { 3070 /* Unexpected continuation byte? */ 3071 if (!vc->vc_utf_count) 3072 goto bad_sequence; 3073 3074 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f); 3075 vc->vc_npar++; 3076 if (--vc->vc_utf_count) 3077 goto need_more_bytes; 3078 3079 /* Got a whole character */ 3080 c = vc->vc_utf_char; 3081 /* Reject overlong sequences */ 3082 if (c <= utf8_length_changes[vc->vc_npar - 1] || 3083 c > utf8_length_changes[vc->vc_npar]) 3084 goto bad_sequence; 3085 3086 return vc_sanitize_unicode(c); 3087 } 3088 3089 /* Single ASCII byte or first byte of a sequence received */ 3090 if (vc->vc_utf_count) { 3091 /* A continuation byte was expected */ 3092 *rescan = true; 3093 vc->vc_utf_count = 0; 3094 goto bad_sequence; 3095 } 3096 3097 /* Nothing to do if an ASCII byte was received */ 3098 if (c <= 0x7f) 3099 return c; 3100 3101 /* First byte of a multibyte sequence received */ 3102 vc->vc_npar = 0; 3103 if ((c & 0xe0) == 0xc0) { 3104 vc->vc_utf_count = 1; 3105 vc->vc_utf_char = (c & 0x1f); 3106 } else if ((c & 0xf0) == 0xe0) { 3107 vc->vc_utf_count = 2; 3108 vc->vc_utf_char = (c & 0x0f); 3109 } else if ((c & 0xf8) == 0xf0) { 3110 vc->vc_utf_count = 3; 3111 vc->vc_utf_char = (c & 0x07); 3112 } else { 3113 goto bad_sequence; 3114 } 3115 3116 need_more_bytes: 3117 return -1; 3118 3119 bad_sequence: 3120 return 0xfffd; 3121 } 3122 3123 static int vc_translate(struct vc_data *vc, int *c, bool *rescan) 3124 { 3125 /* Do no translation at all in control states */ 3126 if (vc->vc_state != ESnormal) 3127 return *c; 3128 3129 if (vc->vc_utf && !vc->vc_disp_ctrl) 3130 return *c = vc_translate_unicode(vc, *c, rescan); 3131 3132 /* no utf or alternate charset mode */ 3133 return vc_translate_ascii(vc, *c); 3134 } 3135 3136 static inline unsigned char vc_invert_attr(const struct vc_data *vc) 3137 { 3138 if (!vc->vc_can_do_color) 3139 return vc->vc_attr ^ 0x08; 3140 3141 if (vc->vc_hi_font_mask == 0x100) 3142 return (vc->vc_attr & 0x11) | 3143 ((vc->vc_attr & 0xe0) >> 4) | 3144 ((vc->vc_attr & 0x0e) << 4); 3145 3146 return (vc->vc_attr & 0x88) | 3147 ((vc->vc_attr & 0x70) >> 4) | 3148 ((vc->vc_attr & 0x07) << 4); 3149 } 3150 3151 static bool vc_is_control(struct vc_data *vc, int tc, int c) 3152 { 3153 /* 3154 * A bitmap for codes <32. A bit of 1 indicates that the code 3155 * corresponding to that bit number invokes some special action (such 3156 * as cursor movement) and should not be displayed as a glyph unless 3157 * the disp_ctrl mode is explicitly enabled. 3158 */ 3159 static const u32 CTRL_ACTION = BIT(ASCII_NULL) | 3160 GENMASK(ASCII_SHIFTIN, ASCII_BELL) | BIT(ASCII_CANCEL) | 3161 BIT(ASCII_SUBSTITUTE) | BIT(ASCII_ESCAPE); 3162 /* Cannot be overridden by disp_ctrl */ 3163 static const u32 CTRL_ALWAYS = BIT(ASCII_NULL) | BIT(ASCII_BACKSPACE) | 3164 BIT(ASCII_LINEFEED) | BIT(ASCII_SHIFTIN) | BIT(ASCII_SHIFTOUT) | 3165 BIT(ASCII_CAR_RET) | BIT(ASCII_FORMFEED) | BIT(ASCII_ESCAPE); 3166 3167 if (vc->vc_state != ESnormal) 3168 return true; 3169 3170 if (!tc) 3171 return true; 3172 3173 /* 3174 * If the original code was a control character we only allow a glyph 3175 * to be displayed if the code is not normally used (such as for cursor 3176 * movement) or if the disp_ctrl mode has been explicitly enabled. 3177 * Certain characters (as given by the CTRL_ALWAYS bitmap) are always 3178 * displayed as control characters, as the console would be pretty 3179 * useless without them; to display an arbitrary font position use the 3180 * direct-to-font zone in UTF-8 mode. 3181 */ 3182 if (c < BITS_PER_TYPE(CTRL_ALWAYS)) { 3183 if (vc->vc_disp_ctrl) 3184 return CTRL_ALWAYS & BIT(c); 3185 else 3186 return vc->vc_utf || (CTRL_ACTION & BIT(c)); 3187 } 3188 3189 if (c == ASCII_DEL && !vc->vc_disp_ctrl) 3190 return true; 3191 3192 if (c == ASCII_EXT_CSI) 3193 return true; 3194 3195 return false; 3196 } 3197 3198 static void vc_con_rewind(struct vc_data *vc) 3199 { 3200 if (vc->state.x && !vc->vc_need_wrap) { 3201 vc->vc_pos -= 2; 3202 vc->state.x--; 3203 } 3204 vc->vc_need_wrap = 0; 3205 } 3206 3207 #define UCS_ZWS 0x200b /* Zero Width Space */ 3208 #define UCS_VS16 0xfe0f /* Variation Selector 16 */ 3209 #define UCS_REPLACEMENT 0xfffd /* Replacement Character */ 3210 3211 static int vc_process_ucs(struct vc_data *vc, int *c, int *tc) 3212 { 3213 u32 prev_c, curr_c = *c; 3214 unsigned int w = ucs_get_width(curr_c); 3215 3216 if (likely(w == 1)) 3217 return 1; 3218 3219 if (w == 2) { 3220 /* 3221 * The Unicode screen memory is allocated only when 3222 * required. This is one such case as we need to remember 3223 * which displayed characters are double-width. 3224 */ 3225 vc_uniscr_check(vc); 3226 return 2; 3227 } 3228 3229 /* From here curr_c is known to be zero-width. */ 3230 3231 if (ucs_get_width(vc_uniscr_getc(vc, -2)) == 2) { 3232 /* 3233 * Let's merge this zero-width code point with the preceding 3234 * double-width code point by replacing the existing 3235 * zero-width space padding. To do so we rewind one column 3236 * and pretend this has a width of 1. 3237 * We give the legacy display the same initial space padding. 3238 */ 3239 vc_con_rewind(vc); 3240 *tc = ' '; 3241 return 1; 3242 } 3243 3244 /* From here the preceding character, if any, must be single-width. */ 3245 prev_c = vc_uniscr_getc(vc, -1); 3246 3247 if (curr_c == UCS_VS16 && prev_c != 0) { 3248 /* 3249 * VS16 (U+FE0F) is special. It typically turns the preceding 3250 * single-width character into a double-width one. Let it 3251 * have a width of 1 effectively making the combination with 3252 * the preceding character double-width. 3253 */ 3254 *tc = ' '; 3255 return 1; 3256 } 3257 3258 /* try recomposition */ 3259 prev_c = ucs_recompose(prev_c, curr_c); 3260 if (prev_c != 0) { 3261 vc_con_rewind(vc); 3262 *tc = *c = prev_c; 3263 return 1; 3264 } 3265 3266 /* Otherwise zero-width code points are ignored. */ 3267 return 0; 3268 } 3269 3270 static int vc_get_glyph(struct vc_data *vc, int tc) 3271 { 3272 int glyph = conv_uni_to_pc(vc, tc); 3273 u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff; 3274 3275 if (!(glyph & ~charmask)) 3276 return glyph; 3277 3278 if (glyph == -1) 3279 return -1; /* nothing to display */ 3280 3281 /* Glyph not found */ 3282 if ((!vc->vc_utf || vc->vc_disp_ctrl || tc < 128) && !(tc & ~charmask)) { 3283 /* 3284 * In legacy mode use the glyph we get by a 1:1 mapping. 3285 * This would make absolutely no sense with Unicode in mind, but do this for 3286 * ASCII characters since a font may lack Unicode mapping info and we don't 3287 * want to end up with having question marks only. 3288 */ 3289 return tc; 3290 } 3291 3292 /* 3293 * The Unicode screen memory is allocated only when required. 3294 * This is one such case: we're about to "cheat" with the displayed 3295 * character meaning the simple screen buffer won't hold the original 3296 * information, whereas the Unicode screen buffer always does. 3297 */ 3298 vc_uniscr_check(vc); 3299 3300 /* Try getting a simpler fallback character. */ 3301 tc = ucs_get_fallback(tc); 3302 if (tc) 3303 return vc_get_glyph(vc, tc); 3304 3305 /* Display U+FFFD (Unicode Replacement Character). */ 3306 return conv_uni_to_pc(vc, UCS_REPLACEMENT); 3307 } 3308 3309 static int vc_con_write_normal(struct vc_data *vc, int tc, int c, 3310 struct vc_draw_region *draw) 3311 { 3312 int next_c; 3313 unsigned char vc_attr = vc->vc_attr; 3314 u16 himask = vc->vc_hi_font_mask; 3315 u8 width = 1; 3316 bool inverse = false; 3317 3318 if (vc->vc_utf && !vc->vc_disp_ctrl) { 3319 width = vc_process_ucs(vc, &c, &tc); 3320 if (!width) 3321 goto out; 3322 } 3323 3324 /* Now try to find out how to display it */ 3325 tc = vc_get_glyph(vc, tc); 3326 if (tc == -1) 3327 return -1; /* nothing to display */ 3328 if (tc < 0) { 3329 inverse = true; 3330 tc = conv_uni_to_pc(vc, '?'); 3331 if (tc < 0) 3332 tc = '?'; 3333 3334 vc_attr = vc_invert_attr(vc); 3335 con_flush(vc, draw); 3336 } 3337 3338 next_c = c; 3339 while (1) { 3340 if (vc->vc_need_wrap || vc->vc_decim) 3341 con_flush(vc, draw); 3342 if (vc->vc_need_wrap) { 3343 cr(vc); 3344 lf(vc); 3345 } 3346 if (vc->vc_decim) 3347 insert_char(vc, 1); 3348 vc_uniscr_putc(vc, next_c); 3349 3350 if (himask) 3351 tc = ((tc & 0x100) ? himask : 0) | 3352 (tc & 0xff); 3353 tc |= (vc_attr << 8) & ~himask; 3354 3355 scr_writew(tc, (u16 *)vc->vc_pos); 3356 3357 if (con_should_update(vc) && draw->x < 0) { 3358 draw->x = vc->state.x; 3359 draw->from = vc->vc_pos; 3360 } 3361 if (vc->state.x == vc->vc_cols - 1) { 3362 vc->vc_need_wrap = vc->vc_decawm; 3363 draw->to = vc->vc_pos + 2; 3364 } else { 3365 vc->state.x++; 3366 draw->to = (vc->vc_pos += 2); 3367 } 3368 3369 if (!--width) 3370 break; 3371 3372 /* A space is printed in the second column */ 3373 tc = conv_uni_to_pc(vc, ' '); 3374 if (tc < 0) 3375 tc = ' '; 3376 /* 3377 * Store a zero-width space in the Unicode screen given that 3378 * the previous code point is semantically double width. 3379 */ 3380 next_c = UCS_ZWS; 3381 } 3382 3383 out: 3384 notify_write(vc, c); 3385 3386 if (inverse) 3387 con_flush(vc, draw); 3388 3389 return 0; 3390 } 3391 3392 /* acquires console_lock */ 3393 static int do_con_write(struct tty_struct *tty, const u8 *buf, int count) 3394 { 3395 struct vc_draw_region draw = { 3396 .x = -1, 3397 }; 3398 int c, tc, n = 0; 3399 unsigned int currcons; 3400 struct vc_data *vc = tty->driver_data; 3401 struct vt_notifier_param param; 3402 bool rescan; 3403 3404 if (in_interrupt()) 3405 return count; 3406 3407 guard(console_lock)(); 3408 currcons = vc->vc_num; 3409 if (!vc_cons_allocated(currcons)) { 3410 /* could this happen? */ 3411 pr_warn_once("con_write: tty %d not allocated\n", currcons+1); 3412 return 0; 3413 } 3414 3415 3416 /* undraw cursor first */ 3417 if (con_is_fg(vc)) 3418 hide_cursor(vc); 3419 3420 param.vc = vc; 3421 3422 while (!tty->flow.stopped && count) { 3423 u8 orig = *buf; 3424 buf++; 3425 n++; 3426 count--; 3427 rescan_last_byte: 3428 c = orig; 3429 rescan = false; 3430 3431 tc = vc_translate(vc, &c, &rescan); 3432 if (tc == -1) 3433 continue; 3434 3435 param.c = tc; 3436 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE, 3437 ¶m) == NOTIFY_STOP) 3438 continue; 3439 3440 if (vc_is_control(vc, tc, c)) { 3441 con_flush(vc, &draw); 3442 do_con_trol(tty, vc, orig); 3443 continue; 3444 } 3445 3446 if (vc_con_write_normal(vc, tc, c, &draw) < 0) 3447 continue; 3448 3449 if (rescan) 3450 goto rescan_last_byte; 3451 } 3452 con_flush(vc, &draw); 3453 notify_update(vc); 3454 3455 return n; 3456 } 3457 3458 /* 3459 * This is the console switching callback. 3460 * 3461 * Doing console switching in a process context allows 3462 * us to do the switches asynchronously (needed when we want 3463 * to switch due to a keyboard interrupt). Synchronization 3464 * with other console code and prevention of re-entrancy is 3465 * ensured with console_lock. 3466 */ 3467 static void console_callback(struct work_struct *ignored) 3468 { 3469 guard(console_lock)(); 3470 3471 if (want_console >= 0) { 3472 if (want_console != fg_console && 3473 vc_cons_allocated(want_console)) { 3474 hide_cursor(vc_cons[fg_console].d); 3475 change_console(vc_cons[want_console].d); 3476 /* we only changed when the console had already 3477 been allocated - a new console is not created 3478 in an interrupt routine */ 3479 } 3480 want_console = -1; 3481 } 3482 if (do_poke_blanked_console) { /* do not unblank for a LED change */ 3483 do_poke_blanked_console = 0; 3484 poke_blanked_console(); 3485 } 3486 if (scrollback_delta) { 3487 struct vc_data *vc = vc_cons[fg_console].d; 3488 clear_selection(); 3489 if (vc->vc_mode == KD_TEXT && vc->vc_sw->con_scrolldelta) 3490 vc->vc_sw->con_scrolldelta(vc, scrollback_delta); 3491 scrollback_delta = 0; 3492 } 3493 if (blank_timer_expired) { 3494 do_blank_screen(0); 3495 blank_timer_expired = 0; 3496 } 3497 notify_update(vc_cons[fg_console].d); 3498 } 3499 3500 int set_console(int nr) 3501 { 3502 struct vc_data *vc = vc_cons[fg_console].d; 3503 3504 if (!vc_cons_allocated(nr) || vt_dont_switch || 3505 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) { 3506 3507 /* 3508 * Console switch will fail in console_callback() or 3509 * change_console() so there is no point scheduling 3510 * the callback 3511 * 3512 * Existing set_console() users don't check the return 3513 * value so this shouldn't break anything 3514 */ 3515 return -EINVAL; 3516 } 3517 3518 want_console = nr; 3519 schedule_console_callback(); 3520 3521 return 0; 3522 } 3523 3524 struct tty_driver *console_driver; 3525 3526 #ifdef CONFIG_VT_CONSOLE 3527 3528 /** 3529 * vt_kmsg_redirect() - sets/gets the kernel message console 3530 * @new: the new virtual terminal number or -1 if the console should stay 3531 * unchanged 3532 * 3533 * By default, the kernel messages are always printed on the current virtual 3534 * console. However, the user may modify that default with the 3535 * %TIOCL_SETKMSGREDIRECT ioctl call. 3536 * 3537 * This function sets the kernel message console to be @new. It returns the old 3538 * virtual console number. The virtual terminal number %0 (both as parameter and 3539 * return value) means no redirection (i.e. always printed on the currently 3540 * active console). 3541 * 3542 * The parameter -1 means that only the current console is returned, but the 3543 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that 3544 * case to make the code more understandable. 3545 * 3546 * When the kernel is compiled without %CONFIG_VT_CONSOLE, this function ignores 3547 * the parameter and always returns %0. 3548 */ 3549 int vt_kmsg_redirect(int new) 3550 { 3551 static int kmsg_con; 3552 3553 if (new != -1) 3554 return xchg(&kmsg_con, new); 3555 else 3556 return kmsg_con; 3557 } 3558 3559 /* 3560 * Console on virtual terminal 3561 * 3562 * The console must be locked when we get here. 3563 */ 3564 3565 static void vt_console_print(struct console *co, const char *b, unsigned count) 3566 { 3567 struct vc_data *vc = vc_cons[fg_console].d; 3568 unsigned char c; 3569 static DEFINE_SPINLOCK(printing_lock); 3570 const ushort *start; 3571 ushort start_x, cnt; 3572 int kmsg_console; 3573 3574 WARN_CONSOLE_UNLOCKED(); 3575 3576 /* this protects against concurrent oops only */ 3577 if (!spin_trylock(&printing_lock)) 3578 return; 3579 3580 kmsg_console = vt_get_kmsg_redirect(); 3581 if (kmsg_console && vc_cons_allocated(kmsg_console - 1)) 3582 vc = vc_cons[kmsg_console - 1].d; 3583 3584 if (!vc_cons_allocated(fg_console)) { 3585 /* impossible */ 3586 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */ 3587 goto quit; 3588 } 3589 3590 if (vc->vc_mode != KD_TEXT) 3591 goto quit; 3592 3593 /* undraw cursor first */ 3594 if (con_is_fg(vc)) 3595 hide_cursor(vc); 3596 3597 start = (ushort *)vc->vc_pos; 3598 start_x = vc->state.x; 3599 cnt = 0; 3600 while (count--) { 3601 c = *b++; 3602 if (c == ASCII_LINEFEED || c == ASCII_CAR_RET || 3603 c == ASCII_BACKSPACE || vc->vc_need_wrap) { 3604 if (cnt && con_is_visible(vc)) 3605 vc->vc_sw->con_putcs(vc, start, cnt, vc->state.y, start_x); 3606 cnt = 0; 3607 if (c == ASCII_BACKSPACE) { 3608 bs(vc); 3609 start = (ushort *)vc->vc_pos; 3610 start_x = vc->state.x; 3611 continue; 3612 } 3613 if (c != ASCII_CAR_RET) 3614 lf(vc); 3615 cr(vc); 3616 start = (ushort *)vc->vc_pos; 3617 start_x = vc->state.x; 3618 if (c == ASCII_LINEFEED || c == ASCII_CAR_RET) 3619 continue; 3620 } 3621 vc_uniscr_putc(vc, c); 3622 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos); 3623 notify_write(vc, c); 3624 cnt++; 3625 if (vc->state.x == vc->vc_cols - 1) { 3626 vc->vc_need_wrap = 1; 3627 } else { 3628 vc->vc_pos += 2; 3629 vc->state.x++; 3630 } 3631 } 3632 if (cnt && con_is_visible(vc)) 3633 vc->vc_sw->con_putcs(vc, start, cnt, vc->state.y, start_x); 3634 set_cursor(vc); 3635 notify_update(vc); 3636 3637 quit: 3638 spin_unlock(&printing_lock); 3639 } 3640 3641 static struct tty_driver *vt_console_device(struct console *c, int *index) 3642 { 3643 *index = c->index ? c->index-1 : fg_console; 3644 return console_driver; 3645 } 3646 3647 static int vt_console_setup(struct console *co, char *options) 3648 { 3649 return co->index >= MAX_NR_CONSOLES ? -EINVAL : 0; 3650 } 3651 3652 static struct console vt_console_driver = { 3653 .name = "tty", 3654 .setup = vt_console_setup, 3655 .write = vt_console_print, 3656 .device = vt_console_device, 3657 .unblank = unblank_screen, 3658 .flags = CON_PRINTBUFFER, 3659 .index = -1, 3660 }; 3661 #endif 3662 3663 /* 3664 * Handling of Linux-specific VC ioctls 3665 */ 3666 3667 /* 3668 * Generally a bit racy with respect to console_lock();. 3669 * 3670 * There are some functions which don't need it. 3671 * 3672 * There are some functions which can sleep for arbitrary periods 3673 * (paste_selection) but we don't need the lock there anyway. 3674 * 3675 * set_selection_user has locking, and definitely needs it 3676 */ 3677 3678 int tioclinux(struct tty_struct *tty, unsigned long arg) 3679 { 3680 char type, data; 3681 char __user *p = (char __user *)arg; 3682 void __user *param_aligned32 = (u32 __user *)arg + 1; 3683 void __user *param = (void __user *)arg + 1; 3684 int lines; 3685 int ret; 3686 3687 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN)) 3688 return -EPERM; 3689 if (get_user(type, p)) 3690 return -EFAULT; 3691 ret = 0; 3692 3693 switch (type) { 3694 case TIOCL_SETSEL: 3695 return set_selection_user(param, tty); 3696 case TIOCL_PASTESEL: 3697 if (!capable(CAP_SYS_ADMIN)) 3698 return -EPERM; 3699 return paste_selection(tty); 3700 case TIOCL_UNBLANKSCREEN: 3701 scoped_guard(console_lock) 3702 unblank_screen(); 3703 break; 3704 case TIOCL_SELLOADLUT: 3705 if (!capable(CAP_SYS_ADMIN)) 3706 return -EPERM; 3707 return sel_loadlut(param_aligned32); 3708 case TIOCL_GETSHIFTSTATE: 3709 /* 3710 * Make it possible to react to Shift+Mousebutton. Note that 3711 * 'shift_state' is an undocumented kernel-internal variable; 3712 * programs not closely related to the kernel should not use 3713 * this. 3714 */ 3715 data = vt_get_shift_state(); 3716 return put_user(data, p); 3717 case TIOCL_GETMOUSEREPORTING: 3718 scoped_guard(console_lock) /* May be overkill */ 3719 data = mouse_reporting(); 3720 return put_user(data, p); 3721 case TIOCL_SETVESABLANK: 3722 return set_vesa_blanking(param); 3723 case TIOCL_GETKMSGREDIRECT: 3724 data = vt_get_kmsg_redirect(); 3725 return put_user(data, p); 3726 case TIOCL_SETKMSGREDIRECT: 3727 if (!capable(CAP_SYS_ADMIN)) 3728 return -EPERM; 3729 3730 if (get_user(data, p+1)) 3731 return -EFAULT; 3732 3733 vt_kmsg_redirect(data); 3734 3735 break; 3736 case TIOCL_GETFGCONSOLE: 3737 /* 3738 * No locking needed as this is a transiently correct return 3739 * anyway if the caller hasn't disabled switching. 3740 */ 3741 return fg_console; 3742 case TIOCL_SCROLLCONSOLE: 3743 if (get_user(lines, (s32 __user *)param_aligned32)) 3744 return -EFAULT; 3745 3746 /* 3747 * Needs the console lock here. Note that lots of other calls 3748 * need fixing before the lock is actually useful! 3749 */ 3750 scoped_guard(console_lock) 3751 scrollfront(vc_cons[fg_console].d, lines); 3752 break; 3753 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */ 3754 scoped_guard(console_lock) { 3755 ignore_poke = 1; 3756 do_blank_screen(0); 3757 } 3758 break; 3759 case TIOCL_BLANKEDSCREEN: 3760 return console_blanked; 3761 case TIOCL_GETBRACKETEDPASTE: 3762 return get_bracketed_paste(tty); 3763 default: 3764 return -EINVAL; 3765 } 3766 3767 return ret; 3768 } 3769 3770 /* 3771 * /dev/ttyN handling 3772 */ 3773 3774 static ssize_t con_write(struct tty_struct *tty, const u8 *buf, size_t count) 3775 { 3776 int retval; 3777 3778 retval = do_con_write(tty, buf, count); 3779 con_flush_chars(tty); 3780 3781 return retval; 3782 } 3783 3784 static int con_put_char(struct tty_struct *tty, u8 ch) 3785 { 3786 return do_con_write(tty, &ch, 1); 3787 } 3788 3789 static unsigned int con_write_room(struct tty_struct *tty) 3790 { 3791 if (tty->flow.stopped) 3792 return 0; 3793 return 32768; /* No limit, really; we're not buffering */ 3794 } 3795 3796 /* 3797 * con_throttle and con_unthrottle are only used for 3798 * paste_selection(), which has to stuff in a large number of 3799 * characters... 3800 */ 3801 static void con_throttle(struct tty_struct *tty) 3802 { 3803 } 3804 3805 static void con_unthrottle(struct tty_struct *tty) 3806 { 3807 struct vc_data *vc = tty->driver_data; 3808 3809 wake_up_interruptible(&vc->paste_wait); 3810 } 3811 3812 /* 3813 * Turn the Scroll-Lock LED on when the tty is stopped 3814 */ 3815 static void con_stop(struct tty_struct *tty) 3816 { 3817 int console_num; 3818 if (!tty) 3819 return; 3820 console_num = tty->index; 3821 if (!vc_cons_allocated(console_num)) 3822 return; 3823 vt_kbd_con_stop(console_num); 3824 } 3825 3826 /* 3827 * Turn the Scroll-Lock LED off when the console is started 3828 */ 3829 static void con_start(struct tty_struct *tty) 3830 { 3831 int console_num; 3832 if (!tty) 3833 return; 3834 console_num = tty->index; 3835 if (!vc_cons_allocated(console_num)) 3836 return; 3837 vt_kbd_con_start(console_num); 3838 } 3839 3840 static void con_flush_chars(struct tty_struct *tty) 3841 { 3842 struct vc_data *vc = tty->driver_data; 3843 3844 if (in_interrupt()) /* from flush_to_ldisc */ 3845 return; 3846 3847 guard(console_lock)(); 3848 set_cursor(vc); 3849 } 3850 3851 /* 3852 * Allocate the console screen memory. 3853 */ 3854 static int con_install(struct tty_driver *driver, struct tty_struct *tty) 3855 { 3856 unsigned int currcons = tty->index; 3857 struct vc_data *vc; 3858 int ret; 3859 3860 guard(console_lock)(); 3861 ret = vc_allocate(currcons); 3862 if (ret) 3863 return ret; 3864 3865 vc = vc_cons[currcons].d; 3866 3867 /* Still being freed */ 3868 if (vc->port.tty) 3869 return -ERESTARTSYS; 3870 3871 ret = tty_port_install(&vc->port, driver, tty); 3872 if (ret) 3873 return ret; 3874 3875 tty->driver_data = vc; 3876 vc->port.tty = tty; 3877 tty_port_get(&vc->port); 3878 3879 if (!tty->winsize.ws_row && !tty->winsize.ws_col) { 3880 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows; 3881 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols; 3882 } 3883 if (vc->vc_utf) 3884 tty->termios.c_iflag |= IUTF8; 3885 else 3886 tty->termios.c_iflag &= ~IUTF8; 3887 3888 return 0; 3889 } 3890 3891 static int con_open(struct tty_struct *tty, struct file *filp) 3892 { 3893 /* everything done in install */ 3894 return 0; 3895 } 3896 3897 3898 static void con_close(struct tty_struct *tty, struct file *filp) 3899 { 3900 /* Nothing to do - we defer to shutdown */ 3901 } 3902 3903 static void con_shutdown(struct tty_struct *tty) 3904 { 3905 struct vc_data *vc = tty->driver_data; 3906 BUG_ON(vc == NULL); 3907 3908 guard(console_lock)(); 3909 vc->port.tty = NULL; 3910 } 3911 3912 static void con_cleanup(struct tty_struct *tty) 3913 { 3914 struct vc_data *vc = tty->driver_data; 3915 3916 tty_port_put(&vc->port); 3917 } 3918 3919 /* 3920 * We can't deal with anything but the N_TTY ldisc, 3921 * because we can sleep in our write() routine. 3922 */ 3923 static int con_ldisc_ok(struct tty_struct *tty, int ldisc) 3924 { 3925 return ldisc == N_TTY ? 0 : -EINVAL; 3926 } 3927 3928 static int default_color = 7; /* white */ 3929 static int default_italic_color = 2; // green (ASCII) 3930 static int default_underline_color = 3; // cyan (ASCII) 3931 module_param_named(color, default_color, int, S_IRUGO | S_IWUSR); 3932 module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR); 3933 module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR); 3934 3935 static void vc_init(struct vc_data *vc, int do_clear) 3936 { 3937 int j, k ; 3938 3939 set_origin(vc); 3940 vc->vc_pos = vc->vc_origin; 3941 reset_vc(vc); 3942 for (j=k=0; j<16; j++) { 3943 vc->vc_palette[k++] = default_red[j] ; 3944 vc->vc_palette[k++] = default_grn[j] ; 3945 vc->vc_palette[k++] = default_blu[j] ; 3946 } 3947 vc->vc_def_color = default_color; 3948 vc->vc_ulcolor = default_underline_color; 3949 vc->vc_itcolor = default_italic_color; 3950 vc->vc_halfcolor = 0x08; /* grey */ 3951 init_waitqueue_head(&vc->paste_wait); 3952 reset_terminal(vc, do_clear); 3953 } 3954 3955 /* 3956 * This routine initializes console interrupts, and does nothing 3957 * else. If you want the screen to clear, call tty_write with 3958 * the appropriate escape-sequence. 3959 */ 3960 3961 static int __init con_init(void) 3962 { 3963 const char *display_desc = NULL; 3964 struct vc_data *vc; 3965 unsigned int currcons = 0, i; 3966 3967 console_lock(); 3968 3969 if (!conswitchp) 3970 conswitchp = &dummy_con; 3971 display_desc = conswitchp->con_startup(); 3972 if (!display_desc) { 3973 fg_console = 0; 3974 console_unlock(); 3975 return 0; 3976 } 3977 3978 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 3979 struct con_driver *con_driver = ®istered_con_driver[i]; 3980 3981 if (con_driver->con == NULL) { 3982 con_driver->con = conswitchp; 3983 con_driver->desc = display_desc; 3984 con_driver->flag = CON_DRIVER_FLAG_INIT; 3985 con_driver->first = 0; 3986 con_driver->last = MAX_NR_CONSOLES - 1; 3987 break; 3988 } 3989 } 3990 3991 for (i = 0; i < MAX_NR_CONSOLES; i++) 3992 con_driver_map[i] = conswitchp; 3993 3994 if (blankinterval) { 3995 blank_state = blank_normal_wait; 3996 mod_timer(&console_timer, jiffies + (blankinterval * HZ)); 3997 } 3998 3999 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) { 4000 vc_cons[currcons].d = vc = kzalloc_obj(struct vc_data, 4001 GFP_NOWAIT); 4002 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK); 4003 tty_port_init(&vc->port); 4004 visual_init(vc, currcons, true); 4005 /* Assuming vc->vc_{cols,rows,screenbuf_size} are sane here. */ 4006 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT); 4007 vc_init(vc, currcons || !vc->vc_sw->con_save_screen); 4008 } 4009 currcons = fg_console = 0; 4010 master_display_fg = vc = vc_cons[currcons].d; 4011 set_origin(vc); 4012 save_screen(vc); 4013 gotoxy(vc, vc->state.x, vc->state.y); 4014 csi_J(vc, CSI_J_CURSOR_TO_END); 4015 update_screen(vc); 4016 pr_info("Console: %s %s %dx%d\n", 4017 vc->vc_can_do_color ? "colour" : "mono", 4018 display_desc, vc->vc_cols, vc->vc_rows); 4019 4020 console_unlock(); 4021 4022 #ifdef CONFIG_VT_CONSOLE 4023 register_console(&vt_console_driver); 4024 #endif 4025 return 0; 4026 } 4027 console_initcall(con_init); 4028 4029 static const struct tty_operations con_ops = { 4030 .install = con_install, 4031 .open = con_open, 4032 .close = con_close, 4033 .write = con_write, 4034 .write_room = con_write_room, 4035 .put_char = con_put_char, 4036 .flush_chars = con_flush_chars, 4037 .ioctl = vt_ioctl, 4038 #ifdef CONFIG_COMPAT 4039 .compat_ioctl = vt_compat_ioctl, 4040 #endif 4041 .stop = con_stop, 4042 .start = con_start, 4043 .throttle = con_throttle, 4044 .unthrottle = con_unthrottle, 4045 .resize = vt_resize, 4046 .shutdown = con_shutdown, 4047 .cleanup = con_cleanup, 4048 .ldisc_ok = con_ldisc_ok, 4049 }; 4050 4051 static struct cdev vc0_cdev; 4052 4053 static ssize_t show_tty_active(struct device *dev, 4054 struct device_attribute *attr, char *buf) 4055 { 4056 return sprintf(buf, "tty%d\n", fg_console + 1); 4057 } 4058 static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL); 4059 4060 static struct attribute *vt_dev_attrs[] = { 4061 &dev_attr_active.attr, 4062 NULL 4063 }; 4064 4065 ATTRIBUTE_GROUPS(vt_dev); 4066 4067 int __init vty_init(const struct file_operations *console_fops) 4068 { 4069 cdev_init(&vc0_cdev, console_fops); 4070 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) || 4071 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0) 4072 panic("Couldn't register /dev/tty0 driver\n"); 4073 tty0dev = device_create_with_groups(&tty_class, NULL, 4074 MKDEV(TTY_MAJOR, 0), NULL, 4075 vt_dev_groups, "tty0"); 4076 if (IS_ERR(tty0dev)) 4077 tty0dev = NULL; 4078 4079 vcs_init(); 4080 4081 console_driver = tty_alloc_driver(MAX_NR_CONSOLES, TTY_DRIVER_REAL_RAW | 4082 TTY_DRIVER_RESET_TERMIOS); 4083 if (IS_ERR(console_driver)) 4084 panic("Couldn't allocate console driver\n"); 4085 4086 console_driver->name = "tty"; 4087 console_driver->name_base = 1; 4088 console_driver->major = TTY_MAJOR; 4089 console_driver->minor_start = 1; 4090 console_driver->type = TTY_DRIVER_TYPE_CONSOLE; 4091 console_driver->init_termios = tty_std_termios; 4092 if (default_utf8) 4093 console_driver->init_termios.c_iflag |= IUTF8; 4094 tty_set_operations(console_driver, &con_ops); 4095 if (tty_register_driver(console_driver)) 4096 panic("Couldn't register console driver\n"); 4097 kbd_init(); 4098 console_map_init(); 4099 return 0; 4100 } 4101 4102 static const struct class vtconsole_class = { 4103 .name = "vtconsole", 4104 }; 4105 4106 static int do_bind_con_driver(const struct consw *csw, int first, int last, 4107 int deflt) 4108 { 4109 struct module *owner = csw->owner; 4110 const char *desc = NULL; 4111 struct con_driver *con_driver; 4112 int i, j = -1, k = -1, retval = -ENODEV; 4113 4114 if (!try_module_get(owner)) 4115 return -ENODEV; 4116 4117 WARN_CONSOLE_UNLOCKED(); 4118 4119 /* check if driver is registered */ 4120 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4121 con_driver = ®istered_con_driver[i]; 4122 4123 if (con_driver->con == csw) { 4124 desc = con_driver->desc; 4125 retval = 0; 4126 break; 4127 } 4128 } 4129 4130 if (retval) 4131 goto err; 4132 4133 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) { 4134 csw->con_startup(); 4135 con_driver->flag |= CON_DRIVER_FLAG_INIT; 4136 } 4137 4138 if (deflt) { 4139 if (conswitchp) 4140 module_put(conswitchp->owner); 4141 4142 __module_get(owner); 4143 conswitchp = csw; 4144 } 4145 4146 first = max(first, con_driver->first); 4147 last = min(last, con_driver->last); 4148 4149 for (i = first; i <= last; i++) { 4150 int old_was_color; 4151 struct vc_data *vc = vc_cons[i].d; 4152 4153 if (con_driver_map[i]) 4154 module_put(con_driver_map[i]->owner); 4155 __module_get(owner); 4156 con_driver_map[i] = csw; 4157 4158 if (!vc || !vc->vc_sw) 4159 continue; 4160 4161 j = i; 4162 4163 if (con_is_visible(vc)) { 4164 k = i; 4165 save_screen(vc); 4166 } 4167 4168 old_was_color = vc->vc_can_do_color; 4169 vc->vc_sw->con_deinit(vc); 4170 vc->vc_origin = (unsigned long)vc->vc_screenbuf; 4171 visual_init(vc, i, false); 4172 set_origin(vc); 4173 update_attr(vc); 4174 4175 /* If the console changed between mono <-> color, then 4176 * the attributes in the screenbuf will be wrong. The 4177 * following resets all attributes to something sane. 4178 */ 4179 if (old_was_color != vc->vc_can_do_color) 4180 clear_buffer_attributes(vc); 4181 } 4182 4183 pr_info("Console: switching "); 4184 if (!deflt) 4185 pr_cont("consoles %d-%d ", first + 1, last + 1); 4186 if (j >= 0) { 4187 struct vc_data *vc = vc_cons[j].d; 4188 4189 pr_cont("to %s %s %dx%d\n", 4190 vc->vc_can_do_color ? "colour" : "mono", 4191 desc, vc->vc_cols, vc->vc_rows); 4192 4193 if (k >= 0) { 4194 vc = vc_cons[k].d; 4195 update_screen(vc); 4196 } 4197 } else { 4198 pr_cont("to %s\n", desc); 4199 } 4200 4201 retval = 0; 4202 err: 4203 module_put(owner); 4204 return retval; 4205 }; 4206 4207 4208 #ifdef CONFIG_VT_HW_CONSOLE_BINDING 4209 int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt) 4210 { 4211 struct module *owner = csw->owner; 4212 const struct consw *defcsw = NULL; 4213 struct con_driver *con_driver = NULL, *con_back = NULL; 4214 int i, retval = -ENODEV; 4215 4216 if (!try_module_get(owner)) 4217 return -ENODEV; 4218 4219 WARN_CONSOLE_UNLOCKED(); 4220 4221 /* check if driver is registered and if it is unbindable */ 4222 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4223 con_driver = ®istered_con_driver[i]; 4224 4225 if (con_driver->con == csw && 4226 con_driver->flag & CON_DRIVER_FLAG_MODULE) { 4227 retval = 0; 4228 break; 4229 } 4230 } 4231 4232 if (retval) 4233 goto err; 4234 4235 retval = -ENODEV; 4236 4237 /* check if backup driver exists */ 4238 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4239 con_back = ®istered_con_driver[i]; 4240 4241 if (con_back->con && con_back->con != csw) { 4242 defcsw = con_back->con; 4243 retval = 0; 4244 break; 4245 } 4246 } 4247 4248 if (retval) 4249 goto err; 4250 4251 if (!con_is_bound(csw)) 4252 goto err; 4253 4254 first = max(first, con_driver->first); 4255 last = min(last, con_driver->last); 4256 4257 for (i = first; i <= last; i++) { 4258 if (con_driver_map[i] == csw) { 4259 module_put(csw->owner); 4260 con_driver_map[i] = NULL; 4261 } 4262 } 4263 4264 if (!con_is_bound(defcsw)) { 4265 const struct consw *defconsw = conswitchp; 4266 4267 defcsw->con_startup(); 4268 con_back->flag |= CON_DRIVER_FLAG_INIT; 4269 /* 4270 * vgacon may change the default driver to point 4271 * to dummycon, we restore it here... 4272 */ 4273 conswitchp = defconsw; 4274 } 4275 4276 if (!con_is_bound(csw)) 4277 con_driver->flag &= ~CON_DRIVER_FLAG_INIT; 4278 4279 /* ignore return value, binding should not fail */ 4280 do_bind_con_driver(defcsw, first, last, deflt); 4281 err: 4282 module_put(owner); 4283 return retval; 4284 4285 } 4286 EXPORT_SYMBOL_GPL(do_unbind_con_driver); 4287 4288 static int vt_bind(struct con_driver *con) 4289 { 4290 const struct consw *defcsw = NULL, *csw = NULL; 4291 int i, more = 1, first = -1, last = -1, deflt = 0; 4292 4293 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE)) 4294 goto err; 4295 4296 csw = con->con; 4297 4298 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4299 struct con_driver *con = ®istered_con_driver[i]; 4300 4301 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) { 4302 defcsw = con->con; 4303 break; 4304 } 4305 } 4306 4307 if (!defcsw) 4308 goto err; 4309 4310 while (more) { 4311 more = 0; 4312 4313 for (i = con->first; i <= con->last; i++) { 4314 if (con_driver_map[i] == defcsw) { 4315 if (first == -1) 4316 first = i; 4317 last = i; 4318 more = 1; 4319 } else if (first != -1) 4320 break; 4321 } 4322 4323 if (first == 0 && last == MAX_NR_CONSOLES -1) 4324 deflt = 1; 4325 4326 if (first != -1) 4327 do_bind_con_driver(csw, first, last, deflt); 4328 4329 first = -1; 4330 last = -1; 4331 deflt = 0; 4332 } 4333 4334 err: 4335 return 0; 4336 } 4337 4338 static int vt_unbind(struct con_driver *con) 4339 { 4340 const struct consw *csw = NULL; 4341 int i, more = 1, first = -1, last = -1, deflt = 0; 4342 int ret; 4343 4344 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE)) 4345 goto err; 4346 4347 csw = con->con; 4348 4349 while (more) { 4350 more = 0; 4351 4352 for (i = con->first; i <= con->last; i++) { 4353 if (con_driver_map[i] == csw) { 4354 if (first == -1) 4355 first = i; 4356 last = i; 4357 more = 1; 4358 } else if (first != -1) 4359 break; 4360 } 4361 4362 if (first == 0 && last == MAX_NR_CONSOLES -1) 4363 deflt = 1; 4364 4365 if (first != -1) { 4366 ret = do_unbind_con_driver(csw, first, last, deflt); 4367 if (ret != 0) 4368 return ret; 4369 } 4370 4371 first = -1; 4372 last = -1; 4373 deflt = 0; 4374 } 4375 4376 err: 4377 return 0; 4378 } 4379 #else 4380 static inline int vt_bind(struct con_driver *con) 4381 { 4382 return 0; 4383 } 4384 static inline int vt_unbind(struct con_driver *con) 4385 { 4386 return 0; 4387 } 4388 #endif /* CONFIG_VT_HW_CONSOLE_BINDING */ 4389 4390 static ssize_t store_bind(struct device *dev, struct device_attribute *attr, 4391 const char *buf, size_t count) 4392 { 4393 struct con_driver *con = dev_get_drvdata(dev); 4394 int bind = simple_strtoul(buf, NULL, 0); 4395 4396 guard(console_lock)(); 4397 4398 if (bind) 4399 vt_bind(con); 4400 else 4401 vt_unbind(con); 4402 4403 return count; 4404 } 4405 4406 static ssize_t show_bind(struct device *dev, struct device_attribute *attr, 4407 char *buf) 4408 { 4409 struct con_driver *con = dev_get_drvdata(dev); 4410 int bind; 4411 4412 scoped_guard(console_lock) 4413 bind = con_is_bound(con->con); 4414 4415 return sysfs_emit(buf, "%i\n", bind); 4416 } 4417 4418 static ssize_t show_name(struct device *dev, struct device_attribute *attr, 4419 char *buf) 4420 { 4421 struct con_driver *con = dev_get_drvdata(dev); 4422 4423 return sysfs_emit(buf, "%s %s\n", 4424 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)", 4425 con->desc); 4426 4427 } 4428 4429 static DEVICE_ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind); 4430 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); 4431 4432 static struct attribute *con_dev_attrs[] = { 4433 &dev_attr_bind.attr, 4434 &dev_attr_name.attr, 4435 NULL 4436 }; 4437 4438 ATTRIBUTE_GROUPS(con_dev); 4439 4440 static int vtconsole_init_device(struct con_driver *con) 4441 { 4442 con->flag |= CON_DRIVER_FLAG_ATTR; 4443 return 0; 4444 } 4445 4446 static void vtconsole_deinit_device(struct con_driver *con) 4447 { 4448 con->flag &= ~CON_DRIVER_FLAG_ATTR; 4449 } 4450 4451 /** 4452 * con_is_bound - checks if driver is bound to the console 4453 * @csw: console driver 4454 * 4455 * RETURNS: zero if unbound, nonzero if bound 4456 * 4457 * Drivers can call this and if zero, they should release 4458 * all resources allocated on &consw.con_startup() 4459 */ 4460 int con_is_bound(const struct consw *csw) 4461 { 4462 int i, bound = 0; 4463 4464 WARN_CONSOLE_UNLOCKED(); 4465 4466 for (i = 0; i < MAX_NR_CONSOLES; i++) { 4467 if (con_driver_map[i] == csw) { 4468 bound = 1; 4469 break; 4470 } 4471 } 4472 4473 return bound; 4474 } 4475 EXPORT_SYMBOL(con_is_bound); 4476 4477 /** 4478 * con_is_visible - checks whether the current console is visible 4479 * @vc: virtual console 4480 * 4481 * RETURNS: zero if not visible, nonzero if visible 4482 */ 4483 bool con_is_visible(const struct vc_data *vc) 4484 { 4485 WARN_CONSOLE_UNLOCKED(); 4486 4487 return *vc->vc_display_fg == vc; 4488 } 4489 EXPORT_SYMBOL(con_is_visible); 4490 4491 /** 4492 * con_debug_enter - prepare the console for the kernel debugger 4493 * @vc: virtual console 4494 * 4495 * Called when the console is taken over by the kernel debugger, this 4496 * function needs to save the current console state, then put the console 4497 * into a state suitable for the kernel debugger. 4498 */ 4499 void con_debug_enter(struct vc_data *vc) 4500 { 4501 #ifdef CONFIG_KGDB_KDB 4502 /* Set the initial LINES variable if it is not already set */ 4503 if (vc->vc_rows < 999) { 4504 int linecount; 4505 char lns[4]; 4506 const char *setargs[3] = { 4507 "set", 4508 "LINES", 4509 lns, 4510 }; 4511 if (kdbgetintenv(setargs[0], &linecount)) { 4512 snprintf(lns, 4, "%i", vc->vc_rows); 4513 kdb_set(2, setargs); 4514 } 4515 } 4516 if (vc->vc_cols < 999) { 4517 int colcount; 4518 char cols[4]; 4519 const char *setargs[3] = { 4520 "set", 4521 "COLUMNS", 4522 cols, 4523 }; 4524 if (kdbgetintenv(setargs[0], &colcount)) { 4525 snprintf(cols, 4, "%i", vc->vc_cols); 4526 kdb_set(2, setargs); 4527 } 4528 } 4529 #endif /* CONFIG_KGDB_KDB */ 4530 } 4531 EXPORT_SYMBOL_GPL(con_debug_enter); 4532 4533 /** 4534 * con_debug_leave - restore console state 4535 * 4536 * Restore the console state to what it was before the kernel debugger 4537 * was invoked. 4538 */ 4539 void con_debug_leave(void) 4540 { } 4541 EXPORT_SYMBOL_GPL(con_debug_leave); 4542 4543 static int do_register_con_driver(const struct consw *csw, int first, int last) 4544 { 4545 struct module *owner = csw->owner; 4546 struct con_driver *con_driver; 4547 const char *desc; 4548 int i, retval; 4549 4550 WARN_CONSOLE_UNLOCKED(); 4551 4552 if (!try_module_get(owner)) 4553 return -ENODEV; 4554 4555 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4556 con_driver = ®istered_con_driver[i]; 4557 4558 /* already registered */ 4559 if (con_driver->con == csw) { 4560 retval = -EBUSY; 4561 goto err; 4562 } 4563 } 4564 4565 desc = csw->con_startup(); 4566 if (!desc) { 4567 retval = -ENODEV; 4568 goto err; 4569 } 4570 4571 retval = -EINVAL; 4572 4573 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4574 con_driver = ®istered_con_driver[i]; 4575 4576 if (con_driver->con == NULL && 4577 !(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) { 4578 con_driver->con = csw; 4579 con_driver->desc = desc; 4580 con_driver->node = i; 4581 con_driver->flag = CON_DRIVER_FLAG_MODULE | 4582 CON_DRIVER_FLAG_INIT; 4583 con_driver->first = first; 4584 con_driver->last = last; 4585 retval = 0; 4586 break; 4587 } 4588 } 4589 4590 if (retval) 4591 goto err; 4592 4593 con_driver->dev = 4594 device_create_with_groups(&vtconsole_class, NULL, 4595 MKDEV(0, con_driver->node), 4596 con_driver, con_dev_groups, 4597 "vtcon%i", con_driver->node); 4598 if (IS_ERR(con_driver->dev)) { 4599 pr_warn("Unable to create device for %s; errno = %ld\n", 4600 con_driver->desc, PTR_ERR(con_driver->dev)); 4601 con_driver->dev = NULL; 4602 } else { 4603 vtconsole_init_device(con_driver); 4604 } 4605 4606 err: 4607 module_put(owner); 4608 return retval; 4609 } 4610 4611 4612 /** 4613 * do_unregister_con_driver - unregister console driver from console layer 4614 * @csw: console driver 4615 * 4616 * DESCRIPTION: All drivers that registers to the console layer must 4617 * call this function upon exit, or if the console driver is in a state 4618 * where it won't be able to handle console services, such as the 4619 * framebuffer console without loaded framebuffer drivers. 4620 * 4621 * The driver must unbind first prior to unregistration. 4622 */ 4623 int do_unregister_con_driver(const struct consw *csw) 4624 { 4625 int i; 4626 4627 /* cannot unregister a bound driver */ 4628 if (con_is_bound(csw)) 4629 return -EBUSY; 4630 4631 if (csw == conswitchp) 4632 return -EINVAL; 4633 4634 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4635 struct con_driver *con_driver = ®istered_con_driver[i]; 4636 4637 if (con_driver->con == csw) { 4638 /* 4639 * Defer the removal of the sysfs entries since that 4640 * will acquire the kernfs s_active lock and we can't 4641 * acquire this lock while holding the console lock: 4642 * the unbind sysfs entry imposes already the opposite 4643 * order. Reset con already here to prevent any later 4644 * lookup to succeed and mark this slot as zombie, so 4645 * it won't get reused until we complete the removal 4646 * in the deferred work. 4647 */ 4648 con_driver->con = NULL; 4649 con_driver->flag = CON_DRIVER_FLAG_ZOMBIE; 4650 schedule_work(&con_driver_unregister_work); 4651 4652 return 0; 4653 } 4654 } 4655 4656 return -ENODEV; 4657 } 4658 EXPORT_SYMBOL_GPL(do_unregister_con_driver); 4659 4660 static void con_driver_unregister_callback(struct work_struct *ignored) 4661 { 4662 int i; 4663 4664 guard(console_lock)(); 4665 4666 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4667 struct con_driver *con_driver = ®istered_con_driver[i]; 4668 4669 if (!(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) 4670 continue; 4671 4672 console_unlock(); 4673 4674 vtconsole_deinit_device(con_driver); 4675 device_destroy(&vtconsole_class, MKDEV(0, con_driver->node)); 4676 4677 console_lock(); 4678 4679 if (WARN_ON_ONCE(con_driver->con)) 4680 con_driver->con = NULL; 4681 con_driver->desc = NULL; 4682 con_driver->dev = NULL; 4683 con_driver->node = 0; 4684 WARN_ON_ONCE(con_driver->flag != CON_DRIVER_FLAG_ZOMBIE); 4685 con_driver->flag = 0; 4686 con_driver->first = 0; 4687 con_driver->last = 0; 4688 } 4689 } 4690 4691 /* 4692 * If we support more console drivers, this function is used 4693 * when a driver wants to take over some existing consoles 4694 * and become default driver for newly opened ones. 4695 * 4696 * do_take_over_console is basically a register followed by bind 4697 */ 4698 int do_take_over_console(const struct consw *csw, int first, int last, int deflt) 4699 { 4700 int err; 4701 4702 err = do_register_con_driver(csw, first, last); 4703 /* 4704 * If we get an busy error we still want to bind the console driver 4705 * and return success, as we may have unbound the console driver 4706 * but not unregistered it. 4707 */ 4708 if (err == -EBUSY) 4709 err = 0; 4710 if (!err) 4711 do_bind_con_driver(csw, first, last, deflt); 4712 4713 return err; 4714 } 4715 EXPORT_SYMBOL_GPL(do_take_over_console); 4716 4717 4718 /* 4719 * give_up_console is a wrapper to unregister_con_driver. It will only 4720 * work if driver is fully unbound. 4721 */ 4722 void give_up_console(const struct consw *csw) 4723 { 4724 guard(console_lock)(); 4725 do_unregister_con_driver(csw); 4726 } 4727 EXPORT_SYMBOL(give_up_console); 4728 4729 static int __init vtconsole_class_init(void) 4730 { 4731 int i; 4732 4733 i = class_register(&vtconsole_class); 4734 if (i) 4735 pr_warn("Unable to create vt console class; errno = %d\n", i); 4736 4737 /* Add system drivers to sysfs */ 4738 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4739 struct con_driver *con = ®istered_con_driver[i]; 4740 4741 if (con->con && !con->dev) { 4742 con->dev = 4743 device_create_with_groups(&vtconsole_class, NULL, 4744 MKDEV(0, con->node), 4745 con, con_dev_groups, 4746 "vtcon%i", con->node); 4747 4748 if (IS_ERR(con->dev)) { 4749 pr_warn("Unable to create device for %s; errno = %ld\n", 4750 con->desc, PTR_ERR(con->dev)); 4751 con->dev = NULL; 4752 } else { 4753 vtconsole_init_device(con); 4754 } 4755 } 4756 } 4757 4758 return 0; 4759 } 4760 postcore_initcall(vtconsole_class_init); 4761 4762 /* 4763 * Screen blanking 4764 */ 4765 4766 static int set_vesa_blanking(u8 __user *mode_user) 4767 { 4768 u8 mode; 4769 4770 if (get_user(mode, mode_user)) 4771 return -EFAULT; 4772 4773 guard(console_lock)(); 4774 vesa_blank_mode = (mode <= VESA_BLANK_MAX) ? mode : VESA_NO_BLANKING; 4775 4776 return 0; 4777 } 4778 4779 void do_blank_screen(int entering_gfx) 4780 { 4781 struct vc_data *vc = vc_cons[fg_console].d; 4782 int i; 4783 4784 might_sleep(); 4785 4786 WARN_CONSOLE_UNLOCKED(); 4787 4788 if (console_blanked) { 4789 if (blank_state == blank_vesa_wait) { 4790 blank_state = blank_off; 4791 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0); 4792 } 4793 return; 4794 } 4795 4796 /* entering graphics mode? */ 4797 if (entering_gfx) { 4798 hide_cursor(vc); 4799 save_screen(vc); 4800 vc->vc_sw->con_blank(vc, VESA_VSYNC_SUSPEND, 1); 4801 console_blanked = fg_console + 1; 4802 blank_state = blank_off; 4803 set_origin(vc); 4804 return; 4805 } 4806 4807 blank_state = blank_off; 4808 4809 /* don't blank graphics */ 4810 if (vc->vc_mode != KD_TEXT) { 4811 console_blanked = fg_console + 1; 4812 return; 4813 } 4814 4815 hide_cursor(vc); 4816 timer_delete_sync(&console_timer); 4817 blank_timer_expired = 0; 4818 4819 save_screen(vc); 4820 /* In case we need to reset origin, blanking hook returns 1 */ 4821 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? VESA_VSYNC_SUSPEND : 4822 (vesa_blank_mode + 1), 0); 4823 console_blanked = fg_console + 1; 4824 if (i) 4825 set_origin(vc); 4826 4827 if (console_blank_hook && console_blank_hook(1)) 4828 return; 4829 4830 if (vesa_off_interval && vesa_blank_mode) { 4831 blank_state = blank_vesa_wait; 4832 mod_timer(&console_timer, jiffies + vesa_off_interval); 4833 } 4834 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num); 4835 } 4836 EXPORT_SYMBOL(do_blank_screen); 4837 4838 /* 4839 * Called by timer as well as from vt_console_driver 4840 */ 4841 void do_unblank_screen(int leaving_gfx) 4842 { 4843 struct vc_data *vc; 4844 4845 /* This should now always be called from a "sane" (read: can schedule) 4846 * context for the sake of the low level drivers, except in the special 4847 * case of oops_in_progress 4848 */ 4849 if (!oops_in_progress) 4850 might_sleep(); 4851 4852 WARN_CONSOLE_UNLOCKED(); 4853 4854 ignore_poke = 0; 4855 if (!console_blanked) 4856 return; 4857 if (!vc_cons_allocated(fg_console)) { 4858 /* impossible */ 4859 pr_warn("unblank_screen: tty %d not allocated ??\n", 4860 fg_console + 1); 4861 return; 4862 } 4863 vc = vc_cons[fg_console].d; 4864 if (vc->vc_mode != KD_TEXT) 4865 return; /* but leave console_blanked != 0 */ 4866 4867 if (blankinterval) { 4868 mod_timer(&console_timer, jiffies + (blankinterval * HZ)); 4869 blank_state = blank_normal_wait; 4870 } 4871 4872 console_blanked = 0; 4873 if (vc->vc_sw->con_blank(vc, VESA_NO_BLANKING, leaving_gfx)) 4874 /* Low-level driver cannot restore -> do it ourselves */ 4875 update_screen(vc); 4876 if (console_blank_hook) 4877 console_blank_hook(0); 4878 set_palette(vc); 4879 set_cursor(vc); 4880 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num); 4881 notify_update(vc); 4882 } 4883 EXPORT_SYMBOL(do_unblank_screen); 4884 4885 /* 4886 * This is called by the outside world to cause a forced unblank, mostly for 4887 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually 4888 * call it with 1 as an argument and so force a mode restore... that may kill 4889 * X or at least garbage the screen but would also make the Oops visible... 4890 */ 4891 static void unblank_screen(void) 4892 { 4893 do_unblank_screen(0); 4894 } 4895 4896 /* 4897 * We defer the timer blanking to work queue so it can take the console mutex 4898 * (console operations can still happen at irq time, but only from printk which 4899 * has the console mutex. Not perfect yet, but better than no locking 4900 */ 4901 static void blank_screen_t(struct timer_list *unused) 4902 { 4903 blank_timer_expired = 1; 4904 schedule_work(&console_work); 4905 } 4906 4907 void poke_blanked_console(void) 4908 { 4909 WARN_CONSOLE_UNLOCKED(); 4910 4911 /* Add this so we quickly catch whoever might call us in a non 4912 * safe context. Nowadays, unblank_screen() isn't to be called in 4913 * atomic contexts and is allowed to schedule (with the special case 4914 * of oops_in_progress, but that isn't of any concern for this 4915 * function. --BenH. 4916 */ 4917 might_sleep(); 4918 4919 /* This isn't perfectly race free, but a race here would be mostly harmless, 4920 * at worst, we'll do a spurious blank and it's unlikely 4921 */ 4922 timer_delete(&console_timer); 4923 blank_timer_expired = 0; 4924 4925 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS) 4926 return; 4927 if (console_blanked) 4928 unblank_screen(); 4929 else if (blankinterval) { 4930 mod_timer(&console_timer, jiffies + (blankinterval * HZ)); 4931 blank_state = blank_normal_wait; 4932 } 4933 } 4934 4935 /* 4936 * Palettes 4937 */ 4938 4939 static void set_palette(struct vc_data *vc) 4940 { 4941 WARN_CONSOLE_UNLOCKED(); 4942 4943 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_set_palette) 4944 vc->vc_sw->con_set_palette(vc, color_table); 4945 } 4946 4947 /* 4948 * Load palette into the DAC registers. arg points to a colour 4949 * map, 3 bytes per colour, 16 colours, range from 0 to 255. 4950 */ 4951 4952 int con_set_cmap(unsigned char __user *arg) 4953 { 4954 int i, j, k; 4955 unsigned char colormap[3*16]; 4956 4957 if (copy_from_user(colormap, arg, sizeof(colormap))) 4958 return -EFAULT; 4959 4960 guard(console_lock)(); 4961 for (i = k = 0; i < 16; i++) { 4962 default_red[i] = colormap[k++]; 4963 default_grn[i] = colormap[k++]; 4964 default_blu[i] = colormap[k++]; 4965 } 4966 for (i = 0; i < MAX_NR_CONSOLES; i++) { 4967 if (!vc_cons_allocated(i)) 4968 continue; 4969 for (j = k = 0; j < 16; j++) { 4970 vc_cons[i].d->vc_palette[k++] = default_red[j]; 4971 vc_cons[i].d->vc_palette[k++] = default_grn[j]; 4972 vc_cons[i].d->vc_palette[k++] = default_blu[j]; 4973 } 4974 set_palette(vc_cons[i].d); 4975 } 4976 4977 return 0; 4978 } 4979 4980 int con_get_cmap(unsigned char __user *arg) 4981 { 4982 int i, k; 4983 unsigned char colormap[3*16]; 4984 4985 scoped_guard(console_lock) 4986 for (i = k = 0; i < 16; i++) { 4987 colormap[k++] = default_red[i]; 4988 colormap[k++] = default_grn[i]; 4989 colormap[k++] = default_blu[i]; 4990 } 4991 4992 if (copy_to_user(arg, colormap, sizeof(colormap))) 4993 return -EFAULT; 4994 4995 return 0; 4996 } 4997 4998 void reset_palette(struct vc_data *vc) 4999 { 5000 int j, k; 5001 for (j=k=0; j<16; j++) { 5002 vc->vc_palette[k++] = default_red[j]; 5003 vc->vc_palette[k++] = default_grn[j]; 5004 vc->vc_palette[k++] = default_blu[j]; 5005 } 5006 set_palette(vc); 5007 } 5008 5009 /* 5010 * Font switching 5011 * 5012 * Currently we only support fonts up to 128 pixels wide, at a maximum height 5013 * of 128 pixels. Userspace fontdata may have to be stored with 32 bytes 5014 * (shorts/ints, depending on width) reserved for each character which is 5015 * kinda wasty, but this is done in order to maintain compatibility with the 5016 * EGA/VGA fonts. It is up to the actual low-level console-driver convert data 5017 * into its favorite format (maybe we should add a `fontoffset' field to the 5018 * `display' structure so we won't have to convert the fontdata all the time. 5019 * /Jes 5020 */ 5021 5022 #define max_font_width 64 5023 #define max_font_height 128 5024 #define max_font_glyphs 512 5025 #define max_font_size (max_font_glyphs*max_font_width*max_font_height) 5026 5027 static int con_font_get(struct vc_data *vc, struct console_font_op *op) 5028 { 5029 struct console_font font; 5030 int c; 5031 unsigned int vpitch = op->op == KD_FONT_OP_GET_TALL ? op->height : 32; 5032 5033 if (vpitch > max_font_height) 5034 return -EINVAL; 5035 5036 void *font_data __free(kvfree) = NULL; 5037 if (op->data) { 5038 font.data = font_data = kvzalloc(max_font_size, GFP_KERNEL); 5039 if (!font.data) 5040 return -ENOMEM; 5041 } else 5042 font.data = NULL; 5043 5044 scoped_guard(console_lock) { 5045 if (vc->vc_mode != KD_TEXT) 5046 return -EINVAL; 5047 if (!vc->vc_sw->con_font_get) 5048 return -ENOSYS; 5049 5050 int ret = vc->vc_sw->con_font_get(vc, &font, vpitch); 5051 if (ret) 5052 return ret; 5053 } 5054 5055 c = DIV_ROUND_UP(font.width, 8) * vpitch * font.charcount; 5056 5057 if (op->data && font.charcount > op->charcount) 5058 return -ENOSPC; 5059 if (font.width > op->width || font.height > op->height) 5060 return -ENOSPC; 5061 5062 op->height = font.height; 5063 op->width = font.width; 5064 op->charcount = font.charcount; 5065 5066 if (op->data && copy_to_user(op->data, font.data, c)) 5067 return -EFAULT; 5068 5069 return 0; 5070 } 5071 5072 static int con_font_set(struct vc_data *vc, const struct console_font_op *op) 5073 { 5074 struct console_font font; 5075 int size; 5076 unsigned int vpitch = op->op == KD_FONT_OP_SET_TALL ? op->height : 32; 5077 5078 if (!op->data) 5079 return -EINVAL; 5080 if (op->charcount > max_font_glyphs) 5081 return -EINVAL; 5082 if (op->width <= 0 || op->width > max_font_width || !op->height || 5083 op->height > max_font_height) 5084 return -EINVAL; 5085 if (vpitch < op->height) 5086 return -EINVAL; 5087 size = DIV_ROUND_UP(op->width, 8) * vpitch * op->charcount; 5088 if (size > max_font_size) 5089 return -ENOSPC; 5090 5091 void *font_data __free(kfree) = font.data = memdup_user(op->data, size); 5092 if (IS_ERR(font.data)) 5093 return PTR_ERR(font.data); 5094 5095 font.charcount = op->charcount; 5096 font.width = op->width; 5097 font.height = op->height; 5098 5099 guard(console_lock)(); 5100 5101 if (vc->vc_mode != KD_TEXT) 5102 return -EINVAL; 5103 if (!vc->vc_sw->con_font_set) 5104 return -ENOSYS; 5105 5106 /* hide selection and cursor prior font changes */ 5107 hide_cursor(vc); 5108 5109 return vc->vc_sw->con_font_set(vc, &font, vpitch, op->flags); 5110 } 5111 5112 static int con_font_default(struct vc_data *vc, struct console_font_op *op) 5113 { 5114 struct console_font font = {.width = op->width, .height = op->height}; 5115 char name[MAX_FONT_NAME]; 5116 char *s = name; 5117 5118 if (!op->data) 5119 s = NULL; 5120 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0) 5121 return -EFAULT; 5122 else 5123 name[MAX_FONT_NAME - 1] = 0; 5124 5125 scoped_guard(console_lock) { 5126 if (vc->vc_mode != KD_TEXT) 5127 return -EINVAL; 5128 if (!vc->vc_sw->con_font_default) 5129 return -ENOSYS; 5130 5131 /* hide selection and cursor prior font changes */ 5132 hide_cursor(vc); 5133 5134 int ret = vc->vc_sw->con_font_default(vc, &font, s); 5135 if (ret) 5136 return ret; 5137 } 5138 5139 op->width = font.width; 5140 op->height = font.height; 5141 5142 return 0; 5143 } 5144 5145 int con_font_op(struct vc_data *vc, struct console_font_op *op) 5146 { 5147 switch (op->op) { 5148 case KD_FONT_OP_SET: 5149 case KD_FONT_OP_SET_TALL: 5150 return con_font_set(vc, op); 5151 case KD_FONT_OP_GET: 5152 case KD_FONT_OP_GET_TALL: 5153 return con_font_get(vc, op); 5154 case KD_FONT_OP_SET_DEFAULT: 5155 return con_font_default(vc, op); 5156 case KD_FONT_OP_COPY: 5157 /* was buggy and never really used */ 5158 return -EINVAL; 5159 } 5160 return -ENOSYS; 5161 } 5162 5163 /* 5164 * Interface exported to selection and vcs. 5165 */ 5166 5167 /* used by selection */ 5168 u16 screen_glyph(const struct vc_data *vc, int offset) 5169 { 5170 u16 w = scr_readw(screenpos(vc, offset, true)); 5171 u16 c = w & 0xff; 5172 5173 if (w & vc->vc_hi_font_mask) 5174 c |= 0x100; 5175 return c; 5176 } 5177 EXPORT_SYMBOL_GPL(screen_glyph); 5178 5179 u32 screen_glyph_unicode(const struct vc_data *vc, int n) 5180 { 5181 u32 **uni_lines = vc->vc_uni_lines; 5182 5183 if (uni_lines) 5184 return uni_lines[n / vc->vc_cols][n % vc->vc_cols]; 5185 5186 return inverse_translate(vc, screen_glyph(vc, n * 2), true); 5187 } 5188 EXPORT_SYMBOL_GPL(screen_glyph_unicode); 5189 5190 /* used by vcs - note the word offset */ 5191 unsigned short *screen_pos(const struct vc_data *vc, int w_offset, bool viewed) 5192 { 5193 return screenpos(vc, 2 * w_offset, viewed); 5194 } 5195 EXPORT_SYMBOL_GPL(screen_pos); 5196 5197 void getconsxy(const struct vc_data *vc, unsigned char xy[static 2]) 5198 { 5199 /* clamp values if they don't fit */ 5200 xy[0] = min(vc->state.x, 0xFFu); 5201 xy[1] = min(vc->state.y, 0xFFu); 5202 } 5203 5204 void putconsxy(struct vc_data *vc, unsigned char xy[static const 2]) 5205 { 5206 hide_cursor(vc); 5207 gotoxy(vc, xy[0], xy[1]); 5208 set_cursor(vc); 5209 } 5210 5211 u16 vcs_scr_readw(const struct vc_data *vc, const u16 *org) 5212 { 5213 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1) 5214 return softcursor_original; 5215 return scr_readw(org); 5216 } 5217 5218 void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org) 5219 { 5220 scr_writew(val, org); 5221 if ((unsigned long)org == vc->vc_pos) { 5222 softcursor_original = -1; 5223 add_softcursor(vc); 5224 } 5225 } 5226 5227 void vcs_scr_updated(struct vc_data *vc) 5228 { 5229 notify_update(vc); 5230 } 5231