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