sparc.c revision 1.2 1 /* $NetBSD: sparc.c,v 1.2 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: sparc.c,v 1.2 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 sparc_clearboot(ib_params *params)
69 {
70 char bb[SPARC_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), SPARC_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), SPARC_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 sparc_setboot(ib_params *params)
120 {
121 struct stat filesystemsb, bootstrapsb;
122 char bb[SPARC_BOOT_BLOCK_MAX_SIZE];
123 int retval;
124 ssize_t rv;
125 size_t bbi;
126 struct sparc_bbinfo *bbinfop; /* bbinfo in prototype image */
127 uint32_t maxblk, nblk, blk_i;
128 ib_block *blocks;
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(SPARC_BBINFO_MAGICSIZE == 32);
137
138 #define SPARC_AOUT_OFFSET 32
139
140 retval = 0;
141 blocks = NULL;
142
143 if (params->stage2 == NULL) {
144 warnx("You must provide the name of the secondary bootstrap");
145 goto done;
146 }
147 if (params->flags & IB_STAGE1START) {
148 warnx("`-b bno' is not supported for %s",
149 params->machine->name);
150 goto done;
151 }
152
153 if (fstat(params->fsfd, &filesystemsb) == -1) {
154 warn("Examining `%s'", params->filesystem);
155 goto done;
156 }
157 if (fstat(params->s1fd, &bootstrapsb) == -1) {
158 warn("Examining `%s'", params->stage1);
159 goto done;
160 }
161 if (!S_ISREG(bootstrapsb.st_mode)) {
162 warnx("`%s' must be a regular file", params->stage1);
163 goto done;
164 }
165 if (bootstrapsb.st_size > sizeof(bb)) {
166 warnx("`%s' cannot be larger than %lu bytes",
167 params->stage1, (unsigned long)sizeof(bb));
168 goto done;
169 }
170
171 /*
172 * Read 1st stage boot program
173 * Leave room for a 32-byte a.out header.
174 */
175 memset(&bb, 0, sizeof(bb));
176 rv = read(params->s1fd, bb + SPARC_AOUT_OFFSET,
177 sizeof(bb) - SPARC_AOUT_OFFSET);
178 if (rv == -1) {
179 warn("Reading `%s'", params->stage1);
180 goto done;
181 }
182
183 /*
184 * Quick sanity check that the bootstrap given
185 * is *not* an ELF executable.
186 */
187 if (memcmp(bb + SPARC_AOUT_OFFSET + 1, "ELF", strlen("ELF")) == 0) {
188 warnx("`%s' is an ELF executable; need raw binary",
189 params->stage1);
190 goto done;
191 }
192
193 /* Look for the bbinfo structure. */
194 for (bbi = 0; bbi < sizeof(bb); bbi += sizeof(uint32_t)) {
195 bbinfop = (void *) (bb + bbi);
196 if (memcmp(bbinfop->bbi_magic, SPARC_BBINFO_MAGIC,
197 SPARC_BBINFO_MAGICSIZE) == 0)
198 break;
199 }
200 if (bbi >= sizeof(bb)) {
201 warnx("`%s' does not have a bbinfo structure\n",
202 params->stage1);
203 goto done;
204 }
205 maxblk = be32toh(bbinfop->bbi_block_count);
206 if (maxblk == 0 || maxblk > (sizeof(bb) / sizeof(uint32_t))) {
207 warnx("bbinfo structure in `%s' has preposterous size `%u'",
208 params->stage1, maxblk);
209 goto done;
210 }
211
212 /* Allocate space for our block list. */
213 blocks = malloc(sizeof(*blocks) * maxblk);
214 if (blocks == NULL) {
215 warn("Allocating %lu bytes",
216 (unsigned long) sizeof(*blocks) * maxblk);
217 goto done;
218 }
219
220 if (S_ISREG(filesystemsb.st_mode)) {
221 if (fsync(params->fsfd) == -1)
222 warn("Synchronising file system `%s'",
223 params->filesystem);
224 } else {
225 /* Ensure the secondary bootstrap is on disk. */
226 sync();
227 }
228
229 /* Collect the blocks for the secondary bootstrap. */
230 nblk = maxblk;
231 if (! params->fstype->findstage2(params, &nblk, blocks))
232 goto done;
233 if (nblk == 0) {
234 warnx("Secondary bootstrap `%s' is empty",
235 params->stage2);
236 goto done;
237 }
238
239 /* Save those blocks in the primary bootstrap. */
240 bbinfop->bbi_block_count = htobe32(nblk);
241 bbinfop->bbi_block_size = htobe32(blocks[0].blocksize);
242 for (blk_i = 0; blk_i < nblk; blk_i++) {
243 bbinfop->bbi_block_table[blk_i] =
244 htobe32(blocks[blk_i].block);
245 if (blocks[blk_i].blocksize < blocks[0].blocksize &&
246 blk_i + 1 != nblk) {
247 warnx("Secondary bootstrap `%s' blocks do not have " \
248 "a uniform size\n", params->stage2);
249 goto done;
250 }
251 }
252
253 /*
254 * sun4c/sun4m PROMs require an a.out(5) format header.
255 * Old-style sun4 PROMs do not expect a header at all.
256 * To deal with this, we construct a header that is also executable
257 * code containing a forward branch that gets us past the 32-byte
258 * header where the actual code begins. In assembly:
259 * .word MAGIC ! a NOP
260 * ba,a start !
261 * .skip 24 ! pad
262 * start:
263 */
264 #define SUN_MAGIC 0x01030107
265 #define SUN4_BASTART 0x30800007 /* i.e.: ba,a `start' */
266 *((uint32_t *)bb) = htobe32(SUN_MAGIC);
267 *((uint32_t *)bb + 1) = htobe32(SUN4_BASTART);
268
269 if (params->flags & IB_VERBOSE) {
270 printf("Bootstrap start sector: %u\n",
271 SPARC_BOOT_BLOCK_OFFSET / SPARC_BOOT_BLOCK_BLOCKSIZE);
272 printf("Bootstrap byte count: %u\n", (unsigned)rv);
273 printf("Bootstrap block table: %u entries of %u bytes available, %u used:",
274 maxblk, blocks[0].blocksize, nblk);
275 for (blk_i = 0; blk_i < nblk; blk_i++)
276 printf(" %u", blocks[blk_i].block);
277 printf("\n%sriting bootstrap\n",
278 (params->flags & IB_NOWRITE) ? "Not w" : "W");
279 }
280 if (params->flags & IB_NOWRITE) {
281 retval = 1;
282 goto done;
283 }
284
285 rv = pwrite(params->fsfd, &bb, sizeof(bb), SPARC_BOOT_BLOCK_OFFSET);
286 if (rv == -1) {
287 warn("Writing `%s'", params->filesystem);
288 goto done;
289 } else if (rv != sizeof(bb)) {
290 warnx("Writing `%s': short write", params->filesystem);
291 goto done;
292 } else {
293
294 /* Sync filesystems (to clean in-memory superblock?) */
295 sync();
296
297 retval = 1;
298 }
299
300 done:
301 if (blocks != NULL)
302 free (blocks);
303 return (retval);
304 }
305