bbinfo.c revision 1.9 1 /* $NetBSD: bbinfo.c,v 1.9 2003/10/27 00:12:44 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 #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.9 2003/10/27 00:12:44 lukem 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 retval = 0;
143 blocks = NULL;
144 if ((bb = malloc(bbparams->maxsize)) == NULL) {
145 warn("Allocating %lu bytes for bbinfo",
146 (unsigned long) bbparams->maxsize);
147 goto done;
148 }
149
150 if (params->stage2 == NULL) {
151 warnx("Name of secondary bootstrap not provided");
152 goto done;
153 }
154
155 if (params->s1stat.st_size >
156 bbparams->maxsize - bbparams->headeroffset) {
157 warnx("`%s' cannot be larger than %lu bytes",
158 params->stage1, (unsigned long)(bbparams->maxsize -
159 bbparams->headeroffset));
160 goto done;
161 }
162
163 memset(bb, 0, bbparams->maxsize);
164 rv = read(params->s1fd, bb + bbparams->headeroffset,
165 bbparams->maxsize - bbparams->headeroffset);
166 if (rv == -1) {
167 warn("Reading `%s'", params->stage1);
168 goto done;
169 }
170
171 /*
172 * Quick sanity check that the bootstrap given
173 * is *not* an ELF executable.
174 */
175 if (memcmp(bb + bbparams->headeroffset + 1, "ELF", strlen("ELF"))
176 == 0) {
177 warnx("`%s' is an ELF executable; need raw binary",
178 params->stage1);
179 goto done;
180 }
181
182 #define HOSTTOTARGET32(x) ((bbparams->endian == BBINFO_LITTLE_ENDIAN) \
183 ? htole32((x)) : htobe32((x)))
184 #define TARGET32TOHOST(x) ((bbparams->endian == BBINFO_LITTLE_ENDIAN) \
185 ? le32toh((x)) : be32toh((x)))
186
187 /* Look for the bbinfo structure. */
188 for (bbi = 0; bbi < bbparams->maxsize; bbi += sizeof(uint32_t)) {
189 bbinfop = (void *) (bb + bbparams->headeroffset + bbi);
190 if (memcmp(bbinfop->bbi_magic, bbparams->magic,
191 sizeof(bbinfop->bbi_magic)) == 0)
192 break;
193 }
194 if (bbi >= bbparams->maxsize) {
195 warnx("%s bbinfo structure not found in `%s'",
196 params->machine->name, params->stage1);
197 goto done;
198 }
199 maxblk = TARGET32TOHOST(bbinfop->bbi_block_count);
200 if (maxblk == 0 || maxblk > (bbparams->maxsize / sizeof(uint32_t))) {
201 warnx("%s bbinfo structure in `%s' has preposterous size `%u'",
202 params->machine->name, params->stage1, maxblk);
203 goto done;
204 }
205
206 /* Allocate space for our block list. */
207 blocks = malloc(sizeof(*blocks) * maxblk);
208 if (blocks == NULL) {
209 warn("Allocating %lu bytes",
210 (unsigned long)sizeof(*blocks) * maxblk);
211 goto done;
212 }
213
214 if (S_ISREG(params->fsstat.st_mode)) {
215 if (fsync(params->fsfd) == -1)
216 warn("Synchronising file system `%s'",
217 params->filesystem);
218 } else {
219 /* Ensure the secondary bootstrap is on disk. */
220 sync();
221 }
222
223 /* Collect the blocks for the secondary bootstrap. */
224 nblk = maxblk;
225 if (! params->fstype->findstage2(params, &nblk, blocks))
226 goto done;
227 if (nblk == 0) {
228 warnx("Secondary bootstrap `%s' is empty",
229 params->stage2);
230 goto done;
231 }
232
233 /* Save those blocks in the primary bootstrap. */
234 bbinfop->bbi_block_count = HOSTTOTARGET32(nblk);
235 bbinfop->bbi_block_size = HOSTTOTARGET32(blocks[0].blocksize);
236 for (blk_i = 0; blk_i < nblk; blk_i++) {
237 bbinfop->bbi_block_table[blk_i] =
238 HOSTTOTARGET32(blocks[blk_i].block);
239 if (blocks[blk_i].blocksize < blocks[0].blocksize &&
240 blk_i + 1 != nblk) {
241 warnx("Secondary bootstrap `%s' blocks do not have "
242 "a uniform size", params->stage2);
243 goto done;
244 }
245 }
246 if (callback != NULL && ! (*callback)(params, bbparams, bb))
247 goto done;
248
249 if (params->flags & IB_VERBOSE) {
250 printf("Bootstrap start sector: %u\n",
251 bbparams->offset / bbparams->blocksize);
252 printf("Bootstrap byte count: %u\n", (unsigned)rv);
253 printf("Bootstrap block table: "
254 "%u entries of %u bytes available, %u used:",
255 maxblk, blocks[0].blocksize, nblk);
256 for (blk_i = 0; blk_i < nblk; blk_i++)
257 printf(" %llu",
258 (unsigned long long)blocks[blk_i].block);
259 printf("\n%sriting bootstrap\n",
260 (params->flags & IB_NOWRITE) ? "Not w" : "W");
261 }
262 if (params->flags & IB_NOWRITE) {
263 retval = 1;
264 goto done;
265 }
266
267 rv = pwrite(params->fsfd, bb, bbparams->maxsize, bbparams->offset);
268 if (rv == -1) {
269 warn("Writing `%s'", params->filesystem);
270 goto done;
271 } else if (rv != bbparams->maxsize) {
272 warnx("Writing `%s': short write", params->filesystem);
273 goto done;
274 } else {
275 retval = 1;
276 }
277
278 done:
279 if (blocks != NULL)
280 free(blocks);
281 if (bb != NULL)
282 free(bb);
283 return (retval);
284 }
285