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

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