xref: /netbsd-src/usr.sbin/installboot/arch/amiga.c (revision b8c616269f5ebf18ab2e35cb8099d683130a177c)
1 /*	$NetBSD: amiga.c,v 1.1 2003/01/15 06:33:13 mhitch 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 #include <sys/cdefs.h>
43 #if defined(__RCSID) && !defined(__lint)
44 __RCSID("$NetBSD: amiga.c,v 1.1 2003/01/15 06:33:13 mhitch Exp $");
45 #endif	/* !__lint */
46 
47 #include <sys/param.h>
48 #include <sys/stat.h>
49 
50 #include <assert.h>
51 #include <err.h>
52 #include <stddef.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <unistd.h>
56 #include <string.h>
57 
58 #include "installboot.h"
59 
60 /* XXX Must be kept in sync with bbstart.s! */
61 #define CMDLN_LOC 0x10
62 #define CMDLN_LEN 0x20
63 
64 char command[CMDLN_LEN];
65 
66 #define CHKSUMOFFS 1
67 
68 u_int32_t chksum(u_int32_t *, int);
69 
70 int		amiga_parseopt(ib_params *, const char *);
71 int		amiga_setboot(ib_params *);
72 int		amiga_clearboot(ib_params *);
73 
74 int
75 amiga_parseopt(ib_params *params, const char *option)
76 {
77 
78 	if (strncmp("command=", option, strlen("command=")) == 0) {
79 		strncpy(command, option + strlen("command="), CMDLN_LEN);
80 		params->flags |= IB_COMMAND;
81 		return (1);
82 	}
83 
84 	warnx("Unknown -o option `%s'", option);
85 	return (0);
86 }
87 
88 int
89 amiga_clearboot(ib_params *params)
90 {
91 	return 0;
92 }
93 
94 int
95 amiga_setboot(ib_params *params)
96 {
97 	int retval;
98 	ssize_t			rv;
99 	char *dline;
100 	int sumlen;
101 	u_int32_t sum2, sum16;
102 
103 	struct stat		bootstrapsb;
104 
105 	u_int32_t block[128*16];
106 
107 	if (fstat(params->s1fd, &bootstrapsb) == -1) {
108 		warn("Examining `%s'", params->stage1);
109 		goto done;
110 	}
111 	if (!S_ISREG(bootstrapsb.st_mode)) {
112 		warnx("`%s' must be a regular file", params->stage1);
113 		goto done;
114 	}
115 
116 	rv = pread(params->s1fd, &block, sizeof(block), 0);
117 	if (rv == -1) {
118 		warn("Reading `%s'", params->stage1);
119 		goto done;
120 	} else if (rv != sizeof(block)) {
121 		warnx("Reading `%s': short read", params->stage1);
122 		goto done;
123 	}
124 
125 	/* XXX the choices should not be hardcoded */
126 
127 	sum2  = chksum(block, 1024/4);
128 	sum16 = chksum(block, 8192/4);
129 
130 	if (sum16 == 0xffffffff) {
131 		sumlen = 8192/4;
132 	} else if (sum2 == 0xffffffff) {
133 		sumlen = 1024/4;
134 	} else {
135 		errx(1, "%s: wrong checksum", params->stage1);
136 		/* NOTREACHED */
137 	}
138 
139 	if (sum2 == sum16) {
140 		warnx("eek - both sums are the same");
141 	}
142 
143 	if (params->flags & IB_COMMAND) {
144 		dline = (char *)&(block[CMDLN_LOC/4]);
145 		/* XXX keep the default default line in sync with bbstart.s */
146 		if (strcmp(dline, "netbsd -ASn2") != 0) {
147 			errx(1, "Old bootblock version? Can't change command line.");
148 		}
149 		(void)strncpy(dline, command, CMDLN_LEN-1);
150 
151 		block[1] = 0;
152 		block[1] = 0xffffffff - chksum(block, sumlen);
153 	}
154 
155 	if (params->flags & IB_NOWRITE) {
156 		retval = 1;
157 		goto done;
158 	}
159 
160 	if (params->flags & IB_VERBOSE)
161 		printf("Writing boot block\n");
162 	rv = pwrite(params->fsfd, &block, sizeof(block), 0);
163 	if (rv == -1) {
164 		warn("Writing `%s'", params->filesystem);
165 		goto done;
166 	} else if (rv != sizeof(block)) {
167 		warnx("Writing `%s': short write", params->filesystem);
168 		goto done;
169 	} else {
170 		retval = 1;
171 	}
172 
173  done:
174 	return (retval);
175 }
176 
177 u_int32_t
178 chksum(block, size)
179 	u_int32_t *block;
180 	int size;
181 {
182 	u_int32_t sum, lastsum;
183 	int i;
184 
185 	sum = 0;
186 
187 	for (i=0; i<size; i++) {
188 		lastsum = sum;
189 		sum += htobe32(block[i]);
190 		if (sum < lastsum)
191 			++sum;
192 	}
193 
194 	return sum;
195 }
196