Home | History | Annotate | Line # | Download | only in arch
amiga.c revision 1.9
      1  1.9  mlelstv /*	$NetBSD: amiga.c,v 1.9 2015/06/05 05:02:48 mlelstv 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  *
     22  1.1   mhitch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  1.1   mhitch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  1.1   mhitch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  1.1   mhitch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  1.1   mhitch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  1.1   mhitch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  1.1   mhitch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  1.1   mhitch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  1.1   mhitch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  1.1   mhitch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  1.1   mhitch  * POSSIBILITY OF SUCH DAMAGE.
     33  1.1   mhitch  */
     34  1.1   mhitch 
     35  1.3      jmc #if HAVE_NBTOOL_CONFIG_H
     36  1.3      jmc #include "nbtool_config.h"
     37  1.3      jmc #endif
     38  1.3      jmc 
     39  1.1   mhitch #include <sys/cdefs.h>
     40  1.7  tsutsui #if !defined(__lint)
     41  1.9  mlelstv __RCSID("$NetBSD: amiga.c,v 1.9 2015/06/05 05:02:48 mlelstv Exp $");
     42  1.1   mhitch #endif	/* !__lint */
     43  1.1   mhitch 
     44  1.1   mhitch #include <sys/param.h>
     45  1.1   mhitch #include <sys/stat.h>
     46  1.1   mhitch 
     47  1.1   mhitch #include <assert.h>
     48  1.1   mhitch #include <err.h>
     49  1.1   mhitch #include <stddef.h>
     50  1.1   mhitch #include <stdio.h>
     51  1.1   mhitch #include <stdlib.h>
     52  1.1   mhitch #include <unistd.h>
     53  1.1   mhitch #include <string.h>
     54  1.1   mhitch 
     55  1.1   mhitch #include "installboot.h"
     56  1.1   mhitch 
     57  1.1   mhitch /* XXX Must be kept in sync with bbstart.s! */
     58  1.1   mhitch #define CMDLN_LOC 0x10
     59  1.1   mhitch #define CMDLN_LEN 0x20
     60  1.1   mhitch 
     61  1.1   mhitch #define CHKSUMOFFS 1
     62  1.1   mhitch 
     63  1.1   mhitch u_int32_t chksum(u_int32_t *, int);
     64  1.1   mhitch 
     65  1.5      dsl static int amiga_setboot(ib_params *);
     66  1.1   mhitch 
     67  1.5      dsl struct ib_mach ib_mach_amiga =
     68  1.5      dsl 	{ "amiga", amiga_setboot, no_clearboot, no_editboot,
     69  1.5      dsl 		IB_STAGE1START | IB_STAGE2START | IB_COMMAND };
     70  1.1   mhitch 
     71  1.5      dsl static int
     72  1.1   mhitch amiga_setboot(ib_params *params)
     73  1.1   mhitch {
     74  1.1   mhitch 	int retval;
     75  1.1   mhitch 	ssize_t			rv;
     76  1.1   mhitch 	char *dline;
     77  1.1   mhitch 	int sumlen;
     78  1.1   mhitch 	u_int32_t sum2, sum16;
     79  1.8  msaitoh 
     80  1.1   mhitch 	struct stat		bootstrapsb;
     81  1.1   mhitch 
     82  1.1   mhitch 	u_int32_t block[128*16];
     83  1.1   mhitch 
     84  1.4    lukem 	retval = 0;
     85  1.1   mhitch 	if (fstat(params->s1fd, &bootstrapsb) == -1) {
     86  1.1   mhitch 		warn("Examining `%s'", params->stage1);
     87  1.1   mhitch 		goto done;
     88  1.1   mhitch 	}
     89  1.1   mhitch 	if (!S_ISREG(bootstrapsb.st_mode)) {
     90  1.1   mhitch 		warnx("`%s' must be a regular file", params->stage1);
     91  1.1   mhitch 		goto done;
     92  1.1   mhitch 	}
     93  1.1   mhitch 
     94  1.1   mhitch 	rv = pread(params->s1fd, &block, sizeof(block), 0);
     95  1.1   mhitch 	if (rv == -1) {
     96  1.1   mhitch 		warn("Reading `%s'", params->stage1);
     97  1.1   mhitch 		goto done;
     98  1.1   mhitch 	} else if (rv != sizeof(block)) {
     99  1.1   mhitch 		warnx("Reading `%s': short read", params->stage1);
    100  1.1   mhitch 		goto done;
    101  1.1   mhitch 	}
    102  1.1   mhitch 
    103  1.1   mhitch 	/* XXX the choices should not be hardcoded */
    104  1.1   mhitch 
    105  1.1   mhitch 	sum2  = chksum(block, 1024/4);
    106  1.1   mhitch 	sum16 = chksum(block, 8192/4);
    107  1.1   mhitch 
    108  1.1   mhitch 	if (sum16 == 0xffffffff) {
    109  1.1   mhitch 		sumlen = 8192/4;
    110  1.1   mhitch 	} else if (sum2 == 0xffffffff) {
    111  1.1   mhitch 		sumlen = 1024/4;
    112  1.1   mhitch 	} else {
    113  1.1   mhitch 		errx(1, "%s: wrong checksum", params->stage1);
    114  1.1   mhitch 		/* NOTREACHED */
    115  1.1   mhitch 	}
    116  1.1   mhitch 
    117  1.1   mhitch 	if (sum2 == sum16) {
    118  1.1   mhitch 		warnx("eek - both sums are the same");
    119  1.1   mhitch 	}
    120  1.1   mhitch 
    121  1.1   mhitch 	if (params->flags & IB_COMMAND) {
    122  1.1   mhitch 		dline = (char *)&(block[CMDLN_LOC/4]);
    123  1.1   mhitch 		/* XXX keep the default default line in sync with bbstart.s */
    124  1.1   mhitch 		if (strcmp(dline, "netbsd -ASn2") != 0) {
    125  1.1   mhitch 			errx(1, "Old bootblock version? Can't change command line.");
    126  1.1   mhitch 		}
    127  1.2      dsl 		(void)strncpy(dline, params->command, CMDLN_LEN-1);
    128  1.1   mhitch 
    129  1.9  mlelstv 		block[1] = htobe32(0);
    130  1.9  mlelstv 		block[1] = htobe32(0xffffffff - chksum(block, sumlen));
    131  1.1   mhitch 	}
    132  1.1   mhitch 
    133  1.1   mhitch 	if (params->flags & IB_NOWRITE) {
    134  1.1   mhitch 		retval = 1;
    135  1.1   mhitch 		goto done;
    136  1.1   mhitch 	}
    137  1.1   mhitch 
    138  1.1   mhitch 	if (params->flags & IB_VERBOSE)
    139  1.1   mhitch 		printf("Writing boot block\n");
    140  1.1   mhitch 	rv = pwrite(params->fsfd, &block, sizeof(block), 0);
    141  1.1   mhitch 	if (rv == -1) {
    142  1.1   mhitch 		warn("Writing `%s'", params->filesystem);
    143  1.1   mhitch 		goto done;
    144  1.1   mhitch 	} else if (rv != sizeof(block)) {
    145  1.1   mhitch 		warnx("Writing `%s': short write", params->filesystem);
    146  1.1   mhitch 		goto done;
    147  1.1   mhitch 	} else {
    148  1.1   mhitch 		retval = 1;
    149  1.1   mhitch 	}
    150  1.1   mhitch 
    151  1.1   mhitch  done:
    152  1.1   mhitch 	return (retval);
    153  1.1   mhitch }
    154  1.1   mhitch 
    155  1.1   mhitch u_int32_t
    156  1.1   mhitch chksum(block, size)
    157  1.1   mhitch 	u_int32_t *block;
    158  1.1   mhitch 	int size;
    159  1.1   mhitch {
    160  1.1   mhitch 	u_int32_t sum, lastsum;
    161  1.1   mhitch 	int i;
    162  1.1   mhitch 
    163  1.1   mhitch 	sum = 0;
    164  1.1   mhitch 
    165  1.1   mhitch 	for (i=0; i<size; i++) {
    166  1.1   mhitch 		lastsum = sum;
    167  1.9  mlelstv 		sum += be32toh(block[i]);
    168  1.1   mhitch 		if (sum < lastsum)
    169  1.1   mhitch 			++sum;
    170  1.1   mhitch 	}
    171  1.1   mhitch 
    172  1.1   mhitch 	return sum;
    173  1.1   mhitch }
    174