Cosmetic cleanups.
[dragonfly.git] / sys / boot / pc98 / boot0.5 / disk.s
1 # Copyright (c) KATO Takenori, 1999, 2000.
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: src/sys/boot/pc98/boot0.5/disk.s,v 1.4 2000/08/31 10:24:06 kato Exp $
29 # $DragonFly: src/sys/boot/pc98/boot0.5/Attic/disk.s,v 1.3 2003/11/10 06:08:38 dillon Exp $
30 #
31
32         .code16
33         .text
34 #
35 # Check magic number at the end of the sector 0
36 #
37 check_magic:
38         movw    curdevice, %si
39         shlw    %si
40         movw    secsize(%si), %bx
41         decw    %bx
42         decw    %bx
43         movw    iplbuf(%bx), %ax
44         cmpw    $0xaa55, %ax
45         je      magic_ok
46         movw    $1, %ax
47         ret
48 magic_ok:
49         xorw    %ax, %ax
50         ret
51
52 #
53 # Copy partition table from buffer to parttable.
54 #
55 setup_partition:
56         push    %cs
57         pop     %es
58         movw    curdevice, %bx
59         shlw    %bx
60         movw    maxpart(%bx), %cx       # %cx = max num of partitions
61         movw    partoff(%bx), %di
62         movw    %di, %bx                # %bx = offset to partition table
63         xorw    %dx, %dx                # %dx = partition number
64 setup_partition_loop:
65         push    %cx
66         movw    %dx, %si
67         movb    $5, %cl
68         shlw    %cl, %si
69         addw    %bx, %si
70         movb    iplbuf(%si), %al
71         orb     %al, %al
72         jz      unused_partition
73         addw    $iplbuf, %si
74         movw    npartition, %ax
75         movw    %ax, %di
76         movb    $5, %cl
77         shlw    %cl, %di
78         addw    $parttable, %di
79         movw    $32, %cx
80         rep
81         movsb
82         movw    %ax, %di
83         addw    $partnum, %di
84         movb    %dl, (%di)
85         incw    npartition
86 unused_partition:
87         incw    %dx
88         pop     %cx
89         loop    setup_partition_loop
90         ret
91
92 #
93 # Read IPL and partition table in the current device.
94 #
95         .global read_ipl
96 read_ipl:
97         movw    curdevice, %ax
98         movw    %ax, %si                # %si = device number
99         movw    %ax, %di
100         shlw    %di
101
102         movw    %cs, %ax
103         movw    %ax, %es
104         movb    $6, %ah
105         movb    daua(%si), %al
106         movw    $0x400, %bx
107         xorw    %cx, %cx
108         xorw    %dx, %dx
109         movw    $iplbuf, %bp
110         int     $0x1b
111         jc      read_ipl_error
112         movw    defflagoff(%di), %bx
113         movb    iplbuf(%bx), %al
114         movb    %al, defpartflag
115         incw    %bx
116         movb    iplbuf(%bx), %al
117         movb    %al, defpartnum
118         movw    $0, npartition
119         call    check_magic
120         orw     %ax, %ax
121         jnz     no_magic
122         call    setup_partition
123 no_magic:
124         xorw    %ax, %ax
125 read_ipl_error:
126         xorw    %bx, %bx
127         movw    %bx, %es
128         ret
129
130 #
131 # Restore IPL from the buffer
132 #
133         .global write_ipl
134 write_ipl:
135         movw    curdevice, %ax
136         movw    %ax, %si
137         movw    %ax, %di
138         shlw    %di
139
140         # Restore default boot partition info.
141         movw    defflagoff(%di), %bx
142         movb    defpartflag, %al
143         movb    %al, iplbuf(%bx)
144         incw    %bx
145         movb    defpartnum, %al
146         movb    %al, iplbuf(%bx)
147
148         movw    %cs, %ax
149         movw    %ax, %es
150         movb    $5, %ah
151         movb    daua(%si), %al
152         movw    secsize(%di), %bx
153         xorw    %cx, %cx
154         xorw    %dx, %dx
155         movw    $iplbuf, %bp
156         int     $0x1b
157         jc      write_ipl_error
158         xorw    %ax, %ax
159 write_ipl_error:
160         xorw    %bx, %bx
161         movw    %bx, %es
162         ret
163
164 #
165 # Scan HDD devices
166 #
167         .global scan_sasi, scan_scsi
168         # Scan SASI disk
169 scan_sasi:
170         # SASI Disk
171         movw    $4, %cx
172         movw    $0x0001, %ax    # %ah =  unit number, %al = for bit operation
173
174 sasi_loop:
175         movw    %si, %di
176         shlw    %di
177         movw    $0x55d, %bx             # DISK_EQUIP
178         call    read_biosparam
179         testb   %al, %dl
180         jz      no_sasi_unit
181         movb    $0x80, %dh
182         addb    %ah, %dh                # %dh = DA/UA
183         movb    %dh, daua(%si)          # Store DA/UA
184
185         # Try new sense command
186         push    %ax
187         push    %cx
188         movb    %dh, %al
189         movb    $0x84, %ah
190         int     $0x1b
191         pop     %cx
192         pop     %ax
193         jc      err_newsense
194         movw    %bx, %dx
195         jmp     found_sasi_unit
196
197 err_newsense:
198         movw    $0x457, %bx             # capacity & sector size of IDE HDD
199         call    read_biosparam
200         orb     %ah, %ah
201         jz      sasi_1
202         cmpb    $1, %ah
203         jz      sasi_2
204
205         # SASI #3/#4
206         movw    $512, %dx               # XXX
207         jmp     found_sasi_unit
208
209 sasi_1:
210         # SASI #1
211         testb   $0x80, %dl
212         jz      sasi_256
213         jmp     sasi_512
214 sasi_2:
215         # SASI #2
216         testb   $0x40, %dl
217         jz      sasi_256
218         jmp     sasi_512
219
220 sasi_256:
221         movw    $256, %dx
222         jmp     found_sasi_unit
223 sasi_512:
224         movw    $512, %dx
225 found_sasi_unit:
226         movw    %dx, secsize(%di)
227         incw    %si
228 no_sasi_unit:
229         incb    %ah
230         shlb    %al
231         loop    sasi_loop
232         ret
233
234 #
235 # Scan SCSI disk
236 #       SI      number of disks
237 #       destroyed: %ax, %bx, %cx, %dx
238 scan_scsi:
239         movw    $8, %cx
240         movw    $0x0001, %ax    # %ah = ID number, %al = for bit operation
241 scsi_loop:
242         # Check whether drive exist.
243         movw    %si, %di
244         shlw    %di
245         movw    $0x482, %bx             # DISK_EQUIPS
246         call    read_biosparam
247         testb   %al, %dl
248         jz      no_scsi_unit
249         xorw    %bx, %bx
250         movb    %ah, %bl
251         shlw    %bx
252         shlw    %bx
253         addw    $0x460, %bx             # SCSI paramter block
254         call    read_biosparam
255         orb     %dl, %dl
256         jz      no_scsi_unit
257
258         # SCSI harddrive found.
259         movb    $0xa0, %dh
260         addb    %ah, %dh
261         movb    %dh, daua(%si)
262
263         # Check sector size.
264         addw    $3, %bx
265         call    read_biosparam
266         andb    $0x30, %dl
267         cmpb    $0x20, %dl
268         je      scsi_1024
269         cmpb    $0x10, %dl
270         je      scsi_512
271         movw    $256, %dx
272         jmp     found_scsi
273 scsi_1024:
274         movw    $1024, %dx
275         jmp     found_scsi
276 scsi_512:
277         movw    $512, %dx
278 found_scsi:
279         movw    %dx, secsize(%di)
280         incw    %si
281 no_scsi_unit:
282         incb    %ah
283         shlb    %al
284         loop    scsi_loop
285         ret
286
287         .data
288         .global partnum, parttable, defpartflag, defpartnum, npartition
289 partnum:        .space  32              # Index of parttable
290 parttable:      .space  1024            # Copy of valid partition table
291 defpartflag:    .byte   0
292 defpartnum:     .byte   0
293 npartition:     .word   0               # number of partitions
294         .bss
295 iplbuf:         .space  0x400