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

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