2 * GRUB -- GRand Unified Bootloader
3 * Copyright (c) 2000, 2001 Free Software Foundation, Inc.
4 * Copyright (c) 2005 Rink Springer
6 * This file is based on FreeBSD 5.4-RELEASE's /sys/boot/common/ufsread.c,
7 * and has some minor patches so it'll work with Cromwell/GRUB.
11 * Copyright (c) 2002 McAfee, Inc.
12 * All rights reserved.
14 * This software was developed for the FreeBSD Project by Marshall
15 * Kirk McKusick and McAfee Research,, the Security Research Division of
16 * McAfee, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as
17 * part of the DARPA CHATS research program
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * Copyright (c) 1998 Robert Nordier
42 * All rights reserved.
44 * Redistribution and use in source and binary forms are freely
45 * permitted provided that the above copyright notice and this
46 * paragraph and the following disclaimer are duplicated in all
49 * This software is provided "AS IS" and without any express or
50 * implied warranties, including, without limitation, the implied
51 * warranties of merchantability and fitness for a particular
56 #include "asm/types.h"
61 #include "ufs_dinode.h"
65 /* XXX: Revert to old (broken for over 1.5Tb filesystems) version of cgbase
66 (see sys/ufs/ffs/fs.h rev 1.39) so that i386 boot loader (boot2) can
67 support both UFS1 and UFS2 again. */
69 #define cgbase(fs, c) ((ufs2_daddr_t)((fs)->fs_fpg * (c)))
73 * We use 4k `virtual' blocks for filesystem data, whatever the actual
74 * filesystem block size. FFS blocks are always a multiple of 4k.
77 #define VBLKSIZE (1 << VBLKSHIFT)
78 #define VBLKMASK (VBLKSIZE - 1)
79 #define DBPERVBLK (VBLKSIZE / DEV_BSIZE)
80 #define INDIRPERVBLK(fs) (NINDIR(fs) / ((fs)->fs_bsize >> VBLKSHIFT))
81 #define IPERVBLK(fs) (INOPB(fs) / ((fs)->fs_bsize >> VBLKSHIFT))
82 #define INO_TO_VBA(fs, ipervblk, x) \
83 (fsbtodb(fs, cgimin(fs, ino_to_cg(fs, x))) + \
84 (((x) % (fs)->fs_ipg) / (ipervblk) * DBPERVBLK))
85 #define INO_TO_VBO(ipervblk, x) ((x) % ipervblk)
86 #define FS_TO_VBA(fs, fsb, off) (fsbtodb(fs, fsb) + \
87 ((off) / VBLKSIZE) * DBPERVBLK)
88 #define FS_TO_VBO(fs, fsb, off) ((off) & VBLKMASK)
90 /* Buffers that must not span a 64k boundary. */
92 char blkbuf[VBLKSIZE]; /* filesystem blocks */
93 char indbuf[VBLKSIZE]; /* indir blocks */
94 char sbbuf[SBLOCKSIZE]; /* superblock */
95 char secbuf[DEV_BSIZE]; /* for MBR/disklabel */
97 static struct dmadat *dmadat = (struct dmadat*)FSYS_BUF;
99 #define SUPERBLOCK ((struct fs*)dmadat->sbbuf)
101 ino_t lookup(const char *);
102 ssize_t fsread(ino_t, void *, size_t);
105 static uint32_t fs_off;
106 static ino_t cur_ino = 0;
109 dskread (void* buf, unsigned lba, unsigned nblk)
111 return !devread (lba, 0, nblk * DEV_BSIZE, buf) ? -1 : 0;
114 #if defined(UFS2_ONLY)
115 #define DIP(field) dp2.field
116 #elif defined(UFS1_ONLY)
117 #define DIP(field) dp1.field
119 #define DIP(field) fs->fs_magic == FS_UFS1_MAGIC ? dp1.field : dp2.field
123 fsfind(const char *name, ino_t * ino)
126 struct ufs_dirent *d;
130 static struct ufs1_dinode dp1;
133 static struct ufs2_dinode dp2;
135 char* blkbuf = dmadat->blkbuf;
136 struct fs* fs = (struct fs *)dmadat->sbbuf;
139 while ((n = fsread(*ino, buf, DEV_BSIZE)) > 0)
140 for (s = buf; s < buf + DEV_BSIZE;) {
142 if (!strcmp(name, d->d_name)) {
145 /* below is for grub, which wants the file size
148 if (dskread(blkbuf, INO_TO_VBA(fs, n, (*ino)), DBPERVBLK))
150 n = INO_TO_VBO(n, (*ino));
151 #if defined(UFS1_ONLY)
152 dp1 = ((struct ufs1_dinode *)blkbuf)[n];
153 #elif defined(UFS2_ONLY)
154 dp2 = ((struct ufs2_dinode *)blkbuf)[n];
156 if (fs->fs_magic == FS_UFS1_MAGIC)
157 dp1 = ((struct ufs1_dinode *)blkbuf)[n];
159 dp2 = ((struct ufs2_dinode *)blkbuf)[n];
162 filemax = DIP(di_size);
171 lookup(const char *path)
173 char name[MAXNAMLEN + 1];
188 for (s = path; *s && *s != '/'; s++);
189 if ((n = s - path) > MAXNAMLEN)
191 memcpy(name, path, n);
194 printk("%s: not a directory.\n", name);
197 if ((dt = fsfind(name, &ino)) <= 0)
201 return dt == DT_REG ? ino : 0;
205 * Possible superblock locations ordered from most to least likely.
207 static const int sblock_try[] = SBLOCKSEARCH;
210 fsread(ino_t inode, void *buf, size_t nbyte)
213 static struct ufs1_dinode dp1;
216 static struct ufs2_dinode dp2;
223 size_t n, nb, size, off, vboff;
225 ufs2_daddr_t addr, vbaddr;
226 static ufs2_daddr_t blkmap, indmap;
230 blkbuf = dmadat->blkbuf;
231 indbuf = dmadat->indbuf;
232 fs = (struct fs *)dmadat->sbbuf;
235 for (n = 0; sblock_try[n] != -1; n++) {
236 if (dskread(fs, sblock_try[n] / DEV_BSIZE,
237 SBLOCKSIZE / DEV_BSIZE))
240 #if defined(UFS1_ONLY)
241 fs->fs_magic == FS_UFS1_MAGIC
242 #elif defined(UFS2_ONLY)
243 (fs->fs_magic == FS_UFS2_MAGIC &&
244 fs->fs_sblockloc == sblock_try[n])
246 fs->fs_magic == FS_UFS1_MAGIC ||
247 (fs->fs_magic == FS_UFS2_MAGIC &&
248 fs->fs_sblockloc == sblock_try[n])
251 fs->fs_bsize <= MAXBSIZE &&
252 fs->fs_bsize >= sizeof(struct fs))
255 if (sblock_try[n] == -1) {
263 if (inomap != inode) {
265 if (dskread(blkbuf, INO_TO_VBA(fs, n, inode), DBPERVBLK))
267 n = INO_TO_VBO(n, inode);
268 #if defined(UFS1_ONLY)
269 dp1 = ((struct ufs1_dinode *)blkbuf)[n];
270 #elif defined(UFS2_ONLY)
271 dp2 = ((struct ufs2_dinode *)blkbuf)[n];
273 if (fs->fs_magic == FS_UFS1_MAGIC)
274 dp1 = ((struct ufs1_dinode *)blkbuf)[n];
276 dp2 = ((struct ufs2_dinode *)blkbuf)[n];
289 lbn = lblkno(fs, fs_off);
290 off = blkoff(fs, fs_off);
292 addr = DIP(di_db[lbn]);
293 } else if (lbn < NDADDR + NINDIR(fs)) {
294 n = INDIRPERVBLK(fs);
295 addr = DIP(di_ib[0]);
296 u = (unsigned int)(lbn - NDADDR) / (n * DBPERVBLK);
297 vbaddr = fsbtodb(fs, addr) + u;
298 if (indmap != vbaddr) {
299 if (dskread(indbuf, vbaddr, DBPERVBLK))
303 n = (lbn - NDADDR) & (n - 1);
304 #if defined(UFS1_ONLY)
305 addr = ((ufs1_daddr_t *)indbuf)[n];
306 #elif defined(UFS2_ONLY)
307 addr = ((ufs2_daddr_t *)indbuf)[n];
309 if (fs->fs_magic == FS_UFS1_MAGIC)
310 addr = ((ufs1_daddr_t *)indbuf)[n];
312 addr = ((ufs2_daddr_t *)indbuf)[n];
317 vbaddr = fsbtodb(fs, addr) + (off >> VBLKSHIFT) * DBPERVBLK;
318 vboff = off & VBLKMASK;
319 n = sblksize(fs, size, lbn) - (off & ~VBLKMASK);
322 if (blkmap != vbaddr) {
323 if (dskread(blkbuf, vbaddr, n >> DEV_BSHIFT))
330 memcpy(s, blkbuf + vboff, n);
344 * We don't care about stuff being in disklabels or not. If the magic
345 * matches, we're good to go.
347 for (i = 0; sblock_try[i] != -1; ++i)
349 if (! (part_length < (sblock_try[i] + (SBLOCKSIZE / DEV_BSIZE))
350 || ! devread (0, sblock_try[i], SBLOCKSIZE, (char *) SUPERBLOCK)))
353 #if defined(UFS1_ONLY)
354 SUPERBLOCK->fs_magic == FS_UFS1_MAGIC
355 #elif defined(UFS2_ONLY)
356 (SUPERBLOCK->fs_magic == FS_UFS2_MAGIC &&
357 SUPERBLOCK->fs_sblockloc == sblock_try[i])
359 SUPERBLOCK->fs_magic == FS_UFS1_MAGIC ||
360 (SUPERBLOCK->fs_magic == FS_UFS2_MAGIC &&
361 SUPERBLOCK->fs_sblockloc == sblock_try[i])
372 ufs_read (char *buf, int len)
374 return fsread(cur_ino, buf, len);
378 ufs_dir (char *dirname)
380 cur_ino = lookup(dirname);
381 return cur_ino & 0xffffffff;
385 ufs_embed (int* start_sector, int needed_sectors)
387 /* TODO; unused by Cromwell */
391 #endif /* FSYS_UFS */