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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 %04zx data %04zx)\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 = zalloc ( sizeof ( *undirom ) );
  118. if ( ! undirom ) {
  119. DBG ( "Could not allocate UNDI ROM structure\n" );
  120. rc = -ENOMEM;
  121. goto err;
  122. }
  123. DBGC ( undirom, "UNDIROM %p trying expansion ROM at %04x:0000 "
  124. "(%zdkB)\n", undirom, rom_segment, ( rom_len / 1024 ) );
  125. undirom->rom_segment = rom_segment;
  126. /* Check for and parse PXE ROM ID */
  127. pxeromid = romheader.PXEROMID;
  128. if ( ! pxeromid ) {
  129. DBGC ( undirom, "UNDIROM %p has no PXE ROM ID\n", undirom );
  130. rc = -EINVAL;
  131. goto err;
  132. }
  133. if ( pxeromid > rom_len ) {
  134. DBGC ( undirom, "UNDIROM %p PXE ROM ID outside ROM\n",
  135. undirom );
  136. rc = -EINVAL;
  137. goto err;
  138. }
  139. if ( ( rc = undirom_parse_pxeromid ( undirom, pxeromid ) ) != 0 )
  140. goto err;
  141. /* Parse PCIR header, if present */
  142. pcirheader = romheader.PCIRHeader;
  143. if ( pcirheader )
  144. undirom_parse_pcirheader ( undirom, pcirheader );
  145. /* Add to UNDI ROM list and return */
  146. DBGC ( undirom, "UNDIROM %p registered\n", undirom );
  147. list_add ( &undirom->list, &undiroms );
  148. return 0;
  149. err:
  150. free ( undirom );
  151. return rc;
  152. }
  153. /**
  154. * Create UNDI ROMs for all possible expansion ROMs
  155. *
  156. * @ret
  157. */
  158. static void undirom_probe_all_roms ( void ) {
  159. static int probed = 0;
  160. unsigned int rom_segment;
  161. /* Perform probe only once */
  162. if ( probed )
  163. return;
  164. DBG ( "Scanning for PXE expansion ROMs\n" );
  165. /* Scan through expansion ROM region at 2kB intervals */
  166. for ( rom_segment = 0xc000 ; rom_segment < 0x10000 ;
  167. rom_segment += 0x80 ) {
  168. undirom_probe ( rom_segment );
  169. }
  170. probed = 1;
  171. }
  172. /**
  173. * Find UNDI ROM for PCI device
  174. *
  175. * @v vendor_id PCI vendor ID
  176. * @v device_id PCI device ID
  177. * @v rombase ROM base address, or 0 for any
  178. * @ret undirom UNDI ROM, or NULL
  179. */
  180. struct undi_rom * undirom_find_pci ( unsigned int vendor_id,
  181. unsigned int device_id,
  182. unsigned int rombase ) {
  183. struct undi_rom *undirom;
  184. undirom_probe_all_roms();
  185. list_for_each_entry ( undirom, &undiroms, list ) {
  186. if ( undirom->bus_type != PCI_NIC )
  187. continue;
  188. if ( undirom->bus_id.pci.vendor_id != vendor_id )
  189. continue;
  190. if ( undirom->bus_id.pci.device_id != device_id )
  191. continue;
  192. if ( rombase && ( ( undirom->rom_segment << 4 ) != rombase ) )
  193. continue;
  194. DBGC ( undirom, "UNDIROM %p matched PCI %04x:%04x (%08x)\n",
  195. undirom, vendor_id, device_id, rombase );
  196. return undirom;
  197. }
  198. DBG ( "No UNDI ROM matched PCI %04x:%04x (%08x)\n",
  199. vendor_id, device_id, rombase );
  200. return NULL;
  201. }