-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile.windows
349 lines (287 loc) · 14 KB
/
Makefile.windows
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# Copyright (c) 2017, Cyberhaven
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
###############################################################################
WINDOWS_UPDATES :=
include $(SRC)/Makefile.windows.apps
include $(SRC)/Makefile.windows.updates32
include $(SRC)/Makefile.windows.updates64
windows_clean:
rm -rf $(WINDOWS_APPS_DIR)
###############################################################################
# This macro instantiates rules to prepare the Windows XP install ISO
#$1: image name
#$2: iso name
define PREPARE_WINXP_ISO
$(TMPDIR)/$1/install_files.iso: $2 $(SRC)/Windows/Winnt.sif.template | $(TMPDIR)/$1
@# Windows XP doesn't seem to pick up Winnt.sif from floppy, so we put it
@# on the installation ISO
@#
@# *** Prepare install_files.iso image
rm -Rf $(TMPDIR)/$1/install_files && mkdir -p $(TMPDIR)/$1/install_files
@# Extract intall CD
7z x $2 -o$(TMPDIR)/$1/install_files
chmod -R u+w $(TMPDIR)/$1/install_files
mkdir $(TMPDIR)/$1/install_files/cdboot
geteltorito -o $(TMPDIR)/$1/install_files/cdboot/boot.bin $2
rsync -av $(SRC)/Windows/install_scripts $(TMPDIR)/$1/install_files/
$(SRC)/scripts/compile_template.py \
-t $(SRC)/Windows/Winnt.sif.template \
-o $(TMPDIR)/$1/install_files/$(if $(findstring x86_64,$1),AMD64,I386)/winnt.sif \
-n $1 -d $(SRC)/images.json
@# WindowsXP also needs to be rebooted after first login
printf "timeout 60\nshutdown /r /t 0" > $(TMPDIR)/$1/install_files/s2e_startup.bat
@#
genisoimage -b cdboot/boot.bin -no-emul-boot -boot-load-seg 1984 -boot-load-size 4 \
--iso-level 2 -JlDN -joliet-long -relaxed-filenames -V Custom -f \
-o $$@ $(TMPDIR)/$1/install_files
$(TMPDIR)/$1/boot.iso: $(TMPDIR)/$1/install_files.iso
cd $(TMPDIR)/$1 && ln -sf install_files.iso boot.iso
endef
###############################################################################
# This macro instantiates rules to prepare Windows installation files (>XP)
#$1: image name
#$2: iso name
define PREPARE_WIN_ISO
$(TMPDIR)/$1/install_files.iso: $(SRC)/Windows/Autounattend.xml.template | $(TMPDIR)/$1
@#
@# *** Prepare install_files.iso image
rm -Rf $(TMPDIR)/$1/install_files && mkdir -p $(TMPDIR)/$1/install_files
$(SRC)/scripts/compile_template.py -t $(SRC)/Windows/Autounattend.xml.template \
-o $(TMPDIR)/$1/install_files/Autounattend.xml \
-n $1 -d $(SRC)/images.json
genisoimage --iso-level 2 -flDJR -o $$@ $(TMPDIR)/$1/install_files
$(TMPDIR)/$1/boot.iso: $2 | $(TMPDIR)/$1
cd $(TMPDIR)/$1 && ln -sf $$< boot.iso
endef
###############################################################################
# This macro invokes QEMU for initial Windows installation
# Note: windows xp install may get stuck because of the NIC, so we disable it here
# $1: image name
# $2: disk image name
RUN_INITIAL_SETUP = $(QEMU64) \
-no-reboot $(GRAPHICS) $(QEMU_KVM) -m 2G -net none\
$(if $(findstring xp,$1),,-net nic,model=$(call NIC,$(1))) \
$(call TO_VAR_CPU,$(1)) \
$(call QEMU_HD,0,$(2)) \
$(call INITIAL_CD_DRIVES,$(1)) \
-serial file:/dev/null \
-periodic-screenshot $(3)
INITIAL_CD_DRIVES = \
$(call QEMU_CD,2,$(TMPDIR)/$(1)/boot.iso) \
$(call QEMU_CD,3,$(TMPDIR)/$(1)/install_files.iso)
GUEST_TOOLS = $(wildcard $(call GET_GUEST_TOOLS,$(1))/*.sys) \
$(wildcard $(call GET_GUEST_TOOLS,$(1))/*.exe) \
$(wildcard $(call GET_GUEST_TOOLS,$(1))/*.inf)
# $1: image name
# $2: disk image name
# $3: cd drive d:
# $4: cd drive e:
# $5: serial and screenshot prefix
RUN_QEMU_KVM = \
$(QEMU64) -no-reboot $(GRAPHICS) $(QEMU_KVM) -m 2G \
$(call TO_VAR_NIC,$(1)) \
$(call TO_VAR_CPU,$(1)) \
$(call QEMU_HD,0,$(2)) \
$(call QEMU_CD,2,$(3)) \
$(call QEMU_CD,3,$(4)) \
-serial file:$(5)_serial.txt \
-smp cores=4,threads=1,sockets=1 \
-periodic-screenshot $(5)_screenshots
# This is a hack to inject guest tools into the 07_install_software.iso image.
# TODO: figure out how to do it better
GUEST_TOOLS_DEPS = $(if $(findstring 07_install_software,$1),$(call GUEST_TOOLS,$2))
define CREATE_UPDATE_CD
$(TMPDIR)/$1/$2.iso: $(shell find $(SRC)/Windows/install_scripts/$2) $(call GUEST_TOOLS_DEPS,$2,$1) | $(TMPDIR)/$1
genisoimage --iso-level 2 -flDJR -o $$@ $(SRC)/Windows/install_scripts/$2/ $(call GUEST_TOOLS_DEPS,$2,$1)
endef
# $1: image name
# $2: cd with updates (also used as target image name)
# $3: disk image name that we depend on
# $4: message
ifeq ($(DEBUG_INTERMEDIATE_RULES), 1)
GET_FINAL_IMAGE_DEP = $(TMPDIR)/$1/$2
define APPLY_UPDATES
$(TMPDIR)/$1/image.raw.$2: $(TMPDIR)/$1/image.raw.$3 $(TMPDIR)/$1/00_software.iso $(TMPDIR)/$1/$2.iso | $(OUTDIR)/$1
$(call INFO_MSG,[$1] $4)
cp --reflink=auto $$< $$@
rm -rf $$@_screenshots && mkdir -p $$@_screenshots
$(call RUN_QEMU_KVM,$1,$$@,$(TMPDIR)/$1/00_software.iso,$(TMPDIR)/$1/$2.iso,$$@)
endef
else
GET_FINAL_IMAGE_DEP = $(TMPDIR)/$1/$2.stamp
GET_DEP_STAMP_SUFFX = $(if $(findstring 1.fresh,$1),,.stamp)
GET_ORDER_ONLY = $(if $(findstring 1.fresh,$1),$1,)
GET_UPD_IMG = $(if $(findstring 1.fresh,$1),,$1)
define APPLY_UPDATES
$(TMPDIR)/$1/image.raw.$2.stamp: $(call GET_UPD_IMG,$(TMPDIR)/$1/image.raw.$3$(call GET_DEP_STAMP_SUFFX,$3)) \
$(TMPDIR)/$1/00_software.iso $(TMPDIR)/$1/$2.iso | $(OUTDIR)/$1 $(call GET_ORDER_ONLY,$(TMPDIR)/$1/image.raw.$3)
$(call INFO_MSG,[$1] $4)
rm -rf $(TMPDIR)/$1/image.raw.$2_screenshots && mkdir -p $(TMPDIR)/$1/image.raw.$2_screenshots
$(call RUN_QEMU_KVM,$1,$(TMPDIR)/$1/image.raw.1.fresh,$(TMPDIR)/$1/00_software.iso,$(TMPDIR)/$1/$2.iso,$(TMPDIR)/$1/image.raw.$2)
touch $$@
endef
endif
# TODO: find these folders instead of hard-coding them?
UPDATE_CDS := 01_post_install 02_updates1 03_updates2 04_ie 05_dotnet
SOFTWARE_CDS := 07_install_software 10_setup_finalize 20_tcg_reboot
ALL_CDS := $(UPDATE_CDS) $(SOFTWARE_CDS)
UPDATE_CDS_PATHS = $(addprefix $(TMPDIR)/$1/,$(addsuffix .iso,$(UPDATE_CDS)))
SOFTWARE_CDS_PATHS = $(addprefix $(TMPDIR)/$1/,$(addsuffix .iso,$(SOFTWARE_CDS)))
ALL_CDS_PATHS = $(UPDATE_CDS_PATHS) $(SOFTWARE_CDS_PATHS)
###############################################################################
### Provisioning the initial image
#$1: image name
#$2: iso name
define TEMPLATE_BASE_WINDOWS_IMAGE
# These variables cache various values from images.json.
# It is expensive to call shell get_setting.py... so we minimize it here.
# The variable's name is constructed like this: image_name_{CPU|NIC...}:=...
$(eval $(call TO_VAR,$1,NIC):=$(call NIC,$1))
$(eval $(call TO_VAR,$1,CPU):=$(call CPU,$1))
$(eval $(call TO_VAR,$1,DISK_SIZE):=$(call DISK_SIZE,$1))
$(eval $(call TO_VAR,$1,OS_VERSION):=$(call OS_VERSION,$1))
$(eval $(call TO_VAR,$1,SNAPSHOT_SIZE):=$(call SNAPSHOT_SIZE,$1))
$(OUTDIR)/$1 $(TMPDIR)/$1:
mkdir -p "$$@"
# Prepare the boot CD
$(if $(findstring xp,$1), \
$(eval $(call PREPARE_WINXP_ISO,$1,$2)), \
$(eval $(call PREPARE_WIN_ISO,$1,$2)) \
)
# Prepare CDs for each installation step
$(foreach cd,$(ALL_CDS), \
$(eval $(call CREATE_UPDATE_CD,$1,$(cd))) \
)
# Create CD with all extra software. This CD does not have any installation scripts and is mounted
# on most installations steps.
$(TMPDIR)/$1/00_software.iso: $(WIN_APPS_PATH) $(WINDOWS_UPDATES) | $(TMPDIR)/$1
genisoimage --iso-level 2 -flDJR -o $$@ $(WINDOWS_APPS_DIR)
# The tmp image contains a fresh install of the system, including any Windows updates
$(TMPDIR)/$1/image.raw.1.fresh: $(TMPDIR)/$1/boot.iso $(TMPDIR)/$1/install_files.iso
rm -rf $(TMPDIR)/$1/initial_install_screenshots1 && mkdir -p $(TMPDIR)/$1/initial_install_screenshots1
rm -rf $(TMPDIR)/$1/initial_install_screenshots2 && mkdir -p $(TMPDIR)/$1/initial_install_screenshots2
@#
@# *** Create empty disk image
$(QEMU_IMG) create -f raw $$@ $(call TO_VAR_DISK_SIZE,$1)
@#
@# *** Run the installation in QEMU
$(call INFO_MSG,[$1] Running initial setup...)
$(call RUN_INITIAL_SETUP,$1,$$@,$(TMPDIR)/$1/initial_install_screenshots1)
$(call INFO_MSG,[$1] Completing installation...)
$(call RUN_INITIAL_SETUP,$1,$$@,$(TMPDIR)/$1/initial_install_screenshots2)
$(eval $(call APPLY_UPDATES,$1,01_post_install,1.fresh,"Applying post-install config"))
$(if $(findstring 7sp1,$1), \
$(eval $(call APPLY_UPDATES,$1,02_updates1,01_post_install,"Applying updates 1")) \
$(eval $(call APPLY_UPDATES,$1,03_updates2,02_updates1,"Applying updates 2")) \
$(eval $(call APPLY_UPDATES,$1,04_ie,03_updates2,"Installing IE 11")) \
)
$(eval $(call APPLY_UPDATES,$1,05_dotnet,$(if $(findstring 7sp1,$1),04_ie,01_post_install),"Installing .net"))
$(eval $(call APPLY_UPDATES,$1,07_install_software,05_dotnet,"Installing additional software"))
$(eval $(call APPLY_UPDATES,$1,10_setup_finalize,07_install_software,"Finalizing installation"))
# Reboot in TCG mode
$(OUTDIR)/$1/image.raw.s2e: $(call GET_FINAL_IMAGE_DEP,$1,image.raw.10_setup_finalize) $(TMPDIR)/$1/20_tcg_reboot.iso
if [ "x$(DEBUG_INTERMEDIATE_RULES)" = "x" ]; then \
cp --reflink=auto $(TMPDIR)/$1/image.raw.1.fresh $$@; \
else \
cp --reflink=auto $$< $$@; \
fi
rm -rf $(TMPDIR)/$1/tcg_screenshots && mkdir -p $(TMPDIR)/$1/tcg_screenshots
if [ "x$(findstring 8.1,$(call TO_VAR_OS_VERSION,$1))" != "x" ]; then \
virt-win-reg $(OUTDIR)/$1/image.raw.s2e --merge $(SRC)/Windows/install_scripts/win8_desktop.reg; \
fi
@# *** Reboot in TCG mode to install the CPU driver
$(call INFO_MSG,[$1] Rebooting in TCG mode...)
LD_PRELOAD=$(call GETLIBS2E,$1) $(call GETQEMU,$1) \
-enable-kvm -no-reboot $(GRAPHICS) $(QEMU_KVM) -m 2G \
$(call TO_VAR_NIC,$1) \
$(call TO_VAR_CPU,$1) \
$(call QEMU_HD,0,$$@) \
-cdrom $(TMPDIR)/$1/20_tcg_reboot.iso \
-serial file:$(TMPDIR)/$1/tcg_serial.txt \
-periodic-screenshot $(TMPDIR)/$1/tcg_screenshots
@# This must be done here because the registry keys are protected in the guest
$(call INFO_MSG,[$1] Disabling Windows Defender...)
if [ -z "$(findstring xp,$(call TO_VAR_OS_VERSION,$1))" ]; then \
virt-win-reg $(OUTDIR)/$1/image.raw.s2e --merge $(SRC)/Windows/install_scripts/disable_windefend.reg; \
fi
$(call OK_MSG,[$1] Base installation completed.)
# This rule extracts all executable files from the guest image into the guestfs folder.
# This is useful for S2E plugins that need access to the binaries in order to extract
# information from them (e.g., import or export tables).
$(OUTDIR)/$1/guestfs: $(OUTDIR)/$1/image.raw.s2e
rm -Rf $$@ && mkdir -p $$@
$(call INFO_MSG,[$1] Extracting guestfs...)
@# First copy out all the files from the image
@# Unmount any previous folders (e.g., if interrupted)
-fusermount -u $(OUTDIR)/$1/imagefs
mkdir -p $(OUTDIR)/$1/imagefs
cd $(OUTDIR)/$1 && trap 'fusermount -u imagefs && rmdir imagefs' EXIT && \
guestmount --ro -i -a image.raw.s2e imagefs && \
$(SRC)/scripts/filter_exe.py imagefs guestfs
@# Convert the file names to lower case. This makes sure that the case
@# is uniform across all files and plugins can easily access them.
$(SRC)/scripts/lowercase.py "$$@"
$(call OK_MSG,[$1] Guestfs extraction completed.)
$(OUTDIR)/$1/image.raw.s2e.ready: $(OUTDIR)/$1/image.raw.s2e
rm -rf $(TMPDIR)/$1/ready_screenshots && mkdir -p $(TMPDIR)/$1/ready_screenshots
@# Unmount any previous folders (e.g., if interrupted)
-fusermount -u $(OUTDIR)/$1/imagefs
@# *** Create ready snapshot
$(call INFO_MSG,[$1] Creating ready snapshot...)
LD_PRELOAD=$(call GETLIBS2E,$1) $(call GETQEMU,$1) \
-enable-kvm -no-reboot $(GRAPHICS) $(QEMU_KVM) -m $(call TO_VAR_SNAPSHOT_SIZE,$1) \
$(call TO_VAR_NIC,$1) \
$(call TO_VAR_CPU,$1) \
$(call QEMU_HD_S2E,0,$$<) \
-enable-serial-commands \
-serial file:$(TMPDIR)/$1/ready_serial.txt \
-periodic-screenshot $(TMPDIR)/$1/ready_screenshots
@# In case there are blue screens, fail installation
if [ ! -f "$(OUTDIR)/$1/image.raw.s2e.ready" ]; then \
echo "Failed to create $(OUTDIR)/$1/image.raw.s2e.ready snapshot."; \
echo "Please check $(TMPDIR)/$1/ready_screenshots and logs for details"; \
exit 1; \
fi
$(call OK_MSG,[$1] Installation completed.)
$(OUTDIR)/$1/image.json: $(OUTDIR)/$1/guestfs $(OUTDIR)/$1/image.raw.s2e.ready $(SRC)/images.json $(SRC)/scripts/generate_image_descriptor.py
$(call INFO_MSG,[$$@] Creating image descriptor...)
$(SRC)/scripts/generate_image_descriptor.py -i $(SRC)/images.json -o $$@ -n "$1" \
snapshot="ready" qemu_build="$(call ARCH,$1)" \
memory="$(call TO_VAR_SNAPSHOT_SIZE,$1)" \
qemu_extra_flags="$(call TO_VAR_NIC,$1) $(call TO_VAR_CPU,$1)"
endef
###############################################################################
# The entry points of the makefile start here
WINDOWS_IMAGES = \
windows-xpsp3pro-i386 \
windows-8.1ent-x86_64 \
windows-7sp1ent-x86_64 \
windows-7sp1pro-i386 \
windows-10ent1703-x86_64 \
windows-10pro1909-x86_64
ISO_PATH = $(ISODIR)/$(call GET_SETTING,$(1),iso.name)
WINDOWS_TARGETS = $(addsuffix /image.json,$(addprefix $(OUTDIR)/,$(WINDOWS_IMAGES)))
WINDOWS_ARCHIVES=$(addsuffix .tar.xz,$(addprefix $(OUTDIR)/,$(WINDOWS_IMAGES)))
$(foreach os,$(WINDOWS_IMAGES), \
$(eval $(call TEMPLATE_BASE_WINDOWS_IMAGE,$(os),$(call ISO_PATH,$(os)))) \
$(eval $(call IMAGE_RULE,$(os))) \
)
windows: $(WINDOWS_TARGETS)
windows_archives: $(WINDOWS_ARCHIVES)