fd8ce4757e35e45ee4f8c3a1cd30f8a519aba491
[platform/adaptation/setup-scripts.git] / setup-ivi-boot
1 #!/bin/sh -euf
2
3 # Copyright 2013 Intel Corporation
4 # Author: Artem Bityutskiy
5 # License: GPLv2
6
7 PROG="setup-ivi-boot"
8 VER="1.0"
9
10 srcdir="$(readlink -ev -- ${0%/*})"
11 PATH="/usr/share/setup-ivi:$srcdir:$PATH"
12
13 . setup-ivi-sh-functions
14 . installerfw-sh-functions
15
16 # This is a small trick which I use to make sure my scripts are portable -
17 # check if 'dash' is present, and if yes - use it.
18 if can_switch_to_dash; then
19         exec dash -euf "$srcdir/$PROG" "$@"
20         exit $?
21 fi
22
23 install_gummiboot()
24 {
25         verbose "installing gummiboot to $bootdir"
26
27         local installdir="$bootdir/EFI/boot"
28         local gummiboot_path="$(installerfw_mnt_prefix "/usr/lib/gummiboot")"
29
30         # Make sure gummiboot is installed in the system
31         if ! ([ -f $gummiboot_path/gummibootia32.efi ] || \
32               [ -f $gummiboot_path/gummibootx64.efi ]); then
33                 fatal "\"$gummiboot_path/gummiboot*.efi\" files not found!"
34         fi
35
36         # Install gummiboot
37         mkdir -p $verbose "$installdir" >&2
38         [ -f "$gummiboot_path/gummibootia32.efi" ] && \
39                 cp $verbose "$gummiboot_path/gummibootia32.efi" \
40                             "$installdir/bootia32.efi" >&2
41         [ -f "$gummiboot_path/gummibootx64.efi" ] && \
42                 cp $verbose "$gummiboot_path/gummibootx64.efi" \
43                             "$installdir/bootx64.efi"
44
45         verbose "installed gummiboot to $bootdir"
46 }
47
48 install_extlinux()
49 {
50         verbose "installing extlinux to $bootdir"
51
52         local installdir="$bootdir/extlinux"
53         local extlinux="extlinux"
54
55         # Check if extlinux is available
56         if ! command -v "extlinux" >/dev/null 2>&1; then
57                 extlinux="$(installerfw_mnt_prefix "/sbin/extlinux")"
58                 [ -f "$extlinux" ] ||
59                         fatal "cannot find \"$extlinux\""
60         fi
61
62         # Get device node name for the boot partition
63         local boot_devnode
64         installerfw_get_part_info "/boot" "DEVNODE_NOW" "boot_devnode"
65         [ -n "$boot_devnode" ] || \
66                 fatal "cannot find device node of the boot disk, probably" \
67                       "INSTALLERFW_PARTx_DEVNODE_NOW environment" \
68                       "variable is not defined"
69
70         # Install extlinux
71         mkdir -p $verbose "$installdir" >&2
72         "$extlinux" --device "$boot_devnode" -i "$installdir" || \
73                 fatal "cannot install extlinux to \"$installdir\"" \
74                       "(requires extlinux version 5 or greater)"
75
76
77         # Get device node name for the boot disk
78         local mbr_devnode
79         installerfw_get_part_info "/boot" "DISK_DEVNODE_NOW" "mbr_devnode"
80         [ -n "$mbr_devnode" ] || \
81                 fatal "cannot find device node of the boot disk, probably" \
82                       "INSTALLERFW_PARTx_DISK_DEVNODE_NOW environment" \
83                       "variable is not defined"
84
85         # Install the MBR part of extlinux
86         local mbr_bin="$(installerfw_mnt_prefix "/usr/share/syslinux/gptmbr.bin")"
87         dd if="$mbr_bin" of="$mbr_devnode" count=1 || \
88                 fatal "cannot install MBR: dd if=$mbr_bin of=$mbr_devnode"
89
90         verbose "installed extlinux to $bootdir"
91 }
92
93 show_usage()
94 {
95         cat <<-EOF
96 Usage: $PROG [options]
97
98 Install the EFI bootloader (gummiboot) and create the initial configuration
99 for all the currently installed kernels. This program depends on various
100 "installer framework" variables.
101
102 Options:
103   -v, --verbose  be verbose
104   --version      show the program version and exit
105   -h, --help     show this text and exit
106 EOF
107 }
108
109 show_usage_fail()
110 {
111         IFS= printf "%s\n\n" "$PROG: error: $*" >&2
112         show_usage >&2
113         exit 1
114 }
115
116 # Parse the options
117 tmp=`getopt -n $PROG -o v,h --long verbose,version,help -- "$@"` ||
118         show_usage_fail "cannot parse command-line options"
119 eval set -- "$tmp"
120
121 verbose=
122 while true; do
123         case "$1" in
124         -v|--verbose)
125                 verbose="-v"
126                 ;;
127         --version)
128                 printf "%s\n" "$VER"
129                 exit 0
130                 ;;
131         -h|--help)
132                 show_usage
133                 exit 0
134                 ;;
135         --) shift; break
136                 ;;
137         *) show_usage_fail "unrecognized option \"$1\""
138                 ;;
139         esac
140         shift
141 done
142
143 bootdir="$(installerfw_mnt_prefix "/boot")"
144
145 # Save the installer framework variables
146 installerfw_file="$(installerfw_get_env_file_name)"
147 if [ -f "$installerfw_file" ]; then
148         installerfw_restore_env
149 else
150         installerfw_save_env
151 fi
152
153 # Get OS name
154 get_os_name "os_name"
155
156 if installerfw_is_efi_boot_system; then
157         install_gummiboot
158 else
159         install_extlinux
160 fi
161
162 # Create bootloader entries for each kernel
163 kernels="$(ls -1 "$bootdir" | LC_ALL=C grep "^vmlinuz-" | sort -r)"
164
165 [ -n "$kernels" ] || \
166         fatal "no kernels (vmlinuz-*) found in \"$bootdir\""
167
168 printf "%s\n" "$kernels" | while IFS= read -r kernel; do
169         setup-ivi-bootloader-conf $verbose add --force "$kernel"
170 done
171
172 # Set the default kernel to the kernel with highest version
173 newest_kernel="$(get_newest_kernel "$bootdir")"
174 setup-ivi-bootloader-conf $verbose default "$newest_kernel"