bbinfo.c revision 1.3 1 /* $NetBSD: bbinfo.c,v 1.3 2002/05/20 14:56:10 lukem 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 #include <sys/cdefs.h>
40 #if defined(__RCSID) && !defined(__lint)
41 __RCSID("$NetBSD: bbinfo.c,v 1.3 2002/05/20 14:56:10 lukem Exp $");
42 #endif /* !__lint */
43
44 #if HAVE_CONFIG_H
45 #include "config.h"
46 #endif
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 {
63 char *bb;
64 ssize_t rv;
65 int retval;
66
67 assert(params != NULL);
68 assert(params->fsfd != -1);
69 assert(params->filesystem != NULL);
70 assert(bbparams != NULL);
71 assert((strlen(bbparams->magic) + 1) == 32);
72
73 retval = 0;
74 if ((bb = malloc(bbparams->maxsize)) == NULL) {
75 warn("Allocating %lu bytes for bbinfo",
76 (unsigned long) bbparams->maxsize);
77 goto done;
78 }
79
80 if (params->flags & IB_STAGE2START) {
81 warnx("Can't use `-B bno' with `-c'");
82 goto done;
83 }
84
85 /* First check that it _could_ exist here */
86 rv = pread(params->fsfd, bb, bbparams->maxsize, bbparams->offset);
87 if (rv == -1) {
88 warn("Reading `%s'", params->filesystem);
89 goto done;
90 } else if (rv != bbparams->maxsize) {
91 warnx("Reading `%s': short read", params->filesystem);
92 goto done;
93 }
94
95 /* Now clear it out to nothing */
96 memset(bb, 0, bbparams->maxsize);
97
98 if (params->flags & IB_VERBOSE)
99 printf("%slearing boot block\n",
100 (params->flags & IB_NOWRITE) ? "Not c" : "C");
101 if (params->flags & IB_NOWRITE) {
102 retval = 1;
103 goto done;
104 }
105
106 rv = pwrite(params->fsfd, bb, bbparams->maxsize, bbparams->offset);
107 if (rv == -1) {
108 warn("Writing `%s'", params->filesystem);
109 goto done;
110 } else if (rv != bbparams->maxsize) {
111 warnx("Writing `%s': short write", params->filesystem);
112 goto done;
113 } else
114 retval = 1;
115
116 done:
117 if (bb != NULL)
118 free(bb);
119 return (retval);
120 }
121
122 int
123 shared_bbinfo_setboot(ib_params *params, struct bbinfo_params *bbparams,
124 int (*callback)(ib_params *, struct bbinfo_params *, char *))
125 {
126 char *bb;
127 int retval;
128 ssize_t rv;
129 size_t bbi;
130 struct shared_bbinfo *bbinfop; /* bbinfo in prototype image */
131 uint32_t maxblk, nblk, blk_i;
132 ib_block *blocks;
133
134 assert(params != NULL);
135 assert(params->fsfd != -1);
136 assert(params->filesystem != NULL);
137 assert(params->fstype != NULL);
138 assert(params->s1fd != -1);
139 assert(params->stage1 != NULL);
140 assert(bbparams != NULL);
141 assert((strlen(bbparams->magic) + 1) == 32);
142
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->littleendian ? le32toh((x)) : be32toh((x)))
184
185 /* Look for the bbinfo structure. */
186 for (bbi = 0; bbi < bbparams->maxsize; bbi += sizeof(uint32_t)) {
187 bbinfop = (void *) (bb + bbi);
188 if (memcmp(bbinfop->bbi_magic, bbparams->magic,
189 sizeof(bbinfop->bbi_magic)) == 0)
190 break;
191 }
192 if (bbi >= bbparams->maxsize) {
193 warnx("%s bbinfo structure not found in `%s'",
194 params->machine->name, params->stage1);
195 goto done;
196 }
197 maxblk = HOSTTOTARGET32(bbinfop->bbi_block_count);
198 if (maxblk == 0 || maxblk > (bbparams->maxsize / sizeof(uint32_t))) {
199 warnx("%s bbinfo structure in `%s' has preposterous size `%u'",
200 params->machine->name, params->stage1, maxblk);
201 goto done;
202 }
203
204 /* Allocate space for our block list. */
205 blocks = malloc(sizeof(*blocks) * maxblk);
206 if (blocks == NULL) {
207 warn("Allocating %lu bytes",
208 (unsigned long) sizeof(*blocks) * maxblk);
209 goto done;
210 }
211
212 if (S_ISREG(params->fsstat.st_mode)) {
213 if (fsync(params->fsfd) == -1)
214 warn("Synchronising file system `%s'",
215 params->filesystem);
216 } else {
217 /* Ensure the secondary bootstrap is on disk. */
218 sync();
219 }
220
221 /* Collect the blocks for the secondary bootstrap. */
222 nblk = maxblk;
223 if (! params->fstype->findstage2(params, &nblk, blocks))
224 goto done;
225 if (nblk == 0) {
226 warnx("Secondary bootstrap `%s' is empty",
227 params->stage2);
228 goto done;
229 }
230
231 /* Save those blocks in the primary bootstrap. */
232 bbinfop->bbi_block_count = HOSTTOTARGET32(nblk);
233 bbinfop->bbi_block_size = HOSTTOTARGET32(blocks[0].blocksize);
234 for (blk_i = 0; blk_i < nblk; blk_i++) {
235 bbinfop->bbi_block_table[blk_i] =
236 HOSTTOTARGET32(blocks[blk_i].block);
237 if (blocks[blk_i].blocksize < blocks[0].blocksize &&
238 blk_i + 1 != nblk) {
239 warnx("Secondary bootstrap `%s' blocks do not have " \
240 "a uniform size", params->stage2);
241 goto done;
242 }
243 }
244 if (callback != NULL && ! (*callback)(params, bbparams, bb))
245 goto done;
246
247 if (params->flags & IB_VERBOSE) {
248 printf("Bootstrap start sector: %u\n",
249 bbparams->offset / bbparams->blocksize);
250 printf("Bootstrap byte count: %u\n", (unsigned)rv);
251 printf("Bootstrap block table: "
252 "%u entries of %u bytes available, %u used:",
253 maxblk, blocks[0].blocksize, nblk);
254 for (blk_i = 0; blk_i < nblk; blk_i++)
255 printf(" %u", blocks[blk_i].block);
256 printf("\n%sriting bootstrap\n",
257 (params->flags & IB_NOWRITE) ? "Not w" : "W");
258 }
259 if (params->flags & IB_NOWRITE) {
260 retval = 1;
261 goto done;
262 }
263
264 rv = pwrite(params->fsfd, bb, bbparams->maxsize, bbparams->offset);
265 if (rv == -1) {
266 warn("Writing `%s'", params->filesystem);
267 goto done;
268 } else if (rv != bbparams->maxsize) {
269 warnx("Writing `%s': short write", params->filesystem);
270 goto done;
271 } else {
272 retval = 1;
273 }
274
275 done:
276 if (blocks != NULL)
277 free (blocks);
278 if (bb != NULL)
279 free (bb);
280 return (retval);
281 }
282