From 2b7a152fd09cf684597da4ae0c6193a1adb4c5b0 Mon Sep 17 00:00:00 2001 From: Martin Whitaker Date: Mon, 8 Aug 2022 19:11:51 +0100 Subject: bootloader: rework reading/writing of main rEFInd config file. At the moment we only use and modify one setting, but the next commit will change that. --- perl-install/bootloader.pm | 69 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 54 insertions(+), 15 deletions(-) diff --git a/perl-install/bootloader.pm b/perl-install/bootloader.pm index 4a0e1a4bc..e43b51215 100644 --- a/perl-install/bootloader.pm +++ b/perl-install/bootloader.pm @@ -705,17 +705,30 @@ sub read_refind() { } } $bootloader{method} = 'refind'; - #- Try both possible locations for the main config file. The default is to use NVRAM if the - #- config file isn't found. - $bootloader{use_nvram} = get_refind_use_nvram("$::prefix/boot/EFI/EFI/refind/refind.conf") - && get_refind_use_nvram("$::prefix/boot/EFI/EFI/BOOT/refind.conf"); + read_refind_config(\%bootloader); \%bootloader; } -sub get_refind_use_nvram { - my ($path) = @_; - #- The default is to use NVRAM, so test if it is explicitly disabled. - -r $path && (grep { /^\s*use_nvram\s+(false|off|0)/ } cat_utf8($path)) ? 0 : 1; +sub read_refind_config { + my ($bootloader) = @_; + + #- These are the rEFInd default values. + $bootloader->{use_nvram} = 1; + + if (-r "$::prefix/boot/EFI/EFI/refind/refind.conf") { + read_refind_conf($bootloader, "$::prefix/boot/EFI/EFI/refind/refind.conf"); + } elsif (-r "$::prefix/boot/EFI/EFI/BOOT/refind.conf") { + read_refind_conf($bootloader, "$::prefix/boot/EFI/EFI/BOOT/refind.conf"); + } +} + +sub read_refind_conf { + my ($bootloader, $config_file) = @_; + foreach (cat_utf8($config_file)) { + if ($_ =~ /^\s*use_nvram\s+(false|off|0)/) { + $bootloader->{use_nvram} = 0; + } + } } # FIXME: actually read back previous conf @@ -2498,17 +2511,43 @@ sub install_refind { run_program::rooted($::prefix, '/sbin/refind-install', '2>', \$error, @options) or die "refind-install failed: $error"; } + #- Try both possible locations for the main config file. - set_refind_use_nvram($bootloader->{use_nvram}, "$::prefix/boot/EFI/EFI/refind/refind.conf"); - set_refind_use_nvram($bootloader->{use_nvram}, "$::prefix/boot/EFI/EFI/BOOT/refind.conf"); + modify_refind_config($bootloader, "$::prefix/boot/EFI/EFI/refind"); + modify_refind_config($bootloader, "$::prefix/boot/EFI/EFI/BOOT"); + write_refind($bootloader, $all_hds); } -sub set_refind_use_nvram { - my ($use_nvram, $path) = @_; - return if ! -w $path; - my $setting = $use_nvram ? 'true' : 'false'; - substInFile { s/^#?\s*use_nvram\s+.*/use_nvram $setting/ } $path; +sub modify_refind_config { + my ($bootloader, $esp_dir) = @_; + + my $config_file = "$esp_dir/refind.conf"; + return if ! -f $config_file || ! -w $config_file; + + my $use_nvram = $bootloader->{use_nvram} ? 'true' : 'false'; + + my @config; + + %done; + foreach (cat_utf8($config_file)) { + if ($_ =~ /^#?use_nvram\s/) { + if (! $done{use_nvram}) { + push @config, "use_nvram $use_nvram\n" ; + $done{use_nvram} = 1; + } + } else { + push @config, $_; + } + } + + if (@config) { + log::l("writing rEFInd config to $config_file"); + renamef($config_file, $config_file . '.old'); + output_with_perm($config_file, 0600, @config); + } else { + log::l("config has no entries - rEFInd config file not written"); + } } sub when_config_changed_refind { -- cgit v1.2.1