home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / os / machsun3.tz / machsun3 / mk.kernel / sun3 / setroot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-15  |  5.4 KB  |  239 lines

  1. /* 
  2.  * Mach Operating System
  3.  * Copyright (c) 1992 Carnegie Mellon University
  4.  * All Rights Reserved.
  5.  * 
  6.  * Permission to use, copy, modify and distribute this software and its
  7.  * documentation is hereby granted, provided that both the copyright
  8.  * notice and this permission notice appear in all copies of the
  9.  * software, derivative works or modified versions, and any portions
  10.  * thereof, and that both notices appear in supporting documentation.
  11.  * 
  12.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  13.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  14.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  15.  * 
  16.  * Carnegie Mellon requests users of this software to return to
  17.  * 
  18.  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  19.  *  School of Computer Science
  20.  *  Carnegie Mellon University
  21.  *  Pittsburgh PA 15213-3890
  22.  * 
  23.  * any improvements or extensions that they make and grant Carnegie Mellon 
  24.  * the rights to redistribute these changes.
  25.  */
  26. /*
  27.  * HISTORY
  28.  * $Log:    setroot.c,v $
  29.  * Revision 2.9  92/04/01  19:36:20  rpd
  30.  *     Changed to use machine-independent safe_gets.
  31.  *     [92/03/31            rpd]
  32.  * 
  33.  * Revision 2.8  91/07/31  18:14:33  dbg
  34.  *     Move root_name string out of text segment for ANSI C.
  35.  *     [91/07/23            dbg]
  36.  * 
  37.  * Revision 2.7  91/06/19  15:47:14  rvb
  38.  *     gets() must be static to not conflict with kern/printf:gets()
  39.  * 
  40.  * Revision 2.6  90/08/27  22:12:02  dbg
  41.  *     Don't need to pass NOSYNC to boot().
  42.  *     [90/08/14            dbg]
  43.  * 
  44.  * Revision 2.5  89/11/29  14:16:39  af
  45.  *     Made set_root a pointer rather than an array so that it can be 
  46.  *     changed more easily.
  47.  *     [89/10/28  16:25:49  af]
  48.  * 
  49.  * Revision 2.4  89/09/08  11:27:34  dbg
  50.  *     Turn on console input around gets().
  51.  *     [89/09/06            dbg]
  52.  * 
  53.  * Revision 2.3  89/08/04  13:15:46  rwd
  54.  *     Fixed history
  55.  * 
  56.  * 
  57.  * Revision 2.2  89/08/04  13:14:03  rwd
  58.  *     removed ip device
  59.  * 
  60.  */
  61. #include <sys/types.h>
  62. #include <sys/reboot.h>
  63. #include <machine/cpu.h>
  64. #include <sundev/mbvar.h>
  65. #include <mon/sunromvec.h>
  66. #include <device/conf.h>
  67. #include <device/io_req.h>
  68.  
  69. /*
  70.  * Generic configuration;  all in one
  71.  */
  72. dev_t    rootdev;
  73.  
  74. static char devname[][2] = {
  75.     0,0,        /* 0 = ip */
  76.     0,0,        /* 1 = tm */
  77.     0,0,        /* 2 = ar */
  78.     0,0,        /* 3 = xy */
  79.     0,0,        /* 4 = sw */
  80.     0,0,        /* 5 = nd */
  81.     0,0,
  82.     's','d',    /* 7 = sd */
  83.     0,0,        /* 8 = xt */
  84.     0,0        /* 9 = sf */
  85. };
  86.  
  87. #include <xy.h>
  88. #if    NXY > 0
  89. extern    struct mb_driver xycdriver;
  90. #endif
  91.  
  92. #include <sc.h>
  93. #if    NSC > 0
  94. extern    struct mb_driver scdriver;
  95. #endif
  96.  
  97. #include <si.h>
  98. #if    NSI > 0
  99. extern    struct mb_driver sidriver;
  100. #endif
  101.  
  102. struct    genericconf {
  103.     char    *gc_name;
  104.     struct mb_driver *gc_driver;
  105.     dev_t    gc_root;
  106. } genericconf[] = {
  107. #if    NXY > 0
  108.     "xy", &xycdriver, makedev(3, 0),
  109. #endif
  110. #if    NSC > 0
  111.     "sd", &scdriver, makedev(7, 0),
  112. #endif
  113. #if    NSI > 0
  114.     "sd", &sidriver, makedev(7, 0),
  115. #endif
  116. #define NND    0
  117. #if    NND > 0
  118.     "nd", 0, makedev(5, 0),
  119. #endif
  120.    0,
  121. };
  122.  
  123. #define    PARTITIONMASK    0x7
  124. #define    PARTITIONSHIFT    3
  125.  
  126. setconf()
  127. {
  128.     register struct mb_device *md;
  129.     register struct genericconf *gc;
  130.     int unit,swaponroot = 0;
  131.  
  132.     if (boothowto & RB_ASKNAME) {
  133.         char name[128];
  134. retry:
  135.         printf("root device? ");
  136.         cnpollc(TRUE);
  137.         safe_gets(name, sizeof name);
  138.         cnpollc(FALSE);
  139.         for (gc = genericconf; gc->gc_name; gc++)
  140.             if (gc->gc_name[0] == name[0] &&
  141.                 gc->gc_name[1] == name[1])
  142.                 goto gotit;
  143.         goto bad;
  144. gotit:
  145.         if (name[3] == '*') {
  146.             name[3] = name[4];
  147.             swaponroot++;
  148.         }
  149.         if (name[2] >= '0' && name[2] <= '7' && name[3] == 0) {
  150.             unit = name[2] - '0';
  151.             goto found;
  152.         }
  153.         printf("bad/missing unit number\n");
  154. bad:
  155.         printf("use one of: ");
  156.         for (gc = genericconf; gc->gc_name; gc++)
  157.             printf("%s%%d ", gc->gc_name);
  158.         printf("\n");
  159.         goto retry;
  160.     }
  161.     unit = 0;
  162.     for (gc = genericconf; gc->gc_name; gc++) {
  163.         register struct bootparam *bp = (*sunromp->v_bootparam);
  164.  
  165.         if (!((*gc->gc_name == bp->bp_dev[0]) &&
  166.             (*(gc->gc_name+1) == bp->bp_dev[1])))
  167.             continue;
  168.         if (chkroot(gc)) {
  169.             printf("root on %s0\n", gc->gc_name);
  170.             goto found;
  171.         } 
  172.     } 
  173.  
  174.     for (gc = genericconf; gc->gc_name; gc++) {
  175.         if (gc->gc_driver == 0) {
  176.             /* nd is last entry in table, just try it */
  177.             printf("root on %s0\n", gc->gc_name);
  178.             goto found;
  179.         }
  180.         if (chkroot(gc)) {
  181.             printf("root on %s0\n", gc->gc_name);
  182.             goto found;
  183.         } 
  184.     }
  185.     printf("no suitable root\n");
  186.     boot(0, RB_HALT);
  187.     /* NOTREACHED */
  188.  
  189. found:
  190.     gc->gc_root = makedev(major(gc->gc_root), unit*8);
  191.     rootdev = gc->gc_root;
  192.     if (swaponroot)
  193.         rootdev = makedev(major(rootdev), minor(rootdev)+1);
  194.  
  195. }
  196. chkroot(gc)
  197.     register struct genericconf *gc;
  198. {       
  199.     register struct mb_device *md;
  200.     dev_ops_t ops;
  201.     int unit;
  202.     
  203.     for (md = mbdinit; md->md_driver; md++) {
  204.         if (md->md_alive == 0)
  205.             continue;
  206.         if (md->md_unit == 0 && md->md_driver == gc->gc_driver) {
  207.             if (dev_name_lookup(gc->gc_name,&ops,&unit)) {
  208.                 if ((*(ops->d_open))(unit, IO_READ) == 0) {
  209.                 (void)(*(ops->d_close))(unit, IO_READ);
  210.                 return (1);
  211.                 }
  212.             }
  213.         }
  214.     }
  215.     return (0);
  216.  
  217. char    root_name_string[16] = "\0\0\0\0\0\0\0";    /* at least ddNNp */
  218. char    *root_name = root_name_string;
  219.  
  220. get_root_device()
  221. {
  222.     register int    root_major, root_minor;
  223.  
  224.     setconf();
  225.  
  226.     /*
  227.      * Now, translate device name back into string.
  228.      */
  229.     root_major = major(rootdev);
  230.     root_minor = minor(rootdev);
  231.  
  232.     root_name[0] = devname[root_major][0];
  233.     root_name[1] = devname[root_major][1];
  234.     root_name[2] = '0' + (root_minor >> PARTITIONSHIFT);
  235.     root_name[3] = 'a' + (root_minor & PARTITIONMASK);
  236.     root_name[4] = '\0';
  237. }
  238.