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