2023-11-09 22:42:47 +02:00
|
|
|
.code16
|
|
|
|
|
|
|
|
#########################################
|
|
|
|
#
|
|
|
|
# STAGE 1 BOOTLOADER
|
|
|
|
#
|
|
|
|
# its sole purpose is to load stage2 from
|
|
|
|
# bios boot partition
|
|
|
|
#
|
|
|
|
#########################################
|
|
|
|
|
|
|
|
.section .stage1
|
|
|
|
|
2023-11-14 03:26:03 +02:00
|
|
|
.global stage1_main
|
2023-11-11 22:49:00 +02:00
|
|
|
stage1_main:
|
2024-01-07 17:24:33 +02:00
|
|
|
# setup segments and stack
|
|
|
|
xorw %ax, %ax
|
2023-11-09 22:42:47 +02:00
|
|
|
movw %ax, %ds
|
|
|
|
movw %ax, %es
|
|
|
|
movw %ax, %ss
|
2023-11-13 18:55:48 +02:00
|
|
|
movl $0x7C00, %esp
|
2023-11-09 22:42:47 +02:00
|
|
|
|
|
|
|
# save boot disk number
|
2023-11-14 03:26:03 +02:00
|
|
|
call read_stage2_into_memory
|
2023-11-13 18:55:48 +02:00
|
|
|
|
2023-11-11 22:49:00 +02:00
|
|
|
jmp stage2_main
|
2023-11-09 22:42:47 +02:00
|
|
|
|
2023-11-14 03:26:03 +02:00
|
|
|
.global print_and_halt
|
2023-11-09 22:42:47 +02:00
|
|
|
print_and_halt:
|
|
|
|
call puts
|
|
|
|
halt:
|
|
|
|
hlt
|
|
|
|
jmp halt
|
|
|
|
|
|
|
|
|
|
|
|
#########################################
|
|
|
|
#
|
|
|
|
# STAGE 2 BOOTLOADER
|
|
|
|
#
|
|
|
|
#########################################
|
|
|
|
|
|
|
|
.section .stage2
|
2023-11-11 22:49:00 +02:00
|
|
|
|
2023-11-13 18:55:48 +02:00
|
|
|
stage2_main:
|
|
|
|
# clear screen and enter 80x25 text mode
|
|
|
|
movb $0x03, %al
|
|
|
|
movb $0x00, %ah
|
|
|
|
int $0x10
|
|
|
|
|
|
|
|
# print hello message
|
|
|
|
movw $hello_msg, %si
|
|
|
|
call puts; call print_newline
|
|
|
|
|
2024-01-07 17:24:33 +02:00
|
|
|
lgdt gdtr
|
|
|
|
|
2023-11-17 14:22:21 +02:00
|
|
|
call enter_unreal_mode
|
|
|
|
movw $unreal_enter_msg, %si
|
|
|
|
call puts; call print_newline
|
|
|
|
|
2024-01-11 22:03:52 +02:00
|
|
|
call enable_a20
|
|
|
|
|
2023-11-13 18:55:48 +02:00
|
|
|
call get_memory_map
|
2024-01-11 22:03:52 +02:00
|
|
|
|
|
|
|
call print_newline
|
2023-11-14 03:26:03 +02:00
|
|
|
call read_user_command_line
|
2023-11-13 18:55:48 +02:00
|
|
|
|
|
|
|
call print_newline
|
|
|
|
|
|
|
|
movw $start_kernel_load_msg, %si
|
|
|
|
call puts; call print_newline
|
|
|
|
|
|
|
|
call print_memory_map
|
|
|
|
|
|
|
|
call find_root_disk
|
|
|
|
call find_root_partition
|
|
|
|
|
2023-11-14 03:26:03 +02:00
|
|
|
call print_root_partition_info
|
2023-11-15 16:36:53 +02:00
|
|
|
call print_newline
|
|
|
|
|
|
|
|
call has_ext2_filesystem
|
|
|
|
testb %al, %al
|
|
|
|
jz print_and_halt
|
|
|
|
|
|
|
|
call ext2_find_kernel
|
2023-11-17 16:36:29 +02:00
|
|
|
movl $ext2_inode_read_bytes, %esi
|
|
|
|
|
|
|
|
call elf_read_kernel_to_memory
|
|
|
|
|
2023-12-09 17:32:10 +02:00
|
|
|
call vesa_set_video_mode
|
2023-11-17 16:36:29 +02:00
|
|
|
|
2023-11-17 20:31:42 +02:00
|
|
|
cli
|
2023-11-17 16:36:29 +02:00
|
|
|
|
2024-01-07 17:24:33 +02:00
|
|
|
# kernel entry point
|
|
|
|
movl %eax, %ecx
|
|
|
|
|
|
|
|
# setup kernel parameters
|
|
|
|
movl $0xD3C60CFF, %eax
|
|
|
|
movl $banan_boot_info, %ebx
|
|
|
|
|
2023-11-17 16:36:29 +02:00
|
|
|
# setup protected mode
|
2024-01-07 17:24:33 +02:00
|
|
|
movl %cr0, %edx
|
|
|
|
orb $1, %dl
|
|
|
|
movl %edx, %cr0
|
2023-11-11 22:49:00 +02:00
|
|
|
|
2024-01-07 17:24:33 +02:00
|
|
|
# jump to protected mode
|
2023-11-17 16:36:29 +02:00
|
|
|
ljmpl $0x18, $protected_mode
|
2023-11-17 14:22:21 +02:00
|
|
|
|
2023-11-17 16:36:29 +02:00
|
|
|
.code32
|
|
|
|
protected_mode:
|
2024-01-07 17:24:33 +02:00
|
|
|
# setup protected mode segments
|
|
|
|
movw $0x10, %dx
|
|
|
|
movw %dx, %ds
|
|
|
|
movw %dx, %es
|
|
|
|
movw %dx, %fs
|
|
|
|
movw %dx, %gs
|
|
|
|
movw %dx, %ss
|
|
|
|
|
|
|
|
# jump to kernel entry
|
2023-11-17 20:33:02 +02:00
|
|
|
jmp *%ecx
|
2023-11-17 16:36:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
.code16
|
2023-11-17 14:22:21 +02:00
|
|
|
enter_unreal_mode:
|
|
|
|
cli
|
|
|
|
pushw %ds
|
|
|
|
|
|
|
|
movl %cr0, %eax
|
|
|
|
orb $1, %al
|
|
|
|
movl %eax, %cr0
|
|
|
|
ljmpl $0x8, $.enter_unreal_mode_pmode
|
|
|
|
|
|
|
|
.enter_unreal_mode_pmode:
|
|
|
|
movw $0x10, %bx
|
|
|
|
movw %bx, %ds
|
|
|
|
|
2024-01-11 22:03:52 +02:00
|
|
|
andb $0xFE, %al
|
2023-11-17 14:22:21 +02:00
|
|
|
movl %eax, %cr0
|
|
|
|
ljmpl $0x0, $.enter_unreal_mode_unreal
|
|
|
|
|
|
|
|
.enter_unreal_mode_unreal:
|
|
|
|
popw %ds
|
|
|
|
sti
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
2024-01-12 19:25:07 +02:00
|
|
|
.section .data
|
|
|
|
|
2023-11-09 22:42:47 +02:00
|
|
|
hello_msg:
|
|
|
|
.asciz "This is banan-os bootloader"
|
|
|
|
|
2023-11-17 14:22:21 +02:00
|
|
|
unreal_enter_msg:
|
|
|
|
.asciz "Entered unreal mode"
|
|
|
|
|
2023-11-11 22:49:00 +02:00
|
|
|
start_kernel_load_msg:
|
|
|
|
.asciz "Starting to load kernel"
|
2023-11-17 14:22:21 +02:00
|
|
|
|
|
|
|
gdt:
|
|
|
|
.quad 0x0000000000000000
|
2024-01-07 17:24:33 +02:00
|
|
|
.quad 0x008F9A000000FFFF # 16-bit code
|
|
|
|
.quad 0x00CF92000000FFFF # 32-bit data
|
|
|
|
.quad 0x00CF9A000000FFFF # 32-bit code
|
2023-11-17 14:22:21 +02:00
|
|
|
gdtr:
|
|
|
|
.short . - gdt - 1
|
|
|
|
.quad gdt
|
2023-11-17 20:33:02 +02:00
|
|
|
|
|
|
|
banan_boot_info:
|
|
|
|
boot_command_line:
|
|
|
|
.long command_line
|
2023-11-17 22:45:35 +02:00
|
|
|
boot_framebuffer:
|
|
|
|
.long framebuffer
|
2023-11-17 20:33:02 +02:00
|
|
|
boot_memory_map:
|
|
|
|
.long memory_map
|