Commit Graph

1401 Commits

Author SHA1 Message Date
Daan De Meyer
929f41c652 bootctl: update/list/remove all instances of systemd-boot in /EFI/BOOT
systemd-boot might be installed in /EFI/BOOT under more names than
just /EFI/BOOT/BOOTX64.efi. The prime example is shim which loads
its second stage binary from /EFI/BOOT/grubx64.efi. To accomodate
use cases where systemd-boot is installed as /EFI/BOOT/grubx64.efi,
let's always check the entire /EFI/BOOT directory for binaries that
identify as systemd-boot and list/update/remove those as well.

Let's keep this somewhat generic though and not install ourselves as
grubx64.efi since that would mean having to check for shim which is
a can of worms we probably don't want to open.
2023-12-20 13:56:02 +01:00
Lennart Poettering
ec43827bf7 stub: get_extra_dir() can return NULL
If we have a device path that is not the usual file system directory
get_extra_dir() will return NULL. Handle that gracefully.

Fixes: #30228
2023-11-28 23:53:30 +00:00
Lennart Poettering
b0f666f817 boot: suffix SecureBoot message with newline
Output is otherwise so weird, since this is the last log line seen for a
while typically, and if it doesn#t put the cursor back in the first
column it looks like something is incomplete and hanging. Hence do what
we always do: finish log messages with a newline.
2023-11-28 15:40:38 +00:00
Yu Watanabe
5acea42501 fuzz: always call fuzz_setup_logging() 2023-11-18 03:04:27 +09:00
Lennart Poettering
4bec240751 boot: measure config first, only then parse
Fixes: #30026
2023-11-15 13:36:46 +01:00
Clayton Craft
0f781d2aa5 boot: load device tree even if no original config exists
Firmware may not have loaded a devicetree, for example if the device
shipped with windows and exclusively supports ACPI.
We should always load the specified devicetree regardless of firmware
state to enable booting on platforms where Linux only supports DT.

Note: in _cleanup, the orig. config is NULL in this case, and passing
NULL to InstallConfigurationTable is permitted by the EFI spec.
See: https://uefi.org/specs/UEFI/2.10/07_Services_Boot_Services.html

Fixes #24059

Co-authored-by: Daniel Thompson <daniel.thompson@linaro.org>
2023-11-10 22:04:50 +00:00
Antonio Alvarez Feijoo
52c7727f54 bootctl: fix code comment 2023-11-09 12:14:01 +00:00
Antonio Alvarez Feijoo
4697ee9f43 bootctl: fix -q option
Follow up to 14e6e444dd
2023-11-06 16:36:38 +00:00
Raito Bezarius
b9a0a13f7a secure-boot: print just before cold-resetting to help diagnose hangs
When testing the secureboot enroll feature, it can be hard to distinguish without
using the QMP API of QEMU whether we are in a hang situation of the UEFI firmware.

Making it clear that we reached the `ResetSystem` can be helpful towards that need.
2023-10-30 13:20:48 +00:00
Daan De Meyer
c173be0d4c Merge pull request #29708 from DaanDeMeyer/bootctl-always
Always build bootctl
2023-10-25 22:59:59 +02:00
Daan De Meyer
7e7ea1b1c8 meson: Always build systemd-measure
Same idea as with bootctl, we might be doing image builds from a
system that doesn't boot with UEFI but we still might want to measure
stuff for the image we're building so let's not gate this behind
ENABLE_BOOTLOADER.
2023-10-25 16:49:30 +02:00
Daan De Meyer
dedb925eaf meson: Always build bootctl
bootctl is rather useful to have, even if on a system without UEFI,
as it has a number of verbs that are unrelated to UEFI (e.g kernel-identify),
and more importantly, it supports --root to operate on directory trees
(which could be intended to be deployed on UEFI) so let's make sure we
always build it.
2023-10-25 16:49:24 +02:00
Luca Boccassi
e5cd8bc060 Merge pull request #29601 from yuwata/mmap-check-overflow
mmap: check offset and size more carefully
2023-10-24 12:20:04 +01:00
Valentin David
7a876307bb stub: Ignore the boot counter when looking for .extra.d directory
If `foo+3-0.efi` is booted when there are some files in `foo.efi.extra.d`,
those files are ignored. But after the boot is blessed and the system rebooted,
those file are taken into account, and the boot is different from first
boot. This behavior is a bit puzzling.

Instead we now ignore the counter and always look for the extra files in
`foo.efi.extra.d` and always boot the same way.
2023-10-20 16:18:09 +01:00
Lennart Poettering
83ee462c72 tpm2: move event tag sd-boot/sd-stub to make measurements with into src/fundamental/
Ultimately we want to be able to recognize these in userspace, hence
make them available in both UEFI mode and userspace.

While we are at it, let's rename the fields a bit, reflecting more what
they measure, not what the metadata is that we store about them.
2023-10-20 15:58:45 +01:00
Luca Boccassi
41204a6d74 Merge pull request #29617 from keszybz/efi-no-xmalloc0
efi: drop duplicate initialization to 0
2023-10-19 14:23:54 +01:00
Zbigniew Jędrzejewski-Szmek
6e98ddf825 efi: drop unused xmalloc0()
Keeping this separate to make it easy to revert if it's ever needed again.
2023-10-19 12:24:53 +02:00
Zbigniew Jędrzejewski-Szmek
586f19976a efi: do not memzero fields before initializing them
In all three cases we immediately overwrite the whole field anyway,
so the call to memzero is not needed.
2023-10-19 12:24:53 +02:00
Yu Watanabe
86cbbc6d05 tree-wide: check if return value of lseek() and friends is negative
We usually check return value of syscalls or glibc functions by it is
negative or not, something like that `if (stat(path, &st) < 0)`.
Let's also use the same style for lseek() and friends even the type of
their return value is off_t.

Note, fseeko() returns int, instead of off_t.
2023-10-19 18:31:44 +09:00
Yu Watanabe
87a768b801 tree-wide: fix typo 2023-10-18 12:40:25 +09:00
Emil Velikov
6efdd7fec5 sd-boot: add way to disable the 100ms delay when timeout=0
Currently we have a 100ms delay which allows for people to enter/show
the boot menu even when timeout is set to zero.

In a handful of cases, that may not be needed - both in terms of access
policy, as well as latency.

For example: the option to provide the boot menu may be hidden behind an
"expert only" UX in the OS, to avoid end users from accidentally
entering it.

In addition, the current 100ms input polling may cause unexpected
additional delays in the boot. Some example numbers from my SteamDeck:

 - boot counting/rename/flush doubles 300us -> 600us
 - seed/hash setup doubles 900us -> 1800us
 - kernel/image load gets ~40% slower 107ms -> 167ms

It's not entirely clear why the UEFI calls gets slower, nevertheless the
information in itself proves useful.

This commit introduces a new option "menu-disabled", which omits the
100ms delay. The option is documented throughout the manual pages as
well as the Boot Loader Specification.

v2:
 - use STR_IN_SET

v3:
 - drop erroneous whitespace

v4:
 - add a new LoaderFeature bit,
 - don't change ABI keep TIMEOUT_* tokens the same
 - move new token in the 64bit range, update API and storage for it
 - change inc/dec behaviour to TIMEOUT_MIN : TIMEOUT_MENU_FORCE
 - user cannot opt-in from sd-boot itself, add assert_not_reached()

v5:
 - s/Menu disablement control/Menu can be disabled/
 - rewrap comments to 109
 - use SYNTHETIC_ERRNO(EOPNOTSUPP)

Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
2023-10-17 15:09:53 +01:00
Emil Velikov
5b45fad4fc bootctl, sd-boot: cross document the menu-hidden/force ABI
Document the current state and how we got here.

v2:
 - rewrap command to 109 columns

Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
2023-10-17 15:01:27 +01:00
Lukas
c7fda70716 stub: NULL checks for DeviceHandle and FilePath
UKIs may be loaded in a way, that there can not be a device handle to
the filesystem, that contains the image, for example when using a
bootloader to load the image from a partition with a file system that is
not supported by the firmware.

With the current systemd stub, this causes a failed assertion, because
stub gets passed a NULL DeviceHandle and FilePath. Inserting two
explicit checks enables proper boot even in this case.

Fixes: #29331
2023-10-16 10:44:03 +02:00
Jan Janssen
e9e40862de fuzz: Add fuzzer for efi conf/osrel parsing 2023-10-15 17:41:25 +02:00
Jan Janssen
5ce851ab99 boot: Fix OOB reads in conf/osrel parsing 2023-10-15 17:41:25 +02:00
Jan Janssen
f17670a0c3 boot: Move line_get_key_value to efi-string
No functional changes.
2023-10-15 17:41:25 +02:00
Jan Janssen
539d000740 boot: Move parse_boolean to efi-string
No functional changes.
2023-10-15 17:41:25 +02:00
Jan Janssen
ec9d3fc51c test: Remove unnecessary test prefix 2023-10-15 12:10:37 +01:00
Luca Boccassi
5986e3f4db Merge pull request #29502 from keszybz/sd-boot-config-tweaks
Tweaks to sd-boot UX
2023-10-12 23:08:56 +01:00
Zbigniew Jędrzejewski-Szmek
0502bd7e5e efi/boot: rename ConfigEntry to BootEntry
Some of the entries are really configured, but we also have a bunch
of automatic entries. Calling them "config entries" is misleading, let's
use the more natural "boot entry".

While at it, rename:
  config_load_entries() → config_load_type1_entries()
  config_entry_add_unified() → config_load_type2_entries()
  config_title_generate() → generate_boot_entry_titles()
  config_entry_add_<type>() → config_add_entry_<type>()

No functional change.
2023-10-12 12:13:56 +02:00
Zbigniew Jędrzejewski-Szmek
6438161187 efi/boot: adjust grammar and punctuation in comments 2023-10-12 12:13:56 +02:00
Zbigniew Jędrzejewski-Szmek
a7af63f130 efi/boot: use "else if" instead of explicit "continue"
No functional change.
2023-10-12 12:13:56 +02:00
Zbigniew Jędrzejewski-Szmek
1cd26e5e56 efi/boot: make timeout changes relative to current value
When the user pressed + or -, we would set the efivar override, starting
from the default of 0. Instead, set an override that starts at the current
value. This means that when user has e.g. a configured override of 5 s, and
they press +, they get an override of 6 s. I think this is leads to a much
smoother experience for a user, who does not necessarilly need to know that
we have three levels of overrides, they just want to easily configure the
timeout with keys. If they press +, the timeout should increase, and not
jump to some low value.

Also, once an override has been set via the boot menu, i.e. the efivar is set,
do not allow unsetting the efivar from the boot menu. This way we also avoid
an unexpected "jump" to whatever the other sources of configuration specify.
The user can configure any value with the keys that they want, so we don't
need to allow unsetting.
2023-10-12 12:13:56 +02:00
Zbigniew Jędrzejewski-Szmek
1f6dbf7021 efi/boot: split out helper to reduce duplicate formatting code
No functional change.
2023-10-12 12:13:56 +02:00
Zbigniew Jędrzejewski-Szmek
56747a1f13 sd-boot: when rebooting or powering off, save config state
The menu_run() function allows the user to set/unset default entry, or to
increase/decrease menu timeout. After a keypress, status like
  "Menu timeout set to 5 s"
is printed, but there actually isn't any immediate effect. The value is only
written right right before booting a menu entry to avoid unnecessary wear&tear
on the nvram storage. This delayed write is supposed to be invisible to the
user.

Nevertheless, operations like reboot into firmware, reboot, or shutdown were
done immediately. We need to exit the loop first, save the state, and only do
the op afterwards.

Fixup for f6531b11d2 and
e6cab77eca.
Also reverts 498d0cc426.
2023-10-12 12:13:56 +02:00
Zbigniew Jędrzejewski-Szmek
c650bbb8a3 efi/boot: use DEFINE_TRIVIAL_CLEANUP_FUNC() in one more place
No functional change.
2023-10-12 12:13:56 +02:00
Luca Boccassi
86f99bdbe1 docs: clarify difference between kernel stub and sd-stub in UEFI doc 2023-10-11 10:33:38 +01:00
Luca Boccassi
12de4ed1ca boot: measure loader.conf in PCR5
Results in:

- EventNum: 26
  PCRIndex: 5
  EventType: EV_EVENT_TAG
  DigestCount: 4
  Digests:
  - AlgorithmId: sha1
    Digest: 155fb999ca61ba8c7b1f1d87cee821f772ef084a
  - AlgorithmId: sha256
    Digest: 4c26adf231603613afc00bb3d5cad046aec6a525ca01262417c7085caab452b5
  - AlgorithmId: sha384
    Digest: 3e0758cb6605ac274e55d747bf29ee3474fc4413cd5e7a451d1375219cd7f08a30fc915a8df7131657ca78b82b9ccec8
  - AlgorithmId: sha512
    Digest: e32d905b9092c543802f386db9a397d9b6593bdb8360fb747a6d23e491a09595fec8699184cc790d0873a3d52ed16d045538f0c73ece48278fae0fb6ed9b4ed6
  EventSize: 32
  Event: 2a58bcf5180000006c006f0061006400650072002e0063006f006e0066000000
2023-10-09 22:22:09 +01:00
Luca Boccassi
3e6f010e03 stub: measure all cmdline addons together 2023-10-09 22:22:09 +01:00
Luca Boccassi
68f85761e2 stub: add support for dtb addons
Same as kernel command line addons.
2023-10-09 22:22:09 +01:00
Luca Boccassi
3e5a499009 efi: add xmemdup 2023-10-09 22:22:09 +01:00
Luca Boccassi
d869ec4ab0 efi: add EFI_TCG2_TAGGED_EVENT and helpers 2023-10-09 22:22:09 +01:00
Emil Velikov
cb341090d0 sd-boot: add auto-reboot and auto-poweroff entries
Currently only an auto-reboot-to-firmware entry is available. For other
features - like reboot and power off - one needs to press the uppercase
B and O respectively.

Embedded devices may be missing a full fledged keyboard, so allow for
sd-boot to generate those entries.

v2:
 - add to the config parser/man/bootctl/sd-boot info screen
 - keep them off by default
 - add the (O)ff and re(B)oot help text if boot entries are not shown
 - drop irrelevant get_os_indications_supported() comment
 - s/ShutDown/Shutdown/

v3:
 - cast shutdown_system() reboot_system() to void

v4:
 - shutdown -> poweroff
 - add trailing ",ignoring" in parser message
 - drop explicit default state assignment to "false"

Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
2023-10-06 16:21:23 +01:00
Emil Velikov
28052aa8cd sd-boot: sprinkle some ", ignoring" trailing messages
As mentioned by Lennart:

  ... we typically suffix such messages with ", ignoring", to indicate
  that we don't consider this fatal for anything.

Update config_defaults_load_from_file() to follow that pattern.

Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
2023-10-06 16:21:04 +01:00
Emil Velikov
a9bc49b4a8 sd-boot: remove unneeded false assignment
When the assignment is missing, the default 0/NULL/false value is used.
So drop the explicit piece in config_load_defaults()

Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
2023-10-06 16:21:01 +01:00
Emil Velikov
498d0cc426 sd-boot: cast away reboot_into_firmware() return type
As mentioned by Lennart, in a commit where I was adding similar piece of
code:

  maybe cast this call to void, to tell static analyzers that we are
  ignoring the return value on purpose, not by accident

Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
2023-10-06 16:20:59 +01:00
Emil Velikov
976904bf26 bootctl: remove all our non-volatile variables on uninstall
Grepping around showed a few extra entries that are not listed in the
remove_loader_variables() function. Namely:
 - BootNext
 - OsIndications
 - LoaderConfigConsoleMode
 - LoaderEntryLastBooted

Of which the latter two are systemd specific, even though they are
undocumented. Ensure they're removed - follow-up commits will add
documentation references.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
2023-10-05 11:50:48 +01:00
Emil Velikov
5ee3c914a4 sd-boot: introduce and use efivar_unset()
Currently some of the code base check for the variable presence before
removing it, and some do not.

More so, in all cases (being updated) we're dealing with non-volatile
variables where changing those attribute to NVRAM wear out.

From what information I could find, there is no definitive answer if the
UEFI implementation will write to the NVRAM even when the variable is
missing.

So add a simple helper that checks for the variable presence before
removing it. While also having a bit cleaner API than the current
efivar_set(..., NULL, ...);

efivar_unset() follows the design from efivar_set*() where it returns an
EFI_STATUS even though its (presently) unused.

v2:
 - add inline comment, use early return

v3:
 - typos? typos!

Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
2023-10-05 09:13:37 +01:00
Lennart Poettering
3820f69713 stub: drop empty line 2023-10-04 17:59:57 +02:00
Luca Boccassi
714cb48a4f docs: fix typo in UEFI_SECURITY.md 2023-10-04 19:12:26 +09:00