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