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