setup-gummiboot-conf: Disable VGA output in splash-hdmi .conf
[platform/adaptation/setup-scripts.git] / setup-scripts-bootloader-conf
1 #!/bin/sh -euf
2
3 # Copyright 2013-2014 Intel Corporation
4 # Author: Artem Bityutskiy
5 # License: GPLv2
6
7 PROG="setup-scripts-bootloader-conf"
8 VER="1.0"
9
10 srcdir="$(readlink -ev -- ${0%/*})"
11 PATH="/usr/share/setup-scripts:$srcdir:$PATH"
12
13 if [ -f "$srcdir/setup-scripts-sh-functions" ]; then
14         . "$srcdir/setup-scripts-sh-functions"
15         . "$srcdir/installerfw-sh-functions"
16 else
17         .  /usr/share/setup-scripts/setup-scripts-sh-functions
18         .  /usr/share/setup-scripts/installerfw-sh-functions
19 fi
20
21 # This is a small trick which I use to make sure my scripts are portable -
22 # check if 'dash' is present, and if yes - use it.
23 if can_switch_to_dash; then
24         exec dash -euf -- "$srcdir/$PROG" "$@"
25         exit $?
26 fi
27
28 # Preparation stuff common for all subcommands
29 prepare()
30 {
31         # Get the installer framework environment
32         installerfw_restore_env
33
34         rootdir="$(installerfw_mnt_prefix "/")"
35         bootdir="$(installerfw_mnt_prefix "/boot")"
36
37         if installerfw_is_efi_boot_system; then
38                 boot="gummiboot"
39         else
40                 boot="extlinux"
41         fi
42 }
43
44 #
45 # -----------------------------------------------------------------------------
46 # The "add" subcommand
47 # -----------------------------------------------------------------------------
48 #
49
50 show_add_usage()
51 {
52         cat <<-EOF
53 Usage: $PROG add [options] <kernel>
54
55 Add bootloader entries for kernel <kernel>.
56
57 Options:
58   -f, --force  if bootloader entries for <kernel> already exist in bootloader's
59                config file(s) - re-write them, if <kernel> does not exist - do
60                not fail
61   -h, --help   show this text and exit
62 EOF
63 }
64
65 show_add_usage_fail()
66 {
67         IFS= printf "%s\n\n" "$PROG: error: $*" >&2
68         show_add_usage >&2
69         exit 1
70 }
71
72 add_subcommand()
73 {
74         if [ "$#" -eq 0  ]; then
75                 show_add_usage
76                 exit 0
77         fi
78
79         local tmp
80         tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
81                 show_add_usage_fail "cannot parse command-line options"
82         eval set -- "$tmp"
83
84         local force=
85         while true; do
86                 case "$1" in
87                 -f|--force)
88                         force="-f"
89                         ;;
90                 -h|--help)
91                         show_add_usage
92                         exit 0
93                         ;;
94                 --) shift; break
95                         ;;
96                 *) show_add_usage_fail "unrecognized option \"$1\""
97                         ;;
98                 esac
99                 shift
100         done
101
102         if [ "$#" -lt 1 ]; then
103                 show_add_usage_fail "please, specify the kernel"
104         fi
105         if [ "$#" -gt 1 ]; then
106                 show_add_usage_fail "too many arguments: \"$1\""
107         fi
108
109         prepare
110
111         local kernel="$1"
112         local kernel_path="$bootdir/$kernel"
113
114         if ! [ -f "$kernel_path" ] && [ -z "$force" ]; then
115                 fatal "cannot find kernel \"$kernel_path\"" \
116                       "(use -f to ignore this error)"
117         fi
118
119         # Get root partition PARTUUID
120         installerfw_get_part_info "/" "PARTUUID" "root_partuuid"
121         [ -n "$root_partuuid" ] || \
122                 fatal "cannot find PARTUUID of the root partition"
123
124         # Get kernel options
125         local options="${INSTALLERFW_KERNEL_OPTS:-} root=PARTUUID=$root_partuuid"
126
127         # Get OS name
128         local os_name=
129         get_os_name "os_name"
130
131         # Add the default bootloader entry
132         setup-$boot-conf $verbose --bootdir "$bootdir" add $force \
133                 "$kernel" "$os_name" "$kernel" "$options"
134
135         # Add the debug bootloader entry. If there is the "quiet" option,
136         # create a non-quiet configuration.
137         local verbose_opts="$(printf "%s" "$options" | LC_ALL=C \
138                                 sed -e "s/[[:blank:]]\+quiet[[:blank:]]\+/ /
139                                         s/^quiet[[:blank:]]\+//
140                                         s/[[:blank:]]\+quiet$//
141                                         s/^quiet$//")"
142
143         local debug_opts="$verbose_opts ignore_loglevel log_buf_len=2M"
144         local debug_opts="$debug_opts initcall_debug"
145
146
147         setup-$boot-conf $verbose --bootdir "$bootdir" add \
148                 $force "$kernel-debug" "Debug $os_name" \
149                 "$kernel" "$verbose_opts"
150
151         # Add the clone bootloader entry, but only if the cloning tool is
152         # installed
153         if [ -f "$rootdir/usr/sbin/setup-scripts-clone" ]; then
154                 clone_opts="$options systemd.unit=scripts-clone.service"
155                 clone_opts="$clone_opts scripts-clone-target=autodetect"
156                 setup-$boot-conf $verbose --bootdir "$bootdir" add \
157                         $force "$kernel-clone" "Clone $os_name" \
158                         "$kernel" "$clone_opts"
159         fi
160
161         # Use default gummiboot-splash file
162         local splash_path="$rootdir/usr/share/gummiboot/splash.bmp"
163
164         # Add a splash entry for fastboot testing and disable fbcon
165         if [ "$boot" = "gummiboot" ] && [ -f "$splash_path" ]; then
166                 local splash_opts="$options i915.fastboot=1 fbcon=map:9"
167                 setup-$boot-conf $verbose --bootdir "$bootdir" add \
168                         $force --splash "$splash_path" "$kernel-splash" \
169                         "Splash $os_name" "$kernel" "$splash_opts"
170                 splash_opts_dp="$splash_opts video=HDMI-A-1:d"
171                 setup-$boot-conf $verbose --bootdir "$bootdir" add \
172                         $force --splash "$splash_path" "$kernel-splash-dp" \
173                         "Splash DP $os_name" "$kernel" "$splash_opts_dp"
174                 splash_opts_hdmi="$splash_opts video=DP-1:d video=VGA-1:d"
175                 setup-$boot-conf $verbose --bootdir "$bootdir" add \
176                         $force --splash "$splash_path" "$kernel-splash-hdmi" \
177                         "Splash HDMI $os_name" "$kernel" "$splash_opts_hdmi"
178         fi
179 }
180
181 #
182 # -----------------------------------------------------------------------------
183 # The "remove" subcommand
184 # -----------------------------------------------------------------------------
185 #
186
187 show_remove_usage()
188 {
189         cat <<-EOF
190 Usage: $PROG remove [options] <kernel>
191
192 Delete bootloader entries for kernel <kernel> (only those which were previously
193 created with "$PROG add").
194
195 Options:
196   -f, --force  do not fail if <kernel> does not have corresponding bootloader
197                entries
198   -h, --help   show this text and exit
199 EOF
200 }
201
202 show_remove_usage_fail()
203 {
204         IFS= printf "%s\n\n" "$PROG: error: $*" >&2
205         show_remove_usage >&2
206         exit 1
207 }
208
209 remove_subcommand()
210 {
211         if [ "$#" -eq 0  ]; then
212                 show_remove_usage
213                 exit 0
214         fi
215
216         local tmp
217         tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
218                 show_remove_usage_fail "cannot parse command-line options"
219         eval set -- "$tmp"
220
221         local force=
222         while true; do
223                 case "$1" in
224                 -f|--force)
225                         force="-f"
226                         ;;
227                 -h|--help)
228                         show_remove_usage
229                         exit 0
230                         ;;
231                 --) shift; break
232                         ;;
233                 *) show_remove_usage_fail "unrecognized option \"$1\""
234                         ;;
235                 esac
236                 shift
237         done
238
239         if [ "$#" -lt 1 ]; then
240                 show_add_usage_fail "please, specify the kernel"
241         fi
242         if [ "$#" -gt 1 ]; then
243                 show_remove_usage_fail "too many arguments: \"$1\""
244         fi
245
246         local kernel="$1"
247
248         prepare
249
250         # Get the current default entry
251         local default="$(setup-$boot-conf $verbose --bootdir "$bootdir" \
252                                               default $force)"
253         [ $? -eq 0 ] || \
254                 fatal "cannot get the default kernel, setup-$boot-conf failed"
255
256         local default_kernel="$(printf "%s" "$default" | LC_ALL=C \
257                          sed -n -e 's/^kernel: \(.\+\)$/\1/p')"
258
259         if [ -n "$default_kernel" ]; then
260                 verbose "current default boot kernel is " \
261                         "\"$default_kernel\""
262         else
263                 verbose "did not find the default kernel," \
264                       "setup-$boot-conf returned: $default"
265         fi
266
267         # Remove the kernel
268         setup-$boot-conf $verbose --bootdir "$bootdir" \
269                              remove $force "$kernel" || \
270                 fatal "setup-$boot-conf failed to remove" \
271                       "entry \"$kernel\""
272         setup-$boot-conf $verbose --bootdir "$bootdir" \
273                              remove $force "$kernel-debug" || \
274                 fatal "setup-$boot-conf failed to remove" \
275                       "entry \"$kernel-verbose\""
276         # The "clone" entry does not necessary exist, so use --force
277         setup-$boot-conf $verbose --bootdir "$bootdir" \
278                              remove --force "$kernel-clone" || \
279                 fatal "setup-$boot-conf failed to remove" \
280                       "entry \"$kernel-clone\""
281         # Ditto for "splash"
282         setup-$boot-conf $verbose --bootdir "$bootdir" \
283                              remove --force "$kernel-splash" || \
284                 fatal "setup-$boot-conf failed to remove" \
285                       "entry \"$kernel-splash\""
286
287         # If this is not the default kernel, we are done
288         [ "$kernel" = "$default_kernel" ] || return 0
289
290         # We've just removed the default kernel, find the kernel with the
291         # latest version and make it to be the default
292
293         verbose "removed the default kernel, find the newest available"
294
295         local newest_kernel="$(get_newest_kernel "$bootdir" "$kernel")"
296
297         if [ -z "$newest_kernel" ]; then
298                 verbose "no more kernels, set the kernel to none"
299                 setup-$boot-conf $verbose --bootdir "$bootdir" \
300                                      default --force "<none>"
301         else
302                 verbose "new default kernel is \"$newest_kernel\""
303                 setup-$boot-conf $verbose --bootdir "$bootdir" \
304                                      default "$newest_kernel"
305         fi
306
307         if [ "$?" -ne 0 ]; then
308                 fatal "cannot set default kernel, \"setup-$boot-conf\" failed"
309         fi
310 }
311
312 #
313 # -----------------------------------------------------------------------------
314 # The "default" subcommand
315 # -----------------------------------------------------------------------------
316 #
317
318 show_default_usage()
319 {
320         cat <<-EOF
321 Usage: $PROG default [options] <kernel>
322
323 Set the default boot kernel to <kernel>. If <kernel> is omited, print the
324 current default boot kernel name.
325
326 Options:
327   -f, --force  do not fail if <kernel> doesn't exist
328   -h, --help   show this text and exit
329 EOF
330 }
331
332 show_default_usage_fail()
333 {
334         IFS= printf "%s\n\n" "$PROG: error: $*" >&2
335         show_default_usage >&2
336         exit 1
337 }
338
339 default_subcommand()
340 {
341         local tmp
342         tmp=`getopt -n $PROG -o f,h --long force,help -- "$@"` ||
343                 show_default_usage_fail "cannot parse command-line options"
344         eval set -- "$tmp"
345
346         local force=
347         while true; do
348                 case "$1" in
349                 -f|--force)
350                         force="-f"
351                         ;;
352                 -h|--help)
353                         show_default_usage
354                         exit 0
355                         ;;
356                 --) shift; break
357                         ;;
358                 *) show_default_usage_fail "unrecognized option \"$1\""
359                         ;;
360                 esac
361                 shift
362         done
363
364         if [ "$#" -gt 1 ]; then
365                 show_default_usage_fail "too many arguments: \"$1\""
366         fi
367
368         prepare
369
370         local kernel="${1:-}"
371         local kernel_path="$bootdir/$kernel"
372
373         if [ -n "$kernel" ] && ! [ -f "$kernel_path" ] && [ -z "$force" ]; then
374                 fatal "cannot find kernel \"$kernel_path\"" \
375                       "(use -f to ignore this error)"
376         fi
377
378         setup-$boot-conf $verbose --bootdir "$bootdir" default $force "$kernel"
379 }
380
381 #
382 # -----------------------------------------------------------------------------
383 #
384
385 show_usage()
386 {
387         cat <<-EOF
388 Usage: $PROG [options] <subcommand> [options] <arguments>
389
390 This program adds or removes a kernel to/from the bootloader configuration
391 file(s). This is a Tizen specific program and it currently supports only 2
392 bootloader types - extlinux and gummiboot. Each new kernel gets 2 boot menu
393 entries - the default and verbose, and both of these are removed by the
394 "remove" subcommand.
395
396 The supported subcommands are:
397    add     - add bootloader entries for a kernel
398    remove  - remove bootloader entries for a kernel
399    default - get or set the default boot kernel
400
401 Run "$PROG <subcommand>" to see subcommand-specific help.
402
403 Options:
404   --version      show the program version and exit
405   -v, --verbose  be verbose
406   -h, --help     show this text and exit
407 EOF
408 }
409
410 show_usage_fail()
411 {
412         IFS= printf "%s\n\n" "$PROG: error: $*" >&2
413         show_usage >&2
414         exit 1
415 }
416
417 verbose=
418 while [ -n "${1:-""}" ] && [ -z "${1##-*}" ]; do
419         case "$1" in
420         --version)
421                 printf "%s\n" "$VER"
422                 exit 0
423                 ;;
424         -v|--verbose)
425                 verbose="-v"
426                 ;;
427         -h|--help)
428                 show_usage
429                 exit 0
430                 ;;
431         --) shift; break
432                 ;;
433         *) show_usage_fail "unrecognized option \"$1\""
434                 ;;
435         esac
436         shift
437 done
438
439 if [ "$#" -eq 0 ]; then
440         show_usage
441         exit 0
442 fi
443
444 # Parse subcommands
445
446 subcommand="$1"; shift
447
448 case "$subcommand" in
449 add)
450         add_subcommand "$@"
451         break
452         ;;
453 remove)
454         remove_subcommand "$@"
455         break
456         ;;
457 default)
458         default_subcommand "$@"
459         break
460         ;;
461 *) show_usage_fail "unrecognized subcommand \"$subcommand\""
462         ;;
463 esac