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.4KB

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