VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/BIOS/boot.c@ 62564

Last change on this file since 62564 was 62509, checked in by vboxsync, 8 years ago

(C) 2016

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette