Chameleon

Chameleon Svn Source Tree

Root/trunk/i386/libsaio/stringTable.c

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

Archive Download this file

Revision: 2884