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