Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 <undirom.h>
  25. /** @file
  26. *
  27. * UNDI expansion ROMs
  28. *
  29. */
  30. /** List of all UNDI ROMs */
  31. static LIST_HEAD ( undiroms );
  32. /**
  33. * Parse PXE ROM ID structure
  34. *
  35. * @v undirom UNDI ROM
  36. * @v pxeromid Offset within ROM to PXE ROM ID structure
  37. * @ret rc Return status code
  38. */
  39. static int undirom_parse_pxeromid ( struct undi_rom *undirom,
  40. unsigned int pxeromid ) {
  41. struct undi_rom_id undi_rom_id;
  42. unsigned int undiloader;
  43. DBGC ( undirom, "UNDIROM %p has PXE ROM ID at %04x:%04x\n", undirom,
  44. undirom->rom_segment, pxeromid );
  45. /* Read PXE ROM ID structure and verify */
  46. copy_from_real ( &undi_rom_id, undirom->rom_segment, pxeromid,
  47. sizeof ( undi_rom_id ) );
  48. if ( undi_rom_id.Signature != UNDI_ROM_ID_SIGNATURE ) {
  49. DBGC ( undirom, "UNDIROM %p has bad PXE ROM ID signature "
  50. "%08x\n", undirom, undi_rom_id.Signature );
  51. return -EINVAL;
  52. }
  53. /* Check for UNDI loader */
  54. undiloader = undi_rom_id.UNDILoader;
  55. if ( ! undiloader ) {
  56. DBGC ( undirom, "UNDIROM %p has no UNDI loader\n", undirom );
  57. return -EINVAL;
  58. }
  59. /* Fill in UNDI ROM loader fields */
  60. undirom->loader_entry.segment = undirom->rom_segment;
  61. undirom->loader_entry.offset = undiloader;
  62. undirom->code_size = undi_rom_id.CodeSize;
  63. undirom->data_size = undi_rom_id.DataSize;
  64. DBGC ( undirom, "UNDIROM %p has UNDI loader at %04x:%04x "
  65. "(code %04zx data %04zx)\n", undirom,
  66. undirom->loader_entry.segment, undirom->loader_entry.offset,
  67. undirom->code_size, undirom->data_size );
  68. return 0;
  69. }
  70. /**
  71. * Parse PCI expansion header
  72. *
  73. * @v undirom UNDI ROM
  74. * @v pcirheader Offset within ROM to PCI expansion header
  75. */
  76. static int undirom_parse_pcirheader ( struct undi_rom *undirom,
  77. unsigned int pcirheader ) {
  78. struct pcir_header pcir_header;
  79. DBGC ( undirom, "UNDIROM %p has PCI expansion header at %04x:%04x\n",
  80. undirom, undirom->rom_segment, pcirheader );
  81. /* Read PCI expansion header and verify */
  82. copy_from_real ( &pcir_header, undirom->rom_segment, pcirheader,
  83. sizeof ( pcir_header ) );
  84. if ( pcir_header.signature != PCIR_SIGNATURE ) {
  85. DBGC ( undirom, "UNDIROM %p has bad PCI expansion header "
  86. "signature %08x\n", undirom, pcir_header.signature );
  87. return -EINVAL;
  88. }
  89. /* Fill in UNDI ROM PCI device fields */
  90. undirom->bus_type = PCI_NIC;
  91. undirom->bus_id.pci.vendor_id = pcir_header.vendor_id;
  92. undirom->bus_id.pci.device_id = pcir_header.device_id;
  93. DBGC ( undirom, "UNDIROM %p is for PCI devices %04x:%04x\n", undirom,
  94. undirom->bus_id.pci.vendor_id, undirom->bus_id.pci.device_id );
  95. return 0;
  96. }
  97. /**
  98. * Probe UNDI ROM
  99. *
  100. * @v rom_segment ROM segment address
  101. * @ret rc Return status code
  102. */
  103. static int undirom_probe ( unsigned int rom_segment ) {
  104. struct undi_rom *undirom = NULL;
  105. struct undi_rom_header romheader;
  106. size_t rom_len;
  107. unsigned int pxeromid;
  108. unsigned int pcirheader;
  109. int rc;
  110. /* Read expansion ROM header and verify */
  111. copy_from_real ( &romheader, rom_segment, 0, sizeof ( romheader ) );
  112. if ( romheader.Signature != ROM_SIGNATURE ) {
  113. rc = -EINVAL;
  114. goto err;
  115. }
  116. rom_len = ( romheader.ROMLength * 512 );
  117. /* Allocate memory for UNDI ROM */
  118. undirom = zalloc ( sizeof ( *undirom ) );
  119. if ( ! undirom ) {
  120. DBG ( "Could not allocate UNDI ROM structure\n" );
  121. rc = -ENOMEM;
  122. goto err;
  123. }
  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 512 byte intervals */
  167. for ( rom_segment = 0xc000 ; rom_segment < 0x10000 ;
  168. rom_segment += 0x20 ) {
  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. }