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