Repository navigation
Expand file tree
/
Copy pathGNUmakefile
More file actions
225 lines (196 loc) · 7.32 KB
/
Copy pathGNUmakefile
File metadata and controls
225 lines (196 loc) · 7.32 KB
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
# Nuke built-in rules.
.SUFFIXES:
# Delete the target of a failed recipe.
.DELETE_ON_ERROR:
# Target architecture to build for. Default to x86_64.
ARCH := x86_64
# Default user QEMU flags. These are appended to the QEMU command calls.
QEMUFLAGS := -m 2G
# Per-architecture user controllable QEMU flags.
ifeq ($(ARCH),x86_64)
QEMU_MACHINE := -M q35
# Uncomment this line and comment out/remove the xHCI/USB keyboard/mouse
# block below if the kernel does not support xHCI/USB and wants legacy
# PS/2 keyboard and mouse instead.
#QEMU_DEVICES :=
QEMU_DEVICES := \
-device qemu-xhci \
-device usb-kbd \
-device usb-mouse
QEMU_CPU :=
else
QEMU_MACHINE := -M virt
ifeq ($(ARCH),aarch64)
QEMU_CPU := -cpu cortex-a72
endif
ifeq ($(ARCH),riscv64)
QEMU_CPU := -cpu rv64
endif
ifeq ($(ARCH),loongarch64)
QEMU_CPU := -cpu la464
endif
QEMU_DEVICES := \
-device ramfb \
-device qemu-xhci \
-device usb-kbd \
-device usb-mouse
endif
override QEMUFLAGS := \
$(QEMU_MACHINE) \
$(QEMU_CPU) \
$(QEMU_DEVICES) \
$(QEMUFLAGS)
override QEMU_UEFI_FW := \
-drive if=pflash,unit=0,format=raw,file=edk2-ovmf-bins/ovmf-code-$(ARCH).fd,readonly=on
# Internal per-architecture flags for xorriso and sgdisk calls and for which EFI binaries to use.
ifeq ($(ARCH),x86_64)
override LIMINE_EFI := BOOTX64.EFI BOOTIA32.EFI
override XORRISO_BIOS_FLAGS := \
-b boot/limine/limine-bios-cd.bin -no-emul-boot -boot-load-size 4 -boot-info-table
override SGDISK_MBR_FLAGS := -m 1
else
ifeq ($(ARCH),aarch64)
override LIMINE_EFI := BOOTAA64.EFI
endif
ifeq ($(ARCH),riscv64)
override LIMINE_EFI := BOOTRISCV64.EFI
endif
ifeq ($(ARCH),loongarch64)
override LIMINE_EFI := BOOTLOONGARCH64.EFI
endif
override XORRISO_BIOS_FLAGS :=
override SGDISK_MBR_FLAGS :=
endif
override IMAGE_NAME := template-$(ARCH)
# User controllable size of the HDD image, in MiB.
HDD_SIZE := 64
# Internal HDD geometry that should not be changed by the user. Older mtools
# require one; 64 heads of 32 sectors make a cylinder exactly 1 MiB in size.
override HDD_HEADS := 64
override HDD_SECTORS_PER_TRACK := 32
override HDD_CYLINDER_SECTORS := $(shell echo $$(( $(HDD_HEADS) * $(HDD_SECTORS_PER_TRACK) )))
# Internal HDD partition layout that should not be changed by the user. sgdisk
# lays the partition out as GPT, even where it then converts the table to MBR,
# so the first and last cylinders are left to the GPT structures.
override HDD_PART_START := $(HDD_CYLINDER_SECTORS)
override HDD_PART_SECTORS := $(shell echo $$(( ($(HDD_SIZE) - 2) * $(HDD_CYLINDER_SECTORS) )))
override HDD_PART_END := $(shell echo $$(( $(HDD_PART_START) + $(HDD_PART_SECTORS) - 1 )))
override HDD_PART_OFFSET := $(shell echo $$(( $(HDD_PART_START) * 512 )))
# Toolchain for building the 'limine' executable for the host.
HOST_CC := cc
HOST_CFLAGS := -g -O2 -pipe
HOST_CPPFLAGS :=
HOST_LDFLAGS :=
HOST_LIBS :=
.PHONY: all
all: $(IMAGE_NAME).iso
.PHONY: all-hdd
all-hdd: $(IMAGE_NAME).hdd
.PHONY: run
run: edk2-ovmf-bins $(IMAGE_NAME).iso
qemu-system-$(ARCH) \
-cdrom $(IMAGE_NAME).iso \
$(QEMUFLAGS) \
$(QEMU_UEFI_FW)
.PHONY: run-hdd
run-hdd: edk2-ovmf-bins $(IMAGE_NAME).hdd
qemu-system-$(ARCH) \
-hda $(IMAGE_NAME).hdd \
$(QEMUFLAGS) \
$(QEMU_UEFI_FW)
ifeq ($(ARCH),x86_64)
.PHONY: run-bios
run-bios: $(IMAGE_NAME).iso
qemu-system-$(ARCH) \
-cdrom $(IMAGE_NAME).iso \
-boot d \
$(QEMUFLAGS)
.PHONY: run-hdd-bios
run-hdd-bios: $(IMAGE_NAME).hdd
qemu-system-$(ARCH) \
-hda $(IMAGE_NAME).hdd \
$(QEMUFLAGS)
endif
.INTERMEDIATE: edk2-ovmf-bins.tar.gz
edk2-ovmf-bins.tar.gz:
curl -fL -o $@ https://github.com/osdev0/edk2-ovmf-stable-bins/releases/latest/download/edk2-ovmf-bins.tar.gz
edk2-ovmf-bins: edk2-ovmf-bins.tar.gz
rm -rf edk2-ovmf-bins
gunzip < edk2-ovmf-bins.tar.gz | tar -xf -
.INTERMEDIATE: limine-binary.tar.gz
limine-binary.tar.gz:
curl -fL -o $@ https://github.com/Limine-Bootloader/Limine/releases/latest/download/limine-binary.tar.gz
# Date the source by the tarball, not the release inside it, so that what
# is made from it is remade whenever it is extracted. Not by the clock, as
# some touch use a finer one than file writes, dating it after what follows.
limine-binary/limine.c: limine-binary.tar.gz
rm -rf limine-binary
gunzip < limine-binary.tar.gz | tar -xf -
touch -r limine-binary.tar.gz $@
# The utility depends on records of the variables it is built with, named
# after them, which are compared here and remade when they differ. They are
# made after extracting, which removes them.
override VARSFILES := $(addprefix limine-binary/,HOST_CC HOST_CFLAGS HOST_CPPFLAGS HOST_LDFLAGS HOST_LIBS)
override CHANGED_VARSFILES := $(foreach f,$(VARSFILES),$(shell printf '%s\n' '$(subst ','\'',$($(notdir $(f))))' | cmp -s - $(f) 2>/dev/null || echo $(f)))
.PHONY: FORCE
FORCE:
$(CHANGED_VARSFILES): FORCE
$(VARSFILES): limine-binary/limine.c
@printf '%s\n' '$(subst ','\'',$($(notdir $@)))' > $@
# Remove the utility first, as its makefile does not track the toolchain.
limine-binary/limine: limine-binary/limine.c $(VARSFILES)
rm -f $@
$(MAKE) -C limine-binary \
CC="$(HOST_CC)" \
CFLAGS="$(HOST_CFLAGS)" \
CPPFLAGS="$(HOST_CPPFLAGS)" \
LDFLAGS="$(HOST_LDFLAGS)" \
LIBS="$(HOST_LIBS)"
kernel/.deps-obtained:
./kernel/get-deps
.PHONY: kernel
kernel: kernel/.deps-obtained
$(MAKE) -C kernel
$(IMAGE_NAME).iso: limine-binary/limine kernel
rm -rf iso_root
mkdir -p iso_root/boot
cp kernel/bin-$(ARCH)/kernel iso_root/boot/
mkdir -p iso_root/boot/limine
cp limine.conf limine-binary/limine-uefi-cd.bin iso_root/boot/limine/
ifeq ($(ARCH),x86_64)
cp limine-binary/limine-bios.sys limine-binary/limine-bios-cd.bin iso_root/boot/limine/
endif
mkdir -p iso_root/EFI/BOOT
cp $(addprefix limine-binary/,$(LIMINE_EFI)) iso_root/EFI/BOOT/
xorriso -as mkisofs -R -r -J $(XORRISO_BIOS_FLAGS) \
-hfsplus -apm-block-size 2048 \
--efi-boot boot/limine/limine-uefi-cd.bin \
-efi-boot-part --efi-boot-image --protective-msdos-label \
iso_root -o $(IMAGE_NAME).iso
ifeq ($(ARCH),x86_64)
./limine-binary/limine bios-install $(IMAGE_NAME).iso
endif
rm -rf iso_root
$(IMAGE_NAME).hdd: limine-binary/limine kernel
rm -f $(IMAGE_NAME).hdd
dd if=/dev/null bs=1024k seek=$(HDD_SIZE) of=$(IMAGE_NAME).hdd
PATH=$$PATH:/usr/sbin:/sbin sgdisk $(IMAGE_NAME).hdd -n 1:$(HDD_PART_START):$(HDD_PART_END) -t 1:ef00 $(SGDISK_MBR_FLAGS)
ifeq ($(ARCH),x86_64)
./limine-binary/limine bios-install $(IMAGE_NAME).hdd
endif
mformat -i $(IMAGE_NAME).hdd@@$(HDD_PART_OFFSET) -T $(HDD_PART_SECTORS) -h $(HDD_HEADS) -s $(HDD_SECTORS_PER_TRACK) ::
mmd -i $(IMAGE_NAME).hdd@@$(HDD_PART_OFFSET) ::/EFI ::/EFI/BOOT ::/boot ::/boot/limine
mcopy -i $(IMAGE_NAME).hdd@@$(HDD_PART_OFFSET) kernel/bin-$(ARCH)/kernel ::/boot
mcopy -i $(IMAGE_NAME).hdd@@$(HDD_PART_OFFSET) limine.conf ::/boot/limine
ifeq ($(ARCH),x86_64)
mcopy -i $(IMAGE_NAME).hdd@@$(HDD_PART_OFFSET) limine-binary/limine-bios.sys ::/boot/limine
endif
mcopy -i $(IMAGE_NAME).hdd@@$(HDD_PART_OFFSET) $(addprefix limine-binary/,$(LIMINE_EFI)) ::/EFI/BOOT
.PHONY: clean
clean:
$(MAKE) -C kernel clean
rm -rf iso_root $(IMAGE_NAME).iso $(IMAGE_NAME).hdd
.PHONY: distclean
distclean:
$(MAKE) -C kernel distclean
rm -rf iso_root *.iso *.hdd limine-binary limine-binary.tar.gz edk2-ovmf-bins edk2-ovmf-bins.tar.gz