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