xref: /netbsd-src/usr.sbin/installboot/arch/amiga.c (revision 4b896b232495b7a9b8b94a1cf1e21873296d53b8)
1 /*	$NetBSD: amiga.c,v 1.2 2003/04/15 14:22:14 dsl 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.2 2003/04/15 14:22:14 dsl 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 #define CHKSUMOFFS 1
65 
66 u_int32_t chksum(u_int32_t *, int);
67 
68 int		amiga_setboot(ib_params *);
69 int		amiga_clearboot(ib_params *);
70 
71 int
72 amiga_clearboot(ib_params *params)
73 {
74 	return 0;
75 }
76 
77 int
78 amiga_setboot(ib_params *params)
79 {
80 	int retval;
81 	ssize_t			rv;
82 	char *dline;
83 	int sumlen;
84 	u_int32_t sum2, sum16;
85 
86 	struct stat		bootstrapsb;
87 
88 	u_int32_t block[128*16];
89 
90 	if (fstat(params->s1fd, &bootstrapsb) == -1) {
91 		warn("Examining `%s'", params->stage1);
92 		goto done;
93 	}
94 	if (!S_ISREG(bootstrapsb.st_mode)) {
95 		warnx("`%s' must be a regular file", params->stage1);
96 		goto done;
97 	}
98 
99 	rv = pread(params->s1fd, &block, sizeof(block), 0);
100 	if (rv == -1) {
101 		warn("Reading `%s'", params->stage1);
102 		goto done;
103 	} else if (rv != sizeof(block)) {
104 		warnx("Reading `%s': short read", params->stage1);
105 		goto done;
106 	}
107 
108 	/* XXX the choices should not be hardcoded */
109 
110 	sum2  = chksum(block, 1024/4);
111 	sum16 = chksum(block, 8192/4);
112 
113 	if (sum16 == 0xffffffff) {
114 		sumlen = 8192/4;
115 	} else if (sum2 == 0xffffffff) {
116 		sumlen = 1024/4;
117 	} else {
118 		errx(1, "%s: wrong checksum", params->stage1);
119 		/* NOTREACHED */
120 	}
121 
122 	if (sum2 == sum16) {
123 		warnx("eek - both sums are the same");
124 	}
125 
126 	if (params->flags & IB_COMMAND) {
127 		dline = (char *)&(block[CMDLN_LOC/4]);
128 		/* XXX keep the default default line in sync with bbstart.s */
129 		if (strcmp(dline, "netbsd -ASn2") != 0) {
130 			errx(1, "Old bootblock version? Can't change command line.");
131 		}
132 		(void)strncpy(dline, params->command, CMDLN_LEN-1);
133 
134 		block[1] = 0;
135 		block[1] = 0xffffffff - chksum(block, sumlen);
136 	}
137 
138 	if (params->flags & IB_NOWRITE) {
139 		retval = 1;
140 		goto done;
141 	}
142 
143 	if (params->flags & IB_VERBOSE)
144 		printf("Writing boot block\n");
145 	rv = pwrite(params->fsfd, &block, sizeof(block), 0);
146 	if (rv == -1) {
147 		warn("Writing `%s'", params->filesystem);
148 		goto done;
149 	} else if (rv != sizeof(block)) {
150 		warnx("Writing `%s': short write", params->filesystem);
151 		goto done;
152 	} else {
153 		retval = 1;
154 	}
155 
156  done:
157 	return (retval);
158 }
159 
160 u_int32_t
161 chksum(block, size)
162 	u_int32_t *block;
163 	int size;
164 {
165 	u_int32_t sum, lastsum;
166 	int i;
167 
168 	sum = 0;
169 
170 	for (i=0; i<size; i++) {
171 		lastsum = sum;
172 		sum += htobe32(block[i]);
173 		if (sum < lastsum)
174 			++sum;
175 	}
176 
177 	return sum;
178 }
179