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 #ifndef CONFIG_EMBEDDED
36 static int argstrcpy(char *from
, char *to
);
38 static int argstrcpy2(char *from
,char *to
, unsigned maxlen
);
39 static int argnumcpy(long long val
, void *to
, unsigned maxlen
);
40 static int getval(char *s
, long long *val
, argsep_func_t issep
, boolean_t skip_equal_sign
);
41 boolean_t
get_range_bounds(char * c
, int64_t * lower
, int64_t * upper
);
43 extern int IODTGetDefault(const char *key
, void *infoAddr
, unsigned int infoSize
);
54 #if !defined(__LP64__) && !defined(__arm__)
57 const char *arg_string
,
63 /* Limit arg size to 4 byte when no size is given */
67 return PE_parse_boot_argn(arg_string
, arg_ptr
, max_len
);
72 PE_parse_boot_argn_internal(
73 const char *arg_string
,
76 boolean_t force_string
)
82 boolean_t arg_boolean
;
85 args
= PE_boot_args();
86 if (*args
== '\0') return FALSE
;
88 #ifdef CONFIG_EMBEDDED
89 if (max_len
== -1) return FALSE
;
94 while(*args
&& isargsep(*args
)) args
++;
104 while (!isargsep (*cp
) && *cp
!= '=')
106 if (*cp
!= '=' && !arg_boolean
)
112 if (strncmp(args
, arg_string
, i
) ||
113 (i
!=strlen(arg_string
)))
119 argnumcpy(1, arg_ptr
, max_len
);/* max_len of 0 performs no copy at all*/
122 else if (max_len
== 0) {
128 while (*cp
&& isargsep (*cp
))
130 if (*cp
== '=' && c
!= '=') {
134 if ('_' == *arg_string
) /* Force a string copy if the argument name begins with an underscore */
137 int hacklen
= 17 > max_len
? 17 : max_len
;
138 argstrcpy2 (++cp
, (char *)arg_ptr
, hacklen
- 1); /* Hack - terminate after 16 characters */
141 else if (max_len
== 0) {
146 switch ((force_string
&& *cp
== '=') ? STR
: getval(cp
, &val
, isargsep
, FALSE
))
150 argnumcpy(val
, arg_ptr
, max_len
);
153 else if (max_len
== 0) {
159 argstrcpy2(++cp
, (char *)arg_ptr
, max_len
- 1);/*max_len of 0 performs no copy at all*/
162 else if (max_len
== 0) {
166 else if (max_len
== -1) { /* unreachable on embedded */
167 argstrcpy(++cp
, (char *)arg_ptr
);
176 /* Skip over current arg */
177 while(!isargsep(*args
)) args
++;
179 /* Skip leading white space (catch end of args) */
180 while(*args
&& isargsep(*args
)) args
++;
188 const char *arg_string
,
192 return PE_parse_boot_argn_internal(arg_string
, arg_ptr
, max_len
, FALSE
);
196 PE_parse_boot_arg_str(
197 const char *arg_string
,
201 return PE_parse_boot_argn_internal(arg_string
, arg_ptr
, strlen
, TRUE
);
207 if (c
== ' ' || c
== '\0' || c
== '\t')
216 if (isargsep(c
) || c
== '_' || c
== ',')
230 while (!isargsep(*from
)) {
247 while (!isargsep(*from
) && i
< maxlen
) {
255 static int argnumcpy(long long val
, void *to
, unsigned maxlen
)
259 /* No write-back, caller just wants to know if arg was found */
265 *(int16_t *)to
= val
;
268 /* Unlikely in practice */
269 ((struct i24
*)to
)->i24
= val
;
272 *(int32_t *)to
= val
;
275 *(int64_t *)to
= val
;
278 *(int32_t *)to
= val
;
291 boolean_t skip_equal_sign
)
293 unsigned long long radix
, intval
;
296 boolean_t has_value
= FALSE
;
303 if (has_value
|| skip_equal_sign
) {
323 case '0': case '1': case '2': case '3':
324 case '4': case '5': case '6': case '7':
334 } else if (intval
>= radix
) {
342 ((c
>= '0') && (c
<= ('9' - (10 - radix
))))) {
344 } else if ((radix
== 16) &&
345 ((c
>= '0') && (c
<= '9'))) {
347 } else if ((radix
== 16) &&
348 ((c
>= 'a') && (c
<= 'f'))) {
350 } else if ((radix
== 16) &&
351 ((c
>= 'A') && (c
<= 'F'))) {
353 } else if (c
== 'k' || c
== 'K') {
356 } else if (c
== 'm' || c
== 'M') {
359 } else if (c
== 'g' || c
== 'G') {
360 sign
*= 1024 * 1024 * 1024;
370 if (!issep(c
) && !issep(*s
))
372 *val
= intval
* sign
;
380 PE_imgsrc_mount_supported()
387 const char *property_name
,
389 unsigned int max_property
)
392 void **property_data
;
393 unsigned int property_size
;
396 * Look for the property using the PE DT support.
398 if (kSuccess
== DTLookupEntry(NULL
, "/defaults", &dte
)) {
401 * We have a /defaults node, look for the named property.
403 if (kSuccess
!= DTGetProperty(dte
, property_name
, (void **)&property_data
, &property_size
))
407 * This would be a fine place to do smart argument size management for 32/64
408 * translation, but for now we'll insist that callers know how big their
409 * default values are.
411 if (property_size
> max_property
)
415 * Copy back the precisely-sized result.
417 memcpy(property_ptr
, property_data
, property_size
);
422 * Look for the property using I/O Kit's DT support.
424 return IODTGetDefault(property_name
, property_ptr
, max_property
) ? FALSE
: TRUE
;
427 /* function: get_range_bounds
428 * Parse a range string like "1_3,5_20" and return 1,3 as lower and upper.
429 * Note: '_' is separator for bounds integer delimiter and
430 * ',' is considered as separator for range pair.
431 * returns TRUE when both range values are found
434 get_range_bounds(char *c
, int64_t *lower
, int64_t *upper
)
436 if (c
== NULL
|| lower
== NULL
|| upper
== NULL
) {
440 if (NUM
!= getval(c
, lower
, israngesep
, TRUE
)) {
453 if (NUM
!= getval(c
, upper
, israngesep
, TRUE
)) {