boot.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/dl.h>
  20. #include <grub/misc.h>
  21. #include <grub/loader.h>
  22. #include <grub/kernel.h>
  23. #include <grub/mm.h>
  24. #include <grub/command.h>
  25. #include <grub/i18n.h>
  26. GRUB_EXPORT(grub_loader_is_loaded);
  27. GRUB_EXPORT(grub_loader_set);
  28. GRUB_EXPORT(grub_loader_register_preboot_hook);
  29. GRUB_EXPORT(grub_loader_unregister_preboot_hook);
  30. GRUB_EXPORT(grub_loader_unset);
  31. static grub_err_t (*grub_loader_boot_func) (void);
  32. static grub_err_t (*grub_loader_unload_func) (void);
  33. static int grub_loader_noreturn;
  34. struct grub_preboot_t
  35. {
  36. grub_err_t (*preboot_func) (int);
  37. grub_err_t (*preboot_rest_func) (void);
  38. grub_loader_preboot_hook_prio_t prio;
  39. struct grub_preboot_t *next;
  40. struct grub_preboot_t *prev;
  41. };
  42. static int grub_loader_loaded;
  43. static struct grub_preboot_t *preboots_head = 0,
  44. *preboots_tail = 0;
  45. int
  46. grub_loader_is_loaded (void)
  47. {
  48. return grub_loader_loaded;
  49. }
  50. /* Register a preboot hook. */
  51. void *
  52. grub_loader_register_preboot_hook (grub_err_t (*preboot_func) (int noreturn),
  53. grub_err_t (*preboot_rest_func) (void),
  54. grub_loader_preboot_hook_prio_t prio)
  55. {
  56. struct grub_preboot_t *cur, *new_preboot;
  57. if (! preboot_func && ! preboot_rest_func)
  58. return 0;
  59. new_preboot = (struct grub_preboot_t *)
  60. grub_malloc (sizeof (struct grub_preboot_t));
  61. if (! new_preboot)
  62. {
  63. grub_error (GRUB_ERR_OUT_OF_MEMORY, "hook not added");
  64. return 0;
  65. }
  66. new_preboot->preboot_func = preboot_func;
  67. new_preboot->preboot_rest_func = preboot_rest_func;
  68. new_preboot->prio = prio;
  69. for (cur = preboots_head; cur && cur->prio > prio; cur = cur->next);
  70. if (cur)
  71. {
  72. new_preboot->next = cur;
  73. new_preboot->prev = cur->prev;
  74. cur->prev = new_preboot;
  75. }
  76. else
  77. {
  78. new_preboot->next = 0;
  79. new_preboot->prev = preboots_tail;
  80. preboots_tail = new_preboot;
  81. }
  82. if (new_preboot->prev)
  83. new_preboot->prev->next = new_preboot;
  84. else
  85. preboots_head = new_preboot;
  86. return new_preboot;
  87. }
  88. void
  89. grub_loader_unregister_preboot_hook (void *hnd)
  90. {
  91. struct grub_preboot_t *preb = hnd;
  92. if (preb->next)
  93. preb->next->prev = preb->prev;
  94. else
  95. preboots_tail = preb->prev;
  96. if (preb->prev)
  97. preb->prev->next = preb->next;
  98. else
  99. preboots_head = preb->next;
  100. grub_free (preb);
  101. }
  102. void
  103. grub_loader_set (grub_err_t (*boot) (void),
  104. grub_err_t (*unload) (void),
  105. int noreturn)
  106. {
  107. if (grub_loader_loaded && grub_loader_unload_func)
  108. grub_loader_unload_func ();
  109. grub_loader_boot_func = boot;
  110. grub_loader_unload_func = unload;
  111. grub_loader_noreturn = noreturn;
  112. grub_loader_loaded = 1;
  113. }
  114. void
  115. grub_loader_unset(void)
  116. {
  117. if (grub_loader_loaded && grub_loader_unload_func)
  118. grub_loader_unload_func ();
  119. grub_loader_boot_func = 0;
  120. grub_loader_unload_func = 0;
  121. grub_loader_loaded = 0;
  122. }
  123. grub_err_t
  124. grub_loader_boot (void)
  125. {
  126. grub_err_t err = GRUB_ERR_NONE;
  127. struct grub_preboot_t *cur;
  128. if (! grub_loader_loaded)
  129. return grub_error (GRUB_ERR_NO_KERNEL, "no loaded kernel");
  130. if (grub_loader_noreturn)
  131. grub_machine_fini ();
  132. for (cur = preboots_head; cur; cur = cur->next)
  133. {
  134. err = cur->preboot_func (grub_loader_noreturn);
  135. if (err)
  136. {
  137. for (cur = cur->prev; cur; cur = cur->prev)
  138. cur->preboot_rest_func ();
  139. return err;
  140. }
  141. }
  142. err = (grub_loader_boot_func) ();
  143. for (cur = preboots_tail; cur; cur = cur->prev)
  144. if (! err)
  145. err = cur->preboot_rest_func ();
  146. else
  147. cur->preboot_rest_func ();
  148. return err;
  149. }
  150. /* boot */
  151. static grub_err_t
  152. grub_cmd_boot (struct grub_command *cmd __attribute__ ((unused)),
  153. int argc __attribute__ ((unused)),
  154. char *argv[] __attribute__ ((unused)))
  155. {
  156. return grub_loader_boot ();
  157. }
  158. static grub_command_t cmd_boot;
  159. GRUB_MOD_INIT(boot)
  160. {
  161. cmd_boot =
  162. grub_register_command ("boot", grub_cmd_boot,
  163. 0, N_("Boot an operating system."));
  164. }
  165. GRUB_MOD_FINI(boot)
  166. {
  167. grub_unregister_command (cmd_boot);
  168. }