diff options
author | Tim Deegan <Tim.Deegan@xensource.com> | 2006-11-09 14:09:53 +0000 |
---|---|---|
committer | Tim Deegan <Tim.Deegan@xensource.com> | 2006-11-09 14:09:53 +0000 |
commit | cddc1d5a09b0ee520e31d9f56dfb4a20f6fb3ba1 (patch) | |
tree | 40779970f5ad69d7f5cea225844c430e48812b8c /tools/libfsimage/ufs | |
parent | 21b5e9be6acce19ff8aa22e55a0ddc69d7b19750 (diff) | |
download | xen-cddc1d5a09b0ee520e31d9f56dfb4a20f6fb3ba1.tar.gz xen-cddc1d5a09b0ee520e31d9f56dfb4a20f6fb3ba1.tar.bz2 xen-cddc1d5a09b0ee520e31d9f56dfb4a20f6fb3ba1.zip |
Add libfsimage, a C library for reading files from filesystem images.
Initial support is provided for Solaris UFS, ext2 (both using libext2fs
and not), and reiserfs.
Signed-off-by: John Levon <john.levon@sun.com>
Diffstat (limited to 'tools/libfsimage/ufs')
-rw-r--r-- | tools/libfsimage/ufs/Makefile | 13 | ||||
-rw-r--r-- | tools/libfsimage/ufs/fsys_ufs.c | 276 | ||||
-rw-r--r-- | tools/libfsimage/ufs/ufs.h | 228 |
3 files changed, 517 insertions, 0 deletions
diff --git a/tools/libfsimage/ufs/Makefile b/tools/libfsimage/ufs/Makefile new file mode 100644 index 0000000000..b7218c2b3f --- /dev/null +++ b/tools/libfsimage/ufs/Makefile @@ -0,0 +1,13 @@ +XEN_ROOT = ../../.. + +LIB_SRCS-y = fsys_ufs.c + +FS = ufs + +.PHONY: all +all: fs-all + +.PHONY: install +install: fs-install + +include $(XEN_ROOT)/tools/libfsimage/Rules.mk diff --git a/tools/libfsimage/ufs/fsys_ufs.c b/tools/libfsimage/ufs/fsys_ufs.c new file mode 100644 index 0000000000..f1cb917c8c --- /dev/null +++ b/tools/libfsimage/ufs/fsys_ufs.c @@ -0,0 +1,276 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2006 Free Software Foundation, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +/* + * Copyright 2006 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +/* From Solaris usr/src/stand/lib/fs/ufs/ufsops.c */ + +#include <fsimage_grub.h> + +#include "ufs.h" + +/* These are the pools of buffers, etc. */ + +#define SUPERBLOCK ((struct fs *)(FSYS_BUF + 0x2000)) +#define INODE ((struct icommon *)(FSYS_BUF + 0x1000)) +#define DIRENT (FSYS_BUF + 0x4000) +#define INDIRBLK1 ((grub_daddr32_t *)(FSYS_BUF + 0x4000)) /* 2+ indir blk */ +#define INDIRBLK0 ((grub_daddr32_t *)(FSYS_BUF+ 0x6000)) /* 1st indirect blk */ + +#define indirblk0 (*fsig_int1(ffi)) +#define indirblk1 (*fsig_int2(ffi)) + +static int openi(fsi_file_t *, grub_ino_t); +static grub_ino_t dlook(fsi_file_t *, grub_ino_t, char *); +static grub_daddr32_t sbmap(fsi_file_t *, grub_daddr32_t); + +/* read superblock and check fs magic */ +int +ufs_mount(fsi_file_t *ffi) +{ + if (/*! IS_PC_SLICE_TYPE_SOLARIS(current_slice) || */ + !devread(ffi, UFS_SBLOCK, 0, UFS_SBSIZE, (char *)SUPERBLOCK) || + SUPERBLOCK->fs_magic != UFS_MAGIC) + return 0; + + return 1; +} + + +/* + * searching for a file, if successful, inode will be loaded in INODE + * The entry point should really be named ufs_open(char *pathname). + * For now, keep it consistent with the rest of fsys modules. + */ +int +ufs_dir(fsi_file_t *ffi, char *dirname) +{ + grub_ino_t inode = ROOTINO; /* start from root */ + char *fname, ch; + + indirblk0 = indirblk1 = 0; + + /* skip leading slashes */ + while (*dirname == '/') + dirname++; + + while (inode && *dirname && !isspace(*dirname)) { + if (!openi(ffi, inode)) + return 0; + + /* parse for next path component */ + fname = dirname; + while (*dirname && !isspace(*dirname) && *dirname != '/') + dirname++; + ch = *dirname; + *dirname = 0; /* ensure null termination */ + + inode = dlook(ffi, inode, fname); + *dirname = ch; + while (*dirname == '/') + dirname++; + } + + /* return 1 only if inode exists and is a regular file */ + if (! openi(ffi, inode)) + return (0); + filepos = 0; + filemax = INODE->ic_sizelo; + return (inode && ((INODE->ic_smode & IFMT) == IFREG)); +} + +/* + * This is the high-level read function. + */ +int +ufs_read(fsi_file_t *ffi, char *buf, int len) +{ + int off, size, ret = 0, ok; + grub_daddr32_t lblk, dblk; + + while (len) { + off = blkoff(SUPERBLOCK, filepos); + lblk = lblkno(SUPERBLOCK, filepos); + size = SUPERBLOCK->fs_bsize; + size -= off; + if (size > len) + size = len; + + if ((dblk = sbmap(ffi, lblk)) <= 0) { + /* we are in a file hole, just zero the buf */ + grub_memset(buf, 0, size); + } else { + disk_read_func = disk_read_hook; + ok = devread(ffi, fsbtodb(SUPERBLOCK, dblk), + off, size, buf); + disk_read_func = 0; + if (!ok) + return 0; + } + buf += size; + len -= size; + filepos += size; + ret += size; + } + + return (ret); +} + +int +ufs_embed (int *start_sector, int needed_sectors) +{ + if (needed_sectors > 14) + return 0; + + *start_sector = 2; + return 1; +} + +/* read inode and place content in INODE */ +static int +openi(fsi_file_t *ffi, grub_ino_t inode) +{ + grub_daddr32_t dblk; + int off; + + /* get block and byte offset into the block */ + dblk = fsbtodb(SUPERBLOCK, itod(SUPERBLOCK, inode)); + off = itoo(SUPERBLOCK, inode) * sizeof (struct icommon); + + return (devread(ffi, dblk, off, sizeof (struct icommon), (char *)INODE)); +} + +/* + * Performs fileblock mapping. Convert file block no. to disk block no. + * Returns 0 when block doesn't exist and <0 when block isn't initialized + * (i.e belongs to a hole in the file). + */ +grub_daddr32_t +sbmap(fsi_file_t *ffi, grub_daddr32_t bn) +{ + int level, bound, i, index; + grub_daddr32_t nb, blkno; + grub_daddr32_t *db = INODE->ic_db; + + /* blocks 0..UFS_NDADDR are direct blocks */ + if (bn < UFS_NDADDR) { + return db[bn]; + } + + /* determine how many levels of indirection. */ + level = 0; + bn -= UFS_NDADDR; + bound = UFS_NINDIR(SUPERBLOCK); + while (bn >= bound) { + level++; + bn -= bound; + bound *= UFS_NINDIR(SUPERBLOCK); + } + if (level >= UFS_NIADDR) /* bn too big */ + return ((grub_daddr32_t)0); + + /* fetch the first indirect block */ + nb = INODE->ic_ib[level]; + if (nb == 0) { + return ((grub_daddr32_t)0); + } + if (indirblk0 != nb) { + indirblk0 = 0; + blkno = fsbtodb(SUPERBLOCK, nb); + if (!devread(ffi, blkno, 0, SUPERBLOCK->fs_bsize, + (char *)INDIRBLK0)) + return (0); + indirblk0 = nb; + } + bound /= UFS_NINDIR(SUPERBLOCK); + index = (bn / bound) % UFS_NINDIR(SUPERBLOCK); + nb = INDIRBLK0[index]; + + /* fetch through the indirect blocks */ + for (i = 1; i <= level; i++) { + if (indirblk1 != nb) { + blkno = fsbtodb(SUPERBLOCK, nb); + if (!devread(ffi, blkno, 0, SUPERBLOCK->fs_bsize, + (char *)INDIRBLK1)) + return (0); + indirblk1 = nb; + } + bound /= UFS_NINDIR(SUPERBLOCK); + index = (bn / bound) % UFS_NINDIR(SUPERBLOCK); + nb = INDIRBLK1[index]; + if (nb == 0) + return ((grub_daddr32_t)0); + } + + return (nb); +} + +/* search directory content for name, return inode number */ +static grub_ino_t +dlook(fsi_file_t *ffi, grub_ino_t dir_ino, char *name) +{ + int loc, off; + grub_daddr32_t lbn, dbn, dblk; + struct direct *dp; + + if ((INODE->ic_smode & IFMT) != IFDIR) + return 0; + + loc = 0; + while (loc < INODE->ic_sizelo) { + /* offset into block */ + off = blkoff(SUPERBLOCK, loc); + if (off == 0) { /* need to read in a new block */ + /* get logical block number */ + lbn = lblkno(SUPERBLOCK, loc); + /* resolve indrect blocks */ + dbn = sbmap(ffi, lbn); + if (dbn == 0) + return (0); + + dblk = fsbtodb(SUPERBLOCK, dbn); + if (!devread(ffi, dblk, 0, SUPERBLOCK->fs_bsize, + (char *)DIRENT)) { + return 0; + } + } + + dp = (struct direct *)(DIRENT + off); + if (dp->d_ino && substring(name, dp->d_name) == 0) + return (dp->d_ino); + loc += dp->d_reclen; + } + return (0); +} + +fsi_plugin_ops_t * +fsi_init_plugin(int version, fsi_plugin_t *fp, const char **name) +{ + static fsig_plugin_ops_t ops = { + FSIMAGE_PLUGIN_VERSION, + .fpo_mount = ufs_mount, + .fpo_dir = ufs_dir, + .fpo_read = ufs_read + }; + + *name = "ufs"; + return (fsig_init(fp, &ops)); +} diff --git a/tools/libfsimage/ufs/ufs.h b/tools/libfsimage/ufs/ufs.h new file mode 100644 index 0000000000..4e7c736c6d --- /dev/null +++ b/tools/libfsimage/ufs/ufs.h @@ -0,0 +1,228 @@ +/* + * Copyright 2005 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + */ + +#ifndef _GRUB_UFS_H +#define _GRUB_UFS_H_ + +/* ufs specific constants */ +#define UFS_SBLOCK 16 +#define UFS_SBSIZE 8192 +#define UFS_MAGIC 0x011954 +#define ROOTINO 2 /* i number of all roots */ +#define UFS_NDADDR 12 /* direct blocks */ +#define UFS_NIADDR 3 /* indirect blocks */ +#define MAXMNTLEN 512 +#define MAXCSBUFS 32 +#define MAXNAMELEN 256 + +/* file types */ +#define IFMT 0xf000 +#define IFREG 0x8000 +#define IFDIR 0x4000 + +typedef unsigned char grub_uchar_t; +typedef unsigned short grub_ushort_t; +typedef unsigned short grub_o_mode_t; +typedef unsigned short grub_o_uid_t; +typedef unsigned short grub_o_gid_t; +typedef uint32_t grub_ino_t; +typedef int32_t grub_int32_t; +typedef int32_t grub_uid_t; +typedef int32_t grub_gid_t; +typedef uint32_t grub_uint32_t; +typedef uint32_t grub_daddr32_t; +typedef uint32_t grub_time32_t; +typedef struct { int val[2]; } grub_quad_t; + +struct timeval32 { + grub_time32_t tv_sec; + grub_int32_t tv_usec; +}; + +/* + * Per cylinder group information; summarized in blocks allocated + * from first cylinder group data blocks. These blocks have to be + * read in from fs_csaddr (size fs_cssize) in addition to the + * super block. + * + * N.B. sizeof (struct csum) must be a power of two in order for + * the ``fs_cs'' macro to work (see below). + */ +struct csum { + grub_int32_t cs_ndir; /* number of directories */ + grub_int32_t cs_nbfree; /* number of free blocks */ + grub_int32_t cs_nifree; /* number of free inodes */ + grub_int32_t cs_nffree; /* number of free frags */ +}; + +/* Ufs super block */ +struct fs { + grub_uint32_t fs_link; /* linked list of file systems */ + grub_uint32_t fs_rolled; /* logging only: fs fully rolled */ + grub_daddr32_t fs_sblkno; /* addr of super-block in filesys */ + grub_daddr32_t fs_cblkno; /* offset of cyl-block in filesys */ + grub_daddr32_t fs_iblkno; /* offset of inode-blocks in filesys */ + grub_daddr32_t fs_dblkno; /* offset of first data after cg */ + grub_int32_t fs_cgoffset; /* cylinder group offset in cylinder */ + grub_int32_t fs_cgmask; /* used to calc mod fs_ntrak */ + grub_time32_t fs_time; /* last time written */ + grub_int32_t fs_size; /* number of blocks in fs */ + grub_int32_t fs_dsize; /* number of data blocks in fs */ + grub_int32_t fs_ncg; /* number of cylinder groups */ + grub_int32_t fs_bsize; /* size of basic blocks in fs */ + grub_int32_t fs_fsize; /* size of frag blocks in fs */ + grub_int32_t fs_frag; /* number of frags in a block in fs */ + /* these are configuration parameters */ + grub_int32_t fs_minfree; /* minimum percentage of free blocks */ + grub_int32_t fs_rotdelay; /* num of ms for optimal next block */ + grub_int32_t fs_rps; /* disk revolutions per second */ + /* these fields can be computed from the others */ + grub_int32_t fs_bmask; /* ``blkoff'' calc of blk offsets */ + grub_int32_t fs_fmask; /* ``fragoff'' calc of frag offsets */ + grub_int32_t fs_bshift; /* ``lblkno'' calc of logical blkno */ + grub_int32_t fs_fshift; /* ``numfrags'' calc number of frags */ + /* these are configuration parameters */ + grub_int32_t fs_maxcontig; /* max number of contiguous blks */ + grub_int32_t fs_maxbpg; /* max number of blks per cyl group */ + /* these fields can be computed from the others */ + grub_int32_t fs_fragshift; /* block to frag shift */ + grub_int32_t fs_fsbtodb; /* fsbtodb and dbtofsb shift constant */ + grub_int32_t fs_sbsize; /* actual size of super block */ + grub_int32_t fs_csmask; /* csum block offset */ + grub_int32_t fs_csshift; /* csum block number */ + grub_int32_t fs_nindir; /* value of NINDIR */ + grub_int32_t fs_inopb; /* value of INOPB */ + grub_int32_t fs_nspf; /* value of NSPF */ + /* yet another configuration parameter */ + grub_int32_t fs_optim; /* optimization preference, see below */ + /* these fields are derived from the hardware */ + /* USL SVR4 compatibility */ + /* + * * USL SVR4 compatibility + * + * There was a significant divergence here between Solaris and + * SVR4 for x86. By swapping these two members in the superblock, + * we get read-only compatibility of SVR4 filesystems. Otherwise + * there would be no compatibility. This change was introduced + * during bootstrapping of Solaris on x86. By making this ifdef'ed + * on byte order, we provide ongoing compatibility across all + * platforms with the same byte order, the highest compatibility + * that can be achieved. + */ + grub_int32_t fs_state; /* file system state time stamp */ + grub_int32_t fs_si; /* summary info state - lufs only */ + grub_int32_t fs_trackskew; /* sector 0 skew, per track */ + /* unique id for this filesystem (currently unused and unmaintained) */ + /* In 4.3 Tahoe this space is used by fs_headswitch and fs_trkseek */ + /* Neither of those fields is used in the Tahoe code right now but */ + /* there could be problems if they are. */ + grub_int32_t fs_id[2]; /* file system id */ + /* sizes determined by number of cylinder groups and their sizes */ + grub_daddr32_t fs_csaddr; /* blk addr of cyl grp summary area */ + grub_int32_t fs_cssize; /* size of cyl grp summary area */ + grub_int32_t fs_cgsize; /* cylinder group size */ + /* these fields are derived from the hardware */ + grub_int32_t fs_ntrak; /* tracks per cylinder */ + grub_int32_t fs_nsect; /* sectors per track */ + grub_int32_t fs_spc; /* sectors per cylinder */ + /* this comes from the disk driver partitioning */ + grub_int32_t fs_ncyl; /* cylinders in file system */ + /* these fields can be computed from the others */ + grub_int32_t fs_cpg; /* cylinders per group */ + grub_int32_t fs_ipg; /* inodes per group */ + grub_int32_t fs_fpg; /* blocks per group * fs_frag */ + /* this data must be re-computed after crashes */ + struct csum fs_cstotal; /* cylinder summary information */ + /* these fields are cleared at mount time */ + char fs_fmod; /* super block modified flag */ + char fs_clean; /* file system state flag */ + char fs_ronly; /* mounted read-only flag */ + char fs_flags; /* largefiles flag, etc. */ + char fs_fsmnt[MAXMNTLEN]; /* name mounted on */ + /* these fields retain the current block allocation info */ + grub_int32_t fs_cgrotor; /* last cg searched */ + /* + * The following used to be fs_csp[MAXCSBUFS]. It was not + * used anywhere except in old utilities. We removed this + * in 5.6 and expect fs_u.fs_csp to be used instead. + * We no longer limit fs_cssize based on MAXCSBUFS. + */ + union { /* fs_cs (csum) info */ + grub_uint32_t fs_csp_pad[MAXCSBUFS]; + struct csum *fs_csp; + } fs_u; + grub_int32_t fs_cpc; /* cyl per cycle in postbl */ + short fs_opostbl[16][8]; /* old rotation block list head */ + grub_int32_t fs_sparecon[51]; /* reserved for future constants */ + grub_int32_t fs_version; /* minor version of MTB ufs */ + grub_int32_t fs_logbno; /* block # of embedded log */ + grub_int32_t fs_reclaim; /* reclaim open, deleted files */ + grub_int32_t fs_sparecon2; /* reserved for future constant */ + /* USL SVR4 compatibility */ + grub_int32_t fs_npsect; /* # sectors/track including spares */ + grub_quad_t fs_qbmask; /* ~fs_bmask - for use with quad size */ + grub_quad_t fs_qfmask; /* ~fs_fmask - for use with quad size */ + grub_int32_t fs_postblformat; /* fmt of positional layout tables */ + grub_int32_t fs_nrpos; /* number of rotaional positions */ + grub_int32_t fs_postbloff; /* (short) rotation block list head */ + grub_int32_t fs_rotbloff; /* (grub_uchar_t) blocks for each */ + /* rotation */ + grub_int32_t fs_magic; /* magic number */ + grub_uchar_t fs_space[1]; /* list of blocks for each rotation */ + /* actually longer */ +}; + +struct icommon { + grub_o_mode_t ic_smode; /* 0: mode and type of file */ + short ic_nlink; /* 2: number of links to file */ + grub_o_uid_t ic_suid; /* 4: owner's user id */ + grub_o_gid_t ic_sgid; /* 6: owner's group id */ + grub_uint32_t ic_sizelo; /* 8: number of bytes in file */ + grub_uint32_t ic_sizehi; /* 12: number of bytes in file */ + struct timeval32 ic_atime; /* 16: time last accessed */ + struct timeval32 ic_mtime; /* 24: time last modified */ + struct timeval32 ic_ctime; /* 32: last time inode changed */ + grub_daddr32_t ic_db[UFS_NDADDR]; /* 40: disk block addresses */ + grub_daddr32_t ic_ib[UFS_NIADDR]; /* 88: indirect blocks */ + grub_int32_t ic_flags; /* 100: cflags */ + grub_int32_t ic_blocks; /* 104: 512 byte blocks actually held */ + grub_int32_t ic_gen; /* 108: generation number */ + grub_int32_t ic_shadow; /* 112: shadow inode */ + grub_uid_t ic_uid; /* 116: long EFT version of uid */ + grub_gid_t ic_gid; /* 120: long EFT version of gid */ + grub_uint32_t ic_oeftflag; /* 124: extended attr directory ino, */ + /* 0 = none */ +}; + +struct direct { + grub_ino_t d_ino; + grub_ushort_t d_reclen; + grub_ushort_t d_namelen; + char d_name[MAXNAMELEN + 1]; +}; + +/* inode macros */ +#define INOPB(fs) ((fs)->fs_inopb) +#define itoo(fs, x) ((x) % (grub_uint32_t)INOPB(fs)) +#define itog(fs, x) ((x) / (grub_uint32_t)(fs)->fs_ipg) +#define itod(fs, x) ((grub_daddr32_t)(cgimin(fs, itog(fs, x)) + \ + (blkstofrags((fs), \ + ((x) % (grub_uint32_t)(fs)->fs_ipg / (grub_uint32_t)INOPB(fs)))))) + +/* block conversion macros */ +#define UFS_NINDIR(fs) ((fs)->fs_nindir) /* # of indirects */ +#define blkoff(fs, loc) ((int)((loc & ~(fs)->fs_bmask))) +#define lblkno(fs, loc) ((grub_int32_t)((loc) >> (fs)->fs_bshift)) +/* frag to blk */ +#define fsbtodb(fs, b) (((grub_daddr32_t)(b)) << (fs)->fs_fsbtodb) +#define blkstofrags(fs, b) ((b) << (fs)->fs_fragshift) + +/* cynlinder group macros */ +#define cgbase(fs, c) ((grub_daddr32_t)((fs)->fs_fpg * (c))) +#define cgimin(fs, c) (cgstart(fs, c) + (fs)->fs_iblkno) /* inode block */ +#define cgstart(fs, c) \ + (cgbase(fs, c) + (fs)->fs_cgoffset * ((c) & ~((fs)->fs_cgmask))) + +#endif /* !_GRUB_UFS_H */ |