1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions * Copyright (c) 2010 Apple 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 #include <sys/cdefs.h>
30*c54f35caSApple OSS Distributions #include <stdbool.h>
31*c54f35caSApple OSS Distributions
32*c54f35caSApple OSS Distributions #include <IOKit/assert.h>
33*c54f35caSApple OSS Distributions #include <IOKit/system.h>
34*c54f35caSApple OSS Distributions #include <IOKit/IOLib.h>
35*c54f35caSApple OSS Distributions #include <IOKit/IOMemoryDescriptor.h>
36*c54f35caSApple OSS Distributions #include <IOKit/IOKitDebug.h>
37*c54f35caSApple OSS Distributions
38*c54f35caSApple OSS Distributions __BEGIN_DECLS
39*c54f35caSApple OSS Distributions
40*c54f35caSApple OSS Distributions #include <pexpert/pexpert.h>
41*c54f35caSApple OSS Distributions
42*c54f35caSApple OSS Distributions static volatile UInt32 alreadyFetched = 0;
43*c54f35caSApple OSS Distributions static IOMemoryDescriptor * newData;
44*c54f35caSApple OSS Distributions
45*c54f35caSApple OSS Distributions IOMemoryDescriptor *
46*c54f35caSApple OSS Distributions IOGetBootKeyStoreData(void);
47*c54f35caSApple OSS Distributions void
48*c54f35caSApple OSS Distributions IOSetKeyStoreData(IOMemoryDescriptor * data);
49*c54f35caSApple OSS Distributions
50*c54f35caSApple OSS Distributions // APFS
51*c54f35caSApple OSS Distributions static volatile UInt32 apfsKeyFetched = 0;
52*c54f35caSApple OSS Distributions static IOMemoryDescriptor* apfsKeyData = NULL;
53*c54f35caSApple OSS Distributions
54*c54f35caSApple OSS Distributions IOMemoryDescriptor* IOGetAPFSKeyStoreData();
55*c54f35caSApple OSS Distributions void IOSetAPFSKeyStoreData(IOMemoryDescriptor* data);
56*c54f35caSApple OSS Distributions
57*c54f35caSApple OSS Distributions static volatile UInt32 ARVRootHashFetched = 0;
58*c54f35caSApple OSS Distributions static volatile UInt32 bsARVRootHashFetched = 0;
59*c54f35caSApple OSS Distributions
60*c54f35caSApple OSS Distributions IOMemoryDescriptor* IOGetARVRootHashData(void);
61*c54f35caSApple OSS Distributions IOMemoryDescriptor* IOGetBaseSystemARVRootHashData(void);
62*c54f35caSApple OSS Distributions
63*c54f35caSApple OSS Distributions bool IOBaseSystemARVRootHashAvailable(void);
64*c54f35caSApple OSS Distributions
65*c54f35caSApple OSS Distributions static volatile UInt32 ARVManifestFetched = 0;
66*c54f35caSApple OSS Distributions static volatile UInt32 bsARVManifestFetched = 0;
67*c54f35caSApple OSS Distributions
68*c54f35caSApple OSS Distributions IOMemoryDescriptor* IOGetARVManifestData(void);
69*c54f35caSApple OSS Distributions IOMemoryDescriptor* IOGetBaseSystemARVManifestData(void);
70*c54f35caSApple OSS Distributions
71*c54f35caSApple OSS Distributions __END_DECLS
72*c54f35caSApple OSS Distributions
73*c54f35caSApple OSS Distributions #if 1
74*c54f35caSApple OSS Distributions #define DEBG(fmt, args...) { kprintf(fmt, ## args); }
75*c54f35caSApple OSS Distributions #else
76*c54f35caSApple OSS Distributions #define DEBG(fmt, args...) {}
77*c54f35caSApple OSS Distributions #endif
78*c54f35caSApple OSS Distributions
79*c54f35caSApple OSS Distributions void
IOSetKeyStoreData(IOMemoryDescriptor * data)80*c54f35caSApple OSS Distributions IOSetKeyStoreData(IOMemoryDescriptor * data)
81*c54f35caSApple OSS Distributions {
82*c54f35caSApple OSS Distributions newData = data;
83*c54f35caSApple OSS Distributions alreadyFetched = 0;
84*c54f35caSApple OSS Distributions }
85*c54f35caSApple OSS Distributions
86*c54f35caSApple OSS Distributions IOMemoryDescriptor *
IOGetBootKeyStoreData(void)87*c54f35caSApple OSS Distributions IOGetBootKeyStoreData(void)
88*c54f35caSApple OSS Distributions {
89*c54f35caSApple OSS Distributions IOMemoryDescriptor *memoryDescriptor;
90*c54f35caSApple OSS Distributions boot_args *args = (boot_args *)PE_state.bootArgs;
91*c54f35caSApple OSS Distributions IOOptionBits options;
92*c54f35caSApple OSS Distributions IOAddressRange ranges;
93*c54f35caSApple OSS Distributions
94*c54f35caSApple OSS Distributions if (!OSCompareAndSwap(0, 1, &alreadyFetched)) {
95*c54f35caSApple OSS Distributions return NULL;
96*c54f35caSApple OSS Distributions }
97*c54f35caSApple OSS Distributions
98*c54f35caSApple OSS Distributions if (newData) {
99*c54f35caSApple OSS Distributions IOMemoryDescriptor * data = newData;
100*c54f35caSApple OSS Distributions newData = NULL;
101*c54f35caSApple OSS Distributions return data;
102*c54f35caSApple OSS Distributions }
103*c54f35caSApple OSS Distributions
104*c54f35caSApple OSS Distributions DEBG("%s: data at address %u size %u\n", __func__,
105*c54f35caSApple OSS Distributions args->keyStoreDataStart,
106*c54f35caSApple OSS Distributions args->keyStoreDataSize);
107*c54f35caSApple OSS Distributions
108*c54f35caSApple OSS Distributions if (args->keyStoreDataStart == 0) {
109*c54f35caSApple OSS Distributions return NULL;
110*c54f35caSApple OSS Distributions }
111*c54f35caSApple OSS Distributions
112*c54f35caSApple OSS Distributions ranges.address = args->keyStoreDataStart;
113*c54f35caSApple OSS Distributions ranges.length = args->keyStoreDataSize;
114*c54f35caSApple OSS Distributions
115*c54f35caSApple OSS Distributions options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
116*c54f35caSApple OSS Distributions
117*c54f35caSApple OSS Distributions memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges,
118*c54f35caSApple OSS Distributions 1,
119*c54f35caSApple OSS Distributions 0,
120*c54f35caSApple OSS Distributions NULL,
121*c54f35caSApple OSS Distributions options);
122*c54f35caSApple OSS Distributions
123*c54f35caSApple OSS Distributions DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
124*c54f35caSApple OSS Distributions
125*c54f35caSApple OSS Distributions return memoryDescriptor;
126*c54f35caSApple OSS Distributions }
127*c54f35caSApple OSS Distributions
128*c54f35caSApple OSS Distributions // APFS volume key fetcher
129*c54f35caSApple OSS Distributions
130*c54f35caSApple OSS Distributions // Store in-memory key (could be used by IOHibernateDone)
131*c54f35caSApple OSS Distributions void
IOSetAPFSKeyStoreData(IOMemoryDescriptor * data)132*c54f35caSApple OSS Distributions IOSetAPFSKeyStoreData(IOMemoryDescriptor* data)
133*c54f35caSApple OSS Distributions {
134*c54f35caSApple OSS Distributions // Do not allow re-fetching of the boot_args key by passing NULL here.
135*c54f35caSApple OSS Distributions if (data != NULL) {
136*c54f35caSApple OSS Distributions apfsKeyData = data;
137*c54f35caSApple OSS Distributions apfsKeyFetched = 0;
138*c54f35caSApple OSS Distributions }
139*c54f35caSApple OSS Distributions }
140*c54f35caSApple OSS Distributions
141*c54f35caSApple OSS Distributions // Retrieve any key we may have (stored in boot_args or by Hibernate)
142*c54f35caSApple OSS Distributions IOMemoryDescriptor*
IOGetAPFSKeyStoreData()143*c54f35caSApple OSS Distributions IOGetAPFSKeyStoreData()
144*c54f35caSApple OSS Distributions {
145*c54f35caSApple OSS Distributions // Check if someone got the key before us
146*c54f35caSApple OSS Distributions if (!OSCompareAndSwap(0, 1, &apfsKeyFetched)) {
147*c54f35caSApple OSS Distributions return NULL;
148*c54f35caSApple OSS Distributions }
149*c54f35caSApple OSS Distributions
150*c54f35caSApple OSS Distributions // Do we have in-memory key?
151*c54f35caSApple OSS Distributions if (apfsKeyData) {
152*c54f35caSApple OSS Distributions IOMemoryDescriptor* data = apfsKeyData;
153*c54f35caSApple OSS Distributions apfsKeyData = NULL;
154*c54f35caSApple OSS Distributions return data;
155*c54f35caSApple OSS Distributions }
156*c54f35caSApple OSS Distributions
157*c54f35caSApple OSS Distributions // Looks like there was no in-memory key and it's the first call - try boot_args
158*c54f35caSApple OSS Distributions boot_args* args = (boot_args*)PE_state.bootArgs;
159*c54f35caSApple OSS Distributions
160*c54f35caSApple OSS Distributions DEBG("%s: data at address %u size %u\n", __func__, args->apfsDataStart, args->apfsDataSize);
161*c54f35caSApple OSS Distributions if (args->apfsDataStart == 0) {
162*c54f35caSApple OSS Distributions return NULL;
163*c54f35caSApple OSS Distributions }
164*c54f35caSApple OSS Distributions
165*c54f35caSApple OSS Distributions // We have the key in the boot_args, create IOMemoryDescriptor for the blob
166*c54f35caSApple OSS Distributions IOAddressRange ranges;
167*c54f35caSApple OSS Distributions ranges.address = args->apfsDataStart;
168*c54f35caSApple OSS Distributions ranges.length = args->apfsDataSize;
169*c54f35caSApple OSS Distributions
170*c54f35caSApple OSS Distributions const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
171*c54f35caSApple OSS Distributions
172*c54f35caSApple OSS Distributions IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
173*c54f35caSApple OSS Distributions DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
174*c54f35caSApple OSS Distributions return memoryDescriptor;
175*c54f35caSApple OSS Distributions }
176*c54f35caSApple OSS Distributions
177*c54f35caSApple OSS Distributions // ARV Root Hash fetcher
178*c54f35caSApple OSS Distributions
179*c54f35caSApple OSS Distributions // Retrieve any root hash we may have (stored in boot_args)
180*c54f35caSApple OSS Distributions IOMemoryDescriptor*
IOGetARVRootHashData(void)181*c54f35caSApple OSS Distributions IOGetARVRootHashData(void)
182*c54f35caSApple OSS Distributions {
183*c54f35caSApple OSS Distributions // Check if someone got the root hash before us
184*c54f35caSApple OSS Distributions if (!OSCompareAndSwap(0, 1, &ARVRootHashFetched)) {
185*c54f35caSApple OSS Distributions return NULL;
186*c54f35caSApple OSS Distributions }
187*c54f35caSApple OSS Distributions
188*c54f35caSApple OSS Distributions boot_args* args = (boot_args*)PE_state.bootArgs;
189*c54f35caSApple OSS Distributions
190*c54f35caSApple OSS Distributions DEBG("%s: data at address %llu size %llu\n", __func__, args->arvRootHashStart, args->arvRootHashSize);
191*c54f35caSApple OSS Distributions if (args->arvRootHashStart == 0) {
192*c54f35caSApple OSS Distributions return NULL;
193*c54f35caSApple OSS Distributions }
194*c54f35caSApple OSS Distributions
195*c54f35caSApple OSS Distributions // We have the root hash in the boot_args, create IOMemoryDescriptor for the blob
196*c54f35caSApple OSS Distributions IOAddressRange ranges;
197*c54f35caSApple OSS Distributions ranges.address = args->arvRootHashStart;
198*c54f35caSApple OSS Distributions ranges.length = args->arvRootHashSize;
199*c54f35caSApple OSS Distributions
200*c54f35caSApple OSS Distributions const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
201*c54f35caSApple OSS Distributions
202*c54f35caSApple OSS Distributions IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
203*c54f35caSApple OSS Distributions DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
204*c54f35caSApple OSS Distributions return memoryDescriptor;
205*c54f35caSApple OSS Distributions }
206*c54f35caSApple OSS Distributions
207*c54f35caSApple OSS Distributions // Base System Analogue
208*c54f35caSApple OSS Distributions
209*c54f35caSApple OSS Distributions IOMemoryDescriptor*
IOGetBaseSystemARVRootHashData(void)210*c54f35caSApple OSS Distributions IOGetBaseSystemARVRootHashData(void)
211*c54f35caSApple OSS Distributions {
212*c54f35caSApple OSS Distributions // Check if someone got the base system root hash before us
213*c54f35caSApple OSS Distributions if (!OSCompareAndSwap(0, 1, &bsARVRootHashFetched)) {
214*c54f35caSApple OSS Distributions return NULL;
215*c54f35caSApple OSS Distributions }
216*c54f35caSApple OSS Distributions
217*c54f35caSApple OSS Distributions boot_args* args = (boot_args*)PE_state.bootArgs;
218*c54f35caSApple OSS Distributions
219*c54f35caSApple OSS Distributions DEBG("%s: data at address %llu size %llu\n", __func__, args->bsARVRootHashStart, args->bsARVRootHashSize);
220*c54f35caSApple OSS Distributions if (args->bsARVRootHashStart == 0) {
221*c54f35caSApple OSS Distributions return NULL;
222*c54f35caSApple OSS Distributions }
223*c54f35caSApple OSS Distributions
224*c54f35caSApple OSS Distributions // We have the base system root hash in the boot_args, create IOMemoryDescriptor for the blob
225*c54f35caSApple OSS Distributions IOAddressRange ranges;
226*c54f35caSApple OSS Distributions ranges.address = args->bsARVRootHashStart;
227*c54f35caSApple OSS Distributions ranges.length = args->bsARVRootHashSize;
228*c54f35caSApple OSS Distributions
229*c54f35caSApple OSS Distributions const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
230*c54f35caSApple OSS Distributions
231*c54f35caSApple OSS Distributions IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
232*c54f35caSApple OSS Distributions DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
233*c54f35caSApple OSS Distributions return memoryDescriptor;
234*c54f35caSApple OSS Distributions }
235*c54f35caSApple OSS Distributions
236*c54f35caSApple OSS Distributions bool
IOBaseSystemARVRootHashAvailable(void)237*c54f35caSApple OSS Distributions IOBaseSystemARVRootHashAvailable(void)
238*c54f35caSApple OSS Distributions {
239*c54f35caSApple OSS Distributions boot_args* args = (boot_args*)PE_state.bootArgs;
240*c54f35caSApple OSS Distributions
241*c54f35caSApple OSS Distributions if (args->bsARVRootHashStart == 0 || args->bsARVRootHashSize == 0) {
242*c54f35caSApple OSS Distributions return false;
243*c54f35caSApple OSS Distributions }
244*c54f35caSApple OSS Distributions
245*c54f35caSApple OSS Distributions if (args->bsARVManifestStart == 0 || args->bsARVManifestSize == 0) {
246*c54f35caSApple OSS Distributions return false;
247*c54f35caSApple OSS Distributions }
248*c54f35caSApple OSS Distributions
249*c54f35caSApple OSS Distributions return true;
250*c54f35caSApple OSS Distributions }
251*c54f35caSApple OSS Distributions
252*c54f35caSApple OSS Distributions // ARV Manifest fetcher
253*c54f35caSApple OSS Distributions
254*c54f35caSApple OSS Distributions // Retrieve any manifest we may have (stored in boot_args)
255*c54f35caSApple OSS Distributions IOMemoryDescriptor*
IOGetARVManifestData(void)256*c54f35caSApple OSS Distributions IOGetARVManifestData(void)
257*c54f35caSApple OSS Distributions {
258*c54f35caSApple OSS Distributions // Check if someone got the manifest before us
259*c54f35caSApple OSS Distributions if (!OSCompareAndSwap(0, 1, &ARVManifestFetched)) {
260*c54f35caSApple OSS Distributions return NULL;
261*c54f35caSApple OSS Distributions }
262*c54f35caSApple OSS Distributions
263*c54f35caSApple OSS Distributions boot_args* args = (boot_args*)PE_state.bootArgs;
264*c54f35caSApple OSS Distributions
265*c54f35caSApple OSS Distributions DEBG("%s: data at address %llu size %llu\n", __func__, args->arvManifestStart, args->arvManifestSize);
266*c54f35caSApple OSS Distributions if (args->arvManifestStart == 0) {
267*c54f35caSApple OSS Distributions return NULL;
268*c54f35caSApple OSS Distributions }
269*c54f35caSApple OSS Distributions
270*c54f35caSApple OSS Distributions // We have the manifest in the boot_args, create IOMemoryDescriptor for the blob
271*c54f35caSApple OSS Distributions IOAddressRange ranges;
272*c54f35caSApple OSS Distributions ranges.address = args->arvManifestStart;
273*c54f35caSApple OSS Distributions ranges.length = args->arvManifestSize;
274*c54f35caSApple OSS Distributions
275*c54f35caSApple OSS Distributions const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
276*c54f35caSApple OSS Distributions
277*c54f35caSApple OSS Distributions IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
278*c54f35caSApple OSS Distributions DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
279*c54f35caSApple OSS Distributions return memoryDescriptor;
280*c54f35caSApple OSS Distributions }
281*c54f35caSApple OSS Distributions
282*c54f35caSApple OSS Distributions // Base System Analogue
283*c54f35caSApple OSS Distributions
284*c54f35caSApple OSS Distributions IOMemoryDescriptor*
IOGetBaseSystemARVManifestData(void)285*c54f35caSApple OSS Distributions IOGetBaseSystemARVManifestData(void)
286*c54f35caSApple OSS Distributions {
287*c54f35caSApple OSS Distributions // Check if someone got the base system manifest before us
288*c54f35caSApple OSS Distributions if (!OSCompareAndSwap(0, 1, &bsARVManifestFetched)) {
289*c54f35caSApple OSS Distributions return NULL;
290*c54f35caSApple OSS Distributions }
291*c54f35caSApple OSS Distributions
292*c54f35caSApple OSS Distributions boot_args* args = (boot_args*)PE_state.bootArgs;
293*c54f35caSApple OSS Distributions
294*c54f35caSApple OSS Distributions DEBG("%s: data at address %llu size %llu\n", __func__, args->bsARVManifestStart, args->bsARVManifestSize);
295*c54f35caSApple OSS Distributions if (args->bsARVManifestStart == 0) {
296*c54f35caSApple OSS Distributions return NULL;
297*c54f35caSApple OSS Distributions }
298*c54f35caSApple OSS Distributions
299*c54f35caSApple OSS Distributions // We have the manifest in the boot_args, create IOMemoryDescriptor for the blob
300*c54f35caSApple OSS Distributions IOAddressRange ranges;
301*c54f35caSApple OSS Distributions ranges.address = args->bsARVManifestStart;
302*c54f35caSApple OSS Distributions ranges.length = args->bsARVManifestSize;
303*c54f35caSApple OSS Distributions
304*c54f35caSApple OSS Distributions const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
305*c54f35caSApple OSS Distributions
306*c54f35caSApple OSS Distributions IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
307*c54f35caSApple OSS Distributions DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
308*c54f35caSApple OSS Distributions return memoryDescriptor;
309*c54f35caSApple OSS Distributions }
310