2N/A/* loopback.c - command to add loopback devices. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2005,2006,2007 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/dl.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/file.h>
2N/A#include <grub/disk.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/extcmd.h>
2N/A#include <grub/i18n.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/Astruct grub_loopback
2N/A{
2N/A char *devname;
2N/A grub_file_t file;
2N/A struct grub_loopback *next;
2N/A};
2N/A
2N/Astatic struct grub_loopback *loopback_list;
2N/A
2N/Astatic const struct grub_arg_option options[] =
2N/A {
2N/A {"delete", 'd', 0, N_("Delete the loopback device entry."), 0, 0},
2N/A {0, 0, 0, 0, 0, 0}
2N/A };
2N/A
2N/A/* Delete the loopback device NAME. */
2N/Astatic grub_err_t
2N/Adelete_loopback (const char *name)
2N/A{
2N/A struct grub_loopback *dev;
2N/A struct grub_loopback **prev;
2N/A
2N/A /* Search for the device. */
2N/A for (dev = loopback_list, prev = &loopback_list;
2N/A dev;
2N/A prev = &dev->next, dev = dev->next)
2N/A if (grub_strcmp (dev->devname, name) == 0)
2N/A break;
2N/A
2N/A if (! dev)
2N/A return grub_error (GRUB_ERR_BAD_DEVICE, "device not found");
2N/A
2N/A /* Remove the device from the list. */
2N/A *prev = dev->next;
2N/A
2N/A grub_free (dev->devname);
2N/A grub_file_close (dev->file);
2N/A grub_free (dev);
2N/A
2N/A return 0;
2N/A}
2N/A
2N/A/* The command to add and remove loopback devices. */
2N/Astatic grub_err_t
2N/Agrub_cmd_loopback (grub_extcmd_context_t ctxt, int argc, char **args)
2N/A{
2N/A struct grub_arg_list *state = ctxt->state;
2N/A grub_file_t file;
2N/A struct grub_loopback *newdev;
2N/A grub_err_t ret;
2N/A
2N/A if (argc < 1)
2N/A return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
2N/A
2N/A /* Check if `-d' was used. */
2N/A if (state[0].set)
2N/A return delete_loopback (args[0]);
2N/A
2N/A if (argc < 2)
2N/A return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required");
2N/A
2N/A file = grub_file_open (args[1]);
2N/A if (! file)
2N/A return grub_errno;
2N/A
2N/A /* First try to replace the old device. */
2N/A for (newdev = loopback_list; newdev; newdev = newdev->next)
2N/A if (grub_strcmp (newdev->devname, args[0]) == 0)
2N/A break;
2N/A
2N/A if (newdev)
2N/A {
2N/A grub_file_close (newdev->file);
2N/A newdev->file = file;
2N/A
2N/A return 0;
2N/A }
2N/A
2N/A /* Unable to replace it, make a new entry. */
2N/A newdev = grub_malloc (sizeof (struct grub_loopback));
2N/A if (! newdev)
2N/A goto fail;
2N/A
2N/A newdev->devname = grub_strdup (args[0]);
2N/A if (! newdev->devname)
2N/A {
2N/A grub_free (newdev);
2N/A goto fail;
2N/A }
2N/A
2N/A newdev->file = file;
2N/A
2N/A /* Add the new entry to the list. */
2N/A newdev->next = loopback_list;
2N/A loopback_list = newdev;
2N/A
2N/A return 0;
2N/A
2N/Afail:
2N/A ret = grub_errno;
2N/A grub_file_close (file);
2N/A return ret;
2N/A}
2N/A
2N/A
2N/Astatic int
2N/Agrub_loopback_iterate (int (*hook) (const char *name),
2N/A grub_disk_pull_t pull)
2N/A{
2N/A struct grub_loopback *d;
2N/A if (pull != GRUB_DISK_PULL_NONE)
2N/A return 0;
2N/A for (d = loopback_list; d; d = d->next)
2N/A {
2N/A if (hook (d->devname))
2N/A return 1;
2N/A }
2N/A return 0;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_loopback_open (const char *name, grub_disk_t disk)
2N/A{
2N/A struct grub_loopback *dev;
2N/A
2N/A for (dev = loopback_list; dev; dev = dev->next)
2N/A if (grub_strcmp (dev->devname, name) == 0)
2N/A break;
2N/A
2N/A if (! dev)
2N/A return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device");
2N/A
2N/A /* Use the filesize for the disk size, round up to a complete sector. */
2N/A if (dev->file->size != GRUB_FILE_SIZE_UNKNOWN)
2N/A disk->total_sectors = ((dev->file->size + GRUB_DISK_SECTOR_SIZE - 1)
2N/A / GRUB_DISK_SECTOR_SIZE);
2N/A else
2N/A disk->total_sectors = GRUB_DISK_SIZE_UNKNOWN;
2N/A disk->id = (unsigned long) dev;
2N/A
2N/A disk->data = dev;
2N/A
2N/A return 0;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_loopback_read (grub_disk_t disk, grub_disk_addr_t sector,
2N/A grub_size_t size, char *buf)
2N/A{
2N/A grub_file_t file = ((struct grub_loopback *) disk->data)->file;
2N/A grub_off_t pos;
2N/A
2N/A grub_file_seek (file, sector << GRUB_DISK_SECTOR_BITS);
2N/A
2N/A grub_file_read (file, buf, size << GRUB_DISK_SECTOR_BITS);
2N/A if (grub_errno)
2N/A return grub_errno;
2N/A
2N/A /* In case there is more data read than there is available, in case
2N/A of files that are not a multiple of GRUB_DISK_SECTOR_SIZE, fill
2N/A the rest with zeros. */
2N/A pos = (sector + size) << GRUB_DISK_SECTOR_BITS;
2N/A if (pos > file->size)
2N/A {
2N/A grub_size_t amount = pos - file->size;
2N/A grub_memset (buf + (size << GRUB_DISK_SECTOR_BITS) - amount, 0, amount);
2N/A }
2N/A
2N/A return 0;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_loopback_write (grub_disk_t disk __attribute ((unused)),
2N/A grub_disk_addr_t sector __attribute ((unused)),
2N/A grub_size_t size __attribute ((unused)),
2N/A const char *buf __attribute ((unused)))
2N/A{
2N/A return GRUB_ERR_NOT_IMPLEMENTED_YET;
2N/A}
2N/A
2N/Astatic struct grub_disk_dev grub_loopback_dev =
2N/A {
2N/A .name = "loopback",
2N/A .id = GRUB_DISK_DEVICE_LOOPBACK_ID,
2N/A .iterate = grub_loopback_iterate,
2N/A .open = grub_loopback_open,
2N/A .read = grub_loopback_read,
2N/A .write = grub_loopback_write,
2N/A .next = 0
2N/A };
2N/A
2N/Astatic grub_extcmd_t cmd;
2N/A
2N/AGRUB_MOD_INIT(loopback)
2N/A{
2N/A cmd = grub_register_extcmd ("loopback", grub_cmd_loopback, 0,
2N/A N_("[-d] DEVICENAME FILE."),
2N/A N_("Make a device of a file."), options);
2N/A grub_disk_dev_register (&grub_loopback_dev);
2N/A}
2N/A
2N/AGRUB_MOD_FINI(loopback)
2N/A{
2N/A grub_unregister_extcmd (cmd);
2N/A grub_disk_dev_unregister (&grub_loopback_dev);
2N/A}