1N/A/*
1N/A * GRUB -- GRand Unified Bootloader
1N/A * Copyright (C) 2000, 2001 Free Software Foundation, Inc.
1N/A * Copyright (c) 2004 Valery Hromov
1N/A *
1N/A * This program is free software; you can redistribute it and/or modify
1N/A * it under the terms of the GNU General Public License as published by
1N/A * the Free Software Foundation; either version 2 of the License, or
1N/A * (at your option) any later version.
1N/A *
1N/A * This program is distributed in the hope that it will be useful,
1N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A * GNU General Public License for more details.
1N/A *
1N/A * You should have received a copy of the GNU General Public License
1N/A * along with this program; if not, write to the Free Software
1N/A * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1N/A */
1N/A
1N/A/*
1N/A * Elements of this file were originally from the FreeBSD "biosboot"
1N/A * bootloader file "disk.c" dated 4/12/95.
1N/A *
1N/A * The license and header comments from that file are included here.
1N/A */
1N/A
1N/A/*
1N/A * Mach Operating System
1N/A * Copyright (c) 1992, 1991 Carnegie Mellon University
1N/A * All Rights Reserved.
1N/A *
1N/A * Permission to use, copy, modify and distribute this software and its
1N/A * documentation is hereby granted, provided that both the copyright
1N/A * notice and this permission notice appear in all copies of the
1N/A * software, derivative works or modified versions, and any portions
1N/A * thereof, and that both notices appear in supporting documentation.
1N/A *
1N/A * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
1N/A * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
1N/A * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
1N/A *
1N/A * Carnegie Mellon requests users of this software to return to
1N/A *
1N/A * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
1N/A * School of Computer Science
1N/A * Carnegie Mellon University
1N/A * Pittsburgh PA 15213-3890
1N/A *
1N/A * any improvements or extensions that they make and grant Carnegie Mellon
1N/A * the rights to redistribute these changes.
1N/A *
1N/A * from: Mach, Revision 2.2 92/04/04 11:35:49 rpd
1N/A * $Id: fsys_ufs2.c,v 1.2 2004/06/19 12:17:52 okuji Exp $
1N/A */
1N/A
1N/A#ifdef FSYS_UFS2
1N/A
1N/A#include "shared.h"
1N/A#include "filesys.h"
1N/A
1N/A#include "ufs2.h"
1N/A
1N/A/* used for filesystem map blocks */
1N/Astatic int mapblock;
1N/Astatic int mapblock_offset;
1N/Astatic int mapblock_bsize;
1N/A
1N/Astatic int sblock_try[] = SBLOCKSEARCH;
1N/Astatic ufs2_daddr_t sblockloc;
1N/Astatic int type;
1N/A
1N/A/* pointer to superblock */
1N/A#define SUPERBLOCK ((struct fs *) ( FSYS_BUF + 8192 ))
1N/A
1N/A#define INODE_UFS2 ((struct ufs2_dinode *) ( FSYS_BUF + 16384 ))
1N/A
1N/A#define MAPBUF ( FSYS_BUF + 24576 )
1N/A#define MAPBUF_LEN 8192
1N/A
1N/Aint
1N/Aufs2_mount (void)
1N/A{
1N/A int retval = 0;
1N/A int i;
1N/A
1N/A sblockloc = -1;
1N/A type = 0;
1N/A
1N/A if (! (((current_drive & 0x80) || (current_slice != 0))
1N/A && ! IS_PC_SLICE_TYPE_BSD_WITH_FS (current_slice, FS_BSDFFS)))
1N/A {
1N/A for (i = 0; sblock_try[i] != -1; ++i)
1N/A {
1N/A if (! (part_length < (sblock_try[i] + (SBLOCKSIZE / DEV_BSIZE))
1N/A || ! devread (0, sblock_try[i], SBLOCKSIZE, (char *) SUPERBLOCK)))
1N/A {
1N/A if (SUPERBLOCK->fs_magic == FS_UFS2_MAGIC /* &&
1N/A (SUPERBLOCK->fs_sblockloc == sblockloc ||
1N/A (SUPERBLOCK->fs_old_flags & FS_FLAGS_UPDATED) == 0)*/)
1N/A {
1N/A type = 2;
1N/A }
1N/A else
1N/A {
1N/A continue;
1N/A }
1N/A
1N/A retval = 1;
1N/A sblockloc = sblock_try[i];
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A
1N/A mapblock = -1;
1N/A mapblock_offset = -1;
1N/A
1N/A return retval;
1N/A}
1N/A
1N/Astatic grub_int64_t
1N/Ablock_map (int file_block)
1N/A{
1N/A int bnum, offset, bsize;
1N/A
1N/A if (file_block < NDADDR)
1N/A return (INODE_UFS2->di_db[file_block]);
1N/A
1N/A /* If the blockmap loaded does not include FILE_BLOCK,
1N/A load a new blockmap. */
1N/A
1N/A if ((bnum = fsbtodb (SUPERBLOCK, INODE_UFS2->di_ib[0])) != mapblock
1N/A || (mapblock_offset <= bnum && bnum <= mapblock_offset + mapblock_bsize))
1N/A {
1N/A if (MAPBUF_LEN < SUPERBLOCK->fs_bsize)
1N/A {
1N/A offset = ((file_block - NDADDR) % NINDIR (SUPERBLOCK));
1N/A bsize = MAPBUF_LEN;
1N/A
1N/A if (offset + MAPBUF_LEN > SUPERBLOCK->fs_bsize)
1N/A offset = (SUPERBLOCK->fs_bsize - MAPBUF_LEN) / sizeof (int);
1N/A }
1N/A else
1N/A {
1N/A bsize = SUPERBLOCK->fs_bsize;
1N/A offset = 0;
1N/A }
1N/A
1N/A if (! devread (bnum, offset * sizeof (int), bsize, (char *) MAPBUF))
1N/A {
1N/A mapblock = -1;
1N/A mapblock_bsize = -1;
1N/A mapblock_offset = -1;
1N/A errnum = ERR_FSYS_CORRUPT;
1N/A return -1;
1N/A }
1N/A
1N/A mapblock = bnum;
1N/A mapblock_bsize = bsize;
1N/A mapblock_offset = offset;
1N/A }
1N/A
1N/A return (((grub_int64_t *) MAPBUF)[((file_block - NDADDR) % NINDIR (SUPERBLOCK))
1N/A - mapblock_offset]);
1N/A}
1N/A
1N/Aint
1N/Aufs2_read (char *buf, int len)
1N/A{
1N/A int logno, off, size, ret = 0;
1N/A grub_int64_t map;
1N/A
1N/A while (len && !errnum)
1N/A {
1N/A off = blkoff (SUPERBLOCK, filepos);
1N/A logno = lblkno (SUPERBLOCK, filepos);
1N/A size = blksize (SUPERBLOCK, INODE_UFS2, logno);
1N/A
1N/A if ((map = block_map (logno)) < 0)
1N/A break;
1N/A
1N/A size -= off;
1N/A
1N/A if (size > len)
1N/A size = len;
1N/A
1N/A disk_read_func = disk_read_hook;
1N/A
1N/A devread (fsbtodb (SUPERBLOCK, map), off, size, buf);
1N/A
1N/A disk_read_func = NULL;
1N/A
1N/A buf += size;
1N/A len -= size;
1N/A filepos += size;
1N/A ret += size;
1N/A }
1N/A
1N/A if (errnum)
1N/A ret = 0;
1N/A
1N/A return ret;
1N/A}
1N/A
1N/Aint
1N/Aufs2_dir (char *dirname)
1N/A{
1N/A char *rest, ch;
1N/A int block, off, loc, ino = ROOTINO;
1N/A grub_int64_t map;
1N/A struct direct *dp;
1N/A
1N/A/* main loop to find destination inode */
1N/Aloop:
1N/A
1N/A /* load current inode (defaults to the root inode) */
1N/A
1N/A if (!devread (fsbtodb (SUPERBLOCK, ino_to_fsba (SUPERBLOCK, ino)),
1N/A ino % (SUPERBLOCK->fs_inopb) * sizeof (struct ufs2_dinode),
1N/A sizeof (struct ufs2_dinode), (char *) INODE_UFS2))
1N/A return 0; /* XXX what return value? */
1N/A
1N/A /* if we have a real file (and we're not just printing possibilities),
1N/A then this is where we want to exit */
1N/A
1N/A if (!*dirname || isspace (*dirname))
1N/A {
1N/A if ((INODE_UFS2->di_mode & IFMT) != IFREG)
1N/A {
1N/A errnum = ERR_BAD_FILETYPE;
1N/A return 0;
1N/A }
1N/A
1N/A filemax = INODE_UFS2->di_size;
1N/A
1N/A /* incomplete implementation requires this! */
1N/A fsmax = (NDADDR + NINDIR (SUPERBLOCK)) * SUPERBLOCK->fs_bsize;
1N/A return 1;
1N/A }
1N/A
1N/A /* continue with file/directory name interpretation */
1N/A
1N/A while (*dirname == '/')
1N/A dirname++;
1N/A
1N/A if (!(INODE_UFS2->di_size) || ((INODE_UFS2->di_mode & IFMT) != IFDIR))
1N/A {
1N/A errnum = ERR_BAD_FILETYPE;
1N/A return 0;
1N/A }
1N/A
1N/A for (rest = dirname; (ch = *rest) && !isspace (ch) && ch != '/'; rest++);
1N/A
1N/A *rest = 0;
1N/A loc = 0;
1N/A
1N/A /* loop for reading a the entries in a directory */
1N/A
1N/A do
1N/A {
1N/A if (loc >= INODE_UFS2->di_size)
1N/A {
1N/A if (print_possibilities < 0)
1N/A return 1;
1N/A
1N/A errnum = ERR_FILE_NOT_FOUND;
1N/A *rest = ch;
1N/A return 0;
1N/A }
1N/A
1N/A if (!(off = blkoff (SUPERBLOCK, loc)))
1N/A {
1N/A block = lblkno (SUPERBLOCK, loc);
1N/A
1N/A if ((map = block_map (block)) < 0
1N/A || !devread (fsbtodb (SUPERBLOCK, map), 0,
1N/A blksize (SUPERBLOCK, INODE_UFS2, block),
1N/A (char *) FSYS_BUF))
1N/A {
1N/A errnum = ERR_FSYS_CORRUPT;
1N/A *rest = ch;
1N/A return 0;
1N/A }
1N/A }
1N/A
1N/A dp = (struct direct *) (FSYS_BUF + off);
1N/A loc += dp->d_reclen;
1N/A
1N/A#ifndef STAGE1_5
1N/A if (dp->d_ino && print_possibilities && ch != '/'
1N/A && (!*dirname || substring (dirname, dp->d_name) <= 0))
1N/A {
1N/A if (print_possibilities > 0)
1N/A print_possibilities = -print_possibilities;
1N/A
1N/A print_a_completion (dp->d_name);
1N/A }
1N/A#endif /* STAGE1_5 */
1N/A }
1N/A while (!dp->d_ino || (substring (dirname, dp->d_name) != 0
1N/A || (print_possibilities && ch != '/')));
1N/A
1N/A /* only get here if we have a matching directory entry */
1N/A
1N/A ino = dp->d_ino;
1N/A *(dirname = rest) = ch;
1N/A
1N/A /* go back to main loop at top of function */
1N/A goto loop;
1N/A}
1N/A
1N/Aint
1N/Aufs2_embed (int *start_sector, int needed_sectors)
1N/A{
1N/A /* XXX: I don't know if this is really correct. Someone who is
1N/A familiar with BSD should check for this. */
1N/A if (needed_sectors > 14)
1N/A return 0;
1N/A
1N/A *start_sector = 1;
1N/A#if 1
1N/A /* FIXME: Disable the embedding in FFS until someone checks if
1N/A the code above is correct. */
1N/A return 0;
1N/A#else
1N/A return 1;
1N/A#endif
1N/A}
1N/A
1N/A#endif /* FSYS_UFS2 */