8000 minix/boot/boothead.s at v3.1.8 · blackskygg/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
1619 lines (1501 loc) · 40.6 KB

boothead.s

File metadata and controls

1619 lines (1501 loc) · 40.6 KB
! Boothead.s - BIOS support for boot.c Author: Kees J. Bot
!
!
! This file contains the startup and low level support for the secondary
! boot program. It contains functions for disk, tty and keyboard I/O,
! copying memory to arbitrary locations, etc.
!
! The primary bootstrap code supplies the following parameters in registers:
! dl = Boot-device.
! es:si = Partition table entry if hard disk.
!
.text
o32 = 0x66 ! This assembler doesn't know 386 extensions
BOOTOFF = 0x7C00 ! 0x0000:BOOTOFF load a bootstrap here
LOADSEG = 0x1000 ! Where this code is loaded.
BUFFER = 0x0600 ! First free memory
PENTRYSIZE = 16 ! Partition table entry size.
a_flags = 2 ! From a.out.h, struct exec
a_text = 8
a_data = 12
a_bss = 16
a_total = 24
A_SEP = 0x20 ! Separate I&D flag
K_I386 = 0x0001 ! Call Minix in 386 mode
K_RET = 0x0020 ! Returns to the monitor on reboot
K_INT86 = 0x0040 ! Requires generic INT support
K_MEML = 0x0080 ! Pass a list of free memory
DS_SELECTOR = 3*8 ! Kernel data selector
ES_SELECTOR = 4*8 ! Flat 4 Gb
SS_SELECTOR = 5*8 ! Monitor stack
CS_SELECTOR = 6*8 ! Kernel code
MCS_SELECTOR= 7*8 ! Monitor code
ESC = 0x1B ! Escape character
! Imported variables and functions:
.extern _caddr, _daddr, _runsize, _edata, _end ! Runtime environment
.extern _device ! BIOS device number
.extern _rem_part ! To pass partition info
.extern _k_flags ! Special kernel flags
.extern _mem ! Free memory list
.extern _emem ! Free memory list for E820
.extern _mem_entries ! Free memory E820 list entries
.extern _cdbooted ! Whether we booted from CD
.extern _cddevice ! Whether we booted from CD
.text
! Set segment registers and stack pointer using the programs own header!
! The header is either 32 bytes (short form) or 48 bytes (long form). The
! bootblock will jump to address 0x10030 in both cases, calling one of the
! two jmpf instructions below.
!
! CD bootblock jumps to address 0x10050 in both cases.
jmpf boot, LOADSEG+3 ! Set cs right (skipping long a.out header)
.space 11 ! jmpf + 11 = 16 bytes
jmpf boot, LOADSEG+2 ! Set cs right (skipping short a.out header)
.space 11 ! jmpf + 11 = 16 bytes
jmpf cdboot, LOADSEG+3
.space 11
jmpf cdboot, LOADSEG+2
.space 11
cdboot:
mov bx, #1
jmp commonboot
boot:
mov bx, #0
commonboot:
mov ax, #LOADSEG
mov ds, ax ! ds = header
movb al, a_flags
testb al, #A_SEP ! Separate I&D?
jnz sepID
comID: xor ax, ax
xchg ax, a_text ! No text
add a_data, ax ! Treat all text as data
sepID:
mov ax, a_total ! Total nontext memory usage
and ax, #0xFFFE ! Round down to even
mov a_total, ax ! total - text = data + bss + heap + stack
cli ! Ignore interrupts while stack in limbo
mov sp, ax ! Set sp at the top of all that
mov ax, a_text ! Determine offset of ds above cs
movb cl, #4
shr ax, cl
mov cx, cs
add ax, cx
mov ds, ax ! ds = cs + text / 16
mov ss, ax
sti ! Stack ok now
push es ! Save es, we need it for the partition table
mov es, ax
cld ! C compiler wants UP
! Clear bss
xor ax, ax ! Zero
mov di, #_edata ! Start of bss is at end of data
mov cx, #_end ! End of bss (begin of heap)
sub cx, di ! Number of bss bytes
shr cx, #1 ! Number of words
rep
stos ! Clear bss
! Copy primary boot parameters to variables. (Can do this now that bss is
! cleared and may be written into).
xorb dh, dh
mov _device, dx ! Boot device (probably 0x00 or 0x80)
mov _rem_part+0, si ! Remote partition table offset
pop _rem_part+2 ! and segment (saved es)
mov _cdbooted, bx ! Booted from CD? (bx set above)
! Remember the current video mode for restoration on exit.
movb ah, #0x0F ! Get current video mode
int 0x10
andb al, #0x7F ! Mask off bit 7 (no blanking)
movb old_vid_mode, al
movb cur_vid_mode, al
! Give C code access to the code segment, data segment and the size of this
! process.
xor ax, ax
mov dx, cs
call seg2abs
mov _caddr+0, ax
mov _caddr+2, dx
xor ax, ax
mov dx, ds
call seg2abs
mov _daddr+0, ax
mov _daddr+2, dx
push ds
mov ax, #LOADSEG
mov ds, ax ! Back to the header once more
mov ax, a_total+0
mov dx, a_total+2 ! dx:ax = data + bss + heap + stack
add ax, a_text+0
adc dx, a_text+2 ! dx:ax = text + data + bss + heap + stack
pop ds
mov _runsize+0, ax
mov _runsize+2, dx ! 32 bit size of this process
!Determine memory using the 0xE820 BIOS function if available
mov di, #_emem
mov 20(di), #1 ! force a valid ACPI 3.X entry
.data1 o32
xor bx, bx ! zero EBX
xor bp, bp !zero bp
.data1 o32
mov dx, e820_magic
.data1 o32
mov cx, const_24 ! request 24 bytes
.data1 o32
mov ax, const_0xe820
int 0x15
jc e820_failed
.data1 o32
mov dx, e820_magic
.data1 o32
cmp dx, ax
jne e820_failed
.data1 o32
test bx, bx
je e820_failed
jmp e820_gotit
e820_next:
.data1 o32
mov ax, const_0xe820
mov 20(di), #1 ! force a valid ACPI 3.X entry
.data1 o32
mov cx, const_24 ! request 24 bytes
int 0x15
jc e820_done
.data1 o32
mov dx, e820_magic
e820_gotit:
jcxz e820_skip
cmp bp, #16
je e820_done ! we have only space for 16 entries
inc bp
add di, #24
e820_skip:
.data1 o32
test bx, bx
jne e820_next
e820_done:
mov _mem_entries, bp
jmp memory_detected
e820_failed:
! Determine available memory as a list of (base,size) pairs as follows:
! mem[0] = low memory, mem[1] = memory between 1M and 16M, mem[2] = memory
! above 16M. Last two coalesced into mem[1] if adjacent.
mov di, #_mem ! di = memory list
int 0x12 ! Returns low memory size (in K) in ax
mul c1024
mov 4(di), ax ! mem[0].size = low memory size in bytes
mov 6(di), dx
call _getprocessor
cmp ax, #286 ! Only 286s and above have extended memory
jb no_ext
cmp ax, #486 ! Assume 486s were the first to have >64M
jb small_ext ! (It helps to be paranoid when using the BIOS)
big_ext:
mov ax, #0xE801 ! Code for get memory size for >64M
int 0x15 ! ax = mem at 1M per 1K, bx = mem at 16M per 64K
jnc got_ext
small_ext:
movb ah, #0x88 ! Code for get extended memory size
clc ! Carry will stay clear if call exists
int 0x15 ! Returns size (in K) in ax for AT's
jc no_ext
test ax, ax ! An AT with no extended memory?
jz no_ext
xor bx, bx ! bx = mem above 16M per 64K = 0
got_ext:
mov cx, ax ! cx = copy of ext mem at 1M
mov 10(di), #0x0010 ! mem[1].base = 0x00100000 (1M)
mul c1024
mov 12(di), ax ! mem[1].size = "ext mem at 1M" * 1024
mov 14(di), dx
test bx, bx
jz no_ext ! No more ext mem above 16M?
cmp cx, #15*1024 ! Chunks adjacent? (precisely 15M at 1M?)
je adj_ext
mov 18(di), #0x0100 ! mem[2].base = 0x01000000 (16M)
mov 22(di), bx ! mem[2].size = "ext mem at 16M" * 64K
jmp no_ext
adj_ext:
add 14(di), bx ! Add ext mem above 16M to mem below 16M
no_ext:
memory_detected:
! Time to switch to a higher level language (not much higher)
call _boot
! void ..exit(int status)
! Exit the monitor by rebooting the system.
.define _exit, __exit, ___exit ! Make various compilers happy
_exit:
__exit:
___exit:
mov bx, sp
cmp 2(bx), #0 ! Good exit status?
jz reboot
quit: mov ax, #any_key
push ax
call _printf
xorb ah, ah ! Read character from keyboard
int 0x16
reboot: call dev_reset
call restore_video
int 0x19 ! Reboot the system
.data
any_key:
.ascii "\nHit any key to reboot\n\0"
.text
! u32_t mon2abs(void *ptr)
! Address in monitor data to absolute address.
.define _mon2abs
_mon2abs:
mov bx, sp
mov ax, 2(bx) ! ptr
mov dx, ds ! Monitor data segment
jmp seg2abs
! u32_t vec2abs(vector *vec)
! 8086 interrupt vector to absolute address.
.define _vec2abs
_vec2abs:
mov bx, sp
mov bx, 2(bx)
mov ax, (bx)
mov dx, 2(bx) ! dx:ax vector
!jmp seg2abs ! Translate
seg2abs: ! Translate dx:ax to the 32 bit address dx-ax
push cx
movb ch, dh
movb cl, #4
shl dx, cl
shrb ch, cl ! ch-dx = dx << 4
add ax, dx
adcb ch, #0 ! ch-ax = ch-dx + ax
movb dl, ch
xorb dh, dh ! dx-ax = ch-ax
pop cx
ret
abs2seg: ! Translate the 32 bit address dx-ax to dx:ax
push cx
movb ch, dl
mov dx, ax ! ch-dx = dx-ax
and ax, #0x000F ! Offset in ax
movb cl, #4
shr dx, cl
shlb ch, cl
orb dh, ch ! dx = ch-dx >> 4
pop cx
ret
! void raw_copy(u32_t dstaddr, u32_t srcaddr, u32_t count)
! Copy count bytes from srcaddr to dstaddr. Don't do overlaps.
! Also handles copying words to or from extended memory.
.define _raw_copy
_raw_copy:
push bp
mov bp, sp
push si
push di ! Save C variable registers
copy:
cmp 14(bp), #0
jnz bigcopy
mov cx, 12(bp)
jcxz copydone ! Count is zero, end copy
cmp cx, #0xFFF0
jb smallcopy
bigcopy:mov cx, #0xFFF0 ! Don't copy more than about 64K at once
smallcopy:
push cx ! Save copying count
mov ax, 4(bp)
mov dx, 6(bp)
cmp dx, #0x0010 ! Copy to extended memory?
jae ext_copy
cmp 10(bp), #0x0010 ! Copy from extended memory?
jae ext_copy
call abs2seg
mov di, ax
mov es, dx ! es:di = dstaddr
mov ax, 8(bp)
mov dx, 10(bp)
call abs2seg
mov si, ax
mov ds, dx ! ds:si = srcaddr
shr cx, #1 ! Words to move
rep
movs ! Do the word copy
adc cx, cx ! One more byte?
rep
movsb ! Do the byte copy
mov ax, ss ! Restore ds and es from the remaining ss
mov ds, ax
mov es, ax
jmp copyadjust
ext_copy:
mov x_dst_desc+2, ax
movb x_dst_desc+4, dl ! Set base of destination segment
movb x_dst_desc+7, dh
mov ax, 8(bp)
mov dx, 10(bp)
mov x_src_desc+2, ax
movb x_src_desc+4, dl ! Set base of source segment
movb x_src_desc+7, dh
mov si, #x_gdt ! es:si = global descriptor table
shr cx, #1 ! Words to move
movb ah, #0x87 ! Code for extended memory move
int 0x15
copyadjust:
pop cx ! Restore count
add 4(bp), cx
adc 6(bp), #0 ! srcaddr += copycount
add 8(bp), cx
adc 10(bp), #0 ! dstaddr += copycount
sub 12(bp), cx
sbb 14(bp), #0 ! count -= copycount
jmp copy ! and repeat
copydone:
pop di
pop si ! Restore C variable registers
pop bp
ret
! u16_t get_word(u32_t addr);
! void put_word(u32_t addr, u16_t word);
! Read or write a 16 bits word at an arbitrary location.
.define _get_word, _put_word
_get_word:
mov bx, sp
call gp_getaddr
mov ax, (bx) ! Word to get from addr
jmp gp_ret
_put_word:
mov bx, sp
push 6(bx) ! Word to store at addr
call gp_getaddr
pop (bx) ! Store the word
jmp gp_ret
gp_getaddr:
mov ax, 2(bx)
mov dx, 4(bx)
call abs2seg
mov bx, ax
mov ds, dx ! ds:bx = addr
ret
gp_ret:
push es
pop ds ! Restore ds
ret
! void relocate(void);
! After the program has copied itself to a safer place, it needs to change
! the segment registers. Caddr has already been set to the new location.
.define _relocate
_relocate:
pop bx ! Return address
mov ax, _caddr+0
mov dx, _caddr+2
call abs2seg
mov cx, dx ! cx = new code segment
mov ax, cs ! Old code segment
sub ax, cx ! ax = -(new - old) = -Moving offset
mov dx, ds
sub dx, ax
mov ds, dx ! ds += (new - old)
mov es, dx
mov ss, dx
xor ax, ax
call seg2abs
mov _daddr+0, ax
mov _daddr+2, dx ! New data address
push cx ! New text segment
push bx ! Return offset of this function
retf ! Relocate
! void *brk(void *addr)
! void *sbrk(size_t incr)
! Cannot fail implementations of brk(2) and sbrk(3), so we can use
! malloc(3). They reboot on stack collision instead of returning -1.
.data
.align 2
break: .data2 _end ! A fake heap pointer
.text
.define _brk, __brk, _sbrk, __sbrk
_brk:
__brk: ! __brk is for the standard C compiler
xor ax, ax
jmp sbrk ! break= 0; return sbrk(addr);
_sbrk:
__sbrk:
mov ax, break ! ax= current break
sbrk: push ax ! save it as future return value
mov bx, sp ! Stack is now: (retval, retaddr, incr, ...)
add ax, 4(bx) ! ax= break + increment
mov break, ax ! Set new break
lea dx, -1024(bx) ! sp minus a bit of breathing space
cmp dx, ax ! Compare with the new break
jb heaperr ! Suffocating noises
lea dx, -4096(bx) ! A warning when heap+stack goes < 4K
cmp dx, ax
jae plenty ! No reason to complain
mov ax, #memwarn
push ax
call _printf ! Warn about memory running low
pop ax
movb memwarn, #0 ! No more warnings
plenty: pop ax ! Return old break (0 for brk)
ret
heaperr:mov ax, #chmem
push ax
mov ax, #nomem
push ax
call _printf
jmp quit
.data
nomem: .ascii "\nOut of%s\0"
memwarn:.ascii "\nLow on"
chmem: .ascii " memory, use chmem to increase the heap\n\0"
.text
! int dev_open(void);
! Given the device "_device" figure out if it exists and what its number
! of heads and sectors may be. Return the BIOS error code on error,
! otherwise 0.
.define _dev_open
_dev_open:
call dev_reset ! Optionally reset the disks
movb dev_state, #0 ! State is "closed"
push es
push di ! Save registers used by BIOS calls
movb dl, _device ! The default device
cmpb dl, _cddevice
je cdopen
cmpb dl, #0x80 ! Floppy < 0x80, winchester >= 0x80
jae winchester
floppy:
mov di, #3 ! Three tries to init drive by reading sector 0
finit0: xor ax, ax
mov es, ax
mov bx, #BUFFER ! es:bx = scratch buffer
mov ax, #0x0201 ! Read sector, #sectors = 1
mov cx, #0x0001 ! Track 0, first sector
xorb dh, dh ! Drive dl, head 0
int 0x13
jnc finit0ok ! Sector 0 read ok?
cmpb ah, #0x80 ! Disk timed out? (Floppy drive empty)
je geoerr
dec di
jz geoerr
xorb ah, ah ! Reset drive
int 0x13
jc geoerr
jmp finit0 ! Retry once more, it may need to spin up
finit0ok:
mov di, #seclist ! List of per floppy type sectors/track
flast: movb cl, (di) ! Sectors per track to test
cmpb cl, #9 ! No need to do the last 720K/360K test
je ftestok
xor ax, ax
mov es, ax
mov bx, #BUFFER ! es:bx = scratch buffer
mov ax, #0x0201 ! Read sector, #sectors = 1
xorb ch, ch ! Track 0, last sector
xorb dh, dh ! Drive dl, head 0
int 0x13
jnc ftestok ! Sector cl read ok?
xorb ah, ah ! Reset drive
int 0x13
jc geoerr
inc di ! Try next sec/track number
jmp flast
ftestok:
movb dh, #2 ! Floppies have two sides
jmp geoboth
winchester:
movb ah, #0x08 ! Code for drive parameters
int 0x13 ! dl still contains drive
jc geoerr ! No such drive?
andb cl, #0x3F ! cl = max sector number (1-origin)
incb dh ! dh = 1 + max head number (0-origin)
jmp geoboth
cdopen:
movb cl, #0x3F ! Think up geometry for CD's
movb dh, #0x2
geoboth:
movb sectors, cl ! Sectors per track
movb al, cl ! al = sectors per track
mulb dh ! ax = heads * sectors
mov secspcyl, ax ! Sectors per cylinder = heads * sectors
movb dev_state, #1 ! Device state is "open"
xor ax, ax ! Code for success
geodone:
pop di
pop es ! Restore di and es registers
ret
geoerr: movb al, ah
xorb ah, ah ! ax = BIOS error code
jmp geodone
.data
seclist:
.data1 18, 15, 9 ! 1.44M, 1.2M, and 360K/720K floppy sec/track
.text
! int dev_close(void);
! Close the current device. Under the BIOS this does nothing much.
.define _dev_close
_dev_close:
xor ax, ax
movb dev_state, al ! State is "closed"
ret
! Reset the disks if needed. Minix may have messed things up.
dev_reset:
cmpb dev_state, #0 ! Need reset if dev_state < 0
jge 0f
xorb ah, ah ! Reset (ah = 0)
movb dl, #0x80 ! All disks
int 0x13
movb dev_state, #0 ! State is "closed"
0: ret
! int dev_boundary(u32_t sector);
! True if a sector is on a boundary, i.e. sector % sectors == 0.
.define _dev_boundary
_dev_boundary:
mov bx, sp
xor dx, dx
mov ax, 4(bx) ! divide high half of sector number
div sectors
mov ax, 2(bx) ! divide low half of sector number
div sectors ! dx = sector % sectors
sub dx, #1 ! CF = dx == 0
sbb ax, ax ! ax = -CF
neg ax ! ax = (sector % sectors) == 0
ret
! int biosreadsectors(u32_t bufaddr, u32_t sector, u8_t count)
! int writesectors(u32_t bufaddr, u32_t sector, u8_t count)
! Read/write several sectors from/to disk or floppy. The buffer must
! be between 64K boundaries! Count must fit in a byte. The external
! variables _device, sectors and secspcyl describe the disk and its
! geometry. Returns 0 for success, otherwise the BIOS error code.
!
.define _biosreadsectors, _writesectors
_writesectors:
push bp
mov bp, sp
movb 13(bp), #0x03 ! Code for a disk write
jmp rwsec
_biosreadsectors:
push bp
mov bp, sp
movb 13(bp), #0x02 ! Code for a disk read
rwsec: push si
push di
push es
cmpb dev_state, #0 ! Device state?
jg 0f ! >0 if open
call _dev_open ! Initialize
test ax, ax
jnz badopen
0: mov ax, 4(bp)
mov dx, 6(bp)
call abs2seg
mov bx, ax
mov es, dx ! es:bx = bufaddr
mov di, #3 ! Execute 3 resets on floppy error
cmpb _device, #0x80
jb nohd
mov di, #1 ! But only 1 reset on hard disk error
nohd: cmpb 12(bp), #0 ! count equals zero?
jz done
more: mov ax, 8(bp)
mov dx, 10(bp) ! dx:ax = abs sector. Divide it by sectors/cyl
cmp dx, #[1024*255*63-255]>>16 ! Near 8G limit?
jae bigdisk
mov si, _device
cmp si, _cddevice ! Is it a CD?
je bigdisk ! CD's need extended read.
div secspcyl ! ax = cylinder, dx = sector within cylinder
xchg ax, dx ! ax = sector within cylinder, dx = cylinder
movb ch, dl ! ch = low 8 bits of cylinder
divb sectors ! al = head, ah = sector (0-origin)
xorb dl, dl ! About to shift bits 8-9 of cylinder into dl
shr dx, #1
shr dx, #1 ! dl[6..7] = high cylinder
orb dl, ah ! dl[0..5] = sector (0-origin)
movb cl, dl ! cl[0..5] = sector, cl[6..7] = high cyl
incb cl ! cl[0..5] = sector (1-origin)
movb dh, al ! dh = head
movb dl, _device ! dl = device to use
movb al, sectors ! Sectors per track - Sector number (0-origin)
subb al, ah ! = Sectors left on this track
cmpb al, 12(bp) ! Compare with # sectors to transfer
jbe doit ! Can't go past the end of a cylinder?
movb al, 12(bp) ! 12(bp) < sectors left on this track
doit: movb ah, 13(bp) ! Code for disk read (0x02) or write (0x03)
push ax ! Save al = sectors to read
int 0x13 ! call the BIOS to do the transfer
pop cx ! Restore al in cl
jmp rdeval
bigdisk:
mov si, #ext_rw ! si = extended read/write parameter packet
movb cl, 12(bp)
movb 2(si), cl ! Fill in # blocks to transfer
mov 4(si), bx ! Buffer address = es:bx
mov 6(si), es
mov 8(si), ax ! Starting block number = dx:ax
mov 10(si), dx
movb dl, _device ! dl = device to use
mov ax, #0x4000 ! This, or-ed with 0x02 or 0x03 becomes
orb ah, 13(bp) ! extended read (0x4200) or write (0x4300)
int 0x13
!jmp rdeval
rdeval:
jc ioerr ! I/O error
movb al, cl ! Restore al = sectors read
addb bh, al ! bx += 2 * al * 256 (add bytes transferred)
addb bh, al ! es:bx = where next sector is located
add 8(bp), ax ! Update address by sectors transferred
adc 10(bp), #0 ! Don't forget high word
subb 12(bp), al ! Decrement sector count by sectors transferred
jnz more ! Not all sectors have been transferred
done: xorb ah, ah ! No error here!
jmp finish
ioerr: cmpb ah, #0x80 ! Disk timed out? (Floppy drive empty)
je finish
cmpb ah, #0x03 ! Disk write protected?
je finish
dec di ! Do we allow another reset?
jl finish ! No, report the error
xorb ah, ah ! Code for a reset (0)
int 0x13
jnc more ! Succesful reset, try request again
finish: movb al, ah
xorb ah, ah ! ax = error number
badopen:pop es
pop di
pop si
pop bp
ret
.data
.align 4
! Extended read/write commands require a parameter packet.
ext_rw:
.data1 0x10 ! Length of extended r/w packet
.data1 0 ! Reserved
.data2 0 ! Blocks to transfer (to be filled in)
.data2 0 ! Buffer address offset (tbfi)
.data2 0 ! Buffer address segment (tbfi)
.data4 0 ! Starting block number low 32 bits (tbfi)
.data4 0 ! Starting block number high 32 bits
.text
! int getch(void);
! Read a character from the keyboard, and check for an expired timer.
! A carriage return is changed into a linefeed for UNIX compatibility.
.define _getch
_getch:
xor ax, ax
xchg ax, unchar ! Ungotten character?
test ax, ax
jnz gotch
getch:
! hlt ! Play dead until interrupted (see pause())
movb ah, #0x01 ! Keyboard status
int 0x16
jz 0f ! Nothing typed
xorb ah, ah ! 10BC8 Read character from keyboard
int 0x16
jmp press ! Keypress
0: mov dx, line ! Serial line?
test dx, dx
jz 0f
add dx, #5 ! Line Status Register
inb dx
testb al, #0x01 ! Data Ready?
jz 0f
mov dx, line
!add dx, 0 ! Receive Buffer Register
inb dx ! Get character
jmp press
0: call _expired ! Timer expired?
test ax, ax
jz getch
mov ax, #ESC ! Return ESC
ret
press:
cmpb al, #0x0D ! Carriage return?
jnz nocr
movb al, #0x0A ! Change to linefeed
nocr: cmpb al, #ESC ! Escape typed?
jne noesc
inc escape ! Set flag
noesc: xorb ah, ah ! ax = al
gotch: ret
! int ungetch(void);
! Return a character to undo a getch().
.define _ungetch
_ungetch:
mov bx, sp
mov ax, 2(bx)
mov unchar, ax
ret
! int escape(void);
! True if ESC has been typed.
.define _escape
_escape:
movb ah, #0x01 ! Keyboard status
int 0x16
jz escflg ! Keypress?
cmpb al, #ESC ! Escape typed?
jne escflg
xorb ah, ah ! Discard the escape
int 0x16
inc escape ! Set flag
escflg: xor ax, ax
xchg ax, escape ! Escape typed flag
ret
! int putch(int c);
! Write a character in teletype mode. The putk synonym is
! for the kernel printf function that uses it.
! Newlines are automatically preceded by a carriage return.
!
.define _putch, _putk
_putch:
_putk: mov bx, sp
movb al, 2(bx) ! al = character to be printed
testb al, al ! Kernel printf adds a null char to flush queue
jz nulch
cmpb al, #0x0A ! al = newline?
jnz putc
movb al, #0x0D
call putc ! putc('\r')
movb al, #0x0A ! Restore the '\n' and print it
putc: movb ah, #0x0E ! Print character in teletype mode
mov bx, #0x0001 ! Page 0, foreground color
int 0x10
mov bx, line ! Serial line?
test bx, bx
jz nulch
push ax ! Save character to print
call _get_tick ! Current clock tick counter
mov cx, ax
add cx, #2 ! Don't want to see it count twice
1: lea dx, 5(bx) ! Line Status Register
inb dx
testb al, #0x20 ! Transmitter Holding Register Empty?
jnz 0f
call _get_tick
cmp ax, cx ! Clock ticked more than once?
jne 1b
0: pop ax ! Restore character to print
mov dx, bx ! Transmit Holding Register
outb dx ! Send character down the serial line
nulch: ret
! void pause(void);
! Wait for an interrupt using the HLT instruction. This either saves
! power, or tells an x86 emulator that nothing is happening right now.
.define _pause
_pause:
! hlt
ret
! void set_mode(unsigned mode);
! void clear_screen(void);
! Set video mode / clear the screen.
.define _set_mode, _clear_screen
_set_mode:
mov bx, sp
mov ax, 2(bx) ! Video mode
cmp ax, cur_vid_mode
je modeok ! Mode already as requested?
mov cur_vid_mode, ax
_clear_screen:
xor ax, ax
mov es, ax ! es = Vector segment
mov ax, cur_vid_mode
movb ch, ah ! Copy of the special flags
andb ah, #0x0F ! Test bits 8-11, clear special flags
jnz xvesa ! VESA extended mode?
int 0x10 ! Reset video (ah = 0)
jmp md_480
xvesa: mov bx, ax ! bx = extended mode
mov ax, #0x4F02 ! Reset video
int 0x10
md_480: ! Basic video mode is set, now build on it
testb ch, #0x20 ! 480 scan lines requested?
jz md_14pt
mov dx, #0x3CC ! Get CRTC port
inb dx
movb dl, #0xD4
testb al, #1 ! Mono or color?
jnz 0f
movb dl, #0xB4
0: mov ax, #0x110C ! Vertical sync end (also unlocks CR0-7)
call out2
mov ax, #0x060B ! Vertical total
call out2
mov ax, #0x073E ! (Vertical) overflow
call out2
mov ax, #0x10EA ! Vertical sync start
call out2
mov ax, #0x12DF ! Vertical display end
call out2
mov ax, #0x15E7 ! Vertical blank start
call out2
mov ax, #0x1604 ! Vertical blank end
call out2
push dx
movb dl, #0xCC ! Misc output register (read)
inb dx
movb dl, #0xC2 ! (write)
andb al, #0x0D ! Preserve clock select bits and color bit
orb al, #0xE2 ! Set correct sync polarity
outb dx
pop dx ! Index register still in dx
md_14pt:
testb ch, #0x40 ! 9x14 point font requested?
jz md_8pt
mov ax, #0x1111 ! Load ROM 9 by 14 font
xorb bl, bl ! Load block 0
int 0x10
testb ch, #0x20 ! 480 scan lines?
jz md_8pt
mov ax, #0x12DB ! VGA vertical display end
call out2
eseg movb 0x0484, #33 ! Tell BIOS the last line number
md_8pt:
testb ch, #0x80 ! 8x8 point font requested?
jz setcur
mov ax, #0x1112 ! Load ROM 8 by 8 font
xorb bl, bl ! Load block 0
int 0x10
testb ch, #0x20 ! 480 scan lines?
jz setcur
mov ax, #0x12DF ! VGA vertical display end
call out2
eseg movb 0x0484, #59 ! Tell BIOS the last line number
setcur:
xor dx, dx ! dl = column = 0, dh = row = 0
xorb bh, bh ! Page 0
movb ah, #0x02 ! Set cursor position
int 0x10
push ss
pop es ! Restore es
modeok: ret
! Out to the usual [index, data] port pair that are common for VGA devices
! dx = port, ah = index, al = data.
out2:
push dx
push ax
movb al, ah
outb dx ! Set index
inc dx
pop ax
outb dx ! Send data
pop dx
ret
restore_video: ! To restore the video mode on exit
mov ax, old_vid_mode
push ax
call _set_mode
pop ax
ret
! void serial_init(int line)
! Initialize copying console I/O to a serial line.
.define _serial_init
_serial_init:
mov bx, sp
mov dx, 2(bx) ! Line number
mov line, #0
test dx, dx ! Off if line number < 0
js 0f
push ds
xor ax, ax
mov ds, ax ! Vector and BIOS data segment
mov bx, dx ! Line number
shl bx, #1 ! Word offset
mov bx, 0x0400(bx) ! I/O port for the given line
pop ds
mov line, bx ! Remember I/O port
serial_init:
mov bx, line
test bx, bx ! I/O port must be nonzero
jz 0f
mov ax, #0x00E3 ! 9600 N-8-1
int 0x14 ! Initialize serial line dx
lea dx, 4(bx) ! Modem Control Register
movb al, #0x0B ! DTR, RTS, OUT2
outb dx
0: ret
! u32_t get_tick(void);
! Return the current value of the clock tick counter. This counter
! increments 18.2 times per second. Poll it to do delays. Does not
! work on the original PC, but works on the PC/XT.
.define _get_tick
_get_tick:
push cx
xorb ah, ah ! Code for get tick count
int 0x1A
mov ax, dx
mov dx, cx ! dx:ax = cx:dx = tick count
pop cx
ret
! Functions used to obtain info about the hardware. Boot uses this information
! itself, but will also pass them on to a pure 386 kernel, because one can't
! make BIOS calls from protected mode. The video type could probably be
! determined by the kernel too by looking at the hardware, but there is a small
! chance on errors that the monitor allows you to correct by setting variables.
.define _get_bus ! returns type of system bus
.define _get_video ! returns type of display
! u16_t get_bus(void)
! Return type of system bus, in order: XT, AT, MCA.
_get_bus:
call _getprocessor
xor dx, dx ! Assume XT
cmp ax, #286 ! An AT has at least a 286
jb got_bus
inc dx ! Assume AT
movb ah, #0xC0 ! Code for get configuration
0