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