]> git.saurik.com Git - apple/xnu.git/blob - pexpert/gen/bootargs.c
xnu-517.12.7.tar.gz
[apple/xnu.git] / pexpert / gen / bootargs.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 #include <pexpert/pexpert.h>
23
24 extern boolean_t isargsep( char c);
25 extern int argstrcpy(char *from, char *to);
26 extern int getval(char *s, int *val);
27
28 #define NUM 0
29 #define STR 1
30
31 boolean_t
32 PE_parse_boot_arg(
33 char *arg_string,
34 void *arg_ptr)
35 {
36 char *args;
37 char *cp, c;
38 int i;
39 int val;
40 boolean_t arg_boolean;
41 boolean_t arg_found;
42
43 args = PE_boot_args();
44 if (*args == '\0') return FALSE;
45
46 arg_found = FALSE;
47
48 while(isargsep(*args)) args++;
49
50 while (*args)
51 {
52 if (*args == '-')
53 arg_boolean = TRUE;
54 else
55 arg_boolean = FALSE;
56
57 cp = args;
58 while (!isargsep (*cp) && *cp != '=')
59 cp++;
60 if (*cp != '=' && !arg_boolean)
61 goto gotit;
62
63 c = *cp;
64
65 i = cp-args;
66 if (strncmp(args, arg_string, i) ||
67 (i!=strlen(arg_string)))
68 goto gotit;
69 if (arg_boolean) {
70 *(unsigned int *)arg_ptr = TRUE;
71 arg_found = TRUE;
72 break;
73 } else {
74 while (isargsep (*cp))
75 cp++;
76 if (*cp == '=' && c != '=') {
77 args = cp+1;
78 goto gotit;
79 }
80 if ('_' == *arg_string) /* Force a string copy if the argument name begins with an underscore */
81 {
82 argstrcpy2 (++cp, (char *)arg_ptr, 16); /* Hack - terminate after 16 characters */
83 arg_found = TRUE;
84 break;
85 }
86 switch (getval(cp, &val))
87 {
88 case NUM:
89 *(unsigned int *)arg_ptr = val;
90 arg_found = TRUE;
91 break;
92 case STR:
93 argstrcpy(++cp, (char *)arg_ptr);
94 arg_found = TRUE;
95 break;
96 }
97 goto gotit;
98 }
99 gotit:
100 /* Skip over current arg */
101 while(!isargsep(*args)) args++;
102
103 /* Skip leading white space (catch end of args) */
104 while(*args && isargsep(*args)) args++;
105 }
106
107 return(arg_found);
108 }
109
110 boolean_t isargsep(
111 char c)
112 {
113 if (c == ' ' || c == '\0' || c == '\t')
114 return(TRUE);
115 else
116 return(FALSE);
117 }
118
119 int
120 argstrcpy(
121 char *from,
122 char *to)
123 {
124 int i = 0;
125
126 while (!isargsep(*from)) {
127 i++;
128 *to++ = *from++;
129 }
130 *to = 0;
131 return(i);
132 }
133
134 int
135 argstrcpy2(
136 char *from,
137 char *to,
138 unsigned maxlen)
139 {
140 int i = 0;
141
142 while (!isargsep(*from) && i < maxlen) {
143 i++;
144 *to++ = *from++;
145 }
146 *to = 0;
147 return(i);
148 }
149
150 int
151 getval(
152 char *s,
153 int *val)
154 {
155 register unsigned radix, intval;
156 register unsigned char c;
157 int sign = 1;
158
159 if (*s == '=') {
160 s++;
161 if (*s == '-')
162 sign = -1, s++;
163 intval = *s++-'0';
164 radix = 10;
165 if (intval == 0)
166 switch(*s) {
167
168 case 'x':
169 radix = 16;
170 s++;
171 break;
172
173 case 'b':
174 radix = 2;
175 s++;
176 break;
177
178 case '0': case '1': case '2': case '3':
179 case '4': case '5': case '6': case '7':
180 intval = *s-'0';
181 s++;
182 radix = 8;
183 break;
184
185 default:
186 if (!isargsep(*s))
187 return (STR);
188 }
189 for(;;) {
190 if (((c = *s++) >= '0') && (c <= '9'))
191 c -= '0';
192 else if ((c >= 'a') && (c <= 'f'))
193 c -= 'a' - 10;
194 else if ((c >= 'A') && (c <= 'F'))
195 c -= 'A' - 10;
196 else if (c == 'k' || c == 'K')
197 { sign *= 1024; break; }
198 else if (c == 'm' || c == 'M')
199 { sign *= 1024 * 1024; break; }
200 else if (c == 'g' || c == 'G')
201 { sign *= 1024 * 1024 * 1024; break; }
202 else if (isargsep(c))
203 break;
204 else
205 return (STR);
206 if (c >= radix)
207 return (STR);
208 intval *= radix;
209 intval += c;
210 }
211 *val = intval * sign;
212 return (NUM);
213 }
214 *val = 1;
215 return (NUM);
216 }