2 * Copyright (c) 2002 McAfee, Inc.
5 * This software was developed for the FreeBSD Project by Marshall
6 * Kirk McKusick and McAfee Research,, the Security Research Division of
7 * McAfee, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as
8 * part of the DARPA CHATS research program
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Copyright (c) 1998 Robert Nordier
33 * All rights reserved.
35 * Redistribution and use in source and binary forms are freely
36 * permitted provided that the above copyright notice and this
37 * paragraph and the following disclaimer are duplicated in all
40 * This software is provided "AS IS" and without any express or
41 * implied warranties, including, without limitation, the implied
42 * warranties of merchantability and fitness for a particular
46 #include <sys/cdefs.h>
47 #include <ufs/ufs/dinode.h>
48 #include <ufs/ufs/dir.h>
49 #include <ufs/ffs/fs.h>
51 #ifdef UFS_SMALL_CGBASE
52 /* XXX: Revert to old (broken for over 1.5Tb filesystems) version of cgbase
53 (see sys/ufs/ffs/fs.h rev 1.39) so that small boot loaders (e.g. boot2) can
54 support both UFS1 and UFS2. */
56 #define cgbase(fs, c) ((ufs2_daddr_t)((fs)->fs_fpg * (c)))
59 typedef uint32_t ufs_ino_t;
62 * We use 4k `virtual' blocks for filesystem data, whatever the actual
63 * filesystem block size. FFS blocks are always a multiple of 4k.
66 #define VBLKSIZE (1 << VBLKSHIFT)
67 #define VBLKMASK (VBLKSIZE - 1)
68 #define DBPERVBLK (VBLKSIZE / DEV_BSIZE)
69 #define INDIRPERVBLK(fs) (NINDIR(fs) / ((fs)->fs_bsize >> VBLKSHIFT))
70 #define IPERVBLK(fs) (INOPB(fs) / ((fs)->fs_bsize >> VBLKSHIFT))
71 #define INO_TO_VBA(fs, ipervblk, x) \
72 (fsbtodb(fs, cgimin(fs, ino_to_cg(fs, x))) + \
73 (((x) % (fs)->fs_ipg) / (ipervblk) * DBPERVBLK))
74 #define INO_TO_VBO(ipervblk, x) ((x) % ipervblk)
75 #define FS_TO_VBA(fs, fsb, off) (fsbtodb(fs, fsb) + \
76 ((off) / VBLKSIZE) * DBPERVBLK)
77 #define FS_TO_VBO(fs, fsb, off) ((off) & VBLKMASK)
79 /* Buffers that must not span a 64k boundary. */
81 char blkbuf[VBLKSIZE]; /* filesystem blocks */
82 char indbuf[VBLKSIZE]; /* indir blocks */
83 char sbbuf[SBLOCKSIZE]; /* superblock */
84 char secbuf[DEV_BSIZE]; /* for MBR/disklabel */
86 static struct dmadat *dmadat;
88 static ufs_ino_t lookup(const char *);
89 static ssize_t fsread(ufs_ino_t, void *, size_t);
91 static uint8_t ls, dsk_meta;
92 static uint32_t fs_off;
94 static __inline uint8_t
95 fsfind(const char *name, ufs_ino_t * ino)
97 static char buf[DEV_BSIZE];
98 static struct direct d;
103 while ((n = fsread(*ino, buf, DEV_BSIZE)) > 0)
104 for (s = buf; s < buf + DEV_BSIZE;) {
105 memcpy(&d, s, sizeof(struct direct));
107 printf("%s ", d.d_name);
108 else if (!strcmp(name, d.d_name)) {
120 lookup(const char *path)
122 static char name[UFS_MAXNAMLEN + 1];
135 for (s = path; *s && *s != '/'; s++);
136 if ((n = s - path) > UFS_MAXNAMLEN)
138 ls = *path == '?' && n == 1 && !*s;
139 memcpy(name, path, n);
142 printf("%s: not a directory.\n", name);
145 if ((dt = fsfind(name, &ino)) <= 0)
149 return dt == DT_REG ? ino : 0;
153 * Possible superblock locations ordered from most to least likely.
155 static int sblock_try[] = SBLOCKSEARCH;
157 #if defined(UFS2_ONLY)
158 #define DIP(field) dp2.field
159 #elif defined(UFS1_ONLY)
160 #define DIP(field) dp1.field
162 #define DIP(field) fs.fs_magic == FS_UFS1_MAGIC ? dp1.field : dp2.field
166 fsread_size(ufs_ino_t inode, void *buf, size_t nbyte, size_t *fsizep)
169 static struct ufs1_dinode dp1;
173 static struct ufs2_dinode dp2;
176 static ufs_ino_t inomap;
180 size_t n, nb, size, off, vboff;
182 ufs2_daddr_t addr2, vbaddr;
183 static ufs2_daddr_t blkmap, indmap;
186 /* Basic parameter validation. */
187 if ((buf == NULL && nbyte != 0) || dmadat == NULL)
190 blkbuf = dmadat->blkbuf;
191 indbuf = dmadat->indbuf;
194 * Force probe if inode is zero to ensure we have a valid fs, otherwise
195 * when probing multiple paritions, reads from subsequent parititions
196 * will incorrectly succeed.
198 if (!dsk_meta || inode == 0) {
201 for (n = 0; sblock_try[n] != -1; n++) {
202 if (dskread(dmadat->sbbuf, sblock_try[n] / DEV_BSIZE,
203 SBLOCKSIZE / DEV_BSIZE))
205 memcpy(&fs, dmadat->sbbuf, sizeof(struct fs));
207 #if defined(UFS1_ONLY)
208 fs.fs_magic == FS_UFS1_MAGIC
209 #elif defined(UFS2_ONLY)
210 (fs.fs_magic == FS_UFS2_MAGIC &&
211 fs.fs_sblockloc == sblock_try[n])
213 fs.fs_magic == FS_UFS1_MAGIC ||
214 (fs.fs_magic == FS_UFS2_MAGIC &&
215 fs.fs_sblockloc == sblock_try[n])
218 fs.fs_bsize <= MAXBSIZE &&
219 fs.fs_bsize >= (int32_t)sizeof(struct fs))
222 if (sblock_try[n] == -1) {
227 memcpy(&fs, dmadat->sbbuf, sizeof(struct fs));
230 if (inomap != inode) {
232 if (dskread(blkbuf, INO_TO_VBA(&fs, n, inode), DBPERVBLK))
234 n = INO_TO_VBO(n, inode);
235 #if defined(UFS1_ONLY)
236 memcpy(&dp1, (struct ufs1_dinode *)(void *)blkbuf + n,
238 #elif defined(UFS2_ONLY)
239 memcpy(&dp2, (struct ufs2_dinode *)(void *)blkbuf + n,
242 if (fs.fs_magic == FS_UFS1_MAGIC)
243 memcpy(&dp1, (struct ufs1_dinode *)(void *)blkbuf + n,
246 memcpy(&dp2, (struct ufs2_dinode *)(void *)blkbuf + n,
260 lbn = lblkno(&fs, fs_off);
261 off = blkoff(&fs, fs_off);
262 if (lbn < UFS_NDADDR) {
263 addr2 = DIP(di_db[lbn]);
264 } else if (lbn < UFS_NDADDR + NINDIR(&fs)) {
265 n = INDIRPERVBLK(&fs);
266 addr2 = DIP(di_ib[0]);
267 u = (u_int)(lbn - UFS_NDADDR) / n * DBPERVBLK;
268 vbaddr = fsbtodb(&fs, addr2) + u;
269 if (indmap != vbaddr) {
270 if (dskread(indbuf, vbaddr, DBPERVBLK))
274 n = (lbn - UFS_NDADDR) & (n - 1);
275 #if defined(UFS1_ONLY)
276 memcpy(&addr1, (ufs1_daddr_t *)indbuf + n,
277 sizeof(ufs1_daddr_t));
279 #elif defined(UFS2_ONLY)
280 memcpy(&addr2, (ufs2_daddr_t *)indbuf + n,
281 sizeof(ufs2_daddr_t));
283 if (fs.fs_magic == FS_UFS1_MAGIC) {
284 memcpy(&addr1, (ufs1_daddr_t *)indbuf + n,
285 sizeof(ufs1_daddr_t));
288 memcpy(&addr2, (ufs2_daddr_t *)indbuf + n,
289 sizeof(ufs2_daddr_t));
293 vbaddr = fsbtodb(&fs, addr2) + (off >> VBLKSHIFT) * DBPERVBLK;
294 vboff = off & VBLKMASK;
295 n = sblksize(&fs, (off_t)size, lbn) - (off & ~VBLKMASK);
298 if (blkmap != vbaddr) {
299 if (dskread(blkbuf, vbaddr, n >> DEV_BSHIFT))
306 memcpy(s, blkbuf + vboff, n);
319 fsread(ufs_ino_t inode, void *buf, size_t nbyte)
322 return fsread_size(inode, buf, nbyte, NULL);