sparc.c revision 1.3 1 /* $NetBSD: sparc.c,v 1.3 2002/05/14 06:40:33 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.3 2002/05/14 06:40:33 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 #include "installboot.h"
60
61 int
62 sparc_clearboot(ib_params *params)
63 {
64 char bb[SPARC_BOOT_BLOCK_MAX_SIZE];
65 ssize_t rv;
66
67 assert(params != NULL);
68 assert(params->fsfd != -1);
69 assert(params->filesystem != NULL);
70
71 if (params->flags & IB_STAGE2START) {
72 warnx("Can't use `-B bno' with `-c'");
73 return (0);
74 }
75 if (params->flags & IB_STAGE1START) {
76 warnx("`-b bno' is not supported for %s",
77 params->machine->name);
78 return (0);
79 }
80
81 /* first check that it _could_ exist here */
82 rv = pread(params->fsfd, &bb, sizeof(bb), SPARC_BOOT_BLOCK_OFFSET);
83 if (rv == -1) {
84 warn("Reading `%s'", params->filesystem);
85 return (0);
86 } else if (rv != sizeof(bb)) {
87 warnx("Reading `%s': short read", params->filesystem);
88 return (0);
89 }
90
91 /* now clear it out to nothing */
92 memset(&bb, 0, sizeof(bb));
93
94 if (params->flags & IB_VERBOSE)
95 printf("%slearing boot block\n",
96 (params->flags & IB_NOWRITE) ? "Not c" : "C");
97 if (params->flags & IB_NOWRITE)
98 return (1);
99
100 rv = pwrite(params->fsfd, &bb, sizeof(bb), SPARC_BOOT_BLOCK_OFFSET);
101 if (rv == -1) {
102 warn("Writing `%s'", params->filesystem);
103 return (0);
104 } else if (rv != sizeof(bb)) {
105 warnx("Writing `%s': short write", params->filesystem);
106 return (0);
107 }
108
109 return (1);
110 }
111
112 int
113 sparc_setboot(ib_params *params)
114 {
115 struct stat filesystemsb, bootstrapsb;
116 char bb[SPARC_BOOT_BLOCK_MAX_SIZE];
117 int retval;
118 ssize_t rv;
119 size_t bbi;
120 struct sparc_bbinfo *bbinfop; /* bbinfo in prototype image */
121 uint32_t maxblk, nblk, blk_i;
122 ib_block *blocks;
123
124 assert(params != NULL);
125 assert(params->fsfd != -1);
126 assert(params->filesystem != NULL);
127 assert(params->fstype != NULL);
128 assert(params->s1fd != -1);
129 assert(params->stage1 != NULL);
130 assert(SPARC_BBINFO_MAGICSIZE == 32);
131
132 #define SPARC_AOUT_OFFSET 32
133
134 retval = 0;
135 blocks = NULL;
136
137 if (params->stage2 == NULL) {
138 warnx("You must provide the name of the secondary bootstrap");
139 goto done;
140 }
141 if (params->flags & IB_STAGE1START) {
142 warnx("`-b bno' is not supported for %s",
143 params->machine->name);
144 goto done;
145 }
146
147 if (fstat(params->fsfd, &filesystemsb) == -1) {
148 warn("Examining `%s'", params->filesystem);
149 goto done;
150 }
151 if (fstat(params->s1fd, &bootstrapsb) == -1) {
152 warn("Examining `%s'", params->stage1);
153 goto done;
154 }
155 if (!S_ISREG(bootstrapsb.st_mode)) {
156 warnx("`%s' must be a regular file", params->stage1);
157 goto done;
158 }
159 if (bootstrapsb.st_size > sizeof(bb)) {
160 warnx("`%s' cannot be larger than %lu bytes",
161 params->stage1, (unsigned long)sizeof(bb));
162 goto done;
163 }
164
165 /*
166 * Read 1st stage boot program
167 * Leave room for a 32-byte a.out header.
168 */
169 memset(&bb, 0, sizeof(bb));
170 rv = read(params->s1fd, bb + SPARC_AOUT_OFFSET,
171 sizeof(bb) - SPARC_AOUT_OFFSET);
172 if (rv == -1) {
173 warn("Reading `%s'", params->stage1);
174 goto done;
175 }
176
177 /*
178 * Quick sanity check that the bootstrap given
179 * is *not* an ELF executable.
180 */
181 if (memcmp(bb + SPARC_AOUT_OFFSET + 1, "ELF", strlen("ELF")) == 0) {
182 warnx("`%s' is an ELF executable; need raw binary",
183 params->stage1);
184 goto done;
185 }
186
187 /* Look for the bbinfo structure. */
188 for (bbi = 0; bbi < sizeof(bb); bbi += sizeof(uint32_t)) {
189 bbinfop = (void *) (bb + bbi);
190 if (memcmp(bbinfop->bbi_magic, SPARC_BBINFO_MAGIC,
191 SPARC_BBINFO_MAGICSIZE) == 0)
192 break;
193 }
194 if (bbi >= sizeof(bb)) {
195 warnx("`%s' does not have a bbinfo structure\n",
196 params->stage1);
197 goto done;
198 }
199 maxblk = be32toh(bbinfop->bbi_block_count);
200 if (maxblk == 0 || maxblk > (sizeof(bb) / sizeof(uint32_t))) {
201 warnx("bbinfo structure in `%s' has preposterous size `%u'",
202 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(filesystemsb.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 = htobe32(nblk);
235 bbinfop->bbi_block_size = htobe32(blocks[0].blocksize);
236 for (blk_i = 0; blk_i < nblk; blk_i++) {
237 bbinfop->bbi_block_table[blk_i] =
238 htobe32(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\n", params->stage2);
243 goto done;
244 }
245 }
246
247 /*
248 * sun4c/sun4m PROMs require an a.out(5) format header.
249 * Old-style sun4 PROMs do not expect a header at all.
250 * To deal with this, we construct a header that is also executable
251 * code containing a forward branch that gets us past the 32-byte
252 * header where the actual code begins. In assembly:
253 * .word MAGIC ! a NOP
254 * ba,a start !
255 * .skip 24 ! pad
256 * start:
257 */
258 #define SUN_MAGIC 0x01030107
259 #define SUN4_BASTART 0x30800007 /* i.e.: ba,a `start' */
260 *((uint32_t *)bb) = htobe32(SUN_MAGIC);
261 *((uint32_t *)bb + 1) = htobe32(SUN4_BASTART);
262
263 if (params->flags & IB_VERBOSE) {
264 printf("Bootstrap start sector: %u\n",
265 SPARC_BOOT_BLOCK_OFFSET / SPARC_BOOT_BLOCK_BLOCKSIZE);
266 printf("Bootstrap byte count: %u\n", (unsigned)rv);
267 printf("Bootstrap block table: %u entries of %u bytes available, %u used:",
268 maxblk, blocks[0].blocksize, nblk);
269 for (blk_i = 0; blk_i < nblk; blk_i++)
270 printf(" %u", blocks[blk_i].block);
271 printf("\n%sriting bootstrap\n",
272 (params->flags & IB_NOWRITE) ? "Not w" : "W");
273 }
274 if (params->flags & IB_NOWRITE) {
275 retval = 1;
276 goto done;
277 }
278
279 rv = pwrite(params->fsfd, &bb, sizeof(bb), SPARC_BOOT_BLOCK_OFFSET);
280 if (rv == -1) {
281 warn("Writing `%s'", params->filesystem);
282 goto done;
283 } else if (rv != sizeof(bb)) {
284 warnx("Writing `%s': short write", params->filesystem);
285 goto done;
286 } else {
287
288 /* Sync filesystems (to clean in-memory superblock?) */
289 sync();
290
291 retval = 1;
292 }
293
294 done:
295 if (blocks != NULL)
296 free (blocks);
297 return (retval);
298 }
299