3 # Copyright 2013 Intel Corporation
4 # Author: Artem Bityutskiy
7 PROG="setup-gummiboot-conf"
10 srcdir="$(readlink -ev -- ${0%/*})"
11 PATH="/usr/share/setup-ivi:$srcdir:$PATH"
13 . setup-ivi-sh-functions
15 # This is a small trick which I use to make sure my scripts are portable -
16 # check if 'dash' is present, and if yes - use it.
17 if can_switch_to_dash; then
18 exec dash -euf "$srcdir/$PROG" "$@"
22 # Common preparations for all subcommands.
25 verbose "Boot directory is $bootdir"
26 [ -d "$bootdir" ] || \
27 fatal "boot directory path \"$bootdir\" does not exist"
29 # The gummiboot configuration directory
30 conf_dir="$bootdir/loader"
31 # The gummiboot configuration file
32 conf_file="$conf_dir/loader.conf"
33 # The gummiboot kernel entries directory
34 entries_dir="$bootdir/loader/entries"
37 # Get a regular expression for matching gummiboot configuration file option
41 local opt="$(esc_regexp "$1")"
43 opt="$(case_insensitive_regexp "$opt")"
44 printf "%s" "\(^[[:blank:]]*$opt[[:blank:]]\+\)\([^[:blank:]]\+\)\([[:blank:]]*$\)"
47 # Create the default gummiboot configuration file.
48 create_default_conf_file()
50 verbose "creating the default configuration file \"$conf_file\""
52 mkdir -p $verbose "$conf_dir" >&2
53 cat > "$conf_file" <<-EOF
59 # Check wheter the gummiboot configuration file exist, and if not:
60 # o create the default one if --force option was specified
61 # o fail if no --force option was specified
62 check_and_create_default_conf_file()
64 if [ -s "$conf_file" ]; then
68 if [ -n "$force" ]; then
69 create_default_conf_file "$1"
71 fatal "cannot find the gummiboot configuration file" \
72 "(\"$conf_file\") (use -f to create the default" \
78 # -----------------------------------------------------------------------------
79 # The "add" subcommand
80 # -----------------------------------------------------------------------------
86 Usage: $PROG add [options] <entry> <title> <kernel> <options>
88 Add a new gummiboot entry. The mandatory arguments are:
90 <entry> - name of the gummiboot entry to add
91 (<bootdir>/loader/entries/<entry>.conf)
92 <title> - the title of the entry
93 <kernel> - name of the kernel binary to add the entry for
95 <options> - kernel boot options
98 -f, --force if the entry already exists - re-write it, if
99 <bootdir>/loader/loader.conf does not exist - create one,
100 if <bootdir>/<kernel> does not exist - do not fail
101 -h, --help show this text and exit
105 show_add_usage_fail()
107 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
114 if [ "$#" -eq 0 ]; then
120 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
121 show_add_usage_fail "cannot parse command-line options"
136 *) show_add_usage_fail "unrecognized option \"$1\""
142 if [ "$#" -lt 4 ]; then
143 show_add_usage_fail "too little arguments"
145 if [ "$#" -gt 4 ]; then
146 show_add_usage_fail "too many arguments: \"$1\""
151 local entry="$1"; shift
152 local title="$1"; shift
153 local kernel="$1"; shift
154 local options="$1"; shift
155 local kernel_path="$bootdir/$kernel"
156 local entry_path="$entries_dir/${entry}.conf"
158 verbose "entry is \"$entry\""
159 verbose "title is \"$title\""
160 verbose "kernel is \"$kernel\""
161 verbose "options are \"$options\""
163 if [ -f "$entry_path" ] && [ -z "$force" ]; then
164 fatal "gummiboot entry \"$entry_path\" already exists" \
165 "(use -f to force re-creating it)"
168 if ! [ -f "$kernel_path" ] && [ -z "$force" ]; then
169 fatal "cannot find kernel \"$kernel_path\"" \
170 "(use -f to ignore this error)"
173 # Make sure the gummiboot configuration file exists
174 check_and_create_default_conf_file "$entry"
176 # Find out the kernel version from its name
177 local kernel_version="$(printf "%s" "$kernel" | LC_ALL=C \
178 sed -e 's/[^[:digit:]]\+-\([[:digit:]]\+.*\)/\1/')"
179 [ -n "$kernel_version" ] || \
180 fatal "cannot fetch kernel version from \"$kernel\""
182 # Create the new entry
183 mkdir -p $verbose "$entries_dir" >&2
184 cat > "$entry_path" <<-EOF
186 version $kernel_version
191 if [ -n "$verbose" ]; then
192 verbose "contents of \"$entry_path\":"
193 cat "$entry_path" >&2
198 # -----------------------------------------------------------------------------
199 # The "remove" subcommand
200 # -----------------------------------------------------------------------------
206 Usage: $PROG remove [options] <entry>
208 Delete gummiboot entry <entry> (<bootdir>/loader/entries/<entry>.conf).
211 -f, --force do not fail if the entry doesn't exist
212 -h, --help show this text and exit
216 show_remove_usage_fail()
218 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
219 show_remove_usage >&2
225 if [ "$#" -eq 0 ]; then
231 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
232 show_remove_usage_fail "cannot parse command-line options"
247 *) show_remove_usage_fail "unrecognized option \"$1\""
253 if [ "$#" -lt 1 ]; then
254 show_remove_usage_fail "too little arguments"
256 if [ "$#" -gt 1 ]; then
257 show_remove_usage_fail "too many arguments: \"$1\""
263 local entry_path="$entries_dir/${entry}.conf"
265 if ! [ -f "$entry_path" ] && [ -z "$force" ]; then
266 fatal "gummiboot entry \"$entry_path\" doesn't exist" \
267 "(use -f to ignore this error)"
270 rm -rf $verbose "$entry_path" >&2
271 verbose "removed $entry_path"
275 # -----------------------------------------------------------------------------
276 # The "default" subcommand
277 # -----------------------------------------------------------------------------
280 # Get the kernel name from a gummiboot entry
281 get_kernel_from_entry()
284 local entry="$entries_dir/$1.conf"
286 if [ -f "$entry" ]; then
287 local regexp="$(get_regexp "efi")"
288 local result="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" "$entry")"
290 if [ -z "$result" ]; then
291 regexp="$(get_regexp "linux")"
292 result="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" "$entry")"
294 [ -n "$result" ] || return 0
298 printf "%s" "${result##*/}"
304 Usage: $PROG default [options] <entry>
306 Set the default boot kernel to <entry>, which is the gummiboot entry name to
307 become the default (without the ".conf" suffix, like in
308 <bootdir>/loader/entries/<entry>.conf). If <entry> is omited, this command
309 prints the currently default entry name.
312 -f, --force if <bootdir>/loader/loader.conf does not exist - create the
313 default one, if <bootdir>/loader/entries/<entry>.conf does not
315 -h, --help show this text and exit
319 show_default_usage_fail()
321 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
322 show_default_usage >&2
329 tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
330 show_default_usage_fail "cannot parse command-line options"
345 *) show_default_usage_fail "unrecognized option \"$1\""
351 if [ "$#" -gt 1 ]; then
352 show_default_usage_fail "too many arguments: \"$1\""
357 local entry="${1:-}";
359 # Make sure the gummiboot configuration file exists
360 check_and_create_default_conf_file "$entry"
362 # Find the current default entry
363 local regexp="$(get_regexp "default")"
364 local default_entry="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" "$conf_file")"
366 if [ -z "$entry" ]; then
367 printf "%s\n" "entry: $default_entry"
368 printf "%s\n" "kernel: $(get_kernel_from_entry "$default_entry")"
372 local entry_path="$entries_dir/${entry}.conf"
373 if ! [ -f "$entry_path" ] && [ -z "$force" ]; then
374 fatal "cannot find the gummiboot entry \"$entry_path\"" \
375 "(use -f to ignore this error)"
378 if [ -z "$default_entry" ]; then
379 verbose "no default entry found, adding \"$entry\""
380 printf "%s" "default $entry" >> "$conf_file"
384 [ "$(printf "%s\n" "$default_entry" | wc -l)" -eq "1" ] || \
385 fatal "more than one default entry in \"$conf_file\""
387 # Escape special sed characters in "$entry" and replace the old default
388 # entry with the new one
389 local entry_esc="$(esc_sed_replacement "$entry")"
390 LC_ALL=C sed -i -e "s/$regexp/\1$entry_esc\3/" "$conf_file"
392 verbose "set the default boot kernel to \"$entry"\"
396 # -----------------------------------------------------------------------------
402 Usage: $PROG [options] <subcommand> [options] <arguments>
404 This program changes the gummiboot bootloader configuration. Supported
406 add - add a gummiboot entry for a kernel
407 remove - remove a gummiboot entry
408 default - get or set the default gummiboot entry
410 Run "$PROG <subcommand>" to see subcommand-specific help.
413 -b, --bootdir path to the boot directory (default is "/boot")
414 --version show the program version and exit
415 -v, --verbose be verbose
416 -h, --help show this text and exit
422 IFS= printf "%s\n\n" "$PROG: error: $*" >&2
429 while [ -n "${1:-""}" ] && [ -z "${1##-*}" ]; do
433 # If there is no argument or it starts with "-", complain
434 if [ -z "${1:-""}" ] || [ -z "${1##-*}" ]; then
435 fatal "--bootdir requires an argument"
452 *) show_usage_fail "unrecognized option \"$1\""
460 if [ "$#" -eq 0 ]; then
466 subcommand="$1"; shift
468 case "$subcommand" in
474 remove_subcommand "$@"
478 default_subcommand "$@"
481 *) show_usage_fail "unrecognized subcommand \"$subcommand\""