boot.c 4.5 KB

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