1# Copyright (c) KATO Takenori, 1999, 2000, 2007.
2#
3# All rights reserved.  Unpublished rights reserved under the copyright
4# laws of Japan.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer as
12#    the first lines of this file unmodified.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27#
28# $FreeBSD$
29#
30	.global	main
31	.code16
32
33	.text
34main:
35	# Check hireso mode
36	movw	$0x501, %bx		# BIOS_FLAG
37	call	read_biosparam
38	testb	$0x08, %dl
39	jz	normalmode
40	movb	$1, ishireso
41normalmode:
42	call	sc_init
43
44	# Display title and copyright.
45	movw	$title, %di
46	call	sc_puts
47	xorw	%cx, %cx
48	movw	$1, %dx
49	call	sc_goto
50	movw	$copyright, %di
51	call	sc_puts
52
53	# Scan hard drives
54	xorw	%si, %si		# number of partition
55	call	scan_sasi		# SASI/IDE
56	call	scan_scsi		# SCSI
57	movw	%si, ndevice
58	orw	%si, %si
59	jnz	drives_found
60	jmp	exit			# No hard drives
61
62drives_found:
63	# Setup sector size dependent parameters
64	movw	%si, %cx		# %cx = number of devices
65setup_loop:
66	movw	%cx, %di
67	decw	%di
68	shlw	%di
69	movw	secsize(%di), %ax
70	cmpw	$1024, %ax
71	je	setup_1024
72	cmpw	$512, %ax
73	je	setup_512
74	# 256 bytes/sector
75	movw	$0x100, partoff(%di)
76	movw	$0x0fa, defflagoff(%di)
77	movw	$0x0fb, defpartoff(%di)
78	movw	$8, maxpart(%di)
79	jmp	setup_secsize_end
80	# 1024 bytes/sector
81setup_1024:
82	# XXX Fix me!
83	movw	$0x400, partoff(%di)
84	movw	$0x3fa, defflagoff(%di)
85	movw	$0x3fb, defpartoff(%di)
86	movb	$32, maxpart(%di)
87	jmp	setup_secsize_end
88	# 512 bytes/sector
89setup_512:
90	movw	$0x200, partoff(%di)
91	movw	$0x1fa, defflagoff(%di)
92	movw	$0x1fb, defpartoff(%di)
93	movb	$16, maxpart(%di)
94setup_secsize_end:
95	loop	setup_loop
96
97	# For debug with floppy, fake the parameter.
98	movw	$0x584, %bx		# DISK_BOOT
99	call	read_biosparam
100	andb	$0xf0, %dl
101	cmpb	$0x90, %ah
102	jne	boot_from_hdd
103	movb	daua, %dl
104	call	write_biosparam
105
106boot_from_hdd:
107	movw	$500, %cx
108wait_0_5:
109	call	wait1ms
110	loop	wait_0_5
111
112	# If the TAB is pressed, don't try to boot from default partition
113	xorw	%di, %di		# flag
114wait_key_release:
115	call	sc_iskeypress
116	orw	%ax, %ax
117	jz	key_release		# KBD buffer empty.
118	call	sc_getc
119	cmpb	$0x0f, %ah		# TAB
120	jne	wait_key_release
121	# TAB pressed
122	movw	$1, %di
123	jmp	wait_key_release
124key_release:
125	orw	%di, %di
126	jnz	dont_try_default	# TAB pressed.
127	call	trydefault
128	# Default partition not found.
129dont_try_default:
130	call	show_usage
131	call	showdevices
132	call	selector
133exit:
134	ret
135#
136# Display usage
137#
138show_usage:
139	movw	$44, %cx
140	movw	$3, %dx
141	call	sc_goto
142	movw	$msg_usage1, %di
143	call	sc_puts
144	movw	$44, %cx
145	movw	$4, %dx
146	call	sc_goto
147	movw	$msg_usage2, %di
148	call	sc_puts
149	movw	$44, %cx
150	movw	$5, %dx
151	call	sc_goto
152	movw	$msg_usage3, %di
153	call	sc_puts
154	movw	$44, %cx
155	movw	$7, %dx
156	call	sc_goto
157	movw	$msg_usage4, %di
158	call	sc_puts
159	movw	$44, %cx
160	movw	$8, %dx
161	call	sc_goto
162	movw	$msg_usage5, %di
163	call	sc_puts
164	movw	$44, %cx
165	movw	$9, %dx
166	call	sc_goto
167	movw	$msg_usage6, %di
168	call	sc_puts
169	movw	$44, %cx
170	movw	$10, %dx
171	call	sc_goto
172	movw	$msg_usage7, %di
173	call	sc_puts
174	movw	$44, %cx
175	movw	$11, %dx
176	call	sc_goto
177	movw	$msg_usage8, %di
178	call	sc_puts
179	movw	$44, %cx
180	movw	$16, %dx
181	call	sc_goto
182	movw	$msg_usage9, %di
183	call	sc_puts
184	movw	$44, %cx
185	movw	$17, %dx
186	call	sc_goto
187	movw	$msg_usage10, %di
188	call	sc_puts
189	movw	$44, %cx
190	movw	$18, %dx
191	call	sc_goto
192	movw	$msg_usage11, %di
193	call	sc_puts
194	movw	$44, %cx
195	movw	$19, %dx
196	call	sc_goto
197	movw	$msg_usage12, %di
198	call	sc_puts
199	ret
200
201#
202# Display device list
203#
204showdevices:
205	movw	$2, %cx
206	movw	$4, %dx
207	call	sc_goto
208	movw	$msg_device, %di
209	call	sc_puts
210	xorw	%si, %si		# %si = device number
211	movw	ndevice, %cx		# %cx = number of devices
212showdev_loop:
213	push	%cx
214	movw	$2, %cx
215	movw	$5, %dx
216	addw	%si, %dx
217	call	sc_goto
218	# Check DA
219	movb	daua(%si), %al
220	push	%ax
221	andb	$0xf0, %al
222	cmpb	$0x80, %al
223	je	show_sasi
224	cmpb	$0xa0, %al
225	je	show_scsi
226	# unknown device
227	movw	$msg_unknown, %di
228	call	sc_puts
229	jmp	showunit
230	# SASI
231show_sasi:
232	movw	$msg_sasi, %di
233	call	sc_puts
234	jmp	showunit
235	# SCSI
236show_scsi:
237	movw	$msg_scsi, %di
238	call	sc_puts
239	# Display unit number.
240showunit:
241	pop	%ax
242	andb	$0x0f, %al
243	addb	$'0', %al
244	call	sc_putc
245	incw	%si
246	pop	%cx
247	loop	showdev_loop
248	movw	ndevice, %dx
249	addw	$5, %dx
250	movw	$2, %cx
251	call	sc_goto
252	movw	$msg_exitmenu, %di
253	call	sc_puts
254	ret
255
256	.data
257	.global	curdevice, ndevice
258ndevice:	.word	0		# number of device
259curdevice:	.word	0		# current device
260
261	.global	ishireso
262ishireso:	.byte	0
263
264title:		.asciz	"PC98 Boot Selector Version 1.2"
265copyright:	.ascii	"(C)Copyright 1999-2007 KATO Takenori. "
266		.asciz	"All rights reserved."
267msg_device:	.asciz	"Device"
268msg_sasi:	.asciz	"SASI/IDE unit "
269msg_scsi:	.asciz	"SCSI ID "
270msg_unknown:	.asciz	"unknown unit "
271msg_exitmenu:	.asciz	"Exit this menu"
272msg_usage1:	.asciz	"Device list"
273msg_usage2:	.asciz	"UP, DOWN: select boot device"
274msg_usage3:	.asciz	"RETURN: move to slice list"
275msg_usage4:	.asciz	"Slice list"
276msg_usage5:	.asciz	"UP, DOWN: select boot slice"
277msg_usage6:	.asciz	"RETURN: boot"
278msg_usage7:	.asciz	"SPACE: toggle default"
279msg_usage8:	.asciz	"ESC: move to device list"
280msg_usage9:	.asciz	"LEGEND"
281msg_usage10:	.asciz	">>: selected device/slice"
282msg_usage11:	.asciz	"*: default slice to boot"
283msg_usage12:	.asciz	"!: unbootable slice"
284
285	.bss
286	.global	daua, secsize, defflagoff, defpartoff
287	.global	maxpart, partoff
288daua:		.space	12		# DA/DU list
289secsize:	.space	12 * 2		# Sector soize
290defflagoff:	.space	12 * 2
291defpartoff:	.space	12 * 2
292maxpart:	.space	12 * 2
293partoff:	.space	12 * 2
294