Home | History | Annotate | Line # | Download | only in installboot
fstypes.c revision 1.10.16.1
      1 /*	$NetBSD: fstypes.c,v 1.10.16.1 2008/05/18 12:36:19 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 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 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #if HAVE_NBTOOL_CONFIG_H
     33 #include "nbtool_config.h"
     34 #endif
     35 
     36 #include <sys/cdefs.h>
     37 #if defined(__RCSID) && !defined(__lint)
     38 __RCSID("$NetBSD: fstypes.c,v 1.10.16.1 2008/05/18 12:36:19 yamt Exp $");
     39 #endif	/* !__lint */
     40 
     41 #include <sys/types.h>
     42 
     43 #include <assert.h>
     44 #include <err.h>
     45 #include <stdio.h>
     46 
     47 #include "installboot.h"
     48 
     49 struct ib_fs fstypes[] = {
     50 #ifndef NO_STAGE2
     51 	{ .name = "ffs",  .match = ffs_match,	.findstage2 = ffs_findstage2	},
     52 	{ .name = "raid", .match = raid_match,	.findstage2 = ffs_findstage2	},
     53 	{ .name = "raw",  .match = raw_match,	.findstage2 = raw_findstage2	},
     54 #endif
     55 	{ .name = NULL, }
     56 };
     57 
     58 #ifndef NO_STAGE2
     59 int
     60 hardcode_stage2(ib_params *params, uint32_t *maxblk, ib_block *blocks)
     61 {
     62 	struct stat	s2sb;
     63 	uint32_t	nblk, i;
     64 
     65 	assert(params != NULL);
     66 	assert(params->stage2 != NULL);
     67 	assert(maxblk != NULL);
     68 	assert(blocks != NULL);
     69 	assert((params->flags & IB_STAGE2START) != 0);
     70 	assert(params->fstype != NULL);
     71 	assert(params->fstype->blocksize != 0);
     72 
     73 	if (stat(params->stage2, &s2sb) == -1) {
     74 		warn("Examining `%s'", params->stage2);
     75 		return (0);
     76 	}
     77 	if (!S_ISREG(s2sb.st_mode)) {
     78 		warnx("`%s' must be a regular file", params->stage2);
     79 		return (0);
     80 	}
     81 
     82 	nblk = s2sb.st_size / params->fstype->blocksize;
     83 	if (s2sb.st_size % params->fstype->blocksize != 0)
     84 		nblk++;
     85 #if 0
     86 	fprintf(stderr, "for %s got size %lld blksize %u blocks %u\n",
     87 	    params->stage2, s2sb.st_size, params->fstype->blocksize, nblk);
     88 #endif
     89 	if (nblk > *maxblk) {
     90                 warnx("Secondary bootstrap `%s' has too many blocks "
     91                     "(calculated %u, maximum %u)",
     92 		    params->stage2, nblk, *maxblk);
     93                 return (0);
     94         }
     95 
     96 	for (i = 0; i < nblk; i++) {
     97 		blocks[i].block = params->s2start +
     98 		    i * (params->fstype->blocksize / 512);
     99 		blocks[i].blocksize = params->fstype->blocksize;
    100 	}
    101 	*maxblk = nblk;
    102 
    103 	return (1);
    104 }
    105 
    106 
    107 int
    108 raw_match(ib_params *params)
    109 {
    110 
    111 	assert(params != NULL);
    112 	assert(params->fstype != NULL);
    113 
    114 	params->fstype->blocksize = 8192;		// XXX: hardcode
    115 	return (1);		/* can always write to a "raw" file system */
    116 }
    117 
    118 int
    119 raw_findstage2(ib_params *params, uint32_t *maxblk, ib_block *blocks)
    120 {
    121 
    122 	assert(params != NULL);
    123 	assert(params->stage2 != NULL);
    124 	assert(maxblk != NULL);
    125 	assert(blocks != NULL);
    126 
    127 	if ((params->flags & IB_STAGE2START) == 0) {
    128 		warnx("Need `-B bno' for raw file systems");
    129 		return (0);
    130 	}
    131 	return (hardcode_stage2(params, maxblk, blocks));
    132 }
    133 #endif
    134