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