You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

init.c 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <gpxe/device.h>
  19. #include <gpxe/init.h>
  20. /** @file
  21. *
  22. * Initialisation, startup and shutdown routines
  23. *
  24. */
  25. /** Registered initialisation functions */
  26. static struct init_fn init_fns[0]
  27. __table_start ( struct init_fn, init_fns );
  28. static struct init_fn init_fns_end[0]
  29. __table_end ( struct init_fn, init_fns );
  30. /** Registered startup/shutdown functions */
  31. static struct startup_fn startup_fns[0]
  32. __table_start ( struct startup_fn, startup_fns );
  33. static struct startup_fn startup_fns_end[0]
  34. __table_end ( struct startup_fn, startup_fns );
  35. /** "startup() has been called" flag */
  36. static int started = 0;
  37. /**
  38. * Initialise gPXE
  39. *
  40. * This function performs the one-time-only and irreversible
  41. * initialisation steps, such as initialising the heap. It must be
  42. * called before (almost) any other function.
  43. *
  44. * There is, by definition, no counterpart to this function on the
  45. * shutdown path.
  46. */
  47. void initialise ( void ) {
  48. struct init_fn *init_fn;
  49. /* Call registered initialisation functions */
  50. for ( init_fn = init_fns ; init_fn < init_fns_end ; init_fn++ ) {
  51. init_fn->initialise ();
  52. }
  53. }
  54. /**
  55. * Start up gPXE
  56. *
  57. * This function performs the repeatable initialisation steps, such as
  58. * probing devices. You may call startup() and shutdown() multiple
  59. * times (as is done via the PXE API when PXENV_START_UNDI is used).
  60. */
  61. void startup ( void ) {
  62. struct startup_fn *startup_fn;
  63. if ( started )
  64. return;
  65. /* Call registered startup functions */
  66. for ( startup_fn = startup_fns ; startup_fn < startup_fns_end ;
  67. startup_fn++ ) {
  68. if ( startup_fn->startup )
  69. startup_fn->startup();
  70. }
  71. started = 1;
  72. }
  73. /**
  74. * Shut down gPXE
  75. *
  76. * @v flags Shutdown behaviour flags
  77. *
  78. * This function reverses the actions of startup(), and leaves gPXE in
  79. * a state ready to be removed from memory. You may call startup()
  80. * again after calling shutdown().
  81. * Call this function only once, before either exiting main() or
  82. * starting up a non-returnable image.
  83. */
  84. void shutdown ( int flags ) {
  85. struct startup_fn *startup_fn;
  86. if ( ! started )
  87. return;
  88. /* Call registered shutdown functions (in reverse order) */
  89. for ( startup_fn = startup_fns_end - 1 ; startup_fn >= startup_fns ;
  90. startup_fn-- ) {
  91. if ( startup_fn->shutdown )
  92. startup_fn->shutdown ( flags );
  93. }
  94. started = 0;
  95. }