X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fboot%2Fboot.s;h=bf32820c01e3c7f2a6d069abdad35272bf54b251;hb=a6dab855a9f35a8d19016f985d228f6a7b2c5335;hp=4ab605c7f43be867016e38bf749537ab4695f41e;hpb=201a6c7cbc9a6fc7f7d4126cd4e41eff658818a3;p=bootcensus diff --git a/src/boot/boot.s b/src/boot/boot.s index 4ab605c..bf32820 100644 --- a/src/boot/boot.s +++ b/src/boot/boot.s @@ -1,15 +1,35 @@ +# pcboot - bootable PC demo/game kernel +# Copyright (C) 2018 John Tsiombikas +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + .code16 .section .boot,"a" - .set scratchbuf, 0x7b00 + .set stack_top, 0x7be0 + .set read_retries, 0x7be8 + .set drive_number, 0x7bec + .set cursor_x, 0x7bee + .set scratchbuf, 0x7bf0 + .set scratchbuf_size, 16 boot: cli - cld # move stack to just below the code xor %ax, %ax mov %ax, %ss - mov $0x7b00, %sp + mov $stack_top, %sp # use the code segment for data access mov %cs, %ax mov %ax, %ds @@ -19,8 +39,16 @@ boot: call setup_serial + call get_drive_chs + + mov $loading_msg, %si + call print_str + # load the second stage boot loader and jump to it mov $_boot2_size, %eax + call print_num + + mov $_boot2_size, %eax mov %eax, %ebx shr $9, %eax and $0x1ff, %ebx @@ -37,11 +65,59 @@ boot: call read_sectors jmp boot2_addr - .set SECT_PER_TRACK, 18 - .set ARG_NSECT, 6 .set ARG_SIDX, 4 +loading_msg: .asciz "\nLoad " +driveno_msg: .asciz "Drv:" + +sect_per_track: .short 18 +num_cylinders: .short 80 +num_heads: .short 2 +heads_mask: .byte 1 + +get_drive_chs: + mov $driveno_msg, %si + call print_str + xor %eax, %eax + movb drive_number, %dl + mov %dl, %al + call print_num + mov $10, %al + call print_char + + mov $8, %ah + int $0x13 + jnc .Lok + ret + +.Lok: xor %eax, %eax + mov %ch, %al + mov %cl, %ah + rol $2, %ah + inc %ax + and $0x3ff, %ax + mov %ax, num_cylinders + + and $0x3f, %cx + mov %cx, sect_per_track + + shr $8, %dx + mov %dl, heads_mask + inc %dx + mov %dx, num_heads + + call print_num + mov $47, %al + call print_char + mov %dx, %ax + call print_num + mov $47, %al + call print_char + mov %cx, %ax + call print_num + ret + # read_sectors(first, num) read_sectors: push %bp @@ -62,62 +138,43 @@ read_sectors: pop %bp ret - .set VAR_ATTEMPTS, -2 -str_rdsec_msg: .asciz "rdsec: " -str_cyl_msg: .asciz " C " -str_head_msg: .asciz " H " -str_sec_msg: .asciz " S " - # read_sector(sidx) read_sector: push %bp mov %sp, %bp - sub $2, %sp push %cx push %dx - movw $3, VAR_ATTEMPTS(%bp) + movw $3, read_retries .Lread_try: # calculate the track (sidx / sectors_per_track) mov 4(%bp), %ax - mov $str_rdsec_msg, %si - call print_str_num16 xor %dx, %dx - mov $SECT_PER_TRACK, %cx + mov sect_per_track, %cx div %cx mov %ax, %cx - # save the remainder in ax - mov %dx, %ax + # save the remainder + push %dx # head in dh mov %cl, %dh - and $1, %dh - # cylinder (track/2) in ch [0-7] and cl[6,7]<-[8,9] - rol $7, %cx + and heads_mask, %dh + # cylinder (track/heads) in ch [0-7] and cl[6,7]<-[8,9] + push %dx + xor %dx, %dx + movw num_heads, %cx + div %cx + pop %dx + mov %ax, %cx + rol $8, %cx ror $2, %cl and $0xc0, %cl - # sector num cl[0-5] is sidx % sectors_per_track (saved in ax) + # sector num cl[0-5] is sidx % sectors_per_track + 1 + pop %ax inc %al or %al, %cl - mov $str_cyl_msg, %si - mov %cx, %ax - rol $2, %al - and $3, %al - ror $8, %ax - call print_str_num16 - - mov $str_head_msg, %si - xor %ax, %ax - mov %dh, %al - call print_str_num16 - - mov $str_sec_msg, %si - mov %cl, %al - and $0x3f, %ax - call print_str_num16 - # ah = 2 (read), al = 1 sectors mov $0x0201, %ax movb drive_number, %dl @@ -125,7 +182,7 @@ read_sector: jnc .Lread_ok # abort after 3 attempts - decw VAR_ATTEMPTS(%bp) + decw read_retries jz .Lread_fail # error detected, reset controller and retry @@ -138,121 +195,91 @@ read_sector: jmp abort_read .Lread_ok: - # DBG print first dword - mov $str_read_error + 4, %si - mov %es:(%bx), %eax - call print_str_num + mov $46, %ax + call print_char # increment es:bx accordingly (advance es if bx overflows) add $512, %bx - jno 0f + jnc 0f mov %es, %ax add $4096, %ax mov %ax, %es 0: pop %dx pop %cx - add $2, %sp pop %bp ret -str_read_error: .asciz "err read sect: " +str_read_error: .asciz "rderr:" abort_read: mov $str_read_error, %si - call print_str_num16 - hlt - -cursor_x: .byte 0 - - # prints a string (ds:si) followed by a number (eax) -print_str_num: - push %eax call print_str - pop %eax + and $0xffff, %eax call print_num - mov $13, %al - call ser_putchar mov $10, %al - call ser_putchar - ret - -print_str_num16: - push %eax - and $0xffff, %eax - call print_str_num - pop %eax - ret + call print_char + hlt # expects string pointer in ds:si print_str: - push %es pusha - pushw $0xb800 - pop %es - xor %di, %di - movb $0, cursor_x +0: mov (%si), %al + cmp $0, %al + jz .Lend + call print_char + inc %si + jmp 0b - push %si +.Lend: popa + ret + + # expects character in al +print_char: + push %es + + push %ax + cmp $10, %ax + jnz 0f + mov $32, %ax + +0: pushw $0xb800 + pop %es + movw cursor_x, %di + shl $1, %di -0: mov (%si), %al mov %al, %es:(%di) movb $7, %es:1(%di) - inc %si - add $2, %di - incb cursor_x - cmp $0, %al - jnz 0b - - pop %si - call ser_putstr + incw cursor_x - popa + pop %ax + call ser_putchar pop %es ret # expects number in eax + .global print_num print_num: # save registers - push %es pusha - xor %cx, %cx - movw $scratchbuf, %si + movw $scratchbuf + scratchbuf_size, %si + movb $0, (%si) mov $10, %ebx - -0: xor %edx, %edx +.Lconvloop: + xor %edx, %edx div %ebx add $48, %dl + dec %si mov %dl, (%si) - inc %si - inc %cx cmp $0, %eax - jnz 0b + jnz .Lconvloop - # print the backwards string - pushw $0xb800 - pop %es - movb cursor_x, %al - xor %ah, %ah - shl $1, %ax - mov %ax, %di - -0: dec %si - mov (%si), %al - movb %al, %es:(%di) - call ser_putchar - inc %di - mov $7, %al - movb %al, %es:(%di) - inc %di - dec %cx - jnz 0b + call print_str # restore regs popa - pop %es ret .set UART_DATA, 0x3f8 @@ -265,12 +292,8 @@ print_num: .set DIV_9600, 115200 / 9600 .set LCTL_8N1, 0x03 .set LCTL_DLAB, 0x80 - .set FIFO_ENABLE, 0x01 - .set FIFO_SEND_CLEAR, 0x04 - .set FIFO_RECV_CLEAR, 0x02 - .set MCTL_DTR, 0x01 - .set MCTL_RTS, 0x02 - .set MCTL_OUT2, 0x08 + .set FIFO_ENABLE_CLEAR, 0x07 + .set MCTL_DTR_RTS_OUT2, 0x0b .set LST_TREG_EMPTY, 0x20 setup_serial: @@ -289,50 +312,41 @@ setup_serial: mov $UART_LCTL, %dx out %al, %dx # clear and enable fifo - mov $FIFO_ENABLE, %al - or $FIFO_SEND_CLEAR, %al - or $FIFO_RECV_CLEAR, %al + mov $FIFO_ENABLE_CLEAR, %al mov $UART_FIFO, %dx out %al, %dx # assert RTS and DTR - mov $MCTL_DTR, %al - or $MCTL_RTS, %al - or $MCTL_OUT2, %al + mov $MCTL_DTR_RTS_OUT2, %al mov $UART_MCTL, %dx out %al, %dx ret # expects a character in al + .global ser_putchar ser_putchar: push %dx - mov %al, %ah + cmp $10, %al + jnz 0f + push %ax + mov $13, %al + call ser_putchar + pop %ax + +0: mov %al, %ah # wait until the transmit register is empty mov $UART_LSTAT, %dx -0: in %dx, %al +.Lwait: in %dx, %al and $LST_TREG_EMPTY, %al - jz 0b + jz .Lwait mov $UART_DATA, %dx mov %ah, %al out %al, %dx pop %dx ret - - # expects a string in ds:si -ser_putstr: - mov (%si), %al - cmp $0, %al - jz 0f - call ser_putchar - inc %si - jmp ser_putstr -0: ret - -drive_number: .byte 0 .org 510 .byte 0x55 .byte 0xaa - boot2_addr: