grub/grub-core/commands/parttool.c

332 lines
7.8 KiB
C
Raw Normal View History

/* parttool.c - common dispatcher and parser for partition operations */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2009 Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <grub/types.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/err.h>
#include <grub/dl.h>
#include <grub/normal.h>
#include <grub/device.h>
#include <grub/disk.h>
#include <grub/partition.h>
#include <grub/parttool.h>
#include <grub/command.h>
2010-01-03 Carles Pina i Estany <carles@pina.cat> * commands/acpi.c: Gettextizze help strings and/or options. Include `grub/i18n.h' if needed. * commands/blocklist.c: Likewise. * commands/boot.c: Likewise. * commands/cat.c: Likewise. * commands/cmp.c: Likewise. * commands/configfile.c: Likewise. * commands/crc.c: Likewise. * commands/date.c: Likewise. * commands/echo.c: Likewise. * commands/efi/fixvideo.c: Likewise. * commands/efi/loadbios.c: Likewise. * commands/gptsync.c: Likewise. * commands/halt.c: Likewise. * commands/handler.c: Likewise. * commands/hdparm.c: Likewise. * commands/hexdump.c: Likewise. * commands/i386/cpuid.c: Likewise. * commands/i386/pc/drivemap.c: Likewise. * commands/i386/pc/halt.c: Likewise. * commands/i386/pc/pxecmd.c: Likewise. * commands/i386/pc/vbeinfo.c: Likewise. * commands/i386/pc/vbetest.c: Likewise. * commands/ieee1275/suspend.c: Likewise. * commands/keystatus.c: Likewise. * commands/loadenv.c: Likewise. * commands/ls.c: Likewise. * commands/lsmmap.c: Likewise. * commands/lspci.c: Likewise. * commands/memrw.c: Likewise. * commands/minicmd.c: Likewise. * commands/parttool.c: Likewise. * commands/password.c: Likewise. * commands/probe.c: Likewise. * commands/read.c: Likewise. * commands/reboot.c: Likewise. * commands/search.c: Likewise. * commands/sleep.c: Likewise. * commands/test.c: Likewise. * commands/true.c: Likewise. * commands/usbtest.c: Likewise. * commands/videotest.c: Likewise. * commands/xnu_uuid.c: Likewise. * disk/loopback.c: Likewise. * hello/hello.c: Likewise. * loader/i386/bsd.c: Likewise. * term/i386/pc/serial.c: Likewise. * po/POTFILES: Add new files.
2010-01-03 18:24:22 +00:00
#include <grub/i18n.h>
static struct grub_parttool *parts = 0;
static int curhandle = 0;
static grub_dl_t mymod;
static char helpmsg[] =
2009-12-21 Carles Pina i Estany <carles@pina.cat> * commands/acpi.c (options): Fix capitalizations and/or full stops. (GRUB_MOD_INIT): Likewise. * commands/boot.c (GRUB_MOD_INIT): Likewise. * commands/cmp.c (grub_cmd_cmp): Likewise. * commands/echo.c (options): Likewise. * commands/efi/loadbios.c (enable_rom_area): Likewise. (enable_rom_area): Likewise. (GRUB_MOD_INIT): Likewise. * commands/gptsync.c (GRUB_MOD_INIT): Likewise. * commands/halt.c (GRUB_MOD_INIT): Improve the help message. * commands/handler.c (GRUB_MOD_INIT): Likewise. * commands/hdparm.c (options): Fix capitalizations and/or full stops. * commands/hexdump.c (options): Likewise. * commands/i386/cpuid.c (options): Likewise. (GRUB_MOD_INIT): Likewise. * commands/i386/pc/drivemap.c (options): Likewise. (GRUB_MOD_INIT): Likewise. * commands/i386/pc/halt (options): Likewise. (GRUB_MOD_INIT): Likewise. * commands/i386/pc/play.c (GRUB_MOD_INIT): Likewise. * commands/i386/pc/pxecmd.c (options): Likewise. * commands/i386/pc/vbetest.c (GRUB_MOD_INIT): Likewise. * commands/ieee1275/suspend.c (GRUB_MOD_INIT): Likewise. * commands/keystatus.c (options): Likewise. (GRUB_MOD_INIT): Likewise. * commands/loadenv.c (options): Likewise. * commands/ls.c (options): Likewise. * commands/lspci.c (GRUB_MOD_INIT): Likewise. * commands/memrw.c (GRUB_MOD_INIT): Likewise. * commands/minicmd.c (GRUB_MOD_INIT): Likewise. * commands/parttool.c (helpmsg): Likewise. * commands/probe.c (options): Likewise. * commands/read.c (GRUB_MOD_INIT): Likewise. * commands/reboot.c (GRUB_MOD_INIT): Likewise. * commands/search.c (options): Likewise. * commands/sleep.c (options): Likewise. * commands/test.c (GRUB_MOD_INIT): Likewise. * commands/true.c (GRUB_MOD_INIT): Likewise. * commands/usbtest.c (GRUB_MOD_INIT): Likewise. * commands/videotest.c (GRUB_MOD_INIT): Likewise. * lib/arg.c (help_options): Likewise.
2009-12-21 22:06:04 +00:00
"Perform COMMANDS on partition.\n"
"Use \"parttool PARTITION help\" for the list "
2009-12-21 Carles Pina i Estany <carles@pina.cat> * commands/acpi.c (options): Fix capitalizations and/or full stops. (GRUB_MOD_INIT): Likewise. * commands/boot.c (GRUB_MOD_INIT): Likewise. * commands/cmp.c (grub_cmd_cmp): Likewise. * commands/echo.c (options): Likewise. * commands/efi/loadbios.c (enable_rom_area): Likewise. (enable_rom_area): Likewise. (GRUB_MOD_INIT): Likewise. * commands/gptsync.c (GRUB_MOD_INIT): Likewise. * commands/halt.c (GRUB_MOD_INIT): Improve the help message. * commands/handler.c (GRUB_MOD_INIT): Likewise. * commands/hdparm.c (options): Fix capitalizations and/or full stops. * commands/hexdump.c (options): Likewise. * commands/i386/cpuid.c (options): Likewise. (GRUB_MOD_INIT): Likewise. * commands/i386/pc/drivemap.c (options): Likewise. (GRUB_MOD_INIT): Likewise. * commands/i386/pc/halt (options): Likewise. (GRUB_MOD_INIT): Likewise. * commands/i386/pc/play.c (GRUB_MOD_INIT): Likewise. * commands/i386/pc/pxecmd.c (options): Likewise. * commands/i386/pc/vbetest.c (GRUB_MOD_INIT): Likewise. * commands/ieee1275/suspend.c (GRUB_MOD_INIT): Likewise. * commands/keystatus.c (options): Likewise. (GRUB_MOD_INIT): Likewise. * commands/loadenv.c (options): Likewise. * commands/ls.c (options): Likewise. * commands/lspci.c (GRUB_MOD_INIT): Likewise. * commands/memrw.c (GRUB_MOD_INIT): Likewise. * commands/minicmd.c (GRUB_MOD_INIT): Likewise. * commands/parttool.c (helpmsg): Likewise. * commands/probe.c (options): Likewise. * commands/read.c (GRUB_MOD_INIT): Likewise. * commands/reboot.c (GRUB_MOD_INIT): Likewise. * commands/search.c (options): Likewise. * commands/sleep.c (options): Likewise. * commands/test.c (GRUB_MOD_INIT): Likewise. * commands/true.c (GRUB_MOD_INIT): Likewise. * commands/usbtest.c (GRUB_MOD_INIT): Likewise. * commands/videotest.c (GRUB_MOD_INIT): Likewise. * lib/arg.c (help_options): Likewise.
2009-12-21 22:06:04 +00:00
"of available commands.";
2009-06-10 21:04:23 +00:00
int
grub_parttool_register(const char *part_name,
const grub_parttool_function_t func,
const struct grub_parttool_argdesc *args)
{
struct grub_parttool *cur;
int nargs = 0;
if (! parts)
grub_dl_ref (mymod);
cur = (struct grub_parttool *) grub_malloc (sizeof (struct grub_parttool));
cur->next = parts;
cur->name = grub_strdup (part_name);
cur->handle = curhandle++;
for (nargs = 0; args[nargs].name != 0; nargs++);
cur->nargs = nargs;
2009-06-10 21:04:23 +00:00
cur->args = (struct grub_parttool_argdesc *)
grub_malloc ((nargs + 1) * sizeof (struct grub_parttool_argdesc));
2009-06-10 21:04:23 +00:00
grub_memcpy (cur->args, args,
(nargs + 1) * sizeof (struct grub_parttool_argdesc));
2009-06-10 21:04:23 +00:00
cur->func = func;
parts = cur;
return cur->handle;
}
void
grub_parttool_unregister (int handle)
{
struct grub_parttool *prev = 0, *cur, *t;
for (cur = parts; cur; )
if (cur->handle == handle)
{
grub_free (cur->args);
grub_free (cur->name);
if (prev)
prev->next = cur->next;
else
parts = cur->next;
t = cur;
cur = cur->next;
grub_free (t);
}
else
{
prev = cur;
cur = cur->next;
}
if (! parts)
grub_dl_unref (mymod);
}
static grub_err_t
grub_cmd_parttool (grub_command_t cmd __attribute__ ((unused)),
int argc, char **args)
{
grub_device_t dev;
struct grub_parttool *cur, *ptool;
int *parsed;
int i, j;
grub_err_t err = GRUB_ERR_NONE;
auto grub_err_t show_help (void);
grub_err_t show_help (void)
{
int found = 0;
for (cur = parts; cur; cur = cur->next)
if (grub_strcmp (dev->disk->partition->partmap->name, cur->name) == 0)
{
struct grub_parttool_argdesc *curarg;
found = 1;
for (curarg = cur->args; curarg->name; curarg++)
{
int spacing = 20;
2009-06-10 21:04:23 +00:00
spacing -= grub_strlen (curarg->name);
grub_printf ("%s", curarg->name);
2009-06-10 21:04:23 +00:00
switch (curarg->type)
{
case GRUB_PARTTOOL_ARG_BOOL:
grub_printf ("+/-");
spacing -= 3;
break;
2009-06-10 21:04:23 +00:00
case GRUB_PARTTOOL_ARG_VAL:
grub_printf ("=VAL");
spacing -= 4;
break;
2009-06-10 21:04:23 +00:00
case GRUB_PARTTOOL_ARG_END:
break;
}
while (spacing-- > 0)
grub_printf (" ");
grub_printf ("%s\n", curarg->desc);
}
}
if (! found)
2009-06-10 21:04:23 +00:00
grub_printf ("Sorry no parttool is available for %s\n",
dev->disk->partition->partmap->name);
return GRUB_ERR_NONE;
}
if (argc < 1)
{
grub_printf ("%s\n", helpmsg);
return grub_error (GRUB_ERR_BAD_ARGUMENT, "too few arguments");
}
if (args[0][0] == '(' && args[0][grub_strlen (args[0]) - 1] == ')')
{
args[0][grub_strlen (args[0]) - 1] = 0;
2009-06-10 21:04:23 +00:00
dev = grub_device_open (args[0] + 1);
args[0][grub_strlen (args[0]) - 1] = ')';
}
else
2009-06-10 21:04:23 +00:00
dev = grub_device_open (args[0]);
if (! dev)
return grub_errno;
if (! dev->disk)
{
grub_device_close (dev);
return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a disk");
}
if (! dev->disk->partition)
{
grub_device_close (dev);
return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a partition");
}
/* Load modules. */
if (! grub_no_autoload)
{
const char *prefix;
prefix = grub_env_get ("prefix");
if (prefix)
{
char *filename;
filename = grub_xasprintf ("%s/parttool.lst", prefix);
if (filename)
{
grub_file_t file;
2009-06-10 21:04:23 +00:00
file = grub_file_open (filename);
if (file)
{
char *buf = 0;
for (;; grub_free(buf))
{
char *p, *name;
buf = grub_file_getline (file);
2009-06-10 21:04:23 +00:00
if (! buf)
break;
2009-06-10 21:04:23 +00:00
name = buf;
if (! grub_isgraph (name[0]))
continue;
2009-06-10 21:04:23 +00:00
p = grub_strchr (name, ':');
if (! p)
continue;
2009-06-10 21:04:23 +00:00
*p = '\0';
while (*++p == ' ')
;
if (! grub_isgraph (*p))
continue;
2009-06-10 21:04:23 +00:00
if (grub_strcmp (name, dev->disk->partition->partmap->name)
!= 0)
continue;
2009-06-10 21:04:23 +00:00
grub_dl_load (p);
}
2009-06-10 21:04:23 +00:00
grub_file_close (file);
}
2009-06-10 21:04:23 +00:00
grub_free (filename);
}
}
/* Ignore errors. */
grub_errno = GRUB_ERR_NONE;
}
if (argc == 1)
return show_help ();
for (i = 1; i < argc; i++)
if (grub_strcmp (args[i], "help") == 0)
return show_help ();
2009-07-16 Pavel Roskin <proski@gnu.org> * kern/mm.c (grub_zalloc): New function. (grub_debug_zalloc): Likewise. * include/grub/mm.h: Declare grub_zalloc() and grub_debug_zalloc(). * util/misc.c (grub_zalloc): New function. * bus/usb/uhci.c (grub_uhci_pci_iter): Use grub_zalloc() instead of grub_malloc(), remove unneeded initializations. * bus/usb/usbhub.c (grub_usb_hub_add_dev): Likewise. * commands/extcmd.c (grub_extcmd_dispatcher): Likewise. * commands/parttool.c (grub_cmd_parttool): Likewise. * disk/i386/pc/biosdisk.c (grub_biosdisk_open): Likewise. * disk/raid5_recover.c (grub_raid5_recover): Likewise. * disk/raid6_recover.c (grub_raid6_recover): Likewise. * disk/usbms.c (grub_usbms_finddevs): Likewise. * efiemu/mm.c (grub_efiemu_request_memalign): Likewise. * efiemu/pnvram.c (grub_efiemu_pnvram): Likewise. (grub_cmd_efiemu_pnvram): Likewise. * fs/i386/pc/pxe.c (grub_pxefs_open): Likewise. * fs/iso9660.c (grub_iso9660_mount): Likewise. (grub_iso9660_iterate_dir): Likewise. * fs/jfs.c (grub_jfs_opendir): Likewise. * fs/ntfs.c (list_file): Likewise. (grub_ntfs_mount): Likewise. * kern/disk.c (grub_disk_open): Likewise. * kern/dl.c (grub_dl_load_core): Likewise. * kern/elf.c (grub_elf_file): Likewise. * kern/env.c (grub_env_context_open): Likewise. (grub_env_set): Likewise. (grub_env_set_data_slot): Likewise. * kern/file.c (grub_file_open): Likewise. * kern/fs.c (grub_fs_blocklist_open): Likewise. * loader/i386/multiboot.c (grub_module): Likewise. * loader/xnu.c (grub_xnu_create_key): Likewise. (grub_xnu_create_value): Likewise. * normal/main.c (grub_normal_add_menu_entry): Likewise. (read_config_file): Likewise. * normal/menu_entry.c (make_screen): Likewise. * partmap/sun.c (sun_partition_map_iterate): Likewise. * script/sh/lexer.c (grub_script_lexer_init): Likewise. * script/sh/script.c (grub_script_parse): Likewise. * video/bitmap.c (grub_video_bitmap_create): Likewise. * video/readers/jpeg.c (grub_video_reader_jpeg): Likewise. * video/readers/png.c (grub_png_output_byte): Likewise. (grub_video_reader_png): Likewise.
2009-07-16 22:14:09 +00:00
parsed = (int *) grub_zalloc (argc * sizeof (int));
for (i = 1; i < argc; i++)
if (! parsed[i])
{
struct grub_parttool_argdesc *curarg;
struct grub_parttool_args *pargs;
for (cur = parts; cur; cur = cur->next)
if (grub_strcmp (dev->disk->partition->partmap->name, cur->name) == 0)
{
for (curarg = cur->args; curarg->name; curarg++)
2009-06-10 21:04:23 +00:00
if (grub_strncmp (curarg->name, args[i],
grub_strlen (curarg->name)) == 0
2009-06-10 21:04:23 +00:00
&& ((curarg->type == GRUB_PARTTOOL_ARG_BOOL
&& (args[i][grub_strlen (curarg->name)] == '+'
|| args[i][grub_strlen (curarg->name)] == '-'
|| args[i][grub_strlen (curarg->name)] == 0))
|| (curarg->type == GRUB_PARTTOOL_ARG_VAL
&& args[i][grub_strlen (curarg->name)] == '=')))
2009-06-10 21:04:23 +00:00
break;
if (curarg->name)
break;
}
if (! cur)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unrecognised argument %s",
args[i]);
ptool = cur;
2009-06-10 21:04:23 +00:00
pargs = (struct grub_parttool_args *)
2009-07-16 Pavel Roskin <proski@gnu.org> * kern/mm.c (grub_zalloc): New function. (grub_debug_zalloc): Likewise. * include/grub/mm.h: Declare grub_zalloc() and grub_debug_zalloc(). * util/misc.c (grub_zalloc): New function. * bus/usb/uhci.c (grub_uhci_pci_iter): Use grub_zalloc() instead of grub_malloc(), remove unneeded initializations. * bus/usb/usbhub.c (grub_usb_hub_add_dev): Likewise. * commands/extcmd.c (grub_extcmd_dispatcher): Likewise. * commands/parttool.c (grub_cmd_parttool): Likewise. * disk/i386/pc/biosdisk.c (grub_biosdisk_open): Likewise. * disk/raid5_recover.c (grub_raid5_recover): Likewise. * disk/raid6_recover.c (grub_raid6_recover): Likewise. * disk/usbms.c (grub_usbms_finddevs): Likewise. * efiemu/mm.c (grub_efiemu_request_memalign): Likewise. * efiemu/pnvram.c (grub_efiemu_pnvram): Likewise. (grub_cmd_efiemu_pnvram): Likewise. * fs/i386/pc/pxe.c (grub_pxefs_open): Likewise. * fs/iso9660.c (grub_iso9660_mount): Likewise. (grub_iso9660_iterate_dir): Likewise. * fs/jfs.c (grub_jfs_opendir): Likewise. * fs/ntfs.c (list_file): Likewise. (grub_ntfs_mount): Likewise. * kern/disk.c (grub_disk_open): Likewise. * kern/dl.c (grub_dl_load_core): Likewise. * kern/elf.c (grub_elf_file): Likewise. * kern/env.c (grub_env_context_open): Likewise. (grub_env_set): Likewise. (grub_env_set_data_slot): Likewise. * kern/file.c (grub_file_open): Likewise. * kern/fs.c (grub_fs_blocklist_open): Likewise. * loader/i386/multiboot.c (grub_module): Likewise. * loader/xnu.c (grub_xnu_create_key): Likewise. (grub_xnu_create_value): Likewise. * normal/main.c (grub_normal_add_menu_entry): Likewise. (read_config_file): Likewise. * normal/menu_entry.c (make_screen): Likewise. * partmap/sun.c (sun_partition_map_iterate): Likewise. * script/sh/lexer.c (grub_script_lexer_init): Likewise. * script/sh/script.c (grub_script_parse): Likewise. * video/bitmap.c (grub_video_bitmap_create): Likewise. * video/readers/jpeg.c (grub_video_reader_jpeg): Likewise. * video/readers/png.c (grub_png_output_byte): Likewise. (grub_video_reader_png): Likewise.
2009-07-16 22:14:09 +00:00
grub_zalloc (ptool->nargs * sizeof (struct grub_parttool_args));
for (j = i; j < argc; j++)
if (! parsed[j])
{
for (curarg = ptool->args; curarg->name; curarg++)
2009-06-10 21:04:23 +00:00
if (grub_strncmp (curarg->name, args[i],
grub_strlen (curarg->name)) == 0
2009-06-10 21:04:23 +00:00
&& ((curarg->type == GRUB_PARTTOOL_ARG_BOOL
&& (args[j][grub_strlen (curarg->name)] == '+'
|| args[j][grub_strlen (curarg->name)] == '-'
|| args[j][grub_strlen (curarg->name)] == 0))
|| (curarg->type == GRUB_PARTTOOL_ARG_VAL
&& args[j][grub_strlen (curarg->name)] == '=')))
{
parsed[j] = 1;
pargs[curarg - ptool->args].set = 1;
switch (curarg->type)
{
case GRUB_PARTTOOL_ARG_BOOL:
2009-06-10 21:04:23 +00:00
pargs[curarg - ptool->args].bool
= (args[j][grub_strlen (curarg->name)] != '-');
break;
case GRUB_PARTTOOL_ARG_VAL:
2009-06-10 21:04:23 +00:00
pargs[curarg - ptool->args].str
= (args[j] + grub_strlen (curarg->name) + 1);
break;
2009-06-10 21:04:23 +00:00
case GRUB_PARTTOOL_ARG_END:
break;
}
}
}
err = ptool->func (dev, pargs);
grub_free (pargs);
if (err)
break;
}
grub_free (parsed);
grub_device_close (dev);
return err;
}
static grub_command_t cmd;
GRUB_MOD_INIT(parttool)
{
mymod = mod;
2009-06-10 21:04:23 +00:00
cmd = grub_register_command ("parttool", grub_cmd_parttool,
2010-01-03 Carles Pina i Estany <carles@pina.cat> * commands/acpi.c: Gettextizze help strings and/or options. Include `grub/i18n.h' if needed. * commands/blocklist.c: Likewise. * commands/boot.c: Likewise. * commands/cat.c: Likewise. * commands/cmp.c: Likewise. * commands/configfile.c: Likewise. * commands/crc.c: Likewise. * commands/date.c: Likewise. * commands/echo.c: Likewise. * commands/efi/fixvideo.c: Likewise. * commands/efi/loadbios.c: Likewise. * commands/gptsync.c: Likewise. * commands/halt.c: Likewise. * commands/handler.c: Likewise. * commands/hdparm.c: Likewise. * commands/hexdump.c: Likewise. * commands/i386/cpuid.c: Likewise. * commands/i386/pc/drivemap.c: Likewise. * commands/i386/pc/halt.c: Likewise. * commands/i386/pc/pxecmd.c: Likewise. * commands/i386/pc/vbeinfo.c: Likewise. * commands/i386/pc/vbetest.c: Likewise. * commands/ieee1275/suspend.c: Likewise. * commands/keystatus.c: Likewise. * commands/loadenv.c: Likewise. * commands/ls.c: Likewise. * commands/lsmmap.c: Likewise. * commands/lspci.c: Likewise. * commands/memrw.c: Likewise. * commands/minicmd.c: Likewise. * commands/parttool.c: Likewise. * commands/password.c: Likewise. * commands/probe.c: Likewise. * commands/read.c: Likewise. * commands/reboot.c: Likewise. * commands/search.c: Likewise. * commands/sleep.c: Likewise. * commands/test.c: Likewise. * commands/true.c: Likewise. * commands/usbtest.c: Likewise. * commands/videotest.c: Likewise. * commands/xnu_uuid.c: Likewise. * disk/loopback.c: Likewise. * hello/hello.c: Likewise. * loader/i386/bsd.c: Likewise. * term/i386/pc/serial.c: Likewise. * po/POTFILES: Add new files.
2010-01-03 18:24:22 +00:00
N_("PARTITION COMMANDS"),
helpmsg);
}
GRUB_MOD_FINI(parttool)
{
grub_unregister_command (cmd);
}