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., 51 Franklin Street - Fifth Floor, Boston,
22
* Elements of this file were originally from the FreeBSD "biosboot"
23
* bootloader file "disk.c" dated 4/12/95.
25
* The license and header comments from that file are included here.
29
* Mach Operating System
30
* Copyright (c) 1992, 1991 Carnegie Mellon University
31
* All Rights Reserved.
33
* Permission to use, copy, modify and distribute this software and its
34
* documentation is hereby granted, provided that both the copyright
35
* notice and this permission notice appear in all copies of the
36
* software, derivative works or modified versions, and any portions
37
* thereof, and that both notices appear in supporting documentation.
39
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43
* Carnegie Mellon requests users of this software to return to
45
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46
* School of Computer Science
47
* Carnegie Mellon University
48
* Pittsburgh PA 15213-3890
50
* any improvements or extensions that they make and grant Carnegie Mellon
51
* the rights to redistribute these changes.
53
* from: Mach, Revision 2.2 92/04/04 11:35:49 rpd
54
* $Id: fsys_ffs.c,v 1.10 2001/11/12 06:57:29 okuji Exp $
64
#include "disk_inode.h"
65
#include "disk_inode_ffs.h"
69
/* used for filesystem map blocks */
71
static int mapblock_offset;
72
static int mapblock_bsize;
74
/* pointer to superblock */
75
#define SUPERBLOCK ((struct fs *) ( FSYS_BUF + 8192 ))
76
#define INODE ((struct icommon *) ( FSYS_BUF + 16384 ))
77
#define MAPBUF ( FSYS_BUF + 24576 )
78
#define MAPBUF_LEN 8192
86
if ((((current_drive & 0x80) || (current_slice != 0))
87
&& ! IS_PC_SLICE_TYPE_BSD_WITH_FS (current_slice, FS_BSDFFS))
88
|| part_length < (SBLOCK + (SBSIZE / DEV_BSIZE))
89
|| !devread (SBLOCK, 0, SBSIZE, (char *) SUPERBLOCK)
90
|| SUPERBLOCK->fs_magic != FS_MAGIC)
100
block_map (int file_block)
102
int bnum, offset, bsize;
104
if (file_block < NDADDR)
105
return (INODE->i_db[file_block]);
107
/* If the blockmap loaded does not include FILE_BLOCK,
108
load a new blockmap. */
109
if ((bnum = fsbtodb (SUPERBLOCK, INODE->i_ib[0])) != mapblock
110
|| (mapblock_offset <= bnum && bnum <= mapblock_offset + mapblock_bsize))
112
if (MAPBUF_LEN < SUPERBLOCK->fs_bsize)
114
offset = ((file_block - NDADDR) % NINDIR (SUPERBLOCK));
117
if (offset + MAPBUF_LEN > SUPERBLOCK->fs_bsize)
118
offset = (SUPERBLOCK->fs_bsize - MAPBUF_LEN) / sizeof (int);
122
bsize = SUPERBLOCK->fs_bsize;
126
if (! devread (bnum, offset * sizeof (int), bsize, (char *) MAPBUF))
130
mapblock_offset = -1;
131
errnum = ERR_FSYS_CORRUPT;
136
mapblock_bsize = bsize;
137
mapblock_offset = offset;
140
return (((int *) MAPBUF)[((file_block - NDADDR) % NINDIR (SUPERBLOCK))
146
ffs_read (char *buf, int len)
148
int logno, off, size, map, ret = 0;
150
while (len && !errnum)
152
off = blkoff (SUPERBLOCK, filepos);
153
logno = lblkno (SUPERBLOCK, filepos);
154
size = blksize (SUPERBLOCK, INODE, logno);
156
if ((map = block_map (logno)) < 0)
164
disk_read_func = disk_read_hook;
166
devread (fsbtodb (SUPERBLOCK, map), off, size, buf);
168
disk_read_func = NULL;
184
ffs_dir (char *dirname)
187
int block, off, loc, map, ino = ROOTINO;
190
/* main loop to find destination inode */
193
/* load current inode (defaults to the root inode) */
195
if (!devread (fsbtodb (SUPERBLOCK, itod (SUPERBLOCK, ino)),
196
ino % (SUPERBLOCK->fs_inopb) * sizeof (struct dinode),
197
sizeof (struct dinode), (char *) INODE))
198
return 0; /* XXX what return value? */
200
/* if we have a real file (and we're not just printing possibilities),
201
then this is where we want to exit */
203
if (!*dirname || isspace (*dirname))
205
if ((INODE->i_mode & IFMT) != IFREG)
207
errnum = ERR_BAD_FILETYPE;
211
filemax = INODE->i_size;
213
/* incomplete implementation requires this! */
214
fsmax = (NDADDR + NINDIR (SUPERBLOCK)) * SUPERBLOCK->fs_bsize;
218
/* continue with file/directory name interpretation */
220
while (*dirname == '/')
223
if (!(INODE->i_size) || ((INODE->i_mode & IFMT) != IFDIR))
225
errnum = ERR_BAD_FILETYPE;
229
for (rest = dirname; (ch = *rest) && !isspace (ch) && ch != '/'; rest++);
234
/* loop for reading a the entries in a directory */
238
if (loc >= INODE->i_size)
244
if (print_possibilities < 0)
247
errnum = ERR_FILE_NOT_FOUND;
252
if (!(off = blkoff (SUPERBLOCK, loc)))
254
block = lblkno (SUPERBLOCK, loc);
256
if ((map = block_map (block)) < 0
257
|| !devread (fsbtodb (SUPERBLOCK, map), 0,
258
blksize (SUPERBLOCK, INODE, block),
261
errnum = ERR_FSYS_CORRUPT;
267
dp = (struct direct *) (FSYS_BUF + off);
271
if (dp->d_ino && print_possibilities && ch != '/'
272
&& (!*dirname || substring (dirname, dp->d_name) <= 0))
274
if (print_possibilities > 0)
275
print_possibilities = -print_possibilities;
277
print_a_completion (dp->d_name);
279
#endif /* STAGE1_5 */
281
while (!dp->d_ino || (substring (dirname, dp->d_name) != 0
282
|| (print_possibilities && ch != '/')));
284
/* only get here if we have a matching directory entry */
287
*(dirname = rest) = ch;
289
/* go back to main loop at top of function */
294
ffs_embed (int *start_sector, int needed_sectors)
296
/* XXX: I don't know if this is really correct. Someone who is
297
familiar with BSD should check for this. */
298
if (needed_sectors > 14)
303
/* FIXME: Disable the embedding in FFS until someone checks if
304
the code above is correct. */
311
#endif /* FSYS_FFS */