1168457Skato# Copyright (c) KATO Takenori, 1999, 2000, 2007.
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$
2964123Skato#
3064123Skato	.global	main
3164123Skato	.code16
3264123Skato
3364123Skato	.text
3464123Skatomain:
3564123Skato	# Check hireso mode
3664123Skato	movw	$0x501, %bx		# BIOS_FLAG
3764123Skato	call	read_biosparam
3864123Skato	testb	$0x08, %dl
3964123Skato	jz	normalmode
4064123Skato	movb	$1, ishireso
4164123Skatonormalmode:
4264123Skato	call	sc_init
4364123Skato
4464123Skato	# Display title and copyright.
4564123Skato	movw	$title, %di
4664123Skato	call	sc_puts
4764123Skato	xorw	%cx, %cx
4864123Skato	movw	$1, %dx
4964123Skato	call	sc_goto
5064123Skato	movw	$copyright, %di
5164123Skato	call	sc_puts
5264123Skato
5364123Skato	# Scan hard drives
5464123Skato	xorw	%si, %si		# number of partition
5564123Skato	call	scan_sasi		# SASI/IDE
5664123Skato	call	scan_scsi		# SCSI
5764123Skato	movw	%si, ndevice
5864123Skato	orw	%si, %si
5964123Skato	jnz	drives_found
6064123Skato	jmp	exit			# No hard drives
6164123Skato
6264123Skatodrives_found:
63167657Skato	# Setup sector size dependent parameters
6464123Skato	movw	%si, %cx		# %cx = number of devices
6564123Skatosetup_loop:
6664123Skato	movw	%cx, %di
6764123Skato	decw	%di
6864123Skato	shlw	%di
6964123Skato	movw	secsize(%di), %ax
7064123Skato	cmpw	$1024, %ax
7164123Skato	je	setup_1024
7264123Skato	cmpw	$512, %ax
7364123Skato	je	setup_512
7464123Skato	# 256 bytes/sector
7564123Skato	movw	$0x100, partoff(%di)
7664123Skato	movw	$0x0fa, defflagoff(%di)
7764123Skato	movw	$0x0fb, defpartoff(%di)
7864123Skato	movw	$8, maxpart(%di)
7964123Skato	jmp	setup_secsize_end
8064123Skato	# 1024 bytes/sector
8164123Skatosetup_1024:
8264123Skato	# XXX Fix me!
8364123Skato	movw	$0x400, partoff(%di)
8464123Skato	movw	$0x3fa, defflagoff(%di)
8564123Skato	movw	$0x3fb, defpartoff(%di)
8664123Skato	movb	$32, maxpart(%di)
8764123Skato	jmp	setup_secsize_end
8864123Skato	# 512 bytes/sector
8964123Skatosetup_512:
9064123Skato	movw	$0x200, partoff(%di)
9164123Skato	movw	$0x1fa, defflagoff(%di)
9264123Skato	movw	$0x1fb, defpartoff(%di)
9364123Skato	movb	$16, maxpart(%di)
9464123Skatosetup_secsize_end:
9564123Skato	loop	setup_loop
9664123Skato
9764123Skato	# For debug with floppy, fake the parameter.
9864123Skato	movw	$0x584, %bx		# DISK_BOOT
9964123Skato	call	read_biosparam
10064123Skato	andb	$0xf0, %dl
10164123Skato	cmpb	$0x90, %ah
10264123Skato	jne	boot_from_hdd
10364123Skato	movb	daua, %dl
10464123Skato	call	write_biosparam
10564123Skato
10664123Skatoboot_from_hdd:
10764123Skato	movw	$500, %cx
10864123Skatowait_0_5:
10964123Skato	call	wait1ms
11064123Skato	loop	wait_0_5
11164123Skato
11264123Skato	# If the TAB is pressed, don't try to boot from default partition
11364123Skato	xorw	%di, %di		# flag
11464123Skatowait_key_release:
11564123Skato	call	sc_iskeypress
11664123Skato	orw	%ax, %ax
11764123Skato	jz	key_release		# KBD buffer empty.
11864123Skato	call	sc_getc
11964123Skato	cmpb	$0x0f, %ah		# TAB
12064123Skato	jne	wait_key_release
12164123Skato	# TAB pressed
12264123Skato	movw	$1, %di
12364123Skato	jmp	wait_key_release
12464123Skatokey_release:
12564123Skato	orw	%di, %di
12664123Skato	jnz	dont_try_default	# TAB pressed.
12764123Skato	call	trydefault
12864295Skato	# Default partition not found.
12964123Skatodont_try_default:
13064123Skato	call	show_usage
13164123Skato	call	showdevices
13264123Skato	call	selector
13364123Skatoexit:
13464123Skato	ret
13564123Skato#
13664123Skato# Display usage
13764123Skato#
13864123Skatoshow_usage:
13964123Skato	movw	$44, %cx
14064123Skato	movw	$3, %dx
14164123Skato	call	sc_goto
14264123Skato	movw	$msg_usage1, %di
14364123Skato	call	sc_puts
14464123Skato	movw	$44, %cx
14564123Skato	movw	$4, %dx
14664123Skato	call	sc_goto
14764123Skato	movw	$msg_usage2, %di
14864123Skato	call	sc_puts
14964123Skato	movw	$44, %cx
15064123Skato	movw	$5, %dx
15164123Skato	call	sc_goto
15264123Skato	movw	$msg_usage3, %di
15364123Skato	call	sc_puts
15464123Skato	movw	$44, %cx
15564123Skato	movw	$7, %dx
15664123Skato	call	sc_goto
15764123Skato	movw	$msg_usage4, %di
15864123Skato	call	sc_puts
15964123Skato	movw	$44, %cx
16064123Skato	movw	$8, %dx
16164123Skato	call	sc_goto
16264123Skato	movw	$msg_usage5, %di
16364123Skato	call	sc_puts
16464123Skato	movw	$44, %cx
16564123Skato	movw	$9, %dx
16664123Skato	call	sc_goto
16764123Skato	movw	$msg_usage6, %di
16864123Skato	call	sc_puts
16964123Skato	movw	$44, %cx
17064123Skato	movw	$10, %dx
17164123Skato	call	sc_goto
17264123Skato	movw	$msg_usage7, %di
17364123Skato	call	sc_puts
17464123Skato	movw	$44, %cx
17564123Skato	movw	$11, %dx
17664123Skato	call	sc_goto
17764123Skato	movw	$msg_usage8, %di
17864123Skato	call	sc_puts
17964123Skato	movw	$44, %cx
18064123Skato	movw	$16, %dx
18164123Skato	call	sc_goto
18264123Skato	movw	$msg_usage9, %di
18364123Skato	call	sc_puts
18464123Skato	movw	$44, %cx
18564123Skato	movw	$17, %dx
18664123Skato	call	sc_goto
18764123Skato	movw	$msg_usage10, %di
18864123Skato	call	sc_puts
18964123Skato	movw	$44, %cx
19064123Skato	movw	$18, %dx
19164123Skato	call	sc_goto
19264123Skato	movw	$msg_usage11, %di
19364123Skato	call	sc_puts
19464123Skato	movw	$44, %cx
19564123Skato	movw	$19, %dx
19664123Skato	call	sc_goto
19764123Skato	movw	$msg_usage12, %di
19864123Skato	call	sc_puts
19964123Skato	ret
20064123Skato
20164123Skato#
20264123Skato# Display device list
20364123Skato#
20464123Skatoshowdevices:
20564123Skato	movw	$2, %cx
20664123Skato	movw	$4, %dx
20764123Skato	call	sc_goto
20864123Skato	movw	$msg_device, %di
20964123Skato	call	sc_puts
21064123Skato	xorw	%si, %si		# %si = device number
21164123Skato	movw	ndevice, %cx		# %cx = number of devices
21264123Skatoshowdev_loop:
21364123Skato	push	%cx
21464123Skato	movw	$2, %cx
21564123Skato	movw	$5, %dx
21664123Skato	addw	%si, %dx
21764123Skato	call	sc_goto
21864123Skato	# Check DA
21964123Skato	movb	daua(%si), %al
22064123Skato	push	%ax
22164123Skato	andb	$0xf0, %al
22264123Skato	cmpb	$0x80, %al
22364123Skato	je	show_sasi
22464123Skato	cmpb	$0xa0, %al
22564123Skato	je	show_scsi
22664123Skato	# unknown device
22764123Skato	movw	$msg_unknown, %di
22864123Skato	call	sc_puts
22964123Skato	jmp	showunit
23064123Skato	# SASI
23164123Skatoshow_sasi:
23264123Skato	movw	$msg_sasi, %di
23364123Skato	call	sc_puts
23464123Skato	jmp	showunit
23564123Skato	# SCSI
23664123Skatoshow_scsi:
23764123Skato	movw	$msg_scsi, %di
23864123Skato	call	sc_puts
23964123Skato	# Display unit number.
24064123Skatoshowunit:
24164123Skato	pop	%ax
24264123Skato	andb	$0x0f, %al
24364123Skato	addb	$'0', %al
24464123Skato	call	sc_putc
24564123Skato	incw	%si
24664123Skato	pop	%cx
24764123Skato	loop	showdev_loop
24864123Skato	movw	ndevice, %dx
24964123Skato	addw	$5, %dx
25064123Skato	movw	$2, %cx
25164123Skato	call	sc_goto
25264123Skato	movw	$msg_exitmenu, %di
25364123Skato	call	sc_puts
25464123Skato	ret
25564123Skato
25664123Skato	.data
257167657Skato	.global	curdevice, ndevice
25864123Skatondevice:	.word	0		# number of device
25964123Skatocurdevice:	.word	0		# current device
26064123Skato
26164123Skato	.global	ishireso
26264123Skatoishireso:	.byte	0
26364123Skato
264168457Skatotitle:		.asciz	"PC98 Boot Selector Version 1.2"
265168457Skatocopyright:	.ascii	"(C)Copyright 1999-2007 KATO Takenori. "
26664123Skato		.asciz	"All rights reserved."
26764123Skatomsg_device:	.asciz	"Device"
26864123Skatomsg_sasi:	.asciz	"SASI/IDE unit "
26964123Skatomsg_scsi:	.asciz	"SCSI ID "
27064123Skatomsg_unknown:	.asciz	"unknown unit "
27164123Skatomsg_exitmenu:	.asciz	"Exit this menu"
27264123Skatomsg_usage1:	.asciz	"Device list"
27364123Skatomsg_usage2:	.asciz	"UP, DOWN: select boot device"
27464123Skatomsg_usage3:	.asciz	"RETURN: move to slice list"
27564123Skatomsg_usage4:	.asciz	"Slice list"
27664123Skatomsg_usage5:	.asciz	"UP, DOWN: select boot slice"
27764123Skatomsg_usage6:	.asciz	"RETURN: boot"
27864123Skatomsg_usage7:	.asciz	"SPACE: toggle default"
27964123Skatomsg_usage8:	.asciz	"ESC: move to device list"
28064123Skatomsg_usage9:	.asciz	"LEGEND"
28164123Skatomsg_usage10:	.asciz	">>: selected device/slice"
28264123Skatomsg_usage11:	.asciz	"*: default slice to boot"
28364123Skatomsg_usage12:	.asciz	"!: unbootable slice"
284167657Skato
285167657Skato	.bss
286167657Skato	.global	daua, secsize, defflagoff, defpartoff
287167657Skato	.global	maxpart, partoff
288167657Skatodaua:		.space	12		# DA/DU list
289167657Skatosecsize:	.space	12 * 2		# Sector soize
290167657Skatodefflagoff:	.space	12 * 2
291167657Skatodefpartoff:	.space	12 * 2
292167657Skatomaxpart:	.space	12 * 2
293167657Skatopartoff:	.space	12 * 2
294