]>
git.saurik.com Git - apple/xnu.git/blob - iokit/bsddev/IOKitBSDInit.cpp
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 #include <IOKit/IOBSD.h>
23 #include <IOKit/IOLib.h>
24 #include <IOKit/IOService.h>
25 #include <IOKit/IODeviceTreeSupport.h>
26 #include <IOKit/IOKitKeys.h>
27 #include <IOKit/IOPlatformExpert.h>
31 #include <pexpert/pexpert.h>
32 #include <kern/clock.h>
34 // how long to wait for matching root device, secs
35 #define ROOTDEVICETIMEOUT 60
37 extern dev_t
mdevadd(int devid
, ppnum_t base
, unsigned int size
, int phys
);
38 extern dev_t
mdevlookup(int devid
);
43 IOService::publishResource("IOBSD");
45 return( kIOReturnSuccess
);
48 OSDictionary
* IOBSDNameMatching( const char * name
)
51 const OSSymbol
* str
= 0;
55 dict
= IOService::serviceMatching( gIOServiceKey
);
58 str
= OSSymbol::withCString( name
);
61 dict
->setObject( kIOBSDNameKey
, (OSObject
*) str
);
76 OSDictionary
* IOUUIDMatching( void )
78 return IOService::resourceMatching( "boot-uuid-media" );
82 OSDictionary
* IOCDMatching( void )
87 dict
= IOService::serviceMatching( "IOMedia" );
89 IOLog("Unable to find IOMedia\n");
93 str
= OSSymbol::withCString( "CD_ROM_Mode_1" );
99 dict
->setObject( "Content Hint", (OSObject
*)str
);
104 OSDictionary
* IONetworkMatching( const char * path
,
105 char * buf
, int maxLen
)
107 OSDictionary
* matching
= 0;
116 len
= strlen( kIODeviceTreePlane
":" );
121 strcpy( buf
, kIODeviceTreePlane
":" );
124 // remove parameters following ':' from the path
125 skip
= strchr( path
, ':');
133 strncpy( comp
, path
, len
);
136 matching
= IOService::serviceMatching( "IONetworkInterface" );
139 dict
= IOService::addLocation( matching
);
143 str
= OSString::withCString( buf
);
146 dict
->setObject( kIOPathMatchKey
, str
);
159 OSDictionary
* IONetworkNamePrefixMatching( const char * prefix
)
161 OSDictionary
* matching
;
162 OSDictionary
* propDict
= 0;
163 const OSSymbol
* str
= 0;
166 matching
= IOService::serviceMatching( "IONetworkInterface" );
170 propDict
= OSDictionary::withCapacity(1);
174 str
= OSSymbol::withCString( prefix
);
178 propDict
->setObject( "IOInterfaceNamePrefix", (OSObject
*) str
);
182 if ( matching
->setObject( gIOPropertyMatchKey
,
183 (OSObject
*) propDict
) != true )
193 if ( matching
) matching
->release();
194 if ( propDict
) propDict
->release();
195 if ( str
) str
->release();
200 static bool IORegisterNetworkInterface( IOService
* netif
)
202 // A network interface is typically named and registered
203 // with BSD after receiving a request from a user space
204 // "namer". However, for cases when the system needs to
205 // root from the network, this registration task must be
206 // done inside the kernel and completed before the root
207 // device is handed to BSD.
212 OSDictionary
* dict
= 0;
215 enum { kMaxPathLen
= 512 };
218 stack
= IOService::waitForService(
219 IOService::serviceMatching("IONetworkStack") );
220 if ( stack
== 0 ) break;
222 dict
= OSDictionary::withCapacity(3);
223 if ( dict
== 0 ) break;
225 zero
= OSNumber::withNumber((UInt64
) 0, 32);
226 if ( zero
== 0 ) break;
228 pathBuf
= (char *) IOMalloc( kMaxPathLen
);
229 if ( pathBuf
== 0 ) break;
232 if ( netif
->getPath( pathBuf
, &len
, gIOServicePlane
)
235 path
= OSString::withCStringNoCopy( pathBuf
);
236 if ( path
== 0 ) break;
238 dict
->setObject( "IOInterfaceUnit", zero
);
239 dict
->setObject( kIOPathMatchKey
, path
);
241 stack
->setProperties( dict
);
245 if ( zero
) zero
->release();
246 if ( path
) path
->release();
247 if ( dict
) dict
->release();
248 if ( pathBuf
) IOFree(pathBuf
, kMaxPathLen
);
250 return ( netif
->getProperty( kIOBSDNameKey
) != 0 );
253 OSDictionary
* IODiskMatching( const char * path
, char * buf
, int maxLen
)
263 // scan the tail of the path for "@unit:partition"
265 // Have to get the full path to the controller - an alias may
266 // tell us next to nothing, like "hd:8"
267 alias
= IORegistryEntry::dealiasPath( &path
, gIODTPlane
);
269 look
= path
+ strlen( path
);
271 while( look
!= path
) {
272 if( *(--look
) == c
) {
274 partition
= strtol( look
+ 1, 0, 0 );
276 } else if( c
== '@') {
277 unit
= strtol( look
+ 1, &comp
, 16 );
280 lun
= strtol( comp
+ 1, 0, 16 );
284 } else if( c
== '/') {
290 if( alias
&& (look
== path
)) {
292 look
= path
+ strlen( path
);
296 if( c
|| unit
== -1 || partition
== -1)
299 maxLen
-= strlen( "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
300 maxLen
-= ( alias
? strlen( alias
) : 0 ) + (look
- path
);
301 maxLen
-= strlen( "/@hhhhhhhh,hhhhhhhh:dddddddddd';}" );
304 sprintf( buf
, "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
305 comp
= buf
+ strlen( buf
);
308 strcpy( comp
, alias
);
309 comp
+= strlen( alias
);
312 if ( (look
- path
)) {
313 strncpy( comp
, path
, look
- path
);
319 sprintf ( comp
, "/@%lx,%lx:%ld';}", unit
, lun
, partition
);
323 sprintf( comp
, "/@%lx:%ld';}", unit
, partition
);
328 return( OSDynamicCast(OSDictionary
, OSUnserialize( buf
, 0 )) );
335 OSDictionary
* IOOFPathMatching( const char * path
, char * buf
, int maxLen
)
337 OSDictionary
* matching
;
342 /* need to look up path, get device type,
343 call matching help based on device type */
345 matching
= IODiskMatching( path
, buf
, maxLen
);
351 len
= strlen( kIODeviceTreePlane
":" );
356 strcpy( buf
, kIODeviceTreePlane
":" );
359 len
= strlen( path
);
363 strncpy( comp
, path
, len
);
366 matching
= OSDictionary::withCapacity( 1 );
370 str
= OSString::withCString( buf
);
373 matching
->setObject( kIOPathMatchKey
, str
);
386 IOService
* IOFindMatchingChild( IOService
* service
)
388 // find a matching child service
389 IOService
* child
= 0;
390 OSIterator
* iter
= service
->getClientIterator();
392 while( ( child
= (IOService
*) iter
->getNextObject() ) ) {
393 OSDictionary
* dict
= OSDictionary::withCapacity( 1 );
398 const OSSymbol
* str
= OSSymbol::withCString( "Apple_HFS" );
404 dict
->setObject( "Content", (OSObject
*)str
);
406 if ( child
->compareProperty( dict
, "Content" ) ) {
411 IOService
* subchild
= IOFindMatchingChild( child
);
422 static int didRam
= 0;
424 kern_return_t
IOFindBSDRoot( char * rootName
,
425 dev_t
* root
, u_int32_t
* oflags
)
429 IORegistryEntry
* regEntry
;
430 OSDictionary
* matching
= 0;
434 UInt32
*ramdParms
= 0;
438 bool findHFSChild
= false;
439 char * mediaProperty
= 0;
441 enum { kMaxPathBuf
= 512, kMaxBootVar
= 128 };
443 const char * look
= 0;
445 bool forceNet
= false;
446 bool debugInfoPrintedOnce
= false;
447 const char * uuidStr
= NULL
;
449 static int mountAttempts
= 0;
457 str
= (char *) IOMalloc( kMaxPathBuf
+ kMaxBootVar
);
459 return( kIOReturnNoMemory
);
460 rdBootVar
= str
+ kMaxPathBuf
;
462 if (!PE_parse_boot_arg("rd", rdBootVar
)
463 && !PE_parse_boot_arg("rootdev", rdBootVar
))
467 if( (regEntry
= IORegistryEntry::fromPath( "/chosen", gIODTPlane
))) {
468 data
= (OSData
*) regEntry
->getProperty( "boot-uuid" );
470 uuidStr
= (const char*)data
->getBytesNoCopy();
471 OSString
*uuidString
= OSString::withCString( uuidStr
);
473 // match the boot-args boot-uuid processing below
475 IOLog("rooting via boot-uuid from /chosen: %s\n", uuidStr
);
476 IOService::publishResource( "boot-uuid", uuidString
);
477 uuidString
->release();
478 matching
= IOUUIDMatching();
479 mediaProperty
= "boot-uuid-media";
487 // else try for an OF Path
488 data
= (OSData
*) regEntry
->getProperty( "rootpath" );
492 if( (regEntry
= IORegistryEntry::fromPath( "/options", gIODTPlane
))) {
493 data
= (OSData
*) regEntry
->getProperty( "boot-file" );
499 if( data
&& !uuidStr
)
500 look
= (const char *) data
->getBytesNoCopy();
502 if( rdBootVar
[0] == '*') {
503 look
= rdBootVar
+ 1;
506 if( (regEntry
= IORegistryEntry::fromPath( "/", gIODTPlane
))) {
507 forceNet
= (0 != regEntry
->getProperty( "net-boot" ));
515 // See if we have a RAMDisk property in /chosen/memory-map. If so, make it into a device.
516 // It will become /dev/mdx, where x is 0-f.
519 if(!didRam
) { /* Have we already build this ram disk? */
520 didRam
= 1; /* Remember we did this */
521 if((regEntry
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
))) { /* Find the map node */
522 data
= (OSData
*)regEntry
->getProperty("RAMDisk"); /* Find the ram disk, if there */
523 if(data
) { /* We found one */
525 ramdParms
= (UInt32
*)data
->getBytesNoCopy(); /* Point to the ram disk base and size */
526 (void)mdevadd(-1, ramdParms
[0] >> 12, ramdParms
[1] >> 12, 0); /* Initialize it and pass back the device number */
528 regEntry
->release(); /* Toss the entry */
533 // Now check if we are trying to root on a memory device
536 if((rdBootVar
[0] == 'm') && (rdBootVar
[1] == 'd') && (rdBootVar
[3] == 0)) {
537 dchar
= xchar
= rdBootVar
[2]; /* Get the actual device */
538 if((xchar
>= '0') && (xchar
<= '9')) xchar
= xchar
- '0'; /* If digit, convert */
540 xchar
= xchar
& ~' '; /* Fold to upper case */
541 if((xchar
>= 'A') && (xchar
<= 'F')) { /* Is this a valid digit? */
542 xchar
= (xchar
& 0xF) + 9; /* Convert the hex digit */
543 dchar
= dchar
| ' '; /* Fold to lower case */
545 else xchar
= -1; /* Show bogus */
547 if(xchar
>= 0) { /* Do we have a valid memory device name? */
548 *root
= mdevlookup(xchar
); /* Find the device number */
549 if(*root
>= 0) { /* Did we find one? */
551 rootName
[0] = 'm'; /* Build root name */
552 rootName
[1] = 'd'; /* Build root name */
553 rootName
[2] = dchar
; /* Build root name */
554 rootName
[3] = 0; /* Build root name */
555 IOLog("BSD root: %s, major %d, minor %d\n", rootName
, major(*root
), minor(*root
));
556 *oflags
= 0; /* Show that this is not network */
557 goto iofrootx
; /* Join common exit... */
559 panic("IOFindBSDRoot: specified root memory device, %s, has not been configured\n", rdBootVar
); /* Not there */
564 // from OpenFirmware path
565 IOLog("From path: \"%s\", ", look
);
567 if( forceNet
|| (0 == strncmp( look
, "enet", strlen( "enet" ))) ) {
568 matching
= IONetworkMatching( look
, str
, kMaxPathBuf
);
570 matching
= IODiskMatching( look
, str
, kMaxPathBuf
);
574 if( (!matching
) && rdBootVar
[0] ) {
580 if ( strncmp( look
, "en", strlen( "en" )) == 0 ) {
581 matching
= IONetworkNamePrefixMatching( "en" );
582 } else if ( strncmp( look
, "cdrom", strlen( "cdrom" )) == 0 ) {
583 matching
= IOCDMatching();
585 } else if ( strncmp( look
, "uuid", strlen( "uuid" )) == 0 ) {
587 OSString
*uuidString
;
589 uuid
= (char *)IOMalloc( kMaxBootVar
);
592 if (!PE_parse_boot_arg( "boot-uuid", uuid
)) {
593 panic( "rd=uuid but no boot-uuid=<value> specified" );
595 uuidString
= OSString::withCString( uuid
);
597 IOService::publishResource( "boot-uuid", uuidString
);
598 uuidString
->release();
599 IOLog( "\nWaiting for boot volume with UUID %s\n", uuid
);
600 matching
= IOUUIDMatching();
601 mediaProperty
= "boot-uuid-media";
603 IOFree( uuid
, kMaxBootVar
);
606 matching
= IOBSDNameMatching( look
);
613 matching
= IOService::serviceMatching( "IOMedia" );
614 astring
= OSString::withCStringNoCopy("Apple_HFS");
616 matching
->setObject("Content", astring
);
621 if( true && matching
) {
622 OSSerialize
* s
= OSSerialize::withCapacity( 5 );
624 if( matching
->serialize( s
)) {
625 IOLog( "Waiting on %s\n", s
->text() );
631 t
.tv_sec
= ROOTDEVICETIMEOUT
;
634 service
= IOService::waitForService( matching
, &t
);
635 if( (!service
) || (mountAttempts
== 10)) {
636 PE_display_icon( 0, "noroot");
637 IOLog( "Still waiting for root device\n" );
639 if( !debugInfoPrintedOnce
) {
640 debugInfoPrintedOnce
= true;
641 if( gIOKitDebug
& kIOLogDTree
) {
642 IOLog("\nDT plane:\n");
643 IOPrintPlane( gIODTPlane
);
645 if( gIOKitDebug
& kIOLogServiceTree
) {
646 IOLog("\nService plane:\n");
647 IOPrintPlane( gIOServicePlane
);
649 if( gIOKitDebug
& kIOLogMemory
)
656 if ( service
&& findHFSChild
) {
658 // wait for children services to finish registering
660 t
.tv_sec
= ROOTDEVICETIMEOUT
;
662 if ( service
->waitQuiet( &t
) == kIOReturnSuccess
) {
665 IOLog( "Waiting for child registration\n" );
668 // look for a subservice with an Apple_HFS child
669 IOService
* subservice
= IOFindMatchingChild( service
);
670 if ( subservice
) service
= subservice
;
671 } else if ( service
&& mediaProperty
) {
672 service
= service
->getProperty(mediaProperty
);
678 // If the IOService we matched to is a subclass of IONetworkInterface,
679 // then make sure it has been registered with BSD and has a BSD name
683 && service
->metaCast( "IONetworkInterface" )
684 && !IORegisterNetworkInterface( service
) )
692 service
->getPath( str
, &len
, gIOServicePlane
);
693 IOLog( "Got boot device = %s\n", str
);
695 iostr
= (OSString
*) service
->getProperty( kIOBSDNameKey
);
697 strcpy( rootName
, iostr
->getCStringNoCopy() );
698 off
= (OSNumber
*) service
->getProperty( kIOBSDMajorKey
);
700 major
= off
->unsigned32BitValue();
701 off
= (OSNumber
*) service
->getProperty( kIOBSDMinorKey
);
703 minor
= off
->unsigned32BitValue();
705 if( service
->metaCast( "IONetworkInterface" ))
710 IOLog( "Wait for root failed\n" );
711 strcpy( rootName
, "en0");
715 IOLog( "BSD root: %s", rootName
);
717 IOLog(", major %d, minor %d\n", major
, minor
);
721 *root
= makedev( major
, minor
);
724 IOFree( str
, kMaxPathBuf
+ kMaxBootVar
);
727 if( (gIOKitDebug
& (kIOLogDTree
| kIOLogServiceTree
| kIOLogMemory
)) && !debugInfoPrintedOnce
) {
729 IOService::getPlatform()->waitQuiet();
730 if( gIOKitDebug
& kIOLogDTree
) {
731 IOLog("\nDT plane:\n");
732 IOPrintPlane( gIODTPlane
);
734 if( gIOKitDebug
& kIOLogServiceTree
) {
735 IOLog("\nService plane:\n");
736 IOPrintPlane( gIOServicePlane
);
738 if( gIOKitDebug
& kIOLogMemory
)
742 return( kIOReturnSuccess
);
746 IOBSDRegistryEntryForDeviceTree(char * path
)
748 return (IORegistryEntry::fromPath(path
, gIODTPlane
));
752 IOBSDRegistryEntryRelease(void * entry
)
754 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
762 IOBSDRegistryEntryGetData(void * entry
, char * property_name
,
766 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
768 data
= (OSData
*) regEntry
->getProperty(property_name
);
770 *packet_length
= data
->getLength();
771 return (data
->getBytesNoCopy());