-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
97 lines (70 loc) · 3.54 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
ASM := nasm
SRC_DIR := src
BUILD_DIR := bin/build
DEPS_DIR := $(BUILD_DIR)/deps
OUTPUT_DIR := bin/target/i386
PROGRAM_DIR := programs/bin/
BOOTLOADER_SRC := $(SRC_DIR)/boot/boot.asm
BOOTLOADER_OUT := $(BUILD_DIR)/boot.bin
KERNEL_SRC_ASM := $(wildcard $(SRC_DIR)/kernel/*.asm) $(wildcard $(SRC_DIR)/kernel/**/*.asm)
KERNEL_SRC_C := $(wildcard $(SRC_DIR)/kernel/*.c) $(wildcard $(SRC_DIR)/kernel/**/*.c)
KERNEL_OUT_ASM := $(patsubst $(SRC_DIR)/kernel/%.asm, $(BUILD_DIR)/%.asm.obj, $(KERNEL_SRC_ASM))
KERNEL_OUT_C := $(patsubst $(SRC_DIR)/kernel/%.c, $(BUILD_DIR)/%.c.obj, $(KERNEL_SRC_C))
CC := i386-elf-gcc
INCLUDES := -Isrc/kernel/include/
KERN_FLAGS := -g -ffreestanding -falign-jumps -falign-labels -falign-loops -falign-functions -fomit-frame-pointer -finline-functions -fno-builtin -Wcpp -Wunused -Werror -Wall -Wextra -nostdlib -nostartfiles -nodefaultlibs -O0
COMP_FALGS := $(KERN_FLAGS) -MMD -MF
.PHONY:
.SILENT:
all: create bootloader kernel programz
echo "Linking the bootloader, kernel and creating the final bootable OS..."
dd if=$(BUILD_DIR)/boot.bin > $(OUTPUT_DIR)/os.img status=none
dd if=$(BUILD_DIR)/kernel.bin >> $(OUTPUT_DIR)/os.img status=none
# Fill it with empty sectors. Here I will store my kernel. Loading the kernel from the FAT formatted
# disk is hard work.
dd if=/dev/zero bs=512 count=200 >> $(OUTPUT_DIR)/os.img status=none
truncate -s 100K $(OUTPUT_DIR)/os.img
# Now this portion is the FAT16 tabel entries. Add 4 magic bytes to define FAT16 table start.
# I'm using 2 FAT tables. So the method has to be done twice.
# First FAT table.
echo -n -e '\xf8\xff\xff\xff' >> $(OUTPUT_DIR)/os.img
dd if=/dev/zero count=508 bs=1 >> $(OUTPUT_DIR)/os.img status=none
dd if=/dev/zero count=255 bs=512 >> $(OUTPUT_DIR)/os.img status=none
# Second FAT table.
echo -n -e '\xf8\xff\xff\xff' >> $(OUTPUT_DIR)/os.img
dd if=/dev/zero count=508 bs=1 >> $(OUTPUT_DIR)/os.img status=none
dd if=/dev/zero count=255 bs=512 >> $(OUTPUT_DIR)/os.img status=none
dd if=/dev/zero bs=1M count=256 >> $(OUTPUT_DIR)/os.img status=none
truncate -s 256M $(OUTPUT_DIR)/os.img
# Now push all the programs to the filesystem.
mcopy -i $(OUTPUT_DIR)/os.img $(PROGRAM_DIR)/blank.elf "::blank.elf"
mcopy -i $(OUTPUT_DIR)/os.img $(PROGRAM_DIR)/shell.elf "::shell.elf"
echo "Successfully created $(OUTPUT_DIR)/os.img"
bootloader:
echo "Compiling the bootloader..."
echo "Creating $(BOOTLOADER_OUT)..."
$(ASM) -f bin $(BOOTLOADER_SRC) -o $(BOOTLOADER_OUT)
# Create a full kernel object binary by resolving all the linker symbols.
kernel: $(KERNEL_OUT_ASM) $(KERNEL_OUT_C)
echo "Linking kernel object files..."
i386-elf-ld -g -r $^ -o $(BUILD_DIR)/kernelfull.obj
echo "Extracting raw binary from full kernel object..."
i386-elf-gcc -T $(SRC_DIR)/linker.ld $(KERN_FLAGS) $(BUILD_DIR)/kernelfull.obj -o $(BUILD_DIR)/kernel.bin
$(BUILD_DIR)/%.asm.obj: $(SRC_DIR)/kernel/%.asm
mkdir -p $(dir $@)
echo "Compiling ASM translation '$(patsubst $(SRC_DIR)/kernel/%.asm,%.asm, $<)', generating object '$@'..."
$(ASM) -f elf32 -g $< -o $@
$(BUILD_DIR)/%.c.obj: $(SRC_DIR)/kernel/%.c
mkdir -p $(dir $@)
echo "Compiling C translation '$(patsubst $(SRC_DIR)/kernel/%.c,%.c, $<)', generating object '$@'..."
$(CC) -c $(INCLUDES) $(COMP_FALGS) $(patsubst %.c.obj, $(DEPS_DIR)/%.d, $(notdir $@)) $< -o $@
create:
mkdir -p bin/{build/deps,target/i386}
programz:
echo "Generating programs for AsifOS..."
$(MAKE) -s -C ./programs/
clean:
rm -rf bin
$(MAKE) clean -s -C ./programs/
# Inlcude all the header file dependencies.
-include $(wildcard $(DEPS_DIR)/*.d)