iPXE
init.c
Go to the documentation of this file.
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 */
57  DBGC ( colour, "INIT initialising %s...\n", init_fn->name );
58  init_fn->initialise ();
59  }
60 }
61 
62 /**
63  * Start up iPXE
64  *
65  * This function performs the repeatable initialisation steps, such as
66  * probing devices. You may call startup() and shutdown() multiple
67  * times (as is done via the PXE API when PXENV_START_UNDI is used).
68  */
69 void startup ( void ) {
70  struct startup_fn *startup_fn;
71 
72  if ( started )
73  return;
74 
75  /* Call registered startup functions */
77  if ( startup_fn->startup ) {
78  DBGC ( colour, "INIT startup %s...\n",
79  startup_fn->name );
81  }
82  }
83 
84  started = 1;
85  DBGC ( colour, "INIT startup complete\n" );
86 }
87 
88 /**
89  * Shut down iPXE
90  *
91  * @v flags Shutdown behaviour flags
92  *
93  * This function reverses the actions of startup(), and leaves iPXE in
94  * a state ready to be removed from memory. You may call startup()
95  * again after calling shutdown().
96  *
97  * Call this function only once, before either exiting main() or
98  * starting up a non-returnable image.
99  */
100 void shutdown ( int flags ) {
101  struct startup_fn *startup_fn;
102 
103  if ( ! started )
104  return;
105 
106  /* Call registered shutdown functions (in reverse order) */
108  if ( startup_fn->shutdown ) {
109  DBGC ( colour, "INIT shutdown %s...\n",
110  startup_fn->name );
112  }
113  }
114 
115  /* Reset console */
116  console_reset();
117 
118  started = 0;
119  DBGC ( colour, "INIT shutdown complete\n" );
120 }
#define INIT_FNS
Initialisation function table.
Definition: init.h:20
void(* initialise)(void)
Definition: init.h:16
#define DBGC(...)
Definition: compiler.h:505
#define for_each_table_entry_reverse(pointer, table)
Iterate through all entries within a linker table in reverse order.
Definition: tables.h:440
const char * name
Definition: init.h:43
void initialise(void)
Initialise iPXE.
Definition: init.c:52
A startup/shutdown function.
Definition: init.h:42
#define colour
Colour for debug messages.
Definition: init.c:40
static int started
"startup() has been called" flag
Definition: init.c:37
An initialisation function.
Definition: init.h:14
const char * name
Definition: init.h:15
User interaction.
uint8_t flags
Flags.
Definition: ena.h:18
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:385
static void console_reset(void)
Reset console.
Definition: console.h:214
void(* startup)(void)
Definition: init.h:44
void(* shutdown)(int booting)
Definition: init.h:45
Device model.
void shutdown(int flags)
Shut down iPXE.
Definition: init.c:100
#define STARTUP_FNS
Startup/shutdown function table.
Definition: init.h:49
void startup(void)
Start up iPXE.
Definition: init.c:69