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