probe_roms.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include <linux/sched.h>
  2. #include <linux/mm.h>
  3. #include <linux/uaccess.h>
  4. #include <linux/mmzone.h>
  5. #include <linux/ioport.h>
  6. #include <linux/seq_file.h>
  7. #include <linux/console.h>
  8. #include <linux/init.h>
  9. #include <linux/edd.h>
  10. #include <linux/dmi.h>
  11. #include <linux/pfn.h>
  12. #include <linux/pci.h>
  13. #include <asm/pci-direct.h>
  14. #include <asm/e820.h>
  15. #include <asm/mmzone.h>
  16. #include <asm/setup.h>
  17. #include <asm/sections.h>
  18. #include <asm/io.h>
  19. #include <asm/setup_arch.h>
  20. static struct resource system_rom_resource = {
  21. .name = "System ROM",
  22. .start = 0xf0000,
  23. .end = 0xfffff,
  24. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  25. };
  26. static struct resource extension_rom_resource = {
  27. .name = "Extension ROM",
  28. .start = 0xe0000,
  29. .end = 0xeffff,
  30. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  31. };
  32. static struct resource adapter_rom_resources[] = { {
  33. .name = "Adapter ROM",
  34. .start = 0xc8000,
  35. .end = 0,
  36. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  37. }, {
  38. .name = "Adapter ROM",
  39. .start = 0,
  40. .end = 0,
  41. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  42. }, {
  43. .name = "Adapter ROM",
  44. .start = 0,
  45. .end = 0,
  46. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  47. }, {
  48. .name = "Adapter ROM",
  49. .start = 0,
  50. .end = 0,
  51. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  52. }, {
  53. .name = "Adapter ROM",
  54. .start = 0,
  55. .end = 0,
  56. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  57. }, {
  58. .name = "Adapter ROM",
  59. .start = 0,
  60. .end = 0,
  61. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  62. } };
  63. static struct resource video_rom_resource = {
  64. .name = "Video ROM",
  65. .start = 0xc0000,
  66. .end = 0xc7fff,
  67. .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
  68. };
  69. /* does this oprom support the given pci device, or any of the devices
  70. * that the driver supports?
  71. */
  72. static bool match_id(struct pci_dev *pdev, unsigned short vendor, unsigned short device)
  73. {
  74. struct pci_driver *drv = pdev->driver;
  75. const struct pci_device_id *id;
  76. if (pdev->vendor == vendor && pdev->device == device)
  77. return true;
  78. for (id = drv ? drv->id_table : NULL; id && id->vendor; id++)
  79. if (id->vendor == vendor && id->device == device)
  80. break;
  81. return id && id->vendor;
  82. }
  83. static bool probe_list(struct pci_dev *pdev, unsigned short vendor,
  84. const unsigned char *rom_list)
  85. {
  86. unsigned short device;
  87. do {
  88. if (probe_kernel_address(rom_list, device) != 0)
  89. device = 0;
  90. if (device && match_id(pdev, vendor, device))
  91. break;
  92. rom_list += 2;
  93. } while (device);
  94. return !!device;
  95. }
  96. static struct resource *find_oprom(struct pci_dev *pdev)
  97. {
  98. struct resource *oprom = NULL;
  99. int i;
  100. for (i = 0; i < ARRAY_SIZE(adapter_rom_resources); i++) {
  101. struct resource *res = &adapter_rom_resources[i];
  102. unsigned short offset, vendor, device, list, rev;
  103. const unsigned char *rom;
  104. if (res->end == 0)
  105. break;
  106. rom = isa_bus_to_virt(res->start);
  107. if (probe_kernel_address(rom + 0x18, offset) != 0)
  108. continue;
  109. if (probe_kernel_address(rom + offset + 0x4, vendor) != 0)
  110. continue;
  111. if (probe_kernel_address(rom + offset + 0x6, device) != 0)
  112. continue;
  113. if (match_id(pdev, vendor, device)) {
  114. oprom = res;
  115. break;
  116. }
  117. if (probe_kernel_address(rom + offset + 0x8, list) == 0 &&
  118. probe_kernel_address(rom + offset + 0xc, rev) == 0 &&
  119. rev >= 3 && list &&
  120. probe_list(pdev, vendor, rom + offset + list)) {
  121. oprom = res;
  122. break;
  123. }
  124. }
  125. return oprom;
  126. }
  127. void *pci_map_biosrom(struct pci_dev *pdev)
  128. {
  129. struct resource *oprom = find_oprom(pdev);
  130. if (!oprom)
  131. return NULL;
  132. return ioremap(oprom->start, resource_size(oprom));
  133. }
  134. EXPORT_SYMBOL(pci_map_biosrom);
  135. void pci_unmap_biosrom(void __iomem *image)
  136. {
  137. iounmap(image);
  138. }
  139. EXPORT_SYMBOL(pci_unmap_biosrom);
  140. size_t pci_biosrom_size(struct pci_dev *pdev)
  141. {
  142. struct resource *oprom = find_oprom(pdev);
  143. return oprom ? resource_size(oprom) : 0;
  144. }
  145. EXPORT_SYMBOL(pci_biosrom_size);
  146. #define ROMSIGNATURE 0xaa55
  147. static int __init romsignature(const unsigned char *rom)
  148. {
  149. const unsigned short * const ptr = (const unsigned short *)rom;
  150. unsigned short sig;
  151. return probe_kernel_address(ptr, sig) == 0 && sig == ROMSIGNATURE;
  152. }
  153. static int __init romchecksum(const unsigned char *rom, unsigned long length)
  154. {
  155. unsigned char sum, c;
  156. for (sum = 0; length && probe_kernel_address(rom++, c) == 0; length--)
  157. sum += c;
  158. return !length && !sum;
  159. }
  160. void __init probe_roms(void)
  161. {
  162. const unsigned char *rom;
  163. unsigned long start, length, upper;
  164. unsigned char c;
  165. int i;
  166. /* video rom */
  167. upper = adapter_rom_resources[0].start;
  168. for (start = video_rom_resource.start; start < upper; start += 2048) {
  169. rom = isa_bus_to_virt(start);
  170. if (!romsignature(rom))
  171. continue;
  172. video_rom_resource.start = start;
  173. if (probe_kernel_address(rom + 2, c) != 0)
  174. continue;
  175. /* 0 < length <= 0x7f * 512, historically */
  176. length = c * 512;
  177. /* if checksum okay, trust length byte */
  178. if (length && romchecksum(rom, length))
  179. video_rom_resource.end = start + length - 1;
  180. request_resource(&iomem_resource, &video_rom_resource);
  181. break;
  182. }
  183. start = (video_rom_resource.end + 1 + 2047) & ~2047UL;
  184. if (start < upper)
  185. start = upper;
  186. /* system rom */
  187. request_resource(&iomem_resource, &system_rom_resource);
  188. upper = system_rom_resource.start;
  189. /* check for extension rom (ignore length byte!) */
  190. rom = isa_bus_to_virt(extension_rom_resource.start);
  191. if (romsignature(rom)) {
  192. length = extension_rom_resource.end - extension_rom_resource.start + 1;
  193. if (romchecksum(rom, length)) {
  194. request_resource(&iomem_resource, &extension_rom_resource);
  195. upper = extension_rom_resource.start;
  196. }
  197. }
  198. /* check for adapter roms on 2k boundaries */
  199. for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += 2048) {
  200. rom = isa_bus_to_virt(start);
  201. if (!romsignature(rom))
  202. continue;
  203. if (probe_kernel_address(rom + 2, c) != 0)
  204. continue;
  205. /* 0 < length <= 0x7f * 512, historically */
  206. length = c * 512;
  207. /* but accept any length that fits if checksum okay */
  208. if (!length || start + length > upper || !romchecksum(rom, length))
  209. continue;
  210. adapter_rom_resources[i].start = start;
  211. adapter_rom_resources[i].end = start + length - 1;
  212. request_resource(&iomem_resource, &adapter_rom_resources[i]);
  213. start = adapter_rom_resources[i++].end & ~2047UL;
  214. }
  215. }