bbinfo.c revision 1.12 1 /* $NetBSD: bbinfo.c,v 1.12 2008/04/28 20:24:16 martin 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35
36 #include <sys/cdefs.h>
37 #if !defined(__lint)
38 __RCSID("$NetBSD: bbinfo.c,v 1.12 2008/04/28 20:24:16 martin Exp $");
39 #endif /* !__lint */
40
41 #include <sys/param.h>
42
43 #include <assert.h>
44 #include <err.h>
45 #include <stddef.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #include "installboot.h"
52
53 int
54 shared_bbinfo_clearboot(ib_params *params, struct bbinfo_params *bbparams,
55 int (*callback)(ib_params *, struct bbinfo_params *, uint8_t *))
56 {
57 uint8_t *bb;
58 ssize_t rv;
59 int retval;
60
61 assert(params != NULL);
62 assert(params->fsfd != -1);
63 assert(params->filesystem != NULL);
64 assert(bbparams != NULL);
65 assert((strlen(bbparams->magic) + 1) == 32);
66
67 retval = 0;
68 if ((bb = malloc(bbparams->maxsize)) == NULL) {
69 warn("Allocating %lu bytes for bbinfo",
70 (unsigned long) bbparams->maxsize);
71 goto done;
72 }
73
74 /* First check that it _could_ exist here */
75 rv = pread(params->fsfd, bb, bbparams->maxsize, bbparams->offset);
76 if (rv == -1) {
77 warn("Reading `%s'", params->filesystem);
78 goto done;
79 } else if (rv != bbparams->maxsize) {
80 warnx("Reading `%s': short read", params->filesystem);
81 goto done;
82 }
83
84 /* Now clear out (past the header offset) */
85 memset(bb + bbparams->headeroffset, 0,
86 bbparams->maxsize - bbparams->headeroffset);
87 if (callback != NULL && ! (*callback)(params, bbparams, bb))
88 goto done;
89
90 if (params->flags & IB_VERBOSE)
91 printf("%slearing boot block\n",
92 (params->flags & IB_NOWRITE) ? "Not c" : "C");
93 if (params->flags & IB_NOWRITE) {
94 retval = 1;
95 goto done;
96 }
97
98 rv = pwrite(params->fsfd, bb, bbparams->maxsize, bbparams->offset);
99 if (rv == -1) {
100 warn("Writing `%s'", params->filesystem);
101 goto done;
102 } else if (rv != bbparams->maxsize) {
103 warnx("Writing `%s': short write", params->filesystem);
104 goto done;
105 } else
106 retval = 1;
107
108 done:
109 if (bb != NULL)
110 free(bb);
111 return (retval);
112 }
113
114 int
115 shared_bbinfo_setboot(ib_params *params, struct bbinfo_params *bbparams,
116 int (*callback)(ib_params *, struct bbinfo_params *, uint8_t *))
117 {
118 uint8_t *bb;
119 int retval;
120 ssize_t rv;
121 size_t bbi;
122 struct shared_bbinfo *bbinfop; /* bbinfo in prototype image */
123 uint32_t maxblk, nblk, blk_i;
124 ib_block *blocks;
125
126 assert(params != NULL);
127 assert(params->fsfd != -1);
128 assert(params->filesystem != NULL);
129 assert(params->fstype != NULL);
130 assert(params->s1fd != -1);
131 assert(params->stage1 != NULL);
132 assert(bbparams != NULL);
133 assert((strlen(bbparams->magic) + 1) == 32);
134
135 bbinfop = NULL; /* XXXGCC -Wuninitialized [sparc64] */
136 retval = 0;
137 blocks = NULL;
138 if ((bb = malloc(bbparams->maxsize)) == NULL) {
139 warn("Allocating %lu bytes for bbinfo",
140 (unsigned long) bbparams->maxsize);
141 goto done;
142 }
143
144 if (params->stage2 == NULL) {
145 warnx("Name of secondary bootstrap not provided");
146 goto done;
147 }
148
149 if (params->s1stat.st_size >
150 bbparams->maxsize - bbparams->headeroffset) {
151 warnx("`%s' cannot be larger than %lu bytes",
152 params->stage1, (unsigned long)(bbparams->maxsize -
153 bbparams->headeroffset));
154 goto done;
155 }
156
157 memset(bb, 0, bbparams->maxsize);
158 rv = read(params->s1fd, bb + bbparams->headeroffset,
159 bbparams->maxsize - bbparams->headeroffset);
160 if (rv == -1) {
161 warn("Reading `%s'", params->stage1);
162 goto done;
163 }
164
165 /*
166 * Quick sanity check that the bootstrap given
167 * is *not* an ELF executable.
168 */
169 if (memcmp(bb + bbparams->headeroffset + 1, "ELF", strlen("ELF"))
170 == 0) {
171 warnx("`%s' is an ELF executable; need raw binary",
172 params->stage1);
173 goto done;
174 }
175
176 #define HOSTTOTARGET32(x) ((bbparams->endian == BBINFO_LITTLE_ENDIAN) \
177 ? htole32((x)) : htobe32((x)))
178 #define TARGET32TOHOST(x) ((bbparams->endian == BBINFO_LITTLE_ENDIAN) \
179 ? le32toh((x)) : be32toh((x)))
180
181 /* Look for the bbinfo structure. */
182 bbinfop = NULL;
183 for (bbi = 0; bbi < bbparams->maxsize; bbi += sizeof(uint32_t)) {
184 bbinfop = (void *) (bb + bbparams->headeroffset + bbi);
185 if (memcmp(bbinfop->bbi_magic, bbparams->magic,
186 sizeof(bbinfop->bbi_magic)) == 0)
187 break;
188 }
189 if (bbi >= bbparams->maxsize) {
190 warnx("%s bbinfo structure not found in `%s'",
191 params->machine->name, params->stage1);
192 goto done;
193 }
194 maxblk = TARGET32TOHOST(bbinfop->bbi_block_count);
195 if (maxblk == 0 || maxblk > (bbparams->maxsize / sizeof(uint32_t))) {
196 warnx("%s bbinfo structure in `%s' has preposterous size `%u'",
197 params->machine->name, params->stage1, maxblk);
198 goto done;
199 }
200
201 /* Allocate space for our block list. */
202 blocks = malloc(sizeof(*blocks) * maxblk);
203 if (blocks == NULL) {
204 warn("Allocating %lu bytes",
205 (unsigned long)sizeof(*blocks) * maxblk);
206 goto done;
207 }
208
209 if (S_ISREG(params->fsstat.st_mode)) {
210 if (fsync(params->fsfd) == -1)
211 warn("Synchronising file system `%s'",
212 params->filesystem);
213 } else {
214 /* Ensure the secondary bootstrap is on disk. */
215 sync();
216 }
217
218 /* Collect the blocks for the secondary bootstrap. */
219 nblk = maxblk;
220 if (! params->fstype->findstage2(params, &nblk, blocks))
221 goto done;
222 if (nblk == 0) {
223 warnx("Secondary bootstrap `%s' is empty",
224 params->stage2);
225 goto done;
226 }
227
228 /* Save those blocks in the primary bootstrap. */
229 bbinfop->bbi_block_count = HOSTTOTARGET32(nblk);
230 bbinfop->bbi_block_size = HOSTTOTARGET32(blocks[0].blocksize);
231 for (blk_i = 0; blk_i < nblk; blk_i++) {
232 bbinfop->bbi_block_table[blk_i] =
233 HOSTTOTARGET32(blocks[blk_i].block);
234 if (blocks[blk_i].blocksize < blocks[0].blocksize &&
235 blk_i + 1 != nblk) {
236 warnx("Secondary bootstrap `%s' blocks do not have "
237 "a uniform size", params->stage2);
238 goto done;
239 }
240 }
241 if (callback != NULL && ! (*callback)(params, bbparams, bb))
242 goto done;
243
244 if (params->flags & IB_VERBOSE) {
245 printf("Bootstrap start sector: %u\n",
246 bbparams->offset / bbparams->blocksize);
247 printf("Bootstrap byte count: %u\n", (unsigned)rv);
248 printf("Bootstrap block table: "
249 "%u entries of %u bytes available, %u used:",
250 maxblk, blocks[0].blocksize, nblk);
251 for (blk_i = 0; blk_i < nblk; blk_i++)
252 printf(" %llu",
253 (unsigned long long)blocks[blk_i].block);
254 printf("\n%sriting bootstrap\n",
255 (params->flags & IB_NOWRITE) ? "Not w" : "W");
256 }
257 if (params->flags & IB_NOWRITE) {
258 retval = 1;
259 goto done;
260 }
261
262 rv = pwrite(params->fsfd, bb, bbparams->maxsize, bbparams->offset);
263 if (rv == -1) {
264 warn("Writing `%s'", params->filesystem);
265 goto done;
266 } else if (rv != bbparams->maxsize) {
267 warnx("Writing `%s': short write", params->filesystem);
268 goto done;
269 } else {
270 retval = 1;
271 }
272
273 done:
274 if (blocks != NULL)
275 free(blocks);
276 if (bb != NULL)
277 free(bb);
278 return (retval);
279 }
280