-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
144 lines (114 loc) · 4.34 KB
/
Makefile
File metadata and controls
144 lines (114 loc) · 4.34 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
# I AM NOT RESPONSIBLE FOR DATA LOSS, USE THIS FILE AT YOUR OWN RISK
# (the only targets with possible risk are the ones that start with "write",
# because these use `dd` to directly write to what is supposed to be a floppy
# disk drive - I have tried to take caution using the HEAD variable below to
# make sure dd only ever writes to a specific USB device I have, but I cannot
# ever guarantee this is safe)
# ======================================
# Things the user should probably change
# ======================================
# Device path to floppy disk drive
DEV := /dev/sdc
# Desired mount location (also used when creating the disk image)
DEST := /mnt
# First 2 lines that fdisk -l $(DEV) outputs - basically whatever will let you
# be certain you're writing to the floppy drive and not something else because
# this Makefile WILL DESTROY THE MBR OF $(DEV)
HEAD := 'Disk $(DEV): 720 KiB, 737280 bytes, 1440 sectors Disk model: USB UF000x'
# Replace this with doas, or whatever program you wish
RUN_AS_ROOT := sudo
# ==========================================
# Things the user probably should NOT change
# ==========================================
SOURCE_DIR := src
SOURCES := $(shell find $(SOURCE_DIR) -type f -name '*.s')
# You shouldn't need to modify anything below here normally - maybe L2 if you
# want to automate populating the disk with custom .bin files
# Disk components
L1 := bootloader fat fat root data
# Files that go on the disk image
L2 := kernel moss basici
# Anything else
L3 := $(filter-out $(L1) $(L2),$(SOURCES:$(SOURCE_DIR)/%.s=%))
# Created files
IMAGE := boot.bin
TEMPLATE := bins/blank-disk.bin
DISK := $(L1:%=bins/%.bin)
FILE := $(L2:%=bins/%.bin)
MISC := $(L3:%=bins/%.bin)
# No need to call sudo if we're already root. This helps prevent that.
ifeq ($(shell echo $$EUID),0)
sudo = $(1)
else
sudo = $(RUN_AS_ROOT) $(1)
endif
# =====================
# Main callable targets
# =====================
# Fully formed bootable floppy disk image - keep template as first prerequisite
$(IMAGE): $(TEMPLATE) $(FILE)
@if test $< -nt $@; then echo copying template; cp -f $< $@; fi
$(call sudo,mount) $@ $(DEST)
@if ! $(MAKE) copy-bins --no-print-directory SAFE=yes; then $(call sudo,umount) $(DEST); exit 1; fi
$(call sudo,umount) $(DEST)
run:
@if ! [ -f $(IMAGE) ]; then $(MAKE) $(IMAGE) --no-print-directory; fi
qemu-system-x86_64 -fda $(IMAGE)
clean:
rm -f $(IMAGE) $(TEMPLATE) $(DISK) $(FILE)
.PHONY: run clean
# ================================================
# Real device writing targets (WARNING: uses `dd`)
# ================================================
SAFE :=
verification_command = if ! $(call sudo,fdisk) -l $(DEV) | tr '\n' ' ' | grep $(HEAD); then echo 'Could not verify $(DEV) to be desired drive.'; exit 1; fi
# Write to a REAL floppy disk
write:
@$(verification_command)
@$(MAKE) write-bootloader --no-print-directory SAFE=yes
@$(MAKE) write-files --no-print-directory SAFE=yes
# DESTROYS MBR :)
write-bootloader: bins/bootloader.bin
ifeq ($(SAFE),yes)
$(call sudo,dd) bs=512 if=$< of=$(DEV)
else
@$(verification_command)
@$(MAKE) $@ --no-print-directory SAFE=yes
endif
# simply mounts and copies files
write-files: $(FILE)
ifeq ($(SAFE),yes)
$(call sudo,mount) $(DEV) $(DEST)
@$(MAKE) copy-bins --no-print-directory
$(call sudo,umount) $(DEST)
else
@$(verification_command)
@$(MAKE) $@ --no-print-directory SAFE=yes
endif
define execute
$(1)
endef
# Don't call this directly...
copy-bins:
ifeq ($(SAFE),yes)
@$(foreach f,$(L2),$(call execute,@$(MAKE) $(DEST)/$(shell echo $(f) | tr '[:lower:]' '[:upper:]').BIN))
else
@echo PLEASE do not call this target directly :\)
@exit 2
endif
.PHONY: write write-bootloader write-files copy-bins
# ===================
# File creation rules
# ===================
$(TEMPLATE): $(DISK)
cat $+ > $@
define bin_template =
bins/$(1).bin: $(SOURCE_DIR)/$(1).s $(foreach f,$(shell grep '%include' $(SOURCE_DIR)/$(1).s | awk -F'"' '{print $$2}'),$(SOURCE_DIR)/$(f))
@mkdir -p $$(dir $$@)
nasm -f bin -I $(SOURCE_DIR) $$< -o $$@
$(DEST)/$(shell echo $(notdir $(basename $(1))) | cut -d ' ' -f 1 | cut -c -8 | tr '[:lower:]' '[:upper:]').BIN: bins/$(1).bin
$(call sudo,cp) -f $$< $$@
endef
$(foreach f,$(sort $(L1) $(L2) $(L3)),$(eval $(call bin_template,$(f))))
#$(DEST)/%.BIN:
# $(call sudo,cp) --update=older bins/$(shell echo $(notdir $(basename $@)) | tr '[:upper:]' '[:lower:]').bin $@