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