Home | History | Annotate | Line # | Download | only in arch
sun68k.c revision 1.15
      1 /*	$NetBSD: sun68k.c,v 1.15 2002/05/15 02:18:24 lukem Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998, 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matt Fredette, Paul Kranenburg, and Luke Mewburn.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(__RCSID) && !defined(__lint)
     41 __RCSID("$NetBSD: sun68k.c,v 1.15 2002/05/15 02:18:24 lukem Exp $");
     42 #endif	/* !__lint */
     43 
     44 #if HAVE_CONFIG_H
     45 #include "config.h"
     46 #endif
     47 
     48 #include <sys/param.h>
     49 
     50 #include <assert.h>
     51 #include <err.h>
     52 #include <stddef.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <unistd.h>
     57 
     58 #include "installboot.h"
     59 
     60 int
     61 sun68k_clearboot(ib_params *params)
     62 {
     63 	char	bb[SUN68K_BOOT_BLOCK_MAX_SIZE];
     64 	ssize_t	rv;
     65 
     66 	assert(params != NULL);
     67 	assert(params->fsfd != -1);
     68 	assert(params->filesystem != NULL);
     69 
     70 	if (params->flags & IB_STAGE2START) {
     71 		warnx("Can't use `-B bno' with `-c'");
     72 		return (0);
     73 	}
     74 	if (params->flags & IB_STAGE1START) {
     75 		warnx("`-b bno' is not supported for %s",
     76 		    params->machine->name);
     77 		return (0);
     78 	}
     79 
     80 	/* first check that it _could_ exist here */
     81 	rv = pread(params->fsfd, &bb, sizeof(bb), SUN68K_BOOT_BLOCK_OFFSET);
     82 	if (rv == -1) {
     83 		warn("Reading `%s'", params->filesystem);
     84 		return (0);
     85 	} else if (rv != sizeof(bb)) {
     86 		warnx("Reading `%s': short read", params->filesystem);
     87 		return (0);
     88 	}
     89 
     90 	/* now clear it out to nothing */
     91 	memset(&bb, 0, sizeof(bb));
     92 
     93 	if (params->flags & IB_VERBOSE)
     94 		printf("%slearing boot block\n",
     95 		    (params->flags & IB_NOWRITE) ? "Not c" : "C");
     96 	if (params->flags & IB_NOWRITE)
     97 		return (1);
     98 
     99 	rv = pwrite(params->fsfd, &bb, sizeof(bb), SUN68K_BOOT_BLOCK_OFFSET);
    100 	if (rv == -1) {
    101 		warn("Writing `%s'", params->filesystem);
    102 		return (0);
    103 	} else if (rv != sizeof(bb)) {
    104 		warnx("Writing `%s': short write", params->filesystem);
    105 		return (0);
    106 	}
    107 
    108 	return (1);
    109 }
    110 
    111 int
    112 sun68k_setboot(ib_params *params)
    113 {
    114 	char		bb[SUN68K_BOOT_BLOCK_MAX_SIZE];
    115 	int		retval;
    116 	ssize_t		rv;
    117 	size_t		bbi;
    118 	struct sun68k_bbinfo	*bbinfop;	/* bbinfo in prototype image */
    119 	uint32_t	maxblk, nblk, blk_i;
    120 	ib_block	*blocks;
    121 
    122 	assert(params != NULL);
    123 	assert(params->fsfd != -1);
    124 	assert(params->filesystem != NULL);
    125 	assert(params->fstype != NULL);
    126 	assert(params->s1fd != -1);
    127 	assert(params->stage1 != NULL);
    128 	assert(SUN68K_BBINFO_MAGICSIZE == 32);
    129 
    130 	retval = 0;
    131 	blocks = NULL;
    132 
    133 	if (params->stage2 == NULL) {
    134 		warnx("You must provide the name of the secondary bootstrap");
    135 		goto done;
    136 	}
    137 	if (params->flags & IB_STAGE1START) {
    138 		warnx("`-b bno' is not supported for %s",
    139 		    params->machine->name);
    140 		goto done;
    141 	}
    142 
    143 	if (params->s1stat.st_size > sizeof(bb)) {
    144 		warnx("`%s' cannot be larger than %lu bytes",
    145 		    params->stage1, (unsigned long)sizeof(bb));
    146 		goto done;
    147 	}
    148 
    149 	memset(&bb, 0, sizeof(bb));
    150 	rv = read(params->s1fd, &bb, sizeof(bb));
    151 	if (rv == -1) {
    152 		warn("Reading `%s'", params->stage1);
    153 		goto done;
    154 	}
    155 
    156 	/*
    157 	 * Quick sanity check that the bootstrap given
    158 	 * is *not* an ELF executable.
    159 	 */
    160 	if (memcmp(bb + 1, "ELF", strlen("ELF")) == 0) {
    161 		warnx("`%s' is an ELF executable; need raw binary",
    162 		    params->stage1);
    163 		goto done;
    164 	}
    165 
    166 	/* Look for the bbinfo structure. */
    167 	for (bbi = 0; bbi < sizeof(bb); bbi += sizeof(uint32_t)) {
    168 		bbinfop = (void *) (bb + bbi);
    169 		if (memcmp(bbinfop->bbi_magic, SUN68K_BBINFO_MAGIC,
    170 			    SUN68K_BBINFO_MAGICSIZE) == 0)
    171 			break;
    172 	}
    173 	if (bbi >= sizeof(bb)) {
    174 		warnx("`%s' does not have a bbinfo structure\n",
    175 		    params->stage1);
    176 		goto done;
    177 	}
    178 	maxblk = be32toh(bbinfop->bbi_block_count);
    179 	if (maxblk == 0 || maxblk > (sizeof(bb) / sizeof(uint32_t))) {
    180 		warnx("bbinfo structure in `%s' has preposterous size `%u'",
    181 		    params->stage1, maxblk);
    182 		goto done;
    183 	}
    184 
    185 	/* Allocate space for our block list. */
    186 	blocks = malloc(sizeof(*blocks) * maxblk);
    187 	if (blocks == NULL) {
    188 		warn("Allocating %lu bytes",
    189 		    (unsigned long) sizeof(*blocks) * maxblk);
    190 		goto done;
    191 	}
    192 
    193 	if (S_ISREG(params->fsstat.st_mode)) {
    194 		if (fsync(params->fsfd) == -1)
    195 			warn("Synchronising file system `%s'",
    196 			    params->filesystem);
    197 	} else {
    198 		/* Ensure the secondary bootstrap is on disk. */
    199 		sync();
    200 	}
    201 
    202 	/* Collect the blocks for the secondary bootstrap. */
    203 	nblk = maxblk;
    204 	if (! params->fstype->findstage2(params, &nblk, blocks))
    205 		goto done;
    206 	if (nblk == 0) {
    207 		warnx("Secondary bootstrap `%s' is empty",
    208 		   params->stage2);
    209 		goto done;
    210 	}
    211 
    212 	/* Save those blocks in the primary bootstrap. */
    213 	bbinfop->bbi_block_count = htobe32(nblk);
    214 	bbinfop->bbi_block_size = htobe32(blocks[0].blocksize);
    215 	for (blk_i = 0; blk_i < nblk; blk_i++) {
    216 		bbinfop->bbi_block_table[blk_i] =
    217 		    htobe32(blocks[blk_i].block);
    218 		if (blocks[blk_i].blocksize < blocks[0].blocksize &&
    219 		    blk_i + 1 != nblk) {
    220 			warnx("Secondary bootstrap `%s' blocks do not have " \
    221 			    "a uniform size\n", params->stage2);
    222 			goto done;
    223 		}
    224 	}
    225 
    226 	if (params->flags & IB_VERBOSE) {
    227 		printf("Bootstrap start sector: %u\n",
    228 		    SUN68K_BOOT_BLOCK_OFFSET / SUN68K_BOOT_BLOCK_BLOCKSIZE);
    229 		printf("Bootstrap byte count:   %u\n", (unsigned)rv);
    230 		printf("Bootstrap block table:  %u entries of %u bytes available, %u used:",
    231 		    maxblk, blocks[0].blocksize, nblk);
    232 		for (blk_i = 0; blk_i < nblk; blk_i++)
    233 			printf(" %u", blocks[blk_i].block);
    234 		printf("\n%sriting bootstrap\n",
    235 		    (params->flags & IB_NOWRITE) ? "Not w" : "W");
    236 	}
    237 	if (params->flags & IB_NOWRITE) {
    238 		retval = 1;
    239 		goto done;
    240 	}
    241 
    242 	rv = pwrite(params->fsfd, &bb, sizeof(bb), SUN68K_BOOT_BLOCK_OFFSET);
    243 	if (rv == -1) {
    244 		warn("Writing `%s'", params->filesystem);
    245 		goto done;
    246 	} else if (rv != sizeof(bb)) {
    247 		warnx("Writing `%s': short write", params->filesystem);
    248 		goto done;
    249 	} else {
    250 		retval = 1;
    251 	}
    252 
    253  done:
    254 	if (blocks != NULL)
    255 		free (blocks);
    256 	return (retval);
    257 }
    258