]>
Commit | Line | Data |
---|---|---|
2d21ac55 | 1 | /* |
39037602 | 2 | * Copyright (c) 2002-2016 Apple Inc. All rights reserved. |
2d21ac55 A |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
0a7de745 | 5 | * |
2d21ac55 A |
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. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 A |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ | |
9bccf70c A |
28 | /* |
29 | * DINetBootHook.c | |
30 | * DiskImages | |
31 | * | |
32 | * Created by Byron Han on Sat Apr 13 2002. | |
9bccf70c A |
33 | * |
34 | * Revision History | |
35 | * | |
36 | * $Log: DINetBootHook.cpp,v $ | |
2d21ac55 A |
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 | * | |
0c530ab8 A |
41 | * Revision 1.3.1558.1 2005/06/24 01:47:25 lindak |
42 | * Bringing over all of the Karma changes into chardonnay. | |
0a7de745 | 43 | * |
0c530ab8 A |
44 | * Revision 1.1.1.1 2005/02/24 21:48:06 akosut |
45 | * Import xnu-764 from Tiger8A395 | |
0a7de745 | 46 | * |
9bccf70c A |
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) | |
0a7de745 | 50 | * |
9bccf70c A |
51 | * Revision 1.2.40.2 2002/06/15 03:50:38 dieter |
52 | * - corrected com.apple.AppleDiskImageController.load string | |
0a7de745 | 53 | * |
9bccf70c A |
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 | |
0a7de745 | 57 | * |
9bccf70c A |
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) | |
0a7de745 | 61 | * |
9bccf70c A |
62 | * Revision 1.1.2.1 2002/04/24 22:29:12 dieter |
63 | * Bug #: 2909558 | |
64 | * - added IOHDIXController netboot stubs | |
0a7de745 | 65 | * |
9bccf70c A |
66 | * Revision 1.3 2002/04/16 00:41:37 han |
67 | * migrated code out of here to IOHDIXController's setProperty method | |
0a7de745 | 68 | * |
9bccf70c A |
69 | * Revision 1.2 2002/04/14 23:53:53 han |
70 | * eliminate qDEBUG=1, use emums instead of hard coded string constants | |
0a7de745 | 71 | * |
9bccf70c A |
72 | * Revision 1.1 2002/04/14 22:54:42 han |
73 | * Renamed from DINetBookHook.c. | |
74 | * First stab at implementing this code. | |
0a7de745 | 75 | * |
9bccf70c A |
76 | * Revision 1.1 2002/04/13 19:22:28 han |
77 | * added stub file DINetBookHook.c | |
0a7de745 | 78 | * |
9bccf70c A |
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> | |
39037602 | 92 | #include "DINetBootHook.h" |
9bccf70c | 93 | |
0a7de745 A |
94 | #define kIOHDIXControllerClassName "IOHDIXController" |
95 | #define kDIRootImageKey "di-root-image" | |
96 | #define kDIRootImageResultKey "di-root-image-result" | |
97 | #define kDIRootImageDevNameKey "di-root-image-devname" | |
98 | #define kDIRootImageDevTKey "di-root-image-devt" | |
6d2010ae A |
99 | #define kDIRootRamFileKey "di-root-ram-file" |
100 | ||
101 | static IOService * | |
102 | di_load_controller( void ) | |
103 | { | |
cb323159 A |
104 | OSIterator * controllerIterator = NULL; |
105 | OSDictionary * matchDictionary = NULL; | |
106 | IOService * controller = NULL; | |
0a7de745 A |
107 | |
108 | do { | |
109 | IOService::getResourceService()->publishResource("com.apple.AppleDiskImageController.load", kOSBooleanTrue); | |
110 | IOService::getResourceService()->waitQuiet(); | |
111 | ||
112 | // first find IOHDIXController | |
113 | matchDictionary = IOService::serviceMatching(kIOHDIXControllerClassName); | |
114 | if (!matchDictionary) { | |
115 | break; | |
116 | } | |
117 | ||
118 | controllerIterator = IOService::getMatchingServices(matchDictionary); | |
119 | if (!controllerIterator) { | |
120 | break; | |
121 | } | |
122 | ||
123 | controller = OSDynamicCast(IOService, controllerIterator->getNextObject()); | |
124 | if (!controller) { | |
125 | break; | |
126 | } | |
127 | ||
128 | controller->retain(); | |
129 | } while (false); | |
130 | ||
131 | if (matchDictionary) { | |
132 | matchDictionary->release(); | |
133 | } | |
134 | if (controllerIterator) { | |
135 | controllerIterator->release(); | |
136 | } | |
6d2010ae | 137 | |
0a7de745 | 138 | return controller; |
6d2010ae | 139 | } |
9bccf70c A |
140 | |
141 | extern "C" { | |
142 | /* | |
0a7de745 A |
143 | * Name: di_root_image |
144 | * Function: mount the disk image returning the dev node | |
145 | * Parameters: path -> path/url to disk image | |
146 | * devname <- dev node used to set the rootdevice global variable | |
147 | * dev_p <- device number generated from major/minor numbers | |
148 | * Comments: | |
149 | */ | |
150 | int | |
151 | di_root_image(const char *path, char *devname, size_t devsz, dev_t *dev_p) | |
9bccf70c | 152 | { |
0a7de745 | 153 | IOReturn res = 0; |
cb323159 A |
154 | IOService * controller = NULL; |
155 | OSString * pathString = NULL; | |
156 | OSNumber * myResult = NULL; | |
157 | OSString * myDevName = NULL; | |
158 | OSNumber * myDevT = NULL; | |
0a7de745 | 159 | |
9bccf70c | 160 | // sanity check arguments please |
0a7de745 A |
161 | if (devname) { |
162 | *devname = 0; | |
163 | } | |
164 | if (dev_p) { | |
165 | *dev_p = 0; | |
166 | } | |
167 | ||
168 | if (!path) { | |
169 | return kIOReturnBadArgument; | |
170 | } | |
171 | if (!devname) { | |
172 | return kIOReturnBadArgument; | |
173 | } | |
174 | if (!dev_p) { | |
175 | return kIOReturnBadArgument; | |
176 | } | |
177 | ||
178 | controller = di_load_controller(); | |
9bccf70c A |
179 | if (!controller) { |
180 | res = kIOReturnNotFound; | |
181 | goto NoIOHDIXController; | |
182 | } | |
0a7de745 | 183 | |
9bccf70c A |
184 | // okay create path object |
185 | pathString = OSString::withCString(path); | |
186 | if (!pathString) { | |
187 | res = kIOReturnNoMemory; | |
188 | goto CannotCreatePathOSString; | |
189 | } | |
0a7de745 | 190 | |
9bccf70c | 191 | // do it |
0a7de745 | 192 | if (!controller->setProperty(kDIRootImageKey, pathString)) { |
9bccf70c | 193 | IOLog("IOHDIXController::setProperty(%s, %s) failed.\n", kDIRootImageKey, pathString->getCStringNoCopy()); |
0a7de745 A |
194 | } |
195 | ||
9bccf70c A |
196 | myResult = OSDynamicCast(OSNumber, controller->getProperty(kDIRootImageResultKey)); |
197 | res = kIOReturnError; | |
0a7de745 | 198 | if (myResult) { |
9bccf70c | 199 | res = myResult->unsigned32BitValue(); |
0a7de745 A |
200 | } |
201 | ||
9bccf70c A |
202 | if (res) { |
203 | IOLog("%s is 0x%08X/%d\n", kDIRootImageResultKey, res, res); | |
204 | goto di_root_image_FAILED; | |
205 | } | |
206 | ||
0a7de745 | 207 | // success - grab |
9bccf70c | 208 | myDevT = OSDynamicCast(OSNumber, controller->getProperty(kDIRootImageDevTKey)); |
0a7de745 | 209 | if (myDevT) { |
9bccf70c | 210 | *dev_p = myDevT->unsigned32BitValue(); |
0a7de745 | 211 | } else { |
9bccf70c A |
212 | IOLog("could not get %s\n", kDIRootImageDevTKey); |
213 | res = kIOReturnError; | |
214 | goto di_root_image_FAILED; | |
215 | } | |
0a7de745 | 216 | |
9bccf70c | 217 | myDevName = OSDynamicCast(OSString, controller->getProperty(kDIRootImageDevNameKey)); |
2d21ac55 | 218 | if (myDevName) { |
5ba3f43e | 219 | strlcpy(devname, myDevName->getCStringNoCopy(), devsz); |
2d21ac55 | 220 | } else { |
9bccf70c A |
221 | IOLog("could not get %s\n", kDIRootImageDevNameKey); |
222 | res = kIOReturnError; | |
223 | goto di_root_image_FAILED; | |
224 | } | |
0a7de745 | 225 | |
9bccf70c A |
226 | |
227 | di_root_image_FAILED: | |
228 | CannotCreatePathOSString: | |
9bccf70c | 229 | NoIOHDIXController: |
9bccf70c A |
230 | |
231 | // clean up memory allocations | |
0a7de745 A |
232 | if (pathString) { |
233 | pathString->release(); | |
234 | } | |
235 | if (controller) { | |
236 | controller->release(); | |
237 | } | |
9bccf70c A |
238 | |
239 | return res; | |
240 | } | |
241 | ||
5ba3f43e A |
242 | int |
243 | di_root_ramfile_buf(void *buf, size_t bufsz, char *devname, size_t devsz, dev_t *dev_p) | |
244 | { | |
245 | IOReturn res = 0; | |
cb323159 A |
246 | IOService *controller = NULL; |
247 | OSNumber *myResult = NULL; | |
248 | OSString *myDevName = NULL; | |
249 | OSNumber *myDevT = NULL; | |
250 | IOMemoryDescriptor *mem = NULL; | |
5ba3f43e A |
251 | |
252 | mem = IOMemoryDescriptor::withAddress(buf, bufsz, kIODirectionInOut); | |
253 | assert(mem); | |
254 | ||
255 | controller = di_load_controller(); | |
256 | if (controller) { | |
257 | /* attach the image */ | |
258 | controller->setProperty(kDIRootRamFileKey, mem); | |
259 | controller->release(); | |
260 | } else { | |
261 | res = kIOReturnNotFound; | |
262 | goto out; | |
263 | } | |
264 | ||
265 | myResult = OSDynamicCast(OSNumber, controller->getProperty(kDIRootImageResultKey)); | |
266 | res = kIOReturnError; | |
267 | if (myResult) { | |
268 | res = myResult->unsigned32BitValue(); | |
269 | } | |
270 | ||
271 | if (res) { | |
272 | IOLog("%s is 0x%08X/%d\n", kDIRootImageResultKey, res, res); | |
273 | goto out; | |
274 | } | |
275 | ||
276 | myDevT = OSDynamicCast(OSNumber, controller->getProperty(kDIRootImageDevTKey)); | |
0a7de745 | 277 | if (myDevT) { |
5ba3f43e | 278 | *dev_p = myDevT->unsigned32BitValue(); |
0a7de745 | 279 | } else { |
5ba3f43e A |
280 | IOLog("could not get %s\n", kDIRootImageDevTKey); |
281 | res = kIOReturnError; | |
282 | goto out; | |
283 | } | |
284 | ||
285 | myDevName = OSDynamicCast(OSString, controller->getProperty(kDIRootImageDevNameKey)); | |
286 | if (myDevName) { | |
287 | strlcpy(devname, myDevName->getCStringNoCopy(), devsz); | |
288 | } else { | |
289 | IOLog("could not get %s\n", kDIRootImageDevNameKey); | |
290 | res = kIOReturnError; | |
291 | goto out; | |
292 | } | |
293 | ||
294 | out: | |
295 | if (res) { | |
296 | OSSafeReleaseNULL(mem); | |
297 | } | |
298 | ||
299 | return res; | |
300 | } | |
301 | ||
0a7de745 A |
302 | void |
303 | di_root_ramfile( IORegistryEntry * entry ) | |
6d2010ae | 304 | { |
0a7de745 A |
305 | OSData * data; |
306 | IOMemoryDescriptor * mem; | |
307 | uint64_t dmgSize; | |
308 | uint64_t remain, length; | |
cb323159 | 309 | OSData * extentData = NULL; |
0a7de745 A |
310 | IOAddressRange * extentList; |
311 | uint64_t extentSize; | |
312 | uint32_t extentCount; | |
313 | ||
314 | do { | |
315 | data = OSDynamicCast(OSData, entry->getProperty("boot-ramdmg-size")); | |
316 | if (!data || (data->getLength() != sizeof(uint64_t))) { | |
317 | break; // bad disk image size | |
318 | } | |
319 | dmgSize = *(uint64_t *) data->getBytesNoCopy(); | |
320 | if (!dmgSize) { | |
321 | break; | |
322 | } | |
323 | ||
324 | data = OSDynamicCast(OSData, entry->getProperty("boot-ramdmg-extents")); | |
325 | if (!data || (data->getLength() == 0) || | |
326 | ((data->getLength() & (sizeof(IOAddressRange) - 1)) != 0)) { | |
327 | break; // bad extents | |
328 | } | |
329 | // make modifications to local copy | |
330 | extentData = OSData::withData(data); | |
331 | assert(extentData); | |
332 | ||
333 | extentList = (IOAddressRange *) extentData->getBytesNoCopy(); | |
334 | extentCount = extentData->getLength() / sizeof(IOAddressRange); | |
335 | extentSize = 0; | |
336 | remain = dmgSize; | |
337 | ||
338 | // truncate extent length to enclosing disk image | |
339 | for (uint32_t i = 0; i < extentCount; i++) { | |
340 | length = extentList[i].length; | |
341 | if (!length) { | |
342 | break; | |
343 | } | |
344 | ||
345 | extentSize += length; | |
346 | if (length >= remain) { | |
347 | extentList[i].length = remain; | |
348 | extentCount = i + 1; | |
349 | break; | |
350 | } | |
351 | remain -= length; | |
352 | } | |
353 | if (extentSize < dmgSize) { | |
354 | break; // not enough extent bytes for enclosing disk image | |
355 | } | |
356 | mem = IOMemoryDescriptor::withAddressRanges( | |
357 | extentList, extentCount, | |
358 | kIODirectionOut | kIOMemoryMapperNone, NULL); | |
359 | ||
360 | if (mem) { | |
361 | IOService * controller = di_load_controller(); | |
362 | if (controller) { | |
363 | controller->setProperty(kDIRootRamFileKey, mem); | |
364 | controller->release(); | |
365 | } | |
366 | mem->release(); | |
367 | } | |
368 | } while (false); | |
369 | ||
370 | if (extentData) { | |
371 | extentData->release(); | |
372 | } | |
6d2010ae | 373 | } |
9bccf70c | 374 | }; |