2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <pexpert/pexpert.h>
29 #include <pexpert/device_tree.h>
31 typedef boolean_t (*argsep_func_t
) (char c
);
33 static boolean_t
isargsep( char c
);
34 static boolean_t
israngesep( char c
);
35 static int argstrcpy(char *from
, char *to
);
36 static int argstrcpy2(char *from
,char *to
, unsigned maxlen
);
37 static int argnumcpy(long long val
, void *to
, unsigned maxlen
);
38 static int getval(char *s
, long long *val
, argsep_func_t issep
, boolean_t skip_equal_sign
);
39 boolean_t
get_range_bounds(char * c
, int64_t * lower
, int64_t * upper
);
41 extern int IODTGetDefault(const char *key
, void *infoAddr
, unsigned int infoSize
);
52 #if !defined(__LP64__) && !defined(__arm__)
55 const char *arg_string
,
61 return PE_parse_boot_argn(arg_string
, arg_ptr
, max_len
);
66 PE_parse_boot_argn_internal(
67 const char *arg_string
,
70 boolean_t force_string
)
76 boolean_t arg_boolean
;
79 args
= PE_boot_args();
80 if (*args
== '\0') return FALSE
;
85 while(*args
&& isargsep(*args
)) args
++;
95 while (!isargsep (*cp
) && *cp
!= '=')
97 if (*cp
!= '=' && !arg_boolean
)
103 if (strncmp(args
, arg_string
, i
) ||
104 (i
!=strlen(arg_string
)))
109 argnumcpy(1, arg_ptr
, max_len
);
114 while (*cp
&& isargsep (*cp
))
116 if (*cp
== '=' && c
!= '=') {
120 if ('_' == *arg_string
) /* Force a string copy if the argument name begins with an underscore */
122 int hacklen
= 17 > max_len
? 17 : max_len
;
123 argstrcpy2 (++cp
, (char *)arg_ptr
, hacklen
- 1); /* Hack - terminate after 16 characters */
127 switch ((force_string
&& *cp
== '=') ? STR
: getval(cp
, &val
, isargsep
, FALSE
))
130 argnumcpy(val
, arg_ptr
, max_len
);
134 if(max_len
> 0) //max_len of 0 performs no copy at all
135 argstrcpy2(++cp
, (char *)arg_ptr
, max_len
- 1);
136 else if(max_len
== -1) // unreachable on embedded
137 argstrcpy(++cp
, (char *)arg_ptr
);
144 /* Skip over current arg */
145 while(!isargsep(*args
)) args
++;
147 /* Skip leading white space (catch end of args) */
148 while(*args
&& isargsep(*args
)) args
++;
156 const char *arg_string
,
160 return PE_parse_boot_argn_internal(arg_string
, arg_ptr
, max_len
, FALSE
);
164 PE_parse_boot_arg_str(
165 const char *arg_string
,
169 return PE_parse_boot_argn_internal(arg_string
, arg_ptr
, strlen
, TRUE
);
175 if (c
== ' ' || c
== '\0' || c
== '\t')
184 if (isargsep(c
) || c
== '_' || c
== ',')
197 while (!isargsep(*from
)) {
213 while (!isargsep(*from
) && i
< maxlen
) {
221 static int argnumcpy(long long val
, void *to
, unsigned maxlen
)
225 /* No write-back, caller just wants to know if arg was found */
231 *(int16_t *)to
= val
;
234 /* Unlikely in practice */
235 ((struct i24
*)to
)->i24
= val
;
238 *(int32_t *)to
= val
;
241 *(int64_t *)to
= val
;
244 *(int32_t *)to
= val
;
257 boolean_t skip_equal_sign
)
259 unsigned long long radix
, intval
;
262 boolean_t has_value
= FALSE
;
269 if (has_value
|| skip_equal_sign
) {
289 case '0': case '1': case '2': case '3':
290 case '4': case '5': case '6': case '7':
300 } else if (intval
>= radix
) {
308 ((c
>= '0') && (c
<= ('9' - (10 - radix
))))) {
310 } else if ((radix
== 16) &&
311 ((c
>= '0') && (c
<= '9'))) {
313 } else if ((radix
== 16) &&
314 ((c
>= 'a') && (c
<= 'f'))) {
316 } else if ((radix
== 16) &&
317 ((c
>= 'A') && (c
<= 'F'))) {
319 } else if (c
== 'k' || c
== 'K') {
322 } else if (c
== 'm' || c
== 'M') {
325 } else if (c
== 'g' || c
== 'G') {
326 sign
*= 1024 * 1024 * 1024;
336 if (!issep(c
) && !issep(*s
))
338 *val
= intval
* sign
;
346 PE_imgsrc_mount_supported()
353 const char *property_name
,
355 unsigned int max_property
)
358 void **property_data
;
359 unsigned int property_size
;
362 * Look for the property using the PE DT support.
364 if (kSuccess
== DTLookupEntry(NULL
, "/defaults", &dte
)) {
367 * We have a /defaults node, look for the named property.
369 if (kSuccess
!= DTGetProperty(dte
, property_name
, (void **)&property_data
, &property_size
))
373 * This would be a fine place to do smart argument size management for 32/64
374 * translation, but for now we'll insist that callers know how big their
375 * default values are.
377 if (property_size
> max_property
)
381 * Copy back the precisely-sized result.
383 memcpy(property_ptr
, property_data
, property_size
);
388 * Look for the property using I/O Kit's DT support.
390 return IODTGetDefault(property_name
, property_ptr
, max_property
) ? FALSE
: TRUE
;
393 /* function: get_range_bounds
394 * Parse a range string like "1_3,5_20" and return 1,3 as lower and upper.
395 * Note: '_' is separator for bounds integer delimiter and
396 * ',' is considered as separator for range pair.
397 * returns TRUE when both range values are found
400 get_range_bounds(char *c
, int64_t *lower
, int64_t *upper
)
402 if (c
== NULL
|| lower
== NULL
|| upper
== NULL
) {
406 if (NUM
!= getval(c
, lower
, israngesep
, TRUE
)) {
419 if (NUM
!= getval(c
, upper
, israngesep
, TRUE
)) {