1999-05-24 22:36:24 +00:00
|
|
|
#
|
|
|
|
# Copyright (c) 1999 Robert Nordier
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms are freely
|
|
|
|
# permitted provided that the above copyright notice and this
|
|
|
|
# paragraph and the following disclaimer are duplicated in all
|
|
|
|
# such forms.
|
|
|
|
#
|
|
|
|
# This software is provided "AS IS" and without any express or
|
|
|
|
# implied warranties, including, without limitation, the implied
|
|
|
|
# warranties of merchantability and fitness for a particular
|
|
|
|
# purpose.
|
|
|
|
#
|
|
|
|
|
1999-08-28 01:08:13 +00:00
|
|
|
# $FreeBSD$
|
1999-05-24 22:36:24 +00:00
|
|
|
|
2000-06-26 23:58:04 +00:00
|
|
|
# A 512 byte MBR boot manager that simply boots the active partition.
|
1999-05-24 22:36:24 +00:00
|
|
|
|
|
|
|
.set LOAD,0x7c00 # Load address
|
|
|
|
.set EXEC,0x600 # Execution address
|
|
|
|
.set PT_OFF,0x1be # Partition table
|
|
|
|
.set MAGIC,0xaa55 # Magic: bootable
|
|
|
|
|
2000-06-27 20:04:10 +00:00
|
|
|
.set NHRDRV,0x475 # Number of hard drives
|
1999-05-24 22:36:24 +00:00
|
|
|
|
|
|
|
.globl start # Entry point
|
2000-05-23 12:28:31 +00:00
|
|
|
.code16
|
1999-05-24 22:36:24 +00:00
|
|
|
|
2000-06-26 23:58:04 +00:00
|
|
|
#
|
|
|
|
# Setup the segment registers for flat addressing and setup the stack.
|
|
|
|
#
|
1999-05-24 22:36:24 +00:00
|
|
|
start: cld # String ops inc
|
2000-05-23 12:28:31 +00:00
|
|
|
xorw %ax,%ax # Zero
|
|
|
|
movw %ax,%es # Address
|
|
|
|
movw %ax,%ds # data
|
|
|
|
movw %ax,%ss # Set up
|
|
|
|
movw $LOAD,%sp # stack
|
2000-06-26 23:58:04 +00:00
|
|
|
#
|
|
|
|
# Relocate ourself to a lower address so that we are out of the way when
|
|
|
|
# we load in the bootstrap from the partition to boot.
|
|
|
|
#
|
2000-05-23 12:28:31 +00:00
|
|
|
movw $main-EXEC+LOAD,%si # Source
|
|
|
|
movw $main,%di # Destination
|
|
|
|
movw $0x200-(main-start),%cx # Byte count
|
1999-05-24 22:36:24 +00:00
|
|
|
rep # Relocate
|
|
|
|
movsb # code
|
2000-06-26 23:58:04 +00:00
|
|
|
#
|
|
|
|
# Jump to the relocated code.
|
|
|
|
#
|
2000-05-23 12:28:31 +00:00
|
|
|
jmp main-LOAD+EXEC # To relocated code
|
2000-06-26 23:58:04 +00:00
|
|
|
#
|
|
|
|
# Scan the partition table looking for an active entry. Note that %ch is
|
|
|
|
# zero from the repeated string instruction above. We save the offset of
|
|
|
|
# the active partition in %si and scan the entire table to ensure that only
|
|
|
|
# one partition is marked active.
|
|
|
|
#
|
2000-05-23 12:28:31 +00:00
|
|
|
main: xorw %si,%si # No active partition
|
|
|
|
movw $partbl,%bx # Partition table
|
1999-06-19 20:40:32 +00:00
|
|
|
movb $0x4,%cl # Number of entries
|
2000-05-23 12:28:31 +00:00
|
|
|
main.1: cmpb %ch,(%bx) # Null entry?
|
1999-05-24 22:36:24 +00:00
|
|
|
je main.2 # Yes
|
1999-06-19 20:40:32 +00:00
|
|
|
jg err_pt # If 0x1..0x7f
|
2000-05-23 12:28:31 +00:00
|
|
|
testw %si,%si # Active already found?
|
1999-05-24 22:36:24 +00:00
|
|
|
jnz err_pt # Yes
|
2000-05-23 12:28:31 +00:00
|
|
|
movw %bx,%si # Point to active
|
1999-05-24 22:36:24 +00:00
|
|
|
main.2: addb $0x10,%bl # Till
|
|
|
|
loop main.1 # done
|
2000-05-23 12:28:31 +00:00
|
|
|
testw %si,%si # Active found?
|
1999-05-24 22:36:24 +00:00
|
|
|
jnz main.3 # Yes
|
|
|
|
int $0x18 # BIOS: Diskless boot
|
2000-06-26 23:58:04 +00:00
|
|
|
#
|
|
|
|
# Ok, we've found a possible active partition. Check to see that the drive
|
2000-06-27 20:04:10 +00:00
|
|
|
# is a valid hard drive number.
|
2000-06-26 23:58:04 +00:00
|
|
|
#
|
1999-05-24 22:36:24 +00:00
|
|
|
main.3: cmpb $0x80,%dl # Drive valid?
|
|
|
|
jb main.4 # No
|
2000-06-27 20:04:10 +00:00
|
|
|
movb NHRDRV,%dh # Calculate the highest
|
|
|
|
addb $0x80,%dh # drive number available
|
|
|
|
cmpb %dh,%dl # Within range?
|
1999-05-24 22:36:24 +00:00
|
|
|
jb main.5 # Yes
|
2000-06-27 20:04:10 +00:00
|
|
|
main.4: movb (%si),%dl # Load drive
|
2000-06-26 23:58:04 +00:00
|
|
|
#
|
|
|
|
# Ok, now that we have a valid drive and partition entry, load the CHS from
|
|
|
|
# the partition entry and read the sector from the disk.
|
|
|
|
#
|
2000-06-27 20:04:10 +00:00
|
|
|
main.5: movw %sp,%di # Save stack pointer
|
|
|
|
movb 0x1(%si),%dh # Load head
|
2000-05-23 12:28:31 +00:00
|
|
|
movw 0x2(%si),%cx # Load cylinder:sector
|
|
|
|
movw $LOAD,%bx # Transfer buffer
|
2000-06-27 20:04:10 +00:00
|
|
|
cmpb $0xff,%dh # Might we need to use LBA?
|
|
|
|
jnz main.7 # No.
|
|
|
|
cmpw $0xffff,%cx # Do we need to use LBA?
|
|
|
|
jnz main.7 # No.
|
|
|
|
pushw %cx # Save %cx
|
|
|
|
pushw %bx # Save %bx
|
|
|
|
movw $0x55aa,%bx # Magic
|
|
|
|
movb $0x41,%ah # BIOS: EDD extensions
|
|
|
|
int $0x13 # present?
|
|
|
|
jc main.6 # No.
|
|
|
|
cmpw $0xaa55,%bx # Magic ok?
|
|
|
|
jne main.6 # No.
|
|
|
|
testb $0x1,%cl # Packet mode present?
|
|
|
|
jz main.6 # No.
|
|
|
|
popw %bx # Restore %bx
|
|
|
|
pushl $0x0 # Set the LBA
|
|
|
|
pushl 0x8(%si) # address
|
|
|
|
pushw %es # Set the address of
|
|
|
|
pushw %bx # the transfer buffer
|
|
|
|
pushw $0x1 # Read 1 sector
|
|
|
|
pushw $0x10 # Packet length
|
|
|
|
movw %sp,%si # Packer pointer
|
|
|
|
movw $0x4200,%ax # BIOS: LBA Read from disk
|
|
|
|
jmp main.8 # Skip the CHS setup
|
|
|
|
main.6: popw %bx # Restore %bx
|
|
|
|
popw %cx # Restore %cx
|
|
|
|
main.7: movw $0x201,%ax # BIOS: Read from disk
|
|
|
|
main.8: int $0x13 # Call the BIOS
|
|
|
|
movw %di,%sp # Restore stack
|
1999-05-24 22:36:24 +00:00
|
|
|
jc err_rd # If error
|
2000-06-26 23:58:04 +00:00
|
|
|
#
|
|
|
|
# Now that we've loaded the bootstrap, check for the 0xaa55 signature. If it
|
|
|
|
# is present, execute the bootstrap we just loaded.
|
|
|
|
#
|
2000-05-23 12:28:31 +00:00
|
|
|
cmpw $MAGIC,0x1fe(%bx) # Bootable?
|
1999-05-24 22:36:24 +00:00
|
|
|
jne err_os # No
|
2000-05-23 12:28:31 +00:00
|
|
|
jmp *%bx # Invoke bootstrap
|
2000-06-26 23:58:04 +00:00
|
|
|
#
|
|
|
|
# Various error message entry points.
|
|
|
|
#
|
2000-05-23 12:28:31 +00:00
|
|
|
err_pt: movw $msg_pt,%si # "Invalid partition
|
1999-05-24 22:36:24 +00:00
|
|
|
jmp putstr # table"
|
|
|
|
|
2000-05-23 12:28:31 +00:00
|
|
|
err_rd: movw $msg_rd,%si # "Error loading
|
1999-05-24 22:36:24 +00:00
|
|
|
jmp putstr # operating system"
|
|
|
|
|
2000-05-23 12:28:31 +00:00
|
|
|
err_os: movw $msg_os,%si # "Missing operating
|
1999-05-24 22:36:24 +00:00
|
|
|
jmp putstr # system"
|
2000-06-26 23:58:04 +00:00
|
|
|
#
|
|
|
|
# Output an ASCIZ string to the console via the BIOS.
|
|
|
|
#
|
2000-05-23 12:28:31 +00:00
|
|
|
putstr.0: movw $0x7,%bx # Page:attribute
|
1999-05-24 22:36:24 +00:00
|
|
|
movb $0xe,%ah # BIOS: Display
|
|
|
|
int $0x10 # character
|
|
|
|
putstr: lodsb # Get character
|
|
|
|
testb %al,%al # End of string?
|
|
|
|
jnz putstr.0 # No
|
|
|
|
putstr.1: jmp putstr.1 # Await reset
|
|
|
|
|
|
|
|
msg_pt: .asciz "Invalid partition table"
|
|
|
|
msg_rd: .asciz "Error loading operating system"
|
|
|
|
msg_os: .asciz "Missing operating system"
|
|
|
|
|
|
|
|
.org PT_OFF
|
|
|
|
|
|
|
|
partbl: .fill 0x10,0x4,0x0 # Partition table
|
|
|
|
.word MAGIC # Magic number
|