8d46fc973c1e2f84cef952a5a2c51109f27957a7
[grub.git] / util / grub.d / 00_header.in
1 #! /bin/sh
2 set -e
3
4 # grub-mkconfig helper script.
5 # Copyright (C) 2006,2007,2008,2009,2010  Free Software Foundation, Inc.
6 #
7 # GRUB is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # GRUB is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
19
20 prefix="@prefix@"
21 exec_prefix="@exec_prefix@"
22 datarootdir="@datarootdir@"
23 grub_lang=`echo $LANG | cut -d . -f 1`
24
25 export TEXTDOMAIN=@PACKAGE@
26 export TEXTDOMAINDIR="@localedir@"
27
28 . "$pkgdatadir/grub-mkconfig_lib"
29
30 # Do this as early as possible, since other commands might depend on it.
31 # (e.g. the `loadfont' command might need lvm or raid modules)
32 for i in ${GRUB_PRELOAD_MODULES} ; do
33   echo "insmod $i"
34 done
35
36 if [ "x${GRUB_DEFAULT}" = "x" ] ; then GRUB_DEFAULT=0 ; fi
37 if [ "x${GRUB_DEFAULT}" = "xsaved" ] ; then GRUB_DEFAULT='${saved_entry}' ; fi
38 if [ "x${GRUB_TIMEOUT}" = "x" ] ; then GRUB_TIMEOUT=5 ; fi
39 if [ "x${GRUB_GFXMODE}" = "x" ] ; then GRUB_GFXMODE=auto ; fi
40
41 if [ "x${GRUB_DEFAULT_BUTTON}" = "x" ] ; then GRUB_DEFAULT_BUTTON="$GRUB_DEFAULT" ; fi
42 if [ "x${GRUB_DEFAULT_BUTTON}" = "xsaved" ] ; then GRUB_DEFAULT_BUTTON='${saved_entry}' ; fi
43 if [ "x${GRUB_TIMEOUT_BUTTON}" = "x" ] ; then GRUB_TIMEOUT_BUTTON="$GRUB_TIMEOUT" ; fi
44
45 cat << EOF
46 if [ -s \$prefix/grubenv ]; then
47   load_env
48 fi
49 EOF
50 if [ "x$GRUB_BUTTON_CMOS_ADDRESS" != "x" ]; then
51     cat <<EOF
52 if cmostest $GRUB_BUTTON_CMOS_ADDRESS ; then
53    set default="${GRUB_DEFAULT_BUTTON}"
54 elif [ "\${next_entry}" ] ; then
55    set default="\${next_entry}"
56    set next_entry=
57    save_env next_entry
58    set boot_once=true
59 else
60    set default="${GRUB_DEFAULT}"
61 fi
62 EOF
63 else
64     cat <<EOF
65 if [ "\${next_entry}" ] ; then
66    set default="\${next_entry}"
67    set next_entry=
68    save_env next_entry
69    set boot_once=true
70 else
71    set default="${GRUB_DEFAULT}"
72 fi
73 EOF
74 fi
75 cat <<EOF
76
77 if [ x"\${feature_menuentry_id}" = xy ]; then
78   menuentry_id_option="--id"
79 else
80   menuentry_id_option=""
81 fi
82
83 export menuentry_id_option
84
85 if [ "\${prev_saved_entry}" ]; then
86   set saved_entry="\${prev_saved_entry}"
87   save_env saved_entry
88   set prev_saved_entry=
89   save_env prev_saved_entry
90   set boot_once=true
91 fi
92
93 function savedefault {
94   if [ -z "\${boot_once}" ]; then
95     saved_entry="\${chosen}"
96     save_env saved_entry
97   fi
98 }
99
100 function load_video {
101 EOF
102 if [ -n "${GRUB_VIDEO_BACKEND}" ]; then
103     cat <<EOF
104   insmod ${GRUB_VIDEO_BACKEND}
105 EOF
106 else
107 # If all_video.mod isn't available load all modules available
108 # with versions prior to introduction of all_video.mod
109 cat <<EOF
110   if [ x\$feature_all_video_module = xy ]; then
111     insmod all_video
112   else
113     insmod efi_gop
114     insmod efi_uga
115     insmod ieee1275_fb
116     insmod vbe
117     insmod vga
118     insmod video_bochs
119     insmod video_cirrus
120   fi
121 EOF
122 fi
123 cat <<EOF
124 }
125
126 EOF
127
128 serial=0;
129 gfxterm=0;
130 for x in ${GRUB_TERMINAL_INPUT} ${GRUB_TERMINAL_OUTPUT}; do
131     if [ xserial = "x$x" ]; then
132         serial=1;
133     fi
134     if [ xgfxterm = "x$x" ]; then
135         gfxterm=1;
136     fi
137 done
138
139 if [ "x$serial" = x1 ]; then
140     if [ "x${GRUB_SERIAL_COMMAND}" = "x" ] ; then
141         grub_warn "$(gettext "Requested serial terminal but GRUB_SERIAL_COMMAND is unspecified. Default parameters will be used.")"
142         GRUB_SERIAL_COMMAND=serial
143     fi
144     echo "${GRUB_SERIAL_COMMAND}"
145 fi
146
147 if [ "x$gfxterm" = x1 ]; then
148     if [ -n "$GRUB_FONT" ] ; then
149        # Make the font accessible
150        prepare_grub_to_access_device `${grub_probe} --target=device "${GRUB_FONT}"`
151     cat << EOF
152 if loadfont `make_system_path_relative_to_its_root "${GRUB_FONT}"` ; then
153 EOF
154     else
155         for dir in "${pkgdatadir}" "`echo '/@bootdirname@/@grubdirname@' | sed "s,//*,/,g"`" /usr/share/grub ; do
156             for basename in unicode unifont ascii; do
157                 path="${dir}/${basename}.pf2"
158                 if is_path_readable_by_grub "${path}" > /dev/null ; then
159                     font_path="${path}"
160                 else
161                     continue
162                 fi
163                 break 2
164             done
165         done
166         if [ -n "${font_path}" ] ; then
167     cat << EOF
168 if [ x\$feature_default_font_path = xy ] ; then
169    font=unicode
170 else
171 EOF
172                 # Make the font accessible
173                 prepare_grub_to_access_device `${grub_probe} --target=device "${font_path}"`
174     cat << EOF
175     font="`make_system_path_relative_to_its_root "${font_path}"`"
176 fi
177
178 if loadfont \$font ; then
179 EOF
180             else
181     cat << EOF
182 if loadfont unicode ; then
183 EOF
184             fi
185         fi
186
187     cat << EOF
188   set gfxmode=${GRUB_GFXMODE}
189   load_video
190   insmod gfxterm
191 EOF
192
193 # Gettext variables and module
194 if [ "x${LANG}" != "xC" ] &&  [ "x${LANG}" != "x" ]; then
195   cat << EOF
196   set locale_dir=\$prefix/locale
197   set lang=${grub_lang}
198   insmod gettext
199 EOF
200 fi
201
202 cat <<EOF
203 fi
204 EOF
205 fi
206
207 case x${GRUB_TERMINAL_INPUT} in
208   x)
209     # Just use the native terminal
210   ;;
211   x*)
212     cat << EOF
213 terminal_input ${GRUB_TERMINAL_INPUT}
214 EOF
215   ;;
216 esac
217
218 case x${GRUB_TERMINAL_OUTPUT} in
219   x)
220     # Just use the native terminal
221   ;;
222   x*)
223     cat << EOF
224 terminal_output --append ${GRUB_TERMINAL_OUTPUT}
225 EOF
226   ;;
227 esac
228
229 if [ "x$gfxterm" = x1 ]; then
230     if [ "x$GRUB_THEME" != x ] && [ -f "$GRUB_THEME" ] \
231         && is_path_readable_by_grub "$GRUB_THEME"; then
232         gettext_printf "Found theme: %s\n" "$GRUB_THEME" >&2
233
234         prepare_grub_to_access_device `${grub_probe} --target=device "$GRUB_THEME"`
235         cat << EOF
236 insmod gfxmenu
237 EOF
238         themedir="`dirname "$GRUB_THEME"`"
239         for x in "$themedir"/*.pf2 "$themedir"/f/*.pf2; do
240             if [ -f "$x" ]; then
241                 cat << EOF
242 loadfont (\$root)`make_system_path_relative_to_its_root $x`
243 EOF
244             fi
245         done
246         if [ x"`echo "$themedir"/*.jpg`" != x"$themedir/*.jpg" ] || [ x"`echo "$themedir"/*.jpeg`" != x"$themedir/*.jpeg" ]; then
247             cat << EOF
248 insmod jpeg
249 EOF
250         fi
251         if [ x"`echo "$themedir"/*.png`" != x"$themedir/*.png" ]; then
252             cat << EOF
253 insmod png
254 EOF
255         fi
256         if [ x"`echo "$themedir"/*.tga`" != x"$themedir/*.tga" ]; then
257             cat << EOF
258 insmod tga
259 EOF
260         fi
261             
262         cat << EOF
263 set theme=(\$root)`make_system_path_relative_to_its_root $GRUB_THEME`
264 export theme
265 EOF
266     elif [ "x$GRUB_BACKGROUND" != x ] && [ -f "$GRUB_BACKGROUND" ] \
267             && is_path_readable_by_grub "$GRUB_BACKGROUND"; then
268         gettext_printf "Found background: %s\n" "$GRUB_BACKGROUND" >&2
269         case "$GRUB_BACKGROUND" in 
270             *.png)         reader=png ;;
271             *.tga)         reader=tga ;;
272             *.jpg|*.jpeg)  reader=jpeg ;;
273             *)             gettext "Unsupported image format" >&2; echo >&2; exit 1 ;;
274         esac
275         prepare_grub_to_access_device `${grub_probe} --target=device "$GRUB_BACKGROUND"`
276         cat << EOF
277 insmod $reader
278 background_image -m stretch `make_system_path_relative_to_its_root "$GRUB_BACKGROUND"`
279 EOF
280     fi
281 fi
282
283 make_timeout ()
284 {
285     if [ "x${3}" != "x" ] ; then
286         timeout="${2}"
287         style="${3}"
288     elif [ "x${1}" != "x" ] && [ "x${1}" != "x0" ] ; then
289         # Handle the deprecated GRUB_HIDDEN_TIMEOUT scheme.
290         timeout="${1}"
291         if [ "x${2}" != "x0" ] ; then
292             grub_warn "$(gettext "Setting GRUB_TIMEOUT to a non-zero value when GRUB_HIDDEN_TIMEOUT is set is no longer supported.")"
293         fi
294         if [ "x${GRUB_HIDDEN_TIMEOUT_QUIET}" = "xtrue" ] ; then
295             style="hidden"
296             verbose=
297         else
298             style="countdown"
299             verbose=" --verbose"
300         fi
301     else
302         # No hidden timeout, so treat as GRUB_TIMEOUT_STYLE=menu
303         timeout="${2}"
304         style="menu"
305     fi
306     cat << EOF
307 if [ x\$feature_timeout_style = xy ] ; then
308   set timeout_style=${style}
309   set timeout=${timeout}
310 EOF
311     if [ "x${style}" = "xmenu" ] ; then
312         cat << EOF
313 # Fallback normal timeout code in case the timeout_style feature is
314 # unavailable.
315 else
316   set timeout=${timeout}
317 EOF
318     else
319         cat << EOF
320 # Fallback hidden-timeout code in case the timeout_style feature is
321 # unavailable.
322 elif sleep${verbose} --interruptible ${timeout} ; then
323   set timeout=0
324 EOF
325     fi
326     cat << EOF
327 fi
328 EOF
329 }
330
331 if [ "x$GRUB_BUTTON_CMOS_ADDRESS" != "x" ]; then
332     cat <<EOF
333 if cmostest $GRUB_BUTTON_CMOS_ADDRESS ; then
334 EOF
335 make_timeout "${GRUB_HIDDEN_TIMEOUT_BUTTON}" "${GRUB_TIMEOUT_BUTTON}" "${GRUB_TIMEOUT_STYLE_BUTTON}"
336 echo else
337 make_timeout "${GRUB_HIDDEN_TIMEOUT}" "${GRUB_TIMEOUT}" "${GRUB_TIMEOUT_STYLE}"
338 echo fi
339 else
340 make_timeout "${GRUB_HIDDEN_TIMEOUT}" "${GRUB_TIMEOUT}" "${GRUB_TIMEOUT_STYLE}"
341 fi
342
343 if [ "x$GRUB_BUTTON_CMOS_ADDRESS" != "x" ] && [ "x$GRUB_BUTTON_CMOS_CLEAN" = "xyes" ]; then
344     cat <<EOF
345 cmosclean $GRUB_BUTTON_CMOS_ADDRESS
346 EOF
347 fi
348
349 # Play an initial tune
350 if [ "x${GRUB_INIT_TUNE}" != "x" ] ; then
351   echo "play ${GRUB_INIT_TUNE}"
352 fi
353
354 if [ "x${GRUB_BADRAM}" != "x" ] ; then
355   echo "badram ${GRUB_BADRAM}"
356 fi