xref: /netbsd-src/sys/arch/amd64/stand/prekern/console.c (revision dadf0eef45c0862a0008b5e5b75d17ad81495ef6)
1*dadf0eefSkhorben /*	$NetBSD: console.c,v 1.7 2021/05/04 21:09:16 khorben Exp $	*/
2c9759921Smaxv 
3c9759921Smaxv /*
436a49950Smaxv  * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
5c9759921Smaxv  *
6c9759921Smaxv  * This code is derived from software contributed to The NetBSD Foundation
7c9759921Smaxv  * by Maxime Villard.
8c9759921Smaxv  *
9c9759921Smaxv  * Redistribution and use in source and binary forms, with or without
10c9759921Smaxv  * modification, are permitted provided that the following conditions
11c9759921Smaxv  * are met:
12c9759921Smaxv  * 1. Redistributions of source code must retain the above copyright
13c9759921Smaxv  *    notice, this list of conditions and the following disclaimer.
14c9759921Smaxv  * 2. Redistributions in binary form must reproduce the above copyright
15c9759921Smaxv  *    notice, this list of conditions and the following disclaimer in the
16c9759921Smaxv  *    documentation and/or other materials provided with the distribution.
17c9759921Smaxv  *
18c9759921Smaxv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19c9759921Smaxv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20c9759921Smaxv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21c9759921Smaxv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22c9759921Smaxv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23c9759921Smaxv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24c9759921Smaxv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25c9759921Smaxv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26c9759921Smaxv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27c9759921Smaxv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28c9759921Smaxv  * POSSIBILITY OF SUCH DAMAGE.
29c9759921Smaxv  */
30c9759921Smaxv 
31c9759921Smaxv #include "prekern.h"
32c9759921Smaxv 
33c9759921Smaxv extern vaddr_t atdevbase;
34c9759921Smaxv #define CONS_WID 80
35c9759921Smaxv #define CONS_HEI 25
36c9759921Smaxv 
37c9759921Smaxv static char *cons_start;
38c9759921Smaxv static size_t cons_x, cons_y;
39c9759921Smaxv static char cons_buffer[CONS_WID * 2 * CONS_HEI];
40c9759921Smaxv 
init_cons(void)4192fe5cfbSmaxv void init_cons(void)
42c9759921Smaxv {
43c9759921Smaxv 	cons_start = (char *)atdevbase + (0xB8000 - IOM_BEGIN);
44c9759921Smaxv 	cons_x = 0;
45c9759921Smaxv 	cons_y = 0;
46c9759921Smaxv }
47c9759921Smaxv 
check_scroll(void)4892fe5cfbSmaxv static void check_scroll(void)
49c9759921Smaxv {
50c9759921Smaxv 	char *src, *dst;
51c9759921Smaxv 	size_t i;
52c9759921Smaxv 
53c9759921Smaxv 	if (cons_y != CONS_HEI)
54c9759921Smaxv 		return;
55c9759921Smaxv 
56c9759921Smaxv 	for (i = 0; i < CONS_HEI-1; i++) {
57c9759921Smaxv 		dst = &cons_buffer[0] + i * (CONS_WID * 2);
58c9759921Smaxv 		src = &cons_buffer[0] + (i + 1) * (CONS_WID * 2);
59c9759921Smaxv 		memcpy(dst, src, (CONS_WID * 2));
60c9759921Smaxv 	}
61c9759921Smaxv 	memset(&cons_buffer[0] + (CONS_WID * 2) * (CONS_HEI-1), 0,
62c9759921Smaxv 	    (CONS_WID * 2));
63c9759921Smaxv 	cons_y--;
64ef12f26eSmaxv 	memcpy(cons_start, &cons_buffer[0], CONS_WID * 2 * CONS_HEI);
65c9759921Smaxv }
66c9759921Smaxv 
putc(int color,char c)67f4eb15a8Smaxv static void putc(int color, char c)
68c9759921Smaxv {
69c9759921Smaxv 	char *ptr, *scr;
70c9759921Smaxv 
71f4eb15a8Smaxv 	if (c == '\n') {
72c9759921Smaxv 		cons_x = 0;
73c9759921Smaxv 		cons_y++;
74c9759921Smaxv 		check_scroll();
75c9759921Smaxv 	} else {
76c9759921Smaxv 		if (cons_x + 1 == CONS_WID) {
77c9759921Smaxv 			cons_x = 0;
78c9759921Smaxv 			cons_y++;
79c9759921Smaxv 			check_scroll();
80c9759921Smaxv 		}
81c9759921Smaxv 		ptr = (cons_start + 2 * cons_x + 160 * cons_y);
82c9759921Smaxv 		scr = (cons_buffer + 2 * cons_x + 160 * cons_y);
83f4eb15a8Smaxv 		ptr[0] = scr[0] = c;
84c9759921Smaxv 		ptr[1] = scr[1] = color;
85c9759921Smaxv 		cons_x++;
86c9759921Smaxv 	}
87c9759921Smaxv }
88f4eb15a8Smaxv 
print_ext(int color,char * buf)89f4eb15a8Smaxv void print_ext(int color, char *buf)
90f4eb15a8Smaxv {
91f4eb15a8Smaxv 	size_t i;
92f4eb15a8Smaxv 
93f4eb15a8Smaxv 	for (i = 0; buf[i] != '\0'; i++) {
94f4eb15a8Smaxv 		putc(color, buf[i]);
95f4eb15a8Smaxv 	}
96c9759921Smaxv }
97c9759921Smaxv 
print(char * buf)98c9759921Smaxv void print(char *buf)
99c9759921Smaxv {
100c9759921Smaxv 	print_ext(WHITE_ON_BLACK, buf);
101c9759921Smaxv }
102c9759921Smaxv 
print_state(state_t state,char * buf)103*dadf0eefSkhorben void print_state(state_t state, char *buf)
104c9759921Smaxv {
105c9759921Smaxv 	print("[");
106*dadf0eefSkhorben 	switch (state)
107*dadf0eefSkhorben 	{
108*dadf0eefSkhorben 		case STATE_NORMAL:
109c9759921Smaxv 			print_ext(GREEN_ON_BLACK, "+");
110*dadf0eefSkhorben 			break;
111*dadf0eefSkhorben 		case STATE_ERROR:
112c9759921Smaxv 			print_ext(RED_ON_BLACK, "!");
113*dadf0eefSkhorben 			break;
114*dadf0eefSkhorben 		case STATE_WARNING:
115*dadf0eefSkhorben 			print_ext(YELLOW_ON_BLACK, "*");
116*dadf0eefSkhorben 			break;
117*dadf0eefSkhorben 		default:
118*dadf0eefSkhorben 			print_ext(WHITE_ON_BLACK, "?");
119*dadf0eefSkhorben 			break;
120*dadf0eefSkhorben 	}
121c9759921Smaxv 	print("] ");
122c9759921Smaxv 	print(buf);
123c9759921Smaxv 	print("\n");
124c9759921Smaxv }
125c9759921Smaxv 
print_banner(void)12692fe5cfbSmaxv void print_banner(void)
127c9759921Smaxv {
128c9759921Smaxv 	char *banner =
129c9759921Smaxv 		"           __________                 __                        \n"
130c9759921Smaxv 		"           \\______   \\_______   ____ |  | __ ___________  ____  \n"
131c9759921Smaxv 		"            |     ___/\\_  __ \\_/ __ \\|  |/ // __ \\_  __ \\/    \\ \n"
132c9759921Smaxv 		"            |    |     |  | \\/\\  ___/|    <\\  ___/|  | \\/   |  \\\n"
133c9759921Smaxv 		"            |____|     |__|    \\___  >__|_ \\\\___  >__|  |___|  /\n"
134c9759921Smaxv 		"                                   \\/     \\/    \\/           \\/    Version 1.0\n"
135c9759921Smaxv 	;
136c9759921Smaxv 	print(banner);
137c9759921Smaxv }
138