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