1/*
2 * Copyright (c) 2002-2006 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 *  DINetBootHook.c
30 *  DiskImages
31 *
32 *  Created by Byron Han on Sat Apr 13 2002.
33 *
34 *	Revision History
35 *
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
39 *	as of xnu-792.7.4
40 *
41 *	Revision 1.3.1558.1  2005/06/24 01:47:25  lindak
42 *	Bringing over all of the Karma changes into chardonnay.
43 *
44 *	Revision 1.1.1.1  2005/02/24 21:48:06  akosut
45 *	Import xnu-764 from Tiger8A395
46 *
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)
50 *
51 *	Revision 1.2.40.2  2002/06/15 03:50:38  dieter
52 *	- corrected com.apple.AppleDiskImageController.load string
53 *
54 *	Revision 1.2.40.1  2002/06/15 03:01:08  dieter
55 *	Bug #: 2957314
56 *	- add call to force IOHDIXController to get loaded/matched
57 *
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)
61 *
62 *	Revision 1.1.2.1  2002/04/24 22:29:12  dieter
63 *	Bug #: 2909558
64 *	- added IOHDIXController netboot stubs
65 *
66 *	Revision 1.3  2002/04/16 00:41:37  han
67 *	migrated code out of here to IOHDIXController's setProperty method
68 *
69 *	Revision 1.2  2002/04/14 23:53:53  han
70 *	eliminate qDEBUG=1, use emums instead of hard coded string constants
71 *
72 *	Revision 1.1  2002/04/14 22:54:42  han
73 *	Renamed from DINetBookHook.c.
74 *	First stab at implementing this code.
75 *
76 *	Revision 1.1  2002/04/13 19:22:28  han
77 *	added stub file DINetBookHook.c
78 *
79 *
80 */
81#ifndef qDEBUG
82#define qDEBUG 0
83#endif
84
85#if qDEBUG
86#warning qDEBUG is 1!
87#endif
88
89#include <sys/types.h>
90#include <IOKit/IOService.h>
91#include <IOKit/IOLib.h>
92
93#define	kIOHDIXControllerClassName	"IOHDIXController"
94#define	kDIRootImageKey				"di-root-image"
95#define	kDIRootImageResultKey		"di-root-image-result"
96#define	kDIRootImageDevNameKey		"di-root-image-devname"
97#define	kDIRootImageDevTKey			"di-root-image-devt"
98
99extern "C" {
100/*
101	Name:		di_root_image
102	Function:	mount the disk image returning the dev node
103	Parameters:	path	->		path/url to disk image
104				devname	<-		dev node used to set the rootdevice global variable
105				dev_p	<-		device number generated from major/minor numbers
106	Comments:
107*/
108int di_root_image(const char *path, char devname[], dev_t *dev_p)
109{
110	IOReturn			res 				= 0;
111	OSIterator		*	controllerIterator 	= 0;
112	OSDictionary 	*	matchDictionary 	= 0;
113	IOService		*	controller			= 0;
114	OSString		*	pathString			= 0;
115	OSNumber		*	myResult			= 0;
116	OSString		*	myDevName			= 0;
117	OSNumber		*	myDevT				= 0;
118
119	// sanity check arguments please
120	if (devname)		*devname = 0;
121	if (dev_p)			*dev_p = 0;
122
123	if (!path) 			return kIOReturnBadArgument;
124	if (!devname) 		return kIOReturnBadArgument;
125	if (!dev_p) 		return kIOReturnBadArgument;
126
127	(void)IOService::getResourceService()->publishResource("com.apple.AppleDiskImageController.load", kOSBooleanTrue);
128	IOService::getResourceService()->waitQuiet();
129
130	// first find IOHDIXController
131	matchDictionary = IOService::serviceMatching(kIOHDIXControllerClassName);
132	if (!matchDictionary) {
133		res = kIOReturnNoMemory;
134		goto serviceMatching_FAILED;
135	}
136
137	controllerIterator = IOService::getMatchingServices(matchDictionary);
138	if (!controllerIterator) {
139		res = kIOReturnNoMemory;
140		goto getMatchingServices_FAILED;
141	}
142
143	// use the "setProperty" method of IOHDIXController to trigger the desired behaviour
144	controller = OSDynamicCast(IOService, controllerIterator->getNextObject());
145	if (!controller) {
146		res = kIOReturnNotFound;
147		goto NoIOHDIXController;
148	}
149
150	// okay create path object
151	pathString = OSString::withCString(path);
152	if (!pathString) {
153		res = kIOReturnNoMemory;
154		goto CannotCreatePathOSString;
155	}
156
157	// do it
158	if (!controller->setProperty(kDIRootImageKey, pathString))
159		IOLog("IOHDIXController::setProperty(%s, %s) failed.\n", kDIRootImageKey, pathString->getCStringNoCopy());
160
161	myResult = OSDynamicCast(OSNumber, controller->getProperty(kDIRootImageResultKey));
162	res = kIOReturnError;
163	if (myResult)
164		res = myResult->unsigned32BitValue();
165
166	if (res) {
167		IOLog("%s is 0x%08X/%d\n", kDIRootImageResultKey, res, res);
168		goto di_root_image_FAILED;
169	}
170
171	// success - grab
172	myDevT = OSDynamicCast(OSNumber, controller->getProperty(kDIRootImageDevTKey));
173	if (myDevT)
174		*dev_p = myDevT->unsigned32BitValue();
175	else {
176		IOLog("could not get %s\n", kDIRootImageDevTKey);
177		res = kIOReturnError;
178		goto di_root_image_FAILED;
179	}
180
181	myDevName = OSDynamicCast(OSString, controller->getProperty(kDIRootImageDevNameKey));
182	if (myDevName) {
183		/* rootdevice is 16 chars in bsd_init.c */
184		strlcpy(devname, myDevName->getCStringNoCopy(), 16);
185	} else {
186		IOLog("could not get %s\n", kDIRootImageDevNameKey);
187		res = kIOReturnError;
188		goto di_root_image_FAILED;
189	}
190
191
192di_root_image_FAILED:
193CannotCreatePathOSString:
194serviceMatching_FAILED:
195NoIOHDIXController:
196getMatchingServices_FAILED:
197
198	// clean up memory allocations
199	if (pathString)			pathString->release();
200	if (matchDictionary)	matchDictionary->release();
201	if (controllerIterator)	controllerIterator->release();
202
203	return res;
204}
205
206};
207