-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile
178 lines (153 loc) · 6.37 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
# $@ 表示目标文件
# $^ 表示所有的依赖文件
# $< 表示第一个依赖文件
# $? 表示比目标还要新的依赖文件列表
PHONY =
PW = $(shell cat ~/文档/PW)
# Kernel variable ===============================================================================
K_SRC_C = $(wildcard ./Kernel/*.c)
K_SRC_I = $(wildcard ./Kernel/g_reg_only/*.c)
K_SRC_H = Kernel/head.S Kernel/AP_Boot.S
K_OBJ_C = $(patsubst %.c,%.o,$(K_SRC_C))
K_OBJ_H = $(patsubst %.S,%.o,$(K_SRC_H))
K_PRC_H = $(patsubst %.S,%.s,$(K_SRC_H))
K_OBJ_I = $(patsubst %.c,%.o,$(K_SRC_I))
K_SYMS = kallsyms/kallsyms
# User thread variable ==========================================================================
U_SRC = $(wildcard ./user/*.c) # Need to make sure init.c is the first entry
U_OBJ = $(patsubst %.c,%.o,$(U_SRC))
# Test thread variable ==========================================================================
T_SRC = $(wildcard ./test/*.c) # Need to make sure init.c is the first entry
T_OBJ = $(patsubst %.c,%.o,$(T_SRC))
# UEFI variable =================================================================================
EDKDIR = ./edk2
OVMF_DSC = OvmfPkg/OvmfPkgX64.dsc
TRGOBJ = Kernel.bin test.bin user.bin
BFLAGS = -a X64 -t GCC5
# Dump info variable ============================================================================
DUMPOBJ = system user.sys test.sys
DUMPRST = $(patsubst %,%.s,$(DUMPOBJ))
# QEMU variable =================================================================================
QFLAGS = -machine q35 -cpu EPYC -accel kvm -smp 4,cores=2,threads=2,sockets=1 -m 1G -device nvme,drive=D22,serial=1234 -serial stdio -net none
# Link ==========================================================================================
LFLAGS = -b elf64-x86-64
# Local variable ================================================================================
run:CFLAGS = -mcmodel=large -fno-builtin -fno-stack-protector -m64
run:QMEUFL = $(QFLAGS)
dbg:CFLAGS = -ggdb3 -mcmodel=large -fno-builtin -fno-stack-protector -m64
dbg:QMEUFL = -s -S $(QFLAGS)
# Kernel compile ================================================================================
$(K_OBJ_C):%.o:%.c
gcc $(CFLAGS) -c $< -o $@
$(K_OBJ_I):%.o:%.c
gcc $(CFLAGS) -mgeneral-regs-only -c $< -o $@
$(K_PRC_H):%.s:%.S
gcc -E $< > $@
$(K_OBJ_H):%.o:%.s
as --64 -o $@ $<
system_0: $(K_OBJ_H) $(K_OBJ_C) $(K_OBJ_I)
ld $(LFLAGS) -z muldefs -o $@ $^ -T ./Kernel/Kernel.lds
$(K_SYMS): $(K_SYMS).c
gcc -o $@ $<
$(K_SYMS).S: system_0 $(K_SYMS)
nm -n $(word 1,$^) | $(word 2,$^) > $@
$(K_SYMS).o: $(K_SYMS).S
gcc -c $< -o $@
system: $(K_OBJ_H) $(K_OBJ_C) $(K_OBJ_I) $(K_SYMS).o
ld $(LFLAGS) -z muldefs -o $@ $^ -T ./Kernel/Kernel.lds
Kernel.bin: system
objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O binary $^ $@
# User compile ==================================================================================
$(U_OBJ):%.o:%.c
gcc $(CFLAGS) -c $< -o $@
user.sys: $(U_OBJ)
ld $(LFLAGS) -z muldefs -o $@ $^ -T user/user.ids
user.bin: user.sys
objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O binary $^ $@
# Test compile ==================================================================================
$(T_OBJ):%.o:%.c
gcc $(CFLAGS) -c $< -o $@
test.sys: $(T_OBJ)
ld $(LFLAGS) -z muldefs -o $@ $^ -T test/test.ids
test.bin: test.sys
objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O binary $^ $@
# UEFI compile ==================================================================================
# UEFI enviroment need to be set up each time:
# 1, cd edk2
# 2, source edksetup.sh BaseTools
# 3, If BaseTools is built, `make -C ./edk2/BaseTools clean`
$(EDKDIR)/BaseTools/Source/C/bin:
make -C ./edk2/BaseTools
$(EDKDIR)/Build/My/DEBUG_GCC5/X64/BootX64.efi: MyPkg/BootX64/BootX64.c MyPkg/BootX64/BootX64.h MyPkg/BootX64/BootX64.inf MyPkg/MyPkg.dec MyPkg/MyPkg.dsc $(EDKDIR)/Build/OvmfX64/DEBUG_GCC5/FV/OVMF.fd
-rm -rf $(EDKDIR)/MyPkg
cp -rf MyPkg $(EDKDIR)
cd $(EDKDIR) && source ./edksetup.sh && build $(BFLAGS) -p $(word 5,$^) -m $(word 3,$^)
$(EDKDIR)/Build/OvmfX64/DEBUG_GCC5/FV/OVMF.fd:
cd $(EDKDIR) && build $(BFLAGS) -p $(OVMF_DSC)
# Make disk =====================================================================================
mnt:
mkdir mnt
hda.disk: $(EDKDIR)/Build/My/DEBUG_GCC5/X64/BootX64.efi $(TRGOBJ) mnt
-rm hda.disk
qemu-img create hda.disk 1G
echo -e "g\nn\n\n\n+250M\nt\n1\nn\n\n\n\nw" | fdisk hda.disk
echo $(PW) | sudo -S losetup --offset 1048576 --sizelimit 262144000 /dev/loop0 hda.disk
sudo mkfs.vfat -F 32 /dev/loop0 -I
sudo losetup -d /dev/loop0
sudo losetup --offset 263192576 --sizelimit 810532352 /dev/loop0 hda.disk
sudo mkfs.vfat /dev/loop0 -I
sudo losetup -d /dev/loop0
# Prevent mount before delete complete, sleep 0.2s
sleep 0.2
sudo mount hda.disk mnt -t vfat -o loop,offset=1048576
sudo mkdir mnt/EFI/
sudo mkdir mnt/EFI/Boot
sudo cp $(word 1,$^) mnt/EFI/Boot
sleep 0.5
sudo umount mnt
sudo mount hda.disk mnt -t vfat -o loop,offset=263192576
sudo mkdir mnt/abc
sudo cp test.txt mnt/abc
sudo cp $(TRGOBJ) KEYBOARD.DEV mnt
sleep 0.5
sudo umount mnt
nvme.disk:
-rm nvme.disk
qemu-img create nvme.disk 1G
run: hda.disk $(EDKDIR)/Build/OvmfX64/DEBUG_GCC5/FV/OVMF.fd nvme.disk
qemu-system-x86_64 $(QMEUFL) -hda $(word 1,$^) -bios $(word 2,$^) -drive file=$(word 3,$^),if=none,id=D22
dbg: hda.disk $(EDKDIR)/Build/OvmfX64/DEBUG_GCC5/FV/OVMF.fd nvme.disk
# start: gdb
# connecting: target remote: 1234
qemu-system-x86_64 $(QMEUFL) -hda $(word 1,$^) -bios $(word 2,$^) -drive file=$(word 3,$^),if=none,id=D22
clean:
-rm $(K_OBJ_C) $(K_OBJ_I) $(K_PRC_H) $(K_OBJ_H) $(U_OBJ) $(T_OBJ)
-rm $(K_SYMS) $(K_SYMS).S $(K_SYMS).o
-rm system system_0 user.sys test.sys
-rm Kernel.bin user.bin test.bin
-rm *.disk
-rm $(DUMPRST)
-rm -rf $(EDKDIR)/MyPkg
dump: $(DUMPOBJ)
objdump -D $(word 1,$^) > $(word 1,$^).s
objdump -D $(word 2,$^) > $(word 2,$^).s
objdump -D $(word 3,$^) > $(word 3,$^).s
PHONY += clean dump run dbg
# GitHub ========================================================================================
sub_init:
git submodule update --init --recursive
sub_pull:
cd edk2 && git pull --recurse-submodules=yes origin master
commit: clean
git add -A
@echo "Please type in commit comment: "; \
read comment; \
git commit -m"$$comment"
sync: commit
git push -u origin master
reset_hard:
git fetch --all
git reset --hard origin/master
git pull
PHONY += sub_init sub_pull commit sync
.PHONY: $(PHONY)