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