Skip to content

Instantly share code, notes, and snippets.

@beit747

beit747/Makefile Secret

Last active July 5, 2022 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beit747/fc21657b604ce9ac56b022aa7bdeb2c4 to your computer and use it in GitHub Desktop.
Save beit747/fc21657b604ce9ac56b022aa7bdeb2c4 to your computer and use it in GitHub Desktop.
Files to compile boot loader and compile/link kernel
[org 0x7c00]
; Setup stack
mov bp, 0xffff ; Stack base (bottom)
mov sp, bp ; Stack pointer to top of base
mov si, MSG_REAL_MODE
call print_string
mov si, MSG_LOADING_KERNEL
call print_string
mov al, 9 ; Tell BIOS to move 9 sectors after the boot sector
mov bx, kernel_entry ; [0x7e00]
call read_from_disk
; Switch to protected mode
call switch_to_pm
jmp $
; Tell nasm to output 32-bit bytecode
[bits 32]
BEGIN_PM:
mov esi, MSG_PROT_MODE
call print_string_pm
; Disable cursor
call disable_cursor
; Jump to loaded kernel code
jmp kernel_entry
;jmp $
%include "functions/my_new_functions.asm"
[bits 16]
; Includes
%include "functions/my_functions.asm"
%include "functions/read_from_disk.asm"
%include "functions/switch_to_pm.asm"
%include "functions/my_pm_functions.asm"
MSG_REAL_MODE db "Started in 16-bit Real Mode", 0
MSG_LOADING_KERNEL db "Loading kernel from disk to address 0x7e00",0
MSG_PROT_MODE db "Successfully Switched into 32-bit Protected Mode", 0
; Pad bootsector
times 510-($-$$) db 0
dw 0xaa55
; 0x7c00+512
kernel_entry:
; Kernel code will be loaded here
boot.asm:
global start
extern k_main
section .text
start:
jmp k_main
OUTPUT_FORMAT("binary")
ENTRY("start")
SECTIONS
{
. = 0x7e00;
.text :
{
linker.o (.text);
* (.text.startup);
* (.text*);
* (.data*);
* (.rodata*);
}
}
rm -f *.bin *.o
i686-elf-gcc -fno-lto -fno-pie -ffreestanding -c main.c -o main.o -fno-stack-protector -m32
nasm linker.asm -o linker.o -felf32
i686-elf-ld --nmagic --discard-all --strip-all -m elf_i386 -Tlinker.ld linker.o main.o -o kernel.bin
nasm boot.asm -f bin -o boot.bin
cat boot.bin kernel.bin > os_image.bin
truncate os_image.bin -s 10240
qemu-system-x86_64 os_image.bin
CC = i686-elf-gcc
LD = i686-elf-ld
SRC = $(wildcard *.c)
OBJ = $(SRC:.c=.o)
ASMSRC = $(wildcard *.asm)
ASMOBJ = $(ASMSRC:.asm=.o)
CFLAGS = -fno-builtin -ffreestanding -O2 -no-pie -fno-pic -mno-sse
LDFLAGS = --nmagic --discard-all --strip-all -melf_i386
%.o : %.c
$(CC) $^ -c -o $@ $(CFLAGS) -m32
%.o : %.asm
nasm -felf32 $^ -o $@
boot.bin: $(ASMOBJ) $(OBJ)
$(LD) $(OBJ) $(ASMOBJ) $(LDFLAGS) -Tlinker.ld -o boot.bin
clean:
@rm *.o *.bin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment