1 | ; $Id: bs3-bootsector.asm 59975 2016-03-09 23:00:02Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; Generic bootsector for BS3.
|
---|
4 | ;
|
---|
5 | ; This sets up stack at %fff0 and loads the next sectors from the floppy at
|
---|
6 | ; %10000 (1000:0000 in real mode), then starts executing at cs:ip=1000:0000.
|
---|
7 | ;
|
---|
8 |
|
---|
9 | ;
|
---|
10 | ; Copyright (C) 2007-2015 Oracle Corporation
|
---|
11 | ;
|
---|
12 | ; This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
13 | ; available from http://www.virtualbox.org. This file is free software;
|
---|
14 | ; you can redistribute it and/or modify it under the terms of the GNU
|
---|
15 | ; General Public License (GPL) as published by the Free Software
|
---|
16 | ; Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
17 | ; VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
18 | ; hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
19 | ;
|
---|
20 | ; The contents of this file may alternatively be used under the terms
|
---|
21 | ; of the Common Development and Distribution License Version 1.0
|
---|
22 | ; (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
23 | ; VirtualBox OSE distribution, in which case the provisions of the
|
---|
24 | ; CDDL are applicable instead of those of the GPL.
|
---|
25 | ;
|
---|
26 | ; You may elect to license modified versions of this file under the
|
---|
27 | ; terms and conditions of either the GPL or the CDDL or both.
|
---|
28 | ;
|
---|
29 |
|
---|
30 |
|
---|
31 |
|
---|
32 | ;*********************************************************************************************************************************
|
---|
33 | ;* Header Files *
|
---|
34 | ;*********************************************************************************************************************************
|
---|
35 | %include "bs3kit.mac"
|
---|
36 | %include "iprt/asmdefs.mac"
|
---|
37 | %include "iprt/x86.mac"
|
---|
38 |
|
---|
39 |
|
---|
40 | %ifdef __YASM__
|
---|
41 | [map all]
|
---|
42 | %endif
|
---|
43 |
|
---|
44 | ;
|
---|
45 | ; Start with a jump just to follow the convention.
|
---|
46 | ; Also declare all segments/sections to establish them and their order.
|
---|
47 | ;
|
---|
48 | ORG 07c00h
|
---|
49 |
|
---|
50 | BITS 16
|
---|
51 | start:
|
---|
52 | jmp short bs3InitCode
|
---|
53 | db 0ah ; Should be nop, but this looks better.
|
---|
54 | g_OemId: ; 003h
|
---|
55 | db 'BS3Kit', 0ah, 0ah
|
---|
56 |
|
---|
57 | ;
|
---|
58 | ; DOS 4.0 Extended Bios Parameter Block:
|
---|
59 | ;
|
---|
60 | g_cBytesPerSector: ; 00bh
|
---|
61 | dw 512
|
---|
62 | g_cSectorsPerCluster: ; 00dh
|
---|
63 | db 1
|
---|
64 | g_cReservedSectors: ; 00eh
|
---|
65 | dw 1
|
---|
66 | g_cFATs: ; 010h
|
---|
67 | db 0
|
---|
68 | g_cRootDirEntries: ; 011h
|
---|
69 | dw 0
|
---|
70 | g_cTotalSectors: ; 013h
|
---|
71 | dw 0
|
---|
72 | g_bMediaDescriptor: ; 015h
|
---|
73 | db 0
|
---|
74 | g_cSectorsPerFAT: ; 016h
|
---|
75 | dw 0
|
---|
76 | g_cPhysSectorsPerTrack: ; 018h
|
---|
77 | dw 18
|
---|
78 | g_cHeads: ; 01ah
|
---|
79 | dw 2
|
---|
80 | g_cHiddentSectors: ; 01ch
|
---|
81 | dd 1
|
---|
82 | g_cLargeTotalSectors: ; 020h - We (ab)use this to indicate the number of sectors to load.
|
---|
83 | dd 0
|
---|
84 | g_bBootDrv: ; 024h
|
---|
85 | db 80h
|
---|
86 | g_bFlagsEtc: ; 025h
|
---|
87 | db 0
|
---|
88 | g_bExtendedSignature: ; 026h
|
---|
89 | db 0x29
|
---|
90 | g_dwSerialNumber: ; 027h
|
---|
91 | dd 0x0a458634
|
---|
92 | g_abLabel: ; 02bh
|
---|
93 | db 'VirtualBox', 0ah
|
---|
94 | g_abFSType: ; 036h
|
---|
95 | db 'RawCode', 0ah
|
---|
96 | g_BpbEnd: ; 03ch
|
---|
97 |
|
---|
98 |
|
---|
99 | ;
|
---|
100 | ; Where to real init code starts.
|
---|
101 | ;
|
---|
102 | bs3InitCode:
|
---|
103 | cli
|
---|
104 |
|
---|
105 | ; save the registers.
|
---|
106 | mov [cs:BS3_ADDR_REG_SAVE + BS3REGCTX.rax], eax
|
---|
107 | mov [cs:BS3_ADDR_REG_SAVE + BS3REGCTX.rsp], esp
|
---|
108 | mov [cs:BS3_ADDR_REG_SAVE + BS3REGCTX.rbp], ebp
|
---|
109 | mov ax, ss
|
---|
110 | mov [cs:BS3_ADDR_REG_SAVE + BS3REGCTX.ss], ax
|
---|
111 | mov ax, ds
|
---|
112 | mov [cs:BS3_ADDR_REG_SAVE + BS3REGCTX.ds], ax
|
---|
113 | mov ax, es
|
---|
114 | mov [cs:BS3_ADDR_REG_SAVE + BS3REGCTX.es], ax
|
---|
115 | mov ax, fs
|
---|
116 | mov [cs:BS3_ADDR_REG_SAVE + BS3REGCTX.fs], ax
|
---|
117 | mov ax, gs
|
---|
118 |
|
---|
119 | ; set up the segment reisters and stack.
|
---|
120 | xor eax, eax
|
---|
121 | mov ds, ax
|
---|
122 | mov es, ax
|
---|
123 | mov fs, ax
|
---|
124 | mov gs, ax
|
---|
125 | mov ss, ax
|
---|
126 | mov esp, BS3_ADDR_STACK
|
---|
127 | mov ebp, esp
|
---|
128 | mov [ebp], eax ; clear the first 16 bytes (terminates the ebp chain)
|
---|
129 | mov [ebp + 04h], eax
|
---|
130 | mov [ebp + 08h], eax
|
---|
131 | mov [ebp + 0ch], eax
|
---|
132 |
|
---|
133 | ; Save more registers now that ds is known and the stack is usable.
|
---|
134 | pushfd
|
---|
135 | pop eax
|
---|
136 | mov [BS3_ADDR_REG_SAVE + BS3REGCTX.rflags], eax
|
---|
137 | mov [BS3_ADDR_REG_SAVE + BS3REGCTX.rbx], ebx
|
---|
138 | mov [BS3_ADDR_REG_SAVE + BS3REGCTX.rcx], ecx
|
---|
139 | mov [BS3_ADDR_REG_SAVE + BS3REGCTX.rdx], edx
|
---|
140 | mov [BS3_ADDR_REG_SAVE + BS3REGCTX.rsi], esi
|
---|
141 | mov [BS3_ADDR_REG_SAVE + BS3REGCTX.rdi], edi
|
---|
142 | mov eax, cr2
|
---|
143 | mov [BS3_ADDR_REG_SAVE + BS3REGCTX.cr2], eax
|
---|
144 | mov eax, cr3
|
---|
145 | mov [BS3_ADDR_REG_SAVE + BS3REGCTX.cr3], eax
|
---|
146 | mov eax, cr4
|
---|
147 | mov [BS3_ADDR_REG_SAVE + BS3REGCTX.cr4], eax
|
---|
148 | mov byte [BS3_ADDR_REG_SAVE + BS3REGCTX.bMode], BS3_MODE_RM
|
---|
149 | xor eax, eax
|
---|
150 | mov [cs:BS3_ADDR_REG_SAVE + BS3REGCTX.cs], ax
|
---|
151 | mov ax, start
|
---|
152 | mov [cs:BS3_ADDR_REG_SAVE + BS3REGCTX.rip], eax
|
---|
153 |
|
---|
154 | ; Make sure caching is enabled and alignment is off.
|
---|
155 | mov eax, cr0
|
---|
156 | mov [BS3_ADDR_REG_SAVE + BS3REGCTX.cr0], eax
|
---|
157 | and eax, ~(X86_CR0_NW | X86_CR0_CD | X86_CR0_AM)
|
---|
158 | mov cr0, eax
|
---|
159 |
|
---|
160 | ; Load all the code.
|
---|
161 | mov [g_bBootDrv], dl
|
---|
162 | call bs3InitLoadImage
|
---|
163 |
|
---|
164 | ;
|
---|
165 | ; Call the user 'main' procedure (shouldn't return).
|
---|
166 | ;
|
---|
167 | call BS3_SEL_TEXT16:0000h
|
---|
168 |
|
---|
169 | ; Panic/hang.
|
---|
170 | Bs3Panic:
|
---|
171 | cli
|
---|
172 | hlt
|
---|
173 | jmp Bs3Panic
|
---|
174 |
|
---|
175 |
|
---|
176 |
|
---|
177 | ;;
|
---|
178 | ; Loads the image off the floppy.
|
---|
179 | ;
|
---|
180 | ; This uses g_cLargeTotalSectors to figure out how much to load.
|
---|
181 | ;
|
---|
182 | ; Clobbers everything except ebp and esp. Panics on failure.
|
---|
183 | ;
|
---|
184 | ; @param dl The boot drive number (from BIOS).
|
---|
185 | ; @uses ax, cx, bx, esi, di
|
---|
186 | ;
|
---|
187 | BEGINPROC bs3InitLoadImage
|
---|
188 | push bp
|
---|
189 | mov bp, sp
|
---|
190 | push es
|
---|
191 | %define bSavedDiskNo byte [bp - 04h]
|
---|
192 | push dx
|
---|
193 | %define bMaxSector byte [bp - 06h]
|
---|
194 | push 0
|
---|
195 | %define bMaxHead byte [bp - 08h]
|
---|
196 | push 0
|
---|
197 | %define bMaxCylinder byte [bp - 0ah]
|
---|
198 | push 0
|
---|
199 |
|
---|
200 | ;
|
---|
201 | ; Try figure the geometry.
|
---|
202 | ;
|
---|
203 | mov ah, 08h
|
---|
204 | int 13h
|
---|
205 | jc .failure
|
---|
206 | mov bMaxSector, cl
|
---|
207 | mov bMaxHead, dh
|
---|
208 | mov bMaxCylinder, ch
|
---|
209 | mov dl, bSavedDiskNo
|
---|
210 |
|
---|
211 | ;
|
---|
212 | ; Reload all the sectors one at a time (avoids problems).
|
---|
213 | ;
|
---|
214 | mov esi, [g_cLargeTotalSectors]
|
---|
215 | dec esi
|
---|
216 | mov di, BS3_ADDR_LOAD / 16 ; The current load segment.
|
---|
217 | mov cx, 0002h ; ch/cylinder=0 (0-based); cl/sector=2 (1-based)
|
---|
218 | xor dh, dh ; dh/head=0
|
---|
219 | .the_load_loop:
|
---|
220 | xor bx, bx
|
---|
221 | mov es, di ; es:bx -> buffer
|
---|
222 | mov ax, 0201h ; al=1 sector; ah=read function
|
---|
223 | int 13h
|
---|
224 | jc .failure
|
---|
225 |
|
---|
226 | ; advance to the next sector/head/cylinder.
|
---|
227 | inc cl
|
---|
228 | cmp cl, bMaxSector
|
---|
229 | jbe .adv_addr
|
---|
230 |
|
---|
231 | mov cl, 1
|
---|
232 | inc dh
|
---|
233 | cmp dh, bMaxHead
|
---|
234 | jbe .adv_addr
|
---|
235 |
|
---|
236 | mov dh, 0
|
---|
237 | inc ch
|
---|
238 |
|
---|
239 | .adv_addr:
|
---|
240 | add di, 512 / 16
|
---|
241 | dec si
|
---|
242 | jnz .the_load_loop
|
---|
243 |
|
---|
244 | add sp, 3*2
|
---|
245 | pop dx
|
---|
246 | pop es
|
---|
247 | leave
|
---|
248 | ret
|
---|
249 |
|
---|
250 | ;
|
---|
251 | ; Something went wrong, display a message.
|
---|
252 | ;
|
---|
253 | .failure:
|
---|
254 | pusha
|
---|
255 |
|
---|
256 | ; print message
|
---|
257 | mov si, .s_szErrMsg
|
---|
258 | mov ah, 0eh
|
---|
259 | xor bx, bx
|
---|
260 | .failure_next_char:
|
---|
261 | lodsb
|
---|
262 | int 10h
|
---|
263 | cmp si, .s_szErrMsgEnd
|
---|
264 | jb .failure_next_char
|
---|
265 |
|
---|
266 | ; format the error number.
|
---|
267 | movzx bx, byte [bp - 2 - 1] ; read the ah of the pusha frame
|
---|
268 | shr bl, 4
|
---|
269 | mov al, [bx + .s_achHex]
|
---|
270 | int 10h
|
---|
271 |
|
---|
272 | movzx bx, byte [bp - 2 - 1] ; read the ah of the pusha frame
|
---|
273 | and bl, 0fh
|
---|
274 | mov al, [bx + .s_achHex]
|
---|
275 | int 10h
|
---|
276 |
|
---|
277 | ; panic
|
---|
278 | popa
|
---|
279 | call Bs3Panic
|
---|
280 | .s_szErrMsg:
|
---|
281 | db 13, 10, 'read error: '
|
---|
282 | .s_szErrMsgEnd:
|
---|
283 | .s_achHex:
|
---|
284 | db '0123456789abcdef'
|
---|
285 | ENDPROC bs3InitLoadImage
|
---|
286 |
|
---|
287 |
|
---|
288 | ;
|
---|
289 | ; Pad the remainder of the sector with int3's and end it with the DOS signature.
|
---|
290 | ;
|
---|
291 | bs3Padding:
|
---|
292 | times ( 510 - ( (bs3Padding - start) % 512 ) ) db 0cch
|
---|
293 | db 055h, 0aah
|
---|
294 |
|
---|