]> git.saurik.com Git - apple/xnu.git/blob - iokit/bsddev/IOKitBSDInit.cpp
1d9f9af62d326b9cf66f9b73ea697f0fa09c0f9b
[apple/xnu.git] / iokit / bsddev / IOKitBSDInit.cpp
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
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>
28
29 extern "C" {
30
31 #include <pexpert/pexpert.h>
32 #include <kern/clock.h>
33
34 // how long to wait for matching root device, secs
35 #define ROOTDEVICETIMEOUT 60
36
37 extern dev_t mdevadd(int devid, ppnum_t base, unsigned int size, int phys);
38 extern dev_t mdevlookup(int devid);
39
40 kern_return_t
41 IOKitBSDInit( void )
42 {
43 IOService::publishResource("IOBSD");
44
45 return( kIOReturnSuccess );
46 }
47
48 OSDictionary * IOBSDNameMatching( const char * name )
49 {
50 OSDictionary * dict;
51 const OSSymbol * str = 0;
52
53 do {
54
55 dict = IOService::serviceMatching( gIOServiceKey );
56 if( !dict)
57 continue;
58 str = OSSymbol::withCString( name );
59 if( !str)
60 continue;
61 dict->setObject( kIOBSDNameKey, (OSObject *) str );
62 str->release();
63
64 return( dict );
65
66 } while( false );
67
68 if( dict)
69 dict->release();
70 if( str)
71 str->release();
72
73 return( 0 );
74 }
75
76 OSDictionary * IOUUIDMatching( void )
77 {
78 return IOService::resourceMatching( "boot-uuid-media" );
79 }
80
81
82 OSDictionary * IOCDMatching( void )
83 {
84 OSDictionary * dict;
85 const OSSymbol * str;
86
87 dict = IOService::serviceMatching( "IOMedia" );
88 if( dict == 0 ) {
89 IOLog("Unable to find IOMedia\n");
90 return 0;
91 }
92
93 str = OSSymbol::withCString( "CD_ROM_Mode_1" );
94 if( str == 0 ) {
95 dict->release();
96 return 0;
97 }
98
99 dict->setObject( "Content Hint", (OSObject *)str );
100 str->release();
101 return( dict );
102 }
103
104 OSDictionary * IONetworkMatching( const char * path,
105 char * buf, int maxLen )
106 {
107 OSDictionary * matching = 0;
108 OSDictionary * dict;
109 OSString * str;
110 char * comp;
111 const char * skip;
112 int len;
113
114 do {
115
116 len = strlen( kIODeviceTreePlane ":" );
117 maxLen -= len;
118 if( maxLen < 0)
119 continue;
120
121 strcpy( buf, kIODeviceTreePlane ":" );
122 comp = buf + len;
123
124 // remove parameters following ':' from the path
125 skip = strchr( path, ':');
126 if( !skip)
127 continue;
128
129 len = skip - path;
130 maxLen -= len;
131 if( maxLen < 0)
132 continue;
133 strncpy( comp, path, len );
134 comp[ len ] = 0;
135
136 matching = IOService::serviceMatching( "IONetworkInterface" );
137 if( !matching)
138 continue;
139 dict = IOService::addLocation( matching );
140 if( !dict)
141 continue;
142
143 str = OSString::withCString( buf );
144 if( !str)
145 continue;
146 dict->setObject( kIOPathMatchKey, str );
147 str->release();
148
149 return( matching );
150
151 } while( false );
152
153 if( matching)
154 matching->release();
155
156 return( 0 );
157 }
158
159 OSDictionary * IONetworkNamePrefixMatching( const char * prefix )
160 {
161 OSDictionary * matching;
162 OSDictionary * propDict = 0;
163 const OSSymbol * str = 0;
164
165 do {
166 matching = IOService::serviceMatching( "IONetworkInterface" );
167 if ( matching == 0 )
168 continue;
169
170 propDict = OSDictionary::withCapacity(1);
171 if ( propDict == 0 )
172 continue;
173
174 str = OSSymbol::withCString( prefix );
175 if ( str == 0 )
176 continue;
177
178 propDict->setObject( "IOInterfaceNamePrefix", (OSObject *) str );
179 str->release();
180 str = 0;
181
182 if ( matching->setObject( gIOPropertyMatchKey,
183 (OSObject *) propDict ) != true )
184 continue;
185
186 propDict->release();
187 propDict = 0;
188
189 return( matching );
190
191 } while ( false );
192
193 if ( matching ) matching->release();
194 if ( propDict ) propDict->release();
195 if ( str ) str->release();
196
197 return( 0 );
198 }
199
200 static bool IORegisterNetworkInterface( IOService * netif )
201 {
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.
208
209 IOService * stack;
210 OSNumber * zero = 0;
211 OSString * path = 0;
212 OSDictionary * dict = 0;
213 char * pathBuf = 0;
214 int len;
215 enum { kMaxPathLen = 512 };
216
217 do {
218 stack = IOService::waitForService(
219 IOService::serviceMatching("IONetworkStack") );
220 if ( stack == 0 ) break;
221
222 dict = OSDictionary::withCapacity(3);
223 if ( dict == 0 ) break;
224
225 zero = OSNumber::withNumber((UInt64) 0, 32);
226 if ( zero == 0 ) break;
227
228 pathBuf = (char *) IOMalloc( kMaxPathLen );
229 if ( pathBuf == 0 ) break;
230
231 len = kMaxPathLen;
232 if ( netif->getPath( pathBuf, &len, gIOServicePlane )
233 == false ) break;
234
235 path = OSString::withCStringNoCopy( pathBuf );
236 if ( path == 0 ) break;
237
238 dict->setObject( "IOInterfaceUnit", zero );
239 dict->setObject( kIOPathMatchKey, path );
240
241 stack->setProperties( dict );
242 }
243 while ( false );
244
245 if ( zero ) zero->release();
246 if ( path ) path->release();
247 if ( dict ) dict->release();
248 if ( pathBuf ) IOFree(pathBuf, kMaxPathLen);
249
250 return ( netif->getProperty( kIOBSDNameKey ) != 0 );
251 }
252
253 OSDictionary * IODiskMatching( const char * path, char * buf, int maxLen )
254 {
255 const char * look;
256 const char * alias;
257 char * comp;
258 long unit = -1;
259 long partition = -1;
260 long lun = -1;
261 char c;
262
263 // scan the tail of the path for "@unit:partition"
264 do {
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 );
268
269 look = path + strlen( path);
270 c = ':';
271 while( look != path) {
272 if( *(--look) == c) {
273 if( c == ':') {
274 partition = strtol( look + 1, 0, 0 );
275 c = '@';
276 } else if( c == '@') {
277 unit = strtol( look + 1, &comp, 16 );
278
279 if( *comp == ',') {
280 lun = strtol( comp + 1, 0, 16 );
281 }
282
283 c = '/';
284 } else if( c == '/') {
285 c = 0;
286 break;
287 }
288 }
289
290 if( alias && (look == path)) {
291 path = alias;
292 look = path + strlen( path);
293 alias = 0;
294 }
295 }
296 if( c || unit == -1 || partition == -1)
297 continue;
298
299 maxLen -= strlen( "{" kIOPathMatchKey "='" kIODeviceTreePlane ":" );
300 maxLen -= ( alias ? strlen( alias ) : 0 ) + (look - path);
301 maxLen -= strlen( "/@hhhhhhhh,hhhhhhhh:dddddddddd';}" );
302
303 if( maxLen > 0) {
304 sprintf( buf, "{" kIOPathMatchKey "='" kIODeviceTreePlane ":" );
305 comp = buf + strlen( buf );
306
307 if( alias) {
308 strcpy( comp, alias );
309 comp += strlen( alias );
310 }
311
312 if ( (look - path)) {
313 strncpy( comp, path, look - path);
314 comp += look - path;
315 }
316
317 if ( lun != -1 )
318 {
319 sprintf ( comp, "/@%lx,%lx:%ld';}", unit, lun, partition );
320 }
321 else
322 {
323 sprintf( comp, "/@%lx:%ld';}", unit, partition );
324 }
325 } else
326 continue;
327
328 return( OSDynamicCast(OSDictionary, OSUnserialize( buf, 0 )) );
329
330 } while( false );
331
332 return( 0 );
333 }
334
335 OSDictionary * IOOFPathMatching( const char * path, char * buf, int maxLen )
336 {
337 OSDictionary * matching;
338 OSString * str;
339 char * comp;
340 int len;
341
342 /* need to look up path, get device type,
343 call matching help based on device type */
344
345 matching = IODiskMatching( path, buf, maxLen );
346 if( matching)
347 return( matching );
348
349 do {
350
351 len = strlen( kIODeviceTreePlane ":" );
352 maxLen -= len;
353 if( maxLen < 0)
354 continue;
355
356 strcpy( buf, kIODeviceTreePlane ":" );
357 comp = buf + len;
358
359 len = strlen( path );
360 maxLen -= len;
361 if( maxLen < 0)
362 continue;
363 strncpy( comp, path, len );
364 comp[ len ] = 0;
365
366 matching = OSDictionary::withCapacity( 1 );
367 if( !matching)
368 continue;
369
370 str = OSString::withCString( buf );
371 if( !str)
372 continue;
373 matching->setObject( kIOPathMatchKey, str );
374 str->release();
375
376 return( matching );
377
378 } while( false );
379
380 if( matching)
381 matching->release();
382
383 return( 0 );
384 }
385
386 IOService * IOFindMatchingChild( IOService * service )
387 {
388 // find a matching child service
389 IOService * child = 0;
390 OSIterator * iter = service->getClientIterator();
391 if ( iter ) {
392 while( ( child = (IOService *) iter->getNextObject() ) ) {
393 OSDictionary * dict = OSDictionary::withCapacity( 1 );
394 if( dict == 0 ) {
395 iter->release();
396 return 0;
397 }
398 const OSSymbol * str = OSSymbol::withCString( "Apple_HFS" );
399 if( str == 0 ) {
400 dict->release();
401 iter->release();
402 return 0;
403 }
404 dict->setObject( "Content", (OSObject *)str );
405 str->release();
406 if ( child->compareProperty( dict, "Content" ) ) {
407 dict->release();
408 break;
409 }
410 dict->release();
411 IOService * subchild = IOFindMatchingChild( child );
412 if ( subchild ) {
413 child = subchild;
414 break;
415 }
416 }
417 iter->release();
418 }
419 return child;
420 }
421
422 static int didRam = 0;
423
424 kern_return_t IOFindBSDRoot( char * rootName,
425 dev_t * root, u_int32_t * oflags )
426 {
427 mach_timespec_t t;
428 IOService * service;
429 IORegistryEntry * regEntry;
430 OSDictionary * matching = 0;
431 OSString * iostr;
432 OSNumber * off;
433 OSData * data = 0;
434 UInt32 *ramdParms = 0;
435
436 UInt32 flags = 0;
437 int minor, major;
438 bool findHFSChild = false;
439 char * mediaProperty = 0;
440 char * rdBootVar;
441 enum { kMaxPathBuf = 512, kMaxBootVar = 128 };
442 char * str;
443 const char * look = 0;
444 int len;
445 bool forceNet = false;
446 bool debugInfoPrintedOnce = false;
447 const char * uuidStr = NULL;
448
449 static int mountAttempts = 0;
450
451 int xchar, dchar;
452
453
454 if( mountAttempts++)
455 IOSleep( 5 * 1000 );
456
457 str = (char *) IOMalloc( kMaxPathBuf + kMaxBootVar );
458 if( !str)
459 return( kIOReturnNoMemory );
460 rdBootVar = str + kMaxPathBuf;
461
462 if (!PE_parse_boot_arg("rd", rdBootVar )
463 && !PE_parse_boot_arg("rootdev", rdBootVar ))
464 rdBootVar[0] = 0;
465
466 do {
467 if( (regEntry = IORegistryEntry::fromPath( "/chosen", gIODTPlane ))) {
468 data = OSDynamicCast(OSData, regEntry->getProperty( "root-matching" ));
469 if (data) {
470 matching = OSDynamicCast(OSDictionary, OSUnserializeXML((char *)data->getBytesNoCopy()));
471 if (matching) {
472 continue;
473 }
474 }
475
476 data = (OSData *) regEntry->getProperty( "boot-uuid" );
477 if( data) {
478 uuidStr = (const char*)data->getBytesNoCopy();
479 OSString *uuidString = OSString::withCString( uuidStr );
480
481 // match the boot-args boot-uuid processing below
482 if( uuidString) {
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";
488 regEntry->release();
489 continue;
490 } else {
491 uuidStr = NULL;
492 }
493 }
494
495 // else try for an OF Path
496 data = (OSData *) regEntry->getProperty( "rootpath" );
497 regEntry->release();
498 if( data) continue;
499 }
500 if( (regEntry = IORegistryEntry::fromPath( "/options", gIODTPlane ))) {
501 data = (OSData *) regEntry->getProperty( "boot-file" );
502 regEntry->release();
503 if( data) continue;
504 }
505 } while( false );
506
507 if( data && !uuidStr)
508 look = (const char *) data->getBytesNoCopy();
509
510 if( rdBootVar[0] == '*') {
511 look = rdBootVar + 1;
512 forceNet = false;
513 } else {
514 if( (regEntry = IORegistryEntry::fromPath( "/", gIODTPlane ))) {
515 forceNet = (0 != regEntry->getProperty( "net-boot" ));
516 regEntry->release();
517 }
518 }
519
520
521
522 //
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.
525 //
526
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 */
532
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 */
535 }
536 regEntry->release(); /* Toss the entry */
537 }
538 }
539
540 //
541 // Now check if we are trying to root on a memory device
542 //
543
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 */
547 else {
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 */
552 }
553 else xchar = -1; /* Show bogus */
554 }
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? */
558
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... */
566 }
567 panic("IOFindBSDRoot: specified root memory device, %s, has not been configured\n", rdBootVar); /* Not there */
568 }
569 }
570
571 if( look) {
572 // from OpenFirmware path
573 IOLog("From path: \"%s\", ", look);
574
575 if (!matching) {
576 if( forceNet || (0 == strncmp( look, "enet", strlen( "enet" ))) ) {
577 matching = IONetworkMatching( look, str, kMaxPathBuf );
578 } else {
579 matching = IODiskMatching( look, str, kMaxPathBuf );
580 }
581 }
582 }
583
584 if( (!matching) && rdBootVar[0] ) {
585 // by BSD name
586 look = rdBootVar;
587 if( look[0] == '*')
588 look++;
589
590 if ( strncmp( look, "en", strlen( "en" )) == 0 ) {
591 matching = IONetworkNamePrefixMatching( "en" );
592 } else if ( strncmp( look, "cdrom", strlen( "cdrom" )) == 0 ) {
593 matching = IOCDMatching();
594 findHFSChild = true;
595 } else if ( strncmp( look, "uuid", strlen( "uuid" )) == 0 ) {
596 char *uuid;
597 OSString *uuidString;
598
599 uuid = (char *)IOMalloc( kMaxBootVar );
600
601 if ( uuid ) {
602 if (!PE_parse_boot_arg( "boot-uuid", uuid )) {
603 panic( "rd=uuid but no boot-uuid=<value> specified" );
604 }
605 uuidString = OSString::withCString( uuid );
606 if ( uuidString ) {
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";
612 }
613 IOFree( uuid, kMaxBootVar );
614 }
615 } else {
616 matching = IOBSDNameMatching( look );
617 }
618 }
619
620 if( !matching) {
621 OSString * astring;
622 // any HFS
623 matching = IOService::serviceMatching( "IOMedia" );
624 astring = OSString::withCStringNoCopy("Apple_HFS");
625 if ( astring ) {
626 matching->setObject("Content", astring);
627 astring->release();
628 }
629 }
630
631 if( true && matching) {
632 OSSerialize * s = OSSerialize::withCapacity( 5 );
633
634 if( matching->serialize( s )) {
635 IOLog( "Waiting on %s\n", s->text() );
636 s->release();
637 }
638 }
639
640 do {
641 t.tv_sec = ROOTDEVICETIMEOUT;
642 t.tv_nsec = 0;
643 matching->retain();
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" );
648
649 if( !debugInfoPrintedOnce) {
650 debugInfoPrintedOnce = true;
651 if( gIOKitDebug & kIOLogDTree) {
652 IOLog("\nDT plane:\n");
653 IOPrintPlane( gIODTPlane );
654 }
655 if( gIOKitDebug & kIOLogServiceTree) {
656 IOLog("\nService plane:\n");
657 IOPrintPlane( gIOServicePlane );
658 }
659 if( gIOKitDebug & kIOLogMemory)
660 IOPrintMemory();
661 }
662 }
663 } while( !service);
664 matching->release();
665
666 if ( service && findHFSChild ) {
667 bool waiting = true;
668 // wait for children services to finish registering
669 while ( waiting ) {
670 t.tv_sec = ROOTDEVICETIMEOUT;
671 t.tv_nsec = 0;
672 if ( service->waitQuiet( &t ) == kIOReturnSuccess ) {
673 waiting = false;
674 } else {
675 IOLog( "Waiting for child registration\n" );
676 }
677 }
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);
683 }
684
685 major = 0;
686 minor = 0;
687
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
690 // assigned.
691
692 if ( service
693 && service->metaCast( "IONetworkInterface" )
694 && !IORegisterNetworkInterface( service ) )
695 {
696 service = 0;
697 }
698
699 if( service) {
700
701 len = kMaxPathBuf;
702 service->getPath( str, &len, gIOServicePlane );
703 IOLog( "Got boot device = %s\n", str );
704
705 iostr = (OSString *) service->getProperty( kIOBSDNameKey );
706 if( iostr)
707 strcpy( rootName, iostr->getCStringNoCopy() );
708 off = (OSNumber *) service->getProperty( kIOBSDMajorKey );
709 if( off)
710 major = off->unsigned32BitValue();
711 off = (OSNumber *) service->getProperty( kIOBSDMinorKey );
712 if( off)
713 minor = off->unsigned32BitValue();
714
715 if( service->metaCast( "IONetworkInterface" ))
716 flags |= 1;
717
718 } else {
719
720 IOLog( "Wait for root failed\n" );
721 strcpy( rootName, "en0");
722 flags |= 1;
723 }
724
725 IOLog( "BSD root: %s", rootName );
726 if( major)
727 IOLog(", major %d, minor %d\n", major, minor );
728 else
729 IOLog("\n");
730
731 *root = makedev( major, minor );
732 *oflags = flags;
733
734 IOFree( str, kMaxPathBuf + kMaxBootVar );
735
736 iofrootx:
737 if( (gIOKitDebug & (kIOLogDTree | kIOLogServiceTree | kIOLogMemory)) && !debugInfoPrintedOnce) {
738
739 IOService::getPlatform()->waitQuiet();
740 if( gIOKitDebug & kIOLogDTree) {
741 IOLog("\nDT plane:\n");
742 IOPrintPlane( gIODTPlane );
743 }
744 if( gIOKitDebug & kIOLogServiceTree) {
745 IOLog("\nService plane:\n");
746 IOPrintPlane( gIOServicePlane );
747 }
748 if( gIOKitDebug & kIOLogMemory)
749 IOPrintMemory();
750 }
751
752 return( kIOReturnSuccess );
753 }
754
755 void *
756 IOBSDRegistryEntryForDeviceTree(char * path)
757 {
758 return (IORegistryEntry::fromPath(path, gIODTPlane));
759 }
760
761 void
762 IOBSDRegistryEntryRelease(void * entry)
763 {
764 IORegistryEntry * regEntry = (IORegistryEntry *)entry;
765
766 if (regEntry)
767 regEntry->release();
768 return;
769 }
770
771 const void *
772 IOBSDRegistryEntryGetData(void * entry, char * property_name,
773 int * packet_length)
774 {
775 OSData * data;
776 IORegistryEntry * regEntry = (IORegistryEntry *)entry;
777
778 data = (OSData *) regEntry->getProperty(property_name);
779 if (data) {
780 *packet_length = data->getLength();
781 return (data->getBytesNoCopy());
782 }
783 return (NULL);
784 }
785
786 } /* extern "C" */