xref: /netbsd-src/usr.sbin/installboot/bbinfo.c (revision df0caa2637da0538ecdf6b878c4d08e684b43d8f)
1 /*	$NetBSD: bbinfo.c,v 1.11 2005/06/07 13:49:31 he Exp $ */
2 
3 /*-
4  * Copyright (c) 1998, 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Matt Fredette, Paul Kranenburg, and Luke Mewburn.
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: bbinfo.c,v 1.11 2005/06/07 13:49:31 he Exp $");
46 #endif	/* !__lint */
47 
48 #include <sys/param.h>
49 
50 #include <assert.h>
51 #include <err.h>
52 #include <stddef.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 
58 #include "installboot.h"
59 
60 int
61 shared_bbinfo_clearboot(ib_params *params, struct bbinfo_params *bbparams,
62 	int (*callback)(ib_params *, struct bbinfo_params *, uint8_t *))
63 {
64 	uint8_t	*bb;
65 	ssize_t	rv;
66 	int	retval;
67 
68 	assert(params != NULL);
69 	assert(params->fsfd != -1);
70 	assert(params->filesystem != NULL);
71 	assert(bbparams != NULL);
72 	assert((strlen(bbparams->magic) + 1) == 32);
73 
74 	retval = 0;
75 	if ((bb = malloc(bbparams->maxsize)) == NULL) {
76 		warn("Allocating %lu bytes for bbinfo",
77 		    (unsigned long) bbparams->maxsize);
78 		goto done;
79 	}
80 
81 		/* First check that it _could_ exist here */
82 	rv = pread(params->fsfd, bb, bbparams->maxsize, bbparams->offset);
83 	if (rv == -1) {
84 		warn("Reading `%s'", params->filesystem);
85 		goto done;
86 	} else if (rv != bbparams->maxsize) {
87 		warnx("Reading `%s': short read", params->filesystem);
88 		goto done;
89 	}
90 
91 		/* Now clear out (past the header offset) */
92 	memset(bb + bbparams->headeroffset, 0,
93 	    bbparams->maxsize - bbparams->headeroffset);
94 	if (callback != NULL && ! (*callback)(params, bbparams, bb))
95 		goto done;
96 
97 	if (params->flags & IB_VERBOSE)
98 		printf("%slearing boot block\n",
99 		    (params->flags & IB_NOWRITE) ? "Not c" : "C");
100 	if (params->flags & IB_NOWRITE) {
101 		retval = 1;
102 		goto done;
103 	}
104 
105 	rv = pwrite(params->fsfd, bb, bbparams->maxsize, bbparams->offset);
106 	if (rv == -1) {
107 		warn("Writing `%s'", params->filesystem);
108 		goto done;
109 	} else if (rv != bbparams->maxsize) {
110 		warnx("Writing `%s': short write", params->filesystem);
111 		goto done;
112 	} else
113 		retval = 1;
114 
115  done:
116 	if (bb != NULL)
117 		free(bb);
118 	return (retval);
119 }
120 
121 int
122 shared_bbinfo_setboot(ib_params *params, struct bbinfo_params *bbparams,
123 	int (*callback)(ib_params *, struct bbinfo_params *, uint8_t *))
124 {
125 	uint8_t		*bb;
126 	int		retval;
127 	ssize_t		rv;
128 	size_t		bbi;
129 	struct shared_bbinfo	*bbinfop;	/* bbinfo in prototype image */
130 	uint32_t	maxblk, nblk, blk_i;
131 	ib_block	*blocks;
132 
133 	assert(params != NULL);
134 	assert(params->fsfd != -1);
135 	assert(params->filesystem != NULL);
136 	assert(params->fstype != NULL);
137 	assert(params->s1fd != -1);
138 	assert(params->stage1 != NULL);
139 	assert(bbparams != NULL);
140 	assert((strlen(bbparams->magic) + 1) == 32);
141 
142 	bbinfop = NULL;		/* XXXGCC -Wuninitialized [sparc64] */
143 	retval = 0;
144 	blocks = NULL;
145 	if ((bb = malloc(bbparams->maxsize)) == NULL) {
146 		warn("Allocating %lu bytes for bbinfo",
147 		    (unsigned long) bbparams->maxsize);
148 		goto done;
149 	}
150 
151 	if (params->stage2 == NULL) {
152 		warnx("Name of secondary bootstrap not provided");
153 		goto done;
154 	}
155 
156 	if (params->s1stat.st_size >
157 	    bbparams->maxsize - bbparams->headeroffset) {
158 		warnx("`%s' cannot be larger than %lu bytes",
159 		    params->stage1, (unsigned long)(bbparams->maxsize -
160 			bbparams->headeroffset));
161 		goto done;
162 	}
163 
164 	memset(bb, 0, bbparams->maxsize);
165 	rv = read(params->s1fd, bb + bbparams->headeroffset,
166 	    bbparams->maxsize - bbparams->headeroffset);
167 	if (rv == -1) {
168 		warn("Reading `%s'", params->stage1);
169 		goto done;
170 	}
171 
172 		/*
173 		 * Quick sanity check that the bootstrap given
174 		 * is *not* an ELF executable.
175 		 */
176 	if (memcmp(bb + bbparams->headeroffset + 1, "ELF", strlen("ELF"))
177 	    == 0) {
178 		warnx("`%s' is an ELF executable; need raw binary",
179 		    params->stage1);
180 		goto done;
181 	}
182 
183 #define HOSTTOTARGET32(x) ((bbparams->endian == BBINFO_LITTLE_ENDIAN) \
184 			    ? htole32((x)) : htobe32((x)))
185 #define TARGET32TOHOST(x) ((bbparams->endian == BBINFO_LITTLE_ENDIAN) \
186 			    ? le32toh((x)) : be32toh((x)))
187 
188 		/* Look for the bbinfo structure. */
189 	bbinfop = NULL;
190 	for (bbi = 0; bbi < bbparams->maxsize; bbi += sizeof(uint32_t)) {
191 		bbinfop = (void *) (bb + bbparams->headeroffset + bbi);
192 		if (memcmp(bbinfop->bbi_magic, bbparams->magic,
193 			    sizeof(bbinfop->bbi_magic)) == 0)
194 			break;
195 	}
196 	if (bbi >= bbparams->maxsize) {
197 		warnx("%s bbinfo structure not found in `%s'",
198 		    params->machine->name, params->stage1);
199 		goto done;
200 	}
201 	maxblk = TARGET32TOHOST(bbinfop->bbi_block_count);
202 	if (maxblk == 0 || maxblk > (bbparams->maxsize / sizeof(uint32_t))) {
203 		warnx("%s bbinfo structure in `%s' has preposterous size `%u'",
204 		    params->machine->name, params->stage1, maxblk);
205 		goto done;
206 	}
207 
208 		/* Allocate space for our block list. */
209 	blocks = malloc(sizeof(*blocks) * maxblk);
210 	if (blocks == NULL) {
211 		warn("Allocating %lu bytes",
212 		    (unsigned long)sizeof(*blocks) * maxblk);
213 		goto done;
214 	}
215 
216 	if (S_ISREG(params->fsstat.st_mode)) {
217 		if (fsync(params->fsfd) == -1)
218 			warn("Synchronising file system `%s'",
219 			    params->filesystem);
220 	} else {
221 		/* Ensure the secondary bootstrap is on disk. */
222 		sync();
223 	}
224 
225 		/* Collect the blocks for the secondary bootstrap. */
226 	nblk = maxblk;
227 	if (! params->fstype->findstage2(params, &nblk, blocks))
228 		goto done;
229 	if (nblk == 0) {
230 		warnx("Secondary bootstrap `%s' is empty",
231 		    params->stage2);
232 		goto done;
233 	}
234 
235 		/* Save those blocks in the primary bootstrap. */
236 	bbinfop->bbi_block_count = HOSTTOTARGET32(nblk);
237 	bbinfop->bbi_block_size = HOSTTOTARGET32(blocks[0].blocksize);
238 	for (blk_i = 0; blk_i < nblk; blk_i++) {
239 		bbinfop->bbi_block_table[blk_i] =
240 		    HOSTTOTARGET32(blocks[blk_i].block);
241 		if (blocks[blk_i].blocksize < blocks[0].blocksize &&
242 		    blk_i + 1 != nblk) {
243 			warnx("Secondary bootstrap `%s' blocks do not have "
244 			    "a uniform size", params->stage2);
245 			goto done;
246 		}
247 	}
248 	if (callback != NULL && ! (*callback)(params, bbparams, bb))
249 		goto done;
250 
251 	if (params->flags & IB_VERBOSE) {
252 		printf("Bootstrap start sector: %u\n",
253 		    bbparams->offset / bbparams->blocksize);
254 		printf("Bootstrap byte count:   %u\n", (unsigned)rv);
255 		printf("Bootstrap block table:  "
256 			"%u entries of %u bytes available, %u used:",
257 		    maxblk, blocks[0].blocksize, nblk);
258 		for (blk_i = 0; blk_i < nblk; blk_i++)
259 			printf(" %llu",
260 			    (unsigned long long)blocks[blk_i].block);
261 		printf("\n%sriting bootstrap\n",
262 		    (params->flags & IB_NOWRITE) ? "Not w" : "W");
263 	}
264 	if (params->flags & IB_NOWRITE) {
265 		retval = 1;
266 		goto done;
267 	}
268 
269 	rv = pwrite(params->fsfd, bb, bbparams->maxsize, bbparams->offset);
270 	if (rv == -1) {
271 		warn("Writing `%s'", params->filesystem);
272 		goto done;
273 	} else if (rv != bbparams->maxsize) {
274 		warnx("Writing `%s': short write", params->filesystem);
275 		goto done;
276 	} else {
277 		retval = 1;
278 	}
279 
280  done:
281 	if (blocks != NULL)
282 		free(blocks);
283 	if (bb != NULL)
284 		free(bb);
285 	return (retval);
286 }
287