Commit Graph

659 Commits

Author SHA1 Message Date
Andrew Gunnerson a2fb807803 Remove all uses of implicit error propagation
Implicit error propagation was originally used because it was convenient
and made it easy to just bubble up errors via the ? operator without
thinking. However, there have been too many situations where this
resulted in error messages that were completely useless in
troubleshooting the problem. "I/O error", even with a specific reason
attached, is useless where there are potentially hundreds of operations
where I/O can fail.

I no longer think implicit error propagation is a good idea, so this
commit removes every single use of #[from] in every custom error type.
There are now many more error variants, allowing more context to be
attached to the underlying errors.

Previously, it was easy to encounter error messages like:

    Caused by:
        0: Failed to patch payload: payload.bin
        1: Failed to patch boot images: boot, init_boot, vendor_boot
        2: Boot image error
        3: I/O error
        4: failed to fill whole buffer

This is a terrible error message because it doesn't mention which of the
3 boot images failed to parse, nor does it mention during which I/O
operation it encountered EOF. With this commit, this sort of information
is now included. For example, if the boot image happened to be truncated
in the middle of the ramdisk, the error message would now be:

    Caused by:
        0: Failed to patch payload: payload.bin
        1: Failed to patch boot images: boot, init_boot, vendor_boot
        2: Failed to load boot image: init_boot
        3: Failed to read boot image data: Boot::V3::ramdisk
        4: failed to fill whole buffer

Changes:

* Remove all uses of #[from] from thiserror-derived error types.
* Errors during parsing and serialization of RSA private keys, RSA
  public keys, and X509 certificates now include the file path.
* Removed unnecessary uses of BufReader and BufWriter when reading and
  writing RSA private keys, RSA public keys, and X509 certificates,
  since they need to be fully read into memory anyway.
* Use ReadFixedSizeExt instead of read_exact() where possible.
* Use &'static str instead of String in error fields where all possible
  values are known at compile-time to avoid unnecessary heap allocation.
* Use ok_or() instead of ok_or_else() to construct errors when the error
  variant uses known data and does not require heap allocation.
* Using DebugString instead of String in error variants that store a
  preformatted debug string.

While working on this commit, an unrelated bug was found and fixed:

* Fix vendor v4 boot images that were truncated within the padding
  following the bootconfig section being treated as valid.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-25 02:34:17 -05:00
Andrew Gunnerson 6de5cb783a CHANGELOG.md: Add entry for PR #398
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-11 22:09:03 -05:00
Andrew Gunnerson 12d6f7f78c util: Wrap std's range types instead of inventing our own
This makes the type potentially more useful outside of just our current
use case of just error messages. Ranges where the starting value is
exclusive are no longer supported, but weren't used anyway.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-11 21:56:13 -05:00
Andrew Gunnerson 3c0a77df21 CHANGELOG.md: Add entry for PR #397
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-11 21:28:20 -05:00
Andrew Gunnerson d1b6bce227 Update bzip2-rs to 0.5.0 and switch to Rust backend implementation
The bzip2-rs library now has a new maintainer (same folks that maintain
the sudo-rs project). Version 0.5.0 was recently released, which
includes some much needed bug fixes, but is missing one final one, so we
still need to keep our fork for now.

This commit also switches the backend implementation from the official C
implementation to a Rust implementation maintained by the bzip2-rs
folks. This leaves xz as the only C dependency remaining in avbroot.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-11 20:23:40 -05:00
Andrew Gunnerson 10e0748d18 CHANGELOG.md: Add entry for PR #395
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-11 03:58:28 -05:00
Andrew Gunnerson 9cf63c7036 Treat Magisk versions newer than latest as supporting all features
Previously, when patching with a Magisk version newer than the latest
supported version and using --ignore-magisk-warnings, the upper bound of
VER_PREINIT_DEVICE and VER_XZ_BACKUP would prevent those features from
being used. It makes more sense to assume that a newer version supports
all the same features as the latest supported version instead.

Issue: #393

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-11 03:53:11 -05:00
Andrew Gunnerson 291b3c887b CHANGELOG.md: Add entry for PR #394
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-11 00:01:39 -05:00
Andrew Gunnerson 5d7eb13fbc Fix crash when ignoring warning about missing preinit device
MagiskRootPatcher was previously assuming that there is a preinit device
value if the Magisk version requires it.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-10 23:58:36 -05:00
Andrew Gunnerson 05cd74719e Version 3.10.0
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
v3.10.0
2024-12-08 19:20:39 -05:00
Andrew Gunnerson 7e0d5584d9 CHANGELOG.md: Add entry for PR #392
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-08 19:01:08 -05:00
Andrew Gunnerson b351d47f28 Update dependencies and fix most pedantic clippy warnings
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-08 18:46:40 -05:00
Andrew Gunnerson 421b0a5207 CHANGELOG.md: Add entry for PR #391
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-08 17:14:21 -05:00
Andrew Gunnerson cad08a6a2f Add support for Magisk 28100
There is nothing new that breaks compatibility with avbroot.

Fixes: #389

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-08 02:21:05 -05:00
Andrew Gunnerson 0a32dfaec3 CHANGELOG.md: Add entry for PR #390
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-08 02:19:46 -05:00
Andrew Gunnerson b03b2ade6a Improve parser error messages
* Remove the ReadString trait and perform UTF-8 conversions explicitly.
  UTF-8 conversion errors are now individual errors instead of being
  hidden in an Io error.
* Add new IntOutOfBounds errors that include the actual field value and
  the range of values that are valid for the field. Integer casting also
  uses this error type where possible.
* Replace remaining uses of FieldOutOfBounds with IntOverflow, which is
  returned when checked arithmetic fails. For code simplicity, it does
  not store intermediate values.
* Replace ReadFieldError and WriteFieldError with the generic Io error
  to simplify the code. They were used inconsistently anyway.
* Use fully qualified field names in avb and bootimage error messages
  since the same name frequently appears in multiple structs.
* Replace InvalidFieldValue with more specific errors in bootimage.
* Replace all stringly-typed errors in lp and sparse with (very)
  specific errors.
* Fix the wording in a number of errors.
* Add new ReadFixedSizeExt trait for reading fixed size arrays and vecs
  from Read types.
* Remove some fallible casts from u32 to usize and from usize to u64.
  avbroot only supports 32-bit and 64-bit systems anyway.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-08 02:08:28 -05:00
Andrew Gunnerson c198646c7c Add documentation for Android 16's developer switch for 16K page size kernels
avbroot will not support patching these internal OTAs because it
requires modifying filesystems and creating incremental OTAs. However,
it should be possible to do this manually if someone really wants to try
out a 16K page size kernel.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-06 18:09:34 -05:00
Andrew Gunnerson d815a78e9a CHANGELOG.md: Add entry for PR #386
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-01 17:47:31 -05:00
Andrew Gunnerson 99c5800f96 Update dependencies and pin Github Actions actions to specific commits
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-01 17:42:35 -05:00
Andrew Gunnerson c801fbb069 CHANGELOG.md: Add entry for PR #385
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-01 17:37:54 -05:00
Andrew Gunnerson 0b250086a3 protobuf: Update update_metadata.proto from AOSP
Neither of the two new fields need any special handling in avbroot. This
just allows us to preserve the values from the original OTA.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-01 17:33:34 -05:00
Andrew Gunnerson f6c6c8ab40 CHANGELOG.md: Add entry for PR #384
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-01 17:27:38 -05:00
Andrew Gunnerson 5c353a5f42 Remove unused byteorder crate
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-01 17:04:57 -05:00
Andrew Gunnerson ad932ed445 sparse: Switch to read_from_io()/write_to_io()
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-01 17:04:44 -05:00
Andrew Gunnerson d04d5451dc stream: Remove unused padded string functions
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-01 17:04:30 -05:00
Andrew Gunnerson 1d5db9f731 bootimage: Switch to zerocopy
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-01 17:04:28 -05:00
Andrew Gunnerson 409b9298a5 avb: Switch to zerocopy
This commit also fixes a bug where avb::Header::release_string was
allowed to take the full 48-bytes, which was incorrect because libavb
expects the field to be NULL-terminated. This was not a problem in
practice because the release string is usually short and even if it
wasn't, the 80 reserved bytes that immediately follow it are all zeros.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-01 17:01:43 -05:00
Andrew Gunnerson 516238d907 hashtree: Switch to zerocopy
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-01 16:59:56 -05:00
Andrew Gunnerson 39d5fbf76d payload: Switch to zerocopy
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-01 16:58:35 -05:00
Andrew Gunnerson 20f5bdacc7 compression: Switch to zerocopy
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-01 16:58:08 -05:00
Andrew Gunnerson 5b8eefa867 fec: Switch to zerocopy
This commit also adds support for parsing FEC images with unknown extra
custom fields.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-01 16:57:55 -05:00
Andrew Gunnerson b2610fa0e9 Update zerocopy to 0.8.11
>=0.8.10 is needed for the new read_from_io()/write_to_io() helper
functions.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-01 16:57:39 -05:00
Andrew Gunnerson 8a5550a43c Add new traits for zero padding and unpadding strings
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-12-01 16:56:55 -05:00
Ivan Katrovsky e16f4c7fa7 README.ru.md: update translation
* https://github.com/chenxiaolong/avbroot/commit/10c425dedea262678fda3c0c097b82fdf4352f08
2024-11-13 17:54:14 +03:00
Andrew Gunnerson 983e6c40a5 Version 3.9.0
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
v3.9.0
2024-11-11 20:04:59 -05:00
Andrew Gunnerson 8729854727 CHANGELOG.md: Add entry for PR #377
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-11-11 19:55:44 -05:00
Andrew Gunnerson 0d1beb7734 Update dependencies
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-11-11 19:54:50 -05:00
Andrew Gunnerson 2cd0d69238 CHANGELOG.md: Add entry for PR #376
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-11-11 19:43:50 -05:00
Andrew Gunnerson c0e65264e5 sparse: Allow parsing files with unknown fields
The official AOSP implementation does, so we should too. This allows
unpacking Samsung's sparse images, which have an extra 4 bytes in both
the file header and chunk headers.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-11-11 19:41:27 -05:00
Andrew Gunnerson 37b15a2b6a CHANGELOG.md: Add entry for PR #374
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-11-08 17:38:56 -05:00
Andrew Gunnerson e4994fbe98 format/payload: Allow verifying signatures without an unpadded size
Older OTAs created before the payload metadata format supported EC
signatures will not have the `unpadded_signature_size` field set. In
this case, we'll just use the full length of `data`, which is what
update_engine also does.

Issue: #366

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-11-08 17:38:28 -05:00
Andrew Gunnerson cf1bacab30 CHANGELOG.md: Add entry for PR #373
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-11-08 17:27:24 -05:00
Andrew Gunnerson 4832121160 format/ota: Allow verifying OTAs without metadata.pb
While patched OTAs produced by avbroot always include the protobuf
version of the OTA metadata, the original OTA may not. Verifying those
with `avbroot ota verify` is a valid use case.

Issue: #366

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-11-08 17:25:00 -05:00
Andrew Gunnerson c614e61744 CHANGELOG.md: Add entry for PR #371
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-11-07 22:13:45 -05:00
Andrew Gunnerson 9ebce1666c Add option to skip verifying recovery's OTA cert too
Issue: #366

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-11-07 22:04:11 -05:00
Andrew Gunnerson fb34198ffc CHANGELOG.md: Add entry for PR #370
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-11-05 18:00:13 -05:00
Andrew Gunnerson 4a5eab4ba0 crypto: Fix minor clippy warning
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-11-05 17:59:26 -05:00
Andrew Gunnerson 1e1818ad8f CHANGELOG.md: Add entry for PR #369
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-11-05 17:58:59 -05:00
Andrew Gunnerson 3f25ad7c76 Add more context for avb::Header::set_algo_for_key() calls
Previously, it was not always obvious which key was problematic.

Issue: #366

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-11-05 17:53:01 -05:00
Andrew Gunnerson 37e28eb040 CHANGELOG.md: Add entry for PR #367
Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
2024-11-05 17:51:41 -05:00