1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <common.h>
25#include <pci.h>
26#include <asm/io.h>
27#include <asm/pci.h>
28
29#undef PCI_ROM_SCAN_VERBOSE
30
31int pci_shadow_rom(pci_dev_t dev, unsigned char *dest)
32{
33 struct pci_controller *hose;
34 int res = -1;
35 int i;
36
37 u32 rom_addr;
38 u32 addr_reg;
39 u32 size;
40
41 u16 vendor;
42 u16 device;
43 u32 class_code;
44
45 hose = pci_bus_to_hose(PCI_BUS(dev));
46#if 0
47 printf("pci_shadow_rom() asked to shadow device %x to %x\n",
48 dev, (u32)dest);
49#endif
50 pci_read_config_word(dev, PCI_VENDOR_ID, &vendor);
51 pci_read_config_word(dev, PCI_DEVICE_ID, &device);
52 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_code);
53
54 class_code &= 0xffffff00;
55 class_code >>= 8;
56
57#if 0
58 printf("PCI Header Vendor %04x device %04x class %06x\n",
59 vendor, device, class_code);
60#endif
61
62 pci_write_config_dword(dev, PCI_ROM_ADDRESS, (u32)PCI_ROM_ADDRESS_MASK);
63 pci_read_config_dword(dev, PCI_ROM_ADDRESS, &addr_reg);
64
65 if (!addr_reg) {
66
67 printf("pci_chadow_rom: device do not seem to have a rom\n");
68 return -1;
69 }
70
71 size = (~(addr_reg&PCI_ROM_ADDRESS_MASK))+1;
72
73#if 0
74 printf("ROM is %d bytes\n", size);
75#endif
76 rom_addr = pci_get_rom_window(hose, size);
77#if 0
78 printf("ROM mapped at %x \n", rom_addr);
79#endif
80 pci_write_config_dword(dev, PCI_ROM_ADDRESS,
81 pci_phys_to_mem(dev, rom_addr)
82 |PCI_ROM_ADDRESS_ENABLE);
83
84
85 for (i=rom_addr;i<rom_addr+size; i+=512) {
86
87
88 if (readw(i) == 0xaa55) {
89 u32 pci_data;
90#ifdef PCI_ROM_SCAN_VERBOSE
91 printf("ROM signature found\n");
92#endif
93 pci_data = readw(0x18+i);
94 pci_data += i;
95
96 if (0==memcmp((void*)pci_data, "PCIR", 4)) {
97#ifdef PCI_ROM_SCAN_VERBOSE
98 printf("Fount PCI rom image at offset %d\n", i-rom_addr);
99 printf("Vendor %04x device %04x class %06x\n",
100 readw(pci_data+4), readw(pci_data+6),
101 readl(pci_data+0x0d)&0xffffff);
102 printf("%s\n",
103 (readw(pci_data+0x15) &0x80)?
104 "Last image":"More images follow");
105 switch (readb(pci_data+0x14)) {
106 case 0:
107 printf("X86 code\n");
108 break;
109 case 1:
110 printf("Openfirmware code\n");
111 break;
112 case 2:
113 printf("PARISC code\n");
114 break;
115 }
116 printf("Image size %d\n", readw(pci_data+0x10) * 512);
117#endif
118
119
120
121 if (readw(pci_data+4) == vendor &&
122 readw(pci_data+6) == device &&
123
124 readb(pci_data+0x14) == 0 ) {
125#ifdef PCI_ROM_SCAN_VERBOSE
126 printf("Suitable ROM image found, copying\n");
127#endif
128 memmove(dest, (void*)rom_addr, readw(pci_data+0x10) * 512);
129 res = 0;
130 break;
131
132 }
133 if (readw(pci_data+0x15) &0x80) {
134 break;
135 }
136 }
137 }
138
139 }
140
141#ifdef PCI_ROM_SCAN_VERBOSE
142 if (res) {
143 printf("No suitable image found\n");
144 }
145#endif
146
147 pci_remove_rom_window(hose, rom_addr);
148
149 pci_write_config_dword(dev, PCI_ROM_ADDRESS, 0);
150
151 return res;
152}
153