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 /**
00040  * Initialise iPXE
00041  *
00042  * This function performs the one-time-only and irreversible
00043  * initialisation steps, such as initialising the heap.  It must be
00044  * called before (almost) any other function.
00045  *
00046  * There is, by definition, no counterpart to this function on the
00047  * shutdown path.
00048  */
00049 void initialise ( void ) {
00050         struct init_fn *init_fn;
00051 
00052         /* Call registered initialisation functions */
00053         for_each_table_entry ( init_fn, INIT_FNS )
00054                 init_fn->initialise ();
00055 }
00056 
00057 /**
00058  * Start up iPXE
00059  *
00060  * This function performs the repeatable initialisation steps, such as
00061  * probing devices.  You may call startup() and shutdown() multiple
00062  * times (as is done via the PXE API when PXENV_START_UNDI is used).
00063  */
00064 void startup ( void ) {
00065         struct startup_fn *startup_fn;
00066 
00067         if ( started )
00068                 return;
00069 
00070         /* Call registered startup functions */
00071         for_each_table_entry ( startup_fn, STARTUP_FNS ) {
00072                 if ( startup_fn->startup )
00073                         startup_fn->startup();
00074         }
00075 
00076         started = 1;
00077 }
00078 
00079 /**
00080  * Shut down iPXE
00081  *
00082  * @v flags             Shutdown behaviour flags
00083  *
00084  * This function reverses the actions of startup(), and leaves iPXE in
00085  * a state ready to be removed from memory.  You may call startup()
00086  * again after calling shutdown().
00087  *
00088  * Call this function only once, before either exiting main() or
00089  * starting up a non-returnable image.
00090  */
00091 void shutdown ( int flags ) {
00092         struct startup_fn *startup_fn;
00093 
00094         if ( ! started )
00095                 return;
00096 
00097         /* Call registered shutdown functions (in reverse order) */
00098         for_each_table_entry_reverse ( startup_fn, STARTUP_FNS ) {
00099                 if ( startup_fn->shutdown )
00100                         startup_fn->shutdown ( flags );
00101         }
00102 
00103         /* Reset console */
00104         console_reset();
00105 
00106         started = 0;
00107 }