/* nthibr.c - tests whether an MS Windows system partition is hibernated */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2007,2008,2009,2011 Free Software Foundation, Inc.
*
* GRUB 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 3 of the License, or
* (at your option) any later version.
*
* GRUB 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 GRUB. If not, see .
*/
#include
#include
#include
#include
#include
#include
#include
#include
GRUB_MOD_LICENSE("GPLv3+");
static grub_err_t
grub_cmd_nthibr (grub_command_t cmd __attribute__ ((unused)),
int argc, char **args)
{
char *partition_name = 0, *hibr_file_path = 0, hibr_file_magic[5];
grub_size_t name_length;
grub_ssize_t magic_size;
grub_file_t hibr_file = 0;
/* Check argument count */
if (argc != 1)
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid argument count"));
/* Make copy of partition specifier, so it can be modified (if needed) */
name_length = grub_strlen (args[0]);
partition_name = grub_zalloc (name_length + 3);
if (!partition_name)
goto exit;
grub_strcpy (partition_name, args[0]);
/* Ensure partition specifier start with a '(' */
if (partition_name[0] != '(')
{
grub_memmove (partition_name + 1, partition_name, name_length++);
partition_name[0] = '(';
}
/* Ensure partition specifier ends with a ')' */
if (partition_name[name_length - 1] != ')')
partition_name[(++name_length) - 1] = ')';
/* Build path to 'hiberfil.sys' */
hibr_file_path = grub_malloc ((grub_strlen (partition_name)
+ grub_strlen ("/hiberfil.sys") + 1));
if (!hibr_file_path)
goto exit;
grub_strcpy (hibr_file_path, partition_name);
grub_strcat (hibr_file_path, "/hiberfil.sys");
/* Try to open 'hiberfil.sys' */
hibr_file = grub_file_open (hibr_file_path);
if (!hibr_file)
goto exit;
/* Try to read magic number of 'hiberfil.sys' */
magic_size = sizeof (hibr_file_magic) - 1;
grub_memset (hibr_file_magic, 0, sizeof (hibr_file_magic));
if (grub_file_read (hibr_file, hibr_file_magic, magic_size) < magic_size)
{
if (!grub_errno)
grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("'hiberfil.sys' too small"));
goto exit;
}
/* Return SUCCESS if magic indicates file is active; else return FAILURE */
if (!grub_strncasecmp ("hibr", hibr_file_magic, magic_size))
grub_error (GRUB_ERR_NONE, "true");
else
grub_error (GRUB_ERR_TEST_FAILURE, "false");
exit:
/* Ensure unmanaged resources are cleaned up */
grub_free (partition_name);
grub_free (hibr_file_path);
if (hibr_file)
grub_file_close (hibr_file);
return grub_errno;
}
static grub_command_t cmd;
GRUB_MOD_INIT (nthibr)
{
cmd = grub_register_command ("nthibr", grub_cmd_nthibr,
N_("DEVICE"),
N_("Test whether an NT system partition "
"is hibernated."));
}
GRUB_MOD_FINI (nthibr)
{
grub_unregister_command (cmd);
}