Home | History | Annotate | Line # | Download | only in bootxx
boot1.c revision 1.4
      1 /*	$NetBSD: boot1.c,v 1.4 2003/12/07 20:11:11 dsl Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by David Laight.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __RCSID("$NetBSD: boot1.c,v 1.4 2003/12/07 20:11:11 dsl Exp $");
     41 
     42 #include <lib/libsa/stand.h>
     43 #include <lib/libkern/libkern.h>
     44 #include <biosdisk_ll.h>
     45 
     46 #include <sys/param.h>
     47 #include <sys/bootblock.h>
     48 #include <dev/raidframe/raidframevar.h>	/* For RF_PROTECTED_SECTORS */
     49 
     50 #define XSTR(x) #x
     51 #define STR(x) XSTR(x)
     52 
     53 static uint32_t bios_dev;
     54 static uint32_t bios_sector;
     55 
     56 struct biosdisk_ll d;
     57 
     58 const char *boot1(uint32_t biosdev, uint32_t sector);
     59 extern void putstr(const char *);
     60 
     61 const char *
     62 boot1(uint32_t biosdev, uint32_t sector)
     63 {
     64         struct stat sb;
     65 	int fd;
     66 
     67 	bios_sector = sector;
     68 	bios_dev = biosdev;
     69 	d.dev = biosdev;
     70 
     71         putstr("\r\nNetBSD/" MACHINE " " STR(FS) " Primary Bootstrap\r\n");
     72 
     73 	if (set_geometry(&d, NULL))
     74 		return "set_geometry\r\n";
     75 
     76 	fd = open("boot", 0);
     77 	if (fd == -1) {
     78 		/*
     79 		 * Maybe the filesystem is enclosed in a raid set.
     80 		 * add in size of raidframe header and try again.
     81 		 * (Maybe this should only be done if the filesystem
     82 		 * magic number is absent.)
     83 		 */
     84 		 bios_sector += RF_PROTECTED_SECTORS;
     85 		 fd = open("boot", 0);
     86 	}
     87 	if (fd == -1 || fstat(fd, &sb) == -1)
     88 		return "Can't open /boot.\r\n";
     89 
     90 #if 0
     91 	if (sb.st_size > SECONDARY_MAX_LOAD)
     92 		return "/boot too large.\r\n";
     93 #endif
     94 
     95 	if (read(fd, (void *)SECONDARY_LOAD_ADDRESS, sb.st_size) != sb.st_size)
     96 		return "/boot load failed.\r\n";
     97 
     98 	if (*(uint32_t *)(SECONDARY_LOAD_ADDRESS + 4) != X86_BOOT_MAGIC_2)
     99 		return "Invalid /boot file format.\r\n";
    100 
    101 	/* We need to jump to the secondary bootstrap in realmode */
    102 	return 0;
    103 }
    104 
    105 int
    106 blkdevstrategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize)
    107 {
    108 	if (flag != F_READ)
    109 		return EROFS;
    110 
    111 	if (size & (BIOSDISK_SECSIZE - 1))
    112 		return EINVAL;
    113 
    114 	if (rsize)
    115 		*rsize = size;
    116 
    117 	if (size != 0 && readsects(&d, bios_sector + dblk,
    118 				   size / BIOSDISK_SECSIZE, buf, 1) != 0)
    119 		return EIO;
    120 
    121 	return 0;
    122 }
    123