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