Home | History | Annotate | Line # | Download | only in arch
x68k.c revision 1.4.62.1
      1  1.4.62.1  christos /*	$NetBSD: x68k.c,v 1.4.62.1 2019/06/10 22:10:30 christos Exp $	*/
      2       1.1     isaki 
      3       1.1     isaki /*-
      4       1.1     isaki  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5       1.1     isaki  * All rights reserved.
      6       1.1     isaki  *
      7       1.1     isaki  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1     isaki  * by Luke Mewburn of Wasabi Systems.
      9       1.1     isaki  *
     10       1.1     isaki  * Redistribution and use in source and binary forms, with or without
     11       1.1     isaki  * modification, are permitted provided that the following conditions
     12       1.1     isaki  * are met:
     13       1.1     isaki  * 1. Redistributions of source code must retain the above copyright
     14       1.1     isaki  *    notice, this list of conditions and the following disclaimer.
     15       1.1     isaki  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1     isaki  *    notice, this list of conditions and the following disclaimer in the
     17       1.1     isaki  *    documentation and/or other materials provided with the distribution.
     18       1.1     isaki  *
     19       1.1     isaki  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1     isaki  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1     isaki  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1     isaki  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1     isaki  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1     isaki  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1     isaki  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1     isaki  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1     isaki  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1     isaki  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1     isaki  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1     isaki  */
     31       1.1     isaki 
     32       1.2     lukem #if HAVE_NBTOOL_CONFIG_H
     33       1.2     lukem #include "nbtool_config.h"
     34       1.2     lukem #endif
     35       1.2     lukem 
     36       1.1     isaki #include <sys/cdefs.h>
     37       1.2     lukem #if !defined(__lint)
     38  1.4.62.1  christos __RCSID("$NetBSD: x68k.c,v 1.4.62.1 2019/06/10 22:10:30 christos Exp $");
     39       1.1     isaki #endif	/* !__lint */
     40       1.1     isaki 
     41       1.1     isaki #include <sys/param.h>
     42       1.1     isaki #include <sys/stat.h>
     43       1.1     isaki 
     44       1.1     isaki #include <assert.h>
     45       1.1     isaki #include <err.h>
     46       1.1     isaki #include <stddef.h>
     47       1.1     isaki #include <stdio.h>
     48       1.1     isaki #include <stdlib.h>
     49       1.1     isaki #include <string.h>
     50       1.1     isaki #include <unistd.h>
     51       1.1     isaki 
     52       1.1     isaki #include "installboot.h"
     53       1.1     isaki 
     54       1.1     isaki #define X68K_LABELOFFSET		64
     55       1.1     isaki #define X68K_LABELSIZE		404 /* reserve 16 partitions */
     56       1.1     isaki 
     57       1.1     isaki static int x68k_clearheader(ib_params *, struct bbinfo_params *, uint8_t *);
     58       1.1     isaki 
     59       1.3       dsl static int x68k_clearboot(ib_params *);
     60       1.3       dsl static int x68k_setboot(ib_params *);
     61       1.3       dsl 
     62  1.4.62.1  christos struct ib_mach ib_mach_x68k = {
     63  1.4.62.1  christos 	.name		=	"x68k",
     64  1.4.62.1  christos 	.setboot	=	x68k_setboot,
     65  1.4.62.1  christos 	.clearboot	=	x68k_clearboot,
     66  1.4.62.1  christos 	.editboot	=	no_editboot,
     67  1.4.62.1  christos 	.valid_flags	=	IB_STAGE1START | IB_STAGE2START,
     68  1.4.62.1  christos };
     69       1.1     isaki 
     70       1.1     isaki static struct bbinfo_params bbparams = {
     71       1.1     isaki 	X68K_BBINFO_MAGIC,
     72       1.1     isaki 	X68K_BOOT_BLOCK_OFFSET,
     73       1.1     isaki 	X68K_BOOT_BLOCK_BLOCKSIZE,
     74       1.1     isaki 	X68K_BOOT_BLOCK_MAX_SIZE,
     75       1.1     isaki 	X68K_LABELOFFSET + X68K_LABELSIZE, /* XXX */
     76       1.1     isaki 	BBINFO_BIG_ENDIAN,
     77       1.1     isaki };
     78       1.1     isaki 
     79       1.3       dsl static int
     80       1.1     isaki x68k_clearboot(ib_params *params)
     81       1.1     isaki {
     82       1.1     isaki 
     83       1.1     isaki 	assert(params != NULL);
     84       1.1     isaki 
     85       1.1     isaki 	if (params->flags & IB_STAGE1START) {
     86       1.1     isaki 		warnx("`-b bno' is not supported for %s",
     87       1.1     isaki 			params->machine->name);
     88       1.1     isaki 		return 0;
     89       1.1     isaki 	}
     90       1.1     isaki 	return shared_bbinfo_clearboot(params, &bbparams, x68k_clearheader);
     91       1.1     isaki }
     92       1.1     isaki 
     93       1.1     isaki static int
     94       1.1     isaki x68k_clearheader(ib_params *params, struct bbinfo_params *bb_params,
     95       1.1     isaki 	uint8_t *bb)
     96       1.1     isaki {
     97       1.1     isaki 
     98       1.1     isaki 	assert(params != NULL);
     99       1.1     isaki 	assert(bb_params != NULL);
    100       1.1     isaki 	assert(bb != NULL);
    101       1.1     isaki 
    102       1.1     isaki 	memset(bb, 0, X68K_LABELOFFSET);
    103       1.1     isaki 	return 1;
    104       1.1     isaki }
    105       1.1     isaki 
    106       1.3       dsl static int
    107       1.1     isaki x68k_setboot(ib_params *params)
    108       1.1     isaki {
    109       1.1     isaki 	struct stat	bootstrapsb;
    110       1.1     isaki 	char		bb[X68K_BOOT_BLOCK_MAX_SIZE];
    111       1.1     isaki 	char		label[X68K_LABELSIZE];
    112       1.1     isaki 	uint32_t	s1start;
    113       1.1     isaki 	int		retval;
    114       1.1     isaki 	ssize_t		rv;
    115       1.1     isaki 
    116       1.1     isaki 	assert(params != NULL);
    117       1.1     isaki 	assert(params->fsfd != -1);
    118       1.1     isaki 	assert(params->filesystem != NULL);
    119       1.1     isaki 	assert(params->s1fd != -1);
    120       1.1     isaki 	assert(params->stage1 != NULL);
    121       1.1     isaki 
    122       1.1     isaki 	retval = 0;
    123       1.1     isaki 
    124       1.1     isaki 	if (params->flags & IB_STAGE1START)
    125       1.1     isaki 		s1start = params->s1start;
    126       1.1     isaki 	else
    127       1.1     isaki 		s1start = X68K_BOOT_BLOCK_OFFSET /
    128       1.1     isaki 		    X68K_BOOT_BLOCK_BLOCKSIZE;
    129       1.1     isaki 
    130       1.1     isaki 	/* read disklabel on the target disk */
    131       1.1     isaki 	rv = pread(params->fsfd, label, sizeof label,
    132       1.1     isaki 		   s1start * X68K_BOOT_BLOCK_BLOCKSIZE + X68K_LABELOFFSET);
    133       1.1     isaki 	if (rv == -1) {
    134       1.1     isaki 		warn("Reading `%s'", params->filesystem);
    135       1.1     isaki 		goto done;
    136       1.1     isaki 	} else if (rv != sizeof label) {
    137       1.1     isaki 		warnx("Reading `%s': short read", params->filesystem);
    138       1.1     isaki 		goto done;
    139       1.1     isaki 	}
    140       1.1     isaki 
    141       1.1     isaki 	if (fstat(params->s1fd, &bootstrapsb) == -1) {
    142       1.1     isaki 		warn("Examining `%s'", params->stage1);
    143       1.1     isaki 		goto done;
    144       1.1     isaki 	}
    145       1.1     isaki 	if (!S_ISREG(bootstrapsb.st_mode)) {
    146       1.1     isaki 		warnx("`%s' must be a regular file", params->stage1);
    147       1.1     isaki 		goto done;
    148       1.1     isaki 	}
    149       1.1     isaki 
    150       1.1     isaki 	/* read boot loader */
    151       1.1     isaki 	memset(&bb, 0, sizeof bb);
    152       1.1     isaki 	rv = read(params->s1fd, &bb, sizeof bb);
    153       1.1     isaki 	if (rv == -1) {
    154       1.1     isaki 		warn("Reading `%s'", params->stage1);
    155       1.1     isaki 		goto done;
    156       1.1     isaki 	}
    157       1.1     isaki 	/* then, overwrite disklabel */
    158       1.1     isaki 	memcpy(&bb[X68K_LABELOFFSET], &label, sizeof label);
    159       1.1     isaki 
    160       1.1     isaki 	if (params->flags & IB_VERBOSE) {
    161       1.1     isaki 		printf("Bootstrap start sector: %#x\n", s1start);
    162       1.1     isaki 		printf("Bootstrap byte count:   %#x\n", (unsigned)rv);
    163       1.1     isaki 		printf("%sriting bootstrap\n",
    164       1.1     isaki 		    (params->flags & IB_NOWRITE) ? "Not w" : "W");
    165       1.1     isaki 	}
    166       1.1     isaki 	if (params->flags & IB_NOWRITE) {
    167       1.1     isaki 		retval = 1;
    168       1.1     isaki 		goto done;
    169       1.1     isaki 	}
    170       1.1     isaki 
    171       1.1     isaki 	/* write boot loader and disklabel into the target disk */
    172       1.1     isaki 	rv = pwrite(params->fsfd, &bb, X68K_BOOT_BLOCK_MAX_SIZE,
    173       1.1     isaki 	    s1start * X68K_BOOT_BLOCK_BLOCKSIZE);
    174       1.1     isaki 	if (rv == -1) {
    175       1.1     isaki 		warn("Writing `%s'", params->filesystem);
    176       1.1     isaki 		goto done;
    177       1.1     isaki 	} else if (rv != X68K_BOOT_BLOCK_MAX_SIZE) {
    178       1.1     isaki 		warnx("Writing `%s': short write", params->filesystem);
    179       1.1     isaki 		goto done;
    180       1.1     isaki 	} else
    181       1.1     isaki 		retval = 1;
    182       1.1     isaki 
    183       1.1     isaki  done:
    184       1.1     isaki 	return (retval);
    185       1.1     isaki }
    186