extcmd.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* extcmd.c - support extended command */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/mm.h>
  20. #include <grub/list.h>
  21. #include <grub/lockdown.h>
  22. #include <grub/misc.h>
  23. #include <grub/extcmd.h>
  24. #include <grub/script_sh.h>
  25. #include <grub/dl.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. grub_err_t
  28. grub_extcmd_dispatcher (struct grub_command *cmd, int argc, char **args,
  29. struct grub_script *script)
  30. {
  31. int new_argc;
  32. char **new_args;
  33. struct grub_arg_list *state;
  34. struct grub_extcmd_context context;
  35. grub_err_t ret;
  36. grub_extcmd_t ext = cmd->data;
  37. context.state = 0;
  38. context.extcmd = ext;
  39. context.script = script;
  40. if (! ext->options)
  41. {
  42. ret = (ext->func) (&context, argc, args);
  43. return ret;
  44. }
  45. state = grub_arg_list_alloc (ext, argc, args);
  46. if (grub_arg_parse (ext, argc, args, state, &new_args, &new_argc))
  47. {
  48. context.state = state;
  49. ret = (ext->func) (&context, new_argc, new_args);
  50. grub_free (new_args);
  51. grub_free (state);
  52. return ret;
  53. }
  54. grub_free (state);
  55. return grub_errno;
  56. }
  57. static grub_err_t
  58. grub_extcmd_dispatch (struct grub_command *cmd, int argc, char **args)
  59. {
  60. return grub_extcmd_dispatcher (cmd, argc, args, 0);
  61. }
  62. grub_extcmd_t
  63. grub_register_extcmd_prio (const char *name, grub_extcmd_func_t func,
  64. grub_command_flags_t flags, const char *summary,
  65. const char *description,
  66. const struct grub_arg_option *parser,
  67. int prio)
  68. {
  69. grub_extcmd_t ext;
  70. grub_command_t cmd;
  71. ext = (grub_extcmd_t) grub_malloc (sizeof (*ext));
  72. if (! ext)
  73. return 0;
  74. cmd = grub_register_command_prio (name, grub_extcmd_dispatch,
  75. summary, description, prio);
  76. if (! cmd)
  77. {
  78. grub_free (ext);
  79. return 0;
  80. }
  81. cmd->flags = (flags | GRUB_COMMAND_FLAG_EXTCMD);
  82. cmd->data = ext;
  83. ext->cmd = cmd;
  84. ext->func = func;
  85. ext->options = parser;
  86. ext->data = 0;
  87. return ext;
  88. }
  89. grub_extcmd_t
  90. grub_register_extcmd (const char *name, grub_extcmd_func_t func,
  91. grub_command_flags_t flags, const char *summary,
  92. const char *description,
  93. const struct grub_arg_option *parser)
  94. {
  95. return grub_register_extcmd_prio (name, func, flags,
  96. summary, description, parser, 1);
  97. }
  98. static grub_err_t
  99. grub_extcmd_lockdown (grub_extcmd_context_t ctxt __attribute__ ((unused)),
  100. int argc __attribute__ ((unused)),
  101. char **argv __attribute__ ((unused)))
  102. {
  103. return grub_error (GRUB_ERR_ACCESS_DENIED,
  104. N_("%s: the command is not allowed when lockdown is enforced"),
  105. ctxt->extcmd->cmd->name);
  106. }
  107. grub_extcmd_t
  108. grub_register_extcmd_lockdown (const char *name, grub_extcmd_func_t func,
  109. grub_command_flags_t flags, const char *summary,
  110. const char *description,
  111. const struct grub_arg_option *parser)
  112. {
  113. if (grub_is_lockdown () == GRUB_LOCKDOWN_ENABLED)
  114. func = grub_extcmd_lockdown;
  115. return grub_register_extcmd (name, func, flags, summary, description, parser);
  116. }
  117. void
  118. grub_unregister_extcmd (grub_extcmd_t ext)
  119. {
  120. grub_unregister_command (ext->cmd);
  121. grub_free (ext);
  122. }