xref: /netbsd-src/usr.sbin/installboot/arch/hp300.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /* $NetBSD: hp300.c,v 1.4 2003/11/13 08:19:43 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 #if HAVE_NBTOOL_CONFIG_H
40 #include "nbtool_config.h"
41 #endif
42 
43 #include <sys/cdefs.h>
44 #if !defined(__lint)
45 __RCSID("$NetBSD: hp300.c,v 1.4 2003/11/13 08:19:43 dsl Exp $");
46 #endif /* !__lint */
47 
48 /* We need the target disklabel.h, not the hosts one..... */
49 #ifdef HAVE_NBTOOL_CONFIG_H
50 #include "nbtool_config.h"
51 #undef __HAVE_OLD_DISKLABEL	/* host's <machine/types.h> may define this */
52 #include "../../sys/arch/hp300/include/disklabel.h"
53 #include "../../sys/sys/disklabel.h"
54 #else
55 #include <sys/disklabel.h>
56 #endif
57 #include <sys/fcntl.h>
58 #include <sys/ioctl.h>
59 #include <sys/mman.h>
60 #include <sys/param.h>
61 
62 #include <assert.h>
63 #include <err.h>
64 #include <md5.h>
65 #include <stddef.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <unistd.h>
70 
71 #include "installboot.h"
72 
73 int
74 hp300_setboot(ib_params *params)
75 {
76 	int		retval;
77 	uint8_t		*bootstrap;
78 	ssize_t		rv;
79 	struct partition *boot;
80 	struct hp300_lifdir *lifdir;
81 	int		offset;
82 	int		i;
83 	uint		secsize;
84 	uint64_t	boot_size, boot_offset;
85 	char		label_buf[DEV_BSIZE];
86 	struct disklabel *label = (void *)label_buf;
87 
88 	assert(params != NULL);
89 	assert(params->fsfd != -1);
90 	assert(params->filesystem != NULL);
91 	assert(params->s1fd != -1);
92 	assert(params->stage1 != NULL);
93 
94 	retval = 0;
95 	bootstrap = MAP_FAILED;
96 
97 	if (params->flags & IB_APPEND) {
98 		if (!S_ISREG(params->fsstat.st_mode)) {
99 			warnx(
100 		    "`%s' must be a regular file to append a bootstrap",
101 			    params->filesystem);
102 			goto done;
103 		}
104 		boot_offset = roundup(params->fsstat.st_size, HP300_SECTSIZE);
105 	} else {
106 		/*
107 		 * The bootstrap can be well over 8k, and must go into a BOOT
108 		 * partition. Read NetBSD label to locate BOOT partition.
109 		 */
110 		if (pread(params->fsfd, label, DEV_BSIZE, 2 * DEV_BSIZE)
111 								!= DEV_BSIZE) {
112 			warn("reading disklabel");
113 			goto done;
114 		}
115 		/* And a quick validation - must be a big-endian label */
116 		secsize = be32toh(label->d_secsize);
117 		if (label->d_magic != be32toh(DISKMAGIC) ||
118 		    label->d_magic2 != be32toh(DISKMAGIC) ||
119 		    secsize == 0 || secsize & (secsize - 1) ||
120 		    be32toh(label->d_npartitions) > MAXMAXPARTITIONS) {
121 			warnx("Invalid disklabel in %s", params->filesystem);
122 			goto done;
123 		}
124 
125 		i = be32toh(label->d_npartitions);
126 		for (boot = label->d_partitions; ; boot++) {
127 			if (--i < 0) {
128 				warnx("No BOOT partition");
129 				goto done;
130 			}
131 			if (boot->p_fstype == FS_BOOT)
132 				break;
133 		}
134 		boot_size = be32toh(boot->p_size) * (uint64_t)secsize;
135 		boot_offset = be32toh(boot->p_offset) * (uint64_t)secsize;
136 
137 		/*
138 		 * We put the entire LIF file into the BOOT partition even when
139 		 * it doesn't start at the beginning of the disk.
140 		 *
141 		 * Maybe we ought to be able to take a binary file and add
142 		 * it to the LIF filesystem.
143 		 */
144 		if (boot_size < params->s1stat.st_size) {
145 			warn("BOOT partition too small (%llu < %llu)",
146 				(unsigned long long)boot_size,
147 				(unsigned long long)params->s1stat.st_size);
148 			goto done;
149 		}
150 	}
151 
152 	bootstrap = mmap(NULL, params->s1stat.st_size, PROT_READ | PROT_WRITE,
153 			    MAP_PRIVATE, params->s1fd, 0);
154 	if (bootstrap == MAP_FAILED) {
155 		warn("mmaping `%s'", params->stage1);
156 		goto done;
157 	}
158 
159 	/* Relocate files, sanity check LIF directory on the way */
160 	lifdir = (void *)(bootstrap + HP300_SECTSIZE * 2);
161 	for (i = 0; i < 8; lifdir++, i++) {
162 		int addr = be32toh(lifdir->dir_addr);
163 		int limit = (params->s1stat.st_size - 1) / HP300_SECTSIZE + 1;
164 		if (addr + be32toh(lifdir->dir_length) > limit) {
165 			warnx("LIF entry %d larger (%d %d) than LIF file",
166 				i,  addr + be32toh(lifdir->dir_length), limit);
167 			goto done;
168 		}
169 		if (addr != 0 && boot_offset != 0)
170 			lifdir->dir_addr = htobe32(addr + boot_offset
171 							    / HP300_SECTSIZE);
172 	}
173 
174 	if (params->flags & IB_NOWRITE) {
175 		retval = 1;
176 		goto done;
177 	}
178 
179 	/* Write LIF volume header and directory to sectors 0 and 1 */
180 	rv = pwrite(params->fsfd, bootstrap, 1024, 0);
181 	if (rv != 1024) {
182 		if (rv == -1)
183 			warn("Writing `%s'", params->filesystem);
184 		else
185 			warnx("Writing `%s': short write", params->filesystem);
186 		goto done;
187 	}
188 
189 	/* Write files to BOOT partition */
190 	offset = boot_offset <= HP300_SECTSIZE * 16 ? HP300_SECTSIZE * 16 : 0;
191 	i = roundup(params->s1stat.st_size, secsize) - offset;
192 	rv = pwrite(params->fsfd, bootstrap + offset, i, boot_offset + offset);
193 	if (rv != i) {
194 		if (rv == -1)
195 			warn("Writing boot filesystem of `%s'",
196 				params->filesystem);
197 		else
198 			warnx("Writing boot filesystem of `%s': short write",
199 				params->filesystem);
200 		goto done;
201 	}
202 
203 	retval = 1;
204 
205  done:
206 	if (bootstrap != MAP_FAILED)
207 		munmap(bootstrap, params->s1stat.st_size);
208 	return retval;
209 }
210