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.

undiload.c 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <pxe.h>
  28. #include <realmode.h>
  29. #include <bios.h>
  30. #include <pnpbios.h>
  31. #include <basemem.h>
  32. #include <ipxe/pci.h>
  33. #include <undi.h>
  34. #include <undirom.h>
  35. #include <undiload.h>
  36. /** @file
  37. *
  38. * UNDI load/unload
  39. *
  40. */
  41. /* Disambiguate the various error causes */
  42. #define EINFO_EUNDILOAD \
  43. __einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
  44. "UNDI loader error" )
  45. #define EUNDILOAD( status ) EPLATFORM ( EINFO_EUNDILOAD, status )
  46. /** Parameter block for calling UNDI loader */
  47. static struct s_UNDI_LOADER __bss16 ( undi_loader );
  48. #define undi_loader __use_data16 ( undi_loader )
  49. /** UNDI loader entry point */
  50. static SEGOFF16_t __bss16 ( undi_loader_entry );
  51. #define undi_loader_entry __use_data16 ( undi_loader_entry )
  52. /**
  53. * Call UNDI loader to create a pixie
  54. *
  55. * @v undi UNDI device
  56. * @v undirom UNDI ROM
  57. * @ret rc Return status code
  58. */
  59. int undi_load ( struct undi_device *undi, struct undi_rom *undirom ) {
  60. struct s_PXE ppxe;
  61. unsigned int fbms_seg;
  62. uint16_t exit;
  63. int rc;
  64. /* Only one UNDI instance may be loaded at any given time */
  65. if ( undi_loader_entry.segment ) {
  66. DBG ( "UNDI %p cannot load multiple instances\n", undi );
  67. rc = -EBUSY;
  68. goto err_multiple;
  69. }
  70. /* Set up START_UNDI parameters */
  71. memset ( &undi_loader, 0, sizeof ( undi_loader ) );
  72. undi_loader.AX = undi->pci_busdevfn;
  73. undi_loader.BX = undi->isapnp_csn;
  74. undi_loader.DX = undi->isapnp_read_port;
  75. undi_loader.ES = BIOS_SEG;
  76. undi_loader.DI = find_pnp_bios();
  77. /* Allocate base memory for PXE stack */
  78. undi->restore_fbms = get_fbms();
  79. fbms_seg = ( undi->restore_fbms << 6 );
  80. fbms_seg -= ( ( undirom->code_size + 0x0f ) >> 4 );
  81. undi_loader.UNDI_CS = fbms_seg;
  82. fbms_seg -= ( ( undirom->data_size + 0x0f ) >> 4 );
  83. undi_loader.UNDI_DS = fbms_seg;
  84. undi->fbms = ( fbms_seg >> 6 );
  85. set_fbms ( undi->fbms );
  86. DBGC ( undi, "UNDI %p allocated [%d,%d) kB of base memory\n",
  87. undi, undi->fbms, undi->restore_fbms );
  88. /* Debug info */
  89. DBGC ( undi, "UNDI %p loading ROM %p to CS %04x:%04zx DS %04x:%04zx "
  90. "for ", undi, undirom, undi_loader.UNDI_CS, undirom->code_size,
  91. undi_loader.UNDI_DS, undirom->data_size );
  92. if ( undi->pci_busdevfn != UNDI_NO_PCI_BUSDEVFN ) {
  93. unsigned int bus = ( undi->pci_busdevfn >> 8 );
  94. unsigned int devfn = ( undi->pci_busdevfn & 0xff );
  95. DBGC ( undi, "PCI %02x:%02x.%x\n",
  96. bus, PCI_SLOT ( devfn ), PCI_FUNC ( devfn ) );
  97. }
  98. if ( undi->isapnp_csn != UNDI_NO_ISAPNP_CSN ) {
  99. DBGC ( undi, "ISAPnP(%04x) CSN %04x\n",
  100. undi->isapnp_read_port, undi->isapnp_csn );
  101. }
  102. /* Call loader */
  103. undi_loader_entry = undirom->loader_entry;
  104. __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t" /* gcc bug */
  105. "pushw %%ds\n\t"
  106. "pushw %%ax\n\t"
  107. "lcall *undi_loader_entry\n\t"
  108. "popl %%ebp\n\t" /* discard */
  109. "popl %%ebp\n\t" /* gcc bug */ )
  110. : "=a" ( exit )
  111. : "a" ( __from_data16 ( &undi_loader ) )
  112. : "ebx", "ecx", "edx", "esi", "edi" );
  113. if ( exit != PXENV_EXIT_SUCCESS ) {
  114. rc = -EUNDILOAD ( undi_loader.Status );
  115. DBGC ( undi, "UNDI %p loader failed: %s\n",
  116. undi, strerror ( rc ) );
  117. goto err_loader;
  118. }
  119. /* Populate PXE device structure */
  120. undi->pxenv = undi_loader.PXENVptr;
  121. undi->ppxe = undi_loader.PXEptr;
  122. copy_from_real ( &ppxe, undi->ppxe.segment, undi->ppxe.offset,
  123. sizeof ( ppxe ) );
  124. undi->entry = ppxe.EntryPointSP;
  125. DBGC ( undi, "UNDI %p loaded PXENV+ %04x:%04x !PXE %04x:%04x "
  126. "entry %04x:%04x\n", undi, undi->pxenv.segment,
  127. undi->pxenv.offset, undi->ppxe.segment, undi->ppxe.offset,
  128. undi->entry.segment, undi->entry.offset );
  129. return 0;
  130. err_loader:
  131. set_fbms ( undi->restore_fbms );
  132. memset ( &undi_loader_entry, 0, sizeof ( undi_loader_entry ) );
  133. err_multiple:
  134. return rc;
  135. }
  136. /**
  137. * Unload a pixie
  138. *
  139. * @v undi UNDI device
  140. * @ret rc Return status code
  141. *
  142. * Erases the PXENV+ and !PXE signatures, and frees the used base
  143. * memory (if possible).
  144. */
  145. int undi_unload ( struct undi_device *undi ) {
  146. static uint32_t dead = 0xdeaddead;
  147. DBGC ( undi, "UNDI %p unloading\n", undi );
  148. /* Clear entry point */
  149. memset ( &undi_loader_entry, 0, sizeof ( undi_loader_entry ) );
  150. /* Erase signatures */
  151. if ( undi->pxenv.segment )
  152. put_real ( dead, undi->pxenv.segment, undi->pxenv.offset );
  153. if ( undi->ppxe.segment )
  154. put_real ( dead, undi->ppxe.segment, undi->ppxe.offset );
  155. /* Free base memory, if possible */
  156. if ( undi->fbms == get_fbms() ) {
  157. DBGC ( undi, "UNDI %p freeing [%d,%d) kB of base memory\n",
  158. undi, undi->fbms, undi->restore_fbms );
  159. set_fbms ( undi->restore_fbms );
  160. return 0;
  161. } else {
  162. DBGC ( undi, "UNDI %p leaking [%d,%d) kB of base memory\n",
  163. undi, undi->fbms, undi->restore_fbms );
  164. return -EBUSY;
  165. }
  166. }