]>
git.saurik.com Git - apple/xnu.git/blob - iokit/bsddev/IOKitBSDInit.cpp
1d9f9af62d326b9cf66f9b73ea697f0fa09c0f9b
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
= OSDynamicCast(OSData
, regEntry
->getProperty( "root-matching" ));
470 matching
= OSDynamicCast(OSDictionary
, OSUnserializeXML((char *)data
->getBytesNoCopy()));
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
);
576 if( forceNet
|| (0 == strncmp( look
, "enet", strlen( "enet" ))) ) {
577 matching
= IONetworkMatching( look
, str
, kMaxPathBuf
);
579 matching
= IODiskMatching( look
, str
, kMaxPathBuf
);
584 if( (!matching
) && rdBootVar
[0] ) {
590 if ( strncmp( look
, "en", strlen( "en" )) == 0 ) {
591 matching
= IONetworkNamePrefixMatching( "en" );
592 } else if ( strncmp( look
, "cdrom", strlen( "cdrom" )) == 0 ) {
593 matching
= IOCDMatching();
595 } else if ( strncmp( look
, "uuid", strlen( "uuid" )) == 0 ) {
597 OSString
*uuidString
;
599 uuid
= (char *)IOMalloc( kMaxBootVar
);
602 if (!PE_parse_boot_arg( "boot-uuid", uuid
)) {
603 panic( "rd=uuid but no boot-uuid=<value> specified" );
605 uuidString
= OSString::withCString( uuid
);
607 IOService::publishResource( "boot-uuid", uuidString
);
608 uuidString
->release();
609 IOLog( "\nWaiting for boot volume with UUID %s\n", uuid
);
610 matching
= IOUUIDMatching();
611 mediaProperty
= "boot-uuid-media";
613 IOFree( uuid
, kMaxBootVar
);
616 matching
= IOBSDNameMatching( look
);
623 matching
= IOService::serviceMatching( "IOMedia" );
624 astring
= OSString::withCStringNoCopy("Apple_HFS");
626 matching
->setObject("Content", astring
);
631 if( true && matching
) {
632 OSSerialize
* s
= OSSerialize::withCapacity( 5 );
634 if( matching
->serialize( s
)) {
635 IOLog( "Waiting on %s\n", s
->text() );
641 t
.tv_sec
= ROOTDEVICETIMEOUT
;
644 service
= IOService::waitForService( matching
, &t
);
645 if( (!service
) || (mountAttempts
== 10)) {
646 PE_display_icon( 0, "noroot");
647 IOLog( "Still waiting for root device\n" );
649 if( !debugInfoPrintedOnce
) {
650 debugInfoPrintedOnce
= true;
651 if( gIOKitDebug
& kIOLogDTree
) {
652 IOLog("\nDT plane:\n");
653 IOPrintPlane( gIODTPlane
);
655 if( gIOKitDebug
& kIOLogServiceTree
) {
656 IOLog("\nService plane:\n");
657 IOPrintPlane( gIOServicePlane
);
659 if( gIOKitDebug
& kIOLogMemory
)
666 if ( service
&& findHFSChild
) {
668 // wait for children services to finish registering
670 t
.tv_sec
= ROOTDEVICETIMEOUT
;
672 if ( service
->waitQuiet( &t
) == kIOReturnSuccess
) {
675 IOLog( "Waiting for child registration\n" );
678 // look for a subservice with an Apple_HFS child
679 IOService
* subservice
= IOFindMatchingChild( service
);
680 if ( subservice
) service
= subservice
;
681 } else if ( service
&& mediaProperty
) {
682 service
= (IOService
*)service
->getProperty(mediaProperty
);
688 // If the IOService we matched to is a subclass of IONetworkInterface,
689 // then make sure it has been registered with BSD and has a BSD name
693 && service
->metaCast( "IONetworkInterface" )
694 && !IORegisterNetworkInterface( service
) )
702 service
->getPath( str
, &len
, gIOServicePlane
);
703 IOLog( "Got boot device = %s\n", str
);
705 iostr
= (OSString
*) service
->getProperty( kIOBSDNameKey
);
707 strcpy( rootName
, iostr
->getCStringNoCopy() );
708 off
= (OSNumber
*) service
->getProperty( kIOBSDMajorKey
);
710 major
= off
->unsigned32BitValue();
711 off
= (OSNumber
*) service
->getProperty( kIOBSDMinorKey
);
713 minor
= off
->unsigned32BitValue();
715 if( service
->metaCast( "IONetworkInterface" ))
720 IOLog( "Wait for root failed\n" );
721 strcpy( rootName
, "en0");
725 IOLog( "BSD root: %s", rootName
);
727 IOLog(", major %d, minor %d\n", major
, minor
);
731 *root
= makedev( major
, minor
);
734 IOFree( str
, kMaxPathBuf
+ kMaxBootVar
);
737 if( (gIOKitDebug
& (kIOLogDTree
| kIOLogServiceTree
| kIOLogMemory
)) && !debugInfoPrintedOnce
) {
739 IOService::getPlatform()->waitQuiet();
740 if( gIOKitDebug
& kIOLogDTree
) {
741 IOLog("\nDT plane:\n");
742 IOPrintPlane( gIODTPlane
);
744 if( gIOKitDebug
& kIOLogServiceTree
) {
745 IOLog("\nService plane:\n");
746 IOPrintPlane( gIOServicePlane
);
748 if( gIOKitDebug
& kIOLogMemory
)
752 return( kIOReturnSuccess
);
756 IOBSDRegistryEntryForDeviceTree(char * path
)
758 return (IORegistryEntry::fromPath(path
, gIODTPlane
));
762 IOBSDRegistryEntryRelease(void * entry
)
764 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
772 IOBSDRegistryEntryGetData(void * entry
, char * property_name
,
776 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
778 data
= (OSData
*) regEntry
->getProperty(property_name
);
780 *packet_length
= data
->getLength();
781 return (data
->getBytesNoCopy());