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