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