438 Commits

Author SHA1 Message Date
Simon Glass
8ff7005997 blkmap: Handle read-only slices in write path
Some blkmap slices (like blkmap_crypt) don't support writes and have
their write function set to NULL. The blkmap_blk_write_slice() function
calls the write function without checking if it's NULL, causing a crash
when attempting to write to such slices.

Add a NULL check before calling the write function. When the slice
doesn't support writes, return 0 to indicate no blocks were written.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2026-01-01 14:27:30 -07:00
Simon Glass
8d033bde28 luks: Check for out-of-memory with Argon2
This algorithm can use a lot of memory, so add a check for this condition
and return the correct error.

Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-11-17 06:59:47 -07:00
Simon Glass
48dd57511c luks: Support a pre-derived key
Update luks_unlock() to support a pre-derived key, such as that obtained
from a TKey. This must match the key_size of the LUKS partition,
otherwise it will fail to unlock.

Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-11-17 06:59:47 -07:00
Simon Glass
c8b4ede816 luks: Support a pre-derived key with LUKSv2
In some cases we may wish to provide a pre-derived key, e.g. obtained
from a TKey. Provide an option for this with LUKSv2. For now it is not
exported.

Improve the error-return documentation while we are here.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-11-17 06:59:47 -07:00
Simon Glass
c5dcc1c39f luks: Support a pre-derived key with LUKSv1
In some cases we may wish to provide a pre-derived key, e.g. obtained
from a TKey. Provide an option for this with LUKSv1. For now it is not
exported.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-11-17 06:59:47 -07:00
Simon Glass
6fc61beecb luks: Split LUKSv1 unlock code into a separate function
Move the LUKSv1-specific unlock logic from luks_unlock() into a new
unlock_luks1() function, lining up with the structure used for LUKSv2.

Also update unlock_luks1() to use a local key_size variable and only
set the output parameter on success.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-11-17 06:59:47 -07:00
Simon Glass
3caa43da35 luks: Move key derivation to the caller of try_keyslot()
Move the derive_key_pbkdf2() call from inside try_keyslot() to the
caller, luks_unlock()

With this change luks_unlock() deals with key derivation and
try_keyslot() only handles the decryption part, using a supplied
derived key.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-11-17 06:59:47 -07:00
Simon Glass
dba921b736 luks: Extract PBKDF2 key derivation into separate function
Create a new derive_key_pbkdf2() function to handle key derivation, to
allow this be called from other places and to reduce the size of
try_keyslot()

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-11-17 06:59:47 -07:00
Simon Glass
c65703bfb9 luks: Update luks_unlock() to take binary passphrase
Update luks_unlock() to accept a binary passphrase, to match the LUKS2
implementation.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-11-17 06:59:47 -07:00
Simon Glass
19354111d0 luks: Update unlock_luks2() to take binary passphrase
Update unlock_luks2() and related functions to accept a binary
passphrase instead of a string passphrase. This will allow unlocking
using hashed data.

For now this is internal to the luks implementation.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-11-17 06:59:47 -07:00
Simon Glass
efc838e1d2 luks: Update try_keyslot() to allow a bytestring
In preparation for supporting unlock using binary data, update
try_keyslot() to accept an array of bytes instead of a string.

Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-11-17 06:59:47 -07:00
Simon Glass
78c21d6d81 luks: Tidy up debugging of unlock
Name the derived key as such to avoid confusion. Fix a typo in nearby
log_debug() statement.

Update essiv_decrypt() to have a const * for its first argument.

Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-11-17 06:59:47 -07:00
Simon Glass
21c8e23ac7 luks: Make essiv_decrypt() a shared function
Both luks.c and luks2.c have similar implementations of essiv_decrypt().
Drop the version in the later to reduce code duplication.

Drop the duplicate function comments while we are here, since exported
functions should have the information in the header file.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-11-17 06:59:47 -07:00
Simon Glass
a2b25780a4 luks: Add XTS cipher mode support for LUKS2
Add support for AES-XTS cipher mode in addition to the existing
AES-CBC-ESSIV support. This is the default cipher for LUKS2 volumes.

The cipher mode (CBC/XTS) is obtained from the LUKS1 cipher_mode or
LUKS2 encryption metadata.

XTS mode uses 512-byte block numbers for IV generation (plain64),
matching dm-crypt behavior. LUKS2 typically uses 4096-byte sectors
for XTS encryption but the IV is based on 512-byte block numbers.

Fix the blkmap-size calculation to exclude the LUKS header/payload
offset.

Update the LUKSv2 test to check reading a file.

Series-to: concept
Cover-letter:
luks: Support the AES-XTS cipher mode
This series finishes off the implementation of LUKSv2, adding support
for the common cipher mode and testing that files can be read from the
disk.

It includes a fix for using the correct size when mapping the crypt, as
well as some refactoring to split up the code a little better.
END

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-11-12 05:20:11 -07:00
Simon Glass
ed94d01b71 luks: Split out crypt reading into its own function
In preparation for adding support for a new algorithm, move the
decryption part of blkmap_crypt_read() into its own function.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-11-12 05:20:11 -07:00
Simon Glass
3393e4d6ce luks: Move blkmap crypt code into a separate file
Create a new blkmap_crypt.c file to hold the LUKS code, since it is
fairly large. Add an internal header for blkmap as well.

Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-11-12 05:20:10 -07:00
Simon Glass
815974c475 luks: Exclude the payload from the size calculation
Fix the blkmap-size calculation to exclude the LUKS header/payload
offset. This was missed in the initial implementation.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
Fixes: 0cbfb2d490 ("luks: Provide a way to unlock and map encrypted..")
2025-11-12 05:02:37 -07:00
Simon Glass
f0d3a7d5f9 luks: Provide an implementation of luks2
Add supports for luks v2 which is a more common version used on modern
systems.

This makes use of Argon2 and also the JSON->FDT parser.

Enable this feature for sandbox, tidying up the defconfig while we are
here.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-11-11 04:35:08 -07:00
Simon Glass
4b2a5ac9a4 luks: Tidy up the code style in the block driver
In preparation for luks v2, make a few code-style tweaks:
- shorter vars in some cases
- 80cols in a few places
- drop an unwanted blank line
- use 'pass' instead of 'passphrase'
- unnecessary assignments to NULL

Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-11-11 04:34:51 -07:00
Simon Glass
cc31300b5c luks: Export the af_merge() function
Provide this function through an internal header, so that luks2 will be
able to use it.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-11-11 04:16:43 -07:00
Simon Glass
0cbfb2d490 luks: Provide a way to unlock and map encrypted partitions
Add the logic to unlock a partition and set up a blkmap for use with it.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <sjg@chromium.org>
2025-10-24 21:02:11 +01:00
Simon Glass
1f29c19082 luks: Enhance blkmap to support LUKSv1
Enhance blkmap to support decrypting a partition encrypted with LUKS
version 1. This will allow filesystems to access files on the parition.

This will be tested once filesystems support is plumbed in.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <sjg@chromium.org>
2025-10-24 21:02:11 +01:00
Simon Glass
22826bf21f luks: Show the JSON information for LUKSv2
Extract the full information for version 2, which is JSON format. Show
this with the 'luks info' command.

Use the mmc12 disk to check this.

Require the JSON for LUKS.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <sjg@chromium.org>
2025-10-24 21:02:11 +01:00
Simon Glass
414baddf37 luks: Add a simple command
Add a 'luks' command which allows querying a partition to see if it is
encrypted using LUKS, as well as showing information about a LUKS
partition.

Provide some documentation and a test.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <sjg@chromium.org>
2025-10-24 20:12:40 +01:00
Simon Glass
8410d62604 luks: Add the beginning of LUKS support
Linux Unified Key Setup (LUKS) provides a way to encryption a disk
partition with a a key an later unlock it. There are two versions (1 and
2).

Add a definition of the main structures and the ability to detect a LUKS
partition.

Enable this for the sandbox board.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <sjg@chromium.org>
2025-10-24 20:12:40 +01:00
Simon Glass
86f23ce420 scsi: Provide some response flags
Rather than open-coding the SCSI-inquiry-response flags, add an enum and
use that.

Signed-off-by: Simon Glass <sjg@chromium.org>
2025-07-02 13:07:47 -06:00
Simon Glass
235585cbc3 part: Detect errors when reading partition tables
The logic for part_init() is not ideal, since it silently ignores read
errors. Each partition type tries to read the disk and presumably fails
as well. No error is reported, however, so there is no indication that
anything is wrong.

Update the function to return an error, and update each of the probe
functions to do the same, trying to minimise changes.

Signed-off-by: Simon Glass <sjg@chromium.org>
2025-07-02 12:45:23 -06:00
Simon Glass
ddaa12e605 sandbox: Update os_open() to return an error code
It is convenient to be able to report the system error when available.
Update os_open() to return it.

Signed-off-by: Simon Glass <sjg@chromium.org>
2025-06-28 07:53:25 -06:00
Simon Glass
d40e88a868 blk: Use hex indices for the device name
It is confusing to use decimal values in device names. For example, with
virtio:

  => dm tree
  ...
  pci           5  [ + ]   pci_bridge_drv        |   |-- pci_0:1.5
    virtio        5  [ + ]   virtio-pci.m          |   |   `-- virtio-pci.m#5
   blk           0  [ + ]   virtio-blk            |   |       |-- virtio-blk#5
   partition     0  [ + ]   blk_partition         |   |       |   |-- virtio-blk#5:1
   partition     1  [ + ]   blk_partition         |   |       |   |-- virtio-blk#5:14
   partition     2  [ + ]   blk_partition         |   |       |   |-- virtio-blk#5:15
   partition     3  [ + ]   blk_partition         |   |       |   `-- virtio-blk#5:16
   bootdev       2  [ + ]   virtio_bootdev        |   |       `-- virtio-blk#5.bootdev
   pci           6  [ + ]   pci_bridge_drv        |   |-- pci_0:1.6
   ...
  => ls virtio 0:14
  ** Invalid partition 20 **
  Couldn't find partition virtio 0:14

Fix this by using hex for both the block-device number and the partition.

Signed-off-by: Simon Glass <sjg@chromium.org>
2025-06-28 07:53:25 -06:00
Simon Glass
8fdae6fe54 boot: sandbox: Provide a bootdev for the host uclass
Support standard boot on a host device by adding a bootdev. This allows
booting from disk images using sandbox.

Signed-off-by: Simon Glass <sjg@chromium.org>
2025-06-05 14:42:47 -06:00
Simon Glass
538deb46b7 efi: Add a bootdev for EFI media
Bootstd needs a driver so that it can scan EFI media within the EFI app.
Add one.

Signed-off-by: Simon Glass <sjg@chromium.org>
2025-06-02 08:25:41 -06:00
Simon Glass
284c266140 efi: Rename the media device
The current name of 'efi_media' is annoying in that it must be given
with 'bootflow scan'. We would prefer to use 'bootflow scan efi', for
example.

Rename the driver to 'efi'.

Signed-off-by: Simon Glass <sjg@chromium.org>
2025-06-02 08:25:41 -06:00
Simon Glass
ca1f2dbd8e efi: Rename CONFIG_EFI to CONFIG_EFI_CLIENT
The generic name 'EFI' would be more useful for common EFI features. At
present it just refers to the EFI app and stub, which is confusing.

Rename it to EFI_CLIENT

Signed-off-by: Simon Glass <sjg@chromium.org>
2025-05-28 08:02:11 +01:00
Tom Rini
ad42ea0175 blk: Make block subsystems select BLK
The BLK symbol has a few meanings, one of which is that it controls the
driver model portion of a "block device". Rather than having this hidden
symbol be "default y if ..." it should be select'd by the various block
subsystems. Symbols such as PVBLOCK which already select'd BLK are
unchanged".

Reviewed-by: Peter Robinson <pbrobinson@gmail.com>
Reviewed-by: Quentin Schulz <quentin.schulz@cherry.de>
Signed-off-by: Tom Rini <trini@konsulko.com>
2025-05-16 14:45:25 +02:00
Tom Rini
2825b387b0 Kbuild: Always use $(PHASE_)
It is confusing to have both "$(PHASE_)" and "$(XPL_)" be used in our
Makefiles as part of the macros to determine when to do something in our
Makefiles based on what phase of the build we are in. For consistency,
bring this down to a single macro and use "$(PHASE_)" only.

Signed-off-by: Tom Rini <trini@konsulko.com>
2025-05-01 05:56:48 -06:00
Heinrich Schuchardt
db7e81154e block: make blk_create_device() static
There are no users of the blk_create_device() function outside the uclass.
Let's make it static. This will ensure that new block drivers will use
blk_create_devicef().

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
2024-10-24 11:20:58 -06:00
Heinrich Schuchardt
9c19c13bda rockchip: block: simplify rkmtd driver
By using blk_create_devicef() instead of blk_create_devicef() the driver
can be simplified and brought into line with other block device drivers.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Johan Jonker <jbx6244@gmail.com>
2024-10-24 11:20:45 -06:00
Tom Rini
47e544f576 Merge patch series "Tidy up use of 'SPL' and CONFIG_SPL_BUILD"
Simon Glass <sjg@chromium.org> says:

When the SPL build-phase was first created it was designed to solve a
particular problem (the need to init SDRAM so that U-Boot proper could
be loaded). It has since expanded to become an important part of U-Boot,
with three phases now present: TPL, VPL and SPL

Due to this history, the term 'SPL' is used to mean both a particular
phase (the one before U-Boot proper) and all the non-proper phases.
This has become confusing.

For a similar reason CONFIG_SPL_BUILD is set to 'y' for all 'SPL'
phases, not just SPL. So code which can only be compiled for actual SPL,
for example, must use something like this:

   #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD)

In Makefiles we have similar issues. SPL_ has been used as a variable
which expands to either SPL_ or nothing, to chose between options like
CONFIG_BLK and CONFIG_SPL_BLK. When TPL appeared, a new SPL_TPL variable
was created which expanded to 'SPL_', 'TPL_' or nothing. Later it was
updated to support 'VPL_' as well.

This series starts a change in terminology and usage to resolve the
above issues:

- The word 'xPL' is used instead of 'SPL' to mean a non-proper build
- A new CONFIG_XPL_BUILD define indicates that the current build is an
  'xPL' build
- The existing CONFIG_SPL_BUILD is changed to mean SPL; it is not now
  defined for TPL and VPL phases
- The existing SPL_ Makefile variable is renamed to SPL_
- The existing SPL_TPL Makefile variable is renamed to PHASE_

It should be noted that xpl_phase() can generally be used instead of
the above CONFIGs without a code-space or run-time penalty.

This series does not attempt to convert all of U-Boot to use this new
terminology but it makes a start. In particular, renaming spl.h and
common/spl seems like a bridge too far at this point.

The series is fully bisectable. It has also been checked to ensure there
are no code-size changes on any commit.
2024-10-11 12:23:25 -06:00
Simon Glass
5c10c8badf global: Rename SPL_TPL_ to PHASE_
Use PHASE_ as the symbol to select a particular XPL build. This means
that SPL_TPL_ is no-longer set.

Update the comment in bootstage to refer to this symbol, instead of
SPL_

Signed-off-by: Simon Glass <sjg@chromium.org>
2024-10-11 11:44:48 -06:00
Simon Glass
c46760d596 global: Rename SPL_ to XPL_
Use XPL_ as the symbol to indicate an SPL build. This means that SPL_ is
no-longer set.

Signed-off-by: Simon Glass <sjg@chromium.org>
2024-10-11 11:44:48 -06:00
Simon Glass
371dc068bb drivers: Use CONFIG_XPL_BUILD instead of CONFIG_SPL_BUILD
Use the new symbol to refer to any 'SPL' build, including TPL and VPL

Signed-off-by: Simon Glass <sjg@chromium.org>
2024-10-11 11:44:48 -06:00
Hiago De Franco
a280368b48 fs: Fix SPL build if SPL_FS_LOADER is enabled and FS_LOADER is disabled
When SPL_FS_LOADER is set to y and FS_LOADER is not enabled, the SPL build
fails with the following errors:

  AR      spl/boot/built-in.o
  LD      spl/u-boot-spl
arm-none-linux-gnueabihf-ld.bfd: drivers/misc/fs_loader.o: in function
`fw_get_filesystem_firmware':
/u-boot/drivers/misc/fs_loader.c:162: undefined reference to
`fs_set_blk_dev'
arm-none-linux-gnueabihf-ld.bfd: /home/frh/tdx/src/u-boot/drivers/misc/
fs_loader.c:185: undefined reference to `fs_read'
arm-none-linux-gnueabihf-ld.bfd: drivers/misc/fs_loader.o: in function
`select_fs_dev':
/u-boot/drivers/misc/fs_loader.c:89: undefined reference to
`fs_set_blk_dev_with_part'
make[1]: *** [scripts/Makefile.spl:527: spl/u-boot-spl] Error 1
make: *** [Makefile:2055: spl/u-boot-spl] Error 2

Fix it by replacing the FS_LOADER with SPL_FS_LOADER in the Makefile, so
the fs.c with the necessary function definitions are compiled.

Fixes: b071a07743 ("drivers: misc: Makefile: Enable fs_loader compilation at SPL Level")
Suggested-by: Francesco Dolcini <francesco.dolcini@toradex.com>
Signed-off-by: Hiago De Franco <hiago.franco@toradex.com>
Signed-off-by: Francesco Dolcini <francesco.dolcini@toradex.com>
2024-10-02 13:35:31 -06:00
Tom Rini
158cf0270c Merge tag 'v2024.10-rc3' into next
Prepare v2024.10-rc3
2024-08-19 18:24:58 -06:00
Simon Glass
b254a8359e sandbox: Return error code from read/write/seek
The existing API for these functions is different from the rest of
U-Boot, in that any error code must be obtained from the errno variable
on failure. This variable is part of the C library, so accessing it
outside of the special 'sandbox' shim-functions is not ideal.

Adjust the API to return an error code, to avoid this. Update existing
uses to check for any negative value, rather than just -1.

Signed-off-by: Simon Glass <sjg@chromium.org>
2024-08-09 16:03:19 -06:00
Alexey Romanov
9daad11ad1 drivers: introduce UBI block abstraction
UBI block is an virtual device, that runs on top
of the MTD layer. The blocks are UBI volumes.
Intended to be used in combination with other MTD
drivers.

Despite the fact that it, like mtdblock abstraction,
it used with UCLASS_MTD, they can be used together
on the system without conflicting. For example,
using bcb command:

  # Trying to load bcb via mtdblock:
  $ bcb load mtd 0 mtd_partition_name

  # Trying to load bcb via UBI block:
  $ bcb load ubi 1 ubi_volume_name

User always must attach UBI layer (for example, using
ubi_part()) before using UBI block device.

Signed-off-by: Alexey Romanov <avromanov@salutedevices.com>
Reviewed-by: Heiko Schocher <hs@denx.de>
Acked-by: Heiko Schocher <hs@denx.de>
Signed-off-by: Michael Trimarchi <michael@amarulasolutions.com>
2024-08-08 09:28:00 +02:00
Alexey Romanov
e108d10d8c drivers: introduce mtdblock abstraction
MTD block - abstraction over MTD subsystem, allowing
to read and write in blocks using BLK UCLASS.

Signed-off-by: Alexey Romanov <avromanov@salutedevices.com>
Signed-off-by: Michael Trimarchi <michael@amarulasolutions.com>
2024-08-08 09:27:50 +02:00
Marek Vasut
13056cb1e5 drivers: block: Remove duplicate newlines
Drop all duplicate newlines. No functional change.

Signed-off-by: Marek Vasut <marek.vasut+renesas@mailbox.org>
2024-07-22 10:53:04 -06:00
Tom Rini
fd0ab46ead block: Update BLK to be def_bool
At this point in the DM migration, all platforms enable DM. BLK requires
DM. Make BLK "def_bool y" in the cases it had been "default y" to make
this clearer. Now remove the symbol requirement from other places as it
is redundant here.

Signed-off-by: Tom Rini <trini@konsulko.com>
2024-06-14 10:39:35 -06:00
Tom Rini
03de305ec4 Restore patch series "arm: dts: am62-beagleplay: Fix Beagleplay Ethernet"
As part of bringing the master branch back in to next, we need to allow
for all of these changes to exist here.

Reported-by: Jonas Karlman <jonas@kwiboo.se>
Signed-off-by: Tom Rini <trini@konsulko.com>
2024-05-20 13:35:03 -06:00
Tom Rini
d678a59d2d Revert "Merge patch series "arm: dts: am62-beagleplay: Fix Beagleplay Ethernet""
When bringing in the series 'arm: dts: am62-beagleplay: Fix Beagleplay
Ethernet"' I failed to notice that b4 noticed it was based on next and
so took that as the base commit and merged that part of next to master.

This reverts commit c8ffd1356d, reversing
changes made to 2ee6f3a5f7.

Reported-by: Jonas Karlman <jonas@kwiboo.se>
Signed-off-by: Tom Rini <trini@konsulko.com>
2024-05-19 08:16:36 -06:00