Root/
Source at commit 2605 created 9 years 4 months ago. By ifabio, Temp disabled feature introduced in commit 2562 (Added ability to auto-select last booted partition as the boot volume), cause AllocateKernelMemory error. | |
---|---|
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 | ␉␉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 | ␉␉value = 0;␊ |
341 | ␉}␊ |
342 | ␉return value;␊ |
343 | }␊ |
344 | ␊ |
345 | ␊ |
346 | /* Returns TRUE if a value was found, FALSE otherwise.␊ |
347 | * The boolean value of the key is stored in 'val'.␊ |
348 | */␊ |
349 | ␊ |
350 | bool getBoolForKey( const char *key, bool *result_val, config_file_t *config )␊ |
351 | {␊ |
352 | const char *key_val;␊ |
353 | int size;␊ |
354 | ␊ |
355 | ␉if (getValueForKey(key, &key_val, &size, config)) {␊ |
356 | ␉␉if ( (size >= 1) && (key_val[0] == 'Y' || key_val[0] == 'y') ) {␊ |
357 | ␉␉␉*result_val = true;␊ |
358 | ␉␉} else {␊ |
359 | ␉␉␉*result_val = false;␊ |
360 | ␉␉}␊ |
361 | ␉␉return true;␊ |
362 | ␉}␊ |
363 | ␉return false;␊ |
364 | }␊ |
365 | ␊ |
366 | bool getIntForKey( const char *key, int *value, config_file_t *config )␊ |
367 | {␊ |
368 | ␉const char *val;␊ |
369 | ␉int size, sum;␊ |
370 | ␉bool negative = false;␊ |
371 | ␊ |
372 | ␉if (getValueForKey(key, &val, &size, config)) {␊ |
373 | ␉␉if ( size ) {␊ |
374 | ␉␉␉if (*val == '-') {␊ |
375 | ␉␉␉␉negative = true;␊ |
376 | ␉␉␉␉val++;␊ |
377 | ␉␉␉␉size--;␊ |
378 | ␉␉␉}␊ |
379 | ␉␉␉␊ |
380 | ␉␉␉for (sum = 0; size > 0; size--) {␊ |
381 | ␉␉␉␉if (*val < '0' || *val > '9') {␊ |
382 | ␉␉␉␉␉return false;␊ |
383 | ␉␉␉␉}␊ |
384 | ␉␉␉␉␊ |
385 | ␉␉␉␉sum = (sum * 10) + (*val++ - '0');␊ |
386 | ␉␉␉}␊ |
387 | ␉␉␉␊ |
388 | ␉␉␉if (negative) {␊ |
389 | ␉␉␉␉sum = -sum;␊ |
390 | ␉␉␉}␊ |
391 | ␉␉␉*value = sum;␊ |
392 | ␉␉␉return true;␊ |
393 | ␉␉}␊ |
394 | ␉}␊ |
395 | ␉return false;␊ |
396 | }␊ |
397 | ␊ |
398 | /*␊ |
399 | *␊ |
400 | */␊ |
401 | ␊ |
402 | bool getDimensionForKey( const char *key, unsigned int *value, config_file_t *config, unsigned int dimension_max, unsigned int object_size )␊ |
403 | {␊ |
404 | ␉const char *val;␊ |
405 | ␊ |
406 | ␉int size = 0;␊ |
407 | ␉int sum = 0;␊ |
408 | ␊ |
409 | ␉bool negative = false;␊ |
410 | ␉bool percentage = false;␊ |
411 | ␊ |
412 | ␉if (getValueForKey(key, &val, &size, config)) {␊ |
413 | ␉␉if ( size ) {␊ |
414 | ␉␉␉if (*val == '-') {␊ |
415 | ␉␉␉␉negative = true;␊ |
416 | ␉␉␉␉val++;␊ |
417 | ␉␉␉␉size--;␊ |
418 | ␉␉␉}␊ |
419 | ␊ |
420 | ␉␉␉if (val[size-1] == '%') {␊ |
421 | ␉␉␉␉percentage = true;␊ |
422 | ␉␉␉␉size--;␊ |
423 | ␉␉␉}␊ |
424 | ␊ |
425 | ␉␉␉// convert string to integer␊ |
426 | ␉␉␉for (sum = 0; size > 0; size--) {␊ |
427 | ␉␉␉␉if (*val < '0' || *val > '9') {␊ |
428 | ␉␉␉␉␉return false;␊ |
429 | ␉␉␉␉}␊ |
430 | ␊ |
431 | ␉␉␉␉sum = (sum * 10) + (*val++ - '0');␊ |
432 | ␉␉␉}␊ |
433 | ␊ |
434 | ␉␉␉if (percentage) {␊ |
435 | ␉␉␉␉sum = ( dimension_max * sum ) / 100;␊ |
436 | ␉␉␉}␊ |
437 | ␊ |
438 | ␉␉␉// calculate offset from opposite origin␊ |
439 | ␉␉␉if (negative) {␊ |
440 | ␉␉␉␉sum = ( ( dimension_max - object_size ) - sum );␊ |
441 | ␉␉␉}␊ |
442 | ␊ |
443 | ␉␉} else {␊ |
444 | ␊ |
445 | ␉␉␉// null value calculate center␊ |
446 | ␉␉␉sum = ( dimension_max - object_size ) / 2;␊ |
447 | ␊ |
448 | ␉␉}␊ |
449 | ␊ |
450 | ␉␉*value = (uint16_t) sum;␊ |
451 | ␉␉return true;␊ |
452 | ␉}␊ |
453 | ␊ |
454 | ␉// key not found␊ |
455 | ␉return false;␊ |
456 | }␊ |
457 | ␊ |
458 | /*␊ |
459 | *␉get color value from plist format #RRGGBB␊ |
460 | */␊ |
461 | ␊ |
462 | bool getColorForKey( const char *key, unsigned int *value, config_file_t *config )␊ |
463 | {␊ |
464 | ␉const char *val;␊ |
465 | ␉int size;␊ |
466 | ␊ |
467 | ␉if (getValueForKey(key, &val, &size, config))␊ |
468 | ␉{␊ |
469 | ␉␉if (*val == '#') {␊ |
470 | ␉␉␉val++;␊ |
471 | ␉␉␉*value = strtol(val, NULL, 16);␊ |
472 | ␉␉␉return true;␊ |
473 | ␉␉}␊ |
474 | ␉}␊ |
475 | ␉return false;␊ |
476 | }␊ |
477 | ␊ |
478 | bool getValueForKey( const char *key, const char **val, int *size, config_file_t *config )␊ |
479 | {␊ |
480 | ␉const char *overrideVal;␊ |
481 | ␉int overrideSize;␊ |
482 | ␉bool override, ret;␊ |
483 | ␊ |
484 | ␉if (getValueForBootKey(bootArgs->CommandLine, key, val, size)) {␊ |
485 | ␉␉return true;␊ |
486 | ␉}␊ |
487 | ␊ |
488 | ␉ret = getValueForConfigTableKey(config, key, val, size);␊ |
489 | ␊ |
490 | ␉// Try to find alternate keys in bootInfo->chameleonConfig (if config can be overriden)␊ |
491 | ␉// and prefer its values with the exceptions for␊ |
492 | ␉// "Kernel"="mach_kernel" and "Kernel Flags"="".␊ |
493 | ␊ |
494 | ␉if (config->canOverride) {␊ |
495 | ␉␉if (getValueForConfigTableKey(&bootInfo->chameleonConfig, key, &overrideVal, &overrideSize)) {␊ |
496 | ␉␉␉override = true;␊ |
497 | ␊ |
498 | ␉␉␉// NOTE: Values are defined by apple as being in com.apple.Boot.plist␊ |
499 | ␉␉␉// kHelperRootUUIDKey, kKernelArchKey, kMKextCacheKey, kKernelCacheKey, kKernelNameKey, kKernelFlagsKey␊ |
500 | ␉␉␉if (ret && (strcmp(key, kKernelNameKey) == 0) && (overrideSize == 0)) {␊ |
501 | ␉␉␉␉override = false;␊ |
502 | ␉␉␉}␊ |
503 | ␊ |
504 | ␉␉␉if (ret && (strcmp(key, kKernelFlagsKey) == 0) && (overrideSize == 0)) {␊ |
505 | ␉␉␉␉override = false;␊ |
506 | ␉␉␉}␊ |
507 | ␊ |
508 | ␉␉␉if (override) {␊ |
509 | ␉␉␉␉*val = overrideVal;␊ |
510 | ␉␉␉␉*size = overrideSize;␊ |
511 | ␉␉␉␉return true;␊ |
512 | ␉␉␉}␊ |
513 | ␉␉}␊ |
514 | ␉}␊ |
515 | ␉return ret;␊ |
516 | }␊ |
517 | ␊ |
518 | ␊ |
519 | #if UNUSED␊ |
520 | void␊ |
521 | printSystemConfig(char *p1)␊ |
522 | {␊ |
523 | ␉char *p2 = p1, tmp;␊ |
524 | ␊ |
525 | ␉while (*p1 != '\0') {␊ |
526 | ␉␉while (*p2 != '\0' && *p2 != '\n') {␊ |
527 | ␉␉␉p2++;␊ |
528 | ␉␉}␊ |
529 | ␉tmp = *p2;␊ |
530 | ␉*p2 = '\0';␊ |
531 | ␉printf("%s\n", p1);␊ |
532 | ␉*p2 = tmp;␊ |
533 | ␉if (tmp == '\0') {␊ |
534 | ␉␉break;␊ |
535 | ␉}␊ |
536 | ␉p1 = ++p2;␊ |
537 | }␊ |
538 | }␊ |
539 | #endif␊ |
540 | ␊ |
541 | //==========================================================================␊ |
542 | // ParseXMLFile␊ |
543 | // Modifies the input buffer.␊ |
544 | // Expects to see one dictionary in the XML file.␊ |
545 | // Puts the first dictionary it finds in the␊ |
546 | // tag pointer and returns 0, or returns -1 if not found␊ |
547 | // (and does not modify dict pointer).␊ |
548 | // Prints an error message if there is a parsing error.␊ |
549 | //␊ |
550 | int ParseXMLFile( char * buffer, TagPtr * dict )␊ |
551 | {␊ |
552 | ␉long length, pos;␊ |
553 | ␉TagPtr tag;␊ |
554 | ␉pos = 0;␊ |
555 | ␉char *configBuffer;␊ |
556 | ␊ |
557 | ␉configBuffer = malloc(strlen(buffer)+1);␊ |
558 | ␉strcpy(configBuffer, buffer);␊ |
559 | ␊ |
560 | ␉while (1) {␊ |
561 | ␉␉length = XMLParseNextTag(configBuffer + pos, &tag);␊ |
562 | ␉␉␉if (length == -1) {␊ |
563 | ␉␉␉␉break;␊ |
564 | ␉␉␉}␊ |
565 | ␊ |
566 | ␉␉pos += length;␊ |
567 | ␊ |
568 | ␉␉if (tag == 0) {␊ |
569 | ␉␉␉continue;␊ |
570 | ␉␉}␊ |
571 | ␉␉if (tag->type == kTagTypeDict) {␊ |
572 | ␉␉␉break;␊ |
573 | ␉␉}␊ |
574 | ␊ |
575 | ␉␉XMLFreeTag(tag);␊ |
576 | ␉}␊ |
577 | ␉free(configBuffer);␊ |
578 | ␉if (length < 0) {␊ |
579 | ␉␉error ("Error parsing plist file\n");␊ |
580 | ␉␉return -1;␊ |
581 | ␉}␊ |
582 | ␉*dict = tag;␊ |
583 | ␉return 0;␊ |
584 | }␊ |
585 | ␊ |
586 | /* loadConfigFile␊ |
587 | *␊ |
588 | * Returns 0 - successful.␊ |
589 | *␉␉ -1 - unsuccesful.␊ |
590 | */␊ |
591 | int loadConfigFile (const char *configFile, config_file_t *config)␊ |
592 | {␊ |
593 | ␉int fd, count;␊ |
594 | ␊ |
595 | ␉if ((fd = open_bvdev("bt(0,0)", configFile, 0)) < 0) {␊ |
596 | ␉␉return -1;␊ |
597 | ␉}␊ |
598 | ␉// read file␊ |
599 | ␉count = read(fd, config->plist, IO_CONFIG_DATA_SIZE);␊ |
600 | ␉close(fd);␊ |
601 | ␊ |
602 | ␉// build xml dictionary␊ |
603 | ␉ParseXMLFile(config->plist, &config->dictionary);␊ |
604 | ␉return 0;␊ |
605 | }␊ |
606 | ␊ |
607 | ␊ |
608 | /* loadSystemConfig␊ |
609 | *␊ |
610 | * Returns 0 - successful.␊ |
611 | *␉␉ -1 - unsuccesful.␊ |
612 | */␊ |
613 | int loadSystemConfig(config_file_t *config)␊ |
614 | {␊ |
615 | ␉char *dirspec[] = {␊ |
616 | ␉␉"/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",␊ |
617 | ␉␉"/OS X Install Data/com.apple.Boot.plist",␊ |
618 | ␉};␊ |
619 | ␊ |
620 | ␉int i, fd, count, ret=-1;␊ |
621 | ␊ |
622 | ␉for(i = 0; i< sizeof(dirspec)/sizeof(dirspec[0]); i++) {␊ |
623 | ␉␉if ((fd = open(dirspec[i], 0)) >= 0) {␊ |
624 | ␉␉␉// read file␊ |
625 | ␉␉␉count = read(fd, config->plist, IO_CONFIG_DATA_SIZE);␊ |
626 | ␉␉␉close(fd);␊ |
627 | ␊ |
628 | ␉␉␉// build xml dictionary␊ |
629 | ␉␉␉ParseXMLFile(config->plist, &config->dictionary);␊ |
630 | ␉␉␉sysConfigValid = true;␉␊ |
631 | ␉␉␉ret=0;␊ |
632 | ␊ |
633 | ␉␉␉break;␊ |
634 | ␉␉}␊ |
635 | ␉}␊ |
636 | ␊ |
637 | ␉if(ret == -1) ret = loadHelperConfig(config);␊ |
638 | ␊ |
639 | ␉// Always enable canOverride flag (for SystemConfig)␊ |
640 | ␉config->canOverride = true;␊ |
641 | ␊ |
642 | ␉return ret;␊ |
643 | }␊ |
644 | ␊ |
645 | /* loadChameleonConfig␊ |
646 | *␊ |
647 | * Returns 0 - successful.␊ |
648 | *␉␉ -1 - unsuccesful.␊ |
649 | */␊ |
650 | int loadChameleonConfig(config_file_t *config, BVRef chain)␊ |
651 | {␊ |
652 | ␉char *dirspec[] = {␊ |
653 | ␉␉"/Extra/org.chameleon.Boot.plist",␊ |
654 | ␉␉"/Extra/com.apple.Boot.plist", /* DEPRECIATED */␊ |
655 | ␉};␊ |
656 | ␊ |
657 | ␉int i;␊ |
658 | ␊ |
659 | ␉for(i = 0; i< sizeof(dirspec)/sizeof(dirspec[0]); i++) {␊ |
660 | ␉␉if ( loadChameleonConfigForDevice(config, "rd(0,0)", dirspec[i]) == 0 ) {␊ |
661 | ␉␉␉return 0;␊ |
662 | ␉␉}␊ |
663 | ␉␉if ( loadChameleonConfigForDevice(config, "", dirspec[i]) == 0 ) {␊ |
664 | ␉␉␉return 0;␊ |
665 | ␉␉}␊ |
666 | ␉␉if ( loadChameleonConfigForDevice(config, "bt(0,0)", dirspec[i]) == 0 ) {␊ |
667 | ␉␉␉return 0;␊ |
668 | ␉␉}␊ |
669 | ␉␉BVRef bvr;␊ |
670 | ␉␉for ( bvr = chain; bvr; bvr = bvr->next ) { /* C99 Error */␊ |
671 | ␉␉␉char device[256];␊ |
672 | ␉␉␉getDeviceDescription(bvr, device);␊ |
673 | ␊ |
674 | ␉␉␉if ( loadChameleonConfigForDevice(config, device, dirspec[i]) == 0 ) {␊ |
675 | ␉␉␉␉return 0;␊ |
676 | ␉␉␉}␊ |
677 | ␉␉}␊ |
678 | ␉}␊ |
679 | ␉return -1;␊ |
680 | }␊ |
681 | ␊ |
682 | /* loadChameleonConfigForDevice␊ |
683 | *␊ |
684 | * Returns 0 - successful.␊ |
685 | *␉␉ -1 - unsuccesful.␊ |
686 | */␊ |
687 | int loadChameleonConfigForDevice(config_file_t *config, const char *device, const char *path)␊ |
688 | {␊ |
689 | ␉char full_path[1024];␊ |
690 | ␉int fd;␊ |
691 | ␊ |
692 | ␉snprintf(full_path, sizeof(full_path), "%s%s", device, path);␊ |
693 | ␊ |
694 | ␉if ((fd = open(full_path, 0)) >= 0) {␊ |
695 | ␉␉// Check for depreciated file names and annoy the user about it.␊ |
696 | ␉␉if(strstr(full_path, "com.apple.Boot.plist")) {␊ |
697 | ␉␉␉printf("%s is depreciated.\n", full_path);␊ |
698 | ␉␉␉full_path[strlen(full_path) - strlen("com.apple.Boot.plist")] = 0;␊ |
699 | ␉␉␉printf("Please use the file %sorg.chameleon.Boot.plist instead.\n", full_path);␊ |
700 | ␉␉␉pause();␊ |
701 | ␉␉}␊ |
702 | ␉␉// read file␊ |
703 | ␉␉read(fd, config->plist, IO_CONFIG_DATA_SIZE);␊ |
704 | ␉␉close(fd);␊ |
705 | ␊ |
706 | ␉␉// build xml dictionary␊ |
707 | ␉␉ParseXMLFile(config->plist, &config->dictionary);␊ |
708 | ␉␉sysConfigValid = true;␊ |
709 | ␉␉return 0;␊ |
710 | ␉}␊ |
711 | ␉return -1;␊ |
712 | }␊ |
713 | ␊ |
714 | /* loadHelperConfig␊ |
715 | *␊ |
716 | * Returns 0 - successful.␊ |
717 | *␉␉ -1 - unsuccesful.␊ |
718 | */␊ |
719 | int loadHelperConfig(config_file_t *config)␊ |
720 | {␊ |
721 | ␉char *dirspec[] = {␊ |
722 | ␉␉"/com.apple.boot.P/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",␊ |
723 | ␉␉"/com.apple.boot.R/Library/Preferences/SystemConfiguration/com.apple.Boot.plist",␊ |
724 | ␉␉"/com.apple.boot.S/Library/Preferences/SystemConfiguration/com.apple.Boot.plist"␊ |
725 | ␉};␊ |
726 | ␊ |
727 | ␉int i, fd, count, ret=-1;␊ |
728 | ␊ |
729 | ␉for(i = 0; i< sizeof(dirspec)/sizeof(dirspec[0]); i++) {␊ |
730 | ␉␉if ((fd = open(dirspec[i], 0)) >= 0) {␊ |
731 | ␉␉␉// read file␊ |
732 | ␉␉␉count = read(fd, config->plist, IO_CONFIG_DATA_SIZE);␊ |
733 | ␉␉␉close(fd);␊ |
734 | ␉␉␉␊ |
735 | ␉␉␉// build xml dictionary␊ |
736 | ␉␉␉ParseXMLFile(config->plist, &config->dictionary);␊ |
737 | ␉␉␉sysConfigValid = true;␉␊ |
738 | ␉␉␉ret=0;␊ |
739 | ␉␉␉break;␊ |
740 | ␉␉}␊ |
741 | ␉}␊ |
742 | ␉return ret;␊ |
743 | }␊ |
744 | ␊ |
745 | char * newString(const char * oldString)␊ |
746 | {␊ |
747 | ␉if ( oldString ) {␊ |
748 | ␉␉return strcpy(malloc(strlen(oldString)+1), oldString);␊ |
749 | ␉} else {␊ |
750 | ␉␉return NULL;␊ |
751 | ␉}␊ |
752 | }␊ |
753 | ␊ |
754 | /*␊ |
755 | * Extracts the next argument from the command line, double quotes are allowed here.␊ |
756 | */␊ |
757 | char * getNextArg(char ** argPtr, char * val)␊ |
758 | {␊ |
759 | ␉char * ptr = *argPtr;␊ |
760 | ␉const char * strStart;␊ |
761 | ␉int len = 0;␊ |
762 | ␉bool isQuoted = false;␊ |
763 | ␊ |
764 | ␉*val = '\0';␊ |
765 | ␊ |
766 | ␉// Scan for the next non-whitespace character.␊ |
767 | ␉while ( *ptr && (*ptr == ' ' || *ptr == '=') ) {␊ |
768 | ␉␉ptr++;␊ |
769 | ␉}␊ |
770 | ␊ |
771 | ␉strStart = ptr;␊ |
772 | ␊ |
773 | ␉// Skip the leading double quote character.␊ |
774 | ␉if (*ptr == '\"') {␊ |
775 | ␉␉isQuoted = true;␊ |
776 | ␉␉ptr++;␊ |
777 | ␉␉strStart++;␊ |
778 | ␉}␊ |
779 | ␊ |
780 | ␉// Scan for the argument terminator character.␊ |
781 | ␉// This can be either a NULL character - in case we reach the end of the string,␊ |
782 | ␉// a double quote in case of quoted argument,␊ |
783 | ␉// or a whitespace character (' ' or '=') for non-quoted argument.␊ |
784 | ␉while (*ptr && !( (isQuoted && (*ptr == '\"')) ||␊ |
785 | (!isQuoted && (*ptr == ' ' || *ptr == '=')) )␊ |
786 | ) {␊ |
787 | ␉␉ptr++;␊ |
788 | ␉}␊ |
789 | ␊ |
790 | ␉len = ptr - strStart;␊ |
791 | ␊ |
792 | ␉// Skip the closing double quote character and adjust␊ |
793 | ␉// the starting pointer for the next getNextArg call.␊ |
794 | ␉if (*ptr && isQuoted && *ptr == '\"') {␊ |
795 | ␉␉ptr++;␊ |
796 | ␉}␊ |
797 | ␊ |
798 | ␉// Copy the extracted argument to val.␊ |
799 | ␉strncat(val, strStart, len);␊ |
800 | ␊ |
801 | ␉// Set command line pointer.␊ |
802 | ␉*argPtr = ptr;␊ |
803 | ␊ |
804 | ␉return ptr;␊ |
805 | }␊ |
806 |