]>
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_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>
39 #include <uuid/uuid.h>
41 // how long to wait for matching root device, secs
42 #define ROOTDEVICETIMEOUT 60
44 extern dev_t
mdevadd(int devid
, ppnum_t base
, unsigned int size
, int phys
);
45 extern dev_t
mdevlookup(int devid
);
50 IOService::publishResource("IOBSD");
52 return( kIOReturnSuccess
);
55 OSDictionary
* IOBSDNameMatching( const char * name
)
58 const OSSymbol
* str
= 0;
62 dict
= IOService::serviceMatching( gIOServiceKey
);
65 str
= OSSymbol::withCString( name
);
68 dict
->setObject( kIOBSDNameKey
, (OSObject
*) str
);
83 OSDictionary
* IOUUIDMatching( void )
85 return IOService::resourceMatching( "boot-uuid-media" );
89 OSDictionary
* IOCDMatching( void )
94 dict
= IOService::serviceMatching( "IOMedia" );
96 IOLog("Unable to find IOMedia\n");
100 str
= OSSymbol::withCString( "CD_ROM_Mode_1" );
106 dict
->setObject( "Content Hint", (OSObject
*)str
);
111 OSDictionary
* IONetworkMatching( const char * path
,
112 char * buf
, int maxLen
)
114 OSDictionary
* matching
= 0;
123 len
= strlen( kIODeviceTreePlane
":" );
128 strcpy( buf
, kIODeviceTreePlane
":" );
131 // remove parameters following ':' from the path
132 skip
= strchr( path
, ':');
140 strncpy( comp
, path
, len
);
143 matching
= IOService::serviceMatching( "IONetworkInterface" );
146 dict
= IOService::addLocation( matching
);
150 str
= OSString::withCString( buf
);
153 dict
->setObject( kIOPathMatchKey
, str
);
166 OSDictionary
* IONetworkNamePrefixMatching( const char * prefix
)
168 OSDictionary
* matching
;
169 OSDictionary
* propDict
= 0;
170 const OSSymbol
* str
= 0;
171 char networkType
[128];
174 matching
= IOService::serviceMatching( "IONetworkInterface" );
178 propDict
= OSDictionary::withCapacity(1);
182 str
= OSSymbol::withCString( prefix
);
186 propDict
->setObject( "IOInterfaceNamePrefix", (OSObject
*) str
);
190 // see if we're contrained to netroot off of specific network type
191 if(PE_parse_boot_argn( "network-type", networkType
, 128 ))
193 str
= OSSymbol::withCString( networkType
);
196 propDict
->setObject( "IONetworkRootType", str
);
202 if ( matching
->setObject( gIOPropertyMatchKey
,
203 (OSObject
*) propDict
) != true )
213 if ( matching
) matching
->release();
214 if ( propDict
) propDict
->release();
215 if ( str
) str
->release();
220 static bool IORegisterNetworkInterface( IOService
* netif
)
222 // A network interface is typically named and registered
223 // with BSD after receiving a request from a user space
224 // "namer". However, for cases when the system needs to
225 // root from the network, this registration task must be
226 // done inside the kernel and completed before the root
227 // device is handed to BSD.
232 OSDictionary
* dict
= 0;
235 enum { kMaxPathLen
= 512 };
238 stack
= IOService::waitForService(
239 IOService::serviceMatching("IONetworkStack") );
240 if ( stack
== 0 ) break;
242 dict
= OSDictionary::withCapacity(3);
243 if ( dict
== 0 ) break;
245 zero
= OSNumber::withNumber((UInt64
) 0, 32);
246 if ( zero
== 0 ) break;
248 pathBuf
= (char *) IOMalloc( kMaxPathLen
);
249 if ( pathBuf
== 0 ) break;
252 if ( netif
->getPath( pathBuf
, &len
, gIOServicePlane
)
255 path
= OSString::withCStringNoCopy( pathBuf
);
256 if ( path
== 0 ) break;
258 dict
->setObject( "IOInterfaceUnit", zero
);
259 dict
->setObject( kIOPathMatchKey
, path
);
261 stack
->setProperties( dict
);
265 if ( zero
) zero
->release();
266 if ( path
) path
->release();
267 if ( dict
) dict
->release();
268 if ( pathBuf
) IOFree(pathBuf
, kMaxPathLen
);
270 return ( netif
->getProperty( kIOBSDNameKey
) != 0 );
273 OSDictionary
* IODiskMatching( const char * path
, char * buf
, int maxLen
)
283 // scan the tail of the path for "@unit:partition"
285 // Have to get the full path to the controller - an alias may
286 // tell us next to nothing, like "hd:8"
287 alias
= IORegistryEntry::dealiasPath( &path
, gIODTPlane
);
289 look
= path
+ strlen( path
);
291 while( look
!= path
) {
292 if( *(--look
) == c
) {
294 partition
= strtol( look
+ 1, 0, 0 );
296 } else if( c
== '@') {
297 unit
= strtol( look
+ 1, &comp
, 16 );
300 lun
= strtol( comp
+ 1, 0, 16 );
304 } else if( c
== '/') {
310 if( alias
&& (look
== path
)) {
312 look
= path
+ strlen( path
);
316 if( c
|| unit
== -1 || partition
== -1)
319 maxLen
-= strlen( "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
320 maxLen
-= ( alias
? strlen( alias
) : 0 ) + (look
- path
);
321 maxLen
-= strlen( "/@hhhhhhhh,hhhhhhhh:dddddddddd';}" );
324 sprintf( buf
, "{" kIOPathMatchKey
"='" kIODeviceTreePlane
":" );
325 comp
= buf
+ strlen( buf
);
328 strcpy( comp
, alias
);
329 comp
+= strlen( alias
);
332 if ( (look
- path
)) {
333 strncpy( comp
, path
, look
- path
);
339 sprintf ( comp
, "/@%lx,%lx:%ld';}", unit
, lun
, partition
);
343 sprintf( comp
, "/@%lx:%ld';}", unit
, partition
);
348 return( OSDynamicCast(OSDictionary
, OSUnserialize( buf
, 0 )) );
355 OSDictionary
* IOOFPathMatching( const char * path
, char * buf
, int maxLen
)
357 OSDictionary
* matching
;
362 /* need to look up path, get device type,
363 call matching help based on device type */
365 matching
= IODiskMatching( path
, buf
, maxLen
);
371 len
= strlen( kIODeviceTreePlane
":" );
376 strcpy( buf
, kIODeviceTreePlane
":" );
379 len
= strlen( path
);
383 strncpy( comp
, path
, len
);
386 matching
= OSDictionary::withCapacity( 1 );
390 str
= OSString::withCString( buf
);
393 matching
->setObject( kIOPathMatchKey
, str
);
406 IOService
* IOFindMatchingChild( IOService
* service
)
408 // find a matching child service
409 IOService
* child
= 0;
410 OSIterator
* iter
= service
->getClientIterator();
412 while( ( child
= (IOService
*) iter
->getNextObject() ) ) {
413 OSDictionary
* dict
= OSDictionary::withCapacity( 1 );
418 const OSSymbol
* str
= OSSymbol::withCString( "Apple_HFS" );
424 dict
->setObject( "Content", (OSObject
*)str
);
426 if ( child
->compareProperty( dict
, "Content" ) ) {
431 IOService
* subchild
= IOFindMatchingChild( child
);
442 static int didRam
= 0;
444 kern_return_t
IOFindBSDRoot( char * rootName
,
445 dev_t
* root
, u_int32_t
* oflags
)
449 IORegistryEntry
* regEntry
;
450 OSDictionary
* matching
= 0;
454 UInt32
*ramdParms
= 0;
458 bool findHFSChild
= false;
459 char * mediaProperty
= 0;
461 enum { kMaxPathBuf
= 512, kMaxBootVar
= 128 };
463 const char * look
= 0;
465 bool forceNet
= false;
466 bool debugInfoPrintedOnce
= false;
467 const char * uuidStr
= NULL
;
469 static int mountAttempts
= 0;
477 str
= (char *) IOMalloc( kMaxPathBuf
+ kMaxBootVar
);
479 return( kIOReturnNoMemory
);
480 rdBootVar
= str
+ kMaxPathBuf
;
482 if (!PE_parse_boot_argn("rd", rdBootVar
, kMaxBootVar
)
483 && !PE_parse_boot_argn("rootdev", rdBootVar
, kMaxBootVar
))
487 if( (regEntry
= IORegistryEntry::fromPath( "/chosen", gIODTPlane
))) {
488 data
= OSDynamicCast(OSData
, regEntry
->getProperty( "root-matching" ));
490 matching
= OSDynamicCast(OSDictionary
, OSUnserializeXML((char *)data
->getBytesNoCopy()));
496 data
= (OSData
*) regEntry
->getProperty( "boot-uuid" );
498 uuidStr
= (const char*)data
->getBytesNoCopy();
499 OSString
*uuidString
= OSString::withCString( uuidStr
);
501 // match the boot-args boot-uuid processing below
503 IOLog("rooting via boot-uuid from /chosen: %s\n", uuidStr
);
504 IOService::publishResource( "boot-uuid", uuidString
);
505 uuidString
->release();
506 matching
= IOUUIDMatching();
507 mediaProperty
= "boot-uuid-media";
515 // else try for an OF Path
516 data
= (OSData
*) regEntry
->getProperty( "rootpath" );
520 if( (regEntry
= IORegistryEntry::fromPath( "/options", gIODTPlane
))) {
521 data
= (OSData
*) regEntry
->getProperty( "boot-file" );
527 if( data
&& !uuidStr
)
528 look
= (const char *) data
->getBytesNoCopy();
530 if( rdBootVar
[0] == '*') {
531 look
= rdBootVar
+ 1;
534 if( (regEntry
= IORegistryEntry::fromPath( "/", gIODTPlane
))) {
535 forceNet
= (0 != regEntry
->getProperty( "net-boot" ));
543 // See if we have a RAMDisk property in /chosen/memory-map. If so, make it into a device.
544 // It will become /dev/mdx, where x is 0-f.
547 if(!didRam
) { /* Have we already build this ram disk? */
548 didRam
= 1; /* Remember we did this */
549 if((regEntry
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
))) { /* Find the map node */
550 data
= (OSData
*)regEntry
->getProperty("RAMDisk"); /* Find the ram disk, if there */
551 if(data
) { /* We found one */
553 ramdParms
= (UInt32
*)data
->getBytesNoCopy(); /* Point to the ram disk base and size */
554 (void)mdevadd(-1, ramdParms
[0] >> 12, ramdParms
[1] >> 12, 0); /* Initialize it and pass back the device number */
556 regEntry
->release(); /* Toss the entry */
561 // Now check if we are trying to root on a memory device
564 if((rdBootVar
[0] == 'm') && (rdBootVar
[1] == 'd') && (rdBootVar
[3] == 0)) {
565 dchar
= xchar
= rdBootVar
[2]; /* Get the actual device */
566 if((xchar
>= '0') && (xchar
<= '9')) xchar
= xchar
- '0'; /* If digit, convert */
568 xchar
= xchar
& ~' '; /* Fold to upper case */
569 if((xchar
>= 'A') && (xchar
<= 'F')) { /* Is this a valid digit? */
570 xchar
= (xchar
& 0xF) + 9; /* Convert the hex digit */
571 dchar
= dchar
| ' '; /* Fold to lower case */
573 else xchar
= -1; /* Show bogus */
575 if(xchar
>= 0) { /* Do we have a valid memory device name? */
576 *root
= mdevlookup(xchar
); /* Find the device number */
577 if(*root
>= 0) { /* Did we find one? */
579 rootName
[0] = 'm'; /* Build root name */
580 rootName
[1] = 'd'; /* Build root name */
581 rootName
[2] = dchar
; /* Build root name */
582 rootName
[3] = 0; /* Build root name */
583 IOLog("BSD root: %s, major %d, minor %d\n", rootName
, major(*root
), minor(*root
));
584 *oflags
= 0; /* Show that this is not network */
585 goto iofrootx
; /* Join common exit... */
587 panic("IOFindBSDRoot: specified root memory device, %s, has not been configured\n", rdBootVar
); /* Not there */
592 // from OpenFirmware path
593 IOLog("From path: \"%s\", ", look
);
596 if( forceNet
|| (0 == strncmp( look
, "enet", strlen( "enet" ))) ) {
597 matching
= IONetworkMatching( look
, str
, kMaxPathBuf
);
599 matching
= IODiskMatching( look
, str
, kMaxPathBuf
);
604 if( (!matching
) && rdBootVar
[0] ) {
610 if ( strncmp( look
, "en", strlen( "en" )) == 0 ) {
611 matching
= IONetworkNamePrefixMatching( "en" );
612 } else if ( strncmp( look
, "cdrom", strlen( "cdrom" )) == 0 ) {
613 matching
= IOCDMatching();
615 } else if ( strncmp( look
, "uuid", strlen( "uuid" )) == 0 ) {
617 OSString
*uuidString
;
619 uuid
= (char *)IOMalloc( kMaxBootVar
);
622 if (!PE_parse_boot_argn( "boot-uuid", uuid
, kMaxBootVar
)) {
623 panic( "rd=uuid but no boot-uuid=<value> specified" );
625 uuidString
= OSString::withCString( uuid
);
627 IOService::publishResource( "boot-uuid", uuidString
);
628 uuidString
->release();
629 IOLog( "\nWaiting for boot volume with UUID %s\n", uuid
);
630 matching
= IOUUIDMatching();
631 mediaProperty
= "boot-uuid-media";
633 IOFree( uuid
, kMaxBootVar
);
636 matching
= IOBSDNameMatching( look
);
642 // Match any HFS media
644 matching
= IOService::serviceMatching( "IOMedia" );
645 astring
= OSString::withCStringNoCopy("Apple_HFS");
647 matching
->setObject("Content", astring
);
652 if( true && matching
) {
653 OSSerialize
* s
= OSSerialize::withCapacity( 5 );
655 if( matching
->serialize( s
)) {
656 IOLog( "Waiting on %s\n", s
->text() );
662 t
.tv_sec
= ROOTDEVICETIMEOUT
;
665 service
= IOService::waitForService( matching
, &t
);
666 if( (!service
) || (mountAttempts
== 10)) {
667 PE_display_icon( 0, "noroot");
668 IOLog( "Still waiting for root device\n" );
670 if( !debugInfoPrintedOnce
) {
671 debugInfoPrintedOnce
= true;
672 if( gIOKitDebug
& kIOLogDTree
) {
673 IOLog("\nDT plane:\n");
674 IOPrintPlane( gIODTPlane
);
676 if( gIOKitDebug
& kIOLogServiceTree
) {
677 IOLog("\nService plane:\n");
678 IOPrintPlane( gIOServicePlane
);
680 if( gIOKitDebug
& kIOLogMemory
)
687 if ( service
&& findHFSChild
) {
689 // wait for children services to finish registering
691 t
.tv_sec
= ROOTDEVICETIMEOUT
;
693 if ( service
->waitQuiet( &t
) == kIOReturnSuccess
) {
696 IOLog( "Waiting for child registration\n" );
699 // look for a subservice with an Apple_HFS child
700 IOService
* subservice
= IOFindMatchingChild( service
);
701 if ( subservice
) service
= subservice
;
702 } else if ( service
&& mediaProperty
) {
703 service
= (IOService
*)service
->getProperty(mediaProperty
);
709 // If the IOService we matched to is a subclass of IONetworkInterface,
710 // then make sure it has been registered with BSD and has a BSD name
714 && service
->metaCast( "IONetworkInterface" )
715 && !IORegisterNetworkInterface( service
) )
723 service
->getPath( str
, &len
, gIOServicePlane
);
724 IOLog( "Got boot device = %s\n", str
);
726 iostr
= (OSString
*) service
->getProperty( kIOBSDNameKey
);
728 strcpy( rootName
, iostr
->getCStringNoCopy() );
729 off
= (OSNumber
*) service
->getProperty( kIOBSDMajorKey
);
731 mjr
= off
->unsigned32BitValue();
732 off
= (OSNumber
*) service
->getProperty( kIOBSDMinorKey
);
734 mnr
= off
->unsigned32BitValue();
736 if( service
->metaCast( "IONetworkInterface" ))
741 IOLog( "Wait for root failed\n" );
742 strcpy( rootName
, "en0");
746 IOLog( "BSD root: %s", rootName
);
748 IOLog(", major %d, minor %d\n", mjr
, mnr
);
752 *root
= makedev( mjr
, mnr
);
755 IOFree( str
, kMaxPathBuf
+ kMaxBootVar
);
758 if( (gIOKitDebug
& (kIOLogDTree
| kIOLogServiceTree
| kIOLogMemory
)) && !debugInfoPrintedOnce
) {
760 IOService::getPlatform()->waitQuiet();
761 if( gIOKitDebug
& kIOLogDTree
) {
762 IOLog("\nDT plane:\n");
763 IOPrintPlane( gIODTPlane
);
765 if( gIOKitDebug
& kIOLogServiceTree
) {
766 IOLog("\nService plane:\n");
767 IOPrintPlane( gIOServicePlane
);
769 if( gIOKitDebug
& kIOLogMemory
)
773 return( kIOReturnSuccess
);
776 void IOSecureBSDRoot(const char * rootName
)
779 IOPlatformExpert
*pe
;
780 const OSSymbol
*functionName
= OSSymbol::withCStringNoCopy("SecureRootName");
782 while ((pe
= IOService::getPlatform()) == 0) IOSleep(1 * 1000);
784 pe
->callPlatformFunction(functionName
, false, (void *)rootName
, (void *)0, (void *)0, (void *)0);
786 functionName
->release();
791 IOBSDRegistryEntryForDeviceTree(char * path
)
793 return (IORegistryEntry::fromPath(path
, gIODTPlane
));
797 IOBSDRegistryEntryRelease(void * entry
)
799 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
807 IOBSDRegistryEntryGetData(void * entry
, char * property_name
,
811 IORegistryEntry
* regEntry
= (IORegistryEntry
*)entry
;
813 data
= (OSData
*) regEntry
->getProperty(property_name
);
815 *packet_length
= data
->getLength();
816 return (data
->getBytesNoCopy());
821 kern_return_t
IOBSDGetPlatformUUID( uuid_t uuid
, mach_timespec_t timeout
)
823 IOService
* resources
;
826 resources
= IOService::waitForService( IOService::resourceMatching( kIOPlatformUUIDKey
), &timeout
);
827 if ( resources
== 0 ) return KERN_OPERATION_TIMED_OUT
;
829 string
= ( OSString
* ) IOService::getPlatform( )->getProvider( )->getProperty( kIOPlatformUUIDKey
);
830 if ( string
== 0 ) return KERN_NOT_SUPPORTED
;
832 uuid_parse( string
->getCStringNoCopy( ), uuid
);