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 | * Copyright 1993 NeXT, Inc.␊ |
26 | * All rights reserved.␊ |
27 | */␊ |
28 | ␊ |
29 | #include "bootstruct.h"␊ |
30 | #include "libsaio.h"␊ |
31 | #include "boot.h"␊ |
32 | #include "xml.h"␊ |
33 | ␊ |
34 | extern char *Language;␊ |
35 | extern char *LoadableFamilies;␊ |
36 | ␊ |
37 | bool sysConfigValid;␊ |
38 | ␊ |
39 | /*␊ |
40 | * Compare a string to a key with quoted characters␊ |
41 | */␊ |
42 | static inline int␊ |
43 | keyncmp(const char *str, const char *key, int n)␊ |
44 | {␊ |
45 | int c;␊ |
46 | while (n--) {␊ |
47 | ␉c = *key++;␊ |
48 | ␉if (c == '\\') {␊ |
49 | ␉ switch(c = *key++) {␊ |
50 | ␉ case 'n':␊ |
51 | ␉␉c = '\n';␊ |
52 | ␉␉break;␊ |
53 | ␉ case 'r':␊ |
54 | ␉␉c = '\r';␊ |
55 | ␉␉break;␊ |
56 | ␉ case 't':␊ |
57 | ␉␉c = '\t';␊ |
58 | ␉␉break;␊ |
59 | ␉ default:␊ |
60 | ␉␉break;␊ |
61 | ␉ }␊ |
62 | ␉} else if (c == '\"') {␊ |
63 | ␉ // Premature end of key␊ |
64 | ␉ return 1;␊ |
65 | ␉}␊ |
66 | ␉if (c != *str++) {␊ |
67 | ␉ return 1;␊ |
68 | ␉}␊ |
69 | }␊ |
70 | return 0;␊ |
71 | }␊ |
72 | ␊ |
73 | #if UNUSED␊ |
74 | ␊ |
75 | static void eatThru(char val, const char **table_p)␊ |
76 | {␊ |
77 | ␉register const char *table = *table_p;␊ |
78 | ␉register bool found = false;␊ |
79 | ␊ |
80 | ␉while (*table && !found)␊ |
81 | ␉{␊ |
82 | ␉␉if (*table == '\\') table += 2;␊ |
83 | ␉␉else␊ |
84 | ␉␉{␊ |
85 | ␉␉␉if (*table == val) found = true;␊ |
86 | ␉␉␉table++;␊ |
87 | ␉␉}␊ |
88 | ␉}␊ |
89 | ␉*table_p = table;␊ |
90 | }␊ |
91 | ␊ |
92 | /* Remove key and its associated value from the table. */␊ |
93 | ␊ |
94 | bool␊ |
95 | removeKeyFromTable(const char *key, char *table)␊ |
96 | {␊ |
97 | register int len;␊ |
98 | register char *tab;␊ |
99 | char *buf;␊ |
100 | ␊ |
101 | len = strlen(key);␊ |
102 | tab = (char *)table;␊ |
103 | buf = (char *)malloc(len + 3);␊ |
104 | ␊ |
105 | sprintf(buf, "\"%s\"", key);␊ |
106 | len = strlen(buf);␊ |
107 | ␊ |
108 | while(*tab) {␊ |
109 | if(strncmp(buf, tab, len) == 0) {␊ |
110 | char c;␊ |
111 | ␊ |
112 | while((c = *(tab + len)) != ';') {␊ |
113 | if(c == 0) {␊ |
114 | len = -1;␊ |
115 | goto out;␊ |
116 | }␊ |
117 | len++;␊ |
118 | }␊ |
119 | len++;␊ |
120 | if(*(tab + len) == '\n') len++;␊ |
121 | goto out;␊ |
122 | }␊ |
123 | tab++;␊ |
124 | }␊ |
125 | len = -1;␊ |
126 | out:␊ |
127 | free(buf);␊ |
128 | ␊ |
129 | if(len == -1) return false;␊ |
130 | ␊ |
131 | while((*tab = *(tab + len))) {␊ |
132 | tab++;␊ |
133 | }␊ |
134 | ␊ |
135 | return true;␊ |
136 | }␊ |
137 | ␊ |
138 | char *newStringFromList(char **list, int *size)␊ |
139 | {␊ |
140 | ␉char *begin = *list, *end;␊ |
141 | ␉char *newstr;␊ |
142 | ␉int newsize = *size;␊ |
143 | ␉int bufsize;␊ |
144 | ␊ |
145 | ␉while (*begin && newsize && isspace(*begin)) {␊ |
146 | ␉␉begin++;␊ |
147 | ␉␉newsize--;␊ |
148 | ␉}␊ |
149 | ␉end = begin;␊ |
150 | ␉while (*end && newsize && !isspace(*end)) {␊ |
151 | ␉␉end++;␊ |
152 | ␉␉newsize--;␊ |
153 | ␉}␊ |
154 | ␉if (begin == end) {␊ |
155 | ␉␉return 0;␊ |
156 | ␉}␊ |
157 | ␉bufsize = end - begin + 1;␊ |
158 | ␉newstr = malloc(bufsize);␊ |
159 | ␉strlcpy(newstr, begin, bufsize);␊ |
160 | ␉*list = end;␊ |
161 | ␉*size = newsize;␊ |
162 | ␉return newstr;␊ |
163 | }␊ |
164 | ␊ |
165 | #endif␊ |
166 | ␊ |
167 | /* ␊ |
168 | * compress == compress escaped characters to one character␊ |
169 | */␊ |
170 | int stringLength(const char *table, int compress)␊ |
171 | {␊ |
172 | ␉int ret = 0;␊ |
173 | ␊ |
174 | ␉while (*table) {␊ |
175 | ␉␉if (*table == '\\') {␊ |
176 | ␉␉␉table += 2;␊ |
177 | ␉␉␉ret += 1 + (compress ? 0 : 1);␊ |
178 | ␉␉} else {␊ |
179 | ␉␉␉if (*table == '\"') {␊ |
180 | ␉␉␉␉return ret;␊ |
181 | ␉␉␉}␊ |
182 | ␉␉␉ret++;␊ |
183 | ␉␉␉table++;␊ |
184 | ␉␉}␊ |
185 | ␉}␊ |
186 | ␉return ret;␊ |
187 | }␊ |
188 | ␊ |
189 | ␊ |
190 | bool getValueForConfigTableKey(config_file_t *config, const char *key, const char **val, int *size)␊ |
191 | {␊ |
192 | ␉if (config->dictionary != 0 ) {␊ |
193 | ␉␉// Look up key in XML dictionary␊ |
194 | ␉␉TagPtr value;␊ |
195 | ␉␉value = XMLGetProperty(config->dictionary, key);␊ |
196 | ␉␉if (value != 0) {␊ |
197 | ␉␉␉if (value->type != kTagTypeString) {␊ |
198 | ␉␉␉␉error("Non-string tag '%s' found in config file\n", key);␊ |
199 | ␉␉␉␉return false;␊ |
200 | ␉␉␉}␊ |
201 | ␉␉␉*val = value->string;␊ |
202 | ␉␉␉*size = strlen(value->string);␊ |
203 | ␉␉␉return true;␊ |
204 | ␉␉}␊ |
205 | ␉} else {␊ |
206 | ␊ |
207 | ␉␉// Legacy plist-style table␊ |
208 | ␊ |
209 | ␉}␊ |
210 | ␊ |
211 | ␉return false;␊ |
212 | }␊ |
213 | ␊ |
214 | #if UNUSED␊ |
215 | ␊ |
216 | /*␊ |
217 | * Returns a new malloc'ed string if one is found␊ |
218 | * in the string table matching 'key'. Also translates␊ |
219 | * \n escapes in the string.␊ |
220 | */␊ |
221 | char *newStringForStringTableKey(char *table, char *key, config_file_t *config)␊ |
222 | {␊ |
223 | ␉const char *val;␊ |
224 | ␉char *newstr, *p;␊ |
225 | ␉int size;␊ |
226 | ␊ |
227 | ␉if (getValueForConfigTableKey(config, key, &val, &size)) {␊ |
228 | ␉␉newstr = (char *)malloc(size+1);␊ |
229 | ␉␉for (p = newstr; size; size--, p++, val++) {␊ |
230 | ␉␉␉if ((*p = *val) == '\\') {␊ |
231 | ␉␉␉␉switch (*++val) {␊ |
232 | ␉␉␉␉␉case 'r':␊ |
233 | ␉␉␉␉␉␉*p = '\r';␊ |
234 | ␉␉␉␉␉break;␊ |
235 | ␉␉␉␉␉case 'n':␊ |
236 | ␉␉␉␉␉␉*p = '\n';␊ |
237 | ␉␉␉␉␉break;␊ |
238 | ␉␉␉␉␉case 't':␊ |
239 | ␉␉␉␉␉␉*p = '\t';␊ |
240 | ␉␉␉␉␉break;␊ |
241 | ␉␉␉␉␉default:␊ |
242 | ␉␉␉␉␉␉*p = *val;␊ |
243 | ␉␉␉␉␉break;␊ |
244 | ␉␉␉␉}␊ |
245 | ␉␉␉␉size--;␊ |
246 | ␉␉␉}␊ |
247 | ␉␉}␊ |
248 | ␉␉*p = '\0';␊ |
249 | ␉␉return newstr;␊ |
250 | ␉} else {␊ |
251 | ␉␉return 0;␊ |
252 | ␉}␊ |
253 | }␊ |
254 | ␊ |
255 | #endif␊ |
256 | ␊ |
257 | char *newStringForKey(char *key, config_file_t *config)␊ |
258 | {␊ |
259 | ␉const char *val;␊ |
260 | ␉char *newstr;␊ |
261 | ␉int size;␊ |
262 | ␊ |
263 | ␉if (getValueForKey(key, &val, &size, config) && size) {␊ |
264 | ␉␉newstr = (char *)malloc(size + 1);␊ |
265 | ␉␉strlcpy(newstr, val, size + 1);␊ |
266 | ␉␉return newstr;␊ |
267 | ␉} else {␊ |
268 | ␉␉return 0;␊ |
269 | ␉}␊ |
270 | }␊ |
271 | ␊ |
272 | /* parse a command line␊ |
273 | * in the form: [<argument> ...] [<option>=<value> ...]␊ |
274 | * both <option> and <value> must be either composed of␊ |
275 | * non-whitespace characters, or enclosed in quotes.␊ |
276 | */␊ |
277 | ␊ |
278 | static const char *getToken(const char *line, const char **begin, int *len)␊ |
279 | {␊ |
280 | ␉if (*line == '\"') {␊ |
281 | ␉␉*begin = ++line;␊ |
282 | ␉␉while (*line && *line != '\"') {␊ |
283 | ␉␉␉line++;␊ |
284 | ␉␉}␊ |
285 | ␉␉*len = line++ - *begin;␊ |
286 | ␉} else {␊ |
287 | ␉␉*begin = line;␊ |
288 | ␉␉while (*line && !isspace(*line) && *line != '=') {␊ |
289 | ␉␉␉line++;␊ |
290 | ␉␉}␊ |
291 | ␉␉*len = line - *begin;␊ |
292 | ␉}␊ |
293 | ␉return line;␊ |
294 | }␊ |
295 | ␊ |
296 | bool getValueForBootKey(const char *line, const char *match, const char **matchval, int *len)␊ |
297 | {␊ |
298 | ␉const char *key, *value;␊ |
299 | ␉int key_len, value_len;␊ |
300 | ␉bool retval = false;␊ |
301 | ␊ |
302 | ␉while (*line) {␊ |
303 | ␉␉/* look for keyword or argument */␊ |
304 | ␉␉while (isspace(*line)) {␊ |
305 | ␉␉␉line++;␊ |
306 | ␉␉}␊ |
307 | ␊ |
308 | ␉␉/* now look for '=' or whitespace */␊ |
309 | ␉␉line = getToken(line, &key, &key_len);␊ |
310 | ␉␉/* line now points to '=' or space */␊ |
311 | ␉␉if (*line && !isspace(*line)) {␊ |
312 | ␉␉␉line = getToken(++line, &value, &value_len);␊ |
313 | ␉␉} else {␊ |
314 | ␉␉␉value = line;␊ |
315 | ␉␉␉value_len = 0;␊ |
316 | ␉␉}␊ |
317 | ␉␉if ((strlen(match) == key_len) && strncmp(match, key, key_len) == 0) {␊ |
318 | ␉␉␉// create a new string␊ |
319 | ␉␉␉char* newstr = malloc(value_len + 1);␊ |
320 | ␉␉␉strncpy(newstr, value, value_len);␊ |
321 | ␉␉␉newstr[value_len] = 0;␊ |
322 | ␉␉␊ |
323 | ␉␉␉*matchval = newstr;␊ |
324 | ␉␉␉*len = value_len;␊ |
325 | ␉␉␉retval = true;␊ |
326 | ␉␉␉/* Continue to look for this key; last one wins. */␊ |
327 | ␉␉}␊ |
328 | ␉}␊ |
329 | ␊ |
330 | ␉return retval;␊ |
331 | }␊ |
332 | ␊ |
333 | /* Return NULL if no option has been successfully retrieved, or the string otherwise */␊ |
334 | const char * getStringForKey(const char * key, config_file_t *config)␊ |
335 | {␊ |
336 | ␉static const char* value =0;␊ |
337 | ␉int len=0;␊ |
338 | ␉if(!getValueForKey(key, &value, &len, config)) {␊ |
339 | ␉␉value = 0;␊ |
340 | ␉}␊ |
341 | ␉return value;␊ |
342 | }␊ |
343 | ␊ |
344 | ␊ |
345 | /* Returns TRUE if a value was found, FALSE otherwise.␊ |
346 | * The boolean value of the key is stored in 'val'.␊ |
347 | */␊ |
348 | ␊ |
349 | bool getBoolForKey( const char *key, bool *result_val, config_file_t *config )␊ |
350 | {␊ |
351 | const char *key_val;␊ |
352 | int size;␊ |
353 | ␊ |
354 | ␉if (getValueForKey(key, &key_val, &size, config)) {␊ |
355 | ␉␉if ( (size >= 1) && (key_val[0] == 'Y' || key_val[0] == 'y') ) {␊ |
356 | ␉␉␉*result_val = true;␊ |
357 | ␉␉} else {␊ |
358 | ␉␉␉*result_val = false;␊ |
359 | ␉␉}␊ |
360 | ␉␉return true;␊ |
361 | ␉}␊ |
362 | ␉return false;␊ |
363 | }␊ |
364 | ␊ |
365 | bool getIntForKey( const char *key, int *value, config_file_t *config )␊ |
366 | {␊ |
367 | ␉const char *val;␊ |
368 | ␉int size, sum;␊ |
369 | ␉bool negative = false;␊ |
370 | ␊ |
371 | ␉if (getValueForKey(key, &val, &size, config)) {␊ |
372 | ␉␉if ( size ) {␊ |
373 | ␉␉␉if (*val == '-') {␊ |
374 | ␉␉␉␉negative = true;␊ |
375 | ␉␉␉␉val++;␊ |
376 | ␉␉␉␉size--;␊ |
377 | ␉␉␉}␊ |
378 | ␉␉␉␊ |
379 | ␉␉␉for (sum = 0; size > 0; size--) {␊ |
380 | ␉␉␉␉if (*val < '0' || *val > '9') {␊ |
381 | ␉␉␉␉␉return false;␊ |
382 | ␉␉␉␉}␊ |
383 | ␉␉␉␉␊ |
384 | ␉␉␉␉sum = (sum * 10) + (*val++ - '0');␊ |
385 | ␉␉␉}␊ |
386 | ␉␉␉␊ |
387 | ␉␉␉if (negative) {␊ |
388 | ␉␉␉␉sum = -sum;␊ |
389 | ␉␉␉}␊ |
390 | ␉␉␉*value = sum;␊ |
391 | ␉␉␉return true;␊ |
392 | ␉␉}␊ |
393 | ␉}␊ |
394 | ␉return false;␊ |
395 | }␊ |
396 | ␊ |
397 | /*␊ |
398 | *␊ |
399 | */␊ |
400 | ␊ |
401 | bool getDimensionForKey( const char *key, unsigned int *value, config_file_t *config, unsigned int dimension_max, unsigned int object_size )␊ |
402 | {␊ |
403 | ␉const char *val;␊ |
404 | ␊ |
405 | ␉int size = 0;␊ |
406 | ␉int sum = 0;␊ |
407 | ␊ |
408 | ␉bool negative = false;␊ |
409 | ␉bool percentage = false;␊ |
410 | ␊ |
411 | ␉if (getValueForKey(key, &val, &size, config)) {␊ |
412 | ␉␉if ( size ) {␊ |
413 | ␉␉␉if (*val == '-') {␊ |
414 | ␉␉␉␉negative = true;␊ |
415 | ␉␉␉␉val++;␊ |
416 | ␉␉␉␉size--;␊ |
417 | ␉␉␉}␊ |
418 | ␊ |
419 | ␉␉␉if (val[size-1] == '%') {␊ |
420 | ␉␉␉␉percentage = true;␊ |
421 | ␉␉␉␉size--;␊ |
422 | ␉␉␉}␊ |
423 | ␊ |
424 | ␉␉␉// convert string to integer␊ |
425 | ␉␉␉for (sum = 0; size > 0; size--) {␊ |
426 | ␉␉␉␉if (*val < '0' || *val > '9') {␊ |
427 | ␉␉␉␉␉return false;␊ |
428 | ␉␉␉␉}␊ |
429 | ␊ |
430 | ␉␉␉␉sum = (sum * 10) + (*val++ - '0');␊ |
431 | ␉␉␉}␊ |
432 | ␊ |
433 | ␉␉␉if (percentage) {␊ |
434 | ␉␉␉␉sum = ( dimension_max * sum ) / 100;␊ |
435 | ␉␉␉}␊ |
436 | ␊ |
437 | ␉␉␉// calculate offset from opposite origin␊ |
438 | ␉␉␉if (negative) {␊ |
439 | ␉␉␉␉sum = ( ( dimension_max - object_size ) - sum );␊ |
440 | ␉␉␉}␊ |
441 | ␊ |
442 | ␉␉} else {␊ |
443 | ␊ |
444 | ␉␉␉// null value calculate center␊ |
445 | ␉␉␉sum = ( dimension_max - object_size ) / 2;␊ |
446 | ␊ |
447 | ␉␉}␊ |
448 | ␊ |
449 | ␉␉*value = (uint16_t) sum;␊ |
450 | ␉␉return true;␊ |
451 | ␉}␊ |
452 | ␊ |
453 | ␉// key not found␊ |
454 | ␉return false;␊ |
455 | }␊ |
456 | ␊ |
457 | /*␊ |
458 | *␉get color value from plist format #RRGGBB␊ |
459 | */␊ |
460 | ␊ |
461 | bool getColorForKey( const char *key, unsigned int *value, config_file_t *config )␊ |
462 | {␊ |
463 | ␉const char *val;␊ |
464 | ␉int size;␊ |
465 | ␊ |
466 | ␉if (getValueForKey(key, &val, &size, config))␊ |
467 | ␉{␊ |
468 | ␉␉if (*val == '#') {␊ |
469 | ␉␉␉val++;␊ |
470 | ␉␉␉*value = strtol(val, NULL, 16);␊ |
471 | ␉␉␉return true;␊ |
472 | ␉␉}␊ |
473 | ␉}␊ |
474 | ␉return false;␊ |
475 | }␊ |
476 | ␊ |
477 | bool getValueForKey( const char *key, const char **val, int *size, config_file_t *config )␊ |
478 | {␊ |
479 | ␉const char *overrideVal;␊ |
480 | ␉int overrideSize;␊ |
481 | ␉bool override, ret;␊ |
482 | ␊ |
483 | ␉if (getValueForBootKey(bootArgs->CommandLine, key, val, size)) {␊ |
484 | ␉␉return true;␊ |
485 | ␉}␊ |
486 | ␊ |
487 | ␉ret = getValueForConfigTableKey(config, key, val, size);␊ |
488 | ␊ |
489 | ␉// Try to find alternate keys in bootInfo->chameleonConfig (if config can be overriden)␊ |
490 | ␉// and prefer its values with the exceptions for␊ |
491 | ␉// "Kernel"="mach_kernel" and "Kernel Flags"="".␊ |
492 | ␊ |
493 | ␉if (config->canOverride) {␊ |
494 | ␉␉if (getValueForConfigTableKey(&bootInfo->chameleonConfig, key, &overrideVal, &overrideSize)) {␊ |
495 | ␉␉␉override = true;␊ |
496 | ␊ |
497 | ␉␉␉// NOTE: Values are defined by apple as being in com.apple.Boot.plist␊ |
498 | ␉␉␉// kHelperRootUUIDKey, kKernelArchKey, kMKextCacheKey, kKernelCacheKey, kKernelNameKey, kKernelFlagsKey␊ |
499 | ␉␉␉if (ret && (strcmp(key, kKernelNameKey) == 0) && (overrideSize == 0)) {␊ |
500 | ␉␉␉␉override = false;␊ |
501 | ␉␉␉}␊ |
502 | ␊ |
503 | ␉␉␉if (ret && (strcmp(key, kKernelFlagsKey) == 0) && (overrideSize == 0)) {␊ |
504 | ␉␉␉␉override = false;␊ |
505 | ␉␉␉}␊ |
506 | ␊ |
507 | ␉␉␉if (override) {␊ |
508 | ␉␉␉␉*val = overrideVal;␊ |
509 | ␉␉␉␉*size = overrideSize;␊ |
510 | ␉␉␉␉return true;␊ |
511 | ␉␉␉}␊ |
512 | ␉␉}␊ |
513 | ␉}␊ |
514 | ␉return ret;␊ |
515 | }␊ |
516 | ␊ |
517 | ␊ |
518 | #if UNUSED␊ |
519 | void␊ |
520 | printSystemConfig(char *p1)␊ |
521 | {␊ |
522 | ␉char *p2 = p1, tmp;␊ |
523 | ␊ |
524 | ␉while (*p1 != '\0') {␊ |
525 | ␉␉while (*p2 != '\0' && *p2 != '\n') {␊ |
526 | ␉␉␉p2++;␊ |
527 | ␉␉}␊ |
528 | ␉tmp = *p2;␊ |
529 | ␉*p2 = '\0';␊ |
530 | ␉printf("%s\n", p1);␊ |
531 | ␉*p2 = tmp;␊ |
532 | ␉if (tmp == '\0') {␊ |
533 | ␉␉break;␊ |
534 | ␉}␊ |
535 | ␉p1 = ++p2;␊ |
536 | }␊ |
537 | }␊ |
538 | #endif␊ |
539 | ␊ |
540 | //==========================================================================␊ |
541 | // ParseXMLFile␊ |
542 | // Modifies the input buffer.␊ |
543 | // Expects to see one dictionary in the XML file.␊ |
544 | // Puts the first dictionary it finds in the␊ |
545 | // tag pointer and returns 0, or returns -1 if not found␊ |
546 | // (and does not modify dict pointer).␊ |
547 | // Prints an error message if there is a parsing error.␊ |
548 | //␊ |
549 | int ParseXMLFile( char * buffer, TagPtr * dict )␊ |
550 | {␊ |
551 | ␉long length, pos;␊ |
552 | ␉TagPtr tag;␊ |
553 | ␉pos = 0;␊ |
554 | ␉char *configBuffer;␊ |
555 | ␊ |
556 | ␉configBuffer = malloc(strlen(buffer)+1);␊ |
557 | ␉strcpy(configBuffer, buffer);␊ |
558 | ␊ |
559 | ␉while (1) {␊ |
560 | ␉␉length = XMLParseNextTag(configBuffer + pos, &tag);␊ |
561 | ␉␉␉if (length == -1) {␊ |
562 | ␉␉␉␉break;␊ |
563 | ␉␉␉}␊ |
564 | ␊ |
565 | ␉␉pos += length;␊ |
566 | ␊ |
567 | ␉␉if (tag == 0) {␊ |
568 | ␉␉␉continue;␊ |
569 | ␉␉}␊ |
570 | ␉␉if (tag->type == kTagTypeDict) {␊ |
571 | ␉␉␉break;␊ |
572 | ␉␉}␊ |
573 | ␊ |
574 | ␉␉XMLFreeTag(tag);␊ |
575 | ␉}␊ |
576 | ␉free(configBuffer);␊ |
577 | ␉if (length < 0) {␊ |
578 | ␉␉error ("Error parsing plist file\n");␊ |
579 | ␉␉return -1;␊ |
580 | ␉}␊ |
581 | ␉*dict = tag;␊ |
582 | ␉return 0;␊ |
583 | }␊ |
584 | ␊ |
585 | /* loadConfigFile␊ |
586 | *␊ |
587 | * Returns 0 - successful.␊ |
588 | *␉␉ -1 - unsuccesful.␊ |
589 | */␊ |
590 | int loadConfigFile (const char *configFile, config_file_t *config)␊ |
591 | {␊ |
592 | ␉int fd, count;␊ |
593 | ␊ |
594 | ␉if ((fd = open_bvdev("bt(0,0)", configFile, 0)) < 0) {␊ |
595 | ␉␉return -1;␊ |
596 | ␉}␊ |
597 | ␉// read file␊ |
598 | ␉count = read(fd, config->plist, IO_CONFIG_DATA_SIZE);␊ |
599 | ␉close(fd);␊ |
600 | ␊ |
601 | ␉// build xml dictionary␊ |
602 | ␉ParseXMLFile(config->plist, &config->dictionary);␊ |
603 | ␉return 0;␊ |
604 | }␊ |
605 | ␊ |
606 | ␊ |
607 | /* loadSystemConfig␊ |
608 | *␊ |
609 | * Returns 0 - successful.␊ |
610 | *␉␉ -1 - unsuccesful.␊ |
611 | */␊ |
612 | int loadSystemConfig(config_file_t *config)␊ |
613 | {␊ |
614 | ␉char *dirspec[] = {␊ |
615 | ␉␉"/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",␊ |
616 | ␉␉"/OS X Install Data/com.apple.Boot.plist",␊ |
617 | ␉};␊ |
618 | ␊ |
619 | ␉int i, fd, count, ret=-1;␊ |
620 | ␊ |
621 | ␉for(i = 0; i< sizeof(dirspec)/sizeof(dirspec[0]); i++) {␊ |
622 | ␉␉if ((fd = open(dirspec[i], 0)) >= 0) {␊ |
623 | ␉␉␉// read file␊ |
624 | ␉␉␉count = read(fd, config->plist, IO_CONFIG_DATA_SIZE);␊ |
625 | ␉␉␉close(fd);␊ |
626 | ␊ |
627 | ␉␉␉// build xml dictionary␊ |
628 | ␉␉␉ParseXMLFile(config->plist, &config->dictionary);␊ |
629 | ␉␉␉sysConfigValid = true;␉␊ |
630 | ␉␉␉ret=0;␊ |
631 | ␊ |
632 | ␉␉␉break;␊ |
633 | ␉␉}␊ |
634 | ␉}␊ |
635 | ␊ |
636 | ␉if(ret == -1) ret = loadHelperConfig(config);␊ |
637 | ␊ |
638 | ␉// Always enable canOverride flag (for SystemConfig)␊ |
639 | ␉config->canOverride = true;␊ |
640 | ␊ |
641 | ␉return ret;␊ |
642 | }␊ |
643 | ␊ |
644 | /* loadChameleonConfig␊ |
645 | *␊ |
646 | * Returns 0 - successful.␊ |
647 | *␉␉ -1 - unsuccesful.␊ |
648 | */␊ |
649 | int loadChameleonConfig(config_file_t *config)␊ |
650 | {␊ |
651 | ␉char *dirspec[] = {␊ |
652 | ␉␉"rd(0,0)/Extra/org.chameleon.Boot.plist",␊ |
653 | ␉␉"/Extra/org.chameleon.Boot.plist",␊ |
654 | ␉␉"bt(0,0)/Extra/org.chameleon.Boot.plist",␊ |
655 | ␉␉␊ |
656 | ␉␉"rd(0,0)/Extra/com.apple.Boot.plist", /* DEPRECIATED */␊ |
657 | ␉␉"/Extra/com.apple.Boot.plist", /* DEPRECIATED */␊ |
658 | ␉␉"bt(0,0)/Extra/com.apple.Boot.plist", /* DEPRECIATED */␊ |
659 | ␉};␊ |
660 | ␊ |
661 | ␉int i, fd, count, ret=-1;␊ |
662 | ␊ |
663 | ␉for(i = 0; i< sizeof(dirspec)/sizeof(dirspec[0]); i++) {␊ |
664 | ␉␉if ((fd = open(dirspec[i], 0)) >= 0) {␊ |
665 | ␉␉␉// Check for depreciated file names and annoy the user about it.␊ |
666 | ␉␉␉if(strstr(dirspec[i], "com.apple.Boot.plist")) {␊ |
667 | ␉␉␉␉printf("%s is depreciated.\n", dirspec[i]);␊ |
668 | ␉␉␉␉dirspec[i][strlen(dirspec[i]) - strlen("com.apple.Boot.plist")] = 0;␊ |
669 | ␉␉␉␉printf("Please use the file %sorg.chameleon.Boot.plist instead.\n", dirspec[i]);␊ |
670 | ␉␉␉␉pause();␊ |
671 | ␉␉␉}␊ |
672 | ␉␉␉// read file␊ |
673 | ␉␉␉count = read(fd, config->plist, IO_CONFIG_DATA_SIZE);␊ |
674 | ␉␉␉close(fd);␊ |
675 | ␉␉␉␊ |
676 | ␉␉␉// build xml dictionary␊ |
677 | ␉␉␉ParseXMLFile(config->plist, &config->dictionary);␊ |
678 | ␉␉␉sysConfigValid = true;␉␊ |
679 | ␉␉␉ret=0;␊ |
680 | ␉␉␉break;␊ |
681 | ␉␉}␊ |
682 | ␉}␊ |
683 | ␉return ret;␊ |
684 | }␊ |
685 | ␊ |
686 | /* loadHelperConfig␊ |
687 | *␊ |
688 | * Returns 0 - successful.␊ |
689 | *␉␉ -1 - unsuccesful.␊ |
690 | */␊ |
691 | int loadHelperConfig(config_file_t *config)␊ |
692 | {␊ |
693 | ␉char *dirspec[] = {␊ |
694 | ␉␉"/com.apple.boot.P/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",␊ |
695 | ␉␉"/com.apple.boot.R/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",␊ |
696 | ␉␉"/com.apple.boot.S/Library/Preferences/SystemConfiguration/com.apple.Boot.plist"␊ |
697 | ␉};␊ |
698 | ␊ |
699 | ␉int i, fd, count, ret=-1;␊ |
700 | ␊ |
701 | ␉for(i = 0; i< sizeof(dirspec)/sizeof(dirspec[0]); i++) {␊ |
702 | ␉␉if ((fd = open(dirspec[i], 0)) >= 0) {␊ |
703 | ␉␉␉// read file␊ |
704 | ␉␉␉count = read(fd, config->plist, IO_CONFIG_DATA_SIZE);␊ |
705 | ␉␉␉close(fd);␊ |
706 | ␉␉␉␊ |
707 | ␉␉␉// build xml dictionary␊ |
708 | ␉␉␉ParseXMLFile(config->plist, &config->dictionary);␊ |
709 | ␉␉␉sysConfigValid = true;␉␊ |
710 | ␉␉␉ret=0;␊ |
711 | ␉␉␉break;␊ |
712 | ␉␉}␊ |
713 | ␉}␊ |
714 | ␉return ret;␊ |
715 | }␊ |
716 | ␊ |
717 | char * newString(const char * oldString)␊ |
718 | {␊ |
719 | ␉if ( oldString ) {␊ |
720 | ␉␉return strcpy(malloc(strlen(oldString)+1), oldString);␊ |
721 | ␉} else {␊ |
722 | ␉␉return NULL;␊ |
723 | ␉}␊ |
724 | }␊ |
725 | ␊ |
726 | /*␊ |
727 | * Extracts the next argument from the command line, double quotes are allowed here.␊ |
728 | */␊ |
729 | char * getNextArg(char ** argPtr, char * val)␊ |
730 | {␊ |
731 | ␉char * ptr = *argPtr;␊ |
732 | ␉const char * strStart;␊ |
733 | ␉int len = 0;␊ |
734 | ␉bool isQuoted = false;␊ |
735 | ␊ |
736 | ␉*val = '\0';␊ |
737 | ␊ |
738 | ␉// Scan for the next non-whitespace character.␊ |
739 | ␉while ( *ptr && (*ptr == ' ' || *ptr == '=') ) {␊ |
740 | ␉␉ptr++;␊ |
741 | ␉}␊ |
742 | ␊ |
743 | ␉strStart = ptr;␊ |
744 | ␊ |
745 | ␉// Skip the leading double quote character.␊ |
746 | ␉if (*ptr == '\"') {␊ |
747 | ␉␉isQuoted = true;␊ |
748 | ␉␉ptr++;␊ |
749 | ␉␉strStart++;␊ |
750 | ␉}␊ |
751 | ␊ |
752 | ␉// Scan for the argument terminator character.␊ |
753 | ␉// This can be either a NULL character - in case we reach the end of the string,␊ |
754 | ␉// a double quote in case of quoted argument,␊ |
755 | ␉// or a whitespace character (' ' or '=') for non-quoted argument.␊ |
756 | ␉while (*ptr && !( (isQuoted && (*ptr == '\"')) ||␊ |
757 | (!isQuoted && (*ptr == ' ' || *ptr == '=')) )␊ |
758 | ) {␊ |
759 | ␉␉ptr++;␊ |
760 | ␉}␊ |
761 | ␊ |
762 | ␉len = ptr - strStart;␊ |
763 | ␊ |
764 | ␉// Skip the closing double quote character and adjust␊ |
765 | ␉// the starting pointer for the next getNextArg call.␊ |
766 | ␉if (*ptr && isQuoted && *ptr == '\"') {␊ |
767 | ␉␉ptr++;␊ |
768 | ␉}␊ |
769 | ␊ |
770 | ␉// Copy the extracted argument to val.␊ |
771 | ␉strncat(val, strStart, len);␊ |
772 | ␊ |
773 | ␉// Set command line pointer.␊ |
774 | ␉*argPtr = ptr;␊ |
775 | ␊ |
776 | ␉return ptr;␊ |
777 | }␊ |
778 | |