Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / commands / boot.c
blobbbca81e947641f00e7cdb019ef771e19389aeef4
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.
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.
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.
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/>.
20 #include <grub/normal.h>
21 #include <grub/dl.h>
22 #include <grub/misc.h>
23 #include <grub/loader.h>
24 #include <grub/kernel.h>
25 #include <grub/mm.h>
26 #include <grub/i18n.h>
28 GRUB_MOD_LICENSE ("GPLv3+");
30 static grub_err_t (*grub_loader_boot_func) (void);
31 static grub_err_t (*grub_loader_unload_func) (void);
32 static int grub_loader_flags;
34 struct grub_preboot
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 *next;
40 struct grub_preboot *prev;
43 static int grub_loader_loaded;
44 static struct grub_preboot *preboots_head = 0,
45 *preboots_tail = 0;
47 int
48 grub_loader_is_loaded (void)
50 return grub_loader_loaded;
53 /* Register a preboot hook. */
54 struct grub_preboot *
55 grub_loader_register_preboot_hook (grub_err_t (*preboot_func) (int flags),
56 grub_err_t (*preboot_rest_func) (void),
57 grub_loader_preboot_hook_prio_t prio)
59 struct grub_preboot *cur, *new_preboot;
61 if (! preboot_func && ! preboot_rest_func)
62 return 0;
64 new_preboot = (struct grub_preboot *)
65 grub_malloc (sizeof (struct grub_preboot));
66 if (! new_preboot)
67 return 0;
69 new_preboot->preboot_func = preboot_func;
70 new_preboot->preboot_rest_func = preboot_rest_func;
71 new_preboot->prio = prio;
73 for (cur = preboots_head; cur && cur->prio > prio; cur = cur->next);
75 if (cur)
77 new_preboot->next = cur;
78 new_preboot->prev = cur->prev;
79 cur->prev = new_preboot;
81 else
83 new_preboot->next = 0;
84 new_preboot->prev = preboots_tail;
85 preboots_tail = new_preboot;
87 if (new_preboot->prev)
88 new_preboot->prev->next = new_preboot;
89 else
90 preboots_head = new_preboot;
92 return new_preboot;
95 void
96 grub_loader_unregister_preboot_hook (struct grub_preboot *hnd)
98 struct grub_preboot *preb = hnd;
100 if (preb->next)
101 preb->next->prev = preb->prev;
102 else
103 preboots_tail = preb->prev;
104 if (preb->prev)
105 preb->prev->next = preb->next;
106 else
107 preboots_head = preb->next;
109 grub_free (preb);
112 void
113 grub_loader_set (grub_err_t (*boot) (void),
114 grub_err_t (*unload) (void),
115 int flags)
117 if (grub_loader_loaded && grub_loader_unload_func)
118 grub_loader_unload_func ();
120 grub_loader_boot_func = boot;
121 grub_loader_unload_func = unload;
122 grub_loader_flags = flags;
124 grub_loader_loaded = 1;
127 void
128 grub_loader_unset(void)
130 if (grub_loader_loaded && grub_loader_unload_func)
131 grub_loader_unload_func ();
133 grub_loader_boot_func = 0;
134 grub_loader_unload_func = 0;
136 grub_loader_loaded = 0;
139 grub_err_t
140 grub_loader_boot (void)
142 grub_err_t err = GRUB_ERR_NONE;
143 struct grub_preboot *cur;
145 if (! grub_loader_loaded)
146 return grub_error (GRUB_ERR_NO_KERNEL,
147 N_("you need to load the kernel first"));
149 grub_machine_fini (grub_loader_flags);
151 for (cur = preboots_head; cur; cur = cur->next)
153 err = cur->preboot_func (grub_loader_flags);
154 if (err)
156 for (cur = cur->prev; cur; cur = cur->prev)
157 cur->preboot_rest_func ();
158 return err;
161 err = (grub_loader_boot_func) ();
163 for (cur = preboots_tail; cur; cur = cur->prev)
164 if (! err)
165 err = cur->preboot_rest_func ();
166 else
167 cur->preboot_rest_func ();
169 return err;
172 /* boot */
173 static grub_err_t
174 grub_cmd_boot (struct grub_command *cmd __attribute__ ((unused)),
175 int argc __attribute__ ((unused)),
176 char *argv[] __attribute__ ((unused)))
178 return grub_loader_boot ();
183 static grub_command_t cmd_boot;
185 GRUB_MOD_INIT(boot)
187 cmd_boot =
188 grub_register_command ("boot", grub_cmd_boot,
189 0, N_("Boot an operating system."));
192 GRUB_MOD_FINI(boot)
194 grub_unregister_command (cmd_boot);