Skip to content

Instantly share code, notes, and snippets.

@johnlane
Created July 6, 2015 13:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnlane/f5d46294e961ce862060 to your computer and use it in GitHub Desktop.
Save johnlane/f5d46294e961ce862060 to your computer and use it in GitHub Desktop.
Example GRUB Module
/* argshow.c - test module for dynamic loading */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2003,2007 Free Software Foundation, Inc.
* Copyright (C) 2003 NIIBE Yutaka <gniibe@m17n.org>
*
* 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/>.
*/
#include <grub/types.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/err.h>
#include <grub/dl.h>
#include <grub/extcmd.h>
#include <grub/i18n.h>
GRUB_MOD_LICENSE ("GPLv3+");
static const struct grub_arg_option options[] = {
{"string", 's', 0, N_("Option with a string argument"), 0, ARG_TYPE_STRING},
{"integer", 'i', 0, N_("Option with an integer argument"), 0, ARG_TYPE_INT},
{"option1", '1', 0, N_("Option 1 with no argument"), 0, ARG_TYPE_NONE},
{"option2", '2', 0, N_("Option 2 with no argument"), 0, ARG_TYPE_NONE},
{0, 0, 0, 0, 0, 0}
};
static grub_err_t
grub_cmd_argshow (grub_extcmd_context_t ctxt, int argc, char **args)
{
struct grub_arg_list *state = ctxt->state;
int i = 0;
/* options */
for (i=0; options[i].shortarg; i++)
{
grub_printf(_("option '-%c' '--%s' is "), options[i].shortarg, options[i].longarg);
if (state[i].set)
if (options[i].type == ARG_TYPE_STRING)
grub_printf("'%s'\n", state[i].arg);
else if (options[i].type == ARG_TYPE_INT)
grub_printf("%lu\n", grub_strtoul (state[i].arg, 0, 0));
else
grub_printf(_("set\n"));
else
grub_printf(_("not set\n"));
}
/* positional parameters */
if (argc)
grub_printf ("%s\n", _("Positional parameters:"));
for (i=0; i<argc; i++)
grub_printf ("%d : %s\n", i, args[i]);
return 0;
}
static grub_extcmd_t cmd;
GRUB_MOD_INIT(argshow)
{
cmd = grub_register_extcmd ("argshow", grub_cmd_argshow, 0,
N_("[-s STRING] [-i INTEGER] [-1] [-2]"),
N_("Show given arguments"),
options);
}
GRUB_MOD_FINI(argshow)
{
grub_unregister_extcmd (cmd);
}

This is an example Grub module that demonstrates how to add a command to Grub that can process command-line options and other parameters.

AutoGen definitions Makefile.tpl;
module = {
name = argshow;
common = contrib/argshow/argshow.c;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment