164123Skato# Copyright (c) KATO Takenori, 1999, 2000.
264123Skato#
364123Skato# All rights reserved.  Unpublished rights reserved under the copyright
464123Skato# laws of Japan.
564123Skato#
664123Skato# Redistribution and use in source and binary forms, with or without
764123Skato# modification, are permitted provided that the following conditions
864123Skato# are met:
964123Skato#
1064123Skato# 1. Redistributions of source code must retain the above copyright
1164123Skato#    notice, this list of conditions and the following disclaimer as
1264123Skato#    the first lines of this file unmodified.
1364123Skato# 2. Redistributions in binary form must reproduce the above copyright
1464123Skato#    notice, this list of conditions and the following disclaimer in the
1564123Skato#    documentation and/or other materials provided with the distribution.
1664123Skato#
1764123Skato# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1864123Skato# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1964123Skato# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2064123Skato# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2164123Skato# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2264123Skato# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2364123Skato# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2464123Skato# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2564123Skato# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2664123Skato# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2764123Skato#
2864123Skato# $FreeBSD: releng/10.3/sys/boot/pc98/boot0.5/disk.s 167657 2007-03-17 05:30:03Z kato $
2964123Skato#
3064123Skato
3164123Skato	.code16
3264123Skato	.text
3364123Skato#
3464123Skato# Check magic number at the end of the sector 0
3564123Skato#
3664123Skatocheck_magic:
3764123Skato	movw	curdevice, %si
3864123Skato	shlw	%si
3964123Skato	movw	secsize(%si), %bx
4064123Skato	decw	%bx
4164123Skato	decw	%bx
4264123Skato	movw	iplbuf(%bx), %ax
4364123Skato	cmpw	$0xaa55, %ax
4464123Skato	je	magic_ok
4564123Skato	movw	$1, %ax
4664123Skato	ret
4764123Skatomagic_ok:
4864123Skato	xorw	%ax, %ax
4964123Skato	ret
5064123Skato
5164123Skato#
5264123Skato# Copy partition table from buffer to parttable.
5364123Skato#
5464123Skatosetup_partition:
5564123Skato	push	%cs
5664123Skato	pop	%es
5764123Skato	movw	curdevice, %bx
5864123Skato	shlw	%bx
5964123Skato	movw	maxpart(%bx), %cx	# %cx = max num of partitions
6064123Skato	movw	partoff(%bx), %di
6164123Skato	movw	%di, %bx		# %bx = offset to partition table
6264123Skato	xorw	%dx, %dx		# %dx = partition number
6364123Skatosetup_partition_loop:
6464183Skato	push	%cx
6564123Skato	movw	%dx, %si
6664183Skato	movb	$5, %cl
6764183Skato	shlw	%cl, %si
6864123Skato	addw	%bx, %si
6964123Skato	movb	iplbuf(%si), %al
7064123Skato	orb	%al, %al
7164123Skato	jz	unused_partition
7264183Skato	addw	$iplbuf, %si
7364123Skato	movw	npartition, %ax
7464123Skato	movw	%ax, %di
7564183Skato	movb	$5, %cl
7664183Skato	shlw	%cl, %di
7764123Skato	addw	$parttable, %di
7864123Skato	movw	$32, %cx
7964123Skato	rep
8064123Skato	movsb
8164123Skato	movw	%ax, %di
8264123Skato	addw	$partnum, %di
8364123Skato	movb	%dl, (%di)
8464123Skato	incw	npartition
8564123Skatounused_partition:
8664123Skato	incw	%dx
8764183Skato	pop	%cx
8864123Skato	loop	setup_partition_loop
8964123Skato	ret
9064123Skato
9164123Skato#
9264123Skato# Read IPL and partition table in the current device.
9364123Skato#
9464123Skato	.global	read_ipl
9564123Skatoread_ipl:
9664123Skato	movw	curdevice, %ax
9764123Skato	movw	%ax, %si		# %si = device number
9864123Skato	movw	%ax, %di
9964123Skato	shlw	%di
10064123Skato
10164123Skato	movw	%cs, %ax
10264123Skato	movw	%ax, %es
10364123Skato	movb	$6, %ah
10464123Skato	movb	daua(%si), %al
10564123Skato	movw	$0x400, %bx
10664123Skato	xorw	%cx, %cx
10764123Skato	xorw	%dx, %dx
10864123Skato	movw	$iplbuf, %bp
10964123Skato	int	$0x1b
11064123Skato	jc	read_ipl_error
11164123Skato	movw	defflagoff(%di), %bx
11264123Skato	movb	iplbuf(%bx), %al
11364123Skato	movb	%al, defpartflag
11464123Skato	incw	%bx
11564123Skato	movb	iplbuf(%bx), %al
11664123Skato	movb	%al, defpartnum
11764123Skato	movw	$0, npartition
11864123Skato	call	check_magic
11964123Skato	orw	%ax, %ax
12064123Skato	jnz	no_magic
12164123Skato	call	setup_partition
12264123Skatono_magic:
12364123Skato	xorw	%ax, %ax
12464123Skatoread_ipl_error:
12564123Skato	xorw	%bx, %bx
12664123Skato	movw	%bx, %es
12764123Skato	ret
12864123Skato
12964123Skato#
13064123Skato# Restore IPL from the buffer
13164123Skato#
13264123Skato	.global	write_ipl
13364123Skatowrite_ipl:
13464123Skato	movw	curdevice, %ax
13564123Skato	movw	%ax, %si
13664123Skato	movw	%ax, %di
13764123Skato	shlw	%di
13864123Skato
13964123Skato	# Restore default boot partition info.
14064123Skato	movw	defflagoff(%di), %bx
14164123Skato	movb	defpartflag, %al
14264123Skato	movb	%al, iplbuf(%bx)
14364123Skato	incw	%bx
14464123Skato	movb	defpartnum, %al
14564123Skato	movb	%al, iplbuf(%bx)
14664123Skato
14764123Skato	movw	%cs, %ax
14864123Skato	movw	%ax, %es
14964123Skato	movb	$5, %ah
15064123Skato	movb	daua(%si), %al
15164123Skato	movw	secsize(%di), %bx
15264123Skato	xorw	%cx, %cx
15364123Skato	xorw	%dx, %dx
15464123Skato	movw	$iplbuf, %bp
15564123Skato	int	$0x1b
15664123Skato	jc	write_ipl_error
15764123Skato	xorw	%ax, %ax
15864123Skatowrite_ipl_error:
15964123Skato	xorw	%bx, %bx
16064123Skato	movw	%bx, %es
16164123Skato	ret
16264123Skato
16364123Skato#
16464123Skato# Scan HDD devices
16564123Skato#
16664123Skato	.global	scan_sasi, scan_scsi
16764123Skato	# Scan SASI disk
16864123Skatoscan_sasi:
16964123Skato	# SASI Disk
17064123Skato	movw	$4, %cx
17164123Skato	movw	$0x0001, %ax	# %ah =  unit number, %al = for bit operation
17264123Skato
17364123Skatosasi_loop:
17464123Skato	movw	%si, %di
17564123Skato	shlw	%di
17664123Skato	movw	$0x55d, %bx		# DISK_EQUIP
17764123Skato	call	read_biosparam
17864123Skato	testb	%al, %dl
17964123Skato	jz	no_sasi_unit
18064123Skato	movb	$0x80, %dh
18165271Skato	addb	%ah, %dh		# %dh = DA/UA
18265271Skato	movb	%dh, daua(%si)		# Store DA/UA
18365271Skato
18465271Skato	# Try new sense command
18565271Skato	push	%ax
18665271Skato	push	%cx
18765271Skato	movb	%dh, %al
18865271Skato	movb	$0x84, %ah
18965271Skato	int	$0x1b
19065271Skato	pop	%cx
19165271Skato	pop	%ax
19265271Skato	jc	err_newsense
19365271Skato	movw	%bx, %dx
19465271Skato	jmp	found_sasi_unit
19565271Skato
19665271Skatoerr_newsense:
19764123Skato	movw	$0x457, %bx		# capacity & sector size of IDE HDD
19864123Skato	call	read_biosparam
19964123Skato	orb	%ah, %ah
20064123Skato	jz	sasi_1
20164123Skato	cmpb	$1, %ah
20264123Skato	jz	sasi_2
20364123Skato
20464123Skato	# SASI #3/#4
20564123Skato	movw	$512, %dx		# XXX
20664123Skato	jmp	found_sasi_unit
20764123Skato
20864123Skatosasi_1:
20964123Skato	# SASI #1
21064123Skato	testb	$0x80, %dl
21164123Skato	jz	sasi_256
21264123Skato	jmp	sasi_512
21364123Skatosasi_2:
21464123Skato	# SASI #2
21564660Skato	testb	$0x40, %dl
21664123Skato	jz	sasi_256
21764123Skato	jmp	sasi_512
21864123Skato
21964123Skatosasi_256:
22064123Skato	movw	$256, %dx
22164123Skato	jmp	found_sasi_unit
22264123Skatosasi_512:
22364123Skato	movw	$512, %dx
22464123Skatofound_sasi_unit:
22564123Skato	movw	%dx, secsize(%di)
22664123Skato	incw	%si
22764123Skatono_sasi_unit:
22864123Skato	incb	%ah
22964123Skato	shlb	%al
23064123Skato	loop	sasi_loop
23164123Skato	ret
23264123Skato
23364123Skato#
23464123Skato# Scan SCSI disk
23564123Skato#	SI	number of disks
23664123Skato#	destroyed: %ax, %bx, %cx, %dx
23764123Skatoscan_scsi:
23864123Skato	movw	$8, %cx
23964123Skato	movw	$0x0001, %ax	# %ah = ID number, %al = for bit operation
24064123Skatoscsi_loop:
24164123Skato	# Check whether drive exist.
24264123Skato	movw	%si, %di
24364123Skato	shlw	%di
24464123Skato	movw	$0x482, %bx		# DISK_EQUIPS
24564123Skato	call	read_biosparam
24664123Skato	testb	%al, %dl
24764123Skato	jz	no_scsi_unit
24864123Skato	xorw	%bx, %bx
24964123Skato	movb	%ah, %bl
25064123Skato	shlw	%bx
25164123Skato	shlw	%bx
25264123Skato	addw	$0x460, %bx		# SCSI paramter block
25364123Skato	call	read_biosparam
25464123Skato	orb	%dl, %dl
25564123Skato	jz	no_scsi_unit
25664123Skato
25764123Skato	# SCSI harddrive found.
25864123Skato	movb	$0xa0, %dh
25964123Skato	addb	%ah, %dh
26064123Skato	movb	%dh, daua(%si)
26164123Skato
26264123Skato	# Check sector size.
26364123Skato	addw	$3, %bx
26464123Skato	call	read_biosparam
26564123Skato	andb	$0x30, %dl
26664123Skato	cmpb	$0x20, %dl
26764123Skato	je	scsi_1024
26864123Skato	cmpb	$0x10, %dl
26964123Skato	je	scsi_512
27064123Skato	movw	$256, %dx
27164123Skato	jmp	found_scsi
27264123Skatoscsi_1024:
27364123Skato	movw	$1024, %dx
27464123Skato	jmp	found_scsi
27564123Skatoscsi_512:
27664123Skato	movw	$512, %dx
27764123Skatofound_scsi:
27864123Skato	movw	%dx, secsize(%di)
27964123Skato	incw	%si
28064123Skatono_scsi_unit:
28164123Skato	incb	%ah
28264123Skato	shlb	%al
28364123Skato	loop	scsi_loop
28464123Skato	ret
28564123Skato
28664123Skato	.data
287167657Skato	.global	defpartflag, defpartnum, npartition
28864123Skatodefpartflag:	.byte	0
28964123Skatodefpartnum:	.byte	0
29064123Skatonpartition:	.word	0		# number of partitions
291167657Skato
29264123Skato	.bss
293167657Skato	.global	partnum, parttable
294167657Skatoiplbuf:		.space	0x400		# Read buffer for IPL
295167657Skatopartnum:	.space	32		# Index of parttable
296167657Skatoparttable:	.space	1024		# Copy of valid partition table
297