xref: /netbsd-src/sys/arch/landisk/stand/bootxx/boot1.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1 /*	$NetBSD: boot1.c,v 1.2 2008/04/28 20:23:26 martin 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: boot1.c,v 1.2 2008/04/28 20:23:26 martin Exp $");
34 
35 #include <lib/libsa/stand.h>
36 #include <lib/libkern/libkern.h>
37 
38 #include <sys/param.h>
39 #include <sys/bootblock.h>
40 #include <sys/disklabel.h>
41 #include <dev/raidframe/raidframevar.h>	/* For RF_PROTECTED_SECTORS */
42 
43 #include "biosdisk_ll.h"
44 
45 #define	XSTR(x)	#x
46 #define	STR(x)	XSTR(x)
47 
48 static uint32_t bios_sector;
49 
50 const char *boot1(uint32_t *);
51 void putstr(const char *str);
52 int raise(int sig);
53 
54 extern struct disklabel ptn_disklabel;
55 
56 const char *
boot1(uint32_t * sector)57 boot1(uint32_t *sector)
58 {
59         struct stat sb;
60 	int fd;
61 
62 	bios_sector = *sector;
63 
64 	putstr("\r\nNetBSD/" MACHINE " " STR(FS) " Primary Bootstrap\r\n");
65 
66 	do {
67 		/*
68 		 * We default to the filesystem at the start of the
69 		 * MBR partition
70 		 */
71 		fd = open("boot", 0);
72 		if (fd != -1)
73 			break;
74 
75 		/*
76 		 * Maybe the filesystem is enclosed in a raid set.
77 		 * add in size of raidframe header and try again.
78 		 * (Maybe this should only be done if the filesystem
79 		 * magic number is absent.)
80 		 */
81 		bios_sector += RF_PROTECTED_SECTORS;
82 		fd = open("boot", 0);
83 		if (fd != -1)
84 			break;
85 
86 		/*
87 		 * Nothing at the start of the MBR partition, fallback on
88 		 * partition 'a' from the disklabel in this MBR partition.
89 		 */
90 		if (ptn_disklabel.d_magic != DISKMAGIC)
91 			break;
92 		if (ptn_disklabel.d_magic2 != DISKMAGIC)
93 			break;
94 		if (ptn_disklabel.d_partitions[0].p_fstype == FS_UNUSED)
95 			break;
96 
97 		bios_sector = ptn_disklabel.d_partitions[0].p_offset;
98 		*sector = bios_sector;
99 		if (ptn_disklabel.d_partitions[0].p_fstype == FS_RAID)
100 			bios_sector += RF_PROTECTED_SECTORS;
101 		fd = open("boot", 0);
102 	} while (0);
103 
104 	if (fd == -1 || fstat(fd, &sb) == -1)
105 		return "Can't open /boot.\r\n";
106 
107 #if 0
108 	if (sb.st_size > SECONDARY_MAX_LOAD)
109 		return "/boot too large.\r\n";
110 #endif
111 
112 	if (read(fd, (void *)SECONDARY_LOAD_ADDRESS, sb.st_size) != sb.st_size)
113 		return "/boot load failed.\r\n";
114 
115 	if (*(uint32_t *)(SECONDARY_LOAD_ADDRESS + 4) != LANDISK_BOOT_MAGIC_2)
116 		return "Invalid /boot file format.\r\n";
117 
118 	return 0;
119 }
120 
121 int
blkdevstrategy(void * devdata,int flag,daddr_t dblk,size_t size,void * buf,size_t * rsize)122 blkdevstrategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize)
123 {
124 
125 	if (flag != F_READ)
126 		return EROFS;
127 
128 	if (size & (BIOSDISK_SECSIZE - 1))
129 		return EINVAL;
130 
131 	if (rsize)
132 		*rsize = size;
133 
134 	if (size != 0 && readsects(0x40, bios_sector + dblk, buf,
135 				   size / BIOSDISK_SECSIZE) != 0)
136 		return EIO;
137 
138 	return 0;
139 }
140 
141 /*ARGUSED*/
142 int
raise(int sig)143 raise(int sig)
144 {
145 
146 	return 0;
147 }
148