diff options
| -rwxr-xr-x | make.sh | 41 | ||||
| -rw-r--r-- | src/boot/boot.stage1.asm | 119 | ||||
| -rw-r--r-- | src/boot/boot.stage2.asm | 10 | ||||
| -rw-r--r-- | src/kernel/kernel.asm | 4 |
4 files changed, 58 insertions, 116 deletions
@@ -1,9 +1,11 @@ #!/bin/sh -AS_BOOT="nasm -fbin -Isrc/boot/" -AS_KERN="nasm -fbin -Isrc/boot/ -Isrc/kernel/" +AS_BOOT="nasm -felf32 -Isrc/boot/" +AS_KERN="nasm -felf32 -Isrc/boot/ -Isrc/kernel/" SRC_DIR="src" DST_DIR="dst" +LD="ld -T linker.ld -m elf_i386 -nostdlib" +LD_FILE="linked.o" IMG_FILE="disk.img" ISO_FILE="disk.iso" NAME="kotoriforth" @@ -19,32 +21,17 @@ build() { clean mkdir -p $DST_DIR/boot - $AS_BOOT $SRC_DIR/boot/boot.stage1.asm -o $DST_DIR/s1_boot.bin - $AS_BOOT $SRC_DIR/boot/boot.stage2.asm -o $DST_DIR/s2_boot.bin - $AS_KERN $SRC_DIR/kernel/kernel.asm -o $DST_DIR/kernel.bin - dd if=/dev/zero of=disk.img bs=512 count=2880 - dd if=$DST_DIR/s1_boot.bin of=$IMG_FILE conv=notrunc - echo "Written s1_boot.bin to $IMG_FILE" - dd if=$DST_DIR/s2_boot.bin of=$IMG_FILE conv=notrunc bs=512 seek=1 - echo "Written s2_boot.bin to $IMG_FILE" - S2_SECTORS=$(( ($(wc -c < "$DST_DIR/s2_boot.bin") + 511) / 512 )) - KERNEL_OFFSET=$((1 + S2_SECTORS)) - dd if=$DST_DIR/kernel.bin of=$IMG_FILE conv=notrunc bs=512 \ - seek=$KERNEL_OFFSET status=none - echo "Written kernel.bin to $IMG_FILE" - xorriso -as mkisofs \ - -b $IMG_FILE \ - -no-emul-boot \ - -boot-load-size 4 \ - -boot-info-table \ - -o $ISO_FILE \ - . - - - if [ -f $ISO_FILE ]; then - echo "Bootable ISO created" - fi + mkdir -p $DST_DIR/objs + $AS_BOOT $SRC_DIR/boot/boot.stage1.asm -o $DST_DIR/objs/s1_boot.o + $AS_BOOT $SRC_DIR/boot/boot.stage2.asm -o $DST_DIR/objs/s2_boot.o + $AS_KERN $SRC_DIR/kernel/kernel.asm -o $DST_DIR/objs/kernel.o + + $LD $DST_DIR/objs/s1_boot.o \ + $DST_DIR/objs/s2_boot.o \ + $DST_DIR/objs/kernel.o \ + -o $LD_FILE + objcopy -O binary $LD_FILE $DST_DIR/$IMG_FILE } run() diff --git a/src/boot/boot.stage1.asm b/src/boot/boot.stage1.asm index 70e2f17..d8dc84a 100644 --- a/src/boot/boot.stage1.asm +++ b/src/boot/boot.stage1.asm @@ -1,112 +1,59 @@ - ;; boot.stage1.asm - - [org 0x7c00] + ;; boot.stage1.asm [bits 16] - %include "boot.common.asm" + global _start - %define LOAD_ADDRESS 0x7C00 - %define STAGE2_ADDRESS 0x7E00 - %define RELOCATE_ADDRESS 0x0600 - %define SECTOR_SIZE 512 - %define OFFSET_RELOC (RELOCATE_ADDRESS - LOAD_ADDRESS) - %define STAGE2_SECTORS 8 + %include "boot.stage1.print.asm" + %define READ_SECTORS_NUM 64 + %define BOOT_LOAD_ADDR 0x7c00 + %define STAGE2_ADDR 0x7e00 + %define SECTOR_SIZE 512 - global _start +section .boot_sector _start: - jmp 0x0000:.setup_segments - .setup_segments: - ;; Disable interrupts - cli - ;; Setup segment registers xor ax, ax mov ds, ax mov es, ax mov ss, ax mov gs, ax + mov fs, ax - mov sp, LOAD_ADDRESS ; Adjust stack - - ;; Save disk - mov [drive], dl ; Get drive ID from BIOS - - sti - - mov si, LOAD_ADDRESS - mov di, RELOCATE_ADDRESS - mov cx, SECTOR_SIZE - cld - rep movsdb - - jmp 0x0000:(next) -_read_disk_real_mode: - .start: - cmp cx, 127 - jbe .good_size - pusha - mov cx, 127 - call _read_disk_real_mode - popa - add eax, 127 - add dx, 127*512/16 - sub cx, 127 - jmp .start - - .good_size: - mov [DAP.LBA_lower], ax - mov [DAP.num_sectors], cx - mov [DAP.buf_segment], dx - mov [DAP.buf_offset], bx - mov dl, [disk] mov si, DAP - mov ah, 0x42 + mov ah, 0x42 ; Extended read function + mov dl, 0x80 ; Drive Number (starts at 0x80, thus it is 1) int 0x13 - jc .disk_read_err - ret - .disk_read_err: - mov si, disk_read_err_msg - call _print_string - .halt: hlt - jmp .halt + jc _disk_read_err -_print_string: - pusha - mov ah, 0x0e - mov bh, 0x00 +_ignore_disk_read_err: + mov si, _jmp_msg + call _print_string + jmp 0x0000:STAGE2_ADDR - .print_loop: - lodsb - test al, al - je .print_return - int 0x10 - jmp .print_loop +_disk_read_err: + cmp word [DAP.num_sectors], READ_SECTORS_NUM + jle _ignore_disk_read_err - .print_return: - popa - ret + mov si, _disk_read_err_msg + call _print_string -_end: - hlt - jmp _end + .halt: hlt + jmp .halt -drive: db 0 -disk_read_err_msg: db "Failed to read disk", 13, 10, 0 -disk_read_success_msg: db "Read the disk successfully", 13, 10, 0 -boot_msg: db "Booting", 13, 10, 0 + align 16 DAP: - db 0x10 - db 0 - .num_sectors: dw 127 - .buf_offset: dw 0x0 - .buf_segment: dw 0x0 - .LBA_lower: dd 0x0 - .LBA_upper: dd 0x0 + db 0x10 ; Size of packet + db 0 ; Zero + .num_sectors: + dw READ_SECTORS_NUM ; Number of sectors to be read + dd (BOOT_LOAD_ADDR + SECTOR_SIZE) ; Destination address + dq 1 ; Which sector to start at + +_disk_read_err_msg: db "Disk error", 13, 10, 0 +_jmp_msg: db "Jumping to Stage 2", 13, 10, 0 - ;; Padding and magic number times 510 - ($ - $$) db 0 dw 0xaa55 - diff --git a/src/boot/boot.stage2.asm b/src/boot/boot.stage2.asm index 97a2d05..759b784 100644 --- a/src/boot/boot.stage2.asm +++ b/src/boot/boot.stage2.asm @@ -1,13 +1,19 @@ ;; boot.stage2.asm [bits 16] - [org 0x0000] %include "boot.stage2.a20.asm" %include "boot.stage2.pm.asm" + %include "boot.stage1.print.asm" + +section .stage2 - %define KERNEL_OFFSET 0x1000 _s2_entry: + mov si, boot_stage2_msg + call _print_string call _enable_a20 call _enable_pm [bits 32] + + +boot_stage2_msg: db "Entering Stage 2", 13, 10, 0 diff --git a/src/kernel/kernel.asm b/src/kernel/kernel.asm index 244be91..15da780 100644 --- a/src/kernel/kernel.asm +++ b/src/kernel/kernel.asm @@ -1,10 +1,12 @@ ;; kernel.asm [bits 32] - [org 0x1000] + %define VGA_COLOR_BLACK 0 %define VGA_COLOR_GREEN 2 %define VGA_COLOR_RED 4 +section .kernel + _kernel_entry: |
