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