2 * Copyright 1999 Egbert Eich
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the authors not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission. The authors makes no representations
11 * about the suitability of this software for any purpose. It is provided
12 * "as is" without express or implied warranty.
14 * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
27 #include <sys/types.h>
32 #include <readline/readline.h>
33 #include <readline/history.h>
34 #if defined(__alpha__) || defined (__ia64__)
36 #elif defined(HAVE_SYS_PERM)
42 #include "AsmMacros.h"
45 #define VRAM_START 0xA0000
46 #define VRAM_SIZE 0x1FFFF
47 #define V_BIOS_SIZE 0x1FFFF
48 #define BIOS_START 0x7C00 /* default BIOS entry */
49 #define BIOS_MEM 0x600
51 /*CARD8 code[] = { 0xb8 , 0xf0 , 0xf0, 0xf4 }; */
52 #define VB_X(x) (V_BIOS >> x) & 0xFF
53 CARD8 code[] = { 6, 0x9a, 0x03, 0x00, 0x00, VB_X(12), 0xf4 };
54 /*CARD8 code[] = { 0x9a, 0x03, 0x00, 0x00, VB_X(12), 0xb8, 0x03, 0x00, */
55 /*0xcd, 0x10, 0xf4 }; */
56 /*CARD8 code[] = { 0xb8 , 0xf0 , 0xf0 ,0xf4 }; */
58 int ioperm_list[IOPERM_BITS] = {0,};
60 static void sig_handler(int);
62 static void unmap(void);
63 static void bootBIOS(CARD16 ax);
64 static int map_vram(void);
65 static void unmap_vram(void);
66 static int copy_vbios(memType v_base);
67 static int copy_sys_bios(void);
68 static void save_bios_to_file(void);
69 static int setup_system_bios(void);
70 static CARD32 setup_int_vect(void);
72 static CARD32 setup_primary_int_vect(void);
74 static int chksum(CARD8 *start);
75 static void setup_bios_regs(i86biosRegsPtr regs, CARD32 ax);
76 static void print_regs(i86biosRegsPtr regs);
77 static void print_usage(void);
78 static void set_hlt(Bool set);
79 static void set_ioperm(void);
81 extern void yyparse();
83 void loadCodeToMem(unsigned char *ptr, CARD8 *code);
84 void dprint(unsigned long start, unsigned long size);
86 static int vram_mapped = 0;
87 static char* bios_var = NULL;
88 static CARD8 save_msr;
89 static CARD8 save_pos102;
90 static CARD8 save_vse;
91 static CARD8 save_46e8;
92 static haltpoints hltp[20] = { {0, 0}, };
94 console Console = {-1,-1};
97 int main(int argc,char **argv)
101 Config.PrintPort = PRINT_PORT;
102 Config.IoStatistics = IO_STATISTICS;
103 Config.PrintIrq = PRINT_IRQ;
104 Config.PrintPci = PRINT_PCI;
105 Config.ShowAllDev = SHOW_ALL_DEV;
106 Config.PrintIp = PRINT_IP;
107 Config.SaveBios = SAVE_BIOS;
108 Config.Trace = TRACE;
109 Config.ConfigActiveOnly = CONFIG_ACTIVE_ONLY; /* boot */
110 Config.ConfigActiveDevice = CONFIG_ACTIVE_DEVICE; /* boot */
111 Config.MapSysBios = MAP_SYS_BIOS;
112 Config.Resort = RESORT; /* boot */
113 Config.FixRom = FIX_ROM;
114 Config.NoConsole = NO_CONSOLE;
115 Config.BootOnly = FALSE;
116 Config.Verbose = VERBOSE;
119 while ((c = getopt(argc,argv,"psicaPStAdbrfnv:?")) != EOF) {
122 Config.PrintPort = TRUE;
125 Config.IoStatistics = TRUE;
128 Config.PrintIrq = TRUE;
131 Config.PrintPci = TRUE;
134 Config.ShowAllDev = TRUE;
137 Config.PrintIp = TRUE;
140 Config.SaveBios = TRUE;
146 Config.ConfigActiveOnly = TRUE;
149 Config.ConfigActiveDevice = TRUE;
152 Config.MapSysBios = TRUE;
155 Config.Resort = TRUE;
158 Config.FixRom = TRUE;
161 Config.NoConsole = TRUE;
164 Config.Verbose = strtol(optarg,NULL,0);
178 if (!setup_system_bios())
185 save_msr = inb(0x3CC);
186 save_vse = inb(0x3C3);
187 save_46e8 = inb(0x46e8);
188 save_pos102 = inb(0x102);
190 if (Config.BootOnly) {
192 if (!CurrentPci && !Config.ConfigActiveDevice
193 && !Config.ConfigActiveOnly) {
208 outb(0x102, save_pos102);
209 outb(0x46e8, save_46e8);
210 outb(0x3C3, save_vse);
211 outb(0x3C2, save_msr);
215 close_console(Console);
222 call_boot(struct device *dev)
224 int Active_is_Pci = 0;
227 CurrentPci = PciList;
228 Console = open_console();
233 signal(2,sig_handler);
234 signal(11,sig_handler);
236 /* disable primary card */
237 pciVideoRestore(); /* reset PCI state to see primary card */
238 outb(0x3C2,~(CARD8)0x03 & save_msr);
239 outb(0x3C3,~(CARD8)0x01 & save_vse);
240 outb(0x46e8, ~(CARD8)0x08 & save_46e8);
241 outb(0x102, ~(CARD8)0x01 & save_pos102);
248 if (CurrentPci->active) {
250 if (!Config.ConfigActiveDevice && !dev) {
251 CurrentPci = CurrentPci->next;
254 } else if (Config.ConfigActiveOnly && !dev) {
255 CurrentPci = CurrentPci->next;
258 if (dev && ((dev->type != PCI)
260 && (dev->loc.pci.dev != CurrentPci->dev
261 || dev->loc.pci.bus != CurrentPci->bus
262 || dev->loc.pci.func != CurrentPci->func)))) {
263 CurrentPci = CurrentPci->next;
269 if (CurrentPci->active) {
270 outb(0x102, save_pos102);
271 outb(0x46e8, save_46e8);
272 outb(0x3C3, save_vse);
273 outb(0x3C2, save_msr);
276 /* clear interrupt vectors */
278 vbios_base = CurrentPci->active ? setup_primary_int_vect()
281 vbios_base = setup_int_vect();
283 ax = ((CARD16)(CurrentPci->bus) << 8)
284 | (CurrentPci->dev << 3) | (CurrentPci->func & 0x7);
285 if (Config.Verbose > 1) P_printf("ax: 0x%x\n",ax);
287 BootBios = findPciByIDs(CurrentPci->bus,CurrentPci->dev,
289 if (!((mapPciRom(BootBios) && chksum((CARD8*)V_BIOS))
290 || (CurrentPci->active && copy_vbios(vbios_base)))) {
291 CurrentPci = CurrentPci->next;
295 CurrentPci = CurrentPci->next;
298 if (Config.SaveBios) save_bios_to_file();
299 printf("initializing PCI bus: %i dev: %i func: %i\n",CurrentPci->bus,
300 CurrentPci->dev,CurrentPci->func);
304 if (CurrentPci->active)
305 close_console(Console);
309 CurrentPci = CurrentPci->next;
312 /* We have an ISA device - configure if requested */
313 if (!Active_is_Pci /* no isa card in system! */
314 && ((!dev && (Config.ConfigActiveDevice || Config.ConfigActiveOnly))
315 || (dev && dev->type == ISA))) {
319 if (!dev || dev->type == ISA) {
320 outb(0x102, save_pos102);
321 outb(0x46e8, save_46e8);
322 outb(0x3C3, save_vse);
323 outb(0x3C2, save_msr);
326 vbios_base = setup_primary_int_vect();
328 vbios_base = setup_int_vect();
330 if (copy_vbios(vbios_base)) {
332 if (Config.SaveBios) save_bios_to_file();
334 printf("initializing ISA bus\n");
341 close_console(Console);
352 mem = mmap(0, (size_t)SIZE,
353 PROT_EXEC | PROT_READ | PROT_WRITE,
354 MAP_FIXED | MAP_PRIVATE | MAP_ANON,
357 perror("anonymous map");
376 printf("starting BIOS\n");
379 setup_bios_regs(&bRegs, ax);
380 loadCodeToMem((unsigned char *) BIOS_START, code);
381 do_x86(BIOS_START,&bRegs);
393 if ((mem_fd = open(MEM_FILE,O_RDWR | O_SYNC))<0)
395 if ((mem_fd = open(MEM_FILE,O_RDWR))<0)
398 perror("opening memory");
403 if (!_bus_base()) sparse_shift = 7; /* Uh, oh, JENSEN... */
404 if (!_bus_base_sparse()) sparse_shift = 0;
405 if ((vram_map = mmap(0,(size_t) (VRAM_SIZE << sparse_shift),
406 PROT_READ | PROT_WRITE,
408 mem_fd, (VRAM_START << sparse_shift)
409 | _bus_base_sparse())) == (void *) -1)
411 if (mmap((void *) VRAM_START, (size_t) VRAM_SIZE,
412 PROT_EXEC | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED,
413 mem_fd, VRAM_START) == (void *) -1)
416 perror("mmap error in map_hardware_ram (1)");
428 if (!vram_mapped) return;
430 munmap((void*)VRAM_START,VRAM_SIZE);
435 copy_vbios(memType v_base)
441 if ((mem_fd = open(MEM_FILE,O_RDONLY))<0) {
442 perror("opening memory");
446 if (lseek(mem_fd,(off_t) v_base, SEEK_SET) != (off_t) v_base) {
447 fprintf(stderr,"Cannot lseek\n");
450 tmp = (unsigned char *)malloc(3);
451 if (read(mem_fd, (char *)tmp, (size_t) 3) != (size_t) 3) {
452 fprintf(stderr,"Cannot read\n");
455 if (lseek(mem_fd,(off_t) v_base,SEEK_SET) != (off_t) v_base)
458 if (*tmp != 0x55 || *(tmp+1) != 0xAA ) {
459 fprintf(stderr,"No bios found at: 0x%lx\n",v_base);
463 dprint((unsigned long)tmp,0x100);
465 size = *(tmp+2) * 512;
467 if (read(mem_fd, (char *)v_base, (size_t) size) != (size_t) size) {
468 fprintf(stderr,"Cannot read\n");
473 if (!chksum((CARD8*)v_base))
487 #define SYS_BIOS 0xF0000
490 if ((mem_fd = open(MEM_FILE,O_RDONLY))<0) {
491 perror("opening memory");
495 if (lseek(mem_fd,(off_t) SYS_BIOS,SEEK_SET) != (off_t) SYS_BIOS)
497 if (read(mem_fd, (char *)SYS_BIOS, (size_t) 0xFFFF) != (size_t) 0xFFFF)
510 loadCodeToMem(unsigned char *ptr, CARD8 code[])
516 for ( i=1;i<=size;i++) {
524 dprint(unsigned long start, unsigned long size)
527 char *c = (char *)start;
529 for (j = 0; j < (size >> 4); j++) {
531 printf("\n0x%lx: ",(unsigned long)c);
532 for (i = 0; i<16; i++)
533 printf("%2.2x ",(unsigned char) (*(c++)));
535 for (i = 0; i<16; i++) {
536 printf("%c",((((CARD8)(*c)) > 32) && (((CARD8)(*c)) < 128)) ?
537 (unsigned char) (*(c)): '.');
545 save_bios_to_file(void)
552 sprintf(file_name,"bios_%i.fil",num);
553 if ((fd = open(file_name,O_WRONLY | O_CREAT | O_TRUNC,00644)) == -1)
555 size = (*(unsigned char*)(V_BIOS + 2)) * 512;
559 if ((count = write(fd,(void *)(V_BIOS),size)) != size)
560 fprintf(stderr,"only saved %i of %i bytes\n",size,count);
565 sig_handler(int unused)
570 /* put system back in a save state */
573 outb(0x102, save_pos102);
574 outb(0x46e8, save_46e8);
575 outb(0x3C3, save_vse);
576 outb(0x3C2, save_msr);
578 close_console(Console);
586 * For initialization we just pass ax to the BIOS.
587 * PCI BIOSes need this. All other register are set 0.
589 static void setup_bios_regs(i86biosRegsPtr regs, CARD32 ax)
596 regs->ds = 0x40; /* standard pc ds */
602 * here we are really paranoid about faking a "real"
603 * BIOS. Most of this information was pulled from
609 setup_primary_int_vect(void)
615 if ((mem_fd = open(MEM_FILE,O_RDWR))<0)
617 perror("opening memory");
621 if ((map = mmap((void *) 0, (size_t) 0x2000,
622 PROT_EXEC | PROT_READ | PROT_WRITE, MAP_SHARED,
623 mem_fd, 0)) == (void *)-1) {
624 perror("mmap error in map_hardware_ram (2)");
630 memcpy(0,map,BIOS_MEM);
633 * create a backup copy of the bios variables to write back the
637 bios_var = (char *)malloc(BIOS_MEM);
638 memcpy(bios_var,0,BIOS_MEM);
640 vbase = (*((CARD16*)(0x10 << 2) + 1)) << 4;
641 if (Config.Verbose > 0) printf("vbase: 0x%x\n",vbase);
649 const CARD16 cs = 0x0;
650 const CARD16 ip = 0x0;
653 /* let the int vects point to the SYS_BIOS seg */
654 for (i=0; i<0x80; i++) {
655 ((CARD16*)0)[i<<1] = ip;
656 ((CARD16*)0)[(i<<1)+1] = cs;
658 /* video interrupts default location */
659 ((CARD16*)0)[(0x42<<1)+1] = 0xf000;
660 ((CARD16*)0)[0x42<<1] = 0xf065;
661 ((CARD16*)0)[(0x10<<1)+1] = 0xf000;
662 ((CARD16*)0)[0x10<<1] = 0xf065;
663 /* video param table default location (int 1d) */
664 ((CARD16*)0)[(0x1d<<1)+1] = 0xf000;
665 ((CARD16*)0)[0x1d<<1] = 0xf0A4;
666 /* font tables default location (int 1F) */
667 ((CARD16*)0)[(0x1f<<1)+1] = 0xf000;
668 ((CARD16*)0)[0x1f<<1] = 0xfa6e;
670 /* int 11 default location */
671 ((CARD16*)0)[(0x11<<1)+1] = 0xf000;
672 ((CARD16*)0)[0x11<<1] = 0xf84d;
673 /* int 12 default location */
674 ((CARD16*)0)[(0x12<<1)+1] = 0xf000;
675 ((CARD16*)0)[0x12<<1] = 0xf841;
676 /* int 15 default location */
677 ((CARD16*)0)[(0x15<<1)+1] = 0xf000;
678 ((CARD16*)0)[0x15<<1] = 0xf859;
679 /* int 1A default location */
680 ((CARD16*)0)[(0x1a<<1)+1] = 0xf000;
681 ((CARD16*)0)[0x1a<<1] = 0xff6e;
682 /* int 05 default location */
683 ((CARD16*)0)[(0x05<<1)+1] = 0xf000;
684 ((CARD16*)0)[0x05<<1] = 0xff54;
685 /* int 08 default location */
686 ((CARD16*)0)[(0x8<<1)+1] = 0xf000;
687 ((CARD16*)0)[0x8<<1] = 0xfea5;
688 /* int 13 default location (fdd) */
689 ((CARD16*)0)[(0x13<<1)+1] = 0xf000;
690 ((CARD16*)0)[0x13<<1] = 0xec59;
691 /* int 0E default location */
692 ((CARD16*)0)[(0xe<<1)+1] = 0xf000;
693 ((CARD16*)0)[0xe<<1] = 0xef57;
694 /* int 17 default location */
695 ((CARD16*)0)[(0x17<<1)+1] = 0xf000;
696 ((CARD16*)0)[0x17<<1] = 0xefd2;
697 /* fdd table default location (int 1e) */
698 ((CARD16*)0)[(0x1e<<1)+1] = 0xf000;
699 ((CARD16*)0)[0x1e<<1] = 0xefc7;
704 setup_system_bios(void)
706 char *date = "06/01/99";
707 char *eisa_ident = "PCI/ISA";
709 if (Config.MapSysBios) {
711 if (!copy_sys_bios()) return 0;
716 /* memset((void *)0xF0000,0xf4,0xfff7); */
719 * we trap the "industry standard entry points" to the BIOS
720 * and all other locations by filling them with "hlt"
721 * TODO: implement hlt-handler for these
723 memset((void *)0xF0000,0xf4,0x10000);
726 * TODO: we should copy the fdd table (0xfec59-0xfec5b)
727 * the video parameter table (0xf0ac-0xf0fb)
728 * and the font tables (0xfa6e-0xfe6d)
729 * from the original bios here
733 strcpy((char *)0xFFFF5,date);
734 /* set up eisa ident string */
735 strcpy((char *)0xFFFD9,eisa_ident);
736 /* write system model id for IBM-AT */
737 ((char *)0)[0xFFFFE] = 0xfc;
745 update_bios_vars(void)
752 if ((mem_fd = open(MEM_FILE,O_RDWR | O_SYNC))<0)
754 if ((mem_fd = open(MEM_FILE,O_RDWR))<0)
757 perror("opening memory");
761 if ((map = mmap((void *) 0, (size_t) 0x2000,
762 PROT_EXEC | PROT_READ | PROT_WRITE, MAP_SHARED,
763 mem_fd, 0)) == (void *)-1) {
764 perror("mmap error in map_hardware_ram (3)");
769 for (i = 0; i < BIOS_MEM; i++) {
770 if (bios_var[i] != *(CARD8*)i)
771 *((CARD8*)map + i) = *(CARD8*)i;
785 size = *(start+2) * 512;
786 for (i = 0; i<size; i++)
792 fprintf(stderr,"BIOS cksum wrong!\n");
797 runINT(int num, i86biosRegsPtr Regs)
799 Bool isVideo = FALSE;
800 CARD8 code_int[] = { 3, 0xcd, 0x00, 0xf4 };
802 code_int[2] = (CARD8) num;
807 if (!setup_system_bios())
810 if ((isVideo && (!CurrentPci || CurrentPci->active)) || !isVideo) {
814 if (!(vbios_base = setup_primary_int_vect()))
816 if (!(vbios_base = setup_int_vect()))
819 if (!copy_vbios(vbios_base))
827 printf("starting BIOS\n");
829 loadCodeToMem((unsigned char *) BIOS_START, code_int);
834 do_x86(BIOS_START,Regs);
842 if ((isVideo && (!CurrentPci || CurrentPci->active)) || !isVideo)
847 print_regs(i86biosRegsPtr regs)
849 printf("ax=%x bx=%x cx=%x dx=%x ds=%x es=%x di=%x si=%x\n",
850 (CARD16)regs->ax,(CARD16)regs->bx,(CARD16)regs->cx,(CARD16)regs->dx,
851 (CARD16)regs->ds,(CARD16)regs->es,(CARD16)regs->di,
861 add_hlt(unsigned long val)
865 if (val < BIOS_MEM || (val > VRAM_START && val < (VRAM_START + VRAM_SIZE))
867 printf("address out of range\n");
871 for (i=0; i<20; i++) {
872 if (hltp[i].address == 0) {
873 hltp[i].address = (void*)val;
877 if (i == 20) printf("no more hltpoints available\n");
883 if (val == 21) { /* delete all */
885 printf("clearing all hltpoints\n");
886 for (i=0; i <20; i++)
887 hltp[i].address = NULL;
888 } else if (val >= 0 && val <20)
889 hltp[val].address = NULL;
890 else printf("hltpoint %i out of range: valid range 0-19\n",val);
899 printf("hltpoint[%i]: 0x%lx\n",i,(unsigned long)hltp[i].address);
907 if (hltp[i].address) {
909 hltp[i].orgval = *(CARD8*)hltp[i].address;
910 *(CARD8*)hltp[i].address = 0xf4;
912 *(CARD8*)hltp[i].address = hltp[i].orgval;
921 ioperm(0,IOPERM_BITS,0);
923 for (i = 0; i < IOPERM_BITS;i++)
924 if (ioperm_list[i]) {
926 for (;i < IOPERM_BITS; i++) {
927 if (!ioperm_list[i]) {
928 ioperm(start,i - start, 1);