1/*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp> 5 * Copyright (c) 1992-1998 S��ren Schmidt 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer, 13 * without modification, immediately at the beginning of the file. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33__FBSDID("$FreeBSD$"); 34 35#include "opt_syscons.h" 36 37#ifndef SC_NO_HISTORY 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/conf.h> 42#include <sys/consio.h> 43#include <sys/tty.h> 44#include <sys/kernel.h> 45#include <sys/malloc.h> 46 47#if defined(__arm__) || defined(__mips__) || \ 48 defined(__powerpc__) || defined(__sparc64__) 49#include <machine/sc_machdep.h> 50#else 51#include <machine/pc/display.h> 52#endif 53 54#include <dev/syscons/syscons.h> 55 56/* 57 * XXX Placeholder. 58 * This calculations should be dynamically scaled by number of separate sc 59 * devices. A base value of 'extra_history_size' should be defined for 60 * each syscons unit, and added and subtracted from the dynamic 61 * 'extra_history_size' as units are added and removed. This way, each time 62 * a new syscons unit goes online, extra_history_size is automatically bumped. 63 */ 64#define MAXSC 1 65 66#if !defined(SC_MAX_HISTORY_SIZE) 67#define SC_MAX_HISTORY_SIZE (1000 * MAXCONS * MAXSC) 68#endif 69 70#if !defined(SC_HISTORY_SIZE) 71#define SC_HISTORY_SIZE (ROW * 4) 72#endif 73 74#if (SC_HISTORY_SIZE * MAXCONS * MAXSC) > SC_MAX_HISTORY_SIZE 75#undef SC_MAX_HISTORY_SIZE 76#define SC_MAX_HISTORY_SIZE (SC_HISTORY_SIZE * MAXCONS * MAXSC) 77#endif 78 79/* local variables */ 80static int extra_history_size 81 = SC_MAX_HISTORY_SIZE - SC_HISTORY_SIZE*MAXCONS; 82 83/* local functions */ 84static void copy_history(sc_vtb_t *from, sc_vtb_t *to); 85static void history_to_screen(scr_stat *scp); 86 87/* allocate a history buffer */ 88int 89sc_alloc_history_buffer(scr_stat *scp, int lines, int prev_ysize, int wait) 90{ 91 /* 92 * syscons unconditionally allocates buffers up to 93 * SC_HISTORY_SIZE lines or scp->ysize lines, whichever 94 * is larger. A value greater than that is allowed, 95 * subject to extra_history_size. 96 */ 97 sc_vtb_t *history; 98 sc_vtb_t *prev_history; 99 int cur_lines; /* current buffer size */ 100 int min_lines; /* guaranteed buffer size */ 101 int delta; /* lines to put back */ 102 103 if (lines <= 0) 104 lines = SC_HISTORY_SIZE; /* use the default value */ 105 106 /* make it at least as large as the screen size */ 107 lines = imax(lines, scp->ysize); 108 109 /* remove the history buffer while we update it */ 110 history = prev_history = scp->history; 111 scp->history = NULL; 112 113 /* calculate the amount of lines to put back to extra_history_size */ 114 delta = 0; 115 if (prev_history) { 116 cur_lines = sc_vtb_rows(history); 117 min_lines = imax(SC_HISTORY_SIZE, prev_ysize); 118 if (cur_lines > min_lines) 119 delta = cur_lines - min_lines; 120 } 121 122 /* lines up to min_lines are always allowed. */ 123 min_lines = imax(SC_HISTORY_SIZE, scp->ysize); 124 if (lines > min_lines) { 125 if (lines - min_lines > extra_history_size + delta) { 126 /* too many lines are requested */ 127 scp->history = prev_history; 128 return EINVAL; 129 } 130 } 131 132 /* allocate a new buffer */ 133 history = (sc_vtb_t *)malloc(sizeof(*history), 134 M_DEVBUF, 135 (wait) ? M_WAITOK : M_NOWAIT); 136 if (history != NULL) { 137 if (lines > min_lines) 138 extra_history_size -= lines - min_lines; 139 /* XXX error check? */ 140 sc_vtb_init(history, VTB_RINGBUFFER, scp->xsize, lines, 141 NULL, wait); 142 /* FIXME: XXX no good? */ 143 sc_vtb_clear(history, scp->sc->scr_map[0x20], 144 SC_NORM_ATTR << 8); 145 if (prev_history != NULL) 146 copy_history(prev_history, history); 147 scp->history_pos = sc_vtb_tail(history); 148 } else { 149 scp->history_pos = 0; 150 } 151 152 /* destroy the previous buffer */ 153 if (prev_history != NULL) { 154 extra_history_size += delta; 155 sc_vtb_destroy(prev_history); 156 free(prev_history, M_DEVBUF); 157 } 158 159 scp->history = history; 160 161 return 0; 162} 163 164static void 165copy_history(sc_vtb_t *from, sc_vtb_t *to) 166{ 167 int lines; 168 int cols; 169 int cols1; 170 int cols2; 171 int pos; 172 int i; 173 174 lines = sc_vtb_rows(from); 175 cols1 = sc_vtb_cols(from); 176 cols2 = sc_vtb_cols(to); 177 cols = imin(cols1, cols2); 178 pos = sc_vtb_tail(from); 179 for (i = 0; i < lines; ++i) { 180 sc_vtb_append(from, pos, to, cols); 181 if (cols < cols2) 182 sc_vtb_seek(to, sc_vtb_pos(to, 183 sc_vtb_tail(to), 184 cols2 - cols)); 185 pos = sc_vtb_pos(from, pos, cols1); 186 } 187} 188 189void 190sc_free_history_buffer(scr_stat *scp, int prev_ysize) 191{ 192 sc_vtb_t *history; 193 int cur_lines; /* current buffer size */ 194 int min_lines; /* guaranteed buffer size */ 195 196 history = scp->history; 197 scp->history = NULL; 198 if (history == NULL) 199 return; 200 201 cur_lines = sc_vtb_rows(history); 202 min_lines = imax(SC_HISTORY_SIZE, prev_ysize); 203 extra_history_size += (cur_lines > min_lines) ? 204 cur_lines - min_lines : 0; 205 206 sc_vtb_destroy(history); 207 free(history, M_DEVBUF); 208} 209 210/* copy entire screen into the top of the history buffer */ 211void 212sc_hist_save(scr_stat *scp) 213{ 214 sc_vtb_append(&scp->vtb, 0, scp->history, scp->xsize*scp->ysize); 215 scp->history_pos = sc_vtb_tail(scp->history); 216} 217 218/* restore the screen by copying from the history buffer */ 219int 220sc_hist_restore(scr_stat *scp) 221{ 222 int ret; 223 224 if (scp->history_pos != sc_vtb_tail(scp->history)) { 225 scp->history_pos = sc_vtb_tail(scp->history); 226 history_to_screen(scp); 227 ret = 0; 228 } else { 229 ret = 1; 230 } 231 sc_vtb_seek(scp->history, sc_vtb_pos(scp->history, 232 sc_vtb_tail(scp->history), 233 -scp->xsize*scp->ysize)); 234 return ret; 235} 236 237/* copy screen-full of saved lines */ 238static void 239history_to_screen(scr_stat *scp) 240{ 241 int pos; 242 int i; 243 244 pos = scp->history_pos; 245 for (i = 1; i <= scp->ysize; ++i) { 246 pos = sc_vtb_pos(scp->history, pos, -scp->xsize); 247 sc_vtb_copy(scp->history, pos, 248 &scp->vtb, scp->xsize*(scp->ysize - i), 249 scp->xsize); 250 } 251 mark_all(scp); 252} 253 254/* go to the tail of the history buffer */ 255void 256sc_hist_home(scr_stat *scp) 257{ 258 scp->history_pos = sc_vtb_tail(scp->history); 259 history_to_screen(scp); 260} 261 262/* go to the top of the history buffer */ 263void 264sc_hist_end(scr_stat *scp) 265{ 266 scp->history_pos = sc_vtb_pos(scp->history, sc_vtb_tail(scp->history), 267 scp->xsize*scp->ysize); 268 history_to_screen(scp); 269} 270 271/* move one line up */ 272int 273sc_hist_up_line(scr_stat *scp) 274{ 275 if (sc_vtb_pos(scp->history, scp->history_pos, -(scp->xsize*scp->ysize)) 276 == sc_vtb_tail(scp->history)) 277 return -1; 278 scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos, 279 -scp->xsize); 280 history_to_screen(scp); 281 return 0; 282} 283 284/* move one line down */ 285int 286sc_hist_down_line(scr_stat *scp) 287{ 288 if (scp->history_pos == sc_vtb_tail(scp->history)) 289 return -1; 290 scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos, 291 scp->xsize); 292 history_to_screen(scp); 293 return 0; 294} 295 296int 297sc_hist_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td) 298{ 299 scr_stat *scp; 300 int error; 301 302 switch (cmd) { 303 304 case CONS_HISTORY: /* set history size */ 305 scp = SC_STAT(tp); 306 if (*(int *)data <= 0) 307 return EINVAL; 308 if (scp->status & BUFFER_SAVED) 309 return EBUSY; 310 DPRINTF(5, ("lines:%d, ysize:%d, pool:%d\n", 311 *(int *)data, scp->ysize, extra_history_size)); 312 error = sc_alloc_history_buffer(scp, 313 imax(*(int *)data, scp->ysize), 314 scp->ysize, TRUE); 315 DPRINTF(5, ("error:%d, rows:%d, pool:%d\n", error, 316 sc_vtb_rows(scp->history), extra_history_size)); 317 return error; 318 319 case CONS_CLRHIST: 320 scp = SC_STAT(tp); 321 sc_vtb_clear(scp->history, scp->sc->scr_map[0x20], 322 SC_NORM_ATTR << 8); 323 return 0; 324 } 325 326 return ENOIOCTL; 327} 328 329#endif /* SC_NO_HISTORY */ 330