home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / sys / hp300 / stand / boot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-05  |  6.5 KB  |  241 lines

  1. /*-
  2.  * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  *
  33.  *    @(#)boot.c    7.4 (Berkeley) 5/5/91
  34.  */
  35.  
  36. #include <sys/param.h>
  37. #include <sys/reboot.h>
  38. #include <a.out.h>
  39. #include "saio.h"
  40.  
  41. #ifndef INSECURE
  42. #include "sys/stat.h"
  43. struct stat sb;
  44. #endif
  45.                     /* XXX -- see sys/reboot.h */
  46. #define B_MAKEDEV(a,u,p,t) \
  47.     (((a) << B_ADAPTORSHIFT) | ((u) << B_UNITSHIFT) | \
  48.      ((p) << B_PARTITIONSHIFT) | ((t) << B_TYPESHIFT))
  49.  
  50. /*
  51.  * Boot program... arguments in `devtype' and `howto' determine
  52.  * whether boot stops to ask for system name and which device
  53.  * boot comes from.
  54.  */
  55.  
  56. /* Types in `devtype' specifying major device */
  57. char    devname[][2] = {
  58.     '\0', '\0',    /* 0 = ct */
  59.     '\0', '\0',    /* 1 = fd */
  60.      'r',  'd',    /* 2 = rd */
  61.     '\0', '\0',    /* 3 = sw */
  62.      's',  'd',    /* 4 = sd */
  63. };
  64. #define    MAXTYPE    (sizeof(devname) / sizeof(devname[0]))
  65.  
  66. char line[100];
  67.  
  68. int    retry = 0;
  69. extern    char *lowram;
  70. extern    int noconsole;
  71. extern    int howto, devtype;
  72.  
  73. #define    MSUS (0xfffffedc)
  74.  
  75. char rom2mdev[] = {
  76.     0,    /*  0 - none */
  77.     0,    /*  1 - none */
  78.     0,    /*  2 - none */
  79.     0,    /*  3 - none */
  80.     0,    /*  4 - none */
  81.     0,    /*  5 - none */
  82.     0,    /*  6 - none */
  83.     0,    /*  7 - none */
  84.     0,    /*  8 - none */
  85.     0,    /*  9 - none */
  86.     0,    /* 10 - none */
  87.     0,    /* 11 - none */
  88.     0,    /* 12 - none */
  89.     0,    /* 13 - none */
  90.     4,    /* 14 - SCSI disk */
  91.     0,    /* 15 - none */
  92.     2,    /* 16 - CS/80 device on HPIB */
  93.     2,    /* 17 - CS/80 device on HPIB */
  94.     0,    /* 18 - none */
  95.     0,    /* 19 - none */
  96.     0,    /* 20 - none */
  97.     0,    /* 21 - none */
  98.     0,    /* 22 - none */
  99.     0,    /* 23 - none */
  100.     0,    /* 24 - none */
  101.     0,    /* 25 - none */
  102.     0,    /* 26 - none */
  103.     0,    /* 27 - none */
  104.     0,    /* 28 - none */
  105.     0,    /* 29 - none */
  106.     0,    /* 30 - none */
  107.     0,    /* 31 - none */
  108. };
  109.  
  110. main()
  111. {
  112.     register type, part, unit, io;
  113.     register char *cp;
  114.  
  115.     printf("\nBoot\n");
  116. #ifdef JUSTASK
  117.     howto = RB_ASKNAME|RB_SINGLE;
  118. #else
  119.     type = (devtype >> B_TYPESHIFT) & B_TYPEMASK;
  120.     unit = (devtype >> B_UNITSHIFT) & B_UNITMASK;
  121.     unit += 8 * ((devtype >> B_ADAPTORSHIFT) & B_ADAPTORMASK);
  122.     part = (devtype >> B_PARTITIONSHIFT) & B_PARTITIONMASK;
  123.     if ((howto & RB_ASKNAME) == 0) {
  124.         if ((devtype & B_MAGICMASK) != B_DEVMAGIC) {
  125.             /*
  126.              * we have to map the ROM device type codes
  127.              * to Unix major device numbers.
  128.              */
  129.             type = rom2mdev[*(char *)MSUS & 0x1f];
  130.             devtype = (devtype &~ (B_TYPEMASK << B_TYPESHIFT))
  131.                   | (type << B_TYPESHIFT);
  132.         }
  133.         if (type >= 0 && type <= MAXTYPE && devname[type][0]) {
  134.             cp = line;
  135.             *cp++ = devname[type][0];
  136.             *cp++ = devname[type][1];
  137.             *cp++ = '(';
  138.             if (unit >= 10)
  139.                 *cp++ = unit / 10 + '0';
  140.             *cp++ = unit % 10 + '0';
  141.             *cp++ = ',';
  142.             *cp++ = part + '0';
  143.             *cp++ = ')';
  144.             strcpy(cp, UNIX);
  145.         } else
  146.             howto = RB_SINGLE|RB_ASKNAME;
  147.     }
  148. #endif
  149.     for (;;) {
  150.         if (!noconsole && (howto & RB_ASKNAME)) {
  151.             printf(": ");
  152.             gets(line);
  153.         } else
  154.             printf(": %s\n", line);
  155.         io = open(line, 0);
  156.         if (io >= 0) {
  157. #ifndef INSECURE
  158.             (void) fstat(io, &sb);
  159.             if (sb.st_uid || (sb.st_mode & 2)) {
  160.                 printf("non-secure file, will not load\n");
  161.                 howto = RB_SINGLE|RB_ASKNAME;
  162.                 continue;
  163.             }
  164. #endif
  165.             if (howto & RB_ASKNAME) {
  166.                 /*
  167.                  * Build up devtype register to pass on to
  168.                  * booted program.
  169.                  */ 
  170.                 cp = line;
  171.                 for (type = 0; type <= MAXTYPE; type++)
  172.                     if ((devname[type][0] == cp[0]) && 
  173.                         (devname[type][1] == cp[1]))
  174.                             break;
  175.                 if (type <= MAXTYPE) {
  176.                     cp += 3;
  177.                     unit = *cp++ - '0';
  178.                     if (*cp >= '0' && *cp <= '9')
  179.                         unit = unit * 10 + *cp++ - '0';
  180.                     cp++;
  181.                     part = atol(cp);
  182.                     devtype = B_MAKEDEV(unit >> 3, unit & 7, part, type);
  183.                 }
  184.             }
  185.             devtype |= B_DEVMAGIC;
  186.             copyunix(howto, devtype, io);
  187.             close(io);
  188.             howto = RB_SINGLE|RB_ASKNAME;
  189.         }
  190.     bad:
  191.         if (++retry > 2)
  192.             howto = RB_SINGLE|RB_ASKNAME;
  193.     }
  194. }
  195.  
  196. /*ARGSUSED*/
  197. copyunix(howto, devtype, io)
  198.     register howto;        /* d7 contains boot flags */
  199.     register devtype;    /* d6 contains boot device */
  200.     register io;
  201. {
  202.     struct exec x;
  203.     register int i;
  204.     register char *load;    /* a5 contains load addr for unix */
  205.     register char *addr;
  206.  
  207.     i = read(io, (char *)&x, sizeof x);
  208.     if (i != sizeof x ||
  209.         (x.a_magic != 0407 && x.a_magic != 0413 && x.a_magic != 0410))
  210.         _stop("Bad format\n");
  211.     printf("%d", x.a_text);
  212.     if (x.a_magic == 0413 && lseek(io, 0x400, 0) == -1)
  213.         goto shread;
  214.     load = addr = lowram;
  215.     if (read(io, (char *)addr, x.a_text) != x.a_text)
  216.         goto shread;
  217.     addr += x.a_text;
  218.     if (x.a_magic == 0413 || x.a_magic == 0410)
  219.         while ((int)addr & CLOFSET)
  220.             *addr++ = 0;
  221.     printf("+%d", x.a_data);
  222.     if (read(io, addr, x.a_data) != x.a_data)
  223.         goto shread;
  224.     addr += x.a_data;
  225.     printf("+%d", x.a_bss);
  226.     x.a_bss += 128*512;    /* slop */
  227.     for (i = 0; i < x.a_bss; i++)
  228.         *addr++ = 0;
  229.     x.a_entry += (int)lowram;
  230.     printf(" start 0x%x\n", x.a_entry);
  231. #ifdef __GNUC__
  232.     asm("    movl %0,d7" : : "m" (howto));
  233.     asm("    movl %0,d6" : : "m" (devtype));
  234.     asm("    movl %0,a5" : : "a" (load));
  235. #endif
  236.     (*((int (*)()) x.a_entry))();
  237.     exit();
  238. shread:
  239.     _stop("Short read\n");
  240. }
  241.