]>
Commit | Line | Data |
---|---|---|
fc2208b0 RM |
1 | #! /bin/sh -e |
2 | ||
3 | # Make GRUB rescue image | |
4 | # Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009 Free Software Foundation, Inc. | |
5 | # | |
6 | # GRUB is free software: you can redistribute it and/or modify | |
7 | # it under the terms of the GNU General Public License as published by | |
8 | # the Free Software Foundation, either version 3 of the License, or | |
9 | # (at your option) any later version. | |
10 | # | |
11 | # GRUB is distributed in the hope that it will be useful, | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | # GNU General Public License for more details. | |
15 | # | |
16 | # You should have received a copy of the GNU General Public License | |
17 | # along with GRUB. If not, see <http://www.gnu.org/licenses/>. | |
18 | ||
19 | # Initialize some variables. | |
20 | transform="@program_transform_name@" | |
21 | ||
22 | prefix=@prefix@ | |
23 | exec_prefix=@exec_prefix@ | |
24 | bindir=@bindir@ | |
25 | libdir=@libdir@ | |
26 | PACKAGE_NAME=@PACKAGE_NAME@ | |
27 | PACKAGE_TARNAME=@PACKAGE_TARNAME@ | |
28 | PACKAGE_VERSION=@PACKAGE_VERSION@ | |
29 | target_cpu=@target_cpu@ | |
efda854e | 30 | native_platform=@platform@ |
990f3548 | 31 | pkglib_DATA="@pkglib_DATA@" |
fc2208b0 | 32 | |
0ea7c4f9 GS |
33 | self=`basename $0` |
34 | ||
7e70395f VS |
35 | multiboot_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-multiboot |
36 | coreboot_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-coreboot | |
37 | qemu_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-qemu | |
38 | pc_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-pc | |
ac0289ba VS |
39 | efi32_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-efi |
40 | efi64_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/x86_64-efi | |
7e70395f | 41 | rom_directory= |
d1d368e4 | 42 | override_dir= |
7fb5c25f | 43 | grub_mkimage=${bindir}/`echo grub-mkimage | sed ${transform}` |
fc2208b0 RM |
44 | |
45 | # Usage: usage | |
46 | # Print the usage. | |
47 | usage () { | |
48 | cat <<EOF | |
0ea7c4f9 | 49 | Usage: $self [OPTION] SOURCE... |
fc2208b0 RM |
50 | Make GRUB rescue image. |
51 | ||
52 | -h, --help print this message and exit | |
53 | -v, --version print the version information and exit | |
7a64e28d | 54 | -o, --output=FILE save output in FILE [required] |
fc2208b0 | 55 | --modules=MODULES pre-load specified modules MODULES |
7e70395f | 56 | --rom-directory=DIR save rom images in DIR [optional] |
7fb5c25f | 57 | --grub-mkimage=FILE use FILE as grub-mkimage |
fc2208b0 | 58 | |
0ea7c4f9 | 59 | $self generates a bootable rescue image with specified source files or directories. |
fc2208b0 RM |
60 | |
61 | Report bugs to <bug-grub@gnu.org>. | |
62 | EOF | |
63 | } | |
64 | ||
b02c7c8f BC |
65 | argument () { |
66 | opt=$1 | |
67 | shift | |
68 | ||
69 | if test $# -eq 0; then | |
70 | echo "$0: option requires an argument -- '$opt'" 1>&2 | |
71 | exit 1 | |
72 | fi | |
73 | echo $1 | |
74 | } | |
75 | ||
fc2208b0 | 76 | # Check the arguments. |
7a64e28d BC |
77 | while test $# -gt 0 |
78 | do | |
79 | option=$1 | |
80 | shift | |
81 | ||
fc2208b0 RM |
82 | case "$option" in |
83 | -h | --help) | |
84 | usage | |
85 | exit 0 ;; | |
86 | -v | --version) | |
0ea7c4f9 | 87 | echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}" |
fc2208b0 | 88 | exit 0 ;; |
7a64e28d BC |
89 | |
90 | --modules) | |
b02c7c8f | 91 | modules=`argument $option "$@"`; shift ;; |
fc2208b0 RM |
92 | --modules=*) |
93 | modules=`echo "$option" | sed 's/--modules=//'` ;; | |
7a64e28d BC |
94 | |
95 | -o | --output) | |
b02c7c8f | 96 | output_image=`argument $option "$@"`; shift ;; |
fc2208b0 RM |
97 | --output=*) |
98 | output_image=`echo "$option" | sed 's/--output=//'` ;; | |
7a64e28d | 99 | |
d6d0c209 BC |
100 | --rom-directory) |
101 | rom_directory=`argument $option "$@"`; shift ;; | |
7e70395f VS |
102 | --rom-directory=*) |
103 | rom_directory=`echo "$option" | sed 's/--rom-directory=//'` ;; | |
d6d0c209 | 104 | |
efda854e | 105 | # Intentionally undocumented |
7a64e28d | 106 | --override-directory) |
b02c7c8f | 107 | override_dir=`argument $option "$@"` |
7a64e28d BC |
108 | shift |
109 | PATH=${override_dir}:$PATH | |
110 | export PATH | |
111 | ;; | |
efda854e RM |
112 | --override-directory=*) |
113 | override_dir=`echo "${option}/" | sed 's/--override-directory=//'` | |
114 | PATH=${override_dir}:$PATH | |
115 | export PATH | |
116 | ;; | |
d6d0c209 BC |
117 | |
118 | --grub-mkimage) | |
119 | grub_mkimage=`argument $option "$@"`; shift ;; | |
7fb5c25f CW |
120 | --grub-mkimage=*) |
121 | grub_mkimage=`echo "$option" | sed 's/--grub-mkimage=//'` ;; | |
fc2208b0 RM |
122 | -*) |
123 | echo "Unrecognized option \`$option'" 1>&2 | |
124 | usage | |
125 | exit 1 | |
126 | ;; | |
127 | *) | |
b02c7c8f | 128 | source="${source} ${option} $@"; break ;; |
fc2208b0 RM |
129 | esac |
130 | done | |
131 | ||
74ff1dd5 FZ |
132 | if [ "x${output_image}" = x ] ; then |
133 | echo "output file must be given" >&2 | |
134 | usage | |
135 | exit 1 | |
136 | fi | |
137 | ||
7fb5c25f CW |
138 | set $grub_mkimage dummy |
139 | if test -f "$1"; then | |
140 | : | |
141 | else | |
142 | echo "$1: Not found." 1>&2 | |
143 | exit 1 | |
144 | fi | |
145 | ||
33e2e6f3 VS |
146 | if test "x$TMP" != x; then |
147 | MKTEMP_TEMPLATE="$TMP/grub-mkrescue.XXXXXXXXXX" | |
148 | elif test "x$TEMP" != x; then | |
149 | MKTEMP_TEMPLATE="$TEMP/grub-mkrescue.XXXXXXXXXX" | |
150 | else | |
151 | MKTEMP_TEMPLATE="/tmp/grub-mkrescue.XXXXXXXXXX" | |
152 | fi | |
153 | ||
154 | iso9660_dir=`mktemp -d "$MKTEMP_TEMPLATE"` | |
fc2208b0 RM |
155 | mkdir -p ${iso9660_dir}/boot/grub |
156 | ||
efda854e RM |
157 | process_input_dir () |
158 | { | |
159 | input_dir="$1" | |
160 | platform="$2" | |
7e70395f | 161 | mkdir -p ${iso9660_dir}/boot/grub/${platform} |
10a88797 | 162 | for file in ${input_dir}/*.mod; do |
efda854e | 163 | if test -f "$file"; then |
7e70395f | 164 | cp -f "$file" ${iso9660_dir}/boot/grub/${platform}/ |
efda854e RM |
165 | fi |
166 | done | |
10a88797 FZ |
167 | for file in ${pkglib_DATA}; do |
168 | if test -f "${input_dir}/${file}"; then | |
7e70395f | 169 | cp -f "${input_dir}/${file}" ${iso9660_dir}/boot/grub/${platform}/ |
10a88797 FZ |
170 | fi |
171 | done | |
57bbe3be CPE |
172 | |
173 | mkdir -p ${iso9660_dir}/boot/grub/locale | |
174 | for file in ${input_dir}/po/*.mo; do | |
175 | if test -f "$file"; then | |
176 | cp -f "$file" ${iso9660_dir}/boot/grub/locale/ | |
177 | fi | |
178 | done | |
efda854e RM |
179 | } |
180 | ||
7e70395f VS |
181 | make_image () |
182 | { | |
183 | source_directory="$1" | |
184 | platform=$2 | |
185 | if ! test -e "${source_directory}"; then | |
186 | return; | |
efda854e | 187 | fi |
fc2208b0 | 188 | |
7e70395f VS |
189 | echo "Enabling $2 support ..." |
190 | ||
33e2e6f3 VS |
191 | memdisk_img=`mktemp "$MKTEMP_TEMPLATE"` |
192 | memdisk_dir=`mktemp -d "$MKTEMP_TEMPLATE"` | |
fc2208b0 | 193 | mkdir -p ${memdisk_dir}/boot/grub |
fc2208b0 | 194 | |
7e70395f | 195 | modules="$(cat ${source_directory}/partmap.lst) ${modules}" |
fc2208b0 RM |
196 | cat << EOF > ${memdisk_dir}/boot/grub/grub.cfg |
197 | search --fs-uuid --set ${iso_uuid} | |
7e70395f VS |
198 | set prefix=(\${root})/boot/grub/${platform} |
199 | source \$prefix/grub.cfg | |
fc2208b0 RM |
200 | EOF |
201 | (for i in ${modules} ; do | |
202 | echo "insmod $i" | |
203 | done ; \ | |
204 | echo "source /boot/grub/grub.cfg") \ | |
7e70395f | 205 | > ${iso9660_dir}/boot/grub/${platform}/grub.cfg |
fc2208b0 RM |
206 | |
207 | tar -C ${memdisk_dir} -cf ${memdisk_img} boot | |
208 | rm -rf ${memdisk_dir} | |
7fb5c25f | 209 | $grub_mkimage -O ${platform} -d "${source_directory}" -m "${memdisk_img}" -o "$3" --prefix='(memdisk)/boot/grub' \ |
8b26d1d3 | 210 | search iso9660 configfile normal sh memdisk tar $4 |
7e70395f VS |
211 | rm -rf ${memdisk_img} |
212 | } | |
213 | ||
efda854e | 214 | if [ "${override_dir}" = "" ] ; then |
7210dca9 | 215 | if test -e "${multiboot_dir}" ; then |
7e70395f VS |
216 | process_input_dir ${multiboot_dir} i386-multiboot |
217 | fi | |
218 | if test -e "${coreboot_dir}" ; then | |
219 | process_input_dir ${coreboot_dir} i386-coreboot | |
220 | fi | |
221 | if test -e "${qemu_dir}" ; then | |
222 | process_input_dir ${qemu_dir} i386-qemu | |
fc2208b0 | 223 | fi |
efda854e | 224 | if test -e "${pc_dir}" ; then |
7e70395f | 225 | process_input_dir ${pc_dir} i386-pc |
efda854e | 226 | fi |
ac0289ba | 227 | if test -e "${efi32_dir}" ; then |
7e70395f | 228 | process_input_dir ${efi32_dir} i386-efi |
ac0289ba VS |
229 | fi |
230 | if test -e "${efi64_dir}" ; then | |
7e70395f | 231 | process_input_dir ${efi64_dir} x86_64-efi |
ac0289ba | 232 | fi |
efda854e | 233 | else |
7e70395f | 234 | process_input_dir ${override_dir} ${target_cpu}-${native_platform} |
7210dca9 | 235 | multiboot_dir= |
efda854e | 236 | pc_dir= |
ac0289ba VS |
237 | efi32_dir= |
238 | efi64_dir= | |
7e70395f VS |
239 | coreboot_dir= |
240 | qemu_dir= | |
ac0289ba | 241 | case "${target_cpu}-${native_platform}" in |
918405fd | 242 | i386-multiboot) multiboot_dir=${override_dir} ;; |
7e70395f VS |
243 | i386-coreboot) coreboot_dir=${override_dir} ;; |
244 | i386-qemu) qemu_dir=${override_dir} ;; | |
245 | i386-pc) pc_dir=${override_dir} ;; | |
ac0289ba VS |
246 | i386-efi) efi32_dir=${override_dir} ;; |
247 | x86_64-efi) efi64_dir=${override_dir} ;; | |
efda854e | 248 | esac |
fc2208b0 RM |
249 | fi |
250 | ||
13ac4305 VS |
251 | # obtain date-based UUID |
252 | iso_uuid=$(date -u +%Y-%m-%d-%H-%M-%S-00) | |
253 | grub_mkisofs_arguments="${grub_mkisofs_arguments} --modification-date=$(echo ${iso_uuid} | sed -e s/-//g)" | |
254 | ||
52cc3ce0 | 255 | # build BIOS core.img |
efda854e | 256 | if test -e "${pc_dir}" ; then |
52cc3ce0 | 257 | echo "Enabling BIOS support ..." |
33e2e6f3 | 258 | core_img=`mktemp "$MKTEMP_TEMPLATE"` |
26966aeb | 259 | $grub_mkimage -O i386-pc -d ${pc_dir}/ -o ${core_img} --prefix=/boot/grub/i386-pc \ |
b15937b1 | 260 | iso9660 biosdisk |
fc2208b0 | 261 | cat ${pc_dir}/cdboot.img ${core_img} > ${iso9660_dir}/boot/grub/i386-pc/eltorito.img |
0ae56929 | 262 | |
33e2e6f3 | 263 | embed_img=`mktemp "$MKTEMP_TEMPLATE"` |
e9309813 | 264 | cat ${pc_dir}/boot.img ${core_img} > ${embed_img} |
0ae56929 | 265 | |
fc2208b0 RM |
266 | rm -f ${core_img} |
267 | ||
268 | modules="$(cat ${pc_dir}/partmap.lst) ${modules}" | |
269 | (for i in ${modules} ; do | |
270 | echo "insmod $i" | |
271 | done ; \ | |
272 | echo "source /boot/grub/grub.cfg") \ | |
273 | > ${iso9660_dir}/boot/grub/i386-pc/grub.cfg | |
274 | ||
47779711 | 275 | grub_mkisofs_arguments="${grub_mkisofs_arguments} -b boot/grub/i386-pc/eltorito.img -no-emul-boot -boot-info-table \ |
0ae56929 | 276 | --embedded-boot ${embed_img}" |
fc2208b0 RM |
277 | fi |
278 | ||
7e70395f VS |
279 | # build multiboot core.img |
280 | make_image "${multiboot_dir}" i386-multiboot "${iso9660_dir}/boot/multiboot.img" "ata at_keyboard" | |
281 | ||
260ddb94 VS |
282 | if test -e "${efi64_dir}" || test -e "${efi32_dir}"; then |
283 | efi_dir=`mktemp -d "$MKTEMP_TEMPLATE"` | |
284 | mkdir -p "${efi_dir}/efi/boot" | |
13ac4305 | 285 | |
7e70395f VS |
286 | # build bootx64.efi |
287 | make_image "${efi64_dir}" x86_64-efi "${efi_dir}"/efi/boot/bootx64.efi "" | |
288 | # build bootia32.efi | |
289 | make_image "${efi32_dir}" i386-efi "${efi_dir}"/efi/boot/bootia32.efi "" | |
13ac4305 | 290 | |
260ddb94 VS |
291 | mformat -C -f 2880 -L 16 -i "${iso9660_dir}"/efi.img :: |
292 | mcopy -s -i "${iso9660_dir}"/efi.img ${efi_dir}/efi ::/ | |
293 | grub_mkisofs_arguments="${grub_mkisofs_arguments} --efi-boot efi.img" | |
ac0289ba VS |
294 | fi |
295 | ||
7e70395f VS |
296 | make_image "${qemu_dir}" i386-qemu "${iso9660_dir}/boot/qemu.img" "ata at_keyboard" |
297 | if [ -e "${iso9660_dir}/boot/qemu.img" ] && [ -d "${rom_directory}" ]; then | |
298 | cp "${iso9660_dir}/boot/qemu.img" "${rom_directory}/qemu.img" | |
299 | fi | |
300 | make_image "${coreboot_dir}" i386-coreboot "${iso9660_dir}/boot/coreboot.elf" "ata at_keyboard" | |
301 | if [ -e "${iso9660_dir}/boot/coreboot.elf" ] && [ -d "${rom_directory}" ]; then | |
302 | cp "${iso9660_dir}/boot/coreboot.elf" "${rom_directory}/coreboot.elf" | |
303 | fi | |
304 | ||
fc2208b0 | 305 | # build iso image |
ff6bea78 | 306 | xorriso -pathspecs on -as mkisofs ${grub_mkisofs_arguments} --protective-msdos-label -o ${output_image} -r ${iso9660_dir} ${source} |
fc2208b0 RM |
307 | rm -rf ${iso9660_dir} |
308 | ||
0ae56929 RM |
309 | rm -f ${embed_img} |
310 | ||
fc2208b0 | 311 | exit 0 |