mbr.s revision 62146
1#
2# Copyright (c) 1999 Robert Nordier
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms are freely
6# permitted provided that the above copyright notice and this
7# paragraph and the following disclaimer are duplicated in all
8# such forms.
9#
10# This software is provided "AS IS" and without any express or
11# implied warranties, including, without limitation, the implied
12# warranties of merchantability and fitness for a particular
13# purpose.
14#
15
16# $FreeBSD: head/sys/boot/i386/mbr/mbr.s 62146 2000-06-26 23:58:04Z jhb $
17
18# A 512 byte MBR boot manager that simply boots the active partition.
19
20		.set LOAD,0x7c00		# Load address
21		.set EXEC,0x600 		# Execution address
22		.set PT_OFF,0x1be		# Partition table
23		.set MAGIC,0xaa55		# Magic: bootable
24
25		.set NDRIVE,0x8 		# Drives to support
26
27		.globl start			# Entry point
28		.code16
29
30#
31# Setup the segment registers for flat addressing and setup the stack.
32#
33start:		cld				# String ops inc
34		xorw %ax,%ax			# Zero
35		movw %ax,%es			# Address
36		movw %ax,%ds			#  data
37		cli				# Disable interrupts
38		movw %ax,%ss			# Set up
39		movw $LOAD,%sp			#  stack
40		sti				# Enable interrupts
41#
42# Relocate ourself to a lower address so that we are out of the way when
43# we load in the bootstrap from the partition to boot.
44#
45		movw $main-EXEC+LOAD,%si	# Source
46		movw $main,%di			# Destination
47		movw $0x200-(main-start),%cx	# Byte count
48		rep				# Relocate
49		movsb				#  code
50#
51# Jump to the relocated code.
52#
53		jmp main-LOAD+EXEC		# To relocated code
54#
55# Scan the partition table looking for an active entry.  Note that %ch is
56# zero from the repeated string instruction above.  We save the offset of
57# the active partition in %si and scan the entire table to ensure that only
58# one partition is marked active.
59#
60main:		xorw %si,%si			# No active partition
61		movw $partbl,%bx		# Partition table
62		movb $0x4,%cl			# Number of entries
63main.1: 	cmpb %ch,(%bx)			# Null entry?
64		je main.2			# Yes
65		jg err_pt			# If 0x1..0x7f
66		testw %si,%si	 		# Active already found?
67		jnz err_pt			# Yes
68		movw %bx,%si			# Point to active
69main.2: 	addb $0x10,%bl			# Till
70		loop main.1			#  done
71		testw %si,%si	 		# Active found?
72		jnz main.3			# Yes
73		int $0x18			# BIOS: Diskless boot
74#
75# Ok, we've found a possible active partition.  Check to see that the drive
76# is a valid hard drive number.  XXX - We assume that there are up to 8 valid
77# hard drives, regardless of how many are actually installed.  Yuck.
78#
79main.3: 	cmpb $0x80,%dl			# Drive valid?
80		jb main.4			# No
81		cmpb $0x80+NDRIVE,%dl		# Within range?
82		jb main.5			# Yes
83#
84# Ok, now that we have a valid drive and partition entry, load the CHS from
85# the partition entry and read the sector from the disk.
86#
87main.4: 	movb (%si),%dl			# Load drive
88main.5: 	movb 0x1(%si),%dh		# Load head
89		movw 0x2(%si),%cx		# Load cylinder:sector
90		movw $LOAD,%bx			# Transfer buffer
91		movw $0x201,%ax			# BIOS: Read from
92		int $0x13			#  disk
93		jc err_rd			# If error
94#
95# Now that we've loaded the bootstrap, check for the 0xaa55 signature.  If it
96# is present, execute the bootstrap we just loaded.
97#
98		cmpw $MAGIC,0x1fe(%bx)		# Bootable?
99		jne err_os			# No
100		jmp *%bx			# Invoke bootstrap
101#
102# Various error message entry points.
103#
104err_pt: 	movw $msg_pt,%si		# "Invalid partition
105		jmp putstr			#  table"
106
107err_rd: 	movw $msg_rd,%si		# "Error loading
108		jmp putstr			#  operating system"
109
110err_os: 	movw $msg_os,%si		# "Missing operating
111		jmp putstr			#  system"
112#
113# Output an ASCIZ string to the console via the BIOS.
114#
115putstr.0:	movw $0x7,%bx	 		# Page:attribute
116		movb $0xe,%ah			# BIOS: Display
117		int $0x10			#  character
118putstr: 	lodsb				# Get character
119		testb %al,%al			# End of string?
120		jnz putstr.0			# No
121putstr.1:	jmp putstr.1			# Await reset
122
123msg_pt: 	.asciz "Invalid partition table"
124msg_rd: 	.asciz "Error loading operating system"
125msg_os: 	.asciz "Missing operating system"
126
127		.org PT_OFF
128
129partbl: 	.fill 0x10,0x4,0x0		# Partition table
130		.word MAGIC			# Magic number
131