boot.s revision 64123
1275970Scy# Copyright (c) KATO Takenori, 1999, 2000.
2275970Scy#
3275970Scy# All rights reserved.  Unpublished rights reserved under the copyright
4275970Scy# laws of Japan.
5275970Scy#
6275970Scy# Redistribution and use in source and binary forms, with or without
7275970Scy# modification, are permitted provided that the following conditions
8290000Sglebius# are met:
9275970Scy#
10275970Scy# 1. Redistributions of source code must retain the above copyright
11290000Sglebius#    notice, this list of conditions and the following disclaimer as
12275970Scy#    the first lines of this file unmodified.
13275970Scy# 2. Redistributions in binary form must reproduce the above copyright
14275970Scy#    notice, this list of conditions and the following disclaimer in the
15275970Scy#    documentation and/or other materials provided with the distribution.
16275970Scy#
17275970Scy# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18275970Scy# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19275970Scy# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20275970Scy# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21275970Scy# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22275970Scy# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23275970Scy# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24275970Scy# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25275970Scy# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26290000Sglebius# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27275970Scy#
28275970Scy# $FreeBSD: head/sys/boot/pc98/boot0.5/boot.s 64123 2000-08-02 08:46:08Z kato $
29275970Scy#
30275970Scy
31275970Scy	.code16
32275970Scy
33275970Scy	.text
34275970Scy	.global	boot
35275970Scy#
36275970Scy# Read bootstrap program and jump to it.
37275970Scy#
38290000Sglebiusboot:
39275970Scy	# Step 1: Save parameters
40275970Scy	movw	curdevice, %si
41275970Scy	movb	daua(%si), %al
42275970Scy	movb	%al, b_daua
43275970Scy	shlw	%si
44290000Sglebius	movw	secsize(%si), %ax
45275970Scy	movw	%ax, b_secsize
46275970Scy
47275970Scy	movw	curpartition, %si
48275970Scy	movb	partnum(%si), %al	# %al = real partition number
49275970Scy	xorb	%ah, %ah
50275970Scy	movw	%ax, b_partn		# save real parttion number
51275970Scy	movb	$5, %cl
52275970Scy	shlw	%cl, %si		# %si = offset to parttable
53275970Scy	addw	$4, %si
54275970Scy	movb	parttable(%si), %al	# IPLS
55275970Scy	movb	%al, b_sector
56275970Scy	incw	%si
57275970Scy	movb	parttable(%si), %al	# IPLH
58275970Scy	movb	%al, b_head
59275970Scy	incw	%si			# IPLC
60275970Scy	movw	parttable(%si), %ax
61275970Scy	movw	%ax, b_cylinder
62275970Scy
63275970Scy	# Step 2: Calculate egment address of bootstrap routine
64275970Scy	movw	$0x1d00, %ax
65275970Scy	movw	b_secsize, %cx
66290000Sglebius	shrw	%cx
67290000Sglebius	shrw	%cx
68290000Sglebius	subw	%cx, %ax
69290000Sglebius	subw	$0x100, %ax
70275970Scy	movw	%ax, b_bootseg
71275970Scy
72275970Scy	# Step 3: Read bootstrap code
73275970Scy	movb	$6, %ah
74275970Scy	movb	b_daua, %al
75275970Scy	movw	b_secsize, %bx
76275970Scy	shlw	%bx			# 2 sectors
77275970Scy	movw	b_cylinder, %cx
78275970Scy	movb	b_head, %dh
79275970Scy	movb	b_sector, %dl
80290000Sglebius	movw	b_bootseg, %es
81275970Scy	xorw	%bp, %bp
82275970Scy	int	$0x1b
83275970Scy	jc	boot_error
84275970Scy
85275970Scy	# Step 4: Set DA/UA into BIOS work area
86275970Scy	xorw	%ax, %ax
87290000Sglebius	movw	%ax, %es
88275970Scy	movw	$0x584, %bx		# DISK_BOOT
89275970Scy	movb	b_daua, %dl
90275970Scy	call	write_biosparam
91275970Scy
92275970Scy	call	sc_clean
93275970Scy	# Step 5: Set registers
94275970Scy	#	%ah:	00
95290000Sglebius	#	%al:	DA/UA
96275970Scy	#	%bx:	Sector size * 2
97275970Scy	#	%cx:	cylinder number of boot partition
98275970Scy	#	%si:	pointer to partition table
99275970Scy	movw	b_partn, %ax
100290000Sglebius	movb	$10, %cl
101290000Sglebius	shl	%cl, %ax		# %ax = partition number * 32 * 32
102290000Sglebius	addw	b_secsize, %ax
103275970Scy	movw	%ax, %si		# %si = pointer to partition table
104275970Scy	movw	b_cylinder, %cx		# %cx = cylinder
105275970Scy	movb	b_head, %dh		# %dh = head
106275970Scy	movb	b_sector, %dl		# %dl = sector
107275970Scy	movw	b_bootseg, %es		# %es = boot segment
108275970Scy	movb	b_daua, %al		# %al = DA/UA
109275970Scy	movw	b_secsize, %bx
110275970Scy	shlw	%bx			# %bx = sector size * 2
111275970Scy	cli
112290000Sglebius	movw	%cs:iniss, %ss		# Restore stack pointer
113275970Scy	movw	%cs:inisp, %sp
114275970Scy	push	%es			# Boot segment
115275970Scy	xorw	%bp, %bp
116290000Sglebius	push	%bp			# 0
117290000Sglebius	movw	%ax, %di		# Save %ax
118275970Scy	xorw	%ax, %ax
119275970Scy	movw	%ax, %ds		# %ds = 0
120275970Scy	movw	%di, %ax		# Restore %ax
121275970Scy	xorb	%ah, %ah		# %di = 0
122275970Scy	xorw	%di, %di		# %di = 0
123275970Scy	sti
124275970Scy
125275970Scy	# Jump to bootstrap code
126275970Scy	lret
127290000Sglebius	# NOTREACHED
128275970Scy
129275970Scyboot_error:
130275970Scy	ret
131275970Scy
132275970Scy#
133275970Scy# Try to boot from default partition.
134275970Scy#
135275970Scy	.global	trydefault
136275970Scytrydefault:
137275970Scy	movw	ndevice, %cx
138275970Scy	xorw	%si, %si
139275970Scytrydefault_loop:
140275970Scy	movw	%si, curdevice
141275970Scy	push	%cx
142275970Scy	push	%si
143275970Scy	call	read_ipl
144275970Scy	pop	%si
145275970Scy	pop	%cx
146275970Scy	cmpb	$0x80, defpartflag
147275970Scy	jne	nodefpart
148275970Scy	# Default partition is defined.
149275970Scy	push	%cx
150275970Scy	movw	npartition, %cx
151275970Scysrch_part:
152275970Scy	movw	%cx, %bx
153290000Sglebius	decw	%bx
154290000Sglebius	movb	defpartnum, %al		# %al = real partition number
155275970Scy	cmpb	partnum(%bx), %al
156275970Scy	jne	not_match
157275970Scy	movw	%bx, curpartition	# Store partition number
158290000Sglebius	call	boot
159275970Scynot_match:
160290000Sglebius	loop	srch_part
161275970Scy	pop	%cx
162275970Scynodefpart:
163275970Scy	incw	%si
164275970Scy	loop	trydefault_loop
165275970Scy	ret
166275970Scy
167290000Sglebius	.data
168290000Sglebiusb_daua:		.byte	0		# DA/UA
169290000Sglebiusb_head:		.byte	0		# SYSH
170290000Sglebiusb_sector:	.byte	0		# SYSS
171275970Scyb_cylinder:	.word	0		# SYSC
172275970Scyb_bootseg:	.word	0
173275970Scyb_secsize:	.word	0
174275970Scyb_partn:	.word	0		# Real partition number
175275970Scy