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 | //extern int␉vprf(const char * fmt, va_list ap);␊ |
52 | ␊ |
53 | bool gVerboseMode;␊ |
54 | bool gErrors;␊ |
55 | ␊ |
56 | /* Kabyl: BooterLog */␊ |
57 | #define BOOTER_LOG_SIZE (128 * 1024)␊ |
58 | #define SAFE_LOG_SIZE 134␊ |
59 | ␊ |
60 | char *msgbuf = 0;␊ |
61 | char *cursor = 0;␊ |
62 | ␊ |
63 | struct putc_info {␊ |
64 | char * str;␊ |
65 | char * last_str;␊ |
66 | };␊ |
67 | ␊ |
68 | void sputc(int c, struct putc_info * pi)␊ |
69 | {␊ |
70 | ␉if (pi->last_str)␊ |
71 | ␉␉if (pi->str == pi->last_str)␊ |
72 | ␉␉{␊ |
73 | ␉␉␉*(pi->str) = '\0';␊ |
74 | ␉␉␉return;␊ |
75 | ␉␉}␊ |
76 | ␉*(pi->str)++ = c;␊ |
77 | }␊ |
78 | ␊ |
79 | void initBooterLog(void)␊ |
80 | {␊ |
81 | ␉msgbuf = malloc(BOOTER_LOG_SIZE);␊ |
82 | ␉bzero(msgbuf, BOOTER_LOG_SIZE);␊ |
83 | ␉cursor = msgbuf; ␊ |
84 | ␊ |
85 | }␊ |
86 | ␊ |
87 | char *getConsoleMsg(void)␊ |
88 | {␊ |
89 | return msgbuf;␊ |
90 | }␊ |
91 | char *getConsoleCursor(void)␊ |
92 | {␊ |
93 | return cursor;␊ |
94 | }␊ |
95 | void setConsoleMsg(char *p)␊ |
96 | {␊ |
97 | msgbuf = p;␊ |
98 | }␊ |
99 | void setConsoleCursor(char *p)␊ |
100 | {␊ |
101 | cursor = p;␊ |
102 | }␊ |
103 | ␊ |
104 | ␊ |
105 | void msglog(const char * fmt, ...)␊ |
106 | {␊ |
107 | ␉va_list ap;␊ |
108 | ␉struct putc_info pi;␊ |
109 | ␉␊ |
110 | ␉if (!msgbuf)␊ |
111 | ␉␉return;␊ |
112 | ␉␊ |
113 | ␉if (((cursor - msgbuf) > (BOOTER_LOG_SIZE - SAFE_LOG_SIZE)))␊ |
114 | ␉␉return;␊ |
115 | ␉␊ |
116 | ␉va_start(ap, fmt);␊ |
117 | ␉pi.str = cursor;␊ |
118 | ␉pi.last_str = 0;␊ |
119 | ␉prf(fmt, ap, sputc, &pi);␊ |
120 | ␉va_end(ap);␊ |
121 | ␉cursor += strlen((char *)cursor);␊ |
122 | }␊ |
123 | ␊ |
124 | void setupBooterLog(void)␊ |
125 | {␊ |
126 | ␉if (!msgbuf)␊ |
127 | ␉␉return;␉␊ |
128 | ␊ |
129 | ␉Node *node = DT__FindNode("/", false);␊ |
130 | ␉if (node)␊ |
131 | ␉␉DT__AddProperty(node, "boot-log", strlen((char *)msgbuf) + 1, msgbuf);␊ |
132 | }␊ |
133 | /* Kabyl: !BooterLog */␊ |
134 | ␊ |
135 | ␊ |
136 | /*␊ |
137 | * write one character to console␊ |
138 | */␊ |
139 | void putchar(int c)␊ |
140 | {␊ |
141 | ␉if ( c == '\t' )␊ |
142 | ␉{␊ |
143 | ␉␉for (c = 0; c < 8; c++) putc(' ');␊ |
144 | ␉␉return;␊ |
145 | ␉}␊ |
146 | ␉␊ |
147 | ␉if ( c == '\n' )␊ |
148 | {␊ |
149 | ␉␉putc('\r');␊ |
150 | }␊ |
151 | ␉␊ |
152 | ␉putc(c);␊ |
153 | }␊ |
154 | ␊ |
155 | int getc()␊ |
156 | {␉␊ |
157 | int c = bgetc();␉␊ |
158 | ␉␊ |
159 | if ((c & 0xff) == 0) ␉␉␊ |
160 | return c;␊ |
161 | else ␉␊ |
162 | ␉␉return (c & 0xff); ␊ |
163 | }␊ |
164 | ␊ |
165 | // Read and echo a character from console. This doesn't echo backspace␊ |
166 | // since that screws up higher level handling␊ |
167 | ␊ |
168 | int getchar()␊ |
169 | {␊ |
170 | ␉register int c = getc();␊ |
171 | ␉␊ |
172 | ␉if ( c == '\r' ) c = '\n';␊ |
173 | ␉␊ |
174 | ␉if ( c >= ' ' && c < 0x7f) putchar(c);␊ |
175 | ␉␊ |
176 | ␉return (c);␊ |
177 | }␊ |
178 | ␊ |
179 | int printf(const char * fmt, ...)␊ |
180 | {␊ |
181 | va_list ap;␊ |
182 | ␉va_start(ap, fmt);␊ |
183 | ␉␊ |
184 | ␉prf(fmt, ap, putchar, 0);␊ |
185 | ␉␊ |
186 | ␉{␊ |
187 | ␉␉/* Kabyl: BooterLog */␊ |
188 | ␉␉struct putc_info pi;␊ |
189 | ␉␉␊ |
190 | ␉␉if (!msgbuf)␊ |
191 | ␉␉␉return 0;␊ |
192 | ␉␉␊ |
193 | ␉␉if (((cursor - msgbuf) > (BOOTER_LOG_SIZE - SAFE_LOG_SIZE)))␊ |
194 | ␉␉␉return 0;␊ |
195 | ␉␉pi.str = cursor;␊ |
196 | ␉␉pi.last_str = 0;␊ |
197 | ␉␉prf(fmt, ap, sputc, &pi);␊ |
198 | ␉␉cursor += strlen((char *)cursor);␊ |
199 | ␉}␊ |
200 | ␉␊ |
201 | ␉va_end(ap); ␊ |
202 | ␊ |
203 | return 0;␊ |
204 | }␊ |
205 | ␊ |
206 | int verbose(const char * fmt, ...)␊ |
207 | {␊ |
208 | va_list ap;␊ |
209 | ␊ |
210 | ␉va_start(ap, fmt);␊ |
211 | if (gVerboseMode)␊ |
212 | {␊ |
213 | ␉␉prf(fmt, ap, putchar, 0);␊ |
214 | }␊ |
215 | ␉␊ |
216 | ␉{␊ |
217 | ␉␉/* Kabyl: BooterLog */␊ |
218 | ␉␉struct putc_info pi;␊ |
219 | ␉␉␊ |
220 | ␉␉if (!msgbuf)␊ |
221 | ␉␉␉return 0;␊ |
222 | ␉␉␊ |
223 | ␉␉if (((cursor - msgbuf) > (BOOTER_LOG_SIZE - SAFE_LOG_SIZE)))␊ |
224 | ␉␉␉return 0;␊ |
225 | ␉␉pi.str = cursor;␊ |
226 | ␉␉pi.last_str = 0;␊ |
227 | ␉␉prf(fmt, ap, sputc, &pi);␊ |
228 | ␉␉cursor += strlen((char *)cursor);␊ |
229 | ␉}␊ |
230 | ␉␊ |
231 | va_end(ap); ␊ |
232 | ␊ |
233 | return(0);␊ |
234 | }␊ |
235 | ␊ |
236 | int error(const char * fmt, ...)␊ |
237 | {␊ |
238 | va_list ap;␊ |
239 | gErrors = true;␊ |
240 | va_start(ap, fmt);␊ |
241 | ␉␊ |
242 | ␉prf(fmt, ap, putchar, 0);␊ |
243 | ␊ |
244 | ␉va_end(ap);␊ |
245 | return(0);␊ |
246 | }␊ |
247 | ␊ |
248 | void stop(const char * fmt, ...)␊ |
249 | {␊ |
250 | ␉va_list ap;␊ |
251 | ␉␊ |
252 | ␉printf("\n");␊ |
253 | ␉va_start(ap, fmt);␊ |
254 | ␉␊ |
255 | ␉prf(fmt, ap, putchar, 0);␊ |
256 | ␉␊ |
257 | ␉va_end(ap);␊ |
258 | ␉printf("\nThis is a non recoverable error! System HALTED!!!");␊ |
259 | ␉halt();␊ |
260 | ␉while (1);␊ |
261 | }␊ |
262 | ␊ |
263 | /** Print a "Press a key to continue..." message and wait for a key press. */␊ |
264 | void pause(void) ␊ |
265 | {␊ |
266 | printf("Press a key to continue...");␊ |
267 | getc();␊ |
268 | }␊ |
269 | ␊ |
270 | char * newStringWithFormat(const char * fmt, ...)␊ |
271 | {␊ |
272 | va_list ap;␊ |
273 | struct putc_info pi;␊ |
274 | ␉int len;␊ |
275 | ␉char *str = NULL;␊ |
276 | ␉␊ |
277 | va_start(ap, fmt);␊ |
278 | ␉len = prf(fmt, ap, 0, 0);␊ |
279 | ␉if (len > 0)␊ |
280 | ␉{␊ |
281 | ␉␉str = newEmptyStringWithLength(len);␊ |
282 | ␉␉if (str != NULL) ␊ |
283 | ␉␉{␊ |
284 | ␉␉␉pi.last_str = 0;␊ |
285 | ␉␉␉␊ |
286 | ␉␉␉pi.str = str;␊ |
287 | ␉␉␉␊ |
288 | ␉␉␉prf(fmt, ap, sputc, &pi);␊ |
289 | ␉␉␉*pi.str = '\0';␊ |
290 | ␉␉}␊ |
291 | ␉␉␊ |
292 | ␉}␉␊ |
293 | va_end(ap);␉␊ |
294 | ␉␊ |
295 | ␉return str;␊ |
296 | ␉␊ |
297 | } |