command.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* command.c - support basic 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/lockdown.h>
  20. #include <grub/mm.h>
  21. #include <grub/command.h>
  22. grub_command_t grub_command_list;
  23. grub_command_t
  24. grub_register_command_prio (const char *name,
  25. grub_command_func_t func,
  26. const char *summary,
  27. const char *description,
  28. int prio)
  29. {
  30. grub_command_t cmd;
  31. int inactive = 0;
  32. grub_command_t *p, q;
  33. cmd = (grub_command_t) grub_zalloc (sizeof (*cmd));
  34. if (! cmd)
  35. return 0;
  36. cmd->name = name;
  37. cmd->func = func;
  38. cmd->summary = (summary) ? summary : "";
  39. cmd->description = description;
  40. cmd->flags = 0;
  41. cmd->prio = prio;
  42. for (p = &grub_command_list, q = *p; q; p = &(q->next), q = q->next)
  43. {
  44. int r;
  45. r = grub_strcmp (cmd->name, q->name);
  46. if (r < 0)
  47. break;
  48. if (r > 0)
  49. continue;
  50. if (cmd->prio >= (q->prio & GRUB_COMMAND_PRIO_MASK))
  51. {
  52. q->prio &= ~GRUB_COMMAND_FLAG_ACTIVE;
  53. break;
  54. }
  55. inactive = 1;
  56. }
  57. *p = cmd;
  58. cmd->next = q;
  59. if (q)
  60. q->prev = &cmd->next;
  61. cmd->prev = p;
  62. if (! inactive)
  63. cmd->prio |= GRUB_COMMAND_FLAG_ACTIVE;
  64. return cmd;
  65. }
  66. static grub_err_t
  67. grub_cmd_lockdown (grub_command_t cmd __attribute__ ((unused)),
  68. int argc __attribute__ ((unused)),
  69. char **argv __attribute__ ((unused)))
  70. {
  71. return grub_error (GRUB_ERR_ACCESS_DENIED,
  72. N_("%s: the command is not allowed when lockdown is enforced"),
  73. cmd->name);
  74. }
  75. grub_command_t
  76. grub_register_command_lockdown (const char *name,
  77. grub_command_func_t func,
  78. const char *summary,
  79. const char *description)
  80. {
  81. if (grub_is_lockdown () == GRUB_LOCKDOWN_ENABLED)
  82. func = grub_cmd_lockdown;
  83. return grub_register_command_prio (name, func, summary, description, 0);
  84. }
  85. void
  86. grub_unregister_command (grub_command_t cmd)
  87. {
  88. if ((cmd->prio & GRUB_COMMAND_FLAG_ACTIVE) && (cmd->next))
  89. cmd->next->prio |= GRUB_COMMAND_FLAG_ACTIVE;
  90. grub_list_remove (GRUB_AS_LIST (cmd));
  91. grub_free (cmd);
  92. }