grub-devel.gnu.org archive mirror
 help / color / mirror / Atom feed
From: Peter Lustig <peter.lustig.7db@gmail.com>
To: grub-devel@gnu.org
Subject: New command to check NT's hibernation state
Date: Sun, 27 Nov 2011 18:49:45 -0500	[thread overview]
Message-ID: <4ED2CC99.9030703@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 638 bytes --]

Hello all,

   I would like to contribute a small module I wrote to read the header 
of MS Window's "hiberfil.sys" and determine whether it is in use (i.e. 
Windows has been suspended to disk).  What is the procedure for donating 
code?  Would I need commit rights to the repository, or would someone 
else do it for me?  The source for the module is attached.

Thanks,
Peter Lustig

P.S. I have tested the code and verified it works for Windows 7.  Also, 
from what I understand about "hiberfil.sys" from the SandMan project 
(cf. <http://www.msuiche.net/pres/PacSec07-slides-0.4.pdf>), it should 
work for Windows Vista and XP as well.

[-- Attachment #2: nthibr.c --]
[-- Type: text/plain, Size: 5809 bytes --]

/* 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 <http://www.gnu.org/licenses/>.
 */

//======== INCLUDES ==========================================================//

//Common Utilities
#include <grub/mm.h>       //NULL
#include <grub/types.h>    //grub_*size_t
#include <grub/disk.h>     //grub_disk_t, grub_disk_*()
#include <grub/file.h>     //grub_file_t, grub_file_*()
#include <grub/misc.h>     //grub_str*(), grub_*puts(), grub_*printf()

//Module Management
#include <grub/dl.h>       //GRUB_MOD_*()
#include <grub/extcmd.h>   //grub_extcmd_*t, grub_*register_extcmd()
#include <grub/lib/arg.h>  //grub_arg_*, ARG_TYPE_*
#include <grub/err.h>      //grub_err_t, GRUB_ERR_*, grub_error()

//Internationalization
#include <grub/i18n.h>     //N_()

//======== PRAGMAS ===========================================================//

//permit use of the zero-initializer '{0}' for structures
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"

//======== TYPE DEFINITIONS ==================================================//

typedef struct grub_arg_option grub_arg_option;

//======== MODULE VARIABLES ==================================================//

GRUB_MOD_LICENSE("GPLv3+");

static grub_extcmd_t cmd;

static const grub_arg_option options[] = {
  {
    .longarg  = "exit-codes", 
    .shortarg = 'x',
    .flags    = 0x0, 
    .doc      = N_("Display return codes used by this command and exit."), 
    .arg      = NULL,
    .type     = ARG_TYPE_NONE
  },
  {0} //end of list
};

//======== FUNCTIONS =========================================================//

static grub_err_t grub_cmd_nthibr (
  grub_extcmd_context_t ctxt, int argc, char* args[]
){
  grub_err_t status = GRUB_ERR_NONE;

  char *szPartName, szFilePath[32], szFileMagic[5] = {'\0'};

  grub_ssize_t length;

  grub_disk_t hPart;
  grub_file_t hFile = NULL;


  #define ABORT(_code, _message) {             \
    status = grub_error((_code), (_message));  \
    goto exit;                                 \
  }


  //If requested, print return codes and exit; else, proceed
  if (ctxt->state[0].set) {
    grub_printf(
      N_(
        "CODE  MEANING\n"
        "----  -------\n"
        "  %2d  system hibernated\n"
        "  %2d  system not hibernated\n"
        "  %2d  hibernation file too small\n"
        "  %2d  hibernation file not found\n"
        "  %2d  partition not found\n"
        "  %2d  invalid partition specifier\n"
        "  %2d  too few/many arguments provided\n"
      ),
      GRUB_ERR_NONE, GRUB_ERR_TEST_FAILURE, GRUB_ERR_BAD_FILE_TYPE,
      GRUB_ERR_FILE_NOT_FOUND, GRUB_ERR_BAD_FILENAME, GRUB_ERR_UNKNOWN_DEVICE, 
      GRUB_ERR_BAD_ARGUMENT
    );
    goto exit;
  }

  //Check argument count
  if (!argc) {
    ABORT( GRUB_ERR_BAD_ARGUMENT, N_("too few arguments specified") );
  } else if (argc > 1) {
    ABORT( GRUB_ERR_BAD_ARGUMENT, N_("too many arguments specified") );
  }

  szPartName = args[0];
  length = (grub_ssize_t) grub_strlen(szPartName);

  //Check if partition specifier 'looks right'
  if (szPartName[0] != '(' || szPartName[length-1] != ')')
    ABORT( GRUB_ERR_BAD_FILENAME, N_("invalid partition specifier") );

  //Check if partition actually exists
  szPartName[length-1] = '\0';
  if (NULL == (hPart = grub_disk_open(szPartName+1))) {
    ABORT( GRUB_ERR_UNKNOWN_DEVICE, N_("partition not found") );
  } else {
    grub_disk_close(hPart);
    szPartName[length-1] = ')';
  }

  //Build path to 'hiberfil.sys'
  grub_strncpy(szFilePath, szPartName, sizeof(szFilePath) - 1);
  grub_strncat ( 
    szFilePath, 
    "/hiberfil.sys", 
    sizeof(szFilePath) - grub_strlen(szFilePath) - 1
  );

  //Try to open 'hiberfil.sys'
  if (NULL == (hFile = grub_file_open(szFilePath))) {
    ABORT( GRUB_ERR_FILE_NOT_FOUND, N_("'hiberfil.sys' not found") );
  }

  //Try to read magic number of 'hiberfil.sys'
  length = (grub_ssize_t) (sizeof(szFileMagic) - 1);
  if (grub_file_read(hFile, szFileMagic, length) < length)
    ABORT( GRUB_ERR_BAD_FILE_TYPE, N_("'hiberfil.sys' too small") );

  //Return SUCCESS if magic indicates file is active; else return FAILURE
  if (0 == grub_strncasecmp("hibr", szFileMagic, length)) {
    grub_puts(N_("The system is hibernated."));
  } else {
    grub_puts(N_("The system is NOT hibernated."));
    status = GRUB_ERR_TEST_FAILURE;
  }

exit: 
  //Ensure 'hiberfil.sys' is closed
  if (hFile)
    grub_file_close(hFile);

  #undef ABORT
  return status;
} //grub_cmd_nthibr()


GRUB_MOD_INIT(nthibr) {
  cmd = grub_register_extcmd (
    "nthibr",                                                  //name
    grub_cmd_nthibr,                                           //function
    0x0,                                                       //flags = NONE
    N_("DEVICE"),                                              //summary = NONE
    N_("Test whether an NT system partition is hibernated."),  //description
    options                                                    //switches
  );
} //GRUB_MOD_INIT()


GRUB_MOD_FINI(nthibr) {
  grub_unregister_extcmd(cmd);
} //GRUB_MOD_FINI()

             reply	other threads:[~2011-11-27 23:49 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-27 23:49 Peter Lustig [this message]
2011-11-28  1:03 ` New command to check NT's hibernation state Vladimir 'φ-coder/phcoder' Serbinenko
2011-11-28 13:27   ` Vladimir 'φ-coder/phcoder' Serbinenko
  -- strict thread matches above, loose matches on Subject: below --
2011-12-02  5:16 Peter Lustig
2011-12-02  6:05 ` Vladimir 'φ-coder/phcoder' Serbinenko
2011-12-03  4:57 Peter Lustig
2011-12-03 10:41 ` Vladimir 'φ-coder/phcoder' Serbinenko
     [not found] <mailman.207.1322931671.23776.grub-devel@gnu.org>
2011-12-10  6:03 ` Peter Lustig
2011-12-16 15:25   ` Vladimir 'φ-coder/phcoder' Serbinenko
2011-12-16 15:30   ` Vladimir 'φ-coder/phcoder' Serbinenko
     [not found] <mailman.213.1324054906.20674.grub-devel@gnu.org>
2011-12-17  5:41 ` Peter Lustig
2011-12-17 11:39   ` Vladimir 'φ-coder/phcoder' Serbinenko
     [not found] <mailman.171.1324141254.27913.grub-devel@gnu.org>
2011-12-18  3:16 ` Peter Lustig
2011-12-22 12:10   ` Vladimir 'φ-coder/phcoder' Serbinenko
2013-11-04  0:48   ` Vladimir 'φ-coder/phcoder' Serbinenko
2013-11-04 13:13     ` Andrey Borzenkov
2013-11-04 13:24       ` Vladimir 'φ-coder/phcoder' Serbinenko
     [not found] <mailman.235.1324573308.26580.grub-devel@gnu.org>
2011-12-23  4:32 ` Peter Lustig
2012-08-12  4:23   ` Peter Lustig
2013-01-28 18:11   ` Vladimir 'φ-coder/phcoder' Serbinenko
2013-11-04  1:55 Peter Lustig

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4ED2CC99.9030703@gmail.com \
    --to=peter.lustig.7db@gmail.com \
    --cc=grub-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).