-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
148 lines (102 loc) · 4.24 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#* ************************************************************************** *#
#* *#
#* ::: :::::::: *#
#* Makefile :+: :+: :+: *#
#* +:+ +:+ +:+ *#
#* By: blukasho <[email protected]> +#+ +:+ +#+ *#
#* +#+#+#+#+#+ +#+ *#
#* Created: 2020/04/06 18:38:14 by blukasho #+# #+# *#
#* Updated: 2020/04/08 08:25:10 by blukasho ### ########.fr *#
#* *#
#* ************************************************************************** *#
# general parameters
OBJ_DIR = obj/
BIN_NAME = kernel.bin
GCC = gcc
# kernel parameters
SRCS_KERNEL = kernel.c printf.c putchar.c screen.c
SRCS_KERNEL_DIR = srcs/kernel/
OBJ_KERNEL = $(addprefix $(OBJ_DIR), $(SRCS_KERNEL:.c=.o))
GCC_KERNEL_FLAGS = -Iinclude -m32 -nostdlib -fno-builtin # -nostdlib not search std lib for linking, -nostinc not search header file in standart dirs
# asm parameters
SRCS_ASM = loader.s
SRCS_ASM_DIR = srcs/loader/
OBJ_ASM = $(addprefix $(OBJ_DIR), $(SRCS_ASM:.s=.o))
# linker parameters
SRCS_LINKER = linker.ld
SRCS_LINKER_DIR = srcs/linker/
# hdd.img parameters, need sudo rules
HDD_IMG = hdd.img
UNITS = M
HDD_SIZE = 10 #units M
START_SECTOR = 4096
END_SECTOR = 20479
SIZE_SECTOR = 512
FILE_SYSTEM = fat32 # file system inside hdd.img
DEV_LOOP0 = /dev/loop20 # block devise for img mounting
DEV_LOOP1 = /dev/loop21 # block devise for part mounting
DEV_NULL = /dev/null # null devise
#DEV_NULL = /dev/stdout # if need see debug info comment 63 line and uncommen 65
LABEL = msdos
TEMP_FOLDER = /mnt/tmp_hdd_img/
GRUB_FOLDER = ./boot
# general rules
all: $(BIN_NAME)
$(HDD_IMG): image
image: $(BIN_NAME)
$(info Creating $(HDD_IMG))
@dd if=/dev/zero of=./$(HDD_IMG) bs=1$(UNITS) count=$(HDD_SIZE) status=progress 2>$(DEV_NULL)
$(info Mount $(HDD_IMG) to $(DEV_LOOP0))
@losetup $(DEV_LOOP0) ./$(HDD_IMG)
$(info Creating $(LABEL) label inside $(HDD_IMG))
@parted $(DEV_LOOP0) mklabel $(LABEL) 2>$(DEV_NULL)
$(info Creating $(FILE_SYSTEM) part, size $(HDD_SIZE) inside $(HDD_IMG))
@parted $(DEV_LOOP0) mkpart primary $(FILE_SYSTEM) 2M $(HDD_SIZE) 2>$(DEV_NULL)
$(info Set boot first part in $(HDD_IMG))
@parted $(DEV_LOOP0) set 1 boot on 2>$(DEV_NULL)
@losetup $(DEV_LOOP1) $(HDD_IMG) --offset `echo \`fdisk -l $(DEV_LOOP0) | sed -n 9p | awk '{print $$3}'\`*512 | bc` --sizelimit `echo \`fdisk -l $(DEV_LOOP0) | sed -n 9p | awk '{print $$4}'\`*512 | bc`
$(info Umount $(DEV_LOOP0))
@losetup -d $(DEV_LOOP0)
$(info Creating $(FILE_SYSTEM) structure in part)
@mkfs.vfat -F 32 $(DEV_LOOP1) 2>$(DEV_NULL)
$(info Creating mount folder $(TEMP_FOLDER))
@mkdir $(TEMP_FOLDER)
$(info Mount $(DEV_LOOP1) to $(TEMP_FOLDER))
@mount -o loop $(DEV_LOOP1) $(TEMP_FOLDER)
$(info Copy files to image)
@cp -rvf $(GRUB_FOLDER) $(BIN_NAME) -t $(TEMP_FOLDER)
$(info Install grub MBR to $(DEV_LOOP1))
@echo "device (hd0) $(HDD_IMG)\n \
root (hd0,0)\n \
setup (hd0)\n \
quit\n" | grub --batch
$(info Umount $(DEV_LOOP1) from $(TEMP_FOLDER))
@umount $(TEMP_FOLDER)
$(info Remove $(TEMP_FOLDER))
@rm -rf $(TEMP_FOLDER)
$(info Umount $(DEV_LOOP1) from block device)
@losetup -d $(DEV_LOOP1)
run: $(OBJ_DIR) $(BIN_NAME) $(HDD_IMG)
@qemu-system-i386 -hda $(HDD_IMG)
$(BIN_NAME): $(OBJ_DIR) $(OBJ_KERNEL) $(OBJ_ASM)
$(info Linked $(OBJ_KERNEL) to $(BIN_NAME))
@cd $(OBJ_DIR) && ld -m elf_i386 -T ../$(SRCS_LINKER_DIR)$(SRCS_LINKER) -o ../$(BIN_NAME) *.o
$(OBJ_DIR):
@mkdir $(OBJ_DIR)
$(OBJ_DIR)%.o: $(SRCS_KERNEL_DIR)%.c
$(info Compile $< to $@)
@$(GCC) $(GCC_KERNEL_FLAGS) -o $@ -c $<
$(OBJ_DIR)%.o: $(SRCS_ASM_DIR)%.s
$(info Compile $< to $@)
@as --32 -o $@ $<
clean:
@rm -rf $(OBJ_DIR)
fclean: clean
@rm -rf $(BIN_NAME)
@rm -rf $(HDD_IMG)
force_fclean: fclean
@umount $(TEMP_FOLDER) 2>$(DEV_NULL)
@rm -rf $(TEMP_FOLDER) 2>$(DEV_NULL)
@losetup -d $(DEV_LOOP0) $(DEV_LOOP1) 2>$(DEV_NULL)
re: fclean all
.PHONY: all clean fclean re force_fclean image