amiga.c revision 1.3 1 /* $NetBSD: amiga.c,v 1.3 2004/06/20 22:20:17 jmc Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Michael Hitch.
9 *
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Luke Mewburn of Wasabi Systems.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
40 */
41
42 #if HAVE_NBTOOL_CONFIG_H
43 #include "nbtool_config.h"
44 #endif
45
46 #include <sys/cdefs.h>
47 #if defined(__RCSID) && !defined(__lint)
48 __RCSID("$NetBSD: amiga.c,v 1.3 2004/06/20 22:20:17 jmc Exp $");
49 #endif /* !__lint */
50
51 #include <sys/param.h>
52 #include <sys/stat.h>
53
54 #include <assert.h>
55 #include <err.h>
56 #include <stddef.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <unistd.h>
60 #include <string.h>
61
62 #include "installboot.h"
63
64 /* XXX Must be kept in sync with bbstart.s! */
65 #define CMDLN_LOC 0x10
66 #define CMDLN_LEN 0x20
67
68 #define CHKSUMOFFS 1
69
70 u_int32_t chksum(u_int32_t *, int);
71
72 int amiga_setboot(ib_params *);
73 int amiga_clearboot(ib_params *);
74
75 int
76 amiga_clearboot(ib_params *params)
77 {
78 return 0;
79 }
80
81 int
82 amiga_setboot(ib_params *params)
83 {
84 int retval;
85 ssize_t rv;
86 char *dline;
87 int sumlen;
88 u_int32_t sum2, sum16;
89
90 struct stat bootstrapsb;
91
92 u_int32_t block[128*16];
93
94 if (fstat(params->s1fd, &bootstrapsb) == -1) {
95 warn("Examining `%s'", params->stage1);
96 goto done;
97 }
98 if (!S_ISREG(bootstrapsb.st_mode)) {
99 warnx("`%s' must be a regular file", params->stage1);
100 goto done;
101 }
102
103 rv = pread(params->s1fd, &block, sizeof(block), 0);
104 if (rv == -1) {
105 warn("Reading `%s'", params->stage1);
106 goto done;
107 } else if (rv != sizeof(block)) {
108 warnx("Reading `%s': short read", params->stage1);
109 goto done;
110 }
111
112 /* XXX the choices should not be hardcoded */
113
114 sum2 = chksum(block, 1024/4);
115 sum16 = chksum(block, 8192/4);
116
117 if (sum16 == 0xffffffff) {
118 sumlen = 8192/4;
119 } else if (sum2 == 0xffffffff) {
120 sumlen = 1024/4;
121 } else {
122 errx(1, "%s: wrong checksum", params->stage1);
123 /* NOTREACHED */
124 }
125
126 if (sum2 == sum16) {
127 warnx("eek - both sums are the same");
128 }
129
130 if (params->flags & IB_COMMAND) {
131 dline = (char *)&(block[CMDLN_LOC/4]);
132 /* XXX keep the default default line in sync with bbstart.s */
133 if (strcmp(dline, "netbsd -ASn2") != 0) {
134 errx(1, "Old bootblock version? Can't change command line.");
135 }
136 (void)strncpy(dline, params->command, CMDLN_LEN-1);
137
138 block[1] = 0;
139 block[1] = 0xffffffff - chksum(block, sumlen);
140 }
141
142 if (params->flags & IB_NOWRITE) {
143 retval = 1;
144 goto done;
145 }
146
147 if (params->flags & IB_VERBOSE)
148 printf("Writing boot block\n");
149 rv = pwrite(params->fsfd, &block, sizeof(block), 0);
150 if (rv == -1) {
151 warn("Writing `%s'", params->filesystem);
152 goto done;
153 } else if (rv != sizeof(block)) {
154 warnx("Writing `%s': short write", params->filesystem);
155 goto done;
156 } else {
157 retval = 1;
158 }
159
160 done:
161 return (retval);
162 }
163
164 u_int32_t
165 chksum(block, size)
166 u_int32_t *block;
167 int size;
168 {
169 u_int32_t sum, lastsum;
170 int i;
171
172 sum = 0;
173
174 for (i=0; i<size; i++) {
175 lastsum = sum;
176 sum += htobe32(block[i]);
177 if (sum < lastsum)
178 ++sum;
179 }
180
181 return sum;
182 }
183