bbinfo.c revision 1.7 1 /* $NetBSD: bbinfo.c,v 1.7 2003/04/02 10:39:48 fvdl 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.7 2003/04/02 10:39:48 fvdl 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 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 if (params->flags & IB_STAGE2START) {
82 warnx("Can't use `-B bno' with `-c'");
83 goto done;
84 }
85
86 /* First check that it _could_ exist here */
87 rv = pread(params->fsfd, bb, bbparams->maxsize, bbparams->offset);
88 if (rv == -1) {
89 warn("Reading `%s'", params->filesystem);
90 goto done;
91 } else if (rv != bbparams->maxsize) {
92 warnx("Reading `%s': short read", params->filesystem);
93 goto done;
94 }
95
96 /* Now clear out (past the header offset) */
97 memset(bb + bbparams->headeroffset, 0,
98 bbparams->maxsize - bbparams->headeroffset);
99 if (callback != NULL && ! (*callback)(params, bbparams, bb))
100 goto done;
101
102 if (params->flags & IB_VERBOSE)
103 printf("%slearing boot block\n",
104 (params->flags & IB_NOWRITE) ? "Not c" : "C");
105 if (params->flags & IB_NOWRITE) {
106 retval = 1;
107 goto done;
108 }
109
110 rv = pwrite(params->fsfd, bb, bbparams->maxsize, bbparams->offset);
111 if (rv == -1) {
112 warn("Writing `%s'", params->filesystem);
113 goto done;
114 } else if (rv != bbparams->maxsize) {
115 warnx("Writing `%s': short write", params->filesystem);
116 goto done;
117 } else
118 retval = 1;
119
120 done:
121 if (bb != NULL)
122 free(bb);
123 return (retval);
124 }
125
126 int
127 shared_bbinfo_setboot(ib_params *params, struct bbinfo_params *bbparams,
128 int (*callback)(ib_params *, struct bbinfo_params *, uint8_t *))
129 {
130 uint8_t *bb;
131 int retval;
132 ssize_t rv;
133 size_t bbi;
134 struct shared_bbinfo *bbinfop; /* bbinfo in prototype image */
135 uint32_t maxblk, nblk, blk_i;
136 ib_block *blocks;
137
138 assert(params != NULL);
139 assert(params->fsfd != -1);
140 assert(params->filesystem != NULL);
141 assert(params->fstype != NULL);
142 assert(params->s1fd != -1);
143 assert(params->stage1 != NULL);
144 assert(bbparams != NULL);
145 assert((strlen(bbparams->magic) + 1) == 32);
146
147 retval = 0;
148 blocks = NULL;
149 if ((bb = malloc(bbparams->maxsize)) == NULL) {
150 warn("Allocating %lu bytes for bbinfo",
151 (unsigned long) bbparams->maxsize);
152 goto done;
153 }
154
155 if (params->stage2 == NULL) {
156 warnx("Name of secondary bootstrap not provided");
157 goto done;
158 }
159
160 if (params->s1stat.st_size >
161 bbparams->maxsize - bbparams->headeroffset) {
162 warnx("`%s' cannot be larger than %lu bytes",
163 params->stage1, (unsigned long)(bbparams->maxsize -
164 bbparams->headeroffset));
165 goto done;
166 }
167
168 memset(bb, 0, bbparams->maxsize);
169 rv = read(params->s1fd, bb + bbparams->headeroffset,
170 bbparams->maxsize - bbparams->headeroffset);
171 if (rv == -1) {
172 warn("Reading `%s'", params->stage1);
173 goto done;
174 }
175
176 /*
177 * Quick sanity check that the bootstrap given
178 * is *not* an ELF executable.
179 */
180 if (memcmp(bb + bbparams->headeroffset + 1, "ELF", strlen("ELF"))
181 == 0) {
182 warnx("`%s' is an ELF executable; need raw binary",
183 params->stage1);
184 goto done;
185 }
186
187 #define HOSTTOTARGET32(x) ((bbparams->endian == BBINFO_LITTLE_ENDIAN) \
188 ? htole32((x)) : htobe32((x)))
189 #define TARGET32TOHOST(x) ((bbparams->endian == BBINFO_LITTLE_ENDIAN) \
190 ? le32toh((x)) : be32toh((x)))
191
192 /* Look for the bbinfo structure. */
193 for (bbi = 0; bbi < bbparams->maxsize; bbi += sizeof(uint32_t)) {
194 bbinfop = (void *) (bb + bbparams->headeroffset + bbi);
195 if (memcmp(bbinfop->bbi_magic, bbparams->magic,
196 sizeof(bbinfop->bbi_magic)) == 0)
197 break;
198 }
199 if (bbi >= bbparams->maxsize) {
200 warnx("%s bbinfo structure not found in `%s'",
201 params->machine->name, params->stage1);
202 goto done;
203 }
204 maxblk = TARGET32TOHOST(bbinfop->bbi_block_count);
205 if (maxblk == 0 || maxblk > (bbparams->maxsize / sizeof(uint32_t))) {
206 warnx("%s bbinfo structure in `%s' has preposterous size `%u'",
207 params->machine->name, params->stage1, maxblk);
208 goto done;
209 }
210
211 /* Allocate space for our block list. */
212 blocks = malloc(sizeof(*blocks) * maxblk);
213 if (blocks == NULL) {
214 warn("Allocating %lu bytes",
215 (unsigned long)sizeof(*blocks) * maxblk);
216 goto done;
217 }
218
219 if (S_ISREG(params->fsstat.st_mode)) {
220 if (fsync(params->fsfd) == -1)
221 warn("Synchronising file system `%s'",
222 params->filesystem);
223 } else {
224 /* Ensure the secondary bootstrap is on disk. */
225 sync();
226 }
227
228 /* Collect the blocks for the secondary bootstrap. */
229 nblk = maxblk;
230 if (! params->fstype->findstage2(params, &nblk, blocks))
231 goto done;
232 if (nblk == 0) {
233 warnx("Secondary bootstrap `%s' is empty",
234 params->stage2);
235 goto done;
236 }
237
238 /* Save those blocks in the primary bootstrap. */
239 bbinfop->bbi_block_count = HOSTTOTARGET32(nblk);
240 bbinfop->bbi_block_size = HOSTTOTARGET32(blocks[0].blocksize);
241 for (blk_i = 0; blk_i < nblk; blk_i++) {
242 bbinfop->bbi_block_table[blk_i] =
243 HOSTTOTARGET32(blocks[blk_i].block);
244 if (blocks[blk_i].blocksize < blocks[0].blocksize &&
245 blk_i + 1 != nblk) {
246 warnx("Secondary bootstrap `%s' blocks do not have "
247 "a uniform size", params->stage2);
248 goto done;
249 }
250 }
251 if (callback != NULL && ! (*callback)(params, bbparams, bb))
252 goto done;
253
254 if (params->flags & IB_VERBOSE) {
255 printf("Bootstrap start sector: %u\n",
256 bbparams->offset / bbparams->blocksize);
257 printf("Bootstrap byte count: %u\n", (unsigned)rv);
258 printf("Bootstrap block table: "
259 "%u entries of %u bytes available, %u used:",
260 maxblk, blocks[0].blocksize, nblk);
261 for (blk_i = 0; blk_i < nblk; blk_i++)
262 printf(" %llu",
263 (unsigned long long)blocks[blk_i].block);
264 printf("\n%sriting bootstrap\n",
265 (params->flags & IB_NOWRITE) ? "Not w" : "W");
266 }
267 if (params->flags & IB_NOWRITE) {
268 retval = 1;
269 goto done;
270 }
271
272 rv = pwrite(params->fsfd, bb, bbparams->maxsize, bbparams->offset);
273 if (rv == -1) {
274 warn("Writing `%s'", params->filesystem);
275 goto done;
276 } else if (rv != bbparams->maxsize) {
277 warnx("Writing `%s': short write", params->filesystem);
278 goto done;
279 } else {
280 retval = 1;
281 }
282
283 done:
284 if (blocks != NULL)
285 free(blocks);
286 if (bb != NULL)
287 free(bb);
288 return (retval);
289 }
290