Don't allow start_bdf with new auto max bus detection code.
[seabios.git] / src / optionroms.c
1 // Option rom scanning code.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU GPLv3 license.
7
8 #include "bregs.h" // struct bregs
9 #include "biosvar.h" // struct ipl_entry_s
10 #include "util.h" // dprintf
11 #include "pci.h" // pci_find_class
12 #include "pci_regs.h" // PCI_ROM_ADDRESS
13 #include "pci_ids.h" // PCI_CLASS_DISPLAY_VGA
14
15
16 /****************************************************************
17  * Definitions
18  ****************************************************************/
19
20 // $PnP string with special alignment in romlayout.S
21 extern char pnp_string[];
22
23 struct rom_header {
24     u16 signature;
25     u8 size;
26     u8 initVector[4];
27     u8 reserved[17];
28     u16 pcioffset;
29     u16 pnpoffset;
30 } PACKED;
31
32 struct pci_data {
33     u32 signature;
34     u16 vendor;
35     u16 device;
36     u16 vitaldata;
37     u16 dlen;
38     u8 drevision;
39     u8 class_lo;
40     u16 class_hi;
41     u16 ilen;
42     u16 irevision;
43     u8 type;
44     u8 indicator;
45     u16 reserved;
46 } PACKED;
47
48 struct pnp_data {
49     u32 signature;
50     u8 revision;
51     u8 len;
52     u16 nextoffset;
53     u8 reserved_08;
54     u8 checksum;
55     u32 devid;
56     u16 manufacturer;
57     u16 productname;
58     u8 type_lo;
59     u16 type_hi;
60     u8 dev_flags;
61     u16 bcv;
62     u16 dv;
63     u16 bev;
64     u16 reserved_1c;
65     u16 staticresource;
66 } PACKED;
67
68 #define OPTIONROM_BDF_1 0x0000
69 #define OPTIONROM_MEM_1 0x00000000
70 #define OPTIONROM_BDF_2 0x0000
71 #define OPTIONROM_MEM_2 0x00000000
72
73 #define OPTION_ROM_START 0xc0000
74 #define OPTION_ROM_SIGNATURE 0xaa55
75 #define OPTION_ROM_ALIGN 2048
76 #define OPTION_ROM_INITVECTOR offsetof(struct rom_header, initVector[0])
77 #define PCI_ROM_SIGNATURE 0x52494350 // PCIR
78
79 // Next available position for an option rom.
80 static u32 next_rom;
81
82
83 /****************************************************************
84  * Helper functions
85  ****************************************************************/
86
87 // Execute a given option rom.
88 static void
89 callrom(struct rom_header *rom, u16 offset, u16 bdf)
90 {
91     u16 seg = FARPTR_TO_SEG(rom);
92     dprintf(1, "Running option rom at %x:%x\n", seg, offset);
93
94     struct bregs br;
95     memset(&br, 0, sizeof(br));
96     br.ax = bdf;
97     br.bx = 0xffff;
98     br.dx = 0xffff;
99     br.es = SEG_BIOS;
100     br.di = (u32)pnp_string - BUILD_BIOS_ADDR;
101     br.cs = seg;
102     br.ip = offset;
103     // XXX - should call option rom in "big real mode".
104     call16(&br);
105
106     debug_serial_setup();
107
108     if (GET_BDA(ebda_seg) != SEG_EBDA)
109         BX_PANIC("Option rom at %x:%x attempted to move ebda from %x to %x\n"
110                  , seg, offset, SEG_EBDA, GET_BDA(ebda_seg));
111 }
112
113 // Verify that an option rom looks valid
114 static int
115 is_valid_rom(struct rom_header *rom)
116 {
117     if (rom->signature != OPTION_ROM_SIGNATURE)
118         return 0;
119     u32 len = rom->size * 512;
120     u8 sum = checksum((void*)rom, len);
121     if (sum != 0) {
122         dprintf(1, "Found option rom with bad checksum: loc=%p len=%d sum=%x\n"
123                 , rom, len, sum);
124         return 0;
125     }
126     return 1;
127 }
128
129 // Check if a valid option rom has a pnp struct; return it if so.
130 static struct pnp_data *
131 get_pnp_rom(struct rom_header *rom)
132 {
133     struct pnp_data *pnp = (struct pnp_data *)((u8*)rom + rom->pnpoffset);
134     if (pnp->signature != *(u32*)pnp_string)
135         return NULL;
136     return pnp;
137 }
138
139 // Add a BEV vector for a given pnp compatible option rom.
140 static void
141 add_ipl(struct rom_header *rom, struct pnp_data *pnp)
142 {
143 #define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
144
145     // Found a device that thinks it can boot the system.  Record
146     // its BEV and product name string.
147
148     if (! CONFIG_BOOT)
149         return;
150
151     if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
152         return;
153
154     struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
155     ip->type = IPL_TYPE_BEV;
156     ip->vector = (FARPTR_TO_SEG(rom) << 16) | pnp->bev;
157
158     u16 desc = pnp->productname;
159     if (desc)
160         ip->description = MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
161
162     ebda->ipl.count++;
163 }
164
165 // Copy a rom to its permanent location below 1MiB
166 static struct rom_header *
167 copy_rom(struct rom_header *rom)
168 {
169     u32 romsize = rom->size * 512;
170     if (next_rom + romsize > BUILD_BIOS_ADDR) {
171         // Option rom doesn't fit.
172         dprintf(1, "Option rom %p doesn't fit.\n", rom);
173         return NULL;
174     }
175     dprintf(4, "Copying option rom from %p to %x\n", rom, next_rom);
176     memcpy((void*)next_rom, rom, romsize);
177     return (struct rom_header *)next_rom;
178 }
179
180 // Check if an option rom is at a hardcoded location for a device.
181 static struct rom_header *
182 lookup_hardcode(u16 bdf)
183 {
184     if (OPTIONROM_BDF_1 && OPTIONROM_BDF_1 == bdf)
185         return copy_rom((struct rom_header *)OPTIONROM_MEM_1);
186     else if (OPTIONROM_BDF_2 && OPTIONROM_BDF_2 == bdf)
187         return copy_rom((struct rom_header *)OPTIONROM_MEM_2);
188     // XXX - check LAR when in coreboot?
189     return NULL;
190 }
191
192 // Map the option rom of a given PCI device.
193 static struct rom_header *
194 map_optionrom(u16 bdf)
195 {
196     dprintf(6, "Attempting to map option rom on dev %x\n", bdf);
197
198     u32 orig = pci_config_readl(bdf, PCI_ROM_ADDRESS);
199     pci_config_writel(bdf, PCI_ROM_ADDRESS, ~PCI_ROM_ADDRESS_ENABLE);
200     u32 sz = pci_config_readl(bdf, PCI_ROM_ADDRESS);
201
202     dprintf(6, "Option rom sizing returned %x %x\n", orig, sz);
203     orig &= ~PCI_ROM_ADDRESS_ENABLE;
204     if (!sz || sz == 0xffffffff)
205         goto fail;
206
207     // Looks like a rom - enable it.
208     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig | PCI_ROM_ADDRESS_ENABLE);
209
210     u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
211     struct rom_header *rom = (struct rom_header *)orig;
212     for (;;) {
213         dprintf(5, "Inspecting possible rom at %p (vd=%x bdf=%x)\n"
214                 , rom, vendev, bdf);
215         if (rom->signature != OPTION_ROM_SIGNATURE) {
216             dprintf(6, "No option rom signature (got %x)\n", rom->signature);
217             goto fail;
218         }
219         if (!rom->pcioffset) {
220             dprintf(6, "No PCI offset\n");
221             goto fail;
222         }
223         struct pci_data *pci = (struct pci_data *)((u32)rom + rom->pcioffset);
224         if (pci->signature != PCI_ROM_SIGNATURE) {
225             dprintf(6, "Invalid pci signature (got %x)\n", pci->signature);
226             goto fail;
227         }
228         u32 vd = (pci->device << 16) | pci->vendor;
229         if (vd == vendev && pci->type == 0)
230             // A match
231             break;
232         dprintf(6, "Didn't match vendev (got %x) or type (got %d)\n"
233                 , vd, pci->type);
234         if (pci->indicator & 0x80) {
235             dprintf(6, "No more images left\n");
236             goto fail;
237         }
238         rom = (struct rom_header *)((u32)rom + pci->ilen * 512);
239     }
240
241     rom = copy_rom(rom);
242     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
243     return rom;
244 fail:
245     // Not valid - restore original and exit.
246     pci_config_writel(bdf, PCI_ROM_ADDRESS, orig);
247     return NULL;
248 }
249
250 // Attempt to map and initialize the option rom on a given PCI device.
251 static struct rom_header *
252 init_optionrom(u16 bdf)
253 {
254     dprintf(4, "Attempting to init PCI bdf %x\n", bdf);
255     struct rom_header *rom = lookup_hardcode(bdf);
256     if (! rom)
257         rom = map_optionrom(bdf);
258     if (! rom)
259         // No ROM present.
260         return NULL;
261
262     if (! is_valid_rom(rom))
263         return NULL;
264
265     if (get_pnp_rom(rom))
266         // Init the PnP rom.
267         callrom(rom, OPTION_ROM_INITVECTOR, bdf);
268
269     next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
270
271     return rom;
272 }
273
274
275 /****************************************************************
276  * Non-VGA option rom init
277  ****************************************************************/
278
279 void
280 optionrom_setup()
281 {
282     if (! CONFIG_OPTIONROMS)
283         return;
284
285     dprintf(1, "Scan for option roms\n");
286
287     u32 post_vga = next_rom;
288
289     if (CONFIG_OPTIONROMS_DEPLOYED) {
290         // Option roms are already deployed on the system.
291         u32 pos = next_rom;
292         while (pos < BUILD_BIOS_ADDR) {
293             struct rom_header *rom = (struct rom_header *)pos;
294             if (! is_valid_rom(rom)) {
295                 pos += OPTION_ROM_ALIGN;
296                 continue;
297             }
298             if (get_pnp_rom(rom))
299                 callrom(rom, OPTION_ROM_INITVECTOR, 0);
300             pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
301             next_rom = pos;
302         }
303     } else {
304         // Find and deploy PCI roms.
305         int bdf, max;
306         foreachpci(bdf, max) {
307             u16 v = pci_config_readw(bdf, PCI_CLASS_DEVICE);
308             if (v == 0x0000 || v == 0xffff || v == PCI_CLASS_DISPLAY_VGA)
309                 continue;
310             init_optionrom(bdf);
311         }
312     }
313
314     // All option roms found and deployed - now build BEV/BCV vectors.
315
316     u32 pos = post_vga;
317     while (pos < next_rom) {
318         struct rom_header *rom = (struct rom_header *)pos;
319         if (! is_valid_rom(rom)) {
320             pos += OPTION_ROM_ALIGN;
321             continue;
322         }
323         pos += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
324         struct pnp_data *pnp = get_pnp_rom(rom);
325         if (! pnp) {
326             // Legacy rom - run init vector now.
327             callrom(rom, OPTION_ROM_INITVECTOR, 0);
328             continue;
329         }
330         // PnP rom.
331         if (pnp->bev)
332             // Can boot system - add to IPL list.
333             add_ipl(rom, pnp);
334         else if (pnp->bcv)
335             // Has BCV - run it now.
336             callrom(rom, pnp->bcv, 0);
337     }
338 }
339
340
341 /****************************************************************
342  * VGA init
343  ****************************************************************/
344
345 // Call into vga code to turn on console.
346 void
347 vga_setup()
348 {
349     if (! CONFIG_OPTIONROMS)
350         return;
351
352     dprintf(1, "Scan for VGA option rom\n");
353     next_rom = OPTION_ROM_START;
354
355     if (CONFIG_OPTIONROMS_DEPLOYED) {
356         // Option roms are already deployed on the system.
357         struct rom_header *rom = (struct rom_header *)OPTION_ROM_START;
358         if (! is_valid_rom(rom))
359             return;
360         callrom(rom, OPTION_ROM_INITVECTOR, 0);
361         next_rom += ALIGN(rom->size * 512, OPTION_ROM_ALIGN);
362     } else {
363         // Find and deploy PCI VGA rom.
364         int bdf = pci_find_class(PCI_CLASS_DISPLAY_VGA);
365         if (bdf < 0)
366             // Device not found
367             return;
368
369         struct rom_header *rom = init_optionrom(bdf);
370         if (rom && !get_pnp_rom(rom))
371             // Call rom even if it isn't a pnp rom.
372             callrom(rom, OPTION_ROM_INITVECTOR, bdf);
373     }
374
375     dprintf(1, "Turning on vga console\n");
376     struct bregs br;
377     memset(&br, 0, sizeof(br));
378     br.ax = 0x0003;
379     call16_int(0x10, &br);
380
381     // Write to screen.
382     printf("Starting SeaBIOS\n\n");
383 }