probe_roms.c 6.3 KB

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