xref: /illumos-gate/usr/src/boot/i386/pmbr/pmbr.s (revision 55fea89d)
1*22028508SToomas Soome#-
2*22028508SToomas Soome# Copyright (c) 2007 Yahoo!, Inc.
3*22028508SToomas Soome# All rights reserved.
4*22028508SToomas Soome# Written by: John Baldwin <jhb@FreeBSD.org>
5*22028508SToomas Soome#
6*22028508SToomas Soome# Redistribution and use in source and binary forms, with or without
7*22028508SToomas Soome# modification, are permitted provided that the following conditions
8*22028508SToomas Soome# are met:
9*22028508SToomas Soome# 1. Redistributions of source code must retain the above copyright
10*22028508SToomas Soome#    notice, this list of conditions and the following disclaimer.
11*22028508SToomas Soome# 2. Redistributions in binary form must reproduce the above copyright
12*22028508SToomas Soome#    notice, this list of conditions and the following disclaimer in the
13*22028508SToomas Soome#    documentation and/or other materials provided with the distribution.
14*22028508SToomas Soome# 3. Neither the name of the author nor the names of any co-contributors
15*22028508SToomas Soome#    may be used to endorse or promote products derived from this software
16*22028508SToomas Soome#    without specific prior written permission.
17*22028508SToomas Soome#
18*22028508SToomas Soome# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19*22028508SToomas Soome# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*22028508SToomas Soome# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*22028508SToomas Soome# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22*22028508SToomas Soome# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*22028508SToomas Soome# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*22028508SToomas Soome# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*22028508SToomas Soome# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*22028508SToomas Soome# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*22028508SToomas Soome# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*22028508SToomas Soome# SUCH DAMAGE.
29*22028508SToomas Soome#
30*22028508SToomas Soome# $FreeBSD$
31*22028508SToomas Soome#
32*22028508SToomas Soome# Partly from: src/sys/boot/i386/mbr/mbr.s 1.7
33*22028508SToomas Soome
34*22028508SToomas Soome# A 512 byte PMBR boot manager to read a boot program and run it.
35*22028508SToomas Soome# The embedded MBR is set up for PMBR and default bootblock sector
36*22028508SToomas Soome# is hardcoded to 256 and size 1. The actual values are supposed to be
37*22028508SToomas Soome# updated by installboot.
38*22028508SToomas Soome
39*22028508SToomas Soome		.set LOAD,0x7c00		# Load address
40*22028508SToomas Soome		.set EXEC,0x600 		# Execution address
41*22028508SToomas Soome		.set MAGIC,0xaa55		# Magic: bootable
42*22028508SToomas Soome		.set SECSIZE,0x200		# Size of a single disk sector
43*22028508SToomas Soome		.set DISKSIG,440		# Disk signature offset
44*22028508SToomas Soome		.set STACK,EXEC+SECSIZE*4	# Stack address
45*22028508SToomas Soome		.set DPBUF,STACK
46*22028508SToomas Soome
47*22028508SToomas Soome		.set NHRDRV,0x475		# Number of hard drives
48*22028508SToomas Soome
49*22028508SToomas Soome		.globl start			# Entry point
50*22028508SToomas Soome		.code16
51*22028508SToomas Soome		.text
52*22028508SToomas Soome
53*22028508SToomas Soomestart:		jmp real_code
54*22028508SToomas Soome		.fill 0x3c,0x1,0x90		# fill with nop to ease disasm
55*22028508SToomas Soome#
56*22028508SToomas Soome# BIOS Parameter Block. Reserved space from 0xb to 0x3e, the FAT32 BPB
57*22028508SToomas Soome# is 60 (3Ch) bytes.
58*22028508SToomas Soome#
59*22028508SToomas Soome		. = start + 0x3e
60*22028508SToomas Soome
61*22028508SToomas Soome#
62*22028508SToomas Soome# Setup the segment registers for flat addressing and setup the stack.
63*22028508SToomas Soome#
64*22028508SToomas Soomereal_code:	cld				# String ops inc
65*22028508SToomas Soome		xorw %ax,%ax			# Zero
66*22028508SToomas Soome		movw %ax,%es			# Address
67*22028508SToomas Soome		movw %ax,%ds			#  data
68*22028508SToomas Soome		movw %ax,%ss			# Set up
69*22028508SToomas Soome		movw $STACK,%sp			#  stack
70*22028508SToomas Soome#
71*22028508SToomas Soome# Relocate ourself to a lower address so that we have more room to load
72*22028508SToomas Soome# other sectors.
73*22028508SToomas Soome#
74*22028508SToomas Soome		movw $main-EXEC+LOAD,%si	# Source
75*22028508SToomas Soome		movw $main,%di			# Destination
76*22028508SToomas Soome		movw $SECSIZE-(main-start),%cx	# Byte count
77*22028508SToomas Soome		rep				# Relocate
78*22028508SToomas Soome		movsb				#  code
79*22028508SToomas Soome#
80*22028508SToomas Soome# Jump to the relocated code.
81*22028508SToomas Soome#
82*22028508SToomas Soome		jmp main-LOAD+EXEC		# To relocated code
83*22028508SToomas Soome#
84*22028508SToomas Soome# Validate drive number in %dl.
85*22028508SToomas Soome#
86*22028508SToomas Soomemain:	 	cmpb $0x80,%dl			# Drive valid?
87*22028508SToomas Soome		jb main.1			# No
88*22028508SToomas Soome		movb NHRDRV,%dh			# Calculate the highest
89*22028508SToomas Soome		addb $0x80,%dh			#  drive number available
90*22028508SToomas Soome		cmpb %dh,%dl			# Within range?
91*22028508SToomas Soome		jb main.2			# Yes
92*22028508SToomas Soomemain.1: 	movb $0x80,%dl			# Assume drive 0x80
93*22028508SToomas Soome#
94*22028508SToomas Soome# Load stage2 and start it. location and size is written by installboot
95*22028508SToomas Soome# and if size is 0, we can not do anything...
96*22028508SToomas Soome#
97*22028508SToomas Soomemain.2:		movw stage2_size, %ax
98*22028508SToomas Soome		cmpw $0, %ax
99*22028508SToomas Soome		je err_noboot			# the stage2 size is not set
100*22028508SToomas Soome		pushw %dx			# save drive
101*22028508SToomas Soome		movb $0x41, %ah			# check extensions
102*22028508SToomas Soome		movw $0x55aa, %bx
103*22028508SToomas Soome		int $0x13
104*22028508SToomas Soome		popw %dx			# restore drive
105*22028508SToomas Soome		jc err_rd			# need lba mode for now
106*22028508SToomas Soome		cmpw $0xaa55, %bx		# chs support is not
107*22028508SToomas Soome		jne err_rd			# implemented.
108*22028508SToomas Soome		movw $stage2_sector, %si	# pointer to lba
109*22028508SToomas Soome		movw $LOAD/16,%bx		# set buffer segment
110*22028508SToomas Soome		movw %bx,%es
111*22028508SToomas Soome		xorw %bx,%bx			# and offset
112*22028508SToomas Soomeload_boot:	push %si			# Save %si
113*22028508SToomas Soome		call read
114*22028508SToomas Soome		pop %si				# Restore
115*22028508SToomas Soome		decw stage2_size		# stage2_size--
116*22028508SToomas Soome		jnz next_boot
117*22028508SToomas Soomeboot:		mov %bx,%es			# Reset %es to zero
118*22028508SToomas Soome		jmp LOAD			# Jump to boot code
119*22028508SToomas Soomenext_boot:	incl (%si)			# Next LBA
120*22028508SToomas Soome		adcl $0,4(%si)
121*22028508SToomas Soome		mov %es,%ax			# Adjust segment for next
122*22028508SToomas Soome		addw $SECSIZE/16,%ax		#  sector
123*22028508SToomas Soome		mov %ax,%es			#
124*22028508SToomas Soome		jmp load_boot
125*22028508SToomas Soome#
126*22028508SToomas Soome# Load a sector (64-bit LBA at %si) from disk %dl into %es:%bx by creating
127*22028508SToomas Soome# a EDD packet on the stack and passing it to the BIOS.  Trashes %ax and %si.
128*22028508SToomas Soome#
129*22028508SToomas Soomeread:		pushl 0x4(%si)			# Set the LBA
130*22028508SToomas Soome		pushl 0x0(%si)			#  address
131*22028508SToomas Soome		pushw %es			# Set the address of
132*22028508SToomas Soome		pushw %bx			#  the transfer buffer
133*22028508SToomas Soome		pushw $0x1			# Read 1 sector
134*22028508SToomas Soome		pushw $0x10			# Packet length
135*22028508SToomas Soome		movw %sp,%si			# Packer pointer
136*22028508SToomas Soome		movw $0x4200,%ax		# BIOS:	LBA Read from disk
137*22028508SToomas Soome		int $0x13			# Call the BIOS
138*22028508SToomas Soome		add $0x10,%sp			# Restore stack
139*22028508SToomas Soome		jc err_rd			# If error
140*22028508SToomas Soome		ret
141*22028508SToomas Soome#
142*22028508SToomas Soome# Various error message entry points.
143*22028508SToomas Soome#
144*22028508SToomas Soomeerr_rd: 	movw $msg_rd,%si		# "I/O error loading
145*22028508SToomas Soome		jmp putstr			#  boot loader"
146*22028508SToomas Soome
147*22028508SToomas Soomeerr_noboot: 	movw $msg_noboot,%si		# "Missing boot
148*22028508SToomas Soome		jmp putstr			#  loader"
149*22028508SToomas Soome#
150*22028508SToomas Soome# Output an ASCIZ string to the console via the BIOS.
151*22028508SToomas Soome#
152*22028508SToomas Soomeputstr.0:	movw $0x7,%bx	 		# Page:attribute
153*22028508SToomas Soome		movb $0xe,%ah			# BIOS: Display
154*22028508SToomas Soome		int $0x10			#  character
155*22028508SToomas Soomeputstr: 	lodsb				# Get character
156*22028508SToomas Soome		testb %al,%al			# End of string?
157*22028508SToomas Soome		jnz putstr.0			# No
158*22028508SToomas Soomeputstr.1:	jmp putstr.1			# Await reset
159*22028508SToomas Soome
160*22028508SToomas Soomemsg_rd: 	.asciz "I/O error"
161*22028508SToomas Soomemsg_noboot: 	.asciz "No boot loader"
162*22028508SToomas Soome
163*22028508SToomas Soome		nop
164*22028508SToomas Soomembr_version:	.byte 1, 1			# 1.1
165*22028508SToomas Soome		.align 4
166*22028508SToomas Soomestage2_size:	.word 1				# bootblock size in sectors
167*22028508SToomas Soomestage2_sector:	.quad 256			# lba of bootblock
168*22028508SToomas Soomedisk_uuid:	.quad 0				# uuid
169*22028508SToomas Soome		.quad 0
170*22028508SToomas Soome
171*22028508SToomas Soome# this is the end of the code block we can use, next is space for
172*22028508SToomas Soome# signature, partition table 4 entries and signature.
173*22028508SToomas Soome		.org DISKSIG,0x1b8		#
174*22028508SToomas Soomesig:		.long 0				# OS Disk Signature
175*22028508SToomas Soome		.word 0				# "Unknown" in PMBR
176*22028508SToomas Soome
177*22028508SToomas Soomepartbl:		.byte 0x00			# non-bootable
178*22028508SToomas Soome		.byte 0x00			# head 0
179*22028508SToomas Soome		.byte 0x02			# sector
180*22028508SToomas Soome		.byte 0x00			# cylinder
181*22028508SToomas Soome		.byte 0xEE			# ID
182*22028508SToomas Soome		.byte 0xFF			# ending head
183*22028508SToomas Soome		.byte 0xFF			# ending sector
184*22028508SToomas Soome		.byte 0xFF			# ending cylinder
185*22028508SToomas Soome		.long 0x00000001		# starting LBA
186*22028508SToomas Soome		.long 0xFFFFFFFF		# size
187*22028508SToomas Soome		.fill 0x10,0x3,0x0		# other 3 entries
188*22028508SToomas Soome		.word MAGIC			# Magic number
189