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