home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / sys / i386 / stand / bootxx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-28  |  4.2 KB  |  139 lines

  1. /*-
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * William Jolitz.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  *
  36.  *    @(#)bootxx.c    5.2 (Berkeley) 4/28/91
  37.  */
  38.  
  39. #include "param.h"
  40. #include <a.out.h>
  41. #include "saio.h"
  42. #include "reboot.h"
  43. #include "disklabel.h"
  44.  
  45. char *bootprog = "/boot";
  46. extern int opendev, bootdev, cyloffset;
  47. extern struct disklabel disklabel;
  48.  
  49. /*
  50.  * Boot program... loads /boot out of filesystem indicated by arguements.
  51.  * We assume an autoboot unless we detect a misconfiguration.
  52.  */
  53.  
  54. main(dev, unit, off)
  55. {
  56.     register struct disklabel *lp;
  57.     register int io, partition, howto;
  58.  
  59.  
  60.     /* are we a disk, if so look at disklabel and do things */
  61.     lp = &disklabel;
  62.     if (lp->d_magic == DISKMAGIC) {
  63.         /*
  64.          * Synthesize bootdev from dev, unit, type and partition
  65.          * information from the block 0 bootstrap.
  66.          * It's dirty work, but someone's got to do it.
  67.          * This will be used by the filesystem primatives, and
  68.          * drivers. Ultimately, opendev will be created corresponding
  69.          * to which drive to pass to top level bootstrap.
  70.          */
  71.         for (io = 0; io < 8; io++)
  72. #ifdef notyetSCSI
  73.         if (lp->d_type == DTYPE_SCSI) {
  74.             if (lp->d_partitions[io].p_offset == off)
  75.                 break;
  76.         } else
  77. #endif
  78.         if (lp->d_partitions[io].p_offset == off*lp->d_secpercyl)
  79.             break;
  80.  
  81.         if (io == 8) goto screwed;
  82.             cyloffset = off;
  83.     } else {
  84. screwed:
  85.         /* probably a bad or non-existant disklabel */
  86.         io = 0 ;
  87.         howto |= RB_SINGLE|RB_ASKNAME ;
  88.     }
  89.  
  90.     /* construct bootdev */
  91.     /* currently, PC has no way of booting off alternate controllers */
  92.     bootdev = MAKEBOOTDEV(/*i_dev*/ dev, /*i_adapt*/0, /*i_ctlr*/0,
  93.         unit, /*i_part*/io);
  94.  
  95.     printf("loading %s\n", bootprog);
  96.     io = open(bootprog, 0);
  97.     if (io >= 0)
  98.         copyunix(io, howto);
  99.     _stop("boot failed\n");
  100. }
  101.  
  102. /*ARGSUSED*/
  103. copyunix(io, howto)
  104.     register io;    
  105. {
  106.     struct exec x;
  107.     register int i;
  108.     char *addr;
  109.  
  110.     i = read(io, (char *)&x, sizeof x);
  111.     if (i != sizeof x ||
  112.         (x.a_magic != 0407 && x.a_magic != 0413 && x.a_magic != 0410))
  113.         _stop("Bad format\n");
  114.  
  115.     if ((x.a_magic == 0413 || x.a_magic == 0410) &&
  116.         lseek(io, 0x400, 0) == -1)
  117.         goto shread;
  118.  
  119.     if (read(io, (char *)0, x.a_text) != x.a_text)
  120.         goto shread;
  121.  
  122.     addr = (char *)x.a_text;
  123.     if (x.a_magic == 0413 || x.a_magic == 0410)
  124.         while ((int)addr & CLOFSET)
  125.             *addr++ = 0;
  126.  
  127.     if (read(io, addr, x.a_data) != x.a_data)
  128.         goto shread;
  129.  
  130.     addr += x.a_data;
  131.     for (i = 0; i < x.a_bss; i++)
  132.         *addr++ = 0;
  133.  
  134.      (*((int (*)()) x.a_entry))(howto, opendev, cyloffset);
  135.     return;
  136. shread:
  137.     _stop("Short read\n");
  138. }
  139.