1855239e5SApple OSS Distributions /*
2855239e5SApple OSS Distributions  * Copyright (c) 2010 Apple Inc. All rights reserved.
3855239e5SApple OSS Distributions  *
4855239e5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5855239e5SApple OSS Distributions  *
6855239e5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7855239e5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8855239e5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9855239e5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10855239e5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11855239e5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12855239e5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13855239e5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14855239e5SApple OSS Distributions  *
15855239e5SApple OSS Distributions  * Please obtain a copy of the License at
16855239e5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17855239e5SApple OSS Distributions  *
18855239e5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19855239e5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20855239e5SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21855239e5SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22855239e5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23855239e5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24855239e5SApple OSS Distributions  * limitations under the License.
25855239e5SApple OSS Distributions  *
26855239e5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27855239e5SApple OSS Distributions  */
28855239e5SApple OSS Distributions 
29855239e5SApple OSS Distributions #include <sys/cdefs.h>
30855239e5SApple OSS Distributions #include <stdbool.h>
31855239e5SApple OSS Distributions 
32855239e5SApple OSS Distributions #include <IOKit/assert.h>
33855239e5SApple OSS Distributions #include <IOKit/system.h>
34855239e5SApple OSS Distributions #include <IOKit/IOLib.h>
35855239e5SApple OSS Distributions #include <IOKit/IOMemoryDescriptor.h>
36855239e5SApple OSS Distributions #include <IOKit/IOKitDebug.h>
37855239e5SApple OSS Distributions 
38855239e5SApple OSS Distributions __BEGIN_DECLS
39855239e5SApple OSS Distributions 
40855239e5SApple OSS Distributions #include <pexpert/pexpert.h>
41855239e5SApple OSS Distributions 
42855239e5SApple OSS Distributions static volatile UInt32 alreadyFetched = 0;
43855239e5SApple OSS Distributions static IOMemoryDescriptor * newData;
44855239e5SApple OSS Distributions 
45855239e5SApple OSS Distributions IOMemoryDescriptor *
46855239e5SApple OSS Distributions IOGetBootKeyStoreData(void);
47855239e5SApple OSS Distributions void
48855239e5SApple OSS Distributions IOSetKeyStoreData(IOMemoryDescriptor * data);
49855239e5SApple OSS Distributions 
5076e12aa3SApple OSS Distributions // APFS
5176e12aa3SApple OSS Distributions static volatile UInt32 apfsKeyFetched = 0;
5276e12aa3SApple OSS Distributions static IOMemoryDescriptor* apfsKeyData = NULL;
5376e12aa3SApple OSS Distributions 
5476e12aa3SApple OSS Distributions IOMemoryDescriptor* IOGetAPFSKeyStoreData();
5576e12aa3SApple OSS Distributions void IOSetAPFSKeyStoreData(IOMemoryDescriptor* data);
5676e12aa3SApple OSS Distributions 
57*e6231be0SApple OSS Distributions static volatile UInt32 ARVRootHashFetched = 0;
58bb611c8fSApple OSS Distributions static volatile UInt32 bsARVRootHashFetched = 0;
59bb611c8fSApple OSS Distributions 
60bb611c8fSApple OSS Distributions IOMemoryDescriptor* IOGetARVRootHashData(void);
61bb611c8fSApple OSS Distributions IOMemoryDescriptor* IOGetBaseSystemARVRootHashData(void);
62*e6231be0SApple OSS Distributions 
63bb611c8fSApple OSS Distributions bool IOBaseSystemARVRootHashAvailable(void);
64bb611c8fSApple OSS Distributions 
65*e6231be0SApple OSS Distributions static volatile UInt32 ARVManifestFetched = 0;
66*e6231be0SApple OSS Distributions static volatile UInt32 bsARVManifestFetched = 0;
67bb611c8fSApple OSS Distributions 
68bb611c8fSApple OSS Distributions IOMemoryDescriptor* IOGetARVManifestData(void);
69*e6231be0SApple OSS Distributions IOMemoryDescriptor* IOGetBaseSystemARVManifestData(void);
70bb611c8fSApple OSS Distributions 
71855239e5SApple OSS Distributions __END_DECLS
72855239e5SApple OSS Distributions 
73855239e5SApple OSS Distributions #if 1
74855239e5SApple OSS Distributions #define DEBG(fmt, args...)      { kprintf(fmt, ## args); }
75855239e5SApple OSS Distributions #else
76855239e5SApple OSS Distributions #define DEBG(fmt, args...)      {}
77855239e5SApple OSS Distributions #endif
78855239e5SApple OSS Distributions 
79855239e5SApple OSS Distributions void
IOSetKeyStoreData(IOMemoryDescriptor * data)80855239e5SApple OSS Distributions IOSetKeyStoreData(IOMemoryDescriptor * data)
81855239e5SApple OSS Distributions {
82855239e5SApple OSS Distributions 	newData = data;
83855239e5SApple OSS Distributions 	alreadyFetched = 0;
84855239e5SApple OSS Distributions }
85855239e5SApple OSS Distributions 
86855239e5SApple OSS Distributions IOMemoryDescriptor *
IOGetBootKeyStoreData(void)87855239e5SApple OSS Distributions IOGetBootKeyStoreData(void)
88855239e5SApple OSS Distributions {
89855239e5SApple OSS Distributions 	IOMemoryDescriptor *memoryDescriptor;
90855239e5SApple OSS Distributions 	boot_args *args = (boot_args *)PE_state.bootArgs;
91855239e5SApple OSS Distributions 	IOOptionBits options;
92855239e5SApple OSS Distributions 	IOAddressRange ranges;
93855239e5SApple OSS Distributions 
94a5e72196SApple OSS Distributions 	if (!OSCompareAndSwap(0, 1, &alreadyFetched)) {
95a5e72196SApple OSS Distributions 		return NULL;
96a5e72196SApple OSS Distributions 	}
97855239e5SApple OSS Distributions 
98a5e72196SApple OSS Distributions 	if (newData) {
99855239e5SApple OSS Distributions 		IOMemoryDescriptor * data = newData;
100855239e5SApple OSS Distributions 		newData = NULL;
101a5e72196SApple OSS Distributions 		return data;
102855239e5SApple OSS Distributions 	}
103855239e5SApple OSS Distributions 
104855239e5SApple OSS Distributions 	DEBG("%s: data at address %u size %u\n", __func__,
105855239e5SApple OSS Distributions 	    args->keyStoreDataStart,
106855239e5SApple OSS Distributions 	    args->keyStoreDataSize);
107855239e5SApple OSS Distributions 
108a5e72196SApple OSS Distributions 	if (args->keyStoreDataStart == 0) {
109a5e72196SApple OSS Distributions 		return NULL;
110a5e72196SApple OSS Distributions 	}
111855239e5SApple OSS Distributions 
112855239e5SApple OSS Distributions 	ranges.address = args->keyStoreDataStart;
113855239e5SApple OSS Distributions 	ranges.length = args->keyStoreDataSize;
114855239e5SApple OSS Distributions 
115186b8fceSApple OSS Distributions 	options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
116855239e5SApple OSS Distributions 
117855239e5SApple OSS Distributions 	memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges,
118855239e5SApple OSS Distributions 	    1,
119855239e5SApple OSS Distributions 	    0,
120855239e5SApple OSS Distributions 	    NULL,
121855239e5SApple OSS Distributions 	    options);
122855239e5SApple OSS Distributions 
123855239e5SApple OSS Distributions 	DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
124855239e5SApple OSS Distributions 
125855239e5SApple OSS Distributions 	return memoryDescriptor;
126855239e5SApple OSS Distributions }
12776e12aa3SApple OSS Distributions 
12876e12aa3SApple OSS Distributions // APFS volume key fetcher
12976e12aa3SApple OSS Distributions 
13076e12aa3SApple OSS Distributions // Store in-memory key (could be used by IOHibernateDone)
13176e12aa3SApple OSS Distributions void
IOSetAPFSKeyStoreData(IOMemoryDescriptor * data)13276e12aa3SApple OSS Distributions IOSetAPFSKeyStoreData(IOMemoryDescriptor* data)
13376e12aa3SApple OSS Distributions {
13476e12aa3SApple OSS Distributions 	// Do not allow re-fetching of the boot_args key by passing NULL here.
135a5e72196SApple OSS Distributions 	if (data != NULL) {
13676e12aa3SApple OSS Distributions 		apfsKeyData = data;
13776e12aa3SApple OSS Distributions 		apfsKeyFetched = 0;
13876e12aa3SApple OSS Distributions 	}
13976e12aa3SApple OSS Distributions }
14076e12aa3SApple OSS Distributions 
14176e12aa3SApple OSS Distributions // Retrieve any key we may have (stored in boot_args or by Hibernate)
14276e12aa3SApple OSS Distributions IOMemoryDescriptor*
IOGetAPFSKeyStoreData()14376e12aa3SApple OSS Distributions IOGetAPFSKeyStoreData()
14476e12aa3SApple OSS Distributions {
14576e12aa3SApple OSS Distributions 	// Check if someone got the key before us
146a5e72196SApple OSS Distributions 	if (!OSCompareAndSwap(0, 1, &apfsKeyFetched)) {
14776e12aa3SApple OSS Distributions 		return NULL;
148a5e72196SApple OSS Distributions 	}
14976e12aa3SApple OSS Distributions 
15076e12aa3SApple OSS Distributions 	// Do we have in-memory key?
151a5e72196SApple OSS Distributions 	if (apfsKeyData) {
15276e12aa3SApple OSS Distributions 		IOMemoryDescriptor* data = apfsKeyData;
15376e12aa3SApple OSS Distributions 		apfsKeyData = NULL;
15476e12aa3SApple OSS Distributions 		return data;
15576e12aa3SApple OSS Distributions 	}
15676e12aa3SApple OSS Distributions 
15776e12aa3SApple OSS Distributions 	// Looks like there was no in-memory key and it's the first call - try boot_args
15876e12aa3SApple OSS Distributions 	boot_args* args = (boot_args*)PE_state.bootArgs;
15976e12aa3SApple OSS Distributions 
16076e12aa3SApple OSS Distributions 	DEBG("%s: data at address %u size %u\n", __func__, args->apfsDataStart, args->apfsDataSize);
161a5e72196SApple OSS Distributions 	if (args->apfsDataStart == 0) {
16276e12aa3SApple OSS Distributions 		return NULL;
163a5e72196SApple OSS Distributions 	}
16476e12aa3SApple OSS Distributions 
16576e12aa3SApple OSS Distributions 	// We have the key in the boot_args, create IOMemoryDescriptor for the blob
16676e12aa3SApple OSS Distributions 	IOAddressRange ranges;
16776e12aa3SApple OSS Distributions 	ranges.address = args->apfsDataStart;
16876e12aa3SApple OSS Distributions 	ranges.length = args->apfsDataSize;
16976e12aa3SApple OSS Distributions 
17076e12aa3SApple OSS Distributions 	const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
17176e12aa3SApple OSS Distributions 
17276e12aa3SApple OSS Distributions 	IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
17376e12aa3SApple OSS Distributions 	DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
17476e12aa3SApple OSS Distributions 	return memoryDescriptor;
17576e12aa3SApple OSS Distributions }
176bb611c8fSApple OSS Distributions 
177bb611c8fSApple OSS Distributions // ARV Root Hash fetcher
178bb611c8fSApple OSS Distributions 
179*e6231be0SApple OSS Distributions // Retrieve any root hash we may have (stored in boot_args)
180bb611c8fSApple OSS Distributions IOMemoryDescriptor*
IOGetARVRootHashData(void)181bb611c8fSApple OSS Distributions IOGetARVRootHashData(void)
182bb611c8fSApple OSS Distributions {
183bb611c8fSApple OSS Distributions 	// Check if someone got the root hash before us
184*e6231be0SApple OSS Distributions 	if (!OSCompareAndSwap(0, 1, &ARVRootHashFetched)) {
185bb611c8fSApple OSS Distributions 		return NULL;
186bb611c8fSApple OSS Distributions 	}
187bb611c8fSApple OSS Distributions 
188bb611c8fSApple OSS Distributions 	boot_args* args = (boot_args*)PE_state.bootArgs;
189bb611c8fSApple OSS Distributions 
190bb611c8fSApple OSS Distributions 	DEBG("%s: data at address %llu size %llu\n", __func__, args->arvRootHashStart, args->arvRootHashSize);
191bb611c8fSApple OSS Distributions 	if (args->arvRootHashStart == 0) {
192bb611c8fSApple OSS Distributions 		return NULL;
193bb611c8fSApple OSS Distributions 	}
194bb611c8fSApple OSS Distributions 
195bb611c8fSApple OSS Distributions 	// We have the root hash in the boot_args, create IOMemoryDescriptor for the blob
196bb611c8fSApple OSS Distributions 	IOAddressRange ranges;
197bb611c8fSApple OSS Distributions 	ranges.address = args->arvRootHashStart;
198bb611c8fSApple OSS Distributions 	ranges.length = args->arvRootHashSize;
199bb611c8fSApple OSS Distributions 
200bb611c8fSApple OSS Distributions 	const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
201bb611c8fSApple OSS Distributions 
202bb611c8fSApple OSS Distributions 	IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
203bb611c8fSApple OSS Distributions 	DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
204bb611c8fSApple OSS Distributions 	return memoryDescriptor;
205bb611c8fSApple OSS Distributions }
206bb611c8fSApple OSS Distributions 
207*e6231be0SApple OSS Distributions // Base System Analogue
208bb611c8fSApple OSS Distributions 
209bb611c8fSApple OSS Distributions IOMemoryDescriptor*
IOGetBaseSystemARVRootHashData(void)210bb611c8fSApple OSS Distributions IOGetBaseSystemARVRootHashData(void)
211bb611c8fSApple OSS Distributions {
212*e6231be0SApple OSS Distributions 	// Check if someone got the base system root hash before us
213*e6231be0SApple OSS Distributions 	if (!OSCompareAndSwap(0, 1, &bsARVRootHashFetched)) {
214bb611c8fSApple OSS Distributions 		return NULL;
215bb611c8fSApple OSS Distributions 	}
216bb611c8fSApple OSS Distributions 
217*e6231be0SApple OSS Distributions 	boot_args* args = (boot_args*)PE_state.bootArgs;
218*e6231be0SApple OSS Distributions 
219*e6231be0SApple OSS Distributions 	DEBG("%s: data at address %llu size %llu\n", __func__, args->bsARVRootHashStart, args->bsARVRootHashSize);
220*e6231be0SApple OSS Distributions 	if (args->bsARVRootHashStart == 0) {
221*e6231be0SApple OSS Distributions 		return NULL;
222*e6231be0SApple OSS Distributions 	}
223*e6231be0SApple OSS Distributions 
224*e6231be0SApple OSS Distributions 	// We have the base system root hash in the boot_args, create IOMemoryDescriptor for the blob
225*e6231be0SApple OSS Distributions 	IOAddressRange ranges;
226*e6231be0SApple OSS Distributions 	ranges.address = args->bsARVRootHashStart;
227*e6231be0SApple OSS Distributions 	ranges.length = args->bsARVRootHashSize;
228*e6231be0SApple OSS Distributions 
229*e6231be0SApple OSS Distributions 	const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
230*e6231be0SApple OSS Distributions 
231*e6231be0SApple OSS Distributions 	IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
232*e6231be0SApple OSS Distributions 	DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
233*e6231be0SApple OSS Distributions 	return memoryDescriptor;
234*e6231be0SApple OSS Distributions }
235*e6231be0SApple OSS Distributions 
236bb611c8fSApple OSS Distributions bool
IOBaseSystemARVRootHashAvailable(void)237bb611c8fSApple OSS Distributions IOBaseSystemARVRootHashAvailable(void)
238bb611c8fSApple OSS Distributions {
239*e6231be0SApple OSS Distributions 	boot_args* args = (boot_args*)PE_state.bootArgs;
240*e6231be0SApple OSS Distributions 
241*e6231be0SApple OSS Distributions 	if (args->bsARVRootHashStart == 0 || args->bsARVRootHashSize == 0) {
242bb611c8fSApple OSS Distributions 		return false;
243bb611c8fSApple OSS Distributions 	}
244bb611c8fSApple OSS Distributions 
245*e6231be0SApple OSS Distributions 	if (args->bsARVManifestStart == 0 || args->bsARVManifestSize == 0) {
246*e6231be0SApple OSS Distributions 		return false;
247*e6231be0SApple OSS Distributions 	}
248*e6231be0SApple OSS Distributions 
249bb611c8fSApple OSS Distributions 	return true;
250bb611c8fSApple OSS Distributions }
251bb611c8fSApple OSS Distributions 
252bb611c8fSApple OSS Distributions // ARV Manifest fetcher
253bb611c8fSApple OSS Distributions 
254*e6231be0SApple OSS Distributions // Retrieve any manifest we may have (stored in boot_args)
255bb611c8fSApple OSS Distributions IOMemoryDescriptor*
IOGetARVManifestData(void)256bb611c8fSApple OSS Distributions IOGetARVManifestData(void)
257bb611c8fSApple OSS Distributions {
258bb611c8fSApple OSS Distributions 	// Check if someone got the manifest before us
259*e6231be0SApple OSS Distributions 	if (!OSCompareAndSwap(0, 1, &ARVManifestFetched)) {
260bb611c8fSApple OSS Distributions 		return NULL;
261bb611c8fSApple OSS Distributions 	}
262bb611c8fSApple OSS Distributions 
263bb611c8fSApple OSS Distributions 	boot_args* args = (boot_args*)PE_state.bootArgs;
264bb611c8fSApple OSS Distributions 
265bb611c8fSApple OSS Distributions 	DEBG("%s: data at address %llu size %llu\n", __func__, args->arvManifestStart, args->arvManifestSize);
266bb611c8fSApple OSS Distributions 	if (args->arvManifestStart == 0) {
267bb611c8fSApple OSS Distributions 		return NULL;
268bb611c8fSApple OSS Distributions 	}
269bb611c8fSApple OSS Distributions 
270bb611c8fSApple OSS Distributions 	// We have the manifest in the boot_args, create IOMemoryDescriptor for the blob
271bb611c8fSApple OSS Distributions 	IOAddressRange ranges;
272bb611c8fSApple OSS Distributions 	ranges.address = args->arvManifestStart;
273bb611c8fSApple OSS Distributions 	ranges.length = args->arvManifestSize;
274bb611c8fSApple OSS Distributions 
275bb611c8fSApple OSS Distributions 	const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
276bb611c8fSApple OSS Distributions 
277bb611c8fSApple OSS Distributions 	IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
278bb611c8fSApple OSS Distributions 	DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
279bb611c8fSApple OSS Distributions 	return memoryDescriptor;
280bb611c8fSApple OSS Distributions }
281*e6231be0SApple OSS Distributions 
282*e6231be0SApple OSS Distributions // Base System Analogue
283*e6231be0SApple OSS Distributions 
284*e6231be0SApple OSS Distributions IOMemoryDescriptor*
IOGetBaseSystemARVManifestData(void)285*e6231be0SApple OSS Distributions IOGetBaseSystemARVManifestData(void)
286*e6231be0SApple OSS Distributions {
287*e6231be0SApple OSS Distributions 	// Check if someone got the base system manifest before us
288*e6231be0SApple OSS Distributions 	if (!OSCompareAndSwap(0, 1, &bsARVManifestFetched)) {
289*e6231be0SApple OSS Distributions 		return NULL;
290*e6231be0SApple OSS Distributions 	}
291*e6231be0SApple OSS Distributions 
292*e6231be0SApple OSS Distributions 	boot_args* args = (boot_args*)PE_state.bootArgs;
293*e6231be0SApple OSS Distributions 
294*e6231be0SApple OSS Distributions 	DEBG("%s: data at address %llu size %llu\n", __func__, args->bsARVManifestStart, args->bsARVManifestSize);
295*e6231be0SApple OSS Distributions 	if (args->bsARVManifestStart == 0) {
296*e6231be0SApple OSS Distributions 		return NULL;
297*e6231be0SApple OSS Distributions 	}
298*e6231be0SApple OSS Distributions 
299*e6231be0SApple OSS Distributions 	// We have the manifest in the boot_args, create IOMemoryDescriptor for the blob
300*e6231be0SApple OSS Distributions 	IOAddressRange ranges;
301*e6231be0SApple OSS Distributions 	ranges.address = args->bsARVManifestStart;
302*e6231be0SApple OSS Distributions 	ranges.length = args->bsARVManifestSize;
303*e6231be0SApple OSS Distributions 
304*e6231be0SApple OSS Distributions 	const IOOptionBits options = kIODirectionInOut | kIOMemoryTypePhysical64 | kIOMemoryMapperNone;
305*e6231be0SApple OSS Distributions 
306*e6231be0SApple OSS Distributions 	IOMemoryDescriptor* memoryDescriptor = IOMemoryDescriptor::withOptions(&ranges, 1, 0, NULL, options);
307*e6231be0SApple OSS Distributions 	DEBG("%s: memory descriptor %p\n", __func__, memoryDescriptor);
308*e6231be0SApple OSS Distributions 	return memoryDescriptor;
309*e6231be0SApple OSS Distributions }
310