File: mbrfat.asm

package info (click to toggle)
makebootfat 1.4-8
  • links: PTS
  • area: main
  • in suites: bullseye, sid
  • size: 2,804 kB
  • sloc: sh: 3,118; ansic: 2,746; asm: 148; makefile: 92
file content (286 lines) | stat: -rw-r--r-- 6,470 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
; -----------------------------------------------------------------------
;   
;   Copyright 2003-2004 H. Peter Anvin - All Rights Reserved
;   Copyright 2004-2005 Andrea Mazzoleni - All Rights Reserved
;
;   Permission is hereby granted, free of charge, to any person
;   obtaining a copy of this software and associated documentation
;   files (the "Software"), to deal in the Software without
;   restriction, including without limitation the rights to use,
;   copy, modify, merge, publish, distribute, sublicense, and/or
;   sell copies of the Software, and to permit persons to whom
;   the Software is furnished to do so, subject to the following
;   conditions:
;   
;   The above copyright notice and this permission notice shall
;   be included in all copies or substantial portions of the Software.
;   
;   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
;   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
;   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
;   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
;   OTHER DEALINGS IN THE SOFTWARE.
;
; -----------------------------------------------------------------------

;
; mbr.asm
;
; Simple Master Boot Record, including support for EBIOS extensions.
; 
; The MBR lives in front of the boot sector, and is responsible for
; loading the boot sector of the active partition.  The EBIOS support
; is needed if the active partition starts beyond cylinder 1024.
; 
; This MBR determines all geometry info at runtime.  It uses only the
; linear block field in the partition table.  It does, however, pass
; the partition table information unchanged to the target OS.
;
; This MBR should be "8086-clean", i.e. not require a 386.
;

		absolute 0462h
BIOS_page	resb 1			; Current video page

;
; Note: The MBR is actually loaded at 0:7C00h, but we quickly move it down to
; 0600h.
;
		section .text
		cpu 8086
		org 0600h

;
; Standard FAT jump signature
;
		jmp short _start
		nop

%macro	zb	1.nolist
	times %1 db 0
%endmacro

;
; Allocate space for the FAT12/16/32 boot sector
;
FATBootSector	zb 87

;
; Real MBR code
;
_start:		cli
		xor ax,ax
		mov ds,ax
		mov es,ax
		mov ss,ax
		mov sp,7C00h
		sti
		cld
		mov si,sp		; Start address
		mov di,0600h		; Destination address
		mov cx,512/2
		rep movsw

;
; Now, jump to the copy at 0600h so we can load the boot sector at 7C00h.
; Since some BIOSes seem to think 0000:7C00h and 07C0:0000h are the same
; thing, use a far jump to canonicalize the address.  This also makes
; sure that it is a code speculation barrier.
;

		jmp 0:next		; Jump to copy at 0600h
				
next:
		mov [DriveNo], dl		; Drive number stored in DL

;
; Print FDD/HDD
;
		mov si,fdd_msg
		test dl,80h
		jz fdddevice
		mov si,hdd_msg
fdddevice:
		call print_message

;
; Check for CHS parameters.  This doesn't work on floppy disks,
; but for an MBR we don't care.
;
		mov ah,08h			; Get drive parameters
		int 13h
		xor ax,ax
		mov al,dh
		inc ax				; From 0-based to count
		mov [Heads],ax
		and cl,3Fh			; Max sector number
		mov [Sectors],cl
		; Note: we actually don't care about the number of
		; cylinders, since that's the highest-order division

;
; Now look for one (and only one) active partition.
;
		mov si,PartitionTable
		xor ax,ax
		mov cx,4
checkpartloop:
		test byte [si],80h
		jz .notactive
		inc ax
		mov di,si
.notactive:	add si,byte 16
		loop checkpartloop

		cmp ax,byte 1			; Better be only one
		jnz not_one_partition

;
; Now we have the active partition partition information in DS:DI.
; Check to see if we support EBIOS.
;
		mov dl,[DriveNo]
		mov ax,4100h
		mov bx,055AAh
		xor cx,cx
		xor dh,dh
		stc
		int 13h
		jc no_ebios
		cmp bx,0AA55h
		jne no_ebios
		test cl,1			; LBA device access
		jz no_ebios

;
; Print EBIOS
;
		mov si,ebios_msg
		call print_message

;
; We have EBIOS.  Load the boot sector using LBA.
;
		push di
		mov si,dapa
		mov bx,[di+8]			; Copy the block address
		mov [si+8],bx
		mov bx,[di+10]
		mov [si+10],bx
		mov dl,[DriveNo]
		mov ax,ds			; Restore the es register modified by the 13/08 call for floppy
		mov es,ax
		mov ax,4200h			; Extended Read
		jmp short common_tail

;
; Print CRLF
;
no_ebios:
		mov si,crlf_msg
		call print_message

;
; No EBIOS.  Load the boot sector using CHS.
;
		push di
		mov ax,[di+8]
		mov dx,[di+10]
		div word [Sectors]
		inc dx
		mov cx,dx			; Sector #
		xor dx,dx
		div word [Heads]
		; DX = head #, AX = cylinder #
		mov ch,al
		shr ax,1
		shr ax,1
		and al,0C0h
		or cl,al
		mov dh,dl			; Head #
		mov dl,[DriveNo]
		mov ax,ds			; Restore the es register modified by the 13/08 call for floppy
		mov es,ax
		mov bx,7C00h
		mov ax,0201h			; Read one sector
common_tail:
		int 13h
		jc disk_error
		pop si				; DS:SI -> partition table entry
;
; Verify that we have a boot sector, jump
;
		cmp word [7C00h+510],0AA55h
		jne missing_os
		cli
		jmp 0:7C00h			; Jump to boot sector; far
						; jump is speculation barrier
						; (Probably not neecessary, but
						; there is plenty of space.)

not_one_partition:
		ja too_many_os
missing_os:
		mov si,missing_os_msg
		jmp short print_and_die
too_many_os:
disk_error:
		mov si,bad_disk_msg
print_and_die:
		call print_message
die:
		jmp short die

print_message:
		lodsb
		and al,al
		jz print_end
		mov ah,0Eh			; TTY output
		mov bh,[BIOS_page]		; Current page
		mov bl,07h
		int 10h
		jmp short print_message
print_end:
		ret

		align 4, db 0			; Begin data area

;
; EBIOS disk address packet
;
dapa:
		dw 16				; Packet size
.count:		dw 1				; Block count
.off:		dw 7C00h			; Offset of buffer
.seg:		dw 0				; Segment of buffer
.lba:		dd 0				; LBA (LSW)
		dd 0				; LBA (MSW)

; CHS information
Heads:		dw 0
Sectors:	dw 0

; Error messages
missing_os_msg	db 'No operating system', 13, 10, 0
bad_disk_msg	db 'Disk error', 13, 10, 0
fdd_msg		db 'FDD', 0
hdd_msg		db 'HDD', 0
ebios_msg	db ' EBIOS'
crlf_msg	db 13, 10, 0

;
; Maximum MBR size: 446 bytes; end-of-boot-sector signature also needed.
; Note that some operating systems (NT, DR-DOS) put additional stuff at
; the end of the MBR, so shorter is better.  Location 440 is known to
; have a 4-byte attempt-at-unique-ID for some OSes.
;

PartitionTable	equ $$+446			; Start of partition table

;
; BSS data; put at 800h
;
DriveNo		equ 0800h