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 | ␉if ( c == '\t' )␊ |
107 | ␉{␊ |
108 | ␉␉for (c = 0; c < 8; c++) putc(' ');␊ |
109 | ␉␉return;␊ |
110 | ␉}␊ |
111 | ␉␊ |
112 | ␉if ( c == '\n' )␊ |
113 | {␊ |
114 | ␉␉putc('\r');␊ |
115 | }␊ |
116 | ␉␊ |
117 | ␉putc(c);␊ |
118 | }␊ |
119 | ␊ |
120 | int getc()␊ |
121 | {␉␊ |
122 | int c = bgetc();␉␊ |
123 | ␉␊ |
124 | if ((c & 0xff) == 0) ␉␉␊ |
125 | return c;␊ |
126 | else ␉␊ |
127 | ␉␉return (c & 0xff); ␊ |
128 | }␊ |
129 | ␊ |
130 | // Read and echo a character from console. This doesn't echo backspace␊ |
131 | // since that screws up higher level handling␊ |
132 | ␊ |
133 | int getchar()␊ |
134 | {␊ |
135 | ␉register int c = getc();␊ |
136 | ␉␊ |
137 | ␉if ( c == '\r' ) c = '\n';␊ |
138 | ␉␊ |
139 | ␉if ( c >= ' ' && c < 0x7f) putchar(c);␊ |
140 | ␉␊ |
141 | ␉return (c);␊ |
142 | }␊ |
143 | ␊ |
144 | int␊ |
145 | reallyVPrint(const char *format, va_list ap, int flag)␊ |
146 | {␊ |
147 | if (flag & PRINT) prf(format, ap, putchar);␊ |
148 | ␉␊ |
149 | if (flag & LOG)␊ |
150 | ␉{␊ |
151 | ␉␉/* Kabyl: BooterLog */␉␉␊ |
152 | ␉␉prf(format, ap, debug_putc);␊ |
153 | ␉}␊ |
154 | return 0;␊ |
155 | }␊ |
156 | ␊ |
157 | int localVPrintf(const char *format, va_list ap, int flag)␊ |
158 | {␊ |
159 | /**/␊ |
160 | ␊ |
161 | reallyVPrint(format, ap, flag);␊ |
162 | return 0;␊ |
163 | }␊ |
164 | ␊ |
165 | int printf(const char * fmt, ...)␊ |
166 | {␊ |
167 | va_list ap;␊ |
168 | ␉va_start(ap, fmt);␊ |
169 | ␉␊ |
170 | ␉localVPrintf(fmt, ap, LOG | PRINT);␊ |
171 | ␉␊ |
172 | ␉va_end(ap); ␊ |
173 | ␊ |
174 | return 0;␊ |
175 | }␊ |
176 | ␊ |
177 | void msglog(const char * fmt, ...)␊ |
178 | {␊ |
179 | va_list ap;␊ |
180 | ␉va_start(ap, fmt);␉␊ |
181 | ␉␊ |
182 | localVPrintf(fmt, ap, LOG);␊ |
183 | ␉␊ |
184 | ␉va_end(ap); ␊ |
185 | }␊ |
186 | ␊ |
187 | int verbose(const char * fmt, ...)␊ |
188 | {␊ |
189 | va_list ap;␊ |
190 | int flag = 0;␊ |
191 | ␉va_start(ap, fmt);␊ |
192 | if (get_env(envgVerboseMode))␊ |
193 | {␊ |
194 | ␉␉flag = PRINT;␊ |
195 | }␊ |
196 | ␉␊ |
197 | localVPrintf(fmt, ap, LOG | flag);␊ |
198 | ␉␊ |
199 | va_end(ap); ␊ |
200 | ␊ |
201 | return(0);␊ |
202 | }␊ |
203 | ␊ |
204 | int error(const char * fmt, ...)␊ |
205 | {␊ |
206 | va_list ap;␊ |
207 | ␉int len;␊ |
208 | ␉char *str = NULL;␊ |
209 | ␉␊ |
210 | va_start(ap, fmt);␊ |
211 | ␊ |
212 | localVPrintf(fmt, ap, 0);␊ |
213 | ␊ |
214 | ␊ |
215 | ␉len = prf(fmt, ap, 0);␊ |
216 | ␉if (len > 0)␊ |
217 | ␉{␊ |
218 | ␉␉str = newEmptyStringWithLength(len);␊ |
219 | ␉␉if (str != NULL) ␊ |
220 | ␉␉{␉␉␉␊ |
221 | ␉␉␉vsnprintf(str,len,fmt,ap);␊ |
222 | ␉␉}␊ |
223 | ␉␉␊ |
224 | ␉}␉␊ |
225 | va_end(ap);␊ |
226 | ␉␊ |
227 | ␉set_env_copy(envConsoleErr, str, len);␊ |
228 | ␉free(str);␊ |
229 | ␉␊ |
230 | return(0);␊ |
231 | }␊ |
232 | ␊ |
233 | void stop(const char * fmt, ...)␊ |
234 | {␊ |
235 | ␉va_list ap;␊ |
236 | ␉␊ |
237 | ␉printf("\n");␊ |
238 | ␉va_start(ap, fmt);␊ |
239 | ␉␊ |
240 | localVPrintf(fmt, ap, PRINT);␊ |
241 | ␉␊ |
242 | ␉va_end(ap);␊ |
243 | ␉printf("\nThis is a non recoverable error! System HALTED!!!");␊ |
244 | ␉halt();␊ |
245 | ␉while (1);␊ |
246 | }␊ |
247 | ␊ |
248 | /** Print a "Press a key to continue..." message and wait for a key press. */␊ |
249 | void pause(void) ␊ |
250 | {␊ |
251 | printf("Press a key to continue...");␊ |
252 | getc();␊ |
253 | }␊ |
254 | ␊ |
255 | char * newStringWithFormat(const char * fmt, ...)␊ |
256 | {␊ |
257 | va_list ap;␊ |
258 | ␉int len;␊ |
259 | ␉char *str = NULL;␊ |
260 | ␉␊ |
261 | va_start(ap, fmt);␊ |
262 | ␉len = prf(fmt, ap, 0);␊ |
263 | ␉if (len > 0)␊ |
264 | ␉{␊ |
265 | ␉␉str = newEmptyStringWithLength(len);␊ |
266 | ␉␉if (str != NULL) ␊ |
267 | ␉␉{␉␉␉␊ |
268 | ␉␉␉vsnprintf(str,len,fmt,ap);␊ |
269 | ␉␉}␊ |
270 | ␉␉␊ |
271 | ␉}␉␊ |
272 | va_end(ap);␉␊ |
273 | ␉␊ |
274 | ␉return str;␊ |
275 | ␉␊ |
276 | } |