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.

undirom.c 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 <stdint.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <pxe.h>
  22. #include <realmode.h>
  23. #include <undirom.h>
  24. /** @file
  25. *
  26. * UNDI expansion ROMs
  27. *
  28. */
  29. /** List of all UNDI ROMs */
  30. static LIST_HEAD ( undiroms );
  31. /**
  32. * Parse PXE ROM ID structure
  33. *
  34. * @v undirom UNDI ROM
  35. * @v pxeromid Offset within ROM to PXE ROM ID structure
  36. * @ret rc Return status code
  37. */
  38. static int undirom_parse_pxeromid ( struct undi_rom *undirom,
  39. unsigned int pxeromid ) {
  40. struct undi_rom_id undi_rom_id;
  41. unsigned int undiloader;
  42. DBGC ( undirom, "UNDIROM %p has PXE ROM ID at %04x:%04x\n", undirom,
  43. undirom->rom_segment, pxeromid );
  44. /* Read PXE ROM ID structure and verify */
  45. copy_from_real ( &undi_rom_id, undirom->rom_segment, pxeromid,
  46. sizeof ( undi_rom_id ) );
  47. if ( undi_rom_id.Signature != UNDI_ROM_ID_SIGNATURE ) {
  48. DBGC ( undirom, "UNDIROM %p has bad PXE ROM ID signature "
  49. "%08lx\n", undirom, undi_rom_id.Signature );
  50. return -EINVAL;
  51. }
  52. /* Check for UNDI loader */
  53. undiloader = undi_rom_id.UNDILoader;
  54. if ( ! undiloader ) {
  55. DBGC ( undirom, "UNDIROM %p has no UNDI loader\n", undirom );
  56. return -EINVAL;
  57. }
  58. /* Fill in UNDI ROM loader fields */
  59. undirom->loader_entry.segment = undirom->rom_segment;
  60. undirom->loader_entry.offset = undiloader;
  61. undirom->code_size = undi_rom_id.CodeSize;
  62. undirom->data_size = undi_rom_id.DataSize;
  63. DBGC ( undirom, "UNDIROM %p has UNDI loader at %04x:%04x "
  64. "(code %04x data %04x)\n", undirom,
  65. undirom->loader_entry.segment, undirom->loader_entry.offset,
  66. undirom->code_size, undirom->data_size );
  67. return 0;
  68. }
  69. /**
  70. * Parse PCI expansion header
  71. *
  72. * @v undirom UNDI ROM
  73. * @v pcirheader Offset within ROM to PCI expansion header
  74. */
  75. static int undirom_parse_pcirheader ( struct undi_rom *undirom,
  76. unsigned int pcirheader ) {
  77. struct pcir_header pcir_header;
  78. DBGC ( undirom, "UNDIROM %p has PCI expansion header at %04x:%04x\n",
  79. undirom, undirom->rom_segment, pcirheader );
  80. /* Read PCI expansion header and verify */
  81. copy_from_real ( &pcir_header, undirom->rom_segment, pcirheader,
  82. sizeof ( pcir_header ) );
  83. if ( pcir_header.signature != PCIR_SIGNATURE ) {
  84. DBGC ( undirom, "UNDIROM %p has bad PCI expansion header "
  85. "signature %08lx\n", undirom, pcir_header.signature );
  86. return -EINVAL;
  87. }
  88. /* Fill in UNDI ROM PCI device fields */
  89. undirom->bus_type = PCI_NIC;
  90. undirom->bus_id.pci.vendor_id = pcir_header.vendor_id;
  91. undirom->bus_id.pci.device_id = pcir_header.device_id;
  92. DBGC ( undirom, "UNDIROM %p is for PCI devices %04x:%04x\n", undirom,
  93. undirom->bus_id.pci.vendor_id, undirom->bus_id.pci.device_id );
  94. return 0;
  95. }
  96. /**
  97. * Probe UNDI ROM
  98. *
  99. * @v rom_segment ROM segment address
  100. * @ret rc Return status code
  101. */
  102. static int undirom_probe ( unsigned int rom_segment ) {
  103. struct undi_rom *undirom = NULL;
  104. struct undi_rom_header romheader;
  105. size_t rom_len;
  106. unsigned int pxeromid;
  107. unsigned int pcirheader;
  108. int rc;
  109. /* Read expansion ROM header and verify */
  110. copy_from_real ( &romheader, rom_segment, 0, sizeof ( romheader ) );
  111. if ( romheader.Signature != ROM_SIGNATURE ) {
  112. rc = -EINVAL;
  113. goto err;
  114. }
  115. rom_len = ( romheader.ROMLength * 512 );
  116. /* Allocate memory for UNDI ROM */
  117. undirom = malloc ( sizeof ( *undirom ) );
  118. if ( ! undirom ) {
  119. DBG ( "Could not allocate UNDI ROM structure\n" );
  120. rc = -ENOMEM;
  121. goto err;
  122. }
  123. memset ( undirom, 0, sizeof ( *undirom ) );
  124. DBGC ( undirom, "UNDIROM %p trying expansion ROM at %04x:0000 "
  125. "(%zdkB)\n", undirom, rom_segment, ( rom_len / 1024 ) );
  126. undirom->rom_segment = rom_segment;
  127. /* Check for and parse PXE ROM ID */
  128. pxeromid = romheader.PXEROMID;
  129. if ( ! pxeromid ) {
  130. DBGC ( undirom, "UNDIROM %p has no PXE ROM ID\n", undirom );
  131. rc = -EINVAL;
  132. goto err;
  133. }
  134. if ( pxeromid > rom_len ) {
  135. DBGC ( undirom, "UNDIROM %p PXE ROM ID outside ROM\n",
  136. undirom );
  137. rc = -EINVAL;
  138. goto err;
  139. }
  140. if ( ( rc = undirom_parse_pxeromid ( undirom, pxeromid ) ) != 0 )
  141. goto err;
  142. /* Parse PCIR header, if present */
  143. pcirheader = romheader.PCIRHeader;
  144. if ( pcirheader )
  145. undirom_parse_pcirheader ( undirom, pcirheader );
  146. /* Add to UNDI ROM list and return */
  147. DBGC ( undirom, "UNDIROM %p registered\n", undirom );
  148. list_add ( &undirom->list, &undiroms );
  149. return 0;
  150. err:
  151. free ( undirom );
  152. return rc;
  153. }
  154. /**
  155. * Create UNDI ROMs for all possible expansion ROMs
  156. *
  157. * @ret
  158. */
  159. static void undirom_probe_all_roms ( void ) {
  160. static int probed = 0;
  161. unsigned int rom_segment;
  162. /* Perform probe only once */
  163. if ( probed )
  164. return;
  165. DBG ( "Scanning for PXE expansion ROMs\n" );
  166. /* Scan through expansion ROM region at 2kB intervals */
  167. for ( rom_segment = 0xc000 ; rom_segment < 0x10000 ;
  168. rom_segment += 0x80 ) {
  169. undirom_probe ( rom_segment );
  170. }
  171. probed = 1;
  172. }
  173. /**
  174. * Find UNDI ROM for PCI device
  175. *
  176. * @v vendor_id PCI vendor ID
  177. * @v device_id PCI device ID
  178. * @v rombase ROM base address, or 0 for any
  179. * @ret undirom UNDI ROM, or NULL
  180. */
  181. struct undi_rom * undirom_find_pci ( unsigned int vendor_id,
  182. unsigned int device_id,
  183. unsigned int rombase ) {
  184. struct undi_rom *undirom;
  185. undirom_probe_all_roms();
  186. list_for_each_entry ( undirom, &undiroms, list ) {
  187. if ( undirom->bus_type != PCI_NIC )
  188. continue;
  189. if ( undirom->bus_id.pci.vendor_id != vendor_id )
  190. continue;
  191. if ( undirom->bus_id.pci.device_id != device_id )
  192. continue;
  193. if ( rombase && ( ( undirom->rom_segment << 4 ) != rombase ) )
  194. continue;
  195. DBGC ( undirom, "UNDIROM %p matched PCI %04x:%04x (%08x)\n",
  196. undirom, vendor_id, device_id, rombase );
  197. return undirom;
  198. }
  199. DBG ( "No UNDI ROM matched PCI %04x:%04x (%08x)\n",
  200. vendor_id, device_id, rombase );
  201. return NULL;
  202. }