1 | /* $Id: boot.c 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PC BIOS - ???
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | * --------------------------------------------------------------------
|
---|
27 | *
|
---|
28 | * This code is based on:
|
---|
29 | *
|
---|
30 | * ROM BIOS for use with Bochs/Plex86/QEMU emulation environment
|
---|
31 | *
|
---|
32 | * Copyright (C) 2002 MandrakeSoft S.A.
|
---|
33 | *
|
---|
34 | * MandrakeSoft S.A.
|
---|
35 | * 43, rue d'Aboukir
|
---|
36 | * 75002 Paris - France
|
---|
37 | * http://www.linux-mandrake.com/
|
---|
38 | * http://www.mandrakesoft.com/
|
---|
39 | *
|
---|
40 | * This library is free software; you can redistribute it and/or
|
---|
41 | * modify it under the terms of the GNU Lesser General Public
|
---|
42 | * License as published by the Free Software Foundation; either
|
---|
43 | * version 2 of the License, or (at your option) any later version.
|
---|
44 | *
|
---|
45 | * This library is distributed in the hope that it will be useful,
|
---|
46 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
47 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
48 | * Lesser General Public License for more details.
|
---|
49 | *
|
---|
50 | * You should have received a copy of the GNU Lesser General Public
|
---|
51 | * License along with this library; if not, write to the Free Software
|
---|
52 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
53 | *
|
---|
54 | */
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
58 | * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
|
---|
59 | * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
|
---|
60 | * a choice of LGPL license versions is made available with the language indicating
|
---|
61 | * that LGPLv2 or any later version may be used, or where a choice of which version
|
---|
62 | * of the LGPL is applied is otherwise unspecified.
|
---|
63 | */
|
---|
64 |
|
---|
65 |
|
---|
66 | #include <stdint.h>
|
---|
67 | #include <string.h>
|
---|
68 | #include "inlines.h"
|
---|
69 | #include "biosint.h"
|
---|
70 | #include "ebda.h"
|
---|
71 |
|
---|
72 | /* Sanity check the LAN boot segment definition. */
|
---|
73 | #if VBOX_LANBOOT_SEG < 0xA000
|
---|
74 | #error VBOX_LANBOOT_SEG incorrect!
|
---|
75 | #endif
|
---|
76 |
|
---|
77 | /* PnP header used with LAN boot ROMs. */
|
---|
78 | typedef struct {
|
---|
79 | uint32_t sig;
|
---|
80 | uint8_t revision;
|
---|
81 | uint8_t length;
|
---|
82 | uint16_t next_s;
|
---|
83 | uint8_t pad1;
|
---|
84 | uint8_t checksum;
|
---|
85 | uint32_t dev_id;
|
---|
86 | uint16_t mfg_string;
|
---|
87 | uint16_t prod_string;
|
---|
88 | uint8_t base_class;
|
---|
89 | uint8_t subclass;
|
---|
90 | uint8_t interface;
|
---|
91 | uint8_t dev_ind;
|
---|
92 | uint16_t boot_code;
|
---|
93 | uint16_t dv;
|
---|
94 | uint16_t bev;
|
---|
95 | uint16_t pad2;
|
---|
96 | uint16_t sriv;
|
---|
97 | } pnp_exp_t;
|
---|
98 |
|
---|
99 |
|
---|
100 | int read_boot_sec(uint8_t bootdrv, uint16_t segment);
|
---|
101 | #pragma aux read_boot_sec = \
|
---|
102 | "mov ax,0201h" \
|
---|
103 | "mov dh,0" \
|
---|
104 | "mov cx,1" \
|
---|
105 | "xor bx,bx" \
|
---|
106 | "int 13h" \
|
---|
107 | "mov ax,0" \
|
---|
108 | "sbb ax,0" \
|
---|
109 | parm [dl] [es] modify [ax bx cx dx];
|
---|
110 |
|
---|
111 | //--------------------------------------------------------------------------
|
---|
112 | // print_boot_device
|
---|
113 | // displays the boot device
|
---|
114 | //--------------------------------------------------------------------------
|
---|
115 |
|
---|
116 | static const char drivetypes[][10]={"Floppy","Hard Disk","CD-ROM","LAN"};
|
---|
117 |
|
---|
118 | /// @todo pass inputs as bit flags rather than bytes?
|
---|
119 | void print_boot_device(uint8_t cdboot, uint8_t lanboot, uint8_t drive)
|
---|
120 | {
|
---|
121 | int i;
|
---|
122 |
|
---|
123 | // cdboot contains 0 if lan/floppy/harddisk, 1 otherwise
|
---|
124 | // lanboot contains 0 if floppy/harddisk, 1 otherwise
|
---|
125 | // drive contains real/emulated boot drive
|
---|
126 |
|
---|
127 | if(cdboot)i=2; // CD-Rom
|
---|
128 | else if(lanboot)i=3; // LAN
|
---|
129 | else if((drive&0x0080)==0x00)i=0; // Floppy
|
---|
130 | else if((drive&0x0080)==0x80)i=1; // Hard drive
|
---|
131 | else return;
|
---|
132 |
|
---|
133 | BX_INFO("Booting from %s...\n",drivetypes[i]);
|
---|
134 | }
|
---|
135 |
|
---|
136 | //--------------------------------------------------------------------------
|
---|
137 | // print_boot_failure
|
---|
138 | // displays the reason why boot failed
|
---|
139 | //--------------------------------------------------------------------------
|
---|
140 | /// @todo pass inputs as bit flags rather than bytes?
|
---|
141 | void print_boot_failure(uint8_t cdboot, uint8_t lanboot, uint8_t drive,
|
---|
142 | uint8_t reason, uint8_t lastdrive)
|
---|
143 | {
|
---|
144 | uint16_t drivenum = drive&0x7f;
|
---|
145 |
|
---|
146 | // cdboot: 1 if boot from cd, 0 otherwise
|
---|
147 | // lanboot: 1 if boot from lan, 0 otherwise
|
---|
148 | // drive : drive number
|
---|
149 | // reason: 0 signature check failed, 1 read error
|
---|
150 | // lastdrive: 1 boot drive is the last one in boot sequence
|
---|
151 |
|
---|
152 | if (cdboot)
|
---|
153 | BX_INFO("Boot from %s failed\n",drivetypes[2]);
|
---|
154 | else if (lanboot)
|
---|
155 | BX_INFO("Boot from %s failed\n",drivetypes[3]);
|
---|
156 | else if (drive & 0x80)
|
---|
157 | BX_INFO("Boot from %s %d failed\n", drivetypes[1],drivenum);
|
---|
158 | else
|
---|
159 | BX_INFO("Boot from %s %d failed\n", drivetypes[0],drivenum);
|
---|
160 |
|
---|
161 | if (lastdrive==1) {
|
---|
162 | if (reason==0)
|
---|
163 | BX_INFO_CON("No bootable medium found!\n");
|
---|
164 | else
|
---|
165 | BX_INFO_CON("Could not read from the boot medium!\n");
|
---|
166 | BX_INFO_CON("Please insert a bootable medium and reboot.\n");
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | //--------------------------------------------------------------------------
|
---|
171 | // print_cdromboot_failure
|
---|
172 | // displays the reason why boot failed
|
---|
173 | //--------------------------------------------------------------------------
|
---|
174 | void print_cdromboot_failure(uint16_t code)
|
---|
175 | {
|
---|
176 | BX_INFO("CDROM boot failure code : %04x\n",code);
|
---|
177 | return;
|
---|
178 | }
|
---|
179 |
|
---|
180 | // returns bootsegment in ax, drive in bl
|
---|
181 | uint32_t BIOSCALL int19_function(uint8_t bseqnr)
|
---|
182 | {
|
---|
183 | /// @todo common code for getting the EBDA segment
|
---|
184 | uint16_t ebda_seg=read_word(0x0040,0x000E);
|
---|
185 | uint16_t bootseq;
|
---|
186 | uint8_t bootdrv;
|
---|
187 | uint8_t bootcd;
|
---|
188 | uint8_t bootlan;
|
---|
189 | uint8_t bootchk;
|
---|
190 | uint16_t bootseg;
|
---|
191 | uint16_t status;
|
---|
192 | uint8_t lastdrive=0;
|
---|
193 |
|
---|
194 | // if BX_ELTORITO_BOOT is not defined, old behavior
|
---|
195 | // check bit 5 in CMOS reg 0x2d. load either 0x00 or 0x80 into DL
|
---|
196 | // in preparation for the initial INT 13h (0=floppy A:, 0x80=C:)
|
---|
197 | // 0: system boot sequence, first drive C: then A:
|
---|
198 | // 1: system boot sequence, first drive A: then C:
|
---|
199 | // else BX_ELTORITO_BOOT is defined
|
---|
200 | // CMOS regs 0x3D and 0x38 contain the boot sequence:
|
---|
201 | // CMOS reg 0x3D & 0x0f : 1st boot device
|
---|
202 | // CMOS reg 0x3D & 0xf0 : 2nd boot device
|
---|
203 | // CMOS reg 0x38 & 0xf0 : 3rd boot device
|
---|
204 | // CMOS reg 0x3C & 0x0f : 4th boot device
|
---|
205 | // boot device codes:
|
---|
206 | // 0x00 : not defined
|
---|
207 | // 0x01 : first floppy
|
---|
208 | // 0x02 : first harddrive
|
---|
209 | // 0x03 : first cdrom
|
---|
210 | // 0x04 : local area network
|
---|
211 | // else : boot failure
|
---|
212 |
|
---|
213 | // Get the boot sequence
|
---|
214 | #if BX_ELTORITO_BOOT
|
---|
215 | bootseq=inb_cmos(0x3d);
|
---|
216 | bootseq|=((inb_cmos(0x38) & 0xf0) << 4);
|
---|
217 | bootseq|=((inb_cmos(0x3c) & 0x0f) << 12);
|
---|
218 | if (read_byte(ebda_seg, (uint16_t)&EbdaData->uForceBootDevice))
|
---|
219 | bootseq = read_byte(ebda_seg, (uint16_t)&EbdaData->uForceBootDevice);
|
---|
220 | /* Boot delay hack. */
|
---|
221 | if (bseqnr == 1)
|
---|
222 | delay_boot((inb_cmos(0x3c) & 0xf0) >> 4); /* Implemented in logo.c */
|
---|
223 |
|
---|
224 | if (bseqnr==2) bootseq >>= 4;
|
---|
225 | if (bseqnr==3) bootseq >>= 8;
|
---|
226 | if (bseqnr==4) bootseq >>= 12;
|
---|
227 | if (bootseq<0x10) lastdrive = 1;
|
---|
228 | bootdrv=0x00; bootcd=0;
|
---|
229 | bootlan=0;
|
---|
230 | BX_INFO("Boot : bseqnr=%d, bootseq=%x\r\n",bseqnr, bootseq);
|
---|
231 |
|
---|
232 | switch(bootseq & 0x0f) {
|
---|
233 | case 0x01:
|
---|
234 | bootdrv=0x00;
|
---|
235 | bootcd=0;
|
---|
236 | break;
|
---|
237 | case 0x02:
|
---|
238 | {
|
---|
239 | // Get the Boot drive.
|
---|
240 | uint8_t boot_drive = read_byte(ebda_seg, (uint16_t)&EbdaData->uForceBootDrive);
|
---|
241 |
|
---|
242 | bootdrv = boot_drive + 0x80;
|
---|
243 | bootcd=0;
|
---|
244 | break;
|
---|
245 | }
|
---|
246 | case 0x03:
|
---|
247 | bootdrv=0x00;
|
---|
248 | bootcd=1;
|
---|
249 | break;
|
---|
250 | case 0x04: bootlan=1; break;
|
---|
251 | default: return 0x00000000;
|
---|
252 | }
|
---|
253 | #else
|
---|
254 | bootseq=inb_cmos(0x2d);
|
---|
255 |
|
---|
256 | if (bseqnr==2) {
|
---|
257 | bootseq ^= 0x20;
|
---|
258 | lastdrive = 1;
|
---|
259 | }
|
---|
260 | bootdrv=0x00; bootcd=0;
|
---|
261 | if((bootseq&0x20)==0) bootdrv=0x80;
|
---|
262 | #endif // BX_ELTORITO_BOOT
|
---|
263 |
|
---|
264 | #if BX_ELTORITO_BOOT
|
---|
265 | // We have to boot from cd
|
---|
266 | if (bootcd != 0) {
|
---|
267 | status = cdrom_boot();
|
---|
268 |
|
---|
269 | // If failure
|
---|
270 | if ( (status & 0x00ff) !=0 ) {
|
---|
271 | print_cdromboot_failure(status);
|
---|
272 | print_boot_failure(bootcd, bootlan, bootdrv, 1, lastdrive);
|
---|
273 | return 0x00000000;
|
---|
274 | }
|
---|
275 |
|
---|
276 | bootseg = read_word(ebda_seg,(uint16_t)&EbdaData->cdemu.load_segment);
|
---|
277 | bootdrv = (uint8_t)(status>>8);
|
---|
278 | }
|
---|
279 |
|
---|
280 | #endif // BX_ELTORITO_BOOT
|
---|
281 |
|
---|
282 | // Check for boot from LAN first
|
---|
283 | if (bootlan == 1) {
|
---|
284 | uint8_t __far *fplan;
|
---|
285 |
|
---|
286 | fplan = MK_FP(VBOX_LANBOOT_SEG, 0);
|
---|
287 | if (*(uint16_t __far *)fplan == 0xaa55) {
|
---|
288 | pnp_exp_t __far *pnps;
|
---|
289 | uint32_t manuf;
|
---|
290 | void (__far *netboot_entry)(void);
|
---|
291 |
|
---|
292 | // This is NOT a generic PnP implementation, but an Etherboot-specific hack.
|
---|
293 | pnps = (void __far *)(fplan + *(uint16_t __far *)(fplan + 0x1a));
|
---|
294 | if (pnps->sig == 0x506e5024/* '$PnP' */) {
|
---|
295 | // Found PnP signature
|
---|
296 | manuf = *(uint32_t __far *)(fplan + pnps->mfg_string);
|
---|
297 | if (manuf == 0x65687445/* 'Ethe' */) {
|
---|
298 | // Found Etherboot ROM
|
---|
299 | print_boot_device(bootcd, bootlan, bootdrv);
|
---|
300 | netboot_entry = (void __far *)(fplan + 6);
|
---|
301 | netboot_entry();
|
---|
302 | }
|
---|
303 | else
|
---|
304 | {
|
---|
305 | //Found Normal Pnp ROM
|
---|
306 | print_boot_device(bootcd, bootlan, bootdrv);
|
---|
307 | int_enable(); /* Disabled as we were invoked via INT instruction. */
|
---|
308 | netboot_entry = (void __far *)(fplan + pnps->bev);
|
---|
309 | netboot_entry();
|
---|
310 | }
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | // boot from LAN will not return if successful.
|
---|
315 | print_boot_failure(bootcd, bootlan, bootdrv, 1, lastdrive);
|
---|
316 | return 0x00000000;
|
---|
317 | }
|
---|
318 |
|
---|
319 | // We have to boot from harddisk or floppy
|
---|
320 | if (bootcd == 0 && bootlan == 0) {
|
---|
321 | bootseg=0x07c0;
|
---|
322 |
|
---|
323 | status = read_boot_sec(bootdrv,bootseg);
|
---|
324 | if (status != 0) {
|
---|
325 | print_boot_failure(bootcd, bootlan, bootdrv, 1, lastdrive);
|
---|
326 | return 0x00000000;
|
---|
327 | }
|
---|
328 | }
|
---|
329 |
|
---|
330 | // There is *no* requirement whatsoever for a valid floppy boot sector
|
---|
331 | // to have a 55AAh signature. UNIX boot floppies typically have no such
|
---|
332 | // signature. In general, it is impossible to tell a valid bootsector
|
---|
333 | // from an invalid one.
|
---|
334 | // NB: It is somewhat common for failed OS installs to have the
|
---|
335 | // 0x55AA signature and a valid partition table but zeros in the
|
---|
336 | // rest of the boot sector. We do a quick check by comparing the first
|
---|
337 | // and third word of boot sector; if identical, the boot sector is
|
---|
338 | // extremely unlikely to be valid.
|
---|
339 | if (bootdrv != 0) bootchk = 0;
|
---|
340 | else bootchk = 1; /* disable 0x55AA signature check on drive A: */
|
---|
341 |
|
---|
342 | #if BX_ELTORITO_BOOT
|
---|
343 | // if boot from cd, no signature check
|
---|
344 | if (bootcd != 0)
|
---|
345 | bootchk = 1;
|
---|
346 | #endif // BX_ELTORITO_BOOT
|
---|
347 |
|
---|
348 | if (read_word(bootseg,0) == read_word(bootseg,4)
|
---|
349 | || (bootchk == 0 && read_word(bootseg,0x1fe) != 0xaa55))
|
---|
350 | {
|
---|
351 | print_boot_failure(bootcd, bootlan, bootdrv, 0, lastdrive);
|
---|
352 | return 0x00000000;
|
---|
353 | }
|
---|
354 |
|
---|
355 | #if BX_ELTORITO_BOOT
|
---|
356 | // Print out the boot string
|
---|
357 | print_boot_device(bootcd, bootlan, bootdrv);
|
---|
358 | #else // BX_ELTORITO_BOOT
|
---|
359 | print_boot_device(0, bootlan, bootdrv);
|
---|
360 | #endif // BX_ELTORITO_BOOT
|
---|
361 |
|
---|
362 | // return the boot segment
|
---|
363 | return (((uint32_t)bootdrv) << 16) + bootseg;
|
---|
364 | }
|
---|
365 |
|
---|