Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / core / init.c
1 /*
2  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <ipxe/device.h>
23 #include <ipxe/console.h>
24 #include <ipxe/init.h>
25
26 /** @file
27  *
28  * Initialisation, startup and shutdown routines
29  *
30  */
31
32 /** "startup() has been called" flag */
33 static int started = 0;
34
35 /**
36  * Initialise iPXE
37  *
38  * This function performs the one-time-only and irreversible
39  * initialisation steps, such as initialising the heap.  It must be
40  * called before (almost) any other function.
41  *
42  * There is, by definition, no counterpart to this function on the
43  * shutdown path.
44  */
45 void initialise ( void ) {
46         struct init_fn *init_fn;
47
48         /* Call registered initialisation functions */
49         for_each_table_entry ( init_fn, INIT_FNS )
50                 init_fn->initialise ();
51 }
52
53 /**
54  * Start up iPXE
55  *
56  * This function performs the repeatable initialisation steps, such as
57  * probing devices.  You may call startup() and shutdown() multiple
58  * times (as is done via the PXE API when PXENV_START_UNDI is used).
59  */
60 void startup ( void ) {
61         struct startup_fn *startup_fn;
62
63         if ( started )
64                 return;
65
66         /* Call registered startup functions */
67         for_each_table_entry ( startup_fn, STARTUP_FNS ) {
68                 if ( startup_fn->startup )
69                         startup_fn->startup();
70         }
71
72         started = 1;
73 }
74
75 /**
76  * Shut down iPXE
77  *
78  * @v flags             Shutdown behaviour flags
79  *
80  * This function reverses the actions of startup(), and leaves iPXE in
81  * a state ready to be removed from memory.  You may call startup()
82  * again after calling shutdown().
83  *
84  * Call this function only once, before either exiting main() or
85  * starting up a non-returnable image.
86  */
87 void shutdown ( int flags ) {
88         struct startup_fn *startup_fn;
89
90         if ( ! started )
91                 return;
92
93         /* Call registered shutdown functions (in reverse order) */
94         for_each_table_entry_reverse ( startup_fn, STARTUP_FNS ) {
95                 if ( startup_fn->shutdown )
96                         startup_fn->shutdown ( flags );
97         }
98
99         /* Reset console */
100         console_reset();
101
102         started = 0;
103 }