amiga.c revision 1.3.2.1 1 /* $NetBSD: amiga.c,v 1.3.2.1 2005/06/15 05:34:16 snj 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.2.1 2005/06/15 05:34:16 snj 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 retval = 0;
95 if (fstat(params->s1fd, &bootstrapsb) == -1) {
96 warn("Examining `%s'", params->stage1);
97 goto done;
98 }
99 if (!S_ISREG(bootstrapsb.st_mode)) {
100 warnx("`%s' must be a regular file", params->stage1);
101 goto done;
102 }
103
104 rv = pread(params->s1fd, &block, sizeof(block), 0);
105 if (rv == -1) {
106 warn("Reading `%s'", params->stage1);
107 goto done;
108 } else if (rv != sizeof(block)) {
109 warnx("Reading `%s': short read", params->stage1);
110 goto done;
111 }
112
113 /* XXX the choices should not be hardcoded */
114
115 sum2 = chksum(block, 1024/4);
116 sum16 = chksum(block, 8192/4);
117
118 if (sum16 == 0xffffffff) {
119 sumlen = 8192/4;
120 } else if (sum2 == 0xffffffff) {
121 sumlen = 1024/4;
122 } else {
123 errx(1, "%s: wrong checksum", params->stage1);
124 /* NOTREACHED */
125 }
126
127 if (sum2 == sum16) {
128 warnx("eek - both sums are the same");
129 }
130
131 if (params->flags & IB_COMMAND) {
132 dline = (char *)&(block[CMDLN_LOC/4]);
133 /* XXX keep the default default line in sync with bbstart.s */
134 if (strcmp(dline, "netbsd -ASn2") != 0) {
135 errx(1, "Old bootblock version? Can't change command line.");
136 }
137 (void)strncpy(dline, params->command, CMDLN_LEN-1);
138
139 block[1] = 0;
140 block[1] = 0xffffffff - chksum(block, sumlen);
141 }
142
143 if (params->flags & IB_NOWRITE) {
144 retval = 1;
145 goto done;
146 }
147
148 if (params->flags & IB_VERBOSE)
149 printf("Writing boot block\n");
150 rv = pwrite(params->fsfd, &block, sizeof(block), 0);
151 if (rv == -1) {
152 warn("Writing `%s'", params->filesystem);
153 goto done;
154 } else if (rv != sizeof(block)) {
155 warnx("Writing `%s': short write", params->filesystem);
156 goto done;
157 } else {
158 retval = 1;
159 }
160
161 done:
162 return (retval);
163 }
164
165 u_int32_t
166 chksum(block, size)
167 u_int32_t *block;
168 int size;
169 {
170 u_int32_t sum, lastsum;
171 int i;
172
173 sum = 0;
174
175 for (i=0; i<size; i++) {
176 lastsum = sum;
177 sum += htobe32(block[i]);
178 if (sum < lastsum)
179 ++sum;
180 }
181
182 return sum;
183 }
184