]>
git.saurik.com Git - apple/xnu.git/blob - iokit/bsddev/IOKitBSDInit.cpp
c921715044a3914761a9b51c656cd50bdb5bb322
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 #include <IOKit/IOBSD.h>
31 #include <IOKit/IOLib.h>
32 #include <IOKit/IOService.h>
33 #include <IOKit/IODeviceTreeSupport.h>
34 #include <IOKit/IOKitKeys.h>
35 #include <IOKit/IOPlatformExpert.h>
39 #include <pexpert/pexpert.h>
40 #include <kern/clock.h>
42 // how long to wait for matching root device, secs
43 #define ROOTDEVICETIMEOUT 60
45 extern dev_t
mdevadd(int devid
, ppnum_t base
, unsigned int size
, int phys
);
46 extern dev_t
mdevlookup(int devid
);
51 IOService::publishResource("IOBSD");
53 return( kIOReturnSuccess
);
56 OSDictionary
* IOBSDNameMatching( const char * name
)
59 const OSSymbol
* str
= 0;
63 dict
= IOService::serviceMatching( gIOServiceKey
);
66 str
= OSSymbol::withCString( name
);
69 dict
->setObject( kIOBSDNameKey
, (OSObject
*) str
);
84 OSDictionary
* IOUUIDMatching( void )
86 return IOService::resourceMatching( "boot-uuid-media" );
90 OSDictionary
* IOCDMatching( void )
95 dict
= IOService::serviceMatching( "IOMedia" );
97 IOLog("Unable to find IOMedia\n");
101 str
= OSSymbol::withCString( "CD_ROM_Mode_1" );
107 dict
->setObject( "Content Hint", (OSObject
*)str
);
112 OSDictionary
* IONetworkMatching( const char * path
,
113 char * buf
, int maxLen
)
115 OSDictionary
* matching
= 0;
124 len
= strlen( kIODeviceTreePlane
":" );
129 strcpy( buf
, kIODeviceTreePlane
":" );
132 // remove parameters following ':' from the path
133 skip
= strchr( path
, ':');
141 strncpy( comp
, path
, len
);
144 matching
= IOService::serviceMatching( "IONetworkInterface" );
147 dict
= IOService::addLocation( matching
);
151 str
= OSString::withCString( buf
);
154 dict
->setObject( kIOPathMatchKey
, str
);
167 OSDictionary
* IONetworkNamePrefixMatching( const char * prefix
)
169 OSDictionary
* matching
;
170 OSDictionary
* propDict
= 0;
171 const OSSymbol
* str
= 0;
174 matching
= IOService::serviceMatching( "IONetworkInterface" );
178 propDict
= OSDictionary::withCapacity(1);
182 str
= OSSymbol::withCString( prefix
);
186 propDict
->setObject( "IOInterfaceNamePrefix", (OSObject
*) str
);
190 if ( matching
->setObject( gIOPropertyMatchKey
,
191 (OSObject
*) propDict
) != true )
201 if ( matching
) matching
->release();
202 if ( propDict
) propDict
->release();
203 if ( str
) str
->release();
208 static bool IORegisterNetworkInterface( IOService
* netif
)
210 // A network interface is typically named and registered
211 // with BSD after receiving a request from a user space
212 // "namer". However, for cases when the system needs to
213 // root from the network, this registration task must be
214 // done inside the kernel and completed before the root
215 // device is handed to BSD.
220 OSDictionary
* dict
= 0;
223 enum { kMaxPathLen
= 512 };
226 stack
= IOService::waitForService(
227 IOService::serviceMatching("IONetworkStack") );
228 if ( stack
== 0 ) break;
230 dict
= OSDictionary::withCapacity(3);
231 if ( dict
== 0 ) break;
233 zero
= OSNumber::withNumber((UInt64
) 0, 32);
234 if ( zero
== 0 ) break;
236 pathBuf
= (char *) IOMalloc( kMaxPathLen
);
237 if ( pathBuf
== 0 ) break;
240 if ( netif
->getPath( pathBuf
, &len
, gIOServicePlane
)
243 path
= OSString::withCStringNoCopy( pathBuf
);
244 if ( path
== 0 ) break;
246 dict
->setObject( "IOInterfaceUnit", zero
);
247 dict
->setObject( kIOPathMatchKey
, path
);
249 stack
->setProperties( dict
);
253 if ( zero
) zero
->release();
254 if ( path
) path
->release();
255 if ( dict
) dict
->release();
256 if ( pathBuf
) IOFree(pathBuf
, kMaxPathLen
);
258 return ( netif
->getProperty( kIOBSDNameKey
) != 0 );
261 OSDictionary
* IODiskMatching( const char * path
, char * buf
, int maxLen
)
271 // scan the tail of the path for "@unit:partition"
273 // Have to get the full path to the controller - an alias may
274 // tell us next to nothing, like "hd:8"
275 alias
= IORegistryEntry::dealiasPath( &path
, gIODTPlane
);
277 look
= path
+ strlen( path
);
279 while( look
!= path
) {
280 if( *(--look
) == c
) {
282 partition
= strtol( look
+ 1, 0, 0 );
284 } else if( c
== '@') {
285 unit
= strtol( look
+ 1, &comp
, 16 );
288 lun
= strtol( comp
+ 1, 0, 16 );
292 } else if( c
== '/') {
298 if( alias
&& (look
== path
)) {
300 look
= path
+ strlen( path
);
304 if( c
|| unit
== -1 || partition
== -1)
307 maxLen
-= strlen( "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
308 maxLen
-= ( alias
? strlen( alias
) : 0 ) + (look
- path
);
309 maxLen
-= strlen( "/@hhhhhhhh,hhhhhhhh:dddddddddd';}" );
312 sprintf( buf
, "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
313 comp
= buf
+ strlen( buf
);
316 strcpy( comp
, alias
);
317 comp
+= strlen( alias
);
320 if ( (look
- path
)) {
321 strncpy( comp
, path
, look
- path
);
327 sprintf ( comp
, "/@%lx,%lx:%ld';}", unit
, lun
, partition
);
331 sprintf( comp
, "/@%lx:%ld';}", unit
, partition
);
336 return( OSDynamicCast(OSDictionary
, OSUnserialize( buf
, 0 )) );
343 OSDictionary
* IOOFPathMatching( const char * path
, char * buf
, int maxLen
)
345 OSDictionary
* matching
;
350 /* need to look up path, get device type,
351 call matching help based on device type */
353 matching
= IODiskMatching( path
, buf
, maxLen
);
359 len
= strlen( kIODeviceTreePlane
":" );
364 strcpy( buf
, kIODeviceTreePlane
":" );
367 len
= strlen( path
);
371 strncpy( comp
, path
, len
);
374 matching
= OSDictionary::withCapacity( 1 );
378 str
= OSString::withCString( buf
);
381 matching
->setObject( kIOPathMatchKey
, str
);
394 IOService
* IOFindMatchingChild( IOService
* service
)
396 // find a matching child service
397 IOService
* child
= 0;
398 OSIterator
* iter
= service
->getClientIterator();
400 while( ( child
= (IOService
*) iter
->getNextObject() ) ) {
401 OSDictionary
* dict
= OSDictionary::withCapacity( 1 );
406 const OSSymbol
* str
= OSSymbol::withCString( "Apple_HFS" );
412 dict
->setObject( "Content", (OSObject
*)str
);
414 if ( child
->compareProperty( dict
, "Content" ) ) {
419 IOService
* subchild
= IOFindMatchingChild( child
);
430 static int didRam
= 0;
432 kern_return_t
IOFindBSDRoot( char * rootName
,
433 dev_t
* root
, u_int32_t
* oflags
)
437 IORegistryEntry
* regEntry
;
438 OSDictionary
* matching
= 0;
442 UInt32
*ramdParms
= 0;
446 bool findHFSChild
= false;
447 char * mediaProperty
= 0;
449 enum { kMaxPathBuf
= 512, kMaxBootVar
= 128 };
451 const char * look
= 0;
453 bool forceNet
= false;
454 bool debugInfoPrintedOnce
= false;
455 const char * uuidStr
= NULL
;
457 static int mountAttempts
= 0;
465 str
= (char *) IOMalloc( kMaxPathBuf
+ kMaxBootVar
);
467 return( kIOReturnNoMemory
);
468 rdBootVar
= str
+ kMaxPathBuf
;
470 if (!PE_parse_boot_arg("rd", rdBootVar
)
471 && !PE_parse_boot_arg("rootdev", rdBootVar
))
475 if( (regEntry
= IORegistryEntry::fromPath( "/chosen", gIODTPlane
))) {
476 data
= (OSData
*) regEntry
->getProperty( "boot-uuid" );
478 uuidStr
= (const char*)data
->getBytesNoCopy();
479 OSString
*uuidString
= OSString::withCString( uuidStr
);
481 // match the boot-args boot-uuid processing below
483 IOLog("rooting via boot-uuid from /chosen: %s\n", uuidStr
);
484 IOService::publishResource( "boot-uuid", uuidString
);
485 uuidString
->release();
486 matching
= IOUUIDMatching();
487 mediaProperty
= "boot-uuid-media";
495 // else try for an OF Path
496 data
= (OSData
*) regEntry
->getProperty( "rootpath" );
500 if( (regEntry
= IORegistryEntry::fromPath( "/options", gIODTPlane
))) {
501 data
= (OSData
*) regEntry
->getProperty( "boot-file" );
507 if( data
&& !uuidStr
)
508 look
= (const char *) data
->getBytesNoCopy();
510 if( rdBootVar
[0] == '*') {
511 look
= rdBootVar
+ 1;
514 if( (regEntry
= IORegistryEntry::fromPath( "/", gIODTPlane
))) {
515 forceNet
= (0 != regEntry
->getProperty( "net-boot" ));
523 // See if we have a RAMDisk property in /chosen/memory-map. If so, make it into a device.
524 // It will become /dev/mdx, where x is 0-f.
527 if(!didRam
) { /* Have we already build this ram disk? */
528 didRam
= 1; /* Remember we did this */
529 if((regEntry
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
))) { /* Find the map node */
530 data
= (OSData
*)regEntry
->getProperty("RAMDisk"); /* Find the ram disk, if there */
531 if(data
) { /* We found one */
533 ramdParms
= (UInt32
*)data
->getBytesNoCopy(); /* Point to the ram disk base and size */
534 (void)mdevadd(-1, ramdParms
[0] >> 12, ramdParms
[1] >> 12, 0); /* Initialize it and pass back the device number */
536 regEntry
->release(); /* Toss the entry */
541 // Now check if we are trying to root on a memory device
544 if((rdBootVar
[0] == 'm') && (rdBootVar
[1] == 'd') && (rdBootVar
[3] == 0)) {
545 dchar
= xchar
= rdBootVar
[2]; /* Get the actual device */
546 if((xchar
>= '0') && (xchar
<= '9')) xchar
= xchar
- '0'; /* If digit, convert */
548 xchar
= xchar
& ~' '; /* Fold to upper case */
549 if((xchar
>= 'A') && (xchar
<= 'F')) { /* Is this a valid digit? */
550 xchar
= (xchar
& 0xF) + 9; /* Convert the hex digit */
551 dchar
= dchar
| ' '; /* Fold to lower case */
553 else xchar
= -1; /* Show bogus */
555 if(xchar
>= 0) { /* Do we have a valid memory device name? */
556 *root
= mdevlookup(xchar
); /* Find the device number */
557 if(*root
>= 0) { /* Did we find one? */
559 rootName
[0] = 'm'; /* Build root name */
560 rootName
[1] = 'd'; /* Build root name */
561 rootName
[2] = dchar
; /* Build root name */
562 rootName
[3] = 0; /* Build root name */
563 IOLog("BSD root: %s, major %d, minor %d\n", rootName
, major(*root
), minor(*root
));
564 *oflags
= 0; /* Show that this is not network */
565 goto iofrootx
; /* Join common exit... */
567 panic("IOFindBSDRoot: specified root memory device, %s, has not been configured\n", rdBootVar
); /* Not there */
572 // from OpenFirmware path
573 IOLog("From path: \"%s\", ", look
);
575 if( forceNet
|| (0 == strncmp( look
, "enet", strlen( "enet" ))) ) {
576 matching
= IONetworkMatching( look
, str
, kMaxPathBuf
);
578 matching
= IODiskMatching( look
, str
, kMaxPathBuf
);
582 if( (!matching
) && rdBootVar
[0] ) {
588 if ( strncmp( look
, "en", strlen( "en" )) == 0 ) {
589 matching
= IONetworkNamePrefixMatching( "en" );
590 } else if ( strncmp( look
, "cdrom", strlen( "cdrom" )) == 0 ) {
591 matching
= IOCDMatching();
593 } else if ( strncmp( look
, "uuid", strlen( "uuid" )) == 0 ) {
595 OSString
*uuidString
;
597 uuid
= (char *)IOMalloc( kMaxBootVar
);
600 if (!PE_parse_boot_arg( "boot-uuid", uuid
)) {
601 panic( "rd=uuid but no boot-uuid=<value> specified" );
603 uuidString
= OSString::withCString( uuid
);
605 IOService::publishResource( "boot-uuid", uuidString
);
606 uuidString
->release();
607 IOLog( "\nWaiting for boot volume with UUID %s\n", uuid
);
608 matching
= IOUUIDMatching();
609 mediaProperty
= "boot-uuid-media";
611 IOFree( uuid
, kMaxBootVar
);
614 matching
= IOBSDNameMatching( look
);
621 matching
= IOService::serviceMatching( "IOMedia" );
622 astring
= OSString::withCStringNoCopy("Apple_HFS");
624 matching
->setObject("Content", astring
);
629 if( true && matching
) {
630 OSSerialize
* s
= OSSerialize::withCapacity( 5 );
632 if( matching
->serialize( s
)) {
633 IOLog( "Waiting on %s\n", s
->text() );
639 t
.tv_sec
= ROOTDEVICETIMEOUT
;
642 service
= IOService::waitForService( matching
, &t
);
643 if( (!service
) || (mountAttempts
== 10)) {
644 PE_display_icon( 0, "noroot");
645 IOLog( "Still waiting for root device\n" );
647 if( !debugInfoPrintedOnce
) {
648 debugInfoPrintedOnce
= true;
649 if( gIOKitDebug
& kIOLogDTree
) {
650 IOLog("\nDT plane:\n");
651 IOPrintPlane( gIODTPlane
);
653 if( gIOKitDebug
& kIOLogServiceTree
) {
654 IOLog("\nService plane:\n");
655 IOPrintPlane( gIOServicePlane
);
657 if( gIOKitDebug
& kIOLogMemory
)
664 if ( service
&& findHFSChild
) {
666 // wait for children services to finish registering
668 t
.tv_sec
= ROOTDEVICETIMEOUT
;
670 if ( service
->waitQuiet( &t
) == kIOReturnSuccess
) {
673 IOLog( "Waiting for child registration\n" );
676 // look for a subservice with an Apple_HFS child
677 IOService
* subservice
= IOFindMatchingChild( service
);
678 if ( subservice
) service
= subservice
;
679 } else if ( service
&& mediaProperty
) {
680 service
= service
->getProperty(mediaProperty
);
686 // If the IOService we matched to is a subclass of IONetworkInterface,
687 // then make sure it has been registered with BSD and has a BSD name
691 && service
->metaCast( "IONetworkInterface" )
692 && !IORegisterNetworkInterface( service
) )
700 service
->getPath( str
, &len
, gIOServicePlane
);
701 IOLog( "Got boot device = %s\n", str
);
703 iostr
= (OSString
*) service
->getProperty( kIOBSDNameKey
);
705 strcpy( rootName
, iostr
->getCStringNoCopy() );
706 off
= (OSNumber
*) service
->getProperty( kIOBSDMajorKey
);
708 major
= off
->unsigned32BitValue();
709 off
= (OSNumber
*) service
->getProperty( kIOBSDMinorKey
);
711 minor
= off
->unsigned32BitValue();
713 if( service
->metaCast( "IONetworkInterface" ))
718 IOLog( "Wait for root failed\n" );
719 strcpy( rootName
, "en0");
723 IOLog( "BSD root: %s", rootName
);
725 IOLog(", major %d, minor %d\n", major
, minor
);
729 *root
= makedev( major
, minor
);
732 IOFree( str
, kMaxPathBuf
+ kMaxBootVar
);
735 if( (gIOKitDebug
& (kIOLogDTree
| kIOLogServiceTree
| kIOLogMemory
)) && !debugInfoPrintedOnce
) {
737 IOService::getPlatform()->waitQuiet();
738 if( gIOKitDebug
& kIOLogDTree
) {
739 IOLog("\nDT plane:\n");
740 IOPrintPlane( gIODTPlane
);
742 if( gIOKitDebug
& kIOLogServiceTree
) {
743 IOLog("\nService plane:\n");
744 IOPrintPlane( gIOServicePlane
);
746 if( gIOKitDebug
& kIOLogMemory
)
750 return( kIOReturnSuccess
);
754 IOBSDRegistryEntryForDeviceTree(char * path
)
756 return (IORegistryEntry::fromPath(path
, gIODTPlane
));
760 IOBSDRegistryEntryRelease(void * entry
)
762 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
770 IOBSDRegistryEntryGetData(void * entry
, char * property_name
,
774 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
776 data
= (OSData
*) regEntry
->getProperty(property_name
);
778 *packet_length
= data
->getLength();
779 return (data
->getBytesNoCopy());