aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/boot/genimage.sh
blob: 0673fdfc1a11abdd6a12be8b4f324d009065e6f5 (plain) (blame)
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
#!/bin/bash
#
# This file is subject to the terms and conditions of the GNU General Public
# License.  See the file "COPYING" in the main directory of this archive
# for more details.
#
# Copyright (C) 2017 by Changbin Du <changbin.du@intel.com>
#
# Adapted from code in arch/x86/boot/Makefile by H. Peter Anvin and others
#
# "make fdimage/fdimage144/fdimage288/hdimage/isoimage"
# script for x86 architecture
#
# Arguments:
#   $1  - fdimage format
#   $2  - target image file
#   $3  - kernel bzImage file
#   $4  - mtools configuration file
#   $5  - kernel cmdline
#   $6+ - initrd image file(s)
#
# This script requires:
#   bash
#   syslinux
#   mtools (for fdimage* and hdimage)
#   edk2/OVMF (for hdimage)
#
# Otherwise try to stick to POSIX shell commands...
#

# Use "make V=1" to debug this script
case "${KBUILD_VERBOSE}" in
*1*)
        set -x
        ;;
esac

# Exit the top-level shell with an error
topshell=$$
trap 'exit 1' USR1
die() {
	echo ""        1>&2
	echo " *** $*" 1>&2
	echo ""        1>&2
	kill -USR1 $topshell
}

# Verify the existence and readability of a file
verify() {
	if [ ! -f "$1" -o ! -r "$1" ]; then
		die "Missing file: $1"
	fi
}

diskfmt="$1"
FIMAGE="$2"
FBZIMAGE="$3"
MTOOLSRC="$4"
KCMDLINE="$5"
shift 5				# Remaining arguments = initrd files

export MTOOLSRC

# common options for dd
dd='dd iflag=fullblock'

# Make sure the files actually exist
verify "$FBZIMAGE"

declare -a FDINITRDS
irdpfx=' initrd='
initrdopts_syslinux=''
initrdopts_efi=''
for f in "$@"; do
	if [ -f "$f" -a -r "$f" ]; then
	    FDINITRDS=("${FDINITRDS[@]}" "$f")
	    fname="$(basename "$f")"
	    initrdopts_syslinux="${initrdopts_syslinux}${irdpfx}${fname}"
	    irdpfx=,
	    initrdopts_efi="${initrdopts_efi} initrd=${fname}"
	fi
done

# Read a $3-byte littleendian unsigned value at offset $2 from file $1
le() {
	local n=0
	local m=1
	for b in $(od -A n -v -j $2 -N $3 -t u1 "$1"); do
		n=$((n + b*m))
		m=$((m * 256))
	done
	echo $n
}

# Get the EFI architecture name such that boot{name}.efi is the default
# boot file name. Returns false with no output if the file is not an
# EFI image or otherwise unknown.
efiarch() {
	[ -f "$1" ] || return
	[ $(le "$1" 0 2) -eq 23117 ] || return		# MZ magic
	peoffs=$(le "$1" 60 4)				# PE header offset
	[ $peoffs -ge 64 ] || return
	[ $(le "$1" $peoffs 4) -eq 17744 ] || return	# PE magic
	case $(le "$1" $((peoffs+4+20)) 2) in		# PE type
		267)	;;				# PE32
		523)	;;				# PE32+
		*) return 1 ;;				# Invalid
	esac
	[ $(le "$1" $((peoffs+4+20+68)) 2) -eq 10 ] || return # EFI app
	case $(le "$1" $((peoffs+4)) 2) in		# Machine type
		 332)	echo i386	;;
		 450)	echo arm	;;
		 512)	echo ia64	;;
		20530)	echo riscv32	;;
		20580)	echo riscv64	;;
		20776)	echo riscv128	;;
		34404)	echo x64	;;
		43620)	echo aa64	;;
	esac
}

# Get the combined sizes in bytes of the files given, counting sparse
# files as full length, and padding each file to a 4K block size
filesizes() {
	local t=0
	local s
	for s in $(ls -lnL "$@" 2>/dev/null | awk '/^-/{ print $5; }'); do
		t=$((t + ((s+4095)/4096)*4096))
	done
	echo $t
}

# Expand directory names which should be in /usr/share into a list
# of possible alternatives
sharedirs() {
	local dir file
	for dir in /usr/share /usr/lib64 /usr/lib; do
		for file; do
			echo "$dir/$file"
			echo "$dir/${file^^}"
		done
	done
}
efidirs() {
	local dir file
	for dir in /usr/share /boot /usr/lib64 /usr/lib; do
		for file; do
			echo "$dir/$file"
			echo "$dir/${file^^}"
		done
	done
}

findsyslinux() {
	local f="$(find -L $(sharedirs syslinux isolinux) \
		    -name "$1" -readable -type f -print -quit 2>/dev/null)"
	if [ ! -f "$f" ]; then
		die "Need a $1 file, please install syslinux/isolinux."
	fi
	echo "$f"
	return 0
}

findovmf() {
	local arch="$1"
	shift
	local -a names=(-false)
	local name f
	for name; do
		names=("${names[@]}" -or -iname "$name")
	done
	for f in $(find -L $(efidirs edk2 ovmf) \
			\( "${names[@]}" \) -readable -type f \
			-print 2>/dev/null); do
		if [ "$(efiarch "$f")" = "$arch" ]; then
			echo "$f"
			return 0
		fi
	done
	die "Need a $1 file for $arch, please install EDK2/OVMF."
}

do_mcopy() {
	if [ ${#FDINITRDS[@]} -gt 0 ]; then
		mcopy "${FDINITRDS[@]}" "$1"
	fi
	if [ -n "$efishell" ]; then
		mmd "$1"EFI "$1"EFI/Boot
		mcopy "$efishell" "$1"EFI/Boot/boot${kefiarch}.efi
	fi
	if [ -n "$kefiarch" ]; then
		echo linux "$KCMDLINE$initrdopts_efi" | \
			mcopy - "$1"startup.nsh
	fi
	echo default linux "$KCMDLINE$initrdopts_syslinux" | \
		mcopy - "$1"syslinux.cfg
	mcopy "$FBZIMAGE" "$1"linux
}

genbzdisk() {
	verify "$MTOOLSRC"
	mformat -v 'LINUX_BOOT' a:
	syslinux "$FIMAGE"
	do_mcopy a:
}

genfdimage144() {
	verify "$MTOOLSRC"
	$dd if=/dev/zero of="$FIMAGE" bs=1024 count=1440 2>/dev/null
	mformat -v 'LINUX_BOOT' v:
	syslinux "$FIMAGE"
	do_mcopy v:
}

genfdimage288() {
	verify "$MTOOLSRC"
	$dd if=/dev/zero of="$FIMAGE" bs=1024 count=2880 2>/dev/null
	mformat -v 'LINUX_BOOT' w:
	syslinux "$FIMAGE"
	do_mcopy w:
}

genhdimage() {
	verify "$MTOOLSRC"
	mbr="$(findsyslinux mbr.bin)"
	kefiarch="$(efiarch "$FBZIMAGE")"
	if [ -n "$kefiarch" ]; then
		# The efishell provides command line handling
		efishell="$(findovmf $kefiarch shell.efi shell${kefiarch}.efi)"
		ptype='-T 0xef'	# EFI system partition, no GPT
	fi
	sizes=$(filesizes "$FBZIMAGE" "${FDINITRDS[@]}" "$efishell")
	# Allow 1% + 1 MiB for filesystem and partition table overhead,
	# syslinux, and config files
	megs=$(((sizes + sizes/100 + 2*1024*1024 - 1)/(1024*1024)))
	$dd if=/dev/zero of="$FIMAGE" bs=$((1024*1024)) count=$megs 2>/dev/null
	mpartition -I -c -s 32 -h 64 -t $megs $ptype -b 512 -a h:
	$dd if="$mbr" of="$FIMAGE" bs=440 count=1 conv=notrunc 2>/dev/null
	mformat -v 'LINUX_BOOT' -s 32 -h 64 -t $megs h:
	syslinux --offset $((512*512)) "$FIMAGE"
	do_mcopy h:
}

geniso() {
	tmp_dir="$(dirname "$FIMAGE")/isoimage"
	rm -rf "$tmp_dir"
	mkdir "$tmp_dir"
	isolinux=$(findsyslinux isolinux.bin)
	ldlinux=$(findsyslinux  ldlinux.c32)
	cp "$isolinux" "$ldlinux" "$tmp_dir"
	cp "$FBZIMAGE" "$tmp_dir"/linux
	echo default linux "$KCMDLINE" > "$tmp_dir"/isolinux.cfg
	cp "${FDINITRDS[@]}" "$tmp_dir"/
	genisoimage -J -r -appid 'LINUX_BOOT' -input-charset=utf-8 \
		    -quiet -o "$FIMAGE" -b isolinux.bin \
		    -c boot.cat -no-emul-boot -boot-load-size 4 \
		    -boot-info-table "$tmp_dir"
	isohybrid "$FIMAGE" 2>/dev/null || true
	rm -rf "$tmp_dir"
}

rm -f "$FIMAGE"

case "$diskfmt" in
	bzdisk)     genbzdisk;;
	fdimage144) genfdimage144;;
	fdimage288) genfdimage288;;
	hdimage)    genhdimage;;
	isoimage)   geniso;;
	*)          die "Unknown image format: $diskfmt";;
esac