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