1998-10-12 21:16:26 +00:00
|
|
|
#
|
|
|
|
# Copyright (c) 1998 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$
|
1998-10-12 21:16:26 +00:00
|
|
|
|
2000-06-19 22:50:46 +00:00
|
|
|
# Memory Locations
|
1999-01-10 13:29:52 +00:00
|
|
|
.set MEM_REL,0x700 # Relocation address
|
|
|
|
.set MEM_ARG,0x900 # Arguments
|
1998-10-12 21:16:26 +00:00
|
|
|
.set MEM_ORG,0x7c00 # Origin
|
|
|
|
.set MEM_BUF,0x8c00 # Load area
|
|
|
|
.set MEM_BTX,0x9000 # BTX start
|
|
|
|
.set MEM_JMP,0x9010 # BTX entry point
|
|
|
|
.set MEM_USR,0xa000 # Client start
|
2000-06-26 22:57:16 +00:00
|
|
|
.set BDA_BOOT,0x472 # Boot howto flag
|
2000-06-19 22:50:46 +00:00
|
|
|
|
|
|
|
# Partition Constants
|
1998-10-12 21:16:26 +00:00
|
|
|
.set PRT_OFF,0x1be # Partition offset
|
|
|
|
.set PRT_NUM,0x4 # Partitions
|
|
|
|
.set PRT_BSD,0xa5 # Partition type
|
|
|
|
|
2000-06-19 22:50:46 +00:00
|
|
|
# Flag Bits
|
|
|
|
.set FL_PACKET,0x80 # Packet mode
|
|
|
|
|
|
|
|
# Misc. Constants
|
1998-10-12 21:16:26 +00:00
|
|
|
.set SIZ_PAG,0x1000 # Page size
|
|
|
|
.set SIZ_SEC,0x200 # Sector size
|
|
|
|
|
|
|
|
.globl start
|
|
|
|
.globl xread
|
2000-05-23 12:18:49 +00:00
|
|
|
.code16
|
1998-10-12 21:16:26 +00:00
|
|
|
|
|
|
|
start: jmp main # Start recognizably
|
|
|
|
|
|
|
|
.org 0x4,0x90
|
2000-06-19 22:50:46 +00:00
|
|
|
#
|
|
|
|
# Trampoline used by boot2 to call read to read data from the disk via
|
|
|
|
# the BIOS. Call with:
|
|
|
|
#
|
|
|
|
# %cx:%ax - long - LBA to read in
|
|
|
|
# %es:(%bx) - caddr_t - buffer to read data into
|
|
|
|
# %dl - byte - drive to read from
|
|
|
|
# %dh - byte - num sectors to read
|
|
|
|
#
|
1998-11-08 15:36:35 +00:00
|
|
|
|
2000-05-23 12:18:49 +00:00
|
|
|
xread: push %ss # Address
|
|
|
|
pop %ds # data
|
2000-06-19 22:50:46 +00:00
|
|
|
#
|
|
|
|
# Setup an EDD disk packet and pass it to read
|
|
|
|
#
|
2000-05-23 12:18:49 +00:00
|
|
|
xread.1: # Starting
|
|
|
|
pushl $0x0 # absolute
|
|
|
|
push %cx # block
|
|
|
|
push %ax # number
|
|
|
|
push %es # Address of
|
|
|
|
push %bx # transfer buffer
|
|
|
|
xor %ax,%ax # Number of
|
1999-07-20 01:19:23 +00:00
|
|
|
movb %dh,%al # blocks to
|
2000-05-23 12:18:49 +00:00
|
|
|
push %ax # transfer
|
|
|
|
push $0x10 # Size of packet
|
|
|
|
mov %sp,%bp # Packet pointer
|
|
|
|
callw read # Read from disk
|
|
|
|
lea 0x10(%bp),%sp # Clear stack
|
1998-11-08 15:36:35 +00:00
|
|
|
lret # To far caller
|
2000-06-19 22:50:46 +00:00
|
|
|
#
|
|
|
|
# Load the rest of boot2 and BTX up, copy the parts to the right locations,
|
|
|
|
# and start it all up.
|
|
|
|
#
|
1998-11-08 15:36:35 +00:00
|
|
|
|
2000-06-19 22:50:46 +00:00
|
|
|
#
|
|
|
|
# Setup the segment registers to flat addressing (segment 0) and setup the
|
|
|
|
# stack to end just below the start of our code.
|
|
|
|
#
|
1998-10-12 21:16:26 +00:00
|
|
|
main: cld # String ops inc
|
2000-05-23 12:18:49 +00:00
|
|
|
xor %cx,%cx # Zero
|
|
|
|
mov %cx,%es # Address
|
|
|
|
mov %cx,%ds # data
|
|
|
|
mov %cx,%ss # Set up
|
|
|
|
mov $start,%sp # stack
|
2000-06-19 22:50:46 +00:00
|
|
|
#
|
|
|
|
# Relocate ourself to MEM_REL. Since %cx == 0, the inc %ch sets
|
|
|
|
# %cx == 0x100.
|
|
|
|
#
|
2000-05-23 12:18:49 +00:00
|
|
|
mov %sp,%si # Source
|
|
|
|
mov $MEM_REL,%di # Destination
|
1998-11-08 15:36:35 +00:00
|
|
|
incb %ch # Word count
|
1998-10-12 21:16:26 +00:00
|
|
|
rep # Copy
|
2000-05-23 12:18:49 +00:00
|
|
|
movsw # code
|
2000-06-19 22:50:46 +00:00
|
|
|
#
|
|
|
|
# If we are on a hard drive, then load the MBR and look for the first
|
|
|
|
# FreeBSD slice. We use the fake partition entry below that points to
|
|
|
|
# the MBR when we call nread. The first pass looks for the first active
|
|
|
|
# FreeBSD slice. The second pass looks for the first non-active FreeBSD
|
|
|
|
# slice if the first one fails.
|
|
|
|
#
|
2000-05-23 12:18:49 +00:00
|
|
|
mov $part4,%si # Partition
|
1998-10-12 21:16:26 +00:00
|
|
|
cmpb $0x80,%dl # Hard drive?
|
|
|
|
jb main.4 # No
|
1998-11-08 15:36:35 +00:00
|
|
|
movb $0x1,%dh # Block count
|
2000-05-23 12:18:49 +00:00
|
|
|
callw nread # Read MBR
|
|
|
|
mov $0x1,%cx # Two passes
|
|
|
|
main.1: mov $MEM_BUF+PRT_OFF,%si # Partition table
|
1998-10-12 21:16:26 +00:00
|
|
|
movb $0x1,%dh # Partition
|
2000-05-23 12:18:49 +00:00
|
|
|
main.2: cmpb $PRT_BSD,0x4(%si) # Our partition type?
|
1998-10-12 21:16:26 +00:00
|
|
|
jne main.3 # No
|
2000-05-23 12:18:49 +00:00
|
|
|
jcxz main.5 # If second pass
|
|
|
|
testb $0x80,(%si) # Active?
|
1998-10-12 21:16:26 +00:00
|
|
|
jnz main.5 # Yes
|
2000-05-23 12:18:49 +00:00
|
|
|
main.3: add $0x10,%si # Next entry
|
1998-10-12 21:16:26 +00:00
|
|
|
incb %dh # Partition
|
1998-11-08 15:36:35 +00:00
|
|
|
cmpb $0x1+PRT_NUM,%dh # In table?
|
|
|
|
jb main.2 # Yes
|
2000-05-23 12:18:49 +00:00
|
|
|
dec %cx # Do two
|
|
|
|
jcxz main.1 # passes
|
2000-06-19 22:50:46 +00:00
|
|
|
#
|
|
|
|
# If we get here, we didn't find any FreeBSD slices at all, so print an
|
|
|
|
# error message and die.
|
|
|
|
#
|
2000-05-23 12:18:49 +00:00
|
|
|
mov $msg_part,%si # Message
|
1998-10-12 21:16:26 +00:00
|
|
|
jmp error # Error
|
2000-06-19 22:50:46 +00:00
|
|
|
#
|
|
|
|
# Floppies use partition 0 of drive 0.
|
|
|
|
#
|
2000-05-23 12:18:49 +00:00
|
|
|
main.4: xor %dx,%dx # Partition:drive
|
2000-06-19 22:50:46 +00:00
|
|
|
#
|
|
|
|
# Ok, we have a slice and drive in %dx now, so use that to locate and load
|
|
|
|
# boot2. %si references the start of the slice we are looking for, so go
|
|
|
|
# ahead and load up the first 16 sectors (boot1 + boot2) from that. When
|
|
|
|
# we read it in, we conveniently use 0x8c00 as our transfer buffer. Thus,
|
|
|
|
# boot1 ends up at 0x8c00, and boot2 starts at 0x8c00 + 0x200 = 0x8e00.
|
2000-07-06 00:29:40 +00:00
|
|
|
# The first part of boot2 is the disklabel, which is 0x200 bytes long.
|
2000-06-19 22:50:46 +00:00
|
|
|
# The second part is BTX, which is thus loaded into 0x9000, which is where
|
|
|
|
# it also runs from. The boot2.bin binary starts right after the end of
|
|
|
|
# BTX, so we have to figure out where the start of it is and then move the
|
|
|
|
# binary to 0xb000. Normally, BTX clients start at MEM_USR, or 0xa000, but
|
|
|
|
# when we use btxld create boot2, we use an entry point of 0x1000. That
|
2000-07-06 00:22:50 +00:00
|
|
|
# entry point is relative to MEM_USR; thus boot2.bin starts at 0xb000.
|
2000-06-19 22:50:46 +00:00
|
|
|
#
|
2000-05-23 12:18:49 +00:00
|
|
|
main.5: mov %dx,MEM_ARG # Save args
|
1998-11-08 15:36:35 +00:00
|
|
|
movb $0x10,%dh # Sector count
|
2000-05-23 12:18:49 +00:00
|
|
|
callw nread # Read disk
|
|
|
|
mov $MEM_BTX,%bx # BTX
|
2000-06-19 22:50:46 +00:00
|
|
|
mov 0xa(%bx),%si # Get BTX length and set
|
|
|
|
add %bx,%si # %si to start of boot2.bin
|
2000-05-23 12:18:49 +00:00
|
|
|
mov $MEM_USR+SIZ_PAG,%di # Client page 1
|
|
|
|
mov $MEM_BTX+0xe*SIZ_SEC,%cx # Byte
|
|
|
|
sub %si,%cx # count
|
1998-10-12 21:16:26 +00:00
|
|
|
rep # Relocate
|
|
|
|
movsb # client
|
2000-05-23 12:18:49 +00:00
|
|
|
sub %di,%cx # Byte count
|
2000-06-19 22:50:46 +00:00
|
|
|
xorb %al,%al # Zero assumed bss from
|
|
|
|
rep # the end of boot2.bin
|
|
|
|
stosb # up to 0x10000
|
2000-05-23 12:18:49 +00:00
|
|
|
callw seta20 # Enable A20
|
|
|
|
jmp start+MEM_JMP-MEM_ORG # Start BTX
|
2000-06-19 22:50:46 +00:00
|
|
|
#
|
|
|
|
# Enable A20 so we can access memory above 1 meg.
|
|
|
|
#
|
1998-10-27 20:19:24 +00:00
|
|
|
seta20: cli # Disable interrupts
|
|
|
|
seta20.1: inb $0x64,%al # Get status
|
1998-10-12 21:16:26 +00:00
|
|
|
testb $0x2,%al # Busy?
|
1998-10-27 20:19:24 +00:00
|
|
|
jnz seta20.1 # Yes
|
1998-10-12 21:16:26 +00:00
|
|
|
movb $0xd1,%al # Command: Write
|
|
|
|
outb %al,$0x64 # output port
|
1998-10-27 20:19:24 +00:00
|
|
|
seta20.2: inb $0x64,%al # Get status
|
1998-10-12 21:16:26 +00:00
|
|
|
testb $0x2,%al # Busy?
|
1998-10-27 20:19:24 +00:00
|
|
|
jnz seta20.2 # Yes
|
1998-10-12 21:16:26 +00:00
|
|
|
movb $0xdf,%al # Enable
|
|
|
|
outb %al,$0x60 # A20
|
1998-10-27 20:19:24 +00:00
|
|
|
sti # Enable interrupts
|
2000-05-23 12:18:49 +00:00
|
|
|
retw # To caller
|
2000-06-19 22:50:46 +00:00
|
|
|
#
|
|
|
|
# Trampoline used to call read from within boot1.
|
|
|
|
#
|
2000-05-23 12:18:49 +00:00
|
|
|
nread: mov $MEM_BUF,%bx # Transfer buffer
|
|
|
|
mov 0x8(%si),%ax # Get
|
|
|
|
mov 0xa(%si),%cx # LBA
|
|
|
|
push %cs # Read from
|
|
|
|
callw xread.1 # disk
|
2000-06-19 22:50:46 +00:00
|
|
|
jnc return # If success, return
|
|
|
|
mov $msg_read,%si # Otherwise, set the error
|
|
|
|
# message and fall through to
|
|
|
|
# the error routine
|
|
|
|
#
|
|
|
|
# Print out the error message pointed to by %ds:(%si) followed
|
|
|
|
# by a prompt, wait for a keypress, and then reboot the machine.
|
|
|
|
#
|
2000-05-23 12:18:49 +00:00
|
|
|
error: callw putstr # Display message
|
|
|
|
mov $prompt,%si # Display
|
|
|
|
callw putstr # prompt
|
1998-10-12 21:16:26 +00:00
|
|
|
xorb %ah,%ah # BIOS: Get
|
|
|
|
int $0x16 # keypress
|
2000-06-26 22:57:16 +00:00
|
|
|
movw $0x1234, BDA_BOOT # Do a warm boot
|
|
|
|
ljmp $0xffff,$0x0 # reboot the machine
|
2000-06-19 22:50:46 +00:00
|
|
|
#
|
|
|
|
# Display a null-terminated string using the BIOS output.
|
|
|
|
#
|
2000-05-23 12:18:49 +00:00
|
|
|
putstr.0: mov $0x7,%bx # Page:attribute
|
1998-10-12 21:16:26 +00:00
|
|
|
movb $0xe,%ah # BIOS: Display
|
|
|
|
int $0x10 # character
|
|
|
|
putstr: lodsb # Get char
|
|
|
|
testb %al,%al # End of string?
|
|
|
|
jne putstr.0 # No
|
|
|
|
|
2000-06-19 22:50:46 +00:00
|
|
|
#
|
|
|
|
# Overused return code. ereturn is used to return an error from the
|
|
|
|
# read function. Since we assume putstr succeeds, we (ab)use the
|
|
|
|
# same code when we return from putstr.
|
|
|
|
#
|
1998-11-08 15:36:35 +00:00
|
|
|
ereturn: movb $0x1,%ah # Invalid
|
|
|
|
stc # argument
|
2000-05-23 12:18:49 +00:00
|
|
|
return: retw # To caller
|
2000-06-19 22:50:46 +00:00
|
|
|
#
|
|
|
|
# Reads sectors from the disk. If EDD is enabled, then check if it is
|
|
|
|
# installed and use it if it is. If it is not installed or not enabled, then
|
|
|
|
# fall back to using CHS. Since we use a LBA, if we are using CHS, we have to
|
|
|
|
# fetch the drive parameters from the BIOS and divide it out ourselves.
|
|
|
|
# Call with:
|
|
|
|
#
|
|
|
|
# %dl - byte - drive number
|
|
|
|
# stack - 10 bytes - EDD Packet
|
2000-06-26 22:57:16 +00:00
|
|
|
#
|
|
|
|
read: push %dx # Save
|
1998-10-12 21:16:26 +00:00
|
|
|
movb $0x8,%ah # BIOS: Get drive
|
|
|
|
int $0x13 # parameters
|
|
|
|
movb %dh,%ch # Max head number
|
2000-05-23 12:18:49 +00:00
|
|
|
pop %dx # Restore
|
1998-11-08 15:36:35 +00:00
|
|
|
jc return # If error
|
1998-10-12 21:16:26 +00:00
|
|
|
andb $0x3f,%cl # Sectors per track
|
1998-11-08 15:36:35 +00:00
|
|
|
jz ereturn # If zero
|
|
|
|
cli # Disable interrupts
|
2000-05-23 12:18:49 +00:00
|
|
|
mov 0x8(%bp),%eax # Get LBA
|
|
|
|
push %dx # Save
|
|
|
|
movzbl %cl,%ebx # Divide by
|
|
|
|
xor %edx,%edx # sectors
|
|
|
|
div %ebx # per track
|
1998-10-12 21:16:26 +00:00
|
|
|
movb %ch,%bl # Max head number
|
|
|
|
movb %dl,%ch # Sector number
|
2000-05-23 12:18:49 +00:00
|
|
|
inc %bx # Divide by
|
1998-11-08 15:36:35 +00:00
|
|
|
xorb %dl,%dl # number
|
2000-05-23 12:18:49 +00:00
|
|
|
div %ebx # of heads
|
1998-10-12 21:16:26 +00:00
|
|
|
movb %dl,%bh # Head number
|
2000-05-23 12:18:49 +00:00
|
|
|
pop %dx # Restore
|
|
|
|
cmpl $0x3ff,%eax # Cylinder number supportable?
|
1998-11-08 15:36:35 +00:00
|
|
|
sti # Enable interrupts
|
2000-06-26 22:57:16 +00:00
|
|
|
ja read.7 # No, try EDD
|
1998-10-12 21:16:26 +00:00
|
|
|
xchgb %al,%ah # Set up cylinder
|
|
|
|
rorb $0x2,%al # number
|
|
|
|
orb %ch,%al # Merge
|
2000-05-23 12:18:49 +00:00
|
|
|
inc %ax # sector
|
|
|
|
xchg %ax,%cx # number
|
1998-10-12 21:16:26 +00:00
|
|
|
movb %bh,%dh # Head number
|
|
|
|
subb %ah,%al # Sectors this track
|
2000-05-23 12:18:49 +00:00
|
|
|
mov 0x2(%bp),%ah # Blocks to read
|
1998-10-12 21:16:26 +00:00
|
|
|
cmpb %ah,%al # To read
|
1999-07-20 01:19:23 +00:00
|
|
|
jb read.2 # this
|
1998-10-12 21:16:26 +00:00
|
|
|
movb %ah,%al # track
|
2000-05-23 12:18:49 +00:00
|
|
|
read.2: mov $0x5,%di # Try count
|
|
|
|
read.3: les 0x4(%bp),%bx # Transfer buffer
|
|
|
|
push %ax # Save
|
1998-11-08 15:36:35 +00:00
|
|
|
movb $0x2,%ah # BIOS: Read
|
|
|
|
int $0x13 # from disk
|
2000-05-23 12:18:49 +00:00
|
|
|
pop %bx # Restore
|
1999-07-20 01:19:23 +00:00
|
|
|
jnc read.4 # If success
|
2000-05-23 12:18:49 +00:00
|
|
|
dec %di # Retry?
|
1999-07-20 01:19:23 +00:00
|
|
|
jz read.6 # No
|
1998-10-12 21:16:26 +00:00
|
|
|
xorb %ah,%ah # BIOS: Reset
|
|
|
|
int $0x13 # disk system
|
2000-05-23 12:18:49 +00:00
|
|
|
xchg %bx,%ax # Block count
|
1999-07-20 01:19:23 +00:00
|
|
|
jmp read.3 # Continue
|
2000-05-23 12:18:49 +00:00
|
|
|
read.4: movzbw %bl,%ax # Sectors read
|
|
|
|
add %ax,0x8(%bp) # Adjust
|
1999-07-20 01:19:23 +00:00
|
|
|
jnc read.5 # LBA,
|
2000-05-23 12:18:49 +00:00
|
|
|
incw 0xa(%bp) # transfer
|
1999-07-20 01:19:23 +00:00
|
|
|
read.5: shlb %bl # buffer
|
2000-05-23 12:18:49 +00:00
|
|
|
add %bl,0x5(%bp) # pointer,
|
|
|
|
sub %al,0x2(%bp) # block count
|
2000-06-26 22:57:16 +00:00
|
|
|
ja read # If not done
|
2000-05-23 12:18:49 +00:00
|
|
|
read.6: retw # To caller
|
2000-06-26 22:57:16 +00:00
|
|
|
read.7: testb $FL_PACKET,%cs:MEM_REL+flags-start # LBA support enabled?
|
|
|
|
jz ereturn # No, so return an error
|
|
|
|
mov $0x55aa,%bx # Magic
|
|
|
|
push %dx # Save
|
|
|
|
movb $0x41,%ah # BIOS: Check
|
|
|
|
int $0x13 # extensions present
|
|
|
|
pop %dx # Restore
|
|
|
|
jc return # If error, return an error
|
|
|
|
cmp $0xaa55,%bx # Magic?
|
|
|
|
jne ereturn # No, so return an error
|
|
|
|
testb $0x1,%cl # Packet interface?
|
|
|
|
jz ereturn # No, so return an error
|
|
|
|
mov %bp,%si # Disk packet
|
|
|
|
movb $0x42,%ah # BIOS: Extended
|
|
|
|
int $0x13 # read
|
|
|
|
retw # To caller
|
1998-10-12 21:16:26 +00:00
|
|
|
|
|
|
|
# Messages
|
|
|
|
|
1998-11-08 15:36:35 +00:00
|
|
|
msg_read: .asciz "Read"
|
|
|
|
msg_part: .asciz "Boot"
|
1998-10-12 21:16:26 +00:00
|
|
|
|
1998-11-11 08:56:17 +00:00
|
|
|
prompt: .asciz " error\r\n"
|
1998-11-05 20:52:25 +00:00
|
|
|
|
1999-07-20 01:19:23 +00:00
|
|
|
flags: .byte FLAGS # Flags
|
|
|
|
|
1998-10-12 21:16:26 +00:00
|
|
|
.org PRT_OFF,0x90
|
|
|
|
|
|
|
|
# Partition table
|
|
|
|
|
|
|
|
.fill 0x30,0x1,0x0
|
|
|
|
part4: .byte 0x80, 0x00, 0x01, 0x00
|
|
|
|
.byte 0xa5, 0xff, 0xff, 0xff
|
|
|
|
.byte 0x00, 0x00, 0x00, 0x00
|
2000-06-19 22:50:46 +00:00
|
|
|
.byte 0x50, 0xc3, 0x00, 0x00 # 50000 sectors long, bleh
|
1998-10-12 21:16:26 +00:00
|
|
|
|
|
|
|
.word 0xaa55 # Magic number
|