Home | History | Annotate | Line # | Download | only in arch
sparc.c revision 1.4
      1 /*	$NetBSD: sparc.c,v 1.4 2002/05/15 02:18:23 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: sparc.c,v 1.4 2002/05/15 02:18:23 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 sparc_clearboot(ib_params *params)
     62 {
     63 	char	bb[SPARC_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), SPARC_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), SPARC_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 sparc_setboot(ib_params *params)
    113 {
    114 	char		bb[SPARC_BOOT_BLOCK_MAX_SIZE];
    115 	int		retval;
    116 	ssize_t		rv;
    117 	size_t		bbi;
    118 	struct sparc_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(SPARC_BBINFO_MAGICSIZE == 32);
    129 
    130 #define	SPARC_AOUT_OFFSET	32
    131 
    132 	retval = 0;
    133 	blocks = NULL;
    134 
    135 	if (params->stage2 == NULL) {
    136 		warnx("You must provide the name of the secondary bootstrap");
    137 		goto done;
    138 	}
    139 	if (params->flags & IB_STAGE1START) {
    140 		warnx("`-b bno' is not supported for %s",
    141 		    params->machine->name);
    142 		goto done;
    143 	}
    144 
    145 	if (!S_ISREG(params->s1stat.st_mode)) {
    146 		warnx("`%s' must be a regular file", params->stage1);
    147 		goto done;
    148 	}
    149 	if (params->s1stat.st_size > sizeof(bb)) {
    150 		warnx("`%s' cannot be larger than %lu bytes",
    151 		    params->stage1, (unsigned long)sizeof(bb));
    152 		goto done;
    153 	}
    154 
    155 	/*
    156 	 * Read 1st stage boot program
    157 	 * Leave room for a 32-byte a.out header.
    158 	 */
    159 	memset(&bb, 0, sizeof(bb));
    160 	rv = read(params->s1fd, bb + SPARC_AOUT_OFFSET,
    161 	    sizeof(bb) - SPARC_AOUT_OFFSET);
    162 	if (rv == -1) {
    163 		warn("Reading `%s'", params->stage1);
    164 		goto done;
    165 	}
    166 
    167 	/*
    168 	 * Quick sanity check that the bootstrap given
    169 	 * is *not* an ELF executable.
    170 	 */
    171 	if (memcmp(bb + SPARC_AOUT_OFFSET + 1, "ELF", strlen("ELF")) == 0) {
    172 		warnx("`%s' is an ELF executable; need raw binary",
    173 		    params->stage1);
    174 		goto done;
    175 	}
    176 
    177 	/* Look for the bbinfo structure. */
    178 	for (bbi = 0; bbi < sizeof(bb); bbi += sizeof(uint32_t)) {
    179 		bbinfop = (void *) (bb + bbi);
    180 		if (memcmp(bbinfop->bbi_magic, SPARC_BBINFO_MAGIC,
    181 			    SPARC_BBINFO_MAGICSIZE) == 0)
    182 			break;
    183 	}
    184 	if (bbi >= sizeof(bb)) {
    185 		warnx("`%s' does not have a bbinfo structure\n",
    186 		    params->stage1);
    187 		goto done;
    188 	}
    189 	maxblk = be32toh(bbinfop->bbi_block_count);
    190 	if (maxblk == 0 || maxblk > (sizeof(bb) / sizeof(uint32_t))) {
    191 		warnx("bbinfo structure in `%s' has preposterous size `%u'",
    192 		    params->stage1, maxblk);
    193 		goto done;
    194 	}
    195 
    196 	/* Allocate space for our block list. */
    197 	blocks = malloc(sizeof(*blocks) * maxblk);
    198 	if (blocks == NULL) {
    199 		warn("Allocating %lu bytes",
    200 		    (unsigned long) sizeof(*blocks) * maxblk);
    201 		goto done;
    202 	}
    203 
    204 	if (S_ISREG(params->fsstat.st_mode)) {
    205 		if (fsync(params->fsfd) == -1)
    206 			warn("Synchronising file system `%s'",
    207 			    params->filesystem);
    208 	} else {
    209 		/* Ensure the secondary bootstrap is on disk. */
    210 		sync();
    211 	}
    212 
    213 	/* Collect the blocks for the secondary bootstrap. */
    214 	nblk = maxblk;
    215 	if (! params->fstype->findstage2(params, &nblk, blocks))
    216 		goto done;
    217 	if (nblk == 0) {
    218 		warnx("Secondary bootstrap `%s' is empty",
    219 		   params->stage2);
    220 		goto done;
    221 	}
    222 
    223 	/* Save those blocks in the primary bootstrap. */
    224 	bbinfop->bbi_block_count = htobe32(nblk);
    225 	bbinfop->bbi_block_size = htobe32(blocks[0].blocksize);
    226 	for (blk_i = 0; blk_i < nblk; blk_i++) {
    227 		bbinfop->bbi_block_table[blk_i] =
    228 		    htobe32(blocks[blk_i].block);
    229 		if (blocks[blk_i].blocksize < blocks[0].blocksize &&
    230 		    blk_i + 1 != nblk) {
    231 			warnx("Secondary bootstrap `%s' blocks do not have " \
    232 			    "a uniform size\n", params->stage2);
    233 			goto done;
    234 		}
    235 	}
    236 
    237 	/*
    238 	 * sun4c/sun4m PROMs require an a.out(5) format header.
    239 	 * Old-style sun4 PROMs do not expect a header at all.
    240 	 * To deal with this, we construct a header that is also executable
    241 	 * code containing a forward branch that gets us past the 32-byte
    242 	 * header where the actual code begins. In assembly:
    243 	 * 	.word	MAGIC		! a NOP
    244 	 * 	ba,a	start		!
    245 	 * 	.skip	24		! pad
    246 	 * start:
    247 	 */
    248 #define SUN_MAGIC	0x01030107
    249 #define SUN4_BASTART	0x30800007	/* i.e.: ba,a `start' */
    250 	*((uint32_t *)bb) = htobe32(SUN_MAGIC);
    251 	*((uint32_t *)bb + 1) = htobe32(SUN4_BASTART);
    252 
    253 	if (params->flags & IB_VERBOSE) {
    254 		printf("Bootstrap start sector: %u\n",
    255 		    SPARC_BOOT_BLOCK_OFFSET / SPARC_BOOT_BLOCK_BLOCKSIZE);
    256 		printf("Bootstrap byte count:   %u\n", (unsigned)rv);
    257 		printf("Bootstrap block table:  %u entries of %u bytes available, %u used:",
    258 		    maxblk, blocks[0].blocksize, nblk);
    259 		for (blk_i = 0; blk_i < nblk; blk_i++)
    260 			printf(" %u", blocks[blk_i].block);
    261 		printf("\n%sriting bootstrap\n",
    262 		    (params->flags & IB_NOWRITE) ? "Not w" : "W");
    263 	}
    264 	if (params->flags & IB_NOWRITE) {
    265 		retval = 1;
    266 		goto done;
    267 	}
    268 
    269 	rv = pwrite(params->fsfd, &bb, sizeof(bb), SPARC_BOOT_BLOCK_OFFSET);
    270 	if (rv == -1) {
    271 		warn("Writing `%s'", params->filesystem);
    272 		goto done;
    273 	} else if (rv != sizeof(bb)) {
    274 		warnx("Writing `%s': short write", params->filesystem);
    275 		goto done;
    276 	} else {
    277 		retval = 1;
    278 	}
    279 
    280  done:
    281 	if (blocks != NULL)
    282 		free (blocks);
    283 	return (retval);
    284 }
    285