Home | History | Annotate | Line # | Download | only in installboot
ffs.c revision 1.29.12.2
      1  1.29.12.2       tls /*	$NetBSD: ffs.c,v 1.29.12.2 2013/06/23 06:29:04 tls 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  *
     19        1.1     lukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.1     lukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.1     lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.1     lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.1     lukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.1     lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.1     lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.1     lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.1     lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.1     lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.1     lukem  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1     lukem  */
     31        1.1     lukem 
     32       1.15       jmc #if HAVE_NBTOOL_CONFIG_H
     33       1.15       jmc #include "nbtool_config.h"
     34       1.15       jmc #endif
     35       1.15       jmc 
     36        1.1     lukem #include <sys/cdefs.h>
     37       1.29   tsutsui #if !defined(__lint)
     38  1.29.12.2       tls __RCSID("$NetBSD: ffs.c,v 1.29.12.2 2013/06/23 06:29:04 tls Exp $");
     39        1.1     lukem #endif	/* !__lint */
     40        1.1     lukem 
     41        1.1     lukem #include <sys/param.h>
     42       1.15       jmc 
     43       1.15       jmc #if !HAVE_NBTOOL_CONFIG_H
     44        1.1     lukem #include <sys/mount.h>
     45       1.15       jmc #endif
     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.20       jdc /* From <dev/raidframe/raidframevar.h> */
     60       1.20       jdc #define RF_PROTECTED_SECTORS 64L
     61       1.19       jdc 
     62        1.1     lukem #undef DIRBLKSIZ
     63        1.1     lukem 
     64        1.1     lukem #include <ufs/ufs/dinode.h>
     65        1.1     lukem #include <ufs/ufs/dir.h>
     66        1.1     lukem #include <ufs/ffs/fs.h>
     67        1.1     lukem #include <ufs/ffs/ffs_extern.h>
     68       1.18       dsl #ifndef NO_FFS_SWAP
     69        1.1     lukem #include <ufs/ufs/ufs_bswap.h>
     70       1.18       dsl #else
     71       1.18       dsl #define	ffs_sb_swap(fs_a, fs_b)
     72       1.18       dsl #define	ffs_dinode1_swap(inode_a, inode_b)
     73       1.18       dsl #define	ffs_dinode2_swap(inode_a, inode_b)
     74       1.18       dsl #endif
     75        1.1     lukem 
     76       1.19       jdc static int	ffs_match_common(ib_params *, off_t);
     77       1.23  christos static int	ffs_read_disk_block(ib_params *, uint64_t, int, char []);
     78        1.8      fvdl static int	ffs_find_disk_blocks_ufs1(ib_params *, ino_t,
     79        1.8      fvdl 		    int (*)(ib_params *, void *, uint64_t, uint32_t), void *);
     80        1.8      fvdl static int	ffs_find_disk_blocks_ufs2(ib_params *, ino_t,
     81        1.8      fvdl 		    int (*)(ib_params *, void *, uint64_t, uint32_t), void *);
     82        1.8      fvdl static int	ffs_findstage2_ino(ib_params *, void *, uint64_t, uint32_t);
     83        1.8      fvdl static int	ffs_findstage2_blocks(ib_params *, void *, uint64_t, uint32_t);
     84        1.8      fvdl 
     85        1.8      fvdl static int is_ufs2;
     86        1.5     lukem 
     87        1.5     lukem 
     88        1.1     lukem /* This reads a disk block from the filesystem. */
     89        1.1     lukem static int
     90       1.23  christos ffs_read_disk_block(ib_params *params, uint64_t blkno, int size, char blk[])
     91        1.1     lukem {
     92        1.1     lukem 	int	rv;
     93        1.1     lukem 
     94        1.2     lukem 	assert(params != NULL);
     95        1.1     lukem 	assert(params->filesystem != NULL);
     96        1.1     lukem 	assert(params->fsfd != -1);
     97        1.1     lukem 	assert(size > 0);
     98        1.1     lukem 	assert(blk != NULL);
     99        1.1     lukem 
    100       1.28   tsutsui 	rv = pread(params->fsfd, blk, size, blkno * params->sectorsize);
    101        1.1     lukem 	if (rv == -1) {
    102       1.10        he 		warn("Reading block %llu in `%s'",
    103       1.10        he 		    (unsigned long long)blkno, params->filesystem);
    104        1.1     lukem 		return (0);
    105        1.1     lukem 	} else if (rv != size) {
    106       1.10        he 		warnx("Reading block %llu in `%s': short read",
    107       1.10        he 		    (unsigned long long)blkno, params->filesystem);
    108        1.1     lukem 		return (0);
    109        1.1     lukem 	}
    110        1.1     lukem 
    111        1.1     lukem 	return (1);
    112        1.1     lukem }
    113        1.1     lukem 
    114        1.1     lukem /*
    115        1.1     lukem  * This iterates over the data blocks belonging to an inode,
    116        1.1     lukem  * making a callback each iteration with the disk block number
    117        1.1     lukem  * and the size.
    118        1.1     lukem  */
    119        1.1     lukem static int
    120        1.8      fvdl ffs_find_disk_blocks_ufs1(ib_params *params, ino_t ino,
    121        1.8      fvdl 	int (*callback)(ib_params *, void *, uint64_t, uint32_t),
    122        1.1     lukem 	void *state)
    123        1.1     lukem {
    124        1.8      fvdl 	char		sbbuf[SBLOCKSIZE];
    125        1.1     lukem 	struct fs	*fs;
    126        1.1     lukem 	char		inodebuf[MAXBSIZE];
    127        1.8      fvdl 	struct ufs1_dinode	*inode;
    128        1.1     lukem 	int		level_i;
    129        1.8      fvdl 	int32_t	blk, lblk, nblk;
    130        1.1     lukem 	int		rv;
    131        1.1     lukem #define LEVELS 4
    132        1.1     lukem 	struct {
    133        1.7      fvdl 		int32_t		*blknums;
    134        1.1     lukem 		unsigned long	blkcount;
    135        1.1     lukem 		char		diskbuf[MAXBSIZE];
    136        1.1     lukem 	} level[LEVELS];
    137        1.1     lukem 
    138        1.2     lukem 	assert(params != NULL);
    139        1.5     lukem 	assert(params->fstype != NULL);
    140        1.2     lukem 	assert(callback != NULL);
    141        1.2     lukem 	assert(state != NULL);
    142        1.2     lukem 
    143        1.1     lukem 	/* Read the superblock. */
    144       1.11        he 	if (!ffs_read_disk_block(params, params->fstype->sblockloc, SBLOCKSIZE,
    145        1.8      fvdl 	    sbbuf))
    146        1.1     lukem 		return (0);
    147        1.1     lukem 	fs = (struct fs *)sbbuf;
    148       1.24        he #ifndef NO_FFS_SWAP
    149        1.5     lukem 	if (params->fstype->needswap)
    150        1.1     lukem 		ffs_sb_swap(fs, fs);
    151       1.24        he #endif
    152        1.1     lukem 
    153        1.1     lukem 	if (fs->fs_inopb <= 0) {
    154        1.1     lukem 		warnx("Bad inopb %d in superblock in `%s'",
    155        1.1     lukem 		    fs->fs_inopb, params->filesystem);
    156        1.1     lukem 		return (0);
    157        1.1     lukem 	}
    158        1.1     lukem 
    159        1.1     lukem 	/* Read the inode. */
    160       1.19       jdc 	if (! ffs_read_disk_block(params,
    161       1.19       jdc 		fsbtodb(fs, ino_to_fsba(fs, ino)) + params->fstype->offset,
    162        1.1     lukem 		fs->fs_bsize, inodebuf))
    163        1.1     lukem 		return (0);
    164        1.8      fvdl 	inode = (struct ufs1_dinode *)inodebuf;
    165        1.1     lukem 	inode += ino_to_fsbo(fs, ino);
    166       1.24        he #ifndef NO_FFS_SWAP
    167        1.5     lukem 	if (params->fstype->needswap)
    168        1.8      fvdl 		ffs_dinode1_swap(inode, inode);
    169       1.24        he #endif
    170        1.1     lukem 
    171        1.1     lukem 	/* Get the block count and initialize for our block walk. */
    172        1.1     lukem 	nblk = howmany(inode->di_size, fs->fs_bsize);
    173        1.1     lukem 	lblk = 0;
    174        1.1     lukem 	level_i = 0;
    175        1.1     lukem 	level[0].blknums = &inode->di_db[0];
    176  1.29.12.1       tls 	level[0].blkcount = UFS_NDADDR;
    177        1.1     lukem 	level[1].blknums = &inode->di_ib[0];
    178        1.1     lukem 	level[1].blkcount = 1;
    179        1.1     lukem 	level[2].blknums = &inode->di_ib[1];
    180        1.1     lukem 	level[2].blkcount = 1;
    181        1.1     lukem 	level[3].blknums = &inode->di_ib[2];
    182        1.1     lukem 	level[3].blkcount = 1;
    183        1.1     lukem 
    184        1.1     lukem 	/* Walk the data blocks. */
    185        1.1     lukem 	while (nblk > 0) {
    186        1.1     lukem 
    187        1.1     lukem 		/*
    188        1.1     lukem 		 * If there are no more blocks at this indirection
    189        1.1     lukem 		 * level, move up one indirection level and loop.
    190        1.1     lukem 		 */
    191        1.1     lukem 		if (level[level_i].blkcount == 0) {
    192        1.1     lukem 			if (++level_i == LEVELS)
    193        1.1     lukem 				break;
    194        1.1     lukem 			continue;
    195        1.1     lukem 		}
    196        1.1     lukem 
    197        1.1     lukem 		/* Get the next block at this level. */
    198        1.1     lukem 		blk = *(level[level_i].blknums++);
    199        1.1     lukem 		level[level_i].blkcount--;
    200        1.5     lukem 		if (params->fstype->needswap)
    201        1.1     lukem 			blk = bswap32(blk);
    202        1.1     lukem 
    203        1.1     lukem #if 0
    204        1.1     lukem 		fprintf(stderr, "ino %lu blk %lu level %d\n", ino, blk,
    205        1.1     lukem 		    level_i);
    206        1.1     lukem #endif
    207        1.1     lukem 
    208        1.1     lukem 		/*
    209        1.1     lukem 		 * If we're not at the direct level, descend one
    210        1.1     lukem 		 * level, read in that level's new block list,
    211        1.1     lukem 		 * and loop.
    212        1.1     lukem 		 */
    213        1.1     lukem 		if (level_i > 0) {
    214        1.1     lukem 			level_i--;
    215        1.1     lukem 			if (blk == 0)
    216        1.1     lukem 				memset(level[level_i].diskbuf, 0, MAXBSIZE);
    217        1.1     lukem 			else if (! ffs_read_disk_block(params,
    218       1.19       jdc 				fsbtodb(fs, blk) + params->fstype->offset,
    219        1.1     lukem 				fs->fs_bsize, level[level_i].diskbuf))
    220        1.1     lukem 				return (0);
    221        1.7      fvdl 			/* XXX ondisk32 */
    222        1.1     lukem 			level[level_i].blknums =
    223        1.7      fvdl 				(int32_t *)level[level_i].diskbuf;
    224  1.29.12.2       tls 			level[level_i].blkcount = FFS_NINDIR(fs);
    225        1.1     lukem 			continue;
    226        1.1     lukem 		}
    227        1.1     lukem 
    228        1.1     lukem 		/* blk is the next direct level block. */
    229        1.1     lukem #if 0
    230        1.1     lukem 		fprintf(stderr, "ino %lu db %lu blksize %lu\n", ino,
    231  1.29.12.2       tls 		    fsbtodb(fs, blk), ffs_sblksize(fs, inode->di_size, lblk));
    232        1.8      fvdl #endif
    233        1.8      fvdl 		rv = (*callback)(params, state,
    234       1.19       jdc 		    fsbtodb(fs, blk) + params->fstype->offset,
    235  1.29.12.2       tls 		    ffs_sblksize(fs, (int64_t)inode->di_size, lblk));
    236        1.8      fvdl 		lblk++;
    237        1.8      fvdl 		nblk--;
    238        1.8      fvdl 		if (rv != 1)
    239        1.8      fvdl 			return (rv);
    240        1.8      fvdl 	}
    241        1.8      fvdl 
    242        1.8      fvdl 	if (nblk != 0) {
    243       1.16  christos 		warnx("Inode %llu in `%s' ran out of blocks?",
    244       1.16  christos 		    (unsigned long long)ino, params->filesystem);
    245        1.8      fvdl 		return (0);
    246        1.8      fvdl 	}
    247        1.8      fvdl 
    248        1.8      fvdl 	return (1);
    249        1.8      fvdl }
    250        1.8      fvdl 
    251        1.8      fvdl /*
    252        1.8      fvdl  * This iterates over the data blocks belonging to an inode,
    253        1.8      fvdl  * making a callback each iteration with the disk block number
    254        1.8      fvdl  * and the size.
    255        1.8      fvdl  */
    256        1.8      fvdl static int
    257        1.8      fvdl ffs_find_disk_blocks_ufs2(ib_params *params, ino_t ino,
    258        1.8      fvdl 	int (*callback)(ib_params *, void *, uint64_t, uint32_t),
    259        1.8      fvdl 	void *state)
    260        1.8      fvdl {
    261        1.8      fvdl 	char		sbbuf[SBLOCKSIZE];
    262        1.8      fvdl 	struct fs	*fs;
    263        1.8      fvdl 	char		inodebuf[MAXBSIZE];
    264        1.8      fvdl 	struct ufs2_dinode	*inode;
    265        1.8      fvdl 	int		level_i;
    266       1.27     lukem 	int64_t		blk, lblk, nblk;
    267        1.8      fvdl 	int		rv;
    268        1.8      fvdl #define LEVELS 4
    269        1.8      fvdl 	struct {
    270        1.8      fvdl 		int64_t		*blknums;
    271        1.8      fvdl 		unsigned long	blkcount;
    272        1.8      fvdl 		char		diskbuf[MAXBSIZE];
    273        1.8      fvdl 	} level[LEVELS];
    274        1.8      fvdl 
    275        1.8      fvdl 	assert(params != NULL);
    276        1.8      fvdl 	assert(params->fstype != NULL);
    277        1.8      fvdl 	assert(callback != NULL);
    278        1.8      fvdl 	assert(state != NULL);
    279        1.8      fvdl 
    280        1.8      fvdl 	/* Read the superblock. */
    281       1.11        he 	if (!ffs_read_disk_block(params, params->fstype->sblockloc, SBLOCKSIZE,
    282        1.8      fvdl 	    sbbuf))
    283        1.8      fvdl 		return (0);
    284        1.8      fvdl 	fs = (struct fs *)sbbuf;
    285       1.24        he #ifndef NO_FFS_SWAP
    286        1.8      fvdl 	if (params->fstype->needswap)
    287        1.8      fvdl 		ffs_sb_swap(fs, fs);
    288       1.24        he #endif
    289        1.8      fvdl 
    290        1.8      fvdl 	if (fs->fs_inopb <= 0) {
    291        1.8      fvdl 		warnx("Bad inopb %d in superblock in `%s'",
    292        1.8      fvdl 		    fs->fs_inopb, params->filesystem);
    293        1.8      fvdl 		return (0);
    294        1.8      fvdl 	}
    295        1.8      fvdl 
    296        1.8      fvdl 	/* Read the inode. */
    297       1.21       bad 	if (! ffs_read_disk_block(params,
    298       1.21       bad 		fsbtodb(fs, ino_to_fsba(fs, ino)) + params->fstype->offset,
    299        1.8      fvdl 		fs->fs_bsize, inodebuf))
    300        1.8      fvdl 		return (0);
    301        1.8      fvdl 	inode = (struct ufs2_dinode *)inodebuf;
    302        1.8      fvdl 	inode += ino_to_fsbo(fs, ino);
    303       1.24        he #ifndef NO_FFS_SWAP
    304        1.8      fvdl 	if (params->fstype->needswap)
    305        1.8      fvdl 		ffs_dinode2_swap(inode, inode);
    306       1.24        he #endif
    307        1.8      fvdl 
    308        1.8      fvdl 	/* Get the block count and initialize for our block walk. */
    309        1.8      fvdl 	nblk = howmany(inode->di_size, fs->fs_bsize);
    310        1.8      fvdl 	lblk = 0;
    311        1.8      fvdl 	level_i = 0;
    312        1.8      fvdl 	level[0].blknums = &inode->di_db[0];
    313  1.29.12.1       tls 	level[0].blkcount = UFS_NDADDR;
    314        1.8      fvdl 	level[1].blknums = &inode->di_ib[0];
    315        1.8      fvdl 	level[1].blkcount = 1;
    316        1.8      fvdl 	level[2].blknums = &inode->di_ib[1];
    317        1.8      fvdl 	level[2].blkcount = 1;
    318        1.8      fvdl 	level[3].blknums = &inode->di_ib[2];
    319        1.8      fvdl 	level[3].blkcount = 1;
    320        1.8      fvdl 
    321        1.8      fvdl 	/* Walk the data blocks. */
    322        1.8      fvdl 	while (nblk > 0) {
    323        1.8      fvdl 
    324        1.8      fvdl 		/*
    325        1.8      fvdl 		 * If there are no more blocks at this indirection
    326        1.8      fvdl 		 * level, move up one indirection level and loop.
    327        1.8      fvdl 		 */
    328        1.8      fvdl 		if (level[level_i].blkcount == 0) {
    329        1.8      fvdl 			if (++level_i == LEVELS)
    330        1.8      fvdl 				break;
    331        1.8      fvdl 			continue;
    332        1.8      fvdl 		}
    333        1.8      fvdl 
    334        1.8      fvdl 		/* Get the next block at this level. */
    335        1.8      fvdl 		blk = *(level[level_i].blknums++);
    336        1.8      fvdl 		level[level_i].blkcount--;
    337        1.8      fvdl 		if (params->fstype->needswap)
    338        1.8      fvdl 			blk = bswap64(blk);
    339        1.8      fvdl 
    340        1.8      fvdl #if 0
    341        1.8      fvdl 		fprintf(stderr, "ino %lu blk %llu level %d\n", ino,
    342        1.8      fvdl 		    (unsigned long long)blk, level_i);
    343        1.8      fvdl #endif
    344        1.8      fvdl 
    345        1.8      fvdl 		/*
    346        1.8      fvdl 		 * If we're not at the direct level, descend one
    347        1.8      fvdl 		 * level, read in that level's new block list,
    348        1.8      fvdl 		 * and loop.
    349        1.8      fvdl 		 */
    350        1.8      fvdl 		if (level_i > 0) {
    351        1.8      fvdl 			level_i--;
    352        1.8      fvdl 			if (blk == 0)
    353        1.8      fvdl 				memset(level[level_i].diskbuf, 0, MAXBSIZE);
    354        1.8      fvdl 			else if (! ffs_read_disk_block(params,
    355       1.21       bad 				fsbtodb(fs, blk) + params->fstype->offset,
    356        1.8      fvdl 				fs->fs_bsize, level[level_i].diskbuf))
    357        1.8      fvdl 				return (0);
    358        1.8      fvdl 			level[level_i].blknums =
    359        1.8      fvdl 				(int64_t *)level[level_i].diskbuf;
    360  1.29.12.2       tls 			level[level_i].blkcount = FFS_NINDIR(fs);
    361        1.8      fvdl 			continue;
    362        1.8      fvdl 		}
    363        1.8      fvdl 
    364        1.8      fvdl 		/* blk is the next direct level block. */
    365        1.8      fvdl #if 0
    366        1.8      fvdl 		fprintf(stderr, "ino %lu db %llu blksize %lu\n", ino,
    367  1.29.12.2       tls 		    fsbtodb(fs, blk), ffs_sblksize(fs, inode->di_size, lblk));
    368        1.1     lukem #endif
    369        1.1     lukem 		rv = (*callback)(params, state,
    370       1.21       bad 		    fsbtodb(fs, blk) + params->fstype->offset,
    371  1.29.12.2       tls 		    ffs_sblksize(fs, (int64_t)inode->di_size, lblk));
    372        1.1     lukem 		lblk++;
    373        1.1     lukem 		nblk--;
    374        1.1     lukem 		if (rv != 1)
    375        1.1     lukem 			return (rv);
    376        1.1     lukem 	}
    377        1.1     lukem 
    378        1.1     lukem 	if (nblk != 0) {
    379       1.16  christos 		warnx("Inode %llu in `%s' ran out of blocks?",
    380       1.16  christos 		    (unsigned long long)ino, params->filesystem);
    381        1.1     lukem 		return (0);
    382        1.1     lukem 	}
    383        1.1     lukem 
    384        1.1     lukem 	return (1);
    385        1.1     lukem }
    386        1.1     lukem 
    387        1.1     lukem /*
    388        1.1     lukem  * This callback reads a block of the root directory,
    389        1.1     lukem  * searches for an entry for the secondary bootstrap,
    390        1.1     lukem  * and saves the inode number if one is found.
    391        1.1     lukem  */
    392        1.1     lukem static int
    393        1.1     lukem ffs_findstage2_ino(ib_params *params, void *_ino,
    394        1.8      fvdl 	uint64_t blk, uint32_t blksize)
    395        1.1     lukem {
    396        1.1     lukem 	char		dirbuf[MAXBSIZE];
    397        1.1     lukem 	struct direct	*de, *ede;
    398        1.1     lukem 	uint32_t	ino;
    399        1.1     lukem 
    400        1.2     lukem 	assert(params != NULL);
    401        1.5     lukem 	assert(params->fstype != NULL);
    402        1.3     lukem 	assert(params->stage2 != NULL);
    403        1.2     lukem 	assert(_ino != NULL);
    404        1.2     lukem 
    405        1.1     lukem 	/* Skip directory holes. */
    406        1.1     lukem 	if (blk == 0)
    407        1.1     lukem 		return (1);
    408        1.1     lukem 
    409        1.1     lukem 	/* Read the directory block. */
    410        1.1     lukem 	if (! ffs_read_disk_block(params, blk, blksize, dirbuf))
    411        1.1     lukem 		return (0);
    412        1.1     lukem 
    413        1.1     lukem 	/* Loop over the directory entries. */
    414        1.1     lukem 	de = (struct direct *)&dirbuf[0];
    415        1.1     lukem 	ede = (struct direct *)&dirbuf[blksize];
    416        1.1     lukem 	while (de < ede) {
    417       1.17   thorpej 		ino = de->d_fileno;
    418        1.5     lukem 		if (params->fstype->needswap) {
    419        1.1     lukem 			ino = bswap32(ino);
    420        1.1     lukem 			de->d_reclen = bswap16(de->d_reclen);
    421        1.1     lukem 		}
    422        1.1     lukem 		if (ino != 0 && strcmp(de->d_name, params->stage2) == 0) {
    423        1.1     lukem 			*((uint32_t *)_ino) = ino;
    424        1.1     lukem 			return (2);
    425        1.1     lukem 		}
    426        1.4        pk 		if (de->d_reclen == 0)
    427        1.4        pk 			break;
    428        1.1     lukem 		de = (struct direct *)((char *)de + de->d_reclen);
    429        1.1     lukem 	}
    430        1.1     lukem 
    431        1.1     lukem 	return (1);
    432        1.1     lukem }
    433        1.1     lukem 
    434        1.1     lukem struct findblks_state {
    435        1.1     lukem 	uint32_t	maxblk;
    436        1.1     lukem 	uint32_t	nblk;
    437        1.1     lukem 	ib_block	*blocks;
    438        1.1     lukem };
    439        1.1     lukem 
    440        1.1     lukem /* This callback records the blocks of the secondary bootstrap. */
    441        1.1     lukem static int
    442        1.1     lukem ffs_findstage2_blocks(ib_params *params, void *_state,
    443        1.8      fvdl 	uint64_t blk, uint32_t blksize)
    444        1.1     lukem {
    445        1.1     lukem 	struct findblks_state *state = _state;
    446        1.1     lukem 
    447        1.2     lukem 	assert(params != NULL);
    448        1.3     lukem 	assert(params->stage2 != NULL);
    449        1.2     lukem 	assert(_state != NULL);
    450        1.2     lukem 
    451        1.1     lukem 	if (state->nblk == state->maxblk) {
    452        1.6     lukem 		warnx("Secondary bootstrap `%s' has too many blocks (max %d)",
    453        1.6     lukem 		    params->stage2, state->maxblk);
    454        1.1     lukem 		return (0);
    455        1.1     lukem 	}
    456        1.1     lukem 	state->blocks[state->nblk].block = blk;
    457        1.1     lukem 	state->blocks[state->nblk].blocksize = blksize;
    458        1.1     lukem 	state->nblk++;
    459        1.1     lukem 	return (1);
    460        1.1     lukem }
    461        1.1     lukem 
    462        1.5     lukem /*
    463       1.13     lukem  *	publicly visible functions
    464        1.5     lukem  */
    465        1.1     lukem 
    466        1.8      fvdl static off_t sblock_try[] = SBLOCKSEARCH;
    467        1.8      fvdl 
    468        1.1     lukem int
    469        1.1     lukem ffs_match(ib_params *params)
    470        1.1     lukem {
    471       1.19       jdc 	return ffs_match_common(params, (off_t) 0);
    472       1.19       jdc }
    473       1.19       jdc 
    474       1.19       jdc int
    475       1.19       jdc raid_match(ib_params *params)
    476       1.19       jdc {
    477       1.19       jdc 	/* XXX Assumes 512 bytes / sector */
    478       1.28   tsutsui 	if (params->sectorsize != 512) {
    479       1.19       jdc 		warnx("Media is %d bytes/sector."
    480       1.19       jdc 			"  RAID is only supported on 512 bytes/sector media.",
    481       1.28   tsutsui 			params->sectorsize);
    482       1.19       jdc 		return 0;
    483       1.19       jdc 	}
    484       1.19       jdc 	return ffs_match_common(params, (off_t) RF_PROTECTED_SECTORS);
    485       1.19       jdc }
    486       1.19       jdc 
    487       1.19       jdc int
    488       1.19       jdc ffs_match_common(ib_params *params, off_t offset)
    489       1.19       jdc {
    490        1.8      fvdl 	char		sbbuf[SBLOCKSIZE];
    491        1.1     lukem 	struct fs	*fs;
    492        1.8      fvdl 	int i;
    493        1.8      fvdl 	off_t loc;
    494        1.1     lukem 
    495        1.2     lukem 	assert(params != NULL);
    496        1.5     lukem 	assert(params->fstype != NULL);
    497        1.2     lukem 
    498        1.1     lukem 	fs = (struct fs *)sbbuf;
    499        1.8      fvdl 	for (i = 0; sblock_try[i] != -1; i++) {
    500       1.28   tsutsui 		loc = sblock_try[i] / params->sectorsize + offset;
    501        1.8      fvdl 		if (!ffs_read_disk_block(params, loc, SBLOCKSIZE, sbbuf))
    502        1.8      fvdl 			continue;
    503        1.8      fvdl 		switch (fs->fs_magic) {
    504        1.8      fvdl 		case FS_UFS2_MAGIC:
    505        1.8      fvdl 			is_ufs2 = 1;
    506        1.8      fvdl 			/* FALLTHROUGH */
    507        1.8      fvdl 		case FS_UFS1_MAGIC:
    508        1.8      fvdl 			params->fstype->needswap = 0;
    509        1.8      fvdl 			params->fstype->blocksize = fs->fs_bsize;
    510        1.8      fvdl 			params->fstype->sblockloc = loc;
    511       1.19       jdc 			params->fstype->offset = offset;
    512       1.14       dsl 			break;
    513       1.18       dsl #ifndef FFS_NO_SWAP
    514        1.8      fvdl 		case FS_UFS2_MAGIC_SWAPPED:
    515        1.8      fvdl 			is_ufs2 = 1;
    516        1.8      fvdl 			/* FALLTHROUGH */
    517        1.8      fvdl 		case FS_UFS1_MAGIC_SWAPPED:
    518        1.8      fvdl 			params->fstype->needswap = 1;
    519        1.8      fvdl 			params->fstype->blocksize = bswap32(fs->fs_bsize);
    520        1.8      fvdl 			params->fstype->sblockloc = loc;
    521       1.19       jdc 			params->fstype->offset = offset;
    522       1.14       dsl 			break;
    523       1.18       dsl #endif
    524        1.8      fvdl 		default:
    525        1.8      fvdl 			continue;
    526        1.8      fvdl 		}
    527       1.14       dsl 		if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2)
    528       1.14       dsl 			continue;
    529       1.14       dsl 		return 1;
    530        1.5     lukem 	}
    531        1.1     lukem 
    532        1.8      fvdl 	return (0);
    533        1.1     lukem }
    534        1.1     lukem 
    535        1.1     lukem int
    536        1.1     lukem ffs_findstage2(ib_params *params, uint32_t *maxblk, ib_block *blocks)
    537        1.1     lukem {
    538        1.1     lukem 	int			rv;
    539        1.1     lukem 	uint32_t		ino;
    540        1.1     lukem 	struct findblks_state	state;
    541        1.1     lukem 
    542        1.2     lukem 	assert(params != NULL);
    543        1.2     lukem 	assert(params->stage2 != NULL);
    544        1.2     lukem 	assert(maxblk != NULL);
    545        1.2     lukem 	assert(blocks != NULL);
    546        1.5     lukem 
    547        1.5     lukem 	if (params->flags & IB_STAGE2START)
    548        1.5     lukem 		return (hardcode_stage2(params, maxblk, blocks));
    549        1.1     lukem 
    550        1.1     lukem 	/* The secondary bootstrap must be clearly in /. */
    551        1.1     lukem 	if (params->stage2[0] == '/')
    552        1.1     lukem 		params->stage2++;
    553        1.1     lukem 	if (strchr(params->stage2, '/') != NULL) {
    554        1.1     lukem 		warnx("The secondary bootstrap `%s' must be in /",
    555        1.1     lukem 		    params->stage2);
    556       1.26       apb 		warnx("(Path must be relative to the file system in `%s')",
    557       1.26       apb 		    params->filesystem);
    558        1.1     lukem 		return (0);
    559        1.1     lukem 	}
    560        1.1     lukem 
    561        1.1     lukem 	/* Get the inode number of the secondary bootstrap. */
    562        1.8      fvdl 	if (is_ufs2)
    563  1.29.12.1       tls 		rv = ffs_find_disk_blocks_ufs2(params, UFS_ROOTINO,
    564        1.8      fvdl 		    ffs_findstage2_ino, &ino);
    565        1.8      fvdl 	else
    566  1.29.12.1       tls 		rv = ffs_find_disk_blocks_ufs1(params, UFS_ROOTINO,
    567        1.8      fvdl 		    ffs_findstage2_ino, &ino);
    568        1.6     lukem 	if (rv != 2) {
    569        1.6     lukem 		warnx("Could not find secondary bootstrap `%s' in `%s'",
    570        1.6     lukem 		    params->stage2, params->filesystem);
    571       1.26       apb 		warnx("(Path must be relative to the file system in `%s')",
    572       1.26       apb 		    params->filesystem);
    573        1.1     lukem 		return (0);
    574        1.6     lukem 	}
    575        1.1     lukem 
    576        1.1     lukem 	/* Record the disk blocks of the secondary bootstrap. */
    577        1.1     lukem 	state.maxblk = *maxblk;
    578        1.1     lukem 	state.nblk = 0;
    579        1.1     lukem 	state.blocks = blocks;
    580        1.8      fvdl 	if (is_ufs2)
    581        1.8      fvdl 		rv = ffs_find_disk_blocks_ufs2(params, ino,
    582        1.8      fvdl 		    ffs_findstage2_blocks, &state);
    583       1.12       dsl 	else
    584        1.8      fvdl 		rv = ffs_find_disk_blocks_ufs1(params, ino,
    585        1.8      fvdl 		    ffs_findstage2_blocks, &state);
    586        1.6     lukem 	if (! rv) {
    587        1.1     lukem 		return (0);
    588        1.6     lukem 	}
    589        1.1     lukem 
    590        1.1     lukem 	*maxblk = state.nblk;
    591        1.1     lukem 	return (1);
    592        1.1     lukem }
    593