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