summaryrefslogtreecommitdiffstats
path: root/sys/boot/pc98/boot0.5/disk.s
blob: 6e8034898c82b768345b803642f5675b2c2e6c81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# Copyright (c) KATO Takenori, 1999, 2000.
#
# All rights reserved.  Unpublished rights reserved under the copyright
# laws of Japan.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer as
#    the first lines of this file unmodified.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# $FreeBSD$
#

	.code16
	.text
#
# Check magic number at the end of the sector 0
#
check_magic:
	movw	curdevice, %si
	shlw	%si
	movw	secsize(%si), %bx
	decw	%bx
	decw	%bx
	movw	iplbuf(%bx), %ax
	cmpw	$0xaa55, %ax
	je	magic_ok
	movw	$1, %ax
	ret
magic_ok:
	xorw	%ax, %ax
	ret

#
# Copy partition table from buffer to parttable.
#
setup_partition:
	push	%cs
	pop	%es
	movw	curdevice, %bx
	shlw	%bx
	movw	maxpart(%bx), %cx	# %cx = max num of partitions
	movw	partoff(%bx), %di
	movw	%di, %bx		# %bx = offset to partition table
	xorw	%dx, %dx		# %dx = partition number
setup_partition_loop:
	push	%cx
	movw	%dx, %si
	movb	$5, %cl
	shlw	%cl, %si
	addw	%bx, %si
	movb	iplbuf(%si), %al
	orb	%al, %al
	jz	unused_partition
	addw	$iplbuf, %si
	movw	npartition, %ax
	movw	%ax, %di
	movb	$5, %cl
	shlw	%cl, %di
	addw	$parttable, %di
	movw	$32, %cx
	rep
	movsb
	movw	%ax, %di
	addw	$partnum, %di
	movb	%dl, (%di)
	incw	npartition
unused_partition:
	incw	%dx
	pop	%cx
	loop	setup_partition_loop
	ret

#
# Read IPL and partition table in the current device.
#
	.global	read_ipl
read_ipl:
	movw	curdevice, %ax
	movw	%ax, %si		# %si = device number
	movw	%ax, %di
	shlw	%di

	movw	%cs, %ax
	movw	%ax, %es
	movb	$6, %ah
	movb	daua(%si), %al
	movw	$0x400, %bx
	xorw	%cx, %cx
	xorw	%dx, %dx
	movw	$iplbuf, %bp
	int	$0x1b
	jc	read_ipl_error
	movw	defflagoff(%di), %bx
	movb	iplbuf(%bx), %al
	movb	%al, defpartflag
	incw	%bx
	movb	iplbuf(%bx), %al
	movb	%al, defpartnum
	movw	$0, npartition
	call	check_magic
	orw	%ax, %ax
	jnz	no_magic
	call	setup_partition
no_magic:
	xorw	%ax, %ax
read_ipl_error:
	xorw	%bx, %bx
	movw	%bx, %es
	ret

#
# Restore IPL from the buffer
#
	.global	write_ipl
write_ipl:
	movw	curdevice, %ax
	movw	%ax, %si
	movw	%ax, %di
	shlw	%di

	# Restore default boot partition info.
	movw	defflagoff(%di), %bx
	movb	defpartflag, %al
	movb	%al, iplbuf(%bx)
	incw	%bx
	movb	defpartnum, %al
	movb	%al, iplbuf(%bx)

	movw	%cs, %ax
	movw	%ax, %es
	movb	$5, %ah
	movb	daua(%si), %al
	movw	secsize(%di), %bx
	xorw	%cx, %cx
	xorw	%dx, %dx
	movw	$iplbuf, %bp
	int	$0x1b
	jc	write_ipl_error
	xorw	%ax, %ax
write_ipl_error:
	xorw	%bx, %bx
	movw	%bx, %es
	ret

#
# Scan HDD devices
#
	.global	scan_sasi, scan_scsi
	# Scan SASI disk
scan_sasi:
	# SASI Disk
	movw	$4, %cx
	movw	$0x0001, %ax	# %ah =  unit number, %al = for bit operation

sasi_loop:
	movw	%si, %di
	shlw	%di
	movw	$0x55d, %bx		# DISK_EQUIP
	call	read_biosparam
	testb	%al, %dl
	jz	no_sasi_unit
	movb	$0x80, %dh
	addb	%ah, %dh		# %dh = DA/UA
	movb	%dh, daua(%si)		# Store DA/UA

	# Try new sense command
	push	%ax
	push	%cx
	movb	%dh, %al
	movb	$0x84, %ah
	int	$0x1b
	pop	%cx
	pop	%ax
	jc	err_newsense
	movw	%bx, %dx
	jmp	found_sasi_unit

err_newsense:
	movw	$0x457, %bx		# capacity & sector size of IDE HDD
	call	read_biosparam
	orb	%ah, %ah
	jz	sasi_1
	cmpb	$1, %ah
	jz	sasi_2

	# SASI #3/#4
	movw	$512, %dx		# XXX
	jmp	found_sasi_unit

sasi_1:
	# SASI #1
	testb	$0x80, %dl
	jz	sasi_256
	jmp	sasi_512
sasi_2:
	# SASI #2
	testb	$0x40, %dl
	jz	sasi_256
	jmp	sasi_512

sasi_256:
	movw	$256, %dx
	jmp	found_sasi_unit
sasi_512:
	movw	$512, %dx
found_sasi_unit:
	movw	%dx, secsize(%di)
	incw	%si
no_sasi_unit:
	incb	%ah
	shlb	%al
	loop	sasi_loop
	ret

#
# Scan SCSI disk
#	SI	number of disks
#	destroyed: %ax, %bx, %cx, %dx
scan_scsi:
	movw	$8, %cx
	movw	$0x0001, %ax	# %ah = ID number, %al = for bit operation
scsi_loop:
	# Check whether drive exist.
	movw	%si, %di
	shlw	%di
	movw	$0x482, %bx		# DISK_EQUIPS
	call	read_biosparam
	testb	%al, %dl
	jz	no_scsi_unit
	xorw	%bx, %bx
	movb	%ah, %bl
	shlw	%bx
	shlw	%bx
	addw	$0x460, %bx		# SCSI parameter block
	call	read_biosparam
	orb	%dl, %dl
	jz	no_scsi_unit

	# SCSI harddrive found.
	movb	$0xa0, %dh
	addb	%ah, %dh
	movb	%dh, daua(%si)

	# Check sector size.
	addw	$3, %bx
	call	read_biosparam
	andb	$0x30, %dl
	cmpb	$0x20, %dl
	je	scsi_1024
	cmpb	$0x10, %dl
	je	scsi_512
	movw	$256, %dx
	jmp	found_scsi
scsi_1024:
	movw	$1024, %dx
	jmp	found_scsi
scsi_512:
	movw	$512, %dx
found_scsi:
	movw	%dx, secsize(%di)
	incw	%si
no_scsi_unit:
	incb	%ah
	shlb	%al
	loop	scsi_loop
	ret

	.data
	.global	defpartflag, defpartnum, npartition
defpartflag:	.byte	0
defpartnum:	.byte	0
npartition:	.word	0		# number of partitions

	.bss
	.global	partnum, parttable
iplbuf:		.space	0x400		# Read buffer for IPL
partnum:	.space	32		# Index of parttable
parttable:	.space	1024		# Copy of valid partition table
OpenPOWER on IntegriCloud