+
+str_read_error: .asciz "Read error while reading track: "
+
+abort_read:
+ mov $str_read_error, %esi
+ call putstr
+ mov cur_track, %eax
+ call print_num
+ mov $10, %al
+ call putchar
+
+ cli
+0: hlt
+ jmp 0b
+
+
+ # better print routines, since we're not constrainted by the 512b of
+ # the boot sector.
+ .global cursor_x
+ .global cursor_y
+cursor_x: .long 0
+cursor_y: .long 0
+
+putchar:
+ pushal
+ call ser_putchar
+
+ cmp $10, %al
+ jnz 0f
+ call video_newline
+ jmp 1f
+
+0: push %eax
+ mov cursor_y, %eax
+ mov $80, %ecx
+ mul %ecx
+ add cursor_x, %eax
+ mov %eax, %ebx
+ pop %eax
+
+ mov $0xb8000, %edx
+
+ # this looks retarded. in nasm: [ebx * 2 + edx]
+ mov %al, (%edx, %ebx, 2)
+ movb $7, 1(%edx, %ebx, 2)
+ incl cursor_x
+ cmpl $80, cursor_x
+ jnz 1f
+ call video_newline
+
+1: popal
+ ret
+
+ # expects string pointer in esi
+putstr:
+ mov (%esi), %al
+ cmp $0, %al
+ jz 0f
+ call putchar
+ inc %esi
+ jmp putstr
+0: ret
+
+ # expects number in eax
+print_num:
+ # save registers
+ pushal
+
+ mov $numbuf + 16, %esi
+ movb $0, (%esi)
+ mov $10, %ebx
+convloop:
+ xor %edx, %edx
+ div %ebx
+ add $48, %dl
+ dec %esi
+ mov %dl, (%esi)
+ cmp $0, %eax
+ jnz convloop
+
+ call putstr
+
+ # restore regs
+ popal
+ ret
+
+
+video_newline:
+ movl $0, cursor_x
+ incl cursor_y
+ cmpl $25, cursor_y
+ jnz 0f
+ call scrollup
+ decl cursor_y
+0: ret
+
+scrollup:
+ pushal
+ # move 80 * 24 lines from b80a0 -> b8000
+ mov $0xb8000, %edi
+ mov $0xb80a0, %esi
+ mov $960, %ecx
+ addr32 rep movsl
+ # clear last line (b8f00)
+ mov $0xb8f00, %edi
+ xor %eax, %eax
+ mov $40, %ecx
+ addr32 rep stosl
+ popal
+ ret
+
+clearscr:
+ mov $0xb8000, %edi
+ xor %eax, %eax
+ mov $1000, %ecx
+ addr32 rep stosl
+ ret
+
+ .set UART_DATA, 0x3f8
+ .set UART_LSTAT, 0x3fd
+ .set LST_TREG_EMPTY, 0x20
+
+ser_putchar:
+ push %dx
+
+ 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
+wait: in %dx, %al
+ and $LST_TREG_EMPTY, %al
+ jz wait
+ mov $UART_DATA, %dx
+ mov %ah, %al
+ out %al, %dx
+
+ pop %dx
+ ret
+
+
+
+ena20_msg: .asciz "A20 line enabled\n"
+
+enable_a20:
+ call test_a20
+ jnc a20done
+ call enable_a20_kbd
+ call test_a20
+ jnc a20done
+ call enable_a20_fast
+ call test_a20
+ jnc a20done
+ # keep trying ... we can't do anything useful without A20 anyway
+ jmp enable_a20
+a20done:
+ mov $ena20_msg, %esi
+ call putstr
+ ret
+
+ # CF = 1 if A20 test fails (not enabled)
+test_a20:
+ mov $0x07c000, %ebx
+ mov $0x17c000, %edx
+ movl $0xbaadf00d, (%ebx)
+ movl $0xaabbcc42, (%edx)
+ subl $0xbaadf00d, (%ebx)
+ ret
+
+ # enable A20 line through port 0x92 (fast A20)
+enable_a20_fast:
+ mov $ena20_fast_msg, %esi
+ call putstr
+
+ in $0x92, %al
+ or $2, %al
+ out %al, $0x92
+ ret
+
+ena20_fast_msg: .asciz "Attempting fast A20 enable\n"
+
+
+ # enable A20 line through the keyboard controller
+ .set KBC_DATA_PORT, 0x60
+ .set KBC_CMD_PORT, 0x64
+ .set KBC_STATUS_PORT, 0x64
+ .set KBC_CMD_RD_OUTPORT, 0xd0
+ .set KBC_CMD_WR_OUTPORT, 0xd1
+
+ .set KBC_STAT_OUT_RDY, 0x01
+ .set KBC_STAT_IN_FULL, 0x02
+
+enable_a20_kbd:
+ mov $ena20_kbd_msg, %esi
+ call putstr
+
+ call kbc_wait_write
+ mov $KBC_CMD_WR_OUTPORT, %al
+ out %al, $KBC_CMD_PORT
+ call kbc_wait_write
+ mov $0xdf, %al
+ out %al, $KBC_DATA_PORT
+ ret
+
+ena20_kbd_msg: .asciz "Attempting KBD A20 enable\n"
+
+ # wait until the keyboard controller is ready to accept another byte
+kbc_wait_write:
+ in $KBC_STATUS_PORT, %al
+ and $KBC_STAT_IN_FULL, %al
+ jnz kbc_wait_write
+ ret
+
+numbuf: .space 16
+
+
+# this is not boot loader code. It's called later on by the main
+# kernel code in 32bit protected mode. It's placed here because
+# it needs to be in base memory as it returns and runs in real mode.
+
+ .code32
+ .align 4
+ # place to save the protected mode IDTR pseudo-descriptor
+ # with sidt, so that it can be restored before returning
+ .short 0
+saved_idtr:
+idtlim: .short 0
+idtaddr:.long 0
+ # real mode IDTR pseudo-descriptor pointing to the IVT at addr 0
+ .short 0
+rmidt: .short 0x3ff
+ .long 0
+
+ # drop back to unreal mode to set video mode
+ .global set_mode13h
+set_mode13h:
+ pushal
+ cli
+ # save protected mode IDTR and replace it with the real mode vectors
+ sidt (saved_idtr)
+ lidt (rmidt)
+
+ # long jump to load code selector for 16bit code (6)
+ ljmp $0x30,$0f
+0:
+ .code16
+ # disable protection
+ mov %cr0, %eax
+ and $0xfffe, %ax
+ mov %eax, %cr0
+ # load cs <- 0
+ ljmp $0,$0f
+0: # zero data segments
+ xor %ax, %ax
+ mov %ax, %ds
+ mov %ax, %es
+ mov %ax, %ss
+ nop
+
+ # switch video mode by calling the video bios
+ mov $0x13, %ax
+ int $0x10
+
+ # re-enable protection
+ mov %cr0, %eax
+ or $1, %ax
+ mov %eax, %cr0
+ # long jump to load code selector for 32bit code (1)
+ ljmp $0x8,$0f
+0:
+ .code32
+ # set data selector (2) to all segment regs
+ mov $0x10, %ax
+ mov %ax, %ds
+ mov %ax, %es
+ mov %ax, %ss
+ nop
+
+ # restore 32bit interrupt descriptor table
+ lidt (saved_idtr)
+ sti
+ popal
+ ret
+
+
+ # buffer used by the track loader ... to load tracks.
+ .align 16
+buffer: