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 #if defined(__x86_64__)
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
);
55 PE_parse_boot_argn_internal(
56 const char *arg_string
,
59 boolean_t force_string
)
65 boolean_t arg_boolean
;
68 args
= PE_boot_args();
73 #if !defined(__x86_64__)
81 while (*args
&& isargsep(*args
)) {
93 while (!isargsep(*cp
) && *cp
!= '=') {
96 if (*cp
!= '=' && !arg_boolean
) {
103 if (strncmp(args
, arg_string
, i
) ||
104 (i
!= strlen(arg_string
))) {
111 argnumcpy(1, arg_ptr
, max_len
);/* max_len of 0 performs no copy at all*/
113 } else if (max_len
== 0) {
119 while (*cp
&& isargsep(*cp
)) {
122 if (*cp
== '=' && c
!= '=') {
126 if ('_' == *arg_string
) { /* Force a string copy if the argument name begins with an underscore */
128 int hacklen
= 17 > max_len
? 17 : max_len
;
129 argstrcpy2(++cp
, (char *)arg_ptr
, hacklen
- 1); /* Hack - terminate after 16 characters */
131 } else if (max_len
== 0) {
136 switch ((force_string
&& *cp
== '=') ? STR
: getval(cp
, &val
, isargsep
, FALSE
)) {
139 argnumcpy(val
, arg_ptr
, max_len
);
141 } else if (max_len
== 0) {
147 argstrcpy2(++cp
, (char *)arg_ptr
, max_len
- 1); /*max_len of 0 performs no copy at all*/
149 } else if (max_len
== 0) {
152 #if defined(__x86_64__)
153 else if (max_len
== -1) { /* unreachable on embedded */
154 argstrcpy(++cp
, (char *)arg_ptr
);
163 /* Skip over current arg */
164 while (!isargsep(*args
)) {
168 /* Skip leading white space (catch end of args) */
169 while (*args
&& isargsep(*args
)) {
179 const char *arg_string
,
183 return PE_parse_boot_argn_internal(arg_string
, arg_ptr
, max_len
, FALSE
);
187 PE_parse_boot_arg_str(
188 const char *arg_string
,
192 return PE_parse_boot_argn_internal(arg_string
, arg_ptr
, strlen
, TRUE
);
198 if (c
== ' ' || c
== '\0' || c
== '\t') {
208 if (isargsep(c
) || c
== '_' || c
== ',') {
215 #if defined(__x86_64__)
223 while (!isargsep(*from
)) {
240 while (!isargsep(*from
) && i
< maxlen
) {
249 argnumcpy(long long val
, void *to
, unsigned maxlen
)
253 /* No write-back, caller just wants to know if arg was found */
256 *(int8_t *)to
= (int8_t)val
;
259 *(int16_t *)to
= (int16_t)val
;
262 /* Unlikely in practice */
263 ((struct i24
*)to
)->i24
= (int32_t)val
;
266 *(int32_t *)to
= (int32_t)val
;
269 *(int64_t *)to
= (int64_t)val
;
272 *(int32_t *)to
= (int32_t)val
;
285 boolean_t skip_equal_sign
)
287 unsigned long long radix
, intval
;
290 boolean_t has_value
= FALSE
;
297 if (has_value
|| skip_equal_sign
) {
316 case '0': case '1': case '2': case '3':
317 case '4': case '5': case '6': case '7':
328 } else if (intval
>= radix
) {
337 ((c
>= '0') && (c
<= ('9' - (10 - radix
))))) {
339 } else if ((radix
== 16) &&
340 ((c
>= '0') && (c
<= '9'))) {
342 } else if ((radix
== 16) &&
343 ((c
>= 'a') && (c
<= 'f'))) {
345 } else if ((radix
== 16) &&
346 ((c
>= 'A') && (c
<= 'F'))) {
348 } else if (c
== 'k' || c
== 'K') {
351 } else if (c
== 'm' || c
== 'M') {
354 } else if (c
== 'g' || c
== 'G') {
355 sign
*= 1024 * 1024 * 1024;
366 if (!issep(c
) && !issep(*s
)) {
369 *val
= intval
* sign
;
377 PE_imgsrc_mount_supported()
379 #if CONFIG_LOCKERBOOT
381 * Booting from a locker requires that we be able to mount the containing
382 * volume inside the locker. This looks redundant, but this is here in case
383 * the other conditional needs to be modified for some reason.
393 const char *property_name
,
395 unsigned int max_property
)
398 void const *property_data
;
399 unsigned int property_size
;
402 * Look for the property using the PE DT support.
404 if (kSuccess
== SecureDTLookupEntry(NULL
, "/defaults", &dte
)) {
406 * We have a /defaults node, look for the named property.
408 if (kSuccess
!= SecureDTGetProperty(dte
, property_name
, &property_data
, &property_size
)) {
413 * This would be a fine place to do smart argument size management for 32/64
414 * translation, but for now we'll insist that callers know how big their
415 * default values are.
417 if (property_size
> max_property
) {
422 * Copy back the precisely-sized result.
424 memcpy(property_ptr
, property_data
, property_size
);
429 * Look for the property using I/O Kit's DT support.
431 return IODTGetDefault(property_name
, property_ptr
, max_property
) ? FALSE
: TRUE
;
434 /* function: get_range_bounds
435 * Parse a range string like "1_3,5_20" and return 1,3 as lower and upper.
436 * Note: '_' is separator for bounds integer delimiter and
437 * ',' is considered as separator for range pair.
438 * returns TRUE when both range values are found
441 get_range_bounds(char *c
, int64_t *lower
, int64_t *upper
)
443 if (c
== NULL
|| lower
== NULL
|| upper
== NULL
) {
447 if (NUM
!= getval(c
, lower
, israngesep
, TRUE
)) {
460 if (NUM
!= getval(c
, upper
, israngesep
, TRUE
)) {