boot.c 4.4 KB

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