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 | #if UNUSED␊ |
40 | ␊ |
41 | /*␊ |
42 | * Compare a string to a key with quoted characters␊ |
43 | */␊ |
44 | static inline int␊ |
45 | keyncmp(const char *str, const char *key, int n)␊ |
46 | {␊ |
47 | int c;␊ |
48 | while (n--) {␊ |
49 | ␉c = *key++;␊ |
50 | ␉if (c == '\\') {␊ |
51 | ␉ switch(c = *key++) {␊ |
52 | ␉ case 'n':␊ |
53 | ␉␉c = '\n';␊ |
54 | ␉␉break;␊ |
55 | ␉ case 'r':␊ |
56 | ␉␉c = '\r';␊ |
57 | ␉␉break;␊ |
58 | ␉ case 't':␊ |
59 | ␉␉c = '\t';␊ |
60 | ␉␉break;␊ |
61 | ␉ default:␊ |
62 | ␉␉break;␊ |
63 | ␉ }␊ |
64 | ␉} else if (c == '\"') {␊ |
65 | ␉ // Premature end of key␊ |
66 | ␉ return 1;␊ |
67 | ␉}␊ |
68 | ␉if (c != *str++) {␊ |
69 | ␉ return 1;␊ |
70 | ␉}␊ |
71 | }␊ |
72 | return 0;␊ |
73 | }␊ |
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 | ␉␉TagPtr 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 *␊ |
258 | newStringForKey(char *key, config_file_t *config)␊ |
259 | {␊ |
260 | ␉const char *val;␊ |
261 | ␉char *newstr;␊ |
262 | ␉int size;␊ |
263 | ␊ |
264 | ␉if (getValueForKey(key, &val, &size, config) && size) {␊ |
265 | ␉␉newstr = (char *)malloc(size + 1);␊ |
266 | ␉␉strlcpy(newstr, val, size + 1);␊ |
267 | ␉␉return newstr;␊ |
268 | ␉} else {␊ |
269 | ␉␉return 0;␊ |
270 | ␉}␊ |
271 | }␊ |
272 | ␊ |
273 | /* parse a command line␊ |
274 | * in the form: [<argument> ...] [<option>=<value> ...]␊ |
275 | * both <option> and <value> must be either composed of␊ |
276 | * non-whitespace characters, or enclosed in quotes.␊ |
277 | */␊ |
278 | ␊ |
279 | static const char *getToken(const char *line, const char **begin, int *len)␊ |
280 | {␊ |
281 | ␉if (*line == '\"') {␊ |
282 | ␉␉*begin = ++line;␊ |
283 | ␉␉while (*line && *line != '\"') {␊ |
284 | ␉␉␉line++;␊ |
285 | ␉␉}␊ |
286 | ␉␉*len = line++ - *begin;␊ |
287 | ␉} else {␊ |
288 | ␉␉*begin = line;␊ |
289 | ␉␉while (*line && !isspace(*line) && *line != '=') {␊ |
290 | ␉␉␉line++;␊ |
291 | ␉␉}␊ |
292 | ␉␉*len = line - *begin;␊ |
293 | ␉}␊ |
294 | ␉return line;␊ |
295 | }␊ |
296 | ␊ |
297 | bool getValueForBootKey(const char *line, const char *match, const char **matchval, int *len)␊ |
298 | {␊ |
299 | ␉const char *key, *value;␊ |
300 | ␉int key_len, value_len;␊ |
301 | ␉bool retval = false;␊ |
302 | ␊ |
303 | ␉while (*line) {␊ |
304 | ␉␉/* look for keyword or argument */␊ |
305 | ␉␉while (isspace(*line)) {␊ |
306 | ␉␉␉line++;␊ |
307 | ␉␉}␊ |
308 | ␊ |
309 | ␉␉/* now look for '=' or whitespace */␊ |
310 | ␉␉line = getToken(line, &key, &key_len);␊ |
311 | ␉␉/* line now points to '=' or space */␊ |
312 | ␉␉if (*line && !isspace(*line)) {␊ |
313 | ␉␉␉line = getToken(++line, &value, &value_len);␊ |
314 | ␉␉} else {␊ |
315 | ␉␉␉value = line;␊ |
316 | ␉␉␉value_len = 0;␊ |
317 | ␉␉}␊ |
318 | ␉␉if ((strlen(match) == key_len) && strncmp(match, key, key_len) == 0) {␊ |
319 | ␉␉␉// create a new string␊ |
320 | ␉␉␉char* newstr = malloc(value_len + 1);␊ |
321 | ␉␉␉strncpy(newstr, value, value_len);␊ |
322 | ␉␉␉newstr[value_len] = 0;␊ |
323 | ␉␉␊ |
324 | ␉␉␉*matchval = newstr;␊ |
325 | ␉␉␉*len = value_len;␊ |
326 | ␉␉␉retval = true;␊ |
327 | ␉␉␉/* Continue to look for this key; last one wins. */␊ |
328 | ␉␉}␊ |
329 | ␉}␊ |
330 | ␊ |
331 | ␉return retval;␊ |
332 | }␊ |
333 | ␊ |
334 | /* Return NULL if no option has been successfully retrieved, or the string otherwise */␊ |
335 | const char *getStringForKey(const char *key, config_file_t *config)␊ |
336 | {␊ |
337 | ␉static const char *value = 0;␊ |
338 | ␉int len = 0;␊ |
339 | ␉if (!getValueForKey(key, &value, &len, config))␊ |
340 | ␉{␊ |
341 | ␉␉value = 0;␊ |
342 | ␉}␊ |
343 | ␉return value;␊ |
344 | }␊ |
345 | ␊ |
346 | ␊ |
347 | /* Returns TRUE if a value was found, FALSE otherwise.␊ |
348 | * The boolean value of the key is stored in 'val'.␊ |
349 | */␊ |
350 | ␊ |
351 | bool getBoolForKey(const char *key, bool *result_val, config_file_t *config)␊ |
352 | {␊ |
353 | const char *key_val;␊ |
354 | int size;␊ |
355 | ␊ |
356 | ␉if (getValueForKey(key, &key_val, &size, config))␊ |
357 | ␉{␊ |
358 | ␉␉if ((size >= 1) && (key_val[0] == 'Y' || key_val[0] == 'y'))␊ |
359 | ␉␉{␊ |
360 | ␉␉␉*result_val = true;␊ |
361 | ␉␉}␊ |
362 | ␉␉else␊ |
363 | ␉␉{␊ |
364 | ␉␉␉*result_val = false;␊ |
365 | ␉␉}␊ |
366 | ␉␉return true;␊ |
367 | ␉}␊ |
368 | ␉return false;␊ |
369 | }␊ |
370 | ␊ |
371 | bool getIntForKey(const char *key, int *value, config_file_t *config)␊ |
372 | {␊ |
373 | ␉const char *val;␊ |
374 | ␉int size, sum;␊ |
375 | ␉bool negative = false;␊ |
376 | ␊ |
377 | ␉if (getValueForKey(key, &val, &size, config)) {␊ |
378 | ␉␉if (size) {␊ |
379 | ␉␉␉if (*val == '-') {␊ |
380 | ␉␉␉␉negative = true;␊ |
381 | ␉␉␉␉val++;␊ |
382 | ␉␉␉␉size--;␊ |
383 | ␉␉␉}␊ |
384 | ␉␉␉␊ |
385 | ␉␉␉for (sum = 0; size > 0; size--) {␊ |
386 | ␉␉␉␉if (*val < '0' || *val > '9') {␊ |
387 | ␉␉␉␉␉return false;␊ |
388 | ␉␉␉␉}␊ |
389 | ␉␉␉␉␊ |
390 | ␉␉␉␉sum = (sum * 10) + (*val++ - '0');␊ |
391 | ␉␉␉}␊ |
392 | ␉␉␉␊ |
393 | ␉␉␉if (negative) {␊ |
394 | ␉␉␉␉sum = -sum;␊ |
395 | ␉␉␉}␊ |
396 | ␉␉␉*value = sum;␊ |
397 | ␉␉␉return true;␊ |
398 | ␉␉}␊ |
399 | ␉}␊ |
400 | ␉return false;␊ |
401 | }␊ |
402 | ␊ |
403 | /*␊ |
404 | *␊ |
405 | */␊ |
406 | ␊ |
407 | bool getDimensionForKey( const char *key, unsigned int *value, config_file_t *config, unsigned int dimension_max, unsigned int object_size )␊ |
408 | {␊ |
409 | ␉const char *val;␊ |
410 | ␊ |
411 | ␉int size = 0;␊ |
412 | ␉int sum = 0;␊ |
413 | ␊ |
414 | ␉bool negative = false;␊ |
415 | ␉bool percentage = false;␊ |
416 | ␊ |
417 | ␉if (getValueForKey(key, &val, &size, config)) {␊ |
418 | ␉␉if ( size ) {␊ |
419 | ␉␉␉if (*val == '-') {␊ |
420 | ␉␉␉␉negative = true;␊ |
421 | ␉␉␉␉val++;␊ |
422 | ␉␉␉␉size--;␊ |
423 | ␉␉␉}␊ |
424 | ␊ |
425 | ␉␉␉if (val[size-1] == '%') {␊ |
426 | ␉␉␉␉percentage = true;␊ |
427 | ␉␉␉␉size--;␊ |
428 | ␉␉␉}␊ |
429 | ␊ |
430 | ␉␉␉// convert string to integer␊ |
431 | ␉␉␉for (sum = 0; size > 0; size--) {␊ |
432 | ␉␉␉␉if (*val < '0' || *val > '9') {␊ |
433 | ␉␉␉␉␉return false;␊ |
434 | ␉␉␉␉}␊ |
435 | ␊ |
436 | ␉␉␉␉sum = (sum * 10) + (*val++ - '0');␊ |
437 | ␉␉␉}␊ |
438 | ␊ |
439 | ␉␉␉if (percentage) {␊ |
440 | ␉␉␉␉sum = ( dimension_max * sum ) / 100;␊ |
441 | ␉␉␉}␊ |
442 | ␊ |
443 | ␉␉␉// calculate offset from opposite origin␊ |
444 | ␉␉␉if (negative) {␊ |
445 | ␉␉␉␉sum = ( ( dimension_max - object_size ) - sum );␊ |
446 | ␉␉␉}␊ |
447 | ␊ |
448 | ␉␉} else {␊ |
449 | ␊ |
450 | ␉␉␉// null value calculate center␊ |
451 | ␉␉␉sum = ( dimension_max - object_size ) / 2;␊ |
452 | ␊ |
453 | ␉␉}␊ |
454 | ␊ |
455 | ␉␉*value = (uint16_t) sum;␊ |
456 | ␉␉return true;␊ |
457 | ␉}␊ |
458 | ␊ |
459 | ␉// key not found␊ |
460 | ␉return false;␊ |
461 | }␊ |
462 | ␊ |
463 | /*␊ |
464 | *␉get color value from plist format #RRGGBB␊ |
465 | */␊ |
466 | ␊ |
467 | bool getColorForKey( const char *key, unsigned int *value, config_file_t *config )␊ |
468 | {␊ |
469 | ␉const char *val;␊ |
470 | ␉int size;␊ |
471 | ␊ |
472 | ␉if (getValueForKey(key, &val, &size, config))␊ |
473 | ␉{␊ |
474 | ␉␉if (*val == '#') {␊ |
475 | ␉␉␉val++;␊ |
476 | ␉␉␉*value = strtol(val, NULL, 16);␊ |
477 | ␉␉␉return true;␊ |
478 | ␉␉}␊ |
479 | ␉}␊ |
480 | ␉return false;␊ |
481 | }␊ |
482 | ␊ |
483 | bool getValueForKey( const char *key, const char **val, int *size, config_file_t *config )␊ |
484 | {␊ |
485 | ␉const char *overrideVal;␊ |
486 | ␉int overrideSize;␊ |
487 | ␉bool override, ret;␊ |
488 | ␊ |
489 | ␉if (getValueForBootKey(bootArgs->CommandLine, key, val, size)) {␊ |
490 | ␉␉return true;␊ |
491 | ␉}␊ |
492 | ␊ |
493 | ␉ret = getValueForConfigTableKey(config, key, val, size);␊ |
494 | ␊ |
495 | ␉// Try to find alternate keys in bootInfo->chameleonConfig (if config can be overriden)␊ |
496 | ␉// and prefer its values with the exceptions for␊ |
497 | ␉// "Kernel"="mach_kernel" and "Kernel Flags"="".␊ |
498 | ␊ |
499 | ␉if (config->canOverride) {␊ |
500 | ␉␉if (getValueForConfigTableKey(&bootInfo->chameleonConfig, key, &overrideVal, &overrideSize)) {␊ |
501 | ␉␉␉override = true;␊ |
502 | ␊ |
503 | ␉␉␉// NOTE: Values are defined by apple as being in com.apple.Boot.plist␊ |
504 | ␉␉␉// kHelperRootUUIDKey, kKernelArchKey, kMKextCacheKey, kKernelCacheKey, kKernelNameKey, kKernelFlagsKey␊ |
505 | ␉␉␉if (ret && (strcmp(key, kKernelNameKey) == 0) && (overrideSize == 0)) {␊ |
506 | ␉␉␉␉override = false;␊ |
507 | ␉␉␉}␊ |
508 | ␊ |
509 | ␉␉␉if (ret && (strcmp(key, kKernelFlagsKey) == 0) && (overrideSize == 0)) {␊ |
510 | ␉␉␉␉override = false;␊ |
511 | ␉␉␉}␊ |
512 | ␊ |
513 | ␉␉␉if (override) {␊ |
514 | ␉␉␉␉*val = overrideVal;␊ |
515 | ␉␉␉␉*size = overrideSize;␊ |
516 | ␉␉␉␉ret = true;␊ |
517 | ␉␉␉}␊ |
518 | ␉␉}␊ |
519 | ␉}␊ |
520 | ␉return ret;␊ |
521 | }␊ |
522 | ␊ |
523 | ␊ |
524 | #if UNUSED␊ |
525 | void␊ |
526 | printSystemConfig(char *p1)␊ |
527 | {␊ |
528 | ␉char *p2 = p1, tmp;␊ |
529 | ␊ |
530 | ␉while (*p1 != '\0') {␊ |
531 | ␉␉while (*p2 != '\0' && *p2 != '\n') {␊ |
532 | ␉␉␉p2++;␊ |
533 | ␉␉}␊ |
534 | ␉tmp = *p2;␊ |
535 | ␉*p2 = '\0';␊ |
536 | ␉printf("%s\n", p1);␊ |
537 | ␉*p2 = tmp;␊ |
538 | ␉if (tmp == '\0') {␊ |
539 | ␉␉break;␊ |
540 | ␉}␊ |
541 | ␉p1 = ++p2;␊ |
542 | }␊ |
543 | }␊ |
544 | #endif␊ |
545 | ␊ |
546 | //==========================================================================␊ |
547 | // ParseXMLFile␊ |
548 | // Modifies the input buffer.␊ |
549 | // Expects to see one dictionary in the XML file.␊ |
550 | // Puts the first dictionary it finds in the␊ |
551 | // tag pointer and returns 0, or returns -1 if not found␊ |
552 | // (and does not modify dict pointer).␊ |
553 | // Prints an error message if there is a parsing error.␊ |
554 | //␊ |
555 | int ParseXMLFile( char *buffer, TagPtr *dict )␊ |
556 | {␊ |
557 | ␉long length, pos;␊ |
558 | ␉TagPtr tag;␊ |
559 | ␉pos = 0;␊ |
560 | ␉char *configBuffer;␊ |
561 | ␊ |
562 | ␉configBuffer = malloc(strlen(buffer)+1);␊ |
563 | ␉strcpy(configBuffer, buffer);␊ |
564 | ␊ |
565 | ␉while (1) {␊ |
566 | ␉␉length = XMLParseNextTag(configBuffer + pos, &tag);␊ |
567 | ␉␉␉if (length == -1) {␊ |
568 | ␉␉␉␉break;␊ |
569 | ␉␉␉}␊ |
570 | ␊ |
571 | ␉␉pos += length;␊ |
572 | ␊ |
573 | ␉␉if (tag == 0) {␊ |
574 | ␉␉␉continue;␊ |
575 | ␉␉}␊ |
576 | ␉␉if (tag->type == kTagTypeDict) {␊ |
577 | ␉␉␉break;␊ |
578 | ␉␉}␊ |
579 | ␊ |
580 | ␉␉XMLFreeTag(tag);␊ |
581 | ␉}␊ |
582 | ␉free(configBuffer);␊ |
583 | ␉if (length < 0) {␊ |
584 | ␉␉error ("Error parsing plist file\n");␊ |
585 | ␉␉return -1;␊ |
586 | ␉}␊ |
587 | ␉*dict = tag;␊ |
588 | ␉return 0;␊ |
589 | }␊ |
590 | ␊ |
591 | /* loadConfigFile␊ |
592 | *␊ |
593 | * Returns 0 - successful.␊ |
594 | *␉␉ -1 - unsuccesful.␊ |
595 | */␊ |
596 | int loadConfigFile (const char *configFile, config_file_t *config)␊ |
597 | {␊ |
598 | ␉int fd, count;␊ |
599 | ␊ |
600 | ␉if ((fd = open_bvdev("bt(0,0)", configFile, 0)) < 0)␊ |
601 | ␉{␊ |
602 | ␉␉return -1;␊ |
603 | ␉}␊ |
604 | ␉// read file␊ |
605 | ␉count = read(fd, config->plist, IO_CONFIG_DATA_SIZE);␊ |
606 | ␉close(fd);␊ |
607 | ␊ |
608 | ␉// build xml dictionary␊ |
609 | ␉ParseXMLFile(config->plist, &config->dictionary);␊ |
610 | ␉return 0;␊ |
611 | }␊ |
612 | ␊ |
613 | ␊ |
614 | /* loadSystemConfig␊ |
615 | *␊ |
616 | * Returns 0 - successful.␊ |
617 | *␉␉ -1 - unsuccesful.␊ |
618 | */␊ |
619 | int loadSystemConfig(config_file_t *config)␊ |
620 | {␊ |
621 | ␉char *dirspec[] = {␊ |
622 | ␉␉"/Mac OS X Install Data/com.apple.Boot.plist",␉␉␉␉// OS X Installer (Lion 10.7)␊ |
623 | ␉␉"/OS X Install Data/com.apple.Boot.plist",␉␉␉␉// OS X Installer (10.8+)␊ |
624 | ␉␉//"/.IABootFiles/com.apple.Boot.plist",␉␉␉␉␉// OS X Installer␊ |
625 | ␉␉"/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",␉// com.apple.Boot.plist␊ |
626 | ␉␉"/com.apple.recovery.boot/com.apple.Boot.plist"␉␉␉␉// OS X Recovery␊ |
627 | ␉};␊ |
628 | ␊ |
629 | ␉int i, fd, count, ret=-1;␊ |
630 | ␊ |
631 | ␉for(i = 0; i< sizeof(dirspec)/sizeof(dirspec[0]); i++)␊ |
632 | ␉{␊ |
633 | ␉␉if ((fd = open(dirspec[i], 0)) >= 0)␊ |
634 | ␉␉{␊ |
635 | ␉␉␉// read file␊ |
636 | ␉␉␉count = read(fd, config->plist, IO_CONFIG_DATA_SIZE);␊ |
637 | ␉␉␉close(fd);␊ |
638 | ␊ |
639 | ␉␉␉// build xml dictionary␊ |
640 | ␉␉␉ParseXMLFile(config->plist, &config->dictionary);␊ |
641 | ␉␉␉sysConfigValid = true;␉␊ |
642 | ␉␉␉ret=0;␊ |
643 | ␊ |
644 | ␉␉␉break;␊ |
645 | ␉␉}␊ |
646 | ␉}␊ |
647 | ␊ |
648 | ␉if(ret == -1) ret = loadHelperConfig(config);␊ |
649 | ␊ |
650 | ␉// Always enable canOverride flag (for SystemConfig)␊ |
651 | ␉config->canOverride = true;␊ |
652 | ␊ |
653 | ␉return ret;␊ |
654 | }␊ |
655 | ␊ |
656 | /* loadChameleonConfig␊ |
657 | *␊ |
658 | * Returns 0 - successful.␊ |
659 | *␉␉ -1 - unsuccesful.␊ |
660 | */␊ |
661 | int loadChameleonConfig(config_file_t *config, BVRef chain)␊ |
662 | {␊ |
663 | ␉char *dirspec[] = {␊ |
664 | ␉␉"/Extra/org.chameleon.Boot.plist",␊ |
665 | ␉␉"/Extra/com.apple.Boot.plist", /* DEPRECIATED */␊ |
666 | ␉};␊ |
667 | ␊ |
668 | ␉int i;␊ |
669 | ␊ |
670 | ␉for(i = 0; i< sizeof(dirspec)/sizeof(dirspec[0]); i++)␊ |
671 | ␉{␊ |
672 | ␉␉if ( loadChameleonConfigForDevice(config, "rd(0,0)", dirspec[i]) == 0 )␊ |
673 | ␉␉{␊ |
674 | ␉␉␉return 0;␊ |
675 | ␉␉}␊ |
676 | ␊ |
677 | ␉␉if ( loadChameleonConfigForDevice(config, "", dirspec[i]) == 0 )␊ |
678 | ␉␉{␊ |
679 | ␉␉␉return 0;␊ |
680 | ␉␉}␊ |
681 | ␊ |
682 | ␉␉if ( loadChameleonConfigForDevice(config, "bt(0,0)", dirspec[i]) == 0 )␊ |
683 | ␉␉{␊ |
684 | ␉␉␉return 0;␊ |
685 | ␉␉}␊ |
686 | ␉␉BVRef bvr;␊ |
687 | ␉␉for ( bvr = chain; bvr; bvr = bvr->next ) /* C99 Error */␊ |
688 | ␉␉{␊ |
689 | ␉␉␉char device[256];␊ |
690 | ␉␉␉getDeviceDescription(bvr, device);␊ |
691 | ␊ |
692 | ␉␉␉if ( loadChameleonConfigForDevice(config, device, dirspec[i]) == 0 )␊ |
693 | ␉␉␉{␊ |
694 | ␉␉␉␉return 0;␊ |
695 | ␉␉␉}␊ |
696 | ␉␉}␊ |
697 | ␉}␊ |
698 | ␉return -1;␊ |
699 | }␊ |
700 | ␊ |
701 | /* loadChameleonConfigForDevice␊ |
702 | *␊ |
703 | * Returns 0 - successful.␊ |
704 | *␉␉ -1 - unsuccesful.␊ |
705 | */␊ |
706 | int loadChameleonConfigForDevice(config_file_t *config, const char *device, const char *path)␊ |
707 | {␊ |
708 | ␉char full_path[1024];␊ |
709 | ␉int fd;␊ |
710 | ␊ |
711 | ␉snprintf(full_path, sizeof(full_path), "%s%s", device, path);␊ |
712 | ␊ |
713 | ␉if ((fd = open(full_path, 0)) >= 0)␊ |
714 | ␉{␊ |
715 | ␉␉// Check for depreciated file names and annoy the user about it.␊ |
716 | ␉␉if(strstr(full_path, "com.apple.Boot.plist")) {␊ |
717 | ␉␉␉error("%s is depreciated.\n", full_path);␊ |
718 | ␉␉␉full_path[strlen(full_path) - strlen("com.apple.Boot.plist")] = 0;␊ |
719 | ␉␉␉error("Please use the file %sorg.chameleon.Boot.plist instead.\n", full_path);␊ |
720 | ␉␉␉pause("");␊ |
721 | ␉␉}␊ |
722 | ␉␉// read file␊ |
723 | ␉␉read(fd, config->plist, IO_CONFIG_DATA_SIZE);␊ |
724 | ␉␉close(fd);␊ |
725 | ␊ |
726 | ␉␉// build xml dictionary␊ |
727 | ␉␉ParseXMLFile(config->plist, &config->dictionary);␊ |
728 | ␉␉sysConfigValid = true;␊ |
729 | ␉␉return 0;␊ |
730 | ␉}␊ |
731 | ␉return -1;␊ |
732 | }␊ |
733 | ␊ |
734 | /* loadHelperConfig␊ |
735 | *␊ |
736 | * Returns 0 - successful.␊ |
737 | *␉␉ -1 - unsuccesful.␊ |
738 | */␊ |
739 | int loadHelperConfig(config_file_t *config)␊ |
740 | {␊ |
741 | ␉char *dirspec[] = {␊ |
742 | ␉␉"/com.apple.boot.P/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",␊ |
743 | ␉␉"/com.apple.boot.R/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",␊ |
744 | ␉␉"/com.apple.boot.S/Library/Preferences/SystemConfiguration/com.apple.Boot.plist"␊ |
745 | ␉};␊ |
746 | ␊ |
747 | ␉int i, fd, count, ret=-1;␊ |
748 | ␊ |
749 | ␉for(i = 0; i< sizeof(dirspec)/sizeof(dirspec[0]); i++)␊ |
750 | ␉{␊ |
751 | ␉␉if ((fd = open(dirspec[i], 0)) >= 0)␊ |
752 | ␉␉{␊ |
753 | ␉␉␉// read file␊ |
754 | ␉␉␉count = read(fd, config->plist, IO_CONFIG_DATA_SIZE);␊ |
755 | ␉␉␉close(fd);␊ |
756 | ␉␉␉␊ |
757 | ␉␉␉// build xml dictionary␊ |
758 | ␉␉␉ParseXMLFile(config->plist, &config->dictionary);␊ |
759 | ␉␉␉sysConfigValid = true;␉␊ |
760 | ␉␉␉ret=0;␊ |
761 | ␉␉␉break;␊ |
762 | ␉␉}␊ |
763 | ␉}␊ |
764 | ␉return ret;␊ |
765 | }␊ |
766 | ␊ |
767 | char * newString(const char * oldString)␊ |
768 | {␊ |
769 | ␉if ( oldString )␊ |
770 | ␉{␊ |
771 | ␉␉return strcpy(malloc(strlen(oldString)+1), oldString);␊ |
772 | ␉}␊ |
773 | ␉else␊ |
774 | ␉{␊ |
775 | ␉␉return NULL;␊ |
776 | ␉}␊ |
777 | }␊ |
778 | ␊ |
779 | /*␊ |
780 | * Extracts the next argument from the command line, double quotes are allowed here.␊ |
781 | */␊ |
782 | char * getNextArg(char ** argPtr, char * val)␊ |
783 | {␊ |
784 | ␉char * ptr = *argPtr;␊ |
785 | ␉const char * strStart;␊ |
786 | ␉int len = 0;␊ |
787 | ␉bool isQuoted = false;␊ |
788 | ␊ |
789 | ␉*val = '\0';␊ |
790 | ␊ |
791 | ␉// Scan for the next non-whitespace character.␊ |
792 | ␉while ( *ptr && (*ptr == ' ' || *ptr == '=') )␊ |
793 | ␉{␊ |
794 | ␉␉ptr++;␊ |
795 | ␉}␊ |
796 | ␊ |
797 | ␉strStart = ptr;␊ |
798 | ␊ |
799 | ␉// Skip the leading double quote character.␊ |
800 | ␉if (*ptr == '\"')␊ |
801 | ␉{␊ |
802 | ␉␉isQuoted = true;␊ |
803 | ␉␉ptr++;␊ |
804 | ␉␉strStart++;␊ |
805 | ␉}␊ |
806 | ␊ |
807 | ␉// Scan for the argument terminator character.␊ |
808 | ␉// This can be either a NULL character - in case we reach the end of the string,␊ |
809 | ␉// a double quote in case of quoted argument,␊ |
810 | ␉// or a whitespace character (' ' or '=') for non-quoted argument.␊ |
811 | ␉while (*ptr && !( (isQuoted && (*ptr == '\"')) ||␊ |
812 | (!isQuoted && (*ptr == ' ' || *ptr == '=')) )␊ |
813 | ) {␊ |
814 | ␉␉ptr++;␊ |
815 | ␉}␊ |
816 | ␊ |
817 | ␉len = ptr - strStart;␊ |
818 | ␊ |
819 | ␉// Skip the closing double quote character and adjust␊ |
820 | ␉// the starting pointer for the next getNextArg call.␊ |
821 | ␉if (*ptr && isQuoted && *ptr == '\"') {␊ |
822 | ␉␉ptr++;␊ |
823 | ␉}␊ |
824 | ␊ |
825 | ␉// Copy the extracted argument to val.␊ |
826 | ␉strncat(val, strStart, len);␊ |
827 | ␊ |
828 | ␉// Set command line pointer.␊ |
829 | ␉*argPtr = ptr;␊ |
830 | ␊ |
831 | ␉return ptr;␊ |
832 | }␊ |
833 | |