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