Unleashed v1.4
[unleashed.git] / usr / src / boot / sys / boot / common / ufsread.c
blob3dee081a80a45d979262877f43055f604182f3fd
1 /*
2 * Copyright (c) 2002 McAfee, Inc.
3 * All rights reserved.
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
12 * are met:
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
29 * SUCH DAMAGE.
31 /*-
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
38 * such forms.
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
43 * purpose.
46 #include <sys/cdefs.h>
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ufs/dir.h>
50 #include <ufs/ffs/fs.h>
52 #ifdef UFS_SMALL_CGBASE
53 /* XXX: Revert to old (broken for over 1.5Tb filesystems) version of cgbase
54 (see sys/ufs/ffs/fs.h rev 1.39) so that small boot loaders (e.g. boot2) can
55 support both UFS1 and UFS2. */
56 #undef cgbase
57 #define cgbase(fs, c) ((ufs2_daddr_t)((fs)->fs_fpg * (c)))
58 #endif
60 typedef uint32_t ufs_ino_t;
63 * We use 4k `virtual' blocks for filesystem data, whatever the actual
64 * filesystem block size. FFS blocks are always a multiple of 4k.
66 #define VBLKSHIFT 12
67 #define VBLKSIZE (1 << VBLKSHIFT)
68 #define VBLKMASK (VBLKSIZE - 1)
69 #define DBPERVBLK (VBLKSIZE / DEV_BSIZE)
70 #define INDIRPERVBLK(fs) (NINDIR(fs) / ((fs)->fs_bsize >> VBLKSHIFT))
71 #define IPERVBLK(fs) (INOPB(fs) / ((fs)->fs_bsize >> VBLKSHIFT))
72 #define INO_TO_VBA(fs, ipervblk, x) \
73 (fsbtodb(fs, cgimin(fs, ino_to_cg(fs, x))) + \
74 (((x) % (fs)->fs_ipg) / (ipervblk) * DBPERVBLK))
75 #define INO_TO_VBO(ipervblk, x) ((x) % ipervblk)
76 #define FS_TO_VBA(fs, fsb, off) (fsbtodb(fs, fsb) + \
77 ((off) / VBLKSIZE) * DBPERVBLK)
78 #define FS_TO_VBO(fs, fsb, off) ((off) & VBLKMASK)
80 /* Buffers that must not span a 64k boundary. */
81 struct dmadat {
82 char blkbuf[VBLKSIZE]; /* filesystem blocks */
83 char indbuf[VBLKSIZE]; /* indir blocks */
84 char sbbuf[SBLOCKSIZE]; /* superblock */
85 char secbuf[DEV_BSIZE]; /* for MBR/disklabel */
87 static struct dmadat *dmadat;
89 static ufs_ino_t lookup(const char *);
90 static ssize_t fsread(ufs_ino_t, void *, size_t);
91 static uint8_t inode_type(ufs_ino_t inode);
93 static uint8_t ls, dsk_meta;
94 static uint32_t fs_off;
96 #ifndef UFS2_ONLY
97 static struct ufs1_dinode dp1;
98 ufs1_daddr_t addr1;
99 #endif
100 #ifndef UFS1_ONLY
101 static struct ufs2_dinode dp2;
102 #endif
103 static struct fs fs;
104 static ufs_ino_t inomap;
105 static ufs2_daddr_t blkmap, indmap;
107 static __inline uint8_t
108 fsfind(const char *name, ufs_ino_t * ino)
110 static char buf[DEV_BSIZE];
111 struct direct *d;
112 char *s;
113 ssize_t n;
115 fs_off = 0;
116 while ((n = fsread(*ino, buf, DEV_BSIZE)) > 0)
117 for (s = buf; s < buf + DEV_BSIZE;) {
118 d = (void *)s;
119 if (ls)
120 printf("%s ", d->d_name);
121 else if (!strcmp(name, d->d_name)) {
122 *ino = d->d_ino;
123 return inode_type(*ino);
125 s += d->d_reclen;
127 if (n != -1 && ls)
128 printf("\n");
129 return 0;
132 static ufs_ino_t
133 lookup(const char *path)
135 static char name[UFS_MAXNAMLEN + 1];
136 const char *s;
137 ufs_ino_t ino;
138 ssize_t n;
139 uint8_t dt;
141 ino = ROOTINO;
142 dt = DT_DIR;
143 for (;;) {
144 if (*path == '/')
145 path++;
146 if (!*path)
147 break;
148 for (s = path; *s && *s != '/'; s++);
149 if ((n = s - path) > UFS_MAXNAMLEN)
150 return 0;
151 ls = *path == '?' && n == 1 && !*s;
152 memcpy(name, path, n);
153 name[n] = 0;
154 if (dt != DT_DIR) {
155 printf("%s: not a directory.\n", name);
156 return (0);
158 if ((dt = fsfind(name, &ino)) <= 0)
159 break;
160 path = s;
162 return dt == DT_REG ? ino : 0;
166 * Possible superblock locations ordered from most to least likely.
168 static int sblock_try[] = SBLOCKSEARCH;
170 #if defined(UFS2_ONLY)
171 #define DIP(field) dp2.field
172 #elif defined(UFS1_ONLY)
173 #define DIP(field) dp1.field
174 #else
175 #define DIP(field) fs.fs_magic == FS_UFS1_MAGIC ? dp1.field : dp2.field
176 #endif
178 static uint8_t
179 inode_type(ufs_ino_t inode)
181 char *blkbuf;
182 size_t n;
184 blkbuf = dmadat->blkbuf;
186 if (!inode)
187 return (0);
188 if (inomap != inode) {
189 n = IPERVBLK(&fs);
190 if (dskread(blkbuf, INO_TO_VBA(&fs, n, inode), DBPERVBLK))
191 return (-1);
192 n = INO_TO_VBO(n, inode);
193 #if defined(UFS1_ONLY)
194 memcpy(&dp1, (struct ufs1_dinode *)blkbuf + n,
195 sizeof(struct ufs1_dinode));
196 #elif defined(UFS2_ONLY)
197 memcpy(&dp2, (struct ufs2_dinode *)blkbuf + n,
198 sizeof(struct ufs2_dinode));
199 #else
200 if (fs.fs_magic == FS_UFS1_MAGIC)
201 memcpy(&dp1, (struct ufs1_dinode *)blkbuf + n,
202 sizeof(struct ufs1_dinode));
203 else
204 memcpy(&dp2, (struct ufs2_dinode *)blkbuf + n,
205 sizeof(struct ufs2_dinode));
206 #endif
207 inomap = inode;
208 fs_off = 0;
209 blkmap = indmap = 0;
211 return (IFTODT(DIP(di_mode)));
214 static ssize_t
215 fsread_size(ufs_ino_t inode, void *buf, size_t nbyte, size_t *fsizep)
217 char *blkbuf;
218 void *indbuf;
219 char *s;
220 size_t n, nb, size, off, vboff;
221 ufs_lbn_t lbn;
222 ufs2_daddr_t addr2, vbaddr;
223 u_int u;
225 /* Basic parameter validation. */
226 if ((buf == NULL && nbyte != 0) || dmadat == NULL)
227 return (-1);
229 blkbuf = dmadat->blkbuf;
230 indbuf = dmadat->indbuf;
233 * Force probe if inode is zero to ensure we have a valid fs, otherwise
234 * when probing multiple paritions, reads from subsequent parititions
235 * will incorrectly succeed.
237 if (!dsk_meta || inode == 0) {
238 inomap = 0;
239 dsk_meta = 0;
240 for (n = 0; sblock_try[n] != -1; n++) {
241 if (dskread(dmadat->sbbuf, sblock_try[n] / DEV_BSIZE,
242 SBLOCKSIZE / DEV_BSIZE))
243 return -1;
244 memcpy(&fs, dmadat->sbbuf, sizeof(struct fs));
245 if ((
246 #if defined(UFS1_ONLY)
247 fs.fs_magic == FS_UFS1_MAGIC
248 #elif defined(UFS2_ONLY)
249 (fs.fs_magic == FS_UFS2_MAGIC &&
250 fs.fs_sblockloc == sblock_try[n])
251 #else
252 fs.fs_magic == FS_UFS1_MAGIC ||
253 (fs.fs_magic == FS_UFS2_MAGIC &&
254 fs.fs_sblockloc == sblock_try[n])
255 #endif
256 ) &&
257 fs.fs_bsize <= MAXBSIZE &&
258 fs.fs_bsize >= (int32_t)sizeof(struct fs))
259 break;
261 if (sblock_try[n] == -1) {
262 return -1;
264 dsk_meta++;
265 } else
266 memcpy(&fs, dmadat->sbbuf, sizeof(struct fs));
267 if (!inode)
268 return 0;
269 if (inomap != inode) {
270 n = IPERVBLK(&fs);
271 if (dskread(blkbuf, INO_TO_VBA(&fs, n, inode), DBPERVBLK))
272 return -1;
273 n = INO_TO_VBO(n, inode);
274 #if defined(UFS1_ONLY)
275 memcpy(&dp1, (struct ufs1_dinode *)(void *)blkbuf + n,
276 sizeof(dp1));
277 #elif defined(UFS2_ONLY)
278 memcpy(&dp2, (struct ufs2_dinode *)(void *)blkbuf + n,
279 sizeof(dp2));
280 #else
281 if (fs.fs_magic == FS_UFS1_MAGIC)
282 memcpy(&dp1, (struct ufs1_dinode *)(void *)blkbuf + n,
283 sizeof(dp1));
284 else
285 memcpy(&dp2, (struct ufs2_dinode *)(void *)blkbuf + n,
286 sizeof(dp2));
287 #endif
288 inomap = inode;
289 fs_off = 0;
290 blkmap = indmap = 0;
292 s = buf;
293 size = DIP(di_size);
294 n = size - fs_off;
295 if (nbyte > n)
296 nbyte = n;
297 nb = nbyte;
298 while (nb) {
299 lbn = lblkno(&fs, fs_off);
300 off = blkoff(&fs, fs_off);
301 if (lbn < NDADDR) {
302 addr2 = DIP(di_db[lbn]);
303 } else if (lbn < NDADDR + NINDIR(&fs)) {
304 n = INDIRPERVBLK(&fs);
305 addr2 = DIP(di_ib[0]);
306 u = (u_int)(lbn - NDADDR) / n * DBPERVBLK;
307 vbaddr = fsbtodb(&fs, addr2) + u;
308 if (indmap != vbaddr) {
309 if (dskread(indbuf, vbaddr, DBPERVBLK))
310 return -1;
311 indmap = vbaddr;
313 n = (lbn - NDADDR) & (n - 1);
314 #if defined(UFS1_ONLY)
315 memcpy(&addr1, (ufs1_daddr_t *)indbuf + n,
316 sizeof(ufs1_daddr_t));
317 addr2 = addr1;
318 #elif defined(UFS2_ONLY)
319 memcpy(&addr2, (ufs2_daddr_t *)indbuf + n,
320 sizeof(ufs2_daddr_t));
321 #else
322 if (fs.fs_magic == FS_UFS1_MAGIC) {
323 memcpy(&addr1, (ufs1_daddr_t *)indbuf + n,
324 sizeof(ufs1_daddr_t));
325 addr2 = addr1;
326 } else
327 memcpy(&addr2, (ufs2_daddr_t *)indbuf + n,
328 sizeof(ufs2_daddr_t));
329 #endif
330 } else
331 return -1;
332 vbaddr = fsbtodb(&fs, addr2) + (off >> VBLKSHIFT) * DBPERVBLK;
333 vboff = off & VBLKMASK;
334 n = sblksize(&fs, (off_t)size, lbn) - (off & ~VBLKMASK);
335 if (n > VBLKSIZE)
336 n = VBLKSIZE;
337 if (blkmap != vbaddr) {
338 if (dskread(blkbuf, vbaddr, n >> DEV_BSHIFT))
339 return -1;
340 blkmap = vbaddr;
342 n -= vboff;
343 if (n > nb)
344 n = nb;
345 memcpy(s, blkbuf + vboff, n);
346 s += n;
347 fs_off += n;
348 nb -= n;
351 if (fsizep != NULL)
352 *fsizep = size;
354 return nbyte;
357 static ssize_t
358 fsread(ufs_ino_t inode, void *buf, size_t nbyte)
361 return fsread_size(inode, buf, nbyte, NULL);