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