2N/A/* affs.c - Amiga Fast 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 affs bootblock. */
2N/Astruct grub_affs_bblock
2N/A{
2N/A grub_uint8_t type[3];
2N/A grub_uint8_t flags;
2N/A grub_uint32_t checksum;
2N/A grub_uint32_t rootblock;
2N/A} __attribute__ ((packed));
2N/A
2N/A/* Set if the filesystem is a AFFS filesystem. Otherwise this is an
2N/A OFS filesystem. */
2N/A#define GRUB_AFFS_FLAG_FFS 1
2N/A
2N/A/* The affs rootblock. */
2N/Astruct grub_affs_rblock
2N/A{
2N/A grub_uint8_t type[4];
2N/A grub_uint8_t unused1[8];
2N/A grub_uint32_t htsize;
2N/A grub_uint32_t unused2;
2N/A grub_uint32_t checksum;
2N/A grub_uint32_t hashtable[1];
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct grub_affs_time
2N/A{
2N/A grub_int32_t day;
2N/A grub_uint32_t min;
2N/A grub_uint32_t hz;
2N/A} __attribute__ ((packed));
2N/A
2N/A/* The second part of a file header block. */
2N/Astruct grub_affs_file
2N/A{
2N/A grub_uint8_t unused1[12];
2N/A grub_uint32_t size;
2N/A grub_uint8_t unused2[92];
2N/A struct grub_affs_time mtime;
2N/A grub_uint8_t namelen;
2N/A grub_uint8_t name[30];
2N/A grub_uint8_t unused3[33];
2N/A grub_uint32_t next;
2N/A grub_uint32_t parent;
2N/A grub_uint32_t extension;
2N/A grub_int32_t type;
2N/A} __attribute__ ((packed));
2N/A
2N/A/* The location of `struct grub_affs_file' relative to the end of a
2N/A file header block. */
2N/A#define GRUB_AFFS_FILE_LOCATION 200
2N/A
2N/A/* The offset in both the rootblock and the file header block for the
2N/A hashtable, symlink and block pointers (all synonyms). */
2N/A#define GRUB_AFFS_HASHTABLE_OFFSET 24
2N/A#define GRUB_AFFS_BLOCKPTR_OFFSET 24
2N/A#define GRUB_AFFS_SYMLINK_OFFSET 24
2N/A
2N/A#define GRUB_AFFS_FILETYPE_REG 0xfffffffd
2N/A#define GRUB_AFFS_FILETYPE_DIR 2
2N/A#define GRUB_AFFS_FILETYPE_SYMLINK 3
2N/A
2N/A
2N/Astruct grub_fshelp_node
2N/A{
2N/A struct grub_affs_data *data;
2N/A grub_uint32_t block;
2N/A struct grub_fshelp_node *parent;
2N/A struct grub_affs_file di;
2N/A grub_uint32_t *block_cache;
2N/A grub_uint32_t last_block_cache;
2N/A};
2N/A
2N/A/* Information about a "mounted" affs filesystem. */
2N/Astruct grub_affs_data
2N/A{
2N/A struct grub_affs_bblock bblock;
2N/A struct grub_fshelp_node diropen;
2N/A grub_disk_t disk;
2N/A
2N/A /* Blocksize in sectors. */
2N/A int blocksize;
2N/A
2N/A /* The number of entries in the hashtable. */
2N/A int htsize;
2N/A};
2N/A
2N/Astatic grub_dl_t my_mod;
2N/A
2N/A
2N/Astatic grub_disk_addr_t
2N/Agrub_affs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
2N/A{
2N/A grub_uint32_t target, curblock;
2N/A grub_uint32_t pos;
2N/A struct grub_affs_file file;
2N/A struct grub_affs_data *data = node->data;
2N/A grub_uint64_t mod;
2N/A
2N/A if (!node->block_cache)
2N/A {
2N/A node->block_cache = grub_malloc ((((grub_be_to_cpu32 (node->di.size)
2N/A + 511) >> 9) / data->htsize + 1)
2N/A * sizeof (node->block_cache[0]));
2N/A if (!node->block_cache)
2N/A return -1;
2N/A node->last_block_cache = 0;
2N/A node->block_cache[0] = node->block;
2N/A }
2N/A
2N/A /* Files are at most 2G on AFFS, so no need for 64-bit division. */
2N/A target = (grub_uint32_t) fileblock / data->htsize;
2N/A mod = (grub_uint32_t) fileblock % data->htsize;
2N/A /* Find the block that points to the fileblock we are looking up by
2N/A following the chain until the right table is reached. */
2N/A for (curblock = node->last_block_cache + 1; curblock <= target; curblock++)
2N/A {
2N/A grub_disk_read (data->disk,
2N/A node->block_cache[curblock - 1] + data->blocksize - 1,
2N/A data->blocksize * (GRUB_DISK_SECTOR_SIZE
2N/A - GRUB_AFFS_FILE_LOCATION),
2N/A sizeof (file), &file);
2N/A if (grub_errno)
2N/A return 0;
2N/A
2N/A node->block_cache[curblock] = grub_be_to_cpu32 (file.extension);
2N/A node->last_block_cache = curblock;
2N/A }
2N/A
2N/A /* Translate the fileblock to the block within the right table. */
2N/A grub_disk_read (data->disk, node->block_cache[target],
2N/A GRUB_AFFS_BLOCKPTR_OFFSET
2N/A + (data->htsize - mod - 1) * sizeof (pos),
2N/A sizeof (pos), &pos);
2N/A if (grub_errno)
2N/A return 0;
2N/A
2N/A return grub_be_to_cpu32 (pos);
2N/A}
2N/A
2N/Astatic struct grub_affs_data *
2N/Agrub_affs_mount (grub_disk_t disk)
2N/A{
2N/A struct grub_affs_data *data;
2N/A grub_uint32_t *rootblock = 0;
2N/A struct grub_affs_rblock *rblock;
2N/A
2N/A int checksum = 0;
2N/A int blocksize = 0;
2N/A
2N/A data = grub_zalloc (sizeof (struct grub_affs_data));
2N/A if (!data)
2N/A return 0;
2N/A
2N/A /* Read the bootblock. */
2N/A grub_disk_read (disk, 0, 0, sizeof (struct grub_affs_bblock),
2N/A &data->bblock);
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A /* Make sure this is an affs filesystem. */
2N/A if (grub_strncmp ((char *) (data->bblock.type), "DOS", 3))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "not an AFFS filesystem");
2N/A goto fail;
2N/A }
2N/A
2N/A /* Test if the filesystem is a OFS filesystem. */
2N/A if (! (data->bblock.flags & GRUB_AFFS_FLAG_FFS))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "OFS not yet supported");
2N/A goto fail;
2N/A }
2N/A
2N/A /* No sane person uses more than 8KB for a block. At least I hope
2N/A for that person because in that case this won't work. */
2N/A rootblock = grub_malloc (GRUB_DISK_SECTOR_SIZE * 16);
2N/A if (!rootblock)
2N/A goto fail;
2N/A
2N/A rblock = (struct grub_affs_rblock *) rootblock;
2N/A
2N/A /* Read the rootblock. */
2N/A grub_disk_read (disk, grub_be_to_cpu32 (data->bblock.rootblock), 0,
2N/A GRUB_DISK_SECTOR_SIZE * 16, rootblock);
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A /* The filesystem blocksize is not stored anywhere in the filesystem
2N/A itself. One way to determine it is reading blocks for the
2N/A rootblock until the checksum is correct. */
2N/A for (blocksize = 0; blocksize < 8; blocksize++)
2N/A {
2N/A grub_uint32_t *currblock = rootblock + GRUB_DISK_SECTOR_SIZE * blocksize;
2N/A unsigned int i;
2N/A
2N/A for (i = 0; i < GRUB_DISK_SECTOR_SIZE / sizeof (*currblock); i++)
2N/A checksum += grub_be_to_cpu32 (currblock[i]);
2N/A
2N/A if (checksum == 0)
2N/A break;
2N/A }
2N/A if (checksum != 0)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "AFFS blocksize couldn't be determined");
2N/A goto fail;
2N/A }
2N/A blocksize++;
2N/A
2N/A data->blocksize = blocksize;
2N/A data->disk = disk;
2N/A data->htsize = grub_be_to_cpu32 (rblock->htsize);
2N/A data->diropen.data = data;
2N/A data->diropen.block = grub_be_to_cpu32 (data->bblock.rootblock);
2N/A data->diropen.parent = NULL;
2N/A grub_memcpy (&data->diropen.di, rootblock, sizeof (data->diropen.di));
2N/A
2N/A grub_free (rootblock);
2N/A
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 AFFS filesystem");
2N/A
2N/A grub_free (data);
2N/A grub_free (rootblock);
2N/A return 0;
2N/A}
2N/A
2N/A
2N/Astatic char *
2N/Agrub_affs_read_symlink (grub_fshelp_node_t node)
2N/A{
2N/A struct grub_affs_data *data = node->data;
2N/A char *symlink;
2N/A const grub_size_t symlink_size = (data->blocksize * GRUB_DISK_SECTOR_SIZE
2N/A - 225);
2N/A
2N/A symlink = grub_malloc (symlink_size + 1);
2N/A if (!symlink)
2N/A return 0;
2N/A
2N/A grub_disk_read (data->disk, node->block, GRUB_AFFS_SYMLINK_OFFSET,
2N/A symlink_size, symlink);
2N/A if (grub_errno)
2N/A {
2N/A grub_free (symlink);
2N/A return 0;
2N/A }
2N/A symlink[symlink_size] = 1;
2N/A grub_dprintf ("affs", "Symlink: `%s'\n", symlink);
2N/A return symlink;
2N/A}
2N/A
2N/A
2N/Astatic int
2N/Agrub_affs_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 int i;
2N/A struct grub_affs_file file;
2N/A struct grub_fshelp_node *node = 0;
2N/A struct grub_affs_data *data = dir->data;
2N/A grub_uint32_t *hashtable;
2N/A
2N/A auto int NESTED_FUNC_ATTR grub_affs_create_node (grub_uint32_t block,
2N/A const struct grub_affs_file *fil);
2N/A
2N/A int NESTED_FUNC_ATTR grub_affs_create_node (grub_uint32_t block,
2N/A const struct grub_affs_file *fil)
2N/A {
2N/A int type;
2N/A grub_uint8_t name_u8[sizeof (fil->name) * GRUB_MAX_UTF8_PER_LATIN1 + 1];
2N/A grub_size_t len;
2N/A
2N/A node = grub_zalloc (sizeof (*node));
2N/A if (!node)
2N/A {
2N/A grub_free (hashtable);
2N/A return 1;
2N/A }
2N/A
2N/A if (grub_be_to_cpu32 (fil->type) == GRUB_AFFS_FILETYPE_REG)
2N/A type = GRUB_FSHELP_REG;
2N/A else if (grub_be_to_cpu32 (fil->type) == GRUB_AFFS_FILETYPE_DIR)
2N/A type = GRUB_FSHELP_DIR;
2N/A else if (grub_be_to_cpu32 (fil->type) == GRUB_AFFS_FILETYPE_SYMLINK)
2N/A type = GRUB_FSHELP_SYMLINK;
2N/A else
2N/A type = GRUB_FSHELP_UNKNOWN;
2N/A
2N/A node->data = data;
2N/A node->block = block;
2N/A node->di = *fil;
2N/A node->parent = dir;
2N/A
2N/A len = fil->namelen;
2N/A if (len > sizeof (fil->name))
2N/A len = sizeof (fil->name);
2N/A *grub_latin1_to_utf8 (name_u8, fil->name, len) = '\0';
2N/A
2N/A if (hook ((char *) name_u8, type, node))
2N/A {
2N/A grub_free (hashtable);
2N/A return 1;
2N/A }
2N/A return 0;
2N/A }
2N/A
2N/A /* Create the directory entries for `.' and `..'. */
2N/A node = grub_zalloc (sizeof (*node));
2N/A if (!node)
2N/A return 1;
2N/A
2N/A *node = *dir;
2N/A if (hook (".", GRUB_FSHELP_DIR, node))
2N/A return 1;
2N/A if (dir->parent)
2N/A {
2N/A node = grub_zalloc (sizeof (*node));
2N/A if (!node)
2N/A return 1;
2N/A *node = *dir->parent;
2N/A if (hook ("..", GRUB_FSHELP_DIR, node))
2N/A return 1;
2N/A }
2N/A
2N/A hashtable = grub_zalloc (data->htsize * sizeof (*hashtable));
2N/A if (!hashtable)
2N/A return 1;
2N/A
2N/A grub_disk_read (data->disk, dir->block, GRUB_AFFS_HASHTABLE_OFFSET,
2N/A data->htsize * sizeof (*hashtable), (char *) hashtable);
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A for (i = 0; i < data->htsize; i++)
2N/A {
2N/A grub_uint32_t next;
2N/A
2N/A if (!hashtable[i])
2N/A continue;
2N/A
2N/A /* Every entry in the hashtable can be chained. Read the entire
2N/A chain. */
2N/A next = grub_be_to_cpu32 (hashtable[i]);
2N/A
2N/A while (next)
2N/A {
2N/A grub_disk_read (data->disk, next + data->blocksize - 1,
2N/A data->blocksize * GRUB_DISK_SECTOR_SIZE
2N/A - GRUB_AFFS_FILE_LOCATION,
2N/A sizeof (file), (char *) &file);
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A if (grub_affs_create_node (next, &file))
2N/A return 1;
2N/A
2N/A next = grub_be_to_cpu32 (file.next);
2N/A }
2N/A }
2N/A
2N/A grub_free (hashtable);
2N/A return 0;
2N/A
2N/A fail:
2N/A grub_free (node);
2N/A grub_free (hashtable);
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_affs_open (struct grub_file *file, const char *name)
2N/A{
2N/A struct grub_affs_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_affs_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_affs_iterate_dir,
2N/A grub_affs_read_symlink, GRUB_FSHELP_REG);
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A file->size = grub_be_to_cpu32 (fdiro->di.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 grub_free (data);
2N/A
2N/A grub_dl_unref (my_mod);
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_affs_close (grub_file_t file)
2N/A{
2N/A struct grub_affs_data *data =
2N/A (struct grub_affs_data *) file->data;
2N/A
2N/A grub_free (data->diropen.block_cache);
2N/A grub_free (file->data);
2N/A
2N/A grub_dl_unref (my_mod);
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/A/* Read LEN bytes data from FILE into BUF. */
2N/Astatic grub_ssize_t
2N/Agrub_affs_read (grub_file_t file, char *buf, grub_size_t len)
2N/A{
2N/A struct grub_affs_data *data =
2N/A (struct grub_affs_data *) file->data;
2N/A
2N/A return grub_fshelp_read_file (data->diropen.data->disk, &data->diropen,
2N/A file->read_hook,
2N/A file->offset, len, buf, grub_affs_read_block,
2N/A grub_be_to_cpu32 (data->diropen.di.size), 0);
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_affs_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_affs_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.mtimeset = 1;
2N/A info.mtime = grub_be_to_cpu32 (node->di.mtime.day) * 86400
2N/A + grub_be_to_cpu32 (node->di.mtime.min) * 60
2N/A + grub_be_to_cpu32 (node->di.mtime.hz) / 50
2N/A + 8 * 365 * 86400 + 86400 * 2;
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_affs_mount (device->disk);
2N/A if (!data)
2N/A goto fail;
2N/A
2N/A grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_affs_iterate_dir,
2N/A grub_affs_read_symlink, GRUB_FSHELP_DIR);
2N/A if (grub_errno)
2N/A goto fail;
2N/A
2N/A grub_affs_iterate_dir (fdiro, iterate);
2N/A
2N/A fail:
2N/A if (data && fdiro != &data->diropen)
2N/A grub_free (fdiro);
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_affs_label (grub_device_t device, char **label)
2N/A{
2N/A struct grub_affs_data *data;
2N/A struct grub_affs_file file;
2N/A grub_disk_t disk = device->disk;
2N/A
2N/A grub_dl_ref (my_mod);
2N/A
2N/A data = grub_affs_mount (disk);
2N/A if (data)
2N/A {
2N/A grub_size_t len;
2N/A /* The rootblock maps quite well on a file header block, it's
2N/A something we can use here. */
2N/A grub_disk_read (data->disk, grub_be_to_cpu32 (data->bblock.rootblock),
2N/A data->blocksize * (GRUB_DISK_SECTOR_SIZE
2N/A - GRUB_AFFS_FILE_LOCATION),
2N/A sizeof (file), &file);
2N/A if (grub_errno)
2N/A return 0;
2N/A
2N/A len = file.namelen;
2N/A if (len > sizeof (file.name))
2N/A len = sizeof (file.name);
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, file.name, len) = '\0';
2N/A }
2N/A else
2N/A *label = 0;
2N/A
2N/A grub_dl_unref (my_mod);
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_affs_fs =
2N/A {
2N/A .name = "affs",
2N/A .dir = grub_affs_dir,
2N/A .open = grub_affs_open,
2N/A .read = grub_affs_read,
2N/A .close = grub_affs_close,
2N/A .label = grub_affs_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(affs)
2N/A{
2N/A grub_fs_register (&grub_affs_fs);
2N/A my_mod = mod;
2N/A}
2N/A
2N/AGRUB_MOD_FINI(affs)
2N/A{
2N/A grub_fs_unregister (&grub_affs_fs);
2N/A}