2N/A/* sfs.c - Amiga Smart FileSystem. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2005,2006,2007,2008,2009 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/err.h>
2N/A#include <grub/file.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/disk.h>
2N/A#include <grub/dl.h>
2N/A#include <grub/types.h>
2N/A#include <grub/fshelp.h>
2N/A#include <grub/charset.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/A/* The common header for a block. */
2N/Astruct grub_sfs_bheader
2N/A{
2N/A grub_uint8_t magic[4];
2N/A grub_uint32_t chksum;
2N/A grub_uint32_t ipointtomyself;
2N/A} __attribute__ ((packed));
2N/A
2N/A/* The sfs rootblock. */
2N/Astruct grub_sfs_rblock
2N/A{
2N/A struct grub_sfs_bheader header;
2N/A grub_uint32_t version;
2N/A grub_uint8_t unused1[36];
2N/A grub_uint32_t blocksize;
2N/A grub_uint8_t unused2[40];
2N/A grub_uint8_t unused3[8];
2N/A grub_uint32_t rootobject;
2N/A grub_uint32_t btree;
2N/A} __attribute__ ((packed));
2N/A
2N/A/* A SFS object container. */
2N/Astruct grub_sfs_obj
2N/A{
2N/A grub_uint8_t unused1[4];
2N/A grub_uint32_t nodeid;
2N/A grub_uint8_t unused2[4];
2N/A union
2N/A {
2N/A struct
2N/A {
2N/A grub_uint32_t first_block;
2N/A grub_uint32_t size;
2N/A } file __attribute__ ((packed));
2N/A struct
2N/A {
2N/A grub_uint32_t hashtable;
2N/A grub_uint32_t dir_objc;
2N/A } dir __attribute__ ((packed));
2N/A } file_dir;
2N/A grub_uint32_t mtime;
2N/A grub_uint8_t type;
2N/A grub_uint8_t filename[1];
2N/A grub_uint8_t comment[1];
2N/A} __attribute__ ((packed));
2N/A
2N/A#define GRUB_SFS_TYPE_DELETED 32
2N/A#define GRUB_SFS_TYPE_SYMLINK 64
2N/A#define GRUB_SFS_TYPE_DIR 128
2N/A
2N/A/* A SFS object container. */
2N/Astruct grub_sfs_objc
2N/A{
2N/A struct grub_sfs_bheader header;
2N/A grub_uint32_t parent;
2N/A grub_uint32_t next;
2N/A grub_uint32_t prev;
2N/A /* The amount of objects depends on the blocksize. */
2N/A struct grub_sfs_obj objects[1];
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_sfs_btree_node
2N/A{
2N/A grub_uint32_t key;
2N/A grub_uint32_t data;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_sfs_btree_extent
2N/A{
2N/A grub_uint32_t key;
2N/A grub_uint32_t next;
2N/A grub_uint32_t prev;
2N/A grub_uint16_t size;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_sfs_btree
2N/A{
2N/A struct grub_sfs_bheader header;
2N/A grub_uint16_t nodes;
2N/A grub_uint8_t leaf;
2N/A grub_uint8_t nodesize;
2N/A /* Normally this can be kind of node, but just extents are
2N/A supported. */
2N/A struct grub_sfs_btree_node node[1];
2N/A} __attribute__ ((packed));
2N/A
2N/A
2N/A
2N/Astruct grub_fshelp_node
2N/A{
2N/A struct grub_sfs_data *data;
2N/A int block;
2N/A int size;
2N/A grub_uint32_t mtime;
2N/A};
2N/A
2N/A/* Information about a "mounted" sfs filesystem. */
2N/Astruct grub_sfs_data
2N/A{
2N/A struct grub_sfs_rblock rblock;
2N/A struct grub_fshelp_node diropen;
2N/A grub_disk_t disk;
2N/A
2N/A /* Blocksize in sectors. */
2N/A unsigned int blocksize;
2N/A
2N/A /* Label of the filesystem. */
2N/A char *label;
2N/A};
2N/A
2N/Astatic grub_dl_t my_mod;
2N/A
2N/A
2N/A/* Lookup the extent starting with BLOCK in the filesystem described
2N/A by DATA. Return the extent size in SIZE and the following extent
2N/A in NEXTEXT. */
2N/Astatic grub_err_t
2N/Agrub_sfs_read_extent (struct grub_sfs_data *data, unsigned int block,
2N/A int *size, int *nextext)
2N/A{
2N/A char *treeblock;
2N/A struct grub_sfs_btree *tree;
2N/A int i;
2N/A int next;
2N/A
2N/A treeblock = grub_malloc (data->blocksize);
2N/A if (!block)
2N/A return 0;
2N/A
2N/A next = grub_be_to_cpu32 (data->rblock.btree);
2N/A tree = (struct grub_sfs_btree *) treeblock;
2N/A
2N/A /* Handle this level in the btree. */
2N/A do
2N/A {
2N/A grub_disk_read (data->disk, next, 0, data->blocksize, treeblock);
2N/A if (grub_errno)
2N/A {
2N/A grub_free (treeblock);
2N/A return grub_errno;
2N/A }
2N/A
2N/A for (i = grub_be_to_cpu16 (tree->nodes) - 1; i >= 0; i--)
2N/A {
2N/A
2N/A#define EXTNODE(tree, index) \
2N/A ((struct grub_sfs_btree_node *) (((char *) &(tree)->node[0]) \
2N/A + (index) * (tree)->nodesize))
2N/A
2N/A /* Follow the tree down to the leaf level. */
2N/A if ((grub_be_to_cpu32 (EXTNODE(tree, i)->key) <= block)
2N/A && !tree->leaf)
2N/A {
2N/A next = grub_be_to_cpu32 (EXTNODE (tree, i)->data);
2N/A break;
2N/A }
2N/A
2N/A /* If the leaf level is reached, just find the correct extent. */
2N/A if (grub_be_to_cpu32 (EXTNODE (tree, i)->key) == block && tree->leaf)
2N/A {
2N/A struct grub_sfs_btree_extent *extent;
2N/A extent = (struct grub_sfs_btree_extent *) EXTNODE (tree, i);
2N/A
2N/A /* We found a correct leaf. */
2N/A *size = grub_be_to_cpu16 (extent->size);
2N/A *nextext = grub_be_to_cpu32 (extent->next);
2N/A
2N/A grub_free (treeblock);
2N/A return 0;
2N/A }
2N/A
2N/A#undef EXTNODE
2N/A
2N/A }
2N/A } while (!tree->leaf);
2N/A
2N/A grub_free (treeblock);
2N/A
2N/A return grub_error (GRUB_ERR_FILE_READ_ERROR, "SFS extent not found");
2N/A}
2N/A
2N/Astatic grub_disk_addr_t
2N/Agrub_sfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
2N/A{
2N/A int blk = node->block;
2N/A int size = 0;
2N/A int next = 0;
2N/A
2N/A while (blk)
2N/A {
2N/A grub_err_t err;
2N/A
2N/A /* In case of the first block we don't have to lookup the
2N/A extent, the minimum size is always 1. */
2N/A if (fileblock == 0)
2N/A return blk;
2N/A
2N/A err = grub_sfs_read_extent (node->data, blk, &size, &next);
2N/A if (err)
2N/A return 0;
2N/A
2N/A if (fileblock < (unsigned int) size)
2N/A return fileblock + blk;
2N/A
2N/A fileblock -= size;
2N/A
2N/A blk = next;
2N/A }
2N/A
2N/A grub_error (GRUB_ERR_FILE_READ_ERROR,
2N/A "reading a SFS block outside the extent");
2N/A
2N/A return 0;
2N/A}
2N/A
2N/A
2N/A/* Read LEN bytes from the file described by DATA starting with byte
2N/A POS. Return the amount of read bytes in READ. */
2N/Astatic grub_ssize_t
2N/Agrub_sfs_read_file (grub_fshelp_node_t node,
2N/A void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
2N/A unsigned offset, unsigned length),
2N/A grub_off_t pos, grub_size_t len, char *buf)
2N/A{
2N/A return grub_fshelp_read_file (node->data->disk, node, read_hook,
2N/A pos, len, buf, grub_sfs_read_block,
2N/A node->size, 0);
2N/A}
2N/A
2N/A
2N/Astatic struct grub_sfs_data *
2N/Agrub_sfs_mount (grub_disk_t disk)
2N/A{
2N/A struct grub_sfs_data *data;
2N/A struct grub_sfs_objc *rootobjc;
2N/A char *rootobjc_data = 0;
2N/A unsigned int blk;
2N/A
2N/A data = grub_malloc (sizeof (*data));
2N/A if (!data)
2N/A return 0;
2N/A
2N/A /* Read the rootblock. */
2N/A grub_disk_read (disk, 0, 0, sizeof (struct grub_sfs_rblock),
2N/A &data->rblock);
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A /* Make sure this is a sfs filesystem. */
2N/A if (grub_strncmp ((char *) (data->rblock.header.magic), "SFS", 4))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "not a SFS filesystem");
2N/A goto fail;
2N/A }
2N/A
2N/A data->blocksize = grub_be_to_cpu32 (data->rblock.blocksize);
2N/A rootobjc_data = grub_malloc (data->blocksize);
2N/A if (! rootobjc_data)
2N/A goto fail;
2N/A
2N/A /* Read the root object container. */
2N/A grub_disk_read (disk, grub_be_to_cpu32 (data->rblock.rootobject), 0,
2N/A data->blocksize, rootobjc_data);
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A rootobjc = (struct grub_sfs_objc *) rootobjc_data;
2N/A
2N/A blk = grub_be_to_cpu32 (rootobjc->objects[0].file_dir.dir.dir_objc);
2N/A data->diropen.size = 0;
2N/A data->diropen.block = blk;
2N/A data->diropen.data = data;
2N/A data->disk = disk;
2N/A data->label = grub_strdup ((char *) (rootobjc->objects[0].filename));
2N/A
2N/A grub_free (rootobjc_data);
2N/A return data;
2N/A
2N/A fail:
2N/A if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
2N/A grub_error (GRUB_ERR_BAD_FS, "not an SFS filesystem");
2N/A
2N/A grub_free (data);
2N/A grub_free (rootobjc_data);
2N/A return 0;
2N/A}
2N/A
2N/A
2N/Astatic char *
2N/Agrub_sfs_read_symlink (grub_fshelp_node_t node)
2N/A{
2N/A struct grub_sfs_data *data = node->data;
2N/A char *symlink;
2N/A char *block;
2N/A
2N/A block = grub_malloc (data->blocksize);
2N/A if (!block)
2N/A return 0;
2N/A
2N/A grub_disk_read (data->disk, node->block, 0, data->blocksize, block);
2N/A if (grub_errno)
2N/A {
2N/A grub_free (block);
2N/A return 0;
2N/A }
2N/A
2N/A /* This is just a wild guess, but it always worked for me. How the
2N/A SLNK block looks like is not documented in the SFS docs. */
2N/A symlink = grub_strdup (&block[24]);
2N/A grub_free (block);
2N/A if (!symlink)
2N/A return 0;
2N/A
2N/A return symlink;
2N/A}
2N/A
2N/Astatic int
2N/Agrub_sfs_iterate_dir (grub_fshelp_node_t dir,
2N/A int NESTED_FUNC_ATTR
2N/A (*hook) (const char *filename,
2N/A enum grub_fshelp_filetype filetype,
2N/A grub_fshelp_node_t node))
2N/A{
2N/A struct grub_fshelp_node *node = 0;
2N/A struct grub_sfs_data *data = dir->data;
2N/A char *objc_data;
2N/A struct grub_sfs_objc *objc;
2N/A unsigned int next = dir->block;
2N/A int pos;
2N/A
2N/A auto int NESTED_FUNC_ATTR grub_sfs_create_node (const char *name,
2N/A int block,
2N/A int size, int type,
2N/A grub_uint32_t mtime);
2N/A
2N/A int NESTED_FUNC_ATTR grub_sfs_create_node (const char *name,
2N/A int block,
2N/A int size, int type,
2N/A grub_uint32_t mtime)
2N/A {
2N/A grub_size_t len = grub_strlen (name);
2N/A grub_uint8_t *name_u8;
2N/A int ret;
2N/A node = grub_malloc (sizeof (*node));
2N/A if (!node)
2N/A return 1;
2N/A name_u8 = grub_malloc (len * GRUB_MAX_UTF8_PER_LATIN1 + 1);
2N/A if (!name_u8)
2N/A {
2N/A grub_free (node);
2N/A return 1;
2N/A }
2N/A
2N/A node->data = data;
2N/A node->size = size;
2N/A node->block = block;
2N/A node->mtime = mtime;
2N/A
2N/A *grub_latin1_to_utf8 (name_u8, (const grub_uint8_t *) name, len) = '\0';
2N/A
2N/A ret = hook ((char *) name_u8, type, node);
2N/A grub_free (name_u8);
2N/A return ret;
2N/A }
2N/A
2N/A objc_data = grub_malloc (data->blocksize);
2N/A if (!objc_data)
2N/A goto fail;
2N/A
2N/A /* The Object container can consist of multiple blocks, iterate over
2N/A every block. */
2N/A while (next)
2N/A {
2N/A grub_disk_read (data->disk, next, 0, data->blocksize, objc_data);
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A objc = (struct grub_sfs_objc *) objc_data;
2N/A
2N/A pos = (char *) &objc->objects[0] - (char *) objc;
2N/A
2N/A /* Iterate over all entries in this block. */
2N/A while (pos + sizeof (struct grub_sfs_obj) < data->blocksize)
2N/A {
2N/A struct grub_sfs_obj *obj;
2N/A obj = (struct grub_sfs_obj *) ((char *) objc + pos);
2N/A const char *filename = (const char *) obj->filename;
2N/A int len;
2N/A enum grub_fshelp_filetype type;
2N/A unsigned int block;
2N/A
2N/A /* The filename and comment dynamically increase the size of
2N/A the object. */
2N/A len = grub_strlen (filename);
2N/A len += grub_strlen (filename + len + 1);
2N/A
2N/A pos += sizeof (*obj) + len;
2N/A /* Round up to a multiple of two bytes. */
2N/A pos = ((pos + 1) >> 1) << 1;
2N/A
2N/A if (filename[0] == 0)
2N/A continue;
2N/A
2N/A /* First check if the file was not deleted. */
2N/A if (obj->type & GRUB_SFS_TYPE_DELETED)
2N/A continue;
2N/A else if (obj->type & GRUB_SFS_TYPE_SYMLINK)
2N/A type = GRUB_FSHELP_SYMLINK;
2N/A else if (obj->type & GRUB_SFS_TYPE_DIR)
2N/A type = GRUB_FSHELP_DIR;
2N/A else
2N/A type = GRUB_FSHELP_REG;
2N/A
2N/A if (type == GRUB_FSHELP_DIR)
2N/A block = grub_be_to_cpu32 (obj->file_dir.dir.dir_objc);
2N/A else
2N/A block = grub_be_to_cpu32 (obj->file_dir.file.first_block);
2N/A
2N/A if (grub_sfs_create_node (filename, block,
2N/A grub_be_to_cpu32 (obj->file_dir.file.size),
2N/A type, grub_be_to_cpu32 (obj->mtime)))
2N/A {
2N/A grub_free (objc_data);
2N/A return 1;
2N/A }
2N/A }
2N/A
2N/A next = grub_be_to_cpu32 (objc->next);
2N/A }
2N/A
2N/A fail:
2N/A grub_free (objc_data);
2N/A return 0;
2N/A}
2N/A
2N/A
2N/A/* Open a file named NAME and initialize FILE. */
2N/Astatic grub_err_t
2N/Agrub_sfs_open (struct grub_file *file, const char *name)
2N/A{
2N/A struct grub_sfs_data *data;
2N/A struct grub_fshelp_node *fdiro = 0;
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A data = grub_sfs_mount (file->device->disk);
2N/A if (!data)
2N/A goto fail;
2N/A
2N/A grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_sfs_iterate_dir,
2N/A grub_sfs_read_symlink, GRUB_FSHELP_REG);
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A file->size = fdiro->size;
2N/A data->diropen = *fdiro;
2N/A grub_free (fdiro);
2N/A
2N/A file->data = data;
2N/A file->offset = 0;
2N/A
2N/A return 0;
2N/A
2N/A fail:
2N/A if (data && fdiro != &data->diropen)
2N/A grub_free (fdiro);
2N/A if (data)
2N/A grub_free (data->label);
2N/A grub_free (data);
2N/A
2N/A grub_dl_unref (my_mod);
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/A
2N/Astatic grub_err_t
2N/Agrub_sfs_close (grub_file_t file)
2N/A{
2N/A struct grub_sfs_data *data = (struct grub_sfs_data *) file->data;
2N/A
2N/A grub_free (data->label);
2N/A grub_free (data);
2N/A
2N/A grub_dl_unref (my_mod);
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/A
2N/A/* Read LEN bytes data from FILE into BUF. */
2N/Astatic grub_ssize_t
2N/Agrub_sfs_read (grub_file_t file, char *buf, grub_size_t len)
2N/A{
2N/A struct grub_sfs_data *data = (struct grub_sfs_data *) file->data;
2N/A
2N/A int size = grub_sfs_read_file (&data->diropen, file->read_hook,
2N/A file->offset, len, buf);
2N/A
2N/A return size;
2N/A}
2N/A
2N/A
2N/Astatic grub_err_t
2N/Agrub_sfs_dir (grub_device_t device, const char *path,
2N/A int (*hook) (const char *filename,
2N/A const struct grub_dirhook_info *info))
2N/A{
2N/A struct grub_sfs_data *data = 0;
2N/A struct grub_fshelp_node *fdiro = 0;
2N/A
2N/A auto int NESTED_FUNC_ATTR iterate (const char *filename,
2N/A enum grub_fshelp_filetype filetype,
2N/A grub_fshelp_node_t node);
2N/A
2N/A int NESTED_FUNC_ATTR iterate (const char *filename,
2N/A enum grub_fshelp_filetype filetype,
2N/A grub_fshelp_node_t node)
2N/A {
2N/A struct grub_dirhook_info info;
2N/A grub_memset (&info, 0, sizeof (info));
2N/A info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
2N/A info.mtime = node->mtime + 8 * 365 * 86400 + 86400 * 2;
2N/A info.mtimeset = 1;
2N/A grub_free (node);
2N/A return hook (filename, &info);
2N/A }
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A data = grub_sfs_mount (device->disk);
2N/A if (!data)
2N/A goto fail;
2N/A
2N/A grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_sfs_iterate_dir,
2N/A grub_sfs_read_symlink, GRUB_FSHELP_DIR);
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A grub_sfs_iterate_dir (fdiro, iterate);
2N/A
2N/A fail:
2N/A if (data && fdiro != &data->diropen)
2N/A grub_free (fdiro);
2N/A if (data)
2N/A grub_free (data->label);
2N/A grub_free (data);
2N/A
2N/A grub_dl_unref (my_mod);
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/A
2N/Astatic grub_err_t
2N/Agrub_sfs_label (grub_device_t device, char **label)
2N/A{
2N/A struct grub_sfs_data *data;
2N/A grub_disk_t disk = device->disk;
2N/A
2N/A data = grub_sfs_mount (disk);
2N/A if (data)
2N/A {
2N/A grub_size_t len = grub_strlen (data->label);
2N/A *label = grub_malloc (len * GRUB_MAX_UTF8_PER_LATIN1 + 1);
2N/A if (*label)
2N/A *grub_latin1_to_utf8 ((grub_uint8_t *) *label,
2N/A (const grub_uint8_t *) data->label,
2N/A len) = '\0';
2N/A grub_free (data->label);
2N/A }
2N/A grub_free (data);
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/A
2N/Astatic struct grub_fs grub_sfs_fs =
2N/A {
2N/A .name = "sfs",
2N/A .dir = grub_sfs_dir,
2N/A .open = grub_sfs_open,
2N/A .read = grub_sfs_read,
2N/A .close = grub_sfs_close,
2N/A .label = grub_sfs_label,
2N/A#ifdef GRUB_UTIL
2N/A .reserved_first_sector = 0,
2N/A#endif
2N/A .next = 0
2N/A };
2N/A
2N/AGRUB_MOD_INIT(sfs)
2N/A{
2N/A grub_fs_register (&grub_sfs_fs);
2N/A my_mod = mod;
2N/A}
2N/A
2N/AGRUB_MOD_FINI(sfs)
2N/A{
2N/A grub_fs_unregister (&grub_sfs_fs);
2N/A}