User Tools

Site Tools


my_mbr

This is an old revision of the document!


My MBR

I'm using NASM to write my real mode code, and I use file extension “nasm” to separate them from other assembler files. This makes the build process able to automatically select the correct assembler.

[bits 16]
 
struc	PT_ENT		;Partition table entry
	BootFlag		resb	1
	BeginHead		resb	1
	BeginSector		resb	1
	BeginCyl		resb	1
	SystemID		resb	1
	EndHead			resb	1
	EndSector		resb	1
	EndCyl			resb	1
	RelSectorLow	resw	1
	RelSectorHigh	resw	1
	NumSectorsLow	resw	1
	NumSectorsHigh	resw	1
endstruc
 
%include "mbr/mem.inc"
 
[org loc_MBR]
 
SEGMENT START align=16			;At 0:7C00
 
start:
; register 'dl' contains boot disk number
	cli
	xor	cx, cx			; Set segment registers to zero
						; cs - Code Segment (we're here, so it's obviously zero - or erroneously 0x07C0)
	mov	es, cx			; Extra Segment
	mov	ss, cx			; Stack Segment
	mov	sp, loc_MBR		; Top of stack is bottom of relocation point
	mov	ds, cx			; Data Segment
	mov	di, sp			; Destination for stream
	mov	si, bootpoint	; Source of stream
	cld					; Clear direction flag (stream increment)
	mov	ch, 1			; cx = 256
	rep movsw			; word move = 512 bytes copy, one HD sector
	jmp	0x0000:continue	; JMP to copy of self
 
;bootdisk	db	0
 
SEGMENT MAIN align=16			;At 0:7A00 + START.length
continue:
;	mov [bootdisk], dl
	sti					; enable interrupts
	mov	si, 3			;Three tries
try:
	mov	bx, loc_LOADER	;Read it into 0x7C00
	mov	ax, 0x0210		;Command to read 16 sectors
	mov	cx, 0x0002			;Sector 2 / Cylinder 1
	int	13h				; 'dl' contains boot disk from MBR entry
	jnc	ok			;Success
	dec	si			;retry count
	jnz	try
fail:
	mov	bx, 0x15		;   bold attribute
	mov ah, 0x0E
	mov al, 'F'
	int 10h
crap:
	cli
	hlt
	jmp crap
ok:
	cli
	jmp	loc_LOADER		; set up for 64-bit, long mode, and bootloader
 
; put segment FINAL at the end of the sector
SEGMENT FINAL start=loc_MBR+0x200-2-4*PT_ENT_size
 
PartitionTable	db	0x80		;BootFlag		resb	1
				db	1			;BeginHead       resb	1	8 bit
				db	1			;BeginSector     resb	1	6 bit
				db	0			;BeginCyl        resb	1	10 bit
				db	0xED		;SystemID        resb	1	ED - Evil Duck
				db	2			;EndHead         resb	1	8 bit
				db	2			;EndSector       resb	1	6 bit
				db	0			;EndCyl          resb	1	10 bit
				dw	2			;RelSectorLow    resw	1
				dw	0			;RelSectorHigh   resw	1
				dw	1000		;NumSectorsLow   resw	1
				dw	0			;NumSectorsHigh  resw	1
 
PartitionTable2  resb PT_ENT_size
PartitionTable3  resb PT_ENT_size
PartitionTable4  resb PT_ENT_size
	dw	0xAA55
my_mbr.1488785516.txt.gz · Last modified: 2017/03/06 07:31 by admin