]>
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 | 44 | |
a6085973 TS |
45 | xorriso=xorriso |
46 | ||
fc2208b0 RM |
47 | # Usage: usage |
48 | # Print the usage. | |
49 | usage () { | |
50 | cat <<EOF | |
0ea7c4f9 | 51 | Usage: $self [OPTION] SOURCE... |
fc2208b0 RM |
52 | Make GRUB rescue image. |
53 | ||
54 | -h, --help print this message and exit | |
55 | -v, --version print the version information and exit | |
74ff1dd5 | 56 | --output=FILE save output in FILE [required] |
7e70395f VS |
57 | --modules=MODULES pre-load specified modules MODULES |
58 | --rom-directory=DIR save rom images in DIR [optional] | |
a6085973 | 59 | --xorriso=FILE use FILE as xorriso [optional] |
7fb5c25f | 60 | --grub-mkimage=FILE use FILE as grub-mkimage |
fc2208b0 | 61 | |
0ea7c4f9 | 62 | $self generates a bootable rescue image with specified source files or directories. |
fc2208b0 RM |
63 | |
64 | Report bugs to <bug-grub@gnu.org>. | |
65 | EOF | |
66 | } | |
67 | ||
68 | # Check the arguments. | |
69 | for option in "$@"; do | |
70 | case "$option" in | |
71 | -h | --help) | |
72 | usage | |
73 | exit 0 ;; | |
74 | -v | --version) | |
0ea7c4f9 | 75 | echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}" |
fc2208b0 RM |
76 | exit 0 ;; |
77 | --modules=*) | |
78 | modules=`echo "$option" | sed 's/--modules=//'` ;; | |
79 | --output=*) | |
80 | output_image=`echo "$option" | sed 's/--output=//'` ;; | |
7e70395f VS |
81 | --rom-directory=*) |
82 | rom_directory=`echo "$option" | sed 's/--rom-directory=//'` ;; | |
efda854e RM |
83 | # Intentionally undocumented |
84 | --override-directory=*) | |
85 | override_dir=`echo "${option}/" | sed 's/--override-directory=//'` | |
86 | PATH=${override_dir}:$PATH | |
87 | export PATH | |
88 | ;; | |
7fb5c25f CW |
89 | --grub-mkimage=*) |
90 | grub_mkimage=`echo "$option" | sed 's/--grub-mkimage=//'` ;; | |
a6085973 TS |
91 | --xorriso=*) |
92 | xorriso=`echo "${option}/" | sed 's/--xorriso=//'` ;; | |
fc2208b0 RM |
93 | -*) |
94 | echo "Unrecognized option \`$option'" 1>&2 | |
95 | usage | |
96 | exit 1 | |
97 | ;; | |
98 | *) | |
99 | source="${source} ${option}" ;; | |
100 | esac | |
101 | done | |
102 | ||
74ff1dd5 FZ |
103 | if [ "x${output_image}" = x ] ; then |
104 | echo "output file must be given" >&2 | |
105 | usage | |
106 | exit 1 | |
107 | fi | |
108 | ||
7fb5c25f CW |
109 | set $grub_mkimage dummy |
110 | if test -f "$1"; then | |
111 | : | |
112 | else | |
113 | echo "$1: Not found." 1>&2 | |
114 | exit 1 | |
115 | fi | |
116 | ||
33e2e6f3 VS |
117 | if test "x$TMP" != x; then |
118 | MKTEMP_TEMPLATE="$TMP/grub-mkrescue.XXXXXXXXXX" | |
119 | elif test "x$TEMP" != x; then | |
120 | MKTEMP_TEMPLATE="$TEMP/grub-mkrescue.XXXXXXXXXX" | |
121 | else | |
122 | MKTEMP_TEMPLATE="/tmp/grub-mkrescue.XXXXXXXXXX" | |
123 | fi | |
124 | ||
125 | iso9660_dir=`mktemp -d "$MKTEMP_TEMPLATE"` | |
fc2208b0 RM |
126 | mkdir -p ${iso9660_dir}/boot/grub |
127 | ||
efda854e RM |
128 | process_input_dir () |
129 | { | |
130 | input_dir="$1" | |
131 | platform="$2" | |
7e70395f | 132 | mkdir -p ${iso9660_dir}/boot/grub/${platform} |
10a88797 | 133 | for file in ${input_dir}/*.mod; do |
efda854e | 134 | if test -f "$file"; then |
7e70395f | 135 | cp -f "$file" ${iso9660_dir}/boot/grub/${platform}/ |
efda854e RM |
136 | fi |
137 | done | |
10a88797 FZ |
138 | for file in ${pkglib_DATA}; do |
139 | if test -f "${input_dir}/${file}"; then | |
7e70395f | 140 | cp -f "${input_dir}/${file}" ${iso9660_dir}/boot/grub/${platform}/ |
10a88797 FZ |
141 | fi |
142 | done | |
57bbe3be CPE |
143 | |
144 | mkdir -p ${iso9660_dir}/boot/grub/locale | |
145 | for file in ${input_dir}/po/*.mo; do | |
146 | if test -f "$file"; then | |
147 | cp -f "$file" ${iso9660_dir}/boot/grub/locale/ | |
148 | fi | |
149 | done | |
efda854e RM |
150 | } |
151 | ||
7e70395f VS |
152 | make_image () |
153 | { | |
154 | source_directory="$1" | |
155 | platform=$2 | |
156 | if ! test -e "${source_directory}"; then | |
157 | return; | |
158 | fi | |
159 | ||
160 | echo "Enabling $2 support ..." | |
161 | ||
162 | memdisk_img=`mktemp "$MKTEMP_TEMPLATE"` | |
163 | memdisk_dir=`mktemp -d "$MKTEMP_TEMPLATE"` | |
164 | mkdir -p ${memdisk_dir}/boot/grub | |
165 | ||
166 | modules="$(cat ${source_directory}/partmap.lst) ${modules}" | |
167 | cat << EOF > ${memdisk_dir}/boot/grub/grub.cfg | |
168 | search --fs-uuid --set ${iso_uuid} | |
169 | set prefix=(\${root})/boot/grub/${platform} | |
170 | source \$prefix/grub.cfg | |
171 | EOF | |
172 | (for i in ${modules} ; do | |
173 | echo "insmod $i" | |
174 | done ; \ | |
175 | echo "source /boot/grub/grub.cfg") \ | |
176 | > ${iso9660_dir}/boot/grub/${platform}/grub.cfg | |
177 | ||
178 | tar -C ${memdisk_dir} -cf ${memdisk_img} boot | |
179 | rm -rf ${memdisk_dir} | |
7fb5c25f | 180 | $grub_mkimage -O ${platform} -d "${source_directory}" -m "${memdisk_img}" -o "$3" --prefix='(memdisk)/boot/grub' \ |
ee62c427 | 181 | search iso9660 configfile normal memdisk tar $4 |
7e70395f VS |
182 | rm -rf ${memdisk_img} |
183 | } | |
184 | ||
efda854e | 185 | if [ "${override_dir}" = "" ] ; then |
7210dca9 | 186 | if test -e "${multiboot_dir}" ; then |
7e70395f VS |
187 | process_input_dir ${multiboot_dir} i386-multiboot |
188 | fi | |
189 | if test -e "${coreboot_dir}" ; then | |
190 | process_input_dir ${coreboot_dir} i386-coreboot | |
191 | fi | |
192 | if test -e "${qemu_dir}" ; then | |
193 | process_input_dir ${qemu_dir} i386-qemu | |
fc2208b0 | 194 | fi |
efda854e | 195 | if test -e "${pc_dir}" ; then |
7e70395f | 196 | process_input_dir ${pc_dir} i386-pc |
efda854e | 197 | fi |
ac0289ba | 198 | if test -e "${efi32_dir}" ; then |
7e70395f | 199 | process_input_dir ${efi32_dir} i386-efi |
ac0289ba VS |
200 | fi |
201 | if test -e "${efi64_dir}" ; then | |
7e70395f | 202 | process_input_dir ${efi64_dir} x86_64-efi |
ac0289ba | 203 | fi |
efda854e | 204 | else |
7e70395f | 205 | process_input_dir ${override_dir} ${target_cpu}-${native_platform} |
7210dca9 | 206 | multiboot_dir= |
efda854e | 207 | pc_dir= |
ac0289ba VS |
208 | efi32_dir= |
209 | efi64_dir= | |
7e70395f VS |
210 | coreboot_dir= |
211 | qemu_dir= | |
ac0289ba | 212 | case "${target_cpu}-${native_platform}" in |
918405fd | 213 | i386-multiboot) multiboot_dir=${override_dir} ;; |
7e70395f VS |
214 | i386-coreboot) coreboot_dir=${override_dir} ;; |
215 | i386-qemu) qemu_dir=${override_dir} ;; | |
216 | i386-pc) pc_dir=${override_dir} ;; | |
ac0289ba VS |
217 | i386-efi) efi32_dir=${override_dir} ;; |
218 | x86_64-efi) efi64_dir=${override_dir} ;; | |
efda854e RM |
219 | esac |
220 | fi | |
fc2208b0 | 221 | |
13ac4305 VS |
222 | # obtain date-based UUID |
223 | iso_uuid=$(date -u +%Y-%m-%d-%H-%M-%S-00) | |
224 | grub_mkisofs_arguments="${grub_mkisofs_arguments} --modification-date=$(echo ${iso_uuid} | sed -e s/-//g)" | |
225 | ||
52cc3ce0 | 226 | # build BIOS core.img |
efda854e | 227 | if test -e "${pc_dir}" ; then |
52cc3ce0 | 228 | echo "Enabling BIOS support ..." |
33e2e6f3 | 229 | core_img=`mktemp "$MKTEMP_TEMPLATE"` |
26966aeb | 230 | $grub_mkimage -O i386-pc -d ${pc_dir}/ -o ${core_img} --prefix=/boot/grub/i386-pc \ |
b15937b1 | 231 | iso9660 biosdisk |
fc2208b0 | 232 | cat ${pc_dir}/cdboot.img ${core_img} > ${iso9660_dir}/boot/grub/i386-pc/eltorito.img |
0ae56929 | 233 | |
33e2e6f3 | 234 | embed_img=`mktemp "$MKTEMP_TEMPLATE"` |
e9309813 | 235 | cat ${pc_dir}/boot.img ${core_img} > ${embed_img} |
0ae56929 | 236 | |
fc2208b0 RM |
237 | rm -f ${core_img} |
238 | ||
239 | modules="$(cat ${pc_dir}/partmap.lst) ${modules}" | |
240 | (for i in ${modules} ; do | |
241 | echo "insmod $i" | |
242 | done ; \ | |
243 | echo "source /boot/grub/grub.cfg") \ | |
244 | > ${iso9660_dir}/boot/grub/i386-pc/grub.cfg | |
245 | ||
47779711 | 246 | grub_mkisofs_arguments="${grub_mkisofs_arguments} -b boot/grub/i386-pc/eltorito.img -no-emul-boot -boot-info-table \ |
0ae56929 | 247 | --embedded-boot ${embed_img}" |
fc2208b0 RM |
248 | fi |
249 | ||
7e70395f VS |
250 | # build multiboot core.img |
251 | make_image "${multiboot_dir}" i386-multiboot "${iso9660_dir}/boot/multiboot.img" "ata at_keyboard" | |
252 | ||
260ddb94 VS |
253 | if test -e "${efi64_dir}" || test -e "${efi32_dir}"; then |
254 | efi_dir=`mktemp -d "$MKTEMP_TEMPLATE"` | |
255 | mkdir -p "${efi_dir}/efi/boot" | |
13ac4305 | 256 | |
7e70395f VS |
257 | # build bootx64.efi |
258 | make_image "${efi64_dir}" x86_64-efi "${efi_dir}"/efi/boot/bootx64.efi "" | |
259 | # build bootia32.efi | |
260 | make_image "${efi32_dir}" i386-efi "${efi_dir}"/efi/boot/bootia32.efi "" | |
13ac4305 | 261 | |
260ddb94 VS |
262 | mformat -C -f 2880 -L 16 -i "${iso9660_dir}"/efi.img :: |
263 | mcopy -s -i "${iso9660_dir}"/efi.img ${efi_dir}/efi ::/ | |
264 | grub_mkisofs_arguments="${grub_mkisofs_arguments} --efi-boot efi.img" | |
ac0289ba VS |
265 | fi |
266 | ||
7e70395f VS |
267 | make_image "${qemu_dir}" i386-qemu "${iso9660_dir}/boot/qemu.img" "ata at_keyboard" |
268 | if [ -e "${iso9660_dir}/boot/qemu.img" ] && [ -d "${rom_directory}" ]; then | |
269 | cp "${iso9660_dir}/boot/qemu.img" "${rom_directory}/qemu.img" | |
270 | fi | |
271 | make_image "${coreboot_dir}" i386-coreboot "${iso9660_dir}/boot/coreboot.elf" "ata at_keyboard" | |
272 | if [ -e "${iso9660_dir}/boot/coreboot.elf" ] && [ -d "${rom_directory}" ]; then | |
273 | cp "${iso9660_dir}/boot/coreboot.elf" "${rom_directory}/coreboot.elf" | |
274 | fi | |
275 | ||
fc2208b0 | 276 | # build iso image |
a6085973 | 277 | "${xorriso}" -as mkisofs -graft-points ${grub_mkisofs_arguments} --protective-msdos-label -o ${output_image} -r ${iso9660_dir} --sort-weight 0 / --sort-weight 1 /boot ${source} |
fc2208b0 RM |
278 | rm -rf ${iso9660_dir} |
279 | ||
0ae56929 RM |
280 | rm -f ${embed_img} |
281 | ||
fc2208b0 | 282 | exit 0 |