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