3 # Copyright 2013-2014 Intel Corporation
4 # Author: Artem Bityutskiy
7 PROG="setup-gummiboot-conf"
10 srcdir="$(readlink -ev -- ${0%/*})"
11 if [ -f "$srcdir/setup-ivi-sh-functions" ]; then
12 . "$srcdir/setup-ivi-sh-functions"
14 . /usr/share/setup-ivi/setup-ivi-sh-functions
17 # This is a small trick which I use to make sure my scripts are portable -
18 # check if 'dash' is present, and if yes - use it.
19 if can_switch_to_dash; then
20 exec dash -euf -- "$srcdir/$PROG" "$@"
24 # Common preparations for all subcommands.
27 verbose "Boot directory is $bootdir"
28 [ -d "$bootdir" ] || \
29 fatal "boot directory path \"$bootdir\" does not exist"
31 # The gummiboot configuration directory
32 conf_dir="$bootdir/loader"
33 # The gummiboot configuration file
34 conf_file="$conf_dir/loader.conf"
35 # The gummiboot kernel entries directory
36 entries_dir="$bootdir/loader/entries"
39 # Get a regular expression for matching gummiboot configuration file option
43 local opt="$(esc_regexp "$1")"
45 opt="$(case_insensitive_regexp "$opt")"
46 printf "%s" "\(^[[:blank:]]*$opt[[:blank:]]\+\)\([^[:blank:]]\+\)\([[:blank:]]*$\)"
49 # Create the default gummiboot configuration file.
50 create_default_conf_file()
52 verbose "creating the default configuration file \"$conf_file\""
54 mkdir -p $verbose -- "$conf_dir" >&2
55 cat > "$conf_file" <<-EOF
63 # Check wheter the gummiboot configuration file exist, and if not:
64 # o create the default one if --force option was specified
65 # o fail if no --force option was specified
66 check_and_create_default_conf_file()
68 if [ -s "$conf_file" ]; then
72 if [ -n "$force" ]; then
73 create_default_conf_file "$1"
75 fatal "cannot find the gummiboot configuration file" \
76 "(\"$conf_file\") (use -f to create the default" \
82 # -----------------------------------------------------------------------------
83 # The "add" subcommand
84 # -----------------------------------------------------------------------------
90 Usage: $PROG add [options] <entry> <title> <kernel> <options>
92 Add a new gummiboot entry. The mandatory arguments are:
94 <entry> - name of the gummiboot entry to add
95 (<bootdir>/loader/entries/<entry>.conf)
96 <title> - the title of the entry
97 <kernel> - name of the kernel binary to add the entry for
99 <options> - kernel boot options
102 -f, --force if the entry already exists - re-write it, if
103 <bootdir>/loader/loader.conf does not exist - create one,
104 if <bootdir>/<kernel> does not exist - do not fail
106 add a splash field to the entry file. <file> is a
107 mandatory parameter to specify splash image location.
108 -h, --help show this text and exit
112 show_add_usage_fail()
114 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
121 if [ "$#" -eq 0 ]; then
127 tmp=`getopt -n $PROG -o f,s:,h --long force,splash:,help -- "$@"` ||
128 show_add_usage_fail "cannot parse command-line options"
147 *) show_add_usage_fail "unrecognized option \"$1\""
153 if [ "$#" -lt 4 ]; then
154 show_add_usage_fail "too little arguments"
156 if [ "$#" -gt 4 ]; then
157 show_add_usage_fail "too many arguments: \"$1\""
159 if [ -n "$splash" ] && ! [ -f "$splash" ]; then
160 show_add_usage_fail "splash file not found: \"$splash\""
165 local entry="$1"; shift
166 local title="$1"; shift
167 local kernel="$1"; shift
168 local options="$1"; shift
169 local kernel_path="$bootdir/$kernel"
170 local entry_path="$entries_dir/${entry}.conf"
172 verbose "entry is \"$entry\""
173 verbose "title is \"$title\""
174 verbose "kernel is \"$kernel\""
175 verbose "options are \"$options\""
177 if [ -f "$entry_path" ] && [ -z "$force" ]; then
178 fatal "gummiboot entry \"$entry_path\" already exists" \
179 "(use -f to force re-creating it)"
182 if ! [ -f "$kernel_path" ] && [ -z "$force" ]; then
183 fatal "cannot find kernel \"$kernel_path\"" \
184 "(use -f to ignore this error)"
187 # Make sure the gummiboot configuration file exists
188 check_and_create_default_conf_file "$entry"
190 # Find out the kernel version from its name
191 local kernel_version="$(printf "%s" "$kernel" | LC_ALL=C \
192 sed -e 's/[^[:digit:]]\+-\([[:digit:]]\+.*\)/\1/')"
193 [ -n "$kernel_version" ] || \
194 fatal "cannot fetch kernel version from \"$kernel\""
196 # Create the new entry
197 mkdir -p $verbose -- "$entries_dir" >&2
198 cat > "$entry_path" <<-EOF
201 version $kernel_version
206 # Add an optional splash image entry if the image file exists
207 if [ -n "$splash" ]; then
208 cp $verbose -- "$splash" "$entries_dir"
209 printf "%s\n" "splash \\loader\\entries\\${splash##*/}" \
213 if [ -n "$verbose" ]; then
214 verbose "contents of \"$entry_path\":"
215 cat -- "$entry_path" >&2
220 # -----------------------------------------------------------------------------
221 # The "remove" subcommand
222 # -----------------------------------------------------------------------------
228 Usage: $PROG remove [options] <entry>
230 Delete gummiboot entry <entry> (<bootdir>/loader/entries/<entry>.conf).
233 -f, --force do not fail if the entry doesn't exist
234 -h, --help show this text and exit
238 show_remove_usage_fail()
240 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
241 show_remove_usage >&2
247 if [ "$#" -eq 0 ]; then
253 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
254 show_remove_usage_fail "cannot parse command-line options"
269 *) show_remove_usage_fail "unrecognized option \"$1\""
275 if [ "$#" -lt 1 ]; then
276 show_remove_usage_fail "too little arguments"
278 if [ "$#" -gt 1 ]; then
279 show_remove_usage_fail "too many arguments: \"$1\""
285 local entry_path="$entries_dir/${entry}.conf"
287 if ! [ -f "$entry_path" ] && [ -z "$force" ]; then
288 fatal "gummiboot entry \"$entry_path\" doesn't exist" \
289 "(use -f to ignore this error)"
292 rm -rf $verbose -- "$entry_path" >&2
293 verbose "removed $entry_path"
297 # -----------------------------------------------------------------------------
298 # The "default" subcommand
299 # -----------------------------------------------------------------------------
302 # Get the kernel name from a gummiboot entry
303 get_kernel_from_entry()
306 local entry="$entries_dir/$1.conf"
308 if [ -f "$entry" ]; then
309 local regexp="$(get_regexp "efi")"
310 local result="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" -- \
313 if [ -z "$result" ]; then
314 regexp="$(get_regexp "linux")"
315 result="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" -- \
318 [ -n "$result" ] || return 0
322 printf "%s" "${result##*/}"
328 Usage: $PROG default [options] <entry>
330 Set the default boot kernel to <entry>, which is the gummiboot entry name to
331 become the default (without the ".conf" suffix, like in
332 <bootdir>/loader/entries/<entry>.conf). If <entry> is omited, this command
333 prints the currently default entry name.
336 -f, --force if <bootdir>/loader/loader.conf does not exist - create the
337 default one, if <bootdir>/loader/entries/<entry>.conf does not
339 -h, --help show this text and exit
343 show_default_usage_fail()
345 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
346 show_default_usage >&2
353 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
354 show_default_usage_fail "cannot parse command-line options"
369 *) show_default_usage_fail "unrecognized option \"$1\""
375 if [ "$#" -gt 1 ]; then
376 show_default_usage_fail "too many arguments: \"$1\""
381 local entry="${1:-}";
383 # Make sure the gummiboot configuration file exists
384 check_and_create_default_conf_file "$entry"
386 # Find the current default entry
387 local regexp="$(get_regexp "default")"
388 local default_entry="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" -- \
391 if [ -z "$entry" ]; then
392 printf "%s\n" "entry: $default_entry"
393 printf "%s\n" "kernel: $(get_kernel_from_entry "$default_entry")"
397 local entry_path="$entries_dir/${entry}.conf"
398 if ! [ -f "$entry_path" ] && [ -z "$force" ]; then
399 fatal "cannot find the gummiboot entry \"$entry_path\"" \
400 "(use -f to ignore this error)"
403 if [ -z "$default_entry" ]; then
404 verbose "no default entry found, adding \"$entry\""
405 printf "%s" "default $entry" >> "$conf_file"
409 [ "$(printf "%s\n" "$default_entry" | wc -l)" -eq "1" ] || \
410 fatal "more than one default entry in \"$conf_file\""
412 # Escape special sed characters in "$entry" and replace the old default
413 # entry with the new one
414 local entry_esc="$(esc_sed_replacement "$entry")"
415 LC_ALL=C sed -i -e "s/$regexp/\1$entry_esc\3/" -- "$conf_file"
417 verbose "set the default boot kernel to \"$entry"\"
421 # -----------------------------------------------------------------------------
427 Usage: $PROG [options] <subcommand> [options] <arguments>
429 This program changes the gummiboot bootloader configuration. Supported
431 add - add a gummiboot entry for a kernel
432 remove - remove a gummiboot entry
433 default - get or set the default gummiboot entry
435 Run "$PROG <subcommand>" to see subcommand-specific help.
438 -b, --bootdir path to the boot directory (default is "/boot")
439 --version show the program version and exit
440 -v, --verbose be verbose
441 -h, --help show this text and exit
447 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
454 while [ -n "${1:-""}" ] && [ -z "${1##-*}" ]; do
458 # If there is no argument or it starts with "-", complain
459 if [ -z "${1:-""}" ] || [ -z "${1##-*}" ]; then
460 fatal "--bootdir requires an argument"
477 *) show_usage_fail "unrecognized option \"$1\""
485 if [ "$#" -eq 0 ]; then
491 subcommand="$1"; shift
493 case "$subcommand" in
499 remove_subcommand "$@"
503 default_subcommand "$@"
506 *) show_usage_fail "unrecognized subcommand \"$subcommand\""