1N/A/*
1N/A * GRUB -- GRand Unified Bootloader
1N/A * Copyright (C) 2006 Free Software Foundation, Inc.
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 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
1N/A * Use is subject to license terms.
1N/A */
1N/A
1N/A/* From Solaris usr/src/stand/lib/fs/ufs/ufsops.c */
1N/A
1N/A#ifdef FSYS_UFS
1N/A
1N/A#include "shared.h"
1N/A#include "filesys.h"
1N/A
1N/A#include "ufs.h"
1N/A
1N/A/* These are the pools of buffers, etc. */
1N/A
1N/A#define SUPERBLOCK ((struct fs *)(FSYS_BUF + 0x2000))
1N/A#define INODE ((struct icommon *)(FSYS_BUF + 0x1000))
1N/A#define DIRENT (FSYS_BUF + 0x4000)
1N/A#define INDIRBLK1 ((grub_daddr32_t *)(FSYS_BUF + 0x4000)) /* 2+ indir blk */
1N/A#define INDIRBLK0 ((grub_daddr32_t *)(FSYS_BUF+ 0x6000)) /* 1st indirect blk */
1N/A
1N/Astatic int indirblk0, indirblk1;
1N/A
1N/Astatic int openi(grub_ino_t);
1N/Astatic grub_ino_t dlook(grub_ino_t, char *);
1N/Astatic grub_daddr32_t sbmap(grub_daddr32_t);
1N/A
1N/A/* read superblock and check fs magic */
1N/Aint
1N/Aufs_mount(void)
1N/A{
1N/A if (! IS_PC_SLICE_TYPE_SOLARIS(current_slice) ||
1N/A !devread(UFS_SBLOCK, 0, UFS_SBSIZE, (char *)SUPERBLOCK) ||
1N/A SUPERBLOCK->fs_magic != UFS_MAGIC)
1N/A return 0;
1N/A
1N/A return 1;
1N/A}
1N/A
1N/A
1N/A/*
1N/A * searching for a file, if successful, inode will be loaded in INODE
1N/A * The entry point should really be named ufs_open(char *pathname).
1N/A * For now, keep it consistent with the rest of fsys modules.
1N/A */
1N/Aint
1N/Aufs_dir(char *dirname)
1N/A{
1N/A grub_ino_t inode = ROOTINO; /* start from root */
1N/A char *fname, ch;
1N/A
1N/A indirblk0 = indirblk1 = 0;
1N/A
1N/A /* skip leading slashes */
1N/A while (*dirname == '/')
1N/A dirname++;
1N/A
1N/A while (inode && *dirname && !isspace(*dirname)) {
1N/A if (!openi(inode))
1N/A return 0;
1N/A
1N/A /* parse for next path component */
1N/A fname = dirname;
1N/A while (*dirname && !isspace(*dirname) && *dirname != '/')
1N/A dirname++;
1N/A ch = *dirname;
1N/A *dirname = 0; /* ensure null termination */
1N/A
1N/A inode = dlook(inode, fname);
1N/A *dirname = ch;
1N/A while (*dirname == '/')
1N/A dirname++;
1N/A }
1N/A
1N/A /* return 1 only if inode exists and is a regular file */
1N/A if (! openi(inode))
1N/A return (0);
1N/A filepos = 0;
1N/A filemax = INODE->ic_sizelo;
1N/A return (inode && ((INODE->ic_smode & IFMT) == IFREG));
1N/A}
1N/A
1N/A/*
1N/A * This is the high-level read function.
1N/A */
1N/Aint
1N/Aufs_read(char *buf, int len)
1N/A{
1N/A int off, size, ret = 0, ok;
1N/A grub_daddr32_t lblk, dblk;
1N/A
1N/A while (len) {
1N/A off = blkoff(SUPERBLOCK, filepos);
1N/A lblk = lblkno(SUPERBLOCK, filepos);
1N/A size = SUPERBLOCK->fs_bsize;
1N/A size -= off;
1N/A if (size > len)
1N/A size = len;
1N/A
1N/A if ((dblk = sbmap(lblk)) <= 0) {
1N/A /* we are in a file hole, just zero the buf */
1N/A grub_memset(buf, 0, size);
1N/A } else {
1N/A disk_read_func = disk_read_hook;
1N/A ok = devread(fsbtodb(SUPERBLOCK, dblk), off, size, buf);
1N/A disk_read_func = 0;
1N/A if (!ok)
1N/A return 0;
1N/A }
1N/A buf += size;
1N/A len -= size;
1N/A filepos += size;
1N/A ret += size;
1N/A }
1N/A
1N/A return (ret);
1N/A}
1N/A
1N/Aint
1N/Aufs_embed (int *start_sector, int needed_sectors)
1N/A{
1N/A if (needed_sectors > 14)
1N/A return 0;
1N/A
1N/A *start_sector = 2;
1N/A return 1;
1N/A}
1N/A
1N/A/* read inode and place content in INODE */
1N/Astatic int
1N/Aopeni(grub_ino_t inode)
1N/A{
1N/A grub_daddr32_t dblk;
1N/A int off;
1N/A
1N/A /* get block and byte offset into the block */
1N/A dblk = fsbtodb(SUPERBLOCK, itod(SUPERBLOCK, inode));
1N/A off = itoo(SUPERBLOCK, inode) * sizeof (struct icommon);
1N/A
1N/A return (devread(dblk, off, sizeof (struct icommon), (char *)INODE));
1N/A}
1N/A
1N/A/*
1N/A * Performs fileblock mapping. Convert file block no. to disk block no.
1N/A * Returns 0 when block doesn't exist and <0 when block isn't initialized
1N/A * (i.e belongs to a hole in the file).
1N/A */
1N/Agrub_daddr32_t
1N/Asbmap(grub_daddr32_t bn)
1N/A{
1N/A int level, bound, i, index;
1N/A grub_daddr32_t nb, blkno;
1N/A grub_daddr32_t *db = INODE->ic_db;
1N/A
1N/A /* blocks 0..UFS_NDADDR are direct blocks */
1N/A if (bn < UFS_NDADDR) {
1N/A return db[bn];
1N/A }
1N/A
1N/A /* determine how many levels of indirection. */
1N/A level = 0;
1N/A bn -= UFS_NDADDR;
1N/A bound = UFS_NINDIR(SUPERBLOCK);
1N/A while (bn >= bound) {
1N/A level++;
1N/A bn -= bound;
1N/A bound *= UFS_NINDIR(SUPERBLOCK);
1N/A }
1N/A if (level >= UFS_NIADDR) /* bn too big */
1N/A return ((grub_daddr32_t)0);
1N/A
1N/A /* fetch the first indirect block */
1N/A nb = INODE->ic_ib[level];
1N/A if (nb == 0) {
1N/A return ((grub_daddr32_t)0);
1N/A }
1N/A if (indirblk0 != nb) {
1N/A indirblk0 = 0;
1N/A blkno = fsbtodb(SUPERBLOCK, nb);
1N/A if (!devread(blkno, 0, SUPERBLOCK->fs_bsize,
1N/A (char *)INDIRBLK0))
1N/A return (0);
1N/A indirblk0 = nb;
1N/A }
1N/A bound /= UFS_NINDIR(SUPERBLOCK);
1N/A index = (bn / bound) % UFS_NINDIR(SUPERBLOCK);
1N/A nb = INDIRBLK0[index];
1N/A
1N/A /* fetch through the indirect blocks */
1N/A for (i = 1; i <= level; i++) {
1N/A if (indirblk1 != nb) {
1N/A blkno = fsbtodb(SUPERBLOCK, nb);
1N/A if (!devread(blkno, 0, SUPERBLOCK->fs_bsize,
1N/A (char *)INDIRBLK1))
1N/A return (0);
1N/A indirblk1 = nb;
1N/A }
1N/A bound /= UFS_NINDIR(SUPERBLOCK);
1N/A index = (bn / bound) % UFS_NINDIR(SUPERBLOCK);
1N/A nb = INDIRBLK1[index];
1N/A if (nb == 0)
1N/A return ((grub_daddr32_t)0);
1N/A }
1N/A
1N/A return (nb);
1N/A}
1N/A
1N/A/* search directory content for name, return inode number */
1N/Astatic grub_ino_t
1N/Adlook(grub_ino_t dir_ino, char *name)
1N/A{
1N/A int loc, off;
1N/A grub_daddr32_t lbn, dbn, dblk;
1N/A struct direct *dp;
1N/A
1N/A if ((INODE->ic_smode & IFMT) != IFDIR)
1N/A return 0;
1N/A
1N/A loc = 0;
1N/A while (loc < INODE->ic_sizelo) {
1N/A /* offset into block */
1N/A off = blkoff(SUPERBLOCK, loc);
1N/A if (off == 0) { /* need to read in a new block */
1N/A
1N/A /* get logical block number */
1N/A lbn = lblkno(SUPERBLOCK, loc);
1N/A /* resolve indrect blocks */
1N/A dbn = sbmap(lbn);
1N/A if (dbn == 0)
1N/A return (0);
1N/A
1N/A dblk = fsbtodb(SUPERBLOCK, dbn);
1N/A if (!devread(dblk, 0, SUPERBLOCK->fs_bsize,
1N/A (char *)DIRENT)) {
1N/A return 0;
1N/A }
1N/A }
1N/A
1N/A dp = (struct direct *)(DIRENT + off);
1N/A if (dp->d_ino && substring(name, dp->d_name) == 0)
1N/A return (dp->d_ino);
1N/A loc += dp->d_reclen;
1N/A }
1N/A return (0);
1N/A}
1N/A
1N/A#endif /* FSYS_UFS */