xref: /titanic_50/usr/src/uts/i86pc/dboot/dboot_printf.c (revision 532877c46d04a2d0b254f9b5797720078adcea07)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/machparam.h>
32 #include <sys/archsystm.h>
33 
34 #include <sys/boot_console.h>
35 
36 #include "dboot_printf.h"
37 
38 #include "dboot_xboot.h"
39 #include <sys/varargs.h>
40 
41 /*
42  * This file provides simple output formatting via dboot_printf()
43  */
44 
45 static void do_dboot_printf(char *fmt, va_list args);
46 
47 static char digits[] = "0123456789abcdef";
48 
49 /*
50  * Primitive version of panic, prints a message then resets the system
51  */
52 void
53 dboot_panic(char *fmt, ...)
54 {
55 	va_list	args;
56 
57 	va_start(args, fmt);
58 	do_dboot_printf(fmt, args);
59 
60 	if (console == CONS_SCREEN_TEXT) {
61 		dboot_printf("Press any key to reboot\n");
62 		(void) bcons_getchar();
63 	}
64 
65 	outb(0x64, 0xfe);	/* this resets the system, see pc_reset() */
66 	dboot_halt();		/* just in case */
67 }
68 
69 /*
70  * printf for boot code
71  */
72 void
73 dboot_printf(char *fmt, ...)
74 {
75 	va_list args;
76 
77 	va_start(args, fmt);
78 	do_dboot_printf(fmt, args);
79 }
80 
81 
82 /*
83  * output a string
84  */
85 static void
86 dboot_puts(char *s)
87 {
88 	while (*s != 0) {
89 		bcons_putchar(*s);
90 		++s;
91 	}
92 }
93 
94 static void
95 dboot_putnum(uint64_t x, uint_t is_signed, uint8_t base)
96 {
97 	char buffer[64];	/* digits in reverse order */
98 	int i;
99 
100 	if (is_signed && (int64_t)x < 0) {
101 		bcons_putchar('-');
102 		x = -x;
103 	}
104 
105 	for (i  = -1; x != 0 && i <= 63; x /= base)
106 		buffer[++i] = digits[x - ((x / base) * base)];
107 
108 	if (i < 0)
109 		buffer[++i] = '0';
110 
111 	while (i >= 0)
112 		bcons_putchar(buffer[i--]);
113 }
114 
115 /*
116  * very primitive printf - only does %s, %d, %x, %lx, or %%
117  */
118 static void
119 do_dboot_printf(char *fmt, va_list args)
120 {
121 	char *s;
122 	uint64_t x;
123 	uint8_t base;
124 	uint8_t size;
125 	uint_t is_signed = 1;
126 
127 	if (fmt == NULL) {
128 		dboot_puts("dboot_printf(): 1st arg is NULL\n");
129 		return;
130 	}
131 	for (; *fmt; ++fmt) {
132 		if (*fmt != '%') {
133 			bcons_putchar(*fmt);
134 			continue;
135 		}
136 
137 		size = 0;
138 again:
139 		++fmt;
140 		switch (*fmt) {
141 
142 		case '%':
143 			bcons_putchar(*fmt);
144 			break;
145 
146 		case 'c':
147 			x = va_arg(args, int);
148 			bcons_putchar(x);
149 			break;
150 
151 		case 's':
152 			s = va_arg(args, char *);
153 			if (s == NULL)
154 				dboot_puts("*NULL*");
155 			else
156 				dboot_puts(s);
157 			break;
158 
159 		case 'p':
160 			x = va_arg(args, ulong_t);
161 			dboot_putnum(x, !is_signed, 16);
162 			break;
163 
164 		case 'l':
165 			if (size == 0)
166 				size = sizeof (long);
167 			else if (size == sizeof (long))
168 				size = sizeof (long long);
169 			goto again;
170 
171 		case 'd':
172 			if (size == 0)
173 				x = va_arg(args, int);
174 			else if (size == sizeof (long))
175 				x = va_arg(args, long);
176 			else
177 				x = va_arg(args, long long);
178 			dboot_putnum(x, is_signed, 10);
179 			break;
180 
181 		case 'b':
182 			base = 2;
183 			goto unsigned_num;
184 
185 		case 'o':
186 			base = 8;
187 			goto unsigned_num;
188 
189 		case 'x':
190 			base = 16;
191 unsigned_num:
192 			if (size == 0)
193 				x = va_arg(args, uint_t);
194 			else if (size == sizeof (long))
195 				x = va_arg(args, ulong_t);
196 			else
197 				x = va_arg(args, unsigned long long);
198 			dboot_putnum(x, !is_signed, base);
199 			break;
200 
201 		default:
202 			dboot_puts("dboot_printf(): unknown % escape\n");
203 		}
204 	}
205 }
206