next68k.c revision 1.3 1 1.3 jmc /* $NetBSD: next68k.c,v 1.3 2004/06/20 22:20:17 jmc Exp $ */
2 1.1 cl
3 1.1 cl /*-
4 1.1 cl * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 1.1 cl * All rights reserved.
6 1.1 cl *
7 1.1 cl * This code is derived from software contributed to The NetBSD Foundation
8 1.1 cl * by David Laight and Christian Limpach.
9 1.1 cl *
10 1.1 cl * Redistribution and use in source and binary forms, with or without
11 1.1 cl * modification, are permitted provided that the following conditions
12 1.1 cl * are met:
13 1.1 cl * 1. Redistributions of source code must retain the above copyright
14 1.1 cl * notice, this list of conditions and the following disclaimer.
15 1.1 cl * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cl * notice, this list of conditions and the following disclaimer in the
17 1.1 cl * documentation and/or other materials provided with the distribution.
18 1.1 cl * 3. All advertising materials mentioning features or use of this software
19 1.1 cl * must display the following acknowledgement:
20 1.1 cl * This product includes software developed by the NetBSD
21 1.1 cl * Foundation, Inc. and its contributors.
22 1.1 cl * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 cl * contributors may be used to endorse or promote products derived
24 1.1 cl * from this software without specific prior written permission.
25 1.1 cl *
26 1.1 cl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 cl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 cl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 cl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 cl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 cl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 cl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 cl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 cl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 cl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 cl * POSSIBILITY OF SUCH DAMAGE.
37 1.1 cl */
38 1.1 cl
39 1.1 cl #if HAVE_NBTOOL_CONFIG_H
40 1.1 cl #include "nbtool_config.h"
41 1.1 cl #endif
42 1.1 cl
43 1.2 lukem #include <sys/cdefs.h>
44 1.2 lukem #if !defined(__lint)
45 1.3 jmc __RCSID("$NetBSD: next68k.c,v 1.3 2004/06/20 22:20:17 jmc Exp $");
46 1.2 lukem #endif /* !__lint */
47 1.2 lukem
48 1.1 cl #include <sys/param.h>
49 1.1 cl
50 1.1 cl #include <assert.h>
51 1.1 cl #include <err.h>
52 1.1 cl #include <md5.h>
53 1.1 cl #include <stddef.h>
54 1.1 cl #include <stdio.h>
55 1.1 cl #include <stdlib.h>
56 1.1 cl #include <string.h>
57 1.1 cl #include <unistd.h>
58 1.1 cl
59 1.1 cl #include "installboot.h"
60 1.1 cl
61 1.1 cl #define SECTOR_SIZE DEV_BSIZE
62 1.1 cl
63 1.1 cl static uint16_t nextstep_checksum(const void *, const void *);
64 1.1 cl
65 1.1 cl static uint16_t
66 1.1 cl nextstep_checksum(const void *vbuf, const void *vlimit)
67 1.1 cl {
68 1.1 cl const uint16_t *buf = vbuf;
69 1.1 cl const uint16_t *limit = vlimit;
70 1.3 jmc u_int sum = 0;
71 1.1 cl
72 1.1 cl while (buf < limit) {
73 1.1 cl sum += be16toh(*buf++);
74 1.1 cl }
75 1.1 cl sum += (sum >> 16);
76 1.1 cl return (sum & 0xffff);
77 1.1 cl }
78 1.1 cl
79 1.1 cl int
80 1.1 cl next68k_setboot(ib_params *params)
81 1.1 cl {
82 1.1 cl int retval, labelupdated;
83 1.1 cl uint8_t *bootbuf;
84 1.3 jmc u_int bootsize;
85 1.1 cl ssize_t rv;
86 1.1 cl uint32_t cd_secsize;
87 1.1 cl int sec_netonb_mult;
88 1.1 cl struct next68k_disklabel *next68klabel;
89 1.1 cl uint16_t *checksum;
90 1.1 cl uint32_t fp, b0, b1;
91 1.1 cl
92 1.1 cl assert(params != NULL);
93 1.1 cl assert(params->fsfd != -1);
94 1.1 cl assert(params->filesystem != NULL);
95 1.1 cl assert(params->s1fd != -1);
96 1.1 cl assert(params->stage1 != NULL);
97 1.1 cl
98 1.1 cl retval = 0;
99 1.1 cl labelupdated = 0;
100 1.1 cl bootbuf = NULL;
101 1.1 cl
102 1.1 cl next68klabel = malloc(NEXT68K_LABEL_SIZE);
103 1.1 cl if (next68klabel == NULL) {
104 1.2 lukem warn("Allocating %lu bytes", (unsigned long)NEXT68K_LABEL_SIZE);
105 1.1 cl goto done;
106 1.1 cl }
107 1.1 cl
108 1.1 cl /*
109 1.1 cl * Read in the next68k disklabel
110 1.1 cl */
111 1.1 cl rv = pread(params->fsfd, next68klabel, NEXT68K_LABEL_SIZE,
112 1.1 cl NEXT68K_LABEL_SECTOR * SECTOR_SIZE + NEXT68K_LABEL_OFFSET);
113 1.1 cl if (rv == -1) {
114 1.1 cl warn("Reading `%s'", params->filesystem);
115 1.1 cl goto done;
116 1.1 cl }
117 1.1 cl if (rv != NEXT68K_LABEL_SIZE) {
118 1.1 cl warnx("Reading `%s': short read", params->filesystem);
119 1.1 cl goto done;
120 1.1 cl }
121 1.1 cl if (be32toh(next68klabel->cd_version) == NEXT68K_LABEL_CD_V3) {
122 1.1 cl checksum = &next68klabel->NEXT68K_LABEL_cd_v3_checksum;
123 1.1 cl } else {
124 1.1 cl checksum = &next68klabel->cd_checksum;
125 1.1 cl }
126 1.1 cl if (nextstep_checksum (next68klabel, checksum) !=
127 1.1 cl be16toh(*checksum)) {
128 1.2 lukem warn("Disklabel checksum invalid on `%s'",
129 1.1 cl params->filesystem);
130 1.1 cl goto done;
131 1.1 cl }
132 1.1 cl
133 1.1 cl cd_secsize = be32toh(next68klabel->cd_secsize);
134 1.1 cl sec_netonb_mult = (cd_secsize / SECTOR_SIZE);
135 1.1 cl
136 1.1 cl /*
137 1.1 cl * Allocate a buffer, with space to round up the input file
138 1.1 cl * to the next block size boundary, and with space for the boot
139 1.1 cl * block.
140 1.1 cl */
141 1.1 cl bootsize = roundup(params->s1stat.st_size, cd_secsize);
142 1.1 cl
143 1.1 cl bootbuf = malloc(bootsize);
144 1.1 cl if (bootbuf == NULL) {
145 1.2 lukem warn("Allocating %lu bytes", (unsigned long)bootsize);
146 1.1 cl goto done;
147 1.1 cl }
148 1.1 cl memset(bootbuf, 0, bootsize);
149 1.1 cl
150 1.1 cl /*
151 1.1 cl * Read the file into the buffer.
152 1.1 cl */
153 1.1 cl rv = pread(params->s1fd, bootbuf, params->s1stat.st_size, 0);
154 1.1 cl if (rv == -1) {
155 1.1 cl warn("Reading `%s'", params->stage1);
156 1.1 cl goto done;
157 1.1 cl } else if (rv != params->s1stat.st_size) {
158 1.1 cl warnx("Reading `%s': short read", params->stage1);
159 1.1 cl goto done;
160 1.1 cl }
161 1.1 cl
162 1.1 cl if (bootsize > be16toh(next68klabel->cd_front) * cd_secsize -
163 1.1 cl NEXT68K_LABEL_SIZE) {
164 1.1 cl warnx("Boot program is larger than front porch space");
165 1.1 cl goto done;
166 1.1 cl }
167 1.1 cl
168 1.1 cl fp = be16toh(next68klabel->cd_front);
169 1.1 cl b0 = be32toh(next68klabel->cd_boot_blkno[0]);
170 1.1 cl b1 = be32toh(next68klabel->cd_boot_blkno[1]);
171 1.1 cl
172 1.1 cl if (b0 > fp)
173 1.1 cl b0 = fp;
174 1.1 cl if (b1 > fp)
175 1.1 cl b1 = fp;
176 1.1 cl if (((bootsize / cd_secsize) > b1 - b0) ||
177 1.1 cl ((bootsize / cd_secsize) > fp - b1)) {
178 1.1 cl if (2 * bootsize > (fp * cd_secsize - NEXT68K_LABEL_SIZE))
179 1.1 cl /* can only fit one copy */
180 1.1 cl b0 = b1 = NEXT68K_LABEL_SIZE / cd_secsize;
181 1.1 cl else {
182 1.1 cl if (2 * bootsize > (fp * cd_secsize -
183 1.1 cl NEXT68K_LABEL_DEFAULTBOOT0_1 * SECTOR_SIZE))
184 1.1 cl /* can fit two copies starting after label */
185 1.1 cl b0 = NEXT68K_LABEL_SIZE / cd_secsize;
186 1.1 cl else
187 1.1 cl /* can fit two copies starting at default 1 */
188 1.1 cl b0 = NEXT68K_LABEL_DEFAULTBOOT0_1 /
189 1.1 cl sec_netonb_mult;
190 1.1 cl /* try to fit 2nd copy at default 2 */
191 1.1 cl b1 = NEXT68K_LABEL_DEFAULTBOOT0_2 / sec_netonb_mult;
192 1.1 cl if (fp < b1)
193 1.1 cl b1 = fp;
194 1.1 cl if (bootsize / cd_secsize > (fp - b1))
195 1.1 cl /* fit 2nd copy before front porch */
196 1.1 cl b1 = fp - bootsize / cd_secsize;
197 1.1 cl }
198 1.1 cl }
199 1.1 cl if (next68klabel->cd_boot_blkno[0] != htobe32(b0)) {
200 1.1 cl next68klabel->cd_boot_blkno[0] = htobe32(b0);
201 1.1 cl labelupdated = 1;
202 1.1 cl }
203 1.1 cl if (next68klabel->cd_boot_blkno[1] != htobe32(b1)) {
204 1.1 cl next68klabel->cd_boot_blkno[1] = htobe32(b1);
205 1.1 cl labelupdated = 1;
206 1.1 cl }
207 1.1 cl if (params->flags & IB_VERBOSE)
208 1.1 cl printf("Boot programm locations%s: %d %d\n",
209 1.1 cl labelupdated ? " updated" : "", b0 * sec_netonb_mult,
210 1.1 cl b1 * sec_netonb_mult);
211 1.1 cl
212 1.1 cl if (params->flags & IB_NOWRITE) {
213 1.1 cl retval = 1;
214 1.1 cl goto done;
215 1.1 cl }
216 1.1 cl
217 1.1 cl /*
218 1.1 cl * Write the updated next68k disklabel
219 1.1 cl */
220 1.1 cl if (labelupdated) {
221 1.1 cl if (params->flags & IB_VERBOSE)
222 1.1 cl printf ("Writing updated label\n");
223 1.1 cl *checksum = htobe16(nextstep_checksum (next68klabel,
224 1.1 cl checksum));
225 1.1 cl rv = pwrite(params->fsfd, next68klabel, NEXT68K_LABEL_SIZE,
226 1.1 cl NEXT68K_LABEL_SECTOR * SECTOR_SIZE + NEXT68K_LABEL_OFFSET);
227 1.1 cl if (rv == -1) {
228 1.1 cl warn("Writing `%s'", params->filesystem);
229 1.1 cl goto done;
230 1.1 cl }
231 1.1 cl if (rv != NEXT68K_LABEL_SIZE) {
232 1.1 cl warnx("Writing `%s': short write", params->filesystem);
233 1.1 cl goto done;
234 1.1 cl }
235 1.1 cl }
236 1.1 cl
237 1.1 cl b0 *= sec_netonb_mult;
238 1.1 cl b1 *= sec_netonb_mult;
239 1.1 cl
240 1.1 cl /*
241 1.1 cl * Write boot program to locations b0 and b1 (if different).
242 1.1 cl */
243 1.1 cl for (;;) {
244 1.1 cl if (params->flags & IB_VERBOSE)
245 1.1 cl printf ("Writing boot program at %d\n", b0);
246 1.1 cl rv = pwrite(params->fsfd, bootbuf, bootsize, b0 * SECTOR_SIZE);
247 1.1 cl if (rv == -1) {
248 1.1 cl warn("Writing `%s' at %d", params->filesystem, b0);
249 1.1 cl goto done;
250 1.1 cl }
251 1.1 cl if (rv != bootsize) {
252 1.1 cl warnx("Writing `%s' at %d: short write",
253 1.1 cl params->filesystem, b0);
254 1.1 cl goto done;
255 1.1 cl }
256 1.1 cl if (b0 == b1)
257 1.1 cl break;
258 1.1 cl b0 = b1;
259 1.1 cl }
260 1.1 cl
261 1.1 cl retval = 1;
262 1.1 cl
263 1.1 cl done:
264 1.1 cl if (bootbuf)
265 1.1 cl free(bootbuf);
266 1.1 cl if (next68klabel)
267 1.1 cl free(next68klabel);
268 1.1 cl return retval;
269 1.1 cl }
270