xref: /minix3/sys/arch/i386/stand/bootxx/boot1.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
158a2b000SEvgeniy Ivanov /*	$NetBSD: boot1.c,v 1.20 2011/01/06 01:08:48 jakllsch Exp $	*/
258a2b000SEvgeniy Ivanov 
358a2b000SEvgeniy Ivanov /*-
458a2b000SEvgeniy Ivanov  * Copyright (c) 2003 The NetBSD Foundation, Inc.
558a2b000SEvgeniy Ivanov  * All rights reserved.
658a2b000SEvgeniy Ivanov  *
758a2b000SEvgeniy Ivanov  * This code is derived from software contributed to The NetBSD Foundation
858a2b000SEvgeniy Ivanov  * by David Laight.
958a2b000SEvgeniy Ivanov  *
1058a2b000SEvgeniy Ivanov  * Redistribution and use in source and binary forms, with or without
1158a2b000SEvgeniy Ivanov  * modification, are permitted provided that the following conditions
1258a2b000SEvgeniy Ivanov  * are met:
1358a2b000SEvgeniy Ivanov  * 1. Redistributions of source code must retain the above copyright
1458a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer.
1558a2b000SEvgeniy Ivanov  * 2. Redistributions in binary form must reproduce the above copyright
1658a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer in the
1758a2b000SEvgeniy Ivanov  *    documentation and/or other materials provided with the distribution.
1858a2b000SEvgeniy Ivanov  *
1958a2b000SEvgeniy Ivanov  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2058a2b000SEvgeniy Ivanov  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2158a2b000SEvgeniy Ivanov  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2258a2b000SEvgeniy Ivanov  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2358a2b000SEvgeniy Ivanov  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2458a2b000SEvgeniy Ivanov  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2558a2b000SEvgeniy Ivanov  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2658a2b000SEvgeniy Ivanov  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2758a2b000SEvgeniy Ivanov  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2858a2b000SEvgeniy Ivanov  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2958a2b000SEvgeniy Ivanov  * POSSIBILITY OF SUCH DAMAGE.
3058a2b000SEvgeniy Ivanov  */
3158a2b000SEvgeniy Ivanov 
3258a2b000SEvgeniy Ivanov #include <sys/cdefs.h>
3358a2b000SEvgeniy Ivanov __RCSID("$NetBSD: boot1.c,v 1.20 2011/01/06 01:08:48 jakllsch Exp $");
3458a2b000SEvgeniy Ivanov 
3558a2b000SEvgeniy Ivanov #include <lib/libsa/stand.h>
3658a2b000SEvgeniy Ivanov #include <lib/libkern/libkern.h>
3758a2b000SEvgeniy Ivanov #include <biosdisk_ll.h>
3858a2b000SEvgeniy Ivanov 
3958a2b000SEvgeniy Ivanov #include <sys/param.h>
4058a2b000SEvgeniy Ivanov #include <sys/bootblock.h>
4158a2b000SEvgeniy Ivanov #include <sys/disklabel.h>
42*84d9c625SLionel Sambuc #if !defined(__minix)
4358a2b000SEvgeniy Ivanov #include <dev/raidframe/raidframevar.h>	/* For RF_PROTECTED_SECTORS */
447f2d47d8SEvgeniy Ivanov #else
457f2d47d8SEvgeniy Ivanov #define RF_PROTECTED_SECTORS 64
46*84d9c625SLionel Sambuc #endif /* !defined(__minix) */
4758a2b000SEvgeniy Ivanov 
4858a2b000SEvgeniy Ivanov #define XSTR(x) #x
4958a2b000SEvgeniy Ivanov #define STR(x) XSTR(x)
5058a2b000SEvgeniy Ivanov 
5158a2b000SEvgeniy Ivanov static daddr_t bios_sector;
5258a2b000SEvgeniy Ivanov 
5358a2b000SEvgeniy Ivanov static struct biosdisk_ll d;
5458a2b000SEvgeniy Ivanov 
5558a2b000SEvgeniy Ivanov const char *boot1(uint32_t, uint64_t *);
5658a2b000SEvgeniy Ivanov extern void putstr(const char *);
5758a2b000SEvgeniy Ivanov 
5858a2b000SEvgeniy Ivanov extern struct disklabel ptn_disklabel;
5958a2b000SEvgeniy Ivanov 
6058a2b000SEvgeniy Ivanov static int
ob(void)6158a2b000SEvgeniy Ivanov ob(void)
6258a2b000SEvgeniy Ivanov {
63*84d9c625SLionel Sambuc #if !defined(__minix)
6458a2b000SEvgeniy Ivanov 	return open("boot", 0);
654c4c045fSEvgeniy Ivanov #else
664c4c045fSEvgeniy Ivanov 	return open("boot_monitor", 0);
67*84d9c625SLionel Sambuc #endif /* !defined(__minix) */
6858a2b000SEvgeniy Ivanov }
6958a2b000SEvgeniy Ivanov 
7058a2b000SEvgeniy Ivanov const char *
boot1(uint32_t biosdev,uint64_t * sector)7158a2b000SEvgeniy Ivanov boot1(uint32_t biosdev, uint64_t *sector)
7258a2b000SEvgeniy Ivanov {
7358a2b000SEvgeniy Ivanov 	struct stat sb;
7458a2b000SEvgeniy Ivanov 	int fd;
7558a2b000SEvgeniy Ivanov 
7658a2b000SEvgeniy Ivanov 	bios_sector = *sector;
7758a2b000SEvgeniy Ivanov 	d.dev = biosdev;
7858a2b000SEvgeniy Ivanov 
79*84d9c625SLionel Sambuc #if defined(__minix)
807f2d47d8SEvgeniy Ivanov 	putstr("\r\nMINIX/x86 " STR(FS) " Primary Bootstrap\r\n");
817f2d47d8SEvgeniy Ivanov #else
8258a2b000SEvgeniy Ivanov 	putstr("\r\nNetBSD/x86 " STR(FS) " Primary Bootstrap\r\n");
83*84d9c625SLionel Sambuc #endif /* defined(__minix) */
8458a2b000SEvgeniy Ivanov 
8558a2b000SEvgeniy Ivanov 	if (set_geometry(&d, NULL))
8658a2b000SEvgeniy Ivanov 		return "set_geometry\r\n";
8758a2b000SEvgeniy Ivanov 
8858a2b000SEvgeniy Ivanov 	/*
8958a2b000SEvgeniy Ivanov 	 * We default to the filesystem at the start of the
9058a2b000SEvgeniy Ivanov 	 * MBR partition
9158a2b000SEvgeniy Ivanov 	 */
9258a2b000SEvgeniy Ivanov 	fd = ob();
9358a2b000SEvgeniy Ivanov 	if (fd != -1)
9458a2b000SEvgeniy Ivanov 		goto done;
9558a2b000SEvgeniy Ivanov 	/*
9658a2b000SEvgeniy Ivanov 	 * Maybe the filesystem is enclosed in a raid set.
9758a2b000SEvgeniy Ivanov 	 * add in size of raidframe header and try again.
9858a2b000SEvgeniy Ivanov 	 * (Maybe this should only be done if the filesystem
9958a2b000SEvgeniy Ivanov 	 * magic number is absent.)
10058a2b000SEvgeniy Ivanov 	 */
10158a2b000SEvgeniy Ivanov 	bios_sector += RF_PROTECTED_SECTORS;
10258a2b000SEvgeniy Ivanov 	fd = ob();
10358a2b000SEvgeniy Ivanov 	if (fd != -1)
10458a2b000SEvgeniy Ivanov 		goto done;
10558a2b000SEvgeniy Ivanov 
106*84d9c625SLionel Sambuc #if defined(__minix) && defined(BOOT_FROM_MINIXFS3)
10758a2b000SEvgeniy Ivanov 	bios_sector -= RF_PROTECTED_SECTORS;
1084c4c045fSEvgeniy Ivanov 	bios_sector += MINIX3_FIRST_SUBP_OFFSET;
10958a2b000SEvgeniy Ivanov 	*sector = bios_sector;
11058a2b000SEvgeniy Ivanov 
11158a2b000SEvgeniy Ivanov 	fd = ob();
11258a2b000SEvgeniy Ivanov 	if (fd != -1)
11358a2b000SEvgeniy Ivanov 		goto done;
114*84d9c625SLionel Sambuc #endif /* defined(__minix) && defined(BOOT_FROM_MINIXFS3) */
11558a2b000SEvgeniy Ivanov 
11658a2b000SEvgeniy Ivanov 	/*
11758a2b000SEvgeniy Ivanov 	 * Nothing at the start of the MBR partition, fallback on
11858a2b000SEvgeniy Ivanov 	 * partition 'a' from the disklabel in this MBR partition.
11958a2b000SEvgeniy Ivanov 	 */
12058a2b000SEvgeniy Ivanov 	if (ptn_disklabel.d_magic != DISKMAGIC ||
12158a2b000SEvgeniy Ivanov 	    ptn_disklabel.d_magic2 != DISKMAGIC ||
12258a2b000SEvgeniy Ivanov 	    ptn_disklabel.d_partitions[0].p_fstype == FS_UNUSED)
12358a2b000SEvgeniy Ivanov 		goto done;
12458a2b000SEvgeniy Ivanov 	bios_sector = ptn_disklabel.d_partitions[0].p_offset;
12558a2b000SEvgeniy Ivanov 	*sector = bios_sector;
12658a2b000SEvgeniy Ivanov 	if (ptn_disklabel.d_partitions[0].p_fstype == FS_RAID)
12758a2b000SEvgeniy Ivanov 		bios_sector += RF_PROTECTED_SECTORS;
12858a2b000SEvgeniy Ivanov 
12958a2b000SEvgeniy Ivanov 	fd = ob();
13058a2b000SEvgeniy Ivanov 
13158a2b000SEvgeniy Ivanov done:
13258a2b000SEvgeniy Ivanov 	/* if we fail here, so will fstat, so keep going */
133f14fb602SLionel Sambuc 	if (fd == -1 || fstat(fd, &sb) == -1)
134*84d9c625SLionel Sambuc #if !defined(__minix)
13558a2b000SEvgeniy Ivanov 		return "Can't open /boot\r\n";
1364c4c045fSEvgeniy Ivanov #else
1374c4c045fSEvgeniy Ivanov 		return "Can't open /boot_monitor\r\n";
138*84d9c625SLionel Sambuc #endif /* !defined(__minix) */
13958a2b000SEvgeniy Ivanov 
14058a2b000SEvgeniy Ivanov 	biosdev = (uint32_t)sb.st_size;
14158a2b000SEvgeniy Ivanov #if 0
14258a2b000SEvgeniy Ivanov 	if (biosdev > SECONDARY_MAX_LOAD)
14358a2b000SEvgeniy Ivanov 		return "/boot too large\r\n";
14458a2b000SEvgeniy Ivanov #endif
14558a2b000SEvgeniy Ivanov 
146f14fb602SLionel Sambuc 	if (read(fd, (void *)SECONDARY_LOAD_ADDRESS, biosdev) != biosdev)
147*84d9c625SLionel Sambuc #if !defined(__minix)
14858a2b000SEvgeniy Ivanov 		return "/boot load failed\r\n";
1494c4c045fSEvgeniy Ivanov #else
1504c4c045fSEvgeniy Ivanov 		return "/boot_monitor load failed\r\n";
151*84d9c625SLionel Sambuc #endif /* !defined(__minix) */
15258a2b000SEvgeniy Ivanov 
153f14fb602SLionel Sambuc 	if (*(uint32_t *)(SECONDARY_LOAD_ADDRESS + 4) != X86_BOOT_MAGIC_2)
154*84d9c625SLionel Sambuc #if !defined(__minix)
15558a2b000SEvgeniy Ivanov 		return "Invalid /boot file format\r\n";
1564c4c045fSEvgeniy Ivanov #else
1574c4c045fSEvgeniy Ivanov 		return "Invalid /boot_monitor file format\r\n";
158*84d9c625SLionel Sambuc #endif /* !defined(__minix) */
15958a2b000SEvgeniy Ivanov 
16058a2b000SEvgeniy Ivanov 	/* We need to jump to the secondary bootstrap in realmode */
16158a2b000SEvgeniy Ivanov 	return 0;
16258a2b000SEvgeniy Ivanov }
16358a2b000SEvgeniy Ivanov 
16458a2b000SEvgeniy Ivanov int
blkdevstrategy(void * devdata,int flag,daddr_t dblk,size_t size,void * buf,size_t * rsize)16558a2b000SEvgeniy Ivanov blkdevstrategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize)
16658a2b000SEvgeniy Ivanov {
16758a2b000SEvgeniy Ivanov 	if (flag != F_READ)
16858a2b000SEvgeniy Ivanov 		return EROFS;
16958a2b000SEvgeniy Ivanov 
17058a2b000SEvgeniy Ivanov 	if (size & (BIOSDISK_DEFAULT_SECSIZE - 1))
17158a2b000SEvgeniy Ivanov 		return EINVAL;
17258a2b000SEvgeniy Ivanov 
17358a2b000SEvgeniy Ivanov 	if (rsize)
17458a2b000SEvgeniy Ivanov 		*rsize = size;
17558a2b000SEvgeniy Ivanov 
17658a2b000SEvgeniy Ivanov 	if (size != 0 && readsects(&d, bios_sector + dblk,
17758a2b000SEvgeniy Ivanov 				   size / BIOSDISK_DEFAULT_SECSIZE,
17858a2b000SEvgeniy Ivanov 				   buf, 1) != 0)
17958a2b000SEvgeniy Ivanov 		return EIO;
18058a2b000SEvgeniy Ivanov 
18158a2b000SEvgeniy Ivanov 	return 0;
18258a2b000SEvgeniy Ivanov }
183