]> git.saurik.com Git - apple/xnu.git/blame - iokit/bsddev/IOKitBSDInit.cpp
xnu-1504.7.4.tar.gz
[apple/xnu.git] / iokit / bsddev / IOKitBSDInit.cpp
CommitLineData
1c79356b 1/*
b0d623f7 2 * Copyright (c) 1998-2008 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 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.
8f6c56a5 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.
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28#include <IOKit/IOBSD.h>
29#include <IOKit/IOLib.h>
30#include <IOKit/IOService.h>
d1ecb069 31#include <IOKit/IOCatalogue.h>
1c79356b
A
32#include <IOKit/IODeviceTreeSupport.h>
33#include <IOKit/IOKitKeys.h>
1c79356b
A
34#include <IOKit/IOPlatformExpert.h>
35
1c79356b
A
36extern "C" {
37
38#include <pexpert/pexpert.h>
39#include <kern/clock.h>
2d21ac55 40#include <uuid/uuid.h>
1c79356b
A
41
42// how long to wait for matching root device, secs
b0d623f7
A
43#if DEBUG
44#define ROOTDEVICETIMEOUT 120
45#else
46#define ROOTDEVICETIMEOUT 60
47#endif
1c79356b 48
55e303ae
A
49extern dev_t mdevadd(int devid, ppnum_t base, unsigned int size, int phys);
50extern dev_t mdevlookup(int devid);
4a3eedf9 51extern void mdevremoveall(void);
1c79356b
A
52
53kern_return_t
54IOKitBSDInit( void )
55{
1c79356b 56 IOService::publishResource("IOBSD");
b0d623f7 57
1c79356b
A
58 return( kIOReturnSuccess );
59}
60
d1ecb069
A
61void
62IOServicePublishResource( const char * property, boolean_t value )
63{
64 if ( value)
65 IOService::publishResource( property, kOSBooleanTrue );
66 else
67 IOService::getResourceService()->removeProperty( property );
68}
69
70boolean_t
71IOServiceWaitForMatchingResource( const char * property, uint64_t timeout )
72{
73 OSDictionary * dict = 0;
74 IOService * match = 0;
75 boolean_t found = false;
76
77 do {
78
79 dict = IOService::resourceMatching( property );
80 if( !dict)
81 continue;
82 match = IOService::waitForMatchingService( dict, timeout );
83 if ( match)
84 found = true;
85
86 } while( false );
87
88 if( dict)
89 dict->release();
90 if( match)
91 match->release();
92
93 return( found );
94}
95
96boolean_t
97IOCatalogueMatchingDriversPresent( const char * property )
98{
99 OSDictionary * dict = 0;
100 OSOrderedSet * set = 0;
101 SInt32 generationCount = 0;
102 boolean_t found = false;
103
104 do {
105
106 dict = OSDictionary::withCapacity(1);
107 if( !dict)
108 continue;
109 dict->setObject( property, kOSBooleanTrue );
110 set = gIOCatalogue->findDrivers( dict, &generationCount );
111 if ( set && (set->getCount() > 0))
112 found = true;
113
114 } while( false );
115
116 if( dict)
117 dict->release();
118 if( set)
119 set->release();
120
121 return( found );
122}
123
1c79356b
A
124OSDictionary * IOBSDNameMatching( const char * name )
125{
126 OSDictionary * dict;
127 const OSSymbol * str = 0;
128
129 do {
130
131 dict = IOService::serviceMatching( gIOServiceKey );
132 if( !dict)
133 continue;
134 str = OSSymbol::withCString( name );
135 if( !str)
136 continue;
137 dict->setObject( kIOBSDNameKey, (OSObject *) str );
138 str->release();
139
140 return( dict );
141
142 } while( false );
143
144 if( dict)
145 dict->release();
146 if( str)
147 str->release();
148
149 return( 0 );
150}
151
91447636
A
152OSDictionary * IOUUIDMatching( void )
153{
154 return IOService::resourceMatching( "boot-uuid-media" );
155}
156
157
55e303ae 158OSDictionary * IOCDMatching( void )
0b4e3aa0
A
159{
160 OSDictionary * dict;
161 const OSSymbol * str;
55e303ae
A
162
163 dict = IOService::serviceMatching( "IOMedia" );
164 if( dict == 0 ) {
165 IOLog("Unable to find IOMedia\n");
166 return 0;
167 }
168
169 str = OSSymbol::withCString( "CD_ROM_Mode_1" );
170 if( str == 0 ) {
171 dict->release();
172 return 0;
173 }
174
175 dict->setObject( "Content Hint", (OSObject *)str );
176 str->release();
177 return( dict );
0b4e3aa0
A
178}
179
1c79356b
A
180OSDictionary * IONetworkMatching( const char * path,
181 char * buf, int maxLen )
182{
183 OSDictionary * matching = 0;
184 OSDictionary * dict;
185 OSString * str;
186 char * comp;
187 const char * skip;
188 int len;
189
190 do {
191
192 len = strlen( kIODeviceTreePlane ":" );
193 maxLen -= len;
cf7d32b8 194 if( maxLen <= 0)
1c79356b
A
195 continue;
196
cf7d32b8 197 strlcpy( buf, kIODeviceTreePlane ":", len + 1 );
1c79356b
A
198 comp = buf + len;
199
200 // remove parameters following ':' from the path
201 skip = strchr( path, ':');
202 if( !skip)
203 continue;
204
205 len = skip - path;
206 maxLen -= len;
cf7d32b8 207 if( maxLen <= 0)
1c79356b 208 continue;
cf7d32b8 209 strlcpy( comp, path, len + 1 );
1c79356b
A
210
211 matching = IOService::serviceMatching( "IONetworkInterface" );
212 if( !matching)
213 continue;
214 dict = IOService::addLocation( matching );
215 if( !dict)
216 continue;
217
218 str = OSString::withCString( buf );
219 if( !str)
220 continue;
221 dict->setObject( kIOPathMatchKey, str );
222 str->release();
223
224 return( matching );
225
226 } while( false );
227
228 if( matching)
229 matching->release();
230
231 return( 0 );
232}
233
234OSDictionary * IONetworkNamePrefixMatching( const char * prefix )
235{
236 OSDictionary * matching;
237 OSDictionary * propDict = 0;
238 const OSSymbol * str = 0;
2d21ac55
A
239 char networkType[128];
240
1c79356b
A
241 do {
242 matching = IOService::serviceMatching( "IONetworkInterface" );
243 if ( matching == 0 )
244 continue;
245
246 propDict = OSDictionary::withCapacity(1);
247 if ( propDict == 0 )
248 continue;
249
250 str = OSSymbol::withCString( prefix );
251 if ( str == 0 )
252 continue;
253
0b4e3aa0 254 propDict->setObject( "IOInterfaceNamePrefix", (OSObject *) str );
1c79356b
A
255 str->release();
256 str = 0;
257
2d21ac55
A
258 // see if we're contrained to netroot off of specific network type
259 if(PE_parse_boot_argn( "network-type", networkType, 128 ))
260 {
261 str = OSSymbol::withCString( networkType );
262 if(str)
263 {
264 propDict->setObject( "IONetworkRootType", str);
265 str->release();
266 str = 0;
267 }
268 }
269
1c79356b
A
270 if ( matching->setObject( gIOPropertyMatchKey,
271 (OSObject *) propDict ) != true )
272 continue;
273
274 propDict->release();
275 propDict = 0;
276
277 return( matching );
278
279 } while ( false );
280
281 if ( matching ) matching->release();
282 if ( propDict ) propDict->release();
283 if ( str ) str->release();
284
285 return( 0 );
286}
287
0b4e3aa0 288static bool IORegisterNetworkInterface( IOService * netif )
1c79356b 289{
0b4e3aa0
A
290 // A network interface is typically named and registered
291 // with BSD after receiving a request from a user space
292 // "namer". However, for cases when the system needs to
293 // root from the network, this registration task must be
294 // done inside the kernel and completed before the root
295 // device is handed to BSD.
296
297 IOService * stack;
298 OSNumber * zero = 0;
299 OSString * path = 0;
300 OSDictionary * dict = 0;
301 char * pathBuf = 0;
302 int len;
303 enum { kMaxPathLen = 512 };
1c79356b 304
0b4e3aa0
A
305 do {
306 stack = IOService::waitForService(
307 IOService::serviceMatching("IONetworkStack") );
308 if ( stack == 0 ) break;
1c79356b 309
0b4e3aa0
A
310 dict = OSDictionary::withCapacity(3);
311 if ( dict == 0 ) break;
1c79356b 312
0b4e3aa0
A
313 zero = OSNumber::withNumber((UInt64) 0, 32);
314 if ( zero == 0 ) break;
1c79356b 315
0b4e3aa0
A
316 pathBuf = (char *) IOMalloc( kMaxPathLen );
317 if ( pathBuf == 0 ) break;
318
319 len = kMaxPathLen;
320 if ( netif->getPath( pathBuf, &len, gIOServicePlane )
321 == false ) break;
322
323 path = OSString::withCStringNoCopy( pathBuf );
324 if ( path == 0 ) break;
325
326 dict->setObject( "IOInterfaceUnit", zero );
327 dict->setObject( kIOPathMatchKey, path );
328
329 stack->setProperties( dict );
330 }
331 while ( false );
332
333 if ( zero ) zero->release();
334 if ( path ) path->release();
335 if ( dict ) dict->release();
336 if ( pathBuf ) IOFree(pathBuf, kMaxPathLen);
337
338 return ( netif->getProperty( kIOBSDNameKey ) != 0 );
1c79356b
A
339}
340
341OSDictionary * IODiskMatching( const char * path, char * buf, int maxLen )
342{
343 const char * look;
344 const char * alias;
345 char * comp;
346 long unit = -1;
347 long partition = -1;
55e303ae 348 long lun = -1;
1c79356b 349 char c;
cf7d32b8 350 int len;
1c79356b
A
351
352 // scan the tail of the path for "@unit:partition"
353 do {
354 // Have to get the full path to the controller - an alias may
355 // tell us next to nothing, like "hd:8"
356 alias = IORegistryEntry::dealiasPath( &path, gIODTPlane );
55e303ae 357
1c79356b
A
358 look = path + strlen( path);
359 c = ':';
360 while( look != path) {
361 if( *(--look) == c) {
362 if( c == ':') {
363 partition = strtol( look + 1, 0, 0 );
364 c = '@';
365 } else if( c == '@') {
91447636
A
366 unit = strtol( look + 1, &comp, 16 );
367
368 if( *comp == ',') {
369 lun = strtol( comp + 1, 0, 16 );
55e303ae
A
370 }
371
1c79356b
A
372 c = '/';
373 } else if( c == '/') {
374 c = 0;
375 break;
376 }
377 }
378
379 if( alias && (look == path)) {
380 path = alias;
381 look = path + strlen( path);
382 alias = 0;
383 }
384 }
385 if( c || unit == -1 || partition == -1)
386 continue;
55e303ae 387
cf7d32b8
A
388 len = strlen( "{" kIOPathMatchKey "='" kIODeviceTreePlane ":" );
389 maxLen -= len;
390 if( maxLen <= 0)
391 continue;
1c79356b 392
cf7d32b8
A
393 snprintf( buf, len + 1, "{" kIOPathMatchKey "='" kIODeviceTreePlane ":" );
394 comp = buf + len;
395
396 if( alias) {
397 len = strlen( alias );
398 maxLen -= len;
399 if( maxLen <= 0)
400 continue;
401
402 strlcpy( comp, alias, len + 1 );
403 comp += len;
404 }
405
406 if ( (look - path)) {
407 len = (look - path);
408 maxLen -= len;
409 if( maxLen <= 0)
410 continue;
411
412 strlcpy( comp, path, len + 1 );
413 comp += len;
414 }
55e303ae 415
cf7d32b8
A
416 if ( lun != -1 )
417 {
418 len = strlen( "/@hhhhhhhh,hhhhhhhh:dddddddddd';}" );
419 maxLen -= len;
420 if( maxLen <= 0)
421 continue;
422
423 snprintf( comp, len + 1, "/@%lx,%lx:%ld';}", unit, lun, partition );
424 }
425 else
426 {
427 len = strlen( "/@hhhhhhhh:dddddddddd';}" );
428 maxLen -= len;
429 if( maxLen <= 0)
430 continue;
431
432 snprintf( comp, len + 1, "/@%lx:%ld';}", unit, partition );
433 }
55e303ae 434
1c79356b
A
435 return( OSDynamicCast(OSDictionary, OSUnserialize( buf, 0 )) );
436
437 } while( false );
438
439 return( 0 );
440}
441
442OSDictionary * IOOFPathMatching( const char * path, char * buf, int maxLen )
443{
91447636
A
444 OSDictionary * matching;
445 OSString * str;
446 char * comp;
447 int len;
448
1c79356b
A
449 /* need to look up path, get device type,
450 call matching help based on device type */
451
91447636
A
452 matching = IODiskMatching( path, buf, maxLen );
453 if( matching)
454 return( matching );
455
456 do {
457
458 len = strlen( kIODeviceTreePlane ":" );
459 maxLen -= len;
cf7d32b8 460 if( maxLen <= 0)
91447636
A
461 continue;
462
cf7d32b8 463 strlcpy( buf, kIODeviceTreePlane ":", len + 1 );
91447636
A
464 comp = buf + len;
465
466 len = strlen( path );
467 maxLen -= len;
cf7d32b8 468 if( maxLen <= 0)
91447636 469 continue;
cf7d32b8 470 strlcpy( comp, path, len + 1 );
91447636
A
471
472 matching = OSDictionary::withCapacity( 1 );
473 if( !matching)
474 continue;
475
476 str = OSString::withCString( buf );
477 if( !str)
478 continue;
479 matching->setObject( kIOPathMatchKey, str );
480 str->release();
481
482 return( matching );
483
484 } while( false );
1c79356b 485
91447636
A
486 if( matching)
487 matching->release();
488
489 return( 0 );
1c79356b
A
490}
491
55e303ae
A
492IOService * IOFindMatchingChild( IOService * service )
493{
494 // find a matching child service
495 IOService * child = 0;
496 OSIterator * iter = service->getClientIterator();
497 if ( iter ) {
498 while( ( child = (IOService *) iter->getNextObject() ) ) {
499 OSDictionary * dict = OSDictionary::withCapacity( 1 );
500 if( dict == 0 ) {
501 iter->release();
502 return 0;
503 }
504 const OSSymbol * str = OSSymbol::withCString( "Apple_HFS" );
505 if( str == 0 ) {
506 dict->release();
507 iter->release();
508 return 0;
509 }
510 dict->setObject( "Content", (OSObject *)str );
511 str->release();
512 if ( child->compareProperty( dict, "Content" ) ) {
513 dict->release();
514 break;
515 }
516 dict->release();
517 IOService * subchild = IOFindMatchingChild( child );
518 if ( subchild ) {
519 child = subchild;
520 break;
521 }
522 }
523 iter->release();
524 }
525 return child;
526}
527
528static int didRam = 0;
529
cf7d32b8 530kern_return_t IOFindBSDRoot( char * rootName, unsigned int rootNameSize,
1c79356b
A
531 dev_t * root, u_int32_t * oflags )
532{
533 mach_timespec_t t;
534 IOService * service;
535 IORegistryEntry * regEntry;
536 OSDictionary * matching = 0;
537 OSString * iostr;
538 OSNumber * off;
539 OSData * data = 0;
55e303ae 540 UInt32 *ramdParms = 0;
1c79356b
A
541
542 UInt32 flags = 0;
2d21ac55 543 int mnr, mjr;
55e303ae 544 bool findHFSChild = false;
91447636 545 char * mediaProperty = 0;
1c79356b
A
546 char * rdBootVar;
547 enum { kMaxPathBuf = 512, kMaxBootVar = 128 };
548 char * str;
549 const char * look = 0;
550 int len;
551 bool forceNet = false;
0b4e3aa0 552 bool debugInfoPrintedOnce = false;
91447636 553 const char * uuidStr = NULL;
1c79356b
A
554
555 static int mountAttempts = 0;
55e303ae 556
91447636 557 int xchar, dchar;
55e303ae 558
1c79356b
A
559
560 if( mountAttempts++)
561 IOSleep( 5 * 1000 );
562
563 str = (char *) IOMalloc( kMaxPathBuf + kMaxBootVar );
564 if( !str)
565 return( kIOReturnNoMemory );
566 rdBootVar = str + kMaxPathBuf;
567
2d21ac55
A
568 if (!PE_parse_boot_argn("rd", rdBootVar, kMaxBootVar )
569 && !PE_parse_boot_argn("rootdev", rdBootVar, kMaxBootVar ))
1c79356b
A
570 rdBootVar[0] = 0;
571
572 do {
91447636 573 if( (regEntry = IORegistryEntry::fromPath( "/chosen", gIODTPlane ))) {
0c530ab8
A
574 data = OSDynamicCast(OSData, regEntry->getProperty( "root-matching" ));
575 if (data) {
576 matching = OSDynamicCast(OSDictionary, OSUnserializeXML((char *)data->getBytesNoCopy()));
577 if (matching) {
578 continue;
579 }
580 }
581
91447636
A
582 data = (OSData *) regEntry->getProperty( "boot-uuid" );
583 if( data) {
584 uuidStr = (const char*)data->getBytesNoCopy();
585 OSString *uuidString = OSString::withCString( uuidStr );
586
587 // match the boot-args boot-uuid processing below
588 if( uuidString) {
589 IOLog("rooting via boot-uuid from /chosen: %s\n", uuidStr);
590 IOService::publishResource( "boot-uuid", uuidString );
591 uuidString->release();
592 matching = IOUUIDMatching();
593 mediaProperty = "boot-uuid-media";
594 regEntry->release();
595 continue;
596 } else {
597 uuidStr = NULL;
55e303ae 598 }
91447636
A
599 }
600
601 // else try for an OF Path
602 data = (OSData *) regEntry->getProperty( "rootpath" );
603 regEntry->release();
604 if( data) continue;
605 }
1c79356b 606 if( (regEntry = IORegistryEntry::fromPath( "/options", gIODTPlane ))) {
91447636
A
607 data = (OSData *) regEntry->getProperty( "boot-file" );
608 regEntry->release();
609 if( data) continue;
610 }
1c79356b
A
611 } while( false );
612
91447636 613 if( data && !uuidStr)
1c79356b
A
614 look = (const char *) data->getBytesNoCopy();
615
616 if( rdBootVar[0] == '*') {
617 look = rdBootVar + 1;
55e303ae 618 forceNet = false;
1c79356b
A
619 } else {
620 if( (regEntry = IORegistryEntry::fromPath( "/", gIODTPlane ))) {
621 forceNet = (0 != regEntry->getProperty( "net-boot" ));
55e303ae
A
622 regEntry->release();
623 }
de355530 624 }
d7e50217 625
55e303ae
A
626
627
628//
629// See if we have a RAMDisk property in /chosen/memory-map. If so, make it into a device.
630// It will become /dev/mdx, where x is 0-f.
631//
632
633 if(!didRam) { /* Have we already build this ram disk? */
634 didRam = 1; /* Remember we did this */
635 if((regEntry = IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane ))) { /* Find the map node */
636 data = (OSData *)regEntry->getProperty("RAMDisk"); /* Find the ram disk, if there */
637 if(data) { /* We found one */
638
639 ramdParms = (UInt32 *)data->getBytesNoCopy(); /* Point to the ram disk base and size */
b0d623f7 640 (void)mdevadd(-1, ml_static_ptovirt(ramdParms[0]) >> 12, ramdParms[1] >> 12, 0); /* Initialize it and pass back the device number */
55e303ae
A
641 }
642 regEntry->release(); /* Toss the entry */
643 }
644 }
645
646//
647// Now check if we are trying to root on a memory device
648//
649
650 if((rdBootVar[0] == 'm') && (rdBootVar[1] == 'd') && (rdBootVar[3] == 0)) {
651 dchar = xchar = rdBootVar[2]; /* Get the actual device */
652 if((xchar >= '0') && (xchar <= '9')) xchar = xchar - '0'; /* If digit, convert */
653 else {
654 xchar = xchar & ~' '; /* Fold to upper case */
655 if((xchar >= 'A') && (xchar <= 'F')) { /* Is this a valid digit? */
656 xchar = (xchar & 0xF) + 9; /* Convert the hex digit */
657 dchar = dchar | ' '; /* Fold to lower case */
658 }
659 else xchar = -1; /* Show bogus */
660 }
661 if(xchar >= 0) { /* Do we have a valid memory device name? */
662 *root = mdevlookup(xchar); /* Find the device number */
663 if(*root >= 0) { /* Did we find one? */
664
665 rootName[0] = 'm'; /* Build root name */
666 rootName[1] = 'd'; /* Build root name */
667 rootName[2] = dchar; /* Build root name */
668 rootName[3] = 0; /* Build root name */
669 IOLog("BSD root: %s, major %d, minor %d\n", rootName, major(*root), minor(*root));
670 *oflags = 0; /* Show that this is not network */
671 goto iofrootx; /* Join common exit... */
672 }
673 panic("IOFindBSDRoot: specified root memory device, %s, has not been configured\n", rdBootVar); /* Not there */
674 }
675 }
676
1c79356b
A
677 if( look) {
678 // from OpenFirmware path
679 IOLog("From path: \"%s\", ", look);
680
0c530ab8
A
681 if (!matching) {
682 if( forceNet || (0 == strncmp( look, "enet", strlen( "enet" ))) ) {
683 matching = IONetworkMatching( look, str, kMaxPathBuf );
684 } else {
685 matching = IODiskMatching( look, str, kMaxPathBuf );
686 }
0b4e3aa0 687 }
1c79356b 688 }
55e303ae
A
689
690 if( (!matching) && rdBootVar[0] ) {
1c79356b
A
691 // by BSD name
692 look = rdBootVar;
693 if( look[0] == '*')
694 look++;
695
0b4e3aa0
A
696 if ( strncmp( look, "en", strlen( "en" )) == 0 ) {
697 matching = IONetworkNamePrefixMatching( "en" );
55e303ae
A
698 } else if ( strncmp( look, "cdrom", strlen( "cdrom" )) == 0 ) {
699 matching = IOCDMatching();
700 findHFSChild = true;
91447636
A
701 } else if ( strncmp( look, "uuid", strlen( "uuid" )) == 0 ) {
702 char *uuid;
703 OSString *uuidString;
704
705 uuid = (char *)IOMalloc( kMaxBootVar );
706
707 if ( uuid ) {
2d21ac55 708 if (!PE_parse_boot_argn( "boot-uuid", uuid, kMaxBootVar )) {
91447636
A
709 panic( "rd=uuid but no boot-uuid=<value> specified" );
710 }
711 uuidString = OSString::withCString( uuid );
712 if ( uuidString ) {
713 IOService::publishResource( "boot-uuid", uuidString );
714 uuidString->release();
715 IOLog( "\nWaiting for boot volume with UUID %s\n", uuid );
716 matching = IOUUIDMatching();
717 mediaProperty = "boot-uuid-media";
718 }
719 IOFree( uuid, kMaxBootVar );
720 }
0b4e3aa0
A
721 } else {
722 matching = IOBSDNameMatching( look );
723 }
1c79356b
A
724 }
725
726 if( !matching) {
2d21ac55
A
727 OSString * astring;
728 // Match any HFS media
729
1c79356b 730 matching = IOService::serviceMatching( "IOMedia" );
55e303ae 731 astring = OSString::withCStringNoCopy("Apple_HFS");
1c79356b 732 if ( astring ) {
0b4e3aa0 733 matching->setObject("Content", astring);
1c79356b
A
734 astring->release();
735 }
736 }
737
738 if( true && matching) {
739 OSSerialize * s = OSSerialize::withCapacity( 5 );
740
741 if( matching->serialize( s )) {
742 IOLog( "Waiting on %s\n", s->text() );
743 s->release();
744 }
745 }
746
1c79356b
A
747 do {
748 t.tv_sec = ROOTDEVICETIMEOUT;
749 t.tv_nsec = 0;
750 matching->retain();
751 service = IOService::waitForService( matching, &t );
752 if( (!service) || (mountAttempts == 10)) {
753 PE_display_icon( 0, "noroot");
754 IOLog( "Still waiting for root device\n" );
0b4e3aa0
A
755
756 if( !debugInfoPrintedOnce) {
757 debugInfoPrintedOnce = true;
758 if( gIOKitDebug & kIOLogDTree) {
759 IOLog("\nDT plane:\n");
760 IOPrintPlane( gIODTPlane );
761 }
762 if( gIOKitDebug & kIOLogServiceTree) {
763 IOLog("\nService plane:\n");
764 IOPrintPlane( gIOServicePlane );
765 }
766 if( gIOKitDebug & kIOLogMemory)
767 IOPrintMemory();
768 }
1c79356b
A
769 }
770 } while( !service);
771 matching->release();
772
55e303ae
A
773 if ( service && findHFSChild ) {
774 bool waiting = true;
b0d623f7
A
775 uint64_t timeoutNS;
776
55e303ae
A
777 // wait for children services to finish registering
778 while ( waiting ) {
b0d623f7
A
779 timeoutNS = ROOTDEVICETIMEOUT;
780 timeoutNS *= kSecondScale;
781
782 if ( (service->waitQuiet(timeoutNS) ) == kIOReturnSuccess) {
55e303ae
A
783 waiting = false;
784 } else {
785 IOLog( "Waiting for child registration\n" );
786 }
787 }
788 // look for a subservice with an Apple_HFS child
789 IOService * subservice = IOFindMatchingChild( service );
790 if ( subservice ) service = subservice;
91447636 791 } else if ( service && mediaProperty ) {
0c530ab8 792 service = (IOService *)service->getProperty(mediaProperty);
55e303ae
A
793 }
794
2d21ac55
A
795 mjr = 0;
796 mnr = 0;
1c79356b
A
797
798 // If the IOService we matched to is a subclass of IONetworkInterface,
799 // then make sure it has been registered with BSD and has a BSD name
800 // assigned.
801
802 if ( service
803 && service->metaCast( "IONetworkInterface" )
0b4e3aa0 804 && !IORegisterNetworkInterface( service ) )
1c79356b
A
805 {
806 service = 0;
807 }
1c79356b
A
808
809 if( service) {
810
811 len = kMaxPathBuf;
812 service->getPath( str, &len, gIOServicePlane );
813 IOLog( "Got boot device = %s\n", str );
814
815 iostr = (OSString *) service->getProperty( kIOBSDNameKey );
816 if( iostr)
cf7d32b8 817 strlcpy( rootName, iostr->getCStringNoCopy(), rootNameSize );
1c79356b
A
818 off = (OSNumber *) service->getProperty( kIOBSDMajorKey );
819 if( off)
2d21ac55 820 mjr = off->unsigned32BitValue();
1c79356b
A
821 off = (OSNumber *) service->getProperty( kIOBSDMinorKey );
822 if( off)
2d21ac55 823 mnr = off->unsigned32BitValue();
1c79356b
A
824
825 if( service->metaCast( "IONetworkInterface" ))
826 flags |= 1;
827
828 } else {
829
830 IOLog( "Wait for root failed\n" );
cf7d32b8 831 strlcpy( rootName, "en0", rootNameSize );
1c79356b
A
832 flags |= 1;
833 }
834
835 IOLog( "BSD root: %s", rootName );
2d21ac55
A
836 if( mjr)
837 IOLog(", major %d, minor %d\n", mjr, mnr );
1c79356b
A
838 else
839 IOLog("\n");
840
2d21ac55 841 *root = makedev( mjr, mnr );
1c79356b
A
842 *oflags = flags;
843
844 IOFree( str, kMaxPathBuf + kMaxBootVar );
845
55e303ae 846iofrootx:
0b4e3aa0 847 if( (gIOKitDebug & (kIOLogDTree | kIOLogServiceTree | kIOLogMemory)) && !debugInfoPrintedOnce) {
1c79356b 848
0b4e3aa0 849 IOService::getPlatform()->waitQuiet();
1c79356b
A
850 if( gIOKitDebug & kIOLogDTree) {
851 IOLog("\nDT plane:\n");
852 IOPrintPlane( gIODTPlane );
853 }
854 if( gIOKitDebug & kIOLogServiceTree) {
855 IOLog("\nService plane:\n");
856 IOPrintPlane( gIOServicePlane );
857 }
858 if( gIOKitDebug & kIOLogMemory)
859 IOPrintMemory();
860 }
861
862 return( kIOReturnSuccess );
863}
864
2d21ac55
A
865void IOSecureBSDRoot(const char * rootName)
866{
867#if CONFIG_EMBEDDED
4a3eedf9 868 IOReturn result;
2d21ac55 869 IOPlatformExpert *pe;
4a3eedf9 870 const OSSymbol *functionName = OSSymbol::withCStringNoCopy("SecureRootName");
2d21ac55
A
871
872 while ((pe = IOService::getPlatform()) == 0) IOSleep(1 * 1000);
873
4a3eedf9
A
874 // Returns kIOReturnNotPrivileged is the root device is not secure.
875 // Returns kIOReturnUnsupported if "SecureRootName" is not implemented.
876 result = pe->callPlatformFunction(functionName, false, (void *)rootName, (void *)0, (void *)0, (void *)0);
2d21ac55
A
877
878 functionName->release();
4a3eedf9
A
879
880 if (result == kIOReturnNotPrivileged) mdevremoveall();
2d21ac55
A
881#endif
882}
883
9bccf70c
A
884void *
885IOBSDRegistryEntryForDeviceTree(char * path)
886{
887 return (IORegistryEntry::fromPath(path, gIODTPlane));
888}
889
890void
891IOBSDRegistryEntryRelease(void * entry)
892{
893 IORegistryEntry * regEntry = (IORegistryEntry *)entry;
894
895 if (regEntry)
896 regEntry->release();
897 return;
898}
899
900const void *
901IOBSDRegistryEntryGetData(void * entry, char * property_name,
902 int * packet_length)
903{
904 OSData * data;
905 IORegistryEntry * regEntry = (IORegistryEntry *)entry;
906
907 data = (OSData *) regEntry->getProperty(property_name);
908 if (data) {
909 *packet_length = data->getLength();
910 return (data->getBytesNoCopy());
911 }
912 return (NULL);
913}
914
2d21ac55
A
915kern_return_t IOBSDGetPlatformUUID( uuid_t uuid, mach_timespec_t timeout )
916{
917 IOService * resources;
918 OSString * string;
919
920 resources = IOService::waitForService( IOService::resourceMatching( kIOPlatformUUIDKey ), &timeout );
921 if ( resources == 0 ) return KERN_OPERATION_TIMED_OUT;
922
923 string = ( OSString * ) IOService::getPlatform( )->getProvider( )->getProperty( kIOPlatformUUIDKey );
924 if ( string == 0 ) return KERN_NOT_SUPPORTED;
925
926 uuid_parse( string->getCStringNoCopy( ), uuid );
927
928 return KERN_SUCCESS;
929}
930
b0d623f7
A
931kern_return_t IOBSDGetPlatformSerialNumber( char *serial_number_str, u_int32_t len )
932{
933 OSDictionary * platform_dict;
934 IOService *platform;
935 OSString * string;
936
937 if (len < 1) {
938 return 0;
939 }
940 serial_number_str[0] = '\0';
941
942 platform_dict = IOService::serviceMatching( "IOPlatformExpertDevice" );
943 if (platform_dict == NULL) {
944 return KERN_NOT_SUPPORTED;
945 }
946
947 platform = IOService::waitForService( platform_dict );
948 if (platform) {
949 string = ( OSString * ) platform->getProperty( kIOPlatformSerialNumberKey );
950 if ( string == 0 ) {
951 return KERN_NOT_SUPPORTED;
952 } else {
953 strlcpy( serial_number_str, string->getCStringNoCopy( ), len );
954 }
955 }
956
957 return KERN_SUCCESS;
958}
959
960dev_t IOBSDGetMediaWithUUID( const char *uuid_cstring, char *bsd_name, int bsd_name_len, int timeout)
961{
962 dev_t dev = 0;
963 OSDictionary *dictionary;
964 OSString *uuid_string;
965
966 if (bsd_name_len < 1) {
967 return 0;
968 }
969 bsd_name[0] = '\0';
970
971 dictionary = IOService::serviceMatching( "IOMedia" );
972 if( dictionary ) {
973 uuid_string = OSString::withCString( uuid_cstring );
974 if( uuid_string ) {
975 IOService *service;
976 mach_timespec_t tv = { timeout, 0 }; // wait up to "timeout" seconds for the device
977
978 dictionary->setObject( "UUID", uuid_string );
979 dictionary->retain();
980 service = IOService::waitForService( dictionary, &tv );
981 if( service ) {
982 OSNumber *dev_major = (OSNumber *) service->getProperty( kIOBSDMajorKey );
983 OSNumber *dev_minor = (OSNumber *) service->getProperty( kIOBSDMinorKey );
984 OSString *iostr = (OSString *) service->getProperty( kIOBSDNameKey );
985
986 if( iostr)
987 strlcpy( bsd_name, iostr->getCStringNoCopy(), bsd_name_len );
988
989 if ( dev_major && dev_minor )
990 dev = makedev( dev_major->unsigned32BitValue(), dev_minor->unsigned32BitValue() );
991 }
992 uuid_string->release();
993 }
994 dictionary->release();
995 }
996
997 return dev;
998}
999
1000
1001void IOBSDIterateMediaWithContent(const char *content_uuid_cstring, int (*func)(const char *bsd_dev_name, const char *uuid_str, void *arg), void *arg)
1002{
1003 OSDictionary *dictionary;
1004 OSString *content_uuid_string;
1005
1006 dictionary = IOService::serviceMatching( "IOMedia" );
1007 if( dictionary ) {
1008 content_uuid_string = OSString::withCString( content_uuid_cstring );
1009 if( content_uuid_string ) {
1010 IOService *service;
1011 OSIterator *iter;
1012
1013 dictionary->setObject( "Content", content_uuid_string );
1014 dictionary->retain();
1015
1016 iter = IOService::getMatchingServices(dictionary);
1017 while (iter && (service = (IOService *)iter->getNextObject())) {
1018 if( service ) {
1019 OSString *iostr = (OSString *) service->getProperty( kIOBSDNameKey );
1020 OSString *uuidstr = (OSString *) service->getProperty( "UUID" );
1021 const char *uuid;
1022
1023 if( iostr) {
1024 if (uuidstr) {
1025 uuid = uuidstr->getCStringNoCopy();
1026 } else {
1027 uuid = "00000000-0000-0000-0000-000000000000";
1028 }
1029
1030 // call the callback
1031 if (func && func(iostr->getCStringNoCopy(), uuid, arg) == 0) {
1032 break;
1033 }
1034 }
1035 }
1036 }
1037 if (iter)
1038 iter->release();
1039
1040 content_uuid_string->release();
1041 }
1042 dictionary->release();
1043 }
1044}
1045
e2fac8b1
A
1046
1047int IOBSDIsMediaEjectable( const char *cdev_name )
1048{
1049 int ret = 0;
1050 OSDictionary *dictionary;
1051 OSString *dev_name;
1052
1053 if (strncmp(cdev_name, "/dev/", 5) == 0) {
1054 cdev_name += 5;
1055 }
1056
1057 dictionary = IOService::serviceMatching( "IOMedia" );
1058 if( dictionary ) {
1059 dev_name = OSString::withCString( cdev_name );
1060 if( dev_name ) {
1061 IOService *service;
1062 mach_timespec_t tv = { 5, 0 }; // wait up to "timeout" seconds for the device
1063
1064 dictionary->setObject( kIOBSDNameKey, dev_name );
1065 dictionary->retain();
1066 service = IOService::waitForService( dictionary, &tv );
1067 if( service ) {
1068 OSBoolean *ejectable = (OSBoolean *) service->getProperty( "Ejectable" );
1069
1070 if( ejectable ) {
1071 ret = (int)ejectable->getValue();
1072 }
1073
1074 }
1075 dev_name->release();
1076 }
1077 dictionary->release();
1078 }
1079
1080 return ret;
1081}
1082
1c79356b 1083} /* extern "C" */