Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.sbin / installboot / arch / amiga.c
blobc645deb6f78b3531c0674739ffaee7b3b69bcf98
1 /* $NetBSD: amiga.c,v 1.5 2006/02/18 10:08:07 dsl Exp $ */
3 /*-
4 * Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Michael Hitch.
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Luke Mewburn of Wasabi Systems.
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.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
37 #endif
39 #include <sys/cdefs.h>
40 #if defined(__RCSID) && !defined(__lint)
41 __RCSID("$NetBSD: amiga.c,v 1.5 2006/02/18 10:08:07 dsl Exp $");
42 #endif /* !__lint */
44 #include <sys/param.h>
45 #include <sys/stat.h>
47 #include <assert.h>
48 #include <err.h>
49 #include <stddef.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 #include <string.h>
55 #include "installboot.h"
57 /* XXX Must be kept in sync with bbstart.s! */
58 #define CMDLN_LOC 0x10
59 #define CMDLN_LEN 0x20
61 #define CHKSUMOFFS 1
63 u_int32_t chksum(u_int32_t *, int);
65 static int amiga_setboot(ib_params *);
67 struct ib_mach ib_mach_amiga =
68 { "amiga", amiga_setboot, no_clearboot, no_editboot,
69 IB_STAGE1START | IB_STAGE2START | IB_COMMAND };
71 static int
72 amiga_setboot(ib_params *params)
74 int retval;
75 ssize_t rv;
76 char *dline;
77 int sumlen;
78 u_int32_t sum2, sum16;
80 struct stat bootstrapsb;
82 u_int32_t block[128*16];
84 retval = 0;
85 if (fstat(params->s1fd, &bootstrapsb) == -1) {
86 warn("Examining `%s'", params->stage1);
87 goto done;
89 if (!S_ISREG(bootstrapsb.st_mode)) {
90 warnx("`%s' must be a regular file", params->stage1);
91 goto done;
94 rv = pread(params->s1fd, &block, sizeof(block), 0);
95 if (rv == -1) {
96 warn("Reading `%s'", params->stage1);
97 goto done;
98 } else if (rv != sizeof(block)) {
99 warnx("Reading `%s': short read", params->stage1);
100 goto done;
103 /* XXX the choices should not be hardcoded */
105 sum2 = chksum(block, 1024/4);
106 sum16 = chksum(block, 8192/4);
108 if (sum16 == 0xffffffff) {
109 sumlen = 8192/4;
110 } else if (sum2 == 0xffffffff) {
111 sumlen = 1024/4;
112 } else {
113 errx(1, "%s: wrong checksum", params->stage1);
114 /* NOTREACHED */
117 if (sum2 == sum16) {
118 warnx("eek - both sums are the same");
121 if (params->flags & IB_COMMAND) {
122 dline = (char *)&(block[CMDLN_LOC/4]);
123 /* XXX keep the default default line in sync with bbstart.s */
124 if (strcmp(dline, "netbsd -ASn2") != 0) {
125 errx(1, "Old bootblock version? Can't change command line.");
127 (void)strncpy(dline, params->command, CMDLN_LEN-1);
129 block[1] = 0;
130 block[1] = 0xffffffff - chksum(block, sumlen);
133 if (params->flags & IB_NOWRITE) {
134 retval = 1;
135 goto done;
138 if (params->flags & IB_VERBOSE)
139 printf("Writing boot block\n");
140 rv = pwrite(params->fsfd, &block, sizeof(block), 0);
141 if (rv == -1) {
142 warn("Writing `%s'", params->filesystem);
143 goto done;
144 } else if (rv != sizeof(block)) {
145 warnx("Writing `%s': short write", params->filesystem);
146 goto done;
147 } else {
148 retval = 1;
151 done:
152 return (retval);
155 u_int32_t
156 chksum(block, size)
157 u_int32_t *block;
158 int size;
160 u_int32_t sum, lastsum;
161 int i;
163 sum = 0;
165 for (i=0; i<size; i++) {
166 lastsum = sum;
167 sum += htobe32(block[i]);
168 if (sum < lastsum)
169 ++sum;
172 return sum;