1 ; -----------------------------------------------------------------------
3 ; Copyright 2003-2008 H. Peter Anvin - All Rights Reserved
5 ; Permission is hereby granted, free of charge, to any person
6 ; obtaining a copy of this software and associated documentation
7 ; files (the "Software"), to deal in the Software without
8 ; restriction, including without limitation the rights to use,
9 ; copy, modify, merge, publish, distribute, sublicense, and/or
10 ; sell copies of the Software, and to permit persons to whom
11 ; the Software is furnished to do so, subject to the following
14 ; The above copyright notice and this permission notice shall
15 ; be included in all copies or substantial portions of the Software.
17 ; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 ; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 ; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 ; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 ; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 ; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 ; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 ; OTHER DEALINGS IN THE SOFTWARE.
26 ; -----------------------------------------------------------------------
31 ; Simple Master Boot Record, including support for EBIOS extensions.
33 ; The MBR lives in front of the boot sector, and is responsible for
34 ; loading the boot sector of the active partition. The EBIOS support
35 ; is needed if the active partition starts beyond cylinder 1024.
37 ; This MBR determines all geometry info at runtime. It uses only the
38 ; linear block field in the partition table. It does, however, pass
39 ; the partition table information unchanged to the target OS.
41 ; This MBR should be "8086-clean", i.e. not require a 386.
47 ; Note: The MBR is actually loaded at 0:7C00h, but we quickly move it down to
62 mov si,sp ; Start address
63 mov di,0600h ; Destination address
68 ; Now, jump to the copy at 0600h so we can load the boot sector at 7C00h.
69 ; Since some BIOSes seem to think 0000:7C00h and 07C0:0000h are the same
70 ; thing, use a far jump to canonicalize the address. This also makes
71 ; sure that it is a code speculation barrier.
74 jmp 0:next
; Jump to copy at 0600h
77 mov [DriveNo
], dl ; Drive number stored in DL
79 ; Check for CHS parameters. This doesn't work on floppy disks,
80 ; but for an MBR we don't care.
82 mov ah,08h ; Get drive parameters
84 and cx,3Fh
; Max sector number
88 inc ax ; From 0-based to count
89 mul cx ; Heads*Sectors
91 ; Note: we actually don't care about the number of
92 ; cylinders, since that's the highest-order division
95 ; Now look for one (and only one) active partition.
105 .
notactive: add si,byte 16
108 cmp ax,byte 1 ; Better be only one
109 jnz not_one_partition
112 ; Now we have the active partition partition information in DS:DI.
113 ; Check to see if we support EBIOS.
125 test cl,1 ; LBA device access
128 ; We have EBIOS. Load the boot sector using LBA.
132 mov bx,[di+8] ; Copy the block address
137 mov ah,42h ; Extended Read
138 jmp short common_tail
140 ; No EBIOS. Load the boot sector using CHS.
146 div word [SecPerCyl
] ; AX = cylinder DX = sec in cyl
150 mov ch,al ; CL = cyl[9:8], CH = cyl[7:0]
153 div byte [Sectors
] ; AL = head AH = sector
156 or cl,ah ; CX = cylinder and sector
160 mov ax,0201h ; Read one sector
164 pop si ; DS:SI -> partition table entry
166 ; Verify that we have a boot sector, jump
168 cmp word [7C00h
+510],0AA55h
171 jmp 0:7C00h
; Jump to boot sector; far
172 ; jump is speculation barrier
173 ; (Probably not neecessary, but
174 ; there is plenty of space.)
179 mov si,missing_os_msg
189 mov ah,0Eh
; TTY output
190 mov bh,[BIOS_page
] ; Current page
197 align 4, db 0 ; Begin data area
200 ; EBIOS disk address packet
204 .
count: dw 1 ; Block count
205 .
off: dw 7C00h
; Offset of buffer
206 .
seg: dw 0 ; Segment of buffer
207 .
lba: dd 0 ; LBA (LSW)
211 SecPerCyl: dw 0 ; Heads*Sectors
215 missing_os_msg
db 'Missing operating system', 13, 10, 0
216 bad_disk_msg
db 'Operating system loading error', 13, 10, 0
219 ; Maximum MBR size: 446 bytes; end-of-boot-sector signature also needed.
220 ; Note that some operating systems (NT, DR-DOS) put additional stuff at
221 ; the end of the MBR, so shorter is better. Location 440 is known to
222 ; have a 4-byte attempt-at-unique-ID for some OSes.
225 PartitionTable
equ $$
+446 ; Start of partition table
228 ; BSS data; put at 800h