summaryrefslogtreecommitdiff
path: root/scripts/build/binary_grub-efi
blob: 384135b15a919db428a41067af01d3331e696731 (plain)
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
#!/bin/sh

## live-build(7) - System Build Scripts
## Copyright (C) 2016 Adrian Gibanel Lopez <adrian15sgd@gmail.com>
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.


set -e

# Including common functions
[ -e "${LIVE_BUILD}/scripts/build.sh" ] && . "${LIVE_BUILD}/scripts/build.sh" || . /usr/lib/live/build.sh

# Setting static variables
DESCRIPTION="$(Echo 'prepares and installs Grub based EFI support into binary')"
HELP=""
USAGE="${PROGRAM} [--force]"

Arguments "${@}"

# Reading configuration files
Read_conffiles config/all config/common config/bootstrap config/chroot config/binary config/source
Set_defaults

Check_Any_Bootloader_Role "grub-efi"

Echo_message "Begin preparing Grub based EFI support..."

# Requiring stage file
Require_stagefile .build/config .build/bootstrap

# Checking stage file
Check_stagefile .build/binary_grub-efi

# Checking lock file
Check_lockfile .lock

# Creating lock file
Create_lockfile .lock

# Check architecture
Check_architectures amd64 i386
Check_crossarchitectures

case "${LB_ARCHITECTURES}" in
	amd64)
		_EFI_TYPE=efi64
		;;
	i386)
		_EFI_TYPE=efi32
		;;
	*)
		echo "ERROR: can't provide EFI boot support to architecture ${LB_ARCHITECTURES}" >&2
		exit 1
		;;
esac


# Checking depends
case "${LB_BUILD_WITH_CHROOT}" in
	true)
		_CHROOT_DIR=""
		_SYSLINUX_EFI_DIR="chroot/usr/lib/SYSLINUX.EFI/$_EFI_TYPE"
		_SYSLINUX_COMMON_DIR="chroot/usr/lib/syslinux/modules/$_EFI_TYPE"

		Check_package chroot /usr/lib/grub/x86_64-efi/configfile.mod grub-efi-amd64-bin
		Check_package chroot /usr/lib/grub/i386-efi/configfile.mod grub-efi-ia32-bin
		Check_package chroot /usr/bin/grub-mkimage grub-common
		Check_package chroot /usr/bin/mcopy mtools
		Check_package chroot /sbin/mkfs.msdos dosfstools
		;;

	false)
		_CHROOT_DIR="chroot"

		if [ ! -e /usr/lib/grub/x86_64-efi ]
		then
			# grub-efi-amd64-bin
			Echo_error "/usr/lib/grub/x86_64-efi - no such directory"
			exit 1
		fi

		if [ ! -e /usr/lib/grub/i386-efi ]
		then
			# grub-efi-ia32-bin
			Echo_error "/usr/lib/grub/i386-efi - no such directory"
			exit 1
		fi

		if [ ! -e /usr/bin/grub-mkimage ]
		then
			# grub-common
			Echo_error "/usr/bin/grub-mkimage - no such file."
			exit 1
		fi

		if [ ! -e /usr/bin/mcopy ]
		then
			# mtools
			Echo_error "/usr/bin/mcopy - no such file."
			exit 1
		fi

		if [ ! -e /sbin/mkfs.msdos ]
		then
			# dosfstools
			Echo_error "/sbin/mkfs.msdos - no such file."
			exit 1
		fi
		;;
esac




case "${LB_INITRAMFS}" in
	live-boot)
		INITFS="live"
		;;

	*)
		INITFS=""
		;;
esac

# Setting destination directory
case "${LIVE_IMAGE_TYPE}" in
	iso*|tar)
		case "${LB_INITRAMFS}" in
			live-boot)
				DESTDIR_LIVE="binary/live"
				;;

			*)
				DESTDIR_LIVE="binary/live"
				;;
		esac

		DESTDIR_INSTALL="binary/install"
		;;

	hdd*|netboot)
		Echo_warning "Bootloader in this image type not yet supported by live-build."
		Echo_warning "This would produce a not bootable image, aborting (FIXME)."
		exit 1
	;;
esac

# Restoring cache
Restore_cache cache/packages.binary

# Installing depends
Install_package

# Cleanup files that we generate
rm -rf binary/boot/efi.img binary/boot/grub/i386-efi/ binary/boot/grub/x86_64-efi

# This is workaround till both efi-image and grub-cpmodules are put into a binary package
case "${LB_BUILD_WITH_CHROOT}" in
        true)
		if [ ! -e "${LIVE_BUILD}" ] ; then
			LIVE_BUILD_PATH="/usr/lib/live/build"
		else
			LIVE_BUILD_PATH="${LIVE_BUILD}/scripts/build"
		fi
		mkdir -p chroot/${LIVE_BUILD_PATH}
		cp "${LIVE_BUILD_PATH}/efi-image" "chroot/${LIVE_BUILD_PATH}"
		cp "${LIVE_BUILD_PATH}/grub-cpmodules" "chroot/${LIVE_BUILD_PATH}"
        ;;
esac
#####
cat >binary.sh <<END
#!/bin/sh

set -e

PRE_EFI_IMAGE_PATH="${PATH}"
if [ ! -e "${LIVE_BUILD}" ] ; then
	LIVE_BUILD_PATH="/usr/lib/live/build"
else
	LIVE_BUILD_PATH="${LIVE_BUILD}/scripts/build"
fi

PATH="${PATH}:\${LIVE_BUILD_PATH}" # Make sure grub-cpmodules is used as if it was installed in the system

"\${LIVE_BUILD_PATH}/efi-image" "${_CHROOT_DIR}/grub-efi-temp-x86_64-efi/" "x86_64-efi" "x64" "debian-live/amd64"
mkdir -p ${_CHROOT_DIR}/grub-efi-temp/efi/boot
mcopy -n -i ${_CHROOT_DIR}/grub-efi-temp-x86_64-efi/efi.img '::efi/boot/boot*.efi' ${_CHROOT_DIR}/grub-efi-temp/efi/boot
cp -r "${_CHROOT_DIR}"/grub-efi-temp-x86_64-efi/* "${_CHROOT_DIR}/grub-efi-temp/"

"\${LIVE_BUILD_PATH}/efi-image" "${_CHROOT_DIR}/grub-efi-temp-i386-efi/" "i386-efi" "ia32" "debian-live/i386"
PATH="\${PRE_EFI_IMAGE_PATH}"
mkdir -p ${_CHROOT_DIR}/grub-efi-temp/efi/boot
mcopy -n -i ${_CHROOT_DIR}/grub-efi-temp-i386-efi/efi.img '::efi/boot/boot*.efi' ${_CHROOT_DIR}/grub-efi-temp/efi/boot
cp -r "${_CHROOT_DIR}"/grub-efi-temp-i386-efi/* "${_CHROOT_DIR}/grub-efi-temp/"

# The code below is adapted from tools/boot/jessie/boot-x86
# in debian-cd

# Stuff the EFI boot files into a FAT filesystem, making it as
# small as possible.  24KiB headroom seems to be enough;
# (x+31)/32*32 rounds up to multiple of 32.
# This is the same as in efi-image, but we need to redo it here in
# the case of a multi-arch amd64/i386 image

size=0
for file in ${_CHROOT_DIR}/grub-efi-temp/efi/boot/boot*.efi; do
	size=\$((\$size + \$(stat -c %s "\$file")))
done

blocks=\$(((\$size / 1024 + 55) / 32 * 32 ))

rm -f ${_CHROOT_DIR}/grub-efi-temp/boot/grub/efi.img
mkfs.msdos -C "${_CHROOT_DIR}/grub-efi-temp/boot/grub/efi.img" \$blocks >/dev/null
mmd -i "${_CHROOT_DIR}/grub-efi-temp/boot/grub/efi.img" ::efi
mmd -i "${_CHROOT_DIR}/grub-efi-temp/boot/grub/efi.img" ::efi/boot
mcopy -o -i "${_CHROOT_DIR}/grub-efi-temp/boot/grub/efi.img" ${_CHROOT_DIR}/grub-efi-temp/efi/boot/boot*.efi \
	"::efi/boot"
END

case "${LB_BUILD_WITH_CHROOT}" in
        true)
			mv binary.sh chroot/
			Chroot chroot "sh binary.sh"
			rm -f chroot/binary.sh

			# Saving cache
			Save_cache cache/packages.binary

			# Removing depends
			Remove_package
			;;

		false)
			sh binary.sh
			rm -f binary.sh
			;;
esac

# Remove unnecessary files
rm -f chroot/grub-efi-temp/bootnetia32.efi
rm -f chroot/grub-efi-temp/bootnetx64.efi

mkdir -p binary
cp -r chroot/grub-efi-temp/* binary/
rm -rf chroot/grub-efi-temp-x86_64-efi
rm -rf chroot/grub-efi-temp-i386-efi
rm -rf chroot/grub-efi-temp

# We rely on: binary_loopback_cfg to generate grub.cfg and other configuration files

# Creating stage file
Create_stagefile .build/binary_grub-efi