boot.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* boot.c - command to boot an operating system */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2003,2004,2005,2007,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/normal.h>
  20. #include <grub/dl.h>
  21. #include <grub/misc.h>
  22. #include <grub/loader.h>
  23. #include <grub/kernel.h>
  24. #include <grub/mm.h>
  25. #include <grub/i18n.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. static grub_err_t (*grub_loader_boot_func) (void *context);
  28. static grub_err_t (*grub_loader_unload_func) (void *context);
  29. static void *grub_loader_context;
  30. static int grub_loader_flags;
  31. struct grub_simple_loader_hooks
  32. {
  33. grub_err_t (*boot) (void);
  34. grub_err_t (*unload) (void);
  35. };
  36. /* Don't heap allocate this to avoid making grub_loader_set() fallible. */
  37. static struct grub_simple_loader_hooks simple_loader_hooks;
  38. struct grub_preboot
  39. {
  40. grub_err_t (*preboot_func) (int);
  41. grub_err_t (*preboot_rest_func) (void);
  42. grub_loader_preboot_hook_prio_t prio;
  43. struct grub_preboot *next;
  44. struct grub_preboot *prev;
  45. };
  46. static int grub_loader_loaded;
  47. static struct grub_preboot *preboots_head = 0,
  48. *preboots_tail = 0;
  49. static grub_err_t
  50. grub_simple_boot_hook (void *context)
  51. {
  52. struct grub_simple_loader_hooks *hooks;
  53. hooks = (struct grub_simple_loader_hooks *) context;
  54. return hooks->boot ();
  55. }
  56. static grub_err_t
  57. grub_simple_unload_hook (void *context)
  58. {
  59. struct grub_simple_loader_hooks *hooks;
  60. grub_err_t ret;
  61. hooks = (struct grub_simple_loader_hooks *) context;
  62. ret = hooks->unload ();
  63. grub_memset (hooks, 0, sizeof (*hooks));
  64. return ret;
  65. }
  66. int
  67. grub_loader_is_loaded (void)
  68. {
  69. return grub_loader_loaded;
  70. }
  71. /* Register a preboot hook. */
  72. struct grub_preboot *
  73. grub_loader_register_preboot_hook (grub_err_t (*preboot_func) (int flags),
  74. grub_err_t (*preboot_rest_func) (void),
  75. grub_loader_preboot_hook_prio_t prio)
  76. {
  77. struct grub_preboot *cur, *new_preboot;
  78. if (! preboot_func && ! preboot_rest_func)
  79. return 0;
  80. new_preboot = (struct grub_preboot *)
  81. grub_malloc (sizeof (struct grub_preboot));
  82. if (! new_preboot)
  83. return 0;
  84. new_preboot->preboot_func = preboot_func;
  85. new_preboot->preboot_rest_func = preboot_rest_func;
  86. new_preboot->prio = prio;
  87. for (cur = preboots_head; cur && cur->prio > prio; cur = cur->next);
  88. if (cur)
  89. {
  90. new_preboot->next = cur;
  91. new_preboot->prev = cur->prev;
  92. cur->prev = new_preboot;
  93. }
  94. else
  95. {
  96. new_preboot->next = 0;
  97. new_preboot->prev = preboots_tail;
  98. preboots_tail = new_preboot;
  99. }
  100. if (new_preboot->prev)
  101. new_preboot->prev->next = new_preboot;
  102. else
  103. preboots_head = new_preboot;
  104. return new_preboot;
  105. }
  106. void
  107. grub_loader_unregister_preboot_hook (struct grub_preboot *hnd)
  108. {
  109. struct grub_preboot *preb = hnd;
  110. if (preb->next)
  111. preb->next->prev = preb->prev;
  112. else
  113. preboots_tail = preb->prev;
  114. if (preb->prev)
  115. preb->prev->next = preb->next;
  116. else
  117. preboots_head = preb->next;
  118. grub_free (preb);
  119. }
  120. void
  121. grub_loader_set_ex (grub_err_t (*boot) (void *context),
  122. grub_err_t (*unload) (void *context),
  123. void *context,
  124. int flags)
  125. {
  126. if (grub_loader_loaded && grub_loader_unload_func)
  127. grub_loader_unload_func (grub_loader_context);
  128. grub_loader_boot_func = boot;
  129. grub_loader_unload_func = unload;
  130. grub_loader_context = context;
  131. grub_loader_flags = flags;
  132. grub_loader_loaded = 1;
  133. }
  134. void
  135. grub_loader_set (grub_err_t (*boot) (void),
  136. grub_err_t (*unload) (void),
  137. int flags)
  138. {
  139. grub_loader_set_ex (grub_simple_boot_hook,
  140. grub_simple_unload_hook,
  141. &simple_loader_hooks,
  142. flags);
  143. simple_loader_hooks.boot = boot;
  144. simple_loader_hooks.unload = unload;
  145. }
  146. void
  147. grub_loader_unset(void)
  148. {
  149. if (grub_loader_loaded && grub_loader_unload_func)
  150. grub_loader_unload_func (grub_loader_context);
  151. grub_loader_boot_func = 0;
  152. grub_loader_unload_func = 0;
  153. grub_loader_context = 0;
  154. grub_loader_loaded = 0;
  155. }
  156. grub_err_t
  157. grub_loader_boot (void)
  158. {
  159. grub_err_t err = GRUB_ERR_NONE;
  160. struct grub_preboot *cur;
  161. if (! grub_loader_loaded)
  162. return grub_error (GRUB_ERR_NO_KERNEL,
  163. N_("you need to load the kernel first"));
  164. grub_machine_fini (grub_loader_flags);
  165. for (cur = preboots_head; cur; cur = cur->next)
  166. {
  167. err = cur->preboot_func (grub_loader_flags);
  168. if (err)
  169. {
  170. for (cur = cur->prev; cur; cur = cur->prev)
  171. cur->preboot_rest_func ();
  172. return err;
  173. }
  174. }
  175. err = (grub_loader_boot_func) (grub_loader_context);
  176. for (cur = preboots_tail; cur; cur = cur->prev)
  177. if (! err)
  178. err = cur->preboot_rest_func ();
  179. else
  180. cur->preboot_rest_func ();
  181. return err;
  182. }
  183. /* boot */
  184. static grub_err_t
  185. grub_cmd_boot (struct grub_command *cmd __attribute__ ((unused)),
  186. int argc __attribute__ ((unused)),
  187. char *argv[] __attribute__ ((unused)))
  188. {
  189. return grub_loader_boot ();
  190. }
  191. static grub_command_t cmd_boot;
  192. GRUB_MOD_INIT(boot)
  193. {
  194. cmd_boot =
  195. grub_register_command ("boot", grub_cmd_boot,
  196. 0, N_("Boot an operating system."));
  197. }
  198. GRUB_MOD_FINI(boot)
  199. {
  200. grub_unregister_command (cmd_boot);
  201. }