sun68k.c revision 1.12 1 /* $NetBSD: sun68k.c,v 1.12 2002/05/14 06:18:52 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: sun68k.c,v 1.12 2002/05/14 06:18:52 lukem Exp $");
42 #endif /* !__lint */
43
44 #if HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include <sys/param.h>
49 #include <sys/stat.h>
50
51 #include <assert.h>
52 #include <err.h>
53 #include <stddef.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 #if HAVE_CONFIG_H
60 #include "../../sys/sys/bootblock.h"
61 #else
62 #include <sys/bootblock.h>
63 #endif
64
65 #include "installboot.h"
66
67 int
68 sun68k_clearboot(ib_params *params)
69 {
70 char bb[SUN68K_BOOT_BLOCK_MAX_SIZE];
71 ssize_t rv;
72
73 assert(params != NULL);
74 assert(params->fsfd != -1);
75 assert(params->filesystem != NULL);
76
77 if (params->flags & IB_STAGE2START) {
78 warnx("Can't use `-B bno' with `-c'");
79 return (0);
80 }
81 if (params->flags & IB_STAGE1START) {
82 warnx("`-b bno' is not supported for %s",
83 params->machine->name);
84 return (0);
85 }
86
87 /* first check that it _could_ exist here */
88 rv = pread(params->fsfd, &bb, sizeof(bb), SUN68K_BOOT_BLOCK_OFFSET);
89 if (rv == -1) {
90 warn("Reading `%s'", params->filesystem);
91 return (0);
92 } else if (rv != sizeof(bb)) {
93 warnx("Reading `%s': short read", params->filesystem);
94 return (0);
95 }
96
97 /* now clear it out to nothing */
98 memset(&bb, 0, sizeof(bb));
99
100 if (params->flags & IB_VERBOSE)
101 printf("%slearing boot block\n",
102 (params->flags & IB_NOWRITE) ? "Not c" : "C");
103 if (params->flags & IB_NOWRITE)
104 return (1);
105
106 rv = pwrite(params->fsfd, &bb, sizeof(bb), SUN68K_BOOT_BLOCK_OFFSET);
107 if (rv == -1) {
108 warn("Writing `%s'", params->filesystem);
109 return (0);
110 } else if (rv != sizeof(bb)) {
111 warnx("Writing `%s': short write", params->filesystem);
112 return (0);
113 }
114
115 return (1);
116 }
117
118 int
119 sun68k_setboot(ib_params *params)
120 {
121 struct stat filesystemsb, bootstrapsb;
122 char bb[SUN68K_BOOT_BLOCK_MAX_SIZE];
123 int retval;
124 ssize_t rv;
125 size_t bbi;
126 struct sun68k_bbinfo *bbinfop; /* bbinfo in prototype image */
127 uint32_t maxblk, nblk, blk_i;
128 ib_block *blocks = NULL;
129
130 assert(params != NULL);
131 assert(params->fsfd != -1);
132 assert(params->filesystem != NULL);
133 assert(params->fstype != NULL);
134 assert(params->s1fd != -1);
135 assert(params->stage1 != NULL);
136 assert(SUN68K_BBINFO_MAGICSIZE == 32);
137
138 if (params->stage2 == NULL) {
139 warnx("You must provide the name of the secondary bootstrap");
140 return (0);
141 }
142
143 retval = 0;
144
145 if (fstat(params->fsfd, &filesystemsb) == -1) {
146 warn("Examining `%s'", params->filesystem);
147 goto done;
148 }
149 if (fstat(params->s1fd, &bootstrapsb) == -1) {
150 warn("Examining `%s'", params->stage1);
151 goto done;
152 }
153 if (!S_ISREG(bootstrapsb.st_mode)) {
154 warnx("`%s' must be a regular file", params->stage1);
155 goto done;
156 }
157 if (bootstrapsb.st_size > sizeof(bb)) {
158 warnx("`%s' cannot be larger than %lu bytes",
159 params->stage1, (unsigned long)sizeof(bb));
160 goto done;
161 }
162
163 memset(&bb, 0, sizeof(bb));
164 rv = read(params->s1fd, &bb, sizeof(bb));
165 if (rv == -1) {
166 warn("Reading `%s'", params->stage1);
167 goto done;
168 }
169
170 /*
171 * Quick sanity check that the bootstrap given
172 * is *not* an ELF executable.
173 */
174 if (memcmp(bb + 1, "ELF", strlen("ELF")) == 0) {
175 warnx("`%s' is an ELF executable; need raw binary",
176 params->stage1);
177 goto done;
178 }
179
180 /* Look for the bbinfo structure. */
181 for (bbi = 0; bbi < sizeof(bb); bbi += sizeof(uint32_t)) {
182 bbinfop = (void *) (bb + bbi);
183 if (memcmp(bbinfop->bbi_magic, SUN68K_BBINFO_MAGIC,
184 SUN68K_BBINFO_MAGICSIZE) == 0)
185 break;
186 }
187 if (bbi >= sizeof(bb)) {
188 warnx("`%s' does not have a bbinfo structure\n",
189 params->stage1);
190 goto done;
191 }
192 maxblk = be32toh(bbinfop->bbi_block_count);
193 if (maxblk == 0 || maxblk > (sizeof(bb) / sizeof(uint32_t))) {
194 warnx("bbinfo structure in `%s' has preposterous size `%u'",
195 params->stage1, maxblk);
196 goto done;
197 }
198
199 /* Allocate space for our block list. */
200 blocks = malloc(sizeof(*blocks) * maxblk);
201 if (blocks == NULL) {
202 warn("Allocating %lu bytes",
203 (unsigned long) sizeof(*blocks) * maxblk);
204 goto done;
205 }
206
207 if (S_ISREG(filesystemsb.st_mode)) {
208 if (fsync(params->fsfd) == -1)
209 warn("Synchronising file system `%s'",
210 params->filesystem);
211 } else {
212 /* Ensure the secondary bootstrap is on disk. */
213 sync();
214 }
215
216 /* Collect the blocks for the secondary bootstrap. */
217 nblk = maxblk;
218 if (! params->fstype->findstage2(params, &nblk, blocks))
219 goto done;
220 if (nblk == 0) {
221 warnx("Secondary bootstrap `%s' is empty",
222 params->stage2);
223 goto done;
224 }
225
226 /* Save those blocks in the primary bootstrap. */
227 bbinfop->bbi_block_count = htobe32(nblk);
228 bbinfop->bbi_block_size = htobe32(blocks[0].blocksize);
229 for (blk_i = 0; blk_i < nblk; blk_i++) {
230 bbinfop->bbi_block_table[blk_i] =
231 htobe32(blocks[blk_i].block);
232 if (blocks[blk_i].blocksize < blocks[0].blocksize &&
233 blk_i + 1 != nblk) {
234 warnx("Secondary bootstrap `%s' blocks do not have " \
235 "a uniform size\n", params->stage2);
236 goto done;
237 }
238 }
239
240 if (params->flags & IB_VERBOSE) {
241 printf("Bootstrap start sector: %u\n",
242 SUN68K_BOOT_BLOCK_OFFSET / SUN68K_BOOT_BLOCK_BLOCKSIZE);
243 printf("Bootstrap byte count: %u\n", (unsigned)rv);
244 printf("Bootstrap block table: %u entries of %u bytes available, %u used:",
245 maxblk, blocks[0].blocksize, nblk);
246 for (blk_i = 0; blk_i < nblk; blk_i++)
247 printf(" %u", blocks[blk_i].block);
248 printf("\n%sriting bootstrap\n",
249 (params->flags & IB_NOWRITE) ? "Not w" : "W");
250 }
251 if (params->flags & IB_NOWRITE) {
252 retval = 1;
253 goto done;
254 }
255
256 rv = pwrite(params->fsfd, &bb, sizeof(bb), SUN68K_BOOT_BLOCK_OFFSET);
257 if (rv == -1) {
258 warn("Writing `%s'", params->filesystem);
259 goto done;
260 } else if (rv != sizeof(bb)) {
261 warnx("Writing `%s': short write", params->filesystem);
262 goto done;
263 } else {
264
265 /* Sync filesystems (to clean in-memory superblock?) */
266 sync();
267
268 retval = 1;
269 }
270
271 done:
272 if (blocks != NULL)
273 free (blocks);
274 return (retval);
275 }
276