2 * Copyright (c) 2002-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Created by Byron Han on Sat Apr 13 2002.
36 * $Log: DINetBootHook.cpp,v $
37 * Revision 1.4 2005/07/29 21:49:57 lindak
38 * Merge of branch "chardonnay" to pick up all chardonnay changes in Leopard
41 * Revision 1.3.1558.1 2005/06/24 01:47:25 lindak
42 * Bringing over all of the Karma changes into chardonnay.
44 * Revision 1.1.1.1 2005/02/24 21:48:06 akosut
45 * Import xnu-764 from Tiger8A395
47 * Revision 1.3 2002/06/16 20:36:02 lindak
48 * Merged PR-2957314 into Jaguar (siegmund: netboot kernel code needs to set
49 * com.apple.AppleDiskImageController.load to boolean Yes)
51 * Revision 1.2.40.2 2002/06/15 03:50:38 dieter
52 * - corrected com.apple.AppleDiskImageController.load string
54 * Revision 1.2.40.1 2002/06/15 03:01:08 dieter
56 * - add call to force IOHDIXController to get loaded/matched
58 * Revision 1.2 2002/05/03 18:08:39 lindak
59 * Merged PR-2909558 into Jaguar (siegmund POST WWDC: add support for NetBoot
60 * over IOHDIXController)
62 * Revision 1.1.2.1 2002/04/24 22:29:12 dieter
64 * - added IOHDIXController netboot stubs
66 * Revision 1.3 2002/04/16 00:41:37 han
67 * migrated code out of here to IOHDIXController's setProperty method
69 * Revision 1.2 2002/04/14 23:53:53 han
70 * eliminate qDEBUG=1, use emums instead of hard coded string constants
72 * Revision 1.1 2002/04/14 22:54:42 han
73 * Renamed from DINetBookHook.c.
74 * First stab at implementing this code.
76 * Revision 1.1 2002/04/13 19:22:28 han
77 * added stub file DINetBookHook.c
89 #include <sys/types.h>
90 #include <mach/clock_types.h>
91 #include <IOKit/IOService.h>
92 #include <IOKit/IOLib.h>
93 #include "DINetBootHook.h"
95 #define kIOHDIXControllerClassName "IOHDIXController"
96 #define kDIRootImageKey "di-root-image"
97 #define kDIRootImageRemovableKey "di-root-removable"
98 #define kDIRootImageResultKey "di-root-image-result"
99 #define kDIRootImageDevNameKey "di-root-image-devname"
100 #define kDIRootImageDevTKey "di-root-image-devt"
101 #define kDIRootRamFileKey "di-root-ram-file"
103 #define kDIMatchQuiesceTimeout 30ull
106 di_load_controller( void )
108 OSIterator
* controllerIterator
= NULL
;
109 OSDictionary
* matchDictionary
= NULL
;
110 IOService
* controller
= NULL
;
113 IOService::getResourceService()->publishResource("com.apple.AppleDiskImageController.load", kOSBooleanTrue
);
114 IOService::getResourceService()->waitQuiet();
116 // first find IOHDIXController
117 matchDictionary
= IOService::serviceMatching(kIOHDIXControllerClassName
);
118 if (!matchDictionary
) {
122 controllerIterator
= IOService::getMatchingServices(matchDictionary
);
123 if (!controllerIterator
) {
127 controller
= OSDynamicCast(IOService
, controllerIterator
->getNextObject());
132 controller
->retain();
135 if (matchDictionary
) {
136 matchDictionary
->release();
138 if (controllerIterator
) {
139 controllerIterator
->release();
146 /* FIXME: removable should be replaced with a struct (so it could be easily
147 * extensible in the future). However, since there is no common header file
148 * between imageboot and NetBoot, we opt for a simple bool for now.
149 * Refactor this into a common header file.
152 di_add_properties(IOService
*controller
, bool removable
)
154 if (!controller
->setProperty(kDIRootImageRemovableKey
, removable
? kOSBooleanTrue
: kOSBooleanFalse
)) {
155 IOLog("IOHDIXController::setProperty(%s, %d) failed.\n", kDIRootImageRemovableKey
, !!removable
);
156 return kIOReturnBadArgument
;
159 return kIOReturnSuccess
;
163 di_root_image_ext(const char *path
, char *devname
, size_t devsz
, dev_t
*dev_p
, bool removable
)
166 IOService
* controller
= NULL
;
167 OSString
* pathString
= NULL
;
168 OSNumber
* myResult
= NULL
;
169 OSString
* myDevName
= NULL
;
170 OSNumber
* myDevT
= NULL
;
172 // sanity check arguments please
181 return kIOReturnBadArgument
;
184 return kIOReturnBadArgument
;
187 return kIOReturnBadArgument
;
190 controller
= di_load_controller();
192 res
= kIOReturnNotFound
;
193 goto NoIOHDIXController
;
196 // okay create path object
197 pathString
= OSString::withCString(path
);
199 res
= kIOReturnNoMemory
;
200 goto CannotCreatePathOSString
;
204 * This is a bit racy, as two concurrent attached could have
205 * different properties. However, since we query the result and dev
206 * below locklessly, the existing code is already racy, so we
207 * keep the status quo.
209 res
= di_add_properties(controller
, removable
);
211 goto error_add_properties
;
215 if (!controller
->setProperty(kDIRootImageKey
, pathString
)) {
216 IOLog("IOHDIXController::setProperty(%s, %s) failed.\n", kDIRootImageKey
, pathString
->getCStringNoCopy());
219 myResult
= OSDynamicCast(OSNumber
, controller
->getProperty(kDIRootImageResultKey
));
220 res
= kIOReturnError
;
222 res
= myResult
->unsigned32BitValue();
226 IOLog("%s is 0x%08X/%d\n", kDIRootImageResultKey
, res
, res
);
227 goto di_root_image_FAILED
;
231 myDevT
= OSDynamicCast(OSNumber
, controller
->getProperty(kDIRootImageDevTKey
));
233 *dev_p
= myDevT
->unsigned32BitValue();
235 IOLog("could not get %s\n", kDIRootImageDevTKey
);
236 res
= kIOReturnError
;
237 goto di_root_image_FAILED
;
240 myDevName
= OSDynamicCast(OSString
, controller
->getProperty(kDIRootImageDevNameKey
));
242 strlcpy(devname
, myDevName
->getCStringNoCopy(), devsz
);
244 IOLog("could not get %s\n", kDIRootImageDevNameKey
);
245 res
= kIOReturnError
;
246 goto di_root_image_FAILED
;
250 * NOTE: The attached disk image may trigger IOKit matching. At the very least, an IOMedia
251 * must claim it. More complex scenarios might include a GPT containing a partition mapping
252 * to an APFS container, both of which need to probe and claim their respective media devices.
254 * After the attach is complete, we should quiesce the disk image controller before returning
255 * from this function successfully. If we failed to quiesce, then we should treat it as a hard
256 * failure, to make it more obvious to triage.
258 res
= controller
->waitQuiet((NSEC_PER_SEC
* kDIMatchQuiesceTimeout
));
260 IOLog("failed to quiesce attached disk image (%s)! \n", devname
);
261 goto di_root_image_FAILED
;
264 di_root_image_FAILED
:
265 CannotCreatePathOSString
:
267 error_add_properties
:
269 // clean up memory allocations
271 pathString
->release();
274 controller
->release();
281 * Name: di_root_image
282 * Function: mount the disk image returning the dev node
283 * Parameters: path -> path/url to disk image
284 * devname <- dev node used to set the rootdevice global variable
285 * dev_p <- device number generated from major/minor numbers
287 * This is an exported function. Changing this will break API.
290 di_root_image(const char *path
, char *devname
, size_t devsz
, dev_t
*dev_p
)
292 return di_root_image_ext(path
, devname
, devsz
, dev_p
, false);
296 di_root_ramfile_buf(void *buf
, size_t bufsz
, char *devname
, size_t devsz
, dev_t
*dev_p
)
299 IOService
*controller
= NULL
;
300 OSNumber
*myResult
= NULL
;
301 OSString
*myDevName
= NULL
;
302 OSNumber
*myDevT
= NULL
;
303 IOMemoryDescriptor
*mem
= NULL
;
305 mem
= IOMemoryDescriptor::withAddress(buf
, bufsz
, kIODirectionInOut
);
308 controller
= di_load_controller();
310 /* attach the image */
311 controller
->setProperty(kDIRootRamFileKey
, mem
);
312 controller
->release();
314 res
= kIOReturnNotFound
;
318 myResult
= OSDynamicCast(OSNumber
, controller
->getProperty(kDIRootImageResultKey
));
319 res
= kIOReturnError
;
321 res
= myResult
->unsigned32BitValue();
325 IOLog("%s is 0x%08X/%d\n", kDIRootImageResultKey
, res
, res
);
329 myDevT
= OSDynamicCast(OSNumber
, controller
->getProperty(kDIRootImageDevTKey
));
331 *dev_p
= myDevT
->unsigned32BitValue();
333 IOLog("could not get %s\n", kDIRootImageDevTKey
);
334 res
= kIOReturnError
;
338 myDevName
= OSDynamicCast(OSString
, controller
->getProperty(kDIRootImageDevNameKey
));
340 strlcpy(devname
, myDevName
->getCStringNoCopy(), devsz
);
342 IOLog("could not get %s\n", kDIRootImageDevNameKey
);
343 res
= kIOReturnError
;
349 OSSafeReleaseNULL(mem
);
356 di_root_ramfile( IORegistryEntry
* entry
)
359 IOMemoryDescriptor
* mem
;
361 uint64_t remain
, length
;
362 OSData
* extentData
= NULL
;
363 IOAddressRange
* extentList
;
365 uint32_t extentCount
;
368 data
= OSDynamicCast(OSData
, entry
->getProperty("boot-ramdmg-size"));
369 if (!data
|| (data
->getLength() != sizeof(uint64_t))) {
370 break; // bad disk image size
372 dmgSize
= *(uint64_t *) data
->getBytesNoCopy();
377 data
= OSDynamicCast(OSData
, entry
->getProperty("boot-ramdmg-extents"));
378 if (!data
|| (data
->getLength() == 0) ||
379 ((data
->getLength() & (sizeof(IOAddressRange
) - 1)) != 0)) {
380 break; // bad extents
382 // make modifications to local copy
383 extentData
= OSData::withData(data
);
386 extentList
= (IOAddressRange
*) extentData
->getBytesNoCopy();
387 extentCount
= extentData
->getLength() / sizeof(IOAddressRange
);
391 // truncate extent length to enclosing disk image
392 for (uint32_t i
= 0; i
< extentCount
; i
++) {
393 length
= extentList
[i
].length
;
398 extentSize
+= length
;
399 if (length
>= remain
) {
400 extentList
[i
].length
= remain
;
406 if (extentSize
< dmgSize
) {
407 break; // not enough extent bytes for enclosing disk image
409 mem
= IOMemoryDescriptor::withAddressRanges(
410 extentList
, extentCount
,
411 kIODirectionOut
| kIOMemoryMapperNone
, NULL
);
414 IOService
* controller
= di_load_controller();
416 controller
->setProperty(kDIRootRamFileKey
, mem
);
417 controller
->release();
424 extentData
->release();