[init] Show startup and shutdown function names in debug messages
[ipxe.git] / 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 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <ipxe/device.h>
27 #include <ipxe/console.h>
28 #include <ipxe/init.h>
29
30 /** @file
31 *
32 * Initialisation, startup and shutdown routines
33 *
34 */
35
36 /** "startup() has been called" flag */
37 static int started = 0;
38
39 /** Colour for debug messages */
40 #define colour table_start ( INIT_FNS )
41
42 /**
43 * Initialise iPXE
44 *
45 * This function performs the one-time-only and irreversible
46 * initialisation steps, such as initialising the heap. It must be
47 * called before (almost) any other function.
48 *
49 * There is, by definition, no counterpart to this function on the
50 * shutdown path.
51 */
52 void initialise ( void ) {
53 struct init_fn *init_fn;
54
55 /* Call registered initialisation functions */
56 for_each_table_entry ( init_fn, INIT_FNS )
57 init_fn->initialise ();
58 }
59
60 /**
61 * Start up iPXE
62 *
63 * This function performs the repeatable initialisation steps, such as
64 * probing devices. You may call startup() and shutdown() multiple
65 * times (as is done via the PXE API when PXENV_START_UNDI is used).
66 */
67 void startup ( void ) {
68 struct startup_fn *startup_fn;
69
70 if ( started )
71 return;
72
73 /* Call registered startup functions */
74 for_each_table_entry ( startup_fn, STARTUP_FNS ) {
75 if ( startup_fn->startup ) {
76 DBGC ( colour, "INIT startup %s...\n",
77 startup_fn->name );
78 startup_fn->startup();
79 }
80 }
81
82 started = 1;
83 DBGC ( colour, "INIT startup complete\n" );
84 }
85
86 /**
87 * Shut down iPXE
88 *
89 * @v flags Shutdown behaviour flags
90 *
91 * This function reverses the actions of startup(), and leaves iPXE in
92 * a state ready to be removed from memory. You may call startup()
93 * again after calling shutdown().
94 *
95 * Call this function only once, before either exiting main() or
96 * starting up a non-returnable image.
97 */
98 void shutdown ( int flags ) {
99 struct startup_fn *startup_fn;
100
101 if ( ! started )
102 return;
103
104 /* Call registered shutdown functions (in reverse order) */
105 for_each_table_entry_reverse ( startup_fn, STARTUP_FNS ) {
106 if ( startup_fn->shutdown ) {
107 DBGC ( colour, "INIT shutdown %s...\n",
108 startup_fn->name );
109 startup_fn->shutdown ( flags );
110 }
111 }
112
113 /* Reset console */
114 console_reset();
115
116 started = 0;
117 DBGC ( colour, "INIT shutdown complete\n" );
118 }