2
* GRUB -- GRand Unified Bootloader
3
* Copyright (C) 2000, 2001 Free Software Foundation, Inc.
4
* Copyright (c) 2004 Valery Hromov
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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_ufs2.c,v 1.2 2004/06/19 12:17:52 okuji Exp $
64
/* used for filesystem map blocks */
66
static int mapblock_offset;
67
static int mapblock_bsize;
69
static int sblock_try[] = SBLOCKSEARCH;
70
static ufs2_daddr_t sblockloc;
73
/* pointer to superblock */
74
#define SUPERBLOCK ((struct fs *) ( FSYS_BUF + 8192 ))
76
#define INODE_UFS2 ((struct ufs2_dinode *) ( FSYS_BUF + 16384 ))
78
#define MAPBUF ( FSYS_BUF + 24576 )
79
#define MAPBUF_LEN 8192
80
#define NAME_BUF (FSYS_BUF - 512)
91
// if (! (((current_drive & 0x80) || (current_slice != 0))
92
// && ! IS_PC_SLICE_TYPE_BSD_WITH_FS (current_slice, FS_BSDFFS)))
94
for (i = 0; sblock_try[i] != -1; ++i)
96
if (! (part_length < (sblock_try[i] + (SBLOCKSIZE / DEV_BSIZE))
97
|| ! devread (0, sblock_try[i], SBLOCKSIZE, (char *) SUPERBLOCK)))
99
if (SUPERBLOCK->fs_magic == FS_UFS2_MAGIC /* &&
100
(SUPERBLOCK->fs_sblockloc == sblockloc ||
101
(SUPERBLOCK->fs_old_flags & FS_FLAGS_UPDATED) == 0)*/)
111
sblockloc = sblock_try[i];
118
mapblock_offset = -1;
124
block_map (int file_block)
126
int bnum, offset, bsize;
128
if (file_block < NDADDR)
129
return (INODE_UFS2->di_db[file_block]);
131
/* If the blockmap loaded does not include FILE_BLOCK,
132
load a new blockmap. */
134
if ((bnum = fsbtodb (SUPERBLOCK, INODE_UFS2->di_ib[0])) != mapblock
135
|| (mapblock_offset <= bnum && bnum <= mapblock_offset + mapblock_bsize))
137
if (MAPBUF_LEN < SUPERBLOCK->fs_bsize)
139
offset = ((file_block - NDADDR) % NINDIR (SUPERBLOCK));
142
if (offset + MAPBUF_LEN > SUPERBLOCK->fs_bsize)
143
offset = (SUPERBLOCK->fs_bsize - MAPBUF_LEN) / sizeof (int);
147
bsize = SUPERBLOCK->fs_bsize;
151
if (! devread (bnum, offset * sizeof (int), bsize, (char *) MAPBUF))
155
mapblock_offset = -1;
156
errnum = ERR_FSYS_CORRUPT;
161
mapblock_bsize = bsize;
162
mapblock_offset = offset;
165
return (((grub_int64_t *) MAPBUF)[((file_block - NDADDR) % NINDIR (SUPERBLOCK))
170
ufs2_read (char *buf, unsigned long len)
172
unsigned long logno, off, size, ret = 0;
175
while (len && !errnum)
177
off = blkoff (SUPERBLOCK, filepos);
178
logno = lblkno (SUPERBLOCK, filepos);
179
size = blksize (SUPERBLOCK, INODE_UFS2, logno);
181
if ((map = block_map (logno)) < 0)
189
disk_read_func = disk_read_hook;
191
devread (fsbtodb (SUPERBLOCK, map), off, size, buf);
193
disk_read_func = NULL;
196
len -= size; /* len always >= 0 */
208
ufs2_dir (char *dirname)
211
unsigned long block, off, loc, ino = ROOTINO;
219
char *tmp_name = (char *)(NAME_BUF); /* MAXNAMLEN is 255, so 512 byte buffer is needed. */
222
/* main loop to find destination inode */
225
/* load current inode (defaults to the root inode) */
227
if (!devread (fsbtodb (SUPERBLOCK, ino_to_fsba (SUPERBLOCK, ino)),
228
ino % (SUPERBLOCK->fs_inopb) * sizeof (struct ufs2_dinode),
229
sizeof (struct ufs2_dinode), (char *) INODE_UFS2))
230
return 0; /* XXX what return value? */
232
/* if we have a real file (and we're not just printing possibilities),
233
then this is where we want to exit */
235
if (!*dirname || isspace (*dirname))
237
if ((INODE_UFS2->di_mode & IFMT) != IFREG)
239
errnum = ERR_BAD_FILETYPE;
243
filemax = INODE_UFS2->di_size;
245
/* incomplete implementation requires this! */
246
fsmax = (NDADDR + NINDIR (SUPERBLOCK)) * SUPERBLOCK->fs_bsize;
250
/* continue with file/directory name interpretation */
252
while (*dirname == '/')
255
if (!(INODE_UFS2->di_size) || ((INODE_UFS2->di_mode & IFMT) != IFDIR))
257
errnum = ERR_BAD_FILETYPE;
261
//for (rest = dirname; (ch = *rest) && !isspace (ch) && ch != '/'; rest++);
262
for (rest = dirname; (ch = *rest) && !isspace (ch) && ch != '/'; rest++)
275
/* loop for reading a the entries in a directory */
279
if (loc >= INODE_UFS2->di_size)
281
if (print_possibilities < 0)
284
errnum = ERR_FILE_NOT_FOUND;
289
if (!(off = blkoff (SUPERBLOCK, loc)))
291
block = lblkno (SUPERBLOCK, loc);
293
if ((map = block_map (block)) < 0
294
|| !devread (fsbtodb (SUPERBLOCK, map), 0,
295
blksize (SUPERBLOCK, INODE_UFS2, block),
298
errnum = ERR_FSYS_CORRUPT;
304
dp = (struct direct *) (FSYS_BUF + off);
307
/* copy dp->name to tmp_name, and quote the spaces with a '\\' */
308
for (j = 0, k = 0; j < dp->d_namlen; j++)
310
if (! (ch1 = dp->d_name[j]))
313
tmp_name[k++] = '\\';
319
if (dp->d_ino && print_possibilities && ch != '/'
320
&& (!*dirname || substring (dirname, tmp_name, 0) <= 0))
322
if (print_possibilities > 0)
323
print_possibilities = -print_possibilities;
325
print_a_completion (tmp_name);
327
#endif /* STAGE1_5 */
329
while (!dp->d_ino || (substring (dirname, dp->d_name, 0) != 0
330
|| (print_possibilities && ch != '/')));
332
/* only get here if we have a matching directory entry */
335
*(dirname = rest) = ch;
337
/* go back to main loop at top of function */
342
ufs2_embed (unsigned long *start_sector, unsigned long needed_sectors)
344
/* XXX: I don't know if this is really correct. Someone who is
345
familiar with BSD should check for this. */
346
if (needed_sectors > 14)
351
/* FIXME: Disable the embedding in FFS until someone checks if
352
the code above is correct. */
359
#endif /* FSYS_UFS2 */