2
* GRUB -- GRand Unified Bootloader
3
* Copyright (C) 2000, 2001 Free Software Foundation, Inc.
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
* Elements of this file were originally from the FreeBSD "biosboot"
22
* bootloader file "disk.c" dated 4/12/95.
24
* The license and header comments from that file are included here.
28
* Mach Operating System
29
* Copyright (c) 1992, 1991 Carnegie Mellon University
30
* All Rights Reserved.
32
* Permission to use, copy, modify and distribute this software and its
33
* documentation is hereby granted, provided that both the copyright
34
* notice and this permission notice appear in all copies of the
35
* software, derivative works or modified versions, and any portions
36
* thereof, and that both notices appear in supporting documentation.
38
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
39
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
40
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
42
* Carnegie Mellon requests users of this software to return to
44
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
45
* School of Computer Science
46
* Carnegie Mellon University
47
* Pittsburgh PA 15213-3890
49
* any improvements or extensions that they make and grant Carnegie Mellon
50
* the rights to redistribute these changes.
52
* from: Mach, Revision 2.2 92/04/04 11:35:49 rpd
53
* $Id: fsys_ffs.c,v 1.10 2001/11/12 06:57:29 okuji Exp $
63
#include "disk_inode.h"
64
#include "disk_inode_ffs.h"
68
/* used for filesystem map blocks */
70
static int mapblock_offset;
71
static int mapblock_bsize;
73
/* pointer to superblock */
74
#define SUPERBLOCK ((struct fs *) ( FSYS_BUF + 8192 ))
75
#define INODE ((struct icommon *) ( FSYS_BUF + 16384 ))
76
#define MAPBUF ( FSYS_BUF + 24576 )
77
#define MAPBUF_LEN 8192
85
if ((((current_drive & 0x80) || (current_slice != 0))
86
&& ! IS_PC_SLICE_TYPE_BSD_WITH_FS (current_slice, FS_BSDFFS))
87
|| part_length < (SBLOCK + (SBSIZE / DEV_BSIZE))
88
|| !devread (SBLOCK, 0, SBSIZE, (char *) SUPERBLOCK)
89
|| SUPERBLOCK->fs_magic != FS_MAGIC)
99
block_map (int file_block)
101
int bnum, offset, bsize;
103
if (file_block < NDADDR)
104
return (INODE->i_db[file_block]);
106
/* If the blockmap loaded does not include FILE_BLOCK,
107
load a new blockmap. */
108
if ((bnum = fsbtodb (SUPERBLOCK, INODE->i_ib[0])) != mapblock
109
|| (mapblock_offset <= bnum && bnum <= mapblock_offset + mapblock_bsize))
111
if (MAPBUF_LEN < SUPERBLOCK->fs_bsize)
113
offset = ((file_block - NDADDR) % NINDIR (SUPERBLOCK));
116
if (offset + MAPBUF_LEN > SUPERBLOCK->fs_bsize)
117
offset = (SUPERBLOCK->fs_bsize - MAPBUF_LEN) / sizeof (int);
121
bsize = SUPERBLOCK->fs_bsize;
125
if (! devread (bnum, offset * sizeof (int), bsize, (char *) MAPBUF))
129
mapblock_offset = -1;
130
errnum = ERR_FSYS_CORRUPT;
135
mapblock_bsize = bsize;
136
mapblock_offset = offset;
139
return (((int *) MAPBUF)[((file_block - NDADDR) % NINDIR (SUPERBLOCK))
145
ffs_read (char *buf, int len)
147
int logno, off, size, map, ret = 0;
149
while (len && !errnum)
151
off = blkoff (SUPERBLOCK, filepos);
152
logno = lblkno (SUPERBLOCK, filepos);
153
size = blksize (SUPERBLOCK, INODE, logno);
155
if ((map = block_map (logno)) < 0)
163
disk_read_func = disk_read_hook;
165
devread (fsbtodb (SUPERBLOCK, map), off, size, buf);
167
disk_read_func = NULL;
183
ffs_dir (char *dirname)
186
int block, off, loc, map, ino = ROOTINO;
189
/* main loop to find destination inode */
192
/* load current inode (defaults to the root inode) */
194
if (!devread (fsbtodb (SUPERBLOCK, itod (SUPERBLOCK, ino)),
195
ino % (SUPERBLOCK->fs_inopb) * sizeof (struct dinode),
196
sizeof (struct dinode), (char *) INODE))
197
return 0; /* XXX what return value? */
199
/* if we have a real file (and we're not just printing possibilities),
200
then this is where we want to exit */
202
if (!*dirname || isspace (*dirname))
204
if ((INODE->i_mode & IFMT) != IFREG)
206
errnum = ERR_BAD_FILETYPE;
210
filemax = INODE->i_size;
212
/* incomplete implementation requires this! */
213
fsmax = (NDADDR + NINDIR (SUPERBLOCK)) * SUPERBLOCK->fs_bsize;
217
/* continue with file/directory name interpretation */
219
while (*dirname == '/')
222
if (!(INODE->i_size) || ((INODE->i_mode & IFMT) != IFDIR))
224
errnum = ERR_BAD_FILETYPE;
228
for (rest = dirname; (ch = *rest) && !isspace (ch) && ch != '/'; rest++);
233
/* loop for reading a the entries in a directory */
237
if (loc >= INODE->i_size)
243
if (print_possibilities < 0)
246
errnum = ERR_FILE_NOT_FOUND;
251
if (!(off = blkoff (SUPERBLOCK, loc)))
253
block = lblkno (SUPERBLOCK, loc);
255
if ((map = block_map (block)) < 0
256
|| !devread (fsbtodb (SUPERBLOCK, map), 0,
257
blksize (SUPERBLOCK, INODE, block),
260
errnum = ERR_FSYS_CORRUPT;
266
dp = (struct direct *) (FSYS_BUF + off);
270
if (dp->d_ino && print_possibilities && ch != '/'
271
&& (!*dirname || substring (dirname, dp->d_name) <= 0))
273
if (print_possibilities > 0)
274
print_possibilities = -print_possibilities;
276
print_a_completion (dp->d_name);
278
#endif /* STAGE1_5 */
280
while (!dp->d_ino || (substring (dirname, dp->d_name) != 0
281
|| (print_possibilities && ch != '/')));
283
/* only get here if we have a matching directory entry */
286
*(dirname = rest) = ch;
288
/* go back to main loop at top of function */
293
ffs_embed (int *start_sector, int needed_sectors)
295
/* XXX: I don't know if this is really correct. Someone who is
296
familiar with BSD should check for this. */
297
if (needed_sectors > 14)
302
/* FIXME: Disable the embedding in FFS until someone checks if
303
the code above is correct. */
310
#endif /* FSYS_FFS */