iPXE
init.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <ipxe/device.h>
00027 #include <ipxe/console.h>
00028 #include <ipxe/init.h>
00029 
00030 /** @file
00031  *
00032  * Initialisation, startup and shutdown routines
00033  *
00034  */
00035 
00036 /** "startup() has been called" flag */
00037 static int started = 0;
00038 
00039 /** Colour for debug messages */
00040 #define colour table_start ( INIT_FNS )
00041 
00042 /**
00043  * Initialise iPXE
00044  *
00045  * This function performs the one-time-only and irreversible
00046  * initialisation steps, such as initialising the heap.  It must be
00047  * called before (almost) any other function.
00048  *
00049  * There is, by definition, no counterpart to this function on the
00050  * shutdown path.
00051  */
00052 void initialise ( void ) {
00053         struct init_fn *init_fn;
00054 
00055         /* Call registered initialisation functions */
00056         for_each_table_entry ( init_fn, INIT_FNS )
00057                 init_fn->initialise ();
00058 }
00059 
00060 /**
00061  * Start up iPXE
00062  *
00063  * This function performs the repeatable initialisation steps, such as
00064  * probing devices.  You may call startup() and shutdown() multiple
00065  * times (as is done via the PXE API when PXENV_START_UNDI is used).
00066  */
00067 void startup ( void ) {
00068         struct startup_fn *startup_fn;
00069 
00070         if ( started )
00071                 return;
00072 
00073         /* Call registered startup functions */
00074         for_each_table_entry ( startup_fn, STARTUP_FNS ) {
00075                 if ( startup_fn->startup ) {
00076                         DBGC ( colour, "INIT startup %s...\n",
00077                                startup_fn->name );
00078                         startup_fn->startup();
00079                 }
00080         }
00081 
00082         started = 1;
00083         DBGC ( colour, "INIT startup complete\n" );
00084 }
00085 
00086 /**
00087  * Shut down iPXE
00088  *
00089  * @v flags             Shutdown behaviour flags
00090  *
00091  * This function reverses the actions of startup(), and leaves iPXE in
00092  * a state ready to be removed from memory.  You may call startup()
00093  * again after calling shutdown().
00094  *
00095  * Call this function only once, before either exiting main() or
00096  * starting up a non-returnable image.
00097  */
00098 void shutdown ( int flags ) {
00099         struct startup_fn *startup_fn;
00100 
00101         if ( ! started )
00102                 return;
00103 
00104         /* Call registered shutdown functions (in reverse order) */
00105         for_each_table_entry_reverse ( startup_fn, STARTUP_FNS ) {
00106                 if ( startup_fn->shutdown ) {
00107                         DBGC ( colour, "INIT shutdown %s...\n",
00108                                startup_fn->name );
00109                         startup_fn->shutdown ( flags );
00110                 }
00111         }
00112 
00113         /* Reset console */
00114         console_reset();
00115 
00116         started = 0;
00117         DBGC ( colour, "INIT shutdown complete\n" );
00118 }