Home | History | Annotate | Line # | Download | only in arch
sun68k.c revision 1.13
      1 /*	$NetBSD: sun68k.c,v 1.13 2002/05/14 06:40:33 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.13 2002/05/14 06:40:33 lukem Exp $");
     42 #endif	/* !__lint */
     43 
     44 #if HAVE_CONFIG_H
     45 #include "config.h"
     46 #endif
     47 
     48 #include <sys/param.h>
     49 #include <sys/stat.h>
     50 
     51 #include <assert.h>
     52 #include <err.h>
     53 #include <stddef.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 #include <unistd.h>
     58 
     59 #include "installboot.h"
     60 
     61 int
     62 sun68k_clearboot(ib_params *params)
     63 {
     64 	char	bb[SUN68K_BOOT_BLOCK_MAX_SIZE];
     65 	ssize_t	rv;
     66 
     67 	assert(params != NULL);
     68 	assert(params->fsfd != -1);
     69 	assert(params->filesystem != NULL);
     70 
     71 	if (params->flags & IB_STAGE2START) {
     72 		warnx("Can't use `-B bno' with `-c'");
     73 		return (0);
     74 	}
     75 	if (params->flags & IB_STAGE1START) {
     76 		warnx("`-b bno' is not supported for %s",
     77 		    params->machine->name);
     78 		return (0);
     79 	}
     80 
     81 	/* first check that it _could_ exist here */
     82 	rv = pread(params->fsfd, &bb, sizeof(bb), SUN68K_BOOT_BLOCK_OFFSET);
     83 	if (rv == -1) {
     84 		warn("Reading `%s'", params->filesystem);
     85 		return (0);
     86 	} else if (rv != sizeof(bb)) {
     87 		warnx("Reading `%s': short read", params->filesystem);
     88 		return (0);
     89 	}
     90 
     91 	/* now clear it out to nothing */
     92 	memset(&bb, 0, sizeof(bb));
     93 
     94 	if (params->flags & IB_VERBOSE)
     95 		printf("%slearing boot block\n",
     96 		    (params->flags & IB_NOWRITE) ? "Not c" : "C");
     97 	if (params->flags & IB_NOWRITE)
     98 		return (1);
     99 
    100 	rv = pwrite(params->fsfd, &bb, sizeof(bb), SUN68K_BOOT_BLOCK_OFFSET);
    101 	if (rv == -1) {
    102 		warn("Writing `%s'", params->filesystem);
    103 		return (0);
    104 	} else if (rv != sizeof(bb)) {
    105 		warnx("Writing `%s': short write", params->filesystem);
    106 		return (0);
    107 	}
    108 
    109 	return (1);
    110 }
    111 
    112 int
    113 sun68k_setboot(ib_params *params)
    114 {
    115 	struct stat	filesystemsb, bootstrapsb;
    116 	char		bb[SUN68K_BOOT_BLOCK_MAX_SIZE];
    117 	int		retval;
    118 	ssize_t		rv;
    119 	size_t		bbi;
    120 	struct sun68k_bbinfo	*bbinfop;	/* bbinfo in prototype image */
    121 	uint32_t	maxblk, nblk, blk_i;
    122 	ib_block	*blocks = NULL;
    123 
    124 	assert(params != NULL);
    125 	assert(params->fsfd != -1);
    126 	assert(params->filesystem != NULL);
    127 	assert(params->fstype != NULL);
    128 	assert(params->s1fd != -1);
    129 	assert(params->stage1 != NULL);
    130 	assert(SUN68K_BBINFO_MAGICSIZE == 32);
    131 
    132 	if (params->stage2 == NULL) {
    133 		warnx("You must provide the name of the secondary bootstrap");
    134 		return (0);
    135 	}
    136 
    137 	retval = 0;
    138 
    139 	if (fstat(params->fsfd, &filesystemsb) == -1) {
    140 		warn("Examining `%s'", params->filesystem);
    141 		goto done;
    142 	}
    143 	if (fstat(params->s1fd, &bootstrapsb) == -1) {
    144 		warn("Examining `%s'", params->stage1);
    145 		goto done;
    146 	}
    147 	if (!S_ISREG(bootstrapsb.st_mode)) {
    148 		warnx("`%s' must be a regular file", params->stage1);
    149 		goto done;
    150 	}
    151 	if (bootstrapsb.st_size > sizeof(bb)) {
    152 		warnx("`%s' cannot be larger than %lu bytes",
    153 		    params->stage1, (unsigned long)sizeof(bb));
    154 		goto done;
    155 	}
    156 
    157 	memset(&bb, 0, sizeof(bb));
    158 	rv = read(params->s1fd, &bb, sizeof(bb));
    159 	if (rv == -1) {
    160 		warn("Reading `%s'", params->stage1);
    161 		goto done;
    162 	}
    163 
    164 	/*
    165 	 * Quick sanity check that the bootstrap given
    166 	 * is *not* an ELF executable.
    167 	 */
    168 	if (memcmp(bb + 1, "ELF", strlen("ELF")) == 0) {
    169 		warnx("`%s' is an ELF executable; need raw binary",
    170 		    params->stage1);
    171 		goto done;
    172 	}
    173 
    174 	/* Look for the bbinfo structure. */
    175 	for (bbi = 0; bbi < sizeof(bb); bbi += sizeof(uint32_t)) {
    176 		bbinfop = (void *) (bb + bbi);
    177 		if (memcmp(bbinfop->bbi_magic, SUN68K_BBINFO_MAGIC,
    178 			    SUN68K_BBINFO_MAGICSIZE) == 0)
    179 			break;
    180 	}
    181 	if (bbi >= sizeof(bb)) {
    182 		warnx("`%s' does not have a bbinfo structure\n",
    183 		    params->stage1);
    184 		goto done;
    185 	}
    186 	maxblk = be32toh(bbinfop->bbi_block_count);
    187 	if (maxblk == 0 || maxblk > (sizeof(bb) / sizeof(uint32_t))) {
    188 		warnx("bbinfo structure in `%s' has preposterous size `%u'",
    189 		    params->stage1, maxblk);
    190 		goto done;
    191 	}
    192 
    193 	/* Allocate space for our block list. */
    194 	blocks = malloc(sizeof(*blocks) * maxblk);
    195 	if (blocks == NULL) {
    196 		warn("Allocating %lu bytes",
    197 		    (unsigned long) sizeof(*blocks) * maxblk);
    198 		goto done;
    199 	}
    200 
    201 	if (S_ISREG(filesystemsb.st_mode)) {
    202 		if (fsync(params->fsfd) == -1)
    203 			warn("Synchronising file system `%s'",
    204 			    params->filesystem);
    205 	} else {
    206 		/* Ensure the secondary bootstrap is on disk. */
    207 		sync();
    208 	}
    209 
    210 	/* Collect the blocks for the secondary bootstrap. */
    211 	nblk = maxblk;
    212 	if (! params->fstype->findstage2(params, &nblk, blocks))
    213 		goto done;
    214 	if (nblk == 0) {
    215 		warnx("Secondary bootstrap `%s' is empty",
    216 		   params->stage2);
    217 		goto done;
    218 	}
    219 
    220 	/* Save those blocks in the primary bootstrap. */
    221 	bbinfop->bbi_block_count = htobe32(nblk);
    222 	bbinfop->bbi_block_size = htobe32(blocks[0].blocksize);
    223 	for (blk_i = 0; blk_i < nblk; blk_i++) {
    224 		bbinfop->bbi_block_table[blk_i] =
    225 		    htobe32(blocks[blk_i].block);
    226 		if (blocks[blk_i].blocksize < blocks[0].blocksize &&
    227 		    blk_i + 1 != nblk) {
    228 			warnx("Secondary bootstrap `%s' blocks do not have " \
    229 			    "a uniform size\n", params->stage2);
    230 			goto done;
    231 		}
    232 	}
    233 
    234 	if (params->flags & IB_VERBOSE) {
    235 		printf("Bootstrap start sector: %u\n",
    236 		    SUN68K_BOOT_BLOCK_OFFSET / SUN68K_BOOT_BLOCK_BLOCKSIZE);
    237 		printf("Bootstrap byte count:   %u\n", (unsigned)rv);
    238 		printf("Bootstrap block table:  %u entries of %u bytes available, %u used:",
    239 		    maxblk, blocks[0].blocksize, nblk);
    240 		for (blk_i = 0; blk_i < nblk; blk_i++)
    241 			printf(" %u", blocks[blk_i].block);
    242 		printf("\n%sriting bootstrap\n",
    243 		    (params->flags & IB_NOWRITE) ? "Not w" : "W");
    244 	}
    245 	if (params->flags & IB_NOWRITE) {
    246 		retval = 1;
    247 		goto done;
    248 	}
    249 
    250 	rv = pwrite(params->fsfd, &bb, sizeof(bb), SUN68K_BOOT_BLOCK_OFFSET);
    251 	if (rv == -1) {
    252 		warn("Writing `%s'", params->filesystem);
    253 		goto done;
    254 	} else if (rv != sizeof(bb)) {
    255 		warnx("Writing `%s': short write", params->filesystem);
    256 		goto done;
    257 	} else {
    258 
    259 		/* Sync filesystems (to clean in-memory superblock?) */
    260 		sync();
    261 
    262 		retval = 1;
    263 	}
    264 
    265  done:
    266 	if (blocks != NULL)
    267 		free (blocks);
    268 	return (retval);
    269 }
    270