| 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 | |
| 32 | enum |
| 33 | { |
| 34 | kIOPolledPreflightState = 1, |
| 35 | kIOPolledBeforeSleepState = 2, |
| 36 | kIOPolledAfterSleepState = 3, |
| 37 | kIOPolledPostflightState = 4, |
| 38 | |
| 39 | kIOPolledPreflightCoreDumpState = 5, |
| 40 | kIOPolledPostflightCoreDumpState = 6, |
| 41 | |
| 42 | kIOPolledBeforeSleepStateAborted = 7, |
| 43 | }; |
| 44 | |
| 45 | #if defined(__cplusplus) |
| 46 | |
| 47 | #include <libkern/c++/OSObject.h> |
| 48 | #include <IOKit/IOMemoryDescriptor.h> |
| 49 | |
| 50 | #define kIOPolledInterfaceSupportKey "IOPolledInterface" |
| 51 | #define kIOPolledInterfaceActiveKey "IOPolledInterfaceActive" |
| 52 | #define kIOPolledInterfaceStackKey "IOPolledInterfaceStack" |
| 53 | |
| 54 | enum |
| 55 | { |
| 56 | kIOPolledWrite = 1, |
| 57 | kIOPolledRead = 2, |
| 58 | kIOPolledFlush = 3 |
| 59 | }; |
| 60 | |
| 61 | typedef void (*IOPolledCompletionAction)( void * target, |
| 62 | void * parameter, |
| 63 | IOReturn status, |
| 64 | uint64_t actualByteCount); |
| 65 | struct IOPolledCompletion |
| 66 | { |
| 67 | void * target; |
| 68 | IOPolledCompletionAction action; |
| 69 | void * parameter; |
| 70 | }; |
| 71 | |
| 72 | class IOPolledInterface : public OSObject |
| 73 | { |
| 74 | OSDeclareAbstractStructors(IOPolledInterface); |
| 75 | |
| 76 | protected: |
| 77 | struct ExpansionData { }; |
| 78 | ExpansionData * reserved; |
| 79 | |
| 80 | public: |
| 81 | virtual IOReturn probe(IOService * target) = 0; |
| 82 | |
| 83 | virtual IOReturn open( IOOptionBits state, IOMemoryDescriptor * buffer) = 0; |
| 84 | virtual IOReturn close(IOOptionBits state) = 0; |
| 85 | |
| 86 | virtual IOReturn startIO(uint32_t operation, |
| 87 | uint32_t bufferOffset, |
| 88 | uint64_t deviceOffset, |
| 89 | uint64_t length, |
| 90 | IOPolledCompletion completion) = 0; |
| 91 | |
| 92 | virtual IOReturn checkForWork(void) = 0; |
| 93 | |
| 94 | virtual IOReturn setEncryptionKey(const uint8_t * key, size_t keySize); |
| 95 | |
| 96 | OSMetaClassDeclareReservedUsed(IOPolledInterface, 0); |
| 97 | OSMetaClassDeclareReservedUnused(IOPolledInterface, 1); |
| 98 | OSMetaClassDeclareReservedUnused(IOPolledInterface, 2); |
| 99 | OSMetaClassDeclareReservedUnused(IOPolledInterface, 3); |
| 100 | OSMetaClassDeclareReservedUnused(IOPolledInterface, 4); |
| 101 | OSMetaClassDeclareReservedUnused(IOPolledInterface, 5); |
| 102 | OSMetaClassDeclareReservedUnused(IOPolledInterface, 6); |
| 103 | OSMetaClassDeclareReservedUnused(IOPolledInterface, 7); |
| 104 | OSMetaClassDeclareReservedUnused(IOPolledInterface, 8); |
| 105 | OSMetaClassDeclareReservedUnused(IOPolledInterface, 9); |
| 106 | OSMetaClassDeclareReservedUnused(IOPolledInterface, 10); |
| 107 | OSMetaClassDeclareReservedUnused(IOPolledInterface, 11); |
| 108 | OSMetaClassDeclareReservedUnused(IOPolledInterface, 12); |
| 109 | OSMetaClassDeclareReservedUnused(IOPolledInterface, 13); |
| 110 | OSMetaClassDeclareReservedUnused(IOPolledInterface, 14); |
| 111 | OSMetaClassDeclareReservedUnused(IOPolledInterface, 15); |
| 112 | }; |
| 113 | |
| 114 | #endif /* defined(__cplusplus) */ |
| 115 | |
| 116 | #ifdef XNU_KERNEL_PRIVATE |
| 117 | |
| 118 | #include <libkern/crypto/aes.h> |
| 119 | #include <IOKit/IOTypes.h> |
| 120 | #include <IOKit/IOHibernatePrivate.h> |
| 121 | |
| 122 | // kern_open_file_for_direct_io() flags |
| 123 | enum |
| 124 | { |
| 125 | kIOPolledFileCreate = 0x00000001, |
| 126 | kIOPolledFileHibernate = 0x00000002, |
| 127 | }; |
| 128 | |
| 129 | // kern_open_file_for_direct_io() oflags |
| 130 | enum |
| 131 | { |
| 132 | kIOPolledFileSSD = 0x00000001 |
| 133 | }; |
| 134 | |
| 135 | #if !defined(__cplusplus) |
| 136 | typedef struct IORegistryEntry IORegistryEntry; |
| 137 | typedef struct OSData OSData; |
| 138 | typedef struct OSArray OSArray; |
| 139 | typedef struct IOMemoryDescriptor IOMemoryDescriptor; |
| 140 | typedef struct IOPolledFilePollers IOPolledFilePollers; |
| 141 | #else |
| 142 | class IOPolledFilePollers; |
| 143 | #endif |
| 144 | |
| 145 | struct IOPolledFileIOVars |
| 146 | { |
| 147 | IOPolledFilePollers * pollers; |
| 148 | struct kern_direct_file_io_ref_t * fileRef; |
| 149 | OSData * fileExtents; |
| 150 | uint64_t block0; |
| 151 | IOByteCount blockSize; |
| 152 | uint64_t maxiobytes; |
| 153 | IOByteCount bufferLimit; |
| 154 | uint8_t * buffer; |
| 155 | IOByteCount bufferSize; |
| 156 | IOByteCount bufferOffset; |
| 157 | IOByteCount bufferHalf; |
| 158 | IOByteCount extentRemaining; |
| 159 | IOByteCount lastRead; |
| 160 | IOByteCount readEnd; |
| 161 | uint32_t flags; |
| 162 | uint64_t fileSize; |
| 163 | uint64_t position; |
| 164 | uint64_t extentPosition; |
| 165 | uint64_t encryptStart; |
| 166 | uint64_t encryptEnd; |
| 167 | uint64_t cryptBytes; |
| 168 | AbsoluteTime cryptTime; |
| 169 | IOPolledFileExtent * extentMap; |
| 170 | IOPolledFileExtent * currentExtent; |
| 171 | bool allocated; |
| 172 | }; |
| 173 | |
| 174 | typedef struct IOPolledFileIOVars IOPolledFileIOVars; |
| 175 | |
| 176 | struct IOPolledFileCryptVars |
| 177 | { |
| 178 | uint8_t aes_iv[AES_BLOCK_SIZE]; |
| 179 | aes_ctx ctx; |
| 180 | }; |
| 181 | typedef struct IOPolledFileCryptVars IOPolledFileCryptVars; |
| 182 | |
| 183 | #if defined(__cplusplus) |
| 184 | |
| 185 | IOReturn IOPolledFileOpen(const char * filename, |
| 186 | uint32_t flags, |
| 187 | uint64_t setFileSize, uint64_t fsFreeSize, |
| 188 | void * write_file_addr, size_t write_file_len, |
| 189 | IOPolledFileIOVars ** fileVars, |
| 190 | OSData ** imagePath, |
| 191 | uint8_t * volumeCryptKey, size_t * keySize); |
| 192 | |
| 193 | IOReturn IOPolledFileClose(IOPolledFileIOVars ** pVars, |
| 194 | off_t write_offset, void * addr, size_t write_length, |
| 195 | off_t discard_offset, off_t discard_end); |
| 196 | |
| 197 | IOReturn (IOPolledFileIOVars * vars, uint32_t openState); |
| 198 | |
| 199 | IOMemoryDescriptor * IOPolledFileGetIOBuffer(IOPolledFileIOVars * vars); |
| 200 | |
| 201 | #endif /* defined(__cplusplus) */ |
| 202 | |
| 203 | #if defined(__cplusplus) |
| 204 | #define __C "C" |
| 205 | #else |
| 206 | #define __C |
| 207 | #endif |
| 208 | |
| 209 | extern __C IOReturn IOPolledFileSeek(IOPolledFileIOVars * vars, uint64_t position); |
| 210 | |
| 211 | extern __C IOReturn IOPolledFileWrite(IOPolledFileIOVars * vars, |
| 212 | const uint8_t * bytes, IOByteCount size, |
| 213 | IOPolledFileCryptVars * cryptvars); |
| 214 | extern __C IOReturn IOPolledFileRead(IOPolledFileIOVars * vars, |
| 215 | uint8_t * bytes, IOByteCount size, |
| 216 | IOPolledFileCryptVars * cryptvars); |
| 217 | |
| 218 | extern __C IOReturn IOPolledFileFlush(IOPolledFileIOVars * vars); |
| 219 | |
| 220 | extern __C IOReturn IOPolledFilePollersOpen(IOPolledFileIOVars * vars, uint32_t state, bool abortable); |
| 221 | |
| 222 | extern __C IOReturn IOPolledFilePollersClose(IOPolledFileIOVars * vars, uint32_t state); |
| 223 | |
| 224 | extern __C IOReturn (IOPolledFileIOVars * vars, |
| 225 | const uint8_t * key, size_t keySize); |
| 226 | |
| 227 | extern __C IOPolledFileIOVars * gCoreFileVars; |
| 228 | |
| 229 | #ifdef _SYS_CONF_H_ |
| 230 | |
| 231 | __BEGIN_DECLS |
| 232 | |
| 233 | typedef void (*kern_get_file_extents_callback_t)(void * ref, uint64_t start, uint64_t size); |
| 234 | |
| 235 | struct kern_direct_file_io_ref_t * |
| 236 | kern_open_file_for_direct_io(const char * name, |
| 237 | uint32_t flags, |
| 238 | kern_get_file_extents_callback_t callback, |
| 239 | void * callback_ref, |
| 240 | off_t set_file_size, |
| 241 | off_t fs_free_size, |
| 242 | off_t write_file_offset, |
| 243 | void * write_file_addr, |
| 244 | size_t write_file_len, |
| 245 | dev_t * partition_device_result, |
| 246 | dev_t * image_device_result, |
| 247 | uint64_t * partitionbase_result, |
| 248 | uint64_t * maxiocount_result, |
| 249 | uint32_t * oflags); |
| 250 | void |
| 251 | kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t * ref, |
| 252 | off_t write_offset, void * addr, size_t write_length, |
| 253 | off_t discard_offset, off_t discard_end); |
| 254 | int |
| 255 | kern_write_file(struct kern_direct_file_io_ref_t * ref, off_t offset, void * addr, size_t len, int ioflag); |
| 256 | int |
| 257 | kern_read_file(struct kern_direct_file_io_ref_t * ref, off_t offset, void * addr, size_t len, int ioflag); |
| 258 | |
| 259 | struct mount * |
| 260 | kern_file_mount(struct kern_direct_file_io_ref_t * ref); |
| 261 | |
| 262 | enum |
| 263 | { |
| 264 | kIOPolledFileMountChangeMount = 0x00000101, |
| 265 | kIOPolledFileMountChangeUnmount = 0x00000102, |
| 266 | kIOPolledFileMountChangeWillResize = 0x00000201, |
| 267 | kIOPolledFileMountChangeDidResize = 0x00000202, |
| 268 | }; |
| 269 | extern void IOPolledFileMountChange(struct mount * mp, uint32_t op); |
| 270 | |
| 271 | __END_DECLS |
| 272 | |
| 273 | #endif /* _SYS_CONF_H_ */ |
| 274 | |
| 275 | #endif /* XNU_KERNEL_PRIVATE */ |
| 276 | |
| 277 | #endif /* _IOPOLLEDINTERFACE_H_ */ |
| 278 | |