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