xref: /xnu-8792.41.9/iokit/bsddev/DINetBootHook.cpp (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1*5c2921b0SApple OSS Distributions /*
2*5c2921b0SApple OSS Distributions  * Copyright (c) 2002-2016 Apple Inc. All rights reserved.
3*5c2921b0SApple OSS Distributions  *
4*5c2921b0SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5c2921b0SApple OSS Distributions  *
6*5c2921b0SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5c2921b0SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5c2921b0SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5c2921b0SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5c2921b0SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5c2921b0SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5c2921b0SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5c2921b0SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5c2921b0SApple OSS Distributions  *
15*5c2921b0SApple OSS Distributions  * Please obtain a copy of the License at
16*5c2921b0SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5c2921b0SApple OSS Distributions  *
18*5c2921b0SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5c2921b0SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5c2921b0SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5c2921b0SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5c2921b0SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5c2921b0SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5c2921b0SApple OSS Distributions  * limitations under the License.
25*5c2921b0SApple OSS Distributions  *
26*5c2921b0SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5c2921b0SApple OSS Distributions  */
28*5c2921b0SApple OSS Distributions /*
29*5c2921b0SApple OSS Distributions  *  DINetBootHook.c
30*5c2921b0SApple OSS Distributions  *  DiskImages
31*5c2921b0SApple OSS Distributions  *
32*5c2921b0SApple OSS Distributions  *  Created by Byron Han on Sat Apr 13 2002.
33*5c2921b0SApple OSS Distributions  *
34*5c2921b0SApple OSS Distributions  *	Revision History
35*5c2921b0SApple OSS Distributions  *
36*5c2921b0SApple OSS Distributions  *	$Log: DINetBootHook.cpp,v $
37*5c2921b0SApple OSS Distributions  *	Revision 1.4  2005/07/29 21:49:57  lindak
38*5c2921b0SApple OSS Distributions  *	Merge of branch "chardonnay" to pick up all chardonnay changes in Leopard
39*5c2921b0SApple OSS Distributions  *	as of xnu-792.7.4
40*5c2921b0SApple OSS Distributions  *
41*5c2921b0SApple OSS Distributions  *	Revision 1.3.1558.1  2005/06/24 01:47:25  lindak
42*5c2921b0SApple OSS Distributions  *	Bringing over all of the Karma changes into chardonnay.
43*5c2921b0SApple OSS Distributions  *
44*5c2921b0SApple OSS Distributions  *	Revision 1.1.1.1  2005/02/24 21:48:06  akosut
45*5c2921b0SApple OSS Distributions  *	Import xnu-764 from Tiger8A395
46*5c2921b0SApple OSS Distributions  *
47*5c2921b0SApple OSS Distributions  *	Revision 1.3  2002/06/16 20:36:02  lindak
48*5c2921b0SApple OSS Distributions  *	Merged PR-2957314 into Jaguar (siegmund: netboot kernel code needs to set
49*5c2921b0SApple OSS Distributions  *	com.apple.AppleDiskImageController.load to boolean Yes)
50*5c2921b0SApple OSS Distributions  *
51*5c2921b0SApple OSS Distributions  *	Revision 1.2.40.2  2002/06/15 03:50:38  dieter
52*5c2921b0SApple OSS Distributions  *	- corrected com.apple.AppleDiskImageController.load string
53*5c2921b0SApple OSS Distributions  *
54*5c2921b0SApple OSS Distributions  *	Revision 1.2.40.1  2002/06/15 03:01:08  dieter
55*5c2921b0SApple OSS Distributions  *	Bug #: 2957314
56*5c2921b0SApple OSS Distributions  *	- add call to force IOHDIXController to get loaded/matched
57*5c2921b0SApple OSS Distributions  *
58*5c2921b0SApple OSS Distributions  *	Revision 1.2  2002/05/03 18:08:39  lindak
59*5c2921b0SApple OSS Distributions  *	Merged PR-2909558 into Jaguar (siegmund POST WWDC: add support for NetBoot
60*5c2921b0SApple OSS Distributions  *	over IOHDIXController)
61*5c2921b0SApple OSS Distributions  *
62*5c2921b0SApple OSS Distributions  *	Revision 1.1.2.1  2002/04/24 22:29:12  dieter
63*5c2921b0SApple OSS Distributions  *	Bug #: 2909558
64*5c2921b0SApple OSS Distributions  *	- added IOHDIXController netboot stubs
65*5c2921b0SApple OSS Distributions  *
66*5c2921b0SApple OSS Distributions  *	Revision 1.3  2002/04/16 00:41:37  han
67*5c2921b0SApple OSS Distributions  *	migrated code out of here to IOHDIXController's setProperty method
68*5c2921b0SApple OSS Distributions  *
69*5c2921b0SApple OSS Distributions  *	Revision 1.2  2002/04/14 23:53:53  han
70*5c2921b0SApple OSS Distributions  *	eliminate qDEBUG=1, use emums instead of hard coded string constants
71*5c2921b0SApple OSS Distributions  *
72*5c2921b0SApple OSS Distributions  *	Revision 1.1  2002/04/14 22:54:42  han
73*5c2921b0SApple OSS Distributions  *	Renamed from DINetBookHook.c.
74*5c2921b0SApple OSS Distributions  *	First stab at implementing this code.
75*5c2921b0SApple OSS Distributions  *
76*5c2921b0SApple OSS Distributions  *	Revision 1.1  2002/04/13 19:22:28  han
77*5c2921b0SApple OSS Distributions  *	added stub file DINetBookHook.c
78*5c2921b0SApple OSS Distributions  *
79*5c2921b0SApple OSS Distributions  *
80*5c2921b0SApple OSS Distributions  */
81*5c2921b0SApple OSS Distributions #ifndef qDEBUG
82*5c2921b0SApple OSS Distributions #define qDEBUG 0
83*5c2921b0SApple OSS Distributions #endif
84*5c2921b0SApple OSS Distributions 
85*5c2921b0SApple OSS Distributions #if qDEBUG
86*5c2921b0SApple OSS Distributions #warning qDEBUG is 1!
87*5c2921b0SApple OSS Distributions #endif
88*5c2921b0SApple OSS Distributions 
89*5c2921b0SApple OSS Distributions #include <sys/types.h>
90*5c2921b0SApple OSS Distributions #include <mach/clock_types.h>
91*5c2921b0SApple OSS Distributions #include <IOKit/IOService.h>
92*5c2921b0SApple OSS Distributions #include <IOKit/IOLib.h>
93*5c2921b0SApple OSS Distributions #include "DINetBootHook.h"
94*5c2921b0SApple OSS Distributions 
95*5c2921b0SApple OSS Distributions #define kIOHDIXControllerClassName      "IOHDIXController"
96*5c2921b0SApple OSS Distributions #define kDIRootImageKey                         "di-root-image"
97*5c2921b0SApple OSS Distributions #define kDIRootImageRemovableKey                "di-root-removable"
98*5c2921b0SApple OSS Distributions #define kDIRootImageResultKey           "di-root-image-result"
99*5c2921b0SApple OSS Distributions #define kDIRootImageDevNameKey          "di-root-image-devname"
100*5c2921b0SApple OSS Distributions #define kDIRootImageDevTKey                     "di-root-image-devt"
101*5c2921b0SApple OSS Distributions #define kDIRootRamFileKey           "di-root-ram-file"
102*5c2921b0SApple OSS Distributions 
103*5c2921b0SApple OSS Distributions #define kDIMatchQuiesceTimeout          30ull
104*5c2921b0SApple OSS Distributions 
105*5c2921b0SApple OSS Distributions static IOService *
di_load_controller(void)106*5c2921b0SApple OSS Distributions di_load_controller( void )
107*5c2921b0SApple OSS Distributions {
108*5c2921b0SApple OSS Distributions 	OSIterator *    controllerIterator      = NULL;
109*5c2921b0SApple OSS Distributions 	OSDictionary *  matchDictionary         = NULL;
110*5c2921b0SApple OSS Distributions 	IOService *     controller                      = NULL;
111*5c2921b0SApple OSS Distributions 
112*5c2921b0SApple OSS Distributions 	do {
113*5c2921b0SApple OSS Distributions 		IOService::getResourceService()->publishResource("com.apple.AppleDiskImageController.load", kOSBooleanTrue);
114*5c2921b0SApple OSS Distributions 		IOService::getResourceService()->waitQuiet();
115*5c2921b0SApple OSS Distributions 
116*5c2921b0SApple OSS Distributions 		// first find IOHDIXController
117*5c2921b0SApple OSS Distributions 		matchDictionary = IOService::serviceMatching(kIOHDIXControllerClassName);
118*5c2921b0SApple OSS Distributions 		if (!matchDictionary) {
119*5c2921b0SApple OSS Distributions 			break;
120*5c2921b0SApple OSS Distributions 		}
121*5c2921b0SApple OSS Distributions 
122*5c2921b0SApple OSS Distributions 		controllerIterator = IOService::getMatchingServices(matchDictionary);
123*5c2921b0SApple OSS Distributions 		if (!controllerIterator) {
124*5c2921b0SApple OSS Distributions 			break;
125*5c2921b0SApple OSS Distributions 		}
126*5c2921b0SApple OSS Distributions 
127*5c2921b0SApple OSS Distributions 		controller = OSDynamicCast(IOService, controllerIterator->getNextObject());
128*5c2921b0SApple OSS Distributions 		if (!controller) {
129*5c2921b0SApple OSS Distributions 			break;
130*5c2921b0SApple OSS Distributions 		}
131*5c2921b0SApple OSS Distributions 
132*5c2921b0SApple OSS Distributions 		controller->retain();
133*5c2921b0SApple OSS Distributions 	} while (false);
134*5c2921b0SApple OSS Distributions 
135*5c2921b0SApple OSS Distributions 	if (matchDictionary) {
136*5c2921b0SApple OSS Distributions 		matchDictionary->release();
137*5c2921b0SApple OSS Distributions 	}
138*5c2921b0SApple OSS Distributions 	if (controllerIterator) {
139*5c2921b0SApple OSS Distributions 		controllerIterator->release();
140*5c2921b0SApple OSS Distributions 	}
141*5c2921b0SApple OSS Distributions 
142*5c2921b0SApple OSS Distributions 	return controller;
143*5c2921b0SApple OSS Distributions }
144*5c2921b0SApple OSS Distributions 
145*5c2921b0SApple OSS Distributions extern "C" {
146*5c2921b0SApple OSS Distributions /* FIXME: removable should be replaced with a struct (so it could be easily
147*5c2921b0SApple OSS Distributions  * extensible in the future). However, since there is no common header file
148*5c2921b0SApple OSS Distributions  * between imageboot and NetBoot, we opt for a simple bool for now.
149*5c2921b0SApple OSS Distributions  * Refactor this into a common header file.
150*5c2921b0SApple OSS Distributions  */
151*5c2921b0SApple OSS Distributions static int
di_add_properties(IOService * controller,bool removable)152*5c2921b0SApple OSS Distributions di_add_properties(IOService *controller, bool removable)
153*5c2921b0SApple OSS Distributions {
154*5c2921b0SApple OSS Distributions 	if (!controller->setProperty(kDIRootImageRemovableKey, removable ? kOSBooleanTrue : kOSBooleanFalse)) {
155*5c2921b0SApple OSS Distributions 		IOLog("IOHDIXController::setProperty(%s, %d) failed.\n", kDIRootImageRemovableKey, !!removable);
156*5c2921b0SApple OSS Distributions 		return kIOReturnBadArgument;
157*5c2921b0SApple OSS Distributions 	}
158*5c2921b0SApple OSS Distributions 
159*5c2921b0SApple OSS Distributions 	return kIOReturnSuccess;
160*5c2921b0SApple OSS Distributions }
161*5c2921b0SApple OSS Distributions 
162*5c2921b0SApple OSS Distributions int
di_root_image_ext(const char * path,char * devname,size_t devsz,dev_t * dev_p,bool removable)163*5c2921b0SApple OSS Distributions di_root_image_ext(const char *path, char *devname, size_t devsz, dev_t *dev_p, bool removable)
164*5c2921b0SApple OSS Distributions {
165*5c2921b0SApple OSS Distributions 	IOReturn                        res                             = 0;
166*5c2921b0SApple OSS Distributions 	IOService               *       controller                      = NULL;
167*5c2921b0SApple OSS Distributions 	OSString                *       pathString                      = NULL;
168*5c2921b0SApple OSS Distributions 	OSNumber                *       myResult                        = NULL;
169*5c2921b0SApple OSS Distributions 	OSString                *       myDevName                       = NULL;
170*5c2921b0SApple OSS Distributions 	OSNumber                *       myDevT                          = NULL;
171*5c2921b0SApple OSS Distributions 
172*5c2921b0SApple OSS Distributions 	// sanity check arguments please
173*5c2921b0SApple OSS Distributions 	if (devname) {
174*5c2921b0SApple OSS Distributions 		*devname = 0;
175*5c2921b0SApple OSS Distributions 	}
176*5c2921b0SApple OSS Distributions 	if (dev_p) {
177*5c2921b0SApple OSS Distributions 		*dev_p = 0;
178*5c2921b0SApple OSS Distributions 	}
179*5c2921b0SApple OSS Distributions 
180*5c2921b0SApple OSS Distributions 	if (!path) {
181*5c2921b0SApple OSS Distributions 		return kIOReturnBadArgument;
182*5c2921b0SApple OSS Distributions 	}
183*5c2921b0SApple OSS Distributions 	if (!devname) {
184*5c2921b0SApple OSS Distributions 		return kIOReturnBadArgument;
185*5c2921b0SApple OSS Distributions 	}
186*5c2921b0SApple OSS Distributions 	if (!dev_p) {
187*5c2921b0SApple OSS Distributions 		return kIOReturnBadArgument;
188*5c2921b0SApple OSS Distributions 	}
189*5c2921b0SApple OSS Distributions 
190*5c2921b0SApple OSS Distributions 	controller = di_load_controller();
191*5c2921b0SApple OSS Distributions 	if (!controller) {
192*5c2921b0SApple OSS Distributions 		res = kIOReturnNotFound;
193*5c2921b0SApple OSS Distributions 		goto NoIOHDIXController;
194*5c2921b0SApple OSS Distributions 	}
195*5c2921b0SApple OSS Distributions 
196*5c2921b0SApple OSS Distributions 	// okay create path object
197*5c2921b0SApple OSS Distributions 	pathString = OSString::withCString(path);
198*5c2921b0SApple OSS Distributions 	if (!pathString) {
199*5c2921b0SApple OSS Distributions 		res = kIOReturnNoMemory;
200*5c2921b0SApple OSS Distributions 		goto CannotCreatePathOSString;
201*5c2921b0SApple OSS Distributions 	}
202*5c2921b0SApple OSS Distributions 
203*5c2921b0SApple OSS Distributions 	/*
204*5c2921b0SApple OSS Distributions 	 * This is a bit racy, as two concurrent attached could have
205*5c2921b0SApple OSS Distributions 	 * different properties. However, since we query the result and dev
206*5c2921b0SApple OSS Distributions 	 * below locklessly, the existing code is already racy, so we
207*5c2921b0SApple OSS Distributions 	 * keep the status quo.
208*5c2921b0SApple OSS Distributions 	 */
209*5c2921b0SApple OSS Distributions 	res = di_add_properties(controller, removable);
210*5c2921b0SApple OSS Distributions 	if (res) {
211*5c2921b0SApple OSS Distributions 		goto error_add_properties;
212*5c2921b0SApple OSS Distributions 	}
213*5c2921b0SApple OSS Distributions 
214*5c2921b0SApple OSS Distributions 	// do it
215*5c2921b0SApple OSS Distributions 	if (!controller->setProperty(kDIRootImageKey, pathString)) {
216*5c2921b0SApple OSS Distributions 		IOLog("IOHDIXController::setProperty(%s, %s) failed.\n", kDIRootImageKey, pathString->getCStringNoCopy());
217*5c2921b0SApple OSS Distributions 	}
218*5c2921b0SApple OSS Distributions 
219*5c2921b0SApple OSS Distributions 	myResult = OSDynamicCast(OSNumber, controller->getProperty(kDIRootImageResultKey));
220*5c2921b0SApple OSS Distributions 	res = kIOReturnError;
221*5c2921b0SApple OSS Distributions 	if (myResult) {
222*5c2921b0SApple OSS Distributions 		res = myResult->unsigned32BitValue();
223*5c2921b0SApple OSS Distributions 	}
224*5c2921b0SApple OSS Distributions 
225*5c2921b0SApple OSS Distributions 	if (res) {
226*5c2921b0SApple OSS Distributions 		IOLog("%s is 0x%08X/%d\n", kDIRootImageResultKey, res, res);
227*5c2921b0SApple OSS Distributions 		goto di_root_image_FAILED;
228*5c2921b0SApple OSS Distributions 	}
229*5c2921b0SApple OSS Distributions 
230*5c2921b0SApple OSS Distributions 	// success - grab
231*5c2921b0SApple OSS Distributions 	myDevT = OSDynamicCast(OSNumber, controller->getProperty(kDIRootImageDevTKey));
232*5c2921b0SApple OSS Distributions 	if (myDevT) {
233*5c2921b0SApple OSS Distributions 		*dev_p = myDevT->unsigned32BitValue();
234*5c2921b0SApple OSS Distributions 	} else {
235*5c2921b0SApple OSS Distributions 		IOLog("could not get %s\n", kDIRootImageDevTKey);
236*5c2921b0SApple OSS Distributions 		res = kIOReturnError;
237*5c2921b0SApple OSS Distributions 		goto di_root_image_FAILED;
238*5c2921b0SApple OSS Distributions 	}
239*5c2921b0SApple OSS Distributions 
240*5c2921b0SApple OSS Distributions 	myDevName = OSDynamicCast(OSString, controller->getProperty(kDIRootImageDevNameKey));
241*5c2921b0SApple OSS Distributions 	if (myDevName) {
242*5c2921b0SApple OSS Distributions 		strlcpy(devname, myDevName->getCStringNoCopy(), devsz);
243*5c2921b0SApple OSS Distributions 	} else {
244*5c2921b0SApple OSS Distributions 		IOLog("could not get %s\n", kDIRootImageDevNameKey);
245*5c2921b0SApple OSS Distributions 		res = kIOReturnError;
246*5c2921b0SApple OSS Distributions 		goto di_root_image_FAILED;
247*5c2921b0SApple OSS Distributions 	}
248*5c2921b0SApple OSS Distributions 
249*5c2921b0SApple OSS Distributions 	/*
250*5c2921b0SApple OSS Distributions 	 * NOTE: The attached disk image may trigger IOKit matching. At the very least, an IOMedia
251*5c2921b0SApple OSS Distributions 	 * must claim it.  More complex scenarios might include a GPT containing a partition mapping
252*5c2921b0SApple OSS Distributions 	 * to an APFS container, both of which need to probe and claim their respective media devices.
253*5c2921b0SApple OSS Distributions 	 *
254*5c2921b0SApple OSS Distributions 	 * After the attach is complete, we should quiesce the disk image controller before returning
255*5c2921b0SApple OSS Distributions 	 * from this function successfully.  If we failed to quiesce, then we should treat it as a hard
256*5c2921b0SApple OSS Distributions 	 * failure, to make it more obvious to triage.
257*5c2921b0SApple OSS Distributions 	 */
258*5c2921b0SApple OSS Distributions 	res = controller->waitQuiet((NSEC_PER_SEC * kDIMatchQuiesceTimeout));
259*5c2921b0SApple OSS Distributions 	if (res) {
260*5c2921b0SApple OSS Distributions 		IOLog("failed to quiesce attached disk image (%s)! \n", devname);
261*5c2921b0SApple OSS Distributions 		goto di_root_image_FAILED;
262*5c2921b0SApple OSS Distributions 	}
263*5c2921b0SApple OSS Distributions 
264*5c2921b0SApple OSS Distributions di_root_image_FAILED:
265*5c2921b0SApple OSS Distributions CannotCreatePathOSString:
266*5c2921b0SApple OSS Distributions NoIOHDIXController:
267*5c2921b0SApple OSS Distributions error_add_properties:
268*5c2921b0SApple OSS Distributions 
269*5c2921b0SApple OSS Distributions 	// clean up memory allocations
270*5c2921b0SApple OSS Distributions 	if (pathString) {
271*5c2921b0SApple OSS Distributions 		pathString->release();
272*5c2921b0SApple OSS Distributions 	}
273*5c2921b0SApple OSS Distributions 	if (controller) {
274*5c2921b0SApple OSS Distributions 		controller->release();
275*5c2921b0SApple OSS Distributions 	}
276*5c2921b0SApple OSS Distributions 
277*5c2921b0SApple OSS Distributions 	return res;
278*5c2921b0SApple OSS Distributions }
279*5c2921b0SApple OSS Distributions 
280*5c2921b0SApple OSS Distributions /*
281*5c2921b0SApple OSS Distributions  *       Name:		di_root_image
282*5c2921b0SApple OSS Distributions  *       Function:	mount the disk image returning the dev node
283*5c2921b0SApple OSS Distributions  *       Parameters:	path	->		path/url to disk image
284*5c2921b0SApple OSS Distributions  *                               devname	<-		dev node used to set the rootdevice global variable
285*5c2921b0SApple OSS Distributions  *                               dev_p	<-		device number generated from major/minor numbers
286*5c2921b0SApple OSS Distributions  *       Comments:
287*5c2921b0SApple OSS Distributions  *       This is an exported function. Changing this will break API.
288*5c2921b0SApple OSS Distributions  */
289*5c2921b0SApple OSS Distributions int
di_root_image(const char * path,char * devname,size_t devsz,dev_t * dev_p)290*5c2921b0SApple OSS Distributions di_root_image(const char *path, char *devname, size_t devsz, dev_t *dev_p)
291*5c2921b0SApple OSS Distributions {
292*5c2921b0SApple OSS Distributions 	return di_root_image_ext(path, devname, devsz, dev_p, false);
293*5c2921b0SApple OSS Distributions }
294*5c2921b0SApple OSS Distributions 
295*5c2921b0SApple OSS Distributions int
di_root_ramfile_buf(void * buf,size_t bufsz,char * devname,size_t devsz,dev_t * dev_p)296*5c2921b0SApple OSS Distributions di_root_ramfile_buf(void *buf, size_t bufsz, char *devname, size_t devsz, dev_t *dev_p)
297*5c2921b0SApple OSS Distributions {
298*5c2921b0SApple OSS Distributions 	IOReturn res = 0;
299*5c2921b0SApple OSS Distributions 	IOService *controller = NULL;
300*5c2921b0SApple OSS Distributions 	OSNumber *myResult = NULL;
301*5c2921b0SApple OSS Distributions 	OSString *myDevName = NULL;
302*5c2921b0SApple OSS Distributions 	OSNumber *myDevT = NULL;
303*5c2921b0SApple OSS Distributions 	IOMemoryDescriptor *mem = NULL;
304*5c2921b0SApple OSS Distributions 
305*5c2921b0SApple OSS Distributions 	/* Use kIOMemoryAutoPrepare and wire down the buffer so readBytes() will work. */
306*5c2921b0SApple OSS Distributions 	mem = IOMemoryDescriptor::withAddressRange(
307*5c2921b0SApple OSS Distributions 		(mach_vm_address_t)buf, (mach_vm_size_t)bufsz,
308*5c2921b0SApple OSS Distributions 		kIODirectionOut | kIOMemoryAutoPrepare, kernel_task);
309*5c2921b0SApple OSS Distributions 	if (!mem) {
310*5c2921b0SApple OSS Distributions 		res = kIOReturnNoMemory;
311*5c2921b0SApple OSS Distributions 		goto out;
312*5c2921b0SApple OSS Distributions 	}
313*5c2921b0SApple OSS Distributions 
314*5c2921b0SApple OSS Distributions 	controller = di_load_controller();
315*5c2921b0SApple OSS Distributions 	if (controller) {
316*5c2921b0SApple OSS Distributions 		/* attach the image */
317*5c2921b0SApple OSS Distributions 		controller->setProperty(kDIRootRamFileKey, mem);
318*5c2921b0SApple OSS Distributions 		controller->release();
319*5c2921b0SApple OSS Distributions 	} else {
320*5c2921b0SApple OSS Distributions 		res = kIOReturnNotFound;
321*5c2921b0SApple OSS Distributions 		goto out;
322*5c2921b0SApple OSS Distributions 	}
323*5c2921b0SApple OSS Distributions 
324*5c2921b0SApple OSS Distributions 	myResult = OSDynamicCast(OSNumber, controller->getProperty(kDIRootImageResultKey));
325*5c2921b0SApple OSS Distributions 	res = kIOReturnError;
326*5c2921b0SApple OSS Distributions 	if (myResult) {
327*5c2921b0SApple OSS Distributions 		res = myResult->unsigned32BitValue();
328*5c2921b0SApple OSS Distributions 	}
329*5c2921b0SApple OSS Distributions 
330*5c2921b0SApple OSS Distributions 	if (res) {
331*5c2921b0SApple OSS Distributions 		IOLog("%s is 0x%08X/%d\n", kDIRootImageResultKey, res, res);
332*5c2921b0SApple OSS Distributions 		goto out;
333*5c2921b0SApple OSS Distributions 	}
334*5c2921b0SApple OSS Distributions 
335*5c2921b0SApple OSS Distributions 	myDevT = OSDynamicCast(OSNumber, controller->getProperty(kDIRootImageDevTKey));
336*5c2921b0SApple OSS Distributions 	if (myDevT) {
337*5c2921b0SApple OSS Distributions 		*dev_p = myDevT->unsigned32BitValue();
338*5c2921b0SApple OSS Distributions 	} else {
339*5c2921b0SApple OSS Distributions 		IOLog("could not get %s\n", kDIRootImageDevTKey);
340*5c2921b0SApple OSS Distributions 		res = kIOReturnError;
341*5c2921b0SApple OSS Distributions 		goto out;
342*5c2921b0SApple OSS Distributions 	}
343*5c2921b0SApple OSS Distributions 
344*5c2921b0SApple OSS Distributions 	myDevName = OSDynamicCast(OSString, controller->getProperty(kDIRootImageDevNameKey));
345*5c2921b0SApple OSS Distributions 	if (myDevName) {
346*5c2921b0SApple OSS Distributions 		strlcpy(devname, myDevName->getCStringNoCopy(), devsz);
347*5c2921b0SApple OSS Distributions 	} else {
348*5c2921b0SApple OSS Distributions 		IOLog("could not get %s\n", kDIRootImageDevNameKey);
349*5c2921b0SApple OSS Distributions 		res = kIOReturnError;
350*5c2921b0SApple OSS Distributions 		goto out;
351*5c2921b0SApple OSS Distributions 	}
352*5c2921b0SApple OSS Distributions 
353*5c2921b0SApple OSS Distributions out:
354*5c2921b0SApple OSS Distributions 	OSSafeReleaseNULL(mem);
355*5c2921b0SApple OSS Distributions 	return res;
356*5c2921b0SApple OSS Distributions }
357*5c2921b0SApple OSS Distributions 
358*5c2921b0SApple OSS Distributions void
di_root_ramfile(IORegistryEntry * entry)359*5c2921b0SApple OSS Distributions di_root_ramfile( IORegistryEntry * entry )
360*5c2921b0SApple OSS Distributions {
361*5c2921b0SApple OSS Distributions 	OSData *                data;
362*5c2921b0SApple OSS Distributions 	IOMemoryDescriptor *    mem;
363*5c2921b0SApple OSS Distributions 	uint64_t                dmgSize;
364*5c2921b0SApple OSS Distributions 	uint64_t                remain, length;
365*5c2921b0SApple OSS Distributions 	OSData *                extentData = NULL;
366*5c2921b0SApple OSS Distributions 	IOAddressRange *        extentList;
367*5c2921b0SApple OSS Distributions 	uint64_t                extentSize;
368*5c2921b0SApple OSS Distributions 	uint32_t                extentCount;
369*5c2921b0SApple OSS Distributions 
370*5c2921b0SApple OSS Distributions 	do {
371*5c2921b0SApple OSS Distributions 		data = OSDynamicCast(OSData, entry->getProperty("boot-ramdmg-size"));
372*5c2921b0SApple OSS Distributions 		if (!data || (data->getLength() != sizeof(uint64_t))) {
373*5c2921b0SApple OSS Distributions 			break; // bad disk image size
374*5c2921b0SApple OSS Distributions 		}
375*5c2921b0SApple OSS Distributions 		dmgSize = *(uint64_t *) data->getBytesNoCopy();
376*5c2921b0SApple OSS Distributions 		if (!dmgSize) {
377*5c2921b0SApple OSS Distributions 			break;
378*5c2921b0SApple OSS Distributions 		}
379*5c2921b0SApple OSS Distributions 
380*5c2921b0SApple OSS Distributions 		data = OSDynamicCast(OSData, entry->getProperty("boot-ramdmg-extents"));
381*5c2921b0SApple OSS Distributions 		if (!data || (data->getLength() == 0) ||
382*5c2921b0SApple OSS Distributions 		    ((data->getLength() & (sizeof(IOAddressRange) - 1)) != 0)) {
383*5c2921b0SApple OSS Distributions 			break; // bad extents
384*5c2921b0SApple OSS Distributions 		}
385*5c2921b0SApple OSS Distributions 		// make modifications to local copy
386*5c2921b0SApple OSS Distributions 		extentData  = OSData::withData(data);
387*5c2921b0SApple OSS Distributions 		assert(extentData);
388*5c2921b0SApple OSS Distributions 
389*5c2921b0SApple OSS Distributions 		extentList  = (IOAddressRange *) extentData->getBytesNoCopy();
390*5c2921b0SApple OSS Distributions 		extentCount = extentData->getLength() / sizeof(IOAddressRange);
391*5c2921b0SApple OSS Distributions 		extentSize  = 0;
392*5c2921b0SApple OSS Distributions 		remain = dmgSize;
393*5c2921b0SApple OSS Distributions 
394*5c2921b0SApple OSS Distributions 		// truncate extent length to enclosing disk image
395*5c2921b0SApple OSS Distributions 		for (uint32_t i = 0; i < extentCount; i++) {
396*5c2921b0SApple OSS Distributions 			length = extentList[i].length;
397*5c2921b0SApple OSS Distributions 			if (!length) {
398*5c2921b0SApple OSS Distributions 				break;
399*5c2921b0SApple OSS Distributions 			}
400*5c2921b0SApple OSS Distributions 
401*5c2921b0SApple OSS Distributions 			extentSize += length;
402*5c2921b0SApple OSS Distributions 			if (length >= remain) {
403*5c2921b0SApple OSS Distributions 				extentList[i].length = remain;
404*5c2921b0SApple OSS Distributions 				extentCount = i + 1;
405*5c2921b0SApple OSS Distributions 				break;
406*5c2921b0SApple OSS Distributions 			}
407*5c2921b0SApple OSS Distributions 			remain -= length;
408*5c2921b0SApple OSS Distributions 		}
409*5c2921b0SApple OSS Distributions 		if (extentSize < dmgSize) {
410*5c2921b0SApple OSS Distributions 			break; // not enough extent bytes for enclosing disk image
411*5c2921b0SApple OSS Distributions 		}
412*5c2921b0SApple OSS Distributions 		mem = IOMemoryDescriptor::withAddressRanges(
413*5c2921b0SApple OSS Distributions 			extentList, extentCount,
414*5c2921b0SApple OSS Distributions 			kIODirectionOut | kIOMemoryMapperNone, NULL);
415*5c2921b0SApple OSS Distributions 
416*5c2921b0SApple OSS Distributions 		if (mem) {
417*5c2921b0SApple OSS Distributions 			IOService * controller = di_load_controller();
418*5c2921b0SApple OSS Distributions 			if (controller) {
419*5c2921b0SApple OSS Distributions 				controller->setProperty(kDIRootRamFileKey, mem);
420*5c2921b0SApple OSS Distributions 				controller->release();
421*5c2921b0SApple OSS Distributions 			}
422*5c2921b0SApple OSS Distributions 			mem->release();
423*5c2921b0SApple OSS Distributions 		}
424*5c2921b0SApple OSS Distributions 	} while (false);
425*5c2921b0SApple OSS Distributions 
426*5c2921b0SApple OSS Distributions 	if (extentData) {
427*5c2921b0SApple OSS Distributions 		extentData->release();
428*5c2921b0SApple OSS Distributions 	}
429*5c2921b0SApple OSS Distributions }
430*5c2921b0SApple OSS Distributions };
431