Home | History | Annotate | Line # | Download | only in installboot
bbinfo.c revision 1.5
      1 /*	$NetBSD: bbinfo.c,v 1.5 2002/05/20 16:05:26 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: bbinfo.c,v 1.5 2002/05/20 16:05:26 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 shared_bbinfo_clearboot(ib_params *params, struct bbinfo_params *bbparams,
     62 	int (*callback)(ib_params *, struct bbinfo_params *, uint8_t *))
     63 {
     64 	uint8_t	*bb;
     65 	ssize_t	rv;
     66 	int	retval;
     67 
     68 	assert(params != NULL);
     69 	assert(params->fsfd != -1);
     70 	assert(params->filesystem != NULL);
     71 	assert(bbparams != NULL);
     72 	assert((strlen(bbparams->magic) + 1) == 32);
     73 
     74 	retval = 0;
     75 	if ((bb = malloc(bbparams->maxsize)) == NULL) {
     76 		warn("Allocating %lu bytes for bbinfo",
     77 		    (unsigned long) bbparams->maxsize);
     78 		goto done;
     79 	}
     80 
     81 	if (params->flags & IB_STAGE2START) {
     82 		warnx("Can't use `-B bno' with `-c'");
     83 		goto done;
     84 	}
     85 
     86 		/* First check that it _could_ exist here */
     87 	rv = pread(params->fsfd, bb, bbparams->maxsize, bbparams->offset);
     88 	if (rv == -1) {
     89 		warn("Reading `%s'", params->filesystem);
     90 		goto done;
     91 	} else if (rv != bbparams->maxsize) {
     92 		warnx("Reading `%s': short read", params->filesystem);
     93 		goto done;
     94 	}
     95 
     96 		/* Now clear out (past the header offset) */
     97 	memset(bb + bbparams->headeroffset, 0,
     98 	    bbparams->maxsize - bbparams->headeroffset);
     99 	if (callback != NULL && ! (*callback)(params, bbparams, bb))
    100 		goto done;
    101 
    102 	if (params->flags & IB_VERBOSE)
    103 		printf("%slearing boot block\n",
    104 		    (params->flags & IB_NOWRITE) ? "Not c" : "C");
    105 	if (params->flags & IB_NOWRITE) {
    106 		retval = 1;
    107 		goto done;
    108 	}
    109 
    110 	rv = pwrite(params->fsfd, bb, bbparams->maxsize, bbparams->offset);
    111 	if (rv == -1) {
    112 		warn("Writing `%s'", params->filesystem);
    113 		goto done;
    114 	} else if (rv != bbparams->maxsize) {
    115 		warnx("Writing `%s': short write", params->filesystem);
    116 		goto done;
    117 	} else
    118 		retval = 1;
    119 
    120  done:
    121 	if (bb != NULL)
    122 		free(bb);
    123 	return (retval);
    124 }
    125 
    126 int
    127 shared_bbinfo_setboot(ib_params *params, struct bbinfo_params *bbparams,
    128 	int (*callback)(ib_params *, struct bbinfo_params *, uint8_t *))
    129 {
    130 	uint8_t		*bb;
    131 	int		retval;
    132 	ssize_t		rv;
    133 	size_t		bbi;
    134 	struct shared_bbinfo	*bbinfop;	/* bbinfo in prototype image */
    135 	uint32_t	maxblk, nblk, blk_i;
    136 	ib_block	*blocks;
    137 
    138 	assert(params != NULL);
    139 	assert(params->fsfd != -1);
    140 	assert(params->filesystem != NULL);
    141 	assert(params->fstype != NULL);
    142 	assert(params->s1fd != -1);
    143 	assert(params->stage1 != NULL);
    144 	assert(bbparams != NULL);
    145 	assert((strlen(bbparams->magic) + 1) == 32);
    146 
    147 	retval = 0;
    148 	blocks = NULL;
    149 	if ((bb = malloc(bbparams->maxsize)) == NULL) {
    150 		warn("Allocating %lu bytes for bbinfo",
    151 		    (unsigned long) bbparams->maxsize);
    152 		goto done;
    153 	}
    154 
    155 	if (params->stage2 == NULL) {
    156 		warnx("Name of secondary bootstrap not provided");
    157 		goto done;
    158 	}
    159 
    160 	if (params->s1stat.st_size >
    161 	    bbparams->maxsize - bbparams->headeroffset) {
    162 		warnx("`%s' cannot be larger than %lu bytes",
    163 		    params->stage1, (unsigned long)(bbparams->maxsize -
    164 			bbparams->headeroffset));
    165 		goto done;
    166 	}
    167 
    168 	memset(bb, 0, bbparams->maxsize);
    169 	rv = read(params->s1fd, bb + bbparams->headeroffset,
    170 	    bbparams->maxsize - bbparams->headeroffset);
    171 	if (rv == -1) {
    172 		warn("Reading `%s'", params->stage1);
    173 		goto done;
    174 	}
    175 
    176 		/*
    177 		 * Quick sanity check that the bootstrap given
    178 		 * is *not* an ELF executable.
    179 		 */
    180 	if (memcmp(bb + bbparams->headeroffset + 1, "ELF", strlen("ELF"))
    181 	    == 0) {
    182 		warnx("`%s' is an ELF executable; need raw binary",
    183 		    params->stage1);
    184 		goto done;
    185 	}
    186 
    187 #define HOSTTOTARGET32(x) ((bbparams->endian == BBINFO_LITTLE_ENDIAN) \
    188 			    ? le32toh((x)) : be32toh((x)))
    189 
    190 		/* Look for the bbinfo structure. */
    191 	for (bbi = 0; bbi < bbparams->maxsize; bbi += sizeof(uint32_t)) {
    192 		bbinfop = (void *) (bb + bbparams->headeroffset + bbi);
    193 		if (memcmp(bbinfop->bbi_magic, bbparams->magic,
    194 			    sizeof(bbinfop->bbi_magic)) == 0)
    195 			break;
    196 	}
    197 	if (bbi >= bbparams->maxsize) {
    198 		warnx("%s bbinfo structure not found in `%s'",
    199 		    params->machine->name, params->stage1);
    200 		goto done;
    201 	}
    202 	maxblk = HOSTTOTARGET32(bbinfop->bbi_block_count);
    203 	if (maxblk == 0 || maxblk > (bbparams->maxsize / sizeof(uint32_t))) {
    204 		warnx("%s bbinfo structure in `%s' has preposterous size `%u'",
    205 		    params->machine->name, params->stage1, maxblk);
    206 		goto done;
    207 	}
    208 
    209 		/* Allocate space for our block list. */
    210 	blocks = malloc(sizeof(*blocks) * maxblk);
    211 	if (blocks == NULL) {
    212 		warn("Allocating %lu bytes",
    213 		    (unsigned long)sizeof(*blocks) * maxblk);
    214 		goto done;
    215 	}
    216 
    217 	if (S_ISREG(params->fsstat.st_mode)) {
    218 		if (fsync(params->fsfd) == -1)
    219 			warn("Synchronising file system `%s'",
    220 			    params->filesystem);
    221 	} else {
    222 		/* Ensure the secondary bootstrap is on disk. */
    223 		sync();
    224 	}
    225 
    226 		/* Collect the blocks for the secondary bootstrap. */
    227 	nblk = maxblk;
    228 	if (! params->fstype->findstage2(params, &nblk, blocks))
    229 		goto done;
    230 	if (nblk == 0) {
    231 		warnx("Secondary bootstrap `%s' is empty",
    232 		    params->stage2);
    233 		goto done;
    234 	}
    235 
    236 		/* Save those blocks in the primary bootstrap. */
    237 	bbinfop->bbi_block_count = HOSTTOTARGET32(nblk);
    238 	bbinfop->bbi_block_size = HOSTTOTARGET32(blocks[0].blocksize);
    239 	for (blk_i = 0; blk_i < nblk; blk_i++) {
    240 		bbinfop->bbi_block_table[blk_i] =
    241 		    HOSTTOTARGET32(blocks[blk_i].block);
    242 		if (blocks[blk_i].blocksize < blocks[0].blocksize &&
    243 		    blk_i + 1 != nblk) {
    244 			warnx("Secondary bootstrap `%s' blocks do not have "
    245 			    "a uniform size", params->stage2);
    246 			goto done;
    247 		}
    248 	}
    249 	if (callback != NULL && ! (*callback)(params, bbparams, bb))
    250 		goto done;
    251 
    252 	if (params->flags & IB_VERBOSE) {
    253 		printf("Bootstrap start sector: %u\n",
    254 		    bbparams->offset / bbparams->blocksize);
    255 		printf("Bootstrap byte count:   %u\n", (unsigned)rv);
    256 		printf("Bootstrap block table:  "
    257 			"%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, bbparams->maxsize, bbparams->offset);
    270 	if (rv == -1) {
    271 		warn("Writing `%s'", params->filesystem);
    272 		goto done;
    273 	} else if (rv != bbparams->maxsize) {
    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 	if (bb != NULL)
    284 		free(bb);
    285 	return (retval);
    286 }
    287