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