Dash:
[t2.git] / package / boot / grub2 / stone_mod_grub2.sh
blobb73825455de0bf2bb2493f00fa39f82c97f1734e
1 # --- T2-COPYRIGHT-NOTE-BEGIN ---
2 # T2 SDE: package/*/grub2/stone_mod_grub2.sh
3 # Copyright (C) 2004 - 2021 The T2 SDE Project
4 # Copyright (C) 1998 - 2003 ROCK Linux Project
5 #
6 # This Copyright note is generated by scripts/Create-CopyPatch,
7 # more information can be found in the files COPYING and README.
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License version 2.
11 # --- T2-COPYRIGHT-NOTE-END ---
13 # [MAIN] 70 grub2 GRUB2 Boot Loader Setup
14 # [SETUP] 90 grub2
16 # TODO:
17 # void efibootmgr duplicates :-/
18 # auto-mount efivars?
19 # impl. & test direct sparc, direct i386-pc-mbr, mips-arc, ...
20 # unify non-crypt, and direct non-EFI BIOS install
22 arch=$(uname -m)
23 arch=${arch/i?86/i386}
24 arch=${arch/aarch/arm}
26 create_kernel_list() {
27 first=1
28 for x in `(cd /boot/; ls vmlinux-* ) | sort -r`; do
29 ver=${x/vmlinux-/}
30 [[ $arch = *86* ]] && x=${x/vmlinux/vmlinuz}
31 [[ $arch = *arm* ]] && x=${x/vmlinux/Image}.gz
32 if [ $first = 1 ]; then
33 label=linux; first=0
34 else
35 label=linux-$ver
38 cat << EOT
40 menuentry "T2/Linux $label" {
41 linux $bootpath/$x root=$rootdev ro
42 initrd $bootpath/initrd-${ver}
44 EOT
45 done
49 create_boot_menu() {
50 # determine /boot path, relative to the boot device
51 # (non local as used by create_kernel_list() ...)
53 [ "$rootdev" = "$bootdev" ] && bootpath="/boot" || bootpath=""
55 mkdir -p /boot/grub/
56 cat << EOT > /boot/grub/grub.cfg
57 set timeout=30
58 set default=0
59 set fallback=1
61 if [ "\$grub_platform" = "efi" ]; then
62 set debug=video
63 insmod efi_gop
64 insmod efi_uga
65 insmod font
66 if loadfont \${prefix}/unicode.pf2; then
67 insmod gfxterm
68 set gfxmode=auto
69 set gfxpayload=keep
70 terminal_output gfxterm
74 EOT
75 if [ -z "$cryptdev" ]; then
76 cat << EOT >> /boot/grub/grub.cfg
77 set uuid=$grubdev
78 search --set=root --no-floppy --fs-uuid \$uuid
80 EOT
81 else
82 cat << EOT >> /boot/grub/grub.cfg
83 set root=$cryptdev
85 EOT
88 create_kernel_list >> /boot/grub/grub.cfg
90 gui_message "This is the new /boot/grub/grub.cfg file:
92 $( cat /boot/grub/grub.cfg )"
93 unset bootpath
96 grubmods="normal boot configfile linux part_msdos part_gpt \
97 fat ext2 iso9660 reiserfs xfs jfs lvm \
98 crypto cryptodisk luks all_video sleep reboot \
99 search search_fs_file search_label search_fs_uuid"
101 case "$arch" in
102 ppc*) grubmods="$grubmods part_apple hfs hfsplus suspend" ;;
103 sparc*) grubmods="$grubmods part_sun" ;;
104 x86*) grubmods="$grubmods ntfs ntfscomp" ;;
105 esac
107 grub_inst() {
108 if [[ $arch != ppc* ]]; then
109 if [ -z "$cryptdev" -a ! -d /sys/firmware/efi ]; then
110 if [[ "$arch" != sparc* ]]; then
111 grub2-install $instdev
112 else
113 grub2-install --skip-fs-probe --force $instdev
115 else
116 for efi in /boot/efi*; do
117 mkdir -p $efi/EFI/boot
119 if [ -z "$cryptdev" ]; then
120 cat << EOT > $efi/EFI/boot/grub.cfg
121 set uuid=$grubdev
122 search --set=root --no-floppy --fs-uuid \$uuid
123 configfile (\$root)/boot/grub/grub.cfg
125 else
126 cat << EOT > $efi/EFI/boot/grub.cfg
127 set uuid=$grubdev
128 cryptomount -u \$uuid
129 configfile (crypto0)/boot/grub/grub.cfg
132 # TODO: case
133 local exe=BOOTX64.EFI
134 [ $arch = i386 ] && exe=${exe/X64/IA32}
135 [ $arch = arm64 ] && exe=${exe/X/AA}
136 [[ $arch = riscv* ]] && exe={exe/X64/${arch^^}}
138 grub-mkimage -O $arch-efi -o $efi/EFI/boot/$exe \
139 -p /efi/boot -d /usr/lib*/grub/$arch-efi/ \
140 $grubmods
141 done
142 efibootmgr -c -L "T2 Linux" -l "\\EFI\\boot\\$exe"
144 else
145 # Apple PowerPC - install into FW read-able HFS partition
146 local bootstrap=$instdev$(disktype $instdev | grep Apple_Bootstrap -B 1 |
147 sed -n 's/Partition \(.*\):.*/\1/p')
148 if [ "$bootstrap" = "$instdev" ]; then
149 echo "No HFS bootstrap partition found!"
150 return
153 umount /mnt 2>/dev/null
154 hformat -l bootstrap $bootstrap
155 if ! mount $bootstrap /mnt; then
156 echo "Error mounting HFS bootstrap partition"
157 else
158 mkdir -p /mnt/boot/grub
159 if [ -z "$cryptdev" ]; then
160 cat << EOT > /mnt/boot/grub/grub.cfg
161 set uuid=$grubdev
162 search --set=root --no-floppy --fs-uuid \$uuid
163 configfile (\$root)/boot/grub/grub.cfg
165 else
166 cat << EOT > /mnt/boot/grub/grub.cfg
167 set uuid=$grubdev
168 cryptomount -u \$uuid
169 configfile (crypto0)/boot/grub/grub.cfg
172 grub-mkimage -O powerpc-ieee1275 -p /boot/grub \
173 -o /mnt/boot/grub.elf -d /usr/lib*/grub/powerpc-ieee1275 \
174 $grubmods
176 cat > /mnt/ofboot.b <<-EOT
177 <CHRP-BOOT>
178 <COMPATIBLE>
179 MacRISC MacRISC3 MacRISC4
180 </COMPATIBLE>
181 <DESCRIPTION>
182 T2 SDE
183 </DESCRIPTION>
184 <BOOT-SCRIPT>
185 " screen" output
186 load-base release-load-area
187 boot hd:2,\\boot\\grub.elf
188 </BOOT-SCRIPT>
189 <OS-BADGE-ICONS>
190 1010
191 000000000000F8FEACF6000000000000
192 0000000000F5FFFFFEFEF50000000000
193 00000000002BFAFEFAFCF70000000000
194 0000000000F65D5857812B0000000000
195 0000000000F5350B2F88560000000000
196 0000000000F6335708F8FE0000000000
197 00000000005600F600F5FD8100000000
198 00000000F9F8000000F5FAFFF8000000
199 000000008100F5F50000F6FEFE000000
200 000000F8F700F500F50000FCFFF70000
201 00000088F70000F50000F5FCFF2B0000
202 0000002F582A00F5000008ADE02C0000
203 00090B0A35A62B0000002D3B350A0000
204 000A0A0B0B3BF60000505E0B0A0B0A00
205 002E350B0B2F87FAFCF45F0B2E090000
206 00000007335FF82BF72B575907000000
207 000000000000ACFFFF81000000000000
208 000000000081FFFFFFFF810000000000
209 0000000000FBFFFFFFFFAC0000000000
210 000000000081DFDFDFFFFB0000000000
211 000000000081DD5F83FFFD0000000000
212 000000000081DDDF5EACFF0000000000
213 0000000000FDF981F981FFFF00000000
214 00000000FFACF9F9F981FFFFAC000000
215 00000000FFF98181F9F981FFFF000000
216 000000ACACF981F981F9F9FFFFAC0000
217 000000FFACF9F981F9F981FFFFFB0000
218 00000083DFFBF981F9F95EFFFFFC0000
219 005F5F5FDDFFFBF9F9F983DDDD5F0000
220 005F5F5F5FDD81F9F9E7DF5F5F5F5F00
221 0083DD5F5F83FFFFFFFFDF5F835F0000
222 000000FBDDDFACFBACFBDFDFFB000000
223 000000000000FFFFFFFF000000000000
224 0000000000FFFFFFFFFFFF0000000000
225 0000000000FFFFFFFFFFFF0000000000
226 0000000000FFFFFFFFFFFF0000000000
227 0000000000FFFFFFFFFFFF0000000000
228 0000000000FFFFFFFFFFFF0000000000
229 0000000000FFFFFFFFFFFFFF00000000
230 00000000FFFFFFFFFFFFFFFFFF000000
231 00000000FFFFFFFFFFFFFFFFFF000000
232 000000FFFFFFFFFFFFFFFFFFFFFF0000
233 000000FFFFFFFFFFFFFFFFFFFFFF0000
234 000000FFFFFFFFFFFFFFFFFFFFFF0000
235 00FFFFFFFFFFFFFFFFFFFFFFFFFF0000
236 00FFFFFFFFFFFFFFFFFFFFFFFFFFFF00
237 00FFFFFFFFFFFFFFFFFFFFFFFFFF0000
238 000000FFFFFFFFFFFFFFFFFFFF000000
239 </OS-BADGE-ICONS>
240 </CHRP-BOOT>
243 umount /mnt
244 hmount $bootstrap
245 hattrib -b bootstrap:
246 hattrib -c UNIX -t tbxi bootstrap:ofboot.b
247 humount
253 grub_install() {
254 gui_cmd 'Installing GRUB2' "grub_inst"
257 get_dm_dev() {
258 local dev="$1"
259 local devnode=$(stat -c "%t:%T" $dev)
260 for d in /dev/dm-*; do
261 [ "$(stat -c "%t:%T" "$d")" = "$devnode" ] && echo $d && return
262 done
265 get_dm_type() {
266 local dev="$1"
267 dev="${dev##*/}"
268 [ -e /sys/block/$dev/dm/uuid ] && cat /sys/block/$dev/dm/uuid
271 get_uuid() {
272 local dev="$1"
274 # look up uuid
275 for _dev in /dev/disk/by-uuid/*; do
276 local d=$(readlink $_dev)
277 d="/dev/${d##*/}"
278 if [ "$d" = $dev ]; then
279 echo $_dev
280 return
282 done
285 get_realdev() {
286 local dev="$1"
287 dev=$(readlink $dev)
288 [ "$dev" ] && echo /dev/${dev##*/} || echo $1
291 main() {
292 rootdev="`grep ' / ' /proc/mounts | tail -n 1 | sed 's, .*,,'`"
293 bootdev="`grep ' /boot ' /proc/mounts | tail -n 1 | sed 's, .*,,'`"
295 # if device-mapper, get backing device
296 if [[ "$rootdev" = *mapper* ]]; then
297 rootdev2=$(get_dm_dev $rootdev)
298 # encrypted?
299 if [[ "$(get_dm_type $rootdev2)" = CRYPT* ]]; then
300 rootdev=$rootdev2
301 realroot=$(cd /sys/block/${rootdev##*/}/slaves/; ls -d [a-z]*)
302 if [ "$realroot" ]; then
303 rootdev="/dev/$realroot"
304 cryptdev="(crypto0)"
306 else
307 cryptdev="lvm/${rootdev#/dev/mapper/}"
311 # get uuid
312 uuid=$(get_uuid $rootdev)
313 [ "$uuid" ] && rootdev=$uuid
314 if [ "$bootdev" ]; then
315 uuid=$(get_uuid $bootdev)
316 [ "$uuid" ] && bootdev=$uuid
319 [ "$bootdev" ] || bootdev="$rootdev"
320 instdev=$(get_realdev $bootdev); instdev="${instdev%%[0-9*]}"
321 [ "$grubdev" ] || grubdev="${bootdev##*/}"
323 if [ ! -f /boot/grub/grub.cfg ]; then
324 if gui_yesno "GRUB2 does not appear to be configured.
325 Automatically install GRUB2 now?"; then
326 create_boot_menu
327 if ! grub_install; then
328 gui_message "There was an error while installing GRUB2."
333 while
335 gui_menu grub 'GRUB2 Boot Loader Setup' \
336 "Root device ... $rootdev" "" \
337 "Boot device ... $bootdev" "" \
338 "Crypt device .. $cryptdev" "" \
339 "Grub device ... $grubdev" "" \
340 "Inst device ... $instdev" "" \
341 '' '' \
342 '(Re-)Create boot menu with installed kernels' 'create_boot_menu' \
343 "(Re-)Install GRUB2 in boot record ($instdev)" 'grub_install' \
344 '' '' \
345 "Edit /boot/grub/grub.cfg (Boot Menu)" \
346 "gui_edit 'GRUB2 Boot Menu' /boot/grub/grub.cfg"
347 do : ; done