-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
327 lines (280 loc) · 8.67 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# default tool chain is GNU
TOOL_CHAIN=GNU
# TOOL_CHAIN=LLVM
# Target host config
TARGET_ARCH:=x86
#TARGET_ARCH:=arm
#TARGET_ARCH:=risc-v
TARGET_ARCH_SUB:=i386
# TARGET_ARCH_SUB:=x86_64
#TARGET_ARCH_SUB:=aarch64
ifeq ($(TARGET_ARCH_SUB), x86_64)
TARGET_CPU=x86-64
MEM_SIZE:=16G
endif
ifeq ($(TARGET_ARCH_SUB), i386)
TARGET_CPU=i386
MEM_SIZE:=32M
endif
# Building host config
HOST_ARCH:=$(shell uname -m)
HOST_KERNEL:=$(shell uname -s)
HOST_DISTRO:=$(shell uname -n)
# working tree
SRC:=src
BOOT:=arch/$(TARGET_ARCH)/$(TARGET_ARCH_SUB)/boot/
TEST=tests
BUILD:=build
TOOLS:=tools
TEST_BUILD:=$(TEST)/build
BOCHS_CONFIG:=$(TOOLS)/bochs-config
# Set cross compile tools chain PATH
HOME_BREW_ARM64_PATH:=/opt/homebrew/bin/
LINUX_PATH:=/usr/bin/
LINUX_LOCAL_PATH:=/usr/local/bin/
BREW_LLVM_PATH:=/opt/homebrew/opt/llvm/bin/
# auto config (selecting tool chain)
# Host: Apple Silicon Mac(arm64)
ifeq ($(HOST_KERNEL), Darwin)
ifeq ($(HOST_ARCH), arm64)
ifeq ($(TARGET_ARCH_SUB), i386)
TOOL_PATH:=$(HOME_BREW_ARM64_PATH)x86_64-elf-
GDB:=$(HOME_BREW_ARM64_PATH)$(TARGET_ARCH_SUB)-elf-gdb
endif
endif
# TOOL_CHAIN=LLVM
# AS:=$(TOOL_PATH)as
# LD:=$(BREW_LLVM_PATH)ld.lld
# CC:=clang
# OBJCOPY:=$(BREW_LLVM_PATH)llvm-objcopy
# OBJDUMP:=$(BREW_LLVM_PATH)llvm-objdump
# NM:=$(BREW_LLVM_PATH)llvm-nm
endif
ifeq ($(HOST_KERNEL), Linux)
ifeq ($(HOST_ARCH), x86_64)
ifeq ($(TARGET_ARCH_SUB), i386)
TOOL_PATH:=$(LINUX_PATH)
endif
endif
ifeq ($(HOST_ARCH), aarch64)
ifeq ($(TARGET_ARCH_SUB), i386)
ifeq ($(HOST_DISTRO), Asahi)
# Host: Linux (arm64) (Arch like)
TOOL_PATH:=$(LINUX_LOCAL_PATH)x86_64-elf-
endif
ifneq ($(HOST_DISTRO), Asahi)
# Host: Linux (arm64) (Debian like)
TOOL_PATH:=$(LINUX_PATH)x86_64-linux-gnu-
endif
endif
endif
GDB:=$(LINUX_PATH)gdb
endif
# using GNU toolchain (default)
ifeq ($(TOOL_CHAIN), GNU)
AS:=$(TOOL_PATH)as
LD:=$(TOOL_PATH)ld
CC:=$(TOOL_PATH)gcc
OBJCOPY:=$(TOOL_PATH)objcopy
OBJDUMP:=$(TOOL_PATH)objdump
NM:=$(TOOL_PATH)nm
endif
# # using clang
ifeq ($(TOOL_CHAIN), LLVM)
AS:=$(TOOL_PATH)as
LD:=$(LINUX_PATH)ld.lld
CC:=$(LINUX_PATH)clang
OBJCOPY:=$(LINUX_PATH)llvm-objcopy
OBJDUMP:=$(LINUX_PATH)llvm-objdump
NM:=$(TOOL_PATH)llvm-nm
endif
.PHONY: show_config
show_config:
$(info $$HOST_ARCH = ${HOST_ARCH})
$(info $$HOST_KERNEL = ${HOST_KERNEL})
$(info $$TARGET_ARCH = ${TARGET_CPU})
$(info $$AS = ${AS})
$(info $$LD = ${LD})
$(info $$CC = ${CC})
$(info $$OBJCOPY = ${OBJCOPY})
$(info $$OBJDUMP = ${OBJDUMP})
$(info $$NM = ${NM})
$(info $$GDB = ${GDB})
$(info $$QEMU_MEM_SIZE = ${MEM_SIZE})
DEBUG:= -g
# DEBUG:= -gstabs
INCLUDE:= -I $(SRC)/include/
# general CFLAGS
CFLAGS:= -fno-builtin # no built-in function in gcc
CFLAGS+= -fno-pic # no position independent code
CFLAGS+= -fno-pie # no position independent excutable
CFLAGS+= -ffreestanding
CFLAGS+= -fno-tree-vectorize # no vectorize instructor
CFLAGS+= -fno-stack-protector # no stack protector
CFLAGS+= -nostdinc # no standard c header
# CFLAGS+= -nostdinc++ # no standard c++ header
CFLAGS+= -nostdlib # no standard library
CFLAGS+= -O0 # disable all optimization
# using gcc (default)
ifeq ($(TOOL_CHAIN), GNU)
ifeq ($(TARGET_ARCH_SUB), i386)
CFLAGS+= -m32 # (when using gcc that target x86_64 (if target i686(already 32bits, then no need))) to enable i386 mode
CFLAGS+= -march=$(TARGET_CPU)
# CFLAGS+= -mtune=$(TARGET_CPU) # optimize (will be ignored if march has set)
endif
ifeq ($(TARGET_ARCH_SUB), x86_64)
CFLAGS+= -m64
ifeq ($(HOST_ARCH), x86_64)
CFLAGS+= -march=native
# CFLAGS+= -mtune=$(TARGET_CPU)
endif
ifneq ($(HOST_ARCH), x86_64)
CFLAGS+= -march=$(TARGET_CPU)
endif
endif
endif
# if using clang
ifeq ($(TOOL_CHAIN), LLVM)
CFLAGS+= --target=$(TARGET_ARCH_SUB)-unknown-none-elf
# CFLAGS+= -mcpu=$(TARGET_ARCH_SUB)
# CFLAGS+= -fno-integrated-as # comment when buidling on MacOS
endif
# for GNU AS
ifeq ($(TARGET_ARCH_SUB), i386)
ASFLAGS+= --32 # 32bit mode
ASFLAGS+= -march=$(TARGET_CPU)
ASFLAGS+= -mtune=$(TARGET_CPU) # optimize
ASFLAGS+= -O0
endif
ASFLAGS+= $(DEBUG)
# ASFLAGS+= --gstabs
# ASFLAGS+= -mmnemonic=intel # default: att
# ASFLAGS+= -msyntax=intel # default: att
# for both GNU LD and LLVM LD.LLD
LDFLAGS+= -m elf_$(TARGET_ARCH_SUB)
LDFLAGS+= -static
# combine flags properly
CFLAGS:=$(strip ${CFLAGS})
ASFLAGS:=$(strip ${ASFLAGS})
LDFLAGS:=$(strip ${LDFLAGS})
# entry points for assembly
BOOT_EP:=0x7c00
LOADER_EP:=0x1000
KERNEL_EP:=0x10000
# # # long mode test using nasm
# BOOT:=arch/$(TARGET_ARCH)/$(TARGET_ARCH_SUB)/boot/nasm/
# $(BUILD)/$(BOOT)%.bin: $(SRC)/$(BOOT)%.asm
# $(shell mkdir -p $(dir $@))
# nasm -f bin -i $(BOOT) $< -o $@
# $(BUILD)/%.o: $(SRC)/%.asm
# $(shell mkdir -p $(dir $@))
# nasm -f elf32 $(DEBUG) $< -o $@
# building from source
$(BUILD)/$(BOOT)boot.bin: $(SRC)/$(BOOT)boot.S
$(shell mkdir -p $(dir $@))
$(AS) $(ASFLAGS) $< -o [email protected]
$(LD) $(LDFLAGS) [email protected] -o [email protected] -Ttext $(BOOT_EP)
$(OBJCOPY) -O binary [email protected] $@
$(BUILD)/$(BOOT)loader.bin: $(SRC)/$(BOOT)loader.S
$(shell mkdir -p $(dir $@))
$(AS) $(ASFLAGS) $< -o [email protected]
$(LD) $(LDFLAGS) [email protected] -o [email protected] -Ttext $(LOADER_EP)
$(OBJCOPY) -O binary [email protected] $@
$(BUILD)/%.o: $(SRC)/%.S
$(shell mkdir -p $(dir $@))
$(AS) $(ASFLAGS) $< -o $@
$(BUILD)/%.o: $(SRC)/%.c
$(shell mkdir -p $(dir $@))
$(CC) $(CFLAGS) $(DEBUG) $(INCLUDE) -c $< -o $@
$(BUILD)/kernel.bin: \
$(BUILD)/kernel/start.o \
$(BUILD)/kernel/main.o \
$(BUILD)/kernel/io.o \
$(BUILD)/kernel/console.o \
$(BUILD)/kernel/printk.o \
$(BUILD)/kernel/assert.o \
$(BUILD)/kernel/debug.o \
$(BUILD)/kernel/global.o \
$(BUILD)/kernel/task.o \
$(BUILD)/kernel/schedule.o \
$(BUILD)/lib/string.o \
$(BUILD)/lib/vsprintf.o
$(shell mkdir -p $(dir $@))
$(LD) -m elf_$(TARGET_ARCH_SUB) -static $^ -o $@ -Ttext $(KERNEL_EP)
$(BUILD)/system.bin: $(BUILD)/kernel.bin
$(OBJCOPY) -O binary $< $@
$(BUILD)/system.map: $(BUILD)/kernel.bin
$(NM) $< | sort > $@
$(BUILD)/master.img: $(BUILD)/$(BOOT)boot.bin \
$(BUILD)/$(BOOT)loader.bin \
$(BUILD)/system.bin \
$(BUILD)/system.map
# yes | bximage -q -func=create -hd=16 -sectsize=512 -imgmode=flat $@
qemu-img create -o size=16M $@
dd if=$(BUILD)/$(BOOT)boot.bin of=$@ bs=512 count=1 conv=notrunc
dd if=$(BUILD)/$(BOOT)loader.bin of=$@ bs=512 count=4 seek=2 conv=notrunc
dd if=$(BUILD)/system.bin of=$@ bs=512 count=200 seek=10 conv=notrunc
# # long mode test...
# $(BUILD)/master.img: $(BUILD)/$(BOOT)boot.bin \
# $(BUILD)/system.bin \
# $(BUILD)/system.map
# qemu-img create -o size=16M $@
# dd if=$(BUILD)/$(BOOT)boot.bin of=$@ bs=512 count=1 conv=notrunc
# dd if=$(BUILD)/system.bin of=$@ bs=512 count=200 seek=10 conv=notrunc
.PHONY: clean
clean:
rm -rf $(BUILD); \
rm -f $(SRC)/bochsrc*;
QEMU_BOOT_OPTIONS+= -m $(MEM_SIZE)
QEMU_BOOT_OPTIONS+= -boot c
QEMU_BOOT_OPTIONS:= $(strip ${QEMU_BOOT_OPTIONS})
.PHONY: qemu
qemu: $(BUILD)/master.img
qemu-system-$(TARGET_ARCH_SUB) $(QEMU_BOOT_OPTIONS) -hda $<
.PHONY: qemu-gdb
qemu-gdb: $(BUILD)/master.img
qemu-system-$(TARGET_ARCH_SUB) $(QEMU_BOOT_OPTIONS) -hda $< -s -S
.PHONY: disam-boot
disam_boot:
$(OBJDUMP) -D -b binary -m $(TARGET_ARCH_SUB):x86-64 $(BUILD)/boot/boot.bin
# Bochs X11 gui-debug
.PHONY: bochs
bochs: $(BUILD)/master.img
bochs -q -f $(BOCHS_CONFIG)/bochsrc
# Bochs gdb with stub
.PHONY: bochs-gdb
bochs-gdb: $(BUILD)/master.img
bochs-gdb -q -f $(BOCHS_CONFIG)/bochsrc-gdb
# generate vmware disk image
$(BUILD)/master.vmdk: $(BUILD)/master.img
qemu-img convert -pO vmdk $< $@
.PHONY: vmdk
vmdk: $(BUILD)/master.vmdk
# generate virtualbox disk image
$(BUILD)/master.vdi: $(BUILD)/master.img
qemu-img convert -pO vdi $< $@
.PHONY: vdi
vdi: $(BUILD)/master.vdi
# for testing image in usb drive in virtualbox before going physic machine test
# should manually set path for IDE disk of usb.vmdk for usb test in virtualbox
.PHONY: vbox_usb_test
vbox_usb_test: usb
sudo vboxmanage internalcommands createrawvmdk -filename $(BUILD)/usb.vmdk -rawdisk /dev/sda; \
sudo chown kali:kali $(BUILD)/usb.vmdk
.PHONY: usb # sample code for write image to USB device
usb: $(BUILD)/master.img /dev/sda
# sudo dd if=/dev/sda of=$(BUILD)/boot/tmp.bin bs=512 count=1000 conv=notrunc
# cp $(BUILD)/boot/tmp.bin $(BUILD)/boot/usb.bin
# sudo rm $(BUILD)/boot/tmp.bin
# dd if=$(BUILD)/master.img of=$(BUILD)/boot/usb.bin bs=512 count=1000 conv=notrunc
# sudo dd if=$(BUILD)/boot/usb.bin of=/dev/sda bs=512 count=1000 conv=notrunc
# rm $(BUILD)/boot/usb.bin
# sudo dd if=/dev/sda of=$(BUILD)/boot/tmp.bin bs=512 conv=notrunc
# cp $(BUILD)/boot/tmp.bin $(BUILD)/boot/usb.bin
# sudo rm $(BUILD)/boot/tmp.bin
# dd if=$(BUILD)/master.img of=$(BUILD)/boot/usb.bin bs=512 conv=notrunc
# sudo dd if=$(BUILD)/boot/usb.bin of=/dev/sda bs=512 conv=notrunc
# rm $(BUILD)/boot/usb.bin
sudo dd if=$(BUILD)/master.img of=/dev/sda
.PHONY: test
test: $(BUILD)/master.img