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