xref: /xnu-8796.141.3/pexpert/gen/device_tree.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions  * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions  *
4*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions  *
6*1b191cb5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*1b191cb5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*1b191cb5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*1b191cb5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*1b191cb5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*1b191cb5SApple OSS Distributions  *
15*1b191cb5SApple OSS Distributions  * Please obtain a copy of the License at
16*1b191cb5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1b191cb5SApple OSS Distributions  *
18*1b191cb5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*1b191cb5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1b191cb5SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1b191cb5SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1b191cb5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1b191cb5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*1b191cb5SApple OSS Distributions  * limitations under the License.
25*1b191cb5SApple OSS Distributions  *
26*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1b191cb5SApple OSS Distributions  */
28*1b191cb5SApple OSS Distributions /*
29*1b191cb5SApple OSS Distributions  * @OSF_FREE_COPYRIGHT@
30*1b191cb5SApple OSS Distributions  */
31*1b191cb5SApple OSS Distributions 
32*1b191cb5SApple OSS Distributions #include <pexpert/protos.h>
33*1b191cb5SApple OSS Distributions #include <pexpert/boot.h>
34*1b191cb5SApple OSS Distributions #include <pexpert/device_tree.h>
35*1b191cb5SApple OSS Distributions 
36*1b191cb5SApple OSS Distributions #include <mach/mach_types.h>
37*1b191cb5SApple OSS Distributions #include <mach/machine/vm_types.h>
38*1b191cb5SApple OSS Distributions #include <kern/debug.h>
39*1b191cb5SApple OSS Distributions #include <kern/kern_types.h>
40*1b191cb5SApple OSS Distributions #include <kern/kalloc.h>
41*1b191cb5SApple OSS Distributions #include <libkern/kernel_mach_header.h>
42*1b191cb5SApple OSS Distributions #include <os/overflow.h>
43*1b191cb5SApple OSS Distributions 
44*1b191cb5SApple OSS Distributions #if defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR)
45*1b191cb5SApple OSS Distributions extern addr64_t kvtophys(vm_offset_t va);
46*1b191cb5SApple OSS Distributions #endif /* defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR) */
47*1b191cb5SApple OSS Distributions 
48*1b191cb5SApple OSS Distributions #include <sys/types.h>
49*1b191cb5SApple OSS Distributions 
50*1b191cb5SApple OSS Distributions SECURITY_READ_ONLY_LATE(static int) DTInitialized;
51*1b191cb5SApple OSS Distributions SECURITY_READ_ONLY_LATE(RealDTEntry) DTRootNode;
52*1b191cb5SApple OSS Distributions SECURITY_READ_ONLY_LATE(static vm_size_t) DTSize;
53*1b191cb5SApple OSS Distributions SECURITY_READ_ONLY_LATE(static vm_offset_t) DTEnd;
54*1b191cb5SApple OSS Distributions 
55*1b191cb5SApple OSS Distributions /*
56*1b191cb5SApple OSS Distributions  *
57*1b191cb5SApple OSS Distributions  * Support Routines
58*1b191cb5SApple OSS Distributions  *
59*1b191cb5SApple OSS Distributions  */
60*1b191cb5SApple OSS Distributions 
61*1b191cb5SApple OSS Distributions static inline void
assert_in_dt_region(vm_offset_t const start,vm_offset_t const end,void const * p)62*1b191cb5SApple OSS Distributions assert_in_dt_region(vm_offset_t const start, vm_offset_t const end, void const *p)
63*1b191cb5SApple OSS Distributions {
64*1b191cb5SApple OSS Distributions 	if ((vm_offset_t)p < start || (vm_offset_t)p > end) {
65*1b191cb5SApple OSS Distributions 		panic("Device tree pointer outside of device tree region: pointer %p, DTEnd %lx", p, (unsigned long)DTEnd);
66*1b191cb5SApple OSS Distributions 	}
67*1b191cb5SApple OSS Distributions }
68*1b191cb5SApple OSS Distributions #define ASSERT_IN_DT(p) assert_in_dt_region((vm_offset_t)DTRootNode, (vm_offset_t)DTEnd, (p))
69*1b191cb5SApple OSS Distributions 
70*1b191cb5SApple OSS Distributions static inline void
assert_prop_in_dt_region(vm_offset_t const start,vm_offset_t const end,DeviceTreeNodeProperty const * prop)71*1b191cb5SApple OSS Distributions assert_prop_in_dt_region(vm_offset_t const start, vm_offset_t const end, DeviceTreeNodeProperty const *prop)
72*1b191cb5SApple OSS Distributions {
73*1b191cb5SApple OSS Distributions 	vm_offset_t prop_end;
74*1b191cb5SApple OSS Distributions 
75*1b191cb5SApple OSS Distributions 	assert_in_dt_region(start, end, prop);
76*1b191cb5SApple OSS Distributions 	if (os_add3_overflow((vm_offset_t)prop, sizeof(DeviceTreeNodeProperty), prop->length, &prop_end)) {
77*1b191cb5SApple OSS Distributions 		panic("Device tree property overflow: prop %p, length 0x%x", prop, prop->length);
78*1b191cb5SApple OSS Distributions 	}
79*1b191cb5SApple OSS Distributions 	assert_in_dt_region(start, end, (void*)prop_end);
80*1b191cb5SApple OSS Distributions }
81*1b191cb5SApple OSS Distributions #define ASSERT_PROP_IN_DT(prop) assert_prop_in_dt_region((vm_offset_t)DTRootNode, (vm_offset_t)DTEnd, (prop))
82*1b191cb5SApple OSS Distributions 
83*1b191cb5SApple OSS Distributions #define ASSERT_HEADER_IN_DT_REGION(start, end, p, size) assert_in_dt_region((start), (end), (uint8_t const *)(p) + (size))
84*1b191cb5SApple OSS Distributions #define ASSERT_HEADER_IN_DT(p, size) ASSERT_IN_DT((uint8_t const *)(p) + (size))
85*1b191cb5SApple OSS Distributions 
86*1b191cb5SApple OSS Distributions /*
87*1b191cb5SApple OSS Distributions  * Since there is no way to know the size of a device tree node
88*1b191cb5SApple OSS Distributions  * without fully walking it, we employ the following principle to make
89*1b191cb5SApple OSS Distributions  * sure that the accessed device tree is fully within its memory
90*1b191cb5SApple OSS Distributions  * region:
91*1b191cb5SApple OSS Distributions  *
92*1b191cb5SApple OSS Distributions  * Internally, we check anything we want to access just before we want
93*1b191cb5SApple OSS Distributions  * to access it (not after creating a pointer).
94*1b191cb5SApple OSS Distributions  *
95*1b191cb5SApple OSS Distributions  * Then, before returning a DTEntry to the caller, we check whether
96*1b191cb5SApple OSS Distributions  * the start address (only!) of the entry is still within the device
97*1b191cb5SApple OSS Distributions  * tree region.
98*1b191cb5SApple OSS Distributions  *
99*1b191cb5SApple OSS Distributions  * Before returning a property value the caller, we check whether the
100*1b191cb5SApple OSS Distributions  * property is fully within the region.
101*1b191cb5SApple OSS Distributions  *
102*1b191cb5SApple OSS Distributions  * "DTEntry"s are opaque to the caller, so only checking their
103*1b191cb5SApple OSS Distributions  * starting address is enough to satisfy existence within the device
104*1b191cb5SApple OSS Distributions  * tree region, while for property values we need to make sure that
105*1b191cb5SApple OSS Distributions  * they are fully within the region.
106*1b191cb5SApple OSS Distributions  */
107*1b191cb5SApple OSS Distributions 
108*1b191cb5SApple OSS Distributions static inline DeviceTreeNodeProperty const *
next_prop_region(vm_offset_t const start,vm_offset_t end,DeviceTreeNodeProperty const * prop)109*1b191cb5SApple OSS Distributions next_prop_region(vm_offset_t const start, vm_offset_t end, DeviceTreeNodeProperty const *prop)
110*1b191cb5SApple OSS Distributions {
111*1b191cb5SApple OSS Distributions 	uintptr_t next_addr;
112*1b191cb5SApple OSS Distributions 
113*1b191cb5SApple OSS Distributions 	ASSERT_HEADER_IN_DT_REGION(start, end, prop, sizeof(DeviceTreeNode));
114*1b191cb5SApple OSS Distributions 
115*1b191cb5SApple OSS Distributions 	if (os_add3_overflow((uintptr_t)prop, prop->length, sizeof(DeviceTreeNodeProperty) + 3, &next_addr)) {
116*1b191cb5SApple OSS Distributions 		panic("Device tree property overflow: prop %p, length 0x%x", prop, prop->length);
117*1b191cb5SApple OSS Distributions 	}
118*1b191cb5SApple OSS Distributions 
119*1b191cb5SApple OSS Distributions 	next_addr &= ~(3ULL);
120*1b191cb5SApple OSS Distributions 
121*1b191cb5SApple OSS Distributions 	return (DeviceTreeNodeProperty*)next_addr;
122*1b191cb5SApple OSS Distributions }
123*1b191cb5SApple OSS Distributions #define next_prop(prop) next_prop_region((vm_offset_t)DTRootNode, (vm_offset_t)DTEnd, (prop))
124*1b191cb5SApple OSS Distributions 
125*1b191cb5SApple OSS Distributions static RealDTEntry
skipProperties(RealDTEntry entry)126*1b191cb5SApple OSS Distributions skipProperties(RealDTEntry entry)
127*1b191cb5SApple OSS Distributions {
128*1b191cb5SApple OSS Distributions 	DeviceTreeNodeProperty const *prop;
129*1b191cb5SApple OSS Distributions 	unsigned int k;
130*1b191cb5SApple OSS Distributions 
131*1b191cb5SApple OSS Distributions 	if (entry == NULL) {
132*1b191cb5SApple OSS Distributions 		return NULL;
133*1b191cb5SApple OSS Distributions 	}
134*1b191cb5SApple OSS Distributions 
135*1b191cb5SApple OSS Distributions 	ASSERT_HEADER_IN_DT(entry, sizeof(DeviceTreeNode));
136*1b191cb5SApple OSS Distributions 
137*1b191cb5SApple OSS Distributions 	if (entry->nProperties == 0) {
138*1b191cb5SApple OSS Distributions 		return NULL;
139*1b191cb5SApple OSS Distributions 	} else {
140*1b191cb5SApple OSS Distributions 		prop = (DeviceTreeNodeProperty const *) (entry + 1);
141*1b191cb5SApple OSS Distributions 		for (k = 0; k < entry->nProperties; k++) {
142*1b191cb5SApple OSS Distributions 			prop = next_prop(prop);
143*1b191cb5SApple OSS Distributions 		}
144*1b191cb5SApple OSS Distributions 	}
145*1b191cb5SApple OSS Distributions 	ASSERT_IN_DT(prop);
146*1b191cb5SApple OSS Distributions 	return (RealDTEntry) prop;
147*1b191cb5SApple OSS Distributions }
148*1b191cb5SApple OSS Distributions 
149*1b191cb5SApple OSS Distributions static RealDTEntry
skipTree(RealDTEntry root)150*1b191cb5SApple OSS Distributions skipTree(RealDTEntry root)
151*1b191cb5SApple OSS Distributions {
152*1b191cb5SApple OSS Distributions 	RealDTEntry entry;
153*1b191cb5SApple OSS Distributions 	unsigned int k;
154*1b191cb5SApple OSS Distributions 
155*1b191cb5SApple OSS Distributions 	ASSERT_HEADER_IN_DT(root, sizeof(DeviceTreeNode));
156*1b191cb5SApple OSS Distributions 
157*1b191cb5SApple OSS Distributions 	entry = skipProperties(root);
158*1b191cb5SApple OSS Distributions 	if (entry == NULL) {
159*1b191cb5SApple OSS Distributions 		return NULL;
160*1b191cb5SApple OSS Distributions 	}
161*1b191cb5SApple OSS Distributions 	for (k = 0; k < root->nChildren; k++) {
162*1b191cb5SApple OSS Distributions 		entry = skipTree(entry);
163*1b191cb5SApple OSS Distributions 	}
164*1b191cb5SApple OSS Distributions 	return entry;
165*1b191cb5SApple OSS Distributions }
166*1b191cb5SApple OSS Distributions 
167*1b191cb5SApple OSS Distributions static RealDTEntry
GetFirstChild(RealDTEntry parent)168*1b191cb5SApple OSS Distributions GetFirstChild(RealDTEntry parent)
169*1b191cb5SApple OSS Distributions {
170*1b191cb5SApple OSS Distributions 	return skipProperties(parent);
171*1b191cb5SApple OSS Distributions }
172*1b191cb5SApple OSS Distributions 
173*1b191cb5SApple OSS Distributions static RealDTEntry
GetNextChild(RealDTEntry sibling)174*1b191cb5SApple OSS Distributions GetNextChild(RealDTEntry sibling)
175*1b191cb5SApple OSS Distributions {
176*1b191cb5SApple OSS Distributions 	return skipTree(sibling);
177*1b191cb5SApple OSS Distributions }
178*1b191cb5SApple OSS Distributions 
179*1b191cb5SApple OSS Distributions static const char *
GetNextComponent(const char * cp,char * bp)180*1b191cb5SApple OSS Distributions GetNextComponent(const char *cp, char *bp)
181*1b191cb5SApple OSS Distributions {
182*1b191cb5SApple OSS Distributions 	size_t length = 0;
183*1b191cb5SApple OSS Distributions 	char *origbp = bp;
184*1b191cb5SApple OSS Distributions 
185*1b191cb5SApple OSS Distributions 	while (*cp != 0) {
186*1b191cb5SApple OSS Distributions 		if (*cp == kDTPathNameSeparator) {
187*1b191cb5SApple OSS Distributions 			cp++;
188*1b191cb5SApple OSS Distributions 			break;
189*1b191cb5SApple OSS Distributions 		}
190*1b191cb5SApple OSS Distributions 		if (++length > kDTMaxEntryNameLength) {
191*1b191cb5SApple OSS Distributions 			*origbp = '\0';
192*1b191cb5SApple OSS Distributions 			return cp;
193*1b191cb5SApple OSS Distributions 		}
194*1b191cb5SApple OSS Distributions 		*bp++ = *cp++;
195*1b191cb5SApple OSS Distributions 	}
196*1b191cb5SApple OSS Distributions 	*bp = 0;
197*1b191cb5SApple OSS Distributions 	return cp;
198*1b191cb5SApple OSS Distributions }
199*1b191cb5SApple OSS Distributions 
200*1b191cb5SApple OSS Distributions static RealDTEntry
FindChild(RealDTEntry cur,char * buf)201*1b191cb5SApple OSS Distributions FindChild(RealDTEntry cur, char *buf)
202*1b191cb5SApple OSS Distributions {
203*1b191cb5SApple OSS Distributions 	RealDTEntry     child;
204*1b191cb5SApple OSS Distributions 	unsigned long   index;
205*1b191cb5SApple OSS Distributions 	char const *    str;
206*1b191cb5SApple OSS Distributions 	unsigned int    dummy;
207*1b191cb5SApple OSS Distributions 
208*1b191cb5SApple OSS Distributions 	ASSERT_HEADER_IN_DT(cur, sizeof(DeviceTreeNode));
209*1b191cb5SApple OSS Distributions 
210*1b191cb5SApple OSS Distributions 	if (cur->nChildren == 0) {
211*1b191cb5SApple OSS Distributions 		return NULL;
212*1b191cb5SApple OSS Distributions 	}
213*1b191cb5SApple OSS Distributions 	index = 1;
214*1b191cb5SApple OSS Distributions 	child = GetFirstChild(cur);
215*1b191cb5SApple OSS Distributions 	while (1) {
216*1b191cb5SApple OSS Distributions 		if (SecureDTGetProperty(child, "name", (void const **)&str, &dummy) != kSuccess) {
217*1b191cb5SApple OSS Distributions 			break;
218*1b191cb5SApple OSS Distributions 		}
219*1b191cb5SApple OSS Distributions 		if (strcmp(str, buf) == 0) {
220*1b191cb5SApple OSS Distributions 			return child;
221*1b191cb5SApple OSS Distributions 		}
222*1b191cb5SApple OSS Distributions 		if (index >= cur->nChildren) {
223*1b191cb5SApple OSS Distributions 			break;
224*1b191cb5SApple OSS Distributions 		}
225*1b191cb5SApple OSS Distributions 		child = GetNextChild(child);
226*1b191cb5SApple OSS Distributions 		index++;
227*1b191cb5SApple OSS Distributions 	}
228*1b191cb5SApple OSS Distributions 	return NULL;
229*1b191cb5SApple OSS Distributions }
230*1b191cb5SApple OSS Distributions 
231*1b191cb5SApple OSS Distributions /*
232*1b191cb5SApple OSS Distributions  * External Routines
233*1b191cb5SApple OSS Distributions  */
234*1b191cb5SApple OSS Distributions void
SecureDTInit(void const * base,size_t size)235*1b191cb5SApple OSS Distributions SecureDTInit(void const *base, size_t size)
236*1b191cb5SApple OSS Distributions {
237*1b191cb5SApple OSS Distributions 	if ((uintptr_t)base + size < (uintptr_t)base) {
238*1b191cb5SApple OSS Distributions 		panic("DeviceTree overflow: %p, size %#zx", base, size);
239*1b191cb5SApple OSS Distributions 	}
240*1b191cb5SApple OSS Distributions 	DTRootNode = base;
241*1b191cb5SApple OSS Distributions 	DTSize = size;
242*1b191cb5SApple OSS Distributions 	DTEnd = (vm_offset_t)DTRootNode + DTSize;
243*1b191cb5SApple OSS Distributions 	DTInitialized = (DTRootNode != 0);
244*1b191cb5SApple OSS Distributions }
245*1b191cb5SApple OSS Distributions 
246*1b191cb5SApple OSS Distributions bool
SecureDTIsLockedDown(void)247*1b191cb5SApple OSS Distributions SecureDTIsLockedDown(void)
248*1b191cb5SApple OSS Distributions {
249*1b191cb5SApple OSS Distributions #if defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR)
250*1b191cb5SApple OSS Distributions 	/*
251*1b191cb5SApple OSS Distributions 	 * We cannot check if the DT is in the CTRR region early on,
252*1b191cb5SApple OSS Distributions 	 * because knowledge of the CTRR region is set up later.  But the
253*1b191cb5SApple OSS Distributions 	 * DT is used in all kinds of early bootstrapping before that.
254*1b191cb5SApple OSS Distributions 	 *
255*1b191cb5SApple OSS Distributions 	 * Luckily, we know that the device tree must be in front of the
256*1b191cb5SApple OSS Distributions 	 * kernel if set up in EXTRADATA (which means it's covered by
257*1b191cb5SApple OSS Distributions 	 * CTRR), and after it otherwise.
258*1b191cb5SApple OSS Distributions 	 */
259*1b191cb5SApple OSS Distributions 	addr64_t exec_header_phys = kvtophys((vm_offset_t)&_mh_execute_header);
260*1b191cb5SApple OSS Distributions 
261*1b191cb5SApple OSS Distributions 	if (kvtophys((vm_offset_t)DTRootNode) < exec_header_phys) {
262*1b191cb5SApple OSS Distributions 		assert(kvtophys(DTEnd) <= exec_header_phys);
263*1b191cb5SApple OSS Distributions 		return true;
264*1b191cb5SApple OSS Distributions 	}
265*1b191cb5SApple OSS Distributions 
266*1b191cb5SApple OSS Distributions #endif
267*1b191cb5SApple OSS Distributions 	return false;
268*1b191cb5SApple OSS Distributions }
269*1b191cb5SApple OSS Distributions 
270*1b191cb5SApple OSS Distributions int
SecureDTEntryIsEqual(const DTEntry ref1,const DTEntry ref2)271*1b191cb5SApple OSS Distributions SecureDTEntryIsEqual(const DTEntry ref1, const DTEntry ref2)
272*1b191cb5SApple OSS Distributions {
273*1b191cb5SApple OSS Distributions 	/* equality of pointers */
274*1b191cb5SApple OSS Distributions 	return ref1 == ref2;
275*1b191cb5SApple OSS Distributions }
276*1b191cb5SApple OSS Distributions 
277*1b191cb5SApple OSS Distributions static char const *startingP;         // needed for find_entry
278*1b191cb5SApple OSS Distributions int find_entry(const char *propName, const char *propValue, DTEntry *entryH);
279*1b191cb5SApple OSS Distributions 
280*1b191cb5SApple OSS Distributions int
SecureDTFindEntry(const char * propName,const char * propValue,DTEntry * entryH)281*1b191cb5SApple OSS Distributions SecureDTFindEntry(const char *propName, const char *propValue, DTEntry *entryH)
282*1b191cb5SApple OSS Distributions {
283*1b191cb5SApple OSS Distributions 	if (!DTInitialized) {
284*1b191cb5SApple OSS Distributions 		return kError;
285*1b191cb5SApple OSS Distributions 	}
286*1b191cb5SApple OSS Distributions 
287*1b191cb5SApple OSS Distributions 	startingP = (char const *)DTRootNode;
288*1b191cb5SApple OSS Distributions 	return find_entry(propName, propValue, entryH);
289*1b191cb5SApple OSS Distributions }
290*1b191cb5SApple OSS Distributions 
291*1b191cb5SApple OSS Distributions int
find_entry(const char * propName,const char * propValue,DTEntry * entryH)292*1b191cb5SApple OSS Distributions find_entry(const char *propName, const char *propValue, DTEntry *entryH)
293*1b191cb5SApple OSS Distributions {
294*1b191cb5SApple OSS Distributions 	DeviceTreeNode const *nodeP = (DeviceTreeNode const *) (void const *) startingP;
295*1b191cb5SApple OSS Distributions 	unsigned int k;
296*1b191cb5SApple OSS Distributions 
297*1b191cb5SApple OSS Distributions 	ASSERT_HEADER_IN_DT(nodeP, sizeof(DeviceTreeNode));
298*1b191cb5SApple OSS Distributions 
299*1b191cb5SApple OSS Distributions 	if (nodeP->nProperties == 0) {
300*1b191cb5SApple OSS Distributions 		return kError;                        // End of the list of nodes
301*1b191cb5SApple OSS Distributions 	}
302*1b191cb5SApple OSS Distributions 	startingP = (char const *) (nodeP + 1);
303*1b191cb5SApple OSS Distributions 
304*1b191cb5SApple OSS Distributions 	// Search current entry
305*1b191cb5SApple OSS Distributions 	for (k = 0; k < nodeP->nProperties; ++k) {
306*1b191cb5SApple OSS Distributions 		DeviceTreeNodeProperty const *propP = (DeviceTreeNodeProperty const *) (void const *) startingP;
307*1b191cb5SApple OSS Distributions 		ASSERT_PROP_IN_DT(propP);
308*1b191cb5SApple OSS Distributions 
309*1b191cb5SApple OSS Distributions 		startingP += sizeof(*propP) + ((propP->length + 3) & -4);
310*1b191cb5SApple OSS Distributions 
311*1b191cb5SApple OSS Distributions 		if (strcmp(propP->name, propName) == 0) {
312*1b191cb5SApple OSS Distributions 			if (propValue == NULL || strcmp((char const *)(propP + 1), propValue) == 0) {
313*1b191cb5SApple OSS Distributions 				*entryH = (DTEntry)nodeP;
314*1b191cb5SApple OSS Distributions 				ASSERT_HEADER_IN_DT(*entryH, sizeof(DeviceTreeNode));
315*1b191cb5SApple OSS Distributions 				return kSuccess;
316*1b191cb5SApple OSS Distributions 			}
317*1b191cb5SApple OSS Distributions 		}
318*1b191cb5SApple OSS Distributions 	}
319*1b191cb5SApple OSS Distributions 
320*1b191cb5SApple OSS Distributions 	// Search child nodes
321*1b191cb5SApple OSS Distributions 	for (k = 0; k < nodeP->nChildren; ++k) {
322*1b191cb5SApple OSS Distributions 		if (find_entry(propName, propValue, entryH) == kSuccess) {
323*1b191cb5SApple OSS Distributions 			return kSuccess;
324*1b191cb5SApple OSS Distributions 		}
325*1b191cb5SApple OSS Distributions 	}
326*1b191cb5SApple OSS Distributions 	return kError;
327*1b191cb5SApple OSS Distributions }
328*1b191cb5SApple OSS Distributions 
329*1b191cb5SApple OSS Distributions int
SecureDTLookupEntry(const DTEntry searchPoint,const char * pathName,DTEntry * foundEntry)330*1b191cb5SApple OSS Distributions SecureDTLookupEntry(const DTEntry searchPoint, const char *pathName, DTEntry *foundEntry)
331*1b191cb5SApple OSS Distributions {
332*1b191cb5SApple OSS Distributions 	DTEntryNameBuf  buf;
333*1b191cb5SApple OSS Distributions 	RealDTEntry     cur;
334*1b191cb5SApple OSS Distributions 	const char *    cp;
335*1b191cb5SApple OSS Distributions 
336*1b191cb5SApple OSS Distributions 	if (!DTInitialized) {
337*1b191cb5SApple OSS Distributions 		return kError;
338*1b191cb5SApple OSS Distributions 	}
339*1b191cb5SApple OSS Distributions 	if (searchPoint == NULL) {
340*1b191cb5SApple OSS Distributions 		cur = DTRootNode;
341*1b191cb5SApple OSS Distributions 	} else {
342*1b191cb5SApple OSS Distributions 		cur = searchPoint;
343*1b191cb5SApple OSS Distributions 	}
344*1b191cb5SApple OSS Distributions 	ASSERT_IN_DT(cur);
345*1b191cb5SApple OSS Distributions 	cp = pathName;
346*1b191cb5SApple OSS Distributions 	if (*cp == kDTPathNameSeparator) {
347*1b191cb5SApple OSS Distributions 		cp++;
348*1b191cb5SApple OSS Distributions 		if (*cp == 0) {
349*1b191cb5SApple OSS Distributions 			*foundEntry = cur;
350*1b191cb5SApple OSS Distributions 			return kSuccess;
351*1b191cb5SApple OSS Distributions 		}
352*1b191cb5SApple OSS Distributions 	}
353*1b191cb5SApple OSS Distributions 	do {
354*1b191cb5SApple OSS Distributions 		cp = GetNextComponent(cp, buf);
355*1b191cb5SApple OSS Distributions 
356*1b191cb5SApple OSS Distributions 		/* Check for done */
357*1b191cb5SApple OSS Distributions 		if (*buf == 0) {
358*1b191cb5SApple OSS Distributions 			if (*cp == 0) {
359*1b191cb5SApple OSS Distributions 				*foundEntry = cur;
360*1b191cb5SApple OSS Distributions 				return kSuccess;
361*1b191cb5SApple OSS Distributions 			}
362*1b191cb5SApple OSS Distributions 			break;
363*1b191cb5SApple OSS Distributions 		}
364*1b191cb5SApple OSS Distributions 
365*1b191cb5SApple OSS Distributions 		cur = FindChild(cur, buf);
366*1b191cb5SApple OSS Distributions 	} while (cur != NULL);
367*1b191cb5SApple OSS Distributions 
368*1b191cb5SApple OSS Distributions 	return kError;
369*1b191cb5SApple OSS Distributions }
370*1b191cb5SApple OSS Distributions 
371*1b191cb5SApple OSS Distributions int
SecureDTInitEntryIterator(const DTEntry startEntry,DTEntryIterator iter)372*1b191cb5SApple OSS Distributions SecureDTInitEntryIterator(const DTEntry startEntry, DTEntryIterator iter)
373*1b191cb5SApple OSS Distributions {
374*1b191cb5SApple OSS Distributions 	if (!DTInitialized) {
375*1b191cb5SApple OSS Distributions 		return kError;
376*1b191cb5SApple OSS Distributions 	}
377*1b191cb5SApple OSS Distributions 
378*1b191cb5SApple OSS Distributions 	if (startEntry != NULL) {
379*1b191cb5SApple OSS Distributions 		iter->outerScope = (RealDTEntry) startEntry;
380*1b191cb5SApple OSS Distributions 		iter->currentScope = (RealDTEntry) startEntry;
381*1b191cb5SApple OSS Distributions 	} else {
382*1b191cb5SApple OSS Distributions 		iter->outerScope = DTRootNode;
383*1b191cb5SApple OSS Distributions 		iter->currentScope = DTRootNode;
384*1b191cb5SApple OSS Distributions 	}
385*1b191cb5SApple OSS Distributions 	iter->currentEntry = NULL;
386*1b191cb5SApple OSS Distributions 	iter->savedScope = NULL;
387*1b191cb5SApple OSS Distributions 	iter->currentIndex = 0;
388*1b191cb5SApple OSS Distributions 
389*1b191cb5SApple OSS Distributions 	return kSuccess;
390*1b191cb5SApple OSS Distributions }
391*1b191cb5SApple OSS Distributions 
392*1b191cb5SApple OSS Distributions int
SecureDTEnterEntry(DTEntryIterator iter,DTEntry childEntry)393*1b191cb5SApple OSS Distributions SecureDTEnterEntry(DTEntryIterator iter, DTEntry childEntry)
394*1b191cb5SApple OSS Distributions {
395*1b191cb5SApple OSS Distributions 	DTSavedScopePtr newScope;
396*1b191cb5SApple OSS Distributions 
397*1b191cb5SApple OSS Distributions 	if (childEntry == NULL) {
398*1b191cb5SApple OSS Distributions 		return kError;
399*1b191cb5SApple OSS Distributions 	}
400*1b191cb5SApple OSS Distributions 	newScope = (DTSavedScopePtr) kalloc_type(struct DTSavedScope, Z_WAITOK);
401*1b191cb5SApple OSS Distributions 	newScope->nextScope = iter->savedScope;
402*1b191cb5SApple OSS Distributions 	newScope->scope = iter->currentScope;
403*1b191cb5SApple OSS Distributions 	newScope->entry = iter->currentEntry;
404*1b191cb5SApple OSS Distributions 	newScope->index = iter->currentIndex;
405*1b191cb5SApple OSS Distributions 
406*1b191cb5SApple OSS Distributions 	iter->currentScope = childEntry;
407*1b191cb5SApple OSS Distributions 	iter->currentEntry = NULL;
408*1b191cb5SApple OSS Distributions 	iter->savedScope = newScope;
409*1b191cb5SApple OSS Distributions 	iter->currentIndex = 0;
410*1b191cb5SApple OSS Distributions 
411*1b191cb5SApple OSS Distributions 	return kSuccess;
412*1b191cb5SApple OSS Distributions }
413*1b191cb5SApple OSS Distributions 
414*1b191cb5SApple OSS Distributions int
SecureDTExitEntry(DTEntryIterator iter,DTEntry * currentPosition)415*1b191cb5SApple OSS Distributions SecureDTExitEntry(DTEntryIterator iter, DTEntry *currentPosition)
416*1b191cb5SApple OSS Distributions {
417*1b191cb5SApple OSS Distributions 	DTSavedScopePtr newScope;
418*1b191cb5SApple OSS Distributions 
419*1b191cb5SApple OSS Distributions 	newScope = iter->savedScope;
420*1b191cb5SApple OSS Distributions 	if (newScope == NULL) {
421*1b191cb5SApple OSS Distributions 		return kError;
422*1b191cb5SApple OSS Distributions 	}
423*1b191cb5SApple OSS Distributions 	iter->savedScope = newScope->nextScope;
424*1b191cb5SApple OSS Distributions 	iter->currentScope = newScope->scope;
425*1b191cb5SApple OSS Distributions 	iter->currentEntry = newScope->entry;
426*1b191cb5SApple OSS Distributions 	iter->currentIndex = newScope->index;
427*1b191cb5SApple OSS Distributions 	*currentPosition = iter->currentEntry;
428*1b191cb5SApple OSS Distributions 
429*1b191cb5SApple OSS Distributions 	kfree_type(struct DTSavedScope, newScope);
430*1b191cb5SApple OSS Distributions 
431*1b191cb5SApple OSS Distributions 	return kSuccess;
432*1b191cb5SApple OSS Distributions }
433*1b191cb5SApple OSS Distributions 
434*1b191cb5SApple OSS Distributions int
SecureDTIterateEntries(DTEntryIterator iter,DTEntry * nextEntry)435*1b191cb5SApple OSS Distributions SecureDTIterateEntries(DTEntryIterator iter, DTEntry *nextEntry)
436*1b191cb5SApple OSS Distributions {
437*1b191cb5SApple OSS Distributions 	if (iter->currentIndex >= iter->currentScope->nChildren) {
438*1b191cb5SApple OSS Distributions 		*nextEntry = NULL;
439*1b191cb5SApple OSS Distributions 		return kIterationDone;
440*1b191cb5SApple OSS Distributions 	} else {
441*1b191cb5SApple OSS Distributions 		iter->currentIndex++;
442*1b191cb5SApple OSS Distributions 		if (iter->currentIndex == 1) {
443*1b191cb5SApple OSS Distributions 			iter->currentEntry = GetFirstChild(iter->currentScope);
444*1b191cb5SApple OSS Distributions 		} else {
445*1b191cb5SApple OSS Distributions 			iter->currentEntry = GetNextChild(iter->currentEntry);
446*1b191cb5SApple OSS Distributions 		}
447*1b191cb5SApple OSS Distributions 		ASSERT_IN_DT(iter->currentEntry);
448*1b191cb5SApple OSS Distributions 		*nextEntry = iter->currentEntry;
449*1b191cb5SApple OSS Distributions 		return kSuccess;
450*1b191cb5SApple OSS Distributions 	}
451*1b191cb5SApple OSS Distributions }
452*1b191cb5SApple OSS Distributions 
453*1b191cb5SApple OSS Distributions int
SecureDTRestartEntryIteration(DTEntryIterator iter)454*1b191cb5SApple OSS Distributions SecureDTRestartEntryIteration(DTEntryIterator iter)
455*1b191cb5SApple OSS Distributions {
456*1b191cb5SApple OSS Distributions #if 0
457*1b191cb5SApple OSS Distributions 	// This commented out code allows a second argument (outer)
458*1b191cb5SApple OSS Distributions 	// which (if true) causes restarting at the outer scope
459*1b191cb5SApple OSS Distributions 	// rather than the current scope.
460*1b191cb5SApple OSS Distributions 	DTSavedScopePtr scope;
461*1b191cb5SApple OSS Distributions 
462*1b191cb5SApple OSS Distributions 	if (outer) {
463*1b191cb5SApple OSS Distributions 		while ((scope = iter->savedScope) != NULL) {
464*1b191cb5SApple OSS Distributions 			iter->savedScope = scope->nextScope;
465*1b191cb5SApple OSS Distributions 			kfree_type(struct DTSavedScope, scope);
466*1b191cb5SApple OSS Distributions 		}
467*1b191cb5SApple OSS Distributions 		iter->currentScope = iter->outerScope;
468*1b191cb5SApple OSS Distributions 	}
469*1b191cb5SApple OSS Distributions #endif
470*1b191cb5SApple OSS Distributions 	iter->currentEntry = NULL;
471*1b191cb5SApple OSS Distributions 	iter->currentIndex = 0;
472*1b191cb5SApple OSS Distributions 	return kSuccess;
473*1b191cb5SApple OSS Distributions }
474*1b191cb5SApple OSS Distributions 
475*1b191cb5SApple OSS Distributions static int
SecureDTGetPropertyInternal(const DTEntry entry,const char * propertyName,void const ** propertyValue,unsigned int * propertySize,vm_offset_t const region_start,vm_size_t region_size)476*1b191cb5SApple OSS Distributions SecureDTGetPropertyInternal(const DTEntry entry, const char *propertyName, void const **propertyValue, unsigned int *propertySize, vm_offset_t const region_start, vm_size_t region_size)
477*1b191cb5SApple OSS Distributions {
478*1b191cb5SApple OSS Distributions 	DeviceTreeNodeProperty const *prop;
479*1b191cb5SApple OSS Distributions 	unsigned int k;
480*1b191cb5SApple OSS Distributions 
481*1b191cb5SApple OSS Distributions 	if (entry == NULL) {
482*1b191cb5SApple OSS Distributions 		return kError;
483*1b191cb5SApple OSS Distributions 	}
484*1b191cb5SApple OSS Distributions 
485*1b191cb5SApple OSS Distributions 	ASSERT_HEADER_IN_DT_REGION(region_start, region_start + region_size, entry, sizeof(DeviceTreeNode));
486*1b191cb5SApple OSS Distributions 
487*1b191cb5SApple OSS Distributions 	if (entry->nProperties == 0) {
488*1b191cb5SApple OSS Distributions 		return kError;
489*1b191cb5SApple OSS Distributions 	} else {
490*1b191cb5SApple OSS Distributions 		prop = (DeviceTreeNodeProperty const *) (entry + 1);
491*1b191cb5SApple OSS Distributions 		for (k = 0; k < entry->nProperties; k++) {
492*1b191cb5SApple OSS Distributions 			assert_prop_in_dt_region(region_start, region_start + region_size, prop);
493*1b191cb5SApple OSS Distributions 			if (strcmp(prop->name, propertyName) == 0) {
494*1b191cb5SApple OSS Distributions 				*propertyValue = (void const *) (((uintptr_t)prop)
495*1b191cb5SApple OSS Distributions 				    + sizeof(DeviceTreeNodeProperty));
496*1b191cb5SApple OSS Distributions 				*propertySize = prop->length;
497*1b191cb5SApple OSS Distributions 				return kSuccess;
498*1b191cb5SApple OSS Distributions 			}
499*1b191cb5SApple OSS Distributions 			prop = next_prop_region(region_start, region_start + region_size, prop);
500*1b191cb5SApple OSS Distributions 		}
501*1b191cb5SApple OSS Distributions 	}
502*1b191cb5SApple OSS Distributions 	return kError;
503*1b191cb5SApple OSS Distributions }
504*1b191cb5SApple OSS Distributions 
505*1b191cb5SApple OSS Distributions int
SecureDTGetProperty(const DTEntry entry,const char * propertyName,void const ** propertyValue,unsigned int * propertySize)506*1b191cb5SApple OSS Distributions SecureDTGetProperty(const DTEntry entry, const char *propertyName, void const **propertyValue, unsigned int *propertySize)
507*1b191cb5SApple OSS Distributions {
508*1b191cb5SApple OSS Distributions 	return SecureDTGetPropertyInternal(entry, propertyName, propertyValue, propertySize,
509*1b191cb5SApple OSS Distributions 	           (vm_offset_t)DTRootNode, (vm_size_t)((uintptr_t)DTEnd - (uintptr_t)DTRootNode));
510*1b191cb5SApple OSS Distributions }
511*1b191cb5SApple OSS Distributions 
512*1b191cb5SApple OSS Distributions int
SecureDTGetPropertyRegion(const DTEntry entry,const char * propertyName,void const ** propertyValue,unsigned int * propertySize,vm_offset_t const region_start,vm_size_t region_size)513*1b191cb5SApple OSS Distributions SecureDTGetPropertyRegion(const DTEntry entry, const char *propertyName, void const **propertyValue, unsigned int *propertySize, vm_offset_t const region_start, vm_size_t region_size)
514*1b191cb5SApple OSS Distributions {
515*1b191cb5SApple OSS Distributions 	return SecureDTGetPropertyInternal(entry, propertyName, propertyValue, propertySize,
516*1b191cb5SApple OSS Distributions 	           region_start, region_size);
517*1b191cb5SApple OSS Distributions }
518*1b191cb5SApple OSS Distributions 
519*1b191cb5SApple OSS Distributions 
520*1b191cb5SApple OSS Distributions int
SecureDTInitPropertyIterator(const DTEntry entry,DTPropertyIterator iter)521*1b191cb5SApple OSS Distributions SecureDTInitPropertyIterator(const DTEntry entry, DTPropertyIterator iter)
522*1b191cb5SApple OSS Distributions {
523*1b191cb5SApple OSS Distributions 	iter->entry = entry;
524*1b191cb5SApple OSS Distributions 	iter->currentProperty = NULL;
525*1b191cb5SApple OSS Distributions 	iter->currentIndex = 0;
526*1b191cb5SApple OSS Distributions 	return kSuccess;
527*1b191cb5SApple OSS Distributions }
528*1b191cb5SApple OSS Distributions 
529*1b191cb5SApple OSS Distributions int
SecureDTIterateProperties(DTPropertyIterator iter,char const ** foundProperty)530*1b191cb5SApple OSS Distributions SecureDTIterateProperties(DTPropertyIterator iter, char const **foundProperty)
531*1b191cb5SApple OSS Distributions {
532*1b191cb5SApple OSS Distributions 	if (iter->currentIndex >= iter->entry->nProperties) {
533*1b191cb5SApple OSS Distributions 		*foundProperty = NULL;
534*1b191cb5SApple OSS Distributions 		return kIterationDone;
535*1b191cb5SApple OSS Distributions 	} else {
536*1b191cb5SApple OSS Distributions 		iter->currentIndex++;
537*1b191cb5SApple OSS Distributions 		if (iter->currentIndex == 1) {
538*1b191cb5SApple OSS Distributions 			iter->currentProperty = (DeviceTreeNodeProperty const *) (iter->entry + 1);
539*1b191cb5SApple OSS Distributions 		} else {
540*1b191cb5SApple OSS Distributions 			iter->currentProperty = next_prop(iter->currentProperty);
541*1b191cb5SApple OSS Distributions 		}
542*1b191cb5SApple OSS Distributions 		ASSERT_PROP_IN_DT(iter->currentProperty);
543*1b191cb5SApple OSS Distributions 		*foundProperty = iter->currentProperty->name;
544*1b191cb5SApple OSS Distributions 		return kSuccess;
545*1b191cb5SApple OSS Distributions 	}
546*1b191cb5SApple OSS Distributions }
547*1b191cb5SApple OSS Distributions 
548*1b191cb5SApple OSS Distributions int
SecureDTRestartPropertyIteration(DTPropertyIterator iter)549*1b191cb5SApple OSS Distributions SecureDTRestartPropertyIteration(DTPropertyIterator iter)
550*1b191cb5SApple OSS Distributions {
551*1b191cb5SApple OSS Distributions 	iter->currentProperty = NULL;
552*1b191cb5SApple OSS Distributions 	iter->currentIndex = 0;
553*1b191cb5SApple OSS Distributions 	return kSuccess;
554*1b191cb5SApple OSS Distributions }
555