Skip to content

Commit

Permalink
Add and configure build tools
Browse files Browse the repository at this point in the history
  • Loading branch information
jonpas committed Jan 17, 2018
1 parent 5c16e90 commit f83e156
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf

*.img
59 changes: 59 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
.RECIPEPREFIX +=
.POSIX:

BIN = bin
SRC = src
CC = gcc
CFLAGS = -Wextra -fno-builtin -fno-stack-protector -m32 -nostdlib -nostdinc -std=gnu99 -ggdb3
LINK = ld
LINKFLAGS = -melf_i386 -Tlink.ld
CASM = nasm
ASMFLAGS = -f elf
GRUB = grub-mkrescue
GRUBFLAGS = iso

BOOT = iso/boot
ELF = $(BIN)/main.elf
IMG = $(BIN)/main.img

OBJS := $(foreach IN_EXT, .c .asm, $(patsubst %$(IN_EXT), %.o, $(wildcard $(SRC)/*$(IN_EXT)))) $(SRC)/boot.o

all: $(IMG)

$(IMG): $(ELF)
@echo " GRUB $<"
@cp '$<' '$(BOOT)'
@$(GRUB) -o '$@' $(GRUBFLAGS)

$(ELF): $(OBJS)
@echo " LINK $<"
@$(LINK) -o '$(ELF)' $(LINKFLAGS) $^

$(SRC)/%.o: $(SRC)/%.c
@mkdir -p $(BIN)
@echo " CC $<"
@$(CC) $(CFLAGS) -o '$@' -c '$<' $(CLIBS)

$(SRC)/%.o: $(SRC)/%.asm
@echo " CASM $<"
@$(CASM) $(ASMFLAGS) $< -o $@

bochs: $(IMG)
CYLINDERS="$$(($$(stat -c '%s' '$<') / 512))" && \
bochs -qf /dev/null \
'ata0-master: type=disk, path="$<", mode=flat, cylinders='"$$CYLINDERS"', heads=1, spt=1' \
'boot: disk' \
'display_library: sdl' \
'megs: 128'

clean:
rm -rf $(BIN) $(SRC)/*.o $(BOOT)/*.elf

debug: $(IMG)
qemu-system-i386 -hda '$<' -S -s &
@gdb $(ELF) -x gdb.gdb

qemu: $(IMG)
qemu-system-i386 -drive file=$<,format=raw

.PHONY: all bochs clean debug qemu
5 changes: 5 additions & 0 deletions iso/boot/grub/grub.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set timeout=0
set default="0"
menuentry "main" {
multiboot /boot/main.elf
}
32 changes: 32 additions & 0 deletions link.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
ENTRY(start)
SECTIONS
{

.mbheader 0x100000 :
{
*(.mbheader)
}
.text :
{
code = .; _code = .; __code = .;
*(.text)
. = ALIGN(4096);
}

.data :
{
data = .; _data = .; __data = .;
*(.data)
*(.rodata)
. = ALIGN(4096);
}

.bss :
{
bss = .; _bss = .; __bss = .;
*(.bss)
. = ALIGN(4096);
}

end = .; _end = .; __end = .;
}
39 changes: 39 additions & 0 deletions src/boot.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
MBOOT_PAGE_ALIGN equ 1<<0 ; Load kernel and modules on a page boundary
MBOOT_MEM_INFO equ 1<<1 ; Provide your kernel with memory info
MBOOT_HEADER_MAGIC equ 0x1BADB002 ; Multiboot Magic value
; NOTE: We do not use MBOOT_AOUT_KLUDGE. It means that GRUB does not
; pass us a symbol table.
MBOOT_HEADER_FLAGS equ MBOOT_PAGE_ALIGN | MBOOT_MEM_INFO
MBOOT_CHECKSUM equ -(MBOOT_HEADER_MAGIC + MBOOT_HEADER_FLAGS)


[BITS 32]

[GLOBAL mboot]
[EXTERN code]
[EXTERN bss]
[EXTERN end]

section .mbheader

mboot:
dd MBOOT_HEADER_MAGIC
dd MBOOT_HEADER_FLAGS
dd MBOOT_CHECKSUM
dd mboot
dd code
dd bss
dd end
dd start

section .text

[GLOBAL start]
[EXTERN main]

start:
; Load multiboot information:
push ebx
cli
call main
jmp $
5 changes: 5 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
struct multiboot;

int main(struct multiboot *mboot_ptr) {
return 0xDEADBABA;
}

0 comments on commit f83e156

Please sign in to comment.