1*5c2921b0SApple OSS Distributions /*
2*5c2921b0SApple OSS Distributions * Copyright (c) 2010 Apple Inc. All rights reserved.
3*5c2921b0SApple OSS Distributions *
4*5c2921b0SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5c2921b0SApple OSS Distributions *
6*5c2921b0SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*5c2921b0SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*5c2921b0SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*5c2921b0SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*5c2921b0SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*5c2921b0SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*5c2921b0SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*5c2921b0SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*5c2921b0SApple OSS Distributions *
15*5c2921b0SApple OSS Distributions * Please obtain a copy of the License at
16*5c2921b0SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5c2921b0SApple OSS Distributions *
18*5c2921b0SApple OSS Distributions * The Original Code and all software distributed under the License are
19*5c2921b0SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5c2921b0SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5c2921b0SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5c2921b0SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5c2921b0SApple OSS Distributions * Please see the License for the specific language governing rights and
24*5c2921b0SApple OSS Distributions * limitations under the License.
25*5c2921b0SApple OSS Distributions *
26*5c2921b0SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5c2921b0SApple OSS Distributions */
28*5c2921b0SApple OSS Distributions
29*5c2921b0SApple OSS Distributions #include <sys/cdefs.h>
30*5c2921b0SApple OSS Distributions #include <stdbool.h>
31*5c2921b0SApple OSS Distributions
32*5c2921b0SApple OSS Distributions #include <IOKit/assert.h>
33*5c2921b0SApple OSS Distributions #include <IOKit/system.h>
34*5c2921b0SApple OSS Distributions #include <IOKit/IOLib.h>
35*5c2921b0SApple OSS Distributions #include <IOKit/IOMemoryDescriptor.h>
36*5c2921b0SApple OSS Distributions #include <IOKit/IOKitDebug.h>
37*5c2921b0SApple OSS Distributions
38*5c2921b0SApple OSS Distributions __BEGIN_DECLS
39*5c2921b0SApple OSS Distributions
40*5c2921b0SApple OSS Distributions #include <pexpert/pexpert.h>
41*5c2921b0SApple OSS Distributions
42*5c2921b0SApple OSS Distributions static volatile UInt32 alreadyFetched = 0;
43*5c2921b0SApple OSS Distributions static IOMemoryDescriptor * newData;
44*5c2921b0SApple OSS Distributions
45*5c2921b0SApple OSS Distributions IOMemoryDescriptor *
46*5c2921b0SApple OSS Distributions IOGetBootKeyStoreData(void);
47*5c2921b0SApple OSS Distributions void
48*5c2921b0SApple OSS Distributions IOSetKeyStoreData(IOMemoryDescriptor * data);
49*5c2921b0SApple OSS Distributions
50*5c2921b0SApple OSS Distributions // APFS
51*5c2921b0SApple OSS Distributions static volatile UInt32 apfsKeyFetched = 0;
52*5c2921b0SApple OSS Distributions static IOMemoryDescriptor* apfsKeyData = NULL;
53*5c2921b0SApple OSS Distributions
54*5c2921b0SApple OSS Distributions IOMemoryDescriptor* IOGetAPFSKeyStoreData();
55*5c2921b0SApple OSS Distributions void IOSetAPFSKeyStoreData(IOMemoryDescriptor* data);
56*5c2921b0SApple OSS Distributions
57*5c2921b0SApple OSS Distributions static volatile UInt32 ARVRootHashFetched = 0;
58*5c2921b0SApple OSS Distributions static volatile UInt32 bsARVRootHashFetched = 0;
59*5c2921b0SApple OSS Distributions
60*5c2921b0SApple OSS Distributions IOMemoryDescriptor* IOGetARVRootHashData(void);
61*5c2921b0SApple OSS Distributions IOMemoryDescriptor* IOGetBaseSystemARVRootHashData(void);
62*5c2921b0SApple OSS Distributions
63*5c2921b0SApple OSS Distributions bool IOBaseSystemARVRootHashAvailable(void);
64*5c2921b0SApple OSS Distributions
65*5c2921b0SApple OSS Distributions static volatile UInt32 ARVManifestFetched = 0;
66*5c2921b0SApple OSS Distributions static volatile UInt32 bsARVManifestFetched = 0;
67*5c2921b0SApple OSS Distributions
68*5c2921b0SApple OSS Distributions IOMemoryDescriptor* IOGetARVManifestData(void);
69*5c2921b0SApple OSS Distributions IOMemoryDescriptor* IOGetBaseSystemARVManifestData(void);
70*5c2921b0SApple OSS Distributions
71*5c2921b0SApple OSS Distributions __END_DECLS
72*5c2921b0SApple OSS Distributions
73*5c2921b0SApple OSS Distributions #if 1
74*5c2921b0SApple OSS Distributions #define DEBG(fmt, args...) { kprintf(fmt, ## args); }
75*5c2921b0SApple OSS Distributions #else
76*5c2921b0SApple OSS Distributions #define DEBG(fmt, args...) {}
77*5c2921b0SApple OSS Distributions #endif
78*5c2921b0SApple OSS Distributions
79*5c2921b0SApple OSS Distributions void
IOSetKeyStoreData(IOMemoryDescriptor * data)80*5c2921b0SApple OSS Distributions IOSetKeyStoreData(IOMemoryDescriptor * data)
81*5c2921b0SApple OSS Distributions {
82*5c2921b0SApple OSS Distributions newData = data;
83*5c2921b0SApple OSS Distributions alreadyFetched = 0;
84*5c2921b0SApple OSS Distributions }
85*5c2921b0SApple OSS Distributions
86*5c2921b0SApple OSS Distributions IOMemoryDescriptor *
IOGetBootKeyStoreData(void)87*5c2921b0SApple OSS Distributions IOGetBootKeyStoreData(void)
88*5c2921b0SApple OSS Distributions {
89*5c2921b0SApple OSS Distributions IOMemoryDescriptor *memoryDescriptor;
90*5c2921b0SApple OSS Distributions boot_args *args = (boot_args *)PE_state.bootArgs;
91*5c2921b0SApple OSS Distributions IOOptionBits options;
92*5c2921b0SApple OSS Distributions IOAddressRange ranges;
93*5c2921b0SApple OSS Distributions
94*5c2921b0SApple OSS Distributions if (!OSCompareAndSwap(0, 1, &alreadyFetched)) {
95*5c2921b0SApple OSS Distributions return NULL;
96*5c2921b0SApple OSS Distributions }
97*5c2921b0SApple OSS Distributions
98*5c2921b0SApple OSS Distributions if (newData) {
99*5c2921b0SApple OSS Distributions IOMemoryDescriptor * data = newData;
100*5c2921b0SApple OSS Distributions newData = NULL;
101*5c2921b0SApple OSS Distributions return data;
102*5c2921b0SApple OSS Distributions }
103*5c2921b0SApple OSS Distributions
104*5c2921b0SApple OSS Distributions DEBG("%s: data at address %u size %u\n", __func__,
105*5c2921b0SApple OSS Distributions args->keyStoreDataStart,
106*5c2921b0SApple OSS Distributions args->keyStoreDataSize);
107*5c2921b0SApple OSS Distributions
108*5c2921b0SApple OSS Distributions if (args->keyStoreDataStart == 0) {
109*5c2921b0SApple OSS Distributions return NULL;
110*5c2921b0SApple OSS Distributions }
111*5c2921b0SApple OSS Distributions
112*5c2921b0SApple OSS Distributions ranges.address = args->keyStoreDataStart;
113*5c2921b0SApple OSS Distributions ranges.length = args->keyStoreDataSize;
114*5c2921b0SApple OSS Distributions
115*5c2921b0SApple OSS Distributions options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
116*5c2921b0SApple OSS Distributions
117*5c2921b0SApple OSS Distributions memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges,
118*5c2921b0SApple OSS Distributions 1,
119*5c2921b0SApple OSS Distributions 0,
120*5c2921b0SApple OSS Distributions NULL,
121*5c2921b0SApple OSS Distributions options);
122*5c2921b0SApple OSS Distributions
123*5c2921b0SApple OSS Distributions DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
124*5c2921b0SApple OSS Distributions
125*5c2921b0SApple OSS Distributions return memoryDescriptor;
126*5c2921b0SApple OSS Distributions }
127*5c2921b0SApple OSS Distributions
128*5c2921b0SApple OSS Distributions // APFS volume key fetcher
129*5c2921b0SApple OSS Distributions
130*5c2921b0SApple OSS Distributions // Store in-memory key (could be used by IOHibernateDone)
131*5c2921b0SApple OSS Distributions void
IOSetAPFSKeyStoreData(IOMemoryDescriptor * data)132*5c2921b0SApple OSS Distributions IOSetAPFSKeyStoreData(IOMemoryDescriptor* data)
133*5c2921b0SApple OSS Distributions {
134*5c2921b0SApple OSS Distributions // Do not allow re-fetching of the boot_args key by passing NULL here.
135*5c2921b0SApple OSS Distributions if (data != NULL) {
136*5c2921b0SApple OSS Distributions apfsKeyData = data;
137*5c2921b0SApple OSS Distributions apfsKeyFetched = 0;
138*5c2921b0SApple OSS Distributions }
139*5c2921b0SApple OSS Distributions }
140*5c2921b0SApple OSS Distributions
141*5c2921b0SApple OSS Distributions // Retrieve any key we may have (stored in boot_args or by Hibernate)
142*5c2921b0SApple OSS Distributions IOMemoryDescriptor*
IOGetAPFSKeyStoreData()143*5c2921b0SApple OSS Distributions IOGetAPFSKeyStoreData()
144*5c2921b0SApple OSS Distributions {
145*5c2921b0SApple OSS Distributions // Check if someone got the key before us
146*5c2921b0SApple OSS Distributions if (!OSCompareAndSwap(0, 1, &apfsKeyFetched)) {
147*5c2921b0SApple OSS Distributions return NULL;
148*5c2921b0SApple OSS Distributions }
149*5c2921b0SApple OSS Distributions
150*5c2921b0SApple OSS Distributions // Do we have in-memory key?
151*5c2921b0SApple OSS Distributions if (apfsKeyData) {
152*5c2921b0SApple OSS Distributions IOMemoryDescriptor* data = apfsKeyData;
153*5c2921b0SApple OSS Distributions apfsKeyData = NULL;
154*5c2921b0SApple OSS Distributions return data;
155*5c2921b0SApple OSS Distributions }
156*5c2921b0SApple OSS Distributions
157*5c2921b0SApple OSS Distributions // Looks like there was no in-memory key and it's the first call - try boot_args
158*5c2921b0SApple OSS Distributions boot_args* args = (boot_args*)PE_state.bootArgs;
159*5c2921b0SApple OSS Distributions
160*5c2921b0SApple OSS Distributions DEBG("%s: data at address %u size %u\n", __func__, args->apfsDataStart, args->apfsDataSize);
161*5c2921b0SApple OSS Distributions if (args->apfsDataStart == 0) {
162*5c2921b0SApple OSS Distributions return NULL;
163*5c2921b0SApple OSS Distributions }
164*5c2921b0SApple OSS Distributions
165*5c2921b0SApple OSS Distributions // We have the key in the boot_args, create IOMemoryDescriptor for the blob
166*5c2921b0SApple OSS Distributions IOAddressRange ranges;
167*5c2921b0SApple OSS Distributions ranges.address = args->apfsDataStart;
168*5c2921b0SApple OSS Distributions ranges.length = args->apfsDataSize;
169*5c2921b0SApple OSS Distributions
170*5c2921b0SApple OSS Distributions const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
171*5c2921b0SApple OSS Distributions
172*5c2921b0SApple OSS Distributions IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
173*5c2921b0SApple OSS Distributions DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
174*5c2921b0SApple OSS Distributions return memoryDescriptor;
175*5c2921b0SApple OSS Distributions }
176*5c2921b0SApple OSS Distributions
177*5c2921b0SApple OSS Distributions // ARV Root Hash fetcher
178*5c2921b0SApple OSS Distributions
179*5c2921b0SApple OSS Distributions // Retrieve any root hash we may have (stored in boot_args)
180*5c2921b0SApple OSS Distributions IOMemoryDescriptor*
IOGetARVRootHashData(void)181*5c2921b0SApple OSS Distributions IOGetARVRootHashData(void)
182*5c2921b0SApple OSS Distributions {
183*5c2921b0SApple OSS Distributions // Check if someone got the root hash before us
184*5c2921b0SApple OSS Distributions if (!OSCompareAndSwap(0, 1, &ARVRootHashFetched)) {
185*5c2921b0SApple OSS Distributions return NULL;
186*5c2921b0SApple OSS Distributions }
187*5c2921b0SApple OSS Distributions
188*5c2921b0SApple OSS Distributions boot_args* args = (boot_args*)PE_state.bootArgs;
189*5c2921b0SApple OSS Distributions
190*5c2921b0SApple OSS Distributions DEBG("%s: data at address %llu size %llu\n", __func__, args->arvRootHashStart, args->arvRootHashSize);
191*5c2921b0SApple OSS Distributions if (args->arvRootHashStart == 0) {
192*5c2921b0SApple OSS Distributions return NULL;
193*5c2921b0SApple OSS Distributions }
194*5c2921b0SApple OSS Distributions
195*5c2921b0SApple OSS Distributions // We have the root hash in the boot_args, create IOMemoryDescriptor for the blob
196*5c2921b0SApple OSS Distributions IOAddressRange ranges;
197*5c2921b0SApple OSS Distributions ranges.address = args->arvRootHashStart;
198*5c2921b0SApple OSS Distributions ranges.length = args->arvRootHashSize;
199*5c2921b0SApple OSS Distributions
200*5c2921b0SApple OSS Distributions const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
201*5c2921b0SApple OSS Distributions
202*5c2921b0SApple OSS Distributions IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
203*5c2921b0SApple OSS Distributions DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
204*5c2921b0SApple OSS Distributions return memoryDescriptor;
205*5c2921b0SApple OSS Distributions }
206*5c2921b0SApple OSS Distributions
207*5c2921b0SApple OSS Distributions // Base System Analogue
208*5c2921b0SApple OSS Distributions
209*5c2921b0SApple OSS Distributions IOMemoryDescriptor*
IOGetBaseSystemARVRootHashData(void)210*5c2921b0SApple OSS Distributions IOGetBaseSystemARVRootHashData(void)
211*5c2921b0SApple OSS Distributions {
212*5c2921b0SApple OSS Distributions // Check if someone got the base system root hash before us
213*5c2921b0SApple OSS Distributions if (!OSCompareAndSwap(0, 1, &bsARVRootHashFetched)) {
214*5c2921b0SApple OSS Distributions return NULL;
215*5c2921b0SApple OSS Distributions }
216*5c2921b0SApple OSS Distributions
217*5c2921b0SApple OSS Distributions boot_args* args = (boot_args*)PE_state.bootArgs;
218*5c2921b0SApple OSS Distributions
219*5c2921b0SApple OSS Distributions DEBG("%s: data at address %llu size %llu\n", __func__, args->bsARVRootHashStart, args->bsARVRootHashSize);
220*5c2921b0SApple OSS Distributions if (args->bsARVRootHashStart == 0) {
221*5c2921b0SApple OSS Distributions return NULL;
222*5c2921b0SApple OSS Distributions }
223*5c2921b0SApple OSS Distributions
224*5c2921b0SApple OSS Distributions // We have the base system root hash in the boot_args, create IOMemoryDescriptor for the blob
225*5c2921b0SApple OSS Distributions IOAddressRange ranges;
226*5c2921b0SApple OSS Distributions ranges.address = args->bsARVRootHashStart;
227*5c2921b0SApple OSS Distributions ranges.length = args->bsARVRootHashSize;
228*5c2921b0SApple OSS Distributions
229*5c2921b0SApple OSS Distributions const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
230*5c2921b0SApple OSS Distributions
231*5c2921b0SApple OSS Distributions IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
232*5c2921b0SApple OSS Distributions DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
233*5c2921b0SApple OSS Distributions return memoryDescriptor;
234*5c2921b0SApple OSS Distributions }
235*5c2921b0SApple OSS Distributions
236*5c2921b0SApple OSS Distributions bool
IOBaseSystemARVRootHashAvailable(void)237*5c2921b0SApple OSS Distributions IOBaseSystemARVRootHashAvailable(void)
238*5c2921b0SApple OSS Distributions {
239*5c2921b0SApple OSS Distributions boot_args* args = (boot_args*)PE_state.bootArgs;
240*5c2921b0SApple OSS Distributions
241*5c2921b0SApple OSS Distributions if (args->bsARVRootHashStart == 0 || args->bsARVRootHashSize == 0) {
242*5c2921b0SApple OSS Distributions return false;
243*5c2921b0SApple OSS Distributions }
244*5c2921b0SApple OSS Distributions
245*5c2921b0SApple OSS Distributions if (args->bsARVManifestStart == 0 || args->bsARVManifestSize == 0) {
246*5c2921b0SApple OSS Distributions return false;
247*5c2921b0SApple OSS Distributions }
248*5c2921b0SApple OSS Distributions
249*5c2921b0SApple OSS Distributions return true;
250*5c2921b0SApple OSS Distributions }
251*5c2921b0SApple OSS Distributions
252*5c2921b0SApple OSS Distributions // ARV Manifest fetcher
253*5c2921b0SApple OSS Distributions
254*5c2921b0SApple OSS Distributions // Retrieve any manifest we may have (stored in boot_args)
255*5c2921b0SApple OSS Distributions IOMemoryDescriptor*
IOGetARVManifestData(void)256*5c2921b0SApple OSS Distributions IOGetARVManifestData(void)
257*5c2921b0SApple OSS Distributions {
258*5c2921b0SApple OSS Distributions // Check if someone got the manifest before us
259*5c2921b0SApple OSS Distributions if (!OSCompareAndSwap(0, 1, &ARVManifestFetched)) {
260*5c2921b0SApple OSS Distributions return NULL;
261*5c2921b0SApple OSS Distributions }
262*5c2921b0SApple OSS Distributions
263*5c2921b0SApple OSS Distributions boot_args* args = (boot_args*)PE_state.bootArgs;
264*5c2921b0SApple OSS Distributions
265*5c2921b0SApple OSS Distributions DEBG("%s: data at address %llu size %llu\n", __func__, args->arvManifestStart, args->arvManifestSize);
266*5c2921b0SApple OSS Distributions if (args->arvManifestStart == 0) {
267*5c2921b0SApple OSS Distributions return NULL;
268*5c2921b0SApple OSS Distributions }
269*5c2921b0SApple OSS Distributions
270*5c2921b0SApple OSS Distributions // We have the manifest in the boot_args, create IOMemoryDescriptor for the blob
271*5c2921b0SApple OSS Distributions IOAddressRange ranges;
272*5c2921b0SApple OSS Distributions ranges.address = args->arvManifestStart;
273*5c2921b0SApple OSS Distributions ranges.length = args->arvManifestSize;
274*5c2921b0SApple OSS Distributions
275*5c2921b0SApple OSS Distributions const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
276*5c2921b0SApple OSS Distributions
277*5c2921b0SApple OSS Distributions IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
278*5c2921b0SApple OSS Distributions DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
279*5c2921b0SApple OSS Distributions return memoryDescriptor;
280*5c2921b0SApple OSS Distributions }
281*5c2921b0SApple OSS Distributions
282*5c2921b0SApple OSS Distributions // Base System Analogue
283*5c2921b0SApple OSS Distributions
284*5c2921b0SApple OSS Distributions IOMemoryDescriptor*
IOGetBaseSystemARVManifestData(void)285*5c2921b0SApple OSS Distributions IOGetBaseSystemARVManifestData(void)
286*5c2921b0SApple OSS Distributions {
287*5c2921b0SApple OSS Distributions // Check if someone got the base system manifest before us
288*5c2921b0SApple OSS Distributions if (!OSCompareAndSwap(0, 1, &bsARVManifestFetched)) {
289*5c2921b0SApple OSS Distributions return NULL;
290*5c2921b0SApple OSS Distributions }
291*5c2921b0SApple OSS Distributions
292*5c2921b0SApple OSS Distributions boot_args* args = (boot_args*)PE_state.bootArgs;
293*5c2921b0SApple OSS Distributions
294*5c2921b0SApple OSS Distributions DEBG("%s: data at address %llu size %llu\n", __func__, args->bsARVManifestStart, args->bsARVManifestSize);
295*5c2921b0SApple OSS Distributions if (args->bsARVManifestStart == 0) {
296*5c2921b0SApple OSS Distributions return NULL;
297*5c2921b0SApple OSS Distributions }
298*5c2921b0SApple OSS Distributions
299*5c2921b0SApple OSS Distributions // We have the manifest in the boot_args, create IOMemoryDescriptor for the blob
300*5c2921b0SApple OSS Distributions IOAddressRange ranges;
301*5c2921b0SApple OSS Distributions ranges.address = args->bsARVManifestStart;
302*5c2921b0SApple OSS Distributions ranges.length = args->bsARVManifestSize;
303*5c2921b0SApple OSS Distributions
304*5c2921b0SApple OSS Distributions const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
305*5c2921b0SApple OSS Distributions
306*5c2921b0SApple OSS Distributions IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
307*5c2921b0SApple OSS Distributions DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
308*5c2921b0SApple OSS Distributions return memoryDescriptor;
309*5c2921b0SApple OSS Distributions }
310