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