1/*
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#ifndef _IOPOLLEDINTERFACE_H_
30#define _IOPOLLEDINTERFACE_H_
31
32enum{
33 kIOPolledPreflightState = 1,
34 kIOPolledBeforeSleepState = 2,
35 kIOPolledAfterSleepState = 3,
36 kIOPolledPostflightState = 4,
37
38 kIOPolledPreflightCoreDumpState = 5,
39 kIOPolledPostflightCoreDumpState = 6,
40
41 kIOPolledBeforeSleepStateAborted = 7,
42};
43
44#if defined(__cplusplus)
45
46#include <libkern/c++/OSObject.h>
47#include <libkern/c++/OSPtr.h>
48#include <IOKit/IOMemoryDescriptor.h>
49
50#define kIOPolledInterfaceSupportKey "IOPolledInterface"
51#define kIOPolledInterfaceActiveKey "IOPolledInterfaceActive"
52#define kIOPolledInterfaceStackKey "IOPolledInterfaceStack"
53
54enum{
55 kIOPolledWrite = 1,
56 kIOPolledRead = 2,
57 kIOPolledFlush = 3
58};
59
60typedef void (*IOPolledCompletionAction)( void * target,
61 void * parameter,
62 IOReturn status,
63 uint64_t actualByteCount);
64struct IOPolledCompletion {
65 void * target;
66 IOPolledCompletionAction action;
67 void * parameter;
68};
69
70class IOPolledInterface : public OSObject
71{
72 OSDeclareAbstractStructors(IOPolledInterface);
73
74protected:
75 struct ExpansionData { };
76 ExpansionData * reserved;
77
78public:
79 virtual IOReturn probe(IOService * target) = 0;
80
81 virtual IOReturn open( IOOptionBits state, IOMemoryDescriptor * buffer) = 0;
82 virtual IOReturn close(IOOptionBits state) = 0;
83
84 virtual IOReturn startIO(uint32_t operation,
85 uint32_t bufferOffset,
86 uint64_t deviceOffset,
87 uint64_t length,
88 IOPolledCompletion completion) = 0;
89
90 virtual IOReturn checkForWork(void) = 0;
91
92 virtual IOReturn setEncryptionKey(const uint8_t * key, size_t keySize);
93
94 OSMetaClassDeclareReservedUsedX86(IOPolledInterface, 0);
95 OSMetaClassDeclareReservedUnused(IOPolledInterface, 1);
96 OSMetaClassDeclareReservedUnused(IOPolledInterface, 2);
97 OSMetaClassDeclareReservedUnused(IOPolledInterface, 3);
98 OSMetaClassDeclareReservedUnused(IOPolledInterface, 4);
99 OSMetaClassDeclareReservedUnused(IOPolledInterface, 5);
100 OSMetaClassDeclareReservedUnused(IOPolledInterface, 6);
101 OSMetaClassDeclareReservedUnused(IOPolledInterface, 7);
102 OSMetaClassDeclareReservedUnused(IOPolledInterface, 8);
103 OSMetaClassDeclareReservedUnused(IOPolledInterface, 9);
104 OSMetaClassDeclareReservedUnused(IOPolledInterface, 10);
105 OSMetaClassDeclareReservedUnused(IOPolledInterface, 11);
106 OSMetaClassDeclareReservedUnused(IOPolledInterface, 12);
107 OSMetaClassDeclareReservedUnused(IOPolledInterface, 13);
108 OSMetaClassDeclareReservedUnused(IOPolledInterface, 14);
109 OSMetaClassDeclareReservedUnused(IOPolledInterface, 15);
110};
111
112#endif /* defined(__cplusplus) */
113
114#ifdef XNU_KERNEL_PRIVATE
115
116#include <libkern/crypto/aes.h>
117#include <IOKit/IOTypes.h>
118#include <IOKit/IOHibernatePrivate.h>
119
120// kern_open_file_for_direct_io() flags
121enum{
122 kIOPolledFileCreate = 0x00000001,
123 kIOPolledFileHibernate = 0x00000002,
124};
125
126// kern_open_file_for_direct_io() oflags
127enum{
128 kIOPolledFileSSD = 0x00000001
129};
130
131#if !defined(__cplusplus)
132typedef struct IORegistryEntry IORegistryEntry;
133typedef struct OSData OSData;
134typedef struct OSArray OSArray;
135typedef struct IOMemoryDescriptor IOMemoryDescriptor;
136typedef struct IOPolledFilePollers IOPolledFilePollers;
137#else
138class IOPolledFilePollers;
139#endif
140
141struct IOPolledFileIOVars {
142 IOPolledFilePollers * pollers;
143 struct kern_direct_file_io_ref_t * fileRef;
144 OSData * fileExtents;
145 uint64_t block0;
146 uint32_t blockSize;
147 uint64_t maxiobytes;
148 uint32_t bufferLimit;
149 uint8_t * buffer;
150 uint32_t bufferSize;
151 uint32_t bufferOffset;
152 uint32_t bufferHalf;
153 uint64_t extentRemaining;
154 uint32_t lastRead;
155 uint64_t readEnd;
156 uint32_t flags;
157 uint64_t fileSize;
158 uint64_t position;
159 uint64_t extentPosition;
160 uint64_t encryptStart;
161 uint64_t encryptEnd;
162 uint64_t cryptBytes;
163 AbsoluteTime cryptTime;
164 IOPolledFileExtent * extentMap;
165 IOPolledFileExtent * currentExtent;
166 bool allocated;
167};
168
169typedef struct IOPolledFileIOVars IOPolledFileIOVars;
170
171struct IOPolledFileCryptVars {
172 uint8_t aes_iv[AES_BLOCK_SIZE];
173 aes_ctx ctx;
174};
175typedef struct IOPolledFileCryptVars IOPolledFileCryptVars;
176
177#if defined(__cplusplus)
178
179IOReturn IOPolledFileOpen(const char * filename,
180 uint32_t flags,
181 uint64_t setFileSize, uint64_t fsFreeSize,
182 void * write_file_addr, size_t write_file_len,
183 IOPolledFileIOVars ** fileVars,
184 LIBKERN_RETURNS_RETAINED OSData ** imagePath,
185 uint8_t * volumeCryptKey, size_t * keySize);
186
187IOReturn IOPolledFileOpen(const char * filename,
188 uint32_t flags,
189 uint64_t setFileSize, uint64_t fsFreeSize,
190 void * write_file_addr, size_t write_file_len,
191 IOPolledFileIOVars ** fileVars,
192 OSSharedPtr<OSData>& imagePath,
193 uint8_t * volumeCryptKey, size_t * keySize);
194
195IOReturn IOPolledFileClose(IOPolledFileIOVars ** pVars,
196 off_t write_offset, void * addr, size_t write_length,
197 off_t discard_offset, off_t discard_end, bool unlink);
198
199IOReturn IOPolledFilePollersSetup(IOPolledFileIOVars * vars, uint32_t openState);
200
201LIBKERN_RETURNS_NOT_RETAINED IOMemoryDescriptor * IOPolledFileGetIOBuffer(IOPolledFileIOVars * vars);
202
203#endif /* defined(__cplusplus) */
204
205#if defined(__cplusplus)
206#define __C "C"
207#else
208#define __C
209#endif
210
211extern __C IOReturn IOPolledFileSeek(IOPolledFileIOVars * vars, uint64_t position);
212
213extern __C IOReturn IOPolledFileWrite(IOPolledFileIOVars * vars,
214 const uint8_t * bytes, IOByteCount size,
215 IOPolledFileCryptVars * cryptvars);
216extern __C IOReturn IOPolledFileRead(IOPolledFileIOVars * vars,
217 uint8_t * bytes, IOByteCount size,
218 IOPolledFileCryptVars * cryptvars);
219
220extern __C IOReturn IOPolledFileFlush(IOPolledFileIOVars * vars);
221
222extern __C IOReturn IOPolledFilePollersOpen(IOPolledFileIOVars * vars, uint32_t state, bool abortable);
223
224extern __C IOReturn IOPolledFilePollersClose(IOPolledFileIOVars * vars, uint32_t state);
225
226extern __C IOReturn IOPolledFilePollersSetEncryptionKey(IOPolledFileIOVars * vars,
227 const uint8_t * key, size_t keySize);
228
229extern __C IOPolledFileIOVars * gCoreFileVars;
230
231#ifdef _SYS_CONF_H_
232
233__BEGIN_DECLS
234
235typedef void (*kern_get_file_extents_callback_t)(void * ref, uint64_t start, uint64_t size);
236
237struct kern_direct_file_io_ref_t *
238kern_open_file_for_direct_io(const char * name,
239 uint32_t flags,
240 kern_get_file_extents_callback_t callback,
241 void * callback_ref,
242 off_t set_file_size,
243 off_t fs_free_size,
244 off_t write_file_offset,
245 void * write_file_addr,
246 size_t write_file_len,
247 dev_t * partition_device_result,
248 dev_t * image_device_result,
249 uint64_t * partitionbase_result,
250 uint64_t * maxiocount_result,
251 uint32_t * oflags);
252void
253kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t * ref,
254 off_t write_offset, void * addr, size_t write_length,
255 off_t discard_offset, off_t discard_end, bool unlink);
256int
257kern_write_file(struct kern_direct_file_io_ref_t * ref, off_t offset, void * addr, size_t len, int ioflag);
258int
259kern_read_file(struct kern_direct_file_io_ref_t * ref, off_t offset, void * addr, size_t len, int ioflag);
260
261struct mount *
262kern_file_mount(struct kern_direct_file_io_ref_t * ref);
263
264enum{
265 kIOPolledFileMountChangeMount = 0x00000101,
266 kIOPolledFileMountChangeUnmount = 0x00000102,
267 kIOPolledFileMountChangeWillResize = 0x00000201,
268 kIOPolledFileMountChangeDidResize = 0x00000202,
269};
270extern void IOPolledFileMountChange(struct mount * mp, uint32_t op);
271
272__END_DECLS
273
274#endif /* _SYS_CONF_H_ */
275
276#endif /* XNU_KERNEL_PRIVATE */
277
278#endif /* _IOPOLLEDINTERFACE_H_ */
279