Annotation of prex/boot/i386/utils/mkboot/mkboot.s, Revision 1.1
1.1 ! nbrk 1: #
! 2: # Copyright (c) 2005, Kohsuke Ohtani
! 3: # All rights reserved.
! 4: #
! 5: # Redistribution and use in source and binary forms, with or without
! 6: # modification, are permitted provided that the following conditions
! 7: # are met:
! 8: # 1. Redistributions of source code must retain the above copyright
! 9: # notice, this list of conditions and the following disclaimer.
! 10: # 2. Redistributions in binary form must reproduce the above copyright
! 11: # notice, this list of conditions and the following disclaimer in the
! 12: # documentation and/or other materials provided with the distribution.
! 13: # 3. Neither the name of the author nor the names of any co-contributors
! 14: # may be used to endorse or promote products derived from this software
! 15: # without specific prior written permission.
! 16: #
! 17: # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
! 18: # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 19: # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 20: # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
! 21: # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 22: # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 23: # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 24: # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 25: # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 26: # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 27: # SUCH DAMAGE.
! 28: #
! 29:
! 30: #
! 31: # mkboot.S - DOS utility to install boot sector into disk
! 32: #
! 33: # Usage: mkboot [drive]
! 34: # ex. mkboot a:
! 35: #
! 36: # Read image file "bootsect.bin" and write it to disk.
! 37: # Current boot sector is automatically saved as file "bootsect.dbg".
! 38: #
! 39: # Prerequisite: Correct BIOS Parameter Block (BPB) has already stored
! 40: # in the target disk device. Generally, this data is written by a format tool.
! 41: #
! 42: .code16
! 43: .text
! 44: .align 1
! 45:
! 46: #
! 47: # Entry point
! 48: #
! 49: main:
! 50: movw %cs, %ax
! 51: movw %ax, %ds
! 52: movw %ax, %es
! 53: cld
! 54:
! 55: call get_drive # Get drive information
! 56: jc dos_exit
! 57:
! 58: call read_file # Read boot sector file
! 59: jc dos_exit
! 60:
! 61: call read_bsec # Read actual boot sector
! 62: jc dos_exit
! 63:
! 64: call store_bpb # Copy BPB data
! 65: jc dos_exit
! 66:
! 67: # cmp $1, dbg_mode
! 68: # je skip_write
! 69:
! 70: call write_bsec # Write boot sector
! 71: jc dos_exit
! 72: skip_write:
! 73:
! 74: call debug_file # Put to file for debug
! 75: movw $msg_ok, %dx
! 76: call puts
! 77:
! 78: dos_exit:
! 79: movb $0x4c, %ah # Return to DOS
! 80: int $0x21
! 81: ret
! 82:
! 83: #
! 84: # Get specified drive
! 85: #
! 86: get_drive:
! 87: pushw %es
! 88: pushaw
! 89:
! 90: movb $0x62, %ah # Get DOS PSP
! 91: int $0x21
! 92:
! 93: movw %bx, %es
! 94: movb %es:(0x80), %al # Get parameter size
! 95: cmpb $0, %al # No parameter ?
! 96: je invalid_arg
! 97:
! 98: movb %es:(0x82), %al # Get drive character
! 99: cmpb $0x41, %al # A ?
! 100: jb invalid_arg
! 101: cmpb $0x5a, %al # Z ?
! 102: ja chk_low
! 103: subb $0x41, %al # - A
! 104: jmp drive_ok
! 105:
! 106: chk_low:
! 107: cmpb $0x61, %al # a ?
! 108: jb invalid_arg
! 109: cmpb $0x7a, %al # z ?
! 110: ja invalid_arg
! 111: subb $0x61, %al # - a
! 112:
! 113: drive_ok:
! 114: cmpb $2, %al
! 115: jb skip_verify
! 116:
! 117: movw $err_dbg, %dx
! 118: pushw %ax
! 119: call puts
! 120: movb $1, dbg_mode
! 121: popw %ax
! 122:
! 123: skip_verify:
! 124: movb %al, drive
! 125:
! 126: movb $0, phys_drive
! 127: cmp $2, %al
! 128: jb not_hdd
! 129: movb $0x80, phys_drive
! 130: not_hdd:
! 131: popaw
! 132: popw %es
! 133: clc
! 134: ret
! 135:
! 136: invalid_arg:
! 137: popaw
! 138: popw %es
! 139: movw $msg_usage, %dx
! 140: call puts
! 141: stc
! 142: ret
! 143:
! 144: #
! 145: # puts
! 146: #
! 147: puts:
! 148: movb $0x09, %ah
! 149: int $0x21
! 150: ret
! 151:
! 152: #
! 153: # Read file
! 154: #
! 155: read_file:
! 156: pushaw
! 157: movw $bsec_name, %dx
! 158: movb $0x3d, %ah # Open file
! 159: movb $0x80, %al
! 160: int $0x21
! 161: jc error_read
! 162:
! 163: movw %ax, %bx
! 164: movw $tmpSect, %dx
! 165: movw 512, %cx
! 166: movb $0x3f, %ah # Read file
! 167: int $0x21
! 168: jc error_read
! 169:
! 170: mov $0x3e, %ah # Close file
! 171: int $0x21
! 172:
! 173: movw $tmpSect, %bx
! 174: movw 510(%bx), %ax
! 175: cmpw $0xaa55, %ax
! 176: jne error_file
! 177: popaw
! 178: clc
! 179: ret
! 180:
! 181: error_file:
! 182: movw $err_bad_file, %dx
! 183: call puts
! 184: popaw
! 185: stc
! 186: ret
! 187:
! 188: error_read:
! 189: movw $err_not_found, %dx
! 190: call puts
! 191: popaw
! 192: stc
! 193: ret
! 194:
! 195: #
! 196: # Read boot sector image from disk
! 197: #
! 198: read_bsec:
! 199: pushaw
! 200: movw 5, %cx
! 201: retry_loop:
! 202: pushw %cx
! 203:
! 204: movw $dos_packet, %bx # bx = buffer address
! 205: movw $orgSect, %ax
! 206: movw %ax, 6(%bx)
! 207: movw %cs, %ax
! 208: movw %ax, 8(%bx)
! 209: movl $0, (%bx) # Sector = 0
! 210: movw $1, 4(%bx) # Num of sector = 1
! 211: movb drive, %al # al = drive number
! 212: movw $0xffff, %cx # Extended call
! 213: int $0x25
! 214: popw %ax
! 215:
! 216: popw %cx
! 217: jnc read_ok
! 218: loopw retry_loop
! 219:
! 220: movw $err_read, %dx
! 221: call puts
! 222: stc
! 223: read_ok:
! 224: popaw
! 225: ret
! 226:
! 227:
! 228: #
! 229: # Write boot sector image to disk
! 230: #
! 231: write_bsec:
! 232: pushaw
! 233: movw $dos_packet, %bx # bx = buffer address
! 234: movw $tmpSect, %ax
! 235: movw %ax, 6(%bx)
! 236: movw %cs, %ax
! 237: movw %ax, 8(%bx)
! 238: movl $0, (%bx) # Sector = 0
! 239: movw $1, 4(%bx) # Num of sector = 1
! 240: movb drive, %al # al = drive number
! 241: movw $0xffff, %cx # Extended call
! 242: int $0x26
! 243: popw %ax
! 244:
! 245: jnc write_ok
! 246:
! 247: movw $err_write, %dx
! 248: call puts
! 249: popaw
! 250: stc
! 251: write_ok:
! 252: popaw
! 253: ret
! 254:
! 255: #
! 256: # Store BPB from current disk BPB
! 257: #
! 258: store_bpb:
! 259: cld
! 260: movw $orgSect, %si
! 261: addw $3, %si # Skip jump instruction
! 262: addw $8, %si # Skip OEM ID
! 263: movw $tmpSect, %di
! 264: addw $3, %di
! 265: addw $8, %di
! 266: movw $0x33, %cx # Copy disk parameter
! 267: rep
! 268: movsb
! 269:
! 270: ### movw $tmpSect, %di # Update physical drive
! 271: ### addw $0x24, %di
! 272: ### movb phys_drive, %al
! 273: ### movb %al, (%di)
! 274:
! 275: clc
! 276: ret
! 277:
! 278: #
! 279: # Create new BPB file for debug
! 280: #
! 281: debug_file:
! 282: movw $dbg_name, %dx
! 283: movb $0x3c, %ah # Create file
! 284: movw $0, %cx
! 285: int $0x21
! 286: jc exit_debug
! 287:
! 288: movw %ax, %bx
! 289: movw $tmpSect, %dx
! 290: movw $512, %cx
! 291: movb $0x40, %ah # Read file
! 292: int $0x21
! 293: jc exit_debug
! 294:
! 295: movb $0x3e, %ah # Close file
! 296: int $0x21
! 297:
! 298: exit_debug:
! 299: ret
! 300:
! 301: #
! 302: # Data
! 303: #
! 304: drive: .byte 0
! 305: phys_drive: .byte 0
! 306: dbg_mode: .byte 0
! 307:
! 308: .align 4
! 309: dos_packet:
! 310: .long 0 # Sector
! 311: .word 1 # Number of sector
! 312: .word 0 # Buffer offset
! 313: .word 0 # Buffer segment
! 314:
! 315: bsec_name: .asciz "bootsect.bin"
! 316: dbg_name: .asciz "bootsect.dbg"
! 317:
! 318: msg_usage: .ascii "mkboot [drive]"
! 319: .byte 0xd,0xa,0x24
! 320: msg_ok: .ascii "System transferred"
! 321: .byte 0xd,0xa,0x24
! 322: err_dbg: .ascii "Warning: Write to HDD!"
! 323: .byte 0xd,0xa,0x24
! 324: err_not_found: .ascii "Error: bootsect.bin was not found"
! 325: .byte 0xd,0xa,0x24
! 326: err_bad_file: .ascii "Error: bootsect.bin is not valid"
! 327: .byte 0xd,0xa,0x24
! 328: err_read: .ascii "Error: Disk read errror"
! 329: .byte 0xd,0xa,0x24
! 330: err_write: .ascii "Error: Disk write errror"
! 331: .byte 0xd,0xa,0x24
! 332:
! 333: .align 16
! 334: orgSect: .fill 512,1,0
! 335: tmpSect: .fill 512,1,0
! 336:
CVSweb