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