]> git.saurik.com Git - apple/xnu.git/blame - pexpert/gen/bootargs.c
xnu-792.12.6.tar.gz
[apple/xnu.git] / pexpert / gen / bootargs.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
8ad349bb 4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
1c79356b 5 *
8ad349bb
A
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@
1c79356b
A
29 */
30#include <pexpert/pexpert.h>
31
32extern boolean_t isargsep( char c);
33extern int argstrcpy(char *from, char *to);
34extern int getval(char *s, int *val);
35
36#define NUM 0
37#define STR 1
38
91447636 39boolean_t
1c79356b 40PE_parse_boot_arg(
91447636
A
41 const char *arg_string,
42 void *arg_ptr)
43{
44 return PE_parse_boot_argn(arg_string, arg_ptr, -1);
45}
46
47boolean_t
48PE_parse_boot_argn(
49 const char *arg_string,
50 void *arg_ptr,
51 int max_len)
1c79356b
A
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();
9bccf70c
A
61 if (*args == '\0') return FALSE;
62
1c79356b
A
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 }
55e303ae 97 if ('_' == *arg_string) /* Force a string copy if the argument name begins with an underscore */
91447636
A
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 }
1c79356b
A
104 switch (getval(cp, &val))
105 {
106 case NUM:
107 *(unsigned int *)arg_ptr = val;
108 arg_found = TRUE;
109 break;
110 case STR:
91447636
A
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);
1c79356b
A
115 arg_found = TRUE;
116 break;
117 }
118 goto gotit;
119 }
120gotit:
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
131boolean_t isargsep(
132 char c)
133{
134 if (c == ' ' || c == '\0' || c == '\t')
135 return(TRUE);
136 else
137 return(FALSE);
138}
139
140int
141argstrcpy(
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
55e303ae
A
155int
156argstrcpy2(
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
1c79356b
A
171int
172getval(
173 char *s,
174 int *val)
175{
8ad349bb
A
176 register unsigned radix, intval;
177 register unsigned char c;
1c79356b
A
178 int sign = 1;
179
180 if (*s == '=') {
181 s++;
182 if (*s == '-')
183 sign = -1, s++;
184 intval = *s++-'0';
185 radix = 10;
8ad349bb 186 if (intval == 0)
1c79356b
A
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 for(;;) {
8ad349bb 211 if (((c = *s++) >= '0') && (c <= '9'))
1c79356b 212 c -= '0';
8ad349bb 213 else if ((c >= 'a') && (c <= 'f'))
1c79356b 214 c -= 'a' - 10;
8ad349bb 215 else if ((c >= 'A') && (c <= 'F'))
1c79356b 216 c -= 'A' - 10;
8ad349bb
A
217 else if (c == 'k' || c == 'K')
218 { sign *= 1024; break; }
219 else if (c == 'm' || c == 'M')
220 { sign *= 1024 * 1024; break; }
221 else if (c == 'g' || c == 'G')
222 { sign *= 1024 * 1024 * 1024; break; }
223 else if (isargsep(c))
1c79356b 224 break;
8ad349bb 225 else
1c79356b
A
226 return (STR);
227 if (c >= radix)
228 return (STR);
229 intval *= radix;
230 intval += c;
231 }
232 *val = intval * sign;
233 return (NUM);
234 }
235 *val = 1;
236 return (NUM);
237}