xref: /netbsd-src/sys/arch/amd64/stand/prekern/console.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /*	$NetBSD: console.c,v 1.4 2019/04/03 19:14:25 maxv Exp $	*/
2 
3 /*
4  * Copyright (c) 2017 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 
41 void init_cons(void)
42 {
43 	cons_start = (char *)atdevbase + (0xB8000 - IOM_BEGIN);
44 	cons_x = 0;
45 	cons_y = 0;
46 }
47 
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 
67 void print_ext(int color, char *buf)
68 {
69 	char *ptr, *scr;
70 	size_t i;
71 
72 	for (i = 0; buf[i] != '\0'; i++) {
73 		if (buf[i] == '\n') {
74 			cons_x = 0;
75 			cons_y++;
76 			check_scroll();
77 		} else {
78 			if (cons_x + 1 == CONS_WID) {
79 				cons_x = 0;
80 				cons_y++;
81 				check_scroll();
82 			}
83 			ptr = (cons_start + 2 * cons_x + 160 * cons_y);
84 			scr = (cons_buffer + 2 * cons_x + 160 * cons_y);
85 			ptr[0] = scr[0] = buf[i];
86 			ptr[1] = scr[1] = color;
87 			cons_x++;
88 		}
89 	}
90 }
91 
92 void print(char *buf)
93 {
94 	print_ext(WHITE_ON_BLACK, buf);
95 }
96 
97 void print_state(bool ok, char *buf)
98 {
99 	print("[");
100 	if (ok)
101 		print_ext(GREEN_ON_BLACK, "+");
102 	else
103 		print_ext(RED_ON_BLACK, "!");
104 	print("] ");
105 	print(buf);
106 	print("\n");
107 }
108 
109 void print_banner(void)
110 {
111 	char *banner =
112 		"           __________                 __                        \n"
113 		"           \\______   \\_______   ____ |  | __ ___________  ____  \n"
114 		"            |     ___/\\_  __ \\_/ __ \\|  |/ // __ \\_  __ \\/    \\ \n"
115 		"            |    |     |  | \\/\\  ___/|    <\\  ___/|  | \\/   |  \\\n"
116 		"            |____|     |__|    \\___  >__|_ \\\\___  >__|  |___|  /\n"
117 		"                                   \\/     \\/    \\/           \\/    Version 1.0\n"
118 	;
119 	print(banner);
120 }
121