Bug fixes; get mouse working.
[seabios.git] / src / boot.c
1 // 16bit code to load disk image and start system boot.
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 "util.h" // irq_enable
9 #include "biosvar.h" // struct bregs
10 #include "config.h" // CONFIG_*
11 #include "cmos.h" // inb_cmos
12 #include "ata.h" // ata_detect
13
14 // We need a copy of this string, but we are not actually a PnP BIOS,
15 // so make sure it is *not* aligned, so OSes will not see it if they
16 // scan.
17 char pnp_string[] VISIBLE __attribute__((aligned (2))) = " $PnP";
18
19 //--------------------------------------------------------------------------
20 // print_boot_device
21 //   displays the boot device
22 //--------------------------------------------------------------------------
23
24 static const char drivetypes[][10]={
25     "", "Floppy","Hard Disk","CD-Rom", "Network"
26 };
27
28 static void
29 print_boot_device(u16 type)
30 {
31     /* NIC appears as type 0x80 */
32     if (type == IPL_TYPE_BEV)
33         type = 0x4;
34     if (type == 0 || type > 0x4)
35         BX_PANIC("Bad drive type\n");
36     printf("Booting from %s...\n", drivetypes[type]);
37
38     // XXX - latest cvs has BEV description
39 }
40
41 //--------------------------------------------------------------------------
42 // print_boot_failure
43 //   displays the reason why boot failed
44 //--------------------------------------------------------------------------
45 static void
46 print_boot_failure(u16 type, u8 reason)
47 {
48     if (type == 0 || type > 0x3)
49         BX_PANIC("Bad drive type\n");
50
51     printf("Boot failed");
52     if (type < 4) {
53         /* Report the reason too */
54         if (reason==0)
55             printf(": not a bootable disk");
56         else
57             printf(": could not read the boot disk");
58     }
59     printf("\n\n");
60 }
61
62 static void
63 try_boot(u16 seq_nr)
64 {
65     SET_IPL(sequence, seq_nr);
66     u16 bootseg;
67     u8 bootdrv = 0;
68     u16 bootdev, bootip;
69
70     if (CONFIG_ELTORITO_BOOT) {
71         bootdev = inb_cmos(CMOS_BIOS_BOOTFLAG2);
72         bootdev |= ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4);
73         bootdev >>= 4 * seq_nr;
74         bootdev &= 0xf;
75         if (bootdev == 0)
76             BX_PANIC("No bootable device.\n");
77
78         /* Translate from CMOS runes to an IPL table offset by subtracting 1 */
79         bootdev -= 1;
80     } else {
81         if (seq_nr ==2)
82             BX_PANIC("No more boot devices.");
83         if (!!(inb_cmos(CMOS_BIOS_CONFIG) & 0x20) ^ (seq_nr == 1))
84             /* Boot from floppy if the bit is set or it's the second boot */
85             bootdev = 0x00;
86         else
87             bootdev = 0x01;
88     }
89
90     if (bootdev >= GET_IPL(count)) {
91         BX_INFO("Invalid boot device (0x%x)\n", bootdev);
92         return;
93     }
94     u16 type = GET_IPL(table[bootdev].type);
95
96     /* Do the loading, and set up vector as a far pointer to the boot
97      * address, and bootdrv as the boot drive */
98     print_boot_device(type);
99
100     struct bregs cr;
101     switch(type) {
102     case IPL_TYPE_FLOPPY: /* FDD */
103     case IPL_TYPE_HARDDISK: /* HDD */
104
105         bootdrv = (type == IPL_TYPE_HARDDISK) ? 0x80 : 0x00;
106         bootseg = 0x07c0;
107
108         // Read sector
109         memset(&cr, 0, sizeof(cr));
110         cr.dl = bootdrv;
111         cr.es = bootseg;
112         cr.ah = 2;
113         cr.al = 1;
114         cr.cl = 1;
115         call16_int(0x13, &cr);
116
117         if (cr.flags & F_CF) {
118             print_boot_failure(type, 1);
119             return;
120         }
121
122         /* Always check the signature on a HDD boot sector; on FDD,
123          * only do the check if the CMOS doesn't tell us to skip it */
124         if ((type != IPL_TYPE_FLOPPY)
125             || !((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0x01))) {
126             if (GET_FARVAR(bootseg, *(u16*)0x1fe) != 0xaa55) {
127                 print_boot_failure(type, 0);
128                 return;
129             }
130         }
131
132         /* Canonicalize bootseg:bootip */
133         bootip = (bootseg & 0x0fff) << 4;
134         bootseg &= 0xf000;
135         break;
136     case IPL_TYPE_CDROM: /* CD-ROM */
137         // XXX
138         return;
139         break;
140     case IPL_TYPE_BEV: {
141         /* Expansion ROM with a Bootstrap Entry Vector (a far
142          * pointer) */
143         u32 vector = GET_IPL(table[bootdev].vector);
144         bootseg = vector >> 16;
145         bootip = vector & 0xffff;
146         break;
147     }
148     default:
149         return;
150     }
151
152     memset(&cr, 0, sizeof(cr));
153     cr.ip = bootip;
154     cr.cs = bootseg;
155     // Set the magic number in ax and the boot drive in dl.
156     cr.dl = bootdrv;
157     cr.ax = 0xaa55;
158     call16(&cr);
159
160     // Boot failed: invoke the boot recovery function
161     memset(&cr, 0, sizeof(cr));
162     call16_int(0x18, &cr);
163 }
164
165 // Boot Failure recovery: try the next device.
166 void VISIBLE
167 handle_18()
168 {
169     debug_enter(NULL);
170     u16 seq = GET_IPL(sequence) + 1;
171     try_boot(seq);
172 }
173
174 // INT 19h Boot Load Service Entry Point
175 void VISIBLE
176 handle_19()
177 {
178     debug_enter(NULL);
179     try_boot(0);
180 }
181
182 // Called from 32bit code - start boot process
183 void VISIBLE
184 begin_boot()
185 {
186     if (CONFIG_ATA)
187         ata_detect();
188     irq_enable();
189     struct bregs br;
190     memset(&br, 0, sizeof(br));
191     call16_int(0x19, &br);
192 }