dyncmd.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* dyncmd.c - support dynamic 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/dl.h>
  20. #include <grub/mm.h>
  21. #include <grub/env.h>
  22. #include <grub/misc.h>
  23. #include <grub/command.h>
  24. #include <grub/normal.h>
  25. #include <grub/extcmd.h>
  26. #include <grub/script_sh.h>
  27. #include <grub/i18n.h>
  28. grub_command_t
  29. grub_dyncmd_get_cmd (grub_command_t cmd)
  30. {
  31. grub_extcmd_t extcmd = cmd->data;
  32. char *modname;
  33. char *name;
  34. grub_dl_t mod;
  35. modname = extcmd->data;
  36. mod = grub_dl_load (modname);
  37. if (!mod)
  38. return NULL;
  39. grub_free (modname);
  40. grub_dl_ref (mod);
  41. name = (char *) cmd->name;
  42. grub_unregister_extcmd (extcmd);
  43. cmd = grub_command_find (name);
  44. grub_free (name);
  45. return cmd;
  46. }
  47. static grub_err_t
  48. grub_dyncmd_dispatcher (struct grub_extcmd_context *ctxt,
  49. int argc, char **args)
  50. {
  51. char *modname;
  52. grub_dl_t mod;
  53. grub_err_t ret;
  54. grub_extcmd_t extcmd = ctxt->extcmd;
  55. grub_command_t cmd = extcmd->cmd;
  56. char *name;
  57. modname = extcmd->data;
  58. mod = grub_dl_load (modname);
  59. if (!mod)
  60. return grub_errno;
  61. grub_free (modname);
  62. grub_dl_ref (mod);
  63. name = (char *) cmd->name;
  64. grub_unregister_extcmd (extcmd);
  65. cmd = grub_command_find (name);
  66. if (cmd)
  67. {
  68. if (cmd->flags & GRUB_COMMAND_FLAG_BLOCKS &&
  69. cmd->flags & GRUB_COMMAND_FLAG_EXTCMD)
  70. ret = grub_extcmd_dispatcher (cmd, argc, args, ctxt->script);
  71. else
  72. ret = (cmd->func) (cmd, argc, args);
  73. }
  74. else
  75. ret = grub_errno;
  76. grub_free (name);
  77. return ret;
  78. }
  79. /* Read the file command.lst for auto-loading. */
  80. void
  81. read_command_list (const char *prefix)
  82. {
  83. if (prefix)
  84. {
  85. char *filename;
  86. filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM
  87. "/command.lst", prefix);
  88. if (filename)
  89. {
  90. grub_file_t file;
  91. file = grub_file_open (filename, GRUB_FILE_TYPE_GRUB_MODULE_LIST);
  92. if (file)
  93. {
  94. char *buf = NULL;
  95. grub_command_t ptr, last = 0, next;
  96. /* Override previous commands.lst. */
  97. for (ptr = grub_command_list; ptr; ptr = next)
  98. {
  99. next = ptr->next;
  100. if (ptr->flags & GRUB_COMMAND_FLAG_DYNCMD)
  101. {
  102. if (last)
  103. last->next = ptr->next;
  104. else
  105. grub_command_list = ptr->next;
  106. grub_free (ptr->data); /* extcmd struct */
  107. grub_free (ptr);
  108. }
  109. else
  110. last = ptr;
  111. }
  112. for (;; grub_free (buf))
  113. {
  114. char *p, *name, *modname;
  115. grub_extcmd_t cmd;
  116. int prio = 0;
  117. buf = grub_file_getline (file);
  118. if (! buf)
  119. break;
  120. name = buf;
  121. while (grub_isspace (name[0]))
  122. name++;
  123. if (*name == '*')
  124. {
  125. name++;
  126. prio++;
  127. }
  128. if (! grub_isgraph (name[0]))
  129. continue;
  130. p = grub_strchr (name, ':');
  131. if (! p)
  132. continue;
  133. *p = '\0';
  134. p++;
  135. while (*p == ' ' || *p == '\t')
  136. p++;
  137. if (! grub_isgraph (*p))
  138. continue;
  139. if (grub_dl_get (p))
  140. continue;
  141. name = grub_strdup (name);
  142. if (! name)
  143. continue;
  144. modname = grub_strdup (p);
  145. if (! modname)
  146. {
  147. grub_free (name);
  148. continue;
  149. }
  150. cmd = grub_register_extcmd_prio (name,
  151. grub_dyncmd_dispatcher,
  152. GRUB_COMMAND_FLAG_BLOCKS
  153. | GRUB_COMMAND_FLAG_EXTCMD
  154. | GRUB_COMMAND_FLAG_DYNCMD,
  155. 0, N_("module isn't loaded"),
  156. 0, prio);
  157. if (! cmd)
  158. {
  159. grub_free (name);
  160. grub_free (modname);
  161. continue;
  162. }
  163. cmd->data = modname;
  164. /* Update the active flag. */
  165. grub_command_find (name);
  166. }
  167. grub_file_close (file);
  168. }
  169. grub_free (filename);
  170. }
  171. }
  172. /* Ignore errors. */
  173. grub_errno = GRUB_ERR_NONE;
  174. }