TSOS/src/boot/mbr.s

83 lines
2.6 KiB
ArmAsm

; Copyright 2022 Mattia Giambirtone & Contributors
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
; Definition of the MBR (Master Boot Record). This is basically our bootloader and
; is located in the first 512 bytes of the drive we're booting from. From here, we
; do some basic setup and then call into the kernel
[org 0x7c00] ; Address where the code expects to be loaded in. The BIOS always loads us here
kernel_offset: equ 0x1000
; We save the value of the current boot drive
mov [boot_drive], dl
; Now we setup the stack by setting the
; base pointer to address 0x9000. The address
; itself doesn't matter as long as it's far
; enough away from memory already in use by
; the BIOS
mov bp, 0x9000
mov sp, bp
call bios_cls
mov si, real_mode_msg
call bios_println
call load_kernel
call switch_to_protected_mode
jmp $ ; Keeps jumping at the current address (loops forever)
; Now we include our "function definitions" (after the
; loop, so they're never executed unless explicitly called)
%include "src/boot/util/disk.s"
%include "src/boot/util/io.s"
%include "src/boot/gdt.s"
%include "src/boot/switch32.s"
; Here we define our variables: They need to be defined after the
; halting because otherwise they will be executed as code
real_mode_msg: db "TSOS is starting up", 0
protected_mode_msg: db "Switched to protected mode", 0
loading_kernel_msg: db "Loading kernel into memory", 0
boot_drive: db 0
[bits 16] ; All x86 CPUs start in 16 bit (aka "real") mode, so we tell nasm to emit 16-bit code
load_kernel: ; Loads the kernel into memory
mov si, loading_kernel_msg
call bios_println
mov bx, kernel_offset
mov dh, 3
mov dl, [boot_drive]
call load_disk
ret
[bits 32]
BEGIN_32BIT: ; After the switch we will get here
mov esi, protected_mode_msg
; My modified print function takes an offset
; to add to the start of the video memory that
; is added before writing. We skip the first
; 320 bytes so that we don't overwrite the log
; messages we have already written
mov ecx, 0x140
call vga_print
call kernel_offset
jmp $
; Padding and magic number
times 510 - ($-$$) db 0
dw 0xaa55