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