1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions * Copyright (c) 2006-2019 Apple Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions *
4*1b191cb5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions *
6*1b191cb5SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*1b191cb5SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*1b191cb5SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*1b191cb5SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*1b191cb5SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*1b191cb5SApple OSS Distributions *
15*1b191cb5SApple OSS Distributions * Please obtain a copy of the License at
16*1b191cb5SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1b191cb5SApple OSS Distributions *
18*1b191cb5SApple OSS Distributions * The Original Code and all software distributed under the License are
19*1b191cb5SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1b191cb5SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1b191cb5SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1b191cb5SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1b191cb5SApple OSS Distributions * Please see the License for the specific language governing rights and
24*1b191cb5SApple OSS Distributions * limitations under the License.
25*1b191cb5SApple OSS Distributions *
26*1b191cb5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1b191cb5SApple OSS Distributions */
28*1b191cb5SApple OSS Distributions
29*1b191cb5SApple OSS Distributions #include <sys/uio.h>
30*1b191cb5SApple OSS Distributions #include <sys/conf.h>
31*1b191cb5SApple OSS Distributions
32*1b191cb5SApple OSS Distributions #include <IOKit/IOLib.h>
33*1b191cb5SApple OSS Distributions #include <IOKit/IOBSD.h>
34*1b191cb5SApple OSS Distributions #include <IOKit/IOService.h>
35*1b191cb5SApple OSS Distributions #include <IOKit/IOPlatformExpert.h>
36*1b191cb5SApple OSS Distributions #include <IOKit/IOPolledInterface.h>
37*1b191cb5SApple OSS Distributions #include <IOKit/IOHibernatePrivate.h>
38*1b191cb5SApple OSS Distributions #include <IOKit/IOBufferMemoryDescriptor.h>
39*1b191cb5SApple OSS Distributions #include <IOKit/AppleKeyStoreInterface.h>
40*1b191cb5SApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
41*1b191cb5SApple OSS Distributions #include "IOKitKernelInternal.h"
42*1b191cb5SApple OSS Distributions
43*1b191cb5SApple OSS Distributions #if defined(__arm64__)
44*1b191cb5SApple OSS Distributions #include <pexpert/arm64/board_config.h>
45*1b191cb5SApple OSS Distributions #if XNU_MONITOR_PPL_HIB
46*1b191cb5SApple OSS Distributions #include <IOKit/SEPHibernator.h>
47*1b191cb5SApple OSS Distributions #endif /* XNU_MONITOR_PPL_HIB */
48*1b191cb5SApple OSS Distributions #endif /* defined(__arm64__) */
49*1b191cb5SApple OSS Distributions
50*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
51*1b191cb5SApple OSS Distributions
52*1b191cb5SApple OSS Distributions OSDefineMetaClassAndAbstractStructors(IOPolledInterface, OSObject);
53*1b191cb5SApple OSS Distributions
54*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOPolledInterface, 0);
55*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOPolledInterface, 1);
56*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOPolledInterface, 2);
57*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOPolledInterface, 3);
58*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOPolledInterface, 4);
59*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOPolledInterface, 5);
60*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOPolledInterface, 6);
61*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOPolledInterface, 7);
62*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOPolledInterface, 8);
63*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOPolledInterface, 9);
64*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOPolledInterface, 10);
65*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOPolledInterface, 11);
66*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOPolledInterface, 12);
67*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOPolledInterface, 13);
68*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOPolledInterface, 14);
69*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(IOPolledInterface, 15);
70*1b191cb5SApple OSS Distributions
71*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
72*1b191cb5SApple OSS Distributions
73*1b191cb5SApple OSS Distributions #ifndef kIOMediaPreferredBlockSizeKey
74*1b191cb5SApple OSS Distributions #define kIOMediaPreferredBlockSizeKey "Preferred Block Size"
75*1b191cb5SApple OSS Distributions #endif
76*1b191cb5SApple OSS Distributions
77*1b191cb5SApple OSS Distributions enum { kDefaultIOSize = 128 * 1024 };
78*1b191cb5SApple OSS Distributions
79*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
80*1b191cb5SApple OSS Distributions
81*1b191cb5SApple OSS Distributions class IOPolledFilePollers : public OSObject
82*1b191cb5SApple OSS Distributions {
83*1b191cb5SApple OSS Distributions OSDeclareDefaultStructors(IOPolledFilePollers);
84*1b191cb5SApple OSS Distributions
85*1b191cb5SApple OSS Distributions public:
86*1b191cb5SApple OSS Distributions IOService * media;
87*1b191cb5SApple OSS Distributions OSArray * pollers;
88*1b191cb5SApple OSS Distributions IOBufferMemoryDescriptor * ioBuffer;
89*1b191cb5SApple OSS Distributions bool abortable;
90*1b191cb5SApple OSS Distributions bool io;
91*1b191cb5SApple OSS Distributions IOReturn ioStatus;
92*1b191cb5SApple OSS Distributions uint32_t openCount;
93*1b191cb5SApple OSS Distributions
94*1b191cb5SApple OSS Distributions static IOPolledFilePollers * copyPollers(IOService * media);
95*1b191cb5SApple OSS Distributions };
96*1b191cb5SApple OSS Distributions
OSDefineMetaClassAndStructors(IOPolledFilePollers,OSObject)97*1b191cb5SApple OSS Distributions OSDefineMetaClassAndStructors(IOPolledFilePollers, OSObject)
98*1b191cb5SApple OSS Distributions
99*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
100*1b191cb5SApple OSS Distributions
101*1b191cb5SApple OSS Distributions IOPolledFilePollers *
102*1b191cb5SApple OSS Distributions IOPolledFilePollers::copyPollers(IOService * media)
103*1b191cb5SApple OSS Distributions {
104*1b191cb5SApple OSS Distributions IOPolledFilePollers * vars;
105*1b191cb5SApple OSS Distributions IOReturn err;
106*1b191cb5SApple OSS Distributions IOService * service;
107*1b191cb5SApple OSS Distributions OSObject * obj;
108*1b191cb5SApple OSS Distributions IORegistryEntry * next;
109*1b191cb5SApple OSS Distributions IORegistryEntry * child;
110*1b191cb5SApple OSS Distributions
111*1b191cb5SApple OSS Distributions if ((obj = media->copyProperty(kIOPolledInterfaceStackKey))) {
112*1b191cb5SApple OSS Distributions IOPolledFilePollers * ioPFPObj = OSDynamicCast(IOPolledFilePollers, obj);
113*1b191cb5SApple OSS Distributions if (!ioPFPObj) {
114*1b191cb5SApple OSS Distributions OSSafeReleaseNULL(obj);
115*1b191cb5SApple OSS Distributions }
116*1b191cb5SApple OSS Distributions return ioPFPObj;
117*1b191cb5SApple OSS Distributions }
118*1b191cb5SApple OSS Distributions
119*1b191cb5SApple OSS Distributions do{
120*1b191cb5SApple OSS Distributions vars = OSTypeAlloc(IOPolledFilePollers);
121*1b191cb5SApple OSS Distributions vars->init();
122*1b191cb5SApple OSS Distributions
123*1b191cb5SApple OSS Distributions vars->pollers = OSArray::withCapacity(4);
124*1b191cb5SApple OSS Distributions if (!vars->pollers) {
125*1b191cb5SApple OSS Distributions err = kIOReturnNoMemory;
126*1b191cb5SApple OSS Distributions break;
127*1b191cb5SApple OSS Distributions }
128*1b191cb5SApple OSS Distributions
129*1b191cb5SApple OSS Distributions next = vars->media = media;
130*1b191cb5SApple OSS Distributions do{
131*1b191cb5SApple OSS Distributions IOPolledInterface * poller;
132*1b191cb5SApple OSS Distributions OSObject * obj;
133*1b191cb5SApple OSS Distributions
134*1b191cb5SApple OSS Distributions obj = next->getProperty(kIOPolledInterfaceSupportKey);
135*1b191cb5SApple OSS Distributions if (kOSBooleanFalse == obj) {
136*1b191cb5SApple OSS Distributions vars->pollers->flushCollection();
137*1b191cb5SApple OSS Distributions break;
138*1b191cb5SApple OSS Distributions } else if ((poller = OSDynamicCast(IOPolledInterface, obj))) {
139*1b191cb5SApple OSS Distributions vars->pollers->setObject(poller);
140*1b191cb5SApple OSS Distributions }
141*1b191cb5SApple OSS Distributions
142*1b191cb5SApple OSS Distributions if ((service = OSDynamicCast(IOService, next))
143*1b191cb5SApple OSS Distributions && service->getDeviceMemory()
144*1b191cb5SApple OSS Distributions && !vars->pollers->getCount()) {
145*1b191cb5SApple OSS Distributions break;
146*1b191cb5SApple OSS Distributions }
147*1b191cb5SApple OSS Distributions
148*1b191cb5SApple OSS Distributions child = next;
149*1b191cb5SApple OSS Distributions }while ((next = child->getParentEntry(gIOServicePlane))
150*1b191cb5SApple OSS Distributions && child->isParent(next, gIOServicePlane, true));
151*1b191cb5SApple OSS Distributions
152*1b191cb5SApple OSS Distributions if (!vars->pollers->getCount()) {
153*1b191cb5SApple OSS Distributions err = kIOReturnUnsupported;
154*1b191cb5SApple OSS Distributions break;
155*1b191cb5SApple OSS Distributions }
156*1b191cb5SApple OSS Distributions }while (false);
157*1b191cb5SApple OSS Distributions
158*1b191cb5SApple OSS Distributions media->setProperty(kIOPolledInterfaceStackKey, vars);
159*1b191cb5SApple OSS Distributions
160*1b191cb5SApple OSS Distributions return vars;
161*1b191cb5SApple OSS Distributions }
162*1b191cb5SApple OSS Distributions
163*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
164*1b191cb5SApple OSS Distributions
165*1b191cb5SApple OSS Distributions static IOReturn
166*1b191cb5SApple OSS Distributions IOPolledFilePollersIODone(IOPolledFilePollers * vars, bool abortable);
167*1b191cb5SApple OSS Distributions
168*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
169*1b191cb5SApple OSS Distributions
170*1b191cb5SApple OSS Distributions static IOReturn
IOPolledFilePollersProbe(IOPolledFilePollers * vars)171*1b191cb5SApple OSS Distributions IOPolledFilePollersProbe(IOPolledFilePollers * vars)
172*1b191cb5SApple OSS Distributions {
173*1b191cb5SApple OSS Distributions IOReturn err = kIOReturnError;
174*1b191cb5SApple OSS Distributions int32_t idx;
175*1b191cb5SApple OSS Distributions IOPolledInterface * poller;
176*1b191cb5SApple OSS Distributions
177*1b191cb5SApple OSS Distributions for (idx = vars->pollers->getCount() - 1; idx >= 0; idx--) {
178*1b191cb5SApple OSS Distributions poller = (IOPolledInterface *) vars->pollers->getObject(idx);
179*1b191cb5SApple OSS Distributions err = poller->probe(vars->media);
180*1b191cb5SApple OSS Distributions if (err) {
181*1b191cb5SApple OSS Distributions HIBLOG("IOPolledInterface::probe[%d] 0x%x\n", idx, err);
182*1b191cb5SApple OSS Distributions break;
183*1b191cb5SApple OSS Distributions }
184*1b191cb5SApple OSS Distributions }
185*1b191cb5SApple OSS Distributions
186*1b191cb5SApple OSS Distributions return err;
187*1b191cb5SApple OSS Distributions }
188*1b191cb5SApple OSS Distributions
189*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
190*1b191cb5SApple OSS Distributions
191*1b191cb5SApple OSS Distributions IOReturn
IOPolledFilePollersOpen(IOPolledFileIOVars * filevars,uint32_t state,bool abortable)192*1b191cb5SApple OSS Distributions IOPolledFilePollersOpen(IOPolledFileIOVars * filevars, uint32_t state, bool abortable)
193*1b191cb5SApple OSS Distributions {
194*1b191cb5SApple OSS Distributions IOPolledFilePollers * vars = filevars->pollers;
195*1b191cb5SApple OSS Distributions IOBufferMemoryDescriptor * ioBuffer;
196*1b191cb5SApple OSS Distributions IOPolledInterface * poller;
197*1b191cb5SApple OSS Distributions IOService * next;
198*1b191cb5SApple OSS Distributions IOReturn err = kIOReturnError;
199*1b191cb5SApple OSS Distributions int32_t idx;
200*1b191cb5SApple OSS Distributions
201*1b191cb5SApple OSS Distributions vars->abortable = abortable;
202*1b191cb5SApple OSS Distributions ioBuffer = NULL;
203*1b191cb5SApple OSS Distributions
204*1b191cb5SApple OSS Distributions if (kIOPolledAfterSleepState == state) {
205*1b191cb5SApple OSS Distributions vars->ioStatus = 0;
206*1b191cb5SApple OSS Distributions vars->io = false;
207*1b191cb5SApple OSS Distributions }
208*1b191cb5SApple OSS Distributions (void) IOPolledFilePollersIODone(vars, false);
209*1b191cb5SApple OSS Distributions
210*1b191cb5SApple OSS Distributions if ((kIOPolledPreflightState == state) || (kIOPolledPreflightCoreDumpState == state)) {
211*1b191cb5SApple OSS Distributions ioBuffer = vars->ioBuffer;
212*1b191cb5SApple OSS Distributions if (!ioBuffer) {
213*1b191cb5SApple OSS Distributions vars->ioBuffer = ioBuffer = IOBufferMemoryDescriptor::withOptions(kIODirectionInOut,
214*1b191cb5SApple OSS Distributions 2 * kDefaultIOSize, page_size);
215*1b191cb5SApple OSS Distributions if (!ioBuffer) {
216*1b191cb5SApple OSS Distributions return kIOReturnNoMemory;
217*1b191cb5SApple OSS Distributions }
218*1b191cb5SApple OSS Distributions }
219*1b191cb5SApple OSS Distributions }
220*1b191cb5SApple OSS Distributions
221*1b191cb5SApple OSS Distributions for (idx = vars->pollers->getCount() - 1; idx >= 0; idx--) {
222*1b191cb5SApple OSS Distributions poller = (IOPolledInterface *) vars->pollers->getObject(idx);
223*1b191cb5SApple OSS Distributions err = poller->open(state, ioBuffer);
224*1b191cb5SApple OSS Distributions if (kIOReturnSuccess != err) {
225*1b191cb5SApple OSS Distributions HIBLOG("IOPolledInterface::open[%d] 0x%x\n", idx, err);
226*1b191cb5SApple OSS Distributions break;
227*1b191cb5SApple OSS Distributions }
228*1b191cb5SApple OSS Distributions }
229*1b191cb5SApple OSS Distributions if ((kIOReturnSuccess == err) && (kIOPolledPreflightState == state)) {
230*1b191cb5SApple OSS Distributions next = vars->media;
231*1b191cb5SApple OSS Distributions while (next) {
232*1b191cb5SApple OSS Distributions next->setProperty(kIOPolledInterfaceActiveKey, kOSBooleanTrue);
233*1b191cb5SApple OSS Distributions next = next->getProvider();
234*1b191cb5SApple OSS Distributions }
235*1b191cb5SApple OSS Distributions }
236*1b191cb5SApple OSS Distributions
237*1b191cb5SApple OSS Distributions return err;
238*1b191cb5SApple OSS Distributions }
239*1b191cb5SApple OSS Distributions
240*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
241*1b191cb5SApple OSS Distributions
242*1b191cb5SApple OSS Distributions IOReturn
IOPolledFilePollersClose(IOPolledFileIOVars * filevars,uint32_t state)243*1b191cb5SApple OSS Distributions IOPolledFilePollersClose(IOPolledFileIOVars * filevars, uint32_t state)
244*1b191cb5SApple OSS Distributions {
245*1b191cb5SApple OSS Distributions IOPolledFilePollers * vars = filevars->pollers;
246*1b191cb5SApple OSS Distributions IOPolledInterface * poller;
247*1b191cb5SApple OSS Distributions IORegistryEntry * next;
248*1b191cb5SApple OSS Distributions IOReturn err;
249*1b191cb5SApple OSS Distributions int32_t idx;
250*1b191cb5SApple OSS Distributions
251*1b191cb5SApple OSS Distributions (void) IOPolledFilePollersIODone(vars, false);
252*1b191cb5SApple OSS Distributions
253*1b191cb5SApple OSS Distributions if ((kIOPolledPostflightState == state) || (kIOPolledPostflightCoreDumpState == state)) {
254*1b191cb5SApple OSS Distributions vars->openCount--;
255*1b191cb5SApple OSS Distributions }
256*1b191cb5SApple OSS Distributions
257*1b191cb5SApple OSS Distributions for (idx = 0, err = kIOReturnSuccess;
258*1b191cb5SApple OSS Distributions (poller = (IOPolledInterface *) vars->pollers->getObject(idx));
259*1b191cb5SApple OSS Distributions idx++) {
260*1b191cb5SApple OSS Distributions err = poller->close(state);
261*1b191cb5SApple OSS Distributions if ((kIOReturnSuccess != err) && (kIOPolledBeforeSleepStateAborted == state)) {
262*1b191cb5SApple OSS Distributions err = poller->close(kIOPolledBeforeSleepState);
263*1b191cb5SApple OSS Distributions }
264*1b191cb5SApple OSS Distributions if (err) {
265*1b191cb5SApple OSS Distributions HIBLOG("IOPolledInterface::close[%d] 0x%x\n", idx, err);
266*1b191cb5SApple OSS Distributions }
267*1b191cb5SApple OSS Distributions }
268*1b191cb5SApple OSS Distributions
269*1b191cb5SApple OSS Distributions if (kIOPolledPostflightState == state) {
270*1b191cb5SApple OSS Distributions next = vars->media;
271*1b191cb5SApple OSS Distributions while (next) {
272*1b191cb5SApple OSS Distributions next->removeProperty(kIOPolledInterfaceActiveKey);
273*1b191cb5SApple OSS Distributions next = next->getParentEntry(gIOServicePlane);
274*1b191cb5SApple OSS Distributions }
275*1b191cb5SApple OSS Distributions }
276*1b191cb5SApple OSS Distributions
277*1b191cb5SApple OSS Distributions if ((kIOPolledPostflightState == state) || (kIOPolledPostflightCoreDumpState == state)) {
278*1b191cb5SApple OSS Distributions do{
279*1b191cb5SApple OSS Distributions if (vars->openCount) {
280*1b191cb5SApple OSS Distributions break;
281*1b191cb5SApple OSS Distributions }
282*1b191cb5SApple OSS Distributions if (vars->ioBuffer) {
283*1b191cb5SApple OSS Distributions vars->ioBuffer->release();
284*1b191cb5SApple OSS Distributions vars->ioBuffer = NULL;
285*1b191cb5SApple OSS Distributions }
286*1b191cb5SApple OSS Distributions }while (false);
287*1b191cb5SApple OSS Distributions }
288*1b191cb5SApple OSS Distributions
289*1b191cb5SApple OSS Distributions return err;
290*1b191cb5SApple OSS Distributions }
291*1b191cb5SApple OSS Distributions
292*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
293*1b191cb5SApple OSS Distributions
294*1b191cb5SApple OSS Distributions IOReturn
setEncryptionKey(const uint8_t * key,size_t keySize)295*1b191cb5SApple OSS Distributions IOPolledInterface::setEncryptionKey(const uint8_t * key, size_t keySize)
296*1b191cb5SApple OSS Distributions {
297*1b191cb5SApple OSS Distributions return kIOReturnUnsupported;
298*1b191cb5SApple OSS Distributions }
299*1b191cb5SApple OSS Distributions
300*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
301*1b191cb5SApple OSS Distributions
302*1b191cb5SApple OSS Distributions IOReturn
IOPolledFilePollersSetEncryptionKey(IOPolledFileIOVars * filevars,const uint8_t * key,size_t keySize)303*1b191cb5SApple OSS Distributions IOPolledFilePollersSetEncryptionKey(IOPolledFileIOVars * filevars,
304*1b191cb5SApple OSS Distributions const uint8_t * key, size_t keySize)
305*1b191cb5SApple OSS Distributions {
306*1b191cb5SApple OSS Distributions IOReturn ret = kIOReturnUnsupported;
307*1b191cb5SApple OSS Distributions IOReturn err;
308*1b191cb5SApple OSS Distributions int32_t idx;
309*1b191cb5SApple OSS Distributions IOPolledFilePollers * vars = filevars->pollers;
310*1b191cb5SApple OSS Distributions IOPolledInterface * poller;
311*1b191cb5SApple OSS Distributions
312*1b191cb5SApple OSS Distributions for (idx = 0;
313*1b191cb5SApple OSS Distributions (poller = (IOPolledInterface *) vars->pollers->getObject(idx));
314*1b191cb5SApple OSS Distributions idx++) {
315*1b191cb5SApple OSS Distributions poller = (IOPolledInterface *) vars->pollers->getObject(idx);
316*1b191cb5SApple OSS Distributions err = poller->setEncryptionKey(key, keySize);
317*1b191cb5SApple OSS Distributions if (kIOReturnSuccess == err) {
318*1b191cb5SApple OSS Distributions ret = err;
319*1b191cb5SApple OSS Distributions }
320*1b191cb5SApple OSS Distributions }
321*1b191cb5SApple OSS Distributions
322*1b191cb5SApple OSS Distributions return ret;
323*1b191cb5SApple OSS Distributions }
324*1b191cb5SApple OSS Distributions
325*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
326*1b191cb5SApple OSS Distributions
327*1b191cb5SApple OSS Distributions IOMemoryDescriptor *
IOPolledFileGetIOBuffer(IOPolledFileIOVars * vars)328*1b191cb5SApple OSS Distributions IOPolledFileGetIOBuffer(IOPolledFileIOVars * vars)
329*1b191cb5SApple OSS Distributions {
330*1b191cb5SApple OSS Distributions return vars->pollers->ioBuffer;
331*1b191cb5SApple OSS Distributions }
332*1b191cb5SApple OSS Distributions
333*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
334*1b191cb5SApple OSS Distributions
335*1b191cb5SApple OSS Distributions static void
IOPolledIOComplete(void * target,void * parameter,IOReturn status,UInt64 actualByteCount)336*1b191cb5SApple OSS Distributions IOPolledIOComplete(void * target,
337*1b191cb5SApple OSS Distributions void * parameter,
338*1b191cb5SApple OSS Distributions IOReturn status,
339*1b191cb5SApple OSS Distributions UInt64 actualByteCount)
340*1b191cb5SApple OSS Distributions {
341*1b191cb5SApple OSS Distributions IOPolledFilePollers * vars = (IOPolledFilePollers *) parameter;
342*1b191cb5SApple OSS Distributions
343*1b191cb5SApple OSS Distributions vars->ioStatus = status;
344*1b191cb5SApple OSS Distributions }
345*1b191cb5SApple OSS Distributions
346*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
347*1b191cb5SApple OSS Distributions
348*1b191cb5SApple OSS Distributions static IOReturn
IOStartPolledIO(IOPolledFilePollers * vars,uint32_t operation,uint32_t bufferOffset,uint64_t deviceOffset,uint64_t length)349*1b191cb5SApple OSS Distributions IOStartPolledIO(IOPolledFilePollers * vars,
350*1b191cb5SApple OSS Distributions uint32_t operation, uint32_t bufferOffset,
351*1b191cb5SApple OSS Distributions uint64_t deviceOffset, uint64_t length)
352*1b191cb5SApple OSS Distributions {
353*1b191cb5SApple OSS Distributions IOReturn err;
354*1b191cb5SApple OSS Distributions IOPolledInterface * poller;
355*1b191cb5SApple OSS Distributions IOPolledCompletion completion;
356*1b191cb5SApple OSS Distributions
357*1b191cb5SApple OSS Distributions err = vars->ioStatus;
358*1b191cb5SApple OSS Distributions if (kIOReturnSuccess != err) {
359*1b191cb5SApple OSS Distributions return err;
360*1b191cb5SApple OSS Distributions }
361*1b191cb5SApple OSS Distributions
362*1b191cb5SApple OSS Distributions completion.target = NULL;
363*1b191cb5SApple OSS Distributions completion.action = &IOPolledIOComplete;
364*1b191cb5SApple OSS Distributions completion.parameter = vars;
365*1b191cb5SApple OSS Distributions
366*1b191cb5SApple OSS Distributions vars->ioStatus = -1;
367*1b191cb5SApple OSS Distributions
368*1b191cb5SApple OSS Distributions poller = (IOPolledInterface *) vars->pollers->getObject(0);
369*1b191cb5SApple OSS Distributions err = poller->startIO(operation, bufferOffset, deviceOffset, length, completion);
370*1b191cb5SApple OSS Distributions if (err) {
371*1b191cb5SApple OSS Distributions if (kernel_debugger_entry_count) {
372*1b191cb5SApple OSS Distributions HIBLOG("IOPolledInterface::startIO[%d] 0x%x\n", 0, err);
373*1b191cb5SApple OSS Distributions } else {
374*1b191cb5SApple OSS Distributions HIBLOGFROMPANIC("IOPolledInterface::IOStartPolledIO(0x%p, %d, 0x%x, 0x%llx, %llu) : poller->startIO(%d, 0x%x, 0x%llx, %llu, completion) returned 0x%x",
375*1b191cb5SApple OSS Distributions vars, operation, bufferOffset, deviceOffset, length, operation, bufferOffset, deviceOffset, length, err);
376*1b191cb5SApple OSS Distributions }
377*1b191cb5SApple OSS Distributions }
378*1b191cb5SApple OSS Distributions return err;
379*1b191cb5SApple OSS Distributions }
380*1b191cb5SApple OSS Distributions
381*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
382*1b191cb5SApple OSS Distributions
383*1b191cb5SApple OSS Distributions static IOReturn
IOPolledFilePollersIODone(IOPolledFilePollers * vars,bool abortable)384*1b191cb5SApple OSS Distributions IOPolledFilePollersIODone(IOPolledFilePollers * vars, bool abortable)
385*1b191cb5SApple OSS Distributions {
386*1b191cb5SApple OSS Distributions IOReturn err = kIOReturnSuccess;
387*1b191cb5SApple OSS Distributions int32_t idx = 0;
388*1b191cb5SApple OSS Distributions IOPolledInterface * poller;
389*1b191cb5SApple OSS Distributions AbsoluteTime deadline;
390*1b191cb5SApple OSS Distributions
391*1b191cb5SApple OSS Distributions if (!vars->io) {
392*1b191cb5SApple OSS Distributions return kIOReturnSuccess;
393*1b191cb5SApple OSS Distributions }
394*1b191cb5SApple OSS Distributions
395*1b191cb5SApple OSS Distributions abortable &= vars->abortable;
396*1b191cb5SApple OSS Distributions
397*1b191cb5SApple OSS Distributions clock_interval_to_deadline(2000, kMillisecondScale, &deadline);
398*1b191cb5SApple OSS Distributions
399*1b191cb5SApple OSS Distributions while (-1 == vars->ioStatus) {
400*1b191cb5SApple OSS Distributions for (idx = 0;
401*1b191cb5SApple OSS Distributions (poller = (IOPolledInterface *) vars->pollers->getObject(idx));
402*1b191cb5SApple OSS Distributions idx++) {
403*1b191cb5SApple OSS Distributions IOReturn newErr;
404*1b191cb5SApple OSS Distributions newErr = poller->checkForWork();
405*1b191cb5SApple OSS Distributions if ((newErr == kIOReturnAborted) && !abortable) {
406*1b191cb5SApple OSS Distributions newErr = kIOReturnSuccess;
407*1b191cb5SApple OSS Distributions }
408*1b191cb5SApple OSS Distributions if (kIOReturnSuccess == err) {
409*1b191cb5SApple OSS Distributions err = newErr;
410*1b191cb5SApple OSS Distributions }
411*1b191cb5SApple OSS Distributions }
412*1b191cb5SApple OSS Distributions if ((false) && (kIOReturnSuccess == err) && (mach_absolute_time() > AbsoluteTime_to_scalar(&deadline))) {
413*1b191cb5SApple OSS Distributions HIBLOG("IOPolledInterface::forced timeout\n");
414*1b191cb5SApple OSS Distributions vars->ioStatus = kIOReturnTimeout;
415*1b191cb5SApple OSS Distributions }
416*1b191cb5SApple OSS Distributions }
417*1b191cb5SApple OSS Distributions vars->io = false;
418*1b191cb5SApple OSS Distributions
419*1b191cb5SApple OSS Distributions #if HIBERNATION
420*1b191cb5SApple OSS Distributions if ((kIOReturnSuccess == err) && abortable && hibernate_should_abort()) {
421*1b191cb5SApple OSS Distributions err = kIOReturnAborted;
422*1b191cb5SApple OSS Distributions HIBLOG("IOPolledInterface::checkForWork sw abort\n");
423*1b191cb5SApple OSS Distributions }
424*1b191cb5SApple OSS Distributions #endif
425*1b191cb5SApple OSS Distributions
426*1b191cb5SApple OSS Distributions if (err) {
427*1b191cb5SApple OSS Distributions HIBLOG("IOPolledInterface::checkForWork[%d] 0x%x\n", idx, err);
428*1b191cb5SApple OSS Distributions } else {
429*1b191cb5SApple OSS Distributions err = vars->ioStatus;
430*1b191cb5SApple OSS Distributions if (kIOReturnSuccess != err) {
431*1b191cb5SApple OSS Distributions HIBLOG("IOPolledInterface::ioStatus 0x%x\n", err);
432*1b191cb5SApple OSS Distributions }
433*1b191cb5SApple OSS Distributions }
434*1b191cb5SApple OSS Distributions
435*1b191cb5SApple OSS Distributions return err;
436*1b191cb5SApple OSS Distributions }
437*1b191cb5SApple OSS Distributions
438*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
439*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
440*1b191cb5SApple OSS Distributions
441*1b191cb5SApple OSS Distributions struct _OpenFileContext {
442*1b191cb5SApple OSS Distributions OSData * extents;
443*1b191cb5SApple OSS Distributions uint64_t size;
444*1b191cb5SApple OSS Distributions };
445*1b191cb5SApple OSS Distributions
446*1b191cb5SApple OSS Distributions static void
file_extent_callback(void * ref,uint64_t start,uint64_t length)447*1b191cb5SApple OSS Distributions file_extent_callback(void * ref, uint64_t start, uint64_t length)
448*1b191cb5SApple OSS Distributions {
449*1b191cb5SApple OSS Distributions _OpenFileContext * ctx = (_OpenFileContext *) ref;
450*1b191cb5SApple OSS Distributions IOPolledFileExtent extent;
451*1b191cb5SApple OSS Distributions
452*1b191cb5SApple OSS Distributions extent.start = start;
453*1b191cb5SApple OSS Distributions extent.length = length;
454*1b191cb5SApple OSS Distributions ctx->extents->appendValue(extent);
455*1b191cb5SApple OSS Distributions ctx->size += length;
456*1b191cb5SApple OSS Distributions }
457*1b191cb5SApple OSS Distributions
458*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
459*1b191cb5SApple OSS Distributions
460*1b191cb5SApple OSS Distributions static IOService *
IOCopyMediaForDev(dev_t device)461*1b191cb5SApple OSS Distributions IOCopyMediaForDev(dev_t device)
462*1b191cb5SApple OSS Distributions {
463*1b191cb5SApple OSS Distributions OSDictionary * matching;
464*1b191cb5SApple OSS Distributions OSNumber * num;
465*1b191cb5SApple OSS Distributions OSIterator * iter;
466*1b191cb5SApple OSS Distributions IOService * result = NULL;
467*1b191cb5SApple OSS Distributions
468*1b191cb5SApple OSS Distributions matching = IOService::serviceMatching("IOMedia");
469*1b191cb5SApple OSS Distributions if (!matching) {
470*1b191cb5SApple OSS Distributions return NULL;
471*1b191cb5SApple OSS Distributions }
472*1b191cb5SApple OSS Distributions do{
473*1b191cb5SApple OSS Distributions num = OSNumber::withNumber(major(device), 32);
474*1b191cb5SApple OSS Distributions if (!num) {
475*1b191cb5SApple OSS Distributions break;
476*1b191cb5SApple OSS Distributions }
477*1b191cb5SApple OSS Distributions matching->setObject(kIOBSDMajorKey, num);
478*1b191cb5SApple OSS Distributions num->release();
479*1b191cb5SApple OSS Distributions num = OSNumber::withNumber(minor(device), 32);
480*1b191cb5SApple OSS Distributions if (!num) {
481*1b191cb5SApple OSS Distributions break;
482*1b191cb5SApple OSS Distributions }
483*1b191cb5SApple OSS Distributions matching->setObject(kIOBSDMinorKey, num);
484*1b191cb5SApple OSS Distributions num->release();
485*1b191cb5SApple OSS Distributions if (!num) {
486*1b191cb5SApple OSS Distributions break;
487*1b191cb5SApple OSS Distributions }
488*1b191cb5SApple OSS Distributions iter = IOService::getMatchingServices(matching);
489*1b191cb5SApple OSS Distributions if (iter) {
490*1b191cb5SApple OSS Distributions result = (IOService *) iter->getNextObject();
491*1b191cb5SApple OSS Distributions result->retain();
492*1b191cb5SApple OSS Distributions iter->release();
493*1b191cb5SApple OSS Distributions }
494*1b191cb5SApple OSS Distributions }while (false);
495*1b191cb5SApple OSS Distributions matching->release();
496*1b191cb5SApple OSS Distributions
497*1b191cb5SApple OSS Distributions return result;
498*1b191cb5SApple OSS Distributions }
499*1b191cb5SApple OSS Distributions
500*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
501*1b191cb5SApple OSS Distributions
502*1b191cb5SApple OSS Distributions #if defined(__i386__) || defined(__x86_64__)
503*1b191cb5SApple OSS Distributions #define APFSMEDIA_GETHIBERKEY "getHiberKey"
504*1b191cb5SApple OSS Distributions
505*1b191cb5SApple OSS Distributions static IOReturn
IOGetVolumeCryptKey(dev_t block_dev,LIBKERN_RETURNS_RETAINED OSString ** pKeyUUID,uint8_t * volumeCryptKey,size_t * keySize)506*1b191cb5SApple OSS Distributions IOGetVolumeCryptKey(dev_t block_dev,
507*1b191cb5SApple OSS Distributions LIBKERN_RETURNS_RETAINED OSString ** pKeyUUID,
508*1b191cb5SApple OSS Distributions uint8_t * volumeCryptKey,
509*1b191cb5SApple OSS Distributions size_t * keySize)
510*1b191cb5SApple OSS Distributions {
511*1b191cb5SApple OSS Distributions IOReturn err;
512*1b191cb5SApple OSS Distributions IOService * part;
513*1b191cb5SApple OSS Distributions OSString * keyUUID = NULL;
514*1b191cb5SApple OSS Distributions OSString * keyStoreUUID = NULL;
515*1b191cb5SApple OSS Distributions uuid_t volumeKeyUUID;
516*1b191cb5SApple OSS Distributions aks_volume_key_t vek;
517*1b191cb5SApple OSS Distributions size_t callerKeySize;
518*1b191cb5SApple OSS Distributions
519*1b191cb5SApple OSS Distributions static IOService * sKeyStore;
520*1b191cb5SApple OSS Distributions
521*1b191cb5SApple OSS Distributions part = IOCopyMediaForDev(block_dev);
522*1b191cb5SApple OSS Distributions if (!part) {
523*1b191cb5SApple OSS Distributions return kIOReturnNotFound;
524*1b191cb5SApple OSS Distributions }
525*1b191cb5SApple OSS Distributions
526*1b191cb5SApple OSS Distributions callerKeySize = *keySize;
527*1b191cb5SApple OSS Distributions // Try APFS first
528*1b191cb5SApple OSS Distributions {
529*1b191cb5SApple OSS Distributions uuid_t volUuid = {0};
530*1b191cb5SApple OSS Distributions err = part->callPlatformFunction(APFSMEDIA_GETHIBERKEY, false, &volUuid, volumeCryptKey, keySize, keySize);
531*1b191cb5SApple OSS Distributions if (kIOReturnBadArgument == err) {
532*1b191cb5SApple OSS Distributions // apfs fails on buffer size >32
533*1b191cb5SApple OSS Distributions *keySize = 32;
534*1b191cb5SApple OSS Distributions err = part->callPlatformFunction(APFSMEDIA_GETHIBERKEY, false, &volUuid, volumeCryptKey, keySize, keySize);
535*1b191cb5SApple OSS Distributions }
536*1b191cb5SApple OSS Distributions if (err != kIOReturnSuccess) {
537*1b191cb5SApple OSS Distributions *keySize = 0;
538*1b191cb5SApple OSS Distributions } else {
539*1b191cb5SApple OSS Distributions // No need to create uuid string if it's not requested
540*1b191cb5SApple OSS Distributions if (pKeyUUID) {
541*1b191cb5SApple OSS Distributions uuid_string_t volUuidStr;
542*1b191cb5SApple OSS Distributions uuid_unparse(volUuid, volUuidStr);
543*1b191cb5SApple OSS Distributions *pKeyUUID = OSString::withCString(volUuidStr);
544*1b191cb5SApple OSS Distributions }
545*1b191cb5SApple OSS Distributions
546*1b191cb5SApple OSS Distributions part->release();
547*1b191cb5SApple OSS Distributions return kIOReturnSuccess;
548*1b191cb5SApple OSS Distributions }
549*1b191cb5SApple OSS Distributions }
550*1b191cb5SApple OSS Distributions
551*1b191cb5SApple OSS Distributions // Then old CS path
552*1b191cb5SApple OSS Distributions err = part->callPlatformFunction(PLATFORM_FUNCTION_GET_MEDIA_ENCRYPTION_KEY_UUID, false,
553*1b191cb5SApple OSS Distributions (void *) &keyUUID, (void *) &keyStoreUUID, NULL, NULL);
554*1b191cb5SApple OSS Distributions if ((kIOReturnSuccess == err) && keyUUID && keyStoreUUID) {
555*1b191cb5SApple OSS Distributions // IOLog("got volume key %s\n", keyStoreUUID->getCStringNoCopy());
556*1b191cb5SApple OSS Distributions
557*1b191cb5SApple OSS Distributions if (!sKeyStore) {
558*1b191cb5SApple OSS Distributions sKeyStore = (IOService *) IORegistryEntry::fromPath(AKS_SERVICE_PATH, gIOServicePlane);
559*1b191cb5SApple OSS Distributions }
560*1b191cb5SApple OSS Distributions if (sKeyStore) {
561*1b191cb5SApple OSS Distributions err = uuid_parse(keyStoreUUID->getCStringNoCopy(), volumeKeyUUID);
562*1b191cb5SApple OSS Distributions } else {
563*1b191cb5SApple OSS Distributions err = kIOReturnNoResources;
564*1b191cb5SApple OSS Distributions }
565*1b191cb5SApple OSS Distributions if (kIOReturnSuccess == err) {
566*1b191cb5SApple OSS Distributions err = sKeyStore->callPlatformFunction(gAKSGetKey, true, volumeKeyUUID, &vek, NULL, NULL);
567*1b191cb5SApple OSS Distributions }
568*1b191cb5SApple OSS Distributions if (kIOReturnSuccess != err) {
569*1b191cb5SApple OSS Distributions IOLog("volume key err 0x%x\n", err);
570*1b191cb5SApple OSS Distributions } else {
571*1b191cb5SApple OSS Distributions if (vek.key.keybytecount <= callerKeySize) {
572*1b191cb5SApple OSS Distributions *keySize = vek.key.keybytecount;
573*1b191cb5SApple OSS Distributions }
574*1b191cb5SApple OSS Distributions bcopy(&vek.key.keybytes[0], volumeCryptKey, *keySize);
575*1b191cb5SApple OSS Distributions }
576*1b191cb5SApple OSS Distributions bzero(&vek, sizeof(vek));
577*1b191cb5SApple OSS Distributions
578*1b191cb5SApple OSS Distributions if (pKeyUUID) {
579*1b191cb5SApple OSS Distributions // Create a copy because the caller would release it
580*1b191cb5SApple OSS Distributions *pKeyUUID = OSString::withString(keyUUID);
581*1b191cb5SApple OSS Distributions }
582*1b191cb5SApple OSS Distributions }
583*1b191cb5SApple OSS Distributions
584*1b191cb5SApple OSS Distributions part->release();
585*1b191cb5SApple OSS Distributions return err;
586*1b191cb5SApple OSS Distributions }
587*1b191cb5SApple OSS Distributions #endif /* defined(__i386__) || defined(__x86_64__) */
588*1b191cb5SApple OSS Distributions
589*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
590*1b191cb5SApple OSS Distributions
591*1b191cb5SApple OSS Distributions #if defined(__arm64__)
592*1b191cb5SApple OSS Distributions static IOReturn
IOGetHibernationCryptKey(uint8_t * hibernationKey,size_t * keySize,uint32_t * swSeed)593*1b191cb5SApple OSS Distributions IOGetHibernationCryptKey(uint8_t * hibernationKey,
594*1b191cb5SApple OSS Distributions size_t * keySize,
595*1b191cb5SApple OSS Distributions uint32_t *swSeed
596*1b191cb5SApple OSS Distributions )
597*1b191cb5SApple OSS Distributions {
598*1b191cb5SApple OSS Distributions return kIOReturnNotFound;
599*1b191cb5SApple OSS Distributions }
600*1b191cb5SApple OSS Distributions #endif /* defined(__arm64__) */
601*1b191cb5SApple OSS Distributions
602*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
603*1b191cb5SApple OSS Distributions
604*1b191cb5SApple OSS Distributions IOReturn
IOPolledFileOpen(const char * filename,uint32_t flags,uint64_t setFileSize,uint64_t fsFreeSize,void * write_file_addr,size_t write_file_len,IOPolledFileIOVars ** fileVars,OSData ** imagePath,uint8_t * volumeCryptKey,size_t * keySize)605*1b191cb5SApple OSS Distributions IOPolledFileOpen(const char * filename,
606*1b191cb5SApple OSS Distributions uint32_t flags,
607*1b191cb5SApple OSS Distributions uint64_t setFileSize, uint64_t fsFreeSize,
608*1b191cb5SApple OSS Distributions void * write_file_addr, size_t write_file_len,
609*1b191cb5SApple OSS Distributions IOPolledFileIOVars ** fileVars,
610*1b191cb5SApple OSS Distributions OSData ** imagePath,
611*1b191cb5SApple OSS Distributions uint8_t * volumeCryptKey, size_t * keySize)
612*1b191cb5SApple OSS Distributions {
613*1b191cb5SApple OSS Distributions IOReturn err = kIOReturnSuccess;
614*1b191cb5SApple OSS Distributions IOPolledFileIOVars * vars;
615*1b191cb5SApple OSS Distributions _OpenFileContext ctx;
616*1b191cb5SApple OSS Distributions OSData * extentsData = NULL;
617*1b191cb5SApple OSS Distributions OSNumber * num;
618*1b191cb5SApple OSS Distributions IOService * part = NULL;
619*1b191cb5SApple OSS Distributions dev_t block_dev;
620*1b191cb5SApple OSS Distributions dev_t image_dev;
621*1b191cb5SApple OSS Distributions AbsoluteTime startTime, endTime;
622*1b191cb5SApple OSS Distributions uint64_t nsec;
623*1b191cb5SApple OSS Distributions
624*1b191cb5SApple OSS Distributions vars = IOMallocType(IOPolledFileIOVars);
625*1b191cb5SApple OSS Distributions vars->allocated = true;
626*1b191cb5SApple OSS Distributions
627*1b191cb5SApple OSS Distributions do{
628*1b191cb5SApple OSS Distributions extentsData = OSData::withCapacity(32);
629*1b191cb5SApple OSS Distributions ctx.extents = extentsData;
630*1b191cb5SApple OSS Distributions ctx.size = 0;
631*1b191cb5SApple OSS Distributions clock_get_uptime(&startTime);
632*1b191cb5SApple OSS Distributions
633*1b191cb5SApple OSS Distributions vars->fileRef = kern_open_file_for_direct_io(filename,
634*1b191cb5SApple OSS Distributions flags,
635*1b191cb5SApple OSS Distributions &file_extent_callback, &ctx,
636*1b191cb5SApple OSS Distributions setFileSize,
637*1b191cb5SApple OSS Distributions fsFreeSize,
638*1b191cb5SApple OSS Distributions // write file:
639*1b191cb5SApple OSS Distributions 0, write_file_addr, write_file_len,
640*1b191cb5SApple OSS Distributions // results
641*1b191cb5SApple OSS Distributions &block_dev,
642*1b191cb5SApple OSS Distributions &image_dev,
643*1b191cb5SApple OSS Distributions &vars->block0,
644*1b191cb5SApple OSS Distributions &vars->maxiobytes,
645*1b191cb5SApple OSS Distributions &vars->flags);
646*1b191cb5SApple OSS Distributions #if 0
647*1b191cb5SApple OSS Distributions uint32_t msDelay = (131071 & random());
648*1b191cb5SApple OSS Distributions HIBLOG("sleep %d\n", msDelay);
649*1b191cb5SApple OSS Distributions IOSleep(msDelay);
650*1b191cb5SApple OSS Distributions #endif
651*1b191cb5SApple OSS Distributions clock_get_uptime(&endTime);
652*1b191cb5SApple OSS Distributions SUB_ABSOLUTETIME(&endTime, &startTime);
653*1b191cb5SApple OSS Distributions absolutetime_to_nanoseconds(endTime, &nsec);
654*1b191cb5SApple OSS Distributions
655*1b191cb5SApple OSS Distributions if (!vars->fileRef) {
656*1b191cb5SApple OSS Distributions err = kIOReturnNoSpace;
657*1b191cb5SApple OSS Distributions }
658*1b191cb5SApple OSS Distributions
659*1b191cb5SApple OSS Distributions HIBLOG("kern_open_file_for_direct_io took %qd ms\n", nsec / 1000000ULL);
660*1b191cb5SApple OSS Distributions if (kIOReturnSuccess != err) {
661*1b191cb5SApple OSS Distributions break;
662*1b191cb5SApple OSS Distributions }
663*1b191cb5SApple OSS Distributions
664*1b191cb5SApple OSS Distributions HIBLOG("Opened file %s, size %qd, extents %ld, maxio %qx ssd %d\n", filename, ctx.size,
665*1b191cb5SApple OSS Distributions (extentsData->getLength() / sizeof(IOPolledFileExtent)) - 1,
666*1b191cb5SApple OSS Distributions vars->maxiobytes, kIOPolledFileSSD & vars->flags);
667*1b191cb5SApple OSS Distributions assert(!vars->block0);
668*1b191cb5SApple OSS Distributions if (extentsData->getLength() < sizeof(IOPolledFileExtent)) {
669*1b191cb5SApple OSS Distributions err = kIOReturnNoSpace;
670*1b191cb5SApple OSS Distributions break;
671*1b191cb5SApple OSS Distributions }
672*1b191cb5SApple OSS Distributions
673*1b191cb5SApple OSS Distributions vars->fileSize = ctx.size;
674*1b191cb5SApple OSS Distributions vars->extentMap = (IOPolledFileExtent *) extentsData->getBytesNoCopy();
675*1b191cb5SApple OSS Distributions
676*1b191cb5SApple OSS Distributions part = IOCopyMediaForDev(image_dev);
677*1b191cb5SApple OSS Distributions if (!part) {
678*1b191cb5SApple OSS Distributions err = kIOReturnNotFound;
679*1b191cb5SApple OSS Distributions break;
680*1b191cb5SApple OSS Distributions }
681*1b191cb5SApple OSS Distributions
682*1b191cb5SApple OSS Distributions if (!(vars->pollers = IOPolledFilePollers::copyPollers(part))) {
683*1b191cb5SApple OSS Distributions break;
684*1b191cb5SApple OSS Distributions }
685*1b191cb5SApple OSS Distributions
686*1b191cb5SApple OSS Distributions if ((num = OSDynamicCast(OSNumber, part->getProperty(kIOMediaPreferredBlockSizeKey)))) {
687*1b191cb5SApple OSS Distributions vars->blockSize = num->unsigned32BitValue();
688*1b191cb5SApple OSS Distributions }
689*1b191cb5SApple OSS Distributions if (vars->blockSize < 4096) {
690*1b191cb5SApple OSS Distributions vars->blockSize = 4096;
691*1b191cb5SApple OSS Distributions }
692*1b191cb5SApple OSS Distributions
693*1b191cb5SApple OSS Distributions HIBLOG("polled file major %d, minor %d, blocksize %ld, pollers %d\n",
694*1b191cb5SApple OSS Distributions major(image_dev), minor(image_dev), (long)vars->blockSize,
695*1b191cb5SApple OSS Distributions vars->pollers->pollers->getCount());
696*1b191cb5SApple OSS Distributions
697*1b191cb5SApple OSS Distributions OSString * keyUUID = NULL;
698*1b191cb5SApple OSS Distributions #if defined(__i386__) || defined(__x86_64__)
699*1b191cb5SApple OSS Distributions if (volumeCryptKey) {
700*1b191cb5SApple OSS Distributions err = IOGetVolumeCryptKey(block_dev, &keyUUID, volumeCryptKey, keySize);
701*1b191cb5SApple OSS Distributions }
702*1b191cb5SApple OSS Distributions #elif defined(__arm64__)
703*1b191cb5SApple OSS Distributions uint32_t swSeed = 0;
704*1b191cb5SApple OSS Distributions if (volumeCryptKey) {
705*1b191cb5SApple OSS Distributions if (flags & kIOPolledFileHibernate) {
706*1b191cb5SApple OSS Distributions err = IOGetHibernationCryptKey(volumeCryptKey, keySize, &swSeed);
707*1b191cb5SApple OSS Distributions if (kIOReturnSuccess != err) {
708*1b191cb5SApple OSS Distributions HIBLOG("error 0x%x from IOGetHibernationCryptKey\n", err);
709*1b191cb5SApple OSS Distributions break;
710*1b191cb5SApple OSS Distributions }
711*1b191cb5SApple OSS Distributions } else {
712*1b191cb5SApple OSS Distributions *keySize = 0;
713*1b191cb5SApple OSS Distributions }
714*1b191cb5SApple OSS Distributions }
715*1b191cb5SApple OSS Distributions #else
716*1b191cb5SApple OSS Distributions if (volumeCryptKey) {
717*1b191cb5SApple OSS Distributions HIBLOG("IOPolledFileOpen: unable to get volumeCryptKey\n");
718*1b191cb5SApple OSS Distributions err = kIOReturnNotFound;
719*1b191cb5SApple OSS Distributions break;
720*1b191cb5SApple OSS Distributions }
721*1b191cb5SApple OSS Distributions #endif
722*1b191cb5SApple OSS Distributions
723*1b191cb5SApple OSS Distributions *fileVars = vars;
724*1b191cb5SApple OSS Distributions vars->fileExtents = extentsData;
725*1b191cb5SApple OSS Distributions
726*1b191cb5SApple OSS Distributions // make imagePath
727*1b191cb5SApple OSS Distributions OSData * data = NULL;
728*1b191cb5SApple OSS Distributions if (imagePath) {
729*1b191cb5SApple OSS Distributions #if defined(__i386__) || defined(__x86_64__)
730*1b191cb5SApple OSS Distributions char str2[24 + sizeof(uuid_string_t) + 2];
731*1b191cb5SApple OSS Distributions
732*1b191cb5SApple OSS Distributions if (keyUUID) {
733*1b191cb5SApple OSS Distributions snprintf(str2, sizeof(str2), "%qx:%s",
734*1b191cb5SApple OSS Distributions vars->extentMap[0].start, keyUUID->getCStringNoCopy());
735*1b191cb5SApple OSS Distributions } else {
736*1b191cb5SApple OSS Distributions snprintf(str2, sizeof(str2), "%qx", vars->extentMap[0].start);
737*1b191cb5SApple OSS Distributions }
738*1b191cb5SApple OSS Distributions
739*1b191cb5SApple OSS Distributions err = IOService::getPlatform()->callPlatformFunction(
740*1b191cb5SApple OSS Distributions gIOCreateEFIDevicePathSymbol, false,
741*1b191cb5SApple OSS Distributions (void *) part, (void *) str2,
742*1b191cb5SApple OSS Distributions (void *) (uintptr_t) true, (void *) &data);
743*1b191cb5SApple OSS Distributions #elif defined(__arm64__)
744*1b191cb5SApple OSS Distributions char str2[26];
745*1b191cb5SApple OSS Distributions snprintf(str2, sizeof(str2), "%qx:%x", vars->extentMap[0].start, swSeed);
746*1b191cb5SApple OSS Distributions data = OSData::withBytes(str2, (unsigned int) strlen(str2));
747*1b191cb5SApple OSS Distributions err = kIOReturnSuccess;
748*1b191cb5SApple OSS Distributions #else
749*1b191cb5SApple OSS Distributions err = kIOReturnNotFound;
750*1b191cb5SApple OSS Distributions #endif
751*1b191cb5SApple OSS Distributions if (kIOReturnSuccess != err) {
752*1b191cb5SApple OSS Distributions HIBLOG("error 0x%x getting path\n", err);
753*1b191cb5SApple OSS Distributions OSSafeReleaseNULL(keyUUID);
754*1b191cb5SApple OSS Distributions break;
755*1b191cb5SApple OSS Distributions }
756*1b191cb5SApple OSS Distributions *imagePath = data;
757*1b191cb5SApple OSS Distributions }
758*1b191cb5SApple OSS Distributions
759*1b191cb5SApple OSS Distributions // Release key UUID if we have one
760*1b191cb5SApple OSS Distributions OSSafeReleaseNULL(keyUUID);
761*1b191cb5SApple OSS Distributions }while (false);
762*1b191cb5SApple OSS Distributions
763*1b191cb5SApple OSS Distributions if (kIOReturnSuccess != err) {
764*1b191cb5SApple OSS Distributions HIBLOG("error 0x%x opening polled file\n", err);
765*1b191cb5SApple OSS Distributions IOPolledFileClose(&vars, 0, NULL, 0, 0, 0, false);
766*1b191cb5SApple OSS Distributions if (extentsData) {
767*1b191cb5SApple OSS Distributions extentsData->release();
768*1b191cb5SApple OSS Distributions }
769*1b191cb5SApple OSS Distributions }
770*1b191cb5SApple OSS Distributions
771*1b191cb5SApple OSS Distributions if (part) {
772*1b191cb5SApple OSS Distributions part->release();
773*1b191cb5SApple OSS Distributions }
774*1b191cb5SApple OSS Distributions
775*1b191cb5SApple OSS Distributions return err;
776*1b191cb5SApple OSS Distributions }
777*1b191cb5SApple OSS Distributions
778*1b191cb5SApple OSS Distributions IOReturn
IOPolledFileOpen(const char * filename,uint32_t flags,uint64_t setFileSize,uint64_t fsFreeSize,void * write_file_addr,size_t write_file_len,IOPolledFileIOVars ** fileVars,OSSharedPtr<OSData> & imagePath,uint8_t * volumeCryptKey,size_t * keySize)779*1b191cb5SApple OSS Distributions IOPolledFileOpen(const char * filename,
780*1b191cb5SApple OSS Distributions uint32_t flags,
781*1b191cb5SApple OSS Distributions uint64_t setFileSize, uint64_t fsFreeSize,
782*1b191cb5SApple OSS Distributions void * write_file_addr, size_t write_file_len,
783*1b191cb5SApple OSS Distributions IOPolledFileIOVars ** fileVars,
784*1b191cb5SApple OSS Distributions OSSharedPtr<OSData>& imagePath,
785*1b191cb5SApple OSS Distributions uint8_t * volumeCryptKey, size_t * keySize)
786*1b191cb5SApple OSS Distributions {
787*1b191cb5SApple OSS Distributions OSData* imagePathRaw = NULL;
788*1b191cb5SApple OSS Distributions IOReturn result = IOPolledFileOpen(filename, flags, setFileSize, fsFreeSize, write_file_addr, write_file_len,
789*1b191cb5SApple OSS Distributions fileVars, &imagePathRaw, volumeCryptKey, keySize);
790*1b191cb5SApple OSS Distributions imagePath.reset(imagePathRaw, OSNoRetain);
791*1b191cb5SApple OSS Distributions return result;
792*1b191cb5SApple OSS Distributions }
793*1b191cb5SApple OSS Distributions
794*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
795*1b191cb5SApple OSS Distributions
796*1b191cb5SApple OSS Distributions IOReturn
IOPolledFileClose(IOPolledFileIOVars ** pVars,off_t write_offset,void * addr,size_t write_length,off_t discard_offset,off_t discard_end,bool unlink)797*1b191cb5SApple OSS Distributions IOPolledFileClose(IOPolledFileIOVars ** pVars,
798*1b191cb5SApple OSS Distributions off_t write_offset, void * addr, size_t write_length,
799*1b191cb5SApple OSS Distributions off_t discard_offset, off_t discard_end, bool unlink)
800*1b191cb5SApple OSS Distributions {
801*1b191cb5SApple OSS Distributions IOPolledFileIOVars * vars;
802*1b191cb5SApple OSS Distributions
803*1b191cb5SApple OSS Distributions vars = *pVars;
804*1b191cb5SApple OSS Distributions if (!vars) {
805*1b191cb5SApple OSS Distributions return kIOReturnSuccess;
806*1b191cb5SApple OSS Distributions }
807*1b191cb5SApple OSS Distributions
808*1b191cb5SApple OSS Distributions if (vars->fileRef) {
809*1b191cb5SApple OSS Distributions kern_close_file_for_direct_io(vars->fileRef, write_offset, addr, write_length,
810*1b191cb5SApple OSS Distributions discard_offset, discard_end, unlink);
811*1b191cb5SApple OSS Distributions vars->fileRef = NULL;
812*1b191cb5SApple OSS Distributions }
813*1b191cb5SApple OSS Distributions if (vars->fileExtents) {
814*1b191cb5SApple OSS Distributions vars->fileExtents->release();
815*1b191cb5SApple OSS Distributions vars->fileExtents = NULL;
816*1b191cb5SApple OSS Distributions }
817*1b191cb5SApple OSS Distributions if (vars->pollers) {
818*1b191cb5SApple OSS Distributions vars->pollers->release();
819*1b191cb5SApple OSS Distributions vars->pollers = NULL;
820*1b191cb5SApple OSS Distributions }
821*1b191cb5SApple OSS Distributions
822*1b191cb5SApple OSS Distributions if (vars->allocated) {
823*1b191cb5SApple OSS Distributions IOFreeType(vars, IOPolledFileIOVars);
824*1b191cb5SApple OSS Distributions } else {
825*1b191cb5SApple OSS Distributions bzero(vars, sizeof(IOPolledFileIOVars));
826*1b191cb5SApple OSS Distributions }
827*1b191cb5SApple OSS Distributions *pVars = NULL;
828*1b191cb5SApple OSS Distributions
829*1b191cb5SApple OSS Distributions return kIOReturnSuccess;
830*1b191cb5SApple OSS Distributions }
831*1b191cb5SApple OSS Distributions
832*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
833*1b191cb5SApple OSS Distributions
834*1b191cb5SApple OSS Distributions IOReturn
IOPolledFilePollersSetup(IOPolledFileIOVars * vars,uint32_t openState)835*1b191cb5SApple OSS Distributions IOPolledFilePollersSetup(IOPolledFileIOVars * vars,
836*1b191cb5SApple OSS Distributions uint32_t openState)
837*1b191cb5SApple OSS Distributions {
838*1b191cb5SApple OSS Distributions IOReturn err;
839*1b191cb5SApple OSS Distributions
840*1b191cb5SApple OSS Distributions err = kIOReturnSuccess;
841*1b191cb5SApple OSS Distributions do{
842*1b191cb5SApple OSS Distributions if (!vars->pollers->openCount) {
843*1b191cb5SApple OSS Distributions err = IOPolledFilePollersProbe(vars->pollers);
844*1b191cb5SApple OSS Distributions if (kIOReturnSuccess != err) {
845*1b191cb5SApple OSS Distributions break;
846*1b191cb5SApple OSS Distributions }
847*1b191cb5SApple OSS Distributions }
848*1b191cb5SApple OSS Distributions err = IOPolledFilePollersOpen(vars, openState, false);
849*1b191cb5SApple OSS Distributions if (kIOReturnSuccess != err) {
850*1b191cb5SApple OSS Distributions break;
851*1b191cb5SApple OSS Distributions }
852*1b191cb5SApple OSS Distributions if ((kIOPolledPreflightState == openState) || (kIOPolledPreflightCoreDumpState == openState)) {
853*1b191cb5SApple OSS Distributions vars->pollers->openCount++;
854*1b191cb5SApple OSS Distributions }
855*1b191cb5SApple OSS Distributions vars->pollers->io = false;
856*1b191cb5SApple OSS Distributions vars->buffer = (uint8_t *) vars->pollers->ioBuffer->getBytesNoCopy();
857*1b191cb5SApple OSS Distributions vars->bufferHalf = 0;
858*1b191cb5SApple OSS Distributions vars->bufferOffset = 0;
859*1b191cb5SApple OSS Distributions assert(vars->pollers->ioBuffer->getLength() <= UINT_MAX);
860*1b191cb5SApple OSS Distributions vars->bufferSize = (typeof(vars->bufferSize))(vars->pollers->ioBuffer->getLength() >> 1);
861*1b191cb5SApple OSS Distributions
862*1b191cb5SApple OSS Distributions if (vars->maxiobytes < vars->bufferSize) {
863*1b191cb5SApple OSS Distributions vars->bufferSize = (typeof(vars->bufferSize))vars->maxiobytes;
864*1b191cb5SApple OSS Distributions }
865*1b191cb5SApple OSS Distributions }while (false);
866*1b191cb5SApple OSS Distributions
867*1b191cb5SApple OSS Distributions if (kIOReturnSuccess != err) {
868*1b191cb5SApple OSS Distributions HIBLOG("IOPolledFilePollersSetup(%d) error 0x%x\n", openState, err);
869*1b191cb5SApple OSS Distributions }
870*1b191cb5SApple OSS Distributions
871*1b191cb5SApple OSS Distributions return err;
872*1b191cb5SApple OSS Distributions }
873*1b191cb5SApple OSS Distributions
874*1b191cb5SApple OSS Distributions
875*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
876*1b191cb5SApple OSS Distributions
877*1b191cb5SApple OSS Distributions IOReturn
IOPolledFileSeek(IOPolledFileIOVars * vars,uint64_t position)878*1b191cb5SApple OSS Distributions IOPolledFileSeek(IOPolledFileIOVars * vars, uint64_t position)
879*1b191cb5SApple OSS Distributions {
880*1b191cb5SApple OSS Distributions IOPolledFileExtent * extentMap;
881*1b191cb5SApple OSS Distributions
882*1b191cb5SApple OSS Distributions extentMap = vars->extentMap;
883*1b191cb5SApple OSS Distributions
884*1b191cb5SApple OSS Distributions vars->position = position;
885*1b191cb5SApple OSS Distributions
886*1b191cb5SApple OSS Distributions if (position > vars->fileSize) {
887*1b191cb5SApple OSS Distributions HIBLOG("IOPolledFileSeek: called to seek to 0x%llx greater than file size of 0x%llx\n", vars->position, vars->fileSize);
888*1b191cb5SApple OSS Distributions return kIOReturnNoSpace;
889*1b191cb5SApple OSS Distributions }
890*1b191cb5SApple OSS Distributions
891*1b191cb5SApple OSS Distributions while (position >= extentMap->length) {
892*1b191cb5SApple OSS Distributions position -= extentMap->length;
893*1b191cb5SApple OSS Distributions extentMap++;
894*1b191cb5SApple OSS Distributions }
895*1b191cb5SApple OSS Distributions
896*1b191cb5SApple OSS Distributions vars->currentExtent = extentMap;
897*1b191cb5SApple OSS Distributions vars->extentRemaining = extentMap->length - position;
898*1b191cb5SApple OSS Distributions vars->extentPosition = vars->position - position;
899*1b191cb5SApple OSS Distributions
900*1b191cb5SApple OSS Distributions if (vars->bufferSize <= vars->extentRemaining) {
901*1b191cb5SApple OSS Distributions vars->bufferLimit = vars->bufferSize;
902*1b191cb5SApple OSS Distributions } else {
903*1b191cb5SApple OSS Distributions vars->bufferLimit = ((uint32_t) vars->extentRemaining);
904*1b191cb5SApple OSS Distributions }
905*1b191cb5SApple OSS Distributions
906*1b191cb5SApple OSS Distributions return kIOReturnSuccess;
907*1b191cb5SApple OSS Distributions }
908*1b191cb5SApple OSS Distributions
909*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
910*1b191cb5SApple OSS Distributions
911*1b191cb5SApple OSS Distributions IOReturn
IOPolledFileWrite(IOPolledFileIOVars * vars,const uint8_t * bytes,IOByteCount size,IOPolledFileCryptVars * cryptvars)912*1b191cb5SApple OSS Distributions IOPolledFileWrite(IOPolledFileIOVars * vars,
913*1b191cb5SApple OSS Distributions const uint8_t * bytes, IOByteCount size,
914*1b191cb5SApple OSS Distributions IOPolledFileCryptVars * cryptvars)
915*1b191cb5SApple OSS Distributions {
916*1b191cb5SApple OSS Distributions IOReturn err = kIOReturnSuccess;
917*1b191cb5SApple OSS Distributions IOByteCount copy, original_size = size;
918*1b191cb5SApple OSS Distributions bool flush = false;
919*1b191cb5SApple OSS Distributions
920*1b191cb5SApple OSS Distributions do{
921*1b191cb5SApple OSS Distributions if (!bytes && !size) {
922*1b191cb5SApple OSS Distributions // seek to end of block & flush
923*1b191cb5SApple OSS Distributions size = vars->position & (vars->blockSize - 1);
924*1b191cb5SApple OSS Distributions if (size) {
925*1b191cb5SApple OSS Distributions size = vars->blockSize - size;
926*1b191cb5SApple OSS Distributions }
927*1b191cb5SApple OSS Distributions flush = true;
928*1b191cb5SApple OSS Distributions }
929*1b191cb5SApple OSS Distributions
930*1b191cb5SApple OSS Distributions copy = vars->bufferLimit - vars->bufferOffset;
931*1b191cb5SApple OSS Distributions if (copy > size) {
932*1b191cb5SApple OSS Distributions copy = size;
933*1b191cb5SApple OSS Distributions } else {
934*1b191cb5SApple OSS Distributions flush = true;
935*1b191cb5SApple OSS Distributions }
936*1b191cb5SApple OSS Distributions
937*1b191cb5SApple OSS Distributions if (bytes) {
938*1b191cb5SApple OSS Distributions #if KASAN
939*1b191cb5SApple OSS Distributions /* Since this may copy mach-o segments in bulk, use the nosan variants of bcopy to
940*1b191cb5SApple OSS Distributions * avoid triggering global redzone sanitizer violations when accessing
941*1b191cb5SApple OSS Distributions * interstices between 'C' structures
942*1b191cb5SApple OSS Distributions */
943*1b191cb5SApple OSS Distributions __nosan_bcopy(bytes, vars->buffer + vars->bufferHalf + vars->bufferOffset, copy);
944*1b191cb5SApple OSS Distributions #else
945*1b191cb5SApple OSS Distributions bcopy(bytes, vars->buffer + vars->bufferHalf + vars->bufferOffset, copy);
946*1b191cb5SApple OSS Distributions #endif
947*1b191cb5SApple OSS Distributions bytes += copy;
948*1b191cb5SApple OSS Distributions } else {
949*1b191cb5SApple OSS Distributions bzero(vars->buffer + vars->bufferHalf + vars->bufferOffset, copy);
950*1b191cb5SApple OSS Distributions }
951*1b191cb5SApple OSS Distributions
952*1b191cb5SApple OSS Distributions size -= copy;
953*1b191cb5SApple OSS Distributions vars->bufferOffset += copy;
954*1b191cb5SApple OSS Distributions vars->position += copy;
955*1b191cb5SApple OSS Distributions
956*1b191cb5SApple OSS Distributions if (flush && vars->bufferOffset) {
957*1b191cb5SApple OSS Distributions uint64_t offset = (vars->position - vars->bufferOffset
958*1b191cb5SApple OSS Distributions - vars->extentPosition + vars->currentExtent->start);
959*1b191cb5SApple OSS Distributions uint32_t length = (vars->bufferOffset);
960*1b191cb5SApple OSS Distributions
961*1b191cb5SApple OSS Distributions #if CRYPTO
962*1b191cb5SApple OSS Distributions if (cryptvars && vars->encryptStart
963*1b191cb5SApple OSS Distributions && (vars->position > vars->encryptStart)
964*1b191cb5SApple OSS Distributions && ((vars->position - length) < vars->encryptEnd)) {
965*1b191cb5SApple OSS Distributions AbsoluteTime startTime, endTime;
966*1b191cb5SApple OSS Distributions
967*1b191cb5SApple OSS Distributions uint64_t encryptLen, encryptStart;
968*1b191cb5SApple OSS Distributions encryptLen = vars->position - vars->encryptStart;
969*1b191cb5SApple OSS Distributions if (encryptLen > length) {
970*1b191cb5SApple OSS Distributions encryptLen = length;
971*1b191cb5SApple OSS Distributions }
972*1b191cb5SApple OSS Distributions encryptStart = length - encryptLen;
973*1b191cb5SApple OSS Distributions if (vars->position > vars->encryptEnd) {
974*1b191cb5SApple OSS Distributions encryptLen -= (vars->position - vars->encryptEnd);
975*1b191cb5SApple OSS Distributions }
976*1b191cb5SApple OSS Distributions
977*1b191cb5SApple OSS Distributions clock_get_uptime(&startTime);
978*1b191cb5SApple OSS Distributions
979*1b191cb5SApple OSS Distributions assert(encryptLen <= UINT_MAX);
980*1b191cb5SApple OSS Distributions // encrypt the buffer
981*1b191cb5SApple OSS Distributions aes_encrypt_cbc(vars->buffer + vars->bufferHalf + encryptStart,
982*1b191cb5SApple OSS Distributions &cryptvars->aes_iv[0],
983*1b191cb5SApple OSS Distributions (unsigned int) (encryptLen / AES_BLOCK_SIZE),
984*1b191cb5SApple OSS Distributions vars->buffer + vars->bufferHalf + encryptStart,
985*1b191cb5SApple OSS Distributions &cryptvars->ctx.encrypt);
986*1b191cb5SApple OSS Distributions
987*1b191cb5SApple OSS Distributions clock_get_uptime(&endTime);
988*1b191cb5SApple OSS Distributions ADD_ABSOLUTETIME(&vars->cryptTime, &endTime);
989*1b191cb5SApple OSS Distributions SUB_ABSOLUTETIME(&vars->cryptTime, &startTime);
990*1b191cb5SApple OSS Distributions vars->cryptBytes += encryptLen;
991*1b191cb5SApple OSS Distributions
992*1b191cb5SApple OSS Distributions // save initial vector for following encrypts
993*1b191cb5SApple OSS Distributions bcopy(vars->buffer + vars->bufferHalf + encryptStart + encryptLen - AES_BLOCK_SIZE,
994*1b191cb5SApple OSS Distributions &cryptvars->aes_iv[0],
995*1b191cb5SApple OSS Distributions AES_BLOCK_SIZE);
996*1b191cb5SApple OSS Distributions }
997*1b191cb5SApple OSS Distributions #endif /* CRYPTO */
998*1b191cb5SApple OSS Distributions
999*1b191cb5SApple OSS Distributions err = IOPolledFilePollersIODone(vars->pollers, true);
1000*1b191cb5SApple OSS Distributions if (kIOReturnSuccess != err) {
1001*1b191cb5SApple OSS Distributions break;
1002*1b191cb5SApple OSS Distributions }
1003*1b191cb5SApple OSS Distributions
1004*1b191cb5SApple OSS Distributions if (vars->position & (vars->blockSize - 1)) {
1005*1b191cb5SApple OSS Distributions HIBLOG("misaligned file pos %qx\n", vars->position);
1006*1b191cb5SApple OSS Distributions }
1007*1b191cb5SApple OSS Distributions //if (length != vars->bufferSize) HIBLOG("short write of %qx ends@ %qx\n", length, offset + length);
1008*1b191cb5SApple OSS Distributions
1009*1b191cb5SApple OSS Distributions err = IOStartPolledIO(vars->pollers, kIOPolledWrite, vars->bufferHalf, offset, length);
1010*1b191cb5SApple OSS Distributions if (kIOReturnSuccess != err) {
1011*1b191cb5SApple OSS Distributions HIBLOGFROMPANIC("IOPolledFileWrite(0x%p, 0x%p, %llu, 0x%p) : IOStartPolledIO(0x%p, kIOPolledWrite, %llu, 0x%llx, %d) returned 0x%x\n",
1012*1b191cb5SApple OSS Distributions vars, bytes, (uint64_t) original_size, cryptvars, vars->pollers, (uint64_t) vars->bufferHalf, offset, length, err);
1013*1b191cb5SApple OSS Distributions break;
1014*1b191cb5SApple OSS Distributions }
1015*1b191cb5SApple OSS Distributions vars->pollers->io = true;
1016*1b191cb5SApple OSS Distributions
1017*1b191cb5SApple OSS Distributions vars->extentRemaining -= vars->bufferOffset;
1018*1b191cb5SApple OSS Distributions if (!vars->extentRemaining) {
1019*1b191cb5SApple OSS Distributions vars->currentExtent++;
1020*1b191cb5SApple OSS Distributions vars->extentRemaining = vars->currentExtent->length;
1021*1b191cb5SApple OSS Distributions vars->extentPosition = vars->position;
1022*1b191cb5SApple OSS Distributions }
1023*1b191cb5SApple OSS Distributions
1024*1b191cb5SApple OSS Distributions vars->bufferHalf = vars->bufferHalf ? 0 : vars->bufferSize;
1025*1b191cb5SApple OSS Distributions vars->bufferOffset = 0;
1026*1b191cb5SApple OSS Distributions if (vars->bufferSize <= vars->extentRemaining) {
1027*1b191cb5SApple OSS Distributions vars->bufferLimit = vars->bufferSize;
1028*1b191cb5SApple OSS Distributions } else {
1029*1b191cb5SApple OSS Distributions vars->bufferLimit = ((uint32_t) vars->extentRemaining);
1030*1b191cb5SApple OSS Distributions }
1031*1b191cb5SApple OSS Distributions
1032*1b191cb5SApple OSS Distributions if (!vars->extentRemaining) {
1033*1b191cb5SApple OSS Distributions err = kIOReturnOverrun;
1034*1b191cb5SApple OSS Distributions break;
1035*1b191cb5SApple OSS Distributions }
1036*1b191cb5SApple OSS Distributions
1037*1b191cb5SApple OSS Distributions flush = false;
1038*1b191cb5SApple OSS Distributions }
1039*1b191cb5SApple OSS Distributions }while (size);
1040*1b191cb5SApple OSS Distributions
1041*1b191cb5SApple OSS Distributions return err;
1042*1b191cb5SApple OSS Distributions }
1043*1b191cb5SApple OSS Distributions
1044*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1045*1b191cb5SApple OSS Distributions
1046*1b191cb5SApple OSS Distributions IOReturn
IOPolledFileFlush(IOPolledFileIOVars * vars)1047*1b191cb5SApple OSS Distributions IOPolledFileFlush(IOPolledFileIOVars * vars)
1048*1b191cb5SApple OSS Distributions {
1049*1b191cb5SApple OSS Distributions // Only supported by the underlying polled mode driver on embedded currently (expect kIOReturnUnsupported on other platforms)
1050*1b191cb5SApple OSS Distributions IOReturn err = kIOReturnSuccess;
1051*1b191cb5SApple OSS Distributions
1052*1b191cb5SApple OSS Distributions err = IOPolledFilePollersIODone(vars->pollers, true);
1053*1b191cb5SApple OSS Distributions if (kIOReturnSuccess != err) {
1054*1b191cb5SApple OSS Distributions return err;
1055*1b191cb5SApple OSS Distributions }
1056*1b191cb5SApple OSS Distributions
1057*1b191cb5SApple OSS Distributions err = IOStartPolledIO(vars->pollers, kIOPolledFlush, 0, 0, 0);
1058*1b191cb5SApple OSS Distributions if (kIOReturnSuccess != err) {
1059*1b191cb5SApple OSS Distributions HIBLOGFROMPANIC("IOPolledFileFlush(0x%p) : IOStartPolledIO(0x%p, kIOPolledFlush, 0, 0, 0) returned 0x%x\n",
1060*1b191cb5SApple OSS Distributions vars, vars->pollers, err);
1061*1b191cb5SApple OSS Distributions return err;
1062*1b191cb5SApple OSS Distributions }
1063*1b191cb5SApple OSS Distributions vars->pollers->io = true;
1064*1b191cb5SApple OSS Distributions
1065*1b191cb5SApple OSS Distributions return err;
1066*1b191cb5SApple OSS Distributions }
1067*1b191cb5SApple OSS Distributions
1068*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1069*1b191cb5SApple OSS Distributions
1070*1b191cb5SApple OSS Distributions IOReturn
IOPolledFileRead(IOPolledFileIOVars * vars,uint8_t * bytes,IOByteCount size,IOPolledFileCryptVars * cryptvars)1071*1b191cb5SApple OSS Distributions IOPolledFileRead(IOPolledFileIOVars * vars,
1072*1b191cb5SApple OSS Distributions uint8_t * bytes, IOByteCount size,
1073*1b191cb5SApple OSS Distributions IOPolledFileCryptVars * cryptvars)
1074*1b191cb5SApple OSS Distributions {
1075*1b191cb5SApple OSS Distributions IOReturn err = kIOReturnSuccess;
1076*1b191cb5SApple OSS Distributions IOByteCount copy;
1077*1b191cb5SApple OSS Distributions
1078*1b191cb5SApple OSS Distributions // bytesWritten += size;
1079*1b191cb5SApple OSS Distributions
1080*1b191cb5SApple OSS Distributions do{
1081*1b191cb5SApple OSS Distributions copy = vars->bufferLimit - vars->bufferOffset;
1082*1b191cb5SApple OSS Distributions if (copy > size) {
1083*1b191cb5SApple OSS Distributions copy = size;
1084*1b191cb5SApple OSS Distributions }
1085*1b191cb5SApple OSS Distributions
1086*1b191cb5SApple OSS Distributions if (bytes) {
1087*1b191cb5SApple OSS Distributions #if KASAN
1088*1b191cb5SApple OSS Distributions __nosan_bcopy(vars->buffer + vars->bufferHalf + vars->bufferOffset, bytes, copy);
1089*1b191cb5SApple OSS Distributions #else
1090*1b191cb5SApple OSS Distributions bcopy(vars->buffer + vars->bufferHalf + vars->bufferOffset, bytes, copy);
1091*1b191cb5SApple OSS Distributions #endif
1092*1b191cb5SApple OSS Distributions bytes += copy;
1093*1b191cb5SApple OSS Distributions }
1094*1b191cb5SApple OSS Distributions size -= copy;
1095*1b191cb5SApple OSS Distributions vars->bufferOffset += copy;
1096*1b191cb5SApple OSS Distributions // vars->position += copy;
1097*1b191cb5SApple OSS Distributions
1098*1b191cb5SApple OSS Distributions if ((vars->bufferOffset == vars->bufferLimit) && (vars->position < vars->readEnd)) {
1099*1b191cb5SApple OSS Distributions if (!vars->pollers->io) {
1100*1b191cb5SApple OSS Distributions cryptvars = NULL;
1101*1b191cb5SApple OSS Distributions }
1102*1b191cb5SApple OSS Distributions err = IOPolledFilePollersIODone(vars->pollers, true);
1103*1b191cb5SApple OSS Distributions if (kIOReturnSuccess != err) {
1104*1b191cb5SApple OSS Distributions break;
1105*1b191cb5SApple OSS Distributions }
1106*1b191cb5SApple OSS Distributions
1107*1b191cb5SApple OSS Distributions if (vars->position & (vars->blockSize - 1)) {
1108*1b191cb5SApple OSS Distributions HIBLOG("misaligned file pos %qx\n", vars->position);
1109*1b191cb5SApple OSS Distributions }
1110*1b191cb5SApple OSS Distributions
1111*1b191cb5SApple OSS Distributions vars->position += vars->lastRead;
1112*1b191cb5SApple OSS Distributions vars->extentRemaining -= vars->lastRead;
1113*1b191cb5SApple OSS Distributions vars->bufferLimit = vars->lastRead;
1114*1b191cb5SApple OSS Distributions
1115*1b191cb5SApple OSS Distributions if (!vars->extentRemaining) {
1116*1b191cb5SApple OSS Distributions vars->currentExtent++;
1117*1b191cb5SApple OSS Distributions vars->extentRemaining = vars->currentExtent->length;
1118*1b191cb5SApple OSS Distributions vars->extentPosition = vars->position;
1119*1b191cb5SApple OSS Distributions if (!vars->extentRemaining) {
1120*1b191cb5SApple OSS Distributions err = kIOReturnOverrun;
1121*1b191cb5SApple OSS Distributions break;
1122*1b191cb5SApple OSS Distributions }
1123*1b191cb5SApple OSS Distributions }
1124*1b191cb5SApple OSS Distributions
1125*1b191cb5SApple OSS Distributions uint32_t length;
1126*1b191cb5SApple OSS Distributions uint32_t lastReadLength = vars->lastRead;
1127*1b191cb5SApple OSS Distributions uint64_t offset = (vars->position
1128*1b191cb5SApple OSS Distributions - vars->extentPosition + vars->currentExtent->start);
1129*1b191cb5SApple OSS Distributions if (vars->extentRemaining <= vars->bufferSize) {
1130*1b191cb5SApple OSS Distributions length = ((uint32_t) vars->extentRemaining);
1131*1b191cb5SApple OSS Distributions } else {
1132*1b191cb5SApple OSS Distributions length = vars->bufferSize;
1133*1b191cb5SApple OSS Distributions }
1134*1b191cb5SApple OSS Distributions if ((length + vars->position) > vars->readEnd) {
1135*1b191cb5SApple OSS Distributions length = ((uint32_t) (vars->readEnd - vars->position));
1136*1b191cb5SApple OSS Distributions }
1137*1b191cb5SApple OSS Distributions
1138*1b191cb5SApple OSS Distributions vars->lastRead = length;
1139*1b191cb5SApple OSS Distributions if (length) {
1140*1b191cb5SApple OSS Distributions //if (length != vars->bufferSize) HIBLOG("short read of %qx ends@ %qx\n", length, offset + length);
1141*1b191cb5SApple OSS Distributions err = IOStartPolledIO(vars->pollers, kIOPolledRead, vars->bufferHalf, offset, length);
1142*1b191cb5SApple OSS Distributions if (kIOReturnSuccess != err) {
1143*1b191cb5SApple OSS Distributions break;
1144*1b191cb5SApple OSS Distributions }
1145*1b191cb5SApple OSS Distributions vars->pollers->io = true;
1146*1b191cb5SApple OSS Distributions }
1147*1b191cb5SApple OSS Distributions
1148*1b191cb5SApple OSS Distributions vars->bufferHalf = vars->bufferHalf ? 0 : vars->bufferSize;
1149*1b191cb5SApple OSS Distributions vars->bufferOffset = 0;
1150*1b191cb5SApple OSS Distributions
1151*1b191cb5SApple OSS Distributions #if CRYPTO
1152*1b191cb5SApple OSS Distributions if (cryptvars) {
1153*1b191cb5SApple OSS Distributions uint8_t thisVector[AES_BLOCK_SIZE];
1154*1b191cb5SApple OSS Distributions AbsoluteTime startTime, endTime;
1155*1b191cb5SApple OSS Distributions
1156*1b191cb5SApple OSS Distributions // save initial vector for following decrypts
1157*1b191cb5SApple OSS Distributions bcopy(&cryptvars->aes_iv[0], &thisVector[0], AES_BLOCK_SIZE);
1158*1b191cb5SApple OSS Distributions bcopy(vars->buffer + vars->bufferHalf + lastReadLength - AES_BLOCK_SIZE,
1159*1b191cb5SApple OSS Distributions &cryptvars->aes_iv[0], AES_BLOCK_SIZE);
1160*1b191cb5SApple OSS Distributions
1161*1b191cb5SApple OSS Distributions // decrypt the buffer
1162*1b191cb5SApple OSS Distributions clock_get_uptime(&startTime);
1163*1b191cb5SApple OSS Distributions
1164*1b191cb5SApple OSS Distributions assert(lastReadLength <= UINT_MAX);
1165*1b191cb5SApple OSS Distributions aes_decrypt_cbc(vars->buffer + vars->bufferHalf,
1166*1b191cb5SApple OSS Distributions &thisVector[0],
1167*1b191cb5SApple OSS Distributions (unsigned int) (lastReadLength / AES_BLOCK_SIZE),
1168*1b191cb5SApple OSS Distributions vars->buffer + vars->bufferHalf,
1169*1b191cb5SApple OSS Distributions &cryptvars->ctx.decrypt);
1170*1b191cb5SApple OSS Distributions
1171*1b191cb5SApple OSS Distributions clock_get_uptime(&endTime);
1172*1b191cb5SApple OSS Distributions ADD_ABSOLUTETIME(&vars->cryptTime, &endTime);
1173*1b191cb5SApple OSS Distributions SUB_ABSOLUTETIME(&vars->cryptTime, &startTime);
1174*1b191cb5SApple OSS Distributions vars->cryptBytes += lastReadLength;
1175*1b191cb5SApple OSS Distributions }
1176*1b191cb5SApple OSS Distributions #endif /* CRYPTO */
1177*1b191cb5SApple OSS Distributions }
1178*1b191cb5SApple OSS Distributions }while (size);
1179*1b191cb5SApple OSS Distributions
1180*1b191cb5SApple OSS Distributions return err;
1181*1b191cb5SApple OSS Distributions }
1182*1b191cb5SApple OSS Distributions
1183*1b191cb5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1184