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