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