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