]>
git.saurik.com Git - apple/xnu.git/blob - iokit/bsddev/IOKitBSDInit.cpp
e7ec120be3f9340b724e623c168d0e7721c30e12
2 * Copyright (c) 1998-2000 Apple Computer, 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@
28 #include <IOKit/IOBSD.h>
29 #include <IOKit/IOLib.h>
30 #include <IOKit/IOService.h>
31 #include <IOKit/IODeviceTreeSupport.h>
32 #include <IOKit/IOKitKeys.h>
33 #include <IOKit/IOPlatformExpert.h>
37 #include <pexpert/pexpert.h>
38 #include <kern/clock.h>
40 // how long to wait for matching root device, secs
41 #define ROOTDEVICETIMEOUT 60
43 extern dev_t
mdevadd(int devid
, ppnum_t base
, unsigned int size
, int phys
);
44 extern dev_t
mdevlookup(int devid
);
49 IOService::publishResource("IOBSD");
51 return( kIOReturnSuccess
);
54 OSDictionary
* IOBSDNameMatching( const char * name
)
57 const OSSymbol
* str
= 0;
61 dict
= IOService::serviceMatching( gIOServiceKey
);
64 str
= OSSymbol::withCString( name
);
67 dict
->setObject( kIOBSDNameKey
, (OSObject
*) str
);
82 OSDictionary
* IOUUIDMatching( void )
84 return IOService::resourceMatching( "boot-uuid-media" );
88 OSDictionary
* IOCDMatching( void )
93 dict
= IOService::serviceMatching( "IOMedia" );
95 IOLog("Unable to find IOMedia\n");
99 str
= OSSymbol::withCString( "CD_ROM_Mode_1" );
105 dict
->setObject( "Content Hint", (OSObject
*)str
);
110 OSDictionary
* IONetworkMatching( const char * path
,
111 char * buf
, int maxLen
)
113 OSDictionary
* matching
= 0;
122 len
= strlen( kIODeviceTreePlane
":" );
127 strcpy( buf
, kIODeviceTreePlane
":" );
130 // remove parameters following ':' from the path
131 skip
= strchr( path
, ':');
139 strncpy( comp
, path
, len
);
142 matching
= IOService::serviceMatching( "IONetworkInterface" );
145 dict
= IOService::addLocation( matching
);
149 str
= OSString::withCString( buf
);
152 dict
->setObject( kIOPathMatchKey
, str
);
165 OSDictionary
* IONetworkNamePrefixMatching( const char * prefix
)
167 OSDictionary
* matching
;
168 OSDictionary
* propDict
= 0;
169 const OSSymbol
* str
= 0;
172 matching
= IOService::serviceMatching( "IONetworkInterface" );
176 propDict
= OSDictionary::withCapacity(1);
180 str
= OSSymbol::withCString( prefix
);
184 propDict
->setObject( "IOInterfaceNamePrefix", (OSObject
*) str
);
188 if ( matching
->setObject( gIOPropertyMatchKey
,
189 (OSObject
*) propDict
) != true )
199 if ( matching
) matching
->release();
200 if ( propDict
) propDict
->release();
201 if ( str
) str
->release();
206 static bool IORegisterNetworkInterface( IOService
* netif
)
208 // A network interface is typically named and registered
209 // with BSD after receiving a request from a user space
210 // "namer". However, for cases when the system needs to
211 // root from the network, this registration task must be
212 // done inside the kernel and completed before the root
213 // device is handed to BSD.
218 OSDictionary
* dict
= 0;
221 enum { kMaxPathLen
= 512 };
224 stack
= IOService::waitForService(
225 IOService::serviceMatching("IONetworkStack") );
226 if ( stack
== 0 ) break;
228 dict
= OSDictionary::withCapacity(3);
229 if ( dict
== 0 ) break;
231 zero
= OSNumber::withNumber((UInt64
) 0, 32);
232 if ( zero
== 0 ) break;
234 pathBuf
= (char *) IOMalloc( kMaxPathLen
);
235 if ( pathBuf
== 0 ) break;
238 if ( netif
->getPath( pathBuf
, &len
, gIOServicePlane
)
241 path
= OSString::withCStringNoCopy( pathBuf
);
242 if ( path
== 0 ) break;
244 dict
->setObject( "IOInterfaceUnit", zero
);
245 dict
->setObject( kIOPathMatchKey
, path
);
247 stack
->setProperties( dict
);
251 if ( zero
) zero
->release();
252 if ( path
) path
->release();
253 if ( dict
) dict
->release();
254 if ( pathBuf
) IOFree(pathBuf
, kMaxPathLen
);
256 return ( netif
->getProperty( kIOBSDNameKey
) != 0 );
259 OSDictionary
* IODiskMatching( const char * path
, char * buf
, int maxLen
)
269 // scan the tail of the path for "@unit:partition"
271 // Have to get the full path to the controller - an alias may
272 // tell us next to nothing, like "hd:8"
273 alias
= IORegistryEntry::dealiasPath( &path
, gIODTPlane
);
275 look
= path
+ strlen( path
);
277 while( look
!= path
) {
278 if( *(--look
) == c
) {
280 partition
= strtol( look
+ 1, 0, 0 );
282 } else if( c
== '@') {
283 unit
= strtol( look
+ 1, &comp
, 16 );
286 lun
= strtol( comp
+ 1, 0, 16 );
290 } else if( c
== '/') {
296 if( alias
&& (look
== path
)) {
298 look
= path
+ strlen( path
);
302 if( c
|| unit
== -1 || partition
== -1)
305 maxLen
-= strlen( "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
306 maxLen
-= ( alias
? strlen( alias
) : 0 ) + (look
- path
);
307 maxLen
-= strlen( "/@hhhhhhhh,hhhhhhhh:dddddddddd';}" );
310 sprintf( buf
, "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
311 comp
= buf
+ strlen( buf
);
314 strcpy( comp
, alias
);
315 comp
+= strlen( alias
);
318 if ( (look
- path
)) {
319 strncpy( comp
, path
, look
- path
);
325 sprintf ( comp
, "/@%lx,%lx:%ld';}", unit
, lun
, partition
);
329 sprintf( comp
, "/@%lx:%ld';}", unit
, partition
);
334 return( OSDynamicCast(OSDictionary
, OSUnserialize( buf
, 0 )) );
341 OSDictionary
* IOOFPathMatching( const char * path
, char * buf
, int maxLen
)
343 OSDictionary
* matching
;
348 /* need to look up path, get device type,
349 call matching help based on device type */
351 matching
= IODiskMatching( path
, buf
, maxLen
);
357 len
= strlen( kIODeviceTreePlane
":" );
362 strcpy( buf
, kIODeviceTreePlane
":" );
365 len
= strlen( path
);
369 strncpy( comp
, path
, len
);
372 matching
= OSDictionary::withCapacity( 1 );
376 str
= OSString::withCString( buf
);
379 matching
->setObject( kIOPathMatchKey
, str
);
392 IOService
* IOFindMatchingChild( IOService
* service
)
394 // find a matching child service
395 IOService
* child
= 0;
396 OSIterator
* iter
= service
->getClientIterator();
398 while( ( child
= (IOService
*) iter
->getNextObject() ) ) {
399 OSDictionary
* dict
= OSDictionary::withCapacity( 1 );
404 const OSSymbol
* str
= OSSymbol::withCString( "Apple_HFS" );
410 dict
->setObject( "Content", (OSObject
*)str
);
412 if ( child
->compareProperty( dict
, "Content" ) ) {
417 IOService
* subchild
= IOFindMatchingChild( child
);
428 static int didRam
= 0;
430 kern_return_t
IOFindBSDRoot( char * rootName
,
431 dev_t
* root
, u_int32_t
* oflags
)
435 IORegistryEntry
* regEntry
;
436 OSDictionary
* matching
= 0;
440 UInt32
*ramdParms
= 0;
444 bool findHFSChild
= false;
445 char * mediaProperty
= 0;
447 enum { kMaxPathBuf
= 512, kMaxBootVar
= 128 };
449 const char * look
= 0;
451 bool forceNet
= false;
452 bool debugInfoPrintedOnce
= false;
453 const char * uuidStr
= NULL
;
455 static int mountAttempts
= 0;
463 str
= (char *) IOMalloc( kMaxPathBuf
+ kMaxBootVar
);
465 return( kIOReturnNoMemory
);
466 rdBootVar
= str
+ kMaxPathBuf
;
468 if (!PE_parse_boot_arg("rd", rdBootVar
)
469 && !PE_parse_boot_arg("rootdev", rdBootVar
))
473 if( (regEntry
= IORegistryEntry::fromPath( "/chosen", gIODTPlane
))) {
474 data
= (OSData
*) regEntry
->getProperty( "boot-uuid" );
476 uuidStr
= (const char*)data
->getBytesNoCopy();
477 OSString
*uuidString
= OSString::withCString( uuidStr
);
479 // match the boot-args boot-uuid processing below
481 IOLog("rooting via boot-uuid from /chosen: %s\n", uuidStr
);
482 IOService::publishResource( "boot-uuid", uuidString
);
483 uuidString
->release();
484 matching
= IOUUIDMatching();
485 mediaProperty
= "boot-uuid-media";
493 // else try for an OF Path
494 data
= (OSData
*) regEntry
->getProperty( "rootpath" );
498 if( (regEntry
= IORegistryEntry::fromPath( "/options", gIODTPlane
))) {
499 data
= (OSData
*) regEntry
->getProperty( "boot-file" );
505 if( data
&& !uuidStr
)
506 look
= (const char *) data
->getBytesNoCopy();
508 if( rdBootVar
[0] == '*') {
509 look
= rdBootVar
+ 1;
512 if( (regEntry
= IORegistryEntry::fromPath( "/", gIODTPlane
))) {
513 forceNet
= (0 != regEntry
->getProperty( "net-boot" ));
521 // See if we have a RAMDisk property in /chosen/memory-map. If so, make it into a device.
522 // It will become /dev/mdx, where x is 0-f.
525 if(!didRam
) { /* Have we already build this ram disk? */
526 didRam
= 1; /* Remember we did this */
527 if((regEntry
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
))) { /* Find the map node */
528 data
= (OSData
*)regEntry
->getProperty("RAMDisk"); /* Find the ram disk, if there */
529 if(data
) { /* We found one */
531 ramdParms
= (UInt32
*)data
->getBytesNoCopy(); /* Point to the ram disk base and size */
532 (void)mdevadd(-1, ramdParms
[0] >> 12, ramdParms
[1] >> 12, 0); /* Initialize it and pass back the device number */
534 regEntry
->release(); /* Toss the entry */
539 // Now check if we are trying to root on a memory device
542 if((rdBootVar
[0] == 'm') && (rdBootVar
[1] == 'd') && (rdBootVar
[3] == 0)) {
543 dchar
= xchar
= rdBootVar
[2]; /* Get the actual device */
544 if((xchar
>= '0') && (xchar
<= '9')) xchar
= xchar
- '0'; /* If digit, convert */
546 xchar
= xchar
& ~' '; /* Fold to upper case */
547 if((xchar
>= 'A') && (xchar
<= 'F')) { /* Is this a valid digit? */
548 xchar
= (xchar
& 0xF) + 9; /* Convert the hex digit */
549 dchar
= dchar
| ' '; /* Fold to lower case */
551 else xchar
= -1; /* Show bogus */
553 if(xchar
>= 0) { /* Do we have a valid memory device name? */
554 *root
= mdevlookup(xchar
); /* Find the device number */
555 if(*root
>= 0) { /* Did we find one? */
557 rootName
[0] = 'm'; /* Build root name */
558 rootName
[1] = 'd'; /* Build root name */
559 rootName
[2] = dchar
; /* Build root name */
560 rootName
[3] = 0; /* Build root name */
561 IOLog("BSD root: %s, major %d, minor %d\n", rootName
, major(*root
), minor(*root
));
562 *oflags
= 0; /* Show that this is not network */
563 goto iofrootx
; /* Join common exit... */
565 panic("IOFindBSDRoot: specified root memory device, %s, has not been configured\n", rdBootVar
); /* Not there */
570 // from OpenFirmware path
571 IOLog("From path: \"%s\", ", look
);
573 if( forceNet
|| (0 == strncmp( look
, "enet", strlen( "enet" ))) ) {
574 matching
= IONetworkMatching( look
, str
, kMaxPathBuf
);
576 matching
= IODiskMatching( look
, str
, kMaxPathBuf
);
580 if( (!matching
) && rdBootVar
[0] ) {
586 if ( strncmp( look
, "en", strlen( "en" )) == 0 ) {
587 matching
= IONetworkNamePrefixMatching( "en" );
588 } else if ( strncmp( look
, "cdrom", strlen( "cdrom" )) == 0 ) {
589 matching
= IOCDMatching();
591 } else if ( strncmp( look
, "uuid", strlen( "uuid" )) == 0 ) {
593 OSString
*uuidString
;
595 uuid
= (char *)IOMalloc( kMaxBootVar
);
598 if (!PE_parse_boot_arg( "boot-uuid", uuid
)) {
599 panic( "rd=uuid but no boot-uuid=<value> specified" );
601 uuidString
= OSString::withCString( uuid
);
603 IOService::publishResource( "boot-uuid", uuidString
);
604 uuidString
->release();
605 IOLog( "\nWaiting for boot volume with UUID %s\n", uuid
);
606 matching
= IOUUIDMatching();
607 mediaProperty
= "boot-uuid-media";
609 IOFree( uuid
, kMaxBootVar
);
612 matching
= IOBSDNameMatching( look
);
619 matching
= IOService::serviceMatching( "IOMedia" );
620 astring
= OSString::withCStringNoCopy("Apple_HFS");
622 matching
->setObject("Content", astring
);
627 if( true && matching
) {
628 OSSerialize
* s
= OSSerialize::withCapacity( 5 );
630 if( matching
->serialize( s
)) {
631 IOLog( "Waiting on %s\n", s
->text() );
637 t
.tv_sec
= ROOTDEVICETIMEOUT
;
640 service
= IOService::waitForService( matching
, &t
);
641 if( (!service
) || (mountAttempts
== 10)) {
642 PE_display_icon( 0, "noroot");
643 IOLog( "Still waiting for root device\n" );
645 if( !debugInfoPrintedOnce
) {
646 debugInfoPrintedOnce
= true;
647 if( gIOKitDebug
& kIOLogDTree
) {
648 IOLog("\nDT plane:\n");
649 IOPrintPlane( gIODTPlane
);
651 if( gIOKitDebug
& kIOLogServiceTree
) {
652 IOLog("\nService plane:\n");
653 IOPrintPlane( gIOServicePlane
);
655 if( gIOKitDebug
& kIOLogMemory
)
662 if ( service
&& findHFSChild
) {
664 // wait for children services to finish registering
666 t
.tv_sec
= ROOTDEVICETIMEOUT
;
668 if ( service
->waitQuiet( &t
) == kIOReturnSuccess
) {
671 IOLog( "Waiting for child registration\n" );
674 // look for a subservice with an Apple_HFS child
675 IOService
* subservice
= IOFindMatchingChild( service
);
676 if ( subservice
) service
= subservice
;
677 } else if ( service
&& mediaProperty
) {
678 service
= service
->getProperty(mediaProperty
);
684 // If the IOService we matched to is a subclass of IONetworkInterface,
685 // then make sure it has been registered with BSD and has a BSD name
689 && service
->metaCast( "IONetworkInterface" )
690 && !IORegisterNetworkInterface( service
) )
698 service
->getPath( str
, &len
, gIOServicePlane
);
699 IOLog( "Got boot device = %s\n", str
);
701 iostr
= (OSString
*) service
->getProperty( kIOBSDNameKey
);
703 strcpy( rootName
, iostr
->getCStringNoCopy() );
704 off
= (OSNumber
*) service
->getProperty( kIOBSDMajorKey
);
706 major
= off
->unsigned32BitValue();
707 off
= (OSNumber
*) service
->getProperty( kIOBSDMinorKey
);
709 minor
= off
->unsigned32BitValue();
711 if( service
->metaCast( "IONetworkInterface" ))
716 IOLog( "Wait for root failed\n" );
717 strcpy( rootName
, "en0");
721 IOLog( "BSD root: %s", rootName
);
723 IOLog(", major %d, minor %d\n", major
, minor
);
727 *root
= makedev( major
, minor
);
730 IOFree( str
, kMaxPathBuf
+ kMaxBootVar
);
733 if( (gIOKitDebug
& (kIOLogDTree
| kIOLogServiceTree
| kIOLogMemory
)) && !debugInfoPrintedOnce
) {
735 IOService::getPlatform()->waitQuiet();
736 if( gIOKitDebug
& kIOLogDTree
) {
737 IOLog("\nDT plane:\n");
738 IOPrintPlane( gIODTPlane
);
740 if( gIOKitDebug
& kIOLogServiceTree
) {
741 IOLog("\nService plane:\n");
742 IOPrintPlane( gIOServicePlane
);
744 if( gIOKitDebug
& kIOLogMemory
)
748 return( kIOReturnSuccess
);
752 IOBSDRegistryEntryForDeviceTree(char * path
)
754 return (IORegistryEntry::fromPath(path
, gIODTPlane
));
758 IOBSDRegistryEntryRelease(void * entry
)
760 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
768 IOBSDRegistryEntryGetData(void * entry
, char * property_name
,
772 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
774 data
= (OSData
*) regEntry
->getProperty(property_name
);
776 *packet_length
= data
->getLength();
777 return (data
->getBytesNoCopy());