[69501] | 1 | /* $Id: boot.c 101319 2023-09-29 15:25:06Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * PC BIOS - ???
|
---|
| 4 | */
|
---|
| 5 |
|
---|
[38699] | 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[38699] | 8 | *
|
---|
[96407] | 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
|
---|
[69498] | 26 | * --------------------------------------------------------------------
|
---|
[38699] | 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 |
|
---|
[69501] | 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 | */
|
---|
[38699] | 64 |
|
---|
[69501] | 65 |
|
---|
[38699] | 66 | #include <stdint.h>
|
---|
| 67 | #include <string.h>
|
---|
| 68 | #include "inlines.h"
|
---|
| 69 | #include "biosint.h"
|
---|
| 70 | #include "ebda.h"
|
---|
| 71 |
|
---|
[54451] | 72 | /* Sanity check the LAN boot segment definition. */
|
---|
| 73 | #if VBOX_LANBOOT_SEG < 0xA000
|
---|
| 74 | #error VBOX_LANBOOT_SEG incorrect!
|
---|
| 75 | #endif
|
---|
[38699] | 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 |
|
---|
[63562] | 118 | /// @todo pass inputs as bit flags rather than bytes?
|
---|
[38699] | 119 | void print_boot_device(uint8_t cdboot, uint8_t lanboot, uint8_t drive)
|
---|
| 120 | {
|
---|
| 121 | int i;
|
---|
[48123] | 122 |
|
---|
[38699] | 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
|
---|
[48123] | 126 |
|
---|
[38699] | 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;
|
---|
[48123] | 132 |
|
---|
[38699] | 133 | BX_INFO("Booting from %s...\n",drivetypes[i]);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | //--------------------------------------------------------------------------
|
---|
| 137 | // print_boot_failure
|
---|
| 138 | // displays the reason why boot failed
|
---|
| 139 | //--------------------------------------------------------------------------
|
---|
[63562] | 140 | /// @todo pass inputs as bit flags rather than bytes?
|
---|
[48123] | 141 | void print_boot_failure(uint8_t cdboot, uint8_t lanboot, uint8_t drive,
|
---|
[38699] | 142 | uint8_t reason, uint8_t lastdrive)
|
---|
| 143 | {
|
---|
| 144 | uint16_t drivenum = drive&0x7f;
|
---|
[48123] | 145 |
|
---|
[38699] | 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
|
---|
[48123] | 151 |
|
---|
[38699] | 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);
|
---|
[48123] | 160 |
|
---|
[38699] | 161 | if (lastdrive==1) {
|
---|
| 162 | if (reason==0)
|
---|
[84829] | 163 | BX_INFO_CON("No bootable medium found!\n");
|
---|
[38699] | 164 | else
|
---|
[84829] | 165 | BX_INFO_CON("Could not read from the boot medium!\n");
|
---|
| 166 | BX_INFO_CON("Please insert a bootable medium and reboot.\n");
|
---|
[38699] | 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 |
|
---|
[101319] | 180 |
|
---|
| 181 | #define BOOT_CHK_WORDS 8
|
---|
| 182 |
|
---|
| 183 | /* Check if the first n words of a boot sector
|
---|
| 184 | * are identical. Only consider the boot sector
|
---|
| 185 | * valid if they're not.
|
---|
| 186 | */
|
---|
| 187 | static int valid_bootsect(uint16_t __far *boot)
|
---|
| 188 | {
|
---|
| 189 | int i;
|
---|
| 190 | uint16_t word1;
|
---|
| 191 |
|
---|
| 192 | word1 = boot[0];
|
---|
| 193 | for (i = 1; i < BOOT_CHK_WORDS; ++i) {
|
---|
| 194 | if (word1 != boot[i])
|
---|
| 195 | break;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | return i != BOOT_CHK_WORDS;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[38699] | 201 | // returns bootsegment in ax, drive in bl
|
---|
| 202 | uint32_t BIOSCALL int19_function(uint8_t bseqnr)
|
---|
| 203 | {
|
---|
[63562] | 204 | /// @todo common code for getting the EBDA segment
|
---|
[38699] | 205 | uint16_t ebda_seg=read_word(0x0040,0x000E);
|
---|
| 206 | uint16_t bootseq;
|
---|
| 207 | uint8_t bootdrv;
|
---|
| 208 | uint8_t bootcd;
|
---|
| 209 | uint8_t bootlan;
|
---|
| 210 | uint8_t bootchk;
|
---|
| 211 | uint16_t bootseg;
|
---|
| 212 | uint16_t status;
|
---|
| 213 | uint8_t lastdrive=0;
|
---|
[48123] | 214 |
|
---|
[38699] | 215 | // if BX_ELTORITO_BOOT is not defined, old behavior
|
---|
| 216 | // check bit 5 in CMOS reg 0x2d. load either 0x00 or 0x80 into DL
|
---|
| 217 | // in preparation for the initial INT 13h (0=floppy A:, 0x80=C:)
|
---|
| 218 | // 0: system boot sequence, first drive C: then A:
|
---|
| 219 | // 1: system boot sequence, first drive A: then C:
|
---|
| 220 | // else BX_ELTORITO_BOOT is defined
|
---|
| 221 | // CMOS regs 0x3D and 0x38 contain the boot sequence:
|
---|
| 222 | // CMOS reg 0x3D & 0x0f : 1st boot device
|
---|
| 223 | // CMOS reg 0x3D & 0xf0 : 2nd boot device
|
---|
| 224 | // CMOS reg 0x38 & 0xf0 : 3rd boot device
|
---|
| 225 | // CMOS reg 0x3C & 0x0f : 4th boot device
|
---|
| 226 | // boot device codes:
|
---|
| 227 | // 0x00 : not defined
|
---|
| 228 | // 0x01 : first floppy
|
---|
| 229 | // 0x02 : first harddrive
|
---|
| 230 | // 0x03 : first cdrom
|
---|
| 231 | // 0x04 : local area network
|
---|
| 232 | // else : boot failure
|
---|
[48123] | 233 |
|
---|
[38699] | 234 | // Get the boot sequence
|
---|
| 235 | #if BX_ELTORITO_BOOT
|
---|
| 236 | bootseq=inb_cmos(0x3d);
|
---|
| 237 | bootseq|=((inb_cmos(0x38) & 0xf0) << 4);
|
---|
| 238 | bootseq|=((inb_cmos(0x3c) & 0x0f) << 12);
|
---|
| 239 | if (read_byte(ebda_seg, (uint16_t)&EbdaData->uForceBootDevice))
|
---|
| 240 | bootseq = read_byte(ebda_seg, (uint16_t)&EbdaData->uForceBootDevice);
|
---|
| 241 | /* Boot delay hack. */
|
---|
| 242 | if (bseqnr == 1)
|
---|
| 243 | delay_boot((inb_cmos(0x3c) & 0xf0) >> 4); /* Implemented in logo.c */
|
---|
[48123] | 244 |
|
---|
[38699] | 245 | if (bseqnr==2) bootseq >>= 4;
|
---|
| 246 | if (bseqnr==3) bootseq >>= 8;
|
---|
| 247 | if (bseqnr==4) bootseq >>= 12;
|
---|
| 248 | if (bootseq<0x10) lastdrive = 1;
|
---|
| 249 | bootdrv=0x00; bootcd=0;
|
---|
| 250 | bootlan=0;
|
---|
[48123] | 251 | BX_INFO("Boot : bseqnr=%d, bootseq=%x\r\n",bseqnr, bootseq);
|
---|
| 252 |
|
---|
[38699] | 253 | switch(bootseq & 0x0f) {
|
---|
| 254 | case 0x01:
|
---|
| 255 | bootdrv=0x00;
|
---|
| 256 | bootcd=0;
|
---|
| 257 | break;
|
---|
| 258 | case 0x02:
|
---|
| 259 | {
|
---|
| 260 | // Get the Boot drive.
|
---|
| 261 | uint8_t boot_drive = read_byte(ebda_seg, (uint16_t)&EbdaData->uForceBootDrive);
|
---|
[48123] | 262 |
|
---|
[38699] | 263 | bootdrv = boot_drive + 0x80;
|
---|
| 264 | bootcd=0;
|
---|
| 265 | break;
|
---|
| 266 | }
|
---|
| 267 | case 0x03:
|
---|
| 268 | bootdrv=0x00;
|
---|
| 269 | bootcd=1;
|
---|
| 270 | break;
|
---|
| 271 | case 0x04: bootlan=1; break;
|
---|
| 272 | default: return 0x00000000;
|
---|
| 273 | }
|
---|
| 274 | #else
|
---|
| 275 | bootseq=inb_cmos(0x2d);
|
---|
[48123] | 276 |
|
---|
[38699] | 277 | if (bseqnr==2) {
|
---|
| 278 | bootseq ^= 0x20;
|
---|
| 279 | lastdrive = 1;
|
---|
| 280 | }
|
---|
| 281 | bootdrv=0x00; bootcd=0;
|
---|
| 282 | if((bootseq&0x20)==0) bootdrv=0x80;
|
---|
| 283 | #endif // BX_ELTORITO_BOOT
|
---|
[48123] | 284 |
|
---|
[38699] | 285 | #if BX_ELTORITO_BOOT
|
---|
| 286 | // We have to boot from cd
|
---|
| 287 | if (bootcd != 0) {
|
---|
| 288 | status = cdrom_boot();
|
---|
[48123] | 289 |
|
---|
[38699] | 290 | // If failure
|
---|
| 291 | if ( (status & 0x00ff) !=0 ) {
|
---|
| 292 | print_cdromboot_failure(status);
|
---|
| 293 | print_boot_failure(bootcd, bootlan, bootdrv, 1, lastdrive);
|
---|
| 294 | return 0x00000000;
|
---|
| 295 | }
|
---|
[48123] | 296 |
|
---|
[38699] | 297 | bootseg = read_word(ebda_seg,(uint16_t)&EbdaData->cdemu.load_segment);
|
---|
| 298 | bootdrv = (uint8_t)(status>>8);
|
---|
| 299 | }
|
---|
[48123] | 300 |
|
---|
[38699] | 301 | #endif // BX_ELTORITO_BOOT
|
---|
[48123] | 302 |
|
---|
[38699] | 303 | // Check for boot from LAN first
|
---|
| 304 | if (bootlan == 1) {
|
---|
| 305 | uint8_t __far *fplan;
|
---|
| 306 |
|
---|
| 307 | fplan = MK_FP(VBOX_LANBOOT_SEG, 0);
|
---|
| 308 | if (*(uint16_t __far *)fplan == 0xaa55) {
|
---|
| 309 | pnp_exp_t __far *pnps;
|
---|
| 310 | uint32_t manuf;
|
---|
| 311 | void (__far *netboot_entry)(void);
|
---|
| 312 |
|
---|
| 313 | // This is NOT a generic PnP implementation, but an Etherboot-specific hack.
|
---|
| 314 | pnps = (void __far *)(fplan + *(uint16_t __far *)(fplan + 0x1a));
|
---|
| 315 | if (pnps->sig == 0x506e5024/* '$PnP' */) {
|
---|
| 316 | // Found PnP signature
|
---|
| 317 | manuf = *(uint32_t __far *)(fplan + pnps->mfg_string);
|
---|
| 318 | if (manuf == 0x65687445/* 'Ethe' */) {
|
---|
| 319 | // Found Etherboot ROM
|
---|
| 320 | print_boot_device(bootcd, bootlan, bootdrv);
|
---|
| 321 | netboot_entry = (void __far *)(fplan + 6);
|
---|
| 322 | netboot_entry();
|
---|
[48123] | 323 | }
|
---|
[42202] | 324 | else
|
---|
| 325 | {
|
---|
| 326 | //Found Normal Pnp ROM
|
---|
[38699] | 327 | print_boot_device(bootcd, bootlan, bootdrv);
|
---|
| 328 | int_enable(); /* Disabled as we were invoked via INT instruction. */
|
---|
| 329 | netboot_entry = (void __far *)(fplan + pnps->bev);
|
---|
| 330 | netboot_entry();
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
[48123] | 334 |
|
---|
[38699] | 335 | // boot from LAN will not return if successful.
|
---|
| 336 | print_boot_failure(bootcd, bootlan, bootdrv, 1, lastdrive);
|
---|
| 337 | return 0x00000000;
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | // We have to boot from harddisk or floppy
|
---|
| 341 | if (bootcd == 0 && bootlan == 0) {
|
---|
| 342 | bootseg=0x07c0;
|
---|
[48123] | 343 |
|
---|
[38699] | 344 | status = read_boot_sec(bootdrv,bootseg);
|
---|
| 345 | if (status != 0) {
|
---|
| 346 | print_boot_failure(bootcd, bootlan, bootdrv, 1, lastdrive);
|
---|
| 347 | return 0x00000000;
|
---|
| 348 | }
|
---|
| 349 | }
|
---|
[48123] | 350 |
|
---|
[40754] | 351 | // There is *no* requirement whatsoever for a valid floppy boot sector
|
---|
| 352 | // to have a 55AAh signature. UNIX boot floppies typically have no such
|
---|
[38699] | 353 | // signature. In general, it is impossible to tell a valid bootsector
|
---|
| 354 | // from an invalid one.
|
---|
| 355 | // NB: It is somewhat common for failed OS installs to have the
|
---|
| 356 | // 0x55AA signature and a valid partition table but zeros in the
|
---|
[101319] | 357 | // rest of the boot sector. We check the first few words; if identical,
|
---|
| 358 | // the boot sector is extremely unlikely to be valid.
|
---|
[38699] | 359 | if (bootdrv != 0) bootchk = 0;
|
---|
| 360 | else bootchk = 1; /* disable 0x55AA signature check on drive A: */
|
---|
[48123] | 361 |
|
---|
[38699] | 362 | #if BX_ELTORITO_BOOT
|
---|
| 363 | // if boot from cd, no signature check
|
---|
| 364 | if (bootcd != 0)
|
---|
| 365 | bootchk = 1;
|
---|
| 366 | #endif // BX_ELTORITO_BOOT
|
---|
[48123] | 367 |
|
---|
[101319] | 368 | if (!valid_bootsect(MK_FP(bootseg,0))
|
---|
[40754] | 369 | || (bootchk == 0 && read_word(bootseg,0x1fe) != 0xaa55))
|
---|
| 370 | {
|
---|
| 371 | print_boot_failure(bootcd, bootlan, bootdrv, 0, lastdrive);
|
---|
| 372 | return 0x00000000;
|
---|
[38699] | 373 | }
|
---|
[48123] | 374 |
|
---|
[38699] | 375 | #if BX_ELTORITO_BOOT
|
---|
| 376 | // Print out the boot string
|
---|
| 377 | print_boot_device(bootcd, bootlan, bootdrv);
|
---|
| 378 | #else // BX_ELTORITO_BOOT
|
---|
| 379 | print_boot_device(0, bootlan, bootdrv);
|
---|
| 380 | #endif // BX_ELTORITO_BOOT
|
---|
[48123] | 381 |
|
---|
[38699] | 382 | // return the boot segment
|
---|
| 383 | return (((uint32_t)bootdrv) << 16) + bootseg;
|
---|
| 384 | }
|
---|
[58841] | 385 |
|
---|