probe_roms.c 6.2 KB

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