Home | History | Annotate | Line # | Download | only in arch
amiga.c revision 1.1
      1  1.1  mhitch /*	$NetBSD: amiga.c,v 1.1 2003/01/15 06:33:13 mhitch 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.1  mhitch #include <sys/cdefs.h>
     43  1.1  mhitch #if defined(__RCSID) && !defined(__lint)
     44  1.1  mhitch __RCSID("$NetBSD: amiga.c,v 1.1 2003/01/15 06:33:13 mhitch Exp $");
     45  1.1  mhitch #endif	/* !__lint */
     46  1.1  mhitch 
     47  1.1  mhitch #include <sys/param.h>
     48  1.1  mhitch #include <sys/stat.h>
     49  1.1  mhitch 
     50  1.1  mhitch #include <assert.h>
     51  1.1  mhitch #include <err.h>
     52  1.1  mhitch #include <stddef.h>
     53  1.1  mhitch #include <stdio.h>
     54  1.1  mhitch #include <stdlib.h>
     55  1.1  mhitch #include <unistd.h>
     56  1.1  mhitch #include <string.h>
     57  1.1  mhitch 
     58  1.1  mhitch #include "installboot.h"
     59  1.1  mhitch 
     60  1.1  mhitch /* XXX Must be kept in sync with bbstart.s! */
     61  1.1  mhitch #define CMDLN_LOC 0x10
     62  1.1  mhitch #define CMDLN_LEN 0x20
     63  1.1  mhitch 
     64  1.1  mhitch char command[CMDLN_LEN];
     65  1.1  mhitch 
     66  1.1  mhitch #define CHKSUMOFFS 1
     67  1.1  mhitch 
     68  1.1  mhitch u_int32_t chksum(u_int32_t *, int);
     69  1.1  mhitch 
     70  1.1  mhitch int		amiga_parseopt(ib_params *, const char *);
     71  1.1  mhitch int		amiga_setboot(ib_params *);
     72  1.1  mhitch int		amiga_clearboot(ib_params *);
     73  1.1  mhitch 
     74  1.1  mhitch int
     75  1.1  mhitch amiga_parseopt(ib_params *params, const char *option)
     76  1.1  mhitch {
     77  1.1  mhitch 
     78  1.1  mhitch 	if (strncmp("command=", option, strlen("command=")) == 0) {
     79  1.1  mhitch 		strncpy(command, option + strlen("command="), CMDLN_LEN);
     80  1.1  mhitch 		params->flags |= IB_COMMAND;
     81  1.1  mhitch 		return (1);
     82  1.1  mhitch 	}
     83  1.1  mhitch 
     84  1.1  mhitch 	warnx("Unknown -o option `%s'", option);
     85  1.1  mhitch 	return (0);
     86  1.1  mhitch }
     87  1.1  mhitch 
     88  1.1  mhitch int
     89  1.1  mhitch amiga_clearboot(ib_params *params)
     90  1.1  mhitch {
     91  1.1  mhitch 	return 0;
     92  1.1  mhitch }
     93  1.1  mhitch 
     94  1.1  mhitch int
     95  1.1  mhitch amiga_setboot(ib_params *params)
     96  1.1  mhitch {
     97  1.1  mhitch 	int retval;
     98  1.1  mhitch 	ssize_t			rv;
     99  1.1  mhitch 	char *dline;
    100  1.1  mhitch 	int sumlen;
    101  1.1  mhitch 	u_int32_t sum2, sum16;
    102  1.1  mhitch 
    103  1.1  mhitch 	struct stat		bootstrapsb;
    104  1.1  mhitch 
    105  1.1  mhitch 	u_int32_t block[128*16];
    106  1.1  mhitch 
    107  1.1  mhitch 	if (fstat(params->s1fd, &bootstrapsb) == -1) {
    108  1.1  mhitch 		warn("Examining `%s'", params->stage1);
    109  1.1  mhitch 		goto done;
    110  1.1  mhitch 	}
    111  1.1  mhitch 	if (!S_ISREG(bootstrapsb.st_mode)) {
    112  1.1  mhitch 		warnx("`%s' must be a regular file", params->stage1);
    113  1.1  mhitch 		goto done;
    114  1.1  mhitch 	}
    115  1.1  mhitch 
    116  1.1  mhitch 	rv = pread(params->s1fd, &block, sizeof(block), 0);
    117  1.1  mhitch 	if (rv == -1) {
    118  1.1  mhitch 		warn("Reading `%s'", params->stage1);
    119  1.1  mhitch 		goto done;
    120  1.1  mhitch 	} else if (rv != sizeof(block)) {
    121  1.1  mhitch 		warnx("Reading `%s': short read", params->stage1);
    122  1.1  mhitch 		goto done;
    123  1.1  mhitch 	}
    124  1.1  mhitch 
    125  1.1  mhitch 	/* XXX the choices should not be hardcoded */
    126  1.1  mhitch 
    127  1.1  mhitch 	sum2  = chksum(block, 1024/4);
    128  1.1  mhitch 	sum16 = chksum(block, 8192/4);
    129  1.1  mhitch 
    130  1.1  mhitch 	if (sum16 == 0xffffffff) {
    131  1.1  mhitch 		sumlen = 8192/4;
    132  1.1  mhitch 	} else if (sum2 == 0xffffffff) {
    133  1.1  mhitch 		sumlen = 1024/4;
    134  1.1  mhitch 	} else {
    135  1.1  mhitch 		errx(1, "%s: wrong checksum", params->stage1);
    136  1.1  mhitch 		/* NOTREACHED */
    137  1.1  mhitch 	}
    138  1.1  mhitch 
    139  1.1  mhitch 	if (sum2 == sum16) {
    140  1.1  mhitch 		warnx("eek - both sums are the same");
    141  1.1  mhitch 	}
    142  1.1  mhitch 
    143  1.1  mhitch 	if (params->flags & IB_COMMAND) {
    144  1.1  mhitch 		dline = (char *)&(block[CMDLN_LOC/4]);
    145  1.1  mhitch 		/* XXX keep the default default line in sync with bbstart.s */
    146  1.1  mhitch 		if (strcmp(dline, "netbsd -ASn2") != 0) {
    147  1.1  mhitch 			errx(1, "Old bootblock version? Can't change command line.");
    148  1.1  mhitch 		}
    149  1.1  mhitch 		(void)strncpy(dline, command, CMDLN_LEN-1);
    150  1.1  mhitch 
    151  1.1  mhitch 		block[1] = 0;
    152  1.1  mhitch 		block[1] = 0xffffffff - chksum(block, sumlen);
    153  1.1  mhitch 	}
    154  1.1  mhitch 
    155  1.1  mhitch 	if (params->flags & IB_NOWRITE) {
    156  1.1  mhitch 		retval = 1;
    157  1.1  mhitch 		goto done;
    158  1.1  mhitch 	}
    159  1.1  mhitch 
    160  1.1  mhitch 	if (params->flags & IB_VERBOSE)
    161  1.1  mhitch 		printf("Writing boot block\n");
    162  1.1  mhitch 	rv = pwrite(params->fsfd, &block, sizeof(block), 0);
    163  1.1  mhitch 	if (rv == -1) {
    164  1.1  mhitch 		warn("Writing `%s'", params->filesystem);
    165  1.1  mhitch 		goto done;
    166  1.1  mhitch 	} else if (rv != sizeof(block)) {
    167  1.1  mhitch 		warnx("Writing `%s': short write", params->filesystem);
    168  1.1  mhitch 		goto done;
    169  1.1  mhitch 	} else {
    170  1.1  mhitch 		retval = 1;
    171  1.1  mhitch 	}
    172  1.1  mhitch 
    173  1.1  mhitch  done:
    174  1.1  mhitch 	return (retval);
    175  1.1  mhitch }
    176  1.1  mhitch 
    177  1.1  mhitch u_int32_t
    178  1.1  mhitch chksum(block, size)
    179  1.1  mhitch 	u_int32_t *block;
    180  1.1  mhitch 	int size;
    181  1.1  mhitch {
    182  1.1  mhitch 	u_int32_t sum, lastsum;
    183  1.1  mhitch 	int i;
    184  1.1  mhitch 
    185  1.1  mhitch 	sum = 0;
    186  1.1  mhitch 
    187  1.1  mhitch 	for (i=0; i<size; i++) {
    188  1.1  mhitch 		lastsum = sum;
    189  1.1  mhitch 		sum += htobe32(block[i]);
    190  1.1  mhitch 		if (sum < lastsum)
    191  1.1  mhitch 			++sum;
    192  1.1  mhitch 	}
    193  1.1  mhitch 
    194  1.1  mhitch 	return sum;
    195  1.1  mhitch }
    196