Home | History | Annotate | Line # | Download | only in installboot
ffs.c revision 1.8
      1  1.8   fvdl /*	$NetBSD: ffs.c,v 1.8 2003/04/02 10:39:48 fvdl Exp $	*/
      2  1.1  lukem 
      3  1.1  lukem /*-
      4  1.1  lukem  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  1.1  lukem  * All rights reserved.
      6  1.1  lukem  *
      7  1.1  lukem  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  lukem  * by Matt Fredette.
      9  1.1  lukem  *
     10  1.1  lukem  * Redistribution and use in source and binary forms, with or without
     11  1.1  lukem  * modification, are permitted provided that the following conditions
     12  1.1  lukem  * are met:
     13  1.1  lukem  * 1. Redistributions of source code must retain the above copyright
     14  1.1  lukem  *    notice, this list of conditions and the following disclaimer.
     15  1.1  lukem  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  lukem  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  lukem  *    documentation and/or other materials provided with the distribution.
     18  1.1  lukem  * 3. All advertising materials mentioning features or use of this software
     19  1.1  lukem  *    must display the following acknowledgement:
     20  1.1  lukem  *	This product includes software developed by the NetBSD
     21  1.1  lukem  *	Foundation, Inc. and its contributors.
     22  1.1  lukem  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  lukem  *    contributors may be used to endorse or promote products derived
     24  1.1  lukem  *    from this software without specific prior written permission.
     25  1.1  lukem  *
     26  1.1  lukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  lukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  lukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  lukem  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  lukem  */
     38  1.1  lukem 
     39  1.1  lukem #include <sys/cdefs.h>
     40  1.1  lukem #if defined(__RCSID) && !defined(__lint)
     41  1.8   fvdl __RCSID("$NetBSD: ffs.c,v 1.8 2003/04/02 10:39:48 fvdl Exp $");
     42  1.1  lukem #endif	/* !__lint */
     43  1.1  lukem 
     44  1.1  lukem #include <sys/param.h>
     45  1.1  lukem #include <sys/mount.h>
     46  1.1  lukem 
     47  1.1  lukem #include <assert.h>
     48  1.1  lukem #include <err.h>
     49  1.1  lukem #include <errno.h>
     50  1.1  lukem #include <fcntl.h>
     51  1.1  lukem #include <stdarg.h>
     52  1.1  lukem #include <stdio.h>
     53  1.1  lukem #include <stdlib.h>
     54  1.1  lukem #include <string.h>
     55  1.1  lukem #include <unistd.h>
     56  1.1  lukem 
     57  1.1  lukem #include "installboot.h"
     58  1.1  lukem 
     59  1.1  lukem #undef DIRBLKSIZ
     60  1.1  lukem 
     61  1.1  lukem #include <ufs/ufs/dinode.h>
     62  1.1  lukem #include <ufs/ufs/dir.h>
     63  1.1  lukem #include <ufs/ffs/fs.h>
     64  1.1  lukem #include <ufs/ffs/ffs_extern.h>
     65  1.1  lukem #include <ufs/ufs/ufs_bswap.h>
     66  1.1  lukem 
     67  1.8   fvdl static int	ffs_read_disk_block(ib_params *, uint64_t, int, char *);
     68  1.8   fvdl static int	ffs_find_disk_blocks_ufs1(ib_params *, ino_t,
     69  1.8   fvdl 		    int (*)(ib_params *, void *, uint64_t, uint32_t), void *);
     70  1.8   fvdl static int	ffs_find_disk_blocks_ufs2(ib_params *, ino_t,
     71  1.8   fvdl 		    int (*)(ib_params *, void *, uint64_t, uint32_t), void *);
     72  1.8   fvdl static int	ffs_findstage2_ino(ib_params *, void *, uint64_t, uint32_t);
     73  1.8   fvdl static int	ffs_findstage2_blocks(ib_params *, void *, uint64_t, uint32_t);
     74  1.8   fvdl 
     75  1.8   fvdl static int is_ufs2;
     76  1.5  lukem 
     77  1.5  lukem 
     78  1.1  lukem /* This reads a disk block from the filesystem. */
     79  1.1  lukem static int
     80  1.8   fvdl ffs_read_disk_block(ib_params *params, uint64_t blkno, int size, char *blk)
     81  1.1  lukem {
     82  1.1  lukem 	int	rv;
     83  1.1  lukem 
     84  1.2  lukem 	assert(params != NULL);
     85  1.2  lukem 	assert(blk != NULL);
     86  1.1  lukem 	assert(params->filesystem != NULL);
     87  1.1  lukem 	assert(params->fsfd != -1);
     88  1.1  lukem 	assert(blkno > 0);
     89  1.1  lukem 	assert(size > 0);
     90  1.1  lukem 	assert(blk != NULL);
     91  1.1  lukem 
     92  1.1  lukem 	rv = pread(params->fsfd, blk, size, blkno * DEV_BSIZE);
     93  1.1  lukem 	if (rv == -1) {
     94  1.8   fvdl 		warn("Reading block %llu in `%s'", blkno, params->filesystem);
     95  1.1  lukem 		return (0);
     96  1.1  lukem 	} else if (rv != size) {
     97  1.8   fvdl 		warnx("Reading block %llu in `%s': short read", blkno,
     98  1.1  lukem 		    params->filesystem);
     99  1.1  lukem 		return (0);
    100  1.1  lukem 	}
    101  1.1  lukem 
    102  1.1  lukem 	return (1);
    103  1.1  lukem }
    104  1.1  lukem 
    105  1.1  lukem /*
    106  1.1  lukem  * This iterates over the data blocks belonging to an inode,
    107  1.1  lukem  * making a callback each iteration with the disk block number
    108  1.1  lukem  * and the size.
    109  1.1  lukem  */
    110  1.1  lukem static int
    111  1.8   fvdl ffs_find_disk_blocks_ufs1(ib_params *params, ino_t ino,
    112  1.8   fvdl 	int (*callback)(ib_params *, void *, uint64_t, uint32_t),
    113  1.1  lukem 	void *state)
    114  1.1  lukem {
    115  1.8   fvdl 	char		sbbuf[SBLOCKSIZE];
    116  1.1  lukem 	struct fs	*fs;
    117  1.1  lukem 	char		inodebuf[MAXBSIZE];
    118  1.8   fvdl 	struct ufs1_dinode	*inode;
    119  1.1  lukem 	int		level_i;
    120  1.8   fvdl 	int32_t	blk, lblk, nblk;
    121  1.1  lukem 	int		rv;
    122  1.1  lukem #define LEVELS 4
    123  1.1  lukem 	struct {
    124  1.7   fvdl 		int32_t		*blknums;
    125  1.1  lukem 		unsigned long	blkcount;
    126  1.1  lukem 		char		diskbuf[MAXBSIZE];
    127  1.1  lukem 	} level[LEVELS];
    128  1.1  lukem 
    129  1.2  lukem 	assert(params != NULL);
    130  1.5  lukem 	assert(params->fstype != NULL);
    131  1.2  lukem 	assert(callback != NULL);
    132  1.2  lukem 	assert(state != NULL);
    133  1.2  lukem 
    134  1.1  lukem 	/* Read the superblock. */
    135  1.8   fvdl 	if (!ffs_read_disk_block(params, SBLOCKSIZE, params->fstype->sblockloc,
    136  1.8   fvdl 	    sbbuf))
    137  1.1  lukem 		return (0);
    138  1.1  lukem 	fs = (struct fs *)sbbuf;
    139  1.5  lukem 	if (params->fstype->needswap)
    140  1.1  lukem 		ffs_sb_swap(fs, fs);
    141  1.1  lukem 
    142  1.1  lukem 	if (fs->fs_inopb <= 0) {
    143  1.1  lukem 		warnx("Bad inopb %d in superblock in `%s'",
    144  1.1  lukem 		    fs->fs_inopb, params->filesystem);
    145  1.1  lukem 		return (0);
    146  1.1  lukem 	}
    147  1.1  lukem 
    148  1.1  lukem 	/* Read the inode. */
    149  1.1  lukem 	if (! ffs_read_disk_block(params, fsbtodb(fs, ino_to_fsba(fs, ino)),
    150  1.1  lukem 		fs->fs_bsize, inodebuf))
    151  1.1  lukem 		return (0);
    152  1.8   fvdl 	inode = (struct ufs1_dinode *)inodebuf;
    153  1.1  lukem 	inode += ino_to_fsbo(fs, ino);
    154  1.5  lukem 	if (params->fstype->needswap)
    155  1.8   fvdl 		ffs_dinode1_swap(inode, inode);
    156  1.1  lukem 
    157  1.1  lukem 	/* Get the block count and initialize for our block walk. */
    158  1.1  lukem 	nblk = howmany(inode->di_size, fs->fs_bsize);
    159  1.1  lukem 	lblk = 0;
    160  1.1  lukem 	level_i = 0;
    161  1.1  lukem 	level[0].blknums = &inode->di_db[0];
    162  1.1  lukem 	level[0].blkcount = NDADDR;
    163  1.1  lukem 	level[1].blknums = &inode->di_ib[0];
    164  1.1  lukem 	level[1].blkcount = 1;
    165  1.1  lukem 	level[2].blknums = &inode->di_ib[1];
    166  1.1  lukem 	level[2].blkcount = 1;
    167  1.1  lukem 	level[3].blknums = &inode->di_ib[2];
    168  1.1  lukem 	level[3].blkcount = 1;
    169  1.1  lukem 
    170  1.1  lukem 	/* Walk the data blocks. */
    171  1.1  lukem 	while (nblk > 0) {
    172  1.1  lukem 
    173  1.1  lukem 		/*
    174  1.1  lukem 		 * If there are no more blocks at this indirection
    175  1.1  lukem 		 * level, move up one indirection level and loop.
    176  1.1  lukem 		 */
    177  1.1  lukem 		if (level[level_i].blkcount == 0) {
    178  1.1  lukem 			if (++level_i == LEVELS)
    179  1.1  lukem 				break;
    180  1.1  lukem 			continue;
    181  1.1  lukem 		}
    182  1.1  lukem 
    183  1.1  lukem 		/* Get the next block at this level. */
    184  1.1  lukem 		blk = *(level[level_i].blknums++);
    185  1.1  lukem 		level[level_i].blkcount--;
    186  1.5  lukem 		if (params->fstype->needswap)
    187  1.1  lukem 			blk = bswap32(blk);
    188  1.1  lukem 
    189  1.1  lukem #if 0
    190  1.1  lukem 		fprintf(stderr, "ino %lu blk %lu level %d\n", ino, blk,
    191  1.1  lukem 		    level_i);
    192  1.1  lukem #endif
    193  1.1  lukem 
    194  1.1  lukem 		/*
    195  1.1  lukem 		 * If we're not at the direct level, descend one
    196  1.1  lukem 		 * level, read in that level's new block list,
    197  1.1  lukem 		 * and loop.
    198  1.1  lukem 		 */
    199  1.1  lukem 		if (level_i > 0) {
    200  1.1  lukem 			level_i--;
    201  1.1  lukem 			if (blk == 0)
    202  1.1  lukem 				memset(level[level_i].diskbuf, 0, MAXBSIZE);
    203  1.1  lukem 			else if (! ffs_read_disk_block(params,
    204  1.1  lukem 				fsbtodb(fs, blk),
    205  1.1  lukem 				fs->fs_bsize, level[level_i].diskbuf))
    206  1.1  lukem 				return (0);
    207  1.7   fvdl 			/* XXX ondisk32 */
    208  1.1  lukem 			level[level_i].blknums =
    209  1.7   fvdl 				(int32_t *)level[level_i].diskbuf;
    210  1.1  lukem 			level[level_i].blkcount = NINDIR(fs);
    211  1.1  lukem 			continue;
    212  1.1  lukem 		}
    213  1.1  lukem 
    214  1.1  lukem 		/* blk is the next direct level block. */
    215  1.1  lukem #if 0
    216  1.1  lukem 		fprintf(stderr, "ino %lu db %lu blksize %lu\n", ino,
    217  1.8   fvdl 		    fsbtodb(fs, blk), sblksize(fs, inode->di_size, lblk));
    218  1.8   fvdl #endif
    219  1.8   fvdl 		rv = (*callback)(params, state,
    220  1.8   fvdl 		    fsbtodb(fs, blk), sblksize(fs, inode->di_size, lblk));
    221  1.8   fvdl 		lblk++;
    222  1.8   fvdl 		nblk--;
    223  1.8   fvdl 		if (rv != 1)
    224  1.8   fvdl 			return (rv);
    225  1.8   fvdl 	}
    226  1.8   fvdl 
    227  1.8   fvdl 	if (nblk != 0) {
    228  1.8   fvdl 		warnx("Inode %d in `%s' ran out of blocks?", ino,
    229  1.8   fvdl 		    params->filesystem);
    230  1.8   fvdl 		return (0);
    231  1.8   fvdl 	}
    232  1.8   fvdl 
    233  1.8   fvdl 	return (1);
    234  1.8   fvdl }
    235  1.8   fvdl 
    236  1.8   fvdl /*
    237  1.8   fvdl  * This iterates over the data blocks belonging to an inode,
    238  1.8   fvdl  * making a callback each iteration with the disk block number
    239  1.8   fvdl  * and the size.
    240  1.8   fvdl  */
    241  1.8   fvdl static int
    242  1.8   fvdl ffs_find_disk_blocks_ufs2(ib_params *params, ino_t ino,
    243  1.8   fvdl 	int (*callback)(ib_params *, void *, uint64_t, uint32_t),
    244  1.8   fvdl 	void *state)
    245  1.8   fvdl {
    246  1.8   fvdl 	char		sbbuf[SBLOCKSIZE];
    247  1.8   fvdl 	struct fs	*fs;
    248  1.8   fvdl 	char		inodebuf[MAXBSIZE];
    249  1.8   fvdl 	struct ufs2_dinode	*inode;
    250  1.8   fvdl 	int		level_i;
    251  1.8   fvdl 	int64_t	blk, lblk, nblk;
    252  1.8   fvdl 	int		rv;
    253  1.8   fvdl #define LEVELS 4
    254  1.8   fvdl 	struct {
    255  1.8   fvdl 		int64_t		*blknums;
    256  1.8   fvdl 		unsigned long	blkcount;
    257  1.8   fvdl 		char		diskbuf[MAXBSIZE];
    258  1.8   fvdl 	} level[LEVELS];
    259  1.8   fvdl 
    260  1.8   fvdl 	assert(params != NULL);
    261  1.8   fvdl 	assert(params->fstype != NULL);
    262  1.8   fvdl 	assert(callback != NULL);
    263  1.8   fvdl 	assert(state != NULL);
    264  1.8   fvdl 
    265  1.8   fvdl 	/* Read the superblock. */
    266  1.8   fvdl 	if (!ffs_read_disk_block(params, SBLOCKSIZE, params->fstype->sblockloc,
    267  1.8   fvdl 	    sbbuf))
    268  1.8   fvdl 		return (0);
    269  1.8   fvdl 	fs = (struct fs *)sbbuf;
    270  1.8   fvdl 	if (params->fstype->needswap)
    271  1.8   fvdl 		ffs_sb_swap(fs, fs);
    272  1.8   fvdl 
    273  1.8   fvdl 	if (fs->fs_inopb <= 0) {
    274  1.8   fvdl 		warnx("Bad inopb %d in superblock in `%s'",
    275  1.8   fvdl 		    fs->fs_inopb, params->filesystem);
    276  1.8   fvdl 		return (0);
    277  1.8   fvdl 	}
    278  1.8   fvdl 
    279  1.8   fvdl 	/* Read the inode. */
    280  1.8   fvdl 	if (! ffs_read_disk_block(params, fsbtodb(fs, ino_to_fsba(fs, ino)),
    281  1.8   fvdl 		fs->fs_bsize, inodebuf))
    282  1.8   fvdl 		return (0);
    283  1.8   fvdl 	inode = (struct ufs2_dinode *)inodebuf;
    284  1.8   fvdl 	inode += ino_to_fsbo(fs, ino);
    285  1.8   fvdl 	if (params->fstype->needswap)
    286  1.8   fvdl 		ffs_dinode2_swap(inode, inode);
    287  1.8   fvdl 
    288  1.8   fvdl 	/* Get the block count and initialize for our block walk. */
    289  1.8   fvdl 	nblk = howmany(inode->di_size, fs->fs_bsize);
    290  1.8   fvdl 	lblk = 0;
    291  1.8   fvdl 	level_i = 0;
    292  1.8   fvdl 	level[0].blknums = &inode->di_db[0];
    293  1.8   fvdl 	level[0].blkcount = NDADDR;
    294  1.8   fvdl 	level[1].blknums = &inode->di_ib[0];
    295  1.8   fvdl 	level[1].blkcount = 1;
    296  1.8   fvdl 	level[2].blknums = &inode->di_ib[1];
    297  1.8   fvdl 	level[2].blkcount = 1;
    298  1.8   fvdl 	level[3].blknums = &inode->di_ib[2];
    299  1.8   fvdl 	level[3].blkcount = 1;
    300  1.8   fvdl 
    301  1.8   fvdl 	/* Walk the data blocks. */
    302  1.8   fvdl 	while (nblk > 0) {
    303  1.8   fvdl 
    304  1.8   fvdl 		/*
    305  1.8   fvdl 		 * If there are no more blocks at this indirection
    306  1.8   fvdl 		 * level, move up one indirection level and loop.
    307  1.8   fvdl 		 */
    308  1.8   fvdl 		if (level[level_i].blkcount == 0) {
    309  1.8   fvdl 			if (++level_i == LEVELS)
    310  1.8   fvdl 				break;
    311  1.8   fvdl 			continue;
    312  1.8   fvdl 		}
    313  1.8   fvdl 
    314  1.8   fvdl 		/* Get the next block at this level. */
    315  1.8   fvdl 		blk = *(level[level_i].blknums++);
    316  1.8   fvdl 		level[level_i].blkcount--;
    317  1.8   fvdl 		if (params->fstype->needswap)
    318  1.8   fvdl 			blk = bswap64(blk);
    319  1.8   fvdl 
    320  1.8   fvdl #if 0
    321  1.8   fvdl 		fprintf(stderr, "ino %lu blk %llu level %d\n", ino,
    322  1.8   fvdl 		    (unsigned long long)blk, level_i);
    323  1.8   fvdl #endif
    324  1.8   fvdl 
    325  1.8   fvdl 		/*
    326  1.8   fvdl 		 * If we're not at the direct level, descend one
    327  1.8   fvdl 		 * level, read in that level's new block list,
    328  1.8   fvdl 		 * and loop.
    329  1.8   fvdl 		 */
    330  1.8   fvdl 		if (level_i > 0) {
    331  1.8   fvdl 			level_i--;
    332  1.8   fvdl 			if (blk == 0)
    333  1.8   fvdl 				memset(level[level_i].diskbuf, 0, MAXBSIZE);
    334  1.8   fvdl 			else if (! ffs_read_disk_block(params,
    335  1.8   fvdl 				fsbtodb(fs, blk),
    336  1.8   fvdl 				fs->fs_bsize, level[level_i].diskbuf))
    337  1.8   fvdl 				return (0);
    338  1.8   fvdl 			level[level_i].blknums =
    339  1.8   fvdl 				(int64_t *)level[level_i].diskbuf;
    340  1.8   fvdl 			level[level_i].blkcount = NINDIR(fs);
    341  1.8   fvdl 			continue;
    342  1.8   fvdl 		}
    343  1.8   fvdl 
    344  1.8   fvdl 		/* blk is the next direct level block. */
    345  1.8   fvdl #if 0
    346  1.8   fvdl 		fprintf(stderr, "ino %lu db %llu blksize %lu\n", ino,
    347  1.8   fvdl 		    fsbtodb(fs, blk), sblksize(fs, inode->di_size, lblk));
    348  1.1  lukem #endif
    349  1.1  lukem 		rv = (*callback)(params, state,
    350  1.8   fvdl 		    fsbtodb(fs, blk), sblksize(fs, inode->di_size, lblk));
    351  1.1  lukem 		lblk++;
    352  1.1  lukem 		nblk--;
    353  1.1  lukem 		if (rv != 1)
    354  1.1  lukem 			return (rv);
    355  1.1  lukem 	}
    356  1.1  lukem 
    357  1.1  lukem 	if (nblk != 0) {
    358  1.1  lukem 		warnx("Inode %d in `%s' ran out of blocks?", ino,
    359  1.1  lukem 		    params->filesystem);
    360  1.1  lukem 		return (0);
    361  1.1  lukem 	}
    362  1.1  lukem 
    363  1.1  lukem 	return (1);
    364  1.1  lukem }
    365  1.1  lukem 
    366  1.1  lukem /*
    367  1.1  lukem  * This callback reads a block of the root directory,
    368  1.1  lukem  * searches for an entry for the secondary bootstrap,
    369  1.1  lukem  * and saves the inode number if one is found.
    370  1.1  lukem  */
    371  1.1  lukem static int
    372  1.1  lukem ffs_findstage2_ino(ib_params *params, void *_ino,
    373  1.8   fvdl 	uint64_t blk, uint32_t blksize)
    374  1.1  lukem {
    375  1.1  lukem 	char		dirbuf[MAXBSIZE];
    376  1.1  lukem 	struct direct	*de, *ede;
    377  1.1  lukem 	uint32_t	ino;
    378  1.1  lukem 
    379  1.2  lukem 	assert(params != NULL);
    380  1.5  lukem 	assert(params->fstype != NULL);
    381  1.3  lukem 	assert(params->stage2 != NULL);
    382  1.2  lukem 	assert(_ino != NULL);
    383  1.2  lukem 
    384  1.1  lukem 	/* Skip directory holes. */
    385  1.1  lukem 	if (blk == 0)
    386  1.1  lukem 		return (1);
    387  1.1  lukem 
    388  1.1  lukem 	/* Read the directory block. */
    389  1.1  lukem 	if (! ffs_read_disk_block(params, blk, blksize, dirbuf))
    390  1.1  lukem 		return (0);
    391  1.1  lukem 
    392  1.1  lukem 	/* Loop over the directory entries. */
    393  1.1  lukem 	de = (struct direct *)&dirbuf[0];
    394  1.1  lukem 	ede = (struct direct *)&dirbuf[blksize];
    395  1.1  lukem 	while (de < ede) {
    396  1.1  lukem 		ino = de->d_ino;
    397  1.5  lukem 		if (params->fstype->needswap) {
    398  1.1  lukem 			ino = bswap32(ino);
    399  1.1  lukem 			de->d_reclen = bswap16(de->d_reclen);
    400  1.1  lukem 		}
    401  1.1  lukem 		if (ino != 0 && strcmp(de->d_name, params->stage2) == 0) {
    402  1.1  lukem 			*((uint32_t *)_ino) = ino;
    403  1.1  lukem 			return (2);
    404  1.1  lukem 		}
    405  1.4     pk 		if (de->d_reclen == 0)
    406  1.4     pk 			break;
    407  1.1  lukem 		de = (struct direct *)((char *)de + de->d_reclen);
    408  1.1  lukem 	}
    409  1.1  lukem 
    410  1.1  lukem 	return (1);
    411  1.1  lukem }
    412  1.1  lukem 
    413  1.1  lukem struct findblks_state {
    414  1.1  lukem 	uint32_t	maxblk;
    415  1.1  lukem 	uint32_t	nblk;
    416  1.1  lukem 	ib_block	*blocks;
    417  1.1  lukem };
    418  1.1  lukem 
    419  1.1  lukem /* This callback records the blocks of the secondary bootstrap. */
    420  1.1  lukem static int
    421  1.1  lukem ffs_findstage2_blocks(ib_params *params, void *_state,
    422  1.8   fvdl 	uint64_t blk, uint32_t blksize)
    423  1.1  lukem {
    424  1.1  lukem 	struct findblks_state *state = _state;
    425  1.1  lukem 
    426  1.2  lukem 	assert(params != NULL);
    427  1.3  lukem 	assert(params->stage2 != NULL);
    428  1.2  lukem 	assert(_state != NULL);
    429  1.2  lukem 
    430  1.1  lukem 	if (state->nblk == state->maxblk) {
    431  1.6  lukem 		warnx("Secondary bootstrap `%s' has too many blocks (max %d)",
    432  1.6  lukem 		    params->stage2, state->maxblk);
    433  1.1  lukem 		return (0);
    434  1.1  lukem 	}
    435  1.1  lukem 	state->blocks[state->nblk].block = blk;
    436  1.1  lukem 	state->blocks[state->nblk].blocksize = blksize;
    437  1.1  lukem 	state->nblk++;
    438  1.1  lukem 	return (1);
    439  1.1  lukem }
    440  1.1  lukem 
    441  1.5  lukem /*
    442  1.5  lukem  *	publically visible functions
    443  1.5  lukem  */
    444  1.1  lukem 
    445  1.8   fvdl static off_t sblock_try[] = SBLOCKSEARCH;
    446  1.8   fvdl 
    447  1.1  lukem int
    448  1.1  lukem ffs_match(ib_params *params)
    449  1.1  lukem {
    450  1.8   fvdl 	char		sbbuf[SBLOCKSIZE];
    451  1.1  lukem 	struct fs	*fs;
    452  1.8   fvdl 	int i;
    453  1.8   fvdl 	off_t loc;
    454  1.1  lukem 
    455  1.2  lukem 	assert(params != NULL);
    456  1.5  lukem 	assert(params->fstype != NULL);
    457  1.2  lukem 
    458  1.1  lukem 	fs = (struct fs *)sbbuf;
    459  1.8   fvdl 	for (i = 0; sblock_try[i] != -1; i++) {
    460  1.8   fvdl 		loc = sblock_try[i];
    461  1.8   fvdl 		if (!ffs_read_disk_block(params, loc, SBLOCKSIZE, sbbuf))
    462  1.8   fvdl 			continue;
    463  1.8   fvdl 		switch (fs->fs_magic) {
    464  1.8   fvdl 		case FS_UFS2_MAGIC:
    465  1.8   fvdl 			is_ufs2 = 1;
    466  1.8   fvdl 			/* FALLTHROUGH */
    467  1.8   fvdl 		case FS_UFS1_MAGIC:
    468  1.8   fvdl 			params->fstype->needswap = 0;
    469  1.8   fvdl 			params->fstype->blocksize = fs->fs_bsize;
    470  1.8   fvdl 			params->fstype->sblockloc = loc;
    471  1.8   fvdl 			return (1);
    472  1.8   fvdl 		case FS_UFS2_MAGIC_SWAPPED:
    473  1.8   fvdl 			is_ufs2 = 1;
    474  1.8   fvdl 			/* FALLTHROUGH */
    475  1.8   fvdl 		case FS_UFS1_MAGIC_SWAPPED:
    476  1.8   fvdl 			params->fstype->needswap = 1;
    477  1.8   fvdl 			params->fstype->blocksize = bswap32(fs->fs_bsize);
    478  1.8   fvdl 			params->fstype->sblockloc = loc;
    479  1.8   fvdl 			return (1);
    480  1.8   fvdl 		default:
    481  1.8   fvdl 			continue;
    482  1.8   fvdl 		}
    483  1.5  lukem 	}
    484  1.1  lukem 
    485  1.8   fvdl 	return (0);
    486  1.1  lukem }
    487  1.1  lukem 
    488  1.1  lukem int
    489  1.1  lukem ffs_findstage2(ib_params *params, uint32_t *maxblk, ib_block *blocks)
    490  1.1  lukem {
    491  1.1  lukem 	int			rv;
    492  1.1  lukem 	uint32_t		ino;
    493  1.1  lukem 	struct findblks_state	state;
    494  1.1  lukem 
    495  1.2  lukem 	assert(params != NULL);
    496  1.2  lukem 	assert(params->stage2 != NULL);
    497  1.2  lukem 	assert(maxblk != NULL);
    498  1.2  lukem 	assert(blocks != NULL);
    499  1.5  lukem 
    500  1.5  lukem 	if (params->flags & IB_STAGE2START)
    501  1.5  lukem 		return (hardcode_stage2(params, maxblk, blocks));
    502  1.1  lukem 
    503  1.1  lukem 	/* The secondary bootstrap must be clearly in /. */
    504  1.1  lukem 	if (params->stage2[0] == '/')
    505  1.1  lukem 		params->stage2++;
    506  1.1  lukem 	if (strchr(params->stage2, '/') != NULL) {
    507  1.1  lukem 		warnx("The secondary bootstrap `%s' must be in /",
    508  1.1  lukem 		    params->stage2);
    509  1.1  lukem 		return (0);
    510  1.1  lukem 	}
    511  1.1  lukem 
    512  1.1  lukem 	/* Get the inode number of the secondary bootstrap. */
    513  1.8   fvdl 	if (is_ufs2)
    514  1.8   fvdl 		rv = ffs_find_disk_blocks_ufs2(params, ROOTINO,
    515  1.8   fvdl 		    ffs_findstage2_ino, &ino);
    516  1.8   fvdl 	else
    517  1.8   fvdl 		rv = ffs_find_disk_blocks_ufs1(params, ROOTINO,
    518  1.8   fvdl 		    ffs_findstage2_ino, &ino);
    519  1.6  lukem 	if (rv != 2) {
    520  1.6  lukem 		warnx("Could not find secondary bootstrap `%s' in `%s'",
    521  1.6  lukem 		    params->stage2, params->filesystem);
    522  1.1  lukem 		return (0);
    523  1.6  lukem 	}
    524  1.1  lukem 
    525  1.1  lukem 	/* Record the disk blocks of the secondary bootstrap. */
    526  1.1  lukem 	state.maxblk = *maxblk;
    527  1.1  lukem 	state.nblk = 0;
    528  1.1  lukem 	state.blocks = blocks;
    529  1.8   fvdl 	if (is_ufs2)
    530  1.8   fvdl 		rv = ffs_find_disk_blocks_ufs2(params, ino,
    531  1.8   fvdl 		    ffs_findstage2_blocks, &state);
    532  1.8   fvdl 		rv = ffs_find_disk_blocks_ufs1(params, ino,
    533  1.8   fvdl 		    ffs_findstage2_blocks, &state);
    534  1.6  lukem 	if (! rv) {
    535  1.1  lukem 		return (0);
    536  1.6  lukem 	}
    537  1.1  lukem 
    538  1.1  lukem 	*maxblk = state.nblk;
    539  1.1  lukem 	return (1);
    540  1.1  lukem }
    541