1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the "License"). You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | * ␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | /* ␊ |
25 | * Mach Operating System␊ |
26 | * Copyright (c) 1990 Carnegie-Mellon University␊ |
27 | * Copyright (c) 1989 Carnegie-Mellon University␊ |
28 | * All rights reserved. The CMU software License Agreement specifies␊ |
29 | * the terms and conditions for use and redistribution.␊ |
30 | */␊ |
31 | ␊ |
32 | /*␊ |
33 | * ␉␉␉INTEL CORPORATION PROPRIETARY INFORMATION␊ |
34 | *␊ |
35 | *␉This software is supplied under the terms of a license agreement or ␊ |
36 | *␉nondisclosure agreement with Intel Corporation and may not be copied ␊ |
37 | *␉nor disclosed except in accordance with the terms of that agreement.␊ |
38 | *␊ |
39 | *␉Copyright 1988, 1989 Intel Corporation␊ |
40 | */␊ |
41 | ␊ |
42 | /*␊ |
43 | * Copyright 1993 NeXT, Inc.␊ |
44 | * All rights reserved.␊ |
45 | */␊ |
46 | ␊ |
47 | #include "libsaio.h"␊ |
48 | #include "bootstruct.h"␊ |
49 | #include "platform.h"␊ |
50 | ␊ |
51 | /* Kabyl: BooterLog */␊ |
52 | #define BOOTER_LOG_SIZE (128 * 1024)␊ |
53 | #define SAFE_LOG_SIZE 134␊ |
54 | ␊ |
55 | struct LOG {␊ |
56 | char *buf;␊ |
57 | char *cursor;␊ |
58 | };␊ |
59 | typedef struct LOG LOG;␊ |
60 | static LOG booterlog;␊ |
61 | ␊ |
62 | struct putc_info {␊ |
63 | char * str;␊ |
64 | char * last_str;␊ |
65 | };␊ |
66 | ␊ |
67 | void sputc(int c, struct putc_info * pi)␊ |
68 | {␊ |
69 | ␉if (pi->last_str)␊ |
70 | ␉␉if (pi->str == pi->last_str)␊ |
71 | ␉␉{␊ |
72 | ␉␉␉*(pi->str) = '\0';␊ |
73 | ␉␉␉return;␊ |
74 | ␉␉}␊ |
75 | ␉*(pi->str)++ = c;␊ |
76 | }␊ |
77 | ␊ |
78 | void initBooterLog(void)␊ |
79 | {␊ |
80 | ␉booterlog.buf = malloc(BOOTER_LOG_SIZE);␊ |
81 | if (!booterlog.buf) {␊ |
82 | printf("Couldn't allocate buffer for booter log\n");␊ |
83 | booterlog.cursor = 0;␊ |
84 | booterlog.buf = 0;␊ |
85 | return;␊ |
86 | }␊ |
87 | ␉bzero(booterlog.buf, BOOTER_LOG_SIZE);␊ |
88 | ␉booterlog.cursor = booterlog.buf; ␊ |
89 | ␉␊ |
90 | }␊ |
91 | ␊ |
92 | char *getConsoleMsg(void)␊ |
93 | {␊ |
94 | return booterlog.buf;␊ |
95 | }␊ |
96 | char *getConsoleCursor(void)␊ |
97 | {␊ |
98 | return booterlog.buf;␊ |
99 | }␊ |
100 | void setConsoleMsg(char *p)␊ |
101 | {␊ |
102 | booterlog.buf = p;␊ |
103 | }␊ |
104 | void setConsoleCursor(char *p)␊ |
105 | {␊ |
106 | booterlog.cursor = p;␊ |
107 | }␊ |
108 | ␊ |
109 | void msglog(const char * fmt, ...)␊ |
110 | {␊ |
111 | ␉va_list ap;␊ |
112 | ␉struct putc_info pi;␊ |
113 | ␉␊ |
114 | ␉if (!booterlog.buf)␊ |
115 | ␉␉return;␊ |
116 | ␉␊ |
117 | ␉if (((booterlog.cursor - booterlog.buf) > (BOOTER_LOG_SIZE - SAFE_LOG_SIZE)))␊ |
118 | ␉␉return;␊ |
119 | ␉␊ |
120 | ␉va_start(ap, fmt);␊ |
121 | ␉pi.str = booterlog.cursor;␊ |
122 | ␉pi.last_str = 0;␊ |
123 | ␉prf(fmt, ap, sputc, &pi);␊ |
124 | ␉va_end(ap);␊ |
125 | ␉booterlog.cursor += strlen((char *)booterlog.cursor);␊ |
126 | }␊ |
127 | ␊ |
128 | void setupBooterLog(void)␊ |
129 | {␊ |
130 | ␉if (!booterlog.buf)␊ |
131 | ␉␉return;␉␊ |
132 | ␊ |
133 | ␉Node *node = DT__FindNode("/", false);␊ |
134 | ␉if (node)␊ |
135 | ␉␉DT__AddProperty(node, "boot-log", strlen((char *)booterlog.buf) + 1, booterlog.buf);␊ |
136 | }␊ |
137 | ␊ |
138 | /* Kabyl: !BooterLog */␊ |
139 | ␊ |
140 | ␊ |
141 | /*␊ |
142 | * write one character to console␊ |
143 | */␊ |
144 | void putchar(int c)␊ |
145 | {␊ |
146 | ␉if ( c == '\t' )␊ |
147 | ␉{␊ |
148 | ␉␉for (c = 0; c < 8; c++) putc(' ');␊ |
149 | ␉␉return;␊ |
150 | ␉}␊ |
151 | ␉␊ |
152 | ␉if ( c == '\n' )␊ |
153 | {␊ |
154 | ␉␉putc('\r');␊ |
155 | }␊ |
156 | ␉␊ |
157 | ␉putc(c);␊ |
158 | }␊ |
159 | ␊ |
160 | int getc()␊ |
161 | {␉␊ |
162 | int c = bgetc();␉␊ |
163 | ␉␊ |
164 | if ((c & 0xff) == 0) ␉␉␊ |
165 | return c;␊ |
166 | else ␉␊ |
167 | ␉␉return (c & 0xff); ␊ |
168 | }␊ |
169 | ␊ |
170 | // Read and echo a character from console. This doesn't echo backspace␊ |
171 | // since that screws up higher level handling␊ |
172 | ␊ |
173 | int getchar()␊ |
174 | {␊ |
175 | ␉register int c = getc();␊ |
176 | ␉␊ |
177 | ␉if ( c == '\r' ) c = '\n';␊ |
178 | ␉␊ |
179 | ␉if ( c >= ' ' && c < 0x7f) putchar(c);␊ |
180 | ␉␊ |
181 | ␉return (c);␊ |
182 | }␊ |
183 | ␊ |
184 | int printf(const char * fmt, ...)␊ |
185 | {␊ |
186 | va_list ap;␊ |
187 | ␉va_start(ap, fmt);␊ |
188 | ␉␊ |
189 | ␉prf(fmt, ap, putchar, 0);␊ |
190 | ␉␊ |
191 | ␉{␊ |
192 | ␉␉/* Kabyl: BooterLog */␊ |
193 | ␉␉struct putc_info pi;␊ |
194 | ␉␉␊ |
195 | ␉␉if (!booterlog.buf)␊ |
196 | ␉␉␉return 0;␊ |
197 | ␉␉␊ |
198 | ␉␉if (((booterlog.cursor - booterlog.buf) > (BOOTER_LOG_SIZE - SAFE_LOG_SIZE)))␊ |
199 | ␉␉␉return 0;␊ |
200 | ␉␉pi.str = booterlog.cursor;␊ |
201 | ␉␉pi.last_str = 0;␊ |
202 | ␉␉prf(fmt, ap, sputc, &pi);␊ |
203 | ␉␉booterlog.cursor += strlen((char *)booterlog.cursor);␊ |
204 | ␉}␊ |
205 | ␉␊ |
206 | ␉va_end(ap); ␊ |
207 | ␊ |
208 | return 0;␊ |
209 | }␊ |
210 | ␊ |
211 | int verbose(const char * fmt, ...)␊ |
212 | {␊ |
213 | va_list ap;␊ |
214 | ␊ |
215 | ␉va_start(ap, fmt);␊ |
216 | if (get_env(envgVerboseMode))␊ |
217 | {␊ |
218 | ␉␉prf(fmt, ap, putchar, 0);␊ |
219 | }␊ |
220 | ␉␊ |
221 | ␉{␊ |
222 | ␉␉/* Kabyl: BooterLog */␊ |
223 | ␉␉struct putc_info pi;␊ |
224 | ␉␉␊ |
225 | ␉␉if (!booterlog.buf)␊ |
226 | ␉␉␉return 0;␊ |
227 | ␉␉␊ |
228 | ␉␉if (((booterlog.cursor - booterlog.buf) > (BOOTER_LOG_SIZE - SAFE_LOG_SIZE)))␊ |
229 | ␉␉␉return 0;␊ |
230 | ␉␉pi.str = booterlog.cursor;␊ |
231 | ␉␉pi.last_str = 0;␊ |
232 | ␉␉prf(fmt, ap, sputc, &pi);␊ |
233 | ␉␉booterlog.cursor += strlen((char *)booterlog.cursor);␊ |
234 | ␉}␊ |
235 | ␉␊ |
236 | va_end(ap); ␊ |
237 | ␊ |
238 | return(0);␊ |
239 | }␊ |
240 | ␊ |
241 | int error(const char * fmt, ...)␊ |
242 | {␊ |
243 | va_list ap;␊ |
244 | struct putc_info pi;␊ |
245 | ␉int len;␊ |
246 | ␉char *str = NULL;␊ |
247 | ␉␊ |
248 | va_start(ap, fmt);␊ |
249 | ␉len = prf(fmt, ap, 0, 0);␊ |
250 | ␉if (len > 0)␊ |
251 | ␉{␊ |
252 | ␉␉str = newEmptyStringWithLength(len);␊ |
253 | ␉␉if (str != NULL) ␊ |
254 | ␉␉{␊ |
255 | ␉␉␉pi.last_str = 0;␊ |
256 | ␉␉␉␊ |
257 | ␉␉␉pi.str = str;␊ |
258 | ␉␉␉␊ |
259 | ␉␉␉prf(fmt, ap, sputc, &pi);␊ |
260 | ␉␉␉*pi.str = '\0';␊ |
261 | ␉␉}␊ |
262 | ␉␉␊ |
263 | ␉}␉␊ |
264 | va_end(ap);␊ |
265 | ␉␊ |
266 | ␉set_env_copy(envConsoleErr, str, len);␊ |
267 | ␉free(str);␊ |
268 | ␉␊ |
269 | return(0);␊ |
270 | }␊ |
271 | ␊ |
272 | void stop(const char * fmt, ...)␊ |
273 | {␊ |
274 | ␉va_list ap;␊ |
275 | ␉␊ |
276 | ␉printf("\n");␊ |
277 | ␉va_start(ap, fmt);␊ |
278 | ␉␊ |
279 | ␉prf(fmt, ap, putchar, 0);␊ |
280 | ␉␊ |
281 | ␉va_end(ap);␊ |
282 | ␉printf("\nThis is a non recoverable error! System HALTED!!!");␊ |
283 | ␉halt();␊ |
284 | ␉while (1);␊ |
285 | }␊ |
286 | ␊ |
287 | /** Print a "Press a key to continue..." message and wait for a key press. */␊ |
288 | void pause(void) ␊ |
289 | {␊ |
290 | printf("Press a key to continue...");␊ |
291 | getc();␊ |
292 | }␊ |
293 | ␊ |
294 | char * newStringWithFormat(const char * fmt, ...)␊ |
295 | {␊ |
296 | va_list ap;␊ |
297 | struct putc_info pi;␊ |
298 | ␉int len;␊ |
299 | ␉char *str = NULL;␊ |
300 | ␉␊ |
301 | va_start(ap, fmt);␊ |
302 | ␉len = prf(fmt, ap, 0, 0);␊ |
303 | ␉if (len > 0)␊ |
304 | ␉{␊ |
305 | ␉␉str = newEmptyStringWithLength(len);␊ |
306 | ␉␉if (str != NULL) ␊ |
307 | ␉␉{␊ |
308 | ␉␉␉pi.last_str = 0;␊ |
309 | ␉␉␉␊ |
310 | ␉␉␉pi.str = str;␊ |
311 | ␉␉␉␊ |
312 | ␉␉␉prf(fmt, ap, sputc, &pi);␊ |
313 | ␉␉␉*pi.str = '\0';␊ |
314 | ␉␉}␊ |
315 | ␉␉␊ |
316 | ␉}␉␊ |
317 | va_end(ap);␉␊ |
318 | ␉␊ |
319 | ␉return str;␊ |
320 | ␉␊ |
321 | } |